[FFmpeg-soc] [soc]: r5388 - in sbr-wip: . TODO aacsbr.c aacsbr.h aacsbrdata.h checkout.sh ffmpeg.diff

superdump subversion at mplayerhq.hu
Sat Sep 26 16:59:27 CEST 2009


Author: superdump
Date: Sat Sep 26 16:59:27 2009
New Revision: 5388

Log:
Initial AAC Spectral Band Replication commit. Please note that this code is not
yet functional and needs cleaning up quite a bit.

Added:
   sbr-wip/
   sbr-wip/TODO
   sbr-wip/aacsbr.c
   sbr-wip/aacsbr.h
   sbr-wip/aacsbrdata.h
   sbr-wip/checkout.sh   (contents, props changed)
   sbr-wip/ffmpeg.diff

Added: sbr-wip/TODO
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ sbr-wip/TODO	Sat Sep 26 16:59:27 2009	(r5388)
@@ -0,0 +1,10 @@
+This AAC SBR code is currently not functional and is merely a work-in-progress.
+This document will be updated when it is functional. At the moment, the
+following things need doing:
+
+- Write single channel and channel pair sbr application functions as well as an
+  apply_sbr() function
+- Move variables to appropriate contexts and code blocks
+- Use FFmpeg's dsputils for FFTs and other operations as appropriate
+- Clean up the code
+- More

