Add a test for MAX_BANDWIDTH to test_opus_api.
[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 #ifdef __GLIBC__
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 char dash_head[54] = "  ---------------------------------------------------\n";
76 static opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};
77 static opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP,
78        OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY};
79
80 opus_int32 test_dec_api(void)
81 {
82    opus_uint32 dec_final_range;
83    OpusDecoder *dec;
84    OpusDecoder *dec2;
85    opus_int32 i,j,cfgs;
86    unsigned char packet[1276];
87 #ifndef DISABLE_FLOAT_API
88    float fbuf[960*2];
89 #endif
90    short sbuf[960*2];
91    int c,err;
92    int *nullptr;
93    nullptr=0;
94
95    cfgs=0;
96    /*First test invalid configurations which should fail*/
97    fprintf(stdout,"\n  Decoder basic API tests\n");
98    fprintf(stdout,dash_head);
99    for(c=0;c<4;c++)
100    {
101       i=opus_decoder_get_size(c);
102       if(((c==1||c==2)&&(i<=2048||i>1<<16))||((c!=1&&c!=2)&&i!=0))test_failed();
103       fprintf(stdout,"    opus_decoder_get_size(%d)=%d ..............%s OK.\n",c,i,i>0?"":"....");
104       cfgs++;
105    }
106
107    /*Test with unsupported sample rates*/
108    for(c=0;c<4;c++)
109    {
110       for(i=-7;i<=96000;i++)
111       {
112          int fs;
113          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
114          switch(i)
115          {
116            case(-5):fs=-8000;break;
117            case(-6):fs=INT32_MAX;break;
118            case(-7):fs=INT32_MIN;break;
119            default:fs=i;
120          }
121          err = OPUS_OK;
122          VG_UNDEF(&err,sizeof(err));
123          dec = opus_decoder_create(fs, c, &err);
124          if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
125          cfgs++;
126          dec=malloc(opus_decoder_get_size(2));
127          if(dec==NULL)test_failed();
128          err = opus_decoder_init(dec,fs,c);
129          if(err!=OPUS_BAD_ARG)test_failed();
130          cfgs++;
131          free(dec);
132       }
133    }
134
135    VG_UNDEF(&err,sizeof(err));
136    dec = opus_decoder_create(48000, 2, &err);
137    if(err!=OPUS_OK || dec==NULL)test_failed();
138    VG_CHECK(dec,opus_decoder_get_size(2));
139    cfgs++;
140
141    fprintf(stdout,"    opus_decoder_create() ....................... OK.\n");
142    fprintf(stdout,"    opus_decoder_init() ......................... OK.\n");
143
144    VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
145    err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
146    if(err!=OPUS_OK)test_failed();
147    VG_CHECK(&dec_final_range,sizeof(dec_final_range));
148    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ........................ OK.\n");
149    cfgs++;
150
151    err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
152    if(err!=OPUS_UNIMPLEMENTED)test_failed();
153    fprintf(stdout,"    OPUS_UNIMPLEMENTED .......................... OK.\n");
154    cfgs++;
155
156    VG_UNDEF(&i,sizeof(i));
157    err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
158    if(err != OPUS_OK || i!=0)test_failed();
159    fprintf(stdout,"    OPUS_GET_BANDWIDTH .......................... OK.\n");
160    cfgs++;
161
162    /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
163    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(nullptr));
164    if(err!=OPUS_BAD_ARG)test_failed();
165    cfgs++;
166    VG_UNDEF(&i,sizeof(i));
167    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
168    if(err != OPUS_OK || i>0 || i<-1)test_failed();
169    cfgs++;
170    VG_UNDEF(packet,sizeof(packet));
171    packet[0]=63<<2;packet[1]=packet[2]=0;
172    if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
173    cfgs++;
174    VG_UNDEF(&i,sizeof(i));
175    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
176    if(err != OPUS_OK || i>0 || i<-1)test_failed();
177    cfgs++;
178    packet[0]=1;
179    if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
180    cfgs++;
181    VG_UNDEF(&i,sizeof(i));
182    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
183    if(err != OPUS_OK || i>0 || i<-1)test_failed();
184    cfgs++;
185    fprintf(stdout,"    OPUS_GET_PITCH .............................. OK.\n");
186
187    /*Reset the decoder*/
188    dec2=malloc(opus_decoder_get_size(2));
189    memcpy(dec2,dec,opus_decoder_get_size(2));
190    if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
191    if(memcmp(dec2,dec,opus_decoder_get_size(2))==0)test_failed();
192    free(dec2);
193    fprintf(stdout,"    OPUS_RESET_STATE ............................ OK.\n");
194    cfgs++;
195
196    VG_UNDEF(packet,sizeof(packet));
197    packet[0]=0;
198    if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
199    cfgs++;
200    packet[0]=(63<<2)|3;
201    packet[1]=63;
202    if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed();
203    fprintf(stdout,"    opus_decoder_get_nb_samples() ............... OK.\n");
204    cfgs++;
205
206    if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();
207    for(i=0;i<256;i++) {
208      int l1res[4]={1,2,2,OPUS_INVALID_PACKET};
209      packet[0]=i;
210      if(l1res[packet[0]&3]!=opus_packet_get_nb_frames(packet,1))test_failed();
211      cfgs++;
212      for(j=0;j<256;j++) {
213        packet[1]=j;
214        if(((packet[0]&3)!=3?l1res[packet[0]&3]:packet[1]&63)!=opus_packet_get_nb_frames(packet,2))test_failed();
215        cfgs++;
216      }
217    }
218    fprintf(stdout,"    opus_packet_get_nb_frames() ................. OK.\n");
219
220    for(i=0;i<256;i++) {
221      int bw;
222      packet[0]=i;
223      bw=packet[0]>>4;
224      bw=OPUS_BANDWIDTH_NARROWBAND+(((((bw&7)*9)&(63-(bw&8)))+2+12*((bw&8)!=0))>>4);
225      if(bw!=opus_packet_get_bandwidth(packet))test_failed();
226      cfgs++;
227    }
228    fprintf(stdout,"    opus_packet_get_bandwidth() ................. OK.\n");
229
230    for(i=0;i<256;i++) {
231      int fp3s,rate;
232      packet[0]=i;
233      fp3s=packet[0]>>3;
234      fp3s=((((3-(fp3s&3))*13&119)+9)>>2)*((fp3s>13)*(3-((fp3s&3)==3))+1)*25;
235      for(rate=0;rate<5;rate++) {
236        if((opus_rates[rate]*3/fp3s)!=opus_packet_get_samples_per_frame(packet,opus_rates[rate]))test_failed();
237        cfgs++;
238      }
239    }
240    fprintf(stdout,"    opus_packet_get_samples_per_frame() ......... OK.\n");
241
242    packet[0]=(63<<2)+3;
243    packet[1]=49;
244    for(j=2;j<51;j++)packet[j]=0;
245    VG_UNDEF(sbuf,sizeof(sbuf));
246    if(opus_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
247    cfgs++;
248    packet[0]=(63<<2);
249    packet[1]=packet[2]=0;
250    if(opus_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG)test_failed();
251    cfgs++;
252    if(opus_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
253    cfgs++;
254    if(opus_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
255    cfgs++;
256    if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
257    cfgs++;
258    fprintf(stdout,"    opus_decode() ............................... OK.\n");
259 #ifndef DISABLE_FLOAT_API
260    VG_UNDEF(fbuf,sizeof(fbuf));
261    if(opus_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
262    cfgs++;
263    fprintf(stdout,"    opus_decode_float() ......................... OK.\n");
264 #endif
265
266 #if 0
267    /*These tests are disabled because the library crashes with null states*/
268    if(opus_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
269    if(opus_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
270    if(opus_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
271    if(opus_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
272    if(opus_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
273    if(opus_packet_get_nb_frames(NULL,1)            !=OPUS_BAD_ARG)test_failed();
274    if(opus_packet_get_bandwidth(NULL)              !=OPUS_BAD_ARG)test_failed();
275    if(opus_packet_get_samples_per_frame(NULL,48000)!=OPUS_BAD_ARG)test_failed();
276 #endif
277    opus_decoder_destroy(dec);
278    cfgs++;
279    fprintf(stdout,"                   All decoder interface tests passed\n");
280    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
281    return cfgs;
282 }
283
284 #ifdef VALGRIND
285 #define UNDEFINE_FOR_PARSE  toc=-1; \
286    frames[0]=(unsigned char *)0; \
287    frames[1]=(unsigned char *)0; \
288    payload_offset=-1; \
289    VG_UNDEF(&toc,sizeof(toc)); \
290    VG_UNDEF(frames,sizeof(frames));\
291    VG_UNDEF(&payload_offset,sizeof(payload_offset));
292 #else
293 #define UNDEFINE_FOR_PARSE  toc=-1; \
294    frames[0]=(unsigned char *)0; \
295    frames[1]=(unsigned char *)0; \
296    payload_offset=-1;
297 #endif
298
299 /* This test exercises the heck out of the libopus parser.
300    It is much larger than the parser itself in part because
301    it tries to hit a lot of corner cases that could never
302    fail with the libopus code, but might be problematic for
303    other implementations. */
304 opus_int32 test_parse(void)
305 {
306    opus_int32 i,j,jj,sz;
307    unsigned char packet[1276];
308    opus_int32 cfgs,cfgs_total;
309    unsigned char toc;
310    const unsigned char *frames[48];
311    short size[48];
312    int payload_offset, ret;
313    fprintf(stdout,"\n  Packet header parsing tests\n");
314    fprintf(stdout,dash_head);
315    memset(packet,0,sizeof(char)*1276);
316    packet[0]=63<<2;
317    if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
318    cfgs_total=cfgs=1;
319    /*code 0*/
320    for(i=0;i<64;i++)
321    {
322       UNDEFINE_FOR_PARSE
323       ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
324       cfgs++;
325       if(ret!=1)test_failed();
326       if(size[0]!=3)test_failed();
327       if(frames[0]!=packet+1)test_failed();
328    }
329    fprintf(stdout,"    code 0 (%2d cases) ........................... OK.\n",cfgs);
330    cfgs_total+=cfgs;cfgs=0;
331
332    /*code 1, two frames of the same size*/
333    for(i=0;i<64;i++)
334    {
335       packet[0]=(i<<2)+1;
336       for(jj=0;jj<=1275*2+3;jj++)
337       {
338          UNDEFINE_FOR_PARSE
339          ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
340          cfgs++;
341          if((jj&1)==1 && jj<=2551)
342          {
343             /* Must pass if payload length even (packet length odd) and
344                size<=2551, must fail otherwise. */
345             if(ret!=2)test_failed();
346             if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
347             if(frames[0]!=packet+1)test_failed();
348             if(frames[1]!=frames[0]+size[0])test_failed();
349             if((toc>>2)!=i)test_failed();
350          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
351       }
352    }
353    fprintf(stdout,"    code 1 (%6d cases) ....................... OK.\n",cfgs);
354    cfgs_total+=cfgs;cfgs=0;
355
356    for(i=0;i<64;i++)
357    {
358       /*code 2, length code overflow*/
359       packet[0]=(i<<2)+2;
360       UNDEFINE_FOR_PARSE
361       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
362       cfgs++;
363       if(ret!=OPUS_INVALID_PACKET)test_failed();
364       packet[1]=252;
365       UNDEFINE_FOR_PARSE
366       ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
367       cfgs++;
368       if(ret!=OPUS_INVALID_PACKET)test_failed();
369       for(j=0;j<1275;j++)
370       {
371          if(j<252)packet[1]=j;
372          else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
373          /*Code 2, one too short*/
374          UNDEFINE_FOR_PARSE
375          ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
376          cfgs++;
377          if(ret!=OPUS_INVALID_PACKET)test_failed();
378          /*Code 2, one too long*/
379          UNDEFINE_FOR_PARSE
380          ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
381          cfgs++;
382          if(ret!=OPUS_INVALID_PACKET)test_failed();
383          /*Code 2, second zero*/
384          UNDEFINE_FOR_PARSE
385          ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
386          cfgs++;
387          if(ret!=2)test_failed();
388          if(size[0]!=j||size[1]!=0)test_failed();
389          if(frames[1]!=frames[0]+size[0])test_failed();
390          if((toc>>2)!=i)test_failed();
391          /*Code 2, normal*/
392          UNDEFINE_FOR_PARSE
393          ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
394          cfgs++;
395          if(ret!=2)test_failed();
396          if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
397          if(frames[1]!=frames[0]+size[0])test_failed();
398          if((toc>>2)!=i)test_failed();
399       }
400    }
401    fprintf(stdout,"    code 2 (%6d cases) ....................... OK.\n",cfgs);
402    cfgs_total+=cfgs;cfgs=0;
403
404    for(i=0;i<64;i++)
405    {
406       packet[0]=(i<<2)+3;
407       /*code 3, length code overflow*/
408       UNDEFINE_FOR_PARSE
409       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
410       cfgs++;
411       if(ret!=OPUS_INVALID_PACKET)test_failed();
412    }
413    fprintf(stdout,"    code 3 m-truncation (%2d cases) .............. OK.\n",cfgs);
414    cfgs_total+=cfgs;cfgs=0;
415
416    for(i=0;i<64;i++)
417    {
418       /*code 3, m is zero or 49-63*/
419       packet[0]=(i<<2)+3;
420       for(jj=49;jj<=64;jj++)
421       {
422         packet[1]=0+(jj&63); /*CBR, no padding*/
423         UNDEFINE_FOR_PARSE
424         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
425         cfgs++;
426         if(ret!=OPUS_INVALID_PACKET)test_failed();
427         packet[1]=128+(jj&63); /*VBR, no padding*/
428         UNDEFINE_FOR_PARSE
429         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
430         cfgs++;
431         if(ret!=OPUS_INVALID_PACKET)test_failed();
432         packet[1]=64+(jj&63); /*CBR, padding*/
433         UNDEFINE_FOR_PARSE
434         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
435         cfgs++;
436         if(ret!=OPUS_INVALID_PACKET)test_failed();
437         packet[1]=128+64+(jj&63); /*VBR, padding*/
438         UNDEFINE_FOR_PARSE
439         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
440         cfgs++;
441         if(ret!=OPUS_INVALID_PACKET)test_failed();
442       }
443    }
444    fprintf(stdout,"    code 3 m=0,49-64 (%2d cases) ............... OK.\n",cfgs);
445    cfgs_total+=cfgs;cfgs=0;
446
447    for(i=0;i<64;i++)
448    {
449       packet[0]=(i<<2)+3;
450       /*code 3, m is one, cbr*/
451       packet[1]=1;
452       for(j=0;j<1276;j++)
453       {
454         UNDEFINE_FOR_PARSE
455         ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
456         cfgs++;
457         if(ret!=1)test_failed();
458         if(size[0]!=j)test_failed();
459         if((toc>>2)!=i)test_failed();
460       }
461       UNDEFINE_FOR_PARSE
462       ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
463       cfgs++;
464       if(ret!=OPUS_INVALID_PACKET)test_failed();
465    }
466    fprintf(stdout,"    code 3 m=1 CBR (%2d cases) ................ OK.\n",cfgs);
467    cfgs_total+=cfgs;cfgs=0;
468
469    for(i=0;i<64;i++)
470    {
471       int frame_samp;
472       /*code 3, m>1 CBR*/
473       packet[0]=(i<<2)+3;
474       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
475       for(j=2;j<49;j++)
476       {
477          packet[1]=j;
478          for(sz=2;sz<((j+2)*1275);sz++)
479          {
480             UNDEFINE_FOR_PARSE
481             ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
482             cfgs++;
483             /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
484             if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
485             {
486                if(ret!=j)test_failed();
487                for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
488                if((toc>>2)!=i)test_failed();
489             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
490          }
491       }
492       /*Super jumbo packets*/
493       packet[1]=5760/frame_samp;
494       UNDEFINE_FOR_PARSE
495       ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
496       cfgs++;
497       if(ret!=packet[1])test_failed();
498       for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
499    }
500    fprintf(stdout,"    code 3 m=1-48 CBR (%2d cases) ......... OK.\n",cfgs);
501    cfgs_total+=cfgs;cfgs=0;
502
503    for(i=0;i<64;i++)
504    {
505       int frame_samp;
506       /*Code 3 VBR, m one*/
507       packet[0]=(i<<2)+3;
508       packet[1]=128+1;
509       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
510       for(jj=0;jj<1276;jj++)
511       {
512          UNDEFINE_FOR_PARSE
513          ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
514          cfgs++;
515          if(ret!=1)test_failed();
516          if(size[0]!=jj)test_failed();
517          if((toc>>2)!=i)test_failed();
518       }
519       UNDEFINE_FOR_PARSE
520       ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
521       cfgs++;
522       if(ret!=OPUS_INVALID_PACKET)test_failed();
523       for(j=2;j<49;j++)
524       {
525          packet[1]=128+j;
526          /*Length code overflow*/
527          UNDEFINE_FOR_PARSE
528          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
529          cfgs++;
530          if(ret!=OPUS_INVALID_PACKET)test_failed();
531          packet[2]=252;
532          packet[3]=0;
533          for(jj=4;jj<2+j;jj++)packet[jj]=0;
534          UNDEFINE_FOR_PARSE
535          ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
536          cfgs++;
537          if(ret!=OPUS_INVALID_PACKET)test_failed();
538          /*One byte too short*/
539          for(jj=2;jj<2+j;jj++)packet[jj]=0;
540          UNDEFINE_FOR_PARSE
541          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
542          cfgs++;
543          if(ret!=OPUS_INVALID_PACKET)test_failed();
544          /*One byte too short thanks to length coding*/
545          packet[2]=252;
546          packet[3]=0;
547          for(jj=4;jj<2+j;jj++)packet[jj]=0;
548          UNDEFINE_FOR_PARSE
549          ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
550          cfgs++;
551          if(ret!=OPUS_INVALID_PACKET)test_failed();
552          /*Most expensive way of coding zeros*/
553          for(jj=2;jj<2+j;jj++)packet[jj]=0;
554          UNDEFINE_FOR_PARSE
555          ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
556          cfgs++;
557          if(frame_samp*j<=5760){
558             if(ret!=j)test_failed();
559             for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
560             if((toc>>2)!=i)test_failed();
561          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
562          /*Quasi-CBR use of mode 3*/
563          for(sz=0;sz<8;sz++)
564          {
565             const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
566             int pos=0;
567             int as=(tsz[sz]+i-j-2)/j;
568             for(jj=0;jj<j-1;jj++)
569             {
570               if(as<252){packet[2+pos]=as;pos++;}
571               else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
572             }
573             UNDEFINE_FOR_PARSE
574             ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
575             cfgs++;
576             if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
577                if(ret!=j)test_failed();
578                for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
579                if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
580                if((toc>>2)!=i)test_failed();
581             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
582          }
583       }
584    }
585    fprintf(stdout,"    code 3 m=1-48 VBR (%2d cases) ............ OK.\n",cfgs);
586    cfgs_total+=cfgs;cfgs=0;
587
588    for(i=0;i<64;i++)
589    {
590       packet[0]=(i<<2)+3;
591       /*Padding*/
592       packet[1]=128+1+64;
593       /*Overflow the length coding*/
594       for(jj=2;jj<127;jj++)packet[jj]=255;
595       UNDEFINE_FOR_PARSE
596       ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
597       cfgs++;
598       if(ret!=OPUS_INVALID_PACKET)test_failed();
599
600       for(sz=0;sz<4;sz++)
601       {
602          const int tsz[4]={0,72,512,1275};
603          for(jj=sz;jj<65025;jj+=11)
604          {
605             int pos;
606             for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
607             packet[2+pos]=jj%254;
608             pos++;
609             if(sz==0&&i==63)
610             {
611                /*Code more padding than there is room in the packet*/
612                UNDEFINE_FOR_PARSE
613                ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
614                cfgs++;
615                if(ret!=OPUS_INVALID_PACKET)test_failed();
616             }
617             UNDEFINE_FOR_PARSE
618             ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
619             cfgs++;
620             if(tsz[sz]+i<1276)
621             {
622                if(ret!=1)test_failed();
623                if(size[0]!=tsz[sz]+i)test_failed();
624                if((toc>>2)!=i)test_failed();
625             } else if (ret!=OPUS_INVALID_PACKET)test_failed();
626          }
627       }
628    }
629    fprintf(stdout,"    code 3 padding (%2d cases) .............. OK.\n",cfgs);
630    cfgs_total+=cfgs;cfgs=0;
631    fprintf(stdout,"    opus_packet_parse ........................... OK.\n");
632    fprintf(stdout,"                      All packet parsing tests passed\n");
633    fprintf(stdout,"                          (%d API invocations)\n",cfgs_total);
634    return cfgs_total;
635 }
636
637 /* This is a helper macro for the encoder tests.
638    The encoder api tests all have a pattern of set-must-fail, set-must-fail,
639    set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
640 #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
641    i=(badv);\
642    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
643    i=(badv2);\
644    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
645    j=i=(goodv);\
646    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
647    i=-12345;\
648    VG_UNDEF(&i,sizeof(i)); \
649    err=opus_encoder_ctl(enc,getcall);\
650    if(err!=OPUS_OK || i!=j)test_failed();\
651    j=i=(goodv2);\
652    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
653    fprintf(stdout,sok);\
654    i=-12345;\
655    VG_UNDEF(&i,sizeof(i)); \
656    err=opus_encoder_ctl(enc,getcall);\
657    if(err!=OPUS_OK || i!=j)test_failed();\
658    fprintf(stdout,gok);\
659    cfgs+=6;
660
661 opus_int32 test_enc_api(void)
662 {
663    opus_uint32 enc_final_range;
664    OpusEncoder *enc;
665    opus_int32 i,j;
666    unsigned char packet[1276];
667 #ifndef DISABLE_FLOAT_API
668    float fbuf[960*2];
669 #endif
670    short sbuf[960*2];
671    int c,err,cfgs;
672
673    cfgs=0;
674    /*First test invalid configurations which should fail*/
675    fprintf(stdout,"\n  Encoder basic API tests\n");
676    fprintf(stdout,dash_head);
677    for(c=0;c<4;c++)
678    {
679       i=opus_encoder_get_size(c);
680       if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
681       fprintf(stdout,"    opus_encoder_get_size(%d)=%d ..............%s OK.\n",c,i,i>0?"":"....");
682       cfgs++;
683    }
684
685    /*Test with unsupported sample rates, channel counts*/
686    for(c=0;c<4;c++)
687    {
688       for(i=-7;i<=96000;i++)
689       {
690          int fs;
691          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
692          switch(i)
693          {
694            case(-5):fs=-8000;break;
695            case(-6):fs=INT32_MAX;break;
696            case(-7):fs=INT32_MIN;break;
697            default:fs=i;
698          }
699          err = OPUS_OK;
700          VG_UNDEF(&err,sizeof(err));
701          enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
702          if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
703          cfgs++;
704          opus_encoder_destroy(enc);
705          enc=malloc(opus_encoder_get_size(2));
706          if(enc==NULL)test_failed();
707          err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
708          if(err!=OPUS_BAD_ARG)test_failed();
709          cfgs++;
710          free(enc);
711       }
712    }
713
714    enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
715    if(enc!=NULL)test_failed();
716    cfgs++;
717
718    VG_UNDEF(&err,sizeof(err));
719    enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
720    if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
721    cfgs++;
722
723    VG_UNDEF(&err,sizeof(err));
724    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
725    if(enc==NULL)test_failed();
726    opus_encoder_destroy(enc);
727    cfgs++;
728
729    VG_UNDEF(&err,sizeof(err));
730    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
731    if(err!=OPUS_OK || enc==NULL)test_failed();
732    cfgs++;
733    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
734    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
735    opus_encoder_destroy(enc);
736    cfgs++;
737
738    VG_UNDEF(&err,sizeof(err));
739    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
740    if(err!=OPUS_OK || enc==NULL)test_failed();
741    cfgs++;
742    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
743    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
744    opus_encoder_destroy(enc);
745    cfgs++;
746
747    VG_UNDEF(&err,sizeof(err));
748    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
749    if(err!=OPUS_OK || enc==NULL)test_failed();
750    cfgs++;
751
752    fprintf(stdout,"    opus_encoder_create() ....................... OK.\n");
753    fprintf(stdout,"    opus_encoder_init() ......................... OK.\n");
754
755    i=-12345;
756    VG_UNDEF(&i,sizeof(i));
757    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
758    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
759    cfgs++;
760    fprintf(stdout,"    OPUS_GET_LOOKAHEAD .......................... OK.\n");
761
762    if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
763    fprintf(stdout,"    OPUS_UNIMPLEMENTED .......................... OK.\n");
764    cfgs++;
765
766    CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
767      OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
768      "    OPUS_SET_APPLICATION ........................ OK.\n",
769      "    OPUS_GET_APPLICATION ........................ OK.\n")
770
771    if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
772    cfgs++;
773    VG_UNDEF(&i,sizeof(i));
774    if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
775    if(i>700000||i<256000)test_failed();
776    cfgs++;
777    CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
778      500,256000,
779      "    OPUS_SET_BITRATE ............................ OK.\n",
780      "    OPUS_GET_BITRATE ............................ OK.\n")
781
782    CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
783      1,OPUS_AUTO,
784      "    OPUS_SET_FORCE_CHANNELS ..................... OK.\n",
785      "    OPUS_GET_FORCE_CHANNELS ..................... OK.\n")
786
787    i=-2;
788    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
789    cfgs++;
790    i=OPUS_BANDWIDTH_FULLBAND+1;
791    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
792    cfgs++;
793    i=OPUS_BANDWIDTH_NARROWBAND;
794    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
795    cfgs++;
796    i=OPUS_BANDWIDTH_FULLBAND;
797    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
798    cfgs++;
799    i=OPUS_BANDWIDTH_WIDEBAND;
800    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
801    cfgs++;
802    i=OPUS_BANDWIDTH_MEDIUMBAND;
803    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
804    cfgs++;
805    fprintf(stdout,"    OPUS_SET_BANDWIDTH .......................... OK.\n");
806    /*We don't test if the bandwidth has actually changed.
807      because the change may be delayed until the encoder is advanced.*/
808    i=-12345;
809    VG_UNDEF(&i,sizeof(i));
810    err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
811    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
812       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
813       i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
814    cfgs++;
815    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
816    cfgs++;
817    fprintf(stdout,"    OPUS_GET_BANDWIDTH .......................... OK.\n");
818
819    i=-2;
820    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
821    cfgs++;
822    i=OPUS_BANDWIDTH_FULLBAND+1;
823    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
824    cfgs++;
825    i=OPUS_BANDWIDTH_NARROWBAND;
826    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
827    cfgs++;
828    i=OPUS_BANDWIDTH_FULLBAND;
829    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
830    cfgs++;
831    i=OPUS_BANDWIDTH_WIDEBAND;
832    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
833    cfgs++;
834    i=OPUS_BANDWIDTH_MEDIUMBAND;
835    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
836    cfgs++;
837    fprintf(stdout,"    OPUS_SET_MAX_BANDWIDTH ...................... OK.\n");
838    /*We don't test if the bandwidth has actually changed.
839      because the change may be delayed until the encoder is advanced.*/
840    i=-12345;
841    VG_UNDEF(&i,sizeof(i));
842    err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
843    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
844       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
845       i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
846    cfgs++;
847    fprintf(stdout,"    OPUS_GET_MAX_BANDWIDTH ...................... OK.\n");
848
849    CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
850      1,0,
851      "    OPUS_SET_DTX ................................ OK.\n",
852      "    OPUS_GET_DTX ................................ OK.\n")
853
854    CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
855      0,10,
856      "    OPUS_SET_COMPLEXITY ......................... OK.\n",
857      "    OPUS_GET_COMPLEXITY ......................... OK.\n")
858
859    CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
860      1,0,
861      "    OPUS_SET_INBAND_FEC ......................... OK.\n",
862      "    OPUS_GET_INBAND_FEC ......................... OK.\n")
863
864    CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
865      100,0,
866      "    OPUS_SET_PACKET_LOSS_PERC ................... OK.\n",
867      "    OPUS_GET_PACKET_LOSS_PERC ................... OK.\n")
868
869    CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
870      1,0,
871      "    OPUS_SET_VBR ................................ OK.\n",
872      "    OPUS_GET_VBR ................................ OK.\n")
873
874    CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
875      0,50,
876      "    OPUS_SET_VOICE_RATIO ........................ OK.\n",
877      "    OPUS_GET_VOICE_RATIO ........................ OK.\n")
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,dash_head);
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 typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
1164 #endif
1165
1166 int test_malloc_fail(void)
1167 {
1168 #ifdef MALLOC_FAIL
1169    OpusDecoder *dec;
1170    OpusEncoder *enc;
1171    OpusRepacketizer *rp;
1172    int rate,c,app,cfgs,err,useerr;
1173    int *ep;
1174    mhook orig_malloc;
1175    cfgs=0;
1176 #endif
1177    fprintf(stdout,"\n  malloc() failure tests\n");
1178    fprintf(stdout,dash_head);
1179 #ifdef MALLOC_FAIL
1180    orig_malloc=__malloc_hook;
1181    __malloc_hook=malloc_hook;
1182    ep=(int *)opus_alloc(sizeof(int));
1183    if(ep!=NULL)
1184    {
1185       if(ep)free(ep);
1186       __malloc_hook=orig_malloc;
1187 #endif
1188       fprintf(stdout,"    opus_decoder_create() .................. SKIPPED.\n");
1189       fprintf(stdout,"    opus_encoder_create() .................. SKIPPED.\n");
1190       fprintf(stdout,"    opus_repacketizer_create() ............. SKIPPED.\n");
1191       fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
1192       return 0;
1193 #ifdef MALLOC_FAIL
1194    }
1195    for(useerr=0;useerr<2;useerr++)
1196    {
1197       ep=useerr?&err:0;
1198       for(rate=0;rate<5;rate++)
1199       {
1200         for(c=1;c<3;c++)
1201         {
1202            err=1;
1203            if(useerr)
1204            {
1205               VG_UNDEF(&err,sizeof(err));
1206            }
1207            dec=opus_decoder_create(opus_rates[rate], c, ep);
1208            if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1209            {
1210               __malloc_hook=orig_malloc;
1211               test_failed();
1212            }
1213            cfgs++;
1214            for(app=0;app<3;app++)
1215            {
1216               if(useerr)
1217               {
1218                  VG_UNDEF(&err,sizeof(err));
1219               }
1220               enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
1221               if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1222               {
1223                  __malloc_hook=orig_malloc;
1224                  test_failed();
1225               }
1226               cfgs++;
1227            }
1228         }
1229      }
1230    }
1231    rp=opus_repacketizer_create();
1232    if(rp!=NULL)
1233    {
1234       __malloc_hook=orig_malloc;
1235       test_failed();
1236    }
1237    cfgs++;
1238    __malloc_hook=orig_malloc;
1239    fprintf(stdout,"    opus_decoder_create() ....................... OK.\n");
1240    fprintf(stdout,"    opus_encoder_create() ....................... OK.\n");
1241    fprintf(stdout,"    opus_repacketizer_create() .................. OK.\n");
1242    fprintf(stdout,"                      All malloc failure tests passed\n");
1243    fprintf(stdout,"                                 (%2d API invocations)\n",cfgs);
1244    return cfgs;
1245 #endif
1246 }
1247
1248 int main(int _argc, char **_argv)
1249 {
1250    opus_int32 total;
1251    const char * oversion;
1252    if(_argc>1)
1253    {
1254       fprintf(stderr,"Usage: %s\n",_argv[0]);
1255       return 1;
1256    }
1257    iseed=0;
1258
1259    oversion=opus_get_version_string();
1260    if(!oversion)test_failed();
1261    fprintf(stderr,"Testing the %s API deterministically\n", oversion);
1262    if(opus_strerror(-32768)==NULL)test_failed();
1263    if(opus_strerror(32767)==NULL)test_failed();
1264    if(strlen(opus_strerror(0))<1)test_failed();
1265    total=4;
1266
1267    total+=test_dec_api();
1268    total+=test_parse();
1269    total+=test_enc_api();
1270    total+=test_repacketizer_api();
1271    total+=test_malloc_fail();
1272
1273    fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
1274
1275    return 0;
1276 }