00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdint.h>
00023 #include "libavutil/common.h"
00024 #include "libavutil/mathematics.h"
00025 #include "avcodec.h"
00026 #include "get_bits.h"
00027 #include "aacps.h"
00028 #include "aacps_tablegen.h"
00029 #include "aacpsdata.c"
00030 #include "dsputil.h"
00031
00032 #define PS_BASELINE 0
00033
00034
00035
00036 #define numQMFSlots 32 //numTimeSlots * RATE
00037
00038 static const int8_t num_env_tab[2][4] = {
00039 { 0, 1, 2, 4, },
00040 { 1, 2, 3, 4, },
00041 };
00042
00043 static const int8_t nr_iidicc_par_tab[] = {
00044 10, 20, 34, 10, 20, 34,
00045 };
00046
00047 static const int8_t nr_iidopd_par_tab[] = {
00048 5, 11, 17, 5, 11, 17,
00049 };
00050
00051 enum {
00052 huff_iid_df1,
00053 huff_iid_dt1,
00054 huff_iid_df0,
00055 huff_iid_dt0,
00056 huff_icc_df,
00057 huff_icc_dt,
00058 huff_ipd_df,
00059 huff_ipd_dt,
00060 huff_opd_df,
00061 huff_opd_dt,
00062 };
00063
00064 static const int huff_iid[] = {
00065 huff_iid_df0,
00066 huff_iid_df1,
00067 huff_iid_dt0,
00068 huff_iid_dt1,
00069 };
00070
00071 static VLC vlc_ps[10];
00072
00073 #define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
00074 \
00086 static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
00087 int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
00088 { \
00089 int b, num = ps->nr_ ## PAR ## _par; \
00090 VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
00091 if (dt) { \
00092 int e_prev = e ? e - 1 : ps->num_env_old - 1; \
00093 e_prev = FFMAX(e_prev, 0); \
00094 for (b = 0; b < num; b++) { \
00095 int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
00096 if (MASK) val &= MASK; \
00097 PAR[e][b] = val; \
00098 if (ERR_CONDITION) \
00099 goto err; \
00100 } \
00101 } else { \
00102 int val = 0; \
00103 for (b = 0; b < num; b++) { \
00104 val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
00105 if (MASK) val &= MASK; \
00106 PAR[e][b] = val; \
00107 if (ERR_CONDITION) \
00108 goto err; \
00109 } \
00110 } \
00111 return 0; \
00112 err: \
00113 av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
00114 return -1; \
00115 }
00116
00117 READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
00118 READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U)
00119 READ_PAR_DATA(ipdopd, 0, 0x07, 0)
00120
00121 static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
00122 {
00123 int e;
00124 int count = get_bits_count(gb);
00125
00126 if (ps_extension_id)
00127 return 0;
00128
00129 ps->enable_ipdopd = get_bits1(gb);
00130 if (ps->enable_ipdopd) {
00131 for (e = 0; e < ps->num_env; e++) {
00132 int dt = get_bits1(gb);
00133 read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
00134 dt = get_bits1(gb);
00135 read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
00136 }
00137 }
00138 skip_bits1(gb);
00139 return get_bits_count(gb) - count;
00140 }
00141
00142 static void ipdopd_reset(int8_t *opd_hist, int8_t *ipd_hist)
00143 {
00144 int i;
00145 for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
00146 opd_hist[i] = 0;
00147 ipd_hist[i] = 0;
00148 }
00149 }
00150
00151 int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
00152 {
00153 int e;
00154 int bit_count_start = get_bits_count(gb_host);
00155 int header;
00156 int bits_consumed;
00157 GetBitContext gbc = *gb_host, *gb = &gbc;
00158
00159 header = get_bits1(gb);
00160 if (header) {
00161 ps->enable_iid = get_bits1(gb);
00162 if (ps->enable_iid) {
00163 int iid_mode = get_bits(gb, 3);
00164 if (iid_mode > 5) {
00165 av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
00166 iid_mode);
00167 goto err;
00168 }
00169 ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
00170 ps->iid_quant = iid_mode > 2;
00171 ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
00172 }
00173 ps->enable_icc = get_bits1(gb);
00174 if (ps->enable_icc) {
00175 ps->icc_mode = get_bits(gb, 3);
00176 if (ps->icc_mode > 5) {
00177 av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
00178 ps->icc_mode);
00179 goto err;
00180 }
00181 ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
00182 }
00183 ps->enable_ext = get_bits1(gb);
00184 }
00185
00186 ps->frame_class = get_bits1(gb);
00187 ps->num_env_old = ps->num_env;
00188 ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)];
00189
00190 ps->border_position[0] = -1;
00191 if (ps->frame_class) {
00192 for (e = 1; e <= ps->num_env; e++)
00193 ps->border_position[e] = get_bits(gb, 5);
00194 } else
00195 for (e = 1; e <= ps->num_env; e++)
00196 ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
00197
00198 if (ps->enable_iid) {
00199 for (e = 0; e < ps->num_env; e++) {
00200 int dt = get_bits1(gb);
00201 if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
00202 goto err;
00203 }
00204 } else
00205 memset(ps->iid_par, 0, sizeof(ps->iid_par));
00206
00207 if (ps->enable_icc)
00208 for (e = 0; e < ps->num_env; e++) {
00209 int dt = get_bits1(gb);
00210 if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
00211 goto err;
00212 }
00213 else
00214 memset(ps->icc_par, 0, sizeof(ps->icc_par));
00215
00216 if (ps->enable_ext) {
00217 int cnt = get_bits(gb, 4);
00218 if (cnt == 15) {
00219 cnt += get_bits(gb, 8);
00220 }
00221 cnt *= 8;
00222 while (cnt > 7) {
00223 int ps_extension_id = get_bits(gb, 2);
00224 cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
00225 }
00226 if (cnt < 0) {
00227 av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
00228 goto err;
00229 }
00230 skip_bits(gb, cnt);
00231 }
00232
00233 ps->enable_ipdopd &= !PS_BASELINE;
00234
00235
00236 if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
00237
00238 int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
00239 if (source >= 0 && source != ps->num_env) {
00240 if (ps->enable_iid) {
00241 memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
00242 }
00243 if (ps->enable_icc) {
00244 memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
00245 }
00246 if (ps->enable_ipdopd) {
00247 memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
00248 memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
00249 }
00250 }
00251 ps->num_env++;
00252 ps->border_position[ps->num_env] = numQMFSlots - 1;
00253 }
00254
00255
00256 ps->is34bands_old = ps->is34bands;
00257 if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
00258 ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
00259 (ps->enable_icc && ps->nr_icc_par == 34);
00260
00261
00262 if (!ps->enable_ipdopd) {
00263 memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
00264 memset(ps->opd_par, 0, sizeof(ps->opd_par));
00265 }
00266
00267 if (header)
00268 ps->start = 1;
00269
00270 bits_consumed = get_bits_count(gb) - bit_count_start;
00271 if (bits_consumed <= bits_left) {
00272 skip_bits_long(gb_host, bits_consumed);
00273 return bits_consumed;
00274 }
00275 av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
00276 err:
00277 ps->start = 0;
00278 skip_bits_long(gb_host, bits_left);
00279 memset(ps->iid_par, 0, sizeof(ps->iid_par));
00280 memset(ps->icc_par, 0, sizeof(ps->icc_par));
00281 memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
00282 memset(ps->opd_par, 0, sizeof(ps->opd_par));
00283 return bits_left;
00284 }
00285
00288 static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[8], int len, int reverse)
00289 {
00290 int i, j;
00291 for (i = 0; i < len; i++, in++) {
00292 float re_in = filter[6] * in[6][0];
00293 float re_op = 0.0f;
00294 float im_in = filter[6] * in[6][1];
00295 float im_op = 0.0f;
00296 for (j = 0; j < 6; j += 2) {
00297 re_op += filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
00298 im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
00299 }
00300 out[ reverse][i][0] = re_in + re_op;
00301 out[ reverse][i][1] = im_in + im_op;
00302 out[!reverse][i][0] = re_in - re_op;
00303 out[!reverse][i][1] = im_in - im_op;
00304 }
00305 }
00306
00308 static void hybrid6_cx(PSDSPContext *dsp, float (*in)[2], float (*out)[32][2], const float (*filter)[8][2], int len)
00309 {
00310 int i;
00311 int N = 8;
00312 LOCAL_ALIGNED_16(float, temp, [8], [2]);
00313
00314 for (i = 0; i < len; i++, in++) {
00315 dsp->hybrid_analysis(temp, in, filter, 1, N);
00316 out[0][i][0] = temp[6][0];
00317 out[0][i][1] = temp[6][1];
00318 out[1][i][0] = temp[7][0];
00319 out[1][i][1] = temp[7][1];
00320 out[2][i][0] = temp[0][0];
00321 out[2][i][1] = temp[0][1];
00322 out[3][i][0] = temp[1][0];
00323 out[3][i][1] = temp[1][1];
00324 out[4][i][0] = temp[2][0] + temp[5][0];
00325 out[4][i][1] = temp[2][1] + temp[5][1];
00326 out[5][i][0] = temp[3][0] + temp[4][0];
00327 out[5][i][1] = temp[3][1] + temp[4][1];
00328 }
00329 }
00330
00331 static void hybrid4_8_12_cx(PSDSPContext *dsp, float (*in)[2], float (*out)[32][2], const float (*filter)[8][2], int N, int len)
00332 {
00333 int i;
00334
00335 for (i = 0; i < len; i++, in++) {
00336 dsp->hybrid_analysis(out[0] + i, in, filter, 32, N);
00337 }
00338 }
00339
00340 static void hybrid_analysis(PSDSPContext *dsp, float out[91][32][2],
00341 float in[5][44][2], float L[2][38][64],
00342 int is34, int len)
00343 {
00344 int i, j;
00345 for (i = 0; i < 5; i++) {
00346 for (j = 0; j < 38; j++) {
00347 in[i][j+6][0] = L[0][j][i];
00348 in[i][j+6][1] = L[1][j][i];
00349 }
00350 }
00351 if (is34) {
00352 hybrid4_8_12_cx(dsp, in[0], out, f34_0_12, 12, len);
00353 hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8, 8, len);
00354 hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4, 4, len);
00355 hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4, 4, len);
00356 hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4, 4, len);
00357 dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
00358 } else {
00359 hybrid6_cx(dsp, in[0], out, f20_0_8, len);
00360 hybrid2_re(in[1], out+6, g1_Q2, len, 1);
00361 hybrid2_re(in[2], out+8, g1_Q2, len, 0);
00362 dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
00363 }
00364
00365 for (i = 0; i < 5; i++) {
00366 memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
00367 }
00368 }
00369
00370 static void hybrid_synthesis(PSDSPContext *dsp, float out[2][38][64],
00371 float in[91][32][2], int is34, int len)
00372 {
00373 int i, n;
00374 if (is34) {
00375 for (n = 0; n < len; n++) {
00376 memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
00377 memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
00378 for (i = 0; i < 12; i++) {
00379 out[0][n][0] += in[ i][n][0];
00380 out[1][n][0] += in[ i][n][1];
00381 }
00382 for (i = 0; i < 8; i++) {
00383 out[0][n][1] += in[12+i][n][0];
00384 out[1][n][1] += in[12+i][n][1];
00385 }
00386 for (i = 0; i < 4; i++) {
00387 out[0][n][2] += in[20+i][n][0];
00388 out[1][n][2] += in[20+i][n][1];
00389 out[0][n][3] += in[24+i][n][0];
00390 out[1][n][3] += in[24+i][n][1];
00391 out[0][n][4] += in[28+i][n][0];
00392 out[1][n][4] += in[28+i][n][1];
00393 }
00394 }
00395 dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
00396 } else {
00397 for (n = 0; n < len; n++) {
00398 out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
00399 in[3][n][0] + in[4][n][0] + in[5][n][0];
00400 out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
00401 in[3][n][1] + in[4][n][1] + in[5][n][1];
00402 out[0][n][1] = in[6][n][0] + in[7][n][0];
00403 out[1][n][1] = in[6][n][1] + in[7][n][1];
00404 out[0][n][2] = in[8][n][0] + in[9][n][0];
00405 out[1][n][2] = in[8][n][1] + in[9][n][1];
00406 }
00407 dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
00408 }
00409 }
00410
00412 #define DECAY_SLOPE 0.05f
00414 static const int NR_PAR_BANDS[] = { 20, 34 };
00416 static const int NR_BANDS[] = { 71, 91 };
00418 static const int DECAY_CUTOFF[] = { 10, 32 };
00420 static const int NR_ALLPASS_BANDS[] = { 30, 50 };
00422 static const int SHORT_DELAY_BAND[] = { 42, 62 };
00423
00425 static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
00426 {
00427 int b;
00428 if (full)
00429 b = 9;
00430 else {
00431 b = 4;
00432 par_mapped[10] = 0;
00433 }
00434 for (; b >= 0; b--) {
00435 par_mapped[2*b+1] = par_mapped[2*b] = par[b];
00436 }
00437 }
00438
00439 static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
00440 {
00441 par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3;
00442 par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3;
00443 par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3;
00444 par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3;
00445 par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2;
00446 par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2;
00447 par_mapped[ 6] = par[10];
00448 par_mapped[ 7] = par[11];
00449 par_mapped[ 8] = ( par[12] + par[13]) / 2;
00450 par_mapped[ 9] = ( par[14] + par[15]) / 2;
00451 par_mapped[10] = par[16];
00452 if (full) {
00453 par_mapped[11] = par[17];
00454 par_mapped[12] = par[18];
00455 par_mapped[13] = par[19];
00456 par_mapped[14] = ( par[20] + par[21]) / 2;
00457 par_mapped[15] = ( par[22] + par[23]) / 2;
00458 par_mapped[16] = ( par[24] + par[25]) / 2;
00459 par_mapped[17] = ( par[26] + par[27]) / 2;
00460 par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4;
00461 par_mapped[19] = ( par[32] + par[33]) / 2;
00462 }
00463 }
00464
00465 static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC])
00466 {
00467 par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f;
00468 par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f;
00469 par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f;
00470 par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f;
00471 par[ 4] = ( par[ 6] + par[ 7]) * 0.5f;
00472 par[ 5] = ( par[ 8] + par[ 9]) * 0.5f;
00473 par[ 6] = par[10];
00474 par[ 7] = par[11];
00475 par[ 8] = ( par[12] + par[13]) * 0.5f;
00476 par[ 9] = ( par[14] + par[15]) * 0.5f;
00477 par[10] = par[16];
00478 par[11] = par[17];
00479 par[12] = par[18];
00480 par[13] = par[19];
00481 par[14] = ( par[20] + par[21]) * 0.5f;
00482 par[15] = ( par[22] + par[23]) * 0.5f;
00483 par[16] = ( par[24] + par[25]) * 0.5f;
00484 par[17] = ( par[26] + par[27]) * 0.5f;
00485 par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f;
00486 par[19] = ( par[32] + par[33]) * 0.5f;
00487 }
00488
00489 static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
00490 {
00491 if (full) {
00492 par_mapped[33] = par[9];
00493 par_mapped[32] = par[9];
00494 par_mapped[31] = par[9];
00495 par_mapped[30] = par[9];
00496 par_mapped[29] = par[9];
00497 par_mapped[28] = par[9];
00498 par_mapped[27] = par[8];
00499 par_mapped[26] = par[8];
00500 par_mapped[25] = par[8];
00501 par_mapped[24] = par[8];
00502 par_mapped[23] = par[7];
00503 par_mapped[22] = par[7];
00504 par_mapped[21] = par[7];
00505 par_mapped[20] = par[7];
00506 par_mapped[19] = par[6];
00507 par_mapped[18] = par[6];
00508 par_mapped[17] = par[5];
00509 par_mapped[16] = par[5];
00510 } else {
00511 par_mapped[16] = 0;
00512 }
00513 par_mapped[15] = par[4];
00514 par_mapped[14] = par[4];
00515 par_mapped[13] = par[4];
00516 par_mapped[12] = par[4];
00517 par_mapped[11] = par[3];
00518 par_mapped[10] = par[3];
00519 par_mapped[ 9] = par[2];
00520 par_mapped[ 8] = par[2];
00521 par_mapped[ 7] = par[2];
00522 par_mapped[ 6] = par[2];
00523 par_mapped[ 5] = par[1];
00524 par_mapped[ 4] = par[1];
00525 par_mapped[ 3] = par[1];
00526 par_mapped[ 2] = par[0];
00527 par_mapped[ 1] = par[0];
00528 par_mapped[ 0] = par[0];
00529 }
00530
00531 static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
00532 {
00533 if (full) {
00534 par_mapped[33] = par[19];
00535 par_mapped[32] = par[19];
00536 par_mapped[31] = par[18];
00537 par_mapped[30] = par[18];
00538 par_mapped[29] = par[18];
00539 par_mapped[28] = par[18];
00540 par_mapped[27] = par[17];
00541 par_mapped[26] = par[17];
00542 par_mapped[25] = par[16];
00543 par_mapped[24] = par[16];
00544 par_mapped[23] = par[15];
00545 par_mapped[22] = par[15];
00546 par_mapped[21] = par[14];
00547 par_mapped[20] = par[14];
00548 par_mapped[19] = par[13];
00549 par_mapped[18] = par[12];
00550 par_mapped[17] = par[11];
00551 }
00552 par_mapped[16] = par[10];
00553 par_mapped[15] = par[ 9];
00554 par_mapped[14] = par[ 9];
00555 par_mapped[13] = par[ 8];
00556 par_mapped[12] = par[ 8];
00557 par_mapped[11] = par[ 7];
00558 par_mapped[10] = par[ 6];
00559 par_mapped[ 9] = par[ 5];
00560 par_mapped[ 8] = par[ 5];
00561 par_mapped[ 7] = par[ 4];
00562 par_mapped[ 6] = par[ 4];
00563 par_mapped[ 5] = par[ 3];
00564 par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
00565 par_mapped[ 3] = par[ 2];
00566 par_mapped[ 2] = par[ 1];
00567 par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
00568 par_mapped[ 0] = par[ 0];
00569 }
00570
00571 static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC])
00572 {
00573 par[33] = par[19];
00574 par[32] = par[19];
00575 par[31] = par[18];
00576 par[30] = par[18];
00577 par[29] = par[18];
00578 par[28] = par[18];
00579 par[27] = par[17];
00580 par[26] = par[17];
00581 par[25] = par[16];
00582 par[24] = par[16];
00583 par[23] = par[15];
00584 par[22] = par[15];
00585 par[21] = par[14];
00586 par[20] = par[14];
00587 par[19] = par[13];
00588 par[18] = par[12];
00589 par[17] = par[11];
00590 par[16] = par[10];
00591 par[15] = par[ 9];
00592 par[14] = par[ 9];
00593 par[13] = par[ 8];
00594 par[12] = par[ 8];
00595 par[11] = par[ 7];
00596 par[10] = par[ 6];
00597 par[ 9] = par[ 5];
00598 par[ 8] = par[ 5];
00599 par[ 7] = par[ 4];
00600 par[ 6] = par[ 4];
00601 par[ 5] = par[ 3];
00602 par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
00603 par[ 3] = par[ 2];
00604 par[ 2] = par[ 1];
00605 par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
00606 par[ 0] = par[ 0];
00607 }
00608
00609 static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
00610 {
00611 LOCAL_ALIGNED_16(float, power, [34], [PS_QMF_TIME_SLOTS]);
00612 LOCAL_ALIGNED_16(float, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
00613 float *peak_decay_nrg = ps->peak_decay_nrg;
00614 float *power_smooth = ps->power_smooth;
00615 float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
00616 float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
00617 float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
00618 const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
00619 const float peak_decay_factor = 0.76592833836465f;
00620 const float transient_impact = 1.5f;
00621 const float a_smooth = 0.25f;
00622 int i, k, m, n;
00623 int n0 = 0, nL = 32;
00624
00625 memset(power, 0, 34 * sizeof(*power));
00626
00627 if (is34 != ps->is34bands_old) {
00628 memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
00629 memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
00630 memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
00631 memset(ps->delay, 0, sizeof(ps->delay));
00632 memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
00633 }
00634
00635 for (k = 0; k < NR_BANDS[is34]; k++) {
00636 int i = k_to_i[k];
00637 ps->dsp.add_squares(power[i], s[k], nL - n0);
00638 }
00639
00640
00641 for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
00642 for (n = n0; n < nL; n++) {
00643 float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
00644 float denom;
00645 peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
00646 power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
00647 peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
00648 denom = transient_impact * peak_decay_diff_smooth[i];
00649 transient_gain[i][n] = (denom > power_smooth[i]) ?
00650 power_smooth[i] / denom : 1.0f;
00651 }
00652 }
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662 for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
00663 int b = k_to_i[k];
00664 float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
00665 g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
00666 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
00667 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
00668 for (m = 0; m < PS_AP_LINKS; m++) {
00669 memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
00670 }
00671 ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
00672 phi_fract[is34][k], Q_fract_allpass[is34][k],
00673 transient_gain[b], g_decay_slope, nL - n0);
00674 }
00675 for (; k < SHORT_DELAY_BAND[is34]; k++) {
00676 int i = k_to_i[k];
00677 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
00678 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
00679
00680 ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
00681 transient_gain[i], nL - n0);
00682 }
00683 for (; k < NR_BANDS[is34]; k++) {
00684 int i = k_to_i[k];
00685 memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
00686 memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
00687
00688 ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
00689 transient_gain[i], nL - n0);
00690 }
00691 }
00692
00693 static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
00694 int8_t (*par)[PS_MAX_NR_IIDICC],
00695 int num_par, int num_env, int full)
00696 {
00697 int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
00698 int e;
00699 if (num_par == 20 || num_par == 11) {
00700 for (e = 0; e < num_env; e++) {
00701 map_idx_20_to_34(par_mapped[e], par[e], full);
00702 }
00703 } else if (num_par == 10 || num_par == 5) {
00704 for (e = 0; e < num_env; e++) {
00705 map_idx_10_to_34(par_mapped[e], par[e], full);
00706 }
00707 } else {
00708 *p_par_mapped = par;
00709 }
00710 }
00711
00712 static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
00713 int8_t (*par)[PS_MAX_NR_IIDICC],
00714 int num_par, int num_env, int full)
00715 {
00716 int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
00717 int e;
00718 if (num_par == 34 || num_par == 17) {
00719 for (e = 0; e < num_env; e++) {
00720 map_idx_34_to_20(par_mapped[e], par[e], full);
00721 }
00722 } else if (num_par == 10 || num_par == 5) {
00723 for (e = 0; e < num_env; e++) {
00724 map_idx_10_to_20(par_mapped[e], par[e], full);
00725 }
00726 } else {
00727 *p_par_mapped = par;
00728 }
00729 }
00730
00731 static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34)
00732 {
00733 int e, b, k;
00734
00735 float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
00736 float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
00737 float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
00738 float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
00739 int8_t *opd_hist = ps->opd_hist;
00740 int8_t *ipd_hist = ps->ipd_hist;
00741 int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
00742 int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
00743 int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
00744 int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
00745 int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
00746 int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
00747 int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
00748 int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
00749 const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
00750 const float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
00751
00752
00753 if (ps->num_env_old) {
00754 memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
00755 memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
00756 memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
00757 memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
00758 memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
00759 memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
00760 memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
00761 memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
00762 }
00763
00764 if (is34) {
00765 remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
00766 remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
00767 if (ps->enable_ipdopd) {
00768 remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
00769 remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
00770 }
00771 if (!ps->is34bands_old) {
00772 map_val_20_to_34(H11[0][0]);
00773 map_val_20_to_34(H11[1][0]);
00774 map_val_20_to_34(H12[0][0]);
00775 map_val_20_to_34(H12[1][0]);
00776 map_val_20_to_34(H21[0][0]);
00777 map_val_20_to_34(H21[1][0]);
00778 map_val_20_to_34(H22[0][0]);
00779 map_val_20_to_34(H22[1][0]);
00780 ipdopd_reset(ipd_hist, opd_hist);
00781 }
00782 } else {
00783 remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
00784 remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
00785 if (ps->enable_ipdopd) {
00786 remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
00787 remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
00788 }
00789 if (ps->is34bands_old) {
00790 map_val_34_to_20(H11[0][0]);
00791 map_val_34_to_20(H11[1][0]);
00792 map_val_34_to_20(H12[0][0]);
00793 map_val_34_to_20(H12[1][0]);
00794 map_val_34_to_20(H21[0][0]);
00795 map_val_34_to_20(H21[1][0]);
00796 map_val_34_to_20(H22[0][0]);
00797 map_val_34_to_20(H22[1][0]);
00798 ipdopd_reset(ipd_hist, opd_hist);
00799 }
00800 }
00801
00802
00803 for (e = 0; e < ps->num_env; e++) {
00804 for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
00805 float h11, h12, h21, h22;
00806 h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
00807 h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
00808 h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
00809 h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
00810 if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) {
00811
00812
00813 float h11i, h12i, h21i, h22i;
00814 float ipd_adj_re, ipd_adj_im;
00815 int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
00816 int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
00817 float opd_re = pd_re_smooth[opd_idx];
00818 float opd_im = pd_im_smooth[opd_idx];
00819 float ipd_re = pd_re_smooth[ipd_idx];
00820 float ipd_im = pd_im_smooth[ipd_idx];
00821 opd_hist[b] = opd_idx & 0x3F;
00822 ipd_hist[b] = ipd_idx & 0x3F;
00823
00824 ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
00825 ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
00826 h11i = h11 * opd_im;
00827 h11 = h11 * opd_re;
00828 h12i = h12 * ipd_adj_im;
00829 h12 = h12 * ipd_adj_re;
00830 h21i = h21 * opd_im;
00831 h21 = h21 * opd_re;
00832 h22i = h22 * ipd_adj_im;
00833 h22 = h22 * ipd_adj_re;
00834 H11[1][e+1][b] = h11i;
00835 H12[1][e+1][b] = h12i;
00836 H21[1][e+1][b] = h21i;
00837 H22[1][e+1][b] = h22i;
00838 }
00839 H11[0][e+1][b] = h11;
00840 H12[0][e+1][b] = h12;
00841 H21[0][e+1][b] = h21;
00842 H22[0][e+1][b] = h22;
00843 }
00844 for (k = 0; k < NR_BANDS[is34]; k++) {
00845 float h[2][4];
00846 float h_step[2][4];
00847 int start = ps->border_position[e];
00848 int stop = ps->border_position[e+1];
00849 float width = 1.f / (stop - start);
00850 b = k_to_i[k];
00851 h[0][0] = H11[0][e][b];
00852 h[0][1] = H12[0][e][b];
00853 h[0][2] = H21[0][e][b];
00854 h[0][3] = H22[0][e][b];
00855 if (!PS_BASELINE && ps->enable_ipdopd) {
00856
00857 if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
00858 h[1][0] = -H11[1][e][b];
00859 h[1][1] = -H12[1][e][b];
00860 h[1][2] = -H21[1][e][b];
00861 h[1][3] = -H22[1][e][b];
00862 } else {
00863 h[1][0] = H11[1][e][b];
00864 h[1][1] = H12[1][e][b];
00865 h[1][2] = H21[1][e][b];
00866 h[1][3] = H22[1][e][b];
00867 }
00868 }
00869
00870 h_step[0][0] = (H11[0][e+1][b] - h[0][0]) * width;
00871 h_step[0][1] = (H12[0][e+1][b] - h[0][1]) * width;
00872 h_step[0][2] = (H21[0][e+1][b] - h[0][2]) * width;
00873 h_step[0][3] = (H22[0][e+1][b] - h[0][3]) * width;
00874 if (!PS_BASELINE && ps->enable_ipdopd) {
00875 h_step[1][0] = (H11[1][e+1][b] - h[1][0]) * width;
00876 h_step[1][1] = (H12[1][e+1][b] - h[1][1]) * width;
00877 h_step[1][2] = (H21[1][e+1][b] - h[1][2]) * width;
00878 h_step[1][3] = (H22[1][e+1][b] - h[1][3]) * width;
00879 }
00880 ps->dsp.stereo_interpolate[!PS_BASELINE && ps->enable_ipdopd](
00881 l[k] + start + 1, r[k] + start + 1,
00882 h, h_step, stop - start);
00883 }
00884 }
00885 }
00886
00887 int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
00888 {
00889 LOCAL_ALIGNED_16(float, Lbuf, [91], [32][2]);
00890 LOCAL_ALIGNED_16(float, Rbuf, [91], [32][2]);
00891 const int len = 32;
00892 int is34 = ps->is34bands;
00893
00894 top += NR_BANDS[is34] - 64;
00895 memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
00896 if (top < NR_ALLPASS_BANDS[is34])
00897 memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
00898
00899 hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
00900 decorrelation(ps, Rbuf, Lbuf, is34);
00901 stereo_processing(ps, Lbuf, Rbuf, is34);
00902 hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
00903 hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
00904
00905 return 0;
00906 }
00907
00908 #define PS_INIT_VLC_STATIC(num, size) \
00909 INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
00910 ps_tmp[num].ps_bits, 1, 1, \
00911 ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
00912 size);
00913
00914 #define PS_VLC_ROW(name) \
00915 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
00916
00917 av_cold void ff_ps_init(void) {
00918
00919 static const struct {
00920 const void *ps_codes, *ps_bits;
00921 const unsigned int table_size, elem_size;
00922 } ps_tmp[] = {
00923 PS_VLC_ROW(huff_iid_df1),
00924 PS_VLC_ROW(huff_iid_dt1),
00925 PS_VLC_ROW(huff_iid_df0),
00926 PS_VLC_ROW(huff_iid_dt0),
00927 PS_VLC_ROW(huff_icc_df),
00928 PS_VLC_ROW(huff_icc_dt),
00929 PS_VLC_ROW(huff_ipd_df),
00930 PS_VLC_ROW(huff_ipd_dt),
00931 PS_VLC_ROW(huff_opd_df),
00932 PS_VLC_ROW(huff_opd_dt),
00933 };
00934
00935 PS_INIT_VLC_STATIC(0, 1544);
00936 PS_INIT_VLC_STATIC(1, 832);
00937 PS_INIT_VLC_STATIC(2, 1024);
00938 PS_INIT_VLC_STATIC(3, 1036);
00939 PS_INIT_VLC_STATIC(4, 544);
00940 PS_INIT_VLC_STATIC(5, 544);
00941 PS_INIT_VLC_STATIC(6, 512);
00942 PS_INIT_VLC_STATIC(7, 512);
00943 PS_INIT_VLC_STATIC(8, 512);
00944 PS_INIT_VLC_STATIC(9, 512);
00945
00946 ps_tableinit();
00947 }
00948
00949 av_cold void ff_ps_ctx_init(PSContext *ps)
00950 {
00951 ff_psdsp_init(&ps->dsp);
00952 }