2007-03-09 13:54:44 +00:00
|
|
|
/*
|
2008-08-03 16:42:32 +00:00
|
|
|
* Common code between the AC-3 encoder and decoder
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2000 Fabrice Bellard
|
2007-03-09 13:54:44 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-02-01 02:00:19 +00:00
|
|
|
* @file libavcodec/ac3.c
|
2008-08-03 16:42:32 +00:00
|
|
|
* Common code between the AC-3 encoder and decoder.
|
2007-03-09 13:54:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "ac3.h"
|
2009-04-13 16:20:26 +00:00
|
|
|
#include "get_bits.h"
|
2007-03-09 13:54:44 +00:00
|
|
|
|
2009-09-27 04:07:49 +00:00
|
|
|
#if CONFIG_HARDCODED_TABLES
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starting frequency coefficient bin for each critical band.
|
|
|
|
*/
|
|
|
|
static const uint8_t band_start_tab[51] = {
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
|
|
|
20, 21, 22, 23, 24, 25, 26, 27, 28, 31,
|
|
|
|
34, 37, 40, 43, 46, 49, 55, 61, 67, 73,
|
|
|
|
79, 85, 97, 109, 121, 133, 157, 181, 205, 229, 253
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps each frequency coefficient bin to the critical band that contains it.
|
|
|
|
*/
|
|
|
|
static const uint8_t bin_to_band_tab[253] = {
|
|
|
|
0,
|
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
|
|
|
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
|
|
|
25, 26, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30,
|
|
|
|
31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 34,
|
|
|
|
35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36,
|
|
|
|
37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38,
|
|
|
|
39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40,
|
|
|
|
41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
|
|
|
|
42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
|
|
|
|
43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
|
|
|
|
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
|
|
|
|
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
|
|
|
|
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
|
|
|
|
46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
|
|
|
|
46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
|
|
|
|
47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
|
|
|
|
47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
|
|
|
|
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
|
|
|
|
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
|
|
|
|
49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
|
|
|
|
49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49
|
|
|
|
};
|
|
|
|
|
|
|
|
#else /* CONFIG_HARDCODED_TABLES */
|
2007-12-09 03:23:04 +00:00
|
|
|
static uint8_t band_start_tab[51];
|
|
|
|
static uint8_t bin_to_band_tab[253];
|
2009-09-27 04:07:49 +00:00
|
|
|
#endif
|
2007-04-25 22:12:51 +00:00
|
|
|
|
2007-03-09 13:54:44 +00:00
|
|
|
static inline int calc_lowcomp1(int a, int b0, int b1, int c)
|
|
|
|
{
|
|
|
|
if ((b0 + 256) == b1) {
|
|
|
|
a = c;
|
|
|
|
} else if (b0 > b1) {
|
|
|
|
a = FFMAX(a - 64, 0);
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int calc_lowcomp(int a, int b0, int b1, int bin)
|
|
|
|
{
|
|
|
|
if (bin < 7) {
|
|
|
|
return calc_lowcomp1(a, b0, b1, 384);
|
|
|
|
} else if (bin < 20) {
|
|
|
|
return calc_lowcomp1(a, b0, b1, 320);
|
|
|
|
} else {
|
|
|
|
return FFMAX(a - 128, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-18 21:43:20 +00:00
|
|
|
void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd,
|
2007-12-09 03:23:04 +00:00
|
|
|
int16_t *band_psd)
|
2007-03-09 13:54:44 +00:00
|
|
|
{
|
2009-09-27 04:46:56 +00:00
|
|
|
int bin, band;
|
2007-03-09 13:54:44 +00:00
|
|
|
|
|
|
|
/* exponent mapping to PSD */
|
2009-09-27 04:55:37 +00:00
|
|
|
for (bin = start; bin < end; bin++) {
|
2007-03-09 13:54:44 +00:00
|
|
|
psd[bin]=(3072 - (exp[bin] << 7));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PSD integration */
|
2009-09-27 04:46:56 +00:00
|
|
|
bin = start;
|
|
|
|
band = bin_to_band_tab[start];
|
2007-03-09 13:54:44 +00:00
|
|
|
do {
|
2009-09-27 04:46:56 +00:00
|
|
|
int v = psd[bin++];
|
|
|
|
int band_end = FFMIN(band_start_tab[band+1], end);
|
|
|
|
for (; bin < band_end; bin++) {
|
2010-01-14 17:50:33 +00:00
|
|
|
int max = FFMAX(v, psd[bin]);
|
2007-03-09 13:54:44 +00:00
|
|
|
/* logadd */
|
2010-01-14 17:50:33 +00:00
|
|
|
int adr = FFMIN(max - ((v + psd[bin] + 1) >> 1), 255);
|
|
|
|
v = max + ff_ac3_log_add_tab[adr];
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
2009-09-27 04:47:38 +00:00
|
|
|
band_psd[band++] = v;
|
2009-09-27 04:46:56 +00:00
|
|
|
} while (end > band_start_tab[band]);
|
2007-03-18 21:43:20 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 03:13:20 +00:00
|
|
|
int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd,
|
2008-12-08 03:15:10 +00:00
|
|
|
int start, int end, int fast_gain, int is_lfe,
|
|
|
|
int dba_mode, int dba_nsegs, uint8_t *dba_offsets,
|
|
|
|
uint8_t *dba_lengths, uint8_t *dba_values,
|
|
|
|
int16_t *mask)
|
2007-03-18 21:43:20 +00:00
|
|
|
{
|
|
|
|
int16_t excite[50]; /* excitation */
|
2009-09-27 05:36:52 +00:00
|
|
|
int band;
|
|
|
|
int band_start, band_end, begin, end1;
|
2007-03-18 21:43:20 +00:00
|
|
|
int lowcomp, fastleak, slowleak;
|
2007-03-09 13:54:44 +00:00
|
|
|
|
|
|
|
/* excitation function */
|
2009-09-27 05:36:52 +00:00
|
|
|
band_start = bin_to_band_tab[start];
|
|
|
|
band_end = bin_to_band_tab[end-1] + 1;
|
2007-03-09 13:54:44 +00:00
|
|
|
|
2009-09-27 05:36:52 +00:00
|
|
|
if (band_start == 0) {
|
2007-03-09 13:54:44 +00:00
|
|
|
lowcomp = 0;
|
2007-12-09 03:23:04 +00:00
|
|
|
lowcomp = calc_lowcomp1(lowcomp, band_psd[0], band_psd[1], 384);
|
|
|
|
excite[0] = band_psd[0] - fast_gain - lowcomp;
|
|
|
|
lowcomp = calc_lowcomp1(lowcomp, band_psd[1], band_psd[2], 384);
|
|
|
|
excite[1] = band_psd[1] - fast_gain - lowcomp;
|
2007-03-09 13:54:44 +00:00
|
|
|
begin = 7;
|
2009-09-27 05:36:52 +00:00
|
|
|
for (band = 2; band < 7; band++) {
|
|
|
|
if (!(is_lfe && band == 6))
|
|
|
|
lowcomp = calc_lowcomp1(lowcomp, band_psd[band], band_psd[band+1], 384);
|
|
|
|
fastleak = band_psd[band] - fast_gain;
|
|
|
|
slowleak = band_psd[band] - s->slow_gain;
|
|
|
|
excite[band] = fastleak - lowcomp;
|
|
|
|
if (!(is_lfe && band == 6)) {
|
|
|
|
if (band_psd[band] <= band_psd[band+1]) {
|
|
|
|
begin = band + 1;
|
2007-03-09 13:54:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-27 05:36:52 +00:00
|
|
|
end1 = FFMIN(band_end, 22);
|
|
|
|
for (band = begin; band < end1; band++) {
|
|
|
|
if (!(is_lfe && band == 6))
|
|
|
|
lowcomp = calc_lowcomp(lowcomp, band_psd[band], band_psd[band+1], band);
|
|
|
|
fastleak = FFMAX(fastleak - s->fast_decay, band_psd[band] - fast_gain);
|
|
|
|
slowleak = FFMAX(slowleak - s->slow_decay, band_psd[band] - s->slow_gain);
|
|
|
|
excite[band] = FFMAX(fastleak - lowcomp, slowleak);
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
|
|
|
begin = 22;
|
|
|
|
} else {
|
|
|
|
/* coupling channel */
|
2009-09-27 05:36:52 +00:00
|
|
|
begin = band_start;
|
2007-12-09 03:23:04 +00:00
|
|
|
fastleak = (s->cpl_fast_leak << 8) + 768;
|
|
|
|
slowleak = (s->cpl_slow_leak << 8) + 768;
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-27 05:36:52 +00:00
|
|
|
for (band = begin; band < band_end; band++) {
|
|
|
|
fastleak = FFMAX(fastleak - s->fast_decay, band_psd[band] - fast_gain);
|
|
|
|
slowleak = FFMAX(slowleak - s->slow_decay, band_psd[band] - s->slow_gain);
|
|
|
|
excite[band] = FFMAX(fastleak, slowleak);
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compute masking curve */
|
|
|
|
|
2009-09-27 05:36:52 +00:00
|
|
|
for (band = band_start; band < band_end; band++) {
|
|
|
|
int tmp = s->db_per_bit - band_psd[band];
|
2007-03-09 13:54:44 +00:00
|
|
|
if (tmp > 0) {
|
2009-09-27 05:36:52 +00:00
|
|
|
excite[band] += tmp >> 2;
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
2009-09-27 05:36:52 +00:00
|
|
|
mask[band] = FFMAX(ff_ac3_hearing_threshold_tab[band >> s->sr_shift][s->sr_code], excite[band]);
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* delta bit allocation */
|
|
|
|
|
2007-12-09 03:23:04 +00:00
|
|
|
if (dba_mode == DBA_REUSE || dba_mode == DBA_NEW) {
|
2009-09-27 05:36:52 +00:00
|
|
|
int i, seg, delta;
|
2008-12-08 03:13:20 +00:00
|
|
|
if (dba_nsegs >= 8)
|
|
|
|
return -1;
|
2007-03-09 13:54:44 +00:00
|
|
|
band = 0;
|
2008-12-08 03:13:20 +00:00
|
|
|
for (seg = 0; seg < dba_nsegs; seg++) {
|
|
|
|
band += dba_offsets[seg];
|
|
|
|
if (band >= 50 || dba_lengths[seg] > 50-band)
|
|
|
|
return -1;
|
2007-12-09 03:23:04 +00:00
|
|
|
if (dba_values[seg] >= 4) {
|
|
|
|
delta = (dba_values[seg] - 3) << 7;
|
2007-03-09 13:54:44 +00:00
|
|
|
} else {
|
2007-12-09 03:23:04 +00:00
|
|
|
delta = (dba_values[seg] - 4) << 7;
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
2009-09-27 05:36:52 +00:00
|
|
|
for (i = 0; i < dba_lengths[seg]; i++) {
|
|
|
|
mask[band++] += delta;
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 03:13:20 +00:00
|
|
|
return 0;
|
2007-03-18 21:43:20 +00:00
|
|
|
}
|
2007-03-09 13:54:44 +00:00
|
|
|
|
2007-03-18 21:43:20 +00:00
|
|
|
void ff_ac3_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end,
|
2008-06-07 22:29:54 +00:00
|
|
|
int snr_offset, int floor,
|
|
|
|
const uint8_t *bap_tab, uint8_t *bap)
|
2007-03-18 21:43:20 +00:00
|
|
|
{
|
2009-09-27 06:13:01 +00:00
|
|
|
int bin, band;
|
2007-03-09 13:54:44 +00:00
|
|
|
|
2007-12-09 03:23:04 +00:00
|
|
|
/* special case, if snr offset is -960, set all bap's to zero */
|
2009-09-27 04:55:37 +00:00
|
|
|
if (snr_offset == -960) {
|
2007-03-20 02:12:13 +00:00
|
|
|
memset(bap, 0, 256);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-27 06:13:01 +00:00
|
|
|
bin = start;
|
|
|
|
band = bin_to_band_tab[start];
|
2007-03-09 13:54:44 +00:00
|
|
|
do {
|
2009-09-27 06:13:01 +00:00
|
|
|
int m = (FFMAX(mask[band] - snr_offset - floor, 0) & 0x1FE0) + floor;
|
2009-11-03 03:26:26 +00:00
|
|
|
int band_end = FFMIN(band_start_tab[band+1], end);
|
2009-09-27 06:13:01 +00:00
|
|
|
for (; bin < band_end; bin++) {
|
|
|
|
int address = av_clip((psd[bin] - m) >> 5, 0, 63);
|
|
|
|
bap[bin] = bap_tab[address];
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
2009-09-27 06:13:01 +00:00
|
|
|
} while (end > band_start_tab[band++]);
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
|
|
|
|
2008-08-03 16:42:32 +00:00
|
|
|
/* AC-3 bit allocation. The algorithm is the one described in the AC-3
|
2007-03-18 21:43:20 +00:00
|
|
|
spec. */
|
|
|
|
void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
|
|
|
|
int8_t *exp, int start, int end,
|
2007-12-09 03:23:04 +00:00
|
|
|
int snr_offset, int fast_gain, int is_lfe,
|
|
|
|
int dba_mode, int dba_nsegs,
|
|
|
|
uint8_t *dba_offsets, uint8_t *dba_lengths,
|
|
|
|
uint8_t *dba_values)
|
2007-03-18 21:43:20 +00:00
|
|
|
{
|
|
|
|
int16_t psd[256]; /* scaled exponents */
|
2007-12-09 03:23:04 +00:00
|
|
|
int16_t band_psd[50]; /* interpolated exponents */
|
2007-03-18 21:43:20 +00:00
|
|
|
int16_t mask[50]; /* masking value */
|
|
|
|
|
2007-12-09 03:23:04 +00:00
|
|
|
ff_ac3_bit_alloc_calc_psd(exp, start, end, psd, band_psd);
|
2007-03-18 21:43:20 +00:00
|
|
|
|
2007-12-09 03:23:04 +00:00
|
|
|
ff_ac3_bit_alloc_calc_mask(s, band_psd, start, end, fast_gain, is_lfe,
|
2009-09-27 04:55:37 +00:00
|
|
|
dba_mode, dba_nsegs, dba_offsets, dba_lengths,
|
|
|
|
dba_values, mask);
|
2007-03-18 21:43:20 +00:00
|
|
|
|
2008-06-07 22:29:54 +00:00
|
|
|
ff_ac3_bit_alloc_calc_bap(mask, psd, start, end, snr_offset, s->floor,
|
|
|
|
ff_ac3_bap_tab, bap);
|
2007-03-18 21:43:20 +00:00
|
|
|
}
|
|
|
|
|
2007-03-11 02:13:05 +00:00
|
|
|
/**
|
|
|
|
* Initializes some tables.
|
|
|
|
* note: This function must remain thread safe because it is called by the
|
|
|
|
* AVParser init code.
|
|
|
|
*/
|
2008-03-21 03:11:20 +00:00
|
|
|
av_cold void ac3_common_init(void)
|
2007-03-09 13:54:44 +00:00
|
|
|
{
|
2009-09-27 04:07:49 +00:00
|
|
|
#if !CONFIG_HARDCODED_TABLES
|
2007-03-09 13:54:44 +00:00
|
|
|
/* compute bndtab and masktab from bandsz */
|
2009-09-27 05:48:03 +00:00
|
|
|
int bin = 0, band;
|
|
|
|
for (band = 0; band < 50; band++) {
|
|
|
|
int band_end = bin + ff_ac3_critical_band_size_tab[band];
|
|
|
|
band_start_tab[band] = bin;
|
|
|
|
while (bin < band_end)
|
|
|
|
bin_to_band_tab[bin++] = band;
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|
2009-09-27 05:48:03 +00:00
|
|
|
band_start_tab[50] = bin;
|
2009-09-27 04:07:49 +00:00
|
|
|
#endif /* !CONFIG_HARDCODED_TABLES */
|
2007-03-09 13:54:44 +00:00
|
|
|
}
|