FFmpeg
 All Data Structures Namespaces 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],
326  TABLE_CONST float (*filter)[8][2], int len)
327 {
328  int i;
329  int N = 8;
330  LOCAL_ALIGNED_16(float, temp, [8], [2]);
331 
332  for (i = 0; i < len; i++, in++) {
333  dsp->hybrid_analysis(temp, in, (const float (*)[8][2]) filter, 1, N);
334  out[0][i][0] = temp[6][0];
335  out[0][i][1] = temp[6][1];
336  out[1][i][0] = temp[7][0];
337  out[1][i][1] = temp[7][1];
338  out[2][i][0] = temp[0][0];
339  out[2][i][1] = temp[0][1];
340  out[3][i][0] = temp[1][0];
341  out[3][i][1] = temp[1][1];
342  out[4][i][0] = temp[2][0] + temp[5][0];
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];
346  }
347 }
348 
349 static void hybrid4_8_12_cx(PSDSPContext *dsp,
350  float (*in)[2], float (*out)[32][2],
351  TABLE_CONST float (*filter)[8][2], int N, int len)
352 {
353  int i;
354 
355  for (i = 0; i < len; i++, in++) {
356  dsp->hybrid_analysis(out[0] + i, in, (const float (*)[8][2]) filter, 32, N);
357  }
358 }
359 
360 static void hybrid_analysis(PSDSPContext *dsp, float out[91][32][2],
361  float in[5][44][2], float L[2][38][64],
362  int is34, int len)
363 {
364  int i, j;
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];
369  }
370  }
371  if (is34) {
372  hybrid4_8_12_cx(dsp, in[0], out, f34_0_12, 12, len);
373  hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8, 8, len);
374  hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4, 4, len);
375  hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4, 4, len);
376  hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4, 4, len);
377  dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
378  } else {
379  hybrid6_cx(dsp, in[0], out, f20_0_8, len);
380  hybrid2_re(in[1], out+6, g1_Q2, len, 1);
381  hybrid2_re(in[2], out+8, g1_Q2, len, 0);
382  dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
383  }
384  //update in_buf
385  for (i = 0; i < 5; i++) {
386  memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
387  }
388 }
389 
390 static void hybrid_synthesis(PSDSPContext *dsp, float out[2][38][64],
391  float in[91][32][2], int is34, int len)
392 {
393  int i, n;
394  if (is34) {
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];
401  }
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];
405  }
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];
413  }
414  }
415  dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
416  } else {
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];
426  }
427  dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
428  }
429 }
431 /// All-pass filter decay slope
432 #define DECAY_SLOPE 0.05f
433 /// Number of frequency bands that can be addressed by the parameter index, b(k)
434 static const int NR_PAR_BANDS[] = { 20, 34 };
435 static const int NR_IPDOPD_BANDS[] = { 11, 17 };
436 /// Number of frequency bands that can be addressed by the sub subband index, k
437 static const int NR_BANDS[] = { 71, 91 };
438 /// Start frequency band for the all-pass filter decay slope
439 static const int DECAY_CUTOFF[] = { 10, 32 };
440 /// Number of all-pass filer bands
441 static const int NR_ALLPASS_BANDS[] = { 30, 50 };
442 /// First stereo band using the short one sample delay
443 static const int SHORT_DELAY_BAND[] = { 42, 62 };
444 
445 /** Table 8.46 */
446 static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
447 {
448  int b;
449  if (full)
450  b = 9;
451  else {
452  b = 4;
453  par_mapped[10] = 0;
454  }
455  for (; b >= 0; b--) {
456  par_mapped[2*b+1] = par_mapped[2*b] = par[b];
457  }
458 }
459 
460 static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
461 {
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];
473  if (full) {
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;
483  }
484 }
485 
486 static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC])
487 {
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;
494  par[ 6] = par[10];
495  par[ 7] = par[11];
496  par[ 8] = ( par[12] + par[13]) * 0.5f;
497  par[ 9] = ( par[14] + par[15]) * 0.5f;
498  par[10] = par[16];
499  par[11] = par[17];
500  par[12] = par[18];
501  par[13] = par[19];
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;
508 }
509 
510 static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
511 {
512  if (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];
531  } else {
532  par_mapped[16] = 0;
533  }
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];
550 }
551 
552 static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
553 {
554  if (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];
572  }
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];
590 }
591 
592 static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC])
593 {
594  par[33] = par[19];
595  par[32] = par[19];
596  par[31] = par[18];
597  par[30] = par[18];
598  par[29] = par[18];
599  par[28] = par[18];
600  par[27] = par[17];
601  par[26] = par[17];
602  par[25] = par[16];
603  par[24] = par[16];
604  par[23] = par[15];
605  par[22] = par[15];
606  par[21] = par[14];
607  par[20] = par[14];
608  par[19] = par[13];
609  par[18] = par[12];
610  par[17] = par[11];
611  par[16] = par[10];
612  par[15] = par[ 9];
613  par[14] = par[ 9];
614  par[13] = par[ 8];
615  par[12] = par[ 8];
616  par[11] = par[ 7];
617  par[10] = par[ 6];
618  par[ 9] = par[ 5];
619  par[ 8] = par[ 5];
620  par[ 7] = par[ 4];
621  par[ 6] = par[ 4];
622  par[ 5] = par[ 3];
623  par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
624  par[ 3] = par[ 2];
625  par[ 2] = par[ 1];
626  par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
627 }
628 
629 static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
630 {
631  LOCAL_ALIGNED_16(float, power, [34], [PS_QMF_TIME_SLOTS]);
632  LOCAL_ALIGNED_16(float, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
633  float *peak_decay_nrg = ps->peak_decay_nrg;
634  float *power_smooth = ps->power_smooth;
635  float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
636  float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
637  float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
638  const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
639  const float peak_decay_factor = 0.76592833836465f;
640  const float transient_impact = 1.5f;
641  const float a_smooth = 0.25f; ///< Smoothing coefficient
642  int i, k, m, n;
643  int n0 = 0, nL = 32;
644 
645  memset(power, 0, 34 * sizeof(*power));
646 
647  if (is34 != ps->is34bands_old) {
648  memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
649  memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
650  memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
651  memset(ps->delay, 0, sizeof(ps->delay));
652  memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
653  }
654 
655  for (k = 0; k < NR_BANDS[is34]; k++) {
656  int i = k_to_i[k];
657  ps->dsp.add_squares(power[i], s[k], nL - n0);
658  }
659 
660  //Transient detection
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];
664  float denom;
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;
671  }
672  }
673 
674  //Decorrelation and transient reduction
675  // PS_AP_LINKS - 1
676  // -----
677  // | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
678  //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
679  // | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
680  // m = 0
681  //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
682  for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
683  int b = k_to_i[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]));
687  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
688  for (m = 0; m < PS_AP_LINKS; m++) {
689  memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
690  }
691  ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
692  phi_fract[is34][k],
693  (const float (*)[2]) Q_fract_allpass[is34][k],
694  transient_gain[b], g_decay_slope, nL - n0);
695  }
696  for (; k < SHORT_DELAY_BAND[is34]; k++) {
697  int i = k_to_i[k];
698  memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
699  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
700  //H = delay 14
701  ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
702  transient_gain[i], nL - n0);
703  }
704  for (; k < NR_BANDS[is34]; k++) {
705  int i = k_to_i[k];
706  memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
707  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
708  //H = delay 1
709  ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
710  transient_gain[i], nL - n0);
711  }
712 }
713 
714 static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
715  int8_t (*par)[PS_MAX_NR_IIDICC],
716  int num_par, int num_env, int full)
717 {
718  int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
719  int e;
720  if (num_par == 20 || num_par == 11) {
721  for (e = 0; e < num_env; e++) {
722  map_idx_20_to_34(par_mapped[e], par[e], full);
723  }
724  } else if (num_par == 10 || num_par == 5) {
725  for (e = 0; e < num_env; e++) {
726  map_idx_10_to_34(par_mapped[e], par[e], full);
727  }
728  } else {
729  *p_par_mapped = par;
730  }
731 }
732 
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)
736 {
737  int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
738  int e;
739  if (num_par == 34 || num_par == 17) {
740  for (e = 0; e < num_env; e++) {
741  map_idx_34_to_20(par_mapped[e], par[e], full);
742  }
743  } else if (num_par == 10 || num_par == 5) {
744  for (e = 0; e < num_env; e++) {
745  map_idx_10_to_20(par_mapped[e], par[e], full);
746  }
747  } else {
748  *p_par_mapped = par;
749  }
750 }
751 
752 static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34)
753 {
754  int e, b, k;
755 
756  float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
757  float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
758  float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
759  float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
760  int8_t *opd_hist = ps->opd_hist;
761  int8_t *ipd_hist = ps->ipd_hist;
762  int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
763  int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
764  int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
765  int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
766  int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
767  int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
768  int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
769  int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
770  const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
771  TABLE_CONST float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
772 
773  //Remapping
774  if (ps->num_env_old) {
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]));
783  }
784 
785  if (is34) {
786  remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
787  remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
788  if (ps->enable_ipdopd) {
789  remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
790  remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
791  }
792  if (!ps->is34bands_old) {
793  map_val_20_to_34(H11[0][0]);
794  map_val_20_to_34(H11[1][0]);
795  map_val_20_to_34(H12[0][0]);
796  map_val_20_to_34(H12[1][0]);
797  map_val_20_to_34(H21[0][0]);
798  map_val_20_to_34(H21[1][0]);
799  map_val_20_to_34(H22[0][0]);
800  map_val_20_to_34(H22[1][0]);
801  ipdopd_reset(ipd_hist, opd_hist);
802  }
803  } else {
804  remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
805  remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
806  if (ps->enable_ipdopd) {
807  remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
808  remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
809  }
810  if (ps->is34bands_old) {
811  map_val_34_to_20(H11[0][0]);
812  map_val_34_to_20(H11[1][0]);
813  map_val_34_to_20(H12[0][0]);
814  map_val_34_to_20(H12[1][0]);
815  map_val_34_to_20(H21[0][0]);
816  map_val_34_to_20(H21[1][0]);
817  map_val_34_to_20(H22[0][0]);
818  map_val_34_to_20(H22[1][0]);
819  ipdopd_reset(ipd_hist, opd_hist);
820  }
821  }
822 
823  //Mixing
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];
831 
832  if (!PS_BASELINE && ps->enable_ipdopd && b < NR_IPDOPD_BANDS[is34]) {
833  //The spec say says to only run this smoother when enable_ipdopd
834  //is set but the reference decoder appears to run it constantly
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];
839  float opd_re = pd_re_smooth[opd_idx];
840  float opd_im = pd_im_smooth[opd_idx];
841  float ipd_re = pd_re_smooth[ipd_idx];
842  float ipd_im = pd_im_smooth[ipd_idx];
843  opd_hist[b] = opd_idx & 0x3F;
844  ipd_hist[b] = ipd_idx & 0x3F;
845 
846  ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
847  ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
848  h11i = h11 * opd_im;
849  h11 = h11 * opd_re;
850  h12i = h12 * ipd_adj_im;
851  h12 = h12 * ipd_adj_re;
852  h21i = h21 * opd_im;
853  h21 = h21 * opd_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;
860  }
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;
865  }
866  for (k = 0; k < NR_BANDS[is34]; k++) {
867  float h[2][4];
868  float h_step[2][4];
869  int start = ps->border_position[e];
870  int stop = ps->border_position[e+1];
871  float width = 1.f / (stop - start);
872  b = k_to_i[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];
877  if (!PS_BASELINE && ps->enable_ipdopd) {
878  //Is this necessary? ps_04_new seems unchanged
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];
884  } else {
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];
889  }
890  }
891  //Interpolation
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;
896  if (!PS_BASELINE && ps->enable_ipdopd) {
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;
901  }
903  l[k] + start + 1, r[k] + start + 1,
904  h, h_step, stop - start);
905  }
906  }
907 }
908 
909 int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
910 {
911  LOCAL_ALIGNED_16(float, Lbuf, [91], [32][2]);
912  LOCAL_ALIGNED_16(float, Rbuf, [91], [32][2]);
913  const int len = 32;
914  int is34 = ps->is34bands;
915 
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])
919  memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
920 
921  hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
922  decorrelation(ps, Rbuf, (const float (*)[32][2]) Lbuf, is34);
923  stereo_processing(ps, Lbuf, Rbuf, is34);
924  hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
925  hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
926 
927  return 0;
928 }
929 
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, \
934  size);
935 
936 #define PS_VLC_ROW(name) \
937  { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
938 
939 av_cold void ff_ps_init(void) {
940  // Syntax initialization
941  static const struct {
942  const void *ps_codes, *ps_bits;
943  const unsigned int table_size, elem_size;
944  } ps_tmp[] = {
955  };
956 
957  PS_INIT_VLC_STATIC(0, 1544);
958  PS_INIT_VLC_STATIC(1, 832);
959  PS_INIT_VLC_STATIC(2, 1024);
961  PS_INIT_VLC_STATIC(4, 544);
962  PS_INIT_VLC_STATIC(5, 544);
963  PS_INIT_VLC_STATIC(6, 512);
964  PS_INIT_VLC_STATIC(7, 512);
965  PS_INIT_VLC_STATIC(8, 512);
966  PS_INIT_VLC_STATIC(9, 512);
967 
968  ps_tableinit();
969 }
970 
972 {
973  ff_psdsp_init(&ps->dsp);
974 }