43c6d1437e8d59a27b3b8c22ca735ade9e67c8fb
[opus.git] / tests / test_opus_api.c
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
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
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
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, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
657    cfgs++;
658    if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
659    cfgs++;
660    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
661    cfgs++;
662    fprintf(stdout,"    opus_multistream_decode() .................... OK.\n");
663 #ifndef DISABLE_FLOAT_API
664    VG_UNDEF(fbuf,sizeof(fbuf));
665    if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
666    cfgs++;
667    fprintf(stdout,"    opus_multistream_decode_float() .............. OK.\n");
668 #endif
669
670 #if 0
671    /*These tests are disabled because the library crashes with null states*/
672    if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
673    if(opus_multistream_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
674    if(opus_multistream_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
675    if(opus_multistream_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
676    if(opus_multistream_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
677 #endif
678    opus_multistream_decoder_destroy(dec);
679    cfgs++;
680    fprintf(stdout,"       All multistream decoder interface tests passed\n");
681    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
682    return cfgs;
683 }
684
685 #ifdef VALGRIND
686 #define UNDEFINE_FOR_PARSE  toc=-1; \
687    frames[0]=(unsigned char *)0; \
688    frames[1]=(unsigned char *)0; \
689    payload_offset=-1; \
690    VG_UNDEF(&toc,sizeof(toc)); \
691    VG_UNDEF(frames,sizeof(frames));\
692    VG_UNDEF(&payload_offset,sizeof(payload_offset));
693 #else
694 #define UNDEFINE_FOR_PARSE  toc=-1; \
695    frames[0]=(unsigned char *)0; \
696    frames[1]=(unsigned char *)0; \
697    payload_offset=-1;
698 #endif
699
700 /* This test exercises the heck out of the libopus parser.
701    It is much larger than the parser itself in part because
702    it tries to hit a lot of corner cases that could never
703    fail with the libopus code, but might be problematic for
704    other implementations. */
705 opus_int32 test_parse(void)
706 {
707    opus_int32 i,j,jj,sz;
708    unsigned char packet[1276];
709    opus_int32 cfgs,cfgs_total;
710    unsigned char toc;
711    const unsigned char *frames[48];
712    short size[48];
713    int payload_offset, ret;
714    fprintf(stdout,"\n  Packet header parsing tests\n");
715    fprintf(stdout,"  ---------------------------------------------------\n");
716    memset(packet,0,sizeof(char)*1276);
717    packet[0]=63<<2;
718    if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
719    cfgs_total=cfgs=1;
720    /*code 0*/
721    for(i=0;i<64;i++)
722    {
723       packet[0]=i<<2;
724       UNDEFINE_FOR_PARSE
725       ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
726       cfgs++;
727       if(ret!=1)test_failed();
728       if(size[0]!=3)test_failed();
729       if(frames[0]!=packet+1)test_failed();
730    }
731    fprintf(stdout,"    code 0 (%2d cases) ............................ OK.\n",cfgs);
732    cfgs_total+=cfgs;cfgs=0;
733
734    /*code 1, two frames of the same size*/
735    for(i=0;i<64;i++)
736    {
737       packet[0]=(i<<2)+1;
738       for(jj=0;jj<=1275*2+3;jj++)
739       {
740          UNDEFINE_FOR_PARSE
741          ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
742          cfgs++;
743          if((jj&1)==1 && jj<=2551)
744          {
745             /* Must pass if payload length even (packet length odd) and
746                size<=2551, must fail otherwise. */
747             if(ret!=2)test_failed();
748             if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
749             if(frames[0]!=packet+1)test_failed();
750             if(frames[1]!=frames[0]+size[0])test_failed();
751             if((toc>>2)!=i)test_failed();
752          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
753       }
754    }
755    fprintf(stdout,"    code 1 (%6d cases) ........................ OK.\n",cfgs);
756    cfgs_total+=cfgs;cfgs=0;
757
758    for(i=0;i<64;i++)
759    {
760       /*code 2, length code overflow*/
761       packet[0]=(i<<2)+2;
762       UNDEFINE_FOR_PARSE
763       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
764       cfgs++;
765       if(ret!=OPUS_INVALID_PACKET)test_failed();
766       packet[1]=252;
767       UNDEFINE_FOR_PARSE
768       ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
769       cfgs++;
770       if(ret!=OPUS_INVALID_PACKET)test_failed();
771       for(j=0;j<1275;j++)
772       {
773          if(j<252)packet[1]=j;
774          else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
775          /*Code 2, one too short*/
776          UNDEFINE_FOR_PARSE
777          ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
778          cfgs++;
779          if(ret!=OPUS_INVALID_PACKET)test_failed();
780          /*Code 2, one too long*/
781          UNDEFINE_FOR_PARSE
782          ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
783          cfgs++;
784          if(ret!=OPUS_INVALID_PACKET)test_failed();
785          /*Code 2, second zero*/
786          UNDEFINE_FOR_PARSE
787          ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
788          cfgs++;
789          if(ret!=2)test_failed();
790          if(size[0]!=j||size[1]!=0)test_failed();
791          if(frames[1]!=frames[0]+size[0])test_failed();
792          if((toc>>2)!=i)test_failed();
793          /*Code 2, normal*/
794          UNDEFINE_FOR_PARSE
795          ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
796          cfgs++;
797          if(ret!=2)test_failed();
798          if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
799          if(frames[1]!=frames[0]+size[0])test_failed();
800          if((toc>>2)!=i)test_failed();
801       }
802    }
803    fprintf(stdout,"    code 2 (%6d cases) ........................ OK.\n",cfgs);
804    cfgs_total+=cfgs;cfgs=0;
805
806    for(i=0;i<64;i++)
807    {
808       packet[0]=(i<<2)+3;
809       /*code 3, length code overflow*/
810       UNDEFINE_FOR_PARSE
811       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
812       cfgs++;
813       if(ret!=OPUS_INVALID_PACKET)test_failed();
814    }
815    fprintf(stdout,"    code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
816    cfgs_total+=cfgs;cfgs=0;
817
818    for(i=0;i<64;i++)
819    {
820       /*code 3, m is zero or 49-63*/
821       packet[0]=(i<<2)+3;
822       for(jj=49;jj<=64;jj++)
823       {
824         packet[1]=0+(jj&63); /*CBR, no padding*/
825         UNDEFINE_FOR_PARSE
826         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
827         cfgs++;
828         if(ret!=OPUS_INVALID_PACKET)test_failed();
829         packet[1]=128+(jj&63); /*VBR, no padding*/
830         UNDEFINE_FOR_PARSE
831         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
832         cfgs++;
833         if(ret!=OPUS_INVALID_PACKET)test_failed();
834         packet[1]=64+(jj&63); /*CBR, padding*/
835         UNDEFINE_FOR_PARSE
836         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
837         cfgs++;
838         if(ret!=OPUS_INVALID_PACKET)test_failed();
839         packet[1]=128+64+(jj&63); /*VBR, padding*/
840         UNDEFINE_FOR_PARSE
841         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
842         cfgs++;
843         if(ret!=OPUS_INVALID_PACKET)test_failed();
844       }
845    }
846    fprintf(stdout,"    code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
847    cfgs_total+=cfgs;cfgs=0;
848
849    for(i=0;i<64;i++)
850    {
851       packet[0]=(i<<2)+3;
852       /*code 3, m is one, cbr*/
853       packet[1]=1;
854       for(j=0;j<1276;j++)
855       {
856         UNDEFINE_FOR_PARSE
857         ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
858         cfgs++;
859         if(ret!=1)test_failed();
860         if(size[0]!=j)test_failed();
861         if((toc>>2)!=i)test_failed();
862       }
863       UNDEFINE_FOR_PARSE
864       ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
865       cfgs++;
866       if(ret!=OPUS_INVALID_PACKET)test_failed();
867    }
868    fprintf(stdout,"    code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
869    cfgs_total+=cfgs;cfgs=0;
870
871    for(i=0;i<64;i++)
872    {
873       int frame_samp;
874       /*code 3, m>1 CBR*/
875       packet[0]=(i<<2)+3;
876       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
877       for(j=2;j<49;j++)
878       {
879          packet[1]=j;
880          for(sz=2;sz<((j+2)*1275);sz++)
881          {
882             UNDEFINE_FOR_PARSE
883             ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
884             cfgs++;
885             /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
886             if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
887             {
888                if(ret!=j)test_failed();
889                for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
890                if((toc>>2)!=i)test_failed();
891             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
892          }
893       }
894       /*Super jumbo packets*/
895       packet[1]=5760/frame_samp;
896       UNDEFINE_FOR_PARSE
897       ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
898       cfgs++;
899       if(ret!=packet[1])test_failed();
900       for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
901    }
902    fprintf(stdout,"    code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
903    cfgs_total+=cfgs;cfgs=0;
904
905    for(i=0;i<64;i++)
906    {
907       int frame_samp;
908       /*Code 3 VBR, m one*/
909       packet[0]=(i<<2)+3;
910       packet[1]=128+1;
911       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
912       for(jj=0;jj<1276;jj++)
913       {
914          UNDEFINE_FOR_PARSE
915          ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
916          cfgs++;
917          if(ret!=1)test_failed();
918          if(size[0]!=jj)test_failed();
919          if((toc>>2)!=i)test_failed();
920       }
921       UNDEFINE_FOR_PARSE
922       ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
923       cfgs++;
924       if(ret!=OPUS_INVALID_PACKET)test_failed();
925       for(j=2;j<49;j++)
926       {
927          packet[1]=128+j;
928          /*Length code overflow*/
929          UNDEFINE_FOR_PARSE
930          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
931          cfgs++;
932          if(ret!=OPUS_INVALID_PACKET)test_failed();
933          packet[2]=252;
934          packet[3]=0;
935          for(jj=4;jj<2+j;jj++)packet[jj]=0;
936          UNDEFINE_FOR_PARSE
937          ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
938          cfgs++;
939          if(ret!=OPUS_INVALID_PACKET)test_failed();
940          /*One byte too short*/
941          for(jj=2;jj<2+j;jj++)packet[jj]=0;
942          UNDEFINE_FOR_PARSE
943          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
944          cfgs++;
945          if(ret!=OPUS_INVALID_PACKET)test_failed();
946          /*One byte too short thanks to length coding*/
947          packet[2]=252;
948          packet[3]=0;
949          for(jj=4;jj<2+j;jj++)packet[jj]=0;
950          UNDEFINE_FOR_PARSE
951          ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
952          cfgs++;
953          if(ret!=OPUS_INVALID_PACKET)test_failed();
954          /*Most expensive way of coding zeros*/
955          for(jj=2;jj<2+j;jj++)packet[jj]=0;
956          UNDEFINE_FOR_PARSE
957          ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
958          cfgs++;
959          if(frame_samp*j<=5760){
960             if(ret!=j)test_failed();
961             for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
962             if((toc>>2)!=i)test_failed();
963          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
964          /*Quasi-CBR use of mode 3*/
965          for(sz=0;sz<8;sz++)
966          {
967             const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
968             int pos=0;
969             int as=(tsz[sz]+i-j-2)/j;
970             for(jj=0;jj<j-1;jj++)
971             {
972               if(as<252){packet[2+pos]=as;pos++;}
973               else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
974             }
975             UNDEFINE_FOR_PARSE
976             ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
977             cfgs++;
978             if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
979                if(ret!=j)test_failed();
980                for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
981                if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
982                if((toc>>2)!=i)test_failed();
983             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
984          }
985       }
986    }
987    fprintf(stdout,"    code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
988    cfgs_total+=cfgs;cfgs=0;
989
990    for(i=0;i<64;i++)
991    {
992       packet[0]=(i<<2)+3;
993       /*Padding*/
994       packet[1]=128+1+64;
995       /*Overflow the length coding*/
996       for(jj=2;jj<127;jj++)packet[jj]=255;
997       UNDEFINE_FOR_PARSE
998       ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
999       cfgs++;
1000       if(ret!=OPUS_INVALID_PACKET)test_failed();
1001
1002       for(sz=0;sz<4;sz++)
1003       {
1004          const int tsz[4]={0,72,512,1275};
1005          for(jj=sz;jj<65025;jj+=11)
1006          {
1007             int pos;
1008             for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
1009             packet[2+pos]=jj%254;
1010             pos++;
1011             if(sz==0&&i==63)
1012             {
1013                /*Code more padding than there is room in the packet*/
1014                UNDEFINE_FOR_PARSE
1015                ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
1016                cfgs++;
1017                if(ret!=OPUS_INVALID_PACKET)test_failed();
1018             }
1019             UNDEFINE_FOR_PARSE
1020             ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
1021             cfgs++;
1022             if(tsz[sz]+i<1276)
1023             {
1024                if(ret!=1)test_failed();
1025                if(size[0]!=tsz[sz]+i)test_failed();
1026                if((toc>>2)!=i)test_failed();
1027             } else if (ret!=OPUS_INVALID_PACKET)test_failed();
1028          }
1029       }
1030    }
1031    fprintf(stdout,"    code 3 padding (%2d cases) ............... OK.\n",cfgs);
1032    cfgs_total+=cfgs;
1033    fprintf(stdout,"    opus_packet_parse ............................ OK.\n");
1034    fprintf(stdout,"                      All packet parsing tests passed\n");
1035    fprintf(stdout,"                          (%d API invocations)\n",cfgs_total);
1036    return cfgs_total;
1037 }
1038
1039 /* This is a helper macro for the encoder tests.
1040    The encoder api tests all have a pattern of set-must-fail, set-must-fail,
1041    set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
1042 #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
1043    i=(badv);\
1044    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
1045    i=(badv2);\
1046    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
1047    j=i=(goodv);\
1048    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
1049    i=-12345;\
1050    VG_UNDEF(&i,sizeof(i)); \
1051    err=opus_encoder_ctl(enc,getcall);\
1052    if(err!=OPUS_OK || i!=j)test_failed();\
1053    j=i=(goodv2);\
1054    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
1055    fprintf(stdout,sok);\
1056    i=-12345;\
1057    VG_UNDEF(&i,sizeof(i)); \
1058    err=opus_encoder_ctl(enc,getcall);\
1059    if(err!=OPUS_OK || i!=j)test_failed();\
1060    fprintf(stdout,gok);\
1061    cfgs+=6;
1062
1063 opus_int32 test_enc_api(void)
1064 {
1065    opus_uint32 enc_final_range;
1066    OpusEncoder *enc;
1067    opus_int32 i,j;
1068    unsigned char packet[1276];
1069 #ifndef DISABLE_FLOAT_API
1070    float fbuf[960*2];
1071 #endif
1072    short sbuf[960*2];
1073    int c,err,cfgs;
1074
1075    cfgs=0;
1076    /*First test invalid configurations which should fail*/
1077    fprintf(stdout,"\n  Encoder basic API tests\n");
1078    fprintf(stdout,"  ---------------------------------------------------\n");
1079    for(c=0;c<4;c++)
1080    {
1081       i=opus_encoder_get_size(c);
1082       if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
1083       fprintf(stdout,"    opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
1084       cfgs++;
1085    }
1086
1087    /*Test with unsupported sample rates, channel counts*/
1088    for(c=0;c<4;c++)
1089    {
1090       for(i=-7;i<=96000;i++)
1091       {
1092          int fs;
1093          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
1094          switch(i)
1095          {
1096            case(-5):fs=-8000;break;
1097            case(-6):fs=INT32_MAX;break;
1098            case(-7):fs=INT32_MIN;break;
1099            default:fs=i;
1100          }
1101          err = OPUS_OK;
1102          VG_UNDEF(&err,sizeof(err));
1103          enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
1104          if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
1105          cfgs++;
1106          enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, 0);
1107          if(enc!=NULL)test_failed();
1108          cfgs++;
1109          opus_encoder_destroy(enc);
1110          enc=malloc(opus_encoder_get_size(2));
1111          if(enc==NULL)test_failed();
1112          err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
1113          if(err!=OPUS_BAD_ARG)test_failed();
1114          cfgs++;
1115          free(enc);
1116       }
1117    }
1118
1119    enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
1120    if(enc!=NULL)test_failed();
1121    cfgs++;
1122
1123    VG_UNDEF(&err,sizeof(err));
1124    enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
1125    if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
1126    cfgs++;
1127
1128    VG_UNDEF(&err,sizeof(err));
1129    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
1130    if(enc==NULL)test_failed();
1131    opus_encoder_destroy(enc);
1132    cfgs++;
1133
1134    VG_UNDEF(&err,sizeof(err));
1135    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
1136    if(err!=OPUS_OK || enc==NULL)test_failed();
1137    cfgs++;
1138    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1139    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1140    cfgs++;
1141    opus_encoder_destroy(enc);
1142
1143    VG_UNDEF(&err,sizeof(err));
1144    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
1145    if(err!=OPUS_OK || enc==NULL)test_failed();
1146    cfgs++;
1147    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1148    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1149    opus_encoder_destroy(enc);
1150    cfgs++;
1151
1152    VG_UNDEF(&err,sizeof(err));
1153    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
1154    if(err!=OPUS_OK || enc==NULL)test_failed();
1155    cfgs++;
1156
1157    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
1158    fprintf(stdout,"    opus_encoder_init() .......................... OK.\n");
1159
1160    i=-12345;
1161    VG_UNDEF(&i,sizeof(i));
1162    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
1163    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
1164    cfgs++;
1165    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(null_int_ptr));
1166    if(err!=OPUS_BAD_ARG)test_failed();
1167    cfgs++;
1168    fprintf(stdout,"    OPUS_GET_LOOKAHEAD ........................... OK.\n");
1169
1170    err=opus_encoder_ctl(enc,OPUS_GET_SAMPLE_RATE(&i));
1171    if(err!=OPUS_OK || i!=48000)test_failed();
1172    cfgs++;
1173    err=opus_encoder_ctl(enc,OPUS_GET_SAMPLE_RATE(null_int_ptr));
1174    if(err!=OPUS_BAD_ARG)test_failed();
1175    cfgs++;
1176    fprintf(stdout,"    OPUS_GET_SAMPLE_RATE ......................... OK.\n");
1177
1178    if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
1179    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
1180    cfgs++;
1181
1182    err=opus_encoder_ctl(enc,OPUS_GET_APPLICATION(null_int_ptr));
1183    if(err!=OPUS_BAD_ARG)test_failed();
1184    cfgs++;
1185    CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
1186      OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
1187      "    OPUS_SET_APPLICATION ......................... OK.\n",
1188      "    OPUS_GET_APPLICATION ......................... OK.\n")
1189
1190    err=opus_encoder_ctl(enc,OPUS_GET_BITRATE(null_int_ptr));
1191    if(err!=OPUS_BAD_ARG)test_failed();
1192    cfgs++;
1193    if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
1194    cfgs++;
1195    VG_UNDEF(&i,sizeof(i));
1196    if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
1197    if(i>700000||i<256000)test_failed();
1198    cfgs++;
1199    CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
1200      500,256000,
1201      "    OPUS_SET_BITRATE ............................. OK.\n",
1202      "    OPUS_GET_BITRATE ............................. OK.\n")
1203
1204    err=opus_encoder_ctl(enc,OPUS_GET_FORCE_CHANNELS(null_int_ptr));
1205    if(err!=OPUS_BAD_ARG)test_failed();
1206    cfgs++;
1207    CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
1208      1,OPUS_AUTO,
1209      "    OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
1210      "    OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
1211
1212    i=-2;
1213    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
1214    cfgs++;
1215    i=OPUS_BANDWIDTH_FULLBAND+1;
1216    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
1217    cfgs++;
1218    i=OPUS_BANDWIDTH_NARROWBAND;
1219    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1220    cfgs++;
1221    i=OPUS_BANDWIDTH_FULLBAND;
1222    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1223    cfgs++;
1224    i=OPUS_BANDWIDTH_WIDEBAND;
1225    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1226    cfgs++;
1227    i=OPUS_BANDWIDTH_MEDIUMBAND;
1228    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1229    cfgs++;
1230    fprintf(stdout,"    OPUS_SET_BANDWIDTH ........................... OK.\n");
1231    /*We don't test if the bandwidth has actually changed.
1232      because the change may be delayed until the encoder is advanced.*/
1233    i=-12345;
1234    VG_UNDEF(&i,sizeof(i));
1235    err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
1236    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1237       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1238       i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
1239    cfgs++;
1240    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
1241    cfgs++;
1242    err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(null_int_ptr));
1243    if(err!=OPUS_BAD_ARG)test_failed();
1244    cfgs++;
1245    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
1246
1247    i=-2;
1248    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
1249    cfgs++;
1250    i=OPUS_BANDWIDTH_FULLBAND+1;
1251    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
1252    cfgs++;
1253    i=OPUS_BANDWIDTH_NARROWBAND;
1254    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1255    cfgs++;
1256    i=OPUS_BANDWIDTH_FULLBAND;
1257    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1258    cfgs++;
1259    i=OPUS_BANDWIDTH_WIDEBAND;
1260    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1261    cfgs++;
1262    i=OPUS_BANDWIDTH_MEDIUMBAND;
1263    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1264    cfgs++;
1265    fprintf(stdout,"    OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
1266    /*We don't test if the bandwidth has actually changed.
1267      because the change may be delayed until the encoder is advanced.*/
1268    i=-12345;
1269    VG_UNDEF(&i,sizeof(i));
1270    err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
1271    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1272       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1273       i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
1274    cfgs++;
1275    err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(null_int_ptr));
1276    if(err!=OPUS_BAD_ARG)test_failed();
1277    cfgs++;
1278    fprintf(stdout,"    OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
1279
1280    err=opus_encoder_ctl(enc,OPUS_GET_DTX(null_int_ptr));
1281    if(err!=OPUS_BAD_ARG)test_failed();
1282    cfgs++;
1283    CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
1284      1,0,
1285      "    OPUS_SET_DTX ................................. OK.\n",
1286      "    OPUS_GET_DTX ................................. OK.\n")
1287
1288    err=opus_encoder_ctl(enc,OPUS_GET_COMPLEXITY(null_int_ptr));
1289    if(err!=OPUS_BAD_ARG)test_failed();
1290    cfgs++;
1291    CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
1292      0,10,
1293      "    OPUS_SET_COMPLEXITY .......................... OK.\n",
1294      "    OPUS_GET_COMPLEXITY .......................... OK.\n")
1295
1296    err=opus_encoder_ctl(enc,OPUS_GET_INBAND_FEC(null_int_ptr));
1297    if(err!=OPUS_BAD_ARG)test_failed();
1298    cfgs++;
1299    CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
1300      1,0,
1301      "    OPUS_SET_INBAND_FEC .......................... OK.\n",
1302      "    OPUS_GET_INBAND_FEC .......................... OK.\n")
1303
1304    err=opus_encoder_ctl(enc,OPUS_GET_PACKET_LOSS_PERC(null_int_ptr));
1305    if(err!=OPUS_BAD_ARG)test_failed();
1306    cfgs++;
1307    CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
1308      100,0,
1309      "    OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
1310      "    OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
1311
1312    err=opus_encoder_ctl(enc,OPUS_GET_VBR(null_int_ptr));
1313    if(err!=OPUS_BAD_ARG)test_failed();
1314    cfgs++;
1315    CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
1316      1,0,
1317      "    OPUS_SET_VBR ................................. OK.\n",
1318      "    OPUS_GET_VBR ................................. OK.\n")
1319
1320 /*   err=opus_encoder_ctl(enc,OPUS_GET_VOICE_RATIO(null_int_ptr));
1321    if(err!=OPUS_BAD_ARG)test_failed();
1322    cfgs++;
1323    CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
1324      0,50,
1325      "    OPUS_SET_VOICE_RATIO ......................... OK.\n",
1326      "    OPUS_GET_VOICE_RATIO ......................... OK.\n")*/
1327
1328    err=opus_encoder_ctl(enc,OPUS_GET_VBR_CONSTRAINT(null_int_ptr));
1329    if(err!=OPUS_BAD_ARG)test_failed();
1330    cfgs++;
1331    CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
1332      1,0,
1333      "    OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
1334      "    OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
1335
1336    err=opus_encoder_ctl(enc,OPUS_GET_SIGNAL(null_int_ptr));
1337    if(err!=OPUS_BAD_ARG)test_failed();
1338    cfgs++;
1339    CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
1340      OPUS_SIGNAL_MUSIC,OPUS_AUTO,
1341      "    OPUS_SET_SIGNAL .............................. OK.\n",
1342      "    OPUS_GET_SIGNAL .............................. OK.\n")
1343
1344    err=opus_encoder_ctl(enc,OPUS_GET_LSB_DEPTH(null_int_ptr));
1345    if(err!=OPUS_BAD_ARG)test_failed();
1346    cfgs++;
1347    CHECK_SETGET(OPUS_SET_LSB_DEPTH(i),OPUS_GET_LSB_DEPTH(&i),7,25,16,24,
1348      "    OPUS_SET_LSB_DEPTH ........................... OK.\n",
1349      "    OPUS_GET_LSB_DEPTH ........................... OK.\n")
1350
1351    err=opus_encoder_ctl(enc,OPUS_GET_PREDICTION_DISABLED(&i));
1352    if(i!=0)test_failed();
1353    cfgs++;
1354    err=opus_encoder_ctl(enc,OPUS_GET_PREDICTION_DISABLED(null_int_ptr));
1355    if(err!=OPUS_BAD_ARG)test_failed();
1356    cfgs++;
1357    CHECK_SETGET(OPUS_SET_PREDICTION_DISABLED(i),OPUS_GET_PREDICTION_DISABLED(&i),-1,2,1,0,
1358      "    OPUS_SET_PREDICTION_DISABLED ................. OK.\n",
1359      "    OPUS_GET_PREDICTION_DISABLED ................. OK.\n")
1360
1361    err=opus_encoder_ctl(enc,OPUS_GET_EXPERT_FRAME_DURATION(null_int_ptr));
1362    if(err!=OPUS_BAD_ARG)test_failed();
1363    cfgs++;
1364    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_2_5_MS));
1365    if(err!=OPUS_OK)test_failed();
1366    cfgs++;
1367    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_5_MS));
1368    if(err!=OPUS_OK)test_failed();
1369    cfgs++;
1370    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_10_MS));
1371    if(err!=OPUS_OK)test_failed();
1372    cfgs++;
1373    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_20_MS));
1374    if(err!=OPUS_OK)test_failed();
1375    cfgs++;
1376    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_40_MS));
1377    if(err!=OPUS_OK)test_failed();
1378    cfgs++;
1379    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_60_MS));
1380    if(err!=OPUS_OK)test_failed();
1381    cfgs++;
1382    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_80_MS));
1383    if(err!=OPUS_OK)test_failed();
1384    cfgs++;
1385    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_100_MS));
1386    if(err!=OPUS_OK)test_failed();
1387    cfgs++;
1388    err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_120_MS));
1389    if(err!=OPUS_OK)test_failed();
1390    cfgs++;
1391    CHECK_SETGET(OPUS_SET_EXPERT_FRAME_DURATION(i),OPUS_GET_EXPERT_FRAME_DURATION(&i),0,-1,
1392          OPUS_FRAMESIZE_60_MS,OPUS_FRAMESIZE_ARG,
1393      "    OPUS_SET_EXPERT_FRAME_DURATION ............... OK.\n",
1394      "    OPUS_GET_EXPERT_FRAME_DURATION ............... OK.\n")
1395
1396    /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
1397
1398    err=opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(null_uint_ptr));
1399    if(err!=OPUS_BAD_ARG)test_failed();
1400    cfgs++;
1401    if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
1402    cfgs++;
1403    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
1404
1405    /*Reset the encoder*/
1406    if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
1407    cfgs++;
1408    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
1409
1410    memset(sbuf,0,sizeof(short)*2*960);
1411    VG_UNDEF(packet,sizeof(packet));
1412    i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
1413    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1414    VG_CHECK(packet,i);
1415    cfgs++;
1416    fprintf(stdout,"    opus_encode() ................................ OK.\n");
1417 #ifndef DISABLE_FLOAT_API
1418    memset(fbuf,0,sizeof(float)*2*960);
1419    VG_UNDEF(packet,sizeof(packet));
1420    i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
1421    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1422    VG_CHECK(packet,i);
1423    cfgs++;
1424    fprintf(stdout,"    opus_encode_float() .......................... OK.\n");
1425 #endif
1426
1427 #if 0
1428    /*These tests are disabled because the library crashes with null states*/
1429    if(opus_encoder_ctl(0,OPUS_RESET_STATE)               !=OPUS_INVALID_STATE)test_failed();
1430    if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
1431    if(opus_encode(0,sbuf,960,packet,sizeof(packet))      !=OPUS_INVALID_STATE)test_failed();
1432    if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
1433 #endif
1434    opus_encoder_destroy(enc);
1435    cfgs++;
1436    fprintf(stdout,"                   All encoder interface tests passed\n");
1437    fprintf(stdout,"                             (%d API invocations)\n",cfgs);
1438    return cfgs;
1439 }
1440
1441 #define max_out (1276*48+48*2+2)
1442 int test_repacketizer_api(void)
1443 {
1444    int ret,cfgs,i,j,k;
1445    OpusRepacketizer *rp;
1446    unsigned char *packet;
1447    unsigned char *po;
1448    cfgs=0;
1449    fprintf(stdout,"\n  Repacketizer tests\n");
1450    fprintf(stdout,"  ---------------------------------------------------\n");
1451
1452    packet=malloc(max_out);
1453    if(packet==NULL)test_failed();
1454    memset(packet,0,max_out);
1455    po=malloc(max_out+256);
1456    if(po==NULL)test_failed();
1457
1458    i=opus_repacketizer_get_size();
1459    if(i<=0)test_failed();
1460    cfgs++;
1461    fprintf(stdout,"    opus_repacketizer_get_size()=%d ............. OK.\n",i);
1462
1463    rp=malloc(i);
1464    rp=opus_repacketizer_init(rp);
1465    if(rp==NULL)test_failed();
1466    cfgs++;
1467    free(rp);
1468    fprintf(stdout,"    opus_repacketizer_init ....................... OK.\n");
1469
1470    rp=opus_repacketizer_create();
1471    if(rp==NULL)test_failed();
1472    cfgs++;
1473    fprintf(stdout,"    opus_repacketizer_create ..................... OK.\n");
1474
1475    if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
1476    cfgs++;
1477    fprintf(stdout,"    opus_repacketizer_get_nb_frames .............. OK.\n");
1478
1479    /*Length overflows*/
1480    VG_UNDEF(packet,4);
1481    if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
1482    cfgs++;
1483    packet[0]=1;
1484    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
1485    cfgs++;
1486    packet[0]=2;
1487    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
1488    cfgs++;
1489    packet[0]=3;
1490    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
1491    cfgs++;
1492    packet[0]=2;
1493    packet[1]=255;
1494    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
1495    cfgs++;
1496    packet[0]=2;
1497    packet[1]=250;
1498    if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
1499    cfgs++;
1500    packet[0]=3;
1501    packet[1]=0;
1502    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
1503    cfgs++;
1504    packet[1]=49;
1505    if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
1506    cfgs++;
1507    packet[0]=0;
1508    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1509    cfgs++;
1510    packet[0]=1<<2;
1511    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
1512    cfgs++;
1513
1514    /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
1515    opus_repacketizer_init(rp);
1516    for(j=0;j<32;j++)
1517    {
1518       /* TOC types, test half with stereo */
1519       int maxi;
1520       packet[0]=((j<<1)+(j&1))<<2;
1521       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1522       for(i=1;i<=maxi;i++)
1523       {
1524          /* Number of CBR frames in the input packets */
1525          int maxp;
1526          packet[0]=((j<<1)+(j&1))<<2;
1527          if(i>1)packet[0]+=i==2?1:3;
1528          packet[1]=i>2?i:0;
1529          maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
1530          for(k=0;k<=(1275+75);k+=3)
1531          {
1532             /*Payload size*/
1533             opus_int32 cnt,rcnt;
1534             if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
1535             for(cnt=0;cnt<maxp+2;cnt++)
1536             {
1537                if(cnt>0)
1538                {
1539                   ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
1540                   if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
1541                   cfgs++;
1542                }
1543                rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
1544                if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
1545                cfgs++;
1546                ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
1547                if(rcnt>0)
1548                {
1549                   int len;
1550                   len=k*rcnt+((rcnt*i)>2?2:1);
1551                   if(ret!=len)test_failed();
1552                   if((rcnt*i)<2&&(po[0]&3)!=0)test_failed();                      /* Code 0 */
1553                   if((rcnt*i)==2&&(po[0]&3)!=1)test_failed();                     /* Code 1 */
1554                   if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
1555                   cfgs++;
1556                   if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1557                   cfgs++;
1558                   if(opus_packet_unpad(po,len)!=len)test_failed();
1559                   cfgs++;
1560                   if(opus_packet_pad(po,len,len+1)!=OPUS_OK)test_failed();
1561                   cfgs++;
1562                   if(opus_packet_pad(po,len+1,len+256)!=OPUS_OK)test_failed();
1563                   cfgs++;
1564                   if(opus_packet_unpad(po,len+256)!=len)test_failed();
1565                   cfgs++;
1566                   if(opus_multistream_packet_unpad(po,len,1)!=len)test_failed();
1567                   cfgs++;
1568                   if(opus_multistream_packet_pad(po,len,len+1,1)!=OPUS_OK)test_failed();
1569                   cfgs++;
1570                   if(opus_multistream_packet_pad(po,len+1,len+256,1)!=OPUS_OK)test_failed();
1571                   cfgs++;
1572                   if(opus_multistream_packet_unpad(po,len+256,1)!=len)test_failed();
1573                   cfgs++;
1574                   if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1575                   cfgs++;
1576                   if(len>1)
1577                   {
1578                      if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1579                      cfgs++;
1580                   }
1581                   if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1582                   cfgs++;
1583                } else if (ret!=OPUS_BAD_ARG)test_failed();                        /* M must not be 0 */
1584             }
1585             opus_repacketizer_init(rp);
1586          }
1587       }
1588    }
1589
1590    /*Change in input count code, CBR out*/
1591    opus_repacketizer_init(rp);
1592    packet[0]=0;
1593    if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
1594    cfgs++;
1595    packet[0]+=1;
1596    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1597    cfgs++;
1598    i=opus_repacketizer_out(rp,po,max_out);
1599    if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
1600    cfgs++;
1601    i=opus_repacketizer_out_range(rp,0,1,po,max_out);
1602    if(i!=5||(po[0]&3)!=0)test_failed();
1603    cfgs++;
1604    i=opus_repacketizer_out_range(rp,1,2,po,max_out);
1605    if(i!=5||(po[0]&3)!=0)test_failed();
1606    cfgs++;
1607
1608    /*Change in input count code, VBR out*/
1609    opus_repacketizer_init(rp);
1610    packet[0]=1;
1611    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1612    cfgs++;
1613    packet[0]=0;
1614    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1615    cfgs++;
1616    i=opus_repacketizer_out(rp,po,max_out);
1617    if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
1618    cfgs++;
1619
1620    /*VBR in, VBR out*/
1621    opus_repacketizer_init(rp);
1622    packet[0]=2;
1623    packet[1]=4;
1624    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1625    cfgs++;
1626    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1627    cfgs++;
1628    i=opus_repacketizer_out(rp,po,max_out);
1629    if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
1630    cfgs++;
1631
1632    /*VBR in, CBR out*/
1633    opus_repacketizer_init(rp);
1634    packet[0]=2;
1635    packet[1]=4;
1636    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1637    cfgs++;
1638    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1639    cfgs++;
1640    i=opus_repacketizer_out(rp,po,max_out);
1641    if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
1642    cfgs++;
1643
1644    /*Count 0 in, VBR out*/
1645    for(j=0;j<32;j++)
1646    {
1647       /* TOC types, test half with stereo */
1648       int maxi,sum,rcnt;
1649       packet[0]=((j<<1)+(j&1))<<2;
1650       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1651       sum=0;
1652       rcnt=0;
1653       opus_repacketizer_init(rp);
1654       for(i=1;i<=maxi+2;i++)
1655       {
1656          int len;
1657          ret=opus_repacketizer_cat(rp,packet,i);
1658          if(rcnt<maxi)
1659          {
1660             if(ret!=OPUS_OK)test_failed();
1661             rcnt++;
1662             sum+=i-1;
1663          } else if (ret!=OPUS_INVALID_PACKET)test_failed();
1664          cfgs++;
1665          len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
1666          if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
1667          if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
1668          if(rcnt==2&&(po[0]&3)!=2)test_failed();
1669          if(rcnt==1&&(po[0]&3)!=0)test_failed();
1670          cfgs++;
1671          if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1672          cfgs++;
1673          if(opus_packet_unpad(po,len)!=len)test_failed();
1674          cfgs++;
1675          if(opus_packet_pad(po,len,len+1)!=OPUS_OK)test_failed();
1676          cfgs++;
1677          if(opus_packet_pad(po,len+1,len+256)!=OPUS_OK)test_failed();
1678          cfgs++;
1679          if(opus_packet_unpad(po,len+256)!=len)test_failed();
1680          cfgs++;
1681          if(opus_multistream_packet_unpad(po,len,1)!=len)test_failed();
1682          cfgs++;
1683          if(opus_multistream_packet_pad(po,len,len+1,1)!=OPUS_OK)test_failed();
1684          cfgs++;
1685          if(opus_multistream_packet_pad(po,len+1,len+256,1)!=OPUS_OK)test_failed();
1686          cfgs++;
1687          if(opus_multistream_packet_unpad(po,len+256,1)!=len)test_failed();
1688          cfgs++;
1689          if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1690          cfgs++;
1691          if(len>1)
1692          {
1693             if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1694             cfgs++;
1695          }
1696          if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1697          cfgs++;
1698       }
1699    }
1700
1701    po[0]='O';
1702    po[1]='p';
1703    if(opus_packet_pad(po,4,4)!=OPUS_OK)test_failed();
1704    cfgs++;
1705    if(opus_multistream_packet_pad(po,4,4,1)!=OPUS_OK)test_failed();
1706    cfgs++;
1707    if(opus_packet_pad(po,4,5)!=OPUS_INVALID_PACKET)test_failed();
1708    cfgs++;
1709    if(opus_multistream_packet_pad(po,4,5,1)!=OPUS_INVALID_PACKET)test_failed();
1710    cfgs++;
1711    if(opus_packet_pad(po,0,5)!=OPUS_BAD_ARG)test_failed();
1712    cfgs++;
1713    if(opus_multistream_packet_pad(po,0,5,1)!=OPUS_BAD_ARG)test_failed();
1714    cfgs++;
1715    if(opus_packet_unpad(po,0)!=OPUS_BAD_ARG)test_failed();
1716    cfgs++;
1717    if(opus_multistream_packet_unpad(po,0,1)!=OPUS_BAD_ARG)test_failed();
1718    cfgs++;
1719    if(opus_packet_unpad(po,4)!=OPUS_INVALID_PACKET)test_failed();
1720    cfgs++;
1721    if(opus_multistream_packet_unpad(po,4,1)!=OPUS_INVALID_PACKET)test_failed();
1722    cfgs++;
1723    po[0]=0;
1724    po[1]=0;
1725    po[2]=0;
1726    if(opus_packet_pad(po,5,4)!=OPUS_BAD_ARG)test_failed();
1727    cfgs++;
1728    if(opus_multistream_packet_pad(po,5,4,1)!=OPUS_BAD_ARG)test_failed();
1729    cfgs++;
1730
1731    fprintf(stdout,"    opus_repacketizer_cat ........................ OK.\n");
1732    fprintf(stdout,"    opus_repacketizer_out ........................ OK.\n");
1733    fprintf(stdout,"    opus_repacketizer_out_range .................. OK.\n");
1734    fprintf(stdout,"    opus_packet_pad .............................. OK.\n");
1735    fprintf(stdout,"    opus_packet_unpad ............................ OK.\n");
1736    fprintf(stdout,"    opus_multistream_packet_pad .................. OK.\n");
1737    fprintf(stdout,"    opus_multistream_packet_unpad ................ OK.\n");
1738
1739    opus_repacketizer_destroy(rp);
1740    cfgs++;
1741    free(packet);
1742    free(po);
1743    fprintf(stdout,"                        All repacketizer tests passed\n");
1744    fprintf(stdout,"                            (%7d API invocations)\n",cfgs);
1745
1746    return cfgs;
1747 }
1748
1749 #ifdef MALLOC_FAIL
1750 /* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
1751  * under GCC. However, this is the cleanest way to test malloc failure
1752  * handling in our codebase, and the lack of thread safety isn't an
1753  * issue here. We therefore disable the warning for this function.
1754  */
1755 #if OPUS_GNUC_PREREQ(4,6)
1756 /* Save the current warning settings */
1757 #pragma GCC diagnostic push
1758 #endif
1759 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1760
1761 typedef void *(*mhook)(size_t __size, __const void *);
1762 #endif
1763
1764 int test_malloc_fail(void)
1765 {
1766 #ifdef MALLOC_FAIL
1767    OpusDecoder *dec;
1768    OpusEncoder *enc;
1769    OpusRepacketizer *rp;
1770    unsigned char mapping[256] = {0,1};
1771    OpusMSDecoder *msdec;
1772    OpusMSEncoder *msenc;
1773    int rate,c,app,cfgs,err,useerr;
1774    int *ep;
1775    mhook orig_malloc;
1776    cfgs=0;
1777 #endif
1778    fprintf(stdout,"\n  malloc() failure tests\n");
1779    fprintf(stdout,"  ---------------------------------------------------\n");
1780 #ifdef MALLOC_FAIL
1781    orig_malloc=__malloc_hook;
1782    __malloc_hook=malloc_hook;
1783    ep=(int *)opus_alloc(sizeof(int));
1784    if(ep!=NULL)
1785    {
1786       if(ep)free(ep);
1787       __malloc_hook=orig_malloc;
1788 #endif
1789       fprintf(stdout,"    opus_decoder_create() ................... SKIPPED.\n");
1790       fprintf(stdout,"    opus_encoder_create() ................... SKIPPED.\n");
1791       fprintf(stdout,"    opus_repacketizer_create() .............. SKIPPED.\n");
1792       fprintf(stdout,"    opus_multistream_decoder_create() ....... SKIPPED.\n");
1793       fprintf(stdout,"    opus_multistream_encoder_create() ....... SKIPPED.\n");
1794       fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
1795       return 0;
1796 #ifdef MALLOC_FAIL
1797    }
1798    for(useerr=0;useerr<2;useerr++)
1799    {
1800       ep=useerr?&err:0;
1801       for(rate=0;rate<5;rate++)
1802       {
1803         for(c=1;c<3;c++)
1804         {
1805            err=1;
1806            if(useerr)
1807            {
1808               VG_UNDEF(&err,sizeof(err));
1809            }
1810            dec=opus_decoder_create(opus_rates[rate], c, ep);
1811            if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1812            {
1813               __malloc_hook=orig_malloc;
1814               test_failed();
1815            }
1816            cfgs++;
1817            msdec=opus_multistream_decoder_create(opus_rates[rate], c, 1, c-1, mapping, ep);
1818            if(msdec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1819            {
1820               __malloc_hook=orig_malloc;
1821               test_failed();
1822            }
1823            cfgs++;
1824            for(app=0;app<3;app++)
1825            {
1826               if(useerr)
1827               {
1828                  VG_UNDEF(&err,sizeof(err));
1829               }
1830               enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
1831               if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1832               {
1833                  __malloc_hook=orig_malloc;
1834                  test_failed();
1835               }
1836               cfgs++;
1837               msenc=opus_multistream_encoder_create(opus_rates[rate], c, 1, c-1, mapping, opus_apps[app],ep);
1838               if(msenc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1839               {
1840                  __malloc_hook=orig_malloc;
1841                  test_failed();
1842               }
1843               cfgs++;
1844            }
1845         }
1846      }
1847    }
1848    rp=opus_repacketizer_create();
1849    if(rp!=NULL)
1850    {
1851       __malloc_hook=orig_malloc;
1852       test_failed();
1853    }
1854    cfgs++;
1855    __malloc_hook=orig_malloc;
1856    fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
1857    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
1858    fprintf(stdout,"    opus_repacketizer_create() ................... OK.\n");
1859    fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
1860    fprintf(stdout,"    opus_multistream_encoder_create() ............ OK.\n");
1861    fprintf(stdout,"                      All malloc failure tests passed\n");
1862    fprintf(stdout,"                                 (%2d API invocations)\n",cfgs);
1863    return cfgs;
1864 #endif
1865 }
1866
1867 #ifdef MALLOC_FAIL
1868 #if __GNUC_PREREQ(4,6)
1869 #pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
1870 #endif
1871 #endif
1872
1873 int main(int _argc, char **_argv)
1874 {
1875    opus_int32 total;
1876    const char * oversion;
1877    if(_argc>1)
1878    {
1879       fprintf(stderr,"Usage: %s\n",_argv[0]);
1880       return 1;
1881    }
1882    iseed=0;
1883
1884    oversion=opus_get_version_string();
1885    if(!oversion)test_failed();
1886    fprintf(stderr,"Testing the %s API deterministically\n", oversion);
1887    if(opus_strerror(-32768)==NULL)test_failed();
1888    if(opus_strerror(32767)==NULL)test_failed();
1889    if(strlen(opus_strerror(0))<1)test_failed();
1890    total=4;
1891
1892    total+=test_dec_api();
1893    total+=test_msdec_api();
1894    total+=test_parse();
1895    total+=test_enc_api();
1896    total+=test_repacketizer_api();
1897    total+=test_malloc_fail();
1898
1899    fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
1900
1901    return 0;
1902 }