latmdec: fix audio specific config parsing

Pass the correct size in bits to mpeg4audio_get_config and add a flag
to disable parsing of the sync extension when the size is not known.

Latm with AudioMuxVersion 0 does not specify the size of the audio
specific config. Data after the audio specific config can be
misinterpreted as sync extension resulting in random and wrong configs.
This commit is contained in:
Janne Grunau 2011-10-21 17:25:30 +02:00
parent 0eea212943
commit fd095539d1
10 changed files with 40 additions and 25 deletions

View File

@ -452,15 +452,17 @@ static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
* @param ac pointer to AACContext, may be null
* @param avctx pointer to AVCCodecContext, used for logging
* @param m4ac pointer to MPEG4AudioConfig, used for parsing
* @param data pointer to AVCodecContext extradata
* @param data_size size of AVCCodecContext extradata
* @param data pointer to buffer holding an audio specific config
* @param bit_size size of audio specific config or data in bits
* @param sync_extension look for an appended sync extension
*
* @return Returns error status or number of consumed bits. <0 - error
*/
static int decode_audio_specific_config(AACContext *ac,
AVCodecContext *avctx,
MPEG4AudioConfig *m4ac,
const uint8_t *data, int data_size)
const uint8_t *data, int bit_size,
int sync_extension)
{
GetBitContext gb;
int i;
@ -470,9 +472,9 @@ static int decode_audio_specific_config(AACContext *ac,
av_dlog(avctx, "%02x ", avctx->extradata[i]);
av_dlog(avctx, "\n");
init_get_bits(&gb, data, data_size * 8);
init_get_bits(&gb, data, bit_size);
if ((i = avpriv_mpeg4audio_get_config(m4ac, data, data_size)) < 0)
if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)
return -1;
if (m4ac->sampling_index > 12) {
av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
@ -572,7 +574,7 @@ static av_cold int aac_decode_init(AVCodecContext *avctx)
if (avctx->extradata_size > 0) {
if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
avctx->extradata,
avctx->extradata_size) < 0)
avctx->extradata_size*8, 1) < 0)
return -1;
} else {
int sr, i;
@ -2315,12 +2317,19 @@ static inline uint32_t latm_get_value(GetBitContext *b)
}
static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
GetBitContext *gb)
GetBitContext *gb, int asclen)
{
AVCodecContext *avctx = latmctx->aac_ctx.avctx;
MPEG4AudioConfig m4ac;
int config_start_bit = get_bits_count(gb);
int bits_consumed, esize;
int config_start_bit = get_bits_count(gb);
int sync_extension = 0;
int bits_consumed, esize;
if (asclen) {
sync_extension = 1;
asclen = FFMIN(asclen, get_bits_left(gb));
} else
asclen = get_bits_left(gb);
if (config_start_bit % 8) {
av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
@ -2330,7 +2339,7 @@ static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
bits_consumed =
decode_audio_specific_config(NULL, avctx, &m4ac,
gb->buffer + (config_start_bit / 8),
get_bits_left(gb) / 8);
asclen, sync_extension);
if (bits_consumed < 0)
return AVERROR_INVALIDDATA;
@ -2388,11 +2397,11 @@ static int read_stream_mux_config(struct LATMContext *latmctx,
// for all but first stream: use_same_config = get_bits(gb, 1);
if (!audio_mux_version) {
if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
return ret;
} else {
int ascLen = latm_get_value(gb);
if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
return ret;
ascLen -= ret;
skip_bits_long(gb, ascLen);
@ -2514,7 +2523,7 @@ static int latm_decode_frame(AVCodecContext *avctx, void *out,
} else {
if ((err = decode_audio_specific_config(
&latmctx->aac_ctx, avctx, &latmctx->aac_ctx.m4ac,
avctx->extradata, avctx->extradata_size)) < 0)
avctx->extradata, avctx->extradata_size*8, 1)) < 0)
return err;
latmctx->initialized = 1;
}

