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 #ifdef HAVE_CONFIG_H
     29 #include "config.h"
     30 #endif
     31 
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <limits.h>
     35 #include <stdint.h>
     36 #include <math.h>
     37 #include <string.h>
     38 #include <time.h>
     39 #if (!defined WIN32 && !defined _WIN32) || defined(__MINGW32__)
     40 #include <unistd.h>
     41 #else
     42 #include <process.h>
     43 #define getpid _getpid
     44 #endif
     45 #include "opus.h"
     46 #include "test_opus_common.h"
     47 
     48 #define MAX_PACKET (1500)
     49 #define MAX_FRAME_SAMP (5760)
     50 
     51 int test_decoder_code0(int no_fuzz)
     52 {
     53    static const opus_int32 fsv[5]={48000,24000,16000,12000,8000};
     54    int err,skip,plen;
     55    int out_samples,fec;
     56    int t;
     57    opus_int32 i;
     58    OpusDecoder *dec[5*2];
     59    opus_int32 decsize;
     60    OpusDecoder *decbak;
     61    opus_uint32 dec_final_range1,dec_final_range2,dec_final_acc;
     62    unsigned char *packet;
     63    unsigned char modes[4096];
     64    short *outbuf_int;
     65    short *outbuf;
     66 
     67    dec_final_range1=dec_final_range2=2;
     68 
     69    packet=malloc(sizeof(unsigned char)*MAX_PACKET);
     70    if(packet==NULL)test_failed();
     71 
     72    outbuf_int=malloc(sizeof(short)*(MAX_FRAME_SAMP+16)*2);
     73    for(i=0;i<(MAX_FRAME_SAMP+16)*2;i++)outbuf_int[i]=32749;
     74    outbuf=&outbuf_int[8*2];
     75 
     76    fprintf(stdout,"  Starting %d decoders...\n",5*2);
     77    for(t=0;t<5*2;t++)
     78    {
     79       int fs=fsv[t>>1];
     80       int c=(t&1)+1;
     81       err=OPUS_INTERNAL_ERROR;
     82       dec[t] = opus_decoder_create(fs, c, &err);
     83       if(err!=OPUS_OK || dec[t]==NULL)test_failed();
     84       fprintf(stdout,"    opus_decoder_create(%5d,%d) OK. Copy ",fs,c);
     85       {
     86          OpusDecoder *dec2;
     87          /*The opus state structures contain no pointers and can be freely copied*/
     88          dec2=(OpusDecoder *)malloc(opus_decoder_get_size(c));
     89          if(dec2==NULL)test_failed();
     90          memcpy(dec2,dec[t],opus_decoder_get_size(c));
     91          memset(dec[t],255,opus_decoder_get_size(c));
     92          opus_decoder_destroy(dec[t]);
     93          printf("OK.\n");
     94          dec[t]=dec2;
     95       }
     96    }
     97 
     98    decsize=opus_decoder_get_size(1);
     99    decbak=(OpusDecoder *)malloc(decsize);
    100    if(decbak==NULL)test_failed();
    101 
    102    for(t=0;t<5*2;t++)
    103    {
    104       int factor=48000/fsv[t>>1];
    105       for(fec=0;fec<2;fec++)
    106       {
    107          int dur;
    108          /*Test PLC on a fresh decoder*/
    109          out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor, fec);
    110          if(out_samples!=120/factor)test_failed();
    111          if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
    112          if(dur!=120/factor)test_failed();
    113 
    114          /*Test null pointer input*/
    115          out_samples = opus_decode(dec[t], 0, -1, outbuf, 120/factor, fec);
    116          if(out_samples!=120/factor)test_failed();
    117          out_samples = opus_decode(dec[t], 0, 1, outbuf, 120/factor, fec);
    118          if(out_samples!=120/factor)test_failed();
    119          out_samples = opus_decode(dec[t], 0, 10, outbuf, 120/factor, fec);
    120          if(out_samples!=120/factor)test_failed();
    121          out_samples = opus_decode(dec[t], 0, fast_rand(), outbuf, 120/factor, fec);
    122          if(out_samples!=120/factor)test_failed();
    123          if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
    124          if(dur!=120/factor)test_failed();
    125 
    126          /*Zero lengths*/
    127          out_samples = opus_decode(dec[t], packet, 0, outbuf, 120/factor, fec);
    128          if(out_samples!=120/factor)test_failed();
    129 
    130          /*Zero buffer*/
    131          outbuf[0]=32749;
    132          out_samples = opus_decode(dec[t], packet, 0, outbuf, 0, fec);
    133          if(out_samples>0)test_failed();
    134          out_samples = opus_decode(dec[t], packet, 0, 0, 0, fec);
    135          if(out_samples>0)test_failed();
    136          if(outbuf[0]!=32749)test_failed();
    137 
    138          /*Invalid lengths*/
    139          out_samples = opus_decode(dec[t], packet, -1, outbuf, MAX_FRAME_SAMP, fec);
    140          if(out_samples>=0)test_failed();
    141          out_samples = opus_decode(dec[t], packet, INT_MIN, outbuf, MAX_FRAME_SAMP, fec);
    142          if(out_samples>=0)test_failed();
    143          out_samples = opus_decode(dec[t], packet, -1, outbuf, -1, fec);
    144          if(out_samples>=0)test_failed();
    145 
    146          /*Crazy FEC values*/
    147          out_samples = opus_decode(dec[t], packet, 1, outbuf, MAX_FRAME_SAMP, fec?-1:2);
    148          if(out_samples>=0)test_failed();
    149 
    150          /*Reset the decoder*/
    151          if(opus_decoder_ctl(dec[t], OPUS_RESET_STATE)!=OPUS_OK)test_failed();
    152       }
    153    }
    154    fprintf(stdout,"  dec[all] initial frame PLC OK.\n");
    155 
    156    /*Count code 0 tests*/
    157    for(i=0;i<64;i++)
    158    {
    159       int dur;
    160       int j,expected[5*2];
    161       packet[0]=i<<2;
    162       packet[1]=255;
    163       packet[2]=255;
    164       err=opus_packet_get_nb_channels(packet);
    165       if(err!=(i&1)+1)test_failed();
    166 
    167       for(t=0;t<5*2;t++){
    168          expected[t]=opus_decoder_get_nb_samples(dec[t],packet,1);
    169          if(expected[t]>2880)test_failed();
    170       }
    171 
    172       for(j=0;j<256;j++)
    173       {
    174          packet[1]=j;
    175          for(t=0;t<5*2;t++)
    176          {
    177             out_samples = opus_decode(dec[t], packet, 3, outbuf, MAX_FRAME_SAMP, 0);
    178             if(out_samples!=expected[t])test_failed();
    179             if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
    180             if(dur!=out_samples)test_failed();
    181             opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1));
    182             if(t==0)dec_final_range2=dec_final_range1;
    183             else if(dec_final_range1!=dec_final_range2)test_failed();
    184          }
    185       }
    186 
    187       for(t=0;t<5*2;t++){
    188          int factor=48000/fsv[t>>1];
    189          /* The PLC is run for 6 frames in order to get better PLC coverage. */
    190          for(j=0;j<6;j++)
    191          {
    192             out_samples = opus_decode(dec[t], 0, 0, outbuf, expected[t], 0);
    193             if(out_samples!=expected[t])test_failed();
    194             if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
    195             if(dur!=out_samples)test_failed();
    196          }
    197          /* Run the PLC once at 2.5ms, as a simulation of someone trying to
    198             do small drift corrections. */
    199          if(expected[t]!=120/factor)
    200          {
    201             out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor, 0);
    202             if(out_samples!=120/factor)test_failed();
    203             if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
    204             if(dur!=out_samples)test_failed();
    205          }
    206          out_samples = opus_decode(dec[t], packet, 2, outbuf, expected[t]-1, 0);
    207          if(out_samples>0)test_failed();
    208       }
    209    }
    210    fprintf(stdout,"  dec[all] all 2-byte prefix for length 3 and PLC, all modes (64) OK.\n");
    211 
    212    if(no_fuzz)
    213    {
    214       fprintf(stdout,"  Skipping many tests which fuzz the decoder as requested.\n");
    215       free(decbak);
    216       for(t=0;t<5*2;t++)opus_decoder_destroy(dec[t]);
    217       printf("  Decoders stopped.\n");
    218 
    219       err=0;
    220       for(i=0;i<8*2;i++)err|=outbuf_int[i]!=32749;
    221       for(i=MAX_FRAME_SAMP*2;i<(MAX_FRAME_SAMP+8)*2;i++)err|=outbuf[i]!=32749;
    222       if(err)test_failed();
    223 
    224       free(outbuf_int);
    225       free(packet);
    226       return 0;
    227    }
    228 
    229    {
    230      /*We only test a subset of the modes here simply because the longer
    231        durations end up taking a long time.*/
    232       static const int cmodes[4]={16,20,24,28};
    233       static const opus_uint32 cres[4]={116290185,2172123586,2172123586,2172123586};
    234       static const opus_uint32 lres[3]={3285687739,1481572662,694350475};
    235       static const int lmodes[3]={0,4,8};
    236       int mode=fast_rand()%4;
    237 
    238       packet[0]=cmodes[mode]<<3;
    239       dec_final_acc=0;
    240       t=fast_rand()%10;
    241 
    242       for(i=0;i<65536;i++)
    243       {
    244          int factor=48000/fsv[t>>1];
    245          packet[1]=i>>8;
    246          packet[2]=i&255;
    247          packet[3]=255;
    248          out_samples = opus_decode(dec[t], packet, 4, outbuf, MAX_FRAME_SAMP, 0);
    249          if(out_samples!=120/factor)test_failed();
    250          opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1));
    251          dec_final_acc+=dec_final_range1;
    252       }
    253       if(dec_final_acc!=cres[mode])test_failed();
    254       fprintf(stdout,"  dec[%3d] all 3-byte prefix for length 4, mode %2d OK.\n",t,cmodes[mode]);
    255 
    256       mode=fast_rand()%3;
    257       packet[0]=lmodes[mode]<<3;
    258       dec_final_acc=0;
    259       t=fast_rand()%10;
    260       for(i=0;i<65536;i++)
    261       {
    262          int factor=48000/fsv[t>>1];
    263          packet[1]=i>>8;
    264          packet[2]=i&255;
    265          packet[3]=255;
    266          out_samples = opus_decode(dec[t], packet, 4, outbuf, MAX_FRAME_SAMP, 0);
    267          if(out_samples!=480/factor)test_failed();
    268          opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1));
    269          dec_final_acc+=dec_final_range1;
    270       }
    271       if(dec_final_acc!=lres[mode])test_failed();
    272       fprintf(stdout,"  dec[%3d] all 3-byte prefix for length 4, mode %2d OK.\n",t,lmodes[mode]);
    273    }
    274 
    275    skip=fast_rand()%7;
    276    for(i=0;i<64;i++)
    277    {
    278       int j,expected[5*2];
    279       packet[0]=i<<2;
    280       for(t=0;t<5*2;t++)expected[t]=opus_decoder_get_nb_samples(dec[t],packet,1);
    281       for(j=2+skip;j<1275;j+=4)
    282       {
    283          int jj;
    284          for(jj=0;jj<j;jj++)packet[jj+1]=fast_rand()&255;
    285          for(t=0;t<5*2;t++)
    286          {
    287             out_samples = opus_decode(dec[t], packet, j+1, outbuf, MAX_FRAME_SAMP, 0);
    288             if(out_samples!=expected[t])test_failed();
    289             opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1));
    290             if(t==0)dec_final_range2=dec_final_range1;
    291             else if(dec_final_range1!=dec_final_range2)test_failed();
    292          }
    293       }
    294    }
    295    fprintf(stdout,"  dec[all] random packets, all modes (64), every 8th size from from %d bytes to maximum OK.\n",2+skip);
    296 
    297    debruijn2(64,modes);
    298    plen=(fast_rand()%18+3)*8+skip+3;
    299    for(i=0;i<4096;i++)
    300    {
    301       int j,expected[5*2];
    302       packet[0]=modes[i]<<2;
    303       for(t=0;t<5*2;t++)expected[t]=opus_decoder_get_nb_samples(dec[t],packet,plen);
    304       for(j=0;j<plen;j++)packet[j+1]=(fast_rand()|fast_rand())&255;
    305       memcpy(decbak,dec[0],decsize);
    306       if(opus_decode(decbak, packet, plen+1, outbuf, expected[0], 1)!=expected[0])test_failed();
    307       memcpy(decbak,dec[0],decsize);
    308       if(opus_decode(decbak,  0, 0, outbuf, MAX_FRAME_SAMP, 1)<20)test_failed();
    309       memcpy(decbak,dec[0],decsize);
    310       if(opus_decode(decbak,  0, 0, outbuf, MAX_FRAME_SAMP, 0)<20)test_failed();
    311       for(t=0;t<5*2;t++)
    312       {
    313          int dur;
    314          out_samples = opus_decode(dec[t], packet, plen+1, outbuf, MAX_FRAME_SAMP, 0);
    315          if(out_samples!=expected[t])test_failed();
    316          if(t==0)dec_final_range2=dec_final_range1;
    317          else if(dec_final_range1!=dec_final_range2)test_failed();
    318          if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
    319          if(dur!=out_samples)test_failed();
    320       }
    321    }
    322    fprintf(stdout,"  dec[all] random packets, all mode pairs (4096), %d bytes/frame OK.\n",plen+1);
    323 
    324    plen=(fast_rand()%18+3)*8+skip+3;
    325    t=rand()&3;
    326    for(i=0;i<4096;i++)
    327    {
    328       int count,j,expected;
    329       packet[0]=modes[i]<<2;
    330       expected=opus_decoder_get_nb_samples(dec[t],packet,plen);
    331       for(count=0;count<10;count++)
    332       {
    333          for(j=0;j<plen;j++)packet[j+1]=(fast_rand()|fast_rand())&255;
    334          out_samples = opus_decode(dec[t], packet, plen+1, outbuf, MAX_FRAME_SAMP, 0);
    335          if(out_samples!=expected)test_failed();
    336       }
    337    }
    338    fprintf(stdout,"  dec[%3d] random packets, all mode pairs (4096)*10, %d bytes/frame OK.\n",t,plen+1);
    339 
    340    {
    341       int tmodes[1]={25<<2};
    342       opus_uint32 tseeds[1]={140441};
    343       int tlen[1]={157};
    344       opus_int32 tret[1]={480};
    345       t=fast_rand()&1;
    346       for(i=0;i<1;i++)
    347       {
    348          int j;
    349          packet[0]=tmodes[i];
    350          Rw=Rz=tseeds[i];
    351          for(j=1;j<tlen[i];j++)packet[j]=fast_rand()&255;
    352          out_samples=opus_decode(dec[t], packet, tlen[i], outbuf, MAX_FRAME_SAMP, 0);
    353          if(out_samples!=tret[i])test_failed();
    354       }
    355       fprintf(stdout,"  dec[%3d] pre-selected random packets OK.\n",t);
    356    }
    357 
    358    free(decbak);
    359    for(t=0;t<5*2;t++)opus_decoder_destroy(dec[t]);
    360    printf("  Decoders stopped.\n");
    361 
    362    err=0;
    363    for(i=0;i<8*2;i++)err|=outbuf_int[i]!=32749;
    364    for(i=MAX_FRAME_SAMP*2;i<(MAX_FRAME_SAMP+8)*2;i++)err|=outbuf[i]!=32749;
    365    if(err)test_failed();
    366 
    367    free(outbuf_int);
    368    free(packet);
    369    return 0;
    370 }
    371 
    372 int main(int _argc, char **_argv)
    373 {
    374    const char * oversion;
    375    const char * env_seed;
    376    int env_used;
    377 
    378    if(_argc>2)
    379    {
    380       fprintf(stderr,"Usage: %s [<seed>]\n",_argv[0]);
    381       return 1;
    382    }
    383 
    384    env_used=0;
    385    env_seed=getenv("SEED");
    386    if(_argc>1)iseed=atoi(_argv[1]);
    387    else if(env_seed)
    388    {
    389       iseed=atoi(env_seed);
    390       env_used=1;
    391    }
    392    else iseed=(opus_uint32)time(NULL)^((getpid()&65535)<<16);
    393    Rw=Rz=iseed;
    394 
    395    oversion=opus_get_version_string();
    396    if(!oversion)test_failed();
    397    fprintf(stderr,"Testing %s decoder. Random seed: %u (%.4X)\n", oversion, iseed, fast_rand() % 65535);
    398    if(env_used)fprintf(stderr,"  Random seed set from the environment (SEED=%s).\n", env_seed);
    399 
    400    /*Setting TEST_OPUS_NOFUZZ tells the tool not to send garbage data
    401      into the decoders. This is helpful because garbage data
    402      may cause the decoders to clip, which angers CLANG IOC.*/
    403    test_decoder_code0(getenv("TEST_OPUS_NOFUZZ")!=NULL);
    404 
    405    return 0;
    406 }
    407