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