| Index: src/opus_encoder.c
|
| diff --git a/src/opus_encoder.c b/src/opus_encoder.c
|
| index aae31256ca579a7642b474adc97516d37080b85c..f6f0ce5c3ad162e7a0fa67ed28d98b85b8549f1d 100644
|
| --- a/src/opus_encoder.c
|
| +++ b/src/opus_encoder.c
|
| @@ -40,7 +40,9 @@
|
| #include "arch.h"
|
| #include "opus_private.h"
|
| #include "os_support.h"
|
| -
|
| +#include "cpu_support.h"
|
| +#include "analysis.h"
|
| +#include "mathops.h"
|
| #include "tuning_parameters.h"
|
| #ifdef FIXED_POINT
|
| #include "fixed/structs_FIX.h"
|
| @@ -50,6 +52,12 @@
|
|
|
| #define MAX_ENCODER_BUFFER 480
|
|
|
| +typedef struct {
|
| + opus_val32 XX, XY, YY;
|
| + opus_val16 smoothed_width;
|
| + opus_val16 max_follower;
|
| +} StereoWidthState;
|
| +
|
| struct OpusEncoder {
|
| int celt_enc_offset;
|
| int silk_enc_offset;
|
| @@ -66,14 +74,18 @@ struct OpusEncoder {
|
| opus_int32 Fs;
|
| int use_vbr;
|
| int vbr_constraint;
|
| + int variable_duration;
|
| opus_int32 bitrate_bps;
|
| opus_int32 user_bitrate_bps;
|
| + int lsb_depth;
|
| int encoder_buffer;
|
| + int lfe;
|
|
|
| #define OPUS_ENCODER_RESET_START stream_channels
|
| int stream_channels;
|
| opus_int16 hybrid_stereo_width_Q14;
|
| opus_int32 variable_HP_smth2_Q15;
|
| + opus_val16 prev_HB_gain;
|
| opus_val32 hp_mem[4];
|
| int mode;
|
| int prev_mode;
|
| @@ -83,9 +95,16 @@ struct OpusEncoder {
|
| int silk_bw_switch;
|
| /* Sampling rate (at the API level) */
|
| int first;
|
| + opus_val16 * energy_masking;
|
| + StereoWidthState width_mem;
|
| opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2];
|
| -
|
| +#ifndef DISABLE_FLOAT_API
|
| + TonalityAnalysisState analysis;
|
| + int detected_bandwidth;
|
| + int analysis_offset;
|
| +#endif
|
| opus_uint32 rangeFinal;
|
| + int arch;
|
| };
|
|
|
| /* Transition tables for the voice and music. First column is the
|
| @@ -94,8 +113,8 @@ struct OpusEncoder {
|
| static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
|
| 11000, 1000, /* NB<->MB */
|
| 14000, 1000, /* MB<->WB */
|
| - 21000, 2000, /* WB<->SWB */
|
| - 29000, 2000, /* SWB<->FB */
|
| + 17000, 1000, /* WB<->SWB */
|
| + 20000, 1000, /* SWB<->FB */
|
| };
|
| static const opus_int32 mono_music_bandwidth_thresholds[8] = {
|
| 14000, 1000, /* MB not allowed */
|
| @@ -116,14 +135,14 @@ static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
|
| 48000, 2000, /* SWB<->FB */
|
| };
|
| /* Threshold bit-rates for switching between mono and stereo */
|
| -static const opus_int32 stereo_voice_threshold = 26000;
|
| -static const opus_int32 stereo_music_threshold = 36000;
|
| +static const opus_int32 stereo_voice_threshold = 31000;
|
| +static const opus_int32 stereo_music_threshold = 31000;
|
|
|
| /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
|
| static const opus_int32 mode_thresholds[2][2] = {
|
| /* voice */ /* music */
|
| - { 48000, 24000}, /* mono */
|
| - { 48000, 24000}, /* stereo */
|
| + { 64000, 20000}, /* mono */
|
| + { 36000, 20000}, /* stereo */
|
| };
|
|
|
| int opus_encoder_get_size(int channels)
|
| @@ -167,6 +186,8 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
|
|
|
| st->Fs = Fs;
|
|
|
| + st->arch = opus_select_arch();
|
| +
|
| ret = silk_InitEncoder( silk_enc, &st->silk_mode );
|
| if(ret)return OPUS_INTERNAL_ERROR;
|
|
|
| @@ -180,7 +201,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
|
| st->silk_mode.payloadSize_ms = 20;
|
| st->silk_mode.bitRate = 25000;
|
| st->silk_mode.packetLossPercentage = 0;
|
| - st->silk_mode.complexity = 10;
|
| + st->silk_mode.complexity = 9;
|
| st->silk_mode.useInBandFEC = 0;
|
| st->silk_mode.useDTX = 0;
|
| st->silk_mode.useCBR = 0;
|
| @@ -191,7 +212,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
|
| if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
|
|
|
| celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
|
| - celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(10));
|
| + celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity));
|
|
|
| st->use_vbr = 1;
|
| /* Makes constrained VBR the default (safer for real-time use) */
|
| @@ -206,12 +227,15 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
|
| st->user_forced_mode = OPUS_AUTO;
|
| st->voice_ratio = -1;
|
| st->encoder_buffer = st->Fs/100;
|
| + st->lsb_depth = 24;
|
| + st->variable_duration = OPUS_FRAMESIZE_ARG;
|
|
|
| /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
|
| + 1.5 ms for SILK resamplers and stereo prediction) */
|
| st->delay_compensation = st->Fs/250;
|
|
|
| st->hybrid_stereo_width_Q14 = 1 << 14;
|
| + st->prev_HB_gain = Q15ONE;
|
| st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
|
| st->first = 1;
|
| st->mode = MODE_HYBRID;
|
| @@ -322,7 +346,7 @@ static void silk_biquad_float(
|
|
|
| S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
|
|
|
| - S[ 1 ] = - vout*A[1] + B[2]*inval;
|
| + S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL;
|
|
|
| /* Scale back to Q0 and saturate */
|
| out[ k*stride ] = vout;
|
| @@ -365,6 +389,56 @@ static void hp_cutoff(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *ou
|
| #endif
|
| }
|
|
|
| +#ifdef FIXED_POINT
|
| +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)
|
| +{
|
| + int c, i;
|
| + int shift;
|
| +
|
| + /* Approximates -round(log2(4.*cutoff_Hz/Fs)) */
|
| + shift=celt_ilog2(Fs/(cutoff_Hz*3));
|
| + for (c=0;c<channels;c++)
|
| + {
|
| + for (i=0;i<len;i++)
|
| + {
|
| + opus_val32 x, tmp, y;
|
| + x = SHL32(EXTEND32(in[channels*i+c]), 15);
|
| + /* First stage */
|
| + tmp = x-hp_mem[2*c];
|
| + hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift);
|
| + /* Second stage */
|
| + y = tmp - hp_mem[2*c+1];
|
| + hp_mem[2*c+1] = hp_mem[2*c+1] + PSHR32(tmp - hp_mem[2*c+1], shift);
|
| + out[channels*i+c] = EXTRACT16(SATURATE(PSHR32(y, 15), 32767));
|
| + }
|
| + }
|
| +}
|
| +
|
| +#else
|
| +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)
|
| +{
|
| + int c, i;
|
| + float coef;
|
| +
|
| + coef = 4.0f*cutoff_Hz/Fs;
|
| + for (c=0;c<channels;c++)
|
| + {
|
| + for (i=0;i<len;i++)
|
| + {
|
| + opus_val32 x, tmp, y;
|
| + x = in[channels*i+c];
|
| + /* First stage */
|
| + tmp = x-hp_mem[2*c];
|
| + hp_mem[2*c] = hp_mem[2*c] + coef*(x - hp_mem[2*c]) + VERY_SMALL;
|
| + /* Second stage */
|
| + y = tmp - hp_mem[2*c+1];
|
| + hp_mem[2*c+1] = hp_mem[2*c+1] + coef*(tmp - hp_mem[2*c+1]) + VERY_SMALL;
|
| + out[channels*i+c] = y;
|
| + }
|
| + }
|
| +}
|
| +#endif
|
| +
|
| static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
|
| int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs)
|
| {
|
| @@ -397,6 +471,45 @@ static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, op
|
| }
|
| }
|
|
|
| +static void gain_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
|
| + int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs)
|
| +{
|
| + int i;
|
| + int inc;
|
| + int overlap;
|
| + int c;
|
| + inc = 48000/Fs;
|
| + overlap=overlap48/inc;
|
| + if (channels==1)
|
| + {
|
| + for (i=0;i<overlap;i++)
|
| + {
|
| + opus_val16 g, w;
|
| + w = MULT16_16_Q15(window[i*inc], window[i*inc]);
|
| + g = SHR32(MAC16_16(MULT16_16(w,g2),
|
| + Q15ONE-w, g1), 15);
|
| + out[i] = MULT16_16_Q15(g, in[i]);
|
| + }
|
| + } else {
|
| + for (i=0;i<overlap;i++)
|
| + {
|
| + opus_val16 g, w;
|
| + w = MULT16_16_Q15(window[i*inc], window[i*inc]);
|
| + g = SHR32(MAC16_16(MULT16_16(w,g2),
|
| + Q15ONE-w, g1), 15);
|
| + out[i*2] = MULT16_16_Q15(g, in[i*2]);
|
| + out[i*2+1] = MULT16_16_Q15(g, in[i*2+1]);
|
| + }
|
| + }
|
| + c=0;do {
|
| + for (i=overlap;i<frame_size;i++)
|
| + {
|
| + out[i*channels+c] = MULT16_16_Q15(g2, in[i*channels+c]);
|
| + }
|
| + }
|
| + while (++c<channels);
|
| +}
|
| +
|
| OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
|
| {
|
| int ret;
|
| @@ -438,15 +551,412 @@ static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int m
|
| return st->user_bitrate_bps;
|
| }
|
|
|
| +#ifndef DISABLE_FLOAT_API
|
| +/* Don't use more than 60 ms for the frame size analysis */
|
| +#define MAX_DYNAMIC_FRAMESIZE 24
|
| +/* Estimates how much the bitrate will be boosted based on the sub-frame energy */
|
| +static float transient_boost(const float *E, const float *E_1, int LM, int maxM)
|
| +{
|
| + int i;
|
| + int M;
|
| + float sumE=0, sumE_1=0;
|
| + float metric;
|
| +
|
| + M = IMIN(maxM, (1<<LM)+1);
|
| + for (i=0;i<M;i++)
|
| + {
|
| + sumE += E[i];
|
| + sumE_1 += E_1[i];
|
| + }
|
| + metric = sumE*sumE_1/(M*M);
|
| + /*if (LM==3)
|
| + printf("%f\n", metric);*/
|
| + /*return metric>10 ? 1 : 0;*/
|
| + /*return MAX16(0,1-exp(-.25*(metric-2.)));*/
|
| + return MIN16(1,(float)sqrt(MAX16(0,.05f*(metric-2))));
|
| +}
|
| +
|
| +/* Viterbi decoding trying to find the best frame size combination using look-ahead
|
| +
|
| + State numbering:
|
| + 0: unused
|
| + 1: 2.5 ms
|
| + 2: 5 ms (#1)
|
| + 3: 5 ms (#2)
|
| + 4: 10 ms (#1)
|
| + 5: 10 ms (#2)
|
| + 6: 10 ms (#3)
|
| + 7: 10 ms (#4)
|
| + 8: 20 ms (#1)
|
| + 9: 20 ms (#2)
|
| + 10: 20 ms (#3)
|
| + 11: 20 ms (#4)
|
| + 12: 20 ms (#5)
|
| + 13: 20 ms (#6)
|
| + 14: 20 ms (#7)
|
| + 15: 20 ms (#8)
|
| +*/
|
| +static int transient_viterbi(const float *E, const float *E_1, int N, int frame_cost, int rate)
|
| +{
|
| + int i;
|
| + float cost[MAX_DYNAMIC_FRAMESIZE][16];
|
| + int states[MAX_DYNAMIC_FRAMESIZE][16];
|
| + float best_cost;
|
| + int best_state;
|
| + float factor;
|
| + /* Take into account that we damp VBR in the 32 kb/s to 64 kb/s range. */
|
| + if (rate<80)
|
| + factor=0;
|
| + else if (rate>160)
|
| + factor=1;
|
| + else
|
| + factor = (rate-80.f)/80.f;
|
| + /* Makes variable framesize less aggressive at lower bitrates, but I can't
|
| + find any valid theoretical justification for this (other than it seems
|
| + to help) */
|
| + for (i=0;i<16;i++)
|
| + {
|
| + /* Impossible state */
|
| + states[0][i] = -1;
|
| + cost[0][i] = 1e10;
|
| + }
|
| + for (i=0;i<4;i++)
|
| + {
|
| + cost[0][1<<i] = (frame_cost + rate*(1<<i))*(1+factor*transient_boost(E, E_1, i, N+1));
|
| + states[0][1<<i] = i;
|
| + }
|
| + for (i=1;i<N;i++)
|
| + {
|
| + int j;
|
| +
|
| + /* Follow continuations */
|
| + for (j=2;j<16;j++)
|
| + {
|
| + cost[i][j] = cost[i-1][j-1];
|
| + states[i][j] = j-1;
|
| + }
|
| +
|
| + /* New frames */
|
| + for(j=0;j<4;j++)
|
| + {
|
| + int k;
|
| + float min_cost;
|
| + float curr_cost;
|
| + states[i][1<<j] = 1;
|
| + min_cost = cost[i-1][1];
|
| + for(k=1;k<4;k++)
|
| + {
|
| + float tmp = cost[i-1][(1<<(k+1))-1];
|
| + if (tmp < min_cost)
|
| + {
|
| + states[i][1<<j] = (1<<(k+1))-1;
|
| + min_cost = tmp;
|
| + }
|
| + }
|
| + curr_cost = (frame_cost + rate*(1<<j))*(1+factor*transient_boost(E+i, E_1+i, j, N-i+1));
|
| + cost[i][1<<j] = min_cost;
|
| + /* If part of the frame is outside the analysis window, only count part of the cost */
|
| + if (N-i < (1<<j))
|
| + cost[i][1<<j] += curr_cost*(float)(N-i)/(1<<j);
|
| + else
|
| + cost[i][1<<j] += curr_cost;
|
| + }
|
| + }
|
| +
|
| + best_state=1;
|
| + best_cost = cost[N-1][1];
|
| + /* Find best end state (doesn't force a frame to end at N-1) */
|
| + for (i=2;i<16;i++)
|
| + {
|
| + if (cost[N-1][i]<best_cost)
|
| + {
|
| + best_cost = cost[N-1][i];
|
| + best_state = i;
|
| + }
|
| + }
|
| +
|
| + /* Follow transitions back */
|
| + for (i=N-1;i>=0;i--)
|
| + {
|
| + /*printf("%d ", best_state);*/
|
| + best_state = states[i][best_state];
|
| + }
|
| + /*printf("%d\n", best_state);*/
|
| + return best_state;
|
| +}
|
| +
|
| +int optimize_framesize(const opus_val16 *x, int len, int C, opus_int32 Fs,
|
| + int bitrate, opus_val16 tonality, opus_val32 *mem, int buffering,
|
| + downmix_func downmix)
|
| +{
|
| + int N;
|
| + int i;
|
| + float e[MAX_DYNAMIC_FRAMESIZE+4];
|
| + float e_1[MAX_DYNAMIC_FRAMESIZE+3];
|
| + float memx;
|
| + int bestLM=0;
|
| + int subframe;
|
| + int pos;
|
| + VARDECL(opus_val32, sub);
|
| +
|
| + subframe = Fs/400;
|
| + ALLOC(sub, subframe, opus_val32);
|
| + e[0]=mem[0];
|
| + e_1[0]=1.f/(EPSILON+mem[0]);
|
| + if (buffering)
|
| + {
|
| + /* Consider the CELT delay when not in restricted-lowdelay */
|
| + /* We assume the buffering is between 2.5 and 5 ms */
|
| + int offset = 2*subframe - buffering;
|
| + celt_assert(offset>=0 && offset <= subframe);
|
| + x += C*offset;
|
| + len -= offset;
|
| + e[1]=mem[1];
|
| + e_1[1]=1.f/(EPSILON+mem[1]);
|
| + e[2]=mem[2];
|
| + e_1[2]=1.f/(EPSILON+mem[2]);
|
| + pos = 3;
|
| + } else {
|
| + pos=1;
|
| + }
|
| + N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE);
|
| + memx = x[0];
|
| + for (i=0;i<N;i++)
|
| + {
|
| + float tmp;
|
| + float tmpx;
|
| + int j;
|
| + tmp=EPSILON;
|
| +
|
| + downmix(x, sub, subframe, i*subframe, 0, -2, C);
|
| + if (i==0)
|
| + memx = sub[0];
|
| + for (j=0;j<subframe;j++)
|
| + {
|
| + tmpx = sub[j];
|
| + tmp += (tmpx-memx)*(tmpx-memx);
|
| + memx = tmpx;
|
| + }
|
| + e[i+pos] = tmp;
|
| + e_1[i+pos] = 1.f/tmp;
|
| + }
|
| + /* Hack to get 20 ms working with APPLICATION_AUDIO
|
| + The real problem is that the corresponding memory needs to use 1.5 ms
|
| + from this frame and 1 ms from the next frame */
|
| + e[i+pos] = e[i+pos-1];
|
| + if (buffering)
|
| + N=IMIN(MAX_DYNAMIC_FRAMESIZE, N+2);
|
| + bestLM = transient_viterbi(e, e_1, N, (int)((1.f+.5f*tonality)*(60*C+40)), bitrate/400);
|
| + mem[0] = e[1<<bestLM];
|
| + if (buffering)
|
| + {
|
| + mem[1] = e[(1<<bestLM)+1];
|
| + mem[2] = e[(1<<bestLM)+2];
|
| + }
|
| + return bestLM;
|
| +}
|
| +
|
| +#endif
|
| +
|
| +#ifndef DISABLE_FLOAT_API
|
| #ifdef FIXED_POINT
|
| -#define opus_encode_native opus_encode
|
| -opus_int32 opus_encode(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
|
| - unsigned char *data, opus_int32 out_data_bytes)
|
| +#define PCM2VAL(x) FLOAT2INT16(x)
|
| #else
|
| -#define opus_encode_native opus_encode_float
|
| -opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
|
| - unsigned char *data, opus_int32 out_data_bytes)
|
| +#define PCM2VAL(x) SCALEIN(x)
|
| +#endif
|
| +void downmix_float(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C)
|
| +{
|
| + const float *x;
|
| + opus_val32 scale;
|
| + int j;
|
| + x = (const float *)_x;
|
| + for (j=0;j<subframe;j++)
|
| + sub[j] = PCM2VAL(x[(j+offset)*C+c1]);
|
| + if (c2>-1)
|
| + {
|
| + for (j=0;j<subframe;j++)
|
| + sub[j] += PCM2VAL(x[(j+offset)*C+c2]);
|
| + } else if (c2==-2)
|
| + {
|
| + int c;
|
| + for (c=1;c<C;c++)
|
| + {
|
| + for (j=0;j<subframe;j++)
|
| + sub[j] += PCM2VAL(x[(j+offset)*C+c]);
|
| + }
|
| + }
|
| +#ifdef FIXED_POINT
|
| + scale = (1<<SIG_SHIFT);
|
| +#else
|
| + scale = 1.f;
|
| +#endif
|
| + if (C==-2)
|
| + scale /= C;
|
| + else
|
| + scale /= 2;
|
| + for (j=0;j<subframe;j++)
|
| + sub[j] *= scale;
|
| +}
|
| +#endif
|
| +
|
| +void downmix_int(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C)
|
| +{
|
| + const opus_int16 *x;
|
| + opus_val32 scale;
|
| + int j;
|
| + x = (const opus_int16 *)_x;
|
| + for (j=0;j<subframe;j++)
|
| + sub[j] = x[(j+offset)*C+c1];
|
| + if (c2>-1)
|
| + {
|
| + for (j=0;j<subframe;j++)
|
| + sub[j] += x[(j+offset)*C+c2];
|
| + } else if (c2==-2)
|
| + {
|
| + int c;
|
| + for (c=1;c<C;c++)
|
| + {
|
| + for (j=0;j<subframe;j++)
|
| + sub[j] += x[(j+offset)*C+c];
|
| + }
|
| + }
|
| +#ifdef FIXED_POINT
|
| + scale = (1<<SIG_SHIFT);
|
| +#else
|
| + scale = 1.f/32768;
|
| +#endif
|
| + if (C==-2)
|
| + scale /= C;
|
| + else
|
| + scale /= 2;
|
| + for (j=0;j<subframe;j++)
|
| + sub[j] *= scale;
|
| +}
|
| +
|
| +opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_int32 Fs)
|
| +{
|
| + int new_size;
|
| + if (frame_size<Fs/400)
|
| + return -1;
|
| + if (variable_duration == OPUS_FRAMESIZE_ARG)
|
| + new_size = frame_size;
|
| + else if (variable_duration == OPUS_FRAMESIZE_VARIABLE)
|
| + new_size = Fs/50;
|
| + else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_60_MS)
|
| + new_size = IMIN(3*Fs/50, (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS));
|
| + else
|
| + return -1;
|
| + if (new_size>frame_size)
|
| + return -1;
|
| + if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs &&
|
| + 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs)
|
| + return -1;
|
| + return new_size;
|
| +}
|
| +
|
| +opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size,
|
| + int variable_duration, int C, opus_int32 Fs, int bitrate_bps,
|
| + int delay_compensation, downmix_func downmix, opus_val32 *subframe_mem)
|
| +{
|
| +#ifndef DISABLE_FLOAT_API
|
| + if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200)
|
| + {
|
| + int LM = 3;
|
| + LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps,
|
| + 0, subframe_mem, delay_compensation, downmix);
|
| + while ((Fs/400<<LM)>frame_size)
|
| + LM--;
|
| + frame_size = (Fs/400<<LM);
|
| + } else
|
| #endif
|
| + {
|
| + frame_size = frame_size_select(frame_size, variable_duration, Fs);
|
| + }
|
| + if (frame_size<0)
|
| + return -1;
|
| + return frame_size;
|
| +}
|
| +
|
| +opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem)
|
| +{
|
| + opus_val16 corr;
|
| + opus_val16 ldiff;
|
| + opus_val16 width;
|
| + opus_val32 xx, xy, yy;
|
| + opus_val16 sqrt_xx, sqrt_yy;
|
| + opus_val16 qrrt_xx, qrrt_yy;
|
| + int frame_rate;
|
| + int i;
|
| + opus_val16 short_alpha;
|
| +
|
| + frame_rate = Fs/frame_size;
|
| + short_alpha = Q15ONE - 25*Q15ONE/IMAX(50,frame_rate);
|
| + xx=xy=yy=0;
|
| + for (i=0;i<frame_size;i+=4)
|
| + {
|
| + opus_val32 pxx=0;
|
| + opus_val32 pxy=0;
|
| + opus_val32 pyy=0;
|
| + opus_val16 x, y;
|
| + x = pcm[2*i];
|
| + y = pcm[2*i+1];
|
| + pxx = SHR32(MULT16_16(x,x),2);
|
| + pxy = SHR32(MULT16_16(x,y),2);
|
| + pyy = SHR32(MULT16_16(y,y),2);
|
| + x = pcm[2*i+2];
|
| + y = pcm[2*i+3];
|
| + pxx += SHR32(MULT16_16(x,x),2);
|
| + pxy += SHR32(MULT16_16(x,y),2);
|
| + pyy += SHR32(MULT16_16(y,y),2);
|
| + x = pcm[2*i+4];
|
| + y = pcm[2*i+5];
|
| + pxx += SHR32(MULT16_16(x,x),2);
|
| + pxy += SHR32(MULT16_16(x,y),2);
|
| + pyy += SHR32(MULT16_16(y,y),2);
|
| + x = pcm[2*i+6];
|
| + y = pcm[2*i+7];
|
| + pxx += SHR32(MULT16_16(x,x),2);
|
| + pxy += SHR32(MULT16_16(x,y),2);
|
| + pyy += SHR32(MULT16_16(y,y),2);
|
| +
|
| + xx += SHR32(pxx, 10);
|
| + xy += SHR32(pxy, 10);
|
| + yy += SHR32(pyy, 10);
|
| + }
|
| + mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX);
|
| + mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);
|
| + mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY);
|
| + mem->XX = MAX32(0, mem->XX);
|
| + mem->XY = MAX32(0, mem->XY);
|
| + mem->YY = MAX32(0, mem->YY);
|
| + if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
|
| + {
|
| + sqrt_xx = celt_sqrt(mem->XX);
|
| + sqrt_yy = celt_sqrt(mem->YY);
|
| + qrrt_xx = celt_sqrt(sqrt_xx);
|
| + qrrt_yy = celt_sqrt(sqrt_yy);
|
| + /* Inter-channel correlation */
|
| + mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy);
|
| + corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16);
|
| + /* Approximate loudness difference */
|
| + ldiff = Q15ONE*ABS16(qrrt_xx-qrrt_yy)/(EPSILON+qrrt_xx+qrrt_yy);
|
| + width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ldiff);
|
| + /* Smoothing over one second */
|
| + mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
|
| + /* Peak follower */
|
| + mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
|
| + } else {
|
| + width = 0;
|
| + corr=Q15ONE;
|
| + ldiff=0;
|
| + }
|
| + /*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);*/
|
| + return EXTRACT16(MIN32(Q15ONE,20*mem->max_follower));
|
| +}
|
| +
|
| +opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
|
| + unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
|
| + const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, int analysis_channels, downmix_func downmix)
|
| {
|
| void *silk_enc;
|
| CELTEncoder *celt_enc;
|
| @@ -471,7 +981,14 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| int frame_rate;
|
| opus_int32 max_rate; /* Max bitrate we're allowed to use */
|
| int curr_bandwidth;
|
| + opus_val16 HB_gain;
|
| opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
|
| + int total_buffer;
|
| + opus_val16 stereo_width;
|
| + const CELTMode *celt_mode;
|
| + AnalysisInfo analysis_info;
|
| + int analysis_read_pos_bak=-1;
|
| + int analysis_read_subframe_bak=-1;
|
| VARDECL(opus_val16, tmp_prefill);
|
|
|
| ALLOC_STACK;
|
| @@ -479,25 +996,70 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| max_data_bytes = IMIN(1276, out_data_bytes);
|
|
|
| st->rangeFinal = 0;
|
| - if (400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs &&
|
| + if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs &&
|
| 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size != 3*st->Fs)
|
| - {
|
| - RESTORE_STACK;
|
| - return OPUS_BAD_ARG;
|
| - }
|
| - if (max_data_bytes<=0)
|
| + || (400*frame_size < st->Fs)
|
| + || max_data_bytes<=0
|
| + )
|
| {
|
| RESTORE_STACK;
|
| return OPUS_BAD_ARG;
|
| }
|
| silk_enc = (char*)st+st->silk_enc_offset;
|
| celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
|
| -
|
| if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
|
| delay_compensation = 0;
|
| else
|
| delay_compensation = st->delay_compensation;
|
|
|
| + lsb_depth = IMIN(lsb_depth, st->lsb_depth);
|
| +
|
| + analysis_info.valid = 0;
|
| + celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
|
| +#ifndef DISABLE_FLOAT_API
|
| +#ifdef FIXED_POINT
|
| + if (st->silk_mode.complexity >= 10 && st->Fs==48000)
|
| +#else
|
| + if (st->silk_mode.complexity >= 7 && st->Fs==48000)
|
| +#endif
|
| + {
|
| + analysis_read_pos_bak = st->analysis.read_pos;
|
| + analysis_read_subframe_bak = st->analysis.read_subframe;
|
| + run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
|
| + c1, c2, analysis_channels, st->Fs,
|
| + lsb_depth, downmix, &analysis_info);
|
| + }
|
| +#endif
|
| +
|
| + st->voice_ratio = -1;
|
| +
|
| +#ifndef DISABLE_FLOAT_API
|
| + st->detected_bandwidth = 0;
|
| + if (analysis_info.valid)
|
| + {
|
| + int analysis_bandwidth;
|
| + if (st->signal_type == OPUS_AUTO)
|
| + st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob));
|
| +
|
| + analysis_bandwidth = analysis_info.bandwidth;
|
| + if (analysis_bandwidth<=12)
|
| + st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
|
| + else if (analysis_bandwidth<=14)
|
| + st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
|
| + else if (analysis_bandwidth<=16)
|
| + st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
|
| + else if (analysis_bandwidth<=18)
|
| + st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
|
| + else
|
| + st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
|
| + }
|
| +#endif
|
| +
|
| + if (st->channels==2 && st->force_channels!=1)
|
| + stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
|
| + else
|
| + stereo_width = 0;
|
| + total_buffer = delay_compensation;
|
| st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
|
|
|
| frame_rate = st->Fs/frame_size;
|
| @@ -540,8 +1102,12 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| else if (st->signal_type == OPUS_SIGNAL_MUSIC)
|
| voice_est = 0;
|
| else if (st->voice_ratio >= 0)
|
| + {
|
| voice_est = st->voice_ratio*327>>8;
|
| - else if (st->application == OPUS_APPLICATION_VOIP)
|
| + /* For AUDIO, never be more than 90% confident of having speech */
|
| + if (st->application == OPUS_APPLICATION_AUDIO)
|
| + voice_est = IMIN(voice_est, 115);
|
| + } else if (st->application == OPUS_APPLICATION_VOIP)
|
| voice_est = 115;
|
| else
|
| voice_est = 48;
|
| @@ -561,9 +1127,9 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| opus_int32 stereo_threshold;
|
| stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
|
| if (st->stream_channels == 2)
|
| - stereo_threshold -= 4000;
|
| + stereo_threshold -= 1000;
|
| else
|
| - stereo_threshold += 4000;
|
| + stereo_threshold += 1000;
|
| st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
|
| } else {
|
| st->stream_channels = st->channels;
|
| @@ -592,15 +1158,21 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| st->mode = MODE_SILK_ONLY;
|
| }
|
| #else
|
| - int chan;
|
| opus_int32 mode_voice, mode_music;
|
| opus_int32 threshold;
|
|
|
| - chan = (st->channels==2) && st->force_channels!=1;
|
| - mode_voice = mode_thresholds[chan][0];
|
| - mode_music = mode_thresholds[chan][1];
|
| + /* Interpolate based on stereo width */
|
| + mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
|
| + + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
|
| + mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
|
| + + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
|
| + /* Interpolate based on speech/music probability */
|
| threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
|
| + /* Bias towards SILK for VoIP because of some useful features */
|
| + if (st->application == OPUS_APPLICATION_VOIP)
|
| + threshold += 8000;
|
|
|
| + /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
|
| /* Hysteresis */
|
| if (st->prev_mode == MODE_CELT_ONLY)
|
| threshold -= 4000;
|
| @@ -658,6 +1230,7 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| redundancy = 1;
|
| celt_to_silk = 1;
|
| st->silk_bw_switch = 0;
|
| + prefill=1;
|
| }
|
|
|
| if (redundancy)
|
| @@ -750,6 +1323,31 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
|
| if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
|
| st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
|
| +#ifndef FIXED_POINT
|
| + /* Use detected bandwidth to reduce the encoded bandwidth. */
|
| + if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
|
| + {
|
| + int min_detected_bandwidth;
|
| + /* Makes bandwidth detection more conservative just in case the detector
|
| + gets it wrong when we could have coded a high bandwidth transparently.
|
| + When operating in SILK/hybrid mode, we don't go below wideband to avoid
|
| + more complicated switches that require redundancy. */
|
| + if (st->bitrate_bps <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
|
| + min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
|
| + else if (st->bitrate_bps <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
|
| + min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
|
| + else if (st->bitrate_bps <= 30000*st->stream_channels)
|
| + min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
|
| + else if (st->bitrate_bps <= 44000*st->stream_channels)
|
| + min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
|
| + else
|
| + min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
|
| +
|
| + st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
|
| + st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
|
| + }
|
| +#endif
|
| + celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
|
|
|
| /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */
|
| if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
|
| @@ -758,6 +1356,11 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| /* CELT mode doesn't support mediumband, use wideband instead */
|
| if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
|
| st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
|
| + if (st->lfe)
|
| + {
|
| + st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
|
| + st->mode = MODE_CELT_ONLY;
|
| + }
|
|
|
| /* Can't support higher than wideband for >20 ms frames */
|
| if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND))
|
| @@ -765,16 +1368,22 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| VARDECL(unsigned char, tmp_data);
|
| int nb_frames;
|
| int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
|
| - OpusRepacketizer rp;
|
| + VARDECL(OpusRepacketizer, rp);
|
| opus_int32 bytes_per_frame;
|
|
|
| + if (analysis_read_pos_bak!= -1)
|
| + {
|
| + st->analysis.read_pos = analysis_read_pos_bak;
|
| + st->analysis.read_subframe = analysis_read_subframe_bak;
|
| + }
|
|
|
| nb_frames = frame_size > st->Fs/25 ? 3 : 2;
|
| bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames);
|
|
|
| ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
|
|
|
| - opus_repacketizer_init(&rp);
|
| + ALLOC(rp, 1, OpusRepacketizer);
|
| + opus_repacketizer_init(rp);
|
|
|
| bak_mode = st->user_forced_mode;
|
| bak_bandwidth = st->user_bandwidth;
|
| @@ -796,20 +1405,22 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
|
| if (to_celt && i==nb_frames-1)
|
| st->user_forced_mode = MODE_CELT_ONLY;
|
| - tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50, tmp_data+i*bytes_per_frame, bytes_per_frame);
|
| + tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50,
|
| + tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth,
|
| + NULL, 0, c1, c2, analysis_channels, downmix);
|
| if (tmp_len<0)
|
| {
|
| RESTORE_STACK;
|
| return OPUS_INTERNAL_ERROR;
|
| }
|
| - ret = opus_repacketizer_cat(&rp, tmp_data+i*bytes_per_frame, tmp_len);
|
| + ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
|
| if (ret<0)
|
| {
|
| RESTORE_STACK;
|
| return OPUS_INTERNAL_ERROR;
|
| }
|
| }
|
| - ret = opus_repacketizer_out(&rp, data, out_data_bytes);
|
| + ret = opus_repacketizer_out(rp, data, out_data_bytes);
|
| if (ret<0)
|
| {
|
| RESTORE_STACK;
|
| @@ -822,7 +1433,6 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| RESTORE_STACK;
|
| return ret;
|
| }
|
| -
|
| curr_bandwidth = st->bandwidth;
|
|
|
| /* Chooses the appropriate mode for speech
|
| @@ -839,9 +1449,9 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
|
|
| ec_enc_init(&enc, data, max_data_bytes-1);
|
|
|
| - ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16);
|
| - for (i=0;i<delay_compensation*st->channels;i++)
|
| - pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st->channels+i];
|
| + ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16);
|
| + for (i=0;i<total_buffer*st->channels;i++)
|
| + pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels+i];
|
|
|
| if (st->mode == MODE_CELT_ONLY)
|
| hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
|
| @@ -856,46 +1466,89 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
|
|
| if (st->application == OPUS_APPLICATION_VOIP)
|
| {
|
| - hp_cutoff(pcm, cutoff_Hz, &pcm_buf[delay_compensation*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
|
| + hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
|
| } else {
|
| - for (i=0;i<frame_size*st->channels;i++)
|
| - pcm_buf[delay_compensation*st->channels + i] = pcm[i];
|
| + dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
|
| }
|
|
|
| +
|
| +
|
| /* SILK processing */
|
| + HB_gain = Q15ONE;
|
| if (st->mode != MODE_CELT_ONLY)
|
| {
|
| + opus_int32 total_bitRate, celt_rate;
|
| #ifdef FIXED_POINT
|
| const opus_int16 *pcm_silk;
|
| #else
|
| VARDECL(opus_int16, pcm_silk);
|
| ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
|
| #endif
|
| - st->silk_mode.bitRate = 8*bytes_target*frame_rate;
|
| +
|
| + /* Distribute bits between SILK and CELT */
|
| + total_bitRate = 8 * bytes_target * frame_rate;
|
| if( st->mode == MODE_HYBRID ) {
|
| - st->silk_mode.bitRate /= st->stream_channels;
|
| + int HB_gain_ref;
|
| + /* Base rate for SILK */
|
| + st->silk_mode.bitRate = st->stream_channels * ( 5000 + 1000 * ( st->Fs == 100 * frame_size ) );
|
| if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) {
|
| - if( st->Fs == 100 * frame_size ) {
|
| - /* 24 kHz, 10 ms */
|
| - st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + st->use_vbr * 1000 ) * 2 ) / 3;
|
| - } else {
|
| - /* 24 kHz, 20 ms */
|
| - st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 1000 + st->use_vbr * 1000 ) * 2 ) / 3;
|
| - }
|
| - } else {
|
| - if( st->Fs == 100 * frame_size ) {
|
| - /* 48 kHz, 10 ms */
|
| - st->silk_mode.bitRate = ( st->silk_mode.bitRate + 8000 + st->use_vbr * 3000 ) / 2;
|
| - } else {
|
| - /* 48 kHz, 20 ms */
|
| - st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st->use_vbr * 1000 ) / 2;
|
| - }
|
| + /* SILK gets 2/3 of the remaining bits */
|
| + st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 2 / 3;
|
| + } else { /* FULLBAND */
|
| + /* SILK gets 3/5 of the remaining bits */
|
| + st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 3 / 5;
|
| }
|
| - st->silk_mode.bitRate *= st->stream_channels;
|
| - /* don't let SILK use more than 80% */
|
| - if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) {
|
| - st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
|
| + /* Don't let SILK use more than 80% */
|
| + if( st->silk_mode.bitRate > total_bitRate * 4/5 ) {
|
| + st->silk_mode.bitRate = total_bitRate * 4/5;
|
| }
|
| + /* Increasingly attenuate high band when it gets allocated fewer bits */
|
| + celt_rate = total_bitRate - st->silk_mode.bitRate;
|
| + HB_gain_ref = (curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND) ? 3000 : 3600;
|
| + HB_gain = SHL32((opus_val32)celt_rate, 9) / SHR32((opus_val32)celt_rate + st->stream_channels * HB_gain_ref, 6);
|
| + HB_gain = HB_gain < Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE;
|
| + } else {
|
| + /* SILK gets all bits */
|
| + st->silk_mode.bitRate = total_bitRate;
|
| + }
|
| +
|
| + /* Surround masking for SILK */
|
| + if (st->energy_masking && st->use_vbr && !st->lfe)
|
| + {
|
| + opus_val32 mask_sum=0;
|
| + opus_val16 masking_depth;
|
| + opus_int32 rate_offset;
|
| + int c;
|
| + int end = 17;
|
| + opus_int16 srate = 16000;
|
| + if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
|
| + {
|
| + end = 13;
|
| + srate = 8000;
|
| + } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
|
| + {
|
| + end = 15;
|
| + srate = 12000;
|
| + }
|
| + for (c=0;c<st->channels;c++)
|
| + {
|
| + for(i=0;i<end;i++)
|
| + {
|
| + opus_val16 mask;
|
| + mask = MAX16(MIN16(st->energy_masking[21*c+i],
|
| + QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
|
| + if (mask > 0)
|
| + mask = HALF16(mask);
|
| + mask_sum += mask;
|
| + }
|
| + }
|
| + /* Conservative rate reduction, we cut the masking in half */
|
| + masking_depth = HALF16(mask_sum / end*st->channels);
|
| + rate_offset = PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT);
|
| + rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
|
| + rate_offset += QCONST16(.4f, DB_SHIFT);
|
| + st->silk_mode.bitRate += rate_offset;
|
| + bytes_target += rate_offset * frame_size / (8 * st->Fs);
|
| }
|
|
|
| st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
|
| @@ -955,6 +1608,18 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| if (prefill)
|
| {
|
| opus_int32 zero=0;
|
| + int prefill_offset;
|
| + /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
|
| + a discontinuity. The exact location is what we need to avoid leaving any "gap"
|
| + in the audio when mixing with the redundant CELT frame. Here we can afford to
|
| + overwrite st->delay_buffer because the only thing that uses it before it gets
|
| + rewritten is tmp_prefill[] and even then only the part after the ramp really
|
| + gets used (rather than sent to the encoder and discarded) */
|
| + prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
|
| + gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
|
| + 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
|
| + for(i=0;i<prefill_offset;i++)
|
| + st->delay_buffer[i]=0;
|
| #ifdef FIXED_POINT
|
| pcm_silk = st->delay_buffer;
|
| #else
|
| @@ -965,10 +1630,10 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| }
|
|
|
| #ifdef FIXED_POINT
|
| - pcm_silk = pcm_buf+delay_compensation*st->channels;
|
| + pcm_silk = pcm_buf+total_buffer*st->channels;
|
| #else
|
| for (i=0;i<frame_size*st->channels;i++)
|
| - pcm_silk[i] = FLOAT2INT16(pcm_buf[delay_compensation*st->channels + i]);
|
| + pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]);
|
| #endif
|
| ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
|
| if( ret ) {
|
| @@ -1053,9 +1718,18 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| } else {
|
| if (st->use_vbr)
|
| {
|
| + opus_int32 bonus=0;
|
| +#ifndef DISABLE_FLOAT_API
|
| + if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != st->Fs/50)
|
| + {
|
| + bonus = (60*st->stream_channels+40)*(st->Fs/frame_size-50);
|
| + if (analysis_info.valid)
|
| + bonus = (opus_int32)(bonus*(1.f+.5f*analysis_info.tonality));
|
| + }
|
| +#endif
|
| celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
|
| celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
|
| - celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
|
| + celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps+bonus));
|
| nb_compr_bytes = max_data_bytes-1-redundancy_bytes;
|
| } else {
|
| nb_compr_bytes = bytes_target;
|
| @@ -1070,24 +1744,27 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
|
| {
|
| for (i=0;i<st->channels*st->Fs/400;i++)
|
| - tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-st->delay_compensation-st->Fs/400)*st->channels + i];
|
| + tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels + i];
|
| }
|
|
|
| - for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+delay_compensation));i++)
|
| + for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+total_buffer));i++)
|
| st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size];
|
| for (;i<st->encoder_buffer*st->channels;i++)
|
| - st->delay_buffer[i] = pcm_buf[(frame_size+delay_compensation-st->encoder_buffer)*st->channels+i];
|
| -
|
| + st->delay_buffer[i] = pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels+i];
|
|
|
| + /* gain_fade() and stereo_fade() need to be after the buffer copying
|
| + because we don't want any of this to affect the SILK part */
|
| + if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
|
| + gain_fade(pcm_buf, pcm_buf,
|
| + st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
|
| + }
|
| + st->prev_HB_gain = HB_gain;
|
| if (st->mode != MODE_HYBRID || st->stream_channels==1)
|
| - st->silk_mode.stereoWidth_Q14 = 1<<14;
|
| - if( st->channels == 2 ) {
|
| + st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),IMAX(0,st->bitrate_bps-32000));
|
| + if( !st->energy_masking && st->channels == 2 ) {
|
| /* Apply stereo width reduction (at low bitrates) */
|
| if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
|
| opus_val16 g1, g2;
|
| - const CELTMode *celt_mode;
|
| -
|
| - celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
|
| g1 = st->hybrid_stereo_width_Q14;
|
| g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
|
| #ifdef FIXED_POINT
|
| @@ -1144,6 +1821,10 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| ec_enc_shrink(&enc, nb_compr_bytes);
|
| }
|
|
|
| +#ifndef DISABLE_FLOAT_API
|
| + if (redundancy || st->mode != MODE_SILK_ONLY)
|
| + celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info));
|
| +#endif
|
|
|
| /* 5 ms redundant frame for CELT->SILK */
|
| if (redundancy && celt_to_silk)
|
| @@ -1268,45 +1949,89 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
|
| #ifdef FIXED_POINT
|
|
|
| #ifndef DISABLE_FLOAT_API
|
| -opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int frame_size,
|
| +opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
|
| unsigned char *data, opus_int32 max_data_bytes)
|
| {
|
| int i, ret;
|
| + int frame_size;
|
| + int delay_compensation;
|
| VARDECL(opus_int16, in);
|
| ALLOC_STACK;
|
|
|
| - if(frame_size<0)
|
| - {
|
| - RESTORE_STACK;
|
| - return OPUS_BAD_ARG;
|
| - }
|
| + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
|
| + delay_compensation = 0;
|
| + else
|
| + delay_compensation = st->delay_compensation;
|
| + frame_size = compute_frame_size(pcm, analysis_frame_size,
|
| + st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
|
| + delay_compensation, downmix_float, st->analysis.subframe_mem);
|
|
|
| ALLOC(in, frame_size*st->channels, opus_int16);
|
|
|
| for (i=0;i<frame_size*st->channels;i++)
|
| in[i] = FLOAT2INT16(pcm[i]);
|
| - ret = opus_encode(st, in, frame_size, data, max_data_bytes);
|
| + ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_float);
|
| RESTORE_STACK;
|
| return ret;
|
| }
|
| #endif
|
|
|
| +opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
|
| + unsigned char *data, opus_int32 out_data_bytes)
|
| +{
|
| + int frame_size;
|
| + int delay_compensation;
|
| + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
|
| + delay_compensation = 0;
|
| + else
|
| + delay_compensation = st->delay_compensation;
|
| + frame_size = compute_frame_size(pcm, analysis_frame_size,
|
| + st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
|
| + delay_compensation, downmix_float, st->analysis.subframe_mem);
|
| + return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int);
|
| +}
|
| +
|
| #else
|
| -opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size,
|
| +opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
|
| unsigned char *data, opus_int32 max_data_bytes)
|
| {
|
| int i, ret;
|
| + int frame_size;
|
| + int delay_compensation;
|
| VARDECL(float, in);
|
| ALLOC_STACK;
|
|
|
| + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
|
| + delay_compensation = 0;
|
| + else
|
| + delay_compensation = st->delay_compensation;
|
| + frame_size = compute_frame_size(pcm, analysis_frame_size,
|
| + st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
|
| + delay_compensation, downmix_float, st->analysis.subframe_mem);
|
| +
|
| ALLOC(in, frame_size*st->channels, float);
|
|
|
| for (i=0;i<frame_size*st->channels;i++)
|
| in[i] = (1.0f/32768)*pcm[i];
|
| - ret = opus_encode_float(st, in, frame_size, data, max_data_bytes);
|
| + ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int);
|
| RESTORE_STACK;
|
| return ret;
|
| }
|
| +opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
|
| + unsigned char *data, opus_int32 out_data_bytes)
|
| +{
|
| + int frame_size;
|
| + int delay_compensation;
|
| + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
|
| + delay_compensation = 0;
|
| + else
|
| + delay_compensation = st->delay_compensation;
|
| + frame_size = compute_frame_size(pcm, analysis_frame_size,
|
| + st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
|
| + delay_compensation, downmix_float, st->analysis.subframe_mem);
|
| + return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24,
|
| + pcm, analysis_frame_size, 0, -2, st->channels, downmix_float);
|
| +}
|
| #endif
|
|
|
|
|
| @@ -1339,6 +2064,10 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_APPLICATION_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->application;
|
| }
|
| break;
|
| @@ -1360,6 +2089,10 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_BITRATE_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
|
| }
|
| break;
|
| @@ -1367,21 +2100,29 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if((value<1 || value>st->channels) && value != OPUS_AUTO)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->force_channels = value;
|
| }
|
| break;
|
| case OPUS_GET_FORCE_CHANNELS_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->force_channels;
|
| }
|
| break;
|
| case OPUS_SET_MAX_BANDWIDTH_REQUEST:
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| - if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
|
| - return OPUS_BAD_ARG;
|
| + if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->max_bandwidth = value;
|
| if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
|
| st->silk_mode.maxInternalSampleRate = 8000;
|
| @@ -1395,6 +2136,10 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_MAX_BANDWIDTH_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->max_bandwidth;
|
| }
|
| break;
|
| @@ -1402,7 +2147,9 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->user_bandwidth = value;
|
| if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
|
| st->silk_mode.maxInternalSampleRate = 8000;
|
| @@ -1416,6 +2163,10 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_BANDWIDTH_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->bandwidth;
|
| }
|
| break;
|
| @@ -1423,13 +2174,19 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if(value<0 || value>1)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->silk_mode.useDTX = value;
|
| }
|
| break;
|
| case OPUS_GET_DTX_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->silk_mode.useDTX;
|
| }
|
| break;
|
| @@ -1437,7 +2194,9 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if(value<0 || value>10)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->silk_mode.complexity = value;
|
| celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
|
| }
|
| @@ -1445,6 +2204,10 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_COMPLEXITY_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->silk_mode.complexity;
|
| }
|
| break;
|
| @@ -1452,13 +2215,19 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if(value<0 || value>1)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->silk_mode.useInBandFEC = value;
|
| }
|
| break;
|
| case OPUS_GET_INBAND_FEC_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->silk_mode.useInBandFEC;
|
| }
|
| break;
|
| @@ -1466,7 +2235,9 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if (value < 0 || value > 100)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->silk_mode.packetLossPercentage = value;
|
| celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
|
| }
|
| @@ -1474,6 +2245,10 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->silk_mode.packetLossPercentage;
|
| }
|
| break;
|
| @@ -1481,7 +2256,9 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if(value<0 || value>1)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->use_vbr = value;
|
| st->silk_mode.useCBR = 1-value;
|
| }
|
| @@ -1489,20 +2266,30 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_VBR_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->use_vbr;
|
| }
|
| break;
|
| case OPUS_SET_VOICE_RATIO_REQUEST:
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| - if (value>100 || value<-1)
|
| - goto bad_arg;
|
| + if (value<-1 || value>100)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->voice_ratio = value;
|
| }
|
| break;
|
| case OPUS_GET_VOICE_RATIO_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->voice_ratio;
|
| }
|
| break;
|
| @@ -1510,13 +2297,19 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if(value<0 || value>1)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->vbr_constraint = value;
|
| }
|
| break;
|
| case OPUS_GET_VBR_CONSTRAINT_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->vbr_constraint;
|
| }
|
| break;
|
| @@ -1524,19 +2317,29 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
|
| - return OPUS_BAD_ARG;
|
| + {
|
| + goto bad_arg;
|
| + }
|
| st->signal_type = value;
|
| }
|
| break;
|
| case OPUS_GET_SIGNAL_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->signal_type;
|
| }
|
| break;
|
| case OPUS_GET_LOOKAHEAD_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->Fs/400;
|
| if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
|
| *value += st->delay_compensation;
|
| @@ -1545,10 +2348,9 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_SAMPLE_RATE_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| - if (value==NULL)
|
| + if (!value)
|
| {
|
| - ret = OPUS_BAD_ARG;
|
| - break;
|
| + goto bad_arg;
|
| }
|
| *value = st->Fs;
|
| }
|
| @@ -1556,19 +2358,55 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| case OPUS_GET_FINAL_RANGE_REQUEST:
|
| {
|
| opus_uint32 *value = va_arg(ap, opus_uint32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| *value = st->rangeFinal;
|
| }
|
| break;
|
| case OPUS_SET_LSB_DEPTH_REQUEST:
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| - ret = celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(value));
|
| + if (value<8 || value>24)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| + st->lsb_depth=value;
|
| }
|
| break;
|
| case OPUS_GET_LSB_DEPTH_REQUEST:
|
| {
|
| opus_int32 *value = va_arg(ap, opus_int32*);
|
| - celt_encoder_ctl(celt_enc, OPUS_GET_LSB_DEPTH(value));
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| + *value = st->lsb_depth;
|
| + }
|
| + break;
|
| + case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
|
| + {
|
| + opus_int32 value = va_arg(ap, opus_int32);
|
| + if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS &&
|
| + value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS &&
|
| + value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS &&
|
| + value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_VARIABLE)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| + st->variable_duration = value;
|
| + celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value));
|
| + }
|
| + break;
|
| + case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
|
| + {
|
| + opus_int32 *value = va_arg(ap, opus_int32*);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| + *value = st->variable_duration;
|
| }
|
| break;
|
| case OPUS_RESET_STATE:
|
| @@ -1585,6 +2423,7 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| silk_InitEncoder( silk_enc, &dummy );
|
| st->stream_channels = st->channels;
|
| st->hybrid_stereo_width_Q14 = 1 << 14;
|
| + st->prev_HB_gain = Q15ONE;
|
| st->first = 1;
|
| st->mode = MODE_HYBRID;
|
| st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
|
| @@ -1595,10 +2434,37 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
|
| {
|
| opus_int32 value = va_arg(ap, opus_int32);
|
| if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
|
| + {
|
| goto bad_arg;
|
| + }
|
| st->user_forced_mode = value;
|
| }
|
| break;
|
| + case OPUS_SET_LFE_REQUEST:
|
| + {
|
| + opus_int32 value = va_arg(ap, opus_int32);
|
| + st->lfe = value;
|
| + ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
|
| + }
|
| + break;
|
| + case OPUS_SET_ENERGY_MASK_REQUEST:
|
| + {
|
| + opus_val16 *value = va_arg(ap, opus_val16*);
|
| + st->energy_masking = value;
|
| + ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
|
| + }
|
| + break;
|
| +
|
| + case CELT_GET_MODE_REQUEST:
|
| + {
|
| + const CELTMode ** value = va_arg(ap, const CELTMode**);
|
| + if (!value)
|
| + {
|
| + goto bad_arg;
|
| + }
|
| + ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
|
| + }
|
| + break;
|
| default:
|
| /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
|
| ret = OPUS_UNIMPLEMENTED;
|
|
|