test_opus_decode.c 16 KB

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