Added: sbr-wip/aacsbr.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ sbr-wip/aacsbr.c	Sat Sep 26 16:59:27 2009	(r5388)
@@ -0,0 +1,1509 @@
+/*
+ * AAC Spectral Band Replication decoding functions
+ * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
+ *
+ * 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 libavcodec/aacsbr.c
+ * AAC Spectral Band Replication decoding functions
+ * @author Robert Swain ( rob opendot cl )
+ */
+
+#include "aac.h"
+#include "aacsbr.h"
+#include "aacsbrdata.h"
+
+#include <stdint.h>
+
+static VLC vlc_sbr[10];
+
+av_cold void ff_aac_sbr_init()
+{
+    static const struct {
+        const void *sbr_codes, *sbr_bits;
+        const unsigned int table_size, elem_size;
+    } sbr_tmp[] = {
+        { t_huffman_env_1_5dB_codes,       t_huffman_env_1_5dB_bits,
+            sizeof(t_huffman_env_1_5dB_codes),       sizeof(t_huffman_env_1_5dB_codes[0]) },
+        { f_huffman_env_1_5dB_codes,       f_huffman_env_1_5dB_bits,
+            sizeof(f_huffman_env_1_5dB_codes),       sizeof(f_huffman_env_1_5dB_codes[0]) },
+        { t_huffman_env_bal_1_5dB_codes,   t_huffman_env_bal_1_5dB_bits,
+            sizeof(t_huffman_env_bal_1_5dB_codes),   sizeof(t_huffman_env_bal_1_5dB_codes[0]) },
+        { f_huffman_env_bal_1_5dB_codes,   f_huffman_env_bal_1_5dB_bits,
+            sizeof(f_huffman_env_bal_1_5dB_codes),   sizeof(f_huffman_env_bal_1_5dB_codes[0]) },
+        { t_huffman_env_3_0dB_codes,       t_huffman_env_3_0dB_bits,
+            sizeof(t_huffman_env_3_0dB_codes),       sizeof(t_huffman_env_3_0dB_codes[0]) },
+        { f_huffman_env_3_0dB_codes,       f_huffman_env_3_0dB_bits,
+            sizeof(f_huffman_env_3_0dB_codes),       sizeof(f_huffman_env_3_0dB_codes[0]) },
+        { t_huffman_env_bal_3_0dB_codes,   t_huffman_env_bal_3_0dB_bits,
+            sizeof(t_huffman_env_bal_3_0dB_codes),   sizeof(t_huffman_env_bal_3_0dB_codes[0]) },
+        { f_huffman_env_bal_3_0dB_codes,   f_huffman_env_bal_3_0dB_bits,
+            sizeof(f_huffman_env_bal_3_0dB_codes),   sizeof(f_huffman_env_bal_3_0dB_codes[0]) },
+        { t_huffman_noise_3_0dB_codes,     t_huffman_noise_3_0dB_bits,
+            sizeof(t_huffman_noise_3_0dB_codes),     sizeof(t_huffman_noise_3_0dB_codes[0]) },
+        { t_huffman_noise_bal_3_0dB_codes, t_huffman_noise_bal_3_0dB_bits,
+            sizeof(t_huffman_noise_bal_3_0dB_codes), sizeof(t_huffman_noise_bal_3_0dB_codes[0]) },
+    };
+
+    // SBR VLC table initialization
+    SBR_INIT_VLC_STATIC(0, 1098);
+    SBR_INIT_VLC_STATIC(1, 1092);
+    SBR_INIT_VLC_STATIC(2, 768);
+    SBR_INIT_VLC_STATIC(3, 1026);
+    SBR_INIT_VLC_STATIC(4, 1058);
+    SBR_INIT_VLC_STATIC(5, 1052);
+    SBR_INIT_VLC_STATIC(6, 544);
+    SBR_INIT_VLC_STATIC(7, 544);
+    SBR_INIT_VLC_STATIC(8, 592);
+    SBR_INIT_VLC_STATIC(9, 512);
+}
+
+static unsigned int sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
+{
+    unsigned int cnt = get_bits_count(gb);
+    uint8_t bs_header_extra_1;
+    uint8_t bs_header_extra_2;
+
+    // Save last spectrum parameters variables to compare to new ones
+    memcpy(&sbr->spectrum_params[0], &sbr->spectrum_params[1], sizeof(SpectrumParameters));
+
+    sbr->bs_amp_res                        = get_bits1(gb);
+    sbr->spectrum_params[1].bs_start_freq  = get_bits(gb, 4);
+    sbr->spectrum_params[1].bs_stop_freq   = get_bits(gb, 4);
+    sbr->spectrum_params[1].bs_xover_band  = get_bits(gb, 3);
+                                             skip_bits(gb, 2); // bs_reserved
+
+    bs_header_extra_1 = get_bits1(gb);
+    bs_header_extra_2 = get_bits1(gb);
+
+    if (bs_header_extra_1) {
+        sbr->spectrum_params[1].bs_freq_scale  = get_bits(gb, 2);
+        sbr->spectrum_params[1].bs_alter_scale = get_bits1(gb);
+        sbr->spectrum_params[1].bs_noise_bands = get_bits(gb, 2);
+    }
+
+    // Check if spectrum parameters changed
+    if (memcmp(&sbr->spectrum_params[0], &sbr->spectrum_params[1], sizeof(SpectrumParameters)))
+        sbr->reset = 1;
+
+    if (bs_header_extra_2) {
+        sbr->bs_limiter_bands  = get_bits(gb, 2);
+        sbr->bs_limiter_gains  = get_bits(gb, 2);
+        sbr->bs_interpol_freq  = get_bits1(gb);
+        sbr->bs_smoothing_mode = get_bits1(gb);
+    }
+
+    return get_bits_count(gb) - cnt;
+}
+
+int array_min_int(int *array, int nel)
+{
+    int i, min = array[0];
+    for (i=1; i<nel; i++)
+        if (array[i] < min) min = array[i];
+    return min;
+}
+
+int qsort_compare(const int *a, const int *b)
+{
+    return *a - *b;
+}
+
+// Master Frequency Band Table (14496-3 sp04 p194)
+static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
+                             SpectrumParameters *spectrum)
+{
+    unsigned int temp;
+    unsigned int start_min, stop_min;
+    int k;
+    const uint8_t *sbr_offset_ptr;
+
+    if (ac->m4ac.ext_sample_rate < 32000) {
+        temp = 3000;
+    } else if (ac->m4ac.ext_sample_rate < 64000) {
+        temp = 4000;
+    } else
+        temp = 5000;
+
+    start_min = (unsigned int)lrintf((temp<<7) / (float)ac->m4ac.ext_sample_rate);
+    stop_min  = (unsigned int)lrintf((temp<<8) / (float)ac->m4ac.ext_sample_rate);
+
+    if (ac->m4ac.ext_sample_rate == 16000) {
+        sbr_offset_ptr = sbr_offset[0];
+    } else if (ac->m4ac.ext_sample_rate == 22050) {
+        sbr_offset_ptr = sbr_offset[1];
+    } else if (ac->m4ac.ext_sample_rate == 24000) {
+        sbr_offset_ptr = sbr_offset[2];
+    } else if (ac->m4ac.ext_sample_rate == 32000) {
+        sbr_offset_ptr = sbr_offset[3];
+    } else if ((ac->m4ac.ext_sample_rate >= 44100) &&
+            (ac->m4ac.ext_sample_rate <= 64000)) {
+        sbr_offset_ptr = sbr_offset[4];
+    } else if (ac->m4ac.ext_sample_rate > 64000) {
+        sbr_offset_ptr = sbr_offset[5];
+    } else {
+        av_log(ac->avccontext, AV_LOG_ERROR, "Unsupported sample rate for SBR: %d\n", ac->m4ac.ext_sample_rate);
+        return -1;
+    }
+
+    sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
+
+    if (spectrum->bs_stop_freq < 14) {
+        sbr->k[2] = stop_min;
+        for (k=0; k<spectrum->bs_stop_freq; k++) {
+            sbr->k[2] += lrintf(stop_min * powf(64.0f / (float)stop_min, (k+1) / 13.0f))  -
+                         lrintf(stop_min * powf(64.0f / (float)stop_min,  k    / 13.0f));
+        }
+    } else if (spectrum->bs_stop_freq == 14) {
+        sbr->k[2] = 2*sbr->k[0];
+    } else if (spectrum->bs_stop_freq == 15) {
+        sbr->k[2] = 3*sbr->k[0];
+    } else {
+        av_log(ac->avccontext, AV_LOG_ERROR, "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
+        return -1;
+    }
+    sbr->k[2] = FFMIN(64, sbr->k[2]);
+
+    if (spectrum->bs_freq_scale == 0) {
+        unsigned int dk;
+        int k2diff;
+
+        if (spectrum->bs_alter_scale == 0) {
+            dk = 1;
+            sbr->n_master = ((unsigned int)((sbr->k[2] - sbr->k[0]) / (float)(dk << 1))) << 1;
+        } else {
+            dk = 2;
+            sbr->n_master =          lrintf((sbr->k[2] - sbr->k[0]) / (float)(dk << 1))  << 1;
+        }
+
+        for (k=1; k<=sbr->n_master; k++)
+            sbr->f_master[k] = dk;
+
+        k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
+        if (k2diff) {
+            int incr;
+            if (k2diff < 0) {
+                incr = 1;
+                k = 1;
+            } else {
+                incr = -1;
+                k = sbr->n_master;
+            }
+
+            while (k2diff != 0) {
+                sbr->f_master[k] -= incr;
+                k                += incr;
+                k2diff           += incr;
+            }
+        }
+
+        sbr->f_master[0] = sbr->k[0];
+        for (k=1; k<=sbr->n_master; k++)
+            sbr->f_master[k] += sbr->f_master[k-1];
+
+        // Requirements (14496-3 sp04 p205)
+        if (sbr->n_master <= 0) {
+            av_log(ac->avccontext, AV_LOG_ERROR, "Invalid n_master: %d\n", sbr->n_master);
+            return -1;
+        }
+    } else {
+        unsigned int bands = 14 - (spectrum->bs_freq_scale<<1); // bs_freq_scale = {1,2,3}
+        float warp = spectrum->bs_alter_scale ? 1.3 : 1.0; // bs_alter_scale = {0,1}
+        unsigned int two_regions, num_bands_0;
+        int vdk0_max, vdk1_min;
+        int *vk0;
+
+        if (sbr->k[2]/(float)sbr->k[0] > 2.2449) {
+            two_regions = 1;
+            sbr->k[1] = sbr->k[0] << 1;
+        } else {
+            two_regions = 0;
+            sbr->k[1] = sbr->k[2];
+        }
+
+        num_bands_0 = lrintf(bands * logf(sbr->k[1] / (float)sbr->k[0]) / (2.0f * logf(2.0f))) << 1;
+
+        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);
+            return -1;
+        }
+
+        vk0 = av_malloc((num_bands_0 + 1) * sizeof(int));
+        vk0[0] = 0;
+
+        for (k=0; k<num_bands_0; k++) {
+            vk0[k+1] = lrintf(sbr->k[0] * powf(sbr->k[1] / (float)sbr->k[0], (k+1) / (float)num_bands_0)) -
+                       lrintf(sbr->k[0] * powf(sbr->k[1] / (float)sbr->k[0],  k    / (float)num_bands_0));
+        }
+
+        qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_compare);
+        vdk0_max = vk0[num_bands_0];
+
+        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]);
+                return -1;
+            }
+            vk0[k] += vk0[k-1];
+        }
+
+        if (two_regions) {
+            int *vk1;
+            unsigned int num_bands_1
+                = lrintf(bands * logf(sbr->k[2] / (float)sbr->k[1]) / (2.0f * logf(2.0f) * warp)) << 1;
+
+            vk1 = av_malloc((num_bands_1 + 1) * sizeof(int));
+
+            for (k=0; k<num_bands_1; k++) {
+                vk1[k+1] = lrintf(sbr->k[1] * powf(sbr->k[2] / (float)sbr->k[1], (k+1) / (float)num_bands_1)) -
+                           lrintf(sbr->k[1] * powf(sbr->k[2] / (float)sbr->k[1],  k    / (float)num_bands_1));
+            }
+
+            vdk1_min = array_min_int(vk1 + 1, num_bands_1);
+
+            if (vdk1_min < vdk0_max) {
+                int change;
+                qsort(vk1+1, num_bands_1, sizeof(vk1[1]), qsort_compare);
+                change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
+                vk1[1]           += change;
+                vk1[num_bands_1] -= change;
+            }
+
+            qsort(vk1+1, num_bands_1, sizeof(vk1[1]), qsort_compare);
+
+            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]);
+                    return -1;
+                }
+                vk1[k] += vk1[k-1];
+            }
+
+            sbr->n_master = num_bands_0 + num_bands_1;
+            memcpy(&sbr->f_master[0],               vk0,   (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
+            memcpy(&sbr->f_master[num_bands_0 + 1], vk1+1,  num_bands_1      * sizeof(sbr->f_master[0]));
+
+            av_free(vk1);
+        } else {
+            sbr->n_master = num_bands_0;
+            memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
+        }
+        av_free(vk0);
+    }
+    // Requirements (14496-3 sp04 p205)
+    if (sbr->spectrum_params[1].bs_xover_band >= sbr->n_master) {
+        av_log(ac->avccontext, AV_LOG_ERROR,
+                "Invalid bitstream, crossover band index beyond array bounds: %d\n",
+                sbr->spectrum_params[1].bs_xover_band);
+        return -1;
+    }
+    // temp == max number of QMF subbands
+    if (ac->m4ac.ext_sample_rate <= 32000) {
+        temp = 48;
+    } else if (ac->m4ac.ext_sample_rate == 44100) {
+        temp = 35;
+    } else if (ac->m4ac.ext_sample_rate >= 48000)
+        temp = 32;
+
+    if (sbr->k[2] - sbr->k[0] > temp) {
+        av_log(ac->avccontext, AV_LOG_ERROR, "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
+        return -1;
+    }
+
+    return 0;
+}
+
+// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
+static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
+{
+    int i, k, sb = 0;
+    int msb = sbr->k[0];
+    int usb = sbr->k[3];
+    int goal_sb = lrintf((1<<11)*1000/(float)ac->m4ac.sample_rate);
+
+    sbr->num_patches = 0;
+
+    if (goal_sb < sbr->k[3] + sbr->m) {
+        for (k=0; sbr->f_master[k] < goal_sb; k++);
+        k++;
+    } else
+        k = sbr->n_master;
+
+    do {
+        int odd = 0;
+        for (i=k; sb<=(sbr->k[0] - 1 + msb - odd); i--) {
+            sb = sbr->f_master[i];
+            odd = (sb - 2 + sbr->k[0])&1;
+        }
+
+        sbr->patch_num_subbands[sbr->num_patches]  = FFMAX(sb - usb, 0);
+        sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
+
+        if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
+            usb = sb;
+            msb = sb;
+            sbr->num_patches++;
+        } else
+            msb = sbr->k[3];
+
+        if (sbr->f_master[k] - sb < 3)
+            k = sbr->n_master;
+    } while (sb != sbr->k[3] + sbr->m);
+
+    if ((sbr->patch_num_subbands[sbr->num_patches-1] < 3) && (sbr->num_patches > 1))
+        sbr->num_patches--;
+
+    if (sbr->num_patches > 5) { // Requirements (14496-3 sp04 p205)
+        av_log(ac->avccontext, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
+        return -1;
+    }
+
+    return 0;
+}
+
+static inline void remove_table_element(void *table, uint8_t *nel, int el_size,
+                                        int el)
+{
+    memmove((uint8_t *)table + el_size*el, (uint8_t *)table + el_size*(el + 1), (*nel - el - 1)*el_size);
+    (*nel)--;
+}
+
+static inline int in_table(void *table, int nel, int el_size, void *needle)
+{
+    int i;
+    uint8_t *table_ptr = table; // avoids a warning with void * ptr arith
+    for (i=0; i<nel; i++, table_ptr += el_size)
+        if (!memcmp(table_ptr, needle, el_size))
+            return 1;
+    return 0;
+}
+
+// Derived Frequency Band Tables (14496-3 sp04 p197)
+static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
+{
+    int k, temp;
+
+    sbr->n[1] = sbr->n_master - sbr->spectrum_params[1].bs_xover_band;
+    sbr->n[0] = (sbr->n[1] + 1) >> 1;
+
+    memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params[1].bs_xover_band],
+            (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
+    sbr->m    = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
+    sbr->k[3] = sbr->f_tablehigh[0];
+
+    // Requirements (14496-3 sp04 p205)
+    if (sbr->k[3] + sbr->m > 64) {
+        av_log(ac->avccontext, AV_LOG_ERROR, "Stop frequency border too high: %d\n", sbr->k[3] + sbr->m);
+        return -1;
+    }
+    if (sbr->k[3] > 32) {
+        av_log(ac->avccontext, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->k[3]);
+        return -1;
+    }
+
+    sbr->f_tablelow[0] = sbr->f_tablehigh[0];
+    temp = (1 - (sbr->n[1] & 1 ? -1 : 1)) >> 1;
+    for (k=1; k<=sbr->n[0]; k++)
+        sbr->f_tablelow[k] = sbr->f_tablehigh[(k << 1) - temp];
+
+    sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params[1].bs_noise_bands * logf(sbr->k[2] / (float)sbr->k[3]) / logf(2.0f))); // 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);
+        return -1;
+    }
+
+    sbr->f_tablenoise[0] = sbr->f_tablelow[0];
+    temp = 0;
+    for (k=1; k<=sbr->n_q; k++) {
+        temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
+        sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
+    }
+
+    sbr_hf_calc_npatches(ac, sbr);
+
+    // Limiter Frequency Band Table (14496-3 sp04 p198)
+    if (sbr->bs_limiter_bands > 0) {
+        const float lim_bands_per_octave[3] = {1.2, 2, 3};
+        int patch_borders[sbr->num_patches + 1];
+
+        patch_borders[0] = sbr->k[3];
+        for (k=1; k<=sbr->num_patches; k++)
+            patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
+
+        memcpy( sbr->f_tablelim,                  sbr->f_tablelow, (sbr->n[0]        + 1) * sizeof(sbr->f_tablelow[0]));
+        memcpy(&sbr->f_tablelim[sbr->n[0] + 1], &patch_borders[1], (sbr->num_patches - 1) * sizeof(patch_borders[0]));
+
+        qsort(sbr->f_tablelim, sbr->num_patches + sbr->n[0], sizeof(sbr->f_tablelim[0]), qsort_compare);
+
+        k = 1;
+        sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
+        while (k <= sbr->n_lim) {
+            // if ( nOctaves * limBands >= 0.49) ...
+            if (log2(sbr->f_tablelim[k] / sbr->f_tablelim[k-1]) * lim_bands_per_octave[sbr->bs_limiter_bands - 1] >= 0.49) {
+                k++;
+                continue;
+            }
+            if (sbr->f_tablelim[k] == sbr->f_tablelim[k-1] ||
+                    !in_table(patch_borders, sbr->num_patches, sizeof(patch_borders[0]), &sbr->f_tablelim[k]))
+                remove_table_element(sbr->f_tablelim, &sbr->n_lim, sizeof(sbr->f_tablelim[0]), k);
+            else if (!in_table(patch_borders, sbr->num_patches, sizeof(patch_borders[0]), &sbr->f_tablelim[k-1]))
+                remove_table_element(sbr->f_tablelim, &sbr->n_lim, sizeof(sbr->f_tablelim[0]), k-1);
+            else
+                k++;
+        };
+    } else {
+        sbr->f_tablelim[0] = sbr->f_tablelow[0];
+        sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
+        sbr->n_lim = 1;
+    }
+
+    return 0;
+}
+
+static int sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
+                    GetBitContext *gb, SBRData *ch_data)
+{
+    int i;
+
+    ch_data->bs_num_env[0] = ch_data->bs_num_env[1];
+
+    switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
+    case FIXFIX:
+        ch_data->bs_num_env[1] = 1 << get_bits(gb, 2);
+        if (ch_data->bs_num_env[1] == 1)
+            sbr->bs_amp_res = 0;
+
+        ch_data->bs_freq_res[0] = get_bits1(gb);
+        for (i=1; i<ch_data->bs_num_env[1]; i++)
+            ch_data->bs_freq_res[i] = ch_data->bs_freq_res[0];
+        break;
+    case FIXVAR:
+        ch_data->bs_var_bord[1] = get_bits(gb, 2);
+        ch_data->bs_num_rel[1]  = get_bits(gb, 2);
+        ch_data->bs_num_env[1] = ch_data->bs_num_rel[1] + 1;
+
+        for (i=0; i<ch_data->bs_num_rel[1]; i++)
+            ch_data->bs_rel_bord[1][i] = (get_bits(gb, 2) << 1) + 2;
+
+        ch_data->bs_pointer = get_bits(gb, ceil(logf(ch_data->bs_num_env[1] + 1) / M_LN2));
+
+        for (i=0; i<ch_data->bs_num_env[1]; i++)
+            ch_data->bs_freq_res[ch_data->bs_num_env[1] - 1 - i] = get_bits1(gb);
+        break;
+    case VARFIX:
+        ch_data->bs_var_bord[0] = get_bits(gb, 2);
+        ch_data->bs_num_rel[0]  = get_bits(gb, 2);
+        ch_data->bs_num_env[1] = ch_data->bs_num_rel[0] + 1;
+
+        for (i=0; i<ch_data->bs_num_rel[0]; i++)
+            ch_data->bs_rel_bord[0][i] = (get_bits(gb, 2) << 1) + 2;
+
+        ch_data->bs_pointer = get_bits(gb, ceil(logf(ch_data->bs_num_env[1] + 1) / M_LN2));
+
+        for (i=0; i<ch_data->bs_num_env[1]; i++)
+            ch_data->bs_freq_res[i] = get_bits1(gb);
+        break;
+    case VARVAR:
+        ch_data->bs_var_bord[0] = get_bits(gb, 2);
+        ch_data->bs_var_bord[1] = get_bits(gb, 2);
+        ch_data->bs_num_rel[0]  = get_bits(gb, 2);
+        ch_data->bs_num_rel[1]  = get_bits(gb, 2);
+        ch_data->bs_num_env[1] = ch_data->bs_num_rel[0] + ch_data->bs_num_rel[1] + 1;
+
+        for (i=0; i<ch_data->bs_num_rel[0]; i++)
+            ch_data->bs_rel_bord[0][i] = (get_bits(gb, 2) << 1) + 2;
+        for (i=0; i<ch_data->bs_num_rel[1]; i++)
+            ch_data->bs_rel_bord[1][i] = (get_bits(gb, 2) << 1) + 2;
+
+        ch_data->bs_pointer = get_bits(gb, ceil(logf(ch_data->bs_num_env[1] + 1) / M_LN2));
+
+        for (i=0; i<ch_data->bs_num_env[1]; i++)
+            ch_data->bs_freq_res[i] = get_bits1(gb);
+        break;
+    default:
+        break;
+    }
+
+    if (ch_data->bs_frame_class == FIXFIX && ch_data->bs_num_env[1] > 4) {
+        av_log(ac->avccontext, AV_LOG_ERROR,
+                "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n", ch_data->bs_num_env[1]);
+        return -1;
+    }
+    if (ch_data->bs_frame_class == VARVAR && ch_data->bs_num_env[1] > 5) {
+        av_log(ac->avccontext, AV_LOG_ERROR,
+                "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n", ch_data->bs_num_env[1]);
+        return -1;
+    }
+
+    ch_data->bs_num_noise = ch_data->bs_num_env[1] > 1 ? 2 : 1;
+
+    return 0;
+}
+
+static void sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
+                     SBRData *ch_data)
+{
+    int i;
+
+    for (i=0; i<ch_data->bs_num_env[1]; i++)
+        ch_data->bs_df_env[i] = get_bits1(gb);
+    for (i=0; i<ch_data->bs_num_noise; i++)
+        ch_data->bs_df_noise[i] = get_bits1(gb);
+}
+
+static void sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
+                     SBRData *ch_data)
+{
+    int i;
+
+    memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
+    for (i=0; i<sbr->n_q; i++)
+        ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
+}
+
+static void sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb,
+                         SBRData *ch_data, int ch)
+{
+    int bits, max_depth;
+    int i, j;
+    VLC_TYPE (*t_huff)[2], (*f_huff)[2];
+
+    if (sbr->bs_coupling && ch) {
+        max_depth = 2;
+        if (sbr->bs_amp_res) {
+            bits = 5;
+            t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
+            f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
+        } else {
+            bits = 6;
+            t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
+            f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
+        }
+    } else {
+        max_depth = 3;
+        if (sbr->bs_amp_res) {
+            bits = 6;
+            t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
+            f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
+        } else {
+            bits = 7;
+            t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
+            f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
+        }
+    }
+
+    for (i=0; i<ch_data->bs_num_env[1]; i++) {
+        if (!ch_data->bs_df_env[i]) {
+            ch_data->bs_data_env[i][0] = get_bits(gb, bits); // bs_env_start_value_balance
+            for (j=1; j<sbr->n[ch_data->bs_freq_res[i]]; j++)
+                ch_data->bs_data_env[i][j] = get_vlc2(gb, f_huff, 9, max_depth);
+        } else {
+            for (j=0; j<sbr->n[ch_data->bs_freq_res[i]]; j++)
+                ch_data->bs_data_env[i][j] = get_vlc2(gb, t_huff, 9, max_depth);
+        }
+    }
+}
+
+static void sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb,
+                      SBRData *ch_data, int ch)
+{
+    int max_depth;
+    int i, j;
+    VLC_TYPE (*t_huff)[2], (*f_huff)[2];
+
+    if (sbr->bs_coupling && ch) {
+        max_depth = 1;
+        t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
+        f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
+    } else {
+        max_depth = 2;
+        t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
+        f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
+    }
+
+    for (i = 0; i < ch_data->bs_num_noise; i++) {
+        if (!ch_data->bs_df_noise[i]) {
+            if (sbr->bs_coupling && ch)
+                ch_data->bs_data_noise[i][0] = get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
+            for (j=1; j<sbr->n_q; j++)
+                ch_data->bs_data_noise[i][j] = get_vlc2(gb, f_huff, 9, max_depth + 1);
+        } else {
+            for (j=0; j<sbr->n_q; j++)
+                ch_data->bs_data_noise[i][j] = get_vlc2(gb, t_huff, 9, max_depth);
+        }
+    }
+}
+
+static void sbr_sinusoidal_coding(SpectralBandReplication *sbr,
+                                  GetBitContext *gb, SBRData *ch_data)
+{
+    int i;
+    for (i=0; i<sbr->n[1]; i++)
+        ch_data->bs_add_harmonic[i] = get_bits1(gb);
+}
+
+static void sbr_extension(SpectralBandReplication *sbr, GetBitContext *gb,
+                          int bs_extension_id, int *num_bits_left)
+{
+/* FIXME - implement ps_data for parametric stereo parsing
+    switch (bs_extension_id) {
+    case EXTENSION_ID_PS:
+        num_bits_left -= ps_data(sbr, gb);
+        break;
+    default:
+*/
+        skip_bits(gb, *num_bits_left); // bs_fill_bits
+        *num_bits_left = 0;
+/*
+        break;
+    }
+*/
+}
+
+static void sbr_single_channel_element(AACContext *ac,
+                                       SpectralBandReplication *sbr,
+                                       GetBitContext *gb)
+{
+    if (get_bits1(gb)) // bs_data_extra
+        skip_bits(gb, 4); // bs_reserved
+
+    sbr_grid(ac, sbr, gb, &sbr->data[0]);
+    sbr_dtdf(sbr, gb, &sbr->data[0]);
+    sbr_invf(sbr, gb, &sbr->data[0]);
+    sbr_envelope(sbr, gb, &sbr->data[0], 0);
+    sbr_noise(sbr, gb, &sbr->data[0], 0);
+
+    if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
+        sbr_sinusoidal_coding(sbr, gb, &sbr->data[0]);
+}
+
+static void sbr_channel_pair_element(AACContext *ac,
+                                     SpectralBandReplication *sbr,
+                                     GetBitContext *gb)
+{
+    if (get_bits1(gb)) // bs_data_extra
+        skip_bits(gb, 8); // bs_reserved
+
+    if ((sbr->bs_coupling = get_bits1(gb))) {
+        sbr_grid(ac, sbr, gb, &sbr->data[0]);
+        sbr_dtdf(sbr, gb, &sbr->data[0]);
+        sbr_dtdf(sbr, gb, &sbr->data[1]);
+        sbr_invf(sbr, gb, &sbr->data[0]);
+        sbr_envelope(sbr, gb, &sbr->data[0], 0);
+        sbr_noise(sbr, gb, &sbr->data[0], 0);
+        sbr_envelope(sbr, gb, &sbr->data[1], 1);
+        sbr_noise(sbr, gb, &sbr->data[1], 1);
+    } else {
+        sbr_grid(ac, sbr, gb, &sbr->data[0]);
+        sbr_grid(ac, sbr, gb, &sbr->data[1]);
+        sbr_dtdf(sbr, gb, &sbr->data[0]);
+        sbr_dtdf(sbr, gb, &sbr->data[1]);
+        sbr_invf(sbr, gb, &sbr->data[0]);
+        sbr_invf(sbr, gb, &sbr->data[1]);
+        sbr_envelope(sbr, gb, &sbr->data[0], 0);
+        sbr_envelope(sbr, gb, &sbr->data[1], 1);
+        sbr_noise(sbr, gb, &sbr->data[0], 0);
+        sbr_noise(sbr, gb, &sbr->data[1], 1);
+    }
+
+    if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
+        sbr_sinusoidal_coding(sbr, gb, &sbr->data[0]);
+    if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
+        sbr_sinusoidal_coding(sbr, gb, &sbr->data[1]);
+}
+
+static unsigned int sbr_data(AACContext *ac, SpectralBandReplication *sbr,
+                             GetBitContext *gb, int id_aac)
+{
+    unsigned int cnt = get_bits_count(gb);
+
+    if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
+        sbr_single_channel_element(ac, sbr, gb);
+    } else if (id_aac == TYPE_CPE) {
+        sbr_channel_pair_element(ac, sbr, gb);
+    } else {
+        av_log(ac->avccontext, AV_LOG_ERROR,
+            "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
+        return -1;
+    }
+    if (get_bits1(gb)) { // bs_extended_data
+        int num_bits_left = get_bits(gb, 4); // bs_extension_size
+        if (num_bits_left == 15)
+            num_bits_left += get_bits(gb, 8); // bs_esc_count
+
+        num_bits_left <<= 3;
+        while (num_bits_left > 7) {
+            num_bits_left -= 2;
+            sbr_extension(sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
+        }
+    }
+
+    return get_bits_count(gb) - cnt;
+}
+
+static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
+{
+    sbr_make_f_master(ac, sbr, sbr->spectrum_params);
+    sbr_make_f_derived(ac, sbr);
+    sbr->reset = 0;
+}
+
+/**
+ * Decode Spectral Band Replication extension data; reference: table 4.55.
+ *
+ * @param   crc flag indicating the presence of CRC checksum
+ * @param   cnt length of TYPE_FIL syntactic element in bytes
+ *
+ * @return  Returns number of bytes consumed from the TYPE_FIL element.
+ */
+int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
+                                GetBitContext *gb, int crc, int cnt, int id_aac)
+{
+    unsigned int num_sbr_bits = 0, num_align_bits;
+
+    if (crc) {
+        skip_bits(gb, 10); // bs_sbr_crc_bits; FIXME - implement CRC check
+        num_sbr_bits += 10;
+    }
+
+    num_sbr_bits++;
+    if (get_bits1(gb)) // bs_header_flag
+        num_sbr_bits += sbr_header(sbr, gb);
+
+    if (sbr->reset)
+        sbr_reset(ac, sbr);
+
+    num_sbr_bits  += sbr_data(ac, sbr, gb, id_aac);
+    num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
+
+    skip_bits(gb, num_align_bits); // bs_fill_bits
+
+    return (num_sbr_bits + num_align_bits + 4) >> 3;
+}
+
+// Time/frequency Grid (14496-3 sp04 p200)
+static int sbr_time_freq_grid(AACContext *ac, SpectralBandReplication *sbr,
+                              SBRData *ch_data, int ch)
+{
+    unsigned int abs_bord_lead  =  ch_data->bs_frame_class >= 2 ? ch_data->bs_var_bord[0] : 0;
+    // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
+    unsigned int abs_bord_trail = (ch_data->bs_frame_class  & 1 ? ch_data->bs_var_bord[1] : 0) + 16;
+    unsigned int n_rel_lead, n_rel_trail;
+    int i;
+
+    if (ch_data->bs_frame_class == FIXFIX) {
+        n_rel_lead = ch_data->bs_num_env[1] - 1;
+    } else if (ch_data->bs_frame_class == FIXVAR) {
+        n_rel_lead = 0;
+    } else if (ch_data->bs_frame_class < 4) { // VARFIX or VARVAR
+        n_rel_lead = ch_data->bs_num_rel[0];
+    } else {
+        av_log(ac->avccontext, AV_LOG_ERROR, "Invalid bs_frame_class for SBR: %d\n", ch_data->bs_frame_class);
+        return -1;
+    }
+
+    n_rel_trail = ch_data->bs_frame_class & 1 ? ch_data->bs_num_rel[1] : 0;
+
+    sbr->t_env[ch][0]                      = abs_bord_lead;
+    sbr->t_env[ch][ch_data->bs_num_env[1]] = abs_bord_trail;
+
+    if (ch_data->bs_frame_class == FIXFIX) {
+        unsigned int temp = (unsigned int)lrintf(abs_bord_trail / (float)ch_data->bs_num_env[1]);
+        for (i=0; i<n_rel_lead; i++)
+            sbr->t_env[ch][i+1] = sbr->t_env[ch][i] + temp;
+    } else if (ch_data->bs_frame_class > 1) { // VARFIX or VARVAR
+        for (i=0; i<n_rel_lead; i++)
+            sbr->t_env[ch][i+1] = sbr->t_env[ch][i] + ch_data->bs_rel_bord[0][i];
+    } else { // FIXVAR
+        for (i=0; i<n_rel_lead; i++)
+            sbr->t_env[ch][i+1] = abs_bord_lead;
+    }
+
+    if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
+        for (i=ch_data->bs_num_env[1]-1; i>n_rel_lead; i--)
+            sbr->t_env[ch][i] = sbr->t_env[ch][i+1] - ch_data->bs_rel_bord[1][ch_data->bs_num_env[1] - 1 - i];
+    } else { // FIXFIX or VARFIX
+        for (i=n_rel_lead; i<ch_data->bs_num_env[1]; i++)
+            sbr->t_env[ch][i+1] = abs_bord_trail;
+    }
+
+    sbr->t_q[ch][0] = sbr->t_env[ch][0];
+    if (ch_data->bs_num_noise > 1) { // typo in spec bases this on bs_num_env...
+        unsigned int idx;
+        if (ch_data->bs_frame_class == FIXFIX) {
+            idx = ch_data->bs_num_env[1] >> 1;
+        } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
+            idx = ch_data->bs_num_env[1] - FFMAX(ch_data->bs_pointer - 1, 1);
+        } else { // VARFIX
+            if (ch_data->bs_pointer == 0)
+                idx = 1;
+            else if (ch_data->bs_pointer == 1)
+                idx = ch_data->bs_num_env[1] - 1;
+            else // bs_pointer > 1
+                idx = ch_data->bs_pointer - 1;
+        }
+        sbr->t_q[ch][1] = sbr->t_env[ch][idx];
+        sbr->t_q[ch][2] = sbr->t_env[ch][ch_data->bs_num_env[1]];
+    } else
+        sbr->t_q[ch][1] = sbr->t_env[ch][ch_data->bs_num_env[1]];
+}
+
+// SBR Envelope and Noise Floor Decoding (14496-3 sp04 p201)
+static void sbr_env_noise_floors(SpectralBandReplication *sbr, SBRData *ch_data,
+                                 int ch)
+{
+    int delta = (ch == 1 && sbr->bs_coupling == 1) ? 2 : 1;
+    int i, k, l;
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        if (ch_data->bs_df_env[l]) {
+            // bs_freq_res[0] == bs_freq_res[bs_num_env[1]] from prev frame
+            if (ch_data->bs_freq_res[l+1] == ch_data->bs_freq_res[l]) {
+                for (k=0; k<sbr->n[ch_data->bs_freq_res[l+1]]; k++)
+                    sbr->env_facs[ch][l+1][k] = sbr->env_facs[ch][l][k] + delta * ch_data->bs_data_env[l][k];
+            } else if (ch_data->bs_freq_res[l+1]) {
+                i = 0; // optimisation : f_* are ascending freq bands so start at last i for each search
+                for (k=0; k<sbr->n[ch_data->bs_freq_res[l+1]]; k++) {
+                    // find i such that f_tablelow[i] <= f_tablehigh[k] < f_tablelow[i+1]
+                    for (; i<sbr->n[0]; i++)
+                        if (sbr->f_tablelow[i]   <= sbr->f_tablehigh[k] &&
+                           sbr->f_tablelow[i+1] >  sbr->f_tablehigh[k])
+                            break;
+                    sbr->env_facs[ch][l+1][k] = sbr->env_facs[ch][i][k] + delta * ch_data->bs_data_env[l][k];
+                }
+            } else {
+                i = 0; // optimisation : f_* are ascending freq bands so start at last i for each search
+                for (k=0; k<sbr->n[ch_data->bs_freq_res[l+1]]; k++) {
+                    // find i such that f_tablehigh[i] == f_tablelow[k]
+                    for (; i<sbr->n[1]; i++)
+                        if (sbr->f_tablehigh[i] == sbr->f_tablelow[k])
+                            break;
+                    sbr->env_facs[ch][l+1][k] = sbr->env_facs[ch][i][k] + delta * ch_data->bs_data_env[l][k];
+                }
+            }
+        } else {
+            for (k=0; k<sbr->n[ch_data->bs_freq_res[l+1]]; k++) {
+                sbr->env_facs[ch][l+1][k] = ch_data->bs_data_env[l][0];
+                for (i=1; i<=k; i++)
+                    sbr->env_facs[ch][l+1][k] += ch_data->bs_data_env[l][i];
+                sbr->env_facs[ch][l+1][k] *= delta;
+            }
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_noise; l++) {
+        if (ch_data->bs_df_noise[l])
+            for (k=0; k<sbr->n_q; k++)
+                sbr->noise_facs[ch][l+1][k] = sbr->noise_facs[ch][l][k] + delta * ch_data->bs_data_noise[l][k];
+        else {
+            for (k=0; k<sbr->n_q; k++) {
+                sbr->noise_facs[ch][l+1][k] = ch_data->bs_data_noise[l][0];
+                for (i=1; i<=k; i++)
+                    sbr->noise_facs[ch][l+1][k] += ch_data->bs_data_noise[l][i];
+                sbr->noise_facs[ch][l+1][k] *= delta;
+            }
+        }
+    }
+    //FIXME : assign 0th elements of (env|noise)_facs from last elements
+}
+
+// Dequantisation and stereo decoding (14496-3 sp04 p203)
+static void sbr_dequant(SpectralBandReplication *sbr, int id_aac, int ch)
+{
+    int k, l;
+    float alpha = sbr->bs_amp_res ? 1.0f : 0.5f;
+
+    if (id_aac == TYPE_CCE && sbr->bs_coupling) {
+        float pan_offset = sbr->bs_amp_res ? 12.0f : 24.0f;
+        for (l=1; l<=sbr->data[ch].bs_num_env[1]; l++) {
+            for (k=0; k<sbr->n[sbr->data[ch].bs_freq_res[l+1]]; k++) {
+                float temp1 = powf(2.0f, sbr->env_facs[0][l][k] * alpha + 7.0f);
+                float temp2 = (pan_offset - sbr->env_facs[1][l][k]) * alpha;
+                sbr->env_facs[0][l][k] = temp1 / (1.0f + powf(2.0f,  temp2));
+                sbr->env_facs[1][l][k] = temp1 / (1.0f + powf(2.0f, -temp2));
+            }
+        }
+        for (l=1; l<=sbr->data[ch].bs_num_noise; l++) {
+            for (k=0; k<sbr->n_q; k++) {
+                float temp1 = powf(2.0f, NOISE_FLOOR_OFFSET - sbr->noise_facs[0][l][k] + 1);
+                float temp2 = pan_offset - sbr->noise_facs[0][l][k];
+                sbr->noise_facs[0][l][k] = temp1 / (1.0f + powf(2.0f,  temp2));
+                sbr->noise_facs[1][l][k] = temp1 / (1.0f + powf(2.0f, -temp2));
+            }
+        }
+    } else { // SCE or one non-coupled CPE
+        for (l=1; l<=sbr->data[ch].bs_num_env[1]; l++)
+            for (k=0; k<sbr->n[sbr->data[ch].bs_freq_res[l+1]]; k++)
+                sbr->env_facs[ch][l][k] = powf(2.0f, alpha * sbr->env_facs[ch][l][k] + 6.0f);
+        for (l=1; l<=sbr->data[ch].bs_num_noise; l++)
+            for (k=0; k<sbr->n_q; k++)
+                sbr->noise_facs[ch][l][k] = powf(2.0f, NOISE_FLOOR_OFFSET - sbr->noise_facs[ch][l][k]);
+    }
+}
+
+  /**
+ * Analysis QMF Bank (14496-3 sp04 p206)
+ *
+ * @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(const float *x, float W[32][32][2])
+{
+    int i, k, l, n;
+    x += 319;
+    for (l=0; l<32; l++) { // 32 = numTimeSlots*RATE = 16*2 as 960 sample frames are not supported
+        float z[320], u[64];
+        for (i=0; i<320; i++)
+            z[i] = x[-i] * sbr_qmf_window[i<<1];
+        for (i=0; i<64; i++)
+            u[i] = z[i] + z[i + 64] + z[i + 128] + z[i + 192] + z[i + 256];
+        for (k=0; k<32; k++) {
+            float temp1 = u[0] * 2.0f;
+            float temp2 = -(k + 0.5f) * M_PI / 128.0f;
+            W[k][l][0] = temp1 * cosf(temp2);
+            W[k][l][1] = temp1 * sinf(temp2);
+            for (n=1; n<64; n++) {
+                temp1 = u[n] * 2.0f;
+                temp2 = (n - 0.25f) * (k + 0.5f) * M_PI / 32.0f;
+                W[k][l][0] += temp1 * cosf(temp2);
+                W[k][l][1] += temp1 * sinf(temp2);
+            }
+        }
+        x += 32;
+    }
+}
+
+// Synthesis QMF Bank (14496-3 sp04 p206)
+// Downsampled Synthesis QMF Bank (14496-3 sp04 p206)
+static void sbr_qmf_synthesis(float *out, const float **X,
+                              const unsigned int div)
+{
+    int k, l, n;
+    float v[1280], w[640];
+    for (l=0; l<32; l++) {
+        memmove(&v[128/div], v, (1280-128)/div * sizeof(float));
+        for (n=0; n<128/div; n++) {
+            v[n] = X[0][l] * cosf((2.0f * n - 255.0f/div) * M_PI / (256.0f/div));
+            for (k=1; k<64/div; k++) {
+                v[n] += X[k][l] * cosf((k + 0.5f) * (2.0f * n - 255.0f/div) * M_PI / (128.0f/div));
+            }
+            v[n] /= 64.0f * 64/div;
+        }
+        for (n=0; n<=4; n++) {
+            int temp1 = 128/div * n, temp2 = temp1 << 1;
+            for (k=0; k<64/div; k++) {
+                w[temp1 + k]          = v[temp2 + k]           * sbr_qmf_window[temp1 + k];
+                w[temp1 + k + 64/div] = v[temp2 + k + 192/div] * sbr_qmf_window[temp1 + k + 64/div];
+            }
+        }
+        for (k=0; k<64/div; k++) {
+            out[k] = w[k]           + w[64/div + k]  + w[128/div + k] + w[192/div + k] + w[256/div + k]
+                   + w[320/div + k] + w[384/div + k] + w[448/div + k] + w[512/div + k] + w[576/div + k];
+        }
+        out += 64/div;
+    }
+}
+
+// High Frequency Generation (14496-3 sp04 p214+)
+
+// Inverse Filtering (14496-3 sp04 p214)
+static void sbr_hf_inverse_filter(float **alpha0, float **alpha1,
+                                  const float ***x_low, int k0)
+{
+    int i, j, k, n;
+    for (k=0; k<k0; k++) {
+        float mod_var_sq;
+        float phi[3][2][2], dk[2];
+
+        for (i=0; i<3; i++) {
+            for (j=0; j<2; j++) {
+                unsigned int idxtmp1 = T_HFADJ - i;
+                unsigned int idxtmp2 = T_HFADJ - j;
+
+                phi[i][j][0] = 0.0f;
+                phi[i][j][1] = 0.0f;
+
+                for (n=0; n<16 * 2 + 6; n++) {
+                    unsigned int idx1 = n + idxtmp1;
+                    unsigned int idx2 = n + idxtmp2;
+                    phi[i][j][0] += x_low[k][idx1][0] * x_low[k][idx2][0] +
+                                    x_low[k][idx1][1] * x_low[k][idx2][1];
+                    if (i!=j+1) { // imaginary part
+                        phi[i][j][1] += x_low[k][idx1][1] * x_low[k][idx2][0] -
+                                        x_low[k][idx1][0] * x_low[k][idx2][1];
+                    }
+                }
+            }
+        }
+
+        dk[0] = phi[2][1][0] * phi[1][0][0] - phi[2][1][1] * phi[1][0][1] -
+               (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
+        dk[1] = phi[2][1][0] * phi[1][0][1] + phi[2][1][1] * phi[1][0][0];
+
+        mod_var_sq = dk[0] * dk[0] + dk[1] * dk[1];
+        if (mod_var_sq == 0) {
+            alpha1[k][0] = 0;
+            alpha1[k][1] = 0;
+        } else {
+            float temp_real, temp_im;
+            temp_real = phi[0][0][0] * phi[1][1][0] -
+                        phi[0][0][1] * phi[1][1][1] -
+                        phi[0][1][0] * phi[1][0][0] +
+                        phi[0][1][1] * phi[1][0][1];
+            temp_im   = phi[0][0][0] * phi[1][1][1] +
+                        phi[0][0][1] * phi[1][1][0] -
+                        phi[0][1][0] * phi[1][0][1] -
+                        phi[0][1][1] * phi[1][0][0];
+
+            alpha1[k][0] = (dk[0] * temp_real + dk[1] * temp_im  ) / mod_var_sq;
+            alpha1[k][1] = (dk[0] * temp_im   - dk[1] * temp_real) / mod_var_sq;
+        }
+
+        mod_var_sq = phi[1][0][0] * phi[1][0][0] + phi[1][0][1] * phi[1][0][1];
+        if (mod_var_sq == 0) {
+            alpha0[k][0] = 0;
+            alpha0[k][1] = 0;
+        } else {
+            float temp_real, temp_im;
+            temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
+                                       alpha1[k][1] * phi[1][1][1];
+            temp_im   = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
+                                       alpha1[k][0] * phi[1][1][1];
+
+            alpha0[k][0] = -(phi[1][0][0] * temp_real + phi[1][0][1] * temp_im  ) / mod_var_sq;
+            alpha0[k][0] = -(phi[1][0][0] * temp_im   - phi[1][0][1] * temp_real) / mod_var_sq;
+        }
+
+        if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
+           alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
+            alpha1[k][0] = 0;
+            alpha1[k][1] = 0;
+            alpha0[k][0] = 0;
+            alpha0[k][1] = 0;
+        }
+    }
+}
+
+// Chirp Factors (14496-3 sp04 p214)
+static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
+{
+    int i;
+    float new_bw;
+
+    for (i=0; i<sbr->n_q; i++) {
+        switch(ch_data->bs_invf_mode[0][i]) {
+            case 0:
+                if (ch_data->bs_invf_mode[1][i] == 1) {
+                    new_bw = 0.6f;
+                } else
+                    new_bw = 0.0f;
+                break;
+            case 1:
+                if (ch_data->bs_invf_mode[1][i] == 0) {
+                    new_bw = 0.6f;
+                } else
+                    new_bw = 0.75f;
+                break;
+            case 2:
+                new_bw = 0.9f;
+                break;
+            case 3:
+                new_bw = 0.98f;
+                break;
+            default:
+                break;
+        }
+
+        if (new_bw < sbr->bw_array[1][i]) {
+            sbr->bw_array[0][i] = 0.75f    * new_bw + 0.25f    * sbr->bw_array[1][i];
+        } else
+            sbr->bw_array[0][i] = 0.90625f * new_bw + 0.09375f * sbr->bw_array[1][i];
+    }
+
+    // update previous bw_array values
+    memcpy(sbr->bw_array[1], sbr->bw_array[0], 5 * sizeof(float));
+}
+
+static inline int find_freq_subband(uint16_t *table, int nel, int needle)
+{
+    int i;
+    for (i=0; i<nel; i++) {
+        if (needle >= table[i] && needle < table[i+1])
+            return i;
+    }
+    return -1;
+}
+
+// High Frequency Generator (14496-3 sp04 p215)
+static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
+                      float **x_high[2], float **x_low[2], float *alpha0[2],
+                      float *alpha1[2], float **bw_array, uint8_t *t_env,
+                      int bs_num_env)
+{
+    int i, x, l;
+    int ktmp = sbr->k[3];
+    for (i=0; i<sbr->num_patches; i++) {
+        if (i >= 1)
+            ktmp += sbr->patch_num_subbands[i];
+        for (x=0; x<sbr->patch_num_subbands[i]; x++) {
+            const int k = ktmp + x;
+            const int g = find_freq_subband(sbr->f_tablenoise, sbr->n_q + 1, k);
+            const int p = sbr->patch_start_subband[i] + x;
+
+            if (g < 0) {
+                av_log(ac->avccontext, AV_LOG_ERROR, "ERROR : no subband found for frequency %d\n", k);
+                return -1;
+            }
+
+            for (l=t_env[0]<<1; l<t_env[bs_num_env]<<1; l++) {
+                const int idx = l + T_HFADJ;
+                x_high[k][idx][0] =
+                    (x_low[p][idx - 2][0] * alpha1[p][0] -
+                     x_low[p][idx - 2][1] * alpha1[p][1]) * bw_array[0][g] * bw_array[0][g] +
+                    (x_low[p][idx - 1][0] * alpha0[p][0] -
+                     x_low[p][idx - 1][1] * alpha0[p][1]) * bw_array[0][g] +
+                     x_low[p][idx][0];
+                x_high[k][idx][1] =
+                    (x_low[p][idx - 2][1] * alpha1[p][0] +
+                     x_low[p][idx - 2][0] * alpha1[p][1]) * bw_array[0][g] * bw_array[0][g] +
+                    (x_low[p][idx - 1][1] * alpha0[p][0] +
+                     x_low[p][idx - 1][0] * alpha0[p][1]) * bw_array[0][g] +
+                     x_low[p][idx][1];
+            }
+        }
+    }
+
+    return 0;
+}
+
+// High Frequency Adjustment (14496-3 sp04 p217)
+
+// Mapping (14496-3 sp04 p217)
+static void sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
+                        SBRData *ch_data, int ch, int l_a[2])
+{
+    int i, l, m;
+
+    // The following is used for
+    l_a[0] = l_a[1]; // update previous frame's l_a value
+    l_a[1] = -1;
+    if ((ch_data->bs_frame_class & 1) && ch_data->bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
+        l_a[1] = ch_data->bs_num_env[1] + 1 - ch_data->bs_pointer;
+    } else if ((ch_data->bs_frame_class == 2) && (ch_data->bs_pointer > 1)) // VARFIX and bs_pointer > 1
+        l_a[1] = ch_data->bs_pointer - 1;
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        const unsigned int ilim = sbr->n[ch_data->bs_freq_res[l+1]];
+        uint16_t *table = ch_data->bs_freq_res[l+1] ? sbr->f_tablehigh : sbr->f_tablelow;
+        int k;
+
+        for (i=0; i<ilim; i++)
+            for (m=table[i]; m<table[i+1]; m++)
+                sbr->e_origmapped[l][m - sbr->k[3]] = sbr->env_facs[ch][l][i];
+
+        // ch_data->bs_num_noise > 1 => 2 noise floors
+        k = (ch_data->bs_num_noise > 1) && (sbr->t_env[ch][l] >= sbr->t_q[ch][1]);
+        for (i=0; i<sbr->n_q; i++)
+            for (m=table[i]; m<table[i+1]; m++)
+                sbr->q_mapped[l][m - sbr->k[3]] = sbr->noise_facs[ch][k][i];
+
+        for (i=0; i<sbr->n[1]; i++) {
+            memset(&sbr->s_indexmapped[l+1][sbr->f_tablehigh[i] - sbr->k[3]], 0,
+                   (sbr->f_tablehigh[i+1] - sbr->f_tablehigh[i]) * sizeof(sbr->s_indexmapped[l+1][0]));
+
+            if (ch_data->bs_add_harmonic_flag) {
+                const unsigned int m_midpoint =
+                    (sbr->f_tablehigh[i] + sbr->f_tablehigh[i+1])>>1;
+
+                sbr->s_indexmapped[l+1][m_midpoint - sbr->k[3]] = ch_data->bs_add_harmonic[i] *
+                    (l >= l_a[1] || (sbr->s_indexmapped[0][m_midpoint - sbr->k[3]] == 1));
+            }
+        }
+
+        for (i=0; i<ilim; i++) {
+            int additional_sinusoid_present = 0;
+            for (m=table[i]; m<table[i+1]; m++) {
+                if (sbr->s_indexmapped[l+1][m - sbr->k[3]]) {
+                    additional_sinusoid_present = 1;
+                    break;
+                }
+            }
+            memset(&sbr->s_mapped[l][table[i] - sbr->k[3]], additional_sinusoid_present,
+                    (table[i+1] - table[i]) * sizeof(sbr->s_mapped[l][0]));
+        }
+    }
+}
+
+// Estimation of current envelope (14496-3 sp04 p218)
+static void sbr_env_estimate(float **e_curr, float ***x_high,
+                             SpectralBandReplication *sbr, SBRData *ch_data,
+                             int ch)
+{
+    int i, l, m;
+    int ilb = sbr->t_env[ch][l]   * 2 + T_HFADJ;
+    int iub = sbr->t_env[ch][l+1] * 2 + T_HFADJ;
+
+    if (sbr->bs_interpol_freq) {
+        for (l=0; l<ch_data->bs_num_env[1]; l++) {
+            const int env_size = (sbr->t_env[ch][l+1] - sbr->t_env[ch][l]) << 1;
+
+            for (m=0; m<sbr->m; m++) {
+                float sum = 0.0f;
+
+                for (i=ilb; i<iub; i++) {
+                    sum += x_high[m + sbr->k[3]][i][0] * x_high[m + sbr->k[3]][i][0] +
+                           x_high[m + sbr->k[3]][i][1] * x_high[m + sbr->k[3]][i][1];
+                }
+                e_curr[l][m] = sum / env_size;
+            }
+        }
+    } else {
+        int k, p;
+
+        for (l=0; l<ch_data->bs_num_env[1]; l++) {
+            const int env_size = (sbr->t_env[ch][l+1] - sbr->t_env[ch][l]) << 1;
+            const uint16_t *table = ch_data->bs_freq_res[l+1] ? sbr->f_tablehigh : sbr->f_tablelow;
+
+            for (p=0; p<sbr->n[ch_data->bs_freq_res[l+1]]; p++) {
+                float sum = 0.0f;
+                const int den = env_size * (table[p+1] - table[p] + 1);
+
+                for (k=table[p]; k<table[p+1]; k++) {
+                    for (i=ilb; i<iub; i++) {
+                        sum += x_high[k][i][0] * x_high[k][i][0] +
+                               x_high[k][i][1] * x_high[k][i][1];
+                    }
+                    e_curr[l][k - sbr->k[3]] = sum / den;
+                }
+            }
+        }
+    }
+}
+
+// Calculation of levels of additional HF signal components (14496-3 sp04 p219)
+static void sbr_hf_additional_levels(SpectralBandReplication *sbr,
+                                     SBRData *ch_data)
+{
+    int l, m;
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        for (m=0; m<sbr->m; m++) {
+            const float temp = sbr->e_origmapped[l][m] / (1.0f + sbr->q_mapped[l][m]);
+            sbr->q_m[l][m] = sqrtf(temp * sbr->q_mapped[l][m]);
+            sbr->s_m[l][m] = sqrtf(temp * sbr->s_indexmapped[l][m]);
+        }
+    }
+}
+
+// Calculation of gain (14496-3 sp04 p219)
+static void sbr_gain_calc(AACContext * ac, SpectralBandReplication *sbr,
+                          SBRData *ch_data, int l_a[2])
+{
+    int i, k, l, m;
+    float gain_boost_temp[7][48];
+    float gain_max_temp[7][48];
+    // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
+    const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        int delta = !((l == l_a[1]) || (l == -(l_a[0] != ch_data->bs_num_env[0])));
+        for (m=0; m<sbr->m; m++) {
+            if (sbr->s_mapped[l][m]) {
+                sbr->gain[l][m] = sqrtf(sbr->e_origmapped[l][m] /
+                    ((1.0f + sbr->e_curr[l][m]) * (1.0f + sbr->q_mapped[l][m] * delta)));
+            } else {
+                sbr->gain[l][m] = sqrtf(sbr->e_origmapped[l][m] * sbr->q_mapped[l][m] /
+                    ((1.0f + sbr->e_curr[l][m]) * (1.0f + sbr->q_mapped[l][m])));
+            }
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        for (k=0; k<sbr->n[0]; k++) {
+            float sum[2] = { 0.0f, 0.0f };
+            for (i = sbr->f_tablelim[k] - sbr->k[3]; i < sbr->f_tablelim[k + 1] - sbr->k[3]; i++) {
+                sum[0] += sbr->e_origmapped[l][i];
+                sum[1] += sbr->e_curr[l][i];
+            }
+            gain_max_temp[l][k] = limgain[sbr->bs_limiter_gains]
+                * FFMIN(100000, sqrtf((EPS0 + sum[0]) / (EPS0 + sum[1])));
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        for (m=0; m<sbr->m; m++) {
+            if ((k = find_freq_subband(sbr->f_tablelim, sbr->n_lim, m + sbr->k[3])) < 0) {
+                av_log(ac->avccontext, AV_LOG_ERROR,
+                    "No subband found for frequency %d\n", m + sbr->k[3]);
+            }
+            sbr->gain_max[l][m] = gain_max_temp[l][k];
+            sbr->q_m_lim[l][m] = FFMIN(sbr->q_m[l][m],
+                sbr->q_m[l][m] * sbr->gain_max[l][m] / sbr->gain[l][m]);
+            sbr->gain_lim[l][m] = FFMIN(sbr->gain[l][m], sbr->gain_max[l][m]);
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        int delta = !((l == l_a[1]) || (l == -(l_a[0] != ch_data->bs_num_env[0])));
+        for (k=0; k<sbr->n[0]; k++) {
+            float sum[2] = { 0.0f, 0.0f };
+            for (i = sbr->f_tablelim[k] - sbr->k[3]; i < sbr->f_tablelim[k + 1] - sbr->k[3]; i++) {
+                sum[0] += sbr->e_origmapped[l][i];
+                sum[1] += sbr->e_curr[l][i] * sbr->gain_lim[l][i] * sbr->gain_lim[l][i]
+                    + sbr->s_m[l][i] * sbr->s_m[l][i]
+                    + (delta || !sbr->s_m[l][i]) * sbr->q_m_lim[l][i] * sbr->q_m_lim[l][i];
+            }
+            gain_boost_temp[l][k] = FFMIN(1.584893192, sqrtf((EPS0 + sum[0]) / (EPS0 + sum[1])));
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        for (m=0; m<sbr->m; m++) {
+            if ((k = find_freq_subband(sbr->f_tablelim, sbr->n_lim, m + sbr->k[3])) < 0) {
+                av_log(ac->avccontext, AV_LOG_ERROR,
+                    "No subband found for frequency %d\n", m + sbr->k[3]);
+            }
+            sbr->gain_boost[l][m] = gain_boost_temp[l][k];
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        for (m=0; m<sbr->m; m++) {
+            sbr->gain_limboost[l][m] = sbr->gain_lim[l][m] * sbr->gain_boost[l][m];
+            sbr->q_m_limboost[l][m]  = sbr->q_m_lim[l][m]  * sbr->gain_boost[l][m];
+            sbr->s_m_boost[l][m]     = sbr->s_m[l][m]      * sbr->gain_boost[l][m];
+        }
+    }
+}
+
+// Assembling HF Signals (14496-3 sp04 p220)
+static void sbr_hf_assemble(float **y[2], float **x_high[2],
+                            SpectralBandReplication *sbr, SBRData *ch_data,
+                            int ch, int l_a[2])
+{
+    int i, j, l, m;
+    const int h_SL = sbr->bs_smoothing_mode ? 0 : 4;
+    const float h_smooth[5] = {
+        0.33333333333333,
+        0.30150283239582,
+        0.21816949906249,
+        0.11516383427084,
+        0.03183050093751,
+    };
+    const int8_t phi[2][4] = {
+        {  1,  0, -1,  0}, // real
+        {  0,  1,  0, -1}, // imaginary
+    };
+    float g_temp[42][48], g_filt[42][48], q_temp[42][48], q_filt[42][48], w_temp[42][48][2];
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        for (i=sbr->t_env[ch][l]<<1; i<sbr->t_env[ch][l+1]<<1; i++) {
+            memcpy(g_temp[h_SL + i], sbr->gain_limboost[l], sbr->m * sizeof(sbr->gain_limboost[l][0]));
+            memcpy(q_temp[h_SL + i], sbr->q_m_limboost[l],  sbr->m * sizeof(sbr->q_m_limboost[l][0]));
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        if (h_SL == 0 && l != l_a[0] && l != l_a[1]) {
+            for (i=sbr->t_env[ch][l]<<1; i<sbr->t_env[ch][l+1]<<1; i++) {
+                for (m=0; m<sbr->m; m++) {
+                    const int idx1 = i + h_SL;
+                    g_filt[i][m] = 0.0f;
+                    for (j=0; j<=h_SL; j++)
+                        g_filt[i][m] += g_temp[idx1 - j][m] * h_smooth[j];
+                }
+            }
+        } else {
+            for (i=sbr->t_env[ch][l]<<1; i<sbr->t_env[ch][l+1]<<1; i++)
+                memcpy(g_filt[i], g_temp[i + h_SL], sbr->m * sizeof(g_temp[i + h_SL][0]));
+        }
+    }
+
+    for (i=sbr->t_env[ch][0]<<1; i < sbr->t_env[ch][ch_data->bs_num_env[1]]<<1; i++) {
+        const int idx1 = i + T_HFADJ;
+        for (m=0; m<sbr->m; m++) {
+            const int idx2 = m + sbr->k[3];
+            w_temp[i][m][0] = x_high[idx1][idx2][0] * g_filt[i][m];
+            w_temp[i][m][1] = x_high[idx1][idx2][1] * g_filt[i][m];
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        if (l != l_a[0] && l != l_a[1]) {
+            for (i=sbr->t_env[ch][l]<<1; i<sbr->t_env[ch][l+1]<<1; i++) {
+                for (m=0; m<sbr->m; m++) {
+                    if (sbr->s_m_boost[l][m] && h_SL) {
+                        const int idx1 = i + h_SL;
+                        q_filt[i][m] = 0.0f;
+                        for (j=0; j<=h_SL; j++)
+                            q_filt[i][m] += q_temp[idx1 - j][m] * h_smooth[j];
+                    } else
+                        q_filt[i][m] = q_temp[i][m];
+                }
+            }
+        } else {
+            for (i=sbr->t_env[ch][l]<<1; i<sbr->t_env[ch][l+1]<<1; i++)
+                memset(q_filt[i], 0, sbr->m * sizeof(q_filt[i][0]));
+        }
+    }
+
+    // FIXME - reset f_indexnoise[][][1] as appropriate
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        for (i=sbr->t_env[ch][l]<<1; i<sbr->t_env[ch][l+1]<<1; i++) {
+            for (m=0; m<sbr->m; m++) {
+                sbr->f_indexnoise[i][m][0] =
+                    (sbr->f_indexnoise[i][m][1] + (i - (sbr->t_env[ch][0]<<1)) * sbr->m + m + 1) & 0x1ff;
+                w_temp[i][m][0] += q_filt[i][m] * sbr_noise_table[sbr->f_indexnoise[i][m][0]][0];
+                w_temp[i][m][1] += q_filt[i][m] * sbr_noise_table[sbr->f_indexnoise[i][m][0]][1];
+            }
+        }
+    }
+
+    for (l=0; l<ch_data->bs_num_env[1]; l++) {
+        for (i=sbr->t_env[ch][l]<<1; i<sbr->t_env[ch][l+1]<<1; i++) {
+            sbr->f_indexsine[i][0] = (((sbr->f_indexsine[i][1] + 1) & 3) + i - (sbr->t_env[ch][0]<<1)) & 3;
+            for (m=0; m<sbr->m; m++) {
+                y[i + T_HFADJ][m + sbr->k[3]][0] =
+                    w_temp[i][m][0] + sbr->s_m_boost[i][m] * phi[0][sbr->f_indexsine[i][0]];
+                y[i + T_HFADJ][m + sbr->k[3]][1] =
+                    w_temp[i][m][1] + sbr->s_m_boost[i][m] * phi[1][sbr->f_indexsine[i][0]] * (1 - 2*((m + sbr->k[3]) & 1));
+            }
+        }
+    }
+}
+
+static void apply_sbr()
+{
+#if 0
+    /* decode channel */
+    sbr_qmf_analysis();
+    sbr_hf_gen();
+
+    // hf_adj
+    sbr_mapping();
+    sbr_env_estimate();
+    sbr_hf_additional_levels();
+    sbr_gain_calc();
+    sbr_hf_assemble();
+
+    /* synthesis */
+    sbr_qmf_synthesis();
+#endif
+}

