Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: opus/tests/test_opus_encode.c

Issue 11196031: Add copy of opus library in deps/third_party. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « opus/tests/test_opus_decode.c ('k') | opus/win32/config.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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_multistream.h"
43 #include "opus.h"
44 #include "../src/opus_private.h"
45 #include "test_opus_common.h"
46
47 #define MAX_PACKET (1500)
48 #define SAMPLES (48000*30)
49 #define SSAMPLES (SAMPLES/3)
50 #define MAX_FRAME_SAMP (5760)
51
52 #define PI (3.141592653589793238462643f)
53
54 void generate_music(short *buf, opus_int32 len)
55 {
56 opus_int32 a1,b1,a2,b2;
57 opus_int32 c1,c2,d1,d2;
58 opus_int32 i,j;
59 a1=b1=a2=b2=0;
60 c1=c2=d1=d2=0;
61 j=0;
62 /*60ms silence*/
63 for(i=0;i<2880;i++)buf[i*2]=buf[i*2+1]=0;
64 for(i=2880;i<len;i++)
65 {
66 opus_uint32 r;
67 opus_int32 v1,v2;
68 v1=v2=(((j*((j>>12)^((j>>10|j>>12)&26&j>>7)))&128)+128)<<15;
69 r=fast_rand();v1+=r&65535;v1-=r>>16;
70 r=fast_rand();v2+=r&65535;v2-=r>>16;
71 b1=v1-a1+((b1*61+32)>>6);a1=v1;
72 b2=v2-a2+((b2*61+32)>>6);a2=v2;
73 c1=(30*(c1+b1+d1)+32)>>6;d1=b1;
74 c2=(30*(c2+b2+d2)+32)>>6;d2=b2;
75 v1=(c1+128)>>8;
76 v2=(c2+128)>>8;
77 buf[i*2]=v1>32767?32767:(v1<-32768?-32768:v1);
78 buf[i*2+1]=v2>32767?32767:(v2<-32768?-32768:v2);
79 if(i%6==0)j++;
80 }
81 }
82
83 #if 0
84 static int save_ctr = 0;
85 static void int_to_char(opus_uint32 i, unsigned char ch[4])
86 {
87 ch[0] = i>>24;
88 ch[1] = (i>>16)&0xFF;
89 ch[2] = (i>>8)&0xFF;
90 ch[3] = i&0xFF;
91 }
92
93 static inline void save_packet(unsigned char* p, int len, opus_uint32 rng)
94 {
95 FILE *fout;
96 unsigned char int_field[4];
97 char name[256];
98 snprintf(name,255,"test_opus_encode.%llu.%d.bit",(unsigned long long)iseed,sa ve_ctr);
99 fprintf(stdout,"writing %d byte packet to %s\n",len,name);
100 fout=fopen(name, "wb+");
101 if(fout==NULL)test_failed();
102 int_to_char(len, int_field);
103 fwrite(int_field, 1, 4, fout);
104 int_to_char(rng, int_field);
105 fwrite(int_field, 1, 4, fout);
106 fwrite(p, 1, len, fout);
107 fclose(fout);
108 save_ctr++;
109 }
110 #endif
111
112 int run_test1(int no_fuzz)
113 {
114 static const int fsizes[6]={960*3,960*2,120,240,480,960};
115 static const char *mstrings[3] = {" LP","Hybrid"," MDCT"};
116 unsigned char mapping[256] = {0,1,255};
117 unsigned char db62[36];
118 opus_int32 i;
119 int rc,j,err;
120 OpusEncoder *enc;
121 OpusMSEncoder *MSenc;
122 OpusDecoder *dec;
123 OpusMSDecoder *MSdec;
124 OpusMSDecoder *MSdec_err;
125 OpusDecoder *dec_err[10];
126 short *inbuf;
127 short *outbuf;
128 short *out2buf;
129 opus_int32 bitrate_bps;
130 unsigned char packet[MAX_PACKET];
131 opus_uint32 enc_final_range;
132 opus_uint32 dec_final_range;
133 int fswitch;
134 int fsize;
135 int count;
136
137 /*FIXME: encoder api tests, fs!=48k, mono, VBR*/
138
139 fprintf(stdout," Encode+Decode tests.\n");
140
141 enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
142 if(err != OPUS_OK || enc==NULL)test_failed();
143
144 MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_APPLICAT ION_AUDIO, &err);
145 if(err != OPUS_OK || MSenc==NULL)test_failed();
146
147 if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_BITRATE(&i))!=OPUS_OK)test_fa iled();
148 if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_LSB_DEPTH(&i))!=OPUS_OK)test_ failed();
149
150 dec = opus_decoder_create(48000, 2, &err);
151 if(err != OPUS_OK || dec==NULL)test_failed();
152
153 MSdec = opus_multistream_decoder_create(48000, 2, 2, 0, mapping, &err);
154 if(err != OPUS_OK || MSdec==NULL)test_failed();
155
156 MSdec_err = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, &err);
157 if(err != OPUS_OK || MSdec_err==NULL)test_failed();
158
159 dec_err[0]=(OpusDecoder *)malloc(opus_decoder_get_size(2));
160 memcpy(dec_err[0],dec,opus_decoder_get_size(2));
161 dec_err[1] = opus_decoder_create(48000, 1, &err);
162 dec_err[2] = opus_decoder_create(24000, 2, &err);
163 dec_err[3] = opus_decoder_create(24000, 1, &err);
164 dec_err[4] = opus_decoder_create(16000, 2, &err);
165 dec_err[5] = opus_decoder_create(16000, 1, &err);
166 dec_err[6] = opus_decoder_create(12000, 2, &err);
167 dec_err[7] = opus_decoder_create(12000, 1, &err);
168 dec_err[8] = opus_decoder_create(8000, 2, &err);
169 dec_err[9] = opus_decoder_create(8000, 1, &err);
170 for(i=0;i<10;i++)if(dec_err[i]==NULL)test_failed();
171
172 {
173 OpusEncoder *enccpy;
174 /*The opus state structures contain no pointers and can be freely copied*/
175 enccpy=(OpusEncoder *)malloc(opus_encoder_get_size(2));
176 memcpy(enccpy,enc,opus_encoder_get_size(2));
177 memset(enc,255,opus_encoder_get_size(2));
178 opus_encoder_destroy(enc);
179 enc=enccpy;
180 }
181
182 inbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
183 outbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
184 out2buf=(short *)malloc(sizeof(short)*MAX_FRAME_SAMP*3);
185 if(inbuf==NULL || outbuf==NULL || out2buf==NULL)test_failed();
186
187 generate_music(inbuf,SAMPLES);
188
189 /* FILE *foo;
190 foo = fopen("foo.sw", "wb+");
191 fwrite(inbuf, 1, SAMPLES*2*2, foo);
192 fclose(foo);*/
193
194 if(opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed( );
195 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(-2))!=OPUS_BAD_ARG)test_failed() ;
196
197 for(rc=0;rc<3;rc++)
198 {
199 if(opus_encoder_ctl(enc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
200 if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_fai led();
201 if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_fai led();
202 if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed( );
203 for(j=0;j<13;j++)
204 {
205 int rate;
206 int modes[13]={0,0,0,1,1,1,1,2,2,2,2,2,2};
207 int rates[13]={6000,12000,48000,16000,32000,48000,64000,512000,13000,24 000,48000,64000,96000};
208 int frame[13]={960*2,960,480,960,960,960,480,960*3,960*3,960,480,240,12 0};
209 rate=rates[j]+fast_rand()%rates[j];
210 count=i=0;
211 do {
212 int bw,len,out_samples,frame_size;
213 frame_size=frame[j];
214 if(fast_rand()%50==0)opus_encoder_ctl(enc, OPUS_RESET_STATE);
215 if(fast_rand()%50==0)opus_decoder_ctl(dec, OPUS_RESET_STATE);
216 if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_f ailed();
217 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j] ))!=OPUS_OK)test_failed();
218 if(opus_encoder_ctl(enc, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_ failed();
219 if(opus_encoder_ctl(enc, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_faile d();
220 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS((rates[j]>=64000?2: 1)))!=OPUS_OK)test_failed();
221 if(opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_O K)test_failed();
222 if(opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)& (fast_rand()%15)))!=OPUS_OK)test_failed();
223 bw=modes[j]==0?OPUS_BANDWIDTH_NARROWBAND+(fast_rand()%3):
224 modes[j]==1?OPUS_BANDWIDTH_SUPERWIDEBAND+(fast_rand()&1):
225 OPUS_BANDWIDTH_NARROWBAND+(fast_rand()%5);
226 if(modes[j]==2&&bw==OPUS_BANDWIDTH_MEDIUMBAND)bw+=3;
227 if(opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(bw))!=OPUS_OK)test_faile d();
228 len = opus_encode(enc, &inbuf[i<<1], frame_size, packet, MAX_PACKET) ;
229 if(len<0 || len>MAX_PACKET)test_failed();
230 if(opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OP US_OK)test_failed();
231 out_samples = opus_decode(dec, packet, len, &outbuf[i<<1], MAX_FRAME _SAMP, 0);
232 if(out_samples!=frame_size)test_failed();
233 if(opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OP US_OK)test_failed();
234 if(enc_final_range!=dec_final_range)test_failed();
235 /*LBRR decode*/
236 out_samples = opus_decode(dec_err[0], packet, len, out2buf, MAX_FRAM E_SAMP, (fast_rand()&3)!=0);
237 if(out_samples!=frame_size)test_failed();
238 out_samples = opus_decode(dec_err[1], packet, (fast_rand()&3)==0?0:l en, out2buf, MAX_FRAME_SAMP, (fast_rand()&7)!=0);
239 if(out_samples<120)test_failed();
240 i+=frame_size;
241 count++;
242 }while(i<(SSAMPLES-MAX_FRAME_SAMP));
243 fprintf(stdout," Mode %s FB encode %s, %6d bps OK.\n",mstrings[modes [j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
244 }
245 }
246
247 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(OPUS_AUTO))!=OPUS_OK)test_failed ();
248 if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO))!=OPUS_OK)test_fa iled();
249 if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(0))!=OPUS_OK)test_failed();
250 if(opus_encoder_ctl(enc, OPUS_SET_DTX(0))!=OPUS_OK)test_failed();
251
252 for(rc=0;rc<3;rc++)
253 {
254 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_f ailed();
255 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OP US_OK)test_failed();
256 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OP US_OK)test_failed();
257 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_O K)test_failed();
258 for(j=0;j<16;j++)
259 {
260 int rate;
261 int modes[16]={0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2};
262 int rates[16]={4000,12000,32000,8000,16000,32000,48000,88000,4000,12000 ,32000,8000,16000,32000,48000,88000};
263 int frame[16]={160*1,160,80,160,160,80,40,20,160*1,160,80,160,160,80,40 ,20};
264 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0&&j==1) )!=OPUS_OK)test_failed();
265 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_FORCE_MODE(MODE_SILK_ON LY+modes[j]))!=OPUS_OK)test_failed();
266 rate=rates[j]+fast_rand()%rates[j];
267 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_DTX(fast_rand()&1))!=OP US_OK)test_failed();
268 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_BITRATE(rate))!=OPUS_OK )test_failed();
269 count=i=0;
270 do {
271 int len,out_samples,frame_size,loss;
272 frame_size=frame[j];
273 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_COMPLEXITY((count>>2 )%11))!=OPUS_OK)test_failed();
274 if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_PACKET_LOSS_PERC((fa st_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
275 len = opus_multistream_encode(MSenc, &inbuf[i<<1], frame_size, packe t, MAX_PACKET);
276 if(len<0 || len>MAX_PACKET)test_failed();
277 if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_FINAL_RANGE(&enc_fin al_range))!=OPUS_OK)test_failed();
278 out_samples = opus_multistream_decode(MSdec, packet, len, out2buf, M AX_FRAME_SAMP, 0);
279 if(out_samples!=frame_size*6)test_failed();
280 if(opus_multistream_decoder_ctl(MSdec, OPUS_GET_FINAL_RANGE(&dec_fin al_range))!=OPUS_OK)test_failed();
281 if(enc_final_range!=dec_final_range)test_failed();
282 /*LBRR decode*/
283 loss=(fast_rand()&63)==0;
284 out_samples = opus_multistream_decode(MSdec_err, packet, loss?0:len, out2buf, MAX_FRAME_SAMP, (fast_rand()&3)!=0);
285 if(loss?out_samples<120:out_samples!=(frame_size*6))test_failed();
286 i+=frame_size;
287 count++;
288 }while(i<(SSAMPLES/12-MAX_FRAME_SAMP));
289 fprintf(stdout," Mode %s NB dual-mono MS encode %s, %6d bps OK.\n",m strings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
290 }
291 }
292
293 bitrate_bps=512000;
294 fsize=fast_rand()%31;
295 fswitch=100;
296
297 debruijn2(6,db62);
298 count=i=0;
299 do {
300 unsigned char toc;
301 const unsigned char *frames[48];
302 short size[48];
303 int payload_offset;
304 opus_uint32 dec_final_range2;
305 int jj,dec2;
306 int len,out_samples;
307 int frame_size=fsizes[db62[fsize]];
308 opus_int32 offset=i%(SAMPLES-MAX_FRAME_SAMP);
309
310 opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps));
311
312 len = opus_encode(enc, &inbuf[offset<<1], frame_size, packet, MAX_PACKET);
313 if(len<0 || len>MAX_PACKET)test_failed();
314 count++;
315
316 opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range));
317
318 out_samples = opus_decode(dec, packet, len, &outbuf[offset<<1], MAX_FRAME_ SAMP, 0);
319 if(out_samples!=frame_size)test_failed();
320
321 opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
322
323 /* compare final range encoder rng values of encoder and decoder */
324 if(dec_final_range!=enc_final_range)test_failed();
325
326 /* We fuzz the packet, but take care not to only corrupt the payload
327 Corrupted headers are tested elsewhere and we need to actually run
328 the decoders in order to compare them. */
329 if(opus_packet_parse(packet,len,&toc,frames,size,&payload_offset)<=0)test_ failed();
330 if((fast_rand()&1023)==0)len=0;
331 for(j=(frames[0]-packet);j<len;j++)for(jj=0;jj<8;jj++)packet[j]^=((!no_fuz z)&&((fast_rand()&1023)==0))<<jj;
332 out_samples = opus_decode(dec_err[0], len>0?packet:NULL, len, out2buf, MAX _FRAME_SAMP, 0);
333 if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed();
334 if((len>0&&out_samples!=frame_size))test_failed(); /*FIXME use lastframe*/
335
336 opus_decoder_ctl(dec_err[0], OPUS_GET_FINAL_RANGE(&dec_final_range));
337
338 /*randomly select one of the decoders to compare with*/
339 dec2=fast_rand()%9+1;
340 out_samples = opus_decode(dec_err[dec2], len>0?packet:NULL, len, out2buf, MAX_FRAME_SAMP, 0);
341 if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed(); /*FIXME, use f actor, lastframe for loss*/
342
343 opus_decoder_ctl(dec_err[dec2], OPUS_GET_FINAL_RANGE(&dec_final_range2));
344 if(len>0&&dec_final_range!=dec_final_range2)test_failed();
345
346 fswitch--;
347 if(fswitch<1)
348 {
349 int new_size;
350 fsize=(fsize+1)%36;
351 new_size=fsizes[db62[fsize]];
352 if(new_size==960||new_size==480)fswitch=2880/new_size*(fast_rand()%19+1) ;
353 else fswitch=(fast_rand()%(2880/new_size))+1;
354 }
355 bitrate_bps=((fast_rand()%508000+4000)+bitrate_bps)>>1;
356 i+=frame_size;
357 }while(i<SAMPLES*4);
358 fprintf(stdout," All framesize pairs switching encode, %d frames OK.\n",co unt);
359
360 opus_encoder_destroy(enc);
361 opus_multistream_encoder_destroy(MSenc);
362 opus_decoder_destroy(dec);
363 opus_multistream_decoder_destroy(MSdec);
364 opus_multistream_decoder_destroy(MSdec_err);
365 for(i=0;i<10;i++)opus_decoder_destroy(dec_err[i]);
366 free(inbuf);
367 free(outbuf);
368 free(out2buf);
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 encoder. Random seed: %u (%.4X)\n", oversion, isee d, 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 run_test1(getenv("TEST_OPUS_NOFUZZ")!=NULL);
404
405 fprintf(stderr,"Tests completed successfully.\n");
406
407 return 0;
408 }
OLDNEW
« no previous file with comments | « opus/tests/test_opus_decode.c ('k') | opus/win32/config.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698