45 #define LPC_FILTERORDER 10
49 #define ST_MEM_L_TBL 85
50 #define MEM_LF_TBL 147
51 #define STATE_SHORT_LEN_20MS 57
52 #define STATE_SHORT_LEN_30MS 58
54 #define BLOCKL_MAX 240
57 #define CB_HALFFILTERLEN 4
58 #define CB_FILTERLEN 8
60 #define ENH_NBLOCKS_TOT 8
62 #define ENH_BUFL (ENH_NBLOCKS_TOT)*ENH_BLOCKL
63 #define ENH_BUFL_FILTEROVERHEAD 3
64 #define BLOCKL_MAX 240
72 #define STATE_SHORT_LEN_30MS 58
73 #define STATE_SHORT_LEN_20MS 57
75 #define SPL_MUL_16_16(a, b) ((int32_t) (((int16_t)(a)) * ((int16_t)(b))))
76 #define SPL_MUL_16_16_RSFT(a, b, c) (SPL_MUL_16_16(a, b) >> (c))
166 for (j = 0; j < 48; j++)
201 for (j = 0; j < 56; j++)
274 for (k = 4; k < 6; k++) {
277 }
else if (
index[k] >= 108 &&
index[k] < 128) {
285 int i, j,
pos = 0, cb_pos = 0;
313 for (
int n = 0; n < 2; n++) {
314 for (
int m = 0; m < nb_vectors; m++) {
315 for (
int k = 0; k <
dim - 1; k++) {
318 if ((lsf[
i + 1] - lsf[
i]) < 319) {
319 if (lsf[
i + 1] < lsf[
i]) {
320 lsf[
i + 1] = lsf[
i] + 160;
321 lsf[
i] = lsf[
i + 1] - 160;
335 int16_t *in2, int16_t coef,
338 int invcoef = 16384 - coef,
i;
341 out[
i] = (coef * in1[
i] + invcoef * in2[
i] + 8192) >> 14;
344 static void lsf2lsp(int16_t *lsf, int16_t *lsp,
int order)
350 for (
i = 0;
i < order;
i++) {
351 freq = (lsf[
i] * 20861) >> 15;
358 k =
FFMIN(freq >> 8, 63);
374 f[1] = lsp[0] * -1024;
376 for (
i = 2, k = 2, l = 2;
i <= 5;
i++, k += 2) {
379 for (j =
i; j > 1; j--, l--) {
380 high =
f[l - 1] >> 16;
381 low = (
f[l - 1] - (high * (1 << 16))) >> 1;
383 tmp = ((high * lsp[k]) * 4) + (((low * lsp[k]) >> 15) * 4);
386 f[l] -= (unsigned)
tmp;
389 f[l] -= lsp[k] * (1 << 10);
406 for (
i = 5;
i > 0;
i--) {
407 f[0][
i] += (unsigned)
f[0][
i - 1];
408 f[1][
i] -= (unsigned)
f[1][
i - 1];
412 for (
i = 5;
i > 0;
i--) {
413 tmp =
f[0][6 -
i] + (unsigned)
f[1][6 -
i] + 4096;
414 a[6 -
i] =
tmp >> 13;
416 tmp =
f[0][6 -
i] - (unsigned)
f[1][6 -
i] + 4096;
417 a[5 +
i] =
tmp >> 13;
422 int16_t *lsf2,
int coef,
int length)
430 static void bw_expand(int16_t *
out,
const int16_t *in,
const int16_t *coef,
int length)
435 for (
i = 1;
i < length;
i++)
436 out[
i] = (coef[
i] * in[
i] + 16384) >> 15;
440 int16_t *lsfdeq, int16_t length,
444 int i,
pos, lp_length;
446 lsfdeq2 = lsfdeq + length;
447 lp_length = length + 1;
451 memcpy(syntdenum, lp, lp_length * 2);
455 for (
i = 1;
i < 6;
i++) {
459 memcpy(syntdenum +
pos, lp, lp_length * 2);
465 for (
i = 0;
i <
s->nsub;
i++) {
468 memcpy(syntdenum +
pos, lp, lp_length * 2);
475 memcpy(
s->lsfdeqold, lsfdeq2, length * 2);
477 memcpy(
s->lsfdeqold, lsfdeq, length * 2);
482 int16_t *
B, int16_t B_length,
487 for (
i = 0;
i < length;
i++) {
488 const int16_t *b_ptr = &
B[0];
489 const int16_t *x_ptr = &in_ptr[
i];
492 for (j = 0; j < B_length; j++)
493 o += b_ptr[j] * *x_ptr--;
495 o =
av_clip(o, -134217728, 134215679);
497 out_ptr[
i] = ((o + 2048) >> 12);
504 int coefficients_length,
509 for (
i = 0;
i < data_length;
i++) {
512 for (j = coefficients_length - 1; j > 0; j--) {
519 data_out[
i] = (
output + 2048) >> 12;
524 int16_t *synt_denum, int16_t *Out_fix,
529 int16_t *tmp1, *tmp2, *tmp3;
550 tmp2 = &idx[
len - 1];
553 for (k = 0; k <
len; k++) {
560 }
else if (ifm < 59) {
561 for (k = 0; k <
len; k++) {
569 for (k = 0; k <
len; k++) {
579 memset(&sampleVal[
len], 0,
len * 2);
591 tmp1 = &sampleAr[
len - 1];
592 tmp2 = &sampleAr[2 *
len - 1];
594 for (k = 0; k <
len; k++) {
595 (*tmp3) = (*tmp1) + (*tmp2);
611 int length,
int shift)
613 for (
int i = 0;
i < length;
i++)
618 const int16_t *
win,
int length,
621 for (
int i = 0;
i < length;
i++)
626 const int16_t *in2,
int length,
629 for (
int i = 0;
i < length;
i++)
637 int16_t ilow =
index - interpolation_length;
655 int16_t k, base_size;
658 int16_t tempbuff2[
SUBL + 5];
661 base_size = lMem - cbveclen + 1;
663 if (cbveclen ==
SUBL) {
664 base_size += cbveclen / 2;
668 if (
index < lMem - cbveclen + 1) {
671 k =
index + cbveclen;
673 memcpy(cbvec, mem + lMem - k, cbveclen * 2);
674 }
else if (
index < base_size) {
688 if (
index - base_size < lMem - cbveclen + 1) {
692 memIndTest = lMem - (
index - base_size + cbveclen);
710 lag = (cbveclen << 1) - 20 +
index - base_size - lMem - 1;
726 int16_t cbvec0[
SUBL];
727 int16_t cbvec1[
SUBL];
728 int16_t cbvec2[
SUBL];
747 for (j = 0; j < veclen; j++) {
752 decvector[j] = (
int)(a32 + 8192) >> 14;
758 int16_t* destPtr = dest;
759 int16_t* sourcePtr =
source;
762 for (j = 0; j < length; j++)
763 *destPtr-- = *sourcePtr++;
768 int16_t *decresidual,
771 int16_t meml_gotten, Nfor, Nback,
diff, start_pos;
772 int16_t subcount, subframe;
773 int16_t *reverseDecresidual =
s->enh_buf;
774 int16_t *memVec =
s->prevResidual;
791 memset(mem, 0, (int16_t) (
CB_MEML -
s->state_short_len) * 2);
792 memcpy(mem +
CB_MEML -
s->state_short_len, decresidual + start_pos,
s->state_short_len * 2);
800 meml_gotten =
s->state_short_len;
802 memset(mem, 0, (int16_t) (
CB_MEML - meml_gotten) * 2);
815 Nfor =
s->nsub - encbits->
start - 1;
823 for (subframe = 0; subframe < Nfor; subframe++) {
837 Nback = encbits->
start - 1;
841 meml_gotten =
SUBL * (
s->nsub + 1 - encbits->
start);
847 memset(mem, 0, (int16_t) (
CB_MEML - meml_gotten) * 2);
850 for (subframe = 0; subframe < Nback; subframe++) {
869 int i = 0, absolute = 0, maximum = 0;
871 if (vector ==
NULL || length <= 0) {
875 for (
i = 0;
i < length;
i++) {
876 absolute =
FFABS(vector[
i]);
877 if (absolute > maximum)
882 return FFMIN(maximum, INT16_MAX);
889 if (0xFFFF0000 & n) {
895 if (0x0000FF00 & (n >>
bits))
bits += 8;
896 if (0x000000F0 & (n >>
bits))
bits += 4;
897 if (0x0000000C & (n >>
bits))
bits += 2;
898 if (0x00000002 & (n >>
bits))
bits += 1;
899 if (0x00000001 & (n >>
bits))
bits += 1;
908 for (
int i = 0;
i < length;
i++)
909 sum += (v1[
i] * v2[
i]) >> scaling;
915 int16_t lag, int16_t blen, int16_t srange, int16_t
scale)
919 w16ptr = &
buffer[blen - srange - lag];
930 #define SPL_SHIFT_W32(x, c) (((c) >= 0) ? ((x) << (c)) : ((x) >> (-(c))))
951 static void do_plc(int16_t *plc_residual,
955 int16_t *decresidual,
961 int32_t cross, ener, cross_comp, ener_comp = 0;
962 int32_t measure, max_measure, energy;
963 int16_t
max, cross_square_max, cross_square;
964 int16_t j, lag, tmp1, tmp2, randlag;
971 int16_t max_perSquare;
972 int16_t scale1, scale2;
984 s->consPLICount += 1;
989 if (
s->prevPLI != 1) {
1001 s->prevScale = scale3;
1008 corrLen =
FFMIN(60,
s->block_samples - (inlag + 3));
1010 correlation(&cross, &ener,
s->prevResidual, lag,
s->block_samples, corrLen, scale3);
1016 for (j = inlag - 2; j <= inlag + 3; j++) {
1017 correlation(&cross_comp, &ener_comp,
s->prevResidual, j,
s->block_samples, corrLen, scale3);
1033 if (((shift_max << 1) + shift3) > ((
shift1 << 1) +
shift2)) {
1041 if ((measure >> tmp1) > (max_measure >> tmp2)) {
1044 cross_square_max = cross_square;
1059 tmp2W32 =
scale_dot_product(&
s->prevResidual[
s->block_samples - corrLen], &
s->prevResidual[
s->block_samples - corrLen], corrLen, scale3);
1061 if ((tmp2W32 > 0) && (ener_comp > 0)) {
1075 totscale = scale1 + scale2 - 1;
1087 max_perSquare =
s->per_square;
1095 if (
s->consPLICount *
s->block_samples > 320) {
1097 }
else if (
s->consPLICount *
s->block_samples > 640) {
1099 }
else if (
s->consPLICount *
s->block_samples > 960) {
1101 }
else if (
s->consPLICount *
s->block_samples > 1280) {
1111 if (max_perSquare > 7868) {
1113 }
else if (max_perSquare > 839) {
1116 while ((max_perSquare <
kPlcPerSqr[ind]) && (ind > 0)) {
1122 pitchfact =
FFMIN(tmpW32, 32767);
1137 for (
i = 0;
i <
s->block_samples;
i++) {
1140 randlag = 53 + (
s->seed & 63);
1145 randvec[
i] =
s->prevResidual[
s->block_samples + pick];
1147 randvec[
i] =
s->prevResidual[pick];
1154 plc_residual[
i] =
s->prevResidual[
s->block_samples + pick];
1156 plc_residual[
i] = plc_residual[pick];
1161 tot_gain = use_gain;
1162 }
else if (
i < 160) {
1169 plc_residual[
i] =
SPL_MUL_16_16_RSFT(tot_gain, (pitchfact * plc_residual[
i] + (32767 - pitchfact) * randvec[
i] + 16384) >> 15, 15);
1178 if (energy <
SPL_SHIFT_W32(
s->block_samples * 900, -
s->prevScale - 1)) {
1180 for (
i = 0;
i <
s->block_samples;
i++) {
1181 plc_residual[
i] = randvec[
i];
1190 s->per_square = max_perSquare;
1192 memcpy(plc_residual, decresidual,
s->block_samples * 2);
1194 s->consPLICount = 0;
1200 memcpy(
s->prevResidual, plc_residual,
s->block_samples * 2);
1206 int16_t subl, int16_t searchLen,
1212 int16_t cross_corr_scale, energy_scale;
1213 int16_t cross_corr_sg_mod, cross_corr_sg_mod_max;
1215 int16_t cross_corr_mod, energy_mod, enery_mod_max;
1217 int16_t *rp_beg, *rp_end;
1218 int16_t totscale, totscale_max;
1225 cross_corr_sg_mod_max = 0;
1226 enery_mod_max = INT16_MAX;
1227 totscale_max = -500;
1235 rp_end = ®ressor[subl];
1238 rp_beg = ®ressor[-1];
1239 rp_end = ®ressor[subl - 1];
1255 for (k = 0; k < searchLen; k++) {
1257 rp = ®ressor[
pos];
1261 if ((energy > 0) && (cross_corr > 0)) {
1263 cross_corr_scale =
norm_w32(cross_corr) - 16;
1264 cross_corr_mod = (int16_t)
SPL_SHIFT_W32(cross_corr, cross_corr_scale);
1265 energy_scale =
norm_w32(energy) - 16;
1269 cross_corr_sg_mod = (int16_t)
SPL_MUL_16_16_RSFT(cross_corr_mod, cross_corr_mod, 16);
1274 totscale = energy_scale - (cross_corr_scale * 2);
1279 scalediff = totscale - totscale_max;
1280 scalediff =
FFMIN(scalediff, 31);
1281 scalediff =
FFMAX(scalediff, -31);
1287 if (scalediff < 0) {
1288 new_crit = ((
int32_t) cross_corr_sg_mod * enery_mod_max) >> (-scalediff);
1289 max_crit = ((
int32_t) cross_corr_sg_mod_max * energy_mod);
1291 new_crit = ((
int32_t) cross_corr_sg_mod * enery_mod_max);
1292 max_crit = ((
int32_t) cross_corr_sg_mod_max * energy_mod) >> scalediff;
1298 if (new_crit > max_crit) {
1299 cross_corr_sg_mod_max = cross_corr_sg_mod;
1300 enery_mod_max = energy_mod;
1301 totscale_max = totscale;
1308 energy += (unsigned)
step * ((*rp_end * *rp_end - *rp_beg * *rp_beg) >>
shifts);
1317 static void hp_output(int16_t *signal,
const int16_t *ba, int16_t *y,
1318 int16_t *x, int16_t
len)
1322 for (
int i = 0;
i <
len;
i++) {
1346 if (
tmp > 268435455) {
1348 }
else if (
tmp < -268435456) {
1355 y[1] = (
tmp - (y[0] * (1 << 16))) >> 1;
1360 int *got_frame_ptr,
AVPacket *avpkt)
1362 const uint8_t *buf = avpkt->
data;
1371 frame->nb_samples =
s->block_samples;
1377 if (
s->frame.start < 1 ||
s->frame.start > 5)
1389 do_plc(
s->plc_residual,
s->plc_lpc, 0,
1393 memcpy(
s->decresidual,
s->plc_residual,
s->block_samples * 2);
1402 if (
s->mode == 20) {
1403 lag =
xcorr_coeff(&
s->decresidual[
s->block_samples-60], &
s->decresidual[
s->block_samples-80],
1415 memcpy(plc_data,
s->decresidual,
s->block_samples * 2);
1420 for (
i = 0;
i <
s->nsub;
i++) {
1430 memcpy(
frame->data[0], plc_data,
s->block_samples * 2);
1433 s->hpimemy,
s->hpimemx,
s->block_samples);
1455 s->mode = avctx->
bit_rate <= 14000 ? 30 : 20;
1464 if (
s->mode == 30) {
1465 s->block_samples = 240;
1471 s->block_samples = 160;