Skip to content

Commit 8486ee9

Browse files
committed
Use a follower to better distribute extra bits
1 parent ae7f5a2 commit 8486ee9

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

celt/rate.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,46 @@ static int ec_dec_depth(ec_dec *dec, opus_int32 cap, opus_int32 *last) {
686686
return depth;
687687
}
688688

689+
#define MSWAP16(a,b) do {opus_val16 tmp = a;a=b;b=tmp;} while(0)
690+
static opus_val16 median_of_5_val16(const opus_val16 *x)
691+
{
692+
opus_val16 t0, t1, t2, t3, t4;
693+
t2 = x[2];
694+
if (x[0] > x[1])
695+
{
696+
t0 = x[1];
697+
t1 = x[0];
698+
} else {
699+
t0 = x[0];
700+
t1 = x[1];
701+
}
702+
if (x[3] > x[4])
703+
{
704+
t3 = x[4];
705+
t4 = x[3];
706+
} else {
707+
t3 = x[3];
708+
t4 = x[4];
709+
}
710+
if (t0 > t3)
711+
{
712+
MSWAP16(t0, t3);
713+
MSWAP16(t1, t4);
714+
}
715+
if (t2 > t1)
716+
{
717+
if (t1 < t3)
718+
return MIN16(t2, t3);
719+
else
720+
return MIN16(t4, t1);
721+
} else {
722+
if (t2 < t3)
723+
return MIN16(t1, t3);
724+
else
725+
return MIN16(t2, t4);
726+
}
727+
}
728+
689729
void clt_compute_extra_allocation(const CELTMode *m, const CELTMode *qext_mode, int start, int end, int qext_end, const celt_glog *bandLogE, const celt_glog *qext_bandLogE,
690730
opus_int32 total, int *extra_pulses, int *extra_equant, int C, int LM, ec_ctx *ec, int encode, opus_val16 tone_freq, opus_val32 toneishness)
691731
{
@@ -730,6 +770,7 @@ void clt_compute_extra_allocation(const CELTMode *m, const CELTMode *qext_mode,
730770
VARDECL(opus_val16, flatE);
731771
VARDECL(int, Ncoef);
732772
VARDECL(opus_val16, min);
773+
VARDECL(opus_val16, follower);
733774

734775
ALLOC(flatE, tot_bands, opus_val16);
735776
ALLOC(min, tot_bands, opus_val16);
@@ -765,6 +806,21 @@ void clt_compute_extra_allocation(const CELTMode *m, const CELTMode *qext_mode,
765806
flatE[end+i] = MAXG(flatE[end+i], PSHR32(qext_bandLogE[NB_QEXT_BANDS+i] - GCONST(0.0625f)*qext_mode->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(end+i+5)*(end+i+5), DB_SHIFT-10));
766807
}
767808
}
809+
}
810+
ALLOC(follower, tot_bands, opus_val16);
811+
for (i=start+2;i<tot_bands-2;i++) {
812+
follower[i] = median_of_5_val16(&flatE[i-2]);
813+
}
814+
follower[start] = follower[start+1] = follower[start+2];
815+
follower[tot_bands-1] = follower[tot_bands-2] = follower[tot_bands-3];
816+
for (i=start+1;i<tot_bands;i++) {
817+
follower[i] = MAX16(follower[i], follower[i-1]-QCONST16(1.f, 10));
818+
}
819+
for (i=tot_bands-2;i>=start;i--) {
820+
follower[i] = MAX16(follower[i], follower[i+1]-QCONST16(1.f, 10));
821+
}
822+
for (i=start;i<tot_bands;i++) flatE[i] -= MULT16_16_Q15(Q15ONE-PSHR32(toneishness, 14), follower[i]);
823+
if (qext_mode != NULL) {
768824
for (i=0;i<qext_end;i++) flatE[end+i] = flatE[end+i] + QCONST16(3.f, 10) + QCONST16(.2f, 10)*i;
769825
}
770826
/* Approximate fill level assuming all bands contribute fully. */

0 commit comments

Comments
 (0)