From a20639017bfca0490bb1799575714f22bf470b4f Mon Sep 17 00:00:00 2001 From: Alex Converse Date: Sat, 19 Jun 2010 14:14:51 +0000 Subject: [PATCH] Add HE-AAC v2 support to the AAC decoder. Originally committed as revision 23647 to svn://svn.ffmpeg.org/ffmpeg/trunk --- Changelog | 1 + libavcodec/Makefile | 3 +- libavcodec/aacdec.c | 16 +- libavcodec/aacsbr.c | 30 +- libavcodec/avcodec.h | 2 +- libavcodec/mpeg4audio.c | 8 + libavcodec/ps.c | 1124 ++++++++++++++++++++++++++++++++++++++ libavcodec/ps.h | 80 +++ libavcodec/ps_tablegen.c | 93 ++++ libavcodec/ps_tablegen.h | 221 ++++++++ libavcodec/psdata.c | 163 ++++++ libavcodec/sbr.h | 4 +- 12 files changed, 1733 insertions(+), 12 deletions(-) create mode 100644 libavcodec/ps.c create mode 100644 libavcodec/ps.h create mode 100644 libavcodec/ps_tablegen.c create mode 100644 libavcodec/ps_tablegen.h create mode 100644 libavcodec/psdata.c diff --git a/Changelog b/Changelog index e510b10104..aa9b08bd93 100644 --- a/Changelog +++ b/Changelog @@ -11,6 +11,7 @@ version : - CODEC_CAP_EXPERIMENTAL added - Demuxer for On2's IVF format - Pictor/PC Paint decoder +- HE-AAC v2 decoder diff --git a/libavcodec/Makefile b/libavcodec/Makefile index a8bec0a57f..ecc7856f39 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -42,7 +42,7 @@ OBJS-$(CONFIG_VAAPI) += vaapi.o OBJS-$(CONFIG_VDPAU) += vdpau.o # decoders/encoders/hardware accelerators -OBJS-$(CONFIG_AAC_DECODER) += aacdec.o aactab.o aacsbr.o +OBJS-$(CONFIG_AAC_DECODER) += aacdec.o aactab.o aacsbr.o ps.o OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o \ aacpsy.o aactab.o \ psymodel.o iirfilter.o \ @@ -667,5 +667,6 @@ $(SUBDIR)mpegaudiodec.o: $(SUBDIR)mpegaudio_tables.h $(SUBDIR)mpegaudiodec_float.o: $(SUBDIR)mpegaudio_tables.h $(SUBDIR)motionpixels.o: $(SUBDIR)motionpixels_tables.h $(SUBDIR)pcm.o: $(SUBDIR)pcm_tables.h +$(SUBDIR)ps.o: $(SUBDIR)ps_tables.h $(SUBDIR)qdm2.o: $(SUBDIR)qdm2_tables.h endif diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index ba84678aa8..3c409a69cf 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -67,7 +67,7 @@ * Y (not in this code) Layer-2 * Y (not in this code) Layer-3 * N SinuSoidal Coding (Transient, Sinusoid, Noise) - * N (planned) Parametric Stereo + * Y Parametric Stereo * N Direct Stream Transfer * * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication. @@ -200,7 +200,8 @@ static av_cold int che_configure(AACContext *ac, ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr); if (type != TYPE_CCE) { ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret; - if (type == TYPE_CPE) { + if (type == TYPE_CPE || + (type == TYPE_SCE && ac->m4ac.ps == 1)) { ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret; } } @@ -228,6 +229,7 @@ static av_cold int output_configure(AACContext *ac, AVCodecContext *avctx = ac->avctx; int i, type, channels = 0, ret; + if (new_che_pos != che_pos) memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])); if (channel_config) { @@ -471,6 +473,8 @@ static int decode_audio_specific_config(AACContext *ac, void *data, av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); return -1; } + if (ac->m4ac.sbr == 1 && ac->m4ac.ps == -1) + ac->m4ac.ps = 1; skip_bits_long(&gb, i); @@ -1667,6 +1671,10 @@ static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt, av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n"); skip_bits_long(gb, 8 * cnt - 4); return res; + } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) { + ac->m4ac.sbr = 1; + ac->m4ac.ps = 1; + output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured); } else { ac->m4ac.sbr = 1; } @@ -1946,8 +1954,10 @@ static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb) } else if (ac->output_configured != OC_LOCKED) { ac->output_configured = OC_NONE; } - if (ac->output_configured != OC_LOCKED) + if (ac->output_configured != OC_LOCKED) { ac->m4ac.sbr = -1; + ac->m4ac.ps = -1; + } ac->m4ac.sample_rate = hdr_info.sample_rate; ac->m4ac.sampling_index = hdr_info.sampling_index; ac->m4ac.object_type = hdr_info.object_type; diff --git a/libavcodec/aacsbr.c b/libavcodec/aacsbr.c index b6631a07fc..c8ff299fe0 100644 --- a/libavcodec/aacsbr.c +++ b/libavcodec/aacsbr.c @@ -31,6 +31,7 @@ #include "aacsbr.h" #include "aacsbrdata.h" #include "fft.h" +#include "ps.h" #include #include @@ -120,6 +121,8 @@ av_cold void ff_aac_sbr_init(void) for (n = 0; n < 320; n++) sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n]; + + ff_ps_init(); } av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr) @@ -130,6 +133,7 @@ av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr) sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128); ff_mdct_init(&sbr->mdct, 7, 1, 1.0/64); ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0); + ff_ps_ctx_init(&sbr->ps); } av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr) @@ -890,7 +894,6 @@ static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int bs_extension_id, int *num_bits_left) { -//TODO - implement ps_data for parametric stereo parsing switch (bs_extension_id) { case EXTENSION_ID_PS: if (!ac->m4ac.ps) { @@ -898,8 +901,8 @@ static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, skip_bits_long(gb, *num_bits_left); // bs_fill_bits *num_bits_left = 0; } else { -#if 0 - *num_bits_left -= ff_ps_data(gb, ps); +#if 1 + *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left); #else av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0); skip_bits_long(gb, *num_bits_left); // bs_fill_bits @@ -1008,6 +1011,11 @@ static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr, num_bits_left -= 2; read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id } + if (num_bits_left < 0) { + av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n"); + } + if (num_bits_left > 0) + skip_bits(gb, num_bits_left); } return get_bits_count(gb) - cnt; @@ -1166,7 +1174,7 @@ static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, * (14496-3 sp04 p206) */ static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct, - float *out, float X[2][32][64], + float *out, float X[2][38][64], float mdct_buf[2][64], float *v0, int *v_off, const unsigned int div, float bias, float scale) @@ -1402,7 +1410,7 @@ static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr, } /// Generate the subband filtered lowband -static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][32][64], +static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64], const float X_low[32][40][2], const float Y[2][38][64][2], int ch) { @@ -1424,7 +1432,7 @@ static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][32][64], } for (k = 0; k < sbr->kx[1]; k++) { - for (i = i_Temp; i < i_f; i++) { + for (i = i_Temp; i < 38; i++) { X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0]; X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1]; } @@ -1740,6 +1748,16 @@ void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac, /* synthesis */ sbr_x_gen(sbr, sbr->X[ch], sbr->X_low, sbr->data[ch].Y, ch); } + + if (ac->m4ac.ps == 1) { + if (sbr->ps.start) { + ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]); + } else { + memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0])); + } + nch = 2; + } + sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, L, sbr->X[0], sbr->qmf_filter_scratch, sbr->data[0].synthesis_filterbank_samples, &sbr->data[0].synthesis_filterbank_samples_offset, diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 42b20811ca..53ec546295 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -30,7 +30,7 @@ #include "libavutil/avutil.h" #define LIBAVCODEC_VERSION_MAJOR 52 -#define LIBAVCODEC_VERSION_MINOR 76 +#define LIBAVCODEC_VERSION_MINOR 77 #define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ diff --git a/libavcodec/mpeg4audio.c b/libavcodec/mpeg4audio.c index 7507212c76..f9951b1604 100644 --- a/libavcodec/mpeg4audio.c +++ b/libavcodec/mpeg4audio.c @@ -131,6 +131,14 @@ int ff_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int buf_si get_bits1(&gb); // skip 1 bit } } + + //PS requires SBR + if (!c->sbr) + c->ps = 0; + //Limit implicit PS to the HE-AACv2 Profile + if ((c->ps == -1 && c->object_type != AOT_AAC_LC) || c->channels & ~0x01) + c->ps = 0; + return specific_config_bitindex; } diff --git a/libavcodec/ps.c b/libavcodec/ps.c new file mode 100644 index 0000000000..1f2922639d --- /dev/null +++ b/libavcodec/ps.c @@ -0,0 +1,1124 @@ +/* + * MPEG-4 Parametric Stereo decoding functions + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include "libavutil/mathematics.h" +#include "avcodec.h" +#include "get_bits.h" +#include "ps.h" +#include "ps_tablegen.h" +#include "psdata.c" + +#define PS_BASELINE 0 + +#define numQMFSlots 32 //numTimeSlots * RATE + +static const int8_t num_env_tab[2][4] = { + { 0, 1, 2, 4, }, + { 1, 2, 3, 4, }, +}; + +static const int8_t nr_iidicc_par_tab[] = { + 10, 20, 34, 10, 20, 34, +}; + +static const int8_t nr_iidopd_par_tab[] = { + 5, 11, 17, 5, 11, 17, +}; + +enum { + huff_iid_df1, + huff_iid_dt1, + huff_iid_df0, + huff_iid_dt0, + huff_icc_df, + huff_icc_dt, + huff_ipd_df, + huff_ipd_dt, + huff_opd_df, + huff_opd_dt, +}; + +static const int huff_iid[] = { + huff_iid_df0, + huff_iid_df1, + huff_iid_dt0, + huff_iid_dt1, +}; + +static VLC vlc_ps[10]; + +/** + * Read Inter-channel Intensity Difference parameters from the bitstream. + * + * @param avctx contains the current codec context + * @param gb pointer to the input bitstream + * @param ps pointer to the Parametric Stereo context + * @param e envelope to decode + * @param dt 1: time delta-coded, 0: frequency delta-coded + */ +static int iid_data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, int e, int dt) +{ + int b; + int table_idx = huff_iid[2*dt+ps->iid_quant]; + VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; + if (dt) { + int e_prev = e ? e - 1 : ps->num_env_old - 1; + e_prev = FFMAX(e_prev, 0); + for (b = 0; b < ps->nr_iid_par; b++) { + ps->iid_par[e][b] = ps->iid_par[e_prev][b] + + get_vlc2(gb, vlc_table, 9, 3) - + huff_offset[table_idx]; + if (FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant) + goto err; + } + } else { + int prev = 0; + for (b = 0; b < ps->nr_iid_par; b++) { + prev += get_vlc2(gb, vlc_table, 9, 3) - + huff_offset[table_idx]; + ps->iid_par[e][b] = prev; + if (FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant) + goto err; + } + } + return 0; +err: + av_log(avctx, AV_LOG_ERROR, "illegal iid\n"); + return -1; +} + +/** + * Read Inter-Channel Coherence parameters from the bitstream. + * + * @param avctx contains the current codec context + * @param gb pointer to the input bitstream + * @param ps pointer to the Parametric Stereo context + * @param e envelope to decode + * @param dt 1: time delta-coded, 0: frequency delta-coded + */ +static int icc_data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, int e, int dt) +{ + int b; + int table_idx = dt ? huff_icc_dt : huff_icc_df; + VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; + if (dt) { + int e_prev = e ? e - 1 : ps->num_env_old - 1; + e_prev = FFMAX(e_prev, 0); + for (b = 0; b < ps->nr_icc_par; b++) { + ps->icc_par[e][b] = ps->icc_par[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - huff_offset[table_idx]; + if (ps->icc_par[e][b] > 7U) + goto err; + } + } else { + int prev = 0; + for (b = 0; b < ps->nr_icc_par; b++) { + prev += get_vlc2(gb, vlc_table, 9, 3) - huff_offset[table_idx]; + ps->icc_par[e][b] = prev; + if (ps->icc_par[e][b] > 7U) + goto err; + } + } + return 0; +err: + av_log(avctx, AV_LOG_ERROR, "illegal icc\n"); + return -1; +} + +/** + * Read Inter-channel Phase Difference parameters from the bitstream. + * + * @param gb pointer to the input bitstream + * @param ps pointer to the Parametric Stereo context + * @param e envelope to decode + * @param dt 1: time delta-coded, 0: frequency delta-coded + */ +static void ipd_data(GetBitContext *gb, PSContext *ps, int e, int dt) +{ + int b; + int table_idx = dt ? huff_ipd_dt : huff_ipd_df; + VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; + if (dt) { + int e_prev = e ? e - 1 : ps->num_env_old - 1; + e_prev = FFMAX(e_prev, 0); + for (b = 0; b < ps->nr_ipdopd_par; b++) { + ps->ipd_par[e][b] = (ps->ipd_par[e_prev][b] + get_vlc2(gb, vlc_table, 9, 1)) & 0x07; + } + } else { + int prev = 0; + for (b = 0; b < ps->nr_ipdopd_par; b++) { + prev += get_vlc2(gb, vlc_table, 9, 3); + prev &= 0x07; + ps->ipd_par[e][b] = prev; + } + } +} + +/** + * Read Overall Phase Difference parameters from the bitstream. + * + * @param gb pointer to the input bitstream + * @param ps pointer to the Parametric Stereo context + * @param e envelope to decode + * @param dt 1: time delta-coded, 0: frequency delta-coded + */ +static void opd_data(GetBitContext *gb, PSContext *ps, int e, int dt) +{ + int b; + int table_idx = dt ? huff_opd_dt : huff_opd_df; + VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; + if (dt) { + int e_prev = e ? e - 1 : ps->num_env_old - 1; + e_prev = FFMAX(e_prev, 0); + for (b = 0; b < ps->nr_ipdopd_par; b++) { + ps->opd_par[e][b] = (ps->opd_par[e_prev][b] + get_vlc2(gb, vlc_table, 9, 1)) & 0x07; + } + } else { + int prev = 0; + for (b = 0; b < ps->nr_ipdopd_par; b++) { + prev += get_vlc2(gb, vlc_table, 9, 3); + prev &= 0x07; + ps->opd_par[e][b] = prev; + } + } +} + +static int ps_extension(GetBitContext *gb, PSContext *ps, int ps_extension_id) +{ + int e; + int count = get_bits_count(gb); + + if (ps_extension_id) + return 0; + + ps->enable_ipdopd = get_bits1(gb); + if (ps->enable_ipdopd) { + for (e = 0; e < ps->num_env; e++) { + int dt = get_bits1(gb); + ipd_data(gb, ps, e, dt); + dt = get_bits1(gb); + opd_data(gb, ps, e, dt); + } + } + skip_bits1(gb); //reserved_ps + return get_bits_count(gb) - count; +} + +static void ipdopd_reset(int8_t *opd_hist, int8_t *ipd_hist) +{ + int i; + for (i = 0; i < PS_MAX_NR_IPDOPD; i++) { + opd_hist[i] = 0; + ipd_hist[i] = 0; + } +} + +int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left) +{ + int e; + int bit_count_start = get_bits_count(gb_host); + int header; + int bits_consumed; + GetBitContext gbc = *gb_host, *gb = &gbc; + + header = get_bits1(gb); + if (header) { //enable_ps_header + ps->enable_iid = get_bits1(gb); + if (ps->enable_iid) { + ps->iid_mode = get_bits(gb, 3); + if (ps->iid_mode > 5) { + av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n", + ps->iid_mode); + goto err; + } + ps->nr_iid_par = nr_iidicc_par_tab[ps->iid_mode]; + ps->iid_quant = ps->iid_mode > 2; + ps->nr_ipdopd_par = nr_iidopd_par_tab[ps->iid_mode]; + } + ps->enable_icc = get_bits1(gb); + if (ps->enable_icc) { + ps->icc_mode = get_bits(gb, 3); + if (ps->icc_mode > 5) { + av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n", + ps->icc_mode); + goto err; + } + ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode]; + } + ps->enable_ext = get_bits1(gb); + } + + ps->frame_class = get_bits1(gb); + ps->num_env_old = ps->num_env; + ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)]; + + ps->border_position[0] = -1; + if (ps->frame_class) { + for (e = 1; e <= ps->num_env; e++) + ps->border_position[e] = get_bits(gb, 5); + } else + for (e = 1; e <= ps->num_env; e++) + ps->border_position[e] = e * numQMFSlots / ps->num_env - 1; + + if (ps->enable_iid) { + for (e = 0; e < ps->num_env; e++) { + int dt = get_bits1(gb); + if (iid_data(avctx, gb, ps, e, dt)) + goto err; + } + } else + memset(ps->iid_par, 0, sizeof(ps->iid_par)); + + if (ps->enable_icc) + for (e = 0; e < ps->num_env; e++) { + int dt = get_bits1(gb); + if (icc_data(avctx, gb, ps, e, dt)) + goto err; + } + else + memset(ps->icc_par, 0, sizeof(ps->icc_par)); + + if (ps->enable_ext) { + int cnt = get_bits(gb, 4); + if (cnt == 15) { + cnt += get_bits(gb, 8); + } + cnt *= 8; + while (cnt > 7) { + int ps_extension_id = get_bits(gb, 2); + cnt -= 2 + ps_extension(gb, ps, ps_extension_id); + } + if (cnt < 0) { + av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d", cnt); + goto err; + } + skip_bits(gb, cnt); + } + + ps->enable_ipdopd &= !PS_BASELINE; + + //Fix up envelopes + if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) { + //Create a fake envelope + int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1; + if (source >= 0 && source != ps->num_env) { + if (ps->enable_iid && ps->num_env_old > 1) { + memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0])); + } + if (ps->enable_icc && ps->num_env_old > 1) { + memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0])); + } + if (ps->enable_ipdopd && ps->num_env_old > 1) { + memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0])); + memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0])); + } + } + ps->num_env++; + ps->border_position[ps->num_env] = numQMFSlots - 1; + } + + + ps->is34bands_old = ps->is34bands; + if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc)) + ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) || + (ps->enable_icc && ps->nr_icc_par == 34); + + //Baseline + if (!ps->enable_ipdopd) { + memset(ps->ipd_par, 0, sizeof(ps->ipd_par)); + memset(ps->opd_par, 0, sizeof(ps->opd_par)); + } + + if (header) + ps->start = 1; + + bits_consumed = get_bits_count(gb) - bit_count_start; + if (bits_consumed <= bits_left) { + skip_bits_long(gb_host, bits_consumed); + return bits_consumed; + } + av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed); +err: + ps->start = 0; + skip_bits_long(gb_host, bits_left); + return bits_left; +} + +/** Split one subband into 2 subsubbands with a symmetric real filter. + * The filter must have its non-center even coefficients equal to zero. */ +static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[7], int len, int reverse) +{ + int i, j; + for (i = 0; i < len; i++) { + float re_in = filter[6] * in[6+i][0]; //real inphase + float re_op = 0.0f; //real out of phase + float im_in = filter[6] * in[6+i][1]; //imag inphase + float im_op = 0.0f; //imag out of phase + for (j = 0; j < 6; j += 2) { + re_op += filter[j+1] * (in[i+j+1][0] + in[12-j-1+i][0]); + im_op += filter[j+1] * (in[i+j+1][1] + in[12-j-1+i][1]); + } + out[ reverse][i][0] = re_in + re_op; + out[ reverse][i][1] = im_in + im_op; + out[!reverse][i][0] = re_in - re_op; + out[!reverse][i][1] = im_in - im_op; + } +} + +/** Split one subband into 6 subsubbands with a complex filter */ +static void hybrid6_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int len) +{ + int i, j, ssb; + int N = 8; + float temp[8][2]; + + for (i = 0; i < len; i++) { + for (ssb = 0; ssb < N; ssb++) { + float sum_re = filter[ssb][6][0] * in[i+6][0], sum_im = filter[ssb][6][0] * in[i+6][1]; + for (j = 0; j < 6; j++) { + float in0_re = in[i+j][0]; + float in0_im = in[i+j][1]; + float in1_re = in[i+12-j][0]; + float in1_im = in[i+12-j][1]; + sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im); + sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re); + } + temp[ssb][0] = sum_re; + temp[ssb][1] = sum_im; + } + out[0][i][0] = temp[6][0]; + out[0][i][1] = temp[6][1]; + out[1][i][0] = temp[7][0]; + out[1][i][1] = temp[7][1]; + out[2][i][0] = temp[0][0]; + out[2][i][1] = temp[0][1]; + out[3][i][0] = temp[1][0]; + out[3][i][1] = temp[1][1]; + out[4][i][0] = temp[2][0] + temp[5][0]; + out[4][i][1] = temp[2][1] + temp[5][1]; + out[5][i][0] = temp[3][0] + temp[4][0]; + out[5][i][1] = temp[3][1] + temp[4][1]; + } +} + +static void hybrid4_8_12_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int N, int len) +{ + int i, j, ssb; + + for (i = 0; i < len; i++) { + for (ssb = 0; ssb < N; ssb++) { + float sum_re = filter[ssb][6][0] * in[i+6][0], sum_im = filter[ssb][6][0] * in[i+6][1]; + for (j = 0; j < 6; j++) { + float in0_re = in[i+j][0]; + float in0_im = in[i+j][1]; + float in1_re = in[i+12-j][0]; + float in1_im = in[i+12-j][1]; + sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im); + sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re); + } + out[ssb][i][0] = sum_re; + out[ssb][i][1] = sum_im; + } + } +} + +static void hybrid_analysis(float out[91][32][2], float in[5][44][2], float L[2][38][64], int is34, int len) +{ + int i, j; + for (i = 0; i < 5; i++) { + for (j = 0; j < 38; j++) { + in[i][j+6][0] = L[0][j][i]; + in[i][j+6][1] = L[1][j][i]; + } + } + if(is34) { + hybrid4_8_12_cx(in[0], out, f34_0_12, 12, len); + hybrid4_8_12_cx(in[1], out+12, f34_1_8, 8, len); + hybrid4_8_12_cx(in[2], out+20, f34_2_4, 4, len); + hybrid4_8_12_cx(in[3], out+24, f34_2_4, 4, len); + hybrid4_8_12_cx(in[4], out+28, f34_2_4, 4, len); + for (i = 0; i < 59; i++) { + for (j = 0; j < len; j++) { + out[i+32][j][0] = L[0][j][i+5]; + out[i+32][j][1] = L[1][j][i+5]; + } + } + } else { + hybrid6_cx(in[0], out, f20_0_8, len); + hybrid2_re(in[1], out+6, g1_Q2, len, 1); + hybrid2_re(in[2], out+8, g1_Q2, len, 0); + for (i = 0; i < 61; i++) { + for (j = 0; j < len; j++) { + out[i+10][j][0] = L[0][j][i+3]; + out[i+10][j][1] = L[1][j][i+3]; + } + } + } + //update in_buf + for (i = 0; i < 5; i++) { + memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0])); + } +} + +static void hybrid_synthesis(float out[2][38][64], float in[91][32][2], int is34, int len) +{ + int i, n; + if(is34) { + for (n = 0; n < len; n++) { + memset(out[0][n], 0, 5*sizeof(out[0][n][0])); + memset(out[1][n], 0, 5*sizeof(out[1][n][0])); + for(i = 0; i < 12; i++) { + out[0][n][0] += in[ i][n][0]; + out[1][n][0] += in[ i][n][1]; + } + for(i = 0; i < 8; i++) { + out[0][n][1] += in[12+i][n][0]; + out[1][n][1] += in[12+i][n][1]; + } + for(i = 0; i < 4; i++) { + out[0][n][2] += in[20+i][n][0]; + out[1][n][2] += in[20+i][n][1]; + out[0][n][3] += in[24+i][n][0]; + out[1][n][3] += in[24+i][n][1]; + out[0][n][4] += in[28+i][n][0]; + out[1][n][4] += in[28+i][n][1]; + } + } + for (i = 0; i < 59; i++) { + for (n = 0; n < len; n++) { + out[0][n][i+5] = in[i+32][n][0]; + out[1][n][i+5] = in[i+32][n][1]; + } + } + } else { + for (n = 0; n < len; n++) { + out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] + + in[3][n][0] + in[4][n][0] + in[5][n][0]; + out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] + + in[3][n][1] + in[4][n][1] + in[5][n][1]; + out[0][n][1] = in[6][n][0] + in[7][n][0]; + out[1][n][1] = in[6][n][1] + in[7][n][1]; + out[0][n][2] = in[8][n][0] + in[9][n][0]; + out[1][n][2] = in[8][n][1] + in[9][n][1]; + } + for (i = 0; i < 61; i++) { + for (n = 0; n < len; n++) { + out[0][n][i+3] = in[i+10][n][0]; + out[1][n][i+3] = in[i+10][n][1]; + } + } + } +} + +/// All-pass filter decay slope +#define DECAY_SLOPE 0.05f +/// Number of frequency bands that can be addressed by the parameter index, b(k) +static const int NR_PAR_BANDS[] = { 20, 34 }; +/// Number of frequency bands that can be addressed by the sub subband index, k +static const int NR_BANDS[] = { 71, 91 }; +/// Start frequency band for the all-pass filter decay slope +static const int DECAY_CUTOFF[] = { 10, 32 }; +/// Number of all-pass filer bands +static const int NR_ALLPASS_BANDS[] = { 30, 50 }; +/// First stereo band using the short one sample delay +static const int SHORT_DELAY_BAND[] = { 42, 62 }; + +/** Table 8.46 */ +static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full) +{ + int b; + if (full) + b = 9; + else { + b = 4; + par_mapped[10] = 0; + } + for (; b >= 0; b--) { + par_mapped[2*b+1] = par_mapped[2*b] = par[b]; + } +} + +static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full) +{ + par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3; + par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3; + par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3; + par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3; + par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2; + par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2; + par_mapped[ 6] = par[10]; + par_mapped[ 7] = par[11]; + par_mapped[ 8] = ( par[12] + par[13]) / 2; + par_mapped[ 9] = ( par[14] + par[15]) / 2; + par_mapped[10] = par[16]; + if (full) { + par_mapped[11] = par[17]; + par_mapped[12] = par[18]; + par_mapped[13] = par[19]; + par_mapped[14] = ( par[20] + par[21]) / 2; + par_mapped[15] = ( par[22] + par[23]) / 2; + par_mapped[16] = ( par[24] + par[25]) / 2; + par_mapped[17] = ( par[26] + par[27]) / 2; + par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4; + par_mapped[19] = ( par[32] + par[33]) / 2; + } +} + +static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC]) +{ + par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f; + par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f; + par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f; + par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f; + par[ 4] = ( par[ 6] + par[ 7]) * 0.5f; + par[ 5] = ( par[ 8] + par[ 9]) * 0.5f; + par[ 6] = par[10]; + par[ 7] = par[11]; + par[ 8] = ( par[12] + par[13]) * 0.5f; + par[ 9] = ( par[14] + par[15]) * 0.5f; + par[10] = par[16]; + par[11] = par[17]; + par[12] = par[18]; + par[13] = par[19]; + par[14] = ( par[20] + par[21]) * 0.5f; + par[15] = ( par[22] + par[23]) * 0.5f; + par[16] = ( par[24] + par[25]) * 0.5f; + par[17] = ( par[26] + par[27]) * 0.5f; + par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f; + par[19] = ( par[32] + par[33]) * 0.5f; +} + +static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full) +{ + if (full) { + par_mapped[33] = par[9]; + par_mapped[32] = par[9]; + par_mapped[31] = par[9]; + par_mapped[30] = par[9]; + par_mapped[29] = par[9]; + par_mapped[28] = par[9]; + par_mapped[27] = par[8]; + par_mapped[26] = par[8]; + par_mapped[25] = par[8]; + par_mapped[24] = par[8]; + par_mapped[23] = par[7]; + par_mapped[22] = par[7]; + par_mapped[21] = par[7]; + par_mapped[20] = par[7]; + par_mapped[19] = par[6]; + par_mapped[18] = par[6]; + par_mapped[17] = par[5]; + par_mapped[16] = par[5]; + } else { + par_mapped[16] = 0; + } + par_mapped[15] = par[4]; + par_mapped[14] = par[4]; + par_mapped[13] = par[4]; + par_mapped[12] = par[4]; + par_mapped[11] = par[3]; + par_mapped[10] = par[3]; + par_mapped[ 9] = par[2]; + par_mapped[ 8] = par[2]; + par_mapped[ 7] = par[2]; + par_mapped[ 6] = par[2]; + par_mapped[ 5] = par[1]; + par_mapped[ 4] = par[1]; + par_mapped[ 3] = par[1]; + par_mapped[ 2] = par[0]; + par_mapped[ 1] = par[0]; + par_mapped[ 0] = par[0]; +} + +static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full) +{ + if (full) { + par_mapped[33] = par[19]; + par_mapped[32] = par[19]; + par_mapped[31] = par[18]; + par_mapped[30] = par[18]; + par_mapped[29] = par[18]; + par_mapped[28] = par[18]; + par_mapped[27] = par[17]; + par_mapped[26] = par[17]; + par_mapped[25] = par[16]; + par_mapped[24] = par[16]; + par_mapped[23] = par[15]; + par_mapped[22] = par[15]; + par_mapped[21] = par[14]; + par_mapped[20] = par[14]; + par_mapped[19] = par[13]; + par_mapped[18] = par[12]; + par_mapped[17] = par[11]; + } + par_mapped[16] = par[10]; + par_mapped[15] = par[ 9]; + par_mapped[14] = par[ 9]; + par_mapped[13] = par[ 8]; + par_mapped[12] = par[ 8]; + par_mapped[11] = par[ 7]; + par_mapped[10] = par[ 6]; + par_mapped[ 9] = par[ 5]; + par_mapped[ 8] = par[ 5]; + par_mapped[ 7] = par[ 4]; + par_mapped[ 6] = par[ 4]; + par_mapped[ 5] = par[ 3]; + par_mapped[ 4] = (par[ 2] + par[ 3]) / 2; + par_mapped[ 3] = par[ 2]; + par_mapped[ 2] = par[ 1]; + par_mapped[ 1] = (par[ 0] + par[ 1]) / 2; + par_mapped[ 0] = par[ 0]; +} + +static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC]) +{ + par[33] = par[19]; + par[32] = par[19]; + par[31] = par[18]; + par[30] = par[18]; + par[29] = par[18]; + par[28] = par[18]; + par[27] = par[17]; + par[26] = par[17]; + par[25] = par[16]; + par[24] = par[16]; + par[23] = par[15]; + par[22] = par[15]; + par[21] = par[14]; + par[20] = par[14]; + par[19] = par[13]; + par[18] = par[12]; + par[17] = par[11]; + par[16] = par[10]; + par[15] = par[ 9]; + par[14] = par[ 9]; + par[13] = par[ 8]; + par[12] = par[ 8]; + par[11] = par[ 7]; + par[10] = par[ 6]; + par[ 9] = par[ 5]; + par[ 8] = par[ 5]; + par[ 7] = par[ 4]; + par[ 6] = par[ 4]; + par[ 5] = par[ 3]; + par[ 4] = (par[ 2] + par[ 3]) * 0.5f; + par[ 3] = par[ 2]; + par[ 2] = par[ 1]; + par[ 1] = (par[ 0] + par[ 1]) * 0.5f; + par[ 0] = par[ 0]; +} + +static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34) +{ + float power[34][PS_QMF_TIME_SLOTS] = {{0}}; + float transient_gain[34][PS_QMF_TIME_SLOTS]; + float *peak_decay_nrg = ps->peak_decay_nrg; + float *power_smooth = ps->power_smooth; + float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth; + float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay; + float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay; + const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20; + const float peak_decay_factor = 0.76592833836465f; + const float transient_impact = 1.5f; + const float a_smooth = 0.25f; //< Smoothing coefficient + int i, k, m, n; + int n0 = 0, nL = 32; + static const int link_delay[] = { 3, 4, 5 }; + static const float a[] = { 0.65143905753106f, + 0.56471812200776f, + 0.48954165955695f }; + + if (is34 != ps->is34bands_old) { + memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg)); + memset(ps->power_smooth, 0, sizeof(ps->power_smooth)); + memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth)); + memset(ps->delay, 0, sizeof(ps->delay)); + memset(ps->ap_delay, 0, sizeof(ps->ap_delay)); + } + + for (n = n0; n < nL; n++) { + for (k = 0; k < NR_BANDS[is34]; k++) { + int i = k_to_i[k]; + power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1]; + } + } + + //Transient detection + for (i = 0; i < NR_PAR_BANDS[is34]; i++) { + for (n = n0; n < nL; n++) { + float decayed_peak = peak_decay_factor * peak_decay_nrg[i]; + float denom; + peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]); + power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]); + peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]); + denom = transient_impact * peak_decay_diff_smooth[i]; + transient_gain[i][n] = (denom > power_smooth[i]) ? + power_smooth[i] / denom : 1.0f; + } + } + + //Decorrelation and transient reduction + // PS_AP_LINKS - 1 + // ----- + // | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k] + //H[k][z] = z^-2 * phi_fract[k] * | | ---------------------------------------------------------------- + // | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m] + // m = 0 + //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z] + for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) { + int b = k_to_i[k]; + float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]); + float ag[PS_AP_LINKS]; + g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f); + memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0])); + memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0])); + for (m = 0; m < PS_AP_LINKS; m++) { + memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0])); + ag[m] = a[m] * g_decay_slope; + } + for (n = n0; n < nL; n++) { + float in_re = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][0] - + delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][1]; + float in_im = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][1] + + delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][0]; + for (m = 0; m < PS_AP_LINKS; m++) { + float a_re = ag[m] * in_re; + float a_im = ag[m] * in_im; + float link_delay_re = ap_delay[k][m][n+5-link_delay[m]][0]; + float link_delay_im = ap_delay[k][m][n+5-link_delay[m]][1]; + float fractional_delay_re = Q_fract_allpass[is34][k][m][0]; + float fractional_delay_im = Q_fract_allpass[is34][k][m][1]; + ap_delay[k][m][n+5][0] = in_re; + ap_delay[k][m][n+5][1] = in_im; + in_re = link_delay_re * fractional_delay_re - link_delay_im * fractional_delay_im - a_re; + in_im = link_delay_re * fractional_delay_im + link_delay_im * fractional_delay_re - a_im; + ap_delay[k][m][n+5][0] += ag[m] * in_re; + ap_delay[k][m][n+5][1] += ag[m] * in_im; + } + out[k][n][0] = transient_gain[b][n] * in_re; + out[k][n][1] = transient_gain[b][n] * in_im; + } + } + for (; k < SHORT_DELAY_BAND[is34]; k++) { + memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0])); + memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0])); + for (n = n0; n < nL; n++) { + //H = delay 14 + out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][0]; + out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][1]; + } + } + for (; k < NR_BANDS[is34]; k++) { + memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0])); + memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0])); + for (n = n0; n < nL; n++) { + //H = delay 1 + out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][0]; + out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][1]; + } + } +} + +static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC], + int8_t (*par)[PS_MAX_NR_IIDICC], + int num_par, int num_env, int full) +{ + int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped; + int e; + if (num_par == 20 || num_par == 11) { + for (e = 0; e < num_env; e++) { + map_idx_20_to_34(par_mapped[e], par[e], full); + } + } else if (num_par == 10 || num_par == 5) { + for (e = 0; e < num_env; e++) { + map_idx_10_to_34(par_mapped[e], par[e], full); + } + } else { + *p_par_mapped = par; + } +} + +static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC], + int8_t (*par)[PS_MAX_NR_IIDICC], + int num_par, int num_env, int full) +{ + int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped; + int e; + if (num_par == 34 || num_par == 17) { + for (e = 0; e < num_env; e++) { + map_idx_34_to_20(par_mapped[e], par[e], full); + } + } else if (num_par == 10 || num_par == 5) { + for (e = 0; e < num_env; e++) { + map_idx_10_to_20(par_mapped[e], par[e], full); + } + } else { + *p_par_mapped = par; + } +} + +static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34) +{ + int e, b, k, n; + + float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11; + float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12; + float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21; + float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22; + int8_t *opd_hist = ps->opd_hist; + int8_t *ipd_hist = ps->ipd_hist; + int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; + int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; + int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; + int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; + int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf; + int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf; + int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf; + int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf; + const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20; + const float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB; + + //Remapping + for (b = 0; b < PS_MAX_NR_IIDICC; b++) { + H11[0][0][b] = H11[0][ps->num_env_old][b]; + H12[0][0][b] = H12[0][ps->num_env_old][b]; + H21[0][0][b] = H21[0][ps->num_env_old][b]; + H22[0][0][b] = H22[0][ps->num_env_old][b]; + H11[1][0][b] = H11[1][ps->num_env_old][b]; + H12[1][0][b] = H12[1][ps->num_env_old][b]; + H21[1][0][b] = H21[1][ps->num_env_old][b]; + H22[1][0][b] = H22[1][ps->num_env_old][b]; + } + if (is34) { + remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1); + remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1); + if (ps->enable_ipdopd) { + remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0); + remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0); + } + if (!ps->is34bands_old) { + map_val_20_to_34(H11[0][0]); + map_val_20_to_34(H11[1][0]); + map_val_20_to_34(H12[0][0]); + map_val_20_to_34(H12[1][0]); + map_val_20_to_34(H21[0][0]); + map_val_20_to_34(H21[1][0]); + map_val_20_to_34(H22[0][0]); + map_val_20_to_34(H22[1][0]); + ipdopd_reset(ipd_hist, opd_hist); + } + } else { + remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1); + remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1); + if (ps->enable_ipdopd) { + remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0); + remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0); + } + if (ps->is34bands_old) { + map_val_34_to_20(H11[0][0]); + map_val_34_to_20(H11[1][0]); + map_val_34_to_20(H12[0][0]); + map_val_34_to_20(H12[1][0]); + map_val_34_to_20(H21[0][0]); + map_val_34_to_20(H21[1][0]); + map_val_34_to_20(H22[0][0]); + map_val_34_to_20(H22[1][0]); + ipdopd_reset(ipd_hist, opd_hist); + } + } + + //Mixing + for (e = 0; e < ps->num_env; e++) { + for (b = 0; b < NR_PAR_BANDS[is34]; b++) { + float h11, h12, h21, h22; + h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0]; + h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1]; + h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2]; + h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3]; + if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) { + //The spec say says to only run this smoother when enable_ipdopd + //is set but the reference decoder appears to run it constantly + float h11i, h12i, h21i, h22i; + float ipd_adj_re, ipd_adj_im; + int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b]; + int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b]; + float opd_re = pd_re_smooth[opd_idx]; + float opd_im = pd_im_smooth[opd_idx]; + float ipd_re = pd_re_smooth[ipd_idx]; + float ipd_im = pd_im_smooth[ipd_idx]; + opd_hist[b] = opd_idx & 0x3F; + ipd_hist[b] = ipd_idx & 0x3F; + + ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im; + ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im; + h11i = h11 * opd_im; + h11 = h11 * opd_re; + h12i = h12 * ipd_adj_im; + h12 = h12 * ipd_adj_re; + h21i = h21 * opd_im; + h21 = h21 * opd_re; + h22i = h22 * ipd_adj_im; + h22 = h22 * ipd_adj_re; + H11[1][e+1][b] = h11i; + H12[1][e+1][b] = h12i; + H21[1][e+1][b] = h21i; + H22[1][e+1][b] = h22i; + } + H11[0][e+1][b] = h11; + H12[0][e+1][b] = h12; + H21[0][e+1][b] = h21; + H22[0][e+1][b] = h22; + } + for (k = 0; k < NR_BANDS[is34]; k++) { + float h11r, h12r, h21r, h22r; + float h11i, h12i, h21i, h22i; + float h11r_step, h12r_step, h21r_step, h22r_step; + float h11i_step, h12i_step, h21i_step, h22i_step; + int start = ps->border_position[e]; + int stop = ps->border_position[e+1]; + float width = 1.f / (stop - start); + b = k_to_i[k]; + h11r = H11[0][e][b]; + h12r = H12[0][e][b]; + h21r = H21[0][e][b]; + h22r = H22[0][e][b]; + if (!PS_BASELINE && ps->enable_ipdopd) { + //Is this necessary? ps_04_new seems unchanged + if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) { + h11i = -H11[1][e][b]; + h12i = -H12[1][e][b]; + h21i = -H21[1][e][b]; + h22i = -H22[1][e][b]; + } else { + h11i = H11[1][e][b]; + h12i = H12[1][e][b]; + h21i = H21[1][e][b]; + h22i = H22[1][e][b]; + } + } + //Interpolation + h11r_step = (H11[0][e+1][b] - h11r) * width; + h12r_step = (H12[0][e+1][b] - h12r) * width; + h21r_step = (H21[0][e+1][b] - h21r) * width; + h22r_step = (H22[0][e+1][b] - h22r) * width; + if (!PS_BASELINE && ps->enable_ipdopd) { + h11i_step = (H11[1][e+1][b] - h11i) * width; + h12i_step = (H12[1][e+1][b] - h12i) * width; + h21i_step = (H21[1][e+1][b] - h21i) * width; + h22i_step = (H22[1][e+1][b] - h22i) * width; + } + for (n = start + 1; n <= stop; n++) { + //l is s, r is d + float l_re = l[k][n][0]; + float l_im = l[k][n][1]; + float r_re = r[k][n][0]; + float r_im = r[k][n][1]; + h11r += h11r_step; + h12r += h12r_step; + h21r += h21r_step; + h22r += h22r_step; + if (!PS_BASELINE && ps->enable_ipdopd) { + h11i += h11i_step; + h12i += h12i_step; + h21i += h21i_step; + h22i += h22i_step; + + l[k][n][0] = h11r*l_re + h21r*r_re - h11i*l_im - h21i*r_im; + l[k][n][1] = h11r*l_im + h21r*r_im + h11i*l_re + h21i*r_re; + r[k][n][0] = h12r*l_re + h22r*r_re - h12i*l_im - h22i*r_im; + r[k][n][1] = h12r*l_im + h22r*r_im + h12i*l_re + h22i*r_re; + } else { + l[k][n][0] = h11r*l_re + h21r*r_re; + l[k][n][1] = h11r*l_im + h21r*r_im; + r[k][n][0] = h12r*l_re + h22r*r_re; + r[k][n][1] = h12r*l_im + h22r*r_im; + } + } + } + } +} + +int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top) +{ + float Lbuf[91][32][2]; + float Rbuf[91][32][2]; + const int len = 32; + int is34 = ps->is34bands; + + top += NR_BANDS[is34] - 64; + memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0])); + if (top < NR_ALLPASS_BANDS[is34]) + memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0])); + + hybrid_analysis(Lbuf, ps->in_buf, L, is34, len); + decorrelation(ps, Rbuf, Lbuf, is34); + stereo_processing(ps, Lbuf, Rbuf, is34); + hybrid_synthesis(L, Lbuf, is34, len); + hybrid_synthesis(R, Rbuf, is34, len); + + return 0; +} + +#define PS_INIT_VLC_STATIC(num, size) \ + INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \ + ps_tmp[num].ps_bits, 1, 1, \ + ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \ + size); + +#define PS_VLC_ROW(name) \ + { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) } + +av_cold void ff_ps_init(void) { + // Syntax initialization + static const struct { + const void *ps_codes, *ps_bits; + const unsigned int table_size, elem_size; + } ps_tmp[] = { + PS_VLC_ROW(huff_iid_df1), + PS_VLC_ROW(huff_iid_dt1), + PS_VLC_ROW(huff_iid_df0), + PS_VLC_ROW(huff_iid_dt0), + PS_VLC_ROW(huff_icc_df), + PS_VLC_ROW(huff_icc_dt), + PS_VLC_ROW(huff_ipd_df), + PS_VLC_ROW(huff_ipd_dt), + PS_VLC_ROW(huff_opd_df), + PS_VLC_ROW(huff_opd_dt), + }; + + PS_INIT_VLC_STATIC(0, 1544); + PS_INIT_VLC_STATIC(1, 832); + PS_INIT_VLC_STATIC(2, 1024); + PS_INIT_VLC_STATIC(3, 1036); + PS_INIT_VLC_STATIC(4, 544); + PS_INIT_VLC_STATIC(5, 544); + PS_INIT_VLC_STATIC(6, 512); + PS_INIT_VLC_STATIC(7, 512); + PS_INIT_VLC_STATIC(8, 512); + PS_INIT_VLC_STATIC(9, 512); + + ps_tableinit(); +} + +av_cold void ff_ps_ctx_init(PSContext *ps) +{ + ipdopd_reset(ps->ipd_hist, ps->opd_hist); +} diff --git a/libavcodec/ps.h b/libavcodec/ps.h new file mode 100644 index 0000000000..41c925ccf7 --- /dev/null +++ b/libavcodec/ps.h @@ -0,0 +1,80 @@ +/* + * MPEG-4 Parametric Stereo definitions and declarations + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_PS_H +#define AVCODEC_PS_H + +#include + +#define PS_MAX_NUM_ENV 5 +#define PS_MAX_NR_IIDICC 34 +#define PS_MAX_NR_IPDOPD 17 +#define PS_MAX_SSB 91 +#define PS_MAX_AP_BANDS 50 +#define PS_QMF_TIME_SLOTS 32 +#define PS_MAX_DELAY 14 +#define PS_AP_LINKS 3 +#define PS_MAX_AP_DELAY 5 + +typedef struct { + int start; + int enable_iid; + int iid_mode; + int iid_quant; + int nr_iid_par; + int nr_ipdopd_par; + int enable_icc; + int icc_mode; + int nr_icc_par; + int enable_ext; + int frame_class; + int num_env_old; + int num_env; + int enable_ipdopd; + int border_position[PS_MAX_NUM_ENV+1]; + int8_t iid_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; // + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#define CONFIG_HARDCODED_TABLES 0 +#include "ps_tablegen.h" +#include "tableprint.h" + +void write_float_3d_array (const void *p, int b, int c, int d) +{ + int i; + const float *f = p; + for (i = 0; i < b; i++) { + printf("{\n"); + write_float_2d_array(f, c, d); + printf("},\n"); + f += c * d; + } +} + +void write_float_4d_array (const void *p, int a, int b, int c, int d) +{ + int i; + const float *f = p; + for (i = 0; i < a; i++) { + printf("{\n"); + write_float_3d_array(f, b, c, d); + printf("},\n"); + f += b * c * d; + } +} + +int main(void) +{ + ps_tableinit(); + + write_fileheader(); + + printf("static const float pd_re_smooth[8*8*8] = {\n"); + write_float_array(pd_re_smooth, 8*8*8); + printf("};\n"); + printf("static const float pd_im_smooth[8*8*8] = {\n"); + write_float_array(pd_im_smooth, 8*8*8); + printf("};\n"); + + printf("static const float HA[46][8][4] = {\n"); + write_float_3d_array(HA, 46, 8, 4); + printf("};\n"); + printf("static const float HB[46][8][4] = {\n"); + write_float_3d_array(HB, 46, 8, 4); + printf("};\n"); + + printf("static const float f20_0_8[8][7][2] = {\n"); + write_float_3d_array(f20_0_8, 8, 7, 2); + printf("};\n"); + printf("static const float f34_0_12[12][7][2] = {\n"); + write_float_3d_array(f34_0_12, 12, 7, 2); + printf("};\n"); + printf("static const float f34_1_8[8][7][2] = {\n"); + write_float_3d_array(f34_1_8, 8, 7, 2); + printf("};\n"); + printf("static const float f34_2_4[4][7][2] = {\n"); + write_float_3d_array(f34_2_4, 4, 7, 2); + printf("};\n"); + + printf("static const float Q_fract_allpass[2][50][3][2] = {\n"); + write_float_4d_array(Q_fract_allpass, 2, 50, 3, 2); + printf("};\n"); + printf("static const float phi_fract[2][50][2] = {\n"); + write_float_3d_array(phi_fract, 2, 50, 2); + printf("};\n"); + + return 0; +} diff --git a/libavcodec/ps_tablegen.h b/libavcodec/ps_tablegen.h new file mode 100644 index 0000000000..a1773c987c --- /dev/null +++ b/libavcodec/ps_tablegen.h @@ -0,0 +1,221 @@ +/* + * Header file for hardcoded Parametric Stereo tables + * + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PS_TABLEGEN_H +#define PS_TABLEGEN_H + +#include +#include + +#if CONFIG_HARDCODED_TABLES +#define ps_tableinit() +#include "libavcodec/ps_tables.h" +#else +#include "../libavutil/common.h" +#ifndef M_SQRT1_2 +#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ +#endif +#ifndef M_PI +#define M_PI 3.14159265358979323846 /* pi */ +#endif +#ifndef M_SQRT2 +#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ +#endif +#define NR_ALLPASS_BANDS20 30 +#define NR_ALLPASS_BANDS34 50 +#define PS_AP_LINKS 3 +static float pd_re_smooth[8*8*8]; +static float pd_im_smooth[8*8*8]; +static float HA[46][8][4]; +static float HB[46][8][4]; +static float f20_0_8 [ 8][7][2]; +static float f34_0_12[12][7][2]; +static float f34_1_8 [ 8][7][2]; +static float f34_2_4 [ 4][7][2]; +static float Q_fract_allpass[2][50][3][2]; +static float phi_fract[2][50][2]; + +static const float g0_Q8[] = { + 0.00746082949812f, 0.02270420949825f, 0.04546865930473f, 0.07266113929591f, + 0.09885108575264f, 0.11793710567217f, 0.125f +}; + +static const float g0_Q12[] = { + 0.04081179924692f, 0.03812810994926f, 0.05144908135699f, 0.06399831151592f, + 0.07428313801106f, 0.08100347892914f, 0.08333333333333f +}; + +static const float g1_Q8[] = { + 0.01565675600122f, 0.03752716391991f, 0.05417891378782f, 0.08417044116767f, + 0.10307344158036f, 0.12222452249753f, 0.125f +}; + +static const float g2_Q4[] = { + -0.05908211155639f, -0.04871498374946f, 0.0f, 0.07778723915851f, + 0.16486303567403f, 0.23279856662996f, 0.25f +}; + +static void make_filters_from_proto(float (*filter)[7][2], const float *proto, int bands) +{ + int q, n; + for (q = 0; q < bands; q++) { + for (n = 0; n < 7; n++) { + double theta = 2 * M_PI * (q + 0.5) * (n - 6) / bands; + filter[q][n][0] = proto[n] * cos(theta); + filter[q][n][1] = proto[n] * -sin(theta); + } + } +} + +static void ps_tableinit(void) +{ + static const float ipdopd_sin[] = { 0, M_SQRT1_2, 1, M_SQRT1_2, 0, -M_SQRT1_2, -1, -M_SQRT1_2 }; + static const float ipdopd_cos[] = { 1, M_SQRT1_2, 0, -M_SQRT1_2, -1, -M_SQRT1_2, 0, M_SQRT1_2 }; + int pd0, pd1, pd2; + + static const float iid_par_dequant[] = { + //iid_par_dequant_default + 0.05623413251903, 0.12589254117942, 0.19952623149689, 0.31622776601684, + 0.44668359215096, 0.63095734448019, 0.79432823472428, 1, + 1.25892541179417, 1.58489319246111, 2.23872113856834, 3.16227766016838, + 5.01187233627272, 7.94328234724282, 17.7827941003892, + //iid_par_dequant_fine + 0.00316227766017, 0.00562341325190, 0.01, 0.01778279410039, + 0.03162277660168, 0.05623413251903, 0.07943282347243, 0.11220184543020, + 0.15848931924611, 0.22387211385683, 0.31622776601684, 0.39810717055350, + 0.50118723362727, 0.63095734448019, 0.79432823472428, 1, + 1.25892541179417, 1.58489319246111, 1.99526231496888, 2.51188643150958, + 3.16227766016838, 4.46683592150963, 6.30957344480193, 8.91250938133745, + 12.5892541179417, 17.7827941003892, 31.6227766016838, 56.2341325190349, + 100, 177.827941003892, 316.227766016837, + }; + static const float icc_invq[] = { + 1, 0.937, 0.84118, 0.60092, 0.36764, 0, -0.589, -1 + }; + static const float acos_icc_invq[] = { + 0, 0.35685527, 0.57133466, 0.92614472, 1.1943263, M_PI/2, 2.2006171, M_PI + }; + int iid, icc; + + int k, m; + static const int8_t f_center_20[] = { + -3, -1, 1, 3, 5, 7, 10, 14, 18, 22, + }; + static const int8_t f_center_34[] = { + 2, 6, 10, 14, 18, 22, 26, 30, + 34,-10, -6, -2, 51, 57, 15, 21, + 27, 33, 39, 45, 54, 66, 78, 42, + 102, 66, 78, 90,102,114,126, 90, + }; + static const float fractional_delay_links[] = { 0.43f, 0.75f, 0.347f }; + const float fractional_delay_gain = 0.39f; + + for (pd0 = 0; pd0 < 8; pd0++) { + float pd0_re = ipdopd_cos[pd0]; + float pd0_im = ipdopd_sin[pd0]; + for (pd1 = 0; pd1 < 8; pd1++) { + float pd1_re = ipdopd_cos[pd1]; + float pd1_im = ipdopd_sin[pd1]; + for (pd2 = 0; pd2 < 8; pd2++) { + float pd2_re = ipdopd_cos[pd2]; + float pd2_im = ipdopd_sin[pd2]; + float re_smooth = 0.25f * pd0_re + 0.5f * pd1_re + pd2_re; + float im_smooth = 0.25f * pd0_im + 0.5f * pd1_im + pd2_im; + float pd_mag = 1 / sqrt(im_smooth * im_smooth + re_smooth * re_smooth); + pd_re_smooth[pd0*64+pd1*8+pd2] = re_smooth * pd_mag; + pd_im_smooth[pd0*64+pd1*8+pd2] = im_smooth * pd_mag; + } + } + } + + for (iid = 0; iid < 46; iid++) { + float c = iid_par_dequant[iid]; //icc_mode < 3)*/ { + float alpha = 0.5f * acos_icc_invq[icc]; + float beta = alpha * (c1 - c2) * (float)M_SQRT1_2; + HA[iid][icc][0] = c2 * cosf(beta + alpha); + HA[iid][icc][1] = c1 * cosf(beta - alpha); + HA[iid][icc][2] = c2 * sinf(beta + alpha); + HA[iid][icc][3] = c1 * sinf(beta - alpha); + } /* else */ { + float alpha, gamma, mu, rho; + float alpha_c, alpha_s, gamma_c, gamma_s; + rho = FFMAX(icc_invq[icc], 0.05f); + alpha = 0.5f * atan2f(2.0f * c * rho, c*c - 1.0f); + mu = c + 1.0f / c; + mu = sqrtf(1 + (4 * rho * rho - 4)/(mu * mu)); + gamma = atanf(sqrtf((1.0f - mu)/(1.0f + mu))); + if (alpha < 0) alpha += M_PI/2; + alpha_c = cosf(alpha); + alpha_s = sinf(alpha); + gamma_c = cosf(gamma); + gamma_s = sinf(gamma); + HB[iid][icc][0] = M_SQRT2 * alpha_c * gamma_c; + HB[iid][icc][1] = M_SQRT2 * alpha_s * gamma_c; + HB[iid][icc][2] = -M_SQRT2 * alpha_s * gamma_s; + HB[iid][icc][3] = M_SQRT2 * alpha_c * gamma_s; + } + } + } + + for (k = 0; k < NR_ALLPASS_BANDS20; k++) { + double f_center, theta; + if (k < FF_ARRAY_ELEMS(f_center_20)) + f_center = f_center_20[k] * 0.125; + else + f_center = k - 6.5f; + for (m = 0; m < PS_AP_LINKS; m++) { + theta = -M_PI * fractional_delay_links[m] * f_center; + Q_fract_allpass[0][k][m][0] = cos(theta); + Q_fract_allpass[0][k][m][1] = sin(theta); + } + theta = -M_PI*fractional_delay_gain*f_center; + phi_fract[0][k][0] = cos(theta); + phi_fract[0][k][1] = sin(theta); + } + for (k = 0; k < NR_ALLPASS_BANDS34; k++) { + double f_center, theta; + if (k < FF_ARRAY_ELEMS(f_center_34)) + f_center = f_center_34[k] / 24.; + else + f_center = k - 26.5f; + for (m = 0; m < PS_AP_LINKS; m++) { + theta = -M_PI * fractional_delay_links[m] * f_center; + Q_fract_allpass[1][k][m][0] = cos(theta); + Q_fract_allpass[1][k][m][1] = sin(theta); + } + theta = -M_PI*fractional_delay_gain*f_center; + phi_fract[1][k][0] = cos(theta); + phi_fract[1][k][1] = sin(theta); + } + + make_filters_from_proto(f20_0_8, g0_Q8, 8); + make_filters_from_proto(f34_0_12, g0_Q12, 12); + make_filters_from_proto(f34_1_8, g1_Q8, 8); + make_filters_from_proto(f34_2_4, g2_Q4, 4); +} +#endif /* CONFIG_HARDCODED_TABLES */ + +#endif /* PS_TABLEGEN_H */ diff --git a/libavcodec/psdata.c b/libavcodec/psdata.c new file mode 100644 index 0000000000..7431caebc6 --- /dev/null +++ b/libavcodec/psdata.c @@ -0,0 +1,163 @@ +/* + * MPEG-4 Parametric Stereo data tables + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +static const uint8_t huff_iid_df1_bits[] = { + 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 16, 16, 15, 14, 14, + 13, 12, 12, 11, 10, 10, 8, 7, 6, 5, 4, 3, 1, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 16, 17, 17, 18, 17, 18, 18, + 18, 18, 18, 18, 18, 18, 18, +}; + +static const uint32_t huff_iid_df1_codes[] = { + 0x01FEB4, 0x01FEB5, 0x01FD76, 0x01FD77, 0x01FD74, 0x01FD75, 0x01FE8A, + 0x01FE8B, 0x01FE88, 0x00FE80, 0x01FEB6, 0x00FE82, 0x00FEB8, 0x007F42, + 0x007FAE, 0x003FAF, 0x001FD1, 0x001FE9, 0x000FE9, 0x0007EA, 0x0007FB, + 0x0003FB, 0x0001FB, 0x0001FF, 0x00007C, 0x00003C, 0x00001C, 0x00000C, + 0x000000, 0x000001, 0x000001, 0x000002, 0x000001, 0x00000D, 0x00001D, + 0x00003D, 0x00007D, 0x0000FC, 0x0001FC, 0x0003FC, 0x0003F4, 0x0007EB, + 0x000FEA, 0x001FEA, 0x001FD6, 0x003FD0, 0x007FAF, 0x007F43, 0x00FEB9, + 0x00FE83, 0x01FEB7, 0x00FE81, 0x01FE89, 0x01FE8E, 0x01FE8F, 0x01FE8C, + 0x01FE8D, 0x01FEB2, 0x01FEB3, 0x01FEB0, 0x01FEB1, +}; + +static const uint8_t huff_iid_dt1_bits[] = { + 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, 14, 13, + 13, 13, 12, 12, 11, 10, 9, 9, 7, 6, 5, 3, 1, 2, 5, 6, 7, 8, + 9, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, +}; + +static const uint16_t huff_iid_dt1_codes[] = { + 0x004ED4, 0x004ED5, 0x004ECE, 0x004ECF, 0x004ECC, 0x004ED6, 0x004ED8, + 0x004F46, 0x004F60, 0x002718, 0x002719, 0x002764, 0x002765, 0x00276D, + 0x0027B1, 0x0013B7, 0x0013D6, 0x0009C7, 0x0009E9, 0x0009ED, 0x0004EE, + 0x0004F7, 0x000278, 0x000139, 0x00009A, 0x00009F, 0x000020, 0x000011, + 0x00000A, 0x000003, 0x000001, 0x000000, 0x00000B, 0x000012, 0x000021, + 0x00004C, 0x00009B, 0x00013A, 0x000279, 0x000270, 0x0004EF, 0x0004E2, + 0x0009EA, 0x0009D8, 0x0013D7, 0x0013D0, 0x0027B2, 0x0027A2, 0x00271A, + 0x00271B, 0x004F66, 0x004F67, 0x004F61, 0x004F47, 0x004ED9, 0x004ED7, + 0x004ECD, 0x004ED2, 0x004ED3, 0x004ED0, 0x004ED1, +}; + +static const uint8_t huff_iid_df0_bits[] = { + 17, 17, 17, 17, 16, 15, 13, 10, 9, 7, 6, 5, 4, 3, 1, 3, 4, 5, + 6, 6, 8, 11, 13, 14, 14, 15, 17, 18, 18, +}; + +static const uint32_t huff_iid_df0_codes[] = { + 0x01FFFB, 0x01FFFC, 0x01FFFD, 0x01FFFA, 0x00FFFC, 0x007FFC, 0x001FFD, + 0x0003FE, 0x0001FE, 0x00007E, 0x00003C, 0x00001D, 0x00000D, 0x000005, + 0x000000, 0x000004, 0x00000C, 0x00001C, 0x00003D, 0x00003E, 0x0000FE, + 0x0007FE, 0x001FFC, 0x003FFC, 0x003FFD, 0x007FFD, 0x01FFFE, 0x03FFFE, + 0x03FFFF, +}; + +static const uint8_t huff_iid_dt0_bits[] = { + 19, 19, 19, 20, 20, 20, 17, 15, 12, 10, 8, 6, 4, 2, 1, 3, 5, 7, + 9, 11, 13, 14, 17, 19, 20, 20, 20, 20, 20, +}; + +static const uint32_t huff_iid_dt0_codes[] = { + 0x07FFF9, 0x07FFFA, 0x07FFFB, 0x0FFFF8, 0x0FFFF9, 0x0FFFFA, 0x01FFFD, + 0x007FFE, 0x000FFE, 0x0003FE, 0x0000FE, 0x00003E, 0x00000E, 0x000002, + 0x000000, 0x000006, 0x00001E, 0x00007E, 0x0001FE, 0x0007FE, 0x001FFE, + 0x003FFE, 0x01FFFC, 0x07FFF8, 0x0FFFFB, 0x0FFFFC, 0x0FFFFD, 0x0FFFFE, + 0x0FFFFF, +}; + +static const uint8_t huff_icc_df_bits[] = { + 14, 14, 12, 10, 7, 5, 3, 1, 2, 4, 6, 8, 9, 11, 13, +}; + +static const uint16_t huff_icc_df_codes[] = { + 0x3FFF, 0x3FFE, 0x0FFE, 0x03FE, 0x007E, 0x001E, 0x0006, 0x0000, + 0x0002, 0x000E, 0x003E, 0x00FE, 0x01FE, 0x07FE, 0x1FFE, +}; + +static const uint8_t huff_icc_dt_bits[] = { + 14, 13, 11, 9, 7, 5, 3, 1, 2, 4, 6, 8, 10, 12, 14, +}; + +static const uint16_t huff_icc_dt_codes[] = { + 0x3FFE, 0x1FFE, 0x07FE, 0x01FE, 0x007E, 0x001E, 0x0006, 0x0000, + 0x0002, 0x000E, 0x003E, 0x00FE, 0x03FE, 0x0FFE, 0x3FFF, +}; + +static const uint8_t huff_ipd_df_bits[] = { + 1, 3, 4, 4, 4, 4, 4, 4, +}; + +static const uint8_t huff_ipd_df_codes[] = { + 0x01, 0x00, 0x06, 0x04, 0x02, 0x03, 0x05, 0x07, +}; + +static const uint8_t huff_ipd_dt_bits[] = { + 1, 3, 4, 5, 5, 4, 4, 3, +}; + +static const uint8_t huff_ipd_dt_codes[] = { + 0x01, 0x02, 0x02, 0x03, 0x02, 0x00, 0x03, 0x03, +}; + +static const uint8_t huff_opd_df_bits[] = { + 1, 3, 4, 4, 5, 5, 4, 3, +}; + +static const uint8_t huff_opd_df_codes[] = { + 0x01, 0x01, 0x06, 0x04, 0x0F, 0x0E, 0x05, 0x00, +}; + +static const uint8_t huff_opd_dt_bits[] = { + 1, 3, 4, 5, 5, 4, 4, 3, +}; + +static const uint8_t huff_opd_dt_codes[] = { + 0x01, 0x02, 0x01, 0x07, 0x06, 0x00, 0x02, 0x03, +}; + +static const int8_t huff_offset[] = { + 30, 30, + 14, 14, + 7, 7, + 0, 0, + 0, 0, +}; + +///Table 8.48 +static const int8_t k_to_i_20[] = { + 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 15, + 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 +}; +///Table 8.49 +static const int8_t k_to_i_34[] = { + 0, 1, 2, 3, 4, 5, 6, 6, 7, 2, 1, 0, 10, 10, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 9, 14, 11, 12, 13, 14, 15, 16, 13, 16, 17, 18, 19, 20, 21, + 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, + 30, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33 +}; + +static const float g1_Q2[] = { + 0.0f, 0.01899487526049f, 0.0f, -0.07293139167538f, + 0.0f, 0.30596630545168f, 0.5f +}; diff --git a/libavcodec/sbr.h b/libavcodec/sbr.h index f1da5c5fec..adf799a7e6 100644 --- a/libavcodec/sbr.h +++ b/libavcodec/sbr.h @@ -31,6 +31,7 @@ #include #include "fft.h" +#include "ps.h" /** * Spectral Band Replication header - spectrum parameters that invoke a reset if they differ from the previous header. @@ -133,6 +134,7 @@ typedef struct { ///The number of frequency bands in f_master unsigned n_master; SBRData data[2]; + PSContext ps; ///N_Low and N_High respectively, the number of frequency bands for low and high resolution unsigned n[2]; ///Number of noise floor bands @@ -157,7 +159,7 @@ typedef struct { ///QMF output of the HF generator float X_high[64][40][2]; ///QMF values of the reconstructed signal - DECLARE_ALIGNED(16, float, X)[2][2][32][64]; + DECLARE_ALIGNED(16, float, X)[2][2][38][64]; ///Zeroth coefficient used to filter the subband signals float alpha0[64][2]; ///First coefficient used to filter the subband signals