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