blob: eadcf4bae922f9f4485696105dd3a3acb3205b5f [file] [log] [blame]
Alexandre Lision744f7422013-09-25 11:39:37 -04001/* 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
53struct 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) */
94static 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};
100static 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};
106static 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};
112static 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 */
119static const opus_int32 stereo_voice_threshold = 26000;
120static const opus_int32 stereo_music_threshold = 36000;
121
122/* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
123static const opus_int32 mode_thresholds[2][2] = {
124 /* voice */ /* music */
125 { 48000, 24000}, /* mono */
126 { 48000, 24000}, /* stereo */
127};
128
129int 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
143int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application)
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&&channels!=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_CUTOFF_HZ ), 8 );
216 st->first = 1;
217 st->mode = MODE_HYBRID;
218 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
219
220 return OPUS_OK;
221}
222
223static 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
261static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
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
294static 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
333static void hp_cutoff(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
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 / 1000, 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_Q19 );
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_CONST( 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
368static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
369 int overlap48, int frame_size, int channels, const opus_val16 *window, opus_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*channels+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*channels+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
400OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
401{
402 int ret;
403 OpusEncoder *st;
404 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=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
430static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_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
443opus_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
447opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
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; /* Number of bytes to use for redundancy frame */
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; /* Probability of voice in Q7 */
469 opus_int32 equiv_rate;
470 int delay_compensation;
471 int frame_rate;
472 opus_int32 max_rate; /* Max bitrate we're allowed to use */
473 int curr_bandwidth;
474 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
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_data_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*(stereo_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 > (128-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.toMono==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 smooth 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 /* For the first frame at a new SILK bandwidth */
656 if (st->silk_bw_switch)
657 {
658 redundancy = 1;
659 celt_to_silk = 1;
660 st->silk_bw_switch = 0;
661 prefill=1;
662 }
663
664 if (redundancy)
665 {
666 /* Fair share of the max size allowed */
667 redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(frame_size+st->Fs/200));
668 /* For VBR, target the actual bitrate (subject to the limit above) */
669 if (st->use_vbr)
670 redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600);
671 }
672
673 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
674 {
675 silk_EncControlStruct dummy;
676 silk_InitEncoder( silk_enc, &dummy);
677 prefill=1;
678 }
679
680 /* Automatic (rate-dependent) bandwidth selection */
681 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
682 {
683 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
684 opus_int32 bandwidth_thresholds[8];
685 int bandwidth = OPUS_BANDWIDTH_FULLBAND;
686 opus_int32 equiv_rate2;
687
688 equiv_rate2 = equiv_rate;
689 if (st->mode != MODE_CELT_ONLY)
690 {
691 /* Adjust the threshold +/- 10% depending on complexity */
692 equiv_rate2 = equiv_rate2 * (45+st->silk_mode.complexity)/50;
693 /* CBR is less efficient by ~1 kb/s */
694 if (!st->use_vbr)
695 equiv_rate2 -= 1000;
696 }
697 if (st->channels==2 && st->force_channels!=1)
698 {
699 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
700 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
701 } else {
702 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
703 music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
704 }
705 /* Interpolate bandwidth thresholds depending on voice estimation */
706 for (i=0;i<8;i++)
707 {
708 bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
709 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
710 }
711 do {
712 int threshold, hysteresis;
713 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
714 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
715 if (!st->first)
716 {
717 if (st->bandwidth >= bandwidth)
718 threshold -= hysteresis;
719 else
720 threshold += hysteresis;
721 }
722 if (equiv_rate2 >= threshold)
723 break;
724 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
725 st->bandwidth = bandwidth;
726 /* Prevents any transition to SWB/FB until the SILK layer has fully
727 switched to WB mode and turned the variable LP filter off */
728 if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
729 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
730 }
731
732 if (st->bandwidth>st->max_bandwidth)
733 st->bandwidth = st->max_bandwidth;
734
735 if (st->user_bandwidth != OPUS_AUTO)
736 st->bandwidth = st->user_bandwidth;
737
738 /* This prevents us from using hybrid at unsafe CBR/max rates */
739 if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
740 {
741 st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
742 }
743
744 /* Prevents Opus from wasting bits on frequencies that are above
745 the Nyquist rate of the input signal */
746 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
747 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
748 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
749 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
750 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
751 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
752 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
753 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
754
755 /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */
756 if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
757 st->mode = MODE_CELT_ONLY;
758
759 /* CELT mode doesn't support mediumband, use wideband instead */
760 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
761 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
762
763 /* Can't support higher than wideband for >20 ms frames */
764 if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND))
765 {
766 VARDECL(unsigned char, tmp_data);
767 int nb_frames;
768 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
769 OpusRepacketizer rp;
770 opus_int32 bytes_per_frame;
771
772
773 nb_frames = frame_size > st->Fs/25 ? 3 : 2;
774 bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames);
775
776 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
777
778 opus_repacketizer_init(&rp);
779
780 bak_mode = st->user_forced_mode;
781 bak_bandwidth = st->user_bandwidth;
782 bak_channels = st->force_channels;
783
784 st->user_forced_mode = st->mode;
785 st->user_bandwidth = st->bandwidth;
786 st->force_channels = st->stream_channels;
787 bak_to_mono = st->silk_mode.toMono;
788
789 if (bak_to_mono)
790 st->force_channels = 1;
791 else
792 st->prev_channels = st->stream_channels;
793 for (i=0;i<nb_frames;i++)
794 {
795 int tmp_len;
796 st->silk_mode.toMono = 0;
797 /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
798 if (to_celt && i==nb_frames-1)
799 st->user_forced_mode = MODE_CELT_ONLY;
800 tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50, tmp_data+i*bytes_per_frame, bytes_per_frame);
801 if (tmp_len<0)
802 {
803 RESTORE_STACK;
804 return OPUS_INTERNAL_ERROR;
805 }
806 ret = opus_repacketizer_cat(&rp, tmp_data+i*bytes_per_frame, tmp_len);
807 if (ret<0)
808 {
809 RESTORE_STACK;
810 return OPUS_INTERNAL_ERROR;
811 }
812 }
813 ret = opus_repacketizer_out(&rp, data, out_data_bytes);
814 if (ret<0)
815 {
816 RESTORE_STACK;
817 return OPUS_INTERNAL_ERROR;
818 }
819 st->user_forced_mode = bak_mode;
820 st->user_bandwidth = bak_bandwidth;
821 st->force_channels = bak_channels;
822 st->silk_mode.toMono = bak_to_mono;
823 RESTORE_STACK;
824 return ret;
825 }
826
827 curr_bandwidth = st->bandwidth;
828
829 /* Chooses the appropriate mode for speech
830 *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
831 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
832 st->mode = MODE_HYBRID;
833 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
834 st->mode = MODE_SILK_ONLY;
835
836 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
837 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
838
839 data += 1;
840
841 ec_enc_init(&enc, data, max_data_bytes-1);
842
843 ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16);
844 for (i=0;i<delay_compensation*st->channels;i++)
845 pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st->channels+i];
846
847 if (st->mode == MODE_CELT_ONLY)
848 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
849 else
850 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
851
852 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
853 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
854
855 /* convert from log scale to Hertz */
856 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
857
858 if (st->application == OPUS_APPLICATION_VOIP)
859 {
860 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[delay_compensation*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
861 } else {
862 for (i=0;i<frame_size*st->channels;i++)
863 pcm_buf[delay_compensation*st->channels + i] = pcm[i];
864 }
865
866 /* SILK processing */
867 if (st->mode != MODE_CELT_ONLY)
868 {
869#ifdef FIXED_POINT
870 const opus_int16 *pcm_silk;
871#else
872 VARDECL(opus_int16, pcm_silk);
873 ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
874#endif
875 st->silk_mode.bitRate = 8*bytes_target*frame_rate;
876 if( st->mode == MODE_HYBRID ) {
877 st->silk_mode.bitRate /= st->stream_channels;
878 if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) {
879 if( st->Fs == 100 * frame_size ) {
880 /* 24 kHz, 10 ms */
881 st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + st->use_vbr * 1000 ) * 2 ) / 3;
882 } else {
883 /* 24 kHz, 20 ms */
884 st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 1000 + st->use_vbr * 1000 ) * 2 ) / 3;
885 }
886 } else {
887 if( st->Fs == 100 * frame_size ) {
888 /* 48 kHz, 10 ms */
889 st->silk_mode.bitRate = ( st->silk_mode.bitRate + 8000 + st->use_vbr * 3000 ) / 2;
890 } else {
891 /* 48 kHz, 20 ms */
892 st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st->use_vbr * 1000 ) / 2;
893 }
894 }
895 st->silk_mode.bitRate *= st->stream_channels;
896 /* don't let SILK use more than 80% */
897 if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) {
898 st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
899 }
900 }
901
902 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
903 st->silk_mode.nChannelsAPI = st->channels;
904 st->silk_mode.nChannelsInternal = st->stream_channels;
905 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
906 st->silk_mode.desiredInternalSampleRate = 8000;
907 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
908 st->silk_mode.desiredInternalSampleRate = 12000;
909 } else {
910 silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
911 st->silk_mode.desiredInternalSampleRate = 16000;
912 }
913 if( st->mode == MODE_HYBRID ) {
914 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
915 st->silk_mode.minInternalSampleRate = 16000;
916 } else {
917 st->silk_mode.minInternalSampleRate = 8000;
918 }
919
920 if (st->mode == MODE_SILK_ONLY)
921 {
922 opus_int32 effective_max_rate = max_rate;
923 st->silk_mode.maxInternalSampleRate = 16000;
924 if (frame_rate > 50)
925 effective_max_rate = effective_max_rate*2/3;
926 if (effective_max_rate < 13000)
927 {
928 st->silk_mode.maxInternalSampleRate = 12000;
929 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
930 }
931 if (effective_max_rate < 9600)
932 {
933 st->silk_mode.maxInternalSampleRate = 8000;
934 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
935 }
936 } else {
937 st->silk_mode.maxInternalSampleRate = 16000;
938 }
939
940 st->silk_mode.useCBR = !st->use_vbr;
941
942 /* Call SILK encoder for the low band */
943 nBytes = IMIN(1275, max_data_bytes-1-redundancy_bytes);
944
945 st->silk_mode.maxBits = nBytes*8;
946 /* Only allow up to 90% of the bits for hybrid mode*/
947 if (st->mode == MODE_HYBRID)
948 st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10;
949 if (st->silk_mode.useCBR)
950 {
951 st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8;
952 /* Reduce the initial target to make it easier to reach the CBR rate */
953 st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000);
954 }
955
956 if (prefill)
957 {
958 opus_int32 zero=0;
959#ifdef FIXED_POINT
960 pcm_silk = st->delay_buffer;
961#else
962 for (i=0;i<st->encoder_buffer*st->channels;i++)
963 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
964#endif
965 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 );
966 }
967
968#ifdef FIXED_POINT
969 pcm_silk = pcm_buf+delay_compensation*st->channels;
970#else
971 for (i=0;i<frame_size*st->channels;i++)
972 pcm_silk[i] = FLOAT2INT16(pcm_buf[delay_compensation*st->channels + i]);
973#endif
974 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
975 if( ret ) {
976 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
977 /* Handle error */
978 RESTORE_STACK;
979 return OPUS_INTERNAL_ERROR;
980 }
981 if (nBytes==0)
982 {
983 st->rangeFinal = 0;
984 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
985 RESTORE_STACK;
986 return 1;
987 }
988 /* Extract SILK internal bandwidth for signaling in first byte */
989 if( st->mode == MODE_SILK_ONLY ) {
990 if( st->silk_mode.internalSampleRate == 8000 ) {
991 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
992 } else if( st->silk_mode.internalSampleRate == 12000 ) {
993 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
994 } else if( st->silk_mode.internalSampleRate == 16000 ) {
995 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
996 }
997 } else {
998 silk_assert( st->silk_mode.internalSampleRate == 16000 );
999 }
1000
1001 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady;
1002 /* FIXME: How do we allocate the redundancy for CBR? */
1003 if (st->silk_mode.opusCanSwitch)
1004 {
1005 redundancy = 1;
1006 celt_to_silk = 0;
1007 st->silk_bw_switch = 1;
1008 }
1009 }
1010
1011 /* CELT processing */
1012 {
1013 int endband=21;
1014
1015 switch(curr_bandwidth)
1016 {
1017 case OPUS_BANDWIDTH_NARROWBAND:
1018 endband = 13;
1019 break;
1020 case OPUS_BANDWIDTH_MEDIUMBAND:
1021 case OPUS_BANDWIDTH_WIDEBAND:
1022 endband = 17;
1023 break;
1024 case OPUS_BANDWIDTH_SUPERWIDEBAND:
1025 endband = 19;
1026 break;
1027 case OPUS_BANDWIDTH_FULLBAND:
1028 endband = 21;
1029 break;
1030 }
1031 celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
1032 celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
1033 }
1034 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
1035 if (st->mode != MODE_SILK_ONLY)
1036 {
1037 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1038 /* Allow prediction unless we decide to disable it later */
1039 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(2));
1040
1041 if (st->mode == MODE_HYBRID)
1042 {
1043 int len;
1044
1045 len = (ec_tell(&enc)+7)>>3;
1046 if (redundancy)
1047 len += st->mode == MODE_HYBRID ? 3 : 1;
1048 if( st->use_vbr ) {
1049 nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * frame_size) / (8 * st->Fs);
1050 } else {
1051 /* check if SILK used up too much */
1052 nb_compr_bytes = len > bytes_target ? len : bytes_target;
1053 }
1054 } else {
1055 if (st->use_vbr)
1056 {
1057 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
1058 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
1059 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
1060 nb_compr_bytes = max_data_bytes-1-redundancy_bytes;
1061 } else {
1062 nb_compr_bytes = bytes_target;
1063 }
1064 }
1065
1066 } else {
1067 nb_compr_bytes = 0;
1068 }
1069
1070 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
1071 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
1072 {
1073 for (i=0;i<st->channels*st->Fs/400;i++)
1074 tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-st->delay_compensation-st->Fs/400)*st->channels + i];
1075 }
1076
1077 for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+delay_compensation));i++)
1078 st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size];
1079 for (;i<st->encoder_buffer*st->channels;i++)
1080 st->delay_buffer[i] = pcm_buf[(frame_size+delay_compensation-st->encoder_buffer)*st->channels+i];
1081
1082
1083 if (st->mode != MODE_HYBRID || st->stream_channels==1)
1084 st->silk_mode.stereoWidth_Q14 = 1<<14;
1085 if( st->channels == 2 ) {
1086 /* Apply stereo width reduction (at low bitrates) */
1087 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
1088 opus_val16 g1, g2;
1089 const CELTMode *celt_mode;
1090
1091 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1092 g1 = st->hybrid_stereo_width_Q14;
1093 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
1094#ifdef FIXED_POINT
1095 g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
1096 g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
1097#else
1098 g1 *= (1.f/16384);
1099 g2 *= (1.f/16384);
1100#endif
1101 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
1102 frame_size, st->channels, celt_mode->window, st->Fs);
1103 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
1104 }
1105 }
1106
1107 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
1108 {
1109 /* For SILK mode, the redundancy is inferred from the length */
1110 if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_compr_bytes))
1111 ec_enc_bit_logp(&enc, redundancy, 12);
1112 if (redundancy)
1113 {
1114 int max_redundancy;
1115 ec_enc_bit_logp(&enc, celt_to_silk, 1);
1116 if (st->mode == MODE_HYBRID)
1117 max_redundancy = (max_data_bytes-1)-nb_compr_bytes-1;
1118 else
1119 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
1120 /* Target the same bit-rate for redundancy as for the rest,
1121 up to a max of 257 bytes */
1122 redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600);
1123 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
1124 if (st->mode == MODE_HYBRID)
1125 ec_enc_uint(&enc, redundancy_bytes-2, 256);
1126 }
1127 } else {
1128 redundancy = 0;
1129 }
1130
1131 if (!redundancy)
1132 {
1133 st->silk_bw_switch = 0;
1134 redundancy_bytes = 0;
1135 }
1136 if (st->mode != MODE_CELT_ONLY)start_band=17;
1137
1138 if (st->mode == MODE_SILK_ONLY)
1139 {
1140 ret = (ec_tell(&enc)+7)>>3;
1141 ec_enc_done(&enc);
1142 nb_compr_bytes = ret;
1143 } else {
1144 nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes);
1145 ec_enc_shrink(&enc, nb_compr_bytes);
1146 }
1147
1148
1149 /* 5 ms redundant frame for CELT->SILK */
1150 if (redundancy && celt_to_silk)
1151 {
1152 int err;
1153 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
1154 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1155 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
1156 if (err < 0)
1157 {
1158 RESTORE_STACK;
1159 return OPUS_INTERNAL_ERROR;
1160 }
1161 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
1162 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1163 }
1164
1165 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
1166
1167 if (st->mode != MODE_SILK_ONLY)
1168 {
1169 if (st->mode != st->prev_mode && st->prev_mode > 0)
1170 {
1171 unsigned char dummy[2];
1172 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1173
1174 /* Prefilling */
1175 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
1176 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
1177 }
1178 /* If false, we already busted the budget and we'll end up with a "PLC packet" */
1179 if (ec_tell(&enc) <= 8*nb_compr_bytes)
1180 {
1181 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
1182 if (ret < 0)
1183 {
1184 RESTORE_STACK;
1185 return OPUS_INTERNAL_ERROR;
1186 }
1187 }
1188 }
1189
1190 /* 5 ms redundant frame for SILK->CELT */
1191 if (redundancy && !celt_to_silk)
1192 {
1193 int err;
1194 unsigned char dummy[2];
1195 int N2, N4;
1196 N2 = st->Fs/200;
1197 N4 = st->Fs/400;
1198
1199 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1200 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
1201 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
1202
1203 /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
1204 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
1205
1206 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
1207 if (err < 0)
1208 {
1209 RESTORE_STACK;
1210 return OPUS_INTERNAL_ERROR;
1211 }
1212 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
1213 }
1214
1215
1216
1217 /* Signalling the mode in the first byte */
1218 data--;
1219 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
1220
1221 st->rangeFinal = enc.rng ^ redundant_rng;
1222
1223 if (to_celt)
1224 st->prev_mode = MODE_CELT_ONLY;
1225 else
1226 st->prev_mode = st->mode;
1227 st->prev_channels = st->stream_channels;
1228 st->prev_framesize = frame_size;
1229
1230 st->first = 0;
1231
1232 /* In the unlikely case that the SILK encoder busted its target, tell
1233 the decoder to call the PLC */
1234 if (ec_tell(&enc) > (max_data_bytes-1)*8)
1235 {
1236 if (max_data_bytes < 2)
1237 {
1238 RESTORE_STACK;
1239 return OPUS_BUFFER_TOO_SMALL;
1240 }
1241 data[1] = 0;
1242 ret = 1;
1243 st->rangeFinal = 0;
1244 } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
1245 {
1246 /*When in LPC only mode it's perfectly
1247 reasonable to strip off trailing zero bytes as
1248 the required range decoder behavior is to
1249 fill these in. This can't be done when the MDCT
1250 modes are used because the decoder needs to know
1251 the actual length for allocation purposes.*/
1252 while(ret>2&&data[ret]==0)ret--;
1253 }
1254 /* Count ToC and redundancy */
1255 ret += 1+redundancy_bytes;
1256 if (!st->use_vbr && ret >= 3)
1257 {
1258 if (pad_frame(data, ret, max_data_bytes))
1259 {
1260 RESTORE_STACK;
1261 return OPUS_INTERNAL_ERROR;
1262 }
1263 ret = max_data_bytes;
1264 }
1265 RESTORE_STACK;
1266 return ret;
1267}
1268
1269#ifdef FIXED_POINT
1270
1271#ifndef DISABLE_FLOAT_API
1272opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int frame_size,
1273 unsigned char *data, opus_int32 max_data_bytes)
1274{
1275 int i, ret;
1276 VARDECL(opus_int16, in);
1277 ALLOC_STACK;
1278
1279 if(frame_size<0)
1280 {
1281 RESTORE_STACK;
1282 return OPUS_BAD_ARG;
1283 }
1284
1285 ALLOC(in, frame_size*st->channels, opus_int16);
1286
1287 for (i=0;i<frame_size*st->channels;i++)
1288 in[i] = FLOAT2INT16(pcm[i]);
1289 ret = opus_encode(st, in, frame_size, data, max_data_bytes);
1290 RESTORE_STACK;
1291 return ret;
1292}
1293#endif
1294
1295#else
1296opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size,
1297 unsigned char *data, opus_int32 max_data_bytes)
1298{
1299 int i, ret;
1300 VARDECL(float, in);
1301 ALLOC_STACK;
1302
1303 ALLOC(in, frame_size*st->channels, float);
1304
1305 for (i=0;i<frame_size*st->channels;i++)
1306 in[i] = (1.0f/32768)*pcm[i];
1307 ret = opus_encode_float(st, in, frame_size, data, max_data_bytes);
1308 RESTORE_STACK;
1309 return ret;
1310}
1311#endif
1312
1313
1314int opus_encoder_ctl(OpusEncoder *st, int request, ...)
1315{
1316 int ret;
1317 CELTEncoder *celt_enc;
1318 va_list ap;
1319
1320 ret = OPUS_OK;
1321 va_start(ap, request);
1322
1323 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1324
1325 switch (request)
1326 {
1327 case OPUS_SET_APPLICATION_REQUEST:
1328 {
1329 opus_int32 value = va_arg(ap, opus_int32);
1330 if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
1331 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1332 || (!st->first && st->application != value))
1333 {
1334 ret = OPUS_BAD_ARG;
1335 break;
1336 }
1337 st->application = value;
1338 }
1339 break;
1340 case OPUS_GET_APPLICATION_REQUEST:
1341 {
1342 opus_int32 *value = va_arg(ap, opus_int32*);
1343 *value = st->application;
1344 }
1345 break;
1346 case OPUS_SET_BITRATE_REQUEST:
1347 {
1348 opus_int32 value = va_arg(ap, opus_int32);
1349 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
1350 {
1351 if (value <= 0)
1352 goto bad_arg;
1353 else if (value <= 500)
1354 value = 500;
1355 else if (value > (opus_int32)300000*st->channels)
1356 value = (opus_int32)300000*st->channels;
1357 }
1358 st->user_bitrate_bps = value;
1359 }
1360 break;
1361 case OPUS_GET_BITRATE_REQUEST:
1362 {
1363 opus_int32 *value = va_arg(ap, opus_int32*);
1364 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
1365 }
1366 break;
1367 case OPUS_SET_FORCE_CHANNELS_REQUEST:
1368 {
1369 opus_int32 value = va_arg(ap, opus_int32);
1370 if((value<1 || value>st->channels) && value != OPUS_AUTO)
1371 return OPUS_BAD_ARG;
1372 st->force_channels = value;
1373 }
1374 break;
1375 case OPUS_GET_FORCE_CHANNELS_REQUEST:
1376 {
1377 opus_int32 *value = va_arg(ap, opus_int32*);
1378 *value = st->force_channels;
1379 }
1380 break;
1381 case OPUS_SET_MAX_BANDWIDTH_REQUEST:
1382 {
1383 opus_int32 value = va_arg(ap, opus_int32);
1384 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
1385 return OPUS_BAD_ARG;
1386 st->max_bandwidth = value;
1387 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1388 st->silk_mode.maxInternalSampleRate = 8000;
1389 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1390 st->silk_mode.maxInternalSampleRate = 12000;
1391 } else {
1392 st->silk_mode.maxInternalSampleRate = 16000;
1393 }
1394 }
1395 break;
1396 case OPUS_GET_MAX_BANDWIDTH_REQUEST:
1397 {
1398 opus_int32 *value = va_arg(ap, opus_int32*);
1399 *value = st->max_bandwidth;
1400 }
1401 break;
1402 case OPUS_SET_BANDWIDTH_REQUEST:
1403 {
1404 opus_int32 value = va_arg(ap, opus_int32);
1405 if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
1406 return OPUS_BAD_ARG;
1407 st->user_bandwidth = value;
1408 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1409 st->silk_mode.maxInternalSampleRate = 8000;
1410 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1411 st->silk_mode.maxInternalSampleRate = 12000;
1412 } else {
1413 st->silk_mode.maxInternalSampleRate = 16000;
1414 }
1415 }
1416 break;
1417 case OPUS_GET_BANDWIDTH_REQUEST:
1418 {
1419 opus_int32 *value = va_arg(ap, opus_int32*);
1420 *value = st->bandwidth;
1421 }
1422 break;
1423 case OPUS_SET_DTX_REQUEST:
1424 {
1425 opus_int32 value = va_arg(ap, opus_int32);
1426 if(value<0 || value>1)
1427 return OPUS_BAD_ARG;
1428 st->silk_mode.useDTX = value;
1429 }
1430 break;
1431 case OPUS_GET_DTX_REQUEST:
1432 {
1433 opus_int32 *value = va_arg(ap, opus_int32*);
1434 *value = st->silk_mode.useDTX;
1435 }
1436 break;
1437 case OPUS_SET_COMPLEXITY_REQUEST:
1438 {
1439 opus_int32 value = va_arg(ap, opus_int32);
1440 if(value<0 || value>10)
1441 return OPUS_BAD_ARG;
1442 st->silk_mode.complexity = value;
1443 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
1444 }
1445 break;
1446 case OPUS_GET_COMPLEXITY_REQUEST:
1447 {
1448 opus_int32 *value = va_arg(ap, opus_int32*);
1449 *value = st->silk_mode.complexity;
1450 }
1451 break;
1452 case OPUS_SET_INBAND_FEC_REQUEST:
1453 {
1454 opus_int32 value = va_arg(ap, opus_int32);
1455 if(value<0 || value>1)
1456 return OPUS_BAD_ARG;
1457 st->silk_mode.useInBandFEC = value;
1458 }
1459 break;
1460 case OPUS_GET_INBAND_FEC_REQUEST:
1461 {
1462 opus_int32 *value = va_arg(ap, opus_int32*);
1463 *value = st->silk_mode.useInBandFEC;
1464 }
1465 break;
1466 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1467 {
1468 opus_int32 value = va_arg(ap, opus_int32);
1469 if (value < 0 || value > 100)
1470 return OPUS_BAD_ARG;
1471 st->silk_mode.packetLossPercentage = value;
1472 celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
1473 }
1474 break;
1475 case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
1476 {
1477 opus_int32 *value = va_arg(ap, opus_int32*);
1478 *value = st->silk_mode.packetLossPercentage;
1479 }
1480 break;
1481 case OPUS_SET_VBR_REQUEST:
1482 {
1483 opus_int32 value = va_arg(ap, opus_int32);
1484 if(value<0 || value>1)
1485 return OPUS_BAD_ARG;
1486 st->use_vbr = value;
1487 st->silk_mode.useCBR = 1-value;
1488 }
1489 break;
1490 case OPUS_GET_VBR_REQUEST:
1491 {
1492 opus_int32 *value = va_arg(ap, opus_int32*);
1493 *value = st->use_vbr;
1494 }
1495 break;
1496 case OPUS_SET_VOICE_RATIO_REQUEST:
1497 {
1498 opus_int32 value = va_arg(ap, opus_int32);
1499 if (value>100 || value<-1)
1500 goto bad_arg;
1501 st->voice_ratio = value;
1502 }
1503 break;
1504 case OPUS_GET_VOICE_RATIO_REQUEST:
1505 {
1506 opus_int32 *value = va_arg(ap, opus_int32*);
1507 *value = st->voice_ratio;
1508 }
1509 break;
1510 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1511 {
1512 opus_int32 value = va_arg(ap, opus_int32);
1513 if(value<0 || value>1)
1514 return OPUS_BAD_ARG;
1515 st->vbr_constraint = value;
1516 }
1517 break;
1518 case OPUS_GET_VBR_CONSTRAINT_REQUEST:
1519 {
1520 opus_int32 *value = va_arg(ap, opus_int32*);
1521 *value = st->vbr_constraint;
1522 }
1523 break;
1524 case OPUS_SET_SIGNAL_REQUEST:
1525 {
1526 opus_int32 value = va_arg(ap, opus_int32);
1527 if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
1528 return OPUS_BAD_ARG;
1529 st->signal_type = value;
1530 }
1531 break;
1532 case OPUS_GET_SIGNAL_REQUEST:
1533 {
1534 opus_int32 *value = va_arg(ap, opus_int32*);
1535 *value = st->signal_type;
1536 }
1537 break;
1538 case OPUS_GET_LOOKAHEAD_REQUEST:
1539 {
1540 opus_int32 *value = va_arg(ap, opus_int32*);
1541 *value = st->Fs/400;
1542 if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1543 *value += st->delay_compensation;
1544 }
1545 break;
1546 case OPUS_GET_SAMPLE_RATE_REQUEST:
1547 {
1548 opus_int32 *value = va_arg(ap, opus_int32*);
1549 if (value==NULL)
1550 {
1551 ret = OPUS_BAD_ARG;
1552 break;
1553 }
1554 *value = st->Fs;
1555 }
1556 break;
1557 case OPUS_GET_FINAL_RANGE_REQUEST:
1558 {
1559 opus_uint32 *value = va_arg(ap, opus_uint32*);
1560 *value = st->rangeFinal;
1561 }
1562 break;
1563 case OPUS_SET_LSB_DEPTH_REQUEST:
1564 {
1565 opus_int32 value = va_arg(ap, opus_int32);
1566 ret = celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(value));
1567 }
1568 break;
1569 case OPUS_GET_LSB_DEPTH_REQUEST:
1570 {
1571 opus_int32 *value = va_arg(ap, opus_int32*);
1572 celt_encoder_ctl(celt_enc, OPUS_GET_LSB_DEPTH(value));
1573 }
1574 break;
1575 case OPUS_RESET_STATE:
1576 {
1577 void *silk_enc;
1578 silk_EncControlStruct dummy;
1579 silk_enc = (char*)st+st->silk_enc_offset;
1580
1581 OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START,
1582 sizeof(OpusEncoder)-
1583 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st));
1584
1585 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
1586 silk_InitEncoder( silk_enc, &dummy );
1587 st->stream_channels = st->channels;
1588 st->hybrid_stereo_width_Q14 = 1 << 14;
1589 st->first = 1;
1590 st->mode = MODE_HYBRID;
1591 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
1592 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1593 }
1594 break;
1595 case OPUS_SET_FORCE_MODE_REQUEST:
1596 {
1597 opus_int32 value = va_arg(ap, opus_int32);
1598 if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
1599 goto bad_arg;
1600 st->user_forced_mode = value;
1601 }
1602 break;
1603 default:
1604 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
1605 ret = OPUS_UNIMPLEMENTED;
1606 break;
1607 }
1608 va_end(ap);
1609 return ret;
1610bad_arg:
1611 va_end(ap);
1612 return OPUS_BAD_ARG;
1613}
1614
1615void opus_encoder_destroy(OpusEncoder *st)
1616{
1617 opus_free(st);
1618}