Add OPUS_{GET|SET}_GAIN CTLs for adjusting output gain.
[opus.git] / tests / test_opus_api.c
1 /* 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
71 static const opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP,
72        OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY};
73
74 void *malloc_hook(__attribute__((unused)) size_t size,
75                   __attribute__((unused)) const void *caller)
76 {
77    return 0;
78 }
79 #endif
80
81 static const opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};
82
83 opus_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
312 opus_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] = {0,1};
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    cfgs=0;
332    /*First test invalid configurations which should fail*/
333    fprintf(stdout,"\n  Multistream decoder basic API tests\n");
334    fprintf(stdout,"  ---------------------------------------------------\n");
335    for(a=-1;a<4;a++)
336    {
337       for(b=-1;b<4;b++)
338       {
339          i=opus_multistream_decoder_get_size(a,b);
340          if(((a>0&&b<=a&&b>=0)&&(i<=2048||i>((1<<16)*a)))||((a<1||b>a||b<0)&&i!=0))test_failed();
341          fprintf(stdout,"    opus_multistream_decoder_get_size(%2d,%2d)=%d %sOK.\n",a,b,i,i>0?"":"... ");
342          cfgs++;
343       }
344    }
345
346    /*Test with unsupported sample rates*/
347    for(c=1;c<3;c++)
348    {
349       for(i=-7;i<=96000;i++)
350       {
351          int fs;
352          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
353          switch(i)
354          {
355            case(-5):fs=-8000;break;
356            case(-6):fs=INT32_MAX;break;
357            case(-7):fs=INT32_MIN;break;
358            default:fs=i;
359          }
360          err = OPUS_OK;
361          VG_UNDEF(&err,sizeof(err));
362          dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, &err);
363          if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
364          cfgs++;
365          dec=malloc(opus_multistream_decoder_get_size(1,1));
366          if(dec==NULL)test_failed();
367          err = opus_multistream_decoder_init(dec,fs,c,1,c-1, mapping);
368          if(err!=OPUS_BAD_ARG)test_failed();
369          cfgs++;
370          free(dec);
371       }
372    }
373
374    VG_UNDEF(&err,sizeof(err));
375    dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, &err);
376    if(err==OPUS_OK || dec!=NULL)test_failed();
377    cfgs++;
378
379    VG_UNDEF(&err,sizeof(err));
380    mapping[0]=mapping[1]=0;
381    dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, &err);
382    if(err!=OPUS_OK || dec==NULL)test_failed();
383    cfgs++;
384    opus_multistream_decoder_destroy(dec);
385    cfgs++;
386
387    VG_UNDEF(&err,sizeof(err));
388    dec = opus_multistream_decoder_create(48000, 1, 4, 1, mapping, &err);
389    if(err!=OPUS_OK || dec==NULL)test_failed();
390    cfgs++;
391    opus_multistream_decoder_destroy(dec);
392    cfgs++;
393
394    VG_UNDEF(&err,sizeof(err));
395    dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
396    if(err!=OPUS_OK || dec==NULL)test_failed();
397    cfgs++;
398    opus_multistream_decoder_destroy(dec);
399    cfgs++;
400
401    VG_UNDEF(&err,sizeof(err));
402    mapping[0]=0;
403    mapping[1]=1;
404    dec = opus_multistream_decoder_create(48000, 2, 2, 0, mapping, &err);
405    if(err!=OPUS_OK || dec==NULL)test_failed();
406    cfgs++;
407
408    fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
409    fprintf(stdout,"    opus_multistream_decoder_init() .............. OK.\n");
410
411    VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
412    err=opus_multistream_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
413    if(err!=OPUS_OK)test_failed();
414    VG_CHECK(&dec_final_range,sizeof(dec_final_range));
415    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
416    cfgs++;
417
418    streamdec=0;
419    VG_UNDEF(&streamdec,sizeof(streamdec));
420    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(-1,&streamdec));
421    if(err!=OPUS_BAD_ARG)test_failed();
422    cfgs++;
423    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(1,&streamdec));
424    if(err!=OPUS_OK||streamdec==NULL)test_failed();
425    cfgs++;
426    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(2,&streamdec));
427    if(err!=OPUS_BAD_ARG)test_failed();
428    cfgs++;
429    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(0,&streamdec));
430    if(err!=OPUS_OK||streamdec==NULL)test_failed();
431    VG_CHECK(streamdec,opus_decoder_get_size(2));
432    fprintf(stdout,"    OPUS_MULTISTREAM_GET_DECODER_STATE ........... OK.\n");
433    cfgs++;
434
435    for(j=0;j<2;j++)
436    {
437       OpusDecoder *od;
438       err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
439       if(err != OPUS_OK)test_failed();
440       VG_UNDEF(&i,sizeof(i));
441       err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
442       VG_CHECK(&i,sizeof(i));
443       if(err != OPUS_OK || i!=0)test_failed();
444       cfgs++;
445    }
446    err=opus_multistream_decoder_ctl(dec,OPUS_SET_GAIN(15));
447    if(err!=OPUS_OK)test_failed();
448    fprintf(stdout,"    OPUS_SET_GAIN ................................ OK.\n");
449    for(j=0;j<2;j++)
450    {
451       OpusDecoder *od;
452       err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
453       if(err != OPUS_OK)test_failed();
454       VG_UNDEF(&i,sizeof(i));
455       err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
456       VG_CHECK(&i,sizeof(i));
457       if(err != OPUS_OK || i!=15)test_failed();
458       cfgs++;
459    }
460    fprintf(stdout,"    OPUS_GET_GAIN ................................ OK.\n");
461
462    err=opus_multistream_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
463    if(err!=OPUS_UNIMPLEMENTED)test_failed();
464    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
465    cfgs++;
466
467 #if 0
468    /*Currently unimplemented for multistream*/
469    VG_UNDEF(&i,sizeof(i));
470    err=opus_multistream_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
471    if(err != OPUS_OK || i!=0)test_failed();
472    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
473    cfgs++;
474    /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
475    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
476    if(err!=OPUS_BAD_ARG)test_failed();
477    cfgs++;
478    VG_UNDEF(&i,sizeof(i));
479    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
480    if(err != OPUS_OK || i>0 || i<-1)test_failed();
481    cfgs++;
482    VG_UNDEF(packet,sizeof(packet));
483    packet[0]=63<<2;packet[1]=packet[2]=0;
484    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
485    cfgs++;
486    VG_UNDEF(&i,sizeof(i));
487    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
488    if(err != OPUS_OK || i>0 || i<-1)test_failed();
489    cfgs++;
490    packet[0]=1;
491    if(opus_multistream_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
492    cfgs++;
493    VG_UNDEF(&i,sizeof(i));
494    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
495    if(err != OPUS_OK || i>0 || i<-1)test_failed();
496    cfgs++;
497    fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
498 #endif
499
500    /*Reset the decoder*/
501    if(opus_multistream_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
502    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
503    cfgs++;
504
505    opus_multistream_decoder_destroy(dec);
506    cfgs++;
507    VG_UNDEF(&err,sizeof(err));
508    dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
509    if(err!=OPUS_OK || dec==NULL)test_failed();
510    cfgs++;
511
512    packet[0]=(63<<2)+3;
513    packet[1]=49;
514    for(j=2;j<51;j++)packet[j]=0;
515    VG_UNDEF(sbuf,sizeof(sbuf));
516    if(opus_multistream_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
517    cfgs++;
518    packet[0]=(63<<2);
519    packet[1]=packet[2]=0;
520    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();}
521    cfgs++;
522    if(opus_multistream_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
523    cfgs++;
524    if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
525    cfgs++;
526    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
527    cfgs++;
528    fprintf(stdout,"    opus_multistream_decode() .................... OK.\n");
529 #ifndef DISABLE_FLOAT_API
530    VG_UNDEF(fbuf,sizeof(fbuf));
531    if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
532    cfgs++;
533    fprintf(stdout,"    opus_multistream_decode_float() .............. OK.\n");
534 #endif
535
536 #if 0
537    /*These tests are disabled because the library crashes with null states*/
538    if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
539    if(opus_multistream_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
540    if(opus_multistream_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
541    if(opus_multistream_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
542    if(opus_multistream_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
543 #endif
544    opus_multistream_decoder_destroy(dec);
545    cfgs++;
546    fprintf(stdout,"       All multistream decoder interface tests passed\n");
547    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
548    return cfgs;
549 }
550
551 #ifdef VALGRIND
552 #define UNDEFINE_FOR_PARSE  toc=-1; \
553    frames[0]=(unsigned char *)0; \
554    frames[1]=(unsigned char *)0; \
555    payload_offset=-1; \
556    VG_UNDEF(&toc,sizeof(toc)); \
557    VG_UNDEF(frames,sizeof(frames));\
558    VG_UNDEF(&payload_offset,sizeof(payload_offset));
559 #else
560 #define UNDEFINE_FOR_PARSE  toc=-1; \
561    frames[0]=(unsigned char *)0; \
562    frames[1]=(unsigned char *)0; \
563    payload_offset=-1;
564 #endif
565
566 /* This test exercises the heck out of the libopus parser.
567    It is much larger than the parser itself in part because
568    it tries to hit a lot of corner cases that could never
569    fail with the libopus code, but might be problematic for
570    other implementations. */
571 opus_int32 test_parse(void)
572 {
573    opus_int32 i,j,jj,sz;
574    unsigned char packet[1276];
575    opus_int32 cfgs,cfgs_total;
576    unsigned char toc;
577    const unsigned char *frames[48];
578    short size[48];
579    int payload_offset, ret;
580    fprintf(stdout,"\n  Packet header parsing tests\n");
581    fprintf(stdout,"  ---------------------------------------------------\n");
582    memset(packet,0,sizeof(char)*1276);
583    packet[0]=63<<2;
584    if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
585    cfgs_total=cfgs=1;
586    /*code 0*/
587    for(i=0;i<64;i++)
588    {
589       UNDEFINE_FOR_PARSE
590       ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
591       cfgs++;
592       if(ret!=1)test_failed();
593       if(size[0]!=3)test_failed();
594       if(frames[0]!=packet+1)test_failed();
595    }
596    fprintf(stdout,"    code 0 (%2d cases) ............................ OK.\n",cfgs);
597    cfgs_total+=cfgs;cfgs=0;
598
599    /*code 1, two frames of the same size*/
600    for(i=0;i<64;i++)
601    {
602       packet[0]=(i<<2)+1;
603       for(jj=0;jj<=1275*2+3;jj++)
604       {
605          UNDEFINE_FOR_PARSE
606          ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
607          cfgs++;
608          if((jj&1)==1 && jj<=2551)
609          {
610             /* Must pass if payload length even (packet length odd) and
611                size<=2551, must fail otherwise. */
612             if(ret!=2)test_failed();
613             if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
614             if(frames[0]!=packet+1)test_failed();
615             if(frames[1]!=frames[0]+size[0])test_failed();
616             if((toc>>2)!=i)test_failed();
617          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
618       }
619    }
620    fprintf(stdout,"    code 1 (%6d cases) ........................ OK.\n",cfgs);
621    cfgs_total+=cfgs;cfgs=0;
622
623    for(i=0;i<64;i++)
624    {
625       /*code 2, length code overflow*/
626       packet[0]=(i<<2)+2;
627       UNDEFINE_FOR_PARSE
628       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
629       cfgs++;
630       if(ret!=OPUS_INVALID_PACKET)test_failed();
631       packet[1]=252;
632       UNDEFINE_FOR_PARSE
633       ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
634       cfgs++;
635       if(ret!=OPUS_INVALID_PACKET)test_failed();
636       for(j=0;j<1275;j++)
637       {
638          if(j<252)packet[1]=j;
639          else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
640          /*Code 2, one too short*/
641          UNDEFINE_FOR_PARSE
642          ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
643          cfgs++;
644          if(ret!=OPUS_INVALID_PACKET)test_failed();
645          /*Code 2, one too long*/
646          UNDEFINE_FOR_PARSE
647          ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
648          cfgs++;
649          if(ret!=OPUS_INVALID_PACKET)test_failed();
650          /*Code 2, second zero*/
651          UNDEFINE_FOR_PARSE
652          ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
653          cfgs++;
654          if(ret!=2)test_failed();
655          if(size[0]!=j||size[1]!=0)test_failed();
656          if(frames[1]!=frames[0]+size[0])test_failed();
657          if((toc>>2)!=i)test_failed();
658          /*Code 2, normal*/
659          UNDEFINE_FOR_PARSE
660          ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
661          cfgs++;
662          if(ret!=2)test_failed();
663          if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
664          if(frames[1]!=frames[0]+size[0])test_failed();
665          if((toc>>2)!=i)test_failed();
666       }
667    }
668    fprintf(stdout,"    code 2 (%6d cases) ........................ OK.\n",cfgs);
669    cfgs_total+=cfgs;cfgs=0;
670
671    for(i=0;i<64;i++)
672    {
673       packet[0]=(i<<2)+3;
674       /*code 3, length code overflow*/
675       UNDEFINE_FOR_PARSE
676       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
677       cfgs++;
678       if(ret!=OPUS_INVALID_PACKET)test_failed();
679    }
680    fprintf(stdout,"    code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
681    cfgs_total+=cfgs;cfgs=0;
682
683    for(i=0;i<64;i++)
684    {
685       /*code 3, m is zero or 49-63*/
686       packet[0]=(i<<2)+3;
687       for(jj=49;jj<=64;jj++)
688       {
689         packet[1]=0+(jj&63); /*CBR, no padding*/
690         UNDEFINE_FOR_PARSE
691         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
692         cfgs++;
693         if(ret!=OPUS_INVALID_PACKET)test_failed();
694         packet[1]=128+(jj&63); /*VBR, no padding*/
695         UNDEFINE_FOR_PARSE
696         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
697         cfgs++;
698         if(ret!=OPUS_INVALID_PACKET)test_failed();
699         packet[1]=64+(jj&63); /*CBR, padding*/
700         UNDEFINE_FOR_PARSE
701         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
702         cfgs++;
703         if(ret!=OPUS_INVALID_PACKET)test_failed();
704         packet[1]=128+64+(jj&63); /*VBR, padding*/
705         UNDEFINE_FOR_PARSE
706         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
707         cfgs++;
708         if(ret!=OPUS_INVALID_PACKET)test_failed();
709       }
710    }
711    fprintf(stdout,"    code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
712    cfgs_total+=cfgs;cfgs=0;
713
714    for(i=0;i<64;i++)
715    {
716       packet[0]=(i<<2)+3;
717       /*code 3, m is one, cbr*/
718       packet[1]=1;
719       for(j=0;j<1276;j++)
720       {
721         UNDEFINE_FOR_PARSE
722         ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
723         cfgs++;
724         if(ret!=1)test_failed();
725         if(size[0]!=j)test_failed();
726         if((toc>>2)!=i)test_failed();
727       }
728       UNDEFINE_FOR_PARSE
729       ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
730       cfgs++;
731       if(ret!=OPUS_INVALID_PACKET)test_failed();
732    }
733    fprintf(stdout,"    code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
734    cfgs_total+=cfgs;cfgs=0;
735
736    for(i=0;i<64;i++)
737    {
738       int frame_samp;
739       /*code 3, m>1 CBR*/
740       packet[0]=(i<<2)+3;
741       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
742       for(j=2;j<49;j++)
743       {
744          packet[1]=j;
745          for(sz=2;sz<((j+2)*1275);sz++)
746          {
747             UNDEFINE_FOR_PARSE
748             ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
749             cfgs++;
750             /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
751             if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
752             {
753                if(ret!=j)test_failed();
754                for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
755                if((toc>>2)!=i)test_failed();
756             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
757          }
758       }
759       /*Super jumbo packets*/
760       packet[1]=5760/frame_samp;
761       UNDEFINE_FOR_PARSE
762       ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
763       cfgs++;
764       if(ret!=packet[1])test_failed();
765       for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
766    }
767    fprintf(stdout,"    code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
768    cfgs_total+=cfgs;cfgs=0;
769
770    for(i=0;i<64;i++)
771    {
772       int frame_samp;
773       /*Code 3 VBR, m one*/
774       packet[0]=(i<<2)+3;
775       packet[1]=128+1;
776       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
777       for(jj=0;jj<1276;jj++)
778       {
779          UNDEFINE_FOR_PARSE
780          ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
781          cfgs++;
782          if(ret!=1)test_failed();
783          if(size[0]!=jj)test_failed();
784          if((toc>>2)!=i)test_failed();
785       }
786       UNDEFINE_FOR_PARSE
787       ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
788       cfgs++;
789       if(ret!=OPUS_INVALID_PACKET)test_failed();
790       for(j=2;j<49;j++)
791       {
792          packet[1]=128+j;
793          /*Length code overflow*/
794          UNDEFINE_FOR_PARSE
795          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
796          cfgs++;
797          if(ret!=OPUS_INVALID_PACKET)test_failed();
798          packet[2]=252;
799          packet[3]=0;
800          for(jj=4;jj<2+j;jj++)packet[jj]=0;
801          UNDEFINE_FOR_PARSE
802          ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
803          cfgs++;
804          if(ret!=OPUS_INVALID_PACKET)test_failed();
805          /*One byte too short*/
806          for(jj=2;jj<2+j;jj++)packet[jj]=0;
807          UNDEFINE_FOR_PARSE
808          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
809          cfgs++;
810          if(ret!=OPUS_INVALID_PACKET)test_failed();
811          /*One byte too short thanks to length coding*/
812          packet[2]=252;
813          packet[3]=0;
814          for(jj=4;jj<2+j;jj++)packet[jj]=0;
815          UNDEFINE_FOR_PARSE
816          ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
817          cfgs++;
818          if(ret!=OPUS_INVALID_PACKET)test_failed();
819          /*Most expensive way of coding zeros*/
820          for(jj=2;jj<2+j;jj++)packet[jj]=0;
821          UNDEFINE_FOR_PARSE
822          ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
823          cfgs++;
824          if(frame_samp*j<=5760){
825             if(ret!=j)test_failed();
826             for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
827             if((toc>>2)!=i)test_failed();
828          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
829          /*Quasi-CBR use of mode 3*/
830          for(sz=0;sz<8;sz++)
831          {
832             const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
833             int pos=0;
834             int as=(tsz[sz]+i-j-2)/j;
835             for(jj=0;jj<j-1;jj++)
836             {
837               if(as<252){packet[2+pos]=as;pos++;}
838               else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
839             }
840             UNDEFINE_FOR_PARSE
841             ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
842             cfgs++;
843             if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
844                if(ret!=j)test_failed();
845                for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
846                if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
847                if((toc>>2)!=i)test_failed();
848             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
849          }
850       }
851    }
852    fprintf(stdout,"    code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
853    cfgs_total+=cfgs;cfgs=0;
854
855    for(i=0;i<64;i++)
856    {
857       packet[0]=(i<<2)+3;
858       /*Padding*/
859       packet[1]=128+1+64;
860       /*Overflow the length coding*/
861       for(jj=2;jj<127;jj++)packet[jj]=255;
862       UNDEFINE_FOR_PARSE
863       ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
864       cfgs++;
865       if(ret!=OPUS_INVALID_PACKET)test_failed();
866
867       for(sz=0;sz<4;sz++)
868       {
869          const int tsz[4]={0,72,512,1275};
870          for(jj=sz;jj<65025;jj+=11)
871          {
872             int pos;
873             for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
874             packet[2+pos]=jj%254;
875             pos++;
876             if(sz==0&&i==63)
877             {
878                /*Code more padding than there is room in the packet*/
879                UNDEFINE_FOR_PARSE
880                ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
881                cfgs++;
882                if(ret!=OPUS_INVALID_PACKET)test_failed();
883             }
884             UNDEFINE_FOR_PARSE
885             ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
886             cfgs++;
887             if(tsz[sz]+i<1276)
888             {
889                if(ret!=1)test_failed();
890                if(size[0]!=tsz[sz]+i)test_failed();
891                if((toc>>2)!=i)test_failed();
892             } else if (ret!=OPUS_INVALID_PACKET)test_failed();
893          }
894       }
895    }
896    fprintf(stdout,"    code 3 padding (%2d cases) ............... OK.\n",cfgs);
897    cfgs_total+=cfgs;cfgs=0;
898    fprintf(stdout,"    opus_packet_parse ............................ OK.\n");
899    fprintf(stdout,"                      All packet parsing tests passed\n");
900    fprintf(stdout,"                          (%d API invocations)\n",cfgs_total);
901    return cfgs_total;
902 }
903
904 /* This is a helper macro for the encoder tests.
905    The encoder api tests all have a pattern of set-must-fail, set-must-fail,
906    set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
907 #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
908    i=(badv);\
909    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
910    i=(badv2);\
911    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
912    j=i=(goodv);\
913    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
914    i=-12345;\
915    VG_UNDEF(&i,sizeof(i)); \
916    err=opus_encoder_ctl(enc,getcall);\
917    if(err!=OPUS_OK || i!=j)test_failed();\
918    j=i=(goodv2);\
919    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
920    fprintf(stdout,sok);\
921    i=-12345;\
922    VG_UNDEF(&i,sizeof(i)); \
923    err=opus_encoder_ctl(enc,getcall);\
924    if(err!=OPUS_OK || i!=j)test_failed();\
925    fprintf(stdout,gok);\
926    cfgs+=6;
927
928 opus_int32 test_enc_api(void)
929 {
930    opus_uint32 enc_final_range;
931    OpusEncoder *enc;
932    opus_int32 i,j;
933    unsigned char packet[1276];
934 #ifndef DISABLE_FLOAT_API
935    float fbuf[960*2];
936 #endif
937    short sbuf[960*2];
938    int c,err,cfgs;
939
940    cfgs=0;
941    /*First test invalid configurations which should fail*/
942    fprintf(stdout,"\n  Encoder basic API tests\n");
943    fprintf(stdout,"  ---------------------------------------------------\n");
944    for(c=0;c<4;c++)
945    {
946       i=opus_encoder_get_size(c);
947       if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
948       fprintf(stdout,"    opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
949       cfgs++;
950    }
951
952    /*Test with unsupported sample rates, channel counts*/
953    for(c=0;c<4;c++)
954    {
955       for(i=-7;i<=96000;i++)
956       {
957          int fs;
958          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
959          switch(i)
960          {
961            case(-5):fs=-8000;break;
962            case(-6):fs=INT32_MAX;break;
963            case(-7):fs=INT32_MIN;break;
964            default:fs=i;
965          }
966          err = OPUS_OK;
967          VG_UNDEF(&err,sizeof(err));
968          enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
969          if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
970          cfgs++;
971          opus_encoder_destroy(enc);
972          enc=malloc(opus_encoder_get_size(2));
973          if(enc==NULL)test_failed();
974          err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
975          if(err!=OPUS_BAD_ARG)test_failed();
976          cfgs++;
977          free(enc);
978       }
979    }
980
981    enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
982    if(enc!=NULL)test_failed();
983    cfgs++;
984
985    VG_UNDEF(&err,sizeof(err));
986    enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
987    if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
988    cfgs++;
989
990    VG_UNDEF(&err,sizeof(err));
991    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
992    if(enc==NULL)test_failed();
993    opus_encoder_destroy(enc);
994    cfgs++;
995
996    VG_UNDEF(&err,sizeof(err));
997    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
998    if(err!=OPUS_OK || enc==NULL)test_failed();
999    cfgs++;
1000    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1001    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1002    opus_encoder_destroy(enc);
1003    cfgs++;
1004
1005    VG_UNDEF(&err,sizeof(err));
1006    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
1007    if(err!=OPUS_OK || enc==NULL)test_failed();
1008    cfgs++;
1009    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1010    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1011    opus_encoder_destroy(enc);
1012    cfgs++;
1013
1014    VG_UNDEF(&err,sizeof(err));
1015    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
1016    if(err!=OPUS_OK || enc==NULL)test_failed();
1017    cfgs++;
1018
1019    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
1020    fprintf(stdout,"    opus_encoder_init() .......................... OK.\n");
1021
1022    i=-12345;
1023    VG_UNDEF(&i,sizeof(i));
1024    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1025    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1026    cfgs++;
1027    fprintf(stdout,"    OPUS_GET_LOOKAHEAD ........................... OK.\n");
1028
1029    if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
1030    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
1031    cfgs++;
1032
1033    CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
1034      OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
1035      "    OPUS_SET_APPLICATION ......................... OK.\n",
1036      "    OPUS_GET_APPLICATION ......................... OK.\n")
1037
1038    if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
1039    cfgs++;
1040    VG_UNDEF(&i,sizeof(i));
1041    if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
1042    if(i>700000||i<256000)test_failed();
1043    cfgs++;
1044    CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
1045      500,256000,
1046      "    OPUS_SET_BITRATE ............................. OK.\n",
1047      "    OPUS_GET_BITRATE ............................. OK.\n")
1048
1049    CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
1050      1,OPUS_AUTO,
1051      "    OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
1052      "    OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
1053
1054    i=-2;
1055    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
1056    cfgs++;
1057    i=OPUS_BANDWIDTH_FULLBAND+1;
1058    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
1059    cfgs++;
1060    i=OPUS_BANDWIDTH_NARROWBAND;
1061    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1062    cfgs++;
1063    i=OPUS_BANDWIDTH_FULLBAND;
1064    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1065    cfgs++;
1066    i=OPUS_BANDWIDTH_WIDEBAND;
1067    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1068    cfgs++;
1069    i=OPUS_BANDWIDTH_MEDIUMBAND;
1070    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1071    cfgs++;
1072    fprintf(stdout,"    OPUS_SET_BANDWIDTH ........................... OK.\n");
1073    /*We don't test if the bandwidth has actually changed.
1074      because the change may be delayed until the encoder is advanced.*/
1075    i=-12345;
1076    VG_UNDEF(&i,sizeof(i));
1077    err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
1078    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1079       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1080       i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
1081    cfgs++;
1082    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
1083    cfgs++;
1084    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
1085
1086    i=-2;
1087    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
1088    cfgs++;
1089    i=OPUS_BANDWIDTH_FULLBAND+1;
1090    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
1091    cfgs++;
1092    i=OPUS_BANDWIDTH_NARROWBAND;
1093    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1094    cfgs++;
1095    i=OPUS_BANDWIDTH_FULLBAND;
1096    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1097    cfgs++;
1098    i=OPUS_BANDWIDTH_WIDEBAND;
1099    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1100    cfgs++;
1101    i=OPUS_BANDWIDTH_MEDIUMBAND;
1102    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1103    cfgs++;
1104    fprintf(stdout,"    OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
1105    /*We don't test if the bandwidth has actually changed.
1106      because the change may be delayed until the encoder is advanced.*/
1107    i=-12345;
1108    VG_UNDEF(&i,sizeof(i));
1109    err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
1110    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1111       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1112       i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
1113    cfgs++;
1114    fprintf(stdout,"    OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
1115
1116    CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
1117      1,0,
1118      "    OPUS_SET_DTX ................................. OK.\n",
1119      "    OPUS_GET_DTX ................................. OK.\n")
1120
1121    CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
1122      0,10,
1123      "    OPUS_SET_COMPLEXITY .......................... OK.\n",
1124      "    OPUS_GET_COMPLEXITY .......................... OK.\n")
1125
1126    CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
1127      1,0,
1128      "    OPUS_SET_INBAND_FEC .......................... OK.\n",
1129      "    OPUS_GET_INBAND_FEC .......................... OK.\n")
1130
1131    CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
1132      100,0,
1133      "    OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
1134      "    OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
1135
1136    CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
1137      1,0,
1138      "    OPUS_SET_VBR ................................. OK.\n",
1139      "    OPUS_GET_VBR ................................. OK.\n")
1140
1141    /*CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
1142      0,50,
1143      "    OPUS_SET_VOICE_RATIO ......................... OK.\n",
1144      "    OPUS_GET_VOICE_RATIO ......................... OK.\n")
1145    */
1146
1147    CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
1148      1,0,
1149      "    OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
1150      "    OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
1151
1152    CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
1153      OPUS_SIGNAL_MUSIC,OPUS_AUTO,
1154      "    OPUS_SET_SIGNAL .............................. OK.\n",
1155      "    OPUS_GET_SIGNAL .............................. OK.\n")
1156
1157    /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
1158
1159    if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
1160    cfgs++;
1161    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
1162
1163    /*Reset the encoder*/
1164    if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
1165    cfgs++;
1166    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
1167
1168    memset(sbuf,0,sizeof(short)*2*960);
1169    VG_UNDEF(packet,sizeof(packet));
1170    i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
1171    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1172    VG_CHECK(packet,i);
1173    cfgs++;
1174    fprintf(stdout,"    opus_encode() ................................ OK.\n");
1175 #ifndef DISABLE_FLOAT_API
1176    memset(fbuf,0,sizeof(float)*2*960);
1177    VG_UNDEF(packet,sizeof(packet));
1178    i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
1179    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1180    VG_CHECK(packet,i);
1181    cfgs++;
1182    fprintf(stdout,"    opus_encode_float() .......................... OK.\n");
1183 #endif
1184
1185 #if 0
1186    /*These tests are disabled because the library crashes with null states*/
1187    if(opus_encoder_ctl(0,OPUS_RESET_STATE)               !=OPUS_INVALID_STATE)test_failed();
1188    if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
1189    if(opus_encode(0,sbuf,960,packet,sizeof(packet))      !=OPUS_INVALID_STATE)test_failed();
1190    if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
1191 #endif
1192    opus_encoder_destroy(enc);
1193    cfgs++;
1194    fprintf(stdout,"                   All encoder interface tests passed\n");
1195    fprintf(stdout,"                             (%d API invocations)\n",cfgs);
1196    return cfgs;
1197 }
1198
1199 #define max_out (1276*48+48*2+2)
1200 int test_repacketizer_api(void)
1201 {
1202    int ret,cfgs,i,j,k;
1203    OpusRepacketizer *rp;
1204    unsigned char *packet;
1205    unsigned char *po;
1206    cfgs=0;
1207    fprintf(stdout,"\n  Repacketizer tests\n");
1208    fprintf(stdout,"  ---------------------------------------------------\n");
1209
1210    packet=malloc(max_out);
1211    if(packet==NULL)test_failed();
1212    po=malloc(max_out);
1213    if(po==NULL)test_failed();
1214    memset(packet,0,max_out);
1215
1216    i=opus_repacketizer_get_size();
1217    if(i<=0)test_failed();
1218    cfgs++;
1219    fprintf(stdout,"    opus_repacketizer_get_size()=%d ............. OK.\n",i);
1220
1221    rp=malloc(i);
1222    rp=opus_repacketizer_init(rp);
1223    if(rp==NULL)test_failed();
1224    cfgs++;
1225    free(rp);
1226    fprintf(stdout,"    opus_repacketizer_init ....................... OK.\n");
1227
1228    rp=opus_repacketizer_create();
1229    if(rp==NULL)test_failed();
1230    cfgs++;
1231    fprintf(stdout,"    opus_repacketizer_create ..................... OK.\n");
1232
1233    if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
1234    cfgs++;
1235    fprintf(stdout,"    opus_repacketizer_get_nb_frames .............. OK.\n");
1236
1237    /*Length overflows*/
1238    VG_UNDEF(packet,4);
1239    if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
1240    cfgs++;
1241    packet[0]=1;
1242    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
1243    cfgs++;
1244    packet[0]=2;
1245    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
1246    cfgs++;
1247    packet[0]=3;
1248    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
1249    cfgs++;
1250    packet[0]=2;
1251    packet[1]=255;
1252    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
1253    cfgs++;
1254    packet[0]=2;
1255    packet[1]=250;
1256    if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
1257    cfgs++;
1258    packet[0]=3;
1259    packet[1]=0;
1260    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
1261    cfgs++;
1262    packet[1]=49;
1263    if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
1264    cfgs++;
1265    packet[0]=0;
1266    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1267    cfgs++;
1268    packet[0]=1<<2;
1269    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
1270    cfgs++;
1271
1272    /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
1273    opus_repacketizer_init(rp);
1274    for(j=0;j<32;j++)
1275    {
1276       /* TOC types, test half with stereo */
1277       int maxi;
1278       packet[0]=((j<<1)+(j&1))<<2;
1279       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1280       for(i=1;i<=maxi;i++)
1281       {
1282          /* Number of CBR frames in the input packets */
1283          int maxp;
1284          packet[0]=((j<<1)+(j&1))<<2;
1285          if(i>1)packet[0]+=i==2?1:3;
1286          packet[1]=i>2?i:0;
1287          maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
1288          for(k=0;k<=(1275+75);k+=3)
1289          {
1290             /*Payload size*/
1291             opus_int32 cnt,rcnt;
1292             if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
1293             for(cnt=0;cnt<maxp+2;cnt++)
1294             {
1295                if(cnt>0)
1296                {
1297                   ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
1298                   if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
1299                   cfgs++;
1300                }
1301                rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
1302                if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
1303                cfgs++;
1304                ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
1305                if(rcnt>0)
1306                {
1307                   int len;
1308                   len=k*rcnt+((rcnt*i)>2?2:1);
1309                   if(ret!=len)test_failed();
1310                   if((rcnt*i)<2&&(po[0]&3)!=0)test_failed();                      /* Code 0 */
1311                   if((rcnt*i)==2&&(po[0]&3)!=1)test_failed();                     /* Code 1 */
1312                   if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
1313                   cfgs++;
1314                   if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1315                   cfgs++;
1316                   if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1317                   cfgs++;
1318                   if(len>1)
1319                   {
1320                      if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1321                      cfgs++;
1322                   }
1323                   if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1324                   cfgs++;
1325                } else if (ret!=OPUS_BAD_ARG)test_failed();                        /* M must not be 0 */
1326             }
1327             opus_repacketizer_init(rp);
1328          }
1329       }
1330    }
1331
1332    /*Change in input count code, CBR out*/
1333    opus_repacketizer_init(rp);
1334    packet[0]=0;
1335    if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
1336    cfgs++;
1337    packet[0]+=1;
1338    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1339    cfgs++;
1340    i=opus_repacketizer_out(rp,po,max_out);
1341    if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
1342    cfgs++;
1343    i=opus_repacketizer_out_range(rp,0,1,po,max_out);
1344    if(i!=5||(po[0]&3)!=0)test_failed();
1345    cfgs++;
1346    i=opus_repacketizer_out_range(rp,1,2,po,max_out);
1347    if(i!=5||(po[0]&3)!=0)test_failed();
1348    cfgs++;
1349
1350    /*Change in input count code, VBR out*/
1351    opus_repacketizer_init(rp);
1352    packet[0]=1;
1353    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1354    cfgs++;
1355    packet[0]=0;
1356    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1357    cfgs++;
1358    i=opus_repacketizer_out(rp,po,max_out);
1359    if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
1360    cfgs++;
1361
1362    /*VBR in, VBR out*/
1363    opus_repacketizer_init(rp);
1364    packet[0]=2;
1365    packet[1]=4;
1366    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1367    cfgs++;
1368    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1369    cfgs++;
1370    i=opus_repacketizer_out(rp,po,max_out);
1371    if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
1372    cfgs++;
1373
1374    /*VBR in, CBR out*/
1375    opus_repacketizer_init(rp);
1376    packet[0]=2;
1377    packet[1]=4;
1378    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1379    cfgs++;
1380    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1381    cfgs++;
1382    i=opus_repacketizer_out(rp,po,max_out);
1383    if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
1384    cfgs++;
1385
1386    /*Count 0 in, VBR out*/
1387    for(j=0;j<32;j++)
1388    {
1389       /* TOC types, test half with stereo */
1390       int maxi,sum,rcnt;
1391       packet[0]=((j<<1)+(j&1))<<2;
1392       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1393       sum=0;
1394       rcnt=0;
1395       opus_repacketizer_init(rp);
1396       for(i=1;i<=maxi+2;i++)
1397       {
1398          int len;
1399          ret=opus_repacketizer_cat(rp,packet,i);
1400          if(rcnt<maxi)
1401          {
1402             if(ret!=OPUS_OK)test_failed();
1403             rcnt++;
1404             sum+=i-1;
1405          } else if (ret!=OPUS_INVALID_PACKET)test_failed();
1406          cfgs++;
1407          len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
1408          if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
1409          if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
1410          if(rcnt==2&&(po[0]&3)!=2)test_failed();
1411          if(rcnt==1&&(po[0]&3)!=0)test_failed();
1412          cfgs++;
1413          if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1414          cfgs++;
1415          if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1416          cfgs++;
1417          if(len>1)
1418          {
1419             if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1420             cfgs++;
1421          }
1422          if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1423          cfgs++;
1424       }
1425    }
1426
1427    fprintf(stdout,"    opus_repacketizer_cat ........................ OK.\n");
1428    fprintf(stdout,"    opus_repacketizer_out ........................ OK.\n");
1429    fprintf(stdout,"    opus_repacketizer_out_range .................. OK.\n");
1430
1431    opus_repacketizer_destroy(rp);
1432    cfgs++;
1433    free(packet);
1434    free(po);
1435    fprintf(stdout,"                        All repacketizer tests passed\n");
1436    fprintf(stdout,"                            (%7d API invocations)\n",cfgs);
1437
1438    return cfgs;
1439 }
1440
1441 #ifdef MALLOC_FAIL
1442 /* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
1443  * under GCC. However, this is the cleanest way to test malloc failure
1444  * handling in our codebase, and the lack of thread saftey isn't an
1445  * issue here. We therefore disable the warning for this function.
1446  */
1447 #if OPUS_GNUC_PREREQ(4,6)
1448 /* Save the current warning settings */
1449 #pragma GCC diagnostic push
1450 #endif
1451 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1452
1453 typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
1454 #endif
1455
1456 int test_malloc_fail(void)
1457 {
1458 #ifdef MALLOC_FAIL
1459    OpusDecoder *dec;
1460    OpusEncoder *enc;
1461    OpusRepacketizer *rp;
1462    unsigned char mapping[256] = {0,1};
1463    OpusMSDecoder *msdec;
1464    OpusMSEncoder *msenc;
1465    int rate,c,app,cfgs,err,useerr;
1466    int *ep;
1467    mhook orig_malloc;
1468    cfgs=0;
1469 #endif
1470    fprintf(stdout,"\n  malloc() failure tests\n");
1471    fprintf(stdout,"  ---------------------------------------------------\n");
1472 #ifdef MALLOC_FAIL
1473    orig_malloc=__malloc_hook;
1474    __malloc_hook=malloc_hook;
1475    ep=(int *)opus_alloc(sizeof(int));
1476    if(ep!=NULL)
1477    {
1478       if(ep)free(ep);
1479       __malloc_hook=orig_malloc;
1480 #endif
1481       fprintf(stdout,"    opus_decoder_create() ................... SKIPPED.\n");
1482       fprintf(stdout,"    opus_encoder_create() ................... SKIPPED.\n");
1483       fprintf(stdout,"    opus_repacketizer_create() .............. SKIPPED.\n");
1484       fprintf(stdout,"    opus_multistream_decoder_create() ....... SKIPPED.\n");
1485       fprintf(stdout,"    opus_multistream_encoder_create() ....... SKIPPED.\n");
1486       fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
1487       return 0;
1488 #ifdef MALLOC_FAIL
1489    }
1490    for(useerr=0;useerr<2;useerr++)
1491    {
1492       ep=useerr?&err:0;
1493       for(rate=0;rate<5;rate++)
1494       {
1495         for(c=1;c<3;c++)
1496         {
1497            err=1;
1498            if(useerr)
1499            {
1500               VG_UNDEF(&err,sizeof(err));
1501            }
1502            dec=opus_decoder_create(opus_rates[rate], c, ep);
1503            if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1504            {
1505               __malloc_hook=orig_malloc;
1506               test_failed();
1507            }
1508            cfgs++;
1509            msdec=opus_multistream_decoder_create(opus_rates[rate], c, 1, c-1, mapping, ep);
1510            if(msdec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1511            {
1512               __malloc_hook=orig_malloc;
1513               test_failed();
1514            }
1515            cfgs++;
1516            for(app=0;app<3;app++)
1517            {
1518               if(useerr)
1519               {
1520                  VG_UNDEF(&err,sizeof(err));
1521               }
1522               enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
1523               if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1524               {
1525                  __malloc_hook=orig_malloc;
1526                  test_failed();
1527               }
1528               cfgs++;
1529               msenc=opus_multistream_encoder_create(opus_rates[rate], c, 1, c-1, mapping, opus_apps[app],ep);
1530               if(msenc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1531               {
1532                  __malloc_hook=orig_malloc;
1533                  test_failed();
1534               }
1535               cfgs++;
1536            }
1537         }
1538      }
1539    }
1540    rp=opus_repacketizer_create();
1541    if(rp!=NULL)
1542    {
1543       __malloc_hook=orig_malloc;
1544       test_failed();
1545    }
1546    cfgs++;
1547    __malloc_hook=orig_malloc;
1548    fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
1549    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
1550    fprintf(stdout,"    opus_repacketizer_create() ................... OK.\n");
1551    fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
1552    fprintf(stdout,"    opus_multistream_encoder_create() ............ OK.\n");
1553    fprintf(stdout,"                      All malloc failure tests passed\n");
1554    fprintf(stdout,"                                 (%2d API invocations)\n",cfgs);
1555    return cfgs;
1556 #endif
1557 }
1558
1559 #ifdef MALLOC_FAIL
1560 #if __GNUC_PREREQ(4,6)
1561 #pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
1562 #endif
1563 #endif
1564
1565 int main(int _argc, char **_argv)
1566 {
1567    opus_int32 total;
1568    const char * oversion;
1569    if(_argc>1)
1570    {
1571       fprintf(stderr,"Usage: %s\n",_argv[0]);
1572       return 1;
1573    }
1574    iseed=0;
1575
1576    oversion=opus_get_version_string();
1577    if(!oversion)test_failed();
1578    fprintf(stderr,"Testing the %s API deterministically\n", oversion);
1579    if(opus_strerror(-32768)==NULL)test_failed();
1580    if(opus_strerror(32767)==NULL)test_failed();
1581    if(strlen(opus_strerror(0))<1)test_failed();
1582    total=4;
1583
1584    total+=test_dec_api();
1585    total+=test_msdec_api();
1586    total+=test_parse();
1587    total+=test_enc_api();
1588    total+=test_repacketizer_api();
1589    total+=test_malloc_fail();
1590
1591    fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
1592
1593    return 0;
1594 }