ac3dec.c File Reference

#include <stdio.h>
#include <stddef.h>
#include <math.h>
#include <string.h>
#include "libavutil/crc.h"
#include "libavutil/random.h"
#include "avcodec.h"
#include "ac3_parser.h"
#include "bitstream.h"
#include "dsputil.h"
#include "ac3dec.h"
#include "ac3dec_data.h"

Go to the source code of this file.

Data Structures

struct  mant_groups

Defines

#define AC3_MAX_FRAME_SIZE   21695
#define LEVEL_PLUS_3DB   1.4142135623730950
#define LEVEL_PLUS_1POINT5DB   1.1892071150027209
#define LEVEL_MINUS_1POINT5DB   0.8408964152537145
#define LEVEL_MINUS_3DB   0.7071067811865476
#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
#define LEVEL_MINUS_6DB   0.5000000000000000
#define LEVEL_MINUS_9DB   0.3535533905932738
#define LEVEL_ZERO   0.0000000000000000
#define LEVEL_ONE   1.0000000000000000

Functions

static int symmetric_dequant (int code, int levels)
static av_cold void ac3_tables_init (void)
static av_cold int ac3_decode_init (AVCodecContext *avctx)
static int ac3_parse_header (AC3DecodeContext *s)
static int parse_frame_header (AC3DecodeContext *s)
static void set_downmix_coeffs (AC3DecodeContext *s)
static void decode_exponents (GetBitContext *gbc, int exp_strategy, int ngrps, uint8_t absexp, int8_t *dexps)
static void uncouple_channels (AC3DecodeContext *s)
static void get_transform_coeffs_ch (AC3DecodeContext *s, int ch_index, mant_groups *m)
static void remove_dithering (AC3DecodeContext *s)
static void get_transform_coeffs (AC3DecodeContext *s)
static void do_rematrixing (AC3DecodeContext *s)
static void do_imdct_256 (AC3DecodeContext *s, int chindex)
static void do_imdct (AC3DecodeContext *s, int channels)
static void ac3_downmix (AC3DecodeContext *s, float samples[AC3_MAX_CHANNELS][256], int ch_offset)
static void ac3_upmix_delay (AC3DecodeContext *s)
static int ac3_parse_audio_block (AC3DecodeContext *s, int blk)
static int ac3_decode_frame (AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
static av_cold int ac3_decode_end (AVCodecContext *avctx)

Variables

static uint8_t exp_ungroup_tab [128][3]
static int b1_mantissas [32][3]
static int b2_mantissas [128][3]
static int b3_mantissas [8]
static int b4_mantissas [128][2]
static int b5_mantissas [16]
static const uint8_t quantization_tab [16]
static float dynamic_range_tab [256]
static const float gain_levels [9]
static const uint8_t center_levels [4] = { 4, 5, 6, 5 }
static const uint8_t surround_levels [4] = { 4, 6, 7, 6 }
static const uint8_t ac3_default_coeffs [8][5][2]
AVCodec ac3_decoder


Define Documentation

#define AC3_MAX_FRAME_SIZE   21695

Maximum possible frame size when the specification limit is ignored

Definition at line 45 of file ac3dec.c.

Referenced by ac3_decode_frame(), and ac3_decode_init().

#define LEVEL_MINUS_1POINT5DB   0.8408964152537145

Definition at line 73 of file ac3dec.c.

#define LEVEL_MINUS_3DB   0.7071067811865476

Definition at line 74 of file ac3dec.c.

Referenced by ac3_downmix().

#define LEVEL_MINUS_4POINT5DB   0.5946035575013605

Definition at line 75 of file ac3dec.c.

#define LEVEL_MINUS_6DB   0.5000000000000000

Definition at line 76 of file ac3dec.c.

#define LEVEL_MINUS_9DB   0.3535533905932738

Definition at line 77 of file ac3dec.c.

#define LEVEL_ONE   1.0000000000000000

Definition at line 79 of file ac3dec.c.

#define LEVEL_PLUS_1POINT5DB   1.1892071150027209

Definition at line 72 of file ac3dec.c.

#define LEVEL_PLUS_3DB   1.4142135623730950

Adjustments in dB gain

Definition at line 71 of file ac3dec.c.

#define LEVEL_ZERO   0.0000000000000000

Definition at line 78 of file ac3dec.c.


Function Documentation

static av_cold int ac3_decode_end ( AVCodecContext avctx  )  [static]

Uninitialize the AC-3 decoder.

Definition at line 1182 of file ac3dec.c.

References av_freep(), ff_mdct_end(), AC3DecodeContext::imdct_256, AC3DecodeContext::imdct_512, AC3DecodeContext::input_buffer, and AVCodecContext::priv_data.

01183 {
01184     AC3DecodeContext *s = avctx->priv_data;
01185     ff_mdct_end(&s->imdct_512);
01186     ff_mdct_end(&s->imdct_256);
01187 
01188     av_freep(&s->input_buffer);
01189 
01190     return 0;
01191 }

static int ac3_decode_frame ( AVCodecContext avctx,
void *  data,
int *  data_size,
const uint8_t *  buf,
int  buf_size 
) [static]

Decode a single AC-3 frame.

Definition at line 1071 of file ac3dec.c.

References AC3_CHMODE_MONO, AC3_CHMODE_STEREO, AC3_MAX_FRAME_SIZE, AC3_OUTPUT_LFEON, ac3_parse_audio_block(), AC3_PARSE_ERROR_BSID, AC3_PARSE_ERROR_CRC, AC3_PARSE_ERROR_FRAME_SIZE, AC3_PARSE_ERROR_FRAME_TYPE, AC3_PARSE_ERROR_SAMPLE_RATE, AC3_PARSE_ERROR_SYNC, av_crc(), AV_CRC_16_ANSI, av_crc_get_table(), av_log(), AV_LOG_ERROR, AC3DecodeContext::bit_rate, AVCodecContext::bit_rate, blk, AC3DecodeContext::channel_mode, AVCodecContext::channels, AC3DecodeContext::channels, EAC3_FRAME_TYPE_DEPENDENT, AVCodecContext::error_resilience, AC3DecodeContext::fbw_channels, FF_ER_CAREFUL, FFMIN, AC3DecodeContext::frame_size, AC3DecodeContext::frame_type, AC3DecodeContext::gbc, init_get_bits(), AC3DecodeContext::input_buffer, AC3DecodeContext::lfe_on, AC3DecodeContext::num_blocks, AC3DecodeContext::out_channels, AC3DecodeContext::output_mode, parse_frame_header(), AVCodecContext::priv_data, AVCodecContext::request_channels, AC3DecodeContext::sample_rate, AVCodecContext::sample_rate, set_downmix_coeffs(), and AC3DecodeContext::substreamid.

01073 {
01074     AC3DecodeContext *s = avctx->priv_data;
01075     int16_t *out_samples = (int16_t *)data;
01076     int i, blk, ch, err;
01077 
01078     /* initialize the GetBitContext with the start of valid AC-3 Frame */
01079     if (s->input_buffer) {
01080         /* copy input buffer to decoder context to avoid reading past the end
01081            of the buffer, which can be caused by a damaged input stream. */
01082         memcpy(s->input_buffer, buf, FFMIN(buf_size, AC3_MAX_FRAME_SIZE));
01083         init_get_bits(&s->gbc, s->input_buffer, buf_size * 8);
01084     } else {
01085         init_get_bits(&s->gbc, buf, buf_size * 8);
01086     }
01087 
01088     /* parse the syncinfo */
01089     *data_size = 0;
01090     err = parse_frame_header(s);
01091 
01092     /* check that reported frame size fits in input buffer */
01093     if(s->frame_size > buf_size) {
01094         av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
01095         err = AC3_PARSE_ERROR_FRAME_SIZE;
01096     }
01097 
01098     /* check for crc mismatch */
01099     if(err != AC3_PARSE_ERROR_FRAME_SIZE && avctx->error_resilience >= FF_ER_CAREFUL) {
01100         if(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2], s->frame_size-2)) {
01101             av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
01102             err = AC3_PARSE_ERROR_CRC;
01103         }
01104     }
01105 
01106     if(err && err != AC3_PARSE_ERROR_CRC) {
01107         switch(err) {
01108             case AC3_PARSE_ERROR_SYNC:
01109                 av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
01110                 return -1;
01111             case AC3_PARSE_ERROR_BSID:
01112                 av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");
01113                 break;
01114             case AC3_PARSE_ERROR_SAMPLE_RATE:
01115                 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
01116                 break;
01117             case AC3_PARSE_ERROR_FRAME_SIZE:
01118                 av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");
01119                 break;
01120             case AC3_PARSE_ERROR_FRAME_TYPE:
01121                 /* skip frame if CRC is ok. otherwise use error concealment. */
01122                 /* TODO: add support for substreams and dependent frames */
01123                 if(s->frame_type == EAC3_FRAME_TYPE_DEPENDENT || s->substreamid) {
01124                     av_log(avctx, AV_LOG_ERROR, "unsupported frame type : skipping frame\n");
01125                     return s->frame_size;
01126                 } else {
01127                     av_log(avctx, AV_LOG_ERROR, "invalid frame type\n");
01128                 }
01129                 break;
01130             default:
01131                 av_log(avctx, AV_LOG_ERROR, "invalid header\n");
01132                 break;
01133         }
01134     }
01135 
01136     /* if frame is ok, set audio parameters */
01137     if (!err) {
01138         avctx->sample_rate = s->sample_rate;
01139         avctx->bit_rate = s->bit_rate;
01140 
01141         /* channel config */
01142         s->out_channels = s->channels;
01143         s->output_mode = s->channel_mode;
01144         if(s->lfe_on)
01145             s->output_mode |= AC3_OUTPUT_LFEON;
01146         if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
01147                 avctx->request_channels < s->channels) {
01148             s->out_channels = avctx->request_channels;
01149             s->output_mode  = avctx->request_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
01150         }
01151         avctx->channels = s->out_channels;
01152 
01153         /* set downmixing coefficients if needed */
01154         if(s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
01155                 s->fbw_channels == s->out_channels)) {
01156             set_downmix_coeffs(s);
01157         }
01158     } else if (!s->out_channels) {
01159         s->out_channels = avctx->channels;
01160         if(s->out_channels < s->channels)
01161             s->output_mode  = s->out_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
01162     }
01163 
01164     /* parse the audio blocks */
01165     for (blk = 0; blk < s->num_blocks; blk++) {
01166         if (!err && ac3_parse_audio_block(s, blk)) {
01167             av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
01168         }
01169 
01170         /* interleave output samples */
01171         for (i = 0; i < 256; i++)
01172             for (ch = 0; ch < s->out_channels; ch++)
01173                 *(out_samples++) = s->int_output[ch][i];
01174     }
01175     *data_size = s->num_blocks * 256 * avctx->channels * sizeof (int16_t);
01176     return s->frame_size;
01177 }

