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