Skip to content

Commit

Permalink
Handle the case where the initial state didn't fit
Browse files Browse the repository at this point in the history
  • Loading branch information
jmvalin committed Sep 20, 2023
1 parent 71da978 commit 52c1562
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
5 changes: 5 additions & 0 deletions silk/dred_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ int dred_encode_silk_frame(const DREDEnc *enc, unsigned char *buf, int max_chunk
dead_zone + state_qoffset,
r + state_qoffset,
p0 + state_qoffset);
if (ec_tell(&ec_encoder) > 8*max_bytes) {
return 0;
}
for (i = 0; i < IMIN(2*max_chunks, enc->latents_buffer_fill-enc->latent_offset-1); i += 2)
{
ec_enc ec_bak;
Expand All @@ -256,6 +259,8 @@ int dred_encode_silk_frame(const DREDEnc *enc, unsigned char *buf, int max_chunk
);
if (ec_tell(&ec_encoder) > 8*max_bytes) {
ec_encoder = ec_bak;
/* If we haven't been able to code one chunk, give up on DRED completely. */
if (i==0) return 0;
break;
}
}
Expand Down
37 changes: 19 additions & 18 deletions src/opus_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
int analysis_read_subframe_bak=-1;
int is_silence = 0;
#endif
int apply_padding;
#ifdef ENABLE_NEURAL_FEC
opus_int32 dred_bitrate_bps;
#endif
Expand Down Expand Up @@ -2252,6 +2253,7 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
}
/* Count ToC and redundancy */
ret += 1+redundancy_bytes;
apply_padding = !st->use_vbr;
#ifdef ENABLE_NEURAL_FEC
if (st->dred_duration > 0) {
opus_extension_data extension;
Expand All @@ -2271,26 +2273,25 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
buf[1] = DRED_EXPERIMENTAL_VERSION;
#endif
dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES);
dred_bytes += DRED_EXPERIMENTAL_BYTES;
celt_assert(dred_bytes <= dred_bytes_left);
extension.id = DRED_EXTENSION_ID;
extension.frame = 0;
extension.data = buf;
extension.len = dred_bytes;
ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1);
} else if (!st->use_vbr) {
ret = opus_packet_pad(data, ret, max_data_bytes);
if (ret == OPUS_OK)
ret = max_data_bytes;
}
if (ret < 0)
{
RESTORE_STACK;
return OPUS_INTERNAL_ERROR;
if (dred_bytes > 0) {
dred_bytes += DRED_EXPERIMENTAL_BYTES;
celt_assert(dred_bytes <= dred_bytes_left);
extension.id = DRED_EXTENSION_ID;
extension.frame = 0;
extension.data = buf;
extension.len = dred_bytes;
ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1);
if (ret < 0)
{
RESTORE_STACK;
return OPUS_INTERNAL_ERROR;
}
apply_padding = 0;
}
}
} else
}
#endif
if (!st->use_vbr)
if (apply_padding)
{
if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK)
{
Expand Down

0 comments on commit 52c1562

Please sign in to comment.