blob: b5348c3ab2dde2f19aaddd1f504bdd15f591851c [file] [log] [blame]
sergeyu@chromium.org885f2ff2012-10-17 22:31:52 +00001/* Copyright (c) 2011 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/* This tests the API presented by the libopus system.
29 It does not attempt to extensively exercise the codec internals.
30 The strategy here is to simply the API interface invariants:
31 That sane options are accepted, insane options are rejected,
32 and that nothing blows up. In particular we don't actually test
33 that settings are heeded by the codec (though we do check that
34 get after set returns a sane value when it should). Other
35 tests check the actual codec behavior.
36 In cases where its reasonable to do so we test exhaustively,
37 but its not reasonable to do so in all cases.
38 Although these tests are simple they found several library bugs
39 when they were initially developed. */
40
41/* These tests are more sensitive if compiled with -DVALGRIND and
42 run inside valgrind. Malloc failure testing requires glibc. */
43
44#ifdef HAVE_CONFIG_H
45#include "config.h"
46#endif
47
48#include <stdio.h>
49#include <stdlib.h>
50#include <stdint.h>
51#include <string.h>
52#include "arch.h"
53#include "opus_multistream.h"
54#include "opus.h"
55#include "test_opus_common.h"
56
57#ifdef VALGRIND
58#include <valgrind/memcheck.h>
59#define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
60#define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
61#else
62#define VG_UNDEF(x,y)
63#define VG_CHECK(x,y)
64#endif
65
66#if defined(HAVE___MALLOC_HOOK)
67#define MALLOC_FAIL
68#include "os_support.h"
69#include <malloc.h>
70
71static const opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP,
72 OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY};
73
74void *malloc_hook(__attribute__((unused)) size_t size,
75 __attribute__((unused)) const void *caller)
76{
77 return 0;
78}
79#endif
80
81static const opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};
82
83opus_int32 test_dec_api(void)
84{
85 opus_uint32 dec_final_range;
86 OpusDecoder *dec;
87 OpusDecoder *dec2;
88 opus_int32 i,j,cfgs;
89 unsigned char packet[1276];
90#ifndef DISABLE_FLOAT_API
91 float fbuf[960*2];
92#endif
93 short sbuf[960*2];
94 int c,err;
95 opus_int32 *nullvalue;
96 nullvalue=0;
97
98 cfgs=0;
99 /*First test invalid configurations which should fail*/
100 fprintf(stdout,"\n Decoder basic API tests\n");
101 fprintf(stdout," ---------------------------------------------------\n");
102 for(c=0;c<4;c++)
103 {
104 i=opus_decoder_get_size(c);
105 if(((c==1||c==2)&&(i<=2048||i>1<<16))||((c!=1&&c!=2)&&i!=0))test_failed();
106 fprintf(stdout," opus_decoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
107 cfgs++;
108 }
109
110 /*Test with unsupported sample rates*/
111 for(c=0;c<4;c++)
112 {
113 for(i=-7;i<=96000;i++)
114 {
115 int fs;
116 if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
117 switch(i)
118 {
119 case(-5):fs=-8000;break;
120 case(-6):fs=INT32_MAX;break;
121 case(-7):fs=INT32_MIN;break;
122 default:fs=i;
123 }
124 err = OPUS_OK;
125 VG_UNDEF(&err,sizeof(err));
126 dec = opus_decoder_create(fs, c, &err);
127 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
128 cfgs++;
129 dec=malloc(opus_decoder_get_size(2));
130 if(dec==NULL)test_failed();
131 err = opus_decoder_init(dec,fs,c);
132 if(err!=OPUS_BAD_ARG)test_failed();
133 cfgs++;
134 free(dec);
135 }
136 }
137
138 VG_UNDEF(&err,sizeof(err));
139 dec = opus_decoder_create(48000, 2, &err);
140 if(err!=OPUS_OK || dec==NULL)test_failed();
141 VG_CHECK(dec,opus_decoder_get_size(2));
142 cfgs++;
143
144 fprintf(stdout," opus_decoder_create() ........................ OK.\n");
145 fprintf(stdout," opus_decoder_init() .......................... OK.\n");
146
147 VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
148 err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
149 if(err!=OPUS_OK)test_failed();
150 VG_CHECK(&dec_final_range,sizeof(dec_final_range));
151 fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
152 cfgs++;
153
154 err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
155 if(err!=OPUS_UNIMPLEMENTED)test_failed();
156 fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
157 cfgs++;
158
159 VG_UNDEF(&i,sizeof(i));
160 err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
161 if(err != OPUS_OK || i!=0)test_failed();
162 fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
163 cfgs++;
164
165 /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
166 err=opus_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
167 if(err!=OPUS_BAD_ARG)test_failed();
168 cfgs++;
169 VG_UNDEF(&i,sizeof(i));
170 err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
171 if(err != OPUS_OK || i>0 || i<-1)test_failed();
172 cfgs++;
173 VG_UNDEF(packet,sizeof(packet));
174 packet[0]=63<<2;packet[1]=packet[2]=0;
175 if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
176 cfgs++;
177 VG_UNDEF(&i,sizeof(i));
178 err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
179 if(err != OPUS_OK || i>0 || i<-1)test_failed();
180 cfgs++;
181 packet[0]=1;
182 if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
183 cfgs++;
184 VG_UNDEF(&i,sizeof(i));
185 err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
186 if(err != OPUS_OK || i>0 || i<-1)test_failed();
187 cfgs++;
188 fprintf(stdout," OPUS_GET_PITCH ............................... OK.\n");
189
190 VG_UNDEF(&i,sizeof(i));
191 err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
192 VG_CHECK(&i,sizeof(i));
193 if(err != OPUS_OK || i!=0)test_failed();
194 cfgs++;
195 err=opus_decoder_ctl(dec, OPUS_GET_GAIN(nullvalue));
196 if(err != OPUS_BAD_ARG)test_failed();
197 cfgs++;
198 err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-32769));
199 if(err != OPUS_BAD_ARG)test_failed();
200 cfgs++;
201 err=opus_decoder_ctl(dec, OPUS_SET_GAIN(32768));
202 if(err != OPUS_BAD_ARG)test_failed();
203 cfgs++;
204 err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-15));
205 if(err != OPUS_OK)test_failed();
206 cfgs++;
207 VG_UNDEF(&i,sizeof(i));
208 err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
209 VG_CHECK(&i,sizeof(i));
210 if(err != OPUS_OK || i!=-15)test_failed();
211 cfgs++;
212 fprintf(stdout," OPUS_SET_GAIN ................................ OK.\n");
213 fprintf(stdout," OPUS_GET_GAIN ................................ OK.\n");
214
215 /*Reset the decoder*/
216 dec2=malloc(opus_decoder_get_size(2));
217 memcpy(dec2,dec,opus_decoder_get_size(2));
218 if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
219 if(memcmp(dec2,dec,opus_decoder_get_size(2))==0)test_failed();
220 free(dec2);
221 fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
222 cfgs++;
223
224 VG_UNDEF(packet,sizeof(packet));
225 packet[0]=0;
226 if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
227 cfgs++;
228 packet[0]=(63<<2)|3;
229 packet[1]=63;
230 if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed();
231 fprintf(stdout," opus_decoder_get_nb_samples() ................ OK.\n");
232 cfgs++;
233
234 if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();
235 for(i=0;i<256;i++) {
236 int l1res[4]={1,2,2,OPUS_INVALID_PACKET};
237 packet[0]=i;
238 if(l1res[packet[0]&3]!=opus_packet_get_nb_frames(packet,1))test_failed();
239 cfgs++;
240 for(j=0;j<256;j++) {
241 packet[1]=j;
242 if(((packet[0]&3)!=3?l1res[packet[0]&3]:packet[1]&63)!=opus_packet_get_nb_frames(packet,2))test_failed();
243 cfgs++;
244 }
245 }
246 fprintf(stdout," opus_packet_get_nb_frames() .................. OK.\n");
247
248 for(i=0;i<256;i++) {
249 int bw;
250 packet[0]=i;
251 bw=packet[0]>>4;
252 bw=OPUS_BANDWIDTH_NARROWBAND+(((((bw&7)*9)&(63-(bw&8)))+2+12*((bw&8)!=0))>>4);
253 if(bw!=opus_packet_get_bandwidth(packet))test_failed();
254 cfgs++;
255 }
256 fprintf(stdout," opus_packet_get_bandwidth() .................. OK.\n");
257
258 for(i=0;i<256;i++) {
259 int fp3s,rate;
260 packet[0]=i;
261 fp3s=packet[0]>>3;
262 fp3s=((((3-(fp3s&3))*13&119)+9)>>2)*((fp3s>13)*(3-((fp3s&3)==3))+1)*25;
263 for(rate=0;rate<5;rate++) {
264 if((opus_rates[rate]*3/fp3s)!=opus_packet_get_samples_per_frame(packet,opus_rates[rate]))test_failed();
265 cfgs++;
266 }
267 }
268 fprintf(stdout," opus_packet_get_samples_per_frame() .......... OK.\n");
269
270 packet[0]=(63<<2)+3;
271 packet[1]=49;
272 for(j=2;j<51;j++)packet[j]=0;
273 VG_UNDEF(sbuf,sizeof(sbuf));
274 if(opus_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
275 cfgs++;
276 packet[0]=(63<<2);
277 packet[1]=packet[2]=0;
278 if(opus_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG)test_failed();
279 cfgs++;
280 if(opus_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
281 cfgs++;
282 if(opus_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
283 cfgs++;
284 if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
285 cfgs++;
286 fprintf(stdout," opus_decode() ................................ OK.\n");
287#ifndef DISABLE_FLOAT_API
288 VG_UNDEF(fbuf,sizeof(fbuf));
289 if(opus_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
290 cfgs++;
291 fprintf(stdout," opus_decode_float() .......................... OK.\n");
292#endif
293
294#if 0
295 /*These tests are disabled because the library crashes with null states*/
296 if(opus_decoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
297 if(opus_decoder_init(0,48000,1) !=OPUS_INVALID_STATE)test_failed();
298 if(opus_decode(0,packet,1,outbuf,2880,0) !=OPUS_INVALID_STATE)test_failed();
299 if(opus_decode_float(0,packet,1,0,2880,0) !=OPUS_INVALID_STATE)test_failed();
300 if(opus_decoder_get_nb_samples(0,packet,1) !=OPUS_INVALID_STATE)test_failed();
301 if(opus_packet_get_nb_frames(NULL,1) !=OPUS_BAD_ARG)test_failed();
302 if(opus_packet_get_bandwidth(NULL) !=OPUS_BAD_ARG)test_failed();
303 if(opus_packet_get_samples_per_frame(NULL,48000)!=OPUS_BAD_ARG)test_failed();
304#endif
305 opus_decoder_destroy(dec);
306 cfgs++;
307 fprintf(stdout," All decoder interface tests passed\n");
308 fprintf(stdout," (%6d API invocations)\n",cfgs);
309 return cfgs;
310}
311
312opus_int32 test_msdec_api(void)
313{
314 opus_uint32 dec_final_range;
315 OpusMSDecoder *dec;
316 OpusDecoder *streamdec;
317 opus_int32 i,j,cfgs;
318 unsigned char packet[1276];
319 unsigned char mapping[256];
320#ifndef DISABLE_FLOAT_API
321 float fbuf[960*2];
322#endif
323 short sbuf[960*2];
324 int a,b,c,err;
325#if 0
326 /*Relevant test not enabled for multistream*/
327 int *nullvalue;
328 nullvalue=0;
329#endif
330
331 mapping[0]=0;
332 mapping[1]=1;
333 for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
334
335 cfgs=0;
336 /*First test invalid configurations which should fail*/
337 fprintf(stdout,"\n Multistream decoder basic API tests\n");
338 fprintf(stdout," ---------------------------------------------------\n");
339 for(a=-1;a<4;a++)
340 {
341 for(b=-1;b<4;b++)
342 {
343 i=opus_multistream_decoder_get_size(a,b);
344 if(((a>0&&b<=a&&b>=0)&&(i<=2048||i>((1<<16)*a)))||((a<1||b>a||b<0)&&i!=0))test_failed();
345 fprintf(stdout," opus_multistream_decoder_get_size(%2d,%2d)=%d %sOK.\n",a,b,i,i>0?"":"... ");
346 cfgs++;
347 }
348 }
349
350 /*Test with unsupported sample rates*/
351 for(c=1;c<3;c++)
352 {
353 for(i=-7;i<=96000;i++)
354 {
355 int fs;
356 if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
357 switch(i)
358 {
359 case(-5):fs=-8000;break;
360 case(-6):fs=INT32_MAX;break;
361 case(-7):fs=INT32_MIN;break;
362 default:fs=i;
363 }
364 err = OPUS_OK;
365 VG_UNDEF(&err,sizeof(err));
366 dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, &err);
367 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
368 cfgs++;
369 dec=malloc(opus_multistream_decoder_get_size(1,1));
370 if(dec==NULL)test_failed();
371 err = opus_multistream_decoder_init(dec,fs,c,1,c-1, mapping);
372 if(err!=OPUS_BAD_ARG)test_failed();
373 cfgs++;
374 free(dec);
375 }
376 }
377
378 VG_UNDEF(&err,sizeof(err));
379 dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, &err);
380 VG_CHECK(&err,sizeof(err));
381 if(err==OPUS_OK || dec!=NULL)test_failed();
382 cfgs++;
383
384 VG_UNDEF(&err,sizeof(err));
385 mapping[0]=mapping[1]=0;
386 dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, &err);
387 VG_CHECK(&err,sizeof(err));
388 if(err!=OPUS_OK || dec==NULL)test_failed();
389 cfgs++;
390 opus_multistream_decoder_destroy(dec);
391 cfgs++;
392
393 VG_UNDEF(&err,sizeof(err));
394 dec = opus_multistream_decoder_create(48000, 1, 4, 1, mapping, &err);
395 VG_CHECK(&err,sizeof(err));
396 if(err!=OPUS_OK || dec==NULL)test_failed();
397 cfgs++;
398
399 err = opus_multistream_decoder_init(dec,48000, 1, 0, 0, mapping);
400 if(err!=OPUS_BAD_ARG)test_failed();
401 cfgs++;
402
403 err = opus_multistream_decoder_init(dec,48000, 1, 1, -1, mapping);
404 if(err!=OPUS_BAD_ARG)test_failed();
405 cfgs++;
406
407 opus_multistream_decoder_destroy(dec);
408 cfgs++;
409
410 VG_UNDEF(&err,sizeof(err));
411 dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
412 VG_CHECK(&err,sizeof(err));
413 if(err!=OPUS_OK || dec==NULL)test_failed();
414 cfgs++;
415 opus_multistream_decoder_destroy(dec);
416 cfgs++;
417
418 VG_UNDEF(&err,sizeof(err));
419 dec = opus_multistream_decoder_create(48000, 255, 255, 1, mapping, &err);
420 VG_CHECK(&err,sizeof(err));
421 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
422 cfgs++;
423
424 VG_UNDEF(&err,sizeof(err));
425 dec = opus_multistream_decoder_create(48000, -1, 1, 1, mapping, &err);
426 VG_CHECK(&err,sizeof(err));
427 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
428 cfgs++;
429
430 VG_UNDEF(&err,sizeof(err));
431 dec = opus_multistream_decoder_create(48000, 0, 1, 1, mapping, &err);
432 VG_CHECK(&err,sizeof(err));
433 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
434 cfgs++;
435
436 VG_UNDEF(&err,sizeof(err));
437 dec = opus_multistream_decoder_create(48000, 1, -1, 2, mapping, &err);
438 VG_CHECK(&err,sizeof(err));
439 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
440 cfgs++;
441
442 VG_UNDEF(&err,sizeof(err));
443 dec = opus_multistream_decoder_create(48000, 1, -1, -1, mapping, &err);
444 VG_CHECK(&err,sizeof(err));
445 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
446 cfgs++;
447
448 VG_UNDEF(&err,sizeof(err));
449 dec = opus_multistream_decoder_create(48000, 256, 255, 1, mapping, &err);
450 VG_CHECK(&err,sizeof(err));
451 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
452 cfgs++;
453
454 VG_UNDEF(&err,sizeof(err));
455 dec = opus_multistream_decoder_create(48000, 256, 255, 0, mapping, &err);
456 VG_CHECK(&err,sizeof(err));
457 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
458 cfgs++;
459
460 VG_UNDEF(&err,sizeof(err));
461 mapping[0]=255;
462 mapping[1]=1;
463 mapping[2]=2;
464 dec = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, &err);
465 VG_CHECK(&err,sizeof(err));
466 if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
467 cfgs++;
468
469 VG_UNDEF(&err,sizeof(err));
470 mapping[0]=0;
471 mapping[1]=0;
472 mapping[2]=0;
473 dec = opus_multistream_decoder_create(48000, 3, 2, 1, mapping, &err);
474 VG_CHECK(&err,sizeof(err));
475 if(err!=OPUS_OK || dec==NULL)test_failed();
476 cfgs++;
477 opus_multistream_decoder_destroy(dec);
478 cfgs++;
479
480 mapping[0]=0;
481 mapping[1]=255;
482 mapping[2]=1;
483 mapping[3]=2;
484 mapping[4]=3;
485 dec = opus_multistream_decoder_create(48001, 5, 4, 1, mapping, 0);
486 if(dec!=NULL)test_failed();
487 cfgs++;
488
489 VG_UNDEF(&err,sizeof(err));
490 mapping[0]=0;
491 mapping[1]=255;
492 mapping[2]=1;
493 mapping[3]=2;
494 dec = opus_multistream_decoder_create(48000, 4, 2, 1, mapping, &err);
495 VG_CHECK(&err,sizeof(err));
496 if(err!=OPUS_OK || dec==NULL)test_failed();
497 cfgs++;
498
499 fprintf(stdout," opus_multistream_decoder_create() ............ OK.\n");
500 fprintf(stdout," opus_multistream_decoder_init() .............. OK.\n");
501
502 VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
503 err=opus_multistream_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
504 if(err!=OPUS_OK)test_failed();
505 VG_CHECK(&dec_final_range,sizeof(dec_final_range));
506 fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
507 cfgs++;
508
509 streamdec=0;
510 VG_UNDEF(&streamdec,sizeof(streamdec));
511 err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(-1,&streamdec));
512 if(err!=OPUS_BAD_ARG)test_failed();
513 cfgs++;
514 err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(1,&streamdec));
515 if(err!=OPUS_OK||streamdec==NULL)test_failed();
516 VG_CHECK(streamdec,opus_decoder_get_size(1));
517 cfgs++;
518 err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(2,&streamdec));
519 if(err!=OPUS_BAD_ARG)test_failed();
520 cfgs++;
521 err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(0,&streamdec));
522 if(err!=OPUS_OK||streamdec==NULL)test_failed();
523 VG_CHECK(streamdec,opus_decoder_get_size(1));
524 fprintf(stdout," OPUS_MULTISTREAM_GET_DECODER_STATE ........... OK.\n");
525 cfgs++;
526
527 for(j=0;j<2;j++)
528 {
529 OpusDecoder *od;
530 err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
531 if(err != OPUS_OK)test_failed();
532 VG_UNDEF(&i,sizeof(i));
533 err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
534 VG_CHECK(&i,sizeof(i));
535 if(err != OPUS_OK || i!=0)test_failed();
536 cfgs++;
537 }
538 err=opus_multistream_decoder_ctl(dec,OPUS_SET_GAIN(15));
539 if(err!=OPUS_OK)test_failed();
540 fprintf(stdout," OPUS_SET_GAIN ................................ OK.\n");
541 for(j=0;j<2;j++)
542 {
543 OpusDecoder *od;
544 err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
545 if(err != OPUS_OK)test_failed();
546 VG_UNDEF(&i,sizeof(i));
547 err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
548 VG_CHECK(&i,sizeof(i));
549 if(err != OPUS_OK || i!=15)test_failed();
550 cfgs++;
551 }
552 fprintf(stdout," OPUS_GET_GAIN ................................ OK.\n");
553
554 VG_UNDEF(&i,sizeof(i));
555 err=opus_multistream_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
556 if(err != OPUS_OK || i!=0)test_failed();
557 fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
558 cfgs++;
559
560 err=opus_multistream_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
561 if(err!=OPUS_UNIMPLEMENTED)test_failed();
562 fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
563 cfgs++;
564
565#if 0
566 /*Currently unimplemented for multistream*/
567 /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
568 err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
569 if(err!=OPUS_BAD_ARG)test_failed();
570 cfgs++;
571 VG_UNDEF(&i,sizeof(i));
572 err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
573 if(err != OPUS_OK || i>0 || i<-1)test_failed();
574 cfgs++;
575 VG_UNDEF(packet,sizeof(packet));
576 packet[0]=63<<2;packet[1]=packet[2]=0;
577 if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
578 cfgs++;
579 VG_UNDEF(&i,sizeof(i));
580 err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
581 if(err != OPUS_OK || i>0 || i<-1)test_failed();
582 cfgs++;
583 packet[0]=1;
584 if(opus_multistream_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
585 cfgs++;
586 VG_UNDEF(&i,sizeof(i));
587 err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
588 if(err != OPUS_OK || i>0 || i<-1)test_failed();
589 cfgs++;
590 fprintf(stdout," OPUS_GET_PITCH ............................... OK.\n");
591#endif
592
593 /*Reset the decoder*/
594 if(opus_multistream_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
595 fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
596 cfgs++;
597
598 opus_multistream_decoder_destroy(dec);
599 cfgs++;
600 VG_UNDEF(&err,sizeof(err));
601 dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
602 if(err!=OPUS_OK || dec==NULL)test_failed();
603 cfgs++;
604
605 packet[0]=(63<<2)+3;
606 packet[1]=49;
607 for(j=2;j<51;j++)packet[j]=0;
608 VG_UNDEF(sbuf,sizeof(sbuf));
609 if(opus_multistream_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
610 cfgs++;
611 packet[0]=(63<<2);
612 packet[1]=packet[2]=0;
613 if(opus_multistream_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG){printf("%d\n",opus_multistream_decode(dec, packet, -1, sbuf, 960, 0));test_failed();}
614 cfgs++;
615 if(opus_multistream_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
616 cfgs++;
617 if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
618 cfgs++;
619 if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
620 cfgs++;
621 fprintf(stdout," opus_multistream_decode() .................... OK.\n");
622#ifndef DISABLE_FLOAT_API
623 VG_UNDEF(fbuf,sizeof(fbuf));
624 if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
625 cfgs++;
626 fprintf(stdout," opus_multistream_decode_float() .............. OK.\n");
627#endif
628
629#if 0
630 /*These tests are disabled because the library crashes with null states*/
631 if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
632 if(opus_multistream_decoder_init(0,48000,1) !=OPUS_INVALID_STATE)test_failed();
633 if(opus_multistream_decode(0,packet,1,outbuf,2880,0) !=OPUS_INVALID_STATE)test_failed();
634 if(opus_multistream_decode_float(0,packet,1,0,2880,0) !=OPUS_INVALID_STATE)test_failed();
635 if(opus_multistream_decoder_get_nb_samples(0,packet,1) !=OPUS_INVALID_STATE)test_failed();
636#endif
637 opus_multistream_decoder_destroy(dec);
638 cfgs++;
639 fprintf(stdout," All multistream decoder interface tests passed\n");
640 fprintf(stdout," (%6d API invocations)\n",cfgs);
641 return cfgs;
642}
643
644#ifdef VALGRIND
645#define UNDEFINE_FOR_PARSE toc=-1; \
646 frames[0]=(unsigned char *)0; \
647 frames[1]=(unsigned char *)0; \
648 payload_offset=-1; \
649 VG_UNDEF(&toc,sizeof(toc)); \
650 VG_UNDEF(frames,sizeof(frames));\
651 VG_UNDEF(&payload_offset,sizeof(payload_offset));
652#else
653#define UNDEFINE_FOR_PARSE toc=-1; \
654 frames[0]=(unsigned char *)0; \
655 frames[1]=(unsigned char *)0; \
656 payload_offset=-1;
657#endif
658
659/* This test exercises the heck out of the libopus parser.
660 It is much larger than the parser itself in part because
661 it tries to hit a lot of corner cases that could never
662 fail with the libopus code, but might be problematic for
663 other implementations. */
664opus_int32 test_parse(void)
665{
666 opus_int32 i,j,jj,sz;
667 unsigned char packet[1276];
668 opus_int32 cfgs,cfgs_total;
669 unsigned char toc;
670 const unsigned char *frames[48];
671 short size[48];
672 int payload_offset, ret;
673 fprintf(stdout,"\n Packet header parsing tests\n");
674 fprintf(stdout," ---------------------------------------------------\n");
675 memset(packet,0,sizeof(char)*1276);
676 packet[0]=63<<2;
677 if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
678 cfgs_total=cfgs=1;
679 /*code 0*/
680 for(i=0;i<64;i++)
681 {
682 UNDEFINE_FOR_PARSE
683 ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
684 cfgs++;
685 if(ret!=1)test_failed();
686 if(size[0]!=3)test_failed();
687 if(frames[0]!=packet+1)test_failed();
688 }
689 fprintf(stdout," code 0 (%2d cases) ............................ OK.\n",cfgs);
690 cfgs_total+=cfgs;cfgs=0;
691
692 /*code 1, two frames of the same size*/
693 for(i=0;i<64;i++)
694 {
695 packet[0]=(i<<2)+1;
696 for(jj=0;jj<=1275*2+3;jj++)
697 {
698 UNDEFINE_FOR_PARSE
699 ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
700 cfgs++;
701 if((jj&1)==1 && jj<=2551)
702 {
703 /* Must pass if payload length even (packet length odd) and
704 size<=2551, must fail otherwise. */
705 if(ret!=2)test_failed();
706 if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
707 if(frames[0]!=packet+1)test_failed();
708 if(frames[1]!=frames[0]+size[0])test_failed();
709 if((toc>>2)!=i)test_failed();
710 } else if(ret!=OPUS_INVALID_PACKET)test_failed();
711 }
712 }
713 fprintf(stdout," code 1 (%6d cases) ........................ OK.\n",cfgs);
714 cfgs_total+=cfgs;cfgs=0;
715
716 for(i=0;i<64;i++)
717 {
718 /*code 2, length code overflow*/
719 packet[0]=(i<<2)+2;
720 UNDEFINE_FOR_PARSE
721 ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
722 cfgs++;
723 if(ret!=OPUS_INVALID_PACKET)test_failed();
724 packet[1]=252;
725 UNDEFINE_FOR_PARSE
726 ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
727 cfgs++;
728 if(ret!=OPUS_INVALID_PACKET)test_failed();
729 for(j=0;j<1275;j++)
730 {
731 if(j<252)packet[1]=j;
732 else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
733 /*Code 2, one too short*/
734 UNDEFINE_FOR_PARSE
735 ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
736 cfgs++;
737 if(ret!=OPUS_INVALID_PACKET)test_failed();
738 /*Code 2, one too long*/
739 UNDEFINE_FOR_PARSE
740 ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
741 cfgs++;
742 if(ret!=OPUS_INVALID_PACKET)test_failed();
743 /*Code 2, second zero*/
744 UNDEFINE_FOR_PARSE
745 ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
746 cfgs++;
747 if(ret!=2)test_failed();
748 if(size[0]!=j||size[1]!=0)test_failed();
749 if(frames[1]!=frames[0]+size[0])test_failed();
750 if((toc>>2)!=i)test_failed();
751 /*Code 2, normal*/
752 UNDEFINE_FOR_PARSE
753 ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
754 cfgs++;
755 if(ret!=2)test_failed();
756 if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
757 if(frames[1]!=frames[0]+size[0])test_failed();
758 if((toc>>2)!=i)test_failed();
759 }
760 }
761 fprintf(stdout," code 2 (%6d cases) ........................ OK.\n",cfgs);
762 cfgs_total+=cfgs;cfgs=0;
763
764 for(i=0;i<64;i++)
765 {
766 packet[0]=(i<<2)+3;
767 /*code 3, length code overflow*/
768 UNDEFINE_FOR_PARSE
769 ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
770 cfgs++;
771 if(ret!=OPUS_INVALID_PACKET)test_failed();
772 }
773 fprintf(stdout," code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
774 cfgs_total+=cfgs;cfgs=0;
775
776 for(i=0;i<64;i++)
777 {
778 /*code 3, m is zero or 49-63*/
779 packet[0]=(i<<2)+3;
780 for(jj=49;jj<=64;jj++)
781 {
782 packet[1]=0+(jj&63); /*CBR, no padding*/
783 UNDEFINE_FOR_PARSE
784 ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
785 cfgs++;
786 if(ret!=OPUS_INVALID_PACKET)test_failed();
787 packet[1]=128+(jj&63); /*VBR, no padding*/
788 UNDEFINE_FOR_PARSE
789 ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
790 cfgs++;
791 if(ret!=OPUS_INVALID_PACKET)test_failed();
792 packet[1]=64+(jj&63); /*CBR, padding*/
793 UNDEFINE_FOR_PARSE
794 ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
795 cfgs++;
796 if(ret!=OPUS_INVALID_PACKET)test_failed();
797 packet[1]=128+64+(jj&63); /*VBR, padding*/
798 UNDEFINE_FOR_PARSE
799 ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
800 cfgs++;
801 if(ret!=OPUS_INVALID_PACKET)test_failed();
802 }
803 }
804 fprintf(stdout," code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
805 cfgs_total+=cfgs;cfgs=0;
806
807 for(i=0;i<64;i++)
808 {
809 packet[0]=(i<<2)+3;
810 /*code 3, m is one, cbr*/
811 packet[1]=1;
812 for(j=0;j<1276;j++)
813 {
814 UNDEFINE_FOR_PARSE
815 ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
816 cfgs++;
817 if(ret!=1)test_failed();
818 if(size[0]!=j)test_failed();
819 if((toc>>2)!=i)test_failed();
820 }
821 UNDEFINE_FOR_PARSE
822 ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
823 cfgs++;
824 if(ret!=OPUS_INVALID_PACKET)test_failed();
825 }
826 fprintf(stdout," code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
827 cfgs_total+=cfgs;cfgs=0;
828
829 for(i=0;i<64;i++)
830 {
831 int frame_samp;
832 /*code 3, m>1 CBR*/
833 packet[0]=(i<<2)+3;
834 frame_samp=opus_packet_get_samples_per_frame(packet,48000);
835 for(j=2;j<49;j++)
836 {
837 packet[1]=j;
838 for(sz=2;sz<((j+2)*1275);sz++)
839 {
840 UNDEFINE_FOR_PARSE
841 ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
842 cfgs++;
843 /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
844 if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
845 {
846 if(ret!=j)test_failed();
847 for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
848 if((toc>>2)!=i)test_failed();
849 } else if(ret!=OPUS_INVALID_PACKET)test_failed();
850 }
851 }
852 /*Super jumbo packets*/
853 packet[1]=5760/frame_samp;
854 UNDEFINE_FOR_PARSE
855 ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
856 cfgs++;
857 if(ret!=packet[1])test_failed();
858 for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
859 }
860 fprintf(stdout," code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
861 cfgs_total+=cfgs;cfgs=0;
862
863 for(i=0;i<64;i++)
864 {
865 int frame_samp;
866 /*Code 3 VBR, m one*/
867 packet[0]=(i<<2)+3;
868 packet[1]=128+1;
869 frame_samp=opus_packet_get_samples_per_frame(packet,48000);
870 for(jj=0;jj<1276;jj++)
871 {
872 UNDEFINE_FOR_PARSE
873 ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
874 cfgs++;
875 if(ret!=1)test_failed();
876 if(size[0]!=jj)test_failed();
877 if((toc>>2)!=i)test_failed();
878 }
879 UNDEFINE_FOR_PARSE
880 ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
881 cfgs++;
882 if(ret!=OPUS_INVALID_PACKET)test_failed();
883 for(j=2;j<49;j++)
884 {
885 packet[1]=128+j;
886 /*Length code overflow*/
887 UNDEFINE_FOR_PARSE
888 ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
889 cfgs++;
890 if(ret!=OPUS_INVALID_PACKET)test_failed();
891 packet[2]=252;
892 packet[3]=0;
893 for(jj=4;jj<2+j;jj++)packet[jj]=0;
894 UNDEFINE_FOR_PARSE
895 ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
896 cfgs++;
897 if(ret!=OPUS_INVALID_PACKET)test_failed();
898 /*One byte too short*/
899 for(jj=2;jj<2+j;jj++)packet[jj]=0;
900 UNDEFINE_FOR_PARSE
901 ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
902 cfgs++;
903 if(ret!=OPUS_INVALID_PACKET)test_failed();
904 /*One byte too short thanks to length coding*/
905 packet[2]=252;
906 packet[3]=0;
907 for(jj=4;jj<2+j;jj++)packet[jj]=0;
908 UNDEFINE_FOR_PARSE
909 ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
910 cfgs++;
911 if(ret!=OPUS_INVALID_PACKET)test_failed();
912 /*Most expensive way of coding zeros*/
913 for(jj=2;jj<2+j;jj++)packet[jj]=0;
914 UNDEFINE_FOR_PARSE
915 ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
916 cfgs++;
917 if(frame_samp*j<=5760){
918 if(ret!=j)test_failed();
919 for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
920 if((toc>>2)!=i)test_failed();
921 } else if(ret!=OPUS_INVALID_PACKET)test_failed();
922 /*Quasi-CBR use of mode 3*/
923 for(sz=0;sz<8;sz++)
924 {
925 const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
926 int pos=0;
927 int as=(tsz[sz]+i-j-2)/j;
928 for(jj=0;jj<j-1;jj++)
929 {
930 if(as<252){packet[2+pos]=as;pos++;}
931 else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
932 }
933 UNDEFINE_FOR_PARSE
934 ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
935 cfgs++;
936 if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
937 if(ret!=j)test_failed();
938 for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
939 if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
940 if((toc>>2)!=i)test_failed();
941 } else if(ret!=OPUS_INVALID_PACKET)test_failed();
942 }
943 }
944 }
945 fprintf(stdout," code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
946 cfgs_total+=cfgs;cfgs=0;
947
948 for(i=0;i<64;i++)
949 {
950 packet[0]=(i<<2)+3;
951 /*Padding*/
952 packet[1]=128+1+64;
953 /*Overflow the length coding*/
954 for(jj=2;jj<127;jj++)packet[jj]=255;
955 UNDEFINE_FOR_PARSE
956 ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
957 cfgs++;
958 if(ret!=OPUS_INVALID_PACKET)test_failed();
959
960 for(sz=0;sz<4;sz++)
961 {
962 const int tsz[4]={0,72,512,1275};
963 for(jj=sz;jj<65025;jj+=11)
964 {
965 int pos;
966 for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
967 packet[2+pos]=jj%254;
968 pos++;
969 if(sz==0&&i==63)
970 {
971 /*Code more padding than there is room in the packet*/
972 UNDEFINE_FOR_PARSE
973 ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
974 cfgs++;
975 if(ret!=OPUS_INVALID_PACKET)test_failed();
976 }
977 UNDEFINE_FOR_PARSE
978 ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
979 cfgs++;
980 if(tsz[sz]+i<1276)
981 {
982 if(ret!=1)test_failed();
983 if(size[0]!=tsz[sz]+i)test_failed();
984 if((toc>>2)!=i)test_failed();
985 } else if (ret!=OPUS_INVALID_PACKET)test_failed();
986 }
987 }
988 }
989 fprintf(stdout," code 3 padding (%2d cases) ............... OK.\n",cfgs);
990 cfgs_total+=cfgs;cfgs=0;
991 fprintf(stdout," opus_packet_parse ............................ OK.\n");
992 fprintf(stdout," All packet parsing tests passed\n");
993 fprintf(stdout," (%d API invocations)\n",cfgs_total);
994 return cfgs_total;
995}
996
997/* This is a helper macro for the encoder tests.
998 The encoder api tests all have a pattern of set-must-fail, set-must-fail,
999 set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
1000#define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
1001 i=(badv);\
1002 if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
1003 i=(badv2);\
1004 if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
1005 j=i=(goodv);\
1006 if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
1007 i=-12345;\
1008 VG_UNDEF(&i,sizeof(i)); \
1009 err=opus_encoder_ctl(enc,getcall);\
1010 if(err!=OPUS_OK || i!=j)test_failed();\
1011 j=i=(goodv2);\
1012 if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
1013 fprintf(stdout,sok);\
1014 i=-12345;\
1015 VG_UNDEF(&i,sizeof(i)); \
1016 err=opus_encoder_ctl(enc,getcall);\
1017 if(err!=OPUS_OK || i!=j)test_failed();\
1018 fprintf(stdout,gok);\
1019 cfgs+=6;
1020
1021opus_int32 test_enc_api(void)
1022{
1023 opus_uint32 enc_final_range;
1024 OpusEncoder *enc;
1025 opus_int32 i,j;
1026 unsigned char packet[1276];
1027#ifndef DISABLE_FLOAT_API
1028 float fbuf[960*2];
1029#endif
1030 short sbuf[960*2];
1031 int c,err,cfgs;
1032
1033 cfgs=0;
1034 /*First test invalid configurations which should fail*/
1035 fprintf(stdout,"\n Encoder basic API tests\n");
1036 fprintf(stdout," ---------------------------------------------------\n");
1037 for(c=0;c<4;c++)
1038 {
1039 i=opus_encoder_get_size(c);
1040 if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
1041 fprintf(stdout," opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
1042 cfgs++;
1043 }
1044
1045 /*Test with unsupported sample rates, channel counts*/
1046 for(c=0;c<4;c++)
1047 {
1048 for(i=-7;i<=96000;i++)
1049 {
1050 int fs;
1051 if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
1052 switch(i)
1053 {
1054 case(-5):fs=-8000;break;
1055 case(-6):fs=INT32_MAX;break;
1056 case(-7):fs=INT32_MIN;break;
1057 default:fs=i;
1058 }
1059 err = OPUS_OK;
1060 VG_UNDEF(&err,sizeof(err));
1061 enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
1062 if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
1063 cfgs++;
1064 opus_encoder_destroy(enc);
1065 enc=malloc(opus_encoder_get_size(2));
1066 if(enc==NULL)test_failed();
1067 err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
1068 if(err!=OPUS_BAD_ARG)test_failed();
1069 cfgs++;
1070 free(enc);
1071 }
1072 }
1073
1074 enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
1075 if(enc!=NULL)test_failed();
1076 cfgs++;
1077
1078 VG_UNDEF(&err,sizeof(err));
1079 enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
1080 if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
1081 cfgs++;
1082
1083 VG_UNDEF(&err,sizeof(err));
1084 enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
1085 if(enc==NULL)test_failed();
1086 opus_encoder_destroy(enc);
1087 cfgs++;
1088
1089 VG_UNDEF(&err,sizeof(err));
1090 enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
1091 if(err!=OPUS_OK || enc==NULL)test_failed();
1092 cfgs++;
1093 err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1094 if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1095 opus_encoder_destroy(enc);
1096 cfgs++;
1097
1098 VG_UNDEF(&err,sizeof(err));
1099 enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
1100 if(err!=OPUS_OK || enc==NULL)test_failed();
1101 cfgs++;
1102 err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1103 if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1104 opus_encoder_destroy(enc);
1105 cfgs++;
1106
1107 VG_UNDEF(&err,sizeof(err));
1108 enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
1109 if(err!=OPUS_OK || enc==NULL)test_failed();
1110 cfgs++;
1111
1112 fprintf(stdout," opus_encoder_create() ........................ OK.\n");
1113 fprintf(stdout," opus_encoder_init() .......................... OK.\n");
1114
1115 i=-12345;
1116 VG_UNDEF(&i,sizeof(i));
1117 err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1118 if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1119 cfgs++;
1120 fprintf(stdout," OPUS_GET_LOOKAHEAD ........................... OK.\n");
1121
1122 if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
1123 fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
1124 cfgs++;
1125
1126 CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
1127 OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
1128 " OPUS_SET_APPLICATION ......................... OK.\n",
1129 " OPUS_GET_APPLICATION ......................... OK.\n")
1130
1131 if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
1132 cfgs++;
1133 VG_UNDEF(&i,sizeof(i));
1134 if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
1135 if(i>700000||i<256000)test_failed();
1136 cfgs++;
1137 CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
1138 500,256000,
1139 " OPUS_SET_BITRATE ............................. OK.\n",
1140 " OPUS_GET_BITRATE ............................. OK.\n")
1141
1142 CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
1143 1,OPUS_AUTO,
1144 " OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
1145 " OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
1146
1147 i=-2;
1148 if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
1149 cfgs++;
1150 i=OPUS_BANDWIDTH_FULLBAND+1;
1151 if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
1152 cfgs++;
1153 i=OPUS_BANDWIDTH_NARROWBAND;
1154 if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1155 cfgs++;
1156 i=OPUS_BANDWIDTH_FULLBAND;
1157 if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1158 cfgs++;
1159 i=OPUS_BANDWIDTH_WIDEBAND;
1160 if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1161 cfgs++;
1162 i=OPUS_BANDWIDTH_MEDIUMBAND;
1163 if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1164 cfgs++;
1165 fprintf(stdout," OPUS_SET_BANDWIDTH ........................... OK.\n");
1166 /*We don't test if the bandwidth has actually changed.
1167 because the change may be delayed until the encoder is advanced.*/
1168 i=-12345;
1169 VG_UNDEF(&i,sizeof(i));
1170 err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
1171 if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1172 i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1173 i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
1174 cfgs++;
1175 if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
1176 cfgs++;
1177 fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
1178
1179 i=-2;
1180 if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
1181 cfgs++;
1182 i=OPUS_BANDWIDTH_FULLBAND+1;
1183 if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
1184 cfgs++;
1185 i=OPUS_BANDWIDTH_NARROWBAND;
1186 if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1187 cfgs++;
1188 i=OPUS_BANDWIDTH_FULLBAND;
1189 if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1190 cfgs++;
1191 i=OPUS_BANDWIDTH_WIDEBAND;
1192 if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1193 cfgs++;
1194 i=OPUS_BANDWIDTH_MEDIUMBAND;
1195 if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1196 cfgs++;
1197 fprintf(stdout," OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
1198 /*We don't test if the bandwidth has actually changed.
1199 because the change may be delayed until the encoder is advanced.*/
1200 i=-12345;
1201 VG_UNDEF(&i,sizeof(i));
1202 err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
1203 if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1204 i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1205 i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
1206 cfgs++;
1207 fprintf(stdout," OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
1208
1209 CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
1210 1,0,
1211 " OPUS_SET_DTX ................................. OK.\n",
1212 " OPUS_GET_DTX ................................. OK.\n")
1213
1214 CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
1215 0,10,
1216 " OPUS_SET_COMPLEXITY .......................... OK.\n",
1217 " OPUS_GET_COMPLEXITY .......................... OK.\n")
1218
1219 CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
1220 1,0,
1221 " OPUS_SET_INBAND_FEC .......................... OK.\n",
1222 " OPUS_GET_INBAND_FEC .......................... OK.\n")
1223
1224 CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
1225 100,0,
1226 " OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
1227 " OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
1228
1229 CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
1230 1,0,
1231 " OPUS_SET_VBR ................................. OK.\n",
1232 " OPUS_GET_VBR ................................. OK.\n")
1233
1234 /*CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
1235 0,50,
1236 " OPUS_SET_VOICE_RATIO ......................... OK.\n",
1237 " OPUS_GET_VOICE_RATIO ......................... OK.\n")
1238 */
1239
1240 CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
1241 1,0,
1242 " OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
1243 " OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
1244
1245 CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
1246 OPUS_SIGNAL_MUSIC,OPUS_AUTO,
1247 " OPUS_SET_SIGNAL .............................. OK.\n",
1248 " OPUS_GET_SIGNAL .............................. OK.\n")
1249
1250 CHECK_SETGET(OPUS_SET_LSB_DEPTH(i),OPUS_GET_LSB_DEPTH(&i),7,25,16,24,
1251 " OPUS_SET_LSB_DEPTH ........................... OK.\n",
1252 " OPUS_GET_LSB_DEPTH ........................... OK.\n")
1253
1254 /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
1255
1256 if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
1257 cfgs++;
1258 fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
1259
1260 /*Reset the encoder*/
1261 if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
1262 cfgs++;
1263 fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
1264
1265 memset(sbuf,0,sizeof(short)*2*960);
1266 VG_UNDEF(packet,sizeof(packet));
1267 i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
1268 if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1269 VG_CHECK(packet,i);
1270 cfgs++;
1271 fprintf(stdout," opus_encode() ................................ OK.\n");
1272#ifndef DISABLE_FLOAT_API
1273 memset(fbuf,0,sizeof(float)*2*960);
1274 VG_UNDEF(packet,sizeof(packet));
1275 i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
1276 if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1277 VG_CHECK(packet,i);
1278 cfgs++;
1279 fprintf(stdout," opus_encode_float() .......................... OK.\n");
1280#endif
1281
1282#if 0
1283 /*These tests are disabled because the library crashes with null states*/
1284 if(opus_encoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
1285 if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
1286 if(opus_encode(0,sbuf,960,packet,sizeof(packet)) !=OPUS_INVALID_STATE)test_failed();
1287 if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
1288#endif
1289 opus_encoder_destroy(enc);
1290 cfgs++;
1291 fprintf(stdout," All encoder interface tests passed\n");
1292 fprintf(stdout," (%d API invocations)\n",cfgs);
1293 return cfgs;
1294}
1295
1296#define max_out (1276*48+48*2+2)
1297int test_repacketizer_api(void)
1298{
1299 int ret,cfgs,i,j,k;
1300 OpusRepacketizer *rp;
1301 unsigned char *packet;
1302 unsigned char *po;
1303 cfgs=0;
1304 fprintf(stdout,"\n Repacketizer tests\n");
1305 fprintf(stdout," ---------------------------------------------------\n");
1306
1307 packet=malloc(max_out);
1308 if(packet==NULL)test_failed();
1309 po=malloc(max_out);
1310 if(po==NULL)test_failed();
1311 memset(packet,0,max_out);
1312
1313 i=opus_repacketizer_get_size();
1314 if(i<=0)test_failed();
1315 cfgs++;
1316 fprintf(stdout," opus_repacketizer_get_size()=%d ............. OK.\n",i);
1317
1318 rp=malloc(i);
1319 rp=opus_repacketizer_init(rp);
1320 if(rp==NULL)test_failed();
1321 cfgs++;
1322 free(rp);
1323 fprintf(stdout," opus_repacketizer_init ....................... OK.\n");
1324
1325 rp=opus_repacketizer_create();
1326 if(rp==NULL)test_failed();
1327 cfgs++;
1328 fprintf(stdout," opus_repacketizer_create ..................... OK.\n");
1329
1330 if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
1331 cfgs++;
1332 fprintf(stdout," opus_repacketizer_get_nb_frames .............. OK.\n");
1333
1334 /*Length overflows*/
1335 VG_UNDEF(packet,4);
1336 if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
1337 cfgs++;
1338 packet[0]=1;
1339 if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
1340 cfgs++;
1341 packet[0]=2;
1342 if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
1343 cfgs++;
1344 packet[0]=3;
1345 if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
1346 cfgs++;
1347 packet[0]=2;
1348 packet[1]=255;
1349 if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
1350 cfgs++;
1351 packet[0]=2;
1352 packet[1]=250;
1353 if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
1354 cfgs++;
1355 packet[0]=3;
1356 packet[1]=0;
1357 if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
1358 cfgs++;
1359 packet[1]=49;
1360 if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
1361 cfgs++;
1362 packet[0]=0;
1363 if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1364 cfgs++;
1365 packet[0]=1<<2;
1366 if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
1367 cfgs++;
1368
1369 /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
1370 opus_repacketizer_init(rp);
1371 for(j=0;j<32;j++)
1372 {
1373 /* TOC types, test half with stereo */
1374 int maxi;
1375 packet[0]=((j<<1)+(j&1))<<2;
1376 maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1377 for(i=1;i<=maxi;i++)
1378 {
1379 /* Number of CBR frames in the input packets */
1380 int maxp;
1381 packet[0]=((j<<1)+(j&1))<<2;
1382 if(i>1)packet[0]+=i==2?1:3;
1383 packet[1]=i>2?i:0;
1384 maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
1385 for(k=0;k<=(1275+75);k+=3)
1386 {
1387 /*Payload size*/
1388 opus_int32 cnt,rcnt;
1389 if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
1390 for(cnt=0;cnt<maxp+2;cnt++)
1391 {
1392 if(cnt>0)
1393 {
1394 ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
1395 if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
1396 cfgs++;
1397 }
1398 rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
1399 if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
1400 cfgs++;
1401 ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
1402 if(rcnt>0)
1403 {
1404 int len;
1405 len=k*rcnt+((rcnt*i)>2?2:1);
1406 if(ret!=len)test_failed();
1407 if((rcnt*i)<2&&(po[0]&3)!=0)test_failed(); /* Code 0 */
1408 if((rcnt*i)==2&&(po[0]&3)!=1)test_failed(); /* Code 1 */
1409 if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
1410 cfgs++;
1411 if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1412 cfgs++;
1413 if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1414 cfgs++;
1415 if(len>1)
1416 {
1417 if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1418 cfgs++;
1419 }
1420 if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1421 cfgs++;
1422 } else if (ret!=OPUS_BAD_ARG)test_failed(); /* M must not be 0 */
1423 }
1424 opus_repacketizer_init(rp);
1425 }
1426 }
1427 }
1428
1429 /*Change in input count code, CBR out*/
1430 opus_repacketizer_init(rp);
1431 packet[0]=0;
1432 if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
1433 cfgs++;
1434 packet[0]+=1;
1435 if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1436 cfgs++;
1437 i=opus_repacketizer_out(rp,po,max_out);
1438 if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
1439 cfgs++;
1440 i=opus_repacketizer_out_range(rp,0,1,po,max_out);
1441 if(i!=5||(po[0]&3)!=0)test_failed();
1442 cfgs++;
1443 i=opus_repacketizer_out_range(rp,1,2,po,max_out);
1444 if(i!=5||(po[0]&3)!=0)test_failed();
1445 cfgs++;
1446
1447 /*Change in input count code, VBR out*/
1448 opus_repacketizer_init(rp);
1449 packet[0]=1;
1450 if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1451 cfgs++;
1452 packet[0]=0;
1453 if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1454 cfgs++;
1455 i=opus_repacketizer_out(rp,po,max_out);
1456 if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
1457 cfgs++;
1458
1459 /*VBR in, VBR out*/
1460 opus_repacketizer_init(rp);
1461 packet[0]=2;
1462 packet[1]=4;
1463 if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1464 cfgs++;
1465 if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1466 cfgs++;
1467 i=opus_repacketizer_out(rp,po,max_out);
1468 if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
1469 cfgs++;
1470
1471 /*VBR in, CBR out*/
1472 opus_repacketizer_init(rp);
1473 packet[0]=2;
1474 packet[1]=4;
1475 if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1476 cfgs++;
1477 if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1478 cfgs++;
1479 i=opus_repacketizer_out(rp,po,max_out);
1480 if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
1481 cfgs++;
1482
1483 /*Count 0 in, VBR out*/
1484 for(j=0;j<32;j++)
1485 {
1486 /* TOC types, test half with stereo */
1487 int maxi,sum,rcnt;
1488 packet[0]=((j<<1)+(j&1))<<2;
1489 maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1490 sum=0;
1491 rcnt=0;
1492 opus_repacketizer_init(rp);
1493 for(i=1;i<=maxi+2;i++)
1494 {
1495 int len;
1496 ret=opus_repacketizer_cat(rp,packet,i);
1497 if(rcnt<maxi)
1498 {
1499 if(ret!=OPUS_OK)test_failed();
1500 rcnt++;
1501 sum+=i-1;
1502 } else if (ret!=OPUS_INVALID_PACKET)test_failed();
1503 cfgs++;
1504 len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
1505 if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
1506 if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
1507 if(rcnt==2&&(po[0]&3)!=2)test_failed();
1508 if(rcnt==1&&(po[0]&3)!=0)test_failed();
1509 cfgs++;
1510 if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1511 cfgs++;
1512 if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1513 cfgs++;
1514 if(len>1)
1515 {
1516 if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1517 cfgs++;
1518 }
1519 if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1520 cfgs++;
1521 }
1522 }
1523
1524 fprintf(stdout," opus_repacketizer_cat ........................ OK.\n");
1525 fprintf(stdout," opus_repacketizer_out ........................ OK.\n");
1526 fprintf(stdout," opus_repacketizer_out_range .................. OK.\n");
1527
1528 opus_repacketizer_destroy(rp);
1529 cfgs++;
1530 free(packet);
1531 free(po);
1532 fprintf(stdout," All repacketizer tests passed\n");
1533 fprintf(stdout," (%7d API invocations)\n",cfgs);
1534
1535 return cfgs;
1536}
1537
1538#ifdef MALLOC_FAIL
1539/* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
1540 * under GCC. However, this is the cleanest way to test malloc failure
1541 * handling in our codebase, and the lack of thread saftey isn't an
1542 * issue here. We therefore disable the warning for this function.
1543 */
1544#if OPUS_GNUC_PREREQ(4,6)
1545/* Save the current warning settings */
1546#pragma GCC diagnostic push
1547#endif
1548#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1549
1550typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
1551#endif
1552
1553int test_malloc_fail(void)
1554{
1555#ifdef MALLOC_FAIL
1556 OpusDecoder *dec;
1557 OpusEncoder *enc;
1558 OpusRepacketizer *rp;
1559 unsigned char mapping[256] = {0,1};
1560 OpusMSDecoder *msdec;
1561 OpusMSEncoder *msenc;
1562 int rate,c,app,cfgs,err,useerr;
1563 int *ep;
1564 mhook orig_malloc;
1565 cfgs=0;
1566#endif
1567 fprintf(stdout,"\n malloc() failure tests\n");
1568 fprintf(stdout," ---------------------------------------------------\n");
1569#ifdef MALLOC_FAIL
1570 orig_malloc=__malloc_hook;
1571 __malloc_hook=malloc_hook;
1572 ep=(int *)opus_alloc(sizeof(int));
1573 if(ep!=NULL)
1574 {
1575 if(ep)free(ep);
1576 __malloc_hook=orig_malloc;
1577#endif
1578 fprintf(stdout," opus_decoder_create() ................... SKIPPED.\n");
1579 fprintf(stdout," opus_encoder_create() ................... SKIPPED.\n");
1580 fprintf(stdout," opus_repacketizer_create() .............. SKIPPED.\n");
1581 fprintf(stdout," opus_multistream_decoder_create() ....... SKIPPED.\n");
1582 fprintf(stdout," opus_multistream_encoder_create() ....... SKIPPED.\n");
1583 fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
1584 return 0;
1585#ifdef MALLOC_FAIL
1586 }
1587 for(useerr=0;useerr<2;useerr++)
1588 {
1589 ep=useerr?&err:0;
1590 for(rate=0;rate<5;rate++)
1591 {
1592 for(c=1;c<3;c++)
1593 {
1594 err=1;
1595 if(useerr)
1596 {
1597 VG_UNDEF(&err,sizeof(err));
1598 }
1599 dec=opus_decoder_create(opus_rates[rate], c, ep);
1600 if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1601 {
1602 __malloc_hook=orig_malloc;
1603 test_failed();
1604 }
1605 cfgs++;
1606 msdec=opus_multistream_decoder_create(opus_rates[rate], c, 1, c-1, mapping, ep);
1607 if(msdec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1608 {
1609 __malloc_hook=orig_malloc;
1610 test_failed();
1611 }
1612 cfgs++;
1613 for(app=0;app<3;app++)
1614 {
1615 if(useerr)
1616 {
1617 VG_UNDEF(&err,sizeof(err));
1618 }
1619 enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
1620 if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1621 {
1622 __malloc_hook=orig_malloc;
1623 test_failed();
1624 }
1625 cfgs++;
1626 msenc=opus_multistream_encoder_create(opus_rates[rate], c, 1, c-1, mapping, opus_apps[app],ep);
1627 if(msenc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1628 {
1629 __malloc_hook=orig_malloc;
1630 test_failed();
1631 }
1632 cfgs++;
1633 }
1634 }
1635 }
1636 }
1637 rp=opus_repacketizer_create();
1638 if(rp!=NULL)
1639 {
1640 __malloc_hook=orig_malloc;
1641 test_failed();
1642 }
1643 cfgs++;
1644 __malloc_hook=orig_malloc;
1645 fprintf(stdout," opus_decoder_create() ........................ OK.\n");
1646 fprintf(stdout," opus_encoder_create() ........................ OK.\n");
1647 fprintf(stdout," opus_repacketizer_create() ................... OK.\n");
1648 fprintf(stdout," opus_multistream_decoder_create() ............ OK.\n");
1649 fprintf(stdout," opus_multistream_encoder_create() ............ OK.\n");
1650 fprintf(stdout," All malloc failure tests passed\n");
1651 fprintf(stdout," (%2d API invocations)\n",cfgs);
1652 return cfgs;
1653#endif
1654}
1655
1656#ifdef MALLOC_FAIL
1657#if __GNUC_PREREQ(4,6)
1658#pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
1659#endif
1660#endif
1661
1662int main(int _argc, char **_argv)
1663{
1664 opus_int32 total;
1665 const char * oversion;
1666 if(_argc>1)
1667 {
1668 fprintf(stderr,"Usage: %s\n",_argv[0]);
1669 return 1;
1670 }
1671 iseed=0;
1672
1673 oversion=opus_get_version_string();
1674 if(!oversion)test_failed();
1675 fprintf(stderr,"Testing the %s API deterministically\n", oversion);
1676 if(opus_strerror(-32768)==NULL)test_failed();
1677 if(opus_strerror(32767)==NULL)test_failed();
1678 if(strlen(opus_strerror(0))<1)test_failed();
1679 total=4;
1680
1681 total+=test_dec_api();
1682 total+=test_msdec_api();
1683 total+=test_parse();
1684 total+=test_enc_api();
1685 total+=test_repacketizer_api();
1686 total+=test_malloc_fail();
1687
1688 fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
1689
1690 return 0;
1691}