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