Skip to content

Commit 825c482

Browse files
authored
Merge branch 'xiph:main' into neon_float2int
2 parents f8b4bc2 + 5c13840 commit 825c482

13 files changed

+867
-133
lines changed

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -745,4 +745,11 @@ if(BUILD_TESTING AND NOT BUILD_SHARED_LIBS)
745745
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
746746
-P "${PROJECT_SOURCE_DIR}/cmake/RunTest.cmake")
747747
endif()
748+
if(OPUS_CUSTOM_MODES)
749+
add_executable(test_opus_custom ${test_opus_custom_sources})
750+
target_include_directories(test_opus_custom
751+
PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt dnn)
752+
target_link_libraries(test_opus_custom PRIVATE opus)
753+
target_compile_definitions(test_opus_custom PRIVATE OPUS_BUILD)
754+
endif()
748755
endif()

Makefile.am

+8
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ tests_test_opus_padding_LDADD = libopus.la $(NE10_LIBS) $(LIBM)
223223
tests_test_opus_dred_SOURCES = tests/test_opus_dred.c tests/test_opus_common.h
224224
tests_test_opus_dred_LDADD = libopus.la $(NE10_LIBS) $(LIBM)
225225

226+
if CUSTOM_MODES
227+
tests_test_opus_custom_SOURCES = tests/test_opus_custom.c tests/test_opus_common.h
228+
tests_test_opus_custom_LDADD = libopus.la $(NE10_LIBS) $(LIBM)
229+
endif
230+
226231
CELT_OBJ = $(CELT_SOURCES:.c=.lo)
227232
SILK_OBJ = $(SILK_SOURCES:.c=.lo)
228233
LPCNET_OBJ = $(LPCNET_SOURCES:.c=.lo)
@@ -289,6 +294,9 @@ if EXTRA_PROGRAMS
289294
noinst_PROGRAMS += opus_custom_demo
290295
opus_custom_demo_SOURCES = celt/opus_custom_demo.c
291296
opus_custom_demo_LDADD = libopus.la $(LIBM)
297+
298+
TESTS += tests/test_opus_custom
299+
noinst_PROGRAMS += tests/test_opus_custom
292300
endif
293301
endif
294302

celt/arch.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,14 @@ typedef opus_val32 celt_ener;
132132
#define Q15ONE 32767
133133

134134
#define SIG_SHIFT 12
135-
/* Safe saturation value for 32-bit signals. Should be less than
136-
2^31*(1-0.85) to avoid blowing up on DC at deemphasis.*/
137-
#define SIG_SAT (300000000)
135+
/* Safe saturation value for 32-bit signals. We need to make sure that we can
136+
add two sig values and that the first stages of the MDCT don't cause an overflow.
137+
The most constraining is the ARM_ASM comb filter where we shift left by one
138+
and then add two values. Because of that, we use 2^29-1. SIG_SAT must be large
139+
enough to fit a full-scale high-freq tone through the prefilter and comb filter,
140+
meaning 1.85*1.75*2^(15+SIG_SHIFT) = 434529895.
141+
so the limit should be about 2^31*sqrt(.5). */
142+
#define SIG_SAT (536870911)
138143

139144
#define NORM_SCALING 16384
140145

