2
2
* Common code between the AC-3 encoder and decoder
3
3
* Copyright (c) 2000 Fabrice Bellard
5
* This file is part of FFmpeg.
5
* This file is part of Libav.
7
* FFmpeg is free software; you can redistribute it and/or
7
* Libav is free software; you can redistribute it and/or
8
8
* modify it under the terms of the GNU Lesser General Public
9
9
* License as published by the Free Software Foundation; either
10
10
* version 2.1 of the License, or (at your option) any later version.
12
* FFmpeg is distributed in the hope that it will be useful,
12
* Libav is distributed in the hope that it will be useful,
13
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
15
* Lesser General Public License for more details.
17
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with FFmpeg; if not, write to the Free Software
18
* License along with Libav; if not, write to the Free Software
19
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29
29
#include "get_bits.h"
31
#if CONFIG_HARDCODED_TABLES
34
32
* Starting frequency coefficient bin for each critical band.
36
static const uint8_t band_start_tab[AC3_CRITICAL_BANDS+1] = {
34
const uint8_t ff_ac3_band_start_tab[AC3_CRITICAL_BANDS+1] = {
37
35
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
38
36
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
39
37
20, 21, 22, 23, 24, 25, 26, 27, 28, 31,
41
39
79, 85, 97, 109, 121, 133, 157, 181, 205, 229, 253
42
#if CONFIG_HARDCODED_TABLES
45
45
* Map each frequency coefficient bin to the critical band that contains it.
47
static const uint8_t bin_to_band_tab[253] = {
47
const uint8_t ff_ac3_bin_to_band_tab[253] = {
49
49
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
50
50
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
216
void ff_ac3_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end,
217
int snr_offset, int floor,
218
const uint8_t *bap_tab, uint8_t *bap)
222
/* special case, if snr offset is -960, set all bap's to zero */
223
if (snr_offset == -960) {
224
memset(bap, 0, AC3_MAX_COEFS);
229
band = bin_to_band_tab[start];
231
int m = (FFMAX(mask[band] - snr_offset - floor, 0) & 0x1FE0) + floor;
232
int band_end = FFMIN(band_start_tab[band+1], end);
233
for (; bin < band_end; bin++) {
234
int address = av_clip((psd[bin] - m) >> 5, 0, 63);
235
bap[bin] = bap_tab[address];
237
} while (end > band_start_tab[band++]);
240
/* AC-3 bit allocation. The algorithm is the one described in the AC-3
242
void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
243
int8_t *exp, int start, int end,
244
int snr_offset, int fast_gain, int is_lfe,
245
int dba_mode, int dba_nsegs,
246
uint8_t *dba_offsets, uint8_t *dba_lengths,
249
int16_t psd[AC3_MAX_COEFS]; /* scaled exponents */
250
int16_t band_psd[AC3_CRITICAL_BANDS]; /* interpolated exponents */
251
int16_t mask[AC3_CRITICAL_BANDS]; /* masking value */
253
ff_ac3_bit_alloc_calc_psd(exp, start, end, psd, band_psd);
255
ff_ac3_bit_alloc_calc_mask(s, band_psd, start, end, fast_gain, is_lfe,
256
dba_mode, dba_nsegs, dba_offsets, dba_lengths,
259
ff_ac3_bit_alloc_calc_bap(mask, psd, start, end, snr_offset, s->floor,
260
ff_ac3_bap_tab, bap);
264
216
* Initialize some tables.
265
217
* note: This function must remain thread safe because it is called by the
266
218
* AVParser init code.
268
av_cold void ac3_common_init(void)
220
av_cold void ff_ac3_common_init(void)
270
222
#if !CONFIG_HARDCODED_TABLES
271
/* compute bndtab and masktab from bandsz */
223
/* compute ff_ac3_bin_to_band_tab from ff_ac3_band_start_tab */
272
224
int bin = 0, band;
273
225
for (band = 0; band < AC3_CRITICAL_BANDS; band++) {
274
int band_end = bin + ff_ac3_critical_band_size_tab[band];
275
band_start_tab[band] = bin;
226
int band_end = ff_ac3_band_start_tab[band+1];
276
227
while (bin < band_end)
277
bin_to_band_tab[bin++] = band;
228
ff_ac3_bin_to_band_tab[bin++] = band;
279
band_start_tab[AC3_CRITICAL_BANDS] = bin;
280
230
#endif /* !CONFIG_HARDCODED_TABLES */