2 * MPEG-4 Parametric Stereo decoding functions
3 * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/common.h"
24 #include "libavutil/mathematics.h"
28 #include "aacps_tablegen.h"
29 #include "aacpsdata.c"
31 #define PS_BASELINE 0 ///< Operate in Baseline PS mode
32 ///< Baseline implies 10 or 20 stereo bands,
33 ///< mixing mode A, and no ipd/opd
35 #define numQMFSlots 32 //numTimeSlots * RATE
37 static const int8_t num_env_tab[2][4] = {
42 static const int8_t nr_iidicc_par_tab[] = {
43 10, 20, 34, 10, 20, 34,
46 static const int8_t nr_iidopd_par_tab[] = {
63 static const int huff_iid[] = {
70 static VLC vlc_ps[10];
72 #define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
74 * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \
75 * Inter-channel Phase Difference/Overall Phase Difference parameters from the \
78 * @param avctx contains the current codec context \
79 * @param gb pointer to the input bitstream \
80 * @param ps pointer to the Parametric Stereo context \
81 * @param PAR pointer to the parameter to be read \
82 * @param e envelope to decode \
83 * @param dt 1: time delta-coded, 0: frequency delta-coded \
85 static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
86 int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
88 int b, num = ps->nr_ ## PAR ## _par; \
89 VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
91 int e_prev = e ? e - 1 : ps->num_env_old - 1; \
92 e_prev = FFMAX(e_prev, 0); \
93 for (b = 0; b < num; b++) { \
94 int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
95 if (MASK) val &= MASK; \
102 for (b = 0; b < num; b++) { \
103 val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
104 if (MASK) val &= MASK; \
112 av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
116 READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
117 READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U)
118 READ_PAR_DATA(ipdopd, 0, 0x07, 0)
120 static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
123 int count = get_bits_count(gb);
128 ps->enable_ipdopd = get_bits1(gb);
129 if (ps->enable_ipdopd) {
130 for (e = 0; e < ps->num_env; e++) {
131 int dt = get_bits1(gb);
132 read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
134 read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
137 skip_bits1(gb); //reserved_ps
138 return get_bits_count(gb) - count;
141 static void ipdopd_reset(int8_t *opd_hist, int8_t *ipd_hist)
144 for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
150 int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
153 int bit_count_start = get_bits_count(gb_host);
156 GetBitContext gbc = *gb_host, *gb = &gbc;
158 header = get_bits1(gb);
159 if (header) { //enable_ps_header
160 ps->enable_iid = get_bits1(gb);
161 if (ps->enable_iid) {
162 int iid_mode = get_bits(gb, 3);
164 av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
168 ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
169 ps->iid_quant = iid_mode > 2;
170 ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
172 ps->enable_icc = get_bits1(gb);
173 if (ps->enable_icc) {
174 ps->icc_mode = get_bits(gb, 3);
175 if (ps->icc_mode > 5) {
176 av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
180 ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
182 ps->enable_ext = get_bits1(gb);
185 ps->frame_class = get_bits1(gb);
186 ps->num_env_old = ps->num_env;
187 ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)];
189 ps->border_position[0] = -1;
190 if (ps->frame_class) {
191 for (e = 1; e <= ps->num_env; e++)
192 ps->border_position[e] = get_bits(gb, 5);
194 for (e = 1; e <= ps->num_env; e++)
195 ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
197 if (ps->enable_iid) {
198 for (e = 0; e < ps->num_env; e++) {
199 int dt = get_bits1(gb);
200 if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
204 memset(ps->iid_par, 0, sizeof(ps->iid_par));
207 for (e = 0; e < ps->num_env; e++) {
208 int dt = get_bits1(gb);
209 if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
213 memset(ps->icc_par, 0, sizeof(ps->icc_par));
215 if (ps->enable_ext) {
216 int cnt = get_bits(gb, 4);
218 cnt += get_bits(gb, 8);
222 int ps_extension_id = get_bits(gb, 2);
223 cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
226 av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
232 ps->enable_ipdopd &= !PS_BASELINE;
235 if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
236 //Create a fake envelope
237 int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
238 if (source >= 0 && source != ps->num_env) {
239 if (ps->enable_iid) {
240 memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
242 if (ps->enable_icc) {
243 memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
245 if (ps->enable_ipdopd) {
246 memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
247 memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
251 ps->border_position[ps->num_env] = numQMFSlots - 1;
255 ps->is34bands_old = ps->is34bands;
256 if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
257 ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
258 (ps->enable_icc && ps->nr_icc_par == 34);
261 if (!ps->enable_ipdopd) {
262 memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
263 memset(ps->opd_par, 0, sizeof(ps->opd_par));
269 bits_consumed = get_bits_count(gb) - bit_count_start;
270 if (bits_consumed <= bits_left) {
271 skip_bits_long(gb_host, bits_consumed);
272 return bits_consumed;
274 av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
277 skip_bits_long(gb_host, bits_left);
278 memset(ps->iid_par, 0, sizeof(ps->iid_par));
279 memset(ps->icc_par, 0, sizeof(ps->icc_par));
280 memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
281 memset(ps->opd_par, 0, sizeof(ps->opd_par));
285 /** Split one subband into 2 subsubbands with a symmetric real filter.
286 * The filter must have its non-center even coefficients equal to zero. */
287 static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[7], int len, int reverse)
290 for (i = 0; i < len; i++, in++) {
291 float re_in = filter[6] * in[6][0]; //real inphase
292 float re_op = 0.0f; //real out of phase
293 float im_in = filter[6] * in[6][1]; //imag inphase
294 float im_op = 0.0f; //imag out of phase
295 for (j = 0; j < 6; j += 2) {
296 re_op += filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
297 im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
299 out[ reverse][i][0] = re_in + re_op;
300 out[ reverse][i][1] = im_in + im_op;
301 out[!reverse][i][0] = re_in - re_op;
302 out[!reverse][i][1] = im_in - im_op;
306 /** Split one subband into 6 subsubbands with a complex filter */
307 static void hybrid6_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int len)
313 for (i = 0; i < len; i++, in++) {
314 for (ssb = 0; ssb < N; ssb++) {
315 float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
316 for (j = 0; j < 6; j++) {
317 float in0_re = in[j][0];
318 float in0_im = in[j][1];
319 float in1_re = in[12-j][0];
320 float in1_im = in[12-j][1];
321 sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im);
322 sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re);
324 temp[ssb][0] = sum_re;
325 temp[ssb][1] = sum_im;
327 out[0][i][0] = temp[6][0];
328 out[0][i][1] = temp[6][1];
329 out[1][i][0] = temp[7][0];
330 out[1][i][1] = temp[7][1];
331 out[2][i][0] = temp[0][0];
332 out[2][i][1] = temp[0][1];
333 out[3][i][0] = temp[1][0];
334 out[3][i][1] = temp[1][1];
335 out[4][i][0] = temp[2][0] + temp[5][0];
336 out[4][i][1] = temp[2][1] + temp[5][1];
337 out[5][i][0] = temp[3][0] + temp[4][0];
338 out[5][i][1] = temp[3][1] + temp[4][1];
342 static void hybrid4_8_12_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int N, int len)
346 for (i = 0; i < len; i++, in++) {
347 for (ssb = 0; ssb < N; ssb++) {
348 float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
349 for (j = 0; j < 6; j++) {
350 float in0_re = in[j][0];
351 float in0_im = in[j][1];
352 float in1_re = in[12-j][0];
353 float in1_im = in[12-j][1];
354 sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im);
355 sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re);
357 out[ssb][i][0] = sum_re;
358 out[ssb][i][1] = sum_im;
363 static void hybrid_analysis(float out[91][32][2], float in[5][44][2], float L[2][38][64], int is34, int len)
366 for (i = 0; i < 5; i++) {
367 for (j = 0; j < 38; j++) {
368 in[i][j+6][0] = L[0][j][i];
369 in[i][j+6][1] = L[1][j][i];
373 hybrid4_8_12_cx(in[0], out, f34_0_12, 12, len);
374 hybrid4_8_12_cx(in[1], out+12, f34_1_8, 8, len);
375 hybrid4_8_12_cx(in[2], out+20, f34_2_4, 4, len);
376 hybrid4_8_12_cx(in[3], out+24, f34_2_4, 4, len);
377 hybrid4_8_12_cx(in[4], out+28, f34_2_4, 4, len);
378 for (i = 0; i < 59; i++) {
379 for (j = 0; j < len; j++) {
380 out[i+32][j][0] = L[0][j][i+5];
381 out[i+32][j][1] = L[1][j][i+5];
385 hybrid6_cx(in[0], out, f20_0_8, len);
386 hybrid2_re(in[1], out+6, g1_Q2, len, 1);
387 hybrid2_re(in[2], out+8, g1_Q2, len, 0);
388 for (i = 0; i < 61; i++) {
389 for (j = 0; j < len; j++) {
390 out[i+10][j][0] = L[0][j][i+3];
391 out[i+10][j][1] = L[1][j][i+3];
396 for (i = 0; i < 5; i++) {
397 memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
401 static void hybrid_synthesis(float out[2][38][64], float in[91][32][2], int is34, int len)
405 for (n = 0; n < len; n++) {
406 memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
407 memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
408 for (i = 0; i < 12; i++) {
409 out[0][n][0] += in[ i][n][0];
410 out[1][n][0] += in[ i][n][1];
412 for (i = 0; i < 8; i++) {
413 out[0][n][1] += in[12+i][n][0];
414 out[1][n][1] += in[12+i][n][1];
416 for (i = 0; i < 4; i++) {
417 out[0][n][2] += in[20+i][n][0];
418 out[1][n][2] += in[20+i][n][1];
419 out[0][n][3] += in[24+i][n][0];
420 out[1][n][3] += in[24+i][n][1];
421 out[0][n][4] += in[28+i][n][0];
422 out[1][n][4] += in[28+i][n][1];
425 for (i = 0; i < 59; i++) {
426 for (n = 0; n < len; n++) {
427 out[0][n][i+5] = in[i+32][n][0];
428 out[1][n][i+5] = in[i+32][n][1];
432 for (n = 0; n < len; n++) {
433 out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
434 in[3][n][0] + in[4][n][0] + in[5][n][0];
435 out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
436 in[3][n][1] + in[4][n][1] + in[5][n][1];
437 out[0][n][1] = in[6][n][0] + in[7][n][0];
438 out[1][n][1] = in[6][n][1] + in[7][n][1];
439 out[0][n][2] = in[8][n][0] + in[9][n][0];
440 out[1][n][2] = in[8][n][1] + in[9][n][1];
442 for (i = 0; i < 61; i++) {
443 for (n = 0; n < len; n++) {
444 out[0][n][i+3] = in[i+10][n][0];
445 out[1][n][i+3] = in[i+10][n][1];
451 /// All-pass filter decay slope
452 #define DECAY_SLOPE 0.05f
453 /// Number of frequency bands that can be addressed by the parameter index, b(k)
454 static const int NR_PAR_BANDS[] = { 20, 34 };
455 /// Number of frequency bands that can be addressed by the sub subband index, k
456 static const int NR_BANDS[] = { 71, 91 };
457 /// Start frequency band for the all-pass filter decay slope
458 static const int DECAY_CUTOFF[] = { 10, 32 };
459 /// Number of all-pass filer bands
460 static const int NR_ALLPASS_BANDS[] = { 30, 50 };
461 /// First stereo band using the short one sample delay
462 static const int SHORT_DELAY_BAND[] = { 42, 62 };
465 static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
474 for (; b >= 0; b--) {
475 par_mapped[2*b+1] = par_mapped[2*b] = par[b];
479 static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
481 par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3;
482 par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3;
483 par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3;
484 par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3;
485 par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2;
486 par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2;
487 par_mapped[ 6] = par[10];
488 par_mapped[ 7] = par[11];
489 par_mapped[ 8] = ( par[12] + par[13]) / 2;
490 par_mapped[ 9] = ( par[14] + par[15]) / 2;
491 par_mapped[10] = par[16];
493 par_mapped[11] = par[17];
494 par_mapped[12] = par[18];
495 par_mapped[13] = par[19];
496 par_mapped[14] = ( par[20] + par[21]) / 2;
497 par_mapped[15] = ( par[22] + par[23]) / 2;
498 par_mapped[16] = ( par[24] + par[25]) / 2;
499 par_mapped[17] = ( par[26] + par[27]) / 2;
500 par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4;
501 par_mapped[19] = ( par[32] + par[33]) / 2;
505 static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC])
507 par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f;
508 par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f;
509 par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f;
510 par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f;
511 par[ 4] = ( par[ 6] + par[ 7]) * 0.5f;
512 par[ 5] = ( par[ 8] + par[ 9]) * 0.5f;
515 par[ 8] = ( par[12] + par[13]) * 0.5f;
516 par[ 9] = ( par[14] + par[15]) * 0.5f;
521 par[14] = ( par[20] + par[21]) * 0.5f;
522 par[15] = ( par[22] + par[23]) * 0.5f;
523 par[16] = ( par[24] + par[25]) * 0.5f;
524 par[17] = ( par[26] + par[27]) * 0.5f;
525 par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f;
526 par[19] = ( par[32] + par[33]) * 0.5f;
529 static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
532 par_mapped[33] = par[9];
533 par_mapped[32] = par[9];
534 par_mapped[31] = par[9];
535 par_mapped[30] = par[9];
536 par_mapped[29] = par[9];
537 par_mapped[28] = par[9];
538 par_mapped[27] = par[8];
539 par_mapped[26] = par[8];
540 par_mapped[25] = par[8];
541 par_mapped[24] = par[8];
542 par_mapped[23] = par[7];
543 par_mapped[22] = par[7];
544 par_mapped[21] = par[7];
545 par_mapped[20] = par[7];
546 par_mapped[19] = par[6];
547 par_mapped[18] = par[6];
548 par_mapped[17] = par[5];
549 par_mapped[16] = par[5];
553 par_mapped[15] = par[4];
554 par_mapped[14] = par[4];
555 par_mapped[13] = par[4];
556 par_mapped[12] = par[4];
557 par_mapped[11] = par[3];
558 par_mapped[10] = par[3];
559 par_mapped[ 9] = par[2];
560 par_mapped[ 8] = par[2];
561 par_mapped[ 7] = par[2];
562 par_mapped[ 6] = par[2];
563 par_mapped[ 5] = par[1];
564 par_mapped[ 4] = par[1];
565 par_mapped[ 3] = par[1];
566 par_mapped[ 2] = par[0];
567 par_mapped[ 1] = par[0];
568 par_mapped[ 0] = par[0];
571 static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
574 par_mapped[33] = par[19];
575 par_mapped[32] = par[19];
576 par_mapped[31] = par[18];
577 par_mapped[30] = par[18];
578 par_mapped[29] = par[18];
579 par_mapped[28] = par[18];
580 par_mapped[27] = par[17];
581 par_mapped[26] = par[17];
582 par_mapped[25] = par[16];
583 par_mapped[24] = par[16];
584 par_mapped[23] = par[15];
585 par_mapped[22] = par[15];
586 par_mapped[21] = par[14];
587 par_mapped[20] = par[14];
588 par_mapped[19] = par[13];
589 par_mapped[18] = par[12];
590 par_mapped[17] = par[11];
592 par_mapped[16] = par[10];
593 par_mapped[15] = par[ 9];
594 par_mapped[14] = par[ 9];
595 par_mapped[13] = par[ 8];
596 par_mapped[12] = par[ 8];
597 par_mapped[11] = par[ 7];
598 par_mapped[10] = par[ 6];
599 par_mapped[ 9] = par[ 5];
600 par_mapped[ 8] = par[ 5];
601 par_mapped[ 7] = par[ 4];
602 par_mapped[ 6] = par[ 4];
603 par_mapped[ 5] = par[ 3];
604 par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
605 par_mapped[ 3] = par[ 2];
606 par_mapped[ 2] = par[ 1];
607 par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
608 par_mapped[ 0] = par[ 0];
611 static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC])
642 par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
645 par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
649 static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
651 float power[34][PS_QMF_TIME_SLOTS] = {{0}};
652 float transient_gain[34][PS_QMF_TIME_SLOTS];
653 float *peak_decay_nrg = ps->peak_decay_nrg;
654 float *power_smooth = ps->power_smooth;
655 float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
656 float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
657 float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
658 const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
659 const float peak_decay_factor = 0.76592833836465f;
660 const float transient_impact = 1.5f;
661 const float a_smooth = 0.25f; ///< Smoothing coefficient
664 static const int link_delay[] = { 3, 4, 5 };
665 static const float a[] = { 0.65143905753106f,
669 if (is34 != ps->is34bands_old) {
670 memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
671 memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
672 memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
673 memset(ps->delay, 0, sizeof(ps->delay));
674 memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
677 for (n = n0; n < nL; n++) {
678 for (k = 0; k < NR_BANDS[is34]; k++) {
680 power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1];
684 //Transient detection
685 for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
686 for (n = n0; n < nL; n++) {
687 float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
689 peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
690 power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
691 peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
692 denom = transient_impact * peak_decay_diff_smooth[i];
693 transient_gain[i][n] = (denom > power_smooth[i]) ?
694 power_smooth[i] / denom : 1.0f;
698 //Decorrelation and transient reduction
701 // | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
702 //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
703 // | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
705 //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
706 for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
708 float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
709 float ag[PS_AP_LINKS];
710 g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
711 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
712 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
713 for (m = 0; m < PS_AP_LINKS; m++) {
714 memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
715 ag[m] = a[m] * g_decay_slope;
717 for (n = n0; n < nL; n++) {
718 float in_re = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][0] -
719 delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][1];
720 float in_im = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][1] +
721 delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][0];
722 for (m = 0; m < PS_AP_LINKS; m++) {
723 float a_re = ag[m] * in_re;
724 float a_im = ag[m] * in_im;
725 float link_delay_re = ap_delay[k][m][n+5-link_delay[m]][0];
726 float link_delay_im = ap_delay[k][m][n+5-link_delay[m]][1];
727 float fractional_delay_re = Q_fract_allpass[is34][k][m][0];
728 float fractional_delay_im = Q_fract_allpass[is34][k][m][1];
729 ap_delay[k][m][n+5][0] = in_re;
730 ap_delay[k][m][n+5][1] = in_im;
731 in_re = link_delay_re * fractional_delay_re - link_delay_im * fractional_delay_im - a_re;
732 in_im = link_delay_re * fractional_delay_im + link_delay_im * fractional_delay_re - a_im;
733 ap_delay[k][m][n+5][0] += ag[m] * in_re;
734 ap_delay[k][m][n+5][1] += ag[m] * in_im;
736 out[k][n][0] = transient_gain[b][n] * in_re;
737 out[k][n][1] = transient_gain[b][n] * in_im;
740 for (; k < SHORT_DELAY_BAND[is34]; k++) {
741 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
742 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
743 for (n = n0; n < nL; n++) {
745 out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][0];
746 out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][1];
749 for (; k < NR_BANDS[is34]; k++) {
750 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
751 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
752 for (n = n0; n < nL; n++) {
754 out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][0];
755 out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][1];
760 static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
761 int8_t (*par)[PS_MAX_NR_IIDICC],
762 int num_par, int num_env, int full)
764 int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
766 if (num_par == 20 || num_par == 11) {
767 for (e = 0; e < num_env; e++) {
768 map_idx_20_to_34(par_mapped[e], par[e], full);
770 } else if (num_par == 10 || num_par == 5) {
771 for (e = 0; e < num_env; e++) {
772 map_idx_10_to_34(par_mapped[e], par[e], full);
779 static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
780 int8_t (*par)[PS_MAX_NR_IIDICC],
781 int num_par, int num_env, int full)
783 int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
785 if (num_par == 34 || num_par == 17) {
786 for (e = 0; e < num_env; e++) {
787 map_idx_34_to_20(par_mapped[e], par[e], full);
789 } else if (num_par == 10 || num_par == 5) {
790 for (e = 0; e < num_env; e++) {
791 map_idx_10_to_20(par_mapped[e], par[e], full);
798 static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34)
802 float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
803 float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
804 float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
805 float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
806 int8_t *opd_hist = ps->opd_hist;
807 int8_t *ipd_hist = ps->ipd_hist;
808 int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
809 int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
810 int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
811 int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
812 int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
813 int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
814 int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
815 int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
816 const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
817 const float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
820 if (ps->num_env_old) {
821 memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
822 memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
823 memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
824 memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
825 memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
826 memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
827 memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
828 memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
832 remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
833 remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
834 if (ps->enable_ipdopd) {
835 remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
836 remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
838 if (!ps->is34bands_old) {
839 map_val_20_to_34(H11[0][0]);
840 map_val_20_to_34(H11[1][0]);
841 map_val_20_to_34(H12[0][0]);
842 map_val_20_to_34(H12[1][0]);
843 map_val_20_to_34(H21[0][0]);
844 map_val_20_to_34(H21[1][0]);
845 map_val_20_to_34(H22[0][0]);
846 map_val_20_to_34(H22[1][0]);
847 ipdopd_reset(ipd_hist, opd_hist);
850 remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
851 remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
852 if (ps->enable_ipdopd) {
853 remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
854 remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
856 if (ps->is34bands_old) {
857 map_val_34_to_20(H11[0][0]);
858 map_val_34_to_20(H11[1][0]);
859 map_val_34_to_20(H12[0][0]);
860 map_val_34_to_20(H12[1][0]);
861 map_val_34_to_20(H21[0][0]);
862 map_val_34_to_20(H21[1][0]);
863 map_val_34_to_20(H22[0][0]);
864 map_val_34_to_20(H22[1][0]);
865 ipdopd_reset(ipd_hist, opd_hist);
870 for (e = 0; e < ps->num_env; e++) {
871 for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
872 float h11, h12, h21, h22;
873 h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
874 h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
875 h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
876 h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
877 if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) {
878 //The spec say says to only run this smoother when enable_ipdopd
879 //is set but the reference decoder appears to run it constantly
880 float h11i, h12i, h21i, h22i;
881 float ipd_adj_re, ipd_adj_im;
882 int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
883 int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
884 float opd_re = pd_re_smooth[opd_idx];
885 float opd_im = pd_im_smooth[opd_idx];
886 float ipd_re = pd_re_smooth[ipd_idx];
887 float ipd_im = pd_im_smooth[ipd_idx];
888 opd_hist[b] = opd_idx & 0x3F;
889 ipd_hist[b] = ipd_idx & 0x3F;
891 ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
892 ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
895 h12i = h12 * ipd_adj_im;
896 h12 = h12 * ipd_adj_re;
899 h22i = h22 * ipd_adj_im;
900 h22 = h22 * ipd_adj_re;
901 H11[1][e+1][b] = h11i;
902 H12[1][e+1][b] = h12i;
903 H21[1][e+1][b] = h21i;
904 H22[1][e+1][b] = h22i;
906 H11[0][e+1][b] = h11;
907 H12[0][e+1][b] = h12;
908 H21[0][e+1][b] = h21;
909 H22[0][e+1][b] = h22;
911 for (k = 0; k < NR_BANDS[is34]; k++) {
912 float h11r, h12r, h21r, h22r;
913 float h11i, h12i, h21i, h22i;
914 float h11r_step, h12r_step, h21r_step, h22r_step;
915 float h11i_step, h12i_step, h21i_step, h22i_step;
916 int start = ps->border_position[e];
917 int stop = ps->border_position[e+1];
918 float width = 1.f / (stop - start);
924 if (!PS_BASELINE && ps->enable_ipdopd) {
925 //Is this necessary? ps_04_new seems unchanged
926 if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
927 h11i = -H11[1][e][b];
928 h12i = -H12[1][e][b];
929 h21i = -H21[1][e][b];
930 h22i = -H22[1][e][b];
939 h11r_step = (H11[0][e+1][b] - h11r) * width;
940 h12r_step = (H12[0][e+1][b] - h12r) * width;
941 h21r_step = (H21[0][e+1][b] - h21r) * width;
942 h22r_step = (H22[0][e+1][b] - h22r) * width;
943 if (!PS_BASELINE && ps->enable_ipdopd) {
944 h11i_step = (H11[1][e+1][b] - h11i) * width;
945 h12i_step = (H12[1][e+1][b] - h12i) * width;
946 h21i_step = (H21[1][e+1][b] - h21i) * width;
947 h22i_step = (H22[1][e+1][b] - h22i) * width;
949 for (n = start + 1; n <= stop; n++) {
951 float l_re = l[k][n][0];
952 float l_im = l[k][n][1];
953 float r_re = r[k][n][0];
954 float r_im = r[k][n][1];
959 if (!PS_BASELINE && ps->enable_ipdopd) {
965 l[k][n][0] = h11r*l_re + h21r*r_re - h11i*l_im - h21i*r_im;
966 l[k][n][1] = h11r*l_im + h21r*r_im + h11i*l_re + h21i*r_re;
967 r[k][n][0] = h12r*l_re + h22r*r_re - h12i*l_im - h22i*r_im;
968 r[k][n][1] = h12r*l_im + h22r*r_im + h12i*l_re + h22i*r_re;
970 l[k][n][0] = h11r*l_re + h21r*r_re;
971 l[k][n][1] = h11r*l_im + h21r*r_im;
972 r[k][n][0] = h12r*l_re + h22r*r_re;
973 r[k][n][1] = h12r*l_im + h22r*r_im;
980 int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
982 float Lbuf[91][32][2];
983 float Rbuf[91][32][2];
985 int is34 = ps->is34bands;
987 top += NR_BANDS[is34] - 64;
988 memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
989 if (top < NR_ALLPASS_BANDS[is34])
990 memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
992 hybrid_analysis(Lbuf, ps->in_buf, L, is34, len);
993 decorrelation(ps, Rbuf, Lbuf, is34);
994 stereo_processing(ps, Lbuf, Rbuf, is34);
995 hybrid_synthesis(L, Lbuf, is34, len);
996 hybrid_synthesis(R, Rbuf, is34, len);
1001 #define PS_INIT_VLC_STATIC(num, size) \
1002 INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
1003 ps_tmp[num].ps_bits, 1, 1, \
1004 ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
1007 #define PS_VLC_ROW(name) \
1008 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
1010 av_cold void ff_ps_init(void) {
1011 // Syntax initialization
1012 static const struct {
1013 const void *ps_codes, *ps_bits;
1014 const unsigned int table_size, elem_size;
1016 PS_VLC_ROW(huff_iid_df1),
1017 PS_VLC_ROW(huff_iid_dt1),
1018 PS_VLC_ROW(huff_iid_df0),
1019 PS_VLC_ROW(huff_iid_dt0),
1020 PS_VLC_ROW(huff_icc_df),
1021 PS_VLC_ROW(huff_icc_dt),
1022 PS_VLC_ROW(huff_ipd_df),
1023 PS_VLC_ROW(huff_ipd_dt),
1024 PS_VLC_ROW(huff_opd_df),
1025 PS_VLC_ROW(huff_opd_dt),
1028 PS_INIT_VLC_STATIC(0, 1544);
1029 PS_INIT_VLC_STATIC(1, 832);
1030 PS_INIT_VLC_STATIC(2, 1024);
1031 PS_INIT_VLC_STATIC(3, 1036);
1032 PS_INIT_VLC_STATIC(4, 544);
1033 PS_INIT_VLC_STATIC(5, 544);
1034 PS_INIT_VLC_STATIC(6, 512);
1035 PS_INIT_VLC_STATIC(7, 512);
1036 PS_INIT_VLC_STATIC(8, 512);
1037 PS_INIT_VLC_STATIC(9, 512);
1042 av_cold void ff_ps_ctx_init(PSContext *ps)