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

Side by Side Diff: src/opus_encoder.c

Issue 28553003: Updating Opus to a pre-release of 1.1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/opus
Patch Set: Removing failing file Created 7 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « src/opus_demo.c ('k') | src/opus_multistream.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 22 matching lines...) Expand all
33 #include "celt.h" 33 #include "celt.h"
34 #include "entenc.h" 34 #include "entenc.h"
35 #include "modes.h" 35 #include "modes.h"
36 #include "API.h" 36 #include "API.h"
37 #include "stack_alloc.h" 37 #include "stack_alloc.h"
38 #include "float_cast.h" 38 #include "float_cast.h"
39 #include "opus.h" 39 #include "opus.h"
40 #include "arch.h" 40 #include "arch.h"
41 #include "opus_private.h" 41 #include "opus_private.h"
42 #include "os_support.h" 42 #include "os_support.h"
43 43 #include "cpu_support.h"
44 #include "analysis.h"
45 #include "mathops.h"
44 #include "tuning_parameters.h" 46 #include "tuning_parameters.h"
45 #ifdef FIXED_POINT 47 #ifdef FIXED_POINT
46 #include "fixed/structs_FIX.h" 48 #include "fixed/structs_FIX.h"
47 #else 49 #else
48 #include "float/structs_FLP.h" 50 #include "float/structs_FLP.h"
49 #endif 51 #endif
50 52
51 #define MAX_ENCODER_BUFFER 480 53 #define MAX_ENCODER_BUFFER 480
52 54
55 typedef struct {
56 opus_val32 XX, XY, YY;
57 opus_val16 smoothed_width;
58 opus_val16 max_follower;
59 } StereoWidthState;
60
53 struct OpusEncoder { 61 struct OpusEncoder {
54 int celt_enc_offset; 62 int celt_enc_offset;
55 int silk_enc_offset; 63 int silk_enc_offset;
56 silk_EncControlStruct silk_mode; 64 silk_EncControlStruct silk_mode;
57 int application; 65 int application;
58 int channels; 66 int channels;
59 int delay_compensation; 67 int delay_compensation;
60 int force_channels; 68 int force_channels;
61 int signal_type; 69 int signal_type;
62 int user_bandwidth; 70 int user_bandwidth;
63 int max_bandwidth; 71 int max_bandwidth;
64 int user_forced_mode; 72 int user_forced_mode;
65 int voice_ratio; 73 int voice_ratio;
66 opus_int32 Fs; 74 opus_int32 Fs;
67 int use_vbr; 75 int use_vbr;
68 int vbr_constraint; 76 int vbr_constraint;
77 int variable_duration;
69 opus_int32 bitrate_bps; 78 opus_int32 bitrate_bps;
70 opus_int32 user_bitrate_bps; 79 opus_int32 user_bitrate_bps;
80 int lsb_depth;
71 int encoder_buffer; 81 int encoder_buffer;
82 int lfe;
72 83
73 #define OPUS_ENCODER_RESET_START stream_channels 84 #define OPUS_ENCODER_RESET_START stream_channels
74 int stream_channels; 85 int stream_channels;
75 opus_int16 hybrid_stereo_width_Q14; 86 opus_int16 hybrid_stereo_width_Q14;
76 opus_int32 variable_HP_smth2_Q15; 87 opus_int32 variable_HP_smth2_Q15;
88 opus_val16 prev_HB_gain;
77 opus_val32 hp_mem[4]; 89 opus_val32 hp_mem[4];
78 int mode; 90 int mode;
79 int prev_mode; 91 int prev_mode;
80 int prev_channels; 92 int prev_channels;
81 int prev_framesize; 93 int prev_framesize;
82 int bandwidth; 94 int bandwidth;
83 int silk_bw_switch; 95 int silk_bw_switch;
84 /* Sampling rate (at the API level) */ 96 /* Sampling rate (at the API level) */
85 int first; 97 int first;
98 opus_val16 * energy_masking;
99 StereoWidthState width_mem;
86 opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2]; 100 opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2];
87 101 #ifndef DISABLE_FLOAT_API
102 TonalityAnalysisState analysis;
103 int detected_bandwidth;
104 int analysis_offset;
105 #endif
88 opus_uint32 rangeFinal; 106 opus_uint32 rangeFinal;
107 int arch;
89 }; 108 };
90 109
91 /* Transition tables for the voice and music. First column is the 110 /* Transition tables for the voice and music. First column is the
92 middle (memoriless) threshold. The second column is the hysteresis 111 middle (memoriless) threshold. The second column is the hysteresis
93 (difference with the middle) */ 112 (difference with the middle) */
94 static const opus_int32 mono_voice_bandwidth_thresholds[8] = { 113 static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
95 11000, 1000, /* NB<->MB */ 114 11000, 1000, /* NB<->MB */
96 14000, 1000, /* MB<->WB */ 115 14000, 1000, /* MB<->WB */
97 21000, 2000, /* WB<->SWB */ 116 17000, 1000, /* WB<->SWB */
98 29000, 2000, /* SWB<->FB */ 117 20000, 1000, /* SWB<->FB */
99 }; 118 };
100 static const opus_int32 mono_music_bandwidth_thresholds[8] = { 119 static const opus_int32 mono_music_bandwidth_thresholds[8] = {
101 14000, 1000, /* MB not allowed */ 120 14000, 1000, /* MB not allowed */
102 18000, 2000, /* MB<->WB */ 121 18000, 2000, /* MB<->WB */
103 24000, 2000, /* WB<->SWB */ 122 24000, 2000, /* WB<->SWB */
104 33000, 2000, /* SWB<->FB */ 123 33000, 2000, /* SWB<->FB */
105 }; 124 };
106 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { 125 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
107 11000, 1000, /* NB<->MB */ 126 11000, 1000, /* NB<->MB */
108 14000, 1000, /* MB<->WB */ 127 14000, 1000, /* MB<->WB */
109 21000, 2000, /* WB<->SWB */ 128 21000, 2000, /* WB<->SWB */
110 32000, 2000, /* SWB<->FB */ 129 32000, 2000, /* SWB<->FB */
111 }; 130 };
112 static const opus_int32 stereo_music_bandwidth_thresholds[8] = { 131 static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
113 14000, 1000, /* MB not allowed */ 132 14000, 1000, /* MB not allowed */
114 18000, 2000, /* MB<->WB */ 133 18000, 2000, /* MB<->WB */
115 24000, 2000, /* WB<->SWB */ 134 24000, 2000, /* WB<->SWB */
116 48000, 2000, /* SWB<->FB */ 135 48000, 2000, /* SWB<->FB */
117 }; 136 };
118 /* Threshold bit-rates for switching between mono and stereo */ 137 /* Threshold bit-rates for switching between mono and stereo */
119 static const opus_int32 stereo_voice_threshold = 26000; 138 static const opus_int32 stereo_voice_threshold = 31000;
120 static const opus_int32 stereo_music_threshold = 36000; 139 static const opus_int32 stereo_music_threshold = 31000;
121 140
122 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ 141 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
123 static const opus_int32 mode_thresholds[2][2] = { 142 static const opus_int32 mode_thresholds[2][2] = {
124 /* voice */ /* music */ 143 /* voice */ /* music */
125 { 48000, 24000}, /* mono */ 144 { 64000, 20000}, /* mono */
126 { 48000, 24000}, /* stereo */ 145 { 36000, 20000}, /* stereo */
127 }; 146 };
128 147
129 int opus_encoder_get_size(int channels) 148 int opus_encoder_get_size(int channels)
130 { 149 {
131 int silkEncSizeBytes, celtEncSizeBytes; 150 int silkEncSizeBytes, celtEncSizeBytes;
132 int ret; 151 int ret;
133 if (channels<1 || channels > 2) 152 if (channels<1 || channels > 2)
134 return 0; 153 return 0;
135 ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); 154 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
136 if (ret) 155 if (ret)
(...skipping 23 matching lines...) Expand all
160 silkEncSizeBytes = align(silkEncSizeBytes); 179 silkEncSizeBytes = align(silkEncSizeBytes);
161 st->silk_enc_offset = align(sizeof(OpusEncoder)); 180 st->silk_enc_offset = align(sizeof(OpusEncoder));
162 st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; 181 st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
163 silk_enc = (char*)st+st->silk_enc_offset; 182 silk_enc = (char*)st+st->silk_enc_offset;
164 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 183 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
165 184
166 st->stream_channels = st->channels = channels; 185 st->stream_channels = st->channels = channels;
167 186
168 st->Fs = Fs; 187 st->Fs = Fs;
169 188
189 st->arch = opus_select_arch();
190
170 ret = silk_InitEncoder( silk_enc, &st->silk_mode ); 191 ret = silk_InitEncoder( silk_enc, &st->silk_mode );
171 if(ret)return OPUS_INTERNAL_ERROR; 192 if(ret)return OPUS_INTERNAL_ERROR;
172 193
173 /* default SILK parameters */ 194 /* default SILK parameters */
174 st->silk_mode.nChannelsAPI = channels; 195 st->silk_mode.nChannelsAPI = channels;
175 st->silk_mode.nChannelsInternal = channels; 196 st->silk_mode.nChannelsInternal = channels;
176 st->silk_mode.API_sampleRate = st->Fs; 197 st->silk_mode.API_sampleRate = st->Fs;
177 st->silk_mode.maxInternalSampleRate = 16000; 198 st->silk_mode.maxInternalSampleRate = 16000;
178 st->silk_mode.minInternalSampleRate = 8000; 199 st->silk_mode.minInternalSampleRate = 8000;
179 st->silk_mode.desiredInternalSampleRate = 16000; 200 st->silk_mode.desiredInternalSampleRate = 16000;
180 st->silk_mode.payloadSize_ms = 20; 201 st->silk_mode.payloadSize_ms = 20;
181 st->silk_mode.bitRate = 25000; 202 st->silk_mode.bitRate = 25000;
182 st->silk_mode.packetLossPercentage = 0; 203 st->silk_mode.packetLossPercentage = 0;
183 st->silk_mode.complexity = 10; 204 st->silk_mode.complexity = 9;
184 st->silk_mode.useInBandFEC = 0; 205 st->silk_mode.useInBandFEC = 0;
185 st->silk_mode.useDTX = 0; 206 st->silk_mode.useDTX = 0;
186 st->silk_mode.useCBR = 0; 207 st->silk_mode.useCBR = 0;
187 208
188 /* Create CELT encoder */ 209 /* Create CELT encoder */
189 /* Initialize CELT encoder */ 210 /* Initialize CELT encoder */
190 err = celt_encoder_init(celt_enc, Fs, channels); 211 err = celt_encoder_init(celt_enc, Fs, channels);
191 if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR; 212 if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
192 213
193 celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); 214 celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
194 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(10)); 215 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity));
195 216
196 st->use_vbr = 1; 217 st->use_vbr = 1;
197 /* Makes constrained VBR the default (safer for real-time use) */ 218 /* Makes constrained VBR the default (safer for real-time use) */
198 st->vbr_constraint = 1; 219 st->vbr_constraint = 1;
199 st->user_bitrate_bps = OPUS_AUTO; 220 st->user_bitrate_bps = OPUS_AUTO;
200 st->bitrate_bps = 3000+Fs*channels; 221 st->bitrate_bps = 3000+Fs*channels;
201 st->application = application; 222 st->application = application;
202 st->signal_type = OPUS_AUTO; 223 st->signal_type = OPUS_AUTO;
203 st->user_bandwidth = OPUS_AUTO; 224 st->user_bandwidth = OPUS_AUTO;
204 st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND; 225 st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
205 st->force_channels = OPUS_AUTO; 226 st->force_channels = OPUS_AUTO;
206 st->user_forced_mode = OPUS_AUTO; 227 st->user_forced_mode = OPUS_AUTO;
207 st->voice_ratio = -1; 228 st->voice_ratio = -1;
208 st->encoder_buffer = st->Fs/100; 229 st->encoder_buffer = st->Fs/100;
230 st->lsb_depth = 24;
231 st->variable_duration = OPUS_FRAMESIZE_ARG;
209 232
210 /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead 233 /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
211 + 1.5 ms for SILK resamplers and stereo prediction) */ 234 + 1.5 ms for SILK resamplers and stereo prediction) */
212 st->delay_compensation = st->Fs/250; 235 st->delay_compensation = st->Fs/250;
213 236
214 st->hybrid_stereo_width_Q14 = 1 << 14; 237 st->hybrid_stereo_width_Q14 = 1 << 14;
238 st->prev_HB_gain = Q15ONE;
215 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 ); 239 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 );
216 st->first = 1; 240 st->first = 1;
217 st->mode = MODE_HYBRID; 241 st->mode = MODE_HYBRID;
218 st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 242 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
219 243
220 return OPUS_OK; 244 return OPUS_OK;
221 } 245 }
222 246
223 static int pad_frame(unsigned char *data, opus_int32 len, opus_int32 new_len) 247 static int pad_frame(unsigned char *data, opus_int32 len, opus_int32 new_len)
224 { 248 {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 339
316 /* Negate A_Q28 values and split in two parts */ 340 /* Negate A_Q28 values and split in two parts */
317 341
318 for( k = 0; k < len; k++ ) { 342 for( k = 0; k < len; k++ ) {
319 /* S[ 0 ], S[ 1 ]: Q12 */ 343 /* S[ 0 ], S[ 1 ]: Q12 */
320 inval = in[ k*stride ]; 344 inval = in[ k*stride ];
321 vout = S[ 0 ] + B[0]*inval; 345 vout = S[ 0 ] + B[0]*inval;
322 346
323 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval; 347 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
324 348
325 S[ 1 ] = - vout*A[1] + B[2]*inval; 349 S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL;
326 350
327 /* Scale back to Q0 and saturate */ 351 /* Scale back to Q0 and saturate */
328 out[ k*stride ] = vout; 352 out[ k*stride ] = vout;
329 } 353 }
330 } 354 }
331 #endif 355 #endif
332 356
333 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) 357 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)
334 { 358 {
335 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ]; 359 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
(...skipping 22 matching lines...) Expand all
358 silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); 382 silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
359 } 383 }
360 #else 384 #else
361 silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels ); 385 silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels );
362 if( channels == 2 ) { 386 if( channels == 2 ) {
363 silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); 387 silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
364 } 388 }
365 #endif 389 #endif
366 } 390 }
367 391
392 #ifdef FIXED_POINT
393 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)
394 {
395 int c, i;
396 int shift;
397
398 /* Approximates -round(log2(4.*cutoff_Hz/Fs)) */
399 shift=celt_ilog2(Fs/(cutoff_Hz*3));
400 for (c=0;c<channels;c++)
401 {
402 for (i=0;i<len;i++)
403 {
404 opus_val32 x, tmp, y;
405 x = SHL32(EXTEND32(in[channels*i+c]), 15);
406 /* First stage */
407 tmp = x-hp_mem[2*c];
408 hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift);
409 /* Second stage */
410 y = tmp - hp_mem[2*c+1];
411 hp_mem[2*c+1] = hp_mem[2*c+1] + PSHR32(tmp - hp_mem[2*c+1], shift);
412 out[channels*i+c] = EXTRACT16(SATURATE(PSHR32(y, 15), 32767));
413 }
414 }
415 }
416
417 #else
418 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)
419 {
420 int c, i;
421 float coef;
422
423 coef = 4.0f*cutoff_Hz/Fs;
424 for (c=0;c<channels;c++)
425 {
426 for (i=0;i<len;i++)
427 {
428 opus_val32 x, tmp, y;
429 x = in[channels*i+c];
430 /* First stage */
431 tmp = x-hp_mem[2*c];
432 hp_mem[2*c] = hp_mem[2*c] + coef*(x - hp_mem[2*c]) + VERY_SMALL;
433 /* Second stage */
434 y = tmp - hp_mem[2*c+1];
435 hp_mem[2*c+1] = hp_mem[2*c+1] + coef*(tmp - hp_mem[2*c+1]) + VERY_SMALL ;
436 out[channels*i+c] = y;
437 }
438 }
439 }
440 #endif
441
368 static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, op us_val16 g2, 442 static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, op us_val16 g2,
369 int overlap48, int frame_size, int channels, const opus_val16 *window, o pus_int32 Fs) 443 int overlap48, int frame_size, int channels, const opus_val16 *window, o pus_int32 Fs)
370 { 444 {
371 int i; 445 int i;
372 int overlap; 446 int overlap;
373 int inc; 447 int inc;
374 inc = 48000/Fs; 448 inc = 48000/Fs;
375 overlap=overlap48/inc; 449 overlap=overlap48/inc;
376 g1 = Q15ONE-g1; 450 g1 = Q15ONE-g1;
377 g2 = Q15ONE-g2; 451 g2 = Q15ONE-g2;
(...skipping 12 matching lines...) Expand all
390 for (;i<frame_size;i++) 464 for (;i<frame_size;i++)
391 { 465 {
392 opus_val32 diff; 466 opus_val32 diff;
393 diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*cha nnels+1])); 467 diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*cha nnels+1]));
394 diff = MULT16_16_Q15(g2, diff); 468 diff = MULT16_16_Q15(g2, diff);
395 out[i*channels] = out[i*channels] - diff; 469 out[i*channels] = out[i*channels] - diff;
396 out[i*channels+1] = out[i*channels+1] + diff; 470 out[i*channels+1] = out[i*channels+1] + diff;
397 } 471 }
398 } 472 }
399 473
474 static void gain_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus _val16 g2,
475 int overlap48, int frame_size, int channels, const opus_val16 *window, o pus_int32 Fs)
476 {
477 int i;
478 int inc;
479 int overlap;
480 int c;
481 inc = 48000/Fs;
482 overlap=overlap48/inc;
483 if (channels==1)
484 {
485 for (i=0;i<overlap;i++)
486 {
487 opus_val16 g, w;
488 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
489 g = SHR32(MAC16_16(MULT16_16(w,g2),
490 Q15ONE-w, g1), 15);
491 out[i] = MULT16_16_Q15(g, in[i]);
492 }
493 } else {
494 for (i=0;i<overlap;i++)
495 {
496 opus_val16 g, w;
497 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
498 g = SHR32(MAC16_16(MULT16_16(w,g2),
499 Q15ONE-w, g1), 15);
500 out[i*2] = MULT16_16_Q15(g, in[i*2]);
501 out[i*2+1] = MULT16_16_Q15(g, in[i*2+1]);
502 }
503 }
504 c=0;do {
505 for (i=overlap;i<frame_size;i++)
506 {
507 out[i*channels+c] = MULT16_16_Q15(g2, in[i*channels+c]);
508 }
509 }
510 while (++c<channels);
511 }
512
400 OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, i nt *error) 513 OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, i nt *error)
401 { 514 {
402 int ret; 515 int ret;
403 OpusEncoder *st; 516 OpusEncoder *st;
404 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&chan nels!=2)|| 517 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&chan nels!=2)||
405 (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_ AUDIO 518 (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_ AUDIO
406 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)) 519 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
407 { 520 {
408 if (error) 521 if (error)
409 *error = OPUS_BAD_ARG; 522 *error = OPUS_BAD_ARG;
(...skipping 21 matching lines...) Expand all
431 { 544 {
432 if(!frame_size)frame_size=st->Fs/400; 545 if(!frame_size)frame_size=st->Fs/400;
433 if (st->user_bitrate_bps==OPUS_AUTO) 546 if (st->user_bitrate_bps==OPUS_AUTO)
434 return 60*st->Fs/frame_size + st->Fs*st->channels; 547 return 60*st->Fs/frame_size + st->Fs*st->channels;
435 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX) 548 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
436 return max_data_bytes*8*st->Fs/frame_size; 549 return max_data_bytes*8*st->Fs/frame_size;
437 else 550 else
438 return st->user_bitrate_bps; 551 return st->user_bitrate_bps;
439 } 552 }
440 553
554 #ifndef DISABLE_FLOAT_API
555 /* Don't use more than 60 ms for the frame size analysis */
556 #define MAX_DYNAMIC_FRAMESIZE 24
557 /* Estimates how much the bitrate will be boosted based on the sub-frame energy */
558 static float transient_boost(const float *E, const float *E_1, int LM, int maxM)
559 {
560 int i;
561 int M;
562 float sumE=0, sumE_1=0;
563 float metric;
564
565 M = IMIN(maxM, (1<<LM)+1);
566 for (i=0;i<M;i++)
567 {
568 sumE += E[i];
569 sumE_1 += E_1[i];
570 }
571 metric = sumE*sumE_1/(M*M);
572 /*if (LM==3)
573 printf("%f\n", metric);*/
574 /*return metric>10 ? 1 : 0;*/
575 /*return MAX16(0,1-exp(-.25*(metric-2.)));*/
576 return MIN16(1,(float)sqrt(MAX16(0,.05f*(metric-2))));
577 }
578
579 /* Viterbi decoding trying to find the best frame size combination using look-ah ead
580
581 State numbering:
582 0: unused
583 1: 2.5 ms
584 2: 5 ms (#1)
585 3: 5 ms (#2)
586 4: 10 ms (#1)
587 5: 10 ms (#2)
588 6: 10 ms (#3)
589 7: 10 ms (#4)
590 8: 20 ms (#1)
591 9: 20 ms (#2)
592 10: 20 ms (#3)
593 11: 20 ms (#4)
594 12: 20 ms (#5)
595 13: 20 ms (#6)
596 14: 20 ms (#7)
597 15: 20 ms (#8)
598 */
599 static int transient_viterbi(const float *E, const float *E_1, int N, int frame_ cost, int rate)
600 {
601 int i;
602 float cost[MAX_DYNAMIC_FRAMESIZE][16];
603 int states[MAX_DYNAMIC_FRAMESIZE][16];
604 float best_cost;
605 int best_state;
606 float factor;
607 /* Take into account that we damp VBR in the 32 kb/s to 64 kb/s range. */
608 if (rate<80)
609 factor=0;
610 else if (rate>160)
611 factor=1;
612 else
613 factor = (rate-80.f)/80.f;
614 /* Makes variable framesize less aggressive at lower bitrates, but I can't
615 find any valid theoretical justification for this (other than it seems
616 to help) */
617 for (i=0;i<16;i++)
618 {
619 /* Impossible state */
620 states[0][i] = -1;
621 cost[0][i] = 1e10;
622 }
623 for (i=0;i<4;i++)
624 {
625 cost[0][1<<i] = (frame_cost + rate*(1<<i))*(1+factor*transient_boost(E, E_ 1, i, N+1));
626 states[0][1<<i] = i;
627 }
628 for (i=1;i<N;i++)
629 {
630 int j;
631
632 /* Follow continuations */
633 for (j=2;j<16;j++)
634 {
635 cost[i][j] = cost[i-1][j-1];
636 states[i][j] = j-1;
637 }
638
639 /* New frames */
640 for(j=0;j<4;j++)
641 {
642 int k;
643 float min_cost;
644 float curr_cost;
645 states[i][1<<j] = 1;
646 min_cost = cost[i-1][1];
647 for(k=1;k<4;k++)
648 {
649 float tmp = cost[i-1][(1<<(k+1))-1];
650 if (tmp < min_cost)
651 {
652 states[i][1<<j] = (1<<(k+1))-1;
653 min_cost = tmp;
654 }
655 }
656 curr_cost = (frame_cost + rate*(1<<j))*(1+factor*transient_boost(E+i, E _1+i, j, N-i+1));
657 cost[i][1<<j] = min_cost;
658 /* If part of the frame is outside the analysis window, only count part of the cost */
659 if (N-i < (1<<j))
660 cost[i][1<<j] += curr_cost*(float)(N-i)/(1<<j);
661 else
662 cost[i][1<<j] += curr_cost;
663 }
664 }
665
666 best_state=1;
667 best_cost = cost[N-1][1];
668 /* Find best end state (doesn't force a frame to end at N-1) */
669 for (i=2;i<16;i++)
670 {
671 if (cost[N-1][i]<best_cost)
672 {
673 best_cost = cost[N-1][i];
674 best_state = i;
675 }
676 }
677
678 /* Follow transitions back */
679 for (i=N-1;i>=0;i--)
680 {
681 /*printf("%d ", best_state);*/
682 best_state = states[i][best_state];
683 }
684 /*printf("%d\n", best_state);*/
685 return best_state;
686 }
687
688 int optimize_framesize(const opus_val16 *x, int len, int C, opus_int32 Fs,
689 int bitrate, opus_val16 tonality, opus_val32 *mem, int buffering ,
690 downmix_func downmix)
691 {
692 int N;
693 int i;
694 float e[MAX_DYNAMIC_FRAMESIZE+4];
695 float e_1[MAX_DYNAMIC_FRAMESIZE+3];
696 float memx;
697 int bestLM=0;
698 int subframe;
699 int pos;
700 VARDECL(opus_val32, sub);
701
702 subframe = Fs/400;
703 ALLOC(sub, subframe, opus_val32);
704 e[0]=mem[0];
705 e_1[0]=1.f/(EPSILON+mem[0]);
706 if (buffering)
707 {
708 /* Consider the CELT delay when not in restricted-lowdelay */
709 /* We assume the buffering is between 2.5 and 5 ms */
710 int offset = 2*subframe - buffering;
711 celt_assert(offset>=0 && offset <= subframe);
712 x += C*offset;
713 len -= offset;
714 e[1]=mem[1];
715 e_1[1]=1.f/(EPSILON+mem[1]);
716 e[2]=mem[2];
717 e_1[2]=1.f/(EPSILON+mem[2]);
718 pos = 3;
719 } else {
720 pos=1;
721 }
722 N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE);
723 memx = x[0];
724 for (i=0;i<N;i++)
725 {
726 float tmp;
727 float tmpx;
728 int j;
729 tmp=EPSILON;
730
731 downmix(x, sub, subframe, i*subframe, 0, -2, C);
732 if (i==0)
733 memx = sub[0];
734 for (j=0;j<subframe;j++)
735 {
736 tmpx = sub[j];
737 tmp += (tmpx-memx)*(tmpx-memx);
738 memx = tmpx;
739 }
740 e[i+pos] = tmp;
741 e_1[i+pos] = 1.f/tmp;
742 }
743 /* Hack to get 20 ms working with APPLICATION_AUDIO
744 The real problem is that the corresponding memory needs to use 1.5 ms
745 from this frame and 1 ms from the next frame */
746 e[i+pos] = e[i+pos-1];
747 if (buffering)
748 N=IMIN(MAX_DYNAMIC_FRAMESIZE, N+2);
749 bestLM = transient_viterbi(e, e_1, N, (int)((1.f+.5f*tonality)*(60*C+40)), bi trate/400);
750 mem[0] = e[1<<bestLM];
751 if (buffering)
752 {
753 mem[1] = e[(1<<bestLM)+1];
754 mem[2] = e[(1<<bestLM)+2];
755 }
756 return bestLM;
757 }
758
759 #endif
760
761 #ifndef DISABLE_FLOAT_API
441 #ifdef FIXED_POINT 762 #ifdef FIXED_POINT
442 #define opus_encode_native opus_encode 763 #define PCM2VAL(x) FLOAT2INT16(x)
443 opus_int32 opus_encode(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
444 unsigned char *data, opus_int32 out_data_bytes)
445 #else 764 #else
446 #define opus_encode_native opus_encode_float 765 #define PCM2VAL(x) SCALEIN(x)
447 opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s ize, 766 #endif
448 unsigned char *data, opus_int32 out_data_bytes) 767 void downmix_float(const void *_x, opus_val32 *sub, int subframe, int offset, in t c1, int c2, int C)
449 #endif 768 {
769 const float *x;
770 opus_val32 scale;
771 int j;
772 x = (const float *)_x;
773 for (j=0;j<subframe;j++)
774 sub[j] = PCM2VAL(x[(j+offset)*C+c1]);
775 if (c2>-1)
776 {
777 for (j=0;j<subframe;j++)
778 sub[j] += PCM2VAL(x[(j+offset)*C+c2]);
779 } else if (c2==-2)
780 {
781 int c;
782 for (c=1;c<C;c++)
783 {
784 for (j=0;j<subframe;j++)
785 sub[j] += PCM2VAL(x[(j+offset)*C+c]);
786 }
787 }
788 #ifdef FIXED_POINT
789 scale = (1<<SIG_SHIFT);
790 #else
791 scale = 1.f;
792 #endif
793 if (C==-2)
794 scale /= C;
795 else
796 scale /= 2;
797 for (j=0;j<subframe;j++)
798 sub[j] *= scale;
799 }
800 #endif
801
802 void downmix_int(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C)
803 {
804 const opus_int16 *x;
805 opus_val32 scale;
806 int j;
807 x = (const opus_int16 *)_x;
808 for (j=0;j<subframe;j++)
809 sub[j] = x[(j+offset)*C+c1];
810 if (c2>-1)
811 {
812 for (j=0;j<subframe;j++)
813 sub[j] += x[(j+offset)*C+c2];
814 } else if (c2==-2)
815 {
816 int c;
817 for (c=1;c<C;c++)
818 {
819 for (j=0;j<subframe;j++)
820 sub[j] += x[(j+offset)*C+c];
821 }
822 }
823 #ifdef FIXED_POINT
824 scale = (1<<SIG_SHIFT);
825 #else
826 scale = 1.f/32768;
827 #endif
828 if (C==-2)
829 scale /= C;
830 else
831 scale /= 2;
832 for (j=0;j<subframe;j++)
833 sub[j] *= scale;
834 }
835
836 opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_ int32 Fs)
837 {
838 int new_size;
839 if (frame_size<Fs/400)
840 return -1;
841 if (variable_duration == OPUS_FRAMESIZE_ARG)
842 new_size = frame_size;
843 else if (variable_duration == OPUS_FRAMESIZE_VARIABLE)
844 new_size = Fs/50;
845 else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= O PUS_FRAMESIZE_60_MS)
846 new_size = IMIN(3*Fs/50, (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_M S));
847 else
848 return -1;
849 if (new_size>frame_size)
850 return -1;
851 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs &&
852 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs)
853 return -1;
854 return new_size;
855 }
856
857 opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size,
858 int variable_duration, int C, opus_int32 Fs, int bitrate_bps,
859 int delay_compensation, downmix_func downmix, opus_val32 *subframe_mem)
860 {
861 #ifndef DISABLE_FLOAT_API
862 if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200)
863 {
864 int LM = 3;
865 LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps,
866 0, subframe_mem, delay_compensation, downmix);
867 while ((Fs/400<<LM)>frame_size)
868 LM--;
869 frame_size = (Fs/400<<LM);
870 } else
871 #endif
872 {
873 frame_size = frame_size_select(frame_size, variable_duration, Fs);
874 }
875 if (frame_size<0)
876 return -1;
877 return frame_size;
878 }
879
880 opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int3 2 Fs, StereoWidthState *mem)
881 {
882 opus_val16 corr;
883 opus_val16 ldiff;
884 opus_val16 width;
885 opus_val32 xx, xy, yy;
886 opus_val16 sqrt_xx, sqrt_yy;
887 opus_val16 qrrt_xx, qrrt_yy;
888 int frame_rate;
889 int i;
890 opus_val16 short_alpha;
891
892 frame_rate = Fs/frame_size;
893 short_alpha = Q15ONE - 25*Q15ONE/IMAX(50,frame_rate);
894 xx=xy=yy=0;
895 for (i=0;i<frame_size;i+=4)
896 {
897 opus_val32 pxx=0;
898 opus_val32 pxy=0;
899 opus_val32 pyy=0;
900 opus_val16 x, y;
901 x = pcm[2*i];
902 y = pcm[2*i+1];
903 pxx = SHR32(MULT16_16(x,x),2);
904 pxy = SHR32(MULT16_16(x,y),2);
905 pyy = SHR32(MULT16_16(y,y),2);
906 x = pcm[2*i+2];
907 y = pcm[2*i+3];
908 pxx += SHR32(MULT16_16(x,x),2);
909 pxy += SHR32(MULT16_16(x,y),2);
910 pyy += SHR32(MULT16_16(y,y),2);
911 x = pcm[2*i+4];
912 y = pcm[2*i+5];
913 pxx += SHR32(MULT16_16(x,x),2);
914 pxy += SHR32(MULT16_16(x,y),2);
915 pyy += SHR32(MULT16_16(y,y),2);
916 x = pcm[2*i+6];
917 y = pcm[2*i+7];
918 pxx += SHR32(MULT16_16(x,x),2);
919 pxy += SHR32(MULT16_16(x,y),2);
920 pyy += SHR32(MULT16_16(y,y),2);
921
922 xx += SHR32(pxx, 10);
923 xy += SHR32(pxy, 10);
924 yy += SHR32(pyy, 10);
925 }
926 mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX);
927 mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);
928 mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY);
929 mem->XX = MAX32(0, mem->XX);
930 mem->XY = MAX32(0, mem->XY);
931 mem->YY = MAX32(0, mem->YY);
932 if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
933 {
934 sqrt_xx = celt_sqrt(mem->XX);
935 sqrt_yy = celt_sqrt(mem->YY);
936 qrrt_xx = celt_sqrt(sqrt_xx);
937 qrrt_yy = celt_sqrt(sqrt_yy);
938 /* Inter-channel correlation */
939 mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy);
940 corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16);
941 /* Approximate loudness difference */
942 ldiff = Q15ONE*ABS16(qrrt_xx-qrrt_yy)/(EPSILON+qrrt_xx+qrrt_yy);
943 width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ld iff);
944 /* Smoothing over one second */
945 mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
946 /* Peak follower */
947 mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
948 } else {
949 width = 0;
950 corr=Q15ONE;
951 ldiff=0;
952 }
953 /*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) ;*/
954 return EXTRACT16(MIN32(Q15ONE,20*mem->max_follower));
955 }
956
957 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_ size,
958 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
959 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, int analysis_channels, downmix_func downmix)
450 { 960 {
451 void *silk_enc; 961 void *silk_enc;
452 CELTEncoder *celt_enc; 962 CELTEncoder *celt_enc;
453 int i; 963 int i;
454 int ret=0; 964 int ret=0;
455 opus_int32 nBytes; 965 opus_int32 nBytes;
456 ec_enc enc; 966 ec_enc enc;
457 int bytes_target; 967 int bytes_target;
458 int prefill=0; 968 int prefill=0;
459 int start_band = 0; 969 int start_band = 0;
460 int redundancy = 0; 970 int redundancy = 0;
461 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */ 971 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
462 int celt_to_silk = 0; 972 int celt_to_silk = 0;
463 VARDECL(opus_val16, pcm_buf); 973 VARDECL(opus_val16, pcm_buf);
464 int nb_compr_bytes; 974 int nb_compr_bytes;
465 int to_celt = 0; 975 int to_celt = 0;
466 opus_uint32 redundant_rng = 0; 976 opus_uint32 redundant_rng = 0;
467 int cutoff_Hz, hp_freq_smth1; 977 int cutoff_Hz, hp_freq_smth1;
468 int voice_est; /* Probability of voice in Q7 */ 978 int voice_est; /* Probability of voice in Q7 */
469 opus_int32 equiv_rate; 979 opus_int32 equiv_rate;
470 int delay_compensation; 980 int delay_compensation;
471 int frame_rate; 981 int frame_rate;
472 opus_int32 max_rate; /* Max bitrate we're allowed to use */ 982 opus_int32 max_rate; /* Max bitrate we're allowed to use */
473 int curr_bandwidth; 983 int curr_bandwidth;
984 opus_val16 HB_gain;
474 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ 985 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
986 int total_buffer;
987 opus_val16 stereo_width;
988 const CELTMode *celt_mode;
989 AnalysisInfo analysis_info;
990 int analysis_read_pos_bak=-1;
991 int analysis_read_subframe_bak=-1;
475 VARDECL(opus_val16, tmp_prefill); 992 VARDECL(opus_val16, tmp_prefill);
476 993
477 ALLOC_STACK; 994 ALLOC_STACK;
478 995
479 max_data_bytes = IMIN(1276, out_data_bytes); 996 max_data_bytes = IMIN(1276, out_data_bytes);
480 997
481 st->rangeFinal = 0; 998 st->rangeFinal = 0;
482 if (400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size ! = st->Fs && 999 if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs &&
483 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size ! = 3*st->Fs) 1000 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size ! = 3*st->Fs)
484 { 1001 || (400*frame_size < st->Fs)
485 RESTORE_STACK; 1002 || max_data_bytes<=0
486 return OPUS_BAD_ARG; 1003 )
487 }
488 if (max_data_bytes<=0)
489 { 1004 {
490 RESTORE_STACK; 1005 RESTORE_STACK;
491 return OPUS_BAD_ARG; 1006 return OPUS_BAD_ARG;
492 } 1007 }
493 silk_enc = (char*)st+st->silk_enc_offset; 1008 silk_enc = (char*)st+st->silk_enc_offset;
494 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 1009 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
495
496 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1010 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
497 delay_compensation = 0; 1011 delay_compensation = 0;
498 else 1012 else
499 delay_compensation = st->delay_compensation; 1013 delay_compensation = st->delay_compensation;
500 1014
1015 lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1016
1017 analysis_info.valid = 0;
1018 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1019 #ifndef DISABLE_FLOAT_API
1020 #ifdef FIXED_POINT
1021 if (st->silk_mode.complexity >= 10 && st->Fs==48000)
1022 #else
1023 if (st->silk_mode.complexity >= 7 && st->Fs==48000)
1024 #endif
1025 {
1026 analysis_read_pos_bak = st->analysis.read_pos;
1027 analysis_read_subframe_bak = st->analysis.read_subframe;
1028 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame _size,
1029 c1, c2, analysis_channels, st->Fs,
1030 lsb_depth, downmix, &analysis_info);
1031 }
1032 #endif
1033
1034 st->voice_ratio = -1;
1035
1036 #ifndef DISABLE_FLOAT_API
1037 st->detected_bandwidth = 0;
1038 if (analysis_info.valid)
1039 {
1040 int analysis_bandwidth;
1041 if (st->signal_type == OPUS_AUTO)
1042 st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob));
1043
1044 analysis_bandwidth = analysis_info.bandwidth;
1045 if (analysis_bandwidth<=12)
1046 st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1047 else if (analysis_bandwidth<=14)
1048 st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1049 else if (analysis_bandwidth<=16)
1050 st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1051 else if (analysis_bandwidth<=18)
1052 st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1053 else
1054 st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1055 }
1056 #endif
1057
1058 if (st->channels==2 && st->force_channels!=1)
1059 stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_m em);
1060 else
1061 stereo_width = 0;
1062 total_buffer = delay_compensation;
501 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes); 1063 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
502 1064
503 frame_rate = st->Fs/frame_size; 1065 frame_rate = st->Fs/frame_size;
504 if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8 1066 if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
505 || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400))) 1067 || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400)))
506 { 1068 {
507 /*If the space is too low to do something useful, emit 'PLC' frames.*/ 1069 /*If the space is too low to do something useful, emit 'PLC' frames.*/
508 int tocmode = st->mode; 1070 int tocmode = st->mode;
509 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth; 1071 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
510 if (tocmode==0) 1072 if (tocmode==0)
(...skipping 22 matching lines...) Expand all
533 max_rate = frame_rate*max_data_bytes*8; 1095 max_rate = frame_rate*max_data_bytes*8;
534 1096
535 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ 1097 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
536 equiv_rate = st->bitrate_bps - 60*(st->Fs/frame_size - 50); 1098 equiv_rate = st->bitrate_bps - 60*(st->Fs/frame_size - 50);
537 1099
538 if (st->signal_type == OPUS_SIGNAL_VOICE) 1100 if (st->signal_type == OPUS_SIGNAL_VOICE)
539 voice_est = 127; 1101 voice_est = 127;
540 else if (st->signal_type == OPUS_SIGNAL_MUSIC) 1102 else if (st->signal_type == OPUS_SIGNAL_MUSIC)
541 voice_est = 0; 1103 voice_est = 0;
542 else if (st->voice_ratio >= 0) 1104 else if (st->voice_ratio >= 0)
1105 {
543 voice_est = st->voice_ratio*327>>8; 1106 voice_est = st->voice_ratio*327>>8;
544 else if (st->application == OPUS_APPLICATION_VOIP) 1107 /* For AUDIO, never be more than 90% confident of having speech */
1108 if (st->application == OPUS_APPLICATION_AUDIO)
1109 voice_est = IMIN(voice_est, 115);
1110 } else if (st->application == OPUS_APPLICATION_VOIP)
545 voice_est = 115; 1111 voice_est = 115;
546 else 1112 else
547 voice_est = 48; 1113 voice_est = 48;
548 1114
549 if (st->force_channels!=OPUS_AUTO && st->channels == 2) 1115 if (st->force_channels!=OPUS_AUTO && st->channels == 2)
550 { 1116 {
551 st->stream_channels = st->force_channels; 1117 st->stream_channels = st->force_channels;
552 } else { 1118 } else {
553 #ifdef FUZZING 1119 #ifdef FUZZING
554 /* Random mono/stereo decision */ 1120 /* Random mono/stereo decision */
555 if (st->channels == 2 && (rand()&0x1F)==0) 1121 if (st->channels == 2 && (rand()&0x1F)==0)
556 st->stream_channels = 3-st->stream_channels; 1122 st->stream_channels = 3-st->stream_channels;
557 #else 1123 #else
558 /* Rate-dependent mono-stereo decision */ 1124 /* Rate-dependent mono-stereo decision */
559 if (st->channels == 2) 1125 if (st->channels == 2)
560 { 1126 {
561 opus_int32 stereo_threshold; 1127 opus_int32 stereo_threshold;
562 stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(ste reo_voice_threshold-stereo_music_threshold))>>14); 1128 stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(ste reo_voice_threshold-stereo_music_threshold))>>14);
563 if (st->stream_channels == 2) 1129 if (st->stream_channels == 2)
564 stereo_threshold -= 4000; 1130 stereo_threshold -= 1000;
565 else 1131 else
566 stereo_threshold += 4000; 1132 stereo_threshold += 1000;
567 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; 1133 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
568 } else { 1134 } else {
569 st->stream_channels = st->channels; 1135 st->stream_channels = st->channels;
570 } 1136 }
571 #endif 1137 #endif
572 } 1138 }
573 1139
574 /* Mode selection depending on application and signal type */ 1140 /* Mode selection depending on application and signal type */
575 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1141 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
576 { 1142 {
577 st->mode = MODE_CELT_ONLY; 1143 st->mode = MODE_CELT_ONLY;
578 } else if (st->user_forced_mode == OPUS_AUTO) 1144 } else if (st->user_forced_mode == OPUS_AUTO)
579 { 1145 {
580 #ifdef FUZZING 1146 #ifdef FUZZING
581 /* Random mode switching */ 1147 /* Random mode switching */
582 if ((rand()&0xF)==0) 1148 if ((rand()&0xF)==0)
583 { 1149 {
584 if ((rand()&0x1)==0) 1150 if ((rand()&0x1)==0)
585 st->mode = MODE_CELT_ONLY; 1151 st->mode = MODE_CELT_ONLY;
586 else 1152 else
587 st->mode = MODE_SILK_ONLY; 1153 st->mode = MODE_SILK_ONLY;
588 } else { 1154 } else {
589 if (st->prev_mode==MODE_CELT_ONLY) 1155 if (st->prev_mode==MODE_CELT_ONLY)
590 st->mode = MODE_CELT_ONLY; 1156 st->mode = MODE_CELT_ONLY;
591 else 1157 else
592 st->mode = MODE_SILK_ONLY; 1158 st->mode = MODE_SILK_ONLY;
593 } 1159 }
594 #else 1160 #else
595 int chan;
596 opus_int32 mode_voice, mode_music; 1161 opus_int32 mode_voice, mode_music;
597 opus_int32 threshold; 1162 opus_int32 threshold;
598 1163
599 chan = (st->channels==2) && st->force_channels!=1; 1164 /* Interpolate based on stereo width */
600 mode_voice = mode_thresholds[chan][0]; 1165 mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_threshol ds[0][0])
601 mode_music = mode_thresholds[chan][1]; 1166 + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1167 mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_threshol ds[1][1])
1168 + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1169 /* Interpolate based on speech/music probability */
602 threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>> 14); 1170 threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>> 14);
1171 /* Bias towards SILK for VoIP because of some useful features */
1172 if (st->application == OPUS_APPLICATION_VOIP)
1173 threshold += 8000;
603 1174
1175 /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
604 /* Hysteresis */ 1176 /* Hysteresis */
605 if (st->prev_mode == MODE_CELT_ONLY) 1177 if (st->prev_mode == MODE_CELT_ONLY)
606 threshold -= 4000; 1178 threshold -= 4000;
607 else if (st->prev_mode>0) 1179 else if (st->prev_mode>0)
608 threshold += 4000; 1180 threshold += 4000;
609 1181
610 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY; 1182 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
611 1183
612 /* When FEC is enabled and there's enough packet loss, use SILK */ 1184 /* When FEC is enabled and there's enough packet loss, use SILK */
613 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (1 28-voice_est)>>4) 1185 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (1 28-voice_est)>>4)
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 redundancy=0; 1223 redundancy=0;
652 } 1224 }
653 } 1225 }
654 } 1226 }
655 /* For the first frame at a new SILK bandwidth */ 1227 /* For the first frame at a new SILK bandwidth */
656 if (st->silk_bw_switch) 1228 if (st->silk_bw_switch)
657 { 1229 {
658 redundancy = 1; 1230 redundancy = 1;
659 celt_to_silk = 1; 1231 celt_to_silk = 1;
660 st->silk_bw_switch = 0; 1232 st->silk_bw_switch = 0;
1233 prefill=1;
661 } 1234 }
662 1235
663 if (redundancy) 1236 if (redundancy)
664 { 1237 {
665 /* Fair share of the max size allowed */ 1238 /* Fair share of the max size allowed */
666 redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(fra me_size+st->Fs/200)); 1239 redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(fra me_size+st->Fs/200));
667 /* For VBR, target the actual bitrate (subject to the limit above) */ 1240 /* For VBR, target the actual bitrate (subject to the limit above) */
668 if (st->use_vbr) 1241 if (st->use_vbr)
669 redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600); 1242 redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600);
670 } 1243 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
743 /* Prevents Opus from wasting bits on frequencies that are above 1316 /* Prevents Opus from wasting bits on frequencies that are above
744 the Nyquist rate of the input signal */ 1317 the Nyquist rate of the input signal */
745 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND) 1318 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
746 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1319 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
747 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1320 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
748 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1321 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
749 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND) 1322 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
750 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1323 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
751 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND) 1324 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
752 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1325 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1326 #ifndef FIXED_POINT
1327 /* Use detected bandwidth to reduce the encoded bandwidth. */
1328 if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1329 {
1330 int min_detected_bandwidth;
1331 /* Makes bandwidth detection more conservative just in case the detector
1332 gets it wrong when we could have coded a high bandwidth transparently.
1333 When operating in SILK/hybrid mode, we don't go below wideband to avoi d
1334 more complicated switches that require redundancy. */
1335 if (st->bitrate_bps <= 18000*st->stream_channels && st->mode == MODE_CELT _ONLY)
1336 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1337 else if (st->bitrate_bps <= 24000*st->stream_channels && st->mode == MODE _CELT_ONLY)
1338 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1339 else if (st->bitrate_bps <= 30000*st->stream_channels)
1340 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1341 else if (st->bitrate_bps <= 44000*st->stream_channels)
1342 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1343 else
1344 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1345
1346 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwi dth);
1347 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1348 }
1349 #endif
1350 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
753 1351
754 /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode * / 1352 /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode * /
755 if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8)) 1353 if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
756 st->mode = MODE_CELT_ONLY; 1354 st->mode = MODE_CELT_ONLY;
757 1355
758 /* CELT mode doesn't support mediumband, use wideband instead */ 1356 /* CELT mode doesn't support mediumband, use wideband instead */
759 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) 1357 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND )
760 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1358 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1359 if (st->lfe)
1360 {
1361 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1362 st->mode = MODE_CELT_ONLY;
1363 }
761 1364
762 /* Can't support higher than wideband for >20 ms frames */ 1365 /* Can't support higher than wideband for >20 ms frames */
763 if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)) 1366 if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND))
764 { 1367 {
765 VARDECL(unsigned char, tmp_data); 1368 VARDECL(unsigned char, tmp_data);
766 int nb_frames; 1369 int nb_frames;
767 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono; 1370 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
768 OpusRepacketizer rp; 1371 VARDECL(OpusRepacketizer, rp);
769 opus_int32 bytes_per_frame; 1372 opus_int32 bytes_per_frame;
770 1373
1374 if (analysis_read_pos_bak!= -1)
1375 {
1376 st->analysis.read_pos = analysis_read_pos_bak;
1377 st->analysis.read_subframe = analysis_read_subframe_bak;
1378 }
771 1379
772 nb_frames = frame_size > st->Fs/25 ? 3 : 2; 1380 nb_frames = frame_size > st->Fs/25 ? 3 : 2;
773 bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames); 1381 bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames);
774 1382
775 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char); 1383 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
776 1384
777 opus_repacketizer_init(&rp); 1385 ALLOC(rp, 1, OpusRepacketizer);
1386 opus_repacketizer_init(rp);
778 1387
779 bak_mode = st->user_forced_mode; 1388 bak_mode = st->user_forced_mode;
780 bak_bandwidth = st->user_bandwidth; 1389 bak_bandwidth = st->user_bandwidth;
781 bak_channels = st->force_channels; 1390 bak_channels = st->force_channels;
782 1391
783 st->user_forced_mode = st->mode; 1392 st->user_forced_mode = st->mode;
784 st->user_bandwidth = st->bandwidth; 1393 st->user_bandwidth = st->bandwidth;
785 st->force_channels = st->stream_channels; 1394 st->force_channels = st->stream_channels;
786 bak_to_mono = st->silk_mode.toMono; 1395 bak_to_mono = st->silk_mode.toMono;
787 1396
788 if (bak_to_mono) 1397 if (bak_to_mono)
789 st->force_channels = 1; 1398 st->force_channels = 1;
790 else 1399 else
791 st->prev_channels = st->stream_channels; 1400 st->prev_channels = st->stream_channels;
792 for (i=0;i<nb_frames;i++) 1401 for (i=0;i<nb_frames;i++)
793 { 1402 {
794 int tmp_len; 1403 int tmp_len;
795 st->silk_mode.toMono = 0; 1404 st->silk_mode.toMono = 0;
796 /* When switching from SILK/Hybrid to CELT, only ask for a switch at t he last frame */ 1405 /* When switching from SILK/Hybrid to CELT, only ask for a switch at t he last frame */
797 if (to_celt && i==nb_frames-1) 1406 if (to_celt && i==nb_frames-1)
798 st->user_forced_mode = MODE_CELT_ONLY; 1407 st->user_forced_mode = MODE_CELT_ONLY;
799 tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->F s/50, tmp_data+i*bytes_per_frame, bytes_per_frame); 1408 tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->F s/50,
1409 tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth,
1410 NULL, 0, c1, c2, analysis_channels, downmix);
800 if (tmp_len<0) 1411 if (tmp_len<0)
801 { 1412 {
802 RESTORE_STACK; 1413 RESTORE_STACK;
803 return OPUS_INTERNAL_ERROR; 1414 return OPUS_INTERNAL_ERROR;
804 } 1415 }
805 ret = opus_repacketizer_cat(&rp, tmp_data+i*bytes_per_frame, tmp_len); 1416 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
806 if (ret<0) 1417 if (ret<0)
807 { 1418 {
808 RESTORE_STACK; 1419 RESTORE_STACK;
809 return OPUS_INTERNAL_ERROR; 1420 return OPUS_INTERNAL_ERROR;
810 } 1421 }
811 } 1422 }
812 ret = opus_repacketizer_out(&rp, data, out_data_bytes); 1423 ret = opus_repacketizer_out(rp, data, out_data_bytes);
813 if (ret<0) 1424 if (ret<0)
814 { 1425 {
815 RESTORE_STACK; 1426 RESTORE_STACK;
816 return OPUS_INTERNAL_ERROR; 1427 return OPUS_INTERNAL_ERROR;
817 } 1428 }
818 st->user_forced_mode = bak_mode; 1429 st->user_forced_mode = bak_mode;
819 st->user_bandwidth = bak_bandwidth; 1430 st->user_bandwidth = bak_bandwidth;
820 st->force_channels = bak_channels; 1431 st->force_channels = bak_channels;
821 st->silk_mode.toMono = bak_to_mono; 1432 st->silk_mode.toMono = bak_to_mono;
822 RESTORE_STACK; 1433 RESTORE_STACK;
823 return ret; 1434 return ret;
824 } 1435 }
825
826 curr_bandwidth = st->bandwidth; 1436 curr_bandwidth = st->bandwidth;
827 1437
828 /* Chooses the appropriate mode for speech 1438 /* Chooses the appropriate mode for speech
829 *NEVER* switch to/from CELT-only mode here as this will invalidate some a ssumptions */ 1439 *NEVER* switch to/from CELT-only mode here as this will invalidate some a ssumptions */
830 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1440 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
831 st->mode = MODE_HYBRID; 1441 st->mode = MODE_HYBRID;
832 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) 1442 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
833 st->mode = MODE_SILK_ONLY; 1443 st->mode = MODE_SILK_ONLY;
834 1444
835 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c urr_bandwidth); */ 1445 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c urr_bandwidth); */
836 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame _size / (st->Fs * 8)) - 1; 1446 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame _size / (st->Fs * 8)) - 1;
837 1447
838 data += 1; 1448 data += 1;
839 1449
840 ec_enc_init(&enc, data, max_data_bytes-1); 1450 ec_enc_init(&enc, data, max_data_bytes-1);
841 1451
842 ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16); 1452 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16);
843 for (i=0;i<delay_compensation*st->channels;i++) 1453 for (i=0;i<total_buffer*st->channels;i++)
844 pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st- >channels+i]; 1454 pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-total_buffer)*st->chann els+i];
845 1455
846 if (st->mode == MODE_CELT_ONLY) 1456 if (st->mode == MODE_CELT_ONLY)
847 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 1457 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
848 else 1458 else
849 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_ smth1_Q15; 1459 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_ smth1_Q15;
850 1460
851 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, 1461 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
852 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP _SMTH_COEF2, 16 ) ); 1462 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP _SMTH_COEF2, 16 ) );
853 1463
854 /* convert from log scale to Hertz */ 1464 /* convert from log scale to Hertz */
855 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); 1465 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
856 1466
857 if (st->application == OPUS_APPLICATION_VOIP) 1467 if (st->application == OPUS_APPLICATION_VOIP)
858 { 1468 {
859 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[delay_compensation*st->channels], st-> hp_mem, frame_size, st->channels, st->Fs); 1469 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem , frame_size, st->channels, st->Fs);
860 } else { 1470 } else {
861 for (i=0;i<frame_size*st->channels;i++) 1471 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_ size, st->channels, st->Fs);
862 pcm_buf[delay_compensation*st->channels + i] = pcm[i];
863 } 1472 }
864 1473
1474
1475
865 /* SILK processing */ 1476 /* SILK processing */
1477 HB_gain = Q15ONE;
866 if (st->mode != MODE_CELT_ONLY) 1478 if (st->mode != MODE_CELT_ONLY)
867 { 1479 {
1480 opus_int32 total_bitRate, celt_rate;
868 #ifdef FIXED_POINT 1481 #ifdef FIXED_POINT
869 const opus_int16 *pcm_silk; 1482 const opus_int16 *pcm_silk;
870 #else 1483 #else
871 VARDECL(opus_int16, pcm_silk); 1484 VARDECL(opus_int16, pcm_silk);
872 ALLOC(pcm_silk, st->channels*frame_size, opus_int16); 1485 ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
873 #endif 1486 #endif
874 st->silk_mode.bitRate = 8*bytes_target*frame_rate; 1487
1488 /* Distribute bits between SILK and CELT */
1489 total_bitRate = 8 * bytes_target * frame_rate;
875 if( st->mode == MODE_HYBRID ) { 1490 if( st->mode == MODE_HYBRID ) {
876 st->silk_mode.bitRate /= st->stream_channels; 1491 int HB_gain_ref;
1492 /* Base rate for SILK */
1493 st->silk_mode.bitRate = st->stream_channels * ( 5000 + 1000 * ( st-> Fs == 100 * frame_size ) );
877 if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) { 1494 if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) {
878 if( st->Fs == 100 * frame_size ) { 1495 /* SILK gets 2/3 of the remaining bits */
879 /* 24 kHz, 10 ms */ 1496 st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 2 / 3;
880 st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + s t->use_vbr * 1000 ) * 2 ) / 3; 1497 } else { /* FULLBAND */
881 } else { 1498 /* SILK gets 3/5 of the remaining bits */
882 /* 24 kHz, 20 ms */ 1499 st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 3 / 5;
883 st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 1000 + s t->use_vbr * 1000 ) * 2 ) / 3;
884 }
885 } else {
886 if( st->Fs == 100 * frame_size ) {
887 /* 48 kHz, 10 ms */
888 st->silk_mode.bitRate = ( st->silk_mode.bitRate + 8000 + st- >use_vbr * 3000 ) / 2;
889 } else {
890 /* 48 kHz, 20 ms */
891 st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st- >use_vbr * 1000 ) / 2;
892 }
893 } 1500 }
894 st->silk_mode.bitRate *= st->stream_channels; 1501 /* Don't let SILK use more than 80% */
895 /* don't let SILK use more than 80% */ 1502 if( st->silk_mode.bitRate > total_bitRate * 4/5 ) {
896 if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) { 1503 st->silk_mode.bitRate = total_bitRate * 4/5;
897 st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
898 } 1504 }
1505 /* Increasingly attenuate high band when it gets allocated fewer bit s */
1506 celt_rate = total_bitRate - st->silk_mode.bitRate;
1507 HB_gain_ref = (curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND) ? 300 0 : 3600;
1508 HB_gain = SHL32((opus_val32)celt_rate, 9) / SHR32((opus_val32)celt_r ate + st->stream_channels * HB_gain_ref, 6);
1509 HB_gain = HB_gain < Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE;
1510 } else {
1511 /* SILK gets all bits */
1512 st->silk_mode.bitRate = total_bitRate;
1513 }
1514
1515 /* Surround masking for SILK */
1516 if (st->energy_masking && st->use_vbr && !st->lfe)
1517 {
1518 opus_val32 mask_sum=0;
1519 opus_val16 masking_depth;
1520 opus_int32 rate_offset;
1521 int c;
1522 int end = 17;
1523 opus_int16 srate = 16000;
1524 if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
1525 {
1526 end = 13;
1527 srate = 8000;
1528 } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1529 {
1530 end = 15;
1531 srate = 12000;
1532 }
1533 for (c=0;c<st->channels;c++)
1534 {
1535 for(i=0;i<end;i++)
1536 {
1537 opus_val16 mask;
1538 mask = MAX16(MIN16(st->energy_masking[21*c+i],
1539 QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1540 if (mask > 0)
1541 mask = HALF16(mask);
1542 mask_sum += mask;
1543 }
1544 }
1545 /* Conservative rate reduction, we cut the masking in half */
1546 masking_depth = HALF16(mask_sum / end*st->channels);
1547 rate_offset = PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT);
1548 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
1549 rate_offset += QCONST16(.4f, DB_SHIFT);
1550 st->silk_mode.bitRate += rate_offset;
1551 bytes_target += rate_offset * frame_size / (8 * st->Fs);
899 } 1552 }
900 1553
901 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; 1554 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
902 st->silk_mode.nChannelsAPI = st->channels; 1555 st->silk_mode.nChannelsAPI = st->channels;
903 st->silk_mode.nChannelsInternal = st->stream_channels; 1556 st->silk_mode.nChannelsInternal = st->stream_channels;
904 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 1557 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
905 st->silk_mode.desiredInternalSampleRate = 8000; 1558 st->silk_mode.desiredInternalSampleRate = 8000;
906 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 1559 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
907 st->silk_mode.desiredInternalSampleRate = 12000; 1560 st->silk_mode.desiredInternalSampleRate = 12000;
908 } else { 1561 } else {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 if (st->silk_mode.useCBR) 1601 if (st->silk_mode.useCBR)
949 { 1602 {
950 st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8; 1603 st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8;
951 /* Reduce the initial target to make it easier to reach the CBR rate */ 1604 /* Reduce the initial target to make it easier to reach the CBR rate */
952 st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000); 1605 st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000);
953 } 1606 }
954 1607
955 if (prefill) 1608 if (prefill)
956 { 1609 {
957 opus_int32 zero=0; 1610 opus_int32 zero=0;
1611 int prefill_offset;
1612 /* Use a smooth onset for the SILK prefill to avoid the encoder tryi ng to encode
1613 a discontinuity. The exact location is what we need to avoid leav ing any "gap"
1614 in the audio when mixing with the redundant CELT frame. Here we c an afford to
1615 overwrite st->delay_buffer because the only thing that uses it be fore it gets
1616 rewritten is tmp_prefill[] and even then only the part after the ramp really
1617 gets used (rather than sent to the encoder and discarded) */
1618 prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensa tion-st->Fs/400);
1619 gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_ offset,
1620 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_ mode->window, st->Fs);
1621 for(i=0;i<prefill_offset;i++)
1622 st->delay_buffer[i]=0;
958 #ifdef FIXED_POINT 1623 #ifdef FIXED_POINT
959 pcm_silk = st->delay_buffer; 1624 pcm_silk = st->delay_buffer;
960 #else 1625 #else
961 for (i=0;i<st->encoder_buffer*st->channels;i++) 1626 for (i=0;i<st->encoder_buffer*st->channels;i++)
962 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]); 1627 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
963 #endif 1628 #endif
964 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 ); 1629 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 );
965 } 1630 }
966 1631
967 #ifdef FIXED_POINT 1632 #ifdef FIXED_POINT
968 pcm_silk = pcm_buf+delay_compensation*st->channels; 1633 pcm_silk = pcm_buf+total_buffer*st->channels;
969 #else 1634 #else
970 for (i=0;i<frame_size*st->channels;i++) 1635 for (i=0;i<frame_size*st->channels;i++)
971 pcm_silk[i] = FLOAT2INT16(pcm_buf[delay_compensation*st->channels + i]); 1636 pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]);
972 #endif 1637 #endif
973 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 ); 1638 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
974 if( ret ) { 1639 if( ret ) {
975 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/ 1640 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
976 /* Handle error */ 1641 /* Handle error */
977 RESTORE_STACK; 1642 RESTORE_STACK;
978 return OPUS_INTERNAL_ERROR; 1643 return OPUS_INTERNAL_ERROR;
979 } 1644 }
980 if (nBytes==0) 1645 if (nBytes==0)
981 { 1646 {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1046 len += st->mode == MODE_HYBRID ? 3 : 1; 1711 len += st->mode == MODE_HYBRID ? 3 : 1;
1047 if( st->use_vbr ) { 1712 if( st->use_vbr ) {
1048 nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * f rame_size) / (8 * st->Fs); 1713 nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * f rame_size) / (8 * st->Fs);
1049 } else { 1714 } else {
1050 /* check if SILK used up too much */ 1715 /* check if SILK used up too much */
1051 nb_compr_bytes = len > bytes_target ? len : bytes_target; 1716 nb_compr_bytes = len > bytes_target ? len : bytes_target;
1052 } 1717 }
1053 } else { 1718 } else {
1054 if (st->use_vbr) 1719 if (st->use_vbr)
1055 { 1720 {
1721 opus_int32 bonus=0;
1722 #ifndef DISABLE_FLOAT_API
1723 if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != st->Fs/50)
1724 {
1725 bonus = (60*st->stream_channels+40)*(st->Fs/frame_size-50);
1726 if (analysis_info.valid)
1727 bonus = (opus_int32)(bonus*(1.f+.5f*analysis_info.tonality ));
1728 }
1729 #endif
1056 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); 1730 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
1057 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_const raint)); 1731 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_const raint));
1058 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps)); 1732 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps+bonu s));
1059 nb_compr_bytes = max_data_bytes-1-redundancy_bytes; 1733 nb_compr_bytes = max_data_bytes-1-redundancy_bytes;
1060 } else { 1734 } else {
1061 nb_compr_bytes = bytes_target; 1735 nb_compr_bytes = bytes_target;
1062 } 1736 }
1063 } 1737 }
1064 1738
1065 } else { 1739 } else {
1066 nb_compr_bytes = 0; 1740 nb_compr_bytes = 0;
1067 } 1741 }
1068 1742
1069 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16); 1743 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
1070 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0) 1744 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
1071 { 1745 {
1072 for (i=0;i<st->channels*st->Fs/400;i++) 1746 for (i=0;i<st->channels*st->Fs/400;i++)
1073 tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-st->delay_compen sation-st->Fs/400)*st->channels + i]; 1747 tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-total_buffer-st- >Fs/400)*st->channels + i];
1074 } 1748 }
1075 1749
1076 for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+delay_compensation)) ;i++) 1750 for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+total_buffer));i++)
1077 st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size]; 1751 st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size];
1078 for (;i<st->encoder_buffer*st->channels;i++) 1752 for (;i<st->encoder_buffer*st->channels;i++)
1079 st->delay_buffer[i] = pcm_buf[(frame_size+delay_compensation-st->encoder _buffer)*st->channels+i]; 1753 st->delay_buffer[i] = pcm_buf[(frame_size+total_buffer-st->encoder_buffe r)*st->channels+i];
1080 1754
1081 1755 /* gain_fade() and stereo_fade() need to be after the buffer copying
1756 because we don't want any of this to affect the SILK part */
1757 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
1758 gain_fade(pcm_buf, pcm_buf,
1759 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->chan nels, celt_mode->window, st->Fs);
1760 }
1761 st->prev_HB_gain = HB_gain;
1082 if (st->mode != MODE_HYBRID || st->stream_channels==1) 1762 if (st->mode != MODE_HYBRID || st->stream_channels==1)
1083 st->silk_mode.stereoWidth_Q14 = 1<<14; 1763 st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),IMAX(0,st->bitrate_bps-32000 ));
1084 if( st->channels == 2 ) { 1764 if( !st->energy_masking && st->channels == 2 ) {
1085 /* Apply stereo width reduction (at low bitrates) */ 1765 /* Apply stereo width reduction (at low bitrates) */
1086 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth _Q14 < (1 << 14) ) { 1766 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth _Q14 < (1 << 14) ) {
1087 opus_val16 g1, g2; 1767 opus_val16 g1, g2;
1088 const CELTMode *celt_mode;
1089
1090 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1091 g1 = st->hybrid_stereo_width_Q14; 1768 g1 = st->hybrid_stereo_width_Q14;
1092 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14); 1769 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
1093 #ifdef FIXED_POINT 1770 #ifdef FIXED_POINT
1094 g1 = g1==16384 ? Q15ONE : SHL16(g1,1); 1771 g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
1095 g2 = g2==16384 ? Q15ONE : SHL16(g2,1); 1772 g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
1096 #else 1773 #else
1097 g1 *= (1.f/16384); 1774 g1 *= (1.f/16384);
1098 g2 *= (1.f/16384); 1775 g2 *= (1.f/16384);
1099 #endif 1776 #endif
1100 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, 1777 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 if (st->mode == MODE_SILK_ONLY) 1814 if (st->mode == MODE_SILK_ONLY)
1138 { 1815 {
1139 ret = (ec_tell(&enc)+7)>>3; 1816 ret = (ec_tell(&enc)+7)>>3;
1140 ec_enc_done(&enc); 1817 ec_enc_done(&enc);
1141 nb_compr_bytes = ret; 1818 nb_compr_bytes = ret;
1142 } else { 1819 } else {
1143 nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes ); 1820 nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes );
1144 ec_enc_shrink(&enc, nb_compr_bytes); 1821 ec_enc_shrink(&enc, nb_compr_bytes);
1145 } 1822 }
1146 1823
1824 #ifndef DISABLE_FLOAT_API
1825 if (redundancy || st->mode != MODE_SILK_ONLY)
1826 celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info));
1827 #endif
1147 1828
1148 /* 5 ms redundant frame for CELT->SILK */ 1829 /* 5 ms redundant frame for CELT->SILK */
1149 if (redundancy && celt_to_silk) 1830 if (redundancy && celt_to_silk)
1150 { 1831 {
1151 int err; 1832 int err;
1152 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); 1833 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
1153 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); 1834 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1154 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_b ytes, redundancy_bytes, NULL); 1835 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_b ytes, redundancy_bytes, NULL);
1155 if (err < 0) 1836 if (err < 0)
1156 { 1837 {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1261 } 1942 }
1262 ret = max_data_bytes; 1943 ret = max_data_bytes;
1263 } 1944 }
1264 RESTORE_STACK; 1945 RESTORE_STACK;
1265 return ret; 1946 return ret;
1266 } 1947 }
1267 1948
1268 #ifdef FIXED_POINT 1949 #ifdef FIXED_POINT
1269 1950
1270 #ifndef DISABLE_FLOAT_API 1951 #ifndef DISABLE_FLOAT_API
1271 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int frame_size, 1952 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size,
1272 unsigned char *data, opus_int32 max_data_bytes) 1953 unsigned char *data, opus_int32 max_data_bytes)
1273 { 1954 {
1274 int i, ret; 1955 int i, ret;
1956 int frame_size;
1957 int delay_compensation;
1275 VARDECL(opus_int16, in); 1958 VARDECL(opus_int16, in);
1276 ALLOC_STACK; 1959 ALLOC_STACK;
1277 1960
1278 if(frame_size<0) 1961 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1279 { 1962 delay_compensation = 0;
1280 RESTORE_STACK; 1963 else
1281 return OPUS_BAD_ARG; 1964 delay_compensation = st->delay_compensation;
1282 } 1965 frame_size = compute_frame_size(pcm, analysis_frame_size,
1966 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
1967 delay_compensation, downmix_float, st->analysis.subframe_mem);
1283 1968
1284 ALLOC(in, frame_size*st->channels, opus_int16); 1969 ALLOC(in, frame_size*st->channels, opus_int16);
1285 1970
1286 for (i=0;i<frame_size*st->channels;i++) 1971 for (i=0;i<frame_size*st->channels;i++)
1287 in[i] = FLOAT2INT16(pcm[i]); 1972 in[i] = FLOAT2INT16(pcm[i]);
1288 ret = opus_encode(st, in, frame_size, data, max_data_bytes); 1973 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, a nalysis_frame_size, 0, -2, st->channels, downmix_float);
1289 RESTORE_STACK; 1974 RESTORE_STACK;
1290 return ret; 1975 return ret;
1291 } 1976 }
1292 #endif 1977 #endif
1293 1978
1979 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size,
1980 unsigned char *data, opus_int32 out_data_bytes)
1981 {
1982 int frame_size;
1983 int delay_compensation;
1984 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1985 delay_compensation = 0;
1986 else
1987 delay_compensation = st->delay_compensation;
1988 frame_size = compute_frame_size(pcm, analysis_frame_size,
1989 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
1990 delay_compensation, downmix_float, st->analysis.subframe_mem);
1991 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int);
1992 }
1993
1294 #else 1994 #else
1295 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size, 1995 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size,
1296 unsigned char *data, opus_int32 max_data_bytes) 1996 unsigned char *data, opus_int32 max_data_bytes)
1297 { 1997 {
1298 int i, ret; 1998 int i, ret;
1999 int frame_size;
2000 int delay_compensation;
1299 VARDECL(float, in); 2001 VARDECL(float, in);
1300 ALLOC_STACK; 2002 ALLOC_STACK;
1301 2003
2004 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2005 delay_compensation = 0;
2006 else
2007 delay_compensation = st->delay_compensation;
2008 frame_size = compute_frame_size(pcm, analysis_frame_size,
2009 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2010 delay_compensation, downmix_float, st->analysis.subframe_mem);
2011
1302 ALLOC(in, frame_size*st->channels, float); 2012 ALLOC(in, frame_size*st->channels, float);
1303 2013
1304 for (i=0;i<frame_size*st->channels;i++) 2014 for (i=0;i<frame_size*st->channels;i++)
1305 in[i] = (1.0f/32768)*pcm[i]; 2015 in[i] = (1.0f/32768)*pcm[i];
1306 ret = opus_encode_float(st, in, frame_size, data, max_data_bytes); 2016 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, a nalysis_frame_size, 0, -2, st->channels, downmix_int);
1307 RESTORE_STACK; 2017 RESTORE_STACK;
1308 return ret; 2018 return ret;
1309 } 2019 }
2020 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size,
2021 unsigned char *data, opus_int32 out_data_bytes)
2022 {
2023 int frame_size;
2024 int delay_compensation;
2025 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2026 delay_compensation = 0;
2027 else
2028 delay_compensation = st->delay_compensation;
2029 frame_size = compute_frame_size(pcm, analysis_frame_size,
2030 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2031 delay_compensation, downmix_float, st->analysis.subframe_mem);
2032 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24,
2033 pcm, analysis_frame_size, 0, -2, st->channels, down mix_float);
2034 }
1310 #endif 2035 #endif
1311 2036
1312 2037
1313 int opus_encoder_ctl(OpusEncoder *st, int request, ...) 2038 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
1314 { 2039 {
1315 int ret; 2040 int ret;
1316 CELTEncoder *celt_enc; 2041 CELTEncoder *celt_enc;
1317 va_list ap; 2042 va_list ap;
1318 2043
1319 ret = OPUS_OK; 2044 ret = OPUS_OK;
(...skipping 12 matching lines...) Expand all
1332 { 2057 {
1333 ret = OPUS_BAD_ARG; 2058 ret = OPUS_BAD_ARG;
1334 break; 2059 break;
1335 } 2060 }
1336 st->application = value; 2061 st->application = value;
1337 } 2062 }
1338 break; 2063 break;
1339 case OPUS_GET_APPLICATION_REQUEST: 2064 case OPUS_GET_APPLICATION_REQUEST:
1340 { 2065 {
1341 opus_int32 *value = va_arg(ap, opus_int32*); 2066 opus_int32 *value = va_arg(ap, opus_int32*);
2067 if (!value)
2068 {
2069 goto bad_arg;
2070 }
1342 *value = st->application; 2071 *value = st->application;
1343 } 2072 }
1344 break; 2073 break;
1345 case OPUS_SET_BITRATE_REQUEST: 2074 case OPUS_SET_BITRATE_REQUEST:
1346 { 2075 {
1347 opus_int32 value = va_arg(ap, opus_int32); 2076 opus_int32 value = va_arg(ap, opus_int32);
1348 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) 2077 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
1349 { 2078 {
1350 if (value <= 0) 2079 if (value <= 0)
1351 goto bad_arg; 2080 goto bad_arg;
1352 else if (value <= 500) 2081 else if (value <= 500)
1353 value = 500; 2082 value = 500;
1354 else if (value > (opus_int32)300000*st->channels) 2083 else if (value > (opus_int32)300000*st->channels)
1355 value = (opus_int32)300000*st->channels; 2084 value = (opus_int32)300000*st->channels;
1356 } 2085 }
1357 st->user_bitrate_bps = value; 2086 st->user_bitrate_bps = value;
1358 } 2087 }
1359 break; 2088 break;
1360 case OPUS_GET_BITRATE_REQUEST: 2089 case OPUS_GET_BITRATE_REQUEST:
1361 { 2090 {
1362 opus_int32 *value = va_arg(ap, opus_int32*); 2091 opus_int32 *value = va_arg(ap, opus_int32*);
2092 if (!value)
2093 {
2094 goto bad_arg;
2095 }
1363 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276); 2096 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
1364 } 2097 }
1365 break; 2098 break;
1366 case OPUS_SET_FORCE_CHANNELS_REQUEST: 2099 case OPUS_SET_FORCE_CHANNELS_REQUEST:
1367 { 2100 {
1368 opus_int32 value = va_arg(ap, opus_int32); 2101 opus_int32 value = va_arg(ap, opus_int32);
1369 if((value<1 || value>st->channels) && value != OPUS_AUTO) 2102 if((value<1 || value>st->channels) && value != OPUS_AUTO)
1370 return OPUS_BAD_ARG; 2103 {
2104 goto bad_arg;
2105 }
1371 st->force_channels = value; 2106 st->force_channels = value;
1372 } 2107 }
1373 break; 2108 break;
1374 case OPUS_GET_FORCE_CHANNELS_REQUEST: 2109 case OPUS_GET_FORCE_CHANNELS_REQUEST:
1375 { 2110 {
1376 opus_int32 *value = va_arg(ap, opus_int32*); 2111 opus_int32 *value = va_arg(ap, opus_int32*);
2112 if (!value)
2113 {
2114 goto bad_arg;
2115 }
1377 *value = st->force_channels; 2116 *value = st->force_channels;
1378 } 2117 }
1379 break; 2118 break;
1380 case OPUS_SET_MAX_BANDWIDTH_REQUEST: 2119 case OPUS_SET_MAX_BANDWIDTH_REQUEST:
1381 { 2120 {
1382 opus_int32 value = va_arg(ap, opus_int32); 2121 opus_int32 value = va_arg(ap, opus_int32);
1383 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULL BAND) 2122 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULL BAND)
1384 return OPUS_BAD_ARG; 2123 {
2124 goto bad_arg;
2125 }
1385 st->max_bandwidth = value; 2126 st->max_bandwidth = value;
1386 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 2127 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1387 st->silk_mode.maxInternalSampleRate = 8000; 2128 st->silk_mode.maxInternalSampleRate = 8000;
1388 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 2129 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1389 st->silk_mode.maxInternalSampleRate = 12000; 2130 st->silk_mode.maxInternalSampleRate = 12000;
1390 } else { 2131 } else {
1391 st->silk_mode.maxInternalSampleRate = 16000; 2132 st->silk_mode.maxInternalSampleRate = 16000;
1392 } 2133 }
1393 } 2134 }
1394 break; 2135 break;
1395 case OPUS_GET_MAX_BANDWIDTH_REQUEST: 2136 case OPUS_GET_MAX_BANDWIDTH_REQUEST:
1396 { 2137 {
1397 opus_int32 *value = va_arg(ap, opus_int32*); 2138 opus_int32 *value = va_arg(ap, opus_int32*);
2139 if (!value)
2140 {
2141 goto bad_arg;
2142 }
1398 *value = st->max_bandwidth; 2143 *value = st->max_bandwidth;
1399 } 2144 }
1400 break; 2145 break;
1401 case OPUS_SET_BANDWIDTH_REQUEST: 2146 case OPUS_SET_BANDWIDTH_REQUEST:
1402 { 2147 {
1403 opus_int32 value = va_arg(ap, opus_int32); 2148 opus_int32 value = va_arg(ap, opus_int32);
1404 if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FUL LBAND) && value != OPUS_AUTO) 2149 if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FUL LBAND) && value != OPUS_AUTO)
1405 return OPUS_BAD_ARG; 2150 {
2151 goto bad_arg;
2152 }
1406 st->user_bandwidth = value; 2153 st->user_bandwidth = value;
1407 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 2154 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1408 st->silk_mode.maxInternalSampleRate = 8000; 2155 st->silk_mode.maxInternalSampleRate = 8000;
1409 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 2156 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1410 st->silk_mode.maxInternalSampleRate = 12000; 2157 st->silk_mode.maxInternalSampleRate = 12000;
1411 } else { 2158 } else {
1412 st->silk_mode.maxInternalSampleRate = 16000; 2159 st->silk_mode.maxInternalSampleRate = 16000;
1413 } 2160 }
1414 } 2161 }
1415 break; 2162 break;
1416 case OPUS_GET_BANDWIDTH_REQUEST: 2163 case OPUS_GET_BANDWIDTH_REQUEST:
1417 { 2164 {
1418 opus_int32 *value = va_arg(ap, opus_int32*); 2165 opus_int32 *value = va_arg(ap, opus_int32*);
2166 if (!value)
2167 {
2168 goto bad_arg;
2169 }
1419 *value = st->bandwidth; 2170 *value = st->bandwidth;
1420 } 2171 }
1421 break; 2172 break;
1422 case OPUS_SET_DTX_REQUEST: 2173 case OPUS_SET_DTX_REQUEST:
1423 { 2174 {
1424 opus_int32 value = va_arg(ap, opus_int32); 2175 opus_int32 value = va_arg(ap, opus_int32);
1425 if(value<0 || value>1) 2176 if(value<0 || value>1)
1426 return OPUS_BAD_ARG; 2177 {
2178 goto bad_arg;
2179 }
1427 st->silk_mode.useDTX = value; 2180 st->silk_mode.useDTX = value;
1428 } 2181 }
1429 break; 2182 break;
1430 case OPUS_GET_DTX_REQUEST: 2183 case OPUS_GET_DTX_REQUEST:
1431 { 2184 {
1432 opus_int32 *value = va_arg(ap, opus_int32*); 2185 opus_int32 *value = va_arg(ap, opus_int32*);
2186 if (!value)
2187 {
2188 goto bad_arg;
2189 }
1433 *value = st->silk_mode.useDTX; 2190 *value = st->silk_mode.useDTX;
1434 } 2191 }
1435 break; 2192 break;
1436 case OPUS_SET_COMPLEXITY_REQUEST: 2193 case OPUS_SET_COMPLEXITY_REQUEST:
1437 { 2194 {
1438 opus_int32 value = va_arg(ap, opus_int32); 2195 opus_int32 value = va_arg(ap, opus_int32);
1439 if(value<0 || value>10) 2196 if(value<0 || value>10)
1440 return OPUS_BAD_ARG; 2197 {
2198 goto bad_arg;
2199 }
1441 st->silk_mode.complexity = value; 2200 st->silk_mode.complexity = value;
1442 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value)); 2201 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
1443 } 2202 }
1444 break; 2203 break;
1445 case OPUS_GET_COMPLEXITY_REQUEST: 2204 case OPUS_GET_COMPLEXITY_REQUEST:
1446 { 2205 {
1447 opus_int32 *value = va_arg(ap, opus_int32*); 2206 opus_int32 *value = va_arg(ap, opus_int32*);
2207 if (!value)
2208 {
2209 goto bad_arg;
2210 }
1448 *value = st->silk_mode.complexity; 2211 *value = st->silk_mode.complexity;
1449 } 2212 }
1450 break; 2213 break;
1451 case OPUS_SET_INBAND_FEC_REQUEST: 2214 case OPUS_SET_INBAND_FEC_REQUEST:
1452 { 2215 {
1453 opus_int32 value = va_arg(ap, opus_int32); 2216 opus_int32 value = va_arg(ap, opus_int32);
1454 if(value<0 || value>1) 2217 if(value<0 || value>1)
1455 return OPUS_BAD_ARG; 2218 {
2219 goto bad_arg;
2220 }
1456 st->silk_mode.useInBandFEC = value; 2221 st->silk_mode.useInBandFEC = value;
1457 } 2222 }
1458 break; 2223 break;
1459 case OPUS_GET_INBAND_FEC_REQUEST: 2224 case OPUS_GET_INBAND_FEC_REQUEST:
1460 { 2225 {
1461 opus_int32 *value = va_arg(ap, opus_int32*); 2226 opus_int32 *value = va_arg(ap, opus_int32*);
2227 if (!value)
2228 {
2229 goto bad_arg;
2230 }
1462 *value = st->silk_mode.useInBandFEC; 2231 *value = st->silk_mode.useInBandFEC;
1463 } 2232 }
1464 break; 2233 break;
1465 case OPUS_SET_PACKET_LOSS_PERC_REQUEST: 2234 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1466 { 2235 {
1467 opus_int32 value = va_arg(ap, opus_int32); 2236 opus_int32 value = va_arg(ap, opus_int32);
1468 if (value < 0 || value > 100) 2237 if (value < 0 || value > 100)
1469 return OPUS_BAD_ARG; 2238 {
2239 goto bad_arg;
2240 }
1470 st->silk_mode.packetLossPercentage = value; 2241 st->silk_mode.packetLossPercentage = value;
1471 celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value)); 2242 celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
1472 } 2243 }
1473 break; 2244 break;
1474 case OPUS_GET_PACKET_LOSS_PERC_REQUEST: 2245 case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
1475 { 2246 {
1476 opus_int32 *value = va_arg(ap, opus_int32*); 2247 opus_int32 *value = va_arg(ap, opus_int32*);
2248 if (!value)
2249 {
2250 goto bad_arg;
2251 }
1477 *value = st->silk_mode.packetLossPercentage; 2252 *value = st->silk_mode.packetLossPercentage;
1478 } 2253 }
1479 break; 2254 break;
1480 case OPUS_SET_VBR_REQUEST: 2255 case OPUS_SET_VBR_REQUEST:
1481 { 2256 {
1482 opus_int32 value = va_arg(ap, opus_int32); 2257 opus_int32 value = va_arg(ap, opus_int32);
1483 if(value<0 || value>1) 2258 if(value<0 || value>1)
1484 return OPUS_BAD_ARG; 2259 {
2260 goto bad_arg;
2261 }
1485 st->use_vbr = value; 2262 st->use_vbr = value;
1486 st->silk_mode.useCBR = 1-value; 2263 st->silk_mode.useCBR = 1-value;
1487 } 2264 }
1488 break; 2265 break;
1489 case OPUS_GET_VBR_REQUEST: 2266 case OPUS_GET_VBR_REQUEST:
1490 { 2267 {
1491 opus_int32 *value = va_arg(ap, opus_int32*); 2268 opus_int32 *value = va_arg(ap, opus_int32*);
2269 if (!value)
2270 {
2271 goto bad_arg;
2272 }
1492 *value = st->use_vbr; 2273 *value = st->use_vbr;
1493 } 2274 }
1494 break; 2275 break;
1495 case OPUS_SET_VOICE_RATIO_REQUEST: 2276 case OPUS_SET_VOICE_RATIO_REQUEST:
1496 { 2277 {
1497 opus_int32 value = va_arg(ap, opus_int32); 2278 opus_int32 value = va_arg(ap, opus_int32);
1498 if (value>100 || value<-1) 2279 if (value<-1 || value>100)
1499 goto bad_arg; 2280 {
2281 goto bad_arg;
2282 }
1500 st->voice_ratio = value; 2283 st->voice_ratio = value;
1501 } 2284 }
1502 break; 2285 break;
1503 case OPUS_GET_VOICE_RATIO_REQUEST: 2286 case OPUS_GET_VOICE_RATIO_REQUEST:
1504 { 2287 {
1505 opus_int32 *value = va_arg(ap, opus_int32*); 2288 opus_int32 *value = va_arg(ap, opus_int32*);
2289 if (!value)
2290 {
2291 goto bad_arg;
2292 }
1506 *value = st->voice_ratio; 2293 *value = st->voice_ratio;
1507 } 2294 }
1508 break; 2295 break;
1509 case OPUS_SET_VBR_CONSTRAINT_REQUEST: 2296 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1510 { 2297 {
1511 opus_int32 value = va_arg(ap, opus_int32); 2298 opus_int32 value = va_arg(ap, opus_int32);
1512 if(value<0 || value>1) 2299 if(value<0 || value>1)
1513 return OPUS_BAD_ARG; 2300 {
2301 goto bad_arg;
2302 }
1514 st->vbr_constraint = value; 2303 st->vbr_constraint = value;
1515 } 2304 }
1516 break; 2305 break;
1517 case OPUS_GET_VBR_CONSTRAINT_REQUEST: 2306 case OPUS_GET_VBR_CONSTRAINT_REQUEST:
1518 { 2307 {
1519 opus_int32 *value = va_arg(ap, opus_int32*); 2308 opus_int32 *value = va_arg(ap, opus_int32*);
2309 if (!value)
2310 {
2311 goto bad_arg;
2312 }
1520 *value = st->vbr_constraint; 2313 *value = st->vbr_constraint;
1521 } 2314 }
1522 break; 2315 break;
1523 case OPUS_SET_SIGNAL_REQUEST: 2316 case OPUS_SET_SIGNAL_REQUEST:
1524 { 2317 {
1525 opus_int32 value = va_arg(ap, opus_int32); 2318 opus_int32 value = va_arg(ap, opus_int32);
1526 if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNA L_MUSIC) 2319 if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNA L_MUSIC)
1527 return OPUS_BAD_ARG; 2320 {
2321 goto bad_arg;
2322 }
1528 st->signal_type = value; 2323 st->signal_type = value;
1529 } 2324 }
1530 break; 2325 break;
1531 case OPUS_GET_SIGNAL_REQUEST: 2326 case OPUS_GET_SIGNAL_REQUEST:
1532 { 2327 {
1533 opus_int32 *value = va_arg(ap, opus_int32*); 2328 opus_int32 *value = va_arg(ap, opus_int32*);
2329 if (!value)
2330 {
2331 goto bad_arg;
2332 }
1534 *value = st->signal_type; 2333 *value = st->signal_type;
1535 } 2334 }
1536 break; 2335 break;
1537 case OPUS_GET_LOOKAHEAD_REQUEST: 2336 case OPUS_GET_LOOKAHEAD_REQUEST:
1538 { 2337 {
1539 opus_int32 *value = va_arg(ap, opus_int32*); 2338 opus_int32 *value = va_arg(ap, opus_int32*);
2339 if (!value)
2340 {
2341 goto bad_arg;
2342 }
1540 *value = st->Fs/400; 2343 *value = st->Fs/400;
1541 if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY) 2344 if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1542 *value += st->delay_compensation; 2345 *value += st->delay_compensation;
1543 } 2346 }
1544 break; 2347 break;
1545 case OPUS_GET_SAMPLE_RATE_REQUEST: 2348 case OPUS_GET_SAMPLE_RATE_REQUEST:
1546 { 2349 {
1547 opus_int32 *value = va_arg(ap, opus_int32*); 2350 opus_int32 *value = va_arg(ap, opus_int32*);
1548 if (value==NULL) 2351 if (!value)
1549 { 2352 {
1550 ret = OPUS_BAD_ARG; 2353 goto bad_arg;
1551 break;
1552 } 2354 }
1553 *value = st->Fs; 2355 *value = st->Fs;
1554 } 2356 }
1555 break; 2357 break;
1556 case OPUS_GET_FINAL_RANGE_REQUEST: 2358 case OPUS_GET_FINAL_RANGE_REQUEST:
1557 { 2359 {
1558 opus_uint32 *value = va_arg(ap, opus_uint32*); 2360 opus_uint32 *value = va_arg(ap, opus_uint32*);
2361 if (!value)
2362 {
2363 goto bad_arg;
2364 }
1559 *value = st->rangeFinal; 2365 *value = st->rangeFinal;
1560 } 2366 }
1561 break; 2367 break;
1562 case OPUS_SET_LSB_DEPTH_REQUEST: 2368 case OPUS_SET_LSB_DEPTH_REQUEST:
1563 { 2369 {
1564 opus_int32 value = va_arg(ap, opus_int32); 2370 opus_int32 value = va_arg(ap, opus_int32);
1565 ret = celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(value)); 2371 if (value<8 || value>24)
2372 {
2373 goto bad_arg;
2374 }
2375 st->lsb_depth=value;
1566 } 2376 }
1567 break; 2377 break;
1568 case OPUS_GET_LSB_DEPTH_REQUEST: 2378 case OPUS_GET_LSB_DEPTH_REQUEST:
1569 { 2379 {
1570 opus_int32 *value = va_arg(ap, opus_int32*); 2380 opus_int32 *value = va_arg(ap, opus_int32*);
1571 celt_encoder_ctl(celt_enc, OPUS_GET_LSB_DEPTH(value)); 2381 if (!value)
2382 {
2383 goto bad_arg;
2384 }
2385 *value = st->lsb_depth;
2386 }
2387 break;
2388 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2389 {
2390 opus_int32 value = va_arg(ap, opus_int32);
2391 if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS &&
2392 value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS &&
2393 value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS &&
2394 value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_VARIABL E)
2395 {
2396 goto bad_arg;
2397 }
2398 st->variable_duration = value;
2399 celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value));
2400 }
2401 break;
2402 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
2403 {
2404 opus_int32 *value = va_arg(ap, opus_int32*);
2405 if (!value)
2406 {
2407 goto bad_arg;
2408 }
2409 *value = st->variable_duration;
1572 } 2410 }
1573 break; 2411 break;
1574 case OPUS_RESET_STATE: 2412 case OPUS_RESET_STATE:
1575 { 2413 {
1576 void *silk_enc; 2414 void *silk_enc;
1577 silk_EncControlStruct dummy; 2415 silk_EncControlStruct dummy;
1578 silk_enc = (char*)st+st->silk_enc_offset; 2416 silk_enc = (char*)st+st->silk_enc_offset;
1579 2417
1580 OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START, 2418 OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START,
1581 sizeof(OpusEncoder)- 2419 sizeof(OpusEncoder)-
1582 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st)); 2420 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st));
1583 2421
1584 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 2422 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1585 silk_InitEncoder( silk_enc, &dummy ); 2423 silk_InitEncoder( silk_enc, &dummy );
1586 st->stream_channels = st->channels; 2424 st->stream_channels = st->channels;
1587 st->hybrid_stereo_width_Q14 = 1 << 14; 2425 st->hybrid_stereo_width_Q14 = 1 << 14;
2426 st->prev_HB_gain = Q15ONE;
1588 st->first = 1; 2427 st->first = 1;
1589 st->mode = MODE_HYBRID; 2428 st->mode = MODE_HYBRID;
1590 st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 2429 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
1591 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MI N_CUTOFF_HZ ), 8 ); 2430 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MI N_CUTOFF_HZ ), 8 );
1592 } 2431 }
1593 break; 2432 break;
1594 case OPUS_SET_FORCE_MODE_REQUEST: 2433 case OPUS_SET_FORCE_MODE_REQUEST:
1595 { 2434 {
1596 opus_int32 value = va_arg(ap, opus_int32); 2435 opus_int32 value = va_arg(ap, opus_int32);
1597 if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != O PUS_AUTO) 2436 if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != O PUS_AUTO)
2437 {
1598 goto bad_arg; 2438 goto bad_arg;
2439 }
1599 st->user_forced_mode = value; 2440 st->user_forced_mode = value;
1600 } 2441 }
1601 break; 2442 break;
2443 case OPUS_SET_LFE_REQUEST:
2444 {
2445 opus_int32 value = va_arg(ap, opus_int32);
2446 st->lfe = value;
2447 ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
2448 }
2449 break;
2450 case OPUS_SET_ENERGY_MASK_REQUEST:
2451 {
2452 opus_val16 *value = va_arg(ap, opus_val16*);
2453 st->energy_masking = value;
2454 ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
2455 }
2456 break;
2457
2458 case CELT_GET_MODE_REQUEST:
2459 {
2460 const CELTMode ** value = va_arg(ap, const CELTMode**);
2461 if (!value)
2462 {
2463 goto bad_arg;
2464 }
2465 ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
2466 }
2467 break;
1602 default: 2468 default:
1603 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request );*/ 2469 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request );*/
1604 ret = OPUS_UNIMPLEMENTED; 2470 ret = OPUS_UNIMPLEMENTED;
1605 break; 2471 break;
1606 } 2472 }
1607 va_end(ap); 2473 va_end(ap);
1608 return ret; 2474 return ret;
1609 bad_arg: 2475 bad_arg:
1610 va_end(ap); 2476 va_end(ap);
1611 return OPUS_BAD_ARG; 2477 return OPUS_BAD_ARG;
1612 } 2478 }
1613 2479
1614 void opus_encoder_destroy(OpusEncoder *st) 2480 void opus_encoder_destroy(OpusEncoder *st)
1615 { 2481 {
1616 opus_free(st); 2482 opus_free(st);
1617 } 2483 }
OLDNEW
« no previous file with comments | « src/opus_demo.c ('k') | src/opus_multistream.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698