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