1 /* (C) 2009 Gregory Maxwell
3 This test runs pink noise through the encoder and decoder many times
4 while feeding the output back into the input. It checks that after
5 a number of cycles the energy has not increased or decreased by too
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
12 - Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
15 - Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
19 - Neither the name of the Xiph.org Foundation nor the names of its
20 contributors may be used to endorse or promote products derived from
21 this software without specific prior written permission.
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/types.h>
51 int async_tandem(int rate, int frame_size, int channels, int bitrate_min,
54 unsigned char data[250];
55 CELTMode *mode = NULL;
62 int ret, i, j, bytes_per_frame;
65 bmin = floor((bitrate_min / (rate / (float) frame_size)) / 8.0);
66 bmax = ceil((bitrate_max / (rate / (float) frame_size)) / 8.0);
74 /*increment += (bmax - bmin) / 64; */
76 printf ("Testing asynchronous tandeming (%dHz, %dch, %d samples, %d - %d bytes).\n",
77 rate, channels, frame_size, bmin, bmax);
79 mode = celt_mode_create(rate, frame_size, NULL);
81 fprintf(stderr, "Error: failed to create a mode\n");
85 dec = celt_decoder_create(mode, channels, NULL);
86 enc = celt_encoder_create(mode, channels, NULL);
88 for (j = 0; j < frame_size * channels; j++)
91 for (bytes_per_frame = bmin; bytes_per_frame <= bmax;
92 bytes_per_frame += increment) {
94 /*Prime the encoder and decoder */
95 for (i = 0; i < (1024 + (frame_size >> 1)) / frame_size + 2; i++) {
97 for (j = 0; j < channels; j++)
98 pcm[j] = pcm[frame_size * channels - (channels - j + 1)];
99 for (j = channels; j < frame_size * channels - 1; j++)
100 pcm[j] = ((rand() % 4096) - 2048) + .9 * pcm[j - channels];
102 ret = celt_encode(enc, pcm, frame_size, data, bytes_per_frame);
103 if (ret != bytes_per_frame) {
104 fprintf(stderr, "Error: during init celt_encode returned %d\n", ret);
108 ret = celt_decode(dec, data, ret, pcm, frame_size);
109 if (ret != CELT_OK) {
110 fprintf(stderr, "Error: during init celt_decode returned %d\n", ret);
114 for (j = 0; j < channels; j++)
115 pcm[j] = pcm[frame_size * channels - (channels - j)];
116 for (j = channels; j < frame_size * channels - 1; j++)
117 pcm[j] = ((rand() % 4096) - 2048) + .9 * pcm[j - channels];
119 for (i = 0; i < 8; i++) {
120 for (j = 0; j < channels; j++)
121 carry[j] = pcm[frame_size * channels - (channels - j)];
122 memmove(pcm + channels, pcm, sizeof(short) * frame_size * channels);
123 for (j = 0; j < channels; j++)
126 ret = celt_encode(enc, pcm, frame_size, data, bytes_per_frame);
127 if (ret != bytes_per_frame) {
128 fprintf(stderr, "Error: at %d bytes_per_frame celt_encode returned %d\n",
129 bytes_per_frame, ret);
133 ret = celt_decode(dec, data, ret, pcm, frame_size);
134 if (ret != CELT_OK) {
135 fprintf(stderr, "Error: at %d bytes_per_frame celt_decode returned %d\n",
136 bytes_per_frame, ret);
141 for (j = 0; j < frame_size * channels; j++)
142 ms += pcm[j] * pcm[j];
143 ms = sqrt(ms / (frame_size * channels));
144 if (ms > 7000 || ms < 1000) {
145 fprintf(stderr, "Error: Signal out of expected range. %d %d %d %d %f\n",
146 rate, channels, frame_size, bytes_per_frame, ms);
151 celt_encoder_destroy(enc);
152 celt_decoder_destroy(dec);
153 celt_mode_destroy(mode);
158 int main(int argc, char *argv[])
164 fprintf(stderr, "Usage: %s [<seed>]\n", argv[0]);
169 seed = atoi(argv[1]);
171 seed = (time(NULL) ^ ((getpid() % 65536) << 16));
174 printf("CELT codec tests. Random seed: %u (%.4X)\n", seed, rand() % 65536);
176 for (n = 6; n < 10; n++) {
177 for (ch = 1; ch <= 2; ch++) {
178 async_tandem(44100, 1 << n, ch, 47000 * ch, 77000 * ch);
179 async_tandem(48000, 1 << n, ch, 47000 * ch, 77000 * ch);
180 async_tandem(32000, 1 << n, ch, 31000 * ch, 65000 * ch);
184 for (ch = 1; ch <= 2; ch++)
185 async_tandem(32000, 320, ch, 31000 * ch, 65000 * ch);
187 for (ch = 1; ch <= 2; ch++)
188 async_tandem(48000, 480, ch, 31000 * ch, 77000 * ch);