1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

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
This commit is contained in:
Bartlomiej Wolowiec 2008-04-22 11:14:01 +00:00
parent 0ec1eb6906
commit 55736cfbd2
5 changed files with 56 additions and 64 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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 */

View File

@ -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 {

View File

@ -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))