Added: sbr-wip/aacsbr.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ sbr-wip/aacsbr.h	Sat Sep 26 16:59:27 2009	(r5388)
@@ -0,0 +1,161 @@
+/*
+ * AAC Spectral Band Replication definitions and structures
+ * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
+ *
+ * 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 libavcodec/aacsbr.h
+ * AAC Spectral Band Replication definitions and structures
+ * @author Robert Swain ( rob opendot cl )
+ */
+
+#ifndef AVCODEC_AACSBR_H
+#define AVCODEC_AACSBR_H
+
+#include <stdint.h>
+
+#define SBR_INIT_VLC_STATIC(num, size) \
+    INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size/sbr_tmp[num].elem_size, \
+        sbr_tmp[num].sbr_bits ,                      1,                      1, \
+        sbr_tmp[num].sbr_codes, sbr_tmp[num].elem_size, sbr_tmp[num].elem_size, \
+        size);
+
+#define NOISE_FLOOR_OFFSET 6.0f
+#define T_HFADJ 2
+// constant to avoid division by zero, e.g. 96 dB below maximum signal input
+#define EPS0 0.000000000001
+
+/**
+ * SBR VLC tables
+ */
+enum {
+    T_HUFFMAN_ENV_1_5DB,
+    F_HUFFMAN_ENV_1_5DB,
+    T_HUFFMAN_ENV_BAL_1_5DB,
+    F_HUFFMAN_ENV_BAL_1_5DB,
+    T_HUFFMAN_ENV_3_0DB,
+    F_HUFFMAN_ENV_3_0DB,
+    T_HUFFMAN_ENV_BAL_3_0DB,
+    F_HUFFMAN_ENV_BAL_3_0DB,
+    T_HUFFMAN_NOISE_3_0DB,
+    T_HUFFMAN_NOISE_BAL_3_0DB,
+};
+
+/**
+ * bs_frame_class - frame class of current SBR frame (14496-3 sp04 p98)
+ */
+enum {
+    FIXFIX,
+    FIXVAR,
+    VARFIX,
+    VARVAR,
+};
+
+/**
+ * Spectral Band Replication header - spectrum parameters that invoke a reset if they differ from the previous header.
+ */
+typedef struct {
+    uint8_t bs_start_freq;
+    uint8_t bs_stop_freq;
+    uint8_t bs_xover_band;
+
+    // if(bs_header_extra_1)
+    uint8_t bs_freq_scale;
+    uint8_t bs_alter_scale;
+    uint8_t bs_noise_bands;
+} SpectrumParameters;
+
+/**
+ * Spectral Band Replication data - main bitstream data variables
+ */
+typedef struct {
+    uint8_t            bs_frame_class;
+    uint8_t            bs_add_harmonic_flag;
+    uint8_t            bs_num_env[2];
+    uint8_t            bs_freq_res[7];
+    uint8_t            bs_var_bord[2];
+    uint8_t            bs_num_rel[2];
+    uint8_t            bs_rel_bord[2][3];
+    uint8_t            bs_pointer;
+    uint8_t            bs_num_noise;
+    uint8_t            bs_df_env[7];
+    uint8_t            bs_df_noise[2];
+    uint8_t            bs_invf_mode[2][5];
+    uint32_t           bs_data_env[7][32];
+    uint32_t           bs_data_noise[2][5];
+    uint8_t            bs_add_harmonic[32];
+} SBRData;
+
+/**
+ * Spectral Band Replication
+ */
+typedef struct {
+    // SBR header bitstream variables
+    uint8_t            bs_amp_res;
+    SpectrumParameters spectrum_params[2];
+    // if(bs_header_extra_2)
+    uint8_t            bs_limiter_bands;
+    uint8_t            bs_limiter_gains;
+    uint8_t            bs_interpol_freq;
+    uint8_t            bs_smoothing_mode;
+    // SBR data bitstream variables
+    uint8_t            bs_coupling;
+    SBRData            data[2];
+    // SBR tool variables
+    uint8_t            reset;
+    uint8_t            k[4]; // k0, k1, k2 and kx respectively
+    uint8_t            m;
+    uint8_t            n_master;
+    uint8_t            n[2]; // n_low and n_high respectively
+    uint8_t            n_q;
+    uint8_t            n_lim;
+    uint16_t           f_master[49];
+    uint16_t           f_tablelow[25];
+    uint16_t           f_tablehigh[49];
+    uint16_t           f_tablenoise[6];
+    uint16_t           f_tablelim[29];
+    uint8_t            num_patches;
+    uint8_t            patch_num_subbands[5];
+    uint8_t            patch_start_subband[5];
+    uint8_t            t_env[2][8];
+    uint8_t            t_q[2][3];
+    float              env_facs[2][7][48];
+    float              noise_facs[2][2][5];
+    float              W[32][32][2];
+    float              bw_array[2][5];
+    float              e_origmapped[7][48];
+    float              q_mapped[7][48];
+    float              s_indexmapped[8][48];
+    float              s_mapped[7][48];
+    float              e_curr[7][48];
+    float              q_m[7][48];
+    float              s_m[7][48];
+    float              gain[7][48];
+    float              gain_max[7][48];
+    float              gain_lim[7][48];
+    float              gain_boost[7][48];
+    float              gain_limboost[7][48];
+    float              q_m_lim[7][48];
+    float              q_m_limboost[7][48];
+    float              s_m_boost[7][48];
+    uint8_t            f_indexnoise[42][48][2];
+    uint8_t            f_indexsine[42][2];
+} SpectralBandReplication;
+
+#endif /* AVCODEC_AACSBR_H */

