LCOV - code coverage report
Current view: top level - src/libavcodec - aacsbr_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 756 902 83.8 %
Date: 2017-01-24 04:42:20 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         151 : 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         151 :     SBR_INIT_VLC_STATIC(0, 1098);
      57         151 :     SBR_INIT_VLC_STATIC(1, 1092);
      58         151 :     SBR_INIT_VLC_STATIC(2, 768);
      59         151 :     SBR_INIT_VLC_STATIC(3, 1026);
      60         151 :     SBR_INIT_VLC_STATIC(4, 1058);
      61         151 :     SBR_INIT_VLC_STATIC(5, 1052);
      62         151 :     SBR_INIT_VLC_STATIC(6, 544);
      63         151 :     SBR_INIT_VLC_STATIC(7, 544);
      64         151 :     SBR_INIT_VLC_STATIC(8, 592);
      65         151 :     SBR_INIT_VLC_STATIC(9, 512);
      66             : 
      67         151 :     aacsbr_tableinit();
      68             : 
      69         151 :     AAC_RENAME(ff_ps_init)();
      70         151 : }
      71             : 
      72             : /** Places SBR in pure upsampling mode. */
      73        5279 : static void sbr_turnoff(SpectralBandReplication *sbr) {
      74        5279 :     sbr->start = 0;
      75        5279 :     sbr->ready_for_dequant = 0;
      76             :     // Init defults used in pure upsampling mode
      77        5279 :     sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
      78        5279 :     sbr->m[1] = 0;
      79             :     // Reset values for first SBR header
      80        5279 :     sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
      81        5279 :     memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
      82        5279 : }
      83             : 
      84         329 : av_cold void AAC_RENAME(ff_aac_sbr_ctx_init)(AACContext *ac, SpectralBandReplication *sbr)
      85             : {
      86         329 :     if(sbr->mdct.mdct_bits)
      87           0 :         return;
      88         329 :     sbr->kx[0] = sbr->kx[1];
      89         329 :     sbr_turnoff(sbr);
      90         329 :     sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
      91         329 :     sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
      92             :     /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
      93             :      * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
      94             :      * and scale back down at synthesis. */
      95         329 :     AAC_RENAME_32(ff_mdct_init)(&sbr->mdct,     7, 1, 1.0 / (64 * 32768.0));
      96         329 :     AAC_RENAME_32(ff_mdct_init)(&sbr->mdct_ana, 7, 1, -2.0 * 32768.0);
      97         329 :     AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
      98         329 :     AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
      99         329 :     aacsbr_func_ptr_init(&sbr->c);
     100             : }
     101             : 
     102         329 : av_cold void AAC_RENAME(ff_aac_sbr_ctx_close)(SpectralBandReplication *sbr)
     103             : {
     104         329 :     AAC_RENAME_32(ff_mdct_end)(&sbr->mdct);
     105         329 :     AAC_RENAME_32(ff_mdct_end)(&sbr->mdct_ana);
     106         329 : }
     107             : 
     108        6070 : static int qsort_comparison_function_int16(const void *a, const void *b)
     109             : {
     110        6070 :     return *(const int16_t *)a - *(const int16_t *)b;
     111             : }
     112             : 
     113         379 : static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
     114             : {
     115             :     int i;
     116        1768 :     for (i = 0; i <= last_el; i++)
     117        1529 :         if (table[i] == needle)
     118         140 :             return 1;
     119         239 :     return 0;
     120             : }
     121             : 
     122             : /// Limiter Frequency Band Table (14496-3 sp04 p198)
     123          64 : static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
     124             : {
     125             :     int k;
     126          64 :     if (sbr->bs_limiter_bands > 0) {
     127             :         static const INTFLOAT bands_warped[3] = { Q23(1.32715174233856803909f),   //2^(0.49/1.2)
     128             :                                                Q23(1.18509277094158210129f),   //2^(0.49/2)
     129             :                                                Q23(1.11987160404675912501f) }; //2^(0.49/3)
     130          64 :         const INTFLOAT lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
     131             :         int16_t patch_borders[7];
     132          64 :         uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
     133             : 
     134          64 :         patch_borders[0] = sbr->kx[1];
     135         249 :         for (k = 1; k <= sbr->num_patches; k++)
     136         185 :             patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
     137             : 
     138          64 :         memcpy(sbr->f_tablelim, sbr->f_tablelow,
     139          64 :                (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
     140          64 :         if (sbr->num_patches > 1)
     141          63 :             memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
     142          63 :                    (sbr->num_patches - 1) * sizeof(patch_borders[0]));
     143             : 
     144          64 :         AV_QSORT(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
     145             :               uint16_t,
     146             :               qsort_comparison_function_int16);
     147             : 
     148          64 :         sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
     149         668 :         while (out < sbr->f_tablelim + sbr->n_lim) {
     150             : #if USE_FIXED
     151          35 :             if ((*in << 23) >= *out * lim_bands_per_octave_warped) {
     152             : #else
     153         505 :             if (*in >= *out * lim_bands_per_octave_warped) {
     154             : #endif /* USE_FIXED */
     155         212 :                 *++out = *in++;
     156         610 :             } else if (*in == *out ||
     157         282 :                 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
     158         231 :                 in++;
     159         231 :                 sbr->n_lim--;
     160          97 :             } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
     161          54 :                 *out = *in++;
     162          54 :                 sbr->n_lim--;
     163             :             } else {
     164          43 :                 *++out = *in++;
     165             :             }
     166             :         }
     167             :     } else {
     168           0 :         sbr->f_tablelim[0] = sbr->f_tablelow[0];
     169           0 :         sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
     170           0 :         sbr->n_lim = 1;
     171             :     }
     172          64 : }
     173             : 
     174         485 : static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
     175             : {
     176         485 :     unsigned int cnt = get_bits_count(gb);
     177             :     uint8_t bs_header_extra_1;
     178             :     uint8_t bs_header_extra_2;
     179         485 :     int old_bs_limiter_bands = sbr->bs_limiter_bands;
     180             :     SpectrumParameters old_spectrum_params;
     181             : 
     182         485 :     sbr->start = 1;
     183         485 :     sbr->ready_for_dequant = 0;
     184             : 
     185             :     // Save last spectrum parameters variables to compare to new ones
     186         485 :     memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
     187             : 
     188         485 :     sbr->bs_amp_res_header              = get_bits1(gb);
     189         485 :     sbr->spectrum_params.bs_start_freq  = get_bits(gb, 4);
     190         485 :     sbr->spectrum_params.bs_stop_freq   = get_bits(gb, 4);
     191         485 :     sbr->spectrum_params.bs_xover_band  = get_bits(gb, 3);
     192         485 :                                           skip_bits(gb, 2); // bs_reserved
     193             : 
     194         485 :     bs_header_extra_1 = get_bits1(gb);
     195         485 :     bs_header_extra_2 = get_bits1(gb);
     196             : 
     197         485 :     if (bs_header_extra_1) {
     198         430 :         sbr->spectrum_params.bs_freq_scale  = get_bits(gb, 2);
     199         430 :         sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
     200         430 :         sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
     201             :     } else {
     202          55 :         sbr->spectrum_params.bs_freq_scale  = 2;
     203          55 :         sbr->spectrum_params.bs_alter_scale = 1;
     204          55 :         sbr->spectrum_params.bs_noise_bands = 2;
     205             :     }
     206             : 
     207             :     // Check if spectrum parameters changed
     208         485 :     if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
     209          64 :         sbr->reset = 1;
     210             : 
     211         485 :     if (bs_header_extra_2) {
     212          26 :         sbr->bs_limiter_bands  = get_bits(gb, 2);
     213          26 :         sbr->bs_limiter_gains  = get_bits(gb, 2);
     214          26 :         sbr->bs_interpol_freq  = get_bits1(gb);
     215          26 :         sbr->bs_smoothing_mode = get_bits1(gb);
     216             :     } else {
     217         459 :         sbr->bs_limiter_bands  = 2;
     218         459 :         sbr->bs_limiter_gains  = 2;
     219         459 :         sbr->bs_interpol_freq  = 1;
     220         459 :         sbr->bs_smoothing_mode = 1;
     221             :     }
     222             : 
     223         485 :     if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
     224           0 :         sbr_make_f_tablelim(sbr);
     225             : 
     226         485 :     return get_bits_count(gb) - cnt;
     227             : }
     228             : 
     229          12 : static int array_min_int16(const int16_t *array, int nel)
     230             : {
     231          12 :     int i, min = array[0];
     232          28 :     for (i = 1; i < nel; i++)
     233          16 :         min = FFMIN(array[i], min);
     234          12 :     return min;
     235             : }
     236             : 
     237          64 : static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
     238             : {
     239             :     // Requirements (14496-3 sp04 p205)
     240          64 :     if (n_master <= 0) {
     241           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
     242           0 :         return -1;
     243             :     }
     244          64 :     if (bs_xover_band >= n_master) {
     245           0 :         av_log(avctx, AV_LOG_ERROR,
     246             :                "Invalid bitstream, crossover band index beyond array bounds: %d\n",
     247             :                bs_xover_band);
     248           0 :         return -1;
     249             :     }
     250          64 :     return 0;
     251             : }
     252             : 
     253             : /// Master Frequency Band Table (14496-3 sp04 p194)
     254          64 : static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
     255             :                              SpectrumParameters *spectrum)
     256             : {
     257          64 :     unsigned int temp, max_qmf_subbands = 0;
     258             :     unsigned int start_min, stop_min;
     259             :     int k;
     260             :     const int8_t *sbr_offset_ptr;
     261             :     int16_t stop_dk[13];
     262             : 
     263          64 :     if (sbr->sample_rate < 32000) {
     264           6 :         temp = 3000;
     265          58 :     } else if (sbr->sample_rate < 64000) {
     266          56 :         temp = 4000;
     267             :     } else
     268           2 :         temp = 5000;
     269             : 
     270          64 :     switch (sbr->sample_rate) {
     271             :     case 16000:
     272           6 :         sbr_offset_ptr = sbr_offset[0];
     273           6 :         break;
     274             :     case 22050:
     275           0 :         sbr_offset_ptr = sbr_offset[1];
     276           0 :         break;
     277             :     case 24000:
     278           0 :         sbr_offset_ptr = sbr_offset[2];
     279           0 :         break;
     280             :     case 32000:
     281          11 :         sbr_offset_ptr = sbr_offset[3];
     282          11 :         break;
     283             :     case 44100: case 48000: case 64000:
     284          45 :         sbr_offset_ptr = sbr_offset[4];
     285          45 :         break;
     286             :     case 88200: case 96000: case 128000: case 176400: case 192000:
     287           2 :         sbr_offset_ptr = sbr_offset[5];
     288           2 :         break;
     289             :     default:
     290           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     291             :                "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
     292           0 :         return -1;
     293             :     }
     294             : 
     295          64 :     start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
     296          64 :     stop_min  = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
     297             : 
     298          64 :     sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
     299             : 
     300          64 :     if (spectrum->bs_stop_freq < 14) {
     301          64 :         sbr->k[2] = stop_min;
     302          64 :         make_bands(stop_dk, stop_min, 64, 13);
     303          64 :         AV_QSORT(stop_dk, 13, int16_t, qsort_comparison_function_int16);
     304         722 :         for (k = 0; k < spectrum->bs_stop_freq; k++)
     305         658 :             sbr->k[2] += stop_dk[k];
     306           0 :     } else if (spectrum->bs_stop_freq == 14) {
     307           0 :         sbr->k[2] = 2*sbr->k[0];
     308           0 :     } else if (spectrum->bs_stop_freq == 15) {
     309           0 :         sbr->k[2] = 3*sbr->k[0];
     310             :     } else {
     311           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     312           0 :                "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
     313           0 :         return -1;
     314             :     }
     315          64 :     sbr->k[2] = FFMIN(64, sbr->k[2]);
     316             : 
     317             :     // Requirements (14496-3 sp04 p205)
     318          64 :     if (sbr->sample_rate <= 32000) {
     319          17 :         max_qmf_subbands = 48;
     320          47 :     } else if (sbr->sample_rate == 44100) {
     321          36 :         max_qmf_subbands = 35;
     322          11 :     } else if (sbr->sample_rate >= 48000)
     323          11 :         max_qmf_subbands = 32;
     324             :     else
     325           0 :         av_assert0(0);
     326             : 
     327          64 :     if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
     328           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     329           0 :                "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
     330           0 :         return -1;
     331             :     }
     332             : 
     333          64 :     if (!spectrum->bs_freq_scale) {
     334             :         int dk, k2diff;
     335             : 
     336          14 :         dk = spectrum->bs_alter_scale + 1;
     337          14 :         sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
     338          14 :         if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
     339           0 :             return -1;
     340             : 
     341         266 :         for (k = 1; k <= sbr->n_master; k++)
     342         252 :             sbr->f_master[k] = dk;
     343             : 
     344          14 :         k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
     345          14 :         if (k2diff < 0) {
     346          14 :             sbr->f_master[1]--;
     347          14 :             sbr->f_master[2]-= (k2diff < -1);
     348           0 :         } else if (k2diff) {
     349           0 :             sbr->f_master[sbr->n_master]++;
     350             :         }
     351             : 
     352          14 :         sbr->f_master[0] = sbr->k[0];
     353         266 :         for (k = 1; k <= sbr->n_master; k++)
     354         252 :             sbr->f_master[k] += sbr->f_master[k - 1];
     355             : 
     356             :     } else {
     357          50 :         int half_bands = 7 - spectrum->bs_freq_scale;      // bs_freq_scale  = {1,2,3}
     358             :         int two_regions, num_bands_0;
     359             :         int vdk0_max, vdk1_min;
     360             :         int16_t vk0[49];
     361             : #if USE_FIXED
     362           5 :         int tmp, nz = 0;
     363             : #endif /* USE_FIXED */
     364             : 
     365          50 :         if (49 * sbr->k[2] > 110 * sbr->k[0]) {
     366          12 :             two_regions = 1;
     367          12 :             sbr->k[1] = 2 * sbr->k[0];
     368             :         } else {
     369          38 :             two_regions = 0;
     370          38 :             sbr->k[1] = sbr->k[2];
     371             :         }
     372             : 
     373             : #if USE_FIXED
     374           5 :         tmp = (sbr->k[1] << 23) / sbr->k[0];
     375          40 :         while (tmp < 0x40000000) {
     376          30 :           tmp <<= 1;
     377          30 :           nz++;
     378             :         }
     379           5 :         tmp = fixed_log(tmp - 0x80000000);
     380           5 :         tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
     381           5 :         tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
     382           5 :         num_bands_0 = ((tmp + 0x400000) >> 23) * 2;
     383             : #else
     384          45 :         num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
     385             : #endif /* USE_FIXED */
     386             : 
     387          50 :         if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
     388           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
     389           0 :             return -1;
     390             :         }
     391             : 
     392          50 :         vk0[0] = 0;
     393             : 
     394          50 :         make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
     395             : 
     396          50 :         AV_QSORT(vk0 + 1, num_bands_0, int16_t, qsort_comparison_function_int16);
     397          50 :         vdk0_max = vk0[num_bands_0];
     398             : 
     399          50 :         vk0[0] = sbr->k[0];
     400         608 :         for (k = 1; k <= num_bands_0; k++) {
     401         558 :             if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
     402           0 :                 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
     403           0 :                 return -1;
     404             :             }
     405         558 :             vk0[k] += vk0[k-1];
     406             :         }
     407             : 
     408          50 :         if (two_regions) {
     409             :             int16_t vk1[49];
     410             : #if USE_FIXED
     411             :             int num_bands_1;
     412             : 
     413           2 :             tmp = (sbr->k[2] << 23) / sbr->k[1];
     414           2 :             nz = 0;
     415          18 :             while (tmp < 0x40000000) {
     416          14 :               tmp <<= 1;
     417          14 :               nz++;
     418             :             }
     419           2 :             tmp = fixed_log(tmp - 0x80000000);
     420           2 :             tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
     421           2 :             tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
     422           2 :             if (spectrum->bs_alter_scale)
     423           2 :                 tmp = (int)(((int64_t)tmp * CONST_076923 + 0x40000000) >> 31);
     424           2 :             num_bands_1 = ((tmp + 0x400000) >> 23) * 2;
     425             : #else
     426          20 :             float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
     427          10 :                                                      : 1.0f; // bs_alter_scale = {0,1}
     428          20 :             int num_bands_1 = lrintf(half_bands * invwarp *
     429          10 :                                      log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
     430             : #endif /* USE_FIXED */
     431          12 :             make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
     432             : 
     433          12 :             vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
     434             : 
     435          12 :             if (vdk1_min < vdk0_max) {
     436             :                 int change;
     437           0 :                 AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
     438           0 :                 change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
     439           0 :                 vk1[1]           += change;
     440           0 :                 vk1[num_bands_1] -= change;
     441             :             }
     442             : 
     443          12 :             AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
     444             : 
     445          12 :             vk1[0] = sbr->k[1];
     446          40 :             for (k = 1; k <= num_bands_1; k++) {
     447          28 :                 if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
     448           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
     449           0 :                     return -1;
     450             :                 }
     451          28 :                 vk1[k] += vk1[k-1];
     452             :             }
     453             : 
     454          12 :             sbr->n_master = num_bands_0 + num_bands_1;
     455          12 :             if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
     456           0 :                 return -1;
     457          12 :             memcpy(&sbr->f_master[0],               vk0,
     458          12 :                    (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
     459          12 :             memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
     460             :                     num_bands_1      * sizeof(sbr->f_master[0]));
     461             : 
     462             :         } else {
     463          38 :             sbr->n_master = num_bands_0;
     464          38 :             if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
     465           0 :                 return -1;
     466          38 :             memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
     467             :         }
     468             :     }
     469             : 
     470          64 :     return 0;
     471             : }
     472             : 
     473             : /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
     474          64 : static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
     475             : {
     476          64 :     int i, k, last_k = -1, last_msb = -1, sb = 0;
     477          64 :     int msb = sbr->k[0];
     478          64 :     int usb = sbr->kx[1];
     479          64 :     int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
     480             : 
     481          64 :     sbr->num_patches = 0;
     482             : 
     483          64 :     if (goal_sb < sbr->kx[1] + sbr->m[1]) {
     484          31 :         for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
     485             :     } else
     486          33 :         k = sbr->n_master;
     487             : 
     488             :     do {
     489         185 :         int odd = 0;
     490         185 :         if (k == last_k && msb == last_msb) {
     491           0 :             av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
     492           0 :             return AVERROR_INVALIDDATA;
     493             :         }
     494         185 :         last_k = k;
     495         185 :         last_msb = msb;
     496        1058 :         for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
     497         873 :             sb = sbr->f_master[i];
     498         873 :             odd = (sb + sbr->k[0]) & 1;
     499             :         }
     500             : 
     501             :         // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
     502             :         // After this check the final number of patches can still be six which is
     503             :         // illegal however the Coding Technologies decoder check stream has a final
     504             :         // count of 6 patches
     505         185 :         if (sbr->num_patches > 5) {
     506           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
     507           0 :             return -1;
     508             :         }
     509             : 
     510         185 :         sbr->patch_num_subbands[sbr->num_patches]  = FFMAX(sb - usb, 0);
     511         185 :         sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
     512             : 
     513         185 :         if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
     514         185 :             usb = sb;
     515         185 :             msb = sb;
     516         185 :             sbr->num_patches++;
     517             :         } else
     518           0 :             msb = sbr->kx[1];
     519             : 
     520         185 :         if (sbr->f_master[k] - sb < 3)
     521          85 :             k = sbr->n_master;
     522         185 :     } while (sb != sbr->kx[1] + sbr->m[1]);
     523             : 
     524         127 :     if (sbr->num_patches > 1 &&
     525          63 :         sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
     526           0 :         sbr->num_patches--;
     527             : 
     528          64 :     return 0;
     529             : }
     530             : 
     531             : /// Derived Frequency Band Tables (14496-3 sp04 p197)
     532          64 : static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
     533             : {
     534             :     int k, temp;
     535             : #if USE_FIXED
     536           5 :     int nz = 0;
     537             : #endif /* USE_FIXED */
     538             : 
     539          64 :     sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
     540          64 :     sbr->n[0] = (sbr->n[1] + 1) >> 1;
     541             : 
     542          64 :     memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
     543          64 :            (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
     544          64 :     sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
     545          64 :     sbr->kx[1] = sbr->f_tablehigh[0];
     546             : 
     547             :     // Requirements (14496-3 sp04 p205)
     548          64 :     if (sbr->kx[1] + sbr->m[1] > 64) {
     549           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     550           0 :                "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
     551           0 :         return -1;
     552             :     }
     553          64 :     if (sbr->kx[1] > 32) {
     554           0 :         av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
     555           0 :         return -1;
     556             :     }
     557             : 
     558          64 :     sbr->f_tablelow[0] = sbr->f_tablehigh[0];
     559          64 :     temp = sbr->n[1] & 1;
     560         483 :     for (k = 1; k <= sbr->n[0]; k++)
     561         419 :         sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
     562             : #if USE_FIXED
     563           5 :     temp = (sbr->k[2] << 23) / sbr->kx[1];
     564          40 :     while (temp < 0x40000000) {
     565          30 :         temp <<= 1;
     566          30 :         nz++;
     567             :     }
     568           5 :     temp = fixed_log(temp - 0x80000000);
     569           5 :     temp = (int)(((int64_t)temp * CONST_RECIP_LN2 + 0x20000000) >> 30);
     570           5 :     temp = (((temp + 0x80) >> 8) + ((8 - nz) << 23)) * sbr->spectrum_params.bs_noise_bands;
     571             : 
     572           5 :     sbr->n_q = (temp + 0x400000) >> 23;
     573           5 :     if (sbr->n_q < 1)
     574           0 :         sbr->n_q = 1;
     575             : #else
     576          59 :     sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
     577             :                                log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
     578             : #endif /* USE_FIXED */
     579             : 
     580          64 :     if (sbr->n_q > 5) {
     581           0 :         av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
     582           0 :         return -1;
     583             :     }
     584             : 
     585          64 :     sbr->f_tablenoise[0] = sbr->f_tablelow[0];
     586          64 :     temp = 0;
     587         264 :     for (k = 1; k <= sbr->n_q; k++) {
     588         200 :         temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
     589         200 :         sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
     590             :     }
     591             : 
     592          64 :     if (sbr_hf_calc_npatches(ac, sbr) < 0)
     593           0 :         return -1;
     594             : 
     595          64 :     sbr_make_f_tablelim(sbr);
     596             : 
     597          64 :     sbr->data[0].f_indexnoise = 0;
     598          64 :     sbr->data[1].f_indexnoise = 0;
     599             : 
     600          64 :     return 0;
     601             : }
     602             : 
     603       22832 : static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
     604             :                                               int elements)
     605             : {
     606             :     int i;
     607       71265 :     for (i = 0; i < elements; i++) {
     608       48433 :         vec[i] = get_bits1(gb);
     609             :     }
     610       22832 : }
     611             : 
     612             : /** ceil(log2(index+1)) */
     613             : static const int8_t ceil_log2[] = {
     614             :     0, 1, 2, 2, 3, 3,
     615             : };
     616             : 
     617        7740 : static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
     618             :                          GetBitContext *gb, SBRData *ch_data)
     619             : {
     620             :     int i;
     621        7740 :     int bs_pointer = 0;
     622             :     // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
     623        7740 :     int abs_bord_trail = 16;
     624             :     int num_rel_lead, num_rel_trail;
     625        7740 :     unsigned bs_num_env_old = ch_data->bs_num_env;
     626             : 
     627        7740 :     ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
     628        7740 :     ch_data->bs_amp_res = sbr->bs_amp_res_header;
     629        7740 :     ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
     630             : 
     631        7740 :     switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
     632             :     case FIXFIX:
     633        6346 :         ch_data->bs_num_env                 = 1 << get_bits(gb, 2);
     634        6346 :         num_rel_lead                        = ch_data->bs_num_env - 1;
     635        6346 :         if (ch_data->bs_num_env == 1)
     636        4880 :             ch_data->bs_amp_res = 0;
     637             : 
     638        6346 :         if (ch_data->bs_num_env > 4) {
     639           0 :             av_log(ac->avctx, AV_LOG_ERROR,
     640             :                    "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
     641             :                    ch_data->bs_num_env);
     642           0 :             return -1;
     643             :         }
     644             : 
     645        6346 :         ch_data->t_env[0]                   = 0;
     646        6346 :         ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
     647             : 
     648       12692 :         abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
     649        6346 :                    ch_data->bs_num_env;
     650        7812 :         for (i = 0; i < num_rel_lead; i++)
     651        1466 :             ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
     652             : 
     653        6346 :         ch_data->bs_freq_res[1] = get_bits1(gb);
     654        7812 :         for (i = 1; i < ch_data->bs_num_env; i++)
     655        1466 :             ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
     656        6346 :         break;
     657             :     case FIXVAR:
     658         628 :         abs_bord_trail                     += get_bits(gb, 2);
     659         628 :         num_rel_trail                       = get_bits(gb, 2);
     660         628 :         ch_data->bs_num_env                 = num_rel_trail + 1;
     661         628 :         ch_data->t_env[0]                   = 0;
     662         628 :         ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
     663             : 
     664        1986 :         for (i = 0; i < num_rel_trail; i++)
     665        2716 :             ch_data->t_env[ch_data->bs_num_env - 1 - i] =
     666        1358 :                 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
     667             : 
     668         628 :         bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
     669             : 
     670        2614 :         for (i = 0; i < ch_data->bs_num_env; i++)
     671        1986 :             ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
     672         628 :         break;
     673             :     case VARFIX:
     674         618 :         ch_data->t_env[0]                   = get_bits(gb, 2);
     675         618 :         num_rel_lead                        = get_bits(gb, 2);
     676         618 :         ch_data->bs_num_env                 = num_rel_lead + 1;
     677         618 :         ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
     678             : 
     679        1391 :         for (i = 0; i < num_rel_lead; i++)
     680         773 :             ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
     681             : 
     682         618 :         bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
     683             : 
     684         618 :         get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
     685         618 :         break;
     686             :     case VARVAR:
     687         148 :         ch_data->t_env[0]                   = get_bits(gb, 2);
     688         148 :         abs_bord_trail                     += get_bits(gb, 2);
     689         148 :         num_rel_lead                        = get_bits(gb, 2);
     690         148 :         num_rel_trail                       = get_bits(gb, 2);
     691         148 :         ch_data->bs_num_env                 = num_rel_lead + num_rel_trail + 1;
     692             : 
     693         148 :         if (ch_data->bs_num_env > 5) {
     694           0 :             av_log(ac->avctx, AV_LOG_ERROR,
     695             :                    "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
     696             :                    ch_data->bs_num_env);
     697           0 :             return -1;
     698             :         }
     699             : 
     700         148 :         ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
     701             : 
     702         220 :         for (i = 0; i < num_rel_lead; i++)
     703          72 :             ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
     704         439 :         for (i = 0; i < num_rel_trail; i++)
     705         582 :             ch_data->t_env[ch_data->bs_num_env - 1 - i] =
     706         291 :                 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
     707             : 
     708         148 :         bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
     709             : 
     710         148 :         get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
     711         148 :         break;
     712             :     }
     713             : 
     714        7740 :     av_assert0(bs_pointer >= 0);
     715        7740 :     if (bs_pointer > ch_data->bs_num_env + 1) {
     716           0 :         av_log(ac->avctx, AV_LOG_ERROR,
     717             :                "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
     718             :                bs_pointer);
     719           0 :         return -1;
     720             :     }
     721             : 
     722       19440 :     for (i = 1; i <= ch_data->bs_num_env; i++) {
     723       11700 :         if (ch_data->t_env[i-1] >= ch_data->t_env[i]) {
     724           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Not strictly monotone time borders\n");
     725           0 :             return -1;
     726             :         }
     727             :     }
     728             : 
     729        7740 :     ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
     730             : 
     731        7740 :     ch_data->t_q[0]                     = ch_data->t_env[0];
     732        7740 :     ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
     733        7740 :     if (ch_data->bs_num_noise > 1) {
     734             :         int idx;
     735        2860 :         if (ch_data->bs_frame_class == FIXFIX) {
     736        1466 :             idx = ch_data->bs_num_env >> 1;
     737        1394 :         } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
     738         776 :             idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
     739             :         } else { // VARFIX
     740         618 :             if (!bs_pointer)
     741         463 :                 idx = 1;
     742         155 :             else if (bs_pointer == 1)
     743         155 :                 idx = ch_data->bs_num_env - 1;
     744             :             else // bs_pointer > 1
     745           0 :                 idx = bs_pointer - 1;
     746             :         }
     747        2860 :         ch_data->t_q[1] = ch_data->t_env[idx];
     748             :     }
     749             : 
     750        7740 :     ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
     751        7740 :     ch_data->e_a[1] = -1;
     752        7740 :     if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
     753         776 :         ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
     754        6964 :     } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
     755           0 :         ch_data->e_a[1] = bs_pointer - 1;
     756             : 
     757        7740 :     return 0;
     758             : }
     759             : 
     760        2625 : static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
     761             :     //These variables are saved from the previous frame rather than copied
     762        2625 :     dst->bs_freq_res[0]    = dst->bs_freq_res[dst->bs_num_env];
     763        2625 :     dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
     764        2625 :     dst->e_a[0]            = -(dst->e_a[1] != dst->bs_num_env);
     765             : 
     766             :     //These variables are read from the bitstream and therefore copied
     767        2625 :     memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
     768        2625 :     memcpy(dst->t_env,         src->t_env,         sizeof(dst->t_env));
     769        2625 :     memcpy(dst->t_q,           src->t_q,           sizeof(dst->t_q));
     770        2625 :     dst->bs_num_env        = src->bs_num_env;
     771        2625 :     dst->bs_amp_res        = src->bs_amp_res;
     772        2625 :     dst->bs_num_noise      = src->bs_num_noise;
     773        2625 :     dst->bs_frame_class    = src->bs_frame_class;
     774        2625 :     dst->e_a[1]            = src->e_a[1];
     775        2625 : }
     776             : 
     777             : /// Read how the envelope and noise floor data is delta coded
     778       10365 : static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
     779             :                           SBRData *ch_data)
     780             : {
     781       10365 :     get_bits1_vector(gb, ch_data->bs_df_env,   ch_data->bs_num_env);
     782       10365 :     get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
     783       10365 : }
     784             : 
     785             : /// Read inverse filtering data
     786        7740 : static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
     787             :                           SBRData *ch_data)
     788             : {
     789             :     int i;
     790             : 
     791        7740 :     memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
     792       31672 :     for (i = 0; i < sbr->n_q; i++)
     793       23932 :         ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
     794        7740 : }
     795             : 
     796       10365 : static int read_sbr_envelope(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
     797             :                               SBRData *ch_data, int ch)
     798             : {
     799             :     int bits;
     800             :     int i, j, k;
     801             :     VLC_TYPE (*t_huff)[2], (*f_huff)[2];
     802             :     int t_lav, f_lav;
     803       10365 :     const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
     804       10365 :     const int odd = sbr->n[1] & 1;
     805             : 
     806       10365 :     if (sbr->bs_coupling && ch) {
     807        5250 :         if (ch_data->bs_amp_res) {
     808         582 :             bits   = 5;
     809         582 :             t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
     810         582 :             t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];
     811         582 :             f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
     812         582 :             f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
     813             :         } else {
     814        2043 :             bits   = 6;
     815        2043 :             t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
     816        2043 :             t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];
     817        2043 :             f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
     818        2043 :             f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];
     819             :         }
     820             :     } else {
     821        7740 :         if (ch_data->bs_amp_res) {
     822        2860 :             bits   = 6;
     823        2860 :             t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
     824        2860 :             t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];
     825        2860 :             f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
     826        2860 :             f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
     827             :         } else {
     828        4880 :             bits   = 7;
     829        4880 :             t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
     830        4880 :             t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];
     831        4880 :             f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
     832        4880 :             f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];
     833             :         }
     834             :     }
     835             : 
     836       25465 :     for (i = 0; i < ch_data->bs_num_env; i++) {
     837       15100 :         if (ch_data->bs_df_env[i]) {
     838             :             // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
     839        4905 :             if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
     840       55617 :                 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
     841       51203 :                     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);
     842       51203 :                     if (ch_data->env_facs_q[i + 1][j] > 127U) {
     843           0 :                         av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
     844           0 :                         return AVERROR_INVALIDDATA;
     845             :                     }
     846             :                 }
     847         491 :             } else if (ch_data->bs_freq_res[i + 1]) {
     848        4652 :                 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
     849        4280 :                     k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
     850        4280 :                     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);
     851        4280 :                     if (ch_data->env_facs_q[i + 1][j] > 127U) {
     852           0 :                         av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
     853           0 :                         return AVERROR_INVALIDDATA;
     854             :                     }
     855             :                 }
     856             :             } else {
     857         797 :                 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
     858         678 :                     k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
     859         678 :                     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);
     860         678 :                     if (ch_data->env_facs_q[i + 1][j] > 127U) {
     861           0 :                         av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
     862           0 :                         return AVERROR_INVALIDDATA;
     863             :                     }
     864             :                 }
     865             :             }
     866             :         } else {
     867       10195 :             ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
     868      115639 :             for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
     869      105444 :                 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);
     870      105444 :                 if (ch_data->env_facs_q[i + 1][j] > 127U) {
     871           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
     872           0 :                     return AVERROR_INVALIDDATA;
     873             :                 }
     874             :             }
     875             :         }
     876             :     }
     877             : 
     878             :     //assign 0th elements of env_facs_q from last elements
     879       10365 :     memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],
     880             :            sizeof(ch_data->env_facs_q[0]));
     881             : 
     882       10365 :     return 0;
     883             : }
     884             : 
     885       10365 : static int read_sbr_noise(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
     886             :                            SBRData *ch_data, int ch)
     887             : {
     888             :     int i, j;
     889             :     VLC_TYPE (*t_huff)[2], (*f_huff)[2];
     890             :     int t_lav, f_lav;
     891       10365 :     int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
     892             : 
     893       10365 :     if (sbr->bs_coupling && ch) {
     894        2625 :         t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
     895        2625 :         t_lav  = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB];
     896        2625 :         f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
     897        2625 :         f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
     898             :     } else {
     899        7740 :         t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
     900        7740 :         t_lav  = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB];
     901        7740 :         f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
     902        7740 :         f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
     903             :     }
     904             : 
     905       24172 :     for (i = 0; i < ch_data->bs_num_noise; i++) {
     906       13807 :         if (ch_data->bs_df_noise[i]) {
     907       43597 :             for (j = 0; j < sbr->n_q; j++) {
     908       32815 :                 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);
     909       32815 :                 if (ch_data->noise_facs_q[i + 1][j] > 30U) {
     910           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
     911           0 :                     return AVERROR_INVALIDDATA;
     912             :                 }
     913             :             }
     914             :         } else {
     915        3025 :             ch_data->noise_facs_q[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
     916        8935 :             for (j = 1; j < sbr->n_q; j++) {
     917        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);
     918        5910 :                 if (ch_data->noise_facs_q[i + 1][j] > 30U) {
     919           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
     920           0 :                     return AVERROR_INVALIDDATA;
     921             :                 }
     922             :             }
     923             :         }
     924             :     }
     925             : 
     926             :     //assign 0th elements of noise_facs_q from last elements
     927       10365 :     memcpy(ch_data->noise_facs_q[0], ch_data->noise_facs_q[ch_data->bs_num_noise],
     928             :            sizeof(ch_data->noise_facs_q[0]));
     929       10365 :     return 0;
     930             : }
     931             : 
     932        1563 : static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
     933             :                                GetBitContext *gb,
     934             :                                int bs_extension_id, int *num_bits_left)
     935             : {
     936        1563 :     switch (bs_extension_id) {
     937             :     case EXTENSION_ID_PS:
     938        1563 :         if (!ac->oc[1].m4ac.ps) {
     939           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
     940           0 :             skip_bits_long(gb, *num_bits_left); // bs_fill_bits
     941           0 :             *num_bits_left = 0;
     942             :         } else {
     943             : #if 1
     944        1563 :             *num_bits_left -= AAC_RENAME(ff_ps_read_data)(ac->avctx, gb, &sbr->ps, *num_bits_left);
     945        1563 :             ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
     946             : #else
     947             :             avpriv_report_missing_feature(ac->avctx, "Parametric Stereo");
     948             :             skip_bits_long(gb, *num_bits_left); // bs_fill_bits
     949             :             *num_bits_left = 0;
     950             : #endif
     951             :         }
     952        1563 :         break;
     953             :     default:
     954             :         // some files contain 0-padding
     955           0 :         if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
     956           0 :             avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
     957           0 :         skip_bits_long(gb, *num_bits_left); // bs_fill_bits
     958           0 :         *num_bits_left = 0;
     959           0 :         break;
     960             :     }
     961        1563 : }
     962             : 
     963        2593 : static int read_sbr_single_channel_element(AACContext *ac,
     964             :                                             SpectralBandReplication *sbr,
     965             :                                             GetBitContext *gb)
     966             : {
     967             :     int ret;
     968             : 
     969        2593 :     if (get_bits1(gb)) // bs_data_extra
     970           0 :         skip_bits(gb, 4); // bs_reserved
     971             : 
     972        2593 :     if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
     973           0 :         return -1;
     974        2593 :     read_sbr_dtdf(sbr, gb, &sbr->data[0]);
     975        2593 :     read_sbr_invf(sbr, gb, &sbr->data[0]);
     976        2593 :     if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
     977           0 :         return ret;
     978        2593 :     if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
     979           0 :         return ret;
     980             : 
     981        2593 :     if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
     982         174 :         get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
     983             : 
     984        2593 :     return 0;
     985             : }
     986             : 
     987        3886 : static int read_sbr_channel_pair_element(AACContext *ac,
     988             :                                           SpectralBandReplication *sbr,
     989             :                                           GetBitContext *gb)
     990             : {
     991             :     int ret;
     992             : 
     993        3886 :     if (get_bits1(gb))    // bs_data_extra
     994           0 :         skip_bits(gb, 8); // bs_reserved
     995             : 
     996        3886 :     if ((sbr->bs_coupling = get_bits1(gb))) {
     997        2625 :         if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
     998           0 :             return -1;
     999        2625 :         copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
    1000        2625 :         read_sbr_dtdf(sbr, gb, &sbr->data[0]);
    1001        2625 :         read_sbr_dtdf(sbr, gb, &sbr->data[1]);
    1002        2625 :         read_sbr_invf(sbr, gb, &sbr->data[0]);
    1003        2625 :         memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
    1004        2625 :         memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
    1005        2625 :         if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
    1006           0 :             return ret;
    1007        2625 :         if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
    1008           0 :             return ret;
    1009        2625 :         if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
    1010           0 :             return ret;
    1011        2625 :         if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
    1012           0 :             return ret;
    1013             :     } else {
    1014        2522 :         if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
    1015        1261 :             read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
    1016           0 :             return -1;
    1017        1261 :         read_sbr_dtdf(sbr, gb, &sbr->data[0]);
    1018        1261 :         read_sbr_dtdf(sbr, gb, &sbr->data[1]);
    1019        1261 :         read_sbr_invf(sbr, gb, &sbr->data[0]);
    1020        1261 :         read_sbr_invf(sbr, gb, &sbr->data[1]);
    1021        1261 :         if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
    1022           0 :             return ret;
    1023        1261 :         if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
    1024           0 :             return ret;
    1025        1261 :         if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
    1026           0 :             return ret;
    1027        1261 :         if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
    1028           0 :             return ret;
    1029             :     }
    1030             : 
    1031        3886 :     if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
    1032         698 :         get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
    1033        3886 :     if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
    1034         464 :         get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
    1035             : 
    1036        3886 :     return 0;
    1037             : }
    1038             : 
    1039        6479 : static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
    1040             :                                   GetBitContext *gb, int id_aac)
    1041             : {
    1042        6479 :     unsigned int cnt = get_bits_count(gb);
    1043             : 
    1044        6479 :     sbr->id_aac = id_aac;
    1045        6479 :     sbr->ready_for_dequant = 1;
    1046             : 
    1047        6479 :     if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
    1048        5186 :         if (read_sbr_single_channel_element(ac, sbr, gb)) {
    1049           0 :             sbr_turnoff(sbr);
    1050           0 :             return get_bits_count(gb) - cnt;
    1051             :         }
    1052        3886 :     } else if (id_aac == TYPE_CPE) {
    1053        3886 :         if (read_sbr_channel_pair_element(ac, sbr, gb)) {
    1054           0 :             sbr_turnoff(sbr);
    1055           0 :             return get_bits_count(gb) - cnt;
    1056             :         }
    1057             :     } else {
    1058           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1059             :             "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
    1060           0 :         sbr_turnoff(sbr);
    1061           0 :         return get_bits_count(gb) - cnt;
    1062             :     }
    1063        6479 :     if (get_bits1(gb)) { // bs_extended_data
    1064        1563 :         int num_bits_left = get_bits(gb, 4); // bs_extension_size
    1065        1563 :         if (num_bits_left == 15)
    1066         224 :             num_bits_left += get_bits(gb, 8); // bs_esc_count
    1067             : 
    1068        1563 :         num_bits_left <<= 3;
    1069        4689 :         while (num_bits_left > 7) {
    1070        1563 :             num_bits_left -= 2;
    1071        1563 :             read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
    1072             :         }
    1073        1563 :         if (num_bits_left < 0) {
    1074           0 :             av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
    1075             :         }
    1076        1563 :         if (num_bits_left > 0)
    1077        1414 :             skip_bits(gb, num_bits_left);
    1078             :     }
    1079             : 
    1080        6479 :     return get_bits_count(gb) - cnt;
    1081             : }
    1082             : 
    1083          64 : static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
    1084             : {
    1085             :     int err;
    1086          64 :     err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
    1087          64 :     if (err >= 0)
    1088          64 :         err = sbr_make_f_derived(ac, sbr);
    1089          64 :     if (err < 0) {
    1090           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1091             :                "SBR reset failed. Switching SBR to pure upsampling mode.\n");
    1092           0 :         sbr_turnoff(sbr);
    1093             :     }
    1094          64 : }
    1095             : 
    1096             : /**
    1097             :  * Decode Spectral Band Replication extension data; reference: table 4.55.
    1098             :  *
    1099             :  * @param   crc flag indicating the presence of CRC checksum
    1100             :  * @param   cnt length of TYPE_FIL syntactic element in bytes
    1101             :  *
    1102             :  * @return  Returns number of bytes consumed from the TYPE_FIL element.
    1103             :  */
    1104       10794 : int AAC_RENAME(ff_decode_sbr_extension)(AACContext *ac, SpectralBandReplication *sbr,
    1105             :                             GetBitContext *gb_host, int crc, int cnt, int id_aac)
    1106             : {
    1107       10794 :     unsigned int num_sbr_bits = 0, num_align_bits;
    1108             :     unsigned bytes_read;
    1109       10794 :     GetBitContext gbc = *gb_host, *gb = &gbc;
    1110       10794 :     skip_bits_long(gb_host, cnt*8 - 4);
    1111             : 
    1112       10794 :     sbr->reset = 0;
    1113             : 
    1114       10794 :     if (!sbr->sample_rate)
    1115          80 :         sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
    1116       10794 :     if (!ac->oc[1].m4ac.ext_sample_rate)
    1117          18 :         ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
    1118             : 
    1119       10794 :     if (crc) {
    1120           0 :         skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
    1121           0 :         num_sbr_bits += 10;
    1122             :     }
    1123             : 
    1124             :     //Save some state from the previous frame.
    1125       10794 :     sbr->kx[0] = sbr->kx[1];
    1126       10794 :     sbr->m[0] = sbr->m[1];
    1127       10794 :     sbr->kx_and_m_pushed = 1;
    1128             : 
    1129       10794 :     num_sbr_bits++;
    1130       10794 :     if (get_bits1(gb)) // bs_header_flag
    1131         485 :         num_sbr_bits += read_sbr_header(sbr, gb);
    1132             : 
    1133       10794 :     if (sbr->reset)
    1134          64 :         sbr_reset(ac, sbr);
    1135             : 
    1136       10794 :     if (sbr->start)
    1137        6479 :         num_sbr_bits  += read_sbr_data(ac, sbr, gb, id_aac);
    1138             : 
    1139       10794 :     num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
    1140       10794 :     bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
    1141             : 
    1142       10794 :     if (bytes_read > cnt) {
    1143           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1144             :                "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
    1145             :     }
    1146       10794 :     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       11302 : 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       19630 :     memcpy(x    , x+1024, (320-32)*sizeof(x[0]));
    1169       19630 :     memcpy(x+288, in,         1024*sizeof(x[0]));
    1170      647790 :     for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
    1171             :                                // are not supported
    1172      628160 :         dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
    1173      628160 :         sbrdsp->sum64x5(z);
    1174      628160 :         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      628160 :         mdct->imdct_half(mdct, z, z+64);
    1191      628160 :         sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
    1192      628160 :         x += 32;
    1193             :     }
    1194       19630 : }
    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       21522 : 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       21522 :     const INTFLOAT *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
    1214       21522 :     const int step = 128 >> div;
    1215             :     INTFLOAT *v;
    1216      710226 :     for (i = 0; i < 32; i++) {
    1217      688704 :         if (*v_off < step) {
    1218       65526 :             int saved_samples = (1280 - 128) >> div;
    1219       65526 :             memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
    1220       65526 :             *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
    1221             :         } else {
    1222      623178 :             *v_off -= step;
    1223             :         }
    1224      688704 :         v = v0 + *v_off;
    1225      688704 :         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      541632 :             sbrdsp->neg_odd_64(X[1][i]);
    1234      541632 :             mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
    1235      541632 :             mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
    1236      541632 :             sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
    1237             :         }
    1238      688704 :         dsp->vector_fmul    (out, v                , sbr_qmf_window                       , 64 >> div);
    1239      688704 :         dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out   , 64 >> div);
    1240      688704 :         dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out   , 64 >> div);
    1241      688704 :         dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out   , 64 >> div);
    1242      688704 :         dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out   , 64 >> div);
    1243      688704 :         dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out   , 64 >> div);
    1244      688704 :         dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out   , 64 >> div);
    1245      688704 :         dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out   , 64 >> div);
    1246      688704 :         dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out   , 64 >> div);
    1247      688704 :         dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out   , 64 >> div);
    1248      688704 :         out += 64 >> div;
    1249             :     }
    1250       21522 : }
    1251             : #endif
    1252             : 
    1253             : /// Generate the subband filtered lowband
    1254       19630 : 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       19630 :     const int t_HFGen = 8;
    1260       19630 :     const int i_f = 32;
    1261       19630 :     memset(X_low, 0, 32*sizeof(*X_low));
    1262      506836 :     for (k = 0; k < sbr->kx[1]; k++) {
    1263    16077798 :         for (i = t_HFGen; i < i_f + t_HFGen; i++) {
    1264    15590592 :             X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
    1265    15590592 :             X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
    1266             :         }
    1267             :     }
    1268       19630 :     buf_idx = 1-buf_idx;
    1269      507676 :     for (k = 0; k < sbr->kx[0]; k++) {
    1270     4392414 :         for (i = 0; i < t_HFGen; i++) {
    1271     3904368 :             X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
    1272     3904368 :             X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
    1273             :         }
    1274             :     }
    1275       19630 :     return 0;
    1276             : }
    1277             : 
    1278             : /// High Frequency Generator (14496-3 sp04 p215)
    1279       10365 : 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       10365 :     int g = 0;
    1287       10365 :     int k = sbr->kx[1];
    1288       36503 :     for (j = 0; j < sbr->num_patches; j++) {
    1289      282668 :         for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
    1290      256530 :             const int p = sbr->patch_start_subband[j] + x;
    1291      789944 :             while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
    1292      276884 :                 g++;
    1293      256530 :             g--;
    1294             : 
    1295      256530 :             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     1282650 :             sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
    1302      256530 :                             X_low[p]  + ENVELOPE_ADJUSTMENT_OFFSET,
    1303      513060 :                             alpha0[p], alpha1[p], bw_array[g],
    1304      513060 :                             2 * t_env[0], 2 * t_env[bs_num_env]);
    1305             :         }
    1306             :     }
    1307       10365 :     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       10365 :     return 0;
    1311             : }
    1312             : 
    1313             : /// Generate the subband filtered lowband
    1314       19630 : 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       19630 :     const int i_f = 32;
    1320       19630 :     const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
    1321       19630 :     memset(X, 0, 2*sizeof(*X));
    1322      507676 :     for (k = 0; k < sbr->kx[0]; k++) {
    1323      543584 :         for (i = 0; i < i_Temp; i++) {
    1324       55538 :             X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
    1325       55538 :             X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
    1326             :         }
    1327             :     }
    1328      273568 :     for (; k < sbr->kx[0] + sbr->m[0]; k++) {
    1329      322180 :         for (i = 0; i < i_Temp; i++) {
    1330       68242 :             X[0][i][k] = Y0[i + i_f][k][0];
    1331       68242 :             X[1][i][k] = Y0[i + i_f][k][1];
    1332             :         }
    1333             :     }
    1334             : 
    1335      506836 :     for (k = 0; k < sbr->kx[1]; k++) {
    1336    18945496 :         for (i = i_Temp; i < 38; i++) {
    1337    18458290 :             X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
    1338    18458290 :             X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
    1339             :         }
    1340             :     }
    1341      276160 :     for (; k < sbr->kx[1] + sbr->m[1]; k++) {
    1342     8397248 :         for (i = i_Temp; i < i_f; i++) {
    1343     8140718 :             X[0][i][k] = Y1[i][k][0];
    1344     8140718 :             X[1][i][k] = Y1[i][k][1];
    1345             :         }
    1346             :     }
    1347       19630 :     return 0;
    1348             : }
    1349             : 
    1350             : /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
    1351             :  * (14496-3 sp04 p217)
    1352             :  */
    1353       10365 : static int sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
    1354             :                         SBRData *ch_data, int e_a[2])
    1355             : {
    1356             :     int e, i, m;
    1357             : 
    1358       10365 :     memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
    1359       25465 :     for (e = 0; e < ch_data->bs_num_env; e++) {
    1360       15100 :         const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
    1361       15100 :         uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
    1362             :         int k;
    1363             : 
    1364       15100 :         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      186900 :         for (i = 0; i < ilim; i++)
    1371      550582 :             for (m = table[i]; m < table[i + 1]; m++)
    1372      378782 :                 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       15100 :         k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
    1376       60624 :         for (i = 0; i < sbr->n_q; i++)
    1377      424306 :             for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
    1378      378782 :                 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
    1379             : 
    1380      197196 :         for (i = 0; i < sbr->n[1]; i++) {
    1381      182096 :             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      186900 :         for (i = 0; i < ilim; i++) {
    1391      171800 :             int additional_sinusoid_present = 0;
    1392      544126 :             for (m = table[i]; m < table[i + 1]; m++) {
    1393      376056 :                 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
    1394        3730 :                     additional_sinusoid_present = 1;
    1395        3730 :                     break;
    1396             :                 }
    1397             :             }
    1398      171800 :             memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
    1399      171800 :                    (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
    1400             :         }
    1401             :     }
    1402             : 
    1403       10365 :     memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
    1404       10365 :     return 0;
    1405             : }
    1406             : 
    1407             : /// Estimation of current envelope (14496-3 sp04 p218)
    1408       10365 : 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       10365 :     int kx1 = sbr->kx[1];
    1413             : 
    1414       10365 :     if (sbr->bs_interpol_freq) {
    1415       25465 :         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        9512 :             const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
    1420             : #endif /* USE_FIXED */
    1421       15100 :             int ilb = ch_data->t_env[e]     * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
    1422       15100 :             int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
    1423             : 
    1424      393882 :             for (m = 0; m < sbr->m[1]; m++) {
    1425      378782 :                 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      250945 :                 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       10365 : }
    1466             : 
    1467       12304 : void AAC_RENAME(ff_sbr_apply)(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
    1468             :                   INTFLOAT* L, INTFLOAT* R)
    1469             : {
    1470       12304 :     int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
    1471             :     int ch;
    1472       12304 :     int nch = (id_aac == TYPE_CPE) ? 2 : 1;
    1473             :     int err;
    1474             : 
    1475       12304 :     if (id_aac != sbr->id_aac) {
    1476        4950 :         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        4950 :         sbr_turnoff(sbr);
    1479             :     }
    1480             : 
    1481       12304 :     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       12304 :     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       10794 :         sbr->kx_and_m_pushed = 0;
    1492             :     }
    1493             : 
    1494       12304 :     if (sbr->start) {
    1495        6479 :         sbr_dequant(sbr, id_aac);
    1496        6479 :         sbr->ready_for_dequant = 0;
    1497             :     }
    1498       31934 :     for (ch = 0; ch < nch; ch++) {
    1499             :         /* decode channel */
    1500       39260 :         sbr_qmf_analysis(ac->fdsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
    1501       19630 :                          (INTFLOAT*)sbr->qmf_filter_scratch,
    1502       19630 :                          sbr->data[ch].W, sbr->data[ch].Ypos);
    1503       39260 :         sbr->c.sbr_lf_gen(ac, sbr, sbr->X_low,
    1504       19630 :                           (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
    1505             :                           sbr->data[ch].Ypos);
    1506       19630 :         sbr->data[ch].Ypos ^= 1;
    1507       19630 :         if (sbr->start) {
    1508       27308 :             sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
    1509       16943 :                                          (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
    1510       10365 :             sbr_chirp(sbr, &sbr->data[ch]);
    1511       10365 :             av_assert0(sbr->data[ch].bs_num_env > 0);
    1512       20730 :             sbr_hf_gen(ac, sbr, sbr->X_high,
    1513       10365 :                        (const INTFLOAT (*)[40][2]) sbr->X_low,
    1514       10365 :                        (const INTFLOAT (*)[2]) sbr->alpha0,
    1515       10365 :                        (const INTFLOAT (*)[2]) sbr->alpha1,
    1516       10365 :                        sbr->data[ch].bw_array, sbr->data[ch].t_env,
    1517        6578 :                        sbr->data[ch].bs_num_env);
    1518             : 
    1519             :             // hf_adj
    1520       10365 :             err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
    1521       10365 :             if (!err) {
    1522       10365 :                 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
    1523       10365 :                 sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
    1524       31095 :                 sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
    1525       10365 :                                 (const INTFLOAT (*)[40][2]) sbr->X_high,
    1526             :                                 sbr, &sbr->data[ch],
    1527       10365 :                                 sbr->data[ch].e_a);
    1528             :             }
    1529             :         }
    1530             : 
    1531             :         /* synthesis */
    1532       39260 :         sbr->c.sbr_x_gen(sbr, sbr->X[ch],
    1533       19630 :                   (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
    1534       19630 :                   (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[  sbr->data[ch].Ypos],
    1535       19630 :                   (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
    1536             :     }
    1537             : 
    1538       12304 :     if (ac->oc[1].m4ac.ps == 1) {
    1539        1892 :         if (sbr->ps.start) {
    1540        1563 :             AAC_RENAME(ff_ps_apply)(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
    1541             :         } else {
    1542         329 :             memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
    1543             :         }
    1544        1892 :         nch = 2;
    1545             :     }
    1546             : 
    1547       24608 :     sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
    1548       12304 :                       L, sbr->X[0], sbr->qmf_filter_scratch,
    1549       12304 :                       sbr->data[0].synthesis_filterbank_samples,
    1550             :                       &sbr->data[0].synthesis_filterbank_samples_offset,
    1551             :                       downsampled);
    1552       12304 :     if (nch == 2)
    1553       18436 :         sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
    1554        9218 :                           R, sbr->X[1], sbr->qmf_filter_scratch,
    1555        9218 :                           sbr->data[1].synthesis_filterbank_samples,
    1556             :                           &sbr->data[1].synthesis_filterbank_samples_offset,
    1557             :                           downsampled);
    1558       12304 : }
    1559             : 
    1560         329 : static void aacsbr_func_ptr_init(AACSBRContext *c)
    1561             : {
    1562         329 :     c->sbr_lf_gen            = sbr_lf_gen;
    1563         329 :     c->sbr_hf_assemble       = sbr_hf_assemble;
    1564         329 :     c->sbr_x_gen             = sbr_x_gen;
    1565         329 :     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         329 : }

Generated by: LCOV version 1.12