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  * Note: Rounding-to-nearest used unless otherwise stated
22  *
23  */
24 
25 #include <stdint.h>
26 #include "libavutil/common.h"
27 #include "libavutil/mathematics.h"
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "aacps.h"
31 #if USE_FIXED
32 #include "aacps_fixed_tablegen.h"
33 #else
34 #include "libavutil/internal.h"
35 #include "aacps_tablegen.h"
36 #endif /* USE_FIXED */
37 #include "aacpsdata.c"
38 
39 #define PS_BASELINE 0 ///< Operate in Baseline PS mode
40  ///< Baseline implies 10 or 20 stereo bands,
41  ///< mixing mode A, and no ipd/opd
42 
43 #define numQMFSlots 32 //numTimeSlots * RATE
44 
45 static const int8_t num_env_tab[2][4] = {
46  { 0, 1, 2, 4, },
47  { 1, 2, 3, 4, },
48 };
49 
50 static const int8_t nr_iidicc_par_tab[] = {
51  10, 20, 34, 10, 20, 34,
52 };
53 
54 static const int8_t nr_iidopd_par_tab[] = {
55  5, 11, 17, 5, 11, 17,
56 };
57 
58 enum {
69 };
70 
71 static const int huff_iid[] = {
76 };
77 
78 static VLC vlc_ps[10];
79 
80 #define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
81 /** \
82  * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \
83  * Inter-channel Phase Difference/Overall Phase Difference parameters from the \
84  * bitstream. \
85  * \
86  * @param avctx contains the current codec context \
87  * @param gb pointer to the input bitstream \
88  * @param ps pointer to the Parametric Stereo context \
89  * @param PAR pointer to the parameter to be read \
90  * @param e envelope to decode \
91  * @param dt 1: time delta-coded, 0: frequency delta-coded \
92  */ \
93 static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
94  int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
95 { \
96  int b, num = ps->nr_ ## PAR ## _par; \
97  VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
98  if (dt) { \
99  int e_prev = e ? e - 1 : ps->num_env_old - 1; \
100  e_prev = FFMAX(e_prev, 0); \
101  for (b = 0; b < num; b++) { \
102  int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
103  if (MASK) val &= MASK; \
104  PAR[e][b] = val; \
105  if (ERR_CONDITION) \
106  goto err; \
107  } \
108  } else { \
109  int val = 0; \
110  for (b = 0; b < num; b++) { \
111  val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
112  if (MASK) val &= MASK; \
113  PAR[e][b] = val; \
114  if (ERR_CONDITION) \
115  goto err; \
116  } \
117  } \
118  return 0; \
119 err: \
120  av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
121  return -1; \
122 }
123 
124 READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
125 READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U)
126 READ_PAR_DATA(ipdopd, 0, 0x07, 0)
127 
128 static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
129 {
130  int e;
131  int count = get_bits_count(gb);
132 
133  if (ps_extension_id)
134  return 0;
135 
136  ps->enable_ipdopd = get_bits1(gb);
137  if (ps->enable_ipdopd) {
138  for (e = 0; e < ps->num_env; e++) {
139  int dt = get_bits1(gb);
140  read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
141  dt = get_bits1(gb);
142  read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
143  }
144  }
145  skip_bits1(gb); //reserved_ps
146  return get_bits_count(gb) - count;
147 }
148 
149 static void ipdopd_reset(int8_t *ipd_hist, int8_t *opd_hist)
150 {
151  int i;
152  for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
153  opd_hist[i] = 0;
154  ipd_hist[i] = 0;
155  }
156 }
157 
158 int AAC_RENAME(ff_ps_read_data)(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
159 {
160  int e;
161  int bit_count_start = get_bits_count(gb_host);
162  int header;
163  int bits_consumed;
164  GetBitContext gbc = *gb_host, *gb = &gbc;
165 
166  header = get_bits1(gb);
167  if (header) { //enable_ps_header
168  ps->enable_iid = get_bits1(gb);
169  if (ps->enable_iid) {
170  int iid_mode = get_bits(gb, 3);
171  if (iid_mode > 5) {
172  av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
173  iid_mode);
174  goto err;
175  }
176  ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
177  ps->iid_quant = iid_mode > 2;
178  ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
179  }
180  ps->enable_icc = get_bits1(gb);
181  if (ps->enable_icc) {
182  ps->icc_mode = get_bits(gb, 3);
183  if (ps->icc_mode > 5) {
184  av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
185  ps->icc_mode);
186  goto err;
187  }
188  ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
189  }
190  ps->enable_ext = get_bits1(gb);
191  }
192 
193  ps->frame_class = get_bits1(gb);
194  ps->num_env_old = ps->num_env;
195  ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)];
196 
197  ps->border_position[0] = -1;
198  if (ps->frame_class) {
199  for (e = 1; e <= ps->num_env; e++)
200  ps->border_position[e] = get_bits(gb, 5);
201  } else
202  for (e = 1; e <= ps->num_env; e++)
203  ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
204 
205  if (ps->enable_iid) {
206  for (e = 0; e < ps->num_env; e++) {
207  int dt = get_bits1(gb);
208  if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
209  goto err;
210  }
211  } else
212  memset(ps->iid_par, 0, sizeof(ps->iid_par));
213 
214  if (ps->enable_icc)
215  for (e = 0; e < ps->num_env; e++) {
216  int dt = get_bits1(gb);
217  if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
218  goto err;
219  }
220  else
221  memset(ps->icc_par, 0, sizeof(ps->icc_par));
222 
223  if (ps->enable_ext) {
224  int cnt = get_bits(gb, 4);
225  if (cnt == 15) {
226  cnt += get_bits(gb, 8);
227  }
228  cnt *= 8;
229  while (cnt > 7) {
230  int ps_extension_id = get_bits(gb, 2);
231  cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
232  }
233  if (cnt < 0) {
234  av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
235  goto err;
236  }
237  skip_bits(gb, cnt);
238  }
239 
240  ps->enable_ipdopd &= !PS_BASELINE;
241 
242  //Fix up envelopes
243  if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
244  //Create a fake envelope
245  int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
246  int b;
247  if (source >= 0 && source != ps->num_env) {
248  if (ps->enable_iid) {
249  memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
250  }
251  if (ps->enable_icc) {
252  memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
253  }
254  if (ps->enable_ipdopd) {
255  memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
256  memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
257  }
258  }
259  if (ps->enable_iid){
260  for (b = 0; b < ps->nr_iid_par; b++) {
261  if (FFABS(ps->iid_par[ps->num_env][b]) > 7 + 8 * ps->iid_quant) {
262  av_log(avctx, AV_LOG_ERROR, "iid_par invalid\n");
263  goto err;
264  }
265  }
266  }
267  if (ps->enable_icc){
268  for (b = 0; b < ps->nr_iid_par; b++) {
269  if (ps->icc_par[ps->num_env][b] > 7U) {
270  av_log(avctx, AV_LOG_ERROR, "icc_par invalid\n");
271  goto err;
272  }
273  }
274  }
275  ps->num_env++;
276  ps->border_position[ps->num_env] = numQMFSlots - 1;
277  }
278 
279 
280  ps->is34bands_old = ps->is34bands;
281  if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
282  ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
283  (ps->enable_icc && ps->nr_icc_par == 34);
284 
285  //Baseline
286  if (!ps->enable_ipdopd) {
287  memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
288  memset(ps->opd_par, 0, sizeof(ps->opd_par));
289  }
290 
291  if (header)
292  ps->start = 1;
293 
294  bits_consumed = get_bits_count(gb) - bit_count_start;
295  if (bits_consumed <= bits_left) {
296  skip_bits_long(gb_host, bits_consumed);
297  return bits_consumed;
298  }
299  av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
300 err:
301  ps->start = 0;
302  skip_bits_long(gb_host, bits_left);
303  memset(ps->iid_par, 0, sizeof(ps->iid_par));
304  memset(ps->icc_par, 0, sizeof(ps->icc_par));
305  memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
306  memset(ps->opd_par, 0, sizeof(ps->opd_par));
307  return bits_left;
308 }
309 
310 /** Split one subband into 2 subsubbands with a symmetric real filter.
311  * The filter must have its non-center even coefficients equal to zero. */
312 static void hybrid2_re(INTFLOAT (*in)[2], INTFLOAT (*out)[32][2], const INTFLOAT filter[8], int len, int reverse)
313 {
314  int i, j;
315  for (i = 0; i < len; i++, in++) {
316  INT64FLOAT re_in = AAC_MUL31(filter[6], in[6][0]); //real inphase
317  INT64FLOAT re_op = 0.0f; //real out of phase
318  INT64FLOAT im_in = AAC_MUL31(filter[6], in[6][1]); //imag inphase
319  INT64FLOAT im_op = 0.0f; //imag out of phase
320  for (j = 0; j < 6; j += 2) {
321  re_op += (INT64FLOAT)filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
322  im_op += (INT64FLOAT)filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
323  }
324 
325 #if USE_FIXED
326  re_op = (re_op + 0x40000000) >> 31;
327  im_op = (im_op + 0x40000000) >> 31;
328 #endif /* USE_FIXED */
329 
330  out[ reverse][i][0] = (INTFLOAT)(re_in + re_op);
331  out[ reverse][i][1] = (INTFLOAT)(im_in + im_op);
332  out[!reverse][i][0] = (INTFLOAT)(re_in - re_op);
333  out[!reverse][i][1] = (INTFLOAT)(im_in - im_op);
334  }
335 }
336 
337 /** Split one subband into 6 subsubbands with a complex filter */
338 static void hybrid6_cx(PSDSPContext *dsp, INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
339  TABLE_CONST INTFLOAT (*filter)[8][2], int len)
340 {
341  int i;
342  int N = 8;
343  LOCAL_ALIGNED_16(INTFLOAT, temp, [8], [2]);
344 
345  for (i = 0; i < len; i++, in++) {
346  dsp->hybrid_analysis(temp, in, (const INTFLOAT (*)[8][2]) filter, 1, N);
347  out[0][i][0] = temp[6][0];
348  out[0][i][1] = temp[6][1];
349  out[1][i][0] = temp[7][0];
350  out[1][i][1] = temp[7][1];
351  out[2][i][0] = temp[0][0];
352  out[2][i][1] = temp[0][1];
353  out[3][i][0] = temp[1][0];
354  out[3][i][1] = temp[1][1];
355  out[4][i][0] = temp[2][0] + temp[5][0];
356  out[4][i][1] = temp[2][1] + temp[5][1];
357  out[5][i][0] = temp[3][0] + temp[4][0];
358  out[5][i][1] = temp[3][1] + temp[4][1];
359  }
360 }
361 
362 static void hybrid4_8_12_cx(PSDSPContext *dsp,
363  INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
364  TABLE_CONST INTFLOAT (*filter)[8][2], int N, int len)
365 {
366  int i;
367 
368  for (i = 0; i < len; i++, in++) {
369  dsp->hybrid_analysis(out[0] + i, in, (const INTFLOAT (*)[8][2]) filter, 32, N);
370  }
371 }
372 
373 static void hybrid_analysis(PSDSPContext *dsp, INTFLOAT out[91][32][2],
374  INTFLOAT in[5][44][2], INTFLOAT L[2][38][64],
375  int is34, int len)
376 {
377  int i, j;
378  for (i = 0; i < 5; i++) {
379  for (j = 0; j < 38; j++) {
380  in[i][j+6][0] = L[0][j][i];
381  in[i][j+6][1] = L[1][j][i];
382  }
383  }
384  if (is34) {
385  hybrid4_8_12_cx(dsp, in[0], out, f34_0_12, 12, len);
386  hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8, 8, len);
387  hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4, 4, len);
388  hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4, 4, len);
389  hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4, 4, len);
390  dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
391  } else {
392  hybrid6_cx(dsp, in[0], out, f20_0_8, len);
393  hybrid2_re(in[1], out+6, g1_Q2, len, 1);
394  hybrid2_re(in[2], out+8, g1_Q2, len, 0);
395  dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
396  }
397  //update in_buf
398  for (i = 0; i < 5; i++) {
399  memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
400  }
401 }
402 
403 static void hybrid_synthesis(PSDSPContext *dsp, INTFLOAT out[2][38][64],
404  INTFLOAT in[91][32][2], int is34, int len)
405 {
406  int i, n;
407  if (is34) {
408  for (n = 0; n < len; n++) {
409  memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
410  memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
411  for (i = 0; i < 12; i++) {
412  out[0][n][0] += in[ i][n][0];
413  out[1][n][0] += in[ i][n][1];
414  }
415  for (i = 0; i < 8; i++) {
416  out[0][n][1] += in[12+i][n][0];
417  out[1][n][1] += in[12+i][n][1];
418  }
419  for (i = 0; i < 4; i++) {
420  out[0][n][2] += in[20+i][n][0];
421  out[1][n][2] += in[20+i][n][1];
422  out[0][n][3] += in[24+i][n][0];
423  out[1][n][3] += in[24+i][n][1];
424  out[0][n][4] += in[28+i][n][0];
425  out[1][n][4] += in[28+i][n][1];
426  }
427  }
428  dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
429  } else {
430  for (n = 0; n < len; n++) {
431  out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
432  in[3][n][0] + in[4][n][0] + in[5][n][0];
433  out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
434  in[3][n][1] + in[4][n][1] + in[5][n][1];
435  out[0][n][1] = in[6][n][0] + in[7][n][0];
436  out[1][n][1] = in[6][n][1] + in[7][n][1];
437  out[0][n][2] = in[8][n][0] + in[9][n][0];
438  out[1][n][2] = in[8][n][1] + in[9][n][1];
439  }
440  dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
441  }
442 }
443 
444 /// All-pass filter decay slope
445 #define DECAY_SLOPE Q30(0.05f)
446 /// Number of frequency bands that can be addressed by the parameter index, b(k)
447 static const int NR_PAR_BANDS[] = { 20, 34 };
448 static const int NR_IPDOPD_BANDS[] = { 11, 17 };
449 /// Number of frequency bands that can be addressed by the sub subband index, k
450 static const int NR_BANDS[] = { 71, 91 };
451 /// Start frequency band for the all-pass filter decay slope
452 static const int DECAY_CUTOFF[] = { 10, 32 };
453 /// Number of all-pass filer bands
454 static const int NR_ALLPASS_BANDS[] = { 30, 50 };
455 /// First stereo band using the short one sample delay
456 static const int SHORT_DELAY_BAND[] = { 42, 62 };
457 
458 /** Table 8.46 */
459 static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
460 {
461  int b;
462  if (full)
463  b = 9;
464  else {
465  b = 4;
466  par_mapped[10] = 0;
467  }
468  for (; b >= 0; b--) {
469  par_mapped[2*b+1] = par_mapped[2*b] = par[b];
470  }
471 }
472 
473 static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
474 {
475  par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3;
476  par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3;
477  par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3;
478  par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3;
479  par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2;
480  par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2;
481  par_mapped[ 6] = par[10];
482  par_mapped[ 7] = par[11];
483  par_mapped[ 8] = ( par[12] + par[13]) / 2;
484  par_mapped[ 9] = ( par[14] + par[15]) / 2;
485  par_mapped[10] = par[16];
486  if (full) {
487  par_mapped[11] = par[17];
488  par_mapped[12] = par[18];
489  par_mapped[13] = par[19];
490  par_mapped[14] = ( par[20] + par[21]) / 2;
491  par_mapped[15] = ( par[22] + par[23]) / 2;
492  par_mapped[16] = ( par[24] + par[25]) / 2;
493  par_mapped[17] = ( par[26] + par[27]) / 2;
494  par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4;
495  par_mapped[19] = ( par[32] + par[33]) / 2;
496  }
497 }
498 
500 {
501 #if USE_FIXED
502  par[ 0] = (int)(((int64_t)(par[ 0] + (unsigned)(par[ 1]>>1)) * 1431655765 + \
503  0x40000000) >> 31);
504  par[ 1] = (int)(((int64_t)((par[ 1]>>1) + (unsigned)par[ 2]) * 1431655765 + \
505  0x40000000) >> 31);
506  par[ 2] = (int)(((int64_t)(par[ 3] + (unsigned)(par[ 4]>>1)) * 1431655765 + \
507  0x40000000) >> 31);
508  par[ 3] = (int)(((int64_t)((par[ 4]>>1) + (unsigned)par[ 5]) * 1431655765 + \
509  0x40000000) >> 31);
510 #else
511  par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f;
512  par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f;
513  par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f;
514  par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f;
515 #endif /* USE_FIXED */
516  par[ 4] = AAC_HALF_SUM(par[ 6], par[ 7]);
517  par[ 5] = AAC_HALF_SUM(par[ 8], par[ 9]);
518  par[ 6] = par[10];
519  par[ 7] = par[11];
520  par[ 8] = AAC_HALF_SUM(par[12], par[13]);
521  par[ 9] = AAC_HALF_SUM(par[14], par[15]);
522  par[10] = par[16];
523  par[11] = par[17];
524  par[12] = par[18];
525  par[13] = par[19];
526  par[14] = AAC_HALF_SUM(par[20], par[21]);
527  par[15] = AAC_HALF_SUM(par[22], par[23]);
528  par[16] = AAC_HALF_SUM(par[24], par[25]);
529  par[17] = AAC_HALF_SUM(par[26], par[27]);
530 #if USE_FIXED
531  par[18] = (((par[28]+2)>>2) + ((par[29]+2)>>2) + ((par[30]+2)>>2) + ((par[31]+2)>>2));
532 #else
533  par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f;
534 #endif /* USE_FIXED */
535  par[19] = AAC_HALF_SUM(par[32], par[33]);
536 }
537 
538 static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
539 {
540  if (full) {
541  par_mapped[33] = par[9];
542  par_mapped[32] = par[9];
543  par_mapped[31] = par[9];
544  par_mapped[30] = par[9];
545  par_mapped[29] = par[9];
546  par_mapped[28] = par[9];
547  par_mapped[27] = par[8];
548  par_mapped[26] = par[8];
549  par_mapped[25] = par[8];
550  par_mapped[24] = par[8];
551  par_mapped[23] = par[7];
552  par_mapped[22] = par[7];
553  par_mapped[21] = par[7];
554  par_mapped[20] = par[7];
555  par_mapped[19] = par[6];
556  par_mapped[18] = par[6];
557  par_mapped[17] = par[5];
558  par_mapped[16] = par[5];
559  } else {
560  par_mapped[16] = 0;
561  }
562  par_mapped[15] = par[4];
563  par_mapped[14] = par[4];
564  par_mapped[13] = par[4];
565  par_mapped[12] = par[4];
566  par_mapped[11] = par[3];
567  par_mapped[10] = par[3];
568  par_mapped[ 9] = par[2];
569  par_mapped[ 8] = par[2];
570  par_mapped[ 7] = par[2];
571  par_mapped[ 6] = par[2];
572  par_mapped[ 5] = par[1];
573  par_mapped[ 4] = par[1];
574  par_mapped[ 3] = par[1];
575  par_mapped[ 2] = par[0];
576  par_mapped[ 1] = par[0];
577  par_mapped[ 0] = par[0];
578 }
579 
580 static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
581 {
582  if (full) {
583  par_mapped[33] = par[19];
584  par_mapped[32] = par[19];
585  par_mapped[31] = par[18];
586  par_mapped[30] = par[18];
587  par_mapped[29] = par[18];
588  par_mapped[28] = par[18];
589  par_mapped[27] = par[17];
590  par_mapped[26] = par[17];
591  par_mapped[25] = par[16];
592  par_mapped[24] = par[16];
593  par_mapped[23] = par[15];
594  par_mapped[22] = par[15];
595  par_mapped[21] = par[14];
596  par_mapped[20] = par[14];
597  par_mapped[19] = par[13];
598  par_mapped[18] = par[12];
599  par_mapped[17] = par[11];
600  }
601  par_mapped[16] = par[10];
602  par_mapped[15] = par[ 9];
603  par_mapped[14] = par[ 9];
604  par_mapped[13] = par[ 8];
605  par_mapped[12] = par[ 8];
606  par_mapped[11] = par[ 7];
607  par_mapped[10] = par[ 6];
608  par_mapped[ 9] = par[ 5];
609  par_mapped[ 8] = par[ 5];
610  par_mapped[ 7] = par[ 4];
611  par_mapped[ 6] = par[ 4];
612  par_mapped[ 5] = par[ 3];
613  par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
614  par_mapped[ 3] = par[ 2];
615  par_mapped[ 2] = par[ 1];
616  par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
617  par_mapped[ 0] = par[ 0];
618 }
619 
621 {
622  par[33] = par[19];
623  par[32] = par[19];
624  par[31] = par[18];
625  par[30] = par[18];
626  par[29] = par[18];
627  par[28] = par[18];
628  par[27] = par[17];
629  par[26] = par[17];
630  par[25] = par[16];
631  par[24] = par[16];
632  par[23] = par[15];
633  par[22] = par[15];
634  par[21] = par[14];
635  par[20] = par[14];
636  par[19] = par[13];
637  par[18] = par[12];
638  par[17] = par[11];
639  par[16] = par[10];
640  par[15] = par[ 9];
641  par[14] = par[ 9];
642  par[13] = par[ 8];
643  par[12] = par[ 8];
644  par[11] = par[ 7];
645  par[10] = par[ 6];
646  par[ 9] = par[ 5];
647  par[ 8] = par[ 5];
648  par[ 7] = par[ 4];
649  par[ 6] = par[ 4];
650  par[ 5] = par[ 3];
651  par[ 4] = AAC_HALF_SUM(par[ 2], par[ 3]);
652  par[ 3] = par[ 2];
653  par[ 2] = par[ 1];
654  par[ 1] = AAC_HALF_SUM(par[ 0], par[ 1]);
655 }
656 
657 static void decorrelation(PSContext *ps, INTFLOAT (*out)[32][2], const INTFLOAT (*s)[32][2], int is34)
658 {
660  LOCAL_ALIGNED_16(INTFLOAT, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
661  INTFLOAT *peak_decay_nrg = ps->peak_decay_nrg;
662  INTFLOAT *power_smooth = ps->power_smooth;
663  INTFLOAT *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
664  INTFLOAT (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
666 #if !USE_FIXED
667  const float transient_impact = 1.5f;
668  const float a_smooth = 0.25f; ///< Smoothing coefficient
669 #endif /* USE_FIXED */
670  const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
671  int i, k, m, n;
672  int n0 = 0, nL = 32;
673  const INTFLOAT peak_decay_factor = Q31(0.76592833836465f);
674 
675  memset(power, 0, 34 * sizeof(*power));
676 
677  if (is34 != ps->is34bands_old) {
678  memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
679  memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
680  memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
681  memset(ps->delay, 0, sizeof(ps->delay));
682  memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
683  }
684 
685  for (k = 0; k < NR_BANDS[is34]; k++) {
686  int i = k_to_i[k];
687  ps->dsp.add_squares(power[i], s[k], nL - n0);
688  }
689 
690  //Transient detection
691 #if USE_FIXED
692  for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
693  for (n = n0; n < nL; n++) {
694  int decayed_peak;
695  decayed_peak = (int)(((int64_t)peak_decay_factor * \
696  peak_decay_nrg[i] + 0x40000000) >> 31);
697  peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
698  power_smooth[i] += (power[i][n] + 2LL - power_smooth[i]) >> 2;
699  peak_decay_diff_smooth[i] += (peak_decay_nrg[i] + 2LL - power[i][n] - \
700  peak_decay_diff_smooth[i]) >> 2;
701 
702  if (peak_decay_diff_smooth[i]) {
703  transient_gain[i][n] = FFMIN(power_smooth[i]*43691LL / peak_decay_diff_smooth[i], 1<<16);
704  } else
705  transient_gain[i][n] = 1 << 16;
706  }
707  }
708 #else
709  for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
710  for (n = n0; n < nL; n++) {
711  float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
712  float denom;
713  peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
714  power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
715  peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
716  denom = transient_impact * peak_decay_diff_smooth[i];
717  transient_gain[i][n] = (denom > power_smooth[i]) ?
718  power_smooth[i] / denom : 1.0f;
719  }
720  }
721 
722 #endif /* USE_FIXED */
723  //Decorrelation and transient reduction
724  // PS_AP_LINKS - 1
725  // -----
726  // | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
727  //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
728  // | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
729  // m = 0
730  //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
731  for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
732  int b = k_to_i[k];
733 #if USE_FIXED
734  int g_decay_slope;
735 
736  if (k - DECAY_CUTOFF[is34] <= 0) {
737  g_decay_slope = 1 << 30;
738  }
739  else if (k - DECAY_CUTOFF[is34] >= 20) {
740  g_decay_slope = 0;
741  }
742  else {
743  g_decay_slope = (1 << 30) - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
744  }
745 #else
746  float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
747  g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
748 #endif /* USE_FIXED */
749  memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
750  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
751  for (m = 0; m < PS_AP_LINKS; m++) {
752  memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
753  }
754  ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
755  phi_fract[is34][k],
756  (const INTFLOAT (*)[2]) Q_fract_allpass[is34][k],
757  transient_gain[b], g_decay_slope, nL - n0);
758  }
759  for (; k < SHORT_DELAY_BAND[is34]; k++) {
760  int i = k_to_i[k];
761  memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
762  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
763  //H = delay 14
764  ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
765  transient_gain[i], nL - n0);
766  }
767  for (; k < NR_BANDS[is34]; k++) {
768  int i = k_to_i[k];
769  memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
770  memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
771  //H = delay 1
772  ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
773  transient_gain[i], nL - n0);
774  }
775 }
776 
777 static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
778  int8_t (*par)[PS_MAX_NR_IIDICC],
779  int num_par, int num_env, int full)
780 {
781  int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
782  int e;
783  if (num_par == 20 || num_par == 11) {
784  for (e = 0; e < num_env; e++) {
785  map_idx_20_to_34(par_mapped[e], par[e], full);
786  }
787  } else if (num_par == 10 || num_par == 5) {
788  for (e = 0; e < num_env; e++) {
789  map_idx_10_to_34(par_mapped[e], par[e], full);
790  }
791  } else {
792  *p_par_mapped = par;
793  }
794 }
795 
796 static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
797  int8_t (*par)[PS_MAX_NR_IIDICC],
798  int num_par, int num_env, int full)
799 {
800  int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
801  int e;
802  if (num_par == 34 || num_par == 17) {
803  for (e = 0; e < num_env; e++) {
804  map_idx_34_to_20(par_mapped[e], par[e], full);
805  }
806  } else if (num_par == 10 || num_par == 5) {
807  for (e = 0; e < num_env; e++) {
808  map_idx_10_to_20(par_mapped[e], par[e], full);
809  }
810  } else {
811  *p_par_mapped = par;
812  }
813 }
814 
815 static void stereo_processing(PSContext *ps, INTFLOAT (*l)[32][2], INTFLOAT (*r)[32][2], int is34)
816 {
817  int e, b, k;
818 
819  INTFLOAT (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
820  INTFLOAT (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
821  INTFLOAT (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
822  INTFLOAT (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
823  int8_t *opd_hist = ps->opd_hist;
824  int8_t *ipd_hist = ps->ipd_hist;
825  int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
826  int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
827  int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
828  int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
829  int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
830  int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
831  int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
832  int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
833  const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
834  TABLE_CONST INTFLOAT (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
835 
836  //Remapping
837  if (ps->num_env_old) {
838  memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
839  memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
840  memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
841  memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
842  memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
843  memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
844  memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
845  memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
846  }
847 
848  if (is34) {
849  remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
850  remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
851  if (ps->enable_ipdopd) {
852  remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
853  remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
854  }
855  if (!ps->is34bands_old) {
856  map_val_20_to_34(H11[0][0]);
857  map_val_20_to_34(H11[1][0]);
858  map_val_20_to_34(H12[0][0]);
859  map_val_20_to_34(H12[1][0]);
860  map_val_20_to_34(H21[0][0]);
861  map_val_20_to_34(H21[1][0]);
862  map_val_20_to_34(H22[0][0]);
863  map_val_20_to_34(H22[1][0]);
864  ipdopd_reset(ipd_hist, opd_hist);
865  }
866  } else {
867  remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
868  remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
869  if (ps->enable_ipdopd) {
870  remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
871  remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
872  }
873  if (ps->is34bands_old) {
874  map_val_34_to_20(H11[0][0]);
875  map_val_34_to_20(H11[1][0]);
876  map_val_34_to_20(H12[0][0]);
877  map_val_34_to_20(H12[1][0]);
878  map_val_34_to_20(H21[0][0]);
879  map_val_34_to_20(H21[1][0]);
880  map_val_34_to_20(H22[0][0]);
881  map_val_34_to_20(H22[1][0]);
882  ipdopd_reset(ipd_hist, opd_hist);
883  }
884  }
885 
886  //Mixing
887  for (e = 0; e < ps->num_env; e++) {
888  for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
889  INTFLOAT h11, h12, h21, h22;
890  h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
891  h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
892  h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
893  h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
894 
895  if (!PS_BASELINE && ps->enable_ipdopd && b < NR_IPDOPD_BANDS[is34]) {
896  //The spec say says to only run this smoother when enable_ipdopd
897  //is set but the reference decoder appears to run it constantly
898  INTFLOAT h11i, h12i, h21i, h22i;
899  INTFLOAT ipd_adj_re, ipd_adj_im;
900  int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
901  int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
902  INTFLOAT opd_re = pd_re_smooth[opd_idx];
903  INTFLOAT opd_im = pd_im_smooth[opd_idx];
904  INTFLOAT ipd_re = pd_re_smooth[ipd_idx];
905  INTFLOAT ipd_im = pd_im_smooth[ipd_idx];
906  opd_hist[b] = opd_idx & 0x3F;
907  ipd_hist[b] = ipd_idx & 0x3F;
908 
909  ipd_adj_re = AAC_MADD30(opd_re, ipd_re, opd_im, ipd_im);
910  ipd_adj_im = AAC_MSUB30(opd_im, ipd_re, opd_re, ipd_im);
911  h11i = AAC_MUL30(h11, opd_im);
912  h11 = AAC_MUL30(h11, opd_re);
913  h12i = AAC_MUL30(h12, ipd_adj_im);
914  h12 = AAC_MUL30(h12, ipd_adj_re);
915  h21i = AAC_MUL30(h21, opd_im);
916  h21 = AAC_MUL30(h21, opd_re);
917  h22i = AAC_MUL30(h22, ipd_adj_im);
918  h22 = AAC_MUL30(h22, ipd_adj_re);
919  H11[1][e+1][b] = h11i;
920  H12[1][e+1][b] = h12i;
921  H21[1][e+1][b] = h21i;
922  H22[1][e+1][b] = h22i;
923  }
924  H11[0][e+1][b] = h11;
925  H12[0][e+1][b] = h12;
926  H21[0][e+1][b] = h21;
927  H22[0][e+1][b] = h22;
928  }
929  for (k = 0; k < NR_BANDS[is34]; k++) {
930  LOCAL_ALIGNED_16(INTFLOAT, h, [2], [4]);
931  LOCAL_ALIGNED_16(INTFLOAT, h_step, [2], [4]);
932  int start = ps->border_position[e];
933  int stop = ps->border_position[e+1];
934  INTFLOAT width = Q30(1.f) / ((stop - start) ? (stop - start) : 1);
935 #if USE_FIXED
936  width = FFMIN(2U*width, INT_MAX);
937 #endif
938  b = k_to_i[k];
939  h[0][0] = H11[0][e][b];
940  h[0][1] = H12[0][e][b];
941  h[0][2] = H21[0][e][b];
942  h[0][3] = H22[0][e][b];
943  if (!PS_BASELINE && ps->enable_ipdopd) {
944  //Is this necessary? ps_04_new seems unchanged
945  if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
946  h[1][0] = -H11[1][e][b];
947  h[1][1] = -H12[1][e][b];
948  h[1][2] = -H21[1][e][b];
949  h[1][3] = -H22[1][e][b];
950  } else {
951  h[1][0] = H11[1][e][b];
952  h[1][1] = H12[1][e][b];
953  h[1][2] = H21[1][e][b];
954  h[1][3] = H22[1][e][b];
955  }
956  }
957  //Interpolation
958  h_step[0][0] = AAC_MSUB31_V3(H11[0][e+1][b], h[0][0], width);
959  h_step[0][1] = AAC_MSUB31_V3(H12[0][e+1][b], h[0][1], width);
960  h_step[0][2] = AAC_MSUB31_V3(H21[0][e+1][b], h[0][2], width);
961  h_step[0][3] = AAC_MSUB31_V3(H22[0][e+1][b], h[0][3], width);
962  if (!PS_BASELINE && ps->enable_ipdopd) {
963  h_step[1][0] = AAC_MSUB31_V3(H11[1][e+1][b], h[1][0], width);
964  h_step[1][1] = AAC_MSUB31_V3(H12[1][e+1][b], h[1][1], width);
965  h_step[1][2] = AAC_MSUB31_V3(H21[1][e+1][b], h[1][2], width);
966  h_step[1][3] = AAC_MSUB31_V3(H22[1][e+1][b], h[1][3], width);
967  }
969  l[k] + 1 + start, r[k] + 1 + start,
970  h, h_step, stop - start);
971  }
972  }
973 }
974 
975 int AAC_RENAME(ff_ps_apply)(AVCodecContext *avctx, PSContext *ps, INTFLOAT L[2][38][64], INTFLOAT R[2][38][64], int top)
976 {
977  INTFLOAT (*Lbuf)[32][2] = ps->Lbuf;
978  INTFLOAT (*Rbuf)[32][2] = ps->Rbuf;
979  const int len = 32;
980  int is34 = ps->is34bands;
981 
982  top += NR_BANDS[is34] - 64;
983  memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
984  if (top < NR_ALLPASS_BANDS[is34])
985  memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
986 
987  hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
988  decorrelation(ps, Rbuf, (const INTFLOAT (*)[32][2]) Lbuf, is34);
989  stereo_processing(ps, Lbuf, Rbuf, is34);
990  hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
991  hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
992 
993  return 0;
994 }
995 
996 #define PS_INIT_VLC_STATIC(num, size) \
997  INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
998  ps_tmp[num].ps_bits, 1, 1, \
999  ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
1000  size);
1001 
1002 #define PS_VLC_ROW(name) \
1003  { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
1004 
1006  // Syntax initialization
1007  static const struct {
1008  const void *ps_codes, *ps_bits;
1009  const unsigned int table_size, elem_size;
1010  } ps_tmp[] = {
1021  };
1022 
1023  PS_INIT_VLC_STATIC(0, 1544);
1024  PS_INIT_VLC_STATIC(1, 832);
1025  PS_INIT_VLC_STATIC(2, 1024);
1026  PS_INIT_VLC_STATIC(3, 1036);
1027  PS_INIT_VLC_STATIC(4, 544);
1028  PS_INIT_VLC_STATIC(5, 544);
1029  PS_INIT_VLC_STATIC(6, 512);
1030  PS_INIT_VLC_STATIC(7, 512);
1031  PS_INIT_VLC_STATIC(8, 512);
1032  PS_INIT_VLC_STATIC(9, 512);
1033 
1034  ps_tableinit();
1035 }
1036 
1038 {
1039  AAC_RENAME(ff_psdsp_init)(&ps->dsp);
1040 }
void(* mul_pair_single)(INTFLOAT(*dst)[2], INTFLOAT(*src0)[2], INTFLOAT *src1, int n)
Definition: aacpsdsp.h:32
void AAC_RENAME() ff_psdsp_init(PSDSPContext *s)
static const INTFLOAT g1_Q2[]
Definition: aacpsdata.c:160
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
Definition: aacps.c:580
INTFLOAT peak_decay_diff_smooth[34]
Definition: aacps.h:69
int nr_iid_par
Definition: aacps.h:45
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
else temp
Definition: vf_mcdeint.c:259
static int HA[46][8][4]
static TABLE_CONST int Q_fract_allpass[2][50][3][2]
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
static void ipdopd_reset(int8_t *ipd_hist, int8_t *opd_hist)
Definition: aacps.c:149
INTFLOAT peak_decay_nrg[34]
Definition: aacps.h:67
static const int NR_BANDS[]
Number of frequency bands that can be addressed by the sub subband index, k.
Definition: aacps.c:450
int AAC_RENAME() ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
Definition: aacps.c:158
void(* decorrelate)(INTFLOAT(*out)[2], INTFLOAT(*delay)[2], INTFLOAT(*ap_delay)[PS_QMF_TIME_SLOTS+PS_MAX_AP_DELAY][2], const INTFLOAT phi_fract[2], const INTFLOAT(*Q_fract)[2], const INTFLOAT *transient_gain, INTFLOAT g_decay_slope, int len)
Definition: aacpsdsp.h:41
const char * b
Definition: vf_curves.c:113
static const int8_t nr_iidopd_par_tab[]
Definition: aacps.c:54
static const int NR_IPDOPD_BANDS[]
Definition: aacps.c:448
static const int8_t huff_offset[]
Definition: aacpsdata.c:136
void(* add_squares)(INTFLOAT *dst, const INTFLOAT(*src)[2], int n)
Definition: aacpsdsp.h:31
static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
Table 8.46.
Definition: aacps.c:459
static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
Definition: aacps.c:473
int enable_ipdopd
Definition: aacps.h:54
INTFLOAT ap_delay[PS_MAX_AP_BANDS][PS_AP_LINKS][PS_QMF_TIME_SLOTS+PS_MAX_AP_DELAY][2]
Definition: aacps.h:66
static const int8_t k_to_i_34[]
Table 8.49.
Definition: aacpsdata.c:152
#define PS_INIT_VLC_STATIC(num, size)
Definition: aacps.c:996
int AAC_RENAME() ff_ps_apply(AVCodecContext *avctx, PSContext *ps, INTFLOAT L[2][38][64], INTFLOAT R[2][38][64], int top)
Definition: aacps.c:975
float INTFLOAT
Definition: aac_defines.h:86
#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION)
Definition: aacps.c:80
static int pd_im_smooth[8 *8 *8]
#define Q30(x)
Definition: aac_defines.h:95
#define av_cold
Definition: attributes.h:82
INTFLOAT H21[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC]
Definition: aacps.h:72
INTFLOAT delay[PS_MAX_SSB][PS_QMF_TIME_SLOTS+PS_MAX_DELAY][2]
Definition: aacps.h:65
static uint32_t reverse(uint32_t num, int bits)
Definition: speedhq.c:562
#define PS_MAX_AP_DELAY
Definition: aacps.h:39
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:80
#define N
Definition: vf_pp7.c:73
static void hybrid_analysis(PSDSPContext *dsp, INTFLOAT out[91][32][2], INTFLOAT in[5][44][2], INTFLOAT L[2][38][64], int is34, int len)
Definition: aacps.c:373
static void remap34(int8_t(**p_par_mapped)[PS_MAX_NR_IIDICC], int8_t(*par)[PS_MAX_NR_IIDICC], int num_par, int num_env, int full)
Definition: aacps.c:777
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
#define AAC_MUL31(x, y)
Definition: aac_defines.h:102
int num_env_old
Definition: aacps.h:52
bitstream reader API header.
static int f34_0_12[12][8][2]
static const uint8_t header[24]
Definition: sdr2.c:67
static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
Definition: aacps.c:538
#define av_log(a,...)
#define DECAY_SLOPE
All-pass filter decay slope.
Definition: aacps.c:445
static void hybrid6_cx(PSDSPContext *dsp, INTFLOAT(*in)[2], INTFLOAT(*out)[32][2], TABLE_CONST INTFLOAT(*filter)[8][2], int len)
Split one subband into 6 subsubbands with a complex filter.
Definition: aacps.c:338
#define TABLE_CONST
#define U(x)
Definition: vp56_arith.h:37
#define AAC_MSUB30(x, y, a, b)
Definition: aac_defines.h:107
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define PS_MAX_NR_IIDICC
Definition: aacps.h:32
static void hybrid2_re(INTFLOAT(*in)[2], INTFLOAT(*out)[32][2], const INTFLOAT filter[8], int len, int reverse)
Split one subband into 2 subsubbands with a symmetric real filter.
Definition: aacps.c:312
int8_t icc_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-Channel Coherence Parameters.
Definition: aacps.h:57
int iid_quant
Definition: aacps.h:44
#define AAC_HALF_SUM(x, y)
Definition: aac_defines.h:111
static int read_iid_data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, int8_t(*iid)[34], int table_idx, int e, int dt)
\ * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \ * Inter-channel Phase Differen...
Definition: aacps.c:124
static const int SHORT_DELAY_BAND[]
First stereo band using the short one sample delay.
Definition: aacps.c:456
#define PS_BASELINE
Operate in Baseline PS mode.
Definition: aacps.c:39
INTFLOAT H22[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC]
Definition: aacps.h:73
const char * r
Definition: vf_curves.c:111
#define PS_VLC_ROW(name)
Definition: aacps.c:1002
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
int num_env
Definition: aacps.h:53
static int phi_fract[2][50][2]
Definition: vlc.h:26
int nr_icc_par
Definition: aacps.h:49
static int read_icc_data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, int8_t(*icc)[34], int table_idx, int e, int dt)
\ * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \ * Inter-channel Phase Differen...
Definition: aacps.c:125
#define AAC_RENAME(x)
Definition: aac_defines.h:84
common internal API header
static int f34_1_8[8][8][2]
void(* stereo_interpolate[2])(INTFLOAT(*l)[2], INTFLOAT(*r)[2], INTFLOAT h[2][4], INTFLOAT h_step[2][4], int len)
Definition: aacpsdsp.h:47
#define PS_MAX_NUM_ENV
Definition: aacps.h:31
#define Q31(x)
Definition: aac_defines.h:96
#define FFMIN(a, b)
Definition: common.h:96
static void stereo_processing(PSContext *ps, INTFLOAT(*l)[32][2], INTFLOAT(*r)[32][2], int is34)
Definition: aacps.c:815
#define width
static void map_val_20_to_34(INTFLOAT par[PS_MAX_NR_IIDICC])
Definition: aacps.c:620
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static void hybrid_synthesis(PSDSPContext *dsp, INTFLOAT out[2][38][64], INTFLOAT in[91][32][2], int is34, int len)
Definition: aacps.c:403
static int read_ipdopd_data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, int8_t(*ipdopd)[34], int table_idx, int e, int dt)
\ * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \ * Inter-channel Phase Differen...
Definition: aacps.c:126
int8_t ipd_hist[PS_MAX_NR_IIDICC]
Definition: aacps.h:77
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static const int huff_iid[]
Definition: aacps.c:71
int n
Definition: avisynth_c.h:684
int8_t ipd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-channel Phase Difference Parameters.
Definition: aacps.h:59
av_cold void AAC_RENAME() ff_ps_init(void)
Definition: aacps.c:1005
static const int8_t nr_iidicc_par_tab[]
Definition: aacps.c:50
#define L(x)
Definition: vp56_arith.h:36
#define INTFLOAT
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:23
#define PS_MAX_DELAY
Definition: aacps.h:37
PSDSPContext dsp
Definition: aacps.h:78
static int HB[46][8][4]
static const int NR_ALLPASS_BANDS[]
Number of all-pass filer bands.
Definition: aacps.c:454
Libavcodec external API header.
static const int8_t num_env_tab[2][4]
Definition: aacps.c:45
static void hybrid4_8_12_cx(PSDSPContext *dsp, INTFLOAT(*in)[2], INTFLOAT(*out)[32][2], TABLE_CONST INTFLOAT(*filter)[8][2], int N, int len)
Definition: aacps.c:362
main external API structure.
Definition: avcodec.h:1732
Definition: vf_geq.c:46
static void decorrelation(PSContext *ps, INTFLOAT(*out)[32][2], const INTFLOAT(*s)[32][2], int is34)
Definition: aacps.c:657
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static void ps_tableinit(void)
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
int8_t iid_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Inter-channel Intensity Difference Parameters.
Definition: aacps.h:56
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:338
static const int NR_PAR_BANDS[]
Number of frequency bands that can be addressed by the parameter index, b(k)
Definition: aacps.c:447
int is34bands_old
Definition: aacps.h:62
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
#define AAC_MUL30(x, y)
Definition: aac_defines.h:101
void(* hybrid_analysis)(INTFLOAT(*out)[2], INTFLOAT(*in)[2], const INTFLOAT(*filter)[8][2], int stride, int n)
Definition: aacpsdsp.h:34
INTFLOAT power_smooth[34]
Definition: aacps.h:68
av_cold void AAC_RENAME() ff_ps_ctx_init(PSContext *ps)
Definition: aacps.c:1037
int8_t opd_hist[PS_MAX_NR_IIDICC]
Definition: aacps.h:76
int border_position[PS_MAX_NUM_ENV+1]
Definition: aacps.h:55
#define AAC_MADD30(x, y, a, b)
Definition: aac_defines.h:104
static VLC vlc_ps[10]
Definition: aacps.c:78
#define AAC_MSUB31_V3(x, y, z)
Definition: aac_defines.h:110
#define PS_AP_LINKS
Definition: aacps.h:38
int
void(* hybrid_synthesis_deint)(INTFLOAT out[2][38][64], INTFLOAT(*in)[32][2], int i, int len)
Definition: aacpsdsp.h:39
common internal and external API header
static const int8_t k_to_i_20[]
Table 8.48.
Definition: aacpsdata.c:145
#define numQMFSlots
Definition: aacps.c:43
#define PS_QMF_TIME_SLOTS
Definition: aacps.h:36
static void remap20(int8_t(**p_par_mapped)[PS_MAX_NR_IIDICC], int8_t(*par)[PS_MAX_NR_IIDICC], int num_par, int num_env, int full)
Definition: aacps.c:796
int len
static int pd_re_smooth[8 *8 *8]
INTFLOAT H12[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC]
Definition: aacps.h:71
static int f34_2_4[4][8][2]
static void map_val_34_to_20(INTFLOAT par[PS_MAX_NR_IIDICC])
Definition: aacps.c:499
INTFLOAT H11[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC]
Definition: aacps.h:70
static const int DECAY_CUTOFF[]
Start frequency band for the all-pass filter decay slope.
Definition: aacps.c:452
#define PS_MAX_NR_IPDOPD
Definition: aacps.h:33
FILE * out
Definition: movenc.c:54
static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
Definition: aacps.c:128
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:124
void INT64 start
Definition: avisynth_c.h:690
static int f20_0_8[8][8][2]
int nr_ipdopd_par
Definition: aacps.h:46
int8_t opd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]
Overall Phase Difference Parameters.
Definition: aacps.h:60
void(* hybrid_analysis_ileave)(INTFLOAT(*out)[32][2], INTFLOAT L[2][38][64], int i, int len)
Definition: aacpsdsp.h:37
float INT64FLOAT
Definition: aac_defines.h:88
int icc_mode
Definition: aacps.h:48