cannam@154: /* Copyright (c) 2011-2013 Xiph.Org Foundation cannam@154: Written by Gregory Maxwell */ cannam@154: /* cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: #ifdef HAVE_CONFIG_H cannam@154: #include "config.h" cannam@154: #endif cannam@154: cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: #if (!defined WIN32 && !defined _WIN32) || defined(__MINGW32__) cannam@154: #include cannam@154: #else cannam@154: #include cannam@154: #define getpid _getpid cannam@154: #endif cannam@154: #include "opus.h" cannam@154: #include "test_opus_common.h" cannam@154: cannam@154: #define MAX_PACKET (1500) cannam@154: #define MAX_FRAME_SAMP (5760) cannam@154: cannam@154: int test_decoder_code0(int no_fuzz) cannam@154: { cannam@154: static const opus_int32 fsv[5]={48000,24000,16000,12000,8000}; cannam@154: int err,skip,plen; cannam@154: int out_samples,fec; cannam@154: int t; cannam@154: opus_int32 i; cannam@154: OpusDecoder *dec[5*2]; cannam@154: opus_int32 decsize; cannam@154: OpusDecoder *decbak; cannam@154: opus_uint32 dec_final_range1,dec_final_range2,dec_final_acc; cannam@154: unsigned char *packet; cannam@154: unsigned char modes[4096]; cannam@154: short *outbuf_int; cannam@154: short *outbuf; cannam@154: cannam@154: dec_final_range1=dec_final_range2=2; cannam@154: cannam@154: packet=malloc(sizeof(unsigned char)*MAX_PACKET); cannam@154: if(packet==NULL)test_failed(); cannam@154: cannam@154: outbuf_int=malloc(sizeof(short)*(MAX_FRAME_SAMP+16)*2); cannam@154: for(i=0;i<(MAX_FRAME_SAMP+16)*2;i++)outbuf_int[i]=32749; cannam@154: outbuf=&outbuf_int[8*2]; cannam@154: cannam@154: fprintf(stdout," Starting %d decoders...\n",5*2); cannam@154: for(t=0;t<5*2;t++) cannam@154: { cannam@154: int fs=fsv[t>>1]; cannam@154: int c=(t&1)+1; cannam@154: err=OPUS_INTERNAL_ERROR; cannam@154: dec[t] = opus_decoder_create(fs, c, &err); cannam@154: if(err!=OPUS_OK || dec[t]==NULL)test_failed(); cannam@154: fprintf(stdout," opus_decoder_create(%5d,%d) OK. Copy ",fs,c); cannam@154: { cannam@154: OpusDecoder *dec2; cannam@154: /*The opus state structures contain no pointers and can be freely copied*/ cannam@154: dec2=(OpusDecoder *)malloc(opus_decoder_get_size(c)); cannam@154: if(dec2==NULL)test_failed(); cannam@154: memcpy(dec2,dec[t],opus_decoder_get_size(c)); cannam@154: memset(dec[t],255,opus_decoder_get_size(c)); cannam@154: opus_decoder_destroy(dec[t]); cannam@154: printf("OK.\n"); cannam@154: dec[t]=dec2; cannam@154: } cannam@154: } cannam@154: cannam@154: decsize=opus_decoder_get_size(1); cannam@154: decbak=(OpusDecoder *)malloc(decsize); cannam@154: if(decbak==NULL)test_failed(); cannam@154: cannam@154: for(t=0;t<5*2;t++) cannam@154: { cannam@154: int factor=48000/fsv[t>>1]; cannam@154: for(fec=0;fec<2;fec++) cannam@154: { cannam@154: opus_int32 dur; cannam@154: /*Test PLC on a fresh decoder*/ cannam@154: out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor, fec); cannam@154: if(out_samples!=120/factor)test_failed(); cannam@154: if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); cannam@154: if(dur!=120/factor)test_failed(); cannam@154: cannam@154: /*Test on a size which isn't a multiple of 2.5ms*/ cannam@154: out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor+2, fec); cannam@154: if(out_samples!=OPUS_BAD_ARG)test_failed(); cannam@154: cannam@154: /*Test null pointer input*/ cannam@154: out_samples = opus_decode(dec[t], 0, -1, outbuf, 120/factor, fec); cannam@154: if(out_samples!=120/factor)test_failed(); cannam@154: out_samples = opus_decode(dec[t], 0, 1, outbuf, 120/factor, fec); cannam@154: if(out_samples!=120/factor)test_failed(); cannam@154: out_samples = opus_decode(dec[t], 0, 10, outbuf, 120/factor, fec); cannam@154: if(out_samples!=120/factor)test_failed(); cannam@154: out_samples = opus_decode(dec[t], 0, fast_rand(), outbuf, 120/factor, fec); cannam@154: if(out_samples!=120/factor)test_failed(); cannam@154: if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); cannam@154: if(dur!=120/factor)test_failed(); cannam@154: cannam@154: /*Zero lengths*/ cannam@154: out_samples = opus_decode(dec[t], packet, 0, outbuf, 120/factor, fec); cannam@154: if(out_samples!=120/factor)test_failed(); cannam@154: cannam@154: /*Zero buffer*/ cannam@154: outbuf[0]=32749; cannam@154: out_samples = opus_decode(dec[t], packet, 0, outbuf, 0, fec); cannam@154: if(out_samples>0)test_failed(); cannam@154: #if !defined(OPUS_BUILD) && (OPUS_GNUC_PREREQ(4, 6) || (defined(__clang_major__) && __clang_major__ >= 3)) cannam@154: #pragma GCC diagnostic push cannam@154: #pragma GCC diagnostic ignored "-Wnonnull" cannam@154: #endif cannam@154: out_samples = opus_decode(dec[t], packet, 0, 0, 0, fec); cannam@154: #if !defined(OPUS_BUILD) && (OPUS_GNUC_PREREQ(4, 6) || (defined(__clang_major__) && __clang_major__ >= 3)) cannam@154: #pragma GCC diagnostic pop cannam@154: #endif cannam@154: if(out_samples>0)test_failed(); cannam@154: if(outbuf[0]!=32749)test_failed(); cannam@154: cannam@154: /*Invalid lengths*/ cannam@154: out_samples = opus_decode(dec[t], packet, -1, outbuf, MAX_FRAME_SAMP, fec); cannam@154: if(out_samples>=0)test_failed(); cannam@154: out_samples = opus_decode(dec[t], packet, INT_MIN, outbuf, MAX_FRAME_SAMP, fec); cannam@154: if(out_samples>=0)test_failed(); cannam@154: out_samples = opus_decode(dec[t], packet, -1, outbuf, -1, fec); cannam@154: if(out_samples>=0)test_failed(); cannam@154: cannam@154: /*Crazy FEC values*/ cannam@154: out_samples = opus_decode(dec[t], packet, 1, outbuf, MAX_FRAME_SAMP, fec?-1:2); cannam@154: if(out_samples>=0)test_failed(); cannam@154: cannam@154: /*Reset the decoder*/ cannam@154: if(opus_decoder_ctl(dec[t], OPUS_RESET_STATE)!=OPUS_OK)test_failed(); cannam@154: } cannam@154: } cannam@154: fprintf(stdout," dec[all] initial frame PLC OK.\n"); cannam@154: cannam@154: /*Count code 0 tests*/ cannam@154: for(i=0;i<64;i++) cannam@154: { cannam@154: opus_int32 dur; cannam@154: int j,expected[5*2]; cannam@154: packet[0]=i<<2; cannam@154: packet[1]=255; cannam@154: packet[2]=255; cannam@154: err=opus_packet_get_nb_channels(packet); cannam@154: if(err!=(i&1)+1)test_failed(); cannam@154: cannam@154: for(t=0;t<5*2;t++){ cannam@154: expected[t]=opus_decoder_get_nb_samples(dec[t],packet,1); cannam@154: if(expected[t]>2880)test_failed(); cannam@154: } cannam@154: cannam@154: for(j=0;j<256;j++) cannam@154: { cannam@154: packet[1]=j; cannam@154: for(t=0;t<5*2;t++) cannam@154: { cannam@154: out_samples = opus_decode(dec[t], packet, 3, outbuf, MAX_FRAME_SAMP, 0); cannam@154: if(out_samples!=expected[t])test_failed(); cannam@154: if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); cannam@154: if(dur!=out_samples)test_failed(); cannam@154: opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1)); cannam@154: if(t==0)dec_final_range2=dec_final_range1; cannam@154: else if(dec_final_range1!=dec_final_range2)test_failed(); cannam@154: } cannam@154: } cannam@154: cannam@154: for(t=0;t<5*2;t++){ cannam@154: int factor=48000/fsv[t>>1]; cannam@154: /* The PLC is run for 6 frames in order to get better PLC coverage. */ cannam@154: for(j=0;j<6;j++) cannam@154: { cannam@154: out_samples = opus_decode(dec[t], 0, 0, outbuf, expected[t], 0); cannam@154: if(out_samples!=expected[t])test_failed(); cannam@154: if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); cannam@154: if(dur!=out_samples)test_failed(); cannam@154: } cannam@154: /* Run the PLC once at 2.5ms, as a simulation of someone trying to cannam@154: do small drift corrections. */ cannam@154: if(expected[t]!=120/factor) cannam@154: { cannam@154: out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor, 0); cannam@154: if(out_samples!=120/factor)test_failed(); cannam@154: if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); cannam@154: if(dur!=out_samples)test_failed(); cannam@154: } cannam@154: out_samples = opus_decode(dec[t], packet, 2, outbuf, expected[t]-1, 0); cannam@154: if(out_samples>0)test_failed(); cannam@154: } cannam@154: } cannam@154: fprintf(stdout," dec[all] all 2-byte prefix for length 3 and PLC, all modes (64) OK.\n"); cannam@154: cannam@154: if(no_fuzz) cannam@154: { cannam@154: fprintf(stdout," Skipping many tests which fuzz the decoder as requested.\n"); cannam@154: free(decbak); cannam@154: for(t=0;t<5*2;t++)opus_decoder_destroy(dec[t]); cannam@154: printf(" Decoders stopped.\n"); cannam@154: cannam@154: err=0; cannam@154: for(i=0;i<8*2;i++)err|=outbuf_int[i]!=32749; cannam@154: for(i=MAX_FRAME_SAMP*2;i<(MAX_FRAME_SAMP+8)*2;i++)err|=outbuf[i]!=32749; cannam@154: if(err)test_failed(); cannam@154: cannam@154: free(outbuf_int); cannam@154: free(packet); cannam@154: return 0; cannam@154: } cannam@154: cannam@154: { cannam@154: /*We only test a subset of the modes here simply because the longer cannam@154: durations end up taking a long time.*/ cannam@154: static const int cmodes[4]={16,20,24,28}; cannam@154: static const opus_uint32 cres[4]={116290185,2172123586u,2172123586u,2172123586u}; cannam@154: static const opus_uint32 lres[3]={3285687739u,1481572662,694350475}; cannam@154: static const int lmodes[3]={0,4,8}; cannam@154: int mode=fast_rand()%4; cannam@154: cannam@154: packet[0]=cmodes[mode]<<3; cannam@154: dec_final_acc=0; cannam@154: t=fast_rand()%10; cannam@154: cannam@154: for(i=0;i<65536;i++) cannam@154: { cannam@154: int factor=48000/fsv[t>>1]; cannam@154: packet[1]=i>>8; cannam@154: packet[2]=i&255; cannam@154: packet[3]=255; cannam@154: out_samples = opus_decode(dec[t], packet, 4, outbuf, MAX_FRAME_SAMP, 0); cannam@154: if(out_samples!=120/factor)test_failed(); cannam@154: opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1)); cannam@154: dec_final_acc+=dec_final_range1; cannam@154: } cannam@154: if(dec_final_acc!=cres[mode])test_failed(); cannam@154: fprintf(stdout," dec[%3d] all 3-byte prefix for length 4, mode %2d OK.\n",t,cmodes[mode]); cannam@154: cannam@154: mode=fast_rand()%3; cannam@154: packet[0]=lmodes[mode]<<3; cannam@154: dec_final_acc=0; cannam@154: t=fast_rand()%10; cannam@154: for(i=0;i<65536;i++) cannam@154: { cannam@154: int factor=48000/fsv[t>>1]; cannam@154: packet[1]=i>>8; cannam@154: packet[2]=i&255; cannam@154: packet[3]=255; cannam@154: out_samples = opus_decode(dec[t], packet, 4, outbuf, MAX_FRAME_SAMP, 0); cannam@154: if(out_samples!=480/factor)test_failed(); cannam@154: opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1)); cannam@154: dec_final_acc+=dec_final_range1; cannam@154: } cannam@154: if(dec_final_acc!=lres[mode])test_failed(); cannam@154: fprintf(stdout," dec[%3d] all 3-byte prefix for length 4, mode %2d OK.\n",t,lmodes[mode]); cannam@154: } cannam@154: cannam@154: skip=fast_rand()%7; cannam@154: for(i=0;i<64;i++) cannam@154: { cannam@154: int j,expected[5*2]; cannam@154: packet[0]=i<<2; cannam@154: for(t=0;t<5*2;t++)expected[t]=opus_decoder_get_nb_samples(dec[t],packet,1); cannam@154: for(j=2+skip;j<1275;j+=4) cannam@154: { cannam@154: int jj; cannam@154: for(jj=0;jj1.f)test_failed(); cannam@154: if(x[j]<-1.f)test_failed(); cannam@154: } cannam@154: } cannam@154: for(i=1;i<9;i++) cannam@154: { cannam@154: for (j=0;j<1024;j++) cannam@154: { cannam@154: x[j]=(j&255)*(1/32.f)-4.f; cannam@154: } cannam@154: opus_pcm_soft_clip(x,1024/i,i,s); cannam@154: for (j=0;j<(1024/i)*i;j++) cannam@154: { cannam@154: if(x[j]>1.f)test_failed(); cannam@154: if(x[j]<-1.f)test_failed(); cannam@154: } cannam@154: } cannam@154: opus_pcm_soft_clip(x,0,1,s); cannam@154: opus_pcm_soft_clip(x,1,0,s); cannam@154: opus_pcm_soft_clip(x,1,1,0); cannam@154: opus_pcm_soft_clip(x,1,-1,s); cannam@154: opus_pcm_soft_clip(x,-1,1,s); cannam@154: opus_pcm_soft_clip(0,1,1,s); cannam@154: printf("OK.\n"); cannam@154: } cannam@154: #endif cannam@154: cannam@154: int main(int _argc, char **_argv) cannam@154: { cannam@154: const char * oversion; cannam@154: const char * env_seed; cannam@154: int env_used; cannam@154: cannam@154: if(_argc>2) cannam@154: { cannam@154: fprintf(stderr,"Usage: %s []\n",_argv[0]); cannam@154: return 1; cannam@154: } cannam@154: cannam@154: env_used=0; cannam@154: env_seed=getenv("SEED"); cannam@154: if(_argc>1)iseed=atoi(_argv[1]); cannam@154: else if(env_seed) cannam@154: { cannam@154: iseed=atoi(env_seed); cannam@154: env_used=1; cannam@154: } cannam@154: else iseed=(opus_uint32)time(NULL)^(((opus_uint32)getpid()&65535)<<16); cannam@154: Rw=Rz=iseed; cannam@154: cannam@154: oversion=opus_get_version_string(); cannam@154: if(!oversion)test_failed(); cannam@154: fprintf(stderr,"Testing %s decoder. Random seed: %u (%.4X)\n", oversion, iseed, fast_rand() % 65535); cannam@154: if(env_used)fprintf(stderr," Random seed set from the environment (SEED=%s).\n", env_seed); cannam@154: cannam@154: /*Setting TEST_OPUS_NOFUZZ tells the tool not to send garbage data cannam@154: into the decoders. This is helpful because garbage data cannam@154: may cause the decoders to clip, which angers CLANG IOC.*/ cannam@154: test_decoder_code0(getenv("TEST_OPUS_NOFUZZ")!=NULL); cannam@154: #ifndef DISABLE_FLOAT_API cannam@154: test_soft_clip(); cannam@154: #endif cannam@154: cannam@154: return 0; cannam@154: }