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