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