aacps: Adjust some const qualifiers to suppress warnings
[libav.git] / libavcodec / aacps.c
CommitLineData
a2063901
AC
1/*
2 * MPEG-4 Parametric Stereo decoding functions
3 * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4 *
2912e87a 5 * This file is part of Libav.
a2063901 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
a2063901
AC
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
a2063901
AC
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
a2063901
AC
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdint.h>
c6b09471 23#include "libavutil/common.h"
218aefce 24#include "libavutil/internal.h"
a2063901
AC
25#include "libavutil/mathematics.h"
26#include "avcodec.h"
27#include "get_bits.h"
fd6eb4a0
AC
28#include "aacps.h"
29#include "aacps_tablegen.h"
30#include "aacpsdata.c"
a2063901 31
02494787
DB
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
a2063901
AC
35
36#define numQMFSlots 32 //numTimeSlots * RATE
37
38static const int8_t num_env_tab[2][4] = {
39 { 0, 1, 2, 4, },
40 { 1, 2, 3, 4, },
41};
42
43static const int8_t nr_iidicc_par_tab[] = {
44 10, 20, 34, 10, 20, 34,
45};
46
47static const int8_t nr_iidopd_par_tab[] = {
48 5, 11, 17, 5, 11, 17,
49};
50
51enum {
52 huff_iid_df1,
53 huff_iid_dt1,
54 huff_iid_df0,
55 huff_iid_dt0,
56 huff_icc_df,
57 huff_icc_dt,
58 huff_ipd_df,
59 huff_ipd_dt,
60 huff_opd_df,
61 huff_opd_dt,
62};
63
64static const int huff_iid[] = {
65 huff_iid_df0,
66 huff_iid_df1,
67 huff_iid_dt0,
68 huff_iid_dt1,
69};
70
71static VLC vlc_ps[10];
72
1076b0d0 73#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
d0fcf39f
DB
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 */ \
3fa1a932 86static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
1076b0d0
AC
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; \
112err: \
113 av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
114 return -1; \
a2063901
AC
115}
116
1076b0d0
AC
117READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
118READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U)
119READ_PAR_DATA(ipdopd, 0, 0x07, 0)
a2063901 120
3fa1a932 121static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
a2063901
AC
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);
3fa1a932 133 read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
a2063901 134 dt = get_bits1(gb);
3fa1a932 135 read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
a2063901
AC
136 }
137 }
138 skip_bits1(gb); //reserved_ps
139 return get_bits_count(gb) - count;
140}
141
142static void ipdopd_reset(int8_t *opd_hist, int8_t *ipd_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
151int 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) {
284b041b
AC
163 int iid_mode = get_bits(gb, 3);
164 if (iid_mode > 5) {
a2063901 165 av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
284b041b 166 iid_mode);
a2063901
AC
167 goto err;
168 }
284b041b
AC
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];
a2063901
AC
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 }
181 ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
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++)
c6b09471 196 ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
a2063901
AC
197
198 if (ps->enable_iid) {
199 for (e = 0; e < ps->num_env; e++) {
200 int dt = get_bits1(gb);
3fa1a932 201 if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
a2063901
AC
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);
3fa1a932 210 if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
a2063901
AC
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);
3fa1a932 224 cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
a2063901
AC
225 }
226 if (cnt < 0) {
9de3cbc1 227 av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
a2063901
AC
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 if (source >= 0 && source != ps->num_env) {
d23077af 240 if (ps->enable_iid) {
a2063901
AC
241 memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
242 }
d23077af 243 if (ps->enable_icc) {
a2063901
AC
244 memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
245 }
d23077af 246 if (ps->enable_ipdopd) {
a2063901
AC
247 memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
248 memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
249 }
250 }
251 ps->num_env++;
252 ps->border_position[ps->num_env] = numQMFSlots - 1;
253 }
254
255
256 ps->is34bands_old = ps->is34bands;
257 if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
258 ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
259 (ps->enable_icc && ps->nr_icc_par == 34);
260
261 //Baseline
262 if (!ps->enable_ipdopd) {
263 memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
264 memset(ps->opd_par, 0, sizeof(ps->opd_par));
265 }
266
267 if (header)
268 ps->start = 1;
269
270 bits_consumed = get_bits_count(gb) - bit_count_start;
271 if (bits_consumed <= bits_left) {
272 skip_bits_long(gb_host, bits_consumed);
273 return bits_consumed;
274 }
275 av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
276err:
277 ps->start = 0;
278 skip_bits_long(gb_host, bits_left);
a237b380
AC
279 memset(ps->iid_par, 0, sizeof(ps->iid_par));
280 memset(ps->icc_par, 0, sizeof(ps->icc_par));
281 memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
282 memset(ps->opd_par, 0, sizeof(ps->opd_par));
a2063901
AC
283 return bits_left;
284}
285
286/** Split one subband into 2 subsubbands with a symmetric real filter.
287 * The filter must have its non-center even coefficients equal to zero. */
47d18d53 288static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[8], int len, int reverse)
a2063901
AC
289{
290 int i, j;
5e071f53
AC
291 for (i = 0; i < len; i++, in++) {
292 float re_in = filter[6] * in[6][0]; //real inphase
a2063901 293 float re_op = 0.0f; //real out of phase
5e071f53 294 float im_in = filter[6] * in[6][1]; //imag inphase
a2063901
AC
295 float im_op = 0.0f; //imag out of phase
296 for (j = 0; j < 6; j += 2) {
5e071f53
AC
297 re_op += filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
298 im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
a2063901
AC
299 }
300 out[ reverse][i][0] = re_in + re_op;
301 out[ reverse][i][1] = im_in + im_op;
302 out[!reverse][i][0] = re_in - re_op;
303 out[!reverse][i][1] = im_in - im_op;
304 }
305}
306
307/** Split one subband into 6 subsubbands with a complex filter */
0ba8ba16
DB
308static void hybrid6_cx(PSDSPContext *dsp, float (*in)[2], float (*out)[32][2],
309 TABLE_CONST float (*filter)[8][2], int len)
a2063901 310{
bf1945af 311 int i;
a2063901 312 int N = 8;
47d18d53 313 LOCAL_ALIGNED_16(float, temp, [8], [2]);
a2063901 314
5e071f53 315 for (i = 0; i < len; i++, in++) {
0ba8ba16 316 dsp->hybrid_analysis(temp, in, (const float (*)[8][2]) filter, 1, N);
a2063901
AC
317 out[0][i][0] = temp[6][0];
318 out[0][i][1] = temp[6][1];
319 out[1][i][0] = temp[7][0];
320 out[1][i][1] = temp[7][1];
321 out[2][i][0] = temp[0][0];
322 out[2][i][1] = temp[0][1];
323 out[3][i][0] = temp[1][0];
324 out[3][i][1] = temp[1][1];
325 out[4][i][0] = temp[2][0] + temp[5][0];
326 out[4][i][1] = temp[2][1] + temp[5][1];
327 out[5][i][0] = temp[3][0] + temp[4][0];
328 out[5][i][1] = temp[3][1] + temp[4][1];
329 }
330}
331
0ba8ba16
DB
332static void hybrid4_8_12_cx(PSDSPContext *dsp,
333 float (*in)[2], float (*out)[32][2],
334 TABLE_CONST float (*filter)[8][2], int N, int len)
a2063901 335{
bf1945af 336 int i;
a2063901 337
5e071f53 338 for (i = 0; i < len; i++, in++) {
0ba8ba16 339 dsp->hybrid_analysis(out[0] + i, in, (const float (*)[8][2]) filter, 32, N);
a2063901
AC
340 }
341}
342
bf1945af
MR
343static void hybrid_analysis(PSDSPContext *dsp, float out[91][32][2],
344 float in[5][44][2], float L[2][38][64],
345 int is34, int len)
a2063901
AC
346{
347 int i, j;
348 for (i = 0; i < 5; i++) {
349 for (j = 0; j < 38; j++) {
350 in[i][j+6][0] = L[0][j][i];
351 in[i][j+6][1] = L[1][j][i];
352 }
353 }
890fe85f 354 if (is34) {
bf1945af
MR
355 hybrid4_8_12_cx(dsp, in[0], out, f34_0_12, 12, len);
356 hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8, 8, len);
357 hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4, 4, len);
358 hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4, 4, len);
359 hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4, 4, len);
360 dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
a2063901 361 } else {
bf1945af 362 hybrid6_cx(dsp, in[0], out, f20_0_8, len);
a2063901
AC
363 hybrid2_re(in[1], out+6, g1_Q2, len, 1);
364 hybrid2_re(in[2], out+8, g1_Q2, len, 0);
bf1945af 365 dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
a2063901
AC
366 }
367 //update in_buf
368 for (i = 0; i < 5; i++) {
369 memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
370 }
371}
372
bf1945af
MR
373static void hybrid_synthesis(PSDSPContext *dsp, float out[2][38][64],
374 float in[91][32][2], int is34, int len)
a2063901
AC
375{
376 int i, n;
890fe85f 377 if (is34) {
a2063901
AC
378 for (n = 0; n < len; n++) {
379 memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
380 memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
890fe85f 381 for (i = 0; i < 12; i++) {
a2063901
AC
382 out[0][n][0] += in[ i][n][0];
383 out[1][n][0] += in[ i][n][1];
384 }
890fe85f 385 for (i = 0; i < 8; i++) {
a2063901
AC
386 out[0][n][1] += in[12+i][n][0];
387 out[1][n][1] += in[12+i][n][1];
388 }
890fe85f 389 for (i = 0; i < 4; i++) {
a2063901
AC
390 out[0][n][2] += in[20+i][n][0];
391 out[1][n][2] += in[20+i][n][1];
392 out[0][n][3] += in[24+i][n][0];
393 out[1][n][3] += in[24+i][n][1];
394 out[0][n][4] += in[28+i][n][0];
395 out[1][n][4] += in[28+i][n][1];
396 }
397 }
bf1945af 398 dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
a2063901
AC
399 } else {
400 for (n = 0; n < len; n++) {
401 out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
402 in[3][n][0] + in[4][n][0] + in[5][n][0];
403 out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
404 in[3][n][1] + in[4][n][1] + in[5][n][1];
405 out[0][n][1] = in[6][n][0] + in[7][n][0];
406 out[1][n][1] = in[6][n][1] + in[7][n][1];
407 out[0][n][2] = in[8][n][0] + in[9][n][0];
408 out[1][n][2] = in[8][n][1] + in[9][n][1];
409 }
bf1945af 410 dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
a2063901
AC
411 }
412}
413
414/// All-pass filter decay slope
415#define DECAY_SLOPE 0.05f
416/// Number of frequency bands that can be addressed by the parameter index, b(k)
417static const int NR_PAR_BANDS[] = { 20, 34 };
418/// Number of frequency bands that can be addressed by the sub subband index, k
419static const int NR_BANDS[] = { 71, 91 };
420/// Start frequency band for the all-pass filter decay slope
421static const int DECAY_CUTOFF[] = { 10, 32 };
422/// Number of all-pass filer bands
423static const int NR_ALLPASS_BANDS[] = { 30, 50 };
424/// First stereo band using the short one sample delay
425static const int SHORT_DELAY_BAND[] = { 42, 62 };
426
427/** Table 8.46 */
428static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
429{
430 int b;
431 if (full)
432 b = 9;
433 else {
434 b = 4;
435 par_mapped[10] = 0;
436 }
437 for (; b >= 0; b--) {
438 par_mapped[2*b+1] = par_mapped[2*b] = par[b];
439 }
440}
441
442static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
443{
444 par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3;
445 par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3;
446 par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3;
447 par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3;
448 par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2;
449 par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2;
450 par_mapped[ 6] = par[10];
451 par_mapped[ 7] = par[11];
452 par_mapped[ 8] = ( par[12] + par[13]) / 2;
453 par_mapped[ 9] = ( par[14] + par[15]) / 2;
454 par_mapped[10] = par[16];
455 if (full) {
456 par_mapped[11] = par[17];
457 par_mapped[12] = par[18];
458 par_mapped[13] = par[19];
459 par_mapped[14] = ( par[20] + par[21]) / 2;
460 par_mapped[15] = ( par[22] + par[23]) / 2;
461 par_mapped[16] = ( par[24] + par[25]) / 2;
462 par_mapped[17] = ( par[26] + par[27]) / 2;
463 par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4;
464 par_mapped[19] = ( par[32] + par[33]) / 2;
465 }
466}
467
468static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC])
469{
470 par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f;
471 par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f;
472 par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f;
473 par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f;
474 par[ 4] = ( par[ 6] + par[ 7]) * 0.5f;
475 par[ 5] = ( par[ 8] + par[ 9]) * 0.5f;
476 par[ 6] = par[10];
477 par[ 7] = par[11];
478 par[ 8] = ( par[12] + par[13]) * 0.5f;
479 par[ 9] = ( par[14] + par[15]) * 0.5f;
480 par[10] = par[16];
481 par[11] = par[17];
482 par[12] = par[18];
483 par[13] = par[19];
484 par[14] = ( par[20] + par[21]) * 0.5f;
485 par[15] = ( par[22] + par[23]) * 0.5f;
486 par[16] = ( par[24] + par[25]) * 0.5f;
487 par[17] = ( par[26] + par[27]) * 0.5f;
488 par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f;
489 par[19] = ( par[32] + par[33]) * 0.5f;
490}
491
492static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
493{
494 if (full) {
495 par_mapped[33] = par[9];
496 par_mapped[32] = par[9];
497 par_mapped[31] = par[9];
498 par_mapped[30] = par[9];
499 par_mapped[29] = par[9];
500 par_mapped[28] = par[9];
501 par_mapped[27] = par[8];
502 par_mapped[26] = par[8];
503 par_mapped[25] = par[8];
504 par_mapped[24] = par[8];
505 par_mapped[23] = par[7];
506 par_mapped[22] = par[7];
507 par_mapped[21] = par[7];
508 par_mapped[20] = par[7];
509 par_mapped[19] = par[6];
510 par_mapped[18] = par[6];
511 par_mapped[17] = par[5];
512 par_mapped[16] = par[5];
513 } else {
514 par_mapped[16] = 0;
515 }
516 par_mapped[15] = par[4];
517 par_mapped[14] = par[4];
518 par_mapped[13] = par[4];
519 par_mapped[12] = par[4];
520 par_mapped[11] = par[3];
521 par_mapped[10] = par[3];
522 par_mapped[ 9] = par[2];
523 par_mapped[ 8] = par[2];
524 par_mapped[ 7] = par[2];
525 par_mapped[ 6] = par[2];
526 par_mapped[ 5] = par[1];
527 par_mapped[ 4] = par[1];
528 par_mapped[ 3] = par[1];
529 par_mapped[ 2] = par[0];
530 par_mapped[ 1] = par[0];
531 par_mapped[ 0] = par[0];
532}
533
534static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
535{
536 if (full) {
537 par_mapped[33] = par[19];
538 par_mapped[32] = par[19];
539 par_mapped[31] = par[18];
540 par_mapped[30] = par[18];
541 par_mapped[29] = par[18];
542 par_mapped[28] = par[18];
543 par_mapped[27] = par[17];
544 par_mapped[26] = par[17];
545 par_mapped[25] = par[16];
546 par_mapped[24] = par[16];
547 par_mapped[23] = par[15];
548 par_mapped[22] = par[15];
549 par_mapped[21] = par[14];
550 par_mapped[20] = par[14];
551 par_mapped[19] = par[13];
552 par_mapped[18] = par[12];
553 par_mapped[17] = par[11];
554 }
555 par_mapped[16] = par[10];
556 par_mapped[15] = par[ 9];
557 par_mapped[14] = par[ 9];
558 par_mapped[13] = par[ 8];
559 par_mapped[12] = par[ 8];
560 par_mapped[11] = par[ 7];
561 par_mapped[10] = par[ 6];
562 par_mapped[ 9] = par[ 5];
563 par_mapped[ 8] = par[ 5];
564 par_mapped[ 7] = par[ 4];
565 par_mapped[ 6] = par[ 4];
566 par_mapped[ 5] = par[ 3];
567 par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
568 par_mapped[ 3] = par[ 2];
569 par_mapped[ 2] = par[ 1];
570 par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
571 par_mapped[ 0] = par[ 0];
572}
573
574static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC])
575{
576 par[33] = par[19];
577 par[32] = par[19];
578 par[31] = par[18];
579 par[30] = par[18];
580 par[29] = par[18];
581 par[28] = par[18];
582 par[27] = par[17];
583 par[26] = par[17];
584 par[25] = par[16];
585 par[24] = par[16];
586 par[23] = par[15];
587 par[22] = par[15];
588 par[21] = par[14];
589 par[20] = par[14];
590 par[19] = par[13];
591 par[18] = par[12];
592 par[17] = par[11];
593 par[16] = par[10];
594 par[15] = par[ 9];
595 par[14] = par[ 9];
596 par[13] = par[ 8];
597 par[12] = par[ 8];
598 par[11] = par[ 7];
599 par[10] = par[ 6];
600 par[ 9] = par[ 5];
601 par[ 8] = par[ 5];
602 par[ 7] = par[ 4];
603 par[ 6] = par[ 4];
604 par[ 5] = par[ 3];
605 par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
606 par[ 3] = par[ 2];
607 par[ 2] = par[ 1];
608 par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
609 par[ 0] = par[ 0];
610}
611
612static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
613{
47d18d53
MR
614 LOCAL_ALIGNED_16(float, power, [34], [PS_QMF_TIME_SLOTS]);
615 LOCAL_ALIGNED_16(float, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
a2063901
AC
616 float *peak_decay_nrg = ps->peak_decay_nrg;
617 float *power_smooth = ps->power_smooth;
618 float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
619 float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
620 float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
621 const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
622 const float peak_decay_factor = 0.76592833836465f;
623 const float transient_impact = 1.5f;
02494787 624 const float a_smooth = 0.25f; ///< Smoothing coefficient
a2063901
AC
625 int i, k, m, n;
626 int n0 = 0, nL = 32;
a2063901 627
47d18d53
MR
628 memset(power, 0, 34 * sizeof(*power));
629
a2063901
AC
630 if (is34 != ps->is34bands_old) {
631 memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
632 memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
633 memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
634 memset(ps->delay, 0, sizeof(ps->delay));
635 memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
636 }
637
bf1945af
MR
638 for (k = 0; k < NR_BANDS[is34]; k++) {
639 int i = k_to_i[k];
640 ps->dsp.add_squares(power[i], s[k], nL - n0);
a2063901
AC
641 }
642
643 //Transient detection
644 for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
645 for (n = n0; n < nL; n++) {
646 float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
647 float denom;
648 peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
649 power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
650 peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
651 denom = transient_impact * peak_decay_diff_smooth[i];
652 transient_gain[i][n] = (denom > power_smooth[i]) ?
653 power_smooth[i] / denom : 1.0f;
654 }
655 }
656
657 //Decorrelation and transient reduction
658 // PS_AP_LINKS - 1
659 // -----
660 // | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
661 //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
662 // | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
663 // m = 0
664 //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
665 for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
666 int b = k_to_i[k];
667 float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
a2063901
AC
668 g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
669 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
670 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
671 for (m = 0; m < PS_AP_LINKS; m++) {
672 memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
a2063901 673 }
bf1945af 674 ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
0ba8ba16
DB
675 phi_fract[is34][k],
676 (const float (*)[2]) Q_fract_allpass[is34][k],
bf1945af 677 transient_gain[b], g_decay_slope, nL - n0);
a2063901
AC
678 }
679 for (; k < SHORT_DELAY_BAND[is34]; k++) {
bf1945af 680 int i = k_to_i[k];
a2063901
AC
681 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
682 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
bf1945af
MR
683 //H = delay 14
684 ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
685 transient_gain[i], nL - n0);
a2063901
AC
686 }
687 for (; k < NR_BANDS[is34]; k++) {
bf1945af 688 int i = k_to_i[k];
a2063901
AC
689 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
690 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
bf1945af
MR
691 //H = delay 1
692 ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
693 transient_gain[i], nL - n0);
a2063901
AC
694 }
695}
696
697static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
698 int8_t (*par)[PS_MAX_NR_IIDICC],
699 int num_par, int num_env, int full)
700{
701 int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
702 int e;
703 if (num_par == 20 || num_par == 11) {
704 for (e = 0; e < num_env; e++) {
705 map_idx_20_to_34(par_mapped[e], par[e], full);
706 }
707 } else if (num_par == 10 || num_par == 5) {
708 for (e = 0; e < num_env; e++) {
709 map_idx_10_to_34(par_mapped[e], par[e], full);
710 }
711 } else {
712 *p_par_mapped = par;
713 }
714}
715
716static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
717 int8_t (*par)[PS_MAX_NR_IIDICC],
718 int num_par, int num_env, int full)
719{
720 int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
721 int e;
722 if (num_par == 34 || num_par == 17) {
723 for (e = 0; e < num_env; e++) {
724 map_idx_34_to_20(par_mapped[e], par[e], full);
725 }
726 } else if (num_par == 10 || num_par == 5) {
727 for (e = 0; e < num_env; e++) {
728 map_idx_10_to_20(par_mapped[e], par[e], full);
729 }
730 } else {
731 *p_par_mapped = par;
732 }
733}
734
735static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34)
736{
bf1945af 737 int e, b, k;
a2063901
AC
738
739 float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
740 float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
741 float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
742 float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
743 int8_t *opd_hist = ps->opd_hist;
744 int8_t *ipd_hist = ps->ipd_hist;
745 int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
746 int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
747 int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
748 int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
749 int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
750 int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
751 int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
752 int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
753 const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
0ba8ba16 754 TABLE_CONST float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
a2063901
AC
755
756 //Remapping
e5902d60
MR
757 if (ps->num_env_old) {
758 memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
759 memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
760 memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
761 memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
762 memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
763 memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
764 memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
765 memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
766 }
767
a2063901
AC
768 if (is34) {
769 remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
770 remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
771 if (ps->enable_ipdopd) {
772 remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
773 remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
774 }
775 if (!ps->is34bands_old) {
776 map_val_20_to_34(H11[0][0]);
777 map_val_20_to_34(H11[1][0]);
778 map_val_20_to_34(H12[0][0]);
779 map_val_20_to_34(H12[1][0]);
780 map_val_20_to_34(H21[0][0]);
781 map_val_20_to_34(H21[1][0]);
782 map_val_20_to_34(H22[0][0]);
783 map_val_20_to_34(H22[1][0]);
784 ipdopd_reset(ipd_hist, opd_hist);
785 }
786 } else {
787 remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
788 remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
789 if (ps->enable_ipdopd) {
790 remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
791 remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
792 }
793 if (ps->is34bands_old) {
794 map_val_34_to_20(H11[0][0]);
795 map_val_34_to_20(H11[1][0]);
796 map_val_34_to_20(H12[0][0]);
797 map_val_34_to_20(H12[1][0]);
798 map_val_34_to_20(H21[0][0]);
799 map_val_34_to_20(H21[1][0]);
800 map_val_34_to_20(H22[0][0]);
801 map_val_34_to_20(H22[1][0]);
802 ipdopd_reset(ipd_hist, opd_hist);
803 }
804 }
805
806 //Mixing
807 for (e = 0; e < ps->num_env; e++) {
808 for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
809 float h11, h12, h21, h22;
810 h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
811 h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
812 h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
813 h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
814 if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) {
815 //The spec say says to only run this smoother when enable_ipdopd
816 //is set but the reference decoder appears to run it constantly
817 float h11i, h12i, h21i, h22i;
818 float ipd_adj_re, ipd_adj_im;
819 int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
820 int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
821 float opd_re = pd_re_smooth[opd_idx];
822 float opd_im = pd_im_smooth[opd_idx];
823 float ipd_re = pd_re_smooth[ipd_idx];
824 float ipd_im = pd_im_smooth[ipd_idx];
825 opd_hist[b] = opd_idx & 0x3F;
826 ipd_hist[b] = ipd_idx & 0x3F;
827
828 ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
829 ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
830 h11i = h11 * opd_im;
831 h11 = h11 * opd_re;
832 h12i = h12 * ipd_adj_im;
833 h12 = h12 * ipd_adj_re;
834 h21i = h21 * opd_im;
835 h21 = h21 * opd_re;
836 h22i = h22 * ipd_adj_im;
837 h22 = h22 * ipd_adj_re;
838 H11[1][e+1][b] = h11i;
839 H12[1][e+1][b] = h12i;
840 H21[1][e+1][b] = h21i;
841 H22[1][e+1][b] = h22i;
842 }
843 H11[0][e+1][b] = h11;
844 H12[0][e+1][b] = h12;
845 H21[0][e+1][b] = h21;
846 H22[0][e+1][b] = h22;
847 }
848 for (k = 0; k < NR_BANDS[is34]; k++) {
bf1945af
MR
849 float h[2][4];
850 float h_step[2][4];
a2063901
AC
851 int start = ps->border_position[e];
852 int stop = ps->border_position[e+1];
853 float width = 1.f / (stop - start);
854 b = k_to_i[k];
bf1945af
MR
855 h[0][0] = H11[0][e][b];
856 h[0][1] = H12[0][e][b];
857 h[0][2] = H21[0][e][b];
858 h[0][3] = H22[0][e][b];
a2063901
AC
859 if (!PS_BASELINE && ps->enable_ipdopd) {
860 //Is this necessary? ps_04_new seems unchanged
861 if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
bf1945af
MR
862 h[1][0] = -H11[1][e][b];
863 h[1][1] = -H12[1][e][b];
864 h[1][2] = -H21[1][e][b];
865 h[1][3] = -H22[1][e][b];
a2063901 866 } else {
bf1945af
MR
867 h[1][0] = H11[1][e][b];
868 h[1][1] = H12[1][e][b];
869 h[1][2] = H21[1][e][b];
870 h[1][3] = H22[1][e][b];
a2063901
AC
871 }
872 }
873 //Interpolation
bf1945af
MR
874 h_step[0][0] = (H11[0][e+1][b] - h[0][0]) * width;
875 h_step[0][1] = (H12[0][e+1][b] - h[0][1]) * width;
876 h_step[0][2] = (H21[0][e+1][b] - h[0][2]) * width;
877 h_step[0][3] = (H22[0][e+1][b] - h[0][3]) * width;
a2063901 878 if (!PS_BASELINE && ps->enable_ipdopd) {
bf1945af
MR
879 h_step[1][0] = (H11[1][e+1][b] - h[1][0]) * width;
880 h_step[1][1] = (H12[1][e+1][b] - h[1][1]) * width;
881 h_step[1][2] = (H21[1][e+1][b] - h[1][2]) * width;
882 h_step[1][3] = (H22[1][e+1][b] - h[1][3]) * width;
a2063901 883 }
bf1945af
MR
884 ps->dsp.stereo_interpolate[!PS_BASELINE && ps->enable_ipdopd](
885 l[k] + start + 1, r[k] + start + 1,
886 h, h_step, stop - start);
a2063901
AC
887 }
888 }
889}
890
891int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
892{
47d18d53
MR
893 LOCAL_ALIGNED_16(float, Lbuf, [91], [32][2]);
894 LOCAL_ALIGNED_16(float, Rbuf, [91], [32][2]);
a2063901
AC
895 const int len = 32;
896 int is34 = ps->is34bands;
897
898 top += NR_BANDS[is34] - 64;
899 memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
900 if (top < NR_ALLPASS_BANDS[is34])
901 memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
902
bf1945af 903 hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
0ba8ba16 904 decorrelation(ps, Rbuf, (const float (*)[32][2]) Lbuf, is34);
a2063901 905 stereo_processing(ps, Lbuf, Rbuf, is34);
bf1945af
MR
906 hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
907 hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
a2063901
AC
908
909 return 0;
910}
911
912#define PS_INIT_VLC_STATIC(num, size) \
913 INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
914 ps_tmp[num].ps_bits, 1, 1, \
915 ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
916 size);
917
918#define PS_VLC_ROW(name) \
919 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
920
921av_cold void ff_ps_init(void) {
922 // Syntax initialization
923 static const struct {
924 const void *ps_codes, *ps_bits;
925 const unsigned int table_size, elem_size;
926 } ps_tmp[] = {
927 PS_VLC_ROW(huff_iid_df1),
928 PS_VLC_ROW(huff_iid_dt1),
929 PS_VLC_ROW(huff_iid_df0),
930 PS_VLC_ROW(huff_iid_dt0),
931 PS_VLC_ROW(huff_icc_df),
932 PS_VLC_ROW(huff_icc_dt),
933 PS_VLC_ROW(huff_ipd_df),
934 PS_VLC_ROW(huff_ipd_dt),
935 PS_VLC_ROW(huff_opd_df),
936 PS_VLC_ROW(huff_opd_dt),
937 };
938
939 PS_INIT_VLC_STATIC(0, 1544);
940 PS_INIT_VLC_STATIC(1, 832);
941 PS_INIT_VLC_STATIC(2, 1024);
942 PS_INIT_VLC_STATIC(3, 1036);
943 PS_INIT_VLC_STATIC(4, 544);
944 PS_INIT_VLC_STATIC(5, 544);
945 PS_INIT_VLC_STATIC(6, 512);
946 PS_INIT_VLC_STATIC(7, 512);
947 PS_INIT_VLC_STATIC(8, 512);
948 PS_INIT_VLC_STATIC(9, 512);
949
950 ps_tableinit();
951}
952
953av_cold void ff_ps_ctx_init(PSContext *ps)
954{
bf1945af 955 ff_psdsp_init(&ps->dsp);
a2063901 956}