~ubuntu-branches/ubuntu/precise/mplayer2/precise-proposed

« back to all changes in this revision

Viewing changes to ffmpeg-mt/libavcodec/ac3.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-04-21 09:21:39 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110421092139-7a21foqroxvir3wr
Tags: 2.0-54-gd33877a-1
* New upstream version
* Bug fix: "internal MP3 decoder miscompiles with gcc 4.6", thanks to
  Norbert Preining (Closes: #623279). Fixed by no longer using internal
  mp3lib copy.
* drop build host specific optimizations

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Common code between the AC-3 encoder and decoder
3
3
 * Copyright (c) 2000 Fabrice Bellard
4
4
 *
5
 
 * This file is part of FFmpeg.
 
5
 * This file is part of Libav.
6
6
 *
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.
11
11
 *
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.
16
16
 *
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
20
20
 */
21
21
 
28
28
#include "ac3.h"
29
29
#include "get_bits.h"
30
30
 
31
 
#if CONFIG_HARDCODED_TABLES
32
 
 
33
31
/**
34
32
 * Starting frequency coefficient bin for each critical band.
35
33
 */
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
40
};
43
41
 
 
42
#if CONFIG_HARDCODED_TABLES
 
43
 
44
44
/**
45
45
 * Map each frequency coefficient bin to the critical band that contains it.
46
46
 */
47
 
static const uint8_t bin_to_band_tab[253] = {
 
47
const uint8_t ff_ac3_bin_to_band_tab[253] = {
48
48
     0,
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,
70
70
};
71
71
 
72
72
#else /* CONFIG_HARDCODED_TABLES */
73
 
static uint8_t band_start_tab[51];
74
 
static uint8_t bin_to_band_tab[253];
 
73
uint8_t ff_ac3_bin_to_band_tab[253];
75
74
#endif
76
75
 
77
76
static inline int calc_lowcomp1(int a, int b0, int b1, int c)
107
106
 
108
107
    /* PSD integration */
109
108
    bin  = start;
110
 
    band = bin_to_band_tab[start];
 
109
    band = ff_ac3_bin_to_band_tab[start];
111
110
    do {
112
111
        int v = psd[bin++];
113
 
        int band_end = FFMIN(band_start_tab[band+1], end);
 
112
        int band_end = FFMIN(ff_ac3_band_start_tab[band+1], end);
114
113
        for (; bin < band_end; bin++) {
115
114
            int max = FFMAX(v, psd[bin]);
116
115
            /* logadd */
118
117
            v = max + ff_ac3_log_add_tab[adr];
119
118
        }
120
119
        band_psd[band++] = v;
121
 
    } while (end > band_start_tab[band]);
 
120
    } while (end > ff_ac3_band_start_tab[band]);
122
121
}
123
122
 
124
123
int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd,
133
132
    int lowcomp, fastleak, slowleak;
134
133
 
135
134
    /* excitation function */
136
 
    band_start = bin_to_band_tab[start];
137
 
    band_end   = bin_to_band_tab[end-1] + 1;
 
135
    band_start = ff_ac3_bin_to_band_tab[start];
 
136
    band_end   = ff_ac3_bin_to_band_tab[end-1] + 1;
138
137
 
139
138
    if (band_start == 0) {
140
139
        lowcomp = 0;
213
212
    return 0;
214
213
}
215
214
 
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)
219
 
{
220
 
    int bin, band;
221
 
 
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);
225
 
        return;
226
 
    }
227
 
 
228
 
    bin  = start;
229
 
    band = bin_to_band_tab[start];
230
 
    do {
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];
236
 
        }
237
 
    } while (end > band_start_tab[band++]);
238
 
}
239
 
 
240
 
/* AC-3 bit allocation. The algorithm is the one described in the AC-3
241
 
   spec. */
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,
247
 
                                   uint8_t *dba_values)
248
 
{
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 */
252
 
 
253
 
    ff_ac3_bit_alloc_calc_psd(exp, start, end, psd, band_psd);
254
 
 
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,
257
 
                               dba_values, mask);
258
 
 
259
 
    ff_ac3_bit_alloc_calc_bap(mask, psd, start, end, snr_offset, s->floor,
260
 
                              ff_ac3_bap_tab, bap);
261
 
}
262
 
 
263
215
/**
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.
267
219
 */
268
 
av_cold void ac3_common_init(void)
 
220
av_cold void ff_ac3_common_init(void)
269
221
{
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;
278
229
    }
279
 
    band_start_tab[AC3_CRITICAL_BANDS] = bin;
280
230
#endif /* !CONFIG_HARDCODED_TABLES */
281
231
}