static av_cold int ac3_decode_init ( AVCodecContext avctx  )  [static]

AVCodec initialization

Definition at line 187 of file ac3dec.c.

References ac3_common_init(), AC3_MAX_FRAME_SIZE, ac3_tables_init(), AC3DecodeContext::add_bias, av_init_random(), av_mallocz(), AC3DecodeContext::avctx, AVERROR_NOMEM, AVCodecContext::channels, AC3DecodeContext::dith_state, AC3DecodeContext::downmixed, AC3DecodeContext::dsp, dsputil_init(), AVCodecContext::error_resilience, FF_ER_CAREFUL, ff_float_to_int16_c(), FF_INPUT_BUFFER_PADDING_SIZE, ff_kbd_window_init(), ff_mdct_init(), DSPContext::float_to_int16, AC3DecodeContext::imdct_256, AC3DecodeContext::imdct_512, AC3DecodeContext::input_buffer, AC3DecodeContext::mul_bias, AVCodecContext::priv_data, and AVCodecContext::request_channels.

00188 {
00189     AC3DecodeContext *s = avctx->priv_data;
00190     s->avctx = avctx;
00191 
00192     ac3_common_init();
00193     ac3_tables_init();
00194     ff_mdct_init(&s->imdct_256, 8, 1);
00195     ff_mdct_init(&s->imdct_512, 9, 1);
00196     ff_kbd_window_init(s->window, 5.0, 256);
00197     dsputil_init(&s->dsp, avctx);
00198     av_init_random(0, &s->dith_state);
00199 
00200     /* set bias values for float to int16 conversion */
00201     if(s->dsp.float_to_int16 == ff_float_to_int16_c) {
00202         s->add_bias = 385.0f;
00203         s->mul_bias = 1.0f;
00204     } else {
00205         s->add_bias = 0.0f;
00206         s->mul_bias = 32767.0f;
00207     }
00208 
00209     /* allow downmixing to stereo or mono */
00210     if (avctx->channels > 0 && avctx->request_channels > 0 &&
00211             avctx->request_channels < avctx->channels &&
00212             avctx->request_channels <= 2) {
00213         avctx->channels = avctx->request_channels;
00214     }
00215     s->downmixed = 1;
00216 
00217     /* allocate context input buffer */
00218     if (avctx->error_resilience >= FF_ER_CAREFUL) {
00219         s->input_buffer = av_mallocz(AC3_MAX_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
00220         if (!s->input_buffer)
00221             return AVERROR_NOMEM;
00222     }
00223 
00224     return 0;
00225 }

static void ac3_downmix ( AC3DecodeContext s,
float  samples[AC3_MAX_CHANNELS][256],
int  ch_offset 
) [static]

Downmix the output to mono or stereo.

Definition at line 659 of file ac3dec.c.

References AC3_CHMODE_MONO, AC3_CHMODE_STEREO, AC3DecodeContext::downmix_coeff_adjust, AC3DecodeContext::downmix_coeffs, AC3DecodeContext::fbw_channels, LEVEL_MINUS_3DB, AC3DecodeContext::output_mode, samples, and v0.

00661 {
00662     int i, j;
00663     float v0, v1;
00664 
00665     for(i=0; i<256; i++) {
00666         v0 = v1 = 0.0f;
00667         for(j=0; j<s->fbw_channels; j++) {
00668             v0 += samples[j+ch_offset][i] * s->downmix_coeffs[j][0];
00669             v1 += samples[j+ch_offset][i] * s->downmix_coeffs[j][1];
00670         }
00671         v0 *= s->downmix_coeff_adjust[0];
00672         v1 *= s->downmix_coeff_adjust[1];
00673         if(s->output_mode == AC3_CHMODE_MONO) {
00674             samples[ch_offset][i] = (v0 + v1) * LEVEL_MINUS_3DB;
00675         } else if(s->output_mode == AC3_CHMODE_STEREO) {
00676             samples[  ch_offset][i] = v0;
00677             samples[1+ch_offset][i] = v1;
00678         }
00679     }
00680 }

static int ac3_parse_audio_block ( AC3DecodeContext s,
int  blk 
) [static]

Parse an audio block from AC-3 bitstream.

Definition at line 713 of file ac3dec.c.

References AC3_CHMODE_STEREO, AC3_MAX_CHANNELS, av_log(), AV_LOG_ERROR, AC3DecodeContext::avctx, AC3DecodeContext::block_switch, AC3DecodeContext::channel_in_cpl, AC3DecodeContext::channel_mode, AC3DecodeContext::cpl_band_struct, CPL_CH, AC3DecodeContext::cpl_coords, AC3DecodeContext::cpl_in_use, AC3DecodeContext::dither_all, AC3DecodeContext::dither_flag, AVCodecContext::drc_scale, AC3DecodeContext::dynamic_range, dynamic_range_tab, AC3DecodeContext::end_freq, AC3DecodeContext::fbw_channels, AC3DecodeContext::gbc, get_bits(), get_bits1(), AC3DecodeContext::num_cpl_bands, AC3DecodeContext::num_cpl_subbands, AC3DecodeContext::phase_flags, AC3DecodeContext::phase_flags_in_use, and AC3DecodeContext::start_freq.

Referenced by ac3_decode_frame().

00714 {
00715     int fbw_channels = s->fbw_channels;
00716     int channel_mode = s->channel_mode;
00717     int i, bnd, seg, ch;
00718     int different_transforms;
00719     int downmix_output;
00720     int cpl_in_use;
00721     GetBitContext *gbc = &s->gbc;
00722     uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
00723 
00724     memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
00725 
00726     /* block switch flags */
00727     different_transforms = 0;
00728     for (ch = 1; ch <= fbw_channels; ch++) {
00729         s->block_switch[ch] = get_bits1(gbc);
00730         if(ch > 1 && s->block_switch[ch] != s->block_switch[1])
00731             different_transforms = 1;
00732     }
00733 
00734     /* dithering flags */
00735     s->dither_all = 1;
00736     for (ch = 1; ch <= fbw_channels; ch++) {
00737         s->dither_flag[ch] = get_bits1(gbc);
00738         if(!s->dither_flag[ch])
00739             s->dither_all = 0;
00740     }
00741 
00742     /* dynamic range */
00743     i = !(s->channel_mode);
00744     do {
00745         if(get_bits1(gbc)) {
00746             s->dynamic_range[i] = ((dynamic_range_tab[get_bits(gbc, 8)]-1.0) *
00747                                   s->avctx->drc_scale)+1.0;
00748         } else if(blk == 0) {
00749             s->dynamic_range[i] = 1.0f;
00750         }
00751     } while(i--);
00752 
00753     /* coupling strategy */
00754     if (get_bits1(gbc)) {
00755         memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00756         s->cpl_in_use[blk] = get_bits1(gbc);
00757         if (s->cpl_in_use[blk]) {
00758             /* coupling in use */
00759             int cpl_begin_freq, cpl_end_freq;
00760 
00761             if (channel_mode < AC3_CHMODE_STEREO) {
00762                 av_log(s->avctx, AV_LOG_ERROR, "coupling not allowed in mono or dual-mono\n");
00763                 return -1;
00764             }
00765 
00766             /* determine which channels are coupled */
00767             for (ch = 1; ch <= fbw_channels; ch++)
00768                 s->channel_in_cpl[ch] = get_bits1(gbc);
00769 
00770             /* phase flags in use */
00771             if (channel_mode == AC3_CHMODE_STEREO)
00772                 s->phase_flags_in_use = get_bits1(gbc);
00773 
00774             /* coupling frequency range and band structure */
00775             cpl_begin_freq = get_bits(gbc, 4);
00776             cpl_end_freq = get_bits(gbc, 4);
00777             if (3 + cpl_end_freq - cpl_begin_freq < 0) {
00778                 av_log(s->avctx, AV_LOG_ERROR, "3+cplendf = %d < cplbegf = %d\n", 3+cpl_end_freq, cpl_begin_freq);
00779                 return -1;
00780             }
00781             s->num_cpl_bands = s->num_cpl_subbands = 3 + cpl_end_freq - cpl_begin_freq;
00782             s->start_freq[CPL_CH] = cpl_begin_freq * 12 + 37;
00783             s->end_freq[CPL_CH] = cpl_end_freq * 12 + 73;
00784             for (bnd = 0; bnd < s->num_cpl_subbands - 1; bnd++) {
00785                 if (get_bits1(gbc)) {
00786                     s->cpl_band_struct[bnd] = 1;
00787                     s->num_cpl_bands--;
00788                 }
00789             }
00790             s->cpl_band_struct[s->num_cpl_subbands-1] = 0;
00791         } else {
00792             /* coupling not in use */
00793             for (ch = 1; ch <= fbw_channels; ch++)
00794                 s->channel_in_cpl[ch] = 0;
00795         }
00796     } else if (!blk) {
00797         av_log(s->avctx, AV_LOG_ERROR, "new coupling strategy must be present in block 0\n");
00798         return -1;
00799     } else {
00800         s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
00801     }
00802     cpl_in_use = s->cpl_in_use[blk];
00803 
00804     /* coupling coordinates */
00805     if (cpl_in_use) {
00806         int cpl_coords_exist = 0;
00807 
00808         for (ch = 1; ch <= fbw_channels; ch++) {
00809             if (s->channel_in_cpl[ch]) {
00810                 if (get_bits1(gbc)) {
00811                     int master_cpl_coord, cpl_coord_exp, cpl_coord_mant;
00812                     cpl_coords_exist = 1;
00813                     master_cpl_coord = 3 * get_bits(gbc, 2);
00814                     for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00815                         cpl_coord_exp = get_bits(gbc, 4);
00816                         cpl_coord_mant = get_bits(gbc, 4);
00817                         if (cpl_coord_exp == 15)
00818                             s->cpl_coords[ch][bnd] = cpl_coord_mant << 22;
00819                         else
00820                             s->cpl_coords[ch][bnd] = (cpl_coord_mant + 16) << 21;
00821                         s->cpl_coords[ch][bnd] >>= (cpl_coord_exp + master_cpl_coord);
00822                     }
00823                 } else if (!blk) {
00824                     av_log(s->avctx, AV_LOG_ERROR, "new coupling coordinates must be present in block 0\n");
00825                     return -1;
00826                 }
00827             }
00828         }
00829         /* phase flags */
00830         if (channel_mode == AC3_CHMODE_STEREO && cpl_coords_exist) {
00831             for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
00832                 s->phase_flags[bnd] = s->phase_flags_in_use? get_bits1(gbc) : 0;
00833             }
00834         }
00835     }
00836 
00837     /* stereo rematrixing strategy and band structure */
00838     if (channel_mode == AC3_CHMODE_STEREO) {
00839         if (get_bits1(gbc)) {
00840             s->num_rematrixing_bands = 4;
00841             if(cpl_in_use && s->start_freq[CPL_CH] <= 61)
00842                 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37);
00843             for(bnd=0; bnd<s->num_rematrixing_bands; bnd++)
00844                 s->rematrixing_flags[bnd] = get_bits1(gbc);
00845         } else if (!blk) {
00846             av_log(s->avctx, AV_LOG_ERROR, "new rematrixing strategy must be present in block 0\n");
00847             return -1;
00848         }
00849     }
00850 
00851     /* exponent strategies for each channel */
00852     s->exp_strategy[blk][CPL_CH] = EXP_REUSE;
00853     s->exp_strategy[blk][s->lfe_ch] = EXP_REUSE;
00854     for (ch = !cpl_in_use; ch <= s->channels; ch++) {
00855         s->exp_strategy[blk][ch] = get_bits(gbc, 2 - (ch == s->lfe_ch));
00856         if(s->exp_strategy[blk][ch] != EXP_REUSE)
00857             bit_alloc_stages[ch] = 3;
00858     }
00859 
00860     /* channel bandwidth */
00861     for (ch = 1; ch <= fbw_channels; ch++) {
00862         s->start_freq[ch] = 0;
00863         if (s->exp_strategy[blk][ch] != EXP_REUSE) {
00864             int group_size;
00865             int prev = s->end_freq[ch];
00866             if (s->channel_in_cpl[ch])
00867                 s->end_freq[ch] = s->start_freq[CPL_CH];
00868             else {
00869                 int bandwidth_code = get_bits(gbc, 6);
00870                 if (bandwidth_code > 60) {
00871                     av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60", bandwidth_code);
00872                     return -1;
00873                 }
00874                 s->end_freq[ch] = bandwidth_code * 3 + 73;
00875             }
00876             group_size = 3 << (s->exp_strategy[blk][ch] - 1);
00877             s->num_exp_groups[ch] = (s->end_freq[ch]+group_size-4) / group_size;
00878             if(blk > 0 && s->end_freq[ch] != prev)
00879                 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00880         }
00881     }
00882     if (cpl_in_use && s->exp_strategy[blk][CPL_CH] != EXP_REUSE) {
00883         s->num_exp_groups[CPL_CH] = (s->end_freq[CPL_CH] - s->start_freq[CPL_CH]) /
00884                                     (3 << (s->exp_strategy[blk][CPL_CH] - 1));
00885     }
00886 
00887     /* decode exponents for each channel */
00888     for (ch = !cpl_in_use; ch <= s->channels; ch++) {
00889         if (s->exp_strategy[blk][ch] != EXP_REUSE) {
00890             s->dexps[ch][0] = get_bits(gbc, 4) << !ch;
00891             decode_exponents(gbc, s->exp_strategy[blk][ch],
00892                              s->num_exp_groups[ch], s->dexps[ch][0],
00893                              &s->dexps[ch][s->start_freq[ch]+!!ch]);
00894             if(ch != CPL_CH && ch != s->lfe_ch)
00895                 skip_bits(gbc, 2); /* skip gainrng */
00896         }
00897     }
00898 
00899     /* bit allocation information */
00900     if (get_bits1(gbc)) {
00901         s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
00902         s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
00903         s->bit_alloc_params.slow_gain  = ff_ac3_slow_gain_tab[get_bits(gbc, 2)];
00904         s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gbc, 2)];
00905         s->bit_alloc_params.floor  = ff_ac3_floor_tab[get_bits(gbc, 3)];
00906         for(ch=!cpl_in_use; ch<=s->channels; ch++)
00907             bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
00908     } else if (!blk) {
00909         av_log(s->avctx, AV_LOG_ERROR, "new bit allocation info must be present in block 0\n");
00910         return -1;
00911     }
00912 
00913     /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */
00914     if (get_bits1(gbc)) {
00915         int csnr;
00916         csnr = (get_bits(gbc, 6) - 15) << 4;
00917         for (ch = !cpl_in_use; ch <= s->channels; ch++) { /* snr offset and fast gain */
00918             s->snr_offset[ch] = (csnr + get_bits(gbc, 4)) << 2;
00919             s->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gbc, 3)];
00920         }
00921         memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
00922     } else if (!blk) {
00923         av_log(s->avctx, AV_LOG_ERROR, "new snr offsets must be present in block 0\n");
00924         return -1;
00925     }
00926 
00927     /* coupling leak information */
00928     if (cpl_in_use) {
00929         if (get_bits1(gbc)) {
00930             s->bit_alloc_params.cpl_fast_leak = get_bits(gbc, 3);
00931             s->bit_alloc_params.cpl_slow_leak = get_bits(gbc, 3);
00932             bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
00933         } else if (!blk) {
00934             av_log(s->avctx, AV_LOG_ERROR, "new coupling leak info must be present in block 0\n");
00935             return -1;
00936         }
00937     }
00938 
00939     /* delta bit allocation information */
00940     if (get_bits1(gbc)) {
00941         /* delta bit allocation exists (strategy) */
00942         for (ch = !cpl_in_use; ch <= fbw_channels; ch++) {
00943             s->dba_mode[ch] = get_bits(gbc, 2);
00944             if (s->dba_mode[ch] == DBA_RESERVED) {
00945                 av_log(s->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
00946                 return -1;
00947             }
00948             bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
00949         }
00950         /* channel delta offset, len and bit allocation */
00951         for (ch = !cpl_in_use; ch <= fbw_channels; ch++) {
00952             if (s->dba_mode[ch] == DBA_NEW) {
00953                 s->dba_nsegs[ch] = get_bits(gbc, 3);
00954                 for (seg = 0; seg <= s->dba_nsegs[ch]; seg++) {
00955                     s->dba_offsets[ch][seg] = get_bits(gbc, 5);
00956                     s->dba_lengths[ch][seg] = get_bits(gbc, 4);
00957                     s->dba_values[ch][seg] = get_bits(gbc, 3);
00958                 }
00959                 /* run last 2 bit allocation stages if new dba values */
00960                 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
00961             }
00962         }
00963     } else if(blk == 0) {
00964         for(ch=0; ch<=s->channels; ch++) {
00965             s->dba_mode[ch] = DBA_NONE;
00966         }
00967     }
00968 
00969     /* Bit allocation */
00970     for(ch=!cpl_in_use; ch<=s->channels; ch++) {
00971         if(bit_alloc_stages[ch] > 2) {
00972             /* Exponent mapping into PSD and PSD integration */
00973             ff_ac3_bit_alloc_calc_psd(s->dexps[ch],
00974                                       s->start_freq[ch], s->end_freq[ch],
00975                                       s->psd[ch], s->band_psd[ch]);
00976         }
00977         if(bit_alloc_stages[ch] > 1) {
00978             /* Compute excitation function, Compute masking curve, and
00979                Apply delta bit allocation */
00980             ff_ac3_bit_alloc_calc_mask(&s->bit_alloc_params, s->band_psd[ch],
00981                                        s->start_freq[ch], s->end_freq[ch],
00982                                        s->fast_gain[ch], (ch == s->lfe_ch),
00983                                        s->dba_mode[ch], s->dba_nsegs[ch],
00984                                        s->dba_offsets[ch], s->dba_lengths[ch],
00985                                        s->dba_values[ch], s->mask[ch]);
00986         }
00987         if(bit_alloc_stages[ch] > 0) {
00988             /* Compute bit allocation */
00989             ff_ac3_bit_alloc_calc_bap(s->mask[ch], s->psd[ch],
00990                                       s->start_freq[ch], s->end_freq[ch],
00991                                       s->snr_offset[ch],
00992                                       s->bit_alloc_params.floor,
00993                                       ff_ac3_bap_tab, s->bap[ch]);
00994         }
00995     }
00996 
00997     /* unused dummy data */
00998     if (get_bits1(gbc)) {
00999         int skipl = get_bits(gbc, 9);
01000         while(skipl--)
01001             skip_bits(gbc, 8);
01002     }
01003 
01004     /* unpack the transform coefficients
01005        this also uncouples channels if coupling is in use. */
01006     get_transform_coeffs(s);
01007 
01008     /* recover coefficients if rematrixing is in use */
01009     if(s->channel_mode == AC3_CHMODE_STEREO)
01010         do_rematrixing(s);
01011 
01012     /* apply scaling to coefficients (headroom, dynrng) */
01013     for(ch=1; ch<=s->channels; ch++) {
01014         float gain = s->mul_bias / 4194304.0f;
01015         if(s->channel_mode == AC3_CHMODE_DUALMONO) {
01016             gain *= s->dynamic_range[ch-1];
01017         } else {
01018             gain *= s->dynamic_range[0];
01019         }
01020         for(i=0; i<256; i++) {
01021             s->transform_coeffs[ch][i] = s->fixed_coeffs[ch][i] * gain;
01022         }
01023     }
01024 
01025     /* downmix and MDCT. order depends on whether block switching is used for
01026        any channel in this block. this is because coefficients for the long
01027        and short transforms cannot be mixed. */
01028     downmix_output = s->channels != s->out_channels &&
01029                      !((s->output_mode & AC3_OUTPUT_LFEON) &&
01030                      s->fbw_channels == s->out_channels);
01031     if(different_transforms) {
01032         /* the delay samples have already been downmixed, so we upmix the delay
01033            samples in order to reconstruct all channels before downmixing. */
01034         if(s->downmixed) {
01035             s->downmixed = 0;
01036             ac3_upmix_delay(s);
01037         }
01038 
01039         do_imdct(s, s->channels);
01040 
01041         if(downmix_output) {
01042             ac3_downmix(s, s->output, 0);
01043         }
01044     } else {
01045         if(downmix_output) {
01046             ac3_downmix(s, s->transform_coeffs, 1);
01047         }
01048 
01049         if(!s->downmixed) {
01050             s->downmixed = 1;
01051             ac3_downmix(s, s->delay, 0);
01052         }
01053 
01054         do_imdct(s, s->out_channels);
01055     }
01056 
01057     /* convert float to 16-bit integer */
01058     for(ch=0; ch<s->out_channels; ch++) {
01059         for(i=0; i<256; i++) {
01060             s->output[ch][i] += s->add_bias;
01061         }
01062         s->dsp.float_to_int16(s->int_output[ch], s->output[ch], 256);
01063     }
01064 
01065     return 0;
01066 }

