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