Coverage Report

Created: 2025-11-24 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/src/opus_encoder.c
Line
Count
Source
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 "pitch.h"
42
#include "opus_private.h"
43
#include "os_support.h"
44
#include "cpu_support.h"
45
#include "analysis.h"
46
#include "mathops.h"
47
#include "tuning_parameters.h"
48
49
#ifdef ENABLE_DRED
50
#include "dred_coding.h"
51
#endif
52
53
#ifdef FIXED_POINT
54
#include "fixed/structs_FIX.h"
55
#else
56
#include "float/structs_FLP.h"
57
#endif
58
#ifdef ENABLE_OSCE_TRAINING_DATA
59
#include <stdio.h>
60
#endif
61
62
#ifdef ENABLE_QEXT
63
#define MAX_ENCODER_BUFFER 960
64
#else
65
#define MAX_ENCODER_BUFFER 480
66
#endif
67
68
#ifndef DISABLE_FLOAT_API
69
6.92k
#define PSEUDO_SNR_THRESHOLD 316.23f    /* 10^(25/10) */
70
#endif
71
72
typedef struct {
73
   opus_val32 XX, XY, YY;
74
   opus_val16 smoothed_width;
75
   opus_val16 max_follower;
76
} StereoWidthState;
77
78
struct OpusEncoder {
79
    int          celt_enc_offset;
80
    int          silk_enc_offset;
81
    silk_EncControlStruct silk_mode;
82
#ifdef ENABLE_DRED
83
    DREDEnc      dred_encoder;
84
#endif
85
    int          application;
86
    int          channels;
87
    int          delay_compensation;
88
    int          force_channels;
89
    int          signal_type;
90
    int          user_bandwidth;
91
    int          max_bandwidth;
92
    int          user_forced_mode;
93
    int          voice_ratio;
94
    opus_int32   Fs;
95
    int          use_vbr;
96
    int          vbr_constraint;
97
    int          variable_duration;
98
    opus_int32   bitrate_bps;
99
    opus_int32   user_bitrate_bps;
100
    int          lsb_depth;
101
    int          encoder_buffer;
102
    int          lfe;
103
    int          arch;
104
    int          use_dtx;                 /* general DTX for both SILK and CELT */
105
    int          fec_config;
106
#ifndef DISABLE_FLOAT_API
107
    TonalityAnalysisState analysis;
108
#endif
109
#ifdef ENABLE_QEXT
110
   int enable_qext;
111
#endif
112
113
0
#define OPUS_ENCODER_RESET_START stream_channels
114
    int          stream_channels;
115
    opus_int16   hybrid_stereo_width_Q14;
116
    opus_int32   variable_HP_smth2_Q15;
117
    opus_val16   prev_HB_gain;
118
    opus_val32   hp_mem[4];
119
    int          mode;
120
    int          prev_mode;
121
    int          prev_channels;
122
    int          prev_framesize;
123
    int          bandwidth;
124
    /* Bandwidth determined automatically from the rate (before any other adjustment) */
125
    int          auto_bandwidth;
126
    int          silk_bw_switch;
127
    /* Sampling rate (at the API level) */
128
    int          first;
129
    celt_glog * energy_masking;
130
    StereoWidthState width_mem;
131
    opus_res     delay_buffer[MAX_ENCODER_BUFFER*2];
132
#ifndef DISABLE_FLOAT_API
133
    int          detected_bandwidth;
134
    int          nb_no_activity_ms_Q1;
135
    opus_val32   peak_signal_energy;
136
#endif
137
#ifdef ENABLE_DRED
138
    int          dred_duration;
139
    int          dred_q0;
140
    int          dred_dQ;
141
    int          dred_qmax;
142
    int          dred_target_chunks;
143
    unsigned char activity_mem[DRED_MAX_FRAMES*4]; /* 2.5ms resolution*/
144
#endif
145
    int          nonfinal_frame; /* current frame is not the final in a packet */
146
    opus_uint32  rangeFinal;
147
};
148
149
/* Transition tables for the voice and music. First column is the
150
   middle (memoriless) threshold. The second column is the hysteresis
151
   (difference with the middle) */
152
static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
153
         9000,  700, /* NB<->MB */
154
         9000,  700, /* MB<->WB */
155
        13500, 1000, /* WB<->SWB */
156
        14000, 2000, /* SWB<->FB */
157
};
158
static const opus_int32 mono_music_bandwidth_thresholds[8] = {
159
         9000,  700, /* NB<->MB */
160
         9000,  700, /* MB<->WB */
161
        11000, 1000, /* WB<->SWB */
162
        12000, 2000, /* SWB<->FB */
163
};
164
static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
165
         9000,  700, /* NB<->MB */
166
         9000,  700, /* MB<->WB */
167
        13500, 1000, /* WB<->SWB */
168
        14000, 2000, /* SWB<->FB */
169
};
170
static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
171
         9000,  700, /* NB<->MB */
172
         9000,  700, /* MB<->WB */
173
        11000, 1000, /* WB<->SWB */
174
        12000, 2000, /* SWB<->FB */
175
};
176
/* Threshold bit-rates for switching between mono and stereo */
177
static const opus_int32 stereo_voice_threshold = 19000;
178
static const opus_int32 stereo_music_threshold = 17000;
179
180
/* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
181
static const opus_int32 mode_thresholds[2][2] = {
182
      /* voice */ /* music */
183
      {  64000,      10000}, /* mono */
184
      {  44000,      10000}, /* stereo */
185
};
186
187
static const opus_int32 fec_thresholds[] = {
188
        12000, 1000, /* NB */
189
        14000, 1000, /* MB */
190
        16000, 1000, /* WB */
191
        20000, 1000, /* SWB */
192
        22000, 1000, /* FB */
193
};
194
195
int opus_encoder_get_size(int channels)
196
2.10M
{
197
2.10M
    int ret;
198
2.10M
    ret = opus_encoder_init(NULL, 48000, channels, OPUS_APPLICATION_AUDIO);
199
2.10M
    if (ret < 0)
200
0
        return 0;
201
2.10M
    else
202
2.10M
        return ret;
203
2.10M
}
204
205
int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application)
206
1.35M
{
207
1.35M
    void *silk_enc=NULL;
208
1.35M
    CELTEncoder *celt_enc=NULL;
209
1.35M
    int err;
210
1.35M
    int ret, silkEncSizeBytes, celtEncSizeBytes=0;
211
1.35M
    int tot_size;
212
213
1.35M
   if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000
214
10.7k
#ifdef ENABLE_QEXT
215
10.7k
         &&Fs!=96000
216
1.35M
#endif
217
1.35M
         )||(channels!=1&&channels!=2)||
218
1.35M
        (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
219
85.2k
        && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY
220
0
        && application != OPUS_APPLICATION_FORCED_SILK
221
0
        && application != OPUS_APPLICATION_FORCED_CELT))
222
0
        return OPUS_BAD_ARG;
223
224
    /* Create SILK encoder */
225
1.35M
    ret = silk_Get_Encoder_Size( &silkEncSizeBytes, channels );
226
1.35M
    if (ret)
227
0
        return OPUS_BAD_ARG;
228
1.35M
    silkEncSizeBytes = align(silkEncSizeBytes);
229
1.35M
    if (application == OPUS_APPLICATION_FORCED_CELT)
230
0
        silkEncSizeBytes = 0;
231
1.35M
    if (application != OPUS_APPLICATION_FORCED_SILK)
232
1.35M
        celtEncSizeBytes = celt_encoder_get_size(channels);
233
1.35M
    tot_size = align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes;
234
1.35M
    if (st == NULL) {
235
1.09M
        return tot_size;
236
1.09M
    }
237
258k
    OPUS_CLEAR((char*)st, tot_size);
238
258k
    st->silk_enc_offset = align(sizeof(OpusEncoder));
239
258k
    st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
240
241
258k
    st->stream_channels = st->channels = channels;
242
243
258k
    st->Fs = Fs;
244
245
258k
    st->arch = opus_select_arch();
246
247
258k
    if (application != OPUS_APPLICATION_FORCED_CELT)
248
258k
    {
249
258k
       silk_enc = (char*)st+st->silk_enc_offset;
250
258k
       ret = silk_InitEncoder( silk_enc, st->channels, st->arch, &st->silk_mode );
251
258k
    }
252
258k
    if(ret)return OPUS_INTERNAL_ERROR;
253
254
    /* default SILK parameters */
255
258k
    st->silk_mode.nChannelsAPI              = channels;
256
258k
    st->silk_mode.nChannelsInternal         = channels;
257
258k
    st->silk_mode.API_sampleRate            = st->Fs;
258
258k
    st->silk_mode.maxInternalSampleRate     = 16000;
259
258k
    st->silk_mode.minInternalSampleRate     = 8000;
260
258k
    st->silk_mode.desiredInternalSampleRate = 16000;
261
258k
    st->silk_mode.payloadSize_ms            = 20;
262
258k
    st->silk_mode.bitRate                   = 25000;
263
258k
    st->silk_mode.packetLossPercentage      = 0;
264
258k
    st->silk_mode.complexity                = 9;
265
258k
    st->silk_mode.useInBandFEC              = 0;
266
258k
    st->silk_mode.useDRED                   = 0;
267
258k
    st->silk_mode.useDTX                    = 0;
268
258k
    st->silk_mode.useCBR                    = 0;
269
258k
    st->silk_mode.reducedDependency         = 0;
270
271
    /* Create CELT encoder */
272
    /* Initialize CELT encoder */
273
258k
    if (application != OPUS_APPLICATION_FORCED_SILK)
274
258k
    {
275
258k
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
276
258k
       err = celt_encoder_init(celt_enc, Fs, channels, st->arch);
277
258k
       if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
278
258k
       celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
279
258k
       celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity));
280
258k
    }
281
282
#ifdef ENABLE_DRED
283
    /* Initialize DRED Encoder */
284
    dred_encoder_init( &st->dred_encoder, Fs, channels );
285
#endif
286
287
258k
    st->use_vbr = 1;
288
    /* Makes constrained VBR the default (safer for real-time use) */
289
258k
    st->vbr_constraint = 1;
290
258k
    st->user_bitrate_bps = OPUS_AUTO;
291
258k
    st->bitrate_bps = 3000+Fs*channels;
292
258k
    st->application = application;
293
258k
    st->signal_type = OPUS_AUTO;
294
258k
    st->user_bandwidth = OPUS_AUTO;
295
258k
    st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
296
258k
    st->force_channels = OPUS_AUTO;
297
258k
    st->user_forced_mode = OPUS_AUTO;
298
258k
    st->voice_ratio = -1;
299
258k
    st->encoder_buffer = st->Fs/100;
300
258k
    st->lsb_depth = 24;
301
258k
    st->variable_duration = OPUS_FRAMESIZE_ARG;
302
303
    /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
304
       + 1.5 ms for SILK resamplers and stereo prediction) */
305
258k
    st->delay_compensation = st->Fs/250;
306
307
258k
    st->hybrid_stereo_width_Q14 = 1 << 14;
308
258k
    st->prev_HB_gain = Q15ONE;
309
258k
    st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
310
258k
    st->first = 1;
311
258k
    st->mode = MODE_HYBRID;
312
258k
    st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
313
314
258k
#ifndef DISABLE_FLOAT_API
315
258k
    tonality_analysis_init(&st->analysis, st->Fs);
316
258k
    st->analysis.application = st->application;
317
258k
#endif
318
319
258k
    return OPUS_OK;
320
258k
}
321
322
static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
323
632k
{
324
632k
   int period;
325
632k
   unsigned char toc;
326
632k
   period = 0;
327
2.01M
   while (framerate < 400)
328
1.37M
   {
329
1.37M
       framerate <<= 1;
330
1.37M
       period++;
331
1.37M
   }
332
632k
   if (mode == MODE_SILK_ONLY)
333
250k
   {
334
250k
       toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
335
250k
       toc |= (period-2)<<3;
336
382k
   } else if (mode == MODE_CELT_ONLY)
337
325k
   {
338
325k
       int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND;
339
325k
       if (tmp < 0)
340
221k
           tmp = 0;
341
325k
       toc = 0x80;
342
325k
       toc |= tmp << 5;
343
325k
       toc |= period<<3;
344
325k
   } else /* Hybrid */
345
56.5k
   {
346
56.5k
       toc = 0x60;
347
56.5k
       toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4;
348
56.5k
       toc |= (period-2)<<3;
349
56.5k
   }
350
632k
   toc |= (channels==2)<<2;
351
632k
   return toc;
352
632k
}
353
354
#ifdef FIXED_POINT
355
/* Second order ARMA filter, alternative implementation */
356
void silk_biquad_res(
357
    const opus_res              *in,                /* I     input signal                                               */
358
    const opus_int32            *B_Q28,             /* I     MA coefficients [3]                                        */
359
    const opus_int32            *A_Q28,             /* I     AR coefficients [2]                                        */
360
    opus_int32                  *S,                 /* I/O   State vector [2]                                           */
361
    opus_res                    *out,               /* O     output signal                                              */
362
    const opus_int32            len,                /* I     signal length (must be even)                               */
363
    int stride
364
)
365
126k
{
366
    /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
367
126k
    opus_int   k;
368
126k
    opus_int32 inval, A0_U_Q28, A0_L_Q28, A1_U_Q28, A1_L_Q28, out32_Q14;
369
370
    /* Negate A_Q28 values and split in two parts */
371
126k
    A0_L_Q28 = ( -A_Q28[ 0 ] ) & 0x00003FFF;        /* lower part */
372
126k
    A0_U_Q28 = silk_RSHIFT( -A_Q28[ 0 ], 14 );      /* upper part */
373
126k
    A1_L_Q28 = ( -A_Q28[ 1 ] ) & 0x00003FFF;        /* lower part */
374
126k
    A1_U_Q28 = silk_RSHIFT( -A_Q28[ 1 ], 14 );      /* upper part */
375
376
36.1M
    for( k = 0; k < len; k++ ) {
377
        /* S[ 0 ], S[ 1 ]: Q12 */
378
36.0M
        inval = RES2INT16(in[ k*stride ]);
379
36.0M
        out32_Q14 = silk_LSHIFT( silk_SMLAWB( S[ 0 ], B_Q28[ 0 ], inval ), 2 );
380
381
36.0M
        S[ 0 ] = S[1] + silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A0_L_Q28 ), 14 );
382
36.0M
        S[ 0 ] = silk_SMLAWB( S[ 0 ], out32_Q14, A0_U_Q28 );
383
36.0M
        S[ 0 ] = silk_SMLAWB( S[ 0 ], B_Q28[ 1 ], inval);
384
385
36.0M
        S[ 1 ] = silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A1_L_Q28 ), 14 );
386
36.0M
        S[ 1 ] = silk_SMLAWB( S[ 1 ], out32_Q14, A1_U_Q28 );
387
36.0M
        S[ 1 ] = silk_SMLAWB( S[ 1 ], B_Q28[ 2 ], inval );
388
389
        /* Scale back to Q0 and saturate */
390
36.0M
        out[ k*stride ] = INT16TORES( silk_SAT16( silk_RSHIFT( out32_Q14 + (1<<14) - 1, 14 ) ) );
391
36.0M
    }
392
126k
}
393
#else
394
static void silk_biquad_res(
395
    const opus_res        *in,            /* I:    Input signal                   */
396
    const opus_int32      *B_Q28,         /* I:    MA coefficients [3]            */
397
    const opus_int32      *A_Q28,         /* I:    AR coefficients [2]            */
398
    opus_val32            *S,             /* I/O:  State vector [2]               */
399
    opus_res              *out,           /* O:    Output signal                  */
400
    const opus_int32      len,            /* I:    Signal length (must be even)   */
401
    int stride
402
)
403
99.6k
{
404
    /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
405
99.6k
    opus_int   k;
406
99.6k
    opus_val32 vout;
407
99.6k
    opus_val32 inval;
408
99.6k
    opus_val32 A[2], B[3];
409
410
99.6k
    A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28)));
411
99.6k
    A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28)));
412
99.6k
    B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28)));
413
99.6k
    B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28)));
414
99.6k
    B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28)));
415
416
    /* Negate A_Q28 values and split in two parts */
417
418
27.8M
    for( k = 0; k < len; k++ ) {
419
        /* S[ 0 ], S[ 1 ]: Q12 */
420
27.7M
        inval = in[ k*stride ];
421
27.7M
        vout = S[ 0 ] + B[0]*inval;
422
423
27.7M
        S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
424
425
27.7M
        S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL;
426
427
        /* Scale back to Q0 and saturate */
428
27.7M
        out[ k*stride ] = vout;
429
27.7M
    }
430
99.6k
}
431
#endif
432
433
static void hp_cutoff(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs, int arch)
434
151k
{
435
151k
   opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
436
151k
   opus_int32 Fc_Q19, r_Q28, r_Q22;
437
151k
   (void)arch;
438
439
151k
   silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) );
440
151k
   Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
441
151k
   silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
442
443
151k
   r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 );
444
445
   /* b = r * [ 1; -2; 1 ]; */
446
   /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
447
151k
   B_Q28[ 0 ] = r_Q28;
448
151k
   B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 );
449
151k
   B_Q28[ 2 ] = r_Q28;
450
451
   /* -r * ( 2 - Fc * Fc ); */
452
151k
   r_Q22  = silk_RSHIFT( r_Q28, 6 );
453
151k
   A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0,  22 ) );
454
151k
   A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 );
455
456
#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
457
   if( channels == 1 ) {
458
      silk_biquad_alt_stride1( in, B_Q28, A_Q28, hp_mem, out, len );
459
   } else {
460
      silk_biquad_alt_stride2( in, B_Q28, A_Q28, hp_mem, out, len, arch );
461
   }
462
#else
463
151k
   silk_biquad_res( in, B_Q28, A_Q28, hp_mem, out, len, channels );
464
151k
   if( channels == 2 ) {
465
75.0k
       silk_biquad_res( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
466
75.0k
   }
467
151k
#endif
468
151k
}
469
470
#ifdef FIXED_POINT
471
static void dc_reject(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
472
219k
{
473
219k
   int c, i;
474
219k
   int shift;
475
476
   /* Approximates -round(log2(6.3*cutoff_Hz/Fs)) */
477
219k
   shift=celt_ilog2(Fs/(cutoff_Hz*4));
478
525k
   for (c=0;c<channels;c++)
479
305k
   {
480
55.3M
      for (i=0;i<len;i++)
481
55.0M
      {
482
55.0M
         opus_val32 x, y;
483
         /* Saturate at +6 dBFS to avoid any wrap-around. */
484
55.0M
         x = SATURATE((opus_val32)in[channels*i+c], (1<<16<<RES_SHIFT)-1);
485
55.0M
         x = SHL32(x, 14-RES_SHIFT);
486
55.0M
         y = x-hp_mem[2*c];
487
55.0M
         hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift);
488
55.0M
#ifdef ENABLE_RES24
489
         /* Don't saturate if we have the headroom to avoid it. */
490
55.0M
         out[channels*i+c] = PSHR32(y, 14-RES_SHIFT);
491
#else
492
         out[channels*i+c] = SATURATE(PSHR32(y, 14-RES_SHIFT), 32767);
493
#endif
494
55.0M
      }
495
305k
   }
496
219k
}
497
498
#else
499
static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
500
208k
{
501
208k
   int i;
502
208k
   float coef, coef2;
503
208k
   coef = 6.3f*cutoff_Hz/Fs;
504
208k
   coef2 = 1-coef;
505
208k
   if (channels==2)
506
86.1k
   {
507
86.1k
      float m0, m2;
508
86.1k
      m0 = hp_mem[0];
509
86.1k
      m2 = hp_mem[2];
510
21.1M
      for (i=0;i<len;i++)
511
21.0M
      {
512
21.0M
         opus_val32 x0, x1, out0, out1;
513
21.0M
         x0 = in[2*i+0];
514
21.0M
         x1 = in[2*i+1];
515
21.0M
         out0 = x0-m0;
516
21.0M
         out1 = x1-m2;
517
21.0M
         m0 = coef*x0 + VERY_SMALL + coef2*m0;
518
21.0M
         m2 = coef*x1 + VERY_SMALL + coef2*m2;
519
21.0M
         out[2*i+0] = out0;
520
21.0M
         out[2*i+1] = out1;
521
21.0M
      }
522
86.1k
      hp_mem[0] = m0;
523
86.1k
      hp_mem[2] = m2;
524
121k
   } else {
525
121k
      float m0;
526
121k
      m0 = hp_mem[0];
527
21.9M
      for (i=0;i<len;i++)
528
21.8M
      {
529
21.8M
         opus_val32 x, y;
530
21.8M
         x = in[i];
531
21.8M
         y = x-m0;
532
21.8M
         m0 = coef*x + VERY_SMALL + coef2*m0;
533
21.8M
         out[i] = y;
534
21.8M
      }
535
121k
      hp_mem[0] = m0;
536
121k
   }
537
208k
}
538
#endif
539
540
static void stereo_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2,
541
        int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs)
542
214k
{
543
214k
    int i;
544
214k
    int overlap;
545
214k
    int inc;
546
214k
    inc = IMAX(1, 48000/Fs);
547
214k
    overlap=overlap48/inc;
548
214k
    g1 = Q15ONE-g1;
549
214k
    g2 = Q15ONE-g2;
550
5.04M
    for (i=0;i<overlap;i++)
551
4.83M
    {
552
4.83M
       opus_val32 diff;
553
4.83M
       opus_val16 g, w;
554
4.83M
       w = COEF2VAL16(window[i*inc]);
555
4.83M
       w = MULT16_16_Q15(w, w);
556
4.83M
       g = SHR32(MAC16_16(MULT16_16(w,g2),
557
4.83M
             Q15ONE-w, g1), 15);
558
4.83M
       diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
559
4.83M
       diff = MULT16_RES_Q15(g, diff);
560
4.83M
       out[i*channels] = out[i*channels] - diff;
561
4.83M
       out[i*channels+1] = out[i*channels+1] + diff;
562
4.83M
    }
563
36.9M
    for (;i<frame_size;i++)
564
36.7M
    {
565
36.7M
       opus_val32 diff;
566
36.7M
       diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
567
36.7M
       diff = MULT16_RES_Q15(g2, diff);
568
36.7M
       out[i*channels] = out[i*channels] - diff;
569
36.7M
       out[i*channels+1] = out[i*channels+1] + diff;
570
36.7M
    }
571
214k
}
opus_encoder.c:stereo_fade
Line
Count
Source
542
71.4k
{
543
71.4k
    int i;
544
71.4k
    int overlap;
545
71.4k
    int inc;
546
71.4k
    inc = IMAX(1, 48000/Fs);
547
71.4k
    overlap=overlap48/inc;
548
71.4k
    g1 = Q15ONE-g1;
549
71.4k
    g2 = Q15ONE-g2;
550
1.68M
    for (i=0;i<overlap;i++)
551
1.61M
    {
552
1.61M
       opus_val32 diff;
553
1.61M
       opus_val16 g, w;
554
1.61M
       w = COEF2VAL16(window[i*inc]);
555
1.61M
       w = MULT16_16_Q15(w, w);
556
1.61M
       g = SHR32(MAC16_16(MULT16_16(w,g2),
557
1.61M
             Q15ONE-w, g1), 15);
558
1.61M
       diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
559
1.61M
       diff = MULT16_RES_Q15(g, diff);
560
1.61M
       out[i*channels] = out[i*channels] - diff;
561
1.61M
       out[i*channels+1] = out[i*channels+1] + diff;
562
1.61M
    }
563
12.3M
    for (;i<frame_size;i++)
564
12.2M
    {
565
12.2M
       opus_val32 diff;
566
12.2M
       diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
567
12.2M
       diff = MULT16_RES_Q15(g2, diff);
568
12.2M
       out[i*channels] = out[i*channels] - diff;
569
12.2M
       out[i*channels+1] = out[i*channels+1] + diff;
570
12.2M
    }
571
71.4k
}
opus_encoder.c:stereo_fade
Line
Count
Source
542
71.4k
{
543
71.4k
    int i;
544
71.4k
    int overlap;
545
71.4k
    int inc;
546
71.4k
    inc = IMAX(1, 48000/Fs);
547
71.4k
    overlap=overlap48/inc;
548
71.4k
    g1 = Q15ONE-g1;
549
71.4k
    g2 = Q15ONE-g2;
550
1.68M
    for (i=0;i<overlap;i++)
551
1.61M
    {
552
1.61M
       opus_val32 diff;
553
1.61M
       opus_val16 g, w;
554
1.61M
       w = COEF2VAL16(window[i*inc]);
555
1.61M
       w = MULT16_16_Q15(w, w);
556
1.61M
       g = SHR32(MAC16_16(MULT16_16(w,g2),
557
1.61M
             Q15ONE-w, g1), 15);
558
1.61M
       diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
559
1.61M
       diff = MULT16_RES_Q15(g, diff);
560
1.61M
       out[i*channels] = out[i*channels] - diff;
561
1.61M
       out[i*channels+1] = out[i*channels+1] + diff;
562
1.61M
    }
563
12.3M
    for (;i<frame_size;i++)
564
12.2M
    {
565
12.2M
       opus_val32 diff;
566
12.2M
       diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
567
12.2M
       diff = MULT16_RES_Q15(g2, diff);
568
12.2M
       out[i*channels] = out[i*channels] - diff;
569
12.2M
       out[i*channels+1] = out[i*channels+1] + diff;
570
12.2M
    }
571
71.4k
}
opus_encoder.c:stereo_fade
Line
Count
Source
542
71.4k
{
543
71.4k
    int i;
544
71.4k
    int overlap;
545
71.4k
    int inc;
546
71.4k
    inc = IMAX(1, 48000/Fs);
547
71.4k
    overlap=overlap48/inc;
548
71.4k
    g1 = Q15ONE-g1;
549
71.4k
    g2 = Q15ONE-g2;
550
1.68M
    for (i=0;i<overlap;i++)
551
1.61M
    {
552
1.61M
       opus_val32 diff;
553
1.61M
       opus_val16 g, w;
554
1.61M
       w = COEF2VAL16(window[i*inc]);
555
1.61M
       w = MULT16_16_Q15(w, w);
556
1.61M
       g = SHR32(MAC16_16(MULT16_16(w,g2),
557
1.61M
             Q15ONE-w, g1), 15);
558
1.61M
       diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
559
1.61M
       diff = MULT16_RES_Q15(g, diff);
560
1.61M
       out[i*channels] = out[i*channels] - diff;
561
1.61M
       out[i*channels+1] = out[i*channels+1] + diff;
562
1.61M
    }
563
12.3M
    for (;i<frame_size;i++)
564
12.2M
    {
565
12.2M
       opus_val32 diff;
566
12.2M
       diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
567
12.2M
       diff = MULT16_RES_Q15(g2, diff);
568
12.2M
       out[i*channels] = out[i*channels] - diff;
569
12.2M
       out[i*channels+1] = out[i*channels+1] + diff;
570
12.2M
    }
571
71.4k
}
572
573
static void gain_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2,
574
        int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs)
575
45.1k
{
576
45.1k
    int i;
577
45.1k
    int inc;
578
45.1k
    int overlap;
579
45.1k
    int c;
580
45.1k
    inc = IMAX(1, 48000/Fs);
581
45.1k
    overlap=overlap48/inc;
582
45.1k
    if (channels==1)
583
27.8k
    {
584
2.01M
       for (i=0;i<overlap;i++)
585
1.98M
       {
586
1.98M
          opus_val16 g, w;
587
1.98M
          w = COEF2VAL16(window[i*inc]);
588
1.98M
          w = MULT16_16_Q15(w, w);
589
1.98M
          g = SHR32(MAC16_16(MULT16_16(w,g2),
590
1.98M
                Q15ONE-w, g1), 15);
591
1.98M
          out[i] = MULT16_RES_Q15(g, in[i]);
592
1.98M
       }
593
27.8k
    } else {
594
1.17M
       for (i=0;i<overlap;i++)
595
1.16M
       {
596
1.16M
          opus_val16 g, w;
597
1.16M
          w = COEF2VAL16(window[i*inc]);
598
1.16M
          w = MULT16_16_Q15(w, w);
599
1.16M
          g = SHR32(MAC16_16(MULT16_16(w,g2),
600
1.16M
                Q15ONE-w, g1), 15);
601
1.16M
          out[i*2] = MULT16_RES_Q15(g, in[i*2]);
602
1.16M
          out[i*2+1] = MULT16_RES_Q15(g, in[i*2+1]);
603
1.16M
       }
604
17.2k
    }
605
62.3k
    c=0;do {
606
24.3M
       for (i=overlap;i<frame_size;i++)
607
24.2M
       {
608
24.2M
          out[i*channels+c] = MULT16_RES_Q15(g2, in[i*channels+c]);
609
24.2M
       }
610
62.3k
    }
611
62.3k
    while (++c<channels);
612
45.1k
}
opus_encoder.c:gain_fade
Line
Count
Source
575
15.0k
{
576
15.0k
    int i;
577
15.0k
    int inc;
578
15.0k
    int overlap;
579
15.0k
    int c;
580
15.0k
    inc = IMAX(1, 48000/Fs);
581
15.0k
    overlap=overlap48/inc;
582
15.0k
    if (channels==1)
583
9.27k
    {
584
670k
       for (i=0;i<overlap;i++)
585
661k
       {
586
661k
          opus_val16 g, w;
587
661k
          w = COEF2VAL16(window[i*inc]);
588
661k
          w = MULT16_16_Q15(w, w);
589
661k
          g = SHR32(MAC16_16(MULT16_16(w,g2),
590
661k
                Q15ONE-w, g1), 15);
591
661k
          out[i] = MULT16_RES_Q15(g, in[i]);
592
661k
       }
593
9.27k
    } else {
594
392k
       for (i=0;i<overlap;i++)
595
387k
       {
596
387k
          opus_val16 g, w;
597
387k
          w = COEF2VAL16(window[i*inc]);
598
387k
          w = MULT16_16_Q15(w, w);
599
387k
          g = SHR32(MAC16_16(MULT16_16(w,g2),
600
387k
                Q15ONE-w, g1), 15);
601
387k
          out[i*2] = MULT16_RES_Q15(g, in[i*2]);
602
387k
          out[i*2+1] = MULT16_RES_Q15(g, in[i*2+1]);
603
387k
       }
604
5.75k
    }
605
20.7k
    c=0;do {
606
8.11M
       for (i=overlap;i<frame_size;i++)
607
8.09M
       {
608
8.09M
          out[i*channels+c] = MULT16_RES_Q15(g2, in[i*channels+c]);
609
8.09M
       }
610
20.7k
    }
611
20.7k
    while (++c<channels);
612
15.0k
}
opus_encoder.c:gain_fade
Line
Count
Source
575
15.0k
{
576
15.0k
    int i;
577
15.0k
    int inc;
578
15.0k
    int overlap;
579
15.0k
    int c;
580
15.0k
    inc = IMAX(1, 48000/Fs);
581
15.0k
    overlap=overlap48/inc;
582
15.0k
    if (channels==1)
583
9.27k
    {
584
670k
       for (i=0;i<overlap;i++)
585
661k
       {
586
661k
          opus_val16 g, w;
587
661k
          w = COEF2VAL16(window[i*inc]);
588
661k
          w = MULT16_16_Q15(w, w);
589
661k
          g = SHR32(MAC16_16(MULT16_16(w,g2),
590
661k
                Q15ONE-w, g1), 15);
591
661k
          out[i] = MULT16_RES_Q15(g, in[i]);
592
661k
       }
593
9.27k
    } else {
594
392k
       for (i=0;i<overlap;i++)
595
387k
       {
596
387k
          opus_val16 g, w;
597
387k
          w = COEF2VAL16(window[i*inc]);
598
387k
          w = MULT16_16_Q15(w, w);
599
387k
          g = SHR32(MAC16_16(MULT16_16(w,g2),
600
387k
                Q15ONE-w, g1), 15);
601
387k
          out[i*2] = MULT16_RES_Q15(g, in[i*2]);
602
387k
          out[i*2+1] = MULT16_RES_Q15(g, in[i*2+1]);
603
387k
       }
604
5.75k
    }
605
20.7k
    c=0;do {
606
8.11M
       for (i=overlap;i<frame_size;i++)
607
8.09M
       {
608
8.09M
          out[i*channels+c] = MULT16_RES_Q15(g2, in[i*channels+c]);
609
8.09M
       }
610
20.7k
    }
611
20.7k
    while (++c<channels);
612
15.0k
}
opus_encoder.c:gain_fade
Line
Count
Source
575
15.0k
{
576
15.0k
    int i;
577
15.0k
    int inc;
578
15.0k
    int overlap;
579
15.0k
    int c;
580
15.0k
    inc = IMAX(1, 48000/Fs);
581
15.0k
    overlap=overlap48/inc;
582
15.0k
    if (channels==1)
583
9.27k
    {
584
670k
       for (i=0;i<overlap;i++)
585
661k
       {
586
661k
          opus_val16 g, w;
587
661k
          w = COEF2VAL16(window[i*inc]);
588
661k
          w = MULT16_16_Q15(w, w);
589
661k
          g = SHR32(MAC16_16(MULT16_16(w,g2),
590
661k
                Q15ONE-w, g1), 15);
591
661k
          out[i] = MULT16_RES_Q15(g, in[i]);
592
661k
       }
593
9.27k
    } else {
594
392k
       for (i=0;i<overlap;i++)
595
387k
       {
596
387k
          opus_val16 g, w;
597
387k
          w = COEF2VAL16(window[i*inc]);
598
387k
          w = MULT16_16_Q15(w, w);
599
387k
          g = SHR32(MAC16_16(MULT16_16(w,g2),
600
387k
                Q15ONE-w, g1), 15);
601
387k
          out[i*2] = MULT16_RES_Q15(g, in[i*2]);
602
387k
          out[i*2+1] = MULT16_RES_Q15(g, in[i*2+1]);
603
387k
       }
604
5.75k
    }
605
20.7k
    c=0;do {
606
8.11M
       for (i=overlap;i<frame_size;i++)
607
8.09M
       {
608
8.09M
          out[i*channels+c] = MULT16_RES_Q15(g2, in[i*channels+c]);
609
8.09M
       }
610
20.7k
    }
611
20.7k
    while (++c<channels);
612
15.0k
}
613
614
OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
615
0
{
616
0
   int ret;
617
0
   OpusEncoder *st;
618
0
   int size;
619
0
   if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000
620
0
#ifdef ENABLE_QEXT
621
0
         &&Fs!=96000
622
0
#endif
623
0
         )||(channels!=1&&channels!=2)||
624
0
       (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
625
0
       && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY
626
0
       && application != OPUS_APPLICATION_FORCED_SILK
627
0
       && application != OPUS_APPLICATION_FORCED_CELT))
628
0
   {
629
0
      if (error)
630
0
         *error = OPUS_BAD_ARG;
631
0
      return NULL;
632
0
   }
633
0
   size = opus_encoder_init(NULL, Fs, channels, application);
634
0
   if (size <= 0)
635
0
   {
636
0
      if (error)
637
0
         *error = OPUS_INTERNAL_ERROR;
638
0
      return NULL;
639
0
   }
640
0
   st = (OpusEncoder *)opus_alloc(size);
641
0
   if (st == NULL)
642
0
   {
643
0
      if (error)
644
0
         *error = OPUS_ALLOC_FAIL;
645
0
      return NULL;
646
0
   }
647
0
   ret = opus_encoder_init(st, Fs, channels, application);
648
0
   if (error)
649
0
      *error = ret;
650
0
   if (ret != OPUS_OK)
651
0
   {
652
0
      opus_free(st);
653
0
      st = NULL;
654
0
   }
655
0
   return st;
656
0
}
657
658
#ifdef ENABLE_DRED
659
660
static const float dred_bits_table[16] = {73.2f, 68.1f, 62.5f, 57.0f, 51.5f, 45.7f, 39.9f, 32.4f, 26.4f, 20.4f, 16.3f, 13.f, 9.3f, 8.2f, 7.2f, 6.4f};
661
static int estimate_dred_bitrate(int q0, int dQ, int qmax, int duration, opus_int32 target_bits, int *target_chunks) {
662
   int dred_chunks;
663
   int i;
664
   float bits;
665
   /* Signaling DRED costs 3 bytes. */
666
   bits = 8*(3+DRED_EXPERIMENTAL_BYTES);
667
   /* Approximation for the size of the IS. */
668
   bits += 50.f+dred_bits_table[q0];
669
   dred_chunks = IMIN((duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2);
670
   if (target_chunks != NULL) *target_chunks = 0;
671
   for (i=0;i<dred_chunks;i++) {
672
      int q = compute_quantizer(q0, dQ, qmax, i);
673
      bits += dred_bits_table[q];
674
      if (target_chunks != NULL && bits < target_bits) *target_chunks = i+1;
675
   }
676
   return (int)floor(.5f+bits);
677
}
678
679
static opus_int32 compute_dred_bitrate(OpusEncoder *st, opus_int32 bitrate_bps, int frame_size)
680
{
681
   float dred_frac;
682
   int bitrate_offset;
683
   opus_int32 dred_bitrate;
684
   opus_int32 target_dred_bitrate;
685
   int target_chunks;
686
   opus_int32 max_dred_bits;
687
   int q0, dQ, qmax;
688
   if (st->silk_mode.useInBandFEC) {
689
      dred_frac = MIN16(.7f, 3.f*st->silk_mode.packetLossPercentage/100.f);
690
      bitrate_offset = 20000;
691
   } else {
692
      if (st->silk_mode.packetLossPercentage > 5) {
693
         dred_frac = MIN16(.8f, .55f + st->silk_mode.packetLossPercentage/100.f);
694
      } else {
695
         dred_frac = 12*st->silk_mode.packetLossPercentage/100.f;
696
      }
697
      bitrate_offset = 12000;
698
   }
699
   /* Account for the fact that longer packets require less redundancy. */
700
   dred_frac = dred_frac/(dred_frac + (1-dred_frac)*(frame_size*50.f)/st->Fs);
701
   /* Approximate fit based on a few experiments. Could probably be improved. */
702
   q0 = IMIN(15, IMAX(4, 51 - 3*EC_ILOG(IMAX(1, bitrate_bps-bitrate_offset))));
703
   dQ = bitrate_bps-bitrate_offset > 36000 ? 3 : 5;
704
   qmax = 15;
705
   target_dred_bitrate = IMAX(0, (int)(dred_frac*(bitrate_bps-bitrate_offset)));
706
   if (st->dred_duration > 0) {
707
      opus_int32 target_bits = target_dred_bitrate*frame_size/st->Fs;
708
      max_dred_bits = estimate_dred_bitrate(q0, dQ, qmax, st->dred_duration, target_bits, &target_chunks);
709
   } else {
710
      max_dred_bits = 0;
711
      target_chunks=0;
712
   }
713
   dred_bitrate = IMIN(target_dred_bitrate, max_dred_bits*st->Fs/frame_size);
714
   /* If we can't afford enough bits, don't bother with DRED at all. */
715
   if (target_chunks < 2)
716
      dred_bitrate = 0;
717
   st->dred_q0 = q0;
718
   st->dred_dQ = dQ;
719
   st->dred_qmax = qmax;
720
   st->dred_target_chunks = target_chunks;
721
   return dred_bitrate;
722
}
723
#endif
724
725
static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes)
726
459k
{
727
459k
  if(!frame_size)frame_size=st->Fs/400;
728
459k
  if (st->user_bitrate_bps==OPUS_AUTO)
729
0
    return 60*st->Fs/frame_size + st->Fs*st->channels;
730
459k
  else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
731
0
    return max_data_bytes*8*st->Fs/frame_size;
732
459k
  else
733
459k
    return st->user_bitrate_bps;
734
459k
}
735
736
#ifndef DISABLE_FLOAT_API
737
void downmix_float(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
738
0
{
739
0
   const float *x;
740
0
   int j;
741
742
0
   x = (const float *)_x;
743
0
   for (j=0;j<subframe;j++)
744
0
      y[j] = FLOAT2SIG(x[(j+offset)*C+c1]);
745
0
   if (c2>-1)
746
0
   {
747
0
      for (j=0;j<subframe;j++)
748
0
         y[j] += FLOAT2SIG(x[(j+offset)*C+c2]);
749
0
   } else if (c2==-2)
750
0
   {
751
0
      int c;
752
0
      for (c=1;c<C;c++)
753
0
      {
754
0
         for (j=0;j<subframe;j++)
755
0
            y[j] += FLOAT2SIG(x[(j+offset)*C+c]);
756
0
      }
757
0
   }
758
0
}
759
#endif
760
761
void downmix_int(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
762
444k
{
763
444k
   const opus_int16 *x;
764
444k
   int j;
765
766
444k
   x = (const opus_int16 *)_x;
767
159M
   for (j=0;j<subframe;j++)
768
159M
      y[j] = INT16TOSIG(x[(j+offset)*C+c1]);
769
444k
   if (c2>-1)
770
165k
   {
771
65.2M
      for (j=0;j<subframe;j++)
772
65.0M
         y[j] += INT16TOSIG(x[(j+offset)*C+c2]);
773
278k
   } else if (c2==-2)
774
0
   {
775
0
      int c;
776
0
      for (c=1;c<C;c++)
777
0
      {
778
0
         for (j=0;j<subframe;j++)
779
0
            y[j] += INT16TOSIG(x[(j+offset)*C+c]);
780
0
      }
781
0
   }
782
444k
}
downmix_int
Line
Count
Source
762
222k
{
763
222k
   const opus_int16 *x;
764
222k
   int j;
765
766
222k
   x = (const opus_int16 *)_x;
767
79.9M
   for (j=0;j<subframe;j++)
768
79.7M
      y[j] = INT16TOSIG(x[(j+offset)*C+c1]);
769
222k
   if (c2>-1)
770
82.9k
   {
771
32.6M
      for (j=0;j<subframe;j++)
772
32.5M
         y[j] += INT16TOSIG(x[(j+offset)*C+c2]);
773
139k
   } else if (c2==-2)
774
0
   {
775
0
      int c;
776
0
      for (c=1;c<C;c++)
777
0
      {
778
0
         for (j=0;j<subframe;j++)
779
0
            y[j] += INT16TOSIG(x[(j+offset)*C+c]);
780
0
      }
781
0
   }
782
222k
}
downmix_int
Line
Count
Source
762
222k
{
763
222k
   const opus_int16 *x;
764
222k
   int j;
765
766
222k
   x = (const opus_int16 *)_x;
767
79.9M
   for (j=0;j<subframe;j++)
768
79.7M
      y[j] = INT16TOSIG(x[(j+offset)*C+c1]);
769
222k
   if (c2>-1)
770
82.9k
   {
771
32.6M
      for (j=0;j<subframe;j++)
772
32.5M
         y[j] += INT16TOSIG(x[(j+offset)*C+c2]);
773
139k
   } else if (c2==-2)
774
0
   {
775
0
      int c;
776
0
      for (c=1;c<C;c++)
777
0
      {
778
0
         for (j=0;j<subframe;j++)
779
0
            y[j] += INT16TOSIG(x[(j+offset)*C+c]);
780
0
      }
781
0
   }
782
222k
}
783
784
void downmix_int24(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
785
0
{
786
0
   const opus_int32 *x;
787
0
   int j;
788
789
0
   x = (const opus_int32 *)_x;
790
0
   for (j=0;j<subframe;j++)
791
0
      y[j] = INT24TOSIG(x[(j+offset)*C+c1]);
792
0
   if (c2>-1)
793
0
   {
794
0
      for (j=0;j<subframe;j++)
795
0
         y[j] += INT24TOSIG(x[(j+offset)*C+c2]);
796
0
   } else if (c2==-2)
797
0
   {
798
0
      int c;
799
0
      for (c=1;c<C;c++)
800
0
      {
801
0
         for (j=0;j<subframe;j++)
802
0
            y[j] += INT24TOSIG(x[(j+offset)*C+c]);
803
0
      }
804
0
   }
805
0
}
Unexecuted instantiation: downmix_int24
Unexecuted instantiation: downmix_int24
806
807
opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_int32 Fs)
808
67.7k
{
809
67.7k
   int new_size;
810
67.7k
   if (frame_size<Fs/400)
811
0
      return -1;
812
67.7k
   if (variable_duration == OPUS_FRAMESIZE_ARG)
813
67.7k
      new_size = frame_size;
814
0
   else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_120_MS)
815
0
   {
816
0
      if (variable_duration <= OPUS_FRAMESIZE_40_MS)
817
0
         new_size = (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS);
818
0
      else
819
0
         new_size = (variable_duration-OPUS_FRAMESIZE_2_5_MS-2)*Fs/50;
820
0
   }
821
0
   else
822
0
      return -1;
823
67.7k
   if (new_size>frame_size)
824
0
      return -1;
825
67.7k
   if (400*new_size!=Fs   && 200*new_size!=Fs   && 100*new_size!=Fs   &&
826
30.7k
        50*new_size!=Fs   &&  25*new_size!=Fs   &&  50*new_size!=3*Fs &&
827
12.7k
        50*new_size!=4*Fs &&  50*new_size!=5*Fs &&  50*new_size!=6*Fs)
828
0
      return -1;
829
67.7k
   return new_size;
830
67.7k
}
831
832
opus_val16 compute_stereo_width(const opus_res *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem)
833
128k
{
834
128k
   opus_val32 xx, xy, yy;
835
128k
   opus_val16 sqrt_xx, sqrt_yy;
836
128k
   opus_val16 qrrt_xx, qrrt_yy;
837
128k
   int frame_rate;
838
128k
   int i;
839
128k
   opus_val16 short_alpha;
840
841
128k
   frame_rate = Fs/frame_size;
842
128k
   short_alpha = Q15ONE - MULT16_16(25, Q15ONE)/IMAX(50,frame_rate);
843
128k
   xx=xy=yy=0;
844
   /* Unroll by 4. The frame size is always a multiple of 4 *except* for
845
      2.5 ms frames at 12 kHz. Since this setting is very rare (and very
846
      stupid), we just discard the last two samples. */
847
14.5M
   for (i=0;i<frame_size-3;i+=4)
848
14.4M
   {
849
14.4M
      opus_val32 pxx=0;
850
14.4M
      opus_val32 pxy=0;
851
14.4M
      opus_val32 pyy=0;
852
14.4M
      opus_val16 x, y;
853
14.4M
      x = RES2VAL16(pcm[2*i]);
854
14.4M
      y = RES2VAL16(pcm[2*i+1]);
855
14.4M
      pxx = SHR32(MULT16_16(x,x),2);
856
14.4M
      pxy = SHR32(MULT16_16(x,y),2);
857
14.4M
      pyy = SHR32(MULT16_16(y,y),2);
858
14.4M
      x = RES2VAL16(pcm[2*i+2]);
859
14.4M
      y = RES2VAL16(pcm[2*i+3]);
860
14.4M
      pxx += SHR32(MULT16_16(x,x),2);
861
14.4M
      pxy += SHR32(MULT16_16(x,y),2);
862
14.4M
      pyy += SHR32(MULT16_16(y,y),2);
863
14.4M
      x = RES2VAL16(pcm[2*i+4]);
864
14.4M
      y = RES2VAL16(pcm[2*i+5]);
865
14.4M
      pxx += SHR32(MULT16_16(x,x),2);
866
14.4M
      pxy += SHR32(MULT16_16(x,y),2);
867
14.4M
      pyy += SHR32(MULT16_16(y,y),2);
868
14.4M
      x = RES2VAL16(pcm[2*i+6]);
869
14.4M
      y = RES2VAL16(pcm[2*i+7]);
870
14.4M
      pxx += SHR32(MULT16_16(x,x),2);
871
14.4M
      pxy += SHR32(MULT16_16(x,y),2);
872
14.4M
      pyy += SHR32(MULT16_16(y,y),2);
873
874
14.4M
      xx += SHR32(pxx, 10);
875
14.4M
      xy += SHR32(pxy, 10);
876
14.4M
      yy += SHR32(pyy, 10);
877
14.4M
   }
878
#ifndef FIXED_POINT
879
57.2k
   if (!(xx < 1e9f) || celt_isnan(xx) || !(yy < 1e9f) || celt_isnan(yy))
880
0
   {
881
0
      xy = xx = yy = 0;
882
0
   }
883
#endif
884
128k
   mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX);
885
128k
   mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);
886
128k
   mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY);
887
128k
   mem->XX = MAX32(0, mem->XX);
888
128k
   mem->XY = MAX32(0, mem->XY);
889
128k
   mem->YY = MAX32(0, mem->YY);
890
128k
   if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
891
119k
   {
892
119k
      opus_val16 corr;
893
119k
      opus_val16 ldiff;
894
119k
      opus_val16 width;
895
119k
      sqrt_xx = celt_sqrt(mem->XX);
896
119k
      sqrt_yy = celt_sqrt(mem->YY);
897
119k
      qrrt_xx = celt_sqrt(sqrt_xx);
898
119k
      qrrt_yy = celt_sqrt(sqrt_yy);
899
      /* Inter-channel correlation */
900
119k
      mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy);
901
119k
      corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16);
902
      /* Approximate loudness difference */
903
119k
      ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))/(EPSILON+qrrt_xx+qrrt_yy);
904
119k
      width = MULT16_16_Q15(MIN16(Q15ONE, celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr))), ldiff);
905
      /* Smoothing over one second */
906
119k
      mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
907
      /* Peak follower */
908
119k
      mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
909
119k
   }
910
   /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(float)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE);*/
911
128k
   return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)));
912
128k
}
compute_stereo_width
Line
Count
Source
833
71.1k
{
834
71.1k
   opus_val32 xx, xy, yy;
835
71.1k
   opus_val16 sqrt_xx, sqrt_yy;
836
71.1k
   opus_val16 qrrt_xx, qrrt_yy;
837
71.1k
   int frame_rate;
838
71.1k
   int i;
839
71.1k
   opus_val16 short_alpha;
840
841
71.1k
   frame_rate = Fs/frame_size;
842
71.1k
   short_alpha = Q15ONE - MULT16_16(25, Q15ONE)/IMAX(50,frame_rate);
843
71.1k
   xx=xy=yy=0;
844
   /* Unroll by 4. The frame size is always a multiple of 4 *except* for
845
      2.5 ms frames at 12 kHz. Since this setting is very rare (and very
846
      stupid), we just discard the last two samples. */
847
7.40M
   for (i=0;i<frame_size-3;i+=4)
848
7.33M
   {
849
7.33M
      opus_val32 pxx=0;
850
7.33M
      opus_val32 pxy=0;
851
7.33M
      opus_val32 pyy=0;
852
7.33M
      opus_val16 x, y;
853
7.33M
      x = RES2VAL16(pcm[2*i]);
854
7.33M
      y = RES2VAL16(pcm[2*i+1]);
855
7.33M
      pxx = SHR32(MULT16_16(x,x),2);
856
7.33M
      pxy = SHR32(MULT16_16(x,y),2);
857
7.33M
      pyy = SHR32(MULT16_16(y,y),2);
858
7.33M
      x = RES2VAL16(pcm[2*i+2]);
859
7.33M
      y = RES2VAL16(pcm[2*i+3]);
860
7.33M
      pxx += SHR32(MULT16_16(x,x),2);
861
7.33M
      pxy += SHR32(MULT16_16(x,y),2);
862
7.33M
      pyy += SHR32(MULT16_16(y,y),2);
863
7.33M
      x = RES2VAL16(pcm[2*i+4]);
864
7.33M
      y = RES2VAL16(pcm[2*i+5]);
865
7.33M
      pxx += SHR32(MULT16_16(x,x),2);
866
7.33M
      pxy += SHR32(MULT16_16(x,y),2);
867
7.33M
      pyy += SHR32(MULT16_16(y,y),2);
868
7.33M
      x = RES2VAL16(pcm[2*i+6]);
869
7.33M
      y = RES2VAL16(pcm[2*i+7]);
870
7.33M
      pxx += SHR32(MULT16_16(x,x),2);
871
7.33M
      pxy += SHR32(MULT16_16(x,y),2);
872
7.33M
      pyy += SHR32(MULT16_16(y,y),2);
873
874
7.33M
      xx += SHR32(pxx, 10);
875
7.33M
      xy += SHR32(pxy, 10);
876
7.33M
      yy += SHR32(pyy, 10);
877
7.33M
   }
878
#ifndef FIXED_POINT
879
   if (!(xx < 1e9f) || celt_isnan(xx) || !(yy < 1e9f) || celt_isnan(yy))
880
   {
881
      xy = xx = yy = 0;
882
   }
883
#endif
884
71.1k
   mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX);
885
71.1k
   mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);
886
71.1k
   mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY);
887
71.1k
   mem->XX = MAX32(0, mem->XX);
888
71.1k
   mem->XY = MAX32(0, mem->XY);
889
71.1k
   mem->YY = MAX32(0, mem->YY);
890
71.1k
   if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
891
65.8k
   {
892
65.8k
      opus_val16 corr;
893
65.8k
      opus_val16 ldiff;
894
65.8k
      opus_val16 width;
895
65.8k
      sqrt_xx = celt_sqrt(mem->XX);
896
65.8k
      sqrt_yy = celt_sqrt(mem->YY);
897
65.8k
      qrrt_xx = celt_sqrt(sqrt_xx);
898
65.8k
      qrrt_yy = celt_sqrt(sqrt_yy);
899
      /* Inter-channel correlation */
900
65.8k
      mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy);
901
65.8k
      corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16);
902
      /* Approximate loudness difference */
903
65.8k
      ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))/(EPSILON+qrrt_xx+qrrt_yy);
904
65.8k
      width = MULT16_16_Q15(MIN16(Q15ONE, celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr))), ldiff);
905
      /* Smoothing over one second */
906
65.8k
      mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
907
      /* Peak follower */
908
65.8k
      mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
909
65.8k
   }
910
   /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(float)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE);*/
911
71.1k
   return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)));
912
71.1k
}
compute_stereo_width
Line
Count
Source
833
57.2k
{
834
57.2k
   opus_val32 xx, xy, yy;
835
57.2k
   opus_val16 sqrt_xx, sqrt_yy;
836
57.2k
   opus_val16 qrrt_xx, qrrt_yy;
837
57.2k
   int frame_rate;
838
57.2k
   int i;
839
57.2k
   opus_val16 short_alpha;
840
841
57.2k
   frame_rate = Fs/frame_size;
842
57.2k
   short_alpha = Q15ONE - MULT16_16(25, Q15ONE)/IMAX(50,frame_rate);
843
57.2k
   xx=xy=yy=0;
844
   /* Unroll by 4. The frame size is always a multiple of 4 *except* for
845
      2.5 ms frames at 12 kHz. Since this setting is very rare (and very
846
      stupid), we just discard the last two samples. */
847
7.13M
   for (i=0;i<frame_size-3;i+=4)
848
7.07M
   {
849
7.07M
      opus_val32 pxx=0;
850
7.07M
      opus_val32 pxy=0;
851
7.07M
      opus_val32 pyy=0;
852
7.07M
      opus_val16 x, y;
853
7.07M
      x = RES2VAL16(pcm[2*i]);
854
7.07M
      y = RES2VAL16(pcm[2*i+1]);
855
7.07M
      pxx = SHR32(MULT16_16(x,x),2);
856
7.07M
      pxy = SHR32(MULT16_16(x,y),2);
857
7.07M
      pyy = SHR32(MULT16_16(y,y),2);
858
7.07M
      x = RES2VAL16(pcm[2*i+2]);
859
7.07M
      y = RES2VAL16(pcm[2*i+3]);
860
7.07M
      pxx += SHR32(MULT16_16(x,x),2);
861
7.07M
      pxy += SHR32(MULT16_16(x,y),2);
862
7.07M
      pyy += SHR32(MULT16_16(y,y),2);
863
7.07M
      x = RES2VAL16(pcm[2*i+4]);
864
7.07M
      y = RES2VAL16(pcm[2*i+5]);
865
7.07M
      pxx += SHR32(MULT16_16(x,x),2);
866
7.07M
      pxy += SHR32(MULT16_16(x,y),2);
867
7.07M
      pyy += SHR32(MULT16_16(y,y),2);
868
7.07M
      x = RES2VAL16(pcm[2*i+6]);
869
7.07M
      y = RES2VAL16(pcm[2*i+7]);
870
7.07M
      pxx += SHR32(MULT16_16(x,x),2);
871
7.07M
      pxy += SHR32(MULT16_16(x,y),2);
872
7.07M
      pyy += SHR32(MULT16_16(y,y),2);
873
874
7.07M
      xx += SHR32(pxx, 10);
875
7.07M
      xy += SHR32(pxy, 10);
876
7.07M
      yy += SHR32(pyy, 10);
877
7.07M
   }
878
57.2k
#ifndef FIXED_POINT
879
57.2k
   if (!(xx < 1e9f) || celt_isnan(xx) || !(yy < 1e9f) || celt_isnan(yy))
880
0
   {
881
0
      xy = xx = yy = 0;
882
0
   }
883
57.2k
#endif
884
57.2k
   mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX);
885
57.2k
   mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);
886
57.2k
   mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY);
887
57.2k
   mem->XX = MAX32(0, mem->XX);
888
57.2k
   mem->XY = MAX32(0, mem->XY);
889
57.2k
   mem->YY = MAX32(0, mem->YY);
890
57.2k
   if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
891
53.8k
   {
892
53.8k
      opus_val16 corr;
893
53.8k
      opus_val16 ldiff;
894
53.8k
      opus_val16 width;
895
53.8k
      sqrt_xx = celt_sqrt(mem->XX);
896
53.8k
      sqrt_yy = celt_sqrt(mem->YY);
897
53.8k
      qrrt_xx = celt_sqrt(sqrt_xx);
898
53.8k
      qrrt_yy = celt_sqrt(sqrt_yy);
899
      /* Inter-channel correlation */
900
53.8k
      mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy);
901
53.8k
      corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16);
902
      /* Approximate loudness difference */
903
53.8k
      ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))/(EPSILON+qrrt_xx+qrrt_yy);
904
53.8k
      width = MULT16_16_Q15(MIN16(Q15ONE, celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr))), ldiff);
905
      /* Smoothing over one second */
906
53.8k
      mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
907
      /* Peak follower */
908
53.8k
      mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
909
53.8k
   }
910
   /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(float)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE);*/
911
57.2k
   return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)));
912
57.2k
}
913
914
static int decide_fec(int useInBandFEC, int PacketLoss_perc, int last_fec, int mode, int *bandwidth, opus_int32 rate)
915
405k
{
916
405k
   int orig_bandwidth;
917
405k
   if (!useInBandFEC || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY)
918
296k
      return 0;
919
108k
   orig_bandwidth = *bandwidth;
920
108k
   for (;;)
921
113k
   {
922
113k
      opus_int32 hysteresis;
923
113k
      opus_int32 LBRR_rate_thres_bps;
924
      /* Compute threshold for using FEC at the current bandwidth setting */
925
113k
      LBRR_rate_thres_bps = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND)];
926
113k
      hysteresis = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1];
927
113k
      if (last_fec == 1) LBRR_rate_thres_bps -= hysteresis;
928
113k
      if (last_fec == 0) LBRR_rate_thres_bps += hysteresis;
929
113k
      LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps,
930
113k
            125 - silk_min( PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) );
931
      /* If loss <= 5%, we look at whether we have enough rate to enable FEC.
932
         If loss > 5%, we decrease the bandwidth until we can enable FEC. */
933
113k
      if (rate > LBRR_rate_thres_bps)
934
96.1k
         return 1;
935
17.8k
      else if (PacketLoss_perc <= 5)
936
1.90k
         return 0;
937
15.9k
      else if (*bandwidth > OPUS_BANDWIDTH_NARROWBAND)
938
5.50k
         (*bandwidth)--;
939
10.4k
      else
940
10.4k
         break;
941
113k
   }
942
   /* Couldn't find any bandwidth to enable FEC, keep original bandwidth. */
943
10.4k
   *bandwidth = orig_bandwidth;
944
10.4k
   return 0;
945
108k
}
946
947
31.6k
static int compute_silk_rate_for_hybrid(int rate, int bandwidth, int frame20ms, int vbr, int fec, int channels) {
948
31.6k
   int entry;
949
31.6k
   int i;
950
31.6k
   int N;
951
31.6k
   int silk_rate;
952
31.6k
   static int rate_table[][5] = {
953
  /*  |total| |-------- SILK------------|
954
              |-- No FEC -| |--- FEC ---|
955
               10ms   20ms   10ms   20ms */
956
31.6k
      {    0,     0,     0,     0,     0},
957
31.6k
      {12000, 10000, 10000, 11000, 11000},
958
31.6k
      {16000, 13500, 13500, 15000, 15000},
959
31.6k
      {20000, 16000, 16000, 18000, 18000},
960
31.6k
      {24000, 18000, 18000, 21000, 21000},
961
31.6k
      {32000, 22000, 22000, 28000, 28000},
962
31.6k
      {64000, 38000, 38000, 50000, 50000}
963
31.6k
   };
964
   /* Do the allocation per-channel. */
965
31.6k
   rate /= channels;
966
31.6k
   entry = 1 + frame20ms + 2*fec;
967
31.6k
   N = sizeof(rate_table)/sizeof(rate_table[0]);
968
191k
   for (i=1;i<N;i++)
969
182k
   {
970
182k
      if (rate_table[i][0] > rate) break;
971
182k
   }
972
31.6k
   if (i == N)
973
8.86k
   {
974
8.86k
      silk_rate = rate_table[i-1][entry];
975
      /* For now, just give 50% of the extra bits to SILK. */
976
8.86k
      silk_rate += (rate-rate_table[i-1][0])/2;
977
22.7k
   } else {
978
22.7k
      opus_int32 lo, hi, x0, x1;
979
22.7k
      lo = rate_table[i-1][entry];
980
22.7k
      hi = rate_table[i][entry];
981
22.7k
      x0 = rate_table[i-1][0];
982
22.7k
      x1 = rate_table[i][0];
983
22.7k
      silk_rate = (lo*(x1-rate) + hi*(rate-x0))/(x1-x0);
984
22.7k
   }
985
31.6k
   if (!vbr)
986
5.17k
   {
987
      /* Tiny boost to SILK for CBR. We should probably tune this better. */
988
5.17k
      silk_rate += 100;
989
5.17k
   }
990
31.6k
   if (bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND)
991
26.4k
      silk_rate += 300;
992
31.6k
   silk_rate *= channels;
993
   /* Small adjustment for stereo (calibrated for 32 kb/s, haven't tried other bitrates). */
994
31.6k
   if (channels == 2 && rate >= 12000)
995
10.8k
      silk_rate -= 1000;
996
31.6k
   return silk_rate;
997
31.6k
}
998
999
/* Returns the equivalent bitrate corresponding to 20 ms frames,
1000
   complexity 10 VBR operation. */
1001
static opus_int32 compute_equiv_rate(opus_int32 bitrate, int channels,
1002
      int frame_rate, int vbr, int mode, int complexity, int loss)
1003
1.21M
{
1004
1.21M
   opus_int32 equiv;
1005
1.21M
   equiv = bitrate;
1006
   /* Take into account overhead from smaller frames. */
1007
1.21M
   if (frame_rate > 50)
1008
845k
      equiv -= (40*channels+20)*(frame_rate - 50);
1009
   /* CBR is about a 8% penalty for both SILK and CELT. */
1010
1.21M
   if (!vbr)
1011
482k
      equiv -= equiv/12;
1012
   /* Complexity makes about 10% difference (from 0 to 10) in general. */
1013
1.21M
   equiv = equiv * (90+complexity)/100;
1014
1.21M
   if (mode == MODE_SILK_ONLY || mode == MODE_HYBRID)
1015
177k
   {
1016
      /* SILK complexity 0-1 uses the non-delayed-decision NSQ, which
1017
         costs about 20%. */
1018
177k
      if (complexity<2)
1019
43.7k
         equiv = equiv*4/5;
1020
177k
      equiv -= equiv*loss/(6*loss + 10);
1021
1.03M
   } else if (mode == MODE_CELT_ONLY) {
1022
      /* CELT complexity 0-4 doesn't have the pitch filter, which costs
1023
         about 10%. */
1024
228k
      if (complexity<5)
1025
66.5k
         equiv = equiv*9/10;
1026
810k
   } else {
1027
      /* Mode not known yet */
1028
      /* Half the SILK loss*/
1029
810k
      equiv -= equiv*loss/(12*loss + 20);
1030
810k
   }
1031
1.21M
   return equiv;
1032
1.21M
}
1033
1034
#ifndef DISABLE_FLOAT_API
1035
1036
int is_digital_silence(const opus_res* pcm, int frame_size, int channels, int lsb_depth)
1037
83.1k
{
1038
83.1k
   int silence = 0;
1039
83.1k
   opus_val32 sample_max = 0;
1040
#ifdef MLP_TRAINING
1041
   return 0;
1042
#endif
1043
83.1k
   sample_max = celt_maxabs_res(pcm, frame_size*channels);
1044
1045
83.1k
#ifdef FIXED_POINT
1046
83.1k
   silence = (sample_max == 0);
1047
83.1k
   (void)lsb_depth;
1048
#else
1049
   silence = (sample_max <= (opus_val16) 1 / (1 << lsb_depth));
1050
#endif
1051
1052
83.1k
   return silence;
1053
83.1k
}
1054
1055
#ifdef FIXED_POINT
1056
static opus_val32 compute_frame_energy(const opus_res *pcm, int frame_size, int channels, int arch)
1057
64.7k
{
1058
64.7k
   int i;
1059
64.7k
   opus_val32 sample_max;
1060
64.7k
   int max_shift;
1061
64.7k
   int shift;
1062
64.7k
   opus_val32 energy = 0;
1063
64.7k
   int len = frame_size*channels;
1064
64.7k
   (void)arch;
1065
   /* Max amplitude in the signal */
1066
64.7k
   sample_max = RES2INT16(celt_maxabs_res(pcm, len));
1067
1068
   /* Compute the right shift required in the MAC to avoid an overflow */
1069
64.7k
   max_shift = celt_ilog2(len);
1070
64.7k
   shift = IMAX(0, (celt_ilog2(1+sample_max) << 1) + max_shift - 28);
1071
1072
   /* Compute the energy */
1073
88.5M
   for (i=0; i<len; i++)
1074
88.5M
      energy += SHR32(MULT16_16(RES2INT16(pcm[i]), RES2INT16(pcm[i])), shift);
1075
1076
   /* Normalize energy by the frame size and left-shift back to the original position */
1077
64.7k
   energy /= len;
1078
64.7k
   energy = SHL32(energy, shift);
1079
1080
64.7k
   return energy;
1081
64.7k
}
1082
#else
1083
static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch)
1084
64.8k
{
1085
64.8k
   int len = frame_size*channels;
1086
64.8k
   return celt_inner_prod(pcm, pcm, len, arch)/len;
1087
64.8k
}
1088
#endif
1089
1090
/* Decides if DTX should be turned on (=1) or off (=0) */
1091
static int decide_dtx_mode(opus_int activity,            /* indicates if this frame contains speech/music */
1092
                           int *nb_no_activity_ms_Q1,    /* number of consecutive milliseconds with no activity, in Q1 */
1093
                           int frame_size_ms_Q1          /* number of milliseconds in this update, in Q1 */
1094
                           )
1095
1096
40.7k
{
1097
40.7k
   if (!activity)
1098
2.88k
   {
1099
      /* The number of consecutive DTX frames should be within the allowed bounds.
1100
         Note that the allowed bound is defined in the SILK headers and assumes 20 ms
1101
         frames. As this function can be called with any frame length, a conversion to
1102
         milliseconds is done before the comparisons. */
1103
2.88k
      (*nb_no_activity_ms_Q1) += frame_size_ms_Q1;
1104
2.88k
      if (*nb_no_activity_ms_Q1 > NB_SPEECH_FRAMES_BEFORE_DTX*20*2)
1105
0
      {
1106
0
         if (*nb_no_activity_ms_Q1 <= (NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX)*20*2)
1107
            /* Valid frame for DTX! */
1108
0
            return 1;
1109
0
         else
1110
0
            (*nb_no_activity_ms_Q1) = NB_SPEECH_FRAMES_BEFORE_DTX*20*2;
1111
0
      }
1112
2.88k
   } else
1113
37.9k
      (*nb_no_activity_ms_Q1) = 0;
1114
1115
40.7k
   return 0;
1116
40.7k
}
1117
1118
#endif
1119
1120
static int compute_redundancy_bytes(opus_int32 max_data_bytes, opus_int32 bitrate_bps, int frame_rate, int channels)
1121
0
{
1122
0
   int redundancy_bytes_cap;
1123
0
   int redundancy_bytes;
1124
0
   opus_int32 redundancy_rate;
1125
0
   int base_bits;
1126
0
   opus_int32 available_bits;
1127
0
   base_bits = (40*channels+20);
1128
1129
   /* Equivalent rate for 5 ms frames. */
1130
0
   redundancy_rate = bitrate_bps + base_bits*(200 - frame_rate);
1131
   /* For VBR, further increase the bitrate if we can afford it. It's pretty short
1132
      and we'll avoid artefacts. */
1133
0
   redundancy_rate = 3*redundancy_rate/2;
1134
0
   redundancy_bytes = redundancy_rate/1600;
1135
1136
   /* Compute the max rate we can use given CBR or VBR with cap. */
1137
0
   available_bits = max_data_bytes*8 - 2*base_bits;
1138
0
   redundancy_bytes_cap = (available_bits*240/(240+48000/frame_rate) + base_bits)/8;
1139
0
   redundancy_bytes = IMIN(redundancy_bytes, redundancy_bytes_cap);
1140
   /* It we can't get enough bits for redundancy to be worth it, rely on the decoder PLC. */
1141
0
   if (redundancy_bytes > 4 + 8*channels)
1142
0
      redundancy_bytes = IMIN(257, redundancy_bytes);
1143
0
   else
1144
0
      redundancy_bytes = 0;
1145
0
   return redundancy_bytes;
1146
0
}
1147
1148
static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1149
                unsigned char *data, opus_int32 max_data_bytes,
1150
                int float_api, int first_frame,
1151
#ifdef ENABLE_DRED
1152
                opus_int32 dred_bitrate_bps,
1153
#endif
1154
#ifndef DISABLE_FLOAT_API
1155
                AnalysisInfo *analysis_info, int is_silence,
1156
#endif
1157
                int redundancy, int celt_to_silk, int prefill,
1158
                opus_int32 equiv_rate, int to_celt);
1159
1160
opus_int32 opus_encode_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1161
                unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
1162
                const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
1163
                int analysis_channels, downmix_func downmix, int float_api)
1164
918k
{
1165
918k
    void *silk_enc=NULL;
1166
918k
    CELTEncoder *celt_enc=NULL;
1167
918k
    int i;
1168
918k
    int ret=0;
1169
918k
    int prefill=0;
1170
918k
    int redundancy = 0;
1171
918k
    int celt_to_silk = 0;
1172
918k
    int to_celt = 0;
1173
918k
    int voice_est; /* Probability of voice in Q7 */
1174
918k
    opus_int32 equiv_rate;
1175
918k
    int frame_rate;
1176
918k
    opus_int32 max_rate; /* Max bitrate we're allowed to use */
1177
918k
    int curr_bandwidth;
1178
918k
    opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1179
918k
    opus_int32 cbr_bytes=-1;
1180
918k
    opus_val16 stereo_width;
1181
918k
    const CELTMode *celt_mode=NULL;
1182
918k
    int packet_size_cap = 1276;
1183
918k
#ifndef DISABLE_FLOAT_API
1184
918k
    AnalysisInfo analysis_info;
1185
918k
    int analysis_read_pos_bak=-1;
1186
918k
    int analysis_read_subframe_bak=-1;
1187
918k
    int is_silence = 0;
1188
918k
#endif
1189
#ifdef ENABLE_DRED
1190
    opus_int32 dred_bitrate_bps;
1191
#endif
1192
918k
    ALLOC_STACK;
1193
1194
#ifdef ENABLE_QEXT
1195
475k
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1196
#endif
1197
1198
    /* Just avoid insane packet sizes here, but the real bounds are applied later on. */
1199
918k
    max_data_bytes = IMIN(packet_size_cap*6, out_data_bytes);
1200
1201
918k
    st->rangeFinal = 0;
1202
918k
    if (frame_size <= 0 || max_data_bytes <= 0)
1203
0
    {
1204
0
       RESTORE_STACK;
1205
0
       return OPUS_BAD_ARG;
1206
0
    }
1207
1208
    /* Cannot encode 100 ms in 1 byte */
1209
918k
    if (max_data_bytes==1 && st->Fs==(frame_size*10))
1210
0
    {
1211
0
      RESTORE_STACK;
1212
0
      return OPUS_BUFFER_TOO_SMALL;
1213
0
    }
1214
1215
918k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1216
918k
        silk_enc = (char*)st+st->silk_enc_offset;
1217
918k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1218
918k
        celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1219
1220
918k
    lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1221
1222
918k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1223
918k
        celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1224
918k
#ifndef DISABLE_FLOAT_API
1225
918k
    analysis_info.valid = 0;
1226
#ifdef FIXED_POINT
1227
459k
    if (st->silk_mode.complexity >= 10 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1228
#else
1229
459k
    if (st->silk_mode.complexity >= 7 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1230
101k
#endif
1231
203k
    {
1232
203k
       is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1233
203k
       analysis_read_pos_bak = st->analysis.read_pos;
1234
203k
       analysis_read_subframe_bak = st->analysis.read_subframe;
1235
203k
       run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1236
203k
             c1, c2, analysis_channels, st->Fs,
1237
203k
             lsb_depth, downmix, &analysis_info);
1238
1239
       /* Track the peak signal energy */
1240
203k
       if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1241
122k
          st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),
1242
203k
                compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1243
715k
    } else if (st->analysis.initialized) {
1244
0
       tonality_analysis_reset(&st->analysis);
1245
0
    }
1246
#else
1247
    (void)analysis_pcm;
1248
    (void)analysis_size;
1249
    (void)c1;
1250
    (void)c2;
1251
    (void)analysis_channels;
1252
    (void)downmix;
1253
#endif
1254
1255
918k
#ifndef DISABLE_FLOAT_API
1256
    /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1257
     * Otherwise, preserve voice_ratio from the last non-silent frame */
1258
918k
    if (!is_silence)
1259
906k
      st->voice_ratio = -1;
1260
1261
918k
    st->detected_bandwidth = 0;
1262
918k
    if (analysis_info.valid)
1263
124k
    {
1264
124k
       int analysis_bandwidth;
1265
124k
       if (st->signal_type == OPUS_AUTO)
1266
80.0k
       {
1267
80.0k
          float prob;
1268
80.0k
          if (st->prev_mode == 0)
1269
80.0k
             prob = analysis_info.music_prob;
1270
0
          else if (st->prev_mode == MODE_CELT_ONLY)
1271
0
             prob = analysis_info.music_prob_max;
1272
0
          else
1273
0
             prob = analysis_info.music_prob_min;
1274
80.0k
          st->voice_ratio = (int)floor(.5+100*(1-prob));
1275
80.0k
       }
1276
1277
124k
       analysis_bandwidth = analysis_info.bandwidth;
1278
124k
       if (analysis_bandwidth<=12)
1279
0
          st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1280
124k
       else if (analysis_bandwidth<=14)
1281
0
          st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1282
124k
       else if (analysis_bandwidth<=16)
1283
0
          st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1284
124k
       else if (analysis_bandwidth<=18)
1285
0
          st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1286
124k
       else
1287
124k
          st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1288
124k
    }
1289
#else
1290
    st->voice_ratio = -1;
1291
#endif
1292
1293
918k
    if (st->channels==2 && st->force_channels!=1)
1294
256k
       stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1295
661k
    else
1296
661k
       stereo_width = 0;
1297
918k
    st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1298
1299
918k
    frame_rate = st->Fs/frame_size;
1300
918k
    if (!st->use_vbr)
1301
363k
    {
1302
       /* Multiply by 12 to make sure the division is exact. */
1303
363k
       int frame_rate12 = 12*st->Fs/frame_size;
1304
       /* We need to make sure that "int" values always fit in 16 bits. */
1305
363k
       cbr_bytes = IMIN( (12*st->bitrate_bps/8 + frame_rate12/2)/frame_rate12, max_data_bytes);
1306
363k
       st->bitrate_bps = cbr_bytes*(opus_int32)frame_rate12*8/12;
1307
       /* Make sure we provide at least one byte to avoid failing. */
1308
363k
       max_data_bytes = IMAX(1, cbr_bytes);
1309
363k
    }
1310
#ifdef ENABLE_DRED
1311
    /* Allocate some of the bits to DRED if needed. */
1312
    dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size);
1313
    st->bitrate_bps -= dred_bitrate_bps;
1314
#endif
1315
918k
    if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1316
846k
       || (frame_rate<50 && (max_data_bytes*(opus_int32)frame_rate<300 || st->bitrate_bps < 2400)))
1317
107k
    {
1318
       /*If the space is too low to do something useful, emit 'PLC' frames.*/
1319
107k
       int tocmode = st->mode;
1320
107k
       int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1321
107k
       int packet_code = 0;
1322
107k
       int num_multiframes = 0;
1323
1324
107k
       if (tocmode==0)
1325
0
          tocmode = MODE_SILK_ONLY;
1326
107k
       if (frame_rate>100)
1327
19.5k
          tocmode = MODE_CELT_ONLY;
1328
       /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1329
107k
       if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1330
9.39k
       {
1331
9.39k
          frame_rate = 50;
1332
9.39k
          packet_code = 1;
1333
9.39k
       }
1334
1335
       /* >= 60 ms frames */
1336
107k
       if (frame_rate<=16)
1337
38.8k
       {
1338
          /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1339
38.8k
          if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1340
11.6k
          {
1341
11.6k
             tocmode = MODE_SILK_ONLY;
1342
1343
11.6k
             packet_code = frame_rate <= 12;
1344
11.6k
             frame_rate = frame_rate == 12 ? 25 : 16;
1345
11.6k
          }
1346
27.2k
          else
1347
27.2k
          {
1348
27.2k
             num_multiframes = 50/frame_rate;
1349
27.2k
             frame_rate = 50;
1350
27.2k
             packet_code = 3;
1351
27.2k
          }
1352
38.8k
       }
1353
1354
107k
       if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1355
11.6k
          bw=OPUS_BANDWIDTH_WIDEBAND;
1356
95.9k
       else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1357
0
          bw=OPUS_BANDWIDTH_NARROWBAND;
1358
95.9k
       else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1359
0
          bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1360
1361
107k
       data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1362
107k
       data[0] |= packet_code;
1363
1364
107k
       ret = packet_code <= 1 ? 1 : 2;
1365
1366
107k
       max_data_bytes = IMAX(max_data_bytes, ret);
1367
1368
107k
       if (packet_code==3)
1369
27.2k
          data[1] = num_multiframes;
1370
1371
107k
       if (!st->use_vbr)
1372
42.1k
       {
1373
42.1k
          ret = opus_packet_pad(data, ret, max_data_bytes);
1374
42.1k
          if (ret == OPUS_OK)
1375
42.1k
             ret = max_data_bytes;
1376
0
          else
1377
0
             ret = OPUS_INTERNAL_ERROR;
1378
42.1k
       }
1379
107k
       RESTORE_STACK;
1380
107k
       return ret;
1381
107k
    }
1382
810k
    max_rate = frame_rate*max_data_bytes*8;
1383
1384
    /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1385
810k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1386
810k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1387
1388
810k
    if (st->signal_type == OPUS_SIGNAL_VOICE)
1389
167k
       voice_est = 127;
1390
643k
    else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1391
131k
       voice_est = 0;
1392
511k
    else if (st->voice_ratio >= 0)
1393
51.5k
    {
1394
51.5k
       voice_est = st->voice_ratio*327>>8;
1395
       /* For AUDIO, never be more than 90% confident of having speech */
1396
51.5k
       if (st->application == OPUS_APPLICATION_AUDIO)
1397
29.2k
          voice_est = IMIN(voice_est, 115);
1398
460k
    } else if (st->application == OPUS_APPLICATION_VOIP)
1399
100k
       voice_est = 115;
1400
359k
    else
1401
359k
       voice_est = 48;
1402
1403
810k
    if (st->force_channels!=OPUS_AUTO && st->channels == 2)
1404
133k
    {
1405
133k
        st->stream_channels = st->force_channels;
1406
676k
    } else {
1407
#ifdef FUZZING
1408
        (void)stereo_music_threshold;
1409
        (void)stereo_voice_threshold;
1410
       /* Random mono/stereo decision */
1411
       if (st->channels == 2 && (rand()&0x1F)==0)
1412
          st->stream_channels = 3-st->stream_channels;
1413
#else
1414
       /* Rate-dependent mono-stereo decision */
1415
676k
       if (st->channels == 2)
1416
165k
       {
1417
165k
          opus_int32 stereo_threshold;
1418
165k
          stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1419
165k
          if (st->stream_channels == 2)
1420
165k
             stereo_threshold -= 1000;
1421
0
          else
1422
0
             stereo_threshold += 1000;
1423
165k
          st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1424
511k
       } else {
1425
511k
          st->stream_channels = st->channels;
1426
511k
       }
1427
676k
#endif
1428
676k
    }
1429
    /* Update equivalent rate for channels decision. */
1430
810k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1431
810k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1432
1433
    /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used,
1434
       e.g. because of the complexity setting or sample rate. */
1435
810k
#ifndef DISABLE_FLOAT_API
1436
810k
    st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1437
#else
1438
    st->silk_mode.useDTX = st->use_dtx;
1439
#endif
1440
1441
    /* Mode selection depending on application and signal type */
1442
810k
    if (st->application == OPUS_APPLICATION_FORCED_SILK)
1443
0
    {
1444
0
       st->mode = MODE_SILK_ONLY;
1445
810k
    } else if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1446
267k
    {
1447
267k
       st->mode = MODE_CELT_ONLY;
1448
543k
    } else if (st->user_forced_mode == OPUS_AUTO)
1449
506k
    {
1450
#ifdef FUZZING
1451
        (void)stereo_width;
1452
        (void)mode_thresholds;
1453
       /* Random mode switching */
1454
       if ((rand()&0xF)==0)
1455
       {
1456
          if ((rand()&0x1)==0)
1457
             st->mode = MODE_CELT_ONLY;
1458
          else
1459
             st->mode = MODE_SILK_ONLY;
1460
       } else {
1461
          if (st->prev_mode==MODE_CELT_ONLY)
1462
             st->mode = MODE_CELT_ONLY;
1463
          else
1464
             st->mode = MODE_SILK_ONLY;
1465
       }
1466
#else
1467
506k
       opus_int32 mode_voice, mode_music;
1468
506k
       opus_int32 threshold;
1469
1470
       /* Interpolate based on stereo width */
1471
506k
       mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1472
506k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1473
506k
       mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1474
506k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1475
       /* Interpolate based on speech/music probability */
1476
506k
       threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1477
       /* Bias towards SILK for VoIP because of some useful features */
1478
506k
       if (st->application == OPUS_APPLICATION_VOIP)
1479
179k
          threshold += 8000;
1480
1481
       /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1482
       /* Hysteresis */
1483
506k
       if (st->prev_mode == MODE_CELT_ONLY)
1484
0
           threshold -= 4000;
1485
506k
       else if (st->prev_mode>0)
1486
0
           threshold += 4000;
1487
1488
506k
       st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1489
1490
       /* When FEC is enabled and there's enough packet loss, use SILK.
1491
          Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */
1492
506k
       if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25))
1493
253k
          st->mode = MODE_SILK_ONLY;
1494
       /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1495
          use SILK in order to make use of its DTX. */
1496
506k
       if (st->silk_mode.useDTX && voice_est > 100)
1497
92.4k
          st->mode = MODE_SILK_ONLY;
1498
506k
#endif
1499
1500
       /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1501
506k
       if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1502
4.71k
          st->mode = MODE_CELT_ONLY;
1503
506k
    } else {
1504
36.4k
       st->mode = st->user_forced_mode;
1505
36.4k
    }
1506
1507
    /* Override the chosen mode to make sure we meet the requested frame size */
1508
810k
    if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1509
64.1k
       st->mode = MODE_CELT_ONLY;
1510
810k
    if (st->lfe)
1511
1.47k
       st->mode = MODE_CELT_ONLY;
1512
1513
810k
    if (st->prev_mode > 0 &&
1514
0
        ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1515
0
    (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1516
0
    {
1517
0
        redundancy = 1;
1518
0
        celt_to_silk = (st->mode != MODE_CELT_ONLY);
1519
0
        if (!celt_to_silk)
1520
0
        {
1521
            /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1522
0
            if (frame_size >= st->Fs/100)
1523
0
            {
1524
0
                st->mode = st->prev_mode;
1525
0
                to_celt = 1;
1526
0
            } else {
1527
0
                redundancy=0;
1528
0
            }
1529
0
        }
1530
0
    }
1531
1532
    /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1533
     * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1534
810k
    if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1535
0
          && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1536
0
    {
1537
       /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1538
0
       st->silk_mode.toMono = 1;
1539
0
       st->stream_channels = 2;
1540
810k
    } else {
1541
810k
       st->silk_mode.toMono = 0;
1542
810k
    }
1543
1544
    /* Update equivalent rate with mode decision. */
1545
810k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1546
810k
          st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1547
1548
810k
    if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1549
0
    {
1550
0
        silk_EncControlStruct dummy;
1551
0
        silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy);
1552
0
        prefill=1;
1553
0
    }
1554
1555
    /* Automatic (rate-dependent) bandwidth selection */
1556
810k
    if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1557
810k
    {
1558
810k
        const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1559
810k
        opus_int32 bandwidth_thresholds[8];
1560
810k
        int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1561
1562
810k
        if (st->channels==2 && st->force_channels!=1)
1563
244k
        {
1564
244k
           voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1565
244k
           music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1566
565k
        } else {
1567
565k
           voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1568
565k
           music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1569
565k
        }
1570
        /* Interpolate bandwidth thresholds depending on voice estimation */
1571
7.29M
        for (i=0;i<8;i++)
1572
6.48M
        {
1573
6.48M
           bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1574
6.48M
                    + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1575
6.48M
        }
1576
973k
        do {
1577
973k
            int threshold, hysteresis;
1578
973k
            threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1579
973k
            hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1580
973k
            if (!st->first)
1581
0
            {
1582
0
                if (st->auto_bandwidth >= bandwidth)
1583
0
                    threshold -= hysteresis;
1584
0
                else
1585
0
                    threshold += hysteresis;
1586
0
            }
1587
973k
            if (equiv_rate >= threshold)
1588
801k
                break;
1589
973k
        } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1590
        /* We don't use mediumband anymore, except when explicitly requested or during
1591
           mode transitions. */
1592
810k
        if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1593
0
           bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1594
810k
        st->bandwidth = st->auto_bandwidth = bandwidth;
1595
        /* Prevents any transition to SWB/FB until the SILK layer has fully
1596
           switched to WB mode and turned the variable LP filter off */
1597
810k
        if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1598
0
            st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1599
810k
    }
1600
1601
810k
    if (st->bandwidth>st->max_bandwidth)
1602
589k
       st->bandwidth = st->max_bandwidth;
1603
1604
810k
    if (st->user_bandwidth != OPUS_AUTO)
1605
15.6k
        st->bandwidth = st->user_bandwidth;
1606
1607
    /* This prevents us from using hybrid at unsafe CBR/max rates */
1608
810k
    if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1609
9.00k
    {
1610
9.00k
       st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1611
9.00k
    }
1612
1613
    /* Prevents Opus from wasting bits on frequencies that are above
1614
       the Nyquist rate of the input signal */
1615
810k
    if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1616
148k
        st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1617
810k
    if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1618
202k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1619
810k
    if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1620
245k
        st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1621
810k
    if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1622
251k
        st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1623
810k
#ifndef DISABLE_FLOAT_API
1624
    /* Use detected bandwidth to reduce the encoded bandwidth. */
1625
810k
    if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1626
78.2k
    {
1627
78.2k
       int min_detected_bandwidth;
1628
       /* Makes bandwidth detection more conservative just in case the detector
1629
          gets it wrong when we could have coded a high bandwidth transparently.
1630
          When operating in SILK/hybrid mode, we don't go below wideband to avoid
1631
          more complicated switches that require redundancy. */
1632
78.2k
       if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1633
632
          min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1634
77.6k
       else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1635
182
          min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1636
77.4k
       else if (equiv_rate <= 30000*st->stream_channels)
1637
54.5k
          min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1638
22.9k
       else if (equiv_rate <= 44000*st->stream_channels)
1639
10.0k
          min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1640
12.8k
       else
1641
12.8k
          min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1642
1643
78.2k
       st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1644
78.2k
       st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1645
78.2k
    }
1646
810k
#endif
1647
810k
    st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1648
810k
          st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1649
810k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1650
810k
        celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1651
1652
    /* CELT mode doesn't support mediumband, use wideband instead */
1653
810k
    if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1654
49.5k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1655
810k
    if (st->lfe)
1656
1.47k
       st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1657
1658
810k
    curr_bandwidth = st->bandwidth;
1659
1660
810k
    if (st->application == OPUS_APPLICATION_FORCED_SILK && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1661
0
       st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1662
    /* Chooses the appropriate mode for speech
1663
       *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1664
810k
    if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1665
23.7k
        st->mode = MODE_HYBRID;
1666
810k
    if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1667
0
        st->mode = MODE_SILK_ONLY;
1668
1669
    /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1670
810k
    if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1671
135k
    {
1672
135k
       int enc_frame_size;
1673
135k
       int nb_frames;
1674
135k
       VARDECL(unsigned char, tmp_data);
1675
135k
       VARDECL(OpusRepacketizer, rp);
1676
135k
       int max_header_bytes;
1677
135k
       opus_int32 repacketize_len;
1678
135k
       opus_int32 max_len_sum;
1679
135k
       opus_int32 tot_size=0;
1680
135k
       unsigned char *curr_data;
1681
135k
       int tmp_len;
1682
135k
       int dtx_count = 0;
1683
135k
       int bak_to_mono;
1684
1685
135k
       if (st->mode == MODE_SILK_ONLY)
1686
68.0k
       {
1687
68.0k
         if (frame_size == 2*st->Fs/25)  /* 80 ms -> 2x 40 ms */
1688
19.6k
           enc_frame_size = st->Fs/25;
1689
48.3k
         else if (frame_size == 3*st->Fs/25)  /* 120 ms -> 2x 60 ms */
1690
17.9k
           enc_frame_size = 3*st->Fs/50;
1691
30.3k
         else                            /* 100 ms -> 5x 20 ms */
1692
30.3k
           enc_frame_size = st->Fs/50;
1693
68.0k
       }
1694
66.9k
       else
1695
66.9k
         enc_frame_size = st->Fs/50;
1696
1697
135k
       nb_frames = frame_size/enc_frame_size;
1698
1699
135k
#ifndef DISABLE_FLOAT_API
1700
135k
       if (analysis_read_pos_bak!= -1)
1701
40.4k
       {
1702
          /* Reset analysis position to the beginning of the first frame so we
1703
             can use it one frame at a time. */
1704
40.4k
          st->analysis.read_pos = analysis_read_pos_bak;
1705
40.4k
          st->analysis.read_subframe = analysis_read_subframe_bak;
1706
40.4k
       }
1707
135k
#endif
1708
1709
       /* Worst cases:
1710
        * 2 frames: Code 2 with different compressed sizes
1711
        * >2 frames: Code 3 VBR */
1712
135k
       max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
1713
#ifdef ENABLE_QEXT
1714
       /* Cover the use of the separators that are the only thing that can get us over
1715
          once we consider that we need to subtract the extension overhead in each
1716
          of the individual frames. Also consider that a separator can get our padding
1717
          from 254 to 255, which costs an extra length byte (at most once). */
1718
68.5k
       if (st->enable_qext) max_header_bytes += (nb_frames-1) + 1;
1719
#endif
1720
1721
135k
       if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
1722
86.6k
          repacketize_len = out_data_bytes;
1723
48.3k
       else {
1724
48.3k
          celt_assert(cbr_bytes>=0);
1725
48.3k
          repacketize_len = IMIN(cbr_bytes, out_data_bytes);
1726
48.3k
       }
1727
135k
       max_len_sum = nb_frames + repacketize_len - max_header_bytes;
1728
1729
135k
       ALLOC(tmp_data, max_len_sum, unsigned char);
1730
135k
       curr_data = tmp_data;
1731
135k
       ALLOC(rp, 1, OpusRepacketizer);
1732
135k
       opus_repacketizer_init(rp);
1733
1734
1735
135k
       bak_to_mono = st->silk_mode.toMono;
1736
135k
       if (bak_to_mono)
1737
0
          st->force_channels = 1;
1738
135k
       else
1739
135k
          st->prev_channels = st->stream_channels;
1740
1741
617k
       for (i=0;i<nb_frames;i++)
1742
482k
       {
1743
482k
          int first_frame;
1744
482k
          int frame_to_celt;
1745
482k
          int frame_redundancy;
1746
482k
          opus_int32 curr_max;
1747
          /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh,
1748
             that allows for DRED not to be in the first frame. */
1749
482k
          first_frame = (i == 0) || (i == dtx_count);
1750
482k
          st->silk_mode.toMono = 0;
1751
482k
          st->nonfinal_frame = i<(nb_frames-1);
1752
1753
          /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
1754
482k
          frame_to_celt = to_celt && i==nb_frames-1;
1755
482k
          frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0));
1756
1757
482k
          curr_max = IMIN(3*st->bitrate_bps/(3*8*st->Fs/enc_frame_size), max_len_sum/nb_frames);
1758
#ifdef ENABLE_DRED
1759
          curr_max = IMIN(curr_max, (max_len_sum-3*dred_bitrate_bps/(3*8*st->Fs/frame_size))/nb_frames);
1760
          if (first_frame) curr_max += 3*dred_bitrate_bps/(3*8*st->Fs/frame_size);
1761
#endif
1762
482k
          curr_max = IMIN(max_len_sum-tot_size, curr_max);
1763
482k
#ifndef DISABLE_FLOAT_API
1764
482k
          if (analysis_read_pos_bak != -1) {
1765
147k
            is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1766
            /* Get analysis for current frame. */
1767
147k
            tonality_get_info(&st->analysis, &analysis_info, enc_frame_size);
1768
147k
          }
1769
482k
#endif
1770
1771
482k
          tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame,
1772
#ifdef ENABLE_DRED
1773
          dred_bitrate_bps,
1774
#endif
1775
482k
#ifndef DISABLE_FLOAT_API
1776
482k
          &analysis_info,
1777
482k
          is_silence,
1778
482k
#endif
1779
482k
                    frame_redundancy, celt_to_silk, prefill,
1780
482k
                    equiv_rate, frame_to_celt
1781
482k
              );
1782
482k
          if (tmp_len<0)
1783
0
          {
1784
0
             RESTORE_STACK;
1785
0
             return OPUS_INTERNAL_ERROR;
1786
482k
          } else if (tmp_len==1) {
1787
0
             dtx_count++;
1788
0
          }
1789
482k
          ret = opus_repacketizer_cat(rp, curr_data, tmp_len);
1790
1791
482k
          if (ret<0)
1792
0
          {
1793
0
             RESTORE_STACK;
1794
0
             return OPUS_INTERNAL_ERROR;
1795
0
          }
1796
482k
          tot_size += tmp_len;
1797
482k
          curr_data += tmp_len;
1798
482k
       }
1799
135k
       ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL, 0);
1800
135k
       if (ret<0)
1801
0
       {
1802
0
          ret = OPUS_INTERNAL_ERROR;
1803
0
       }
1804
135k
       st->silk_mode.toMono = bak_to_mono;
1805
135k
       RESTORE_STACK;
1806
135k
       return ret;
1807
675k
    } else {
1808
675k
      ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1,
1809
#ifdef ENABLE_DRED
1810
                dred_bitrate_bps,
1811
#endif
1812
675k
#ifndef DISABLE_FLOAT_API
1813
675k
                &analysis_info,
1814
675k
                is_silence,
1815
675k
#endif
1816
675k
                redundancy, celt_to_silk, prefill,
1817
675k
                equiv_rate, to_celt
1818
675k
          );
1819
675k
      RESTORE_STACK;
1820
675k
      return ret;
1821
675k
    }
1822
810k
}
opus_encode_native
Line
Count
Source
1164
237k
{
1165
237k
    void *silk_enc=NULL;
1166
237k
    CELTEncoder *celt_enc=NULL;
1167
237k
    int i;
1168
237k
    int ret=0;
1169
237k
    int prefill=0;
1170
237k
    int redundancy = 0;
1171
237k
    int celt_to_silk = 0;
1172
237k
    int to_celt = 0;
1173
237k
    int voice_est; /* Probability of voice in Q7 */
1174
237k
    opus_int32 equiv_rate;
1175
237k
    int frame_rate;
1176
237k
    opus_int32 max_rate; /* Max bitrate we're allowed to use */
1177
237k
    int curr_bandwidth;
1178
237k
    opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1179
237k
    opus_int32 cbr_bytes=-1;
1180
237k
    opus_val16 stereo_width;
1181
237k
    const CELTMode *celt_mode=NULL;
1182
237k
    int packet_size_cap = 1276;
1183
237k
#ifndef DISABLE_FLOAT_API
1184
237k
    AnalysisInfo analysis_info;
1185
237k
    int analysis_read_pos_bak=-1;
1186
237k
    int analysis_read_subframe_bak=-1;
1187
237k
    int is_silence = 0;
1188
237k
#endif
1189
#ifdef ENABLE_DRED
1190
    opus_int32 dred_bitrate_bps;
1191
#endif
1192
237k
    ALLOC_STACK;
1193
1194
237k
#ifdef ENABLE_QEXT
1195
237k
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1196
237k
#endif
1197
1198
    /* Just avoid insane packet sizes here, but the real bounds are applied later on. */
1199
237k
    max_data_bytes = IMIN(packet_size_cap*6, out_data_bytes);
1200
1201
237k
    st->rangeFinal = 0;
1202
237k
    if (frame_size <= 0 || max_data_bytes <= 0)
1203
0
    {
1204
0
       RESTORE_STACK;
1205
0
       return OPUS_BAD_ARG;
1206
0
    }
1207
1208
    /* Cannot encode 100 ms in 1 byte */
1209
237k
    if (max_data_bytes==1 && st->Fs==(frame_size*10))
1210
0
    {
1211
0
      RESTORE_STACK;
1212
0
      return OPUS_BUFFER_TOO_SMALL;
1213
0
    }
1214
1215
237k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1216
237k
        silk_enc = (char*)st+st->silk_enc_offset;
1217
237k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1218
237k
        celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1219
1220
237k
    lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1221
1222
237k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1223
237k
        celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1224
237k
#ifndef DISABLE_FLOAT_API
1225
237k
    analysis_info.valid = 0;
1226
237k
#ifdef FIXED_POINT
1227
237k
    if (st->silk_mode.complexity >= 10 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1228
#else
1229
    if (st->silk_mode.complexity >= 7 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1230
#endif
1231
50.5k
    {
1232
50.5k
       is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1233
50.5k
       analysis_read_pos_bak = st->analysis.read_pos;
1234
50.5k
       analysis_read_subframe_bak = st->analysis.read_subframe;
1235
50.5k
       run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1236
50.5k
             c1, c2, analysis_channels, st->Fs,
1237
50.5k
             lsb_depth, downmix, &analysis_info);
1238
1239
       /* Track the peak signal energy */
1240
50.5k
       if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1241
30.3k
          st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),
1242
50.5k
                compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1243
187k
    } else if (st->analysis.initialized) {
1244
0
       tonality_analysis_reset(&st->analysis);
1245
0
    }
1246
#else
1247
    (void)analysis_pcm;
1248
    (void)analysis_size;
1249
    (void)c1;
1250
    (void)c2;
1251
    (void)analysis_channels;
1252
    (void)downmix;
1253
#endif
1254
1255
237k
#ifndef DISABLE_FLOAT_API
1256
    /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1257
     * Otherwise, preserve voice_ratio from the last non-silent frame */
1258
237k
    if (!is_silence)
1259
235k
      st->voice_ratio = -1;
1260
1261
237k
    st->detected_bandwidth = 0;
1262
237k
    if (analysis_info.valid)
1263
30.9k
    {
1264
30.9k
       int analysis_bandwidth;
1265
30.9k
       if (st->signal_type == OPUS_AUTO)
1266
18.1k
       {
1267
18.1k
          float prob;
1268
18.1k
          if (st->prev_mode == 0)
1269
18.1k
             prob = analysis_info.music_prob;
1270
0
          else if (st->prev_mode == MODE_CELT_ONLY)
1271
0
             prob = analysis_info.music_prob_max;
1272
0
          else
1273
0
             prob = analysis_info.music_prob_min;
1274
18.1k
          st->voice_ratio = (int)floor(.5+100*(1-prob));
1275
18.1k
       }
1276
1277
30.9k
       analysis_bandwidth = analysis_info.bandwidth;
1278
30.9k
       if (analysis_bandwidth<=12)
1279
0
          st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1280
30.9k
       else if (analysis_bandwidth<=14)
1281
0
          st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1282
30.9k
       else if (analysis_bandwidth<=16)
1283
0
          st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1284
30.9k
       else if (analysis_bandwidth<=18)
1285
0
          st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1286
30.9k
       else
1287
30.9k
          st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1288
30.9k
    }
1289
#else
1290
    st->voice_ratio = -1;
1291
#endif
1292
1293
237k
    if (st->channels==2 && st->force_channels!=1)
1294
64.9k
       stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1295
172k
    else
1296
172k
       stereo_width = 0;
1297
237k
    st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1298
1299
237k
    frame_rate = st->Fs/frame_size;
1300
237k
    if (!st->use_vbr)
1301
98.5k
    {
1302
       /* Multiply by 12 to make sure the division is exact. */
1303
98.5k
       int frame_rate12 = 12*st->Fs/frame_size;
1304
       /* We need to make sure that "int" values always fit in 16 bits. */
1305
98.5k
       cbr_bytes = IMIN( (12*st->bitrate_bps/8 + frame_rate12/2)/frame_rate12, max_data_bytes);
1306
98.5k
       st->bitrate_bps = cbr_bytes*(opus_int32)frame_rate12*8/12;
1307
       /* Make sure we provide at least one byte to avoid failing. */
1308
98.5k
       max_data_bytes = IMAX(1, cbr_bytes);
1309
98.5k
    }
1310
#ifdef ENABLE_DRED
1311
    /* Allocate some of the bits to DRED if needed. */
1312
    dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size);
1313
    st->bitrate_bps -= dred_bitrate_bps;
1314
#endif
1315
237k
    if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1316
217k
       || (frame_rate<50 && (max_data_bytes*(opus_int32)frame_rate<300 || st->bitrate_bps < 2400)))
1317
29.2k
    {
1318
       /*If the space is too low to do something useful, emit 'PLC' frames.*/
1319
29.2k
       int tocmode = st->mode;
1320
29.2k
       int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1321
29.2k
       int packet_code = 0;
1322
29.2k
       int num_multiframes = 0;
1323
1324
29.2k
       if (tocmode==0)
1325
0
          tocmode = MODE_SILK_ONLY;
1326
29.2k
       if (frame_rate>100)
1327
5.21k
          tocmode = MODE_CELT_ONLY;
1328
       /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1329
29.2k
       if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1330
2.55k
       {
1331
2.55k
          frame_rate = 50;
1332
2.55k
          packet_code = 1;
1333
2.55k
       }
1334
1335
       /* >= 60 ms frames */
1336
29.2k
       if (frame_rate<=16)
1337
10.1k
       {
1338
          /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1339
10.1k
          if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1340
3.32k
          {
1341
3.32k
             tocmode = MODE_SILK_ONLY;
1342
1343
3.32k
             packet_code = frame_rate <= 12;
1344
3.32k
             frame_rate = frame_rate == 12 ? 25 : 16;
1345
3.32k
          }
1346
6.83k
          else
1347
6.83k
          {
1348
6.83k
             num_multiframes = 50/frame_rate;
1349
6.83k
             frame_rate = 50;
1350
6.83k
             packet_code = 3;
1351
6.83k
          }
1352
10.1k
       }
1353
1354
29.2k
       if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1355
3.32k
          bw=OPUS_BANDWIDTH_WIDEBAND;
1356
25.9k
       else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1357
0
          bw=OPUS_BANDWIDTH_NARROWBAND;
1358
25.9k
       else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1359
0
          bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1360
1361
29.2k
       data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1362
29.2k
       data[0] |= packet_code;
1363
1364
29.2k
       ret = packet_code <= 1 ? 1 : 2;
1365
1366
29.2k
       max_data_bytes = IMAX(max_data_bytes, ret);
1367
1368
29.2k
       if (packet_code==3)
1369
6.83k
          data[1] = num_multiframes;
1370
1371
29.2k
       if (!st->use_vbr)
1372
13.3k
       {
1373
13.3k
          ret = opus_packet_pad(data, ret, max_data_bytes);
1374
13.3k
          if (ret == OPUS_OK)
1375
13.3k
             ret = max_data_bytes;
1376
0
          else
1377
0
             ret = OPUS_INTERNAL_ERROR;
1378
13.3k
       }
1379
29.2k
       RESTORE_STACK;
1380
29.2k
       return ret;
1381
29.2k
    }
1382
208k
    max_rate = frame_rate*max_data_bytes*8;
1383
1384
    /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1385
208k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1386
208k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1387
1388
208k
    if (st->signal_type == OPUS_SIGNAL_VOICE)
1389
48.1k
       voice_est = 127;
1390
160k
    else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1391
36.8k
       voice_est = 0;
1392
123k
    else if (st->voice_ratio >= 0)
1393
11.4k
    {
1394
11.4k
       voice_est = st->voice_ratio*327>>8;
1395
       /* For AUDIO, never be more than 90% confident of having speech */
1396
11.4k
       if (st->application == OPUS_APPLICATION_AUDIO)
1397
6.26k
          voice_est = IMIN(voice_est, 115);
1398
112k
    } else if (st->application == OPUS_APPLICATION_VOIP)
1399
23.6k
       voice_est = 115;
1400
88.5k
    else
1401
88.5k
       voice_est = 48;
1402
1403
208k
    if (st->force_channels!=OPUS_AUTO && st->channels == 2)
1404
34.3k
    {
1405
34.3k
        st->stream_channels = st->force_channels;
1406
174k
    } else {
1407
#ifdef FUZZING
1408
        (void)stereo_music_threshold;
1409
        (void)stereo_voice_threshold;
1410
       /* Random mono/stereo decision */
1411
       if (st->channels == 2 && (rand()&0x1F)==0)
1412
          st->stream_channels = 3-st->stream_channels;
1413
#else
1414
       /* Rate-dependent mono-stereo decision */
1415
174k
       if (st->channels == 2)
1416
40.9k
       {
1417
40.9k
          opus_int32 stereo_threshold;
1418
40.9k
          stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1419
40.9k
          if (st->stream_channels == 2)
1420
40.9k
             stereo_threshold -= 1000;
1421
0
          else
1422
0
             stereo_threshold += 1000;
1423
40.9k
          st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1424
133k
       } else {
1425
133k
          st->stream_channels = st->channels;
1426
133k
       }
1427
174k
#endif
1428
174k
    }
1429
    /* Update equivalent rate for channels decision. */
1430
208k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1431
208k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1432
1433
    /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used,
1434
       e.g. because of the complexity setting or sample rate. */
1435
208k
#ifndef DISABLE_FLOAT_API
1436
208k
    st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1437
#else
1438
    st->silk_mode.useDTX = st->use_dtx;
1439
#endif
1440
1441
    /* Mode selection depending on application and signal type */
1442
208k
    if (st->application == OPUS_APPLICATION_FORCED_SILK)
1443
0
    {
1444
0
       st->mode = MODE_SILK_ONLY;
1445
208k
    } else if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1446
69.4k
    {
1447
69.4k
       st->mode = MODE_CELT_ONLY;
1448
139k
    } else if (st->user_forced_mode == OPUS_AUTO)
1449
129k
    {
1450
#ifdef FUZZING
1451
        (void)stereo_width;
1452
        (void)mode_thresholds;
1453
       /* Random mode switching */
1454
       if ((rand()&0xF)==0)
1455
       {
1456
          if ((rand()&0x1)==0)
1457
             st->mode = MODE_CELT_ONLY;
1458
          else
1459
             st->mode = MODE_SILK_ONLY;
1460
       } else {
1461
          if (st->prev_mode==MODE_CELT_ONLY)
1462
             st->mode = MODE_CELT_ONLY;
1463
          else
1464
             st->mode = MODE_SILK_ONLY;
1465
       }
1466
#else
1467
129k
       opus_int32 mode_voice, mode_music;
1468
129k
       opus_int32 threshold;
1469
1470
       /* Interpolate based on stereo width */
1471
129k
       mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1472
129k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1473
129k
       mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1474
129k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1475
       /* Interpolate based on speech/music probability */
1476
129k
       threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1477
       /* Bias towards SILK for VoIP because of some useful features */
1478
129k
       if (st->application == OPUS_APPLICATION_VOIP)
1479
47.0k
          threshold += 8000;
1480
1481
       /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1482
       /* Hysteresis */
1483
129k
       if (st->prev_mode == MODE_CELT_ONLY)
1484
0
           threshold -= 4000;
1485
129k
       else if (st->prev_mode>0)
1486
0
           threshold += 4000;
1487
1488
129k
       st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1489
1490
       /* When FEC is enabled and there's enough packet loss, use SILK.
1491
          Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */
1492
129k
       if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25))
1493
66.9k
          st->mode = MODE_SILK_ONLY;
1494
       /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1495
          use SILK in order to make use of its DTX. */
1496
129k
       if (st->silk_mode.useDTX && voice_est > 100)
1497
26.2k
          st->mode = MODE_SILK_ONLY;
1498
129k
#endif
1499
1500
       /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1501
129k
       if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1502
1.28k
          st->mode = MODE_CELT_ONLY;
1503
129k
    } else {
1504
9.37k
       st->mode = st->user_forced_mode;
1505
9.37k
    }
1506
1507
    /* Override the chosen mode to make sure we meet the requested frame size */
1508
208k
    if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1509
16.0k
       st->mode = MODE_CELT_ONLY;
1510
208k
    if (st->lfe)
1511
409
       st->mode = MODE_CELT_ONLY;
1512
1513
208k
    if (st->prev_mode > 0 &&
1514
0
        ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1515
0
    (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1516
0
    {
1517
0
        redundancy = 1;
1518
0
        celt_to_silk = (st->mode != MODE_CELT_ONLY);
1519
0
        if (!celt_to_silk)
1520
0
        {
1521
            /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1522
0
            if (frame_size >= st->Fs/100)
1523
0
            {
1524
0
                st->mode = st->prev_mode;
1525
0
                to_celt = 1;
1526
0
            } else {
1527
0
                redundancy=0;
1528
0
            }
1529
0
        }
1530
0
    }
1531
1532
    /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1533
     * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1534
208k
    if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1535
0
          && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1536
0
    {
1537
       /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1538
0
       st->silk_mode.toMono = 1;
1539
0
       st->stream_channels = 2;
1540
208k
    } else {
1541
208k
       st->silk_mode.toMono = 0;
1542
208k
    }
1543
1544
    /* Update equivalent rate with mode decision. */
1545
208k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1546
208k
          st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1547
1548
208k
    if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1549
0
    {
1550
0
        silk_EncControlStruct dummy;
1551
0
        silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy);
1552
0
        prefill=1;
1553
0
    }
1554
1555
    /* Automatic (rate-dependent) bandwidth selection */
1556
208k
    if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1557
208k
    {
1558
208k
        const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1559
208k
        opus_int32 bandwidth_thresholds[8];
1560
208k
        int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1561
1562
208k
        if (st->channels==2 && st->force_channels!=1)
1563
61.6k
        {
1564
61.6k
           voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1565
61.6k
           music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1566
147k
        } else {
1567
147k
           voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1568
147k
           music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1569
147k
        }
1570
        /* Interpolate bandwidth thresholds depending on voice estimation */
1571
1.87M
        for (i=0;i<8;i++)
1572
1.66M
        {
1573
1.66M
           bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1574
1.66M
                    + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1575
1.66M
        }
1576
252k
        do {
1577
252k
            int threshold, hysteresis;
1578
252k
            threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1579
252k
            hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1580
252k
            if (!st->first)
1581
0
            {
1582
0
                if (st->auto_bandwidth >= bandwidth)
1583
0
                    threshold -= hysteresis;
1584
0
                else
1585
0
                    threshold += hysteresis;
1586
0
            }
1587
252k
            if (equiv_rate >= threshold)
1588
206k
                break;
1589
252k
        } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1590
        /* We don't use mediumband anymore, except when explicitly requested or during
1591
           mode transitions. */
1592
208k
        if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1593
0
           bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1594
208k
        st->bandwidth = st->auto_bandwidth = bandwidth;
1595
        /* Prevents any transition to SWB/FB until the SILK layer has fully
1596
           switched to WB mode and turned the variable LP filter off */
1597
208k
        if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1598
0
            st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1599
208k
    }
1600
1601
208k
    if (st->bandwidth>st->max_bandwidth)
1602
149k
       st->bandwidth = st->max_bandwidth;
1603
1604
208k
    if (st->user_bandwidth != OPUS_AUTO)
1605
4.35k
        st->bandwidth = st->user_bandwidth;
1606
1607
    /* This prevents us from using hybrid at unsafe CBR/max rates */
1608
208k
    if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1609
2.45k
    {
1610
2.45k
       st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1611
2.45k
    }
1612
1613
    /* Prevents Opus from wasting bits on frequencies that are above
1614
       the Nyquist rate of the input signal */
1615
208k
    if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1616
37.2k
        st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1617
208k
    if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1618
53.2k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1619
208k
    if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1620
67.9k
        st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1621
208k
    if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1622
70.7k
        st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1623
208k
#ifndef DISABLE_FLOAT_API
1624
    /* Use detected bandwidth to reduce the encoded bandwidth. */
1625
208k
    if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1626
18.7k
    {
1627
18.7k
       int min_detected_bandwidth;
1628
       /* Makes bandwidth detection more conservative just in case the detector
1629
          gets it wrong when we could have coded a high bandwidth transparently.
1630
          When operating in SILK/hybrid mode, we don't go below wideband to avoid
1631
          more complicated switches that require redundancy. */
1632
18.7k
       if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1633
165
          min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1634
18.5k
       else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1635
60
          min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1636
18.4k
       else if (equiv_rate <= 30000*st->stream_channels)
1637
13.5k
          min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1638
4.95k
       else if (equiv_rate <= 44000*st->stream_channels)
1639
2.34k
          min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1640
2.61k
       else
1641
2.61k
          min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1642
1643
18.7k
       st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1644
18.7k
       st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1645
18.7k
    }
1646
208k
#endif
1647
208k
    st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1648
208k
          st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1649
208k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1650
208k
        celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1651
1652
    /* CELT mode doesn't support mediumband, use wideband instead */
1653
208k
    if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1654
13.1k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1655
208k
    if (st->lfe)
1656
409
       st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1657
1658
208k
    curr_bandwidth = st->bandwidth;
1659
1660
208k
    if (st->application == OPUS_APPLICATION_FORCED_SILK && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1661
0
       st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1662
    /* Chooses the appropriate mode for speech
1663
       *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1664
208k
    if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1665
6.33k
        st->mode = MODE_HYBRID;
1666
208k
    if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1667
0
        st->mode = MODE_SILK_ONLY;
1668
1669
    /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1670
208k
    if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1671
34.2k
    {
1672
34.2k
       int enc_frame_size;
1673
34.2k
       int nb_frames;
1674
34.2k
       VARDECL(unsigned char, tmp_data);
1675
34.2k
       VARDECL(OpusRepacketizer, rp);
1676
34.2k
       int max_header_bytes;
1677
34.2k
       opus_int32 repacketize_len;
1678
34.2k
       opus_int32 max_len_sum;
1679
34.2k
       opus_int32 tot_size=0;
1680
34.2k
       unsigned char *curr_data;
1681
34.2k
       int tmp_len;
1682
34.2k
       int dtx_count = 0;
1683
34.2k
       int bak_to_mono;
1684
1685
34.2k
       if (st->mode == MODE_SILK_ONLY)
1686
17.5k
       {
1687
17.5k
         if (frame_size == 2*st->Fs/25)  /* 80 ms -> 2x 40 ms */
1688
4.96k
           enc_frame_size = st->Fs/25;
1689
12.5k
         else if (frame_size == 3*st->Fs/25)  /* 120 ms -> 2x 60 ms */
1690
4.85k
           enc_frame_size = 3*st->Fs/50;
1691
7.73k
         else                            /* 100 ms -> 5x 20 ms */
1692
7.73k
           enc_frame_size = st->Fs/50;
1693
17.5k
       }
1694
16.7k
       else
1695
16.7k
         enc_frame_size = st->Fs/50;
1696
1697
34.2k
       nb_frames = frame_size/enc_frame_size;
1698
1699
34.2k
#ifndef DISABLE_FLOAT_API
1700
34.2k
       if (analysis_read_pos_bak!= -1)
1701
10.1k
       {
1702
          /* Reset analysis position to the beginning of the first frame so we
1703
             can use it one frame at a time. */
1704
10.1k
          st->analysis.read_pos = analysis_read_pos_bak;
1705
10.1k
          st->analysis.read_subframe = analysis_read_subframe_bak;
1706
10.1k
       }
1707
34.2k
#endif
1708
1709
       /* Worst cases:
1710
        * 2 frames: Code 2 with different compressed sizes
1711
        * >2 frames: Code 3 VBR */
1712
34.2k
       max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
1713
34.2k
#ifdef ENABLE_QEXT
1714
       /* Cover the use of the separators that are the only thing that can get us over
1715
          once we consider that we need to subtract the extension overhead in each
1716
          of the individual frames. Also consider that a separator can get our padding
1717
          from 254 to 255, which costs an extra length byte (at most once). */
1718
34.2k
       if (st->enable_qext) max_header_bytes += (nb_frames-1) + 1;
1719
34.2k
#endif
1720
1721
34.2k
       if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
1722
22.2k
          repacketize_len = out_data_bytes;
1723
12.0k
       else {
1724
12.0k
          celt_assert(cbr_bytes>=0);
1725
12.0k
          repacketize_len = IMIN(cbr_bytes, out_data_bytes);
1726
12.0k
       }
1727
34.2k
       max_len_sum = nb_frames + repacketize_len - max_header_bytes;
1728
1729
34.2k
       ALLOC(tmp_data, max_len_sum, unsigned char);
1730
34.2k
       curr_data = tmp_data;
1731
34.2k
       ALLOC(rp, 1, OpusRepacketizer);
1732
34.2k
       opus_repacketizer_init(rp);
1733
1734
1735
34.2k
       bak_to_mono = st->silk_mode.toMono;
1736
34.2k
       if (bak_to_mono)
1737
0
          st->force_channels = 1;
1738
34.2k
       else
1739
34.2k
          st->prev_channels = st->stream_channels;
1740
1741
157k
       for (i=0;i<nb_frames;i++)
1742
122k
       {
1743
122k
          int first_frame;
1744
122k
          int frame_to_celt;
1745
122k
          int frame_redundancy;
1746
122k
          opus_int32 curr_max;
1747
          /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh,
1748
             that allows for DRED not to be in the first frame. */
1749
122k
          first_frame = (i == 0) || (i == dtx_count);
1750
122k
          st->silk_mode.toMono = 0;
1751
122k
          st->nonfinal_frame = i<(nb_frames-1);
1752
1753
          /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
1754
122k
          frame_to_celt = to_celt && i==nb_frames-1;
1755
122k
          frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0));
1756
1757
122k
          curr_max = IMIN(3*st->bitrate_bps/(3*8*st->Fs/enc_frame_size), max_len_sum/nb_frames);
1758
#ifdef ENABLE_DRED
1759
          curr_max = IMIN(curr_max, (max_len_sum-3*dred_bitrate_bps/(3*8*st->Fs/frame_size))/nb_frames);
1760
          if (first_frame) curr_max += 3*dred_bitrate_bps/(3*8*st->Fs/frame_size);
1761
#endif
1762
122k
          curr_max = IMIN(max_len_sum-tot_size, curr_max);
1763
122k
#ifndef DISABLE_FLOAT_API
1764
122k
          if (analysis_read_pos_bak != -1) {
1765
36.2k
            is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1766
            /* Get analysis for current frame. */
1767
36.2k
            tonality_get_info(&st->analysis, &analysis_info, enc_frame_size);
1768
36.2k
          }
1769
122k
#endif
1770
1771
122k
          tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame,
1772
#ifdef ENABLE_DRED
1773
          dred_bitrate_bps,
1774
#endif
1775
122k
#ifndef DISABLE_FLOAT_API
1776
122k
          &analysis_info,
1777
122k
          is_silence,
1778
122k
#endif
1779
122k
                    frame_redundancy, celt_to_silk, prefill,
1780
122k
                    equiv_rate, frame_to_celt
1781
122k
              );
1782
122k
          if (tmp_len<0)
1783
0
          {
1784
0
             RESTORE_STACK;
1785
0
             return OPUS_INTERNAL_ERROR;
1786
122k
          } else if (tmp_len==1) {
1787
0
             dtx_count++;
1788
0
          }
1789
122k
          ret = opus_repacketizer_cat(rp, curr_data, tmp_len);
1790
1791
122k
          if (ret<0)
1792
0
          {
1793
0
             RESTORE_STACK;
1794
0
             return OPUS_INTERNAL_ERROR;
1795
0
          }
1796
122k
          tot_size += tmp_len;
1797
122k
          curr_data += tmp_len;
1798
122k
       }
1799
34.2k
       ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL, 0);
1800
34.2k
       if (ret<0)
1801
0
       {
1802
0
          ret = OPUS_INTERNAL_ERROR;
1803
0
       }
1804
34.2k
       st->silk_mode.toMono = bak_to_mono;
1805
34.2k
       RESTORE_STACK;
1806
34.2k
       return ret;
1807
174k
    } else {
1808
174k
      ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1,
1809
#ifdef ENABLE_DRED
1810
                dred_bitrate_bps,
1811
#endif
1812
174k
#ifndef DISABLE_FLOAT_API
1813
174k
                &analysis_info,
1814
174k
                is_silence,
1815
174k
#endif
1816
174k
                redundancy, celt_to_silk, prefill,
1817
174k
                equiv_rate, to_celt
1818
174k
          );
1819
174k
      RESTORE_STACK;
1820
174k
      return ret;
1821
174k
    }
1822
208k
}
opus_encode_native
Line
Count
Source
1164
237k
{
1165
237k
    void *silk_enc=NULL;
1166
237k
    CELTEncoder *celt_enc=NULL;
1167
237k
    int i;
1168
237k
    int ret=0;
1169
237k
    int prefill=0;
1170
237k
    int redundancy = 0;
1171
237k
    int celt_to_silk = 0;
1172
237k
    int to_celt = 0;
1173
237k
    int voice_est; /* Probability of voice in Q7 */
1174
237k
    opus_int32 equiv_rate;
1175
237k
    int frame_rate;
1176
237k
    opus_int32 max_rate; /* Max bitrate we're allowed to use */
1177
237k
    int curr_bandwidth;
1178
237k
    opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1179
237k
    opus_int32 cbr_bytes=-1;
1180
237k
    opus_val16 stereo_width;
1181
237k
    const CELTMode *celt_mode=NULL;
1182
237k
    int packet_size_cap = 1276;
1183
237k
#ifndef DISABLE_FLOAT_API
1184
237k
    AnalysisInfo analysis_info;
1185
237k
    int analysis_read_pos_bak=-1;
1186
237k
    int analysis_read_subframe_bak=-1;
1187
237k
    int is_silence = 0;
1188
237k
#endif
1189
#ifdef ENABLE_DRED
1190
    opus_int32 dred_bitrate_bps;
1191
#endif
1192
237k
    ALLOC_STACK;
1193
1194
237k
#ifdef ENABLE_QEXT
1195
237k
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1196
237k
#endif
1197
1198
    /* Just avoid insane packet sizes here, but the real bounds are applied later on. */
1199
237k
    max_data_bytes = IMIN(packet_size_cap*6, out_data_bytes);
1200
1201
237k
    st->rangeFinal = 0;
1202
237k
    if (frame_size <= 0 || max_data_bytes <= 0)
1203
0
    {
1204
0
       RESTORE_STACK;
1205
0
       return OPUS_BAD_ARG;
1206
0
    }
1207
1208
    /* Cannot encode 100 ms in 1 byte */
1209
237k
    if (max_data_bytes==1 && st->Fs==(frame_size*10))
1210
0
    {
1211
0
      RESTORE_STACK;
1212
0
      return OPUS_BUFFER_TOO_SMALL;
1213
0
    }
1214
1215
237k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1216
237k
        silk_enc = (char*)st+st->silk_enc_offset;
1217
237k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1218
237k
        celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1219
1220
237k
    lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1221
1222
237k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1223
237k
        celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1224
237k
#ifndef DISABLE_FLOAT_API
1225
237k
    analysis_info.valid = 0;
1226
#ifdef FIXED_POINT
1227
    if (st->silk_mode.complexity >= 10 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1228
#else
1229
237k
    if (st->silk_mode.complexity >= 7 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1230
50.5k
#endif
1231
50.5k
    {
1232
50.5k
       is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1233
50.5k
       analysis_read_pos_bak = st->analysis.read_pos;
1234
50.5k
       analysis_read_subframe_bak = st->analysis.read_subframe;
1235
50.5k
       run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1236
50.5k
             c1, c2, analysis_channels, st->Fs,
1237
50.5k
             lsb_depth, downmix, &analysis_info);
1238
1239
       /* Track the peak signal energy */
1240
50.5k
       if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1241
30.3k
          st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),
1242
50.5k
                compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1243
187k
    } else if (st->analysis.initialized) {
1244
0
       tonality_analysis_reset(&st->analysis);
1245
0
    }
1246
#else
1247
    (void)analysis_pcm;
1248
    (void)analysis_size;
1249
    (void)c1;
1250
    (void)c2;
1251
    (void)analysis_channels;
1252
    (void)downmix;
1253
#endif
1254
1255
237k
#ifndef DISABLE_FLOAT_API
1256
    /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1257
     * Otherwise, preserve voice_ratio from the last non-silent frame */
1258
237k
    if (!is_silence)
1259
235k
      st->voice_ratio = -1;
1260
1261
237k
    st->detected_bandwidth = 0;
1262
237k
    if (analysis_info.valid)
1263
30.9k
    {
1264
30.9k
       int analysis_bandwidth;
1265
30.9k
       if (st->signal_type == OPUS_AUTO)
1266
18.1k
       {
1267
18.1k
          float prob;
1268
18.1k
          if (st->prev_mode == 0)
1269
18.1k
             prob = analysis_info.music_prob;
1270
0
          else if (st->prev_mode == MODE_CELT_ONLY)
1271
0
             prob = analysis_info.music_prob_max;
1272
0
          else
1273
0
             prob = analysis_info.music_prob_min;
1274
18.1k
          st->voice_ratio = (int)floor(.5+100*(1-prob));
1275
18.1k
       }
1276
1277
30.9k
       analysis_bandwidth = analysis_info.bandwidth;
1278
30.9k
       if (analysis_bandwidth<=12)
1279
0
          st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1280
30.9k
       else if (analysis_bandwidth<=14)
1281
0
          st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1282
30.9k
       else if (analysis_bandwidth<=16)
1283
0
          st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1284
30.9k
       else if (analysis_bandwidth<=18)
1285
0
          st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1286
30.9k
       else
1287
30.9k
          st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1288
30.9k
    }
1289
#else
1290
    st->voice_ratio = -1;
1291
#endif
1292
1293
237k
    if (st->channels==2 && st->force_channels!=1)
1294
64.9k
       stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1295
172k
    else
1296
172k
       stereo_width = 0;
1297
237k
    st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1298
1299
237k
    frame_rate = st->Fs/frame_size;
1300
237k
    if (!st->use_vbr)
1301
98.5k
    {
1302
       /* Multiply by 12 to make sure the division is exact. */
1303
98.5k
       int frame_rate12 = 12*st->Fs/frame_size;
1304
       /* We need to make sure that "int" values always fit in 16 bits. */
1305
98.5k
       cbr_bytes = IMIN( (12*st->bitrate_bps/8 + frame_rate12/2)/frame_rate12, max_data_bytes);
1306
98.5k
       st->bitrate_bps = cbr_bytes*(opus_int32)frame_rate12*8/12;
1307
       /* Make sure we provide at least one byte to avoid failing. */
1308
98.5k
       max_data_bytes = IMAX(1, cbr_bytes);
1309
98.5k
    }
1310
#ifdef ENABLE_DRED
1311
    /* Allocate some of the bits to DRED if needed. */
1312
    dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size);
1313
    st->bitrate_bps -= dred_bitrate_bps;
1314
#endif
1315
237k
    if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1316
217k
       || (frame_rate<50 && (max_data_bytes*(opus_int32)frame_rate<300 || st->bitrate_bps < 2400)))
1317
29.2k
    {
1318
       /*If the space is too low to do something useful, emit 'PLC' frames.*/
1319
29.2k
       int tocmode = st->mode;
1320
29.2k
       int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1321
29.2k
       int packet_code = 0;
1322
29.2k
       int num_multiframes = 0;
1323
1324
29.2k
       if (tocmode==0)
1325
0
          tocmode = MODE_SILK_ONLY;
1326
29.2k
       if (frame_rate>100)
1327
5.21k
          tocmode = MODE_CELT_ONLY;
1328
       /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1329
29.2k
       if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1330
2.55k
       {
1331
2.55k
          frame_rate = 50;
1332
2.55k
          packet_code = 1;
1333
2.55k
       }
1334
1335
       /* >= 60 ms frames */
1336
29.2k
       if (frame_rate<=16)
1337
10.1k
       {
1338
          /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1339
10.1k
          if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1340
3.32k
          {
1341
3.32k
             tocmode = MODE_SILK_ONLY;
1342
1343
3.32k
             packet_code = frame_rate <= 12;
1344
3.32k
             frame_rate = frame_rate == 12 ? 25 : 16;
1345
3.32k
          }
1346
6.83k
          else
1347
6.83k
          {
1348
6.83k
             num_multiframes = 50/frame_rate;
1349
6.83k
             frame_rate = 50;
1350
6.83k
             packet_code = 3;
1351
6.83k
          }
1352
10.1k
       }
1353
1354
29.2k
       if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1355
3.32k
          bw=OPUS_BANDWIDTH_WIDEBAND;
1356
25.9k
       else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1357
0
          bw=OPUS_BANDWIDTH_NARROWBAND;
1358
25.9k
       else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1359
0
          bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1360
1361
29.2k
       data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1362
29.2k
       data[0] |= packet_code;
1363
1364
29.2k
       ret = packet_code <= 1 ? 1 : 2;
1365
1366
29.2k
       max_data_bytes = IMAX(max_data_bytes, ret);
1367
1368
29.2k
       if (packet_code==3)
1369
6.83k
          data[1] = num_multiframes;
1370
1371
29.2k
       if (!st->use_vbr)
1372
13.3k
       {
1373
13.3k
          ret = opus_packet_pad(data, ret, max_data_bytes);
1374
13.3k
          if (ret == OPUS_OK)
1375
13.3k
             ret = max_data_bytes;
1376
0
          else
1377
0
             ret = OPUS_INTERNAL_ERROR;
1378
13.3k
       }
1379
29.2k
       RESTORE_STACK;
1380
29.2k
       return ret;
1381
29.2k
    }
1382
208k
    max_rate = frame_rate*max_data_bytes*8;
1383
1384
    /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1385
208k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1386
208k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1387
1388
208k
    if (st->signal_type == OPUS_SIGNAL_VOICE)
1389
48.1k
       voice_est = 127;
1390
160k
    else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1391
36.8k
       voice_est = 0;
1392
123k
    else if (st->voice_ratio >= 0)
1393
11.4k
    {
1394
11.4k
       voice_est = st->voice_ratio*327>>8;
1395
       /* For AUDIO, never be more than 90% confident of having speech */
1396
11.4k
       if (st->application == OPUS_APPLICATION_AUDIO)
1397
6.26k
          voice_est = IMIN(voice_est, 115);
1398
112k
    } else if (st->application == OPUS_APPLICATION_VOIP)
1399
23.6k
       voice_est = 115;
1400
88.5k
    else
1401
88.5k
       voice_est = 48;
1402
1403
208k
    if (st->force_channels!=OPUS_AUTO && st->channels == 2)
1404
34.3k
    {
1405
34.3k
        st->stream_channels = st->force_channels;
1406
174k
    } else {
1407
#ifdef FUZZING
1408
        (void)stereo_music_threshold;
1409
        (void)stereo_voice_threshold;
1410
       /* Random mono/stereo decision */
1411
       if (st->channels == 2 && (rand()&0x1F)==0)
1412
          st->stream_channels = 3-st->stream_channels;
1413
#else
1414
       /* Rate-dependent mono-stereo decision */
1415
174k
       if (st->channels == 2)
1416
40.9k
       {
1417
40.9k
          opus_int32 stereo_threshold;
1418
40.9k
          stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1419
40.9k
          if (st->stream_channels == 2)
1420
40.9k
             stereo_threshold -= 1000;
1421
0
          else
1422
0
             stereo_threshold += 1000;
1423
40.9k
          st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1424
133k
       } else {
1425
133k
          st->stream_channels = st->channels;
1426
133k
       }
1427
174k
#endif
1428
174k
    }
1429
    /* Update equivalent rate for channels decision. */
1430
208k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1431
208k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1432
1433
    /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used,
1434
       e.g. because of the complexity setting or sample rate. */
1435
208k
#ifndef DISABLE_FLOAT_API
1436
208k
    st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1437
#else
1438
    st->silk_mode.useDTX = st->use_dtx;
1439
#endif
1440
1441
    /* Mode selection depending on application and signal type */
1442
208k
    if (st->application == OPUS_APPLICATION_FORCED_SILK)
1443
0
    {
1444
0
       st->mode = MODE_SILK_ONLY;
1445
208k
    } else if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1446
69.4k
    {
1447
69.4k
       st->mode = MODE_CELT_ONLY;
1448
139k
    } else if (st->user_forced_mode == OPUS_AUTO)
1449
129k
    {
1450
#ifdef FUZZING
1451
        (void)stereo_width;
1452
        (void)mode_thresholds;
1453
       /* Random mode switching */
1454
       if ((rand()&0xF)==0)
1455
       {
1456
          if ((rand()&0x1)==0)
1457
             st->mode = MODE_CELT_ONLY;
1458
          else
1459
             st->mode = MODE_SILK_ONLY;
1460
       } else {
1461
          if (st->prev_mode==MODE_CELT_ONLY)
1462
             st->mode = MODE_CELT_ONLY;
1463
          else
1464
             st->mode = MODE_SILK_ONLY;
1465
       }
1466
#else
1467
129k
       opus_int32 mode_voice, mode_music;
1468
129k
       opus_int32 threshold;
1469
1470
       /* Interpolate based on stereo width */
1471
129k
       mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1472
129k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1473
129k
       mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1474
129k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1475
       /* Interpolate based on speech/music probability */
1476
129k
       threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1477
       /* Bias towards SILK for VoIP because of some useful features */
1478
129k
       if (st->application == OPUS_APPLICATION_VOIP)
1479
47.0k
          threshold += 8000;
1480
1481
       /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1482
       /* Hysteresis */
1483
129k
       if (st->prev_mode == MODE_CELT_ONLY)
1484
0
           threshold -= 4000;
1485
129k
       else if (st->prev_mode>0)
1486
0
           threshold += 4000;
1487
1488
129k
       st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1489
1490
       /* When FEC is enabled and there's enough packet loss, use SILK.
1491
          Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */
1492
129k
       if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25))
1493
66.9k
          st->mode = MODE_SILK_ONLY;
1494
       /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1495
          use SILK in order to make use of its DTX. */
1496
129k
       if (st->silk_mode.useDTX && voice_est > 100)
1497
26.2k
          st->mode = MODE_SILK_ONLY;
1498
129k
#endif
1499
1500
       /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1501
129k
       if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1502
1.28k
          st->mode = MODE_CELT_ONLY;
1503
129k
    } else {
1504
9.37k
       st->mode = st->user_forced_mode;
1505
9.37k
    }
1506
1507
    /* Override the chosen mode to make sure we meet the requested frame size */
1508
208k
    if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1509
16.0k
       st->mode = MODE_CELT_ONLY;
1510
208k
    if (st->lfe)
1511
409
       st->mode = MODE_CELT_ONLY;
1512
1513
208k
    if (st->prev_mode > 0 &&
1514
0
        ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1515
0
    (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1516
0
    {
1517
0
        redundancy = 1;
1518
0
        celt_to_silk = (st->mode != MODE_CELT_ONLY);
1519
0
        if (!celt_to_silk)
1520
0
        {
1521
            /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1522
0
            if (frame_size >= st->Fs/100)
1523
0
            {
1524
0
                st->mode = st->prev_mode;
1525
0
                to_celt = 1;
1526
0
            } else {
1527
0
                redundancy=0;
1528
0
            }
1529
0
        }
1530
0
    }
1531
1532
    /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1533
     * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1534
208k
    if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1535
0
          && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1536
0
    {
1537
       /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1538
0
       st->silk_mode.toMono = 1;
1539
0
       st->stream_channels = 2;
1540
208k
    } else {
1541
208k
       st->silk_mode.toMono = 0;
1542
208k
    }
1543
1544
    /* Update equivalent rate with mode decision. */
1545
208k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1546
208k
          st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1547
1548
208k
    if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1549
0
    {
1550
0
        silk_EncControlStruct dummy;
1551
0
        silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy);
1552
0
        prefill=1;
1553
0
    }
1554
1555
    /* Automatic (rate-dependent) bandwidth selection */
1556
208k
    if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1557
208k
    {
1558
208k
        const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1559
208k
        opus_int32 bandwidth_thresholds[8];
1560
208k
        int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1561
1562
208k
        if (st->channels==2 && st->force_channels!=1)
1563
61.6k
        {
1564
61.6k
           voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1565
61.6k
           music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1566
147k
        } else {
1567
147k
           voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1568
147k
           music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1569
147k
        }
1570
        /* Interpolate bandwidth thresholds depending on voice estimation */
1571
1.87M
        for (i=0;i<8;i++)
1572
1.66M
        {
1573
1.66M
           bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1574
1.66M
                    + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1575
1.66M
        }
1576
252k
        do {
1577
252k
            int threshold, hysteresis;
1578
252k
            threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1579
252k
            hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1580
252k
            if (!st->first)
1581
0
            {
1582
0
                if (st->auto_bandwidth >= bandwidth)
1583
0
                    threshold -= hysteresis;
1584
0
                else
1585
0
                    threshold += hysteresis;
1586
0
            }
1587
252k
            if (equiv_rate >= threshold)
1588
206k
                break;
1589
252k
        } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1590
        /* We don't use mediumband anymore, except when explicitly requested or during
1591
           mode transitions. */
1592
208k
        if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1593
0
           bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1594
208k
        st->bandwidth = st->auto_bandwidth = bandwidth;
1595
        /* Prevents any transition to SWB/FB until the SILK layer has fully
1596
           switched to WB mode and turned the variable LP filter off */
1597
208k
        if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1598
0
            st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1599
208k
    }
1600
1601
208k
    if (st->bandwidth>st->max_bandwidth)
1602
149k
       st->bandwidth = st->max_bandwidth;
1603
1604
208k
    if (st->user_bandwidth != OPUS_AUTO)
1605
4.35k
        st->bandwidth = st->user_bandwidth;
1606
1607
    /* This prevents us from using hybrid at unsafe CBR/max rates */
1608
208k
    if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1609
2.45k
    {
1610
2.45k
       st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1611
2.45k
    }
1612
1613
    /* Prevents Opus from wasting bits on frequencies that are above
1614
       the Nyquist rate of the input signal */
1615
208k
    if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1616
37.2k
        st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1617
208k
    if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1618
53.2k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1619
208k
    if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1620
67.9k
        st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1621
208k
    if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1622
70.7k
        st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1623
208k
#ifndef DISABLE_FLOAT_API
1624
    /* Use detected bandwidth to reduce the encoded bandwidth. */
1625
208k
    if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1626
18.7k
    {
1627
18.7k
       int min_detected_bandwidth;
1628
       /* Makes bandwidth detection more conservative just in case the detector
1629
          gets it wrong when we could have coded a high bandwidth transparently.
1630
          When operating in SILK/hybrid mode, we don't go below wideband to avoid
1631
          more complicated switches that require redundancy. */
1632
18.7k
       if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1633
165
          min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1634
18.5k
       else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1635
60
          min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1636
18.4k
       else if (equiv_rate <= 30000*st->stream_channels)
1637
13.5k
          min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1638
4.95k
       else if (equiv_rate <= 44000*st->stream_channels)
1639
2.34k
          min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1640
2.61k
       else
1641
2.61k
          min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1642
1643
18.7k
       st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1644
18.7k
       st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1645
18.7k
    }
1646
208k
#endif
1647
208k
    st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1648
208k
          st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1649
208k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1650
208k
        celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1651
1652
    /* CELT mode doesn't support mediumband, use wideband instead */
1653
208k
    if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1654
13.1k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1655
208k
    if (st->lfe)
1656
409
       st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1657
1658
208k
    curr_bandwidth = st->bandwidth;
1659
1660
208k
    if (st->application == OPUS_APPLICATION_FORCED_SILK && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1661
0
       st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1662
    /* Chooses the appropriate mode for speech
1663
       *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1664
208k
    if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1665
6.33k
        st->mode = MODE_HYBRID;
1666
208k
    if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1667
0
        st->mode = MODE_SILK_ONLY;
1668
1669
    /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1670
208k
    if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1671
34.2k
    {
1672
34.2k
       int enc_frame_size;
1673
34.2k
       int nb_frames;
1674
34.2k
       VARDECL(unsigned char, tmp_data);
1675
34.2k
       VARDECL(OpusRepacketizer, rp);
1676
34.2k
       int max_header_bytes;
1677
34.2k
       opus_int32 repacketize_len;
1678
34.2k
       opus_int32 max_len_sum;
1679
34.2k
       opus_int32 tot_size=0;
1680
34.2k
       unsigned char *curr_data;
1681
34.2k
       int tmp_len;
1682
34.2k
       int dtx_count = 0;
1683
34.2k
       int bak_to_mono;
1684
1685
34.2k
       if (st->mode == MODE_SILK_ONLY)
1686
17.5k
       {
1687
17.5k
         if (frame_size == 2*st->Fs/25)  /* 80 ms -> 2x 40 ms */
1688
4.96k
           enc_frame_size = st->Fs/25;
1689
12.5k
         else if (frame_size == 3*st->Fs/25)  /* 120 ms -> 2x 60 ms */
1690
4.85k
           enc_frame_size = 3*st->Fs/50;
1691
7.73k
         else                            /* 100 ms -> 5x 20 ms */
1692
7.73k
           enc_frame_size = st->Fs/50;
1693
17.5k
       }
1694
16.7k
       else
1695
16.7k
         enc_frame_size = st->Fs/50;
1696
1697
34.2k
       nb_frames = frame_size/enc_frame_size;
1698
1699
34.2k
#ifndef DISABLE_FLOAT_API
1700
34.2k
       if (analysis_read_pos_bak!= -1)
1701
10.1k
       {
1702
          /* Reset analysis position to the beginning of the first frame so we
1703
             can use it one frame at a time. */
1704
10.1k
          st->analysis.read_pos = analysis_read_pos_bak;
1705
10.1k
          st->analysis.read_subframe = analysis_read_subframe_bak;
1706
10.1k
       }
1707
34.2k
#endif
1708
1709
       /* Worst cases:
1710
        * 2 frames: Code 2 with different compressed sizes
1711
        * >2 frames: Code 3 VBR */
1712
34.2k
       max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
1713
34.2k
#ifdef ENABLE_QEXT
1714
       /* Cover the use of the separators that are the only thing that can get us over
1715
          once we consider that we need to subtract the extension overhead in each
1716
          of the individual frames. Also consider that a separator can get our padding
1717
          from 254 to 255, which costs an extra length byte (at most once). */
1718
34.2k
       if (st->enable_qext) max_header_bytes += (nb_frames-1) + 1;
1719
34.2k
#endif
1720
1721
34.2k
       if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
1722
22.2k
          repacketize_len = out_data_bytes;
1723
12.0k
       else {
1724
12.0k
          celt_assert(cbr_bytes>=0);
1725
12.0k
          repacketize_len = IMIN(cbr_bytes, out_data_bytes);
1726
12.0k
       }
1727
34.2k
       max_len_sum = nb_frames + repacketize_len - max_header_bytes;
1728
1729
34.2k
       ALLOC(tmp_data, max_len_sum, unsigned char);
1730
34.2k
       curr_data = tmp_data;
1731
34.2k
       ALLOC(rp, 1, OpusRepacketizer);
1732
34.2k
       opus_repacketizer_init(rp);
1733
1734
1735
34.2k
       bak_to_mono = st->silk_mode.toMono;
1736
34.2k
       if (bak_to_mono)
1737
0
          st->force_channels = 1;
1738
34.2k
       else
1739
34.2k
          st->prev_channels = st->stream_channels;
1740
1741
157k
       for (i=0;i<nb_frames;i++)
1742
122k
       {
1743
122k
          int first_frame;
1744
122k
          int frame_to_celt;
1745
122k
          int frame_redundancy;
1746
122k
          opus_int32 curr_max;
1747
          /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh,
1748
             that allows for DRED not to be in the first frame. */
1749
122k
          first_frame = (i == 0) || (i == dtx_count);
1750
122k
          st->silk_mode.toMono = 0;
1751
122k
          st->nonfinal_frame = i<(nb_frames-1);
1752
1753
          /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
1754
122k
          frame_to_celt = to_celt && i==nb_frames-1;
1755
122k
          frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0));
1756
1757
122k
          curr_max = IMIN(3*st->bitrate_bps/(3*8*st->Fs/enc_frame_size), max_len_sum/nb_frames);
1758
#ifdef ENABLE_DRED
1759
          curr_max = IMIN(curr_max, (max_len_sum-3*dred_bitrate_bps/(3*8*st->Fs/frame_size))/nb_frames);
1760
          if (first_frame) curr_max += 3*dred_bitrate_bps/(3*8*st->Fs/frame_size);
1761
#endif
1762
122k
          curr_max = IMIN(max_len_sum-tot_size, curr_max);
1763
122k
#ifndef DISABLE_FLOAT_API
1764
122k
          if (analysis_read_pos_bak != -1) {
1765
36.2k
            is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1766
            /* Get analysis for current frame. */
1767
36.2k
            tonality_get_info(&st->analysis, &analysis_info, enc_frame_size);
1768
36.2k
          }
1769
122k
#endif
1770
1771
122k
          tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame,
1772
#ifdef ENABLE_DRED
1773
          dred_bitrate_bps,
1774
#endif
1775
122k
#ifndef DISABLE_FLOAT_API
1776
122k
          &analysis_info,
1777
122k
          is_silence,
1778
122k
#endif
1779
122k
                    frame_redundancy, celt_to_silk, prefill,
1780
122k
                    equiv_rate, frame_to_celt
1781
122k
              );
1782
122k
          if (tmp_len<0)
1783
0
          {
1784
0
             RESTORE_STACK;
1785
0
             return OPUS_INTERNAL_ERROR;
1786
122k
          } else if (tmp_len==1) {
1787
0
             dtx_count++;
1788
0
          }
1789
122k
          ret = opus_repacketizer_cat(rp, curr_data, tmp_len);
1790
1791
122k
          if (ret<0)
1792
0
          {
1793
0
             RESTORE_STACK;
1794
0
             return OPUS_INTERNAL_ERROR;
1795
0
          }
1796
122k
          tot_size += tmp_len;
1797
122k
          curr_data += tmp_len;
1798
122k
       }
1799
34.2k
       ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL, 0);
1800
34.2k
       if (ret<0)
1801
0
       {
1802
0
          ret = OPUS_INTERNAL_ERROR;
1803
0
       }
1804
34.2k
       st->silk_mode.toMono = bak_to_mono;
1805
34.2k
       RESTORE_STACK;
1806
34.2k
       return ret;
1807
174k
    } else {
1808
174k
      ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1,
1809
#ifdef ENABLE_DRED
1810
                dred_bitrate_bps,
1811
#endif
1812
174k
#ifndef DISABLE_FLOAT_API
1813
174k
                &analysis_info,
1814
174k
                is_silence,
1815
174k
#endif
1816
174k
                redundancy, celt_to_silk, prefill,
1817
174k
                equiv_rate, to_celt
1818
174k
          );
1819
174k
      RESTORE_STACK;
1820
174k
      return ret;
1821
174k
    }
1822
208k
}
opus_encode_native
Line
Count
Source
1164
221k
{
1165
221k
    void *silk_enc=NULL;
1166
221k
    CELTEncoder *celt_enc=NULL;
1167
221k
    int i;
1168
221k
    int ret=0;
1169
221k
    int prefill=0;
1170
221k
    int redundancy = 0;
1171
221k
    int celt_to_silk = 0;
1172
221k
    int to_celt = 0;
1173
221k
    int voice_est; /* Probability of voice in Q7 */
1174
221k
    opus_int32 equiv_rate;
1175
221k
    int frame_rate;
1176
221k
    opus_int32 max_rate; /* Max bitrate we're allowed to use */
1177
221k
    int curr_bandwidth;
1178
221k
    opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1179
221k
    opus_int32 cbr_bytes=-1;
1180
221k
    opus_val16 stereo_width;
1181
221k
    const CELTMode *celt_mode=NULL;
1182
221k
    int packet_size_cap = 1276;
1183
221k
#ifndef DISABLE_FLOAT_API
1184
221k
    AnalysisInfo analysis_info;
1185
221k
    int analysis_read_pos_bak=-1;
1186
221k
    int analysis_read_subframe_bak=-1;
1187
221k
    int is_silence = 0;
1188
221k
#endif
1189
#ifdef ENABLE_DRED
1190
    opus_int32 dred_bitrate_bps;
1191
#endif
1192
221k
    ALLOC_STACK;
1193
1194
#ifdef ENABLE_QEXT
1195
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1196
#endif
1197
1198
    /* Just avoid insane packet sizes here, but the real bounds are applied later on. */
1199
221k
    max_data_bytes = IMIN(packet_size_cap*6, out_data_bytes);
1200
1201
221k
    st->rangeFinal = 0;
1202
221k
    if (frame_size <= 0 || max_data_bytes <= 0)
1203
0
    {
1204
0
       RESTORE_STACK;
1205
0
       return OPUS_BAD_ARG;
1206
0
    }
1207
1208
    /* Cannot encode 100 ms in 1 byte */
1209
221k
    if (max_data_bytes==1 && st->Fs==(frame_size*10))
1210
0
    {
1211
0
      RESTORE_STACK;
1212
0
      return OPUS_BUFFER_TOO_SMALL;
1213
0
    }
1214
1215
221k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1216
221k
        silk_enc = (char*)st+st->silk_enc_offset;
1217
221k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1218
221k
        celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1219
1220
221k
    lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1221
1222
221k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1223
221k
        celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1224
221k
#ifndef DISABLE_FLOAT_API
1225
221k
    analysis_info.valid = 0;
1226
#ifdef FIXED_POINT
1227
    if (st->silk_mode.complexity >= 10 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1228
#else
1229
221k
    if (st->silk_mode.complexity >= 7 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1230
50.9k
#endif
1231
50.9k
    {
1232
50.9k
       is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1233
50.9k
       analysis_read_pos_bak = st->analysis.read_pos;
1234
50.9k
       analysis_read_subframe_bak = st->analysis.read_subframe;
1235
50.9k
       run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1236
50.9k
             c1, c2, analysis_channels, st->Fs,
1237
50.9k
             lsb_depth, downmix, &analysis_info);
1238
1239
       /* Track the peak signal energy */
1240
50.9k
       if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1241
30.9k
          st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),
1242
50.9k
                compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1243
170k
    } else if (st->analysis.initialized) {
1244
0
       tonality_analysis_reset(&st->analysis);
1245
0
    }
1246
#else
1247
    (void)analysis_pcm;
1248
    (void)analysis_size;
1249
    (void)c1;
1250
    (void)c2;
1251
    (void)analysis_channels;
1252
    (void)downmix;
1253
#endif
1254
1255
221k
#ifndef DISABLE_FLOAT_API
1256
    /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1257
     * Otherwise, preserve voice_ratio from the last non-silent frame */
1258
221k
    if (!is_silence)
1259
218k
      st->voice_ratio = -1;
1260
1261
221k
    st->detected_bandwidth = 0;
1262
221k
    if (analysis_info.valid)
1263
31.5k
    {
1264
31.5k
       int analysis_bandwidth;
1265
31.5k
       if (st->signal_type == OPUS_AUTO)
1266
21.8k
       {
1267
21.8k
          float prob;
1268
21.8k
          if (st->prev_mode == 0)
1269
21.8k
             prob = analysis_info.music_prob;
1270
0
          else if (st->prev_mode == MODE_CELT_ONLY)
1271
0
             prob = analysis_info.music_prob_max;
1272
0
          else
1273
0
             prob = analysis_info.music_prob_min;
1274
21.8k
          st->voice_ratio = (int)floor(.5+100*(1-prob));
1275
21.8k
       }
1276
1277
31.5k
       analysis_bandwidth = analysis_info.bandwidth;
1278
31.5k
       if (analysis_bandwidth<=12)
1279
0
          st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1280
31.5k
       else if (analysis_bandwidth<=14)
1281
0
          st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1282
31.5k
       else if (analysis_bandwidth<=16)
1283
0
          st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1284
31.5k
       else if (analysis_bandwidth<=18)
1285
0
          st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1286
31.5k
       else
1287
31.5k
          st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1288
31.5k
    }
1289
#else
1290
    st->voice_ratio = -1;
1291
#endif
1292
1293
221k
    if (st->channels==2 && st->force_channels!=1)
1294
63.4k
       stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1295
157k
    else
1296
157k
       stereo_width = 0;
1297
221k
    st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1298
1299
221k
    frame_rate = st->Fs/frame_size;
1300
221k
    if (!st->use_vbr)
1301
83.3k
    {
1302
       /* Multiply by 12 to make sure the division is exact. */
1303
83.3k
       int frame_rate12 = 12*st->Fs/frame_size;
1304
       /* We need to make sure that "int" values always fit in 16 bits. */
1305
83.3k
       cbr_bytes = IMIN( (12*st->bitrate_bps/8 + frame_rate12/2)/frame_rate12, max_data_bytes);
1306
83.3k
       st->bitrate_bps = cbr_bytes*(opus_int32)frame_rate12*8/12;
1307
       /* Make sure we provide at least one byte to avoid failing. */
1308
83.3k
       max_data_bytes = IMAX(1, cbr_bytes);
1309
83.3k
    }
1310
#ifdef ENABLE_DRED
1311
    /* Allocate some of the bits to DRED if needed. */
1312
    dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size);
1313
    st->bitrate_bps -= dred_bitrate_bps;
1314
#endif
1315
221k
    if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1316
205k
       || (frame_rate<50 && (max_data_bytes*(opus_int32)frame_rate<300 || st->bitrate_bps < 2400)))
1317
24.5k
    {
1318
       /*If the space is too low to do something useful, emit 'PLC' frames.*/
1319
24.5k
       int tocmode = st->mode;
1320
24.5k
       int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1321
24.5k
       int packet_code = 0;
1322
24.5k
       int num_multiframes = 0;
1323
1324
24.5k
       if (tocmode==0)
1325
0
          tocmode = MODE_SILK_ONLY;
1326
24.5k
       if (frame_rate>100)
1327
4.57k
          tocmode = MODE_CELT_ONLY;
1328
       /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1329
24.5k
       if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1330
2.13k
       {
1331
2.13k
          frame_rate = 50;
1332
2.13k
          packet_code = 1;
1333
2.13k
       }
1334
1335
       /* >= 60 ms frames */
1336
24.5k
       if (frame_rate<=16)
1337
9.27k
       {
1338
          /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1339
9.27k
          if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1340
2.47k
          {
1341
2.47k
             tocmode = MODE_SILK_ONLY;
1342
1343
2.47k
             packet_code = frame_rate <= 12;
1344
2.47k
             frame_rate = frame_rate == 12 ? 25 : 16;
1345
2.47k
          }
1346
6.79k
          else
1347
6.79k
          {
1348
6.79k
             num_multiframes = 50/frame_rate;
1349
6.79k
             frame_rate = 50;
1350
6.79k
             packet_code = 3;
1351
6.79k
          }
1352
9.27k
       }
1353
1354
24.5k
       if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1355
2.47k
          bw=OPUS_BANDWIDTH_WIDEBAND;
1356
22.0k
       else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1357
0
          bw=OPUS_BANDWIDTH_NARROWBAND;
1358
22.0k
       else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1359
0
          bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1360
1361
24.5k
       data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1362
24.5k
       data[0] |= packet_code;
1363
1364
24.5k
       ret = packet_code <= 1 ? 1 : 2;
1365
1366
24.5k
       max_data_bytes = IMAX(max_data_bytes, ret);
1367
1368
24.5k
       if (packet_code==3)
1369
6.79k
          data[1] = num_multiframes;
1370
1371
24.5k
       if (!st->use_vbr)
1372
7.70k
       {
1373
7.70k
          ret = opus_packet_pad(data, ret, max_data_bytes);
1374
7.70k
          if (ret == OPUS_OK)
1375
7.70k
             ret = max_data_bytes;
1376
0
          else
1377
0
             ret = OPUS_INTERNAL_ERROR;
1378
7.70k
       }
1379
24.5k
       RESTORE_STACK;
1380
24.5k
       return ret;
1381
24.5k
    }
1382
196k
    max_rate = frame_rate*max_data_bytes*8;
1383
1384
    /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1385
196k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1386
196k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1387
1388
196k
    if (st->signal_type == OPUS_SIGNAL_VOICE)
1389
35.4k
       voice_est = 127;
1390
161k
    else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1391
28.9k
       voice_est = 0;
1392
132k
    else if (st->voice_ratio >= 0)
1393
14.2k
    {
1394
14.2k
       voice_est = st->voice_ratio*327>>8;
1395
       /* For AUDIO, never be more than 90% confident of having speech */
1396
14.2k
       if (st->application == OPUS_APPLICATION_AUDIO)
1397
8.35k
          voice_est = IMIN(voice_est, 115);
1398
117k
    } else if (st->application == OPUS_APPLICATION_VOIP)
1399
26.7k
       voice_est = 115;
1400
91.0k
    else
1401
91.0k
       voice_est = 48;
1402
1403
196k
    if (st->force_channels!=OPUS_AUTO && st->channels == 2)
1404
32.6k
    {
1405
32.6k
        st->stream_channels = st->force_channels;
1406
164k
    } else {
1407
#ifdef FUZZING
1408
        (void)stereo_music_threshold;
1409
        (void)stereo_voice_threshold;
1410
       /* Random mono/stereo decision */
1411
       if (st->channels == 2 && (rand()&0x1F)==0)
1412
          st->stream_channels = 3-st->stream_channels;
1413
#else
1414
       /* Rate-dependent mono-stereo decision */
1415
164k
       if (st->channels == 2)
1416
41.5k
       {
1417
41.5k
          opus_int32 stereo_threshold;
1418
41.5k
          stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1419
41.5k
          if (st->stream_channels == 2)
1420
41.5k
             stereo_threshold -= 1000;
1421
0
          else
1422
0
             stereo_threshold += 1000;
1423
41.5k
          st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1424
122k
       } else {
1425
122k
          st->stream_channels = st->channels;
1426
122k
       }
1427
164k
#endif
1428
164k
    }
1429
    /* Update equivalent rate for channels decision. */
1430
196k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1431
196k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1432
1433
    /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used,
1434
       e.g. because of the complexity setting or sample rate. */
1435
196k
#ifndef DISABLE_FLOAT_API
1436
196k
    st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1437
#else
1438
    st->silk_mode.useDTX = st->use_dtx;
1439
#endif
1440
1441
    /* Mode selection depending on application and signal type */
1442
196k
    if (st->application == OPUS_APPLICATION_FORCED_SILK)
1443
0
    {
1444
0
       st->mode = MODE_SILK_ONLY;
1445
196k
    } else if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1446
64.3k
    {
1447
64.3k
       st->mode = MODE_CELT_ONLY;
1448
132k
    } else if (st->user_forced_mode == OPUS_AUTO)
1449
123k
    {
1450
#ifdef FUZZING
1451
        (void)stereo_width;
1452
        (void)mode_thresholds;
1453
       /* Random mode switching */
1454
       if ((rand()&0xF)==0)
1455
       {
1456
          if ((rand()&0x1)==0)
1457
             st->mode = MODE_CELT_ONLY;
1458
          else
1459
             st->mode = MODE_SILK_ONLY;
1460
       } else {
1461
          if (st->prev_mode==MODE_CELT_ONLY)
1462
             st->mode = MODE_CELT_ONLY;
1463
          else
1464
             st->mode = MODE_SILK_ONLY;
1465
       }
1466
#else
1467
123k
       opus_int32 mode_voice, mode_music;
1468
123k
       opus_int32 threshold;
1469
1470
       /* Interpolate based on stereo width */
1471
123k
       mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1472
123k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1473
123k
       mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1474
123k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1475
       /* Interpolate based on speech/music probability */
1476
123k
       threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1477
       /* Bias towards SILK for VoIP because of some useful features */
1478
123k
       if (st->application == OPUS_APPLICATION_VOIP)
1479
42.9k
          threshold += 8000;
1480
1481
       /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1482
       /* Hysteresis */
1483
123k
       if (st->prev_mode == MODE_CELT_ONLY)
1484
0
           threshold -= 4000;
1485
123k
       else if (st->prev_mode>0)
1486
0
           threshold += 4000;
1487
1488
123k
       st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1489
1490
       /* When FEC is enabled and there's enough packet loss, use SILK.
1491
          Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */
1492
123k
       if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25))
1493
59.6k
          st->mode = MODE_SILK_ONLY;
1494
       /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1495
          use SILK in order to make use of its DTX. */
1496
123k
       if (st->silk_mode.useDTX && voice_est > 100)
1497
19.9k
          st->mode = MODE_SILK_ONLY;
1498
123k
#endif
1499
1500
       /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1501
123k
       if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1502
1.07k
          st->mode = MODE_CELT_ONLY;
1503
123k
    } else {
1504
8.83k
       st->mode = st->user_forced_mode;
1505
8.83k
    }
1506
1507
    /* Override the chosen mode to make sure we meet the requested frame size */
1508
196k
    if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1509
16.0k
       st->mode = MODE_CELT_ONLY;
1510
196k
    if (st->lfe)
1511
327
       st->mode = MODE_CELT_ONLY;
1512
1513
196k
    if (st->prev_mode > 0 &&
1514
0
        ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1515
0
    (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1516
0
    {
1517
0
        redundancy = 1;
1518
0
        celt_to_silk = (st->mode != MODE_CELT_ONLY);
1519
0
        if (!celt_to_silk)
1520
0
        {
1521
            /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1522
0
            if (frame_size >= st->Fs/100)
1523
0
            {
1524
0
                st->mode = st->prev_mode;
1525
0
                to_celt = 1;
1526
0
            } else {
1527
0
                redundancy=0;
1528
0
            }
1529
0
        }
1530
0
    }
1531
1532
    /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1533
     * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1534
196k
    if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1535
0
          && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1536
0
    {
1537
       /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1538
0
       st->silk_mode.toMono = 1;
1539
0
       st->stream_channels = 2;
1540
196k
    } else {
1541
196k
       st->silk_mode.toMono = 0;
1542
196k
    }
1543
1544
    /* Update equivalent rate with mode decision. */
1545
196k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1546
196k
          st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1547
1548
196k
    if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1549
0
    {
1550
0
        silk_EncControlStruct dummy;
1551
0
        silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy);
1552
0
        prefill=1;
1553
0
    }
1554
1555
    /* Automatic (rate-dependent) bandwidth selection */
1556
196k
    if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1557
196k
    {
1558
196k
        const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1559
196k
        opus_int32 bandwidth_thresholds[8];
1560
196k
        int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1561
1562
196k
        if (st->channels==2 && st->force_channels!=1)
1563
60.7k
        {
1564
60.7k
           voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1565
60.7k
           music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1566
135k
        } else {
1567
135k
           voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1568
135k
           music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1569
135k
        }
1570
        /* Interpolate bandwidth thresholds depending on voice estimation */
1571
1.76M
        for (i=0;i<8;i++)
1572
1.57M
        {
1573
1.57M
           bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1574
1.57M
                    + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1575
1.57M
        }
1576
233k
        do {
1577
233k
            int threshold, hysteresis;
1578
233k
            threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1579
233k
            hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1580
233k
            if (!st->first)
1581
0
            {
1582
0
                if (st->auto_bandwidth >= bandwidth)
1583
0
                    threshold -= hysteresis;
1584
0
                else
1585
0
                    threshold += hysteresis;
1586
0
            }
1587
233k
            if (equiv_rate >= threshold)
1588
194k
                break;
1589
233k
        } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1590
        /* We don't use mediumband anymore, except when explicitly requested or during
1591
           mode transitions. */
1592
196k
        if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1593
0
           bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1594
196k
        st->bandwidth = st->auto_bandwidth = bandwidth;
1595
        /* Prevents any transition to SWB/FB until the SILK layer has fully
1596
           switched to WB mode and turned the variable LP filter off */
1597
196k
        if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1598
0
            st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1599
196k
    }
1600
1601
196k
    if (st->bandwidth>st->max_bandwidth)
1602
145k
       st->bandwidth = st->max_bandwidth;
1603
1604
196k
    if (st->user_bandwidth != OPUS_AUTO)
1605
3.49k
        st->bandwidth = st->user_bandwidth;
1606
1607
    /* This prevents us from using hybrid at unsafe CBR/max rates */
1608
196k
    if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1609
2.05k
    {
1610
2.05k
       st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1611
2.05k
    }
1612
1613
    /* Prevents Opus from wasting bits on frequencies that are above
1614
       the Nyquist rate of the input signal */
1615
196k
    if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1616
36.8k
        st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1617
196k
    if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1618
47.9k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1619
196k
    if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1620
54.7k
        st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1621
196k
    if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1622
54.9k
        st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1623
196k
#ifndef DISABLE_FLOAT_API
1624
    /* Use detected bandwidth to reduce the encoded bandwidth. */
1625
196k
    if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1626
20.4k
    {
1627
20.4k
       int min_detected_bandwidth;
1628
       /* Makes bandwidth detection more conservative just in case the detector
1629
          gets it wrong when we could have coded a high bandwidth transparently.
1630
          When operating in SILK/hybrid mode, we don't go below wideband to avoid
1631
          more complicated switches that require redundancy. */
1632
20.4k
       if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1633
151
          min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1634
20.2k
       else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1635
31
          min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1636
20.2k
       else if (equiv_rate <= 30000*st->stream_channels)
1637
13.7k
          min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1638
6.49k
       else if (equiv_rate <= 44000*st->stream_channels)
1639
2.69k
          min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1640
3.79k
       else
1641
3.79k
          min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1642
1643
20.4k
       st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1644
20.4k
       st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1645
20.4k
    }
1646
196k
#endif
1647
196k
    st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1648
196k
          st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1649
196k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1650
196k
        celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1651
1652
    /* CELT mode doesn't support mediumband, use wideband instead */
1653
196k
    if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1654
11.6k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1655
196k
    if (st->lfe)
1656
327
       st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1657
1658
196k
    curr_bandwidth = st->bandwidth;
1659
1660
196k
    if (st->application == OPUS_APPLICATION_FORCED_SILK && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1661
0
       st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1662
    /* Chooses the appropriate mode for speech
1663
       *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1664
196k
    if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1665
5.55k
        st->mode = MODE_HYBRID;
1666
196k
    if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1667
0
        st->mode = MODE_SILK_ONLY;
1668
1669
    /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1670
196k
    if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1671
33.2k
    {
1672
33.2k
       int enc_frame_size;
1673
33.2k
       int nb_frames;
1674
33.2k
       VARDECL(unsigned char, tmp_data);
1675
33.2k
       VARDECL(OpusRepacketizer, rp);
1676
33.2k
       int max_header_bytes;
1677
33.2k
       opus_int32 repacketize_len;
1678
33.2k
       opus_int32 max_len_sum;
1679
33.2k
       opus_int32 tot_size=0;
1680
33.2k
       unsigned char *curr_data;
1681
33.2k
       int tmp_len;
1682
33.2k
       int dtx_count = 0;
1683
33.2k
       int bak_to_mono;
1684
1685
33.2k
       if (st->mode == MODE_SILK_ONLY)
1686
16.4k
       {
1687
16.4k
         if (frame_size == 2*st->Fs/25)  /* 80 ms -> 2x 40 ms */
1688
4.88k
           enc_frame_size = st->Fs/25;
1689
11.5k
         else if (frame_size == 3*st->Fs/25)  /* 120 ms -> 2x 60 ms */
1690
4.13k
           enc_frame_size = 3*st->Fs/50;
1691
7.45k
         else                            /* 100 ms -> 5x 20 ms */
1692
7.45k
           enc_frame_size = st->Fs/50;
1693
16.4k
       }
1694
16.7k
       else
1695
16.7k
         enc_frame_size = st->Fs/50;
1696
1697
33.2k
       nb_frames = frame_size/enc_frame_size;
1698
1699
33.2k
#ifndef DISABLE_FLOAT_API
1700
33.2k
       if (analysis_read_pos_bak!= -1)
1701
10.0k
       {
1702
          /* Reset analysis position to the beginning of the first frame so we
1703
             can use it one frame at a time. */
1704
10.0k
          st->analysis.read_pos = analysis_read_pos_bak;
1705
10.0k
          st->analysis.read_subframe = analysis_read_subframe_bak;
1706
10.0k
       }
1707
33.2k
#endif
1708
1709
       /* Worst cases:
1710
        * 2 frames: Code 2 with different compressed sizes
1711
        * >2 frames: Code 3 VBR */
1712
33.2k
       max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
1713
#ifdef ENABLE_QEXT
1714
       /* Cover the use of the separators that are the only thing that can get us over
1715
          once we consider that we need to subtract the extension overhead in each
1716
          of the individual frames. Also consider that a separator can get our padding
1717
          from 254 to 255, which costs an extra length byte (at most once). */
1718
       if (st->enable_qext) max_header_bytes += (nb_frames-1) + 1;
1719
#endif
1720
1721
33.2k
       if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
1722
21.1k
          repacketize_len = out_data_bytes;
1723
12.1k
       else {
1724
12.1k
          celt_assert(cbr_bytes>=0);
1725
12.1k
          repacketize_len = IMIN(cbr_bytes, out_data_bytes);
1726
12.1k
       }
1727
33.2k
       max_len_sum = nb_frames + repacketize_len - max_header_bytes;
1728
1729
33.2k
       ALLOC(tmp_data, max_len_sum, unsigned char);
1730
33.2k
       curr_data = tmp_data;
1731
33.2k
       ALLOC(rp, 1, OpusRepacketizer);
1732
33.2k
       opus_repacketizer_init(rp);
1733
1734
1735
33.2k
       bak_to_mono = st->silk_mode.toMono;
1736
33.2k
       if (bak_to_mono)
1737
0
          st->force_channels = 1;
1738
33.2k
       else
1739
33.2k
          st->prev_channels = st->stream_channels;
1740
1741
151k
       for (i=0;i<nb_frames;i++)
1742
118k
       {
1743
118k
          int first_frame;
1744
118k
          int frame_to_celt;
1745
118k
          int frame_redundancy;
1746
118k
          opus_int32 curr_max;
1747
          /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh,
1748
             that allows for DRED not to be in the first frame. */
1749
118k
          first_frame = (i == 0) || (i == dtx_count);
1750
118k
          st->silk_mode.toMono = 0;
1751
118k
          st->nonfinal_frame = i<(nb_frames-1);
1752
1753
          /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
1754
118k
          frame_to_celt = to_celt && i==nb_frames-1;
1755
118k
          frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0));
1756
1757
118k
          curr_max = IMIN(3*st->bitrate_bps/(3*8*st->Fs/enc_frame_size), max_len_sum/nb_frames);
1758
#ifdef ENABLE_DRED
1759
          curr_max = IMIN(curr_max, (max_len_sum-3*dred_bitrate_bps/(3*8*st->Fs/frame_size))/nb_frames);
1760
          if (first_frame) curr_max += 3*dred_bitrate_bps/(3*8*st->Fs/frame_size);
1761
#endif
1762
118k
          curr_max = IMIN(max_len_sum-tot_size, curr_max);
1763
118k
#ifndef DISABLE_FLOAT_API
1764
118k
          if (analysis_read_pos_bak != -1) {
1765
37.6k
            is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1766
            /* Get analysis for current frame. */
1767
37.6k
            tonality_get_info(&st->analysis, &analysis_info, enc_frame_size);
1768
37.6k
          }
1769
118k
#endif
1770
1771
118k
          tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame,
1772
#ifdef ENABLE_DRED
1773
          dred_bitrate_bps,
1774
#endif
1775
118k
#ifndef DISABLE_FLOAT_API
1776
118k
          &analysis_info,
1777
118k
          is_silence,
1778
118k
#endif
1779
118k
                    frame_redundancy, celt_to_silk, prefill,
1780
118k
                    equiv_rate, frame_to_celt
1781
118k
              );
1782
118k
          if (tmp_len<0)
1783
0
          {
1784
0
             RESTORE_STACK;
1785
0
             return OPUS_INTERNAL_ERROR;
1786
118k
          } else if (tmp_len==1) {
1787
0
             dtx_count++;
1788
0
          }
1789
118k
          ret = opus_repacketizer_cat(rp, curr_data, tmp_len);
1790
1791
118k
          if (ret<0)
1792
0
          {
1793
0
             RESTORE_STACK;
1794
0
             return OPUS_INTERNAL_ERROR;
1795
0
          }
1796
118k
          tot_size += tmp_len;
1797
118k
          curr_data += tmp_len;
1798
118k
       }
1799
33.2k
       ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL, 0);
1800
33.2k
       if (ret<0)
1801
0
       {
1802
0
          ret = OPUS_INTERNAL_ERROR;
1803
0
       }
1804
33.2k
       st->silk_mode.toMono = bak_to_mono;
1805
33.2k
       RESTORE_STACK;
1806
33.2k
       return ret;
1807
163k
    } else {
1808
163k
      ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1,
1809
#ifdef ENABLE_DRED
1810
                dred_bitrate_bps,
1811
#endif
1812
163k
#ifndef DISABLE_FLOAT_API
1813
163k
                &analysis_info,
1814
163k
                is_silence,
1815
163k
#endif
1816
163k
                redundancy, celt_to_silk, prefill,
1817
163k
                equiv_rate, to_celt
1818
163k
          );
1819
163k
      RESTORE_STACK;
1820
163k
      return ret;
1821
163k
    }
1822
196k
}
opus_encode_native
Line
Count
Source
1164
221k
{
1165
221k
    void *silk_enc=NULL;
1166
221k
    CELTEncoder *celt_enc=NULL;
1167
221k
    int i;
1168
221k
    int ret=0;
1169
221k
    int prefill=0;
1170
221k
    int redundancy = 0;
1171
221k
    int celt_to_silk = 0;
1172
221k
    int to_celt = 0;
1173
221k
    int voice_est; /* Probability of voice in Q7 */
1174
221k
    opus_int32 equiv_rate;
1175
221k
    int frame_rate;
1176
221k
    opus_int32 max_rate; /* Max bitrate we're allowed to use */
1177
221k
    int curr_bandwidth;
1178
221k
    opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1179
221k
    opus_int32 cbr_bytes=-1;
1180
221k
    opus_val16 stereo_width;
1181
221k
    const CELTMode *celt_mode=NULL;
1182
221k
    int packet_size_cap = 1276;
1183
221k
#ifndef DISABLE_FLOAT_API
1184
221k
    AnalysisInfo analysis_info;
1185
221k
    int analysis_read_pos_bak=-1;
1186
221k
    int analysis_read_subframe_bak=-1;
1187
221k
    int is_silence = 0;
1188
221k
#endif
1189
#ifdef ENABLE_DRED
1190
    opus_int32 dred_bitrate_bps;
1191
#endif
1192
221k
    ALLOC_STACK;
1193
1194
#ifdef ENABLE_QEXT
1195
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1196
#endif
1197
1198
    /* Just avoid insane packet sizes here, but the real bounds are applied later on. */
1199
221k
    max_data_bytes = IMIN(packet_size_cap*6, out_data_bytes);
1200
1201
221k
    st->rangeFinal = 0;
1202
221k
    if (frame_size <= 0 || max_data_bytes <= 0)
1203
0
    {
1204
0
       RESTORE_STACK;
1205
0
       return OPUS_BAD_ARG;
1206
0
    }
1207
1208
    /* Cannot encode 100 ms in 1 byte */
1209
221k
    if (max_data_bytes==1 && st->Fs==(frame_size*10))
1210
0
    {
1211
0
      RESTORE_STACK;
1212
0
      return OPUS_BUFFER_TOO_SMALL;
1213
0
    }
1214
1215
221k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1216
221k
        silk_enc = (char*)st+st->silk_enc_offset;
1217
221k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1218
221k
        celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1219
1220
221k
    lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1221
1222
221k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1223
221k
        celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1224
221k
#ifndef DISABLE_FLOAT_API
1225
221k
    analysis_info.valid = 0;
1226
221k
#ifdef FIXED_POINT
1227
221k
    if (st->silk_mode.complexity >= 10 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1228
#else
1229
    if (st->silk_mode.complexity >= 7 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_FORCED_SILK)
1230
#endif
1231
50.9k
    {
1232
50.9k
       is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1233
50.9k
       analysis_read_pos_bak = st->analysis.read_pos;
1234
50.9k
       analysis_read_subframe_bak = st->analysis.read_subframe;
1235
50.9k
       run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1236
50.9k
             c1, c2, analysis_channels, st->Fs,
1237
50.9k
             lsb_depth, downmix, &analysis_info);
1238
1239
       /* Track the peak signal energy */
1240
50.9k
       if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1241
30.9k
          st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),
1242
50.9k
                compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1243
170k
    } else if (st->analysis.initialized) {
1244
0
       tonality_analysis_reset(&st->analysis);
1245
0
    }
1246
#else
1247
    (void)analysis_pcm;
1248
    (void)analysis_size;
1249
    (void)c1;
1250
    (void)c2;
1251
    (void)analysis_channels;
1252
    (void)downmix;
1253
#endif
1254
1255
221k
#ifndef DISABLE_FLOAT_API
1256
    /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1257
     * Otherwise, preserve voice_ratio from the last non-silent frame */
1258
221k
    if (!is_silence)
1259
218k
      st->voice_ratio = -1;
1260
1261
221k
    st->detected_bandwidth = 0;
1262
221k
    if (analysis_info.valid)
1263
31.5k
    {
1264
31.5k
       int analysis_bandwidth;
1265
31.5k
       if (st->signal_type == OPUS_AUTO)
1266
21.8k
       {
1267
21.8k
          float prob;
1268
21.8k
          if (st->prev_mode == 0)
1269
21.8k
             prob = analysis_info.music_prob;
1270
0
          else if (st->prev_mode == MODE_CELT_ONLY)
1271
0
             prob = analysis_info.music_prob_max;
1272
0
          else
1273
0
             prob = analysis_info.music_prob_min;
1274
21.8k
          st->voice_ratio = (int)floor(.5+100*(1-prob));
1275
21.8k
       }
1276
1277
31.5k
       analysis_bandwidth = analysis_info.bandwidth;
1278
31.5k
       if (analysis_bandwidth<=12)
1279
0
          st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1280
31.5k
       else if (analysis_bandwidth<=14)
1281
0
          st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1282
31.5k
       else if (analysis_bandwidth<=16)
1283
0
          st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1284
31.5k
       else if (analysis_bandwidth<=18)
1285
0
          st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1286
31.5k
       else
1287
31.5k
          st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1288
31.5k
    }
1289
#else
1290
    st->voice_ratio = -1;
1291
#endif
1292
1293
221k
    if (st->channels==2 && st->force_channels!=1)
1294
63.4k
       stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1295
157k
    else
1296
157k
       stereo_width = 0;
1297
221k
    st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1298
1299
221k
    frame_rate = st->Fs/frame_size;
1300
221k
    if (!st->use_vbr)
1301
83.3k
    {
1302
       /* Multiply by 12 to make sure the division is exact. */
1303
83.3k
       int frame_rate12 = 12*st->Fs/frame_size;
1304
       /* We need to make sure that "int" values always fit in 16 bits. */
1305
83.3k
       cbr_bytes = IMIN( (12*st->bitrate_bps/8 + frame_rate12/2)/frame_rate12, max_data_bytes);
1306
83.3k
       st->bitrate_bps = cbr_bytes*(opus_int32)frame_rate12*8/12;
1307
       /* Make sure we provide at least one byte to avoid failing. */
1308
83.3k
       max_data_bytes = IMAX(1, cbr_bytes);
1309
83.3k
    }
1310
#ifdef ENABLE_DRED
1311
    /* Allocate some of the bits to DRED if needed. */
1312
    dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size);
1313
    st->bitrate_bps -= dred_bitrate_bps;
1314
#endif
1315
221k
    if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1316
205k
       || (frame_rate<50 && (max_data_bytes*(opus_int32)frame_rate<300 || st->bitrate_bps < 2400)))
1317
24.5k
    {
1318
       /*If the space is too low to do something useful, emit 'PLC' frames.*/
1319
24.5k
       int tocmode = st->mode;
1320
24.5k
       int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1321
24.5k
       int packet_code = 0;
1322
24.5k
       int num_multiframes = 0;
1323
1324
24.5k
       if (tocmode==0)
1325
0
          tocmode = MODE_SILK_ONLY;
1326
24.5k
       if (frame_rate>100)
1327
4.57k
          tocmode = MODE_CELT_ONLY;
1328
       /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1329
24.5k
       if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1330
2.13k
       {
1331
2.13k
          frame_rate = 50;
1332
2.13k
          packet_code = 1;
1333
2.13k
       }
1334
1335
       /* >= 60 ms frames */
1336
24.5k
       if (frame_rate<=16)
1337
9.27k
       {
1338
          /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1339
9.27k
          if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1340
2.47k
          {
1341
2.47k
             tocmode = MODE_SILK_ONLY;
1342
1343
2.47k
             packet_code = frame_rate <= 12;
1344
2.47k
             frame_rate = frame_rate == 12 ? 25 : 16;
1345
2.47k
          }
1346
6.79k
          else
1347
6.79k
          {
1348
6.79k
             num_multiframes = 50/frame_rate;
1349
6.79k
             frame_rate = 50;
1350
6.79k
             packet_code = 3;
1351
6.79k
          }
1352
9.27k
       }
1353
1354
24.5k
       if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1355
2.47k
          bw=OPUS_BANDWIDTH_WIDEBAND;
1356
22.0k
       else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1357
0
          bw=OPUS_BANDWIDTH_NARROWBAND;
1358
22.0k
       else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1359
0
          bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1360
1361
24.5k
       data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1362
24.5k
       data[0] |= packet_code;
1363
1364
24.5k
       ret = packet_code <= 1 ? 1 : 2;
1365
1366
24.5k
       max_data_bytes = IMAX(max_data_bytes, ret);
1367
1368
24.5k
       if (packet_code==3)
1369
6.79k
          data[1] = num_multiframes;
1370
1371
24.5k
       if (!st->use_vbr)
1372
7.70k
       {
1373
7.70k
          ret = opus_packet_pad(data, ret, max_data_bytes);
1374
7.70k
          if (ret == OPUS_OK)
1375
7.70k
             ret = max_data_bytes;
1376
0
          else
1377
0
             ret = OPUS_INTERNAL_ERROR;
1378
7.70k
       }
1379
24.5k
       RESTORE_STACK;
1380
24.5k
       return ret;
1381
24.5k
    }
1382
196k
    max_rate = frame_rate*max_data_bytes*8;
1383
1384
    /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1385
196k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1386
196k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1387
1388
196k
    if (st->signal_type == OPUS_SIGNAL_VOICE)
1389
35.4k
       voice_est = 127;
1390
161k
    else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1391
28.9k
       voice_est = 0;
1392
132k
    else if (st->voice_ratio >= 0)
1393
14.2k
    {
1394
14.2k
       voice_est = st->voice_ratio*327>>8;
1395
       /* For AUDIO, never be more than 90% confident of having speech */
1396
14.2k
       if (st->application == OPUS_APPLICATION_AUDIO)
1397
8.35k
          voice_est = IMIN(voice_est, 115);
1398
117k
    } else if (st->application == OPUS_APPLICATION_VOIP)
1399
26.7k
       voice_est = 115;
1400
91.0k
    else
1401
91.0k
       voice_est = 48;
1402
1403
196k
    if (st->force_channels!=OPUS_AUTO && st->channels == 2)
1404
32.6k
    {
1405
32.6k
        st->stream_channels = st->force_channels;
1406
164k
    } else {
1407
#ifdef FUZZING
1408
        (void)stereo_music_threshold;
1409
        (void)stereo_voice_threshold;
1410
       /* Random mono/stereo decision */
1411
       if (st->channels == 2 && (rand()&0x1F)==0)
1412
          st->stream_channels = 3-st->stream_channels;
1413
#else
1414
       /* Rate-dependent mono-stereo decision */
1415
164k
       if (st->channels == 2)
1416
41.5k
       {
1417
41.5k
          opus_int32 stereo_threshold;
1418
41.5k
          stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1419
41.5k
          if (st->stream_channels == 2)
1420
41.5k
             stereo_threshold -= 1000;
1421
0
          else
1422
0
             stereo_threshold += 1000;
1423
41.5k
          st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1424
122k
       } else {
1425
122k
          st->stream_channels = st->channels;
1426
122k
       }
1427
164k
#endif
1428
164k
    }
1429
    /* Update equivalent rate for channels decision. */
1430
196k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1431
196k
          st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1432
1433
    /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used,
1434
       e.g. because of the complexity setting or sample rate. */
1435
196k
#ifndef DISABLE_FLOAT_API
1436
196k
    st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1437
#else
1438
    st->silk_mode.useDTX = st->use_dtx;
1439
#endif
1440
1441
    /* Mode selection depending on application and signal type */
1442
196k
    if (st->application == OPUS_APPLICATION_FORCED_SILK)
1443
0
    {
1444
0
       st->mode = MODE_SILK_ONLY;
1445
196k
    } else if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1446
64.3k
    {
1447
64.3k
       st->mode = MODE_CELT_ONLY;
1448
132k
    } else if (st->user_forced_mode == OPUS_AUTO)
1449
123k
    {
1450
#ifdef FUZZING
1451
        (void)stereo_width;
1452
        (void)mode_thresholds;
1453
       /* Random mode switching */
1454
       if ((rand()&0xF)==0)
1455
       {
1456
          if ((rand()&0x1)==0)
1457
             st->mode = MODE_CELT_ONLY;
1458
          else
1459
             st->mode = MODE_SILK_ONLY;
1460
       } else {
1461
          if (st->prev_mode==MODE_CELT_ONLY)
1462
             st->mode = MODE_CELT_ONLY;
1463
          else
1464
             st->mode = MODE_SILK_ONLY;
1465
       }
1466
#else
1467
123k
       opus_int32 mode_voice, mode_music;
1468
123k
       opus_int32 threshold;
1469
1470
       /* Interpolate based on stereo width */
1471
123k
       mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1472
123k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1473
123k
       mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1474
123k
             + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1475
       /* Interpolate based on speech/music probability */
1476
123k
       threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1477
       /* Bias towards SILK for VoIP because of some useful features */
1478
123k
       if (st->application == OPUS_APPLICATION_VOIP)
1479
42.9k
          threshold += 8000;
1480
1481
       /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1482
       /* Hysteresis */
1483
123k
       if (st->prev_mode == MODE_CELT_ONLY)
1484
0
           threshold -= 4000;
1485
123k
       else if (st->prev_mode>0)
1486
0
           threshold += 4000;
1487
1488
123k
       st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1489
1490
       /* When FEC is enabled and there's enough packet loss, use SILK.
1491
          Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */
1492
123k
       if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25))
1493
59.6k
          st->mode = MODE_SILK_ONLY;
1494
       /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1495
          use SILK in order to make use of its DTX. */
1496
123k
       if (st->silk_mode.useDTX && voice_est > 100)
1497
19.9k
          st->mode = MODE_SILK_ONLY;
1498
123k
#endif
1499
1500
       /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1501
123k
       if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1502
1.07k
          st->mode = MODE_CELT_ONLY;
1503
123k
    } else {
1504
8.83k
       st->mode = st->user_forced_mode;
1505
8.83k
    }
1506
1507
    /* Override the chosen mode to make sure we meet the requested frame size */
1508
196k
    if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1509
16.0k
       st->mode = MODE_CELT_ONLY;
1510
196k
    if (st->lfe)
1511
327
       st->mode = MODE_CELT_ONLY;
1512
1513
196k
    if (st->prev_mode > 0 &&
1514
0
        ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1515
0
    (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1516
0
    {
1517
0
        redundancy = 1;
1518
0
        celt_to_silk = (st->mode != MODE_CELT_ONLY);
1519
0
        if (!celt_to_silk)
1520
0
        {
1521
            /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1522
0
            if (frame_size >= st->Fs/100)
1523
0
            {
1524
0
                st->mode = st->prev_mode;
1525
0
                to_celt = 1;
1526
0
            } else {
1527
0
                redundancy=0;
1528
0
            }
1529
0
        }
1530
0
    }
1531
1532
    /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1533
     * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1534
196k
    if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1535
0
          && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1536
0
    {
1537
       /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1538
0
       st->silk_mode.toMono = 1;
1539
0
       st->stream_channels = 2;
1540
196k
    } else {
1541
196k
       st->silk_mode.toMono = 0;
1542
196k
    }
1543
1544
    /* Update equivalent rate with mode decision. */
1545
196k
    equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1546
196k
          st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1547
1548
196k
    if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1549
0
    {
1550
0
        silk_EncControlStruct dummy;
1551
0
        silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy);
1552
0
        prefill=1;
1553
0
    }
1554
1555
    /* Automatic (rate-dependent) bandwidth selection */
1556
196k
    if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1557
196k
    {
1558
196k
        const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1559
196k
        opus_int32 bandwidth_thresholds[8];
1560
196k
        int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1561
1562
196k
        if (st->channels==2 && st->force_channels!=1)
1563
60.7k
        {
1564
60.7k
           voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1565
60.7k
           music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1566
135k
        } else {
1567
135k
           voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1568
135k
           music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1569
135k
        }
1570
        /* Interpolate bandwidth thresholds depending on voice estimation */
1571
1.76M
        for (i=0;i<8;i++)
1572
1.57M
        {
1573
1.57M
           bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1574
1.57M
                    + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1575
1.57M
        }
1576
233k
        do {
1577
233k
            int threshold, hysteresis;
1578
233k
            threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1579
233k
            hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1580
233k
            if (!st->first)
1581
0
            {
1582
0
                if (st->auto_bandwidth >= bandwidth)
1583
0
                    threshold -= hysteresis;
1584
0
                else
1585
0
                    threshold += hysteresis;
1586
0
            }
1587
233k
            if (equiv_rate >= threshold)
1588
194k
                break;
1589
233k
        } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1590
        /* We don't use mediumband anymore, except when explicitly requested or during
1591
           mode transitions. */
1592
196k
        if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1593
0
           bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1594
196k
        st->bandwidth = st->auto_bandwidth = bandwidth;
1595
        /* Prevents any transition to SWB/FB until the SILK layer has fully
1596
           switched to WB mode and turned the variable LP filter off */
1597
196k
        if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1598
0
            st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1599
196k
    }
1600
1601
196k
    if (st->bandwidth>st->max_bandwidth)
1602
145k
       st->bandwidth = st->max_bandwidth;
1603
1604
196k
    if (st->user_bandwidth != OPUS_AUTO)
1605
3.49k
        st->bandwidth = st->user_bandwidth;
1606
1607
    /* This prevents us from using hybrid at unsafe CBR/max rates */
1608
196k
    if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1609
2.05k
    {
1610
2.05k
       st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1611
2.05k
    }
1612
1613
    /* Prevents Opus from wasting bits on frequencies that are above
1614
       the Nyquist rate of the input signal */
1615
196k
    if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1616
36.8k
        st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1617
196k
    if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1618
47.9k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1619
196k
    if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1620
54.7k
        st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1621
196k
    if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1622
54.9k
        st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1623
196k
#ifndef DISABLE_FLOAT_API
1624
    /* Use detected bandwidth to reduce the encoded bandwidth. */
1625
196k
    if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1626
20.4k
    {
1627
20.4k
       int min_detected_bandwidth;
1628
       /* Makes bandwidth detection more conservative just in case the detector
1629
          gets it wrong when we could have coded a high bandwidth transparently.
1630
          When operating in SILK/hybrid mode, we don't go below wideband to avoid
1631
          more complicated switches that require redundancy. */
1632
20.4k
       if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1633
151
          min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1634
20.2k
       else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1635
31
          min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1636
20.2k
       else if (equiv_rate <= 30000*st->stream_channels)
1637
13.7k
          min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1638
6.49k
       else if (equiv_rate <= 44000*st->stream_channels)
1639
2.69k
          min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1640
3.79k
       else
1641
3.79k
          min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1642
1643
20.4k
       st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1644
20.4k
       st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1645
20.4k
    }
1646
196k
#endif
1647
196k
    st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1648
196k
          st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1649
196k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1650
196k
        celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1651
1652
    /* CELT mode doesn't support mediumband, use wideband instead */
1653
196k
    if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1654
11.6k
        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1655
196k
    if (st->lfe)
1656
327
       st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1657
1658
196k
    curr_bandwidth = st->bandwidth;
1659
1660
196k
    if (st->application == OPUS_APPLICATION_FORCED_SILK && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1661
0
       st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1662
    /* Chooses the appropriate mode for speech
1663
       *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1664
196k
    if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1665
5.55k
        st->mode = MODE_HYBRID;
1666
196k
    if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1667
0
        st->mode = MODE_SILK_ONLY;
1668
1669
    /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1670
196k
    if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1671
33.2k
    {
1672
33.2k
       int enc_frame_size;
1673
33.2k
       int nb_frames;
1674
33.2k
       VARDECL(unsigned char, tmp_data);
1675
33.2k
       VARDECL(OpusRepacketizer, rp);
1676
33.2k
       int max_header_bytes;
1677
33.2k
       opus_int32 repacketize_len;
1678
33.2k
       opus_int32 max_len_sum;
1679
33.2k
       opus_int32 tot_size=0;
1680
33.2k
       unsigned char *curr_data;
1681
33.2k
       int tmp_len;
1682
33.2k
       int dtx_count = 0;
1683
33.2k
       int bak_to_mono;
1684
1685
33.2k
       if (st->mode == MODE_SILK_ONLY)
1686
16.4k
       {
1687
16.4k
         if (frame_size == 2*st->Fs/25)  /* 80 ms -> 2x 40 ms */
1688
4.88k
           enc_frame_size = st->Fs/25;
1689
11.5k
         else if (frame_size == 3*st->Fs/25)  /* 120 ms -> 2x 60 ms */
1690
4.13k
           enc_frame_size = 3*st->Fs/50;
1691
7.45k
         else                            /* 100 ms -> 5x 20 ms */
1692
7.45k
           enc_frame_size = st->Fs/50;
1693
16.4k
       }
1694
16.7k
       else
1695
16.7k
         enc_frame_size = st->Fs/50;
1696
1697
33.2k
       nb_frames = frame_size/enc_frame_size;
1698
1699
33.2k
#ifndef DISABLE_FLOAT_API
1700
33.2k
       if (analysis_read_pos_bak!= -1)
1701
10.0k
       {
1702
          /* Reset analysis position to the beginning of the first frame so we
1703
             can use it one frame at a time. */
1704
10.0k
          st->analysis.read_pos = analysis_read_pos_bak;
1705
10.0k
          st->analysis.read_subframe = analysis_read_subframe_bak;
1706
10.0k
       }
1707
33.2k
#endif
1708
1709
       /* Worst cases:
1710
        * 2 frames: Code 2 with different compressed sizes
1711
        * >2 frames: Code 3 VBR */
1712
33.2k
       max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
1713
#ifdef ENABLE_QEXT
1714
       /* Cover the use of the separators that are the only thing that can get us over
1715
          once we consider that we need to subtract the extension overhead in each
1716
          of the individual frames. Also consider that a separator can get our padding
1717
          from 254 to 255, which costs an extra length byte (at most once). */
1718
       if (st->enable_qext) max_header_bytes += (nb_frames-1) + 1;
1719
#endif
1720
1721
33.2k
       if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
1722
21.1k
          repacketize_len = out_data_bytes;
1723
12.1k
       else {
1724
12.1k
          celt_assert(cbr_bytes>=0);
1725
12.1k
          repacketize_len = IMIN(cbr_bytes, out_data_bytes);
1726
12.1k
       }
1727
33.2k
       max_len_sum = nb_frames + repacketize_len - max_header_bytes;
1728
1729
33.2k
       ALLOC(tmp_data, max_len_sum, unsigned char);
1730
33.2k
       curr_data = tmp_data;
1731
33.2k
       ALLOC(rp, 1, OpusRepacketizer);
1732
33.2k
       opus_repacketizer_init(rp);
1733
1734
1735
33.2k
       bak_to_mono = st->silk_mode.toMono;
1736
33.2k
       if (bak_to_mono)
1737
0
          st->force_channels = 1;
1738
33.2k
       else
1739
33.2k
          st->prev_channels = st->stream_channels;
1740
1741
151k
       for (i=0;i<nb_frames;i++)
1742
118k
       {
1743
118k
          int first_frame;
1744
118k
          int frame_to_celt;
1745
118k
          int frame_redundancy;
1746
118k
          opus_int32 curr_max;
1747
          /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh,
1748
             that allows for DRED not to be in the first frame. */
1749
118k
          first_frame = (i == 0) || (i == dtx_count);
1750
118k
          st->silk_mode.toMono = 0;
1751
118k
          st->nonfinal_frame = i<(nb_frames-1);
1752
1753
          /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
1754
118k
          frame_to_celt = to_celt && i==nb_frames-1;
1755
118k
          frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0));
1756
1757
118k
          curr_max = IMIN(3*st->bitrate_bps/(3*8*st->Fs/enc_frame_size), max_len_sum/nb_frames);
1758
#ifdef ENABLE_DRED
1759
          curr_max = IMIN(curr_max, (max_len_sum-3*dred_bitrate_bps/(3*8*st->Fs/frame_size))/nb_frames);
1760
          if (first_frame) curr_max += 3*dred_bitrate_bps/(3*8*st->Fs/frame_size);
1761
#endif
1762
118k
          curr_max = IMIN(max_len_sum-tot_size, curr_max);
1763
118k
#ifndef DISABLE_FLOAT_API
1764
118k
          if (analysis_read_pos_bak != -1) {
1765
37.6k
            is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1766
            /* Get analysis for current frame. */
1767
37.6k
            tonality_get_info(&st->analysis, &analysis_info, enc_frame_size);
1768
37.6k
          }
1769
118k
#endif
1770
1771
118k
          tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame,
1772
#ifdef ENABLE_DRED
1773
          dred_bitrate_bps,
1774
#endif
1775
118k
#ifndef DISABLE_FLOAT_API
1776
118k
          &analysis_info,
1777
118k
          is_silence,
1778
118k
#endif
1779
118k
                    frame_redundancy, celt_to_silk, prefill,
1780
118k
                    equiv_rate, frame_to_celt
1781
118k
              );
1782
118k
          if (tmp_len<0)
1783
0
          {
1784
0
             RESTORE_STACK;
1785
0
             return OPUS_INTERNAL_ERROR;
1786
118k
          } else if (tmp_len==1) {
1787
0
             dtx_count++;
1788
0
          }
1789
118k
          ret = opus_repacketizer_cat(rp, curr_data, tmp_len);
1790
1791
118k
          if (ret<0)
1792
0
          {
1793
0
             RESTORE_STACK;
1794
0
             return OPUS_INTERNAL_ERROR;
1795
0
          }
1796
118k
          tot_size += tmp_len;
1797
118k
          curr_data += tmp_len;
1798
118k
       }
1799
33.2k
       ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL, 0);
1800
33.2k
       if (ret<0)
1801
0
       {
1802
0
          ret = OPUS_INTERNAL_ERROR;
1803
0
       }
1804
33.2k
       st->silk_mode.toMono = bak_to_mono;
1805
33.2k
       RESTORE_STACK;
1806
33.2k
       return ret;
1807
163k
    } else {
1808
163k
      ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1,
1809
#ifdef ENABLE_DRED
1810
                dred_bitrate_bps,
1811
#endif
1812
163k
#ifndef DISABLE_FLOAT_API
1813
163k
                &analysis_info,
1814
163k
                is_silence,
1815
163k
#endif
1816
163k
                redundancy, celt_to_silk, prefill,
1817
163k
                equiv_rate, to_celt
1818
163k
          );
1819
163k
      RESTORE_STACK;
1820
163k
      return ret;
1821
163k
    }
1822
196k
}
1823
1824
static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1825
                unsigned char *data, opus_int32 orig_max_data_bytes,
1826
                int float_api, int first_frame,
1827
#ifdef ENABLE_DRED
1828
                opus_int32 dred_bitrate_bps,
1829
#endif
1830
#ifndef DISABLE_FLOAT_API
1831
                AnalysisInfo *analysis_info, int is_silence,
1832
#endif
1833
                int redundancy, int celt_to_silk, int prefill,
1834
                opus_int32 equiv_rate, int to_celt)
1835
579k
{
1836
579k
    void *silk_enc=NULL;
1837
579k
    CELTEncoder *celt_enc=NULL;
1838
579k
    const CELTMode *celt_mode=NULL;
1839
579k
    int i;
1840
579k
    int ret=0;
1841
579k
    int max_data_bytes;
1842
579k
    opus_int32 nBytes;
1843
579k
    ec_enc enc;
1844
579k
    int bytes_target;
1845
579k
    int start_band = 0;
1846
579k
    int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1847
579k
    int nb_compr_bytes;
1848
579k
    opus_uint32 redundant_rng = 0;
1849
579k
    int cutoff_Hz;
1850
579k
    int hp_freq_smth1;
1851
579k
    opus_val16 HB_gain;
1852
579k
    int apply_padding;
1853
579k
    int frame_rate;
1854
579k
    int curr_bandwidth;
1855
579k
    int delay_compensation;
1856
579k
    int total_buffer;
1857
579k
    opus_int activity = VAD_NO_DECISION;
1858
579k
    VARDECL(opus_res, pcm_buf);
1859
579k
    VARDECL(opus_res, tmp_prefill);
1860
579k
    SAVE_STACK;
1861
1862
579k
    max_data_bytes = IMIN(orig_max_data_bytes, 1276);
1863
579k
    st->rangeFinal = 0;
1864
579k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1865
579k
       silk_enc = (char*)st+st->silk_enc_offset;
1866
579k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1867
579k
    {
1868
579k
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1869
579k
       celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1870
579k
    }
1871
579k
    curr_bandwidth = st->bandwidth;
1872
579k
    if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1873
156k
       delay_compensation = 0;
1874
422k
    else
1875
422k
       delay_compensation = st->delay_compensation;
1876
579k
    total_buffer = delay_compensation;
1877
1878
579k
    frame_rate = st->Fs/frame_size;
1879
1880
579k
#ifndef DISABLE_FLOAT_API
1881
579k
    if (is_silence)
1882
6.72k
    {
1883
6.72k
       activity = !is_silence;
1884
572k
    } else if (analysis_info->valid)
1885
87.1k
    {
1886
87.1k
       activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD;
1887
87.1k
       if (!activity)
1888
6.92k
       {
1889
           /* Mark as active if this noise frame is sufficiently loud */
1890
6.92k
           opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1891
6.92k
           activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy);
1892
6.92k
       }
1893
87.1k
    }
1894
579k
#endif
1895
1896
    /* For the first frame at a new SILK bandwidth */
1897
579k
    if (st->silk_bw_switch)
1898
0
    {
1899
0
       redundancy = 1;
1900
0
       celt_to_silk = 1;
1901
0
       st->silk_bw_switch = 0;
1902
       /* Do a prefill without resetting the sampling rate control. */
1903
0
       prefill=2;
1904
0
    }
1905
1906
    /* If we decided to go with CELT, make sure redundancy is off, no matter what
1907
       we decided earlier. */
1908
579k
    if (st->mode == MODE_CELT_ONLY)
1909
315k
        redundancy = 0;
1910
1911
579k
    if (redundancy)
1912
0
    {
1913
0
       redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1914
0
       if (redundancy_bytes == 0)
1915
0
          redundancy = 0;
1916
0
    }
1917
1918
    /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1919
579k
    bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1920
1921
579k
    data += 1;
1922
1923
579k
    ec_enc_init(&enc, data, orig_max_data_bytes-1);
1924
1925
579k
    ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res);
1926
579k
    OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1927
1928
579k
    if (st->mode == MODE_CELT_ONLY)
1929
315k
       hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1930
263k
    else
1931
263k
       hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1932
1933
579k
    st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1934
579k
          hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1935
1936
    /* convert from log scale to Hertz */
1937
579k
    cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1938
1939
579k
    if (st->application == OPUS_APPLICATION_VOIP)
1940
151k
    {
1941
151k
       hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
1942
1943
#ifdef ENABLE_OSCE_TRAINING_DATA
1944
       /* write out high pass filtered clean signal*/
1945
       static FILE *fout =NULL;
1946
       if (fout == NULL)
1947
       {
1948
         fout = fopen("clean_hp.s16", "wb");
1949
       }
1950
1951
       {
1952
         int idx;
1953
         opus_int16 tmp;
1954
         for (idx = 0; idx < frame_size; idx++)
1955
         {
1956
            tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f);
1957
            fwrite(&tmp, sizeof(tmp), 1, fout);
1958
         }
1959
       }
1960
#endif
1961
427k
    } else {
1962
#ifdef ENABLE_QEXT
1963
       /* FIXME: Avoid glitching when we switch qext on/off dynamically. */
1964
216k
       if (st->enable_qext) OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels);
1965
216k
       else
1966
216k
#endif
1967
216k
       dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1968
427k
    }
1969
#ifndef FIXED_POINT
1970
279k
    if (float_api)
1971
0
    {
1972
0
       opus_val32 sum;
1973
0
       sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1974
       /* This should filter out both NaNs and ridiculous signals that could
1975
          cause NaNs further down. */
1976
0
       if (!(sum < 1e9f) || celt_isnan(sum))
1977
0
       {
1978
0
          OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1979
0
          st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1980
0
       }
1981
0
    }
1982
#else
1983
    (void)float_api;
1984
#endif
1985
1986
#ifdef ENABLE_DRED
1987
    if ( st->dred_duration > 0 && st->dred_encoder.loaded ) {
1988
        int frame_size_400Hz;
1989
        /* DRED Encoder */
1990
        dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch );
1991
        frame_size_400Hz = frame_size*400/st->Fs;
1992
        OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz);
1993
        for (i=0;i<frame_size_400Hz;i++)
1994
           st->activity_mem[i] = activity;
1995
    } else {
1996
        st->dred_encoder.latents_buffer_fill = 0;
1997
        OPUS_CLEAR(st->activity_mem, DRED_MAX_FRAMES);
1998
    }
1999
#endif
2000
2001
    /* SILK processing */
2002
579k
    HB_gain = Q15ONE;
2003
579k
    if (st->mode != MODE_CELT_ONLY)
2004
263k
    {
2005
263k
        opus_int32 total_bitRate, celt_rate;
2006
263k
        const opus_res *pcm_silk;
2007
2008
        /* Distribute bits between SILK and CELT */
2009
263k
        total_bitRate = 8 * bytes_target * frame_rate;
2010
263k
        if( st->mode == MODE_HYBRID ) {
2011
            /* Base rate for SILK */
2012
18.4k
            st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
2013
18.4k
                  curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2014
18.4k
                  st->stream_channels);
2015
18.4k
            if (!st->energy_masking)
2016
18.4k
            {
2017
               /* Increasingly attenuate high band when it gets allocated fewer bits */
2018
18.4k
               celt_rate = total_bitRate - st->silk_mode.bitRate;
2019
18.4k
               HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
2020
18.4k
            }
2021
244k
        } else {
2022
            /* SILK gets all bits */
2023
244k
            st->silk_mode.bitRate = total_bitRate;
2024
244k
        }
2025
2026
        /* Surround masking for SILK */
2027
263k
        if (st->energy_masking && st->use_vbr && !st->lfe)
2028
676
        {
2029
676
           opus_val32 mask_sum=0;
2030
676
           celt_glog masking_depth;
2031
676
           opus_int32 rate_offset;
2032
676
           int c;
2033
676
           int end = 17;
2034
676
           opus_int16 srate = 16000;
2035
676
           if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
2036
676
           {
2037
676
              end = 13;
2038
676
              srate = 8000;
2039
676
           } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
2040
0
           {
2041
0
              end = 15;
2042
0
              srate = 12000;
2043
0
           }
2044
1.35k
           for (c=0;c<st->channels;c++)
2045
676
           {
2046
9.46k
              for(i=0;i<end;i++)
2047
8.78k
              {
2048
8.78k
                 celt_glog mask;
2049
8.78k
                 mask = MAXG(MING(st->energy_masking[21*c+i],
2050
8.78k
                        GCONST(.5f)), -GCONST(2.0f));
2051
8.78k
                 if (mask > 0)
2052
3.27k
                    mask = HALF32(mask);
2053
8.78k
                 mask_sum += mask;
2054
8.78k
              }
2055
676
           }
2056
           /* Conservative rate reduction, we cut the masking in half */
2057
676
           masking_depth = mask_sum / end*st->channels;
2058
676
           masking_depth += GCONST(.2f);
2059
676
           rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10);
2060
676
           rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
2061
           /* Split the rate change between the SILK and CELT part for hybrid. */
2062
676
           if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
2063
0
              st->silk_mode.bitRate += 3*rate_offset/5;
2064
676
           else
2065
676
              st->silk_mode.bitRate += rate_offset;
2066
676
        }
2067
2068
263k
        st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
2069
263k
        st->silk_mode.nChannelsAPI = st->channels;
2070
263k
        st->silk_mode.nChannelsInternal = st->stream_channels;
2071
263k
        if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2072
186k
            st->silk_mode.desiredInternalSampleRate = 8000;
2073
186k
        } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2074
24.1k
            st->silk_mode.desiredInternalSampleRate = 12000;
2075
52.3k
        } else {
2076
52.3k
            celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
2077
52.3k
            st->silk_mode.desiredInternalSampleRate = 16000;
2078
52.3k
        }
2079
263k
        if( st->mode == MODE_HYBRID ) {
2080
            /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
2081
18.4k
            st->silk_mode.minInternalSampleRate = 16000;
2082
244k
        } else {
2083
244k
            st->silk_mode.minInternalSampleRate = 8000;
2084
244k
        }
2085
2086
263k
        st->silk_mode.maxInternalSampleRate = 16000;
2087
263k
        if (st->mode == MODE_SILK_ONLY)
2088
244k
        {
2089
244k
           opus_int32 effective_max_rate = frame_rate*max_data_bytes*8;
2090
244k
           if (frame_rate > 50)
2091
86.5k
              effective_max_rate = effective_max_rate*2/3;
2092
244k
           if (effective_max_rate < 8000)
2093
4.84k
           {
2094
4.84k
              st->silk_mode.maxInternalSampleRate = 12000;
2095
4.84k
              st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
2096
4.84k
           }
2097
244k
           if (effective_max_rate < 7000)
2098
3.28k
           {
2099
3.28k
              st->silk_mode.maxInternalSampleRate = 8000;
2100
3.28k
              st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
2101
3.28k
           }
2102
#ifdef ENABLE_QEXT
2103
           /* At 96 kHz, we don't have the input resampler to do 8 or 12 kHz. */
2104
126k
           if (st->Fs==96000) st->silk_mode.maxInternalSampleRate = st->silk_mode.desiredInternalSampleRate = 16000;
2105
#endif
2106
244k
        }
2107
2108
263k
        st->silk_mode.useCBR = !st->use_vbr;
2109
2110
        /* Call SILK encoder for the low band */
2111
2112
        /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
2113
263k
        st->silk_mode.maxBits = (max_data_bytes-1)*8;
2114
263k
        if (redundancy && redundancy_bytes >= 2)
2115
0
        {
2116
           /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
2117
0
           st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
2118
0
           if (st->mode == MODE_HYBRID)
2119
0
              st->silk_mode.maxBits -= 20;
2120
0
        }
2121
263k
        if (st->silk_mode.useCBR)
2122
136k
        {
2123
           /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to
2124
              save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still
2125
              produce a constant bitrate without wasting bits. */
2126
#ifdef ENABLE_DRED
2127
           if (st->mode == MODE_HYBRID || dred_bitrate_bps > 0)
2128
#else
2129
136k
           if (st->mode == MODE_HYBRID)
2130
5.17k
#endif
2131
5.17k
           {
2132
              /* Allow SILK to steal up to 25% of the remaining bits */
2133
5.17k
              opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs);
2134
5.17k
              st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4);
2135
5.17k
              st->silk_mode.useCBR = 0;
2136
5.17k
           }
2137
136k
        } else {
2138
           /* Constrained VBR. */
2139
126k
           if (st->mode == MODE_HYBRID)
2140
13.2k
           {
2141
              /* Compute SILK bitrate corresponding to the max total bits available */
2142
13.2k
              opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
2143
13.2k
                    curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2144
13.2k
                    st->stream_channels);
2145
13.2k
              st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
2146
13.2k
           }
2147
126k
        }
2148
2149
263k
        if (prefill)
2150
0
        {
2151
0
            opus_int32 zero=0;
2152
0
            int prefill_offset;
2153
            /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
2154
               a discontinuity. The exact location is what we need to avoid leaving any "gap"
2155
               in the audio when mixing with the redundant CELT frame. Here we can afford to
2156
               overwrite st->delay_buffer because the only thing that uses it before it gets
2157
               rewritten is tmp_prefill[] and even then only the part after the ramp really
2158
               gets used (rather than sent to the encoder and discarded) */
2159
0
            prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
2160
0
            gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
2161
0
                  0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
2162
0
            OPUS_CLEAR(st->delay_buffer, prefill_offset);
2163
0
            pcm_silk = st->delay_buffer;
2164
0
            silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity );
2165
            /* Prevent a second switch in the real encode call. */
2166
0
            st->silk_mode.opusCanSwitch = 0;
2167
0
        }
2168
2169
263k
        pcm_silk = pcm_buf+total_buffer*st->channels;
2170
263k
        ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
2171
263k
        if( ret ) {
2172
            /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
2173
            /* Handle error */
2174
0
           RESTORE_STACK;
2175
0
           return OPUS_INTERNAL_ERROR;
2176
0
        }
2177
2178
        /* Extract SILK internal bandwidth for signaling in first byte */
2179
263k
        if( st->mode == MODE_SILK_ONLY ) {
2180
244k
            if( st->silk_mode.internalSampleRate == 8000 ) {
2181
187k
               curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
2182
187k
            } else if( st->silk_mode.internalSampleRate == 12000 ) {
2183
24.0k
               curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
2184
33.4k
            } else if( st->silk_mode.internalSampleRate == 16000 ) {
2185
33.4k
               curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
2186
33.4k
            }
2187
244k
        } else {
2188
18.4k
            celt_assert( st->silk_mode.internalSampleRate == 16000 );
2189
18.4k
        }
2190
2191
263k
        st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
2192
2193
263k
        if (nBytes==0)
2194
0
        {
2195
0
           st->rangeFinal = 0;
2196
0
           data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2197
0
           RESTORE_STACK;
2198
0
           return 1;
2199
0
        }
2200
2201
        /* FIXME: How do we allocate the redundancy for CBR? */
2202
263k
        if (st->silk_mode.opusCanSwitch)
2203
0
        {
2204
0
           redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
2205
0
           redundancy = (redundancy_bytes != 0);
2206
0
           celt_to_silk = 0;
2207
0
           st->silk_bw_switch = 1;
2208
0
        }
2209
263k
    }
2210
2211
    /* CELT processing */
2212
579k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2213
579k
    {
2214
579k
        int endband=21;
2215
2216
579k
        switch(curr_bandwidth)
2217
579k
        {
2218
409k
            case OPUS_BANDWIDTH_NARROWBAND:
2219
409k
                endband = 13;
2220
409k
                break;
2221
24.0k
            case OPUS_BANDWIDTH_MEDIUMBAND:
2222
116k
            case OPUS_BANDWIDTH_WIDEBAND:
2223
116k
                endband = 17;
2224
116k
                break;
2225
37.2k
            case OPUS_BANDWIDTH_SUPERWIDEBAND:
2226
37.2k
                endband = 19;
2227
37.2k
                break;
2228
16.7k
            case OPUS_BANDWIDTH_FULLBAND:
2229
16.7k
                endband = 21;
2230
16.7k
                break;
2231
579k
        }
2232
579k
        celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
2233
579k
        celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
2234
579k
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2235
579k
    }
2236
579k
    if (st->mode != MODE_SILK_ONLY)
2237
334k
    {
2238
334k
        opus_val32 celt_pred=2;
2239
        /* We may still decide to disable prediction later */
2240
334k
        if (st->silk_mode.reducedDependency)
2241
88.2k
           celt_pred = 0;
2242
334k
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
2243
334k
    }
2244
2245
579k
    ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res);
2246
579k
    if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
2247
0
    {
2248
0
       OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
2249
0
    }
2250
2251
579k
    if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
2252
167k
    {
2253
167k
       OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
2254
167k
       OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
2255
167k
             &pcm_buf[0],
2256
167k
             (frame_size+total_buffer)*st->channels);
2257
411k
    } else {
2258
411k
       OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
2259
411k
    }
2260
    /* gain_fade() and stereo_fade() need to be after the buffer copying
2261
       because we don't want any of this to affect the SILK part */
2262
579k
    if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
2263
15.0k
       gain_fade(pcm_buf, pcm_buf,
2264
15.0k
             st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
2265
15.0k
    }
2266
579k
    st->prev_HB_gain = HB_gain;
2267
579k
    if (st->mode != MODE_HYBRID || st->stream_channels==1)
2268
571k
    {
2269
571k
       if (equiv_rate > 32000)
2270
196k
          st->silk_mode.stereoWidth_Q14 = 16384;
2271
375k
       else if (equiv_rate < 16000)
2272
139k
          st->silk_mode.stereoWidth_Q14 = 0;
2273
235k
       else
2274
235k
          st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
2275
571k
    }
2276
579k
    if( !st->energy_masking && st->channels == 2 ) {
2277
        /* Apply stereo width reduction (at low bitrates) */
2278
235k
        if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
2279
71.4k
            opus_val16 g1, g2;
2280
71.4k
            g1 = st->hybrid_stereo_width_Q14;
2281
71.4k
            g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
2282
#ifdef FIXED_POINT
2283
38.6k
            g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
2284
38.6k
            g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
2285
#else
2286
            g1 *= (1.f/16384);
2287
            g2 *= (1.f/16384);
2288
#endif
2289
71.4k
            stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
2290
71.4k
                  frame_size, st->channels, celt_mode->window, st->Fs);
2291
71.4k
            st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
2292
71.4k
        }
2293
235k
    }
2294
2295
579k
    if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
2296
223k
    {
2297
        /* For SILK mode, the redundancy is inferred from the length */
2298
223k
        if (st->mode == MODE_HYBRID)
2299
17.9k
           ec_enc_bit_logp(&enc, redundancy, 12);
2300
223k
        if (redundancy)
2301
0
        {
2302
0
            int max_redundancy;
2303
0
            ec_enc_bit_logp(&enc, celt_to_silk, 1);
2304
0
            if (st->mode == MODE_HYBRID)
2305
0
            {
2306
               /* Reserve the 8 bits needed for the redundancy length,
2307
                  and at least a few bits for CELT if possible */
2308
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
2309
0
            }
2310
0
            else
2311
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2312
            /* Target the same bit-rate for redundancy as for the rest,
2313
               up to a max of 257 bytes */
2314
0
            redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
2315
0
            redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2316
0
            if (st->mode == MODE_HYBRID)
2317
0
                ec_enc_uint(&enc, redundancy_bytes-2, 256);
2318
0
        }
2319
355k
    } else {
2320
355k
        redundancy = 0;
2321
355k
    }
2322
2323
579k
    if (!redundancy)
2324
579k
    {
2325
579k
       st->silk_bw_switch = 0;
2326
579k
       redundancy_bytes = 0;
2327
579k
    }
2328
579k
    if (st->mode != MODE_CELT_ONLY)start_band=17;
2329
2330
579k
    if (st->mode == MODE_SILK_ONLY)
2331
244k
    {
2332
244k
        ret = (ec_tell(&enc)+7)>>3;
2333
244k
        ec_enc_done(&enc);
2334
244k
        nb_compr_bytes = ret;
2335
334k
    } else {
2336
334k
        nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2337
#ifdef ENABLE_QEXT
2338
170k
        if (st->mode == MODE_CELT_ONLY && st->enable_qext) {
2339
0
           celt_assert(redundancy_bytes==0);
2340
0
           nb_compr_bytes = orig_max_data_bytes-1;
2341
0
        }
2342
170k
#endif
2343
#ifdef ENABLE_DRED
2344
        if (st->dred_duration > 0)
2345
        {
2346
            int max_celt_bytes;
2347
            opus_int32 dred_bytes = dred_bitrate_bps/(frame_rate*8);
2348
            /* Allow CELT to steal up to 25% of the remaining bits. */
2349
            max_celt_bytes = nb_compr_bytes - dred_bytes*3/4;
2350
            /* But try to give CELT at least 5 bytes to prevent a mismatch with
2351
               the redundancy signaling. */
2352
            max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes);
2353
            /* Subject to the original max. */
2354
            nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes);
2355
        }
2356
#endif
2357
170k
        ec_enc_shrink(&enc, nb_compr_bytes);
2358
170k
    }
2359
2360
297k
#ifndef DISABLE_FLOAT_API
2361
579k
    if (redundancy || st->mode != MODE_SILK_ONLY)
2362
334k
       celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info));
2363
297k
#endif
2364
579k
    if (st->mode == MODE_HYBRID) {
2365
18.4k
       SILKInfo info;
2366
18.4k
       info.signalType = st->silk_mode.signalType;
2367
18.4k
       info.offset = st->silk_mode.offset;
2368
18.4k
       celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2369
18.4k
    }
2370
2371
    /* 5 ms redundant frame for CELT->SILK */
2372
579k
    if (redundancy && celt_to_silk)
2373
0
    {
2374
0
        int err;
2375
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2376
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2377
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2378
0
        err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2379
0
        if (err < 0)
2380
0
        {
2381
0
           RESTORE_STACK;
2382
0
           return OPUS_INTERNAL_ERROR;
2383
0
        }
2384
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2385
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2386
0
    }
2387
2388
579k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2389
579k
       celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2390
2391
579k
    data[-1] = 0;
2392
579k
    if (st->mode != MODE_SILK_ONLY)
2393
334k
    {
2394
334k
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2395
334k
        if (st->mode == MODE_HYBRID)
2396
18.4k
        {
2397
18.4k
            if( st->use_vbr ) {
2398
13.2k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2399
13.2k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2400
13.2k
            }
2401
315k
        } else {
2402
315k
            if (st->use_vbr)
2403
235k
            {
2404
235k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2405
235k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
2406
235k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
2407
235k
            }
2408
315k
        }
2409
#ifdef ENABLE_DRED
2410
        /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */
2411
        if (!st->use_vbr && st->dred_duration > 0)
2412
        {
2413
            opus_int32 celt_bitrate = st->bitrate_bps;
2414
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2415
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2416
            if (st->mode == MODE_HYBRID) {
2417
                celt_bitrate -= st->silk_mode.bitRate;
2418
            }
2419
            celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate));
2420
        }
2421
#endif
2422
334k
        if (st->mode != st->prev_mode && st->prev_mode > 0)
2423
0
        {
2424
0
           unsigned char dummy[2];
2425
0
           celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2426
2427
           /* Prefilling */
2428
0
           celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2429
0
           celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2430
0
        }
2431
        /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2432
334k
        if (ec_tell(&enc) <= 8*nb_compr_bytes)
2433
333k
        {
2434
#ifdef ENABLE_QEXT
2435
170k
           if (st->mode == MODE_CELT_ONLY) celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(st->enable_qext));
2436
#endif
2437
333k
           ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2438
#ifdef ENABLE_QEXT
2439
170k
           celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(0));
2440
#endif
2441
333k
           if (ret < 0)
2442
0
           {
2443
0
              RESTORE_STACK;
2444
0
              return OPUS_INTERNAL_ERROR;
2445
0
           }
2446
           /* Put CELT->SILK redundancy data in the right place. */
2447
333k
           if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && nb_compr_bytes != ret)
2448
0
           {
2449
0
              OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2450
0
              nb_compr_bytes = ret+redundancy_bytes;
2451
0
           }
2452
333k
        }
2453
334k
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&st->rangeFinal));
2454
334k
    } else {
2455
244k
       st->rangeFinal = enc.rng;
2456
244k
    }
2457
2458
    /* 5 ms redundant frame for SILK->CELT */
2459
579k
    if (redundancy && !celt_to_silk)
2460
0
    {
2461
0
        int err;
2462
0
        unsigned char dummy[2];
2463
0
        int N2, N4;
2464
0
        N2 = st->Fs/200;
2465
0
        N4 = st->Fs/400;
2466
2467
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2468
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2469
0
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2470
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2471
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2472
2473
0
        if (st->mode == MODE_HYBRID)
2474
0
        {
2475
           /* Shrink packet to what the encoder actually used. */
2476
0
           nb_compr_bytes = ret;
2477
0
           ec_enc_shrink(&enc, nb_compr_bytes);
2478
0
        }
2479
        /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2480
0
        celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2481
2482
0
        err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2483
0
        if (err < 0)
2484
0
        {
2485
0
           RESTORE_STACK;
2486
0
           return OPUS_INTERNAL_ERROR;
2487
0
        }
2488
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2489
0
    }
2490
2491
2492
2493
    /* Signalling the mode in the first byte */
2494
579k
    data--;
2495
579k
    data[0] |= gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2496
2497
579k
    st->rangeFinal ^= redundant_rng;
2498
2499
579k
    if (to_celt)
2500
0
        st->prev_mode = MODE_CELT_ONLY;
2501
579k
    else
2502
579k
        st->prev_mode = st->mode;
2503
579k
    st->prev_channels = st->stream_channels;
2504
579k
    st->prev_framesize = frame_size;
2505
2506
579k
    st->first = 0;
2507
2508
    /* DTX decision */
2509
579k
#ifndef DISABLE_FLOAT_API
2510
579k
    if (st->use_dtx && (analysis_info->valid || is_silence))
2511
40.7k
    {
2512
40.7k
       if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs))
2513
0
       {
2514
0
          st->rangeFinal = 0;
2515
0
          data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2516
0
          RESTORE_STACK;
2517
0
          return 1;
2518
0
       }
2519
538k
    } else {
2520
538k
       st->nb_no_activity_ms_Q1 = 0;
2521
538k
    }
2522
579k
#endif
2523
2524
    /* In the unlikely case that the SILK encoder busted its target, tell
2525
       the decoder to call the PLC */
2526
579k
    if (ec_tell(&enc) > (max_data_bytes-1)*8)
2527
7.93k
    {
2528
7.93k
       if (max_data_bytes < 2)
2529
0
       {
2530
0
          RESTORE_STACK;
2531
0
          return OPUS_BUFFER_TOO_SMALL;
2532
0
       }
2533
7.93k
       data[1] = 0;
2534
7.93k
       ret = 1;
2535
7.93k
       st->rangeFinal = 0;
2536
571k
    } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2537
237k
    {
2538
       /*When in LPC only mode it's perfectly
2539
         reasonable to strip off trailing zero bytes as
2540
         the required range decoder behavior is to
2541
         fill these in. This can't be done when the MDCT
2542
         modes are used because the decoder needs to know
2543
         the actual length for allocation purposes.*/
2544
276k
       while(ret>2&&data[ret]==0)ret--;
2545
237k
    }
2546
    /* Count ToC and redundancy */
2547
579k
    ret += 1+redundancy_bytes;
2548
579k
    apply_padding = !st->use_vbr;
2549
#ifdef ENABLE_DRED
2550
    if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) {
2551
       opus_extension_data extension;
2552
       unsigned char buf[DRED_MAX_DATA_SIZE];
2553
       int dred_chunks;
2554
       int dred_bytes_left;
2555
       dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2);
2556
       if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks);
2557
       /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */
2558
       dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-3);
2559
       /* Account for the extra bytes required to signal large padding length. */
2560
       dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255;
2561
       /* Check whether we actually have something to encode. */
2562
       if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) {
2563
           int dred_bytes;
2564
#ifdef DRED_EXPERIMENTAL_VERSION
2565
           /* Add temporary extension type and version.
2566
              These bytes will be removed once extension is finalized. */
2567
           buf[0] = 'D';
2568
           buf[1] = DRED_EXPERIMENTAL_VERSION;
2569
#endif
2570
           dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES,
2571
                                               st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch);
2572
           if (dred_bytes > 0) {
2573
              dred_bytes += DRED_EXPERIMENTAL_BYTES;
2574
              celt_assert(dred_bytes <= dred_bytes_left);
2575
              extension.id = DRED_EXTENSION_ID;
2576
              extension.frame = 0;
2577
              extension.data = buf;
2578
              extension.len = dred_bytes;
2579
              ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1);
2580
              if (ret < 0)
2581
              {
2582
                 RESTORE_STACK;
2583
                 return OPUS_INTERNAL_ERROR;
2584
              }
2585
              apply_padding = 0;
2586
           }
2587
       }
2588
    }
2589
#else
2590
579k
    (void)first_frame; /* Avoids a warning about first_frame being unused. */
2591
579k
#endif
2592
579k
    if (apply_padding)
2593
217k
    {
2594
217k
       if (opus_packet_pad(data, ret, orig_max_data_bytes) != OPUS_OK)
2595
0
       {
2596
0
          RESTORE_STACK;
2597
0
          return OPUS_INTERNAL_ERROR;
2598
0
       }
2599
217k
       ret = orig_max_data_bytes;
2600
217k
    }
2601
579k
    RESTORE_STACK;
2602
579k
    return ret;
2603
579k
}
opus_encoder.c:opus_encode_frame_native
Line
Count
Source
1835
153k
{
1836
153k
    void *silk_enc=NULL;
1837
153k
    CELTEncoder *celt_enc=NULL;
1838
153k
    const CELTMode *celt_mode=NULL;
1839
153k
    int i;
1840
153k
    int ret=0;
1841
153k
    int max_data_bytes;
1842
153k
    opus_int32 nBytes;
1843
153k
    ec_enc enc;
1844
153k
    int bytes_target;
1845
153k
    int start_band = 0;
1846
153k
    int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1847
153k
    int nb_compr_bytes;
1848
153k
    opus_uint32 redundant_rng = 0;
1849
153k
    int cutoff_Hz;
1850
153k
    int hp_freq_smth1;
1851
153k
    opus_val16 HB_gain;
1852
153k
    int apply_padding;
1853
153k
    int frame_rate;
1854
153k
    int curr_bandwidth;
1855
153k
    int delay_compensation;
1856
153k
    int total_buffer;
1857
153k
    opus_int activity = VAD_NO_DECISION;
1858
153k
    VARDECL(opus_res, pcm_buf);
1859
153k
    VARDECL(opus_res, tmp_prefill);
1860
153k
    SAVE_STACK;
1861
1862
153k
    max_data_bytes = IMIN(orig_max_data_bytes, 1276);
1863
153k
    st->rangeFinal = 0;
1864
153k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1865
153k
       silk_enc = (char*)st+st->silk_enc_offset;
1866
153k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1867
153k
    {
1868
153k
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1869
153k
       celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1870
153k
    }
1871
153k
    curr_bandwidth = st->bandwidth;
1872
153k
    if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1873
43.1k
       delay_compensation = 0;
1874
109k
    else
1875
109k
       delay_compensation = st->delay_compensation;
1876
153k
    total_buffer = delay_compensation;
1877
1878
153k
    frame_rate = st->Fs/frame_size;
1879
1880
153k
#ifndef DISABLE_FLOAT_API
1881
153k
    if (is_silence)
1882
920
    {
1883
920
       activity = !is_silence;
1884
152k
    } else if (analysis_info->valid)
1885
20.5k
    {
1886
20.5k
       activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD;
1887
20.5k
       if (!activity)
1888
1.92k
       {
1889
           /* Mark as active if this noise frame is sufficiently loud */
1890
1.92k
           opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1891
1.92k
           activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy);
1892
1.92k
       }
1893
20.5k
    }
1894
153k
#endif
1895
1896
    /* For the first frame at a new SILK bandwidth */
1897
153k
    if (st->silk_bw_switch)
1898
0
    {
1899
0
       redundancy = 1;
1900
0
       celt_to_silk = 1;
1901
0
       st->silk_bw_switch = 0;
1902
       /* Do a prefill without resetting the sampling rate control. */
1903
0
       prefill=2;
1904
0
    }
1905
1906
    /* If we decided to go with CELT, make sure redundancy is off, no matter what
1907
       we decided earlier. */
1908
153k
    if (st->mode == MODE_CELT_ONLY)
1909
83.2k
        redundancy = 0;
1910
1911
153k
    if (redundancy)
1912
0
    {
1913
0
       redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1914
0
       if (redundancy_bytes == 0)
1915
0
          redundancy = 0;
1916
0
    }
1917
1918
    /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1919
153k
    bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1920
1921
153k
    data += 1;
1922
1923
153k
    ec_enc_init(&enc, data, orig_max_data_bytes-1);
1924
1925
153k
    ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res);
1926
153k
    OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1927
1928
153k
    if (st->mode == MODE_CELT_ONLY)
1929
83.2k
       hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1930
69.7k
    else
1931
69.7k
       hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1932
1933
153k
    st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1934
153k
          hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1935
1936
    /* convert from log scale to Hertz */
1937
153k
    cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1938
1939
153k
    if (st->application == OPUS_APPLICATION_VOIP)
1940
37.6k
    {
1941
37.6k
       hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
1942
1943
#ifdef ENABLE_OSCE_TRAINING_DATA
1944
       /* write out high pass filtered clean signal*/
1945
       static FILE *fout =NULL;
1946
       if (fout == NULL)
1947
       {
1948
         fout = fopen("clean_hp.s16", "wb");
1949
       }
1950
1951
       {
1952
         int idx;
1953
         opus_int16 tmp;
1954
         for (idx = 0; idx < frame_size; idx++)
1955
         {
1956
            tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f);
1957
            fwrite(&tmp, sizeof(tmp), 1, fout);
1958
         }
1959
       }
1960
#endif
1961
115k
    } else {
1962
115k
#ifdef ENABLE_QEXT
1963
       /* FIXME: Avoid glitching when we switch qext on/off dynamically. */
1964
115k
       if (st->enable_qext) OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels);
1965
115k
       else
1966
115k
#endif
1967
115k
       dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1968
115k
    }
1969
#ifndef FIXED_POINT
1970
    if (float_api)
1971
    {
1972
       opus_val32 sum;
1973
       sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1974
       /* This should filter out both NaNs and ridiculous signals that could
1975
          cause NaNs further down. */
1976
       if (!(sum < 1e9f) || celt_isnan(sum))
1977
       {
1978
          OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1979
          st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1980
       }
1981
    }
1982
#else
1983
153k
    (void)float_api;
1984
153k
#endif
1985
1986
#ifdef ENABLE_DRED
1987
    if ( st->dred_duration > 0 && st->dred_encoder.loaded ) {
1988
        int frame_size_400Hz;
1989
        /* DRED Encoder */
1990
        dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch );
1991
        frame_size_400Hz = frame_size*400/st->Fs;
1992
        OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz);
1993
        for (i=0;i<frame_size_400Hz;i++)
1994
           st->activity_mem[i] = activity;
1995
    } else {
1996
        st->dred_encoder.latents_buffer_fill = 0;
1997
        OPUS_CLEAR(st->activity_mem, DRED_MAX_FRAMES);
1998
    }
1999
#endif
2000
2001
    /* SILK processing */
2002
153k
    HB_gain = Q15ONE;
2003
153k
    if (st->mode != MODE_CELT_ONLY)
2004
69.7k
    {
2005
69.7k
        opus_int32 total_bitRate, celt_rate;
2006
69.7k
        const opus_res *pcm_silk;
2007
2008
        /* Distribute bits between SILK and CELT */
2009
69.7k
        total_bitRate = 8 * bytes_target * frame_rate;
2010
69.7k
        if( st->mode == MODE_HYBRID ) {
2011
            /* Base rate for SILK */
2012
4.91k
            st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
2013
4.91k
                  curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2014
4.91k
                  st->stream_channels);
2015
4.91k
            if (!st->energy_masking)
2016
4.91k
            {
2017
               /* Increasingly attenuate high band when it gets allocated fewer bits */
2018
4.91k
               celt_rate = total_bitRate - st->silk_mode.bitRate;
2019
4.91k
               HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
2020
4.91k
            }
2021
64.8k
        } else {
2022
            /* SILK gets all bits */
2023
64.8k
            st->silk_mode.bitRate = total_bitRate;
2024
64.8k
        }
2025
2026
        /* Surround masking for SILK */
2027
69.7k
        if (st->energy_masking && st->use_vbr && !st->lfe)
2028
155
        {
2029
155
           opus_val32 mask_sum=0;
2030
155
           celt_glog masking_depth;
2031
155
           opus_int32 rate_offset;
2032
155
           int c;
2033
155
           int end = 17;
2034
155
           opus_int16 srate = 16000;
2035
155
           if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
2036
155
           {
2037
155
              end = 13;
2038
155
              srate = 8000;
2039
155
           } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
2040
0
           {
2041
0
              end = 15;
2042
0
              srate = 12000;
2043
0
           }
2044
310
           for (c=0;c<st->channels;c++)
2045
155
           {
2046
2.17k
              for(i=0;i<end;i++)
2047
2.01k
              {
2048
2.01k
                 celt_glog mask;
2049
2.01k
                 mask = MAXG(MING(st->energy_masking[21*c+i],
2050
2.01k
                        GCONST(.5f)), -GCONST(2.0f));
2051
2.01k
                 if (mask > 0)
2052
816
                    mask = HALF32(mask);
2053
2.01k
                 mask_sum += mask;
2054
2.01k
              }
2055
155
           }
2056
           /* Conservative rate reduction, we cut the masking in half */
2057
155
           masking_depth = mask_sum / end*st->channels;
2058
155
           masking_depth += GCONST(.2f);
2059
155
           rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10);
2060
155
           rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
2061
           /* Split the rate change between the SILK and CELT part for hybrid. */
2062
155
           if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
2063
0
              st->silk_mode.bitRate += 3*rate_offset/5;
2064
155
           else
2065
155
              st->silk_mode.bitRate += rate_offset;
2066
155
        }
2067
2068
69.7k
        st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
2069
69.7k
        st->silk_mode.nChannelsAPI = st->channels;
2070
69.7k
        st->silk_mode.nChannelsInternal = st->stream_channels;
2071
69.7k
        if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2072
51.0k
            st->silk_mode.desiredInternalSampleRate = 8000;
2073
51.0k
        } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2074
5.04k
            st->silk_mode.desiredInternalSampleRate = 12000;
2075
13.6k
        } else {
2076
13.6k
            celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
2077
13.6k
            st->silk_mode.desiredInternalSampleRate = 16000;
2078
13.6k
        }
2079
69.7k
        if( st->mode == MODE_HYBRID ) {
2080
            /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
2081
4.91k
            st->silk_mode.minInternalSampleRate = 16000;
2082
64.8k
        } else {
2083
64.8k
            st->silk_mode.minInternalSampleRate = 8000;
2084
64.8k
        }
2085
2086
69.7k
        st->silk_mode.maxInternalSampleRate = 16000;
2087
69.7k
        if (st->mode == MODE_SILK_ONLY)
2088
64.8k
        {
2089
64.8k
           opus_int32 effective_max_rate = frame_rate*max_data_bytes*8;
2090
64.8k
           if (frame_rate > 50)
2091
23.8k
              effective_max_rate = effective_max_rate*2/3;
2092
64.8k
           if (effective_max_rate < 8000)
2093
1.35k
           {
2094
1.35k
              st->silk_mode.maxInternalSampleRate = 12000;
2095
1.35k
              st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
2096
1.35k
           }
2097
64.8k
           if (effective_max_rate < 7000)
2098
905
           {
2099
905
              st->silk_mode.maxInternalSampleRate = 8000;
2100
905
              st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
2101
905
           }
2102
64.8k
#ifdef ENABLE_QEXT
2103
           /* At 96 kHz, we don't have the input resampler to do 8 or 12 kHz. */
2104
64.8k
           if (st->Fs==96000) st->silk_mode.maxInternalSampleRate = st->silk_mode.desiredInternalSampleRate = 16000;
2105
64.8k
#endif
2106
64.8k
        }
2107
2108
69.7k
        st->silk_mode.useCBR = !st->use_vbr;
2109
2110
        /* Call SILK encoder for the low band */
2111
2112
        /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
2113
69.7k
        st->silk_mode.maxBits = (max_data_bytes-1)*8;
2114
69.7k
        if (redundancy && redundancy_bytes >= 2)
2115
0
        {
2116
           /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
2117
0
           st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
2118
0
           if (st->mode == MODE_HYBRID)
2119
0
              st->silk_mode.maxBits -= 20;
2120
0
        }
2121
69.7k
        if (st->silk_mode.useCBR)
2122
34.0k
        {
2123
           /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to
2124
              save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still
2125
              produce a constant bitrate without wasting bits. */
2126
#ifdef ENABLE_DRED
2127
           if (st->mode == MODE_HYBRID || dred_bitrate_bps > 0)
2128
#else
2129
34.0k
           if (st->mode == MODE_HYBRID)
2130
1.50k
#endif
2131
1.50k
           {
2132
              /* Allow SILK to steal up to 25% of the remaining bits */
2133
1.50k
              opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs);
2134
1.50k
              st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4);
2135
1.50k
              st->silk_mode.useCBR = 0;
2136
1.50k
           }
2137
35.7k
        } else {
2138
           /* Constrained VBR. */
2139
35.7k
           if (st->mode == MODE_HYBRID)
2140
3.40k
           {
2141
              /* Compute SILK bitrate corresponding to the max total bits available */
2142
3.40k
              opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
2143
3.40k
                    curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2144
3.40k
                    st->stream_channels);
2145
3.40k
              st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
2146
3.40k
           }
2147
35.7k
        }
2148
2149
69.7k
        if (prefill)
2150
0
        {
2151
0
            opus_int32 zero=0;
2152
0
            int prefill_offset;
2153
            /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
2154
               a discontinuity. The exact location is what we need to avoid leaving any "gap"
2155
               in the audio when mixing with the redundant CELT frame. Here we can afford to
2156
               overwrite st->delay_buffer because the only thing that uses it before it gets
2157
               rewritten is tmp_prefill[] and even then only the part after the ramp really
2158
               gets used (rather than sent to the encoder and discarded) */
2159
0
            prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
2160
0
            gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
2161
0
                  0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
2162
0
            OPUS_CLEAR(st->delay_buffer, prefill_offset);
2163
0
            pcm_silk = st->delay_buffer;
2164
0
            silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity );
2165
            /* Prevent a second switch in the real encode call. */
2166
0
            st->silk_mode.opusCanSwitch = 0;
2167
0
        }
2168
2169
69.7k
        pcm_silk = pcm_buf+total_buffer*st->channels;
2170
69.7k
        ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
2171
69.7k
        if( ret ) {
2172
            /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
2173
            /* Handle error */
2174
0
           RESTORE_STACK;
2175
0
           return OPUS_INTERNAL_ERROR;
2176
0
        }
2177
2178
        /* Extract SILK internal bandwidth for signaling in first byte */
2179
69.7k
        if( st->mode == MODE_SILK_ONLY ) {
2180
64.8k
            if( st->silk_mode.internalSampleRate == 8000 ) {
2181
51.0k
               curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
2182
51.0k
            } else if( st->silk_mode.internalSampleRate == 12000 ) {
2183
5.07k
               curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
2184
8.76k
            } else if( st->silk_mode.internalSampleRate == 16000 ) {
2185
8.76k
               curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
2186
8.76k
            }
2187
64.8k
        } else {
2188
4.91k
            celt_assert( st->silk_mode.internalSampleRate == 16000 );
2189
4.91k
        }
2190
2191
69.7k
        st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
2192
2193
69.7k
        if (nBytes==0)
2194
0
        {
2195
0
           st->rangeFinal = 0;
2196
0
           data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2197
0
           RESTORE_STACK;
2198
0
           return 1;
2199
0
        }
2200
2201
        /* FIXME: How do we allocate the redundancy for CBR? */
2202
69.7k
        if (st->silk_mode.opusCanSwitch)
2203
0
        {
2204
0
           redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
2205
0
           redundancy = (redundancy_bytes != 0);
2206
0
           celt_to_silk = 0;
2207
0
           st->silk_bw_switch = 1;
2208
0
        }
2209
69.7k
    }
2210
2211
    /* CELT processing */
2212
153k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2213
153k
    {
2214
153k
        int endband=21;
2215
2216
153k
        switch(curr_bandwidth)
2217
153k
        {
2218
109k
            case OPUS_BANDWIDTH_NARROWBAND:
2219
109k
                endband = 13;
2220
109k
                break;
2221
5.07k
            case OPUS_BANDWIDTH_MEDIUMBAND:
2222
29.1k
            case OPUS_BANDWIDTH_WIDEBAND:
2223
29.1k
                endband = 17;
2224
29.1k
                break;
2225
9.95k
            case OPUS_BANDWIDTH_SUPERWIDEBAND:
2226
9.95k
                endband = 19;
2227
9.95k
                break;
2228
4.62k
            case OPUS_BANDWIDTH_FULLBAND:
2229
4.62k
                endband = 21;
2230
4.62k
                break;
2231
153k
        }
2232
153k
        celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
2233
153k
        celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
2234
153k
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2235
153k
    }
2236
153k
    if (st->mode != MODE_SILK_ONLY)
2237
88.1k
    {
2238
88.1k
        opus_val32 celt_pred=2;
2239
        /* We may still decide to disable prediction later */
2240
88.1k
        if (st->silk_mode.reducedDependency)
2241
25.3k
           celt_pred = 0;
2242
88.1k
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
2243
88.1k
    }
2244
2245
153k
    ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res);
2246
153k
    if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
2247
0
    {
2248
0
       OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
2249
0
    }
2250
2251
153k
    if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
2252
46.7k
    {
2253
46.7k
       OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
2254
46.7k
       OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
2255
46.7k
             &pcm_buf[0],
2256
46.7k
             (frame_size+total_buffer)*st->channels);
2257
106k
    } else {
2258
106k
       OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
2259
106k
    }
2260
    /* gain_fade() and stereo_fade() need to be after the buffer copying
2261
       because we don't want any of this to affect the SILK part */
2262
153k
    if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
2263
3.70k
       gain_fade(pcm_buf, pcm_buf,
2264
3.70k
             st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
2265
3.70k
    }
2266
153k
    st->prev_HB_gain = HB_gain;
2267
153k
    if (st->mode != MODE_HYBRID || st->stream_channels==1)
2268
150k
    {
2269
150k
       if (equiv_rate > 32000)
2270
51.1k
          st->silk_mode.stereoWidth_Q14 = 16384;
2271
99.7k
       else if (equiv_rate < 16000)
2272
38.3k
          st->silk_mode.stereoWidth_Q14 = 0;
2273
61.4k
       else
2274
61.4k
          st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
2275
150k
    }
2276
153k
    if( !st->energy_masking && st->channels == 2 ) {
2277
        /* Apply stereo width reduction (at low bitrates) */
2278
62.6k
        if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
2279
19.9k
            opus_val16 g1, g2;
2280
19.9k
            g1 = st->hybrid_stereo_width_Q14;
2281
19.9k
            g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
2282
19.9k
#ifdef FIXED_POINT
2283
19.9k
            g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
2284
19.9k
            g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
2285
#else
2286
            g1 *= (1.f/16384);
2287
            g2 *= (1.f/16384);
2288
#endif
2289
19.9k
            stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
2290
19.9k
                  frame_size, st->channels, celt_mode->window, st->Fs);
2291
19.9k
            st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
2292
19.9k
        }
2293
62.6k
    }
2294
2295
153k
    if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
2296
58.9k
    {
2297
        /* For SILK mode, the redundancy is inferred from the length */
2298
58.9k
        if (st->mode == MODE_HYBRID)
2299
4.81k
           ec_enc_bit_logp(&enc, redundancy, 12);
2300
58.9k
        if (redundancy)
2301
0
        {
2302
0
            int max_redundancy;
2303
0
            ec_enc_bit_logp(&enc, celt_to_silk, 1);
2304
0
            if (st->mode == MODE_HYBRID)
2305
0
            {
2306
               /* Reserve the 8 bits needed for the redundancy length,
2307
                  and at least a few bits for CELT if possible */
2308
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
2309
0
            }
2310
0
            else
2311
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2312
            /* Target the same bit-rate for redundancy as for the rest,
2313
               up to a max of 257 bytes */
2314
0
            redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
2315
0
            redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2316
0
            if (st->mode == MODE_HYBRID)
2317
0
                ec_enc_uint(&enc, redundancy_bytes-2, 256);
2318
0
        }
2319
94.0k
    } else {
2320
94.0k
        redundancy = 0;
2321
94.0k
    }
2322
2323
153k
    if (!redundancy)
2324
153k
    {
2325
153k
       st->silk_bw_switch = 0;
2326
153k
       redundancy_bytes = 0;
2327
153k
    }
2328
153k
    if (st->mode != MODE_CELT_ONLY)start_band=17;
2329
2330
153k
    if (st->mode == MODE_SILK_ONLY)
2331
64.8k
    {
2332
64.8k
        ret = (ec_tell(&enc)+7)>>3;
2333
64.8k
        ec_enc_done(&enc);
2334
64.8k
        nb_compr_bytes = ret;
2335
88.1k
    } else {
2336
88.1k
        nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2337
88.1k
#ifdef ENABLE_QEXT
2338
88.1k
        if (st->mode == MODE_CELT_ONLY && st->enable_qext) {
2339
0
           celt_assert(redundancy_bytes==0);
2340
0
           nb_compr_bytes = orig_max_data_bytes-1;
2341
0
        }
2342
88.1k
#endif
2343
#ifdef ENABLE_DRED
2344
        if (st->dred_duration > 0)
2345
        {
2346
            int max_celt_bytes;
2347
            opus_int32 dred_bytes = dred_bitrate_bps/(frame_rate*8);
2348
            /* Allow CELT to steal up to 25% of the remaining bits. */
2349
            max_celt_bytes = nb_compr_bytes - dred_bytes*3/4;
2350
            /* But try to give CELT at least 5 bytes to prevent a mismatch with
2351
               the redundancy signaling. */
2352
            max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes);
2353
            /* Subject to the original max. */
2354
            nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes);
2355
        }
2356
#endif
2357
88.1k
        ec_enc_shrink(&enc, nb_compr_bytes);
2358
88.1k
    }
2359
2360
153k
#ifndef DISABLE_FLOAT_API
2361
153k
    if (redundancy || st->mode != MODE_SILK_ONLY)
2362
88.1k
       celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info));
2363
153k
#endif
2364
153k
    if (st->mode == MODE_HYBRID) {
2365
4.91k
       SILKInfo info;
2366
4.91k
       info.signalType = st->silk_mode.signalType;
2367
4.91k
       info.offset = st->silk_mode.offset;
2368
4.91k
       celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2369
4.91k
    }
2370
2371
    /* 5 ms redundant frame for CELT->SILK */
2372
153k
    if (redundancy && celt_to_silk)
2373
0
    {
2374
0
        int err;
2375
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2376
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2377
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2378
0
        err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2379
0
        if (err < 0)
2380
0
        {
2381
0
           RESTORE_STACK;
2382
0
           return OPUS_INTERNAL_ERROR;
2383
0
        }
2384
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2385
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2386
0
    }
2387
2388
153k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2389
153k
       celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2390
2391
153k
    data[-1] = 0;
2392
153k
    if (st->mode != MODE_SILK_ONLY)
2393
88.1k
    {
2394
88.1k
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2395
88.1k
        if (st->mode == MODE_HYBRID)
2396
4.91k
        {
2397
4.91k
            if( st->use_vbr ) {
2398
3.40k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2399
3.40k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2400
3.40k
            }
2401
83.2k
        } else {
2402
83.2k
            if (st->use_vbr)
2403
60.3k
            {
2404
60.3k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2405
60.3k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
2406
60.3k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
2407
60.3k
            }
2408
83.2k
        }
2409
#ifdef ENABLE_DRED
2410
        /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */
2411
        if (!st->use_vbr && st->dred_duration > 0)
2412
        {
2413
            opus_int32 celt_bitrate = st->bitrate_bps;
2414
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2415
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2416
            if (st->mode == MODE_HYBRID) {
2417
                celt_bitrate -= st->silk_mode.bitRate;
2418
            }
2419
            celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate));
2420
        }
2421
#endif
2422
88.1k
        if (st->mode != st->prev_mode && st->prev_mode > 0)
2423
0
        {
2424
0
           unsigned char dummy[2];
2425
0
           celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2426
2427
           /* Prefilling */
2428
0
           celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2429
0
           celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2430
0
        }
2431
        /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2432
88.1k
        if (ec_tell(&enc) <= 8*nb_compr_bytes)
2433
88.0k
        {
2434
88.0k
#ifdef ENABLE_QEXT
2435
88.0k
           if (st->mode == MODE_CELT_ONLY) celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(st->enable_qext));
2436
88.0k
#endif
2437
88.0k
           ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2438
88.0k
#ifdef ENABLE_QEXT
2439
88.0k
           celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(0));
2440
88.0k
#endif
2441
88.0k
           if (ret < 0)
2442
0
           {
2443
0
              RESTORE_STACK;
2444
0
              return OPUS_INTERNAL_ERROR;
2445
0
           }
2446
           /* Put CELT->SILK redundancy data in the right place. */
2447
88.0k
           if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && nb_compr_bytes != ret)
2448
0
           {
2449
0
              OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2450
0
              nb_compr_bytes = ret+redundancy_bytes;
2451
0
           }
2452
88.0k
        }
2453
88.1k
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&st->rangeFinal));
2454
88.1k
    } else {
2455
64.8k
       st->rangeFinal = enc.rng;
2456
64.8k
    }
2457
2458
    /* 5 ms redundant frame for SILK->CELT */
2459
153k
    if (redundancy && !celt_to_silk)
2460
0
    {
2461
0
        int err;
2462
0
        unsigned char dummy[2];
2463
0
        int N2, N4;
2464
0
        N2 = st->Fs/200;
2465
0
        N4 = st->Fs/400;
2466
2467
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2468
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2469
0
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2470
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2471
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2472
2473
0
        if (st->mode == MODE_HYBRID)
2474
0
        {
2475
           /* Shrink packet to what the encoder actually used. */
2476
0
           nb_compr_bytes = ret;
2477
0
           ec_enc_shrink(&enc, nb_compr_bytes);
2478
0
        }
2479
        /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2480
0
        celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2481
2482
0
        err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2483
0
        if (err < 0)
2484
0
        {
2485
0
           RESTORE_STACK;
2486
0
           return OPUS_INTERNAL_ERROR;
2487
0
        }
2488
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2489
0
    }
2490
2491
2492
2493
    /* Signalling the mode in the first byte */
2494
153k
    data--;
2495
153k
    data[0] |= gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2496
2497
153k
    st->rangeFinal ^= redundant_rng;
2498
2499
153k
    if (to_celt)
2500
0
        st->prev_mode = MODE_CELT_ONLY;
2501
153k
    else
2502
153k
        st->prev_mode = st->mode;
2503
153k
    st->prev_channels = st->stream_channels;
2504
153k
    st->prev_framesize = frame_size;
2505
2506
153k
    st->first = 0;
2507
2508
    /* DTX decision */
2509
153k
#ifndef DISABLE_FLOAT_API
2510
153k
    if (st->use_dtx && (analysis_info->valid || is_silence))
2511
9.02k
    {
2512
9.02k
       if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs))
2513
0
       {
2514
0
          st->rangeFinal = 0;
2515
0
          data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2516
0
          RESTORE_STACK;
2517
0
          return 1;
2518
0
       }
2519
143k
    } else {
2520
143k
       st->nb_no_activity_ms_Q1 = 0;
2521
143k
    }
2522
153k
#endif
2523
2524
    /* In the unlikely case that the SILK encoder busted its target, tell
2525
       the decoder to call the PLC */
2526
153k
    if (ec_tell(&enc) > (max_data_bytes-1)*8)
2527
2.26k
    {
2528
2.26k
       if (max_data_bytes < 2)
2529
0
       {
2530
0
          RESTORE_STACK;
2531
0
          return OPUS_BUFFER_TOO_SMALL;
2532
0
       }
2533
2.26k
       data[1] = 0;
2534
2.26k
       ret = 1;
2535
2.26k
       st->rangeFinal = 0;
2536
150k
    } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2537
62.6k
    {
2538
       /*When in LPC only mode it's perfectly
2539
         reasonable to strip off trailing zero bytes as
2540
         the required range decoder behavior is to
2541
         fill these in. This can't be done when the MDCT
2542
         modes are used because the decoder needs to know
2543
         the actual length for allocation purposes.*/
2544
73.2k
       while(ret>2&&data[ret]==0)ret--;
2545
62.6k
    }
2546
    /* Count ToC and redundancy */
2547
153k
    ret += 1+redundancy_bytes;
2548
153k
    apply_padding = !st->use_vbr;
2549
#ifdef ENABLE_DRED
2550
    if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) {
2551
       opus_extension_data extension;
2552
       unsigned char buf[DRED_MAX_DATA_SIZE];
2553
       int dred_chunks;
2554
       int dred_bytes_left;
2555
       dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2);
2556
       if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks);
2557
       /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */
2558
       dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-3);
2559
       /* Account for the extra bytes required to signal large padding length. */
2560
       dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255;
2561
       /* Check whether we actually have something to encode. */
2562
       if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) {
2563
           int dred_bytes;
2564
#ifdef DRED_EXPERIMENTAL_VERSION
2565
           /* Add temporary extension type and version.
2566
              These bytes will be removed once extension is finalized. */
2567
           buf[0] = 'D';
2568
           buf[1] = DRED_EXPERIMENTAL_VERSION;
2569
#endif
2570
           dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES,
2571
                                               st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch);
2572
           if (dred_bytes > 0) {
2573
              dred_bytes += DRED_EXPERIMENTAL_BYTES;
2574
              celt_assert(dred_bytes <= dred_bytes_left);
2575
              extension.id = DRED_EXTENSION_ID;
2576
              extension.frame = 0;
2577
              extension.data = buf;
2578
              extension.len = dred_bytes;
2579
              ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1);
2580
              if (ret < 0)
2581
              {
2582
                 RESTORE_STACK;
2583
                 return OPUS_INTERNAL_ERROR;
2584
              }
2585
              apply_padding = 0;
2586
           }
2587
       }
2588
    }
2589
#else
2590
153k
    (void)first_frame; /* Avoids a warning about first_frame being unused. */
2591
153k
#endif
2592
153k
    if (apply_padding)
2593
56.9k
    {
2594
56.9k
       if (opus_packet_pad(data, ret, orig_max_data_bytes) != OPUS_OK)
2595
0
       {
2596
0
          RESTORE_STACK;
2597
0
          return OPUS_INTERNAL_ERROR;
2598
0
       }
2599
56.9k
       ret = orig_max_data_bytes;
2600
56.9k
    }
2601
153k
    RESTORE_STACK;
2602
153k
    return ret;
2603
153k
}
opus_encoder.c:opus_encode_frame_native
Line
Count
Source
1835
144k
{
1836
144k
    void *silk_enc=NULL;
1837
144k
    CELTEncoder *celt_enc=NULL;
1838
144k
    const CELTMode *celt_mode=NULL;
1839
144k
    int i;
1840
144k
    int ret=0;
1841
144k
    int max_data_bytes;
1842
144k
    opus_int32 nBytes;
1843
144k
    ec_enc enc;
1844
144k
    int bytes_target;
1845
144k
    int start_band = 0;
1846
144k
    int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1847
144k
    int nb_compr_bytes;
1848
144k
    opus_uint32 redundant_rng = 0;
1849
144k
    int cutoff_Hz;
1850
144k
    int hp_freq_smth1;
1851
144k
    opus_val16 HB_gain;
1852
144k
    int apply_padding;
1853
144k
    int frame_rate;
1854
144k
    int curr_bandwidth;
1855
144k
    int delay_compensation;
1856
144k
    int total_buffer;
1857
144k
    opus_int activity = VAD_NO_DECISION;
1858
144k
    VARDECL(opus_res, pcm_buf);
1859
144k
    VARDECL(opus_res, tmp_prefill);
1860
144k
    SAVE_STACK;
1861
1862
144k
    max_data_bytes = IMIN(orig_max_data_bytes, 1276);
1863
144k
    st->rangeFinal = 0;
1864
144k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1865
144k
       silk_enc = (char*)st+st->silk_enc_offset;
1866
144k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1867
144k
    {
1868
144k
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1869
144k
       celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1870
144k
    }
1871
144k
    curr_bandwidth = st->bandwidth;
1872
144k
    if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1873
38.1k
       delay_compensation = 0;
1874
106k
    else
1875
106k
       delay_compensation = st->delay_compensation;
1876
144k
    total_buffer = delay_compensation;
1877
1878
144k
    frame_rate = st->Fs/frame_size;
1879
1880
144k
#ifndef DISABLE_FLOAT_API
1881
144k
    if (is_silence)
1882
2.42k
    {
1883
2.42k
       activity = !is_silence;
1884
141k
    } else if (analysis_info->valid)
1885
21.4k
    {
1886
21.4k
       activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD;
1887
21.4k
       if (!activity)
1888
1.65k
       {
1889
           /* Mark as active if this noise frame is sufficiently loud */
1890
1.65k
           opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1891
1.65k
           activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy);
1892
1.65k
       }
1893
21.4k
    }
1894
144k
#endif
1895
1896
    /* For the first frame at a new SILK bandwidth */
1897
144k
    if (st->silk_bw_switch)
1898
0
    {
1899
0
       redundancy = 1;
1900
0
       celt_to_silk = 1;
1901
0
       st->silk_bw_switch = 0;
1902
       /* Do a prefill without resetting the sampling rate control. */
1903
0
       prefill=2;
1904
0
    }
1905
1906
    /* If we decided to go with CELT, make sure redundancy is off, no matter what
1907
       we decided earlier. */
1908
144k
    if (st->mode == MODE_CELT_ONLY)
1909
77.7k
        redundancy = 0;
1910
1911
144k
    if (redundancy)
1912
0
    {
1913
0
       redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1914
0
       if (redundancy_bytes == 0)
1915
0
          redundancy = 0;
1916
0
    }
1917
1918
    /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1919
144k
    bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1920
1921
144k
    data += 1;
1922
1923
144k
    ec_enc_init(&enc, data, orig_max_data_bytes-1);
1924
1925
144k
    ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res);
1926
144k
    OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1927
1928
144k
    if (st->mode == MODE_CELT_ONLY)
1929
77.7k
       hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1930
66.4k
    else
1931
66.4k
       hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1932
1933
144k
    st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1934
144k
          hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1935
1936
    /* convert from log scale to Hertz */
1937
144k
    cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1938
1939
144k
    if (st->application == OPUS_APPLICATION_VOIP)
1940
42.9k
    {
1941
42.9k
       hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
1942
1943
#ifdef ENABLE_OSCE_TRAINING_DATA
1944
       /* write out high pass filtered clean signal*/
1945
       static FILE *fout =NULL;
1946
       if (fout == NULL)
1947
       {
1948
         fout = fopen("clean_hp.s16", "wb");
1949
       }
1950
1951
       {
1952
         int idx;
1953
         opus_int16 tmp;
1954
         for (idx = 0; idx < frame_size; idx++)
1955
         {
1956
            tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f);
1957
            fwrite(&tmp, sizeof(tmp), 1, fout);
1958
         }
1959
       }
1960
#endif
1961
101k
    } else {
1962
101k
#ifdef ENABLE_QEXT
1963
       /* FIXME: Avoid glitching when we switch qext on/off dynamically. */
1964
101k
       if (st->enable_qext) OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels);
1965
101k
       else
1966
101k
#endif
1967
101k
       dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1968
101k
    }
1969
144k
#ifndef FIXED_POINT
1970
144k
    if (float_api)
1971
0
    {
1972
0
       opus_val32 sum;
1973
0
       sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1974
       /* This should filter out both NaNs and ridiculous signals that could
1975
          cause NaNs further down. */
1976
0
       if (!(sum < 1e9f) || celt_isnan(sum))
1977
0
       {
1978
0
          OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1979
0
          st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1980
0
       }
1981
0
    }
1982
#else
1983
    (void)float_api;
1984
#endif
1985
1986
#ifdef ENABLE_DRED
1987
    if ( st->dred_duration > 0 && st->dred_encoder.loaded ) {
1988
        int frame_size_400Hz;
1989
        /* DRED Encoder */
1990
        dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch );
1991
        frame_size_400Hz = frame_size*400/st->Fs;
1992
        OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz);
1993
        for (i=0;i<frame_size_400Hz;i++)
1994
           st->activity_mem[i] = activity;
1995
    } else {
1996
        st->dred_encoder.latents_buffer_fill = 0;
1997
        OPUS_CLEAR(st->activity_mem, DRED_MAX_FRAMES);
1998
    }
1999
#endif
2000
2001
    /* SILK processing */
2002
144k
    HB_gain = Q15ONE;
2003
144k
    if (st->mode != MODE_CELT_ONLY)
2004
66.4k
    {
2005
66.4k
        opus_int32 total_bitRate, celt_rate;
2006
66.4k
        const opus_res *pcm_silk;
2007
2008
        /* Distribute bits between SILK and CELT */
2009
66.4k
        total_bitRate = 8 * bytes_target * frame_rate;
2010
66.4k
        if( st->mode == MODE_HYBRID ) {
2011
            /* Base rate for SILK */
2012
4.84k
            st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
2013
4.84k
                  curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2014
4.84k
                  st->stream_channels);
2015
4.84k
            if (!st->energy_masking)
2016
4.84k
            {
2017
               /* Increasingly attenuate high band when it gets allocated fewer bits */
2018
4.84k
               celt_rate = total_bitRate - st->silk_mode.bitRate;
2019
4.84k
               HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
2020
4.84k
            }
2021
61.6k
        } else {
2022
            /* SILK gets all bits */
2023
61.6k
            st->silk_mode.bitRate = total_bitRate;
2024
61.6k
        }
2025
2026
        /* Surround masking for SILK */
2027
66.4k
        if (st->energy_masking && st->use_vbr && !st->lfe)
2028
178
        {
2029
178
           opus_val32 mask_sum=0;
2030
178
           celt_glog masking_depth;
2031
178
           opus_int32 rate_offset;
2032
178
           int c;
2033
178
           int end = 17;
2034
178
           opus_int16 srate = 16000;
2035
178
           if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
2036
178
           {
2037
178
              end = 13;
2038
178
              srate = 8000;
2039
178
           } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
2040
0
           {
2041
0
              end = 15;
2042
0
              srate = 12000;
2043
0
           }
2044
356
           for (c=0;c<st->channels;c++)
2045
178
           {
2046
2.49k
              for(i=0;i<end;i++)
2047
2.31k
              {
2048
2.31k
                 celt_glog mask;
2049
2.31k
                 mask = MAXG(MING(st->energy_masking[21*c+i],
2050
2.31k
                        GCONST(.5f)), -GCONST(2.0f));
2051
2.31k
                 if (mask > 0)
2052
795
                    mask = HALF32(mask);
2053
2.31k
                 mask_sum += mask;
2054
2.31k
              }
2055
178
           }
2056
           /* Conservative rate reduction, we cut the masking in half */
2057
178
           masking_depth = mask_sum / end*st->channels;
2058
178
           masking_depth += GCONST(.2f);
2059
178
           rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10);
2060
178
           rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
2061
           /* Split the rate change between the SILK and CELT part for hybrid. */
2062
178
           if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
2063
0
              st->silk_mode.bitRate += 3*rate_offset/5;
2064
178
           else
2065
178
              st->silk_mode.bitRate += rate_offset;
2066
178
        }
2067
2068
66.4k
        st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
2069
66.4k
        st->silk_mode.nChannelsAPI = st->channels;
2070
66.4k
        st->silk_mode.nChannelsInternal = st->stream_channels;
2071
66.4k
        if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2072
44.9k
            st->silk_mode.desiredInternalSampleRate = 8000;
2073
44.9k
        } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2074
7.04k
            st->silk_mode.desiredInternalSampleRate = 12000;
2075
14.4k
        } else {
2076
14.4k
            celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
2077
14.4k
            st->silk_mode.desiredInternalSampleRate = 16000;
2078
14.4k
        }
2079
66.4k
        if( st->mode == MODE_HYBRID ) {
2080
            /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
2081
4.84k
            st->silk_mode.minInternalSampleRate = 16000;
2082
61.6k
        } else {
2083
61.6k
            st->silk_mode.minInternalSampleRate = 8000;
2084
61.6k
        }
2085
2086
66.4k
        st->silk_mode.maxInternalSampleRate = 16000;
2087
66.4k
        if (st->mode == MODE_SILK_ONLY)
2088
61.6k
        {
2089
61.6k
           opus_int32 effective_max_rate = frame_rate*max_data_bytes*8;
2090
61.6k
           if (frame_rate > 50)
2091
21.8k
              effective_max_rate = effective_max_rate*2/3;
2092
61.6k
           if (effective_max_rate < 8000)
2093
1.27k
           {
2094
1.27k
              st->silk_mode.maxInternalSampleRate = 12000;
2095
1.27k
              st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
2096
1.27k
           }
2097
61.6k
           if (effective_max_rate < 7000)
2098
891
           {
2099
891
              st->silk_mode.maxInternalSampleRate = 8000;
2100
891
              st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
2101
891
           }
2102
61.6k
#ifdef ENABLE_QEXT
2103
           /* At 96 kHz, we don't have the input resampler to do 8 or 12 kHz. */
2104
61.6k
           if (st->Fs==96000) st->silk_mode.maxInternalSampleRate = st->silk_mode.desiredInternalSampleRate = 16000;
2105
61.6k
#endif
2106
61.6k
        }
2107
2108
66.4k
        st->silk_mode.useCBR = !st->use_vbr;
2109
2110
        /* Call SILK encoder for the low band */
2111
2112
        /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
2113
66.4k
        st->silk_mode.maxBits = (max_data_bytes-1)*8;
2114
66.4k
        if (redundancy && redundancy_bytes >= 2)
2115
0
        {
2116
           /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
2117
0
           st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
2118
0
           if (st->mode == MODE_HYBRID)
2119
0
              st->silk_mode.maxBits -= 20;
2120
0
        }
2121
66.4k
        if (st->silk_mode.useCBR)
2122
36.0k
        {
2123
           /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to
2124
              save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still
2125
              produce a constant bitrate without wasting bits. */
2126
#ifdef ENABLE_DRED
2127
           if (st->mode == MODE_HYBRID || dred_bitrate_bps > 0)
2128
#else
2129
36.0k
           if (st->mode == MODE_HYBRID)
2130
1.53k
#endif
2131
1.53k
           {
2132
              /* Allow SILK to steal up to 25% of the remaining bits */
2133
1.53k
              opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs);
2134
1.53k
              st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4);
2135
1.53k
              st->silk_mode.useCBR = 0;
2136
1.53k
           }
2137
36.0k
        } else {
2138
           /* Constrained VBR. */
2139
30.4k
           if (st->mode == MODE_HYBRID)
2140
3.31k
           {
2141
              /* Compute SILK bitrate corresponding to the max total bits available */
2142
3.31k
              opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
2143
3.31k
                    curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2144
3.31k
                    st->stream_channels);
2145
3.31k
              st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
2146
3.31k
           }
2147
30.4k
        }
2148
2149
66.4k
        if (prefill)
2150
0
        {
2151
0
            opus_int32 zero=0;
2152
0
            int prefill_offset;
2153
            /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
2154
               a discontinuity. The exact location is what we need to avoid leaving any "gap"
2155
               in the audio when mixing with the redundant CELT frame. Here we can afford to
2156
               overwrite st->delay_buffer because the only thing that uses it before it gets
2157
               rewritten is tmp_prefill[] and even then only the part after the ramp really
2158
               gets used (rather than sent to the encoder and discarded) */
2159
0
            prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
2160
0
            gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
2161
0
                  0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
2162
0
            OPUS_CLEAR(st->delay_buffer, prefill_offset);
2163
0
            pcm_silk = st->delay_buffer;
2164
0
            silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity );
2165
            /* Prevent a second switch in the real encode call. */
2166
0
            st->silk_mode.opusCanSwitch = 0;
2167
0
        }
2168
2169
66.4k
        pcm_silk = pcm_buf+total_buffer*st->channels;
2170
66.4k
        ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
2171
66.4k
        if( ret ) {
2172
            /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
2173
            /* Handle error */
2174
0
           RESTORE_STACK;
2175
0
           return OPUS_INTERNAL_ERROR;
2176
0
        }
2177
2178
        /* Extract SILK internal bandwidth for signaling in first byte */
2179
66.4k
        if( st->mode == MODE_SILK_ONLY ) {
2180
61.6k
            if( st->silk_mode.internalSampleRate == 8000 ) {
2181
45.2k
               curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
2182
45.2k
            } else if( st->silk_mode.internalSampleRate == 12000 ) {
2183
6.93k
               curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
2184
9.46k
            } else if( st->silk_mode.internalSampleRate == 16000 ) {
2185
9.46k
               curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
2186
9.46k
            }
2187
61.6k
        } else {
2188
4.84k
            celt_assert( st->silk_mode.internalSampleRate == 16000 );
2189
4.84k
        }
2190
2191
66.4k
        st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
2192
2193
66.4k
        if (nBytes==0)
2194
0
        {
2195
0
           st->rangeFinal = 0;
2196
0
           data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2197
0
           RESTORE_STACK;
2198
0
           return 1;
2199
0
        }
2200
2201
        /* FIXME: How do we allocate the redundancy for CBR? */
2202
66.4k
        if (st->silk_mode.opusCanSwitch)
2203
0
        {
2204
0
           redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
2205
0
           redundancy = (redundancy_bytes != 0);
2206
0
           celt_to_silk = 0;
2207
0
           st->silk_bw_switch = 1;
2208
0
        }
2209
66.4k
    }
2210
2211
    /* CELT processing */
2212
144k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2213
144k
    {
2214
144k
        int endband=21;
2215
2216
144k
        switch(curr_bandwidth)
2217
144k
        {
2218
97.3k
            case OPUS_BANDWIDTH_NARROWBAND:
2219
97.3k
                endband = 13;
2220
97.3k
                break;
2221
6.93k
            case OPUS_BANDWIDTH_MEDIUMBAND:
2222
32.7k
            case OPUS_BANDWIDTH_WIDEBAND:
2223
32.7k
                endband = 17;
2224
32.7k
                break;
2225
9.06k
            case OPUS_BANDWIDTH_SUPERWIDEBAND:
2226
9.06k
                endband = 19;
2227
9.06k
                break;
2228
5.06k
            case OPUS_BANDWIDTH_FULLBAND:
2229
5.06k
                endband = 21;
2230
5.06k
                break;
2231
144k
        }
2232
144k
        celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
2233
144k
        celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
2234
144k
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2235
144k
    }
2236
144k
    if (st->mode != MODE_SILK_ONLY)
2237
82.6k
    {
2238
82.6k
        opus_val32 celt_pred=2;
2239
        /* We may still decide to disable prediction later */
2240
82.6k
        if (st->silk_mode.reducedDependency)
2241
23.9k
           celt_pred = 0;
2242
82.6k
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
2243
82.6k
    }
2244
2245
144k
    ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res);
2246
144k
    if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
2247
0
    {
2248
0
       OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
2249
0
    }
2250
2251
144k
    if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
2252
39.9k
    {
2253
39.9k
       OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
2254
39.9k
       OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
2255
39.9k
             &pcm_buf[0],
2256
39.9k
             (frame_size+total_buffer)*st->channels);
2257
104k
    } else {
2258
104k
       OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
2259
104k
    }
2260
    /* gain_fade() and stereo_fade() need to be after the buffer copying
2261
       because we don't want any of this to affect the SILK part */
2262
144k
    if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
2263
4.21k
       gain_fade(pcm_buf, pcm_buf,
2264
4.21k
             st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
2265
4.21k
    }
2266
144k
    st->prev_HB_gain = HB_gain;
2267
144k
    if (st->mode != MODE_HYBRID || st->stream_channels==1)
2268
142k
    {
2269
142k
       if (equiv_rate > 32000)
2270
47.9k
          st->silk_mode.stereoWidth_Q14 = 16384;
2271
94.6k
       else if (equiv_rate < 16000)
2272
33.0k
          st->silk_mode.stereoWidth_Q14 = 0;
2273
61.6k
       else
2274
61.6k
          st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
2275
142k
    }
2276
144k
    if( !st->energy_masking && st->channels == 2 ) {
2277
        /* Apply stereo width reduction (at low bitrates) */
2278
55.7k
        if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
2279
17.6k
            opus_val16 g1, g2;
2280
17.6k
            g1 = st->hybrid_stereo_width_Q14;
2281
17.6k
            g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
2282
#ifdef FIXED_POINT
2283
            g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
2284
            g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
2285
#else
2286
17.6k
            g1 *= (1.f/16384);
2287
17.6k
            g2 *= (1.f/16384);
2288
17.6k
#endif
2289
17.6k
            stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
2290
17.6k
                  frame_size, st->channels, celt_mode->window, st->Fs);
2291
17.6k
            st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
2292
17.6k
        }
2293
55.7k
    }
2294
2295
144k
    if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
2296
56.4k
    {
2297
        /* For SILK mode, the redundancy is inferred from the length */
2298
56.4k
        if (st->mode == MODE_HYBRID)
2299
4.74k
           ec_enc_bit_logp(&enc, redundancy, 12);
2300
56.4k
        if (redundancy)
2301
0
        {
2302
0
            int max_redundancy;
2303
0
            ec_enc_bit_logp(&enc, celt_to_silk, 1);
2304
0
            if (st->mode == MODE_HYBRID)
2305
0
            {
2306
               /* Reserve the 8 bits needed for the redundancy length,
2307
                  and at least a few bits for CELT if possible */
2308
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
2309
0
            }
2310
0
            else
2311
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2312
            /* Target the same bit-rate for redundancy as for the rest,
2313
               up to a max of 257 bytes */
2314
0
            redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
2315
0
            redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2316
0
            if (st->mode == MODE_HYBRID)
2317
0
                ec_enc_uint(&enc, redundancy_bytes-2, 256);
2318
0
        }
2319
87.8k
    } else {
2320
87.8k
        redundancy = 0;
2321
87.8k
    }
2322
2323
144k
    if (!redundancy)
2324
144k
    {
2325
144k
       st->silk_bw_switch = 0;
2326
144k
       redundancy_bytes = 0;
2327
144k
    }
2328
144k
    if (st->mode != MODE_CELT_ONLY)start_band=17;
2329
2330
144k
    if (st->mode == MODE_SILK_ONLY)
2331
61.6k
    {
2332
61.6k
        ret = (ec_tell(&enc)+7)>>3;
2333
61.6k
        ec_enc_done(&enc);
2334
61.6k
        nb_compr_bytes = ret;
2335
82.6k
    } else {
2336
82.6k
        nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2337
82.6k
#ifdef ENABLE_QEXT
2338
82.6k
        if (st->mode == MODE_CELT_ONLY && st->enable_qext) {
2339
0
           celt_assert(redundancy_bytes==0);
2340
0
           nb_compr_bytes = orig_max_data_bytes-1;
2341
0
        }
2342
82.6k
#endif
2343
#ifdef ENABLE_DRED
2344
        if (st->dred_duration > 0)
2345
        {
2346
            int max_celt_bytes;
2347
            opus_int32 dred_bytes = dred_bitrate_bps/(frame_rate*8);
2348
            /* Allow CELT to steal up to 25% of the remaining bits. */
2349
            max_celt_bytes = nb_compr_bytes - dred_bytes*3/4;
2350
            /* But try to give CELT at least 5 bytes to prevent a mismatch with
2351
               the redundancy signaling. */
2352
            max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes);
2353
            /* Subject to the original max. */
2354
            nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes);
2355
        }
2356
#endif
2357
82.6k
        ec_enc_shrink(&enc, nb_compr_bytes);
2358
82.6k
    }
2359
2360
144k
#ifndef DISABLE_FLOAT_API
2361
144k
    if (redundancy || st->mode != MODE_SILK_ONLY)
2362
82.6k
       celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info));
2363
144k
#endif
2364
144k
    if (st->mode == MODE_HYBRID) {
2365
4.84k
       SILKInfo info;
2366
4.84k
       info.signalType = st->silk_mode.signalType;
2367
4.84k
       info.offset = st->silk_mode.offset;
2368
4.84k
       celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2369
4.84k
    }
2370
2371
    /* 5 ms redundant frame for CELT->SILK */
2372
144k
    if (redundancy && celt_to_silk)
2373
0
    {
2374
0
        int err;
2375
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2376
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2377
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2378
0
        err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2379
0
        if (err < 0)
2380
0
        {
2381
0
           RESTORE_STACK;
2382
0
           return OPUS_INTERNAL_ERROR;
2383
0
        }
2384
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2385
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2386
0
    }
2387
2388
144k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2389
144k
       celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2390
2391
144k
    data[-1] = 0;
2392
144k
    if (st->mode != MODE_SILK_ONLY)
2393
82.6k
    {
2394
82.6k
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2395
82.6k
        if (st->mode == MODE_HYBRID)
2396
4.84k
        {
2397
4.84k
            if( st->use_vbr ) {
2398
3.31k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2399
3.31k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2400
3.31k
            }
2401
77.7k
        } else {
2402
77.7k
            if (st->use_vbr)
2403
57.6k
            {
2404
57.6k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2405
57.6k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
2406
57.6k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
2407
57.6k
            }
2408
77.7k
        }
2409
#ifdef ENABLE_DRED
2410
        /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */
2411
        if (!st->use_vbr && st->dred_duration > 0)
2412
        {
2413
            opus_int32 celt_bitrate = st->bitrate_bps;
2414
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2415
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2416
            if (st->mode == MODE_HYBRID) {
2417
                celt_bitrate -= st->silk_mode.bitRate;
2418
            }
2419
            celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate));
2420
        }
2421
#endif
2422
82.6k
        if (st->mode != st->prev_mode && st->prev_mode > 0)
2423
0
        {
2424
0
           unsigned char dummy[2];
2425
0
           celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2426
2427
           /* Prefilling */
2428
0
           celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2429
0
           celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2430
0
        }
2431
        /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2432
82.6k
        if (ec_tell(&enc) <= 8*nb_compr_bytes)
2433
82.5k
        {
2434
82.5k
#ifdef ENABLE_QEXT
2435
82.5k
           if (st->mode == MODE_CELT_ONLY) celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(st->enable_qext));
2436
82.5k
#endif
2437
82.5k
           ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2438
82.5k
#ifdef ENABLE_QEXT
2439
82.5k
           celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(0));
2440
82.5k
#endif
2441
82.5k
           if (ret < 0)
2442
0
           {
2443
0
              RESTORE_STACK;
2444
0
              return OPUS_INTERNAL_ERROR;
2445
0
           }
2446
           /* Put CELT->SILK redundancy data in the right place. */
2447
82.5k
           if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && nb_compr_bytes != ret)
2448
0
           {
2449
0
              OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2450
0
              nb_compr_bytes = ret+redundancy_bytes;
2451
0
           }
2452
82.5k
        }
2453
82.6k
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&st->rangeFinal));
2454
82.6k
    } else {
2455
61.6k
       st->rangeFinal = enc.rng;
2456
61.6k
    }
2457
2458
    /* 5 ms redundant frame for SILK->CELT */
2459
144k
    if (redundancy && !celt_to_silk)
2460
0
    {
2461
0
        int err;
2462
0
        unsigned char dummy[2];
2463
0
        int N2, N4;
2464
0
        N2 = st->Fs/200;
2465
0
        N4 = st->Fs/400;
2466
2467
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2468
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2469
0
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2470
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2471
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2472
2473
0
        if (st->mode == MODE_HYBRID)
2474
0
        {
2475
           /* Shrink packet to what the encoder actually used. */
2476
0
           nb_compr_bytes = ret;
2477
0
           ec_enc_shrink(&enc, nb_compr_bytes);
2478
0
        }
2479
        /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2480
0
        celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2481
2482
0
        err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2483
0
        if (err < 0)
2484
0
        {
2485
0
           RESTORE_STACK;
2486
0
           return OPUS_INTERNAL_ERROR;
2487
0
        }
2488
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2489
0
    }
2490
2491
2492
2493
    /* Signalling the mode in the first byte */
2494
144k
    data--;
2495
144k
    data[0] |= gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2496
2497
144k
    st->rangeFinal ^= redundant_rng;
2498
2499
144k
    if (to_celt)
2500
0
        st->prev_mode = MODE_CELT_ONLY;
2501
144k
    else
2502
144k
        st->prev_mode = st->mode;
2503
144k
    st->prev_channels = st->stream_channels;
2504
144k
    st->prev_framesize = frame_size;
2505
2506
144k
    st->first = 0;
2507
2508
    /* DTX decision */
2509
144k
#ifndef DISABLE_FLOAT_API
2510
144k
    if (st->use_dtx && (analysis_info->valid || is_silence))
2511
11.6k
    {
2512
11.6k
       if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs))
2513
0
       {
2514
0
          st->rangeFinal = 0;
2515
0
          data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2516
0
          RESTORE_STACK;
2517
0
          return 1;
2518
0
       }
2519
132k
    } else {
2520
132k
       st->nb_no_activity_ms_Q1 = 0;
2521
132k
    }
2522
144k
#endif
2523
2524
    /* In the unlikely case that the SILK encoder busted its target, tell
2525
       the decoder to call the PLC */
2526
144k
    if (ec_tell(&enc) > (max_data_bytes-1)*8)
2527
2.12k
    {
2528
2.12k
       if (max_data_bytes < 2)
2529
0
       {
2530
0
          RESTORE_STACK;
2531
0
          return OPUS_BUFFER_TOO_SMALL;
2532
0
       }
2533
2.12k
       data[1] = 0;
2534
2.12k
       ret = 1;
2535
2.12k
       st->rangeFinal = 0;
2536
142k
    } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2537
59.5k
    {
2538
       /*When in LPC only mode it's perfectly
2539
         reasonable to strip off trailing zero bytes as
2540
         the required range decoder behavior is to
2541
         fill these in. This can't be done when the MDCT
2542
         modes are used because the decoder needs to know
2543
         the actual length for allocation purposes.*/
2544
69.1k
       while(ret>2&&data[ret]==0)ret--;
2545
59.5k
    }
2546
    /* Count ToC and redundancy */
2547
144k
    ret += 1+redundancy_bytes;
2548
144k
    apply_padding = !st->use_vbr;
2549
#ifdef ENABLE_DRED
2550
    if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) {
2551
       opus_extension_data extension;
2552
       unsigned char buf[DRED_MAX_DATA_SIZE];
2553
       int dred_chunks;
2554
       int dred_bytes_left;
2555
       dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2);
2556
       if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks);
2557
       /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */
2558
       dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-3);
2559
       /* Account for the extra bytes required to signal large padding length. */
2560
       dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255;
2561
       /* Check whether we actually have something to encode. */
2562
       if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) {
2563
           int dred_bytes;
2564
#ifdef DRED_EXPERIMENTAL_VERSION
2565
           /* Add temporary extension type and version.
2566
              These bytes will be removed once extension is finalized. */
2567
           buf[0] = 'D';
2568
           buf[1] = DRED_EXPERIMENTAL_VERSION;
2569
#endif
2570
           dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES,
2571
                                               st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch);
2572
           if (dred_bytes > 0) {
2573
              dred_bytes += DRED_EXPERIMENTAL_BYTES;
2574
              celt_assert(dred_bytes <= dred_bytes_left);
2575
              extension.id = DRED_EXTENSION_ID;
2576
              extension.frame = 0;
2577
              extension.data = buf;
2578
              extension.len = dred_bytes;
2579
              ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1);
2580
              if (ret < 0)
2581
              {
2582
                 RESTORE_STACK;
2583
                 return OPUS_INTERNAL_ERROR;
2584
              }
2585
              apply_padding = 0;
2586
           }
2587
       }
2588
    }
2589
#else
2590
144k
    (void)first_frame; /* Avoids a warning about first_frame being unused. */
2591
144k
#endif
2592
144k
    if (apply_padding)
2593
56.1k
    {
2594
56.1k
       if (opus_packet_pad(data, ret, orig_max_data_bytes) != OPUS_OK)
2595
0
       {
2596
0
          RESTORE_STACK;
2597
0
          return OPUS_INTERNAL_ERROR;
2598
0
       }
2599
56.1k
       ret = orig_max_data_bytes;
2600
56.1k
    }
2601
144k
    RESTORE_STACK;
2602
144k
    return ret;
2603
144k
}
opus_encoder.c:opus_encode_frame_native
Line
Count
Source
1835
135k
{
1836
135k
    void *silk_enc=NULL;
1837
135k
    CELTEncoder *celt_enc=NULL;
1838
135k
    const CELTMode *celt_mode=NULL;
1839
135k
    int i;
1840
135k
    int ret=0;
1841
135k
    int max_data_bytes;
1842
135k
    opus_int32 nBytes;
1843
135k
    ec_enc enc;
1844
135k
    int bytes_target;
1845
135k
    int start_band = 0;
1846
135k
    int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1847
135k
    int nb_compr_bytes;
1848
135k
    opus_uint32 redundant_rng = 0;
1849
135k
    int cutoff_Hz;
1850
135k
    int hp_freq_smth1;
1851
135k
    opus_val16 HB_gain;
1852
135k
    int apply_padding;
1853
135k
    int frame_rate;
1854
135k
    int curr_bandwidth;
1855
135k
    int delay_compensation;
1856
135k
    int total_buffer;
1857
135k
    opus_int activity = VAD_NO_DECISION;
1858
135k
    VARDECL(opus_res, pcm_buf);
1859
135k
    VARDECL(opus_res, tmp_prefill);
1860
135k
    SAVE_STACK;
1861
1862
135k
    max_data_bytes = IMIN(orig_max_data_bytes, 1276);
1863
135k
    st->rangeFinal = 0;
1864
135k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1865
135k
       silk_enc = (char*)st+st->silk_enc_offset;
1866
135k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1867
135k
    {
1868
135k
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1869
135k
       celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1870
135k
    }
1871
135k
    curr_bandwidth = st->bandwidth;
1872
135k
    if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1873
35.1k
       delay_compensation = 0;
1874
100k
    else
1875
100k
       delay_compensation = st->delay_compensation;
1876
135k
    total_buffer = delay_compensation;
1877
1878
135k
    frame_rate = st->Fs/frame_size;
1879
1880
135k
#ifndef DISABLE_FLOAT_API
1881
135k
    if (is_silence)
1882
2.17k
    {
1883
2.17k
       activity = !is_silence;
1884
133k
    } else if (analysis_info->valid)
1885
23.1k
    {
1886
23.1k
       activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD;
1887
23.1k
       if (!activity)
1888
1.71k
       {
1889
           /* Mark as active if this noise frame is sufficiently loud */
1890
1.71k
           opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1891
1.71k
           activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy);
1892
1.71k
       }
1893
23.1k
    }
1894
135k
#endif
1895
1896
    /* For the first frame at a new SILK bandwidth */
1897
135k
    if (st->silk_bw_switch)
1898
0
    {
1899
0
       redundancy = 1;
1900
0
       celt_to_silk = 1;
1901
0
       st->silk_bw_switch = 0;
1902
       /* Do a prefill without resetting the sampling rate control. */
1903
0
       prefill=2;
1904
0
    }
1905
1906
    /* If we decided to go with CELT, make sure redundancy is off, no matter what
1907
       we decided earlier. */
1908
135k
    if (st->mode == MODE_CELT_ONLY)
1909
73.1k
        redundancy = 0;
1910
1911
135k
    if (redundancy)
1912
0
    {
1913
0
       redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1914
0
       if (redundancy_bytes == 0)
1915
0
          redundancy = 0;
1916
0
    }
1917
1918
    /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1919
135k
    bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1920
1921
135k
    data += 1;
1922
1923
135k
    ec_enc_init(&enc, data, orig_max_data_bytes-1);
1924
1925
135k
    ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res);
1926
135k
    OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1927
1928
135k
    if (st->mode == MODE_CELT_ONLY)
1929
73.1k
       hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1930
62.2k
    else
1931
62.2k
       hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1932
1933
135k
    st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1934
135k
          hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1935
1936
    /* convert from log scale to Hertz */
1937
135k
    cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1938
1939
135k
    if (st->application == OPUS_APPLICATION_VOIP)
1940
28.6k
    {
1941
28.6k
       hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
1942
1943
#ifdef ENABLE_OSCE_TRAINING_DATA
1944
       /* write out high pass filtered clean signal*/
1945
       static FILE *fout =NULL;
1946
       if (fout == NULL)
1947
       {
1948
         fout = fopen("clean_hp.s16", "wb");
1949
       }
1950
1951
       {
1952
         int idx;
1953
         opus_int16 tmp;
1954
         for (idx = 0; idx < frame_size; idx++)
1955
         {
1956
            tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f);
1957
            fwrite(&tmp, sizeof(tmp), 1, fout);
1958
         }
1959
       }
1960
#endif
1961
106k
    } else {
1962
#ifdef ENABLE_QEXT
1963
       /* FIXME: Avoid glitching when we switch qext on/off dynamically. */
1964
       if (st->enable_qext) OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels);
1965
       else
1966
#endif
1967
106k
       dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1968
106k
    }
1969
135k
#ifndef FIXED_POINT
1970
135k
    if (float_api)
1971
0
    {
1972
0
       opus_val32 sum;
1973
0
       sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1974
       /* This should filter out both NaNs and ridiculous signals that could
1975
          cause NaNs further down. */
1976
0
       if (!(sum < 1e9f) || celt_isnan(sum))
1977
0
       {
1978
0
          OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1979
0
          st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1980
0
       }
1981
0
    }
1982
#else
1983
    (void)float_api;
1984
#endif
1985
1986
#ifdef ENABLE_DRED
1987
    if ( st->dred_duration > 0 && st->dred_encoder.loaded ) {
1988
        int frame_size_400Hz;
1989
        /* DRED Encoder */
1990
        dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch );
1991
        frame_size_400Hz = frame_size*400/st->Fs;
1992
        OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz);
1993
        for (i=0;i<frame_size_400Hz;i++)
1994
           st->activity_mem[i] = activity;
1995
    } else {
1996
        st->dred_encoder.latents_buffer_fill = 0;
1997
        OPUS_CLEAR(st->activity_mem, DRED_MAX_FRAMES);
1998
    }
1999
#endif
2000
2001
    /* SILK processing */
2002
135k
    HB_gain = Q15ONE;
2003
135k
    if (st->mode != MODE_CELT_ONLY)
2004
62.2k
    {
2005
62.2k
        opus_int32 total_bitRate, celt_rate;
2006
62.2k
        const opus_res *pcm_silk;
2007
2008
        /* Distribute bits between SILK and CELT */
2009
62.2k
        total_bitRate = 8 * bytes_target * frame_rate;
2010
62.2k
        if( st->mode == MODE_HYBRID ) {
2011
            /* Base rate for SILK */
2012
4.38k
            st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
2013
4.38k
                  curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2014
4.38k
                  st->stream_channels);
2015
4.38k
            if (!st->energy_masking)
2016
4.38k
            {
2017
               /* Increasingly attenuate high band when it gets allocated fewer bits */
2018
4.38k
               celt_rate = total_bitRate - st->silk_mode.bitRate;
2019
4.38k
               HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
2020
4.38k
            }
2021
57.8k
        } else {
2022
            /* SILK gets all bits */
2023
57.8k
            st->silk_mode.bitRate = total_bitRate;
2024
57.8k
        }
2025
2026
        /* Surround masking for SILK */
2027
62.2k
        if (st->energy_masking && st->use_vbr && !st->lfe)
2028
194
        {
2029
194
           opus_val32 mask_sum=0;
2030
194
           celt_glog masking_depth;
2031
194
           opus_int32 rate_offset;
2032
194
           int c;
2033
194
           int end = 17;
2034
194
           opus_int16 srate = 16000;
2035
194
           if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
2036
194
           {
2037
194
              end = 13;
2038
194
              srate = 8000;
2039
194
           } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
2040
0
           {
2041
0
              end = 15;
2042
0
              srate = 12000;
2043
0
           }
2044
388
           for (c=0;c<st->channels;c++)
2045
194
           {
2046
2.71k
              for(i=0;i<end;i++)
2047
2.52k
              {
2048
2.52k
                 celt_glog mask;
2049
2.52k
                 mask = MAXG(MING(st->energy_masking[21*c+i],
2050
2.52k
                        GCONST(.5f)), -GCONST(2.0f));
2051
2.52k
                 if (mask > 0)
2052
658
                    mask = HALF32(mask);
2053
2.52k
                 mask_sum += mask;
2054
2.52k
              }
2055
194
           }
2056
           /* Conservative rate reduction, we cut the masking in half */
2057
194
           masking_depth = mask_sum / end*st->channels;
2058
194
           masking_depth += GCONST(.2f);
2059
194
           rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10);
2060
194
           rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
2061
           /* Split the rate change between the SILK and CELT part for hybrid. */
2062
194
           if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
2063
0
              st->silk_mode.bitRate += 3*rate_offset/5;
2064
194
           else
2065
194
              st->silk_mode.bitRate += rate_offset;
2066
194
        }
2067
2068
62.2k
        st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
2069
62.2k
        st->silk_mode.nChannelsAPI = st->channels;
2070
62.2k
        st->silk_mode.nChannelsInternal = st->stream_channels;
2071
62.2k
        if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2072
42.5k
            st->silk_mode.desiredInternalSampleRate = 8000;
2073
42.5k
        } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2074
6.63k
            st->silk_mode.desiredInternalSampleRate = 12000;
2075
13.0k
        } else {
2076
13.0k
            celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
2077
13.0k
            st->silk_mode.desiredInternalSampleRate = 16000;
2078
13.0k
        }
2079
62.2k
        if( st->mode == MODE_HYBRID ) {
2080
            /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
2081
4.38k
            st->silk_mode.minInternalSampleRate = 16000;
2082
57.8k
        } else {
2083
57.8k
            st->silk_mode.minInternalSampleRate = 8000;
2084
57.8k
        }
2085
2086
62.2k
        st->silk_mode.maxInternalSampleRate = 16000;
2087
62.2k
        if (st->mode == MODE_SILK_ONLY)
2088
57.8k
        {
2089
57.8k
           opus_int32 effective_max_rate = frame_rate*max_data_bytes*8;
2090
57.8k
           if (frame_rate > 50)
2091
19.8k
              effective_max_rate = effective_max_rate*2/3;
2092
57.8k
           if (effective_max_rate < 8000)
2093
1.13k
           {
2094
1.13k
              st->silk_mode.maxInternalSampleRate = 12000;
2095
1.13k
              st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
2096
1.13k
           }
2097
57.8k
           if (effective_max_rate < 7000)
2098
751
           {
2099
751
              st->silk_mode.maxInternalSampleRate = 8000;
2100
751
              st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
2101
751
           }
2102
#ifdef ENABLE_QEXT
2103
           /* At 96 kHz, we don't have the input resampler to do 8 or 12 kHz. */
2104
           if (st->Fs==96000) st->silk_mode.maxInternalSampleRate = st->silk_mode.desiredInternalSampleRate = 16000;
2105
#endif
2106
57.8k
        }
2107
2108
62.2k
        st->silk_mode.useCBR = !st->use_vbr;
2109
2110
        /* Call SILK encoder for the low band */
2111
2112
        /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
2113
62.2k
        st->silk_mode.maxBits = (max_data_bytes-1)*8;
2114
62.2k
        if (redundancy && redundancy_bytes >= 2)
2115
0
        {
2116
           /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
2117
0
           st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
2118
0
           if (st->mode == MODE_HYBRID)
2119
0
              st->silk_mode.maxBits -= 20;
2120
0
        }
2121
62.2k
        if (st->silk_mode.useCBR)
2122
31.4k
        {
2123
           /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to
2124
              save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still
2125
              produce a constant bitrate without wasting bits. */
2126
#ifdef ENABLE_DRED
2127
           if (st->mode == MODE_HYBRID || dred_bitrate_bps > 0)
2128
#else
2129
31.4k
           if (st->mode == MODE_HYBRID)
2130
981
#endif
2131
981
           {
2132
              /* Allow SILK to steal up to 25% of the remaining bits */
2133
981
              opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs);
2134
981
              st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4);
2135
981
              st->silk_mode.useCBR = 0;
2136
981
           }
2137
31.4k
        } else {
2138
           /* Constrained VBR. */
2139
30.8k
           if (st->mode == MODE_HYBRID)
2140
3.40k
           {
2141
              /* Compute SILK bitrate corresponding to the max total bits available */
2142
3.40k
              opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
2143
3.40k
                    curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2144
3.40k
                    st->stream_channels);
2145
3.40k
              st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
2146
3.40k
           }
2147
30.8k
        }
2148
2149
62.2k
        if (prefill)
2150
0
        {
2151
0
            opus_int32 zero=0;
2152
0
            int prefill_offset;
2153
            /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
2154
               a discontinuity. The exact location is what we need to avoid leaving any "gap"
2155
               in the audio when mixing with the redundant CELT frame. Here we can afford to
2156
               overwrite st->delay_buffer because the only thing that uses it before it gets
2157
               rewritten is tmp_prefill[] and even then only the part after the ramp really
2158
               gets used (rather than sent to the encoder and discarded) */
2159
0
            prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
2160
0
            gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
2161
0
                  0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
2162
0
            OPUS_CLEAR(st->delay_buffer, prefill_offset);
2163
0
            pcm_silk = st->delay_buffer;
2164
0
            silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity );
2165
            /* Prevent a second switch in the real encode call. */
2166
0
            st->silk_mode.opusCanSwitch = 0;
2167
0
        }
2168
2169
62.2k
        pcm_silk = pcm_buf+total_buffer*st->channels;
2170
62.2k
        ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
2171
62.2k
        if( ret ) {
2172
            /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
2173
            /* Handle error */
2174
0
           RESTORE_STACK;
2175
0
           return OPUS_INTERNAL_ERROR;
2176
0
        }
2177
2178
        /* Extract SILK internal bandwidth for signaling in first byte */
2179
62.2k
        if( st->mode == MODE_SILK_ONLY ) {
2180
57.8k
            if( st->silk_mode.internalSampleRate == 8000 ) {
2181
42.7k
               curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
2182
42.7k
            } else if( st->silk_mode.internalSampleRate == 12000 ) {
2183
6.64k
               curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
2184
8.40k
            } else if( st->silk_mode.internalSampleRate == 16000 ) {
2185
8.40k
               curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
2186
8.40k
            }
2187
57.8k
        } else {
2188
4.38k
            celt_assert( st->silk_mode.internalSampleRate == 16000 );
2189
4.38k
        }
2190
2191
62.2k
        st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
2192
2193
62.2k
        if (nBytes==0)
2194
0
        {
2195
0
           st->rangeFinal = 0;
2196
0
           data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2197
0
           RESTORE_STACK;
2198
0
           return 1;
2199
0
        }
2200
2201
        /* FIXME: How do we allocate the redundancy for CBR? */
2202
62.2k
        if (st->silk_mode.opusCanSwitch)
2203
0
        {
2204
0
           redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
2205
0
           redundancy = (redundancy_bytes != 0);
2206
0
           celt_to_silk = 0;
2207
0
           st->silk_bw_switch = 1;
2208
0
        }
2209
62.2k
    }
2210
2211
    /* CELT processing */
2212
135k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2213
135k
    {
2214
135k
        int endband=21;
2215
2216
135k
        switch(curr_bandwidth)
2217
135k
        {
2218
92.8k
            case OPUS_BANDWIDTH_NARROWBAND:
2219
92.8k
                endband = 13;
2220
92.8k
                break;
2221
6.64k
            case OPUS_BANDWIDTH_MEDIUMBAND:
2222
29.4k
            case OPUS_BANDWIDTH_WIDEBAND:
2223
29.4k
                endband = 17;
2224
29.4k
                break;
2225
9.13k
            case OPUS_BANDWIDTH_SUPERWIDEBAND:
2226
9.13k
                endband = 19;
2227
9.13k
                break;
2228
3.92k
            case OPUS_BANDWIDTH_FULLBAND:
2229
3.92k
                endband = 21;
2230
3.92k
                break;
2231
135k
        }
2232
135k
        celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
2233
135k
        celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
2234
135k
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2235
135k
    }
2236
135k
    if (st->mode != MODE_SILK_ONLY)
2237
77.5k
    {
2238
77.5k
        opus_val32 celt_pred=2;
2239
        /* We may still decide to disable prediction later */
2240
77.5k
        if (st->silk_mode.reducedDependency)
2241
19.3k
           celt_pred = 0;
2242
77.5k
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
2243
77.5k
    }
2244
2245
135k
    ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res);
2246
135k
    if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
2247
0
    {
2248
0
       OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
2249
0
    }
2250
2251
135k
    if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
2252
37.1k
    {
2253
37.1k
       OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
2254
37.1k
       OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
2255
37.1k
             &pcm_buf[0],
2256
37.1k
             (frame_size+total_buffer)*st->channels);
2257
98.2k
    } else {
2258
98.2k
       OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
2259
98.2k
    }
2260
    /* gain_fade() and stereo_fade() need to be after the buffer copying
2261
       because we don't want any of this to affect the SILK part */
2262
135k
    if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
2263
3.83k
       gain_fade(pcm_buf, pcm_buf,
2264
3.83k
             st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
2265
3.83k
    }
2266
135k
    st->prev_HB_gain = HB_gain;
2267
135k
    if (st->mode != MODE_HYBRID || st->stream_channels==1)
2268
133k
    {
2269
133k
       if (equiv_rate > 32000)
2270
44.2k
          st->silk_mode.stereoWidth_Q14 = 16384;
2271
89.3k
       else if (equiv_rate < 16000)
2272
31.9k
          st->silk_mode.stereoWidth_Q14 = 0;
2273
57.4k
       else
2274
57.4k
          st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
2275
133k
    }
2276
135k
    if( !st->energy_masking && st->channels == 2 ) {
2277
        /* Apply stereo width reduction (at low bitrates) */
2278
52.9k
        if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
2279
15.1k
            opus_val16 g1, g2;
2280
15.1k
            g1 = st->hybrid_stereo_width_Q14;
2281
15.1k
            g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
2282
#ifdef FIXED_POINT
2283
            g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
2284
            g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
2285
#else
2286
15.1k
            g1 *= (1.f/16384);
2287
15.1k
            g2 *= (1.f/16384);
2288
15.1k
#endif
2289
15.1k
            stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
2290
15.1k
                  frame_size, st->channels, celt_mode->window, st->Fs);
2291
15.1k
            st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
2292
15.1k
        }
2293
52.9k
    }
2294
2295
135k
    if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
2296
53.2k
    {
2297
        /* For SILK mode, the redundancy is inferred from the length */
2298
53.2k
        if (st->mode == MODE_HYBRID)
2299
4.26k
           ec_enc_bit_logp(&enc, redundancy, 12);
2300
53.2k
        if (redundancy)
2301
0
        {
2302
0
            int max_redundancy;
2303
0
            ec_enc_bit_logp(&enc, celt_to_silk, 1);
2304
0
            if (st->mode == MODE_HYBRID)
2305
0
            {
2306
               /* Reserve the 8 bits needed for the redundancy length,
2307
                  and at least a few bits for CELT if possible */
2308
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
2309
0
            }
2310
0
            else
2311
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2312
            /* Target the same bit-rate for redundancy as for the rest,
2313
               up to a max of 257 bytes */
2314
0
            redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
2315
0
            redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2316
0
            if (st->mode == MODE_HYBRID)
2317
0
                ec_enc_uint(&enc, redundancy_bytes-2, 256);
2318
0
        }
2319
82.1k
    } else {
2320
82.1k
        redundancy = 0;
2321
82.1k
    }
2322
2323
135k
    if (!redundancy)
2324
135k
    {
2325
135k
       st->silk_bw_switch = 0;
2326
135k
       redundancy_bytes = 0;
2327
135k
    }
2328
135k
    if (st->mode != MODE_CELT_ONLY)start_band=17;
2329
2330
135k
    if (st->mode == MODE_SILK_ONLY)
2331
57.8k
    {
2332
57.8k
        ret = (ec_tell(&enc)+7)>>3;
2333
57.8k
        ec_enc_done(&enc);
2334
57.8k
        nb_compr_bytes = ret;
2335
77.5k
    } else {
2336
77.5k
        nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2337
#ifdef ENABLE_QEXT
2338
        if (st->mode == MODE_CELT_ONLY && st->enable_qext) {
2339
           celt_assert(redundancy_bytes==0);
2340
           nb_compr_bytes = orig_max_data_bytes-1;
2341
        }
2342
#endif
2343
#ifdef ENABLE_DRED
2344
        if (st->dred_duration > 0)
2345
        {
2346
            int max_celt_bytes;
2347
            opus_int32 dred_bytes = dred_bitrate_bps/(frame_rate*8);
2348
            /* Allow CELT to steal up to 25% of the remaining bits. */
2349
            max_celt_bytes = nb_compr_bytes - dred_bytes*3/4;
2350
            /* But try to give CELT at least 5 bytes to prevent a mismatch with
2351
               the redundancy signaling. */
2352
            max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes);
2353
            /* Subject to the original max. */
2354
            nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes);
2355
        }
2356
#endif
2357
77.5k
        ec_enc_shrink(&enc, nb_compr_bytes);
2358
77.5k
    }
2359
2360
135k
#ifndef DISABLE_FLOAT_API
2361
135k
    if (redundancy || st->mode != MODE_SILK_ONLY)
2362
77.5k
       celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info));
2363
135k
#endif
2364
135k
    if (st->mode == MODE_HYBRID) {
2365
4.38k
       SILKInfo info;
2366
4.38k
       info.signalType = st->silk_mode.signalType;
2367
4.38k
       info.offset = st->silk_mode.offset;
2368
4.38k
       celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2369
4.38k
    }
2370
2371
    /* 5 ms redundant frame for CELT->SILK */
2372
135k
    if (redundancy && celt_to_silk)
2373
0
    {
2374
0
        int err;
2375
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2376
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2377
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2378
0
        err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2379
0
        if (err < 0)
2380
0
        {
2381
0
           RESTORE_STACK;
2382
0
           return OPUS_INTERNAL_ERROR;
2383
0
        }
2384
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2385
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2386
0
    }
2387
2388
135k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2389
135k
       celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2390
2391
135k
    data[-1] = 0;
2392
135k
    if (st->mode != MODE_SILK_ONLY)
2393
77.5k
    {
2394
77.5k
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2395
77.5k
        if (st->mode == MODE_HYBRID)
2396
4.38k
        {
2397
4.38k
            if( st->use_vbr ) {
2398
3.40k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2399
3.40k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2400
3.40k
            }
2401
73.1k
        } else {
2402
73.1k
            if (st->use_vbr)
2403
55.3k
            {
2404
55.3k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2405
55.3k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
2406
55.3k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
2407
55.3k
            }
2408
73.1k
        }
2409
#ifdef ENABLE_DRED
2410
        /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */
2411
        if (!st->use_vbr && st->dred_duration > 0)
2412
        {
2413
            opus_int32 celt_bitrate = st->bitrate_bps;
2414
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2415
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2416
            if (st->mode == MODE_HYBRID) {
2417
                celt_bitrate -= st->silk_mode.bitRate;
2418
            }
2419
            celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate));
2420
        }
2421
#endif
2422
77.5k
        if (st->mode != st->prev_mode && st->prev_mode > 0)
2423
0
        {
2424
0
           unsigned char dummy[2];
2425
0
           celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2426
2427
           /* Prefilling */
2428
0
           celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2429
0
           celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2430
0
        }
2431
        /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2432
77.5k
        if (ec_tell(&enc) <= 8*nb_compr_bytes)
2433
77.4k
        {
2434
#ifdef ENABLE_QEXT
2435
           if (st->mode == MODE_CELT_ONLY) celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(st->enable_qext));
2436
#endif
2437
77.4k
           ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2438
#ifdef ENABLE_QEXT
2439
           celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(0));
2440
#endif
2441
77.4k
           if (ret < 0)
2442
0
           {
2443
0
              RESTORE_STACK;
2444
0
              return OPUS_INTERNAL_ERROR;
2445
0
           }
2446
           /* Put CELT->SILK redundancy data in the right place. */
2447
77.4k
           if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && nb_compr_bytes != ret)
2448
0
           {
2449
0
              OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2450
0
              nb_compr_bytes = ret+redundancy_bytes;
2451
0
           }
2452
77.4k
        }
2453
77.5k
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&st->rangeFinal));
2454
77.5k
    } else {
2455
57.8k
       st->rangeFinal = enc.rng;
2456
57.8k
    }
2457
2458
    /* 5 ms redundant frame for SILK->CELT */
2459
135k
    if (redundancy && !celt_to_silk)
2460
0
    {
2461
0
        int err;
2462
0
        unsigned char dummy[2];
2463
0
        int N2, N4;
2464
0
        N2 = st->Fs/200;
2465
0
        N4 = st->Fs/400;
2466
2467
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2468
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2469
0
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2470
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2471
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2472
2473
0
        if (st->mode == MODE_HYBRID)
2474
0
        {
2475
           /* Shrink packet to what the encoder actually used. */
2476
0
           nb_compr_bytes = ret;
2477
0
           ec_enc_shrink(&enc, nb_compr_bytes);
2478
0
        }
2479
        /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2480
0
        celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2481
2482
0
        err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2483
0
        if (err < 0)
2484
0
        {
2485
0
           RESTORE_STACK;
2486
0
           return OPUS_INTERNAL_ERROR;
2487
0
        }
2488
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2489
0
    }
2490
2491
2492
2493
    /* Signalling the mode in the first byte */
2494
135k
    data--;
2495
135k
    data[0] |= gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2496
2497
135k
    st->rangeFinal ^= redundant_rng;
2498
2499
135k
    if (to_celt)
2500
0
        st->prev_mode = MODE_CELT_ONLY;
2501
135k
    else
2502
135k
        st->prev_mode = st->mode;
2503
135k
    st->prev_channels = st->stream_channels;
2504
135k
    st->prev_framesize = frame_size;
2505
2506
135k
    st->first = 0;
2507
2508
    /* DTX decision */
2509
135k
#ifndef DISABLE_FLOAT_API
2510
135k
    if (st->use_dtx && (analysis_info->valid || is_silence))
2511
12.1k
    {
2512
12.1k
       if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs))
2513
0
       {
2514
0
          st->rangeFinal = 0;
2515
0
          data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2516
0
          RESTORE_STACK;
2517
0
          return 1;
2518
0
       }
2519
123k
    } else {
2520
123k
       st->nb_no_activity_ms_Q1 = 0;
2521
123k
    }
2522
135k
#endif
2523
2524
    /* In the unlikely case that the SILK encoder busted its target, tell
2525
       the decoder to call the PLC */
2526
135k
    if (ec_tell(&enc) > (max_data_bytes-1)*8)
2527
1.92k
    {
2528
1.92k
       if (max_data_bytes < 2)
2529
0
       {
2530
0
          RESTORE_STACK;
2531
0
          return OPUS_BUFFER_TOO_SMALL;
2532
0
       }
2533
1.92k
       data[1] = 0;
2534
1.92k
       ret = 1;
2535
1.92k
       st->rangeFinal = 0;
2536
133k
    } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2537
55.9k
    {
2538
       /*When in LPC only mode it's perfectly
2539
         reasonable to strip off trailing zero bytes as
2540
         the required range decoder behavior is to
2541
         fill these in. This can't be done when the MDCT
2542
         modes are used because the decoder needs to know
2543
         the actual length for allocation purposes.*/
2544
65.4k
       while(ret>2&&data[ret]==0)ret--;
2545
55.9k
    }
2546
    /* Count ToC and redundancy */
2547
135k
    ret += 1+redundancy_bytes;
2548
135k
    apply_padding = !st->use_vbr;
2549
#ifdef ENABLE_DRED
2550
    if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) {
2551
       opus_extension_data extension;
2552
       unsigned char buf[DRED_MAX_DATA_SIZE];
2553
       int dred_chunks;
2554
       int dred_bytes_left;
2555
       dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2);
2556
       if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks);
2557
       /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */
2558
       dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-3);
2559
       /* Account for the extra bytes required to signal large padding length. */
2560
       dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255;
2561
       /* Check whether we actually have something to encode. */
2562
       if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) {
2563
           int dred_bytes;
2564
#ifdef DRED_EXPERIMENTAL_VERSION
2565
           /* Add temporary extension type and version.
2566
              These bytes will be removed once extension is finalized. */
2567
           buf[0] = 'D';
2568
           buf[1] = DRED_EXPERIMENTAL_VERSION;
2569
#endif
2570
           dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES,
2571
                                               st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch);
2572
           if (dred_bytes > 0) {
2573
              dred_bytes += DRED_EXPERIMENTAL_BYTES;
2574
              celt_assert(dred_bytes <= dred_bytes_left);
2575
              extension.id = DRED_EXTENSION_ID;
2576
              extension.frame = 0;
2577
              extension.data = buf;
2578
              extension.len = dred_bytes;
2579
              ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1);
2580
              if (ret < 0)
2581
              {
2582
                 RESTORE_STACK;
2583
                 return OPUS_INTERNAL_ERROR;
2584
              }
2585
              apply_padding = 0;
2586
           }
2587
       }
2588
    }
2589
#else
2590
135k
    (void)first_frame; /* Avoids a warning about first_frame being unused. */
2591
135k
#endif
2592
135k
    if (apply_padding)
2593
49.2k
    {
2594
49.2k
       if (opus_packet_pad(data, ret, orig_max_data_bytes) != OPUS_OK)
2595
0
       {
2596
0
          RESTORE_STACK;
2597
0
          return OPUS_INTERNAL_ERROR;
2598
0
       }
2599
49.2k
       ret = orig_max_data_bytes;
2600
49.2k
    }
2601
135k
    RESTORE_STACK;
2602
135k
    return ret;
2603
135k
}
opus_encoder.c:opus_encode_frame_native
Line
Count
Source
1835
146k
{
1836
146k
    void *silk_enc=NULL;
1837
146k
    CELTEncoder *celt_enc=NULL;
1838
146k
    const CELTMode *celt_mode=NULL;
1839
146k
    int i;
1840
146k
    int ret=0;
1841
146k
    int max_data_bytes;
1842
146k
    opus_int32 nBytes;
1843
146k
    ec_enc enc;
1844
146k
    int bytes_target;
1845
146k
    int start_band = 0;
1846
146k
    int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1847
146k
    int nb_compr_bytes;
1848
146k
    opus_uint32 redundant_rng = 0;
1849
146k
    int cutoff_Hz;
1850
146k
    int hp_freq_smth1;
1851
146k
    opus_val16 HB_gain;
1852
146k
    int apply_padding;
1853
146k
    int frame_rate;
1854
146k
    int curr_bandwidth;
1855
146k
    int delay_compensation;
1856
146k
    int total_buffer;
1857
146k
    opus_int activity = VAD_NO_DECISION;
1858
146k
    VARDECL(opus_res, pcm_buf);
1859
146k
    VARDECL(opus_res, tmp_prefill);
1860
146k
    SAVE_STACK;
1861
1862
146k
    max_data_bytes = IMIN(orig_max_data_bytes, 1276);
1863
146k
    st->rangeFinal = 0;
1864
146k
    if (st->application != OPUS_APPLICATION_FORCED_CELT)
1865
146k
       silk_enc = (char*)st+st->silk_enc_offset;
1866
146k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
1867
146k
    {
1868
146k
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1869
146k
       celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1870
146k
    }
1871
146k
    curr_bandwidth = st->bandwidth;
1872
146k
    if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_FORCED_CELT)
1873
39.9k
       delay_compensation = 0;
1874
106k
    else
1875
106k
       delay_compensation = st->delay_compensation;
1876
146k
    total_buffer = delay_compensation;
1877
1878
146k
    frame_rate = st->Fs/frame_size;
1879
1880
146k
#ifndef DISABLE_FLOAT_API
1881
146k
    if (is_silence)
1882
1.19k
    {
1883
1.19k
       activity = !is_silence;
1884
145k
    } else if (analysis_info->valid)
1885
22.0k
    {
1886
22.0k
       activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD;
1887
22.0k
       if (!activity)
1888
1.63k
       {
1889
           /* Mark as active if this noise frame is sufficiently loud */
1890
1.63k
           opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1891
1.63k
           activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy);
1892
1.63k
       }
1893
22.0k
    }
1894
146k
#endif
1895
1896
    /* For the first frame at a new SILK bandwidth */
1897
146k
    if (st->silk_bw_switch)
1898
0
    {
1899
0
       redundancy = 1;
1900
0
       celt_to_silk = 1;
1901
0
       st->silk_bw_switch = 0;
1902
       /* Do a prefill without resetting the sampling rate control. */
1903
0
       prefill=2;
1904
0
    }
1905
1906
    /* If we decided to go with CELT, make sure redundancy is off, no matter what
1907
       we decided earlier. */
1908
146k
    if (st->mode == MODE_CELT_ONLY)
1909
81.5k
        redundancy = 0;
1910
1911
146k
    if (redundancy)
1912
0
    {
1913
0
       redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1914
0
       if (redundancy_bytes == 0)
1915
0
          redundancy = 0;
1916
0
    }
1917
1918
    /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1919
146k
    bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1920
1921
146k
    data += 1;
1922
1923
146k
    ec_enc_init(&enc, data, orig_max_data_bytes-1);
1924
1925
146k
    ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res);
1926
146k
    OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1927
1928
146k
    if (st->mode == MODE_CELT_ONLY)
1929
81.5k
       hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1930
64.8k
    else
1931
64.8k
       hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1932
1933
146k
    st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1934
146k
          hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1935
1936
    /* convert from log scale to Hertz */
1937
146k
    cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1938
1939
146k
    if (st->application == OPUS_APPLICATION_VOIP)
1940
42.3k
    {
1941
42.3k
       hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
1942
1943
#ifdef ENABLE_OSCE_TRAINING_DATA
1944
       /* write out high pass filtered clean signal*/
1945
       static FILE *fout =NULL;
1946
       if (fout == NULL)
1947
       {
1948
         fout = fopen("clean_hp.s16", "wb");
1949
       }
1950
1951
       {
1952
         int idx;
1953
         opus_int16 tmp;
1954
         for (idx = 0; idx < frame_size; idx++)
1955
         {
1956
            tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f);
1957
            fwrite(&tmp, sizeof(tmp), 1, fout);
1958
         }
1959
       }
1960
#endif
1961
104k
    } else {
1962
#ifdef ENABLE_QEXT
1963
       /* FIXME: Avoid glitching when we switch qext on/off dynamically. */
1964
       if (st->enable_qext) OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels);
1965
       else
1966
#endif
1967
104k
       dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1968
104k
    }
1969
#ifndef FIXED_POINT
1970
    if (float_api)
1971
    {
1972
       opus_val32 sum;
1973
       sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1974
       /* This should filter out both NaNs and ridiculous signals that could
1975
          cause NaNs further down. */
1976
       if (!(sum < 1e9f) || celt_isnan(sum))
1977
       {
1978
          OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1979
          st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1980
       }
1981
    }
1982
#else
1983
146k
    (void)float_api;
1984
146k
#endif
1985
1986
#ifdef ENABLE_DRED
1987
    if ( st->dred_duration > 0 && st->dred_encoder.loaded ) {
1988
        int frame_size_400Hz;
1989
        /* DRED Encoder */
1990
        dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch );
1991
        frame_size_400Hz = frame_size*400/st->Fs;
1992
        OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz);
1993
        for (i=0;i<frame_size_400Hz;i++)
1994
           st->activity_mem[i] = activity;
1995
    } else {
1996
        st->dred_encoder.latents_buffer_fill = 0;
1997
        OPUS_CLEAR(st->activity_mem, DRED_MAX_FRAMES);
1998
    }
1999
#endif
2000
2001
    /* SILK processing */
2002
146k
    HB_gain = Q15ONE;
2003
146k
    if (st->mode != MODE_CELT_ONLY)
2004
64.8k
    {
2005
64.8k
        opus_int32 total_bitRate, celt_rate;
2006
64.8k
        const opus_res *pcm_silk;
2007
2008
        /* Distribute bits between SILK and CELT */
2009
64.8k
        total_bitRate = 8 * bytes_target * frame_rate;
2010
64.8k
        if( st->mode == MODE_HYBRID ) {
2011
            /* Base rate for SILK */
2012
4.27k
            st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
2013
4.27k
                  curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2014
4.27k
                  st->stream_channels);
2015
4.27k
            if (!st->energy_masking)
2016
4.27k
            {
2017
               /* Increasingly attenuate high band when it gets allocated fewer bits */
2018
4.27k
               celt_rate = total_bitRate - st->silk_mode.bitRate;
2019
4.27k
               HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
2020
4.27k
            }
2021
60.5k
        } else {
2022
            /* SILK gets all bits */
2023
60.5k
            st->silk_mode.bitRate = total_bitRate;
2024
60.5k
        }
2025
2026
        /* Surround masking for SILK */
2027
64.8k
        if (st->energy_masking && st->use_vbr && !st->lfe)
2028
149
        {
2029
149
           opus_val32 mask_sum=0;
2030
149
           celt_glog masking_depth;
2031
149
           opus_int32 rate_offset;
2032
149
           int c;
2033
149
           int end = 17;
2034
149
           opus_int16 srate = 16000;
2035
149
           if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
2036
149
           {
2037
149
              end = 13;
2038
149
              srate = 8000;
2039
149
           } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
2040
0
           {
2041
0
              end = 15;
2042
0
              srate = 12000;
2043
0
           }
2044
298
           for (c=0;c<st->channels;c++)
2045
149
           {
2046
2.08k
              for(i=0;i<end;i++)
2047
1.93k
              {
2048
1.93k
                 celt_glog mask;
2049
1.93k
                 mask = MAXG(MING(st->energy_masking[21*c+i],
2050
1.93k
                        GCONST(.5f)), -GCONST(2.0f));
2051
1.93k
                 if (mask > 0)
2052
1.00k
                    mask = HALF32(mask);
2053
1.93k
                 mask_sum += mask;
2054
1.93k
              }
2055
149
           }
2056
           /* Conservative rate reduction, we cut the masking in half */
2057
149
           masking_depth = mask_sum / end*st->channels;
2058
149
           masking_depth += GCONST(.2f);
2059
149
           rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10);
2060
149
           rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
2061
           /* Split the rate change between the SILK and CELT part for hybrid. */
2062
149
           if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
2063
0
              st->silk_mode.bitRate += 3*rate_offset/5;
2064
149
           else
2065
149
              st->silk_mode.bitRate += rate_offset;
2066
149
        }
2067
2068
64.8k
        st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
2069
64.8k
        st->silk_mode.nChannelsAPI = st->channels;
2070
64.8k
        st->silk_mode.nChannelsInternal = st->stream_channels;
2071
64.8k
        if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2072
48.1k
            st->silk_mode.desiredInternalSampleRate = 8000;
2073
48.1k
        } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2074
5.40k
            st->silk_mode.desiredInternalSampleRate = 12000;
2075
11.2k
        } else {
2076
11.2k
            celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
2077
11.2k
            st->silk_mode.desiredInternalSampleRate = 16000;
2078
11.2k
        }
2079
64.8k
        if( st->mode == MODE_HYBRID ) {
2080
            /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
2081
4.27k
            st->silk_mode.minInternalSampleRate = 16000;
2082
60.5k
        } else {
2083
60.5k
            st->silk_mode.minInternalSampleRate = 8000;
2084
60.5k
        }
2085
2086
64.8k
        st->silk_mode.maxInternalSampleRate = 16000;
2087
64.8k
        if (st->mode == MODE_SILK_ONLY)
2088
60.5k
        {
2089
60.5k
           opus_int32 effective_max_rate = frame_rate*max_data_bytes*8;
2090
60.5k
           if (frame_rate > 50)
2091
21.0k
              effective_max_rate = effective_max_rate*2/3;
2092
60.5k
           if (effective_max_rate < 8000)
2093
1.07k
           {
2094
1.07k
              st->silk_mode.maxInternalSampleRate = 12000;
2095
1.07k
              st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
2096
1.07k
           }
2097
60.5k
           if (effective_max_rate < 7000)
2098
738
           {
2099
738
              st->silk_mode.maxInternalSampleRate = 8000;
2100
738
              st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
2101
738
           }
2102
#ifdef ENABLE_QEXT
2103
           /* At 96 kHz, we don't have the input resampler to do 8 or 12 kHz. */
2104
           if (st->Fs==96000) st->silk_mode.maxInternalSampleRate = st->silk_mode.desiredInternalSampleRate = 16000;
2105
#endif
2106
60.5k
        }
2107
2108
64.8k
        st->silk_mode.useCBR = !st->use_vbr;
2109
2110
        /* Call SILK encoder for the low band */
2111
2112
        /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
2113
64.8k
        st->silk_mode.maxBits = (max_data_bytes-1)*8;
2114
64.8k
        if (redundancy && redundancy_bytes >= 2)
2115
0
        {
2116
           /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
2117
0
           st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
2118
0
           if (st->mode == MODE_HYBRID)
2119
0
              st->silk_mode.maxBits -= 20;
2120
0
        }
2121
64.8k
        if (st->silk_mode.useCBR)
2122
35.1k
        {
2123
           /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to
2124
              save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still
2125
              produce a constant bitrate without wasting bits. */
2126
#ifdef ENABLE_DRED
2127
           if (st->mode == MODE_HYBRID || dred_bitrate_bps > 0)
2128
#else
2129
35.1k
           if (st->mode == MODE_HYBRID)
2130
1.15k
#endif
2131
1.15k
           {
2132
              /* Allow SILK to steal up to 25% of the remaining bits */
2133
1.15k
              opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs);
2134
1.15k
              st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4);
2135
1.15k
              st->silk_mode.useCBR = 0;
2136
1.15k
           }
2137
35.1k
        } else {
2138
           /* Constrained VBR. */
2139
29.7k
           if (st->mode == MODE_HYBRID)
2140
3.11k
           {
2141
              /* Compute SILK bitrate corresponding to the max total bits available */
2142
3.11k
              opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
2143
3.11k
                    curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2144
3.11k
                    st->stream_channels);
2145
3.11k
              st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
2146
3.11k
           }
2147
29.7k
        }
2148
2149
64.8k
        if (prefill)
2150
0
        {
2151
0
            opus_int32 zero=0;
2152
0
            int prefill_offset;
2153
            /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
2154
               a discontinuity. The exact location is what we need to avoid leaving any "gap"
2155
               in the audio when mixing with the redundant CELT frame. Here we can afford to
2156
               overwrite st->delay_buffer because the only thing that uses it before it gets
2157
               rewritten is tmp_prefill[] and even then only the part after the ramp really
2158
               gets used (rather than sent to the encoder and discarded) */
2159
0
            prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
2160
0
            gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
2161
0
                  0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
2162
0
            OPUS_CLEAR(st->delay_buffer, prefill_offset);
2163
0
            pcm_silk = st->delay_buffer;
2164
0
            silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity );
2165
            /* Prevent a second switch in the real encode call. */
2166
0
            st->silk_mode.opusCanSwitch = 0;
2167
0
        }
2168
2169
64.8k
        pcm_silk = pcm_buf+total_buffer*st->channels;
2170
64.8k
        ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
2171
64.8k
        if( ret ) {
2172
            /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
2173
            /* Handle error */
2174
0
           RESTORE_STACK;
2175
0
           return OPUS_INTERNAL_ERROR;
2176
0
        }
2177
2178
        /* Extract SILK internal bandwidth for signaling in first byte */
2179
64.8k
        if( st->mode == MODE_SILK_ONLY ) {
2180
60.5k
            if( st->silk_mode.internalSampleRate == 8000 ) {
2181
48.3k
               curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
2182
48.3k
            } else if( st->silk_mode.internalSampleRate == 12000 ) {
2183
5.41k
               curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
2184
6.80k
            } else if( st->silk_mode.internalSampleRate == 16000 ) {
2185
6.80k
               curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
2186
6.80k
            }
2187
60.5k
        } else {
2188
4.27k
            celt_assert( st->silk_mode.internalSampleRate == 16000 );
2189
4.27k
        }
2190
2191
64.8k
        st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
2192
2193
64.8k
        if (nBytes==0)
2194
0
        {
2195
0
           st->rangeFinal = 0;
2196
0
           data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2197
0
           RESTORE_STACK;
2198
0
           return 1;
2199
0
        }
2200
2201
        /* FIXME: How do we allocate the redundancy for CBR? */
2202
64.8k
        if (st->silk_mode.opusCanSwitch)
2203
0
        {
2204
0
           redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
2205
0
           redundancy = (redundancy_bytes != 0);
2206
0
           celt_to_silk = 0;
2207
0
           st->silk_bw_switch = 1;
2208
0
        }
2209
64.8k
    }
2210
2211
    /* CELT processing */
2212
146k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2213
146k
    {
2214
146k
        int endband=21;
2215
2216
146k
        switch(curr_bandwidth)
2217
146k
        {
2218
109k
            case OPUS_BANDWIDTH_NARROWBAND:
2219
109k
                endband = 13;
2220
109k
                break;
2221
5.41k
            case OPUS_BANDWIDTH_MEDIUMBAND:
2222
24.5k
            case OPUS_BANDWIDTH_WIDEBAND:
2223
24.5k
                endband = 17;
2224
24.5k
                break;
2225
9.09k
            case OPUS_BANDWIDTH_SUPERWIDEBAND:
2226
9.09k
                endband = 19;
2227
9.09k
                break;
2228
3.09k
            case OPUS_BANDWIDTH_FULLBAND:
2229
3.09k
                endband = 21;
2230
3.09k
                break;
2231
146k
        }
2232
146k
        celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
2233
146k
        celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
2234
146k
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2235
146k
    }
2236
146k
    if (st->mode != MODE_SILK_ONLY)
2237
85.8k
    {
2238
85.8k
        opus_val32 celt_pred=2;
2239
        /* We may still decide to disable prediction later */
2240
85.8k
        if (st->silk_mode.reducedDependency)
2241
19.6k
           celt_pred = 0;
2242
85.8k
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
2243
85.8k
    }
2244
2245
146k
    ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res);
2246
146k
    if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
2247
0
    {
2248
0
       OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
2249
0
    }
2250
2251
146k
    if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
2252
43.6k
    {
2253
43.6k
       OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
2254
43.6k
       OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
2255
43.6k
             &pcm_buf[0],
2256
43.6k
             (frame_size+total_buffer)*st->channels);
2257
102k
    } else {
2258
102k
       OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
2259
102k
    }
2260
    /* gain_fade() and stereo_fade() need to be after the buffer copying
2261
       because we don't want any of this to affect the SILK part */
2262
146k
    if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
2263
3.28k
       gain_fade(pcm_buf, pcm_buf,
2264
3.28k
             st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
2265
3.28k
    }
2266
146k
    st->prev_HB_gain = HB_gain;
2267
146k
    if (st->mode != MODE_HYBRID || st->stream_channels==1)
2268
144k
    {
2269
144k
       if (equiv_rate > 32000)
2270
53.2k
          st->silk_mode.stereoWidth_Q14 = 16384;
2271
91.4k
       else if (equiv_rate < 16000)
2272
35.9k
          st->silk_mode.stereoWidth_Q14 = 0;
2273
55.5k
       else
2274
55.5k
          st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
2275
144k
    }
2276
146k
    if( !st->energy_masking && st->channels == 2 ) {
2277
        /* Apply stereo width reduction (at low bitrates) */
2278
64.2k
        if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
2279
18.7k
            opus_val16 g1, g2;
2280
18.7k
            g1 = st->hybrid_stereo_width_Q14;
2281
18.7k
            g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
2282
18.7k
#ifdef FIXED_POINT
2283
18.7k
            g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
2284
18.7k
            g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
2285
#else
2286
            g1 *= (1.f/16384);
2287
            g2 *= (1.f/16384);
2288
#endif
2289
18.7k
            stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
2290
18.7k
                  frame_size, st->channels, celt_mode->window, st->Fs);
2291
18.7k
            st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
2292
18.7k
        }
2293
64.2k
    }
2294
2295
146k
    if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
2296
54.7k
    {
2297
        /* For SILK mode, the redundancy is inferred from the length */
2298
54.7k
        if (st->mode == MODE_HYBRID)
2299
4.17k
           ec_enc_bit_logp(&enc, redundancy, 12);
2300
54.7k
        if (redundancy)
2301
0
        {
2302
0
            int max_redundancy;
2303
0
            ec_enc_bit_logp(&enc, celt_to_silk, 1);
2304
0
            if (st->mode == MODE_HYBRID)
2305
0
            {
2306
               /* Reserve the 8 bits needed for the redundancy length,
2307
                  and at least a few bits for CELT if possible */
2308
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
2309
0
            }
2310
0
            else
2311
0
               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2312
            /* Target the same bit-rate for redundancy as for the rest,
2313
               up to a max of 257 bytes */
2314
0
            redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
2315
0
            redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2316
0
            if (st->mode == MODE_HYBRID)
2317
0
                ec_enc_uint(&enc, redundancy_bytes-2, 256);
2318
0
        }
2319
91.6k
    } else {
2320
91.6k
        redundancy = 0;
2321
91.6k
    }
2322
2323
146k
    if (!redundancy)
2324
146k
    {
2325
146k
       st->silk_bw_switch = 0;
2326
146k
       redundancy_bytes = 0;
2327
146k
    }
2328
146k
    if (st->mode != MODE_CELT_ONLY)start_band=17;
2329
2330
146k
    if (st->mode == MODE_SILK_ONLY)
2331
60.5k
    {
2332
60.5k
        ret = (ec_tell(&enc)+7)>>3;
2333
60.5k
        ec_enc_done(&enc);
2334
60.5k
        nb_compr_bytes = ret;
2335
85.8k
    } else {
2336
85.8k
        nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2337
#ifdef ENABLE_QEXT
2338
        if (st->mode == MODE_CELT_ONLY && st->enable_qext) {
2339
           celt_assert(redundancy_bytes==0);
2340
           nb_compr_bytes = orig_max_data_bytes-1;
2341
        }
2342
#endif
2343
#ifdef ENABLE_DRED
2344
        if (st->dred_duration > 0)
2345
        {
2346
            int max_celt_bytes;
2347
            opus_int32 dred_bytes = dred_bitrate_bps/(frame_rate*8);
2348
            /* Allow CELT to steal up to 25% of the remaining bits. */
2349
            max_celt_bytes = nb_compr_bytes - dred_bytes*3/4;
2350
            /* But try to give CELT at least 5 bytes to prevent a mismatch with
2351
               the redundancy signaling. */
2352
            max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes);
2353
            /* Subject to the original max. */
2354
            nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes);
2355
        }
2356
#endif
2357
85.8k
        ec_enc_shrink(&enc, nb_compr_bytes);
2358
85.8k
    }
2359
2360
146k
#ifndef DISABLE_FLOAT_API
2361
146k
    if (redundancy || st->mode != MODE_SILK_ONLY)
2362
85.8k
       celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info));
2363
146k
#endif
2364
146k
    if (st->mode == MODE_HYBRID) {
2365
4.27k
       SILKInfo info;
2366
4.27k
       info.signalType = st->silk_mode.signalType;
2367
4.27k
       info.offset = st->silk_mode.offset;
2368
4.27k
       celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2369
4.27k
    }
2370
2371
    /* 5 ms redundant frame for CELT->SILK */
2372
146k
    if (redundancy && celt_to_silk)
2373
0
    {
2374
0
        int err;
2375
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2376
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2377
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2378
0
        err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2379
0
        if (err < 0)
2380
0
        {
2381
0
           RESTORE_STACK;
2382
0
           return OPUS_INTERNAL_ERROR;
2383
0
        }
2384
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2385
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2386
0
    }
2387
2388
146k
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2389
146k
       celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2390
2391
146k
    data[-1] = 0;
2392
146k
    if (st->mode != MODE_SILK_ONLY)
2393
85.8k
    {
2394
85.8k
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2395
85.8k
        if (st->mode == MODE_HYBRID)
2396
4.27k
        {
2397
4.27k
            if( st->use_vbr ) {
2398
3.11k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2399
3.11k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2400
3.11k
            }
2401
81.5k
        } else {
2402
81.5k
            if (st->use_vbr)
2403
61.9k
            {
2404
61.9k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2405
61.9k
                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
2406
61.9k
                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
2407
61.9k
            }
2408
81.5k
        }
2409
#ifdef ENABLE_DRED
2410
        /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */
2411
        if (!st->use_vbr && st->dred_duration > 0)
2412
        {
2413
            opus_int32 celt_bitrate = st->bitrate_bps;
2414
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2415
            celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2416
            if (st->mode == MODE_HYBRID) {
2417
                celt_bitrate -= st->silk_mode.bitRate;
2418
            }
2419
            celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate));
2420
        }
2421
#endif
2422
85.8k
        if (st->mode != st->prev_mode && st->prev_mode > 0)
2423
0
        {
2424
0
           unsigned char dummy[2];
2425
0
           celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2426
2427
           /* Prefilling */
2428
0
           celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2429
0
           celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2430
0
        }
2431
        /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2432
85.8k
        if (ec_tell(&enc) <= 8*nb_compr_bytes)
2433
85.8k
        {
2434
#ifdef ENABLE_QEXT
2435
           if (st->mode == MODE_CELT_ONLY) celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(st->enable_qext));
2436
#endif
2437
85.8k
           ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2438
#ifdef ENABLE_QEXT
2439
           celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(0));
2440
#endif
2441
85.8k
           if (ret < 0)
2442
0
           {
2443
0
              RESTORE_STACK;
2444
0
              return OPUS_INTERNAL_ERROR;
2445
0
           }
2446
           /* Put CELT->SILK redundancy data in the right place. */
2447
85.8k
           if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && nb_compr_bytes != ret)
2448
0
           {
2449
0
              OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2450
0
              nb_compr_bytes = ret+redundancy_bytes;
2451
0
           }
2452
85.8k
        }
2453
85.8k
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&st->rangeFinal));
2454
85.8k
    } else {
2455
60.5k
       st->rangeFinal = enc.rng;
2456
60.5k
    }
2457
2458
    /* 5 ms redundant frame for SILK->CELT */
2459
146k
    if (redundancy && !celt_to_silk)
2460
0
    {
2461
0
        int err;
2462
0
        unsigned char dummy[2];
2463
0
        int N2, N4;
2464
0
        N2 = st->Fs/200;
2465
0
        N4 = st->Fs/400;
2466
2467
0
        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2468
0
        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2469
0
        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2470
0
        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2471
0
        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2472
2473
0
        if (st->mode == MODE_HYBRID)
2474
0
        {
2475
           /* Shrink packet to what the encoder actually used. */
2476
0
           nb_compr_bytes = ret;
2477
0
           ec_enc_shrink(&enc, nb_compr_bytes);
2478
0
        }
2479
        /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2480
0
        celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2481
2482
0
        err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2483
0
        if (err < 0)
2484
0
        {
2485
0
           RESTORE_STACK;
2486
0
           return OPUS_INTERNAL_ERROR;
2487
0
        }
2488
0
        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2489
0
    }
2490
2491
2492
2493
    /* Signalling the mode in the first byte */
2494
146k
    data--;
2495
146k
    data[0] |= gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2496
2497
146k
    st->rangeFinal ^= redundant_rng;
2498
2499
146k
    if (to_celt)
2500
0
        st->prev_mode = MODE_CELT_ONLY;
2501
146k
    else
2502
146k
        st->prev_mode = st->mode;
2503
146k
    st->prev_channels = st->stream_channels;
2504
146k
    st->prev_framesize = frame_size;
2505
2506
146k
    st->first = 0;
2507
2508
    /* DTX decision */
2509
146k
#ifndef DISABLE_FLOAT_API
2510
146k
    if (st->use_dtx && (analysis_info->valid || is_silence))
2511
7.99k
    {
2512
7.99k
       if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs))
2513
0
       {
2514
0
          st->rangeFinal = 0;
2515
0
          data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2516
0
          RESTORE_STACK;
2517
0
          return 1;
2518
0
       }
2519
138k
    } else {
2520
138k
       st->nb_no_activity_ms_Q1 = 0;
2521
138k
    }
2522
146k
#endif
2523
2524
    /* In the unlikely case that the SILK encoder busted its target, tell
2525
       the decoder to call the PLC */
2526
146k
    if (ec_tell(&enc) > (max_data_bytes-1)*8)
2527
1.62k
    {
2528
1.62k
       if (max_data_bytes < 2)
2529
0
       {
2530
0
          RESTORE_STACK;
2531
0
          return OPUS_BUFFER_TOO_SMALL;
2532
0
       }
2533
1.62k
       data[1] = 0;
2534
1.62k
       ret = 1;
2535
1.62k
       st->rangeFinal = 0;
2536
144k
    } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2537
58.9k
    {
2538
       /*When in LPC only mode it's perfectly
2539
         reasonable to strip off trailing zero bytes as
2540
         the required range decoder behavior is to
2541
         fill these in. This can't be done when the MDCT
2542
         modes are used because the decoder needs to know
2543
         the actual length for allocation purposes.*/
2544
68.8k
       while(ret>2&&data[ret]==0)ret--;
2545
58.9k
    }
2546
    /* Count ToC and redundancy */
2547
146k
    ret += 1+redundancy_bytes;
2548
146k
    apply_padding = !st->use_vbr;
2549
#ifdef ENABLE_DRED
2550
    if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) {
2551
       opus_extension_data extension;
2552
       unsigned char buf[DRED_MAX_DATA_SIZE];
2553
       int dred_chunks;
2554
       int dred_bytes_left;
2555
       dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2);
2556
       if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks);
2557
       /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */
2558
       dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-3);
2559
       /* Account for the extra bytes required to signal large padding length. */
2560
       dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255;
2561
       /* Check whether we actually have something to encode. */
2562
       if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) {
2563
           int dred_bytes;
2564
#ifdef DRED_EXPERIMENTAL_VERSION
2565
           /* Add temporary extension type and version.
2566
              These bytes will be removed once extension is finalized. */
2567
           buf[0] = 'D';
2568
           buf[1] = DRED_EXPERIMENTAL_VERSION;
2569
#endif
2570
           dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES,
2571
                                               st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch);
2572
           if (dred_bytes > 0) {
2573
              dred_bytes += DRED_EXPERIMENTAL_BYTES;
2574
              celt_assert(dred_bytes <= dred_bytes_left);
2575
              extension.id = DRED_EXTENSION_ID;
2576
              extension.frame = 0;
2577
              extension.data = buf;
2578
              extension.len = dred_bytes;
2579
              ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1);
2580
              if (ret < 0)
2581
              {
2582
                 RESTORE_STACK;
2583
                 return OPUS_INTERNAL_ERROR;
2584
              }
2585
              apply_padding = 0;
2586
           }
2587
       }
2588
    }
2589
#else
2590
146k
    (void)first_frame; /* Avoids a warning about first_frame being unused. */
2591
146k
#endif
2592
146k
    if (apply_padding)
2593
54.7k
    {
2594
54.7k
       if (opus_packet_pad(data, ret, orig_max_data_bytes) != OPUS_OK)
2595
0
       {
2596
0
          RESTORE_STACK;
2597
0
          return OPUS_INTERNAL_ERROR;
2598
0
       }
2599
54.7k
       ret = orig_max_data_bytes;
2600
54.7k
    }
2601
146k
    RESTORE_STACK;
2602
146k
    return ret;
2603
146k
}
2604
2605
2606
2607
#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
2608
opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2609
                unsigned char *data, opus_int32 max_data_bytes)
2610
{
2611
   int frame_size;
2612
   frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2613
   return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, 16,
2614
                             pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2615
}
2616
#else
2617
opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2618
                unsigned char *data, opus_int32 max_data_bytes)
2619
0
{
2620
0
   int i, ret;
2621
0
   int frame_size;
2622
0
   VARDECL(opus_res, in);
2623
0
   ALLOC_STACK;
2624
2625
0
   frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2626
0
   if (frame_size <= 0)
2627
0
   {
2628
0
      RESTORE_STACK;
2629
0
      return OPUS_BAD_ARG;
2630
0
   }
2631
0
   ALLOC(in, frame_size*st->channels, opus_res);
2632
2633
0
   for (i=0;i<frame_size*st->channels;i++)
2634
0
      in[i] = INT16TORES(pcm[i]);
2635
0
   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2636
0
                            pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 1);
2637
0
   RESTORE_STACK;
2638
0
   return ret;
2639
0
}
Unexecuted instantiation: opus_encode
Unexecuted instantiation: opus_encode
2640
#endif
2641
2642
#if defined(FIXED_POINT) && defined(ENABLE_RES24)
2643
opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
2644
                unsigned char *data, opus_int32 max_data_bytes)
2645
0
{
2646
0
   int frame_size;
2647
0
   frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2648
0
   return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
2649
0
                             pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 0);
2650
0
}
2651
#else
2652
opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
2653
                unsigned char *data, opus_int32 max_data_bytes)
2654
0
{
2655
0
   int i, ret;
2656
0
   int frame_size;
2657
0
   VARDECL(opus_res, in);
2658
0
   ALLOC_STACK;
2659
2660
0
   frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2661
0
   if (frame_size <= 0)
2662
0
   {
2663
0
      RESTORE_STACK;
2664
0
      return OPUS_BAD_ARG;
2665
0
   }
2666
0
   ALLOC(in, frame_size*st->channels, opus_res);
2667
2668
0
   for (i=0;i<frame_size*st->channels;i++)
2669
0
      in[i] = INT24TORES(pcm[i]);
2670
0
   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
2671
0
                            pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 1);
2672
0
   RESTORE_STACK;
2673
0
   return ret;
2674
0
}
2675
#endif
2676
2677
2678
#ifndef DISABLE_FLOAT_API
2679
2680
# if !defined(FIXED_POINT)
2681
opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2682
                      unsigned char *data, opus_int32 out_data_bytes)
2683
0
{
2684
0
   int frame_size;
2685
0
   frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2686
0
   return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, MAX_ENCODING_DEPTH,
2687
0
                             pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2688
0
}
2689
# else
2690
opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2691
      unsigned char *data, opus_int32 max_data_bytes)
2692
0
{
2693
0
   int i, ret;
2694
0
   int frame_size;
2695
0
   VARDECL(opus_res, in);
2696
0
   ALLOC_STACK;
2697
2698
0
   frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2699
0
   if (frame_size <= 0)
2700
0
   {
2701
0
      RESTORE_STACK;
2702
0
      return OPUS_BAD_ARG;
2703
0
   }
2704
0
   ALLOC(in, frame_size*st->channels, opus_res);
2705
2706
0
   for (i=0;i<frame_size*st->channels;i++)
2707
0
      in[i] = FLOAT2RES(pcm[i]);
2708
0
   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
2709
0
                            pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2710
0
   RESTORE_STACK;
2711
0
   return ret;
2712
0
}
2713
# endif
2714
2715
#endif
2716
2717
2718
int opus_encoder_ctl(OpusEncoder *st, int request, ...)
2719
6.77M
{
2720
6.77M
    int ret;
2721
6.77M
    CELTEncoder *celt_enc=NULL;
2722
6.77M
    va_list ap;
2723
2724
6.77M
    ret = OPUS_OK;
2725
6.77M
    va_start(ap, request);
2726
2727
6.77M
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2728
6.77M
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
2729
2730
6.77M
    switch (request)
2731
6.77M
    {
2732
0
        case OPUS_SET_APPLICATION_REQUEST:
2733
0
        {
2734
0
            opus_int32 value = va_arg(ap, opus_int32);
2735
0
            if (st->application == OPUS_APPLICATION_FORCED_SILK || st->application == OPUS_APPLICATION_FORCED_CELT)
2736
0
            {
2737
0
               ret = OPUS_BAD_ARG;
2738
0
               break;
2739
0
            }
2740
0
            if (   (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
2741
0
                 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2742
0
               || (!st->first && st->application != value))
2743
0
            {
2744
0
               ret = OPUS_BAD_ARG;
2745
0
               break;
2746
0
            }
2747
0
            st->application = value;
2748
0
#ifndef DISABLE_FLOAT_API
2749
0
            st->analysis.application = value;
2750
0
#endif
2751
0
        }
2752
0
        break;
2753
0
        case OPUS_GET_APPLICATION_REQUEST:
2754
0
        {
2755
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2756
0
            if (!value)
2757
0
            {
2758
0
               goto bad_arg;
2759
0
            }
2760
0
            *value = st->application;
2761
0
        }
2762
0
        break;
2763
494k
        case OPUS_SET_BITRATE_REQUEST:
2764
494k
        {
2765
494k
            opus_int32 value = va_arg(ap, opus_int32);
2766
494k
            if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
2767
494k
            {
2768
494k
                if (value <= 0)
2769
0
                    goto bad_arg;
2770
494k
                else if (value <= 500)
2771
396
                    value = 500;
2772
494k
                else if (value > (opus_int32)750000*st->channels)
2773
56
                    value = (opus_int32)750000*st->channels;
2774
494k
            }
2775
494k
            st->user_bitrate_bps = value;
2776
494k
        }
2777
0
        break;
2778
0
        case OPUS_GET_BITRATE_REQUEST:
2779
0
        {
2780
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2781
0
            if (!value)
2782
0
            {
2783
0
               goto bad_arg;
2784
0
            }
2785
0
            *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
2786
0
        }
2787
0
        break;
2788
443k
        case OPUS_SET_FORCE_CHANNELS_REQUEST:
2789
443k
        {
2790
443k
            opus_int32 value = va_arg(ap, opus_int32);
2791
443k
            if((value<1 || value>st->channels) && value != OPUS_AUTO)
2792
7.38k
            {
2793
7.38k
               goto bad_arg;
2794
7.38k
            }
2795
435k
            st->force_channels = value;
2796
435k
        }
2797
0
        break;
2798
0
        case OPUS_GET_FORCE_CHANNELS_REQUEST:
2799
0
        {
2800
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2801
0
            if (!value)
2802
0
            {
2803
0
               goto bad_arg;
2804
0
            }
2805
0
            *value = st->force_channels;
2806
0
        }
2807
0
        break;
2808
500k
        case OPUS_SET_MAX_BANDWIDTH_REQUEST:
2809
500k
        {
2810
500k
            opus_int32 value = va_arg(ap, opus_int32);
2811
500k
            if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
2812
0
            {
2813
0
               goto bad_arg;
2814
0
            }
2815
500k
            st->max_bandwidth = value;
2816
500k
            if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2817
212k
                st->silk_mode.maxInternalSampleRate = 8000;
2818
288k
            } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2819
56.7k
                st->silk_mode.maxInternalSampleRate = 12000;
2820
231k
            } else {
2821
231k
                st->silk_mode.maxInternalSampleRate = 16000;
2822
231k
            }
2823
500k
        }
2824
0
        break;
2825
0
        case OPUS_GET_MAX_BANDWIDTH_REQUEST:
2826
0
        {
2827
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2828
0
            if (!value)
2829
0
            {
2830
0
               goto bad_arg;
2831
0
            }
2832
0
            *value = st->max_bandwidth;
2833
0
        }
2834
0
        break;
2835
8.19k
        case OPUS_SET_BANDWIDTH_REQUEST:
2836
8.19k
        {
2837
8.19k
            opus_int32 value = va_arg(ap, opus_int32);
2838
8.19k
            if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
2839
0
            {
2840
0
               goto bad_arg;
2841
0
            }
2842
8.19k
            st->user_bandwidth = value;
2843
8.19k
            if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2844
8.19k
                st->silk_mode.maxInternalSampleRate = 8000;
2845
8.19k
            } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2846
0
                st->silk_mode.maxInternalSampleRate = 12000;
2847
0
            } else {
2848
0
                st->silk_mode.maxInternalSampleRate = 16000;
2849
0
            }
2850
8.19k
        }
2851
0
        break;
2852
0
        case OPUS_GET_BANDWIDTH_REQUEST:
2853
0
        {
2854
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2855
0
            if (!value)
2856
0
            {
2857
0
               goto bad_arg;
2858
0
            }
2859
0
            *value = st->bandwidth;
2860
0
        }
2861
0
        break;
2862
500k
        case OPUS_SET_DTX_REQUEST:
2863
500k
        {
2864
500k
            opus_int32 value = va_arg(ap, opus_int32);
2865
500k
            if(value<0 || value>1)
2866
0
            {
2867
0
               goto bad_arg;
2868
0
            }
2869
500k
            st->use_dtx = value;
2870
500k
        }
2871
0
        break;
2872
0
        case OPUS_GET_DTX_REQUEST:
2873
0
        {
2874
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2875
0
            if (!value)
2876
0
            {
2877
0
               goto bad_arg;
2878
0
            }
2879
0
            *value = st->use_dtx;
2880
0
        }
2881
0
        break;
2882
500k
        case OPUS_SET_COMPLEXITY_REQUEST:
2883
500k
        {
2884
500k
            opus_int32 value = va_arg(ap, opus_int32);
2885
500k
            if(value<0 || value>10)
2886
0
            {
2887
0
               goto bad_arg;
2888
0
            }
2889
500k
            st->silk_mode.complexity = value;
2890
500k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
2891
500k
               celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
2892
500k
        }
2893
0
        break;
2894
0
        case OPUS_GET_COMPLEXITY_REQUEST:
2895
0
        {
2896
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2897
0
            if (!value)
2898
0
            {
2899
0
               goto bad_arg;
2900
0
            }
2901
0
            *value = st->silk_mode.complexity;
2902
0
        }
2903
0
        break;
2904
500k
        case OPUS_SET_INBAND_FEC_REQUEST:
2905
500k
        {
2906
500k
            opus_int32 value = va_arg(ap, opus_int32);
2907
500k
            if(value<0 || value>2)
2908
0
            {
2909
0
               goto bad_arg;
2910
0
            }
2911
500k
            st->fec_config = value;
2912
500k
            st->silk_mode.useInBandFEC = (value != 0);
2913
500k
        }
2914
0
        break;
2915
0
        case OPUS_GET_INBAND_FEC_REQUEST:
2916
0
        {
2917
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2918
0
            if (!value)
2919
0
            {
2920
0
               goto bad_arg;
2921
0
            }
2922
0
            *value = st->fec_config;
2923
0
        }
2924
0
        break;
2925
500k
        case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2926
500k
        {
2927
500k
            opus_int32 value = va_arg(ap, opus_int32);
2928
500k
            if (value < 0 || value > 100)
2929
0
            {
2930
0
               goto bad_arg;
2931
0
            }
2932
500k
            st->silk_mode.packetLossPercentage = value;
2933
500k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
2934
500k
               celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
2935
500k
        }
2936
0
        break;
2937
0
        case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
2938
0
        {
2939
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2940
0
            if (!value)
2941
0
            {
2942
0
               goto bad_arg;
2943
0
            }
2944
0
            *value = st->silk_mode.packetLossPercentage;
2945
0
        }
2946
0
        break;
2947
500k
        case OPUS_SET_VBR_REQUEST:
2948
500k
        {
2949
500k
            opus_int32 value = va_arg(ap, opus_int32);
2950
500k
            if(value<0 || value>1)
2951
0
            {
2952
0
               goto bad_arg;
2953
0
            }
2954
500k
            st->use_vbr = value;
2955
500k
            st->silk_mode.useCBR = 1-value;
2956
500k
        }
2957
0
        break;
2958
67.7k
        case OPUS_GET_VBR_REQUEST:
2959
67.7k
        {
2960
67.7k
            opus_int32 *value = va_arg(ap, opus_int32*);
2961
67.7k
            if (!value)
2962
0
            {
2963
0
               goto bad_arg;
2964
0
            }
2965
67.7k
            *value = st->use_vbr;
2966
67.7k
        }
2967
0
        break;
2968
0
        case OPUS_SET_VOICE_RATIO_REQUEST:
2969
0
        {
2970
0
            opus_int32 value = va_arg(ap, opus_int32);
2971
0
            if (value<-1 || value>100)
2972
0
            {
2973
0
               goto bad_arg;
2974
0
            }
2975
0
            st->voice_ratio = value;
2976
0
        }
2977
0
        break;
2978
0
        case OPUS_GET_VOICE_RATIO_REQUEST:
2979
0
        {
2980
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2981
0
            if (!value)
2982
0
            {
2983
0
               goto bad_arg;
2984
0
            }
2985
0
            *value = st->voice_ratio;
2986
0
        }
2987
0
        break;
2988
500k
        case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2989
500k
        {
2990
500k
            opus_int32 value = va_arg(ap, opus_int32);
2991
500k
            if(value<0 || value>1)
2992
0
            {
2993
0
               goto bad_arg;
2994
0
            }
2995
500k
            st->vbr_constraint = value;
2996
500k
        }
2997
0
        break;
2998
0
        case OPUS_GET_VBR_CONSTRAINT_REQUEST:
2999
0
        {
3000
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3001
0
            if (!value)
3002
0
            {
3003
0
               goto bad_arg;
3004
0
            }
3005
0
            *value = st->vbr_constraint;
3006
0
        }
3007
0
        break;
3008
500k
        case OPUS_SET_SIGNAL_REQUEST:
3009
500k
        {
3010
500k
            opus_int32 value = va_arg(ap, opus_int32);
3011
500k
            if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
3012
0
            {
3013
0
               goto bad_arg;
3014
0
            }
3015
500k
            st->signal_type = value;
3016
500k
        }
3017
0
        break;
3018
0
        case OPUS_GET_SIGNAL_REQUEST:
3019
0
        {
3020
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3021
0
            if (!value)
3022
0
            {
3023
0
               goto bad_arg;
3024
0
            }
3025
0
            *value = st->signal_type;
3026
0
        }
3027
0
        break;
3028
0
        case OPUS_GET_LOOKAHEAD_REQUEST:
3029
0
        {
3030
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3031
0
            if (!value)
3032
0
            {
3033
0
               goto bad_arg;
3034
0
            }
3035
0
            *value = st->Fs/400;
3036
0
            if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY && st->application != OPUS_APPLICATION_FORCED_CELT)
3037
0
                *value += st->delay_compensation;
3038
0
        }
3039
0
        break;
3040
135k
        case OPUS_GET_SAMPLE_RATE_REQUEST:
3041
135k
        {
3042
135k
            opus_int32 *value = va_arg(ap, opus_int32*);
3043
135k
            if (!value)
3044
0
            {
3045
0
               goto bad_arg;
3046
0
            }
3047
135k
            *value = st->Fs;
3048
135k
        }
3049
0
        break;
3050
0
        case OPUS_GET_FINAL_RANGE_REQUEST:
3051
0
        {
3052
0
            opus_uint32 *value = va_arg(ap, opus_uint32*);
3053
0
            if (!value)
3054
0
            {
3055
0
               goto bad_arg;
3056
0
            }
3057
0
            *value = st->rangeFinal;
3058
0
        }
3059
0
        break;
3060
500k
        case OPUS_SET_LSB_DEPTH_REQUEST:
3061
500k
        {
3062
500k
            opus_int32 value = va_arg(ap, opus_int32);
3063
500k
            if (value<8 || value>24)
3064
0
            {
3065
0
               goto bad_arg;
3066
0
            }
3067
500k
            st->lsb_depth=value;
3068
500k
        }
3069
0
        break;
3070
0
        case OPUS_GET_LSB_DEPTH_REQUEST:
3071
0
        {
3072
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3073
0
            if (!value)
3074
0
            {
3075
0
               goto bad_arg;
3076
0
            }
3077
0
            *value = st->lsb_depth;
3078
0
        }
3079
0
        break;
3080
0
        case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
3081
0
        {
3082
0
            opus_int32 value = va_arg(ap, opus_int32);
3083
0
            if (value != OPUS_FRAMESIZE_ARG    && value != OPUS_FRAMESIZE_2_5_MS &&
3084
0
                value != OPUS_FRAMESIZE_5_MS   && value != OPUS_FRAMESIZE_10_MS  &&
3085
0
                value != OPUS_FRAMESIZE_20_MS  && value != OPUS_FRAMESIZE_40_MS  &&
3086
0
                value != OPUS_FRAMESIZE_60_MS  && value != OPUS_FRAMESIZE_80_MS  &&
3087
0
                value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS)
3088
0
            {
3089
0
               goto bad_arg;
3090
0
            }
3091
0
            st->variable_duration = value;
3092
0
        }
3093
0
        break;
3094
0
        case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
3095
0
        {
3096
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3097
0
            if (!value)
3098
0
            {
3099
0
               goto bad_arg;
3100
0
            }
3101
0
            *value = st->variable_duration;
3102
0
        }
3103
0
        break;
3104
500k
        case OPUS_SET_PREDICTION_DISABLED_REQUEST:
3105
500k
        {
3106
500k
           opus_int32 value = va_arg(ap, opus_int32);
3107
500k
           if (value > 1 || value < 0)
3108
0
              goto bad_arg;
3109
500k
           st->silk_mode.reducedDependency = value;
3110
500k
        }
3111
0
        break;
3112
0
        case OPUS_GET_PREDICTION_DISABLED_REQUEST:
3113
0
        {
3114
0
           opus_int32 *value = va_arg(ap, opus_int32*);
3115
0
           if (!value)
3116
0
              goto bad_arg;
3117
0
           *value = st->silk_mode.reducedDependency;
3118
0
        }
3119
0
        break;
3120
500k
        case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
3121
500k
        {
3122
500k
            opus_int32 value = va_arg(ap, opus_int32);
3123
500k
            if(value<0 || value>1)
3124
0
            {
3125
0
               goto bad_arg;
3126
0
            }
3127
500k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3128
500k
               celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value));
3129
500k
        }
3130
0
        break;
3131
0
        case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3132
0
        {
3133
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3134
0
            if (!value)
3135
0
            {
3136
0
               goto bad_arg;
3137
0
            }
3138
0
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3139
0
               celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value));
3140
0
            else
3141
0
               *value = 0;
3142
0
        }
3143
0
        break;
3144
#ifdef ENABLE_DRED
3145
        case OPUS_SET_DRED_DURATION_REQUEST:
3146
        {
3147
            opus_int32 value = va_arg(ap, opus_int32);
3148
            if(value<0 || value>DRED_MAX_FRAMES)
3149
            {
3150
               goto bad_arg;
3151
            }
3152
            st->dred_duration = value;
3153
            st->silk_mode.useDRED = !!value;
3154
        }
3155
        break;
3156
        case OPUS_GET_DRED_DURATION_REQUEST:
3157
        {
3158
            opus_int32 *value = va_arg(ap, opus_int32*);
3159
            if (!value)
3160
            {
3161
               goto bad_arg;
3162
            }
3163
            *value = st->dred_duration;
3164
        }
3165
        break;
3166
#endif
3167
#ifdef ENABLE_QEXT
3168
0
      case OPUS_SET_QEXT_REQUEST:
3169
0
      {
3170
0
          opus_int32 value = va_arg(ap, opus_int32);
3171
0
          if(value<0 || value>1)
3172
0
          {
3173
0
             goto bad_arg;
3174
0
          }
3175
0
          st->enable_qext = value;
3176
0
      }
3177
0
      break;
3178
0
      case OPUS_GET_QEXT_REQUEST:
3179
0
      {
3180
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3181
0
          if (!value)
3182
0
          {
3183
0
             goto bad_arg;
3184
0
          }
3185
0
          *value = st->enable_qext;
3186
0
      }
3187
0
      break;
3188
0
#endif
3189
0
        case OPUS_RESET_STATE:
3190
0
        {
3191
0
           void *silk_enc;
3192
0
           silk_EncControlStruct dummy;
3193
0
           char *start;
3194
0
           silk_enc = (char*)st+st->silk_enc_offset;
3195
0
#ifndef DISABLE_FLOAT_API
3196
0
           tonality_analysis_reset(&st->analysis);
3197
0
#endif
3198
3199
0
           start = (char*)&st->OPUS_ENCODER_RESET_START;
3200
0
           OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
3201
3202
0
           if (st->application != OPUS_APPLICATION_FORCED_SILK)
3203
0
              celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
3204
0
           if (st->application != OPUS_APPLICATION_FORCED_CELT)
3205
0
              silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy );
3206
#ifdef ENABLE_DRED
3207
           /* Initialize DRED Encoder */
3208
           dred_encoder_reset( &st->dred_encoder );
3209
#endif
3210
0
           st->stream_channels = st->channels;
3211
0
           st->hybrid_stereo_width_Q14 = 1 << 14;
3212
0
           st->prev_HB_gain = Q15ONE;
3213
0
           st->first = 1;
3214
0
           st->mode = MODE_HYBRID;
3215
0
           st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
3216
0
           st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
3217
0
        }
3218
0
        break;
3219
49.0k
        case OPUS_SET_FORCE_MODE_REQUEST:
3220
49.0k
        {
3221
49.0k
            opus_int32 value = va_arg(ap, opus_int32);
3222
49.0k
            if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
3223
0
            {
3224
0
               goto bad_arg;
3225
0
            }
3226
49.0k
            st->user_forced_mode = value;
3227
49.0k
        }
3228
0
        break;
3229
1.02k
        case OPUS_SET_LFE_REQUEST:
3230
1.02k
        {
3231
1.02k
            opus_int32 value = va_arg(ap, opus_int32);
3232
1.02k
            st->lfe = value;
3233
1.02k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3234
1.02k
               ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
3235
1.02k
        }
3236
1.02k
        break;
3237
8.19k
        case OPUS_SET_ENERGY_MASK_REQUEST:
3238
8.19k
        {
3239
8.19k
            celt_glog *value = va_arg(ap, celt_glog*);
3240
8.19k
            st->energy_masking = value;
3241
8.19k
            ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
3242
8.19k
        }
3243
8.19k
        break;
3244
0
        case OPUS_GET_IN_DTX_REQUEST:
3245
0
        {
3246
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3247
0
            if (!value)
3248
0
            {
3249
0
                goto bad_arg;
3250
0
            }
3251
0
            if (st->silk_mode.useDTX && (st->prev_mode == MODE_SILK_ONLY || st->prev_mode == MODE_HYBRID)) {
3252
                /* DTX determined by Silk. */
3253
0
                silk_encoder *silk_enc = (silk_encoder*)(void *)((char*)st+st->silk_enc_offset);
3254
0
                *value = silk_enc->state_Fxx[0].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
3255
                /* Stereo: check second channel unless only the middle channel was encoded. */
3256
0
                if(*value == 1 && st->silk_mode.nChannelsInternal == 2 && silk_enc->prev_decode_only_middle == 0) {
3257
0
                    *value = silk_enc->state_Fxx[1].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
3258
0
                }
3259
0
            }
3260
0
#ifndef DISABLE_FLOAT_API
3261
0
            else if (st->use_dtx) {
3262
                /* DTX determined by Opus. */
3263
0
                *value = st->nb_no_activity_ms_Q1 >= NB_SPEECH_FRAMES_BEFORE_DTX*20*2;
3264
0
            }
3265
0
#endif
3266
0
            else {
3267
0
                *value = 0;
3268
0
            }
3269
0
        }
3270
0
        break;
3271
#ifdef USE_WEIGHTS_FILE
3272
        case OPUS_SET_DNN_BLOB_REQUEST:
3273
        {
3274
            const unsigned char *data = va_arg(ap, const unsigned char *);
3275
            opus_int32 len = va_arg(ap, opus_int32);
3276
            if(len<0 || data == NULL)
3277
            {
3278
               goto bad_arg;
3279
            }
3280
#ifdef ENABLE_DRED
3281
            ret = dred_encoder_load_model(&st->dred_encoder, data, len);
3282
#endif
3283
        }
3284
        break;
3285
#endif
3286
67.7k
        case CELT_GET_MODE_REQUEST:
3287
67.7k
        {
3288
67.7k
           const CELTMode ** value = va_arg(ap, const CELTMode**);
3289
67.7k
           if (!value)
3290
0
           {
3291
0
              goto bad_arg;
3292
0
           }
3293
67.7k
           ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
3294
67.7k
        }
3295
0
        break;
3296
0
        default:
3297
            /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
3298
0
            ret = OPUS_UNIMPLEMENTED;
3299
0
            break;
3300
6.77M
    }
3301
6.77M
    va_end(ap);
3302
6.77M
    return ret;
3303
7.38k
bad_arg:
3304
7.38k
    va_end(ap);
3305
7.38k
    return OPUS_BAD_ARG;
3306
6.77M
}
opus_encoder_ctl
Line
Count
Source
2719
3.50M
{
2720
3.50M
    int ret;
2721
3.50M
    CELTEncoder *celt_enc=NULL;
2722
3.50M
    va_list ap;
2723
2724
3.50M
    ret = OPUS_OK;
2725
3.50M
    va_start(ap, request);
2726
2727
3.50M
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2728
3.50M
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
2729
2730
3.50M
    switch (request)
2731
3.50M
    {
2732
0
        case OPUS_SET_APPLICATION_REQUEST:
2733
0
        {
2734
0
            opus_int32 value = va_arg(ap, opus_int32);
2735
0
            if (st->application == OPUS_APPLICATION_FORCED_SILK || st->application == OPUS_APPLICATION_FORCED_CELT)
2736
0
            {
2737
0
               ret = OPUS_BAD_ARG;
2738
0
               break;
2739
0
            }
2740
0
            if (   (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
2741
0
                 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2742
0
               || (!st->first && st->application != value))
2743
0
            {
2744
0
               ret = OPUS_BAD_ARG;
2745
0
               break;
2746
0
            }
2747
0
            st->application = value;
2748
0
#ifndef DISABLE_FLOAT_API
2749
0
            st->analysis.application = value;
2750
0
#endif
2751
0
        }
2752
0
        break;
2753
0
        case OPUS_GET_APPLICATION_REQUEST:
2754
0
        {
2755
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2756
0
            if (!value)
2757
0
            {
2758
0
               goto bad_arg;
2759
0
            }
2760
0
            *value = st->application;
2761
0
        }
2762
0
        break;
2763
256k
        case OPUS_SET_BITRATE_REQUEST:
2764
256k
        {
2765
256k
            opus_int32 value = va_arg(ap, opus_int32);
2766
256k
            if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
2767
256k
            {
2768
256k
                if (value <= 0)
2769
0
                    goto bad_arg;
2770
256k
                else if (value <= 500)
2771
210
                    value = 500;
2772
256k
                else if (value > (opus_int32)750000*st->channels)
2773
29
                    value = (opus_int32)750000*st->channels;
2774
256k
            }
2775
256k
            st->user_bitrate_bps = value;
2776
256k
        }
2777
0
        break;
2778
0
        case OPUS_GET_BITRATE_REQUEST:
2779
0
        {
2780
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2781
0
            if (!value)
2782
0
            {
2783
0
               goto bad_arg;
2784
0
            }
2785
0
            *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
2786
0
        }
2787
0
        break;
2788
225k
        case OPUS_SET_FORCE_CHANNELS_REQUEST:
2789
225k
        {
2790
225k
            opus_int32 value = va_arg(ap, opus_int32);
2791
225k
            if((value<1 || value>st->channels) && value != OPUS_AUTO)
2792
3.86k
            {
2793
3.86k
               goto bad_arg;
2794
3.86k
            }
2795
221k
            st->force_channels = value;
2796
221k
        }
2797
0
        break;
2798
0
        case OPUS_GET_FORCE_CHANNELS_REQUEST:
2799
0
        {
2800
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2801
0
            if (!value)
2802
0
            {
2803
0
               goto bad_arg;
2804
0
            }
2805
0
            *value = st->force_channels;
2806
0
        }
2807
0
        break;
2808
258k
        case OPUS_SET_MAX_BANDWIDTH_REQUEST:
2809
258k
        {
2810
258k
            opus_int32 value = va_arg(ap, opus_int32);
2811
258k
            if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
2812
0
            {
2813
0
               goto bad_arg;
2814
0
            }
2815
258k
            st->max_bandwidth = value;
2816
258k
            if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2817
98.7k
                st->silk_mode.maxInternalSampleRate = 8000;
2818
159k
            } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2819
32.9k
                st->silk_mode.maxInternalSampleRate = 12000;
2820
126k
            } else {
2821
126k
                st->silk_mode.maxInternalSampleRate = 16000;
2822
126k
            }
2823
258k
        }
2824
0
        break;
2825
0
        case OPUS_GET_MAX_BANDWIDTH_REQUEST:
2826
0
        {
2827
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2828
0
            if (!value)
2829
0
            {
2830
0
               goto bad_arg;
2831
0
            }
2832
0
            *value = st->max_bandwidth;
2833
0
        }
2834
0
        break;
2835
4.56k
        case OPUS_SET_BANDWIDTH_REQUEST:
2836
4.56k
        {
2837
4.56k
            opus_int32 value = va_arg(ap, opus_int32);
2838
4.56k
            if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
2839
0
            {
2840
0
               goto bad_arg;
2841
0
            }
2842
4.56k
            st->user_bandwidth = value;
2843
4.56k
            if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2844
4.56k
                st->silk_mode.maxInternalSampleRate = 8000;
2845
4.56k
            } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2846
0
                st->silk_mode.maxInternalSampleRate = 12000;
2847
0
            } else {
2848
0
                st->silk_mode.maxInternalSampleRate = 16000;
2849
0
            }
2850
4.56k
        }
2851
0
        break;
2852
0
        case OPUS_GET_BANDWIDTH_REQUEST:
2853
0
        {
2854
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2855
0
            if (!value)
2856
0
            {
2857
0
               goto bad_arg;
2858
0
            }
2859
0
            *value = st->bandwidth;
2860
0
        }
2861
0
        break;
2862
258k
        case OPUS_SET_DTX_REQUEST:
2863
258k
        {
2864
258k
            opus_int32 value = va_arg(ap, opus_int32);
2865
258k
            if(value<0 || value>1)
2866
0
            {
2867
0
               goto bad_arg;
2868
0
            }
2869
258k
            st->use_dtx = value;
2870
258k
        }
2871
0
        break;
2872
0
        case OPUS_GET_DTX_REQUEST:
2873
0
        {
2874
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2875
0
            if (!value)
2876
0
            {
2877
0
               goto bad_arg;
2878
0
            }
2879
0
            *value = st->use_dtx;
2880
0
        }
2881
0
        break;
2882
258k
        case OPUS_SET_COMPLEXITY_REQUEST:
2883
258k
        {
2884
258k
            opus_int32 value = va_arg(ap, opus_int32);
2885
258k
            if(value<0 || value>10)
2886
0
            {
2887
0
               goto bad_arg;
2888
0
            }
2889
258k
            st->silk_mode.complexity = value;
2890
258k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
2891
258k
               celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
2892
258k
        }
2893
0
        break;
2894
0
        case OPUS_GET_COMPLEXITY_REQUEST:
2895
0
        {
2896
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2897
0
            if (!value)
2898
0
            {
2899
0
               goto bad_arg;
2900
0
            }
2901
0
            *value = st->silk_mode.complexity;
2902
0
        }
2903
0
        break;
2904
258k
        case OPUS_SET_INBAND_FEC_REQUEST:
2905
258k
        {
2906
258k
            opus_int32 value = va_arg(ap, opus_int32);
2907
258k
            if(value<0 || value>2)
2908
0
            {
2909
0
               goto bad_arg;
2910
0
            }
2911
258k
            st->fec_config = value;
2912
258k
            st->silk_mode.useInBandFEC = (value != 0);
2913
258k
        }
2914
0
        break;
2915
0
        case OPUS_GET_INBAND_FEC_REQUEST:
2916
0
        {
2917
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2918
0
            if (!value)
2919
0
            {
2920
0
               goto bad_arg;
2921
0
            }
2922
0
            *value = st->fec_config;
2923
0
        }
2924
0
        break;
2925
258k
        case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2926
258k
        {
2927
258k
            opus_int32 value = va_arg(ap, opus_int32);
2928
258k
            if (value < 0 || value > 100)
2929
0
            {
2930
0
               goto bad_arg;
2931
0
            }
2932
258k
            st->silk_mode.packetLossPercentage = value;
2933
258k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
2934
258k
               celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
2935
258k
        }
2936
0
        break;
2937
0
        case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
2938
0
        {
2939
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2940
0
            if (!value)
2941
0
            {
2942
0
               goto bad_arg;
2943
0
            }
2944
0
            *value = st->silk_mode.packetLossPercentage;
2945
0
        }
2946
0
        break;
2947
258k
        case OPUS_SET_VBR_REQUEST:
2948
258k
        {
2949
258k
            opus_int32 value = va_arg(ap, opus_int32);
2950
258k
            if(value<0 || value>1)
2951
0
            {
2952
0
               goto bad_arg;
2953
0
            }
2954
258k
            st->use_vbr = value;
2955
258k
            st->silk_mode.useCBR = 1-value;
2956
258k
        }
2957
0
        break;
2958
35.2k
        case OPUS_GET_VBR_REQUEST:
2959
35.2k
        {
2960
35.2k
            opus_int32 *value = va_arg(ap, opus_int32*);
2961
35.2k
            if (!value)
2962
0
            {
2963
0
               goto bad_arg;
2964
0
            }
2965
35.2k
            *value = st->use_vbr;
2966
35.2k
        }
2967
0
        break;
2968
0
        case OPUS_SET_VOICE_RATIO_REQUEST:
2969
0
        {
2970
0
            opus_int32 value = va_arg(ap, opus_int32);
2971
0
            if (value<-1 || value>100)
2972
0
            {
2973
0
               goto bad_arg;
2974
0
            }
2975
0
            st->voice_ratio = value;
2976
0
        }
2977
0
        break;
2978
0
        case OPUS_GET_VOICE_RATIO_REQUEST:
2979
0
        {
2980
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2981
0
            if (!value)
2982
0
            {
2983
0
               goto bad_arg;
2984
0
            }
2985
0
            *value = st->voice_ratio;
2986
0
        }
2987
0
        break;
2988
258k
        case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2989
258k
        {
2990
258k
            opus_int32 value = va_arg(ap, opus_int32);
2991
258k
            if(value<0 || value>1)
2992
0
            {
2993
0
               goto bad_arg;
2994
0
            }
2995
258k
            st->vbr_constraint = value;
2996
258k
        }
2997
0
        break;
2998
0
        case OPUS_GET_VBR_CONSTRAINT_REQUEST:
2999
0
        {
3000
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3001
0
            if (!value)
3002
0
            {
3003
0
               goto bad_arg;
3004
0
            }
3005
0
            *value = st->vbr_constraint;
3006
0
        }
3007
0
        break;
3008
258k
        case OPUS_SET_SIGNAL_REQUEST:
3009
258k
        {
3010
258k
            opus_int32 value = va_arg(ap, opus_int32);
3011
258k
            if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
3012
0
            {
3013
0
               goto bad_arg;
3014
0
            }
3015
258k
            st->signal_type = value;
3016
258k
        }
3017
0
        break;
3018
0
        case OPUS_GET_SIGNAL_REQUEST:
3019
0
        {
3020
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3021
0
            if (!value)
3022
0
            {
3023
0
               goto bad_arg;
3024
0
            }
3025
0
            *value = st->signal_type;
3026
0
        }
3027
0
        break;
3028
0
        case OPUS_GET_LOOKAHEAD_REQUEST:
3029
0
        {
3030
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3031
0
            if (!value)
3032
0
            {
3033
0
               goto bad_arg;
3034
0
            }
3035
0
            *value = st->Fs/400;
3036
0
            if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY && st->application != OPUS_APPLICATION_FORCED_CELT)
3037
0
                *value += st->delay_compensation;
3038
0
        }
3039
0
        break;
3040
70.5k
        case OPUS_GET_SAMPLE_RATE_REQUEST:
3041
70.5k
        {
3042
70.5k
            opus_int32 *value = va_arg(ap, opus_int32*);
3043
70.5k
            if (!value)
3044
0
            {
3045
0
               goto bad_arg;
3046
0
            }
3047
70.5k
            *value = st->Fs;
3048
70.5k
        }
3049
0
        break;
3050
0
        case OPUS_GET_FINAL_RANGE_REQUEST:
3051
0
        {
3052
0
            opus_uint32 *value = va_arg(ap, opus_uint32*);
3053
0
            if (!value)
3054
0
            {
3055
0
               goto bad_arg;
3056
0
            }
3057
0
            *value = st->rangeFinal;
3058
0
        }
3059
0
        break;
3060
258k
        case OPUS_SET_LSB_DEPTH_REQUEST:
3061
258k
        {
3062
258k
            opus_int32 value = va_arg(ap, opus_int32);
3063
258k
            if (value<8 || value>24)
3064
0
            {
3065
0
               goto bad_arg;
3066
0
            }
3067
258k
            st->lsb_depth=value;
3068
258k
        }
3069
0
        break;
3070
0
        case OPUS_GET_LSB_DEPTH_REQUEST:
3071
0
        {
3072
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3073
0
            if (!value)
3074
0
            {
3075
0
               goto bad_arg;
3076
0
            }
3077
0
            *value = st->lsb_depth;
3078
0
        }
3079
0
        break;
3080
0
        case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
3081
0
        {
3082
0
            opus_int32 value = va_arg(ap, opus_int32);
3083
0
            if (value != OPUS_FRAMESIZE_ARG    && value != OPUS_FRAMESIZE_2_5_MS &&
3084
0
                value != OPUS_FRAMESIZE_5_MS   && value != OPUS_FRAMESIZE_10_MS  &&
3085
0
                value != OPUS_FRAMESIZE_20_MS  && value != OPUS_FRAMESIZE_40_MS  &&
3086
0
                value != OPUS_FRAMESIZE_60_MS  && value != OPUS_FRAMESIZE_80_MS  &&
3087
0
                value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS)
3088
0
            {
3089
0
               goto bad_arg;
3090
0
            }
3091
0
            st->variable_duration = value;
3092
0
        }
3093
0
        break;
3094
0
        case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
3095
0
        {
3096
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3097
0
            if (!value)
3098
0
            {
3099
0
               goto bad_arg;
3100
0
            }
3101
0
            *value = st->variable_duration;
3102
0
        }
3103
0
        break;
3104
258k
        case OPUS_SET_PREDICTION_DISABLED_REQUEST:
3105
258k
        {
3106
258k
           opus_int32 value = va_arg(ap, opus_int32);
3107
258k
           if (value > 1 || value < 0)
3108
0
              goto bad_arg;
3109
258k
           st->silk_mode.reducedDependency = value;
3110
258k
        }
3111
0
        break;
3112
0
        case OPUS_GET_PREDICTION_DISABLED_REQUEST:
3113
0
        {
3114
0
           opus_int32 *value = va_arg(ap, opus_int32*);
3115
0
           if (!value)
3116
0
              goto bad_arg;
3117
0
           *value = st->silk_mode.reducedDependency;
3118
0
        }
3119
0
        break;
3120
258k
        case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
3121
258k
        {
3122
258k
            opus_int32 value = va_arg(ap, opus_int32);
3123
258k
            if(value<0 || value>1)
3124
0
            {
3125
0
               goto bad_arg;
3126
0
            }
3127
258k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3128
258k
               celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value));
3129
258k
        }
3130
0
        break;
3131
0
        case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3132
0
        {
3133
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3134
0
            if (!value)
3135
0
            {
3136
0
               goto bad_arg;
3137
0
            }
3138
0
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3139
0
               celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value));
3140
0
            else
3141
0
               *value = 0;
3142
0
        }
3143
0
        break;
3144
#ifdef ENABLE_DRED
3145
        case OPUS_SET_DRED_DURATION_REQUEST:
3146
        {
3147
            opus_int32 value = va_arg(ap, opus_int32);
3148
            if(value<0 || value>DRED_MAX_FRAMES)
3149
            {
3150
               goto bad_arg;
3151
            }
3152
            st->dred_duration = value;
3153
            st->silk_mode.useDRED = !!value;
3154
        }
3155
        break;
3156
        case OPUS_GET_DRED_DURATION_REQUEST:
3157
        {
3158
            opus_int32 *value = va_arg(ap, opus_int32*);
3159
            if (!value)
3160
            {
3161
               goto bad_arg;
3162
            }
3163
            *value = st->dred_duration;
3164
        }
3165
        break;
3166
#endif
3167
0
#ifdef ENABLE_QEXT
3168
0
      case OPUS_SET_QEXT_REQUEST:
3169
0
      {
3170
0
          opus_int32 value = va_arg(ap, opus_int32);
3171
0
          if(value<0 || value>1)
3172
0
          {
3173
0
             goto bad_arg;
3174
0
          }
3175
0
          st->enable_qext = value;
3176
0
      }
3177
0
      break;
3178
0
      case OPUS_GET_QEXT_REQUEST:
3179
0
      {
3180
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3181
0
          if (!value)
3182
0
          {
3183
0
             goto bad_arg;
3184
0
          }
3185
0
          *value = st->enable_qext;
3186
0
      }
3187
0
      break;
3188
0
#endif
3189
0
        case OPUS_RESET_STATE:
3190
0
        {
3191
0
           void *silk_enc;
3192
0
           silk_EncControlStruct dummy;
3193
0
           char *start;
3194
0
           silk_enc = (char*)st+st->silk_enc_offset;
3195
0
#ifndef DISABLE_FLOAT_API
3196
0
           tonality_analysis_reset(&st->analysis);
3197
0
#endif
3198
3199
0
           start = (char*)&st->OPUS_ENCODER_RESET_START;
3200
0
           OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
3201
3202
0
           if (st->application != OPUS_APPLICATION_FORCED_SILK)
3203
0
              celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
3204
0
           if (st->application != OPUS_APPLICATION_FORCED_CELT)
3205
0
              silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy );
3206
#ifdef ENABLE_DRED
3207
           /* Initialize DRED Encoder */
3208
           dred_encoder_reset( &st->dred_encoder );
3209
#endif
3210
0
           st->stream_channels = st->channels;
3211
0
           st->hybrid_stereo_width_Q14 = 1 << 14;
3212
0
           st->prev_HB_gain = Q15ONE;
3213
0
           st->first = 1;
3214
0
           st->mode = MODE_HYBRID;
3215
0
           st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
3216
0
           st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
3217
0
        }
3218
0
        break;
3219
25.1k
        case OPUS_SET_FORCE_MODE_REQUEST:
3220
25.1k
        {
3221
25.1k
            opus_int32 value = va_arg(ap, opus_int32);
3222
25.1k
            if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
3223
0
            {
3224
0
               goto bad_arg;
3225
0
            }
3226
25.1k
            st->user_forced_mode = value;
3227
25.1k
        }
3228
0
        break;
3229
574
        case OPUS_SET_LFE_REQUEST:
3230
574
        {
3231
574
            opus_int32 value = va_arg(ap, opus_int32);
3232
574
            st->lfe = value;
3233
574
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3234
574
               ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
3235
574
        }
3236
574
        break;
3237
4.56k
        case OPUS_SET_ENERGY_MASK_REQUEST:
3238
4.56k
        {
3239
4.56k
            celt_glog *value = va_arg(ap, celt_glog*);
3240
4.56k
            st->energy_masking = value;
3241
4.56k
            ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
3242
4.56k
        }
3243
4.56k
        break;
3244
0
        case OPUS_GET_IN_DTX_REQUEST:
3245
0
        {
3246
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3247
0
            if (!value)
3248
0
            {
3249
0
                goto bad_arg;
3250
0
            }
3251
0
            if (st->silk_mode.useDTX && (st->prev_mode == MODE_SILK_ONLY || st->prev_mode == MODE_HYBRID)) {
3252
                /* DTX determined by Silk. */
3253
0
                silk_encoder *silk_enc = (silk_encoder*)(void *)((char*)st+st->silk_enc_offset);
3254
0
                *value = silk_enc->state_Fxx[0].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
3255
                /* Stereo: check second channel unless only the middle channel was encoded. */
3256
0
                if(*value == 1 && st->silk_mode.nChannelsInternal == 2 && silk_enc->prev_decode_only_middle == 0) {
3257
0
                    *value = silk_enc->state_Fxx[1].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
3258
0
                }
3259
0
            }
3260
0
#ifndef DISABLE_FLOAT_API
3261
0
            else if (st->use_dtx) {
3262
                /* DTX determined by Opus. */
3263
0
                *value = st->nb_no_activity_ms_Q1 >= NB_SPEECH_FRAMES_BEFORE_DTX*20*2;
3264
0
            }
3265
0
#endif
3266
0
            else {
3267
0
                *value = 0;
3268
0
            }
3269
0
        }
3270
0
        break;
3271
#ifdef USE_WEIGHTS_FILE
3272
        case OPUS_SET_DNN_BLOB_REQUEST:
3273
        {
3274
            const unsigned char *data = va_arg(ap, const unsigned char *);
3275
            opus_int32 len = va_arg(ap, opus_int32);
3276
            if(len<0 || data == NULL)
3277
            {
3278
               goto bad_arg;
3279
            }
3280
#ifdef ENABLE_DRED
3281
            ret = dred_encoder_load_model(&st->dred_encoder, data, len);
3282
#endif
3283
        }
3284
        break;
3285
#endif
3286
35.2k
        case CELT_GET_MODE_REQUEST:
3287
35.2k
        {
3288
35.2k
           const CELTMode ** value = va_arg(ap, const CELTMode**);
3289
35.2k
           if (!value)
3290
0
           {
3291
0
              goto bad_arg;
3292
0
           }
3293
35.2k
           ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
3294
35.2k
        }
3295
0
        break;
3296
0
        default:
3297
            /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
3298
0
            ret = OPUS_UNIMPLEMENTED;
3299
0
            break;
3300
3.50M
    }
3301
3.50M
    va_end(ap);
3302
3.49M
    return ret;
3303
3.86k
bad_arg:
3304
3.86k
    va_end(ap);
3305
3.86k
    return OPUS_BAD_ARG;
3306
3.50M
}
opus_encoder_ctl
Line
Count
Source
2719
3.27M
{
2720
3.27M
    int ret;
2721
3.27M
    CELTEncoder *celt_enc=NULL;
2722
3.27M
    va_list ap;
2723
2724
3.27M
    ret = OPUS_OK;
2725
3.27M
    va_start(ap, request);
2726
2727
3.27M
    if (st->application != OPUS_APPLICATION_FORCED_SILK)
2728
3.27M
       celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
2729
2730
3.27M
    switch (request)
2731
3.27M
    {
2732
0
        case OPUS_SET_APPLICATION_REQUEST:
2733
0
        {
2734
0
            opus_int32 value = va_arg(ap, opus_int32);
2735
0
            if (st->application == OPUS_APPLICATION_FORCED_SILK || st->application == OPUS_APPLICATION_FORCED_CELT)
2736
0
            {
2737
0
               ret = OPUS_BAD_ARG;
2738
0
               break;
2739
0
            }
2740
0
            if (   (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
2741
0
                 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2742
0
               || (!st->first && st->application != value))
2743
0
            {
2744
0
               ret = OPUS_BAD_ARG;
2745
0
               break;
2746
0
            }
2747
0
            st->application = value;
2748
0
#ifndef DISABLE_FLOAT_API
2749
0
            st->analysis.application = value;
2750
0
#endif
2751
0
        }
2752
0
        break;
2753
0
        case OPUS_GET_APPLICATION_REQUEST:
2754
0
        {
2755
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2756
0
            if (!value)
2757
0
            {
2758
0
               goto bad_arg;
2759
0
            }
2760
0
            *value = st->application;
2761
0
        }
2762
0
        break;
2763
238k
        case OPUS_SET_BITRATE_REQUEST:
2764
238k
        {
2765
238k
            opus_int32 value = va_arg(ap, opus_int32);
2766
238k
            if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
2767
238k
            {
2768
238k
                if (value <= 0)
2769
0
                    goto bad_arg;
2770
238k
                else if (value <= 500)
2771
186
                    value = 500;
2772
238k
                else if (value > (opus_int32)750000*st->channels)
2773
27
                    value = (opus_int32)750000*st->channels;
2774
238k
            }
2775
238k
            st->user_bitrate_bps = value;
2776
238k
        }
2777
0
        break;
2778
0
        case OPUS_GET_BITRATE_REQUEST:
2779
0
        {
2780
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2781
0
            if (!value)
2782
0
            {
2783
0
               goto bad_arg;
2784
0
            }
2785
0
            *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
2786
0
        }
2787
0
        break;
2788
218k
        case OPUS_SET_FORCE_CHANNELS_REQUEST:
2789
218k
        {
2790
218k
            opus_int32 value = va_arg(ap, opus_int32);
2791
218k
            if((value<1 || value>st->channels) && value != OPUS_AUTO)
2792
3.51k
            {
2793
3.51k
               goto bad_arg;
2794
3.51k
            }
2795
214k
            st->force_channels = value;
2796
214k
        }
2797
0
        break;
2798
0
        case OPUS_GET_FORCE_CHANNELS_REQUEST:
2799
0
        {
2800
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2801
0
            if (!value)
2802
0
            {
2803
0
               goto bad_arg;
2804
0
            }
2805
0
            *value = st->force_channels;
2806
0
        }
2807
0
        break;
2808
241k
        case OPUS_SET_MAX_BANDWIDTH_REQUEST:
2809
241k
        {
2810
241k
            opus_int32 value = va_arg(ap, opus_int32);
2811
241k
            if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
2812
0
            {
2813
0
               goto bad_arg;
2814
0
            }
2815
241k
            st->max_bandwidth = value;
2816
241k
            if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2817
113k
                st->silk_mode.maxInternalSampleRate = 8000;
2818
128k
            } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2819
23.7k
                st->silk_mode.maxInternalSampleRate = 12000;
2820
104k
            } else {
2821
104k
                st->silk_mode.maxInternalSampleRate = 16000;
2822
104k
            }
2823
241k
        }
2824
0
        break;
2825
0
        case OPUS_GET_MAX_BANDWIDTH_REQUEST:
2826
0
        {
2827
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2828
0
            if (!value)
2829
0
            {
2830
0
               goto bad_arg;
2831
0
            }
2832
0
            *value = st->max_bandwidth;
2833
0
        }
2834
0
        break;
2835
3.63k
        case OPUS_SET_BANDWIDTH_REQUEST:
2836
3.63k
        {
2837
3.63k
            opus_int32 value = va_arg(ap, opus_int32);
2838
3.63k
            if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
2839
0
            {
2840
0
               goto bad_arg;
2841
0
            }
2842
3.63k
            st->user_bandwidth = value;
2843
3.63k
            if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2844
3.63k
                st->silk_mode.maxInternalSampleRate = 8000;
2845
3.63k
            } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2846
0
                st->silk_mode.maxInternalSampleRate = 12000;
2847
0
            } else {
2848
0
                st->silk_mode.maxInternalSampleRate = 16000;
2849
0
            }
2850
3.63k
        }
2851
0
        break;
2852
0
        case OPUS_GET_BANDWIDTH_REQUEST:
2853
0
        {
2854
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2855
0
            if (!value)
2856
0
            {
2857
0
               goto bad_arg;
2858
0
            }
2859
0
            *value = st->bandwidth;
2860
0
        }
2861
0
        break;
2862
241k
        case OPUS_SET_DTX_REQUEST:
2863
241k
        {
2864
241k
            opus_int32 value = va_arg(ap, opus_int32);
2865
241k
            if(value<0 || value>1)
2866
0
            {
2867
0
               goto bad_arg;
2868
0
            }
2869
241k
            st->use_dtx = value;
2870
241k
        }
2871
0
        break;
2872
0
        case OPUS_GET_DTX_REQUEST:
2873
0
        {
2874
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2875
0
            if (!value)
2876
0
            {
2877
0
               goto bad_arg;
2878
0
            }
2879
0
            *value = st->use_dtx;
2880
0
        }
2881
0
        break;
2882
241k
        case OPUS_SET_COMPLEXITY_REQUEST:
2883
241k
        {
2884
241k
            opus_int32 value = va_arg(ap, opus_int32);
2885
241k
            if(value<0 || value>10)
2886
0
            {
2887
0
               goto bad_arg;
2888
0
            }
2889
241k
            st->silk_mode.complexity = value;
2890
241k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
2891
241k
               celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
2892
241k
        }
2893
0
        break;
2894
0
        case OPUS_GET_COMPLEXITY_REQUEST:
2895
0
        {
2896
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2897
0
            if (!value)
2898
0
            {
2899
0
               goto bad_arg;
2900
0
            }
2901
0
            *value = st->silk_mode.complexity;
2902
0
        }
2903
0
        break;
2904
241k
        case OPUS_SET_INBAND_FEC_REQUEST:
2905
241k
        {
2906
241k
            opus_int32 value = va_arg(ap, opus_int32);
2907
241k
            if(value<0 || value>2)
2908
0
            {
2909
0
               goto bad_arg;
2910
0
            }
2911
241k
            st->fec_config = value;
2912
241k
            st->silk_mode.useInBandFEC = (value != 0);
2913
241k
        }
2914
0
        break;
2915
0
        case OPUS_GET_INBAND_FEC_REQUEST:
2916
0
        {
2917
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2918
0
            if (!value)
2919
0
            {
2920
0
               goto bad_arg;
2921
0
            }
2922
0
            *value = st->fec_config;
2923
0
        }
2924
0
        break;
2925
241k
        case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2926
241k
        {
2927
241k
            opus_int32 value = va_arg(ap, opus_int32);
2928
241k
            if (value < 0 || value > 100)
2929
0
            {
2930
0
               goto bad_arg;
2931
0
            }
2932
241k
            st->silk_mode.packetLossPercentage = value;
2933
241k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
2934
241k
               celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
2935
241k
        }
2936
0
        break;
2937
0
        case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
2938
0
        {
2939
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2940
0
            if (!value)
2941
0
            {
2942
0
               goto bad_arg;
2943
0
            }
2944
0
            *value = st->silk_mode.packetLossPercentage;
2945
0
        }
2946
0
        break;
2947
241k
        case OPUS_SET_VBR_REQUEST:
2948
241k
        {
2949
241k
            opus_int32 value = va_arg(ap, opus_int32);
2950
241k
            if(value<0 || value>1)
2951
0
            {
2952
0
               goto bad_arg;
2953
0
            }
2954
241k
            st->use_vbr = value;
2955
241k
            st->silk_mode.useCBR = 1-value;
2956
241k
        }
2957
0
        break;
2958
32.4k
        case OPUS_GET_VBR_REQUEST:
2959
32.4k
        {
2960
32.4k
            opus_int32 *value = va_arg(ap, opus_int32*);
2961
32.4k
            if (!value)
2962
0
            {
2963
0
               goto bad_arg;
2964
0
            }
2965
32.4k
            *value = st->use_vbr;
2966
32.4k
        }
2967
0
        break;
2968
0
        case OPUS_SET_VOICE_RATIO_REQUEST:
2969
0
        {
2970
0
            opus_int32 value = va_arg(ap, opus_int32);
2971
0
            if (value<-1 || value>100)
2972
0
            {
2973
0
               goto bad_arg;
2974
0
            }
2975
0
            st->voice_ratio = value;
2976
0
        }
2977
0
        break;
2978
0
        case OPUS_GET_VOICE_RATIO_REQUEST:
2979
0
        {
2980
0
            opus_int32 *value = va_arg(ap, opus_int32*);
2981
0
            if (!value)
2982
0
            {
2983
0
               goto bad_arg;
2984
0
            }
2985
0
            *value = st->voice_ratio;
2986
0
        }
2987
0
        break;
2988
241k
        case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2989
241k
        {
2990
241k
            opus_int32 value = va_arg(ap, opus_int32);
2991
241k
            if(value<0 || value>1)
2992
0
            {
2993
0
               goto bad_arg;
2994
0
            }
2995
241k
            st->vbr_constraint = value;
2996
241k
        }
2997
0
        break;
2998
0
        case OPUS_GET_VBR_CONSTRAINT_REQUEST:
2999
0
        {
3000
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3001
0
            if (!value)
3002
0
            {
3003
0
               goto bad_arg;
3004
0
            }
3005
0
            *value = st->vbr_constraint;
3006
0
        }
3007
0
        break;
3008
241k
        case OPUS_SET_SIGNAL_REQUEST:
3009
241k
        {
3010
241k
            opus_int32 value = va_arg(ap, opus_int32);
3011
241k
            if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
3012
0
            {
3013
0
               goto bad_arg;
3014
0
            }
3015
241k
            st->signal_type = value;
3016
241k
        }
3017
0
        break;
3018
0
        case OPUS_GET_SIGNAL_REQUEST:
3019
0
        {
3020
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3021
0
            if (!value)
3022
0
            {
3023
0
               goto bad_arg;
3024
0
            }
3025
0
            *value = st->signal_type;
3026
0
        }
3027
0
        break;
3028
0
        case OPUS_GET_LOOKAHEAD_REQUEST:
3029
0
        {
3030
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3031
0
            if (!value)
3032
0
            {
3033
0
               goto bad_arg;
3034
0
            }
3035
0
            *value = st->Fs/400;
3036
0
            if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY && st->application != OPUS_APPLICATION_FORCED_CELT)
3037
0
                *value += st->delay_compensation;
3038
0
        }
3039
0
        break;
3040
64.8k
        case OPUS_GET_SAMPLE_RATE_REQUEST:
3041
64.8k
        {
3042
64.8k
            opus_int32 *value = va_arg(ap, opus_int32*);
3043
64.8k
            if (!value)
3044
0
            {
3045
0
               goto bad_arg;
3046
0
            }
3047
64.8k
            *value = st->Fs;
3048
64.8k
        }
3049
0
        break;
3050
0
        case OPUS_GET_FINAL_RANGE_REQUEST:
3051
0
        {
3052
0
            opus_uint32 *value = va_arg(ap, opus_uint32*);
3053
0
            if (!value)
3054
0
            {
3055
0
               goto bad_arg;
3056
0
            }
3057
0
            *value = st->rangeFinal;
3058
0
        }
3059
0
        break;
3060
241k
        case OPUS_SET_LSB_DEPTH_REQUEST:
3061
241k
        {
3062
241k
            opus_int32 value = va_arg(ap, opus_int32);
3063
241k
            if (value<8 || value>24)
3064
0
            {
3065
0
               goto bad_arg;
3066
0
            }
3067
241k
            st->lsb_depth=value;
3068
241k
        }
3069
0
        break;
3070
0
        case OPUS_GET_LSB_DEPTH_REQUEST:
3071
0
        {
3072
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3073
0
            if (!value)
3074
0
            {
3075
0
               goto bad_arg;
3076
0
            }
3077
0
            *value = st->lsb_depth;
3078
0
        }
3079
0
        break;
3080
0
        case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
3081
0
        {
3082
0
            opus_int32 value = va_arg(ap, opus_int32);
3083
0
            if (value != OPUS_FRAMESIZE_ARG    && value != OPUS_FRAMESIZE_2_5_MS &&
3084
0
                value != OPUS_FRAMESIZE_5_MS   && value != OPUS_FRAMESIZE_10_MS  &&
3085
0
                value != OPUS_FRAMESIZE_20_MS  && value != OPUS_FRAMESIZE_40_MS  &&
3086
0
                value != OPUS_FRAMESIZE_60_MS  && value != OPUS_FRAMESIZE_80_MS  &&
3087
0
                value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS)
3088
0
            {
3089
0
               goto bad_arg;
3090
0
            }
3091
0
            st->variable_duration = value;
3092
0
        }
3093
0
        break;
3094
0
        case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
3095
0
        {
3096
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3097
0
            if (!value)
3098
0
            {
3099
0
               goto bad_arg;
3100
0
            }
3101
0
            *value = st->variable_duration;
3102
0
        }
3103
0
        break;
3104
241k
        case OPUS_SET_PREDICTION_DISABLED_REQUEST:
3105
241k
        {
3106
241k
           opus_int32 value = va_arg(ap, opus_int32);
3107
241k
           if (value > 1 || value < 0)
3108
0
              goto bad_arg;
3109
241k
           st->silk_mode.reducedDependency = value;
3110
241k
        }
3111
0
        break;
3112
0
        case OPUS_GET_PREDICTION_DISABLED_REQUEST:
3113
0
        {
3114
0
           opus_int32 *value = va_arg(ap, opus_int32*);
3115
0
           if (!value)
3116
0
              goto bad_arg;
3117
0
           *value = st->silk_mode.reducedDependency;
3118
0
        }
3119
0
        break;
3120
241k
        case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
3121
241k
        {
3122
241k
            opus_int32 value = va_arg(ap, opus_int32);
3123
241k
            if(value<0 || value>1)
3124
0
            {
3125
0
               goto bad_arg;
3126
0
            }
3127
241k
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3128
241k
               celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value));
3129
241k
        }
3130
0
        break;
3131
0
        case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3132
0
        {
3133
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3134
0
            if (!value)
3135
0
            {
3136
0
               goto bad_arg;
3137
0
            }
3138
0
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3139
0
               celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value));
3140
0
            else
3141
0
               *value = 0;
3142
0
        }
3143
0
        break;
3144
#ifdef ENABLE_DRED
3145
        case OPUS_SET_DRED_DURATION_REQUEST:
3146
        {
3147
            opus_int32 value = va_arg(ap, opus_int32);
3148
            if(value<0 || value>DRED_MAX_FRAMES)
3149
            {
3150
               goto bad_arg;
3151
            }
3152
            st->dred_duration = value;
3153
            st->silk_mode.useDRED = !!value;
3154
        }
3155
        break;
3156
        case OPUS_GET_DRED_DURATION_REQUEST:
3157
        {
3158
            opus_int32 *value = va_arg(ap, opus_int32*);
3159
            if (!value)
3160
            {
3161
               goto bad_arg;
3162
            }
3163
            *value = st->dred_duration;
3164
        }
3165
        break;
3166
#endif
3167
#ifdef ENABLE_QEXT
3168
      case OPUS_SET_QEXT_REQUEST:
3169
      {
3170
          opus_int32 value = va_arg(ap, opus_int32);
3171
          if(value<0 || value>1)
3172
          {
3173
             goto bad_arg;
3174
          }
3175
          st->enable_qext = value;
3176
      }
3177
      break;
3178
      case OPUS_GET_QEXT_REQUEST:
3179
      {
3180
          opus_int32 *value = va_arg(ap, opus_int32*);
3181
          if (!value)
3182
          {
3183
             goto bad_arg;
3184
          }
3185
          *value = st->enable_qext;
3186
      }
3187
      break;
3188
#endif
3189
0
        case OPUS_RESET_STATE:
3190
0
        {
3191
0
           void *silk_enc;
3192
0
           silk_EncControlStruct dummy;
3193
0
           char *start;
3194
0
           silk_enc = (char*)st+st->silk_enc_offset;
3195
0
#ifndef DISABLE_FLOAT_API
3196
0
           tonality_analysis_reset(&st->analysis);
3197
0
#endif
3198
3199
0
           start = (char*)&st->OPUS_ENCODER_RESET_START;
3200
0
           OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
3201
3202
0
           if (st->application != OPUS_APPLICATION_FORCED_SILK)
3203
0
              celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
3204
0
           if (st->application != OPUS_APPLICATION_FORCED_CELT)
3205
0
              silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy );
3206
#ifdef ENABLE_DRED
3207
           /* Initialize DRED Encoder */
3208
           dred_encoder_reset( &st->dred_encoder );
3209
#endif
3210
0
           st->stream_channels = st->channels;
3211
0
           st->hybrid_stereo_width_Q14 = 1 << 14;
3212
0
           st->prev_HB_gain = Q15ONE;
3213
0
           st->first = 1;
3214
0
           st->mode = MODE_HYBRID;
3215
0
           st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
3216
0
           st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
3217
0
        }
3218
0
        break;
3219
23.8k
        case OPUS_SET_FORCE_MODE_REQUEST:
3220
23.8k
        {
3221
23.8k
            opus_int32 value = va_arg(ap, opus_int32);
3222
23.8k
            if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
3223
0
            {
3224
0
               goto bad_arg;
3225
0
            }
3226
23.8k
            st->user_forced_mode = value;
3227
23.8k
        }
3228
0
        break;
3229
455
        case OPUS_SET_LFE_REQUEST:
3230
455
        {
3231
455
            opus_int32 value = va_arg(ap, opus_int32);
3232
455
            st->lfe = value;
3233
455
            if (st->application != OPUS_APPLICATION_FORCED_SILK)
3234
455
               ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
3235
455
        }
3236
455
        break;
3237
3.63k
        case OPUS_SET_ENERGY_MASK_REQUEST:
3238
3.63k
        {
3239
3.63k
            celt_glog *value = va_arg(ap, celt_glog*);
3240
3.63k
            st->energy_masking = value;
3241
3.63k
            ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
3242
3.63k
        }
3243
3.63k
        break;
3244
0
        case OPUS_GET_IN_DTX_REQUEST:
3245
0
        {
3246
0
            opus_int32 *value = va_arg(ap, opus_int32*);
3247
0
            if (!value)
3248
0
            {
3249
0
                goto bad_arg;
3250
0
            }
3251
0
            if (st->silk_mode.useDTX && (st->prev_mode == MODE_SILK_ONLY || st->prev_mode == MODE_HYBRID)) {
3252
                /* DTX determined by Silk. */
3253
0
                silk_encoder *silk_enc = (silk_encoder*)(void *)((char*)st+st->silk_enc_offset);
3254
0
                *value = silk_enc->state_Fxx[0].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
3255
                /* Stereo: check second channel unless only the middle channel was encoded. */
3256
0
                if(*value == 1 && st->silk_mode.nChannelsInternal == 2 && silk_enc->prev_decode_only_middle == 0) {
3257
0
                    *value = silk_enc->state_Fxx[1].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
3258
0
                }
3259
0
            }
3260
0
#ifndef DISABLE_FLOAT_API
3261
0
            else if (st->use_dtx) {
3262
                /* DTX determined by Opus. */
3263
0
                *value = st->nb_no_activity_ms_Q1 >= NB_SPEECH_FRAMES_BEFORE_DTX*20*2;
3264
0
            }
3265
0
#endif
3266
0
            else {
3267
0
                *value = 0;
3268
0
            }
3269
0
        }
3270
0
        break;
3271
#ifdef USE_WEIGHTS_FILE
3272
        case OPUS_SET_DNN_BLOB_REQUEST:
3273
        {
3274
            const unsigned char *data = va_arg(ap, const unsigned char *);
3275
            opus_int32 len = va_arg(ap, opus_int32);
3276
            if(len<0 || data == NULL)
3277
            {
3278
               goto bad_arg;
3279
            }
3280
#ifdef ENABLE_DRED
3281
            ret = dred_encoder_load_model(&st->dred_encoder, data, len);
3282
#endif
3283
        }
3284
        break;
3285
#endif
3286
32.4k
        case CELT_GET_MODE_REQUEST:
3287
32.4k
        {
3288
32.4k
           const CELTMode ** value = va_arg(ap, const CELTMode**);
3289
32.4k
           if (!value)
3290
0
           {
3291
0
              goto bad_arg;
3292
0
           }
3293
32.4k
           ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
3294
32.4k
        }
3295
0
        break;
3296
0
        default:
3297
            /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
3298
0
            ret = OPUS_UNIMPLEMENTED;
3299
0
            break;
3300
3.27M
    }
3301
3.27M
    va_end(ap);
3302
3.27M
    return ret;
3303
3.51k
bad_arg:
3304
3.51k
    va_end(ap);
3305
3.51k
    return OPUS_BAD_ARG;
3306
3.27M
}
3307
3308
void opus_encoder_destroy(OpusEncoder *st)
3309
0
{
3310
0
    opus_free(st);
3311
0
}