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