LCOV - code coverage report
Current view: top level - libavcodec - aacsbr_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 767 920 83.4 %
Date: 2017-12-16 13:57:32 Functions: 40 40 100.0 %

          Line data    Source code
       1             : /*
       2             :  * AAC Spectral Band Replication decoding functions
       3             :  * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
       4             :  * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
       5             :  *
       6             :  * Fixed point code
       7             :  * Copyright (c) 2013
       8             :  *      MIPS Technologies, Inc., California.
       9             :  *
      10             :  * This file is part of FFmpeg.
      11             :  *
      12             :  * FFmpeg is free software; you can redistribute it and/or
      13             :  * modify it under the terms of the GNU Lesser General Public
      14             :  * License as published by the Free Software Foundation; either
      15             :  * version 2.1 of the License, or (at your option) any later version.
      16             :  *
      17             :  * FFmpeg is distributed in the hope that it will be useful,
      18             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      19             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      20             :  * Lesser General Public License for more details.
      21             :  *
      22             :  * You should have received a copy of the GNU Lesser General Public
      23             :  * License along with FFmpeg; if not, write to the Free Software
      24             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      25             :  */
      26             : 
      27             : /**
      28             :  * @file
      29             :  * AAC Spectral Band Replication decoding functions
      30             :  * @author Robert Swain ( rob opendot cl )
      31             :  * @author Stanislav Ocovaj ( stanislav.ocovaj@imgtec.com )
      32             :  * @author Zoran Basaric ( zoran.basaric@imgtec.com )
      33             :  */
      34             : 
      35             : #include "libavutil/qsort.h"
      36             : 
      37         160 : av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
      38             : {
      39             :     static const struct {
      40             :         const void *sbr_codes, *sbr_bits;
      41             :         const unsigned int table_size, elem_size;
      42             :     } sbr_tmp[] = {
      43             :         SBR_VLC_ROW(t_huffman_env_1_5dB),
      44             :         SBR_VLC_ROW(f_huffman_env_1_5dB),
      45             :         SBR_VLC_ROW(t_huffman_env_bal_1_5dB),
      46             :         SBR_VLC_ROW(f_huffman_env_bal_1_5dB),
      47             :         SBR_VLC_ROW(t_huffman_env_3_0dB),
      48             :         SBR_VLC_ROW(f_huffman_env_3_0dB),
      49             :         SBR_VLC_ROW(t_huffman_env_bal_3_0dB),
      50             :         SBR_VLC_ROW(f_huffman_env_bal_3_0dB),
      51             :         SBR_VLC_ROW(t_huffman_noise_3_0dB),
      52             :         SBR_VLC_ROW(t_huffman_noise_bal_3_0dB),
      53             :     };
      54             : 
      55             :     // SBR VLC table initialization
      56         160 :     SBR_INIT_VLC_STATIC(0, 1098);
      57         160 :     SBR_INIT_VLC_STATIC(1, 1092);
      58         160 :     SBR_INIT_VLC_STATIC(2, 768);
      59         160 :     SBR_INIT_VLC_STATIC(3, 1026);
      60         160 :     SBR_INIT_VLC_STATIC(4, 1058);
      61         160 :     SBR_INIT_VLC_STATIC(5, 1052);
      62         160 :     SBR_INIT_VLC_STATIC(6, 544);
      63         160 :     SBR_INIT_VLC_STATIC(7, 544);
      64         160 :     SBR_INIT_VLC_STATIC(8, 592);
      65         160 :     SBR_INIT_VLC_STATIC(9, 512);
      66             : 
      67         160 :     aacsbr_tableinit();
      68             : 
      69         160 :     AAC_RENAME(ff_ps_init)();
      70         160 : }
      71             : 
      72             : /** Places SBR in pure upsampling mode. */
      73         343 : static void sbr_turnoff(SpectralBandReplication *sbr) {
      74         343 :     sbr->start = 0;
      75         343 :     sbr->ready_for_dequant = 0;
      76             :     // Init defults used in pure upsampling mode
      77         343 :     sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
      78         343 :     sbr->m[1] = 0;
      79             :     // Reset values for first SBR header
      80         343 :     sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
      81         343 :     memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
      82         343 : }
      83             : 
      84         343 : av_cold void AAC_RENAME(ff_aac_sbr_ctx_init)(AACContext *ac, SpectralBandReplication *sbr, int id_aac)
      85             : {
      86         343 :     if(sbr->mdct.mdct_bits)
      87           0 :         return;
      88         343 :     sbr->kx[0] = sbr->kx[1];
      89         343 :     sbr->id_aac = id_aac;
      90         343 :     sbr_turnoff(sbr);
      91         343 :     sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
      92         343 :     sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
      93             :     /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
      94             :      * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
      95             :      * and scale back down at synthesis. */
      96         343 :     AAC_RENAME_32(ff_mdct_init)(&sbr->mdct,     7, 1, 1.0 / (64 * 32768.0));
      97         343 :     AAC_RENAME_32(ff_mdct_init)(&sbr->mdct_ana, 7, 1, -2.0 * 32768.0);
      98         343 :     AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
      99         343 :     AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
     100         343 :     aacsbr_func_ptr_init(&sbr->c);
     101             : }
     102             : 
     103         343 : av_cold void AAC_RENAME(ff_aac_sbr_ctx_close)(SpectralBandReplication *sbr)
     104             : {
     105         343 :     AAC_RENAME_32(ff_mdct_end)(&sbr->mdct);
     106         343 :     AAC_RENAME_32(ff_mdct_end)(&sbr->mdct_ana);
     107         343 : }
     108             : 
     109        6109 : static int qsort_comparison_function_int16(const void *a, const void *b)
     110             : {
     111        6109 :     return *(const int16_t *)a - *(const int16_t *)b;
     112             : }
     113             : 
     114         381 : static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
     115             : {
     116             :     int i;
     117        1771 :     for (i = 0; i <= last_el; i++)
     118        1532 :         if (table[i] == needle)
     119         142 :             return 1;
     120         239 :     return 0;
     121             : }
     122             : 
     123             : /// Limiter Frequency Band Table (14496-3 sp04 p198)
     124          65 : static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
     125             : {
     126             :     int k;
     127          65 :     if (sbr->bs_limiter_bands > 0) {
     128             :         static const INTFLOAT bands_warped[3] = { Q23(1.32715174233856803909f),   //2^(0.49/1.2)
     129             :                                                Q23(1.18509277094158210129f),   //2^(0.49/2)
     130             :                                                Q23(1.11987160404675912501f) }; //2^(0.49/3)
     131          65 :         const INTFLOAT lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
     132             :         int16_t patch_borders[7];
     133          65 :         uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
     134             : 
     135          65 :         patch_borders[0] = sbr->kx[1];
     136         251 :         for (k = 1; k <= sbr->num_patches; k++)
     137         186 :             patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
     138             : 
     139          65 :         memcpy(sbr->f_tablelim, sbr->f_tablelow,
     140          65 :                (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
     141          65 :         if (sbr->num_patches > 1)
     142          63 :             memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
     143          63 :                    (sbr->num_patches - 1) * sizeof(patch_borders[0]));
     144             : 
     145          65 :         AV_QSORT(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
     146             :               uint16_t,
     147             :               qsort_comparison_function_int16);
     148             : 
     149          65 :         sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
     150         671 :         while (out < sbr->f_tablelim + sbr->n_lim) {
     151             : #if USE_FIXED
     152          35 :             if ((*in << 23) >= *out * lim_bands_per_octave_warped) {
     153             : #else
     154         506 :             if (*in >= *out * lim_bands_per_octave_warped) {
     155             : #endif /* USE_FIXED */
     156         212 :                 *++out = *in++;
     157         612 :             } else if (*in == *out ||
     158         283 :                 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
     159         231 :                 in++;
     160         231 :                 sbr->n_lim--;
     161          98 :             } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
     162          54 :                 *out = *in++;
     163          54 :                 sbr->n_lim--;
     164             :             } else {
     165          44 :                 *++out = *in++;
     166             :             }
     167             :         }
     168             :     } else {
     169           0 :         sbr->f_tablelim[0] = sbr->f_tablelow[0];
     170           0 :         sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
     171           0 :         sbr->n_lim = 1;
     172             :     }
     173          65 : }
     174             : 
     175         489 : static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
     176             : {
     177         489 :     unsigned int cnt = get_bits_count(gb);
     178             :     uint8_t bs_header_extra_1;
     179             :     uint8_t bs_header_extra_2;
     180         489 :     int old_bs_limiter_bands = sbr->bs_limiter_bands;
     181             :     SpectrumParameters old_spectrum_params;
     182             : 
     183         489 :     sbr->start = 1;
     184         489 :     sbr->ready_for_dequant = 0;
     185             : 
     186             :     // Save last spectrum parameters variables to compare to new ones
     187         489 :     memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
     188             : 
     189         489 :     sbr->bs_amp_res_header              = get_bits1(gb);
     190         489 :     sbr->spectrum_params.bs_start_freq  = get_bits(gb, 4);
     191         489 :     sbr->spectrum_params.bs_stop_freq   = get_bits(gb, 4);
     192         489 :     sbr->spectrum_params.bs_xover_band  = get_bits(gb, 3);
     193         489 :                                           skip_bits(gb, 2); // bs_reserved
     194             : 
     195         489 :     bs_header_extra_1 = get_bits1(gb);
     196         489 :     bs_header_extra_2 = get_bits1(gb);
     197             : 
     198         489 :     if (bs_header_extra_1) {
     199         430 :         sbr->spectrum_params.bs_freq_scale  = get_bits(gb, 2);
     200         430 :         sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
     201         430 :         sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
     202             :     } else {
     203          59 :         sbr->spectrum_params.bs_freq_scale  = 2;
     204          59 :         sbr->spectrum_params.bs_alter_scale = 1;
     205          59 :         sbr->spectrum_params.bs_noise_bands = 2;
     206             :     }
     207             : 
     208             :     // Check if spectrum parameters changed
     209         489 :     if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
     210          65 :         sbr->reset = 1;
     211             : 
     212         489 :     if (bs_header_extra_2) {
     213          26 :         sbr->bs_limiter_bands  = get_bits(gb, 2);
     214          26 :         sbr->bs_limiter_gains  = get_bits(gb, 2);
     215          26 :         sbr->bs_interpol_freq  = get_bits1(gb);
     216          26 :         sbr->bs_smoothing_mode = get_bits1(gb);
     217             :     } else {
     218         463 :         sbr->bs_limiter_bands  = 2;
     219         463 :         sbr->bs_limiter_gains  = 2;
     220         463 :         sbr->bs_interpol_freq  = 1;
     221         463 :         sbr->bs_smoothing_mode = 1;
     222             :     }
     223             : 
     224         489 :     if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
     225           0 :         sbr_make_f_tablelim(sbr);
     226             : 
     227         489 :     return get_bits_count(gb) - cnt;
     228             : }
     229             : 
     230          12 : static int array_min_int16(const int16_t *array, int nel)
     231             : {
     232          12 :     int i, min = array[0];
     233          28 :     for (i = 1; i < nel; i++)
     234          16 :         min = FFMIN(array[i], min);
     235          12 :     return min;
     236             : }
     237             : 
     238          65 : static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
     239             : {
     240             :     // Requirements (14496-3 sp04 p205)
     241          65 :     if (n_master <= 0) {
     242           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
     243           0 :         return -1;
     244             :     }
     245          65 :     if (bs_xover_band >= n_master) {
     246           0 :         av_log(avctx, AV_LOG_ERROR,
     247             :                "Invalid bitstream, crossover band index beyond array bounds: %d\n",
     248             :                bs_xover_band);
     249           0 :         return -1;
     250             :     }
     251          65 :     return 0;
     252             : }
     253             : 
     254             : /// Master Frequency Band Table (14496-3 sp04 p194)
     255          65 : static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
     256             :                              SpectrumParameters *spectrum)
     257             : {
     258          65 :     unsigned int temp, max_qmf_subbands = 0;
     259             :     unsigned int start_min, stop_min;
     260             :     int k;
     261             :     const int8_t *sbr_offset_ptr;
     262             :     int16_t stop_dk[13];
     263             : 
     264          65 :     switch (sbr->sample_rate) {
     265           6 :     case 16000:
     266           6 :         sbr_offset_ptr = sbr_offset[0];
     267           6 :         break;
     268           0 :     case 22050:
     269           0 :         sbr_offset_ptr = sbr_offset[1];
     270           0 :         break;
     271           0 :     case 24000:
     272           0 :         sbr_offset_ptr = sbr_offset[2];
     273           0 :         break;
     274          12 :     case 32000:
     275          12 :         sbr_offset_ptr = sbr_offset[3];
     276          12 :         break;
     277          45 :     case 44100: case 48000: case 64000:
     278          45 :         sbr_offset_ptr = sbr_offset[4];
     279          45 :         break;
     280           2 :     case 88200: case 96000: case 128000: case 176400: case 192000:
     281           2 :         sbr_offset_ptr = sbr_offset[5];
     282           2 :         break;
     283           0 :     default:
     284           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     285             :                "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
     286           0 :         return -1;
     287             :     }
     288             : 
     289          65 :     if (sbr->sample_rate < 32000) {
     290           6 :         temp = 3000;
     291          59 :     } else if (sbr->sample_rate < 64000) {
     292          57 :         temp = 4000;
     293             :     } else
     294           2 :         temp = 5000;
     295             : 
     296          65 :     start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
     297          65 :     stop_min  = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
     298             : 
     299          65 :     sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
     300             : 
     301          65 :     if (spectrum->bs_stop_freq < 14) {
     302          65 :         sbr->k[2] = stop_min;
     303          65 :         make_bands(stop_dk, stop_min, 64, 13);
     304          65 :         AV_QSORT(stop_dk, 13, int16_t, qsort_comparison_function_int16);
     305         725 :         for (k = 0; k < spectrum->bs_stop_freq; k++)
     306         660 :             sbr->k[2] += stop_dk[k];
     307           0 :     } else if (spectrum->bs_stop_freq == 14) {
     308           0 :         sbr->k[2] = 2*sbr->k[0];
     309           0 :     } else if (spectrum->bs_stop_freq == 15) {
     310           0 :         sbr->k[2] = 3*sbr->k[0];
     311             :     } else {
     312           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     313           0 :                "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
     314           0 :         return -1;
     315             :     }
     316          65 :     sbr->k[2] = FFMIN(64, sbr->k[2]);
     317             : 
     318             :     // Requirements (14496-3 sp04 p205)
     319          65 :     if (sbr->sample_rate <= 32000) {
     320          18 :         max_qmf_subbands = 48;
     321          47 :     } else if (sbr->sample_rate == 44100) {
     322          36 :         max_qmf_subbands = 35;
     323          11 :     } else if (sbr->sample_rate >= 48000)
     324          11 :         max_qmf_subbands = 32;
     325             :     else
     326           0 :         av_assert0(0);
     327             : 
     328          65 :     if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
     329           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     330           0 :                "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
     331           0 :         return -1;
     332             :     }
     333             : 
     334          65 :     if (!spectrum->bs_freq_scale) {
     335             :         int dk, k2diff;
     336             : 
     337          14 :         dk = spectrum->bs_alter_scale + 1;
     338          14 :         sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
     339          14 :         if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
     340           0 :             return -1;
     341             : 
     342         266 :         for (k = 1; k <= sbr->n_master; k++)
     343         252 :             sbr->f_master[k] = dk;
     344             : 
     345          14 :         k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
     346          14 :         if (k2diff < 0) {
     347          14 :             sbr->f_master[1]--;
     348          14 :             sbr->f_master[2]-= (k2diff < -1);
     349           0 :         } else if (k2diff) {
     350           0 :             sbr->f_master[sbr->n_master]++;
     351             :         }
     352             : 
     353          14 :         sbr->f_master[0] = sbr->k[0];
     354         266 :         for (k = 1; k <= sbr->n_master; k++)
     355         252 :             sbr->f_master[k] += sbr->f_master[k - 1];
     356             : 
     357             :     } else {
     358          51 :         int half_bands = 7 - spectrum->bs_freq_scale;      // bs_freq_scale  = {1,2,3}
     359             :         int two_regions, num_bands_0;
     360             :         int vdk0_max, vdk1_min;
     361             :         int16_t vk0[49];
     362             : #if USE_FIXED
     363           5 :         int tmp, nz = 0;
     364             : #endif /* USE_FIXED */
     365             : 
     366          51 :         if (49 * sbr->k[2] > 110 * sbr->k[0]) {
     367          12 :             two_regions = 1;
     368          12 :             sbr->k[1] = 2 * sbr->k[0];
     369             :         } else {
     370          39 :             two_regions = 0;
     371          39 :             sbr->k[1] = sbr->k[2];
     372             :         }
     373             : 
     374             : #if USE_FIXED
     375           5 :         tmp = (sbr->k[1] << 23) / sbr->k[0];
     376          40 :         while (tmp < 0x40000000) {
     377          30 :           tmp <<= 1;
     378          30 :           nz++;
     379             :         }
     380           5 :         tmp = fixed_log(tmp - 0x80000000);
     381           5 :         tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
     382           5 :         tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
     383           5 :         num_bands_0 = ((tmp + 0x400000) >> 23) * 2;
     384             : #else
     385          46 :         num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
     386             : #endif /* USE_FIXED */
     387             : 
     388          51 :         if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
     389           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
     390           0 :             return -1;
     391             :         }
     392             : 
     393          51 :         vk0[0] = 0;
     394             : 
     395          51 :         make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
     396             : 
     397          51 :         AV_QSORT(vk0 + 1, num_bands_0, int16_t, qsort_comparison_function_int16);
     398          51 :         vdk0_max = vk0[num_bands_0];
     399             : 
     400          51 :         vk0[0] = sbr->k[0];
     401         611 :         for (k = 1; k <= num_bands_0; k++) {
     402         560 :             if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
     403           0 :                 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
     404           0 :                 return -1;
     405             :             }
     406         560 :             vk0[k] += vk0[k-1];
     407             :         }
     408             : 
     409          51 :         if (two_regions) {
     410             :             int16_t vk1[49];
     411             : #if USE_FIXED
     412             :             int num_bands_1;
     413             : 
     414           2 :             tmp = (sbr->k[2] << 23) / sbr->k[1];
     415           2 :             nz = 0;
     416          18 :             while (tmp < 0x40000000) {
     417          14 :               tmp <<= 1;
     418          14 :               nz++;
     419             :             }
     420           2 :             tmp = fixed_log(tmp - 0x80000000);
     421           2 :             tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
     422           2 :             tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
     423           2 :             if (spectrum->bs_alter_scale)
     424           2 :                 tmp = (int)(((int64_t)tmp * CONST_076923 + 0x40000000) >> 31);
     425           2 :             num_bands_1 = ((tmp + 0x400000) >> 23) * 2;
     426             : #else
     427          20 :             float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
     428          10 :                                                      : 1.0f; // bs_alter_scale = {0,1}
     429          20 :             int num_bands_1 = lrintf(half_bands * invwarp *
     430          10 :                                      log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
     431             : #endif /* USE_FIXED */
     432          12 :             make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
     433             : 
     434          12 :             vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
     435             : 
     436          12 :             if (vdk1_min < vdk0_max) {
     437             :                 int change;
     438           0 :                 AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
     439           0 :                 change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
     440           0 :                 vk1[1]           += change;
     441           0 :                 vk1[num_bands_1] -= change;
     442             :             }
     443             : 
     444          12 :             AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
     445             : 
     446          12 :             vk1[0] = sbr->k[1];
     447          40 :             for (k = 1; k <= num_bands_1; k++) {
     448          28 :                 if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
     449           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
     450           0 :                     return -1;
     451             :                 }
     452          28 :                 vk1[k] += vk1[k-1];
     453             :             }
     454             : 
     455          12 :             sbr->n_master = num_bands_0 + num_bands_1;
     456          12 :             if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
     457           0 :                 return -1;
     458          12 :             memcpy(&sbr->f_master[0],               vk0,
     459          12 :                    (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
     460          12 :             memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
     461             :                     num_bands_1      * sizeof(sbr->f_master[0]));
     462             : 
     463             :         } else {
     464          39 :             sbr->n_master = num_bands_0;
     465          39 :             if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
     466           0 :                 return -1;
     467          39 :             memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
     468             :         }
     469             :     }
     470             : 
     471          65 :     return 0;
     472             : }
     473             : 
     474             : /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
     475          65 : static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
     476             : {
     477          65 :     int i, k, last_k = -1, last_msb = -1, sb = 0;
     478          65 :     int msb = sbr->k[0];
     479          65 :     int usb = sbr->kx[1];
     480          65 :     int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
     481             : 
     482          65 :     sbr->num_patches = 0;
     483             : 
     484          65 :     if (goal_sb < sbr->kx[1] + sbr->m[1]) {
     485          31 :         for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
     486             :     } else
     487          34 :         k = sbr->n_master;
     488             : 
     489             :     do {
     490         186 :         int odd = 0;
     491         186 :         if (k == last_k && msb == last_msb) {
     492           0 :             av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
     493           0 :             return AVERROR_INVALIDDATA;
     494             :         }
     495         186 :         last_k = k;
     496         186 :         last_msb = msb;
     497        1060 :         for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
     498         874 :             sb = sbr->f_master[i];
     499         874 :             odd = (sb + sbr->k[0]) & 1;
     500             :         }
     501             : 
     502             :         // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
     503             :         // After this check the final number of patches can still be six which is
     504             :         // illegal however the Coding Technologies decoder check stream has a final
     505             :         // count of 6 patches
     506         186 :         if (sbr->num_patches > 5) {
     507           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
     508           0 :             return -1;
     509             :         }
     510             : 
     511         186 :         sbr->patch_num_subbands[sbr->num_patches]  = FFMAX(sb - usb, 0);
     512         186 :         sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
     513             : 
     514         186 :         if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
     515         186 :             usb = sb;
     516         186 :             msb = sb;
     517         186 :             sbr->num_patches++;
     518             :         } else
     519           0 :             msb = sbr->kx[1];
     520             : 
     521         186 :         if (sbr->f_master[k] - sb < 3)
     522          86 :             k = sbr->n_master;
     523         186 :     } while (sb != sbr->kx[1] + sbr->m[1]);
     524             : 
     525         128 :     if (sbr->num_patches > 1 &&
     526          63 :         sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
     527           0 :         sbr->num_patches--;
     528             : 
     529          65 :     return 0;
     530             : }
     531             : 
     532             : /// Derived Frequency Band Tables (14496-3 sp04 p197)
     533          65 : static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
     534             : {
     535             :     int k, temp;
     536             : #if USE_FIXED
     537           5 :     int nz = 0;
     538             : #endif /* USE_FIXED */
     539             : 
     540          65 :     sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
     541          65 :     sbr->n[0] = (sbr->n[1] + 1) >> 1;
     542             : 
     543          65 :     memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
     544          65 :            (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
     545          65 :     sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
     546          65 :     sbr->kx[1] = sbr->f_tablehigh[0];
     547             : 
     548             :     // Requirements (14496-3 sp04 p205)
     549          65 :     if (sbr->kx[1] + sbr->m[1] > 64) {
     550           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     551           0 :                "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
     552           0 :         return -1;
     553             :     }
     554          65 :     if (sbr->kx[1] > 32) {
     555           0 :         av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
     556           0 :         return -1;
     557             :     }
     558             : 
     559          65 :     sbr->f_tablelow[0] = sbr->f_tablehigh[0];
     560          65 :     temp = sbr->n[1] & 1;
     561         485 :     for (k = 1; k <= sbr->n[0]; k++)
     562         420 :         sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
     563             : #if USE_FIXED
     564           5 :     temp = (sbr->k[2] << 23) / sbr->kx[1];
     565          40 :     while (temp < 0x40000000) {
     566          30 :         temp <<= 1;
     567          30 :         nz++;
     568             :     }
     569           5 :     temp = fixed_log(temp - 0x80000000);
     570           5 :     temp = (int)(((int64_t)temp * CONST_RECIP_LN2 + 0x20000000) >> 30);
     571           5 :     temp = (((temp + 0x80) >> 8) + ((8 - nz) << 23)) * sbr->spectrum_params.bs_noise_bands;
     572             : 
     573           5 :     sbr->n_q = (temp + 0x400000) >> 23;
     574           5 :     if (sbr->n_q < 1)
     575           0 :         sbr->n_q = 1;
     576             : #else
     577          60 :     sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
     578             :                                log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
     579             : #endif /* USE_FIXED */
     580             : 
     581          65 :     if (sbr->n_q > 5) {
     582           0 :         av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
     583           0 :         return -1;
     584             :     }
     585             : 
     586          65 :     sbr->f_tablenoise[0] = sbr->f_tablelow[0];
     587          65 :     temp = 0;
     588         266 :     for (k = 1; k <= sbr->n_q; k++) {
     589         201 :         temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
     590         201 :         sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
     591             :     }
     592             : 
     593          65 :     if (sbr_hf_calc_npatches(ac, sbr) < 0)
     594           0 :         return -1;
     595             : 
     596          65 :     sbr_make_f_tablelim(sbr);
     597             : 
     598          65 :     sbr->data[0].f_indexnoise = 0;
     599          65 :     sbr->data[1].f_indexnoise = 0;
     600             : 
     601          65 :     return 0;
     602             : }
     603             : 
     604       22940 : static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
     605             :                                               int elements)
     606             : {
     607             :     int i;
     608       71495 :     for (i = 0; i < elements; i++) {
     609       48555 :         vec[i] = get_bits1(gb);
     610             :     }
     611       22940 : }
     612             : 
     613             : /** ceil(log2(index+1)) */
     614             : static const int8_t ceil_log2[] = {
     615             :     0, 1, 2, 2, 3, 3,
     616             : };
     617             : 
     618        7793 : static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
     619             :                          GetBitContext *gb, SBRData *ch_data)
     620             : {
     621             :     int i;
     622        7793 :     int bs_pointer = 0;
     623             :     // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
     624        7793 :     int abs_bord_trail = 16;
     625             :     int num_rel_lead, num_rel_trail;
     626        7793 :     unsigned bs_num_env_old = ch_data->bs_num_env;
     627             :     int bs_frame_class, bs_num_env;
     628             : 
     629        7793 :     ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
     630        7793 :     ch_data->bs_amp_res = sbr->bs_amp_res_header;
     631        7793 :     ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
     632             : 
     633        7793 :     switch (bs_frame_class = get_bits(gb, 2)) {
     634        6395 :     case FIXFIX:
     635        6395 :         bs_num_env = 1 << get_bits(gb, 2);
     636        6395 :         if (bs_num_env > 4) {
     637           0 :             av_log(ac->avctx, AV_LOG_ERROR,
     638             :                    "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
     639             :                    bs_num_env);
     640           0 :             return -1;
     641             :         }
     642        6395 :         ch_data->bs_num_env = bs_num_env;
     643        6395 :         num_rel_lead                        = ch_data->bs_num_env - 1;
     644        6395 :         if (ch_data->bs_num_env == 1)
     645        4929 :             ch_data->bs_amp_res = 0;
     646             : 
     647             : 
     648        6395 :         ch_data->t_env[0]                   = 0;
     649        6395 :         ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
     650             : 
     651       12790 :         abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
     652        6395 :                    ch_data->bs_num_env;
     653        7861 :         for (i = 0; i < num_rel_lead; i++)
     654        1466 :             ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
     655             : 
     656        6395 :         ch_data->bs_freq_res[1] = get_bits1(gb);
     657        7861 :         for (i = 1; i < ch_data->bs_num_env; i++)
     658        1466 :             ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
     659        6395 :         break;
     660         630 :     case FIXVAR:
     661         630 :         abs_bord_trail                     += get_bits(gb, 2);
     662         630 :         num_rel_trail                       = get_bits(gb, 2);
     663         630 :         ch_data->bs_num_env                 = num_rel_trail + 1;
     664         630 :         ch_data->t_env[0]                   = 0;
     665         630 :         ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
     666             : 
     667        1992 :         for (i = 0; i < num_rel_trail; i++)
     668        2724 :             ch_data->t_env[ch_data->bs_num_env - 1 - i] =
     669        2724 :                 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
     670             : 
     671         630 :         bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
     672             : 
     673        2622 :         for (i = 0; i < ch_data->bs_num_env; i++)
     674        1992 :             ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
     675         630 :         break;
     676         620 :     case VARFIX:
     677         620 :         ch_data->t_env[0]                   = get_bits(gb, 2);
     678         620 :         num_rel_lead                        = get_bits(gb, 2);
     679         620 :         ch_data->bs_num_env                 = num_rel_lead + 1;
     680         620 :         ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
     681             : 
     682        1396 :         for (i = 0; i < num_rel_lead; i++)
     683         776 :             ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
     684             : 
     685         620 :         bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
     686             : 
     687         620 :         get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
     688         620 :         break;
     689         148 :     case VARVAR:
     690         148 :         ch_data->t_env[0]                   = get_bits(gb, 2);
     691         148 :         abs_bord_trail                     += get_bits(gb, 2);
     692         148 :         num_rel_lead                        = get_bits(gb, 2);
     693         148 :         num_rel_trail                       = get_bits(gb, 2);
     694         148 :         bs_num_env                          = num_rel_lead + num_rel_trail + 1;
     695             : 
     696         148 :         if (bs_num_env > 5) {
     697           0 :             av_log(ac->avctx, AV_LOG_ERROR,
     698             :                    "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
     699             :                    bs_num_env);
     700           0 :             return -1;
     701             :         }
     702         148 :         ch_data->bs_num_env = bs_num_env;
     703             : 
     704         148 :         ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
     705             : 
     706         220 :         for (i = 0; i < num_rel_lead; i++)
     707          72 :             ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
     708         439 :         for (i = 0; i < num_rel_trail; i++)
     709         582 :             ch_data->t_env[ch_data->bs_num_env - 1 - i] =
     710         582 :                 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
     711             : 
     712         148 :         bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
     713             : 
     714         148 :         get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
     715         148 :         break;
     716             :     }
     717        7793 :     ch_data->bs_frame_class = bs_frame_class;
     718             : 
     719        7793 :     av_assert0(bs_pointer >= 0);
     720        7793 :     if (bs_pointer > ch_data->bs_num_env + 1) {
     721           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     722             :                "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
     723             :                bs_pointer);
     724           0 :         return -1;
     725             :     }
     726             : 
     727       19553 :     for (i = 1; i <= ch_data->bs_num_env; i++) {
     728       11760 :         if (ch_data->t_env[i-1] >= ch_data->t_env[i]) {
     729           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Not strictly monotone time borders\n");
     730           0 :             return -1;
     731             :         }
     732             :     }
     733             : 
     734        7793 :     ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
     735             : 
     736        7793 :     ch_data->t_q[0]                     = ch_data->t_env[0];
     737        7793 :     ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
     738        7793 :     if (ch_data->bs_num_noise > 1) {
     739             :         int idx;
     740        2864 :         if (ch_data->bs_frame_class == FIXFIX) {
     741        1466 :             idx = ch_data->bs_num_env >> 1;
     742        1398 :         } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
     743         778 :             idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
     744             :         } else { // VARFIX
     745         620 :             if (!bs_pointer)
     746         464 :                 idx = 1;
     747         156 :             else if (bs_pointer == 1)
     748         156 :                 idx = ch_data->bs_num_env - 1;
     749             :             else // bs_pointer > 1
     750           0 :                 idx = bs_pointer - 1;
     751             :         }
     752        2864 :         ch_data->t_q[1] = ch_data->t_env[idx];
     753             :     }
     754             : 
     755        7793 :     ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
     756        7793 :     ch_data->e_a[1] = -1;
     757        7793 :     if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
     758         778 :         ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
     759        7015 :     } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
     760           0 :         ch_data->e_a[1] = bs_pointer - 1;
     761             : 
     762        7793 :     return 0;
     763             : }
     764             : 
     765        2625 : static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
     766             :     //These variables are saved from the previous frame rather than copied
     767        2625 :     dst->bs_freq_res[0]    = dst->bs_freq_res[dst->bs_num_env];
     768        2625 :     dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
     769        2625 :     dst->e_a[0]            = -(dst->e_a[1] != dst->bs_num_env);
     770             : 
     771             :     //These variables are read from the bitstream and therefore copied
     772        2625 :     memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
     773        2625 :     memcpy(dst->t_env,         src->t_env,         sizeof(dst->t_env));
     774        2625 :     memcpy(dst->t_q,           src->t_q,           sizeof(dst->t_q));
     775        2625 :     dst->bs_num_env        = src->bs_num_env;
     776        2625 :     dst->bs_amp_res        = src->bs_amp_res;
     777        2625 :     dst->bs_num_noise      = src->bs_num_noise;
     778        2625 :     dst->bs_frame_class    = src->bs_frame_class;
     779        2625 :     dst->e_a[1]            = src->e_a[1];
     780        2625 : }
     781             : 
     782             : /// Read how the envelope and noise floor data is delta coded
     783       10418 : static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
     784             :                           SBRData *ch_data)
     785             : {
     786       10418 :     get_bits1_vector(gb, ch_data->bs_df_env,   ch_data->bs_num_env);
     787       10418 :     get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
     788       10418 : }
     789             : 
     790             : /// Read inverse filtering data
     791        7793 : static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
     792             :                           SBRData *ch_data)
     793             : {
     794             :     int i;
     795             : 
     796        7793 :     memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
     797       31778 :     for (i = 0; i < sbr->n_q; i++)
     798       23985 :         ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
     799        7793 : }
     800             : 
     801       10418 : static int read_sbr_envelope(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
     802             :                               SBRData *ch_data, int ch)
     803             : {
     804             :     int bits;
     805             :     int i, j, k;
     806             :     VLC_TYPE (*t_huff)[2], (*f_huff)[2];
     807             :     int t_lav, f_lav;
     808       10418 :     const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
     809       10418 :     const int odd = sbr->n[1] & 1;
     810             : 
     811       10418 :     if (sbr->bs_coupling && ch) {
     812        5250 :         if (ch_data->bs_amp_res) {
     813         582 :             bits   = 5;
     814         582 :             t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
     815         582 :             t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];
     816         582 :             f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
     817         582 :             f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
     818             :         } else {
     819        2043 :             bits   = 6;
     820        2043 :             t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
     821        2043 :             t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];
     822        2043 :             f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
     823        2043 :             f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];
     824             :         }
     825             :     } else {
     826        7793 :         if (ch_data->bs_amp_res) {
     827        2864 :             bits   = 6;
     828        2864 :             t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
     829        2864 :             t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];
     830        2864 :             f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
     831        2864 :             f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
     832             :         } else {
     833        4929 :             bits   = 7;
     834        4929 :             t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
     835        4929 :             t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];
     836        4929 :             f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
     837        4929 :             f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];
     838             :         }
     839             :     }
     840             : 
     841       25578 :     for (i = 0; i < ch_data->bs_num_env; i++) {
     842       15160 :         if (ch_data->bs_df_env[i]) {
     843             :             // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
     844        4947 :             if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
     845       55736 :                 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
     846       51282 :                     ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
     847       51282 :                     if (ch_data->env_facs_q[i + 1][j] > 127U) {
     848           0 :                         av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
     849           0 :                         return AVERROR_INVALIDDATA;
     850             :                     }
     851             :                 }
     852         493 :             } else if (ch_data->bs_freq_res[i + 1]) {
     853        4655 :                 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
     854        4282 :                     k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
     855        4282 :                     ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
     856        4282 :                     if (ch_data->env_facs_q[i + 1][j] > 127U) {
     857           0 :                         av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
     858           0 :                         return AVERROR_INVALIDDATA;
     859             :                     }
     860             :                 }
     861             :             } else {
     862         799 :                 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
     863         679 :                     k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
     864         679 :                     ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
     865         679 :                     if (ch_data->env_facs_q[i + 1][j] > 127U) {
     866           0 :                         av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
     867           0 :                         return AVERROR_INVALIDDATA;
     868             :                     }
     869             :                 }
     870             :             }
     871             :         } else {
     872       10213 :             ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
     873      115673 :             for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
     874      105460 :                 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
     875      105460 :                 if (ch_data->env_facs_q[i + 1][j] > 127U) {
     876           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
     877           0 :                     return AVERROR_INVALIDDATA;
     878             :                 }
     879             :             }
     880             :         }
     881             :     }
     882             : 
     883             :     //assign 0th elements of env_facs_q from last elements
     884       10418 :     memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],
     885             :            sizeof(ch_data->env_facs_q[0]));
     886             : 
     887       10418 :     return 0;
     888             : }
     889             : 
     890       10418 : static int read_sbr_noise(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
     891             :                            SBRData *ch_data, int ch)
     892             : {
     893             :     int i, j;
     894             :     VLC_TYPE (*t_huff)[2], (*f_huff)[2];
     895             :     int t_lav, f_lav;
     896       10418 :     int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
     897             : 
     898       10418 :     if (sbr->bs_coupling && ch) {
     899        2625 :         t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
     900        2625 :         t_lav  = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB];
     901        2625 :         f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
     902        2625 :         f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
     903             :     } else {
     904        7793 :         t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
     905        7793 :         t_lav  = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB];
     906        7793 :         f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
     907        7793 :         f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
     908             :     }
     909             : 
     910       24282 :     for (i = 0; i < ch_data->bs_num_noise; i++) {
     911       13864 :         if (ch_data->bs_df_noise[i]) {
     912       43693 :             for (j = 0; j < sbr->n_q; j++) {
     913       32863 :                 ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
     914       32863 :                 if (ch_data->noise_facs_q[i + 1][j] > 30U) {
     915           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
     916           0 :                     return AVERROR_INVALIDDATA;
     917             :                 }
     918             :             }
     919             :         } else {
     920        3034 :             ch_data->noise_facs_q[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
     921        8944 :             for (j = 1; j < sbr->n_q; j++) {
     922        5910 :                 ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
     923        5910 :                 if (ch_data->noise_facs_q[i + 1][j] > 30U) {
     924           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
     925           0 :                     return AVERROR_INVALIDDATA;
     926             :                 }
     927             :             }
     928             :         }
     929             :     }
     930             : 
     931             :     //assign 0th elements of noise_facs_q from last elements
     932       10418 :     memcpy(ch_data->noise_facs_q[0], ch_data->noise_facs_q[ch_data->bs_num_noise],
     933             :            sizeof(ch_data->noise_facs_q[0]));
     934       10418 :     return 0;
     935             : }
     936             : 
     937        1616 : static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
     938             :                                GetBitContext *gb,
     939             :                                int bs_extension_id, int *num_bits_left)
     940             : {
     941        1616 :     switch (bs_extension_id) {
     942        1616 :     case EXTENSION_ID_PS:
     943        1616 :         if (!ac->oc[1].m4ac.ps) {
     944           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
     945           0 :             skip_bits_long(gb, *num_bits_left); // bs_fill_bits
     946           0 :             *num_bits_left = 0;
     947             :         } else {
     948        1616 :             *num_bits_left -= AAC_RENAME(ff_ps_read_data)(ac->avctx, gb, &sbr->ps, *num_bits_left);
     949        1616 :             ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
     950             :         }
     951        1616 :         break;
     952           0 :     default:
     953             :         // some files contain 0-padding
     954           0 :         if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
     955           0 :             avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
     956           0 :         skip_bits_long(gb, *num_bits_left); // bs_fill_bits
     957           0 :         *num_bits_left = 0;
     958           0 :         break;
     959             :     }
     960        1616 : }
     961             : 
     962        2646 : static int read_sbr_single_channel_element(AACContext *ac,
     963             :                                             SpectralBandReplication *sbr,
     964             :                                             GetBitContext *gb)
     965             : {
     966             :     int ret;
     967             : 
     968        2646 :     if (get_bits1(gb)) // bs_data_extra
     969           0 :         skip_bits(gb, 4); // bs_reserved
     970             : 
     971        2646 :     if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
     972           0 :         return -1;
     973        2646 :     read_sbr_dtdf(sbr, gb, &sbr->data[0]);
     974        2646 :     read_sbr_invf(sbr, gb, &sbr->data[0]);
     975        2646 :     if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
     976           0 :         return ret;
     977        2646 :     if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
     978           0 :         return ret;
     979             : 
     980        2646 :     if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
     981         174 :         get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
     982             : 
     983        2646 :     return 0;
     984             : }
     985             : 
     986        3886 : static int read_sbr_channel_pair_element(AACContext *ac,
     987             :                                           SpectralBandReplication *sbr,
     988             :                                           GetBitContext *gb)
     989             : {
     990             :     int ret;
     991             : 
     992        3886 :     if (get_bits1(gb))    // bs_data_extra
     993           0 :         skip_bits(gb, 8); // bs_reserved
     994             : 
     995        3886 :     if ((sbr->bs_coupling = get_bits1(gb))) {
     996        2625 :         if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
     997           0 :             return -1;
     998        2625 :         copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
     999        2625 :         read_sbr_dtdf(sbr, gb, &sbr->data[0]);
    1000        2625 :         read_sbr_dtdf(sbr, gb, &sbr->data[1]);
    1001        2625 :         read_sbr_invf(sbr, gb, &sbr->data[0]);
    1002        2625 :         memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
    1003        2625 :         memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
    1004        2625 :         if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
    1005           0 :             return ret;
    1006        2625 :         if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
    1007           0 :             return ret;
    1008        2625 :         if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
    1009           0 :             return ret;
    1010        2625 :         if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
    1011           0 :             return ret;
    1012             :     } else {
    1013        2522 :         if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
    1014        1261 :             read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
    1015           0 :             return -1;
    1016        1261 :         read_sbr_dtdf(sbr, gb, &sbr->data[0]);
    1017        1261 :         read_sbr_dtdf(sbr, gb, &sbr->data[1]);
    1018        1261 :         read_sbr_invf(sbr, gb, &sbr->data[0]);
    1019        1261 :         read_sbr_invf(sbr, gb, &sbr->data[1]);
    1020        1261 :         if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
    1021           0 :             return ret;
    1022        1261 :         if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
    1023           0 :             return ret;
    1024        1261 :         if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
    1025           0 :             return ret;
    1026        1261 :         if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
    1027           0 :             return ret;
    1028             :     }
    1029             : 
    1030        3886 :     if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
    1031         698 :         get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
    1032        3886 :     if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
    1033         464 :         get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
    1034             : 
    1035        3886 :     return 0;
    1036             : }
    1037             : 
    1038        6532 : static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
    1039             :                                   GetBitContext *gb, int id_aac)
    1040             : {
    1041        6532 :     unsigned int cnt = get_bits_count(gb);
    1042             : 
    1043        6532 :     sbr->id_aac = id_aac;
    1044        6532 :     sbr->ready_for_dequant = 1;
    1045             : 
    1046        6532 :     if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
    1047        5292 :         if (read_sbr_single_channel_element(ac, sbr, gb)) {
    1048           0 :             sbr_turnoff(sbr);
    1049           0 :             return get_bits_count(gb) - cnt;
    1050             :         }
    1051        3886 :     } else if (id_aac == TYPE_CPE) {
    1052        3886 :         if (read_sbr_channel_pair_element(ac, sbr, gb)) {
    1053           0 :             sbr_turnoff(sbr);
    1054           0 :             return get_bits_count(gb) - cnt;
    1055             :         }
    1056             :     } else {
    1057           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1058             :             "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
    1059           0 :         sbr_turnoff(sbr);
    1060           0 :         return get_bits_count(gb) - cnt;
    1061             :     }
    1062        6532 :     if (get_bits1(gb)) { // bs_extended_data
    1063        1616 :         int num_bits_left = get_bits(gb, 4); // bs_extension_size
    1064        1616 :         if (num_bits_left == 15)
    1065         258 :             num_bits_left += get_bits(gb, 8); // bs_esc_count
    1066             : 
    1067        1616 :         num_bits_left <<= 3;
    1068        4848 :         while (num_bits_left > 7) {
    1069        1616 :             num_bits_left -= 2;
    1070        1616 :             read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
    1071             :         }
    1072        1616 :         if (num_bits_left < 0) {
    1073           0 :             av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
    1074             :         }
    1075        1616 :         if (num_bits_left > 0)
    1076        1451 :             skip_bits(gb, num_bits_left);
    1077             :     }
    1078             : 
    1079        6532 :     return get_bits_count(gb) - cnt;
    1080             : }
    1081             : 
    1082          65 : static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
    1083             : {
    1084             :     int err;
    1085          65 :     err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
    1086          65 :     if (err >= 0)
    1087          65 :         err = sbr_make_f_derived(ac, sbr);
    1088          65 :     if (err < 0) {
    1089           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1090             :                "SBR reset failed. Switching SBR to pure upsampling mode.\n");
    1091           0 :         sbr_turnoff(sbr);
    1092             :     }
    1093          65 : }
    1094             : 
    1095             : /**
    1096             :  * Decode Spectral Band Replication extension data; reference: table 4.55.
    1097             :  *
    1098             :  * @param   crc flag indicating the presence of CRC checksum
    1099             :  * @param   cnt length of TYPE_FIL syntactic element in bytes
    1100             :  *
    1101             :  * @return  Returns number of bytes consumed from the TYPE_FIL element.
    1102             :  */
    1103       10901 : int AAC_RENAME(ff_decode_sbr_extension)(AACContext *ac, SpectralBandReplication *sbr,
    1104             :                             GetBitContext *gb_host, int crc, int cnt, int id_aac)
    1105             : {
    1106       10901 :     unsigned int num_sbr_bits = 0, num_align_bits;
    1107             :     unsigned bytes_read;
    1108       10901 :     GetBitContext gbc = *gb_host, *gb = &gbc;
    1109       10901 :     skip_bits_long(gb_host, cnt*8 - 4);
    1110             : 
    1111       10901 :     sbr->reset = 0;
    1112             : 
    1113       10901 :     if (!sbr->sample_rate)
    1114          82 :         sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
    1115       10901 :     if (!ac->oc[1].m4ac.ext_sample_rate)
    1116          18 :         ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
    1117             : 
    1118       10901 :     if (crc) {
    1119           0 :         skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
    1120           0 :         num_sbr_bits += 10;
    1121             :     }
    1122             : 
    1123             :     //Save some state from the previous frame.
    1124       10901 :     sbr->kx[0] = sbr->kx[1];
    1125       10901 :     sbr->m[0] = sbr->m[1];
    1126       10901 :     sbr->kx_and_m_pushed = 1;
    1127             : 
    1128       10901 :     num_sbr_bits++;
    1129       10901 :     if (get_bits1(gb)) // bs_header_flag
    1130         489 :         num_sbr_bits += read_sbr_header(sbr, gb);
    1131             : 
    1132       10901 :     if (sbr->reset)
    1133          65 :         sbr_reset(ac, sbr);
    1134             : 
    1135       10901 :     if (sbr->start)
    1136        6532 :         num_sbr_bits  += read_sbr_data(ac, sbr, gb, id_aac);
    1137             : 
    1138       10901 :     num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
    1139       10901 :     bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
    1140             : 
    1141       10901 :     if (bytes_read > cnt) {
    1142           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1143             :                "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
    1144           0 :         sbr_turnoff(sbr);
    1145             :     }
    1146       10901 :     return cnt;
    1147             : }
    1148             : 
    1149             : /**
    1150             :  * Analysis QMF Bank (14496-3 sp04 p206)
    1151             :  *
    1152             :  * @param   x       pointer to the beginning of the first sample window
    1153             :  * @param   W       array of complex-valued samples split into subbands
    1154             :  */
    1155             : #ifndef sbr_qmf_analysis
    1156             : #if USE_FIXED
    1157        8328 : static void sbr_qmf_analysis(AVFixedDSPContext *dsp, FFTContext *mdct,
    1158             : #else
    1159       11409 : static void sbr_qmf_analysis(AVFloatDSPContext *dsp, FFTContext *mdct,
    1160             : #endif /* USE_FIXED */
    1161             :                              SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x,
    1162             :                              INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
    1163             : {
    1164             :     int i;
    1165             : #if USE_FIXED
    1166             :     int j;
    1167             : #endif
    1168       19737 :     memcpy(x    , x+1024, (320-32)*sizeof(x[0]));
    1169       19737 :     memcpy(x+288, in,         1024*sizeof(x[0]));
    1170      651321 :     for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
    1171             :                                // are not supported
    1172      631584 :         dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
    1173      631584 :         sbrdsp->sum64x5(z);
    1174      631584 :         sbrdsp->qmf_pre_shuffle(z);
    1175             : #if USE_FIXED
    1176    17322240 :         for (j = 64; j < 128; j++) {
    1177    17055744 :             if (z[j] > 1<<24) {
    1178           0 :                 av_log(NULL, AV_LOG_WARNING,
    1179             :                        "sbr_qmf_analysis: value %09d too large, setting to %09d\n",
    1180           0 :                        z[j], 1<<24);
    1181           0 :                 z[j] = 1<<24;
    1182    17055744 :             } else if (z[j] < -(1<<24)) {
    1183           0 :                 av_log(NULL, AV_LOG_WARNING,
    1184             :                        "sbr_qmf_analysis: value %09d too small, setting to %09d\n",
    1185           0 :                        z[j], -(1<<24));
    1186           0 :                 z[j] = -(1<<24);
    1187             :             }
    1188             :         }
    1189             : #endif
    1190      631584 :         mdct->imdct_half(mdct, z, z+64);
    1191      631584 :         sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
    1192      631584 :         x += 32;
    1193             :     }
    1194       19737 : }
    1195             : #endif
    1196             : 
    1197             : /**
    1198             :  * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
    1199             :  * (14496-3 sp04 p206)
    1200             :  */
    1201             : #ifndef sbr_qmf_synthesis
    1202       21736 : static void sbr_qmf_synthesis(FFTContext *mdct,
    1203             : #if USE_FIXED
    1204             :                               SBRDSPContext *sbrdsp, AVFixedDSPContext *dsp,
    1205             : #else
    1206             :                               SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
    1207             : #endif /* USE_FIXED */
    1208             :                               INTFLOAT *out, INTFLOAT X[2][38][64],
    1209             :                               INTFLOAT mdct_buf[2][64],
    1210             :                               INTFLOAT *v0, int *v_off, const unsigned int div)
    1211             : {
    1212             :     int i, n;
    1213       21736 :     const INTFLOAT *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
    1214       21736 :     const int step = 128 >> div;
    1215             :     INTFLOAT *v;
    1216      717288 :     for (i = 0; i < 32; i++) {
    1217      695552 :         if (*v_off < step) {
    1218       66284 :             int saved_samples = (1280 - 128) >> div;
    1219       66284 :             memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
    1220       66284 :             *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
    1221             :         } else {
    1222      629268 :             *v_off -= step;
    1223             :         }
    1224      695552 :         v = v0 + *v_off;
    1225      695552 :         if (div) {
    1226     4853376 :             for (n = 0; n < 32; n++) {
    1227     4706304 :                 X[0][i][   n] = -X[0][i][n];
    1228     4706304 :                 X[0][i][32+n] =  X[1][i][31-n];
    1229             :             }
    1230      147072 :             mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
    1231      147072 :             sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
    1232             :         } else {
    1233      548480 :             sbrdsp->neg_odd_64(X[1][i]);
    1234      548480 :             mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
    1235      548480 :             mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
    1236      548480 :             sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
    1237             :         }
    1238      695552 :         dsp->vector_fmul    (out, v                , sbr_qmf_window                       , 64 >> div);
    1239      695552 :         dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out   , 64 >> div);
    1240      695552 :         dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out   , 64 >> div);
    1241      695552 :         dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out   , 64 >> div);
    1242      695552 :         dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out   , 64 >> div);
    1243      695552 :         dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out   , 64 >> div);
    1244      695552 :         dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out   , 64 >> div);
    1245      695552 :         dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out   , 64 >> div);
    1246      695552 :         dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out   , 64 >> div);
    1247      695552 :         dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out   , 64 >> div);
    1248      695552 :         out += 64 >> div;
    1249             :     }
    1250       21736 : }
    1251             : #endif
    1252             : 
    1253             : /// Generate the subband filtered lowband
    1254       19737 : static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr,
    1255             :                       INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2],
    1256             :                       int buf_idx)
    1257             : {
    1258             :     int i, k;
    1259       19737 :     const int t_HFGen = 8;
    1260       19737 :     const int i_f = 32;
    1261       19737 :     memset(X_low, 0, 32*sizeof(*X_low));
    1262      510367 :     for (k = 0; k < sbr->kx[1]; k++) {
    1263    16190790 :         for (i = t_HFGen; i < i_f + t_HFGen; i++) {
    1264    15700160 :             X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
    1265    15700160 :             X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
    1266             :         }
    1267             :     }
    1268       19737 :     buf_idx = 1-buf_idx;
    1269      511207 :     for (k = 0; k < sbr->kx[0]; k++) {
    1270     4423230 :         for (i = 0; i < t_HFGen; i++) {
    1271     3931760 :             X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
    1272     3931760 :             X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
    1273             :         }
    1274             :     }
    1275       19737 :     return 0;
    1276             : }
    1277             : 
    1278             : /// High Frequency Generator (14496-3 sp04 p215)
    1279       10418 : static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
    1280             :                       INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2],
    1281             :                       const INTFLOAT (*alpha0)[2], const INTFLOAT (*alpha1)[2],
    1282             :                       const INTFLOAT bw_array[5], const uint8_t *t_env,
    1283             :                       int bs_num_env)
    1284             : {
    1285             :     int j, x;
    1286       10418 :     int g = 0;
    1287       10418 :     int k = sbr->kx[1];
    1288       36609 :     for (j = 0; j < sbr->num_patches; j++) {
    1289      282933 :         for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
    1290      256742 :             const int p = sbr->patch_start_subband[j] + x;
    1291      790580 :             while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
    1292      277096 :                 g++;
    1293      256742 :             g--;
    1294             : 
    1295      256742 :             if (g < 0) {
    1296           0 :                 av_log(ac->avctx, AV_LOG_ERROR,
    1297             :                        "ERROR : no subband found for frequency %d\n", k);
    1298           0 :                 return -1;
    1299             :             }
    1300             : 
    1301     1283710 :             sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
    1302      256742 :                             X_low[p]  + ENVELOPE_ADJUSTMENT_OFFSET,
    1303      513484 :                             alpha0[p], alpha1[p], bw_array[g],
    1304      513484 :                             2 * t_env[0], 2 * t_env[bs_num_env]);
    1305             :         }
    1306             :     }
    1307       10418 :     if (k < sbr->m[1] + sbr->kx[1])
    1308           0 :         memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
    1309             : 
    1310       10418 :     return 0;
    1311             : }
    1312             : 
    1313             : /// Generate the subband filtered lowband
    1314       19737 : static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64],
    1315             :                      const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2],
    1316             :                      const INTFLOAT X_low[32][40][2], int ch)
    1317             : {
    1318             :     int k, i;
    1319       19737 :     const int i_f = 32;
    1320       19737 :     const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
    1321       19737 :     memset(X, 0, 2*sizeof(*X));
    1322      511207 :     for (k = 0; k < sbr->kx[0]; k++) {
    1323      547200 :         for (i = 0; i < i_Temp; i++) {
    1324       55730 :             X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
    1325       55730 :             X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
    1326             :         }
    1327             :     }
    1328      273883 :     for (; k < sbr->kx[0] + sbr->m[0]; k++) {
    1329      322412 :         for (i = 0; i < i_Temp; i++) {
    1330       68266 :             X[0][i][k] = Y0[i + i_f][k][0];
    1331       68266 :             X[1][i][k] = Y0[i + i_f][k][1];
    1332             :         }
    1333             :     }
    1334             : 
    1335      510367 :     for (k = 0; k < sbr->kx[1]; k++) {
    1336    19078840 :         for (i = i_Temp; i < 38; i++) {
    1337    18588210 :             X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
    1338    18588210 :             X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
    1339             :         }
    1340             :     }
    1341      276479 :     for (; k < sbr->kx[1] + sbr->m[1]; k++) {
    1342     8404220 :         for (i = i_Temp; i < i_f; i++) {
    1343     8147478 :             X[0][i][k] = Y1[i][k][0];
    1344     8147478 :             X[1][i][k] = Y1[i][k][1];
    1345             :         }
    1346             :     }
    1347       19737 :     return 0;
    1348             : }
    1349             : 
    1350             : /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
    1351             :  * (14496-3 sp04 p217)
    1352             :  */
    1353       10418 : static int sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
    1354             :                         SBRData *ch_data, int e_a[2])
    1355             : {
    1356             :     int e, i, m;
    1357             : 
    1358       10418 :     memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
    1359       25578 :     for (e = 0; e < ch_data->bs_num_env; e++) {
    1360       15160 :         const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
    1361       15160 :         uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
    1362             :         int k;
    1363             : 
    1364       15160 :         if (sbr->kx[1] != table[0]) {
    1365           0 :             av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
    1366             :                    "Derived frequency tables were not regenerated.\n");
    1367           0 :             sbr_turnoff(sbr);
    1368           0 :             return AVERROR_BUG;
    1369             :         }
    1370      187076 :         for (i = 0; i < ilim; i++)
    1371      550938 :             for (m = table[i]; m < table[i + 1]; m++)
    1372      379022 :                 sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
    1373             : 
    1374             :         // ch_data->bs_num_noise > 1 => 2 noise floors
    1375       15160 :         k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
    1376       60744 :         for (i = 0; i < sbr->n_q; i++)
    1377      424606 :             for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
    1378      379022 :                 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
    1379             : 
    1380      197376 :         for (i = 0; i < sbr->n[1]; i++) {
    1381      182216 :             if (ch_data->bs_add_harmonic_flag) {
    1382       32694 :                 const unsigned int m_midpoint =
    1383       32694 :                     (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
    1384             : 
    1385       65388 :                 ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
    1386       32694 :                     (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
    1387             :             }
    1388             :         }
    1389             : 
    1390      187076 :         for (i = 0; i < ilim; i++) {
    1391      171916 :             int additional_sinusoid_present = 0;
    1392      544482 :             for (m = table[i]; m < table[i + 1]; m++) {
    1393      376296 :                 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
    1394        3730 :                     additional_sinusoid_present = 1;
    1395        3730 :                     break;
    1396             :                 }
    1397             :             }
    1398      171916 :             memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
    1399      171916 :                    (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
    1400             :         }
    1401             :     }
    1402             : 
    1403       10418 :     memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
    1404       10418 :     return 0;
    1405             : }
    1406             : 
    1407             : /// Estimation of current envelope (14496-3 sp04 p218)
    1408       10418 : static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2],
    1409             :                              SpectralBandReplication *sbr, SBRData *ch_data)
    1410             : {
    1411             :     int e, m;
    1412       10418 :     int kx1 = sbr->kx[1];
    1413             : 
    1414       10418 :     if (sbr->bs_interpol_freq) {
    1415       25578 :         for (e = 0; e < ch_data->bs_num_env; e++) {
    1416             : #if USE_FIXED
    1417        5588 :             const SoftFloat recip_env_size = av_int2sf(0x20000000 / (ch_data->t_env[e + 1] - ch_data->t_env[e]), 30);
    1418             : #else
    1419        9572 :             const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
    1420             : #endif /* USE_FIXED */
    1421       15160 :             int ilb = ch_data->t_env[e]     * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
    1422       15160 :             int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
    1423             : 
    1424      394182 :             for (m = 0; m < sbr->m[1]; m++) {
    1425      379022 :                 AAC_FLOAT sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
    1426             : #if USE_FIXED
    1427      127837 :                 e_curr[e][m] = av_mul_sf(sum, recip_env_size);
    1428             : #else
    1429      251185 :                 e_curr[e][m] = sum * recip_env_size;
    1430             : #endif /* USE_FIXED */
    1431             :             }
    1432             :         }
    1433             :     } else {
    1434             :         int k, p;
    1435             : 
    1436           0 :         for (e = 0; e < ch_data->bs_num_env; e++) {
    1437           0 :             const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
    1438           0 :             int ilb = ch_data->t_env[e]     * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
    1439           0 :             int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
    1440           0 :             const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
    1441             : 
    1442           0 :             for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
    1443             : #if USE_FIXED
    1444           0 :                 SoftFloat sum = FLOAT_0;
    1445           0 :                 const SoftFloat den = av_int2sf(0x20000000 / (env_size * (table[p + 1] - table[p])), 29);
    1446           0 :                 for (k = table[p]; k < table[p + 1]; k++) {
    1447           0 :                     sum = av_add_sf(sum, sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb));
    1448             :                 }
    1449           0 :                 sum = av_mul_sf(sum, den);
    1450             : #else
    1451           0 :                 float sum = 0.0f;
    1452           0 :                 const int den = env_size * (table[p + 1] - table[p]);
    1453             : 
    1454           0 :                 for (k = table[p]; k < table[p + 1]; k++) {
    1455           0 :                     sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
    1456             :                 }
    1457           0 :                 sum /= den;
    1458             : #endif /* USE_FIXED */
    1459           0 :                 for (k = table[p]; k < table[p + 1]; k++) {
    1460           0 :                     e_curr[e][k - kx1] = sum;
    1461             :                 }
    1462             :             }
    1463             :         }
    1464             :     }
    1465       10418 : }
    1466             : 
    1467       12411 : void AAC_RENAME(ff_sbr_apply)(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
    1468             :                   INTFLOAT* L, INTFLOAT* R)
    1469             : {
    1470       12411 :     int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
    1471             :     int ch;
    1472       12411 :     int nch = (id_aac == TYPE_CPE) ? 2 : 1;
    1473             :     int err;
    1474             : 
    1475       12411 :     if (id_aac != sbr->id_aac) {
    1476           0 :         av_log(ac->avctx, id_aac == TYPE_LFE ? AV_LOG_VERBOSE : AV_LOG_WARNING,
    1477             :             "element type mismatch %d != %d\n", id_aac, sbr->id_aac);
    1478           0 :         sbr_turnoff(sbr);
    1479             :     }
    1480             : 
    1481       12411 :     if (sbr->start && !sbr->ready_for_dequant) {
    1482           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1483             :                "No quantized data read for sbr_dequant.\n");
    1484           0 :         sbr_turnoff(sbr);
    1485             :     }
    1486             : 
    1487       12411 :     if (!sbr->kx_and_m_pushed) {
    1488        1510 :         sbr->kx[0] = sbr->kx[1];
    1489        1510 :         sbr->m[0] = sbr->m[1];
    1490             :     } else {
    1491       10901 :         sbr->kx_and_m_pushed = 0;
    1492             :     }
    1493             : 
    1494       12411 :     if (sbr->start) {
    1495        6532 :         sbr_dequant(sbr, id_aac);
    1496        6532 :         sbr->ready_for_dequant = 0;
    1497             :     }
    1498       32148 :     for (ch = 0; ch < nch; ch++) {
    1499             :         /* decode channel */
    1500       39474 :         sbr_qmf_analysis(ac->fdsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
    1501       19737 :                          (INTFLOAT*)sbr->qmf_filter_scratch,
    1502       19737 :                          sbr->data[ch].W, sbr->data[ch].Ypos);
    1503       39474 :         sbr->c.sbr_lf_gen(ac, sbr, sbr->X_low,
    1504       19737 :                           (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
    1505             :                           sbr->data[ch].Ypos);
    1506       19737 :         sbr->data[ch].Ypos ^= 1;
    1507       19737 :         if (sbr->start) {
    1508       27467 :             sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
    1509       17049 :                                          (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
    1510       10418 :             sbr_chirp(sbr, &sbr->data[ch]);
    1511       10418 :             av_assert0(sbr->data[ch].bs_num_env > 0);
    1512       20836 :             sbr_hf_gen(ac, sbr, sbr->X_high,
    1513       10418 :                        (const INTFLOAT (*)[40][2]) sbr->X_low,
    1514       10418 :                        (const INTFLOAT (*)[2]) sbr->alpha0,
    1515       10418 :                        (const INTFLOAT (*)[2]) sbr->alpha1,
    1516       10418 :                        sbr->data[ch].bw_array, sbr->data[ch].t_env,
    1517        6631 :                        sbr->data[ch].bs_num_env);
    1518             : 
    1519             :             // hf_adj
    1520       10418 :             err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
    1521       10418 :             if (!err) {
    1522       10418 :                 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
    1523       10418 :                 sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
    1524       31254 :                 sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
    1525       10418 :                                 (const INTFLOAT (*)[40][2]) sbr->X_high,
    1526             :                                 sbr, &sbr->data[ch],
    1527       10418 :                                 sbr->data[ch].e_a);
    1528             :             }
    1529             :         }
    1530             : 
    1531             :         /* synthesis */
    1532       39474 :         sbr->c.sbr_x_gen(sbr, sbr->X[ch],
    1533       19737 :                   (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
    1534       19737 :                   (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[  sbr->data[ch].Ypos],
    1535       19737 :                   (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
    1536             :     }
    1537             : 
    1538       12411 :     if (ac->oc[1].m4ac.ps == 1) {
    1539        1999 :         if (sbr->ps.start) {
    1540        1616 :             AAC_RENAME(ff_ps_apply)(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
    1541             :         } else {
    1542         383 :             memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
    1543             :         }
    1544        1999 :         nch = 2;
    1545             :     }
    1546             : 
    1547       24822 :     sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
    1548       12411 :                       L, sbr->X[0], sbr->qmf_filter_scratch,
    1549       12411 :                       sbr->data[0].synthesis_filterbank_samples,
    1550             :                       &sbr->data[0].synthesis_filterbank_samples_offset,
    1551             :                       downsampled);
    1552       12411 :     if (nch == 2)
    1553       18650 :         sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
    1554        9325 :                           R, sbr->X[1], sbr->qmf_filter_scratch,
    1555        9325 :                           sbr->data[1].synthesis_filterbank_samples,
    1556             :                           &sbr->data[1].synthesis_filterbank_samples_offset,
    1557             :                           downsampled);
    1558       12411 : }
    1559             : 
    1560         343 : static void aacsbr_func_ptr_init(AACSBRContext *c)
    1561             : {
    1562         343 :     c->sbr_lf_gen            = sbr_lf_gen;
    1563         343 :     c->sbr_hf_assemble       = sbr_hf_assemble;
    1564         343 :     c->sbr_x_gen             = sbr_x_gen;
    1565         343 :     c->sbr_hf_inverse_filter = sbr_hf_inverse_filter;
    1566             : 
    1567             : #if !USE_FIXED
    1568             :     if(ARCH_MIPS)
    1569             :         ff_aacsbr_func_ptr_init_mips(c);
    1570             : #endif
    1571         343 : }

Generated by: LCOV version 1.13