opus_encoder.c (119450B)
1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited 2 Written by Jean-Marc Valin and Koen Vos */ 3 /* 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions 6 are met: 7 8 - Redistributions of source code must retain the above copyright 9 notice, this list of conditions and the following disclaimer. 10 11 - Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifdef HAVE_CONFIG_H 29 #include "config.h" 30 #endif 31 32 #include <stdarg.h> 33 #include "celt.h" 34 #include "entenc.h" 35 #include "modes.h" 36 #include "API.h" 37 #include "stack_alloc.h" 38 #include "float_cast.h" 39 #include "opus.h" 40 #include "arch.h" 41 #include "pitch.h" 42 #include "opus_private.h" 43 #include "os_support.h" 44 #include "cpu_support.h" 45 #include "analysis.h" 46 #include "mathops.h" 47 #include "tuning_parameters.h" 48 49 #ifdef ENABLE_DRED 50 #include "dred_coding.h" 51 #endif 52 53 #ifdef FIXED_POINT 54 #include "fixed/structs_FIX.h" 55 #else 56 #include "float/structs_FLP.h" 57 #endif 58 #ifdef ENABLE_OSCE_TRAINING_DATA 59 #include <stdio.h> 60 #endif 61 62 #ifdef ENABLE_QEXT 63 #define MAX_ENCODER_BUFFER 960 64 #else 65 #define MAX_ENCODER_BUFFER 480 66 #endif 67 68 #define PSEUDO_SNR_THRESHOLD 316.23f /* 10^(25/10) */ 69 70 typedef struct { 71 opus_val32 XX, XY, YY; 72 opus_val16 smoothed_width; 73 opus_val16 max_follower; 74 } StereoWidthState; 75 76 struct OpusEncoder { 77 int celt_enc_offset; 78 int silk_enc_offset; 79 silk_EncControlStruct silk_mode; 80 #ifdef ENABLE_DRED 81 DREDEnc dred_encoder; 82 #endif 83 int application; 84 int channels; 85 int delay_compensation; 86 int force_channels; 87 int signal_type; 88 int user_bandwidth; 89 int max_bandwidth; 90 int user_forced_mode; 91 int voice_ratio; 92 opus_int32 Fs; 93 int use_vbr; 94 int vbr_constraint; 95 int variable_duration; 96 opus_int32 bitrate_bps; 97 opus_int32 user_bitrate_bps; 98 int lsb_depth; 99 int encoder_buffer; 100 int lfe; 101 int arch; 102 int use_dtx; /* general DTX for both SILK and CELT */ 103 int fec_config; 104 #ifndef DISABLE_FLOAT_API 105 TonalityAnalysisState analysis; 106 #endif 107 #ifdef ENABLE_QEXT 108 int enable_qext; 109 #endif 110 111 #define OPUS_ENCODER_RESET_START stream_channels 112 int stream_channels; 113 opus_int16 hybrid_stereo_width_Q14; 114 opus_int32 variable_HP_smth2_Q15; 115 opus_val16 prev_HB_gain; 116 opus_val32 hp_mem[4]; 117 int mode; 118 int prev_mode; 119 int prev_channels; 120 int prev_framesize; 121 int bandwidth; 122 /* Bandwidth determined automatically from the rate (before any other adjustment) */ 123 int auto_bandwidth; 124 int silk_bw_switch; 125 /* Sampling rate (at the API level) */ 126 int first; 127 celt_glog * energy_masking; 128 StereoWidthState width_mem; 129 #ifndef DISABLE_FLOAT_API 130 int detected_bandwidth; 131 #endif 132 int nb_no_activity_ms_Q1; 133 opus_val32 peak_signal_energy; 134 #ifdef ENABLE_DRED 135 int dred_duration; 136 int dred_q0; 137 int dred_dQ; 138 int dred_qmax; 139 int dred_target_chunks; 140 unsigned char activity_mem[DRED_MAX_FRAMES*4]; /* 2.5ms resolution*/ 141 #endif 142 int nonfinal_frame; /* current frame is not the final in a packet */ 143 opus_uint32 rangeFinal; 144 /* Needs to be the last field because it may be partially or completely omitted. */ 145 opus_res delay_buffer[MAX_ENCODER_BUFFER*2]; 146 }; 147 148 /* Transition tables for the voice and music. First column is the 149 middle (memoriless) threshold. The second column is the hysteresis 150 (difference with the middle) */ 151 static const opus_int32 mono_voice_bandwidth_thresholds[8] = { 152 9000, 700, /* NB<->MB */ 153 9000, 700, /* MB<->WB */ 154 13500, 1000, /* WB<->SWB */ 155 14000, 2000, /* SWB<->FB */ 156 }; 157 static const opus_int32 mono_music_bandwidth_thresholds[8] = { 158 9000, 700, /* NB<->MB */ 159 9000, 700, /* MB<->WB */ 160 11000, 1000, /* WB<->SWB */ 161 12000, 2000, /* SWB<->FB */ 162 }; 163 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { 164 9000, 700, /* NB<->MB */ 165 9000, 700, /* MB<->WB */ 166 13500, 1000, /* WB<->SWB */ 167 14000, 2000, /* SWB<->FB */ 168 }; 169 static const opus_int32 stereo_music_bandwidth_thresholds[8] = { 170 9000, 700, /* NB<->MB */ 171 9000, 700, /* MB<->WB */ 172 11000, 1000, /* WB<->SWB */ 173 12000, 2000, /* SWB<->FB */ 174 }; 175 /* Threshold bit-rates for switching between mono and stereo */ 176 static const opus_int32 stereo_voice_threshold = 19000; 177 static const opus_int32 stereo_music_threshold = 17000; 178 179 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ 180 static const opus_int32 mode_thresholds[2][2] = { 181 /* voice */ /* music */ 182 { 64000, 10000}, /* mono */ 183 { 44000, 10000}, /* stereo */ 184 }; 185 186 static const opus_int32 fec_thresholds[] = { 187 12000, 1000, /* NB */ 188 14000, 1000, /* MB */ 189 16000, 1000, /* WB */ 190 20000, 1000, /* SWB */ 191 22000, 1000, /* FB */ 192 }; 193 194 int opus_encoder_get_size(int channels) 195 { 196 int ret; 197 ret = opus_encoder_init(NULL, 48000, channels, OPUS_APPLICATION_AUDIO); 198 if (ret < 0) 199 return 0; 200 else 201 return ret; 202 } 203 204 int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application) 205 { 206 void *silk_enc=NULL; 207 CELTEncoder *celt_enc=NULL; 208 int err; 209 int ret, silkEncSizeBytes, celtEncSizeBytes=0; 210 int tot_size; 211 int base_size; 212 213 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000 214 #ifdef ENABLE_QEXT 215 &&Fs!=96000 216 #endif 217 )||(channels!=1&&channels!=2)|| 218 (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO 219 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY 220 && application != OPUS_APPLICATION_RESTRICTED_SILK 221 && application != OPUS_APPLICATION_RESTRICTED_CELT)) 222 return OPUS_BAD_ARG; 223 224 /* Create SILK encoder */ 225 ret = silk_Get_Encoder_Size( &silkEncSizeBytes, channels ); 226 if (ret) 227 return OPUS_BAD_ARG; 228 silkEncSizeBytes = align(silkEncSizeBytes); 229 if (application == OPUS_APPLICATION_RESTRICTED_CELT) 230 silkEncSizeBytes = 0; 231 if (application != OPUS_APPLICATION_RESTRICTED_SILK) 232 celtEncSizeBytes = celt_encoder_get_size(channels); 233 base_size = align(sizeof(OpusEncoder)); 234 if (application == OPUS_APPLICATION_RESTRICTED_SILK || application == OPUS_APPLICATION_RESTRICTED_CELT) { 235 base_size = align(base_size - MAX_ENCODER_BUFFER*2*sizeof(opus_res)); 236 } else if (channels==1) 237 base_size = align(base_size - MAX_ENCODER_BUFFER*sizeof(opus_res)); 238 tot_size = base_size+silkEncSizeBytes+celtEncSizeBytes; 239 if (st == NULL) { 240 return tot_size; 241 } 242 OPUS_CLEAR((char*)st, tot_size); 243 st->silk_enc_offset = base_size; 244 st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; 245 246 st->stream_channels = st->channels = channels; 247 248 st->Fs = Fs; 249 250 st->arch = opus_select_arch(); 251 252 if (application != OPUS_APPLICATION_RESTRICTED_CELT) 253 { 254 silk_enc = (char*)st+st->silk_enc_offset; 255 ret = silk_InitEncoder( silk_enc, st->channels, st->arch, &st->silk_mode ); 256 } 257 if(ret)return OPUS_INTERNAL_ERROR; 258 259 /* default SILK parameters */ 260 st->silk_mode.nChannelsAPI = channels; 261 st->silk_mode.nChannelsInternal = channels; 262 st->silk_mode.API_sampleRate = st->Fs; 263 st->silk_mode.maxInternalSampleRate = 16000; 264 st->silk_mode.minInternalSampleRate = 8000; 265 st->silk_mode.desiredInternalSampleRate = 16000; 266 st->silk_mode.payloadSize_ms = 20; 267 st->silk_mode.bitRate = 25000; 268 st->silk_mode.packetLossPercentage = 0; 269 st->silk_mode.complexity = 9; 270 st->silk_mode.useInBandFEC = 0; 271 st->silk_mode.useDRED = 0; 272 st->silk_mode.useDTX = 0; 273 st->silk_mode.useCBR = 0; 274 st->silk_mode.reducedDependency = 0; 275 276 /* Create CELT encoder */ 277 /* Initialize CELT encoder */ 278 if (application != OPUS_APPLICATION_RESTRICTED_SILK) 279 { 280 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 281 err = celt_encoder_init(celt_enc, Fs, channels, st->arch); 282 if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR; 283 celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); 284 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity)); 285 } 286 287 #ifdef ENABLE_DRED 288 /* Initialize DRED Encoder */ 289 dred_encoder_init( &st->dred_encoder, Fs, channels ); 290 #endif 291 292 st->use_vbr = 1; 293 /* Makes constrained VBR the default (safer for real-time use) */ 294 st->vbr_constraint = 1; 295 st->user_bitrate_bps = OPUS_AUTO; 296 st->bitrate_bps = 3000+Fs*channels; 297 st->application = application; 298 st->signal_type = OPUS_AUTO; 299 st->user_bandwidth = OPUS_AUTO; 300 st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND; 301 st->force_channels = OPUS_AUTO; 302 st->user_forced_mode = OPUS_AUTO; 303 st->voice_ratio = -1; 304 if (application != OPUS_APPLICATION_RESTRICTED_CELT && application != OPUS_APPLICATION_RESTRICTED_SILK) 305 st->encoder_buffer = st->Fs/100; 306 else 307 st->encoder_buffer = 0; 308 st->lsb_depth = 24; 309 st->variable_duration = OPUS_FRAMESIZE_ARG; 310 311 /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead 312 + 1.5 ms for SILK resamplers and stereo prediction) */ 313 st->delay_compensation = st->Fs/250; 314 315 st->hybrid_stereo_width_Q14 = 1 << 14; 316 st->prev_HB_gain = Q15ONE; 317 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 318 st->first = 1; 319 st->mode = MODE_HYBRID; 320 st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 321 322 #ifndef DISABLE_FLOAT_API 323 tonality_analysis_init(&st->analysis, st->Fs); 324 st->analysis.application = st->application; 325 #endif 326 327 return OPUS_OK; 328 } 329 330 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels) 331 { 332 int period; 333 unsigned char toc; 334 period = 0; 335 while (framerate < 400) 336 { 337 framerate <<= 1; 338 period++; 339 } 340 if (mode == MODE_SILK_ONLY) 341 { 342 toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5; 343 toc |= (period-2)<<3; 344 } else if (mode == MODE_CELT_ONLY) 345 { 346 int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND; 347 if (tmp < 0) 348 tmp = 0; 349 toc = 0x80; 350 toc |= tmp << 5; 351 toc |= period<<3; 352 } else /* Hybrid */ 353 { 354 toc = 0x60; 355 toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4; 356 toc |= (period-2)<<3; 357 } 358 toc |= (channels==2)<<2; 359 return toc; 360 } 361 362 #ifdef FIXED_POINT 363 /* Second order ARMA filter, alternative implementation */ 364 void silk_biquad_res( 365 const opus_res *in, /* I input signal */ 366 const opus_int32 *B_Q28, /* I MA coefficients [3] */ 367 const opus_int32 *A_Q28, /* I AR coefficients [2] */ 368 opus_int32 *S, /* I/O State vector [2] */ 369 opus_res *out, /* O output signal */ 370 const opus_int32 len, /* I signal length (must be even) */ 371 int stride 372 ) 373 { 374 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ 375 opus_int k; 376 opus_int32 inval, A0_U_Q28, A0_L_Q28, A1_U_Q28, A1_L_Q28, out32_Q14; 377 378 /* Negate A_Q28 values and split in two parts */ 379 A0_L_Q28 = ( -A_Q28[ 0 ] ) & 0x00003FFF; /* lower part */ 380 A0_U_Q28 = silk_RSHIFT( -A_Q28[ 0 ], 14 ); /* upper part */ 381 A1_L_Q28 = ( -A_Q28[ 1 ] ) & 0x00003FFF; /* lower part */ 382 A1_U_Q28 = silk_RSHIFT( -A_Q28[ 1 ], 14 ); /* upper part */ 383 384 for( k = 0; k < len; k++ ) { 385 /* S[ 0 ], S[ 1 ]: Q12 */ 386 inval = RES2INT16(in[ k*stride ]); 387 out32_Q14 = silk_LSHIFT( silk_SMLAWB( S[ 0 ], B_Q28[ 0 ], inval ), 2 ); 388 389 S[ 0 ] = S[1] + silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A0_L_Q28 ), 14 ); 390 S[ 0 ] = silk_SMLAWB( S[ 0 ], out32_Q14, A0_U_Q28 ); 391 S[ 0 ] = silk_SMLAWB( S[ 0 ], B_Q28[ 1 ], inval); 392 393 S[ 1 ] = silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A1_L_Q28 ), 14 ); 394 S[ 1 ] = silk_SMLAWB( S[ 1 ], out32_Q14, A1_U_Q28 ); 395 S[ 1 ] = silk_SMLAWB( S[ 1 ], B_Q28[ 2 ], inval ); 396 397 /* Scale back to Q0 and saturate */ 398 out[ k*stride ] = INT16TORES( silk_SAT16( silk_RSHIFT( out32_Q14 + (1<<14) - 1, 14 ) ) ); 399 } 400 } 401 #else 402 static void silk_biquad_res( 403 const opus_res *in, /* I: Input signal */ 404 const opus_int32 *B_Q28, /* I: MA coefficients [3] */ 405 const opus_int32 *A_Q28, /* I: AR coefficients [2] */ 406 opus_val32 *S, /* I/O: State vector [2] */ 407 opus_res *out, /* O: Output signal */ 408 const opus_int32 len, /* I: Signal length (must be even) */ 409 int stride 410 ) 411 { 412 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ 413 opus_int k; 414 opus_val32 vout; 415 opus_val32 inval; 416 opus_val32 A[2], B[3]; 417 418 A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28))); 419 A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28))); 420 B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28))); 421 B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28))); 422 B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28))); 423 424 /* Negate A_Q28 values and split in two parts */ 425 426 for( k = 0; k < len; k++ ) { 427 /* S[ 0 ], S[ 1 ]: Q12 */ 428 inval = in[ k*stride ]; 429 vout = S[ 0 ] + B[0]*inval; 430 431 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval; 432 433 S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL; 434 435 /* Scale back to Q0 and saturate */ 436 out[ k*stride ] = vout; 437 } 438 } 439 #endif 440 441 static void hp_cutoff(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs, int arch) 442 { 443 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ]; 444 opus_int32 Fc_Q19, r_Q28, r_Q22; 445 (void)arch; 446 447 silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) ); 448 Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 ); 449 silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 ); 450 451 r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 ); 452 453 /* b = r * [ 1; -2; 1 ]; */ 454 /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ 455 B_Q28[ 0 ] = r_Q28; 456 B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 ); 457 B_Q28[ 2 ] = r_Q28; 458 459 /* -r * ( 2 - Fc * Fc ); */ 460 r_Q22 = silk_RSHIFT( r_Q28, 6 ); 461 A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) ); 462 A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 ); 463 464 #if defined(FIXED_POINT) && !defined(ENABLE_RES24) 465 if( channels == 1 ) { 466 silk_biquad_alt_stride1( in, B_Q28, A_Q28, hp_mem, out, len ); 467 } else { 468 silk_biquad_alt_stride2( in, B_Q28, A_Q28, hp_mem, out, len, arch ); 469 } 470 #else 471 silk_biquad_res( in, B_Q28, A_Q28, hp_mem, out, len, channels ); 472 if( channels == 2 ) { 473 silk_biquad_res( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); 474 } 475 #endif 476 } 477 478 #ifdef FIXED_POINT 479 static void dc_reject(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) 480 { 481 int c, i; 482 int shift; 483 484 /* Approximates -round(log2(6.3*cutoff_Hz/Fs)) */ 485 shift=celt_ilog2(Fs/(cutoff_Hz*4)); 486 for (c=0;c<channels;c++) 487 { 488 for (i=0;i<len;i++) 489 { 490 opus_val32 x, y; 491 /* Saturate at +6 dBFS to avoid any wrap-around. */ 492 x = SATURATE((opus_val32)in[channels*i+c], (1<<16<<RES_SHIFT)-1); 493 x = SHL32(x, 14-RES_SHIFT); 494 y = x-hp_mem[2*c]; 495 hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift); 496 #ifdef ENABLE_RES24 497 /* Don't saturate if we have the headroom to avoid it. */ 498 out[channels*i+c] = PSHR32(y, 14-RES_SHIFT); 499 #else 500 out[channels*i+c] = SATURATE(PSHR32(y, 14-RES_SHIFT), 32767); 501 #endif 502 } 503 } 504 } 505 506 #else 507 static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) 508 { 509 int i; 510 float coef, coef2; 511 coef = 6.3f*cutoff_Hz/Fs; 512 coef2 = 1-coef; 513 if (channels==2) 514 { 515 float m0, m2; 516 m0 = hp_mem[0]; 517 m2 = hp_mem[2]; 518 for (i=0;i<len;i++) 519 { 520 opus_val32 x0, x1, out0, out1; 521 x0 = in[2*i+0]; 522 x1 = in[2*i+1]; 523 out0 = x0-m0; 524 out1 = x1-m2; 525 m0 = coef*x0 + VERY_SMALL + coef2*m0; 526 m2 = coef*x1 + VERY_SMALL + coef2*m2; 527 out[2*i+0] = out0; 528 out[2*i+1] = out1; 529 } 530 hp_mem[0] = m0; 531 hp_mem[2] = m2; 532 } else { 533 float m0; 534 m0 = hp_mem[0]; 535 for (i=0;i<len;i++) 536 { 537 opus_val32 x, y; 538 x = in[i]; 539 y = x-m0; 540 m0 = coef*x + VERY_SMALL + coef2*m0; 541 out[i] = y; 542 } 543 hp_mem[0] = m0; 544 } 545 } 546 #endif 547 548 static void stereo_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2, 549 int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs) 550 { 551 int i; 552 int overlap; 553 int inc; 554 inc = IMAX(1, 48000/Fs); 555 overlap=overlap48/inc; 556 g1 = Q15ONE-g1; 557 g2 = Q15ONE-g2; 558 for (i=0;i<overlap;i++) 559 { 560 opus_val32 diff; 561 opus_val16 g, w; 562 w = COEF2VAL16(window[i*inc]); 563 w = MULT16_16_Q15(w, w); 564 g = SHR32(MAC16_16(MULT16_16(w,g2), 565 Q15ONE-w, g1), 15); 566 diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]); 567 diff = MULT16_RES_Q15(g, diff); 568 out[i*channels] = out[i*channels] - diff; 569 out[i*channels+1] = out[i*channels+1] + diff; 570 } 571 for (;i<frame_size;i++) 572 { 573 opus_val32 diff; 574 diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]); 575 diff = MULT16_RES_Q15(g2, diff); 576 out[i*channels] = out[i*channels] - diff; 577 out[i*channels+1] = out[i*channels+1] + diff; 578 } 579 } 580 581 static void gain_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2, 582 int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs) 583 { 584 int i; 585 int inc; 586 int overlap; 587 int c; 588 inc = IMAX(1, 48000/Fs); 589 overlap=overlap48/inc; 590 if (channels==1) 591 { 592 for (i=0;i<overlap;i++) 593 { 594 opus_val16 g, w; 595 w = COEF2VAL16(window[i*inc]); 596 w = MULT16_16_Q15(w, w); 597 g = SHR32(MAC16_16(MULT16_16(w,g2), 598 Q15ONE-w, g1), 15); 599 out[i] = MULT16_RES_Q15(g, in[i]); 600 } 601 } else { 602 for (i=0;i<overlap;i++) 603 { 604 opus_val16 g, w; 605 w = COEF2VAL16(window[i*inc]); 606 w = MULT16_16_Q15(w, w); 607 g = SHR32(MAC16_16(MULT16_16(w,g2), 608 Q15ONE-w, g1), 15); 609 out[i*2] = MULT16_RES_Q15(g, in[i*2]); 610 out[i*2+1] = MULT16_RES_Q15(g, in[i*2+1]); 611 } 612 } 613 c=0;do { 614 for (i=overlap;i<frame_size;i++) 615 { 616 out[i*channels+c] = MULT16_RES_Q15(g2, in[i*channels+c]); 617 } 618 } 619 while (++c<channels); 620 } 621 622 OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error) 623 { 624 int ret; 625 OpusEncoder *st; 626 int size; 627 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000 628 #ifdef ENABLE_QEXT 629 &&Fs!=96000 630 #endif 631 )||(channels!=1&&channels!=2)|| 632 (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO 633 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY 634 && application != OPUS_APPLICATION_RESTRICTED_SILK 635 && application != OPUS_APPLICATION_RESTRICTED_CELT)) 636 { 637 if (error) 638 *error = OPUS_BAD_ARG; 639 return NULL; 640 } 641 size = opus_encoder_init(NULL, Fs, channels, application); 642 if (size <= 0) 643 { 644 if (error) 645 *error = OPUS_INTERNAL_ERROR; 646 return NULL; 647 } 648 st = (OpusEncoder *)opus_alloc(size); 649 if (st == NULL) 650 { 651 if (error) 652 *error = OPUS_ALLOC_FAIL; 653 return NULL; 654 } 655 ret = opus_encoder_init(st, Fs, channels, application); 656 if (error) 657 *error = ret; 658 if (ret != OPUS_OK) 659 { 660 opus_free(st); 661 st = NULL; 662 } 663 return st; 664 } 665 666 #ifdef ENABLE_DRED 667 668 static const float dred_bits_table[16] = {73.2f, 68.1f, 62.5f, 57.0f, 51.5f, 45.7f, 39.9f, 32.4f, 26.4f, 20.4f, 16.3f, 13.f, 9.3f, 8.2f, 7.2f, 6.4f}; 669 static int estimate_dred_bitrate(int q0, int dQ, int qmax, int duration, opus_int32 target_bits, int *target_chunks) { 670 int dred_chunks; 671 int i; 672 float bits; 673 /* Signaling DRED costs 3 bytes. */ 674 bits = 8*(3+DRED_EXPERIMENTAL_BYTES); 675 /* Approximation for the size of the IS. */ 676 bits += 50.f+dred_bits_table[q0]; 677 dred_chunks = IMIN((duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2); 678 if (target_chunks != NULL) *target_chunks = 0; 679 for (i=0;i<dred_chunks;i++) { 680 int q = compute_quantizer(q0, dQ, qmax, i); 681 bits += dred_bits_table[q]; 682 if (target_chunks != NULL && bits < target_bits) *target_chunks = i+1; 683 } 684 return (int)floor(.5f+bits); 685 } 686 687 static opus_int32 compute_dred_bitrate(OpusEncoder *st, opus_int32 bitrate_bps, int frame_size) 688 { 689 float dred_frac; 690 int bitrate_offset; 691 opus_int32 dred_bitrate; 692 opus_int32 target_dred_bitrate; 693 int target_chunks; 694 opus_int32 max_dred_bits; 695 int q0, dQ, qmax; 696 if (st->silk_mode.useInBandFEC) { 697 dred_frac = MIN16(.7f, 3.f*st->silk_mode.packetLossPercentage/100.f); 698 bitrate_offset = 20000; 699 } else { 700 if (st->silk_mode.packetLossPercentage > 5) { 701 dred_frac = MIN16(.8f, .55f + st->silk_mode.packetLossPercentage/100.f); 702 } else { 703 dred_frac = 12*st->silk_mode.packetLossPercentage/100.f; 704 } 705 bitrate_offset = 12000; 706 } 707 /* Account for the fact that longer packets require less redundancy. */ 708 dred_frac = dred_frac/(dred_frac + (1-dred_frac)*(frame_size*50.f)/st->Fs); 709 /* Approximate fit based on a few experiments. Could probably be improved. */ 710 q0 = IMIN(15, IMAX(4, 51 - 3*EC_ILOG(IMAX(1, bitrate_bps-bitrate_offset)))); 711 dQ = bitrate_bps-bitrate_offset > 36000 ? 3 : 5; 712 qmax = 15; 713 target_dred_bitrate = IMAX(0, (int)(dred_frac*(bitrate_bps-bitrate_offset))); 714 if (st->dred_duration > 0) { 715 opus_int32 target_bits = bitrate_to_bits(target_dred_bitrate, st->Fs, frame_size); 716 max_dred_bits = estimate_dred_bitrate(q0, dQ, qmax, st->dred_duration, target_bits, &target_chunks); 717 } else { 718 max_dred_bits = 0; 719 target_chunks=0; 720 } 721 dred_bitrate = IMIN(target_dred_bitrate, bits_to_bitrate(max_dred_bits, st->Fs, frame_size)); 722 /* If we can't afford enough bits, don't bother with DRED at all. */ 723 if (target_chunks < 2) 724 dred_bitrate = 0; 725 st->dred_q0 = q0; 726 st->dred_dQ = dQ; 727 st->dred_qmax = qmax; 728 st->dred_target_chunks = target_chunks; 729 return dred_bitrate; 730 } 731 #endif 732 733 static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes) 734 { 735 opus_int32 max_bitrate, user_bitrate; 736 if(!frame_size)frame_size=st->Fs/400; 737 max_bitrate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size); 738 if (st->user_bitrate_bps==OPUS_AUTO) 739 user_bitrate = 60*st->Fs/frame_size + st->Fs*st->channels; 740 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX) 741 user_bitrate = 1500000; 742 else 743 user_bitrate = st->user_bitrate_bps; 744 return IMIN(user_bitrate, max_bitrate); 745 } 746 747 #ifndef DISABLE_FLOAT_API 748 void downmix_float(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C) 749 { 750 const float *x; 751 int j; 752 753 x = (const float *)_x; 754 for (j=0;j<subframe;j++) 755 y[j] = FLOAT2SIG(x[(j+offset)*C+c1]); 756 if (c2>-1) 757 { 758 for (j=0;j<subframe;j++) 759 y[j] += FLOAT2SIG(x[(j+offset)*C+c2]); 760 } else if (c2==-2) 761 { 762 int c; 763 for (c=1;c<C;c++) 764 { 765 for (j=0;j<subframe;j++) 766 y[j] += FLOAT2SIG(x[(j+offset)*C+c]); 767 } 768 } 769 #ifndef FIXED_POINT 770 /* Cap signal to +6 dBFS to avoid problems in the analysis. */ 771 for (j=0;j<subframe;j++) 772 { 773 if (y[j] < -65536.f) y[j] = -65536.f; 774 if (y[j] > 65536.f) y[j] = 65536.f; 775 if (celt_isnan(y[j])) y[j] = 0; 776 } 777 #endif 778 } 779 #endif 780 781 void downmix_int(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C) 782 { 783 const opus_int16 *x; 784 int j; 785 786 x = (const opus_int16 *)_x; 787 for (j=0;j<subframe;j++) 788 y[j] = INT16TOSIG(x[(j+offset)*C+c1]); 789 if (c2>-1) 790 { 791 for (j=0;j<subframe;j++) 792 y[j] += INT16TOSIG(x[(j+offset)*C+c2]); 793 } else if (c2==-2) 794 { 795 int c; 796 for (c=1;c<C;c++) 797 { 798 for (j=0;j<subframe;j++) 799 y[j] += INT16TOSIG(x[(j+offset)*C+c]); 800 } 801 } 802 } 803 804 void downmix_int24(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C) 805 { 806 const opus_int32 *x; 807 int j; 808 809 x = (const opus_int32 *)_x; 810 for (j=0;j<subframe;j++) 811 y[j] = INT24TOSIG(x[(j+offset)*C+c1]); 812 if (c2>-1) 813 { 814 for (j=0;j<subframe;j++) 815 y[j] += INT24TOSIG(x[(j+offset)*C+c2]); 816 } else if (c2==-2) 817 { 818 int c; 819 for (c=1;c<C;c++) 820 { 821 for (j=0;j<subframe;j++) 822 y[j] += INT24TOSIG(x[(j+offset)*C+c]); 823 } 824 } 825 } 826 827 opus_int32 frame_size_select(int application, opus_int32 frame_size, int variable_duration, opus_int32 Fs) 828 { 829 int new_size; 830 if (frame_size<Fs/400) 831 return -1; 832 if (variable_duration == OPUS_FRAMESIZE_ARG) 833 new_size = frame_size; 834 else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_120_MS) 835 { 836 if (variable_duration <= OPUS_FRAMESIZE_40_MS) 837 new_size = (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS); 838 else 839 new_size = (variable_duration-OPUS_FRAMESIZE_2_5_MS-2)*Fs/50; 840 } 841 else 842 return -1; 843 if (new_size>frame_size) 844 return -1; 845 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs && 846 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs && 847 50*new_size!=4*Fs && 50*new_size!=5*Fs && 50*new_size!=6*Fs) 848 return -1; 849 if (application == OPUS_APPLICATION_RESTRICTED_SILK && new_size < Fs/100) 850 return -1; 851 return new_size; 852 } 853 854 opus_val16 compute_stereo_width(const opus_res *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem) 855 { 856 opus_val32 xx, xy, yy; 857 opus_val16 sqrt_xx, sqrt_yy; 858 opus_val16 qrrt_xx, qrrt_yy; 859 int frame_rate; 860 int i; 861 opus_val16 short_alpha; 862 863 frame_rate = Fs/frame_size; 864 short_alpha = Q15ONE - MULT16_16(25, Q15ONE)/IMAX(50,frame_rate); 865 xx=xy=yy=0; 866 /* Unroll by 4. The frame size is always a multiple of 4 *except* for 867 2.5 ms frames at 12 kHz. Since this setting is very rare (and very 868 stupid), we just discard the last two samples. */ 869 for (i=0;i<frame_size-3;i+=4) 870 { 871 opus_val32 pxx=0; 872 opus_val32 pxy=0; 873 opus_val32 pyy=0; 874 opus_val16 x, y; 875 x = RES2VAL16(pcm[2*i]); 876 y = RES2VAL16(pcm[2*i+1]); 877 pxx = SHR32(MULT16_16(x,x),2); 878 pxy = SHR32(MULT16_16(x,y),2); 879 pyy = SHR32(MULT16_16(y,y),2); 880 x = RES2VAL16(pcm[2*i+2]); 881 y = RES2VAL16(pcm[2*i+3]); 882 pxx += SHR32(MULT16_16(x,x),2); 883 pxy += SHR32(MULT16_16(x,y),2); 884 pyy += SHR32(MULT16_16(y,y),2); 885 x = RES2VAL16(pcm[2*i+4]); 886 y = RES2VAL16(pcm[2*i+5]); 887 pxx += SHR32(MULT16_16(x,x),2); 888 pxy += SHR32(MULT16_16(x,y),2); 889 pyy += SHR32(MULT16_16(y,y),2); 890 x = RES2VAL16(pcm[2*i+6]); 891 y = RES2VAL16(pcm[2*i+7]); 892 pxx += SHR32(MULT16_16(x,x),2); 893 pxy += SHR32(MULT16_16(x,y),2); 894 pyy += SHR32(MULT16_16(y,y),2); 895 896 xx += SHR32(pxx, 10); 897 xy += SHR32(pxy, 10); 898 yy += SHR32(pyy, 10); 899 } 900 #ifndef FIXED_POINT 901 if (!(xx < 1e9f) || celt_isnan(xx) || !(yy < 1e9f) || celt_isnan(yy)) 902 { 903 xy = xx = yy = 0; 904 } 905 #endif 906 mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX); 907 mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY); 908 mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY); 909 mem->XX = MAX32(0, mem->XX); 910 mem->XY = MAX32(0, mem->XY); 911 mem->YY = MAX32(0, mem->YY); 912 if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18)) 913 { 914 opus_val16 corr; 915 opus_val16 ldiff; 916 opus_val16 width; 917 sqrt_xx = celt_sqrt(mem->XX); 918 sqrt_yy = celt_sqrt(mem->YY); 919 qrrt_xx = celt_sqrt(sqrt_xx); 920 qrrt_yy = celt_sqrt(sqrt_yy); 921 /* Inter-channel correlation */ 922 mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy); 923 corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16); 924 /* Approximate loudness difference */ 925 ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))/(EPSILON+qrrt_xx+qrrt_yy); 926 width = MULT16_16_Q15(MIN16(Q15ONE, celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr))), ldiff); 927 /* Smoothing over one second */ 928 mem->smoothed_width += (width-mem->smoothed_width)/frame_rate; 929 /* Peak follower */ 930 mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width); 931 } 932 /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(float)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE);*/ 933 return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower))); 934 } 935 936 static int decide_fec(int useInBandFEC, int PacketLoss_perc, int last_fec, int mode, int *bandwidth, opus_int32 rate) 937 { 938 int orig_bandwidth; 939 if (!useInBandFEC || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY) 940 return 0; 941 orig_bandwidth = *bandwidth; 942 for (;;) 943 { 944 opus_int32 hysteresis; 945 opus_int32 LBRR_rate_thres_bps; 946 /* Compute threshold for using FEC at the current bandwidth setting */ 947 LBRR_rate_thres_bps = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND)]; 948 hysteresis = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1]; 949 if (last_fec == 1) LBRR_rate_thres_bps -= hysteresis; 950 if (last_fec == 0) LBRR_rate_thres_bps += hysteresis; 951 LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps, 952 125 - silk_min( PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) ); 953 /* If loss <= 5%, we look at whether we have enough rate to enable FEC. 954 If loss > 5%, we decrease the bandwidth until we can enable FEC. */ 955 if (rate > LBRR_rate_thres_bps) 956 return 1; 957 else if (PacketLoss_perc <= 5) 958 return 0; 959 else if (*bandwidth > OPUS_BANDWIDTH_NARROWBAND) 960 (*bandwidth)--; 961 else 962 break; 963 } 964 /* Couldn't find any bandwidth to enable FEC, keep original bandwidth. */ 965 *bandwidth = orig_bandwidth; 966 return 0; 967 } 968 969 static int compute_silk_rate_for_hybrid(int rate, int bandwidth, int frame20ms, int vbr, int fec, int channels) { 970 int entry; 971 int i; 972 int N; 973 int silk_rate; 974 static const int rate_table[][5] = { 975 /* |total| |-------- SILK------------| 976 |-- No FEC -| |--- FEC ---| 977 10ms 20ms 10ms 20ms */ 978 { 0, 0, 0, 0, 0}, 979 {12000, 10000, 10000, 11000, 11000}, 980 {16000, 13500, 13500, 15000, 15000}, 981 {20000, 16000, 16000, 18000, 18000}, 982 {24000, 18000, 18000, 21000, 21000}, 983 {32000, 22000, 22000, 28000, 28000}, 984 {64000, 38000, 38000, 50000, 50000} 985 }; 986 /* Do the allocation per-channel. */ 987 rate /= channels; 988 entry = 1 + frame20ms + 2*fec; 989 N = sizeof(rate_table)/sizeof(rate_table[0]); 990 for (i=1;i<N;i++) 991 { 992 if (rate_table[i][0] > rate) break; 993 } 994 if (i == N) 995 { 996 silk_rate = rate_table[i-1][entry]; 997 /* For now, just give 50% of the extra bits to SILK. */ 998 silk_rate += (rate-rate_table[i-1][0])/2; 999 } else { 1000 opus_int32 lo, hi, x0, x1; 1001 lo = rate_table[i-1][entry]; 1002 hi = rate_table[i][entry]; 1003 x0 = rate_table[i-1][0]; 1004 x1 = rate_table[i][0]; 1005 silk_rate = (lo*(x1-rate) + hi*(rate-x0))/(x1-x0); 1006 } 1007 if (!vbr) 1008 { 1009 /* Tiny boost to SILK for CBR. We should probably tune this better. */ 1010 silk_rate += 100; 1011 } 1012 if (bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND) 1013 silk_rate += 300; 1014 silk_rate *= channels; 1015 /* Small adjustment for stereo (calibrated for 32 kb/s, haven't tried other bitrates). */ 1016 if (channels == 2 && rate >= 12000) 1017 silk_rate -= 1000; 1018 return silk_rate; 1019 } 1020 1021 /* Returns the equivalent bitrate corresponding to 20 ms frames, 1022 complexity 10 VBR operation. */ 1023 static opus_int32 compute_equiv_rate(opus_int32 bitrate, int channels, 1024 int frame_rate, int vbr, int mode, int complexity, int loss) 1025 { 1026 opus_int32 equiv; 1027 equiv = bitrate; 1028 /* Take into account overhead from smaller frames. */ 1029 if (frame_rate > 50) 1030 equiv -= (40*channels+20)*(frame_rate - 50); 1031 /* CBR is about a 8% penalty for both SILK and CELT. */ 1032 if (!vbr) 1033 equiv -= equiv/12; 1034 /* Complexity makes about 10% difference (from 0 to 10) in general. */ 1035 equiv = equiv * (90+complexity)/100; 1036 if (mode == MODE_SILK_ONLY || mode == MODE_HYBRID) 1037 { 1038 /* SILK complexity 0-1 uses the non-delayed-decision NSQ, which 1039 costs about 20%. */ 1040 if (complexity<2) 1041 equiv = equiv*4/5; 1042 equiv -= equiv*loss/(6*loss + 10); 1043 } else if (mode == MODE_CELT_ONLY) { 1044 /* CELT complexity 0-4 doesn't have the pitch filter, which costs 1045 about 10%. */ 1046 if (complexity<5) 1047 equiv = equiv*9/10; 1048 } else { 1049 /* Mode not known yet */ 1050 /* Half the SILK loss*/ 1051 equiv -= equiv*loss/(12*loss + 20); 1052 } 1053 return equiv; 1054 } 1055 1056 int is_digital_silence(const opus_res* pcm, int frame_size, int channels, int lsb_depth) 1057 { 1058 int silence = 0; 1059 opus_val32 sample_max = 0; 1060 #ifdef MLP_TRAINING 1061 return 0; 1062 #endif 1063 sample_max = celt_maxabs_res(pcm, frame_size*channels); 1064 1065 #ifdef FIXED_POINT 1066 silence = (sample_max == 0); 1067 (void)lsb_depth; 1068 #else 1069 silence = (sample_max <= (opus_val16) 1 / (1 << lsb_depth)); 1070 #endif 1071 1072 return silence; 1073 } 1074 1075 #ifdef FIXED_POINT 1076 static opus_val32 compute_frame_energy(const opus_res *pcm, int frame_size, int channels, int arch) 1077 { 1078 int i; 1079 opus_val32 sample_max; 1080 int max_shift; 1081 int shift; 1082 opus_val32 energy = 0; 1083 int len = frame_size*channels; 1084 (void)arch; 1085 /* Max amplitude in the signal */ 1086 sample_max = RES2INT16(celt_maxabs_res(pcm, len)); 1087 1088 /* Compute the right shift required in the MAC to avoid an overflow */ 1089 max_shift = celt_ilog2(len); 1090 shift = IMAX(0, (celt_ilog2(1+sample_max) << 1) + max_shift - 28); 1091 1092 /* Compute the energy */ 1093 for (i=0; i<len; i++) 1094 energy += SHR32(MULT16_16(RES2INT16(pcm[i]), RES2INT16(pcm[i])), shift); 1095 1096 /* Normalize energy by the frame size and left-shift back to the original position */ 1097 energy /= len; 1098 energy = SHL32(energy, shift); 1099 1100 return energy; 1101 } 1102 #else 1103 static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch) 1104 { 1105 int len = frame_size*channels; 1106 return celt_inner_prod(pcm, pcm, len, arch)/len; 1107 } 1108 #endif 1109 1110 /* Decides if DTX should be turned on (=1) or off (=0) */ 1111 static int decide_dtx_mode(opus_int activity, /* indicates if this frame contains speech/music */ 1112 int *nb_no_activity_ms_Q1, /* number of consecutive milliseconds with no activity, in Q1 */ 1113 int frame_size_ms_Q1 /* number of milliseconds in this update, in Q1 */ 1114 ) 1115 1116 { 1117 if (!activity) 1118 { 1119 /* The number of consecutive DTX frames should be within the allowed bounds. 1120 Note that the allowed bound is defined in the SILK headers and assumes 20 ms 1121 frames. As this function can be called with any frame length, a conversion to 1122 milliseconds is done before the comparisons. */ 1123 (*nb_no_activity_ms_Q1) += frame_size_ms_Q1; 1124 if (*nb_no_activity_ms_Q1 > NB_SPEECH_FRAMES_BEFORE_DTX*20*2) 1125 { 1126 if (*nb_no_activity_ms_Q1 <= (NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX)*20*2) 1127 /* Valid frame for DTX! */ 1128 return 1; 1129 else 1130 (*nb_no_activity_ms_Q1) = NB_SPEECH_FRAMES_BEFORE_DTX*20*2; 1131 } 1132 } else 1133 (*nb_no_activity_ms_Q1) = 0; 1134 1135 return 0; 1136 } 1137 1138 static int compute_redundancy_bytes(opus_int32 max_data_bytes, opus_int32 bitrate_bps, int frame_rate, int channels) 1139 { 1140 int redundancy_bytes_cap; 1141 int redundancy_bytes; 1142 opus_int32 redundancy_rate; 1143 int base_bits; 1144 opus_int32 available_bits; 1145 base_bits = (40*channels+20); 1146 1147 /* Equivalent rate for 5 ms frames. */ 1148 redundancy_rate = bitrate_bps + base_bits*(200 - frame_rate); 1149 /* For VBR, further increase the bitrate if we can afford it. It's pretty short 1150 and we'll avoid artefacts. */ 1151 redundancy_rate = 3*redundancy_rate/2; 1152 redundancy_bytes = redundancy_rate/1600; 1153 1154 /* Compute the max rate we can use given CBR or VBR with cap. */ 1155 available_bits = max_data_bytes*8 - 2*base_bits; 1156 redundancy_bytes_cap = (available_bits*240/(240+48000/frame_rate) + base_bits)/8; 1157 redundancy_bytes = IMIN(redundancy_bytes, redundancy_bytes_cap); 1158 /* It we can't get enough bits for redundancy to be worth it, rely on the decoder PLC. */ 1159 if (redundancy_bytes > 4 + 8*channels) 1160 redundancy_bytes = IMIN(257, redundancy_bytes); 1161 else 1162 redundancy_bytes = 0; 1163 return redundancy_bytes; 1164 } 1165 1166 static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size, 1167 unsigned char *data, opus_int32 max_data_bytes, 1168 int float_api, int first_frame, 1169 #ifdef ENABLE_DRED 1170 opus_int32 dred_bitrate_bps, 1171 #endif 1172 #ifndef DISABLE_FLOAT_API 1173 AnalysisInfo *analysis_info, 1174 #endif 1175 int is_silence, int redundancy, int celt_to_silk, int prefill, 1176 opus_int32 equiv_rate, int to_celt); 1177 1178 opus_int32 opus_encode_native(OpusEncoder *st, const opus_res *pcm, int frame_size, 1179 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth, 1180 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, 1181 int analysis_channels, downmix_func downmix, int float_api) 1182 { 1183 void *silk_enc=NULL; 1184 CELTEncoder *celt_enc=NULL; 1185 int i; 1186 int ret=0; 1187 int prefill=0; 1188 int redundancy = 0; 1189 int celt_to_silk = 0; 1190 int to_celt = 0; 1191 int voice_est; /* Probability of voice in Q7 */ 1192 opus_int32 equiv_rate; 1193 int frame_rate; 1194 opus_int32 max_rate; /* Max bitrate we're allowed to use */ 1195 int curr_bandwidth; 1196 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ 1197 opus_int32 cbr_bytes=-1; 1198 opus_val16 stereo_width; 1199 const CELTMode *celt_mode=NULL; 1200 int packet_size_cap = 1276; 1201 #ifndef DISABLE_FLOAT_API 1202 AnalysisInfo analysis_info; 1203 int analysis_read_pos_bak=-1; 1204 int analysis_read_subframe_bak=-1; 1205 #endif 1206 int is_silence = 0; 1207 #ifdef ENABLE_DRED 1208 opus_int32 dred_bitrate_bps; 1209 #endif 1210 ALLOC_STACK; 1211 1212 #ifdef ENABLE_QEXT 1213 if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP; 1214 #endif 1215 1216 /* Just avoid insane packet sizes here, but the real bounds are applied later on. */ 1217 max_data_bytes = IMIN(packet_size_cap*6, out_data_bytes); 1218 1219 st->rangeFinal = 0; 1220 if (frame_size <= 0 || max_data_bytes <= 0) 1221 { 1222 RESTORE_STACK; 1223 return OPUS_BAD_ARG; 1224 } 1225 1226 /* Cannot encode 100 ms in 1 byte */ 1227 if (max_data_bytes==1 && st->Fs==(frame_size*10)) 1228 { 1229 RESTORE_STACK; 1230 return OPUS_BUFFER_TOO_SMALL; 1231 } 1232 1233 if (st->application != OPUS_APPLICATION_RESTRICTED_CELT) 1234 silk_enc = (char*)st+st->silk_enc_offset; 1235 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 1236 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 1237 1238 lsb_depth = IMIN(lsb_depth, st->lsb_depth); 1239 1240 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 1241 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); 1242 is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth); 1243 #ifndef DISABLE_FLOAT_API 1244 analysis_info.valid = 0; 1245 #ifdef FIXED_POINT 1246 if (st->silk_mode.complexity >= 10 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_RESTRICTED_SILK) 1247 #else 1248 if (st->silk_mode.complexity >= 7 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_RESTRICTED_SILK) 1249 #endif 1250 { 1251 analysis_read_pos_bak = st->analysis.read_pos; 1252 analysis_read_subframe_bak = st->analysis.read_subframe; 1253 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size, 1254 c1, c2, analysis_channels, st->Fs, 1255 lsb_depth, downmix, &analysis_info); 1256 1257 } else if (st->analysis.initialized) { 1258 tonality_analysis_reset(&st->analysis); 1259 } 1260 #else 1261 (void)analysis_pcm; 1262 (void)analysis_size; 1263 (void)c1; 1264 (void)c2; 1265 (void)analysis_channels; 1266 (void)downmix; 1267 #endif 1268 1269 /* Reset voice_ratio if this frame is not silent or if analysis is disabled. 1270 * Otherwise, preserve voice_ratio from the last non-silent frame */ 1271 if (!is_silence) 1272 st->voice_ratio = -1; 1273 #ifndef DISABLE_FLOAT_API 1274 st->detected_bandwidth = 0; 1275 if (analysis_info.valid) 1276 { 1277 int analysis_bandwidth; 1278 if (st->signal_type == OPUS_AUTO) 1279 { 1280 float prob; 1281 if (st->prev_mode == 0) 1282 prob = analysis_info.music_prob; 1283 else if (st->prev_mode == MODE_CELT_ONLY) 1284 prob = analysis_info.music_prob_max; 1285 else 1286 prob = analysis_info.music_prob_min; 1287 st->voice_ratio = (int)floor(.5+100*(1-prob)); 1288 } 1289 1290 analysis_bandwidth = analysis_info.bandwidth; 1291 if (analysis_bandwidth<=12) 1292 st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1293 else if (analysis_bandwidth<=14) 1294 st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1295 else if (analysis_bandwidth<=16) 1296 st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1297 else if (analysis_bandwidth<=18) 1298 st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1299 else 1300 st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; 1301 } 1302 #else 1303 st->voice_ratio = -1; 1304 #endif 1305 1306 /* Track the peak signal energy */ 1307 #ifndef DISABLE_FLOAT_API 1308 if (!analysis_info.valid || analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD) 1309 #endif 1310 { 1311 if (!is_silence) 1312 { 1313 st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy), 1314 compute_frame_energy(pcm, frame_size, st->channels, st->arch)); 1315 } 1316 } 1317 if (st->channels==2 && st->force_channels!=1) 1318 stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem); 1319 else 1320 stereo_width = 0; 1321 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes); 1322 1323 frame_rate = st->Fs/frame_size; 1324 if (!st->use_vbr) 1325 { 1326 cbr_bytes = IMIN((bitrate_to_bits(st->bitrate_bps, st->Fs, frame_size)+4)/8, max_data_bytes); 1327 st->bitrate_bps = bits_to_bitrate(cbr_bytes*8, st->Fs, frame_size); 1328 /* Make sure we provide at least one byte to avoid failing. */ 1329 max_data_bytes = IMAX(1, cbr_bytes); 1330 } 1331 #ifdef ENABLE_DRED 1332 /* Allocate some of the bits to DRED if needed. */ 1333 dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size); 1334 st->bitrate_bps -= dred_bitrate_bps; 1335 #endif 1336 if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8 1337 || (frame_rate<50 && (max_data_bytes*(opus_int32)frame_rate<300 || st->bitrate_bps < 2400))) 1338 { 1339 /*If the space is too low to do something useful, emit 'PLC' frames.*/ 1340 int tocmode = st->mode; 1341 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth; 1342 int packet_code = 0; 1343 int num_multiframes = 0; 1344 1345 if (tocmode==0) 1346 tocmode = MODE_SILK_ONLY; 1347 if (frame_rate>100) 1348 tocmode = MODE_CELT_ONLY; 1349 /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */ 1350 if (frame_rate==25 && tocmode!=MODE_SILK_ONLY) 1351 { 1352 frame_rate = 50; 1353 packet_code = 1; 1354 } 1355 1356 /* >= 60 ms frames */ 1357 if (frame_rate<=16) 1358 { 1359 /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */ 1360 if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10)) 1361 { 1362 tocmode = MODE_SILK_ONLY; 1363 1364 packet_code = frame_rate <= 12; 1365 frame_rate = frame_rate == 12 ? 25 : 16; 1366 } 1367 else 1368 { 1369 num_multiframes = 50/frame_rate; 1370 frame_rate = 50; 1371 packet_code = 3; 1372 } 1373 } 1374 1375 if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND) 1376 bw=OPUS_BANDWIDTH_WIDEBAND; 1377 else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND) 1378 bw=OPUS_BANDWIDTH_NARROWBAND; 1379 else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND) 1380 bw=OPUS_BANDWIDTH_SUPERWIDEBAND; 1381 1382 data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels); 1383 data[0] |= packet_code; 1384 1385 ret = packet_code <= 1 ? 1 : 2; 1386 1387 max_data_bytes = IMAX(max_data_bytes, ret); 1388 1389 if (packet_code==3) 1390 data[1] = num_multiframes; 1391 1392 if (!st->use_vbr) 1393 { 1394 ret = opus_packet_pad(data, ret, max_data_bytes); 1395 if (ret == OPUS_OK) 1396 ret = max_data_bytes; 1397 else 1398 ret = OPUS_INTERNAL_ERROR; 1399 } 1400 RESTORE_STACK; 1401 return ret; 1402 } 1403 max_rate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size); 1404 1405 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ 1406 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size, 1407 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage); 1408 1409 if (st->signal_type == OPUS_SIGNAL_VOICE) 1410 voice_est = 127; 1411 else if (st->signal_type == OPUS_SIGNAL_MUSIC) 1412 voice_est = 0; 1413 else if (st->voice_ratio >= 0) 1414 { 1415 voice_est = st->voice_ratio*327>>8; 1416 /* For AUDIO, never be more than 90% confident of having speech */ 1417 if (st->application == OPUS_APPLICATION_AUDIO) 1418 voice_est = IMIN(voice_est, 115); 1419 } else if (st->application == OPUS_APPLICATION_VOIP) 1420 voice_est = 115; 1421 else 1422 voice_est = 48; 1423 1424 if (st->force_channels!=OPUS_AUTO && st->channels == 2) 1425 { 1426 st->stream_channels = st->force_channels; 1427 } else { 1428 #ifdef FUZZING 1429 (void)stereo_music_threshold; 1430 (void)stereo_voice_threshold; 1431 /* Random mono/stereo decision */ 1432 if (st->channels == 2 && (rand()&0x1F)==0) 1433 st->stream_channels = 3-st->stream_channels; 1434 #else 1435 /* Rate-dependent mono-stereo decision */ 1436 if (st->channels == 2) 1437 { 1438 opus_int32 stereo_threshold; 1439 stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14); 1440 if (st->stream_channels == 2) 1441 stereo_threshold -= 1000; 1442 else 1443 stereo_threshold += 1000; 1444 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; 1445 } else { 1446 st->stream_channels = st->channels; 1447 } 1448 #endif 1449 } 1450 /* Update equivalent rate for channels decision. */ 1451 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size, 1452 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage); 1453 1454 /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used, 1455 e.g. because of the complexity setting or sample rate. */ 1456 #ifndef DISABLE_FLOAT_API 1457 st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence); 1458 #else 1459 st->silk_mode.useDTX = st->use_dtx && !is_silence; 1460 #endif 1461 1462 /* Mode selection depending on application and signal type */ 1463 if (st->application == OPUS_APPLICATION_RESTRICTED_SILK) 1464 { 1465 st->mode = MODE_SILK_ONLY; 1466 } else if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_RESTRICTED_CELT) 1467 { 1468 st->mode = MODE_CELT_ONLY; 1469 } else if (st->user_forced_mode == OPUS_AUTO) 1470 { 1471 #ifdef FUZZING 1472 (void)stereo_width; 1473 (void)mode_thresholds; 1474 /* Random mode switching */ 1475 if ((rand()&0xF)==0) 1476 { 1477 if ((rand()&0x1)==0) 1478 st->mode = MODE_CELT_ONLY; 1479 else 1480 st->mode = MODE_SILK_ONLY; 1481 } else { 1482 if (st->prev_mode==MODE_CELT_ONLY) 1483 st->mode = MODE_CELT_ONLY; 1484 else 1485 st->mode = MODE_SILK_ONLY; 1486 } 1487 #else 1488 opus_int32 mode_voice, mode_music; 1489 opus_int32 threshold; 1490 1491 /* Interpolate based on stereo width */ 1492 mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0]) 1493 + MULT16_32_Q15(stereo_width,mode_thresholds[1][0])); 1494 mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1]) 1495 + MULT16_32_Q15(stereo_width,mode_thresholds[1][1])); 1496 /* Interpolate based on speech/music probability */ 1497 threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14); 1498 /* Bias towards SILK for VoIP because of some useful features */ 1499 if (st->application == OPUS_APPLICATION_VOIP) 1500 threshold += 8000; 1501 1502 /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/ 1503 /* Hysteresis */ 1504 if (st->prev_mode == MODE_CELT_ONLY) 1505 threshold -= 4000; 1506 else if (st->prev_mode>0) 1507 threshold += 4000; 1508 1509 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY; 1510 1511 /* When FEC is enabled and there's enough packet loss, use SILK. 1512 Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */ 1513 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25)) 1514 st->mode = MODE_SILK_ONLY; 1515 /* When encoding voice and DTX is enabled but the generalized DTX cannot be used, 1516 use SILK in order to make use of its DTX. */ 1517 if (st->silk_mode.useDTX && voice_est > 100) 1518 st->mode = MODE_SILK_ONLY; 1519 #endif 1520 1521 /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */ 1522 if (max_data_bytes < bitrate_to_bits(frame_rate > 50 ? 9000 : 6000, st->Fs, frame_size)/8) 1523 st->mode = MODE_CELT_ONLY; 1524 } else { 1525 st->mode = st->user_forced_mode; 1526 } 1527 1528 /* Override the chosen mode to make sure we meet the requested frame size */ 1529 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100) 1530 { 1531 celt_assert(st->application != OPUS_APPLICATION_RESTRICTED_SILK); 1532 st->mode = MODE_CELT_ONLY; 1533 } 1534 if (st->lfe && st->application != OPUS_APPLICATION_RESTRICTED_SILK) 1535 st->mode = MODE_CELT_ONLY; 1536 1537 if (st->prev_mode > 0 && 1538 ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) || 1539 (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY))) 1540 { 1541 redundancy = 1; 1542 celt_to_silk = (st->mode != MODE_CELT_ONLY); 1543 if (!celt_to_silk) 1544 { 1545 /* Switch to SILK/hybrid if frame size is 10 ms or more*/ 1546 if (frame_size >= st->Fs/100) 1547 { 1548 st->mode = st->prev_mode; 1549 to_celt = 1; 1550 } else { 1551 redundancy=0; 1552 } 1553 } 1554 } 1555 1556 /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch 1557 * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */ 1558 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0 1559 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY) 1560 { 1561 /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */ 1562 st->silk_mode.toMono = 1; 1563 st->stream_channels = 2; 1564 } else { 1565 st->silk_mode.toMono = 0; 1566 } 1567 1568 /* Update equivalent rate with mode decision. */ 1569 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size, 1570 st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage); 1571 1572 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) 1573 { 1574 silk_EncControlStruct dummy; 1575 silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy); 1576 prefill=1; 1577 } 1578 1579 /* Automatic (rate-dependent) bandwidth selection */ 1580 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch) 1581 { 1582 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds; 1583 opus_int32 bandwidth_thresholds[8]; 1584 int bandwidth = OPUS_BANDWIDTH_FULLBAND; 1585 1586 if (st->channels==2 && st->force_channels!=1) 1587 { 1588 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds; 1589 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds; 1590 } else { 1591 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds; 1592 music_bandwidth_thresholds = mono_music_bandwidth_thresholds; 1593 } 1594 /* Interpolate bandwidth thresholds depending on voice estimation */ 1595 for (i=0;i<8;i++) 1596 { 1597 bandwidth_thresholds[i] = music_bandwidth_thresholds[i] 1598 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14); 1599 } 1600 do { 1601 int threshold, hysteresis; 1602 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)]; 1603 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1]; 1604 if (!st->first) 1605 { 1606 if (st->auto_bandwidth >= bandwidth) 1607 threshold -= hysteresis; 1608 else 1609 threshold += hysteresis; 1610 } 1611 if (equiv_rate >= threshold) 1612 break; 1613 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND); 1614 /* We don't use mediumband anymore, except when explicitly requested or during 1615 mode transitions. */ 1616 if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) 1617 bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1618 st->bandwidth = st->auto_bandwidth = bandwidth; 1619 /* Prevents any transition to SWB/FB until the SILK layer has fully 1620 switched to WB mode and turned the variable LP filter off */ 1621 if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1622 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1623 } 1624 1625 if (st->bandwidth>st->max_bandwidth) 1626 st->bandwidth = st->max_bandwidth; 1627 1628 if (st->user_bandwidth != OPUS_AUTO) 1629 st->bandwidth = st->user_bandwidth; 1630 1631 /* This prevents us from using hybrid at unsafe CBR/max rates */ 1632 if (st->mode != MODE_CELT_ONLY && max_rate < 15000) 1633 { 1634 st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND); 1635 } 1636 1637 /* Prevents Opus from wasting bits on frequencies that are above 1638 the Nyquist rate of the input signal */ 1639 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND) 1640 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1641 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1642 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1643 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND) 1644 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1645 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND) 1646 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1647 #ifndef DISABLE_FLOAT_API 1648 /* Use detected bandwidth to reduce the encoded bandwidth. */ 1649 if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO) 1650 { 1651 int min_detected_bandwidth; 1652 /* Makes bandwidth detection more conservative just in case the detector 1653 gets it wrong when we could have coded a high bandwidth transparently. 1654 When operating in SILK/hybrid mode, we don't go below wideband to avoid 1655 more complicated switches that require redundancy. */ 1656 if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY) 1657 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1658 else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY) 1659 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1660 else if (equiv_rate <= 30000*st->stream_channels) 1661 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1662 else if (equiv_rate <= 44000*st->stream_channels) 1663 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1664 else 1665 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; 1666 1667 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth); 1668 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth); 1669 } 1670 #endif 1671 st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage, 1672 st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate); 1673 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 1674 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth)); 1675 1676 /* CELT mode doesn't support mediumband, use wideband instead */ 1677 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) 1678 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1679 if (st->lfe) 1680 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1681 1682 curr_bandwidth = st->bandwidth; 1683 1684 if (st->application == OPUS_APPLICATION_RESTRICTED_SILK && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1685 st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1686 /* Chooses the appropriate mode for speech 1687 *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */ 1688 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1689 st->mode = MODE_HYBRID; 1690 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) 1691 st->mode = MODE_SILK_ONLY; 1692 1693 /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */ 1694 if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50) 1695 { 1696 int enc_frame_size; 1697 int nb_frames; 1698 VARDECL(unsigned char, tmp_data); 1699 VARDECL(OpusRepacketizer, rp); 1700 int max_header_bytes; 1701 opus_int32 repacketize_len; 1702 opus_int32 max_len_sum; 1703 opus_int32 tot_size=0; 1704 unsigned char *curr_data; 1705 int tmp_len; 1706 int dtx_count = 0; 1707 int bak_to_mono; 1708 1709 if (st->mode == MODE_SILK_ONLY) 1710 { 1711 if (frame_size == 2*st->Fs/25) /* 80 ms -> 2x 40 ms */ 1712 enc_frame_size = st->Fs/25; 1713 else if (frame_size == 3*st->Fs/25) /* 120 ms -> 2x 60 ms */ 1714 enc_frame_size = 3*st->Fs/50; 1715 else /* 100 ms -> 5x 20 ms */ 1716 enc_frame_size = st->Fs/50; 1717 } 1718 else 1719 enc_frame_size = st->Fs/50; 1720 1721 nb_frames = frame_size/enc_frame_size; 1722 1723 #ifndef DISABLE_FLOAT_API 1724 if (analysis_read_pos_bak!= -1) 1725 { 1726 /* Reset analysis position to the beginning of the first frame so we 1727 can use it one frame at a time. */ 1728 st->analysis.read_pos = analysis_read_pos_bak; 1729 st->analysis.read_subframe = analysis_read_subframe_bak; 1730 } 1731 #endif 1732 1733 /* Worst cases: 1734 * 2 frames: Code 2 with different compressed sizes 1735 * >2 frames: Code 3 VBR */ 1736 max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2); 1737 #ifdef ENABLE_QEXT 1738 /* Cover the use of the separators that are the only thing that can get us over 1739 once we consider that we need to subtract the extension overhead in each 1740 of the individual frames. Also consider that a separator can get our padding 1741 from 254 to 255, which costs an extra length byte (at most once). */ 1742 if (st->enable_qext) max_header_bytes += (nb_frames-1) + 1; 1743 #endif 1744 1745 if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX) 1746 repacketize_len = out_data_bytes; 1747 else { 1748 celt_assert(cbr_bytes>=0); 1749 repacketize_len = IMIN(cbr_bytes, out_data_bytes); 1750 } 1751 max_len_sum = nb_frames + repacketize_len - max_header_bytes; 1752 1753 ALLOC(tmp_data, max_len_sum, unsigned char); 1754 curr_data = tmp_data; 1755 ALLOC(rp, 1, OpusRepacketizer); 1756 opus_repacketizer_init(rp); 1757 1758 1759 bak_to_mono = st->silk_mode.toMono; 1760 if (bak_to_mono) 1761 st->force_channels = 1; 1762 else 1763 st->prev_channels = st->stream_channels; 1764 1765 for (i=0;i<nb_frames;i++) 1766 { 1767 int first_frame; 1768 int frame_to_celt; 1769 int frame_redundancy; 1770 opus_int32 curr_max; 1771 /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh, 1772 that allows for DRED not to be in the first frame. */ 1773 first_frame = (i == 0) || (i == dtx_count); 1774 st->silk_mode.toMono = 0; 1775 st->nonfinal_frame = i<(nb_frames-1); 1776 1777 /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */ 1778 frame_to_celt = to_celt && i==nb_frames-1; 1779 frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0)); 1780 1781 curr_max = IMIN(bitrate_to_bits(st->bitrate_bps, st->Fs, enc_frame_size)/8, max_len_sum/nb_frames); 1782 #ifdef ENABLE_DRED 1783 curr_max = IMIN(curr_max, (max_len_sum-bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8)/nb_frames); 1784 if (first_frame) curr_max += bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8; 1785 #endif 1786 #ifdef ENABLE_QEXT 1787 /* Leave room for signaling the extension size once we repacketize. */ 1788 if (st->enable_qext) 1789 curr_max -= curr_max/254; 1790 #endif 1791 curr_max = IMIN(max_len_sum-tot_size, curr_max); 1792 #ifndef DISABLE_FLOAT_API 1793 if (analysis_read_pos_bak != -1) { 1794 /* Get analysis for current frame. */ 1795 tonality_get_info(&st->analysis, &analysis_info, enc_frame_size); 1796 } 1797 #endif 1798 is_silence = is_digital_silence(pcm+i*(st->channels*enc_frame_size), enc_frame_size, st->channels, lsb_depth); 1799 1800 tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame, 1801 #ifdef ENABLE_DRED 1802 dred_bitrate_bps, 1803 #endif 1804 #ifndef DISABLE_FLOAT_API 1805 &analysis_info, 1806 #endif 1807 is_silence, frame_redundancy, celt_to_silk, prefill, 1808 equiv_rate, frame_to_celt 1809 ); 1810 if (tmp_len<0) 1811 { 1812 RESTORE_STACK; 1813 return OPUS_INTERNAL_ERROR; 1814 } else if (tmp_len==1) { 1815 dtx_count++; 1816 } 1817 ret = opus_repacketizer_cat(rp, curr_data, tmp_len); 1818 1819 if (ret<0) 1820 { 1821 RESTORE_STACK; 1822 return OPUS_INTERNAL_ERROR; 1823 } 1824 tot_size += tmp_len; 1825 curr_data += tmp_len; 1826 } 1827 ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL, 0); 1828 if (ret<0) 1829 { 1830 ret = OPUS_INTERNAL_ERROR; 1831 } 1832 st->silk_mode.toMono = bak_to_mono; 1833 RESTORE_STACK; 1834 return ret; 1835 } else { 1836 ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1, 1837 #ifdef ENABLE_DRED 1838 dred_bitrate_bps, 1839 #endif 1840 #ifndef DISABLE_FLOAT_API 1841 &analysis_info, 1842 #endif 1843 is_silence, redundancy, celt_to_silk, prefill, 1844 equiv_rate, to_celt 1845 ); 1846 RESTORE_STACK; 1847 return ret; 1848 } 1849 } 1850 1851 static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size, 1852 unsigned char *data, opus_int32 orig_max_data_bytes, 1853 int float_api, int first_frame, 1854 #ifdef ENABLE_DRED 1855 opus_int32 dred_bitrate_bps, 1856 #endif 1857 #ifndef DISABLE_FLOAT_API 1858 AnalysisInfo *analysis_info, 1859 #endif 1860 int is_silence, int redundancy, int celt_to_silk, int prefill, 1861 opus_int32 equiv_rate, int to_celt) 1862 { 1863 void *silk_enc=NULL; 1864 CELTEncoder *celt_enc=NULL; 1865 const CELTMode *celt_mode=NULL; 1866 int i; 1867 int ret=0; 1868 int max_data_bytes; 1869 opus_int32 nBytes; 1870 ec_enc enc; 1871 int bits_target; 1872 int start_band = 0; 1873 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */ 1874 int nb_compr_bytes; 1875 opus_uint32 redundant_rng = 0; 1876 int cutoff_Hz; 1877 int hp_freq_smth1; 1878 opus_val16 HB_gain; 1879 int apply_padding; 1880 int frame_rate; 1881 int curr_bandwidth; 1882 int delay_compensation; 1883 int total_buffer; 1884 opus_int activity = VAD_NO_DECISION; 1885 VARDECL(opus_res, pcm_buf); 1886 VARDECL(opus_res, tmp_prefill); 1887 SAVE_STACK; 1888 1889 max_data_bytes = IMIN(orig_max_data_bytes, 1276); 1890 st->rangeFinal = 0; 1891 if (st->application != OPUS_APPLICATION_RESTRICTED_CELT) 1892 silk_enc = (char*)st+st->silk_enc_offset; 1893 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 1894 { 1895 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 1896 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); 1897 } 1898 curr_bandwidth = st->bandwidth; 1899 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_RESTRICTED_CELT || st->application == OPUS_APPLICATION_RESTRICTED_SILK) 1900 delay_compensation = 0; 1901 else 1902 delay_compensation = st->delay_compensation; 1903 total_buffer = delay_compensation; 1904 1905 frame_rate = st->Fs/frame_size; 1906 1907 if (is_silence) 1908 { 1909 activity = !is_silence; 1910 } 1911 #ifndef DISABLE_FLOAT_API 1912 else if (analysis_info->valid) { 1913 activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD; 1914 if (!activity) 1915 { 1916 /* Mark as active if this noise frame is sufficiently loud */ 1917 opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch); 1918 activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy); 1919 } 1920 } 1921 #endif 1922 else if (st->mode == MODE_CELT_ONLY) { 1923 opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch); 1924 /* Boosting peak energy a bit because we didn't just average the active frames. */ 1925 activity = st->peak_signal_energy < (QCONST16(PSEUDO_SNR_THRESHOLD, 0) * (opus_val64)HALF32(noise_energy)); 1926 } 1927 1928 /* For the first frame at a new SILK bandwidth */ 1929 if (st->silk_bw_switch) 1930 { 1931 redundancy = 1; 1932 celt_to_silk = 1; 1933 st->silk_bw_switch = 0; 1934 /* Do a prefill without resetting the sampling rate control. */ 1935 prefill=2; 1936 } 1937 1938 /* If we decided to go with CELT, make sure redundancy is off, no matter what 1939 we decided earlier. */ 1940 if (st->mode == MODE_CELT_ONLY) 1941 redundancy = 0; 1942 1943 if (redundancy) 1944 { 1945 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels); 1946 if (redundancy_bytes == 0) 1947 redundancy = 0; 1948 } 1949 if (st->application == OPUS_APPLICATION_RESTRICTED_SILK) 1950 { 1951 redundancy = 0; 1952 redundancy_bytes = 0; 1953 } 1954 1955 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */ 1956 bits_target = IMIN(8*(max_data_bytes-redundancy_bytes), bitrate_to_bits(st->bitrate_bps, st->Fs, frame_size)) - 8; 1957 1958 data += 1; 1959 1960 ec_enc_init(&enc, data, orig_max_data_bytes-1); 1961 1962 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res); 1963 OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels); 1964 1965 if (st->mode == MODE_CELT_ONLY) 1966 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 1967 else 1968 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15; 1969 1970 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, 1971 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) ); 1972 1973 /* convert from log scale to Hertz */ 1974 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); 1975 1976 if (st->application == OPUS_APPLICATION_VOIP) 1977 { 1978 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch); 1979 1980 #ifdef ENABLE_OSCE_TRAINING_DATA 1981 /* write out high pass filtered clean signal*/ 1982 static FILE *fout =NULL; 1983 if (fout == NULL) 1984 { 1985 fout = fopen("clean_hp.s16", "wb"); 1986 } 1987 1988 { 1989 int idx; 1990 opus_int16 tmp; 1991 for (idx = 0; idx < frame_size; idx++) 1992 { 1993 tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f); 1994 fwrite(&tmp, sizeof(tmp), 1, fout); 1995 } 1996 } 1997 #endif 1998 } else { 1999 #ifdef ENABLE_QEXT 2000 /* FIXME: Avoid glitching when we switch qext on/off dynamically. */ 2001 if (st->enable_qext) OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels); 2002 else 2003 #endif 2004 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); 2005 } 2006 #ifndef FIXED_POINT 2007 if (float_api) 2008 { 2009 opus_val32 sum; 2010 sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch); 2011 /* This should filter out both NaNs and ridiculous signals that could 2012 cause NaNs further down. */ 2013 if (!(sum < 1e9f) || celt_isnan(sum)) 2014 { 2015 OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels); 2016 st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0; 2017 } 2018 } 2019 #else 2020 (void)float_api; 2021 #endif 2022 2023 #ifdef ENABLE_DRED 2024 /* Compute the DRED features. Needs to be before SILK because of DTX. */ 2025 if ( st->dred_duration > 0 && st->dred_encoder.loaded ) { 2026 int frame_size_400Hz; 2027 /* DRED Encoder */ 2028 dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch ); 2029 frame_size_400Hz = frame_size*400/st->Fs; 2030 OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz); 2031 for (i=0;i<frame_size_400Hz;i++) 2032 st->activity_mem[i] = activity; 2033 } else { 2034 st->dred_encoder.latents_buffer_fill = 0; 2035 OPUS_CLEAR(st->activity_mem, 4*DRED_MAX_FRAMES); 2036 } 2037 #endif 2038 2039 /* SILK processing */ 2040 HB_gain = Q15ONE; 2041 if (st->mode != MODE_CELT_ONLY) 2042 { 2043 opus_int32 total_bitRate, celt_rate; 2044 const opus_res *pcm_silk; 2045 2046 /* Distribute bits between SILK and CELT */ 2047 total_bitRate = bits_to_bitrate(bits_target, st->Fs, frame_size); 2048 if( st->mode == MODE_HYBRID ) { 2049 /* Base rate for SILK */ 2050 st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate, 2051 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded, 2052 st->stream_channels); 2053 if (!st->energy_masking) 2054 { 2055 /* Increasingly attenuate high band when it gets allocated fewer bits */ 2056 celt_rate = total_bitRate - st->silk_mode.bitRate; 2057 HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1); 2058 } 2059 } else { 2060 /* SILK gets all bits */ 2061 st->silk_mode.bitRate = total_bitRate; 2062 } 2063 2064 /* Surround masking for SILK */ 2065 if (st->energy_masking && st->use_vbr && !st->lfe) 2066 { 2067 opus_val32 mask_sum=0; 2068 celt_glog masking_depth; 2069 opus_int32 rate_offset; 2070 int c; 2071 int end = 17; 2072 opus_int16 srate = 16000; 2073 if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND) 2074 { 2075 end = 13; 2076 srate = 8000; 2077 } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) 2078 { 2079 end = 15; 2080 srate = 12000; 2081 } 2082 for (c=0;c<st->channels;c++) 2083 { 2084 for(i=0;i<end;i++) 2085 { 2086 celt_glog mask; 2087 mask = MAXG(MING(st->energy_masking[21*c+i], 2088 GCONST(.5f)), -GCONST(2.0f)); 2089 if (mask > 0) 2090 mask = HALF32(mask); 2091 mask_sum += mask; 2092 } 2093 } 2094 /* Conservative rate reduction, we cut the masking in half */ 2095 masking_depth = mask_sum / end*st->channels; 2096 masking_depth += GCONST(.2f); 2097 rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10); 2098 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3); 2099 /* Split the rate change between the SILK and CELT part for hybrid. */ 2100 if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND) 2101 st->silk_mode.bitRate += 3*rate_offset/5; 2102 else 2103 st->silk_mode.bitRate += rate_offset; 2104 } 2105 2106 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; 2107 st->silk_mode.nChannelsAPI = st->channels; 2108 st->silk_mode.nChannelsInternal = st->stream_channels; 2109 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 2110 st->silk_mode.desiredInternalSampleRate = 8000; 2111 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 2112 st->silk_mode.desiredInternalSampleRate = 12000; 2113 } else { 2114 celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND ); 2115 st->silk_mode.desiredInternalSampleRate = 16000; 2116 } 2117 if( st->mode == MODE_HYBRID ) { 2118 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */ 2119 st->silk_mode.minInternalSampleRate = 16000; 2120 } else { 2121 st->silk_mode.minInternalSampleRate = 8000; 2122 } 2123 2124 st->silk_mode.maxInternalSampleRate = 16000; 2125 if (st->mode == MODE_SILK_ONLY) 2126 { 2127 opus_int32 effective_max_rate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size); 2128 if (frame_rate > 50) 2129 effective_max_rate = effective_max_rate*2/3; 2130 if (effective_max_rate < 8000) 2131 { 2132 st->silk_mode.maxInternalSampleRate = 12000; 2133 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate); 2134 } 2135 if (effective_max_rate < 7000) 2136 { 2137 st->silk_mode.maxInternalSampleRate = 8000; 2138 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate); 2139 } 2140 #ifdef ENABLE_QEXT 2141 /* At 96 kHz, we don't have the input resampler to do 8 or 12 kHz. */ 2142 if (st->Fs==96000) st->silk_mode.maxInternalSampleRate = st->silk_mode.desiredInternalSampleRate = 16000; 2143 #endif 2144 } 2145 2146 st->silk_mode.useCBR = !st->use_vbr; 2147 2148 /* Call SILK encoder for the low band */ 2149 2150 /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */ 2151 st->silk_mode.maxBits = (max_data_bytes-1)*8; 2152 if (redundancy && redundancy_bytes >= 2) 2153 { 2154 /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */ 2155 st->silk_mode.maxBits -= redundancy_bytes*8 + 1; 2156 if (st->mode == MODE_HYBRID) 2157 st->silk_mode.maxBits -= 20; 2158 } 2159 if (st->silk_mode.useCBR) 2160 { 2161 /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to 2162 save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still 2163 produce a constant bitrate without wasting bits. */ 2164 #ifdef ENABLE_DRED 2165 if (st->mode == MODE_HYBRID || dred_bitrate_bps > 0) 2166 #else 2167 if (st->mode == MODE_HYBRID) 2168 #endif 2169 { 2170 /* Allow SILK to steal up to 25% of the remaining bits */ 2171 opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs); 2172 st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4); 2173 st->silk_mode.useCBR = 0; 2174 } 2175 } else { 2176 /* Constrained VBR. */ 2177 if (st->mode == MODE_HYBRID) 2178 { 2179 /* Compute SILK bitrate corresponding to the max total bits available */ 2180 opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size, 2181 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded, 2182 st->stream_channels); 2183 st->silk_mode.maxBits = bitrate_to_bits(maxBitRate, st->Fs, frame_size); 2184 } 2185 } 2186 2187 if (prefill && st->application != OPUS_APPLICATION_RESTRICTED_SILK) 2188 { 2189 opus_int32 zero=0; 2190 int prefill_offset; 2191 /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode 2192 a discontinuity. The exact location is what we need to avoid leaving any "gap" 2193 in the audio when mixing with the redundant CELT frame. Here we can afford to 2194 overwrite st->delay_buffer because the only thing that uses it before it gets 2195 rewritten is tmp_prefill[] and even then only the part after the ramp really 2196 gets used (rather than sent to the encoder and discarded) */ 2197 prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400); 2198 gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset, 2199 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs); 2200 OPUS_CLEAR(st->delay_buffer, prefill_offset); 2201 pcm_silk = st->delay_buffer; 2202 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity ); 2203 /* Prevent a second switch in the real encode call. */ 2204 st->silk_mode.opusCanSwitch = 0; 2205 } 2206 2207 pcm_silk = pcm_buf+total_buffer*st->channels; 2208 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity ); 2209 if( ret ) { 2210 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/ 2211 /* Handle error */ 2212 RESTORE_STACK; 2213 return OPUS_INTERNAL_ERROR; 2214 } 2215 2216 /* Extract SILK internal bandwidth for signaling in first byte */ 2217 if( st->mode == MODE_SILK_ONLY ) { 2218 if( st->silk_mode.internalSampleRate == 8000 ) { 2219 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 2220 } else if( st->silk_mode.internalSampleRate == 12000 ) { 2221 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 2222 } else if( st->silk_mode.internalSampleRate == 16000 ) { 2223 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 2224 } 2225 } else { 2226 celt_assert( st->silk_mode.internalSampleRate == 16000 ); 2227 } 2228 2229 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame; 2230 2231 if (activity == VAD_NO_DECISION) { 2232 activity = (st->silk_mode.signalType != TYPE_NO_VOICE_ACTIVITY); 2233 #ifdef ENABLE_DRED 2234 for (i=0;i<frame_size*400/st->Fs;i++) 2235 st->activity_mem[i] = activity; 2236 #endif 2237 } 2238 if (nBytes==0) 2239 { 2240 st->rangeFinal = 0; 2241 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); 2242 RESTORE_STACK; 2243 return 1; 2244 } 2245 2246 /* FIXME: How do we allocate the redundancy for CBR? */ 2247 if (st->silk_mode.opusCanSwitch) 2248 { 2249 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 2250 { 2251 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels); 2252 redundancy = (redundancy_bytes != 0); 2253 } 2254 celt_to_silk = 0; 2255 st->silk_bw_switch = 1; 2256 } 2257 } 2258 2259 /* CELT processing */ 2260 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 2261 { 2262 int endband=21; 2263 2264 switch(curr_bandwidth) 2265 { 2266 case OPUS_BANDWIDTH_NARROWBAND: 2267 endband = 13; 2268 break; 2269 case OPUS_BANDWIDTH_MEDIUMBAND: 2270 case OPUS_BANDWIDTH_WIDEBAND: 2271 endband = 17; 2272 break; 2273 case OPUS_BANDWIDTH_SUPERWIDEBAND: 2274 endband = 19; 2275 break; 2276 case OPUS_BANDWIDTH_FULLBAND: 2277 endband = 21; 2278 break; 2279 } 2280 celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband)); 2281 celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels)); 2282 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); 2283 } 2284 if (st->mode != MODE_SILK_ONLY) 2285 { 2286 opus_val32 celt_pred=2; 2287 /* We may still decide to disable prediction later */ 2288 if (st->silk_mode.reducedDependency) 2289 celt_pred = 0; 2290 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred)); 2291 } 2292 2293 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res); 2294 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0 2295 && st->application != OPUS_APPLICATION_RESTRICTED_CELT) 2296 { 2297 OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400); 2298 } 2299 2300 if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0) 2301 { 2302 OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer)); 2303 OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)], 2304 &pcm_buf[0], 2305 (frame_size+total_buffer)*st->channels); 2306 } else { 2307 OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels); 2308 } 2309 /* gain_fade() and stereo_fade() need to be after the buffer copying 2310 because we don't want any of this to affect the SILK part */ 2311 if( ( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) && celt_mode != NULL ) { 2312 gain_fade(pcm_buf, pcm_buf, 2313 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs); 2314 } 2315 st->prev_HB_gain = HB_gain; 2316 if (st->mode != MODE_HYBRID || st->stream_channels==1) 2317 { 2318 if (equiv_rate > 32000) 2319 st->silk_mode.stereoWidth_Q14 = 16384; 2320 else if (equiv_rate < 16000) 2321 st->silk_mode.stereoWidth_Q14 = 0; 2322 else 2323 st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000); 2324 } 2325 if( !st->energy_masking && st->channels == 2 ) { 2326 /* Apply stereo width reduction (at low bitrates) */ 2327 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) { 2328 opus_val16 g1, g2; 2329 g1 = st->hybrid_stereo_width_Q14; 2330 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14); 2331 #ifdef FIXED_POINT 2332 g1 = g1==16384 ? Q15ONE : SHL16(g1,1); 2333 g2 = g2==16384 ? Q15ONE : SHL16(g2,1); 2334 #else 2335 g1 *= (1.f/16384); 2336 g2 *= (1.f/16384); 2337 #endif 2338 if ( celt_mode != NULL ) 2339 { 2340 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, 2341 frame_size, st->channels, celt_mode->window, st->Fs); 2342 } 2343 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14; 2344 } 2345 } 2346 2347 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1)) 2348 { 2349 /* For SILK mode, the redundancy is inferred from the length */ 2350 if (st->mode == MODE_HYBRID) 2351 ec_enc_bit_logp(&enc, redundancy, 12); 2352 if (redundancy) 2353 { 2354 int max_redundancy; 2355 ec_enc_bit_logp(&enc, celt_to_silk, 1); 2356 if (st->mode == MODE_HYBRID) 2357 { 2358 /* Reserve the 8 bits needed for the redundancy length, 2359 and at least a few bits for CELT if possible */ 2360 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3); 2361 } 2362 else 2363 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3); 2364 /* Target the same bit-rate for redundancy as for the rest, 2365 up to a max of 257 bytes */ 2366 redundancy_bytes = IMIN(max_redundancy, redundancy_bytes); 2367 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); 2368 if (st->mode == MODE_HYBRID) 2369 ec_enc_uint(&enc, redundancy_bytes-2, 256); 2370 } 2371 } else { 2372 redundancy = 0; 2373 } 2374 2375 if (!redundancy) 2376 { 2377 st->silk_bw_switch = 0; 2378 redundancy_bytes = 0; 2379 } 2380 if (st->mode != MODE_CELT_ONLY)start_band=17; 2381 2382 if (st->mode == MODE_SILK_ONLY) 2383 { 2384 ret = (ec_tell(&enc)+7)>>3; 2385 ec_enc_done(&enc); 2386 nb_compr_bytes = ret; 2387 } else { 2388 nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes; 2389 #ifdef ENABLE_QEXT 2390 if (st->mode == MODE_CELT_ONLY && st->enable_qext) { 2391 celt_assert(redundancy_bytes==0); 2392 nb_compr_bytes = orig_max_data_bytes-1; 2393 } 2394 #endif 2395 #ifdef ENABLE_DRED 2396 if (st->dred_duration > 0) 2397 { 2398 int max_celt_bytes; 2399 opus_int32 dred_bytes = bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8; 2400 /* Allow CELT to steal up to 25% of the remaining bits. */ 2401 max_celt_bytes = nb_compr_bytes - dred_bytes*3/4; 2402 /* But try to give CELT at least 5 bytes to prevent a mismatch with 2403 the redundancy signaling. */ 2404 max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes); 2405 /* Subject to the original max. */ 2406 nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes); 2407 } 2408 #endif 2409 ec_enc_shrink(&enc, nb_compr_bytes); 2410 } 2411 2412 #ifndef DISABLE_FLOAT_API 2413 if (redundancy || st->mode != MODE_SILK_ONLY) 2414 celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info)); 2415 #endif 2416 if (st->mode == MODE_HYBRID) { 2417 SILKInfo info; 2418 info.signalType = st->silk_mode.signalType; 2419 info.offset = st->silk_mode.offset; 2420 celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info)); 2421 } 2422 2423 /* 5 ms redundant frame for CELT->SILK */ 2424 if (redundancy && celt_to_silk) 2425 { 2426 int err; 2427 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); 2428 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); 2429 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); 2430 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL); 2431 if (err < 0) 2432 { 2433 RESTORE_STACK; 2434 return OPUS_INTERNAL_ERROR; 2435 } 2436 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); 2437 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 2438 } 2439 2440 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 2441 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band)); 2442 2443 data[-1] = 0; 2444 if (st->mode != MODE_SILK_ONLY) 2445 { 2446 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr)); 2447 if (st->mode == MODE_HYBRID) 2448 { 2449 if( st->use_vbr ) { 2450 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate)); 2451 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0)); 2452 } 2453 } else { 2454 if (st->use_vbr) 2455 { 2456 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); 2457 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint)); 2458 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps)); 2459 } 2460 } 2461 #ifdef ENABLE_DRED 2462 /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */ 2463 if (!st->use_vbr && st->dred_duration > 0) 2464 { 2465 opus_int32 celt_bitrate = st->bitrate_bps; 2466 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); 2467 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0)); 2468 if (st->mode == MODE_HYBRID) { 2469 celt_bitrate -= st->silk_mode.bitRate; 2470 } 2471 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate)); 2472 } 2473 #endif 2474 if (st->mode != st->prev_mode && st->prev_mode > 0 && st->application != OPUS_APPLICATION_RESTRICTED_CELT) 2475 { 2476 unsigned char dummy[2]; 2477 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 2478 2479 /* Prefilling */ 2480 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL); 2481 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); 2482 } 2483 /* If false, we already busted the budget and we'll end up with a "PLC frame" */ 2484 if (ec_tell(&enc) <= 8*nb_compr_bytes) 2485 { 2486 #ifdef ENABLE_QEXT 2487 if (st->mode == MODE_CELT_ONLY) celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(st->enable_qext)); 2488 #endif 2489 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc); 2490 #ifdef ENABLE_QEXT 2491 celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(0)); 2492 #endif 2493 if (ret < 0) 2494 { 2495 RESTORE_STACK; 2496 return OPUS_INTERNAL_ERROR; 2497 } 2498 /* Put CELT->SILK redundancy data in the right place. */ 2499 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && nb_compr_bytes != ret) 2500 { 2501 OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes); 2502 nb_compr_bytes = ret+redundancy_bytes; 2503 } 2504 } 2505 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&st->rangeFinal)); 2506 } else { 2507 st->rangeFinal = enc.rng; 2508 } 2509 2510 /* 5 ms redundant frame for SILK->CELT */ 2511 if (redundancy && !celt_to_silk) 2512 { 2513 int err; 2514 unsigned char dummy[2]; 2515 int N2, N4; 2516 N2 = st->Fs/200; 2517 N4 = st->Fs/400; 2518 2519 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 2520 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); 2521 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); 2522 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); 2523 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); 2524 2525 if (st->mode == MODE_HYBRID) 2526 { 2527 /* Shrink packet to what the encoder actually used. */ 2528 nb_compr_bytes = ret; 2529 ec_enc_shrink(&enc, nb_compr_bytes); 2530 } 2531 /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */ 2532 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL); 2533 2534 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL); 2535 if (err < 0) 2536 { 2537 RESTORE_STACK; 2538 return OPUS_INTERNAL_ERROR; 2539 } 2540 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); 2541 } 2542 2543 2544 2545 /* Signalling the mode in the first byte */ 2546 data--; 2547 data[0] |= gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); 2548 2549 st->rangeFinal ^= redundant_rng; 2550 2551 if (to_celt) 2552 st->prev_mode = MODE_CELT_ONLY; 2553 else 2554 st->prev_mode = st->mode; 2555 st->prev_channels = st->stream_channels; 2556 st->prev_framesize = frame_size; 2557 2558 st->first = 0; 2559 2560 /* DTX decision */ 2561 if (st->use_dtx && !st->silk_mode.useDTX) 2562 { 2563 if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs)) 2564 { 2565 st->rangeFinal = 0; 2566 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); 2567 RESTORE_STACK; 2568 return 1; 2569 } 2570 } else { 2571 st->nb_no_activity_ms_Q1 = 0; 2572 } 2573 2574 /* In the unlikely case that the SILK encoder busted its target, tell 2575 the decoder to call the PLC */ 2576 if (ec_tell(&enc) > (max_data_bytes-1)*8) 2577 { 2578 if (max_data_bytes < 2) 2579 { 2580 RESTORE_STACK; 2581 return OPUS_BUFFER_TOO_SMALL; 2582 } 2583 data[1] = 0; 2584 ret = 1; 2585 st->rangeFinal = 0; 2586 } else if (st->mode==MODE_SILK_ONLY&&!redundancy) 2587 { 2588 /*When in LPC only mode it's perfectly 2589 reasonable to strip off trailing zero bytes as 2590 the required range decoder behavior is to 2591 fill these in. This can't be done when the MDCT 2592 modes are used because the decoder needs to know 2593 the actual length for allocation purposes.*/ 2594 while(ret>2&&data[ret]==0)ret--; 2595 } 2596 /* Count ToC and redundancy */ 2597 ret += 1+redundancy_bytes; 2598 apply_padding = !st->use_vbr; 2599 #ifdef ENABLE_DRED 2600 if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) { 2601 opus_extension_data extension; 2602 unsigned char buf[DRED_MAX_DATA_SIZE]; 2603 int dred_chunks; 2604 int dred_bytes_left; 2605 dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2); 2606 if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks); 2607 /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */ 2608 dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-3); 2609 /* Account for the extra bytes required to signal large padding length. */ 2610 dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255; 2611 /* Check whether we actually have something to encode. */ 2612 if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) { 2613 int dred_bytes; 2614 #ifdef DRED_EXPERIMENTAL_VERSION 2615 /* Add temporary extension type and version. 2616 These bytes will be removed once extension is finalized. */ 2617 buf[0] = 'D'; 2618 buf[1] = DRED_EXPERIMENTAL_VERSION; 2619 #endif 2620 dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES, 2621 st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch); 2622 if (dred_bytes > 0) { 2623 dred_bytes += DRED_EXPERIMENTAL_BYTES; 2624 celt_assert(dred_bytes <= dred_bytes_left); 2625 extension.id = DRED_EXTENSION_ID; 2626 extension.frame = 0; 2627 extension.data = buf; 2628 extension.len = dred_bytes; 2629 ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1); 2630 if (ret < 0) 2631 { 2632 RESTORE_STACK; 2633 return OPUS_INTERNAL_ERROR; 2634 } 2635 apply_padding = 0; 2636 } 2637 } 2638 } 2639 #else 2640 (void)first_frame; /* Avoids a warning about first_frame being unused. */ 2641 #endif 2642 if (apply_padding) 2643 { 2644 if (opus_packet_pad(data, ret, orig_max_data_bytes) != OPUS_OK) 2645 { 2646 RESTORE_STACK; 2647 return OPUS_INTERNAL_ERROR; 2648 } 2649 ret = orig_max_data_bytes; 2650 } 2651 RESTORE_STACK; 2652 return ret; 2653 } 2654 2655 2656 2657 #if defined(FIXED_POINT) && !defined(ENABLE_RES24) 2658 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, 2659 unsigned char *data, opus_int32 max_data_bytes) 2660 { 2661 int frame_size; 2662 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); 2663 return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, 16, 2664 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0); 2665 } 2666 #else 2667 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, 2668 unsigned char *data, opus_int32 max_data_bytes) 2669 { 2670 int i, ret; 2671 int frame_size; 2672 VARDECL(opus_res, in); 2673 ALLOC_STACK; 2674 2675 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); 2676 if (frame_size <= 0) 2677 { 2678 RESTORE_STACK; 2679 return OPUS_BAD_ARG; 2680 } 2681 ALLOC(in, frame_size*st->channels, opus_res); 2682 2683 for (i=0;i<frame_size*st->channels;i++) 2684 in[i] = INT16TORES(pcm[i]); 2685 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, 2686 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 1); 2687 RESTORE_STACK; 2688 return ret; 2689 } 2690 #endif 2691 2692 #if defined(FIXED_POINT) && defined(ENABLE_RES24) 2693 opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size, 2694 unsigned char *data, opus_int32 max_data_bytes) 2695 { 2696 int frame_size; 2697 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); 2698 return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH, 2699 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 0); 2700 } 2701 #else 2702 opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size, 2703 unsigned char *data, opus_int32 max_data_bytes) 2704 { 2705 int i, ret; 2706 int frame_size; 2707 VARDECL(opus_res, in); 2708 ALLOC_STACK; 2709 2710 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); 2711 if (frame_size <= 0) 2712 { 2713 RESTORE_STACK; 2714 return OPUS_BAD_ARG; 2715 } 2716 ALLOC(in, frame_size*st->channels, opus_res); 2717 2718 for (i=0;i<frame_size*st->channels;i++) 2719 in[i] = INT24TORES(pcm[i]); 2720 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH, 2721 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 1); 2722 RESTORE_STACK; 2723 return ret; 2724 } 2725 #endif 2726 2727 2728 #ifndef DISABLE_FLOAT_API 2729 2730 # if !defined(FIXED_POINT) 2731 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, 2732 unsigned char *data, opus_int32 out_data_bytes) 2733 { 2734 int frame_size; 2735 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); 2736 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, MAX_ENCODING_DEPTH, 2737 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1); 2738 } 2739 # else 2740 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, 2741 unsigned char *data, opus_int32 max_data_bytes) 2742 { 2743 int i, ret; 2744 int frame_size; 2745 VARDECL(opus_res, in); 2746 ALLOC_STACK; 2747 2748 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); 2749 if (frame_size <= 0) 2750 { 2751 RESTORE_STACK; 2752 return OPUS_BAD_ARG; 2753 } 2754 ALLOC(in, frame_size*st->channels, opus_res); 2755 2756 for (i=0;i<frame_size*st->channels;i++) 2757 in[i] = FLOAT2RES(pcm[i]); 2758 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH, 2759 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1); 2760 RESTORE_STACK; 2761 return ret; 2762 } 2763 # endif 2764 2765 #endif 2766 2767 2768 int opus_encoder_ctl(OpusEncoder *st, int request, ...) 2769 { 2770 int ret; 2771 CELTEncoder *celt_enc=NULL; 2772 va_list ap; 2773 2774 ret = OPUS_OK; 2775 va_start(ap, request); 2776 2777 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 2778 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 2779 2780 switch (request) 2781 { 2782 case OPUS_SET_APPLICATION_REQUEST: 2783 { 2784 opus_int32 value = va_arg(ap, opus_int32); 2785 if (st->application == OPUS_APPLICATION_RESTRICTED_SILK || st->application == OPUS_APPLICATION_RESTRICTED_CELT) 2786 { 2787 ret = OPUS_BAD_ARG; 2788 break; 2789 } 2790 if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO 2791 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY) 2792 || (!st->first && st->application != value)) 2793 { 2794 ret = OPUS_BAD_ARG; 2795 break; 2796 } 2797 st->application = value; 2798 #ifndef DISABLE_FLOAT_API 2799 st->analysis.application = value; 2800 #endif 2801 } 2802 break; 2803 case OPUS_GET_APPLICATION_REQUEST: 2804 { 2805 opus_int32 *value = va_arg(ap, opus_int32*); 2806 if (!value) 2807 { 2808 goto bad_arg; 2809 } 2810 *value = st->application; 2811 } 2812 break; 2813 case OPUS_SET_BITRATE_REQUEST: 2814 { 2815 opus_int32 value = va_arg(ap, opus_int32); 2816 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) 2817 { 2818 if (value <= 0) 2819 goto bad_arg; 2820 else if (value <= 500) 2821 value = 500; 2822 else if (value > (opus_int32)750000*st->channels) 2823 value = (opus_int32)750000*st->channels; 2824 } 2825 st->user_bitrate_bps = value; 2826 } 2827 break; 2828 case OPUS_GET_BITRATE_REQUEST: 2829 { 2830 opus_int32 *value = va_arg(ap, opus_int32*); 2831 if (!value) 2832 { 2833 goto bad_arg; 2834 } 2835 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276); 2836 } 2837 break; 2838 case OPUS_SET_FORCE_CHANNELS_REQUEST: 2839 { 2840 opus_int32 value = va_arg(ap, opus_int32); 2841 if((value<1 || value>st->channels) && value != OPUS_AUTO) 2842 { 2843 goto bad_arg; 2844 } 2845 st->force_channels = value; 2846 } 2847 break; 2848 case OPUS_GET_FORCE_CHANNELS_REQUEST: 2849 { 2850 opus_int32 *value = va_arg(ap, opus_int32*); 2851 if (!value) 2852 { 2853 goto bad_arg; 2854 } 2855 *value = st->force_channels; 2856 } 2857 break; 2858 case OPUS_SET_MAX_BANDWIDTH_REQUEST: 2859 { 2860 opus_int32 value = va_arg(ap, opus_int32); 2861 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) 2862 { 2863 goto bad_arg; 2864 } 2865 st->max_bandwidth = value; 2866 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 2867 st->silk_mode.maxInternalSampleRate = 8000; 2868 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 2869 st->silk_mode.maxInternalSampleRate = 12000; 2870 } else { 2871 st->silk_mode.maxInternalSampleRate = 16000; 2872 } 2873 } 2874 break; 2875 case OPUS_GET_MAX_BANDWIDTH_REQUEST: 2876 { 2877 opus_int32 *value = va_arg(ap, opus_int32*); 2878 if (!value) 2879 { 2880 goto bad_arg; 2881 } 2882 *value = st->max_bandwidth; 2883 } 2884 break; 2885 case OPUS_SET_BANDWIDTH_REQUEST: 2886 { 2887 opus_int32 value = va_arg(ap, opus_int32); 2888 if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO) 2889 { 2890 goto bad_arg; 2891 } 2892 st->user_bandwidth = value; 2893 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 2894 st->silk_mode.maxInternalSampleRate = 8000; 2895 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 2896 st->silk_mode.maxInternalSampleRate = 12000; 2897 } else { 2898 st->silk_mode.maxInternalSampleRate = 16000; 2899 } 2900 } 2901 break; 2902 case OPUS_GET_BANDWIDTH_REQUEST: 2903 { 2904 opus_int32 *value = va_arg(ap, opus_int32*); 2905 if (!value) 2906 { 2907 goto bad_arg; 2908 } 2909 *value = st->bandwidth; 2910 } 2911 break; 2912 case OPUS_SET_DTX_REQUEST: 2913 { 2914 opus_int32 value = va_arg(ap, opus_int32); 2915 if(value<0 || value>1) 2916 { 2917 goto bad_arg; 2918 } 2919 st->use_dtx = value; 2920 } 2921 break; 2922 case OPUS_GET_DTX_REQUEST: 2923 { 2924 opus_int32 *value = va_arg(ap, opus_int32*); 2925 if (!value) 2926 { 2927 goto bad_arg; 2928 } 2929 *value = st->use_dtx; 2930 } 2931 break; 2932 case OPUS_SET_COMPLEXITY_REQUEST: 2933 { 2934 opus_int32 value = va_arg(ap, opus_int32); 2935 if(value<0 || value>10) 2936 { 2937 goto bad_arg; 2938 } 2939 st->silk_mode.complexity = value; 2940 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 2941 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value)); 2942 } 2943 break; 2944 case OPUS_GET_COMPLEXITY_REQUEST: 2945 { 2946 opus_int32 *value = va_arg(ap, opus_int32*); 2947 if (!value) 2948 { 2949 goto bad_arg; 2950 } 2951 *value = st->silk_mode.complexity; 2952 } 2953 break; 2954 case OPUS_SET_INBAND_FEC_REQUEST: 2955 { 2956 opus_int32 value = va_arg(ap, opus_int32); 2957 if(value<0 || value>2) 2958 { 2959 goto bad_arg; 2960 } 2961 st->fec_config = value; 2962 st->silk_mode.useInBandFEC = (value != 0); 2963 } 2964 break; 2965 case OPUS_GET_INBAND_FEC_REQUEST: 2966 { 2967 opus_int32 *value = va_arg(ap, opus_int32*); 2968 if (!value) 2969 { 2970 goto bad_arg; 2971 } 2972 *value = st->fec_config; 2973 } 2974 break; 2975 case OPUS_SET_PACKET_LOSS_PERC_REQUEST: 2976 { 2977 opus_int32 value = va_arg(ap, opus_int32); 2978 if (value < 0 || value > 100) 2979 { 2980 goto bad_arg; 2981 } 2982 st->silk_mode.packetLossPercentage = value; 2983 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 2984 celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value)); 2985 } 2986 break; 2987 case OPUS_GET_PACKET_LOSS_PERC_REQUEST: 2988 { 2989 opus_int32 *value = va_arg(ap, opus_int32*); 2990 if (!value) 2991 { 2992 goto bad_arg; 2993 } 2994 *value = st->silk_mode.packetLossPercentage; 2995 } 2996 break; 2997 case OPUS_SET_VBR_REQUEST: 2998 { 2999 opus_int32 value = va_arg(ap, opus_int32); 3000 if(value<0 || value>1) 3001 { 3002 goto bad_arg; 3003 } 3004 st->use_vbr = value; 3005 st->silk_mode.useCBR = 1-value; 3006 } 3007 break; 3008 case OPUS_GET_VBR_REQUEST: 3009 { 3010 opus_int32 *value = va_arg(ap, opus_int32*); 3011 if (!value) 3012 { 3013 goto bad_arg; 3014 } 3015 *value = st->use_vbr; 3016 } 3017 break; 3018 case OPUS_SET_VOICE_RATIO_REQUEST: 3019 { 3020 opus_int32 value = va_arg(ap, opus_int32); 3021 if (value<-1 || value>100) 3022 { 3023 goto bad_arg; 3024 } 3025 st->voice_ratio = value; 3026 } 3027 break; 3028 case OPUS_GET_VOICE_RATIO_REQUEST: 3029 { 3030 opus_int32 *value = va_arg(ap, opus_int32*); 3031 if (!value) 3032 { 3033 goto bad_arg; 3034 } 3035 *value = st->voice_ratio; 3036 } 3037 break; 3038 case OPUS_SET_VBR_CONSTRAINT_REQUEST: 3039 { 3040 opus_int32 value = va_arg(ap, opus_int32); 3041 if(value<0 || value>1) 3042 { 3043 goto bad_arg; 3044 } 3045 st->vbr_constraint = value; 3046 } 3047 break; 3048 case OPUS_GET_VBR_CONSTRAINT_REQUEST: 3049 { 3050 opus_int32 *value = va_arg(ap, opus_int32*); 3051 if (!value) 3052 { 3053 goto bad_arg; 3054 } 3055 *value = st->vbr_constraint; 3056 } 3057 break; 3058 case OPUS_SET_SIGNAL_REQUEST: 3059 { 3060 opus_int32 value = va_arg(ap, opus_int32); 3061 if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC) 3062 { 3063 goto bad_arg; 3064 } 3065 st->signal_type = value; 3066 } 3067 break; 3068 case OPUS_GET_SIGNAL_REQUEST: 3069 { 3070 opus_int32 *value = va_arg(ap, opus_int32*); 3071 if (!value) 3072 { 3073 goto bad_arg; 3074 } 3075 *value = st->signal_type; 3076 } 3077 break; 3078 case OPUS_GET_LOOKAHEAD_REQUEST: 3079 { 3080 opus_int32 *value = va_arg(ap, opus_int32*); 3081 if (!value) 3082 { 3083 goto bad_arg; 3084 } 3085 *value = st->Fs/400; 3086 if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY && st->application != OPUS_APPLICATION_RESTRICTED_CELT) 3087 *value += st->delay_compensation; 3088 } 3089 break; 3090 case OPUS_GET_SAMPLE_RATE_REQUEST: 3091 { 3092 opus_int32 *value = va_arg(ap, opus_int32*); 3093 if (!value) 3094 { 3095 goto bad_arg; 3096 } 3097 *value = st->Fs; 3098 } 3099 break; 3100 case OPUS_GET_FINAL_RANGE_REQUEST: 3101 { 3102 opus_uint32 *value = va_arg(ap, opus_uint32*); 3103 if (!value) 3104 { 3105 goto bad_arg; 3106 } 3107 *value = st->rangeFinal; 3108 } 3109 break; 3110 case OPUS_SET_LSB_DEPTH_REQUEST: 3111 { 3112 opus_int32 value = va_arg(ap, opus_int32); 3113 if (value<8 || value>24) 3114 { 3115 goto bad_arg; 3116 } 3117 st->lsb_depth=value; 3118 } 3119 break; 3120 case OPUS_GET_LSB_DEPTH_REQUEST: 3121 { 3122 opus_int32 *value = va_arg(ap, opus_int32*); 3123 if (!value) 3124 { 3125 goto bad_arg; 3126 } 3127 *value = st->lsb_depth; 3128 } 3129 break; 3130 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: 3131 { 3132 opus_int32 value = va_arg(ap, opus_int32); 3133 if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS && 3134 value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS && 3135 value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS && 3136 value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_80_MS && 3137 value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS) 3138 { 3139 goto bad_arg; 3140 } 3141 st->variable_duration = value; 3142 } 3143 break; 3144 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: 3145 { 3146 opus_int32 *value = va_arg(ap, opus_int32*); 3147 if (!value) 3148 { 3149 goto bad_arg; 3150 } 3151 *value = st->variable_duration; 3152 } 3153 break; 3154 case OPUS_SET_PREDICTION_DISABLED_REQUEST: 3155 { 3156 opus_int32 value = va_arg(ap, opus_int32); 3157 if (value > 1 || value < 0) 3158 goto bad_arg; 3159 st->silk_mode.reducedDependency = value; 3160 } 3161 break; 3162 case OPUS_GET_PREDICTION_DISABLED_REQUEST: 3163 { 3164 opus_int32 *value = va_arg(ap, opus_int32*); 3165 if (!value) 3166 goto bad_arg; 3167 *value = st->silk_mode.reducedDependency; 3168 } 3169 break; 3170 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: 3171 { 3172 opus_int32 value = va_arg(ap, opus_int32); 3173 if(value<0 || value>1) 3174 { 3175 goto bad_arg; 3176 } 3177 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 3178 celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value)); 3179 } 3180 break; 3181 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: 3182 { 3183 opus_int32 *value = va_arg(ap, opus_int32*); 3184 if (!value) 3185 { 3186 goto bad_arg; 3187 } 3188 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 3189 celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value)); 3190 else 3191 *value = 0; 3192 } 3193 break; 3194 #ifdef ENABLE_DRED 3195 case OPUS_SET_DRED_DURATION_REQUEST: 3196 { 3197 opus_int32 value = va_arg(ap, opus_int32); 3198 if(value<0 || value>DRED_MAX_FRAMES) 3199 { 3200 goto bad_arg; 3201 } 3202 st->dred_duration = value; 3203 st->silk_mode.useDRED = !!value; 3204 } 3205 break; 3206 case OPUS_GET_DRED_DURATION_REQUEST: 3207 { 3208 opus_int32 *value = va_arg(ap, opus_int32*); 3209 if (!value) 3210 { 3211 goto bad_arg; 3212 } 3213 *value = st->dred_duration; 3214 } 3215 break; 3216 #endif 3217 #ifdef ENABLE_QEXT 3218 case OPUS_SET_QEXT_REQUEST: 3219 { 3220 opus_int32 value = va_arg(ap, opus_int32); 3221 if(value<0 || value>1) 3222 { 3223 goto bad_arg; 3224 } 3225 st->enable_qext = value; 3226 } 3227 break; 3228 case OPUS_GET_QEXT_REQUEST: 3229 { 3230 opus_int32 *value = va_arg(ap, opus_int32*); 3231 if (!value) 3232 { 3233 goto bad_arg; 3234 } 3235 *value = st->enable_qext; 3236 } 3237 break; 3238 #endif 3239 case OPUS_RESET_STATE: 3240 { 3241 void *silk_enc; 3242 silk_EncControlStruct dummy; 3243 char *start; 3244 silk_enc = (char*)st+st->silk_enc_offset; 3245 #ifndef DISABLE_FLOAT_API 3246 tonality_analysis_reset(&st->analysis); 3247 #endif 3248 3249 start = (char*)&st->OPUS_ENCODER_RESET_START; 3250 OPUS_CLEAR(start, st->silk_enc_offset - (start - (char*)st)); 3251 3252 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 3253 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 3254 if (st->application != OPUS_APPLICATION_RESTRICTED_CELT) 3255 silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy ); 3256 #ifdef ENABLE_DRED 3257 /* Initialize DRED Encoder */ 3258 dred_encoder_reset( &st->dred_encoder ); 3259 #endif 3260 st->stream_channels = st->channels; 3261 st->hybrid_stereo_width_Q14 = 1 << 14; 3262 st->prev_HB_gain = Q15ONE; 3263 st->first = 1; 3264 st->mode = MODE_HYBRID; 3265 st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 3266 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 3267 } 3268 break; 3269 case OPUS_SET_FORCE_MODE_REQUEST: 3270 { 3271 opus_int32 value = va_arg(ap, opus_int32); 3272 if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO) 3273 { 3274 goto bad_arg; 3275 } 3276 st->user_forced_mode = value; 3277 } 3278 break; 3279 case OPUS_SET_LFE_REQUEST: 3280 { 3281 opus_int32 value = va_arg(ap, opus_int32); 3282 st->lfe = value; 3283 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 3284 ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value)); 3285 } 3286 break; 3287 case OPUS_SET_ENERGY_MASK_REQUEST: 3288 { 3289 celt_glog *value = va_arg(ap, celt_glog*); 3290 st->energy_masking = value; 3291 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) 3292 ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value)); 3293 } 3294 break; 3295 case OPUS_GET_IN_DTX_REQUEST: 3296 { 3297 opus_int32 *value = va_arg(ap, opus_int32*); 3298 if (!value) 3299 { 3300 goto bad_arg; 3301 } 3302 if (st->silk_mode.useDTX && (st->prev_mode == MODE_SILK_ONLY || st->prev_mode == MODE_HYBRID)) { 3303 /* DTX determined by Silk. */ 3304 silk_encoder *silk_enc = (silk_encoder*)(void *)((char*)st+st->silk_enc_offset); 3305 *value = silk_enc->state_Fxx[0].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX; 3306 /* Stereo: check second channel unless only the middle channel was encoded. */ 3307 if(*value == 1 && st->silk_mode.nChannelsInternal == 2 && silk_enc->prev_decode_only_middle == 0) { 3308 *value = silk_enc->state_Fxx[1].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX; 3309 } 3310 } 3311 else if (st->use_dtx) { 3312 /* DTX determined by Opus. */ 3313 *value = st->nb_no_activity_ms_Q1 >= NB_SPEECH_FRAMES_BEFORE_DTX*20*2; 3314 } 3315 else { 3316 *value = 0; 3317 } 3318 } 3319 break; 3320 #ifdef USE_WEIGHTS_FILE 3321 case OPUS_SET_DNN_BLOB_REQUEST: 3322 { 3323 const unsigned char *data = va_arg(ap, const unsigned char *); 3324 opus_int32 len = va_arg(ap, opus_int32); 3325 if(len<0 || data == NULL) 3326 { 3327 goto bad_arg; 3328 } 3329 #ifdef ENABLE_DRED 3330 ret = dred_encoder_load_model(&st->dred_encoder, data, len); 3331 #endif 3332 } 3333 break; 3334 #endif 3335 case CELT_GET_MODE_REQUEST: 3336 { 3337 const CELTMode ** value = va_arg(ap, const CELTMode**); 3338 if (!value) 3339 { 3340 goto bad_arg; 3341 } 3342 celt_assert(celt_enc != NULL); 3343 ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value)); 3344 } 3345 break; 3346 default: 3347 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/ 3348 ret = OPUS_UNIMPLEMENTED; 3349 break; 3350 } 3351 va_end(ap); 3352 return ret; 3353 bad_arg: 3354 va_end(ap); 3355 return OPUS_BAD_ARG; 3356 } 3357 3358 void opus_encoder_destroy(OpusEncoder *st) 3359 { 3360 opus_free(st); 3361 }