Properly check to see if there's room for DRED

This commit is contained in:
Jean-Marc Valin 2023-05-08 19:04:08 -04:00
parent b446e96ad1
commit d7e1bd507f
No known key found for this signature in database
GPG key ID: 531A52533318F00A
2 changed files with 7 additions and 4 deletions

View file

@ -26,6 +26,7 @@
*/ */
#define DRED_VERSION 0 #define DRED_VERSION 0
#define DRED_MIN_BYTES 16
/* these are inpart duplicates to the values defined in dred_rdovae_constants.h */ /* these are inpart duplicates to the values defined in dred_rdovae_constants.h */
#define DRED_NUM_FEATURES 20 #define DRED_NUM_FEATURES 20

View file

@ -2194,18 +2194,20 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
opus_extension_data extension; opus_extension_data extension;
unsigned char buf[DRED_MAX_DATA_SIZE]; unsigned char buf[DRED_MAX_DATA_SIZE];
int dred_chunks; int dred_chunks;
int dred_bytes; int dred_bytes_left;
DREDEnc *dred = &((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.dred_encoder; DREDEnc *dred = &((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.dred_encoder;
dred_chunks = IMIN(st->dred_duration/4, DRED_NUM_REDUNDANCY_FRAMES/2); dred_chunks = IMIN(st->dred_duration/4, DRED_NUM_REDUNDANCY_FRAMES/2);
dred_bytes = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-2); dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-2);
/* Check whether we actually have something to encode. */ /* Check whether we actually have something to encode. */
if (dred_chunks >= 1 && dred_bytes >= 5) { if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+2) {
int dred_bytes;
/* Add temporary extension type and version. /* Add temporary extension type and version.
These bytes will be removed once extension is finalized. */ These bytes will be removed once extension is finalized. */
buf[0] = 'D'; buf[0] = 'D';
buf[1] = DRED_VERSION; buf[1] = DRED_VERSION;
dred_bytes = dred_encode_silk_frame(dred, buf+2, dred_chunks, dred_bytes-2); dred_bytes = dred_encode_silk_frame(dred, buf+2, dred_chunks, dred_bytes_left-2);
dred_bytes += 2; dred_bytes += 2;
celt_assert(dred_bytes <= dred_bytes_left);
extension.id = 127; extension.id = 127;
extension.frame = 0; extension.frame = 0;
extension.data = buf; extension.data = buf;