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;
331 for (i = 0; i <
len; i++,
in++) {
342 out[4][i][1] = temp[2][1] + temp[5][1];
343 out[5][i][0] = temp[3][0] + temp[4][0];
344 out[5][i][1] = temp[3][1] + temp[4][1];
352 for (i = 0; i <
len; i++,
in++) {
358 float in[5][44][2],
float L[2][38][64],
362 for (i = 0; i < 5; i++) {
363 for (j = 0; j < 38; j++) {
364 in[i][j+6][0] = L[0][j][i];
365 in[i][j+6][1] = L[1][j][i];
382 for (i = 0; i < 5; i++) {
383 memcpy(in[i], in[i]+32, 6 *
sizeof(in[i][0]));
388 float in[91][32][2],
int is34,
int len)
392 for (n = 0; n <
len; n++) {
393 memset(out[0][n], 0, 5*
sizeof(out[0][n][0]));
394 memset(out[1][n], 0, 5*
sizeof(out[1][n][0]));
395 for (i = 0; i < 12; i++) {
396 out[0][n][0] += in[ i][n][0];
397 out[1][n][0] += in[ i][n][1];
399 for (i = 0; i < 8; i++) {
400 out[0][n][1] += in[12+i][n][0];
401 out[1][n][1] += in[12+i][n][1];
403 for (i = 0; i < 4; i++) {
404 out[0][n][2] += in[20+i][n][0];
405 out[1][n][2] += in[20+i][n][1];
406 out[0][n][3] += in[24+i][n][0];
407 out[1][n][3] += in[24+i][n][1];
408 out[0][n][4] += in[28+i][n][0];
409 out[1][n][4] += in[28+i][n][1];
414 for (n = 0; n <
len; n++) {
415 out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
416 in[3][n][0] + in[4][n][0] + in[5][n][0];
417 out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
418 in[3][n][1] + in[4][n][1] + in[5][n][1];
419 out[0][n][1] = in[6][n][0] + in[7][n][0];
420 out[1][n][1] = in[6][n][1] + in[7][n][1];
421 out[0][n][2] = in[8][n][0] + in[9][n][0];
422 out[1][n][2] = in[8][n][1] + in[9][n][1];
429 #define DECAY_SLOPE 0.05f
433 static const int NR_BANDS[] = { 71, 91 };
442 static void map_idx_10_to_20(int8_t *par_mapped,
const int8_t *par,
int full)
451 for (; b >= 0; b--) {
452 par_mapped[2*b+1] = par_mapped[2*
b] = par[
b];
456 static void map_idx_34_to_20(int8_t *par_mapped,
const int8_t *par,
int full)
458 par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3;
459 par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3;
460 par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3;
461 par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3;
462 par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2;
463 par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2;
464 par_mapped[ 6] = par[10];
465 par_mapped[ 7] = par[11];
466 par_mapped[ 8] = ( par[12] + par[13]) / 2;
467 par_mapped[ 9] = ( par[14] + par[15]) / 2;
468 par_mapped[10] = par[16];
470 par_mapped[11] = par[17];
471 par_mapped[12] = par[18];
472 par_mapped[13] = par[19];
473 par_mapped[14] = ( par[20] + par[21]) / 2;
474 par_mapped[15] = ( par[22] + par[23]) / 2;
475 par_mapped[16] = ( par[24] + par[25]) / 2;
476 par_mapped[17] = ( par[26] + par[27]) / 2;
477 par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4;
478 par_mapped[19] = ( par[32] + par[33]) / 2;
484 par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f;
485 par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f;
486 par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f;
487 par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f;
488 par[ 4] = ( par[ 6] + par[ 7]) * 0.5f;
489 par[ 5] = ( par[ 8] + par[ 9]) * 0.5f;
492 par[ 8] = ( par[12] + par[13]) * 0.5f;
493 par[ 9] = ( par[14] + par[15]) * 0.5f;
498 par[14] = ( par[20] + par[21]) * 0.5f;
499 par[15] = ( par[22] + par[23]) * 0.5f;
500 par[16] = ( par[24] + par[25]) * 0.5f;
501 par[17] = ( par[26] + par[27]) * 0.5f;
502 par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f;
503 par[19] = ( par[32] + par[33]) * 0.5f;
506 static void map_idx_10_to_34(int8_t *par_mapped,
const int8_t *par,
int full)
509 par_mapped[33] = par[9];
510 par_mapped[32] = par[9];
511 par_mapped[31] = par[9];
512 par_mapped[30] = par[9];
513 par_mapped[29] = par[9];
514 par_mapped[28] = par[9];
515 par_mapped[27] = par[8];
516 par_mapped[26] = par[8];
517 par_mapped[25] = par[8];
518 par_mapped[24] = par[8];
519 par_mapped[23] = par[7];
520 par_mapped[22] = par[7];
521 par_mapped[21] = par[7];
522 par_mapped[20] = par[7];
523 par_mapped[19] = par[6];
524 par_mapped[18] = par[6];
525 par_mapped[17] = par[5];
526 par_mapped[16] = par[5];
530 par_mapped[15] = par[4];
531 par_mapped[14] = par[4];
532 par_mapped[13] = par[4];
533 par_mapped[12] = par[4];
534 par_mapped[11] = par[3];
535 par_mapped[10] = par[3];
536 par_mapped[ 9] = par[2];
537 par_mapped[ 8] = par[2];
538 par_mapped[ 7] = par[2];
539 par_mapped[ 6] = par[2];
540 par_mapped[ 5] = par[1];
541 par_mapped[ 4] = par[1];
542 par_mapped[ 3] = par[1];
543 par_mapped[ 2] = par[0];
544 par_mapped[ 1] = par[0];
545 par_mapped[ 0] = par[0];
548 static void map_idx_20_to_34(int8_t *par_mapped,
const int8_t *par,
int full)
551 par_mapped[33] = par[19];
552 par_mapped[32] = par[19];
553 par_mapped[31] = par[18];
554 par_mapped[30] = par[18];
555 par_mapped[29] = par[18];
556 par_mapped[28] = par[18];
557 par_mapped[27] = par[17];
558 par_mapped[26] = par[17];
559 par_mapped[25] = par[16];
560 par_mapped[24] = par[16];
561 par_mapped[23] = par[15];
562 par_mapped[22] = par[15];
563 par_mapped[21] = par[14];
564 par_mapped[20] = par[14];
565 par_mapped[19] = par[13];
566 par_mapped[18] = par[12];
567 par_mapped[17] = par[11];
569 par_mapped[16] = par[10];
570 par_mapped[15] = par[ 9];
571 par_mapped[14] = par[ 9];
572 par_mapped[13] = par[ 8];
573 par_mapped[12] = par[ 8];
574 par_mapped[11] = par[ 7];
575 par_mapped[10] = par[ 6];
576 par_mapped[ 9] = par[ 5];
577 par_mapped[ 8] = par[ 5];
578 par_mapped[ 7] = par[ 4];
579 par_mapped[ 6] = par[ 4];
580 par_mapped[ 5] = par[ 3];
581 par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
582 par_mapped[ 3] = par[ 2];
583 par_mapped[ 2] = par[ 1];
584 par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
585 par_mapped[ 0] = par[ 0];
619 par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
622 par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
635 const float peak_decay_factor = 0.76592833836465f;
636 const float transient_impact = 1.5f;
637 const float a_smooth = 0.25f;
641 memset(power, 0, 34 *
sizeof(*power));
651 for (k = 0; k < NR_BANDS[is34]; k++) {
657 for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
658 for (n = n0; n < nL; n++) {
659 float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
661 peak_decay_nrg[i] =
FFMAX(decayed_peak, power[i][n]);
662 power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
663 peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
664 denom = transient_impact * peak_decay_diff_smooth[i];
665 transient_gain[i][n] = (denom > power_smooth[i]) ?
666 power_smooth[i] / denom : 1.0f;
678 for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
680 float g_decay_slope = 1.f -
DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
681 g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
682 memcpy(delay[k], delay[k]+nL,
PS_MAX_DELAY*
sizeof(delay[k][0]));
685 memcpy(ap_delay[k][m], ap_delay[k][m]+
numQMFSlots, 5*
sizeof(ap_delay[k][m][0]));
689 transient_gain[b], g_decay_slope, nL - n0);
691 for (; k < SHORT_DELAY_BAND[is34]; k++) {
693 memcpy(delay[k], delay[k]+nL,
PS_MAX_DELAY*
sizeof(delay[k][0]));
697 transient_gain[i], nL - n0);
699 for (; k < NR_BANDS[is34]; k++) {
701 memcpy(delay[k], delay[k]+nL,
PS_MAX_DELAY*
sizeof(delay[k][0]));
705 transient_gain[i], nL - n0);
710 int8_t (*par)[PS_MAX_NR_IIDICC],
711 int num_par,
int num_env,
int full)
715 if (num_par == 20 || num_par == 11) {
716 for (e = 0; e < num_env; e++) {
719 }
else if (num_par == 10 || num_par == 5) {
720 for (e = 0; e < num_env; e++) {
728 static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
729 int8_t (*par)[PS_MAX_NR_IIDICC],
730 int num_par,
int num_env,
int full)
734 if (num_par == 34 || num_par == 17) {
735 for (e = 0; e < num_env; e++) {
738 }
else if (num_par == 10 || num_par == 5) {
739 for (e = 0; e < num_env; e++) {
770 memcpy(H11[0][0], H11[0][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H11[0][0][0]));
771 memcpy(H11[1][0], H11[1][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H11[1][0][0]));
772 memcpy(H12[0][0], H12[0][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H12[0][0][0]));
773 memcpy(H12[1][0], H12[1][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H12[1][0][0]));
774 memcpy(H21[0][0], H21[0][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H21[0][0][0]));
775 memcpy(H21[1][0], H21[1][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H21[1][0][0]));
776 memcpy(H22[0][0], H22[0][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H22[0][0][0]));
777 memcpy(H22[1][0], H22[1][ps->
num_env_old], PS_MAX_NR_IIDICC*
sizeof(H22[1][0][0]));
819 for (e = 0; e < ps->
num_env; e++) {
820 for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
821 float h11, h12, h21, h22;
822 h11 = H_LUT[iid_mapped[e][
b] + 7 + 23 * ps->
iid_quant][icc_mapped[e][
b]][0];
823 h12 = H_LUT[iid_mapped[e][
b] + 7 + 23 * ps->
iid_quant][icc_mapped[e][
b]][1];
824 h21 = H_LUT[iid_mapped[e][
b] + 7 + 23 * ps->
iid_quant][icc_mapped[e][
b]][2];
825 h22 = H_LUT[iid_mapped[e][
b] + 7 + 23 * ps->
iid_quant][icc_mapped[e][
b]][3];
829 float h11i, h12i, h21i, h22i;
830 float ipd_adj_re, ipd_adj_im;
831 int opd_idx = opd_hist[
b] * 8 + opd_mapped[e][
b];
832 int ipd_idx = ipd_hist[
b] * 8 + ipd_mapped[e][
b];
837 opd_hist[
b] = opd_idx & 0x3F;
838 ipd_hist[
b] = ipd_idx & 0x3F;
840 ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
841 ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
844 h12i = h12 * ipd_adj_im;
845 h12 = h12 * ipd_adj_re;
848 h22i = h22 * ipd_adj_im;
849 h22 = h22 * ipd_adj_re;
850 H11[1][e+1][
b] = h11i;
851 H12[1][e+1][
b] = h12i;
852 H21[1][e+1][
b] = h21i;
853 H22[1][e+1][
b] = h22i;
855 H11[0][e+1][
b] = h11;
856 H12[0][e+1][
b] = h12;
857 H21[0][e+1][
b] = h21;
858 H22[0][e+1][
b] = h22;
860 for (k = 0; k < NR_BANDS[is34]; k++) {
865 float width = 1.f / (stop - start);
867 h[0][0] = H11[0][e][
b];
868 h[0][1] = H12[0][e][
b];
869 h[0][2] = H21[0][e][
b];
870 h[0][3] = H22[0][e][
b];
873 if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
874 h[1][0] = -H11[1][e][
b];
875 h[1][1] = -H12[1][e][
b];
876 h[1][2] = -H21[1][e][
b];
877 h[1][3] = -H22[1][e][
b];
879 h[1][0] = H11[1][e][
b];
880 h[1][1] = H12[1][e][
b];
881 h[1][2] = H21[1][e][
b];
882 h[1][3] = H22[1][e][
b];
886 h_step[0][0] = (H11[0][e+1][
b] - h[0][0]) * width;
887 h_step[0][1] = (H12[0][e+1][
b] - h[0][1]) * width;
888 h_step[0][2] = (H21[0][e+1][
b] - h[0][2]) * width;
889 h_step[0][3] = (H22[0][e+1][
b] - h[0][3]) * width;
891 h_step[1][0] = (H11[1][e+1][
b] - h[1][0]) * width;
892 h_step[1][1] = (H12[1][e+1][
b] - h[1][1]) * width;
893 h_step[1][2] = (H21[1][e+1][
b] - h[1][2]) * width;
894 h_step[1][3] = (H22[1][e+1][
b] - h[1][3]) * width;
897 l[k] + start + 1,
r[k] + start + 1,
898 h, h_step, stop - start);
910 top += NR_BANDS[is34] - 64;
911 memset(ps->
delay+top, 0, (NR_BANDS[is34] - top)*
sizeof(ps->
delay[0]));
912 if (top < NR_ALLPASS_BANDS[is34])
924 #define PS_INIT_VLC_STATIC(num, size) \
925 INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
926 ps_tmp[num].ps_bits, 1, 1, \
927 ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
930 #define PS_VLC_ROW(name) \
931 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
935 static const struct {
936 const void *ps_codes, *ps_bits;
937 const unsigned int table_size, elem_size;