File: test_audio_fmt_convert.c

package info (click to toggle)
rat 4.2.22-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 3,717
  • sloc: ansic: 36,542; tcl: 2,740; sh: 2,675; makefile: 295
file content (152 lines) | stat: -rw-r--r-- 4,699 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "config_unix.h"
#include "config_win32.h"

#include "audio_types.h"
#include "audio_fmt.h"

#define SAMPLES_PER_BLOCK 16

/* Memory debugging.  To check conversion routines do not overstep bounds  */
/* Could have used common library routines, but they depend on being built */
/* with -DDEBUG_MEM */

/* Magic alloc ->  | size | Magic No | Memory ....| Magic no. | */

#define OUR_MAGIC_NO 0xdeadbeef

static int magic_check(void *);

static void*
magic_alloc(size_t size)
{
        uint32_t *m, *s;
        void *blk; 
        uint8_t  *t;
        
        s = (uint32_t*)malloc(size * 3 * sizeof(uint32_t));
        *s = size;
        m  = s + 1;
        *m = OUR_MAGIC_NO;
        t = ((uint8_t*)s) + 2 * sizeof(uint32_t) + size;
        memcpy(t, m, sizeof(uint32_t));

        blk = (void*)s;
        blk += 2 * sizeof(uint32_t);

        memset(blk, 0, size);
        assert(magic_check(blk));
        return blk;
}

static int
magic_check(void *blk)
{
        uint8_t  *t;
        uint32_t *m, *s;

        s = ((uint32_t*)blk) - 2;
        m = s + 1;
        if (*m != OUR_MAGIC_NO) {
                return FALSE;
        }
        t = ((uint8_t*)s) + 2 * sizeof(uint32_t) + *s;

        return !memcmp(m, t, sizeof(uint32_t));
}

static void
magic_free(void *blk)
{
        uint32_t *s = (uint32_t*)(blk - 2 * sizeof(uint32_t));
        free(s);
}

static int
test_sample_conversion(audio_format *fmtin, audio_format *fmtout)
{
        uint8_t *bufin, *bufout;
        int success;

        bufin  = (uint8_t*)magic_alloc(fmtin->bytes_per_block);
        bufout = (uint8_t*)magic_alloc(fmtout->bytes_per_block);

        success = magic_check(bufin) & magic_check(bufout);
        if (success == 0) {
                printf("Test is wrong\n");
                return FALSE;
        }

        audio_format_buffer_convert(fmtin,  bufin,  fmtin->bytes_per_block,
                                    fmtout, bufout, fmtout->bytes_per_block);

        success = 0;
        if (magic_check(bufin) == 0) {
                printf("Input buffer corrupted.\n");
        } else if (magic_check(bufout) == 0) {
                printf("Output buffer corrupted.\n");
        } else {
                success = TRUE;
        }
        
        magic_free(bufin);
        magic_free(bufout);
        return success;
}

static int 
test_sample_conversions(void)
{
        const deve_e encodings[] = {
                DEV_PCMU, DEV_PCMA, DEV_S8, DEV_U8, DEV_S16
        };
        const int    encoding_width[] = {
                8,        8,        8,      8,      16,
        };

        char fmtname[64];

        int ein, eout, msin, msout;
        int num_encodings = sizeof(encodings)/sizeof(encodings[0]);
        audio_format fmtin, fmtout;

        for(msin = 1; msin <= 2; msin++) {
                for(ein = 0; ein < num_encodings; ein++) {
                        fmtin.encoding        = encodings[ein];
                        fmtin.sample_rate     = 8000;
                        fmtin.bits_per_sample = encoding_width[ein];
                        fmtin.channels        = msin;
                        fmtin.bytes_per_block = SAMPLES_PER_BLOCK * fmtin.channels * fmtin.bits_per_sample / 8;
                        audio_format_name(&fmtin, fmtname, sizeof(fmtname));
                        printf("\n\tFrom: %s to:", fmtname);
                        for(msout = 1; msout <= 2; msout++) {
                                
                                for(eout = 0; eout < num_encodings; eout++) {
                                        fmtout.encoding        = encodings[eout];
                                        fmtout.sample_rate     = 8000;
                                        fmtout.bits_per_sample = encoding_width[eout];
                                        fmtout.channels        = msout;
                                        fmtout.bytes_per_block = SAMPLES_PER_BLOCK * fmtout.channels * fmtout.bits_per_sample / 8;
                                        audio_format_name(&fmtout, fmtname, sizeof(fmtname));
                                        printf("\n\t\t%s", fmtname);

                                        if (test_sample_conversion(&fmtin, &fmtout) == FALSE) {
                                                printf("....failed.");
                                        }
                                        printf("....passed");
                                }       
                        }
                }
        }

        return TRUE;
}

int main()
{
        printf("Testing sample type conversions");
        if (test_sample_conversions() == FALSE) {
                printf("failed\n");
        }
        return 0;
}