1 /* Copyright (c) 2011-2013 Xiph.Org Foundation
2 Written by Gregory Maxwell */
3 /*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <limits.h>
35 #include <stdint.h>
36 #include <math.h>
37 #include <string.h>
38 #include <time.h>
39 #if (!defined WIN32 && !defined _WIN32) || defined(__MINGW32__)
40 #include <unistd.h>
41 #else
42 #include <process.h>
43 #define getpid _getpid
44 #endif
45 #include "opus_multistream.h"
46 #include "opus.h"
47 #include "../src/opus_private.h"
48 #include "test_opus_common.h"
49
50 #define MAX_PACKET (1500)
51 #define SAMPLES (48000*30)
52 #define SSAMPLES (SAMPLES/3)
53 #define MAX_FRAME_SAMP (5760)
54 #define PI (3.141592653589793238462643f)
55 #define RAND_SAMPLE(a) (a[fast_rand() % sizeof(a)/sizeof(a[0])])
56
generate_music(short * buf,opus_int32 len)57 void generate_music(short *buf, opus_int32 len)
58 {
59 opus_int32 a1,b1,a2,b2;
60 opus_int32 c1,c2,d1,d2;
61 opus_int32 i,j;
62 a1=b1=a2=b2=0;
63 c1=c2=d1=d2=0;
64 j=0;
65 /*60ms silence*/
66 for(i=0;i<2880;i++)buf[i*2]=buf[i*2+1]=0;
67 for(i=2880;i<len;i++)
68 {
69 opus_uint32 r;
70 opus_int32 v1,v2;
71 v1=v2=(((j*((j>>12)^((j>>10|j>>12)&26&j>>7)))&128)+128)<<15;
72 r=fast_rand();v1+=r&65535;v1-=r>>16;
73 r=fast_rand();v2+=r&65535;v2-=r>>16;
74 b1=v1-a1+((b1*61+32)>>6);a1=v1;
75 b2=v2-a2+((b2*61+32)>>6);a2=v2;
76 c1=(30*(c1+b1+d1)+32)>>6;d1=b1;
77 c2=(30*(c2+b2+d2)+32)>>6;d2=b2;
78 v1=(c1+128)>>8;
79 v2=(c2+128)>>8;
80 buf[i*2]=v1>32767?32767:(v1<-32768?-32768:v1);
81 buf[i*2+1]=v2>32767?32767:(v2<-32768?-32768:v2);
82 if(i%6==0)j++;
83 }
84 }
85
86 #if 0
87 static int save_ctr = 0;
88 static void int_to_char(opus_uint32 i, unsigned char ch[4])
89 {
90 ch[0] = i>>24;
91 ch[1] = (i>>16)&0xFF;
92 ch[2] = (i>>8)&0xFF;
93 ch[3] = i&0xFF;
94 }
95
96 static OPUS_INLINE void save_packet(unsigned char* p, int len, opus_uint32 rng)
97 {
98 FILE *fout;
99 unsigned char int_field[4];
100 char name[256];
101 snprintf(name,255,"test_opus_encode.%llu.%d.bit",(unsigned long long)iseed,save_ctr);
102 fprintf(stdout,"writing %d byte packet to %s\n",len,name);
103 fout=fopen(name, "wb+");
104 if(fout==NULL)test_failed();
105 int_to_char(len, int_field);
106 fwrite(int_field, 1, 4, fout);
107 int_to_char(rng, int_field);
108 fwrite(int_field, 1, 4, fout);
109 fwrite(p, 1, len, fout);
110 fclose(fout);
111 save_ctr++;
112 }
113 #endif
114
get_frame_size_enum(int frame_size,int sampling_rate)115 int get_frame_size_enum(int frame_size, int sampling_rate)
116 {
117 int frame_size_enum;
118
119 if(frame_size==sampling_rate/400)
120 frame_size_enum = OPUS_FRAMESIZE_2_5_MS;
121 else if(frame_size==sampling_rate/200)
122 frame_size_enum = OPUS_FRAMESIZE_5_MS;
123 else if(frame_size==sampling_rate/100)
124 frame_size_enum = OPUS_FRAMESIZE_10_MS;
125 else if(frame_size==sampling_rate/50)
126 frame_size_enum = OPUS_FRAMESIZE_20_MS;
127 else if(frame_size==sampling_rate/25)
128 frame_size_enum = OPUS_FRAMESIZE_40_MS;
129 else if(frame_size==3*sampling_rate/50)
130 frame_size_enum = OPUS_FRAMESIZE_60_MS;
131 else if(frame_size==4*sampling_rate/50)
132 frame_size_enum = OPUS_FRAMESIZE_80_MS;
133 else if(frame_size==5*sampling_rate/50)
134 frame_size_enum = OPUS_FRAMESIZE_100_MS;
135 else if(frame_size==6*sampling_rate/50)
136 frame_size_enum = OPUS_FRAMESIZE_120_MS;
137 else
138 test_failed();
139
140 return frame_size_enum;
141 }
142
test_encode(OpusEncoder * enc,int channels,int frame_size,OpusDecoder * dec)143 int test_encode(OpusEncoder *enc, int channels, int frame_size, OpusDecoder *dec)
144 {
145 int samp_count = 0;
146 opus_int16 *inbuf;
147 unsigned char packet[MAX_PACKET+257];
148 int len;
149 opus_int16 *outbuf;
150 int out_samples;
151 int ret = 0;
152 #ifdef ENABLE_DRED
153 OpusDREDDecoder *dred_dec;
154 OpusDRED *dred;
155 int dred_end;
156 int dred_amount;
157 int dred_ret;
158 dred_dec = opus_dred_decoder_create(&dred_ret);
159 dred = opus_dred_alloc(&dred_ret);
160 #endif
161 /* Generate input data */
162 inbuf = (opus_int16*)malloc(sizeof(*inbuf)*SSAMPLES);
163 generate_music(inbuf, SSAMPLES/2);
164
165 /* Allocate memory for output data */
166 outbuf = (opus_int16*)malloc(sizeof(*outbuf)*MAX_FRAME_SAMP*3);
167
168 /* Encode data, then decode for sanity check */
169 do {
170 len = opus_encode(enc, &inbuf[samp_count*channels], frame_size, packet, MAX_PACKET);
171 if(len<0 || len>MAX_PACKET) {
172 fprintf(stderr,"opus_encode() returned %d\n",len);
173 ret = -1;
174 break;
175 }
176 #ifdef ENABLE_DRED
177 dred_amount = opus_dred_parse(dred_dec, dred, packet, len, 48000, 48000, &dred_end, 0);
178 if(dred_amount<0) {
179 fprintf(stderr,"opus_dred_parse() returned %d\n",dred_amount);
180 ret = -1;
181 break;
182 }
183 if (dred_amount >= frame_size && (fast_rand()&1)) {
184 dred_ret = opus_decoder_dred_decode(dec, dred, frame_size, outbuf, frame_size);
185 if(dred_ret<0) {
186 fprintf(stderr,"opus_decoder_dred_decode() returned %d\n",dred_ret);
187 ret = -1;
188 break;
189 }
190 }
191 #endif
192 out_samples = opus_decode(dec, packet, len, outbuf, MAX_FRAME_SAMP, 0);
193 if(out_samples!=frame_size) {
194 fprintf(stderr,"opus_decode() returned %d\n",out_samples);
195 ret = -1;
196 break;
197 }
198
199 samp_count += frame_size;
200 } while (samp_count < ((SSAMPLES/2)-MAX_FRAME_SAMP));
201 #ifdef ENABLE_DRED
202 opus_dred_decoder_destroy(dred_dec);
203 opus_dred_free(dred);
204 #endif
205 /* Clean up */
206 free(inbuf);
207 free(outbuf);
208 return ret;
209 }
210
fuzz_encoder_settings(const int num_encoders,const int num_setting_changes)211 void fuzz_encoder_settings(const int num_encoders, const int num_setting_changes)
212 {
213 OpusEncoder *enc;
214 OpusDecoder *dec;
215 int i,j,err;
216
217 /* Parameters to fuzz. Some values are duplicated to increase their probability of being tested. */
218 int sampling_rates[5] = {8000, 12000, 16000, 24000, 48000};
219 int channels[2] = {1, 2};
220 int applications[3] = {OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY};
221 int bitrates[11] = {6000, 12000, 16000, 24000, 32000, 48000, 64000, 96000, 510000, OPUS_AUTO, OPUS_BITRATE_MAX};
222 int force_channels[4] = {OPUS_AUTO, OPUS_AUTO, 1, 2};
223 int use_vbr[3] = {0, 1, 1};
224 int vbr_constraints[3] = {0, 1, 1};
225 int complexities[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
226 int max_bandwidths[6] = {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
227 OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
228 OPUS_BANDWIDTH_FULLBAND, OPUS_BANDWIDTH_FULLBAND};
229 int signals[4] = {OPUS_AUTO, OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC};
230 int inband_fecs[3] = {0, 0, 1};
231 int packet_loss_perc[4] = {0, 1, 2, 5};
232 int lsb_depths[2] = {8, 24};
233 int prediction_disabled[3] = {0, 0, 1};
234 int use_dtx[2] = {0, 1};
235 int frame_sizes_ms_x2[9] = {5, 10, 20, 40, 80, 120, 160, 200, 240}; /* x2 to avoid 2.5 ms */
236
237 for (i=0; i<num_encoders; i++) {
238 int sampling_rate = RAND_SAMPLE(sampling_rates);
239 int num_channels = RAND_SAMPLE(channels);
240 int application = RAND_SAMPLE(applications);
241
242 dec = opus_decoder_create(sampling_rate, num_channels, &err);
243 if(err!=OPUS_OK || dec==NULL)test_failed();
244
245 enc = opus_encoder_create(sampling_rate, num_channels, application, &err);
246 if(err!=OPUS_OK || enc==NULL)test_failed();
247
248 for (j=0; j<num_setting_changes; j++) {
249 int bitrate = RAND_SAMPLE(bitrates);
250 int force_channel = RAND_SAMPLE(force_channels);
251 int vbr = RAND_SAMPLE(use_vbr);
252 int vbr_constraint = RAND_SAMPLE(vbr_constraints);
253 int complexity = RAND_SAMPLE(complexities);
254 int max_bw = RAND_SAMPLE(max_bandwidths);
255 int sig = RAND_SAMPLE(signals);
256 int inband_fec = RAND_SAMPLE(inband_fecs);
257 int pkt_loss = RAND_SAMPLE(packet_loss_perc);
258 int lsb_depth = RAND_SAMPLE(lsb_depths);
259 int pred_disabled = RAND_SAMPLE(prediction_disabled);
260 int dtx = RAND_SAMPLE(use_dtx);
261 int frame_size_ms_x2 = RAND_SAMPLE(frame_sizes_ms_x2);
262 int frame_size = frame_size_ms_x2*sampling_rate/2000;
263 int frame_size_enum = get_frame_size_enum(frame_size, sampling_rate);
264 force_channel = IMIN(force_channel, num_channels);
265
266 if(opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate)) != OPUS_OK) test_failed();
267 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(force_channel)) != OPUS_OK) test_failed();
268 if(opus_encoder_ctl(enc, OPUS_SET_VBR(vbr)) != OPUS_OK) test_failed();
269 if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(vbr_constraint)) != OPUS_OK) test_failed();
270 if(opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity)) != OPUS_OK) test_failed();
271 if(opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(max_bw)) != OPUS_OK) test_failed();
272 if(opus_encoder_ctl(enc, OPUS_SET_SIGNAL(sig)) != OPUS_OK) test_failed();
273 if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(inband_fec)) != OPUS_OK) test_failed();
274 if(opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(pkt_loss)) != OPUS_OK) test_failed();
275 if(opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(lsb_depth)) != OPUS_OK) test_failed();
276 if(opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED(pred_disabled)) != OPUS_OK) test_failed();
277 if(opus_encoder_ctl(enc, OPUS_SET_DTX(dtx)) != OPUS_OK) test_failed();
278 if(opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(frame_size_enum)) != OPUS_OK) test_failed();
279 #ifdef ENABLE_DRED
280 if(opus_encoder_ctl(enc, OPUS_SET_DRED_DURATION(fast_rand()%101)) != OPUS_OK) test_failed();
281 #endif
282 if(test_encode(enc, num_channels, frame_size, dec)) {
283 fprintf(stderr,
284 "fuzz_encoder_settings: %d kHz, %d ch, application: %d, "
285 "%d bps, force ch: %d, vbr: %d, vbr constraint: %d, complexity: %d, "
286 "max bw: %d, signal: %d, inband fec: %d, pkt loss: %d%%, lsb depth: %d, "
287 "pred disabled: %d, dtx: %d, (%d/2) ms\n",
288 sampling_rate/1000, num_channels, application, bitrate,
289 force_channel, vbr, vbr_constraint, complexity, max_bw, sig, inband_fec,
290 pkt_loss, lsb_depth, pred_disabled, dtx, frame_size_ms_x2);
291 test_failed();
292 }
293 }
294
295 opus_encoder_destroy(enc);
296 opus_decoder_destroy(dec);
297 }
298 }
299
run_test1(int no_fuzz)300 int run_test1(int no_fuzz)
301 {
302 static const int fsizes[6]={960*3,960*2,120,240,480,960};
303 static const char *mstrings[3] = {" LP","Hybrid"," MDCT"};
304 unsigned char mapping[256] = {0,1,255};
305 unsigned char db62[36];
306 opus_int32 i,j;
307 int rc,err;
308 OpusEncoder *enc;
309 OpusMSEncoder *MSenc;
310 OpusDecoder *dec;
311 OpusMSDecoder *MSdec;
312 OpusMSDecoder *MSdec_err;
313 OpusDecoder *dec_err[10];
314 short *inbuf;
315 short *outbuf;
316 short *out2buf;
317 opus_int32 bitrate_bps;
318 unsigned char packet[MAX_PACKET+257];
319 opus_uint32 enc_final_range;
320 opus_uint32 dec_final_range;
321 int fswitch;
322 int fsize;
323 int count;
324
325 /*FIXME: encoder api tests, fs!=48k, mono, VBR*/
326
327 fprintf(stdout," Encode+Decode tests.\n");
328 fflush(stdout);
329
330 enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
331 if(err != OPUS_OK || enc==NULL)test_failed();
332
333 for(i=0;i<2;i++)
334 {
335 int *ret_err;
336 ret_err = i?0:&err;
337 MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_UNIMPLEMENTED, ret_err);
338 if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
339
340 MSenc = opus_multistream_encoder_create(8000, 0, 1, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
341 if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
342
343 MSenc = opus_multistream_encoder_create(44100, 2, 2, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
344 if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
345
346 MSenc = opus_multistream_encoder_create(8000, 2, 2, 3, mapping, OPUS_APPLICATION_VOIP, ret_err);
347 if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
348
349 MSenc = opus_multistream_encoder_create(8000, 2, -1, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
350 if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
351
352 MSenc = opus_multistream_encoder_create(8000, 256, 2, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
353 if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
354 }
355
356 MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_APPLICATION_AUDIO, &err);
357 if(err != OPUS_OK || MSenc==NULL)test_failed();
358
359 /*Some multistream encoder API tests*/
360 if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
361 if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_LSB_DEPTH(&i))!=OPUS_OK)test_failed();
362 if(i<16)test_failed();
363
364 {
365 OpusEncoder *tmp_enc;
366 if(opus_multistream_encoder_ctl(MSenc, OPUS_MULTISTREAM_GET_ENCODER_STATE(1,&tmp_enc))!=OPUS_OK)test_failed();
367 if(opus_encoder_ctl(tmp_enc, OPUS_GET_LSB_DEPTH(&j))!=OPUS_OK)test_failed();
368 if(i!=j)test_failed();
369 if(opus_multistream_encoder_ctl(MSenc, OPUS_MULTISTREAM_GET_ENCODER_STATE(2,&tmp_enc))!=OPUS_BAD_ARG)test_failed();
370 }
371
372 dec = opus_decoder_create(48000, 2, &err);
373 if(err != OPUS_OK || dec==NULL)test_failed();
374
375 MSdec = opus_multistream_decoder_create(48000, 2, 2, 0, mapping, &err);
376 if(err != OPUS_OK || MSdec==NULL)test_failed();
377
378 MSdec_err = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, &err);
379 if(err != OPUS_OK || MSdec_err==NULL)test_failed();
380
381 dec_err[0]=(OpusDecoder *)malloc(opus_decoder_get_size(2));
382 memcpy(dec_err[0],dec,opus_decoder_get_size(2));
383 dec_err[1] = opus_decoder_create(48000, 1, &err);
384 dec_err[2] = opus_decoder_create(24000, 2, &err);
385 dec_err[3] = opus_decoder_create(24000, 1, &err);
386 dec_err[4] = opus_decoder_create(16000, 2, &err);
387 dec_err[5] = opus_decoder_create(16000, 1, &err);
388 dec_err[6] = opus_decoder_create(12000, 2, &err);
389 dec_err[7] = opus_decoder_create(12000, 1, &err);
390 dec_err[8] = opus_decoder_create(8000, 2, &err);
391 dec_err[9] = opus_decoder_create(8000, 1, &err);
392 for(i=0;i<10;i++)if(dec_err[i]==NULL)test_failed();
393
394 {
395 OpusEncoder *enccpy;
396 /*The opus state structures contain no pointers and can be freely copied*/
397 enccpy=(OpusEncoder *)malloc(opus_encoder_get_size(2));
398 memcpy(enccpy,enc,opus_encoder_get_size(2));
399 memset(enc,255,opus_encoder_get_size(2));
400 opus_encoder_destroy(enc);
401 enc=enccpy;
402 }
403
404 inbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
405 outbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
406 out2buf=(short *)malloc(sizeof(short)*MAX_FRAME_SAMP*3);
407 if(inbuf==NULL || outbuf==NULL || out2buf==NULL)test_failed();
408
409 generate_music(inbuf,SAMPLES);
410
411 /* FILE *foo;
412 foo = fopen("foo.sw", "wb+");
413 fwrite(inbuf, 1, SAMPLES*2*2, foo);
414 fclose(foo);*/
415
416 if(opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
417 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(-2))!=OPUS_BAD_ARG)test_failed();
418 if(opus_encode(enc, inbuf, 500, packet, MAX_PACKET)!=OPUS_BAD_ARG)test_failed();
419
420 for(rc=0;rc<3;rc++)
421 {
422 if(opus_encoder_ctl(enc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
423 if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
424 if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
425 if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
426 #ifdef ENABLE_DRED
427 if(opus_encoder_ctl(enc, OPUS_SET_DRED_DURATION(fast_rand()%101)) != OPUS_OK) test_failed();
428 #endif
429 for(j=0;j<13;j++)
430 {
431 int rate;
432 int modes[13]={0,0,0,1,1,1,1,2,2,2,2,2,2};
433 int rates[13]={6000,12000,48000,16000,32000,48000,64000,512000,13000,24000,48000,64000,96000};
434 int frame[13]={960*2,960,480,960,960,960,480,960*3,960*3,960,480,240,120};
435 rate=rates[j]+fast_rand()%rates[j];
436 count=i=0;
437 do {
438 int bw,len,out_samples,frame_size;
439 frame_size=frame[j];
440 if((fast_rand()&255)==0)
441 {
442 if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
443 if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
444 if((fast_rand()&1)!=0)
445 {
446 if(opus_decoder_ctl(dec_err[fast_rand()&1], OPUS_RESET_STATE)!=OPUS_OK)test_failed();
447 }
448 }
449 if((fast_rand()&127)==0)
450 {
451 if(opus_decoder_ctl(dec_err[fast_rand()&1], OPUS_RESET_STATE)!=OPUS_OK)test_failed();
452 }
453 if(fast_rand()%10==0){
454 int complex=fast_rand()%11;
455 if(opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complex))!=OPUS_OK)test_failed();
456 }
457 if(fast_rand()%50==0)opus_decoder_ctl(dec, OPUS_RESET_STATE);
458 if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
459 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j]))!=OPUS_OK)test_failed();
460 if(opus_encoder_ctl(enc, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_failed();
461 if(opus_encoder_ctl(enc, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_failed();
462 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS((rates[j]>=64000?2:1)))!=OPUS_OK)test_failed();
463 if(opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_OK)test_failed();
464 if(opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
465 bw=modes[j]==0?OPUS_BANDWIDTH_NARROWBAND+(fast_rand()%3):
466 modes[j]==1?OPUS_BANDWIDTH_SUPERWIDEBAND+(fast_rand()&1):
467 OPUS_BANDWIDTH_NARROWBAND+(fast_rand()%5);
468 if(modes[j]==2&&bw==OPUS_BANDWIDTH_MEDIUMBAND)bw+=3;
469 if(opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(bw))!=OPUS_OK)test_failed();
470 len = opus_encode(enc, &inbuf[i<<1], frame_size, packet, MAX_PACKET);
471 if(len<0 || len>MAX_PACKET)test_failed();
472 if(opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
473 if((fast_rand()&3)==0)
474 {
475 if(opus_packet_pad(packet,len,len+1)!=OPUS_OK)test_failed();
476 len++;
477 }
478 if((fast_rand()&7)==0)
479 {
480 if(opus_packet_pad(packet,len,len+256)!=OPUS_OK)test_failed();
481 len+=256;
482 }
483 if((fast_rand()&3)==0)
484 {
485 len=opus_packet_unpad(packet,len);
486 if(len<1)test_failed();
487 }
488 out_samples = opus_decode(dec, packet, len, &outbuf[i<<1], MAX_FRAME_SAMP, 0);
489 if(out_samples!=frame_size)test_failed();
490 if(opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
491 if(enc_final_range!=dec_final_range)test_failed();
492 /*LBRR decode*/
493 out_samples = opus_decode(dec_err[0], packet, len, out2buf, frame_size, (fast_rand()&3)!=0);
494 if(out_samples!=frame_size)test_failed();
495 out_samples = opus_decode(dec_err[1], packet, (fast_rand()&3)==0?0:len, out2buf, MAX_FRAME_SAMP, (fast_rand()&7)!=0);
496 if(out_samples<120)test_failed();
497 i+=frame_size;
498 count++;
499 }while(i<(SSAMPLES-MAX_FRAME_SAMP));
500 fprintf(stdout," Mode %s FB encode %s, %6d bps OK.\n",mstrings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
501 fflush(stdout);
502 }
503 }
504
505 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(OPUS_AUTO))!=OPUS_OK)test_failed();
506 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO))!=OPUS_OK)test_failed();
507 if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(0))!=OPUS_OK)test_failed();
508 if(opus_encoder_ctl(enc, OPUS_SET_DTX(0))!=OPUS_OK)test_failed();
509
510 for(rc=0;rc<3;rc++)
511 {
512 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
513 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
514 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
515 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
516 for(j=0;j<16;j++)
517 {
518 int rate;
519 int modes[16]={0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2};
520 int rates[16]={4000,12000,32000,8000,16000,32000,48000,88000,4000,12000,32000,8000,16000,32000,48000,88000};
521 int frame[16]={160*1,160,80,160,160,80,40,20,160*1,160,80,160,160,80,40,20};
522 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0&&j==1))!=OPUS_OK)test_failed();
523 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j]))!=OPUS_OK)test_failed();
524 rate=rates[j]+fast_rand()%rates[j];
525 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_failed();
526 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_failed();
527 count=i=0;
528 do {
529 int len,out_samples,frame_size,loss;
530 opus_int32 pred;
531 if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_PREDICTION_DISABLED(&pred))!=OPUS_OK)test_failed();
532 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_PREDICTION_DISABLED((int)(fast_rand()&15)<(pred?11:4)))!=OPUS_OK)test_failed();
533 frame_size=frame[j];
534 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_OK)test_failed();
535 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
536 if((fast_rand()&255)==0)
537 {
538 if(opus_multistream_encoder_ctl(MSenc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
539 if(opus_multistream_decoder_ctl(MSdec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
540 if((fast_rand()&3)!=0)
541 {
542 if(opus_multistream_decoder_ctl(MSdec_err, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
543 }
544 }
545 if((fast_rand()&255)==0)
546 {
547 if(opus_multistream_decoder_ctl(MSdec_err, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
548 }
549 len = opus_multistream_encode(MSenc, &inbuf[i<<1], frame_size, packet, MAX_PACKET);
550 if(len<0 || len>MAX_PACKET)test_failed();
551 if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
552 if((fast_rand()&3)==0)
553 {
554 if(opus_multistream_packet_pad(packet,len,len+1,2)!=OPUS_OK)test_failed();
555 len++;
556 }
557 if((fast_rand()&7)==0)
558 {
559 if(opus_multistream_packet_pad(packet,len,len+256,2)!=OPUS_OK)test_failed();
560 len+=256;
561 }
562 if((fast_rand()&3)==0)
563 {
564 len=opus_multistream_packet_unpad(packet,len,2);
565 if(len<1)test_failed();
566 }
567 out_samples = opus_multistream_decode(MSdec, packet, len, out2buf, MAX_FRAME_SAMP, 0);
568 if(out_samples!=frame_size*6)test_failed();
569 if(opus_multistream_decoder_ctl(MSdec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
570 if(enc_final_range!=dec_final_range)test_failed();
571 /*LBRR decode*/
572 loss=(fast_rand()&63)==0;
573 out_samples = opus_multistream_decode(MSdec_err, packet, loss?0:len, out2buf, frame_size*6, (fast_rand()&3)!=0);
574 if(out_samples!=(frame_size*6))test_failed();
575 i+=frame_size;
576 count++;
577 }while(i<(SSAMPLES/12-MAX_FRAME_SAMP));
578 fprintf(stdout," Mode %s NB dual-mono MS encode %s, %6d bps OK.\n",mstrings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
579 fflush(stdout);
580 }
581 }
582
583 bitrate_bps=512000;
584 fsize=fast_rand()%31;
585 fswitch=100;
586
587 debruijn2(6,db62);
588 count=i=0;
589 do {
590 unsigned char toc;
591 const unsigned char *frames[48];
592 short size[48];
593 int payload_offset;
594 opus_uint32 dec_final_range2;
595 int jj,dec2;
596 int len,out_samples;
597 int frame_size=fsizes[db62[fsize]];
598 opus_int32 offset=i%(SAMPLES-MAX_FRAME_SAMP);
599
600 opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps));
601
602 len = opus_encode(enc, &inbuf[offset<<1], frame_size, packet, MAX_PACKET);
603 if(len<0 || len>MAX_PACKET)test_failed();
604 count++;
605
606 opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range));
607
608 out_samples = opus_decode(dec, packet, len, &outbuf[offset<<1], MAX_FRAME_SAMP, 0);
609 if(out_samples!=frame_size)test_failed();
610
611 opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
612
613 /* compare final range encoder rng values of encoder and decoder */
614 if(dec_final_range!=enc_final_range)test_failed();
615
616 /* We fuzz the packet, but take care not to only corrupt the payload
617 Corrupted headers are tested elsewhere and we need to actually run
618 the decoders in order to compare them. */
619 if(opus_packet_parse(packet,len,&toc,frames,size,&payload_offset)<=0)test_failed();
620 if((fast_rand()&1023)==0)len=0;
621 for(j=(opus_int32)(frames[0]-packet);j<len;j++)for(jj=0;jj<8;jj++)packet[j]^=((!no_fuzz)&&((fast_rand()&1023)==0))<<jj;
622 out_samples = opus_decode(dec_err[0], len>0?packet:NULL, len, out2buf, MAX_FRAME_SAMP, 0);
623 if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed();
624 if((len>0&&out_samples!=frame_size))test_failed(); /*FIXME use lastframe*/
625
626 opus_decoder_ctl(dec_err[0], OPUS_GET_FINAL_RANGE(&dec_final_range));
627
628 /*randomly select one of the decoders to compare with*/
629 dec2=fast_rand()%9+1;
630 out_samples = opus_decode(dec_err[dec2], len>0?packet:NULL, len, out2buf, MAX_FRAME_SAMP, 0);
631 if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed(); /*FIXME, use factor, lastframe for loss*/
632
633 opus_decoder_ctl(dec_err[dec2], OPUS_GET_FINAL_RANGE(&dec_final_range2));
634 if(len>0&&dec_final_range!=dec_final_range2)test_failed();
635
636 fswitch--;
637 if(fswitch<1)
638 {
639 int new_size;
640 fsize=(fsize+1)%36;
641 new_size=fsizes[db62[fsize]];
642 if(new_size==960||new_size==480)fswitch=2880/new_size*(fast_rand()%19+1);
643 else fswitch=(fast_rand()%(2880/new_size))+1;
644 }
645 bitrate_bps=((fast_rand()%508000+4000)+bitrate_bps)>>1;
646 i+=frame_size;
647 }while(i<SAMPLES*4);
648 fprintf(stdout," All framesize pairs switching encode, %d frames OK.\n",count);
649 fflush(stdout);
650
651 if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
652 opus_encoder_destroy(enc);
653 if(opus_multistream_encoder_ctl(MSenc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
654 opus_multistream_encoder_destroy(MSenc);
655 if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
656 opus_decoder_destroy(dec);
657 if(opus_multistream_decoder_ctl(MSdec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
658 opus_multistream_decoder_destroy(MSdec);
659 opus_multistream_decoder_destroy(MSdec_err);
660 for(i=0;i<10;i++)opus_decoder_destroy(dec_err[i]);
661 free(inbuf);
662 free(outbuf);
663 free(out2buf);
664 return 0;
665 }
666
print_usage(char * _argv[])667 void print_usage(char* _argv[])
668 {
669 fprintf(stderr,"Usage: %s [<seed>] [-fuzz <num_encoders> <num_settings_per_encoder>]\n",_argv[0]);
670 }
671
main(int _argc,char ** _argv)672 int main(int _argc, char **_argv)
673 {
674 int args=1;
675 char * strtol_str=NULL;
676 const char * oversion;
677 const char * env_seed;
678 int env_used;
679 int num_encoders_to_fuzz=5;
680 int num_setting_changes=40;
681
682 env_used=0;
683 env_seed=getenv("SEED");
684 if(_argc>1)
685 iseed=strtol(_argv[1], &strtol_str, 10); /* the first input argument might be the seed */
686 if(strtol_str!=NULL && strtol_str[0]=='\0') /* iseed is a valid number */
687 args++;
688 else if(env_seed) {
689 iseed=atoi(env_seed);
690 env_used=1;
691 }
692 else iseed=(opus_uint32)time(NULL)^(((opus_uint32)getpid()&65535)<<16);
693 Rw=Rz=iseed;
694
695 while(args<_argc)
696 {
697 if(strcmp(_argv[args], "-fuzz")==0 && _argc==(args+3)) {
698 num_encoders_to_fuzz=strtol(_argv[args+1], &strtol_str, 10);
699 if(strtol_str[0]!='\0' || num_encoders_to_fuzz<=0) {
700 print_usage(_argv);
701 return EXIT_FAILURE;
702 }
703 num_setting_changes=strtol(_argv[args+2], &strtol_str, 10);
704 if(strtol_str[0]!='\0' || num_setting_changes<=0) {
705 print_usage(_argv);
706 return EXIT_FAILURE;
707 }
708 args+=3;
709 }
710 else {
711 print_usage(_argv);
712 return EXIT_FAILURE;
713 }
714 }
715
716 oversion=opus_get_version_string();
717 if(!oversion)test_failed();
718 fprintf(stderr,"Testing %s encoder. Random seed: %u (%.4X)\n", oversion, iseed, fast_rand() % 65535);
719 if(env_used)fprintf(stderr," Random seed set from the environment (SEED=%s).\n", env_seed);
720
721 regression_test();
722
723 /*Setting TEST_OPUS_NOFUZZ tells the tool not to send garbage data
724 into the decoders. This is helpful because garbage data
725 may cause the decoders to clip, which angers CLANG IOC.*/
726 run_test1(getenv("TEST_OPUS_NOFUZZ")!=NULL);
727
728 /* Fuzz encoder settings online */
729 if(getenv("TEST_OPUS_NOFUZZ")==NULL) {
730 fprintf(stderr,"Running fuzz_encoder_settings with %d encoder(s) and %d setting change(s) each.\n",
731 num_encoders_to_fuzz, num_setting_changes);
732 fuzz_encoder_settings(num_encoders_to_fuzz, num_setting_changes);
733 }
734
735 fprintf(stderr,"Tests completed successfully.\n");
736
737 return 0;
738 }
739