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