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