OLD | NEW |
1 /* Copyright (c) 2007-2008 CSIRO | 1 /* Copyright (c) 2007-2008 CSIRO |
2 Copyright (c) 2007-2009 Xiph.Org Foundation | 2 Copyright (c) 2007-2009 Xiph.Org Foundation |
3 Written by Jean-Marc Valin */ | 3 Written by Jean-Marc Valin */ |
4 /* | 4 /* |
5 Redistribution and use in source and binary forms, with or without | 5 Redistribution and use in source and binary forms, with or without |
6 modification, are permitted provided that the following conditions | 6 modification, are permitted provided that the following conditions |
7 are met: | 7 are met: |
8 | 8 |
9 - Redistributions of source code must retain the above copyright | 9 - Redistributions of source code must retain the above copyright |
10 notice, this list of conditions and the following disclaimer. | 10 notice, this list of conditions and the following disclaimer. |
(...skipping 20 matching lines...) Expand all Loading... |
31 #endif | 31 #endif |
32 | 32 |
33 #include <stdio.h> | 33 #include <stdio.h> |
34 #include <stdlib.h> | 34 #include <stdlib.h> |
35 #include <math.h> | 35 #include <math.h> |
36 #include <string.h> | 36 #include <string.h> |
37 #include "opus.h" | 37 #include "opus.h" |
38 #include "debug.h" | 38 #include "debug.h" |
39 #include "opus_types.h" | 39 #include "opus_types.h" |
40 #include "opus_private.h" | 40 #include "opus_private.h" |
| 41 #include "opus_multistream.h" |
41 | 42 |
42 #define MAX_PACKET 1500 | 43 #define MAX_PACKET 1500 |
43 | 44 |
44 void print_usage( char* argv[] ) | 45 void print_usage( char* argv[] ) |
45 { | 46 { |
46 fprintf(stderr, "Usage: %s [-e] <application> <sampling rate (Hz)> <channels
(1/2)> " | 47 fprintf(stderr, "Usage: %s [-e] <application> <sampling rate (Hz)> <channels
(1/2)> " |
47 "<bits per second> [options] <input> <output>\n", argv[0]); | 48 "<bits per second> [options] <input> <output>\n", argv[0]); |
48 fprintf(stderr, " %s -d <sampling rate (Hz)> <channels (1/2)> " | 49 fprintf(stderr, " %s -d <sampling rate (Hz)> <channels (1/2)> " |
49 "[options] <input> <output>\n\n", argv[0]); | 50 "[options] <input> <output>\n\n", argv[0]); |
50 fprintf(stderr, "mode: voip | audio | restricted-lowdelay\n" ); | 51 fprintf(stderr, "mode: voip | audio | restricted-lowdelay\n" ); |
51 fprintf(stderr, "options:\n" ); | 52 fprintf(stderr, "options:\n" ); |
52 fprintf(stderr, "-e : only runs the encoder (output the bi
t-stream)\n" ); | 53 fprintf(stderr, "-e : only runs the encoder (output the bi
t-stream)\n" ); |
53 fprintf(stderr, "-d : only runs the decoder (reads the bit
-stream as input)\n" ); | 54 fprintf(stderr, "-d : only runs the decoder (reads the bit
-stream as input)\n" ); |
54 fprintf(stderr, "-cbr : enable constant bitrate; default: va
riable bitrate\n" ); | 55 fprintf(stderr, "-cbr : enable constant bitrate; default: va
riable bitrate\n" ); |
55 fprintf(stderr, "-cvbr : enable constrained variable bitrate;
default: unconstrained\n" ); | 56 fprintf(stderr, "-cvbr : enable constrained variable bitrate;
default: unconstrained\n" ); |
| 57 fprintf(stderr, "-variable-duration : enable frames of variable duration (
experts only); default: disabled\n" ); |
56 fprintf(stderr, "-bandwidth <NB|MB|WB|SWB|FB> : audio bandwidth (from narrow
band to fullband); default: sampling rate\n" ); | 58 fprintf(stderr, "-bandwidth <NB|MB|WB|SWB|FB> : audio bandwidth (from narrow
band to fullband); default: sampling rate\n" ); |
57 fprintf(stderr, "-framesize <2.5|5|10|20|40|60> : frame size in ms; default:
20 \n" ); | 59 fprintf(stderr, "-framesize <2.5|5|10|20|40|60> : frame size in ms; default:
20 \n" ); |
58 fprintf(stderr, "-max_payload <bytes> : maximum payload size in bytes, defau
lt: 1024\n" ); | 60 fprintf(stderr, "-max_payload <bytes> : maximum payload size in bytes, defau
lt: 1024\n" ); |
59 fprintf(stderr, "-complexity <comp> : complexity, 0 (lowest) ... 10 (highe
st); default: 10\n" ); | 61 fprintf(stderr, "-complexity <comp> : complexity, 0 (lowest) ... 10 (highe
st); default: 10\n" ); |
60 fprintf(stderr, "-inbandfec : enable SILK inband FEC\n" ); | 62 fprintf(stderr, "-inbandfec : enable SILK inband FEC\n" ); |
61 fprintf(stderr, "-forcemono : force mono encoding, even for stereo
input\n" ); | 63 fprintf(stderr, "-forcemono : force mono encoding, even for stereo
input\n" ); |
62 fprintf(stderr, "-dtx : enable SILK DTX\n" ); | 64 fprintf(stderr, "-dtx : enable SILK DTX\n" ); |
63 fprintf(stderr, "-loss <perc> : simulate packet loss, in percent (0-
100); default: 0\n" ); | 65 fprintf(stderr, "-loss <perc> : simulate packet loss, in percent (0-
100); default: 0\n" ); |
64 } | 66 } |
65 | 67 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 | 187 |
186 }; | 188 }; |
187 | 189 |
188 static const int celt_hq_test[][4] = { | 190 static const int celt_hq_test[][4] = { |
189 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 2}, | 191 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 2}, |
190 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 2}, | 192 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 2}, |
191 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 240, 2}, | 193 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 240, 2}, |
192 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 120, 2}, | 194 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 120, 2}, |
193 }; | 195 }; |
194 | 196 |
| 197 #if 0 /* This is a hack that replaces the normal encoder/decoder with the multis
tream version */ |
| 198 #define OpusEncoder OpusMSEncoder |
| 199 #define OpusDecoder OpusMSDecoder |
| 200 #define opus_encode opus_multistream_encode |
| 201 #define opus_decode opus_multistream_decode |
| 202 #define opus_encoder_ctl opus_multistream_encoder_ctl |
| 203 #define opus_decoder_ctl opus_multistream_decoder_ctl |
| 204 #define opus_encoder_create ms_opus_encoder_create |
| 205 #define opus_decoder_create ms_opus_decoder_create |
| 206 #define opus_encoder_destroy opus_multistream_encoder_destroy |
| 207 #define opus_decoder_destroy opus_multistream_decoder_destroy |
| 208 |
| 209 static OpusEncoder *ms_opus_encoder_create(opus_int32 Fs, int channels, int appl
ication, int *error) |
| 210 { |
| 211 int streams, coupled_streams; |
| 212 unsigned char mapping[256]; |
| 213 return (OpusEncoder *)opus_multistream_surround_encoder_create(Fs, channels,
1, &streams, &coupled_streams, mapping, application, error); |
| 214 } |
| 215 static OpusDecoder *ms_opus_decoder_create(opus_int32 Fs, int channels, int *err
or) |
| 216 { |
| 217 int streams; |
| 218 int coupled_streams; |
| 219 unsigned char mapping[256]={0,1}; |
| 220 streams = 1; |
| 221 coupled_streams = channels==2; |
| 222 return (OpusDecoder *)opus_multistream_decoder_create(Fs, channels, streams,
coupled_streams, mapping, error); |
| 223 } |
| 224 #endif |
| 225 |
195 int main(int argc, char *argv[]) | 226 int main(int argc, char *argv[]) |
196 { | 227 { |
197 int err; | 228 int err; |
198 char *inFile, *outFile; | 229 char *inFile, *outFile; |
199 FILE *fin, *fout; | 230 FILE *fin, *fout; |
200 OpusEncoder *enc=NULL; | 231 OpusEncoder *enc=NULL; |
201 OpusDecoder *dec=NULL; | 232 OpusDecoder *dec=NULL; |
202 int args; | 233 int args; |
203 int len[2]; | 234 int len[2]; |
204 int frame_size, channels; | 235 int frame_size, channels; |
205 opus_int32 bitrate_bps=0; | 236 opus_int32 bitrate_bps=0; |
206 unsigned char *data[2]; | 237 unsigned char *data[2]; |
207 unsigned char *fbytes; | 238 unsigned char *fbytes; |
208 opus_int32 sampling_rate; | 239 opus_int32 sampling_rate; |
209 int use_vbr; | 240 int use_vbr; |
210 int max_payload_bytes; | 241 int max_payload_bytes; |
211 int complexity; | 242 int complexity; |
212 int use_inbandfec; | 243 int use_inbandfec; |
213 int use_dtx; | 244 int use_dtx; |
214 int forcechannels; | 245 int forcechannels; |
215 int cvbr = 0; | 246 int cvbr = 0; |
216 int packet_loss_perc; | 247 int packet_loss_perc; |
217 opus_int32 count=0, count_act=0; | 248 opus_int32 count=0, count_act=0; |
218 int k; | 249 int k; |
219 opus_int32 skip=0; | 250 opus_int32 skip=0; |
220 int stop=0; | 251 int stop=0; |
221 short *in, *out; | 252 short *in, *out; |
222 int application=OPUS_APPLICATION_AUDIO; | 253 int application=OPUS_APPLICATION_AUDIO; |
223 double bits=0.0, bits_max=0.0, bits_act=0.0, bits2=0.0, nrg; | 254 double bits=0.0, bits_max=0.0, bits_act=0.0, bits2=0.0, nrg; |
| 255 double tot_samples=0; |
| 256 opus_uint64 tot_in, tot_out; |
224 int bandwidth=-1; | 257 int bandwidth=-1; |
225 const char *bandwidth_string; | 258 const char *bandwidth_string; |
226 int lost = 0, lost_prev = 1; | 259 int lost = 0, lost_prev = 1; |
227 int toggle = 0; | 260 int toggle = 0; |
228 opus_uint32 enc_final_range[2]; | 261 opus_uint32 enc_final_range[2]; |
229 opus_uint32 dec_final_range; | 262 opus_uint32 dec_final_range; |
230 int encode_only=0, decode_only=0; | 263 int encode_only=0, decode_only=0; |
231 int max_frame_size = 960*6; | 264 int max_frame_size = 960*6; |
232 int curr_read=0; | 265 int curr_read=0; |
233 int sweep_bps = 0; | 266 int sweep_bps = 0; |
234 int random_framesize=0, newsize=0, delayed_celt=0; | 267 int random_framesize=0, newsize=0, delayed_celt=0; |
235 int sweep_max=0, sweep_min=0; | 268 int sweep_max=0, sweep_min=0; |
236 int random_fec=0; | 269 int random_fec=0; |
237 const int (*mode_list)[4]=NULL; | 270 const int (*mode_list)[4]=NULL; |
238 int nb_modes_in_list=0; | 271 int nb_modes_in_list=0; |
239 int curr_mode=0; | 272 int curr_mode=0; |
240 int curr_mode_count=0; | 273 int curr_mode_count=0; |
241 int mode_switch_time = 48000; | 274 int mode_switch_time = 48000; |
| 275 int nb_encoded=0; |
| 276 int remaining=0; |
| 277 int variable_duration=OPUS_FRAMESIZE_ARG; |
| 278 int delayed_decision=0; |
242 | 279 |
243 if (argc < 5 ) | 280 if (argc < 5 ) |
244 { | 281 { |
245 print_usage( argv ); | 282 print_usage( argv ); |
246 return EXIT_FAILURE; | 283 return EXIT_FAILURE; |
247 } | 284 } |
248 | 285 |
| 286 tot_in=tot_out=0; |
249 fprintf(stderr, "%s\n", opus_get_version_string()); | 287 fprintf(stderr, "%s\n", opus_get_version_string()); |
250 | 288 |
251 args = 1; | 289 args = 1; |
252 if (strcmp(argv[args], "-e")==0) | 290 if (strcmp(argv[args], "-e")==0) |
253 { | 291 { |
254 encode_only = 1; | 292 encode_only = 1; |
255 args++; | 293 args++; |
256 } else if (strcmp(argv[args], "-d")==0) | 294 } else if (strcmp(argv[args], "-d")==0) |
257 { | 295 { |
258 decode_only = 1; | 296 decode_only = 1; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 | 337 |
300 /* defaults: */ | 338 /* defaults: */ |
301 use_vbr = 1; | 339 use_vbr = 1; |
302 bandwidth = OPUS_AUTO; | 340 bandwidth = OPUS_AUTO; |
303 max_payload_bytes = MAX_PACKET; | 341 max_payload_bytes = MAX_PACKET; |
304 complexity = 10; | 342 complexity = 10; |
305 use_inbandfec = 0; | 343 use_inbandfec = 0; |
306 forcechannels = OPUS_AUTO; | 344 forcechannels = OPUS_AUTO; |
307 use_dtx = 0; | 345 use_dtx = 0; |
308 packet_loss_perc = 0; | 346 packet_loss_perc = 0; |
309 max_frame_size = 960*6; | 347 max_frame_size = 2*48000; |
310 curr_read=0; | 348 curr_read=0; |
311 | 349 |
312 while( args < argc - 2 ) { | 350 while( args < argc - 2 ) { |
313 /* process command line options */ | 351 /* process command line options */ |
314 if( strcmp( argv[ args ], "-cbr" ) == 0 ) { | 352 if( strcmp( argv[ args ], "-cbr" ) == 0 ) { |
315 check_encoder_option(decode_only, "-cbr"); | 353 check_encoder_option(decode_only, "-cbr"); |
316 use_vbr = 0; | 354 use_vbr = 0; |
317 args++; | 355 args++; |
318 } else if( strcmp( argv[ args ], "-bandwidth" ) == 0 ) { | 356 } else if( strcmp( argv[ args ], "-bandwidth" ) == 0 ) { |
319 check_encoder_option(decode_only, "-bandwidth"); | 357 check_encoder_option(decode_only, "-bandwidth"); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 use_inbandfec = 1; | 405 use_inbandfec = 1; |
368 args++; | 406 args++; |
369 } else if( strcmp( argv[ args ], "-forcemono" ) == 0 ) { | 407 } else if( strcmp( argv[ args ], "-forcemono" ) == 0 ) { |
370 check_encoder_option(decode_only, "-forcemono"); | 408 check_encoder_option(decode_only, "-forcemono"); |
371 forcechannels = 1; | 409 forcechannels = 1; |
372 args++; | 410 args++; |
373 } else if( strcmp( argv[ args ], "-cvbr" ) == 0 ) { | 411 } else if( strcmp( argv[ args ], "-cvbr" ) == 0 ) { |
374 check_encoder_option(decode_only, "-cvbr"); | 412 check_encoder_option(decode_only, "-cvbr"); |
375 cvbr = 1; | 413 cvbr = 1; |
376 args++; | 414 args++; |
| 415 } else if( strcmp( argv[ args ], "-variable-duration" ) == 0 ) { |
| 416 check_encoder_option(decode_only, "-variable-duration"); |
| 417 variable_duration = OPUS_FRAMESIZE_VARIABLE; |
| 418 args++; |
| 419 } else if( strcmp( argv[ args ], "-delayed-decision" ) == 0 ) { |
| 420 check_encoder_option(decode_only, "-delayed-decision"); |
| 421 delayed_decision = 1; |
| 422 args++; |
377 } else if( strcmp( argv[ args ], "-dtx") == 0 ) { | 423 } else if( strcmp( argv[ args ], "-dtx") == 0 ) { |
378 check_encoder_option(decode_only, "-dtx"); | 424 check_encoder_option(decode_only, "-dtx"); |
379 use_dtx = 1; | 425 use_dtx = 1; |
380 args++; | 426 args++; |
381 } else if( strcmp( argv[ args ], "-loss" ) == 0 ) { | 427 } else if( strcmp( argv[ args ], "-loss" ) == 0 ) { |
382 check_decoder_option(encode_only, "-loss"); | 428 check_decoder_option(encode_only, "-loss"); |
383 packet_loss_perc = atoi( argv[ args + 1 ] ); | 429 packet_loss_perc = atoi( argv[ args + 1 ] ); |
384 args += 2; | 430 args += 2; |
385 } else if( strcmp( argv[ args ], "-sweep" ) == 0 ) { | 431 } else if( strcmp( argv[ args ], "-sweep" ) == 0 ) { |
386 check_encoder_option(decode_only, "-sweep"); | 432 check_encoder_option(decode_only, "-sweep"); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 opus_encoder_ctl(enc, OPUS_SET_VBR(use_vbr)); | 538 opus_encoder_ctl(enc, OPUS_SET_VBR(use_vbr)); |
493 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cvbr)); | 539 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cvbr)); |
494 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity)); | 540 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity)); |
495 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(use_inbandfec)); | 541 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(use_inbandfec)); |
496 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(forcechannels)); | 542 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(forcechannels)); |
497 opus_encoder_ctl(enc, OPUS_SET_DTX(use_dtx)); | 543 opus_encoder_ctl(enc, OPUS_SET_DTX(use_dtx)); |
498 opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(packet_loss_perc)); | 544 opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(packet_loss_perc)); |
499 | 545 |
500 opus_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&skip)); | 546 opus_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&skip)); |
501 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(16)); | 547 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(16)); |
| 548 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(variable_duration)); |
502 } | 549 } |
503 if (!encode_only) | 550 if (!encode_only) |
504 { | 551 { |
505 dec = opus_decoder_create(sampling_rate, channels, &err); | 552 dec = opus_decoder_create(sampling_rate, channels, &err); |
506 if (err != OPUS_OK) | 553 if (err != OPUS_OK) |
507 { | 554 { |
508 fprintf(stderr, "Cannot create decoder: %s\n", opus_strerror(err)); | 555 fprintf(stderr, "Cannot create decoder: %s\n", opus_strerror(err)); |
509 fclose(fin); | 556 fclose(fin); |
510 fclose(fout); | 557 fclose(fout); |
511 return EXIT_FAILURE; | 558 return EXIT_FAILURE; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 (long)sampling_rate, bitrate_bps*0.001, | 594 (long)sampling_rate, bitrate_bps*0.001, |
548 bandwidth_string, frame_size); | 595 bandwidth_string, frame_size); |
549 | 596 |
550 in = (short*)malloc(max_frame_size*channels*sizeof(short)); | 597 in = (short*)malloc(max_frame_size*channels*sizeof(short)); |
551 out = (short*)malloc(max_frame_size*channels*sizeof(short)); | 598 out = (short*)malloc(max_frame_size*channels*sizeof(short)); |
552 fbytes = (unsigned char*)malloc(max_frame_size*channels*sizeof(short)); | 599 fbytes = (unsigned char*)malloc(max_frame_size*channels*sizeof(short)); |
553 data[0] = (unsigned char*)calloc(max_payload_bytes,sizeof(char)); | 600 data[0] = (unsigned char*)calloc(max_payload_bytes,sizeof(char)); |
554 if ( use_inbandfec ) { | 601 if ( use_inbandfec ) { |
555 data[1] = (unsigned char*)calloc(max_payload_bytes,sizeof(char)); | 602 data[1] = (unsigned char*)calloc(max_payload_bytes,sizeof(char)); |
556 } | 603 } |
| 604 if(delayed_decision) |
| 605 { |
| 606 if (variable_duration!=OPUS_FRAMESIZE_VARIABLE) |
| 607 { |
| 608 if (frame_size==sampling_rate/400) |
| 609 variable_duration = OPUS_FRAMESIZE_2_5_MS; |
| 610 else if (frame_size==sampling_rate/200) |
| 611 variable_duration = OPUS_FRAMESIZE_5_MS; |
| 612 else if (frame_size==sampling_rate/100) |
| 613 variable_duration = OPUS_FRAMESIZE_10_MS; |
| 614 else if (frame_size==sampling_rate/50) |
| 615 variable_duration = OPUS_FRAMESIZE_20_MS; |
| 616 else if (frame_size==sampling_rate/25) |
| 617 variable_duration = OPUS_FRAMESIZE_40_MS; |
| 618 else |
| 619 variable_duration = OPUS_FRAMESIZE_60_MS; |
| 620 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(variable_duration
)); |
| 621 } |
| 622 frame_size = 2*48000; |
| 623 } |
557 while (!stop) | 624 while (!stop) |
558 { | 625 { |
559 if (delayed_celt) | 626 if (delayed_celt) |
560 { | 627 { |
561 frame_size = newsize; | 628 frame_size = newsize; |
562 delayed_celt = 0; | 629 delayed_celt = 0; |
563 } else if (random_framesize && rand()%20==0) | 630 } else if (random_framesize && rand()%20==0) |
564 { | 631 { |
565 newsize = rand()%6; | 632 newsize = rand()%6; |
566 switch(newsize) | 633 switch(newsize) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 } | 677 } |
611 } else { | 678 } else { |
612 int i; | 679 int i; |
613 if (mode_list!=NULL) | 680 if (mode_list!=NULL) |
614 { | 681 { |
615 opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(mode_list[curr_mode][1]
)); | 682 opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(mode_list[curr_mode][1]
)); |
616 opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(mode_list[curr_mode][0
])); | 683 opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(mode_list[curr_mode][0
])); |
617 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(mode_list[curr_mod
e][3])); | 684 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(mode_list[curr_mod
e][3])); |
618 frame_size = mode_list[curr_mode][2]; | 685 frame_size = mode_list[curr_mode][2]; |
619 } | 686 } |
620 err = fread(fbytes, sizeof(short)*channels, frame_size, fin); | 687 err = fread(fbytes, sizeof(short)*channels, frame_size-remaining, fi
n); |
621 curr_read = err; | 688 curr_read = err; |
| 689 tot_in += curr_read; |
622 for(i=0;i<curr_read*channels;i++) | 690 for(i=0;i<curr_read*channels;i++) |
623 { | 691 { |
624 opus_int32 s; | 692 opus_int32 s; |
625 s=fbytes[2*i+1]<<8|fbytes[2*i]; | 693 s=fbytes[2*i+1]<<8|fbytes[2*i]; |
626 s=((s&0xFFFF)^0x8000)-0x8000; | 694 s=((s&0xFFFF)^0x8000)-0x8000; |
627 in[i]=s; | 695 in[i+remaining*channels]=s; |
628 } | 696 } |
629 if (curr_read < frame_size) | 697 if (curr_read+remaining < frame_size) |
630 { | 698 { |
631 for (i=curr_read*channels;i<frame_size*channels;i++) | 699 for (i=(curr_read+remaining)*channels;i<frame_size*channels;i++) |
632 in[i] = 0; | 700 in[i] = 0; |
633 stop = 1; | 701 if (encode_only || decode_only) |
| 702 stop = 1; |
634 } | 703 } |
635 len[toggle] = opus_encode(enc, in, frame_size, data[toggle], max_pay
load_bytes); | 704 len[toggle] = opus_encode(enc, in, frame_size, data[toggle], max_pay
load_bytes); |
| 705 nb_encoded = opus_packet_get_samples_per_frame(data[toggle], samplin
g_rate)*opus_packet_get_nb_frames(data[toggle], len[toggle]); |
| 706 remaining = frame_size-nb_encoded; |
| 707 for(i=0;i<remaining*channels;i++) |
| 708 in[i] = in[nb_encoded*channels+i]; |
636 if (sweep_bps!=0) | 709 if (sweep_bps!=0) |
637 { | 710 { |
638 bitrate_bps += sweep_bps; | 711 bitrate_bps += sweep_bps; |
639 if (sweep_max) | 712 if (sweep_max) |
640 { | 713 { |
641 if (bitrate_bps > sweep_max) | 714 if (bitrate_bps > sweep_max) |
642 sweep_bps = -sweep_bps; | 715 sweep_bps = -sweep_bps; |
643 else if (bitrate_bps < sweep_min) | 716 else if (bitrate_bps < sweep_min) |
644 sweep_bps = -sweep_bps; | 717 sweep_bps = -sweep_bps; |
645 } | 718 } |
(...skipping 28 matching lines...) Expand all Loading... |
674 } | 747 } |
675 int_to_char(enc_final_range[toggle], int_field); | 748 int_to_char(enc_final_range[toggle], int_field); |
676 if (fwrite(int_field, 1, 4, fout) != 4) { | 749 if (fwrite(int_field, 1, 4, fout) != 4) { |
677 fprintf(stderr, "Error writing.\n"); | 750 fprintf(stderr, "Error writing.\n"); |
678 return EXIT_FAILURE; | 751 return EXIT_FAILURE; |
679 } | 752 } |
680 if (fwrite(data[toggle], 1, len[toggle], fout) != (unsigned)len[togg
le]) { | 753 if (fwrite(data[toggle], 1, len[toggle], fout) != (unsigned)len[togg
le]) { |
681 fprintf(stderr, "Error writing.\n"); | 754 fprintf(stderr, "Error writing.\n"); |
682 return EXIT_FAILURE; | 755 return EXIT_FAILURE; |
683 } | 756 } |
| 757 tot_samples += nb_encoded; |
684 } else { | 758 } else { |
685 int output_samples; | 759 int output_samples; |
686 lost = len[toggle]==0 || (packet_loss_perc>0 && rand()%100 < packet_
loss_perc); | 760 lost = len[toggle]==0 || (packet_loss_perc>0 && rand()%100 < packet_
loss_perc); |
687 if (lost) | 761 if (lost) |
688 opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&output_sampl
es)); | 762 opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&output_sampl
es)); |
689 else | 763 else |
690 output_samples = max_frame_size; | 764 output_samples = max_frame_size; |
691 if( count >= use_inbandfec ) { | 765 if( count >= use_inbandfec ) { |
692 /* delay by one packet when using in-band FEC */ | 766 /* delay by one packet when using in-band FEC */ |
693 if( use_inbandfec ) { | 767 if( use_inbandfec ) { |
694 if( lost_prev ) { | 768 if( lost_prev ) { |
695 /* attempt to decode with in-band FEC from next packet *
/ | 769 /* attempt to decode with in-band FEC from next packet *
/ |
| 770 opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&out
put_samples)); |
696 output_samples = opus_decode(dec, lost ? NULL : data[tog
gle], len[toggle], out, output_samples, 1); | 771 output_samples = opus_decode(dec, lost ? NULL : data[tog
gle], len[toggle], out, output_samples, 1); |
697 } else { | 772 } else { |
698 /* regular decode */ | 773 /* regular decode */ |
| 774 output_samples = max_frame_size; |
699 output_samples = opus_decode(dec, data[1-toggle], len[1-
toggle], out, output_samples, 0); | 775 output_samples = opus_decode(dec, data[1-toggle], len[1-
toggle], out, output_samples, 0); |
700 } | 776 } |
701 } else { | 777 } else { |
702 output_samples = opus_decode(dec, lost ? NULL : data[toggle]
, len[toggle], out, output_samples, 0); | 778 output_samples = opus_decode(dec, lost ? NULL : data[toggle]
, len[toggle], out, output_samples, 0); |
703 } | 779 } |
704 if (output_samples>0) | 780 if (output_samples>0) |
705 { | 781 { |
| 782 if (!decode_only && tot_out + output_samples > tot_in) |
| 783 { |
| 784 stop=1; |
| 785 output_samples = tot_in-tot_out; |
| 786 } |
706 if (output_samples>skip) { | 787 if (output_samples>skip) { |
707 int i; | 788 int i; |
708 for(i=0;i<(output_samples-skip)*channels;i++) | 789 for(i=0;i<(output_samples-skip)*channels;i++) |
709 { | 790 { |
710 short s; | 791 short s; |
711 s=out[i+(skip*channels)]; | 792 s=out[i+(skip*channels)]; |
712 fbytes[2*i]=s&0xFF; | 793 fbytes[2*i]=s&0xFF; |
713 fbytes[2*i+1]=(s>>8)&0xFF; | 794 fbytes[2*i+1]=(s>>8)&0xFF; |
714 } | 795 } |
715 if (fwrite(fbytes, sizeof(short)*channels, output_samples
-skip, fout) != (unsigned)(output_samples-skip)){ | 796 if (fwrite(fbytes, sizeof(short)*channels, output_samples
-skip, fout) != (unsigned)(output_samples-skip)){ |
716 fprintf(stderr, "Error writing.\n"); | 797 fprintf(stderr, "Error writing.\n"); |
717 return EXIT_FAILURE; | 798 return EXIT_FAILURE; |
718 } | 799 } |
| 800 tot_out += output_samples-skip; |
719 } | 801 } |
720 if (output_samples<skip) skip -= output_samples; | 802 if (output_samples<skip) skip -= output_samples; |
721 else skip = 0; | 803 else skip = 0; |
722 } else { | 804 } else { |
723 fprintf(stderr, "error decoding frame: %s\n", | 805 fprintf(stderr, "error decoding frame: %s\n", |
724 opus_strerror(output_samples)); | 806 opus_strerror(output_samples)); |
725 } | 807 } |
| 808 tot_samples += output_samples; |
726 } | 809 } |
727 } | 810 } |
728 | 811 |
729 if (!encode_only) | 812 if (!encode_only) |
730 opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range)); | 813 opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range)); |
731 /* compare final range encoder rng values of encoder and decoder */ | 814 /* compare final range encoder rng values of encoder and decoder */ |
732 if( enc_final_range[toggle^use_inbandfec]!=0 && !encode_only | 815 if( enc_final_range[toggle^use_inbandfec]!=0 && !encode_only |
733 && !lost && !lost_prev | 816 && !lost && !lost_prev |
734 && dec_final_range != enc_final_range[toggle^use_inbandfec] ) { | 817 && dec_final_range != enc_final_range[toggle^use_inbandfec] ) { |
735 fprintf (stderr, "Error: Range coder state mismatch " | 818 fprintf (stderr, "Error: Range coder state mismatch " |
(...skipping 24 matching lines...) Expand all Loading... |
760 bits_act += len[toggle]*8; | 843 bits_act += len[toggle]*8; |
761 count_act++; | 844 count_act++; |
762 } | 845 } |
763 /* Variance */ | 846 /* Variance */ |
764 bits2 += len[toggle]*len[toggle]*64; | 847 bits2 += len[toggle]*len[toggle]*64; |
765 } | 848 } |
766 count++; | 849 count++; |
767 toggle = (toggle + use_inbandfec) & 1; | 850 toggle = (toggle + use_inbandfec) & 1; |
768 } | 851 } |
769 fprintf (stderr, "average bitrate: %7.3f kb/s\n", | 852 fprintf (stderr, "average bitrate: %7.3f kb/s\n", |
770 1e-3*bits*sampling_rate/(frame_size*(double)count)); | 853 1e-3*bits*sampling_rate/tot_samples); |
771 fprintf (stderr, "maximum bitrate: %7.3f kb/s\n", | 854 fprintf (stderr, "maximum bitrate: %7.3f kb/s\n", |
772 1e-3*bits_max*sampling_rate/frame_size); | 855 1e-3*bits_max*sampling_rate/frame_size); |
773 if (!decode_only) | 856 if (!decode_only) |
774 fprintf (stderr, "active bitrate: %7.3f kb/s\n", | 857 fprintf (stderr, "active bitrate: %7.3f kb/s\n", |
775 1e-3*bits_act*sampling_rate/(frame_size*(double)count_act)); | 858 1e-3*bits_act*sampling_rate/(frame_size*(double)count_act)); |
776 fprintf (stderr, "bitrate standard deviation: %7.3f kb/s\n", | 859 fprintf (stderr, "bitrate standard deviation: %7.3f kb/s\n", |
777 1e-3*sqrt(bits2/count - bits*bits/(count*(double)count))*sampling_ra
te/frame_size); | 860 1e-3*sqrt(bits2/count - bits*bits/(count*(double)count))*sampling_ra
te/frame_size); |
778 /* Close any files to which intermediate results were stored */ | 861 /* Close any files to which intermediate results were stored */ |
779 SILK_DEBUG_STORE_CLOSE_FILES | 862 SILK_DEBUG_STORE_CLOSE_FILES |
780 silk_TimerSave("opus_timing.txt"); | 863 silk_TimerSave("opus_timing.txt"); |
781 opus_encoder_destroy(enc); | 864 opus_encoder_destroy(enc); |
782 opus_decoder_destroy(dec); | 865 opus_decoder_destroy(dec); |
783 free(data[0]); | 866 free(data[0]); |
784 if (use_inbandfec) | 867 if (use_inbandfec) |
785 free(data[1]); | 868 free(data[1]); |
786 fclose(fin); | 869 fclose(fin); |
787 fclose(fout); | 870 fclose(fout); |
788 free(in); | 871 free(in); |
789 free(out); | 872 free(out); |
790 free(fbytes); | 873 free(fbytes); |
791 return EXIT_SUCCESS; | 874 return EXIT_SUCCESS; |
792 } | 875 } |
OLD | NEW |