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