Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: third_party/opus/src/src/opus_encoder.c

Issue 2962373002: [Opus] Update to v1.2.1 (Closed)
Patch Set: Pre-increment instead of post-increment Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited 1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */ 2 Written by Jean-Marc Valin and Koen Vos */
3 /* 3 /*
4 Redistribution and use in source and binary forms, with or without 4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions 5 modification, are permitted provided that the following conditions
6 are met: 6 are met:
7 7
8 - Redistributions of source code must retain the above copyright 8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer. 9 notice, this list of conditions and the following disclaimer.
10 10
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 #include "mathops.h" 46 #include "mathops.h"
47 #include "tuning_parameters.h" 47 #include "tuning_parameters.h"
48 #ifdef FIXED_POINT 48 #ifdef FIXED_POINT
49 #include "fixed/structs_FIX.h" 49 #include "fixed/structs_FIX.h"
50 #else 50 #else
51 #include "float/structs_FLP.h" 51 #include "float/structs_FLP.h"
52 #endif 52 #endif
53 53
54 #define MAX_ENCODER_BUFFER 480 54 #define MAX_ENCODER_BUFFER 480
55 55
56 #ifndef DISABLE_FLOAT_API
57 #define PSEUDO_SNR_THRESHOLD 316.23f /* 10^(25/10) */
58 #endif
59
56 typedef struct { 60 typedef struct {
57 opus_val32 XX, XY, YY; 61 opus_val32 XX, XY, YY;
58 opus_val16 smoothed_width; 62 opus_val16 smoothed_width;
59 opus_val16 max_follower; 63 opus_val16 max_follower;
60 } StereoWidthState; 64 } StereoWidthState;
61 65
62 struct OpusEncoder { 66 struct OpusEncoder {
63 int celt_enc_offset; 67 int celt_enc_offset;
64 int silk_enc_offset; 68 int silk_enc_offset;
65 silk_EncControlStruct silk_mode; 69 silk_EncControlStruct silk_mode;
66 int application; 70 int application;
67 int channels; 71 int channels;
68 int delay_compensation; 72 int delay_compensation;
69 int force_channels; 73 int force_channels;
70 int signal_type; 74 int signal_type;
71 int user_bandwidth; 75 int user_bandwidth;
72 int max_bandwidth; 76 int max_bandwidth;
73 int user_forced_mode; 77 int user_forced_mode;
74 int voice_ratio; 78 int voice_ratio;
75 opus_int32 Fs; 79 opus_int32 Fs;
76 int use_vbr; 80 int use_vbr;
77 int vbr_constraint; 81 int vbr_constraint;
78 int variable_duration; 82 int variable_duration;
79 opus_int32 bitrate_bps; 83 opus_int32 bitrate_bps;
80 opus_int32 user_bitrate_bps; 84 opus_int32 user_bitrate_bps;
81 int lsb_depth; 85 int lsb_depth;
82 int encoder_buffer; 86 int encoder_buffer;
83 int lfe; 87 int lfe;
84 int arch; 88 int arch;
89 int use_dtx; /* general DTX for both SILK and CELT */
85 #ifndef DISABLE_FLOAT_API 90 #ifndef DISABLE_FLOAT_API
86 TonalityAnalysisState analysis; 91 TonalityAnalysisState analysis;
87 #endif 92 #endif
88 93
89 #define OPUS_ENCODER_RESET_START stream_channels 94 #define OPUS_ENCODER_RESET_START stream_channels
90 int stream_channels; 95 int stream_channels;
91 opus_int16 hybrid_stereo_width_Q14; 96 opus_int16 hybrid_stereo_width_Q14;
92 opus_int32 variable_HP_smth2_Q15; 97 opus_int32 variable_HP_smth2_Q15;
93 opus_val16 prev_HB_gain; 98 opus_val16 prev_HB_gain;
94 opus_val32 hp_mem[4]; 99 opus_val32 hp_mem[4];
95 int mode; 100 int mode;
96 int prev_mode; 101 int prev_mode;
97 int prev_channels; 102 int prev_channels;
98 int prev_framesize; 103 int prev_framesize;
99 int bandwidth; 104 int bandwidth;
105 /* Bandwidth determined automatically from the rate (before any other adjust ment) */
106 int auto_bandwidth;
100 int silk_bw_switch; 107 int silk_bw_switch;
101 /* Sampling rate (at the API level) */ 108 /* Sampling rate (at the API level) */
102 int first; 109 int first;
103 opus_val16 * energy_masking; 110 opus_val16 * energy_masking;
104 StereoWidthState width_mem; 111 StereoWidthState width_mem;
105 opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2]; 112 opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2];
106 #ifndef DISABLE_FLOAT_API 113 #ifndef DISABLE_FLOAT_API
107 int detected_bandwidth; 114 int detected_bandwidth;
115 int nb_no_activity_frames;
116 opus_val32 peak_signal_energy;
108 #endif 117 #endif
118 int nonfinal_frame; /* current frame is not the final in a packet * /
109 opus_uint32 rangeFinal; 119 opus_uint32 rangeFinal;
110 }; 120 };
111 121
112 /* Transition tables for the voice and music. First column is the 122 /* Transition tables for the voice and music. First column is the
113 middle (memoriless) threshold. The second column is the hysteresis 123 middle (memoriless) threshold. The second column is the hysteresis
114 (difference with the middle) */ 124 (difference with the middle) */
115 static const opus_int32 mono_voice_bandwidth_thresholds[8] = { 125 static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
116 11000, 1000, /* NB<->MB */ 126 10000, 1000, /* NB<->MB */
117 14000, 1000, /* MB<->WB */ 127 11000, 1000, /* MB<->WB */
118 17000, 1000, /* WB<->SWB */ 128 13500, 1000, /* WB<->SWB */
119 21000, 2000, /* SWB<->FB */ 129 14000, 2000, /* SWB<->FB */
120 }; 130 };
121 static const opus_int32 mono_music_bandwidth_thresholds[8] = { 131 static const opus_int32 mono_music_bandwidth_thresholds[8] = {
122 12000, 1000, /* NB<->MB */ 132 10000, 1000, /* NB<->MB */
123 15000, 1000, /* MB<->WB */ 133 11000, 1000, /* MB<->WB */
124 18000, 2000, /* WB<->SWB */ 134 13500, 1000, /* WB<->SWB */
125 22000, 2000, /* SWB<->FB */ 135 14000, 2000, /* SWB<->FB */
126 }; 136 };
127 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { 137 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
128 11000, 1000, /* NB<->MB */ 138 10000, 1000, /* NB<->MB */
129 14000, 1000, /* MB<->WB */ 139 11000, 1000, /* MB<->WB */
130 21000, 2000, /* WB<->SWB */ 140 13500, 1000, /* WB<->SWB */
131 28000, 2000, /* SWB<->FB */ 141 14000, 2000, /* SWB<->FB */
132 }; 142 };
133 static const opus_int32 stereo_music_bandwidth_thresholds[8] = { 143 static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
134 12000, 1000, /* NB<->MB */ 144 10000, 1000, /* NB<->MB */
135 18000, 2000, /* MB<->WB */ 145 11000, 1000, /* MB<->WB */
136 21000, 2000, /* WB<->SWB */ 146 13500, 1000, /* WB<->SWB */
137 30000, 2000, /* SWB<->FB */ 147 14000, 2000, /* SWB<->FB */
138 }; 148 };
139 /* Threshold bit-rates for switching between mono and stereo */ 149 /* Threshold bit-rates for switching between mono and stereo */
140 static const opus_int32 stereo_voice_threshold = 30000; 150 static const opus_int32 stereo_voice_threshold = 24000;
141 static const opus_int32 stereo_music_threshold = 30000; 151 static const opus_int32 stereo_music_threshold = 24000;
142 152
143 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ 153 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
144 static const opus_int32 mode_thresholds[2][2] = { 154 static const opus_int32 mode_thresholds[2][2] = {
145 /* voice */ /* music */ 155 /* voice */ /* music */
146 { 64000, 16000}, /* mono */ 156 { 64000, 16000}, /* mono */
147 { 36000, 16000}, /* stereo */ 157 { 36000, 16000}, /* stereo */
148 }; 158 };
149 159
160 static const opus_int32 fec_thresholds[] = {
161 12000, 1000, /* NB */
162 14000, 1000, /* MB */
163 16000, 1000, /* WB */
164 20000, 1000, /* SWB */
165 22000, 1000, /* FB */
166 };
167
150 int opus_encoder_get_size(int channels) 168 int opus_encoder_get_size(int channels)
151 { 169 {
152 int silkEncSizeBytes, celtEncSizeBytes; 170 int silkEncSizeBytes, celtEncSizeBytes;
153 int ret; 171 int ret;
154 if (channels<1 || channels > 2) 172 if (channels<1 || channels > 2)
155 return 0; 173 return 0;
156 ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); 174 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
157 if (ret) 175 if (ret)
158 return 0; 176 return 0;
159 silkEncSizeBytes = align(silkEncSizeBytes); 177 silkEncSizeBytes = align(silkEncSizeBytes);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 st->delay_compensation = st->Fs/250; 256 st->delay_compensation = st->Fs/250;
239 257
240 st->hybrid_stereo_width_Q14 = 1 << 14; 258 st->hybrid_stereo_width_Q14 = 1 << 14;
241 st->prev_HB_gain = Q15ONE; 259 st->prev_HB_gain = Q15ONE;
242 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 ); 260 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 );
243 st->first = 1; 261 st->first = 1;
244 st->mode = MODE_HYBRID; 262 st->mode = MODE_HYBRID;
245 st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 263 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
246 264
247 #ifndef DISABLE_FLOAT_API 265 #ifndef DISABLE_FLOAT_API
248 tonality_analysis_init(&st->analysis); 266 tonality_analysis_init(&st->analysis, st->Fs);
267 st->analysis.application = st->application;
249 #endif 268 #endif
250 269
251 return OPUS_OK; 270 return OPUS_OK;
252 } 271 }
253 272
254 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channel s) 273 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channel s)
255 { 274 {
256 int period; 275 int period;
257 unsigned char toc; 276 unsigned char toc;
258 period = 0; 277 period = 0;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval; 335 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
317 336
318 S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL; 337 S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL;
319 338
320 /* Scale back to Q0 and saturate */ 339 /* Scale back to Q0 and saturate */
321 out[ k*stride ] = vout; 340 out[ k*stride ] = vout;
322 } 341 }
323 } 342 }
324 #endif 343 #endif
325 344
326 static void hp_cutoff(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *ou t, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) 345 static void hp_cutoff(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *ou t, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs, int arch)
327 { 346 {
328 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ]; 347 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
329 opus_int32 Fc_Q19, r_Q28, r_Q22; 348 opus_int32 Fc_Q19, r_Q28, r_Q22;
349 (void)arch;
330 350
331 silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 10 00, 19 ) ); 351 silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 10 00, 19 ) );
332 Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 ); 352 Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
333 silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 ); 353 silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
334 354
335 r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q 19 ); 355 r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q 19 );
336 356
337 /* b = r * [ 1; -2; 1 ]; */ 357 /* b = r * [ 1; -2; 1 ]; */
338 /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ 358 /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
339 B_Q28[ 0 ] = r_Q28; 359 B_Q28[ 0 ] = r_Q28;
340 B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 ); 360 B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 );
341 B_Q28[ 2 ] = r_Q28; 361 B_Q28[ 2 ] = r_Q28;
342 362
343 /* -r * ( 2 - Fc * Fc ); */ 363 /* -r * ( 2 - Fc * Fc ); */
344 r_Q22 = silk_RSHIFT( r_Q28, 6 ); 364 r_Q22 = silk_RSHIFT( r_Q28, 6 );
345 A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CON ST( 2.0, 22 ) ); 365 A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CON ST( 2.0, 22 ) );
346 A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 ); 366 A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 );
347 367
348 #ifdef FIXED_POINT 368 #ifdef FIXED_POINT
349 silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels ); 369 if( channels == 1 ) {
350 if( channels == 2 ) { 370 silk_biquad_alt_stride1( in, B_Q28, A_Q28, hp_mem, out, len );
351 silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); 371 } else {
372 silk_biquad_alt_stride2( in, B_Q28, A_Q28, hp_mem, out, len, arch );
352 } 373 }
353 #else 374 #else
354 silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels ); 375 silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels );
355 if( channels == 2 ) { 376 if( channels == 2 ) {
356 silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); 377 silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
357 } 378 }
358 #endif 379 #endif
359 } 380 }
360 381
361 #ifdef FIXED_POINT 382 #ifdef FIXED_POINT
362 static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *ou t, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) 383 static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *ou t, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
363 { 384 {
364 int c, i; 385 int c, i;
365 int shift; 386 int shift;
366 387
367 /* Approximates -round(log2(4.*cutoff_Hz/Fs)) */ 388 /* Approximates -round(log2(4.*cutoff_Hz/Fs)) */
368 shift=celt_ilog2(Fs/(cutoff_Hz*3)); 389 shift=celt_ilog2(Fs/(cutoff_Hz*3));
369 for (c=0;c<channels;c++) 390 for (c=0;c<channels;c++)
370 { 391 {
371 for (i=0;i<len;i++) 392 for (i=0;i<len;i++)
372 { 393 {
373 opus_val32 x, tmp, y; 394 opus_val32 x, tmp, y;
374 x = SHL32(EXTEND32(in[channels*i+c]), 15); 395 x = SHL32(EXTEND32(in[channels*i+c]), 14);
375 /* First stage */ 396 /* First stage */
376 tmp = x-hp_mem[2*c]; 397 tmp = x-hp_mem[2*c];
377 hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift); 398 hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift);
378 /* Second stage */ 399 /* Second stage */
379 y = tmp - hp_mem[2*c+1]; 400 y = tmp - hp_mem[2*c+1];
380 hp_mem[2*c+1] = hp_mem[2*c+1] + PSHR32(tmp - hp_mem[2*c+1], shift); 401 hp_mem[2*c+1] = hp_mem[2*c+1] + PSHR32(tmp - hp_mem[2*c+1], shift);
381 out[channels*i+c] = EXTRACT16(SATURATE(PSHR32(y, 15), 32767)); 402 out[channels*i+c] = EXTRACT16(SATURATE(PSHR32(y, 14), 32767));
382 } 403 }
383 } 404 }
384 } 405 }
385 406
386 #else 407 #else
387 static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *ou t, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) 408 static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *ou t, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
388 { 409 {
389 int c, i; 410 int i;
390 float coef; 411 float coef, coef2;
391
392 coef = 4.0f*cutoff_Hz/Fs; 412 coef = 4.0f*cutoff_Hz/Fs;
393 for (c=0;c<channels;c++) 413 coef2 = 1-coef;
414 if (channels==2)
394 { 415 {
416 float m0, m1, m2, m3;
417 m0 = hp_mem[0];
418 m1 = hp_mem[1];
419 m2 = hp_mem[2];
420 m3 = hp_mem[3];
421 for (i=0;i<len;i++)
422 {
423 opus_val32 x0, x1, tmp0, tmp1, out0, out1;
424 x0 = in[2*i+0];
425 x1 = in[2*i+1];
426 /* First stage */
427 tmp0 = x0-m0;
428 tmp1 = x1-m2;
429 m0 = coef*x0 + VERY_SMALL + coef2*m0;
430 m2 = coef*x1 + VERY_SMALL + coef2*m2;
431 /* Second stage */
432 out0 = tmp0 - m1;
433 out1 = tmp1 - m3;
434 m1 = coef*tmp0 + VERY_SMALL + coef2*m1;
435 m3 = coef*tmp1 + VERY_SMALL + coef2*m3;
436 out[2*i+0] = out0;
437 out[2*i+1] = out1;
438 }
439 hp_mem[0] = m0;
440 hp_mem[1] = m1;
441 hp_mem[2] = m2;
442 hp_mem[3] = m3;
443 } else {
444 float m0, m1;
445 m0 = hp_mem[0];
446 m1 = hp_mem[1];
395 for (i=0;i<len;i++) 447 for (i=0;i<len;i++)
396 { 448 {
397 opus_val32 x, tmp, y; 449 opus_val32 x, tmp, y;
398 x = in[channels*i+c]; 450 x = in[i];
399 /* First stage */ 451 /* First stage */
400 tmp = x-hp_mem[2*c]; 452 tmp = x-m0;
401 hp_mem[2*c] = hp_mem[2*c] + coef*(x - hp_mem[2*c]) + VERY_SMALL; 453 m0 = coef*x + VERY_SMALL + coef2*m0;
402 /* Second stage */ 454 /* Second stage */
403 y = tmp - hp_mem[2*c+1]; 455 y = tmp - m1;
404 hp_mem[2*c+1] = hp_mem[2*c+1] + coef*(tmp - hp_mem[2*c+1]) + VERY_SMALL ; 456 m1 = coef*tmp + VERY_SMALL + coef2*m1;
405 out[channels*i+c] = y; 457 out[i] = y;
406 } 458 }
459 hp_mem[0] = m0;
460 hp_mem[1] = m1;
407 } 461 }
408 } 462 }
409 #endif 463 #endif
410 464
411 static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, op us_val16 g2, 465 static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, op us_val16 g2,
412 int overlap48, int frame_size, int channels, const opus_val16 *window, o pus_int32 Fs) 466 int overlap48, int frame_size, int channels, const opus_val16 *window, o pus_int32 Fs)
413 { 467 {
414 int i; 468 int i;
415 int overlap; 469 int overlap;
416 int inc; 470 int inc;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 if(!frame_size)frame_size=st->Fs/400; 568 if(!frame_size)frame_size=st->Fs/400;
515 if (st->user_bitrate_bps==OPUS_AUTO) 569 if (st->user_bitrate_bps==OPUS_AUTO)
516 return 60*st->Fs/frame_size + st->Fs*st->channels; 570 return 60*st->Fs/frame_size + st->Fs*st->channels;
517 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX) 571 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
518 return max_data_bytes*8*st->Fs/frame_size; 572 return max_data_bytes*8*st->Fs/frame_size;
519 else 573 else
520 return st->user_bitrate_bps; 574 return st->user_bitrate_bps;
521 } 575 }
522 576
523 #ifndef DISABLE_FLOAT_API 577 #ifndef DISABLE_FLOAT_API
524 /* Don't use more than 60 ms for the frame size analysis */
525 #define MAX_DYNAMIC_FRAMESIZE 24
526 /* Estimates how much the bitrate will be boosted based on the sub-frame energy */
527 static float transient_boost(const float *E, const float *E_1, int LM, int maxM)
528 {
529 int i;
530 int M;
531 float sumE=0, sumE_1=0;
532 float metric;
533
534 M = IMIN(maxM, (1<<LM)+1);
535 for (i=0;i<M;i++)
536 {
537 sumE += E[i];
538 sumE_1 += E_1[i];
539 }
540 metric = sumE*sumE_1/(M*M);
541 /*if (LM==3)
542 printf("%f\n", metric);*/
543 /*return metric>10 ? 1 : 0;*/
544 /*return MAX16(0,1-exp(-.25*(metric-2.)));*/
545 return MIN16(1,(float)sqrt(MAX16(0,.05f*(metric-2))));
546 }
547
548 /* Viterbi decoding trying to find the best frame size combination using look-ah ead
549
550 State numbering:
551 0: unused
552 1: 2.5 ms
553 2: 5 ms (#1)
554 3: 5 ms (#2)
555 4: 10 ms (#1)
556 5: 10 ms (#2)
557 6: 10 ms (#3)
558 7: 10 ms (#4)
559 8: 20 ms (#1)
560 9: 20 ms (#2)
561 10: 20 ms (#3)
562 11: 20 ms (#4)
563 12: 20 ms (#5)
564 13: 20 ms (#6)
565 14: 20 ms (#7)
566 15: 20 ms (#8)
567 */
568 static int transient_viterbi(const float *E, const float *E_1, int N, int frame_ cost, int rate)
569 {
570 int i;
571 float cost[MAX_DYNAMIC_FRAMESIZE][16];
572 int states[MAX_DYNAMIC_FRAMESIZE][16];
573 float best_cost;
574 int best_state;
575 float factor;
576 /* Take into account that we damp VBR in the 32 kb/s to 64 kb/s range. */
577 if (rate<80)
578 factor=0;
579 else if (rate>160)
580 factor=1;
581 else
582 factor = (rate-80.f)/80.f;
583 /* Makes variable framesize less aggressive at lower bitrates, but I can't
584 find any valid theoretical justification for this (other than it seems
585 to help) */
586 for (i=0;i<16;i++)
587 {
588 /* Impossible state */
589 states[0][i] = -1;
590 cost[0][i] = 1e10;
591 }
592 for (i=0;i<4;i++)
593 {
594 cost[0][1<<i] = (frame_cost + rate*(1<<i))*(1+factor*transient_boost(E, E_ 1, i, N+1));
595 states[0][1<<i] = i;
596 }
597 for (i=1;i<N;i++)
598 {
599 int j;
600
601 /* Follow continuations */
602 for (j=2;j<16;j++)
603 {
604 cost[i][j] = cost[i-1][j-1];
605 states[i][j] = j-1;
606 }
607
608 /* New frames */
609 for(j=0;j<4;j++)
610 {
611 int k;
612 float min_cost;
613 float curr_cost;
614 states[i][1<<j] = 1;
615 min_cost = cost[i-1][1];
616 for(k=1;k<4;k++)
617 {
618 float tmp = cost[i-1][(1<<(k+1))-1];
619 if (tmp < min_cost)
620 {
621 states[i][1<<j] = (1<<(k+1))-1;
622 min_cost = tmp;
623 }
624 }
625 curr_cost = (frame_cost + rate*(1<<j))*(1+factor*transient_boost(E+i, E _1+i, j, N-i+1));
626 cost[i][1<<j] = min_cost;
627 /* If part of the frame is outside the analysis window, only count part of the cost */
628 if (N-i < (1<<j))
629 cost[i][1<<j] += curr_cost*(float)(N-i)/(1<<j);
630 else
631 cost[i][1<<j] += curr_cost;
632 }
633 }
634
635 best_state=1;
636 best_cost = cost[N-1][1];
637 /* Find best end state (doesn't force a frame to end at N-1) */
638 for (i=2;i<16;i++)
639 {
640 if (cost[N-1][i]<best_cost)
641 {
642 best_cost = cost[N-1][i];
643 best_state = i;
644 }
645 }
646
647 /* Follow transitions back */
648 for (i=N-1;i>=0;i--)
649 {
650 /*printf("%d ", best_state);*/
651 best_state = states[i][best_state];
652 }
653 /*printf("%d\n", best_state);*/
654 return best_state;
655 }
656
657 static int optimize_framesize(const void *x, int len, int C, opus_int32 Fs,
658 int bitrate, opus_val16 tonality, float *mem, int buffering,
659 downmix_func downmix)
660 {
661 int N;
662 int i;
663 float e[MAX_DYNAMIC_FRAMESIZE+4];
664 float e_1[MAX_DYNAMIC_FRAMESIZE+3];
665 opus_val32 memx;
666 int bestLM=0;
667 int subframe;
668 int pos;
669 int offset;
670 VARDECL(opus_val32, sub);
671
672 subframe = Fs/400;
673 ALLOC(sub, subframe, opus_val32);
674 e[0]=mem[0];
675 e_1[0]=1.f/(EPSILON+mem[0]);
676 if (buffering)
677 {
678 /* Consider the CELT delay when not in restricted-lowdelay */
679 /* We assume the buffering is between 2.5 and 5 ms */
680 offset = 2*subframe - buffering;
681 celt_assert(offset>=0 && offset <= subframe);
682 len -= offset;
683 e[1]=mem[1];
684 e_1[1]=1.f/(EPSILON+mem[1]);
685 e[2]=mem[2];
686 e_1[2]=1.f/(EPSILON+mem[2]);
687 pos = 3;
688 } else {
689 pos=1;
690 offset=0;
691 }
692 N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE);
693 /* Just silencing a warning, it's really initialized later */
694 memx = 0;
695 for (i=0;i<N;i++)
696 {
697 float tmp;
698 opus_val32 tmpx;
699 int j;
700 tmp=EPSILON;
701
702 downmix(x, sub, subframe, i*subframe+offset, 0, -2, C);
703 if (i==0)
704 memx = sub[0];
705 for (j=0;j<subframe;j++)
706 {
707 tmpx = sub[j];
708 tmp += (tmpx-memx)*(float)(tmpx-memx);
709 memx = tmpx;
710 }
711 e[i+pos] = tmp;
712 e_1[i+pos] = 1.f/tmp;
713 }
714 /* Hack to get 20 ms working with APPLICATION_AUDIO
715 The real problem is that the corresponding memory needs to use 1.5 ms
716 from this frame and 1 ms from the next frame */
717 e[i+pos] = e[i+pos-1];
718 if (buffering)
719 N=IMIN(MAX_DYNAMIC_FRAMESIZE, N+2);
720 bestLM = transient_viterbi(e, e_1, N, (int)((1.f+.5f*tonality)*(60*C+40)), bi trate/400);
721 mem[0] = e[1<<bestLM];
722 if (buffering)
723 {
724 mem[1] = e[(1<<bestLM)+1];
725 mem[2] = e[(1<<bestLM)+2];
726 }
727 return bestLM;
728 }
729
730 #endif
731
732 #ifndef DISABLE_FLOAT_API
733 #ifdef FIXED_POINT 578 #ifdef FIXED_POINT
734 #define PCM2VAL(x) FLOAT2INT16(x) 579 #define PCM2VAL(x) FLOAT2INT16(x)
735 #else 580 #else
736 #define PCM2VAL(x) SCALEIN(x) 581 #define PCM2VAL(x) SCALEIN(x)
737 #endif 582 #endif
738 void downmix_float(const void *_x, opus_val32 *sub, int subframe, int offset, in t c1, int c2, int C) 583
584 void downmix_float(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
739 { 585 {
740 const float *x; 586 const float *x;
741 opus_val32 scale;
742 int j; 587 int j;
588
743 x = (const float *)_x; 589 x = (const float *)_x;
744 for (j=0;j<subframe;j++) 590 for (j=0;j<subframe;j++)
745 sub[j] = PCM2VAL(x[(j+offset)*C+c1]); 591 y[j] = PCM2VAL(x[(j+offset)*C+c1]);
746 if (c2>-1) 592 if (c2>-1)
747 { 593 {
748 for (j=0;j<subframe;j++) 594 for (j=0;j<subframe;j++)
749 sub[j] += PCM2VAL(x[(j+offset)*C+c2]); 595 y[j] += PCM2VAL(x[(j+offset)*C+c2]);
750 } else if (c2==-2) 596 } else if (c2==-2)
751 { 597 {
752 int c; 598 int c;
753 for (c=1;c<C;c++) 599 for (c=1;c<C;c++)
754 { 600 {
755 for (j=0;j<subframe;j++) 601 for (j=0;j<subframe;j++)
756 sub[j] += PCM2VAL(x[(j+offset)*C+c]); 602 y[j] += PCM2VAL(x[(j+offset)*C+c]);
757 } 603 }
758 } 604 }
759 #ifdef FIXED_POINT
760 scale = (1<<SIG_SHIFT);
761 #else
762 scale = 1.f;
763 #endif
764 if (C==-2)
765 scale /= C;
766 else
767 scale /= 2;
768 for (j=0;j<subframe;j++)
769 sub[j] *= scale;
770 } 605 }
771 #endif 606 #endif
772 607
773 void downmix_int(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C) 608 void downmix_int(const void *_x, opus_val32 *y, int subframe, int offset, int c1 , int c2, int C)
774 { 609 {
775 const opus_int16 *x; 610 const opus_int16 *x;
776 opus_val32 scale;
777 int j; 611 int j;
612
778 x = (const opus_int16 *)_x; 613 x = (const opus_int16 *)_x;
779 for (j=0;j<subframe;j++) 614 for (j=0;j<subframe;j++)
780 sub[j] = x[(j+offset)*C+c1]; 615 y[j] = x[(j+offset)*C+c1];
781 if (c2>-1) 616 if (c2>-1)
782 { 617 {
783 for (j=0;j<subframe;j++) 618 for (j=0;j<subframe;j++)
784 sub[j] += x[(j+offset)*C+c2]; 619 y[j] += x[(j+offset)*C+c2];
785 } else if (c2==-2) 620 } else if (c2==-2)
786 { 621 {
787 int c; 622 int c;
788 for (c=1;c<C;c++) 623 for (c=1;c<C;c++)
789 { 624 {
790 for (j=0;j<subframe;j++) 625 for (j=0;j<subframe;j++)
791 sub[j] += x[(j+offset)*C+c]; 626 y[j] += x[(j+offset)*C+c];
792 } 627 }
793 } 628 }
794 #ifdef FIXED_POINT
795 scale = (1<<SIG_SHIFT);
796 #else
797 scale = 1.f/32768;
798 #endif
799 if (C==-2)
800 scale /= C;
801 else
802 scale /= 2;
803 for (j=0;j<subframe;j++)
804 sub[j] *= scale;
805 } 629 }
806 630
807 opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_ int32 Fs) 631 opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_ int32 Fs)
808 { 632 {
809 int new_size; 633 int new_size;
810 if (frame_size<Fs/400) 634 if (frame_size<Fs/400)
811 return -1; 635 return -1;
812 if (variable_duration == OPUS_FRAMESIZE_ARG) 636 if (variable_duration == OPUS_FRAMESIZE_ARG)
813 new_size = frame_size; 637 new_size = frame_size;
814 else if (variable_duration == OPUS_FRAMESIZE_VARIABLE) 638 else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= O PUS_FRAMESIZE_120_MS)
815 new_size = Fs/50; 639 {
816 else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= O PUS_FRAMESIZE_60_MS) 640 if (variable_duration <= OPUS_FRAMESIZE_40_MS)
817 new_size = IMIN(3*Fs/50, (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_M S)); 641 new_size = (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS);
642 else
643 new_size = (variable_duration-OPUS_FRAMESIZE_2_5_MS-2)*Fs/50;
644 }
818 else 645 else
819 return -1; 646 return -1;
820 if (new_size>frame_size) 647 if (new_size>frame_size)
821 return -1; 648 return -1;
822 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs && 649 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs &&
823 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs) 650 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs &&
651 50*new_size!=4*Fs && 50*new_size!=5*Fs && 50*new_size!=6*Fs)
824 return -1; 652 return -1;
825 return new_size; 653 return new_size;
826 } 654 }
827 655
828 opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size,
829 int variable_duration, int C, opus_int32 Fs, int bitrate_bps,
830 int delay_compensation, downmix_func downmix
831 #ifndef DISABLE_FLOAT_API
832 , float *subframe_mem
833 #endif
834 )
835 {
836 #ifndef DISABLE_FLOAT_API
837 if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200)
838 {
839 int LM = 3;
840 LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps,
841 0, subframe_mem, delay_compensation, downmix);
842 while ((Fs/400<<LM)>frame_size)
843 LM--;
844 frame_size = (Fs/400<<LM);
845 } else
846 #else
847 (void)analysis_pcm;
848 (void)C;
849 (void)bitrate_bps;
850 (void)delay_compensation;
851 (void)downmix;
852 #endif
853 {
854 frame_size = frame_size_select(frame_size, variable_duration, Fs);
855 }
856 if (frame_size<0)
857 return -1;
858 return frame_size;
859 }
860
861 opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int3 2 Fs, StereoWidthState *mem) 656 opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int3 2 Fs, StereoWidthState *mem)
862 { 657 {
863 opus_val32 xx, xy, yy; 658 opus_val32 xx, xy, yy;
864 opus_val16 sqrt_xx, sqrt_yy; 659 opus_val16 sqrt_xx, sqrt_yy;
865 opus_val16 qrrt_xx, qrrt_yy; 660 opus_val16 qrrt_xx, qrrt_yy;
866 int frame_rate; 661 int frame_rate;
867 int i; 662 int i;
868 opus_val16 short_alpha; 663 opus_val16 short_alpha;
869 664
870 frame_rate = Fs/frame_size; 665 frame_rate = Fs/frame_size;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ld iff); 722 width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ld iff);
928 /* Smoothing over one second */ 723 /* Smoothing over one second */
929 mem->smoothed_width += (width-mem->smoothed_width)/frame_rate; 724 mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
930 /* Peak follower */ 725 /* Peak follower */
931 mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width); 726 mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
932 } 727 }
933 /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(f loat)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE) ;*/ 728 /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(f loat)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE) ;*/
934 return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower))); 729 return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)));
935 } 730 }
936 731
732 static int decide_fec(int useInBandFEC, int PacketLoss_perc, int last_fec, int m ode, int *bandwidth, opus_int32 rate)
733 {
734 int orig_bandwidth;
735 if (!useInBandFEC || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY)
736 return 0;
737 orig_bandwidth = *bandwidth;
738 for (;;)
739 {
740 opus_int32 hysteresis;
741 opus_int32 LBRR_rate_thres_bps;
742 /* Compute threshold for using FEC at the current bandwidth setting */
743 LBRR_rate_thres_bps = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROW BAND)];
744 hysteresis = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1 ];
745 if (last_fec == 1) LBRR_rate_thres_bps -= hysteresis;
746 if (last_fec == 0) LBRR_rate_thres_bps += hysteresis;
747 LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps,
748 125 - silk_min( PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) );
749 /* If loss <= 5%, we look at whether we have enough rate to enable FEC.
750 If loss > 5%, we decrease the bandwidth until we can enable FEC. */
751 if (rate > LBRR_rate_thres_bps)
752 return 1;
753 else if (PacketLoss_perc <= 5)
754 return 0;
755 else if (*bandwidth > OPUS_BANDWIDTH_NARROWBAND)
756 (*bandwidth)--;
757 else
758 break;
759 }
760 /* Couldn't find any bandwidth to enable FEC, keep original bandwidth. */
761 *bandwidth = orig_bandwidth;
762 return 0;
763 }
764
765 static int compute_silk_rate_for_hybrid(int rate, int bandwidth, int frame20ms, int vbr, int fec) {
766 int entry;
767 int i;
768 int N;
769 int silk_rate;
770 static int rate_table[][5] = {
771 /* |total| |-------- SILK------------|
772 |-- No FEC -| |--- FEC ---|
773 10ms 20ms 10ms 20ms */
774 { 0, 0, 0, 0, 0},
775 {12000, 10000, 10000, 11000, 11000},
776 {16000, 13500, 13500, 15000, 15000},
777 {20000, 16000, 16000, 18000, 18000},
778 {24000, 18000, 18000, 21000, 21000},
779 {32000, 22000, 22000, 28000, 28000},
780 {64000, 38000, 38000, 50000, 50000}
781 };
782 entry = 1 + frame20ms + 2*fec;
783 N = sizeof(rate_table)/sizeof(rate_table[0]);
784 for (i=1;i<N;i++)
785 {
786 if (rate_table[i][0] > rate) break;
787 }
788 if (i == N)
789 {
790 silk_rate = rate_table[i-1][entry];
791 /* For now, just give 50% of the extra bits to SILK. */
792 silk_rate += (rate-rate_table[i-1][0])/2;
793 } else {
794 opus_int32 lo, hi, x0, x1;
795 lo = rate_table[i-1][entry];
796 hi = rate_table[i][entry];
797 x0 = rate_table[i-1][0];
798 x1 = rate_table[i][0];
799 silk_rate = (lo*(x1-rate) + hi*(rate-x0))/(x1-x0);
800 }
801 if (!vbr)
802 {
803 /* Tiny boost to SILK for CBR. We should probably tune this better. */
804 silk_rate += 100;
805 }
806 if (bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND)
807 silk_rate += 300;
808 return silk_rate;
809 }
810
811 /* Returns the equivalent bitrate corresponding to 20 ms frames,
812 complexity 10 VBR operation. */
813 static opus_int32 compute_equiv_rate(opus_int32 bitrate, int channels,
814 int frame_rate, int vbr, int mode, int complexity, int loss)
815 {
816 opus_int32 equiv;
817 equiv = bitrate;
818 /* Take into account overhead from smaller frames. */
819 equiv -= (40*channels+20)*(frame_rate - 50);
820 /* CBR is about a 8% penalty for both SILK and CELT. */
821 if (!vbr)
822 equiv -= equiv/12;
823 /* Complexity makes about 10% difference (from 0 to 10) in general. */
824 equiv = equiv * (90+complexity)/100;
825 if (mode == MODE_SILK_ONLY || mode == MODE_HYBRID)
826 {
827 /* SILK complexity 0-1 uses the non-delayed-decision NSQ, which
828 costs about 20%. */
829 if (complexity<2)
830 equiv = equiv*4/5;
831 equiv -= equiv*loss/(6*loss + 10);
832 } else if (mode == MODE_CELT_ONLY) {
833 /* CELT complexity 0-4 doesn't have the pitch filter, which costs
834 about 10%. */
835 if (complexity<5)
836 equiv = equiv*9/10;
837 } else {
838 /* Mode not known yet */
839 /* Half the SILK loss*/
840 equiv -= equiv*loss/(12*loss + 20);
841 }
842 return equiv;
843 }
844
845 #ifndef DISABLE_FLOAT_API
846
847 static int is_digital_silence(const opus_val16* pcm, int frame_size, int channel s, int lsb_depth)
848 {
849 int silence = 0;
850 opus_val32 sample_max = 0;
851 #ifdef MLP_TRAINING
852 return 0;
853 #endif
854 sample_max = celt_maxabs16(pcm, frame_size*channels);
855
856 #ifdef FIXED_POINT
857 silence = (sample_max == 0);
858 (void)lsb_depth;
859 #else
860 silence = (sample_max <= (opus_val16) 1 / (1 << lsb_depth));
861 #endif
862
863 return silence;
864 }
865
866 #ifdef FIXED_POINT
867 static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, in t channels, int arch)
868 {
869 int i;
870 opus_val32 sample_max;
871 int max_shift;
872 int shift;
873 opus_val32 energy = 0;
874 int len = frame_size*channels;
875 (void)arch;
876 /* Max amplitude in the signal */
877 sample_max = celt_maxabs16(pcm, len);
878
879 /* Compute the right shift required in the MAC to avoid an overflow */
880 max_shift = celt_ilog2(len);
881 shift = IMAX(0, (celt_ilog2(sample_max) << 1) + max_shift - 28);
882
883 /* Compute the energy */
884 for (i=0; i<len; i++)
885 energy += SHR32(MULT16_16(pcm[i], pcm[i]), shift);
886
887 /* Normalize energy by the frame size and left-shift back to the original pos ition */
888 energy /= len;
889 energy = SHL32(energy, shift);
890
891 return energy;
892 }
893 #else
894 static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, in t channels, int arch)
895 {
896 int len = frame_size*channels;
897 return celt_inner_prod(pcm, pcm, len, arch)/len;
898 }
899 #endif
900
901 /* Decides if DTX should be turned on (=1) or off (=0) */
902 static int decide_dtx_mode(float activity_probability, /* probability that cu rrent frame contains speech/music */
903 int *nb_no_activity_frames, /* number of consecuti ve frames with no activity */
904 opus_val32 peak_signal_energy, /* peak energy of desi red signal detected so far */
905 const opus_val16 *pcm, /* input pcm signal */
906 int frame_size, /* frame size */
907 int channels,
908 int is_silence, /* only digital silen ce detected in this frame */
909 int arch
910 )
911 {
912 opus_val32 noise_energy;
913
914 if (!is_silence)
915 {
916 if (activity_probability < DTX_ACTIVITY_THRESHOLD) /* is noise */
917 {
918 noise_energy = compute_frame_energy(pcm, frame_size, channels, arch);
919
920 /* but is sufficiently quiet */
921 is_silence = peak_signal_energy >= (PSEUDO_SNR_THRESHOLD * noise_energy );
922 }
923 }
924
925 if (is_silence)
926 {
927 /* The number of consecutive DTX frames should be within the allowed bound s */
928 (*nb_no_activity_frames)++;
929
930 if (*nb_no_activity_frames > NB_SPEECH_FRAMES_BEFORE_DTX)
931 {
932 if (*nb_no_activity_frames <= (NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSEC UTIVE_DTX))
933 /* Valid frame for DTX! */
934 return 1;
935 else
936 (*nb_no_activity_frames) = NB_SPEECH_FRAMES_BEFORE_DTX;
937 }
938 } else
939 (*nb_no_activity_frames) = 0;
940
941 return 0;
942 }
943
944 #endif
945
946 static opus_int32 encode_multiframe_packet(OpusEncoder *st,
947 const opus_val16 *pcm,
948 int nb_frames,
949 int frame_size,
950 unsigned char *data,
951 opus_int32 out_data_bytes,
952 int to_celt,
953 int lsb_depth,
954 int float_api)
955 {
956 int i;
957 int ret = 0;
958 VARDECL(unsigned char, tmp_data);
959 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
960 VARDECL(OpusRepacketizer, rp);
961 int max_header_bytes;
962 opus_int32 bytes_per_frame;
963 opus_int32 cbr_bytes;
964 opus_int32 repacketize_len;
965 int tmp_len;
966 ALLOC_STACK;
967
968 /* Worst cases:
969 * 2 frames: Code 2 with different compressed sizes
970 * >2 frames: Code 3 VBR */
971 max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
972
973 if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
974 repacketize_len = out_data_bytes;
975 else {
976 cbr_bytes = 3*st->bitrate_bps/(3*8*st->Fs/(frame_size*nb_frames));
977 repacketize_len = IMIN(cbr_bytes, out_data_bytes);
978 }
979 bytes_per_frame = IMIN(1276, 1+(repacketize_len-max_header_bytes)/nb_frames);
980
981 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
982 ALLOC(rp, 1, OpusRepacketizer);
983 opus_repacketizer_init(rp);
984
985 bak_mode = st->user_forced_mode;
986 bak_bandwidth = st->user_bandwidth;
987 bak_channels = st->force_channels;
988
989 st->user_forced_mode = st->mode;
990 st->user_bandwidth = st->bandwidth;
991 st->force_channels = st->stream_channels;
992
993 bak_to_mono = st->silk_mode.toMono;
994 if (bak_to_mono)
995 st->force_channels = 1;
996 else
997 st->prev_channels = st->stream_channels;
998
999 for (i=0;i<nb_frames;i++)
1000 {
1001 st->silk_mode.toMono = 0;
1002 st->nonfinal_frame = i<(nb_frames-1);
1003
1004 /* When switching from SILK/Hybrid to CELT, only ask for a switch at the l ast frame */
1005 if (to_celt && i==nb_frames-1)
1006 st->user_forced_mode = MODE_CELT_ONLY;
1007
1008 tmp_len = opus_encode_native(st, pcm+i*(st->channels*frame_size), frame_si ze,
1009 tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth, NULL, 0, 0, 0, 0,
1010 NULL, float_api);
1011
1012 if (tmp_len<0)
1013 {
1014 RESTORE_STACK;
1015 return OPUS_INTERNAL_ERROR;
1016 }
1017
1018 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
1019
1020 if (ret<0)
1021 {
1022 RESTORE_STACK;
1023 return OPUS_INTERNAL_ERROR;
1024 }
1025 }
1026
1027 ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_le n, 0, !st->use_vbr);
1028
1029 if (ret<0)
1030 {
1031 RESTORE_STACK;
1032 return OPUS_INTERNAL_ERROR;
1033 }
1034
1035 /* Discard configs that were forced locally for the purpose of repacketizatio n */
1036 st->user_forced_mode = bak_mode;
1037 st->user_bandwidth = bak_bandwidth;
1038 st->force_channels = bak_channels;
1039 st->silk_mode.toMono = bak_to_mono;
1040
1041 RESTORE_STACK;
1042 return ret;
1043 }
1044
1045 static int compute_redundancy_bytes(opus_int32 max_data_bytes, opus_int32 bitrat e_bps, int frame_rate, int channels)
1046 {
1047 int redundancy_bytes_cap;
1048 int redundancy_bytes;
1049 opus_int32 redundancy_rate;
1050 int base_bits;
1051 opus_int32 available_bits;
1052 base_bits = (40*channels+20);
1053
1054 /* Equivalent rate for 5 ms frames. */
1055 redundancy_rate = bitrate_bps + base_bits*(200 - frame_rate);
1056 /* For VBR, further increase the bitrate if we can afford it. It's pretty sho rt
1057 and we'll avoid artefacts. */
1058 redundancy_rate = 3*redundancy_rate/2;
1059 redundancy_bytes = redundancy_rate/1600;
1060
1061 /* Compute the max rate we can use given CBR or VBR with cap. */
1062 available_bits = max_data_bytes*8 - 2*base_bits;
1063 redundancy_bytes_cap = (available_bits*240/(240+48000/frame_rate) + base_bits )/8;
1064 redundancy_bytes = IMIN(redundancy_bytes, redundancy_bytes_cap);
1065 /* It we can't get enough bits for redundancy to be worth it, rely on the dec oder PLC. */
1066 if (redundancy_bytes > 4 + 8*channels)
1067 redundancy_bytes = IMIN(257, redundancy_bytes);
1068 else
1069 redundancy_bytes = 0;
1070 return redundancy_bytes;
1071 }
1072
937 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_ size, 1073 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_ size,
938 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth, 1074 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
939 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, 1075 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
940 int analysis_channels, downmix_func downmix, int float_api) 1076 int analysis_channels, downmix_func downmix, int float_api)
941 { 1077 {
942 void *silk_enc; 1078 void *silk_enc;
943 CELTEncoder *celt_enc; 1079 CELTEncoder *celt_enc;
944 int i; 1080 int i;
945 int ret=0; 1081 int ret=0;
946 opus_int32 nBytes; 1082 opus_int32 nBytes;
(...skipping 17 matching lines...) Expand all
964 int curr_bandwidth; 1100 int curr_bandwidth;
965 opus_val16 HB_gain; 1101 opus_val16 HB_gain;
966 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ 1102 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
967 int total_buffer; 1103 int total_buffer;
968 opus_val16 stereo_width; 1104 opus_val16 stereo_width;
969 const CELTMode *celt_mode; 1105 const CELTMode *celt_mode;
970 #ifndef DISABLE_FLOAT_API 1106 #ifndef DISABLE_FLOAT_API
971 AnalysisInfo analysis_info; 1107 AnalysisInfo analysis_info;
972 int analysis_read_pos_bak=-1; 1108 int analysis_read_pos_bak=-1;
973 int analysis_read_subframe_bak=-1; 1109 int analysis_read_subframe_bak=-1;
1110 int is_silence = 0;
974 #endif 1111 #endif
975 VARDECL(opus_val16, tmp_prefill); 1112 VARDECL(opus_val16, tmp_prefill);
976 1113
977 ALLOC_STACK; 1114 ALLOC_STACK;
978 1115
979 max_data_bytes = IMIN(1276, out_data_bytes); 1116 max_data_bytes = IMIN(1276, out_data_bytes);
980 1117
981 st->rangeFinal = 0; 1118 st->rangeFinal = 0;
982 if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs && 1119 if (frame_size <= 0 || max_data_bytes <= 0)
983 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size ! = 3*st->Fs)
984 || (400*frame_size < st->Fs)
985 || max_data_bytes<=0
986 )
987 { 1120 {
988 RESTORE_STACK; 1121 RESTORE_STACK;
989 return OPUS_BAD_ARG; 1122 return OPUS_BAD_ARG;
990 } 1123 }
1124
1125 /* Cannot encode 100 ms in 1 byte */
1126 if (max_data_bytes==1 && st->Fs==(frame_size*10))
1127 {
1128 RESTORE_STACK;
1129 return OPUS_BUFFER_TOO_SMALL;
1130 }
1131
991 silk_enc = (char*)st+st->silk_enc_offset; 1132 silk_enc = (char*)st+st->silk_enc_offset;
992 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 1133 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
993 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1134 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
994 delay_compensation = 0; 1135 delay_compensation = 0;
995 else 1136 else
996 delay_compensation = st->delay_compensation; 1137 delay_compensation = st->delay_compensation;
997 1138
998 lsb_depth = IMIN(lsb_depth, st->lsb_depth); 1139 lsb_depth = IMIN(lsb_depth, st->lsb_depth);
999 1140
1000 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); 1141 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1001 #ifndef DISABLE_FLOAT_API 1142 #ifndef DISABLE_FLOAT_API
1002 analysis_info.valid = 0; 1143 analysis_info.valid = 0;
1003 #ifdef FIXED_POINT 1144 #ifdef FIXED_POINT
1004 if (st->silk_mode.complexity >= 10 && st->Fs==48000) 1145 if (st->silk_mode.complexity >= 10 && st->Fs>=16000)
1005 #else 1146 #else
1006 if (st->silk_mode.complexity >= 7 && st->Fs==48000) 1147 if (st->silk_mode.complexity >= 7 && st->Fs>=16000)
1007 #endif 1148 #endif
1008 { 1149 {
1009 analysis_read_pos_bak = st->analysis.read_pos; 1150 if (is_digital_silence(pcm, frame_size, st->channels, lsb_depth))
1010 analysis_read_subframe_bak = st->analysis.read_subframe; 1151 {
1011 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame _size, 1152 is_silence = 1;
1012 c1, c2, analysis_channels, st->Fs, 1153 } else {
1013 lsb_depth, downmix, &analysis_info); 1154 analysis_read_pos_bak = st->analysis.read_pos;
1155 analysis_read_subframe_bak = st->analysis.read_subframe;
1156 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, fr ame_size,
1157 c1, c2, analysis_channels, st->Fs,
1158 lsb_depth, downmix, &analysis_info);
1159 }
1160
1161 /* Track the peak signal energy */
1162 if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRE SHOLD)
1163 st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st- >peak_signal_energy),
1164 compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1014 } 1165 }
1015 #else 1166 #else
1016 (void)analysis_pcm; 1167 (void)analysis_pcm;
1017 (void)analysis_size; 1168 (void)analysis_size;
1169 (void)c1;
1170 (void)c2;
1171 (void)analysis_channels;
1172 (void)downmix;
1018 #endif 1173 #endif
1019 1174
1020 st->voice_ratio = -1; 1175 #ifndef DISABLE_FLOAT_API
1176 /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1177 * Otherwise, preserve voice_ratio from the last non-silent frame */
1178 if (!is_silence)
1179 st->voice_ratio = -1;
1021 1180
1022 #ifndef DISABLE_FLOAT_API
1023 st->detected_bandwidth = 0; 1181 st->detected_bandwidth = 0;
1024 if (analysis_info.valid) 1182 if (analysis_info.valid)
1025 { 1183 {
1026 int analysis_bandwidth; 1184 int analysis_bandwidth;
1027 if (st->signal_type == OPUS_AUTO) 1185 if (st->signal_type == OPUS_AUTO)
1028 st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob)); 1186 st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob));
1029 1187
1030 analysis_bandwidth = analysis_info.bandwidth; 1188 analysis_bandwidth = analysis_info.bandwidth;
1031 if (analysis_bandwidth<=12) 1189 if (analysis_bandwidth<=12)
1032 st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1190 st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1033 else if (analysis_bandwidth<=14) 1191 else if (analysis_bandwidth<=14)
1034 st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1192 st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1035 else if (analysis_bandwidth<=16) 1193 else if (analysis_bandwidth<=16)
1036 st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1194 st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1037 else if (analysis_bandwidth<=18) 1195 else if (analysis_bandwidth<=18)
1038 st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1196 st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1039 else 1197 else
1040 st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; 1198 st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1041 } 1199 }
1200 #else
1201 st->voice_ratio = -1;
1042 #endif 1202 #endif
1043 1203
1044 if (st->channels==2 && st->force_channels!=1) 1204 if (st->channels==2 && st->force_channels!=1)
1045 stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_m em); 1205 stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_m em);
1046 else 1206 else
1047 stereo_width = 0; 1207 stereo_width = 0;
1048 total_buffer = delay_compensation; 1208 total_buffer = delay_compensation;
1049 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes); 1209 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1050 1210
1051 frame_rate = st->Fs/frame_size; 1211 frame_rate = st->Fs/frame_size;
1052 if (!st->use_vbr) 1212 if (!st->use_vbr)
1053 { 1213 {
1054 int cbrBytes; 1214 int cbrBytes;
1055 /* Multiply by 3 to make sure the division is exact. */ 1215 /* Multiply by 12 to make sure the division is exact. */
1056 int frame_rate3 = 3*st->Fs/frame_size; 1216 int frame_rate12 = 12*st->Fs/frame_size;
1057 /* We need to make sure that "int" values always fit in 16 bits. */ 1217 /* We need to make sure that "int" values always fit in 16 bits. */
1058 cbrBytes = IMIN( (3*st->bitrate_bps/8 + frame_rate3/2)/frame_rate3, max_d ata_bytes); 1218 cbrBytes = IMIN( (12*st->bitrate_bps/8 + frame_rate12/2)/frame_rate12, ma x_data_bytes);
1059 st->bitrate_bps = cbrBytes*(opus_int32)frame_rate3*8/3; 1219 st->bitrate_bps = cbrBytes*(opus_int32)frame_rate12*8/12;
1060 max_data_bytes = cbrBytes; 1220 /* Make sure we provide at least one byte to avoid failing. */
1221 max_data_bytes = IMAX(1, cbrBytes);
1061 } 1222 }
1062 if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8 1223 if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1063 || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400))) 1224 || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400)))
1064 { 1225 {
1065 /*If the space is too low to do something useful, emit 'PLC' frames.*/ 1226 /*If the space is too low to do something useful, emit 'PLC' frames.*/
1066 int tocmode = st->mode; 1227 int tocmode = st->mode;
1067 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth; 1228 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1229 int packet_code = 0;
1230 int num_multiframes = 0;
1231
1068 if (tocmode==0) 1232 if (tocmode==0)
1069 tocmode = MODE_SILK_ONLY; 1233 tocmode = MODE_SILK_ONLY;
1070 if (frame_rate>100) 1234 if (frame_rate>100)
1071 tocmode = MODE_CELT_ONLY; 1235 tocmode = MODE_CELT_ONLY;
1072 if (frame_rate < 50) 1236 /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1073 tocmode = MODE_SILK_ONLY; 1237 if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1238 {
1239 frame_rate = 50;
1240 packet_code = 1;
1241 }
1242
1243 /* >= 60 ms frames */
1244 if (frame_rate<=16)
1245 {
1246 /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1247 if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1248 {
1249 tocmode = MODE_SILK_ONLY;
1250
1251 packet_code = frame_rate <= 12;
1252 frame_rate = frame_rate == 12 ? 25 : 16;
1253 }
1254 else
1255 {
1256 num_multiframes = 50/frame_rate;
1257 frame_rate = 50;
1258 packet_code = 3;
1259 }
1260 }
1261
1074 if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND) 1262 if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1075 bw=OPUS_BANDWIDTH_WIDEBAND; 1263 bw=OPUS_BANDWIDTH_WIDEBAND;
1076 else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND) 1264 else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1077 bw=OPUS_BANDWIDTH_NARROWBAND; 1265 bw=OPUS_BANDWIDTH_NARROWBAND;
1078 else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND) 1266 else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1079 bw=OPUS_BANDWIDTH_SUPERWIDEBAND; 1267 bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1268
1080 data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels); 1269 data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1081 ret = 1; 1270 data[0] |= packet_code;
1271
1272 ret = packet_code <= 1 ? 1 : 2;
1273
1274 max_data_bytes = IMAX(max_data_bytes, ret);
1275
1276 if (packet_code==3)
1277 data[1] = num_multiframes;
1278
1082 if (!st->use_vbr) 1279 if (!st->use_vbr)
1083 { 1280 {
1084 ret = opus_packet_pad(data, ret, max_data_bytes); 1281 ret = opus_packet_pad(data, ret, max_data_bytes);
1085 if (ret == OPUS_OK) 1282 if (ret == OPUS_OK)
1086 ret = max_data_bytes; 1283 ret = max_data_bytes;
1284 else
1285 ret = OPUS_INTERNAL_ERROR;
1087 } 1286 }
1088 RESTORE_STACK; 1287 RESTORE_STACK;
1089 return ret; 1288 return ret;
1090 } 1289 }
1091 max_rate = frame_rate*max_data_bytes*8; 1290 max_rate = frame_rate*max_data_bytes*8;
1092 1291
1093 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ 1292 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1094 equiv_rate = st->bitrate_bps - (40*st->channels+20)*(st->Fs/frame_size - 50) ; 1293 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_ size,
1294 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPerc entage);
1095 1295
1096 if (st->signal_type == OPUS_SIGNAL_VOICE) 1296 if (st->signal_type == OPUS_SIGNAL_VOICE)
1097 voice_est = 127; 1297 voice_est = 127;
1098 else if (st->signal_type == OPUS_SIGNAL_MUSIC) 1298 else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1099 voice_est = 0; 1299 voice_est = 0;
1100 else if (st->voice_ratio >= 0) 1300 else if (st->voice_ratio >= 0)
1101 { 1301 {
1102 voice_est = st->voice_ratio*327>>8; 1302 voice_est = st->voice_ratio*327>>8;
1103 /* For AUDIO, never be more than 90% confident of having speech */ 1303 /* For AUDIO, never be more than 90% confident of having speech */
1104 if (st->application == OPUS_APPLICATION_AUDIO) 1304 if (st->application == OPUS_APPLICATION_AUDIO)
(...skipping 20 matching lines...) Expand all
1125 if (st->stream_channels == 2) 1325 if (st->stream_channels == 2)
1126 stereo_threshold -= 1000; 1326 stereo_threshold -= 1000;
1127 else 1327 else
1128 stereo_threshold += 1000; 1328 stereo_threshold += 1000;
1129 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; 1329 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1130 } else { 1330 } else {
1131 st->stream_channels = st->channels; 1331 st->stream_channels = st->channels;
1132 } 1332 }
1133 #endif 1333 #endif
1134 } 1334 }
1135 equiv_rate = st->bitrate_bps - (40*st->stream_channels+20)*(st->Fs/frame_siz e - 50); 1335 /* Update equivalent rate for channels decision. */
1336 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs /frame_size,
1337 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPerc entage);
1136 1338
1137 /* Mode selection depending on application and signal type */ 1339 /* Mode selection depending on application and signal type */
1138 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1340 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1139 { 1341 {
1140 st->mode = MODE_CELT_ONLY; 1342 st->mode = MODE_CELT_ONLY;
1141 } else if (st->user_forced_mode == OPUS_AUTO) 1343 } else if (st->user_forced_mode == OPUS_AUTO)
1142 { 1344 {
1143 #ifdef FUZZING 1345 #ifdef FUZZING
1144 /* Random mode switching */ 1346 /* Random mode switching */
1145 if ((rand()&0xF)==0) 1347 if ((rand()&0xF)==0)
(...skipping 28 matching lines...) Expand all
1174 if (st->prev_mode == MODE_CELT_ONLY) 1376 if (st->prev_mode == MODE_CELT_ONLY)
1175 threshold -= 4000; 1377 threshold -= 4000;
1176 else if (st->prev_mode>0) 1378 else if (st->prev_mode>0)
1177 threshold += 4000; 1379 threshold += 4000;
1178 1380
1179 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY; 1381 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1180 1382
1181 /* When FEC is enabled and there's enough packet loss, use SILK */ 1383 /* When FEC is enabled and there's enough packet loss, use SILK */
1182 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (1 28-voice_est)>>4) 1384 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (1 28-voice_est)>>4)
1183 st->mode = MODE_SILK_ONLY; 1385 st->mode = MODE_SILK_ONLY;
1184 /* When encoding voice and DTX is enabled, set the encoder to SILK mode ( at least for now) */ 1386 /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1387 because of complexity and sampling frequency settings, switch to SILK DTX and
1388 set the encoder to SILK mode */
1389 #ifndef DISABLE_FLOAT_API
1390 st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence );
1391 #else
1392 st->silk_mode.useDTX = st->use_dtx;
1393 #endif
1185 if (st->silk_mode.useDTX && voice_est > 100) 1394 if (st->silk_mode.useDTX && voice_est > 100)
1186 st->mode = MODE_SILK_ONLY; 1395 st->mode = MODE_SILK_ONLY;
1187 #endif 1396 #endif
1397
1398 /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mod e */
1399 if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1400 st->mode = MODE_CELT_ONLY;
1188 } else { 1401 } else {
1189 st->mode = st->user_forced_mode; 1402 st->mode = st->user_forced_mode;
1190 } 1403 }
1191 1404
1192 /* Override the chosen mode to make sure we meet the requested frame size */ 1405 /* Override the chosen mode to make sure we meet the requested frame size */
1193 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100) 1406 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1194 st->mode = MODE_CELT_ONLY; 1407 st->mode = MODE_CELT_ONLY;
1195 if (st->lfe) 1408 if (st->lfe)
1196 st->mode = MODE_CELT_ONLY; 1409 st->mode = MODE_CELT_ONLY;
1197 /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode * /
1198 if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
1199 st->mode = MODE_CELT_ONLY;
1200
1201 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMon o==0
1202 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1203 {
1204 /* Delay stereo->mono transition by two frames so that SILK can do a smoo th downmix */
1205 st->silk_mode.toMono = 1;
1206 st->stream_channels = 2;
1207 } else {
1208 st->silk_mode.toMono = 0;
1209 }
1210 1410
1211 if (st->prev_mode > 0 && 1411 if (st->prev_mode > 0 &&
1212 ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) || 1412 ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1213 (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY))) 1413 (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1214 { 1414 {
1215 redundancy = 1; 1415 redundancy = 1;
1216 celt_to_silk = (st->mode != MODE_CELT_ONLY); 1416 celt_to_silk = (st->mode != MODE_CELT_ONLY);
1217 if (!celt_to_silk) 1417 if (!celt_to_silk)
1218 { 1418 {
1219 /* Switch to SILK/hybrid if frame size is 10 ms or more*/ 1419 /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1220 if (frame_size >= st->Fs/100) 1420 if (frame_size >= st->Fs/100)
1221 { 1421 {
1222 st->mode = st->prev_mode; 1422 st->mode = st->prev_mode;
1223 to_celt = 1; 1423 to_celt = 1;
1224 } else { 1424 } else {
1225 redundancy=0; 1425 redundancy=0;
1226 } 1426 }
1227 } 1427 }
1228 } 1428 }
1229 /* For the first frame at a new SILK bandwidth */ 1429
1230 if (st->silk_bw_switch) 1430 /* When encoding multiframes, we can ask for a switch to CELT only in the la st frame. This switch
1431 * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1432 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMon o==0
1433 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1231 { 1434 {
1232 redundancy = 1; 1435 /* Delay stereo->mono transition by two frames so that SILK can do a smoo th downmix */
1233 celt_to_silk = 1; 1436 st->silk_mode.toMono = 1;
1234 st->silk_bw_switch = 0; 1437 st->stream_channels = 2;
1235 prefill=1; 1438 } else {
1439 st->silk_mode.toMono = 0;
1236 } 1440 }
1237 1441
1238 if (redundancy) 1442 /* Update equivalent rate with mode decision. */
1239 { 1443 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs /frame_size,
1240 /* Fair share of the max size allowed */ 1444 st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetL ossPercentage);
1241 redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(fra me_size+st->Fs/200));
1242 /* For VBR, target the actual bitrate (subject to the limit above) */
1243 if (st->use_vbr)
1244 redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600);
1245 }
1246 1445
1247 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) 1446 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1248 { 1447 {
1249 silk_EncControlStruct dummy; 1448 silk_EncControlStruct dummy;
1250 silk_InitEncoder( silk_enc, st->arch, &dummy); 1449 silk_InitEncoder( silk_enc, st->arch, &dummy);
1251 prefill=1; 1450 prefill=1;
1252 } 1451 }
1253 1452
1254 /* Automatic (rate-dependent) bandwidth selection */ 1453 /* Automatic (rate-dependent) bandwidth selection */
1255 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthS witch) 1454 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthS witch)
1256 { 1455 {
1257 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_threshold s; 1456 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_threshold s;
1258 opus_int32 bandwidth_thresholds[8]; 1457 opus_int32 bandwidth_thresholds[8];
1259 int bandwidth = OPUS_BANDWIDTH_FULLBAND; 1458 int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1260 opus_int32 equiv_rate2;
1261 1459
1262 equiv_rate2 = equiv_rate;
1263 if (st->mode != MODE_CELT_ONLY)
1264 {
1265 /* Adjust the threshold +/- 10% depending on complexity */
1266 equiv_rate2 = equiv_rate2 * (45+st->silk_mode.complexity)/50;
1267 /* CBR is less efficient by ~1 kb/s */
1268 if (!st->use_vbr)
1269 equiv_rate2 -= 1000;
1270 }
1271 if (st->channels==2 && st->force_channels!=1) 1460 if (st->channels==2 && st->force_channels!=1)
1272 { 1461 {
1273 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds; 1462 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1274 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds; 1463 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1275 } else { 1464 } else {
1276 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds; 1465 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1277 music_bandwidth_thresholds = mono_music_bandwidth_thresholds; 1466 music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1278 } 1467 }
1279 /* Interpolate bandwidth thresholds depending on voice estimation */ 1468 /* Interpolate bandwidth thresholds depending on voice estimation */
1280 for (i=0;i<8;i++) 1469 for (i=0;i<8;i++)
1281 { 1470 {
1282 bandwidth_thresholds[i] = music_bandwidth_thresholds[i] 1471 bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1283 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music _bandwidth_thresholds[i]))>>14); 1472 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music _bandwidth_thresholds[i]))>>14);
1284 } 1473 }
1285 do { 1474 do {
1286 int threshold, hysteresis; 1475 int threshold, hysteresis;
1287 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMB AND)]; 1476 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMB AND)];
1288 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUM BAND)+1]; 1477 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUM BAND)+1];
1289 if (!st->first) 1478 if (!st->first)
1290 { 1479 {
1291 if (st->bandwidth >= bandwidth) 1480 if (st->auto_bandwidth >= bandwidth)
1292 threshold -= hysteresis; 1481 threshold -= hysteresis;
1293 else 1482 else
1294 threshold += hysteresis; 1483 threshold += hysteresis;
1295 } 1484 }
1296 if (equiv_rate2 >= threshold) 1485 if (equiv_rate >= threshold)
1297 break; 1486 break;
1298 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND); 1487 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1299 st->bandwidth = bandwidth; 1488 st->bandwidth = st->auto_bandwidth = bandwidth;
1300 /* Prevents any transition to SWB/FB until the SILK layer has fully 1489 /* Prevents any transition to SWB/FB until the SILK layer has fully
1301 switched to WB mode and turned the variable LP filter off */ 1490 switched to WB mode and turned the variable LP filter off */
1302 if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeW ithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1491 if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeW ithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1303 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1492 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1304 } 1493 }
1305 1494
1306 if (st->bandwidth>st->max_bandwidth) 1495 if (st->bandwidth>st->max_bandwidth)
1307 st->bandwidth = st->max_bandwidth; 1496 st->bandwidth = st->max_bandwidth;
1308 1497
1309 if (st->user_bandwidth != OPUS_AUTO) 1498 if (st->user_bandwidth != OPUS_AUTO)
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1531 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1343 else if (equiv_rate <= 44000*st->stream_channels) 1532 else if (equiv_rate <= 44000*st->stream_channels)
1344 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1533 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1345 else 1534 else
1346 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; 1535 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1347 1536
1348 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwi dth); 1537 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwi dth);
1349 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth); 1538 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1350 } 1539 }
1351 #endif 1540 #endif
1541 st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_m ode.packetLossPercentage,
1542 st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1352 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth)); 1543 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1353 1544
1354 /* CELT mode doesn't support mediumband, use wideband instead */ 1545 /* CELT mode doesn't support mediumband, use wideband instead */
1355 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) 1546 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND )
1356 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1547 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1357 if (st->lfe) 1548 if (st->lfe)
1358 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1549 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1359 1550
1360 /* Can't support higher than wideband for >20 ms frames */ 1551 curr_bandwidth = st->bandwidth;
1361 if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)) 1552
1553 /* Chooses the appropriate mode for speech
1554 *NEVER* switch to/from CELT-only mode here as this will invalidate some a ssumptions */
1555 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1556 st->mode = MODE_HYBRID;
1557 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1558 st->mode = MODE_SILK_ONLY;
1559
1560 /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CEL T-only modes */
1561 if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1362 { 1562 {
1363 VARDECL(unsigned char, tmp_data); 1563 int enc_frame_size;
1364 int nb_frames; 1564 int nb_frames;
1365 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono; 1565
1366 VARDECL(OpusRepacketizer, rp); 1566 if (st->mode == MODE_SILK_ONLY)
1367 opus_int32 bytes_per_frame; 1567 {
1368 opus_int32 repacketize_len; 1568 if (frame_size == 2*st->Fs/25) /* 80 ms -> 2x 40 ms */
1569 enc_frame_size = st->Fs/25;
1570 else if (frame_size == 3*st->Fs/25) /* 120 ms -> 2x 60 ms */
1571 enc_frame_size = 3*st->Fs/50;
1572 else /* 100 ms -> 5x 20 ms */
1573 enc_frame_size = st->Fs/50;
1574 }
1575 else
1576 enc_frame_size = st->Fs/50;
1577
1578 nb_frames = frame_size/enc_frame_size;
1369 1579
1370 #ifndef DISABLE_FLOAT_API 1580 #ifndef DISABLE_FLOAT_API
1371 if (analysis_read_pos_bak!= -1) 1581 if (analysis_read_pos_bak!= -1)
1372 { 1582 {
1373 st->analysis.read_pos = analysis_read_pos_bak; 1583 st->analysis.read_pos = analysis_read_pos_bak;
1374 st->analysis.read_subframe = analysis_read_subframe_bak; 1584 st->analysis.read_subframe = analysis_read_subframe_bak;
1375 } 1585 }
1376 #endif 1586 #endif
1377 1587
1378 nb_frames = frame_size > st->Fs/25 ? 3 : 2; 1588 ret = encode_multiframe_packet(st, pcm, nb_frames, enc_frame_size, data,
1379 bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames); 1589 out_data_bytes, to_celt, lsb_depth, float_ api);
1380 1590
1381 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
1382
1383 ALLOC(rp, 1, OpusRepacketizer);
1384 opus_repacketizer_init(rp);
1385
1386 bak_mode = st->user_forced_mode;
1387 bak_bandwidth = st->user_bandwidth;
1388 bak_channels = st->force_channels;
1389
1390 st->user_forced_mode = st->mode;
1391 st->user_bandwidth = st->bandwidth;
1392 st->force_channels = st->stream_channels;
1393 bak_to_mono = st->silk_mode.toMono;
1394
1395 if (bak_to_mono)
1396 st->force_channels = 1;
1397 else
1398 st->prev_channels = st->stream_channels;
1399 for (i=0;i<nb_frames;i++)
1400 {
1401 int tmp_len;
1402 st->silk_mode.toMono = 0;
1403 /* When switching from SILK/Hybrid to CELT, only ask for a switch at t he last frame */
1404 if (to_celt && i==nb_frames-1)
1405 st->user_forced_mode = MODE_CELT_ONLY;
1406 tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->F s/50,
1407 tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth,
1408 NULL, 0, c1, c2, analysis_channels, downmix, float_api);
1409 if (tmp_len<0)
1410 {
1411 RESTORE_STACK;
1412 return OPUS_INTERNAL_ERROR;
1413 }
1414 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
1415 if (ret<0)
1416 {
1417 RESTORE_STACK;
1418 return OPUS_INTERNAL_ERROR;
1419 }
1420 }
1421 if (st->use_vbr)
1422 repacketize_len = out_data_bytes;
1423 else
1424 repacketize_len = IMIN(3*st->bitrate_bps/(3*8*50/nb_frames), out_data_ bytes);
1425 ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketiz e_len, 0, !st->use_vbr);
1426 if (ret<0)
1427 {
1428 RESTORE_STACK;
1429 return OPUS_INTERNAL_ERROR;
1430 }
1431 st->user_forced_mode = bak_mode;
1432 st->user_bandwidth = bak_bandwidth;
1433 st->force_channels = bak_channels;
1434 st->silk_mode.toMono = bak_to_mono;
1435 RESTORE_STACK; 1591 RESTORE_STACK;
1436 return ret; 1592 return ret;
1437 } 1593 }
1438 curr_bandwidth = st->bandwidth;
1439 1594
1440 /* Chooses the appropriate mode for speech 1595 /* For the first frame at a new SILK bandwidth */
1441 *NEVER* switch to/from CELT-only mode here as this will invalidate some a ssumptions */ 1596 if (st->silk_bw_switch)
1442 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1597 {
1443 st->mode = MODE_HYBRID; 1598 redundancy = 1;
1444 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) 1599 celt_to_silk = 1;
1445 st->mode = MODE_SILK_ONLY; 1600 st->silk_bw_switch = 0;
1601 prefill=1;
1602 }
1603
1604 /* If we decided to go with CELT, make sure redundancy is off, no matter wha t
1605 we decided earlier. */
1606 if (st->mode == MODE_CELT_ONLY)
1607 redundancy = 0;
1608
1609 if (redundancy)
1610 {
1611 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_b ps, frame_rate, st->stream_channels);
1612 if (redundancy_bytes == 0)
1613 redundancy = 0;
1614 }
1446 1615
1447 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c urr_bandwidth); */ 1616 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c urr_bandwidth); */
1448 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame _size / (st->Fs * 8)) - 1; 1617 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame _size / (st->Fs * 8)) - 1;
1449 1618
1450 data += 1; 1619 data += 1;
1451 1620
1452 ec_enc_init(&enc, data, max_data_bytes-1); 1621 ec_enc_init(&enc, data, max_data_bytes-1);
1453 1622
1454 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16); 1623 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16);
1455 OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->c hannels], total_buffer*st->channels); 1624 OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->c hannels], total_buffer*st->channels);
1456 1625
1457 if (st->mode == MODE_CELT_ONLY) 1626 if (st->mode == MODE_CELT_ONLY)
1458 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 1627 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1459 else 1628 else
1460 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_ smth1_Q15; 1629 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_ smth1_Q15;
1461 1630
1462 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, 1631 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1463 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP _SMTH_COEF2, 16 ) ); 1632 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP _SMTH_COEF2, 16 ) );
1464 1633
1465 /* convert from log scale to Hertz */ 1634 /* convert from log scale to Hertz */
1466 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); 1635 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1467 1636
1468 if (st->application == OPUS_APPLICATION_VOIP) 1637 if (st->application == OPUS_APPLICATION_VOIP)
1469 { 1638 {
1470 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem , frame_size, st->channels, st->Fs); 1639 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem , frame_size, st->channels, st->Fs, st->arch);
1471 } else { 1640 } else {
1472 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_ size, st->channels, st->Fs); 1641 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_ size, st->channels, st->Fs);
1473 } 1642 }
1474 #ifndef FIXED_POINT 1643 #ifndef FIXED_POINT
1475 if (float_api) 1644 if (float_api)
1476 { 1645 {
1477 opus_val32 sum; 1646 opus_val32 sum;
1478 sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total _buffer*st->channels], frame_size*st->channels, st->arch); 1647 sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total _buffer*st->channels], frame_size*st->channels, st->arch);
1479 /* This should filter out both NaNs and ridiculous signals that could 1648 /* This should filter out both NaNs and ridiculous signals that could
1480 cause NaNs further down. */ 1649 cause NaNs further down. */
(...skipping 14 matching lines...) Expand all
1495 #ifdef FIXED_POINT 1664 #ifdef FIXED_POINT
1496 const opus_int16 *pcm_silk; 1665 const opus_int16 *pcm_silk;
1497 #else 1666 #else
1498 VARDECL(opus_int16, pcm_silk); 1667 VARDECL(opus_int16, pcm_silk);
1499 ALLOC(pcm_silk, st->channels*frame_size, opus_int16); 1668 ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
1500 #endif 1669 #endif
1501 1670
1502 /* Distribute bits between SILK and CELT */ 1671 /* Distribute bits between SILK and CELT */
1503 total_bitRate = 8 * bytes_target * frame_rate; 1672 total_bitRate = 8 * bytes_target * frame_rate;
1504 if( st->mode == MODE_HYBRID ) { 1673 if( st->mode == MODE_HYBRID ) {
1505 int HB_gain_ref;
1506 /* Base rate for SILK */ 1674 /* Base rate for SILK */
1507 st->silk_mode.bitRate = st->stream_channels * ( 5000 + 1000 * ( st-> Fs == 100 * frame_size ) ); 1675 st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
1508 if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) { 1676 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->si lk_mode.LBRR_coded);
1509 /* SILK gets 2/3 of the remaining bits */
1510 st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 2 / 3;
1511 } else { /* FULLBAND */
1512 /* SILK gets 3/5 of the remaining bits */
1513 st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 3 / 5;
1514 }
1515 /* Don't let SILK use more than 80% */
1516 if( st->silk_mode.bitRate > total_bitRate * 4/5 ) {
1517 st->silk_mode.bitRate = total_bitRate * 4/5;
1518 }
1519 if (!st->energy_masking) 1677 if (!st->energy_masking)
1520 { 1678 {
1521 /* Increasingly attenuate high band when it gets allocated fewer bits */ 1679 /* Increasingly attenuate high band when it gets allocated fewer bits */
1522 celt_rate = total_bitRate - st->silk_mode.bitRate; 1680 celt_rate = total_bitRate - st->silk_mode.bitRate;
1523 HB_gain_ref = (curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND) ? 3000 : 3600; 1681 HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024 , 10)), 1);
1524 HB_gain = SHL32((opus_val32)celt_rate, 9) / SHR32((opus_val32)cel t_rate + st->stream_channels * HB_gain_ref, 6);
1525 HB_gain = HB_gain < (opus_val32)Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE;
1526 } 1682 }
1527 } else { 1683 } else {
1528 /* SILK gets all bits */ 1684 /* SILK gets all bits */
1529 st->silk_mode.bitRate = total_bitRate; 1685 st->silk_mode.bitRate = total_bitRate;
1530 } 1686 }
1531 1687
1532 /* Surround masking for SILK */ 1688 /* Surround masking for SILK */
1533 if (st->energy_masking && st->use_vbr && !st->lfe) 1689 if (st->energy_masking && st->use_vbr && !st->lfe)
1534 { 1690 {
1535 opus_val32 mask_sum=0; 1691 opus_val32 mask_sum=0;
(...skipping 26 matching lines...) Expand all
1562 /* Conservative rate reduction, we cut the masking in half */ 1718 /* Conservative rate reduction, we cut the masking in half */
1563 masking_depth = mask_sum / end*st->channels; 1719 masking_depth = mask_sum / end*st->channels;
1564 masking_depth += QCONST16(.2f, DB_SHIFT); 1720 masking_depth += QCONST16(.2f, DB_SHIFT);
1565 rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_ SHIFT); 1721 rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_ SHIFT);
1566 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3); 1722 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
1567 /* Split the rate change between the SILK and CELT part for hybrid. * / 1723 /* Split the rate change between the SILK and CELT part for hybrid. * /
1568 if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPU S_BANDWIDTH_FULLBAND) 1724 if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPU S_BANDWIDTH_FULLBAND)
1569 st->silk_mode.bitRate += 3*rate_offset/5; 1725 st->silk_mode.bitRate += 3*rate_offset/5;
1570 else 1726 else
1571 st->silk_mode.bitRate += rate_offset; 1727 st->silk_mode.bitRate += rate_offset;
1572 bytes_target += rate_offset * frame_size / (8 * st->Fs);
1573 } 1728 }
1574 1729
1575 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; 1730 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
1576 st->silk_mode.nChannelsAPI = st->channels; 1731 st->silk_mode.nChannelsAPI = st->channels;
1577 st->silk_mode.nChannelsInternal = st->stream_channels; 1732 st->silk_mode.nChannelsInternal = st->stream_channels;
1578 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 1733 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1579 st->silk_mode.desiredInternalSampleRate = 8000; 1734 st->silk_mode.desiredInternalSampleRate = 8000;
1580 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 1735 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1581 st->silk_mode.desiredInternalSampleRate = 12000; 1736 st->silk_mode.desiredInternalSampleRate = 12000;
1582 } else { 1737 } else {
1583 silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDW IDTH_WIDEBAND ); 1738 silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDW IDTH_WIDEBAND );
1584 st->silk_mode.desiredInternalSampleRate = 16000; 1739 st->silk_mode.desiredInternalSampleRate = 16000;
1585 } 1740 }
1586 if( st->mode == MODE_HYBRID ) { 1741 if( st->mode == MODE_HYBRID ) {
1587 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */ 1742 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
1588 st->silk_mode.minInternalSampleRate = 16000; 1743 st->silk_mode.minInternalSampleRate = 16000;
1589 } else { 1744 } else {
1590 st->silk_mode.minInternalSampleRate = 8000; 1745 st->silk_mode.minInternalSampleRate = 8000;
1591 } 1746 }
1592 1747
1748 st->silk_mode.maxInternalSampleRate = 16000;
1593 if (st->mode == MODE_SILK_ONLY) 1749 if (st->mode == MODE_SILK_ONLY)
1594 { 1750 {
1595 opus_int32 effective_max_rate = max_rate; 1751 opus_int32 effective_max_rate = max_rate;
1596 st->silk_mode.maxInternalSampleRate = 16000;
1597 if (frame_rate > 50) 1752 if (frame_rate > 50)
1598 effective_max_rate = effective_max_rate*2/3; 1753 effective_max_rate = effective_max_rate*2/3;
1599 if (effective_max_rate < 13000) 1754 if (effective_max_rate < 8000)
1600 { 1755 {
1601 st->silk_mode.maxInternalSampleRate = 12000; 1756 st->silk_mode.maxInternalSampleRate = 12000;
1602 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mod e.desiredInternalSampleRate); 1757 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mod e.desiredInternalSampleRate);
1603 } 1758 }
1604 if (effective_max_rate < 9600) 1759 if (effective_max_rate < 7000)
1605 { 1760 {
1606 st->silk_mode.maxInternalSampleRate = 8000; 1761 st->silk_mode.maxInternalSampleRate = 8000;
1607 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode .desiredInternalSampleRate); 1762 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode .desiredInternalSampleRate);
1608 } 1763 }
1609 } else {
1610 st->silk_mode.maxInternalSampleRate = 16000;
1611 } 1764 }
1612 1765
1613 st->silk_mode.useCBR = !st->use_vbr; 1766 st->silk_mode.useCBR = !st->use_vbr;
1614 1767
1615 /* Call SILK encoder for the low band */ 1768 /* Call SILK encoder for the low band */
1616 nBytes = IMIN(1275, max_data_bytes-1-redundancy_bytes);
1617 1769
1618 st->silk_mode.maxBits = nBytes*8; 1770 /* Max bits for SILK, counting ToC, redundancy bytes, and optionally red undancy. */
1619 /* Only allow up to 90% of the bits for hybrid mode*/ 1771 st->silk_mode.maxBits = (max_data_bytes-1)*8;
1620 if (st->mode == MODE_HYBRID) 1772 if (redundancy && redundancy_bytes >= 2)
1621 st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10; 1773 {
1774 /* Counting 1 bit for redundancy position and 20 bits for flag+size ( only for hybrid). */
1775 st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
1776 if (st->mode == MODE_HYBRID)
1777 st->silk_mode.maxBits -= 20;
1778 }
1622 if (st->silk_mode.useCBR) 1779 if (st->silk_mode.useCBR)
1623 { 1780 {
1624 st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8; 1781 if (st->mode == MODE_HYBRID)
1625 /* Reduce the initial target to make it easier to reach the CBR rate */ 1782 {
1626 st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000); 1783 st->silk_mode.maxBits = IMIN(st->silk_mode.maxBits, st->silk_mode. bitRate * frame_size / st->Fs);
1784 }
1785 } else {
1786 /* Constrained VBR. */
1787 if (st->mode == MODE_HYBRID)
1788 {
1789 /* Compute SILK bitrate corresponding to the max total bits availa ble */
1790 opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode .maxBits*st->Fs / frame_size,
1791 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st-> silk_mode.LBRR_coded);
1792 st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
1793 }
1627 } 1794 }
1628 1795
1629 if (prefill) 1796 if (prefill)
1630 { 1797 {
1631 opus_int32 zero=0; 1798 opus_int32 zero=0;
1632 int prefill_offset; 1799 int prefill_offset;
1633 /* Use a smooth onset for the SILK prefill to avoid the encoder tryi ng to encode 1800 /* Use a smooth onset for the SILK prefill to avoid the encoder tryi ng to encode
1634 a discontinuity. The exact location is what we need to avoid leav ing any "gap" 1801 a discontinuity. The exact location is what we need to avoid leav ing any "gap"
1635 in the audio when mixing with the redundant CELT frame. Here we c an afford to 1802 in the audio when mixing with the redundant CELT frame. Here we c an afford to
1636 overwrite st->delay_buffer because the only thing that uses it be fore it gets 1803 overwrite st->delay_buffer because the only thing that uses it be fore it gets
(...skipping 18 matching lines...) Expand all
1655 for (i=0;i<frame_size*st->channels;i++) 1822 for (i=0;i<frame_size*st->channels;i++)
1656 pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]); 1823 pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]);
1657 #endif 1824 #endif
1658 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 ); 1825 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
1659 if( ret ) { 1826 if( ret ) {
1660 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/ 1827 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
1661 /* Handle error */ 1828 /* Handle error */
1662 RESTORE_STACK; 1829 RESTORE_STACK;
1663 return OPUS_INTERNAL_ERROR; 1830 return OPUS_INTERNAL_ERROR;
1664 } 1831 }
1665 if (nBytes==0) 1832
1666 {
1667 st->rangeFinal = 0;
1668 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->s tream_channels);
1669 RESTORE_STACK;
1670 return 1;
1671 }
1672 /* Extract SILK internal bandwidth for signaling in first byte */ 1833 /* Extract SILK internal bandwidth for signaling in first byte */
1673 if( st->mode == MODE_SILK_ONLY ) { 1834 if( st->mode == MODE_SILK_ONLY ) {
1674 if( st->silk_mode.internalSampleRate == 8000 ) { 1835 if( st->silk_mode.internalSampleRate == 8000 ) {
1675 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1836 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1676 } else if( st->silk_mode.internalSampleRate == 12000 ) { 1837 } else if( st->silk_mode.internalSampleRate == 12000 ) {
1677 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1838 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1678 } else if( st->silk_mode.internalSampleRate == 16000 ) { 1839 } else if( st->silk_mode.internalSampleRate == 16000 ) {
1679 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1840 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1680 } 1841 }
1681 } else { 1842 } else {
1682 silk_assert( st->silk_mode.internalSampleRate == 16000 ); 1843 silk_assert( st->silk_mode.internalSampleRate == 16000 );
1683 } 1844 }
1684 1845
1685 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady; 1846 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal _frame;
1847
1848 if (nBytes==0)
1849 {
1850 st->rangeFinal = 0;
1851 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->s tream_channels);
1852 RESTORE_STACK;
1853 return 1;
1854 }
1855
1686 /* FIXME: How do we allocate the redundancy for CBR? */ 1856 /* FIXME: How do we allocate the redundancy for CBR? */
1687 if (st->silk_mode.opusCanSwitch) 1857 if (st->silk_mode.opusCanSwitch)
1688 { 1858 {
1689 redundancy = 1; 1859 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitra te_bps, frame_rate, st->stream_channels);
1860 redundancy = (redundancy_bytes != 0);
1690 celt_to_silk = 0; 1861 celt_to_silk = 0;
1691 st->silk_bw_switch = 1; 1862 st->silk_bw_switch = 1;
1692 } 1863 }
1693 } 1864 }
1694 1865
1695 /* CELT processing */ 1866 /* CELT processing */
1696 { 1867 {
1697 int endband=21; 1868 int endband=21;
1698 1869
1699 switch(curr_bandwidth) 1870 switch(curr_bandwidth)
(...skipping 20 matching lines...) Expand all
1720 { 1891 {
1721 opus_val32 celt_pred=2; 1892 opus_val32 celt_pred=2;
1722 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); 1893 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1723 /* We may still decide to disable prediction later */ 1894 /* We may still decide to disable prediction later */
1724 if (st->silk_mode.reducedDependency) 1895 if (st->silk_mode.reducedDependency)
1725 celt_pred = 0; 1896 celt_pred = 0;
1726 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred)); 1897 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
1727 1898
1728 if (st->mode == MODE_HYBRID) 1899 if (st->mode == MODE_HYBRID)
1729 { 1900 {
1730 int len;
1731
1732 len = (ec_tell(&enc)+7)>>3;
1733 if (redundancy)
1734 len += st->mode == MODE_HYBRID ? 3 : 1;
1735 if( st->use_vbr ) { 1901 if( st->use_vbr ) {
1736 nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * f rame_size) / (8 * st->Fs); 1902 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st-> silk_mode.bitRate));
1737 } else { 1903 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
1738 /* check if SILK used up too much */
1739 nb_compr_bytes = len > bytes_target ? len : bytes_target;
1740 } 1904 }
1741 } else { 1905 } else {
1742 if (st->use_vbr) 1906 if (st->use_vbr)
1743 { 1907 {
1744 opus_int32 bonus=0;
1745 #ifndef DISABLE_FLOAT_API
1746 if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != st->Fs/50)
1747 {
1748 bonus = (60*st->stream_channels+40)*(st->Fs/frame_size-50);
1749 if (analysis_info.valid)
1750 bonus = (opus_int32)(bonus*(1.f+.5f*analysis_info.tonality ));
1751 }
1752 #endif
1753 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); 1908 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
1754 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_const raint)); 1909 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_const raint));
1755 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps+bonu s)); 1910 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
1756 nb_compr_bytes = max_data_bytes-1-redundancy_bytes;
1757 } else {
1758 nb_compr_bytes = bytes_target;
1759 } 1911 }
1760 } 1912 }
1761
1762 } else {
1763 nb_compr_bytes = 0;
1764 } 1913 }
1765 1914
1766 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16); 1915 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
1767 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0) 1916 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
1768 { 1917 {
1769 OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer -st->Fs/400)*st->channels], st->channels*st->Fs/400); 1918 OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer -st->Fs/400)*st->channels], st->channels*st->Fs/400);
1770 } 1919 }
1771 1920
1772 if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0) 1921 if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
1773 { 1922 {
1774 OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], s t->channels*(st->encoder_buffer-frame_size-total_buffer)); 1923 OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], s t->channels*(st->encoder_buffer-frame_size-total_buffer));
1775 OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-t otal_buffer)], 1924 OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-t otal_buffer)],
1776 &pcm_buf[0], 1925 &pcm_buf[0],
1777 (frame_size+total_buffer)*st->channels); 1926 (frame_size+total_buffer)*st->channels);
1778 } else { 1927 } else {
1779 OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder _buffer)*st->channels], st->encoder_buffer*st->channels); 1928 OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder _buffer)*st->channels], st->encoder_buffer*st->channels);
1780 } 1929 }
1781 /* gain_fade() and stereo_fade() need to be after the buffer copying 1930 /* gain_fade() and stereo_fade() need to be after the buffer copying
1782 because we don't want any of this to affect the SILK part */ 1931 because we don't want any of this to affect the SILK part */
1783 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) { 1932 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
1784 gain_fade(pcm_buf, pcm_buf, 1933 gain_fade(pcm_buf, pcm_buf,
1785 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->chan nels, celt_mode->window, st->Fs); 1934 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->chan nels, celt_mode->window, st->Fs);
1786 } 1935 }
1787 st->prev_HB_gain = HB_gain; 1936 st->prev_HB_gain = HB_gain;
1788 if (st->mode != MODE_HYBRID || st->stream_channels==1) 1937 if (st->mode != MODE_HYBRID || st->stream_channels==1)
1789 st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-30000)); 1938 st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-24000));
1790 if( !st->energy_masking && st->channels == 2 ) { 1939 if( !st->energy_masking && st->channels == 2 ) {
1791 /* Apply stereo width reduction (at low bitrates) */ 1940 /* Apply stereo width reduction (at low bitrates) */
1792 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth _Q14 < (1 << 14) ) { 1941 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth _Q14 < (1 << 14) ) {
1793 opus_val16 g1, g2; 1942 opus_val16 g1, g2;
1794 g1 = st->hybrid_stereo_width_Q14; 1943 g1 = st->hybrid_stereo_width_Q14;
1795 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14); 1944 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
1796 #ifdef FIXED_POINT 1945 #ifdef FIXED_POINT
1797 g1 = g1==16384 ? Q15ONE : SHL16(g1,1); 1946 g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
1798 g2 = g2==16384 ? Q15ONE : SHL16(g2,1); 1947 g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
1799 #else 1948 #else
1800 g1 *= (1.f/16384); 1949 g1 *= (1.f/16384);
1801 g2 *= (1.f/16384); 1950 g2 *= (1.f/16384);
1802 #endif 1951 #endif
1803 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, 1952 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
1804 frame_size, st->channels, celt_mode->window, st->Fs); 1953 frame_size, st->channels, celt_mode->window, st->Fs);
1805 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14; 1954 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
1806 } 1955 }
1807 } 1956 }
1808 1957
1809 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYB RID) <= 8*(max_data_bytes-1)) 1958 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYB RID) <= 8*(max_data_bytes-1))
1810 { 1959 {
1811 /* For SILK mode, the redundancy is inferred from the length */ 1960 /* For SILK mode, the redundancy is inferred from the length */
1812 if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_c ompr_bytes)) 1961 if (st->mode == MODE_HYBRID)
1813 ec_enc_bit_logp(&enc, redundancy, 12); 1962 ec_enc_bit_logp(&enc, redundancy, 12);
1814 if (redundancy) 1963 if (redundancy)
1815 { 1964 {
1816 int max_redundancy; 1965 int max_redundancy;
1817 ec_enc_bit_logp(&enc, celt_to_silk, 1); 1966 ec_enc_bit_logp(&enc, celt_to_silk, 1);
1818 if (st->mode == MODE_HYBRID) 1967 if (st->mode == MODE_HYBRID)
1819 max_redundancy = (max_data_bytes-1)-nb_compr_bytes; 1968 {
1969 /* Reserve the 8 bits needed for the redundancy length,
1970 and at least a few bits for CELT if possible */
1971 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
1972 }
1820 else 1973 else
1821 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3); 1974 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
1822 /* Target the same bit-rate for redundancy as for the rest, 1975 /* Target the same bit-rate for redundancy as for the rest,
1823 up to a max of 257 bytes */ 1976 up to a max of 257 bytes */
1824 redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); 1977 redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
1825 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); 1978 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
1826 if (st->mode == MODE_HYBRID) 1979 if (st->mode == MODE_HYBRID)
1827 ec_enc_uint(&enc, redundancy_bytes-2, 256); 1980 ec_enc_uint(&enc, redundancy_bytes-2, 256);
1828 } 1981 }
1829 } else { 1982 } else {
1830 redundancy = 0; 1983 redundancy = 0;
1831 } 1984 }
1832 1985
1833 if (!redundancy) 1986 if (!redundancy)
1834 { 1987 {
1835 st->silk_bw_switch = 0; 1988 st->silk_bw_switch = 0;
1836 redundancy_bytes = 0; 1989 redundancy_bytes = 0;
1837 } 1990 }
1838 if (st->mode != MODE_CELT_ONLY)start_band=17; 1991 if (st->mode != MODE_CELT_ONLY)start_band=17;
1839 1992
1840 if (st->mode == MODE_SILK_ONLY) 1993 if (st->mode == MODE_SILK_ONLY)
1841 { 1994 {
1842 ret = (ec_tell(&enc)+7)>>3; 1995 ret = (ec_tell(&enc)+7)>>3;
1843 ec_enc_done(&enc); 1996 ec_enc_done(&enc);
1844 nb_compr_bytes = ret; 1997 nb_compr_bytes = ret;
1845 } else { 1998 } else {
1846 nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes ); 1999 nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
1847 ec_enc_shrink(&enc, nb_compr_bytes); 2000 ec_enc_shrink(&enc, nb_compr_bytes);
1848 } 2001 }
1849 2002
1850 #ifndef DISABLE_FLOAT_API 2003 #ifndef DISABLE_FLOAT_API
1851 if (redundancy || st->mode != MODE_SILK_ONLY) 2004 if (redundancy || st->mode != MODE_SILK_ONLY)
1852 celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info)); 2005 celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info));
1853 #endif 2006 #endif
2007 if (st->mode == MODE_HYBRID) {
2008 SILKInfo info;
2009 info.signalType = st->silk_mode.signalType;
2010 info.offset = st->silk_mode.offset;
2011 celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2012 } else {
2013 celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO((SILKInfo*)NULL));
2014 }
1854 2015
1855 /* 5 ms redundant frame for CELT->SILK */ 2016 /* 5 ms redundant frame for CELT->SILK */
1856 if (redundancy && celt_to_silk) 2017 if (redundancy && celt_to_silk)
1857 { 2018 {
1858 int err; 2019 int err;
1859 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); 2020 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
1860 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); 2021 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2022 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
1861 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_b ytes, redundancy_bytes, NULL); 2023 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_b ytes, redundancy_bytes, NULL);
1862 if (err < 0) 2024 if (err < 0)
1863 { 2025 {
1864 RESTORE_STACK; 2026 RESTORE_STACK;
1865 return OPUS_INTERNAL_ERROR; 2027 return OPUS_INTERNAL_ERROR;
1866 } 2028 }
1867 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); 2029 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
1868 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 2030 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1869 } 2031 }
1870 2032
1871 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band)); 2033 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
1872 2034
1873 if (st->mode != MODE_SILK_ONLY) 2035 if (st->mode != MODE_SILK_ONLY)
1874 { 2036 {
1875 if (st->mode != st->prev_mode && st->prev_mode > 0) 2037 if (st->mode != st->prev_mode && st->prev_mode > 0)
1876 { 2038 {
1877 unsigned char dummy[2]; 2039 unsigned char dummy[2];
1878 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 2040 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1879 2041
1880 /* Prefilling */ 2042 /* Prefilling */
1881 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL ); 2043 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL );
1882 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); 2044 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
1883 } 2045 }
1884 /* If false, we already busted the budget and we'll end up with a "PLC p acket" */ 2046 /* If false, we already busted the budget and we'll end up with a "PLC f rame" */
1885 if (ec_tell(&enc) <= 8*nb_compr_bytes) 2047 if (ec_tell(&enc) <= 8*nb_compr_bytes)
1886 { 2048 {
2049 /* Set the bitrate again if it was overridden in the redundancy code above*/
2050 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && st->use_vb r)
2051 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->si lk_mode.bitRate));
2052 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
1887 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_com pr_bytes, &enc); 2053 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_com pr_bytes, &enc);
1888 if (ret < 0) 2054 if (ret < 0)
1889 { 2055 {
1890 RESTORE_STACK; 2056 RESTORE_STACK;
1891 return OPUS_INTERNAL_ERROR; 2057 return OPUS_INTERNAL_ERROR;
1892 } 2058 }
2059 /* Put CELT->SILK redundancy data in the right place. */
2060 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && st->use_vb r)
2061 {
2062 OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2063 nb_compr_bytes = nb_compr_bytes+redundancy_bytes;
2064 }
1893 } 2065 }
1894 } 2066 }
1895 2067
1896 /* 5 ms redundant frame for SILK->CELT */ 2068 /* 5 ms redundant frame for SILK->CELT */
1897 if (redundancy && !celt_to_silk) 2069 if (redundancy && !celt_to_silk)
1898 { 2070 {
1899 int err; 2071 int err;
1900 unsigned char dummy[2]; 2072 unsigned char dummy[2];
1901 int N2, N4; 2073 int N2, N4;
1902 N2 = st->Fs/200; 2074 N2 = st->Fs/200;
1903 N4 = st->Fs/400; 2075 N4 = st->Fs/400;
1904 2076
1905 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 2077 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1906 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); 2078 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
1907 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); 2079 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2080 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2081 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
1908 2082
2083 if (st->mode == MODE_HYBRID)
2084 {
2085 /* Shrink packet to what the encoder actually used. */
2086 nb_compr_bytes = ret;
2087 ec_enc_shrink(&enc, nb_compr_bytes);
2088 }
1909 /* NOTE: We could speed this up slightly (at the expense of code size) b y just adding a function that prefills the buffer */ 2089 /* NOTE: We could speed this up slightly (at the expense of code size) b y just adding a function that prefills the buffer */
1910 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N 4, dummy, 2, NULL); 2090 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N 4, dummy, 2, NULL);
1911 2091
1912 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2) , N2, data+nb_compr_bytes, redundancy_bytes, NULL); 2092 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2) , N2, data+nb_compr_bytes, redundancy_bytes, NULL);
1913 if (err < 0) 2093 if (err < 0)
1914 { 2094 {
1915 RESTORE_STACK; 2095 RESTORE_STACK;
1916 return OPUS_INTERNAL_ERROR; 2096 return OPUS_INTERNAL_ERROR;
1917 } 2097 }
1918 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); 2098 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
1919 } 2099 }
1920 2100
1921 2101
1922 2102
1923 /* Signalling the mode in the first byte */ 2103 /* Signalling the mode in the first byte */
1924 data--; 2104 data--;
1925 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_ch annels); 2105 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_ch annels);
1926 2106
1927 st->rangeFinal = enc.rng ^ redundant_rng; 2107 st->rangeFinal = enc.rng ^ redundant_rng;
1928 2108
1929 if (to_celt) 2109 if (to_celt)
1930 st->prev_mode = MODE_CELT_ONLY; 2110 st->prev_mode = MODE_CELT_ONLY;
1931 else 2111 else
1932 st->prev_mode = st->mode; 2112 st->prev_mode = st->mode;
1933 st->prev_channels = st->stream_channels; 2113 st->prev_channels = st->stream_channels;
1934 st->prev_framesize = frame_size; 2114 st->prev_framesize = frame_size;
1935 2115
1936 st->first = 0; 2116 st->first = 0;
1937 2117
2118 /* DTX decision */
2119 #ifndef DISABLE_FLOAT_API
2120 if (st->use_dtx && (analysis_info.valid || is_silence))
2121 {
2122 if (decide_dtx_mode(analysis_info.activity_probability, &st->nb_no_activi ty_frames,
2123 st->peak_signal_energy, pcm, frame_size, st->channels, is_silence, st->arch))
2124 {
2125 st->rangeFinal = 0;
2126 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->str eam_channels);
2127 RESTORE_STACK;
2128 return 1;
2129 }
2130 }
2131 #endif
2132
1938 /* In the unlikely case that the SILK encoder busted its target, tell 2133 /* In the unlikely case that the SILK encoder busted its target, tell
1939 the decoder to call the PLC */ 2134 the decoder to call the PLC */
1940 if (ec_tell(&enc) > (max_data_bytes-1)*8) 2135 if (ec_tell(&enc) > (max_data_bytes-1)*8)
1941 { 2136 {
1942 if (max_data_bytes < 2) 2137 if (max_data_bytes < 2)
1943 { 2138 {
1944 RESTORE_STACK; 2139 RESTORE_STACK;
1945 return OPUS_BUFFER_TOO_SMALL; 2140 return OPUS_BUFFER_TOO_SMALL;
1946 } 2141 }
1947 data[1] = 0; 2142 data[1] = 0;
1948 ret = 1; 2143 ret = 1;
1949 st->rangeFinal = 0; 2144 st->rangeFinal = 0;
1950 } else if (st->mode==MODE_SILK_ONLY&&!redundancy) 2145 } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
1951 { 2146 {
1952 /*When in LPC only mode it's perfectly 2147 /*When in LPC only mode it's perfectly
1953 reasonable to strip off trailing zero bytes as 2148 reasonable to strip off trailing zero bytes as
1954 the required range decoder behavior is to 2149 the required range decoder behavior is to
1955 fill these in. This can't be done when the MDCT 2150 fill these in. This can't be done when the MDCT
1956 modes are used because the decoder needs to know 2151 modes are used because the decoder needs to know
1957 the actual length for allocation purposes.*/ 2152 the actual length for allocation purposes.*/
1958 while(ret>2&&data[ret]==0)ret--; 2153 while(ret>2&&data[ret]==0)ret--;
1959 } 2154 }
1960 /* Count ToC and redundancy */ 2155 /* Count ToC and redundancy */
1961 ret += 1+redundancy_bytes; 2156 ret += 1+redundancy_bytes;
1962 if (!st->use_vbr) 2157 if (!st->use_vbr)
1963 { 2158 {
1964 if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK) 2159 if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK)
1965
1966 { 2160 {
1967 RESTORE_STACK; 2161 RESTORE_STACK;
1968 return OPUS_INTERNAL_ERROR; 2162 return OPUS_INTERNAL_ERROR;
1969 } 2163 }
1970 ret = max_data_bytes; 2164 ret = max_data_bytes;
1971 } 2165 }
1972 RESTORE_STACK; 2166 RESTORE_STACK;
1973 return ret; 2167 return ret;
1974 } 2168 }
1975 2169
1976 #ifdef FIXED_POINT 2170 #ifdef FIXED_POINT
1977 2171
1978 #ifndef DISABLE_FLOAT_API 2172 #ifndef DISABLE_FLOAT_API
1979 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size, 2173 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size,
1980 unsigned char *data, opus_int32 max_data_bytes) 2174 unsigned char *data, opus_int32 max_data_bytes)
1981 { 2175 {
1982 int i, ret; 2176 int i, ret;
1983 int frame_size; 2177 int frame_size;
1984 int delay_compensation;
1985 VARDECL(opus_int16, in); 2178 VARDECL(opus_int16, in);
1986 ALLOC_STACK; 2179 ALLOC_STACK;
1987 2180
1988 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 2181 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st ->Fs);
1989 delay_compensation = 0; 2182 if (frame_size <= 0)
1990 else 2183 {
1991 delay_compensation = st->delay_compensation; 2184 RESTORE_STACK;
1992 frame_size = compute_frame_size(pcm, analysis_frame_size, 2185 return OPUS_BAD_ARG;
1993 st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 2186 }
1994 delay_compensation, downmix_float, st->analysis.subframe_mem);
1995
1996 ALLOC(in, frame_size*st->channels, opus_int16); 2187 ALLOC(in, frame_size*st->channels, opus_int16);
1997 2188
1998 for (i=0;i<frame_size*st->channels;i++) 2189 for (i=0;i<frame_size*st->channels;i++)
1999 in[i] = FLOAT2INT16(pcm[i]); 2190 in[i] = FLOAT2INT16(pcm[i]);
2000 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, 2191 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2001 pcm, analysis_frame_size, 0, -2, st->channels, downm ix_float, 1); 2192 pcm, analysis_frame_size, 0, -2, st->channels, downm ix_float, 1);
2002 RESTORE_STACK; 2193 RESTORE_STACK;
2003 return ret; 2194 return ret;
2004 } 2195 }
2005 #endif 2196 #endif
2006 2197
2007 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size, 2198 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size,
2008 unsigned char *data, opus_int32 out_data_bytes) 2199 unsigned char *data, opus_int32 out_data_bytes)
2009 { 2200 {
2010 int frame_size; 2201 int frame_size;
2011 int delay_compensation; 2202 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st ->Fs);
2012 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2013 delay_compensation = 0;
2014 else
2015 delay_compensation = st->delay_compensation;
2016 frame_size = compute_frame_size(pcm, analysis_frame_size,
2017 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2018 delay_compensation, downmix_int
2019 #ifndef DISABLE_FLOAT_API
2020 , st->analysis.subframe_mem
2021 #endif
2022 );
2023 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, 2203 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16,
2024 pcm, analysis_frame_size, 0, -2, st->channels, down mix_int, 0); 2204 pcm, analysis_frame_size, 0, -2, st->channels, down mix_int, 0);
2025 } 2205 }
2026 2206
2027 #else 2207 #else
2028 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size, 2208 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size,
2029 unsigned char *data, opus_int32 max_data_bytes) 2209 unsigned char *data, opus_int32 max_data_bytes)
2030 { 2210 {
2031 int i, ret; 2211 int i, ret;
2032 int frame_size; 2212 int frame_size;
2033 int delay_compensation;
2034 VARDECL(float, in); 2213 VARDECL(float, in);
2035 ALLOC_STACK; 2214 ALLOC_STACK;
2036 2215
2037 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 2216 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st ->Fs);
2038 delay_compensation = 0; 2217 if (frame_size <= 0)
2039 else 2218 {
2040 delay_compensation = st->delay_compensation; 2219 RESTORE_STACK;
2041 frame_size = compute_frame_size(pcm, analysis_frame_size, 2220 return OPUS_BAD_ARG;
2042 st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 2221 }
2043 delay_compensation, downmix_int, st->analysis.subframe_mem);
2044
2045 ALLOC(in, frame_size*st->channels, float); 2222 ALLOC(in, frame_size*st->channels, float);
2046 2223
2047 for (i=0;i<frame_size*st->channels;i++) 2224 for (i=0;i<frame_size*st->channels;i++)
2048 in[i] = (1.0f/32768)*pcm[i]; 2225 in[i] = (1.0f/32768)*pcm[i];
2049 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, 2226 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2050 pcm, analysis_frame_size, 0, -2, st->channels, downm ix_int, 0); 2227 pcm, analysis_frame_size, 0, -2, st->channels, downm ix_int, 0);
2051 RESTORE_STACK; 2228 RESTORE_STACK;
2052 return ret; 2229 return ret;
2053 } 2230 }
2054 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size, 2231 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size,
2055 unsigned char *data, opus_int32 out_data_bytes) 2232 unsigned char *data, opus_int32 out_data_bytes)
2056 { 2233 {
2057 int frame_size; 2234 int frame_size;
2058 int delay_compensation; 2235 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st ->Fs);
2059 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2060 delay_compensation = 0;
2061 else
2062 delay_compensation = st->delay_compensation;
2063 frame_size = compute_frame_size(pcm, analysis_frame_size,
2064 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2065 delay_compensation, downmix_float, st->analysis.subframe_mem);
2066 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24, 2236 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24,
2067 pcm, analysis_frame_size, 0, -2, st->channels, down mix_float, 1); 2237 pcm, analysis_frame_size, 0, -2, st->channels, down mix_float, 1);
2068 } 2238 }
2069 #endif 2239 #endif
2070 2240
2071 2241
2072 int opus_encoder_ctl(OpusEncoder *st, int request, ...) 2242 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
2073 { 2243 {
2074 int ret; 2244 int ret;
2075 CELTEncoder *celt_enc; 2245 CELTEncoder *celt_enc;
(...skipping 10 matching lines...) Expand all
2086 { 2256 {
2087 opus_int32 value = va_arg(ap, opus_int32); 2257 opus_int32 value = va_arg(ap, opus_int32);
2088 if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_ AUDIO 2258 if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_ AUDIO
2089 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY) 2259 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2090 || (!st->first && st->application != value)) 2260 || (!st->first && st->application != value))
2091 { 2261 {
2092 ret = OPUS_BAD_ARG; 2262 ret = OPUS_BAD_ARG;
2093 break; 2263 break;
2094 } 2264 }
2095 st->application = value; 2265 st->application = value;
2266 #ifndef DISABLE_FLOAT_API
2267 st->analysis.application = value;
2268 #endif
2096 } 2269 }
2097 break; 2270 break;
2098 case OPUS_GET_APPLICATION_REQUEST: 2271 case OPUS_GET_APPLICATION_REQUEST:
2099 { 2272 {
2100 opus_int32 *value = va_arg(ap, opus_int32*); 2273 opus_int32 *value = va_arg(ap, opus_int32*);
2101 if (!value) 2274 if (!value)
2102 { 2275 {
2103 goto bad_arg; 2276 goto bad_arg;
2104 } 2277 }
2105 *value = st->application; 2278 *value = st->application;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
2204 *value = st->bandwidth; 2377 *value = st->bandwidth;
2205 } 2378 }
2206 break; 2379 break;
2207 case OPUS_SET_DTX_REQUEST: 2380 case OPUS_SET_DTX_REQUEST:
2208 { 2381 {
2209 opus_int32 value = va_arg(ap, opus_int32); 2382 opus_int32 value = va_arg(ap, opus_int32);
2210 if(value<0 || value>1) 2383 if(value<0 || value>1)
2211 { 2384 {
2212 goto bad_arg; 2385 goto bad_arg;
2213 } 2386 }
2214 st->silk_mode.useDTX = value; 2387 st->use_dtx = value;
2215 } 2388 }
2216 break; 2389 break;
2217 case OPUS_GET_DTX_REQUEST: 2390 case OPUS_GET_DTX_REQUEST:
2218 { 2391 {
2219 opus_int32 *value = va_arg(ap, opus_int32*); 2392 opus_int32 *value = va_arg(ap, opus_int32*);
2220 if (!value) 2393 if (!value)
2221 { 2394 {
2222 goto bad_arg; 2395 goto bad_arg;
2223 } 2396 }
2224 *value = st->silk_mode.useDTX; 2397 *value = st->use_dtx;
2225 } 2398 }
2226 break; 2399 break;
2227 case OPUS_SET_COMPLEXITY_REQUEST: 2400 case OPUS_SET_COMPLEXITY_REQUEST:
2228 { 2401 {
2229 opus_int32 value = va_arg(ap, opus_int32); 2402 opus_int32 value = va_arg(ap, opus_int32);
2230 if(value<0 || value>10) 2403 if(value<0 || value>10)
2231 { 2404 {
2232 goto bad_arg; 2405 goto bad_arg;
2233 } 2406 }
2234 st->silk_mode.complexity = value; 2407 st->silk_mode.complexity = value;
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
2415 if (!value) 2588 if (!value)
2416 { 2589 {
2417 goto bad_arg; 2590 goto bad_arg;
2418 } 2591 }
2419 *value = st->lsb_depth; 2592 *value = st->lsb_depth;
2420 } 2593 }
2421 break; 2594 break;
2422 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: 2595 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2423 { 2596 {
2424 opus_int32 value = va_arg(ap, opus_int32); 2597 opus_int32 value = va_arg(ap, opus_int32);
2425 if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS && 2598 if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS &&
2426 value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS && 2599 value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS &&
2427 value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS && 2600 value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS &&
2428 value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_VARIABL E) 2601 value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_80_MS &&
2602 value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS )
2429 { 2603 {
2430 goto bad_arg; 2604 goto bad_arg;
2431 } 2605 }
2432 st->variable_duration = value; 2606 st->variable_duration = value;
2433 celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value)); 2607 celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value));
2434 } 2608 }
2435 break; 2609 break;
2436 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: 2610 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
2437 { 2611 {
2438 opus_int32 *value = va_arg(ap, opus_int32*); 2612 opus_int32 *value = va_arg(ap, opus_int32*);
(...skipping 13 matching lines...) Expand all
2452 } 2626 }
2453 break; 2627 break;
2454 case OPUS_GET_PREDICTION_DISABLED_REQUEST: 2628 case OPUS_GET_PREDICTION_DISABLED_REQUEST:
2455 { 2629 {
2456 opus_int32 *value = va_arg(ap, opus_int32*); 2630 opus_int32 *value = va_arg(ap, opus_int32*);
2457 if (!value) 2631 if (!value)
2458 goto bad_arg; 2632 goto bad_arg;
2459 *value = st->silk_mode.reducedDependency; 2633 *value = st->silk_mode.reducedDependency;
2460 } 2634 }
2461 break; 2635 break;
2636 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2637 {
2638 opus_int32 value = va_arg(ap, opus_int32);
2639 if(value<0 || value>1)
2640 {
2641 goto bad_arg;
2642 }
2643 celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value)) ;
2644 }
2645 break;
2646 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
2647 {
2648 opus_int32 *value = va_arg(ap, opus_int32*);
2649 if (!value)
2650 {
2651 goto bad_arg;
2652 }
2653 celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value)) ;
2654 }
2655 break;
2462 case OPUS_RESET_STATE: 2656 case OPUS_RESET_STATE:
2463 { 2657 {
2464 void *silk_enc; 2658 void *silk_enc;
2465 silk_EncControlStruct dummy; 2659 silk_EncControlStruct dummy;
2466 char *start; 2660 char *start;
2467 silk_enc = (char*)st+st->silk_enc_offset; 2661 silk_enc = (char*)st+st->silk_enc_offset;
2468 #ifndef DISABLE_FLOAT_API 2662 #ifndef DISABLE_FLOAT_API
2469 tonality_analysis_reset(&st->analysis); 2663 tonality_analysis_reset(&st->analysis);
2470 #endif 2664 #endif
2471 2665
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
2527 return ret; 2721 return ret;
2528 bad_arg: 2722 bad_arg:
2529 va_end(ap); 2723 va_end(ap);
2530 return OPUS_BAD_ARG; 2724 return OPUS_BAD_ARG;
2531 } 2725 }
2532 2726
2533 void opus_encoder_destroy(OpusEncoder *st) 2727 void opus_encoder_destroy(OpusEncoder *st)
2534 { 2728 {
2535 opus_free(st); 2729 opus_free(st);
2536 } 2730 }
OLDNEW
« no previous file with comments | « third_party/opus/src/src/opus_demo.c ('k') | third_party/opus/src/src/opus_multistream_decoder.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698