From 55736cfbd22e0a7c10a85eb0d5cc7a22bf841322 Mon Sep 17 00:00:00 2001 From: Bartlomiej Wolowiec Date: Tue, 22 Apr 2008 11:14:01 +0000 Subject: [PATCH] change ff_ac3_parse_header() to take a GetBitContext instead of const char* Originally committed as revision 12922 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/ac3.h | 2 ++ libavcodec/ac3_parser.c | 73 +++++++++++++++++++++++++---------------- libavcodec/ac3_parser.h | 5 +-- libavcodec/ac3dec.c | 35 ++------------------ libavformat/raw.c | 5 ++- 5 files changed, 56 insertions(+), 64 deletions(-) diff --git a/libavcodec/ac3.h b/libavcodec/ac3.h index 0d1a850c9e..cc91f20191 100644 --- a/libavcodec/ac3.h +++ b/libavcodec/ac3.h @@ -95,6 +95,8 @@ typedef struct { uint32_t bit_rate; uint8_t channels; uint16_t frame_size; + int center_mix_level; ///< Center mix level index + int surround_mix_level; ///< Surround mix level index /** @} */ } AC3HeaderInfo; diff --git a/libavcodec/ac3_parser.c b/libavcodec/ac3_parser.c index f0b640dc4a..471fb4f6e2 100644 --- a/libavcodec/ac3_parser.c +++ b/libavcodec/ac3_parser.c @@ -33,51 +33,64 @@ static const uint8_t eac3_blocks[4] = { 1, 2, 3, 6 }; +/** + * Table for center mix levels + * reference: Section 5.4.2.4 cmixlev + */ +static const uint8_t center_levels[4] = { 2, 3, 4, 3 }; -int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr) +/** + * Table for surround mix levels + * reference: Section 5.4.2.5 surmixlev + */ +static const uint8_t surround_levels[4] = { 2, 4, 0, 4 }; + + +int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr) { - GetBitContext gbc; int frame_size_code; int num_blocks; memset(hdr, 0, sizeof(*hdr)); - init_get_bits(&gbc, buf, 54); - - hdr->sync_word = get_bits(&gbc, 16); + hdr->sync_word = get_bits(gbc, 16); if(hdr->sync_word != 0x0B77) return AC3_PARSE_ERROR_SYNC; /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */ - hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F; + hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F; if(hdr->bitstream_id > 16) return AC3_PARSE_ERROR_BSID; if(hdr->bitstream_id <= 10) { /* Normal AC-3 */ - hdr->crc1 = get_bits(&gbc, 16); - hdr->sr_code = get_bits(&gbc, 2); + hdr->crc1 = get_bits(gbc, 16); + hdr->sr_code = get_bits(gbc, 2); if(hdr->sr_code == 3) return AC3_PARSE_ERROR_SAMPLE_RATE; - frame_size_code = get_bits(&gbc, 6); + frame_size_code = get_bits(gbc, 6); if(frame_size_code > 37) return AC3_PARSE_ERROR_FRAME_SIZE; - skip_bits(&gbc, 5); // skip bsid, already got it + skip_bits(gbc, 5); // skip bsid, already got it + + skip_bits(gbc, 3); // skip bitstream mode + hdr->channel_mode = get_bits(gbc, 3); + + /* set default mix levels */ + hdr->center_mix_level = 3; // -4.5dB + hdr->surround_mix_level = 4; // -6.0dB - skip_bits(&gbc, 3); // skip bitstream mode - hdr->channel_mode = get_bits(&gbc, 3); - if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) { - skip_bits(&gbc, 2); // skip center mix level - } - if(hdr->channel_mode & 4) { - skip_bits(&gbc, 2); // skip surround mix level - } if(hdr->channel_mode == AC3_CHMODE_STEREO) { - skip_bits(&gbc, 2); // skip dolby surround mode + skip_bits(gbc, 2); // skip dsurmod + } else { + if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) + hdr->center_mix_level = center_levels[get_bits(gbc, 2)]; + if(hdr->channel_mode & 4) + hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)]; } - hdr->lfe_on = get_bits1(&gbc); + hdr->lfe_on = get_bits1(gbc); hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8; hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift; @@ -88,32 +101,32 @@ int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr) } else { /* Enhanced AC-3 */ hdr->crc1 = 0; - hdr->frame_type = get_bits(&gbc, 2); + hdr->frame_type = get_bits(gbc, 2); if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED) return AC3_PARSE_ERROR_FRAME_TYPE; - skip_bits(&gbc, 3); // skip substream id + skip_bits(gbc, 3); // skip substream id - hdr->frame_size = (get_bits(&gbc, 11) + 1) << 1; + hdr->frame_size = (get_bits(gbc, 11) + 1) << 1; if(hdr->frame_size < AC3_HEADER_SIZE) return AC3_PARSE_ERROR_FRAME_SIZE; - hdr->sr_code = get_bits(&gbc, 2); + hdr->sr_code = get_bits(gbc, 2); if (hdr->sr_code == 3) { - int sr_code2 = get_bits(&gbc, 2); + int sr_code2 = get_bits(gbc, 2); if(sr_code2 == 3) return AC3_PARSE_ERROR_SAMPLE_RATE; hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2; hdr->sr_shift = 1; num_blocks = 6; } else { - num_blocks = eac3_blocks[get_bits(&gbc, 2)]; + num_blocks = eac3_blocks[get_bits(gbc, 2)]; hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code]; hdr->sr_shift = 0; } - hdr->channel_mode = get_bits(&gbc, 3); - hdr->lfe_on = get_bits1(&gbc); + hdr->channel_mode = get_bits(gbc, 3); + hdr->lfe_on = get_bits1(gbc); hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate / (num_blocks * 256.0)); @@ -129,8 +142,10 @@ static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info, int err; uint64_t tmp = be2me_64(state); AC3HeaderInfo hdr; + GetBitContext gbc; - err = ff_ac3_parse_header(((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, &hdr); + init_get_bits(&gbc, ((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, 54); + err = ff_ac3_parse_header(&gbc, &hdr); if(err < 0) return 0; diff --git a/libavcodec/ac3_parser.h b/libavcodec/ac3_parser.h index 1e512bf2b6..3f19b13dd7 100644 --- a/libavcodec/ac3_parser.h +++ b/libavcodec/ac3_parser.h @@ -24,6 +24,7 @@ #define FFMPEG_AC3_PARSER_H #include "ac3.h" +#include "bitstream.h" typedef enum { AC3_PARSE_ERROR_SYNC = -1, @@ -37,12 +38,12 @@ typedef enum { * Parses AC-3 frame header. * Parses the header up to the lfeon element, which is the first 52 or 54 bits * depending on the audio coding mode. - * @param buf[in] Array containing the first 7 bytes of the frame. + * @param gbc[in] BitContext containing the first 54 bits of the frame. * @param hdr[out] Pointer to struct where header info is written. * @return Returns 0 on success, -1 if there is a sync word mismatch, * -2 if the bsid (version) element is invalid, -3 if the fscod (sample rate) * element is invalid, or -4 if the frmsizecod (bit rate) element is invalid. */ -int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr); +int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr); #endif /* FFMPEG_AC3_PARSER_H */ diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fc12f347fb..5852523df7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -88,18 +88,6 @@ static const float gain_levels[6] = { LEVEL_MINUS_9DB }; -/** - * Table for center mix levels - * reference: Section 5.4.2.4 cmixlev - */ -static const uint8_t center_levels[4] = { 2, 3, 4, 3 }; - -/** - * Table for surround mix levels - * reference: Section 5.4.2.5 surmixlev - */ -static const uint8_t surround_levels[4] = { 2, 4, 0, 4 }; - /** * Table for default stereo downmixing coefficients * reference: Section 7.8.2 Downmixing Into Two Channels @@ -315,7 +303,7 @@ static int ac3_parse_header(AC3DecodeContext *s) GetBitContext *gbc = &s->gbc; int err, i; - err = ff_ac3_parse_header(gbc->buffer, &hdr); + err = ff_ac3_parse_header(gbc, &hdr); if(err) return err; @@ -333,6 +321,8 @@ static int ac3_parse_header(AC3DecodeContext *s) s->fbw_channels = s->channels - s->lfe_on; s->lfe_ch = s->fbw_channels + 1; s->frame_size = hdr.frame_size; + s->center_mix_level = hdr.center_mix_level; + s->surround_mix_level = hdr.surround_mix_level; /* set default output to all source channels */ s->out_channels = s->channels; @@ -340,25 +330,6 @@ static int ac3_parse_header(AC3DecodeContext *s) if(s->lfe_on) s->output_mode |= AC3_OUTPUT_LFEON; - /* set default mix levels */ - s->center_mix_level = 3; // -4.5dB - s->surround_mix_level = 4; // -6.0dB - - /* skip over portion of header which has already been read */ - skip_bits(gbc, 16); // skip the sync_word - skip_bits(gbc, 16); // skip crc1 - skip_bits(gbc, 8); // skip fscod and frmsizecod - skip_bits(gbc, 11); // skip bsid, bsmod, and acmod - if(s->channel_mode == AC3_CHMODE_STEREO) { - skip_bits(gbc, 2); // skip dsurmod - } else { - if((s->channel_mode & 1) && s->channel_mode != AC3_CHMODE_MONO) - s->center_mix_level = center_levels[get_bits(gbc, 2)]; - if(s->channel_mode & 4) - s->surround_mix_level = surround_levels[get_bits(gbc, 2)]; - } - skip_bits1(gbc); // skip lfeon - /* read the rest of the bsi. read twice for dual mono mode. */ i = !(s->channel_mode); do { diff --git a/libavformat/raw.c b/libavformat/raw.c index 5c188dc92b..2f7a32e673 100644 --- a/libavformat/raw.c +++ b/libavformat/raw.c @@ -23,6 +23,7 @@ #include "ac3_parser.h" #include "raw.h" #include "crc.h" +#include "bitstream.h" #ifdef CONFIG_MUXERS /* simple formats */ @@ -419,6 +420,7 @@ static int ac3_probe(AVProbeData *p) int max_frames, first_frames = 0, frames; uint8_t *buf, *buf2, *end; AC3HeaderInfo hdr; + GetBitContext gbc; max_frames = 0; buf = p->buf; @@ -428,7 +430,8 @@ static int ac3_probe(AVProbeData *p) buf2 = buf; for(frames = 0; buf2 < end; frames++) { - if(ff_ac3_parse_header(buf2, &hdr) < 0) + init_get_bits(&gbc, buf2, 54); + if(ff_ac3_parse_header(&gbc, &hdr) < 0) break; if(buf2 + hdr.frame_size > end || av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, hdr.frame_size - 2))