static int ac3_parse_header ( AC3DecodeContext s  )  [static]

Parse the 'sync info' and 'bit stream info' from the AC-3 bitstream. GetBitContext within AC3DecodeContext must point to start of the synchronized ac3 bitstream.

Definition at line 232 of file ac3dec.c.

References AC3DecodeContext::channel_mode, AC3DecodeContext::gbc, get_bits(), get_bits1(), and skip_bits().

Referenced by parse_frame_header().

00233 {
00234     GetBitContext *gbc = &s->gbc;
00235     int i;
00236 
00237     /* read the rest of the bsi. read twice for dual mono mode. */
00238     i = !(s->channel_mode);
00239     do {
00240         skip_bits(gbc, 5); // skip dialog normalization
00241         if (get_bits1(gbc))
00242             skip_bits(gbc, 8); //skip compression
00243         if (get_bits1(gbc))
00244             skip_bits(gbc, 8); //skip language code
00245         if (get_bits1(gbc))
00246             skip_bits(gbc, 7); //skip audio production information
00247     } while (i--);
00248 
00249     skip_bits(gbc, 2); //skip copyright bit and original bitstream bit
00250 
00251     /* skip the timecodes (or extra bitstream information for Alternate Syntax)
00252        TODO: read & use the xbsi1 downmix levels */
00253     if (get_bits1(gbc))
00254         skip_bits(gbc, 14); //skip timecode1 / xbsi1
00255     if (get_bits1(gbc))
00256         skip_bits(gbc, 14); //skip timecode2 / xbsi2
00257 
00258     /* skip additional bitstream info */
00259     if (get_bits1(gbc)) {
00260         i = get_bits(gbc, 6);
00261         do {
00262             skip_bits(gbc, 8);
00263         } while(i--);
00264     }
00265 
00266     return 0;
00267 }

