36 #define numQMFSlots 32 //numTimeSlots * RATE
44 10, 20, 34, 10, 20, 34,
73 #define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
86 static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
87 int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
89 int b, num = ps->nr_ ## PAR ## _par; \
90 VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
92 int e_prev = e ? e - 1 : ps->num_env_old - 1; \
93 e_prev = FFMAX(e_prev, 0); \
94 for (b = 0; b < num; b++) { \
95 int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
96 if (MASK) val &= MASK; \
103 for (b = 0; b < num; b++) { \
104 val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
105 if (MASK) val &= MASK; \
113 av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
130 if (ps->enable_ipdopd) {
131 for (e = 0; e < ps->num_env; e++) {
142 static void ipdopd_reset(int8_t *ipd_hist, int8_t *opd_hist)
192 for (e = 1; e <= ps->
num_env; e++)
195 for (e = 1; e <= ps->
num_env; e++)
199 for (e = 0; e < ps->
num_env; e++) {
208 for (e = 0; e < ps->
num_env; e++) {
223 int ps_extension_id =
get_bits(gb, 2);
240 if (source >= 0 && source != ps->
num_env) {
288 if (bits_consumed <= bits_left) {
290 return bits_consumed;
292 av_log(avctx,
AV_LOG_ERROR,
"Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
308 for (i = 0; i <
len; i++,
in++) {
309 float re_in = filter[6] *
in[6][0];
311 float im_in = filter[6] * in[6][1];
313 for (j = 0; j < 6; j += 2) {
314 re_op += filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
315 im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
317 out[ reverse][i][0] = re_in + re_op;
318 out[ reverse][i][1] = im_in + im_op;
319 out[!reverse][i][0] = re_in - re_op;
320 out[!reverse][i][1] = im_in - im_op;
332 for (i = 0; i <
len; i++,
in++) {
343 out[4][i][1] = temp[2][1] + temp[5][1];
344 out[5][i][0] = temp[3][0] + temp[4][0];
345 out[5][i][1] = temp[3][1] + temp[4][1];
350 float (*
in)[2],
float (*
out)[32][2],
355 for (i = 0; i <
len; i++,
in++) {
361 float in[5][44][2],
float L[2][38][64],
365 for (i = 0; i < 5; i++) {
366 for (j = 0; j < 38; j++) {
367 in[i][j+6][0] = L[0][j][i];
368 in[i][j+6][1] = L[1][j][i];
385 for (i = 0; i < 5; i++) {
386 memcpy(in[i], in[i]+32, 6 *
sizeof(in[i][0]));
391 float in[91][32][2],
int is34,
int len)
395 for (n = 0; n <
len; n++) {
396 memset(out[0][n], 0, 5*
sizeof(out[0][n][0]));
397 memset(out[1][n], 0, 5*
sizeof(out[1][n][0]));
398 for (i = 0; i < 12; i++) {
399 out[0][
n][0] += in[ i][
n][0];
400 out[1][
n][0] += in[ i][
n][1];
402 for (i = 0; i < 8; i++) {
403 out[0][
n][1] += in[12+i][
n][0];
404 out[1][
n][1] += in[12+i][
n][1];
406 for (i = 0; i < 4; i++) {
407 out[0][
n][2] += in[20+i][
n][0];
408 out[1][
n][2] += in[20+i][
n][1];
409 out[0][
n][3] += in[24+i][
n][0];
410 out[1][
n][3] += in[24+i][
n][1];
411 out[0][
n][4] += in[28+i][
n][0];
412 out[1][
n][4] += in[28+i][
n][1];
417 for (n = 0; n <
len; n++) {
418 out[0][
n][0] = in[0][
n][0] + in[1][
n][0] + in[2][
n][0] +
419 in[3][
n][0] + in[4][
n][0] + in[5][
n][0];
420 out[1][
n][0] = in[0][
n][1] + in[1][
n][1] + in[2][
n][1] +
421 in[3][
n][1] + in[4][
n][1] + in[5][
n][1];
422 out[0][
n][1] = in[6][
n][0] + in[7][
n][0];
423 out[1][
n][1] = in[6][
n][1] + in[7][
n][1];
424 out[0][
n][2] = in[8][
n][0] + in[9][
n][0];
425 out[1][
n][2] = in[8][
n][1] + in[9][
n][1];
432 #define DECAY_SLOPE 0.05f
437 static const int NR_BANDS[] = { 71, 91 };
446 static void map_idx_10_to_20(int8_t *par_mapped,
const int8_t *par,
int full)
455 for (; b >= 0; b--) {
456 par_mapped[2*b+1] = par_mapped[2*
b] = par[
b];
460 static void map_idx_34_to_20(int8_t *par_mapped,
const int8_t *par,
int full)
462 par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3;
463 par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3;
464 par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3;
465 par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3;
466 par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2;
467 par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2;
468 par_mapped[ 6] = par[10];
469 par_mapped[ 7] = par[11];
470 par_mapped[ 8] = ( par[12] + par[13]) / 2;
471 par_mapped[ 9] = ( par[14] + par[15]) / 2;
472 par_mapped[10] = par[16];
474 par_mapped[11] = par[17];
475 par_mapped[12] = par[18];
476 par_mapped[13] = par[19];
477 par_mapped[14] = ( par[20] + par[21]) / 2;
478 par_mapped[15] = ( par[22] + par[23]) / 2;
479 par_mapped[16] = ( par[24] + par[25]) / 2;
480 par_mapped[17] = ( par[26] + par[27]) / 2;
481 par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4;
482 par_mapped[19] = ( par[32] + par[33]) / 2;
488 par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f;
489 par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f;
490 par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f;
491 par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f;
492 par[ 4] = ( par[ 6] + par[ 7]) * 0.5f;
493 par[ 5] = ( par[ 8] + par[ 9]) * 0.5f;
496 par[ 8] = ( par[12] + par[13]) * 0.5f;
497 par[ 9] = ( par[14] + par[15]) * 0.5f;
502 par[14] = ( par[20] + par[21]) * 0.5f;
503 par[15] = ( par[22] + par[23]) * 0.5f;
504 par[16] = ( par[24] + par[25]) * 0.5f;
505 par[17] = ( par[26] + par[27]) * 0.5f;
506 par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f;
507 par[19] = ( par[32] + par[33]) * 0.5f;
510 static void map_idx_10_to_34(int8_t *par_mapped,
const int8_t *par,
int full)
513 par_mapped[33] = par[9];
514 par_mapped[32] = par[9];
515 par_mapped[31] = par[9];
516 par_mapped[30] = par[9];
517 par_mapped[29] = par[9];
518 par_mapped[28] = par[9];
519 par_mapped[27] = par[8];
520 par_mapped[26] = par[8];
521 par_mapped[25] = par[8];
522 par_mapped[24] = par[8];
523 par_mapped[23] = par[7];
524 par_mapped[22] = par[7];
525 par_mapped[21] = par[7];
526 par_mapped[20] = par[7];
527 par_mapped[19] = par[6];
528 par_mapped[18] = par[6];
529 par_mapped[17] = par[5];
530 par_mapped[16] = par[5];
534 par_mapped[15] = par[4];
535 par_mapped[14] = par[4];
536 par_mapped[13] = par[4];
537 par_mapped[12] = par[4];
538 par_mapped[11] = par[3];
539 par_mapped[10] = par[3];
540 par_mapped[ 9] = par[2];
541 par_mapped[ 8] = par[2];
542 par_mapped[ 7] = par[2];
543 par_mapped[ 6] = par[2];
544 par_mapped[ 5] = par[1];
545 par_mapped[ 4] = par[1];
546 par_mapped[ 3] = par[1];
547 par_mapped[ 2] = par[0];
548 par_mapped[ 1] = par[0];
549 par_mapped[ 0] = par[0];
552 static void map_idx_20_to_34(int8_t *par_mapped,
const int8_t *par,
int full)
555 par_mapped[33] = par[19];
556 par_mapped[32] = par[19];
557 par_mapped[31] = par[18];
558 par_mapped[30] = par[18];
559 par_mapped[29] = par[18];
560 par_mapped[28] = par[18];
561 par_mapped[27] = par[17];
562 par_mapped[26] = par[17];
563 par_mapped[25] = par[16];
564 par_mapped[24] = par[16];
565 par_mapped[23] = par[15];
566 par_mapped[22] = par[15];
567 par_mapped[21] = par[14];
568 par_mapped[20] = par[14];
569 par_mapped[19] = par[13];
570 par_mapped[18] = par[12];
571 par_mapped[17] = par[11];
573 par_mapped[16] = par[10];
574 par_mapped[15] = par[ 9];
575 par_mapped[14] = par[ 9];
576 par_mapped[13] = par[ 8];
577 par_mapped[12] = par[ 8];
578 par_mapped[11] = par[ 7];
579 par_mapped[10] = par[ 6];
580 par_mapped[ 9] = par[ 5];
581 par_mapped[ 8] = par[ 5];
582 par_mapped[ 7] = par[ 4];
583 par_mapped[ 6] = par[ 4];
584 par_mapped[ 5] = par[ 3];
585 par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
586 par_mapped[ 3] = par[ 2];
587 par_mapped[ 2] = par[ 1];
588 par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
589 par_mapped[ 0] = par[ 0];
623 par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
626 par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
639 const float peak_decay_factor = 0.76592833836465f;
640 const float transient_impact = 1.5f;
641 const float a_smooth = 0.25f;
645 memset(power, 0, 34 *
sizeof(*power));
655 for (k = 0; k < NR_BANDS[is34]; k++) {
661 for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
662 for (n = n0; n < nL; n++) {
663 float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
665 peak_decay_nrg[i] =
FFMAX(decayed_peak, power[i][n]);
666 power_smooth[i] += a_smooth * (power[i][
n] - power_smooth[i]);
667 peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][
n] - peak_decay_diff_smooth[i]);
668 denom = transient_impact * peak_decay_diff_smooth[i];
669 transient_gain[i][
n] = (denom > power_smooth[i]) ?
670 power_smooth[i] / denom : 1.0f;
682 for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
684 float g_decay_slope = 1.f -
DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
685 g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
686 memcpy(delay[k], delay[k]+nL,
PS_MAX_DELAY*
sizeof(delay[k][0]));
689 memcpy(ap_delay[k][m], ap_delay[k][m]+
numQMFSlots, 5*
sizeof(ap_delay[k][m][0]));
694 transient_gain[b], g_decay_slope, nL - n0);
696 for (; k < SHORT_DELAY_BAND[is34]; k++) {
698 memcpy(delay[k], delay[k]+nL,
PS_MAX_DELAY*
sizeof(delay[k][0]));
702 transient_gain[i], nL - n0);
704 for (; k < NR_BANDS[is34]; k++) {
706 memcpy(delay[k], delay[k]+nL,
PS_MAX_DELAY*
sizeof(delay[k][0]));
710 transient_gain[i], nL - n0);
715 int8_t (*par)[PS_MAX_NR_IIDICC],
716 int num_par,
int num_env,
int full)
720 if (num_par == 20 || num_par == 11) {
721 for (e = 0; e < num_env; e++) {
724 }
else if (num_par == 10 || num_par == 5) {
725 for (e = 0; e < num_env; e++) {
733 static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
734 int8_t (*par)[PS_MAX_NR_IIDICC],
735 int num_par,
int num_env,
int full)
739 if (num_par == 34 || num_par == 17) {
740 for (e = 0; e < num_env; e++) {
743 }
else if (num_par == 10 || num_par == 5) {
744 for (e = 0; e < num_env; e++) {
775 memcpy(H11[0][0], H11[0][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H11[0][0][0]));
776 memcpy(H11[1][0], H11[1][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H11[1][0][0]));
777 memcpy(H12[0][0], H12[0][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H12[0][0][0]));
778 memcpy(H12[1][0], H12[1][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H12[1][0][0]));
779 memcpy(H21[0][0], H21[0][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H21[0][0][0]));
780 memcpy(H21[1][0], H21[1][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H21[1][0][0]));
781 memcpy(H22[0][0], H22[0][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H22[0][0][0]));
782 memcpy(H22[1][0], H22[1][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H22[1][0][0]));
824 for (e = 0; e < ps->
num_env; e++) {
825 for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
826 float h11, h12, h21, h22;
827 h11 = H_LUT[iid_mapped[e][
b] + 7 + 23 * ps->
iid_quant][icc_mapped[e][
b]][0];
828 h12 = H_LUT[iid_mapped[e][
b] + 7 + 23 * ps->
iid_quant][icc_mapped[e][
b]][1];
829 h21 = H_LUT[iid_mapped[e][
b] + 7 + 23 * ps->
iid_quant][icc_mapped[e][
b]][2];
830 h22 = H_LUT[iid_mapped[e][
b] + 7 + 23 * ps->
iid_quant][icc_mapped[e][
b]][3];
835 float h11i, h12i, h21i, h22i;
836 float ipd_adj_re, ipd_adj_im;
837 int opd_idx = opd_hist[
b] * 8 + opd_mapped[e][
b];
838 int ipd_idx = ipd_hist[
b] * 8 + ipd_mapped[e][
b];
843 opd_hist[
b] = opd_idx & 0x3F;
844 ipd_hist[
b] = ipd_idx & 0x3F;
846 ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
847 ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
850 h12i = h12 * ipd_adj_im;
851 h12 = h12 * ipd_adj_re;
854 h22i = h22 * ipd_adj_im;
855 h22 = h22 * ipd_adj_re;
856 H11[1][e+1][
b] = h11i;
857 H12[1][e+1][
b] = h12i;
858 H21[1][e+1][
b] = h21i;
859 H22[1][e+1][
b] = h22i;
861 H11[0][e+1][
b] = h11;
862 H12[0][e+1][
b] = h12;
863 H21[0][e+1][
b] = h21;
864 H22[0][e+1][
b] = h22;
866 for (k = 0; k < NR_BANDS[is34]; k++) {
873 h[0][0] = H11[0][e][
b];
874 h[0][1] = H12[0][e][
b];
875 h[0][2] = H21[0][e][
b];
876 h[0][3] = H22[0][e][
b];
879 if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
880 h[1][0] = -H11[1][e][
b];
881 h[1][1] = -H12[1][e][
b];
882 h[1][2] = -H21[1][e][
b];
883 h[1][3] = -H22[1][e][
b];
885 h[1][0] = H11[1][e][
b];
886 h[1][1] = H12[1][e][
b];
887 h[1][2] = H21[1][e][
b];
888 h[1][3] = H22[1][e][
b];
892 h_step[0][0] = (H11[0][e+1][
b] - h[0][0]) * width;
893 h_step[0][1] = (H12[0][e+1][
b] - h[0][1]) * width;
894 h_step[0][2] = (H21[0][e+1][
b] - h[0][2]) * width;
895 h_step[0][3] = (H22[0][e+1][
b] - h[0][3]) * width;
897 h_step[1][0] = (H11[1][e+1][
b] - h[1][0]) * width;
898 h_step[1][1] = (H12[1][e+1][
b] - h[1][1]) * width;
899 h_step[1][2] = (H21[1][e+1][
b] - h[1][2]) * width;
900 h_step[1][3] = (H22[1][e+1][
b] - h[1][3]) * width;
903 l[k] + start + 1,
r[k] + start + 1,
904 h, h_step, stop -
start);
916 top += NR_BANDS[is34] - 64;
917 memset(ps->
delay+top, 0, (NR_BANDS[is34] - top)*
sizeof(ps->
delay[0]));
918 if (top < NR_ALLPASS_BANDS[is34])
922 decorrelation(ps, Rbuf, (
const float (*)[32][2]) Lbuf, is34);
930 #define PS_INIT_VLC_STATIC(num, size) \
931 INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
932 ps_tmp[num].ps_bits, 1, 1, \
933 ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
936 #define PS_VLC_ROW(name) \
937 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
941 static const struct {
942 const void *ps_codes, *ps_bits;
943 const unsigned int table_size, elem_size;