diff --git a/include/opus_defines.h b/include/opus_defines.h index 84df7c7a..32b7c976 100644 --- a/include/opus_defines.h +++ b/include/opus_defines.h @@ -46,7 +46,7 @@ extern "C" { #define OPUS_OK 0 /** One or more invalid/out of range arguments @hideinitializer*/ #define OPUS_BAD_ARG -1 -/** The mode struct passed is invalid @hideinitializer*/ +/** Not enough bytes allocated in the buffer @hideinitializer*/ #define OPUS_BUFFER_TOO_SMALL -2 /** An internal error was detected @hideinitializer*/ #define OPUS_INTERNAL_ERROR -3 diff --git a/src/opus_multistream_encoder.c b/src/opus_multistream_encoder.c index aa464f59..854f85e3 100644 --- a/src/opus_multistream_encoder.c +++ b/src/opus_multistream_encoder.c @@ -744,17 +744,13 @@ static int opus_multistream_encode_native RESTORE_STACK; return OPUS_BAD_ARG; } - /* Estimate (slightly overestimating) of the smallest packet the encoder can produce. */ - if (50*frame_size <= Fs) - { - smallest_packet = st->layout.nb_streams*4; - } else { - smallest_packet = st->layout.nb_streams*4*50*frame_size/Fs; - } + + /* Smallest packet the encoder can produce. */ + smallest_packet = st->layout.nb_streams*2-1; if (max_data_bytes < smallest_packet) { RESTORE_STACK; - return OPUS_BAD_ARG; + return OPUS_BUFFER_TOO_SMALL; } ALLOC(buf, 2*frame_size, opus_val16); coupled_size = opus_encoder_get_size(2); @@ -766,12 +762,6 @@ static int opus_multistream_encode_native surround_analysis(celt_mode, pcm, bandSMR, mem, preemph_mem, frame_size, 120, st->layout.nb_channels, Fs, copy_channel_in); } - if (max_data_bytes < 4*st->layout.nb_streams-1) - { - RESTORE_STACK; - return OPUS_BUFFER_TOO_SMALL; - } - /* Compute bitrate allocation between streams (this could be a lot better) */ rate_sum = surround_rate_allocation(st, bitrates, frame_size); @@ -871,8 +861,10 @@ static int opus_multistream_encode_native /* number of bytes left (+Toc) */ curr_max = max_data_bytes - tot_size; /* Reserve three bytes for the last stream and four for the others */ - curr_max -= IMAX(0,4*(st->layout.nb_streams-s-1)-1); + curr_max -= IMAX(0,2*(st->layout.nb_streams-s-1)-1); curr_max = IMIN(curr_max,MS_FRAME_TMP); + /* Repacketizer will add one byte for self-delimited frames */ + if (s != st->layout.nb_streams-1) curr_max--; if (!vbr && s == st->layout.nb_streams-1) opus_encoder_ctl(enc, OPUS_SET_BITRATE(curr_max*(8*Fs/frame_size))); len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max, lsb_depth,