static av_cold void ac3_tables_init ( void   )  [static]

Definition at line 134 of file ac3dec.c.

References b1_mantissas, b2_mantissas, b3_mantissas, b4_mantissas, b5_mantissas, dynamic_range_tab, exp_ungroup_tab, and symmetric_dequant().

Referenced by ac3_decode_init().

00135 {
00136     int i;
00137 
00138     /* generate grouped mantissa tables
00139        reference: Section 7.3.5 Ungrouping of Mantissas */
00140     for(i=0; i<32; i++) {
00141         /* bap=1 mantissas */
00142         b1_mantissas[i][0] = symmetric_dequant( i / 9     , 3);
00143         b1_mantissas[i][1] = symmetric_dequant((i % 9) / 3, 3);
00144         b1_mantissas[i][2] = symmetric_dequant((i % 9) % 3, 3);
00145     }
00146     for(i=0; i<128; i++) {
00147         /* bap=2 mantissas */
00148         b2_mantissas[i][0] = symmetric_dequant( i / 25     , 5);
00149         b2_mantissas[i][1] = symmetric_dequant((i % 25) / 5, 5);
00150         b2_mantissas[i][2] = symmetric_dequant((i % 25) % 5, 5);
00151 
00152         /* bap=4 mantissas */
00153         b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
00154         b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
00155     }
00156     /* generate ungrouped mantissa tables
00157        reference: Tables 7.21 and 7.23 */
00158     for(i=0; i<7; i++) {
00159         /* bap=3 mantissas */
00160         b3_mantissas[i] = symmetric_dequant(i, 7);
00161     }
00162     for(i=0; i<15; i++) {
00163         /* bap=5 mantissas */
00164         b5_mantissas[i] = symmetric_dequant(i, 15);
00165     }