From 570c2a9516ba95de9510586f38bd6d67bc15ca6e Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Mon, 16 Dec 2024 15:02:48 -0500 Subject: [PATCH] Allow for more than 1275 bytes for qext packets --- celt/bands.c | 2 +- celt/celt_encoder.c | 11 ++++++++--- celt/modes.h | 1 + celt/opus_custom_demo.c | 5 +++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/celt/bands.c b/celt/bands.c index 5e33bf117..0a4221b8d 100644 --- a/celt/bands.c +++ b/celt/bands.c @@ -1835,7 +1835,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, ec_ctx ext_ec_save, ext_ec_save2; unsigned char *ext_bytes_buf; int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes; - unsigned char ext_bytes_save[1275]; + unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP]; #endif opus_val16 w[2]; compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w); diff --git a/celt/celt_encoder.c b/celt/celt_encoder.c index b7f7108e1..41b3c3568 100644 --- a/celt/celt_encoder.c +++ b/celt/celt_encoder.c @@ -1714,6 +1714,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in opus_val32 toneishness=0; VARDECL(celt_glog, surround_dynalloc); int qext_bytes=0; + int packet_size_cap = 1275; #ifdef ENABLE_QEXT int qext_scale; int qext_end=0; @@ -1763,6 +1764,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in #ifdef ENABLE_QEXT qext_scale = st->qext_scale; + if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP; #endif prefilter_mem = st->in_mem+CC*(overlap); @@ -1810,7 +1812,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in #endif /* Can't produce more than 1275 output bytes */ - nbCompressedBytes = IMIN(nbCompressedBytes,1275); + nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap); if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX) { @@ -1910,6 +1912,9 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in /* Pretend we've filled all the remaining bits with zeros (that's what the initialiser did anyway) */ tell = nbCompressedBytes*8; +#ifdef ENABLE_QEXT + if (st->enable_qext) nbCompressedBytes = IMIN(nbCompressedBytes, 1275); +#endif enc->nbits_total+=tell-ec_tell(enc); } c=0; do { @@ -2347,7 +2352,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms. The CELT allocator will just not be able to use more than that anyway. */ - nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM)); + nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap>>(3-LM)); if (!hybrid) { base_target = vbr_rate - ((40*C+20)<Fs/8; - qext_bytes = IMAX(0, (nbCompressedBytes-offset)*4/5); + qext_bytes = IMAX(nbCompressedBytes-1275, IMAX(0, (nbCompressedBytes-offset)*4/5)); padding_len_bytes = (qext_bytes+253)/254; qext_bytes = IMIN(qext_bytes, nbCompressedBytes-min_allowed-padding_len_bytes-1); padding_len_bytes = (qext_bytes+253)/254; diff --git a/celt/modes.h b/celt/modes.h index f37dbdd83..a45333f61 100644 --- a/celt/modes.h +++ b/celt/modes.h @@ -81,6 +81,7 @@ struct OpusCustomMode { }; #ifdef ENABLE_QEXT +#define QEXT_PACKET_SIZE_CAP 3825 #define NB_QEXT_BANDS 14 void compute_qext_mode(CELTMode *qext, const CELTMode *m); #endif diff --git a/celt/opus_custom_demo.c b/celt/opus_custom_demo.c index 1b1650ba7..2cb30d387 100644 --- a/celt/opus_custom_demo.c +++ b/celt/opus_custom_demo.c @@ -32,12 +32,17 @@ #include "opus_custom.h" #include "arch.h" +#include "modes.h" #include #include #include #include +#ifdef ENABLE_QEXT +#define MAX_PACKET QEXT_PACKET_SIZE_CAP +#else #define MAX_PACKET 1275 +#endif static void print_usage(char **argv) { fprintf (stderr, "Usage: %s [-e | -d] "