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