Skip to content

Commit

Permalink
Allow for more than 1275 bytes for qext packets
Browse files Browse the repository at this point in the history
  • Loading branch information
jmvalin committed Dec 16, 2024
1 parent 21104a7 commit 570c2a9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion celt/bands.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions celt/celt_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)<<BITRES);
Expand Down Expand Up @@ -2441,7 +2446,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in
int new_compressedBytes;
/* Don't give any bits for the first 80 kb/s per channel. Then 80% of the excess. */
opus_int32 offset = C*80000*frame_size/mode->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;
Expand Down
1 change: 1 addition & 0 deletions celt/modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions celt/opus_custom_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@

#include "opus_custom.h"
#include "arch.h"
#include "modes.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#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] <rate> <channels> <frame size> "
Expand Down

0 comments on commit 570c2a9

Please sign in to comment.