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

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

Issue 11196031: Add copy of opus library in deps/third_party. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/
Patch Set: Created 8 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 | « opus/src/opus_demo.c ('k') | opus/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')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
3 /*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdarg.h>
33 #include "celt.h"
34 #include "entenc.h"
35 #include "modes.h"
36 #include "API.h"
37 #include "stack_alloc.h"
38 #include "float_cast.h"
39 #include "opus.h"
40 #include "arch.h"
41 #include "opus_private.h"
42 #include "os_support.h"
43
44 #include "tuning_parameters.h"
45 #ifdef FIXED_POINT
46 #include "fixed/structs_FIX.h"
47 #else
48 #include "float/structs_FLP.h"
49 #endif
50
51 #define MAX_ENCODER_BUFFER 480
52
53 struct OpusEncoder {
54 int celt_enc_offset;
55 int silk_enc_offset;
56 silk_EncControlStruct silk_mode;
57 int application;
58 int channels;
59 int delay_compensation;
60 int force_channels;
61 int signal_type;
62 int user_bandwidth;
63 int max_bandwidth;
64 int user_forced_mode;
65 int voice_ratio;
66 opus_int32 Fs;
67 int use_vbr;
68 int vbr_constraint;
69 opus_int32 bitrate_bps;
70 opus_int32 user_bitrate_bps;
71 int encoder_buffer;
72
73 #define OPUS_ENCODER_RESET_START stream_channels
74 int stream_channels;
75 opus_int16 hybrid_stereo_width_Q14;
76 opus_int32 variable_HP_smth2_Q15;
77 opus_val32 hp_mem[4];
78 int mode;
79 int prev_mode;
80 int prev_channels;
81 int prev_framesize;
82 int bandwidth;
83 int silk_bw_switch;
84 /* Sampling rate (at the API level) */
85 int first;
86 opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2];
87
88 opus_uint32 rangeFinal;
89 };
90
91 /* Transition tables for the voice and music. First column is the
92 middle (memoriless) threshold. The second column is the hysteresis
93 (difference with the middle) */
94 static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
95 11000, 1000, /* NB<->MB */
96 14000, 1000, /* MB<->WB */
97 21000, 2000, /* WB<->SWB */
98 29000, 2000, /* SWB<->FB */
99 };
100 static const opus_int32 mono_music_bandwidth_thresholds[8] = {
101 14000, 1000, /* MB not allowed */
102 18000, 2000, /* MB<->WB */
103 24000, 2000, /* WB<->SWB */
104 33000, 2000, /* SWB<->FB */
105 };
106 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
107 11000, 1000, /* NB<->MB */
108 14000, 1000, /* MB<->WB */
109 21000, 2000, /* WB<->SWB */
110 32000, 2000, /* SWB<->FB */
111 };
112 static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
113 14000, 1000, /* MB not allowed */
114 18000, 2000, /* MB<->WB */
115 24000, 2000, /* WB<->SWB */
116 48000, 2000, /* SWB<->FB */
117 };
118 /* Threshold bit-rates for switching between mono and stereo */
119 static const opus_int32 stereo_voice_threshold = 26000;
120 static const opus_int32 stereo_music_threshold = 36000;
121
122 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
123 static const opus_int32 mode_thresholds[2][2] = {
124 /* voice */ /* music */
125 { 48000, 24000}, /* mono */
126 { 48000, 24000}, /* stereo */
127 };
128
129 int opus_encoder_get_size(int channels)
130 {
131 int silkEncSizeBytes, celtEncSizeBytes;
132 int ret;
133 if (channels<1 || channels > 2)
134 return 0;
135 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
136 if (ret)
137 return 0;
138 silkEncSizeBytes = align(silkEncSizeBytes);
139 celtEncSizeBytes = celt_encoder_get_size(channels);
140 return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes;
141 }
142
143 int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat ion)
144 {
145 void *silk_enc;
146 CELTEncoder *celt_enc;
147 int err;
148 int ret, silkEncSizeBytes;
149
150 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&chan nels!=2)||
151 (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION _AUDIO
152 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
153 return OPUS_BAD_ARG;
154
155 OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
156 /* Create SILK encoder */
157 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
158 if (ret)
159 return OPUS_BAD_ARG;
160 silkEncSizeBytes = align(silkEncSizeBytes);
161 st->silk_enc_offset = align(sizeof(OpusEncoder));
162 st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
163 silk_enc = (char*)st+st->silk_enc_offset;
164 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
165
166 st->stream_channels = st->channels = channels;
167
168 st->Fs = Fs;
169
170 ret = silk_InitEncoder( silk_enc, &st->silk_mode );
171 if(ret)return OPUS_INTERNAL_ERROR;
172
173 /* default SILK parameters */
174 st->silk_mode.nChannelsAPI = channels;
175 st->silk_mode.nChannelsInternal = channels;
176 st->silk_mode.API_sampleRate = st->Fs;
177 st->silk_mode.maxInternalSampleRate = 16000;
178 st->silk_mode.minInternalSampleRate = 8000;
179 st->silk_mode.desiredInternalSampleRate = 16000;
180 st->silk_mode.payloadSize_ms = 20;
181 st->silk_mode.bitRate = 25000;
182 st->silk_mode.packetLossPercentage = 0;
183 st->silk_mode.complexity = 10;
184 st->silk_mode.useInBandFEC = 0;
185 st->silk_mode.useDTX = 0;
186 st->silk_mode.useCBR = 0;
187
188 /* Create CELT encoder */
189 /* Initialize CELT encoder */
190 err = celt_encoder_init(celt_enc, Fs, channels);
191 if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
192
193 celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
194 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(10));
195
196 st->use_vbr = 1;
197 /* Makes constrained VBR the default (safer for real-time use) */
198 st->vbr_constraint = 1;
199 st->user_bitrate_bps = OPUS_AUTO;
200 st->bitrate_bps = 3000+Fs*channels;
201 st->application = application;
202 st->signal_type = OPUS_AUTO;
203 st->user_bandwidth = OPUS_AUTO;
204 st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
205 st->force_channels = OPUS_AUTO;
206 st->user_forced_mode = OPUS_AUTO;
207 st->voice_ratio = -1;
208 st->encoder_buffer = st->Fs/100;
209
210 /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
211 + 1.5 ms for SILK resamplers and stereo prediction) */
212 st->delay_compensation = st->Fs/250;
213
214 st->hybrid_stereo_width_Q14 = 1 << 14;
215 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 );
216 st->first = 1;
217 st->mode = MODE_HYBRID;
218 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
219
220 return OPUS_OK;
221 }
222
223 static int pad_frame(unsigned char *data, opus_int32 len, opus_int32 new_len)
224 {
225 if (len == new_len)
226 return 0;
227 if (len > new_len)
228 return 1;
229
230 if ((data[0]&0x3)==0)
231 {
232 int i;
233 int padding, nb_255s;
234
235 padding = new_len - len;
236 if (padding >= 2)
237 {
238 nb_255s = (padding-2)/255;
239
240 for (i=len-1;i>=1;i--)
241 data[i+nb_255s+2] = data[i];
242 data[0] |= 0x3;
243 data[1] = 0x41;
244 for (i=0;i<nb_255s;i++)
245 data[i+2] = 255;
246 data[nb_255s+2] = padding-255*nb_255s-2;
247 for (i=len+3+nb_255s;i<new_len;i++)
248 data[i] = 0;
249 } else {
250 for (i=len-1;i>=1;i--)
251 data[i+1] = data[i];
252 data[0] |= 0x3;
253 data[1] = 1;
254 }
255 return 0;
256 } else {
257 return 1;
258 }
259 }
260
261 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channel s)
262 {
263 int period;
264 unsigned char toc;
265 period = 0;
266 while (framerate < 400)
267 {
268 framerate <<= 1;
269 period++;
270 }
271 if (mode == MODE_SILK_ONLY)
272 {
273 toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
274 toc |= (period-2)<<3;
275 } else if (mode == MODE_CELT_ONLY)
276 {
277 int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND;
278 if (tmp < 0)
279 tmp = 0;
280 toc = 0x80;
281 toc |= tmp << 5;
282 toc |= period<<3;
283 } else /* Hybrid */
284 {
285 toc = 0x60;
286 toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4;
287 toc |= (period-2)<<3;
288 }
289 toc |= (channels==2)<<2;
290 return toc;
291 }
292
293 #ifndef FIXED_POINT
294 void silk_biquad_float(
295 const opus_val16 *in, /* I: Input signal */
296 const opus_int32 *B_Q28, /* I: MA coefficients [3] */
297 const opus_int32 *A_Q28, /* I: AR coefficients [2] */
298 opus_val32 *S, /* I/O: State vector [2] */
299 opus_val16 *out, /* O: Output signal */
300 const opus_int32 len, /* I: Signal length (must be even) */
301 int stride
302 )
303 {
304 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
305 opus_int k;
306 opus_val32 vout;
307 opus_val32 inval;
308 opus_val32 A[2], B[3];
309
310 A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28)));
311 A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28)));
312 B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28)));
313 B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28)));
314 B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28)));
315
316 /* Negate A_Q28 values and split in two parts */
317
318 for( k = 0; k < len; k++ ) {
319 /* S[ 0 ], S[ 1 ]: Q12 */
320 inval = in[ k*stride ];
321 vout = S[ 0 ] + B[0]*inval;
322
323 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
324
325 S[ 1 ] = - vout*A[1] + B[2]*inval;
326
327 /* Scale back to Q0 and saturate */
328 out[ k*stride ] = vout;
329 }
330 }
331 #endif
332
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)
334 {
335 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
336 opus_int32 Fc_Q19, r_Q28, r_Q22;
337
338 silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 10 00, 19 ) );
339 Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
340 silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
341
342 r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q 19 );
343
344 /* b = r * [ 1; -2; 1 ]; */
345 /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
346 B_Q28[ 0 ] = r_Q28;
347 B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 );
348 B_Q28[ 2 ] = r_Q28;
349
350 /* -r * ( 2 - Fc * Fc ); */
351 r_Q22 = silk_RSHIFT( r_Q28, 6 );
352 A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CON ST( 2.0, 22 ) );
353 A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 );
354
355 #ifdef FIXED_POINT
356 silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels );
357 if( channels == 2 ) {
358 silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
359 }
360 #else
361 silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels );
362 if( channels == 2 ) {
363 silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
364 }
365 #endif
366 }
367
368 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)
370 {
371 int i;
372 int overlap;
373 int inc;
374 inc = 48000/Fs;
375 overlap=overlap48/inc;
376 g1 = Q15ONE-g1;
377 g2 = Q15ONE-g2;
378 for (i=0;i<overlap;i++)
379 {
380 opus_val32 diff;
381 opus_val16 g, w;
382 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
383 g = SHR32(MAC16_16(MULT16_16(w,g2),
384 Q15ONE-w, g1), 15);
385 diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*cha nnels+1]));
386 diff = MULT16_16_Q15(g, diff);
387 out[i*channels] = out[i*channels] - diff;
388 out[i*channels+1] = out[i*channels+1] + diff;
389 }
390 for (;i<frame_size;i++)
391 {
392 opus_val32 diff;
393 diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*cha nnels+1]));
394 diff = MULT16_16_Q15(g2, diff);
395 out[i*channels] = out[i*channels] - diff;
396 out[i*channels+1] = out[i*channels+1] + diff;
397 }
398 }
399
400 OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, i nt *error)
401 {
402 int ret;
403 OpusEncoder *st;
404 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
406 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
407 {
408 if (error)
409 *error = OPUS_BAD_ARG;
410 return NULL;
411 }
412 st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels));
413 if (st == NULL)
414 {
415 if (error)
416 *error = OPUS_ALLOC_FAIL;
417 return NULL;
418 }
419 ret = opus_encoder_init(st, Fs, channels, application);
420 if (error)
421 *error = ret;
422 if (ret != OPUS_OK)
423 {
424 opus_free(st);
425 st = NULL;
426 }
427 return st;
428 }
429
430 static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int m ax_data_bytes)
431 {
432 if(!frame_size)frame_size=st->Fs/400;
433 if (st->user_bitrate_bps==OPUS_AUTO)
434 return 60*st->Fs/frame_size + st->Fs*st->channels;
435 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
436 return max_data_bytes*8*st->Fs/frame_size;
437 else
438 return st->user_bitrate_bps;
439 }
440
441 #ifdef FIXED_POINT
442 #define opus_encode_native opus_encode
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
446 #define opus_encode_native opus_encode_float
447 opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s ize,
448 unsigned char *data, opus_int32 out_data_bytes)
449 #endif
450 {
451 void *silk_enc;
452 CELTEncoder *celt_enc;
453 int i;
454 int ret=0;
455 opus_int32 nBytes;
456 ec_enc enc;
457 int bytes_target;
458 int prefill=0;
459 int start_band = 0;
460 int redundancy = 0;
461 int redundancy_bytes = 0;
462 int celt_to_silk = 0;
463 VARDECL(opus_val16, pcm_buf);
464 int nb_compr_bytes;
465 int to_celt = 0;
466 opus_uint32 redundant_rng = 0;
467 int cutoff_Hz, hp_freq_smth1;
468 int voice_est;
469 opus_int32 equiv_rate;
470 int delay_compensation;
471 int frame_rate;
472 opus_int32 max_rate;
473 int curr_bandwidth;
474 opus_int32 max_data_bytes;
475 VARDECL(opus_val16, tmp_prefill);
476
477 ALLOC_STACK;
478
479 max_data_bytes = IMIN(1276, out_data_bytes);
480
481 st->rangeFinal = 0;
482 if (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)
484 {
485 RESTORE_STACK;
486 return OPUS_BAD_ARG;
487 }
488 if (max_data_bytes<=0)
489 {
490 RESTORE_STACK;
491 return OPUS_BAD_ARG;
492 }
493 silk_enc = (char*)st+st->silk_enc_offset;
494 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
495
496 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
497 delay_compensation = 0;
498 else
499 delay_compensation = st->delay_compensation;
500
501 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
502
503 frame_rate = st->Fs/frame_size;
504 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)))
506 {
507 /*If the space is too low to do something useful, emit 'PLC' frames.*/
508 int tocmode = st->mode;
509 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
510 if (tocmode==0)
511 tocmode = MODE_SILK_ONLY;
512 if (frame_rate>100)
513 tocmode = MODE_CELT_ONLY;
514 if (frame_rate < 50)
515 tocmode = MODE_SILK_ONLY;
516 if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
517 bw=OPUS_BANDWIDTH_WIDEBAND;
518 else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
519 bw=OPUS_BANDWIDTH_NARROWBAND;
520 else if (bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
521 bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
522 data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
523 RESTORE_STACK;
524 return 1;
525 }
526 if (!st->use_vbr)
527 {
528 int cbrBytes;
529 cbrBytes = IMIN( (st->bitrate_bps + 4*frame_rate)/(8*frame_rate) , max_da ta_bytes);
530 st->bitrate_bps = cbrBytes * (8*frame_rate);
531 max_data_bytes = cbrBytes;
532 }
533 max_rate = frame_rate*max_data_bytes*8;
534
535 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
536 equiv_rate = st->bitrate_bps - 60*(st->Fs/frame_size - 50);
537
538 if (st->signal_type == OPUS_SIGNAL_VOICE)
539 voice_est = 127;
540 else if (st->signal_type == OPUS_SIGNAL_MUSIC)
541 voice_est = 0;
542 else if (st->voice_ratio >= 0)
543 voice_est = st->voice_ratio*327>>8;
544 else if (st->application == OPUS_APPLICATION_VOIP)
545 voice_est = 115;
546 else
547 voice_est = 48;
548
549 if (st->force_channels!=OPUS_AUTO && st->channels == 2)
550 {
551 st->stream_channels = st->force_channels;
552 } else {
553 #ifdef FUZZING
554 /* Random mono/stereo decision */
555 if (st->channels == 2 && (rand()&0x1F)==0)
556 st->stream_channels = 3-st->stream_channels;
557 #else
558 /* Rate-dependent mono-stereo decision */
559 if (st->channels == 2)
560 {
561 opus_int32 stereo_threshold;
562 stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(ste reo_voice_threshold-stereo_music_threshold))>>14);
563 if (st->stream_channels == 2)
564 stereo_threshold -= 4000;
565 else
566 stereo_threshold += 4000;
567 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
568 } else {
569 st->stream_channels = st->channels;
570 }
571 #endif
572 }
573
574 /* Mode selection depending on application and signal type */
575 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
576 {
577 st->mode = MODE_CELT_ONLY;
578 } else if (st->user_forced_mode == OPUS_AUTO)
579 {
580 #ifdef FUZZING
581 /* Random mode switching */
582 if ((rand()&0xF)==0)
583 {
584 if ((rand()&0x1)==0)
585 st->mode = MODE_CELT_ONLY;
586 else
587 st->mode = MODE_SILK_ONLY;
588 } else {
589 if (st->prev_mode==MODE_CELT_ONLY)
590 st->mode = MODE_CELT_ONLY;
591 else
592 st->mode = MODE_SILK_ONLY;
593 }
594 #else
595 int chan;
596 opus_int32 mode_voice, mode_music;
597 opus_int32 threshold;
598
599 chan = (st->channels==2) && st->force_channels!=1;
600 mode_voice = mode_thresholds[chan][0];
601 mode_music = mode_thresholds[chan][1];
602 threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>> 14);
603
604 /* Hysteresis */
605 if (st->prev_mode == MODE_CELT_ONLY)
606 threshold -= 4000;
607 else if (st->prev_mode>0)
608 threshold += 4000;
609
610 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
611
612 /* 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)
614 st->mode = MODE_SILK_ONLY;
615 /* When encoding voice and DTX is enabled, set the encoder to SILK mode ( at least for now) */
616 if (st->silk_mode.useDTX && voice_est > 100)
617 st->mode = MODE_SILK_ONLY;
618 #endif
619 } else {
620 st->mode = st->user_forced_mode;
621 }
622
623 /* Override the chosen mode to make sure we meet the requested frame size */
624 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
625 st->mode = MODE_CELT_ONLY;
626
627 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMon o==0
628 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
629 {
630 /* Delay stereo->mono transition by two frames so that SILK can do a smoo th downmix */
631 st->silk_mode.toMono = 1;
632 st->stream_channels = 2;
633 } else {
634 st->silk_mode.toMono = 0;
635 }
636
637 if (st->prev_mode > 0 &&
638 ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
639 (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
640 {
641 redundancy = 1;
642 celt_to_silk = (st->mode != MODE_CELT_ONLY);
643 if (!celt_to_silk)
644 {
645 /* Switch to SILK/hybrid if frame size is 10 ms or more*/
646 if (frame_size >= st->Fs/100)
647 {
648 st->mode = st->prev_mode;
649 to_celt = 1;
650 } else {
651 redundancy=0;
652 }
653 }
654 }
655 if (st->silk_bw_switch)
656 {
657 redundancy = 1;
658 celt_to_silk = 1;
659 st->silk_bw_switch = 0;
660 }
661
662 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
663 {
664 silk_EncControlStruct dummy;
665 silk_InitEncoder( silk_enc, &dummy);
666 prefill=1;
667 }
668
669 /* Automatic (rate-dependent) bandwidth selection */
670 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthS witch)
671 {
672 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_threshold s;
673 opus_int32 bandwidth_thresholds[8];
674 int bandwidth = OPUS_BANDWIDTH_FULLBAND;
675 opus_int32 equiv_rate2;
676
677 equiv_rate2 = equiv_rate;
678 if (st->mode != MODE_CELT_ONLY)
679 {
680 /* Adjust the threshold +/- 10% depending on complexity */
681 equiv_rate2 = equiv_rate2 * (45+st->silk_mode.complexity)/50;
682 /* CBR is less efficient by ~1 kb/s */
683 if (!st->use_vbr)
684 equiv_rate2 -= 1000;
685 }
686 if (st->channels==2 && st->force_channels!=1)
687 {
688 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
689 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
690 } else {
691 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
692 music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
693 }
694 /* Interpolate bandwidth thresholds depending on voice estimation */
695 for (i=0;i<8;i++)
696 {
697 bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
698 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music _bandwidth_thresholds[i]))>>14);
699 }
700 do {
701 int threshold, hysteresis;
702 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMB AND)];
703 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUM BAND)+1];
704 if (!st->first)
705 {
706 if (st->bandwidth >= bandwidth)
707 threshold -= hysteresis;
708 else
709 threshold += hysteresis;
710 }
711 if (equiv_rate2 >= threshold)
712 break;
713 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
714 st->bandwidth = bandwidth;
715 /* Prevents any transition to SWB/FB until the SILK layer has fully
716 switched to WB mode and turned the variable LP filter off */
717 if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeW ithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
718 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
719 }
720
721 if (st->bandwidth>st->max_bandwidth)
722 st->bandwidth = st->max_bandwidth;
723
724 if (st->user_bandwidth != OPUS_AUTO)
725 st->bandwidth = st->user_bandwidth;
726
727 /* This prevents us from using hybrid at unsafe CBR/max rates */
728 if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
729 {
730 st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
731 }
732
733 /* Prevents Opus from wasting bits on frequencies that are above
734 the Nyquist rate of the input signal */
735 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
736 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
737 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
738 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
739 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
740 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
741 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
742 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
743
744 /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode * /
745 if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
746 st->mode = MODE_CELT_ONLY;
747
748 /* CELT mode doesn't support mediumband, use wideband instead */
749 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND )
750 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
751
752 /* Can't support higher than wideband for >20 ms frames */
753 if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND))
754 {
755 VARDECL(unsigned char, tmp_data);
756 int nb_frames;
757 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
758 OpusRepacketizer rp;
759 opus_int32 bytes_per_frame;
760
761
762 nb_frames = frame_size > st->Fs/25 ? 3 : 2;
763 bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames);
764
765 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
766
767 opus_repacketizer_init(&rp);
768
769 bak_mode = st->user_forced_mode;
770 bak_bandwidth = st->user_bandwidth;
771 bak_channels = st->force_channels;
772
773 st->user_forced_mode = st->mode;
774 st->user_bandwidth = st->bandwidth;
775 st->force_channels = st->stream_channels;
776 bak_to_mono = st->silk_mode.toMono;
777
778 if (bak_to_mono)
779 st->force_channels = 1;
780 else
781 st->prev_channels = st->stream_channels;
782 for (i=0;i<nb_frames;i++)
783 {
784 int tmp_len;
785 st->silk_mode.toMono = 0;
786 /* When switching from SILK/Hybrid to CELT, only ask for a switch at t he last frame */
787 if (to_celt && i==nb_frames-1)
788 st->user_forced_mode = MODE_CELT_ONLY;
789 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);
790 if (tmp_len<0)
791 {
792 RESTORE_STACK;
793 return OPUS_INTERNAL_ERROR;
794 }
795 ret = opus_repacketizer_cat(&rp, tmp_data+i*bytes_per_frame, tmp_len);
796 if (ret<0)
797 {
798 RESTORE_STACK;
799 return OPUS_INTERNAL_ERROR;
800 }
801 }
802 ret = opus_repacketizer_out(&rp, data, out_data_bytes);
803 if (ret<0)
804 {
805 RESTORE_STACK;
806 return OPUS_INTERNAL_ERROR;
807 }
808 st->user_forced_mode = bak_mode;
809 st->user_bandwidth = bak_bandwidth;
810 st->force_channels = bak_channels;
811 st->silk_mode.toMono = bak_to_mono;
812 RESTORE_STACK;
813 return ret;
814 }
815
816 curr_bandwidth = st->bandwidth;
817
818 /* Chooses the appropriate mode for speech
819 *NEVER* switch to/from CELT-only mode here as this will invalidate some a ssumptions */
820 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
821 st->mode = MODE_HYBRID;
822 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
823 st->mode = MODE_SILK_ONLY;
824
825 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c urr_bandwidth); */
826 bytes_target = IMIN(max_data_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
827
828 data += 1;
829
830 ec_enc_init(&enc, data, max_data_bytes-1);
831
832 ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16);
833 for (i=0;i<delay_compensation*st->channels;i++)
834 pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st- >channels+i];
835
836 if (st->mode == MODE_CELT_ONLY)
837 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
838 else
839 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_ smth1_Q15;
840
841 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
842 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP _SMTH_COEF2, 16 ) );
843
844 /* convert from log scale to Hertz */
845 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
846
847 if (st->application == OPUS_APPLICATION_VOIP)
848 {
849 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[delay_compensation*st->channels], st-> hp_mem, frame_size, st->channels, st->Fs);
850 } else {
851 for (i=0;i<frame_size*st->channels;i++)
852 pcm_buf[delay_compensation*st->channels + i] = pcm[i];
853 }
854
855 /* SILK processing */
856 if (st->mode != MODE_CELT_ONLY)
857 {
858 #ifdef FIXED_POINT
859 const opus_int16 *pcm_silk;
860 #else
861 VARDECL(opus_int16, pcm_silk);
862 ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
863 #endif
864 st->silk_mode.bitRate = 8*bytes_target*frame_rate;
865 if( st->mode == MODE_HYBRID ) {
866 st->silk_mode.bitRate /= st->stream_channels;
867 if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) {
868 if( st->Fs == 100 * frame_size ) {
869 /* 24 kHz, 10 ms */
870 st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + s t->use_vbr * 1000 ) * 2 ) / 3;
871 } else {
872 /* 24 kHz, 20 ms */
873 st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 1000 + s t->use_vbr * 1000 ) * 2 ) / 3;
874 }
875 } else {
876 if( st->Fs == 100 * frame_size ) {
877 /* 48 kHz, 10 ms */
878 st->silk_mode.bitRate = ( st->silk_mode.bitRate + 8000 + st- >use_vbr * 3000 ) / 2;
879 } else {
880 /* 48 kHz, 20 ms */
881 st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st- >use_vbr * 1000 ) / 2;
882 }
883 }
884 st->silk_mode.bitRate *= st->stream_channels;
885 /* don't let SILK use more than 80% */
886 if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) {
887 st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
888 }
889 }
890
891 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
892 st->silk_mode.nChannelsAPI = st->channels;
893 st->silk_mode.nChannelsInternal = st->stream_channels;
894 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
895 st->silk_mode.desiredInternalSampleRate = 8000;
896 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
897 st->silk_mode.desiredInternalSampleRate = 12000;
898 } else {
899 silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDW IDTH_WIDEBAND );
900 st->silk_mode.desiredInternalSampleRate = 16000;
901 }
902 if( st->mode == MODE_HYBRID ) {
903 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
904 st->silk_mode.minInternalSampleRate = 16000;
905 } else {
906 st->silk_mode.minInternalSampleRate = 8000;
907 }
908
909 if (st->mode == MODE_SILK_ONLY)
910 {
911 opus_int32 effective_max_rate = max_rate;
912 st->silk_mode.maxInternalSampleRate = 16000;
913 if (frame_rate > 50)
914 effective_max_rate = effective_max_rate*2/3;
915 if (effective_max_rate < 13000)
916 {
917 st->silk_mode.maxInternalSampleRate = 12000;
918 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mod e.desiredInternalSampleRate);
919 }
920 if (effective_max_rate < 9600)
921 {
922 st->silk_mode.maxInternalSampleRate = 8000;
923 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode .desiredInternalSampleRate);
924 }
925 } else {
926 st->silk_mode.maxInternalSampleRate = 16000;
927 }
928
929 st->silk_mode.useCBR = !st->use_vbr;
930
931 /* Call SILK encoder for the low band */
932 nBytes = IMIN(1275, max_data_bytes-1);
933
934 st->silk_mode.maxBits = nBytes*8;
935 /* Only allow up to 90% of the bits for hybrid mode*/
936 if (st->mode == MODE_HYBRID)
937 st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10;
938 if (st->silk_mode.useCBR)
939 {
940 st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8;
941 /* Reduce the initial target to make it easier to reach the CBR rate */
942 st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000);
943 }
944 if (redundancy)
945 st->silk_mode.maxBits -= st->silk_mode.maxBits/(1 + frame_size/(st->F s/200));
946
947 if (prefill)
948 {
949 opus_int32 zero=0;
950 #ifdef FIXED_POINT
951 pcm_silk = st->delay_buffer;
952 #else
953 for (i=0;i<st->encoder_buffer*st->channels;i++)
954 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
955 #endif
956 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 );
957 }
958
959 #ifdef FIXED_POINT
960 pcm_silk = pcm_buf+delay_compensation*st->channels;
961 #else
962 for (i=0;i<frame_size*st->channels;i++)
963 pcm_silk[i] = FLOAT2INT16(pcm_buf[delay_compensation*st->channels + i]);
964 #endif
965 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
966 if( ret ) {
967 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
968 /* Handle error */
969 RESTORE_STACK;
970 return OPUS_INTERNAL_ERROR;
971 }
972 if (nBytes==0)
973 {
974 st->rangeFinal = 0;
975 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->s tream_channels);
976 RESTORE_STACK;
977 return 1;
978 }
979 /* Extract SILK internal bandwidth for signaling in first byte */
980 if( st->mode == MODE_SILK_ONLY ) {
981 if( st->silk_mode.internalSampleRate == 8000 ) {
982 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
983 } else if( st->silk_mode.internalSampleRate == 12000 ) {
984 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
985 } else if( st->silk_mode.internalSampleRate == 16000 ) {
986 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
987 }
988 } else {
989 silk_assert( st->silk_mode.internalSampleRate == 16000 );
990 }
991
992 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady;
993 if (st->silk_mode.opusCanSwitch)
994 {
995 redundancy = 1;
996 celt_to_silk = 0;
997 st->silk_bw_switch = 1;
998 }
999 }
1000
1001 /* CELT processing */
1002 {
1003 int endband=21;
1004
1005 switch(curr_bandwidth)
1006 {
1007 case OPUS_BANDWIDTH_NARROWBAND:
1008 endband = 13;
1009 break;
1010 case OPUS_BANDWIDTH_MEDIUMBAND:
1011 case OPUS_BANDWIDTH_WIDEBAND:
1012 endband = 17;
1013 break;
1014 case OPUS_BANDWIDTH_SUPERWIDEBAND:
1015 endband = 19;
1016 break;
1017 case OPUS_BANDWIDTH_FULLBAND:
1018 endband = 21;
1019 break;
1020 }
1021 celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
1022 celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
1023 }
1024 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
1025 if (st->mode != MODE_SILK_ONLY)
1026 {
1027 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1028 /* Allow prediction unless we decide to disable it later */
1029 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(2));
1030
1031 if (st->mode == MODE_HYBRID)
1032 {
1033 int len;
1034
1035 len = (ec_tell(&enc)+7)>>3;
1036 if (redundancy)
1037 len += st->mode == MODE_HYBRID ? 3 : 1;
1038 if( st->use_vbr ) {
1039 nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * f rame_size) / (8 * st->Fs);
1040 } else {
1041 /* check if SILK used up too much */
1042 nb_compr_bytes = len > bytes_target ? len : bytes_target;
1043 }
1044 } else {
1045 if (st->use_vbr)
1046 {
1047 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
1048 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_const raint));
1049 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
1050 nb_compr_bytes = max_data_bytes-1;
1051 } else {
1052 nb_compr_bytes = bytes_target;
1053 }
1054 }
1055
1056 } else {
1057 nb_compr_bytes = 0;
1058 }
1059
1060 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
1061 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
1062 {
1063 for (i=0;i<st->channels*st->Fs/400;i++)
1064 tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-st->delay_compen sation-st->Fs/400)*st->channels + i];
1065 }
1066
1067 for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+delay_compensation)) ;i++)
1068 st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size];
1069 for (;i<st->encoder_buffer*st->channels;i++)
1070 st->delay_buffer[i] = pcm_buf[(frame_size+delay_compensation-st->encoder _buffer)*st->channels+i];
1071
1072
1073 if (st->mode != MODE_HYBRID || st->stream_channels==1)
1074 st->silk_mode.stereoWidth_Q14 = 1<<14;
1075 if( st->channels == 2 ) {
1076 /* Apply stereo width reduction (at low bitrates) */
1077 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth _Q14 < (1 << 14) ) {
1078 opus_val16 g1, g2;
1079 const CELTMode *celt_mode;
1080
1081 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1082 g1 = st->hybrid_stereo_width_Q14;
1083 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
1084 #ifdef FIXED_POINT
1085 g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
1086 g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
1087 #else
1088 g1 *= (1.f/16384);
1089 g2 *= (1.f/16384);
1090 #endif
1091 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
1092 frame_size, st->channels, celt_mode->window, st->Fs);
1093 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
1094 }
1095 }
1096
1097 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYB RID) <= 8*(max_data_bytes-1))
1098 {
1099 /* For SILK mode, the redundancy is inferred from the length */
1100 if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_c ompr_bytes))
1101 ec_enc_bit_logp(&enc, redundancy, 12);
1102 if (redundancy)
1103 {
1104 int max_redundancy;
1105 ec_enc_bit_logp(&enc, celt_to_silk, 1);
1106 if (st->mode == MODE_HYBRID)
1107 max_redundancy = (max_data_bytes-1)-nb_compr_bytes-1;
1108 else
1109 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
1110 /* Target the same bit-rate for redundancy as for the rest,
1111 up to a max of 257 bytes */
1112 redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600);
1113 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
1114 if (st->mode == MODE_HYBRID)
1115 ec_enc_uint(&enc, redundancy_bytes-2, 256);
1116 }
1117 } else {
1118 redundancy = 0;
1119 }
1120
1121 if (!redundancy)
1122 st->silk_bw_switch = 0;
1123
1124 if (st->mode != MODE_CELT_ONLY)start_band=17;
1125
1126 if (st->mode == MODE_SILK_ONLY)
1127 {
1128 ret = (ec_tell(&enc)+7)>>3;
1129 ec_enc_done(&enc);
1130 nb_compr_bytes = ret;
1131 } else {
1132 nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes );
1133 ec_enc_shrink(&enc, nb_compr_bytes);
1134 }
1135
1136
1137 /* 5 ms redundant frame for CELT->SILK */
1138 if (redundancy && celt_to_silk)
1139 {
1140 int err;
1141 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
1142 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1143 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_b ytes, redundancy_bytes, NULL);
1144 if (err < 0)
1145 {
1146 RESTORE_STACK;
1147 return OPUS_INTERNAL_ERROR;
1148 }
1149 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
1150 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1151 }
1152
1153 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
1154
1155 if (st->mode != MODE_SILK_ONLY)
1156 {
1157 if (st->mode != st->prev_mode && st->prev_mode > 0)
1158 {
1159 unsigned char dummy[2];
1160 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1161
1162 /* Prefilling */
1163 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL );
1164 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
1165 }
1166 /* If false, we already busted the budget and we'll end up with a "PLC p acket" */
1167 if (ec_tell(&enc) <= 8*nb_compr_bytes)
1168 {
1169 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_com pr_bytes, &enc);
1170 if (ret < 0)
1171 {
1172 RESTORE_STACK;
1173 return OPUS_INTERNAL_ERROR;
1174 }
1175 }
1176 }
1177
1178 /* 5 ms redundant frame for SILK->CELT */
1179 if (redundancy && !celt_to_silk)
1180 {
1181 int err;
1182 unsigned char dummy[2];
1183 int N2, N4;
1184 N2 = st->Fs/200;
1185 N4 = st->Fs/400;
1186
1187 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1188 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
1189 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
1190
1191 /* NOTE: We could speed this up slightly (at the expense of code size) b y just adding a function that prefills the buffer */
1192 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N 4, dummy, 2, NULL);
1193
1194 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2) , N2, data+nb_compr_bytes, redundancy_bytes, NULL);
1195 if (err < 0)
1196 {
1197 RESTORE_STACK;
1198 return OPUS_INTERNAL_ERROR;
1199 }
1200 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
1201 }
1202
1203
1204
1205 /* Signalling the mode in the first byte */
1206 data--;
1207 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_ch annels);
1208
1209 st->rangeFinal = enc.rng ^ redundant_rng;
1210
1211 if (to_celt)
1212 st->prev_mode = MODE_CELT_ONLY;
1213 else
1214 st->prev_mode = st->mode;
1215 st->prev_channels = st->stream_channels;
1216 st->prev_framesize = frame_size;
1217
1218 st->first = 0;
1219
1220 /* In the unlikely case that the SILK encoder busted its target, tell
1221 the decoder to call the PLC */
1222 if (ec_tell(&enc) > (max_data_bytes-1)*8)
1223 {
1224 if (max_data_bytes < 2)
1225 {
1226 RESTORE_STACK;
1227 return OPUS_BUFFER_TOO_SMALL;
1228 }
1229 data[1] = 0;
1230 ret = 1;
1231 st->rangeFinal = 0;
1232 } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
1233 {
1234 /*When in LPC only mode it's perfectly
1235 reasonable to strip off trailing zero bytes as
1236 the required range decoder behavior is to
1237 fill these in. This can't be done when the MDCT
1238 modes are used because the decoder needs to know
1239 the actual length for allocation purposes.*/
1240 while(ret>2&&data[ret]==0)ret--;
1241 }
1242 /* Count ToC and redundancy */
1243 ret += 1+redundancy_bytes;
1244 if (!st->use_vbr && ret >= 3)
1245 {
1246 if (pad_frame(data, ret, max_data_bytes))
1247 {
1248 RESTORE_STACK;
1249 return OPUS_INTERNAL_ERROR;
1250 }
1251 ret = max_data_bytes;
1252 }
1253 RESTORE_STACK;
1254 return ret;
1255 }
1256
1257 #ifdef FIXED_POINT
1258
1259 #ifndef DISABLE_FLOAT_API
1260 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int frame_size,
1261 unsigned char *data, opus_int32 max_data_bytes)
1262 {
1263 int i, ret;
1264 VARDECL(opus_int16, in);
1265 ALLOC_STACK;
1266
1267 if(frame_size<0)
1268 {
1269 RESTORE_STACK;
1270 return OPUS_BAD_ARG;
1271 }
1272
1273 ALLOC(in, frame_size*st->channels, opus_int16);
1274
1275 for (i=0;i<frame_size*st->channels;i++)
1276 in[i] = FLOAT2INT16(pcm[i]);
1277 ret = opus_encode(st, in, frame_size, data, max_data_bytes);
1278 RESTORE_STACK;
1279 return ret;
1280 }
1281 #endif
1282
1283 #else
1284 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size,
1285 unsigned char *data, opus_int32 max_data_bytes)
1286 {
1287 int i, ret;
1288 VARDECL(float, in);
1289 ALLOC_STACK;
1290
1291 ALLOC(in, frame_size*st->channels, float);
1292
1293 for (i=0;i<frame_size*st->channels;i++)
1294 in[i] = (1.0f/32768)*pcm[i];
1295 ret = opus_encode_float(st, in, frame_size, data, max_data_bytes);
1296 RESTORE_STACK;
1297 return ret;
1298 }
1299 #endif
1300
1301
1302 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
1303 {
1304 int ret;
1305 CELTEncoder *celt_enc;
1306 va_list ap;
1307
1308 ret = OPUS_OK;
1309 va_start(ap, request);
1310
1311 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1312
1313 switch (request)
1314 {
1315 case OPUS_SET_APPLICATION_REQUEST:
1316 {
1317 opus_int32 value = va_arg(ap, opus_int32);
1318 if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_ AUDIO
1319 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1320 || (!st->first && st->application != value))
1321 {
1322 ret = OPUS_BAD_ARG;
1323 break;
1324 }
1325 st->application = value;
1326 }
1327 break;
1328 case OPUS_GET_APPLICATION_REQUEST:
1329 {
1330 opus_int32 *value = va_arg(ap, opus_int32*);
1331 *value = st->application;
1332 }
1333 break;
1334 case OPUS_SET_BITRATE_REQUEST:
1335 {
1336 opus_int32 value = va_arg(ap, opus_int32);
1337 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
1338 {
1339 if (value <= 0)
1340 goto bad_arg;
1341 else if (value <= 500)
1342 value = 500;
1343 else if (value > (opus_int32)300000*st->channels)
1344 value = (opus_int32)300000*st->channels;
1345 }
1346 st->user_bitrate_bps = value;
1347 }
1348 break;
1349 case OPUS_GET_BITRATE_REQUEST:
1350 {
1351 opus_int32 *value = va_arg(ap, opus_int32*);
1352 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
1353 }
1354 break;
1355 case OPUS_SET_FORCE_CHANNELS_REQUEST:
1356 {
1357 opus_int32 value = va_arg(ap, opus_int32);
1358 if((value<1 || value>st->channels) && value != OPUS_AUTO)
1359 return OPUS_BAD_ARG;
1360 st->force_channels = value;
1361 }
1362 break;
1363 case OPUS_GET_FORCE_CHANNELS_REQUEST:
1364 {
1365 opus_int32 *value = va_arg(ap, opus_int32*);
1366 *value = st->force_channels;
1367 }
1368 break;
1369 case OPUS_SET_MAX_BANDWIDTH_REQUEST:
1370 {
1371 opus_int32 value = va_arg(ap, opus_int32);
1372 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULL BAND)
1373 return OPUS_BAD_ARG;
1374 st->max_bandwidth = value;
1375 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1376 st->silk_mode.maxInternalSampleRate = 8000;
1377 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1378 st->silk_mode.maxInternalSampleRate = 12000;
1379 } else {
1380 st->silk_mode.maxInternalSampleRate = 16000;
1381 }
1382 }
1383 break;
1384 case OPUS_GET_MAX_BANDWIDTH_REQUEST:
1385 {
1386 opus_int32 *value = va_arg(ap, opus_int32*);
1387 *value = st->max_bandwidth;
1388 }
1389 break;
1390 case OPUS_SET_BANDWIDTH_REQUEST:
1391 {
1392 opus_int32 value = va_arg(ap, opus_int32);
1393 if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FUL LBAND) && value != OPUS_AUTO)
1394 return OPUS_BAD_ARG;
1395 st->user_bandwidth = value;
1396 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1397 st->silk_mode.maxInternalSampleRate = 8000;
1398 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1399 st->silk_mode.maxInternalSampleRate = 12000;
1400 } else {
1401 st->silk_mode.maxInternalSampleRate = 16000;
1402 }
1403 }
1404 break;
1405 case OPUS_GET_BANDWIDTH_REQUEST:
1406 {
1407 opus_int32 *value = va_arg(ap, opus_int32*);
1408 *value = st->bandwidth;
1409 }
1410 break;
1411 case OPUS_SET_DTX_REQUEST:
1412 {
1413 opus_int32 value = va_arg(ap, opus_int32);
1414 if(value<0 || value>1)
1415 return OPUS_BAD_ARG;
1416 st->silk_mode.useDTX = value;
1417 }
1418 break;
1419 case OPUS_GET_DTX_REQUEST:
1420 {
1421 opus_int32 *value = va_arg(ap, opus_int32*);
1422 *value = st->silk_mode.useDTX;
1423 }
1424 break;
1425 case OPUS_SET_COMPLEXITY_REQUEST:
1426 {
1427 opus_int32 value = va_arg(ap, opus_int32);
1428 if(value<0 || value>10)
1429 return OPUS_BAD_ARG;
1430 st->silk_mode.complexity = value;
1431 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
1432 }
1433 break;
1434 case OPUS_GET_COMPLEXITY_REQUEST:
1435 {
1436 opus_int32 *value = va_arg(ap, opus_int32*);
1437 *value = st->silk_mode.complexity;
1438 }
1439 break;
1440 case OPUS_SET_INBAND_FEC_REQUEST:
1441 {
1442 opus_int32 value = va_arg(ap, opus_int32);
1443 if(value<0 || value>1)
1444 return OPUS_BAD_ARG;
1445 st->silk_mode.useInBandFEC = value;
1446 }
1447 break;
1448 case OPUS_GET_INBAND_FEC_REQUEST:
1449 {
1450 opus_int32 *value = va_arg(ap, opus_int32*);
1451 *value = st->silk_mode.useInBandFEC;
1452 }
1453 break;
1454 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1455 {
1456 opus_int32 value = va_arg(ap, opus_int32);
1457 if (value < 0 || value > 100)
1458 return OPUS_BAD_ARG;
1459 st->silk_mode.packetLossPercentage = value;
1460 celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
1461 }
1462 break;
1463 case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
1464 {
1465 opus_int32 *value = va_arg(ap, opus_int32*);
1466 *value = st->silk_mode.packetLossPercentage;
1467 }
1468 break;
1469 case OPUS_SET_VBR_REQUEST:
1470 {
1471 opus_int32 value = va_arg(ap, opus_int32);
1472 if(value<0 || value>1)
1473 return OPUS_BAD_ARG;
1474 st->use_vbr = value;
1475 st->silk_mode.useCBR = 1-value;
1476 }
1477 break;
1478 case OPUS_GET_VBR_REQUEST:
1479 {
1480 opus_int32 *value = va_arg(ap, opus_int32*);
1481 *value = st->use_vbr;
1482 }
1483 break;
1484 case OPUS_SET_VOICE_RATIO_REQUEST:
1485 {
1486 opus_int32 value = va_arg(ap, opus_int32);
1487 if (value>100 || value<-1)
1488 goto bad_arg;
1489 st->voice_ratio = value;
1490 }
1491 break;
1492 case OPUS_GET_VOICE_RATIO_REQUEST:
1493 {
1494 opus_int32 *value = va_arg(ap, opus_int32*);
1495 *value = st->voice_ratio;
1496 }
1497 break;
1498 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1499 {
1500 opus_int32 value = va_arg(ap, opus_int32);
1501 if(value<0 || value>1)
1502 return OPUS_BAD_ARG;
1503 st->vbr_constraint = value;
1504 }
1505 break;
1506 case OPUS_GET_VBR_CONSTRAINT_REQUEST:
1507 {
1508 opus_int32 *value = va_arg(ap, opus_int32*);
1509 *value = st->vbr_constraint;
1510 }
1511 break;
1512 case OPUS_SET_SIGNAL_REQUEST:
1513 {
1514 opus_int32 value = va_arg(ap, opus_int32);
1515 if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNA L_MUSIC)
1516 return OPUS_BAD_ARG;
1517 st->signal_type = value;
1518 }
1519 break;
1520 case OPUS_GET_SIGNAL_REQUEST:
1521 {
1522 opus_int32 *value = va_arg(ap, opus_int32*);
1523 *value = st->signal_type;
1524 }
1525 break;
1526 case OPUS_GET_LOOKAHEAD_REQUEST:
1527 {
1528 opus_int32 *value = va_arg(ap, opus_int32*);
1529 *value = st->Fs/400;
1530 if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1531 *value += st->delay_compensation;
1532 }
1533 break;
1534 case OPUS_GET_FINAL_RANGE_REQUEST:
1535 {
1536 opus_uint32 *value = va_arg(ap, opus_uint32*);
1537 *value = st->rangeFinal;
1538 }
1539 break;
1540 case OPUS_SET_LSB_DEPTH_REQUEST:
1541 {
1542 opus_int32 value = va_arg(ap, opus_int32);
1543 ret = celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(value));
1544 }
1545 break;
1546 case OPUS_GET_LSB_DEPTH_REQUEST:
1547 {
1548 opus_int32 *value = va_arg(ap, opus_int32*);
1549 celt_encoder_ctl(celt_enc, OPUS_GET_LSB_DEPTH(value));
1550 }
1551 break;
1552 case OPUS_RESET_STATE:
1553 {
1554 void *silk_enc;
1555 silk_EncControlStruct dummy;
1556 silk_enc = (char*)st+st->silk_enc_offset;
1557
1558 OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START,
1559 sizeof(OpusEncoder)-
1560 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st));
1561
1562 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1563 silk_InitEncoder( silk_enc, &dummy );
1564 st->stream_channels = st->channels;
1565 st->hybrid_stereo_width_Q14 = 1 << 14;
1566 st->first = 1;
1567 st->mode = MODE_HYBRID;
1568 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
1569 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MI N_CUTOFF_HZ ), 8 );
1570 }
1571 break;
1572 case OPUS_SET_FORCE_MODE_REQUEST:
1573 {
1574 opus_int32 value = va_arg(ap, opus_int32);
1575 if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != O PUS_AUTO)
1576 goto bad_arg;
1577 st->user_forced_mode = value;
1578 }
1579 break;
1580 default:
1581 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request );*/
1582 ret = OPUS_UNIMPLEMENTED;
1583 break;
1584 }
1585 va_end(ap);
1586 return ret;
1587 bad_arg:
1588 va_end(ap);
1589 return OPUS_BAD_ARG;
1590 }
1591
1592 void opus_encoder_destroy(OpusEncoder *st)
1593 {
1594 opus_free(st);
1595 }
OLDNEW
« no previous file with comments | « opus/src/opus_demo.c ('k') | opus/src/opus_multistream.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698