celt/bands.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *band
123123
} while (++j<eBands[i+1]<<LM);
124124
}
125125
/* We're adding one here to ensure the normalized band isn't larger than unity norm */
126-
bandE[i+c*m->nbEBands] = EPSILON+VSHR32(EXTEND32(celt_sqrt(sum)),-shift);
126+
bandE[i+c*m->nbEBands] = EPSILON+VSHR32(celt_sqrt(sum),-shift);
127127
} else {
128128
bandE[i+c*m->nbEBands] = EPSILON;
129129
}
@@ -267,7 +267,7 @@ void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X,
267267
/* This prevents energy collapse for transients with multiple short MDCTs */
268268
void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size,
269269
int start, int end, const opus_val16 *logE, const opus_val16 *prev1logE,
270-
const opus_val16 *prev2logE, const int *pulses, opus_uint32 seed, int arch)
270+
const opus_val16 *prev2logE, const int *pulses, opus_uint32 seed, int encode, int arch)
271271
{
272272
int c, i, j, k;
273273
for (i=start;i<end;i++)
@@ -310,7 +310,7 @@ void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_mas
310310
int renormalize=0;
311311
prev1 = prev1logE[c*m->nbEBands+i];
312312
prev2 = prev2logE[c*m->nbEBands+i];
313-
if (C==1)
313+
if (!encode && C==1)
314314
{
315315
prev1 = MAX16(prev1,prev1logE[m->nbEBands+i]);
316316
prev2 = MAX16(prev2,prev2logE[m->nbEBands+i]);

celt/bands.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void anti_collapse(const CELTMode *m, celt_norm *X_,
114114
unsigned char *collapse_masks, int LM, int C, int size, int start,
115115
int end, const opus_val16 *logE, const opus_val16 *prev1logE,
116116
const opus_val16 *prev2logE, const int *pulses, opus_uint32 seed,
117-
int arch);
117+
int encode, int arch);
118118

119119
opus_uint32 celt_lcg_rand(opus_uint32 seed);
120120

celt/celt_decoder.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ static void deemphasis_stereo_simple(celt_sig *in[], opus_val16 *pcm, int N, con
261261
{
262262
celt_sig tmp0, tmp1;
263263
/* Add VERY_SMALL to x[] first to reduce dependency chain. */
264-
tmp0 = x0[j] + VERY_SMALL + m0;
265-
tmp1 = x1[j] + VERY_SMALL + m1;
264+
tmp0 = SATURATE(x0[j] + VERY_SMALL + m0, SIG_SAT);
265+
tmp1 = SATURATE(x1[j] + VERY_SMALL + m1, SIG_SAT);
266266
m0 = MULT16_32_Q15(coef0, tmp0);
267267
m1 = MULT16_32_Q15(coef0, tmp1);
268268
pcm[2*j ] = SCALEOUT(SIG2WORD16(tmp0));
@@ -314,7 +314,7 @@ void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, c
314314
opus_val16 coef3 = coef[3];
315315
for (j=0;j<N;j++)
316316
{
317-
celt_sig tmp = x[j] + m + VERY_SMALL;
317+
celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
318318
m = MULT16_32_Q15(coef0, tmp)
319319
- MULT16_32_Q15(coef1, x[j]);
320320
tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2);
@@ -328,7 +328,7 @@ void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, c
328328
/* Shortcut for the standard (non-custom modes) case */
329329
for (j=0;j<N;j++)
330330
{
331-
celt_sig tmp = x[j] + VERY_SMALL + m;
331+
celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
332332
m = MULT16_32_Q15(coef0, tmp);
333333
scratch[j] = tmp;
334334
}
@@ -340,7 +340,7 @@ void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, c
340340
{
341341
for (j=0;j<N;j++)
342342
{
343-
celt_sig tmp = x[j] + m + VERY_SMALL;
343+
celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
344344
m = MULT16_32_Q15(coef0, tmp);
345345
y[j*C] = SAT16(ADD32(y[j*C], SCALEOUT(SIG2WORD16(tmp))));
346346
}
@@ -349,7 +349,7 @@ void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, c
349349
{
350350
for (j=0;j<N;j++)
351351
{
352-
celt_sig tmp = x[j] + VERY_SMALL + m;
352+
celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
353353
m = MULT16_32_Q15(coef0, tmp);
354354
y[j*C] = SCALEOUT(SIG2WORD16(tmp));
355355
}
@@ -641,10 +641,10 @@ static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, int N, int LM
641641
loss_duration = st->loss_duration;
642642
start = st->start;
643643
#ifdef ENABLE_DEEP_PLC
644-
noise_based = start != 0 || (lpcnet->fec_fill_pos == 0 && (st->skip_plc || loss_duration >= 80));
645-
#else
646-
noise_based = loss_duration >= 40 || start != 0 || st->skip_plc;
644+
if (lpcnet != NULL) noise_based = start != 0 || (lpcnet->fec_fill_pos == 0 && (st->skip_plc || loss_duration >= 80));
645+
else
647646
#endif
647+
noise_based = loss_duration >= 40 || start != 0 || st->skip_plc;
648648
if (noise_based)
649649
{
650650
/* Noise-based PLC/CNG */
@@ -710,7 +710,7 @@ static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, int N, int LM
710710
if (loss_duration == 0)
711711
{
712712
#ifdef ENABLE_DEEP_PLC
713-
if (lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C);
713+
if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C);
714714
#endif
715715
st->last_pitch_index = pitch_index = celt_plc_pitch_search(decode_mem, C, st->arch);
716716
} else {
@@ -903,7 +903,7 @@ static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, int N, int LM
903903
} while (++c<C);
904904

905905
#ifdef ENABLE_DEEP_PLC
906-
if (lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) {
906+
if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) {
907907
float overlap_mem;
908908
int samples_needed16k;
909909
celt_sig *buf;
@@ -1286,7 +1286,7 @@ int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char
12861286

12871287
if (anti_collapse_on)
12881288
anti_collapse(mode, X, collapse_masks, LM, C, N,
1289-
start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, st->arch);
1289+
start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch);
12901290

12911291
if (silence)
12921292
{

celt/celt_encoder.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,6 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm,
15721572

15731573
/* Can't produce more than 1275 output bytes */
15741574
nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1575-
nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
15761575

15771576
if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
15781577
{
@@ -1593,10 +1592,12 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm,
15931592
{
15941593
nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
15951594
(tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1596-
ec_enc_shrink(enc, nbCompressedBytes);
1595+
if (enc != NULL)
1596+
ec_enc_shrink(enc, nbCompressedBytes);
15971597
}
15981598
effectiveBytes = nbCompressedBytes - nbFilledBytes;
15991599
}
1600+
nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
16001601
equiv_rate = ((opus_int32)nbCompressedBytes*8*50 << (3-LM)) - (40*C+20)*((400>>LM) - 50);
16011602
if (st->bitrate != OPUS_BITRATE_MAX)
16021603
equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50));
@@ -1687,7 +1688,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm,
16871688
{
16881689
int enabled;
16891690
int qg;
1690-
enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && !st->disable_pf
1691+
enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && tell+16<=total_bits && !st->disable_pf
16911692
&& st->complexity >= 5;
16921693

16931694
prefilter_tapset = st->tapset_decision;
@@ -2006,6 +2007,8 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm,
20062007
/*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
20072008
}
20082009
ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
2010+
} else {
2011+
st->spread_decision = SPREAD_NORMAL;
20092012
}
20102013

20112014
/* For LFE, everything interesting is in the first band */
@@ -2272,7 +2275,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm,
22722275
if (anti_collapse_on)
22732276
{
22742277
anti_collapse(mode, X, collapse_masks, LM, C, N,
2275-
start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, st->arch);
2278+
start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 1, st->arch);
22762279
}
22772280

22782281
c=0; do {

0 commit comments

Comments
 (0)