View File

@ -291,7 +291,7 @@ static av_cold int read_specific_config(ALSDecContext *ctx)
init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
config_offset = avpriv_mpeg4audio_get_config(&m4ac, avctx->extradata,
avctx->extradata_size);
avctx->extradata_size * 8, 1);
if (config_offset < 0)
return -1;

View File

@ -76,12 +76,13 @@ static inline int get_sample_rate(GetBitContext *gb, int *index)
avpriv_mpeg4audio_sample_rates[*index];
}
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int buf_size)
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf,
int bit_size, int sync_extension)
{
GetBitContext gb;
int specific_config_bitindex;
init_get_bits(&gb, buf, buf_size*8);
init_get_bits(&gb, buf, bit_size);
c->object_type = get_object_type(&gb);
c->sample_rate = get_sample_rate(&gb, &c->sampling_index);
c->chan_config = get_bits(&gb, 4);
@ -117,7 +118,7 @@ int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int bu
return -1;
}
if (c->ext_object_type != AOT_SBR) {
if (c->ext_object_type != AOT_SBR && sync_extension) {
while (get_bits_left(&gb) > 15) {
if (show_bits(&gb, 11) == 0x2b7) { // sync extension
get_bits(&gb, 11);

View File

@ -42,14 +42,17 @@ typedef struct {
extern const int avpriv_mpeg4audio_sample_rates[16];
extern const uint8_t ff_mpeg4audio_channels[8];
/**
* Parse MPEG-4 systems extradata to retrieve audio configuration.
* @param[in] c MPEG4AudioConfig structure to fill.
* @param[in] buf Extradata from container.
* @param[in] buf_size Extradata size.
* @param[in] bit_size Extradata size in bits.
* @param[in] sync_extension look for a sync extension after config if true.
* @return On error -1 is returned, on success AudioSpecificConfig bit index in extradata.
*/
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int buf_size);
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf,
int bit_size, int sync_extension);
enum AudioObjectType {
AOT_NULL,

View File

@ -1966,7 +1966,8 @@ static int decode_init_mp3on4(AVCodecContext * avctx)
return AVERROR_INVALIDDATA;
}
avpriv_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
avctx->extradata_size * 8, 1);
if (!cfg.chan_config || cfg.chan_config > 7) {
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
return AVERROR_INVALIDDATA;

View File

@ -37,7 +37,7 @@ int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf
int off;
init_get_bits(&gb, buf, size * 8);
off = avpriv_mpeg4audio_get_config(&m4ac, buf, size);
off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
if (off < 0)
return off;
skip_bits_long(&gb, off);

View File

@ -534,7 +534,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
if (st->codec->codec_id == CODEC_ID_AAC) {
MPEG4AudioConfig cfg;
avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
st->codec->extradata_size);
st->codec->extradata_size * 8, 1);
st->codec->channels = cfg.channels;
if (cfg.ext_sample_rate)
st->codec->sample_rate = cfg.ext_sample_rate;

View File

@ -436,7 +436,7 @@ int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext
if (st->codec->codec_id == CODEC_ID_AAC) {
MPEG4AudioConfig cfg;
avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
st->codec->extradata_size);
st->codec->extradata_size * 8, 1);
st->codec->channels = cfg.channels;
if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
st->codec->sample_rate = avpriv_mpa_freq_tab[cfg.sampling_index];

View File

@ -54,7 +54,7 @@ static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
MPEG4AudioConfig m4ac;
init_get_bits(&gb, buf, size * 8);
ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size);
ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
if (ctx->off < 0)
return ctx->off;
skip_bits_long(&gb, ctx->off);

View File

@ -448,7 +448,8 @@ static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int
{
MPEG4AudioConfig mp4ac;
if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata, codec->extradata_size) < 0) {
if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
codec->extradata_size * 8, 1) < 0) {
av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n");
return;
}