Added: sbr-wip/aacsbrdata.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ sbr-wip/aacsbrdata.h	Sat Sep 26 16:59:27 2009	(r5388)
@@ -0,0 +1,691 @@
+/*
+ * AAC Spectral Band Replication decoding data
+ * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
+ *
+ * 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 libavcodec/aacsbrdata.h
+ * AAC Spectral Band Replication decoding data
+ * @author Robert Swain ( rob opendot cl )
+ */
+
+#ifndef AVCODEC_AACSBRDATA_H
+#define AVCODEC_AACSBRDATA_H
+
+#include <stdint.h>
+
+///< Huffman tables for SBR
+
+static const uint8_t t_huffman_env_1_5dB_bits[121] = {
+    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, 17, 18, 16, 17, 18, 17,
+    16, 16, 16, 16, 15, 14, 14, 13,
+    13, 12, 11, 10,  9,  8,  7,  6,
+     5,  4,  3,  2,  2,  3,  4,  5,
+     6,  7,  8,  9, 10, 12, 13, 14,
+    14, 15, 16, 17, 16, 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, 19, 19, 19, 19, 19, 19,
+    19, 19, 19, 19, 19, 19, 19, 19,
+    19,
+};
+
+static const uint32_t t_huffman_env_1_5dB_codes[121] = {
+    0x3ffd6, 0x3ffd7, 0x3ffd8, 0x3ffd9, 0x3ffda, 0x3ffdb, 0x7ffb8, 0x7ffb9,
+    0x7ffba, 0x7ffbb, 0x7ffbc, 0x7ffbd, 0x7ffbe, 0x7ffbf, 0x7ffc0, 0x7ffc1,
+    0x7ffc2, 0x7ffc3, 0x7ffc4, 0x7ffc5, 0x7ffc6, 0x7ffc7, 0x7ffc8, 0x7ffc9,
+    0x7ffca, 0x7ffcb, 0x7ffcc, 0x7ffcd, 0x7ffce, 0x7ffcf, 0x7ffd0, 0x7ffd1,
+    0x7ffd2, 0x7ffd3, 0x1ffe6, 0x3ffd4, 0x0fff0, 0x1ffe9, 0x3ffd5, 0x1ffe7,
+    0x0fff1, 0x0ffec, 0x0ffed, 0x0ffee, 0x07ff4, 0x03ff9, 0x03ff7, 0x01ffa,
+    0x01ff9, 0x00ffb, 0x007fc, 0x003fc, 0x001fd, 0x000fd, 0x0007d, 0x0003d,
+    0x0001d, 0x0000d, 0x00005, 0x00001, 0x00000, 0x00004, 0x0000c, 0x0001c,
+    0x0003c, 0x0007c, 0x000fc, 0x001fc, 0x003fd, 0x00ffa, 0x01ff8, 0x03ff6,
+    0x03ff8, 0x07ff5, 0x0ffef, 0x1ffe8, 0x0fff2, 0x7ffd4, 0x7ffd5, 0x7ffd6,
+    0x7ffd7, 0x7ffd8, 0x7ffd9, 0x7ffda, 0x7ffdb, 0x7ffdc, 0x7ffdd, 0x7ffde,
+    0x7ffdf, 0x7ffe0, 0x7ffe1, 0x7ffe2, 0x7ffe3, 0x7ffe4, 0x7ffe5, 0x7ffe6,
+    0x7ffe7, 0x7ffe8, 0x7ffe9, 0x7ffea, 0x7ffeb, 0x7ffec, 0x7ffed, 0x7ffee,
+    0x7ffef, 0x7fff0, 0x7fff1, 0x7fff2, 0x7fff3, 0x7fff4, 0x7fff5, 0x7fff6,
+    0x7fff7, 0x7fff8, 0x7fff9, 0x7fffa, 0x7fffb, 0x7fffc, 0x7fffd, 0x7fffe,
+    0x7ffff,
+};
+
+static const uint8_t f_huffman_env_1_5dB_bits[121] = {
+    19, 19, 20, 20, 20, 20, 20, 20,
+    20, 19, 20, 20, 20, 20, 19, 20,
+    19, 19, 20, 18, 20, 20, 20, 19,
+    20, 20, 20, 19, 20, 19, 18, 19,
+    18, 18, 17, 18, 17, 17, 17, 16,
+    16, 16, 15, 15, 14, 13, 13, 12,
+    12, 11, 10,  9,  9,  8,  7,  6,
+     5,  4,  3,  2,  2,  3,  4,  5,
+     6,  8,  8,  9, 10, 11, 11, 11,
+    12, 12, 13, 13, 14, 14, 16, 16,
+    17, 17, 18, 18, 18, 18, 18, 18,
+    18, 20, 19, 20, 20, 20, 20, 20,
+    20, 19, 20, 20, 20, 20, 19, 20,
+    18, 20, 20, 19, 19, 20, 20, 20,
+    20, 20, 20, 20, 20, 20, 20, 20,
+    20,
+};
+
+static const uint32_t f_huffman_env_1_5dB_codes[121] = {
+    0x7ffe7, 0x7ffe8, 0xfffd2, 0xfffd3, 0xfffd4, 0xfffd5, 0xfffd6, 0xfffd7,
+    0xfffd8, 0x7ffda, 0xfffd9, 0xfffda, 0xfffdb, 0xfffdc, 0x7ffdb, 0xfffdd,
+    0x7ffdc, 0x7ffdd, 0xfffde, 0x3ffe4, 0xfffdf, 0xfffe0, 0xfffe1, 0x7ffde,
+    0xfffe2, 0xfffe3, 0xfffe4, 0x7ffdf, 0xfffe5, 0x7ffe0, 0x3ffe8, 0x7ffe1,
+    0x3ffe0, 0x3ffe9, 0x1ffef, 0x3ffe5, 0x1ffec, 0x1ffed, 0x1ffee, 0x0fff4,
+    0x0fff3, 0x0fff0, 0x07ff7, 0x07ff6, 0x03ffa, 0x01ffa, 0x01ff9, 0x00ffa,
+    0x00ff8, 0x007f9, 0x003fb, 0x001fc, 0x001fa, 0x000fb, 0x0007c, 0x0003c,
+    0x0001c, 0x0000c, 0x00005, 0x00001, 0x00000, 0x00004, 0x0000d, 0x0001d,
+    0x0003d, 0x000fa, 0x000fc, 0x001fb, 0x003fa, 0x007f8, 0x007fa, 0x007fb,
+    0x00ff9, 0x00ffb, 0x01ff8, 0x01ffb, 0x03ff8, 0x03ff9, 0x0fff1, 0x0fff2,
+    0x1ffea, 0x1ffeb, 0x3ffe1, 0x3ffe2, 0x3ffea, 0x3ffe3, 0x3ffe6, 0x3ffe7,
+    0x3ffeb, 0xfffe6, 0x7ffe2, 0xfffe7, 0xfffe8, 0xfffe9, 0xfffea, 0xfffeb,
+    0xfffec, 0x7ffe3, 0xfffed, 0xfffee, 0xfffef, 0xffff0, 0x7ffe4, 0xffff1,
+    0x3ffec, 0xffff2, 0xffff3, 0x7ffe5, 0x7ffe6, 0xffff4, 0xffff5, 0xffff6,
+    0xffff7, 0xffff8, 0xffff9, 0xffffa, 0xffffb, 0xffffc, 0xffffd, 0xffffe,
+    0xfffff,
+};
+
+static const uint8_t t_huffman_env_bal_1_5dB_bits[49] = {
+    16, 16, 16, 16, 16, 16, 16, 16,
+    16, 16, 16, 16, 16, 16, 16, 16,
+    16, 16, 12, 11,  9,  7,  5,  3,
+     1,  2,  4,  6,  8, 11, 12, 15,
+    16, 16, 16, 16, 16, 16, 16, 17,
+    17, 17, 17, 17, 17, 17, 17, 17,
+    17,
+};
+
+static const uint32_t t_huffman_env_bal_1_5dB_codes[49] = {
+    0x0ffe4, 0x0ffe5, 0x0ffe6, 0x0ffe7, 0x0ffe8, 0x0ffe9, 0x0ffea, 0x0ffeb,
+    0x0ffec, 0x0ffed, 0x0ffee, 0x0ffef, 0x0fff0, 0x0fff1, 0x0fff2, 0x0fff3,
+    0x0fff4, 0x0ffe2, 0x00ffc, 0x007fc, 0x001fe, 0x0007e, 0x0001e, 0x00006,
+    0x00000, 0x00002, 0x0000e, 0x0003e, 0x000fe, 0x007fd, 0x00ffd, 0x07ff0,
+    0x0ffe3, 0x0fff5, 0x0fff6, 0x0fff7, 0x0fff8, 0x0fff9, 0x0fffa, 0x1fff6,
+    0x1fff7, 0x1fff8, 0x1fff9, 0x1fffa, 0x1fffb, 0x1fffc, 0x1fffd, 0x1fffe,
+    0x1ffff,
+};
+
+static const uint8_t f_huffman_env_bal_1_5dB_bits[49] = {
+    18, 18, 18, 18, 18, 18, 18, 18,
+    18, 18, 18, 18, 18, 18, 18, 16,
+    17, 14, 11, 11,  8,  7,  4,  2,
+     1,  3,  5,  6,  9, 11, 12, 15,
+    16, 18, 18, 18, 18, 18, 18, 18,
+    18, 18, 18, 18, 18, 18, 18, 19,
+    19,
+};
+
+static const uint32_t f_huffman_env_bal_1_5dB_codes[49] = {
+    0x3ffe2, 0x3ffe3, 0x3ffe4, 0x3ffe5, 0x3ffe6, 0x3ffe7, 0x3ffe8, 0x3ffe9,
+    0x3ffea, 0x3ffeb, 0x3ffec, 0x3ffed, 0x3ffee, 0x3ffef, 0x3fff0, 0x0fff7,
+    0x1fff0, 0x03ffc, 0x007fe, 0x007fc, 0x000fe, 0x0007e, 0x0000e, 0x00002,
+    0x00000, 0x00006, 0x0001e, 0x0003e, 0x001fe, 0x007fd, 0x00ffe, 0x07ffa,
+    0x0fff6, 0x3fff1, 0x3fff2, 0x3fff3, 0x3fff4, 0x3fff5, 0x3fff6, 0x3fff7,
+    0x3fff8, 0x3fff9, 0x3fffa, 0x3fffb, 0x3fffc, 0x3fffd, 0x3fffe, 0x7fffe,
+    0x7ffff,
+};
+
+static const uint8_t t_huffman_env_3_0dB_bits[63] = {
+    18, 18, 19, 19, 19, 19, 19, 19,
+    19, 19, 19, 19, 19, 19, 19, 19,
+    19, 17, 16, 16, 16, 14, 14, 14,
+    13, 12, 11,  8,  6,  4,  2,  1,
+     3,  5,  7,  9, 11, 13, 14, 14,
+    15, 16, 17, 18, 19, 19, 19, 19,
+    19, 19, 19, 19, 19, 19, 19, 19,
+    19, 19, 19, 19, 19, 19, 19,
+};
+
+static const uint32_t t_huffman_env_3_0dB_codes[63] = {
+    0x3ffed, 0x3ffee, 0x7ffde, 0x7ffdf, 0x7ffe0, 0x7ffe1, 0x7ffe2, 0x7ffe3,
+    0x7ffe4, 0x7ffe5, 0x7ffe6, 0x7ffe7, 0x7ffe8, 0x7ffe9, 0x7ffea, 0x7ffeb,
+    0x7ffec, 0x1fff4, 0x0fff7, 0x0fff9, 0x0fff8, 0x03ffb, 0x03ffa, 0x03ff8,
+    0x01ffa, 0x00ffc, 0x007fc, 0x000fe, 0x0003e, 0x0000e, 0x00002, 0x00000,
+    0x00006, 0x0001e, 0x0007e, 0x001fe, 0x007fd, 0x01ffb, 0x03ff9, 0x03ffc,
+    0x07ffa, 0x0fff6, 0x1fff5, 0x3ffec, 0x7ffed, 0x7ffee, 0x7ffef, 0x7fff0,
+    0x7fff1, 0x7fff2, 0x7fff3, 0x7fff4, 0x7fff5, 0x7fff6, 0x7fff7, 0x7fff8,
+    0x7fff9, 0x7fffa, 0x7fffb, 0x7fffc, 0x7fffd, 0x7fffe, 0x7ffff,
+};
+
+static const uint8_t f_huffman_env_3_0dB_bits[63] = {
+    20, 20, 20, 20, 20, 20, 20, 18,
+    19, 19, 19, 19, 18, 18, 20, 19,
+    17, 18, 17, 16, 16, 15, 14, 12,
+    11, 10,  9,  8,  6,  4,  2,  1,
+     3,  5,  8,  9, 10, 11, 12, 13,
+    14, 15, 15, 16, 16, 17, 17, 18,
+    18, 18, 20, 19, 19, 19, 20, 19,
+    19, 20, 20, 20, 20, 20, 20,
+};
+
+static const uint32_t f_huffman_env_3_0dB_codes[63] = {
+    0xffff0, 0xffff1, 0xffff2, 0xffff3, 0xffff4, 0xffff5, 0xffff6, 0x3fff3,
+    0x7fff5, 0x7ffee, 0x7ffef, 0x7fff6, 0x3fff4, 0x3fff2, 0xffff7, 0x7fff0,
+    0x1fff5, 0x3fff0, 0x1fff4, 0x0fff7, 0x0fff6, 0x07ff8, 0x03ffb, 0x00ffd,
+    0x007fd, 0x003fd, 0x001fd, 0x000fd, 0x0003e, 0x0000e, 0x00002, 0x00000,
+    0x00006, 0x0001e, 0x000fc, 0x001fc, 0x003fc, 0x007fc, 0x00ffc, 0x01ffc,
+    0x03ffa, 0x07ff9, 0x07ffa, 0x0fff8, 0x0fff9, 0x1fff6, 0x1fff7, 0x3fff5,
+    0x3fff6, 0x3fff1, 0xffff8, 0x7fff1, 0x7fff2, 0x7fff3, 0xffff9, 0x7fff7,
+    0x7fff4, 0xffffa, 0xffffb, 0xffffc, 0xffffd, 0xffffe, 0xfffff,
+};
+
+static const uint8_t t_huffman_env_bal_3_0dB_bits[25] = {
+    13, 13, 13, 13, 13, 13, 13, 12,
+     8,  7,  4,  3,  1,  2,  5,  6,
+     9, 13, 13, 13, 13, 13, 13, 14,
+    14,
+};
+
+static const uint16_t t_huffman_env_bal_3_0dB_codes[25] = {
+    0x1ff2, 0x1ff3, 0x1ff4, 0x1ff5, 0x1ff6, 0x1ff7, 0x1ff8, 0x0ff8,
+    0x00fe, 0x007e, 0x000e, 0x0006, 0x0000, 0x0002, 0x001e, 0x003e,
+    0x01fe, 0x1ff9, 0x1ffa, 0x1ffb, 0x1ffc, 0x1ffd, 0x1ffe, 0x3ffe,
+    0x3fff,
+};
+
+static const uint8_t f_huffman_env_bal_3_0dB_bits[25] = {
+    13, 13, 13, 13, 13, 14, 14, 11,
+     8,  7,  4,  2,  1,  3,  5,  6,
+     9, 12, 13, 14, 14, 14, 14, 14,
+    14,
+};
+
+static const uint16_t f_huffman_env_bal_3_0dB_codes[25] = {
+    0x1ff7, 0x1ff8, 0x1ff9, 0x1ffa, 0x1ffb, 0x3ff8, 0x3ff9, 0x07fc,
+    0x00fe, 0x007e, 0x000e, 0x0002, 0x0000, 0x0006, 0x001e, 0x003e,
+    0x01fe, 0x0ffa, 0x1ff6, 0x3ffa, 0x3ffb, 0x3ffc, 0x3ffd, 0x3ffe,
+    0x3fff,
+};
+
+static const uint8_t t_huffman_noise_3_0dB_bits[63] = {
+    13, 13, 13, 13, 13, 13, 13, 13,
+    13, 13, 13, 13, 13, 13, 13, 13,
+    13, 13, 13, 13, 13, 13, 13, 13,
+    13, 13, 11,  8,  6,  4,  3,  1,
+     2,  5,  8, 10, 13, 13, 13, 13,
+    13, 13, 13, 13, 13, 13, 13, 13,
+    13, 13, 13, 13, 13, 13, 13, 13,
+    13, 13, 13, 13, 13, 14, 14,
+};
+
+static const uint16_t t_huffman_noise_3_0dB_codes[63] = {
+    0x1fce, 0x1fcf, 0x1fd0, 0x1fd1, 0x1fd2, 0x1fd3, 0x1fd4, 0x1fd5,
+    0x1fd6, 0x1fd7, 0x1fd8, 0x1fd9, 0x1fda, 0x1fdb, 0x1fdc, 0x1fdd,
+    0x1fde, 0x1fdf, 0x1fe0, 0x1fe1, 0x1fe2, 0x1fe3, 0x1fe4, 0x1fe5,
+    0x1fe6, 0x1fe7, 0x07f2, 0x00fd, 0x003e, 0x000e, 0x0006, 0x0000,
+    0x0002, 0x001e, 0x00fc, 0x03f8, 0x1fcc, 0x1fe8, 0x1fe9, 0x1fea,
+    0x1feb, 0x1fec, 0x1fcd, 0x1fed, 0x1fee, 0x1fef, 0x1ff0, 0x1ff1,
+    0x1ff2, 0x1ff3, 0x1ff4, 0x1ff5, 0x1ff6, 0x1ff7, 0x1ff8, 0x1ff9,
+    0x1ffa, 0x1ffb, 0x1ffc, 0x1ffd, 0x1ffe, 0x3ffe, 0x3fff,
+};
+
+static const uint8_t t_huffman_noise_bal_3_0dB_bits[25] = {
+    8, 8, 8, 8, 8, 8, 8, 8,
+    8, 8, 5, 2, 1, 3, 6, 8,
+    8, 8, 8, 8, 8, 8, 8, 8,
+    8,
+};
+
+static const uint8_t t_huffman_noise_bal_3_0dB_codes[25] = {
+    0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3,
+    0xf4, 0xf5, 0x1c, 0x02, 0x00, 0x06, 0x3a, 0xf6,
+    0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
+    0xff,
+};
+
+static const uint8_t sbr_offset[6][16] = {
+    {-8, -7, -6, -5, -4, -3, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7}, //             fs_sbr  = 16000 Hz
+    {-5, -4, -3, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  9, 11, 13}, //             fs_sbr  = 22050 Hz
+    {-5, -3, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  9, 11, 13, 16}, //             fs_sbr  = 24000 Hz
+    {-6, -4, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  9, 11, 13, 16}, //             fs_sbr  = 32000 Hz
+    {-4, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  9, 11, 13, 16, 20}, // 44100 Hz <= fs_sbr <= 64000 Hz
+    {-2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  9, 11, 13, 16, 20, 24}, // 64000 Hz <  fs_sbr
+};
+
+///< window coefficients for analysis/synthesis QMF banks
+static const float sbr_qmf_window[640] = {
+  0.0000000000, -0.0005525286, -0.0005617692, -0.0004947518,
+ -0.0004875227, -0.0004893791, -0.0005040714, -0.0005226564,
+ -0.0005466565, -0.0005677802, -0.0005870930, -0.0006132747,
+ -0.0006312493, -0.0006540333, -0.0006777690, -0.0006941614,
+ -0.0007157736, -0.0007255043, -0.0007440941, -0.0007490598,
+ -0.0007681371, -0.0007724848, -0.0007834332, -0.0007779869,
+ -0.0007803664, -0.0007801449, -0.0007757977, -0.0007630793,
+ -0.0007530001, -0.0007319357, -0.0007215391, -0.0006917937,
+ -0.0006650415, -0.0006341594, -0.0005946118, -0.0005564576,
+ -0.0005145572, -0.0004606325, -0.0004095121, -0.0003501175,
+ -0.0002896981, -0.0002098337, -0.0001446380, -0.0000617334,
+  0.0000134949,  0.0001094383,  0.0002043017,  0.0002949531,
+  0.0004026540,  0.0005107388,  0.0006239376,  0.0007458025,
+  0.0008608443,  0.0009885988,  0.0011250155,  0.0012577884,
+  0.0013902494,  0.0015443219,  0.0016868083,  0.0018348265,
+  0.0019841140,  0.0021461583,  0.0023017254,  0.0024625616,
+  0.0026201758,  0.0027870464,  0.0029469447,  0.0031125420,
+  0.0032739613,  0.0034418874,  0.0036008268,  0.0037603922,
+  0.0039207432,  0.0040819753,  0.0042264269,  0.0043730719,
+  0.0045209852,  0.0046606460,  0.0047932560,  0.0049137603,
+  0.0050393022,  0.0051407353,  0.0052461166,  0.0053471681,
+  0.0054196775,  0.0054876040,  0.0055475714,  0.0055938023,
+  0.0056220643,  0.0056455196,  0.0056389199,  0.0056266114,
+  0.0055917128,  0.0055404363,  0.0054753783,  0.0053838975,
+  0.0052715758,  0.0051382275,  0.0049839687,  0.0048109469,
+  0.0046039530,  0.0043801861,  0.0041251642,  0.0038456408,
+  0.0035401246,  0.0032091885,  0.0028446757,  0.0024508540,
+  0.0020274176,  0.0015784682,  0.0010902329,  0.0005832264,
+  0.0000276045, -0.0005464280, -0.0011568135, -0.0018039472,
+ -0.0024826723, -0.0031933778, -0.0039401124, -0.0047222596,
+ -0.0055337211, -0.0063792293, -0.0072615816, -0.0081798233,
+ -0.0091325329, -0.0101150215, -0.0111315548, -0.0121849995,
+  0.0132718220,  0.0143904666,  0.0155405553,  0.0167324712,
+  0.0179433381,  0.0191872431,  0.0204531793,  0.0217467550,
+  0.0230680169,  0.0244160992,  0.0257875847,  0.0271859429,
+  0.0286072173,  0.0300502657,  0.0315017608,  0.0329754081,
+  0.0344620948,  0.0359697560,  0.0374812850,  0.0390053679,
+  0.0405349170,  0.0420649094,  0.0436097542,  0.0451488405,
+  0.0466843027,  0.0482165720,  0.0497385755,  0.0512556155,
+  0.0527630746,  0.0542452768,  0.0557173648,  0.0571616450,
+  0.0585915683,  0.0599837480,  0.0613455171,  0.0626857808,
+  0.0639715898,  0.0652247106,  0.0664367512,  0.0676075985,
+  0.0687043828,  0.0697630244,  0.0707628710,  0.0717002673,
+  0.0725682583,  0.0733620255,  0.0741003642,  0.0747452558,
+  0.0753137336,  0.0758008358,  0.0761992479,  0.0764992170,
+  0.0767093490,  0.0768173975,  0.0768230011,  0.0767204924,
+  0.0765050718,  0.0761748321,  0.0757305756,  0.0751576255,
+  0.0744664394,  0.0736406005,  0.0726774642,  0.0715826364,
+  0.0703533073,  0.0689664013,  0.0674525021,  0.0657690668,
+  0.0639444805,  0.0619602779,  0.0598166570,  0.0575152691,
+  0.0550460034,  0.0524093821,  0.0495978676,  0.0466303305,
+  0.0434768782,  0.0401458278,  0.0366418116,  0.0329583930,
+  0.0290824006,  0.0250307561,  0.0207997072,  0.0163701258,
+  0.0117623832,  0.0069636862,  0.0019765601, -0.0032086896,
+ -0.0085711749, -0.0141288827, -0.0198834129, -0.0258227288,
+ -0.0319531274, -0.0382776572, -0.0447806821, -0.0514804176,
+ -0.0583705326, -0.0654409853, -0.0726943300, -0.0801372934,
+ -0.0877547536, -0.0955533352, -0.1035329531, -0.1116826931,
+ -0.1200077984, -0.1285002850, -0.1371551761, -0.1459766491,
+ -0.1549607071, -0.1640958855, -0.1733808172, -0.1828172548,
+ -0.1923966745, -0.2021250176, -0.2119735853, -0.2219652696,
+ -0.2320690870, -0.2423016884, -0.2526480309, -0.2631053299,
+ -0.2736634040, -0.2843214189, -0.2950716717, -0.3059098575,
+ -0.3168278913, -0.3278113727, -0.3388722693, -0.3499914122,
+  0.3611589903,  0.3723795546,  0.3836350013,  0.3949211761,
+  0.4062317676,  0.4175696896,  0.4289119920,  0.4402553754,
+  0.4515996535,  0.4629308085,  0.4742453214,  0.4855253091,
+  0.4967708254,  0.5079817500,  0.5191234970,  0.5302240895,
+  0.5412553448,  0.5522051258,  0.5630789140,  0.5738524131,
+  0.5845403235,  0.5951123086,  0.6055783538,  0.6159109932,
+  0.6261242695,  0.6361980107,  0.6461269695,  0.6559016302,
+  0.6655139880,  0.6749663190,  0.6842353293,  0.6933282376,
+  0.7022388719,  0.7109410426,  0.7194462634,  0.7277448900,
+  0.7358211758,  0.7436827863,  0.7513137456,  0.7587080760,
+  0.7658674865,  0.7727780881,  0.7794287519,  0.7858353120,
+  0.7919735841,  0.7978466413,  0.8034485751,  0.8087695004,
+  0.8138191270,  0.8185776004,  0.8230419890,  0.8272275347,
+  0.8311038457,  0.8346937361,  0.8379717337,  0.8409541392,
+  0.8436238281,  0.8459818469,  0.8480315777,  0.8497805198,
+  0.8511971524,  0.8523047035,  0.8531020949,  0.8535720573,
+  0.8537385600,  0.8535720573,  0.8531020949,  0.8523047035,
+  0.8511971524,  0.8497805198,  0.8480315777,  0.8459818469,
+  0.8436238281,  0.8409541392,  0.8379717337,  0.8346937361,
+  0.8311038457,  0.8272275347,  0.8230419890,  0.8185776004,
+  0.8138191270,  0.8087695004,  0.8034485751,  0.7978466413,
+  0.7919735841,  0.7858353120,  0.7794287519,  0.7727780881,
+  0.7658674865,  0.7587080760,  0.7513137456,  0.7436827863,
+  0.7358211758,  0.7277448900,  0.7194462634,  0.7109410426,
+  0.7022388719,  0.6933282376,  0.6842353293,  0.6749663190,
+  0.6655139880,  0.6559016302,  0.6461269695,  0.6361980107,
+  0.6261242695,  0.6159109932,  0.6055783538,  0.5951123086,
+  0.5845403235,  0.5738524131,  0.5630789140,  0.5522051258,
+  0.5412553448,  0.5302240895,  0.5191234970,  0.5079817500,
+  0.4967708254,  0.4855253091,  0.4742453214,  0.4629308085,
+  0.4515996535,  0.4402553754,  0.4289119920,  0.4175696896,
+  0.4062317676,  0.3949211761,  0.3836350013,  0.3723795546,
+ -0.3611589903, -0.3499914122, -0.3388722693, -0.3278113727,
+ -0.3168278913, -0.3059098575, -0.2950716717, -0.2843214189,
+ -0.2736634040, -0.2631053299, -0.2526480309, -0.2423016884,
+ -0.2320690870, -0.2219652696, -0.2119735853, -0.2021250176,
+ -0.1923966745, -0.1828172548, -0.1733808172, -0.1640958855,
+ -0.1549607071, -0.1459766491, -0.1371551761, -0.1285002850,
+ -0.1200077984, -0.1116826931, -0.1035329531, -0.0955533352,
+ -0.0877547536, -0.0801372934, -0.0726943300, -0.0654409853,
+ -0.0583705326, -0.0514804176, -0.0447806821, -0.0382776572,
+ -0.0319531274, -0.0258227288, -0.0198834129, -0.0141288827,
+ -0.0085711749, -0.0032086896,  0.0019765601,  0.0069636862,
+  0.0117623832,  0.0163701258,  0.0207997072,  0.0250307561,
+  0.0290824006,  0.0329583930,  0.0366418116,  0.0401458278,
+  0.0434768782,  0.0466303305,  0.0495978676,  0.0524093821,
+  0.0550460034,  0.0575152691,  0.0598166570,  0.0619602779,
+  0.0639444805,  0.0657690668,  0.0674525021,  0.0689664013,
+  0.0703533073,  0.0715826364,  0.0726774642,  0.0736406005,
+  0.0744664394,  0.0751576255,  0.0757305756,  0.0761748321,
+  0.0765050718,  0.0767204924,  0.0768230011,  0.0768173975,
+  0.0767093490,  0.0764992170,  0.0761992479,  0.0758008358,
+  0.0753137336,  0.0747452558,  0.0741003642,  0.0733620255,
+  0.0725682583,  0.0717002673,  0.0707628710,  0.0697630244,
+  0.0687043828,  0.0676075985,  0.0664367512,  0.0652247106,
+  0.0639715898,  0.0626857808,  0.0613455171,  0.0599837480,
+  0.0585915683,  0.0571616450,  0.0557173648,  0.0542452768,
+  0.0527630746,  0.0512556155,  0.0497385755,  0.0482165720,
+  0.0466843027,  0.0451488405,  0.0436097542,  0.0420649094,
+  0.0405349170,  0.0390053679,  0.0374812850,  0.0359697560,
+  0.0344620948,  0.0329754081,  0.0315017608,  0.0300502657,
+  0.0286072173,  0.0271859429,  0.0257875847,  0.0244160992,
+  0.0230680169,  0.0217467550,  0.0204531793,  0.0191872431,
+  0.0179433381,  0.0167324712,  0.0155405553,  0.0143904666,
+ -0.0132718220, -0.0121849995, -0.0111315548, -0.0101150215,
+ -0.0091325329, -0.0081798233, -0.0072615816, -0.0063792293,
+ -0.0055337211, -0.0047222596, -0.0039401124, -0.0031933778,
+ -0.0024826723, -0.0018039472, -0.0011568135, -0.0005464280,
+  0.0000276045,  0.0005832264,  0.0010902329,  0.0015784682,
+  0.0020274176,  0.0024508540,  0.0028446757,  0.0032091885,
+  0.0035401246,  0.0038456408,  0.0041251642,  0.0043801861,
+  0.0046039530,  0.0048109469,  0.0049839687,  0.0051382275,
+  0.0052715758,  0.0053838975,  0.0054753783,  0.0055404363,
+  0.0055917128,  0.0056266114,  0.0056389199,  0.0056455196,
+  0.0056220643,  0.0055938023,  0.0055475714,  0.0054876040,
+  0.0054196775,  0.0053471681,  0.0052461166,  0.0051407353,
+  0.0050393022,  0.0049137603,  0.0047932560,  0.0046606460,
+  0.0045209852,  0.0043730719,  0.0042264269,  0.0040819753,
+  0.0039207432,  0.0037603922,  0.0036008268,  0.0034418874,
+  0.0032739613,  0.0031125420,  0.0029469447,  0.0027870464,
+  0.0026201758,  0.0024625616,  0.0023017254,  0.0021461583,
+  0.0019841140,  0.0018348265,  0.0016868083,  0.0015443219,
+  0.0013902494,  0.0012577884,  0.0011250155,  0.0009885988,
+  0.0008608443,  0.0007458025,  0.0006239376,  0.0005107388,
+  0.0004026540,  0.0002949531,  0.0002043017,  0.0001094383,
+  0.0000134949, -0.0000617334, -0.0001446380, -0.0002098337,
+ -0.0002896981, -0.0003501175, -0.0004095121, -0.0004606325,
+ -0.0005145572, -0.0005564576, -0.0005946118, -0.0006341594,
+ -0.0006650415, -0.0006917937, -0.0007215391, -0.0007319357,
+ -0.0007530001, -0.0007630793, -0.0007757977, -0.0007801449,
+ -0.0007803664, -0.0007779869, -0.0007834332, -0.0007724848,
+ -0.0007681371, -0.0007490598, -0.0007440941, -0.0007255043,
+ -0.0007157736, -0.0006941614, -0.0006777690, -0.0006540333,
+ -0.0006312493, -0.0006132747, -0.0005870930, -0.0005677802,
+ -0.0005466565, -0.0005226564, -0.0005040714, -0.0004893791,
+ -0.0004875227, -0.0004947518, -0.0005617692, -0.000552528,
+};
+
+static const float sbr_noise_table[512][2] = {
+{-0.99986980746200, -0.36021610299715}, {-0.99969370862163,  0.98369989360250},
+{-0.99948153278296, -0.59483417516607}, {-0.99925837363824,  0.71084847864067},
+{-0.99909615720225, -0.96024605713970}, {-0.99899084509530,  0.74645156992493},
+{-0.99867974711855, -0.88147068645358}, {-0.99848471702976,  0.96245166923809},
+{-0.99829663752818,  0.01877138824311}, {-0.99715979260878, -0.93565784007648},
+{-0.99711581834508,  0.58211560180426}, {-0.99707579362824,  0.93237990079441},
+{-0.99646369485481,  0.84490533520752}, {-0.99635026409640, -0.58107730574765},
+{-0.99602956559179, -0.44654715757688}, {-0.99575054486311,  0.42389784469507},
+{-0.99571588506485,  0.52785521494349}, {-0.99500381284851, -0.02634122068550},
+{-0.99496513054797, -0.90071908066973}, {-0.99484402129368, -0.20052559254934},
+{-0.99264708948101, -0.99542822402536}, {-0.99196309146936,  0.67019017358456},
+{-0.99143875716818, -0.24945277239809}, {-0.99009048343881,  0.85868021604848},
+{-0.98935142339139, -0.40249159006933}, {-0.98892980586032, -0.87881132267556},
+{-0.98759606946049,  0.82288714303073}, {-0.98607857336230,  0.25034911730023},
+{-0.98512833386833, -0.99972330709594}, {-0.98298583762390,  0.21021524625209},
+{-0.98081380091130, -0.72856895534041}, {-0.97987214341034,  0.36526129686425},
+{-0.97790548600584,  0.96290806999242}, {-0.97757125224150,  0.05305894580606},
+{-0.97463695257310, -0.00190223301301}, {-0.97418588163217,  0.96468523666475},
+{-0.97346267944545, -0.96549364384098}, {-0.97327101028521,  0.12378128133110},
+{-0.97224737889348,  0.22081333579837}, {-0.97210735183243, -0.23095213067791},
+{-0.97087374418267,  0.86636445711364}, {-0.96926570524023,  0.73775654896574},
+{-0.96495267812511, -0.53960305946511}, {-0.96490920476211, -0.64020970923102},
+{-0.96440182703856, -0.94739918296622}, {-0.96062769559127,  0.36099095133739},
+{-0.95915368242257, -0.99237800466040}, {-0.95894428168140, -0.43265504344783},
+{-0.95789495447877, -0.20423194696966}, {-0.95531076805040,  0.90908757154593},
+{-0.95476662400101,  0.98364554179143}, {-0.95434497492853, -0.79607978501983},
+{-0.95053182488101, -0.96939905138082}, {-0.95038560288864,  0.89563219587625},
+{-0.94705089665984, -0.29580042814306}, {-0.94701139690956, -0.01826348194255},
+{-0.94063471614176,  0.41332338538963}, {-0.93941931782002, -0.56409379640356},
+{-0.93643603134666,  0.99870790442385}, {-0.93412041758744,  0.41374052024363},
+{-0.93388812549209, -0.84476541096429}, {-0.93307242253692,  0.49431757696466},
+{-0.93149731080767, -0.98313162570490}, {-0.92882402971423,  0.27871809078609},
+{-0.92444085484466, -0.10457590187436}, {-0.92404293670797,  0.85507704027855},
+{-0.92366023326932, -0.97979298068180}, {-0.91781958879280, -0.74012716684186},
+{-0.91592244254432,  0.03687901376713}, {-0.91529461447692, -0.15698707534206},
+{-0.91266367957293, -0.11522938140034}, {-0.90889593602546,  0.62034397054380},
+{-0.89746474625671,  0.99846578838537}, {-0.89054954257993, -0.31791913188064},
+{-0.89039863483811,  0.88866581484602}, {-0.89033658689697, -0.71656563987082},
+{-0.88590003188677,  0.47624600491382}, {-0.87979225745213, -0.71725725041680},
+{-0.87930772356786,  0.74748307690436}, {-0.87259289048043,  0.99233587353666},
+{-0.86875903507313, -0.20291699203564}, {-0.86811883080712,  0.39347308654705},
+{-0.86425093011245, -0.45795025029466}, {-0.86135454941237,  0.98947480909359},
+{-0.86110349531986,  0.42548583726477}, {-0.85913269895572,  0.95766566168880},
+{-0.85694974219574,  0.88844532719844}, {-0.85639281671058,  0.42429854760451},
+{-0.85359479233537,  0.88738125901579}, {-0.85283249275397,  0.91475563922421},
+{-0.85235410573336, -0.08342347966410}, {-0.84764345483665,  0.02372316801261},
+{-0.83822593578728, -0.01695043208885}, {-0.83782144651251,  0.97637632547466},
+{-0.83302725605608, -0.67330410892084}, {-0.82386352534327,  0.08924768823676},
+{-0.82002421836409, -0.73530179553767}, {-0.81689296271203, -0.90827703628298},
+{-0.81412430338535, -0.97480768049637}, {-0.81254441908887, -0.51627234660629},
+{-0.81093025665696,  0.08778370229363}, {-0.80692001248487, -0.32627540663214},
+{-0.80412329643109, -0.88513818199457}, {-0.79942778496547,  0.64323902822857},
+{-0.79732779473535, -0.91582524736159}, {-0.79598702973261,  0.97138411318894},
+{-0.79351832348816, -0.36208897989136}, {-0.78566324168507, -0.75568541079691},
+{-0.77149701404973, -0.33883658042801}, {-0.76910792026848, -0.96226617549298},
+{-0.76497004940162,  0.89210929242238}, {-0.76285492357887, -0.91371867919124},
+{-0.76191692573909,  0.99768118356265}, {-0.75725076534641,  0.53650549640587},
+{-0.75376385639978,  0.00814643438625}, {-0.74904939500519,  0.99985483641521},
+{-0.74774595569805, -0.26898062008959}, {-0.74490104699626,  0.94725911744610},
+{-0.74138124825523,  0.99310339807762}, {-0.72962208425191, -0.76608443420917},
+{-0.72929675029275, -0.98008272727324}, {-0.72094786237696, -0.35008961934255},
+{-0.71886586182037,  0.78030982480538}, {-0.71658965751996, -0.97788200391224},
+{-0.71573331064977, -0.98570608178923}, {-0.70801016548184,  0.33680685948117},
+{-0.70774918760427,  0.52548653416543}, {-0.70737398842068, -0.76547349325992},
+{-0.70467057786826,  0.93272777501857}, {-0.70395684036886,  0.58796798221039},
+{-0.70373594262891,  0.91227665827081}, {-0.70294374303036,  0.55359910445577},
+{-0.70051415345560, -0.45340028808763}, {-0.69506469500450,  0.98633412625459},
+{-0.68883758192426,  0.91338958840772}, {-0.68821476106884, -0.26892306315457},
+{-0.68774481731008, -0.30238837956299}, {-0.68683707712762,  0.80806944710339},
+{-0.67684928085260,  0.12631491649378}, {-0.67680188682972,  0.94502052337695},
+{-0.67414626793544,  0.49548221180078}, {-0.66694269691195, -0.91643611810148},
+{-0.66393410674885, -0.08249679629081}, {-0.65889129659168,  0.58835634138583},
+{-0.65501142790847,  0.82546114655624}, {-0.65298804552119, -0.18439575450921},
+{-0.65284592392918,  0.52186723253637}, {-0.65185446735885, -0.88734990773289},
+{-0.64946246527458,  0.68645507104960}, {-0.64612616129736,  0.72198674804544},
+{-0.63431466947340,  0.21079116459234}, {-0.63364968534650, -0.16473594423746},
+{-0.62928247730667,  0.13627037407335}, {-0.62858772103030,  0.38765693387102},
+{-0.62228872928622,  0.82767262846661}, {-0.62126416356920, -0.59893681700392},
+{-0.62081581361840,  0.75000676218956}, {-0.61510362277374, -0.89515019899997},
+{-0.60182204677608,  0.86779651036123}, {-0.59174397685714, -0.89405370422752},
+{-0.58947456517751, -0.34847132454388}, {-0.58753191905341,  0.01290772389163},
+{-0.58660709669728,  0.96840773806582}, {-0.56649614128386, -0.90494866361587},
+{-0.56049829194163,  0.82522301569036}, {-0.55126773094930, -0.98898543862153},
+{-0.54671580548181, -0.02570928536004}, {-0.54668414224090,  0.95980774020221},
+{-0.53697829178752, -0.97649903936228}, {-0.53499621979720,  0.97241553731237},
+{-0.53282156061942, -0.91423265091354}, {-0.50608540105128, -0.65846015480300},
+{-0.50254500772635, -0.88829338134294}, {-0.49429560226497,  0.98183865291903},
+{-0.49104783137150,  0.32895214359663}, {-0.48972893932274,  0.56289246362686},
+{-0.48202429536989, -0.96805608884164}, {-0.48086065601423, -0.98848504923531},
+{-0.47770832416973, -0.16789556203025}, {-0.47339353684664, -0.85904328834047},
+{-0.47005496701697, -0.37340549728647}, {-0.46353441212724, -0.95249041539006},
+{-0.45725933317144, -0.56716323646760}, {-0.44976380954860,  0.40894572671545},
+{-0.44607178518598, -0.54233252016394}, {-0.44132783753414, -0.92688840659280},
+{-0.43867015250812,  0.99998069244322}, {-0.43645594360633, -0.94805030113284},
+{-0.43311260380975,  0.85321815947490}, {-0.42102998829339,  0.99720941999394},
+{-0.41815140454465,  0.16276422358861}, {-0.41368337314182,  0.28216837680365},
+{-0.40761056640505, -0.90045573444695}, {-0.39922954514662,  0.94129601616966},
+{-0.39466529740375, -0.66809432114456}, {-0.38981478896926,  0.89572605717087},
+{-0.38684144784738,  0.51337349030406}, {-0.38658751133527,  0.99501571208985},
+{-0.38299976567017,  0.98516909715427}, {-0.37024464187437, -0.87071656222959},
+{-0.36834336949252,  0.96458298020975}, {-0.36345126374441,  0.64874435357162},
+{-0.36326018419965,  0.07440243123228}, {-0.36024828242896,  0.34655735648287},
+{-0.35899413170555, -0.46633226649613}, {-0.35712514743563,  0.19298963768574},
+{-0.34875585502238,  0.71472290693300}, {-0.34600785879594, -0.99441426144200},
+{-0.34110827591623,  0.40211222807691}, {-0.33813265086024, -0.38661779441897},
+{-0.32070666698656,  0.50143421908753}, {-0.31744434966056, -0.36834111883652},
+{-0.30963073129751, -0.18076720599336}, {-0.30889773919437, -0.80664389776860},
+{-0.30102157304644, -0.07667808922205}, {-0.29268293575672,  0.05759224927952},
+{-0.29250814029851,  0.37444994344615}, {-0.27338148835532,  0.99950922447209},
+{-0.26843291251234,  0.83115668004362}, {-0.26719850873357,  0.68903369776193},
+{-0.26240603062237, -0.92774095379098}, {-0.26240034795124, -0.68264554369108},
+{-0.26024169633417, -0.75999759855752}, {-0.24664412953388, -0.87642273115183},
+{-0.24523839572639,  0.63206633394807}, {-0.22898461455054,  0.97054853316316},
+{-0.22827527843994,  0.18874759397997}, {-0.22225968841114,  0.57124029781228},
+{-0.21411126572790, -0.93424819052545}, {-0.20651349620689,  0.54593044066355},
+{-0.20461677199539, -0.14209977628489}, {-0.18509915019881,  0.47565762892084},
+{-0.18173078152226, -0.26152145156800}, {-0.17573736667267, -0.48166920859485},
+{-0.17325552859616, -0.92770672250494}, {-0.16796458968998, -0.98987511890470},
+{-0.16258217500792, -0.95939125400802}, {-0.15808569732583,  0.85279555024382},
+{-0.12016920576437, -0.57147322153312}, {-0.11708371046774, -0.99800843444966},
+{-0.11566039853896,  0.28587846253726}, {-0.10488238045009, -0.87769947402394},
+{-0.09215968531446,  0.69540012101253}, {-0.08750604656825,  0.97686944362527},
+{-0.07706847005931, -0.89581437101329}, {-0.07588948563079,  0.74096214084170},
+{-0.07064096339021, -0.94479803205886}, {-0.06618622548177, -0.23812217221359},
+{-0.06449863579434,  0.03250560813135}, {-0.05890199924154,  0.70741827819497},
+{-0.05826828420146, -0.06940774188029}, {-0.05146538187944, -0.92599700120679},
+{-0.03841517601843, -0.69888815681179}, {-0.03305738840705, -0.37205262859764},
+{-0.01752795995444, -0.82616635284178}, {-0.01689629065389,  0.00287506445732},
+{-0.01115998681937,  0.98496019742444}, {-0.01053049862020, -0.66959058036166},
+{-0.00848591195325, -0.76670128000486}, { 0.00287840603348,  0.64768261158166},
+{ 0.00563771969365,  0.61768196727244}, { 0.01995873238855,  0.85223515096765},
+{ 0.02416275806869,  0.27192914288905}, { 0.02704554141885, -0.05417518053666},
+{ 0.03004475787316, -0.99738896333384}, { 0.03273375457980, -0.74933109564108},
+{ 0.03574995626194, -0.97325616900959}, { 0.03671907158312,  0.63606389366675},
+{ 0.04585228574211,  0.99812337444082}, { 0.05198933055162,  0.21269661669964},
+{ 0.05237237274947, -0.25640361602661}, { 0.05946491307025,  0.20511047074866},
+{ 0.06310802338302, -0.54539587529618}, { 0.07069442601050, -0.78247898470706},
+{ 0.07223051368337, -0.88805001733626}, { 0.07717324253925,  0.58638399856595},
+{ 0.08707806671691,  0.80938994918745}, { 0.09140039465500, -0.20537731453108},
+{ 0.09355476558534,  0.54845123045604}, { 0.09638062008048,  0.04411984381457},
+{ 0.10407960510582,  0.77357793811619}, { 0.10831862810749, -0.08628837174592},
+{ 0.11112534735126,  0.21484763313301}, { 0.11657770663191, -0.83662833815041},
+{ 0.11960319006843,  0.99899346780168}, { 0.14130051758487, -0.95090983575689},
+{ 0.14323651387360, -0.94145598222488}, { 0.17005239424212,  0.54683053962658},
+{ 0.17006334670615, -0.76854025314829}, { 0.19050361015753,  0.01602615387195},
+{ 0.19513029146934, -0.94239832251867}, { 0.20320105410437, -0.86879180355289},
+{ 0.20541973692630, -0.94435144369918}, { 0.21356749817493, -0.90716295627033},
+{ 0.24133038992960,  0.51294362630238}, { 0.25872675063360,  0.99893303933816},
+{ 0.26559203620024,  0.73314307966524}, { 0.27572582416567,  0.58634753335832},
+{ 0.28038443336943,  0.14537913654427}, { 0.28058259829990, -0.85361420634036},
+{ 0.28398686150148,  0.34633555702188}, { 0.28631285179909, -0.91035047143603},
+{ 0.28909646383717,  0.96307783970534}, { 0.29078277605775,  0.35393777921520},
+{ 0.29938434065514, -0.46051329682246}, { 0.30424629369539, -0.49438267012479},
+{ 0.30485754879632, -0.70540034357529}, { 0.30872163214726,  0.41514960556126},
+{ 0.33408042438752,  0.86185953874709}, { 0.33454804933804, -0.86231516800408},
+{ 0.34581177741673,  0.94879421061866}, { 0.36703583957424, -0.38653265641875},
+{ 0.36722871286923,  0.65291654172961}, { 0.37050990604091, -0.59910140383171},
+{ 0.38879779059045,  0.97274429344593}, { 0.38975993093975,  0.95515358099121},
+{ 0.39677256130792, -0.74854668609359}, { 0.39701421446381,  0.81779634174316},
+{ 0.40009252867955, -0.98929400334421}, { 0.40616991671205, -0.26469008598449},
+{ 0.41025800019463,  0.02116736935734}, { 0.41881284182683,  0.02188098922282},
+{ 0.42188998312520,  0.48148651230437}, { 0.42215817594807, -0.07712787385208},
+{ 0.43080003649976, -0.21919095636638}, { 0.43440904467688, -0.98546330463232},
+{ 0.43793861458754, -0.78904969892724}, { 0.44183099021786, -0.93568974498761},
+{ 0.44226800932956,  0.71326756742752}, { 0.44844799194357,  0.99211574628306},
+{ 0.46332038247497,  0.10964126185063}, { 0.46857766746029, -0.30140233457198},
+{ 0.47162891065108, -0.18680204049569}, { 0.47336129371299, -0.27333178296162},
+{ 0.47546946844938,  0.68613044836811}, { 0.47788757329038, -0.46333147839295},
+{ 0.47803883714199, -0.39423219786288}, { 0.48708332746299,  0.99999041579432},
+{ 0.49072334613242, -0.45359708737775}, { 0.50449166760303, -0.85995072408434},
+{ 0.50843233159162,  0.96107691266205}, { 0.51646184922287, -0.71373332873917},
+{ 0.51756627678691,  0.66926784710139}, { 0.51992825347895,  0.80247631400510},
+{ 0.52175424682195, -0.85396826735705}, { 0.52890520960295,  0.60048872455592},
+{ 0.54473080610200, -0.11919206037186}, { 0.54840422910309,  0.75221367176302},
+{ 0.55266258627194,  0.59449057465591}, { 0.55526940659947,  0.78891523734774},
+{ 0.56151770568316, -0.70693811747778}, { 0.56721979748394, -0.24076836414499},
+{ 0.57397389364339,  0.35289703373760}, { 0.57563307626120, -0.91034337352097},
+{ 0.57593163224487, -0.98966422921509}, { 0.61130721139669,  0.46950141175917},
+{ 0.62319537462542, -0.93098313552599}, { 0.62664209577999, -0.74402970906471},
+{ 0.63872359151636,  0.08128252493444}, { 0.64016792079480,  0.15649530836856},
+{ 0.64880119792759,  0.41336660830571}, { 0.65269447475094,  0.65916004833932},
+{ 0.65675089314631,  0.18331637134880}, { 0.66223843141647,  0.72528579940326},
+{ 0.66565033746925,  0.64652935542491}, { 0.66614891079092,  0.96590176169121},
+{ 0.66929266740477,  0.98629493401748}, { 0.66951124390363,  0.98905825623893},
+{ 0.67226861393788, -0.13494389011014}, { 0.67248046289143, -0.03646211390569},
+{ 0.67439478141121, -0.81684380846796}, { 0.67709491937357, -0.95478075822906},
+{ 0.67866860118215,  0.81284503870856}, { 0.68865791458395,  0.55660316809678},
+{ 0.69000803499316,  0.90952171386132}, { 0.70176989408455, -0.20453028573322},
+{ 0.72165342518718, -0.69259857349564}, { 0.72556974415690, -0.99899555770747},
+{ 0.72683747733879, -0.48060774432251}, { 0.73504123909879, -0.03747203173192},
+{ 0.73538215752630,  0.96452072373404}, { 0.74238552914587,  0.04491915291044},
+{ 0.74496252926055, -0.91169004445807}, { 0.74649464155061,  0.12144893606462},
+{ 0.74922239129237, -0.89879858826087}, { 0.75081145286948, -0.35533223142265},
+{ 0.75087906691890, -0.29612672982396}, { 0.75214681811150, -0.99955681042665},
+{ 0.75374316974495,  0.96705214651335}, { 0.75622801399036,  0.20950329995549},
+{ 0.76343198951445,  0.41746629422634}, { 0.76789609461795, -0.76519356730966},
+{ 0.77638976371966,  0.94321834873819}, { 0.78368131392666,  0.45506999802597},
+{ 0.78843311019251,  0.52851398958271}, { 0.79788337195331, -0.93180971199849},
+{ 0.80001773566818,  0.91542195141039}, { 0.80099335254678, -0.36851896710853},
+{ 0.80705063769351,  0.29653668284408}, { 0.80723395114371, -0.24717418514605},
+{ 0.81014640078925,  0.53722648362443}, { 0.81505484574602, -0.94685947861369},
+{ 0.81924990025724,  0.99698425250579}, { 0.82068619590515, -0.85087787994476},
+{ 0.82104905483590,  0.99540741724928}, { 0.82368298622748, -0.74036047190173},
+{ 0.82411158711197,  0.96654618432562}, { 0.82907767600783, -0.06323442598128},
+{ 0.83081876925833, -0.94780851414763}, { 0.83434292401346, -0.13023450646997},
+{ 0.83524300028228,  0.83702537075163}, { 0.83996497984604,  0.55839849139647},
+{ 0.84329189340667,  0.10406957462213}, { 0.85126435782309,  0.52349251543547},
+{ 0.85256524470573, -0.64567607735589}, { 0.85289650925190,  0.46766131791044},
+{ 0.86557171579452,  0.55593866696299}, { 0.86849774348749, -0.18333598647899},
+{ 0.87392477144549, -0.12796173740361}, { 0.87434794743625,  0.89399495655433},
+{ 0.88461574003963,  0.57508405276414}, { 0.88547373760759, -0.89636802901469},
+{ 0.88866808958124, -0.99735267083226}, { 0.89065051931895,  0.52783352697585},
+{ 0.89110648599879, -0.97894250343044}, { 0.89162532251878,  0.54950955570563},
+{ 0.90145509409859,  0.99781390365446}, { 0.90998308703519,  0.96715662938132},
+{ 0.91229417540436, -0.65987351408410}, { 0.91328082618125, -0.99839597361769},
+{ 0.91400366022124,  0.57972471346930}, { 0.91427159529618, -0.98290505544444},
+{ 0.91599807087376, -0.98147830385781}, { 0.91653180367913, -0.30587628726597},
+{ 0.91697008020594,  0.17514097332009}, { 0.91783042091762, -0.46356892383970},
+{ 0.91851997982317,  0.09358228901785}, { 0.91980081243087,  0.66507455644919},
+{ 0.92045124735495, -0.62433652524220}, { 0.93310180125532, -0.99913308068246},
+{ 0.93335049681047, -0.43537023883588}, { 0.93379635304810, -0.70881994583682},
+{ 0.93885443798188, -0.74895312615259}, { 0.93887685615875, -0.11284528204636},
+{ 0.93891760988045, -0.89968353740388}, { 0.94044946687963,  0.09026201157416},
+{ 0.94138021032330,  0.35281916733018}, { 0.94214511408023, -0.99696425367461},
+{ 0.94292565553160,  0.83163906518293}, { 0.94813650221268, -0.97506640027128},
+{ 0.95258164539612, -0.54893416026939}, { 0.95404443402072,  0.49162765398743},
+{ 0.95424048234441, -0.99240147091219}, { 0.95432193457128,  0.68849603408441},
+{ 0.95437383549973,  0.97002324109952}, { 0.95472308713099, -0.08588776019550},
+{ 0.95538108220960, -0.85557052096538}, { 0.95781667469567, -0.79047927052628},
+{ 0.95903308477986,  0.76744927173873}, { 0.95964737821728, -0.35435818285502},
+{ 0.96063943315511,  0.93116709541280}, { 0.96349973642406,  0.96640458041000},
+{ 0.96361882270190,  0.40706967140989}, { 0.96367554763201,  0.38486749303242},
+{ 0.96735248738388,  0.90891816978629}, { 0.97079252950321,  0.09325857238682},
+{ 0.97113454393991, -0.67528515225647}, { 0.97139128574778,  0.80093621198236},
+{ 0.97312313923635, -0.99548191630031}, { 0.97394027897442, -0.26695944086561},
+{ 0.97401082477563, -0.16855870075190}, { 0.97534253457837,  0.86150930812689},
+{ 0.97686402381843, -0.28664534366620}, { 0.97875325649683, -0.72467840967746},
+{ 0.97960898850996, -0.94021446752851}, { 0.98111043100884, -0.95854459734407},
+{ 0.98164490790123, -0.77463155528697}, { 0.98299195879514,  0.01989791390047},
+{ 0.98398893639988,  0.39467440420569}, { 0.98590090577724,  0.88241721133981},
+{ 0.98630563232075, -0.53578899600662}, { 0.98720684660488,  0.48409133691962},
+{ 0.98838086953732,  0.99994349600236}, { 0.99010736374716,  0.04602397576623},
+{ 0.99089197565987, -0.92062625581587}, { 0.99096131449250, -0.19451182854402},
+{ 0.99186510988782,  0.72023001312947}, { 0.99194979673836,  0.91876249766422},
+{ 0.99206463477946, -0.57010120849429}, { 0.99267657565094, -0.03757034316958},
+{ 0.99298717043688,  0.40816610075661}, { 0.99430266919728,  0.18812555317553},
+{ 0.99498012188353,  0.97094358113387}, { 0.99570534804836,  0.45844586038111},
+{ 0.99578905365569, -0.54106826257356}, { 0.99642466504163,  0.70190043427512},
+{ 0.99740224117019, -0.16830494996370}, { 0.99743281016846,  0.42697157037567},
+{ 0.99793873738654,  0.90041310491497}, { 0.99817310731176,  0.61133572482148},
+{ 0.99846021905254, -0.98725746254433}, { 0.99881175120751,  0.88993983831354},
+{ 0.99885650204884,  0.11136095490444}, { 0.99923472302773, -0.80142993767554},
+{ 0.99930381973804,  0.94578896296649}, { 0.99947295749905,  0.93625229707912},
+{ 0.99948035025744,  0.58285545563426}, { 0.99956173327206, -0.12358542001404},
+{ 0.99971463703691, -0.83889954253462}, { 0.99979044674350, -0.14130287347405},
+{ 0.99980371023351,  0.79835913565599}, { 0.99992588229018, -0.26281872094289},
+};
+
+#endif /* AVCODEC_AACSBRDATA_H */

