e4afd584f346f4e356506587c6ca78d40690577d
[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    CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
820      1,0,
821      "    OPUS_SET_DTX ................................ OK.\n",
822      "    OPUS_GET_DTX ................................ OK.\n")
823
824    CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
825      0,10,
826      "    OPUS_SET_COMPLEXITY ......................... OK.\n",
827      "    OPUS_GET_COMPLEXITY ......................... OK.\n")
828
829    CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
830      1,0,
831      "    OPUS_SET_INBAND_FEC ......................... OK.\n",
832      "    OPUS_GET_INBAND_FEC ......................... OK.\n")
833
834    CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
835      100,0,
836      "    OPUS_SET_PACKET_LOSS_PERC ................... OK.\n",
837      "    OPUS_GET_PACKET_LOSS_PERC ................... OK.\n")
838
839    CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
840      1,0,
841      "    OPUS_SET_VBR ................................ OK.\n",
842      "    OPUS_GET_VBR ................................ OK.\n")
843
844    CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
845      0,50,
846      "    OPUS_SET_VOICE_RATIO ........................ OK.\n",
847      "    OPUS_GET_VOICE_RATIO ........................ OK.\n")
848
849    CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
850      1,0,
851      "    OPUS_SET_VBR_CONSTRAINT ..................... OK.\n",
852      "    OPUS_GET_VBR_CONSTRAINT ..................... OK.\n")
853
854    CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
855      OPUS_SIGNAL_MUSIC,OPUS_AUTO,
856      "    OPUS_SET_SIGNAL ............................. OK.\n",
857      "    OPUS_GET_SIGNAL ............................. OK.\n")
858
859    /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
860
861    if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
862    cfgs++;
863    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ........................ OK.\n");
864
865    /*Reset the encoder*/
866    if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
867    cfgs++;
868    fprintf(stdout,"    OPUS_RESET_STATE ............................ OK.\n");
869
870    memset(sbuf,0,sizeof(short)*2*960);
871    VG_UNDEF(packet,sizeof(packet));
872    i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
873    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
874    VG_CHECK(packet,i);
875    cfgs++;
876    fprintf(stdout,"    opus_encode() ............................... OK.\n");
877 #ifndef DISABLE_FLOAT_API
878    memset(fbuf,0,sizeof(float)*2*960);
879    VG_UNDEF(packet,sizeof(packet));
880    i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
881    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
882    VG_CHECK(packet,i);
883    cfgs++;
884    fprintf(stdout,"    opus_encode_float() ......................... OK.\n");
885 #endif
886
887 #if 0
888    /*These tests are disabled because the library crashes with null states*/
889    if(opus_encoder_ctl(0,OPUS_RESET_STATE)               !=OPUS_INVALID_STATE)test_failed();
890    if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
891    if(opus_encode(0,sbuf,960,packet,sizeof(packet))      !=OPUS_INVALID_STATE)test_failed();
892    if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
893 #endif
894    opus_encoder_destroy(enc);
895    cfgs++;
896    fprintf(stdout,"                   All encoder interface tests passed\n");
897    fprintf(stdout,"                             (%d API invocations)\n",cfgs);
898    return cfgs;
899 }
900
901 #define max_out (1276*48+48*2+2)
902 int test_repacketizer_api(void)
903 {
904    int ret,cfgs,i,j,k;
905    OpusRepacketizer *rp;
906    unsigned char *packet;
907    unsigned char *po;
908    cfgs=0;
909    fprintf(stdout,"\n  Repacketizer tests\n");
910    fprintf(stdout,dash_head);
911
912    packet=malloc(max_out);
913    if(packet==NULL)test_failed();
914    po=malloc(max_out);
915    if(po==NULL)test_failed();
916    memset(packet,0,max_out);
917
918    i=opus_repacketizer_get_size();
919    if(i<=0)test_failed();
920    cfgs++;
921    fprintf(stdout,"    opus_repacketizer_get_size()=%d ............ OK.\n",i);
922
923    rp=malloc(i);
924    rp=opus_repacketizer_init(rp);
925    if(rp==NULL)test_failed();
926    cfgs++;
927    free(rp);
928    fprintf(stdout,"    opus_repacketizer_init ...................... OK.\n");
929
930    rp=opus_repacketizer_create();
931    if(rp==NULL)test_failed();
932    cfgs++;
933    fprintf(stdout,"    opus_repacketizer_create .................... OK.\n");
934
935    if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
936    cfgs++;
937    fprintf(stdout,"    opus_repacketizer_get_nb_frames ............. OK.\n");
938
939    /*Length overflows*/
940    VG_UNDEF(packet,4);
941    if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
942    cfgs++;
943    packet[0]=1;
944    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
945    cfgs++;
946    packet[0]=2;
947    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
948    cfgs++;
949    packet[0]=3;
950    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
951    cfgs++;
952    packet[0]=2;
953    packet[1]=255;
954    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
955    cfgs++;
956    packet[0]=2;
957    packet[1]=250;
958    if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
959    cfgs++;
960    packet[0]=3;
961    packet[1]=0;
962    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
963    cfgs++;
964    packet[1]=49;
965    if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
966    cfgs++;
967    packet[0]=0;
968    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
969    cfgs++;
970    packet[0]=1<<2;
971    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
972    cfgs++;
973
974    /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
975    opus_repacketizer_init(rp);
976    for(j=0;j<32;j++)
977    {
978       /* TOC types, test half with stereo */
979       int maxi;
980       packet[0]=((j<<1)+(j&1))<<2;
981       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
982       for(i=1;i<=maxi;i++)
983       {
984          /* Number of CBR frames in the input packets */
985          int maxp;
986          packet[0]=((j<<1)+(j&1))<<2;
987          if(i>1)packet[0]+=i==2?1:3;
988          packet[1]=i>2?i:0;
989          maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
990          for(k=0;k<=(1275+75);k+=3)
991          {
992             /*Payload size*/
993             opus_int32 cnt,rcnt;
994             if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
995             for(cnt=0;cnt<maxp+2;cnt++)
996             {
997                if(cnt>0)
998                {
999                   ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
1000                   if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
1001                   cfgs++;
1002                }
1003                rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
1004                if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
1005                cfgs++;
1006                ret=opus_repacketizer_out(rp,po,max_out);
1007                if(rcnt>0)
1008                {
1009                   int len;
1010                   len=k*rcnt+((rcnt*i)>2?2:1);
1011                   if(ret!=len)test_failed();
1012                   if((rcnt*i)<2&&(po[0]&3)!=0)test_failed();                      /* Code 0 */
1013                   if((rcnt*i)==2&&(po[0]&3)!=1)test_failed();                     /* Code 1 */
1014                   if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
1015                   cfgs++;
1016                   if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1017                   cfgs++;
1018                   if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1019                   cfgs++;
1020                   if(len>1)
1021                   {
1022                      if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1023                      cfgs++;
1024                   }
1025                   if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1026                   cfgs++;
1027                } else if (ret!=OPUS_BAD_ARG)test_failed();                        /* M must not be 0 */
1028             }
1029             opus_repacketizer_init(rp);
1030          }
1031       }
1032    }
1033
1034    /*Change in input count code, CBR out*/
1035    opus_repacketizer_init(rp);
1036    packet[0]=0;
1037    if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
1038    cfgs++;
1039    packet[0]+=1;
1040    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1041    cfgs++;
1042    i=opus_repacketizer_out(rp,po,max_out);
1043    if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
1044
1045    /*Change in input count code, VBR out*/
1046    opus_repacketizer_init(rp);
1047    packet[0]=1;
1048    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1049    cfgs++;
1050    packet[0]=0;
1051    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1052    cfgs++;
1053    i=opus_repacketizer_out(rp,po,max_out);
1054    if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
1055
1056    /*VBR in, VBR out*/
1057    opus_repacketizer_init(rp);
1058    packet[0]=2;
1059    packet[1]=4;
1060    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1061    cfgs++;
1062    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1063    cfgs++;
1064    i=opus_repacketizer_out(rp,po,max_out);
1065    if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
1066
1067    /*VBR in, CBR out*/
1068    opus_repacketizer_init(rp);
1069    packet[0]=2;
1070    packet[1]=4;
1071    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1072    cfgs++;
1073    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1074    cfgs++;
1075    i=opus_repacketizer_out(rp,po,max_out);
1076    if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
1077
1078    /*Count 0 in, VBR out*/
1079    for(j=0;j<32;j++)
1080    {
1081       /* TOC types, test half with stereo */
1082       int maxi,sum,rcnt;
1083       packet[0]=((j<<1)+(j&1))<<2;
1084       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1085       sum=0;
1086       rcnt=0;
1087       opus_repacketizer_init(rp);
1088       for(i=1;i<=maxi+2;i++)
1089       {
1090          int len;
1091          ret=opus_repacketizer_cat(rp,packet,i);
1092          if(rcnt<maxi)
1093          {
1094             if(ret!=OPUS_OK)test_failed();
1095             rcnt++;
1096             sum+=i-1;
1097          } else if (ret!=OPUS_INVALID_PACKET)test_failed();
1098          cfgs++;
1099          len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
1100          if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
1101          if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
1102          if(rcnt==2&&(po[0]&3)!=2)test_failed();
1103          if(rcnt==1&&(po[0]&3)!=0)test_failed();
1104          cfgs++;
1105          if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1106          cfgs++;
1107          if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1108          cfgs++;
1109          if(len>1)
1110          {
1111             if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1112             cfgs++;
1113          }
1114          if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1115          cfgs++;
1116       }
1117    }
1118
1119    fprintf(stdout,"    opus_repacketizer_cat ....................... OK.\n");
1120    fprintf(stdout,"    opus_repacketizer_out ....................... OK.\n");
1121
1122    opus_repacketizer_destroy(rp);
1123    cfgs++;
1124    free(packet);
1125    free(po);
1126    fprintf(stdout,"                        All repacketizer tests passed\n");
1127    fprintf(stdout,"                            (%7d API invocations)\n",cfgs);
1128
1129    return cfgs;
1130 }
1131
1132 #ifdef MALLOC_FAIL
1133 typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
1134 #endif
1135
1136 int test_malloc_fail(void)
1137 {
1138 #ifdef MALLOC_FAIL
1139    OpusDecoder *dec;
1140    OpusEncoder *enc;
1141    OpusRepacketizer *rp;
1142    int rate,c,app,cfgs,err,useerr;
1143    int *ep;
1144    mhook orig_malloc;
1145    cfgs=0;
1146 #endif
1147    fprintf(stdout,"\n  malloc() failure tests\n");
1148    fprintf(stdout,dash_head);
1149 #ifdef MALLOC_FAIL
1150    orig_malloc=__malloc_hook;
1151    __malloc_hook=malloc_hook;
1152    ep=(int *)opus_alloc(sizeof(int));
1153    if(ep!=NULL)
1154    {
1155       if(ep)free(ep);
1156       __malloc_hook=orig_malloc;
1157 #endif
1158       fprintf(stdout,"    opus_decoder_create() .................. SKIPPED.\n");
1159       fprintf(stdout,"    opus_encoder_create() .................. SKIPPED.\n");
1160       fprintf(stdout,"    opus_repacketizer_create() ............. SKIPPED.\n");
1161       fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
1162       return 0;
1163 #ifdef MALLOC_FAIL
1164    }
1165    for(useerr=0;useerr<2;useerr++)
1166    {
1167       ep=useerr?&err:0;
1168       for(rate=0;rate<5;rate++)
1169       {
1170         for(c=1;c<3;c++)
1171         {
1172            err=1;
1173            if(useerr)
1174            {
1175               VG_UNDEF(&err,sizeof(err));
1176            }
1177            dec=opus_decoder_create(opus_rates[rate], c, ep);
1178            if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1179            {
1180               __malloc_hook=orig_malloc;
1181               test_failed();
1182            }
1183            cfgs++;
1184            for(app=0;app<3;app++)
1185            {
1186               if(useerr)
1187               {
1188                  VG_UNDEF(&err,sizeof(err));
1189               }
1190               enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
1191               if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1192               {
1193                  __malloc_hook=orig_malloc;
1194                  test_failed();
1195               }
1196               cfgs++;
1197            }
1198         }
1199      }
1200    }
1201    rp=opus_repacketizer_create();
1202    if(rp!=NULL)
1203    {
1204       __malloc_hook=orig_malloc;
1205       test_failed();
1206    }
1207    cfgs++;
1208    __malloc_hook=orig_malloc;
1209    fprintf(stdout,"    opus_decoder_create() ....................... OK.\n");
1210    fprintf(stdout,"    opus_encoder_create() ....................... OK.\n");
1211    fprintf(stdout,"    opus_repacketizer_create() .................. OK.\n");
1212    fprintf(stdout,"                      All malloc failure tests passed\n");
1213    fprintf(stdout,"                                 (%2d API invocations)\n",cfgs);
1214    return cfgs;
1215 #endif
1216 }
1217
1218 int main(int _argc, char **_argv)
1219 {
1220    opus_int32 total;
1221    const char * oversion;
1222    if(_argc>1)
1223    {
1224       fprintf(stderr,"Usage: %s\n",_argv[0]);
1225       return 1;
1226    }
1227    iseed=0;
1228
1229    oversion=opus_get_version_string();
1230    if(!oversion)test_failed();
1231    fprintf(stderr,"Testing the %s API deterministically\n", oversion);
1232    if(opus_strerror(-32768)==NULL)test_failed();
1233    if(opus_strerror(32767)==NULL)test_failed();
1234    if(strlen(opus_strerror(0))<1)test_failed();
1235    total=4;
1236
1237    total+=test_dec_api();
1238    total+=test_parse();
1239    total+=test_enc_api();
1240    total+=test_repacketizer_api();
1241    total+=test_malloc_fail();
1242
1243    fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
1244
1245    return 0;
1246 }