test_opus_encode.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. #if (!defined WIN32 && !defined _WIN32) || defined(__MINGW32__)
  35. #include <unistd.h>
  36. #else
  37. #include <process.h>
  38. #define getpid _getpid
  39. #endif
  40. #include "opus_multistream.h"
  41. #include "opus.h"
  42. #include "../src/opus_private.h"
  43. #include "test_opus_common.h"
  44. #define MAX_PACKET (1500)
  45. #define SAMPLES (48000*30)
  46. #define SSAMPLES (SAMPLES/3)
  47. #define MAX_FRAME_SAMP (5760)
  48. #define PI (3.141592653589793238462643f)
  49. #define RAND_SAMPLE(a) (a[fast_rand() % sizeof(a)/sizeof(a[0])])
  50. void generate_music(short *buf, opus_int32 len)
  51. {
  52. opus_int32 a1,b1,a2,b2;
  53. opus_int32 c1,c2,d1,d2;
  54. opus_int32 i,j;
  55. a1=b1=a2=b2=0;
  56. c1=c2=d1=d2=0;
  57. j=0;
  58. /*60ms silence*/
  59. for(i=0;i<2880;i++)buf[i*2]=buf[i*2+1]=0;
  60. for(i=2880;i<len;i++)
  61. {
  62. opus_uint32 r;
  63. opus_int32 v1,v2;
  64. v1=v2=(((j*((j>>12)^((j>>10|j>>12)&26&j>>7)))&128)+128)<<15;
  65. r=fast_rand();v1+=r&65535;v1-=r>>16;
  66. r=fast_rand();v2+=r&65535;v2-=r>>16;
  67. b1=v1-a1+((b1*61+32)>>6);a1=v1;
  68. b2=v2-a2+((b2*61+32)>>6);a2=v2;
  69. c1=(30*(c1+b1+d1)+32)>>6;d1=b1;
  70. c2=(30*(c2+b2+d2)+32)>>6;d2=b2;
  71. v1=(c1+128)>>8;
  72. v2=(c2+128)>>8;
  73. buf[i*2]=v1>32767?32767:(v1<-32768?-32768:v1);
  74. buf[i*2+1]=v2>32767?32767:(v2<-32768?-32768:v2);
  75. if(i%6==0)j++;
  76. }
  77. }
  78. #if 0
  79. static int save_ctr = 0;
  80. static void int_to_char(opus_uint32 i, unsigned char ch[4])
  81. {
  82. ch[0] = i>>24;
  83. ch[1] = (i>>16)&0xFF;
  84. ch[2] = (i>>8)&0xFF;
  85. ch[3] = i&0xFF;
  86. }
  87. static OPUS_INLINE void save_packet(unsigned char* p, int len, opus_uint32 rng)
  88. {
  89. FILE *fout;
  90. unsigned char int_field[4];
  91. char name[256];
  92. snprintf(name,255,"test_opus_encode.%llu.%d.bit",(unsigned long long)iseed,save_ctr);
  93. fprintf(stdout,"writing %d byte packet to %s\n",len,name);
  94. fout=fopen(name, "wb+");
  95. if(fout==NULL)test_failed();
  96. int_to_char(len, int_field);
  97. fwrite(int_field, 1, 4, fout);
  98. int_to_char(rng, int_field);
  99. fwrite(int_field, 1, 4, fout);
  100. fwrite(p, 1, len, fout);
  101. fclose(fout);
  102. save_ctr++;
  103. }
  104. #endif
  105. int get_frame_size_enum(int frame_size, int sampling_rate)
  106. {
  107. int frame_size_enum;
  108. if(frame_size==sampling_rate/400)
  109. frame_size_enum = OPUS_FRAMESIZE_2_5_MS;
  110. else if(frame_size==sampling_rate/200)
  111. frame_size_enum = OPUS_FRAMESIZE_5_MS;
  112. else if(frame_size==sampling_rate/100)
  113. frame_size_enum = OPUS_FRAMESIZE_10_MS;
  114. else if(frame_size==sampling_rate/50)
  115. frame_size_enum = OPUS_FRAMESIZE_20_MS;
  116. else if(frame_size==sampling_rate/25)
  117. frame_size_enum = OPUS_FRAMESIZE_40_MS;
  118. else if(frame_size==3*sampling_rate/50)
  119. frame_size_enum = OPUS_FRAMESIZE_60_MS;
  120. else if(frame_size==4*sampling_rate/50)
  121. frame_size_enum = OPUS_FRAMESIZE_80_MS;
  122. else if(frame_size==5*sampling_rate/50)
  123. frame_size_enum = OPUS_FRAMESIZE_100_MS;
  124. else if(frame_size==6*sampling_rate/50)
  125. frame_size_enum = OPUS_FRAMESIZE_120_MS;
  126. else
  127. test_failed();
  128. return frame_size_enum;
  129. }
  130. int test_encode(OpusEncoder *enc, int channels, int frame_size, OpusDecoder *dec)
  131. {
  132. int samp_count = 0;
  133. opus_int16 *inbuf;
  134. unsigned char packet[MAX_PACKET+257];
  135. int len;
  136. opus_int16 *outbuf;
  137. int out_samples;
  138. int ret = 0;
  139. /* Generate input data */
  140. inbuf = (opus_int16*)malloc(sizeof(*inbuf)*SSAMPLES);
  141. generate_music(inbuf, SSAMPLES/2);
  142. /* Allocate memory for output data */
  143. outbuf = (opus_int16*)malloc(sizeof(*outbuf)*MAX_FRAME_SAMP*3);
  144. /* Encode data, then decode for sanity check */
  145. do {
  146. len = opus_encode(enc, &inbuf[samp_count*channels], frame_size, packet, MAX_PACKET);
  147. if(len<0 || len>MAX_PACKET) {
  148. fprintf(stderr,"opus_encode() returned %d\n",len);
  149. ret = -1;
  150. break;
  151. }
  152. out_samples = opus_decode(dec, packet, len, outbuf, MAX_FRAME_SAMP, 0);
  153. if(out_samples!=frame_size) {
  154. fprintf(stderr,"opus_decode() returned %d\n",out_samples);
  155. ret = -1;
  156. break;
  157. }
  158. samp_count += frame_size;
  159. } while (samp_count < ((SSAMPLES/2)-MAX_FRAME_SAMP));
  160. /* Clean up */
  161. free(inbuf);
  162. free(outbuf);
  163. return ret;
  164. }
  165. void fuzz_encoder_settings(const int num_encoders, const int num_setting_changes)
  166. {
  167. OpusEncoder *enc;
  168. OpusDecoder *dec;
  169. int i,j,err;
  170. /* Parameters to fuzz. Some values are duplicated to increase their probability of being tested. */
  171. int sampling_rates[5] = {8000, 12000, 16000, 24000, 48000};
  172. int channels[2] = {1, 2};
  173. int applications[3] = {OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY};
  174. int bitrates[11] = {6000, 12000, 16000, 24000, 32000, 48000, 64000, 96000, 510000, OPUS_AUTO, OPUS_BITRATE_MAX};
  175. int force_channels[4] = {OPUS_AUTO, OPUS_AUTO, 1, 2};
  176. int use_vbr[3] = {0, 1, 1};
  177. int vbr_constraints[3] = {0, 1, 1};
  178. int complexities[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  179. int max_bandwidths[6] = {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
  180. OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
  181. OPUS_BANDWIDTH_FULLBAND, OPUS_BANDWIDTH_FULLBAND};
  182. int signals[4] = {OPUS_AUTO, OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC};
  183. int inband_fecs[3] = {0, 0, 1};
  184. int packet_loss_perc[4] = {0, 1, 2, 5};
  185. int lsb_depths[2] = {8, 24};
  186. int prediction_disabled[3] = {0, 0, 1};
  187. int use_dtx[2] = {0, 1};
  188. int frame_sizes_ms_x2[9] = {5, 10, 20, 40, 80, 120, 160, 200, 240}; /* x2 to avoid 2.5 ms */
  189. for (i=0; i<num_encoders; i++) {
  190. int sampling_rate = RAND_SAMPLE(sampling_rates);
  191. int num_channels = RAND_SAMPLE(channels);
  192. int application = RAND_SAMPLE(applications);
  193. dec = opus_decoder_create(sampling_rate, num_channels, &err);
  194. if(err!=OPUS_OK || dec==NULL)test_failed();
  195. enc = opus_encoder_create(sampling_rate, num_channels, application, &err);
  196. if(err!=OPUS_OK || enc==NULL)test_failed();
  197. for (j=0; j<num_setting_changes; j++) {
  198. int bitrate = RAND_SAMPLE(bitrates);
  199. int force_channel = RAND_SAMPLE(force_channels);
  200. int vbr = RAND_SAMPLE(use_vbr);
  201. int vbr_constraint = RAND_SAMPLE(vbr_constraints);
  202. int complexity = RAND_SAMPLE(complexities);
  203. int max_bw = RAND_SAMPLE(max_bandwidths);
  204. int sig = RAND_SAMPLE(signals);
  205. int inband_fec = RAND_SAMPLE(inband_fecs);
  206. int pkt_loss = RAND_SAMPLE(packet_loss_perc);
  207. int lsb_depth = RAND_SAMPLE(lsb_depths);
  208. int pred_disabled = RAND_SAMPLE(prediction_disabled);
  209. int dtx = RAND_SAMPLE(use_dtx);
  210. int frame_size_ms_x2 = RAND_SAMPLE(frame_sizes_ms_x2);
  211. int frame_size = frame_size_ms_x2*sampling_rate/2000;
  212. int frame_size_enum = get_frame_size_enum(frame_size, sampling_rate);
  213. force_channel = IMIN(force_channel, num_channels);
  214. if(opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate)) != OPUS_OK) test_failed();
  215. if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(force_channel)) != OPUS_OK) test_failed();
  216. if(opus_encoder_ctl(enc, OPUS_SET_VBR(vbr)) != OPUS_OK) test_failed();
  217. if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(vbr_constraint)) != OPUS_OK) test_failed();
  218. if(opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity)) != OPUS_OK) test_failed();
  219. if(opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(max_bw)) != OPUS_OK) test_failed();
  220. if(opus_encoder_ctl(enc, OPUS_SET_SIGNAL(sig)) != OPUS_OK) test_failed();
  221. if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(inband_fec)) != OPUS_OK) test_failed();
  222. if(opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(pkt_loss)) != OPUS_OK) test_failed();
  223. if(opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(lsb_depth)) != OPUS_OK) test_failed();
  224. if(opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED(pred_disabled)) != OPUS_OK) test_failed();
  225. if(opus_encoder_ctl(enc, OPUS_SET_DTX(dtx)) != OPUS_OK) test_failed();
  226. if(opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(frame_size_enum)) != OPUS_OK) test_failed();
  227. if(test_encode(enc, num_channels, frame_size, dec)) {
  228. fprintf(stderr,
  229. "fuzz_encoder_settings: %d kHz, %d ch, application: %d, "
  230. "%d bps, force ch: %d, vbr: %d, vbr constraint: %d, complexity: %d, "
  231. "max bw: %d, signal: %d, inband fec: %d, pkt loss: %d%%, lsb depth: %d, "
  232. "pred disabled: %d, dtx: %d, (%d/2) ms\n",
  233. sampling_rate/1000, num_channels, application, bitrate,
  234. force_channel, vbr, vbr_constraint, complexity, max_bw, sig, inband_fec,
  235. pkt_loss, lsb_depth, pred_disabled, dtx, frame_size_ms_x2);
  236. test_failed();
  237. }
  238. }
  239. opus_encoder_destroy(enc);
  240. opus_decoder_destroy(dec);
  241. }
  242. }
  243. int run_test1(int no_fuzz)
  244. {
  245. static const int fsizes[6]={960*3,960*2,120,240,480,960};
  246. static const char *mstrings[3] = {" LP","Hybrid"," MDCT"};
  247. unsigned char mapping[256] = {0,1,255};
  248. unsigned char db62[36];
  249. opus_int32 i,j;
  250. int rc,err;
  251. OpusEncoder *enc;
  252. OpusMSEncoder *MSenc;
  253. OpusDecoder *dec;
  254. OpusMSDecoder *MSdec;
  255. OpusMSDecoder *MSdec_err;
  256. OpusDecoder *dec_err[10];
  257. short *inbuf;
  258. short *outbuf;
  259. short *out2buf;
  260. opus_int32 bitrate_bps;
  261. unsigned char packet[MAX_PACKET+257];
  262. opus_uint32 enc_final_range;
  263. opus_uint32 dec_final_range;
  264. int fswitch;
  265. int fsize;
  266. int count;
  267. /*FIXME: encoder api tests, fs!=48k, mono, VBR*/
  268. fprintf(stdout," Encode+Decode tests.\n");
  269. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
  270. if(err != OPUS_OK || enc==NULL)test_failed();
  271. for(i=0;i<2;i++)
  272. {
  273. int *ret_err;
  274. ret_err = i?0:&err;
  275. MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_UNIMPLEMENTED, ret_err);
  276. if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
  277. MSenc = opus_multistream_encoder_create(8000, 0, 1, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
  278. if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
  279. MSenc = opus_multistream_encoder_create(44100, 2, 2, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
  280. if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
  281. MSenc = opus_multistream_encoder_create(8000, 2, 2, 3, mapping, OPUS_APPLICATION_VOIP, ret_err);
  282. if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
  283. MSenc = opus_multistream_encoder_create(8000, 2, -1, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
  284. if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
  285. MSenc = opus_multistream_encoder_create(8000, 256, 2, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
  286. if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
  287. }
  288. MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_APPLICATION_AUDIO, &err);
  289. if(err != OPUS_OK || MSenc==NULL)test_failed();
  290. /*Some multistream encoder API tests*/
  291. if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
  292. if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_LSB_DEPTH(&i))!=OPUS_OK)test_failed();
  293. if(i<16)test_failed();
  294. {
  295. OpusEncoder *tmp_enc;
  296. if(opus_multistream_encoder_ctl(MSenc, OPUS_MULTISTREAM_GET_ENCODER_STATE(1,&tmp_enc))!=OPUS_OK)test_failed();
  297. if(opus_encoder_ctl(tmp_enc, OPUS_GET_LSB_DEPTH(&j))!=OPUS_OK)test_failed();
  298. if(i!=j)test_failed();
  299. if(opus_multistream_encoder_ctl(MSenc, OPUS_MULTISTREAM_GET_ENCODER_STATE(2,&tmp_enc))!=OPUS_BAD_ARG)test_failed();
  300. }
  301. dec = opus_decoder_create(48000, 2, &err);
  302. if(err != OPUS_OK || dec==NULL)test_failed();
  303. MSdec = opus_multistream_decoder_create(48000, 2, 2, 0, mapping, &err);
  304. if(err != OPUS_OK || MSdec==NULL)test_failed();
  305. MSdec_err = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, &err);
  306. if(err != OPUS_OK || MSdec_err==NULL)test_failed();
  307. dec_err[0]=(OpusDecoder *)malloc(opus_decoder_get_size(2));
  308. memcpy(dec_err[0],dec,opus_decoder_get_size(2));
  309. dec_err[1] = opus_decoder_create(48000, 1, &err);
  310. dec_err[2] = opus_decoder_create(24000, 2, &err);
  311. dec_err[3] = opus_decoder_create(24000, 1, &err);
  312. dec_err[4] = opus_decoder_create(16000, 2, &err);
  313. dec_err[5] = opus_decoder_create(16000, 1, &err);
  314. dec_err[6] = opus_decoder_create(12000, 2, &err);
  315. dec_err[7] = opus_decoder_create(12000, 1, &err);
  316. dec_err[8] = opus_decoder_create(8000, 2, &err);
  317. dec_err[9] = opus_decoder_create(8000, 1, &err);
  318. for(i=0;i<10;i++)if(dec_err[i]==NULL)test_failed();
  319. {
  320. OpusEncoder *enccpy;
  321. /*The opus state structures contain no pointers and can be freely copied*/
  322. enccpy=(OpusEncoder *)malloc(opus_encoder_get_size(2));
  323. memcpy(enccpy,enc,opus_encoder_get_size(2));
  324. memset(enc,255,opus_encoder_get_size(2));
  325. opus_encoder_destroy(enc);
  326. enc=enccpy;
  327. }
  328. inbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
  329. outbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
  330. out2buf=(short *)malloc(sizeof(short)*MAX_FRAME_SAMP*3);
  331. if(inbuf==NULL || outbuf==NULL || out2buf==NULL)test_failed();
  332. generate_music(inbuf,SAMPLES);
  333. /* FILE *foo;
  334. foo = fopen("foo.sw", "wb+");
  335. fwrite(inbuf, 1, SAMPLES*2*2, foo);
  336. fclose(foo);*/
  337. if(opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
  338. if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(-2))!=OPUS_BAD_ARG)test_failed();
  339. if(opus_encode(enc, inbuf, 500, packet, MAX_PACKET)!=OPUS_BAD_ARG)test_failed();
  340. for(rc=0;rc<3;rc++)
  341. {
  342. if(opus_encoder_ctl(enc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
  343. if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
  344. if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
  345. if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
  346. for(j=0;j<13;j++)
  347. {
  348. int rate;
  349. int modes[13]={0,0,0,1,1,1,1,2,2,2,2,2,2};
  350. int rates[13]={6000,12000,48000,16000,32000,48000,64000,512000,13000,24000,48000,64000,96000};
  351. int frame[13]={960*2,960,480,960,960,960,480,960*3,960*3,960,480,240,120};
  352. rate=rates[j]+fast_rand()%rates[j];
  353. count=i=0;
  354. do {
  355. int bw,len,out_samples,frame_size;
  356. frame_size=frame[j];
  357. if((fast_rand()&255)==0)
  358. {
  359. if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  360. if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  361. if((fast_rand()&1)!=0)
  362. {
  363. if(opus_decoder_ctl(dec_err[fast_rand()&1], OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  364. }
  365. }
  366. if((fast_rand()&127)==0)
  367. {
  368. if(opus_decoder_ctl(dec_err[fast_rand()&1], OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  369. }
  370. if(fast_rand()%10==0){
  371. int complex=fast_rand()%11;
  372. if(opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complex))!=OPUS_OK)test_failed();
  373. }
  374. if(fast_rand()%50==0)opus_decoder_ctl(dec, OPUS_RESET_STATE);
  375. if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
  376. if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j]))!=OPUS_OK)test_failed();
  377. if(opus_encoder_ctl(enc, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_failed();
  378. if(opus_encoder_ctl(enc, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_failed();
  379. if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS((rates[j]>=64000?2:1)))!=OPUS_OK)test_failed();
  380. if(opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_OK)test_failed();
  381. if(opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
  382. bw=modes[j]==0?OPUS_BANDWIDTH_NARROWBAND+(fast_rand()%3):
  383. modes[j]==1?OPUS_BANDWIDTH_SUPERWIDEBAND+(fast_rand()&1):
  384. OPUS_BANDWIDTH_NARROWBAND+(fast_rand()%5);
  385. if(modes[j]==2&&bw==OPUS_BANDWIDTH_MEDIUMBAND)bw+=3;
  386. if(opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(bw))!=OPUS_OK)test_failed();
  387. len = opus_encode(enc, &inbuf[i<<1], frame_size, packet, MAX_PACKET);
  388. if(len<0 || len>MAX_PACKET)test_failed();
  389. if(opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
  390. if((fast_rand()&3)==0)
  391. {
  392. if(opus_packet_pad(packet,len,len+1)!=OPUS_OK)test_failed();
  393. len++;
  394. }
  395. if((fast_rand()&7)==0)
  396. {
  397. if(opus_packet_pad(packet,len,len+256)!=OPUS_OK)test_failed();
  398. len+=256;
  399. }
  400. if((fast_rand()&3)==0)
  401. {
  402. len=opus_packet_unpad(packet,len);
  403. if(len<1)test_failed();
  404. }
  405. out_samples = opus_decode(dec, packet, len, &outbuf[i<<1], MAX_FRAME_SAMP, 0);
  406. if(out_samples!=frame_size)test_failed();
  407. if(opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
  408. if(enc_final_range!=dec_final_range)test_failed();
  409. /*LBRR decode*/
  410. out_samples = opus_decode(dec_err[0], packet, len, out2buf, frame_size, (fast_rand()&3)!=0);
  411. if(out_samples!=frame_size)test_failed();
  412. out_samples = opus_decode(dec_err[1], packet, (fast_rand()&3)==0?0:len, out2buf, MAX_FRAME_SAMP, (fast_rand()&7)!=0);
  413. if(out_samples<120)test_failed();
  414. i+=frame_size;
  415. count++;
  416. }while(i<(SSAMPLES-MAX_FRAME_SAMP));
  417. fprintf(stdout," Mode %s FB encode %s, %6d bps OK.\n",mstrings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
  418. }
  419. }
  420. if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(OPUS_AUTO))!=OPUS_OK)test_failed();
  421. if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO))!=OPUS_OK)test_failed();
  422. if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(0))!=OPUS_OK)test_failed();
  423. if(opus_encoder_ctl(enc, OPUS_SET_DTX(0))!=OPUS_OK)test_failed();
  424. for(rc=0;rc<3;rc++)
  425. {
  426. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
  427. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
  428. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
  429. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
  430. for(j=0;j<16;j++)
  431. {
  432. int rate;
  433. int modes[16]={0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2};
  434. int rates[16]={4000,12000,32000,8000,16000,32000,48000,88000,4000,12000,32000,8000,16000,32000,48000,88000};
  435. int frame[16]={160*1,160,80,160,160,80,40,20,160*1,160,80,160,160,80,40,20};
  436. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0&&j==1))!=OPUS_OK)test_failed();
  437. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j]))!=OPUS_OK)test_failed();
  438. rate=rates[j]+fast_rand()%rates[j];
  439. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_failed();
  440. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_failed();
  441. count=i=0;
  442. do {
  443. int len,out_samples,frame_size,loss;
  444. opus_int32 pred;
  445. if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_PREDICTION_DISABLED(&pred))!=OPUS_OK)test_failed();
  446. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_PREDICTION_DISABLED((int)(fast_rand()&15)<(pred?11:4)))!=OPUS_OK)test_failed();
  447. frame_size=frame[j];
  448. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_OK)test_failed();
  449. if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
  450. if((fast_rand()&255)==0)
  451. {
  452. if(opus_multistream_encoder_ctl(MSenc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  453. if(opus_multistream_decoder_ctl(MSdec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  454. if((fast_rand()&3)!=0)
  455. {
  456. if(opus_multistream_decoder_ctl(MSdec_err, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  457. }
  458. }
  459. if((fast_rand()&255)==0)
  460. {
  461. if(opus_multistream_decoder_ctl(MSdec_err, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  462. }
  463. len = opus_multistream_encode(MSenc, &inbuf[i<<1], frame_size, packet, MAX_PACKET);
  464. if(len<0 || len>MAX_PACKET)test_failed();
  465. if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
  466. if((fast_rand()&3)==0)
  467. {
  468. if(opus_multistream_packet_pad(packet,len,len+1,2)!=OPUS_OK)test_failed();
  469. len++;
  470. }
  471. if((fast_rand()&7)==0)
  472. {
  473. if(opus_multistream_packet_pad(packet,len,len+256,2)!=OPUS_OK)test_failed();
  474. len+=256;
  475. }
  476. if((fast_rand()&3)==0)
  477. {
  478. len=opus_multistream_packet_unpad(packet,len,2);
  479. if(len<1)test_failed();
  480. }
  481. out_samples = opus_multistream_decode(MSdec, packet, len, out2buf, MAX_FRAME_SAMP, 0);
  482. if(out_samples!=frame_size*6)test_failed();
  483. if(opus_multistream_decoder_ctl(MSdec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
  484. if(enc_final_range!=dec_final_range)test_failed();
  485. /*LBRR decode*/
  486. loss=(fast_rand()&63)==0;
  487. out_samples = opus_multistream_decode(MSdec_err, packet, loss?0:len, out2buf, frame_size*6, (fast_rand()&3)!=0);
  488. if(out_samples!=(frame_size*6))test_failed();
  489. i+=frame_size;
  490. count++;
  491. }while(i<(SSAMPLES/12-MAX_FRAME_SAMP));
  492. fprintf(stdout," Mode %s NB dual-mono MS encode %s, %6d bps OK.\n",mstrings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
  493. }
  494. }
  495. bitrate_bps=512000;
  496. fsize=fast_rand()%31;
  497. fswitch=100;
  498. debruijn2(6,db62);
  499. count=i=0;
  500. do {
  501. unsigned char toc;
  502. const unsigned char *frames[48];
  503. short size[48];
  504. int payload_offset;
  505. opus_uint32 dec_final_range2;
  506. int jj,dec2;
  507. int len,out_samples;
  508. int frame_size=fsizes[db62[fsize]];
  509. opus_int32 offset=i%(SAMPLES-MAX_FRAME_SAMP);
  510. opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps));
  511. len = opus_encode(enc, &inbuf[offset<<1], frame_size, packet, MAX_PACKET);
  512. if(len<0 || len>MAX_PACKET)test_failed();
  513. count++;
  514. opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range));
  515. out_samples = opus_decode(dec, packet, len, &outbuf[offset<<1], MAX_FRAME_SAMP, 0);
  516. if(out_samples!=frame_size)test_failed();
  517. opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
  518. /* compare final range encoder rng values of encoder and decoder */
  519. if(dec_final_range!=enc_final_range)test_failed();
  520. /* We fuzz the packet, but take care not to only corrupt the payload
  521. Corrupted headers are tested elsewhere and we need to actually run
  522. the decoders in order to compare them. */
  523. if(opus_packet_parse(packet,len,&toc,frames,size,&payload_offset)<=0)test_failed();
  524. if((fast_rand()&1023)==0)len=0;
  525. for(j=(opus_int32)(frames[0]-packet);j<len;j++)for(jj=0;jj<8;jj++)packet[j]^=((!no_fuzz)&&((fast_rand()&1023)==0))<<jj;
  526. out_samples = opus_decode(dec_err[0], len>0?packet:NULL, len, out2buf, MAX_FRAME_SAMP, 0);
  527. if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed();
  528. if((len>0&&out_samples!=frame_size))test_failed(); /*FIXME use lastframe*/
  529. opus_decoder_ctl(dec_err[0], OPUS_GET_FINAL_RANGE(&dec_final_range));
  530. /*randomly select one of the decoders to compare with*/
  531. dec2=fast_rand()%9+1;
  532. out_samples = opus_decode(dec_err[dec2], len>0?packet:NULL, len, out2buf, MAX_FRAME_SAMP, 0);
  533. if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed(); /*FIXME, use factor, lastframe for loss*/
  534. opus_decoder_ctl(dec_err[dec2], OPUS_GET_FINAL_RANGE(&dec_final_range2));
  535. if(len>0&&dec_final_range!=dec_final_range2)test_failed();
  536. fswitch--;
  537. if(fswitch<1)
  538. {
  539. int new_size;
  540. fsize=(fsize+1)%36;
  541. new_size=fsizes[db62[fsize]];
  542. if(new_size==960||new_size==480)fswitch=2880/new_size*(fast_rand()%19+1);
  543. else fswitch=(fast_rand()%(2880/new_size))+1;
  544. }
  545. bitrate_bps=((fast_rand()%508000+4000)+bitrate_bps)>>1;
  546. i+=frame_size;
  547. }while(i<SAMPLES*4);
  548. fprintf(stdout," All framesize pairs switching encode, %d frames OK.\n",count);
  549. if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  550. opus_encoder_destroy(enc);
  551. if(opus_multistream_encoder_ctl(MSenc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  552. opus_multistream_encoder_destroy(MSenc);
  553. if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  554. opus_decoder_destroy(dec);
  555. if(opus_multistream_decoder_ctl(MSdec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  556. opus_multistream_decoder_destroy(MSdec);
  557. opus_multistream_decoder_destroy(MSdec_err);
  558. for(i=0;i<10;i++)opus_decoder_destroy(dec_err[i]);
  559. free(inbuf);
  560. free(outbuf);
  561. free(out2buf);
  562. return 0;
  563. }
  564. void print_usage(char* _argv[])
  565. {
  566. fprintf(stderr,"Usage: %s [<seed>] [-fuzz <num_encoders> <num_settings_per_encoder>]\n",_argv[0]);
  567. }
  568. int main(int _argc, char **_argv)
  569. {
  570. int args=1;
  571. char * strtol_str=NULL;
  572. const char * oversion;
  573. const char * env_seed;
  574. int env_used;
  575. int num_encoders_to_fuzz=5;
  576. int num_setting_changes=40;
  577. env_used=0;
  578. env_seed=getenv("SEED");
  579. if(_argc>1)
  580. iseed=strtol(_argv[1], &strtol_str, 10); /* the first input argument might be the seed */
  581. if(strtol_str!=NULL && strtol_str[0]=='\0') /* iseed is a valid number */
  582. args++;
  583. else if(env_seed) {
  584. iseed=atoi(env_seed);
  585. env_used=1;
  586. }
  587. else iseed=(opus_uint32)time(NULL)^(((opus_uint32)getpid()&65535)<<16);
  588. Rw=Rz=iseed;
  589. while(args<_argc)
  590. {
  591. if(strcmp(_argv[args], "-fuzz")==0 && _argc==(args+3)) {
  592. num_encoders_to_fuzz=strtol(_argv[args+1], &strtol_str, 10);
  593. if(strtol_str[0]!='\0' || num_encoders_to_fuzz<=0) {
  594. print_usage(_argv);
  595. return EXIT_FAILURE;
  596. }
  597. num_setting_changes=strtol(_argv[args+2], &strtol_str, 10);
  598. if(strtol_str[0]!='\0' || num_setting_changes<=0) {
  599. print_usage(_argv);
  600. return EXIT_FAILURE;
  601. }
  602. args+=3;
  603. }
  604. else {
  605. print_usage(_argv);
  606. return EXIT_FAILURE;
  607. }
  608. }
  609. oversion=opus_get_version_string();
  610. if(!oversion)test_failed();
  611. fprintf(stderr,"Testing %s encoder. Random seed: %u (%.4X)\n", oversion, iseed, fast_rand() % 65535);
  612. if(env_used)fprintf(stderr," Random seed set from the environment (SEED=%s).\n", env_seed);
  613. regression_test();
  614. /*Setting TEST_OPUS_NOFUZZ tells the tool not to send garbage data
  615. into the decoders. This is helpful because garbage data
  616. may cause the decoders to clip, which angers CLANG IOC.*/
  617. run_test1(getenv("TEST_OPUS_NOFUZZ")!=NULL);
  618. /* Fuzz encoder settings online */
  619. if(getenv("TEST_OPUS_NOFUZZ")==NULL) {
  620. fprintf(stderr,"Running fuzz_encoder_settings with %d encoder(s) and %d setting change(s) each.\n",
  621. num_encoders_to_fuzz, num_setting_changes);
  622. fuzz_encoder_settings(num_encoders_to_fuzz, num_setting_changes);
  623. }
  624. fprintf(stderr,"Tests completed successfully.\n");
  625. return 0;
  626. }