summaryrefslogtreecommitdiff
path: root/thirdparty/opus/opus_encoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'thirdparty/opus/opus_encoder.c')
-rw-r--r--thirdparty/opus/opus_encoder.c51
1 files changed, 30 insertions, 21 deletions
diff --git a/thirdparty/opus/opus_encoder.c b/thirdparty/opus/opus_encoder.c
index a7e19127d6..9a516a884a 100644
--- a/thirdparty/opus/opus_encoder.c
+++ b/thirdparty/opus/opus_encoder.c
@@ -860,9 +860,6 @@ opus_int32 compute_frame_size(const void *analysis_pcm, int 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;
@@ -871,9 +868,12 @@ opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int3
opus_val16 short_alpha;
frame_rate = Fs/frame_size;
- short_alpha = Q15ONE - 25*Q15ONE/IMAX(50,frame_rate);
+ short_alpha = Q15ONE - MULT16_16(25, Q15ONE)/IMAX(50,frame_rate);
xx=xy=yy=0;
- for (i=0;i<frame_size;i+=4)
+ /* Unroll by 4. The frame size is always a multiple of 4 *except* for
+ 2.5 ms frames at 12 kHz. Since this setting is very rare (and very
+ stupid), we just discard the last two samples. */
+ for (i=0;i<frame_size-3;i+=4)
{
opus_val32 pxx=0;
opus_val32 pxy=0;
@@ -912,6 +912,9 @@ opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int3
mem->YY = MAX32(0, mem->YY);
if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
{
+ opus_val16 corr;
+ opus_val16 ldiff;
+ opus_val16 width;
sqrt_xx = celt_sqrt(mem->XX);
sqrt_yy = celt_sqrt(mem->YY);
qrrt_xx = celt_sqrt(sqrt_xx);
@@ -920,19 +923,15 @@ opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int3
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);
+ ldiff = MULT16_16(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));
+ return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)));
}
opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
@@ -1050,6 +1049,16 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
frame_rate = st->Fs/frame_size;
+ if (!st->use_vbr)
+ {
+ int cbrBytes;
+ /* Multiply by 3 to make sure the division is exact. */
+ int frame_rate3 = 3*st->Fs/frame_size;
+ /* We need to make sure that "int" values always fit in 16 bits. */
+ cbrBytes = IMIN( (3*st->bitrate_bps/8 + frame_rate3/2)/frame_rate3, max_data_bytes);
+ st->bitrate_bps = cbrBytes*(opus_int32)frame_rate3*8/3;
+ max_data_bytes = cbrBytes;
+ }
if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
|| (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400)))
{
@@ -1066,18 +1075,18 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
bw=OPUS_BANDWIDTH_WIDEBAND;
else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
bw=OPUS_BANDWIDTH_NARROWBAND;
- else if (bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
+ else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
+ ret = 1;
+ if (!st->use_vbr)
+ {
+ ret = opus_packet_pad(data, ret, max_data_bytes);
+ if (ret == OPUS_OK)
+ ret = max_data_bytes;
+ }
RESTORE_STACK;
- return 1;
- }
- if (!st->use_vbr)
- {
- int cbrBytes;
- cbrBytes = IMIN( (st->bitrate_bps + 4*frame_rate)/(8*frame_rate) , max_data_bytes);
- st->bitrate_bps = cbrBytes * (8*frame_rate);
- max_data_bytes = cbrBytes;
+ return ret;
}
max_rate = frame_rate*max_data_bytes*8;
@@ -1513,7 +1522,7 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
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;
+ HB_gain = HB_gain < (opus_val32)Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE;
}
} else {
/* SILK gets all bits */