Home | History | Annotate | Download | only in tests
      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 COPYRIGHT OWNER
     19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 */
     27 
     28 /* This tests the API presented by the libopus system.
     29    It does not attempt to extensively exercise the codec internals.
     30    The strategy here is to simply the API interface invariants:
     31    That sane options are accepted, insane options are rejected,
     32    and that nothing blows up. In particular we don't actually test
     33    that settings are heeded by the codec (though we do check that
     34    get after set returns a sane value when it should). Other
     35    tests check the actual codec behavior.
     36    In cases where its reasonable to do so we test exhaustively,
     37    but its not reasonable to do so in all cases.
     38    Although these tests are simple they found several library bugs
     39    when they were initially developed. */
     40 
     41 /* These tests are more sensitive if compiled with -DVALGRIND and
     42    run inside valgrind. Malloc failure testing requires glibc. */
     43 
     44 #ifdef HAVE_CONFIG_H
     45 #include "config.h"
     46 #endif
     47 
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <stdint.h>
     51 #include <string.h>
     52 #include "arch.h"
     53 #include "opus_multistream.h"
     54 #include "opus.h"
     55 #include "test_opus_common.h"
     56 
     57 #ifdef VALGRIND
     58 #include <valgrind/memcheck.h>
     59 #define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
     60 #define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
     61 #else
     62 #define VG_UNDEF(x,y)
     63 #define VG_CHECK(x,y)
     64 #endif
     65 
     66 #if defined(HAVE___MALLOC_HOOK)
     67 #define MALLOC_FAIL
     68 #include "os_support.h"
     69 #include <malloc.h>
     70 
     71 static const opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP,
     72        OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY};
     73 
     74 void *malloc_hook(__attribute__((unused)) size_t size,
     75                   __attribute__((unused)) const void *caller)
     76 {
     77    return 0;
     78 }
     79 #endif
     80 
     81 static const opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};
     82 
     83 opus_int32 test_dec_api(void)
     84 {
     85    opus_uint32 dec_final_range;
     86    OpusDecoder *dec;
     87    OpusDecoder *dec2;
     88    opus_int32 i,j,cfgs;
     89    unsigned char packet[1276];
     90 #ifndef DISABLE_FLOAT_API
     91    float fbuf[960*2];
     92 #endif
     93    short sbuf[960*2];
     94    int c,err;
     95    opus_int32 *nullvalue;
     96    nullvalue=0;
     97 
     98    cfgs=0;
     99    /*First test invalid configurations which should fail*/
    100    fprintf(stdout,"\n  Decoder basic API tests\n");
    101    fprintf(stdout,"  ---------------------------------------------------\n");
    102    for(c=0;c<4;c++)
    103    {
    104       i=opus_decoder_get_size(c);
    105       if(((c==1||c==2)&&(i<=2048||i>1<<16))||((c!=1&&c!=2)&&i!=0))test_failed();
    106       fprintf(stdout,"    opus_decoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
    107       cfgs++;
    108    }
    109 
    110    /*Test with unsupported sample rates*/
    111    for(c=0;c<4;c++)
    112    {
    113       for(i=-7;i<=96000;i++)
    114       {
    115          int fs;
    116          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
    117          switch(i)
    118          {
    119            case(-5):fs=-8000;break;
    120            case(-6):fs=INT32_MAX;break;
    121            case(-7):fs=INT32_MIN;break;
    122            default:fs=i;
    123          }
    124          err = OPUS_OK;
    125          VG_UNDEF(&err,sizeof(err));
    126          dec = opus_decoder_create(fs, c, &err);
    127          if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
    128          cfgs++;
    129          dec = opus_decoder_create(fs, c, 0);
    130          if(dec!=NULL)test_failed();
    131          cfgs++;
    132          dec=malloc(opus_decoder_get_size(2));
    133          if(dec==NULL)test_failed();
    134          err = opus_decoder_init(dec,fs,c);
    135          if(err!=OPUS_BAD_ARG)test_failed();
    136          cfgs++;
    137          free(dec);
    138       }
    139    }
    140 
    141    VG_UNDEF(&err,sizeof(err));
    142    dec = opus_decoder_create(48000, 2, &err);
    143    if(err!=OPUS_OK || dec==NULL)test_failed();
    144    VG_CHECK(dec,opus_decoder_get_size(2));
    145    cfgs++;
    146 
    147    fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
    148    fprintf(stdout,"    opus_decoder_init() .......................... OK.\n");
    149 
    150    VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
    151    err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
    152    if(err!=OPUS_OK)test_failed();
    153    VG_CHECK(&dec_final_range,sizeof(dec_final_range));
    154    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
    155    cfgs++;
    156 
    157    err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
    158    if(err!=OPUS_UNIMPLEMENTED)test_failed();
    159    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
    160    cfgs++;
    161 
    162    VG_UNDEF(&i,sizeof(i));
    163    err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
    164    if(err != OPUS_OK || i!=0)test_failed();
    165    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
    166    cfgs++;
    167 
    168    /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
    169    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
    170    if(err!=OPUS_BAD_ARG)test_failed();
    171    cfgs++;
    172    VG_UNDEF(&i,sizeof(i));
    173    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
    174    if(err != OPUS_OK || i>0 || i<-1)test_failed();
    175    cfgs++;
    176    VG_UNDEF(packet,sizeof(packet));
    177    packet[0]=63<<2;packet[1]=packet[2]=0;
    178    if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
    179    cfgs++;
    180    VG_UNDEF(&i,sizeof(i));
    181    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
    182    if(err != OPUS_OK || i>0 || i<-1)test_failed();
    183    cfgs++;
    184    packet[0]=1;
    185    if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
    186    cfgs++;
    187    VG_UNDEF(&i,sizeof(i));
    188    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
    189    if(err != OPUS_OK || i>0 || i<-1)test_failed();
    190    cfgs++;
    191    fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
    192 
    193    VG_UNDEF(&i,sizeof(i));
    194    err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
    195    VG_CHECK(&i,sizeof(i));
    196    if(err != OPUS_OK || i!=0)test_failed();
    197    cfgs++;
    198    err=opus_decoder_ctl(dec, OPUS_GET_GAIN(nullvalue));
    199    if(err != OPUS_BAD_ARG)test_failed();
    200    cfgs++;
    201    err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-32769));
    202    if(err != OPUS_BAD_ARG)test_failed();
    203    cfgs++;
    204    err=opus_decoder_ctl(dec, OPUS_SET_GAIN(32768));
    205    if(err != OPUS_BAD_ARG)test_failed();
    206    cfgs++;
    207    err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-15));
    208    if(err != OPUS_OK)test_failed();
    209    cfgs++;
    210    VG_UNDEF(&i,sizeof(i));
    211    err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
    212    VG_CHECK(&i,sizeof(i));
    213    if(err != OPUS_OK || i!=-15)test_failed();
    214    cfgs++;
    215    fprintf(stdout,"    OPUS_SET_GAIN ................................ OK.\n");
    216    fprintf(stdout,"    OPUS_GET_GAIN ................................ OK.\n");
    217 
    218    /*Reset the decoder*/
    219    dec2=malloc(opus_decoder_get_size(2));
    220    memcpy(dec2,dec,opus_decoder_get_size(2));
    221    if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
    222    if(memcmp(dec2,dec,opus_decoder_get_size(2))==0)test_failed();
    223    free(dec2);
    224    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
    225    cfgs++;
    226 
    227    VG_UNDEF(packet,sizeof(packet));
    228    packet[0]=0;
    229    if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
    230    if(opus_packet_get_nb_samples(packet,1,48000)!=480)test_failed();
    231    if(opus_packet_get_nb_samples(packet,1,96000)!=960)test_failed();
    232    if(opus_packet_get_nb_samples(packet,1,32000)!=320)test_failed();
    233    if(opus_packet_get_nb_samples(packet,1,8000)!=80)test_failed();
    234    packet[0]=3;
    235    if(opus_packet_get_nb_samples(packet,1,24000)!=OPUS_INVALID_PACKET)test_failed();
    236    packet[0]=(63<<2)|3;
    237    packet[1]=63;
    238    if(opus_packet_get_nb_samples(packet,0,24000)!=OPUS_BAD_ARG)test_failed();
    239    if(opus_packet_get_nb_samples(packet,2,48000)!=OPUS_INVALID_PACKET)test_failed();
    240    if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed();
    241    fprintf(stdout,"    opus_{packet,decoder}_get_nb_samples() ....... OK.\n");
    242    cfgs+=9;
    243 
    244    if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();
    245    for(i=0;i<256;i++) {
    246      int l1res[4]={1,2,2,OPUS_INVALID_PACKET};
    247      packet[0]=i;
    248      if(l1res[packet[0]&3]!=opus_packet_get_nb_frames(packet,1))test_failed();
    249      cfgs++;
    250      for(j=0;j<256;j++) {
    251        packet[1]=j;
    252        if(((packet[0]&3)!=3?l1res[packet[0]&3]:packet[1]&63)!=opus_packet_get_nb_frames(packet,2))test_failed();
    253        cfgs++;
    254      }
    255    }
    256    fprintf(stdout,"    opus_packet_get_nb_frames() .................. OK.\n");
    257 
    258    for(i=0;i<256;i++) {
    259      int bw;
    260      packet[0]=i;
    261      bw=packet[0]>>4;
    262      bw=OPUS_BANDWIDTH_NARROWBAND+(((((bw&7)*9)&(63-(bw&8)))+2+12*((bw&8)!=0))>>4);
    263      if(bw!=opus_packet_get_bandwidth(packet))test_failed();
    264      cfgs++;
    265    }
    266    fprintf(stdout,"    opus_packet_get_bandwidth() .................. OK.\n");
    267 
    268    for(i=0;i<256;i++) {
    269      int fp3s,rate;
    270      packet[0]=i;
    271      fp3s=packet[0]>>3;
    272      fp3s=((((3-(fp3s&3))*13&119)+9)>>2)*((fp3s>13)*(3-((fp3s&3)==3))+1)*25;
    273      for(rate=0;rate<5;rate++) {
    274        if((opus_rates[rate]*3/fp3s)!=opus_packet_get_samples_per_frame(packet,opus_rates[rate]))test_failed();
    275        cfgs++;
    276      }
    277    }
    278    fprintf(stdout,"    opus_packet_get_samples_per_frame() .......... OK.\n");
    279 
    280    packet[0]=(63<<2)+3;
    281    packet[1]=49;
    282    for(j=2;j<51;j++)packet[j]=0;
    283    VG_UNDEF(sbuf,sizeof(sbuf));
    284    if(opus_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
    285    cfgs++;
    286    packet[0]=(63<<2);
    287    packet[1]=packet[2]=0;
    288    if(opus_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG)test_failed();
    289    cfgs++;
    290    if(opus_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
    291    cfgs++;
    292    if(opus_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
    293    cfgs++;
    294    if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
    295    cfgs++;
    296    fprintf(stdout,"    opus_decode() ................................ OK.\n");
    297 #ifndef DISABLE_FLOAT_API
    298    VG_UNDEF(fbuf,sizeof(fbuf));
    299    if(opus_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
    300    cfgs++;
    301    fprintf(stdout,"    opus_decode_float() .......................... OK.\n");
    302 #endif
    303 
    304 #if 0
    305    /*These tests are disabled because the library crashes with null states*/
    306    if(opus_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
    307    if(opus_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
    308    if(opus_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
    309    if(opus_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
    310    if(opus_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
    311    if(opus_packet_get_nb_frames(NULL,1)            !=OPUS_BAD_ARG)test_failed();
    312    if(opus_packet_get_bandwidth(NULL)              !=OPUS_BAD_ARG)test_failed();
    313    if(opus_packet_get_samples_per_frame(NULL,48000)!=OPUS_BAD_ARG)test_failed();
    314 #endif
    315    opus_decoder_destroy(dec);
    316    cfgs++;
    317    fprintf(stdout,"                   All decoder interface tests passed\n");
    318    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
    319    return cfgs;
    320 }
    321 
    322 opus_int32 test_msdec_api(void)
    323 {
    324    opus_uint32 dec_final_range;
    325    OpusMSDecoder *dec;
    326    OpusDecoder *streamdec;
    327    opus_int32 i,j,cfgs;
    328    unsigned char packet[1276];
    329    unsigned char mapping[256];
    330 #ifndef DISABLE_FLOAT_API
    331    float fbuf[960*2];
    332 #endif
    333    short sbuf[960*2];
    334    int a,b,c,err;
    335 #if 0
    336    /*Relevant test not enabled for multistream*/
    337    int *nullvalue;
    338    nullvalue=0;
    339 #endif
    340 
    341    mapping[0]=0;
    342    mapping[1]=1;
    343    for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
    344 
    345    cfgs=0;
    346    /*First test invalid configurations which should fail*/
    347    fprintf(stdout,"\n  Multistream decoder basic API tests\n");
    348    fprintf(stdout,"  ---------------------------------------------------\n");
    349    for(a=-1;a<4;a++)
    350    {
    351       for(b=-1;b<4;b++)
    352       {
    353          i=opus_multistream_decoder_get_size(a,b);
    354          if(((a>0&&b<=a&&b>=0)&&(i<=2048||i>((1<<16)*a)))||((a<1||b>a||b<0)&&i!=0))test_failed();
    355          fprintf(stdout,"    opus_multistream_decoder_get_size(%2d,%2d)=%d %sOK.\n",a,b,i,i>0?"":"... ");
    356          cfgs++;
    357       }
    358    }
    359 
    360    /*Test with unsupported sample rates*/
    361    for(c=1;c<3;c++)
    362    {
    363       for(i=-7;i<=96000;i++)
    364       {
    365          int fs;
    366          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
    367          switch(i)
    368          {
    369            case(-5):fs=-8000;break;
    370            case(-6):fs=INT32_MAX;break;
    371            case(-7):fs=INT32_MIN;break;
    372            default:fs=i;
    373          }
    374          err = OPUS_OK;
    375          VG_UNDEF(&err,sizeof(err));
    376          dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, &err);
    377          if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
    378          cfgs++;
    379          dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, 0);
    380          if(dec!=NULL)test_failed();
    381          cfgs++;
    382          dec=malloc(opus_multistream_decoder_get_size(1,1));
    383          if(dec==NULL)test_failed();
    384          err = opus_multistream_decoder_init(dec,fs,c,1,c-1, mapping);
    385          if(err!=OPUS_BAD_ARG)test_failed();
    386          cfgs++;
    387          free(dec);
    388       }
    389    }
    390 
    391    for(c=0;c<2;c++)
    392    {
    393       int *ret_err;
    394       ret_err = c?0:&err;
    395 
    396       mapping[0]=0;
    397       mapping[1]=1;
    398       for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
    399 
    400       VG_UNDEF(ret_err,sizeof(*ret_err));
    401       dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, ret_err);
    402       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    403       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    404       cfgs++;
    405 
    406       VG_UNDEF(ret_err,sizeof(*ret_err));
    407       mapping[0]=mapping[1]=0;
    408       dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, ret_err);
    409       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    410       if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
    411       cfgs++;
    412       opus_multistream_decoder_destroy(dec);
    413       cfgs++;
    414 
    415       VG_UNDEF(ret_err,sizeof(*ret_err));
    416       dec = opus_multistream_decoder_create(48000, 1, 4, 1, mapping, ret_err);
    417       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    418       if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
    419       cfgs++;
    420 
    421       err = opus_multistream_decoder_init(dec,48000, 1, 0, 0, mapping);
    422       if(err!=OPUS_BAD_ARG)test_failed();
    423       cfgs++;
    424 
    425       err = opus_multistream_decoder_init(dec,48000, 1, 1, -1, mapping);
    426       if(err!=OPUS_BAD_ARG)test_failed();
    427       cfgs++;
    428 
    429       opus_multistream_decoder_destroy(dec);
    430       cfgs++;
    431 
    432       VG_UNDEF(ret_err,sizeof(*ret_err));
    433       dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, ret_err);
    434       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    435       if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
    436       cfgs++;
    437       opus_multistream_decoder_destroy(dec);
    438       cfgs++;
    439 
    440       VG_UNDEF(ret_err,sizeof(*ret_err));
    441       dec = opus_multistream_decoder_create(48000, 255, 255, 1, mapping, ret_err);
    442       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    443       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    444       cfgs++;
    445 
    446       VG_UNDEF(ret_err,sizeof(*ret_err));
    447       dec = opus_multistream_decoder_create(48000, -1, 1, 1, mapping, ret_err);
    448       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    449       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    450       cfgs++;
    451 
    452       VG_UNDEF(ret_err,sizeof(*ret_err));
    453       dec = opus_multistream_decoder_create(48000, 0, 1, 1, mapping, ret_err);
    454       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    455       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    456       cfgs++;
    457 
    458       VG_UNDEF(ret_err,sizeof(*ret_err));
    459       dec = opus_multistream_decoder_create(48000, 1, -1, 2, mapping, ret_err);
    460       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    461       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    462       cfgs++;
    463 
    464       VG_UNDEF(ret_err,sizeof(*ret_err));
    465       dec = opus_multistream_decoder_create(48000, 1, -1, -1, mapping, ret_err);
    466       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    467       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    468       cfgs++;
    469 
    470       VG_UNDEF(ret_err,sizeof(*ret_err));
    471       dec = opus_multistream_decoder_create(48000, 256, 255, 1, mapping, ret_err);
    472       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    473       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    474       cfgs++;
    475 
    476       VG_UNDEF(ret_err,sizeof(*ret_err));
    477       dec = opus_multistream_decoder_create(48000, 256, 255, 0, mapping, ret_err);
    478       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    479       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    480       cfgs++;
    481 
    482       VG_UNDEF(ret_err,sizeof(*ret_err));
    483       mapping[0]=255;
    484       mapping[1]=1;
    485       mapping[2]=2;
    486       dec = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, ret_err);
    487       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    488       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    489       cfgs++;
    490 
    491       VG_UNDEF(ret_err,sizeof(*ret_err));
    492       mapping[0]=0;
    493       mapping[1]=0;
    494       mapping[2]=0;
    495       dec = opus_multistream_decoder_create(48000, 3, 2, 1, mapping, ret_err);
    496       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    497       if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
    498       cfgs++;
    499       opus_multistream_decoder_destroy(dec);
    500       cfgs++;
    501 
    502       VG_UNDEF(ret_err,sizeof(*ret_err));
    503       mapping[0]=0;
    504       mapping[1]=255;
    505       mapping[2]=1;
    506       mapping[3]=2;
    507       mapping[4]=3;
    508       dec = opus_multistream_decoder_create(48001, 5, 4, 1, mapping, ret_err);
    509       if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
    510       if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
    511       cfgs++;
    512    }
    513 
    514    VG_UNDEF(&err,sizeof(err));
    515    mapping[0]=0;
    516    mapping[1]=255;
    517    mapping[2]=1;
    518    mapping[3]=2;
    519    dec = opus_multistream_decoder_create(48000, 4, 2, 1, mapping, &err);
    520    VG_CHECK(&err,sizeof(err));
    521    if(err!=OPUS_OK || dec==NULL)test_failed();
    522    cfgs++;
    523 
    524    fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
    525    fprintf(stdout,"    opus_multistream_decoder_init() .............. OK.\n");
    526 
    527    VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
    528    err=opus_multistream_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
    529    if(err!=OPUS_OK)test_failed();
    530    VG_CHECK(&dec_final_range,sizeof(dec_final_range));
    531    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
    532    cfgs++;
    533 
    534    streamdec=0;
    535    VG_UNDEF(&streamdec,sizeof(streamdec));
    536    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(-1,&streamdec));
    537    if(err!=OPUS_BAD_ARG)test_failed();
    538    cfgs++;
    539    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(1,&streamdec));
    540    if(err!=OPUS_OK||streamdec==NULL)test_failed();
    541    VG_CHECK(streamdec,opus_decoder_get_size(1));
    542    cfgs++;
    543    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(2,&streamdec));
    544    if(err!=OPUS_BAD_ARG)test_failed();
    545    cfgs++;
    546    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(0,&streamdec));
    547    if(err!=OPUS_OK||streamdec==NULL)test_failed();
    548    VG_CHECK(streamdec,opus_decoder_get_size(1));
    549    fprintf(stdout,"    OPUS_MULTISTREAM_GET_DECODER_STATE ........... OK.\n");
    550    cfgs++;
    551 
    552    for(j=0;j<2;j++)
    553    {
    554       OpusDecoder *od;
    555       err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
    556       if(err != OPUS_OK)test_failed();
    557       VG_UNDEF(&i,sizeof(i));
    558       err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
    559       VG_CHECK(&i,sizeof(i));
    560       if(err != OPUS_OK || i!=0)test_failed();
    561       cfgs++;
    562    }
    563    err=opus_multistream_decoder_ctl(dec,OPUS_SET_GAIN(15));
    564    if(err!=OPUS_OK)test_failed();
    565    fprintf(stdout,"    OPUS_SET_GAIN ................................ OK.\n");
    566    for(j=0;j<2;j++)
    567    {
    568       OpusDecoder *od;
    569       err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
    570       if(err != OPUS_OK)test_failed();
    571       VG_UNDEF(&i,sizeof(i));
    572       err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
    573       VG_CHECK(&i,sizeof(i));
    574       if(err != OPUS_OK || i!=15)test_failed();
    575       cfgs++;
    576    }
    577    fprintf(stdout,"    OPUS_GET_GAIN ................................ OK.\n");
    578 
    579    VG_UNDEF(&i,sizeof(i));
    580    err=opus_multistream_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
    581    if(err != OPUS_OK || i!=0)test_failed();
    582    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
    583    cfgs++;
    584 
    585    err=opus_multistream_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
    586    if(err!=OPUS_UNIMPLEMENTED)test_failed();
    587    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
    588    cfgs++;
    589 
    590 #if 0
    591    /*Currently unimplemented for multistream*/
    592    /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
    593    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
    594    if(err!=OPUS_BAD_ARG)test_failed();
    595    cfgs++;
    596    VG_UNDEF(&i,sizeof(i));
    597    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
    598    if(err != OPUS_OK || i>0 || i<-1)test_failed();
    599    cfgs++;
    600    VG_UNDEF(packet,sizeof(packet));
    601    packet[0]=63<<2;packet[1]=packet[2]=0;
    602    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
    603    cfgs++;
    604    VG_UNDEF(&i,sizeof(i));
    605    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
    606    if(err != OPUS_OK || i>0 || i<-1)test_failed();
    607    cfgs++;
    608    packet[0]=1;
    609    if(opus_multistream_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
    610    cfgs++;
    611    VG_UNDEF(&i,sizeof(i));
    612    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
    613    if(err != OPUS_OK || i>0 || i<-1)test_failed();
    614    cfgs++;
    615    fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
    616 #endif
    617 
    618    /*Reset the decoder*/
    619    if(opus_multistream_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
    620    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
    621    cfgs++;
    622 
    623    opus_multistream_decoder_destroy(dec);
    624    cfgs++;
    625    VG_UNDEF(&err,sizeof(err));
    626    dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
    627    if(err!=OPUS_OK || dec==NULL)test_failed();
    628    cfgs++;
    629 
    630    packet[0]=(63<<2)+3;
    631    packet[1]=49;
    632    for(j=2;j<51;j++)packet[j]=0;
    633    VG_UNDEF(sbuf,sizeof(sbuf));
    634    if(opus_multistream_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
    635    cfgs++;
    636    packet[0]=(63<<2);
    637    packet[1]=packet[2]=0;
    638    if(opus_multistream_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG){printf("%d\n",opus_multistream_decode(dec, packet, -1, sbuf, 960, 0));test_failed();}
    639    cfgs++;
    640    if(opus_multistream_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
    641    cfgs++;
    642    if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
    643    cfgs++;
    644    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
    645    cfgs++;
    646    fprintf(stdout,"    opus_multistream_decode() .................... OK.\n");
    647 #ifndef DISABLE_FLOAT_API
    648    VG_UNDEF(fbuf,sizeof(fbuf));
    649    if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
    650    cfgs++;
    651    fprintf(stdout,"    opus_multistream_decode_float() .............. OK.\n");
    652 #endif
    653 
    654 #if 0
    655    /*These tests are disabled because the library crashes with null states*/
    656    if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
    657    if(opus_multistream_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
    658    if(opus_multistream_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
    659    if(opus_multistream_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
    660    if(opus_multistream_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
    661 #endif
    662    opus_multistream_decoder_destroy(dec);
    663    cfgs++;
    664    fprintf(stdout,"       All multistream decoder interface tests passed\n");
    665    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
    666    return cfgs;
    667 }
    668 
    669 #ifdef VALGRIND
    670 #define UNDEFINE_FOR_PARSE  toc=-1; \
    671    frames[0]=(unsigned char *)0; \
    672    frames[1]=(unsigned char *)0; \
    673    payload_offset=-1; \
    674    VG_UNDEF(&toc,sizeof(toc)); \
    675    VG_UNDEF(frames,sizeof(frames));\
    676    VG_UNDEF(&payload_offset,sizeof(payload_offset));
    677 #else
    678 #define UNDEFINE_FOR_PARSE  toc=-1; \
    679    frames[0]=(unsigned char *)0; \
    680    frames[1]=(unsigned char *)0; \
    681    payload_offset=-1;
    682 #endif
    683 
    684 /* This test exercises the heck out of the libopus parser.
    685    It is much larger than the parser itself in part because
    686    it tries to hit a lot of corner cases that could never
    687    fail with the libopus code, but might be problematic for
    688    other implementations. */
    689 opus_int32 test_parse(void)
    690 {
    691    opus_int32 i,j,jj,sz;
    692    unsigned char packet[1276];
    693    opus_int32 cfgs,cfgs_total;
    694    unsigned char toc;
    695    const unsigned char *frames[48];
    696    short size[48];
    697    int payload_offset, ret;
    698    fprintf(stdout,"\n  Packet header parsing tests\n");
    699    fprintf(stdout,"  ---------------------------------------------------\n");
    700    memset(packet,0,sizeof(char)*1276);
    701    packet[0]=63<<2;
    702    if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
    703    cfgs_total=cfgs=1;
    704    /*code 0*/
    705    for(i=0;i<64;i++)
    706    {
    707       UNDEFINE_FOR_PARSE
    708       ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
    709       cfgs++;
    710       if(ret!=1)test_failed();
    711       if(size[0]!=3)test_failed();
    712       if(frames[0]!=packet+1)test_failed();
    713    }
    714    fprintf(stdout,"    code 0 (%2d cases) ............................ OK.\n",cfgs);
    715    cfgs_total+=cfgs;cfgs=0;
    716 
    717    /*code 1, two frames of the same size*/
    718    for(i=0;i<64;i++)
    719    {
    720       packet[0]=(i<<2)+1;
    721       for(jj=0;jj<=1275*2+3;jj++)
    722       {
    723          UNDEFINE_FOR_PARSE
    724          ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
    725          cfgs++;
    726          if((jj&1)==1 && jj<=2551)
    727          {
    728             /* Must pass if payload length even (packet length odd) and
    729                size<=2551, must fail otherwise. */
    730             if(ret!=2)test_failed();
    731             if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
    732             if(frames[0]!=packet+1)test_failed();
    733             if(frames[1]!=frames[0]+size[0])test_failed();
    734             if((toc>>2)!=i)test_failed();
    735          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
    736       }
    737    }
    738    fprintf(stdout,"    code 1 (%6d cases) ........................ OK.\n",cfgs);
    739    cfgs_total+=cfgs;cfgs=0;
    740 
    741    for(i=0;i<64;i++)
    742    {
    743       /*code 2, length code overflow*/
    744       packet[0]=(i<<2)+2;
    745       UNDEFINE_FOR_PARSE
    746       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
    747       cfgs++;
    748       if(ret!=OPUS_INVALID_PACKET)test_failed();
    749       packet[1]=252;
    750       UNDEFINE_FOR_PARSE
    751       ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
    752       cfgs++;
    753       if(ret!=OPUS_INVALID_PACKET)test_failed();
    754       for(j=0;j<1275;j++)
    755       {
    756          if(j<252)packet[1]=j;
    757          else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
    758          /*Code 2, one too short*/
    759          UNDEFINE_FOR_PARSE
    760          ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
    761          cfgs++;
    762          if(ret!=OPUS_INVALID_PACKET)test_failed();
    763          /*Code 2, one too long*/
    764          UNDEFINE_FOR_PARSE
    765          ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
    766          cfgs++;
    767          if(ret!=OPUS_INVALID_PACKET)test_failed();
    768          /*Code 2, second zero*/
    769          UNDEFINE_FOR_PARSE
    770          ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
    771          cfgs++;
    772          if(ret!=2)test_failed();
    773          if(size[0]!=j||size[1]!=0)test_failed();
    774          if(frames[1]!=frames[0]+size[0])test_failed();
    775          if((toc>>2)!=i)test_failed();
    776          /*Code 2, normal*/
    777          UNDEFINE_FOR_PARSE
    778          ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
    779          cfgs++;
    780          if(ret!=2)test_failed();
    781          if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
    782          if(frames[1]!=frames[0]+size[0])test_failed();
    783          if((toc>>2)!=i)test_failed();
    784       }
    785    }
    786    fprintf(stdout,"    code 2 (%6d cases) ........................ OK.\n",cfgs);
    787    cfgs_total+=cfgs;cfgs=0;
    788 
    789    for(i=0;i<64;i++)
    790    {
    791       packet[0]=(i<<2)+3;
    792       /*code 3, length code overflow*/
    793       UNDEFINE_FOR_PARSE
    794       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
    795       cfgs++;
    796       if(ret!=OPUS_INVALID_PACKET)test_failed();
    797    }
    798    fprintf(stdout,"    code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
    799    cfgs_total+=cfgs;cfgs=0;
    800 
    801    for(i=0;i<64;i++)
    802    {
    803       /*code 3, m is zero or 49-63*/
    804       packet[0]=(i<<2)+3;
    805       for(jj=49;jj<=64;jj++)
    806       {
    807         packet[1]=0+(jj&63); /*CBR, no padding*/
    808         UNDEFINE_FOR_PARSE
    809         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
    810         cfgs++;
    811         if(ret!=OPUS_INVALID_PACKET)test_failed();
    812         packet[1]=128+(jj&63); /*VBR, no padding*/
    813         UNDEFINE_FOR_PARSE
    814         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
    815         cfgs++;
    816         if(ret!=OPUS_INVALID_PACKET)test_failed();
    817         packet[1]=64+(jj&63); /*CBR, padding*/
    818         UNDEFINE_FOR_PARSE
    819         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
    820         cfgs++;
    821         if(ret!=OPUS_INVALID_PACKET)test_failed();
    822         packet[1]=128+64+(jj&63); /*VBR, padding*/
    823         UNDEFINE_FOR_PARSE
    824         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
    825         cfgs++;
    826         if(ret!=OPUS_INVALID_PACKET)test_failed();
    827       }
    828    }
    829    fprintf(stdout,"    code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
    830    cfgs_total+=cfgs;cfgs=0;
    831 
    832    for(i=0;i<64;i++)
    833    {
    834       packet[0]=(i<<2)+3;
    835       /*code 3, m is one, cbr*/
    836       packet[1]=1;
    837       for(j=0;j<1276;j++)
    838       {
    839         UNDEFINE_FOR_PARSE
    840         ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
    841         cfgs++;
    842         if(ret!=1)test_failed();
    843         if(size[0]!=j)test_failed();
    844         if((toc>>2)!=i)test_failed();
    845       }
    846       UNDEFINE_FOR_PARSE
    847       ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
    848       cfgs++;
    849       if(ret!=OPUS_INVALID_PACKET)test_failed();
    850    }
    851    fprintf(stdout,"    code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
    852    cfgs_total+=cfgs;cfgs=0;
    853 
    854    for(i=0;i<64;i++)
    855    {
    856       int frame_samp;
    857       /*code 3, m>1 CBR*/
    858       packet[0]=(i<<2)+3;
    859       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
    860       for(j=2;j<49;j++)
    861       {
    862          packet[1]=j;
    863          for(sz=2;sz<((j+2)*1275);sz++)
    864          {
    865             UNDEFINE_FOR_PARSE
    866             ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
    867             cfgs++;
    868             /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
    869             if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
    870             {
    871                if(ret!=j)test_failed();
    872                for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
    873                if((toc>>2)!=i)test_failed();
    874             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
    875          }
    876       }
    877       /*Super jumbo packets*/
    878       packet[1]=5760/frame_samp;
    879       UNDEFINE_FOR_PARSE
    880       ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
    881       cfgs++;
    882       if(ret!=packet[1])test_failed();
    883       for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
    884    }
    885    fprintf(stdout,"    code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
    886    cfgs_total+=cfgs;cfgs=0;
    887 
    888    for(i=0;i<64;i++)
    889    {
    890       int frame_samp;
    891       /*Code 3 VBR, m one*/
    892       packet[0]=(i<<2)+3;
    893       packet[1]=128+1;
    894       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
    895       for(jj=0;jj<1276;jj++)
    896       {
    897          UNDEFINE_FOR_PARSE
    898          ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
    899          cfgs++;
    900          if(ret!=1)test_failed();
    901          if(size[0]!=jj)test_failed();
    902          if((toc>>2)!=i)test_failed();
    903       }
    904       UNDEFINE_FOR_PARSE
    905       ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
    906       cfgs++;
    907       if(ret!=OPUS_INVALID_PACKET)test_failed();
    908       for(j=2;j<49;j++)
    909       {
    910          packet[1]=128+j;
    911          /*Length code overflow*/
    912          UNDEFINE_FOR_PARSE
    913          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
    914          cfgs++;
    915          if(ret!=OPUS_INVALID_PACKET)test_failed();
    916          packet[2]=252;
    917          packet[3]=0;
    918          for(jj=4;jj<2+j;jj++)packet[jj]=0;
    919          UNDEFINE_FOR_PARSE
    920          ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
    921          cfgs++;
    922          if(ret!=OPUS_INVALID_PACKET)test_failed();
    923          /*One byte too short*/
    924          for(jj=2;jj<2+j;jj++)packet[jj]=0;
    925          UNDEFINE_FOR_PARSE
    926          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
    927          cfgs++;
    928          if(ret!=OPUS_INVALID_PACKET)test_failed();
    929          /*One byte too short thanks to length coding*/
    930          packet[2]=252;
    931          packet[3]=0;
    932          for(jj=4;jj<2+j;jj++)packet[jj]=0;
    933          UNDEFINE_FOR_PARSE
    934          ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
    935          cfgs++;
    936          if(ret!=OPUS_INVALID_PACKET)test_failed();
    937          /*Most expensive way of coding zeros*/
    938          for(jj=2;jj<2+j;jj++)packet[jj]=0;
    939          UNDEFINE_FOR_PARSE
    940          ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
    941          cfgs++;
    942          if(frame_samp*j<=5760){
    943             if(ret!=j)test_failed();
    944             for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
    945             if((toc>>2)!=i)test_failed();
    946          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
    947          /*Quasi-CBR use of mode 3*/
    948          for(sz=0;sz<8;sz++)
    949          {
    950             const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
    951             int pos=0;
    952             int as=(tsz[sz]+i-j-2)/j;
    953             for(jj=0;jj<j-1;jj++)
    954             {
    955               if(as<252){packet[2+pos]=as;pos++;}
    956               else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
    957             }
    958             UNDEFINE_FOR_PARSE
    959             ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
    960             cfgs++;
    961             if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
    962                if(ret!=j)test_failed();
    963                for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
    964                if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
    965                if((toc>>2)!=i)test_failed();
    966             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
    967          }
    968       }
    969    }
    970    fprintf(stdout,"    code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
    971    cfgs_total+=cfgs;cfgs=0;
    972 
    973    for(i=0;i<64;i++)
    974    {
    975       packet[0]=(i<<2)+3;
    976       /*Padding*/
    977       packet[1]=128+1+64;
    978       /*Overflow the length coding*/
    979       for(jj=2;jj<127;jj++)packet[jj]=255;
    980       UNDEFINE_FOR_PARSE
    981       ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
    982       cfgs++;
    983       if(ret!=OPUS_INVALID_PACKET)test_failed();
    984 
    985       for(sz=0;sz<4;sz++)
    986       {
    987          const int tsz[4]={0,72,512,1275};
    988          for(jj=sz;jj<65025;jj+=11)
    989          {
    990             int pos;
    991             for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
    992             packet[2+pos]=jj%254;
    993             pos++;
    994             if(sz==0&&i==63)
    995             {
    996                /*Code more padding than there is room in the packet*/
    997                UNDEFINE_FOR_PARSE
    998                ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
    999                cfgs++;
   1000                if(ret!=OPUS_INVALID_PACKET)test_failed();
   1001             }
   1002             UNDEFINE_FOR_PARSE
   1003             ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
   1004             cfgs++;
   1005             if(tsz[sz]+i<1276)
   1006             {
   1007                if(ret!=1)test_failed();
   1008                if(size[0]!=tsz[sz]+i)test_failed();
   1009                if((toc>>2)!=i)test_failed();
   1010             } else if (ret!=OPUS_INVALID_PACKET)test_failed();
   1011          }
   1012       }
   1013    }
   1014    fprintf(stdout,"    code 3 padding (%2d cases) ............... OK.\n",cfgs);
   1015    cfgs_total+=cfgs;cfgs=0;
   1016    fprintf(stdout,"    opus_packet_parse ............................ OK.\n");
   1017    fprintf(stdout,"                      All packet parsing tests passed\n");
   1018    fprintf(stdout,"                          (%d API invocations)\n",cfgs_total);
   1019    return cfgs_total;
   1020 }
   1021 
   1022 /* This is a helper macro for the encoder tests.
   1023    The encoder api tests all have a pattern of set-must-fail, set-must-fail,
   1024    set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
   1025 #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
   1026    i=(badv);\
   1027    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
   1028    i=(badv2);\
   1029    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
   1030    j=i=(goodv);\
   1031    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
   1032    i=-12345;\
   1033    VG_UNDEF(&i,sizeof(i)); \
   1034    err=opus_encoder_ctl(enc,getcall);\
   1035    if(err!=OPUS_OK || i!=j)test_failed();\
   1036    j=i=(goodv2);\
   1037    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
   1038    fprintf(stdout,sok);\
   1039    i=-12345;\
   1040    VG_UNDEF(&i,sizeof(i)); \
   1041    err=opus_encoder_ctl(enc,getcall);\
   1042    if(err!=OPUS_OK || i!=j)test_failed();\
   1043    fprintf(stdout,gok);\
   1044    cfgs+=6;
   1045 
   1046 opus_int32 test_enc_api(void)
   1047 {
   1048    opus_uint32 enc_final_range;
   1049    OpusEncoder *enc;
   1050    opus_int32 i,j;
   1051    unsigned char packet[1276];
   1052 #ifndef DISABLE_FLOAT_API
   1053    float fbuf[960*2];
   1054 #endif
   1055    short sbuf[960*2];
   1056    int c,err,cfgs;
   1057 
   1058    cfgs=0;
   1059    /*First test invalid configurations which should fail*/
   1060    fprintf(stdout,"\n  Encoder basic API tests\n");
   1061    fprintf(stdout,"  ---------------------------------------------------\n");
   1062    for(c=0;c<4;c++)
   1063    {
   1064       i=opus_encoder_get_size(c);
   1065       if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
   1066       fprintf(stdout,"    opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
   1067       cfgs++;
   1068    }
   1069 
   1070    /*Test with unsupported sample rates, channel counts*/
   1071    for(c=0;c<4;c++)
   1072    {
   1073       for(i=-7;i<=96000;i++)
   1074       {
   1075          int fs;
   1076          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
   1077          switch(i)
   1078          {
   1079            case(-5):fs=-8000;break;
   1080            case(-6):fs=INT32_MAX;break;
   1081            case(-7):fs=INT32_MIN;break;
   1082            default:fs=i;
   1083          }
   1084          err = OPUS_OK;
   1085          VG_UNDEF(&err,sizeof(err));
   1086          enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
   1087          if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
   1088          cfgs++;
   1089          enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, 0);
   1090          if(enc!=NULL)test_failed();
   1091          cfgs++;
   1092          opus_encoder_destroy(enc);
   1093          enc=malloc(opus_encoder_get_size(2));
   1094          if(enc==NULL)test_failed();
   1095          err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
   1096          if(err!=OPUS_BAD_ARG)test_failed();
   1097          cfgs++;
   1098          free(enc);
   1099       }
   1100    }
   1101 
   1102    enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
   1103    if(enc!=NULL)test_failed();
   1104    cfgs++;
   1105 
   1106    VG_UNDEF(&err,sizeof(err));
   1107    enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
   1108    if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
   1109    cfgs++;
   1110 
   1111    VG_UNDEF(&err,sizeof(err));
   1112    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
   1113    if(enc==NULL)test_failed();
   1114    opus_encoder_destroy(enc);
   1115    cfgs++;
   1116 
   1117    VG_UNDEF(&err,sizeof(err));
   1118    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
   1119    if(err!=OPUS_OK || enc==NULL)test_failed();
   1120    cfgs++;
   1121    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
   1122    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
   1123    opus_encoder_destroy(enc);
   1124    cfgs++;
   1125 
   1126    VG_UNDEF(&err,sizeof(err));
   1127    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
   1128    if(err!=OPUS_OK || enc==NULL)test_failed();
   1129    cfgs++;
   1130    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
   1131    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
   1132    opus_encoder_destroy(enc);
   1133    cfgs++;
   1134 
   1135    VG_UNDEF(&err,sizeof(err));
   1136    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
   1137    if(err!=OPUS_OK || enc==NULL)test_failed();
   1138    cfgs++;
   1139 
   1140    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
   1141    fprintf(stdout,"    opus_encoder_init() .......................... OK.\n");
   1142 
   1143    i=-12345;
   1144    VG_UNDEF(&i,sizeof(i));
   1145    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
   1146    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
   1147    cfgs++;
   1148    fprintf(stdout,"    OPUS_GET_LOOKAHEAD ........................... OK.\n");
   1149 
   1150    if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
   1151    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
   1152    cfgs++;
   1153 
   1154    CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
   1155      OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
   1156      "    OPUS_SET_APPLICATION ......................... OK.\n",
   1157      "    OPUS_GET_APPLICATION ......................... OK.\n")
   1158 
   1159    if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
   1160    cfgs++;
   1161    VG_UNDEF(&i,sizeof(i));
   1162    if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
   1163    if(i>700000||i<256000)test_failed();
   1164    cfgs++;
   1165    CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
   1166      500,256000,
   1167      "    OPUS_SET_BITRATE ............................. OK.\n",
   1168      "    OPUS_GET_BITRATE ............................. OK.\n")
   1169 
   1170    CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
   1171      1,OPUS_AUTO,
   1172      "    OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
   1173      "    OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
   1174 
   1175    i=-2;
   1176    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
   1177    cfgs++;
   1178    i=OPUS_BANDWIDTH_FULLBAND+1;
   1179    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
   1180    cfgs++;
   1181    i=OPUS_BANDWIDTH_NARROWBAND;
   1182    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
   1183    cfgs++;
   1184    i=OPUS_BANDWIDTH_FULLBAND;
   1185    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
   1186    cfgs++;
   1187    i=OPUS_BANDWIDTH_WIDEBAND;
   1188    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
   1189    cfgs++;
   1190    i=OPUS_BANDWIDTH_MEDIUMBAND;
   1191    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
   1192    cfgs++;
   1193    fprintf(stdout,"    OPUS_SET_BANDWIDTH ........................... OK.\n");
   1194    /*We don't test if the bandwidth has actually changed.
   1195      because the change may be delayed until the encoder is advanced.*/
   1196    i=-12345;
   1197    VG_UNDEF(&i,sizeof(i));
   1198    err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
   1199    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
   1200       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
   1201       i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
   1202    cfgs++;
   1203    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
   1204    cfgs++;
   1205    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
   1206 
   1207    i=-2;
   1208    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
   1209    cfgs++;
   1210    i=OPUS_BANDWIDTH_FULLBAND+1;
   1211    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
   1212    cfgs++;
   1213    i=OPUS_BANDWIDTH_NARROWBAND;
   1214    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
   1215    cfgs++;
   1216    i=OPUS_BANDWIDTH_FULLBAND;
   1217    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
   1218    cfgs++;
   1219    i=OPUS_BANDWIDTH_WIDEBAND;
   1220    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
   1221    cfgs++;
   1222    i=OPUS_BANDWIDTH_MEDIUMBAND;
   1223    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
   1224    cfgs++;
   1225    fprintf(stdout,"    OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
   1226    /*We don't test if the bandwidth has actually changed.
   1227      because the change may be delayed until the encoder is advanced.*/
   1228    i=-12345;
   1229    VG_UNDEF(&i,sizeof(i));
   1230    err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
   1231    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
   1232       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
   1233       i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
   1234    cfgs++;
   1235    fprintf(stdout,"    OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
   1236 
   1237    CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
   1238      1,0,
   1239      "    OPUS_SET_DTX ................................. OK.\n",
   1240      "    OPUS_GET_DTX ................................. OK.\n")
   1241 
   1242    CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
   1243      0,10,
   1244      "    OPUS_SET_COMPLEXITY .......................... OK.\n",
   1245      "    OPUS_GET_COMPLEXITY .......................... OK.\n")
   1246 
   1247    CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
   1248      1,0,
   1249      "    OPUS_SET_INBAND_FEC .......................... OK.\n",
   1250      "    OPUS_GET_INBAND_FEC .......................... OK.\n")
   1251 
   1252    CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
   1253      100,0,
   1254      "    OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
   1255      "    OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
   1256 
   1257    CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
   1258      1,0,
   1259      "    OPUS_SET_VBR ................................. OK.\n",
   1260      "    OPUS_GET_VBR ................................. OK.\n")
   1261 
   1262    /*CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
   1263      0,50,
   1264      "    OPUS_SET_VOICE_RATIO ......................... OK.\n",
   1265      "    OPUS_GET_VOICE_RATIO ......................... OK.\n")
   1266    */
   1267 
   1268    CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
   1269      1,0,
   1270      "    OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
   1271      "    OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
   1272 
   1273    CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
   1274      OPUS_SIGNAL_MUSIC,OPUS_AUTO,
   1275      "    OPUS_SET_SIGNAL .............................. OK.\n",
   1276      "    OPUS_GET_SIGNAL .............................. OK.\n")
   1277 
   1278    CHECK_SETGET(OPUS_SET_LSB_DEPTH(i),OPUS_GET_LSB_DEPTH(&i),7,25,16,24,
   1279      "    OPUS_SET_LSB_DEPTH ........................... OK.\n",
   1280      "    OPUS_GET_LSB_DEPTH ........................... OK.\n")
   1281 
   1282    /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
   1283 
   1284    if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
   1285    cfgs++;
   1286    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
   1287 
   1288    /*Reset the encoder*/
   1289    if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
   1290    cfgs++;
   1291    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
   1292 
   1293    memset(sbuf,0,sizeof(short)*2*960);
   1294    VG_UNDEF(packet,sizeof(packet));
   1295    i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
   1296    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
   1297    VG_CHECK(packet,i);
   1298    cfgs++;
   1299    fprintf(stdout,"    opus_encode() ................................ OK.\n");
   1300 #ifndef DISABLE_FLOAT_API
   1301    memset(fbuf,0,sizeof(float)*2*960);
   1302    VG_UNDEF(packet,sizeof(packet));
   1303    i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
   1304    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
   1305    VG_CHECK(packet,i);
   1306    cfgs++;
   1307    fprintf(stdout,"    opus_encode_float() .......................... OK.\n");
   1308 #endif
   1309 
   1310 #if 0
   1311    /*These tests are disabled because the library crashes with null states*/
   1312    if(opus_encoder_ctl(0,OPUS_RESET_STATE)               !=OPUS_INVALID_STATE)test_failed();
   1313    if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
   1314    if(opus_encode(0,sbuf,960,packet,sizeof(packet))      !=OPUS_INVALID_STATE)test_failed();
   1315    if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
   1316 #endif
   1317    opus_encoder_destroy(enc);
   1318    cfgs++;
   1319    fprintf(stdout,"                   All encoder interface tests passed\n");
   1320    fprintf(stdout,"                             (%d API invocations)\n",cfgs);
   1321    return cfgs;
   1322 }
   1323 
   1324 #define max_out (1276*48+48*2+2)
   1325 int test_repacketizer_api(void)
   1326 {
   1327    int ret,cfgs,i,j,k;
   1328    OpusRepacketizer *rp;
   1329    unsigned char *packet;
   1330    unsigned char *po;
   1331    cfgs=0;
   1332    fprintf(stdout,"\n  Repacketizer tests\n");
   1333    fprintf(stdout,"  ---------------------------------------------------\n");
   1334 
   1335    packet=malloc(max_out);
   1336    if(packet==NULL)test_failed();
   1337    po=malloc(max_out);
   1338    if(po==NULL)test_failed();
   1339    memset(packet,0,max_out);
   1340 
   1341    i=opus_repacketizer_get_size();
   1342    if(i<=0)test_failed();
   1343    cfgs++;
   1344    fprintf(stdout,"    opus_repacketizer_get_size()=%d ............. OK.\n",i);
   1345 
   1346    rp=malloc(i);
   1347    rp=opus_repacketizer_init(rp);
   1348    if(rp==NULL)test_failed();
   1349    cfgs++;
   1350    free(rp);
   1351    fprintf(stdout,"    opus_repacketizer_init ....................... OK.\n");
   1352 
   1353    rp=opus_repacketizer_create();
   1354    if(rp==NULL)test_failed();
   1355    cfgs++;
   1356    fprintf(stdout,"    opus_repacketizer_create ..................... OK.\n");
   1357 
   1358    if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
   1359    cfgs++;
   1360    fprintf(stdout,"    opus_repacketizer_get_nb_frames .............. OK.\n");
   1361 
   1362    /*Length overflows*/
   1363    VG_UNDEF(packet,4);
   1364    if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
   1365    cfgs++;
   1366    packet[0]=1;
   1367    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
   1368    cfgs++;
   1369    packet[0]=2;
   1370    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
   1371    cfgs++;
   1372    packet[0]=3;
   1373    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
   1374    cfgs++;
   1375    packet[0]=2;
   1376    packet[1]=255;
   1377    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
   1378    cfgs++;
   1379    packet[0]=2;
   1380    packet[1]=250;
   1381    if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
   1382    cfgs++;
   1383    packet[0]=3;
   1384    packet[1]=0;
   1385    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
   1386    cfgs++;
   1387    packet[1]=49;
   1388    if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
   1389    cfgs++;
   1390    packet[0]=0;
   1391    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
   1392    cfgs++;
   1393    packet[0]=1<<2;
   1394    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
   1395    cfgs++;
   1396 
   1397    /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
   1398    opus_repacketizer_init(rp);
   1399    for(j=0;j<32;j++)
   1400    {
   1401       /* TOC types, test half with stereo */
   1402       int maxi;
   1403       packet[0]=((j<<1)+(j&1))<<2;
   1404       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
   1405       for(i=1;i<=maxi;i++)
   1406       {
   1407          /* Number of CBR frames in the input packets */
   1408          int maxp;
   1409          packet[0]=((j<<1)+(j&1))<<2;
   1410          if(i>1)packet[0]+=i==2?1:3;
   1411          packet[1]=i>2?i:0;
   1412          maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
   1413          for(k=0;k<=(1275+75);k+=3)
   1414          {
   1415             /*Payload size*/
   1416             opus_int32 cnt,rcnt;
   1417             if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
   1418             for(cnt=0;cnt<maxp+2;cnt++)
   1419             {
   1420                if(cnt>0)
   1421                {
   1422                   ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
   1423                   if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
   1424                   cfgs++;
   1425                }
   1426                rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
   1427                if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
   1428                cfgs++;
   1429                ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
   1430                if(rcnt>0)
   1431                {
   1432                   int len;
   1433                   len=k*rcnt+((rcnt*i)>2?2:1);
   1434                   if(ret!=len)test_failed();
   1435                   if((rcnt*i)<2&&(po[0]&3)!=0)test_failed();                      /* Code 0 */
   1436                   if((rcnt*i)==2&&(po[0]&3)!=1)test_failed();                     /* Code 1 */
   1437                   if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
   1438                   cfgs++;
   1439                   if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
   1440                   cfgs++;
   1441                   if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
   1442                   cfgs++;
   1443                   if(len>1)
   1444                   {
   1445                      if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
   1446                      cfgs++;
   1447                   }
   1448                   if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
   1449                   cfgs++;
   1450                } else if (ret!=OPUS_BAD_ARG)test_failed();                        /* M must not be 0 */
   1451             }
   1452             opus_repacketizer_init(rp);
   1453          }
   1454       }
   1455    }
   1456 
   1457    /*Change in input count code, CBR out*/
   1458    opus_repacketizer_init(rp);
   1459    packet[0]=0;
   1460    if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
   1461    cfgs++;
   1462    packet[0]+=1;
   1463    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
   1464    cfgs++;
   1465    i=opus_repacketizer_out(rp,po,max_out);
   1466    if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
   1467    cfgs++;
   1468    i=opus_repacketizer_out_range(rp,0,1,po,max_out);
   1469    if(i!=5||(po[0]&3)!=0)test_failed();
   1470    cfgs++;
   1471    i=opus_repacketizer_out_range(rp,1,2,po,max_out);
   1472    if(i!=5||(po[0]&3)!=0)test_failed();
   1473    cfgs++;
   1474 
   1475    /*Change in input count code, VBR out*/
   1476    opus_repacketizer_init(rp);
   1477    packet[0]=1;
   1478    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
   1479    cfgs++;
   1480    packet[0]=0;
   1481    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
   1482    cfgs++;
   1483    i=opus_repacketizer_out(rp,po,max_out);
   1484    if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
   1485    cfgs++;
   1486 
   1487    /*VBR in, VBR out*/
   1488    opus_repacketizer_init(rp);
   1489    packet[0]=2;
   1490    packet[1]=4;
   1491    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
   1492    cfgs++;
   1493    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
   1494    cfgs++;
   1495    i=opus_repacketizer_out(rp,po,max_out);
   1496    if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
   1497    cfgs++;
   1498 
   1499    /*VBR in, CBR out*/
   1500    opus_repacketizer_init(rp);
   1501    packet[0]=2;
   1502    packet[1]=4;
   1503    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
   1504    cfgs++;
   1505    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
   1506    cfgs++;
   1507    i=opus_repacketizer_out(rp,po,max_out);
   1508    if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
   1509    cfgs++;
   1510 
   1511    /*Count 0 in, VBR out*/
   1512    for(j=0;j<32;j++)
   1513    {
   1514       /* TOC types, test half with stereo */
   1515       int maxi,sum,rcnt;
   1516       packet[0]=((j<<1)+(j&1))<<2;
   1517       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
   1518       sum=0;
   1519       rcnt=0;
   1520       opus_repacketizer_init(rp);
   1521       for(i=1;i<=maxi+2;i++)
   1522       {
   1523          int len;
   1524          ret=opus_repacketizer_cat(rp,packet,i);
   1525          if(rcnt<maxi)
   1526          {
   1527             if(ret!=OPUS_OK)test_failed();
   1528             rcnt++;
   1529             sum+=i-1;
   1530          } else if (ret!=OPUS_INVALID_PACKET)test_failed();
   1531          cfgs++;
   1532          len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
   1533          if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
   1534          if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
   1535          if(rcnt==2&&(po[0]&3)!=2)test_failed();
   1536          if(rcnt==1&&(po[0]&3)!=0)test_failed();
   1537          cfgs++;
   1538          if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
   1539          cfgs++;
   1540          if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
   1541          cfgs++;
   1542          if(len>1)
   1543          {
   1544             if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
   1545             cfgs++;
   1546          }
   1547          if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
   1548          cfgs++;
   1549       }
   1550    }
   1551 
   1552    fprintf(stdout,"    opus_repacketizer_cat ........................ OK.\n");
   1553    fprintf(stdout,"    opus_repacketizer_out ........................ OK.\n");
   1554    fprintf(stdout,"    opus_repacketizer_out_range .................. OK.\n");
   1555 
   1556    opus_repacketizer_destroy(rp);
   1557    cfgs++;
   1558    free(packet);
   1559    free(po);
   1560    fprintf(stdout,"                        All repacketizer tests passed\n");
   1561    fprintf(stdout,"                            (%7d API invocations)\n",cfgs);
   1562 
   1563    return cfgs;
   1564 }
   1565 
   1566 #ifdef MALLOC_FAIL
   1567 /* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
   1568  * under GCC. However, this is the cleanest way to test malloc failure
   1569  * handling in our codebase, and the lack of thread safety isn't an
   1570  * issue here. We therefore disable the warning for this function.
   1571  */
   1572 #if OPUS_GNUC_PREREQ(4,6)
   1573 /* Save the current warning settings */
   1574 #pragma GCC diagnostic push
   1575 #endif
   1576 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
   1577 
   1578 typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
   1579 #endif
   1580 
   1581 int test_malloc_fail(void)
   1582 {
   1583 #ifdef MALLOC_FAIL
   1584    OpusDecoder *dec;
   1585    OpusEncoder *enc;
   1586    OpusRepacketizer *rp;
   1587    unsigned char mapping[256] = {0,1};
   1588    OpusMSDecoder *msdec;
   1589    OpusMSEncoder *msenc;
   1590    int rate,c,app,cfgs,err,useerr;
   1591    int *ep;
   1592    mhook orig_malloc;
   1593    cfgs=0;
   1594 #endif
   1595    fprintf(stdout,"\n  malloc() failure tests\n");
   1596    fprintf(stdout,"  ---------------------------------------------------\n");
   1597 #ifdef MALLOC_FAIL
   1598    orig_malloc=__malloc_hook;
   1599    __malloc_hook=malloc_hook;
   1600    ep=(int *)opus_alloc(sizeof(int));
   1601    if(ep!=NULL)
   1602    {
   1603       if(ep)free(ep);
   1604       __malloc_hook=orig_malloc;
   1605 #endif
   1606       fprintf(stdout,"    opus_decoder_create() ................... SKIPPED.\n");
   1607       fprintf(stdout,"    opus_encoder_create() ................... SKIPPED.\n");
   1608       fprintf(stdout,"    opus_repacketizer_create() .............. SKIPPED.\n");
   1609       fprintf(stdout,"    opus_multistream_decoder_create() ....... SKIPPED.\n");
   1610       fprintf(stdout,"    opus_multistream_encoder_create() ....... SKIPPED.\n");
   1611       fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
   1612       return 0;
   1613 #ifdef MALLOC_FAIL
   1614    }
   1615    for(useerr=0;useerr<2;useerr++)
   1616    {
   1617       ep=useerr?&err:0;
   1618       for(rate=0;rate<5;rate++)
   1619       {
   1620         for(c=1;c<3;c++)
   1621         {
   1622            err=1;
   1623            if(useerr)
   1624            {
   1625               VG_UNDEF(&err,sizeof(err));
   1626            }
   1627            dec=opus_decoder_create(opus_rates[rate], c, ep);
   1628            if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
   1629            {
   1630               __malloc_hook=orig_malloc;
   1631               test_failed();
   1632            }
   1633            cfgs++;
   1634            msdec=opus_multistream_decoder_create(opus_rates[rate], c, 1, c-1, mapping, ep);
   1635            if(msdec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
   1636            {
   1637               __malloc_hook=orig_malloc;
   1638               test_failed();
   1639            }
   1640            cfgs++;
   1641            for(app=0;app<3;app++)
   1642            {
   1643               if(useerr)
   1644               {
   1645                  VG_UNDEF(&err,sizeof(err));
   1646               }
   1647               enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
   1648               if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
   1649               {
   1650                  __malloc_hook=orig_malloc;
   1651                  test_failed();
   1652               }
   1653               cfgs++;
   1654               msenc=opus_multistream_encoder_create(opus_rates[rate], c, 1, c-1, mapping, opus_apps[app],ep);
   1655               if(msenc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
   1656               {
   1657                  __malloc_hook=orig_malloc;
   1658                  test_failed();
   1659               }
   1660               cfgs++;
   1661            }
   1662         }
   1663      }
   1664    }
   1665    rp=opus_repacketizer_create();
   1666    if(rp!=NULL)
   1667    {
   1668       __malloc_hook=orig_malloc;
   1669       test_failed();
   1670    }
   1671    cfgs++;
   1672    __malloc_hook=orig_malloc;
   1673    fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
   1674    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
   1675    fprintf(stdout,"    opus_repacketizer_create() ................... OK.\n");
   1676    fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
   1677    fprintf(stdout,"    opus_multistream_encoder_create() ............ OK.\n");
   1678    fprintf(stdout,"                      All malloc failure tests passed\n");
   1679    fprintf(stdout,"                                 (%2d API invocations)\n",cfgs);
   1680    return cfgs;
   1681 #endif
   1682 }
   1683 
   1684 #ifdef MALLOC_FAIL
   1685 #if __GNUC_PREREQ(4,6)
   1686 #pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
   1687 #endif
   1688 #endif
   1689 
   1690 int main(int _argc, char **_argv)
   1691 {
   1692    opus_int32 total;
   1693    const char * oversion;
   1694    if(_argc>1)
   1695    {
   1696       fprintf(stderr,"Usage: %s\n",_argv[0]);
   1697       return 1;
   1698    }
   1699    iseed=0;
   1700 
   1701    oversion=opus_get_version_string();
   1702    if(!oversion)test_failed();
   1703    fprintf(stderr,"Testing the %s API deterministically\n", oversion);
   1704    if(opus_strerror(-32768)==NULL)test_failed();
   1705    if(opus_strerror(32767)==NULL)test_failed();
   1706    if(strlen(opus_strerror(0))<1)test_failed();
   1707    total=4;
   1708 
   1709    total+=test_dec_api();
   1710    total+=test_msdec_api();
   1711    total+=test_parse();
   1712    total+=test_enc_api();
   1713    total+=test_repacketizer_api();
   1714    total+=test_malloc_fail();
   1715 
   1716    fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
   1717 
   1718    return 0;
   1719 }
   1720