Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 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 | |
| 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]); |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 51 | fprintf(stderr, "application: voip | audio | restricted-lowdelay\n" ); |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 82 | static void check_encoder_option(int decode_only, const char *opt) |
| 83 | { |
| 84 | if (decode_only) |
| 85 | { |
| 86 | fprintf(stderr, "option %s is only for encoding\n", opt); |
| 87 | exit(EXIT_FAILURE); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static const int silk8_test[][4] = { |
| 92 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960*3, 1}, |
| 93 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960*2, 1}, |
| 94 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960, 1}, |
| 95 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 480, 1}, |
| 96 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960*3, 2}, |
| 97 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960*2, 2}, |
| 98 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960, 2}, |
| 99 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_NARROWBAND, 480, 2} |
| 100 | }; |
| 101 | |
| 102 | static const int silk12_test[][4] = { |
| 103 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960*3, 1}, |
| 104 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960*2, 1}, |
| 105 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960, 1}, |
| 106 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 480, 1}, |
| 107 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960*3, 2}, |
| 108 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960*2, 2}, |
| 109 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 960, 2}, |
| 110 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_MEDIUMBAND, 480, 2} |
| 111 | }; |
| 112 | |
| 113 | static const int silk16_test[][4] = { |
| 114 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960*3, 1}, |
| 115 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960*2, 1}, |
| 116 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960, 1}, |
| 117 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 480, 1}, |
| 118 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960*3, 2}, |
| 119 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960*2, 2}, |
| 120 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960, 2}, |
| 121 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_WIDEBAND, 480, 2} |
| 122 | }; |
| 123 | |
| 124 | static const int hybrid24_test[][4] = { |
| 125 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 960, 1}, |
| 126 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 480, 1}, |
| 127 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 960, 2}, |
| 128 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 480, 2} |
| 129 | }; |
| 130 | |
| 131 | static const int hybrid48_test[][4] = { |
| 132 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 1}, |
| 133 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 1}, |
| 134 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 2}, |
| 135 | {MODE_SILK_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 2} |
| 136 | }; |
| 137 | |
| 138 | static const int celt_test[][4] = { |
| 139 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 1}, |
| 140 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 960, 1}, |
| 141 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960, 1}, |
| 142 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960, 1}, |
| 143 | |
| 144 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 1}, |
| 145 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 480, 1}, |
| 146 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 480, 1}, |
| 147 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 480, 1}, |
| 148 | |
| 149 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 240, 1}, |
| 150 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 240, 1}, |
| 151 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 240, 1}, |
| 152 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 240, 1}, |
| 153 | |
| 154 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 120, 1}, |
| 155 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 120, 1}, |
| 156 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 120, 1}, |
| 157 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 120, 1}, |
| 158 | |
| 159 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 2}, |
| 160 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 960, 2}, |
| 161 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 960, 2}, |
| 162 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 960, 2}, |
| 163 | |
| 164 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 2}, |
| 165 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 480, 2}, |
| 166 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 480, 2}, |
| 167 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 480, 2}, |
| 168 | |
| 169 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 240, 2}, |
| 170 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 240, 2}, |
| 171 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 240, 2}, |
| 172 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 240, 2}, |
| 173 | |
| 174 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 120, 2}, |
| 175 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_SUPERWIDEBAND, 120, 2}, |
| 176 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_WIDEBAND, 120, 2}, |
| 177 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_NARROWBAND, 120, 2}, |
| 178 | |
| 179 | }; |
| 180 | |
| 181 | static const int celt_hq_test[][4] = { |
| 182 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 960, 2}, |
| 183 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 480, 2}, |
| 184 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 240, 2}, |
| 185 | {MODE_CELT_ONLY, OPUS_BANDWIDTH_FULLBAND, 120, 2}, |
| 186 | }; |
| 187 | |
| 188 | #if 0 /* This is a hack that replaces the normal encoder/decoder with the multistream version */ |
| 189 | #define OpusEncoder OpusMSEncoder |
| 190 | #define OpusDecoder OpusMSDecoder |
| 191 | #define opus_encode opus_multistream_encode |
| 192 | #define opus_decode opus_multistream_decode |
| 193 | #define opus_encoder_ctl opus_multistream_encoder_ctl |
| 194 | #define opus_decoder_ctl opus_multistream_decoder_ctl |
| 195 | #define opus_encoder_create ms_opus_encoder_create |
| 196 | #define opus_decoder_create ms_opus_decoder_create |
| 197 | #define opus_encoder_destroy opus_multistream_encoder_destroy |
| 198 | #define opus_decoder_destroy opus_multistream_decoder_destroy |
| 199 | |
| 200 | static OpusEncoder *ms_opus_encoder_create(opus_int32 Fs, int channels, int application, int *error) |
| 201 | { |
| 202 | int streams, coupled_streams; |
| 203 | unsigned char mapping[256]; |
| 204 | return (OpusEncoder *)opus_multistream_surround_encoder_create(Fs, channels, 1, &streams, &coupled_streams, mapping, application, error); |
| 205 | } |
| 206 | static OpusDecoder *ms_opus_decoder_create(opus_int32 Fs, int channels, int *error) |
| 207 | { |
| 208 | int streams; |
| 209 | int coupled_streams; |
| 210 | unsigned char mapping[256]={0,1}; |
| 211 | streams = 1; |
| 212 | coupled_streams = channels==2; |
| 213 | return (OpusDecoder *)opus_multistream_decoder_create(Fs, channels, streams, coupled_streams, mapping, error); |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | int main(int argc, char *argv[]) |
| 218 | { |
| 219 | int err; |
| 220 | char *inFile, *outFile; |
| 221 | FILE *fin, *fout; |
| 222 | OpusEncoder *enc=NULL; |
| 223 | OpusDecoder *dec=NULL; |
| 224 | int args; |
| 225 | int len[2]; |
| 226 | int frame_size, channels; |
| 227 | opus_int32 bitrate_bps=0; |
| 228 | unsigned char *data[2]; |
| 229 | unsigned char *fbytes; |
| 230 | opus_int32 sampling_rate; |
| 231 | int use_vbr; |
| 232 | int max_payload_bytes; |
| 233 | int complexity; |
| 234 | int use_inbandfec; |
| 235 | int use_dtx; |
| 236 | int forcechannels; |
| 237 | int cvbr = 0; |
| 238 | int packet_loss_perc; |
| 239 | opus_int32 count=0, count_act=0; |
| 240 | int k; |
| 241 | opus_int32 skip=0; |
| 242 | int stop=0; |
| 243 | short *in, *out; |
| 244 | int application=OPUS_APPLICATION_AUDIO; |
| 245 | double bits=0.0, bits_max=0.0, bits_act=0.0, bits2=0.0, nrg; |
| 246 | double tot_samples=0; |
| 247 | opus_uint64 tot_in, tot_out; |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 248 | int bandwidth=OPUS_AUTO; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 249 | const char *bandwidth_string; |
| 250 | int lost = 0, lost_prev = 1; |
| 251 | int toggle = 0; |
| 252 | opus_uint32 enc_final_range[2]; |
| 253 | opus_uint32 dec_final_range; |
| 254 | int encode_only=0, decode_only=0; |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 255 | int max_frame_size = 48000*2; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 256 | int curr_read=0; |
| 257 | int sweep_bps = 0; |
| 258 | int random_framesize=0, newsize=0, delayed_celt=0; |
| 259 | int sweep_max=0, sweep_min=0; |
| 260 | int random_fec=0; |
| 261 | const int (*mode_list)[4]=NULL; |
| 262 | int nb_modes_in_list=0; |
| 263 | int curr_mode=0; |
| 264 | int curr_mode_count=0; |
| 265 | int mode_switch_time = 48000; |
| 266 | int nb_encoded=0; |
| 267 | int remaining=0; |
| 268 | int variable_duration=OPUS_FRAMESIZE_ARG; |
| 269 | int delayed_decision=0; |
| 270 | |
| 271 | if (argc < 5 ) |
| 272 | { |
| 273 | print_usage( argv ); |
| 274 | return EXIT_FAILURE; |
| 275 | } |
| 276 | |
| 277 | tot_in=tot_out=0; |
| 278 | fprintf(stderr, "%s\n", opus_get_version_string()); |
| 279 | |
| 280 | args = 1; |
| 281 | if (strcmp(argv[args], "-e")==0) |
| 282 | { |
| 283 | encode_only = 1; |
| 284 | args++; |
| 285 | } else if (strcmp(argv[args], "-d")==0) |
| 286 | { |
| 287 | decode_only = 1; |
| 288 | args++; |
| 289 | } |
| 290 | if (!decode_only && argc < 7 ) |
| 291 | { |
| 292 | print_usage( argv ); |
| 293 | return EXIT_FAILURE; |
| 294 | } |
| 295 | |
| 296 | if (!decode_only) |
| 297 | { |
| 298 | if (strcmp(argv[args], "voip")==0) |
| 299 | application = OPUS_APPLICATION_VOIP; |
| 300 | else if (strcmp(argv[args], "restricted-lowdelay")==0) |
| 301 | application = OPUS_APPLICATION_RESTRICTED_LOWDELAY; |
| 302 | else if (strcmp(argv[args], "audio")!=0) { |
| 303 | fprintf(stderr, "unknown application: %s\n", argv[args]); |
| 304 | print_usage(argv); |
| 305 | return EXIT_FAILURE; |
| 306 | } |
| 307 | args++; |
| 308 | } |
| 309 | sampling_rate = (opus_int32)atol(argv[args]); |
| 310 | args++; |
| 311 | |
| 312 | if (sampling_rate != 8000 && sampling_rate != 12000 |
| 313 | && sampling_rate != 16000 && sampling_rate != 24000 |
| 314 | && sampling_rate != 48000) |
| 315 | { |
| 316 | fprintf(stderr, "Supported sampling rates are 8000, 12000, " |
| 317 | "16000, 24000 and 48000.\n"); |
| 318 | return EXIT_FAILURE; |
| 319 | } |
| 320 | frame_size = sampling_rate/50; |
| 321 | |
| 322 | channels = atoi(argv[args]); |
| 323 | args++; |
| 324 | |
| 325 | if (channels < 1 || channels > 2) |
| 326 | { |
| 327 | fprintf(stderr, "Opus_demo supports only 1 or 2 channels.\n"); |
| 328 | return EXIT_FAILURE; |
| 329 | } |
| 330 | |
| 331 | if (!decode_only) |
| 332 | { |
| 333 | bitrate_bps = (opus_int32)atol(argv[args]); |
| 334 | args++; |
| 335 | } |
| 336 | |
| 337 | /* defaults: */ |
| 338 | use_vbr = 1; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 339 | max_payload_bytes = MAX_PACKET; |
| 340 | complexity = 10; |
| 341 | use_inbandfec = 0; |
| 342 | forcechannels = OPUS_AUTO; |
| 343 | use_dtx = 0; |
| 344 | packet_loss_perc = 0; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 345 | |
| 346 | while( args < argc - 2 ) { |
| 347 | /* process command line options */ |
| 348 | if( strcmp( argv[ args ], "-cbr" ) == 0 ) { |
| 349 | check_encoder_option(decode_only, "-cbr"); |
| 350 | use_vbr = 0; |
| 351 | args++; |
| 352 | } else if( strcmp( argv[ args ], "-bandwidth" ) == 0 ) { |
| 353 | check_encoder_option(decode_only, "-bandwidth"); |
| 354 | if (strcmp(argv[ args + 1 ], "NB")==0) |
| 355 | bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
| 356 | else if (strcmp(argv[ args + 1 ], "MB")==0) |
| 357 | bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
| 358 | else if (strcmp(argv[ args + 1 ], "WB")==0) |
| 359 | bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
| 360 | else if (strcmp(argv[ args + 1 ], "SWB")==0) |
| 361 | bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
| 362 | else if (strcmp(argv[ args + 1 ], "FB")==0) |
| 363 | bandwidth = OPUS_BANDWIDTH_FULLBAND; |
| 364 | else { |
| 365 | fprintf(stderr, "Unknown bandwidth %s. " |
| 366 | "Supported are NB, MB, WB, SWB, FB.\n", |
| 367 | argv[ args + 1 ]); |
| 368 | return EXIT_FAILURE; |
| 369 | } |
| 370 | args += 2; |
| 371 | } else if( strcmp( argv[ args ], "-framesize" ) == 0 ) { |
| 372 | check_encoder_option(decode_only, "-framesize"); |
| 373 | if (strcmp(argv[ args + 1 ], "2.5")==0) |
| 374 | frame_size = sampling_rate/400; |
| 375 | else if (strcmp(argv[ args + 1 ], "5")==0) |
| 376 | frame_size = sampling_rate/200; |
| 377 | else if (strcmp(argv[ args + 1 ], "10")==0) |
| 378 | frame_size = sampling_rate/100; |
| 379 | else if (strcmp(argv[ args + 1 ], "20")==0) |
| 380 | frame_size = sampling_rate/50; |
| 381 | else if (strcmp(argv[ args + 1 ], "40")==0) |
| 382 | frame_size = sampling_rate/25; |
| 383 | else if (strcmp(argv[ args + 1 ], "60")==0) |
| 384 | frame_size = 3*sampling_rate/50; |
| 385 | else { |
| 386 | fprintf(stderr, "Unsupported frame size: %s ms. " |
| 387 | "Supported are 2.5, 5, 10, 20, 40, 60.\n", |
| 388 | argv[ args + 1 ]); |
| 389 | return EXIT_FAILURE; |
| 390 | } |
| 391 | args += 2; |
| 392 | } else if( strcmp( argv[ args ], "-max_payload" ) == 0 ) { |
| 393 | check_encoder_option(decode_only, "-max_payload"); |
| 394 | max_payload_bytes = atoi( argv[ args + 1 ] ); |
| 395 | args += 2; |
| 396 | } else if( strcmp( argv[ args ], "-complexity" ) == 0 ) { |
| 397 | check_encoder_option(decode_only, "-complexity"); |
| 398 | complexity = atoi( argv[ args + 1 ] ); |
| 399 | args += 2; |
| 400 | } else if( strcmp( argv[ args ], "-inbandfec" ) == 0 ) { |
| 401 | use_inbandfec = 1; |
| 402 | args++; |
| 403 | } else if( strcmp( argv[ args ], "-forcemono" ) == 0 ) { |
| 404 | check_encoder_option(decode_only, "-forcemono"); |
| 405 | forcechannels = 1; |
| 406 | args++; |
| 407 | } else if( strcmp( argv[ args ], "-cvbr" ) == 0 ) { |
| 408 | check_encoder_option(decode_only, "-cvbr"); |
| 409 | cvbr = 1; |
| 410 | args++; |
| 411 | } else if( strcmp( argv[ args ], "-variable-duration" ) == 0 ) { |
| 412 | check_encoder_option(decode_only, "-variable-duration"); |
| 413 | variable_duration = OPUS_FRAMESIZE_VARIABLE; |
| 414 | args++; |
| 415 | } else if( strcmp( argv[ args ], "-delayed-decision" ) == 0 ) { |
| 416 | check_encoder_option(decode_only, "-delayed-decision"); |
| 417 | delayed_decision = 1; |
| 418 | args++; |
| 419 | } else if( strcmp( argv[ args ], "-dtx") == 0 ) { |
| 420 | check_encoder_option(decode_only, "-dtx"); |
| 421 | use_dtx = 1; |
| 422 | args++; |
| 423 | } else if( strcmp( argv[ args ], "-loss" ) == 0 ) { |
| 424 | packet_loss_perc = atoi( argv[ args + 1 ] ); |
| 425 | args += 2; |
| 426 | } else if( strcmp( argv[ args ], "-sweep" ) == 0 ) { |
| 427 | check_encoder_option(decode_only, "-sweep"); |
| 428 | sweep_bps = atoi( argv[ args + 1 ] ); |
| 429 | args += 2; |
| 430 | } else if( strcmp( argv[ args ], "-random_framesize" ) == 0 ) { |
| 431 | check_encoder_option(decode_only, "-random_framesize"); |
| 432 | random_framesize = 1; |
| 433 | args++; |
| 434 | } else if( strcmp( argv[ args ], "-sweep_max" ) == 0 ) { |
| 435 | check_encoder_option(decode_only, "-sweep_max"); |
| 436 | sweep_max = atoi( argv[ args + 1 ] ); |
| 437 | args += 2; |
| 438 | } else if( strcmp( argv[ args ], "-random_fec" ) == 0 ) { |
| 439 | check_encoder_option(decode_only, "-random_fec"); |
| 440 | random_fec = 1; |
| 441 | args++; |
| 442 | } else if( strcmp( argv[ args ], "-silk8k_test" ) == 0 ) { |
| 443 | check_encoder_option(decode_only, "-silk8k_test"); |
| 444 | mode_list = silk8_test; |
| 445 | nb_modes_in_list = 8; |
| 446 | args++; |
| 447 | } else if( strcmp( argv[ args ], "-silk12k_test" ) == 0 ) { |
| 448 | check_encoder_option(decode_only, "-silk12k_test"); |
| 449 | mode_list = silk12_test; |
| 450 | nb_modes_in_list = 8; |
| 451 | args++; |
| 452 | } else if( strcmp( argv[ args ], "-silk16k_test" ) == 0 ) { |
| 453 | check_encoder_option(decode_only, "-silk16k_test"); |
| 454 | mode_list = silk16_test; |
| 455 | nb_modes_in_list = 8; |
| 456 | args++; |
| 457 | } else if( strcmp( argv[ args ], "-hybrid24k_test" ) == 0 ) { |
| 458 | check_encoder_option(decode_only, "-hybrid24k_test"); |
| 459 | mode_list = hybrid24_test; |
| 460 | nb_modes_in_list = 4; |
| 461 | args++; |
| 462 | } else if( strcmp( argv[ args ], "-hybrid48k_test" ) == 0 ) { |
| 463 | check_encoder_option(decode_only, "-hybrid48k_test"); |
| 464 | mode_list = hybrid48_test; |
| 465 | nb_modes_in_list = 4; |
| 466 | args++; |
| 467 | } else if( strcmp( argv[ args ], "-celt_test" ) == 0 ) { |
| 468 | check_encoder_option(decode_only, "-celt_test"); |
| 469 | mode_list = celt_test; |
| 470 | nb_modes_in_list = 32; |
| 471 | args++; |
| 472 | } else if( strcmp( argv[ args ], "-celt_hq_test" ) == 0 ) { |
| 473 | check_encoder_option(decode_only, "-celt_hq_test"); |
| 474 | mode_list = celt_hq_test; |
| 475 | nb_modes_in_list = 4; |
| 476 | args++; |
| 477 | } else { |
| 478 | printf( "Error: unrecognized setting: %s\n\n", argv[ args ] ); |
| 479 | print_usage( argv ); |
| 480 | return EXIT_FAILURE; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | if (sweep_max) |
| 485 | sweep_min = bitrate_bps; |
| 486 | |
| 487 | if (max_payload_bytes < 0 || max_payload_bytes > MAX_PACKET) |
| 488 | { |
| 489 | fprintf (stderr, "max_payload_bytes must be between 0 and %d\n", |
| 490 | MAX_PACKET); |
| 491 | return EXIT_FAILURE; |
| 492 | } |
| 493 | |
| 494 | inFile = argv[argc-2]; |
| 495 | fin = fopen(inFile, "rb"); |
| 496 | if (!fin) |
| 497 | { |
| 498 | fprintf (stderr, "Could not open input file %s\n", argv[argc-2]); |
| 499 | return EXIT_FAILURE; |
| 500 | } |
| 501 | if (mode_list) |
| 502 | { |
| 503 | int size; |
| 504 | fseek(fin, 0, SEEK_END); |
| 505 | size = ftell(fin); |
| 506 | fprintf(stderr, "File size is %d bytes\n", size); |
| 507 | fseek(fin, 0, SEEK_SET); |
| 508 | mode_switch_time = size/sizeof(short)/channels/nb_modes_in_list; |
| 509 | fprintf(stderr, "Switching mode every %d samples\n", mode_switch_time); |
| 510 | } |
| 511 | |
| 512 | outFile = argv[argc-1]; |
| 513 | fout = fopen(outFile, "wb+"); |
| 514 | if (!fout) |
| 515 | { |
| 516 | fprintf (stderr, "Could not open output file %s\n", argv[argc-1]); |
| 517 | fclose(fin); |
| 518 | return EXIT_FAILURE; |
| 519 | } |
| 520 | |
| 521 | if (!decode_only) |
| 522 | { |
| 523 | enc = opus_encoder_create(sampling_rate, channels, application, &err); |
| 524 | if (err != OPUS_OK) |
| 525 | { |
| 526 | fprintf(stderr, "Cannot create encoder: %s\n", opus_strerror(err)); |
| 527 | fclose(fin); |
| 528 | fclose(fout); |
| 529 | return EXIT_FAILURE; |
| 530 | } |
| 531 | opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps)); |
| 532 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(bandwidth)); |
| 533 | opus_encoder_ctl(enc, OPUS_SET_VBR(use_vbr)); |
| 534 | opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cvbr)); |
| 535 | opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity)); |
| 536 | opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(use_inbandfec)); |
| 537 | opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(forcechannels)); |
| 538 | opus_encoder_ctl(enc, OPUS_SET_DTX(use_dtx)); |
| 539 | opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(packet_loss_perc)); |
| 540 | |
| 541 | opus_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&skip)); |
| 542 | opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(16)); |
| 543 | opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(variable_duration)); |
| 544 | } |
| 545 | if (!encode_only) |
| 546 | { |
| 547 | dec = opus_decoder_create(sampling_rate, channels, &err); |
| 548 | if (err != OPUS_OK) |
| 549 | { |
| 550 | fprintf(stderr, "Cannot create decoder: %s\n", opus_strerror(err)); |
| 551 | fclose(fin); |
| 552 | fclose(fout); |
| 553 | return EXIT_FAILURE; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | |
| 558 | switch(bandwidth) |
| 559 | { |
| 560 | case OPUS_BANDWIDTH_NARROWBAND: |
| 561 | bandwidth_string = "narrowband"; |
| 562 | break; |
| 563 | case OPUS_BANDWIDTH_MEDIUMBAND: |
| 564 | bandwidth_string = "mediumband"; |
| 565 | break; |
| 566 | case OPUS_BANDWIDTH_WIDEBAND: |
| 567 | bandwidth_string = "wideband"; |
| 568 | break; |
| 569 | case OPUS_BANDWIDTH_SUPERWIDEBAND: |
| 570 | bandwidth_string = "superwideband"; |
| 571 | break; |
| 572 | case OPUS_BANDWIDTH_FULLBAND: |
| 573 | bandwidth_string = "fullband"; |
| 574 | break; |
| 575 | case OPUS_AUTO: |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 576 | bandwidth_string = "auto bandwidth"; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 577 | break; |
| 578 | default: |
| 579 | bandwidth_string = "unknown"; |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | if (decode_only) |
| 584 | fprintf(stderr, "Decoding with %ld Hz output (%d channels)\n", |
| 585 | (long)sampling_rate, channels); |
| 586 | else |
| 587 | fprintf(stderr, "Encoding %ld Hz input at %.3f kb/s " |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 588 | "in %s with %d-sample frames.\n", |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 589 | (long)sampling_rate, bitrate_bps*0.001, |
| 590 | bandwidth_string, frame_size); |
| 591 | |
| 592 | in = (short*)malloc(max_frame_size*channels*sizeof(short)); |
| 593 | out = (short*)malloc(max_frame_size*channels*sizeof(short)); |
| 594 | fbytes = (unsigned char*)malloc(max_frame_size*channels*sizeof(short)); |
| 595 | data[0] = (unsigned char*)calloc(max_payload_bytes,sizeof(char)); |
| 596 | if ( use_inbandfec ) { |
| 597 | data[1] = (unsigned char*)calloc(max_payload_bytes,sizeof(char)); |
| 598 | } |
| 599 | if(delayed_decision) |
| 600 | { |
| 601 | if (variable_duration!=OPUS_FRAMESIZE_VARIABLE) |
| 602 | { |
| 603 | if (frame_size==sampling_rate/400) |
| 604 | variable_duration = OPUS_FRAMESIZE_2_5_MS; |
| 605 | else if (frame_size==sampling_rate/200) |
| 606 | variable_duration = OPUS_FRAMESIZE_5_MS; |
| 607 | else if (frame_size==sampling_rate/100) |
| 608 | variable_duration = OPUS_FRAMESIZE_10_MS; |
| 609 | else if (frame_size==sampling_rate/50) |
| 610 | variable_duration = OPUS_FRAMESIZE_20_MS; |
| 611 | else if (frame_size==sampling_rate/25) |
| 612 | variable_duration = OPUS_FRAMESIZE_40_MS; |
| 613 | else |
| 614 | variable_duration = OPUS_FRAMESIZE_60_MS; |
| 615 | opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(variable_duration)); |
| 616 | } |
| 617 | frame_size = 2*48000; |
| 618 | } |
| 619 | while (!stop) |
| 620 | { |
| 621 | if (delayed_celt) |
| 622 | { |
| 623 | frame_size = newsize; |
| 624 | delayed_celt = 0; |
| 625 | } else if (random_framesize && rand()%20==0) |
| 626 | { |
| 627 | newsize = rand()%6; |
| 628 | switch(newsize) |
| 629 | { |
| 630 | case 0: newsize=sampling_rate/400; break; |
| 631 | case 1: newsize=sampling_rate/200; break; |
| 632 | case 2: newsize=sampling_rate/100; break; |
| 633 | case 3: newsize=sampling_rate/50; break; |
| 634 | case 4: newsize=sampling_rate/25; break; |
| 635 | case 5: newsize=3*sampling_rate/50; break; |
| 636 | } |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 637 | while (newsize < sampling_rate/25 && bitrate_bps-abs(sweep_bps) <= 3*12*sampling_rate/newsize) |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 638 | newsize*=2; |
| 639 | if (newsize < sampling_rate/100 && frame_size >= sampling_rate/100) |
| 640 | { |
| 641 | opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY)); |
| 642 | delayed_celt=1; |
| 643 | } else { |
| 644 | frame_size = newsize; |
| 645 | } |
| 646 | } |
| 647 | if (random_fec && rand()%30==0) |
| 648 | { |
| 649 | opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rand()%4==0)); |
| 650 | } |
| 651 | if (decode_only) |
| 652 | { |
| 653 | unsigned char ch[4]; |
| 654 | err = fread(ch, 1, 4, fin); |
| 655 | if (feof(fin)) |
| 656 | break; |
| 657 | len[toggle] = char_to_int(ch); |
| 658 | if (len[toggle]>max_payload_bytes || len[toggle]<0) |
| 659 | { |
| 660 | fprintf(stderr, "Invalid payload length: %d\n",len[toggle]); |
| 661 | break; |
| 662 | } |
| 663 | err = fread(ch, 1, 4, fin); |
| 664 | enc_final_range[toggle] = char_to_int(ch); |
| 665 | err = fread(data[toggle], 1, len[toggle], fin); |
| 666 | if (err<len[toggle]) |
| 667 | { |
| 668 | fprintf(stderr, "Ran out of input, " |
| 669 | "expecting %d bytes got %d\n", |
| 670 | len[toggle],err); |
| 671 | break; |
| 672 | } |
| 673 | } else { |
| 674 | int i; |
| 675 | if (mode_list!=NULL) |
| 676 | { |
| 677 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(mode_list[curr_mode][1])); |
| 678 | opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(mode_list[curr_mode][0])); |
| 679 | opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(mode_list[curr_mode][3])); |
| 680 | frame_size = mode_list[curr_mode][2]; |
| 681 | } |
| 682 | err = fread(fbytes, sizeof(short)*channels, frame_size-remaining, fin); |
| 683 | curr_read = err; |
| 684 | tot_in += curr_read; |
| 685 | for(i=0;i<curr_read*channels;i++) |
| 686 | { |
| 687 | opus_int32 s; |
| 688 | s=fbytes[2*i+1]<<8|fbytes[2*i]; |
| 689 | s=((s&0xFFFF)^0x8000)-0x8000; |
| 690 | in[i+remaining*channels]=s; |
| 691 | } |
| 692 | if (curr_read+remaining < frame_size) |
| 693 | { |
| 694 | for (i=(curr_read+remaining)*channels;i<frame_size*channels;i++) |
| 695 | in[i] = 0; |
| 696 | if (encode_only || decode_only) |
| 697 | stop = 1; |
| 698 | } |
| 699 | len[toggle] = opus_encode(enc, in, frame_size, data[toggle], max_payload_bytes); |
| 700 | nb_encoded = opus_packet_get_samples_per_frame(data[toggle], sampling_rate)*opus_packet_get_nb_frames(data[toggle], len[toggle]); |
| 701 | remaining = frame_size-nb_encoded; |
| 702 | for(i=0;i<remaining*channels;i++) |
| 703 | in[i] = in[nb_encoded*channels+i]; |
| 704 | if (sweep_bps!=0) |
| 705 | { |
| 706 | bitrate_bps += sweep_bps; |
| 707 | if (sweep_max) |
| 708 | { |
| 709 | if (bitrate_bps > sweep_max) |
| 710 | sweep_bps = -sweep_bps; |
| 711 | else if (bitrate_bps < sweep_min) |
| 712 | sweep_bps = -sweep_bps; |
| 713 | } |
| 714 | /* safety */ |
| 715 | if (bitrate_bps<1000) |
| 716 | bitrate_bps = 1000; |
| 717 | opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps)); |
| 718 | } |
| 719 | opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range[toggle])); |
| 720 | if (len[toggle] < 0) |
| 721 | { |
| 722 | fprintf (stderr, "opus_encode() returned %d\n", len[toggle]); |
| 723 | fclose(fin); |
| 724 | fclose(fout); |
| 725 | return EXIT_FAILURE; |
| 726 | } |
| 727 | curr_mode_count += frame_size; |
| 728 | if (curr_mode_count > mode_switch_time && curr_mode < nb_modes_in_list-1) |
| 729 | { |
| 730 | curr_mode++; |
| 731 | curr_mode_count = 0; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | #if 0 /* This is for testing the padding code, do not enable by default */ |
| 736 | if (len[toggle]<1275) |
| 737 | { |
| 738 | int new_len = len[toggle]+rand()%(max_payload_bytes-len[toggle]); |
| 739 | if ((err = opus_packet_pad(data[toggle], len[toggle], new_len)) != OPUS_OK) |
| 740 | { |
| 741 | fprintf(stderr, "padding failed: %s\n", opus_strerror(err)); |
| 742 | return EXIT_FAILURE; |
| 743 | } |
| 744 | len[toggle] = new_len; |
| 745 | } |
| 746 | #endif |
| 747 | if (encode_only) |
| 748 | { |
| 749 | unsigned char int_field[4]; |
| 750 | int_to_char(len[toggle], int_field); |
| 751 | if (fwrite(int_field, 1, 4, fout) != 4) { |
| 752 | fprintf(stderr, "Error writing.\n"); |
| 753 | return EXIT_FAILURE; |
| 754 | } |
| 755 | int_to_char(enc_final_range[toggle], int_field); |
| 756 | if (fwrite(int_field, 1, 4, fout) != 4) { |
| 757 | fprintf(stderr, "Error writing.\n"); |
| 758 | return EXIT_FAILURE; |
| 759 | } |
| 760 | if (fwrite(data[toggle], 1, len[toggle], fout) != (unsigned)len[toggle]) { |
| 761 | fprintf(stderr, "Error writing.\n"); |
| 762 | return EXIT_FAILURE; |
| 763 | } |
| 764 | tot_samples += nb_encoded; |
| 765 | } else { |
| 766 | int output_samples; |
| 767 | lost = len[toggle]==0 || (packet_loss_perc>0 && rand()%100 < packet_loss_perc); |
| 768 | if (lost) |
| 769 | opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&output_samples)); |
| 770 | else |
| 771 | output_samples = max_frame_size; |
| 772 | if( count >= use_inbandfec ) { |
| 773 | /* delay by one packet when using in-band FEC */ |
| 774 | if( use_inbandfec ) { |
| 775 | if( lost_prev ) { |
| 776 | /* attempt to decode with in-band FEC from next packet */ |
| 777 | opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&output_samples)); |
| 778 | output_samples = opus_decode(dec, lost ? NULL : data[toggle], len[toggle], out, output_samples, 1); |
| 779 | } else { |
| 780 | /* regular decode */ |
| 781 | output_samples = max_frame_size; |
| 782 | output_samples = opus_decode(dec, data[1-toggle], len[1-toggle], out, output_samples, 0); |
| 783 | } |
| 784 | } else { |
| 785 | output_samples = opus_decode(dec, lost ? NULL : data[toggle], len[toggle], out, output_samples, 0); |
| 786 | } |
| 787 | if (output_samples>0) |
| 788 | { |
| 789 | if (!decode_only && tot_out + output_samples > tot_in) |
| 790 | { |
| 791 | stop=1; |
| 792 | output_samples = tot_in-tot_out; |
| 793 | } |
| 794 | if (output_samples>skip) { |
| 795 | int i; |
| 796 | for(i=0;i<(output_samples-skip)*channels;i++) |
| 797 | { |
| 798 | short s; |
| 799 | s=out[i+(skip*channels)]; |
| 800 | fbytes[2*i]=s&0xFF; |
| 801 | fbytes[2*i+1]=(s>>8)&0xFF; |
| 802 | } |
| 803 | if (fwrite(fbytes, sizeof(short)*channels, output_samples-skip, fout) != (unsigned)(output_samples-skip)){ |
| 804 | fprintf(stderr, "Error writing.\n"); |
| 805 | return EXIT_FAILURE; |
| 806 | } |
| 807 | tot_out += output_samples-skip; |
| 808 | } |
| 809 | if (output_samples<skip) skip -= output_samples; |
| 810 | else skip = 0; |
| 811 | } else { |
| 812 | fprintf(stderr, "error decoding frame: %s\n", |
| 813 | opus_strerror(output_samples)); |
| 814 | } |
| 815 | tot_samples += output_samples; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (!encode_only) |
| 820 | opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range)); |
| 821 | /* compare final range encoder rng values of encoder and decoder */ |
| 822 | if( enc_final_range[toggle^use_inbandfec]!=0 && !encode_only |
| 823 | && !lost && !lost_prev |
| 824 | && dec_final_range != enc_final_range[toggle^use_inbandfec] ) { |
| 825 | fprintf (stderr, "Error: Range coder state mismatch " |
| 826 | "between encoder and decoder " |
| 827 | "in frame %ld: 0x%8lx vs 0x%8lx\n", |
| 828 | (long)count, |
| 829 | (unsigned long)enc_final_range[toggle^use_inbandfec], |
| 830 | (unsigned long)dec_final_range); |
| 831 | fclose(fin); |
| 832 | fclose(fout); |
| 833 | return EXIT_FAILURE; |
| 834 | } |
| 835 | |
| 836 | lost_prev = lost; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 837 | if( count >= use_inbandfec ) { |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 838 | /* count bits */ |
| 839 | bits += len[toggle]*8; |
| 840 | bits_max = ( len[toggle]*8 > bits_max ) ? len[toggle]*8 : bits_max; |
| 841 | bits2 += len[toggle]*len[toggle]*64; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 842 | if (!decode_only) |
| 843 | { |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 844 | nrg = 0.0; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 845 | for ( k = 0; k < frame_size * channels; k++ ) { |
| 846 | nrg += in[ k ] * (double)in[ k ]; |
| 847 | } |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 848 | nrg /= frame_size * channels; |
| 849 | if( nrg > 1e5 ) { |
| 850 | bits_act += len[toggle]*8; |
| 851 | count_act++; |
| 852 | } |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 853 | } |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 854 | } |
| 855 | count++; |
| 856 | toggle = (toggle + use_inbandfec) & 1; |
| 857 | } |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 858 | |
| 859 | /* Print out bitrate statistics */ |
| 860 | if(decode_only) |
| 861 | frame_size = (int)(tot_samples / count); |
| 862 | count -= use_inbandfec; |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 863 | fprintf (stderr, "average bitrate: %7.3f kb/s\n", |
| 864 | 1e-3*bits*sampling_rate/tot_samples); |
| 865 | fprintf (stderr, "maximum bitrate: %7.3f kb/s\n", |
| 866 | 1e-3*bits_max*sampling_rate/frame_size); |
| 867 | if (!decode_only) |
| 868 | fprintf (stderr, "active bitrate: %7.3f kb/s\n", |
flim | c91ee5b | 2016-01-26 14:33:44 +0100 | [diff] [blame] | 869 | 1e-3*bits_act*sampling_rate/(1e-15+frame_size*(double)count_act)); |
Vignesh Venkatasubramanian | 2bd8b54 | 2014-02-20 10:50:35 -0800 | [diff] [blame] | 870 | fprintf (stderr, "bitrate standard deviation: %7.3f kb/s\n", |
| 871 | 1e-3*sqrt(bits2/count - bits*bits/(count*(double)count))*sampling_rate/frame_size); |
| 872 | /* Close any files to which intermediate results were stored */ |
| 873 | SILK_DEBUG_STORE_CLOSE_FILES |
| 874 | silk_TimerSave("opus_timing.txt"); |
| 875 | opus_encoder_destroy(enc); |
| 876 | opus_decoder_destroy(dec); |
| 877 | free(data[0]); |
| 878 | if (use_inbandfec) |
| 879 | free(data[1]); |
| 880 | fclose(fin); |
| 881 | fclose(fout); |
| 882 | free(in); |
| 883 | free(out); |
| 884 | free(fbytes); |
| 885 | return EXIT_SUCCESS; |
| 886 | } |