FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aacps.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 Parametric Stereo decoding functions
3  * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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.
11  *
12  * FFmpeg 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 #include "libavutil/common.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/mathematics.h"
26 #include "avcodec.h"
27 #include "get_bits.h"
28 #include "aacps.h"
29 #include "aacps_tablegen.h"
30 #include "aacpsdata.c"
31 
32 #define PS_BASELINE 0 ///< Operate in Baseline PS mode
33  ///< Baseline implies 10 or 20 stereo bands,
34  ///< mixing mode A, and no ipd/opd
35 
36 #define numQMFSlots 32 //numTimeSlots * RATE
37 
38 static const int8_t num_env_tab[2][4] = {
39  { 0, 1, 2, 4, },
40  { 1, 2, 3, 4, },
41 };
42 
43 static const int8_t nr_iidicc_par_tab[] = {
44  10, 20, 34, 10, 20, 34,
45 };
46 
47 static const int8_t nr_iidopd_par_tab[] = {
48  5, 11, 17, 5, 11, 17,
49 };
50 
51 enum {
62 };
63 
64 static const int huff_iid[] = {
69 };
70 
71 static VLC vlc_ps[10];
72 
73 #define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
74 /** \
75  * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \
76  * Inter-channel Phase Difference/Overall Phase Difference parameters from the \
77  * bitstream. \
78  * \
79  * @param avctx contains the current codec context \
80  * @param gb pointer to the input bitstream \
81  * @param ps pointer to the Parametric Stereo context \
82  * @param PAR pointer to the parameter to be read \
83  * @param e envelope to decode \
84  * @param dt 1: time delta-coded, 0: frequency delta-coded \
85  */ \
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) \
88 { \
89  int b, num = ps->nr_ ## PAR ## _par; \
90  VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
91  if (dt) { \
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; \
97  PAR[e][b] = val; \
98  if (ERR_CONDITION) \
99  goto err; \
100  } \
101  } else { \
102  int val = 0; \
103  for (b = 0; b < num; b++) { \
104  val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
105  if (MASK) val &= MASK; \
106  PAR[e][b] = val; \
107  if (ERR_CONDITION) \
108  goto err; \
109  } \
110  } \
111  return 0; \
112 err: \
113  av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
114  return -1; \
115 }
116 
117 READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
118 READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U)
119 READ_PAR_DATA(ipdopd, 0, 0x07, 0)
120 
121 static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
122 {
123  int e;
124  int count = get_bits_count(gb);
125 
126  if (ps_extension_id)
127  return 0;
128 
129  ps->enable_ipdopd = get_bits1(gb);
130  if (ps->enable_ipdopd) {
131  for (e = 0; e < ps->num_env; e++) {
132  int dt = get_bits1(gb);
133  read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
134  dt = get_bits1(gb);
135  read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
136  }
137  }
138  skip_bits1(gb); //reserved_ps
139  return get_bits_count(gb) - count;
140 }
141 
142 static void ipdopd_reset(int8_t *ipd_hist, int8_t *opd_hist)
143 {
144  int i;
145  for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
146  opd_hist[i] = 0;
147  ipd_hist[i] = 0;
148  }
149 }
150 
151 int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
152 {
153  int e;
154  int bit_count_start = get_bits_count(gb_host);
155  int header;
156  int bits_consumed;
157  GetBitContext gbc = *gb_host, *gb = &gbc;
158 
159  header = get_bits1(gb);
160  if (header) { //enable_ps_header
161  ps->enable_iid = get_bits1(gb);
162  if (ps->enable_iid) {
163  int iid_mode = get_bits(gb, 3);
164  if (iid_mode > 5) {
165  av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
166  iid_mode);
167  goto err;
168  }
169  ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
170  ps->iid_quant = iid_mode > 2;
171  ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
172  }
173  ps->enable_icc = get_bits1(gb);
174  if (ps->enable_icc) {
175  ps->icc_mode = get_bits(gb, 3);
176  if (ps->icc_mode > 5) {
177  av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
178  ps->icc_mode);
179  goto err;
180  }
182  }
183  ps->enable_ext = get_bits1(gb);
184  }
185 
186  ps->frame_class = get_bits1(gb);
187  ps->num_env_old = ps->num_env;
188  ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)];
189 
190  ps->border_position[0] = -1;
191  if (ps->frame_class) {
192  for (e = 1; e <= ps->num_env; e++)
193  ps->border_position[e] = get_bits(gb, 5);
194  } else
195  for (e = 1; e <= ps->num_env; e++)
196  ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
197 
198  if (ps->enable_iid) {
199  for (e = 0; e < ps->num_env; e++) {
200  int dt = get_bits1(gb);
201  if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
202  goto err;
203  }
204  } else
205  memset(ps->iid_par, 0, sizeof(ps->iid_par));
206 
207  if (ps->enable_icc)
208  for (e = 0; e < ps->num_env; e++) {
209  int dt = get_bits1(gb);
210  if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
211  goto err;
212  }
213  else
214  memset(ps->icc_par, 0, sizeof(ps->icc_par));
215 
216  if (ps->enable_ext) {
217  int cnt = get_bits(gb, 4);
218  if (cnt == 15) {
219  cnt += get_bits(gb, 8);
220  }
221  cnt *= 8;
222  while (cnt > 7) {
223  int ps_extension_id = get_bits(gb, 2);
224  cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
225  }
226  if (cnt < 0) {
227  av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
228  goto err;
229  }
230  skip_bits(gb, cnt);
231  }
232 
233  ps->enable_ipdopd &= !PS_BASELINE;
234 
235  //Fix up envelopes
236  if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
237  //Create a fake envelope
238  int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
239  int b;
240  if (source >= 0 && source != ps->num_env) {
241  if (ps->enable_iid) {
242  memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
243  }
244  if (ps->enable_icc) {
245  memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
246  }
247  if (ps->enable_ipdopd) {
248  memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
249  memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
250  }
251  }
252  if (ps->enable_iid){
253  for (b = 0; b < ps->nr_iid_par; b++) {
254  if (FFABS(ps->iid_par[ps->num_env][b]) > 7 + 8 * ps->iid_quant) {
255  av_log(avctx, AV_LOG_ERROR, "iid_par invalid\n");
256  goto err;
257  }
258  }
259  }
260  if (ps->enable_icc){
261  for (b = 0; b < ps->nr_iid_par; b++) {
262  if (ps->icc_par[ps->num_env][b] > 7U) {
263  av_log(avctx, AV_LOG_ERROR, "icc_par invalid\n");
264  goto err;
265  }
266  }
267  }
268  ps->num_env++;
269  ps->border_position[ps->num_env] = numQMFSlots - 1;
270  }
271 
272 
273  ps->is34bands_old = ps->is34bands;
274  if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
275  ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
276  (ps->enable_icc && ps->nr_icc_par == 34);
277 
278  //Baseline
279  if (!ps->enable_ipdopd) {
280  memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
281  memset(ps->opd_par, 0, sizeof(ps->opd_par));
282  }
283 
284  if (header)
285  ps->start = 1;
286 
287  bits_consumed = get_bits_count(gb) - bit_count_start;
288  if (bits_consumed <= bits_left) {
289  skip_bits_long(gb_host, bits_consumed);
290  return bits_consumed;
291  }
292  av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
293 err:
294  ps->start = 0;
295  skip_bits_long(gb_host, bits_left);
296  memset(ps->iid_par, 0, sizeof(ps->iid_par));
297  memset(ps->icc_par, 0, sizeof(ps->icc_par));
298  memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
299  memset(ps->opd_par, 0, sizeof(ps->opd_par));
300  return bits_left;
301 }
302 
303 /** Split one subband into 2 subsubbands with a symmetric real filter.
304  * The filter must have its non-center even coefficients equal to zero. */
305 static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[8], int len, int reverse)
306 {
307  int i, j;
308  for (i = 0; i < len; i++, in++) {
309  float re_in = filter[6] * in[6][0]; //real inphase
310  float re_op = 0.0f; //real out of phase
311  float im_in = filter[6] * in[6][1]; //imag inphase
312  float im_op = 0.0f; //imag out of phase
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]);
316  }
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;
321  }
322 }
323 
324 /** Split one subband into 6 subsubbands with a complex filter */
325 static void hybrid6_cx(PSDSPContext *dsp, float (*in)[2], float (*out)[32][2], const float (*filter)[8][2], int len)
326 {
327  int i;
328  int N = 8;
329  LOCAL_ALIGNED_16(float, temp, [8], [2]);
330 
331  for (i = 0; i < len; i++, in++) {
332  dsp->hybrid_analysis(temp, in, filter, 1, N);
333  out[0][i][0] = temp[6][0];
334  out[0][i][1] = temp[6][1];
335  out[1][i][0] = temp[7][0];
336  out[1][i][1] = temp[7][1];
337  out[2][i][0] = temp[0][0];
338  out[2][i][1] = temp[0][1];
339  out[3][i][0] = temp[1][0];
340  out[3][i][1] = temp[1][1];
341  out[4][i][0] = temp[2][0] + temp[5][0];
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];
345  }
346 }
347 
348 static void hybrid4_8_12_cx(PSDSPContext *dsp, float (*in)[2], float (*out)[32][2], const float (*filter)[8][2], int N, int len)
349 {
350  int i;
351 
352  for (i = 0; i < len; i++, in++) {
353  dsp->hybrid_analysis(out[0] + i, in, filter, 32, N);
354  }
355 }
356 
357 static void hybrid_analysis(PSDSPContext *dsp, float out[91][32][2],
358  float in[5][44][2], float L[2][38][64],
359  int is34, int len)
360 {
361  int i, j;
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];
366  }
367  }
368  if (is34) {
369  hybrid4_8_12_cx(dsp, in[0], out, f34_0_12, 12, len);
370  hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8, 8, len);
371  hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4, 4, len);
372  hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4, 4, len);
373  hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4, 4, len);
374  dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
375  } else {
376  hybrid6_cx(dsp, in[0], out, f20_0_8, len);
377  hybrid2_re(in[1], out+6, g1_Q2, len, 1);
378  hybrid2_re(in[2], out+8, g1_Q2, len, 0);
379  dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
380  }
381  //update in_buf
382  for (i = 0; i < 5; i++) {
383  memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
384  }
385 }
386 
387 static void hybrid_synthesis(PSDSPContext *dsp, float out[2][38][64],
388  float in[91][32][2], int is34, int len)
389 {
390  int i, n;
391  if (is34) {
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];
398  }
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];
402  }
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];
410  }
411  }
412  dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
413  } else {
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];
423  }
424  dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
425  }
426 }
427 
428 /// All-pass filter decay slope
429 #define DECAY_SLOPE 0.05f
430 /// Number of frequency bands that can be addressed by the parameter index, b(k)
431 static const int NR_PAR_BANDS[] = { 20, 34 };
432 /// Number of frequency bands that can be addressed by the sub subband index, k
433 static const int NR_BANDS[] = { 71, 91 };
434 /// Start frequency band for the all-pass filter decay slope
435 static const int DECAY_CUTOFF[] = { 10, 32 };
436 /// Number of all-pass filer bands
437 static const int NR_ALLPASS_BANDS[] = { 30, 50 };
438 /// First stereo band using the short one sample delay
439 static const int SHORT_DELAY_BAND[] = { 42, 62 };
440 
441 /** Table 8.46 */
442 static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
443 {
444  int b;
445  if (full)
446  b = 9;
447  else {
448  b = 4;
449  par_mapped[10] = 0;
450  }
451  for (; b >= 0; b--) {
452  par_mapped[2*b+1] = par_mapped[2*b] = par[b];
453  }
454 }
455 
456 static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
457 {
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];
469  if (full) {
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;
479  }
480 }
481 
482 static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC])
483 {
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;
490  par[ 6] = par[10];
491  par[ 7] = par[11];
492  par[ 8] = ( par[12] + par[13]) * 0.5f;
493  par[ 9] = ( par[14] + par[15]) * 0.5f;
494  par[10] = par[16];
495  par[11] = par[17];
496  par[12] = par[18];
497  par[13] = par[19];
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;
504 }
505 
506 static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
507 {
508  if (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];
527  } else {
528  par_mapped[16] = 0;
529  }
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];
546 }
547 
548 static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
549 {
550  if (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];
568  }
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];
586 }
587 
588 static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC])
589 {
590  par[33] = par[19];
591  par[32] = par[19];
592  par[31] = par[18];
593  par[30] = par[18];
594  par[29] = par[18];
595  par[28] = par[18];
596  par[27] = par[17];
597  par[26] = par[17];
598  par[25] = par[16];
599  par[24] = par[16];
600  par[23] = par[15];
601  par[22] = par[15];
602  par[21] = par[14];
603  par[20] = par[14];
604  par[19] = par[13];
605  par[18] = par[12];
606  par[17] = par[11];
607  par[16] = par[10];
608  par[15] = par[ 9];
609  par[14] = par[ 9];
610  par[13] = par[ 8];
611  par[12] = par[ 8];
612  par[11] = par[ 7];
613  par[10] = par[ 6];
614  par[ 9] = par[ 5];
615  par[ 8] = par[ 5];
616  par[ 7] = par[ 4];
617  par[ 6] = par[ 4];
618  par[ 5] = par[ 3];
619  par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
620  par[ 3] = par[ 2];
621  par[ 2] = par[ 1];
622  par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
623 }
624 
625 static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
626 {
627  LOCAL_ALIGNED_16(float, power, [34], [PS_QMF_TIME_SLOTS]);
628  LOCAL_ALIGNED_16(float, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
629  float *peak_decay_nrg = ps->peak_decay_nrg;
630  float *power_smooth = ps->power_smooth;
631  float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
632  float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
633  float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
634  const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
635  const float peak_decay_factor = 0.76592833836465f;
636  const float transient_impact = 1.5f;
637  const float a_smooth = 0.25f; ///< Smoothing coefficient
638  int i, k, m, n;
639  int n0 = 0, nL = 32;
640 
641  memset(power, 0, 34 * sizeof(*power));
642 
643  if (is34 != ps->is34bands_old) {
644  memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
645  memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
646  memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
647  memset(ps->delay, 0, sizeof(ps->delay));
648  memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
649  }
650 
651  for (k = 0; k < NR_BANDS[is34]; k++) {
652  int i = k_to_i[k];
653  ps->dsp.add_squares(power[i], s[k], nL - n0);
654  }
655 
656  //Transient detection
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];
660  float denom;
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;
667  }
668  }
669 
670  //Decorrelation and transient reduction
671  // PS_AP_LINKS - 1
672  // -----
673  // | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
674  //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
675  // | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
676  // m = 0
677  //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
678  for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
679  int b = k_to_i[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]));
683  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
684  for (m = 0; m < PS_AP_LINKS; m++) {
685  memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
686  }
687  ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
688  phi_fract[is34][k], Q_fract_allpass[is34][k],
689  transient_gain[b], g_decay_slope, nL - n0);
690  }
691  for (; k < SHORT_DELAY_BAND[is34]; k++) {
692  int i = k_to_i[k];
693  memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
694  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
695  //H = delay 14
696  ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
697  transient_gain[i], nL - n0);
698  }
699  for (; k < NR_BANDS[is34]; k++) {
700  int i = k_to_i[k];
701  memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
702  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
703  //H = delay 1
704  ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
705  transient_gain[i], nL - n0);
706  }
707 }
708 
709 static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
710  int8_t (*par)[PS_MAX_NR_IIDICC],
711  int num_par, int num_env, int full)
712 {
713  int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
714  int e;
715  if (num_par == 20 || num_par == 11) {
716  for (e = 0; e < num_env; e++) {
717  map_idx_20_to_34(par_mapped[e], par[e], full);
718  }
719  } else if (num_par == 10 || num_par == 5) {
720  for (e = 0; e < num_env; e++) {
721  map_idx_10_to_34(par_mapped[e], par[e], full);
722  }
723  } else {
724  *p_par_mapped = par;
725  }
726 }
727 
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)
731 {
732  int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
733  int e;
734  if (num_par == 34 || num_par == 17) {
735  for (e = 0; e < num_env; e++) {
736  map_idx_34_to_20(par_mapped[e], par[e], full);
737  }
738  } else if (num_par == 10 || num_par == 5) {
739  for (e = 0; e < num_env; e++) {
740  map_idx_10_to_20(par_mapped[e], par[e], full);
741  }
742  } else {
743  *p_par_mapped = par;
744  }
745 }
746 
747 static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34)
748 {
749  int e, b, k;
750 
751  float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
752  float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
753  float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
754  float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
755  int8_t *opd_hist = ps->opd_hist;
756  int8_t *ipd_hist = ps->ipd_hist;
757  int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
758  int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
759  int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
760  int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
761  int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
762  int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
763  int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
764  int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
765  const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
766  const float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
767 
768  //Remapping
769  if (ps->num_env_old) {
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]));
778  }
779 
780  if (is34) {
781  remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
782  remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
783  if (ps->enable_ipdopd) {
784  remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
785  remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
786  }
787  if (!ps->is34bands_old) {
788  map_val_20_to_34(H11[0][0]);
789  map_val_20_to_34(H11[1][0]);
790  map_val_20_to_34(H12[0][0]);
791  map_val_20_to_34(H12[1][0]);
792  map_val_20_to_34(H21[0][0]);
793  map_val_20_to_34(H21[1][0]);
794  map_val_20_to_34(H22[0][0]);
795  map_val_20_to_34(H22[1][0]);
796  ipdopd_reset(ipd_hist, opd_hist);
797  }
798  } else {
799  remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
800  remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
801  if (ps->enable_ipdopd) {
802  remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
803  remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
804  }
805  if (ps->is34bands_old) {
806  map_val_34_to_20(H11[0][0]);
807  map_val_34_to_20(H11[1][0]);
808  map_val_34_to_20(H12[0][0]);
809  map_val_34_to_20(H12[1][0]);
810  map_val_34_to_20(H21[0][0]);
811  map_val_34_to_20(H21[1][0]);
812  map_val_34_to_20(H22[0][0]);
813  map_val_34_to_20(H22[1][0]);
814  ipdopd_reset(ipd_hist, opd_hist);
815  }
816  }
817 
818  //Mixing
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];
826  if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) {
827  //The spec say says to only run this smoother when enable_ipdopd
828  //is set but the reference decoder appears to run it constantly
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];
833  float opd_re = pd_re_smooth[opd_idx];
834  float opd_im = pd_im_smooth[opd_idx];
835  float ipd_re = pd_re_smooth[ipd_idx];
836  float ipd_im = pd_im_smooth[ipd_idx];
837  opd_hist[b] = opd_idx & 0x3F;
838  ipd_hist[b] = ipd_idx & 0x3F;
839 
840  ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
841  ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
842  h11i = h11 * opd_im;
843  h11 = h11 * opd_re;
844  h12i = h12 * ipd_adj_im;
845  h12 = h12 * ipd_adj_re;
846  h21i = h21 * opd_im;
847  h21 = h21 * opd_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;
854  }
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;
859  }
860  for (k = 0; k < NR_BANDS[is34]; k++) {
861  float h[2][4];
862  float h_step[2][4];
863  int start = ps->border_position[e];
864  int stop = ps->border_position[e+1];
865  float width = 1.f / (stop - start);
866  b = k_to_i[k];
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];
871  if (!PS_BASELINE && ps->enable_ipdopd) {
872  //Is this necessary? ps_04_new seems unchanged
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];
878  } else {
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];
883  }
884  }
885  //Interpolation
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;
890  if (!PS_BASELINE && ps->enable_ipdopd) {
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;
895  }
897  l[k] + start + 1, r[k] + start + 1,
898  h, h_step, stop - start);
899  }
900  }
901 }
902 
903 int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
904 {
905  LOCAL_ALIGNED_16(float, Lbuf, [91], [32][2]);
906  LOCAL_ALIGNED_16(float, Rbuf, [91], [32][2]);
907  const int len = 32;
908  int is34 = ps->is34bands;
909 
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])
913  memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
914 
915  hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
916  decorrelation(ps, Rbuf, Lbuf, is34);
917  stereo_processing(ps, Lbuf, Rbuf, is34);
918  hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
919  hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
920 
921  return 0;
922 }
923 
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, \
928  size);
929 
930 #define PS_VLC_ROW(name) \
931  { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
932 
933 av_cold void ff_ps_init(void) {
934  // Syntax initialization
935  static const struct {
936  const void *ps_codes, *ps_bits;
937  const unsigned int table_size, elem_size;
938  } ps_tmp[] = {
949  };
950 
951  PS_INIT_VLC_STATIC(0, 1544);
952  PS_INIT_VLC_STATIC(1, 832);
953  PS_INIT_VLC_STATIC(2, 1024);
955  PS_INIT_VLC_STATIC(4, 544);
956  PS_INIT_VLC_STATIC(5, 544);
957  PS_INIT_VLC_STATIC(6, 512);
958  PS_INIT_VLC_STATIC(7, 512);
959  PS_INIT_VLC_STATIC(8, 512);
960  PS_INIT_VLC_STATIC(9, 512);
961 
962  ps_tableinit();
963 }
964 
966 {
967  ff_psdsp_init(&ps->dsp);
968 }