Skip to content

Commit

Permalink
Add --enable-opus-custom-api for QEXT
Browse files Browse the repository at this point in the history
Makes it possible to use QEXT with static modes
  • Loading branch information
jmvalin committed Jan 17, 2025
1 parent 6e6cd36 commit 67dd564
Show file tree
Hide file tree
Showing 10 changed files with 4,517 additions and 43 deletions.
4 changes: 2 additions & 2 deletions celt/celt.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ int celt_decode_with_ec(OpusCustomDecoder * OPUS_RESTRICT st, const unsigned cha
#define celt_decoder_ctl opus_custom_decoder_ctl


#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
#define OPUS_CUSTOM_NOSTATIC
#else
#define OPUS_CUSTOM_NOSTATIC static OPUS_INLINE
Expand All @@ -189,7 +189,7 @@ static const unsigned char spread_icdf[4] = {25, 23, 2, 0};

static const unsigned char tapset_icdf[3]={2,1,0};

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
static const unsigned char toOpusTable[20] = {
0xE0, 0xE8, 0xF0, 0xF8,
0xC0, 0xC8, 0xD0, 0xD8,
Expand Down
16 changes: 8 additions & 8 deletions celt/celt_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct OpusCustomDecoder {
up writing all over memory. */
void validate_celt_decoder(CELTDecoder *st)
{
#ifndef CUSTOM_MODES
#if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API)
celt_assert(st->mode == opus_custom_mode_create(48000, 960, NULL));
celt_assert(st->overlap == 120);
celt_assert(st->end <= 21);
Expand Down Expand Up @@ -181,7 +181,7 @@ OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int
return size;
}

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
{
int ret;
Expand Down Expand Up @@ -241,14 +241,14 @@ OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMod
return OPUS_OK;
}

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
void opus_custom_decoder_destroy(CELTDecoder *st)
{
opus_free(st);
}
#endif /* CUSTOM_MODES */

#ifndef CUSTOM_MODES
#if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API)
/* Special case for stereo with no downsampling and no accumulation. This is
quite common and we can make it faster by processing both channels in the
same loop, reducing overhead due to the dependency loop in the IIR filter. */
Expand Down Expand Up @@ -291,7 +291,7 @@ void deemphasis(celt_sig *in[], opus_res *pcm, int N, int C, int downsample, con
opus_val16 coef0;
VARDECL(celt_sig, scratch);
SAVE_STACK;
#ifndef CUSTOM_MODES
#if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API)
/* Short version for common case. */
if (downsample == 1 && C == 2 && !accum)
{
Expand All @@ -309,7 +309,7 @@ void deemphasis(celt_sig *in[], opus_res *pcm, int N, int C, int downsample, con
celt_sig m = mem[c];
x =in[c];
y = pcm+c;
#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
if (coef[1] != 0)
{
opus_val16 coef1 = coef[1];
Expand Down Expand Up @@ -1079,7 +1079,7 @@ int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char
#ifdef ENABLE_QEXT
ec_dec_init(&ext_dec, NULL, 0);
#endif
#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
if (st->signalling && data!=NULL)
{
int data0=data[0];
Expand Down Expand Up @@ -1497,7 +1497,7 @@ int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *dat
);
}

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)

#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
Expand Down
18 changes: 9 additions & 9 deletions celt/celt_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int
return size;
}

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
{
int ret;
Expand Down Expand Up @@ -220,7 +220,7 @@ static int opus_custom_encoder_init_arch(CELTEncoder *st, const CELTMode *mode,
return OPUS_OK;
}

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
{
return opus_custom_encoder_init_arch(st, mode, channels, opus_select_arch());
Expand All @@ -239,7 +239,7 @@ int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels,
return OPUS_OK;
}

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
void opus_custom_encoder_destroy(CELTEncoder *st)
{
opus_free(st);
Expand Down Expand Up @@ -575,7 +575,7 @@ void celt_preemphasis(const opus_res * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTR
#else
(void)clip; /* Avoids a warning about clip being unused. */
#endif
#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
if (coef[1] != 0)
{
opus_val16 coef1 = coef[1];
Expand Down Expand Up @@ -1783,7 +1783,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in
nbFilledBytes=(tell+4)>>3;
}

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
if (st->signalling && enc==NULL)
{
int tmp = (mode->effEBands-end)>>1;
Expand Down Expand Up @@ -1818,7 +1818,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in
{
opus_int32 den=mode->Fs>>BITRES;
vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
if (st->signalling)
vbr_rate -= 8<<BITRES;
#endif
Expand Down Expand Up @@ -2709,7 +2709,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in
if (ec_get_error(&ext_enc))
return OPUS_INTERNAL_ERROR;
#endif
#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
if (st->signalling)
nbCompressedBytes++;
#endif
Expand All @@ -2722,7 +2722,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in
}


#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)

#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
Expand Down Expand Up @@ -2981,7 +2981,7 @@ int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
st->tapset_decision = 0;
}
break;
#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
case CELT_SET_INPUT_CLIPPING_REQUEST:
{
opus_int32 value = va_arg(ap, opus_int32);
Expand Down
34 changes: 19 additions & 15 deletions celt/cwrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include "arch.h"

#if defined(CUSTOM_MODES) || defined(ENABLE_QEXT)
#define CWRS_EXTRA_ROWS
#endif

#if defined(CUSTOM_MODES)

/*Guaranteed to return a conservatively large estimate of the binary logarithm
with frac bits of fractional precision.
Expand Down Expand Up @@ -207,7 +211,7 @@ int log2_frac(opus_uint32 val, int frac)
Otherwise, we can limit things to the set of N which can be achieved by
splitting a band from a standard Opus mode: 176, 144, 96, 88, 72, 64, 48,
44, 36, 32, 24, 22, 18, 16, 8, 4, 2).*/
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
static const opus_uint32 CELT_PVQ_U_DATA[1488]={
#else
static const opus_uint32 CELT_PVQ_U_DATA[1272]={
Expand All @@ -220,7 +224,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...208:*/
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
Expand All @@ -233,7 +237,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...208:*/
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,
Expand All @@ -250,7 +254,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293,
295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323,
325, 327, 329, 331, 333, 335, 337, 339, 341, 343, 345, 347, 349, 351,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...208:*/
353, 355, 357, 359, 361, 363, 365, 367, 369, 371, 373, 375, 377, 379, 381,
383, 385, 387, 389, 391, 393, 395, 397, 399, 401, 403, 405, 407, 409, 411,
Expand All @@ -272,7 +276,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
43513, 44105, 44701, 45301, 45905, 46513, 47125, 47741, 48361, 48985, 49613,
50245, 50881, 51521, 52165, 52813, 53465, 54121, 54781, 55445, 56113, 56785,
57461, 58141, 58825, 59513, 60205, 60901, 61601,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...208:*/
62305, 63013, 63725, 64441, 65161, 65885, 66613, 67345, 68081, 68821, 69565,
70313, 71065, 71821, 72581, 73345, 74113, 74885, 75661, 76441, 77225, 78013,
Expand All @@ -299,7 +303,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
5309433, 5410559, 5512961, 5616647, 5721625, 5827903, 5935489, 6044391,
6154617, 6266175, 6379073, 6493319, 6608921, 6725887, 6844225, 6963943,
7085049, 7207551,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...208:*/
7331457, 7456775, 7583513, 7711679, 7841281, 7972327, 8104825, 8238783,
8374209, 8511111, 8649497, 8789375, 8930753, 9073639, 9218041, 9363967,
Expand Down Expand Up @@ -331,7 +335,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
420810249, 431530241, 442453761, 453583369, 464921641, 476471169, 488234561,
500214441, 512413449, 524834241, 537479489, 550351881, 563454121, 576788929,
590359041, 604167209, 618216201, 632508801,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...208:*/
647047809, 661836041, 676876329, 692171521, 707724481, 723538089, 739615241,
755958849, 772571841, 789457161, 806617769, 824056641, 841776769, 859781161,
Expand All @@ -353,7 +357,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
1020274013, 1083597703, 1150027593, 1219676595, 1292660325, 1369097135,
1449108145, 1532817275, 1620351277, 1711839767, 1807415257, 1907213187,
2011371957, 2120032959,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...109:*/
2233340609U, 2351442379U, 2474488829U, 2602633639U, 2736033641U, 2874848851U,
3019242501U, 3169381071U, 3325434321U, 3487575323U, 3655980493U, 3830829623U,
Expand All @@ -367,7 +371,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
292164445, 340600625, 395555537, 457713341, 527810725, 606639529, 695049433,
793950709, 904317037, 1027188385, 1163673953, 1314955181, 1482288821,
1667010073, 1870535785, 2094367717,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...60:*/
2340095869U, 2609401873U, 2904062449U, 3225952925U, 3577050821U, 3959439497U,
#endif
Expand All @@ -377,15 +381,15 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
104692735, 139703809, 184327311, 240673265, 311207743, 398796225, 506750351,
638878193, 799538175, 993696769, 1226990095, 1505789553, 1837271615,
2229491905U,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...40:*/
2691463695U, 3233240945U, 3866006015U,
#endif
/*N=9, K=9...28:*/
265729, 598417, 1256465, 2485825, 4673345, 8405905, 14546705, 24331777,
39490049, 62390545, 96220561, 145198913, 214828609, 312193553, 446304145,
628496897, 872893441, 1196924561, 1621925137, 2173806145U,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...29:*/
2883810113U,
#endif
Expand All @@ -396,7 +400,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
/*N=11, K=11...19:*/
8097453, 18474633, 39753273, 81270333, 158819253, 298199265, 540279585,
948062325, 1616336765,
#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
/*...20:*/
2684641785U,
#endif
Expand All @@ -409,7 +413,7 @@ static const opus_uint32 CELT_PVQ_U_DATA[1272]={
1409933619
};

#if defined(CUSTOM_MODES)
#if defined(CWRS_EXTRA_ROWS)
static const opus_uint32 *const CELT_PVQ_U_ROW[15]={
CELT_PVQ_U_DATA+ 0,CELT_PVQ_U_DATA+ 208,CELT_PVQ_U_DATA+ 415,
CELT_PVQ_U_DATA+ 621,CELT_PVQ_U_DATA+ 826,CELT_PVQ_U_DATA+1030,
Expand All @@ -427,7 +431,7 @@ static const opus_uint32 *const CELT_PVQ_U_ROW[15]={
};
#endif

#if defined(CUSTOM_MODES) || defined(ENABLE_QEXT)
#if defined(CUSTOM_MODES)
void get_required_bits(opus_int16 *_bits,int _n,int _maxk,int _frac){
int k;
/*_maxk==0 => there's nothing to do.*/
Expand Down Expand Up @@ -666,7 +670,7 @@ static OPUS_INLINE opus_uint32 icwrs(int _n,int _k,opus_uint32 *_nc,const int *_
return i;
}

#if defined(CUSTOM_MODES) || defined(ENABLE_QEXT)
#if defined(CUSTOM_MODES)
void get_required_bits(opus_int16 *_bits,int _n,int _maxk,int _frac){
int k;
/*_maxk==0 => there's nothing to do.*/
Expand Down
6 changes: 5 additions & 1 deletion celt/modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error)
#endif /* !CUSTOM_MODES */
}

#ifdef CUSTOM_MODES
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
void opus_custom_mode_destroy(CELTMode *mode)
{
int arch = opus_select_arch();
Expand All @@ -456,6 +456,7 @@ void opus_custom_mode_destroy(CELTMode *mode)
}
}
#endif /* CUSTOM_MODES_ONLY */
#ifdef CUSTOM_MODES
#ifdef ENABLE_QEXT
if (mode->qext_cache.index) opus_free((opus_int16*)mode->qext_cache.index);
if (mode->qext_cache.bits) opus_free((unsigned char*)mode->qext_cache.bits);
Expand All @@ -473,6 +474,9 @@ void opus_custom_mode_destroy(CELTMode *mode)
clt_mdct_clear(&mode->mdct, arch);

opus_free((CELTMode *)mode);
#else
celt_assert(0);
#endif
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion celt/rate.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static const unsigned char LOG2_FRAC_TABLE[24]={
32,33,34,34,35,36,36,37,37
};

#if defined(CUSTOM_MODES) || defined(ENABLE_QEXT)
#if defined(CUSTOM_MODES)

/*Determines if V(N,K) fits in a 32-bit unsigned integer.
N and K are themselves limited to 15 bits.*/
Expand Down
Loading

0 comments on commit 67dd564

Please sign in to comment.