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