1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4 /*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36 #include <string.h>
37 #include "opus.h"
38 #include "debug.h"
39 #include "opus_types.h"
40 #include "opus_private.h"
41 #include "opus_multistream.h"
42
43 #define MAX_PACKET 1500
44
print_usage(char * argv[])45 void print_usage( char* argv[] )
46 {
47 fprintf(stderr, "Usage: %s [-e] <application> <sampling rate (Hz)> <channels (1/2)> "
48 "<bits per second> [options] <input> <output>\n", argv[0]);
49 fprintf(stderr, " %s -d <sampling rate (Hz)> <channels (1/2)> "
50 "[options] <input> <output>\n\n", argv[0]);
51 fprintf(stderr, "mode: voip | audio | restricted-lowdelay\n" );
52 fprintf(stderr, "options:\n" );
53 fprintf(stderr, "-e : only runs the encoder (output the bit-stream)\n" );
54 fprintf(stderr, "-d : only runs the decoder (reads the bit-stream as input)\n" );
55 fprintf(stderr, "-cbr : enable constant bitrate; default: variable bitrate\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" );
58 fprintf(stderr, "-bandwidth <NB|MB|WB|SWB|FB> : audio bandwidth (from narrowband to fullband); default: sampling rate\n" );
59 fprintf(stderr, "-framesize <2.5|5|10|20|40|60> : frame size in ms; default: 20 \n" );
60 fprintf(stderr, "-max_payload <bytes> : maximum payload size in bytes, default: 1024\n" );
61 fprintf(stderr, "-complexity <comp> : complexity, 0 (lowest) ... 10 (highest); default: 10\n" );
62 fprintf(stderr, "-inbandfec : enable SILK inband FEC\n" );
63 fprintf(stderr, "-forcemono : force mono encoding, even for stereo input\n" );
64 fprintf(stderr, "-dtx : enable SILK DTX\n" );
65 fprintf(stderr, "-loss <perc> : simulate packet loss, in percent (0-100); default: 0\n" );
66 }
67
int_to_char(opus_uint32 i,unsigned char ch[4])68 static void int_to_char(opus_uint32 i, unsigned char ch[4])
69 {
70 ch[0] = i>>24;
71 ch[1] = (i>>16)&0xFF;
72 ch[2] = (i>>8)&0xFF;
73 ch[3] = i&0xFF;
74 }
75
char_to_int(unsigned char ch[4])76 static opus_uint32 char_to_int(unsigned char ch[4])
77 {
78 return ((opus_uint32)ch[0]<<24) | ((opus_uint32)ch[1]<<16)
79 | ((opus_uint32)ch[2]<< 8) | (opus_uint32)ch[3];
80 }
81
check_decoder_option(int encode_only,const char * opt)82 static void check_decoder_option(int encode_only, const char *opt)
83 {
84 if (encode_only)
85 {
86 fprintf(stderr, "option %s is only for decoding\n", opt);
87 exit(EXIT_FAILURE);
88 }
89 }
90
check_encoder_option(int decode_only,const char * opt)91 static void check_encoder_option(int decode_only, const char *opt)
92 {
93 if (decode_only)
94 {
95 fprintf(stderr, "option %s is only for encoding\n", opt);
96 exit(EXIT_FAILURE);
97 }
98 }
99
100 static const int silk8_test[][4] = {
101 {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960*3, 1},
102 {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960*2, 1},
103 {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960, 1},
104 {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 480, 1},
105 {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960*3, 2},
106 {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960*2, 2},
107 {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960, 2},
108 {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 480, 2}
109 };
110
111 static const int silk12_test[][4] = {
112 {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960*3, 1},
113 {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960*2, 1},
114 {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960, 1},
115 {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 480, 1},
116 {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960*3, 2},
117 {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960*2, 2},
118 {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960, 2},
119 {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 480, 2}
120 };
121
122 static const int silk16_test[][4] = {
123 {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960*3, 1},
124 {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960*2, 1},
125 {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960, 1},
126 {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 480, 1},
127 {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960*3, 2},
128 {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960*2, 2},
129 {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960, 2},
130 {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 480, 2}
131 };
132
133 static const int hybrid24_test[][4] = {
134 {MODE_SILK_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 960, 1},
135 {MODE_SILK_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 480, 1},
136 {MODE_SILK_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 960, 2},
137 {MODE_SILK_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 480, 2}
138 };
139
140 static const int hybrid48_test[][4] = {
141 {MODE_SILK_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 1},
142 {MODE_SILK_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 1},
143 {MODE_SILK_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 2},
144 {MODE_SILK_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 2}
145 };
146
147 static const int celt_test[][4] = {
148 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 1},
149 {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 960, 1},
150 {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960, 1},
151 {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960, 1},
152
153 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 1},
154 {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 480, 1},
155 {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 480, 1},
156 {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 480, 1},
157
158 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 240, 1},
159 {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 240, 1},
160 {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 240, 1},
161 {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 240, 1},
162
163 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 120, 1},
164 {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 120, 1},
165 {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 120, 1},
166 {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 120, 1},
167
168 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 2},
169 {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 960, 2},
170 {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960, 2},
171 {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960, 2},
172
173 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 2},
174 {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 480, 2},
175 {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 480, 2},
176 {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 480, 2},
177
178 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 240, 2},
179 {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 240, 2},
180 {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 240, 2},
181 {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 240, 2},
182
183 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 120, 2},
184 {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 120, 2},
185 {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 120, 2},
186 {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 120, 2},
187
188 };
189
190 static const int celt_hq_test[][4] = {
191 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 2},
192 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 2},
193 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 240, 2},
194 {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 120, 2},
195 };
196
197 #if 0 /* This is a hack that replaces the normal encoder/decoder with the multistream 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 application, 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 *error)
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
main(int argc,char * argv[])226 int main(int argc, char *argv[])
227 {
228 int err;
229 char *inFile, *outFile;
230 FILE *fin, *fout;
231 OpusEncoder *enc=NULL;
232 OpusDecoder *dec=NULL;
233 int args;
234 int len[2];
235 int frame_size, channels;
236 opus_int32 bitrate_bps=0;
237 unsigned char *data[2];
238 unsigned char *fbytes;
239 opus_int32 sampling_rate;
240 int use_vbr;
241 int max_payload_bytes;
242 int complexity;
243 int use_inbandfec;
244 int use_dtx;
245 int forcechannels;
246 int cvbr = 0;
247 int packet_loss_perc;
248 opus_int32 count=0, count_act=0;
249 int k;
250 opus_int32 skip=0;
251 int stop=0;
252 short *in, *out;
253 int application=OPUS_APPLICATION_AUDIO;
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;
257 int bandwidth=-1;
258 const char *bandwidth_string;
259 int lost = 0, lost_prev = 1;
260 int toggle = 0;
261 opus_uint32 enc_final_range[2];
262 opus_uint32 dec_final_range;
263 int encode_only=0, decode_only=0;
264 int max_frame_size = 960*6;
265 int curr_read=0;
266 int sweep_bps = 0;
267 int random_framesize=0, newsize=0, delayed_celt=0;
268 int sweep_max=0, sweep_min=0;
269 int random_fec=0;
270 const int (*mode_list)[4]=NULL;
271 int nb_modes_in_list=0;
272 int curr_mode=0;
273 int curr_mode_count=0;
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;
279
280 if (argc < 5 )
281 {
282 print_usage( argv );
283 return EXIT_FAILURE;
284 }
285
286 tot_in=tot_out=0;
287 fprintf(stderr, "%s\n", opus_get_version_string());
288
289 args = 1;
290 if (strcmp(argv[args], "-e")==0)
291 {
292 encode_only = 1;
293 args++;
294 } else if (strcmp(argv[args], "-d")==0)
295 {
296 decode_only = 1;
297 args++;
298 }
299 if (!decode_only && argc < 7 )
300 {
301 print_usage( argv );
302 return EXIT_FAILURE;
303 }
304
305 if (!decode_only)
306 {
307 if (strcmp(argv[args], "voip")==0)
308 application = OPUS_APPLICATION_VOIP;
309 else if (strcmp(argv[args], "restricted-lowdelay")==0)
310 application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
311 else if (strcmp(argv[args], "audio")!=0) {
312 fprintf(stderr, "unknown application: %s\n", argv[args]);
313 print_usage(argv);
314 return EXIT_FAILURE;
315 }
316 args++;
317 }
318 sampling_rate = (opus_int32)atol(argv[args]);
319 args++;
320 channels = atoi(argv[args]);
321 args++;
322 if (!decode_only)
323 {
324 bitrate_bps = (opus_int32)atol(argv[args]);
325 args++;
326 }
327
328 if (sampling_rate != 8000 && sampling_rate != 12000
329 && sampling_rate != 16000 && sampling_rate != 24000
330 && sampling_rate != 48000)
331 {
332 fprintf(stderr, "Supported sampling rates are 8000, 12000, "
333 "16000, 24000 and 48000.\n");
334 return EXIT_FAILURE;
335 }
336 frame_size = sampling_rate/50;
337
338 /* defaults: */
339 use_vbr = 1;
340 bandwidth = OPUS_AUTO;
341 max_payload_bytes = MAX_PACKET;
342 complexity = 10;
343 use_inbandfec = 0;
344 forcechannels = OPUS_AUTO;
345 use_dtx = 0;
346 packet_loss_perc = 0;
347 max_frame_size = 2*48000;
348 curr_read=0;
349
350 while( args < argc - 2 ) {
351 /* process command line options */
352 if( strcmp( argv[ args ], "-cbr" ) == 0 ) {
353 check_encoder_option(decode_only, "-cbr");
354 use_vbr = 0;
355 args++;
356 } else if( strcmp( argv[ args ], "-bandwidth" ) == 0 ) {
357 check_encoder_option(decode_only, "-bandwidth");
358 if (strcmp(argv[ args + 1 ], "NB")==0)
359 bandwidth = OPUS_BANDWIDTH_NARROWBAND;
360 else if (strcmp(argv[ args + 1 ], "MB")==0)
361 bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
362 else if (strcmp(argv[ args + 1 ], "WB")==0)
363 bandwidth = OPUS_BANDWIDTH_WIDEBAND;
364 else if (strcmp(argv[ args + 1 ], "SWB")==0)
365 bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
366 else if (strcmp(argv[ args + 1 ], "FB")==0)
367 bandwidth = OPUS_BANDWIDTH_FULLBAND;
368 else {
369 fprintf(stderr, "Unknown bandwidth %s. "
370 "Supported are NB, MB, WB, SWB, FB.\n",
371 argv[ args + 1 ]);
372 return EXIT_FAILURE;
373 }
374 args += 2;
375 } else if( strcmp( argv[ args ], "-framesize" ) == 0 ) {
376 check_encoder_option(decode_only, "-framesize");
377 if (strcmp(argv[ args + 1 ], "2.5")==0)
378 frame_size = sampling_rate/400;
379 else if (strcmp(argv[ args + 1 ], "5")==0)
380 frame_size = sampling_rate/200;
381 else if (strcmp(argv[ args + 1 ], "10")==0)
382 frame_size = sampling_rate/100;
383 else if (strcmp(argv[ args + 1 ], "20")==0)
384 frame_size = sampling_rate/50;
385 else if (strcmp(argv[ args + 1 ], "40")==0)
386 frame_size = sampling_rate/25;
387 else if (strcmp(argv[ args + 1 ], "60")==0)
388 frame_size = 3*sampling_rate/50;
389 else {
390 fprintf(stderr, "Unsupported frame size: %s ms. "
391 "Supported are 2.5, 5, 10, 20, 40, 60.\n",
392 argv[ args + 1 ]);
393 return EXIT_FAILURE;
394 }
395 args += 2;
396 } else if( strcmp( argv[ args ], "-max_payload" ) == 0 ) {
397 check_encoder_option(decode_only, "-max_payload");
398 max_payload_bytes = atoi( argv[ args + 1 ] );
399 args += 2;
400 } else if( strcmp( argv[ args ], "-complexity" ) == 0 ) {
401 check_encoder_option(decode_only, "-complexity");
402 complexity = atoi( argv[ args + 1 ] );
403 args += 2;
404 } else if( strcmp( argv[ args ], "-inbandfec" ) == 0 ) {
405 use_inbandfec = 1;
406 args++;
407 } else if( strcmp( argv[ args ], "-forcemono" ) == 0 ) {
408 check_encoder_option(decode_only, "-forcemono");
409 forcechannels = 1;
410 args++;
411 } else if( strcmp( argv[ args ], "-cvbr" ) == 0 ) {
412 check_encoder_option(decode_only, "-cvbr");
413 cvbr = 1;
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++;
423 } else if( strcmp( argv[ args ], "-dtx") == 0 ) {
424 check_encoder_option(decode_only, "-dtx");
425 use_dtx = 1;
426 args++;
427 } else if( strcmp( argv[ args ], "-loss" ) == 0 ) {
428 check_decoder_option(encode_only, "-loss");
429 packet_loss_perc = atoi( argv[ args + 1 ] );
430 args += 2;
431 } else if( strcmp( argv[ args ], "-sweep" ) == 0 ) {
432 check_encoder_option(decode_only, "-sweep");
433 sweep_bps = atoi( argv[ args + 1 ] );
434 args += 2;
435 } else if( strcmp( argv[ args ], "-random_framesize" ) == 0 ) {
436 check_encoder_option(decode_only, "-random_framesize");
437 random_framesize = 1;
438 args++;
439 } else if( strcmp( argv[ args ], "-sweep_max" ) == 0 ) {
440 check_encoder_option(decode_only, "-sweep_max");
441 sweep_max = atoi( argv[ args + 1 ] );
442 args += 2;
443 } else if( strcmp( argv[ args ], "-random_fec" ) == 0 ) {
444 check_encoder_option(decode_only, "-random_fec");
445 random_fec = 1;
446 args++;
447 } else if( strcmp( argv[ args ], "-silk8k_test" ) == 0 ) {
448 check_encoder_option(decode_only, "-silk8k_test");
449 mode_list = silk8_test;
450 nb_modes_in_list = 8;
451 args++;
452 } else if( strcmp( argv[ args ], "-silk12k_test" ) == 0 ) {
453 check_encoder_option(decode_only, "-silk12k_test");
454 mode_list = silk12_test;
455 nb_modes_in_list = 8;
456 args++;
457 } else if( strcmp( argv[ args ], "-silk16k_test" ) == 0 ) {
458 check_encoder_option(decode_only, "-silk16k_test");
459 mode_list = silk16_test;
460 nb_modes_in_list = 8;
461 args++;
462 } else if( strcmp( argv[ args ], "-hybrid24k_test" ) == 0 ) {
463 check_encoder_option(decode_only, "-hybrid24k_test");
464 mode_list = hybrid24_test;
465 nb_modes_in_list = 4;
466 args++;
467 } else if( strcmp( argv[ args ], "-hybrid48k_test" ) == 0 ) {
468 check_encoder_option(decode_only, "-hybrid48k_test");
469 mode_list = hybrid48_test;
470 nb_modes_in_list = 4;
471 args++;
472 } else if( strcmp( argv[ args ], "-celt_test" ) == 0 ) {
473 check_encoder_option(decode_only, "-celt_test");
474 mode_list = celt_test;
475 nb_modes_in_list = 32;
476 args++;
477 } else if( strcmp( argv[ args ], "-celt_hq_test" ) == 0 ) {
478 check_encoder_option(decode_only, "-celt_hq_test");
479 mode_list = celt_hq_test;
480 nb_modes_in_list = 4;
481 args++;
482 } else {
483 printf( "Error: unrecognized setting: %s\n\n", argv[ args ] );
484 print_usage( argv );
485 return EXIT_FAILURE;
486 }
487 }
488
489 if (sweep_max)
490 sweep_min = bitrate_bps;
491
492 if (max_payload_bytes < 0 || max_payload_bytes > MAX_PACKET)
493 {
494 fprintf (stderr, "max_payload_bytes must be between 0 and %d\n",
495 MAX_PACKET);
496 return EXIT_FAILURE;
497 }
498
499 inFile = argv[argc-2];
500 fin = fopen(inFile, "rb");
501 if (!fin)
502 {
503 fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
504 return EXIT_FAILURE;
505 }
506 if (mode_list)
507 {
508 int size;
509 fseek(fin, 0, SEEK_END);
510 size = ftell(fin);
511 fprintf(stderr, "File size is %d bytes\n", size);
512 fseek(fin, 0, SEEK_SET);
513 mode_switch_time = size/sizeof(short)/channels/nb_modes_in_list;
514 fprintf(stderr, "Switching mode every %d samples\n", mode_switch_time);
515 }
516
517 outFile = argv[argc-1];
518 fout = fopen(outFile, "wb+");
519 if (!fout)
520 {
521 fprintf (stderr, "Could not open output file %s\n", argv[argc-1]);
522 fclose(fin);
523 return EXIT_FAILURE;
524 }
525
526 if (!decode_only)
527 {
528 enc = opus_encoder_create(sampling_rate, channels, application, &err);
529 if (err != OPUS_OK)
530 {
531 fprintf(stderr, "Cannot create encoder: %s\n", opus_strerror(err));
532 fclose(fin);
533 fclose(fout);
534 return EXIT_FAILURE;
535 }
536 opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps));
537 opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(bandwidth));
538 opus_encoder_ctl(enc, OPUS_SET_VBR(use_vbr));
539 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cvbr));
540 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity));
541 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(use_inbandfec));
542 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(forcechannels));
543 opus_encoder_ctl(enc, OPUS_SET_DTX(use_dtx));
544 opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(packet_loss_perc));
545
546 opus_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&skip));
547 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(16));
548 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(variable_duration));
549 }
550 if (!encode_only)
551 {
552 dec = opus_decoder_create(sampling_rate, channels, &err);
553 if (err != OPUS_OK)
554 {
555 fprintf(stderr, "Cannot create decoder: %s\n", opus_strerror(err));
556 fclose(fin);
557 fclose(fout);
558 return EXIT_FAILURE;
559 }
560 }
561
562
563 switch(bandwidth)
564 {
565 case OPUS_BANDWIDTH_NARROWBAND:
566 bandwidth_string = "narrowband";
567 break;
568 case OPUS_BANDWIDTH_MEDIUMBAND:
569 bandwidth_string = "mediumband";
570 break;
571 case OPUS_BANDWIDTH_WIDEBAND:
572 bandwidth_string = "wideband";
573 break;
574 case OPUS_BANDWIDTH_SUPERWIDEBAND:
575 bandwidth_string = "superwideband";
576 break;
577 case OPUS_BANDWIDTH_FULLBAND:
578 bandwidth_string = "fullband";
579 break;
580 case OPUS_AUTO:
581 bandwidth_string = "auto";
582 break;
583 default:
584 bandwidth_string = "unknown";
585 break;
586 }
587
588 if (decode_only)
589 fprintf(stderr, "Decoding with %ld Hz output (%d channels)\n",
590 (long)sampling_rate, channels);
591 else
592 fprintf(stderr, "Encoding %ld Hz input at %.3f kb/s "
593 "in %s mode with %d-sample frames.\n",
594 (long)sampling_rate, bitrate_bps*0.001,
595 bandwidth_string, frame_size);
596
597 in = (short*)malloc(max_frame_size*channels*sizeof(short));
598 out = (short*)malloc(max_frame_size*channels*sizeof(short));
599 fbytes = (unsigned char*)malloc(max_frame_size*channels*sizeof(short));
600 data[0] = (unsigned char*)calloc(max_payload_bytes,sizeof(char));
601 if ( use_inbandfec ) {
602 data[1] = (unsigned char*)calloc(max_payload_bytes,sizeof(char));
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 }
624 while (!stop)
625 {
626 if (delayed_celt)
627 {
628 frame_size = newsize;
629 delayed_celt = 0;
630 } else if (random_framesize && rand()%20==0)
631 {
632 newsize = rand()%6;
633 switch(newsize)
634 {
635 case 0: newsize=sampling_rate/400; break;
636 case 1: newsize=sampling_rate/200; break;
637 case 2: newsize=sampling_rate/100; break;
638 case 3: newsize=sampling_rate/50; break;
639 case 4: newsize=sampling_rate/25; break;
640 case 5: newsize=3*sampling_rate/50; break;
641 }
642 while (newsize < sampling_rate/25 && bitrate_bps-fabs(sweep_bps) <= 3*12*sampling_rate/newsize)
643 newsize*=2;
644 if (newsize < sampling_rate/100 && frame_size >= sampling_rate/100)
645 {
646 opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY));
647 delayed_celt=1;
648 } else {
649 frame_size = newsize;
650 }
651 }
652 if (random_fec && rand()%30==0)
653 {
654 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rand()%4==0));
655 }
656 if (decode_only)
657 {
658 unsigned char ch[4];
659 err = fread(ch, 1, 4, fin);
660 if (feof(fin))
661 break;
662 len[toggle] = char_to_int(ch);
663 if (len[toggle]>max_payload_bytes || len[toggle]<0)
664 {
665 fprintf(stderr, "Invalid payload length: %d\n",len[toggle]);
666 break;
667 }
668 err = fread(ch, 1, 4, fin);
669 enc_final_range[toggle] = char_to_int(ch);
670 err = fread(data[toggle], 1, len[toggle], fin);
671 if (err<len[toggle])
672 {
673 fprintf(stderr, "Ran out of input, "
674 "expecting %d bytes got %d\n",
675 len[toggle],err);
676 break;
677 }
678 } else {
679 int i;
680 if (mode_list!=NULL)
681 {
682 opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(mode_list[curr_mode][1]));
683 opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(mode_list[curr_mode][0]));
684 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(mode_list[curr_mode][3]));
685 frame_size = mode_list[curr_mode][2];
686 }
687 err = fread(fbytes, sizeof(short)*channels, frame_size-remaining, fin);
688 curr_read = err;
689 tot_in += curr_read;
690 for(i=0;i<curr_read*channels;i++)
691 {
692 opus_int32 s;
693 s=fbytes[2*i+1]<<8|fbytes[2*i];
694 s=((s&0xFFFF)^0x8000)-0x8000;
695 in[i+remaining*channels]=s;
696 }
697 if (curr_read+remaining < frame_size)
698 {
699 for (i=(curr_read+remaining)*channels;i<frame_size*channels;i++)
700 in[i] = 0;
701 if (encode_only || decode_only)
702 stop = 1;
703 }
704 len[toggle] = opus_encode(enc, in, frame_size, data[toggle], max_payload_bytes);
705 nb_encoded = opus_packet_get_samples_per_frame(data[toggle], sampling_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];
709 if (sweep_bps!=0)
710 {
711 bitrate_bps += sweep_bps;
712 if (sweep_max)
713 {
714 if (bitrate_bps > sweep_max)
715 sweep_bps = -sweep_bps;
716 else if (bitrate_bps < sweep_min)
717 sweep_bps = -sweep_bps;
718 }
719 /* safety */
720 if (bitrate_bps<1000)
721 bitrate_bps = 1000;
722 opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps));
723 }
724 opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range[toggle]));
725 if (len[toggle] < 0)
726 {
727 fprintf (stderr, "opus_encode() returned %d\n", len[toggle]);
728 fclose(fin);
729 fclose(fout);
730 return EXIT_FAILURE;
731 }
732 curr_mode_count += frame_size;
733 if (curr_mode_count > mode_switch_time && curr_mode < nb_modes_in_list-1)
734 {
735 curr_mode++;
736 curr_mode_count = 0;
737 }
738 }
739
740 if (encode_only)
741 {
742 unsigned char int_field[4];
743 int_to_char(len[toggle], int_field);
744 if (fwrite(int_field, 1, 4, fout) != 4) {
745 fprintf(stderr, "Error writing.\n");
746 return EXIT_FAILURE;
747 }
748 int_to_char(enc_final_range[toggle], int_field);
749 if (fwrite(int_field, 1, 4, fout) != 4) {
750 fprintf(stderr, "Error writing.\n");
751 return EXIT_FAILURE;
752 }
753 if (fwrite(data[toggle], 1, len[toggle], fout) != (unsigned)len[toggle]) {
754 fprintf(stderr, "Error writing.\n");
755 return EXIT_FAILURE;
756 }
757 tot_samples += nb_encoded;
758 } else {
759 int output_samples;
760 lost = len[toggle]==0 || (packet_loss_perc>0 && rand()%100 < packet_loss_perc);
761 if (lost)
762 opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&output_samples));
763 else
764 output_samples = max_frame_size;
765 if( count >= use_inbandfec ) {
766 /* delay by one packet when using in-band FEC */
767 if( use_inbandfec ) {
768 if( lost_prev ) {
769 /* attempt to decode with in-band FEC from next packet */
770 opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&output_samples));
771 output_samples = opus_decode(dec, lost ? NULL : data[toggle], len[toggle], out, output_samples, 1);
772 } else {
773 /* regular decode */
774 output_samples = max_frame_size;
775 output_samples = opus_decode(dec, data[1-toggle], len[1-toggle], out, output_samples, 0);
776 }
777 } else {
778 output_samples = opus_decode(dec, lost ? NULL : data[toggle], len[toggle], out, output_samples, 0);
779 }
780 if (output_samples>0)
781 {
782 if (!decode_only && tot_out + output_samples > tot_in)
783 {
784 stop=1;
785 output_samples = tot_in-tot_out;
786 }
787 if (output_samples>skip) {
788 int i;
789 for(i=0;i<(output_samples-skip)*channels;i++)
790 {
791 short s;
792 s=out[i+(skip*channels)];
793 fbytes[2*i]=s&0xFF;
794 fbytes[2*i+1]=(s>>8)&0xFF;
795 }
796 if (fwrite(fbytes, sizeof(short)*channels, output_samples-skip, fout) != (unsigned)(output_samples-skip)){
797 fprintf(stderr, "Error writing.\n");
798 return EXIT_FAILURE;
799 }
800 tot_out += output_samples-skip;
801 }
802 if (output_samples<skip) skip -= output_samples;
803 else skip = 0;
804 } else {
805 fprintf(stderr, "error decoding frame: %s\n",
806 opus_strerror(output_samples));
807 }
808 tot_samples += output_samples;
809 }
810 }
811
812 if (!encode_only)
813 opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
814 /* compare final range encoder rng values of encoder and decoder */
815 if( enc_final_range[toggle^use_inbandfec]!=0 && !encode_only
816 && !lost && !lost_prev
817 && dec_final_range != enc_final_range[toggle^use_inbandfec] ) {
818 fprintf (stderr, "Error: Range coder state mismatch "
819 "between encoder and decoder "
820 "in frame %ld: 0x%8lx vs 0x%8lx\n",
821 (long)count,
822 (unsigned long)enc_final_range[toggle^use_inbandfec],
823 (unsigned long)dec_final_range);
824 fclose(fin);
825 fclose(fout);
826 return EXIT_FAILURE;
827 }
828
829 lost_prev = lost;
830
831 /* count bits */
832 bits += len[toggle]*8;
833 bits_max = ( len[toggle]*8 > bits_max ) ? len[toggle]*8 : bits_max;
834 if( count >= use_inbandfec ) {
835 nrg = 0.0;
836 if (!decode_only)
837 {
838 for ( k = 0; k < frame_size * channels; k++ ) {
839 nrg += in[ k ] * (double)in[ k ];
840 }
841 }
842 if ( ( nrg / ( frame_size * channels ) ) > 1e5 ) {
843 bits_act += len[toggle]*8;
844 count_act++;
845 }
846 /* Variance */
847 bits2 += len[toggle]*len[toggle]*64;
848 }
849 count++;
850 toggle = (toggle + use_inbandfec) & 1;
851 }
852 fprintf (stderr, "average bitrate: %7.3f kb/s\n",
853 1e-3*bits*sampling_rate/tot_samples);
854 fprintf (stderr, "maximum bitrate: %7.3f kb/s\n",
855 1e-3*bits_max*sampling_rate/frame_size);
856 if (!decode_only)
857 fprintf (stderr, "active bitrate: %7.3f kb/s\n",
858 1e-3*bits_act*sampling_rate/(frame_size*(double)count_act));
859 fprintf (stderr, "bitrate standard deviation: %7.3f kb/s\n",
860 1e-3*sqrt(bits2/count - bits*bits/(count*(double)count))*sampling_rate/frame_size);
861 /* Close any files to which intermediate results were stored */
862 SILK_DEBUG_STORE_CLOSE_FILES
863 silk_TimerSave("opus_timing.txt");
864 opus_encoder_destroy(enc);
865 opus_decoder_destroy(dec);
866 free(data[0]);
867 if (use_inbandfec)
868 free(data[1]);
869 fclose(fin);
870 fclose(fout);
871 free(in);
872 free(out);
873 free(fbytes);
874 return EXIT_SUCCESS;
875 }
876