Fix a number of multistream decoder bugs; add some very basic multistream decoder...
[opus.git] / tests / test_opus_api.c
1 /* Copyright (c) 2011 Xiph.Org Foundation
2    Written by Gregory Maxwell */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
19    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    int *nullptr;
96    nullptr=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=malloc(opus_decoder_get_size(2));
130          if(dec==NULL)test_failed();
131          err = opus_decoder_init(dec,fs,c);
132          if(err!=OPUS_BAD_ARG)test_failed();
133          cfgs++;
134          free(dec);
135       }
136    }
137
138    VG_UNDEF(&err,sizeof(err));
139    dec = opus_decoder_create(48000, 2, &err);
140    if(err!=OPUS_OK || dec==NULL)test_failed();
141    VG_CHECK(dec,opus_decoder_get_size(2));
142    cfgs++;
143
144    fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
145    fprintf(stdout,"    opus_decoder_init() .......................... OK.\n");
146
147    VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
148    err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
149    if(err!=OPUS_OK)test_failed();
150    VG_CHECK(&dec_final_range,sizeof(dec_final_range));
151    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
152    cfgs++;
153
154    err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
155    if(err!=OPUS_UNIMPLEMENTED)test_failed();
156    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
157    cfgs++;
158
159    VG_UNDEF(&i,sizeof(i));
160    err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
161    if(err != OPUS_OK || i!=0)test_failed();
162    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
163    cfgs++;
164
165    /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
166    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(nullptr));
167    if(err!=OPUS_BAD_ARG)test_failed();
168    cfgs++;
169    VG_UNDEF(&i,sizeof(i));
170    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
171    if(err != OPUS_OK || i>0 || i<-1)test_failed();
172    cfgs++;
173    VG_UNDEF(packet,sizeof(packet));
174    packet[0]=63<<2;packet[1]=packet[2]=0;
175    if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
176    cfgs++;
177    VG_UNDEF(&i,sizeof(i));
178    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
179    if(err != OPUS_OK || i>0 || i<-1)test_failed();
180    cfgs++;
181    packet[0]=1;
182    if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)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    fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
189
190    /*Reset the decoder*/
191    dec2=malloc(opus_decoder_get_size(2));
192    memcpy(dec2,dec,opus_decoder_get_size(2));
193    if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
194    if(memcmp(dec2,dec,opus_decoder_get_size(2))==0)test_failed();
195    free(dec2);
196    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
197    cfgs++;
198
199    VG_UNDEF(packet,sizeof(packet));
200    packet[0]=0;
201    if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
202    cfgs++;
203    packet[0]=(63<<2)|3;
204    packet[1]=63;
205    if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed();
206    fprintf(stdout,"    opus_decoder_get_nb_samples() ................ OK.\n");
207    cfgs++;
208
209    if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();
210    for(i=0;i<256;i++) {
211      int l1res[4]={1,2,2,OPUS_INVALID_PACKET};
212      packet[0]=i;
213      if(l1res[packet[0]&3]!=opus_packet_get_nb_frames(packet,1))test_failed();
214      cfgs++;
215      for(j=0;j<256;j++) {
216        packet[1]=j;
217        if(((packet[0]&3)!=3?l1res[packet[0]&3]:packet[1]&63)!=opus_packet_get_nb_frames(packet,2))test_failed();
218        cfgs++;
219      }
220    }
221    fprintf(stdout,"    opus_packet_get_nb_frames() .................. OK.\n");
222
223    for(i=0;i<256;i++) {
224      int bw;
225      packet[0]=i;
226      bw=packet[0]>>4;
227      bw=OPUS_BANDWIDTH_NARROWBAND+(((((bw&7)*9)&(63-(bw&8)))+2+12*((bw&8)!=0))>>4);
228      if(bw!=opus_packet_get_bandwidth(packet))test_failed();
229      cfgs++;
230    }
231    fprintf(stdout,"    opus_packet_get_bandwidth() .................. OK.\n");
232
233    for(i=0;i<256;i++) {
234      int fp3s,rate;
235      packet[0]=i;
236      fp3s=packet[0]>>3;
237      fp3s=((((3-(fp3s&3))*13&119)+9)>>2)*((fp3s>13)*(3-((fp3s&3)==3))+1)*25;
238      for(rate=0;rate<5;rate++) {
239        if((opus_rates[rate]*3/fp3s)!=opus_packet_get_samples_per_frame(packet,opus_rates[rate]))test_failed();
240        cfgs++;
241      }
242    }
243    fprintf(stdout,"    opus_packet_get_samples_per_frame() .......... OK.\n");
244
245    packet[0]=(63<<2)+3;
246    packet[1]=49;
247    for(j=2;j<51;j++)packet[j]=0;
248    VG_UNDEF(sbuf,sizeof(sbuf));
249    if(opus_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
250    cfgs++;
251    packet[0]=(63<<2);
252    packet[1]=packet[2]=0;
253    if(opus_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG)test_failed();
254    cfgs++;
255    if(opus_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
256    cfgs++;
257    if(opus_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
258    cfgs++;
259    if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
260    cfgs++;
261    fprintf(stdout,"    opus_decode() ................................ OK.\n");
262 #ifndef DISABLE_FLOAT_API
263    VG_UNDEF(fbuf,sizeof(fbuf));
264    if(opus_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
265    cfgs++;
266    fprintf(stdout,"    opus_decode_float() .......................... OK.\n");
267 #endif
268
269 #if 0
270    /*These tests are disabled because the library crashes with null states*/
271    if(opus_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
272    if(opus_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
273    if(opus_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
274    if(opus_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
275    if(opus_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
276    if(opus_packet_get_nb_frames(NULL,1)            !=OPUS_BAD_ARG)test_failed();
277    if(opus_packet_get_bandwidth(NULL)              !=OPUS_BAD_ARG)test_failed();
278    if(opus_packet_get_samples_per_frame(NULL,48000)!=OPUS_BAD_ARG)test_failed();
279 #endif
280    opus_decoder_destroy(dec);
281    cfgs++;
282    fprintf(stdout,"                   All decoder interface tests passed\n");
283    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
284    return cfgs;
285 }
286
287 opus_int32 test_msdec_api(void)
288 {
289    opus_uint32 dec_final_range;
290    OpusMSDecoder *dec;
291    opus_int32 i,j,cfgs;
292    unsigned char packet[1276];
293    unsigned char mapping[256] = {0,1};
294 #ifndef DISABLE_FLOAT_API
295    float fbuf[960*2];
296 #endif
297    short sbuf[960*2];
298    int a,b,c,err;
299    int *nullptr;
300    nullptr=0;
301
302    cfgs=0;
303    /*First test invalid configurations which should fail*/
304    fprintf(stdout,"\n  Multistream decoder basic API tests\n");
305    fprintf(stdout,"  ---------------------------------------------------\n");
306    for(a=-1;a<4;a++)
307    {
308       for(b=-1;b<4;b++)
309       {
310          i=opus_multistream_decoder_get_size(a,b);
311          if(((a>0&&b<=a&&b>=0)&&(i<=2048||i>((1<<16)*a)))||((a<1||b>a||b<0)&&i!=0))test_failed();
312          fprintf(stdout,"    opus_multistream_decoder_get_size(%2d,%2d)=%d %sOK.\n",a,b,i,i>0?"":"... ");
313          cfgs++;
314       }
315    }
316
317    /*Test with unsupported sample rates*/
318    for(c=1;c<3;c++)
319    {
320       for(i=-7;i<=96000;i++)
321       {
322          int fs;
323          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
324          switch(i)
325          {
326            case(-5):fs=-8000;break;
327            case(-6):fs=INT32_MAX;break;
328            case(-7):fs=INT32_MIN;break;
329            default:fs=i;
330          }
331          err = OPUS_OK;
332          VG_UNDEF(&err,sizeof(err));
333          dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, &err);
334          if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
335          cfgs++;
336          dec=malloc(opus_multistream_decoder_get_size(1,1));
337          if(dec==NULL)test_failed();
338          err = opus_multistream_decoder_init(dec,fs,c,1,c-1, mapping);
339          if(err!=OPUS_BAD_ARG)test_failed();
340          cfgs++;
341          free(dec);
342       }
343    }
344
345    VG_UNDEF(&err,sizeof(err));
346    dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
347    if(err!=OPUS_OK || dec==NULL)test_failed();
348    VG_CHECK(dec,opus_multistream_decoder_get_size(1,1));
349    cfgs++;
350
351    fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
352    fprintf(stdout,"    opus_multistream_decoder_init() .............. OK.\n");
353
354    VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
355    err=opus_multistream_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
356    if(err!=OPUS_OK)test_failed();
357    VG_CHECK(&dec_final_range,sizeof(dec_final_range));
358    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
359    cfgs++;
360
361    err=opus_multistream_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
362    if(err!=OPUS_UNIMPLEMENTED)test_failed();
363    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
364    cfgs++;
365
366 #if 0
367    /*Currently unimplemented for multistream*/
368    VG_UNDEF(&i,sizeof(i));
369    err=opus_multistream_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
370    if(err != OPUS_OK || i!=0)test_failed();
371    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
372    cfgs++;
373    /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
374    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(nullptr));
375    if(err!=OPUS_BAD_ARG)test_failed();
376    cfgs++;
377    VG_UNDEF(&i,sizeof(i));
378    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
379    if(err != OPUS_OK || i>0 || i<-1)test_failed();
380    cfgs++;
381    VG_UNDEF(packet,sizeof(packet));
382    packet[0]=63<<2;packet[1]=packet[2]=0;
383    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
384    cfgs++;
385    VG_UNDEF(&i,sizeof(i));
386    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
387    if(err != OPUS_OK || i>0 || i<-1)test_failed();
388    cfgs++;
389    packet[0]=1;
390    if(opus_multistream_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
391    cfgs++;
392    VG_UNDEF(&i,sizeof(i));
393    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
394    if(err != OPUS_OK || i>0 || i<-1)test_failed();
395    cfgs++;
396    fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
397 #endif
398
399    /*Reset the decoder*/
400    if(opus_multistream_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
401    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
402    cfgs++;
403
404    packet[0]=(63<<2)+3;
405    packet[1]=49;
406    for(j=2;j<51;j++)packet[j]=0;
407    VG_UNDEF(sbuf,sizeof(sbuf));
408    if(opus_multistream_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
409    cfgs++;
410    packet[0]=(63<<2);
411    packet[1]=packet[2]=0;
412    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();}
413    cfgs++;
414    if(opus_multistream_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
415    cfgs++;
416    if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
417    cfgs++;
418    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
419    cfgs++;
420    fprintf(stdout,"    opus_multistream_decode() .................... OK.\n");
421 #ifndef DISABLE_FLOAT_API
422    VG_UNDEF(fbuf,sizeof(fbuf));
423    if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
424    cfgs++;
425    fprintf(stdout,"    opus_multistream_decode_float() .............. OK.\n");
426 #endif
427
428 #if 0
429    /*These tests are disabled because the library crashes with null states*/
430    if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
431    if(opus_multistream_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
432    if(opus_multistream_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
433    if(opus_multistream_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
434    if(opus_multistream_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
435 #endif
436    opus_multistream_decoder_destroy(dec);
437    cfgs++;
438    fprintf(stdout,"        All multistream decoder interface tests passed\n");
439    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
440    return cfgs;
441 }
442
443 #ifdef VALGRIND
444 #define UNDEFINE_FOR_PARSE  toc=-1; \
445    frames[0]=(unsigned char *)0; \
446    frames[1]=(unsigned char *)0; \
447    payload_offset=-1; \
448    VG_UNDEF(&toc,sizeof(toc)); \
449    VG_UNDEF(frames,sizeof(frames));\
450    VG_UNDEF(&payload_offset,sizeof(payload_offset));
451 #else
452 #define UNDEFINE_FOR_PARSE  toc=-1; \
453    frames[0]=(unsigned char *)0; \
454    frames[1]=(unsigned char *)0; \
455    payload_offset=-1;
456 #endif
457
458 /* This test exercises the heck out of the libopus parser.
459    It is much larger than the parser itself in part because
460    it tries to hit a lot of corner cases that could never
461    fail with the libopus code, but might be problematic for
462    other implementations. */
463 opus_int32 test_parse(void)
464 {
465    opus_int32 i,j,jj,sz;
466    unsigned char packet[1276];
467    opus_int32 cfgs,cfgs_total;
468    unsigned char toc;
469    const unsigned char *frames[48];
470    short size[48];
471    int payload_offset, ret;
472    fprintf(stdout,"\n  Packet header parsing tests\n");
473    fprintf(stdout,"  ---------------------------------------------------\n");
474    memset(packet,0,sizeof(char)*1276);
475    packet[0]=63<<2;
476    if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
477    cfgs_total=cfgs=1;
478    /*code 0*/
479    for(i=0;i<64;i++)
480    {
481       UNDEFINE_FOR_PARSE
482       ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
483       cfgs++;
484       if(ret!=1)test_failed();
485       if(size[0]!=3)test_failed();
486       if(frames[0]!=packet+1)test_failed();
487    }
488    fprintf(stdout,"    code 0 (%2d cases) ............................ OK.\n",cfgs);
489    cfgs_total+=cfgs;cfgs=0;
490
491    /*code 1, two frames of the same size*/
492    for(i=0;i<64;i++)
493    {
494       packet[0]=(i<<2)+1;
495       for(jj=0;jj<=1275*2+3;jj++)
496       {
497          UNDEFINE_FOR_PARSE
498          ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
499          cfgs++;
500          if((jj&1)==1 && jj<=2551)
501          {
502             /* Must pass if payload length even (packet length odd) and
503                size<=2551, must fail otherwise. */
504             if(ret!=2)test_failed();
505             if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
506             if(frames[0]!=packet+1)test_failed();
507             if(frames[1]!=frames[0]+size[0])test_failed();
508             if((toc>>2)!=i)test_failed();
509          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
510       }
511    }
512    fprintf(stdout,"    code 1 (%6d cases) ........................ OK.\n",cfgs);
513    cfgs_total+=cfgs;cfgs=0;
514
515    for(i=0;i<64;i++)
516    {
517       /*code 2, length code overflow*/
518       packet[0]=(i<<2)+2;
519       UNDEFINE_FOR_PARSE
520       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
521       cfgs++;
522       if(ret!=OPUS_INVALID_PACKET)test_failed();
523       packet[1]=252;
524       UNDEFINE_FOR_PARSE
525       ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
526       cfgs++;
527       if(ret!=OPUS_INVALID_PACKET)test_failed();
528       for(j=0;j<1275;j++)
529       {
530          if(j<252)packet[1]=j;
531          else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
532          /*Code 2, one too short*/
533          UNDEFINE_FOR_PARSE
534          ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
535          cfgs++;
536          if(ret!=OPUS_INVALID_PACKET)test_failed();
537          /*Code 2, one too long*/
538          UNDEFINE_FOR_PARSE
539          ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
540          cfgs++;
541          if(ret!=OPUS_INVALID_PACKET)test_failed();
542          /*Code 2, second zero*/
543          UNDEFINE_FOR_PARSE
544          ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
545          cfgs++;
546          if(ret!=2)test_failed();
547          if(size[0]!=j||size[1]!=0)test_failed();
548          if(frames[1]!=frames[0]+size[0])test_failed();
549          if((toc>>2)!=i)test_failed();
550          /*Code 2, normal*/
551          UNDEFINE_FOR_PARSE
552          ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
553          cfgs++;
554          if(ret!=2)test_failed();
555          if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
556          if(frames[1]!=frames[0]+size[0])test_failed();
557          if((toc>>2)!=i)test_failed();
558       }
559    }
560    fprintf(stdout,"    code 2 (%6d cases) ........................ OK.\n",cfgs);
561    cfgs_total+=cfgs;cfgs=0;
562
563    for(i=0;i<64;i++)
564    {
565       packet[0]=(i<<2)+3;
566       /*code 3, length code overflow*/
567       UNDEFINE_FOR_PARSE
568       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
569       cfgs++;
570       if(ret!=OPUS_INVALID_PACKET)test_failed();
571    }
572    fprintf(stdout,"    code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
573    cfgs_total+=cfgs;cfgs=0;
574
575    for(i=0;i<64;i++)
576    {
577       /*code 3, m is zero or 49-63*/
578       packet[0]=(i<<2)+3;
579       for(jj=49;jj<=64;jj++)
580       {
581         packet[1]=0+(jj&63); /*CBR, no padding*/
582         UNDEFINE_FOR_PARSE
583         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
584         cfgs++;
585         if(ret!=OPUS_INVALID_PACKET)test_failed();
586         packet[1]=128+(jj&63); /*VBR, no padding*/
587         UNDEFINE_FOR_PARSE
588         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
589         cfgs++;
590         if(ret!=OPUS_INVALID_PACKET)test_failed();
591         packet[1]=64+(jj&63); /*CBR, padding*/
592         UNDEFINE_FOR_PARSE
593         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
594         cfgs++;
595         if(ret!=OPUS_INVALID_PACKET)test_failed();
596         packet[1]=128+64+(jj&63); /*VBR, padding*/
597         UNDEFINE_FOR_PARSE
598         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
599         cfgs++;
600         if(ret!=OPUS_INVALID_PACKET)test_failed();
601       }
602    }
603    fprintf(stdout,"    code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
604    cfgs_total+=cfgs;cfgs=0;
605
606    for(i=0;i<64;i++)
607    {
608       packet[0]=(i<<2)+3;
609       /*code 3, m is one, cbr*/
610       packet[1]=1;
611       for(j=0;j<1276;j++)
612       {
613         UNDEFINE_FOR_PARSE
614         ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
615         cfgs++;
616         if(ret!=1)test_failed();
617         if(size[0]!=j)test_failed();
618         if((toc>>2)!=i)test_failed();
619       }
620       UNDEFINE_FOR_PARSE
621       ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
622       cfgs++;
623       if(ret!=OPUS_INVALID_PACKET)test_failed();
624    }
625    fprintf(stdout,"    code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
626    cfgs_total+=cfgs;cfgs=0;
627
628    for(i=0;i<64;i++)
629    {
630       int frame_samp;
631       /*code 3, m>1 CBR*/
632       packet[0]=(i<<2)+3;
633       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
634       for(j=2;j<49;j++)
635       {
636          packet[1]=j;
637          for(sz=2;sz<((j+2)*1275);sz++)
638          {
639             UNDEFINE_FOR_PARSE
640             ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
641             cfgs++;
642             /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
643             if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
644             {
645                if(ret!=j)test_failed();
646                for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
647                if((toc>>2)!=i)test_failed();
648             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
649          }
650       }
651       /*Super jumbo packets*/
652       packet[1]=5760/frame_samp;
653       UNDEFINE_FOR_PARSE
654       ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
655       cfgs++;
656       if(ret!=packet[1])test_failed();
657       for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
658    }
659    fprintf(stdout,"    code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
660    cfgs_total+=cfgs;cfgs=0;
661
662    for(i=0;i<64;i++)
663    {
664       int frame_samp;
665       /*Code 3 VBR, m one*/
666       packet[0]=(i<<2)+3;
667       packet[1]=128+1;
668       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
669       for(jj=0;jj<1276;jj++)
670       {
671          UNDEFINE_FOR_PARSE
672          ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
673          cfgs++;
674          if(ret!=1)test_failed();
675          if(size[0]!=jj)test_failed();
676          if((toc>>2)!=i)test_failed();
677       }
678       UNDEFINE_FOR_PARSE
679       ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
680       cfgs++;
681       if(ret!=OPUS_INVALID_PACKET)test_failed();
682       for(j=2;j<49;j++)
683       {
684          packet[1]=128+j;
685          /*Length code overflow*/
686          UNDEFINE_FOR_PARSE
687          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
688          cfgs++;
689          if(ret!=OPUS_INVALID_PACKET)test_failed();
690          packet[2]=252;
691          packet[3]=0;
692          for(jj=4;jj<2+j;jj++)packet[jj]=0;
693          UNDEFINE_FOR_PARSE
694          ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
695          cfgs++;
696          if(ret!=OPUS_INVALID_PACKET)test_failed();
697          /*One byte too short*/
698          for(jj=2;jj<2+j;jj++)packet[jj]=0;
699          UNDEFINE_FOR_PARSE
700          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
701          cfgs++;
702          if(ret!=OPUS_INVALID_PACKET)test_failed();
703          /*One byte too short thanks to length coding*/
704          packet[2]=252;
705          packet[3]=0;
706          for(jj=4;jj<2+j;jj++)packet[jj]=0;
707          UNDEFINE_FOR_PARSE
708          ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
709          cfgs++;
710          if(ret!=OPUS_INVALID_PACKET)test_failed();
711          /*Most expensive way of coding zeros*/
712          for(jj=2;jj<2+j;jj++)packet[jj]=0;
713          UNDEFINE_FOR_PARSE
714          ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
715          cfgs++;
716          if(frame_samp*j<=5760){
717             if(ret!=j)test_failed();
718             for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
719             if((toc>>2)!=i)test_failed();
720          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
721          /*Quasi-CBR use of mode 3*/
722          for(sz=0;sz<8;sz++)
723          {
724             const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
725             int pos=0;
726             int as=(tsz[sz]+i-j-2)/j;
727             for(jj=0;jj<j-1;jj++)
728             {
729               if(as<252){packet[2+pos]=as;pos++;}
730               else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
731             }
732             UNDEFINE_FOR_PARSE
733             ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
734             cfgs++;
735             if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
736                if(ret!=j)test_failed();
737                for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
738                if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
739                if((toc>>2)!=i)test_failed();
740             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
741          }
742       }
743    }
744    fprintf(stdout,"    code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
745    cfgs_total+=cfgs;cfgs=0;
746
747    for(i=0;i<64;i++)
748    {
749       packet[0]=(i<<2)+3;
750       /*Padding*/
751       packet[1]=128+1+64;
752       /*Overflow the length coding*/
753       for(jj=2;jj<127;jj++)packet[jj]=255;
754       UNDEFINE_FOR_PARSE
755       ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
756       cfgs++;
757       if(ret!=OPUS_INVALID_PACKET)test_failed();
758
759       for(sz=0;sz<4;sz++)
760       {
761          const int tsz[4]={0,72,512,1275};
762          for(jj=sz;jj<65025;jj+=11)
763          {
764             int pos;
765             for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
766             packet[2+pos]=jj%254;
767             pos++;
768             if(sz==0&&i==63)
769             {
770                /*Code more padding than there is room in the packet*/
771                UNDEFINE_FOR_PARSE
772                ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
773                cfgs++;
774                if(ret!=OPUS_INVALID_PACKET)test_failed();
775             }
776             UNDEFINE_FOR_PARSE
777             ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
778             cfgs++;
779             if(tsz[sz]+i<1276)
780             {
781                if(ret!=1)test_failed();
782                if(size[0]!=tsz[sz]+i)test_failed();
783                if((toc>>2)!=i)test_failed();
784             } else if (ret!=OPUS_INVALID_PACKET)test_failed();
785          }
786       }
787    }
788    fprintf(stdout,"    code 3 padding (%2d cases) ............... OK.\n",cfgs);
789    cfgs_total+=cfgs;cfgs=0;
790    fprintf(stdout,"    opus_packet_parse ............................ OK.\n");
791    fprintf(stdout,"                      All packet parsing tests passed\n");
792    fprintf(stdout,"                          (%d API invocations)\n",cfgs_total);
793    return cfgs_total;
794 }
795
796 /* This is a helper macro for the encoder tests.
797    The encoder api tests all have a pattern of set-must-fail, set-must-fail,
798    set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
799 #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
800    i=(badv);\
801    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
802    i=(badv2);\
803    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
804    j=i=(goodv);\
805    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
806    i=-12345;\
807    VG_UNDEF(&i,sizeof(i)); \
808    err=opus_encoder_ctl(enc,getcall);\
809    if(err!=OPUS_OK || i!=j)test_failed();\
810    j=i=(goodv2);\
811    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
812    fprintf(stdout,sok);\
813    i=-12345;\
814    VG_UNDEF(&i,sizeof(i)); \
815    err=opus_encoder_ctl(enc,getcall);\
816    if(err!=OPUS_OK || i!=j)test_failed();\
817    fprintf(stdout,gok);\
818    cfgs+=6;
819
820 opus_int32 test_enc_api(void)
821 {
822    opus_uint32 enc_final_range;
823    OpusEncoder *enc;
824    opus_int32 i,j;
825    unsigned char packet[1276];
826 #ifndef DISABLE_FLOAT_API
827    float fbuf[960*2];
828 #endif
829    short sbuf[960*2];
830    int c,err,cfgs;
831
832    cfgs=0;
833    /*First test invalid configurations which should fail*/
834    fprintf(stdout,"\n  Encoder basic API tests\n");
835    fprintf(stdout,"  ---------------------------------------------------\n");
836    for(c=0;c<4;c++)
837    {
838       i=opus_encoder_get_size(c);
839       if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
840       fprintf(stdout,"    opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
841       cfgs++;
842    }
843
844    /*Test with unsupported sample rates, channel counts*/
845    for(c=0;c<4;c++)
846    {
847       for(i=-7;i<=96000;i++)
848       {
849          int fs;
850          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
851          switch(i)
852          {
853            case(-5):fs=-8000;break;
854            case(-6):fs=INT32_MAX;break;
855            case(-7):fs=INT32_MIN;break;
856            default:fs=i;
857          }
858          err = OPUS_OK;
859          VG_UNDEF(&err,sizeof(err));
860          enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
861          if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
862          cfgs++;
863          opus_encoder_destroy(enc);
864          enc=malloc(opus_encoder_get_size(2));
865          if(enc==NULL)test_failed();
866          err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
867          if(err!=OPUS_BAD_ARG)test_failed();
868          cfgs++;
869          free(enc);
870       }
871    }
872
873    enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
874    if(enc!=NULL)test_failed();
875    cfgs++;
876
877    VG_UNDEF(&err,sizeof(err));
878    enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
879    if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
880    cfgs++;
881
882    VG_UNDEF(&err,sizeof(err));
883    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
884    if(enc==NULL)test_failed();
885    opus_encoder_destroy(enc);
886    cfgs++;
887
888    VG_UNDEF(&err,sizeof(err));
889    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
890    if(err!=OPUS_OK || enc==NULL)test_failed();
891    cfgs++;
892    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
893    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
894    opus_encoder_destroy(enc);
895    cfgs++;
896
897    VG_UNDEF(&err,sizeof(err));
898    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
899    if(err!=OPUS_OK || enc==NULL)test_failed();
900    cfgs++;
901    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
902    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
903    opus_encoder_destroy(enc);
904    cfgs++;
905
906    VG_UNDEF(&err,sizeof(err));
907    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
908    if(err!=OPUS_OK || enc==NULL)test_failed();
909    cfgs++;
910
911    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
912    fprintf(stdout,"    opus_encoder_init() .......................... OK.\n");
913
914    i=-12345;
915    VG_UNDEF(&i,sizeof(i));
916    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
917    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
918    cfgs++;
919    fprintf(stdout,"    OPUS_GET_LOOKAHEAD ........................... OK.\n");
920
921    if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
922    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
923    cfgs++;
924
925    CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
926      OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
927      "    OPUS_SET_APPLICATION ......................... OK.\n",
928      "    OPUS_GET_APPLICATION ......................... OK.\n")
929
930    if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
931    cfgs++;
932    VG_UNDEF(&i,sizeof(i));
933    if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
934    if(i>700000||i<256000)test_failed();
935    cfgs++;
936    CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
937      500,256000,
938      "    OPUS_SET_BITRATE ............................. OK.\n",
939      "    OPUS_GET_BITRATE ............................. OK.\n")
940
941    CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
942      1,OPUS_AUTO,
943      "    OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
944      "    OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
945
946    i=-2;
947    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
948    cfgs++;
949    i=OPUS_BANDWIDTH_FULLBAND+1;
950    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
951    cfgs++;
952    i=OPUS_BANDWIDTH_NARROWBAND;
953    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
954    cfgs++;
955    i=OPUS_BANDWIDTH_FULLBAND;
956    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
957    cfgs++;
958    i=OPUS_BANDWIDTH_WIDEBAND;
959    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
960    cfgs++;
961    i=OPUS_BANDWIDTH_MEDIUMBAND;
962    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
963    cfgs++;
964    fprintf(stdout,"    OPUS_SET_BANDWIDTH ........................... OK.\n");
965    /*We don't test if the bandwidth has actually changed.
966      because the change may be delayed until the encoder is advanced.*/
967    i=-12345;
968    VG_UNDEF(&i,sizeof(i));
969    err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
970    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
971       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
972       i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
973    cfgs++;
974    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
975    cfgs++;
976    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
977
978    i=-2;
979    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
980    cfgs++;
981    i=OPUS_BANDWIDTH_FULLBAND+1;
982    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
983    cfgs++;
984    i=OPUS_BANDWIDTH_NARROWBAND;
985    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
986    cfgs++;
987    i=OPUS_BANDWIDTH_FULLBAND;
988    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
989    cfgs++;
990    i=OPUS_BANDWIDTH_WIDEBAND;
991    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
992    cfgs++;
993    i=OPUS_BANDWIDTH_MEDIUMBAND;
994    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
995    cfgs++;
996    fprintf(stdout,"    OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
997    /*We don't test if the bandwidth has actually changed.
998      because the change may be delayed until the encoder is advanced.*/
999    i=-12345;
1000    VG_UNDEF(&i,sizeof(i));
1001    err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
1002    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1003       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1004       i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
1005    cfgs++;
1006    fprintf(stdout,"    OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
1007
1008    CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
1009      1,0,
1010      "    OPUS_SET_DTX ................................. OK.\n",
1011      "    OPUS_GET_DTX ................................. OK.\n")
1012
1013    CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
1014      0,10,
1015      "    OPUS_SET_COMPLEXITY .......................... OK.\n",
1016      "    OPUS_GET_COMPLEXITY .......................... OK.\n")
1017
1018    CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
1019      1,0,
1020      "    OPUS_SET_INBAND_FEC .......................... OK.\n",
1021      "    OPUS_GET_INBAND_FEC .......................... OK.\n")
1022
1023    CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
1024      100,0,
1025      "    OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
1026      "    OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
1027
1028    CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
1029      1,0,
1030      "    OPUS_SET_VBR ................................. OK.\n",
1031      "    OPUS_GET_VBR ................................. OK.\n")
1032
1033    /*CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
1034      0,50,
1035      "    OPUS_SET_VOICE_RATIO ......................... OK.\n",
1036      "    OPUS_GET_VOICE_RATIO ......................... OK.\n")
1037    */
1038
1039    CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
1040      1,0,
1041      "    OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
1042      "    OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
1043
1044    CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
1045      OPUS_SIGNAL_MUSIC,OPUS_AUTO,
1046      "    OPUS_SET_SIGNAL .............................. OK.\n",
1047      "    OPUS_GET_SIGNAL .............................. OK.\n")
1048
1049    /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
1050
1051    if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
1052    cfgs++;
1053    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
1054
1055    /*Reset the encoder*/
1056    if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
1057    cfgs++;
1058    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
1059
1060    memset(sbuf,0,sizeof(short)*2*960);
1061    VG_UNDEF(packet,sizeof(packet));
1062    i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
1063    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1064    VG_CHECK(packet,i);
1065    cfgs++;
1066    fprintf(stdout,"    opus_encode() ................................ OK.\n");
1067 #ifndef DISABLE_FLOAT_API
1068    memset(fbuf,0,sizeof(float)*2*960);
1069    VG_UNDEF(packet,sizeof(packet));
1070    i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
1071    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1072    VG_CHECK(packet,i);
1073    cfgs++;
1074    fprintf(stdout,"    opus_encode_float() .......................... OK.\n");
1075 #endif
1076
1077 #if 0
1078    /*These tests are disabled because the library crashes with null states*/
1079    if(opus_encoder_ctl(0,OPUS_RESET_STATE)               !=OPUS_INVALID_STATE)test_failed();
1080    if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
1081    if(opus_encode(0,sbuf,960,packet,sizeof(packet))      !=OPUS_INVALID_STATE)test_failed();
1082    if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
1083 #endif
1084    opus_encoder_destroy(enc);
1085    cfgs++;
1086    fprintf(stdout,"                   All encoder interface tests passed\n");
1087    fprintf(stdout,"                             (%d API invocations)\n",cfgs);
1088    return cfgs;
1089 }
1090
1091 #define max_out (1276*48+48*2+2)
1092 int test_repacketizer_api(void)
1093 {
1094    int ret,cfgs,i,j,k;
1095    OpusRepacketizer *rp;
1096    unsigned char *packet;
1097    unsigned char *po;
1098    cfgs=0;
1099    fprintf(stdout,"\n  Repacketizer tests\n");
1100    fprintf(stdout,"  ---------------------------------------------------\n");
1101
1102    packet=malloc(max_out);
1103    if(packet==NULL)test_failed();
1104    po=malloc(max_out);
1105    if(po==NULL)test_failed();
1106    memset(packet,0,max_out);
1107
1108    i=opus_repacketizer_get_size();
1109    if(i<=0)test_failed();
1110    cfgs++;
1111    fprintf(stdout,"    opus_repacketizer_get_size()=%d ............. OK.\n",i);
1112
1113    rp=malloc(i);
1114    rp=opus_repacketizer_init(rp);
1115    if(rp==NULL)test_failed();
1116    cfgs++;
1117    free(rp);
1118    fprintf(stdout,"    opus_repacketizer_init ....................... OK.\n");
1119
1120    rp=opus_repacketizer_create();
1121    if(rp==NULL)test_failed();
1122    cfgs++;
1123    fprintf(stdout,"    opus_repacketizer_create ..................... OK.\n");
1124
1125    if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
1126    cfgs++;
1127    fprintf(stdout,"    opus_repacketizer_get_nb_frames .............. OK.\n");
1128
1129    /*Length overflows*/
1130    VG_UNDEF(packet,4);
1131    if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
1132    cfgs++;
1133    packet[0]=1;
1134    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
1135    cfgs++;
1136    packet[0]=2;
1137    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
1138    cfgs++;
1139    packet[0]=3;
1140    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
1141    cfgs++;
1142    packet[0]=2;
1143    packet[1]=255;
1144    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
1145    cfgs++;
1146    packet[0]=2;
1147    packet[1]=250;
1148    if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
1149    cfgs++;
1150    packet[0]=3;
1151    packet[1]=0;
1152    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
1153    cfgs++;
1154    packet[1]=49;
1155    if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
1156    cfgs++;
1157    packet[0]=0;
1158    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1159    cfgs++;
1160    packet[0]=1<<2;
1161    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
1162    cfgs++;
1163
1164    /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
1165    opus_repacketizer_init(rp);
1166    for(j=0;j<32;j++)
1167    {
1168       /* TOC types, test half with stereo */
1169       int maxi;
1170       packet[0]=((j<<1)+(j&1))<<2;
1171       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1172       for(i=1;i<=maxi;i++)
1173       {
1174          /* Number of CBR frames in the input packets */
1175          int maxp;
1176          packet[0]=((j<<1)+(j&1))<<2;
1177          if(i>1)packet[0]+=i==2?1:3;
1178          packet[1]=i>2?i:0;
1179          maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
1180          for(k=0;k<=(1275+75);k+=3)
1181          {
1182             /*Payload size*/
1183             opus_int32 cnt,rcnt;
1184             if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
1185             for(cnt=0;cnt<maxp+2;cnt++)
1186             {
1187                if(cnt>0)
1188                {
1189                   ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
1190                   if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
1191                   cfgs++;
1192                }
1193                rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
1194                if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
1195                cfgs++;
1196                ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
1197                if(rcnt>0)
1198                {
1199                   int len;
1200                   len=k*rcnt+((rcnt*i)>2?2:1);
1201                   if(ret!=len)test_failed();
1202                   if((rcnt*i)<2&&(po[0]&3)!=0)test_failed();                      /* Code 0 */
1203                   if((rcnt*i)==2&&(po[0]&3)!=1)test_failed();                     /* Code 1 */
1204                   if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
1205                   cfgs++;
1206                   if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1207                   cfgs++;
1208                   if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1209                   cfgs++;
1210                   if(len>1)
1211                   {
1212                      if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1213                      cfgs++;
1214                   }
1215                   if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1216                   cfgs++;
1217                } else if (ret!=OPUS_BAD_ARG)test_failed();                        /* M must not be 0 */
1218             }
1219             opus_repacketizer_init(rp);
1220          }
1221       }
1222    }
1223
1224    /*Change in input count code, CBR out*/
1225    opus_repacketizer_init(rp);
1226    packet[0]=0;
1227    if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
1228    cfgs++;
1229    packet[0]+=1;
1230    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1231    cfgs++;
1232    i=opus_repacketizer_out(rp,po,max_out);
1233    if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
1234    cfgs++;
1235    i=opus_repacketizer_out_range(rp,0,1,po,max_out);
1236    if(i!=5||(po[0]&3)!=0)test_failed();
1237    cfgs++;
1238    i=opus_repacketizer_out_range(rp,1,2,po,max_out);
1239    if(i!=5||(po[0]&3)!=0)test_failed();
1240    cfgs++;
1241
1242    /*Change in input count code, VBR out*/
1243    opus_repacketizer_init(rp);
1244    packet[0]=1;
1245    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1246    cfgs++;
1247    packet[0]=0;
1248    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1249    cfgs++;
1250    i=opus_repacketizer_out(rp,po,max_out);
1251    if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
1252    cfgs++;
1253
1254    /*VBR in, VBR out*/
1255    opus_repacketizer_init(rp);
1256    packet[0]=2;
1257    packet[1]=4;
1258    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1259    cfgs++;
1260    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1261    cfgs++;
1262    i=opus_repacketizer_out(rp,po,max_out);
1263    if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
1264    cfgs++;
1265
1266    /*VBR in, CBR out*/
1267    opus_repacketizer_init(rp);
1268    packet[0]=2;
1269    packet[1]=4;
1270    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1271    cfgs++;
1272    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1273    cfgs++;
1274    i=opus_repacketizer_out(rp,po,max_out);
1275    if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
1276    cfgs++;
1277
1278    /*Count 0 in, VBR out*/
1279    for(j=0;j<32;j++)
1280    {
1281       /* TOC types, test half with stereo */
1282       int maxi,sum,rcnt;
1283       packet[0]=((j<<1)+(j&1))<<2;
1284       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1285       sum=0;
1286       rcnt=0;
1287       opus_repacketizer_init(rp);
1288       for(i=1;i<=maxi+2;i++)
1289       {
1290          int len;
1291          ret=opus_repacketizer_cat(rp,packet,i);
1292          if(rcnt<maxi)
1293          {
1294             if(ret!=OPUS_OK)test_failed();
1295             rcnt++;
1296             sum+=i-1;
1297          } else if (ret!=OPUS_INVALID_PACKET)test_failed();
1298          cfgs++;
1299          len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
1300          if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
1301          if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
1302          if(rcnt==2&&(po[0]&3)!=2)test_failed();
1303          if(rcnt==1&&(po[0]&3)!=0)test_failed();
1304          cfgs++;
1305          if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1306          cfgs++;
1307          if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1308          cfgs++;
1309          if(len>1)
1310          {
1311             if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1312             cfgs++;
1313          }
1314          if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1315          cfgs++;
1316       }
1317    }
1318
1319    fprintf(stdout,"    opus_repacketizer_cat ........................ OK.\n");
1320    fprintf(stdout,"    opus_repacketizer_out ........................ OK.\n");
1321    fprintf(stdout,"    opus_repacketizer_out_range .................. OK.\n");
1322
1323    opus_repacketizer_destroy(rp);
1324    cfgs++;
1325    free(packet);
1326    free(po);
1327    fprintf(stdout,"                        All repacketizer tests passed\n");
1328    fprintf(stdout,"                            (%7d API invocations)\n",cfgs);
1329
1330    return cfgs;
1331 }
1332
1333 #ifdef MALLOC_FAIL
1334 /* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
1335  * under GCC. However, this is the cleanest way to test malloc failure
1336  * handling in our codebase, and the lack of thread saftey isn't an
1337  * issue here. We therefore disable the warning for this function.
1338  */
1339 #if __GNUC_PREREQ(4,6)
1340 /* Save the current warning settings */
1341 #pragma GCC diagnostic push
1342 #endif
1343 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1344
1345 typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
1346 #endif
1347
1348 int test_malloc_fail(void)
1349 {
1350 #ifdef MALLOC_FAIL
1351    OpusDecoder *dec;
1352    OpusEncoder *enc;
1353    OpusRepacketizer *rp;
1354    int rate,c,app,cfgs,err,useerr;
1355    int *ep;
1356    mhook orig_malloc;
1357    cfgs=0;
1358 #endif
1359    fprintf(stdout,"\n  malloc() failure tests\n");
1360    fprintf(stdout,"  ---------------------------------------------------\n");
1361 #ifdef MALLOC_FAIL
1362    orig_malloc=__malloc_hook;
1363    __malloc_hook=malloc_hook;
1364    ep=(int *)opus_alloc(sizeof(int));
1365    if(ep!=NULL)
1366    {
1367       if(ep)free(ep);
1368       __malloc_hook=orig_malloc;
1369 #endif
1370       fprintf(stdout,"    opus_decoder_create() ................... SKIPPED.\n");
1371       fprintf(stdout,"    opus_encoder_create() ................... SKIPPED.\n");
1372       fprintf(stdout,"    opus_repacketizer_create() .............. SKIPPED.\n");
1373       fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
1374       return 0;
1375 #ifdef MALLOC_FAIL
1376    }
1377    for(useerr=0;useerr<2;useerr++)
1378    {
1379       ep=useerr?&err:0;
1380       for(rate=0;rate<5;rate++)
1381       {
1382         for(c=1;c<3;c++)
1383         {
1384            err=1;
1385            if(useerr)
1386            {
1387               VG_UNDEF(&err,sizeof(err));
1388            }
1389            dec=opus_decoder_create(opus_rates[rate], c, ep);
1390            if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1391            {
1392               __malloc_hook=orig_malloc;
1393               test_failed();
1394            }
1395            cfgs++;
1396            for(app=0;app<3;app++)
1397            {
1398               if(useerr)
1399               {
1400                  VG_UNDEF(&err,sizeof(err));
1401               }
1402               enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
1403               if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1404               {
1405                  __malloc_hook=orig_malloc;
1406                  test_failed();
1407               }
1408               cfgs++;
1409            }
1410         }
1411      }
1412    }
1413    rp=opus_repacketizer_create();
1414    if(rp!=NULL)
1415    {
1416       __malloc_hook=orig_malloc;
1417       test_failed();
1418    }
1419    cfgs++;
1420    __malloc_hook=orig_malloc;
1421    fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
1422    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
1423    fprintf(stdout,"    opus_repacketizer_create() ................... OK.\n");
1424    fprintf(stdout,"                      All malloc failure tests passed\n");
1425    fprintf(stdout,"                                 (%2d API invocations)\n",cfgs);
1426    return cfgs;
1427 #endif
1428 }
1429
1430 #ifdef MALLOC_FAIL
1431 #if __GNUC_PREREQ(4,6)
1432 #pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
1433 #endif
1434 #endif
1435
1436 int main(int _argc, char **_argv)
1437 {
1438    opus_int32 total;
1439    const char * oversion;
1440    if(_argc>1)
1441    {
1442       fprintf(stderr,"Usage: %s\n",_argv[0]);
1443       return 1;
1444    }
1445    iseed=0;
1446
1447    oversion=opus_get_version_string();
1448    if(!oversion)test_failed();
1449    fprintf(stderr,"Testing the %s API deterministically\n", oversion);
1450    if(opus_strerror(-32768)==NULL)test_failed();
1451    if(opus_strerror(32767)==NULL)test_failed();
1452    if(strlen(opus_strerror(0))<1)test_failed();
1453    total=4;
1454
1455    total+=test_dec_api();
1456    total+=test_msdec_api();
1457    total+=test_parse();
1458    total+=test_enc_api();
1459    total+=test_repacketizer_api();
1460    total+=test_malloc_fail();
1461
1462    fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
1463
1464    return 0;
1465 }