diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/aac.c b/src/add-ons/media/plugins/avcodec/libavcodec/aac.c new file mode 100644 index 0000000000..8747cd0b9b --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/aac.c @@ -0,0 +1,1465 @@ +/* + * AAC decoder + * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org ) + * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com ) + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file aac.c + * AAC decoder + * @author Oded Shimon ( ods15 ods15 dyndns org ) + * @author Maxim Gavrilov ( maxim.gavrilov gmail com ) + */ + +/* + * supported tools + * + * Support? Name + * N (code in SoC repo) gain control + * Y block switching + * Y window shapes - standard + * N window shapes - Low Delay + * Y filterbank - standard + * N (code in SoC repo) filterbank - Scalable Sample Rate + * Y Temporal Noise Shaping + * N (code in SoC repo) Long Term Prediction + * Y intensity stereo + * Y channel coupling + * N frequency domain prediction + * Y Perceptual Noise Substitution + * Y Mid/Side stereo + * N Scalable Inverse AAC Quantization + * N Frequency Selective Switch + * N upsampling filter + * Y quantization & coding - AAC + * N quantization & coding - TwinVQ + * N quantization & coding - BSAC + * N AAC Error Resilience tools + * N Error Resilience payload syntax + * N Error Protection tool + * N CELP + * N Silence Compression + * N HVXC + * N HVXC 4kbits/s VR + * N Structured Audio tools + * N Structured Audio Sample Bank Format + * N MIDI + * N Harmonic and Individual Lines plus Noise + * N Text-To-Speech Interface + * N (in progress) Spectral Band Replication + * Y (not in this code) Layer-1 + * Y (not in this code) Layer-2 + * Y (not in this code) Layer-3 + * N SinuSoidal Coding (Transient, Sinusoid, Noise) + * N (planned) Parametric Stereo + * N Direct Stream Transfer + * + * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication. + * - HE AAC v2 comprises LC AAC with Spectral Band Replication and + Parametric Stereo. + */ + + +#include "avcodec.h" +#include "bitstream.h" +#include "dsputil.h" + +#include "aac.h" +#include "aactab.h" +#include "aacdectab.h" +#include "mpeg4audio.h" + +#include +#include +#include +#include + +static VLC vlc_scalefactors; +static VLC vlc_spectral[11]; + + +/** + * Configure output channel order based on the current program configuration element. + * + * @param che_pos current channel position configuration + * @param new_che_pos New channel position configuration - we only do something if it differs from the current one. + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int output_configure(AACContext *ac, enum ChannelPosition che_pos[4][MAX_ELEM_ID], + enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]) { + AVCodecContext *avctx = ac->avccontext; + int i, type, channels = 0; + + if(!memcmp(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]))) + return 0; /* no change */ + + memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])); + + /* Allocate or free elements depending on if they are in the + * current program configuration. + * + * Set up default 1:1 output mapping. + * + * For a 5.1 stream the output order will be: + * [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ] + */ + + for(i = 0; i < MAX_ELEM_ID; i++) { + for(type = 0; type < 4; type++) { + if(che_pos[type][i]) { + if(!ac->che[type][i] && !(ac->che[type][i] = av_mallocz(sizeof(ChannelElement)))) + return AVERROR(ENOMEM); + if(type != TYPE_CCE) { + ac->output_data[channels++] = ac->che[type][i]->ch[0].ret; + if(type == TYPE_CPE) { + ac->output_data[channels++] = ac->che[type][i]->ch[1].ret; + } + } + } else + av_freep(&ac->che[type][i]); + } + } + + avctx->channels = channels; + return 0; +} + +/** + * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit. + * + * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present. + * @param sce_map mono (Single Channel Element) map + * @param type speaker type/position for these channels + */ +static void decode_channel_map(enum ChannelPosition *cpe_map, + enum ChannelPosition *sce_map, enum ChannelPosition type, GetBitContext * gb, int n) { + while(n--) { + enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map + map[get_bits(gb, 4)] = type; + } +} + +/** + * Decode program configuration element; reference: table 4.2. + * + * @param new_che_pos New channel position configuration - we only do something if it differs from the current one. + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_pce(AACContext * ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID], + GetBitContext * gb) { + int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc; + + skip_bits(gb, 2); // object_type + + ac->m4ac.sampling_index = get_bits(gb, 4); + if(ac->m4ac.sampling_index > 11) { + av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); + return -1; + } + ac->m4ac.sample_rate = ff_mpeg4audio_sample_rates[ac->m4ac.sampling_index]; + num_front = get_bits(gb, 4); + num_side = get_bits(gb, 4); + num_back = get_bits(gb, 4); + num_lfe = get_bits(gb, 2); + num_assoc_data = get_bits(gb, 3); + num_cc = get_bits(gb, 4); + + if (get_bits1(gb)) + skip_bits(gb, 4); // mono_mixdown_tag + if (get_bits1(gb)) + skip_bits(gb, 4); // stereo_mixdown_tag + + if (get_bits1(gb)) + skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround + + decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front); + decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side ); + decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back ); + decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe ); + + skip_bits_long(gb, 4 * num_assoc_data); + + decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc ); + + align_get_bits(gb); + + /* comment field, first byte is length */ + skip_bits_long(gb, 8 * get_bits(gb, 8)); + return 0; +} + +/** + * Set up channel positions based on a default channel configuration + * as specified in table 1.17. + * + * @param new_che_pos New channel position configuration - we only do something if it differs from the current one. + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int set_default_channel_config(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID], + int channel_config) +{ + if(channel_config < 1 || channel_config > 7) { + av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n", + channel_config); + return -1; + } + + /* default channel configurations: + * + * 1ch : front center (mono) + * 2ch : L + R (stereo) + * 3ch : front center + L + R + * 4ch : front center + L + R + back center + * 5ch : front center + L + R + back stereo + * 6ch : front center + L + R + back stereo + LFE + * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE + */ + + if(channel_config != 2) + new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono) + if(channel_config > 1) + new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo) + if(channel_config == 4) + new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center + if(channel_config > 4) + new_che_pos[TYPE_CPE][(channel_config == 7) + 1] + = AAC_CHANNEL_BACK; // back stereo + if(channel_config > 5) + new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE + if(channel_config == 7) + new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right + + return 0; +} + +/** + * Decode GA "General Audio" specific configuration; reference: table 4.1. + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_ga_specific_config(AACContext * ac, GetBitContext * gb, int channel_config) { + enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]; + int extension_flag, ret; + + if(get_bits1(gb)) { // frameLengthFlag + av_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1); + return -1; + } + + if (get_bits1(gb)) // dependsOnCoreCoder + skip_bits(gb, 14); // coreCoderDelay + extension_flag = get_bits1(gb); + + if(ac->m4ac.object_type == AOT_AAC_SCALABLE || + ac->m4ac.object_type == AOT_ER_AAC_SCALABLE) + skip_bits(gb, 3); // layerNr + + memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])); + if (channel_config == 0) { + skip_bits(gb, 4); // element_instance_tag + if((ret = decode_pce(ac, new_che_pos, gb))) + return ret; + } else { + if((ret = set_default_channel_config(ac, new_che_pos, channel_config))) + return ret; + } + if((ret = output_configure(ac, ac->che_pos, new_che_pos))) + return ret; + + if (extension_flag) { + switch (ac->m4ac.object_type) { + case AOT_ER_BSAC: + skip_bits(gb, 5); // numOfSubFrame + skip_bits(gb, 11); // layer_length + break; + case AOT_ER_AAC_LC: + case AOT_ER_AAC_LTP: + case AOT_ER_AAC_SCALABLE: + case AOT_ER_AAC_LD: + skip_bits(gb, 3); /* aacSectionDataResilienceFlag + * aacScalefactorDataResilienceFlag + * aacSpectralDataResilienceFlag + */ + break; + } + skip_bits1(gb); // extensionFlag3 (TBD in version 3) + } + return 0; +} + +/** + * Decode audio specific configuration; reference: table 1.13. + * + * @param data pointer to AVCodecContext extradata + * @param data_size size of AVCCodecContext extradata + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_audio_specific_config(AACContext * ac, void *data, int data_size) { + GetBitContext gb; + int i; + + init_get_bits(&gb, data, data_size * 8); + + if((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0) { + av_log(ac->avccontext, AV_LOG_ERROR, "ff_mpeg4audio_get_config failed\n"); + return -1; + } + + av_log(ac->avccontext, AV_LOG_DEBUG, "Details decoded Sample Rate %d ext Sample Rate %d Channels %d\n",ac->m4ac.sample_rate, ac->m4ac.ext_sample_rate, ac->m4ac.chan_config); + + if(ac->m4ac.sampling_index > 11) { + av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); + return -1; + } + + skip_bits_long(&gb, i); + + switch (ac->m4ac.object_type) { + case AOT_AAC_LC: + if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config)) { + av_log(ac->avccontext, AV_LOG_ERROR, "decode_ga_specific_config failed\n"); + return -1; + } + break; + default: + av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n", + ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type); + return -1; + } + return 0; +} + +/** + * linear congruential pseudorandom number generator + * + * @param previous_val pointer to the current state of the generator + * + * @return Returns a 32-bit pseudorandom integer + */ +static av_always_inline int lcg_random(int previous_val) { + return previous_val * 1664525 + 1013904223; +} + +static av_cold int aac_decode_init(AVCodecContext * avccontext) { + AACContext * ac = avccontext->priv_data; + int i; + + ac->avccontext = avccontext; + + if (avccontext->extradata_size <= 0 || + decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size)) { + av_log(avccontext, AV_LOG_ERROR, "no audio config data supplied (%ld) or data invalid\n",avccontext->extradata_size); + + return -1; + } + + avccontext->sample_fmt = SAMPLE_FMT_S16; + avccontext->sample_rate = ac->m4ac.sample_rate; + avccontext->frame_size = 1024; + + AAC_INIT_VLC_STATIC( 0, 144); + AAC_INIT_VLC_STATIC( 1, 114); + AAC_INIT_VLC_STATIC( 2, 188); + AAC_INIT_VLC_STATIC( 3, 180); + AAC_INIT_VLC_STATIC( 4, 172); + AAC_INIT_VLC_STATIC( 5, 140); + AAC_INIT_VLC_STATIC( 6, 168); + AAC_INIT_VLC_STATIC( 7, 114); + AAC_INIT_VLC_STATIC( 8, 262); + AAC_INIT_VLC_STATIC( 9, 248); + AAC_INIT_VLC_STATIC(10, 384); + + dsputil_init(&ac->dsp, avccontext); + + ac->random_state = 0x1f2e3d4c; + + // -1024 - Compensate wrong IMDCT method. + // 32768 - Required to scale values to the correct range for the bias method + // for float to int16 conversion. + + if(ac->dsp.float_to_int16 == ff_float_to_int16_c) { + ac->add_bias = 385.0f; + ac->sf_scale = 1. / (-1024. * 32768.); + ac->sf_offset = 0; + } else { + ac->add_bias = 0.0f; + ac->sf_scale = 1. / -1024.; + ac->sf_offset = 60; + } + +#ifndef CONFIG_HARDCODED_TABLES + for (i = 0; i < 316; i++) + ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.); +#endif /* CONFIG_HARDCODED_TABLES */ + + INIT_VLC_STATIC(&vlc_scalefactors, 7, sizeof(ff_aac_scalefactor_code)/sizeof(ff_aac_scalefactor_code[0]), + ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]), + ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]), + 352); + + ff_mdct_init(&ac->mdct, 11, 1); + ff_mdct_init(&ac->mdct_small, 8, 1); + // window initialization + ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024); + ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128); + ff_sine_window_init(ff_sine_1024, 1024); + ff_sine_window_init(ff_sine_128, 128); + + return 0; +} + +/** + * Skip data_stream_element; reference: table 4.10. + */ +static void skip_data_stream_element(GetBitContext * gb) { + int byte_align = get_bits1(gb); + int count = get_bits(gb, 8); + if (count == 255) + count += get_bits(gb, 8); + if (byte_align) + align_get_bits(gb); + skip_bits_long(gb, 8 * count); +} + +/** + * Decode Individual Channel Stream info; reference: table 4.6. + * + * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information. + */ +static int decode_ics_info(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb, int common_window) { + if (get_bits1(gb)) { + av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n"); + memset(ics, 0, sizeof(IndividualChannelStream)); + return -1; + } + ics->window_sequence[1] = ics->window_sequence[0]; + ics->window_sequence[0] = get_bits(gb, 2); + ics->use_kb_window[1] = ics->use_kb_window[0]; + ics->use_kb_window[0] = get_bits1(gb); + ics->num_window_groups = 1; + ics->group_len[0] = 1; + if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { + int i; + ics->max_sfb = get_bits(gb, 4); + for (i = 0; i < 7; i++) { + if (get_bits1(gb)) { + ics->group_len[ics->num_window_groups-1]++; + } else { + ics->num_window_groups++; + ics->group_len[ics->num_window_groups-1] = 1; + } + } + ics->num_windows = 8; + ics->swb_offset = swb_offset_128[ac->m4ac.sampling_index]; + ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index]; + ics->tns_max_bands = tns_max_bands_128[ac->m4ac.sampling_index]; + } else { + ics->max_sfb = get_bits(gb, 6); + ics->num_windows = 1; + ics->swb_offset = swb_offset_1024[ac->m4ac.sampling_index]; + ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index]; + ics->tns_max_bands = tns_max_bands_1024[ac->m4ac.sampling_index]; + if (get_bits1(gb)) { + av_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1); + memset(ics, 0, sizeof(IndividualChannelStream)); + return -1; + } + } + + if(ics->max_sfb > ics->num_swb) { + av_log(ac->avccontext, AV_LOG_ERROR, + "Number of scalefactor bands in group (%d) exceeds limit (%d).\n", + ics->max_sfb, ics->num_swb); + memset(ics, 0, sizeof(IndividualChannelStream)); + return -1; + } + + return 0; +} + +/** + * Decode band types (section_data payload); reference: table 4.46. + * + * @param band_type array of the used band type + * @param band_type_run_end array of the last scalefactor band of a band type run + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_band_types(AACContext * ac, enum BandType band_type[120], + int band_type_run_end[120], GetBitContext * gb, IndividualChannelStream * ics) { + int g, idx = 0; + const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5; + for (g = 0; g < ics->num_window_groups; g++) { + int k = 0; + while (k < ics->max_sfb) { + uint8_t sect_len = k; + int sect_len_incr; + int sect_band_type = get_bits(gb, 4); + if (sect_band_type == 12) { + av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n"); + return -1; + } + while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits)-1) + sect_len += sect_len_incr; + sect_len += sect_len_incr; + if (sect_len > ics->max_sfb) { + av_log(ac->avccontext, AV_LOG_ERROR, + "Number of bands (%d) exceeds limit (%d).\n", + sect_len, ics->max_sfb); + return -1; + } + for (; k < sect_len; k++) { + band_type [idx] = sect_band_type; + band_type_run_end[idx++] = sect_len; + } + } + } + return 0; +} + +/** + * Decode scalefactors; reference: table 4.47. + * + * @param global_gain first scalefactor value as scalefactors are differentially coded + * @param band_type array of the used band type + * @param band_type_run_end array of the last scalefactor band of a band type run + * @param sf array of scalefactors or intensity stereo positions + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_scalefactors(AACContext * ac, float sf[120], GetBitContext * gb, + unsigned int global_gain, IndividualChannelStream * ics, + enum BandType band_type[120], int band_type_run_end[120]) { + const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0); + int g, i, idx = 0; + int offset[3] = { global_gain, global_gain - 90, 100 }; + int noise_flag = 1; + static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" }; + for (g = 0; g < ics->num_window_groups; g++) { + for (i = 0; i < ics->max_sfb;) { + int run_end = band_type_run_end[idx]; + if (band_type[idx] == ZERO_BT) { + for(; i < run_end; i++, idx++) + sf[idx] = 0.; + }else if((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) { + for(; i < run_end; i++, idx++) { + offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; + if(offset[2] > 255U) { + av_log(ac->avccontext, AV_LOG_ERROR, + "%s (%d) out of range.\n", sf_str[2], offset[2]); + return -1; + } + sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300]; + } + }else if(band_type[idx] == NOISE_BT) { + for(; i < run_end; i++, idx++) { + if(noise_flag-- > 0) + offset[1] += get_bits(gb, 9) - 256; + else + offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; + if(offset[1] > 255U) { + av_log(ac->avccontext, AV_LOG_ERROR, + "%s (%d) out of range.\n", sf_str[1], offset[1]); + return -1; + } + sf[idx] = -ff_aac_pow2sf_tab[ offset[1] + sf_offset]; + } + }else { + for(; i < run_end; i++, idx++) { + offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; + if(offset[0] > 255U) { + av_log(ac->avccontext, AV_LOG_ERROR, + "%s (%d) out of range.\n", sf_str[0], offset[0]); + return -1; + } + sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset]; + } + } + } + } + return 0; +} + +/** + * Decode pulse data; reference: table 4.7. + */ +static void decode_pulses(Pulse * pulse, GetBitContext * gb, const uint16_t * swb_offset) { + int i; + pulse->num_pulse = get_bits(gb, 2) + 1; + pulse->pos[0] = get_bits(gb, 5) + swb_offset[get_bits(gb, 6)]; + pulse->amp[0] = get_bits(gb, 4); + for (i = 1; i < pulse->num_pulse; i++) { + pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i-1]; + pulse->amp[i] = get_bits(gb, 4); + } +} + +/** + * Decode Temporal Noise Shaping data; reference: table 4.48. + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_tns(AACContext * ac, TemporalNoiseShaping * tns, + GetBitContext * gb, const IndividualChannelStream * ics) { + int w, filt, i, coef_len, coef_res, coef_compress; + const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE; + const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12; + for (w = 0; w < ics->num_windows; w++) { + if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) { + coef_res = get_bits1(gb); + + for (filt = 0; filt < tns->n_filt[w]; filt++) { + int tmp2_idx; + tns->length[w][filt] = get_bits(gb, 6 - 2*is8); + + if ((tns->order[w][filt] = get_bits(gb, 5 - 2*is8)) > tns_max_order) { + av_log(ac->avccontext, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.", + tns->order[w][filt], tns_max_order); + tns->order[w][filt] = 0; + return -1; + } + tns->direction[w][filt] = get_bits1(gb); + coef_compress = get_bits1(gb); + coef_len = coef_res + 3 - coef_compress; + tmp2_idx = 2*coef_compress + coef_res; + + for (i = 0; i < tns->order[w][filt]; i++) + tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)]; + } + } + } + return 0; +} + +/** + * Decode Mid/Side data; reference: table 4.54. + * + * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s; + * [1] mask is decoded from bitstream; [2] mask is all 1s; + * [3] reserved for scalable AAC + */ +static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb, + int ms_present) { + int idx; + if (ms_present == 1) { + for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++) + cpe->ms_mask[idx] = get_bits1(gb); + } else if (ms_present == 2) { + memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0])); + } +} + +/** + * Decode spectral data; reference: table 4.50. + * Dequantize and scale spectral data; reference: 4.6.3.3. + * + * @param coef array of dequantized, scaled spectral data + * @param sf array of scalefactors or intensity stereo positions + * @param pulse_present set if pulses are present + * @param pulse pointer to pulse data struct + * @param band_type array of the used band type + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_spectrum_and_dequant(AACContext * ac, float coef[1024], GetBitContext * gb, float sf[120], + int pulse_present, const Pulse * pulse, const IndividualChannelStream * ics, enum BandType band_type[120]) { + int i, k, g, idx = 0; + const int c = 1024/ics->num_windows; + const uint16_t * offsets = ics->swb_offset; + float *coef_base = coef; + + for (g = 0; g < ics->num_windows; g++) + memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float)*(c - offsets[ics->max_sfb])); + + for (g = 0; g < ics->num_window_groups; g++) { + for (i = 0; i < ics->max_sfb; i++, idx++) { + const int cur_band_type = band_type[idx]; + const int dim = cur_band_type >= FIRST_PAIR_BT ? 2 : 4; + const int is_cb_unsigned = IS_CODEBOOK_UNSIGNED(cur_band_type); + int group; + if (cur_band_type == ZERO_BT) { + for (group = 0; group < ics->group_len[g]; group++) { + memset(coef + group * 128 + offsets[i], 0, (offsets[i+1] - offsets[i])*sizeof(float)); + } + }else if (cur_band_type == NOISE_BT) { + const float scale = sf[idx] / ((offsets[i+1] - offsets[i]) * PNS_MEAN_ENERGY); + for (group = 0; group < ics->group_len[g]; group++) { + for (k = offsets[i]; k < offsets[i+1]; k++) { + ac->random_state = lcg_random(ac->random_state); + coef[group*128+k] = ac->random_state * scale; + } + } + }else if (cur_band_type != INTENSITY_BT2 && cur_band_type != INTENSITY_BT) { + for (group = 0; group < ics->group_len[g]; group++) { + for (k = offsets[i]; k < offsets[i+1]; k += dim) { + const int index = get_vlc2(gb, vlc_spectral[cur_band_type - 1].table, 6, 3); + const int coef_tmp_idx = (group << 7) + k; + const float *vq_ptr; + int j; + if(index >= ff_aac_spectral_sizes[cur_band_type - 1]) { + av_log(ac->avccontext, AV_LOG_ERROR, + "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n", + cur_band_type - 1, index, ff_aac_spectral_sizes[cur_band_type - 1]); + return -1; + } + vq_ptr = &ff_aac_codebook_vectors[cur_band_type - 1][index * dim]; + if (is_cb_unsigned) { + for (j = 0; j < dim; j++) + if (vq_ptr[j]) + coef[coef_tmp_idx + j] = 1 - 2*(int)get_bits1(gb); + }else { + for (j = 0; j < dim; j++) + coef[coef_tmp_idx + j] = 1.0f; + } + if (cur_band_type == ESC_BT) { + for (j = 0; j < 2; j++) { + if (vq_ptr[j] == 64.0f) { + int n = 4; + /* The total length of escape_sequence must be < 22 bits according + to the specification (i.e. max is 11111111110xxxxxxxxxx). */ + while (get_bits1(gb) && n < 15) n++; + if(n == 15) { + av_log(ac->avccontext, AV_LOG_ERROR, "error in spectral data, ESC overflow\n"); + return -1; + } + n = (1<group_len[g]<<7; + } + + if (pulse_present) { + for(i = 0; i < pulse->num_pulse; i++){ + float co = coef_base[ pulse->pos[i] ]; + float ico = co / sqrtf(sqrtf(fabsf(co))) + pulse->amp[i]; + coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico; + } + } + return 0; +} + +/** + * Decode an individual_channel_stream payload; reference: table 4.44. + * + * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information. + * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.) + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_ics(AACContext * ac, SingleChannelElement * sce, GetBitContext * gb, int common_window, int scale_flag) { + Pulse pulse; + TemporalNoiseShaping * tns = &sce->tns; + IndividualChannelStream * ics = &sce->ics; + float * out = sce->coeffs; + int global_gain, pulse_present = 0; + + /* This assignment is to silence a GCC warning about the variable being used + * uninitialized when in fact it always is. + */ + pulse.num_pulse = 0; + + global_gain = get_bits(gb, 8); + + if (!common_window && !scale_flag) { + if (decode_ics_info(ac, ics, gb, 0) < 0) + return -1; + } + + if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0) + return -1; + if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0) + return -1; + + pulse_present = 0; + if (!scale_flag) { + if ((pulse_present = get_bits1(gb))) { + if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { + av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n"); + return -1; + } + decode_pulses(&pulse, gb, ics->swb_offset); + } + if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics)) + return -1; + if (get_bits1(gb)) { + av_log_missing_feature(ac->avccontext, "SSR", 1); + return -1; + } + } + + if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0) + return -1; + return 0; +} + +/** + * Mid/Side stereo decoding; reference: 4.6.8.1.3. + */ +static void apply_mid_side_stereo(ChannelElement * cpe) { + const IndividualChannelStream * ics = &cpe->ch[0].ics; + float *ch0 = cpe->ch[0].coeffs; + float *ch1 = cpe->ch[1].coeffs; + int g, i, k, group, idx = 0; + const uint16_t * offsets = ics->swb_offset; + for (g = 0; g < ics->num_window_groups; g++) { + for (i = 0; i < ics->max_sfb; i++, idx++) { + if (cpe->ms_mask[idx] && + cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) { + for (group = 0; group < ics->group_len[g]; group++) { + for (k = offsets[i]; k < offsets[i+1]; k++) { + float tmp = ch0[group*128 + k] - ch1[group*128 + k]; + ch0[group*128 + k] += ch1[group*128 + k]; + ch1[group*128 + k] = tmp; + } + } + } + } + ch0 += ics->group_len[g]*128; + ch1 += ics->group_len[g]*128; + } +} + +/** + * intensity stereo decoding; reference: 4.6.8.2.3 + * + * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s; + * [1] mask is decoded from bitstream; [2] mask is all 1s; + * [3] reserved for scalable AAC + */ +static void apply_intensity_stereo(ChannelElement * cpe, int ms_present) { + const IndividualChannelStream * ics = &cpe->ch[1].ics; + SingleChannelElement * sce1 = &cpe->ch[1]; + float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs; + const uint16_t * offsets = ics->swb_offset; + int g, group, i, k, idx = 0; + int c; + float scale; + for (g = 0; g < ics->num_window_groups; g++) { + for (i = 0; i < ics->max_sfb;) { + if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) { + const int bt_run_end = sce1->band_type_run_end[idx]; + for (; i < bt_run_end; i++, idx++) { + c = -1 + 2 * (sce1->band_type[idx] - 14); + if (ms_present) + c *= 1 - 2 * cpe->ms_mask[idx]; + scale = c * sce1->sf[idx]; + for (group = 0; group < ics->group_len[g]; group++) + for (k = offsets[i]; k < offsets[i+1]; k++) + coef1[group*128 + k] = scale * coef0[group*128 + k]; + } + } else { + int bt_run_end = sce1->band_type_run_end[idx]; + idx += bt_run_end - i; + i = bt_run_end; + } + } + coef0 += ics->group_len[g]*128; + coef1 += ics->group_len[g]*128; + } +} + +/** + * Decode a channel_pair_element; reference: table 4.4. + * + * @param elem_id Identifies the instance of a syntax element. + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_cpe(AACContext * ac, GetBitContext * gb, int elem_id) { + int i, ret, common_window, ms_present = 0; + ChannelElement * cpe; + + cpe = ac->che[TYPE_CPE][elem_id]; + common_window = get_bits1(gb); + if (common_window) { + if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1)) + return -1; + i = cpe->ch[1].ics.use_kb_window[0]; + cpe->ch[1].ics = cpe->ch[0].ics; + cpe->ch[1].ics.use_kb_window[1] = i; + ms_present = get_bits(gb, 2); + if(ms_present == 3) { + av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n"); + return -1; + } else if(ms_present) + decode_mid_side_stereo(cpe, gb, ms_present); + } + if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0))) + return ret; + if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0))) + return ret; + + if (common_window && ms_present) + apply_mid_side_stereo(cpe); + + apply_intensity_stereo(cpe, ms_present); + return 0; +} + +/** + * Decode coupling_channel_element; reference: table 4.8. + * + * @param elem_id Identifies the instance of a syntax element. + * + * @return Returns error status. 0 - OK, !0 - error + */ +static int decode_cce(AACContext * ac, GetBitContext * gb, ChannelElement * che) { + int num_gain = 0; + int c, g, sfb, ret, idx = 0; + int sign; + float scale; + SingleChannelElement * sce = &che->ch[0]; + ChannelCoupling * coup = &che->coup; + + coup->coupling_point = 2*get_bits1(gb); + coup->num_coupled = get_bits(gb, 3); + for (c = 0; c <= coup->num_coupled; c++) { + num_gain++; + coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE; + coup->id_select[c] = get_bits(gb, 4); + if (coup->type[c] == TYPE_CPE) { + coup->ch_select[c] = get_bits(gb, 2); + if (coup->ch_select[c] == 3) + num_gain++; + } else + coup->ch_select[c] = 1; + } + coup->coupling_point += get_bits1(gb); + + if (coup->coupling_point == 2) { + av_log(ac->avccontext, AV_LOG_ERROR, + "Independently switched CCE with 'invalid' domain signalled.\n"); + memset(coup, 0, sizeof(ChannelCoupling)); + return -1; + } + + sign = get_bits(gb, 1); + scale = pow(2., pow(2., get_bits(gb, 2) - 3)); + + if ((ret = decode_ics(ac, sce, gb, 0, 0))) + return ret; + + for (c = 0; c < num_gain; c++) { + int cge = 1; + int gain = 0; + float gain_cache = 1.; + if (c) { + cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb); + gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0; + gain_cache = pow(scale, gain); + } + for (g = 0; g < sce->ics.num_window_groups; g++) + for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) + if (sce->band_type[idx] != ZERO_BT) { + if (!cge) { + int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; + if (t) { + int s = 1; + if (sign) { + s -= 2 * (t & 0x1); + t >>= 1; + } + gain += t; + gain_cache = pow(scale, gain) * s; + } + } + coup->gain[c][idx] = gain_cache; + } + } + 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. + * + * @return Returns number of bytes consumed. + */ +static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext * gb) { + int i; + int num_excl_chan = 0; + + do { + for (i = 0; i < 7; i++) + che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb); + } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb)); + + return num_excl_chan / 7; +} + +/** + * Decode dynamic range information; reference: table 4.52. + * + * @param cnt length of TYPE_FIL syntactic element in bytes + * + * @return Returns number of bytes consumed. + */ +static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext * gb, int cnt) { + int n = 1; + int drc_num_bands = 1; + int i; + + /* pce_tag_present? */ + if(get_bits1(gb)) { + che_drc->pce_instance_tag = get_bits(gb, 4); + skip_bits(gb, 4); // tag_reserved_bits + n++; + } + + /* excluded_chns_present? */ + if(get_bits1(gb)) { + n += decode_drc_channel_exclusions(che_drc, gb); + } + + /* drc_bands_present? */ + if (get_bits1(gb)) { + che_drc->band_incr = get_bits(gb, 4); + che_drc->interpolation_scheme = get_bits(gb, 4); + n++; + drc_num_bands += che_drc->band_incr; + for (i = 0; i < drc_num_bands; i++) { + che_drc->band_top[i] = get_bits(gb, 8); + n++; + } + } + + /* prog_ref_level_present? */ + if (get_bits1(gb)) { + che_drc->prog_ref_level = get_bits(gb, 7); + skip_bits1(gb); // prog_ref_level_reserved_bits + n++; + } + + for (i = 0; i < drc_num_bands; i++) { + che_drc->dyn_rng_sgn[i] = get_bits1(gb); + che_drc->dyn_rng_ctl[i] = get_bits(gb, 7); + n++; + } + + return n; +} + +/** + * Decode extension data (incomplete); reference: table 4.51. + * + * @param cnt length of TYPE_FIL syntactic element in bytes + * + * @return Returns number of bytes consumed + */ +static int decode_extension_payload(AACContext * ac, GetBitContext * gb, int cnt) { + int crc_flag = 0; + int res = cnt; + switch (get_bits(gb, 4)) { // extension type + case EXT_SBR_DATA_CRC: + crc_flag++; + case EXT_SBR_DATA: + res = decode_sbr_extension(ac, gb, crc_flag, cnt); + break; + case EXT_DYNAMIC_RANGE: + res = decode_dynamic_range(&ac->che_drc, gb, cnt); + break; + case EXT_FILL: + case EXT_FILL_DATA: + case EXT_DATA_ELEMENT: + default: + skip_bits_long(gb, 8*cnt - 4); + break; + }; + return res; +} + +/** + * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3. + * + * @param decode 1 if tool is used normally, 0 if tool is used in LTP. + * @param coef spectral coefficients + */ +static void apply_tns(float coef[1024], TemporalNoiseShaping * tns, IndividualChannelStream * ics, int decode) { + const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb); + int w, filt, m, i; + int bottom, top, order, start, end, size, inc; + float lpc[TNS_MAX_ORDER]; + + for (w = 0; w < ics->num_windows; w++) { + bottom = ics->num_swb; + for (filt = 0; filt < tns->n_filt[w]; filt++) { + top = bottom; + bottom = FFMAX(0, top - tns->length[w][filt]); + order = tns->order[w][filt]; + if (order == 0) + continue; + + /* tns_decode_coef + * FIXME: This duplicates the functionality of some double code in lpc.c. + */ + for (m = 0; m < order; m++) { + float tmp; + lpc[m] = tns->coef[w][filt][m]; + for (i = 0; i < m/2; i++) { + tmp = lpc[i]; + lpc[i] += lpc[m] * lpc[m-1-i]; + lpc[m-1-i] += lpc[m] * tmp; + } + if(m & 1) + lpc[i] += lpc[m] * lpc[i]; + } + + start = ics->swb_offset[FFMIN(bottom, mmm)]; + end = ics->swb_offset[FFMIN( top, mmm)]; + if ((size = end - start) <= 0) + continue; + if (tns->direction[w][filt]) { + inc = -1; start = end - 1; + } else { + inc = 1; + } + start += w * 128; + + // ar filter + for (m = 0; m < size; m++, start += inc) + for (i = 1; i <= FFMIN(m, order); i++) + coef[start] -= coef[start - i*inc] * lpc[i-1]; + } + } +} + +/** + * Conduct IMDCT and windowing. + */ +static void imdct_and_windowing(AACContext * ac, SingleChannelElement * sce) { + IndividualChannelStream * ics = &sce->ics; + float * in = sce->coeffs; + float * out = sce->ret; + float * saved = sce->saved; + const float * swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128; + const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024; + const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128; + float * buf = ac->buf_mdct; + DECLARE_ALIGNED(16, float, temp[128]); + int i; + + // imdct + if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { + if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) + av_log(ac->avccontext, AV_LOG_WARNING, + "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. " + "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n"); + for (i = 0; i < 1024; i += 128) + ff_imdct_half(&ac->mdct_small, buf + i, in + i); + } else + ff_imdct_half(&ac->mdct, buf, in); + + /* window overlapping + * NOTE: To simplify the overlapping code, all 'meaningless' short to long + * and long to short transitions are considered to be short to short + * transitions. This leaves just two cases (long to long and short to short) + * with a little special sauce for EIGHT_SHORT_SEQUENCE. + */ + if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) && + (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) { + ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, ac->add_bias, 512); + } else { + for (i = 0; i < 448; i++) + out[i] = saved[i] + ac->add_bias; + + if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { + ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, ac->add_bias, 64); + ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, ac->add_bias, 64); + ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, ac->add_bias, 64); + ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, ac->add_bias, 64); + ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, ac->add_bias, 64); + memcpy( out + 448 + 4*128, temp, 64 * sizeof(float)); + } else { + ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, ac->add_bias, 64); + for (i = 576; i < 1024; i++) + out[i] = buf[i-512] + ac->add_bias; + } + } + + // buffer update + if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { + for (i = 0; i < 64; i++) + saved[i] = temp[64 + i] - ac->add_bias; + ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64); + ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64); + ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64); + memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float)); + } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) { + memcpy( saved, buf + 512, 448 * sizeof(float)); + memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float)); + } else { // LONG_STOP or ONLY_LONG + memcpy( saved, buf + 512, 512 * sizeof(float)); + } +} + +/** + * Apply dependent channel coupling (applied before IMDCT). + * + * @param index index into coupling gain array + */ +static void apply_dependent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) { + IndividualChannelStream * ics = &cc->ch[0].ics; + const uint16_t * offsets = ics->swb_offset; + float * dest = sce->coeffs; + const float * src = cc->ch[0].coeffs; + int g, i, group, k, idx = 0; + if(ac->m4ac.object_type == AOT_AAC_LTP) { + av_log(ac->avccontext, AV_LOG_ERROR, + "Dependent coupling is not supported together with LTP\n"); + return; + } + for (g = 0; g < ics->num_window_groups; g++) { + for (i = 0; i < ics->max_sfb; i++, idx++) { + if (cc->ch[0].band_type[idx] != ZERO_BT) { + for (group = 0; group < ics->group_len[g]; group++) { + for (k = offsets[i]; k < offsets[i+1]; k++) { + // XXX dsputil-ize + dest[group*128+k] += cc->coup.gain[index][idx] * src[group*128+k]; + } + } + } + } + dest += ics->group_len[g]*128; + src += ics->group_len[g]*128; + } +} + +/** + * Apply independent channel coupling (applied after IMDCT). + * + * @param index index into coupling gain array + */ +static void apply_independent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) { + int i; + for (i = 0; i < 1024; i++) + sce->ret[i] += cc->coup.gain[index][0] * (cc->ch[0].ret[i] - ac->add_bias); +} + +/** + * channel coupling transformation interface + * + * @param index index into coupling gain array + * @param apply_coupling_method pointer to (in)dependent coupling function + */ +static void apply_channel_coupling(AACContext * ac, ChannelElement * cc, + void (*apply_coupling_method)(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index)) +{ + int c; + int index = 0; + ChannelCoupling * coup = &cc->coup; + for (c = 0; c <= coup->num_coupled; c++) { + if (ac->che[coup->type[c]][coup->id_select[c]]) { + if (coup->ch_select[c] != 2) { + apply_coupling_method(ac, &ac->che[coup->type[c]][coup->id_select[c]]->ch[0], cc, index); + if (coup->ch_select[c] != 0) + index++; + } + if (coup->ch_select[c] != 1) + apply_coupling_method(ac, &ac->che[coup->type[c]][coup->id_select[c]]->ch[1], cc, index++); + } else { + av_log(ac->avccontext, AV_LOG_ERROR, + "coupling target %sE[%d] not available\n", + coup->type[c] == TYPE_CPE ? "CP" : "SC", coup->id_select[c]); + break; + } + } +} + +/** + * Convert spectral data to float samples, applying all supported tools as appropriate. + */ +static void spectral_to_sample(AACContext * ac) { + int i, type; + for (i = 0; i < MAX_ELEM_ID; i++) { + for(type = 0; type < 4; type++) { + ChannelElement *che = ac->che[type][i]; + if(che) { + if(che->coup.coupling_point == BEFORE_TNS) + apply_channel_coupling(ac, che, apply_dependent_coupling); + if(che->ch[0].tns.present) + apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1); + if(che->ch[1].tns.present) + apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1); + if(che->coup.coupling_point == BETWEEN_TNS_AND_IMDCT) + apply_channel_coupling(ac, che, apply_dependent_coupling); + imdct_and_windowing(ac, &che->ch[0]); + if(type == TYPE_CPE) + imdct_and_windowing(ac, &che->ch[1]); + if(che->coup.coupling_point == AFTER_IMDCT) + apply_channel_coupling(ac, che, apply_independent_coupling); + } + } + } +} + +static int aac_decode_frame(AVCodecContext * avccontext, void * data, int * data_size, const uint8_t * buf, int buf_size) { + AACContext * ac = avccontext->priv_data; + GetBitContext gb; + enum RawDataBlockType elem_type; + int err, elem_id, data_size_tmp; + + init_get_bits(&gb, buf, buf_size*8); + + // parse + while ((elem_type = get_bits(&gb, 3)) != TYPE_END) { + elem_id = get_bits(&gb, 4); + err = -1; + + if(elem_type == TYPE_SCE && elem_id == 1 && + !ac->che[TYPE_SCE][elem_id] && ac->che[TYPE_LFE][0]) { + /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1] + instead of SCE[0] CPE[0] CPE[0] LFE[0]. If we seem to have + encountered such a stream, transfer the LFE[0] element to SCE[1] */ + ac->che[TYPE_SCE][elem_id] = ac->che[TYPE_LFE][0]; + ac->che[TYPE_LFE][0] = NULL; + } + if(elem_type && elem_type < TYPE_DSE) { + if(!ac->che[elem_type][elem_id]) + return -1; + if(elem_type != TYPE_CCE) + ac->che[elem_type][elem_id]->coup.coupling_point = 4; + } + + switch (elem_type) { + + case TYPE_SCE: + err = decode_ics(ac, &ac->che[TYPE_SCE][elem_id]->ch[0], &gb, 0, 0); + break; + + case TYPE_CPE: + err = decode_cpe(ac, &gb, elem_id); + break; + + case TYPE_CCE: + err = decode_cce(ac, &gb, ac->che[TYPE_SCE][elem_id]); + break; + + case TYPE_LFE: + err = decode_ics(ac, &ac->che[TYPE_LFE][elem_id]->ch[0], &gb, 0, 0); + break; + + case TYPE_DSE: + skip_data_stream_element(&gb); + err = 0; + break; + + case TYPE_PCE: + { + enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]; + memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])); + if((err = decode_pce(ac, new_che_pos, &gb))) + break; + err = output_configure(ac, ac->che_pos, new_che_pos); + break; + } + + case TYPE_FIL: + if (elem_id == 15) + elem_id += get_bits(&gb, 8) - 1; + while (elem_id > 0) + elem_id -= decode_extension_payload(ac, &gb, elem_id); + err = 0; /* FIXME */ + break; + + default: + err = -1; /* should not happen, but keeps compiler happy */ + break; + } + + if(err) + return err; + } + + spectral_to_sample(ac); + + if (!ac->is_saved) { + ac->is_saved = 1; + *data_size = 0; + return buf_size; + } + + data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t); + if(*data_size < data_size_tmp) { + av_log(avccontext, AV_LOG_ERROR, + "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n", + *data_size, data_size_tmp); + return -1; + } + *data_size = data_size_tmp; + + ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels); + + return buf_size; +} + +static av_cold int aac_decode_close(AVCodecContext * avccontext) { + AACContext * ac = avccontext->priv_data; + int i, type; + + for (i = 0; i < MAX_ELEM_ID; i++) { + for(type = 0; type < 4; type++) + av_freep(&ac->che[type][i]); + } + + ff_mdct_end(&ac->mdct); + ff_mdct_end(&ac->mdct_small); + return 0 ; +} + +AVCodec aac_decoder = { + "aac", + CODEC_TYPE_AUDIO, + CODEC_ID_AAC, + sizeof(AACContext), + aac_decode_init, + NULL, + aac_decode_close, + aac_decode_frame, + .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"), + .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/aac_ac3_parser.c b/src/add-ons/media/plugins/avcodec/libavcodec/aac_ac3_parser.c new file mode 100644 index 0000000000..cfb1a222be --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/aac_ac3_parser.c @@ -0,0 +1,88 @@ +/* + * Common AAC and AC-3 parser + * Copyright (c) 2003 Fabrice Bellard. + * Copyright (c) 2003 Michael Niedermayer. + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "parser.h" +#include "aac_ac3_parser.h" + +int ff_aac_ac3_parse(AVCodecParserContext *s1, + AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + AACAC3ParseContext *s = s1->priv_data; + ParseContext *pc = &s->pc; + int len, i; + int new_frame_start; + +get_next: + i=END_NOT_FOUND; + if(s->remaining_size <= buf_size){ + if(s->remaining_size && !s->need_next_header){ + i= s->remaining_size; + s->remaining_size = 0; + }else{ //we need a header first + len=0; + for(i=s->remaining_size; istate = (s->state<<8) + buf[i]; + if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start))) + break; + } + if(len<=0){ + i=END_NOT_FOUND; + }else{ + i-= s->header_size -1; + s->remaining_size = len; + if(!new_frame_start){ + s->remaining_size += i; + goto get_next; + } + } + } + } + + if(ff_combine_frame(pc, i, &buf, &buf_size)<0){ + s->remaining_size -= FFMIN(s->remaining_size, buf_size); + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + + /* update codec info */ + avctx->sample_rate = s->sample_rate; + /* allow downmixing to stereo (or mono for AC-3) */ + if(avctx->request_channels > 0 && + avctx->request_channels < s->channels && + (avctx->request_channels <= 2 || + (avctx->request_channels == 1 && + avctx->codec_id == CODEC_ID_AC3))) { + avctx->channels = avctx->request_channels; + } else { + avctx->channels = s->channels; + } + avctx->bit_rate = s->bit_rate; + avctx->frame_size = s->samples; + + return i; +} diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/aac_parser.c b/src/add-ons/media/plugins/avcodec/libavcodec/aac_parser.c new file mode 100644 index 0000000000..db2c697725 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/aac_parser.c @@ -0,0 +1,91 @@ +/* + * Audio and Video frame extraction + * Copyright (c) 2003 Fabrice Bellard. + * Copyright (c) 2003 Michael Niedermayer. + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "parser.h" +#include "aac_ac3_parser.h" +#include "bitstream.h" +#include "mpeg4audio.h" + +#define AAC_HEADER_SIZE 7 + +static int aac_sync(uint64_t state, AACAC3ParseContext *hdr_info, + int *need_next_header, int *new_frame_start) +{ + GetBitContext bits; + int size, rdb, ch, sr; + uint64_t tmp = be2me_64(state); + + init_get_bits(&bits, ((uint8_t *)&tmp)+8-AAC_HEADER_SIZE, AAC_HEADER_SIZE * 8); + + if(get_bits(&bits, 12) != 0xfff) + return 0; + + skip_bits1(&bits); /* id */ + skip_bits(&bits, 2); /* layer */ + skip_bits1(&bits); /* protection_absent */ + skip_bits(&bits, 2); /* profile_objecttype */ + sr = get_bits(&bits, 4); /* sample_frequency_index */ + if(!ff_mpeg4audio_sample_rates[sr]) + return 0; + skip_bits1(&bits); /* private_bit */ + ch = get_bits(&bits, 3); /* channel_configuration */ + if(!ff_mpeg4audio_channels[ch]) + return 0; + skip_bits1(&bits); /* original/copy */ + skip_bits1(&bits); /* home */ + + /* adts_variable_header */ + skip_bits1(&bits); /* copyright_identification_bit */ + skip_bits1(&bits); /* copyright_identification_start */ + size = get_bits(&bits, 13); /* aac_frame_length */ + if(size < AAC_HEADER_SIZE) + return 0; + + skip_bits(&bits, 11); /* adts_buffer_fullness */ + rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */ + + hdr_info->channels = ff_mpeg4audio_channels[ch]; + hdr_info->sample_rate = ff_mpeg4audio_sample_rates[sr]; + hdr_info->samples = (rdb + 1) * 1024; + hdr_info->bit_rate = size * 8 * hdr_info->sample_rate / hdr_info->samples; + + *need_next_header = 0; + *new_frame_start = 1; + return size; +} + +static av_cold int aac_parse_init(AVCodecParserContext *s1) +{ + AACAC3ParseContext *s = s1->priv_data; + s->header_size = AAC_HEADER_SIZE; + s->sync = aac_sync; + return 0; +} + + +AVCodecParser aac_parser = { + { CODEC_ID_AAC }, + sizeof(AACAC3ParseContext), + aac_parse_init, + ff_aac_ac3_parse, + NULL, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/aacenc.c b/src/add-ons/media/plugins/avcodec/libavcodec/aacenc.c new file mode 100644 index 0000000000..fff69645b3 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/aacenc.c @@ -0,0 +1,365 @@ +/* + * AAC encoder + * Copyright (C) 2008 Konstantin Shishkov + * + * 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 aacenc.c + * AAC encoder + */ + +/*********************************** + * TODOs: + * psy model selection with some option + * add sane pulse detection + * add temporal noise shaping + ***********************************/ + +#include "avcodec.h" +#include "bitstream.h" +#include "dsputil.h" +#include "mpeg4audio.h" + +#include "aacpsy.h" +#include "aac.h" +#include "aactab.h" + +static const uint8_t swb_size_1024_96[] = { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, + 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 +}; + +static const uint8_t swb_size_1024_64[] = { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, + 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36, + 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 +}; + +static const uint8_t swb_size_1024_48[] = { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, + 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 96 +}; + +static const uint8_t swb_size_1024_32[] = { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, + 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 +}; + +static const uint8_t swb_size_1024_24[] = { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28, + 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64 +}; + +static const uint8_t swb_size_1024_16[] = { + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28, + 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64 +}; + +static const uint8_t swb_size_1024_8[] = { + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28, + 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80 +}; + +static const uint8_t *swb_size_1024[] = { + swb_size_1024_96, swb_size_1024_96, swb_size_1024_64, + swb_size_1024_48, swb_size_1024_48, swb_size_1024_32, + swb_size_1024_24, swb_size_1024_24, swb_size_1024_16, + swb_size_1024_16, swb_size_1024_16, swb_size_1024_8 +}; + +static const uint8_t swb_size_128_96[] = { + 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36 +}; + +static const uint8_t swb_size_128_48[] = { + 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16 +}; + +static const uint8_t swb_size_128_24[] = { + 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20 +}; + +static const uint8_t swb_size_128_16[] = { + 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20 +}; + +static const uint8_t swb_size_128_8[] = { + 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20 +}; + +static const uint8_t *swb_size_128[] = { + /* the last entry on the following row is swb_size_128_64 but is a + duplicate of swb_size_128_96 */ + swb_size_128_96, swb_size_128_96, swb_size_128_96, + swb_size_128_48, swb_size_128_48, swb_size_128_48, + swb_size_128_24, swb_size_128_24, swb_size_128_16, + swb_size_128_16, swb_size_128_16, swb_size_128_8 +}; + +/** bits needed to code codebook run value for long windows */ +static const uint8_t run_value_bits_long[64] = { + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15 +}; + +/** bits needed to code codebook run value for short windows */ +static const uint8_t run_value_bits_short[16] = { + 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9 +}; + +static const uint8_t* run_value_bits[2] = { + run_value_bits_long, run_value_bits_short +}; + +/** default channel configurations */ +static const uint8_t aac_chan_configs[6][5] = { + {1, TYPE_SCE}, // 1 channel - single channel element + {1, TYPE_CPE}, // 2 channels - channel pair + {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo + {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center + {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo + {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE +}; + +/** + * structure used in optimal codebook search + */ +typedef struct BandCodingPath { + int prev_idx; ///< pointer to the previous path point + int codebook; ///< codebook for coding band run + int bits; ///< number of bit needed to code given number of bands +} BandCodingPath; + +/** + * AAC encoder context + */ +typedef struct { + PutBitContext pb; + MDCTContext mdct1024; ///< long (1024 samples) frame transform context + MDCTContext mdct128; ///< short (128 samples) frame transform context + DSPContext dsp; + DECLARE_ALIGNED_16(FFTSample, output[2048]); ///< temporary buffer for MDCT input coefficients + int16_t* samples; ///< saved preprocessed input + + int samplerate_index; ///< MPEG-4 samplerate index + + ChannelElement *cpe; ///< channel elements + AACPsyContext psy; ///< psychoacoustic model context + int last_frame; +} AACEncContext; + +/** + * Make AAC audio config object. + * @see 1.6.2.1 "Syntax - AudioSpecificConfig" + */ +static void put_audio_specific_config(AVCodecContext *avctx) +{ + PutBitContext pb; + AACEncContext *s = avctx->priv_data; + + init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8); + put_bits(&pb, 5, 2); //object type - AAC-LC + put_bits(&pb, 4, s->samplerate_index); //sample rate index + put_bits(&pb, 4, avctx->channels); + //GASpecificConfig + put_bits(&pb, 1, 0); //frame length - 1024 samples + put_bits(&pb, 1, 0); //does not depend on core coder + put_bits(&pb, 1, 0); //is not extension + flush_put_bits(&pb); +} + +static av_cold int aac_encode_init(AVCodecContext *avctx) +{ + AACEncContext *s = avctx->priv_data; + int i; + + avctx->frame_size = 1024; + + for(i = 0; i < 16; i++) + if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i]) + break; + if(i == 16){ + av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate); + return -1; + } + if(avctx->channels > 6){ + av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels); + return -1; + } + s->samplerate_index = i; + + dsputil_init(&s->dsp, avctx); + ff_mdct_init(&s->mdct1024, 11, 0); + ff_mdct_init(&s->mdct128, 8, 0); + // window init + ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024); + ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128); + ff_sine_window_init(ff_sine_1024, 1024); + ff_sine_window_init(ff_sine_128, 128); + + s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0])); + s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]); + if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP, + aac_chan_configs[avctx->channels-1][0], 0, + swb_size_1024[i], ff_aac_num_swb_1024[i], swb_size_128[i], ff_aac_num_swb_128[i]) < 0){ + av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n"); + return -1; + } + avctx->extradata = av_malloc(2); + avctx->extradata_size = 2; + put_audio_specific_config(avctx); + return 0; +} + +/** + * Encode ics_info element. + * @see Table 4.6 (syntax of ics_info) + */ +static void put_ics_info(AACEncContext *s, IndividualChannelStream *info) +{ + int i; + + put_bits(&s->pb, 1, 0); // ics_reserved bit + put_bits(&s->pb, 2, info->window_sequence[0]); + put_bits(&s->pb, 1, info->use_kb_window[0]); + if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){ + put_bits(&s->pb, 6, info->max_sfb); + put_bits(&s->pb, 1, 0); // no prediction + }else{ + put_bits(&s->pb, 4, info->max_sfb); + for(i = 1; i < info->num_windows; i++) + put_bits(&s->pb, 1, info->group_len[i]); + } +} + +/** + * Calculate the number of bits needed to code all coefficient signs in current band. + */ +static int calculate_band_sign_bits(AACEncContext *s, SingleChannelElement *sce, + int group_len, int start, int size) +{ + int bits = 0; + int i, w; + for(w = 0; w < group_len; w++){ + for(i = 0; i < size; i++){ + if(sce->icoefs[start + i]) + bits++; + } + start += 128; + } + return bits; +} + +/** + * Encode pulse data. + */ +static void encode_pulses(AACEncContext *s, Pulse *pulse) +{ + int i; + + put_bits(&s->pb, 1, !!pulse->num_pulse); + if(!pulse->num_pulse) return; + + put_bits(&s->pb, 2, pulse->num_pulse - 1); + put_bits(&s->pb, 6, pulse->start); + for(i = 0; i < pulse->num_pulse; i++){ + put_bits(&s->pb, 5, pulse->pos[i]); + put_bits(&s->pb, 4, pulse->amp[i]); + } +} + +/** + * Encode spectral coefficients processed by psychoacoustic model. + */ +static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce) +{ + int start, i, w, w2, wg; + + w = 0; + for(wg = 0; wg < sce->ics.num_window_groups; wg++){ + start = 0; + for(i = 0; i < sce->ics.max_sfb; i++){ + if(sce->zeroes[w*16 + i]){ + start += sce->ics.swb_sizes[i]; + continue; + } + for(w2 = w; w2 < w + sce->ics.group_len[wg]; w2++){ + encode_band_coeffs(s, sce, start + w2*128, + sce->ics.swb_sizes[i], + sce->band_type[w*16 + i]); + } + start += sce->ics.swb_sizes[i]; + } + w += sce->ics.group_len[wg]; + } +} + +/** + * Write some auxiliary information about the created AAC file. + */ +static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name) +{ + int i, namelen, padbits; + + namelen = strlen(name) + 2; + put_bits(&s->pb, 3, TYPE_FIL); + put_bits(&s->pb, 4, FFMIN(namelen, 15)); + if(namelen >= 15) + put_bits(&s->pb, 8, namelen - 16); + put_bits(&s->pb, 4, 0); //extension type - filler + padbits = 8 - (put_bits_count(&s->pb) & 7); + align_put_bits(&s->pb); + for(i = 0; i < namelen - 2; i++) + put_bits(&s->pb, 8, name[i]); + put_bits(&s->pb, 12 - padbits, 0); +} + +static av_cold int aac_encode_end(AVCodecContext *avctx) +{ + AACEncContext *s = avctx->priv_data; + + ff_mdct_end(&s->mdct1024); + ff_mdct_end(&s->mdct128); + ff_aac_psy_end(&s->psy); + av_freep(&s->samples); + av_freep(&s->cpe); + return 0; +} + +AVCodec aac_encoder = { + "aac", + CODEC_TYPE_AUDIO, + CODEC_ID_AAC, + sizeof(AACEncContext), + aac_encode_init, + aac_encode_frame, + aac_encode_end, + .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY, + .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, + .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"), +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/aacpsy.c b/src/add-ons/media/plugins/avcodec/libavcodec/aacpsy.c new file mode 100644 index 0000000000..5a8a8522e9 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/aacpsy.c @@ -0,0 +1,104 @@ +/* + * AAC encoder psychoacoustic model + * Copyright (C) 2008 Konstantin Shishkov + * + * 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 aacpsy.c + * AAC encoder psychoacoustic model + */ + +#include "avcodec.h" +#include "aacpsy.h" +#include "aactab.h" + +/*********************************** + * TODOs: + * General: + * better audio preprocessing (add DC highpass filter?) + * more psy models + * maybe improve coefficient quantization function in some way + * + * 3GPP-based psy model: + * thresholds linearization after their modifications for attaining given bitrate + * try other bitrate controlling mechanism (maybe use ratecontrol.c?) + * control quality for quality-based output + **********************************/ + +/** + * Quantize one coefficient. + * @return absolute value of the quantized coefficient + * @see 3GPP TS26.403 5.6.2 "Scalefactor determination" + */ +static av_always_inline int quant(float coef, const float Q) +{ + return av_clip((int)(pow(fabsf(coef) * Q, 0.75) + 0.4054), 0, 8191); +} + +static inline float get_approximate_quant_error(float *c, int size, int scale_idx) +{ + int i; + int q; + float coef, unquant, sum = 0.0f; + const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; + const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; + for(i = 0; i < size; i++){ + coef = fabs(c[i]); + q = quant(c[i], Q); + unquant = (q * cbrt(q)) * IQ; + sum += (coef - unquant) * (coef - unquant); + } + return sum; +} + +/** + * constants for 3GPP AAC psychoacoustic model + * @{ + */ +#define PSY_3GPP_SPREAD_LOW 1.5f // spreading factor for ascending threshold spreading (15 dB/Bark) +#define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark) +/** + * @} + */ + +/** + * information for single band used by 3GPP TS26.403-inspired psychoacoustic model + */ +typedef struct Psy3gppBand{ + float energy; ///< band energy + float ffac; ///< form factor +}Psy3gppBand; + +/** + * psychoacoustic model frame type-dependent coefficients + */ +typedef struct Psy3gppCoeffs{ + float ath [64]; ///< absolute threshold of hearing per bands + float barks [64]; ///< Bark value for each spectral band in long frame + float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame + float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame +}Psy3gppCoeffs; + +/** + * Calculate Bark value for given line. + */ +static inline float calc_bark(float f) +{ + return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f)); +} diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/aactab.c b/src/add-ons/media/plugins/avcodec/libavcodec/aactab.c new file mode 100644 index 0000000000..aee1d73941 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/aactab.c @@ -0,0 +1,993 @@ +/* + * AAC data + * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org ) + * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com ) + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file aactab.c + * AAC data + * @author Oded Shimon ( ods15 ods15 dyndns org ) + * @author Maxim Gavrilov ( maxim.gavrilov gmail com ) + */ + +#include "mem.h" +#include "aac.h" + +#include + +DECLARE_ALIGNED(16, float, ff_aac_kbd_long_1024[1024]); +DECLARE_ALIGNED(16, float, ff_aac_kbd_short_128[128]); + +const uint8_t ff_aac_num_swb_1024[] = { + 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40 +}; + +const uint8_t ff_aac_num_swb_128[] = { + 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15 +}; + +const uint32_t ff_aac_scalefactor_code[121] = { + 0x3ffe8, 0x3ffe6, 0x3ffe7, 0x3ffe5, 0x7fff5, 0x7fff1, 0x7ffed, 0x7fff6, + 0x7ffee, 0x7ffef, 0x7fff0, 0x7fffc, 0x7fffd, 0x7ffff, 0x7fffe, 0x7fff7, + 0x7fff8, 0x7fffb, 0x7fff9, 0x3ffe4, 0x7fffa, 0x3ffe3, 0x1ffef, 0x1fff0, + 0x0fff5, 0x1ffee, 0x0fff2, 0x0fff3, 0x0fff4, 0x0fff1, 0x07ff6, 0x07ff7, + 0x03ff9, 0x03ff5, 0x03ff7, 0x03ff3, 0x03ff6, 0x03ff2, 0x01ff7, 0x01ff5, + 0x00ff9, 0x00ff7, 0x00ff6, 0x007f9, 0x00ff4, 0x007f8, 0x003f9, 0x003f7, + 0x003f5, 0x001f8, 0x001f7, 0x000fa, 0x000f8, 0x000f6, 0x00079, 0x0003a, + 0x00038, 0x0001a, 0x0000b, 0x00004, 0x00000, 0x0000a, 0x0000c, 0x0001b, + 0x00039, 0x0003b, 0x00078, 0x0007a, 0x000f7, 0x000f9, 0x001f6, 0x001f9, + 0x003f4, 0x003f6, 0x003f8, 0x007f5, 0x007f4, 0x007f6, 0x007f7, 0x00ff5, + 0x00ff8, 0x01ff4, 0x01ff6, 0x01ff8, 0x03ff8, 0x03ff4, 0x0fff0, 0x07ff4, + 0x0fff6, 0x07ff5, 0x3ffe2, 0x7ffd9, 0x7ffda, 0x7ffdb, 0x7ffdc, 0x7ffdd, + 0x7ffde, 0x7ffd8, 0x7ffd2, 0x7ffd3, 0x7ffd4, 0x7ffd5, 0x7ffd6, 0x7fff2, + 0x7ffdf, 0x7ffe7, 0x7ffe8, 0x7ffe9, 0x7ffea, 0x7ffeb, 0x7ffe6, 0x7ffe0, + 0x7ffe1, 0x7ffe2, 0x7ffe3, 0x7ffe4, 0x7ffe5, 0x7ffd7, 0x7ffec, 0x7fff4, + 0x7fff3, +}; + +const uint8_t ff_aac_scalefactor_bits[121] = { + 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 18, 19, 18, 17, 17, 16, 17, 16, 16, 16, 16, 15, 15, + 14, 14, 14, 14, 14, 14, 13, 13, 12, 12, 12, 11, 12, 11, 10, 10, + 10, 9, 9, 8, 8, 8, 7, 6, 6, 5, 4, 3, 1, 4, 4, 5, + 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12, + 12, 13, 13, 13, 14, 14, 16, 15, 16, 15, 18, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, +}; + +static const uint16_t codes1[81] = { + 0x7f8, 0x1f1, 0x7fd, 0x3f5, 0x068, 0x3f0, 0x7f7, 0x1ec, + 0x7f5, 0x3f1, 0x072, 0x3f4, 0x074, 0x011, 0x076, 0x1eb, + 0x06c, 0x3f6, 0x7fc, 0x1e1, 0x7f1, 0x1f0, 0x061, 0x1f6, + 0x7f2, 0x1ea, 0x7fb, 0x1f2, 0x069, 0x1ed, 0x077, 0x017, + 0x06f, 0x1e6, 0x064, 0x1e5, 0x067, 0x015, 0x062, 0x012, + 0x000, 0x014, 0x065, 0x016, 0x06d, 0x1e9, 0x063, 0x1e4, + 0x06b, 0x013, 0x071, 0x1e3, 0x070, 0x1f3, 0x7fe, 0x1e7, + 0x7f3, 0x1ef, 0x060, 0x1ee, 0x7f0, 0x1e2, 0x7fa, 0x3f3, + 0x06a, 0x1e8, 0x075, 0x010, 0x073, 0x1f4, 0x06e, 0x3f7, + 0x7f6, 0x1e0, 0x7f9, 0x3f2, 0x066, 0x1f5, 0x7ff, 0x1f7, + 0x7f4, +}; + +static const uint8_t bits1[81] = { + 11, 9, 11, 10, 7, 10, 11, 9, 11, 10, 7, 10, 7, 5, 7, 9, + 7, 10, 11, 9, 11, 9, 7, 9, 11, 9, 11, 9, 7, 9, 7, 5, + 7, 9, 7, 9, 7, 5, 7, 5, 1, 5, 7, 5, 7, 9, 7, 9, + 7, 5, 7, 9, 7, 9, 11, 9, 11, 9, 7, 9, 11, 9, 11, 10, + 7, 9, 7, 5, 7, 9, 7, 10, 11, 9, 11, 10, 7, 9, 11, 9, + 11, +}; + +static const uint16_t codes2[81] = { + 0x1f3, 0x06f, 0x1fd, 0x0eb, 0x023, 0x0ea, 0x1f7, 0x0e8, + 0x1fa, 0x0f2, 0x02d, 0x070, 0x020, 0x006, 0x02b, 0x06e, + 0x028, 0x0e9, 0x1f9, 0x066, 0x0f8, 0x0e7, 0x01b, 0x0f1, + 0x1f4, 0x06b, 0x1f5, 0x0ec, 0x02a, 0x06c, 0x02c, 0x00a, + 0x027, 0x067, 0x01a, 0x0f5, 0x024, 0x008, 0x01f, 0x009, + 0x000, 0x007, 0x01d, 0x00b, 0x030, 0x0ef, 0x01c, 0x064, + 0x01e, 0x00c, 0x029, 0x0f3, 0x02f, 0x0f0, 0x1fc, 0x071, + 0x1f2, 0x0f4, 0x021, 0x0e6, 0x0f7, 0x068, 0x1f8, 0x0ee, + 0x022, 0x065, 0x031, 0x002, 0x026, 0x0ed, 0x025, 0x06a, + 0x1fb, 0x072, 0x1fe, 0x069, 0x02e, 0x0f6, 0x1ff, 0x06d, + 0x1f6, +}; + +static const uint8_t bits2[81] = { + 9, 7, 9, 8, 6, 8, 9, 8, 9, 8, 6, 7, 6, 5, 6, 7, + 6, 8, 9, 7, 8, 8, 6, 8, 9, 7, 9, 8, 6, 7, 6, 5, + 6, 7, 6, 8, 6, 5, 6, 5, 3, 5, 6, 5, 6, 8, 6, 7, + 6, 5, 6, 8, 6, 8, 9, 7, 9, 8, 6, 8, 8, 7, 9, 8, + 6, 7, 6, 4, 6, 8, 6, 7, 9, 7, 9, 7, 6, 8, 9, 7, + 9, +}; + +static const uint16_t codes3[81] = { + 0x0000, 0x0009, 0x00ef, 0x000b, 0x0019, 0x00f0, 0x01eb, 0x01e6, + 0x03f2, 0x000a, 0x0035, 0x01ef, 0x0034, 0x0037, 0x01e9, 0x01ed, + 0x01e7, 0x03f3, 0x01ee, 0x03ed, 0x1ffa, 0x01ec, 0x01f2, 0x07f9, + 0x07f8, 0x03f8, 0x0ff8, 0x0008, 0x0038, 0x03f6, 0x0036, 0x0075, + 0x03f1, 0x03eb, 0x03ec, 0x0ff4, 0x0018, 0x0076, 0x07f4, 0x0039, + 0x0074, 0x03ef, 0x01f3, 0x01f4, 0x07f6, 0x01e8, 0x03ea, 0x1ffc, + 0x00f2, 0x01f1, 0x0ffb, 0x03f5, 0x07f3, 0x0ffc, 0x00ee, 0x03f7, + 0x7ffe, 0x01f0, 0x07f5, 0x7ffd, 0x1ffb, 0x3ffa, 0xffff, 0x00f1, + 0x03f0, 0x3ffc, 0x01ea, 0x03ee, 0x3ffb, 0x0ff6, 0x0ffa, 0x7ffc, + 0x07f2, 0x0ff5, 0xfffe, 0x03f4, 0x07f7, 0x7ffb, 0x0ff7, 0x0ff9, + 0x7ffa, +}; + +static const uint8_t bits3[81] = { + 1, 4, 8, 4, 5, 8, 9, 9, 10, 4, 6, 9, 6, 6, 9, 9, + 9, 10, 9, 10, 13, 9, 9, 11, 11, 10, 12, 4, 6, 10, 6, 7, + 10, 10, 10, 12, 5, 7, 11, 6, 7, 10, 9, 9, 11, 9, 10, 13, + 8, 9, 12, 10, 11, 12, 8, 10, 15, 9, 11, 15, 13, 14, 16, 8, + 10, 14, 9, 10, 14, 12, 12, 15, 11, 12, 16, 10, 11, 15, 12, 12, + 15, +}; + +static const uint16_t codes4[81] = { + 0x007, 0x016, 0x0f6, 0x018, 0x008, 0x0ef, 0x1ef, 0x0f3, + 0x7f8, 0x019, 0x017, 0x0ed, 0x015, 0x001, 0x0e2, 0x0f0, + 0x070, 0x3f0, 0x1ee, 0x0f1, 0x7fa, 0x0ee, 0x0e4, 0x3f2, + 0x7f6, 0x3ef, 0x7fd, 0x005, 0x014, 0x0f2, 0x009, 0x004, + 0x0e5, 0x0f4, 0x0e8, 0x3f4, 0x006, 0x002, 0x0e7, 0x003, + 0x000, 0x06b, 0x0e3, 0x069, 0x1f3, 0x0eb, 0x0e6, 0x3f6, + 0x06e, 0x06a, 0x1f4, 0x3ec, 0x1f0, 0x3f9, 0x0f5, 0x0ec, + 0x7fb, 0x0ea, 0x06f, 0x3f7, 0x7f9, 0x3f3, 0xfff, 0x0e9, + 0x06d, 0x3f8, 0x06c, 0x068, 0x1f5, 0x3ee, 0x1f2, 0x7f4, + 0x7f7, 0x3f1, 0xffe, 0x3ed, 0x1f1, 0x7f5, 0x7fe, 0x3f5, + 0x7fc, +}; + +static const uint8_t bits4[81] = { + 4, 5, 8, 5, 4, 8, 9, 8, 11, 5, 5, 8, 5, 4, 8, 8, + 7, 10, 9, 8, 11, 8, 8, 10, 11, 10, 11, 4, 5, 8, 4, 4, + 8, 8, 8, 10, 4, 4, 8, 4, 4, 7, 8, 7, 9, 8, 8, 10, + 7, 7, 9, 10, 9, 10, 8, 8, 11, 8, 7, 10, 11, 10, 12, 8, + 7, 10, 7, 7, 9, 10, 9, 11, 11, 10, 12, 10, 9, 11, 11, 10, + 11, +}; + +static const uint16_t codes5[81] = { + 0x1fff, 0x0ff7, 0x07f4, 0x07e8, 0x03f1, 0x07ee, 0x07f9, 0x0ff8, + 0x1ffd, 0x0ffd, 0x07f1, 0x03e8, 0x01e8, 0x00f0, 0x01ec, 0x03ee, + 0x07f2, 0x0ffa, 0x0ff4, 0x03ef, 0x01f2, 0x00e8, 0x0070, 0x00ec, + 0x01f0, 0x03ea, 0x07f3, 0x07eb, 0x01eb, 0x00ea, 0x001a, 0x0008, + 0x0019, 0x00ee, 0x01ef, 0x07ed, 0x03f0, 0x00f2, 0x0073, 0x000b, + 0x0000, 0x000a, 0x0071, 0x00f3, 0x07e9, 0x07ef, 0x01ee, 0x00ef, + 0x0018, 0x0009, 0x001b, 0x00eb, 0x01e9, 0x07ec, 0x07f6, 0x03eb, + 0x01f3, 0x00ed, 0x0072, 0x00e9, 0x01f1, 0x03ed, 0x07f7, 0x0ff6, + 0x07f0, 0x03e9, 0x01ed, 0x00f1, 0x01ea, 0x03ec, 0x07f8, 0x0ff9, + 0x1ffc, 0x0ffc, 0x0ff5, 0x07ea, 0x03f3, 0x03f2, 0x07f5, 0x0ffb, + 0x1ffe, +}; + +static const uint8_t bits5[81] = { + 13, 12, 11, 11, 10, 11, 11, 12, 13, 12, 11, 10, 9, 8, 9, 10, + 11, 12, 12, 10, 9, 8, 7, 8, 9, 10, 11, 11, 9, 8, 5, 4, + 5, 8, 9, 11, 10, 8, 7, 4, 1, 4, 7, 8, 11, 11, 9, 8, + 5, 4, 5, 8, 9, 11, 11, 10, 9, 8, 7, 8, 9, 10, 11, 12, + 11, 10, 9, 8, 9, 10, 11, 12, 13, 12, 12, 11, 10, 10, 11, 12, + 13, +}; + +static const uint16_t codes6[81] = { + 0x7fe, 0x3fd, 0x1f1, 0x1eb, 0x1f4, 0x1ea, 0x1f0, 0x3fc, + 0x7fd, 0x3f6, 0x1e5, 0x0ea, 0x06c, 0x071, 0x068, 0x0f0, + 0x1e6, 0x3f7, 0x1f3, 0x0ef, 0x032, 0x027, 0x028, 0x026, + 0x031, 0x0eb, 0x1f7, 0x1e8, 0x06f, 0x02e, 0x008, 0x004, + 0x006, 0x029, 0x06b, 0x1ee, 0x1ef, 0x072, 0x02d, 0x002, + 0x000, 0x003, 0x02f, 0x073, 0x1fa, 0x1e7, 0x06e, 0x02b, + 0x007, 0x001, 0x005, 0x02c, 0x06d, 0x1ec, 0x1f9, 0x0ee, + 0x030, 0x024, 0x02a, 0x025, 0x033, 0x0ec, 0x1f2, 0x3f8, + 0x1e4, 0x0ed, 0x06a, 0x070, 0x069, 0x074, 0x0f1, 0x3fa, + 0x7ff, 0x3f9, 0x1f6, 0x1ed, 0x1f8, 0x1e9, 0x1f5, 0x3fb, + 0x7fc, +}; + +static const uint8_t bits6[81] = { + 11, 10, 9, 9, 9, 9, 9, 10, 11, 10, 9, 8, 7, 7, 7, 8, + 9, 10, 9, 8, 6, 6, 6, 6, 6, 8, 9, 9, 7, 6, 4, 4, + 4, 6, 7, 9, 9, 7, 6, 4, 4, 4, 6, 7, 9, 9, 7, 6, + 4, 4, 4, 6, 7, 9, 9, 8, 6, 6, 6, 6, 6, 8, 9, 10, + 9, 8, 7, 7, 7, 7, 8, 10, 11, 10, 9, 9, 9, 9, 9, 10, + 11, +}; + +static const uint16_t codes7[64] = { + 0x000, 0x005, 0x037, 0x074, 0x0f2, 0x1eb, 0x3ed, 0x7f7, + 0x004, 0x00c, 0x035, 0x071, 0x0ec, 0x0ee, 0x1ee, 0x1f5, + 0x036, 0x034, 0x072, 0x0ea, 0x0f1, 0x1e9, 0x1f3, 0x3f5, + 0x073, 0x070, 0x0eb, 0x0f0, 0x1f1, 0x1f0, 0x3ec, 0x3fa, + 0x0f3, 0x0ed, 0x1e8, 0x1ef, 0x3ef, 0x3f1, 0x3f9, 0x7fb, + 0x1ed, 0x0ef, 0x1ea, 0x1f2, 0x3f3, 0x3f8, 0x7f9, 0x7fc, + 0x3ee, 0x1ec, 0x1f4, 0x3f4, 0x3f7, 0x7f8, 0xffd, 0xffe, + 0x7f6, 0x3f0, 0x3f2, 0x3f6, 0x7fa, 0x7fd, 0xffc, 0xfff, +}; + +static const uint8_t bits7[64] = { + 1, 3, 6, 7, 8, 9, 10, 11, 3, 4, 6, 7, 8, 8, 9, 9, + 6, 6, 7, 8, 8, 9, 9, 10, 7, 7, 8, 8, 9, 9, 10, 10, + 8, 8, 9, 9, 10, 10, 10, 11, 9, 8, 9, 9, 10, 10, 11, 11, + 10, 9, 9, 10, 10, 11, 12, 12, 11, 10, 10, 10, 11, 11, 12, 12, +}; + +static const uint16_t codes8[64] = { + 0x00e, 0x005, 0x010, 0x030, 0x06f, 0x0f1, 0x1fa, 0x3fe, + 0x003, 0x000, 0x004, 0x012, 0x02c, 0x06a, 0x075, 0x0f8, + 0x00f, 0x002, 0x006, 0x014, 0x02e, 0x069, 0x072, 0x0f5, + 0x02f, 0x011, 0x013, 0x02a, 0x032, 0x06c, 0x0ec, 0x0fa, + 0x071, 0x02b, 0x02d, 0x031, 0x06d, 0x070, 0x0f2, 0x1f9, + 0x0ef, 0x068, 0x033, 0x06b, 0x06e, 0x0ee, 0x0f9, 0x3fc, + 0x1f8, 0x074, 0x073, 0x0ed, 0x0f0, 0x0f6, 0x1f6, 0x1fd, + 0x3fd, 0x0f3, 0x0f4, 0x0f7, 0x1f7, 0x1fb, 0x1fc, 0x3ff, +}; + +static const uint8_t bits8[64] = { + 5, 4, 5, 6, 7, 8, 9, 10, 4, 3, 4, 5, 6, 7, 7, 8, + 5, 4, 4, 5, 6, 7, 7, 8, 6, 5, 5, 6, 6, 7, 8, 8, + 7, 6, 6, 6, 7, 7, 8, 9, 8, 7, 6, 7, 7, 8, 8, 10, + 9, 7, 7, 8, 8, 8, 9, 9, 10, 8, 8, 8, 9, 9, 9, 10, +}; + +static const uint16_t codes9[169] = { + 0x0000, 0x0005, 0x0037, 0x00e7, 0x01de, 0x03ce, 0x03d9, 0x07c8, + 0x07cd, 0x0fc8, 0x0fdd, 0x1fe4, 0x1fec, 0x0004, 0x000c, 0x0035, + 0x0072, 0x00ea, 0x00ed, 0x01e2, 0x03d1, 0x03d3, 0x03e0, 0x07d8, + 0x0fcf, 0x0fd5, 0x0036, 0x0034, 0x0071, 0x00e8, 0x00ec, 0x01e1, + 0x03cf, 0x03dd, 0x03db, 0x07d0, 0x0fc7, 0x0fd4, 0x0fe4, 0x00e6, + 0x0070, 0x00e9, 0x01dd, 0x01e3, 0x03d2, 0x03dc, 0x07cc, 0x07ca, + 0x07de, 0x0fd8, 0x0fea, 0x1fdb, 0x01df, 0x00eb, 0x01dc, 0x01e6, + 0x03d5, 0x03de, 0x07cb, 0x07dd, 0x07dc, 0x0fcd, 0x0fe2, 0x0fe7, + 0x1fe1, 0x03d0, 0x01e0, 0x01e4, 0x03d6, 0x07c5, 0x07d1, 0x07db, + 0x0fd2, 0x07e0, 0x0fd9, 0x0feb, 0x1fe3, 0x1fe9, 0x07c4, 0x01e5, + 0x03d7, 0x07c6, 0x07cf, 0x07da, 0x0fcb, 0x0fda, 0x0fe3, 0x0fe9, + 0x1fe6, 0x1ff3, 0x1ff7, 0x07d3, 0x03d8, 0x03e1, 0x07d4, 0x07d9, + 0x0fd3, 0x0fde, 0x1fdd, 0x1fd9, 0x1fe2, 0x1fea, 0x1ff1, 0x1ff6, + 0x07d2, 0x03d4, 0x03da, 0x07c7, 0x07d7, 0x07e2, 0x0fce, 0x0fdb, + 0x1fd8, 0x1fee, 0x3ff0, 0x1ff4, 0x3ff2, 0x07e1, 0x03df, 0x07c9, + 0x07d6, 0x0fca, 0x0fd0, 0x0fe5, 0x0fe6, 0x1feb, 0x1fef, 0x3ff3, + 0x3ff4, 0x3ff5, 0x0fe0, 0x07ce, 0x07d5, 0x0fc6, 0x0fd1, 0x0fe1, + 0x1fe0, 0x1fe8, 0x1ff0, 0x3ff1, 0x3ff8, 0x3ff6, 0x7ffc, 0x0fe8, + 0x07df, 0x0fc9, 0x0fd7, 0x0fdc, 0x1fdc, 0x1fdf, 0x1fed, 0x1ff5, + 0x3ff9, 0x3ffb, 0x7ffd, 0x7ffe, 0x1fe7, 0x0fcc, 0x0fd6, 0x0fdf, + 0x1fde, 0x1fda, 0x1fe5, 0x1ff2, 0x3ffa, 0x3ff7, 0x3ffc, 0x3ffd, + 0x7fff, +}; + +static const uint8_t bits9[169] = { + 1, 3, 6, 8, 9, 10, 10, 11, 11, 12, 12, 13, 13, 3, 4, 6, + 7, 8, 8, 9, 10, 10, 10, 11, 12, 12, 6, 6, 7, 8, 8, 9, + 10, 10, 10, 11, 12, 12, 12, 8, 7, 8, 9, 9, 10, 10, 11, 11, + 11, 12, 12, 13, 9, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, + 13, 10, 9, 9, 10, 11, 11, 11, 12, 11, 12, 12, 13, 13, 11, 9, + 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 11, 10, 10, 11, 11, + 12, 12, 13, 13, 13, 13, 13, 13, 11, 10, 10, 11, 11, 11, 12, 12, + 13, 13, 14, 13, 14, 11, 10, 11, 11, 12, 12, 12, 12, 13, 13, 14, + 14, 14, 12, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 12, + 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 13, 12, 12, 12, + 13, 13, 13, 13, 14, 14, 14, 14, 15, +}; + +static const uint16_t codes10[169] = { + 0x022, 0x008, 0x01d, 0x026, 0x05f, 0x0d3, 0x1cf, 0x3d0, + 0x3d7, 0x3ed, 0x7f0, 0x7f6, 0xffd, 0x007, 0x000, 0x001, + 0x009, 0x020, 0x054, 0x060, 0x0d5, 0x0dc, 0x1d4, 0x3cd, + 0x3de, 0x7e7, 0x01c, 0x002, 0x006, 0x00c, 0x01e, 0x028, + 0x05b, 0x0cd, 0x0d9, 0x1ce, 0x1dc, 0x3d9, 0x3f1, 0x025, + 0x00b, 0x00a, 0x00d, 0x024, 0x057, 0x061, 0x0cc, 0x0dd, + 0x1cc, 0x1de, 0x3d3, 0x3e7, 0x05d, 0x021, 0x01f, 0x023, + 0x027, 0x059, 0x064, 0x0d8, 0x0df, 0x1d2, 0x1e2, 0x3dd, + 0x3ee, 0x0d1, 0x055, 0x029, 0x056, 0x058, 0x062, 0x0ce, + 0x0e0, 0x0e2, 0x1da, 0x3d4, 0x3e3, 0x7eb, 0x1c9, 0x05e, + 0x05a, 0x05c, 0x063, 0x0ca, 0x0da, 0x1c7, 0x1ca, 0x1e0, + 0x3db, 0x3e8, 0x7ec, 0x1e3, 0x0d2, 0x0cb, 0x0d0, 0x0d7, + 0x0db, 0x1c6, 0x1d5, 0x1d8, 0x3ca, 0x3da, 0x7ea, 0x7f1, + 0x1e1, 0x0d4, 0x0cf, 0x0d6, 0x0de, 0x0e1, 0x1d0, 0x1d6, + 0x3d1, 0x3d5, 0x3f2, 0x7ee, 0x7fb, 0x3e9, 0x1cd, 0x1c8, + 0x1cb, 0x1d1, 0x1d7, 0x1df, 0x3cf, 0x3e0, 0x3ef, 0x7e6, + 0x7f8, 0xffa, 0x3eb, 0x1dd, 0x1d3, 0x1d9, 0x1db, 0x3d2, + 0x3cc, 0x3dc, 0x3ea, 0x7ed, 0x7f3, 0x7f9, 0xff9, 0x7f2, + 0x3ce, 0x1e4, 0x3cb, 0x3d8, 0x3d6, 0x3e2, 0x3e5, 0x7e8, + 0x7f4, 0x7f5, 0x7f7, 0xffb, 0x7fa, 0x3ec, 0x3df, 0x3e1, + 0x3e4, 0x3e6, 0x3f0, 0x7e9, 0x7ef, 0xff8, 0xffe, 0xffc, + 0xfff, +}; + +static const uint8_t bits10[169] = { + 6, 5, 6, 6, 7, 8, 9, 10, 10, 10, 11, 11, 12, 5, 4, 4, + 5, 6, 7, 7, 8, 8, 9, 10, 10, 11, 6, 4, 5, 5, 6, 6, + 7, 8, 8, 9, 9, 10, 10, 6, 5, 5, 5, 6, 7, 7, 8, 8, + 9, 9, 10, 10, 7, 6, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, + 10, 8, 7, 6, 7, 7, 7, 8, 8, 8, 9, 10, 10, 11, 9, 7, + 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 11, 9, 8, 8, 8, 8, + 8, 9, 9, 9, 10, 10, 11, 11, 9, 8, 8, 8, 8, 8, 9, 9, + 10, 10, 10, 11, 11, 10, 9, 9, 9, 9, 9, 9, 10, 10, 10, 11, + 11, 12, 10, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 11, + 10, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 11, 10, 10, 10, + 10, 10, 10, 11, 11, 12, 12, 12, 12, +}; + +static const uint16_t codes11[289] = { + 0x000, 0x006, 0x019, 0x03d, 0x09c, 0x0c6, 0x1a7, 0x390, + 0x3c2, 0x3df, 0x7e6, 0x7f3, 0xffb, 0x7ec, 0xffa, 0xffe, + 0x38e, 0x005, 0x001, 0x008, 0x014, 0x037, 0x042, 0x092, + 0x0af, 0x191, 0x1a5, 0x1b5, 0x39e, 0x3c0, 0x3a2, 0x3cd, + 0x7d6, 0x0ae, 0x017, 0x007, 0x009, 0x018, 0x039, 0x040, + 0x08e, 0x0a3, 0x0b8, 0x199, 0x1ac, 0x1c1, 0x3b1, 0x396, + 0x3be, 0x3ca, 0x09d, 0x03c, 0x015, 0x016, 0x01a, 0x03b, + 0x044, 0x091, 0x0a5, 0x0be, 0x196, 0x1ae, 0x1b9, 0x3a1, + 0x391, 0x3a5, 0x3d5, 0x094, 0x09a, 0x036, 0x038, 0x03a, + 0x041, 0x08c, 0x09b, 0x0b0, 0x0c3, 0x19e, 0x1ab, 0x1bc, + 0x39f, 0x38f, 0x3a9, 0x3cf, 0x093, 0x0bf, 0x03e, 0x03f, + 0x043, 0x045, 0x09e, 0x0a7, 0x0b9, 0x194, 0x1a2, 0x1ba, + 0x1c3, 0x3a6, 0x3a7, 0x3bb, 0x3d4, 0x09f, 0x1a0, 0x08f, + 0x08d, 0x090, 0x098, 0x0a6, 0x0b6, 0x0c4, 0x19f, 0x1af, + 0x1bf, 0x399, 0x3bf, 0x3b4, 0x3c9, 0x3e7, 0x0a8, 0x1b6, + 0x0ab, 0x0a4, 0x0aa, 0x0b2, 0x0c2, 0x0c5, 0x198, 0x1a4, + 0x1b8, 0x38c, 0x3a4, 0x3c4, 0x3c6, 0x3dd, 0x3e8, 0x0ad, + 0x3af, 0x192, 0x0bd, 0x0bc, 0x18e, 0x197, 0x19a, 0x1a3, + 0x1b1, 0x38d, 0x398, 0x3b7, 0x3d3, 0x3d1, 0x3db, 0x7dd, + 0x0b4, 0x3de, 0x1a9, 0x19b, 0x19c, 0x1a1, 0x1aa, 0x1ad, + 0x1b3, 0x38b, 0x3b2, 0x3b8, 0x3ce, 0x3e1, 0x3e0, 0x7d2, + 0x7e5, 0x0b7, 0x7e3, 0x1bb, 0x1a8, 0x1a6, 0x1b0, 0x1b2, + 0x1b7, 0x39b, 0x39a, 0x3ba, 0x3b5, 0x3d6, 0x7d7, 0x3e4, + 0x7d8, 0x7ea, 0x0ba, 0x7e8, 0x3a0, 0x1bd, 0x1b4, 0x38a, + 0x1c4, 0x392, 0x3aa, 0x3b0, 0x3bc, 0x3d7, 0x7d4, 0x7dc, + 0x7db, 0x7d5, 0x7f0, 0x0c1, 0x7fb, 0x3c8, 0x3a3, 0x395, + 0x39d, 0x3ac, 0x3ae, 0x3c5, 0x3d8, 0x3e2, 0x3e6, 0x7e4, + 0x7e7, 0x7e0, 0x7e9, 0x7f7, 0x190, 0x7f2, 0x393, 0x1be, + 0x1c0, 0x394, 0x397, 0x3ad, 0x3c3, 0x3c1, 0x3d2, 0x7da, + 0x7d9, 0x7df, 0x7eb, 0x7f4, 0x7fa, 0x195, 0x7f8, 0x3bd, + 0x39c, 0x3ab, 0x3a8, 0x3b3, 0x3b9, 0x3d0, 0x3e3, 0x3e5, + 0x7e2, 0x7de, 0x7ed, 0x7f1, 0x7f9, 0x7fc, 0x193, 0xffd, + 0x3dc, 0x3b6, 0x3c7, 0x3cc, 0x3cb, 0x3d9, 0x3da, 0x7d3, + 0x7e1, 0x7ee, 0x7ef, 0x7f5, 0x7f6, 0xffc, 0xfff, 0x19d, + 0x1c2, 0x0b5, 0x0a1, 0x096, 0x097, 0x095, 0x099, 0x0a0, + 0x0a2, 0x0ac, 0x0a9, 0x0b1, 0x0b3, 0x0bb, 0x0c0, 0x18f, + 0x004, +}; + +static const uint8_t bits11[289] = { + 4, 5, 6, 7, 8, 8, 9, 10, 10, 10, 11, 11, 12, 11, 12, 12, + 10, 5, 4, 5, 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, + 11, 8, 6, 5, 5, 6, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, + 10, 10, 8, 7, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 10, + 10, 10, 10, 8, 8, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, + 10, 10, 10, 10, 8, 8, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, + 9, 10, 10, 10, 10, 8, 9, 8, 8, 8, 8, 8, 8, 8, 9, 9, + 9, 10, 10, 10, 10, 10, 8, 9, 8, 8, 8, 8, 8, 8, 9, 9, + 9, 10, 10, 10, 10, 10, 10, 8, 10, 9, 8, 8, 9, 9, 9, 9, + 9, 10, 10, 10, 10, 10, 10, 11, 8, 10, 9, 9, 9, 9, 9, 9, + 9, 10, 10, 10, 10, 10, 10, 11, 11, 8, 11, 9, 9, 9, 9, 9, + 9, 10, 10, 10, 10, 10, 11, 10, 11, 11, 8, 11, 10, 9, 9, 10, + 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 8, 11, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 9, 11, 10, 9, + 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 11, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 12, + 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12, 9, + 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, + 5, +}; + +const uint16_t *ff_aac_spectral_codes[11] = { + codes1, codes2, codes3, codes4, codes5, codes6, codes7, codes8, + codes9, codes10, codes11, +}; + +const uint8_t *ff_aac_spectral_bits[11] = { + bits1, bits2, bits3, bits4, bits5, bits6, bits7, bits8, + bits9, bits10, bits11, +}; + +const uint16_t ff_aac_spectral_sizes[11] = { + 81, 81, 81, 81, 81, 81, 64, 64, 169, 169, 289, +}; + +/* NOTE: + * 64.0f is a special value indicating the existence of an escape code in the + * bitstream. + */ +static const float codebook_vector0[324] = { + -1.0000000, -1.0000000, -1.0000000, -1.0000000, + -1.0000000, -1.0000000, -1.0000000, 0.0000000, + -1.0000000, -1.0000000, -1.0000000, 1.0000000, + -1.0000000, -1.0000000, 0.0000000, -1.0000000, + -1.0000000, -1.0000000, 0.0000000, 0.0000000, + -1.0000000, -1.0000000, 0.0000000, 1.0000000, + -1.0000000, -1.0000000, 1.0000000, -1.0000000, + -1.0000000, -1.0000000, 1.0000000, 0.0000000, + -1.0000000, -1.0000000, 1.0000000, 1.0000000, + -1.0000000, 0.0000000, -1.0000000, -1.0000000, + -1.0000000, 0.0000000, -1.0000000, 0.0000000, + -1.0000000, 0.0000000, -1.0000000, 1.0000000, + -1.0000000, 0.0000000, 0.0000000, -1.0000000, + -1.0000000, 0.0000000, 0.0000000, 0.0000000, + -1.0000000, 0.0000000, 0.0000000, 1.0000000, + -1.0000000, 0.0000000, 1.0000000, -1.0000000, + -1.0000000, 0.0000000, 1.0000000, 0.0000000, + -1.0000000, 0.0000000, 1.0000000, 1.0000000, + -1.0000000, 1.0000000, -1.0000000, -1.0000000, + -1.0000000, 1.0000000, -1.0000000, 0.0000000, + -1.0000000, 1.0000000, -1.0000000, 1.0000000, + -1.0000000, 1.0000000, 0.0000000, -1.0000000, + -1.0000000, 1.0000000, 0.0000000, 0.0000000, + -1.0000000, 1.0000000, 0.0000000, 1.0000000, + -1.0000000, 1.0000000, 1.0000000, -1.0000000, + -1.0000000, 1.0000000, 1.0000000, 0.0000000, + -1.0000000, 1.0000000, 1.0000000, 1.0000000, + 0.0000000, -1.0000000, -1.0000000, -1.0000000, + 0.0000000, -1.0000000, -1.0000000, 0.0000000, + 0.0000000, -1.0000000, -1.0000000, 1.0000000, + 0.0000000, -1.0000000, 0.0000000, -1.0000000, + 0.0000000, -1.0000000, 0.0000000, 0.0000000, + 0.0000000, -1.0000000, 0.0000000, 1.0000000, + 0.0000000, -1.0000000, 1.0000000, -1.0000000, + 0.0000000, -1.0000000, 1.0000000, 0.0000000, + 0.0000000, -1.0000000, 1.0000000, 1.0000000, + 0.0000000, 0.0000000, -1.0000000, -1.0000000, + 0.0000000, 0.0000000, -1.0000000, 0.0000000, + 0.0000000, 0.0000000, -1.0000000, 1.0000000, + 0.0000000, 0.0000000, 0.0000000, -1.0000000, + 0.0000000, 0.0000000, 0.0000000, 0.0000000, + 0.0000000, 0.0000000, 0.0000000, 1.0000000, + 0.0000000, 0.0000000, 1.0000000, -1.0000000, + 0.0000000, 0.0000000, 1.0000000, 0.0000000, + 0.0000000, 0.0000000, 1.0000000, 1.0000000, + 0.0000000, 1.0000000, -1.0000000, -1.0000000, + 0.0000000, 1.0000000, -1.0000000, 0.0000000, + 0.0000000, 1.0000000, -1.0000000, 1.0000000, + 0.0000000, 1.0000000, 0.0000000, -1.0000000, + 0.0000000, 1.0000000, 0.0000000, 0.0000000, + 0.0000000, 1.0000000, 0.0000000, 1.0000000, + 0.0000000, 1.0000000, 1.0000000, -1.0000000, + 0.0000000, 1.0000000, 1.0000000, 0.0000000, + 0.0000000, 1.0000000, 1.0000000, 1.0000000, + 1.0000000, -1.0000000, -1.0000000, -1.0000000, + 1.0000000, -1.0000000, -1.0000000, 0.0000000, + 1.0000000, -1.0000000, -1.0000000, 1.0000000, + 1.0000000, -1.0000000, 0.0000000, -1.0000000, + 1.0000000, -1.0000000, 0.0000000, 0.0000000, + 1.0000000, -1.0000000, 0.0000000, 1.0000000, + 1.0000000, -1.0000000, 1.0000000, -1.0000000, + 1.0000000, -1.0000000, 1.0000000, 0.0000000, + 1.0000000, -1.0000000, 1.0000000, 1.0000000, + 1.0000000, 0.0000000, -1.0000000, -1.0000000, + 1.0000000, 0.0000000, -1.0000000, 0.0000000, + 1.0000000, 0.0000000, -1.0000000, 1.0000000, + 1.0000000, 0.0000000, 0.0000000, -1.0000000, + 1.0000000, 0.0000000, 0.0000000, 0.0000000, + 1.0000000, 0.0000000, 0.0000000, 1.0000000, + 1.0000000, 0.0000000, 1.0000000, -1.0000000, + 1.0000000, 0.0000000, 1.0000000, 0.0000000, + 1.0000000, 0.0000000, 1.0000000, 1.0000000, + 1.0000000, 1.0000000, -1.0000000, -1.0000000, + 1.0000000, 1.0000000, -1.0000000, 0.0000000, + 1.0000000, 1.0000000, -1.0000000, 1.0000000, + 1.0000000, 1.0000000, 0.0000000, -1.0000000, + 1.0000000, 1.0000000, 0.0000000, 0.0000000, + 1.0000000, 1.0000000, 0.0000000, 1.0000000, + 1.0000000, 1.0000000, 1.0000000, -1.0000000, + 1.0000000, 1.0000000, 1.0000000, 0.0000000, + 1.0000000, 1.0000000, 1.0000000, 1.0000000, +}; + +static const float codebook_vector2[324] = { + 0.0000000, 0.0000000, 0.0000000, 0.0000000, + 0.0000000, 0.0000000, 0.0000000, 1.0000000, + 0.0000000, 0.0000000, 0.0000000, 2.5198421, + 0.0000000, 0.0000000, 1.0000000, 0.0000000, + 0.0000000, 0.0000000, 1.0000000, 1.0000000, + 0.0000000, 0.0000000, 1.0000000, 2.5198421, + 0.0000000, 0.0000000, 2.5198421, 0.0000000, + 0.0000000, 0.0000000, 2.5198421, 1.0000000, + 0.0000000, 0.0000000, 2.5198421, 2.5198421, + 0.0000000, 1.0000000, 0.0000000, 0.0000000, + 0.0000000, 1.0000000, 0.0000000, 1.0000000, + 0.0000000, 1.0000000, 0.0000000, 2.5198421, + 0.0000000, 1.0000000, 1.0000000, 0.0000000, + 0.0000000, 1.0000000, 1.0000000, 1.0000000, + 0.0000000, 1.0000000, 1.0000000, 2.5198421, + 0.0000000, 1.0000000, 2.5198421, 0.0000000, + 0.0000000, 1.0000000, 2.5198421, 1.0000000, + 0.0000000, 1.0000000, 2.5198421, 2.5198421, + 0.0000000, 2.5198421, 0.0000000, 0.0000000, + 0.0000000, 2.5198421, 0.0000000, 1.0000000, + 0.0000000, 2.5198421, 0.0000000, 2.5198421, + 0.0000000, 2.5198421, 1.0000000, 0.0000000, + 0.0000000, 2.5198421, 1.0000000, 1.0000000, + 0.0000000, 2.5198421, 1.0000000, 2.5198421, + 0.0000000, 2.5198421, 2.5198421, 0.0000000, + 0.0000000, 2.5198421, 2.5198421, 1.0000000, + 0.0000000, 2.5198421, 2.5198421, 2.5198421, + 1.0000000, 0.0000000, 0.0000000, 0.0000000, + 1.0000000, 0.0000000, 0.0000000, 1.0000000, + 1.0000000, 0.0000000, 0.0000000, 2.5198421, + 1.0000000, 0.0000000, 1.0000000, 0.0000000, + 1.0000000, 0.0000000, 1.0000000, 1.0000000, + 1.0000000, 0.0000000, 1.0000000, 2.5198421, + 1.0000000, 0.0000000, 2.5198421, 0.0000000, + 1.0000000, 0.0000000, 2.5198421, 1.0000000, + 1.0000000, 0.0000000, 2.5198421, 2.5198421, + 1.0000000, 1.0000000, 0.0000000, 0.0000000, + 1.0000000, 1.0000000, 0.0000000, 1.0000000, + 1.0000000, 1.0000000, 0.0000000, 2.5198421, + 1.0000000, 1.0000000, 1.0000000, 0.0000000, + 1.0000000, 1.0000000, 1.0000000, 1.0000000, + 1.0000000, 1.0000000, 1.0000000, 2.5198421, + 1.0000000, 1.0000000, 2.5198421, 0.0000000, + 1.0000000, 1.0000000, 2.5198421, 1.0000000, + 1.0000000, 1.0000000, 2.5198421, 2.5198421, + 1.0000000, 2.5198421, 0.0000000, 0.0000000, + 1.0000000, 2.5198421, 0.0000000, 1.0000000, + 1.0000000, 2.5198421, 0.0000000, 2.5198421, + 1.0000000, 2.5198421, 1.0000000, 0.0000000, + 1.0000000, 2.5198421, 1.0000000, 1.0000000, + 1.0000000, 2.5198421, 1.0000000, 2.5198421, + 1.0000000, 2.5198421, 2.5198421, 0.0000000, + 1.0000000, 2.5198421, 2.5198421, 1.0000000, + 1.0000000, 2.5198421, 2.5198421, 2.5198421, + 2.5198421, 0.0000000, 0.0000000, 0.0000000, + 2.5198421, 0.0000000, 0.0000000, 1.0000000, + 2.5198421, 0.0000000, 0.0000000, 2.5198421, + 2.5198421, 0.0000000, 1.0000000, 0.0000000, + 2.5198421, 0.0000000, 1.0000000, 1.0000000, + 2.5198421, 0.0000000, 1.0000000, 2.5198421, + 2.5198421, 0.0000000, 2.5198421, 0.0000000, + 2.5198421, 0.0000000, 2.5198421, 1.0000000, + 2.5198421, 0.0000000, 2.5198421, 2.5198421, + 2.5198421, 1.0000000, 0.0000000, 0.0000000, + 2.5198421, 1.0000000, 0.0000000, 1.0000000, + 2.5198421, 1.0000000, 0.0000000, 2.5198421, + 2.5198421, 1.0000000, 1.0000000, 0.0000000, + 2.5198421, 1.0000000, 1.0000000, 1.0000000, + 2.5198421, 1.0000000, 1.0000000, 2.5198421, + 2.5198421, 1.0000000, 2.5198421, 0.0000000, + 2.5198421, 1.0000000, 2.5198421, 1.0000000, + 2.5198421, 1.0000000, 2.5198421, 2.5198421, + 2.5198421, 2.5198421, 0.0000000, 0.0000000, + 2.5198421, 2.5198421, 0.0000000, 1.0000000, + 2.5198421, 2.5198421, 0.0000000, 2.5198421, + 2.5198421, 2.5198421, 1.0000000, 0.0000000, + 2.5198421, 2.5198421, 1.0000000, 1.0000000, + 2.5198421, 2.5198421, 1.0000000, 2.5198421, + 2.5198421, 2.5198421, 2.5198421, 0.0000000, + 2.5198421, 2.5198421, 2.5198421, 1.0000000, + 2.5198421, 2.5198421, 2.5198421, 2.5198421, +}; + +static const float codebook_vector4[162] = { + -6.3496042, -6.3496042, -6.3496042, -4.3267487, + -6.3496042, -2.5198421, -6.3496042, -1.0000000, + -6.3496042, 0.0000000, -6.3496042, 1.0000000, + -6.3496042, 2.5198421, -6.3496042, 4.3267487, + -6.3496042, 6.3496042, -4.3267487, -6.3496042, + -4.3267487, -4.3267487, -4.3267487, -2.5198421, + -4.3267487, -1.0000000, -4.3267487, 0.0000000, + -4.3267487, 1.0000000, -4.3267487, 2.5198421, + -4.3267487, 4.3267487, -4.3267487, 6.3496042, + -2.5198421, -6.3496042, -2.5198421, -4.3267487, + -2.5198421, -2.5198421, -2.5198421, -1.0000000, + -2.5198421, 0.0000000, -2.5198421, 1.0000000, + -2.5198421, 2.5198421, -2.5198421, 4.3267487, + -2.5198421, 6.3496042, -1.0000000, -6.3496042, + -1.0000000, -4.3267487, -1.0000000, -2.5198421, + -1.0000000, -1.0000000, -1.0000000, 0.0000000, + -1.0000000, 1.0000000, -1.0000000, 2.5198421, + -1.0000000, 4.3267487, -1.0000000, 6.3496042, + 0.0000000, -6.3496042, 0.0000000, -4.3267487, + 0.0000000, -2.5198421, 0.0000000, -1.0000000, + 0.0000000, 0.0000000, 0.0000000, 1.0000000, + 0.0000000, 2.5198421, 0.0000000, 4.3267487, + 0.0000000, 6.3496042, 1.0000000, -6.3496042, + 1.0000000, -4.3267487, 1.0000000, -2.5198421, + 1.0000000, -1.0000000, 1.0000000, 0.0000000, + 1.0000000, 1.0000000, 1.0000000, 2.5198421, + 1.0000000, 4.3267487, 1.0000000, 6.3496042, + 2.5198421, -6.3496042, 2.5198421, -4.3267487, + 2.5198421, -2.5198421, 2.5198421, -1.0000000, + 2.5198421, 0.0000000, 2.5198421, 1.0000000, + 2.5198421, 2.5198421, 2.5198421, 4.3267487, + 2.5198421, 6.3496042, 4.3267487, -6.3496042, + 4.3267487, -4.3267487, 4.3267487, -2.5198421, + 4.3267487, -1.0000000, 4.3267487, 0.0000000, + 4.3267487, 1.0000000, 4.3267487, 2.5198421, + 4.3267487, 4.3267487, 4.3267487, 6.3496042, + 6.3496042, -6.3496042, 6.3496042, -4.3267487, + 6.3496042, -2.5198421, 6.3496042, -1.0000000, + 6.3496042, 0.0000000, 6.3496042, 1.0000000, + 6.3496042, 2.5198421, 6.3496042, 4.3267487, + 6.3496042, 6.3496042, +}; + +static const float codebook_vector6[128] = { + 0.0000000, 0.0000000, 0.0000000, 1.0000000, + 0.0000000, 2.5198421, 0.0000000, 4.3267487, + 0.0000000, 6.3496042, 0.0000000, 8.5498797, + 0.0000000, 10.9027236, 0.0000000, 13.3905183, + 1.0000000, 0.0000000, 1.0000000, 1.0000000, + 1.0000000, 2.5198421, 1.0000000, 4.3267487, + 1.0000000, 6.3496042, 1.0000000, 8.5498797, + 1.0000000, 10.9027236, 1.0000000, 13.3905183, + 2.5198421, 0.0000000, 2.5198421, 1.0000000, + 2.5198421, 2.5198421, 2.5198421, 4.3267487, + 2.5198421, 6.3496042, 2.5198421, 8.5498797, + 2.5198421, 10.9027236, 2.5198421, 13.3905183, + 4.3267487, 0.0000000, 4.3267487, 1.0000000, + 4.3267487, 2.5198421, 4.3267487, 4.3267487, + 4.3267487, 6.3496042, 4.3267487, 8.5498797, + 4.3267487, 10.9027236, 4.3267487, 13.3905183, + 6.3496042, 0.0000000, 6.3496042, 1.0000000, + 6.3496042, 2.5198421, 6.3496042, 4.3267487, + 6.3496042, 6.3496042, 6.3496042, 8.5498797, + 6.3496042, 10.9027236, 6.3496042, 13.3905183, + 8.5498797, 0.0000000, 8.5498797, 1.0000000, + 8.5498797, 2.5198421, 8.5498797, 4.3267487, + 8.5498797, 6.3496042, 8.5498797, 8.5498797, + 8.5498797, 10.9027236, 8.5498797, 13.3905183, + 10.9027236, 0.0000000, 10.9027236, 1.0000000, + 10.9027236, 2.5198421, 10.9027236, 4.3267487, + 10.9027236, 6.3496042, 10.9027236, 8.5498797, + 10.9027236, 10.9027236, 10.9027236, 13.3905183, + 13.3905183, 0.0000000, 13.3905183, 1.0000000, + 13.3905183, 2.5198421, 13.3905183, 4.3267487, + 13.3905183, 6.3496042, 13.3905183, 8.5498797, + 13.3905183, 10.9027236, 13.3905183, 13.3905183, +}; + +static const float codebook_vector8[338] = { + 0.0000000, 0.0000000, 0.0000000, 1.0000000, + 0.0000000, 2.5198421, 0.0000000, 4.3267487, + 0.0000000, 6.3496042, 0.0000000, 8.5498797, + 0.0000000, 10.9027236, 0.0000000, 13.3905183, + 0.0000000, 16.0000000, 0.0000000, 18.7207544, + 0.0000000, 21.5443469, 0.0000000, 24.4637810, + 0.0000000, 27.4731418, 1.0000000, 0.0000000, + 1.0000000, 1.0000000, 1.0000000, 2.5198421, + 1.0000000, 4.3267487, 1.0000000, 6.3496042, + 1.0000000, 8.5498797, 1.0000000, 10.9027236, + 1.0000000, 13.3905183, 1.0000000, 16.0000000, + 1.0000000, 18.7207544, 1.0000000, 21.5443469, + 1.0000000, 24.4637810, 1.0000000, 27.4731418, + 2.5198421, 0.0000000, 2.5198421, 1.0000000, + 2.5198421, 2.5198421, 2.5198421, 4.3267487, + 2.5198421, 6.3496042, 2.5198421, 8.5498797, + 2.5198421, 10.9027236, 2.5198421, 13.3905183, + 2.5198421, 16.0000000, 2.5198421, 18.7207544, + 2.5198421, 21.5443469, 2.5198421, 24.4637810, + 2.5198421, 27.4731418, 4.3267487, 0.0000000, + 4.3267487, 1.0000000, 4.3267487, 2.5198421, + 4.3267487, 4.3267487, 4.3267487, 6.3496042, + 4.3267487, 8.5498797, 4.3267487, 10.9027236, + 4.3267487, 13.3905183, 4.3267487, 16.0000000, + 4.3267487, 18.7207544, 4.3267487, 21.5443469, + 4.3267487, 24.4637810, 4.3267487, 27.4731418, + 6.3496042, 0.0000000, 6.3496042, 1.0000000, + 6.3496042, 2.5198421, 6.3496042, 4.3267487, + 6.3496042, 6.3496042, 6.3496042, 8.5498797, + 6.3496042, 10.9027236, 6.3496042, 13.3905183, + 6.3496042, 16.0000000, 6.3496042, 18.7207544, + 6.3496042, 21.5443469, 6.3496042, 24.4637810, + 6.3496042, 27.4731418, 8.5498797, 0.0000000, + 8.5498797, 1.0000000, 8.5498797, 2.5198421, + 8.5498797, 4.3267487, 8.5498797, 6.3496042, + 8.5498797, 8.5498797, 8.5498797, 10.9027236, + 8.5498797, 13.3905183, 8.5498797, 16.0000000, + 8.5498797, 18.7207544, 8.5498797, 21.5443469, + 8.5498797, 24.4637810, 8.5498797, 27.4731418, + 10.9027236, 0.0000000, 10.9027236, 1.0000000, + 10.9027236, 2.5198421, 10.9027236, 4.3267487, + 10.9027236, 6.3496042, 10.9027236, 8.5498797, + 10.9027236, 10.9027236, 10.9027236, 13.3905183, + 10.9027236, 16.0000000, 10.9027236, 18.7207544, + 10.9027236, 21.5443469, 10.9027236, 24.4637810, + 10.9027236, 27.4731418, 13.3905183, 0.0000000, + 13.3905183, 1.0000000, 13.3905183, 2.5198421, + 13.3905183, 4.3267487, 13.3905183, 6.3496042, + 13.3905183, 8.5498797, 13.3905183, 10.9027236, + 13.3905183, 13.3905183, 13.3905183, 16.0000000, + 13.3905183, 18.7207544, 13.3905183, 21.5443469, + 13.3905183, 24.4637810, 13.3905183, 27.4731418, + 16.0000000, 0.0000000, 16.0000000, 1.0000000, + 16.0000000, 2.5198421, 16.0000000, 4.3267487, + 16.0000000, 6.3496042, 16.0000000, 8.5498797, + 16.0000000, 10.9027236, 16.0000000, 13.3905183, + 16.0000000, 16.0000000, 16.0000000, 18.7207544, + 16.0000000, 21.5443469, 16.0000000, 24.4637810, + 16.0000000, 27.4731418, 18.7207544, 0.0000000, + 18.7207544, 1.0000000, 18.7207544, 2.5198421, + 18.7207544, 4.3267487, 18.7207544, 6.3496042, + 18.7207544, 8.5498797, 18.7207544, 10.9027236, + 18.7207544, 13.3905183, 18.7207544, 16.0000000, + 18.7207544, 18.7207544, 18.7207544, 21.5443469, + 18.7207544, 24.4637810, 18.7207544, 27.4731418, + 21.5443469, 0.0000000, 21.5443469, 1.0000000, + 21.5443469, 2.5198421, 21.5443469, 4.3267487, + 21.5443469, 6.3496042, 21.5443469, 8.5498797, + 21.5443469, 10.9027236, 21.5443469, 13.3905183, + 21.5443469, 16.0000000, 21.5443469, 18.7207544, + 21.5443469, 21.5443469, 21.5443469, 24.4637810, + 21.5443469, 27.4731418, 24.4637810, 0.0000000, + 24.4637810, 1.0000000, 24.4637810, 2.5198421, + 24.4637810, 4.3267487, 24.4637810, 6.3496042, + 24.4637810, 8.5498797, 24.4637810, 10.9027236, + 24.4637810, 13.3905183, 24.4637810, 16.0000000, + 24.4637810, 18.7207544, 24.4637810, 21.5443469, + 24.4637810, 24.4637810, 24.4637810, 27.4731418, + 27.4731418, 0.0000000, 27.4731418, 1.0000000, + 27.4731418, 2.5198421, 27.4731418, 4.3267487, + 27.4731418, 6.3496042, 27.4731418, 8.5498797, + 27.4731418, 10.9027236, 27.4731418, 13.3905183, + 27.4731418, 16.0000000, 27.4731418, 18.7207544, + 27.4731418, 21.5443469, 27.4731418, 24.4637810, + 27.4731418, 27.4731418, +}; + +static const float codebook_vector10[578] = { + 0.0000000, 0.0000000, 0.0000000, 1.0000000, + 0.0000000, 2.5198421, 0.0000000, 4.3267487, + 0.0000000, 6.3496042, 0.0000000, 8.5498797, + 0.0000000, 10.9027236, 0.0000000, 13.3905183, + 0.0000000, 16.0000000, 0.0000000, 18.7207544, + 0.0000000, 21.5443469, 0.0000000, 24.4637810, + 0.0000000, 27.4731418, 0.0000000, 30.5673509, + 0.0000000, 33.7419917, 0.0000000, 36.9931811, + 0.0000000, 64.0f, 1.0000000, 0.0000000, + 1.0000000, 1.0000000, 1.0000000, 2.5198421, + 1.0000000, 4.3267487, 1.0000000, 6.3496042, + 1.0000000, 8.5498797, 1.0000000, 10.9027236, + 1.0000000, 13.3905183, 1.0000000, 16.0000000, + 1.0000000, 18.7207544, 1.0000000, 21.5443469, + 1.0000000, 24.4637810, 1.0000000, 27.4731418, + 1.0000000, 30.5673509, 1.0000000, 33.7419917, + 1.0000000, 36.9931811, 1.0000000, 64.0f, + 2.5198421, 0.0000000, 2.5198421, 1.0000000, + 2.5198421, 2.5198421, 2.5198421, 4.3267487, + 2.5198421, 6.3496042, 2.5198421, 8.5498797, + 2.5198421, 10.9027236, 2.5198421, 13.3905183, + 2.5198421, 16.0000000, 2.5198421, 18.7207544, + 2.5198421, 21.5443469, 2.5198421, 24.4637810, + 2.5198421, 27.4731418, 2.5198421, 30.5673509, + 2.5198421, 33.7419917, 2.5198421, 36.9931811, + 2.5198421, 64.0f, 4.3267487, 0.0000000, + 4.3267487, 1.0000000, 4.3267487, 2.5198421, + 4.3267487, 4.3267487, 4.3267487, 6.3496042, + 4.3267487, 8.5498797, 4.3267487, 10.9027236, + 4.3267487, 13.3905183, 4.3267487, 16.0000000, + 4.3267487, 18.7207544, 4.3267487, 21.5443469, + 4.3267487, 24.4637810, 4.3267487, 27.4731418, + 4.3267487, 30.5673509, 4.3267487, 33.7419917, + 4.3267487, 36.9931811, 4.3267487, 64.0f, + 6.3496042, 0.0000000, 6.3496042, 1.0000000, + 6.3496042, 2.5198421, 6.3496042, 4.3267487, + 6.3496042, 6.3496042, 6.3496042, 8.5498797, + 6.3496042, 10.9027236, 6.3496042, 13.3905183, + 6.3496042, 16.0000000, 6.3496042, 18.7207544, + 6.3496042, 21.5443469, 6.3496042, 24.4637810, + 6.3496042, 27.4731418, 6.3496042, 30.5673509, + 6.3496042, 33.7419917, 6.3496042, 36.9931811, + 6.3496042, 64.0f, 8.5498797, 0.0000000, + 8.5498797, 1.0000000, 8.5498797, 2.5198421, + 8.5498797, 4.3267487, 8.5498797, 6.3496042, + 8.5498797, 8.5498797, 8.5498797, 10.9027236, + 8.5498797, 13.3905183, 8.5498797, 16.0000000, + 8.5498797, 18.7207544, 8.5498797, 21.5443469, + 8.5498797, 24.4637810, 8.5498797, 27.4731418, + 8.5498797, 30.5673509, 8.5498797, 33.7419917, + 8.5498797, 36.9931811, 8.5498797, 64.0f, + 10.9027236, 0.0000000, 10.9027236, 1.0000000, + 10.9027236, 2.5198421, 10.9027236, 4.3267487, + 10.9027236, 6.3496042, 10.9027236, 8.5498797, + 10.9027236, 10.9027236, 10.9027236, 13.3905183, + 10.9027236, 16.0000000, 10.9027236, 18.7207544, + 10.9027236, 21.5443469, 10.9027236, 24.4637810, + 10.9027236, 27.4731418, 10.9027236, 30.5673509, + 10.9027236, 33.7419917, 10.9027236, 36.9931811, + 10.9027236, 64.0f, 13.3905183, 0.0000000, + 13.3905183, 1.0000000, 13.3905183, 2.5198421, + 13.3905183, 4.3267487, 13.3905183, 6.3496042, + 13.3905183, 8.5498797, 13.3905183, 10.9027236, + 13.3905183, 13.3905183, 13.3905183, 16.0000000, + 13.3905183, 18.7207544, 13.3905183, 21.5443469, + 13.3905183, 24.4637810, 13.3905183, 27.4731418, + 13.3905183, 30.5673509, 13.3905183, 33.7419917, + 13.3905183, 36.9931811, 13.3905183, 64.0f, + 16.0000000, 0.0000000, 16.0000000, 1.0000000, + 16.0000000, 2.5198421, 16.0000000, 4.3267487, + 16.0000000, 6.3496042, 16.0000000, 8.5498797, + 16.0000000, 10.9027236, 16.0000000, 13.3905183, + 16.0000000, 16.0000000, 16.0000000, 18.7207544, + 16.0000000, 21.5443469, 16.0000000, 24.4637810, + 16.0000000, 27.4731418, 16.0000000, 30.5673509, + 16.0000000, 33.7419917, 16.0000000, 36.9931811, + 16.0000000, 64.0f, 18.7207544, 0.0000000, + 18.7207544, 1.0000000, 18.7207544, 2.5198421, + 18.7207544, 4.3267487, 18.7207544, 6.3496042, + 18.7207544, 8.5498797, 18.7207544, 10.9027236, + 18.7207544, 13.3905183, 18.7207544, 16.0000000, + 18.7207544, 18.7207544, 18.7207544, 21.5443469, + 18.7207544, 24.4637810, 18.7207544, 27.4731418, + 18.7207544, 30.5673509, 18.7207544, 33.7419917, + 18.7207544, 36.9931811, 18.7207544, 64.0f, + 21.5443469, 0.0000000, 21.5443469, 1.0000000, + 21.5443469, 2.5198421, 21.5443469, 4.3267487, + 21.5443469, 6.3496042, 21.5443469, 8.5498797, + 21.5443469, 10.9027236, 21.5443469, 13.3905183, + 21.5443469, 16.0000000, 21.5443469, 18.7207544, + 21.5443469, 21.5443469, 21.5443469, 24.4637810, + 21.5443469, 27.4731418, 21.5443469, 30.5673509, + 21.5443469, 33.7419917, 21.5443469, 36.9931811, + 21.5443469, 64.0f, 24.4637810, 0.0000000, + 24.4637810, 1.0000000, 24.4637810, 2.5198421, + 24.4637810, 4.3267487, 24.4637810, 6.3496042, + 24.4637810, 8.5498797, 24.4637810, 10.9027236, + 24.4637810, 13.3905183, 24.4637810, 16.0000000, + 24.4637810, 18.7207544, 24.4637810, 21.5443469, + 24.4637810, 24.4637810, 24.4637810, 27.4731418, + 24.4637810, 30.5673509, 24.4637810, 33.7419917, + 24.4637810, 36.9931811, 24.4637810, 64.0f, + 27.4731418, 0.0000000, 27.4731418, 1.0000000, + 27.4731418, 2.5198421, 27.4731418, 4.3267487, + 27.4731418, 6.3496042, 27.4731418, 8.5498797, + 27.4731418, 10.9027236, 27.4731418, 13.3905183, + 27.4731418, 16.0000000, 27.4731418, 18.7207544, + 27.4731418, 21.5443469, 27.4731418, 24.4637810, + 27.4731418, 27.4731418, 27.4731418, 30.5673509, + 27.4731418, 33.7419917, 27.4731418, 36.9931811, + 27.4731418, 64.0f, 30.5673509, 0.0000000, + 30.5673509, 1.0000000, 30.5673509, 2.5198421, + 30.5673509, 4.3267487, 30.5673509, 6.3496042, + 30.5673509, 8.5498797, 30.5673509, 10.9027236, + 30.5673509, 13.3905183, 30.5673509, 16.0000000, + 30.5673509, 18.7207544, 30.5673509, 21.5443469, + 30.5673509, 24.4637810, 30.5673509, 27.4731418, + 30.5673509, 30.5673509, 30.5673509, 33.7419917, + 30.5673509, 36.9931811, 30.5673509, 64.0f, + 33.7419917, 0.0000000, 33.7419917, 1.0000000, + 33.7419917, 2.5198421, 33.7419917, 4.3267487, + 33.7419917, 6.3496042, 33.7419917, 8.5498797, + 33.7419917, 10.9027236, 33.7419917, 13.3905183, + 33.7419917, 16.0000000, 33.7419917, 18.7207544, + 33.7419917, 21.5443469, 33.7419917, 24.4637810, + 33.7419917, 27.4731418, 33.7419917, 30.5673509, + 33.7419917, 33.7419917, 33.7419917, 36.9931811, + 33.7419917, 64.0f, 36.9931811, 0.0000000, + 36.9931811, 1.0000000, 36.9931811, 2.5198421, + 36.9931811, 4.3267487, 36.9931811, 6.3496042, + 36.9931811, 8.5498797, 36.9931811, 10.9027236, + 36.9931811, 13.3905183, 36.9931811, 16.0000000, + 36.9931811, 18.7207544, 36.9931811, 21.5443469, + 36.9931811, 24.4637810, 36.9931811, 27.4731418, + 36.9931811, 30.5673509, 36.9931811, 33.7419917, + 36.9931811, 36.9931811, 36.9931811, 64.0f, + 64.0f, 0.0000000, 64.0f, 1.0000000, + 64.0f, 2.5198421, 64.0f, 4.3267487, + 64.0f, 6.3496042, 64.0f, 8.5498797, + 64.0f, 10.9027236, 64.0f, 13.3905183, + 64.0f, 16.0000000, 64.0f, 18.7207544, + 64.0f, 21.5443469, 64.0f, 24.4637810, + 64.0f, 27.4731418, 64.0f, 30.5673509, + 64.0f, 33.7419917, 64.0f, 36.9931811, + 64.0f, 64.0f, +}; + +const float *ff_aac_codebook_vectors[] = { + codebook_vector0, codebook_vector0, codebook_vector2, + codebook_vector2, codebook_vector4, codebook_vector4, + codebook_vector6, codebook_vector6, codebook_vector8, + codebook_vector8, codebook_vector10, +}; + +#ifdef CONFIG_HARDCODED_TABLES + +/** + * Table of pow(2, (i - 200)/4.) used for different purposes depending on the + * range of indices to the table: + * [ 0, 255] scale factor decoding when using C dsp.float_to_int16 + * [60, 315] scale factor decoding when using SIMD dsp.float_to_int16 + * [45, 300] intensity stereo position decoding mapped in reverse order i.e. 0->300, 1->299, ..., 254->46, 255->45 + */ +const float ff_aac_pow2sf_tab[316] = { + 8.88178420e-16, 1.05622810e-15, 1.25607397e-15, 1.49373210e-15, + 1.77635684e-15, 2.11245619e-15, 2.51214793e-15, 2.98746420e-15, + 3.55271368e-15, 4.22491238e-15, 5.02429587e-15, 5.97492839e-15, + 7.10542736e-15, 8.44982477e-15, 1.00485917e-14, 1.19498568e-14, + 1.42108547e-14, 1.68996495e-14, 2.00971835e-14, 2.38997136e-14, + 2.84217094e-14, 3.37992991e-14, 4.01943669e-14, 4.77994272e-14, + 5.68434189e-14, 6.75985982e-14, 8.03887339e-14, 9.55988543e-14, + 1.13686838e-13, 1.35197196e-13, 1.60777468e-13, 1.91197709e-13, + 2.27373675e-13, 2.70394393e-13, 3.21554936e-13, 3.82395417e-13, + 4.54747351e-13, 5.40788785e-13, 6.43109871e-13, 7.64790834e-13, + 9.09494702e-13, 1.08157757e-12, 1.28621974e-12, 1.52958167e-12, + 1.81898940e-12, 2.16315514e-12, 2.57243948e-12, 3.05916334e-12, + 3.63797881e-12, 4.32631028e-12, 5.14487897e-12, 6.11832668e-12, + 7.27595761e-12, 8.65262056e-12, 1.02897579e-11, 1.22366534e-11, + 1.45519152e-11, 1.73052411e-11, 2.05795159e-11, 2.44733067e-11, + 2.91038305e-11, 3.46104823e-11, 4.11590317e-11, 4.89466134e-11, + 5.82076609e-11, 6.92209645e-11, 8.23180635e-11, 9.78932268e-11, + 1.16415322e-10, 1.38441929e-10, 1.64636127e-10, 1.95786454e-10, + 2.32830644e-10, 2.76883858e-10, 3.29272254e-10, 3.91572907e-10, + 4.65661287e-10, 5.53767716e-10, 6.58544508e-10, 7.83145814e-10, + 9.31322575e-10, 1.10753543e-09, 1.31708902e-09, 1.56629163e-09, + 1.86264515e-09, 2.21507086e-09, 2.63417803e-09, 3.13258326e-09, + 3.72529030e-09, 4.43014173e-09, 5.26835606e-09, 6.26516652e-09, + 7.45058060e-09, 8.86028346e-09, 1.05367121e-08, 1.25303330e-08, + 1.49011612e-08, 1.77205669e-08, 2.10734243e-08, 2.50606661e-08, + 2.98023224e-08, 3.54411338e-08, 4.21468485e-08, 5.01213321e-08, + 5.96046448e-08, 7.08822677e-08, 8.42936970e-08, 1.00242664e-07, + 1.19209290e-07, 1.41764535e-07, 1.68587394e-07, 2.00485328e-07, + 2.38418579e-07, 2.83529071e-07, 3.37174788e-07, 4.00970657e-07, + 4.76837158e-07, 5.67058141e-07, 6.74349576e-07, 8.01941314e-07, + 9.53674316e-07, 1.13411628e-06, 1.34869915e-06, 1.60388263e-06, + 1.90734863e-06, 2.26823256e-06, 2.69739830e-06, 3.20776526e-06, + 3.81469727e-06, 4.53646513e-06, 5.39479661e-06, 6.41553051e-06, + 7.62939453e-06, 9.07293026e-06, 1.07895932e-05, 1.28310610e-05, + 1.52587891e-05, 1.81458605e-05, 2.15791864e-05, 2.56621220e-05, + 3.05175781e-05, 3.62917210e-05, 4.31583729e-05, 5.13242441e-05, + 6.10351562e-05, 7.25834421e-05, 8.63167458e-05, 1.02648488e-04, + 1.22070312e-04, 1.45166884e-04, 1.72633492e-04, 2.05296976e-04, + 2.44140625e-04, 2.90333768e-04, 3.45266983e-04, 4.10593953e-04, + 4.88281250e-04, 5.80667537e-04, 6.90533966e-04, 8.21187906e-04, + 9.76562500e-04, 1.16133507e-03, 1.38106793e-03, 1.64237581e-03, + 1.95312500e-03, 2.32267015e-03, 2.76213586e-03, 3.28475162e-03, + 3.90625000e-03, 4.64534029e-03, 5.52427173e-03, 6.56950324e-03, + 7.81250000e-03, 9.29068059e-03, 1.10485435e-02, 1.31390065e-02, + 1.56250000e-02, 1.85813612e-02, 2.20970869e-02, 2.62780130e-02, + 3.12500000e-02, 3.71627223e-02, 4.41941738e-02, 5.25560260e-02, + 6.25000000e-02, 7.43254447e-02, 8.83883476e-02, 1.05112052e-01, + 1.25000000e-01, 1.48650889e-01, 1.76776695e-01, 2.10224104e-01, + 2.50000000e-01, 2.97301779e-01, 3.53553391e-01, 4.20448208e-01, + 5.00000000e-01, 5.94603558e-01, 7.07106781e-01, 8.40896415e-01, + 1.00000000e+00, 1.18920712e+00, 1.41421356e+00, 1.68179283e+00, + 2.00000000e+00, 2.37841423e+00, 2.82842712e+00, 3.36358566e+00, + 4.00000000e+00, 4.75682846e+00, 5.65685425e+00, 6.72717132e+00, + 8.00000000e+00, 9.51365692e+00, 1.13137085e+01, 1.34543426e+01, + 1.60000000e+01, 1.90273138e+01, 2.26274170e+01, 2.69086853e+01, + 3.20000000e+01, 3.80546277e+01, 4.52548340e+01, 5.38173706e+01, + 6.40000000e+01, 7.61092554e+01, 9.05096680e+01, 1.07634741e+02, + 1.28000000e+02, 1.52218511e+02, 1.81019336e+02, 2.15269482e+02, + 2.56000000e+02, 3.04437021e+02, 3.62038672e+02, 4.30538965e+02, + 5.12000000e+02, 6.08874043e+02, 7.24077344e+02, 8.61077929e+02, + 1.02400000e+03, 1.21774809e+03, 1.44815469e+03, 1.72215586e+03, + 2.04800000e+03, 2.43549617e+03, 2.89630938e+03, 3.44431172e+03, + 4.09600000e+03, 4.87099234e+03, 5.79261875e+03, 6.88862343e+03, + 8.19200000e+03, 9.74198469e+03, 1.15852375e+04, 1.37772469e+04, + 1.63840000e+04, 1.94839694e+04, 2.31704750e+04, 2.75544937e+04, + 3.27680000e+04, 3.89679387e+04, 4.63409500e+04, 5.51089875e+04, + 6.55360000e+04, 7.79358775e+04, 9.26819000e+04, 1.10217975e+05, + 1.31072000e+05, 1.55871755e+05, 1.85363800e+05, 2.20435950e+05, + 2.62144000e+05, 3.11743510e+05, 3.70727600e+05, 4.40871900e+05, + 5.24288000e+05, 6.23487020e+05, 7.41455200e+05, 8.81743800e+05, + 1.04857600e+06, 1.24697404e+06, 1.48291040e+06, 1.76348760e+06, + 2.09715200e+06, 2.49394808e+06, 2.96582080e+06, 3.52697520e+06, + 4.19430400e+06, 4.98789616e+06, 5.93164160e+06, 7.05395040e+06, + 8.38860800e+06, 9.97579232e+06, 1.18632832e+07, 1.41079008e+07, + 1.67772160e+07, 1.99515846e+07, 2.37265664e+07, 2.82158016e+07, + 3.35544320e+07, 3.99031693e+07, 4.74531328e+07, 5.64316032e+07, + 6.71088640e+07, 7.98063385e+07, 9.49062656e+07, 1.12863206e+08, + 1.34217728e+08, 1.59612677e+08, 1.89812531e+08, 2.25726413e+08, + 2.68435456e+08, 3.19225354e+08, 3.79625062e+08, 4.51452825e+08, +}; + +#else + +float ff_aac_pow2sf_tab[316]; + +#endif /* CONFIG_HARDCODED_TABLES */