Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited |
michael@0 | 2 | Written by Jean-Marc Valin and Koen Vos */ |
michael@0 | 3 | /* |
michael@0 | 4 | Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | modification, are permitted provided that the following conditions |
michael@0 | 6 | are met: |
michael@0 | 7 | |
michael@0 | 8 | - Redistributions of source code must retain the above copyright |
michael@0 | 9 | notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | |
michael@0 | 11 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | documentation and/or other materials provided with the distribution. |
michael@0 | 14 | |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 16 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
michael@0 | 19 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | #ifdef HAVE_CONFIG_H |
michael@0 | 29 | #include "config.h" |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | #include <stdarg.h> |
michael@0 | 33 | #include "celt.h" |
michael@0 | 34 | #include "entenc.h" |
michael@0 | 35 | #include "modes.h" |
michael@0 | 36 | #include "API.h" |
michael@0 | 37 | #include "stack_alloc.h" |
michael@0 | 38 | #include "float_cast.h" |
michael@0 | 39 | #include "opus.h" |
michael@0 | 40 | #include "arch.h" |
michael@0 | 41 | #include "opus_private.h" |
michael@0 | 42 | #include "os_support.h" |
michael@0 | 43 | #include "cpu_support.h" |
michael@0 | 44 | #include "analysis.h" |
michael@0 | 45 | #include "mathops.h" |
michael@0 | 46 | #include "tuning_parameters.h" |
michael@0 | 47 | #ifdef FIXED_POINT |
michael@0 | 48 | #include "fixed/structs_FIX.h" |
michael@0 | 49 | #else |
michael@0 | 50 | #include "float/structs_FLP.h" |
michael@0 | 51 | #endif |
michael@0 | 52 | |
michael@0 | 53 | #define MAX_ENCODER_BUFFER 480 |
michael@0 | 54 | |
michael@0 | 55 | typedef struct { |
michael@0 | 56 | opus_val32 XX, XY, YY; |
michael@0 | 57 | opus_val16 smoothed_width; |
michael@0 | 58 | opus_val16 max_follower; |
michael@0 | 59 | } StereoWidthState; |
michael@0 | 60 | |
michael@0 | 61 | struct OpusEncoder { |
michael@0 | 62 | int celt_enc_offset; |
michael@0 | 63 | int silk_enc_offset; |
michael@0 | 64 | silk_EncControlStruct silk_mode; |
michael@0 | 65 | int application; |
michael@0 | 66 | int channels; |
michael@0 | 67 | int delay_compensation; |
michael@0 | 68 | int force_channels; |
michael@0 | 69 | int signal_type; |
michael@0 | 70 | int user_bandwidth; |
michael@0 | 71 | int max_bandwidth; |
michael@0 | 72 | int user_forced_mode; |
michael@0 | 73 | int voice_ratio; |
michael@0 | 74 | opus_int32 Fs; |
michael@0 | 75 | int use_vbr; |
michael@0 | 76 | int vbr_constraint; |
michael@0 | 77 | int variable_duration; |
michael@0 | 78 | opus_int32 bitrate_bps; |
michael@0 | 79 | opus_int32 user_bitrate_bps; |
michael@0 | 80 | int lsb_depth; |
michael@0 | 81 | int encoder_buffer; |
michael@0 | 82 | int lfe; |
michael@0 | 83 | |
michael@0 | 84 | #define OPUS_ENCODER_RESET_START stream_channels |
michael@0 | 85 | int stream_channels; |
michael@0 | 86 | opus_int16 hybrid_stereo_width_Q14; |
michael@0 | 87 | opus_int32 variable_HP_smth2_Q15; |
michael@0 | 88 | opus_val16 prev_HB_gain; |
michael@0 | 89 | opus_val32 hp_mem[4]; |
michael@0 | 90 | int mode; |
michael@0 | 91 | int prev_mode; |
michael@0 | 92 | int prev_channels; |
michael@0 | 93 | int prev_framesize; |
michael@0 | 94 | int bandwidth; |
michael@0 | 95 | int silk_bw_switch; |
michael@0 | 96 | /* Sampling rate (at the API level) */ |
michael@0 | 97 | int first; |
michael@0 | 98 | opus_val16 * energy_masking; |
michael@0 | 99 | StereoWidthState width_mem; |
michael@0 | 100 | opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2]; |
michael@0 | 101 | #ifndef DISABLE_FLOAT_API |
michael@0 | 102 | TonalityAnalysisState analysis; |
michael@0 | 103 | int detected_bandwidth; |
michael@0 | 104 | int analysis_offset; |
michael@0 | 105 | #endif |
michael@0 | 106 | opus_uint32 rangeFinal; |
michael@0 | 107 | int arch; |
michael@0 | 108 | }; |
michael@0 | 109 | |
michael@0 | 110 | /* Transition tables for the voice and music. First column is the |
michael@0 | 111 | middle (memoriless) threshold. The second column is the hysteresis |
michael@0 | 112 | (difference with the middle) */ |
michael@0 | 113 | static const opus_int32 mono_voice_bandwidth_thresholds[8] = { |
michael@0 | 114 | 11000, 1000, /* NB<->MB */ |
michael@0 | 115 | 14000, 1000, /* MB<->WB */ |
michael@0 | 116 | 17000, 1000, /* WB<->SWB */ |
michael@0 | 117 | 21000, 2000, /* SWB<->FB */ |
michael@0 | 118 | }; |
michael@0 | 119 | static const opus_int32 mono_music_bandwidth_thresholds[8] = { |
michael@0 | 120 | 12000, 1000, /* NB<->MB */ |
michael@0 | 121 | 15000, 1000, /* MB<->WB */ |
michael@0 | 122 | 18000, 2000, /* WB<->SWB */ |
michael@0 | 123 | 22000, 2000, /* SWB<->FB */ |
michael@0 | 124 | }; |
michael@0 | 125 | static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { |
michael@0 | 126 | 11000, 1000, /* NB<->MB */ |
michael@0 | 127 | 14000, 1000, /* MB<->WB */ |
michael@0 | 128 | 21000, 2000, /* WB<->SWB */ |
michael@0 | 129 | 28000, 2000, /* SWB<->FB */ |
michael@0 | 130 | }; |
michael@0 | 131 | static const opus_int32 stereo_music_bandwidth_thresholds[8] = { |
michael@0 | 132 | 12000, 1000, /* NB<->MB */ |
michael@0 | 133 | 18000, 2000, /* MB<->WB */ |
michael@0 | 134 | 21000, 2000, /* WB<->SWB */ |
michael@0 | 135 | 30000, 2000, /* SWB<->FB */ |
michael@0 | 136 | }; |
michael@0 | 137 | /* Threshold bit-rates for switching between mono and stereo */ |
michael@0 | 138 | static const opus_int32 stereo_voice_threshold = 30000; |
michael@0 | 139 | static const opus_int32 stereo_music_threshold = 30000; |
michael@0 | 140 | |
michael@0 | 141 | /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ |
michael@0 | 142 | static const opus_int32 mode_thresholds[2][2] = { |
michael@0 | 143 | /* voice */ /* music */ |
michael@0 | 144 | { 64000, 16000}, /* mono */ |
michael@0 | 145 | { 36000, 16000}, /* stereo */ |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | int opus_encoder_get_size(int channels) |
michael@0 | 149 | { |
michael@0 | 150 | int silkEncSizeBytes, celtEncSizeBytes; |
michael@0 | 151 | int ret; |
michael@0 | 152 | if (channels<1 || channels > 2) |
michael@0 | 153 | return 0; |
michael@0 | 154 | ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); |
michael@0 | 155 | if (ret) |
michael@0 | 156 | return 0; |
michael@0 | 157 | silkEncSizeBytes = align(silkEncSizeBytes); |
michael@0 | 158 | celtEncSizeBytes = celt_encoder_get_size(channels); |
michael@0 | 159 | return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application) |
michael@0 | 163 | { |
michael@0 | 164 | void *silk_enc; |
michael@0 | 165 | CELTEncoder *celt_enc; |
michael@0 | 166 | int err; |
michael@0 | 167 | int ret, silkEncSizeBytes; |
michael@0 | 168 | |
michael@0 | 169 | if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)|| |
michael@0 | 170 | (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO |
michael@0 | 171 | && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)) |
michael@0 | 172 | return OPUS_BAD_ARG; |
michael@0 | 173 | |
michael@0 | 174 | OPUS_CLEAR((char*)st, opus_encoder_get_size(channels)); |
michael@0 | 175 | /* Create SILK encoder */ |
michael@0 | 176 | ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); |
michael@0 | 177 | if (ret) |
michael@0 | 178 | return OPUS_BAD_ARG; |
michael@0 | 179 | silkEncSizeBytes = align(silkEncSizeBytes); |
michael@0 | 180 | st->silk_enc_offset = align(sizeof(OpusEncoder)); |
michael@0 | 181 | st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; |
michael@0 | 182 | silk_enc = (char*)st+st->silk_enc_offset; |
michael@0 | 183 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
michael@0 | 184 | |
michael@0 | 185 | st->stream_channels = st->channels = channels; |
michael@0 | 186 | |
michael@0 | 187 | st->Fs = Fs; |
michael@0 | 188 | |
michael@0 | 189 | st->arch = opus_select_arch(); |
michael@0 | 190 | |
michael@0 | 191 | ret = silk_InitEncoder( silk_enc, st->arch, &st->silk_mode ); |
michael@0 | 192 | if(ret)return OPUS_INTERNAL_ERROR; |
michael@0 | 193 | |
michael@0 | 194 | /* default SILK parameters */ |
michael@0 | 195 | st->silk_mode.nChannelsAPI = channels; |
michael@0 | 196 | st->silk_mode.nChannelsInternal = channels; |
michael@0 | 197 | st->silk_mode.API_sampleRate = st->Fs; |
michael@0 | 198 | st->silk_mode.maxInternalSampleRate = 16000; |
michael@0 | 199 | st->silk_mode.minInternalSampleRate = 8000; |
michael@0 | 200 | st->silk_mode.desiredInternalSampleRate = 16000; |
michael@0 | 201 | st->silk_mode.payloadSize_ms = 20; |
michael@0 | 202 | st->silk_mode.bitRate = 25000; |
michael@0 | 203 | st->silk_mode.packetLossPercentage = 0; |
michael@0 | 204 | st->silk_mode.complexity = 9; |
michael@0 | 205 | st->silk_mode.useInBandFEC = 0; |
michael@0 | 206 | st->silk_mode.useDTX = 0; |
michael@0 | 207 | st->silk_mode.useCBR = 0; |
michael@0 | 208 | st->silk_mode.reducedDependency = 0; |
michael@0 | 209 | |
michael@0 | 210 | /* Create CELT encoder */ |
michael@0 | 211 | /* Initialize CELT encoder */ |
michael@0 | 212 | err = celt_encoder_init(celt_enc, Fs, channels, st->arch); |
michael@0 | 213 | if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR; |
michael@0 | 214 | |
michael@0 | 215 | celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); |
michael@0 | 216 | celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity)); |
michael@0 | 217 | |
michael@0 | 218 | st->use_vbr = 1; |
michael@0 | 219 | /* Makes constrained VBR the default (safer for real-time use) */ |
michael@0 | 220 | st->vbr_constraint = 1; |
michael@0 | 221 | st->user_bitrate_bps = OPUS_AUTO; |
michael@0 | 222 | st->bitrate_bps = 3000+Fs*channels; |
michael@0 | 223 | st->application = application; |
michael@0 | 224 | st->signal_type = OPUS_AUTO; |
michael@0 | 225 | st->user_bandwidth = OPUS_AUTO; |
michael@0 | 226 | st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND; |
michael@0 | 227 | st->force_channels = OPUS_AUTO; |
michael@0 | 228 | st->user_forced_mode = OPUS_AUTO; |
michael@0 | 229 | st->voice_ratio = -1; |
michael@0 | 230 | st->encoder_buffer = st->Fs/100; |
michael@0 | 231 | st->lsb_depth = 24; |
michael@0 | 232 | st->variable_duration = OPUS_FRAMESIZE_ARG; |
michael@0 | 233 | |
michael@0 | 234 | /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead |
michael@0 | 235 | + 1.5 ms for SILK resamplers and stereo prediction) */ |
michael@0 | 236 | st->delay_compensation = st->Fs/250; |
michael@0 | 237 | |
michael@0 | 238 | st->hybrid_stereo_width_Q14 = 1 << 14; |
michael@0 | 239 | st->prev_HB_gain = Q15ONE; |
michael@0 | 240 | st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
michael@0 | 241 | st->first = 1; |
michael@0 | 242 | st->mode = MODE_HYBRID; |
michael@0 | 243 | st->bandwidth = OPUS_BANDWIDTH_FULLBAND; |
michael@0 | 244 | |
michael@0 | 245 | return OPUS_OK; |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels) |
michael@0 | 249 | { |
michael@0 | 250 | int period; |
michael@0 | 251 | unsigned char toc; |
michael@0 | 252 | period = 0; |
michael@0 | 253 | while (framerate < 400) |
michael@0 | 254 | { |
michael@0 | 255 | framerate <<= 1; |
michael@0 | 256 | period++; |
michael@0 | 257 | } |
michael@0 | 258 | if (mode == MODE_SILK_ONLY) |
michael@0 | 259 | { |
michael@0 | 260 | toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5; |
michael@0 | 261 | toc |= (period-2)<<3; |
michael@0 | 262 | } else if (mode == MODE_CELT_ONLY) |
michael@0 | 263 | { |
michael@0 | 264 | int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND; |
michael@0 | 265 | if (tmp < 0) |
michael@0 | 266 | tmp = 0; |
michael@0 | 267 | toc = 0x80; |
michael@0 | 268 | toc |= tmp << 5; |
michael@0 | 269 | toc |= period<<3; |
michael@0 | 270 | } else /* Hybrid */ |
michael@0 | 271 | { |
michael@0 | 272 | toc = 0x60; |
michael@0 | 273 | toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4; |
michael@0 | 274 | toc |= (period-2)<<3; |
michael@0 | 275 | } |
michael@0 | 276 | toc |= (channels==2)<<2; |
michael@0 | 277 | return toc; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | #ifndef FIXED_POINT |
michael@0 | 281 | static void silk_biquad_float( |
michael@0 | 282 | const opus_val16 *in, /* I: Input signal */ |
michael@0 | 283 | const opus_int32 *B_Q28, /* I: MA coefficients [3] */ |
michael@0 | 284 | const opus_int32 *A_Q28, /* I: AR coefficients [2] */ |
michael@0 | 285 | opus_val32 *S, /* I/O: State vector [2] */ |
michael@0 | 286 | opus_val16 *out, /* O: Output signal */ |
michael@0 | 287 | const opus_int32 len, /* I: Signal length (must be even) */ |
michael@0 | 288 | int stride |
michael@0 | 289 | ) |
michael@0 | 290 | { |
michael@0 | 291 | /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ |
michael@0 | 292 | opus_int k; |
michael@0 | 293 | opus_val32 vout; |
michael@0 | 294 | opus_val32 inval; |
michael@0 | 295 | opus_val32 A[2], B[3]; |
michael@0 | 296 | |
michael@0 | 297 | A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28))); |
michael@0 | 298 | A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28))); |
michael@0 | 299 | B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28))); |
michael@0 | 300 | B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28))); |
michael@0 | 301 | B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28))); |
michael@0 | 302 | |
michael@0 | 303 | /* Negate A_Q28 values and split in two parts */ |
michael@0 | 304 | |
michael@0 | 305 | for( k = 0; k < len; k++ ) { |
michael@0 | 306 | /* S[ 0 ], S[ 1 ]: Q12 */ |
michael@0 | 307 | inval = in[ k*stride ]; |
michael@0 | 308 | vout = S[ 0 ] + B[0]*inval; |
michael@0 | 309 | |
michael@0 | 310 | S[ 0 ] = S[1] - vout*A[0] + B[1]*inval; |
michael@0 | 311 | |
michael@0 | 312 | S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL; |
michael@0 | 313 | |
michael@0 | 314 | /* Scale back to Q0 and saturate */ |
michael@0 | 315 | out[ k*stride ] = vout; |
michael@0 | 316 | } |
michael@0 | 317 | } |
michael@0 | 318 | #endif |
michael@0 | 319 | |
michael@0 | 320 | static void hp_cutoff(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) |
michael@0 | 321 | { |
michael@0 | 322 | opus_int32 B_Q28[ 3 ], A_Q28[ 2 ]; |
michael@0 | 323 | opus_int32 Fc_Q19, r_Q28, r_Q22; |
michael@0 | 324 | |
michael@0 | 325 | silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) ); |
michael@0 | 326 | Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 ); |
michael@0 | 327 | silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 ); |
michael@0 | 328 | |
michael@0 | 329 | r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 ); |
michael@0 | 330 | |
michael@0 | 331 | /* b = r * [ 1; -2; 1 ]; */ |
michael@0 | 332 | /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ |
michael@0 | 333 | B_Q28[ 0 ] = r_Q28; |
michael@0 | 334 | B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 ); |
michael@0 | 335 | B_Q28[ 2 ] = r_Q28; |
michael@0 | 336 | |
michael@0 | 337 | /* -r * ( 2 - Fc * Fc ); */ |
michael@0 | 338 | r_Q22 = silk_RSHIFT( r_Q28, 6 ); |
michael@0 | 339 | A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) ); |
michael@0 | 340 | A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 ); |
michael@0 | 341 | |
michael@0 | 342 | #ifdef FIXED_POINT |
michael@0 | 343 | silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels ); |
michael@0 | 344 | if( channels == 2 ) { |
michael@0 | 345 | silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); |
michael@0 | 346 | } |
michael@0 | 347 | #else |
michael@0 | 348 | silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels ); |
michael@0 | 349 | if( channels == 2 ) { |
michael@0 | 350 | silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); |
michael@0 | 351 | } |
michael@0 | 352 | #endif |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | #ifdef FIXED_POINT |
michael@0 | 356 | 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) |
michael@0 | 357 | { |
michael@0 | 358 | int c, i; |
michael@0 | 359 | int shift; |
michael@0 | 360 | |
michael@0 | 361 | /* Approximates -round(log2(4.*cutoff_Hz/Fs)) */ |
michael@0 | 362 | shift=celt_ilog2(Fs/(cutoff_Hz*3)); |
michael@0 | 363 | for (c=0;c<channels;c++) |
michael@0 | 364 | { |
michael@0 | 365 | for (i=0;i<len;i++) |
michael@0 | 366 | { |
michael@0 | 367 | opus_val32 x, tmp, y; |
michael@0 | 368 | x = SHL32(EXTEND32(in[channels*i+c]), 15); |
michael@0 | 369 | /* First stage */ |
michael@0 | 370 | tmp = x-hp_mem[2*c]; |
michael@0 | 371 | hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift); |
michael@0 | 372 | /* Second stage */ |
michael@0 | 373 | y = tmp - hp_mem[2*c+1]; |
michael@0 | 374 | hp_mem[2*c+1] = hp_mem[2*c+1] + PSHR32(tmp - hp_mem[2*c+1], shift); |
michael@0 | 375 | out[channels*i+c] = EXTRACT16(SATURATE(PSHR32(y, 15), 32767)); |
michael@0 | 376 | } |
michael@0 | 377 | } |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | #else |
michael@0 | 381 | 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) |
michael@0 | 382 | { |
michael@0 | 383 | int c, i; |
michael@0 | 384 | float coef; |
michael@0 | 385 | |
michael@0 | 386 | coef = 4.0f*cutoff_Hz/Fs; |
michael@0 | 387 | for (c=0;c<channels;c++) |
michael@0 | 388 | { |
michael@0 | 389 | for (i=0;i<len;i++) |
michael@0 | 390 | { |
michael@0 | 391 | opus_val32 x, tmp, y; |
michael@0 | 392 | x = in[channels*i+c]; |
michael@0 | 393 | /* First stage */ |
michael@0 | 394 | tmp = x-hp_mem[2*c]; |
michael@0 | 395 | hp_mem[2*c] = hp_mem[2*c] + coef*(x - hp_mem[2*c]) + VERY_SMALL; |
michael@0 | 396 | /* Second stage */ |
michael@0 | 397 | y = tmp - hp_mem[2*c+1]; |
michael@0 | 398 | hp_mem[2*c+1] = hp_mem[2*c+1] + coef*(tmp - hp_mem[2*c+1]) + VERY_SMALL; |
michael@0 | 399 | out[channels*i+c] = y; |
michael@0 | 400 | } |
michael@0 | 401 | } |
michael@0 | 402 | } |
michael@0 | 403 | #endif |
michael@0 | 404 | |
michael@0 | 405 | static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2, |
michael@0 | 406 | int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs) |
michael@0 | 407 | { |
michael@0 | 408 | int i; |
michael@0 | 409 | int overlap; |
michael@0 | 410 | int inc; |
michael@0 | 411 | inc = 48000/Fs; |
michael@0 | 412 | overlap=overlap48/inc; |
michael@0 | 413 | g1 = Q15ONE-g1; |
michael@0 | 414 | g2 = Q15ONE-g2; |
michael@0 | 415 | for (i=0;i<overlap;i++) |
michael@0 | 416 | { |
michael@0 | 417 | opus_val32 diff; |
michael@0 | 418 | opus_val16 g, w; |
michael@0 | 419 | w = MULT16_16_Q15(window[i*inc], window[i*inc]); |
michael@0 | 420 | g = SHR32(MAC16_16(MULT16_16(w,g2), |
michael@0 | 421 | Q15ONE-w, g1), 15); |
michael@0 | 422 | diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1])); |
michael@0 | 423 | diff = MULT16_16_Q15(g, diff); |
michael@0 | 424 | out[i*channels] = out[i*channels] - diff; |
michael@0 | 425 | out[i*channels+1] = out[i*channels+1] + diff; |
michael@0 | 426 | } |
michael@0 | 427 | for (;i<frame_size;i++) |
michael@0 | 428 | { |
michael@0 | 429 | opus_val32 diff; |
michael@0 | 430 | diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1])); |
michael@0 | 431 | diff = MULT16_16_Q15(g2, diff); |
michael@0 | 432 | out[i*channels] = out[i*channels] - diff; |
michael@0 | 433 | out[i*channels+1] = out[i*channels+1] + diff; |
michael@0 | 434 | } |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | static void gain_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2, |
michael@0 | 438 | int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs) |
michael@0 | 439 | { |
michael@0 | 440 | int i; |
michael@0 | 441 | int inc; |
michael@0 | 442 | int overlap; |
michael@0 | 443 | int c; |
michael@0 | 444 | inc = 48000/Fs; |
michael@0 | 445 | overlap=overlap48/inc; |
michael@0 | 446 | if (channels==1) |
michael@0 | 447 | { |
michael@0 | 448 | for (i=0;i<overlap;i++) |
michael@0 | 449 | { |
michael@0 | 450 | opus_val16 g, w; |
michael@0 | 451 | w = MULT16_16_Q15(window[i*inc], window[i*inc]); |
michael@0 | 452 | g = SHR32(MAC16_16(MULT16_16(w,g2), |
michael@0 | 453 | Q15ONE-w, g1), 15); |
michael@0 | 454 | out[i] = MULT16_16_Q15(g, in[i]); |
michael@0 | 455 | } |
michael@0 | 456 | } else { |
michael@0 | 457 | for (i=0;i<overlap;i++) |
michael@0 | 458 | { |
michael@0 | 459 | opus_val16 g, w; |
michael@0 | 460 | w = MULT16_16_Q15(window[i*inc], window[i*inc]); |
michael@0 | 461 | g = SHR32(MAC16_16(MULT16_16(w,g2), |
michael@0 | 462 | Q15ONE-w, g1), 15); |
michael@0 | 463 | out[i*2] = MULT16_16_Q15(g, in[i*2]); |
michael@0 | 464 | out[i*2+1] = MULT16_16_Q15(g, in[i*2+1]); |
michael@0 | 465 | } |
michael@0 | 466 | } |
michael@0 | 467 | c=0;do { |
michael@0 | 468 | for (i=overlap;i<frame_size;i++) |
michael@0 | 469 | { |
michael@0 | 470 | out[i*channels+c] = MULT16_16_Q15(g2, in[i*channels+c]); |
michael@0 | 471 | } |
michael@0 | 472 | } |
michael@0 | 473 | while (++c<channels); |
michael@0 | 474 | } |
michael@0 | 475 | |
michael@0 | 476 | OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error) |
michael@0 | 477 | { |
michael@0 | 478 | int ret; |
michael@0 | 479 | OpusEncoder *st; |
michael@0 | 480 | if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)|| |
michael@0 | 481 | (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO |
michael@0 | 482 | && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)) |
michael@0 | 483 | { |
michael@0 | 484 | if (error) |
michael@0 | 485 | *error = OPUS_BAD_ARG; |
michael@0 | 486 | return NULL; |
michael@0 | 487 | } |
michael@0 | 488 | st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels)); |
michael@0 | 489 | if (st == NULL) |
michael@0 | 490 | { |
michael@0 | 491 | if (error) |
michael@0 | 492 | *error = OPUS_ALLOC_FAIL; |
michael@0 | 493 | return NULL; |
michael@0 | 494 | } |
michael@0 | 495 | ret = opus_encoder_init(st, Fs, channels, application); |
michael@0 | 496 | if (error) |
michael@0 | 497 | *error = ret; |
michael@0 | 498 | if (ret != OPUS_OK) |
michael@0 | 499 | { |
michael@0 | 500 | opus_free(st); |
michael@0 | 501 | st = NULL; |
michael@0 | 502 | } |
michael@0 | 503 | return st; |
michael@0 | 504 | } |
michael@0 | 505 | |
michael@0 | 506 | static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes) |
michael@0 | 507 | { |
michael@0 | 508 | if(!frame_size)frame_size=st->Fs/400; |
michael@0 | 509 | if (st->user_bitrate_bps==OPUS_AUTO) |
michael@0 | 510 | return 60*st->Fs/frame_size + st->Fs*st->channels; |
michael@0 | 511 | else if (st->user_bitrate_bps==OPUS_BITRATE_MAX) |
michael@0 | 512 | return max_data_bytes*8*st->Fs/frame_size; |
michael@0 | 513 | else |
michael@0 | 514 | return st->user_bitrate_bps; |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | #ifndef DISABLE_FLOAT_API |
michael@0 | 518 | /* Don't use more than 60 ms for the frame size analysis */ |
michael@0 | 519 | #define MAX_DYNAMIC_FRAMESIZE 24 |
michael@0 | 520 | /* Estimates how much the bitrate will be boosted based on the sub-frame energy */ |
michael@0 | 521 | static float transient_boost(const float *E, const float *E_1, int LM, int maxM) |
michael@0 | 522 | { |
michael@0 | 523 | int i; |
michael@0 | 524 | int M; |
michael@0 | 525 | float sumE=0, sumE_1=0; |
michael@0 | 526 | float metric; |
michael@0 | 527 | |
michael@0 | 528 | M = IMIN(maxM, (1<<LM)+1); |
michael@0 | 529 | for (i=0;i<M;i++) |
michael@0 | 530 | { |
michael@0 | 531 | sumE += E[i]; |
michael@0 | 532 | sumE_1 += E_1[i]; |
michael@0 | 533 | } |
michael@0 | 534 | metric = sumE*sumE_1/(M*M); |
michael@0 | 535 | /*if (LM==3) |
michael@0 | 536 | printf("%f\n", metric);*/ |
michael@0 | 537 | /*return metric>10 ? 1 : 0;*/ |
michael@0 | 538 | /*return MAX16(0,1-exp(-.25*(metric-2.)));*/ |
michael@0 | 539 | return MIN16(1,(float)sqrt(MAX16(0,.05f*(metric-2)))); |
michael@0 | 540 | } |
michael@0 | 541 | |
michael@0 | 542 | /* Viterbi decoding trying to find the best frame size combination using look-ahead |
michael@0 | 543 | |
michael@0 | 544 | State numbering: |
michael@0 | 545 | 0: unused |
michael@0 | 546 | 1: 2.5 ms |
michael@0 | 547 | 2: 5 ms (#1) |
michael@0 | 548 | 3: 5 ms (#2) |
michael@0 | 549 | 4: 10 ms (#1) |
michael@0 | 550 | 5: 10 ms (#2) |
michael@0 | 551 | 6: 10 ms (#3) |
michael@0 | 552 | 7: 10 ms (#4) |
michael@0 | 553 | 8: 20 ms (#1) |
michael@0 | 554 | 9: 20 ms (#2) |
michael@0 | 555 | 10: 20 ms (#3) |
michael@0 | 556 | 11: 20 ms (#4) |
michael@0 | 557 | 12: 20 ms (#5) |
michael@0 | 558 | 13: 20 ms (#6) |
michael@0 | 559 | 14: 20 ms (#7) |
michael@0 | 560 | 15: 20 ms (#8) |
michael@0 | 561 | */ |
michael@0 | 562 | static int transient_viterbi(const float *E, const float *E_1, int N, int frame_cost, int rate) |
michael@0 | 563 | { |
michael@0 | 564 | int i; |
michael@0 | 565 | float cost[MAX_DYNAMIC_FRAMESIZE][16]; |
michael@0 | 566 | int states[MAX_DYNAMIC_FRAMESIZE][16]; |
michael@0 | 567 | float best_cost; |
michael@0 | 568 | int best_state; |
michael@0 | 569 | float factor; |
michael@0 | 570 | /* Take into account that we damp VBR in the 32 kb/s to 64 kb/s range. */ |
michael@0 | 571 | if (rate<80) |
michael@0 | 572 | factor=0; |
michael@0 | 573 | else if (rate>160) |
michael@0 | 574 | factor=1; |
michael@0 | 575 | else |
michael@0 | 576 | factor = (rate-80.f)/80.f; |
michael@0 | 577 | /* Makes variable framesize less aggressive at lower bitrates, but I can't |
michael@0 | 578 | find any valid theoretical justification for this (other than it seems |
michael@0 | 579 | to help) */ |
michael@0 | 580 | for (i=0;i<16;i++) |
michael@0 | 581 | { |
michael@0 | 582 | /* Impossible state */ |
michael@0 | 583 | states[0][i] = -1; |
michael@0 | 584 | cost[0][i] = 1e10; |
michael@0 | 585 | } |
michael@0 | 586 | for (i=0;i<4;i++) |
michael@0 | 587 | { |
michael@0 | 588 | cost[0][1<<i] = (frame_cost + rate*(1<<i))*(1+factor*transient_boost(E, E_1, i, N+1)); |
michael@0 | 589 | states[0][1<<i] = i; |
michael@0 | 590 | } |
michael@0 | 591 | for (i=1;i<N;i++) |
michael@0 | 592 | { |
michael@0 | 593 | int j; |
michael@0 | 594 | |
michael@0 | 595 | /* Follow continuations */ |
michael@0 | 596 | for (j=2;j<16;j++) |
michael@0 | 597 | { |
michael@0 | 598 | cost[i][j] = cost[i-1][j-1]; |
michael@0 | 599 | states[i][j] = j-1; |
michael@0 | 600 | } |
michael@0 | 601 | |
michael@0 | 602 | /* New frames */ |
michael@0 | 603 | for(j=0;j<4;j++) |
michael@0 | 604 | { |
michael@0 | 605 | int k; |
michael@0 | 606 | float min_cost; |
michael@0 | 607 | float curr_cost; |
michael@0 | 608 | states[i][1<<j] = 1; |
michael@0 | 609 | min_cost = cost[i-1][1]; |
michael@0 | 610 | for(k=1;k<4;k++) |
michael@0 | 611 | { |
michael@0 | 612 | float tmp = cost[i-1][(1<<(k+1))-1]; |
michael@0 | 613 | if (tmp < min_cost) |
michael@0 | 614 | { |
michael@0 | 615 | states[i][1<<j] = (1<<(k+1))-1; |
michael@0 | 616 | min_cost = tmp; |
michael@0 | 617 | } |
michael@0 | 618 | } |
michael@0 | 619 | curr_cost = (frame_cost + rate*(1<<j))*(1+factor*transient_boost(E+i, E_1+i, j, N-i+1)); |
michael@0 | 620 | cost[i][1<<j] = min_cost; |
michael@0 | 621 | /* If part of the frame is outside the analysis window, only count part of the cost */ |
michael@0 | 622 | if (N-i < (1<<j)) |
michael@0 | 623 | cost[i][1<<j] += curr_cost*(float)(N-i)/(1<<j); |
michael@0 | 624 | else |
michael@0 | 625 | cost[i][1<<j] += curr_cost; |
michael@0 | 626 | } |
michael@0 | 627 | } |
michael@0 | 628 | |
michael@0 | 629 | best_state=1; |
michael@0 | 630 | best_cost = cost[N-1][1]; |
michael@0 | 631 | /* Find best end state (doesn't force a frame to end at N-1) */ |
michael@0 | 632 | for (i=2;i<16;i++) |
michael@0 | 633 | { |
michael@0 | 634 | if (cost[N-1][i]<best_cost) |
michael@0 | 635 | { |
michael@0 | 636 | best_cost = cost[N-1][i]; |
michael@0 | 637 | best_state = i; |
michael@0 | 638 | } |
michael@0 | 639 | } |
michael@0 | 640 | |
michael@0 | 641 | /* Follow transitions back */ |
michael@0 | 642 | for (i=N-1;i>=0;i--) |
michael@0 | 643 | { |
michael@0 | 644 | /*printf("%d ", best_state);*/ |
michael@0 | 645 | best_state = states[i][best_state]; |
michael@0 | 646 | } |
michael@0 | 647 | /*printf("%d\n", best_state);*/ |
michael@0 | 648 | return best_state; |
michael@0 | 649 | } |
michael@0 | 650 | |
michael@0 | 651 | int optimize_framesize(const opus_val16 *x, int len, int C, opus_int32 Fs, |
michael@0 | 652 | int bitrate, opus_val16 tonality, float *mem, int buffering, |
michael@0 | 653 | downmix_func downmix) |
michael@0 | 654 | { |
michael@0 | 655 | int N; |
michael@0 | 656 | int i; |
michael@0 | 657 | float e[MAX_DYNAMIC_FRAMESIZE+4]; |
michael@0 | 658 | float e_1[MAX_DYNAMIC_FRAMESIZE+3]; |
michael@0 | 659 | opus_val32 memx; |
michael@0 | 660 | int bestLM=0; |
michael@0 | 661 | int subframe; |
michael@0 | 662 | int pos; |
michael@0 | 663 | VARDECL(opus_val32, sub); |
michael@0 | 664 | |
michael@0 | 665 | subframe = Fs/400; |
michael@0 | 666 | ALLOC(sub, subframe, opus_val32); |
michael@0 | 667 | e[0]=mem[0]; |
michael@0 | 668 | e_1[0]=1.f/(EPSILON+mem[0]); |
michael@0 | 669 | if (buffering) |
michael@0 | 670 | { |
michael@0 | 671 | /* Consider the CELT delay when not in restricted-lowdelay */ |
michael@0 | 672 | /* We assume the buffering is between 2.5 and 5 ms */ |
michael@0 | 673 | int offset = 2*subframe - buffering; |
michael@0 | 674 | celt_assert(offset>=0 && offset <= subframe); |
michael@0 | 675 | x += C*offset; |
michael@0 | 676 | len -= offset; |
michael@0 | 677 | e[1]=mem[1]; |
michael@0 | 678 | e_1[1]=1.f/(EPSILON+mem[1]); |
michael@0 | 679 | e[2]=mem[2]; |
michael@0 | 680 | e_1[2]=1.f/(EPSILON+mem[2]); |
michael@0 | 681 | pos = 3; |
michael@0 | 682 | } else { |
michael@0 | 683 | pos=1; |
michael@0 | 684 | } |
michael@0 | 685 | N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE); |
michael@0 | 686 | /* Just silencing a warning, it's really initialized later */ |
michael@0 | 687 | memx = 0; |
michael@0 | 688 | for (i=0;i<N;i++) |
michael@0 | 689 | { |
michael@0 | 690 | float tmp; |
michael@0 | 691 | opus_val32 tmpx; |
michael@0 | 692 | int j; |
michael@0 | 693 | tmp=EPSILON; |
michael@0 | 694 | |
michael@0 | 695 | downmix(x, sub, subframe, i*subframe, 0, -2, C); |
michael@0 | 696 | if (i==0) |
michael@0 | 697 | memx = sub[0]; |
michael@0 | 698 | for (j=0;j<subframe;j++) |
michael@0 | 699 | { |
michael@0 | 700 | tmpx = sub[j]; |
michael@0 | 701 | tmp += (tmpx-memx)*(float)(tmpx-memx); |
michael@0 | 702 | memx = tmpx; |
michael@0 | 703 | } |
michael@0 | 704 | e[i+pos] = tmp; |
michael@0 | 705 | e_1[i+pos] = 1.f/tmp; |
michael@0 | 706 | } |
michael@0 | 707 | /* Hack to get 20 ms working with APPLICATION_AUDIO |
michael@0 | 708 | The real problem is that the corresponding memory needs to use 1.5 ms |
michael@0 | 709 | from this frame and 1 ms from the next frame */ |
michael@0 | 710 | e[i+pos] = e[i+pos-1]; |
michael@0 | 711 | if (buffering) |
michael@0 | 712 | N=IMIN(MAX_DYNAMIC_FRAMESIZE, N+2); |
michael@0 | 713 | bestLM = transient_viterbi(e, e_1, N, (int)((1.f+.5f*tonality)*(60*C+40)), bitrate/400); |
michael@0 | 714 | mem[0] = e[1<<bestLM]; |
michael@0 | 715 | if (buffering) |
michael@0 | 716 | { |
michael@0 | 717 | mem[1] = e[(1<<bestLM)+1]; |
michael@0 | 718 | mem[2] = e[(1<<bestLM)+2]; |
michael@0 | 719 | } |
michael@0 | 720 | return bestLM; |
michael@0 | 721 | } |
michael@0 | 722 | |
michael@0 | 723 | #endif |
michael@0 | 724 | |
michael@0 | 725 | #ifndef DISABLE_FLOAT_API |
michael@0 | 726 | #ifdef FIXED_POINT |
michael@0 | 727 | #define PCM2VAL(x) FLOAT2INT16(x) |
michael@0 | 728 | #else |
michael@0 | 729 | #define PCM2VAL(x) SCALEIN(x) |
michael@0 | 730 | #endif |
michael@0 | 731 | void downmix_float(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C) |
michael@0 | 732 | { |
michael@0 | 733 | const float *x; |
michael@0 | 734 | opus_val32 scale; |
michael@0 | 735 | int j; |
michael@0 | 736 | x = (const float *)_x; |
michael@0 | 737 | for (j=0;j<subframe;j++) |
michael@0 | 738 | sub[j] = PCM2VAL(x[(j+offset)*C+c1]); |
michael@0 | 739 | if (c2>-1) |
michael@0 | 740 | { |
michael@0 | 741 | for (j=0;j<subframe;j++) |
michael@0 | 742 | sub[j] += PCM2VAL(x[(j+offset)*C+c2]); |
michael@0 | 743 | } else if (c2==-2) |
michael@0 | 744 | { |
michael@0 | 745 | int c; |
michael@0 | 746 | for (c=1;c<C;c++) |
michael@0 | 747 | { |
michael@0 | 748 | for (j=0;j<subframe;j++) |
michael@0 | 749 | sub[j] += PCM2VAL(x[(j+offset)*C+c]); |
michael@0 | 750 | } |
michael@0 | 751 | } |
michael@0 | 752 | #ifdef FIXED_POINT |
michael@0 | 753 | scale = (1<<SIG_SHIFT); |
michael@0 | 754 | #else |
michael@0 | 755 | scale = 1.f; |
michael@0 | 756 | #endif |
michael@0 | 757 | if (C==-2) |
michael@0 | 758 | scale /= C; |
michael@0 | 759 | else |
michael@0 | 760 | scale /= 2; |
michael@0 | 761 | for (j=0;j<subframe;j++) |
michael@0 | 762 | sub[j] *= scale; |
michael@0 | 763 | } |
michael@0 | 764 | #endif |
michael@0 | 765 | |
michael@0 | 766 | void downmix_int(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C) |
michael@0 | 767 | { |
michael@0 | 768 | const opus_int16 *x; |
michael@0 | 769 | opus_val32 scale; |
michael@0 | 770 | int j; |
michael@0 | 771 | x = (const opus_int16 *)_x; |
michael@0 | 772 | for (j=0;j<subframe;j++) |
michael@0 | 773 | sub[j] = x[(j+offset)*C+c1]; |
michael@0 | 774 | if (c2>-1) |
michael@0 | 775 | { |
michael@0 | 776 | for (j=0;j<subframe;j++) |
michael@0 | 777 | sub[j] += x[(j+offset)*C+c2]; |
michael@0 | 778 | } else if (c2==-2) |
michael@0 | 779 | { |
michael@0 | 780 | int c; |
michael@0 | 781 | for (c=1;c<C;c++) |
michael@0 | 782 | { |
michael@0 | 783 | for (j=0;j<subframe;j++) |
michael@0 | 784 | sub[j] += x[(j+offset)*C+c]; |
michael@0 | 785 | } |
michael@0 | 786 | } |
michael@0 | 787 | #ifdef FIXED_POINT |
michael@0 | 788 | scale = (1<<SIG_SHIFT); |
michael@0 | 789 | #else |
michael@0 | 790 | scale = 1.f/32768; |
michael@0 | 791 | #endif |
michael@0 | 792 | if (C==-2) |
michael@0 | 793 | scale /= C; |
michael@0 | 794 | else |
michael@0 | 795 | scale /= 2; |
michael@0 | 796 | for (j=0;j<subframe;j++) |
michael@0 | 797 | sub[j] *= scale; |
michael@0 | 798 | } |
michael@0 | 799 | |
michael@0 | 800 | opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_int32 Fs) |
michael@0 | 801 | { |
michael@0 | 802 | int new_size; |
michael@0 | 803 | if (frame_size<Fs/400) |
michael@0 | 804 | return -1; |
michael@0 | 805 | if (variable_duration == OPUS_FRAMESIZE_ARG) |
michael@0 | 806 | new_size = frame_size; |
michael@0 | 807 | else if (variable_duration == OPUS_FRAMESIZE_VARIABLE) |
michael@0 | 808 | new_size = Fs/50; |
michael@0 | 809 | else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_60_MS) |
michael@0 | 810 | new_size = IMIN(3*Fs/50, (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS)); |
michael@0 | 811 | else |
michael@0 | 812 | return -1; |
michael@0 | 813 | if (new_size>frame_size) |
michael@0 | 814 | return -1; |
michael@0 | 815 | if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs && |
michael@0 | 816 | 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs) |
michael@0 | 817 | return -1; |
michael@0 | 818 | return new_size; |
michael@0 | 819 | } |
michael@0 | 820 | |
michael@0 | 821 | opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size, |
michael@0 | 822 | int variable_duration, int C, opus_int32 Fs, int bitrate_bps, |
michael@0 | 823 | int delay_compensation, downmix_func downmix |
michael@0 | 824 | #ifndef DISABLE_FLOAT_API |
michael@0 | 825 | , float *subframe_mem |
michael@0 | 826 | #endif |
michael@0 | 827 | ) |
michael@0 | 828 | { |
michael@0 | 829 | #ifndef DISABLE_FLOAT_API |
michael@0 | 830 | if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200) |
michael@0 | 831 | { |
michael@0 | 832 | int LM = 3; |
michael@0 | 833 | LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps, |
michael@0 | 834 | 0, subframe_mem, delay_compensation, downmix); |
michael@0 | 835 | while ((Fs/400<<LM)>frame_size) |
michael@0 | 836 | LM--; |
michael@0 | 837 | frame_size = (Fs/400<<LM); |
michael@0 | 838 | } else |
michael@0 | 839 | #endif |
michael@0 | 840 | { |
michael@0 | 841 | frame_size = frame_size_select(frame_size, variable_duration, Fs); |
michael@0 | 842 | } |
michael@0 | 843 | if (frame_size<0) |
michael@0 | 844 | return -1; |
michael@0 | 845 | return frame_size; |
michael@0 | 846 | } |
michael@0 | 847 | |
michael@0 | 848 | opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem) |
michael@0 | 849 | { |
michael@0 | 850 | opus_val16 corr; |
michael@0 | 851 | opus_val16 ldiff; |
michael@0 | 852 | opus_val16 width; |
michael@0 | 853 | opus_val32 xx, xy, yy; |
michael@0 | 854 | opus_val16 sqrt_xx, sqrt_yy; |
michael@0 | 855 | opus_val16 qrrt_xx, qrrt_yy; |
michael@0 | 856 | int frame_rate; |
michael@0 | 857 | int i; |
michael@0 | 858 | opus_val16 short_alpha; |
michael@0 | 859 | |
michael@0 | 860 | frame_rate = Fs/frame_size; |
michael@0 | 861 | short_alpha = Q15ONE - 25*Q15ONE/IMAX(50,frame_rate); |
michael@0 | 862 | xx=xy=yy=0; |
michael@0 | 863 | for (i=0;i<frame_size;i+=4) |
michael@0 | 864 | { |
michael@0 | 865 | opus_val32 pxx=0; |
michael@0 | 866 | opus_val32 pxy=0; |
michael@0 | 867 | opus_val32 pyy=0; |
michael@0 | 868 | opus_val16 x, y; |
michael@0 | 869 | x = pcm[2*i]; |
michael@0 | 870 | y = pcm[2*i+1]; |
michael@0 | 871 | pxx = SHR32(MULT16_16(x,x),2); |
michael@0 | 872 | pxy = SHR32(MULT16_16(x,y),2); |
michael@0 | 873 | pyy = SHR32(MULT16_16(y,y),2); |
michael@0 | 874 | x = pcm[2*i+2]; |
michael@0 | 875 | y = pcm[2*i+3]; |
michael@0 | 876 | pxx += SHR32(MULT16_16(x,x),2); |
michael@0 | 877 | pxy += SHR32(MULT16_16(x,y),2); |
michael@0 | 878 | pyy += SHR32(MULT16_16(y,y),2); |
michael@0 | 879 | x = pcm[2*i+4]; |
michael@0 | 880 | y = pcm[2*i+5]; |
michael@0 | 881 | pxx += SHR32(MULT16_16(x,x),2); |
michael@0 | 882 | pxy += SHR32(MULT16_16(x,y),2); |
michael@0 | 883 | pyy += SHR32(MULT16_16(y,y),2); |
michael@0 | 884 | x = pcm[2*i+6]; |
michael@0 | 885 | y = pcm[2*i+7]; |
michael@0 | 886 | pxx += SHR32(MULT16_16(x,x),2); |
michael@0 | 887 | pxy += SHR32(MULT16_16(x,y),2); |
michael@0 | 888 | pyy += SHR32(MULT16_16(y,y),2); |
michael@0 | 889 | |
michael@0 | 890 | xx += SHR32(pxx, 10); |
michael@0 | 891 | xy += SHR32(pxy, 10); |
michael@0 | 892 | yy += SHR32(pyy, 10); |
michael@0 | 893 | } |
michael@0 | 894 | mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX); |
michael@0 | 895 | mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY); |
michael@0 | 896 | mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY); |
michael@0 | 897 | mem->XX = MAX32(0, mem->XX); |
michael@0 | 898 | mem->XY = MAX32(0, mem->XY); |
michael@0 | 899 | mem->YY = MAX32(0, mem->YY); |
michael@0 | 900 | if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18)) |
michael@0 | 901 | { |
michael@0 | 902 | sqrt_xx = celt_sqrt(mem->XX); |
michael@0 | 903 | sqrt_yy = celt_sqrt(mem->YY); |
michael@0 | 904 | qrrt_xx = celt_sqrt(sqrt_xx); |
michael@0 | 905 | qrrt_yy = celt_sqrt(sqrt_yy); |
michael@0 | 906 | /* Inter-channel correlation */ |
michael@0 | 907 | mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy); |
michael@0 | 908 | corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16); |
michael@0 | 909 | /* Approximate loudness difference */ |
michael@0 | 910 | ldiff = Q15ONE*ABS16(qrrt_xx-qrrt_yy)/(EPSILON+qrrt_xx+qrrt_yy); |
michael@0 | 911 | width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ldiff); |
michael@0 | 912 | /* Smoothing over one second */ |
michael@0 | 913 | mem->smoothed_width += (width-mem->smoothed_width)/frame_rate; |
michael@0 | 914 | /* Peak follower */ |
michael@0 | 915 | mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width); |
michael@0 | 916 | } else { |
michael@0 | 917 | width = 0; |
michael@0 | 918 | corr=Q15ONE; |
michael@0 | 919 | ldiff=0; |
michael@0 | 920 | } |
michael@0 | 921 | /*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);*/ |
michael@0 | 922 | return EXTRACT16(MIN32(Q15ONE,20*mem->max_follower)); |
michael@0 | 923 | } |
michael@0 | 924 | |
michael@0 | 925 | opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size, |
michael@0 | 926 | unsigned char *data, opus_int32 out_data_bytes, int lsb_depth, |
michael@0 | 927 | const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, int analysis_channels, downmix_func downmix) |
michael@0 | 928 | { |
michael@0 | 929 | void *silk_enc; |
michael@0 | 930 | CELTEncoder *celt_enc; |
michael@0 | 931 | int i; |
michael@0 | 932 | int ret=0; |
michael@0 | 933 | opus_int32 nBytes; |
michael@0 | 934 | ec_enc enc; |
michael@0 | 935 | int bytes_target; |
michael@0 | 936 | int prefill=0; |
michael@0 | 937 | int start_band = 0; |
michael@0 | 938 | int redundancy = 0; |
michael@0 | 939 | int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */ |
michael@0 | 940 | int celt_to_silk = 0; |
michael@0 | 941 | VARDECL(opus_val16, pcm_buf); |
michael@0 | 942 | int nb_compr_bytes; |
michael@0 | 943 | int to_celt = 0; |
michael@0 | 944 | opus_uint32 redundant_rng = 0; |
michael@0 | 945 | int cutoff_Hz, hp_freq_smth1; |
michael@0 | 946 | int voice_est; /* Probability of voice in Q7 */ |
michael@0 | 947 | opus_int32 equiv_rate; |
michael@0 | 948 | int delay_compensation; |
michael@0 | 949 | int frame_rate; |
michael@0 | 950 | opus_int32 max_rate; /* Max bitrate we're allowed to use */ |
michael@0 | 951 | int curr_bandwidth; |
michael@0 | 952 | opus_val16 HB_gain; |
michael@0 | 953 | opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ |
michael@0 | 954 | int total_buffer; |
michael@0 | 955 | opus_val16 stereo_width; |
michael@0 | 956 | const CELTMode *celt_mode; |
michael@0 | 957 | AnalysisInfo analysis_info; |
michael@0 | 958 | int analysis_read_pos_bak=-1; |
michael@0 | 959 | int analysis_read_subframe_bak=-1; |
michael@0 | 960 | VARDECL(opus_val16, tmp_prefill); |
michael@0 | 961 | |
michael@0 | 962 | ALLOC_STACK; |
michael@0 | 963 | |
michael@0 | 964 | max_data_bytes = IMIN(1276, out_data_bytes); |
michael@0 | 965 | |
michael@0 | 966 | st->rangeFinal = 0; |
michael@0 | 967 | if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs && |
michael@0 | 968 | 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size != 3*st->Fs) |
michael@0 | 969 | || (400*frame_size < st->Fs) |
michael@0 | 970 | || max_data_bytes<=0 |
michael@0 | 971 | ) |
michael@0 | 972 | { |
michael@0 | 973 | RESTORE_STACK; |
michael@0 | 974 | return OPUS_BAD_ARG; |
michael@0 | 975 | } |
michael@0 | 976 | silk_enc = (char*)st+st->silk_enc_offset; |
michael@0 | 977 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
michael@0 | 978 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
michael@0 | 979 | delay_compensation = 0; |
michael@0 | 980 | else |
michael@0 | 981 | delay_compensation = st->delay_compensation; |
michael@0 | 982 | |
michael@0 | 983 | lsb_depth = IMIN(lsb_depth, st->lsb_depth); |
michael@0 | 984 | |
michael@0 | 985 | analysis_info.valid = 0; |
michael@0 | 986 | celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); |
michael@0 | 987 | #ifndef DISABLE_FLOAT_API |
michael@0 | 988 | #ifdef FIXED_POINT |
michael@0 | 989 | if (st->silk_mode.complexity >= 10 && st->Fs==48000) |
michael@0 | 990 | #else |
michael@0 | 991 | if (st->silk_mode.complexity >= 7 && st->Fs==48000) |
michael@0 | 992 | #endif |
michael@0 | 993 | { |
michael@0 | 994 | analysis_read_pos_bak = st->analysis.read_pos; |
michael@0 | 995 | analysis_read_subframe_bak = st->analysis.read_subframe; |
michael@0 | 996 | run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size, |
michael@0 | 997 | c1, c2, analysis_channels, st->Fs, |
michael@0 | 998 | lsb_depth, downmix, &analysis_info); |
michael@0 | 999 | } |
michael@0 | 1000 | #endif |
michael@0 | 1001 | |
michael@0 | 1002 | st->voice_ratio = -1; |
michael@0 | 1003 | |
michael@0 | 1004 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1005 | st->detected_bandwidth = 0; |
michael@0 | 1006 | if (analysis_info.valid) |
michael@0 | 1007 | { |
michael@0 | 1008 | int analysis_bandwidth; |
michael@0 | 1009 | if (st->signal_type == OPUS_AUTO) |
michael@0 | 1010 | st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob)); |
michael@0 | 1011 | |
michael@0 | 1012 | analysis_bandwidth = analysis_info.bandwidth; |
michael@0 | 1013 | if (analysis_bandwidth<=12) |
michael@0 | 1014 | st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
michael@0 | 1015 | else if (analysis_bandwidth<=14) |
michael@0 | 1016 | st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
michael@0 | 1017 | else if (analysis_bandwidth<=16) |
michael@0 | 1018 | st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
michael@0 | 1019 | else if (analysis_bandwidth<=18) |
michael@0 | 1020 | st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
michael@0 | 1021 | else |
michael@0 | 1022 | st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; |
michael@0 | 1023 | } |
michael@0 | 1024 | #endif |
michael@0 | 1025 | |
michael@0 | 1026 | if (st->channels==2 && st->force_channels!=1) |
michael@0 | 1027 | stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem); |
michael@0 | 1028 | else |
michael@0 | 1029 | stereo_width = 0; |
michael@0 | 1030 | total_buffer = delay_compensation; |
michael@0 | 1031 | st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes); |
michael@0 | 1032 | |
michael@0 | 1033 | frame_rate = st->Fs/frame_size; |
michael@0 | 1034 | if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8 |
michael@0 | 1035 | || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400))) |
michael@0 | 1036 | { |
michael@0 | 1037 | /*If the space is too low to do something useful, emit 'PLC' frames.*/ |
michael@0 | 1038 | int tocmode = st->mode; |
michael@0 | 1039 | int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth; |
michael@0 | 1040 | if (tocmode==0) |
michael@0 | 1041 | tocmode = MODE_SILK_ONLY; |
michael@0 | 1042 | if (frame_rate>100) |
michael@0 | 1043 | tocmode = MODE_CELT_ONLY; |
michael@0 | 1044 | if (frame_rate < 50) |
michael@0 | 1045 | tocmode = MODE_SILK_ONLY; |
michael@0 | 1046 | if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND) |
michael@0 | 1047 | bw=OPUS_BANDWIDTH_WIDEBAND; |
michael@0 | 1048 | else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND) |
michael@0 | 1049 | bw=OPUS_BANDWIDTH_NARROWBAND; |
michael@0 | 1050 | else if (bw<=OPUS_BANDWIDTH_SUPERWIDEBAND) |
michael@0 | 1051 | bw=OPUS_BANDWIDTH_SUPERWIDEBAND; |
michael@0 | 1052 | data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels); |
michael@0 | 1053 | RESTORE_STACK; |
michael@0 | 1054 | return 1; |
michael@0 | 1055 | } |
michael@0 | 1056 | if (!st->use_vbr) |
michael@0 | 1057 | { |
michael@0 | 1058 | int cbrBytes; |
michael@0 | 1059 | cbrBytes = IMIN( (st->bitrate_bps + 4*frame_rate)/(8*frame_rate) , max_data_bytes); |
michael@0 | 1060 | st->bitrate_bps = cbrBytes * (8*frame_rate); |
michael@0 | 1061 | max_data_bytes = cbrBytes; |
michael@0 | 1062 | } |
michael@0 | 1063 | max_rate = frame_rate*max_data_bytes*8; |
michael@0 | 1064 | |
michael@0 | 1065 | /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ |
michael@0 | 1066 | equiv_rate = st->bitrate_bps - (40*st->channels+20)*(st->Fs/frame_size - 50); |
michael@0 | 1067 | |
michael@0 | 1068 | if (st->signal_type == OPUS_SIGNAL_VOICE) |
michael@0 | 1069 | voice_est = 127; |
michael@0 | 1070 | else if (st->signal_type == OPUS_SIGNAL_MUSIC) |
michael@0 | 1071 | voice_est = 0; |
michael@0 | 1072 | else if (st->voice_ratio >= 0) |
michael@0 | 1073 | { |
michael@0 | 1074 | voice_est = st->voice_ratio*327>>8; |
michael@0 | 1075 | /* For AUDIO, never be more than 90% confident of having speech */ |
michael@0 | 1076 | if (st->application == OPUS_APPLICATION_AUDIO) |
michael@0 | 1077 | voice_est = IMIN(voice_est, 115); |
michael@0 | 1078 | } else if (st->application == OPUS_APPLICATION_VOIP) |
michael@0 | 1079 | voice_est = 115; |
michael@0 | 1080 | else |
michael@0 | 1081 | voice_est = 48; |
michael@0 | 1082 | |
michael@0 | 1083 | if (st->force_channels!=OPUS_AUTO && st->channels == 2) |
michael@0 | 1084 | { |
michael@0 | 1085 | st->stream_channels = st->force_channels; |
michael@0 | 1086 | } else { |
michael@0 | 1087 | #ifdef FUZZING |
michael@0 | 1088 | /* Random mono/stereo decision */ |
michael@0 | 1089 | if (st->channels == 2 && (rand()&0x1F)==0) |
michael@0 | 1090 | st->stream_channels = 3-st->stream_channels; |
michael@0 | 1091 | #else |
michael@0 | 1092 | /* Rate-dependent mono-stereo decision */ |
michael@0 | 1093 | if (st->channels == 2) |
michael@0 | 1094 | { |
michael@0 | 1095 | opus_int32 stereo_threshold; |
michael@0 | 1096 | stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14); |
michael@0 | 1097 | if (st->stream_channels == 2) |
michael@0 | 1098 | stereo_threshold -= 1000; |
michael@0 | 1099 | else |
michael@0 | 1100 | stereo_threshold += 1000; |
michael@0 | 1101 | st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; |
michael@0 | 1102 | } else { |
michael@0 | 1103 | st->stream_channels = st->channels; |
michael@0 | 1104 | } |
michael@0 | 1105 | #endif |
michael@0 | 1106 | } |
michael@0 | 1107 | equiv_rate = st->bitrate_bps - (40*st->stream_channels+20)*(st->Fs/frame_size - 50); |
michael@0 | 1108 | |
michael@0 | 1109 | /* Mode selection depending on application and signal type */ |
michael@0 | 1110 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
michael@0 | 1111 | { |
michael@0 | 1112 | st->mode = MODE_CELT_ONLY; |
michael@0 | 1113 | } else if (st->user_forced_mode == OPUS_AUTO) |
michael@0 | 1114 | { |
michael@0 | 1115 | #ifdef FUZZING |
michael@0 | 1116 | /* Random mode switching */ |
michael@0 | 1117 | if ((rand()&0xF)==0) |
michael@0 | 1118 | { |
michael@0 | 1119 | if ((rand()&0x1)==0) |
michael@0 | 1120 | st->mode = MODE_CELT_ONLY; |
michael@0 | 1121 | else |
michael@0 | 1122 | st->mode = MODE_SILK_ONLY; |
michael@0 | 1123 | } else { |
michael@0 | 1124 | if (st->prev_mode==MODE_CELT_ONLY) |
michael@0 | 1125 | st->mode = MODE_CELT_ONLY; |
michael@0 | 1126 | else |
michael@0 | 1127 | st->mode = MODE_SILK_ONLY; |
michael@0 | 1128 | } |
michael@0 | 1129 | #else |
michael@0 | 1130 | opus_int32 mode_voice, mode_music; |
michael@0 | 1131 | opus_int32 threshold; |
michael@0 | 1132 | |
michael@0 | 1133 | /* Interpolate based on stereo width */ |
michael@0 | 1134 | mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0]) |
michael@0 | 1135 | + MULT16_32_Q15(stereo_width,mode_thresholds[1][0])); |
michael@0 | 1136 | mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1]) |
michael@0 | 1137 | + MULT16_32_Q15(stereo_width,mode_thresholds[1][1])); |
michael@0 | 1138 | /* Interpolate based on speech/music probability */ |
michael@0 | 1139 | threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14); |
michael@0 | 1140 | /* Bias towards SILK for VoIP because of some useful features */ |
michael@0 | 1141 | if (st->application == OPUS_APPLICATION_VOIP) |
michael@0 | 1142 | threshold += 8000; |
michael@0 | 1143 | |
michael@0 | 1144 | /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/ |
michael@0 | 1145 | /* Hysteresis */ |
michael@0 | 1146 | if (st->prev_mode == MODE_CELT_ONLY) |
michael@0 | 1147 | threshold -= 4000; |
michael@0 | 1148 | else if (st->prev_mode>0) |
michael@0 | 1149 | threshold += 4000; |
michael@0 | 1150 | |
michael@0 | 1151 | st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY; |
michael@0 | 1152 | |
michael@0 | 1153 | /* When FEC is enabled and there's enough packet loss, use SILK */ |
michael@0 | 1154 | if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4) |
michael@0 | 1155 | st->mode = MODE_SILK_ONLY; |
michael@0 | 1156 | /* When encoding voice and DTX is enabled, set the encoder to SILK mode (at least for now) */ |
michael@0 | 1157 | if (st->silk_mode.useDTX && voice_est > 100) |
michael@0 | 1158 | st->mode = MODE_SILK_ONLY; |
michael@0 | 1159 | #endif |
michael@0 | 1160 | } else { |
michael@0 | 1161 | st->mode = st->user_forced_mode; |
michael@0 | 1162 | } |
michael@0 | 1163 | |
michael@0 | 1164 | /* Override the chosen mode to make sure we meet the requested frame size */ |
michael@0 | 1165 | if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100) |
michael@0 | 1166 | st->mode = MODE_CELT_ONLY; |
michael@0 | 1167 | if (st->lfe) |
michael@0 | 1168 | st->mode = MODE_CELT_ONLY; |
michael@0 | 1169 | /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */ |
michael@0 | 1170 | if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8)) |
michael@0 | 1171 | st->mode = MODE_CELT_ONLY; |
michael@0 | 1172 | |
michael@0 | 1173 | if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0 |
michael@0 | 1174 | && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY) |
michael@0 | 1175 | { |
michael@0 | 1176 | /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */ |
michael@0 | 1177 | st->silk_mode.toMono = 1; |
michael@0 | 1178 | st->stream_channels = 2; |
michael@0 | 1179 | } else { |
michael@0 | 1180 | st->silk_mode.toMono = 0; |
michael@0 | 1181 | } |
michael@0 | 1182 | |
michael@0 | 1183 | if (st->prev_mode > 0 && |
michael@0 | 1184 | ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) || |
michael@0 | 1185 | (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY))) |
michael@0 | 1186 | { |
michael@0 | 1187 | redundancy = 1; |
michael@0 | 1188 | celt_to_silk = (st->mode != MODE_CELT_ONLY); |
michael@0 | 1189 | if (!celt_to_silk) |
michael@0 | 1190 | { |
michael@0 | 1191 | /* Switch to SILK/hybrid if frame size is 10 ms or more*/ |
michael@0 | 1192 | if (frame_size >= st->Fs/100) |
michael@0 | 1193 | { |
michael@0 | 1194 | st->mode = st->prev_mode; |
michael@0 | 1195 | to_celt = 1; |
michael@0 | 1196 | } else { |
michael@0 | 1197 | redundancy=0; |
michael@0 | 1198 | } |
michael@0 | 1199 | } |
michael@0 | 1200 | } |
michael@0 | 1201 | /* For the first frame at a new SILK bandwidth */ |
michael@0 | 1202 | if (st->silk_bw_switch) |
michael@0 | 1203 | { |
michael@0 | 1204 | redundancy = 1; |
michael@0 | 1205 | celt_to_silk = 1; |
michael@0 | 1206 | st->silk_bw_switch = 0; |
michael@0 | 1207 | prefill=1; |
michael@0 | 1208 | } |
michael@0 | 1209 | |
michael@0 | 1210 | if (redundancy) |
michael@0 | 1211 | { |
michael@0 | 1212 | /* Fair share of the max size allowed */ |
michael@0 | 1213 | redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(frame_size+st->Fs/200)); |
michael@0 | 1214 | /* For VBR, target the actual bitrate (subject to the limit above) */ |
michael@0 | 1215 | if (st->use_vbr) |
michael@0 | 1216 | redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600); |
michael@0 | 1217 | } |
michael@0 | 1218 | |
michael@0 | 1219 | if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) |
michael@0 | 1220 | { |
michael@0 | 1221 | silk_EncControlStruct dummy; |
michael@0 | 1222 | silk_InitEncoder( silk_enc, st->arch, &dummy); |
michael@0 | 1223 | prefill=1; |
michael@0 | 1224 | } |
michael@0 | 1225 | |
michael@0 | 1226 | /* Automatic (rate-dependent) bandwidth selection */ |
michael@0 | 1227 | if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch) |
michael@0 | 1228 | { |
michael@0 | 1229 | const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds; |
michael@0 | 1230 | opus_int32 bandwidth_thresholds[8]; |
michael@0 | 1231 | int bandwidth = OPUS_BANDWIDTH_FULLBAND; |
michael@0 | 1232 | opus_int32 equiv_rate2; |
michael@0 | 1233 | |
michael@0 | 1234 | equiv_rate2 = equiv_rate; |
michael@0 | 1235 | if (st->mode != MODE_CELT_ONLY) |
michael@0 | 1236 | { |
michael@0 | 1237 | /* Adjust the threshold +/- 10% depending on complexity */ |
michael@0 | 1238 | equiv_rate2 = equiv_rate2 * (45+st->silk_mode.complexity)/50; |
michael@0 | 1239 | /* CBR is less efficient by ~1 kb/s */ |
michael@0 | 1240 | if (!st->use_vbr) |
michael@0 | 1241 | equiv_rate2 -= 1000; |
michael@0 | 1242 | } |
michael@0 | 1243 | if (st->channels==2 && st->force_channels!=1) |
michael@0 | 1244 | { |
michael@0 | 1245 | voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds; |
michael@0 | 1246 | music_bandwidth_thresholds = stereo_music_bandwidth_thresholds; |
michael@0 | 1247 | } else { |
michael@0 | 1248 | voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds; |
michael@0 | 1249 | music_bandwidth_thresholds = mono_music_bandwidth_thresholds; |
michael@0 | 1250 | } |
michael@0 | 1251 | /* Interpolate bandwidth thresholds depending on voice estimation */ |
michael@0 | 1252 | for (i=0;i<8;i++) |
michael@0 | 1253 | { |
michael@0 | 1254 | bandwidth_thresholds[i] = music_bandwidth_thresholds[i] |
michael@0 | 1255 | + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14); |
michael@0 | 1256 | } |
michael@0 | 1257 | do { |
michael@0 | 1258 | int threshold, hysteresis; |
michael@0 | 1259 | threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)]; |
michael@0 | 1260 | hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1]; |
michael@0 | 1261 | if (!st->first) |
michael@0 | 1262 | { |
michael@0 | 1263 | if (st->bandwidth >= bandwidth) |
michael@0 | 1264 | threshold -= hysteresis; |
michael@0 | 1265 | else |
michael@0 | 1266 | threshold += hysteresis; |
michael@0 | 1267 | } |
michael@0 | 1268 | if (equiv_rate2 >= threshold) |
michael@0 | 1269 | break; |
michael@0 | 1270 | } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND); |
michael@0 | 1271 | st->bandwidth = bandwidth; |
michael@0 | 1272 | /* Prevents any transition to SWB/FB until the SILK layer has fully |
michael@0 | 1273 | switched to WB mode and turned the variable LP filter off */ |
michael@0 | 1274 | if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
michael@0 | 1275 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
michael@0 | 1276 | } |
michael@0 | 1277 | |
michael@0 | 1278 | if (st->bandwidth>st->max_bandwidth) |
michael@0 | 1279 | st->bandwidth = st->max_bandwidth; |
michael@0 | 1280 | |
michael@0 | 1281 | if (st->user_bandwidth != OPUS_AUTO) |
michael@0 | 1282 | st->bandwidth = st->user_bandwidth; |
michael@0 | 1283 | |
michael@0 | 1284 | /* This prevents us from using hybrid at unsafe CBR/max rates */ |
michael@0 | 1285 | if (st->mode != MODE_CELT_ONLY && max_rate < 15000) |
michael@0 | 1286 | { |
michael@0 | 1287 | st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND); |
michael@0 | 1288 | } |
michael@0 | 1289 | |
michael@0 | 1290 | /* Prevents Opus from wasting bits on frequencies that are above |
michael@0 | 1291 | the Nyquist rate of the input signal */ |
michael@0 | 1292 | if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND) |
michael@0 | 1293 | st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
michael@0 | 1294 | if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
michael@0 | 1295 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
michael@0 | 1296 | if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND) |
michael@0 | 1297 | st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
michael@0 | 1298 | if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND) |
michael@0 | 1299 | st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
michael@0 | 1300 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1301 | /* Use detected bandwidth to reduce the encoded bandwidth. */ |
michael@0 | 1302 | if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO) |
michael@0 | 1303 | { |
michael@0 | 1304 | int min_detected_bandwidth; |
michael@0 | 1305 | /* Makes bandwidth detection more conservative just in case the detector |
michael@0 | 1306 | gets it wrong when we could have coded a high bandwidth transparently. |
michael@0 | 1307 | When operating in SILK/hybrid mode, we don't go below wideband to avoid |
michael@0 | 1308 | more complicated switches that require redundancy. */ |
michael@0 | 1309 | if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY) |
michael@0 | 1310 | min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
michael@0 | 1311 | else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY) |
michael@0 | 1312 | min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
michael@0 | 1313 | else if (equiv_rate <= 30000*st->stream_channels) |
michael@0 | 1314 | min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
michael@0 | 1315 | else if (equiv_rate <= 44000*st->stream_channels) |
michael@0 | 1316 | min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
michael@0 | 1317 | else |
michael@0 | 1318 | min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; |
michael@0 | 1319 | |
michael@0 | 1320 | st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth); |
michael@0 | 1321 | st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth); |
michael@0 | 1322 | } |
michael@0 | 1323 | #endif |
michael@0 | 1324 | celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth)); |
michael@0 | 1325 | |
michael@0 | 1326 | /* CELT mode doesn't support mediumband, use wideband instead */ |
michael@0 | 1327 | if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
michael@0 | 1328 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
michael@0 | 1329 | if (st->lfe) |
michael@0 | 1330 | st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
michael@0 | 1331 | |
michael@0 | 1332 | /* Can't support higher than wideband for >20 ms frames */ |
michael@0 | 1333 | if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)) |
michael@0 | 1334 | { |
michael@0 | 1335 | VARDECL(unsigned char, tmp_data); |
michael@0 | 1336 | int nb_frames; |
michael@0 | 1337 | int bak_mode, bak_bandwidth, bak_channels, bak_to_mono; |
michael@0 | 1338 | VARDECL(OpusRepacketizer, rp); |
michael@0 | 1339 | opus_int32 bytes_per_frame; |
michael@0 | 1340 | opus_int32 repacketize_len; |
michael@0 | 1341 | |
michael@0 | 1342 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1343 | if (analysis_read_pos_bak!= -1) |
michael@0 | 1344 | { |
michael@0 | 1345 | st->analysis.read_pos = analysis_read_pos_bak; |
michael@0 | 1346 | st->analysis.read_subframe = analysis_read_subframe_bak; |
michael@0 | 1347 | } |
michael@0 | 1348 | #endif |
michael@0 | 1349 | |
michael@0 | 1350 | nb_frames = frame_size > st->Fs/25 ? 3 : 2; |
michael@0 | 1351 | bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames); |
michael@0 | 1352 | |
michael@0 | 1353 | ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char); |
michael@0 | 1354 | |
michael@0 | 1355 | ALLOC(rp, 1, OpusRepacketizer); |
michael@0 | 1356 | opus_repacketizer_init(rp); |
michael@0 | 1357 | |
michael@0 | 1358 | bak_mode = st->user_forced_mode; |
michael@0 | 1359 | bak_bandwidth = st->user_bandwidth; |
michael@0 | 1360 | bak_channels = st->force_channels; |
michael@0 | 1361 | |
michael@0 | 1362 | st->user_forced_mode = st->mode; |
michael@0 | 1363 | st->user_bandwidth = st->bandwidth; |
michael@0 | 1364 | st->force_channels = st->stream_channels; |
michael@0 | 1365 | bak_to_mono = st->silk_mode.toMono; |
michael@0 | 1366 | |
michael@0 | 1367 | if (bak_to_mono) |
michael@0 | 1368 | st->force_channels = 1; |
michael@0 | 1369 | else |
michael@0 | 1370 | st->prev_channels = st->stream_channels; |
michael@0 | 1371 | for (i=0;i<nb_frames;i++) |
michael@0 | 1372 | { |
michael@0 | 1373 | int tmp_len; |
michael@0 | 1374 | st->silk_mode.toMono = 0; |
michael@0 | 1375 | /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */ |
michael@0 | 1376 | if (to_celt && i==nb_frames-1) |
michael@0 | 1377 | st->user_forced_mode = MODE_CELT_ONLY; |
michael@0 | 1378 | tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50, |
michael@0 | 1379 | tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth, |
michael@0 | 1380 | NULL, 0, c1, c2, analysis_channels, downmix); |
michael@0 | 1381 | if (tmp_len<0) |
michael@0 | 1382 | { |
michael@0 | 1383 | RESTORE_STACK; |
michael@0 | 1384 | return OPUS_INTERNAL_ERROR; |
michael@0 | 1385 | } |
michael@0 | 1386 | ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len); |
michael@0 | 1387 | if (ret<0) |
michael@0 | 1388 | { |
michael@0 | 1389 | RESTORE_STACK; |
michael@0 | 1390 | return OPUS_INTERNAL_ERROR; |
michael@0 | 1391 | } |
michael@0 | 1392 | } |
michael@0 | 1393 | if (st->use_vbr) |
michael@0 | 1394 | repacketize_len = out_data_bytes; |
michael@0 | 1395 | else |
michael@0 | 1396 | repacketize_len = IMIN(3*st->bitrate_bps/(3*8*50/nb_frames), out_data_bytes); |
michael@0 | 1397 | ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr); |
michael@0 | 1398 | if (ret<0) |
michael@0 | 1399 | { |
michael@0 | 1400 | RESTORE_STACK; |
michael@0 | 1401 | return OPUS_INTERNAL_ERROR; |
michael@0 | 1402 | } |
michael@0 | 1403 | st->user_forced_mode = bak_mode; |
michael@0 | 1404 | st->user_bandwidth = bak_bandwidth; |
michael@0 | 1405 | st->force_channels = bak_channels; |
michael@0 | 1406 | st->silk_mode.toMono = bak_to_mono; |
michael@0 | 1407 | RESTORE_STACK; |
michael@0 | 1408 | return ret; |
michael@0 | 1409 | } |
michael@0 | 1410 | curr_bandwidth = st->bandwidth; |
michael@0 | 1411 | |
michael@0 | 1412 | /* Chooses the appropriate mode for speech |
michael@0 | 1413 | *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */ |
michael@0 | 1414 | if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
michael@0 | 1415 | st->mode = MODE_HYBRID; |
michael@0 | 1416 | if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) |
michael@0 | 1417 | st->mode = MODE_SILK_ONLY; |
michael@0 | 1418 | |
michael@0 | 1419 | /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */ |
michael@0 | 1420 | bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1; |
michael@0 | 1421 | |
michael@0 | 1422 | data += 1; |
michael@0 | 1423 | |
michael@0 | 1424 | ec_enc_init(&enc, data, max_data_bytes-1); |
michael@0 | 1425 | |
michael@0 | 1426 | ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16); |
michael@0 | 1427 | for (i=0;i<total_buffer*st->channels;i++) |
michael@0 | 1428 | pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels+i]; |
michael@0 | 1429 | |
michael@0 | 1430 | if (st->mode == MODE_CELT_ONLY) |
michael@0 | 1431 | hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
michael@0 | 1432 | else |
michael@0 | 1433 | hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15; |
michael@0 | 1434 | |
michael@0 | 1435 | st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, |
michael@0 | 1436 | hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) ); |
michael@0 | 1437 | |
michael@0 | 1438 | /* convert from log scale to Hertz */ |
michael@0 | 1439 | cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); |
michael@0 | 1440 | |
michael@0 | 1441 | if (st->application == OPUS_APPLICATION_VOIP) |
michael@0 | 1442 | { |
michael@0 | 1443 | hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); |
michael@0 | 1444 | } else { |
michael@0 | 1445 | dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); |
michael@0 | 1446 | } |
michael@0 | 1447 | |
michael@0 | 1448 | |
michael@0 | 1449 | |
michael@0 | 1450 | /* SILK processing */ |
michael@0 | 1451 | HB_gain = Q15ONE; |
michael@0 | 1452 | if (st->mode != MODE_CELT_ONLY) |
michael@0 | 1453 | { |
michael@0 | 1454 | opus_int32 total_bitRate, celt_rate; |
michael@0 | 1455 | #ifdef FIXED_POINT |
michael@0 | 1456 | const opus_int16 *pcm_silk; |
michael@0 | 1457 | #else |
michael@0 | 1458 | VARDECL(opus_int16, pcm_silk); |
michael@0 | 1459 | ALLOC(pcm_silk, st->channels*frame_size, opus_int16); |
michael@0 | 1460 | #endif |
michael@0 | 1461 | |
michael@0 | 1462 | /* Distribute bits between SILK and CELT */ |
michael@0 | 1463 | total_bitRate = 8 * bytes_target * frame_rate; |
michael@0 | 1464 | if( st->mode == MODE_HYBRID ) { |
michael@0 | 1465 | int HB_gain_ref; |
michael@0 | 1466 | /* Base rate for SILK */ |
michael@0 | 1467 | st->silk_mode.bitRate = st->stream_channels * ( 5000 + 1000 * ( st->Fs == 100 * frame_size ) ); |
michael@0 | 1468 | if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) { |
michael@0 | 1469 | /* SILK gets 2/3 of the remaining bits */ |
michael@0 | 1470 | st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 2 / 3; |
michael@0 | 1471 | } else { /* FULLBAND */ |
michael@0 | 1472 | /* SILK gets 3/5 of the remaining bits */ |
michael@0 | 1473 | st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 3 / 5; |
michael@0 | 1474 | } |
michael@0 | 1475 | /* Don't let SILK use more than 80% */ |
michael@0 | 1476 | if( st->silk_mode.bitRate > total_bitRate * 4/5 ) { |
michael@0 | 1477 | st->silk_mode.bitRate = total_bitRate * 4/5; |
michael@0 | 1478 | } |
michael@0 | 1479 | if (!st->energy_masking) |
michael@0 | 1480 | { |
michael@0 | 1481 | /* Increasingly attenuate high band when it gets allocated fewer bits */ |
michael@0 | 1482 | celt_rate = total_bitRate - st->silk_mode.bitRate; |
michael@0 | 1483 | HB_gain_ref = (curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND) ? 3000 : 3600; |
michael@0 | 1484 | HB_gain = SHL32((opus_val32)celt_rate, 9) / SHR32((opus_val32)celt_rate + st->stream_channels * HB_gain_ref, 6); |
michael@0 | 1485 | HB_gain = HB_gain < Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE; |
michael@0 | 1486 | } |
michael@0 | 1487 | } else { |
michael@0 | 1488 | /* SILK gets all bits */ |
michael@0 | 1489 | st->silk_mode.bitRate = total_bitRate; |
michael@0 | 1490 | } |
michael@0 | 1491 | |
michael@0 | 1492 | /* Surround masking for SILK */ |
michael@0 | 1493 | if (st->energy_masking && st->use_vbr && !st->lfe) |
michael@0 | 1494 | { |
michael@0 | 1495 | opus_val32 mask_sum=0; |
michael@0 | 1496 | opus_val16 masking_depth; |
michael@0 | 1497 | opus_int32 rate_offset; |
michael@0 | 1498 | int c; |
michael@0 | 1499 | int end = 17; |
michael@0 | 1500 | opus_int16 srate = 16000; |
michael@0 | 1501 | if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND) |
michael@0 | 1502 | { |
michael@0 | 1503 | end = 13; |
michael@0 | 1504 | srate = 8000; |
michael@0 | 1505 | } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
michael@0 | 1506 | { |
michael@0 | 1507 | end = 15; |
michael@0 | 1508 | srate = 12000; |
michael@0 | 1509 | } |
michael@0 | 1510 | for (c=0;c<st->channels;c++) |
michael@0 | 1511 | { |
michael@0 | 1512 | for(i=0;i<end;i++) |
michael@0 | 1513 | { |
michael@0 | 1514 | opus_val16 mask; |
michael@0 | 1515 | mask = MAX16(MIN16(st->energy_masking[21*c+i], |
michael@0 | 1516 | QCONST16(.5f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); |
michael@0 | 1517 | if (mask > 0) |
michael@0 | 1518 | mask = HALF16(mask); |
michael@0 | 1519 | mask_sum += mask; |
michael@0 | 1520 | } |
michael@0 | 1521 | } |
michael@0 | 1522 | /* Conservative rate reduction, we cut the masking in half */ |
michael@0 | 1523 | masking_depth = mask_sum / end*st->channels; |
michael@0 | 1524 | masking_depth += QCONST16(.2f, DB_SHIFT); |
michael@0 | 1525 | rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT); |
michael@0 | 1526 | rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3); |
michael@0 | 1527 | /* Split the rate change between the SILK and CELT part for hybrid. */ |
michael@0 | 1528 | if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND) |
michael@0 | 1529 | st->silk_mode.bitRate += 3*rate_offset/5; |
michael@0 | 1530 | else |
michael@0 | 1531 | st->silk_mode.bitRate += rate_offset; |
michael@0 | 1532 | bytes_target += rate_offset * frame_size / (8 * st->Fs); |
michael@0 | 1533 | } |
michael@0 | 1534 | |
michael@0 | 1535 | st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; |
michael@0 | 1536 | st->silk_mode.nChannelsAPI = st->channels; |
michael@0 | 1537 | st->silk_mode.nChannelsInternal = st->stream_channels; |
michael@0 | 1538 | if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { |
michael@0 | 1539 | st->silk_mode.desiredInternalSampleRate = 8000; |
michael@0 | 1540 | } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { |
michael@0 | 1541 | st->silk_mode.desiredInternalSampleRate = 12000; |
michael@0 | 1542 | } else { |
michael@0 | 1543 | silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND ); |
michael@0 | 1544 | st->silk_mode.desiredInternalSampleRate = 16000; |
michael@0 | 1545 | } |
michael@0 | 1546 | if( st->mode == MODE_HYBRID ) { |
michael@0 | 1547 | /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */ |
michael@0 | 1548 | st->silk_mode.minInternalSampleRate = 16000; |
michael@0 | 1549 | } else { |
michael@0 | 1550 | st->silk_mode.minInternalSampleRate = 8000; |
michael@0 | 1551 | } |
michael@0 | 1552 | |
michael@0 | 1553 | if (st->mode == MODE_SILK_ONLY) |
michael@0 | 1554 | { |
michael@0 | 1555 | opus_int32 effective_max_rate = max_rate; |
michael@0 | 1556 | st->silk_mode.maxInternalSampleRate = 16000; |
michael@0 | 1557 | if (frame_rate > 50) |
michael@0 | 1558 | effective_max_rate = effective_max_rate*2/3; |
michael@0 | 1559 | if (effective_max_rate < 13000) |
michael@0 | 1560 | { |
michael@0 | 1561 | st->silk_mode.maxInternalSampleRate = 12000; |
michael@0 | 1562 | st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate); |
michael@0 | 1563 | } |
michael@0 | 1564 | if (effective_max_rate < 9600) |
michael@0 | 1565 | { |
michael@0 | 1566 | st->silk_mode.maxInternalSampleRate = 8000; |
michael@0 | 1567 | st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate); |
michael@0 | 1568 | } |
michael@0 | 1569 | } else { |
michael@0 | 1570 | st->silk_mode.maxInternalSampleRate = 16000; |
michael@0 | 1571 | } |
michael@0 | 1572 | |
michael@0 | 1573 | st->silk_mode.useCBR = !st->use_vbr; |
michael@0 | 1574 | |
michael@0 | 1575 | /* Call SILK encoder for the low band */ |
michael@0 | 1576 | nBytes = IMIN(1275, max_data_bytes-1-redundancy_bytes); |
michael@0 | 1577 | |
michael@0 | 1578 | st->silk_mode.maxBits = nBytes*8; |
michael@0 | 1579 | /* Only allow up to 90% of the bits for hybrid mode*/ |
michael@0 | 1580 | if (st->mode == MODE_HYBRID) |
michael@0 | 1581 | st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10; |
michael@0 | 1582 | if (st->silk_mode.useCBR) |
michael@0 | 1583 | { |
michael@0 | 1584 | st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8; |
michael@0 | 1585 | /* Reduce the initial target to make it easier to reach the CBR rate */ |
michael@0 | 1586 | st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000); |
michael@0 | 1587 | } |
michael@0 | 1588 | |
michael@0 | 1589 | if (prefill) |
michael@0 | 1590 | { |
michael@0 | 1591 | opus_int32 zero=0; |
michael@0 | 1592 | int prefill_offset; |
michael@0 | 1593 | /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode |
michael@0 | 1594 | a discontinuity. The exact location is what we need to avoid leaving any "gap" |
michael@0 | 1595 | in the audio when mixing with the redundant CELT frame. Here we can afford to |
michael@0 | 1596 | overwrite st->delay_buffer because the only thing that uses it before it gets |
michael@0 | 1597 | rewritten is tmp_prefill[] and even then only the part after the ramp really |
michael@0 | 1598 | gets used (rather than sent to the encoder and discarded) */ |
michael@0 | 1599 | prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400); |
michael@0 | 1600 | gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset, |
michael@0 | 1601 | 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs); |
michael@0 | 1602 | for(i=0;i<prefill_offset;i++) |
michael@0 | 1603 | st->delay_buffer[i]=0; |
michael@0 | 1604 | #ifdef FIXED_POINT |
michael@0 | 1605 | pcm_silk = st->delay_buffer; |
michael@0 | 1606 | #else |
michael@0 | 1607 | for (i=0;i<st->encoder_buffer*st->channels;i++) |
michael@0 | 1608 | pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]); |
michael@0 | 1609 | #endif |
michael@0 | 1610 | silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 ); |
michael@0 | 1611 | } |
michael@0 | 1612 | |
michael@0 | 1613 | #ifdef FIXED_POINT |
michael@0 | 1614 | pcm_silk = pcm_buf+total_buffer*st->channels; |
michael@0 | 1615 | #else |
michael@0 | 1616 | for (i=0;i<frame_size*st->channels;i++) |
michael@0 | 1617 | pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]); |
michael@0 | 1618 | #endif |
michael@0 | 1619 | ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 ); |
michael@0 | 1620 | if( ret ) { |
michael@0 | 1621 | /*fprintf (stderr, "SILK encode error: %d\n", ret);*/ |
michael@0 | 1622 | /* Handle error */ |
michael@0 | 1623 | RESTORE_STACK; |
michael@0 | 1624 | return OPUS_INTERNAL_ERROR; |
michael@0 | 1625 | } |
michael@0 | 1626 | if (nBytes==0) |
michael@0 | 1627 | { |
michael@0 | 1628 | st->rangeFinal = 0; |
michael@0 | 1629 | data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); |
michael@0 | 1630 | RESTORE_STACK; |
michael@0 | 1631 | return 1; |
michael@0 | 1632 | } |
michael@0 | 1633 | /* Extract SILK internal bandwidth for signaling in first byte */ |
michael@0 | 1634 | if( st->mode == MODE_SILK_ONLY ) { |
michael@0 | 1635 | if( st->silk_mode.internalSampleRate == 8000 ) { |
michael@0 | 1636 | curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
michael@0 | 1637 | } else if( st->silk_mode.internalSampleRate == 12000 ) { |
michael@0 | 1638 | curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
michael@0 | 1639 | } else if( st->silk_mode.internalSampleRate == 16000 ) { |
michael@0 | 1640 | curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
michael@0 | 1641 | } |
michael@0 | 1642 | } else { |
michael@0 | 1643 | silk_assert( st->silk_mode.internalSampleRate == 16000 ); |
michael@0 | 1644 | } |
michael@0 | 1645 | |
michael@0 | 1646 | st->silk_mode.opusCanSwitch = st->silk_mode.switchReady; |
michael@0 | 1647 | /* FIXME: How do we allocate the redundancy for CBR? */ |
michael@0 | 1648 | if (st->silk_mode.opusCanSwitch) |
michael@0 | 1649 | { |
michael@0 | 1650 | redundancy = 1; |
michael@0 | 1651 | celt_to_silk = 0; |
michael@0 | 1652 | st->silk_bw_switch = 1; |
michael@0 | 1653 | } |
michael@0 | 1654 | } |
michael@0 | 1655 | |
michael@0 | 1656 | /* CELT processing */ |
michael@0 | 1657 | { |
michael@0 | 1658 | int endband=21; |
michael@0 | 1659 | |
michael@0 | 1660 | switch(curr_bandwidth) |
michael@0 | 1661 | { |
michael@0 | 1662 | case OPUS_BANDWIDTH_NARROWBAND: |
michael@0 | 1663 | endband = 13; |
michael@0 | 1664 | break; |
michael@0 | 1665 | case OPUS_BANDWIDTH_MEDIUMBAND: |
michael@0 | 1666 | case OPUS_BANDWIDTH_WIDEBAND: |
michael@0 | 1667 | endband = 17; |
michael@0 | 1668 | break; |
michael@0 | 1669 | case OPUS_BANDWIDTH_SUPERWIDEBAND: |
michael@0 | 1670 | endband = 19; |
michael@0 | 1671 | break; |
michael@0 | 1672 | case OPUS_BANDWIDTH_FULLBAND: |
michael@0 | 1673 | endband = 21; |
michael@0 | 1674 | break; |
michael@0 | 1675 | } |
michael@0 | 1676 | celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband)); |
michael@0 | 1677 | celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels)); |
michael@0 | 1678 | } |
michael@0 | 1679 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); |
michael@0 | 1680 | if (st->mode != MODE_SILK_ONLY) |
michael@0 | 1681 | { |
michael@0 | 1682 | opus_val32 celt_pred=2; |
michael@0 | 1683 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); |
michael@0 | 1684 | /* We may still decide to disable prediction later */ |
michael@0 | 1685 | if (st->silk_mode.reducedDependency) |
michael@0 | 1686 | celt_pred = 0; |
michael@0 | 1687 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred)); |
michael@0 | 1688 | |
michael@0 | 1689 | if (st->mode == MODE_HYBRID) |
michael@0 | 1690 | { |
michael@0 | 1691 | int len; |
michael@0 | 1692 | |
michael@0 | 1693 | len = (ec_tell(&enc)+7)>>3; |
michael@0 | 1694 | if (redundancy) |
michael@0 | 1695 | len += st->mode == MODE_HYBRID ? 3 : 1; |
michael@0 | 1696 | if( st->use_vbr ) { |
michael@0 | 1697 | nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * frame_size) / (8 * st->Fs); |
michael@0 | 1698 | } else { |
michael@0 | 1699 | /* check if SILK used up too much */ |
michael@0 | 1700 | nb_compr_bytes = len > bytes_target ? len : bytes_target; |
michael@0 | 1701 | } |
michael@0 | 1702 | } else { |
michael@0 | 1703 | if (st->use_vbr) |
michael@0 | 1704 | { |
michael@0 | 1705 | opus_int32 bonus=0; |
michael@0 | 1706 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1707 | if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != st->Fs/50) |
michael@0 | 1708 | { |
michael@0 | 1709 | bonus = (60*st->stream_channels+40)*(st->Fs/frame_size-50); |
michael@0 | 1710 | if (analysis_info.valid) |
michael@0 | 1711 | bonus = (opus_int32)(bonus*(1.f+.5f*analysis_info.tonality)); |
michael@0 | 1712 | } |
michael@0 | 1713 | #endif |
michael@0 | 1714 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); |
michael@0 | 1715 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint)); |
michael@0 | 1716 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps+bonus)); |
michael@0 | 1717 | nb_compr_bytes = max_data_bytes-1-redundancy_bytes; |
michael@0 | 1718 | } else { |
michael@0 | 1719 | nb_compr_bytes = bytes_target; |
michael@0 | 1720 | } |
michael@0 | 1721 | } |
michael@0 | 1722 | |
michael@0 | 1723 | } else { |
michael@0 | 1724 | nb_compr_bytes = 0; |
michael@0 | 1725 | } |
michael@0 | 1726 | |
michael@0 | 1727 | ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16); |
michael@0 | 1728 | if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0) |
michael@0 | 1729 | { |
michael@0 | 1730 | for (i=0;i<st->channels*st->Fs/400;i++) |
michael@0 | 1731 | tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels + i]; |
michael@0 | 1732 | } |
michael@0 | 1733 | |
michael@0 | 1734 | for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+total_buffer));i++) |
michael@0 | 1735 | st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size]; |
michael@0 | 1736 | for (;i<st->encoder_buffer*st->channels;i++) |
michael@0 | 1737 | st->delay_buffer[i] = pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels+i]; |
michael@0 | 1738 | |
michael@0 | 1739 | /* gain_fade() and stereo_fade() need to be after the buffer copying |
michael@0 | 1740 | because we don't want any of this to affect the SILK part */ |
michael@0 | 1741 | if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) { |
michael@0 | 1742 | gain_fade(pcm_buf, pcm_buf, |
michael@0 | 1743 | st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs); |
michael@0 | 1744 | } |
michael@0 | 1745 | st->prev_HB_gain = HB_gain; |
michael@0 | 1746 | if (st->mode != MODE_HYBRID || st->stream_channels==1) |
michael@0 | 1747 | st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-30000)); |
michael@0 | 1748 | if( !st->energy_masking && st->channels == 2 ) { |
michael@0 | 1749 | /* Apply stereo width reduction (at low bitrates) */ |
michael@0 | 1750 | if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) { |
michael@0 | 1751 | opus_val16 g1, g2; |
michael@0 | 1752 | g1 = st->hybrid_stereo_width_Q14; |
michael@0 | 1753 | g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14); |
michael@0 | 1754 | #ifdef FIXED_POINT |
michael@0 | 1755 | g1 = g1==16384 ? Q15ONE : SHL16(g1,1); |
michael@0 | 1756 | g2 = g2==16384 ? Q15ONE : SHL16(g2,1); |
michael@0 | 1757 | #else |
michael@0 | 1758 | g1 *= (1.f/16384); |
michael@0 | 1759 | g2 *= (1.f/16384); |
michael@0 | 1760 | #endif |
michael@0 | 1761 | stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, |
michael@0 | 1762 | frame_size, st->channels, celt_mode->window, st->Fs); |
michael@0 | 1763 | st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14; |
michael@0 | 1764 | } |
michael@0 | 1765 | } |
michael@0 | 1766 | |
michael@0 | 1767 | if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1)) |
michael@0 | 1768 | { |
michael@0 | 1769 | /* For SILK mode, the redundancy is inferred from the length */ |
michael@0 | 1770 | if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_compr_bytes)) |
michael@0 | 1771 | ec_enc_bit_logp(&enc, redundancy, 12); |
michael@0 | 1772 | if (redundancy) |
michael@0 | 1773 | { |
michael@0 | 1774 | int max_redundancy; |
michael@0 | 1775 | ec_enc_bit_logp(&enc, celt_to_silk, 1); |
michael@0 | 1776 | if (st->mode == MODE_HYBRID) |
michael@0 | 1777 | max_redundancy = (max_data_bytes-1)-nb_compr_bytes; |
michael@0 | 1778 | else |
michael@0 | 1779 | max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3); |
michael@0 | 1780 | /* Target the same bit-rate for redundancy as for the rest, |
michael@0 | 1781 | up to a max of 257 bytes */ |
michael@0 | 1782 | redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); |
michael@0 | 1783 | redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); |
michael@0 | 1784 | if (st->mode == MODE_HYBRID) |
michael@0 | 1785 | ec_enc_uint(&enc, redundancy_bytes-2, 256); |
michael@0 | 1786 | } |
michael@0 | 1787 | } else { |
michael@0 | 1788 | redundancy = 0; |
michael@0 | 1789 | } |
michael@0 | 1790 | |
michael@0 | 1791 | if (!redundancy) |
michael@0 | 1792 | { |
michael@0 | 1793 | st->silk_bw_switch = 0; |
michael@0 | 1794 | redundancy_bytes = 0; |
michael@0 | 1795 | } |
michael@0 | 1796 | if (st->mode != MODE_CELT_ONLY)start_band=17; |
michael@0 | 1797 | |
michael@0 | 1798 | if (st->mode == MODE_SILK_ONLY) |
michael@0 | 1799 | { |
michael@0 | 1800 | ret = (ec_tell(&enc)+7)>>3; |
michael@0 | 1801 | ec_enc_done(&enc); |
michael@0 | 1802 | nb_compr_bytes = ret; |
michael@0 | 1803 | } else { |
michael@0 | 1804 | nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes); |
michael@0 | 1805 | ec_enc_shrink(&enc, nb_compr_bytes); |
michael@0 | 1806 | } |
michael@0 | 1807 | |
michael@0 | 1808 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1809 | if (redundancy || st->mode != MODE_SILK_ONLY) |
michael@0 | 1810 | celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info)); |
michael@0 | 1811 | #endif |
michael@0 | 1812 | |
michael@0 | 1813 | /* 5 ms redundant frame for CELT->SILK */ |
michael@0 | 1814 | if (redundancy && celt_to_silk) |
michael@0 | 1815 | { |
michael@0 | 1816 | int err; |
michael@0 | 1817 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); |
michael@0 | 1818 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); |
michael@0 | 1819 | err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL); |
michael@0 | 1820 | if (err < 0) |
michael@0 | 1821 | { |
michael@0 | 1822 | RESTORE_STACK; |
michael@0 | 1823 | return OPUS_INTERNAL_ERROR; |
michael@0 | 1824 | } |
michael@0 | 1825 | celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); |
michael@0 | 1826 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
michael@0 | 1827 | } |
michael@0 | 1828 | |
michael@0 | 1829 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band)); |
michael@0 | 1830 | |
michael@0 | 1831 | if (st->mode != MODE_SILK_ONLY) |
michael@0 | 1832 | { |
michael@0 | 1833 | if (st->mode != st->prev_mode && st->prev_mode > 0) |
michael@0 | 1834 | { |
michael@0 | 1835 | unsigned char dummy[2]; |
michael@0 | 1836 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
michael@0 | 1837 | |
michael@0 | 1838 | /* Prefilling */ |
michael@0 | 1839 | celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL); |
michael@0 | 1840 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); |
michael@0 | 1841 | } |
michael@0 | 1842 | /* If false, we already busted the budget and we'll end up with a "PLC packet" */ |
michael@0 | 1843 | if (ec_tell(&enc) <= 8*nb_compr_bytes) |
michael@0 | 1844 | { |
michael@0 | 1845 | ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc); |
michael@0 | 1846 | if (ret < 0) |
michael@0 | 1847 | { |
michael@0 | 1848 | RESTORE_STACK; |
michael@0 | 1849 | return OPUS_INTERNAL_ERROR; |
michael@0 | 1850 | } |
michael@0 | 1851 | } |
michael@0 | 1852 | } |
michael@0 | 1853 | |
michael@0 | 1854 | /* 5 ms redundant frame for SILK->CELT */ |
michael@0 | 1855 | if (redundancy && !celt_to_silk) |
michael@0 | 1856 | { |
michael@0 | 1857 | int err; |
michael@0 | 1858 | unsigned char dummy[2]; |
michael@0 | 1859 | int N2, N4; |
michael@0 | 1860 | N2 = st->Fs/200; |
michael@0 | 1861 | N4 = st->Fs/400; |
michael@0 | 1862 | |
michael@0 | 1863 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
michael@0 | 1864 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); |
michael@0 | 1865 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); |
michael@0 | 1866 | |
michael@0 | 1867 | /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */ |
michael@0 | 1868 | celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL); |
michael@0 | 1869 | |
michael@0 | 1870 | err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL); |
michael@0 | 1871 | if (err < 0) |
michael@0 | 1872 | { |
michael@0 | 1873 | RESTORE_STACK; |
michael@0 | 1874 | return OPUS_INTERNAL_ERROR; |
michael@0 | 1875 | } |
michael@0 | 1876 | celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); |
michael@0 | 1877 | } |
michael@0 | 1878 | |
michael@0 | 1879 | |
michael@0 | 1880 | |
michael@0 | 1881 | /* Signalling the mode in the first byte */ |
michael@0 | 1882 | data--; |
michael@0 | 1883 | data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); |
michael@0 | 1884 | |
michael@0 | 1885 | st->rangeFinal = enc.rng ^ redundant_rng; |
michael@0 | 1886 | |
michael@0 | 1887 | if (to_celt) |
michael@0 | 1888 | st->prev_mode = MODE_CELT_ONLY; |
michael@0 | 1889 | else |
michael@0 | 1890 | st->prev_mode = st->mode; |
michael@0 | 1891 | st->prev_channels = st->stream_channels; |
michael@0 | 1892 | st->prev_framesize = frame_size; |
michael@0 | 1893 | |
michael@0 | 1894 | st->first = 0; |
michael@0 | 1895 | |
michael@0 | 1896 | /* In the unlikely case that the SILK encoder busted its target, tell |
michael@0 | 1897 | the decoder to call the PLC */ |
michael@0 | 1898 | if (ec_tell(&enc) > (max_data_bytes-1)*8) |
michael@0 | 1899 | { |
michael@0 | 1900 | if (max_data_bytes < 2) |
michael@0 | 1901 | { |
michael@0 | 1902 | RESTORE_STACK; |
michael@0 | 1903 | return OPUS_BUFFER_TOO_SMALL; |
michael@0 | 1904 | } |
michael@0 | 1905 | data[1] = 0; |
michael@0 | 1906 | ret = 1; |
michael@0 | 1907 | st->rangeFinal = 0; |
michael@0 | 1908 | } else if (st->mode==MODE_SILK_ONLY&&!redundancy) |
michael@0 | 1909 | { |
michael@0 | 1910 | /*When in LPC only mode it's perfectly |
michael@0 | 1911 | reasonable to strip off trailing zero bytes as |
michael@0 | 1912 | the required range decoder behavior is to |
michael@0 | 1913 | fill these in. This can't be done when the MDCT |
michael@0 | 1914 | modes are used because the decoder needs to know |
michael@0 | 1915 | the actual length for allocation purposes.*/ |
michael@0 | 1916 | while(ret>2&&data[ret]==0)ret--; |
michael@0 | 1917 | } |
michael@0 | 1918 | /* Count ToC and redundancy */ |
michael@0 | 1919 | ret += 1+redundancy_bytes; |
michael@0 | 1920 | if (!st->use_vbr) |
michael@0 | 1921 | { |
michael@0 | 1922 | if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK) |
michael@0 | 1923 | |
michael@0 | 1924 | { |
michael@0 | 1925 | RESTORE_STACK; |
michael@0 | 1926 | return OPUS_INTERNAL_ERROR; |
michael@0 | 1927 | } |
michael@0 | 1928 | ret = max_data_bytes; |
michael@0 | 1929 | } |
michael@0 | 1930 | RESTORE_STACK; |
michael@0 | 1931 | return ret; |
michael@0 | 1932 | } |
michael@0 | 1933 | |
michael@0 | 1934 | #ifdef FIXED_POINT |
michael@0 | 1935 | |
michael@0 | 1936 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1937 | opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, |
michael@0 | 1938 | unsigned char *data, opus_int32 max_data_bytes) |
michael@0 | 1939 | { |
michael@0 | 1940 | int i, ret; |
michael@0 | 1941 | int frame_size; |
michael@0 | 1942 | int delay_compensation; |
michael@0 | 1943 | VARDECL(opus_int16, in); |
michael@0 | 1944 | ALLOC_STACK; |
michael@0 | 1945 | |
michael@0 | 1946 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
michael@0 | 1947 | delay_compensation = 0; |
michael@0 | 1948 | else |
michael@0 | 1949 | delay_compensation = st->delay_compensation; |
michael@0 | 1950 | frame_size = compute_frame_size(pcm, analysis_frame_size, |
michael@0 | 1951 | st->variable_duration, st->channels, st->Fs, st->bitrate_bps, |
michael@0 | 1952 | delay_compensation, downmix_float, st->analysis.subframe_mem); |
michael@0 | 1953 | |
michael@0 | 1954 | ALLOC(in, frame_size*st->channels, opus_int16); |
michael@0 | 1955 | |
michael@0 | 1956 | for (i=0;i<frame_size*st->channels;i++) |
michael@0 | 1957 | in[i] = FLOAT2INT16(pcm[i]); |
michael@0 | 1958 | ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_float); |
michael@0 | 1959 | RESTORE_STACK; |
michael@0 | 1960 | return ret; |
michael@0 | 1961 | } |
michael@0 | 1962 | #endif |
michael@0 | 1963 | |
michael@0 | 1964 | opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, |
michael@0 | 1965 | unsigned char *data, opus_int32 out_data_bytes) |
michael@0 | 1966 | { |
michael@0 | 1967 | int frame_size; |
michael@0 | 1968 | int delay_compensation; |
michael@0 | 1969 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
michael@0 | 1970 | delay_compensation = 0; |
michael@0 | 1971 | else |
michael@0 | 1972 | delay_compensation = st->delay_compensation; |
michael@0 | 1973 | frame_size = compute_frame_size(pcm, analysis_frame_size, |
michael@0 | 1974 | st->variable_duration, st->channels, st->Fs, st->bitrate_bps, |
michael@0 | 1975 | delay_compensation, downmix_int |
michael@0 | 1976 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1977 | , st->analysis.subframe_mem |
michael@0 | 1978 | #endif |
michael@0 | 1979 | ); |
michael@0 | 1980 | return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int); |
michael@0 | 1981 | } |
michael@0 | 1982 | |
michael@0 | 1983 | #else |
michael@0 | 1984 | opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, |
michael@0 | 1985 | unsigned char *data, opus_int32 max_data_bytes) |
michael@0 | 1986 | { |
michael@0 | 1987 | int i, ret; |
michael@0 | 1988 | int frame_size; |
michael@0 | 1989 | int delay_compensation; |
michael@0 | 1990 | VARDECL(float, in); |
michael@0 | 1991 | ALLOC_STACK; |
michael@0 | 1992 | |
michael@0 | 1993 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
michael@0 | 1994 | delay_compensation = 0; |
michael@0 | 1995 | else |
michael@0 | 1996 | delay_compensation = st->delay_compensation; |
michael@0 | 1997 | frame_size = compute_frame_size(pcm, analysis_frame_size, |
michael@0 | 1998 | st->variable_duration, st->channels, st->Fs, st->bitrate_bps, |
michael@0 | 1999 | delay_compensation, downmix_int, st->analysis.subframe_mem); |
michael@0 | 2000 | |
michael@0 | 2001 | ALLOC(in, frame_size*st->channels, float); |
michael@0 | 2002 | |
michael@0 | 2003 | for (i=0;i<frame_size*st->channels;i++) |
michael@0 | 2004 | in[i] = (1.0f/32768)*pcm[i]; |
michael@0 | 2005 | ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int); |
michael@0 | 2006 | RESTORE_STACK; |
michael@0 | 2007 | return ret; |
michael@0 | 2008 | } |
michael@0 | 2009 | opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, |
michael@0 | 2010 | unsigned char *data, opus_int32 out_data_bytes) |
michael@0 | 2011 | { |
michael@0 | 2012 | int frame_size; |
michael@0 | 2013 | int delay_compensation; |
michael@0 | 2014 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
michael@0 | 2015 | delay_compensation = 0; |
michael@0 | 2016 | else |
michael@0 | 2017 | delay_compensation = st->delay_compensation; |
michael@0 | 2018 | frame_size = compute_frame_size(pcm, analysis_frame_size, |
michael@0 | 2019 | st->variable_duration, st->channels, st->Fs, st->bitrate_bps, |
michael@0 | 2020 | delay_compensation, downmix_float, st->analysis.subframe_mem); |
michael@0 | 2021 | return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24, |
michael@0 | 2022 | pcm, analysis_frame_size, 0, -2, st->channels, downmix_float); |
michael@0 | 2023 | } |
michael@0 | 2024 | #endif |
michael@0 | 2025 | |
michael@0 | 2026 | |
michael@0 | 2027 | int opus_encoder_ctl(OpusEncoder *st, int request, ...) |
michael@0 | 2028 | { |
michael@0 | 2029 | int ret; |
michael@0 | 2030 | CELTEncoder *celt_enc; |
michael@0 | 2031 | va_list ap; |
michael@0 | 2032 | |
michael@0 | 2033 | ret = OPUS_OK; |
michael@0 | 2034 | va_start(ap, request); |
michael@0 | 2035 | |
michael@0 | 2036 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
michael@0 | 2037 | |
michael@0 | 2038 | switch (request) |
michael@0 | 2039 | { |
michael@0 | 2040 | case OPUS_SET_APPLICATION_REQUEST: |
michael@0 | 2041 | { |
michael@0 | 2042 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2043 | if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO |
michael@0 | 2044 | && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
michael@0 | 2045 | || (!st->first && st->application != value)) |
michael@0 | 2046 | { |
michael@0 | 2047 | ret = OPUS_BAD_ARG; |
michael@0 | 2048 | break; |
michael@0 | 2049 | } |
michael@0 | 2050 | st->application = value; |
michael@0 | 2051 | } |
michael@0 | 2052 | break; |
michael@0 | 2053 | case OPUS_GET_APPLICATION_REQUEST: |
michael@0 | 2054 | { |
michael@0 | 2055 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2056 | if (!value) |
michael@0 | 2057 | { |
michael@0 | 2058 | goto bad_arg; |
michael@0 | 2059 | } |
michael@0 | 2060 | *value = st->application; |
michael@0 | 2061 | } |
michael@0 | 2062 | break; |
michael@0 | 2063 | case OPUS_SET_BITRATE_REQUEST: |
michael@0 | 2064 | { |
michael@0 | 2065 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2066 | if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) |
michael@0 | 2067 | { |
michael@0 | 2068 | if (value <= 0) |
michael@0 | 2069 | goto bad_arg; |
michael@0 | 2070 | else if (value <= 500) |
michael@0 | 2071 | value = 500; |
michael@0 | 2072 | else if (value > (opus_int32)300000*st->channels) |
michael@0 | 2073 | value = (opus_int32)300000*st->channels; |
michael@0 | 2074 | } |
michael@0 | 2075 | st->user_bitrate_bps = value; |
michael@0 | 2076 | } |
michael@0 | 2077 | break; |
michael@0 | 2078 | case OPUS_GET_BITRATE_REQUEST: |
michael@0 | 2079 | { |
michael@0 | 2080 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2081 | if (!value) |
michael@0 | 2082 | { |
michael@0 | 2083 | goto bad_arg; |
michael@0 | 2084 | } |
michael@0 | 2085 | *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276); |
michael@0 | 2086 | } |
michael@0 | 2087 | break; |
michael@0 | 2088 | case OPUS_SET_FORCE_CHANNELS_REQUEST: |
michael@0 | 2089 | { |
michael@0 | 2090 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2091 | if((value<1 || value>st->channels) && value != OPUS_AUTO) |
michael@0 | 2092 | { |
michael@0 | 2093 | goto bad_arg; |
michael@0 | 2094 | } |
michael@0 | 2095 | st->force_channels = value; |
michael@0 | 2096 | } |
michael@0 | 2097 | break; |
michael@0 | 2098 | case OPUS_GET_FORCE_CHANNELS_REQUEST: |
michael@0 | 2099 | { |
michael@0 | 2100 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2101 | if (!value) |
michael@0 | 2102 | { |
michael@0 | 2103 | goto bad_arg; |
michael@0 | 2104 | } |
michael@0 | 2105 | *value = st->force_channels; |
michael@0 | 2106 | } |
michael@0 | 2107 | break; |
michael@0 | 2108 | case OPUS_SET_MAX_BANDWIDTH_REQUEST: |
michael@0 | 2109 | { |
michael@0 | 2110 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2111 | if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) |
michael@0 | 2112 | { |
michael@0 | 2113 | goto bad_arg; |
michael@0 | 2114 | } |
michael@0 | 2115 | st->max_bandwidth = value; |
michael@0 | 2116 | if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { |
michael@0 | 2117 | st->silk_mode.maxInternalSampleRate = 8000; |
michael@0 | 2118 | } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { |
michael@0 | 2119 | st->silk_mode.maxInternalSampleRate = 12000; |
michael@0 | 2120 | } else { |
michael@0 | 2121 | st->silk_mode.maxInternalSampleRate = 16000; |
michael@0 | 2122 | } |
michael@0 | 2123 | } |
michael@0 | 2124 | break; |
michael@0 | 2125 | case OPUS_GET_MAX_BANDWIDTH_REQUEST: |
michael@0 | 2126 | { |
michael@0 | 2127 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2128 | if (!value) |
michael@0 | 2129 | { |
michael@0 | 2130 | goto bad_arg; |
michael@0 | 2131 | } |
michael@0 | 2132 | *value = st->max_bandwidth; |
michael@0 | 2133 | } |
michael@0 | 2134 | break; |
michael@0 | 2135 | case OPUS_SET_BANDWIDTH_REQUEST: |
michael@0 | 2136 | { |
michael@0 | 2137 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2138 | if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO) |
michael@0 | 2139 | { |
michael@0 | 2140 | goto bad_arg; |
michael@0 | 2141 | } |
michael@0 | 2142 | st->user_bandwidth = value; |
michael@0 | 2143 | if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { |
michael@0 | 2144 | st->silk_mode.maxInternalSampleRate = 8000; |
michael@0 | 2145 | } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { |
michael@0 | 2146 | st->silk_mode.maxInternalSampleRate = 12000; |
michael@0 | 2147 | } else { |
michael@0 | 2148 | st->silk_mode.maxInternalSampleRate = 16000; |
michael@0 | 2149 | } |
michael@0 | 2150 | } |
michael@0 | 2151 | break; |
michael@0 | 2152 | case OPUS_GET_BANDWIDTH_REQUEST: |
michael@0 | 2153 | { |
michael@0 | 2154 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2155 | if (!value) |
michael@0 | 2156 | { |
michael@0 | 2157 | goto bad_arg; |
michael@0 | 2158 | } |
michael@0 | 2159 | *value = st->bandwidth; |
michael@0 | 2160 | } |
michael@0 | 2161 | break; |
michael@0 | 2162 | case OPUS_SET_DTX_REQUEST: |
michael@0 | 2163 | { |
michael@0 | 2164 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2165 | if(value<0 || value>1) |
michael@0 | 2166 | { |
michael@0 | 2167 | goto bad_arg; |
michael@0 | 2168 | } |
michael@0 | 2169 | st->silk_mode.useDTX = value; |
michael@0 | 2170 | } |
michael@0 | 2171 | break; |
michael@0 | 2172 | case OPUS_GET_DTX_REQUEST: |
michael@0 | 2173 | { |
michael@0 | 2174 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2175 | if (!value) |
michael@0 | 2176 | { |
michael@0 | 2177 | goto bad_arg; |
michael@0 | 2178 | } |
michael@0 | 2179 | *value = st->silk_mode.useDTX; |
michael@0 | 2180 | } |
michael@0 | 2181 | break; |
michael@0 | 2182 | case OPUS_SET_COMPLEXITY_REQUEST: |
michael@0 | 2183 | { |
michael@0 | 2184 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2185 | if(value<0 || value>10) |
michael@0 | 2186 | { |
michael@0 | 2187 | goto bad_arg; |
michael@0 | 2188 | } |
michael@0 | 2189 | st->silk_mode.complexity = value; |
michael@0 | 2190 | celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value)); |
michael@0 | 2191 | } |
michael@0 | 2192 | break; |
michael@0 | 2193 | case OPUS_GET_COMPLEXITY_REQUEST: |
michael@0 | 2194 | { |
michael@0 | 2195 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2196 | if (!value) |
michael@0 | 2197 | { |
michael@0 | 2198 | goto bad_arg; |
michael@0 | 2199 | } |
michael@0 | 2200 | *value = st->silk_mode.complexity; |
michael@0 | 2201 | } |
michael@0 | 2202 | break; |
michael@0 | 2203 | case OPUS_SET_INBAND_FEC_REQUEST: |
michael@0 | 2204 | { |
michael@0 | 2205 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2206 | if(value<0 || value>1) |
michael@0 | 2207 | { |
michael@0 | 2208 | goto bad_arg; |
michael@0 | 2209 | } |
michael@0 | 2210 | st->silk_mode.useInBandFEC = value; |
michael@0 | 2211 | } |
michael@0 | 2212 | break; |
michael@0 | 2213 | case OPUS_GET_INBAND_FEC_REQUEST: |
michael@0 | 2214 | { |
michael@0 | 2215 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2216 | if (!value) |
michael@0 | 2217 | { |
michael@0 | 2218 | goto bad_arg; |
michael@0 | 2219 | } |
michael@0 | 2220 | *value = st->silk_mode.useInBandFEC; |
michael@0 | 2221 | } |
michael@0 | 2222 | break; |
michael@0 | 2223 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
michael@0 | 2224 | { |
michael@0 | 2225 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2226 | if (value < 0 || value > 100) |
michael@0 | 2227 | { |
michael@0 | 2228 | goto bad_arg; |
michael@0 | 2229 | } |
michael@0 | 2230 | st->silk_mode.packetLossPercentage = value; |
michael@0 | 2231 | celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value)); |
michael@0 | 2232 | } |
michael@0 | 2233 | break; |
michael@0 | 2234 | case OPUS_GET_PACKET_LOSS_PERC_REQUEST: |
michael@0 | 2235 | { |
michael@0 | 2236 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2237 | if (!value) |
michael@0 | 2238 | { |
michael@0 | 2239 | goto bad_arg; |
michael@0 | 2240 | } |
michael@0 | 2241 | *value = st->silk_mode.packetLossPercentage; |
michael@0 | 2242 | } |
michael@0 | 2243 | break; |
michael@0 | 2244 | case OPUS_SET_VBR_REQUEST: |
michael@0 | 2245 | { |
michael@0 | 2246 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2247 | if(value<0 || value>1) |
michael@0 | 2248 | { |
michael@0 | 2249 | goto bad_arg; |
michael@0 | 2250 | } |
michael@0 | 2251 | st->use_vbr = value; |
michael@0 | 2252 | st->silk_mode.useCBR = 1-value; |
michael@0 | 2253 | } |
michael@0 | 2254 | break; |
michael@0 | 2255 | case OPUS_GET_VBR_REQUEST: |
michael@0 | 2256 | { |
michael@0 | 2257 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2258 | if (!value) |
michael@0 | 2259 | { |
michael@0 | 2260 | goto bad_arg; |
michael@0 | 2261 | } |
michael@0 | 2262 | *value = st->use_vbr; |
michael@0 | 2263 | } |
michael@0 | 2264 | break; |
michael@0 | 2265 | case OPUS_SET_VOICE_RATIO_REQUEST: |
michael@0 | 2266 | { |
michael@0 | 2267 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2268 | if (value<-1 || value>100) |
michael@0 | 2269 | { |
michael@0 | 2270 | goto bad_arg; |
michael@0 | 2271 | } |
michael@0 | 2272 | st->voice_ratio = value; |
michael@0 | 2273 | } |
michael@0 | 2274 | break; |
michael@0 | 2275 | case OPUS_GET_VOICE_RATIO_REQUEST: |
michael@0 | 2276 | { |
michael@0 | 2277 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2278 | if (!value) |
michael@0 | 2279 | { |
michael@0 | 2280 | goto bad_arg; |
michael@0 | 2281 | } |
michael@0 | 2282 | *value = st->voice_ratio; |
michael@0 | 2283 | } |
michael@0 | 2284 | break; |
michael@0 | 2285 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
michael@0 | 2286 | { |
michael@0 | 2287 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2288 | if(value<0 || value>1) |
michael@0 | 2289 | { |
michael@0 | 2290 | goto bad_arg; |
michael@0 | 2291 | } |
michael@0 | 2292 | st->vbr_constraint = value; |
michael@0 | 2293 | } |
michael@0 | 2294 | break; |
michael@0 | 2295 | case OPUS_GET_VBR_CONSTRAINT_REQUEST: |
michael@0 | 2296 | { |
michael@0 | 2297 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2298 | if (!value) |
michael@0 | 2299 | { |
michael@0 | 2300 | goto bad_arg; |
michael@0 | 2301 | } |
michael@0 | 2302 | *value = st->vbr_constraint; |
michael@0 | 2303 | } |
michael@0 | 2304 | break; |
michael@0 | 2305 | case OPUS_SET_SIGNAL_REQUEST: |
michael@0 | 2306 | { |
michael@0 | 2307 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2308 | if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC) |
michael@0 | 2309 | { |
michael@0 | 2310 | goto bad_arg; |
michael@0 | 2311 | } |
michael@0 | 2312 | st->signal_type = value; |
michael@0 | 2313 | } |
michael@0 | 2314 | break; |
michael@0 | 2315 | case OPUS_GET_SIGNAL_REQUEST: |
michael@0 | 2316 | { |
michael@0 | 2317 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2318 | if (!value) |
michael@0 | 2319 | { |
michael@0 | 2320 | goto bad_arg; |
michael@0 | 2321 | } |
michael@0 | 2322 | *value = st->signal_type; |
michael@0 | 2323 | } |
michael@0 | 2324 | break; |
michael@0 | 2325 | case OPUS_GET_LOOKAHEAD_REQUEST: |
michael@0 | 2326 | { |
michael@0 | 2327 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2328 | if (!value) |
michael@0 | 2329 | { |
michael@0 | 2330 | goto bad_arg; |
michael@0 | 2331 | } |
michael@0 | 2332 | *value = st->Fs/400; |
michael@0 | 2333 | if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
michael@0 | 2334 | *value += st->delay_compensation; |
michael@0 | 2335 | } |
michael@0 | 2336 | break; |
michael@0 | 2337 | case OPUS_GET_SAMPLE_RATE_REQUEST: |
michael@0 | 2338 | { |
michael@0 | 2339 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2340 | if (!value) |
michael@0 | 2341 | { |
michael@0 | 2342 | goto bad_arg; |
michael@0 | 2343 | } |
michael@0 | 2344 | *value = st->Fs; |
michael@0 | 2345 | } |
michael@0 | 2346 | break; |
michael@0 | 2347 | case OPUS_GET_FINAL_RANGE_REQUEST: |
michael@0 | 2348 | { |
michael@0 | 2349 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
michael@0 | 2350 | if (!value) |
michael@0 | 2351 | { |
michael@0 | 2352 | goto bad_arg; |
michael@0 | 2353 | } |
michael@0 | 2354 | *value = st->rangeFinal; |
michael@0 | 2355 | } |
michael@0 | 2356 | break; |
michael@0 | 2357 | case OPUS_SET_LSB_DEPTH_REQUEST: |
michael@0 | 2358 | { |
michael@0 | 2359 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2360 | if (value<8 || value>24) |
michael@0 | 2361 | { |
michael@0 | 2362 | goto bad_arg; |
michael@0 | 2363 | } |
michael@0 | 2364 | st->lsb_depth=value; |
michael@0 | 2365 | } |
michael@0 | 2366 | break; |
michael@0 | 2367 | case OPUS_GET_LSB_DEPTH_REQUEST: |
michael@0 | 2368 | { |
michael@0 | 2369 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2370 | if (!value) |
michael@0 | 2371 | { |
michael@0 | 2372 | goto bad_arg; |
michael@0 | 2373 | } |
michael@0 | 2374 | *value = st->lsb_depth; |
michael@0 | 2375 | } |
michael@0 | 2376 | break; |
michael@0 | 2377 | case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: |
michael@0 | 2378 | { |
michael@0 | 2379 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2380 | if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS && |
michael@0 | 2381 | value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS && |
michael@0 | 2382 | value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS && |
michael@0 | 2383 | value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_VARIABLE) |
michael@0 | 2384 | { |
michael@0 | 2385 | goto bad_arg; |
michael@0 | 2386 | } |
michael@0 | 2387 | st->variable_duration = value; |
michael@0 | 2388 | celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value)); |
michael@0 | 2389 | } |
michael@0 | 2390 | break; |
michael@0 | 2391 | case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: |
michael@0 | 2392 | { |
michael@0 | 2393 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2394 | if (!value) |
michael@0 | 2395 | { |
michael@0 | 2396 | goto bad_arg; |
michael@0 | 2397 | } |
michael@0 | 2398 | *value = st->variable_duration; |
michael@0 | 2399 | } |
michael@0 | 2400 | break; |
michael@0 | 2401 | case OPUS_SET_PREDICTION_DISABLED_REQUEST: |
michael@0 | 2402 | { |
michael@0 | 2403 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2404 | if (value > 1 || value < 0) |
michael@0 | 2405 | goto bad_arg; |
michael@0 | 2406 | st->silk_mode.reducedDependency = value; |
michael@0 | 2407 | } |
michael@0 | 2408 | break; |
michael@0 | 2409 | case OPUS_GET_PREDICTION_DISABLED_REQUEST: |
michael@0 | 2410 | { |
michael@0 | 2411 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2412 | if (!value) |
michael@0 | 2413 | goto bad_arg; |
michael@0 | 2414 | *value = st->silk_mode.reducedDependency; |
michael@0 | 2415 | } |
michael@0 | 2416 | break; |
michael@0 | 2417 | case OPUS_RESET_STATE: |
michael@0 | 2418 | { |
michael@0 | 2419 | void *silk_enc; |
michael@0 | 2420 | silk_EncControlStruct dummy; |
michael@0 | 2421 | silk_enc = (char*)st+st->silk_enc_offset; |
michael@0 | 2422 | |
michael@0 | 2423 | OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START, |
michael@0 | 2424 | sizeof(OpusEncoder)- |
michael@0 | 2425 | ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st)); |
michael@0 | 2426 | |
michael@0 | 2427 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
michael@0 | 2428 | silk_InitEncoder( silk_enc, st->arch, &dummy ); |
michael@0 | 2429 | st->stream_channels = st->channels; |
michael@0 | 2430 | st->hybrid_stereo_width_Q14 = 1 << 14; |
michael@0 | 2431 | st->prev_HB_gain = Q15ONE; |
michael@0 | 2432 | st->first = 1; |
michael@0 | 2433 | st->mode = MODE_HYBRID; |
michael@0 | 2434 | st->bandwidth = OPUS_BANDWIDTH_FULLBAND; |
michael@0 | 2435 | st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
michael@0 | 2436 | } |
michael@0 | 2437 | break; |
michael@0 | 2438 | case OPUS_SET_FORCE_MODE_REQUEST: |
michael@0 | 2439 | { |
michael@0 | 2440 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2441 | if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO) |
michael@0 | 2442 | { |
michael@0 | 2443 | goto bad_arg; |
michael@0 | 2444 | } |
michael@0 | 2445 | st->user_forced_mode = value; |
michael@0 | 2446 | } |
michael@0 | 2447 | break; |
michael@0 | 2448 | case OPUS_SET_LFE_REQUEST: |
michael@0 | 2449 | { |
michael@0 | 2450 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2451 | st->lfe = value; |
michael@0 | 2452 | ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value)); |
michael@0 | 2453 | } |
michael@0 | 2454 | break; |
michael@0 | 2455 | case OPUS_SET_ENERGY_MASK_REQUEST: |
michael@0 | 2456 | { |
michael@0 | 2457 | opus_val16 *value = va_arg(ap, opus_val16*); |
michael@0 | 2458 | st->energy_masking = value; |
michael@0 | 2459 | ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value)); |
michael@0 | 2460 | } |
michael@0 | 2461 | break; |
michael@0 | 2462 | |
michael@0 | 2463 | case CELT_GET_MODE_REQUEST: |
michael@0 | 2464 | { |
michael@0 | 2465 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
michael@0 | 2466 | if (!value) |
michael@0 | 2467 | { |
michael@0 | 2468 | goto bad_arg; |
michael@0 | 2469 | } |
michael@0 | 2470 | ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value)); |
michael@0 | 2471 | } |
michael@0 | 2472 | break; |
michael@0 | 2473 | default: |
michael@0 | 2474 | /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/ |
michael@0 | 2475 | ret = OPUS_UNIMPLEMENTED; |
michael@0 | 2476 | break; |
michael@0 | 2477 | } |
michael@0 | 2478 | va_end(ap); |
michael@0 | 2479 | return ret; |
michael@0 | 2480 | bad_arg: |
michael@0 | 2481 | va_end(ap); |
michael@0 | 2482 | return OPUS_BAD_ARG; |
michael@0 | 2483 | } |
michael@0 | 2484 | |
michael@0 | 2485 | void opus_encoder_destroy(OpusEncoder *st) |
michael@0 | 2486 | { |
michael@0 | 2487 | opus_free(st); |
michael@0 | 2488 | } |