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