From ed492b61dbe64ef8c25eb1e72ac7ca4e9df9bbd6 Mon Sep 17 00:00:00 2001 From: Alex Converse Date: Mon, 8 Mar 2010 04:33:02 +0000 Subject: [PATCH] Add an HE-AAC v1 decoder. A large portion of this code was orignally authored by Robert Swain. The rest was written by me. Full history is available at: svn://svn.ffmpeg.org/soc/aac-sbr http://github.com/aconverse/ffmpeg-heaac/tree/sbr_pub Originally committed as revision 22316 to svn://svn.ffmpeg.org/ffmpeg/trunk --- Changelog | 1 + libavcodec/Makefile | 2 +- libavcodec/aac.c | 83 +- libavcodec/aac.h | 4 +- libavcodec/aacsbr.c | 1759 +++++++++++++++++++++++++++++++++++++++ libavcodec/aacsbr.h | 51 ++ libavcodec/aacsbrdata.h | 613 ++++++++++++++ libavcodec/avcodec.h | 2 +- libavcodec/mpeg4audio.h | 2 +- libavcodec/sbr.h | 187 +++++ 10 files changed, 2669 insertions(+), 35 deletions(-) create mode 100644 libavcodec/aacsbr.c create mode 100644 libavcodec/aacsbr.h create mode 100644 libavcodec/aacsbrdata.h create mode 100644 libavcodec/sbr.h diff --git a/Changelog b/Changelog index cb276da2e2..f1628f79ce 100644 --- a/Changelog +++ b/Changelog @@ -61,6 +61,7 @@ version : - FFprobe tool - AMR-NB decoder - RTSP muxer +- HE-AAC v1 decoder diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 5eeb1437ad..3931aabe50 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -41,7 +41,7 @@ OBJS-$(CONFIG_VAAPI) += vaapi.o OBJS-$(CONFIG_VDPAU) += vdpau.o # decoders/encoders/hardware accelerators -OBJS-$(CONFIG_AAC_DECODER) += aac.o aactab.o +OBJS-$(CONFIG_AAC_DECODER) += aac.o aactab.o aacsbr.o OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o \ aacpsy.o aactab.o \ psymodel.o iirfilter.o \ diff --git a/libavcodec/aac.c b/libavcodec/aac.c index c4755420b4..dbaffeedc6 100644 --- a/libavcodec/aac.c +++ b/libavcodec/aac.c @@ -62,7 +62,7 @@ * N MIDI * N Harmonic and Individual Lines plus Noise * N Text-To-Speech Interface - * N (in progress) Spectral Band Replication + * Y Spectral Band Replication * Y (not in this code) Layer-1 * Y (not in this code) Layer-2 * Y (not in this code) Layer-3 @@ -86,6 +86,8 @@ #include "aac.h" #include "aactab.h" #include "aacdectab.h" +#include "sbr.h" +#include "aacsbr.h" #include "mpeg4audio.h" #include "aac_parser.h" @@ -180,14 +182,18 @@ static av_cold int che_configure(AACContext *ac, if (che_pos[type][id]) { if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement)))) return AVERROR(ENOMEM); + ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr); if (type != TYPE_CCE) { ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret; if (type == TYPE_CPE) { ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret; } } - } else + } else { + if (ac->che[type][id]) + ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr); av_freep(&ac->che[type][id]); + } return 0; } @@ -530,6 +536,8 @@ static av_cold int aac_decode_init(AVCodecContext *avccontext) AAC_INIT_VLC_STATIC( 9, 366); AAC_INIT_VLC_STATIC(10, 462); + ff_aac_sbr_init(); + dsputil_init(&ac->dsp, avccontext); ac->random_state = 0x1f2e3d4c; @@ -1544,23 +1552,6 @@ static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che) return 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. - */ -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. * @@ -1641,7 +1632,8 @@ static int decode_dynamic_range(DynamicRangeControl *che_drc, * * @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, + ChannelElement *che, enum RawDataBlockType elem_type) { int crc_flag = 0; int res = cnt; @@ -1649,7 +1641,21 @@ static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt) case EXT_SBR_DATA_CRC: crc_flag++; case EXT_SBR_DATA: - res = decode_sbr_extension(ac, gb, crc_flag, cnt); + if (!che) { + av_log(ac->avccontext, AV_LOG_ERROR, "SBR was found before the first channel element.\n"); + return res; + } else if (!ac->m4ac.sbr) { + av_log(ac->avccontext, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n"); + skip_bits_long(gb, 8 * cnt - 4); + return res; + } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) { + av_log(ac->avccontext, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n"); + skip_bits_long(gb, 8 * cnt - 4); + return res; + } else { + ac->m4ac.sbr = 1; + } + res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type); break; case EXT_DYNAMIC_RANGE: res = decode_dynamic_range(&ac->che_drc, gb, cnt); @@ -1830,8 +1836,9 @@ static void apply_independent_coupling(AACContext *ac, const float bias = ac->add_bias; const float *src = cce->ch[0].ret; float *dest = target->ret; + const int len = 1024 << (ac->m4ac.sbr == 1); - for (i = 0; i < 1024; i++) + for (i = 0; i < len; i++) dest[i] += gain * (src[i] - bias); } @@ -1889,10 +1896,18 @@ static void spectral_to_sample(AACContext *ac) apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1); if (type <= TYPE_CPE) apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling); - if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) + if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) { imdct_and_windowing(ac, &che->ch[0]); - if (type == TYPE_CPE) + if (ac->m4ac.sbr > 0) { + ff_sbr_dequant(ac, &che->sbr, type == TYPE_CPE ? TYPE_CPE : TYPE_SCE); + ff_sbr_apply(ac, &che->sbr, 0, che->ch[0].ret, che->ch[0].ret); + } + } + if (type == TYPE_CPE) { imdct_and_windowing(ac, &che->ch[1]); + if (ac->m4ac.sbr > 0) + ff_sbr_apply(ac, &che->sbr, 1, che->ch[1].ret, che->ch[1].ret); + } if (type <= TYPE_CCE) apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling); } @@ -1942,9 +1957,9 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; AACContext *ac = avccontext->priv_data; - ChannelElement *che = NULL; + ChannelElement *che = NULL, *che_prev = NULL; GetBitContext gb; - enum RawDataBlockType elem_type; + enum RawDataBlockType elem_type, elem_type_prev = TYPE_END; int err, elem_id, data_size_tmp; int buf_consumed; int samples = 1024, multiplier; @@ -2014,7 +2029,7 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, return -1; } while (elem_id > 0) - elem_id -= decode_extension_payload(ac, &gb, elem_id); + elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev); err = 0; /* FIXME */ break; @@ -2023,6 +2038,9 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, break; } + che_prev = che; + elem_type_prev = elem_type; + if (err) return err; @@ -2034,14 +2052,14 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, spectral_to_sample(ac); - multiplier = 0; + multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0; samples <<= multiplier; if (ac->output_configured < OC_LOCKED) { avccontext->sample_rate = ac->m4ac.sample_rate << multiplier; avccontext->frame_size = samples; } - data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t); + data_size_tmp = samples * avccontext->channels * sizeof(int16_t); if (*data_size < data_size_tmp) { av_log(avccontext, AV_LOG_ERROR, "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n", @@ -2050,7 +2068,7 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, } *data_size = data_size_tmp; - ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels); + ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avccontext->channels); if (ac->output_configured) ac->output_configured = OC_LOCKED; @@ -2065,8 +2083,11 @@ static av_cold int aac_decode_close(AVCodecContext *avccontext) int i, type; for (i = 0; i < MAX_ELEM_ID; i++) { - for (type = 0; type < 4; type++) + for (type = 0; type < 4; type++) { + if (ac->che[type][i]) + ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr); av_freep(&ac->che[type][i]); + } } ff_mdct_end(&ac->mdct); diff --git a/libavcodec/aac.h b/libavcodec/aac.h index ec43e5fe99..4779cbfc57 100644 --- a/libavcodec/aac.h +++ b/libavcodec/aac.h @@ -34,6 +34,7 @@ #include "dsputil.h" #include "fft.h" #include "mpeg4audio.h" +#include "sbr.h" #include @@ -217,7 +218,7 @@ typedef struct { uint8_t zeroes[128]; ///< band is not coded (used by encoder) DECLARE_ALIGNED(16, float, coeffs)[1024]; ///< coefficients for IMDCT DECLARE_ALIGNED(16, float, saved)[1024]; ///< overlap - DECLARE_ALIGNED(16, float, ret)[1024]; ///< PCM output + DECLARE_ALIGNED(16, float, ret)[2048]; ///< PCM output PredictorState predictor_state[MAX_PREDICTORS]; } SingleChannelElement; @@ -233,6 +234,7 @@ typedef struct { SingleChannelElement ch[2]; // CCE specific ChannelCoupling coup; + SpectralBandReplication sbr; } ChannelElement; /** diff --git a/libavcodec/aacsbr.c b/libavcodec/aacsbr.c new file mode 100644 index 0000000000..e317a5c72f --- /dev/null +++ b/libavcodec/aacsbr.c @@ -0,0 +1,1759 @@ +/* + * AAC Spectral Band Replication decoding functions + * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl ) + * Copyright (c) 2009-2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file libavcodec/aacsbr.c + * AAC Spectral Band Replication decoding functions + * @author Robert Swain ( rob opendot cl ) + */ + +#include "aac.h" +#include "sbr.h" +#include "aacsbr.h" +#include "aacsbrdata.h" + +#include +#include + +#define ENVELOPE_ADJUSTMENT_OFFSET 2 +#define NOISE_FLOOR_OFFSET 6.0f + +/** + * 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, +}; + +enum { + EXTENSION_ID_PS = 2, +}; + +static VLC vlc_sbr[10]; +static const int8_t vlc_sbr_lav[10] = + { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 }; +static DECLARE_ALIGNED(16, float, analysis_cos_pre)[64]; +static DECLARE_ALIGNED(16, float, analysis_sin_pre)[64]; +static DECLARE_ALIGNED(16, float, analysis_cossin_post)[32][2]; +static const DECLARE_ALIGNED(16, float, zero64)[64]; + +#define SBR_INIT_VLC_STATIC(num, size) \ + 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 SBR_VLC_ROW(name) \ + { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) } + +av_cold void ff_aac_sbr_init(void) +{ + int n, k; + static const struct { + const void *sbr_codes, *sbr_bits; + const unsigned int table_size, elem_size; + } sbr_tmp[] = { + SBR_VLC_ROW(t_huffman_env_1_5dB), + SBR_VLC_ROW(f_huffman_env_1_5dB), + SBR_VLC_ROW(t_huffman_env_bal_1_5dB), + SBR_VLC_ROW(f_huffman_env_bal_1_5dB), + SBR_VLC_ROW(t_huffman_env_3_0dB), + SBR_VLC_ROW(f_huffman_env_3_0dB), + SBR_VLC_ROW(t_huffman_env_bal_3_0dB), + SBR_VLC_ROW(f_huffman_env_bal_3_0dB), + SBR_VLC_ROW(t_huffman_noise_3_0dB), + SBR_VLC_ROW(t_huffman_noise_bal_3_0dB), + }; + + // 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); + + for (n = 0; n < 64; n++) { + float pre = M_PI * n / 64; + analysis_cos_pre[n] = cosf(pre); + analysis_sin_pre[n] = sinf(pre); + } + for (k = 0; k < 32; k++) { + float post = M_PI * (k + 0.5) / 128; + analysis_cossin_post[k][0] = 4.0 * cosf(post); + analysis_cossin_post[k][1] = -4.0 * sinf(post); + } + for (n = 1; n < 320; n++) + sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n]; + sbr_qmf_window_us[384] = -sbr_qmf_window_us[384]; + sbr_qmf_window_us[512] = -sbr_qmf_window_us[512]; + + for (n = 0; n < 320; n++) + sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n]; +} + +av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr) +{ + sbr->kx[0] = sbr->kx[1] = 32; //Typo in spec, kx' inits to 32 + sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128); + sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128); + ff_mdct_init(&sbr->mdct, 7, 1, 1.0/64); + ff_rdft_init(&sbr->rdft, 6, IDFT_R2C); +} + +av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr) +{ + ff_mdct_end(&sbr->mdct); + ff_rdft_end(&sbr->rdft); +} + +static int qsort_comparison_function_int16(const void *a, const void *b) +{ + return *(const int16_t *)a - *(const int16_t *)b; +} + +static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle) +{ + int i; + for (i = 0; i <= last_el; i++) + if (table[i] == needle) + return 1; + return 0; +} + +/// Limiter Frequency Band Table (14496-3 sp04 p198) +static void sbr_make_f_tablelim(SpectralBandReplication *sbr) +{ + int k; + if (sbr->bs_limiter_bands > 0) { + static const float bands_warped[3] = { 1.32715174233856803909f, //2^(0.49/1.2) + 1.18509277094158210129f, //2^(0.49/2) + 1.11987160404675912501f }; //2^(0.49/3) + const float lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1]; + int16_t patch_borders[5]; + uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim; + + patch_borders[0] = sbr->kx[1]; + 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])); + if (sbr->num_patches > 1) + 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_comparison_function_int16); + + sbr->n_lim = sbr->n[0] + sbr->num_patches - 1; + while (out < sbr->f_tablelim + sbr->n_lim) { + if (*in >= *out * lim_bands_per_octave_warped) { + *++out = *in++; + } else if (*in == *out || + !in_table_int16(patch_borders, sbr->num_patches, *in)) { + in++; + sbr->n_lim--; + } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) { + *out = *in++; + sbr->n_lim--; + } else { + *++out = *in++; + } + } + } else { + sbr->f_tablelim[0] = sbr->f_tablelow[0]; + sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]]; + sbr->n_lim = 1; + } +} + +static unsigned int read_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; + int old_bs_limiter_bands = sbr->bs_limiter_bands; + SpectrumParameters old_spectrum_params; + + sbr->start = 1; + + // Save last spectrum parameters variables to compare to new ones + memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)); + + sbr->bs_amp_res_header = get_bits1(gb); + sbr->spectrum_params.bs_start_freq = get_bits(gb, 4); + sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4); + sbr->spectrum_params.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.bs_freq_scale = get_bits(gb, 2); + sbr->spectrum_params.bs_alter_scale = get_bits1(gb); + sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2); + } else { + sbr->spectrum_params.bs_freq_scale = 2; + sbr->spectrum_params.bs_alter_scale = 1; + sbr->spectrum_params.bs_noise_bands = 2; + } + + // Check if spectrum parameters changed + if (memcmp(&old_spectrum_params, &sbr->spectrum_params, 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); + } else { + sbr->bs_limiter_bands = 2; + sbr->bs_limiter_gains = 2; + sbr->bs_interpol_freq = 1; + sbr->bs_smoothing_mode = 1; + } + + if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset) + sbr_make_f_tablelim(sbr); + + return get_bits_count(gb) - cnt; +} + +static int array_min_int16(const int16_t *array, int nel) +{ + int i, min = array[0]; + for (i = 1; i < nel; i++) + min = FFMIN(array[i], min); + return min; +} + +static void make_bands(int16_t* bands, int start, int stop, int num_bands) +{ + int k, previous, present; + float base, prod; + + base = powf((float)stop / start, 1.0f / num_bands); + prod = start; + previous = start; + + for (k = 0; k < num_bands-1; k++) { + prod *= base; + present = lrintf(prod); + bands[k] = present - previous; + previous = present; + } + bands[num_bands-1] = stop - previous; +} + +static int check_n_master(AVCodecContext *avccontext, int n_master, int bs_xover_band) +{ + // Requirements (14496-3 sp04 p205) + if (n_master <= 0) { + av_log(avccontext, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master); + return -1; + } + if (bs_xover_band >= n_master) { + av_log(avccontext, AV_LOG_ERROR, + "Invalid bitstream, crossover band index beyond array bounds: %d\n", + bs_xover_band); + return -1; + } + return 0; +} + +/// Master Frequency Band Table (14496-3 sp04 p194) +static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, + SpectrumParameters *spectrum) +{ + unsigned int temp, max_qmf_subbands; + unsigned int start_min, stop_min; + int k; + const int8_t *sbr_offset_ptr; + int16_t stop_dk[13]; + + if (sbr->sample_rate < 32000) { + temp = 3000; + } else if (sbr->sample_rate < 64000) { + temp = 4000; + } else + temp = 5000; + + start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate; + stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate; + + switch (sbr->sample_rate) { + case 16000: + sbr_offset_ptr = sbr_offset[0]; + break; + case 22050: + sbr_offset_ptr = sbr_offset[1]; + break; + case 24000: + sbr_offset_ptr = sbr_offset[2]; + break; + case 32000: + sbr_offset_ptr = sbr_offset[3]; + break; + case 44100: case 48000: case 64000: + sbr_offset_ptr = sbr_offset[4]; + break; + case 88200: case 96000: case 128000: case 176400: case 192000: + sbr_offset_ptr = sbr_offset[5]; + break; + default: + av_log(ac->avccontext, AV_LOG_ERROR, + "Unsupported sample rate for SBR: %d\n", sbr->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; + make_bands(stop_dk, stop_min, 64, 13); + qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16); + for (k = 0; k < spectrum->bs_stop_freq; k++) + sbr->k[2] += stop_dk[k]; + } 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]); + + // Requirements (14496-3 sp04 p205) + if (sbr->sample_rate <= 32000) { + max_qmf_subbands = 48; + } else if (sbr->sample_rate == 44100) { + max_qmf_subbands = 35; + } else if (sbr->sample_rate >= 48000) + max_qmf_subbands = 32; + + if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) { + av_log(ac->avccontext, AV_LOG_ERROR, + "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]); + return -1; + } + + if (!spectrum->bs_freq_scale) { + unsigned int dk; + int k2diff; + + dk = spectrum->bs_alter_scale + 1; + sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1; + if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) + return -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 < 0) { + sbr->f_master[1]--; + sbr->f_master[2]-= (k2diff < 1); + } else if (k2diff) { + sbr->f_master[sbr->n_master]++; + } + + sbr->f_master[0] = sbr->k[0]; + for (k = 1; k <= sbr->n_master; k++) + sbr->f_master[k] += sbr->f_master[k - 1]; + + } else { + int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3} + int two_regions, num_bands_0; + int vdk0_max, vdk1_min; + int16_t vk0[49]; + + if (49 * sbr->k[2] > 110 * sbr->k[0]) { + two_regions = 1; + sbr->k[1] = 2 * sbr->k[0]; + } else { + two_regions = 0; + sbr->k[1] = sbr->k[2]; + } + + num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2; + + if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205) + av_log(ac->avccontext, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0); + return -1; + } + + vk0[0] = 0; + + make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0); + + qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16); + 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) { + int16_t vk1[49]; + float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f + : 1.0f; // bs_alter_scale = {0,1} + int num_bands_1 = lrintf(half_bands * invwarp * + log2f(sbr->k[2] / (float)sbr->k[1])) * 2; + + make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1); + + vdk1_min = array_min_int16(vk1 + 1, num_bands_1); + + if (vdk1_min < vdk0_max) { + int change; + qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16); + 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_comparison_function_int16); + + 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; + if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) + return -1; + memcpy(&sbr->f_master[0], vk0, + (num_bands_0 + 1) * sizeof(sbr->f_master[0])); + memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1, + num_bands_1 * sizeof(sbr->f_master[0])); + + } else { + sbr->n_master = num_bands_0; + if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) + return -1; + memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0])); + } + } + + 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->kx[1]; + int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate; + + sbr->num_patches = 0; + + if (goal_sb < sbr->kx[1] + sbr->m[1]) { + for (k = 0; sbr->f_master[k] < goal_sb; k++) ; + } else + k = sbr->n_master; + + do { + int odd = 0; + for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) { + sb = sbr->f_master[i]; + odd = (sb + 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->kx[1]; + + if (sbr->f_master[k] - sb < 3) + k = sbr->n_master; + } while (sb != sbr->kx[1] + sbr->m[1]); + + if (sbr->patch_num_subbands[sbr->num_patches-1] < 3 && sbr->num_patches > 1) + sbr->num_patches--; + + // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5 + // However the Coding Technologies decoder check uses 6 patches + if (sbr->num_patches > 6) { + av_log(ac->avccontext, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches); + 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.bs_xover_band; + sbr->n[0] = (sbr->n[1] + 1) >> 1; + + memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band], + (sbr->n[1] + 1) * sizeof(sbr->f_master[0])); + sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0]; + sbr->kx[1] = sbr->f_tablehigh[0]; + + // Requirements (14496-3 sp04 p205) + if (sbr->kx[1] + sbr->m[1] > 64) { + av_log(ac->avccontext, AV_LOG_ERROR, + "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]); + return -1; + } + if (sbr->kx[1] > 32) { + av_log(ac->avccontext, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]); + return -1; + } + + sbr->f_tablelow[0] = sbr->f_tablehigh[0]; + temp = sbr->n[1] & 1; + for (k = 1; k <= sbr->n[0]; k++) + sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp]; + + sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands * + log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3 + if (sbr->n_q > 5) { + av_log(ac->avccontext, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q); + 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]; + } + + if (sbr_hf_calc_npatches(ac, sbr) < 0) + return -1; + + sbr_make_f_tablelim(sbr); + + sbr->data[0].f_indexnoise = 0; + sbr->data[1].f_indexnoise = 0; + + return 0; +} + +static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec, + int elements) +{ + int i; + for (i = 0; i < elements; i++) { + vec[i] = get_bits1(gb); + } +} + +/** ceil(log2(index+1)) */ +static const int8_t ceil_log2[] = { + 0, 1, 2, 2, 3, 3, +}; + +static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr, + GetBitContext *gb, SBRData *ch_data) +{ + int i; + + ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env[1]]; + ch_data->bs_num_env[0] = ch_data->bs_num_env[1]; + ch_data->bs_amp_res = sbr->bs_amp_res_header; + + 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) + ch_data->bs_amp_res = 0; + + ch_data->bs_freq_res[1] = get_bits1(gb); + for (i = 1; i < ch_data->bs_num_env[1]; i++) + ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1]; + 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] = 2 * get_bits(gb, 2) + 2; + + ch_data->bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]); + + for (i = 0; i < ch_data->bs_num_env[1]; i++) + ch_data->bs_freq_res[ch_data->bs_num_env[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] = 2 * get_bits(gb, 2) + 2; + + ch_data->bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]); + + get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env[1]); + 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] = 2 * get_bits(gb, 2) + 2; + for (i = 0; i < ch_data->bs_num_rel[1]; i++) + ch_data->bs_rel_bord[1][i] = 2 * get_bits(gb, 2) + 2; + + ch_data->bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]); + + get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env[1]); + 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) + 1; + + return 0; +} + +static void copy_sbr_grid(SBRData *dst, const SBRData *src) { + //These variables are saved from the previous frame rather than copied + dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env[1]]; + dst->bs_num_env[0] = dst->bs_num_env[1]; + + //These variables are read from the bitstream and therefore copied + memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res)); + memcpy(dst->bs_num_env+1, src->bs_num_env+1, sizeof(dst->bs_num_env)- sizeof(*dst->bs_num_env)); + memcpy(dst->bs_var_bord, src->bs_var_bord, sizeof(dst->bs_var_bord)); + memcpy(dst->bs_rel_bord, src->bs_rel_bord, sizeof(dst->bs_rel_bord)); + memcpy(dst->bs_num_rel, src->bs_num_rel, sizeof(dst->bs_rel_bord)); + dst->bs_amp_res = src->bs_amp_res; + dst->bs_num_noise = src->bs_num_noise; + dst->bs_pointer = src->bs_pointer; + dst->bs_frame_class = src->bs_frame_class; +} + +/// Read how the envelope and noise floor data is delta coded +static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb, + SBRData *ch_data) +{ + get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env[1]); + get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise); +} + +/// Read inverse filtering data +static void read_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 read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb, + SBRData *ch_data, int ch) +{ + int bits; + int i, j, k; + VLC_TYPE (*t_huff)[2], (*f_huff)[2]; + int t_lav, f_lav; + const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1; + const int odd = sbr->n[1] & 1; + + if (sbr->bs_coupling && ch) { + if (ch_data->bs_amp_res) { + bits = 5; + t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table; + t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB]; + f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table; + f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB]; + } else { + bits = 6; + t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table; + t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB]; + f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table; + f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB]; + } + } else { + if (ch_data->bs_amp_res) { + bits = 6; + t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table; + t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB]; + f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table; + f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB]; + } else { + bits = 7; + t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table; + t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB]; + f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table; + f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB]; + } + } + + for (i = 0; i < ch_data->bs_num_env[1]; i++) { + if (ch_data->bs_df_env[i]) { + // bs_freq_res[0] == bs_freq_res[bs_num_env[1]] from prev frame + if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) { + for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) + ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav); + } else if (ch_data->bs_freq_res[i + 1]) { + for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) { + k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1] + ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav); + } + } else { + for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) { + k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j] + ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav); + } + } + } else { + ch_data->env_facs[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance + for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) + ch_data->env_facs[i + 1][j] = ch_data->env_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav); + } + } + + //assign 0th elements of env_facs from last elements + memcpy(ch_data->env_facs[0], ch_data->env_facs[ch_data->bs_num_env[1]], + sizeof(ch_data->env_facs[0])); +} + +static void read_sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb, + SBRData *ch_data, int ch) +{ + int i, j; + VLC_TYPE (*t_huff)[2], (*f_huff)[2]; + int t_lav, f_lav; + int delta = (ch == 1 && sbr->bs_coupling == 1) + 1; + + if (sbr->bs_coupling && ch) { + t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table; + t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB]; + f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table; + f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB]; + } else { + t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table; + t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB]; + f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table; + f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB]; + } + + for (i = 0; i < ch_data->bs_num_noise; i++) { + if (ch_data->bs_df_noise[i]) { + for (j = 0; j < sbr->n_q; j++) + ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav); + } else { + ch_data->noise_facs[i + 1][0] = delta * 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->noise_facs[i + 1][j] = ch_data->noise_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav); + } + } + + //assign 0th elements of noise_facs from last elements + memcpy(ch_data->noise_facs[0], ch_data->noise_facs[ch_data->bs_num_noise], + sizeof(ch_data->noise_facs[0])); +} + +static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, + GetBitContext *gb, + int bs_extension_id, int *num_bits_left) +{ +//TODO - implement ps_data for parametric stereo parsing + switch (bs_extension_id) { + case EXTENSION_ID_PS: +#if 0 + *num_bits_left -= ff_ps_data(gb, ps); +#else + av_log_missing_feature(ac->avccontext, "Parametric Stereo is", 0); + skip_bits_long(gb, *num_bits_left); // bs_fill_bits + *num_bits_left = 0; +#endif + break; + default: + av_log_missing_feature(ac->avccontext, "Reserved SBR extensions are", 1); + skip_bits_long(gb, *num_bits_left); // bs_fill_bits + *num_bits_left = 0; + break; + } +} + +static void read_sbr_single_channel_element(AACContext *ac, + SpectralBandReplication *sbr, + GetBitContext *gb) +{ + if (get_bits1(gb)) // bs_data_extra + skip_bits(gb, 4); // bs_reserved + + read_sbr_grid(ac, sbr, gb, &sbr->data[0]); + read_sbr_dtdf(sbr, gb, &sbr->data[0]); + read_sbr_invf(sbr, gb, &sbr->data[0]); + read_sbr_envelope(sbr, gb, &sbr->data[0], 0); + read_sbr_noise(sbr, gb, &sbr->data[0], 0); + + if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb))) + get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]); +} + +static void read_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))) { + read_sbr_grid(ac, sbr, gb, &sbr->data[0]); + copy_sbr_grid(&sbr->data[1], &sbr->data[0]); + read_sbr_dtdf(sbr, gb, &sbr->data[0]); + read_sbr_dtdf(sbr, gb, &sbr->data[1]); + read_sbr_invf(sbr, gb, &sbr->data[0]); + memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0])); + memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0])); + read_sbr_envelope(sbr, gb, &sbr->data[0], 0); + read_sbr_noise(sbr, gb, &sbr->data[0], 0); + read_sbr_envelope(sbr, gb, &sbr->data[1], 1); + read_sbr_noise(sbr, gb, &sbr->data[1], 1); + } else { + read_sbr_grid(ac, sbr, gb, &sbr->data[0]); + read_sbr_grid(ac, sbr, gb, &sbr->data[1]); + read_sbr_dtdf(sbr, gb, &sbr->data[0]); + read_sbr_dtdf(sbr, gb, &sbr->data[1]); + read_sbr_invf(sbr, gb, &sbr->data[0]); + read_sbr_invf(sbr, gb, &sbr->data[1]); + read_sbr_envelope(sbr, gb, &sbr->data[0], 0); + read_sbr_envelope(sbr, gb, &sbr->data[1], 1); + read_sbr_noise(sbr, gb, &sbr->data[0], 0); + read_sbr_noise(sbr, gb, &sbr->data[1], 1); + } + + if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb))) + get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]); + if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb))) + get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]); +} + +static unsigned int read_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) { + read_sbr_single_channel_element(ac, sbr, gb); + } else if (id_aac == TYPE_CPE) { + read_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); + sbr->start = 0; + return get_bits_count(gb) - cnt; + } + 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; + read_sbr_extension(ac, 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) +{ + int err; + err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params); + if (err >= 0) + err = sbr_make_f_derived(ac, sbr); + if (err < 0) { + av_log(ac->avccontext, AV_LOG_ERROR, + "SBR reset failed. Switching SBR to pure upsampling mode.\n"); + sbr->start = 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_host, int crc, int cnt, int id_aac) +{ + unsigned int num_sbr_bits = 0, num_align_bits; + unsigned bytes_read; + GetBitContext gbc = *gb_host, *gb = &gbc; + skip_bits_long(gb_host, cnt*8 - 4); + + sbr->reset = 0; + + if (!sbr->sample_rate) + sbr->sample_rate = 2 * ac->m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support + if (!ac->m4ac.ext_sample_rate) + ac->m4ac.ext_sample_rate = 2 * ac->m4ac.sample_rate; + + if (crc) { + skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check + num_sbr_bits += 10; + } + + //Save some state from the previous frame. + sbr->kx[0] = sbr->kx[1]; + sbr->m[0] = sbr->m[1]; + + num_sbr_bits++; + if (get_bits1(gb)) // bs_header_flag + num_sbr_bits += read_sbr_header(sbr, gb); + + if (sbr->reset) + sbr_reset(ac, sbr); + + if (sbr->start) + num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac); + + num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7; + bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3); + + if (bytes_read > cnt) { + av_log(ac->avccontext, AV_LOG_ERROR, + "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read); + } + return cnt; +} + +/// Time/frequency Grid (14496-3 sp04 p200) +static int sbr_time_freq_grid(AACContext *ac, SpectralBandReplication *sbr, + SBRData *ch_data, int ch) +{ + 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 + int abs_bord_trail = (ch_data->bs_frame_class & 1 ? ch_data->bs_var_bord[1] : 0) + 16; + int n_rel_lead; + 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; + } + + ch_data->t_env_num_env_old = ch_data->t_env[ch_data->bs_num_env[0]]; + ch_data->t_env[0] = abs_bord_lead; + ch_data->t_env[ch_data->bs_num_env[1]] = abs_bord_trail; + + if (ch_data->bs_frame_class == FIXFIX) { + int temp = (abs_bord_trail + (ch_data->bs_num_env[1] >> 1)) / + ch_data->bs_num_env[1]; + for (i = 0; i < n_rel_lead; i++) + ch_data->t_env[i + 1] = ch_data->t_env[i] + temp; + } else if (ch_data->bs_frame_class > 1) { // VARFIX or VARVAR + for (i = 0; i < n_rel_lead; i++) + ch_data->t_env[i + 1] = ch_data->t_env[i] + ch_data->bs_rel_bord[0][i]; + } else { // FIXVAR + for (i = 0; i < n_rel_lead; i++) + ch_data->t_env[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--) + ch_data->t_env[i] = ch_data->t_env[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++) + ch_data->t_env[i + 1] = abs_bord_trail; + } + + ch_data->t_q[0] = ch_data->t_env[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) + 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; + } + ch_data->t_q[1] = ch_data->t_env[idx]; + ch_data->t_q[2] = ch_data->t_env[ch_data->bs_num_env[1]]; + } else + ch_data->t_q[1] = ch_data->t_env[ch_data->bs_num_env[1]]; + + return 0; +} + +/// Dequantization and stereo decoding (14496-3 sp04 p203) +static void sbr_dequant(SpectralBandReplication *sbr, int id_aac) +{ + int k, e; + int ch; + + if (id_aac == TYPE_CPE && sbr->bs_coupling) { + float alpha = sbr->data[0].bs_amp_res ? 1.0f : 0.5f; + float pan_offset = sbr->data[0].bs_amp_res ? 12.0f : 24.0f; + for (e = 1; e <= sbr->data[0].bs_num_env[1]; e++) { + for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) { + float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f); + float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha); + float fac = temp1 / (1.0f + temp2); + sbr->data[0].env_facs[e][k] = fac; + sbr->data[1].env_facs[e][k] = fac * temp2; + } + } + for (e = 1; e <= sbr->data[0].bs_num_noise; e++) { + for (k = 0; k < sbr->n_q; k++) { + float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1); + float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]); + float fac = temp1 / (1.0f + temp2); + sbr->data[0].noise_facs[e][k] = fac; + sbr->data[1].noise_facs[e][k] = fac * temp2; + } + } + } else { // SCE or one non-coupled CPE + for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) { + float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f; + for (e = 1; e <= sbr->data[ch].bs_num_env[1]; e++) + for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++) + sbr->data[ch].env_facs[e][k] = + exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f); + for (e = 1; e <= sbr->data[ch].bs_num_noise; e++) + for (k = 0; k < sbr->n_q; k++) + sbr->data[ch].noise_facs[e][k] = + exp2f(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs[e][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(DSPContext *dsp, RDFTContext *rdft, const float *in, float *x, + float z[320], float W[2][32][32][2], + float bias, float scale) +{ + int i, k; + memcpy(W[0], W[1], sizeof(W[0])); + memcpy(x , x+1024, (320-32)*sizeof(x[0])); + if (scale != 1.0f || bias != 0.0f) + for (i = 0; i < 1024; i++) + x[288 + i] = (in[i] - bias) * scale; + else + memcpy(x+288, in, 1024*sizeof(*x)); + for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames + // are not supported + float re, im; + dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320); + for (k = 0; k < 64; k++) { + float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256]; + z[k] = f * analysis_cos_pre[k]; + z[k+64] = f; + } + ff_rdft_calc(rdft, z); + re = z[0] * 0.5f; + im = 0.5f * dsp->scalarproduct_float(z+64, analysis_sin_pre, 64); + W[1][i][0][0] = re * analysis_cossin_post[0][0] - im * analysis_cossin_post[0][1]; + W[1][i][0][1] = re * analysis_cossin_post[0][1] + im * analysis_cossin_post[0][0]; + for (k = 1; k < 32; k++) { + re = z[2*k ] - re; + im = z[2*k+1] - im; + W[1][i][k][0] = re * analysis_cossin_post[k][0] - im * analysis_cossin_post[k][1]; + W[1][i][k][1] = re * analysis_cossin_post[k][1] + im * analysis_cossin_post[k][0]; + } + x += 32; + } +} + +/** + * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank + * (14496-3 sp04 p206) + */ +static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct, + float *out, float X[2][32][64], + float mdct_buf[2][64], + float *v0, int *v_off, const unsigned int div, + float bias, float scale) +{ + int i, n; + const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us; + int scale_and_bias = scale != 1.0f || bias != 0.0f; + float *v; + for (i = 0; i < 32; i++) { + if (*v_off == 0) { + int saved_samples = (1280 - 128) >> div; + memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float)); + *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - (128 >> div); + } else { + *v_off -= 128 >> div; + } + v = v0 + *v_off; + for (n = 1; n < 64 >> div; n+=2) { + X[1][i][n] = -X[1][i][n]; + } + if (div) { + memset(X[0][i]+32, 0, 32*sizeof(float)); + memset(X[1][i]+32, 0, 32*sizeof(float)); + } + ff_imdct_half(mdct, mdct_buf[0], X[0][i]); + ff_imdct_half(mdct, mdct_buf[1], X[1][i]); + if (div) { + for (n = 0; n < 32; n++) { + v[ n] = -mdct_buf[0][63 - 2*n] + mdct_buf[1][2*n ]; + v[ 63 - n] = mdct_buf[0][62 - 2*n] + mdct_buf[1][2*n + 1]; + } + } else { + for (n = 0; n < 64; n++) { + v[ n] = -mdct_buf[0][63 - n] + mdct_buf[1][ n ]; + v[127 - n] = mdct_buf[0][63 - n] + mdct_buf[1][ n ]; + } + } + dsp->vector_fmul_add(out, v , sbr_qmf_window , zero64, 64 >> div); + dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div); + dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div); + dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div); + dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div); + dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div); + dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div); + dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div); + dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div); + dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div); + if (scale_and_bias) + for (n = 0; n < 64 >> div; n++) + out[n] = out[n] * scale + bias; + out += 64 >> div; + } +} + +static void autocorrelate(const float x[40][2], float phi[3][2][2], int lag) +{ + int i; + float real_sum = 0.0f; + float imag_sum = 0.0f; + if (lag) { + for (i = 1; i < 38; i++) { + real_sum += x[i][0] * x[i+lag][0] + x[i][1] * x[i+lag][1]; + imag_sum += x[i][0] * x[i+lag][1] - x[i][1] * x[i+lag][0]; + } + phi[2-lag][1][0] = real_sum + x[ 0][0] * x[lag][0] + x[ 0][1] * x[lag][1]; + phi[2-lag][1][1] = imag_sum + x[ 0][0] * x[lag][1] - x[ 0][1] * x[lag][0]; + if (lag == 1) { + phi[0][0][0] = real_sum + x[38][0] * x[39][0] + x[38][1] * x[39][1]; + phi[0][0][1] = imag_sum + x[38][0] * x[39][1] - x[38][1] * x[39][0]; + } + } else { + for (i = 1; i < 38; i++) { + real_sum += x[i][0] * x[i][0] + x[i][1] * x[i][1]; + } + phi[2][1][0] = real_sum + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1]; + phi[1][0][0] = real_sum + x[38][0] * x[38][0] + x[38][1] * x[38][1]; + } +} + +/** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering + * (14496-3 sp04 p214) + * Warning: This routine does not seem numerically stable. + */ +static void sbr_hf_inverse_filter(float (*alpha0)[2], float (*alpha1)[2], + const float X_low[32][40][2], int k0) +{ + int k; + for (k = 0; k < k0; k++) { + float phi[3][2][2], dk; + + autocorrelate(X_low[k], phi, 0); + autocorrelate(X_low[k], phi, 1); + autocorrelate(X_low[k], phi, 2); + + dk = phi[2][1][0] * phi[1][0][0] - + (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f; + + if (!dk) { + 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]; + temp_im = phi[0][0][0] * phi[1][1][1] + + phi[0][0][1] * phi[1][1][0] - + phi[0][1][1] * phi[1][0][0]; + + alpha1[k][0] = temp_real / dk; + alpha1[k][1] = temp_im / dk; + } + + if (!phi[1][0][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] = -temp_real / phi[1][0][0]; + alpha0[k][1] = -temp_im / phi[1][0][0]; + } + + 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; + static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f }; + + for (i = 0; i < sbr->n_q; i++) { + if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) { + new_bw = 0.6f; + } else + new_bw = bw_tab[ch_data->bs_invf_mode[0][i]]; + + if (new_bw < ch_data->bw_array[i]) { + new_bw = 0.75f * new_bw + 0.25f * ch_data->bw_array[i]; + } else + new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i]; + ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw; + } +} + +/// Generate the subband filtered lowband +static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr, + float X_low[32][40][2], const float W[2][32][32][2]) +{ + int i, k; + const int t_HFGen = 8; + const int i_f = 32; + memset(X_low, 0, 32*sizeof(*X_low)); + for (k = 0; k < sbr->kx[1]; k++) { + for (i = t_HFGen; i < i_f + t_HFGen; i++) { + X_low[k][i][0] = W[1][i - t_HFGen][k][0]; + X_low[k][i][1] = W[1][i - t_HFGen][k][1]; + } + } + for (k = 0; k < sbr->kx[0]; k++) { + for (i = 0; i < t_HFGen; i++) { + X_low[k][i][0] = W[0][i + i_f - t_HFGen][k][0]; + X_low[k][i][1] = W[0][i + i_f - t_HFGen][k][1]; + } + } + return 0; +} + +/// High Frequency Generator (14496-3 sp04 p215) +static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr, + float X_high[64][40][2], const float X_low[32][40][2], + const float (*alpha0)[2], const float (*alpha1)[2], + const float bw_array[5], const uint8_t *t_env, + int bs_num_env) +{ + int i, j, x; + int g = 0; + int k = sbr->kx[1]; + for (j = 0; j < sbr->num_patches; j++) { + for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) { + float alpha[4]; + const int p = sbr->patch_start_subband[j] + x; + while (g <= sbr->n_q && k >= sbr->f_tablenoise[g]) + g++; + g--; + + if (g < 0) { + av_log(ac->avccontext, AV_LOG_ERROR, + "ERROR : no subband found for frequency %d\n", k); + return -1; + } + + alpha[0] = alpha1[p][0] * bw_array[g] * bw_array[g]; + alpha[1] = alpha1[p][1] * bw_array[g] * bw_array[g]; + alpha[2] = alpha0[p][0] * bw_array[g]; + alpha[3] = alpha0[p][1] * bw_array[g]; + + for (i = 2 * t_env[0]; i < 2 * t_env[bs_num_env]; i++) { + const int idx = i + ENVELOPE_ADJUSTMENT_OFFSET; + X_high[k][idx][0] = + X_low[p][idx - 2][0] * alpha[0] - + X_low[p][idx - 2][1] * alpha[1] + + X_low[p][idx - 1][0] * alpha[2] - + X_low[p][idx - 1][1] * alpha[3] + + X_low[p][idx][0]; + X_high[k][idx][1] = + X_low[p][idx - 2][1] * alpha[0] + + X_low[p][idx - 2][0] * alpha[1] + + X_low[p][idx - 1][1] * alpha[2] + + X_low[p][idx - 1][0] * alpha[3] + + X_low[p][idx][1]; + } + } + } + if (k < sbr->m[1] + sbr->kx[1]) + memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high)); + + return 0; +} + +/// Generate the subband filtered lowband +static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][32][64], + const float X_low[32][40][2], const float Y[2][38][64][2], + int ch) +{ + int k, i; + const int i_f = 32; + const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0); + memset(X, 0, 2*sizeof(*X)); + for (k = 0; k < sbr->kx[0]; k++) { + for (i = 0; i < i_Temp; i++) { + X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0]; + X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1]; + } + } + for (; k < sbr->kx[0] + sbr->m[0]; k++) { + for (i = 0; i < i_Temp; i++) { + X[0][i][k] = Y[0][i + i_f][k][0]; + X[1][i][k] = Y[0][i + i_f][k][1]; + } + } + + for (k = 0; k < sbr->kx[1]; k++) { + for (i = i_Temp; i < i_f; i++) { + X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0]; + X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1]; + } + } + for (; k < sbr->kx[1] + sbr->m[1]; k++) { + for (i = i_Temp; i < i_f; i++) { + X[0][i][k] = Y[1][i][k][0]; + X[1][i][k] = Y[1][i][k][1]; + } + } + return 0; +} + +/** High Frequency Adjustment (14496-3 sp04 p217) and Mapping + * (14496-3 sp04 p217) + */ +static void sbr_mapping(AACContext *ac, SpectralBandReplication *sbr, + SBRData *ch_data, int e_a[2]) +{ + int e, i, m; + + e_a[0] = -(e_a[1] != ch_data->bs_num_env[0]); // l_APrev + e_a[1] = -1; + if ((ch_data->bs_frame_class & 1) && ch_data->bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0 + e_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 + e_a[1] = ch_data->bs_pointer - 1; + + memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1])); + for (e = 0; e < ch_data->bs_num_env[1]; e++) { + const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]]; + uint16_t *table = ch_data->bs_freq_res[e + 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[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i]; + + // ch_data->bs_num_noise > 1 => 2 noise floors + k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]); + for (i = 0; i < sbr->n_q; i++) + for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++) + sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i]; + + for (i = 0; i < sbr->n[1]; i++) { + if (ch_data->bs_add_harmonic_flag) { + const unsigned int m_midpoint = + (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1; + + ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] * + (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1)); + } + } + + for (i = 0; i < ilim; i++) { + int additional_sinusoid_present = 0; + for (m = table[i]; m < table[i + 1]; m++) { + if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) { + additional_sinusoid_present = 1; + break; + } + } + memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present, + (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0])); + } + } + + memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env[1]], sizeof(ch_data->s_indexmapped[0])); +} + +/// Estimation of current envelope (14496-3 sp04 p218) +static void sbr_env_estimate(float (*e_curr)[48], float X_high[64][40][2], + SpectralBandReplication *sbr, SBRData *ch_data) +{ + int e, i, m; + + if (sbr->bs_interpol_freq) { + for (e = 0; e < ch_data->bs_num_env[1]; e++) { + const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]); + int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET; + int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET; + + for (m = 0; m < sbr->m[1]; m++) { + float sum = 0.0f; + + for (i = ilb; i < iub; i++) { + sum += X_high[m + sbr->kx[1]][i][0] * X_high[m + sbr->kx[1]][i][0] + + X_high[m + sbr->kx[1]][i][1] * X_high[m + sbr->kx[1]][i][1]; + } + e_curr[e][m] = sum * recip_env_size; + } + } + } else { + int k, p; + + for (e = 0; e < ch_data->bs_num_env[1]; e++) { + const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]); + int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET; + int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET; + const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow; + + for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) { + float sum = 0.0f; + const int den = env_size * (table[p + 1] - table[p]); + + 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]; + } + } + sum /= den; + for (k = table[p]; k < table[p + 1]; k++) { + e_curr[e][k - sbr->kx[1]] = sum; + } + } + } + } +} + +/** + * Calculation of levels of additional HF signal components (14496-3 sp04 p219) + * and Calculation of gain (14496-3 sp04 p219) + */ +static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr, + SBRData *ch_data, const int e_a[2]) +{ + int e, k, m; + // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off) + static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 }; + + for (e = 0; e < ch_data->bs_num_env[1]; e++) { + int delta = !((e == e_a[1]) || (e == e_a[0])); + for (k = 0; k < sbr->n_lim; k++) { + float gain_boost, gain_max; + float sum[2] = { 0.0f, 0.0f }; + for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) { + const float temp = sbr->e_origmapped[e][m] / (1.0f + sbr->q_mapped[e][m]); + sbr->q_m[e][m] = sqrtf(temp * sbr->q_mapped[e][m]); + sbr->s_m[e][m] = sqrtf(temp * ch_data->s_indexmapped[e + 1][m]); + if (!sbr->s_mapped[e][m]) { + sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] / + ((1.0f + sbr->e_curr[e][m]) * + (1.0f + sbr->q_mapped[e][m] * delta))); + } else { + sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] * sbr->q_mapped[e][m] / + ((1.0f + sbr->e_curr[e][m]) * + (1.0f + sbr->q_mapped[e][m]))); + } + } + for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) { + sum[0] += sbr->e_origmapped[e][m]; + sum[1] += sbr->e_curr[e][m]; + } + gain_max = limgain[sbr->bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1])); + gain_max = FFMIN(100000, gain_max); + for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) { + float q_m_max = sbr->q_m[e][m] * gain_max / sbr->gain[e][m]; + sbr->q_m[e][m] = FFMIN(sbr->q_m[e][m], q_m_max); + sbr->gain[e][m] = FFMIN(sbr->gain[e][m], gain_max); + } + sum[0] = sum[1] = 0.0f; + for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) { + sum[0] += sbr->e_origmapped[e][m]; + sum[1] += sbr->e_curr[e][m] * sbr->gain[e][m] * sbr->gain[e][m] + + sbr->s_m[e][m] * sbr->s_m[e][m] + + (delta && !sbr->s_m[e][m]) * sbr->q_m[e][m] * sbr->q_m[e][m]; + } + gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1])); + gain_boost = FFMIN(1.584893192, gain_boost); + for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) { + sbr->gain[e][m] *= gain_boost; + sbr->q_m[e][m] *= gain_boost; + sbr->s_m[e][m] *= gain_boost; + } + } + } +} + +/// Assembling HF Signals (14496-3 sp04 p220) +static void sbr_hf_assemble(float Y[2][38][64][2], const float X_high[64][40][2], + SpectralBandReplication *sbr, SBRData *ch_data, + const int e_a[2]) +{ + int e, i, j, m; + const int h_SL = 4 * !sbr->bs_smoothing_mode; + const int kx = sbr->kx[1]; + const int m_max = sbr->m[1]; + static const float h_smooth[5] = { + 0.33333333333333, + 0.30150283239582, + 0.21816949906249, + 0.11516383427084, + 0.03183050093751, + }; + static const int8_t phi[2][4] = { + { 1, 0, -1, 0}, // real + { 0, 1, 0, -1}, // imaginary + }; + float (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp; + int indexnoise = ch_data->f_indexnoise; + int indexsine = ch_data->f_indexsine; + memcpy(Y[0], Y[1], sizeof(Y[0])); + + if (sbr->reset) { + for (i = 0; i < h_SL; i++) { + memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0])); + memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0], m_max * sizeof(sbr->q_m[0][0])); + } + } else if (h_SL) { + memcpy(g_temp[2*ch_data->t_env[0]], g_temp[2*ch_data->t_env_num_env_old], 4*sizeof(g_temp[0])); + memcpy(q_temp[2*ch_data->t_env[0]], q_temp[2*ch_data->t_env_num_env_old], 4*sizeof(q_temp[0])); + } + + for (e = 0; e < ch_data->bs_num_env[1]; e++) { + for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) { + memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0])); + memcpy(q_temp[h_SL + i], sbr->q_m[e], m_max * sizeof(sbr->q_m[0][0])); + } + } + + for (e = 0; e < ch_data->bs_num_env[1]; e++) { + for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) { + int phi_sign = (1 - 2*(kx & 1)); + + if (h_SL && e != e_a[0] && e != e_a[1]) { + for (m = 0; m < m_max; m++) { + const int idx1 = i + h_SL; + float g_filt = 0.0f; + for (j = 0; j <= h_SL; j++) + g_filt += g_temp[idx1 - j][m] * h_smooth[j]; + Y[1][i][m + kx][0] = + X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][0] * g_filt; + Y[1][i][m + kx][1] = + X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][1] * g_filt; + } + } else { + for (m = 0; m < m_max; m++) { + const float g_filt = g_temp[i + h_SL][m]; + Y[1][i][m + kx][0] = + X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][0] * g_filt; + Y[1][i][m + kx][1] = + X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][1] * g_filt; + } + } + + if (e != e_a[0] && e != e_a[1]) { + for (m = 0; m < m_max; m++) { + indexnoise = (indexnoise + 1) & 0x1ff; + if (sbr->s_m[e][m]) { + Y[1][i][m + kx][0] += + sbr->s_m[e][m] * phi[0][indexsine]; + Y[1][i][m + kx][1] += + sbr->s_m[e][m] * (phi[1][indexsine] * phi_sign); + } else { + float q_filt; + if (h_SL) { + const int idx1 = i + h_SL; + q_filt = 0.0f; + for (j = 0; j <= h_SL; j++) + q_filt += q_temp[idx1 - j][m] * h_smooth[j]; + } else { + q_filt = q_temp[i][m]; + } + Y[1][i][m + kx][0] += + q_filt * sbr_noise_table[indexnoise][0]; + Y[1][i][m + kx][1] += + q_filt * sbr_noise_table[indexnoise][1]; + } + phi_sign = -phi_sign; + } + } else { + indexnoise = (indexnoise + m_max) & 0x1ff; + for (m = 0; m < m_max; m++) { + Y[1][i][m + kx][0] += + sbr->s_m[e][m] * phi[0][indexsine]; + Y[1][i][m + kx][1] += + sbr->s_m[e][m] * (phi[1][indexsine] * phi_sign); + phi_sign = -phi_sign; + } + } + indexsine = (indexsine + 1) & 3; + } + } + ch_data->f_indexnoise = indexnoise; + ch_data->f_indexsine = indexsine; +} + +void ff_sbr_dequant(AACContext *ac, SpectralBandReplication *sbr, int id_aac) +{ + int ch; + + if (sbr->start) { + for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) { + sbr_time_freq_grid(ac, sbr, &sbr->data[ch], ch); + } + sbr_dequant(sbr, id_aac); + } +} + +void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int ch, + const float* in, float* out) +{ + int downsampled = ac->m4ac.ext_sample_rate < sbr->sample_rate; + + /* decode channel */ + sbr_qmf_analysis(&ac->dsp, &sbr->rdft, in, sbr->data[ch].analysis_filterbank_samples, + (float*)sbr->qmf_filter_scratch, + sbr->data[ch].W, ac->add_bias, 1/(-1024 * ac->sf_scale)); + sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W); + if (sbr->start) { + sbr_hf_inverse_filter(sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]); + sbr_chirp(sbr, &sbr->data[ch]); + sbr_hf_gen(ac, sbr, sbr->X_high, sbr->X_low, sbr->alpha0, sbr->alpha1, + sbr->data[ch].bw_array, sbr->data[ch].t_env, + sbr->data[ch].bs_num_env[1]); + + // hf_adj + sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a); + sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]); + sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a); + sbr_hf_assemble(sbr->data[ch].Y, sbr->X_high, sbr, &sbr->data[ch], + sbr->data[ch].e_a); + } + + /* synthesis */ + sbr_x_gen(sbr, sbr->X, sbr->X_low, sbr->data[ch].Y, ch); + sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, out, sbr->X, sbr->qmf_filter_scratch, + sbr->data[ch].synthesis_filterbank_samples, + &sbr->data[ch].synthesis_filterbank_samples_offset, + downsampled, + ac->add_bias, -1024 * ac->sf_scale); +} diff --git a/libavcodec/aacsbr.h b/libavcodec/aacsbr.h new file mode 100644 index 0000000000..c865371b15 --- /dev/null +++ b/libavcodec/aacsbr.h @@ -0,0 +1,51 @@ +/* + * AAC Spectral Band Replication function declarations + * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl ) + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file libavcodec/aacsbr.h + * AAC Spectral Band Replication function declarations + * @author Robert Swain ( rob opendot cl ) + */ + +#ifndef AVCODEC_AACSBR_H +#define AVCODEC_AACSBR_H + +#include "get_bits.h" +#include "aac.h" +#include "sbr.h" + +/** Initialize SBR. */ +av_cold void ff_aac_sbr_init(void); +/** Initialize one SBR context. */ +av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr); +/** Close one SBR context. */ +av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr); +/** Decode one SBR element. */ +int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, + GetBitContext *gb, int crc, int cnt, int id_aac); +/** Dequantized all channels in one SBR element. */ +void ff_sbr_dequant(AACContext *ac, SpectralBandReplication *sbr, int id_aac); +/** Apply dequantized SBR to a single AAC channel. */ +void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int ch, + const float* in, float* out); + +#endif /* AVCODEC_AACSBR_H */ diff --git a/libavcodec/aacsbrdata.h b/libavcodec/aacsbrdata.h new file mode 100644 index 0000000000..f0bd26799d --- /dev/null +++ b/libavcodec/aacsbrdata.h @@ -0,0 +1,613 @@ +/* + * 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 + +///< 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 int8_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 DECLARE_ALIGNED(16, float, sbr_qmf_window_ds)[320]; +static DECLARE_ALIGNED(16, float, sbr_qmf_window_us)[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, +}; + +static const float sbr_noise_table[512][2] = { +{-0.99948153278296, -0.59483417516607}, { 0.97113454393991, -0.67528515225647}, +{ 0.14130051758487, -0.95090983575689}, {-0.47005496701697, -0.37340549728647}, +{ 0.80705063769351, 0.29653668284408}, {-0.38981478896926, 0.89572605717087}, +{-0.01053049862020, -0.66959058036166}, {-0.91266367957293, -0.11522938140034}, +{ 0.54840422910309, 0.75221367176302}, { 0.40009252867955, -0.98929400334421}, +{-0.99867974711855, -0.88147068645358}, {-0.95531076805040, 0.90908757154593}, +{-0.45725933317144, -0.56716323646760}, {-0.72929675029275, -0.98008272727324}, +{ 0.75622801399036, 0.20950329995549}, { 0.07069442601050, -0.78247898470706}, +{ 0.74496252926055, -0.91169004445807}, {-0.96440182703856, -0.94739918296622}, +{ 0.30424629369539, -0.49438267012479}, { 0.66565033746925, 0.64652935542491}, +{ 0.91697008020594, 0.17514097332009}, {-0.70774918760427, 0.52548653416543}, +{-0.70051415345560, -0.45340028808763}, {-0.99496513054797, -0.90071908066973}, +{ 0.98164490790123, -0.77463155528697}, {-0.54671580548181, -0.02570928536004}, +{-0.01689629065389, 0.00287506445732}, {-0.86110349531986, 0.42548583726477}, +{-0.98892980586032, -0.87881132267556}, { 0.51756627678691, 0.66926784710139}, +{-0.99635026409640, -0.58107730574765}, {-0.99969370862163, 0.98369989360250}, +{ 0.55266258627194, 0.59449057465591}, { 0.34581177741673, 0.94879421061866}, +{ 0.62664209577999, -0.74402970906471}, {-0.77149701404973, -0.33883658042801}, +{-0.91592244254432, 0.03687901376713}, {-0.76285492357887, -0.91371867919124}, +{ 0.79788337195331, -0.93180971199849}, { 0.54473080610200, -0.11919206037186}, +{-0.85639281671058, 0.42429854760451}, {-0.92882402971423, 0.27871809078609}, +{-0.11708371046774, -0.99800843444966}, { 0.21356749817493, -0.90716295627033}, +{-0.76191692573909, 0.99768118356265}, { 0.98111043100884, -0.95854459734407}, +{-0.85913269895572, 0.95766566168880}, {-0.93307242253692, 0.49431757696466}, +{ 0.30485754879632, -0.70540034357529}, { 0.85289650925190, 0.46766131791044}, +{ 0.91328082618125, -0.99839597361769}, {-0.05890199924154, 0.70741827819497}, +{ 0.28398686150148, 0.34633555702188}, { 0.95258164539612, -0.54893416026939}, +{-0.78566324168507, -0.75568541079691}, {-0.95789495447877, -0.20423194696966}, +{ 0.82411158711197, 0.96654618432562}, {-0.65185446735885, -0.88734990773289}, +{-0.93643603134666, 0.99870790442385}, { 0.91427159529618, -0.98290505544444}, +{-0.70395684036886, 0.58796798221039}, { 0.00563771969365, 0.61768196727244}, +{ 0.89065051931895, 0.52783352697585}, {-0.68683707712762, 0.80806944710339}, +{ 0.72165342518718, -0.69259857349564}, {-0.62928247730667, 0.13627037407335}, +{ 0.29938434065514, -0.46051329682246}, {-0.91781958879280, -0.74012716684186}, +{ 0.99298717043688, 0.40816610075661}, { 0.82368298622748, -0.74036047190173}, +{-0.98512833386833, -0.99972330709594}, {-0.95915368242257, -0.99237800466040}, +{-0.21411126572790, -0.93424819052545}, {-0.68821476106884, -0.26892306315457}, +{ 0.91851997982317, 0.09358228901785}, {-0.96062769559127, 0.36099095133739}, +{ 0.51646184922287, -0.71373332873917}, { 0.61130721139669, 0.46950141175917}, +{ 0.47336129371299, -0.27333178296162}, { 0.90998308703519, 0.96715662938132}, +{ 0.44844799194357, 0.99211574628306}, { 0.66614891079092, 0.96590176169121}, +{ 0.74922239129237, -0.89879858826087}, {-0.99571588506485, 0.52785521494349}, +{ 0.97401082477563, -0.16855870075190}, { 0.72683747733879, -0.48060774432251}, +{ 0.95432193457128, 0.68849603408441}, {-0.72962208425191, -0.76608443420917}, +{-0.85359479233537, 0.88738125901579}, {-0.81412430338535, -0.97480768049637}, +{-0.87930772356786, 0.74748307690436}, {-0.71573331064977, -0.98570608178923}, +{ 0.83524300028228, 0.83702537075163}, {-0.48086065601423, -0.98848504923531}, +{ 0.97139128574778, 0.80093621198236}, { 0.51992825347895, 0.80247631400510}, +{-0.00848591195325, -0.76670128000486}, {-0.70294374303036, 0.55359910445577}, +{-0.95894428168140, -0.43265504344783}, { 0.97079252950321, 0.09325857238682}, +{-0.92404293670797, 0.85507704027855}, {-0.69506469500450, 0.98633412625459}, +{ 0.26559203620024, 0.73314307966524}, { 0.28038443336943, 0.14537913654427}, +{-0.74138124825523, 0.99310339807762}, {-0.01752795995444, -0.82616635284178}, +{-0.55126773094930, -0.98898543862153}, { 0.97960898850996, -0.94021446752851}, +{-0.99196309146936, 0.67019017358456}, {-0.67684928085260, 0.12631491649378}, +{ 0.09140039465500, -0.20537731453108}, {-0.71658965751996, -0.97788200391224}, +{ 0.81014640078925, 0.53722648362443}, { 0.40616991671205, -0.26469008598449}, +{-0.67680188682972, 0.94502052337695}, { 0.86849774348749, -0.18333598647899}, +{-0.99500381284851, -0.02634122068550}, { 0.84329189340667, 0.10406957462213}, +{-0.09215968531446, 0.69540012101253}, { 0.99956173327206, -0.12358542001404}, +{-0.79732779473535, -0.91582524736159}, { 0.96349973642406, 0.96640458041000}, +{-0.79942778496547, 0.64323902822857}, {-0.11566039853896, 0.28587846253726}, +{-0.39922954514662, 0.94129601616966}, { 0.99089197565987, -0.92062625581587}, +{ 0.28631285179909, -0.91035047143603}, {-0.83302725605608, -0.67330410892084}, +{ 0.95404443402072, 0.49162765398743}, {-0.06449863579434, 0.03250560813135}, +{-0.99575054486311, 0.42389784469507}, {-0.65501142790847, 0.82546114655624}, +{-0.81254441908887, -0.51627234660629}, {-0.99646369485481, 0.84490533520752}, +{ 0.00287840603348, 0.64768261158166}, { 0.70176989408455, -0.20453028573322}, +{ 0.96361882270190, 0.40706967140989}, {-0.68883758192426, 0.91338958840772}, +{-0.34875585502238, 0.71472290693300}, { 0.91980081243087, 0.66507455644919}, +{-0.99009048343881, 0.85868021604848}, { 0.68865791458395, 0.55660316809678}, +{-0.99484402129368, -0.20052559254934}, { 0.94214511408023, -0.99696425367461}, +{-0.67414626793544, 0.49548221180078}, {-0.47339353684664, -0.85904328834047}, +{ 0.14323651387360, -0.94145598222488}, {-0.29268293575672, 0.05759224927952}, +{ 0.43793861458754, -0.78904969892724}, {-0.36345126374441, 0.64874435357162}, +{-0.08750604656825, 0.97686944362527}, {-0.96495267812511, -0.53960305946511}, +{ 0.55526940659947, 0.78891523734774}, { 0.73538215752630, 0.96452072373404}, +{-0.30889773919437, -0.80664389776860}, { 0.03574995626194, -0.97325616900959}, +{ 0.98720684660488, 0.48409133691962}, {-0.81689296271203, -0.90827703628298}, +{ 0.67866860118215, 0.81284503870856}, {-0.15808569732583, 0.85279555024382}, +{ 0.80723395114371, -0.24717418514605}, { 0.47788757329038, -0.46333147839295}, +{ 0.96367554763201, 0.38486749303242}, {-0.99143875716818, -0.24945277239809}, +{ 0.83081876925833, -0.94780851414763}, {-0.58753191905341, 0.01290772389163}, +{ 0.95538108220960, -0.85557052096538}, {-0.96490920476211, -0.64020970923102}, +{-0.97327101028521, 0.12378128133110}, { 0.91400366022124, 0.57972471346930}, +{-0.99925837363824, 0.71084847864067}, {-0.86875903507313, -0.20291699203564}, +{-0.26240034795124, -0.68264554369108}, {-0.24664412953388, -0.87642273115183}, +{ 0.02416275806869, 0.27192914288905}, { 0.82068619590515, -0.85087787994476}, +{ 0.88547373760759, -0.89636802901469}, {-0.18173078152226, -0.26152145156800}, +{ 0.09355476558534, 0.54845123045604}, {-0.54668414224090, 0.95980774020221}, +{ 0.37050990604091, -0.59910140383171}, {-0.70373594262891, 0.91227665827081}, +{-0.34600785879594, -0.99441426144200}, {-0.68774481731008, -0.30238837956299}, +{-0.26843291251234, 0.83115668004362}, { 0.49072334613242, -0.45359708737775}, +{ 0.38975993093975, 0.95515358099121}, {-0.97757125224150, 0.05305894580606}, +{-0.17325552859616, -0.92770672250494}, { 0.99948035025744, 0.58285545563426}, +{-0.64946246527458, 0.68645507104960}, {-0.12016920576437, -0.57147322153312}, +{-0.58947456517751, -0.34847132454388}, {-0.41815140454465, 0.16276422358861}, +{ 0.99885650204884, 0.11136095490444}, {-0.56649614128386, -0.90494866361587}, +{ 0.94138021032330, 0.35281916733018}, {-0.75725076534641, 0.53650549640587}, +{ 0.20541973692630, -0.94435144369918}, { 0.99980371023351, 0.79835913565599}, +{ 0.29078277605775, 0.35393777921520}, {-0.62858772103030, 0.38765693387102}, +{ 0.43440904467688, -0.98546330463232}, {-0.98298583762390, 0.21021524625209}, +{ 0.19513029146934, -0.94239832251867}, {-0.95476662400101, 0.98364554179143}, +{ 0.93379635304810, -0.70881994583682}, {-0.85235410573336, -0.08342347966410}, +{-0.86425093011245, -0.45795025029466}, { 0.38879779059045, 0.97274429344593}, +{ 0.92045124735495, -0.62433652524220}, { 0.89162532251878, 0.54950955570563}, +{-0.36834336949252, 0.96458298020975}, { 0.93891760988045, -0.89968353740388}, +{ 0.99267657565094, -0.03757034316958}, {-0.94063471614176, 0.41332338538963}, +{ 0.99740224117019, -0.16830494996370}, {-0.35899413170555, -0.46633226649613}, +{ 0.05237237274947, -0.25640361602661}, { 0.36703583957424, -0.38653265641875}, +{ 0.91653180367913, -0.30587628726597}, { 0.69000803499316, 0.90952171386132}, +{-0.38658751133527, 0.99501571208985}, {-0.29250814029851, 0.37444994344615}, +{-0.60182204677608, 0.86779651036123}, {-0.97418588163217, 0.96468523666475}, +{ 0.88461574003963, 0.57508405276414}, { 0.05198933055162, 0.21269661669964}, +{-0.53499621979720, 0.97241553731237}, {-0.49429560226497, 0.98183865291903}, +{-0.98935142339139, -0.40249159006933}, {-0.98081380091130, -0.72856895534041}, +{-0.27338148835532, 0.99950922447209}, { 0.06310802338302, -0.54539587529618}, +{-0.20461677199539, -0.14209977628489}, { 0.66223843141647, 0.72528579940326}, +{-0.84764345483665, 0.02372316801261}, {-0.89039863483811, 0.88866581484602}, +{ 0.95903308477986, 0.76744927173873}, { 0.73504123909879, -0.03747203173192}, +{-0.31744434966056, -0.36834111883652}, {-0.34110827591623, 0.40211222807691}, +{ 0.47803883714199, -0.39423219786288}, { 0.98299195879514, 0.01989791390047}, +{-0.30963073129751, -0.18076720599336}, { 0.99992588229018, -0.26281872094289}, +{-0.93149731080767, -0.98313162570490}, { 0.99923472302773, -0.80142993767554}, +{-0.26024169633417, -0.75999759855752}, {-0.35712514743563, 0.19298963768574}, +{-0.99899084509530, 0.74645156992493}, { 0.86557171579452, 0.55593866696299}, +{ 0.33408042438752, 0.86185953874709}, { 0.99010736374716, 0.04602397576623}, +{-0.66694269691195, -0.91643611810148}, { 0.64016792079480, 0.15649530836856}, +{ 0.99570534804836, 0.45844586038111}, {-0.63431466947340, 0.21079116459234}, +{-0.07706847005931, -0.89581437101329}, { 0.98590090577724, 0.88241721133981}, +{ 0.80099335254678, -0.36851896710853}, { 0.78368131392666, 0.45506999802597}, +{ 0.08707806671691, 0.80938994918745}, {-0.86811883080712, 0.39347308654705}, +{-0.39466529740375, -0.66809432114456}, { 0.97875325649683, -0.72467840967746}, +{-0.95038560288864, 0.89563219587625}, { 0.17005239424212, 0.54683053962658}, +{-0.76910792026848, -0.96226617549298}, { 0.99743281016846, 0.42697157037567}, +{ 0.95437383549973, 0.97002324109952}, { 0.99578905365569, -0.54106826257356}, +{ 0.28058259829990, -0.85361420634036}, { 0.85256524470573, -0.64567607735589}, +{-0.50608540105128, -0.65846015480300}, {-0.97210735183243, -0.23095213067791}, +{ 0.95424048234441, -0.99240147091219}, {-0.96926570524023, 0.73775654896574}, +{ 0.30872163214726, 0.41514960556126}, {-0.24523839572639, 0.63206633394807}, +{-0.33813265086024, -0.38661779441897}, {-0.05826828420146, -0.06940774188029}, +{-0.22898461455054, 0.97054853316316}, {-0.18509915019881, 0.47565762892084}, +{-0.10488238045009, -0.87769947402394}, {-0.71886586182037, 0.78030982480538}, +{ 0.99793873738654, 0.90041310491497}, { 0.57563307626120, -0.91034337352097}, +{ 0.28909646383717, 0.96307783970534}, { 0.42188998312520, 0.48148651230437}, +{ 0.93335049681047, -0.43537023883588}, {-0.97087374418267, 0.86636445711364}, +{ 0.36722871286923, 0.65291654172961}, {-0.81093025665696, 0.08778370229363}, +{-0.26240603062237, -0.92774095379098}, { 0.83996497984604, 0.55839849139647}, +{-0.99909615720225, -0.96024605713970}, { 0.74649464155061, 0.12144893606462}, +{-0.74774595569805, -0.26898062008959}, { 0.95781667469567, -0.79047927052628}, +{ 0.95472308713099, -0.08588776019550}, { 0.48708332746299, 0.99999041579432}, +{ 0.46332038247497, 0.10964126185063}, {-0.76497004940162, 0.89210929242238}, +{ 0.57397389364339, 0.35289703373760}, { 0.75374316974495, 0.96705214651335}, +{-0.59174397685714, -0.89405370422752}, { 0.75087906691890, -0.29612672982396}, +{-0.98607857336230, 0.25034911730023}, {-0.40761056640505, -0.90045573444695}, +{ 0.66929266740477, 0.98629493401748}, {-0.97463695257310, -0.00190223301301}, +{ 0.90145509409859, 0.99781390365446}, {-0.87259289048043, 0.99233587353666}, +{-0.91529461447692, -0.15698707534206}, {-0.03305738840705, -0.37205262859764}, +{ 0.07223051368337, -0.88805001733626}, { 0.99498012188353, 0.97094358113387}, +{-0.74904939500519, 0.99985483641521}, { 0.04585228574211, 0.99812337444082}, +{-0.89054954257993, -0.31791913188064}, {-0.83782144651251, 0.97637632547466}, +{ 0.33454804933804, -0.86231516800408}, {-0.99707579362824, 0.93237990079441}, +{-0.22827527843994, 0.18874759397997}, { 0.67248046289143, -0.03646211390569}, +{-0.05146538187944, -0.92599700120679}, { 0.99947295749905, 0.93625229707912}, +{ 0.66951124390363, 0.98905825623893}, {-0.99602956559179, -0.44654715757688}, +{ 0.82104905483590, 0.99540741724928}, { 0.99186510988782, 0.72023001312947}, +{-0.65284592392918, 0.52186723253637}, { 0.93885443798188, -0.74895312615259}, +{ 0.96735248738388, 0.90891816978629}, {-0.22225968841114, 0.57124029781228}, +{-0.44132783753414, -0.92688840659280}, {-0.85694974219574, 0.88844532719844}, +{ 0.91783042091762, -0.46356892383970}, { 0.72556974415690, -0.99899555770747}, +{-0.99711581834508, 0.58211560180426}, { 0.77638976371966, 0.94321834873819}, +{ 0.07717324253925, 0.58638399856595}, {-0.56049829194163, 0.82522301569036}, +{ 0.98398893639988, 0.39467440420569}, { 0.47546946844938, 0.68613044836811}, +{ 0.65675089314631, 0.18331637134880}, { 0.03273375457980, -0.74933109564108}, +{-0.38684144784738, 0.51337349030406}, {-0.97346267944545, -0.96549364384098}, +{-0.53282156061942, -0.91423265091354}, { 0.99817310731176, 0.61133572482148}, +{-0.50254500772635, -0.88829338134294}, { 0.01995873238855, 0.85223515096765}, +{ 0.99930381973804, 0.94578896296649}, { 0.82907767600783, -0.06323442598128}, +{-0.58660709669728, 0.96840773806582}, {-0.17573736667267, -0.48166920859485}, +{ 0.83434292401346, -0.13023450646997}, { 0.05946491307025, 0.20511047074866}, +{ 0.81505484574602, -0.94685947861369}, {-0.44976380954860, 0.40894572671545}, +{-0.89746474625671, 0.99846578838537}, { 0.39677256130792, -0.74854668609359}, +{-0.07588948563079, 0.74096214084170}, { 0.76343198951445, 0.41746629422634}, +{-0.74490104699626, 0.94725911744610}, { 0.64880119792759, 0.41336660830571}, +{ 0.62319537462542, -0.93098313552599}, { 0.42215817594807, -0.07712787385208}, +{ 0.02704554141885, -0.05417518053666}, { 0.80001773566818, 0.91542195141039}, +{-0.79351832348816, -0.36208897989136}, { 0.63872359151636, 0.08128252493444}, +{ 0.52890520960295, 0.60048872455592}, { 0.74238552914587, 0.04491915291044}, +{ 0.99096131449250, -0.19451182854402}, {-0.80412329643109, -0.88513818199457}, +{-0.64612616129736, 0.72198674804544}, { 0.11657770663191, -0.83662833815041}, +{-0.95053182488101, -0.96939905138082}, {-0.62228872928622, 0.82767262846661}, +{ 0.03004475787316, -0.99738896333384}, {-0.97987214341034, 0.36526129686425}, +{-0.99986980746200, -0.36021610299715}, { 0.89110648599879, -0.97894250343044}, +{ 0.10407960510582, 0.77357793811619}, { 0.95964737821728, -0.35435818285502}, +{ 0.50843233159162, 0.96107691266205}, { 0.17006334670615, -0.76854025314829}, +{ 0.25872675063360, 0.99893303933816}, {-0.01115998681937, 0.98496019742444}, +{-0.79598702973261, 0.97138411318894}, {-0.99264708948101, -0.99542822402536}, +{-0.99829663752818, 0.01877138824311}, {-0.70801016548184, 0.33680685948117}, +{-0.70467057786826, 0.93272777501857}, { 0.99846021905254, -0.98725746254433}, +{-0.63364968534650, -0.16473594423746}, {-0.16258217500792, -0.95939125400802}, +{-0.43645594360633, -0.94805030113284}, {-0.99848471702976, 0.96245166923809}, +{-0.16796458968998, -0.98987511890470}, {-0.87979225745213, -0.71725725041680}, +{ 0.44183099021786, -0.93568974498761}, { 0.93310180125532, -0.99913308068246}, +{-0.93941931782002, -0.56409379640356}, {-0.88590003188677, 0.47624600491382}, +{ 0.99971463703691, -0.83889954253462}, {-0.75376385639978, 0.00814643438625}, +{ 0.93887685615875, -0.11284528204636}, { 0.85126435782309, 0.52349251543547}, +{ 0.39701421446381, 0.81779634174316}, {-0.37024464187437, -0.87071656222959}, +{-0.36024828242896, 0.34655735648287}, {-0.93388812549209, -0.84476541096429}, +{-0.65298804552119, -0.18439575450921}, { 0.11960319006843, 0.99899346780168}, +{ 0.94292565553160, 0.83163906518293}, { 0.75081145286948, -0.35533223142265}, +{ 0.56721979748394, -0.24076836414499}, { 0.46857766746029, -0.30140233457198}, +{ 0.97312313923635, -0.99548191630031}, {-0.38299976567017, 0.98516909715427}, +{ 0.41025800019463, 0.02116736935734}, { 0.09638062008048, 0.04411984381457}, +{-0.85283249275397, 0.91475563922421}, { 0.88866808958124, -0.99735267083226}, +{-0.48202429536989, -0.96805608884164}, { 0.27572582416567, 0.58634753335832}, +{-0.65889129659168, 0.58835634138583}, { 0.98838086953732, 0.99994349600236}, +{-0.20651349620689, 0.54593044066355}, {-0.62126416356920, -0.59893681700392}, +{ 0.20320105410437, -0.86879180355289}, {-0.97790548600584, 0.96290806999242}, +{ 0.11112534735126, 0.21484763313301}, {-0.41368337314182, 0.28216837680365}, +{ 0.24133038992960, 0.51294362630238}, {-0.66393410674885, -0.08249679629081}, +{-0.53697829178752, -0.97649903936228}, {-0.97224737889348, 0.22081333579837}, +{ 0.87392477144549, -0.12796173740361}, { 0.19050361015753, 0.01602615387195}, +{-0.46353441212724, -0.95249041539006}, {-0.07064096339021, -0.94479803205886}, +{-0.92444085484466, -0.10457590187436}, {-0.83822593578728, -0.01695043208885}, +{ 0.75214681811150, -0.99955681042665}, {-0.42102998829339, 0.99720941999394}, +{-0.72094786237696, -0.35008961934255}, { 0.78843311019251, 0.52851398958271}, +{ 0.97394027897442, -0.26695944086561}, { 0.99206463477946, -0.57010120849429}, +{ 0.76789609461795, -0.76519356730966}, {-0.82002421836409, -0.73530179553767}, +{ 0.81924990025724, 0.99698425250579}, {-0.26719850873357, 0.68903369776193}, +{-0.43311260380975, 0.85321815947490}, { 0.99194979673836, 0.91876249766422}, +{-0.80692001248487, -0.32627540663214}, { 0.43080003649976, -0.21919095636638}, +{ 0.67709491937357, -0.95478075822906}, { 0.56151770568316, -0.70693811747778}, +{ 0.10831862810749, -0.08628837174592}, { 0.91229417540436, -0.65987351408410}, +{-0.48972893932274, 0.56289246362686}, {-0.89033658689697, -0.71656563987082}, +{ 0.65269447475094, 0.65916004833932}, { 0.67439478141121, -0.81684380846796}, +{-0.47770832416973, -0.16789556203025}, {-0.99715979260878, -0.93565784007648}, +{-0.90889593602546, 0.62034397054380}, {-0.06618622548177, -0.23812217221359}, +{ 0.99430266919728, 0.18812555317553}, { 0.97686402381843, -0.28664534366620}, +{ 0.94813650221268, -0.97506640027128}, {-0.95434497492853, -0.79607978501983}, +{-0.49104783137150, 0.32895214359663}, { 0.99881175120751, 0.88993983831354}, +{ 0.50449166760303, -0.85995072408434}, { 0.47162891065108, -0.18680204049569}, +{-0.62081581361840, 0.75000676218956}, {-0.43867015250812, 0.99998069244322}, +{ 0.98630563232075, -0.53578899600662}, {-0.61510362277374, -0.89515019899997}, +{-0.03841517601843, -0.69888815681179}, {-0.30102157304644, -0.07667808922205}, +{ 0.41881284182683, 0.02188098922282}, {-0.86135454941237, 0.98947480909359}, +{ 0.67226861393788, -0.13494389011014}, {-0.70737398842068, -0.76547349325992}, +{ 0.94044946687963, 0.09026201157416}, {-0.82386352534327, 0.08924768823676}, +{-0.32070666698656, 0.50143421908753}, { 0.57593163224487, -0.98966422921509}, +{-0.36326018419965, 0.07440243123228}, { 0.99979044674350, -0.14130287347405}, +{-0.92366023326932, -0.97979298068180}, {-0.44607178518598, -0.54233252016394}, +{ 0.44226800932956, 0.71326756742752}, { 0.03671907158312, 0.63606389366675}, +{ 0.52175424682195, -0.85396826735705}, {-0.94701139690956, -0.01826348194255}, +{-0.98759606946049, 0.82288714303073}, { 0.87434794743625, 0.89399495655433}, +{-0.93412041758744, 0.41374052024363}, { 0.96063943315511, 0.93116709541280}, +{ 0.97534253457837, 0.86150930812689}, { 0.99642466504163, 0.70190043427512}, +{-0.94705089665984, -0.29580042814306}, { 0.91599807087376, -0.98147830385781}, +}; + +#endif /* AVCODEC_AACSBRDATA_H */ diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 8983f052e4..3d162d7918 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -30,7 +30,7 @@ #include "libavutil/avutil.h" #define LIBAVCODEC_VERSION_MAJOR 52 -#define LIBAVCODEC_VERSION_MINOR 56 +#define LIBAVCODEC_VERSION_MINOR 57 #define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ diff --git a/libavcodec/mpeg4audio.h b/libavcodec/mpeg4audio.h index 59e8f1135b..69b1b732a2 100644 --- a/libavcodec/mpeg4audio.h +++ b/libavcodec/mpeg4audio.h @@ -57,7 +57,7 @@ enum AudioObjectType { AOT_AAC_LC, ///< Y Low Complexity AOT_AAC_SSR, ///< N (code in SoC repo) Scalable Sample Rate AOT_AAC_LTP, ///< N (code in SoC repo) Long Term Prediction - AOT_SBR, ///< N (in progress) Spectral Band Replication + AOT_SBR, ///< Y Spectral Band Replication AOT_AAC_SCALABLE, ///< N Scalable AOT_TWINVQ, ///< N Twin Vector Quantizer AOT_CELP, ///< N Code Excited Linear Prediction diff --git a/libavcodec/sbr.h b/libavcodec/sbr.h new file mode 100644 index 0000000000..5095bdc0cb --- /dev/null +++ b/libavcodec/sbr.h @@ -0,0 +1,187 @@ +/* + * Spectral Band Replication definitions and structures + * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl ) + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file libavcodec/sbr.h + * Spectral Band Replication definitions and structures + * @author Robert Swain ( rob opendot cl ) + */ + +#ifndef AVCODEC_SBR_H +#define AVCODEC_SBR_H + +#include +#include "fft.h" + +/** + * 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; + + /** + * @defgroup bs_header_extra_1 Variables associated with bs_header_extra_1 + * @{ + */ + uint8_t bs_freq_scale; + uint8_t bs_alter_scale; + uint8_t bs_noise_bands; + /** @} */ +} SpectrumParameters; + +#define SBR_SYNTHESIS_BUF_SIZE ((1280-128)*2) + +/** + * Spectral Band Replication per channel data + */ +typedef struct { + /** + * @defgroup bitstream Main bitstream data variables + * @{ + */ + unsigned bs_frame_class; + unsigned bs_add_harmonic_flag; + unsigned 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]; + unsigned bs_pointer; + unsigned bs_num_noise; + uint8_t bs_df_env[5]; + uint8_t bs_df_noise[2]; + uint8_t bs_invf_mode[2][5]; + uint8_t bs_add_harmonic[48]; + unsigned bs_amp_res; + /** @} */ + + /** + * @defgroup state State variables + * @{ + */ + DECLARE_ALIGNED(16, float, synthesis_filterbank_samples)[SBR_SYNTHESIS_BUF_SIZE]; + DECLARE_ALIGNED(16, float, analysis_filterbank_samples) [1312]; + int synthesis_filterbank_samples_offset; + ///l_APrev and l_A + int e_a[2]; + ///Chirp factors + float bw_array[5]; + ///QMF values of the original signal + float W[2][32][32][2]; + ///QMF output of the HF adjustor + float Y[2][38][64][2]; + float g_temp[42][48]; + float q_temp[42][48]; + uint8_t s_indexmapped[8][48]; + ///Envelope scalefactors + float env_facs[6][48]; + ///Noise scalefactors + float noise_facs[3][5]; + ///Envelope time borders + uint8_t t_env[8]; + ///Envelope time border of the last envelope of the previous frame + uint8_t t_env_num_env_old; + ///Noise time borders + uint8_t t_q[3]; + unsigned f_indexnoise; + unsigned f_indexsine; + /** @} */ +} SBRData; + +/** + * Spectral Band Replication + */ +typedef struct { + int sample_rate; + int start; + int reset; + SpectrumParameters spectrum_params; + int bs_amp_res_header; + /** + * @defgroup bs_header_extra_2 variables associated with bs_header_extra_2 + * @{ + */ + unsigned bs_limiter_bands; + unsigned bs_limiter_gains; + unsigned bs_interpol_freq; + unsigned bs_smoothing_mode; + /** @} */ + unsigned bs_coupling; + unsigned k[5]; ///< k0, k1, k2 + ///kx', and kx respectively, kx is the first QMF subband where SBR is used. + ///kx' is its value from the previous frame + unsigned kx[2]; + ///M' and M respectively, M is the number of QMF subbands that use SBR. + unsigned m[2]; + ///The number of frequency bands in f_master + unsigned n_master; + SBRData data[2]; + ///N_Low and N_High respectively, the number of frequency bands for low and high resolution + unsigned n[2]; + ///Number of noise floor bands + unsigned n_q; + ///Number of limiter bands + unsigned n_lim; + ///The master QMF frequency grouping + uint16_t f_master[49]; + ///Frequency borders for low resolution SBR + uint16_t f_tablelow[25]; + ///Frequency borders for high resolution SBR + uint16_t f_tablehigh[49]; + ///Frequency borders for noise floors + uint16_t f_tablenoise[6]; + ///Frequency borders for the limiter + uint16_t f_tablelim[29]; + unsigned num_patches; + uint8_t patch_num_subbands[6]; + uint8_t patch_start_subband[6]; + ///QMF low frequency input to the HF generator + float X_low[32][40][2]; + ///QMF output of the HF generator + float X_high[64][40][2]; + ///QMF values of the reconstructed signal + DECLARE_ALIGNED(16, float, X)[2][32][64]; + ///Zeroth coefficient used to filter the subband signals + float alpha0[64][2]; + ///First coefficient used to filter the subband signals + float alpha1[64][2]; + ///Dequantized envelope scalefactors, remapped + float e_origmapped[7][48]; + ///Dequantized noise scalefactors, remapped + float q_mapped[7][48]; + ///Sinusoidal presence, remapped + uint8_t s_mapped[7][48]; + ///Estimated envelope + float e_curr[7][48]; + ///Amplitude adjusted noise scalefactors + float q_m[7][48]; + ///Sinusoidal levels + float s_m[7][48]; + float gain[7][48]; + DECLARE_ALIGNED(16, float, qmf_filter_scratch)[5][64]; + RDFTContext rdft; + FFTContext mdct; +} SpectralBandReplication; + +#endif /* AVCODEC_SBR_H */