[SCM] FFmpeg packaging branch, master.snapshot, updated. debian/0.6-1-18-g081dbd2
siretart at users.alioth.debian.org
siretart at users.alioth.debian.org
Tue Jun 29 07:10:44 UTC 2010
The following commit has been merged in the master.snapshot branch:
commit 6af547cc4db57ed13713673148fb58809d4d4d12
Author: Reinhard Tartler <siretart at tauware.de>
Date: Tue Jun 29 09:04:43 2010 +0200
Backport-AAC-HE-v2
This patch is currently under consideration for the 0.6.1 release
diff --git a/debian/patches/0003-Backport-AAC-HE-v2.patch b/debian/patches/0003-Backport-AAC-HE-v2.patch
new file mode 100644
index 0000000..babb9f5
--- /dev/null
+++ b/debian/patches/0003-Backport-AAC-HE-v2.patch
@@ -0,0 +1,6774 @@
+From: Reinhard Tartler <siretart at tauware.de>
+Subject: [PATCH] Backport AAC-HE-v2
+
+merge all revision that are related for aac encoder and decoder from trunk
+
+this patch is under consideration for the upcoming 0.6.1 release
+
+--- a/libavcodec/aac.c
++++ /dev/null
+@@ -1,2108 +0,0 @@
+-/*
+- * AAC decoder
+- * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
+- * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
+- *
+- * 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
+- */
+-
+-/**
+- * @file
+- * AAC decoder
+- * @author Oded Shimon ( ods15 ods15 dyndns org )
+- * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
+- */
+-
+-/*
+- * supported tools
+- *
+- * Support? Name
+- * N (code in SoC repo) gain control
+- * Y block switching
+- * Y window shapes - standard
+- * N window shapes - Low Delay
+- * Y filterbank - standard
+- * N (code in SoC repo) filterbank - Scalable Sample Rate
+- * Y Temporal Noise Shaping
+- * N (code in SoC repo) Long Term Prediction
+- * Y intensity stereo
+- * Y channel coupling
+- * Y frequency domain prediction
+- * Y Perceptual Noise Substitution
+- * Y Mid/Side stereo
+- * N Scalable Inverse AAC Quantization
+- * N Frequency Selective Switch
+- * N upsampling filter
+- * Y quantization & coding - AAC
+- * N quantization & coding - TwinVQ
+- * N quantization & coding - BSAC
+- * N AAC Error Resilience tools
+- * N Error Resilience payload syntax
+- * N Error Protection tool
+- * N CELP
+- * N Silence Compression
+- * N HVXC
+- * N HVXC 4kbits/s VR
+- * N Structured Audio tools
+- * N Structured Audio Sample Bank Format
+- * N MIDI
+- * N Harmonic and Individual Lines plus Noise
+- * N Text-To-Speech Interface
+- * Y Spectral Band Replication
+- * Y (not in this code) Layer-1
+- * Y (not in this code) Layer-2
+- * Y (not in this code) Layer-3
+- * N SinuSoidal Coding (Transient, Sinusoid, Noise)
+- * N (planned) Parametric Stereo
+- * N Direct Stream Transfer
+- *
+- * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
+- * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
+- Parametric Stereo.
+- */
+-
+-
+-#include "avcodec.h"
+-#include "internal.h"
+-#include "get_bits.h"
+-#include "dsputil.h"
+-#include "fft.h"
+-#include "lpc.h"
+-
+-#include "aac.h"
+-#include "aactab.h"
+-#include "aacdectab.h"
+-#include "cbrt_tablegen.h"
+-#include "sbr.h"
+-#include "aacsbr.h"
+-#include "mpeg4audio.h"
+-#include "aac_parser.h"
+-
+-#include <assert.h>
+-#include <errno.h>
+-#include <math.h>
+-#include <string.h>
+-
+-#if ARCH_ARM
+-# include "arm/aac.h"
+-#endif
+-
+-union float754 {
+- float f;
+- uint32_t i;
+-};
+-
+-static VLC vlc_scalefactors;
+-static VLC vlc_spectral[11];
+-
+-static const char overread_err[] = "Input buffer exhausted before END element found\n";
+-
+-static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
+-{
+- if (ac->tag_che_map[type][elem_id]) {
+- return ac->tag_che_map[type][elem_id];
+- }
+- if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
+- return NULL;
+- }
+- switch (ac->m4ac.chan_config) {
+- case 7:
+- if (ac->tags_mapped == 3 && type == TYPE_CPE) {
+- ac->tags_mapped++;
+- return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
+- }
+- case 6:
+- /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
+- instead of SCE[0] CPE[0] CPE[0] LFE[0]. If we seem to have
+- encountered such a stream, transfer the LFE[0] element to SCE[1] */
+- if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
+- ac->tags_mapped++;
+- return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
+- }
+- case 5:
+- if (ac->tags_mapped == 2 && type == TYPE_CPE) {
+- ac->tags_mapped++;
+- return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
+- }
+- case 4:
+- if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
+- ac->tags_mapped++;
+- return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
+- }
+- case 3:
+- case 2:
+- if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
+- ac->tags_mapped++;
+- return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
+- } else if (ac->m4ac.chan_config == 2) {
+- return NULL;
+- }
+- case 1:
+- if (!ac->tags_mapped && type == TYPE_SCE) {
+- ac->tags_mapped++;
+- return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
+- }
+- default:
+- return NULL;
+- }
+-}
+-
+-/**
+- * Check for the channel element in the current channel position configuration.
+- * If it exists, make sure the appropriate element is allocated and map the
+- * channel order to match the internal FFmpeg channel layout.
+- *
+- * @param che_pos current channel position configuration
+- * @param type channel element type
+- * @param id channel element id
+- * @param channels count of the number of channels in the configuration
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static av_cold int che_configure(AACContext *ac,
+- enum ChannelPosition che_pos[4][MAX_ELEM_ID],
+- int type, int id,
+- int *channels)
+-{
+- if (che_pos[type][id]) {
+- if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
+- return AVERROR(ENOMEM);
+- 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) {
+- ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
+- }
+- }
+- } else {
+- if (ac->che[type][id])
+- ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
+- av_freep(&ac->che[type][id]);
+- }
+- return 0;
+-}
+-
+-/**
+- * Configure output channel order based on the current program configuration element.
+- *
+- * @param che_pos current channel position configuration
+- * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static av_cold int output_configure(AACContext *ac,
+- enum ChannelPosition che_pos[4][MAX_ELEM_ID],
+- enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
+- int channel_config, enum OCStatus oc_type)
+-{
+- AVCodecContext *avctx = ac->avccontext;
+- int i, type, channels = 0, ret;
+-
+- memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
+-
+- if (channel_config) {
+- for (i = 0; i < tags_per_config[channel_config]; i++) {
+- if ((ret = che_configure(ac, che_pos,
+- aac_channel_layout_map[channel_config - 1][i][0],
+- aac_channel_layout_map[channel_config - 1][i][1],
+- &channels)))
+- return ret;
+- }
+-
+- memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
+- ac->tags_mapped = 0;
+-
+- avctx->channel_layout = aac_channel_layout[channel_config - 1];
+- } else {
+- /* Allocate or free elements depending on if they are in the
+- * current program configuration.
+- *
+- * Set up default 1:1 output mapping.
+- *
+- * For a 5.1 stream the output order will be:
+- * [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
+- */
+-
+- for (i = 0; i < MAX_ELEM_ID; i++) {
+- for (type = 0; type < 4; type++) {
+- if ((ret = che_configure(ac, che_pos, type, i, &channels)))
+- return ret;
+- }
+- }
+-
+- memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
+- ac->tags_mapped = 4 * MAX_ELEM_ID;
+-
+- avctx->channel_layout = 0;
+- }
+-
+- avctx->channels = channels;
+-
+- ac->output_configured = oc_type;
+-
+- return 0;
+-}
+-
+-/**
+- * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
+- *
+- * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
+- * @param sce_map mono (Single Channel Element) map
+- * @param type speaker type/position for these channels
+- */
+-static void decode_channel_map(enum ChannelPosition *cpe_map,
+- enum ChannelPosition *sce_map,
+- enum ChannelPosition type,
+- GetBitContext *gb, int n)
+-{
+- while (n--) {
+- enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
+- map[get_bits(gb, 4)] = type;
+- }
+-}
+-
+-/**
+- * Decode program configuration element; reference: table 4.2.
+- *
+- * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
+- GetBitContext *gb)
+-{
+- int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
+- int comment_len;
+-
+- skip_bits(gb, 2); // object_type
+-
+- sampling_index = get_bits(gb, 4);
+- if (ac->m4ac.sampling_index != sampling_index)
+- av_log(ac->avccontext, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
+-
+- num_front = get_bits(gb, 4);
+- num_side = get_bits(gb, 4);
+- num_back = get_bits(gb, 4);
+- num_lfe = get_bits(gb, 2);
+- num_assoc_data = get_bits(gb, 3);
+- num_cc = get_bits(gb, 4);
+-
+- if (get_bits1(gb))
+- skip_bits(gb, 4); // mono_mixdown_tag
+- if (get_bits1(gb))
+- skip_bits(gb, 4); // stereo_mixdown_tag
+-
+- if (get_bits1(gb))
+- skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
+-
+- decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
+- decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
+- decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
+- decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
+-
+- skip_bits_long(gb, 4 * num_assoc_data);
+-
+- decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
+-
+- align_get_bits(gb);
+-
+- /* comment field, first byte is length */
+- comment_len = get_bits(gb, 8) * 8;
+- if (get_bits_left(gb) < comment_len) {
+- av_log(ac->avccontext, AV_LOG_ERROR, overread_err);
+- return -1;
+- }
+- skip_bits_long(gb, comment_len);
+- return 0;
+-}
+-
+-/**
+- * Set up channel positions based on a default channel configuration
+- * as specified in table 1.17.
+- *
+- * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static av_cold int set_default_channel_config(AACContext *ac,
+- enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
+- int channel_config)
+-{
+- if (channel_config < 1 || channel_config > 7) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
+- channel_config);
+- return -1;
+- }
+-
+- /* default channel configurations:
+- *
+- * 1ch : front center (mono)
+- * 2ch : L + R (stereo)
+- * 3ch : front center + L + R
+- * 4ch : front center + L + R + back center
+- * 5ch : front center + L + R + back stereo
+- * 6ch : front center + L + R + back stereo + LFE
+- * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
+- */
+-
+- if (channel_config != 2)
+- new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
+- if (channel_config > 1)
+- new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
+- if (channel_config == 4)
+- new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center
+- if (channel_config > 4)
+- new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
+- = AAC_CHANNEL_BACK; // back stereo
+- if (channel_config > 5)
+- new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE
+- if (channel_config == 7)
+- new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
+-
+- return 0;
+-}
+-
+-/**
+- * Decode GA "General Audio" specific configuration; reference: table 4.1.
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
+- int channel_config)
+-{
+- enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
+- int extension_flag, ret;
+-
+- if (get_bits1(gb)) { // frameLengthFlag
+- av_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1);
+- return -1;
+- }
+-
+- if (get_bits1(gb)) // dependsOnCoreCoder
+- skip_bits(gb, 14); // coreCoderDelay
+- extension_flag = get_bits1(gb);
+-
+- if (ac->m4ac.object_type == AOT_AAC_SCALABLE ||
+- ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
+- skip_bits(gb, 3); // layerNr
+-
+- memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
+- if (channel_config == 0) {
+- skip_bits(gb, 4); // element_instance_tag
+- if ((ret = decode_pce(ac, new_che_pos, gb)))
+- return ret;
+- } else {
+- if ((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
+- return ret;
+- }
+- if ((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
+- return ret;
+-
+- if (extension_flag) {
+- switch (ac->m4ac.object_type) {
+- case AOT_ER_BSAC:
+- skip_bits(gb, 5); // numOfSubFrame
+- skip_bits(gb, 11); // layer_length
+- break;
+- case AOT_ER_AAC_LC:
+- case AOT_ER_AAC_LTP:
+- case AOT_ER_AAC_SCALABLE:
+- case AOT_ER_AAC_LD:
+- skip_bits(gb, 3); /* aacSectionDataResilienceFlag
+- * aacScalefactorDataResilienceFlag
+- * aacSpectralDataResilienceFlag
+- */
+- break;
+- }
+- skip_bits1(gb); // extensionFlag3 (TBD in version 3)
+- }
+- return 0;
+-}
+-
+-/**
+- * Decode audio specific configuration; reference: table 1.13.
+- *
+- * @param data pointer to AVCodecContext extradata
+- * @param data_size size of AVCCodecContext extradata
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_audio_specific_config(AACContext *ac, void *data,
+- int data_size)
+-{
+- GetBitContext gb;
+- int i;
+-
+- init_get_bits(&gb, data, data_size * 8);
+-
+- if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
+- return -1;
+- if (ac->m4ac.sampling_index > 12) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
+- return -1;
+- }
+-
+- skip_bits_long(&gb, i);
+-
+- switch (ac->m4ac.object_type) {
+- case AOT_AAC_MAIN:
+- case AOT_AAC_LC:
+- if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
+- return -1;
+- break;
+- default:
+- av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
+- ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
+- return -1;
+- }
+- return 0;
+-}
+-
+-/**
+- * linear congruential pseudorandom number generator
+- *
+- * @param previous_val pointer to the current state of the generator
+- *
+- * @return Returns a 32-bit pseudorandom integer
+- */
+-static av_always_inline int lcg_random(int previous_val)
+-{
+- return previous_val * 1664525 + 1013904223;
+-}
+-
+-static av_always_inline void reset_predict_state(PredictorState *ps)
+-{
+- ps->r0 = 0.0f;
+- ps->r1 = 0.0f;
+- ps->cor0 = 0.0f;
+- ps->cor1 = 0.0f;
+- ps->var0 = 1.0f;
+- ps->var1 = 1.0f;
+-}
+-
+-static void reset_all_predictors(PredictorState *ps)
+-{
+- int i;
+- for (i = 0; i < MAX_PREDICTORS; i++)
+- reset_predict_state(&ps[i]);
+-}
+-
+-static void reset_predictor_group(PredictorState *ps, int group_num)
+-{
+- int i;
+- for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
+- reset_predict_state(&ps[i]);
+-}
+-
+-static av_cold int aac_decode_init(AVCodecContext *avccontext)
+-{
+- AACContext *ac = avccontext->priv_data;
+- int i;
+-
+- ac->avccontext = avccontext;
+- ac->m4ac.sample_rate = avccontext->sample_rate;
+-
+- if (avccontext->extradata_size > 0) {
+- if (decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size))
+- return -1;
+- }
+-
+- avccontext->sample_fmt = SAMPLE_FMT_S16;
+-
+- AAC_INIT_VLC_STATIC( 0, 304);
+- AAC_INIT_VLC_STATIC( 1, 270);
+- AAC_INIT_VLC_STATIC( 2, 550);
+- AAC_INIT_VLC_STATIC( 3, 300);
+- AAC_INIT_VLC_STATIC( 4, 328);
+- AAC_INIT_VLC_STATIC( 5, 294);
+- AAC_INIT_VLC_STATIC( 6, 306);
+- AAC_INIT_VLC_STATIC( 7, 268);
+- AAC_INIT_VLC_STATIC( 8, 510);
+- AAC_INIT_VLC_STATIC( 9, 366);
+- AAC_INIT_VLC_STATIC(10, 462);
+-
+- ff_aac_sbr_init();
+-
+- dsputil_init(&ac->dsp, avccontext);
+-
+- ac->random_state = 0x1f2e3d4c;
+-
+- // -1024 - Compensate wrong IMDCT method.
+- // 32768 - Required to scale values to the correct range for the bias method
+- // for float to int16 conversion.
+-
+- if (ac->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
+- ac->add_bias = 385.0f;
+- ac->sf_scale = 1. / (-1024. * 32768.);
+- ac->sf_offset = 0;
+- } else {
+- ac->add_bias = 0.0f;
+- ac->sf_scale = 1. / -1024.;
+- ac->sf_offset = 60;
+- }
+-
+-#if !CONFIG_HARDCODED_TABLES
+- for (i = 0; i < 428; i++)
+- ff_aac_pow2sf_tab[i] = pow(2, (i - 200) / 4.);
+-#endif /* CONFIG_HARDCODED_TABLES */
+-
+- INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
+- ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
+- ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
+- 352);
+-
+- ff_mdct_init(&ac->mdct, 11, 1, 1.0);
+- ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
+- // window initialization
+- ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
+- ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
+- ff_init_ff_sine_windows(10);
+- ff_init_ff_sine_windows( 7);
+-
+- cbrt_tableinit();
+-
+- return 0;
+-}
+-
+-/**
+- * Skip data_stream_element; reference: table 4.10.
+- */
+-static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
+-{
+- int byte_align = get_bits1(gb);
+- int count = get_bits(gb, 8);
+- if (count == 255)
+- count += get_bits(gb, 8);
+- if (byte_align)
+- align_get_bits(gb);
+-
+- if (get_bits_left(gb) < 8 * count) {
+- av_log(ac->avccontext, AV_LOG_ERROR, overread_err);
+- return -1;
+- }
+- skip_bits_long(gb, 8 * count);
+- return 0;
+-}
+-
+-static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
+- GetBitContext *gb)
+-{
+- int sfb;
+- if (get_bits1(gb)) {
+- ics->predictor_reset_group = get_bits(gb, 5);
+- if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
+- return -1;
+- }
+- }
+- for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
+- ics->prediction_used[sfb] = get_bits1(gb);
+- }
+- return 0;
+-}
+-
+-/**
+- * Decode Individual Channel Stream info; reference: table 4.6.
+- *
+- * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
+- */
+-static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
+- GetBitContext *gb, int common_window)
+-{
+- if (get_bits1(gb)) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n");
+- memset(ics, 0, sizeof(IndividualChannelStream));
+- return -1;
+- }
+- ics->window_sequence[1] = ics->window_sequence[0];
+- ics->window_sequence[0] = get_bits(gb, 2);
+- ics->use_kb_window[1] = ics->use_kb_window[0];
+- ics->use_kb_window[0] = get_bits1(gb);
+- ics->num_window_groups = 1;
+- ics->group_len[0] = 1;
+- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+- int i;
+- ics->max_sfb = get_bits(gb, 4);
+- for (i = 0; i < 7; i++) {
+- if (get_bits1(gb)) {
+- ics->group_len[ics->num_window_groups - 1]++;
+- } else {
+- ics->num_window_groups++;
+- ics->group_len[ics->num_window_groups - 1] = 1;
+- }
+- }
+- ics->num_windows = 8;
+- ics->swb_offset = ff_swb_offset_128[ac->m4ac.sampling_index];
+- ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
+- ics->tns_max_bands = ff_tns_max_bands_128[ac->m4ac.sampling_index];
+- ics->predictor_present = 0;
+- } else {
+- ics->max_sfb = get_bits(gb, 6);
+- ics->num_windows = 1;
+- ics->swb_offset = ff_swb_offset_1024[ac->m4ac.sampling_index];
+- ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
+- ics->tns_max_bands = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
+- ics->predictor_present = get_bits1(gb);
+- ics->predictor_reset_group = 0;
+- if (ics->predictor_present) {
+- if (ac->m4ac.object_type == AOT_AAC_MAIN) {
+- if (decode_prediction(ac, ics, gb)) {
+- memset(ics, 0, sizeof(IndividualChannelStream));
+- return -1;
+- }
+- } else if (ac->m4ac.object_type == AOT_AAC_LC) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
+- memset(ics, 0, sizeof(IndividualChannelStream));
+- return -1;
+- } else {
+- av_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1);
+- memset(ics, 0, sizeof(IndividualChannelStream));
+- return -1;
+- }
+- }
+- }
+-
+- if (ics->max_sfb > ics->num_swb) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
+- "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
+- ics->max_sfb, ics->num_swb);
+- memset(ics, 0, sizeof(IndividualChannelStream));
+- return -1;
+- }
+-
+- return 0;
+-}
+-
+-/**
+- * Decode band types (section_data payload); reference: table 4.46.
+- *
+- * @param band_type array of the used band type
+- * @param band_type_run_end array of the last scalefactor band of a band type run
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_band_types(AACContext *ac, enum BandType band_type[120],
+- int band_type_run_end[120], GetBitContext *gb,
+- IndividualChannelStream *ics)
+-{
+- int g, idx = 0;
+- const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
+- for (g = 0; g < ics->num_window_groups; g++) {
+- int k = 0;
+- while (k < ics->max_sfb) {
+- uint8_t sect_end = k;
+- int sect_len_incr;
+- int sect_band_type = get_bits(gb, 4);
+- if (sect_band_type == 12) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n");
+- return -1;
+- }
+- while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
+- sect_end += sect_len_incr;
+- sect_end += sect_len_incr;
+- if (get_bits_left(gb) < 0) {
+- av_log(ac->avccontext, AV_LOG_ERROR, overread_err);
+- return -1;
+- }
+- if (sect_end > ics->max_sfb) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
+- "Number of bands (%d) exceeds limit (%d).\n",
+- sect_end, ics->max_sfb);
+- return -1;
+- }
+- for (; k < sect_end; k++) {
+- band_type [idx] = sect_band_type;
+- band_type_run_end[idx++] = sect_end;
+- }
+- }
+- }
+- return 0;
+-}
+-
+-/**
+- * Decode scalefactors; reference: table 4.47.
+- *
+- * @param global_gain first scalefactor value as scalefactors are differentially coded
+- * @param band_type array of the used band type
+- * @param band_type_run_end array of the last scalefactor band of a band type run
+- * @param sf array of scalefactors or intensity stereo positions
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
+- unsigned int global_gain,
+- IndividualChannelStream *ics,
+- enum BandType band_type[120],
+- int band_type_run_end[120])
+-{
+- const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
+- int g, i, idx = 0;
+- int offset[3] = { global_gain, global_gain - 90, 100 };
+- int noise_flag = 1;
+- static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
+- for (g = 0; g < ics->num_window_groups; g++) {
+- for (i = 0; i < ics->max_sfb;) {
+- int run_end = band_type_run_end[idx];
+- if (band_type[idx] == ZERO_BT) {
+- for (; i < run_end; i++, idx++)
+- sf[idx] = 0.;
+- } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
+- for (; i < run_end; i++, idx++) {
+- offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+- if (offset[2] > 255U) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
+- "%s (%d) out of range.\n", sf_str[2], offset[2]);
+- return -1;
+- }
+- sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
+- }
+- } else if (band_type[idx] == NOISE_BT) {
+- for (; i < run_end; i++, idx++) {
+- if (noise_flag-- > 0)
+- offset[1] += get_bits(gb, 9) - 256;
+- else
+- offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+- if (offset[1] > 255U) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
+- "%s (%d) out of range.\n", sf_str[1], offset[1]);
+- return -1;
+- }
+- sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
+- }
+- } else {
+- for (; i < run_end; i++, idx++) {
+- offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+- if (offset[0] > 255U) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
+- "%s (%d) out of range.\n", sf_str[0], offset[0]);
+- return -1;
+- }
+- sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
+- }
+- }
+- }
+- }
+- return 0;
+-}
+-
+-/**
+- * Decode pulse data; reference: table 4.7.
+- */
+-static int decode_pulses(Pulse *pulse, GetBitContext *gb,
+- const uint16_t *swb_offset, int num_swb)
+-{
+- int i, pulse_swb;
+- pulse->num_pulse = get_bits(gb, 2) + 1;
+- pulse_swb = get_bits(gb, 6);
+- if (pulse_swb >= num_swb)
+- return -1;
+- pulse->pos[0] = swb_offset[pulse_swb];
+- pulse->pos[0] += get_bits(gb, 5);
+- if (pulse->pos[0] > 1023)
+- return -1;
+- pulse->amp[0] = get_bits(gb, 4);
+- for (i = 1; i < pulse->num_pulse; i++) {
+- pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
+- if (pulse->pos[i] > 1023)
+- return -1;
+- pulse->amp[i] = get_bits(gb, 4);
+- }
+- return 0;
+-}
+-
+-/**
+- * Decode Temporal Noise Shaping data; reference: table 4.48.
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
+- GetBitContext *gb, const IndividualChannelStream *ics)
+-{
+- int w, filt, i, coef_len, coef_res, coef_compress;
+- const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
+- const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
+- for (w = 0; w < ics->num_windows; w++) {
+- if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
+- coef_res = get_bits1(gb);
+-
+- for (filt = 0; filt < tns->n_filt[w]; filt++) {
+- int tmp2_idx;
+- tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
+-
+- if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
+- tns->order[w][filt], tns_max_order);
+- tns->order[w][filt] = 0;
+- return -1;
+- }
+- if (tns->order[w][filt]) {
+- tns->direction[w][filt] = get_bits1(gb);
+- coef_compress = get_bits1(gb);
+- coef_len = coef_res + 3 - coef_compress;
+- tmp2_idx = 2 * coef_compress + coef_res;
+-
+- for (i = 0; i < tns->order[w][filt]; i++)
+- tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
+- }
+- }
+- }
+- }
+- return 0;
+-}
+-
+-/**
+- * Decode Mid/Side data; reference: table 4.54.
+- *
+- * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
+- * [1] mask is decoded from bitstream; [2] mask is all 1s;
+- * [3] reserved for scalable AAC
+- */
+-static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
+- int ms_present)
+-{
+- int idx;
+- if (ms_present == 1) {
+- for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
+- cpe->ms_mask[idx] = get_bits1(gb);
+- } else if (ms_present == 2) {
+- memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
+- }
+-}
+-
+-#ifndef VMUL2
+-static inline float *VMUL2(float *dst, const float *v, unsigned idx,
+- const float *scale)
+-{
+- float s = *scale;
+- *dst++ = v[idx & 15] * s;
+- *dst++ = v[idx>>4 & 15] * s;
+- return dst;
+-}
+-#endif
+-
+-#ifndef VMUL4
+-static inline float *VMUL4(float *dst, const float *v, unsigned idx,
+- const float *scale)
+-{
+- float s = *scale;
+- *dst++ = v[idx & 3] * s;
+- *dst++ = v[idx>>2 & 3] * s;
+- *dst++ = v[idx>>4 & 3] * s;
+- *dst++ = v[idx>>6 & 3] * s;
+- return dst;
+-}
+-#endif
+-
+-#ifndef VMUL2S
+-static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
+- unsigned sign, const float *scale)
+-{
+- union float754 s0, s1;
+-
+- s0.f = s1.f = *scale;
+- s0.i ^= sign >> 1 << 31;
+- s1.i ^= sign << 31;
+-
+- *dst++ = v[idx & 15] * s0.f;
+- *dst++ = v[idx>>4 & 15] * s1.f;
+-
+- return dst;
+-}
+-#endif
+-
+-#ifndef VMUL4S
+-static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
+- unsigned sign, const float *scale)
+-{
+- unsigned nz = idx >> 12;
+- union float754 s = { .f = *scale };
+- union float754 t;
+-
+- t.i = s.i ^ (sign & 1<<31);
+- *dst++ = v[idx & 3] * t.f;
+-
+- sign <<= nz & 1; nz >>= 1;
+- t.i = s.i ^ (sign & 1<<31);
+- *dst++ = v[idx>>2 & 3] * t.f;
+-
+- sign <<= nz & 1; nz >>= 1;
+- t.i = s.i ^ (sign & 1<<31);
+- *dst++ = v[idx>>4 & 3] * t.f;
+-
+- sign <<= nz & 1; nz >>= 1;
+- t.i = s.i ^ (sign & 1<<31);
+- *dst++ = v[idx>>6 & 3] * t.f;
+-
+- return dst;
+-}
+-#endif
+-
+-/**
+- * Decode spectral data; reference: table 4.50.
+- * Dequantize and scale spectral data; reference: 4.6.3.3.
+- *
+- * @param coef array of dequantized, scaled spectral data
+- * @param sf array of scalefactors or intensity stereo positions
+- * @param pulse_present set if pulses are present
+- * @param pulse pointer to pulse data struct
+- * @param band_type array of the used band type
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
+- GetBitContext *gb, const float sf[120],
+- int pulse_present, const Pulse *pulse,
+- const IndividualChannelStream *ics,
+- enum BandType band_type[120])
+-{
+- int i, k, g, idx = 0;
+- const int c = 1024 / ics->num_windows;
+- const uint16_t *offsets = ics->swb_offset;
+- float *coef_base = coef;
+- int err_idx;
+-
+- for (g = 0; g < ics->num_windows; g++)
+- memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
+-
+- for (g = 0; g < ics->num_window_groups; g++) {
+- unsigned g_len = ics->group_len[g];
+-
+- for (i = 0; i < ics->max_sfb; i++, idx++) {
+- const unsigned cbt_m1 = band_type[idx] - 1;
+- float *cfo = coef + offsets[i];
+- int off_len = offsets[i + 1] - offsets[i];
+- int group;
+-
+- if (cbt_m1 >= INTENSITY_BT2 - 1) {
+- for (group = 0; group < g_len; group++, cfo+=128) {
+- memset(cfo, 0, off_len * sizeof(float));
+- }
+- } else if (cbt_m1 == NOISE_BT - 1) {
+- for (group = 0; group < g_len; group++, cfo+=128) {
+- float scale;
+- float band_energy;
+-
+- for (k = 0; k < off_len; k++) {
+- ac->random_state = lcg_random(ac->random_state);
+- cfo[k] = ac->random_state;
+- }
+-
+- band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
+- scale = sf[idx] / sqrtf(band_energy);
+- ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
+- }
+- } else {
+- const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
+- const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
+- VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
+- const int cb_size = ff_aac_spectral_sizes[cbt_m1];
+- OPEN_READER(re, gb);
+-
+- switch (cbt_m1 >> 1) {
+- case 0:
+- for (group = 0; group < g_len; group++, cfo+=128) {
+- float *cf = cfo;
+- int len = off_len;
+-
+- do {
+- int code;
+- unsigned cb_idx;
+-
+- UPDATE_CACHE(re, gb);
+- GET_VLC(code, re, gb, vlc_tab, 8, 2);
+-
+- if (code >= cb_size) {
+- err_idx = code;
+- goto err_cb_overflow;
+- }
+-
+- cb_idx = cb_vector_idx[code];
+- cf = VMUL4(cf, vq, cb_idx, sf + idx);
+- } while (len -= 4);
+- }
+- break;
+-
+- case 1:
+- for (group = 0; group < g_len; group++, cfo+=128) {
+- float *cf = cfo;
+- int len = off_len;
+-
+- do {
+- int code;
+- unsigned nnz;
+- unsigned cb_idx;
+- uint32_t bits;
+-
+- UPDATE_CACHE(re, gb);
+- GET_VLC(code, re, gb, vlc_tab, 8, 2);
+-
+- if (code >= cb_size) {
+- err_idx = code;
+- goto err_cb_overflow;
+- }
+-
+-#if MIN_CACHE_BITS < 20
+- UPDATE_CACHE(re, gb);
+-#endif
+- cb_idx = cb_vector_idx[code];
+- nnz = cb_idx >> 8 & 15;
+- bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
+- LAST_SKIP_BITS(re, gb, nnz);
+- cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
+- } while (len -= 4);
+- }
+- break;
+-
+- case 2:
+- for (group = 0; group < g_len; group++, cfo+=128) {
+- float *cf = cfo;
+- int len = off_len;
+-
+- do {
+- int code;
+- unsigned cb_idx;
+-
+- UPDATE_CACHE(re, gb);
+- GET_VLC(code, re, gb, vlc_tab, 8, 2);
+-
+- if (code >= cb_size) {
+- err_idx = code;
+- goto err_cb_overflow;
+- }
+-
+- cb_idx = cb_vector_idx[code];
+- cf = VMUL2(cf, vq, cb_idx, sf + idx);
+- } while (len -= 2);
+- }
+- break;
+-
+- case 3:
+- case 4:
+- for (group = 0; group < g_len; group++, cfo+=128) {
+- float *cf = cfo;
+- int len = off_len;
+-
+- do {
+- int code;
+- unsigned nnz;
+- unsigned cb_idx;
+- unsigned sign;
+-
+- UPDATE_CACHE(re, gb);
+- GET_VLC(code, re, gb, vlc_tab, 8, 2);
+-
+- if (code >= cb_size) {
+- err_idx = code;
+- goto err_cb_overflow;
+- }
+-
+- cb_idx = cb_vector_idx[code];
+- nnz = cb_idx >> 8 & 15;
+- sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
+- LAST_SKIP_BITS(re, gb, nnz);
+- cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
+- } while (len -= 2);
+- }
+- break;
+-
+- default:
+- for (group = 0; group < g_len; group++, cfo+=128) {
+- float *cf = cfo;
+- uint32_t *icf = (uint32_t *) cf;
+- int len = off_len;
+-
+- do {
+- int code;
+- unsigned nzt, nnz;
+- unsigned cb_idx;
+- uint32_t bits;
+- int j;
+-
+- UPDATE_CACHE(re, gb);
+- GET_VLC(code, re, gb, vlc_tab, 8, 2);
+-
+- if (!code) {
+- *icf++ = 0;
+- *icf++ = 0;
+- continue;
+- }
+-
+- if (code >= cb_size) {
+- err_idx = code;
+- goto err_cb_overflow;
+- }
+-
+- cb_idx = cb_vector_idx[code];
+- nnz = cb_idx >> 12;
+- nzt = cb_idx >> 8;
+- bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
+- LAST_SKIP_BITS(re, gb, nnz);
+-
+- for (j = 0; j < 2; j++) {
+- if (nzt & 1<<j) {
+- uint32_t b;
+- int n;
+- /* The total length of escape_sequence must be < 22 bits according
+- to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
+- UPDATE_CACHE(re, gb);
+- b = GET_CACHE(re, gb);
+- b = 31 - av_log2(~b);
+-
+- if (b > 8) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
+- return -1;
+- }
+-
+-#if MIN_CACHE_BITS < 21
+- LAST_SKIP_BITS(re, gb, b + 1);
+- UPDATE_CACHE(re, gb);
+-#else
+- SKIP_BITS(re, gb, b + 1);
+-#endif
+- b += 4;
+- n = (1 << b) + SHOW_UBITS(re, gb, b);
+- LAST_SKIP_BITS(re, gb, b);
+- *icf++ = cbrt_tab[n] | (bits & 1<<31);
+- bits <<= 1;
+- } else {
+- unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
+- *icf++ = (bits & 1<<31) | v;
+- bits <<= !!v;
+- }
+- cb_idx >>= 4;
+- }
+- } while (len -= 2);
+-
+- ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
+- }
+- }
+-
+- CLOSE_READER(re, gb);
+- }
+- }
+- coef += g_len << 7;
+- }
+-
+- if (pulse_present) {
+- idx = 0;
+- for (i = 0; i < pulse->num_pulse; i++) {
+- float co = coef_base[ pulse->pos[i] ];
+- while (offsets[idx + 1] <= pulse->pos[i])
+- idx++;
+- if (band_type[idx] != NOISE_BT && sf[idx]) {
+- float ico = -pulse->amp[i];
+- if (co) {
+- co /= sf[idx];
+- ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
+- }
+- coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
+- }
+- }
+- }
+- return 0;
+-
+-err_cb_overflow:
+- av_log(ac->avccontext, AV_LOG_ERROR,
+- "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n",
+- band_type[idx], err_idx, ff_aac_spectral_sizes[band_type[idx]]);
+- return -1;
+-}
+-
+-static av_always_inline float flt16_round(float pf)
+-{
+- union float754 tmp;
+- tmp.f = pf;
+- tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
+- return tmp.f;
+-}
+-
+-static av_always_inline float flt16_even(float pf)
+-{
+- union float754 tmp;
+- tmp.f = pf;
+- tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
+- return tmp.f;
+-}
+-
+-static av_always_inline float flt16_trunc(float pf)
+-{
+- union float754 pun;
+- pun.f = pf;
+- pun.i &= 0xFFFF0000U;
+- return pun.f;
+-}
+-
+-static av_always_inline void predict(AACContext *ac, PredictorState *ps, float *coef,
+- int output_enable)
+-{
+- const float a = 0.953125; // 61.0 / 64
+- const float alpha = 0.90625; // 29.0 / 32
+- float e0, e1;
+- float pv;
+- float k1, k2;
+-
+- k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
+- k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
+-
+- pv = flt16_round(k1 * ps->r0 + k2 * ps->r1);
+- if (output_enable)
+- *coef += pv * ac->sf_scale;
+-
+- e0 = *coef / ac->sf_scale;
+- e1 = e0 - k1 * ps->r0;
+-
+- ps->cor1 = flt16_trunc(alpha * ps->cor1 + ps->r1 * e1);
+- ps->var1 = flt16_trunc(alpha * ps->var1 + 0.5 * (ps->r1 * ps->r1 + e1 * e1));
+- ps->cor0 = flt16_trunc(alpha * ps->cor0 + ps->r0 * e0);
+- ps->var0 = flt16_trunc(alpha * ps->var0 + 0.5 * (ps->r0 * ps->r0 + e0 * e0));
+-
+- ps->r1 = flt16_trunc(a * (ps->r0 - k1 * e0));
+- ps->r0 = flt16_trunc(a * e0);
+-}
+-
+-/**
+- * Apply AAC-Main style frequency domain prediction.
+- */
+-static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
+-{
+- int sfb, k;
+-
+- if (!sce->ics.predictor_initialized) {
+- reset_all_predictors(sce->predictor_state);
+- sce->ics.predictor_initialized = 1;
+- }
+-
+- if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
+- for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
+- for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
+- predict(ac, &sce->predictor_state[k], &sce->coeffs[k],
+- sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
+- }
+- }
+- if (sce->ics.predictor_reset_group)
+- reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
+- } else
+- reset_all_predictors(sce->predictor_state);
+-}
+-
+-/**
+- * Decode an individual_channel_stream payload; reference: table 4.44.
+- *
+- * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
+- * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_ics(AACContext *ac, SingleChannelElement *sce,
+- GetBitContext *gb, int common_window, int scale_flag)
+-{
+- Pulse pulse;
+- TemporalNoiseShaping *tns = &sce->tns;
+- IndividualChannelStream *ics = &sce->ics;
+- float *out = sce->coeffs;
+- int global_gain, pulse_present = 0;
+-
+- /* This assignment is to silence a GCC warning about the variable being used
+- * uninitialized when in fact it always is.
+- */
+- pulse.num_pulse = 0;
+-
+- global_gain = get_bits(gb, 8);
+-
+- if (!common_window && !scale_flag) {
+- if (decode_ics_info(ac, ics, gb, 0) < 0)
+- return -1;
+- }
+-
+- if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
+- return -1;
+- if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
+- return -1;
+-
+- pulse_present = 0;
+- if (!scale_flag) {
+- if ((pulse_present = get_bits1(gb))) {
+- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
+- return -1;
+- }
+- if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
+- return -1;
+- }
+- }
+- if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
+- return -1;
+- if (get_bits1(gb)) {
+- av_log_missing_feature(ac->avccontext, "SSR", 1);
+- return -1;
+- }
+- }
+-
+- if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
+- return -1;
+-
+- if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
+- apply_prediction(ac, sce);
+-
+- return 0;
+-}
+-
+-/**
+- * Mid/Side stereo decoding; reference: 4.6.8.1.3.
+- */
+-static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
+-{
+- const IndividualChannelStream *ics = &cpe->ch[0].ics;
+- float *ch0 = cpe->ch[0].coeffs;
+- float *ch1 = cpe->ch[1].coeffs;
+- int g, i, group, idx = 0;
+- const uint16_t *offsets = ics->swb_offset;
+- for (g = 0; g < ics->num_window_groups; g++) {
+- for (i = 0; i < ics->max_sfb; i++, idx++) {
+- if (cpe->ms_mask[idx] &&
+- cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
+- for (group = 0; group < ics->group_len[g]; group++) {
+- ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
+- ch1 + group * 128 + offsets[i],
+- offsets[i+1] - offsets[i]);
+- }
+- }
+- }
+- ch0 += ics->group_len[g] * 128;
+- ch1 += ics->group_len[g] * 128;
+- }
+-}
+-
+-/**
+- * intensity stereo decoding; reference: 4.6.8.2.3
+- *
+- * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
+- * [1] mask is decoded from bitstream; [2] mask is all 1s;
+- * [3] reserved for scalable AAC
+- */
+-static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
+-{
+- const IndividualChannelStream *ics = &cpe->ch[1].ics;
+- SingleChannelElement *sce1 = &cpe->ch[1];
+- float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
+- const uint16_t *offsets = ics->swb_offset;
+- int g, group, i, k, idx = 0;
+- int c;
+- float scale;
+- for (g = 0; g < ics->num_window_groups; g++) {
+- for (i = 0; i < ics->max_sfb;) {
+- if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
+- const int bt_run_end = sce1->band_type_run_end[idx];
+- for (; i < bt_run_end; i++, idx++) {
+- c = -1 + 2 * (sce1->band_type[idx] - 14);
+- if (ms_present)
+- c *= 1 - 2 * cpe->ms_mask[idx];
+- scale = c * sce1->sf[idx];
+- for (group = 0; group < ics->group_len[g]; group++)
+- for (k = offsets[i]; k < offsets[i + 1]; k++)
+- coef1[group * 128 + k] = scale * coef0[group * 128 + k];
+- }
+- } else {
+- int bt_run_end = sce1->band_type_run_end[idx];
+- idx += bt_run_end - i;
+- i = bt_run_end;
+- }
+- }
+- coef0 += ics->group_len[g] * 128;
+- coef1 += ics->group_len[g] * 128;
+- }
+-}
+-
+-/**
+- * Decode a channel_pair_element; reference: table 4.4.
+- *
+- * @param elem_id Identifies the instance of a syntax element.
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
+-{
+- int i, ret, common_window, ms_present = 0;
+-
+- common_window = get_bits1(gb);
+- if (common_window) {
+- if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
+- return -1;
+- i = cpe->ch[1].ics.use_kb_window[0];
+- cpe->ch[1].ics = cpe->ch[0].ics;
+- cpe->ch[1].ics.use_kb_window[1] = i;
+- ms_present = get_bits(gb, 2);
+- if (ms_present == 3) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
+- return -1;
+- } else if (ms_present)
+- decode_mid_side_stereo(cpe, gb, ms_present);
+- }
+- if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
+- return ret;
+- if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
+- return ret;
+-
+- if (common_window) {
+- if (ms_present)
+- apply_mid_side_stereo(ac, cpe);
+- if (ac->m4ac.object_type == AOT_AAC_MAIN) {
+- apply_prediction(ac, &cpe->ch[0]);
+- apply_prediction(ac, &cpe->ch[1]);
+- }
+- }
+-
+- apply_intensity_stereo(cpe, ms_present);
+- return 0;
+-}
+-
+-/**
+- * Decode coupling_channel_element; reference: table 4.8.
+- *
+- * @param elem_id Identifies the instance of a syntax element.
+- *
+- * @return Returns error status. 0 - OK, !0 - error
+- */
+-static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
+-{
+- int num_gain = 0;
+- int c, g, sfb, ret;
+- int sign;
+- float scale;
+- SingleChannelElement *sce = &che->ch[0];
+- ChannelCoupling *coup = &che->coup;
+-
+- coup->coupling_point = 2 * get_bits1(gb);
+- coup->num_coupled = get_bits(gb, 3);
+- for (c = 0; c <= coup->num_coupled; c++) {
+- num_gain++;
+- coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
+- coup->id_select[c] = get_bits(gb, 4);
+- if (coup->type[c] == TYPE_CPE) {
+- coup->ch_select[c] = get_bits(gb, 2);
+- if (coup->ch_select[c] == 3)
+- num_gain++;
+- } else
+- coup->ch_select[c] = 2;
+- }
+- coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
+-
+- sign = get_bits(gb, 1);
+- scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));
+-
+- if ((ret = decode_ics(ac, sce, gb, 0, 0)))
+- return ret;
+-
+- for (c = 0; c < num_gain; c++) {
+- int idx = 0;
+- int cge = 1;
+- int gain = 0;
+- float gain_cache = 1.;
+- if (c) {
+- cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
+- gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
+- gain_cache = pow(scale, -gain);
+- }
+- if (coup->coupling_point == AFTER_IMDCT) {
+- coup->gain[c][0] = gain_cache;
+- } else {
+- for (g = 0; g < sce->ics.num_window_groups; g++) {
+- for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
+- if (sce->band_type[idx] != ZERO_BT) {
+- if (!cge) {
+- int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+- if (t) {
+- int s = 1;
+- t = gain += t;
+- if (sign) {
+- s -= 2 * (t & 0x1);
+- t >>= 1;
+- }
+- gain_cache = pow(scale, -t) * s;
+- }
+- }
+- coup->gain[c][idx] = gain_cache;
+- }
+- }
+- }
+- }
+- }
+- return 0;
+-}
+-
+-/**
+- * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
+- *
+- * @return Returns number of bytes consumed.
+- */
+-static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
+- GetBitContext *gb)
+-{
+- int i;
+- int num_excl_chan = 0;
+-
+- do {
+- for (i = 0; i < 7; i++)
+- che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
+- } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
+-
+- return num_excl_chan / 7;
+-}
+-
+-/**
+- * Decode dynamic range information; reference: table 4.52.
+- *
+- * @param cnt length of TYPE_FIL syntactic element in bytes
+- *
+- * @return Returns number of bytes consumed.
+- */
+-static int decode_dynamic_range(DynamicRangeControl *che_drc,
+- GetBitContext *gb, int cnt)
+-{
+- int n = 1;
+- int drc_num_bands = 1;
+- int i;
+-
+- /* pce_tag_present? */
+- if (get_bits1(gb)) {
+- che_drc->pce_instance_tag = get_bits(gb, 4);
+- skip_bits(gb, 4); // tag_reserved_bits
+- n++;
+- }
+-
+- /* excluded_chns_present? */
+- if (get_bits1(gb)) {
+- n += decode_drc_channel_exclusions(che_drc, gb);
+- }
+-
+- /* drc_bands_present? */
+- if (get_bits1(gb)) {
+- che_drc->band_incr = get_bits(gb, 4);
+- che_drc->interpolation_scheme = get_bits(gb, 4);
+- n++;
+- drc_num_bands += che_drc->band_incr;
+- for (i = 0; i < drc_num_bands; i++) {
+- che_drc->band_top[i] = get_bits(gb, 8);
+- n++;
+- }
+- }
+-
+- /* prog_ref_level_present? */
+- if (get_bits1(gb)) {
+- che_drc->prog_ref_level = get_bits(gb, 7);
+- skip_bits1(gb); // prog_ref_level_reserved_bits
+- n++;
+- }
+-
+- for (i = 0; i < drc_num_bands; i++) {
+- che_drc->dyn_rng_sgn[i] = get_bits1(gb);
+- che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
+- n++;
+- }
+-
+- return n;
+-}
+-
+-/**
+- * Decode extension data (incomplete); reference: table 4.51.
+- *
+- * @param cnt length of TYPE_FIL syntactic element in bytes
+- *
+- * @return Returns number of bytes consumed
+- */
+-static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
+- ChannelElement *che, enum RawDataBlockType elem_type)
+-{
+- int crc_flag = 0;
+- int res = cnt;
+- switch (get_bits(gb, 4)) { // extension type
+- case EXT_SBR_DATA_CRC:
+- crc_flag++;
+- case EXT_SBR_DATA:
+- if (!che) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
+- return res;
+- } else if (!ac->m4ac.sbr) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
+- skip_bits_long(gb, 8 * cnt - 4);
+- return res;
+- } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
+- av_log(ac->avccontext, 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 {
+- ac->m4ac.sbr = 1;
+- }
+- res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
+- break;
+- case EXT_DYNAMIC_RANGE:
+- res = decode_dynamic_range(&ac->che_drc, gb, cnt);
+- break;
+- case EXT_FILL:
+- case EXT_FILL_DATA:
+- case EXT_DATA_ELEMENT:
+- default:
+- skip_bits_long(gb, 8 * cnt - 4);
+- break;
+- };
+- return res;
+-}
+-
+-/**
+- * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
+- *
+- * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
+- * @param coef spectral coefficients
+- */
+-static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
+- IndividualChannelStream *ics, int decode)
+-{
+- const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
+- int w, filt, m, i;
+- int bottom, top, order, start, end, size, inc;
+- float lpc[TNS_MAX_ORDER];
+-
+- for (w = 0; w < ics->num_windows; w++) {
+- bottom = ics->num_swb;
+- for (filt = 0; filt < tns->n_filt[w]; filt++) {
+- top = bottom;
+- bottom = FFMAX(0, top - tns->length[w][filt]);
+- order = tns->order[w][filt];
+- if (order == 0)
+- continue;
+-
+- // tns_decode_coef
+- compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
+-
+- start = ics->swb_offset[FFMIN(bottom, mmm)];
+- end = ics->swb_offset[FFMIN( top, mmm)];
+- if ((size = end - start) <= 0)
+- continue;
+- if (tns->direction[w][filt]) {
+- inc = -1;
+- start = end - 1;
+- } else {
+- inc = 1;
+- }
+- start += w * 128;
+-
+- // ar filter
+- for (m = 0; m < size; m++, start += inc)
+- for (i = 1; i <= FFMIN(m, order); i++)
+- coef[start] -= coef[start - i * inc] * lpc[i - 1];
+- }
+- }
+-}
+-
+-/**
+- * Conduct IMDCT and windowing.
+- */
+-static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
+-{
+- IndividualChannelStream *ics = &sce->ics;
+- float *in = sce->coeffs;
+- float *out = sce->ret;
+- float *saved = sce->saved;
+- const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
+- const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
+- const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
+- float *buf = ac->buf_mdct;
+- float *temp = ac->temp;
+- int i;
+-
+- // imdct
+- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+- if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
+- av_log(ac->avccontext, AV_LOG_WARNING,
+- "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
+- "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
+- for (i = 0; i < 1024; i += 128)
+- ff_imdct_half(&ac->mdct_small, buf + i, in + i);
+- } else
+- ff_imdct_half(&ac->mdct, buf, in);
+-
+- /* window overlapping
+- * NOTE: To simplify the overlapping code, all 'meaningless' short to long
+- * and long to short transitions are considered to be short to short
+- * transitions. This leaves just two cases (long to long and short to short)
+- * with a little special sauce for EIGHT_SHORT_SEQUENCE.
+- */
+- if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
+- (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
+- ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, bias, 512);
+- } else {
+- for (i = 0; i < 448; i++)
+- out[i] = saved[i] + bias;
+-
+- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+- ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, bias, 64);
+- ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, bias, 64);
+- ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, bias, 64);
+- ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, bias, 64);
+- ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, bias, 64);
+- memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
+- } else {
+- ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, bias, 64);
+- for (i = 576; i < 1024; i++)
+- out[i] = buf[i-512] + bias;
+- }
+- }
+-
+- // buffer update
+- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+- for (i = 0; i < 64; i++)
+- saved[i] = temp[64 + i] - bias;
+- ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
+- ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
+- ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
+- memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
+- } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
+- memcpy( saved, buf + 512, 448 * sizeof(float));
+- memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
+- } else { // LONG_STOP or ONLY_LONG
+- memcpy( saved, buf + 512, 512 * sizeof(float));
+- }
+-}
+-
+-/**
+- * Apply dependent channel coupling (applied before IMDCT).
+- *
+- * @param index index into coupling gain array
+- */
+-static void apply_dependent_coupling(AACContext *ac,
+- SingleChannelElement *target,
+- ChannelElement *cce, int index)
+-{
+- IndividualChannelStream *ics = &cce->ch[0].ics;
+- const uint16_t *offsets = ics->swb_offset;
+- float *dest = target->coeffs;
+- const float *src = cce->ch[0].coeffs;
+- int g, i, group, k, idx = 0;
+- if (ac->m4ac.object_type == AOT_AAC_LTP) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
+- "Dependent coupling is not supported together with LTP\n");
+- return;
+- }
+- for (g = 0; g < ics->num_window_groups; g++) {
+- for (i = 0; i < ics->max_sfb; i++, idx++) {
+- if (cce->ch[0].band_type[idx] != ZERO_BT) {
+- const float gain = cce->coup.gain[index][idx];
+- for (group = 0; group < ics->group_len[g]; group++) {
+- for (k = offsets[i]; k < offsets[i + 1]; k++) {
+- // XXX dsputil-ize
+- dest[group * 128 + k] += gain * src[group * 128 + k];
+- }
+- }
+- }
+- }
+- dest += ics->group_len[g] * 128;
+- src += ics->group_len[g] * 128;
+- }
+-}
+-
+-/**
+- * Apply independent channel coupling (applied after IMDCT).
+- *
+- * @param index index into coupling gain array
+- */
+-static void apply_independent_coupling(AACContext *ac,
+- SingleChannelElement *target,
+- ChannelElement *cce, int index)
+-{
+- int i;
+- const float gain = cce->coup.gain[index][0];
+- const float bias = ac->add_bias;
+- const float *src = cce->ch[0].ret;
+- float *dest = target->ret;
+- const int len = 1024 << (ac->m4ac.sbr == 1);
+-
+- for (i = 0; i < len; i++)
+- dest[i] += gain * (src[i] - bias);
+-}
+-
+-/**
+- * channel coupling transformation interface
+- *
+- * @param index index into coupling gain array
+- * @param apply_coupling_method pointer to (in)dependent coupling function
+- */
+-static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
+- enum RawDataBlockType type, int elem_id,
+- enum CouplingPoint coupling_point,
+- void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
+-{
+- int i, c;
+-
+- for (i = 0; i < MAX_ELEM_ID; i++) {
+- ChannelElement *cce = ac->che[TYPE_CCE][i];
+- int index = 0;
+-
+- if (cce && cce->coup.coupling_point == coupling_point) {
+- ChannelCoupling *coup = &cce->coup;
+-
+- for (c = 0; c <= coup->num_coupled; c++) {
+- if (coup->type[c] == type && coup->id_select[c] == elem_id) {
+- if (coup->ch_select[c] != 1) {
+- apply_coupling_method(ac, &cc->ch[0], cce, index);
+- if (coup->ch_select[c] != 0)
+- index++;
+- }
+- if (coup->ch_select[c] != 2)
+- apply_coupling_method(ac, &cc->ch[1], cce, index++);
+- } else
+- index += 1 + (coup->ch_select[c] == 3);
+- }
+- }
+- }
+-}
+-
+-/**
+- * Convert spectral data to float samples, applying all supported tools as appropriate.
+- */
+-static void spectral_to_sample(AACContext *ac)
+-{
+- int i, type;
+- float imdct_bias = (ac->m4ac.sbr <= 0) ? ac->add_bias : 0.0f;
+- for (type = 3; type >= 0; type--) {
+- for (i = 0; i < MAX_ELEM_ID; i++) {
+- ChannelElement *che = ac->che[type][i];
+- if (che) {
+- if (type <= TYPE_CPE)
+- apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
+- if (che->ch[0].tns.present)
+- apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
+- if (che->ch[1].tns.present)
+- apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
+- if (type <= TYPE_CPE)
+- apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
+- if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
+- imdct_and_windowing(ac, &che->ch[0], imdct_bias);
+- if (type == TYPE_CPE) {
+- imdct_and_windowing(ac, &che->ch[1], imdct_bias);
+- }
+- if (ac->m4ac.sbr > 0) {
+- ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
+- }
+- }
+- if (type <= TYPE_CCE)
+- apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
+- }
+- }
+- }
+-}
+-
+-static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
+-{
+- int size;
+- AACADTSHeaderInfo hdr_info;
+-
+- size = ff_aac_parse_header(gb, &hdr_info);
+- if (size > 0) {
+- if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
+- enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
+- memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
+- ac->m4ac.chan_config = hdr_info.chan_config;
+- if (set_default_channel_config(ac, new_che_pos, hdr_info.chan_config))
+- return -7;
+- if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
+- return -7;
+- } else if (ac->output_configured != OC_LOCKED) {
+- ac->output_configured = OC_NONE;
+- }
+- if (ac->output_configured != OC_LOCKED)
+- ac->m4ac.sbr = -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;
+- if (!ac->avccontext->sample_rate)
+- ac->avccontext->sample_rate = hdr_info.sample_rate;
+- if (hdr_info.num_aac_frames == 1) {
+- if (!hdr_info.crc_absent)
+- skip_bits(gb, 16);
+- } else {
+- av_log_missing_feature(ac->avccontext, "More than one AAC RDB per ADTS frame is", 0);
+- return -1;
+- }
+- }
+- return size;
+-}
+-
+-static int aac_decode_frame(AVCodecContext *avccontext, void *data,
+- int *data_size, AVPacket *avpkt)
+-{
+- const uint8_t *buf = avpkt->data;
+- int buf_size = avpkt->size;
+- AACContext *ac = avccontext->priv_data;
+- ChannelElement *che = NULL, *che_prev = NULL;
+- GetBitContext gb;
+- enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
+- int err, elem_id, data_size_tmp;
+- int buf_consumed;
+- int samples = 1024, multiplier;
+- int buf_offset;
+-
+- init_get_bits(&gb, buf, buf_size * 8);
+-
+- if (show_bits(&gb, 12) == 0xfff) {
+- if (parse_adts_frame_header(ac, &gb) < 0) {
+- av_log(avccontext, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
+- return -1;
+- }
+- if (ac->m4ac.sampling_index > 12) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
+- return -1;
+- }
+- }
+-
+- // parse
+- while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
+- elem_id = get_bits(&gb, 4);
+-
+- if (elem_type < TYPE_DSE && !(che=get_che(ac, elem_type, elem_id))) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "channel element %d.%d is not allocated\n", elem_type, elem_id);
+- return -1;
+- }
+-
+- switch (elem_type) {
+-
+- case TYPE_SCE:
+- err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
+- break;
+-
+- case TYPE_CPE:
+- err = decode_cpe(ac, &gb, che);
+- break;
+-
+- case TYPE_CCE:
+- err = decode_cce(ac, &gb, che);
+- break;
+-
+- case TYPE_LFE:
+- err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
+- break;
+-
+- case TYPE_DSE:
+- err = skip_data_stream_element(ac, &gb);
+- break;
+-
+- case TYPE_PCE: {
+- enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
+- memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
+- if ((err = decode_pce(ac, new_che_pos, &gb)))
+- break;
+- if (ac->output_configured > OC_TRIAL_PCE)
+- av_log(avccontext, AV_LOG_ERROR,
+- "Not evaluating a further program_config_element as this construct is dubious at best.\n");
+- else
+- err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
+- break;
+- }
+-
+- case TYPE_FIL:
+- if (elem_id == 15)
+- elem_id += get_bits(&gb, 8) - 1;
+- if (get_bits_left(&gb) < 8 * elem_id) {
+- av_log(avccontext, AV_LOG_ERROR, overread_err);
+- return -1;
+- }
+- while (elem_id > 0)
+- elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev);
+- err = 0; /* FIXME */
+- break;
+-
+- default:
+- err = -1; /* should not happen, but keeps compiler happy */
+- break;
+- }
+-
+- che_prev = che;
+- elem_type_prev = elem_type;
+-
+- if (err)
+- return err;
+-
+- if (get_bits_left(&gb) < 3) {
+- av_log(avccontext, AV_LOG_ERROR, overread_err);
+- return -1;
+- }
+- }
+-
+- spectral_to_sample(ac);
+-
+- multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
+- samples <<= multiplier;
+- if (ac->output_configured < OC_LOCKED) {
+- avccontext->sample_rate = ac->m4ac.sample_rate << multiplier;
+- avccontext->frame_size = samples;
+- }
+-
+- data_size_tmp = samples * avccontext->channels * sizeof(int16_t);
+- if (*data_size < data_size_tmp) {
+- av_log(avccontext, AV_LOG_ERROR,
+- "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
+- *data_size, data_size_tmp);
+- return -1;
+- }
+- *data_size = data_size_tmp;
+-
+- ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avccontext->channels);
+-
+- if (ac->output_configured)
+- ac->output_configured = OC_LOCKED;
+-
+- buf_consumed = (get_bits_count(&gb) + 7) >> 3;
+- for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
+- if (buf[buf_offset])
+- break;
+-
+- return buf_size > buf_offset ? buf_consumed : buf_size;
+-}
+-
+-static av_cold int aac_decode_close(AVCodecContext *avccontext)
+-{
+- AACContext *ac = avccontext->priv_data;
+- int i, type;
+-
+- for (i = 0; i < MAX_ELEM_ID; i++) {
+- for (type = 0; type < 4; type++) {
+- if (ac->che[type][i])
+- ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
+- av_freep(&ac->che[type][i]);
+- }
+- }
+-
+- ff_mdct_end(&ac->mdct);
+- ff_mdct_end(&ac->mdct_small);
+- return 0;
+-}
+-
+-AVCodec aac_decoder = {
+- "aac",
+- AVMEDIA_TYPE_AUDIO,
+- CODEC_ID_AAC,
+- sizeof(AACContext),
+- aac_decode_init,
+- NULL,
+- aac_decode_close,
+- aac_decode_frame,
+- .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
+- .sample_fmts = (const enum SampleFormat[]) {
+- SAMPLE_FMT_S16,SAMPLE_FMT_NONE
+- },
+- .channel_layouts = aac_channel_layout,
+-};
+--- a/libavcodec/aacenc.c
++++ b/libavcodec/aacenc.c
+@@ -201,13 +201,11 @@ static av_cold int aac_encode_init(AVCod
+ lengths[1] = ff_aac_num_swb_128[i];
+ ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
+ s->psypp = ff_psy_preprocess_init(avctx);
+- s->coder = &ff_aac_coders[0];
++ s->coder = &ff_aac_coders[2];
+
+ s->lambda = avctx->global_quality ? avctx->global_quality : 120;
+-#if !CONFIG_HARDCODED_TABLES
+- for (i = 0; i < 428; i++)
+- ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
+-#endif /* CONFIG_HARDCODED_TABLES */
++
++ ff_aac_tableinit();
+
+ if (avctx->channels > 5)
+ av_log(avctx, AV_LOG_ERROR, "This encoder does not yet enforce the restrictions on LFEs. "
+@@ -234,25 +232,21 @@ static void apply_window_and_mdct(AVCode
+ s->output[i] = sce->saved[i];
+ }
+ if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
+- j = channel;
+- for (i = 0; i < 1024; i++, j += avctx->channels) {
++ for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) {
+ s->output[i+1024] = audio[j] * lwindow[1024 - i - 1];
+ sce->saved[i] = audio[j] * lwindow[i];
+ }
+ } else {
+- j = channel;
+- for (i = 0; i < 448; i++, j += avctx->channels)
++ for (i = 0, j = channel; i < 448; i++, j += avctx->channels)
+ s->output[i+1024] = audio[j];
+- for (i = 448; i < 576; i++, j += avctx->channels)
++ for (; i < 576; i++, j += avctx->channels)
+ s->output[i+1024] = audio[j] * swindow[576 - i - 1];
+ memset(s->output+1024+576, 0, sizeof(s->output[0]) * 448);
+- j = channel;
+- for (i = 0; i < 1024; i++, j += avctx->channels)
++ for (i = 0, j = channel; i < 1024; i++, j += avctx->channels)
+ sce->saved[i] = audio[j];
+ }
+ ff_mdct_calc(&s->mdct1024, sce->coeffs, s->output);
+ } else {
+- j = channel;
+ for (k = 0; k < 1024; k += 128) {
+ for (i = 448 + k; i < 448 + k + 256; i++)
+ s->output[i - 448 - k] = (i < 1024)
+@@ -262,8 +256,7 @@ static void apply_window_and_mdct(AVCode
+ s->dsp.vector_fmul_reverse(s->output+128, s->output+128, swindow, 128);
+ ff_mdct_calc(&s->mdct128, sce->coeffs + k, s->output);
+ }
+- j = channel;
+- for (i = 0; i < 1024; i++, j += avctx->channels)
++ for (i = 0, j = channel; i < 1024; i++, j += avctx->channels)
+ sce->saved[i] = audio[j];
+ }
+ }
+@@ -562,6 +555,7 @@ static int aac_encode_frame(AVCodecConte
+ cpe = &s->cpe[i];
+ for (j = 0; j < chans; j++) {
+ s->cur_channel = start_ch + j;
++ ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
+ s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], s->lambda);
+ }
+ cpe->common_window = 0;
+@@ -592,7 +586,6 @@ static int aac_encode_frame(AVCodecConte
+ }
+ for (j = 0; j < chans; j++) {
+ s->cur_channel = start_ch + j;
+- ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
+ encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window);
+ }
+ start_ch += chans;
+--- a/libavcodec/aacenc.h
++++ b/libavcodec/aacenc.h
+@@ -64,7 +64,7 @@ typedef struct AACEncContext {
+ int cur_channel;
+ int last_frame;
+ float lambda;
+- DECLARE_ALIGNED(16, int, qcoefs)[96][2]; ///< quantized coefficients
++ DECLARE_ALIGNED(16, int, qcoefs)[96]; ///< quantized coefficients
+ DECLARE_ALIGNED(16, float, scoefs)[1024]; ///< scaled coefficients
+ } AACEncContext;
+
+--- /dev/null
++++ b/libavcodec/aacdec.c
+@@ -0,0 +1,2142 @@
++/*
++ * AAC decoder
++ * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
++ * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
++ *
++ * 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
++ */
++
++/**
++ * @file
++ * AAC decoder
++ * @author Oded Shimon ( ods15 ods15 dyndns org )
++ * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
++ */
++
++/*
++ * supported tools
++ *
++ * Support? Name
++ * N (code in SoC repo) gain control
++ * Y block switching
++ * Y window shapes - standard
++ * N window shapes - Low Delay
++ * Y filterbank - standard
++ * N (code in SoC repo) filterbank - Scalable Sample Rate
++ * Y Temporal Noise Shaping
++ * N (code in SoC repo) Long Term Prediction
++ * Y intensity stereo
++ * Y channel coupling
++ * Y frequency domain prediction
++ * Y Perceptual Noise Substitution
++ * Y Mid/Side stereo
++ * N Scalable Inverse AAC Quantization
++ * N Frequency Selective Switch
++ * N upsampling filter
++ * Y quantization & coding - AAC
++ * N quantization & coding - TwinVQ
++ * N quantization & coding - BSAC
++ * N AAC Error Resilience tools
++ * N Error Resilience payload syntax
++ * N Error Protection tool
++ * N CELP
++ * N Silence Compression
++ * N HVXC
++ * N HVXC 4kbits/s VR
++ * N Structured Audio tools
++ * N Structured Audio Sample Bank Format
++ * N MIDI
++ * N Harmonic and Individual Lines plus Noise
++ * N Text-To-Speech Interface
++ * Y Spectral Band Replication
++ * Y (not in this code) Layer-1
++ * Y (not in this code) Layer-2
++ * Y (not in this code) Layer-3
++ * N SinuSoidal Coding (Transient, Sinusoid, Noise)
++ * Y Parametric Stereo
++ * N Direct Stream Transfer
++ *
++ * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
++ * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
++ Parametric Stereo.
++ */
++
++
++#include "avcodec.h"
++#include "internal.h"
++#include "get_bits.h"
++#include "dsputil.h"
++#include "fft.h"
++#include "lpc.h"
++
++#include "aac.h"
++#include "aactab.h"
++#include "aacdectab.h"
++#include "cbrt_tablegen.h"
++#include "sbr.h"
++#include "aacsbr.h"
++#include "mpeg4audio.h"
++#include "aac_parser.h"
++
++#include <assert.h>
++#include <errno.h>
++#include <math.h>
++#include <string.h>
++
++#if ARCH_ARM
++# include "arm/aac.h"
++#endif
++
++union float754 {
++ float f;
++ uint32_t i;
++};
++
++static VLC vlc_scalefactors;
++static VLC vlc_spectral[11];
++
++static const char overread_err[] = "Input buffer exhausted before END element found\n";
++
++static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
++{
++ /* Some buggy encoders appear to set all elem_ids to zero and rely on
++ channels always occurring in the same order. This is expressly forbidden
++ by the spec but we will try to work around it.
++ */
++ int err_printed = 0;
++ while (ac->tags_seen_this_frame[type][elem_id] && elem_id < MAX_ELEM_ID) {
++ if (ac->output_configured < OC_LOCKED && !err_printed) {
++ av_log(ac->avctx, AV_LOG_WARNING, "Duplicate channel tag found, attempting to remap.\n");
++ err_printed = 1;
++ }
++ elem_id++;
++ }
++ if (elem_id == MAX_ELEM_ID)
++ return NULL;
++ ac->tags_seen_this_frame[type][elem_id] = 1;
++
++ if (ac->tag_che_map[type][elem_id]) {
++ return ac->tag_che_map[type][elem_id];
++ }
++ if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
++ return NULL;
++ }
++ switch (ac->m4ac.chan_config) {
++ case 7:
++ if (ac->tags_mapped == 3 && type == TYPE_CPE) {
++ ac->tags_mapped++;
++ return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
++ }
++ case 6:
++ /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
++ instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
++ encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
++ if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
++ ac->tags_mapped++;
++ return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
++ }
++ case 5:
++ if (ac->tags_mapped == 2 && type == TYPE_CPE) {
++ ac->tags_mapped++;
++ return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
++ }
++ case 4:
++ if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
++ ac->tags_mapped++;
++ return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
++ }
++ case 3:
++ case 2:
++ if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
++ ac->tags_mapped++;
++ return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
++ } else if (ac->m4ac.chan_config == 2) {
++ return NULL;
++ }
++ case 1:
++ if (!ac->tags_mapped && type == TYPE_SCE) {
++ ac->tags_mapped++;
++ return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
++ }
++ default:
++ return NULL;
++ }
++}
++
++/**
++ * Check for the channel element in the current channel position configuration.
++ * If it exists, make sure the appropriate element is allocated and map the
++ * channel order to match the internal FFmpeg channel layout.
++ *
++ * @param che_pos current channel position configuration
++ * @param type channel element type
++ * @param id channel element id
++ * @param channels count of the number of channels in the configuration
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static av_cold int che_configure(AACContext *ac,
++ enum ChannelPosition che_pos[4][MAX_ELEM_ID],
++ int type, int id,
++ int *channels)
++{
++ if (che_pos[type][id]) {
++ if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
++ return AVERROR(ENOMEM);
++ 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 ||
++ (type == TYPE_SCE && ac->m4ac.ps == 1)) {
++ ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
++ }
++ }
++ } else {
++ if (ac->che[type][id])
++ ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
++ av_freep(&ac->che[type][id]);
++ }
++ return 0;
++}
++
++/**
++ * Configure output channel order based on the current program configuration element.
++ *
++ * @param che_pos current channel position configuration
++ * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static av_cold int output_configure(AACContext *ac,
++ enum ChannelPosition che_pos[4][MAX_ELEM_ID],
++ enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
++ int channel_config, enum OCStatus oc_type)
++{
++ 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) {
++ for (i = 0; i < tags_per_config[channel_config]; i++) {
++ if ((ret = che_configure(ac, che_pos,
++ aac_channel_layout_map[channel_config - 1][i][0],
++ aac_channel_layout_map[channel_config - 1][i][1],
++ &channels)))
++ return ret;
++ }
++
++ memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
++ ac->tags_mapped = 0;
++
++ avctx->channel_layout = aac_channel_layout[channel_config - 1];
++ } else {
++ /* Allocate or free elements depending on if they are in the
++ * current program configuration.
++ *
++ * Set up default 1:1 output mapping.
++ *
++ * For a 5.1 stream the output order will be:
++ * [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
++ */
++
++ for (i = 0; i < MAX_ELEM_ID; i++) {
++ for (type = 0; type < 4; type++) {
++ if ((ret = che_configure(ac, che_pos, type, i, &channels)))
++ return ret;
++ }
++ }
++
++ memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
++ ac->tags_mapped = 4 * MAX_ELEM_ID;
++
++ avctx->channel_layout = 0;
++ }
++
++ avctx->channels = channels;
++
++ ac->output_configured = oc_type;
++
++ return 0;
++}
++
++/**
++ * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
++ *
++ * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
++ * @param sce_map mono (Single Channel Element) map
++ * @param type speaker type/position for these channels
++ */
++static void decode_channel_map(enum ChannelPosition *cpe_map,
++ enum ChannelPosition *sce_map,
++ enum ChannelPosition type,
++ GetBitContext *gb, int n)
++{
++ while (n--) {
++ enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
++ map[get_bits(gb, 4)] = type;
++ }
++}
++
++/**
++ * Decode program configuration element; reference: table 4.2.
++ *
++ * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
++ GetBitContext *gb)
++{
++ int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
++ int comment_len;
++
++ skip_bits(gb, 2); // object_type
++
++ sampling_index = get_bits(gb, 4);
++ if (ac->m4ac.sampling_index != sampling_index)
++ av_log(ac->avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
++
++ num_front = get_bits(gb, 4);
++ num_side = get_bits(gb, 4);
++ num_back = get_bits(gb, 4);
++ num_lfe = get_bits(gb, 2);
++ num_assoc_data = get_bits(gb, 3);
++ num_cc = get_bits(gb, 4);
++
++ if (get_bits1(gb))
++ skip_bits(gb, 4); // mono_mixdown_tag
++ if (get_bits1(gb))
++ skip_bits(gb, 4); // stereo_mixdown_tag
++
++ if (get_bits1(gb))
++ skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
++
++ decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
++ decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
++ decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
++ decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
++
++ skip_bits_long(gb, 4 * num_assoc_data);
++
++ decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
++
++ align_get_bits(gb);
++
++ /* comment field, first byte is length */
++ comment_len = get_bits(gb, 8) * 8;
++ if (get_bits_left(gb) < comment_len) {
++ av_log(ac->avctx, AV_LOG_ERROR, overread_err);
++ return -1;
++ }
++ skip_bits_long(gb, comment_len);
++ return 0;
++}
++
++/**
++ * Set up channel positions based on a default channel configuration
++ * as specified in table 1.17.
++ *
++ * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static av_cold int set_default_channel_config(AACContext *ac,
++ enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
++ int channel_config)
++{
++ if (channel_config < 1 || channel_config > 7) {
++ av_log(ac->avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
++ channel_config);
++ return -1;
++ }
++
++ /* default channel configurations:
++ *
++ * 1ch : front center (mono)
++ * 2ch : L + R (stereo)
++ * 3ch : front center + L + R
++ * 4ch : front center + L + R + back center
++ * 5ch : front center + L + R + back stereo
++ * 6ch : front center + L + R + back stereo + LFE
++ * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
++ */
++
++ if (channel_config != 2)
++ new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
++ if (channel_config > 1)
++ new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
++ if (channel_config == 4)
++ new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center
++ if (channel_config > 4)
++ new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
++ = AAC_CHANNEL_BACK; // back stereo
++ if (channel_config > 5)
++ new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE
++ if (channel_config == 7)
++ new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
++
++ return 0;
++}
++
++/**
++ * Decode GA "General Audio" specific configuration; reference: table 4.1.
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
++ int channel_config)
++{
++ enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
++ int extension_flag, ret;
++
++ if (get_bits1(gb)) { // frameLengthFlag
++ av_log_missing_feature(ac->avctx, "960/120 MDCT window is", 1);
++ return -1;
++ }
++
++ if (get_bits1(gb)) // dependsOnCoreCoder
++ skip_bits(gb, 14); // coreCoderDelay
++ extension_flag = get_bits1(gb);
++
++ if (ac->m4ac.object_type == AOT_AAC_SCALABLE ||
++ ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
++ skip_bits(gb, 3); // layerNr
++
++ memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
++ if (channel_config == 0) {
++ skip_bits(gb, 4); // element_instance_tag
++ if ((ret = decode_pce(ac, new_che_pos, gb)))
++ return ret;
++ } else {
++ if ((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
++ return ret;
++ }
++ if ((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
++ return ret;
++
++ if (extension_flag) {
++ switch (ac->m4ac.object_type) {
++ case AOT_ER_BSAC:
++ skip_bits(gb, 5); // numOfSubFrame
++ skip_bits(gb, 11); // layer_length
++ break;
++ case AOT_ER_AAC_LC:
++ case AOT_ER_AAC_LTP:
++ case AOT_ER_AAC_SCALABLE:
++ case AOT_ER_AAC_LD:
++ skip_bits(gb, 3); /* aacSectionDataResilienceFlag
++ * aacScalefactorDataResilienceFlag
++ * aacSpectralDataResilienceFlag
++ */
++ break;
++ }
++ skip_bits1(gb); // extensionFlag3 (TBD in version 3)
++ }
++ return 0;
++}
++
++/**
++ * Decode audio specific configuration; reference: table 1.13.
++ *
++ * @param data pointer to AVCodecContext extradata
++ * @param data_size size of AVCCodecContext extradata
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_audio_specific_config(AACContext *ac, void *data,
++ int data_size)
++{
++ GetBitContext gb;
++ int i;
++
++ init_get_bits(&gb, data, data_size * 8);
++
++ if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
++ return -1;
++ if (ac->m4ac.sampling_index > 12) {
++ 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);
++
++ switch (ac->m4ac.object_type) {
++ case AOT_AAC_MAIN:
++ case AOT_AAC_LC:
++ if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
++ return -1;
++ break;
++ default:
++ av_log(ac->avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
++ ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
++ return -1;
++ }
++ return 0;
++}
++
++/**
++ * linear congruential pseudorandom number generator
++ *
++ * @param previous_val pointer to the current state of the generator
++ *
++ * @return Returns a 32-bit pseudorandom integer
++ */
++static av_always_inline int lcg_random(int previous_val)
++{
++ return previous_val * 1664525 + 1013904223;
++}
++
++static av_always_inline void reset_predict_state(PredictorState *ps)
++{
++ ps->r0 = 0.0f;
++ ps->r1 = 0.0f;
++ ps->cor0 = 0.0f;
++ ps->cor1 = 0.0f;
++ ps->var0 = 1.0f;
++ ps->var1 = 1.0f;
++}
++
++static void reset_all_predictors(PredictorState *ps)
++{
++ int i;
++ for (i = 0; i < MAX_PREDICTORS; i++)
++ reset_predict_state(&ps[i]);
++}
++
++static void reset_predictor_group(PredictorState *ps, int group_num)
++{
++ int i;
++ for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
++ reset_predict_state(&ps[i]);
++}
++
++#define AAC_INIT_VLC_STATIC(num, size) \
++ INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
++ ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
++ ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
++ size);
++
++static av_cold int aac_decode_init(AVCodecContext *avctx)
++{
++ AACContext *ac = avctx->priv_data;
++
++ ac->avctx = avctx;
++ ac->m4ac.sample_rate = avctx->sample_rate;
++
++ if (avctx->extradata_size > 0) {
++ if (decode_audio_specific_config(ac, avctx->extradata, avctx->extradata_size))
++ return -1;
++ }
++
++ avctx->sample_fmt = SAMPLE_FMT_S16;
++
++ AAC_INIT_VLC_STATIC( 0, 304);
++ AAC_INIT_VLC_STATIC( 1, 270);
++ AAC_INIT_VLC_STATIC( 2, 550);
++ AAC_INIT_VLC_STATIC( 3, 300);
++ AAC_INIT_VLC_STATIC( 4, 328);
++ AAC_INIT_VLC_STATIC( 5, 294);
++ AAC_INIT_VLC_STATIC( 6, 306);
++ AAC_INIT_VLC_STATIC( 7, 268);
++ AAC_INIT_VLC_STATIC( 8, 510);
++ AAC_INIT_VLC_STATIC( 9, 366);
++ AAC_INIT_VLC_STATIC(10, 462);
++
++ ff_aac_sbr_init();
++
++ dsputil_init(&ac->dsp, avctx);
++
++ ac->random_state = 0x1f2e3d4c;
++
++ // -1024 - Compensate wrong IMDCT method.
++ // 32768 - Required to scale values to the correct range for the bias method
++ // for float to int16 conversion.
++
++ if (ac->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
++ ac->add_bias = 385.0f;
++ ac->sf_scale = 1. / (-1024. * 32768.);
++ ac->sf_offset = 0;
++ } else {
++ ac->add_bias = 0.0f;
++ ac->sf_scale = 1. / -1024.;
++ ac->sf_offset = 60;
++ }
++
++ ff_aac_tableinit();
++
++ INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
++ ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
++ ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
++ 352);
++
++ ff_mdct_init(&ac->mdct, 11, 1, 1.0);
++ ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
++ // window initialization
++ ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
++ ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
++ ff_init_ff_sine_windows(10);
++ ff_init_ff_sine_windows( 7);
++
++ cbrt_tableinit();
++
++ return 0;
++}
++
++/**
++ * Skip data_stream_element; reference: table 4.10.
++ */
++static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
++{
++ int byte_align = get_bits1(gb);
++ int count = get_bits(gb, 8);
++ if (count == 255)
++ count += get_bits(gb, 8);
++ if (byte_align)
++ align_get_bits(gb);
++
++ if (get_bits_left(gb) < 8 * count) {
++ av_log(ac->avctx, AV_LOG_ERROR, overread_err);
++ return -1;
++ }
++ skip_bits_long(gb, 8 * count);
++ return 0;
++}
++
++static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
++ GetBitContext *gb)
++{
++ int sfb;
++ if (get_bits1(gb)) {
++ ics->predictor_reset_group = get_bits(gb, 5);
++ if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
++ av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
++ return -1;
++ }
++ }
++ for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
++ ics->prediction_used[sfb] = get_bits1(gb);
++ }
++ return 0;
++}
++
++/**
++ * Decode Individual Channel Stream info; reference: table 4.6.
++ *
++ * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
++ */
++static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
++ GetBitContext *gb, int common_window)
++{
++ if (get_bits1(gb)) {
++ av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
++ memset(ics, 0, sizeof(IndividualChannelStream));
++ return -1;
++ }
++ ics->window_sequence[1] = ics->window_sequence[0];
++ ics->window_sequence[0] = get_bits(gb, 2);
++ ics->use_kb_window[1] = ics->use_kb_window[0];
++ ics->use_kb_window[0] = get_bits1(gb);
++ ics->num_window_groups = 1;
++ ics->group_len[0] = 1;
++ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
++ int i;
++ ics->max_sfb = get_bits(gb, 4);
++ for (i = 0; i < 7; i++) {
++ if (get_bits1(gb)) {
++ ics->group_len[ics->num_window_groups - 1]++;
++ } else {
++ ics->num_window_groups++;
++ ics->group_len[ics->num_window_groups - 1] = 1;
++ }
++ }
++ ics->num_windows = 8;
++ ics->swb_offset = ff_swb_offset_128[ac->m4ac.sampling_index];
++ ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
++ ics->tns_max_bands = ff_tns_max_bands_128[ac->m4ac.sampling_index];
++ ics->predictor_present = 0;
++ } else {
++ ics->max_sfb = get_bits(gb, 6);
++ ics->num_windows = 1;
++ ics->swb_offset = ff_swb_offset_1024[ac->m4ac.sampling_index];
++ ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
++ ics->tns_max_bands = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
++ ics->predictor_present = get_bits1(gb);
++ ics->predictor_reset_group = 0;
++ if (ics->predictor_present) {
++ if (ac->m4ac.object_type == AOT_AAC_MAIN) {
++ if (decode_prediction(ac, ics, gb)) {
++ memset(ics, 0, sizeof(IndividualChannelStream));
++ return -1;
++ }
++ } else if (ac->m4ac.object_type == AOT_AAC_LC) {
++ av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
++ memset(ics, 0, sizeof(IndividualChannelStream));
++ return -1;
++ } else {
++ av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1);
++ memset(ics, 0, sizeof(IndividualChannelStream));
++ return -1;
++ }
++ }
++ }
++
++ if (ics->max_sfb > ics->num_swb) {
++ av_log(ac->avctx, AV_LOG_ERROR,
++ "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
++ ics->max_sfb, ics->num_swb);
++ memset(ics, 0, sizeof(IndividualChannelStream));
++ return -1;
++ }
++
++ return 0;
++}
++
++/**
++ * Decode band types (section_data payload); reference: table 4.46.
++ *
++ * @param band_type array of the used band type
++ * @param band_type_run_end array of the last scalefactor band of a band type run
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_band_types(AACContext *ac, enum BandType band_type[120],
++ int band_type_run_end[120], GetBitContext *gb,
++ IndividualChannelStream *ics)
++{
++ int g, idx = 0;
++ const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
++ for (g = 0; g < ics->num_window_groups; g++) {
++ int k = 0;
++ while (k < ics->max_sfb) {
++ uint8_t sect_end = k;
++ int sect_len_incr;
++ int sect_band_type = get_bits(gb, 4);
++ if (sect_band_type == 12) {
++ av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
++ return -1;
++ }
++ while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
++ sect_end += sect_len_incr;
++ sect_end += sect_len_incr;
++ if (get_bits_left(gb) < 0) {
++ av_log(ac->avctx, AV_LOG_ERROR, overread_err);
++ return -1;
++ }
++ if (sect_end > ics->max_sfb) {
++ av_log(ac->avctx, AV_LOG_ERROR,
++ "Number of bands (%d) exceeds limit (%d).\n",
++ sect_end, ics->max_sfb);
++ return -1;
++ }
++ for (; k < sect_end; k++) {
++ band_type [idx] = sect_band_type;
++ band_type_run_end[idx++] = sect_end;
++ }
++ }
++ }
++ return 0;
++}
++
++/**
++ * Decode scalefactors; reference: table 4.47.
++ *
++ * @param global_gain first scalefactor value as scalefactors are differentially coded
++ * @param band_type array of the used band type
++ * @param band_type_run_end array of the last scalefactor band of a band type run
++ * @param sf array of scalefactors or intensity stereo positions
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
++ unsigned int global_gain,
++ IndividualChannelStream *ics,
++ enum BandType band_type[120],
++ int band_type_run_end[120])
++{
++ const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
++ int g, i, idx = 0;
++ int offset[3] = { global_gain, global_gain - 90, 100 };
++ int noise_flag = 1;
++ static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
++ for (g = 0; g < ics->num_window_groups; g++) {
++ for (i = 0; i < ics->max_sfb;) {
++ int run_end = band_type_run_end[idx];
++ if (band_type[idx] == ZERO_BT) {
++ for (; i < run_end; i++, idx++)
++ sf[idx] = 0.;
++ } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
++ for (; i < run_end; i++, idx++) {
++ offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
++ if (offset[2] > 255U) {
++ av_log(ac->avctx, AV_LOG_ERROR,
++ "%s (%d) out of range.\n", sf_str[2], offset[2]);
++ return -1;
++ }
++ sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
++ }
++ } else if (band_type[idx] == NOISE_BT) {
++ for (; i < run_end; i++, idx++) {
++ if (noise_flag-- > 0)
++ offset[1] += get_bits(gb, 9) - 256;
++ else
++ offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
++ if (offset[1] > 255U) {
++ av_log(ac->avctx, AV_LOG_ERROR,
++ "%s (%d) out of range.\n", sf_str[1], offset[1]);
++ return -1;
++ }
++ sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
++ }
++ } else {
++ for (; i < run_end; i++, idx++) {
++ offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
++ if (offset[0] > 255U) {
++ av_log(ac->avctx, AV_LOG_ERROR,
++ "%s (%d) out of range.\n", sf_str[0], offset[0]);
++ return -1;
++ }
++ sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
++ }
++ }
++ }
++ }
++ return 0;
++}
++
++/**
++ * Decode pulse data; reference: table 4.7.
++ */
++static int decode_pulses(Pulse *pulse, GetBitContext *gb,
++ const uint16_t *swb_offset, int num_swb)
++{
++ int i, pulse_swb;
++ pulse->num_pulse = get_bits(gb, 2) + 1;
++ pulse_swb = get_bits(gb, 6);
++ if (pulse_swb >= num_swb)
++ return -1;
++ pulse->pos[0] = swb_offset[pulse_swb];
++ pulse->pos[0] += get_bits(gb, 5);
++ if (pulse->pos[0] > 1023)
++ return -1;
++ pulse->amp[0] = get_bits(gb, 4);
++ for (i = 1; i < pulse->num_pulse; i++) {
++ pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
++ if (pulse->pos[i] > 1023)
++ return -1;
++ pulse->amp[i] = get_bits(gb, 4);
++ }
++ return 0;
++}
++
++/**
++ * Decode Temporal Noise Shaping data; reference: table 4.48.
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
++ GetBitContext *gb, const IndividualChannelStream *ics)
++{
++ int w, filt, i, coef_len, coef_res, coef_compress;
++ const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
++ const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
++ for (w = 0; w < ics->num_windows; w++) {
++ if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
++ coef_res = get_bits1(gb);
++
++ for (filt = 0; filt < tns->n_filt[w]; filt++) {
++ int tmp2_idx;
++ tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
++
++ if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
++ av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
++ tns->order[w][filt], tns_max_order);
++ tns->order[w][filt] = 0;
++ return -1;
++ }
++ if (tns->order[w][filt]) {
++ tns->direction[w][filt] = get_bits1(gb);
++ coef_compress = get_bits1(gb);
++ coef_len = coef_res + 3 - coef_compress;
++ tmp2_idx = 2 * coef_compress + coef_res;
++
++ for (i = 0; i < tns->order[w][filt]; i++)
++ tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
++ }
++ }
++ }
++ }
++ return 0;
++}
++
++/**
++ * Decode Mid/Side data; reference: table 4.54.
++ *
++ * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
++ * [1] mask is decoded from bitstream; [2] mask is all 1s;
++ * [3] reserved for scalable AAC
++ */
++static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
++ int ms_present)
++{
++ int idx;
++ if (ms_present == 1) {
++ for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
++ cpe->ms_mask[idx] = get_bits1(gb);
++ } else if (ms_present == 2) {
++ memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
++ }
++}
++
++#ifndef VMUL2
++static inline float *VMUL2(float *dst, const float *v, unsigned idx,
++ const float *scale)
++{
++ float s = *scale;
++ *dst++ = v[idx & 15] * s;
++ *dst++ = v[idx>>4 & 15] * s;
++ return dst;
++}
++#endif
++
++#ifndef VMUL4
++static inline float *VMUL4(float *dst, const float *v, unsigned idx,
++ const float *scale)
++{
++ float s = *scale;
++ *dst++ = v[idx & 3] * s;
++ *dst++ = v[idx>>2 & 3] * s;
++ *dst++ = v[idx>>4 & 3] * s;
++ *dst++ = v[idx>>6 & 3] * s;
++ return dst;
++}
++#endif
++
++#ifndef VMUL2S
++static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
++ unsigned sign, const float *scale)
++{
++ union float754 s0, s1;
++
++ s0.f = s1.f = *scale;
++ s0.i ^= sign >> 1 << 31;
++ s1.i ^= sign << 31;
++
++ *dst++ = v[idx & 15] * s0.f;
++ *dst++ = v[idx>>4 & 15] * s1.f;
++
++ return dst;
++}
++#endif
++
++#ifndef VMUL4S
++static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
++ unsigned sign, const float *scale)
++{
++ unsigned nz = idx >> 12;
++ union float754 s = { .f = *scale };
++ union float754 t;
++
++ t.i = s.i ^ (sign & 1<<31);
++ *dst++ = v[idx & 3] * t.f;
++
++ sign <<= nz & 1; nz >>= 1;
++ t.i = s.i ^ (sign & 1<<31);
++ *dst++ = v[idx>>2 & 3] * t.f;
++
++ sign <<= nz & 1; nz >>= 1;
++ t.i = s.i ^ (sign & 1<<31);
++ *dst++ = v[idx>>4 & 3] * t.f;
++
++ sign <<= nz & 1; nz >>= 1;
++ t.i = s.i ^ (sign & 1<<31);
++ *dst++ = v[idx>>6 & 3] * t.f;
++
++ return dst;
++}
++#endif
++
++/**
++ * Decode spectral data; reference: table 4.50.
++ * Dequantize and scale spectral data; reference: 4.6.3.3.
++ *
++ * @param coef array of dequantized, scaled spectral data
++ * @param sf array of scalefactors or intensity stereo positions
++ * @param pulse_present set if pulses are present
++ * @param pulse pointer to pulse data struct
++ * @param band_type array of the used band type
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
++ GetBitContext *gb, const float sf[120],
++ int pulse_present, const Pulse *pulse,
++ const IndividualChannelStream *ics,
++ enum BandType band_type[120])
++{
++ int i, k, g, idx = 0;
++ const int c = 1024 / ics->num_windows;
++ const uint16_t *offsets = ics->swb_offset;
++ float *coef_base = coef;
++ int err_idx;
++
++ for (g = 0; g < ics->num_windows; g++)
++ memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
++
++ for (g = 0; g < ics->num_window_groups; g++) {
++ unsigned g_len = ics->group_len[g];
++
++ for (i = 0; i < ics->max_sfb; i++, idx++) {
++ const unsigned cbt_m1 = band_type[idx] - 1;
++ float *cfo = coef + offsets[i];
++ int off_len = offsets[i + 1] - offsets[i];
++ int group;
++
++ if (cbt_m1 >= INTENSITY_BT2 - 1) {
++ for (group = 0; group < g_len; group++, cfo+=128) {
++ memset(cfo, 0, off_len * sizeof(float));
++ }
++ } else if (cbt_m1 == NOISE_BT - 1) {
++ for (group = 0; group < g_len; group++, cfo+=128) {
++ float scale;
++ float band_energy;
++
++ for (k = 0; k < off_len; k++) {
++ ac->random_state = lcg_random(ac->random_state);
++ cfo[k] = ac->random_state;
++ }
++
++ band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
++ scale = sf[idx] / sqrtf(band_energy);
++ ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
++ }
++ } else {
++ const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
++ const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
++ VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
++ const int cb_size = ff_aac_spectral_sizes[cbt_m1];
++ OPEN_READER(re, gb);
++
++ switch (cbt_m1 >> 1) {
++ case 0:
++ for (group = 0; group < g_len; group++, cfo+=128) {
++ float *cf = cfo;
++ int len = off_len;
++
++ do {
++ int code;
++ unsigned cb_idx;
++
++ UPDATE_CACHE(re, gb);
++ GET_VLC(code, re, gb, vlc_tab, 8, 2);
++
++ if (code >= cb_size) {
++ err_idx = code;
++ goto err_cb_overflow;
++ }
++
++ cb_idx = cb_vector_idx[code];
++ cf = VMUL4(cf, vq, cb_idx, sf + idx);
++ } while (len -= 4);
++ }
++ break;
++
++ case 1:
++ for (group = 0; group < g_len; group++, cfo+=128) {
++ float *cf = cfo;
++ int len = off_len;
++
++ do {
++ int code;
++ unsigned nnz;
++ unsigned cb_idx;
++ uint32_t bits;
++
++ UPDATE_CACHE(re, gb);
++ GET_VLC(code, re, gb, vlc_tab, 8, 2);
++
++ if (code >= cb_size) {
++ err_idx = code;
++ goto err_cb_overflow;
++ }
++
++#if MIN_CACHE_BITS < 20
++ UPDATE_CACHE(re, gb);
++#endif
++ cb_idx = cb_vector_idx[code];
++ nnz = cb_idx >> 8 & 15;
++ bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
++ LAST_SKIP_BITS(re, gb, nnz);
++ cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
++ } while (len -= 4);
++ }
++ break;
++
++ case 2:
++ for (group = 0; group < g_len; group++, cfo+=128) {
++ float *cf = cfo;
++ int len = off_len;
++
++ do {
++ int code;
++ unsigned cb_idx;
++
++ UPDATE_CACHE(re, gb);
++ GET_VLC(code, re, gb, vlc_tab, 8, 2);
++
++ if (code >= cb_size) {
++ err_idx = code;
++ goto err_cb_overflow;
++ }
++
++ cb_idx = cb_vector_idx[code];
++ cf = VMUL2(cf, vq, cb_idx, sf + idx);
++ } while (len -= 2);
++ }
++ break;
++
++ case 3:
++ case 4:
++ for (group = 0; group < g_len; group++, cfo+=128) {
++ float *cf = cfo;
++ int len = off_len;
++
++ do {
++ int code;
++ unsigned nnz;
++ unsigned cb_idx;
++ unsigned sign;
++
++ UPDATE_CACHE(re, gb);
++ GET_VLC(code, re, gb, vlc_tab, 8, 2);
++
++ if (code >= cb_size) {
++ err_idx = code;
++ goto err_cb_overflow;
++ }
++
++ cb_idx = cb_vector_idx[code];
++ nnz = cb_idx >> 8 & 15;
++ sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
++ LAST_SKIP_BITS(re, gb, nnz);
++ cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
++ } while (len -= 2);
++ }
++ break;
++
++ default:
++ for (group = 0; group < g_len; group++, cfo+=128) {
++ float *cf = cfo;
++ uint32_t *icf = (uint32_t *) cf;
++ int len = off_len;
++
++ do {
++ int code;
++ unsigned nzt, nnz;
++ unsigned cb_idx;
++ uint32_t bits;
++ int j;
++
++ UPDATE_CACHE(re, gb);
++ GET_VLC(code, re, gb, vlc_tab, 8, 2);
++
++ if (!code) {
++ *icf++ = 0;
++ *icf++ = 0;
++ continue;
++ }
++
++ if (code >= cb_size) {
++ err_idx = code;
++ goto err_cb_overflow;
++ }
++
++ cb_idx = cb_vector_idx[code];
++ nnz = cb_idx >> 12;
++ nzt = cb_idx >> 8;
++ bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
++ LAST_SKIP_BITS(re, gb, nnz);
++
++ for (j = 0; j < 2; j++) {
++ if (nzt & 1<<j) {
++ uint32_t b;
++ int n;
++ /* The total length of escape_sequence must be < 22 bits according
++ to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
++ UPDATE_CACHE(re, gb);
++ b = GET_CACHE(re, gb);
++ b = 31 - av_log2(~b);
++
++ if (b > 8) {
++ av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
++ return -1;
++ }
++
++#if MIN_CACHE_BITS < 21
++ LAST_SKIP_BITS(re, gb, b + 1);
++ UPDATE_CACHE(re, gb);
++#else
++ SKIP_BITS(re, gb, b + 1);
++#endif
++ b += 4;
++ n = (1 << b) + SHOW_UBITS(re, gb, b);
++ LAST_SKIP_BITS(re, gb, b);
++ *icf++ = cbrt_tab[n] | (bits & 1<<31);
++ bits <<= 1;
++ } else {
++ unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
++ *icf++ = (bits & 1<<31) | v;
++ bits <<= !!v;
++ }
++ cb_idx >>= 4;
++ }
++ } while (len -= 2);
++
++ ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
++ }
++ }
++
++ CLOSE_READER(re, gb);
++ }
++ }
++ coef += g_len << 7;
++ }
++
++ if (pulse_present) {
++ idx = 0;
++ for (i = 0; i < pulse->num_pulse; i++) {
++ float co = coef_base[ pulse->pos[i] ];
++ while (offsets[idx + 1] <= pulse->pos[i])
++ idx++;
++ if (band_type[idx] != NOISE_BT && sf[idx]) {
++ float ico = -pulse->amp[i];
++ if (co) {
++ co /= sf[idx];
++ ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
++ }
++ coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
++ }
++ }
++ }
++ return 0;
++
++err_cb_overflow:
++ av_log(ac->avctx, AV_LOG_ERROR,
++ "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n",
++ band_type[idx], err_idx, ff_aac_spectral_sizes[band_type[idx]]);
++ return -1;
++}
++
++static av_always_inline float flt16_round(float pf)
++{
++ union float754 tmp;
++ tmp.f = pf;
++ tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
++ return tmp.f;
++}
++
++static av_always_inline float flt16_even(float pf)
++{
++ union float754 tmp;
++ tmp.f = pf;
++ tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
++ return tmp.f;
++}
++
++static av_always_inline float flt16_trunc(float pf)
++{
++ union float754 pun;
++ pun.f = pf;
++ pun.i &= 0xFFFF0000U;
++ return pun.f;
++}
++
++static av_always_inline void predict(AACContext *ac, PredictorState *ps, float *coef,
++ int output_enable)
++{
++ const float a = 0.953125; // 61.0 / 64
++ const float alpha = 0.90625; // 29.0 / 32
++ float e0, e1;
++ float pv;
++ float k1, k2;
++
++ k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
++ k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
++
++ pv = flt16_round(k1 * ps->r0 + k2 * ps->r1);
++ if (output_enable)
++ *coef += pv * ac->sf_scale;
++
++ e0 = *coef / ac->sf_scale;
++ e1 = e0 - k1 * ps->r0;
++
++ ps->cor1 = flt16_trunc(alpha * ps->cor1 + ps->r1 * e1);
++ ps->var1 = flt16_trunc(alpha * ps->var1 + 0.5 * (ps->r1 * ps->r1 + e1 * e1));
++ ps->cor0 = flt16_trunc(alpha * ps->cor0 + ps->r0 * e0);
++ ps->var0 = flt16_trunc(alpha * ps->var0 + 0.5 * (ps->r0 * ps->r0 + e0 * e0));
++
++ ps->r1 = flt16_trunc(a * (ps->r0 - k1 * e0));
++ ps->r0 = flt16_trunc(a * e0);
++}
++
++/**
++ * Apply AAC-Main style frequency domain prediction.
++ */
++static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
++{
++ int sfb, k;
++
++ if (!sce->ics.predictor_initialized) {
++ reset_all_predictors(sce->predictor_state);
++ sce->ics.predictor_initialized = 1;
++ }
++
++ if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
++ for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
++ for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
++ predict(ac, &sce->predictor_state[k], &sce->coeffs[k],
++ sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
++ }
++ }
++ if (sce->ics.predictor_reset_group)
++ reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
++ } else
++ reset_all_predictors(sce->predictor_state);
++}
++
++/**
++ * Decode an individual_channel_stream payload; reference: table 4.44.
++ *
++ * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
++ * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_ics(AACContext *ac, SingleChannelElement *sce,
++ GetBitContext *gb, int common_window, int scale_flag)
++{
++ Pulse pulse;
++ TemporalNoiseShaping *tns = &sce->tns;
++ IndividualChannelStream *ics = &sce->ics;
++ float *out = sce->coeffs;
++ int global_gain, pulse_present = 0;
++
++ /* This assignment is to silence a GCC warning about the variable being used
++ * uninitialized when in fact it always is.
++ */
++ pulse.num_pulse = 0;
++
++ global_gain = get_bits(gb, 8);
++
++ if (!common_window && !scale_flag) {
++ if (decode_ics_info(ac, ics, gb, 0) < 0)
++ return -1;
++ }
++
++ if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
++ return -1;
++ if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
++ return -1;
++
++ pulse_present = 0;
++ if (!scale_flag) {
++ if ((pulse_present = get_bits1(gb))) {
++ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
++ av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
++ return -1;
++ }
++ if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
++ av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
++ return -1;
++ }
++ }
++ if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
++ return -1;
++ if (get_bits1(gb)) {
++ av_log_missing_feature(ac->avctx, "SSR", 1);
++ return -1;
++ }
++ }
++
++ if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
++ return -1;
++
++ if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
++ apply_prediction(ac, sce);
++
++ return 0;
++}
++
++/**
++ * Mid/Side stereo decoding; reference: 4.6.8.1.3.
++ */
++static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
++{
++ const IndividualChannelStream *ics = &cpe->ch[0].ics;
++ float *ch0 = cpe->ch[0].coeffs;
++ float *ch1 = cpe->ch[1].coeffs;
++ int g, i, group, idx = 0;
++ const uint16_t *offsets = ics->swb_offset;
++ for (g = 0; g < ics->num_window_groups; g++) {
++ for (i = 0; i < ics->max_sfb; i++, idx++) {
++ if (cpe->ms_mask[idx] &&
++ cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
++ for (group = 0; group < ics->group_len[g]; group++) {
++ ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
++ ch1 + group * 128 + offsets[i],
++ offsets[i+1] - offsets[i]);
++ }
++ }
++ }
++ ch0 += ics->group_len[g] * 128;
++ ch1 += ics->group_len[g] * 128;
++ }
++}
++
++/**
++ * intensity stereo decoding; reference: 4.6.8.2.3
++ *
++ * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
++ * [1] mask is decoded from bitstream; [2] mask is all 1s;
++ * [3] reserved for scalable AAC
++ */
++static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
++{
++ const IndividualChannelStream *ics = &cpe->ch[1].ics;
++ SingleChannelElement *sce1 = &cpe->ch[1];
++ float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
++ const uint16_t *offsets = ics->swb_offset;
++ int g, group, i, k, idx = 0;
++ int c;
++ float scale;
++ for (g = 0; g < ics->num_window_groups; g++) {
++ for (i = 0; i < ics->max_sfb;) {
++ if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
++ const int bt_run_end = sce1->band_type_run_end[idx];
++ for (; i < bt_run_end; i++, idx++) {
++ c = -1 + 2 * (sce1->band_type[idx] - 14);
++ if (ms_present)
++ c *= 1 - 2 * cpe->ms_mask[idx];
++ scale = c * sce1->sf[idx];
++ for (group = 0; group < ics->group_len[g]; group++)
++ for (k = offsets[i]; k < offsets[i + 1]; k++)
++ coef1[group * 128 + k] = scale * coef0[group * 128 + k];
++ }
++ } else {
++ int bt_run_end = sce1->band_type_run_end[idx];
++ idx += bt_run_end - i;
++ i = bt_run_end;
++ }
++ }
++ coef0 += ics->group_len[g] * 128;
++ coef1 += ics->group_len[g] * 128;
++ }
++}
++
++/**
++ * Decode a channel_pair_element; reference: table 4.4.
++ *
++ * @param elem_id Identifies the instance of a syntax element.
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
++{
++ int i, ret, common_window, ms_present = 0;
++
++ common_window = get_bits1(gb);
++ if (common_window) {
++ if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
++ return -1;
++ i = cpe->ch[1].ics.use_kb_window[0];
++ cpe->ch[1].ics = cpe->ch[0].ics;
++ cpe->ch[1].ics.use_kb_window[1] = i;
++ ms_present = get_bits(gb, 2);
++ if (ms_present == 3) {
++ av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
++ return -1;
++ } else if (ms_present)
++ decode_mid_side_stereo(cpe, gb, ms_present);
++ }
++ if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
++ return ret;
++ if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
++ return ret;
++
++ if (common_window) {
++ if (ms_present)
++ apply_mid_side_stereo(ac, cpe);
++ if (ac->m4ac.object_type == AOT_AAC_MAIN) {
++ apply_prediction(ac, &cpe->ch[0]);
++ apply_prediction(ac, &cpe->ch[1]);
++ }
++ }
++
++ apply_intensity_stereo(cpe, ms_present);
++ return 0;
++}
++
++/**
++ * Decode coupling_channel_element; reference: table 4.8.
++ *
++ * @param elem_id Identifies the instance of a syntax element.
++ *
++ * @return Returns error status. 0 - OK, !0 - error
++ */
++static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
++{
++ int num_gain = 0;
++ int c, g, sfb, ret;
++ int sign;
++ float scale;
++ SingleChannelElement *sce = &che->ch[0];
++ ChannelCoupling *coup = &che->coup;
++
++ coup->coupling_point = 2 * get_bits1(gb);
++ coup->num_coupled = get_bits(gb, 3);
++ for (c = 0; c <= coup->num_coupled; c++) {
++ num_gain++;
++ coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
++ coup->id_select[c] = get_bits(gb, 4);
++ if (coup->type[c] == TYPE_CPE) {
++ coup->ch_select[c] = get_bits(gb, 2);
++ if (coup->ch_select[c] == 3)
++ num_gain++;
++ } else
++ coup->ch_select[c] = 2;
++ }
++ coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
++
++ sign = get_bits(gb, 1);
++ scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));
++
++ if ((ret = decode_ics(ac, sce, gb, 0, 0)))
++ return ret;
++
++ for (c = 0; c < num_gain; c++) {
++ int idx = 0;
++ int cge = 1;
++ int gain = 0;
++ float gain_cache = 1.;
++ if (c) {
++ cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
++ gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
++ gain_cache = pow(scale, -gain);
++ }
++ if (coup->coupling_point == AFTER_IMDCT) {
++ coup->gain[c][0] = gain_cache;
++ } else {
++ for (g = 0; g < sce->ics.num_window_groups; g++) {
++ for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
++ if (sce->band_type[idx] != ZERO_BT) {
++ if (!cge) {
++ int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
++ if (t) {
++ int s = 1;
++ t = gain += t;
++ if (sign) {
++ s -= 2 * (t & 0x1);
++ t >>= 1;
++ }
++ gain_cache = pow(scale, -t) * s;
++ }
++ }
++ coup->gain[c][idx] = gain_cache;
++ }
++ }
++ }
++ }
++ }
++ return 0;
++}
++
++/**
++ * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
++ *
++ * @return Returns number of bytes consumed.
++ */
++static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
++ GetBitContext *gb)
++{
++ int i;
++ int num_excl_chan = 0;
++
++ do {
++ for (i = 0; i < 7; i++)
++ che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
++ } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
++
++ return num_excl_chan / 7;
++}
++
++/**
++ * Decode dynamic range information; reference: table 4.52.
++ *
++ * @param cnt length of TYPE_FIL syntactic element in bytes
++ *
++ * @return Returns number of bytes consumed.
++ */
++static int decode_dynamic_range(DynamicRangeControl *che_drc,
++ GetBitContext *gb, int cnt)
++{
++ int n = 1;
++ int drc_num_bands = 1;
++ int i;
++
++ /* pce_tag_present? */
++ if (get_bits1(gb)) {
++ che_drc->pce_instance_tag = get_bits(gb, 4);
++ skip_bits(gb, 4); // tag_reserved_bits
++ n++;
++ }
++
++ /* excluded_chns_present? */
++ if (get_bits1(gb)) {
++ n += decode_drc_channel_exclusions(che_drc, gb);
++ }
++
++ /* drc_bands_present? */
++ if (get_bits1(gb)) {
++ che_drc->band_incr = get_bits(gb, 4);
++ che_drc->interpolation_scheme = get_bits(gb, 4);
++ n++;
++ drc_num_bands += che_drc->band_incr;
++ for (i = 0; i < drc_num_bands; i++) {
++ che_drc->band_top[i] = get_bits(gb, 8);
++ n++;
++ }
++ }
++
++ /* prog_ref_level_present? */
++ if (get_bits1(gb)) {
++ che_drc->prog_ref_level = get_bits(gb, 7);
++ skip_bits1(gb); // prog_ref_level_reserved_bits
++ n++;
++ }
++
++ for (i = 0; i < drc_num_bands; i++) {
++ che_drc->dyn_rng_sgn[i] = get_bits1(gb);
++ che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
++ n++;
++ }
++
++ return n;
++}
++
++/**
++ * Decode extension data (incomplete); reference: table 4.51.
++ *
++ * @param cnt length of TYPE_FIL syntactic element in bytes
++ *
++ * @return Returns number of bytes consumed
++ */
++static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
++ ChannelElement *che, enum RawDataBlockType elem_type)
++{
++ int crc_flag = 0;
++ int res = cnt;
++ switch (get_bits(gb, 4)) { // extension type
++ case EXT_SBR_DATA_CRC:
++ crc_flag++;
++ case EXT_SBR_DATA:
++ if (!che) {
++ av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
++ return res;
++ } else if (!ac->m4ac.sbr) {
++ av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
++ skip_bits_long(gb, 8 * cnt - 4);
++ return res;
++ } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
++ 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;
++ }
++ res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
++ break;
++ case EXT_DYNAMIC_RANGE:
++ res = decode_dynamic_range(&ac->che_drc, gb, cnt);
++ break;
++ case EXT_FILL:
++ case EXT_FILL_DATA:
++ case EXT_DATA_ELEMENT:
++ default:
++ skip_bits_long(gb, 8 * cnt - 4);
++ break;
++ };
++ return res;
++}
++
++/**
++ * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
++ *
++ * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
++ * @param coef spectral coefficients
++ */
++static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
++ IndividualChannelStream *ics, int decode)
++{
++ const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
++ int w, filt, m, i;
++ int bottom, top, order, start, end, size, inc;
++ float lpc[TNS_MAX_ORDER];
++
++ for (w = 0; w < ics->num_windows; w++) {
++ bottom = ics->num_swb;
++ for (filt = 0; filt < tns->n_filt[w]; filt++) {
++ top = bottom;
++ bottom = FFMAX(0, top - tns->length[w][filt]);
++ order = tns->order[w][filt];
++ if (order == 0)
++ continue;
++
++ // tns_decode_coef
++ compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
++
++ start = ics->swb_offset[FFMIN(bottom, mmm)];
++ end = ics->swb_offset[FFMIN( top, mmm)];
++ if ((size = end - start) <= 0)
++ continue;
++ if (tns->direction[w][filt]) {
++ inc = -1;
++ start = end - 1;
++ } else {
++ inc = 1;
++ }
++ start += w * 128;
++
++ // ar filter
++ for (m = 0; m < size; m++, start += inc)
++ for (i = 1; i <= FFMIN(m, order); i++)
++ coef[start] -= coef[start - i * inc] * lpc[i - 1];
++ }
++ }
++}
++
++/**
++ * Conduct IMDCT and windowing.
++ */
++static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
++{
++ IndividualChannelStream *ics = &sce->ics;
++ float *in = sce->coeffs;
++ float *out = sce->ret;
++ float *saved = sce->saved;
++ const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
++ const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
++ const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
++ float *buf = ac->buf_mdct;
++ float *temp = ac->temp;
++ int i;
++
++ // imdct
++ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
++ if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
++ av_log(ac->avctx, AV_LOG_WARNING,
++ "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
++ "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
++ for (i = 0; i < 1024; i += 128)
++ ff_imdct_half(&ac->mdct_small, buf + i, in + i);
++ } else
++ ff_imdct_half(&ac->mdct, buf, in);
++
++ /* window overlapping
++ * NOTE: To simplify the overlapping code, all 'meaningless' short to long
++ * and long to short transitions are considered to be short to short
++ * transitions. This leaves just two cases (long to long and short to short)
++ * with a little special sauce for EIGHT_SHORT_SEQUENCE.
++ */
++ if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
++ (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
++ ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, bias, 512);
++ } else {
++ for (i = 0; i < 448; i++)
++ out[i] = saved[i] + bias;
++
++ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
++ ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, bias, 64);
++ ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, bias, 64);
++ ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, bias, 64);
++ ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, bias, 64);
++ ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, bias, 64);
++ memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
++ } else {
++ ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, bias, 64);
++ for (i = 576; i < 1024; i++)
++ out[i] = buf[i-512] + bias;
++ }
++ }
++
++ // buffer update
++ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
++ for (i = 0; i < 64; i++)
++ saved[i] = temp[64 + i] - bias;
++ ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
++ ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
++ ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
++ memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
++ } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
++ memcpy( saved, buf + 512, 448 * sizeof(float));
++ memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
++ } else { // LONG_STOP or ONLY_LONG
++ memcpy( saved, buf + 512, 512 * sizeof(float));
++ }
++}
++
++/**
++ * Apply dependent channel coupling (applied before IMDCT).
++ *
++ * @param index index into coupling gain array
++ */
++static void apply_dependent_coupling(AACContext *ac,
++ SingleChannelElement *target,
++ ChannelElement *cce, int index)
++{
++ IndividualChannelStream *ics = &cce->ch[0].ics;
++ const uint16_t *offsets = ics->swb_offset;
++ float *dest = target->coeffs;
++ const float *src = cce->ch[0].coeffs;
++ int g, i, group, k, idx = 0;
++ if (ac->m4ac.object_type == AOT_AAC_LTP) {
++ av_log(ac->avctx, AV_LOG_ERROR,
++ "Dependent coupling is not supported together with LTP\n");
++ return;
++ }
++ for (g = 0; g < ics->num_window_groups; g++) {
++ for (i = 0; i < ics->max_sfb; i++, idx++) {
++ if (cce->ch[0].band_type[idx] != ZERO_BT) {
++ const float gain = cce->coup.gain[index][idx];
++ for (group = 0; group < ics->group_len[g]; group++) {
++ for (k = offsets[i]; k < offsets[i + 1]; k++) {
++ // XXX dsputil-ize
++ dest[group * 128 + k] += gain * src[group * 128 + k];
++ }
++ }
++ }
++ }
++ dest += ics->group_len[g] * 128;
++ src += ics->group_len[g] * 128;
++ }
++}
++
++/**
++ * Apply independent channel coupling (applied after IMDCT).
++ *
++ * @param index index into coupling gain array
++ */
++static void apply_independent_coupling(AACContext *ac,
++ SingleChannelElement *target,
++ ChannelElement *cce, int index)
++{
++ int i;
++ const float gain = cce->coup.gain[index][0];
++ const float bias = ac->add_bias;
++ const float *src = cce->ch[0].ret;
++ float *dest = target->ret;
++ const int len = 1024 << (ac->m4ac.sbr == 1);
++
++ for (i = 0; i < len; i++)
++ dest[i] += gain * (src[i] - bias);
++}
++
++/**
++ * channel coupling transformation interface
++ *
++ * @param index index into coupling gain array
++ * @param apply_coupling_method pointer to (in)dependent coupling function
++ */
++static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
++ enum RawDataBlockType type, int elem_id,
++ enum CouplingPoint coupling_point,
++ void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
++{
++ int i, c;
++
++ for (i = 0; i < MAX_ELEM_ID; i++) {
++ ChannelElement *cce = ac->che[TYPE_CCE][i];
++ int index = 0;
++
++ if (cce && cce->coup.coupling_point == coupling_point) {
++ ChannelCoupling *coup = &cce->coup;
++
++ for (c = 0; c <= coup->num_coupled; c++) {
++ if (coup->type[c] == type && coup->id_select[c] == elem_id) {
++ if (coup->ch_select[c] != 1) {
++ apply_coupling_method(ac, &cc->ch[0], cce, index);
++ if (coup->ch_select[c] != 0)
++ index++;
++ }
++ if (coup->ch_select[c] != 2)
++ apply_coupling_method(ac, &cc->ch[1], cce, index++);
++ } else
++ index += 1 + (coup->ch_select[c] == 3);
++ }
++ }
++ }
++}
++
++/**
++ * Convert spectral data to float samples, applying all supported tools as appropriate.
++ */
++static void spectral_to_sample(AACContext *ac)
++{
++ int i, type;
++ float imdct_bias = (ac->m4ac.sbr <= 0) ? ac->add_bias : 0.0f;
++ for (type = 3; type >= 0; type--) {
++ for (i = 0; i < MAX_ELEM_ID; i++) {
++ ChannelElement *che = ac->che[type][i];
++ if (che) {
++ if (type <= TYPE_CPE)
++ apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
++ if (che->ch[0].tns.present)
++ apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
++ if (che->ch[1].tns.present)
++ apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
++ if (type <= TYPE_CPE)
++ apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
++ if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
++ imdct_and_windowing(ac, &che->ch[0], imdct_bias);
++ if (type == TYPE_CPE) {
++ imdct_and_windowing(ac, &che->ch[1], imdct_bias);
++ }
++ if (ac->m4ac.sbr > 0) {
++ ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
++ }
++ }
++ if (type <= TYPE_CCE)
++ apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
++ }
++ }
++ }
++}
++
++static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
++{
++ int size;
++ AACADTSHeaderInfo hdr_info;
++
++ size = ff_aac_parse_header(gb, &hdr_info);
++ if (size > 0) {
++ if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
++ enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
++ memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
++ ac->m4ac.chan_config = hdr_info.chan_config;
++ if (set_default_channel_config(ac, new_che_pos, hdr_info.chan_config))
++ return -7;
++ if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
++ return -7;
++ } else if (ac->output_configured != OC_LOCKED) {
++ ac->output_configured = OC_NONE;
++ }
++ 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;
++ if (!ac->avctx->sample_rate)
++ ac->avctx->sample_rate = hdr_info.sample_rate;
++ if (hdr_info.num_aac_frames == 1) {
++ if (!hdr_info.crc_absent)
++ skip_bits(gb, 16);
++ } else {
++ av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
++ return -1;
++ }
++ }
++ return size;
++}
++
++static int aac_decode_frame(AVCodecContext *avctx, void *data,
++ int *data_size, AVPacket *avpkt)
++{
++ const uint8_t *buf = avpkt->data;
++ int buf_size = avpkt->size;
++ AACContext *ac = avctx->priv_data;
++ ChannelElement *che = NULL, *che_prev = NULL;
++ GetBitContext gb;
++ enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
++ int err, elem_id, data_size_tmp;
++ int buf_consumed;
++ int samples = 0, multiplier;
++ int buf_offset;
++
++ init_get_bits(&gb, buf, buf_size * 8);
++
++ if (show_bits(&gb, 12) == 0xfff) {
++ if (parse_adts_frame_header(ac, &gb) < 0) {
++ av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
++ return -1;
++ }
++ if (ac->m4ac.sampling_index > 12) {
++ av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
++ return -1;
++ }
++ }
++
++ memset(ac->tags_seen_this_frame, 0, sizeof(ac->tags_seen_this_frame));
++ // parse
++ while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
++ elem_id = get_bits(&gb, 4);
++
++ if (elem_type < TYPE_DSE) {
++ if (!(che=get_che(ac, elem_type, elem_id))) {
++ av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
++ elem_type, elem_id);
++ return -1;
++ }
++ samples = 1024;
++ }
++
++ switch (elem_type) {
++
++ case TYPE_SCE:
++ err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
++ break;
++
++ case TYPE_CPE:
++ err = decode_cpe(ac, &gb, che);
++ break;
++
++ case TYPE_CCE:
++ err = decode_cce(ac, &gb, che);
++ break;
++
++ case TYPE_LFE:
++ err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
++ break;
++
++ case TYPE_DSE:
++ err = skip_data_stream_element(ac, &gb);
++ break;
++
++ case TYPE_PCE: {
++ enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
++ memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
++ if ((err = decode_pce(ac, new_che_pos, &gb)))
++ break;
++ if (ac->output_configured > OC_TRIAL_PCE)
++ av_log(avctx, AV_LOG_ERROR,
++ "Not evaluating a further program_config_element as this construct is dubious at best.\n");
++ else
++ err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
++ break;
++ }
++
++ case TYPE_FIL:
++ if (elem_id == 15)
++ elem_id += get_bits(&gb, 8) - 1;
++ if (get_bits_left(&gb) < 8 * elem_id) {
++ av_log(avctx, AV_LOG_ERROR, overread_err);
++ return -1;
++ }
++ while (elem_id > 0)
++ elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev);
++ err = 0; /* FIXME */
++ break;
++
++ default:
++ err = -1; /* should not happen, but keeps compiler happy */
++ break;
++ }
++
++ che_prev = che;
++ elem_type_prev = elem_type;
++
++ if (err)
++ return err;
++
++ if (get_bits_left(&gb) < 3) {
++ av_log(avctx, AV_LOG_ERROR, overread_err);
++ return -1;
++ }
++ }
++
++ spectral_to_sample(ac);
++
++ multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
++ samples <<= multiplier;
++ if (ac->output_configured < OC_LOCKED) {
++ avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
++ avctx->frame_size = samples;
++ }
++
++ data_size_tmp = samples * avctx->channels * sizeof(int16_t);
++ if (*data_size < data_size_tmp) {
++ av_log(avctx, AV_LOG_ERROR,
++ "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
++ *data_size, data_size_tmp);
++ return -1;
++ }
++ *data_size = data_size_tmp;
++
++ if (samples)
++ ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
++
++ if (ac->output_configured)
++ ac->output_configured = OC_LOCKED;
++
++ buf_consumed = (get_bits_count(&gb) + 7) >> 3;
++ for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
++ if (buf[buf_offset])
++ break;
++
++ return buf_size > buf_offset ? buf_consumed : buf_size;
++}
++
++static av_cold int aac_decode_close(AVCodecContext *avctx)
++{
++ AACContext *ac = avctx->priv_data;
++ int i, type;
++
++ for (i = 0; i < MAX_ELEM_ID; i++) {
++ for (type = 0; type < 4; type++) {
++ if (ac->che[type][i])
++ ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
++ av_freep(&ac->che[type][i]);
++ }
++ }
++
++ ff_mdct_end(&ac->mdct);
++ ff_mdct_end(&ac->mdct_small);
++ return 0;
++}
++
++AVCodec aac_decoder = {
++ "aac",
++ AVMEDIA_TYPE_AUDIO,
++ CODEC_ID_AAC,
++ sizeof(AACContext),
++ aac_decode_init,
++ NULL,
++ aac_decode_close,
++ aac_decode_frame,
++ .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
++ .sample_fmts = (const enum SampleFormat[]) {
++ SAMPLE_FMT_S16,SAMPLE_FMT_NONE
++ },
++ .channel_layouts = aac_channel_layout,
++};
+--- a/libavcodec/aac.h
++++ b/libavcodec/aac.h
+@@ -38,12 +38,6 @@
+
+ #include <stdint.h>
+
+-#define AAC_INIT_VLC_STATIC(num, size) \
+- INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
+- ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
+- ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
+- size);
+-
+ #define MAX_CHANNELS 64
+ #define MAX_ELEM_ID 16
+
+@@ -241,7 +235,7 @@ typedef struct {
+ * main AAC context
+ */
+ typedef struct {
+- AVCodecContext * avccontext;
++ AVCodecContext *avctx;
+
+ MPEG4AudioConfig m4ac;
+
+@@ -255,8 +249,9 @@ typedef struct {
+ enum ChannelPosition che_pos[4][MAX_ELEM_ID]; /**< channel element channel mapping with the
+ * first index as the first 4 raw data block types
+ */
+- ChannelElement * che[4][MAX_ELEM_ID];
+- ChannelElement * tag_che_map[4][MAX_ELEM_ID];
++ ChannelElement *che[4][MAX_ELEM_ID];
++ ChannelElement *tag_che_map[4][MAX_ELEM_ID];
++ uint8_t tags_seen_this_frame[4][MAX_ELEM_ID];
+ int tags_mapped;
+ /** @} */
+
+--- /dev/null
++++ b/libavcodec/aac_tablegen_decl.h
+@@ -0,0 +1,34 @@
++/*
++ * Header file for hardcoded AAC tables
++ *
++ * Copyright (c) 2010 Alex Converse <alex.converse at gmail.com>
++ *
++ * 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 AAC_TABLEGEN_INIT_H
++#define AAC_TABLEGEN_INIT_H
++
++#if CONFIG_HARDCODED_TABLES
++#define ff_aac_tableinit()
++extern const float ff_aac_pow2sf_tab[428];
++#else
++void ff_aac_tableinit(void);
++extern float ff_aac_pow2sf_tab[428];
++#endif /* CONFIG_HARDCODED_TABLES */
++
++#endif /* AAC_TABLEGEN_INIT_H */
+--- /dev/null
++++ b/libavcodec/aacps.c
+@@ -0,0 +1,1037 @@
++/*
++ * MPEG-4 Parametric Stereo decoding functions
++ * Copyright (c) 2010 Alex Converse <alex.converse at gmail.com>
++ *
++ * 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 <stdint.h>
++#include "libavutil/common.h"
++#include "libavutil/mathematics.h"
++#include "avcodec.h"
++#include "get_bits.h"
++#include "aacps.h"
++#include "aacps_tablegen.h"
++#include "aacpsdata.c"
++
++#define PS_BASELINE 0 //< Operate in Baseline PS mode
++ //< Baseline implies 10 or 20 stereo bands,
++ //< mixing mode A, and no ipd/opd
++
++#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/Inter-Channel Coherence/
++ * Inter-channel Phase Difference/Overall Phase 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 par pointer to the parameter to be read
++ * @param e envelope to decode
++ * @param dt 1: time delta-coded, 0: frequency delta-coded
++ */
++#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
++static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
++ int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
++{ \
++ int b, num = ps->nr_ ## PAR ## _par; \
++ 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 < num; b++) { \
++ int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
++ if (MASK) val &= MASK; \
++ PAR[e][b] = val; \
++ if (ERR_CONDITION) \
++ goto err; \
++ } \
++ } else { \
++ int val = 0; \
++ for (b = 0; b < num; b++) { \
++ val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
++ if (MASK) val &= MASK; \
++ PAR[e][b] = val; \
++ if (ERR_CONDITION) \
++ goto err; \
++ } \
++ } \
++ return 0; \
++err: \
++ av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
++ return -1; \
++}
++
++READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
++READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U)
++READ_PAR_DATA(ipdopd, 0, 0x07, 0)
++
++static int ps_read_extension_data(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);
++ read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
++ dt = get_bits1(gb);
++ read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, 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) {
++ int iid_mode = get_bits(gb, 3);
++ if (iid_mode > 5) {
++ av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
++ iid_mode);
++ goto err;
++ }
++ ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
++ ps->iid_quant = iid_mode > 2;
++ ps->nr_ipdopd_par = nr_iidopd_par_tab[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 >> ff_log2_tab[ps->num_env]) - 1;
++
++ if (ps->enable_iid) {
++ for (e = 0; e < ps->num_env; e++) {
++ int dt = get_bits1(gb);
++ if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], 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 (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, 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_read_extension_data(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) {
++ memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
++ }
++ if (ps->enable_icc) {
++ memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
++ }
++ if (ps->enable_ipdopd) {
++ 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++, in++) {
++ float re_in = filter[6] * in[6][0]; //real inphase
++ float re_op = 0.0f; //real out of phase
++ float im_in = filter[6] * in[6][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[j+1][0] + in[12-j-1][0]);
++ im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][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++, in++) {
++ for (ssb = 0; ssb < N; ssb++) {
++ float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
++ for (j = 0; j < 6; j++) {
++ float in0_re = in[j][0];
++ float in0_im = in[j][1];
++ float in1_re = in[12-j][0];
++ float in1_im = in[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++, in++) {
++ for (ssb = 0; ssb < N; ssb++) {
++ float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
++ for (j = 0; j < 6; j++) {
++ float in0_re = in[j][0];
++ float in0_im = in[j][1];
++ float in1_re = in[12-j][0];
++ float in1_im = in[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
++ memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
++ memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
++ memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
++ memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
++ memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
++ memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
++ memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
++ memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
++ 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)
++{
++}
+--- /dev/null
++++ b/libavcodec/aacps.h
+@@ -0,0 +1,82 @@
++/*
++ * MPEG-4 Parametric Stereo definitions and declarations
++ * Copyright (c) 2010 Alex Converse <alex.converse at gmail.com>
++ *
++ * 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 <stdint.h>
++
++#include "avcodec.h"
++#include "get_bits.h"
++
++#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_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]; //<Inter-channel Intensity Difference Parameters
++ int8_t icc_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; //<Inter-Channel Coherence Parameters
++ /* ipd/opd is iid/icc sized so that the same functions can handle both */
++ int8_t ipd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; //<Inter-channel Phase Difference Parameters
++ int8_t opd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; //<Overall Phase Difference Parameters
++ int is34bands;
++ int is34bands_old;
++
++ float in_buf[5][44][2];
++ float delay[PS_MAX_SSB][PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2];
++ float ap_delay[PS_MAX_AP_BANDS][PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2];
++ float peak_decay_nrg[34];
++ float power_smooth[34];
++ float peak_decay_diff_smooth[34];
++ float H11[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC];
++ float H12[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC];
++ float H21[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC];
++ float H22[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC];
++ int8_t opd_hist[PS_MAX_NR_IIDICC];
++ int8_t ipd_hist[PS_MAX_NR_IIDICC];
++} PSContext;
++
++void ff_ps_init(void);
++void ff_ps_ctx_init(PSContext *ps);
++int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, int bits_left);
++int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top);
++
++#endif /* AVCODEC_PS_H */
+--- /dev/null
++++ b/libavcodec/aacpsdata.c
+@@ -0,0 +1,163 @@
++/*
++ * MPEG-4 Parametric Stereo data tables
++ * Copyright (c) 2010 Alex Converse <alex.converse at gmail.com>
++ *
++ * 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
++};
+--- a/libavcodec/sbr.h
++++ b/libavcodec/sbr.h
+@@ -31,6 +31,7 @@
+
+ #include <stdint.h>
+ #include "fft.h"
++#include "aacps.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
+@@ -176,7 +178,7 @@ typedef struct {
+ float s_m[7][48];
+ float gain[7][48];
+ DECLARE_ALIGNED(16, float, qmf_filter_scratch)[5][64];
+- RDFTContext rdft;
++ FFTContext mdct_ana;
+ FFTContext mdct;
+ } SpectralBandReplication;
+
+--- a/libavcodec/Makefile
++++ b/libavcodec/Makefile
+@@ -43,7 +43,7 @@ OBJS-$(CONFIG_VAAPI) +
+ OBJS-$(CONFIG_VDPAU) += vdpau.o
+
+ # decoders/encoders/hardware accelerators
+-OBJS-$(CONFIG_AAC_DECODER) += aac.o aactab.o aacsbr.o
++OBJS-$(CONFIG_AAC_DECODER) += aacdec.o aactab.o aacsbr.o aacps.o
+ OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o \
+ aacpsy.o aactab.o \
+ psymodel.o iirfilter.o \
+--- a/libavcodec/aacsbr.c
++++ b/libavcodec/aacsbr.c
+@@ -31,6 +31,7 @@
+ #include "aacsbr.h"
+ #include "aacsbrdata.h"
+ #include "fft.h"
++#include "aacps.h"
+
+ #include <stdint.h>
+ #include <float.h>
+@@ -71,9 +72,6 @@ enum {
+ static VLC vlc_sbr[10];
+ static const int8_t vlc_sbr_lav[10] =
+ { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
+-static DECLARE_ALIGNED(16, float, analysis_cos_pre)[64];
+-static DECLARE_ALIGNED(16, float, analysis_sin_pre)[64];
+-static DECLARE_ALIGNED(16, float, analysis_cossin_post)[32][2];
+ static const DECLARE_ALIGNED(16, float, zero64)[64];
+
+ #define SBR_INIT_VLC_STATIC(num, size) \
+@@ -87,7 +85,7 @@ static const DECLARE_ALIGNED(16, float,
+
+ av_cold void ff_aac_sbr_init(void)
+ {
+- int n, k;
++ int n;
+ static const struct {
+ const void *sbr_codes, *sbr_bits;
+ const unsigned int table_size, elem_size;
+@@ -116,16 +114,6 @@ av_cold void ff_aac_sbr_init(void)
+ SBR_INIT_VLC_STATIC(8, 592);
+ SBR_INIT_VLC_STATIC(9, 512);
+
+- for (n = 0; n < 64; n++) {
+- float pre = M_PI * n / 64;
+- analysis_cos_pre[n] = cosf(pre);
+- analysis_sin_pre[n] = sinf(pre);
+- }
+- for (k = 0; k < 32; k++) {
+- float post = M_PI * (k + 0.5) / 128;
+- analysis_cossin_post[k][0] = 4.0 * cosf(post);
+- analysis_cossin_post[k][1] = -4.0 * sinf(post);
+- }
+ for (n = 1; n < 320; n++)
+ sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n];
+ sbr_qmf_window_us[384] = -sbr_qmf_window_us[384];
+@@ -133,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)
+@@ -142,13 +132,14 @@ av_cold void ff_aac_sbr_ctx_init(Spectra
+ sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
+ sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
+ ff_mdct_init(&sbr->mdct, 7, 1, 1.0/64);
+- ff_rdft_init(&sbr->rdft, 6, IDFT_R2C);
++ 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)
+ {
+ ff_mdct_end(&sbr->mdct);
+- ff_rdft_end(&sbr->rdft);
++ ff_mdct_end(&sbr->mdct_ana);
+ }
+
+ static int qsort_comparison_function_int16(const void *a, const void *b)
+@@ -293,15 +284,15 @@ static void make_bands(int16_t* bands, i
+ bands[num_bands-1] = stop - previous;
+ }
+
+-static int check_n_master(AVCodecContext *avccontext, int n_master, int bs_xover_band)
++static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
+ {
+ // Requirements (14496-3 sp04 p205)
+ if (n_master <= 0) {
+- av_log(avccontext, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
++ av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
+ return -1;
+ }
+ if (bs_xover_band >= n_master) {
+- av_log(avccontext, AV_LOG_ERROR,
++ av_log(avctx, AV_LOG_ERROR,
+ "Invalid bitstream, crossover band index beyond array bounds: %d\n",
+ bs_xover_band);
+ return -1;
+@@ -349,7 +340,7 @@ static int sbr_make_f_master(AACContext
+ sbr_offset_ptr = sbr_offset[5];
+ break;
+ default:
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
+ return -1;
+ }
+@@ -367,7 +358,7 @@ static int sbr_make_f_master(AACContext
+ } else if (spectrum->bs_stop_freq == 15) {
+ sbr->k[2] = 3*sbr->k[0];
+ } else {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
+ return -1;
+ }
+@@ -382,18 +373,17 @@ static int sbr_make_f_master(AACContext
+ max_qmf_subbands = 32;
+
+ if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
+ return -1;
+ }
+
+ if (!spectrum->bs_freq_scale) {
+- unsigned int dk;
+- int k2diff;
++ int dk, k2diff;
+
+ dk = spectrum->bs_alter_scale + 1;
+ sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
+- if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
++ if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
+ return -1;
+
+ for (k = 1; k <= sbr->n_master; k++)
+@@ -428,7 +418,7 @@ static int sbr_make_f_master(AACContext
+ num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
+
+ if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
+- av_log(ac->avccontext, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
++ av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
+ return -1;
+ }
+
+@@ -442,7 +432,7 @@ static int sbr_make_f_master(AACContext
+ vk0[0] = sbr->k[0];
+ for (k = 1; k <= num_bands_0; k++) {
+ if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
+- av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
++ av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
+ return -1;
+ }
+ vk0[k] += vk0[k-1];
+@@ -472,14 +462,14 @@ static int sbr_make_f_master(AACContext
+ vk1[0] = sbr->k[1];
+ for (k = 1; k <= num_bands_1; k++) {
+ if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
+- av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
++ av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
+ return -1;
+ }
+ vk1[k] += vk1[k-1];
+ }
+
+ sbr->n_master = num_bands_0 + num_bands_1;
+- if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
++ if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
+ return -1;
+ memcpy(&sbr->f_master[0], vk0,
+ (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
+@@ -488,7 +478,7 @@ static int sbr_make_f_master(AACContext
+
+ } else {
+ sbr->n_master = num_bands_0;
+- if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
++ if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
+ return -1;
+ memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
+ }
+@@ -524,7 +514,7 @@ static int sbr_hf_calc_npatches(AACConte
+ // illegal however the Coding Technologies decoder check stream has a final
+ // count of 6 patches
+ if (sbr->num_patches > 5) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
++ av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
+ return -1;
+ }
+
+@@ -563,12 +553,12 @@ static int sbr_make_f_derived(AACContext
+
+ // Requirements (14496-3 sp04 p205)
+ if (sbr->kx[1] + sbr->m[1] > 64) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
+ return -1;
+ }
+ if (sbr->kx[1] > 32) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
++ av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
+ return -1;
+ }
+
+@@ -580,7 +570,7 @@ static int sbr_make_f_derived(AACContext
+ sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
+ log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
+ if (sbr->n_q > 5) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
++ av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
+ return -1;
+ }
+
+@@ -638,7 +628,7 @@ static int read_sbr_grid(AACContext *ac,
+ ch_data->bs_amp_res = 0;
+
+ if (ch_data->bs_num_env > 4) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
+ ch_data->bs_num_env);
+ return -1;
+@@ -693,7 +683,7 @@ static int read_sbr_grid(AACContext *ac,
+ ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1;
+
+ if (ch_data->bs_num_env > 5) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
+ ch_data->bs_num_env);
+ return -1;
+@@ -714,7 +704,7 @@ static int read_sbr_grid(AACContext *ac,
+ }
+
+ if (bs_pointer > ch_data->bs_num_env + 1) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
+ bs_pointer);
+ return -1;
+@@ -722,7 +712,7 @@ static int read_sbr_grid(AACContext *ac,
+
+ for (i = 1; i <= ch_data->bs_num_env; i++) {
+ if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Non monotone time borders\n");
++ av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
+ return -1;
+ }
+ }
+@@ -903,25 +893,24 @@ static void read_sbr_extension(AACContex
+ 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) {
+- av_log(ac->avccontext, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
++ av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
+ 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->avccontext, "Parametric Stereo is", 0);
++ av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0);
+ skip_bits_long(gb, *num_bits_left); // bs_fill_bits
+ *num_bits_left = 0;
+ #endif
+ }
+ break;
+ default:
+- av_log_missing_feature(ac->avccontext, "Reserved SBR extensions are", 1);
++ av_log_missing_feature(ac->avctx, "Reserved SBR extensions are", 1);
+ skip_bits_long(gb, *num_bits_left); // bs_fill_bits
+ *num_bits_left = 0;
+ break;
+@@ -1006,7 +995,7 @@ static unsigned int read_sbr_data(AACCon
+ return get_bits_count(gb) - cnt;
+ }
+ } else {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
+ sbr->start = 0;
+ return get_bits_count(gb) - cnt;
+@@ -1021,6 +1010,11 @@ static unsigned int read_sbr_data(AACCon
+ 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;
+@@ -1033,7 +1027,7 @@ static void sbr_reset(AACContext *ac, Sp
+ if (err >= 0)
+ err = sbr_make_f_derived(ac, sbr);
+ if (err < 0) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "SBR reset failed. Switching SBR to pure upsampling mode.\n");
+ sbr->start = 0;
+ }
+@@ -1085,7 +1079,7 @@ int ff_decode_sbr_extension(AACContext *
+ bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
+
+ if (bytes_read > cnt) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
+ }
+ return cnt;
+@@ -1139,7 +1133,7 @@ static void sbr_dequant(SpectralBandRepl
+ * @param x pointer to the beginning of the first sample window
+ * @param W array of complex-valued samples split into subbands
+ */
+-static void sbr_qmf_analysis(DSPContext *dsp, RDFTContext *rdft, const float *in, float *x,
++static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x,
+ float z[320], float W[2][32][32][2],
+ float scale)
+ {
+@@ -1152,23 +1146,23 @@ static void sbr_qmf_analysis(DSPContext
+ memcpy(x+288, in, 1024*sizeof(*x));
+ for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
+ // are not supported
+- float re, im;
+ dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
+ for (k = 0; k < 64; k++) {
+ float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256];
+- z[k] = f * analysis_cos_pre[k];
+- z[k+64] = f;
++ z[k] = f;
+ }
+- ff_rdft_calc(rdft, z);
+- re = z[0] * 0.5f;
+- im = 0.5f * dsp->scalarproduct_float(z+64, analysis_sin_pre, 64);
+- W[1][i][0][0] = re * analysis_cossin_post[0][0] - im * analysis_cossin_post[0][1];
+- W[1][i][0][1] = re * analysis_cossin_post[0][1] + im * analysis_cossin_post[0][0];
++ //Shuffle to IMDCT
++ z[64] = z[0];
+ for (k = 1; k < 32; k++) {
+- re = z[2*k ] - re;
+- im = z[2*k+1] - im;
+- W[1][i][k][0] = re * analysis_cossin_post[k][0] - im * analysis_cossin_post[k][1];
+- W[1][i][k][1] = re * analysis_cossin_post[k][1] + im * analysis_cossin_post[k][0];
++ z[64+2*k-1] = z[ k];
++ z[64+2*k ] = -z[64-k];
++ }
++ z[64+63] = z[32];
++
++ ff_imdct_half(mdct, z, z+64);
++ for (k = 0; k < 32; k++) {
++ W[1][i][k][0] = -z[63-k];
++ W[1][i][k][1] = z[k];
+ }
+ x += 32;
+ }
+@@ -1179,7 +1173,7 @@ static void sbr_qmf_analysis(DSPContext
+ * (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)
+@@ -1197,21 +1191,22 @@ static void sbr_qmf_synthesis(DSPContext
+ *v_off -= 128 >> div;
+ }
+ v = v0 + *v_off;
+- for (n = 1; n < 64 >> div; n+=2) {
+- X[1][i][n] = -X[1][i][n];
+- }
+- if (div) {
+- memset(X[0][i]+32, 0, 32*sizeof(float));
+- memset(X[1][i]+32, 0, 32*sizeof(float));
+- }
+- ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
+- ff_imdct_half(mdct, mdct_buf[1], X[1][i]);
+ if (div) {
+ for (n = 0; n < 32; n++) {
+- v[ n] = -mdct_buf[0][63 - 2*n] + mdct_buf[1][2*n ];
+- v[ 63 - n] = mdct_buf[0][62 - 2*n] + mdct_buf[1][2*n + 1];
++ X[0][i][ n] = -X[0][i][n];
++ X[0][i][32+n] = X[1][i][31-n];
++ }
++ ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
++ for (n = 0; n < 32; n++) {
++ v[ n] = mdct_buf[0][63 - 2*n];
++ v[63 - n] = -mdct_buf[0][62 - 2*n];
+ }
+ } else {
++ for (n = 1; n < 64; n+=2) {
++ X[1][i][n] = -X[1][i][n];
++ }
++ ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
++ ff_imdct_half(mdct, mdct_buf[1], X[1][i]);
+ for (n = 0; n < 64; n++) {
+ v[ n] = -mdct_buf[0][63 - n] + mdct_buf[1][ n ];
+ v[127 - n] = mdct_buf[0][63 - n] + mdct_buf[1][ n ];
+@@ -1380,7 +1375,7 @@ static int sbr_hf_gen(AACContext *ac, Sp
+ g--;
+
+ if (g < 0) {
+- av_log(ac->avccontext, AV_LOG_ERROR,
++ av_log(ac->avctx, AV_LOG_ERROR,
+ "ERROR : no subband found for frequency %d\n", k);
+ return -1;
+ }
+@@ -1414,7 +1409,7 @@ static int sbr_hf_gen(AACContext *ac, Sp
+ }
+
+ /// 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)
+ {
+@@ -1436,7 +1431,7 @@ static int sbr_x_gen(SpectralBandReplica
+ }
+
+ 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];
+ }
+@@ -1730,7 +1725,7 @@ void ff_sbr_apply(AACContext *ac, Spectr
+ }
+ for (ch = 0; ch < nch; ch++) {
+ /* decode channel */
+- sbr_qmf_analysis(&ac->dsp, &sbr->rdft, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
++ sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
+ (float*)sbr->qmf_filter_scratch,
+ sbr->data[ch].W, 1/(-1024 * ac->sf_scale));
+ sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W);
+@@ -1752,6 +1747,16 @@ void ff_sbr_apply(AACContext *ac, Spectr
+ /* 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,
+--- a/libavcodec/aactab.c
++++ b/libavcodec/aactab.c
+@@ -29,6 +29,7 @@
+
+ #include "libavutil/mem.h"
+ #include "aac.h"
++#include "aac_tablegen.h"
+
+ #include <stdint.h>
+
+@@ -1204,129 +1205,3 @@ const uint8_t ff_tns_max_bands_128[] = {
+ 9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14
+ };
+ // @}
+-
+-
+-#if CONFIG_HARDCODED_TABLES
+-
+-/**
+- * Table of pow(2, (i - 200)/4.) used for different purposes depending on the
+- * range of indices to the table:
+- * [ 0, 255] scale factor decoding when using C dsp.float_to_int16
+- * [60, 315] scale factor decoding when using SIMD dsp.float_to_int16
+- * [45, 300] intensity stereo position decoding mapped in reverse order i.e. 0->300, 1->299, ..., 254->46, 255->45
+- */
+-const float ff_aac_pow2sf_tab[428] = {
+- 8.88178420e-16, 1.05622810e-15, 1.25607397e-15, 1.49373210e-15,
+- 1.77635684e-15, 2.11245619e-15, 2.51214793e-15, 2.98746420e-15,
+- 3.55271368e-15, 4.22491238e-15, 5.02429587e-15, 5.97492839e-15,
+- 7.10542736e-15, 8.44982477e-15, 1.00485917e-14, 1.19498568e-14,
+- 1.42108547e-14, 1.68996495e-14, 2.00971835e-14, 2.38997136e-14,
+- 2.84217094e-14, 3.37992991e-14, 4.01943669e-14, 4.77994272e-14,
+- 5.68434189e-14, 6.75985982e-14, 8.03887339e-14, 9.55988543e-14,
+- 1.13686838e-13, 1.35197196e-13, 1.60777468e-13, 1.91197709e-13,
+- 2.27373675e-13, 2.70394393e-13, 3.21554936e-13, 3.82395417e-13,
+- 4.54747351e-13, 5.40788785e-13, 6.43109871e-13, 7.64790834e-13,
+- 9.09494702e-13, 1.08157757e-12, 1.28621974e-12, 1.52958167e-12,
+- 1.81898940e-12, 2.16315514e-12, 2.57243948e-12, 3.05916334e-12,
+- 3.63797881e-12, 4.32631028e-12, 5.14487897e-12, 6.11832668e-12,
+- 7.27595761e-12, 8.65262056e-12, 1.02897579e-11, 1.22366534e-11,
+- 1.45519152e-11, 1.73052411e-11, 2.05795159e-11, 2.44733067e-11,
+- 2.91038305e-11, 3.46104823e-11, 4.11590317e-11, 4.89466134e-11,
+- 5.82076609e-11, 6.92209645e-11, 8.23180635e-11, 9.78932268e-11,
+- 1.16415322e-10, 1.38441929e-10, 1.64636127e-10, 1.95786454e-10,
+- 2.32830644e-10, 2.76883858e-10, 3.29272254e-10, 3.91572907e-10,
+- 4.65661287e-10, 5.53767716e-10, 6.58544508e-10, 7.83145814e-10,
+- 9.31322575e-10, 1.10753543e-09, 1.31708902e-09, 1.56629163e-09,
+- 1.86264515e-09, 2.21507086e-09, 2.63417803e-09, 3.13258326e-09,
+- 3.72529030e-09, 4.43014173e-09, 5.26835606e-09, 6.26516652e-09,
+- 7.45058060e-09, 8.86028346e-09, 1.05367121e-08, 1.25303330e-08,
+- 1.49011612e-08, 1.77205669e-08, 2.10734243e-08, 2.50606661e-08,
+- 2.98023224e-08, 3.54411338e-08, 4.21468485e-08, 5.01213321e-08,
+- 5.96046448e-08, 7.08822677e-08, 8.42936970e-08, 1.00242664e-07,
+- 1.19209290e-07, 1.41764535e-07, 1.68587394e-07, 2.00485328e-07,
+- 2.38418579e-07, 2.83529071e-07, 3.37174788e-07, 4.00970657e-07,
+- 4.76837158e-07, 5.67058141e-07, 6.74349576e-07, 8.01941314e-07,
+- 9.53674316e-07, 1.13411628e-06, 1.34869915e-06, 1.60388263e-06,
+- 1.90734863e-06, 2.26823256e-06, 2.69739830e-06, 3.20776526e-06,
+- 3.81469727e-06, 4.53646513e-06, 5.39479661e-06, 6.41553051e-06,
+- 7.62939453e-06, 9.07293026e-06, 1.07895932e-05, 1.28310610e-05,
+- 1.52587891e-05, 1.81458605e-05, 2.15791864e-05, 2.56621220e-05,
+- 3.05175781e-05, 3.62917210e-05, 4.31583729e-05, 5.13242441e-05,
+- 6.10351562e-05, 7.25834421e-05, 8.63167458e-05, 1.02648488e-04,
+- 1.22070312e-04, 1.45166884e-04, 1.72633492e-04, 2.05296976e-04,
+- 2.44140625e-04, 2.90333768e-04, 3.45266983e-04, 4.10593953e-04,
+- 4.88281250e-04, 5.80667537e-04, 6.90533966e-04, 8.21187906e-04,
+- 9.76562500e-04, 1.16133507e-03, 1.38106793e-03, 1.64237581e-03,
+- 1.95312500e-03, 2.32267015e-03, 2.76213586e-03, 3.28475162e-03,
+- 3.90625000e-03, 4.64534029e-03, 5.52427173e-03, 6.56950324e-03,
+- 7.81250000e-03, 9.29068059e-03, 1.10485435e-02, 1.31390065e-02,
+- 1.56250000e-02, 1.85813612e-02, 2.20970869e-02, 2.62780130e-02,
+- 3.12500000e-02, 3.71627223e-02, 4.41941738e-02, 5.25560260e-02,
+- 6.25000000e-02, 7.43254447e-02, 8.83883476e-02, 1.05112052e-01,
+- 1.25000000e-01, 1.48650889e-01, 1.76776695e-01, 2.10224104e-01,
+- 2.50000000e-01, 2.97301779e-01, 3.53553391e-01, 4.20448208e-01,
+- 5.00000000e-01, 5.94603558e-01, 7.07106781e-01, 8.40896415e-01,
+- 1.00000000e+00, 1.18920712e+00, 1.41421356e+00, 1.68179283e+00,
+- 2.00000000e+00, 2.37841423e+00, 2.82842712e+00, 3.36358566e+00,
+- 4.00000000e+00, 4.75682846e+00, 5.65685425e+00, 6.72717132e+00,
+- 8.00000000e+00, 9.51365692e+00, 1.13137085e+01, 1.34543426e+01,
+- 1.60000000e+01, 1.90273138e+01, 2.26274170e+01, 2.69086853e+01,
+- 3.20000000e+01, 3.80546277e+01, 4.52548340e+01, 5.38173706e+01,
+- 6.40000000e+01, 7.61092554e+01, 9.05096680e+01, 1.07634741e+02,
+- 1.28000000e+02, 1.52218511e+02, 1.81019336e+02, 2.15269482e+02,
+- 2.56000000e+02, 3.04437021e+02, 3.62038672e+02, 4.30538965e+02,
+- 5.12000000e+02, 6.08874043e+02, 7.24077344e+02, 8.61077929e+02,
+- 1.02400000e+03, 1.21774809e+03, 1.44815469e+03, 1.72215586e+03,
+- 2.04800000e+03, 2.43549617e+03, 2.89630938e+03, 3.44431172e+03,
+- 4.09600000e+03, 4.87099234e+03, 5.79261875e+03, 6.88862343e+03,
+- 8.19200000e+03, 9.74198469e+03, 1.15852375e+04, 1.37772469e+04,
+- 1.63840000e+04, 1.94839694e+04, 2.31704750e+04, 2.75544937e+04,
+- 3.27680000e+04, 3.89679387e+04, 4.63409500e+04, 5.51089875e+04,
+- 6.55360000e+04, 7.79358775e+04, 9.26819000e+04, 1.10217975e+05,
+- 1.31072000e+05, 1.55871755e+05, 1.85363800e+05, 2.20435950e+05,
+- 2.62144000e+05, 3.11743510e+05, 3.70727600e+05, 4.40871900e+05,
+- 5.24288000e+05, 6.23487020e+05, 7.41455200e+05, 8.81743800e+05,
+- 1.04857600e+06, 1.24697404e+06, 1.48291040e+06, 1.76348760e+06,
+- 2.09715200e+06, 2.49394808e+06, 2.96582080e+06, 3.52697520e+06,
+- 4.19430400e+06, 4.98789616e+06, 5.93164160e+06, 7.05395040e+06,
+- 8.38860800e+06, 9.97579232e+06, 1.18632832e+07, 1.41079008e+07,
+- 1.67772160e+07, 1.99515846e+07, 2.37265664e+07, 2.82158016e+07,
+- 3.35544320e+07, 3.99031693e+07, 4.74531328e+07, 5.64316032e+07,
+- 6.71088640e+07, 7.98063385e+07, 9.49062656e+07, 1.12863206e+08,
+- 1.34217728e+08, 1.59612677e+08, 1.89812531e+08, 2.25726413e+08,
+- 2.68435456e+08, 3.19225354e+08, 3.79625062e+08, 4.51452825e+08,
+- 5.36870912e+08, 6.38450708e+08, 7.59250125e+08, 9.02905651e+08,
+- 1.07374182e+09, 1.27690142e+09, 1.51850025e+09, 1.80581130e+09,
+- 2.14748365e+09, 2.55380283e+09, 3.03700050e+09, 3.61162260e+09,
+- 4.29496730e+09, 5.10760567e+09, 6.07400100e+09, 7.22324521e+09,
+- 8.58993459e+09, 1.02152113e+10, 1.21480020e+10, 1.44464904e+10,
+- 1.71798692e+10, 2.04304227e+10, 2.42960040e+10, 2.88929808e+10,
+- 3.43597384e+10, 4.08608453e+10, 4.85920080e+10, 5.77859616e+10,
+- 6.87194767e+10, 8.17216907e+10, 9.71840160e+10, 1.15571923e+11,
+- 1.37438953e+11, 1.63443381e+11, 1.94368032e+11, 2.31143847e+11,
+- 2.74877907e+11, 3.26886763e+11, 3.88736064e+11, 4.62287693e+11,
+- 5.49755814e+11, 6.53773525e+11, 7.77472128e+11, 9.24575386e+11,
+- 1.09951163e+12, 1.30754705e+12, 1.55494426e+12, 1.84915077e+12,
+- 2.19902326e+12, 2.61509410e+12, 3.10988851e+12, 3.69830155e+12,
+- 4.39804651e+12, 5.23018820e+12, 6.21977702e+12, 7.39660309e+12,
+- 8.79609302e+12, 1.04603764e+13, 1.24395540e+13, 1.47932062e+13,
+- 1.75921860e+13, 2.09207528e+13, 2.48791081e+13, 2.95864124e+13,
+- 3.51843721e+13, 4.18415056e+13, 4.97582162e+13, 5.91728247e+13,
+- 7.03687442e+13, 8.36830112e+13, 9.95164324e+13, 1.18345649e+14,
+- 1.40737488e+14, 1.67366022e+14, 1.99032865e+14, 2.36691299e+14,
+- 2.81474977e+14, 3.34732045e+14, 3.98065730e+14, 4.73382598e+14,
+- 5.62949953e+14, 6.69464090e+14, 7.96131459e+14, 9.46765196e+14,
+- 1.12589991e+15, 1.33892818e+15, 1.59226292e+15, 1.89353039e+15,
+- 2.25179981e+15, 2.67785636e+15, 3.18452584e+15, 3.78706078e+15,
+- 4.50359963e+15, 5.35571272e+15, 6.36905167e+15, 7.57412156e+15,
+- 9.00719925e+15, 1.07114254e+16, 1.27381033e+16, 1.51482431e+16,
+- 1.80143985e+16, 2.14228509e+16, 2.54762067e+16, 3.02964863e+16,
+- 3.60287970e+16, 4.28457018e+16, 5.09524134e+16, 6.05929725e+16,
+- 7.20575940e+16, 8.56914035e+16, 1.01904827e+17, 1.21185945e+17,
+-};
+-
+-#else
+-
+-float ff_aac_pow2sf_tab[428];
+-
+-#endif /* CONFIG_HARDCODED_TABLES */
+--- a/libavcodec/aactab.h
++++ b/libavcodec/aactab.h
+@@ -32,6 +32,7 @@
+
+ #include "libavutil/mem.h"
+ #include "aac.h"
++#include "aac_tablegen_decl.h"
+
+ #include <stdint.h>
+
+@@ -73,10 +74,4 @@ extern const uint16_t * const ff_swb_off
+ extern const uint8_t ff_tns_max_bands_1024[13];
+ extern const uint8_t ff_tns_max_bands_128 [13];
+
+-#if CONFIG_HARDCODED_TABLES
+-extern const float ff_aac_pow2sf_tab[428];
+-#else
+-extern float ff_aac_pow2sf_tab[428];
+-#endif /* CONFIG_HARDCODED_TABLES */
+-
+ #endif /* AVCODEC_AACTAB_H */
+--- /dev/null
++++ b/libavcodec/aac_tablegen.c
+@@ -0,0 +1,39 @@
++/*
++ * Generate a header file for hardcoded AAC tables
++ *
++ * Copyright (c) 2010 Alex Converse <alex.converse at gmail.com>
++ *
++ * 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 <stdlib.h>
++#define CONFIG_HARDCODED_TABLES 0
++#include "aac_tablegen.h"
++#include "tableprint.h"
++
++int main(void)
++{
++ ff_aac_tableinit();
++
++ write_fileheader();
++
++ printf("const float ff_aac_pow2sf_tab[428] = {\n");
++ write_float_array(ff_aac_pow2sf_tab, 428);
++ printf("};\n");
++
++ return 0;
++}
+--- /dev/null
++++ b/libavcodec/aac_tablegen.h
+@@ -0,0 +1,42 @@
++/*
++ * Header file for hardcoded AAC tables
++ *
++ * Copyright (c) 2010 Alex Converse <alex.converse at gmail.com>
++ *
++ * 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 AAC_TABLEGEN_H
++#define AAC_TABLEGEN_H
++
++#include "aac_tablegen_decl.h"
++
++#if CONFIG_HARDCODED_TABLES
++#include "libavcodec/aac_tables.h"
++#else
++#include "../libavutil/mathematics.h"
++float ff_aac_pow2sf_tab[428];
++
++void ff_aac_tableinit(void)
++{
++ int i;
++ for (i = 0; i < 428; i++)
++ ff_aac_pow2sf_tab[i] = pow(2, (i - 200) / 4.);
++}
++#endif /* CONFIG_HARDCODED_TABLES */
++
++#endif /* AAC_TABLEGEN_H */
+--- /dev/null
++++ b/libavcodec/aacps_tablegen.c
+@@ -0,0 +1,93 @@
++/*
++ * Generate a header file for hardcoded Parametric Stereo tables
++ *
++ * Copyright (c) 2010 Alex Converse <alex.converse at gmail.com>
++ *
++ * 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 <stdlib.h>
++#define CONFIG_HARDCODED_TABLES 0
++#include "aacps_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;
++}
+--- /dev/null
++++ b/libavcodec/aacps_tablegen.h
+@@ -0,0 +1,212 @@
++/*
++ * Header file for hardcoded Parametric Stereo tables
++ *
++ * Copyright (c) 2010 Alex Converse <alex.converse at gmail.com>
++ *
++ * 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 AACPS_TABLEGEN_H
++#define AACPS_TABLEGEN_H
++
++#include <stdint.h>
++
++#if CONFIG_HARDCODED_TABLES
++#define ps_tableinit()
++#include "libavcodec/aacps_tables.h"
++#else
++#include "../libavutil/common.h"
++#include "../libavutil/mathematics.h"
++#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]; //<Linear Inter-channel Intensity Difference
++ float c1 = (float)M_SQRT2 / sqrtf(1.0f + c*c);
++ float c2 = c * c1;
++ for (icc = 0; icc < 8; icc++) {
++ /*if (PS_BASELINE || ps->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 /* AACPS_TABLEGEN_H */
diff --git a/debian/patches/series b/debian/patches/series
index 8105776..4db8bde 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -1,2 +1,3 @@
0001-Add-VP80-fourcc.patch
0002-Tweak-doxygen-config.patch
+0003-Backport-AAC-HE-v2.patch
--
FFmpeg packaging
More information about the pkg-multimedia-commits
mailing list