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