Added: sbr-wip/checkout.sh
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ sbr-wip/checkout.sh	Sat Sep 26 16:59:27 2009	(r5388)
@@ -0,0 +1,9 @@
+#! /bin/sh
+
+echo "Checking out FFmpeg..."
+svn co svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg -r 20025
+echo "Copying SBR files into FFmpeg tree..."
+cp aacsbr*.{c,h} ffmpeg/libavcodec
+echo "Applying SBR and build system patch"
+cd ffmpeg; patch -p0 < ../ffmpeg.diff
+echo "All done! Now configure and make FFmpeg"

Added: sbr-wip/ffmpeg.diff
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ sbr-wip/ffmpeg.diff	Sat Sep 26 16:59:27 2009	(r5388)
@@ -0,0 +1,157 @@
+Index: libavcodec/aac.c
+===================================================================
+--- libavcodec/aac.c	(revision 20025)
++++ libavcodec/aac.c	(working copy)
+@@ -62,7 +62,7 @@
+  * N                    MIDI
+  * N                    Harmonic and Individual Lines plus Noise
+  * N                    Text-To-Speech Interface
+- * N (in progress)      Spectral Band Replication
++ * N (code in SoC repo) Spectral Band Replication
+  * Y (not in this code) Layer-1
+  * Y (not in this code) Layer-2
+  * Y (not in this code) Layer-3
+@@ -85,6 +85,8 @@
+ #include "aac.h"
+ #include "aactab.h"
+ #include "aacdectab.h"
++#include "aacsbr.h"
++#include "aacsbrdata.h"
+ #include "mpeg4audio.h"
+ #include "aac_parser.h"
+ 
+@@ -101,7 +103,11 @@
+ static VLC vlc_scalefactors;
+ static VLC vlc_spectral[11];
+ 
++extern av_cold void ff_aac_sbr_init();
++extern int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
++                                GetBitContext *gb, int crc, int cnt, int id_aac);
+ 
++
+ static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
+ {
+     static const int8_t tags_per_config[16] = { 0, 1, 1, 2, 3, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0 };
+@@ -474,6 +480,7 @@
+     avccontext->sample_fmt = SAMPLE_FMT_S16;
+     avccontext->frame_size = 1024;
+ 
++    // main AAC VLC table initialization
+     AAC_INIT_VLC_STATIC( 0, 144);
+     AAC_INIT_VLC_STATIC( 1, 114);
+     AAC_INIT_VLC_STATIC( 2, 188);
+@@ -486,6 +493,8 @@
+     AAC_INIT_VLC_STATIC( 9, 248);
+     AAC_INIT_VLC_STATIC(10, 384);
+ 
++    ff_aac_sbr_init();
++
+     dsputil_init(&ac->dsp, avccontext);
+ 
+     ac->random_state = 0x1f2e3d4c;
+@@ -1281,23 +1290,6 @@
+ }
+ 
+ /**
+- * Decode Spectral Band Replication extension data; reference: table 4.55.
+- *
+- * @param   crc flag indicating the presence of CRC checksum
+- * @param   cnt length of TYPE_FIL syntactic element in bytes
+- *
+- * @return  Returns number of bytes consumed from the TYPE_FIL element.
+- */
+-static int decode_sbr_extension(AACContext *ac, GetBitContext *gb,
+-                                int crc, int cnt)
+-{
+-    // TODO : sbr_extension implementation
+-    av_log_missing_feature(ac->avccontext, "SBR", 0);
+-    skip_bits_long(gb, 8 * cnt - 4); // -4 due to reading extension type
+-    return cnt;
+-}
+-
+-/**
+  * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
+  *
+  * @return  Returns number of bytes consumed.
+@@ -1377,7 +1369,8 @@
+  *
+  * @return Returns number of bytes consumed
+  */
+-static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt)
++static int decode_extension_payload(AACContext * ac, GetBitContext * gb,
++                                    int cnt, int id, int tag)
+ {
+     int crc_flag = 0;
+     int res = cnt;
+@@ -1385,7 +1378,7 @@
+     case EXT_SBR_DATA_CRC:
+         crc_flag++;
+     case EXT_SBR_DATA:
+-        res = decode_sbr_extension(ac, gb, crc_flag, cnt);
++        res = ff_decode_sbr_extension(ac, &ac->che[id][tag]->sbr, gb, crc_flag, cnt, id);
+         break;
+     case EXT_DYNAMIC_RANGE:
+         res = decode_dynamic_range(&ac->che_drc, gb, cnt);
+@@ -1674,8 +1667,8 @@
+     AACContext *ac = avccontext->priv_data;
+     ChannelElement *che = NULL;
+     GetBitContext gb;
+-    enum RawDataBlockType elem_type;
+-    int err, elem_id, data_size_tmp;
++    enum RawDataBlockType elem_type, elem_type_prev = 0;
++    int err, elem_id, elem_id_prev = 0, data_size_tmp;
+ 
+     init_get_bits(&gb, buf, buf_size * 8);
+ 
+@@ -1739,7 +1732,7 @@
+             if (elem_id == 15)
+                 elem_id += get_bits(&gb, 8) - 1;
+             while (elem_id > 0)
+-                elem_id -= decode_extension_payload(ac, &gb, elem_id);
++                elem_id -= decode_extension_payload(ac, &gb, elem_id, elem_type_prev, elem_id_prev);
+             err = 0; /* FIXME */
+             break;
+ 
+@@ -1748,6 +1741,9 @@
+             break;
+         }
+ 
++        elem_type_prev = elem_type;
++        elem_id_prev   = elem_id;
++
+         if (err)
+             return err;
+     }
+Index: libavcodec/aac.h
+===================================================================
+--- libavcodec/aac.h	(revision 20025)
++++ libavcodec/aac.h	(working copy)
+@@ -33,6 +33,7 @@
+ #include "avcodec.h"
+ #include "dsputil.h"
+ #include "mpeg4audio.h"
++#include "aacsbr.h"
+ 
+ #include <stdint.h>
+ 
+@@ -221,6 +222,7 @@
+     SingleChannelElement ch[2];
+     // CCE specific
+     ChannelCoupling coup;
++    SpectralBandReplication sbr;
+ } ChannelElement;
+ 
+ /**
+Index: libavcodec/Makefile
+===================================================================
+--- libavcodec/Makefile	(revision 20025)
++++ libavcodec/Makefile	(working copy)
+@@ -35,7 +35,7 @@
+ OBJS-$(CONFIG_VDPAU)                   += vdpau.o
+ 
+ # decoders/encoders/hardware accelerators
+-OBJS-$(CONFIG_AAC_DECODER)             += aac.o aactab.o mpeg4audio.o aac_parser.o aac_ac3_parser.o
++OBJS-$(CONFIG_AAC_DECODER)             += aac.o aactab.o aacsbr.o mpeg4audio.o aac_parser.o aac_ac3_parser.o
+ OBJS-$(CONFIG_AAC_ENCODER)             += aacenc.o aaccoder.o aacpsy.o aactab.o psymodel.o iirfilter.o mpeg4audio.o
+ OBJS-$(CONFIG_AASC_DECODER)            += aasc.o msrledec.o
+ OBJS-$(CONFIG_AC3_DECODER)             += ac3dec.o ac3tab.o ac3dec_data.o ac3.o ac3_parser.o aac_ac3_parser.o


More information about the FFmpeg-soc mailing list