Index: third_party/opus/src/src/opus_multistream_encoder.c |
diff --git a/third_party/opus/src/src/opus_multistream_encoder.c b/third_party/opus/src/src/opus_multistream_encoder.c |
index 1698223a16a43536b5dc5fc326244657914a856b..032fc0039b91312e756bf89c6bf0de052f68acf1 100644 |
--- a/third_party/opus/src/src/opus_multistream_encoder.c |
+++ b/third_party/opus/src/src/opus_multistream_encoder.c |
@@ -87,7 +87,6 @@ struct OpusMSEncoder { |
int variable_duration; |
MappingType mapping_type; |
opus_int32 bitrate_bps; |
- float subframe_mem[3]; |
/* Encoder states go here */ |
/* then opus_val32 window_mem[channels*120]; */ |
/* then opus_val32 preemph_mem[channels]; */ |
@@ -133,6 +132,29 @@ static opus_val32 *ms_get_window_mem(OpusMSEncoder *st) |
return (opus_val32*)(void*)ptr; |
} |
+#ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
+static int validate_ambisonics(int nb_channels, int *nb_streams, int *nb_coupled_streams) |
+{ |
+ int order_plus_one; |
+ int acn_channels; |
+ int nondiegetic_channels; |
+ |
+ order_plus_one = isqrt32(nb_channels); |
+ acn_channels = order_plus_one * order_plus_one; |
+ nondiegetic_channels = nb_channels - acn_channels; |
+ |
+ if (order_plus_one < 1 || order_plus_one > 15 || |
+ (nondiegetic_channels != 0 && nondiegetic_channels != 2)) |
+ return 0; |
+ |
+ if (nb_streams) |
+ *nb_streams = acn_channels + (nondiegetic_channels != 0); |
+ if (nb_coupled_streams) |
+ *nb_coupled_streams = nondiegetic_channels != 0; |
+ return 1; |
+} |
+#endif |
+ |
static int validate_encoder_layout(const ChannelLayout *layout) |
{ |
int s; |
@@ -240,6 +262,7 @@ void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *b |
int pos[8] = {0}; |
int upsample; |
int frame_size; |
+ int freq_size; |
opus_val16 channel_offset; |
opus_val32 bandE[21]; |
opus_val16 maskLogE[3][21]; |
@@ -250,6 +273,7 @@ void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *b |
upsample = resampling_factor(rate); |
frame_size = len*upsample; |
+ freq_size = IMIN(960, frame_size); |
/* LM = log2(frame_size / 120) */ |
for (LM=0;LM<celt_mode->maxLM;LM++) |
@@ -258,7 +282,7 @@ void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *b |
ALLOC(in, frame_size+overlap, opus_val32); |
ALLOC(x, len, opus_val16); |
- ALLOC(freq, frame_size, opus_val32); |
+ ALLOC(freq, freq_size, opus_val32); |
channel_pos(channels, pos); |
@@ -268,6 +292,9 @@ void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *b |
for (c=0;c<channels;c++) |
{ |
+ int frame; |
+ int nb_frames = frame_size/freq_size; |
+ celt_assert(nb_frames*freq_size == frame_size); |
OPUS_COPY(in, mem+c*overlap, overlap); |
(*copy_channel_in)(x, 1, pcm, channels, c, len); |
celt_preemphasis(x, in+overlap, frame_size, 1, upsample, celt_mode->preemph, preemph_mem+c, 0); |
@@ -284,18 +311,26 @@ void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *b |
} |
} |
#endif |
- clt_mdct_forward(&celt_mode->mdct, in, freq, celt_mode->window, |
- overlap, celt_mode->maxLM-LM, 1, arch); |
- if (upsample != 1) |
+ OPUS_CLEAR(bandE, 21); |
+ for (frame=0;frame<nb_frames;frame++) |
{ |
- int bound = len; |
- for (i=0;i<bound;i++) |
- freq[i] *= upsample; |
- for (;i<frame_size;i++) |
- freq[i] = 0; |
- } |
+ opus_val32 tmpE[21]; |
+ clt_mdct_forward(&celt_mode->mdct, in+960*frame, freq, celt_mode->window, |
+ overlap, celt_mode->maxLM-LM, 1, arch); |
+ if (upsample != 1) |
+ { |
+ int bound = freq_size/upsample; |
+ for (i=0;i<bound;i++) |
+ freq[i] *= upsample; |
+ for (;i<freq_size;i++) |
+ freq[i] = 0; |
+ } |
- compute_band_energies(celt_mode, freq, bandE, 21, 1, LM); |
+ compute_band_energies(celt_mode, freq, tmpE, 21, 1, LM, arch); |
+ /* If we have multiple frames, take the max energy. */ |
+ for (i=0;i<21;i++) |
+ bandE[i] = MAX32(bandE[i], tmpE[i]); |
+ } |
amp2Log2(celt_mode, 21, 21, bandE, bandLogE+21*c, 1); |
/* Apply spreading function with -6 dB/band going up and -12 dB/band going down. */ |
for (i=1;i<21;i++) |
@@ -411,8 +446,8 @@ opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_ |
#ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
} else if (mapping_family==254) |
{ |
- nb_streams=channels; |
- nb_coupled_streams=0; |
+ if (!validate_ambisonics(channels, &nb_streams, &nb_coupled_streams)) |
+ return 0; |
#endif |
} else |
return 0; |
@@ -448,7 +483,6 @@ static int opus_multistream_encoder_init_impl( |
st->layout.nb_channels = channels; |
st->layout.nb_streams = streams; |
st->layout.nb_coupled_streams = coupled_streams; |
- st->subframe_mem[0]=st->subframe_mem[1]=st->subframe_mem[2]=0; |
if (mapping_type != MAPPING_TYPE_SURROUND) |
st->lfe_stream = -1; |
st->bitrate_bps = OPUS_AUTO; |
@@ -456,8 +490,16 @@ static int opus_multistream_encoder_init_impl( |
st->variable_duration = OPUS_FRAMESIZE_ARG; |
for (i=0;i<st->layout.nb_channels;i++) |
st->layout.mapping[i] = mapping[i]; |
- if (!validate_layout(&st->layout) || !validate_encoder_layout(&st->layout)) |
+ if (!validate_layout(&st->layout)) |
return OPUS_BAD_ARG; |
+ if (mapping_type == MAPPING_TYPE_SURROUND && |
+ !validate_encoder_layout(&st->layout)) |
+ return OPUS_BAD_ARG; |
+#ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
+ if (mapping_type == MAPPING_TYPE_AMBISONICS && |
+ !validate_ambisonics(st->layout.nb_channels, NULL, NULL)) |
+ return OPUS_BAD_ARG; |
+#endif |
ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
coupled_size = opus_encoder_get_size(2); |
mono_size = opus_encoder_get_size(1); |
@@ -553,10 +595,12 @@ int opus_multistream_surround_encoder_init( |
} else if (mapping_family==254) |
{ |
int i; |
- *streams=channels; |
- *coupled_streams=0; |
- for(i=0;i<channels;i++) |
- mapping[i] = i; |
+ if (!validate_ambisonics(channels, streams, coupled_streams)) |
+ return OPUS_BAD_ARG; |
+ for(i = 0; i < (*streams - *coupled_streams); i++) |
+ mapping[i] = i + (*coupled_streams * 2); |
+ for(i = 0; i < *coupled_streams * 2; i++) |
+ mapping[i + (*streams - *coupled_streams)] = i; |
#endif |
} else |
return OPUS_UNIMPLEMENTED; |
@@ -672,58 +716,59 @@ static void surround_rate_allocation( |
int lfe_offset; |
int coupled_ratio; /* Q8 */ |
int lfe_ratio; /* Q8 */ |
+ int nb_lfe; |
+ int nb_uncoupled; |
+ int nb_coupled; |
+ int nb_normal; |
+ opus_int32 channel_offset; |
+ opus_int32 bitrate; |
+ int total; |
+ |
+ nb_lfe = (st->lfe_stream!=-1); |
+ nb_coupled = st->layout.nb_coupled_streams; |
+ nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe; |
+ nb_normal = 2*nb_coupled + nb_uncoupled; |
+ |
+ /* Give each non-LFE channel enough bits per channel for coding band energy. */ |
+ channel_offset = 40*IMAX(50, Fs/frame_size); |
- if (st->bitrate_bps > st->layout.nb_channels*40000) |
- stream_offset = 20000; |
- else |
- stream_offset = st->bitrate_bps/st->layout.nb_channels/2; |
- stream_offset += 60*(Fs/frame_size-50); |
- /* We start by giving each stream (coupled or uncoupled) the same bitrate. |
- This models the main saving of coupled channels over uncoupled. */ |
- /* The LFE stream is an exception to the above and gets fewer bits. */ |
- lfe_offset = 3500 + 60*(Fs/frame_size-50); |
- /* Coupled streams get twice the mono rate after the first 20 kb/s. */ |
- coupled_ratio = 512; |
- /* Should depend on the bitrate, for now we assume LFE gets 1/8 the bits of mono */ |
- lfe_ratio = 32; |
- |
- /* Compute bitrate allocation between streams */ |
if (st->bitrate_bps==OPUS_AUTO) |
{ |
- channel_rate = Fs+60*Fs/frame_size; |
+ bitrate = nb_normal*(channel_offset + Fs + 10000) + 8000*nb_lfe; |
} else if (st->bitrate_bps==OPUS_BITRATE_MAX) |
{ |
- channel_rate = 300000; |
+ bitrate = nb_normal*300000 + nb_lfe*128000; |
} else { |
- int nb_lfe; |
- int nb_uncoupled; |
- int nb_coupled; |
- int total; |
- nb_lfe = (st->lfe_stream!=-1); |
- nb_coupled = st->layout.nb_coupled_streams; |
- nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe; |
- total = (nb_uncoupled<<8) /* mono */ |
- + coupled_ratio*nb_coupled /* stereo */ |
- + nb_lfe*lfe_ratio; |
- channel_rate = 256*(st->bitrate_bps-lfe_offset*nb_lfe-stream_offset*(nb_coupled+nb_uncoupled))/total; |
+ bitrate = st->bitrate_bps; |
} |
-#ifndef FIXED_POINT |
- if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) |
- { |
- opus_int32 bonus; |
- bonus = 60*(Fs/frame_size-50); |
- channel_rate += bonus; |
- } |
-#endif |
+ |
+ /* Give LFE some basic stream_channel allocation but never exceed 1/20 of the |
+ total rate for the non-energy part to avoid problems at really low rate. */ |
+ lfe_offset = IMIN(bitrate/20, 3000) + 15*IMAX(50, Fs/frame_size); |
+ |
+ /* We give each stream (coupled or uncoupled) a starting bitrate. |
+ This models the main saving of coupled channels over uncoupled. */ |
+ stream_offset = (bitrate - channel_offset*nb_normal - lfe_offset*nb_lfe)/nb_normal/2; |
+ stream_offset = IMAX(0, IMIN(20000, stream_offset)); |
+ |
+ /* Coupled streams get twice the mono rate after the offset is allocated. */ |
+ coupled_ratio = 512; |
+ /* Should depend on the bitrate, for now we assume LFE gets 1/8 the bits of mono */ |
+ lfe_ratio = 32; |
+ |
+ total = (nb_uncoupled<<8) /* mono */ |
+ + coupled_ratio*nb_coupled /* stereo */ |
+ + nb_lfe*lfe_ratio; |
+ channel_rate = 256*(opus_int64)(bitrate - lfe_offset*nb_lfe - stream_offset*(nb_coupled+nb_uncoupled) - channel_offset*nb_normal)/total; |
for (i=0;i<st->layout.nb_streams;i++) |
{ |
if (i<st->layout.nb_coupled_streams) |
- rate[i] = stream_offset+(channel_rate*coupled_ratio>>8); |
+ rate[i] = 2*channel_offset + IMAX(0, stream_offset+(channel_rate*coupled_ratio>>8)); |
else if (i!=st->lfe_stream) |
- rate[i] = stream_offset+channel_rate; |
+ rate[i] = channel_offset + IMAX(0, stream_offset + channel_rate); |
else |
- rate[i] = lfe_offset+(channel_rate*lfe_ratio>>8); |
+ rate[i] = IMAX(0, lfe_offset+(channel_rate*lfe_ratio>>8)); |
} |
} |
@@ -736,47 +781,72 @@ static void ambisonics_rate_allocation( |
) |
{ |
int i; |
- int non_mono_rate; |
int total_rate; |
+ int directional_rate; |
+ int nondirectional_rate; |
+ int leftover_bits; |
- /* The mono channel gets (rate_ratio_num / rate_ratio_den) times as many bits |
- * as all other channels */ |
+ /* Each nondirectional channel gets (rate_ratio_num / rate_ratio_den) times |
+ * as many bits as all other ambisonics channels. |
+ */ |
const int rate_ratio_num = 4; |
const int rate_ratio_den = 3; |
- const int num_channels = st->layout.nb_streams; |
+ const int nb_channels = st->layout.nb_streams + st->layout.nb_coupled_streams; |
+ const int nb_nondirectional_channels = st->layout.nb_coupled_streams * 2 + 1; |
+ const int nb_directional_channels = st->layout.nb_streams - 1; |
if (st->bitrate_bps==OPUS_AUTO) |
{ |
- total_rate = num_channels * (20000 + st->layout.nb_streams*(Fs+60*Fs/frame_size)); |
+ total_rate = (st->layout.nb_coupled_streams + st->layout.nb_streams) * |
+ (Fs+60*Fs/frame_size) + st->layout.nb_streams * 15000; |
} else if (st->bitrate_bps==OPUS_BITRATE_MAX) |
{ |
- total_rate = num_channels * 320000; |
- } else { |
+ total_rate = nb_channels * 320000; |
+ } else |
+ { |
total_rate = st->bitrate_bps; |
} |
- /* Let y be the non-mono rate and let p, q be integers such that the mono |
- * channel rate is (p/q) * y. |
+ /* Let y be the directional rate, m be the num of nondirectional channels |
+ * m = (s + 1) |
+ * and let p, q be integers such that the nondirectional rate is |
+ * m_rate = (p / q) * y |
* Also let T be the total bitrate to allocate. Then |
- * (n - 1) y + (p/q) y = T |
- * y = (T q) / (qn - q + p) |
+ * T = (n - m) * y + m * m_rate |
+ * Solving for y, |
+ * y = (q * T) / (m * (p - q) + n * q) |
*/ |
- non_mono_rate = |
- total_rate * rate_ratio_den |
- / (rate_ratio_den*num_channels + rate_ratio_num - rate_ratio_den); |
+ directional_rate = |
+ total_rate * rate_ratio_den |
+ / (nb_nondirectional_channels * (rate_ratio_num - rate_ratio_den) |
+ + nb_channels * rate_ratio_den); |
-#ifndef FIXED_POINT |
- if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) |
- { |
- opus_int32 bonus = 60*(Fs/frame_size-50); |
- non_mono_rate += bonus; |
- } |
-#endif |
+ /* Calculate the nondirectional rate. |
+ * m_rate = y * (p / q) |
+ */ |
+ nondirectional_rate = directional_rate * rate_ratio_num / rate_ratio_den; |
- rate[0] = total_rate - (num_channels - 1) * non_mono_rate; |
- for (i=1;i<st->layout.nb_streams;i++) |
+ /* Calculate the leftover from truncation error. |
+ * leftover = T - y * (n - m) - m_rate * m |
+ * Place leftover bits in omnidirectional channel. |
+ */ |
+ leftover_bits = total_rate |
+ - directional_rate * nb_directional_channels |
+ - nondirectional_rate * nb_nondirectional_channels; |
+ |
+ /* Calculate rates for each channel */ |
+ for (i = 0; i < st->layout.nb_streams; i++) |
{ |
- rate[i] = non_mono_rate; |
+ if (i < st->layout.nb_coupled_streams) |
+ { |
+ rate[i] = nondirectional_rate * 2; |
+ } else if (i == st->layout.nb_coupled_streams) |
+ { |
+ rate[i] = nondirectional_rate + leftover_bits; |
+ } else |
+ { |
+ rate[i] = directional_rate; |
+ } |
} |
} |
#endif /* ENABLE_EXPERIMENTAL_AMBISONICS */ |
@@ -812,8 +882,8 @@ static opus_int32 rate_allocation( |
return rate_sum; |
} |
-/* Max size in case the encoder decides to return three frames */ |
-#define MS_FRAME_TMP (3*1275+7) |
+/* Max size in case the encoder decides to return six frames (6 x 20 ms = 120 ms) */ |
+#define MS_FRAME_TMP (6*1275+12) |
static int opus_multistream_encode_native |
( |
OpusMSEncoder *st, |
@@ -859,32 +929,8 @@ static int opus_multistream_encode_native |
opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_VBR(&vbr)); |
opus_encoder_ctl((OpusEncoder*)ptr, CELT_GET_MODE(&celt_mode)); |
- { |
- opus_int32 delay_compensation; |
- int channels; |
- |
- channels = st->layout.nb_streams + st->layout.nb_coupled_streams; |
- opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_LOOKAHEAD(&delay_compensation)); |
- delay_compensation -= Fs/400; |
- frame_size = compute_frame_size(pcm, analysis_frame_size, |
- st->variable_duration, channels, Fs, st->bitrate_bps, |
- delay_compensation, downmix |
-#ifndef DISABLE_FLOAT_API |
- , st->subframe_mem |
-#endif |
- ); |
- } |
- |
- if (400*frame_size < Fs) |
- { |
- RESTORE_STACK; |
- return OPUS_BAD_ARG; |
- } |
- /* Validate frame_size before using it to allocate stack space. |
- This mirrors the checks in opus_encode[_float](). */ |
- if (400*frame_size != Fs && 200*frame_size != Fs && |
- 100*frame_size != Fs && 50*frame_size != Fs && |
- 25*frame_size != Fs && 50*frame_size != 3*Fs) |
+ frame_size = frame_size_select(analysis_frame_size, st->variable_duration, Fs); |
+ if (frame_size <= 0) |
{ |
RESTORE_STACK; |
return OPUS_BAD_ARG; |
@@ -892,6 +938,9 @@ static int opus_multistream_encode_native |
/* Smallest packet the encoder can produce. */ |
smallest_packet = st->layout.nb_streams*2-1; |
+ /* 100 ms needs an extra byte per stream for the ToC. */ |
+ if (Fs/frame_size == 10) |
+ smallest_packet += st->layout.nb_streams; |
if (max_data_bytes < smallest_packet) |
{ |
RESTORE_STACK; |
@@ -1013,6 +1062,9 @@ static int opus_multistream_encode_native |
curr_max = max_data_bytes - tot_size; |
/* Reserve one byte for the last stream and two for the others */ |
curr_max -= IMAX(0,2*(st->layout.nb_streams-s-1)-1); |
+ /* For 100 ms, reserve an extra byte per stream for the ToC */ |
+ if (Fs/frame_size == 10) |
+ curr_max -= st->layout.nb_streams-s-1; |
curr_max = IMIN(curr_max,MS_FRAME_TMP); |
/* Repacketizer will add one or two bytes for self-delimited frames */ |
if (s != st->layout.nb_streams-1) curr_max -= curr_max>253 ? 2 : 1; |
@@ -1161,9 +1213,11 @@ int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) |
case OPUS_SET_BITRATE_REQUEST: |
{ |
opus_int32 value = va_arg(ap, opus_int32); |
- if (value<0 && value!=OPUS_AUTO && value!=OPUS_BITRATE_MAX) |
+ if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) |
{ |
- goto bad_arg; |
+ if (value <= 0) |
+ goto bad_arg; |
+ value = IMIN(300000*st->layout.nb_channels, IMAX(500*st->layout.nb_channels, value)); |
} |
st->bitrate_bps = value; |
} |
@@ -1206,6 +1260,7 @@ int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) |
case OPUS_GET_INBAND_FEC_REQUEST: |
case OPUS_GET_FORCE_CHANNELS_REQUEST: |
case OPUS_GET_PREDICTION_DISABLED_REQUEST: |
+ case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: |
{ |
OpusEncoder *enc; |
/* For int32* GET params, just query the first stream */ |
@@ -1252,6 +1307,7 @@ int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) |
case OPUS_SET_FORCE_MODE_REQUEST: |
case OPUS_SET_FORCE_CHANNELS_REQUEST: |
case OPUS_SET_PREDICTION_DISABLED_REQUEST: |
+ case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: |
{ |
int s; |
/* This works for int32 params */ |
@@ -1313,7 +1369,6 @@ int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) |
case OPUS_RESET_STATE: |
{ |
int s; |
- st->subframe_mem[0] = st->subframe_mem[1] = st->subframe_mem[2] = 0; |
if (st->mapping_type == MAPPING_TYPE_SURROUND) |
{ |
OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels); |