diff options
author | Janne Grunau <janne-ffmpeg@jannau.net> | 2010-11-02 08:32:04 +0000 |
---|---|---|
committer | Janne Grunau <janne-ffmpeg@jannau.net> | 2010-11-02 08:32:04 +0000 |
commit | 136e19e1cf4fb1bc7b35e21357dd75c759b8b35e (patch) | |
tree | 886c9fb41e9204bd6fbb74e689778211bb94a5c3 /libavcodec/aacdec.c | |
parent | 6c003e6de835395a07d47ac11b648f6bbf75d419 (diff) | |
download | ffmpeg-136e19e1cf4fb1bc7b35e21357dd75c759b8b35e.tar.gz |
Add single stream LATM/LOAS decoder
The decoder is just a wrapper around the AAC decoder.
based on patch by Paul Kendall { paul <ät> kcbbs gen nz }
Originally committed as revision 25642 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'libavcodec/aacdec.c')
-rw-r--r-- | libavcodec/aacdec.c | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index 7c23bdf0cb..f3073c6023 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -3,6 +3,10 @@ * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org ) * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com ) * + * AAC LATM decoder + * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz> + * Copyright (c) 2010 Janne Grunau <janne-ffmpeg@jannau.net> + * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or @@ -2098,6 +2102,261 @@ static av_cold int aac_decode_close(AVCodecContext *avctx) return 0; } + +#define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word + +struct LATMContext { + AACContext aac_ctx; ///< containing AACContext + int initialized; ///< initilized after a valid extradata was seen + + // parser data + int audio_mux_version_A; ///< LATM syntax version + int frame_length_type; ///< 0/1 variable/fixed frame length + int frame_length; ///< frame length for fixed frame length +}; + +static inline uint32_t latm_get_value(GetBitContext *b) +{ + int length = get_bits(b, 2); + + return get_bits_long(b, (length+1)*8); +} + +static int latm_decode_audio_specific_config(struct LATMContext *latmctx, + GetBitContext *gb) +{ + AVCodecContext *avctx = latmctx->aac_ctx.avctx; + MPEG4AudioConfig m4ac; + int config_start_bit = get_bits_count(gb); + int bits_consumed, esize; + + if (config_start_bit % 8) { + av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific " + "config not byte aligned.\n", 1); + return AVERROR_INVALIDDATA; + } else { + bits_consumed = + decode_audio_specific_config(NULL, avctx, &m4ac, + gb->buffer + (config_start_bit / 8), + get_bits_left(gb) / 8); + + if (bits_consumed < 0) + return AVERROR_INVALIDDATA; + + esize = (bits_consumed+7) / 8; + + if (avctx->extradata_size <= esize) { + av_free(avctx->extradata); + avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE); + if (!avctx->extradata) + return AVERROR(ENOMEM); + } + + avctx->extradata_size = esize; + memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize); + memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE); + } + + return bits_consumed; +} + +static int read_stream_mux_config(struct LATMContext *latmctx, + GetBitContext *gb) +{ + int ret, audio_mux_version = get_bits(gb, 1); + + latmctx->audio_mux_version_A = 0; + if (audio_mux_version) + latmctx->audio_mux_version_A = get_bits(gb, 1); + + if (!latmctx->audio_mux_version_A) { + + if (audio_mux_version) + latm_get_value(gb); // taraFullness + + skip_bits(gb, 1); // allStreamSameTimeFraming + skip_bits(gb, 6); // numSubFrames + // numPrograms + if (get_bits(gb, 4)) { // numPrograms + av_log_missing_feature(latmctx->aac_ctx.avctx, + "multiple programs are not supported\n", 1); + return AVERROR_PATCHWELCOME; + } + + // for each program (which there is only on in DVB) + + // for each layer (which there is only on in DVB) + if (get_bits(gb, 3)) { // numLayer + av_log_missing_feature(latmctx->aac_ctx.avctx, + "multiple layers are not supported\n", 1); + return AVERROR_PATCHWELCOME; + } + + // 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) + return ret; + } else { + int ascLen = latm_get_value(gb); + if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0) + return ret; + ascLen -= ret; + skip_bits_long(gb, ascLen); + } + + latmctx->frame_length_type = get_bits(gb, 3); + switch (latmctx->frame_length_type) { + case 0: + skip_bits(gb, 8); // latmBufferFullness + break; + case 1: + latmctx->frame_length = get_bits(gb, 9); + break; + case 3: + case 4: + case 5: + skip_bits(gb, 6); // CELP frame length table index + break; + case 6: + case 7: + skip_bits(gb, 1); // HVXC frame length table index + break; + } + + if (get_bits(gb, 1)) { // other data + if (audio_mux_version) { + latm_get_value(gb); // other_data_bits + } else { + int esc; + do { + esc = get_bits(gb, 1); + skip_bits(gb, 8); + } while (esc); + } + } + + if (get_bits(gb, 1)) // crc present + skip_bits(gb, 8); // config_crc + } + + return 0; +} + +static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb) +{ + uint8_t tmp; + + if (ctx->frame_length_type == 0) { + int mux_slot_length = 0; + do { + tmp = get_bits(gb, 8); + mux_slot_length += tmp; + } while (tmp == 255); + return mux_slot_length; + } else if (ctx->frame_length_type == 1) { + return ctx->frame_length; + } else if (ctx->frame_length_type == 3 || + ctx->frame_length_type == 5 || + ctx->frame_length_type == 7) { + skip_bits(gb, 2); // mux_slot_length_coded + } + return 0; +} + +static int read_audio_mux_element(struct LATMContext *latmctx, + GetBitContext *gb) +{ + int err; + uint8_t use_same_mux = get_bits(gb, 1); + if (!use_same_mux) { + if ((err = read_stream_mux_config(latmctx, gb)) < 0) + return err; + } else if (!latmctx->aac_ctx.avctx->extradata) { + av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG, + "no decoder config found\n"); + return AVERROR(EAGAIN); + } + if (latmctx->audio_mux_version_A == 0) { + int mux_slot_length_bytes = read_payload_length_info(latmctx, gb); + if (mux_slot_length_bytes * 8 > get_bits_left(gb)) { + av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n"); + return AVERROR_INVALIDDATA; + } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) { + av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, + "frame length mismatch %d << %d\n", + mux_slot_length_bytes * 8, get_bits_left(gb)); + return AVERROR_INVALIDDATA; + } + } + return 0; +} + + +static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size, + AVPacket *avpkt) +{ + struct LATMContext *latmctx = avctx->priv_data; + int muxlength, err; + GetBitContext gb; + + if (avpkt->size == 0) + return 0; + + init_get_bits(&gb, avpkt->data, avpkt->size * 8); + + // check for LOAS sync word + if (get_bits(&gb, 11) != LOAS_SYNC_WORD) + return AVERROR_INVALIDDATA; + + muxlength = get_bits(&gb, 13); + // not enough data, the parser should have sorted this + if (muxlength+3 > avpkt->size) + return AVERROR_INVALIDDATA; + + if ((err = read_audio_mux_element(latmctx, &gb)) < 0) + return err; + + if (!latmctx->initialized) { + if (!avctx->extradata) { + *out_size = 0; + return avpkt->size; + } else { + if ((err = aac_decode_init(avctx)) < 0) + return err; + latmctx->initialized = 1; + } + } + + if (show_bits(&gb, 12) == 0xfff) { + av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, + "ADTS header detected, probably as result of configuration " + "misparsing\n"); + return AVERROR_INVALIDDATA; + } + + if ((err = aac_decode_frame_int(avctx, out, out_size, &gb)) < 0) + return err; + + return muxlength; +} + +av_cold static int latm_decode_init(AVCodecContext *avctx) +{ + struct LATMContext *latmctx = avctx->priv_data; + int ret; + + ret = aac_decode_init(avctx); + + if (avctx->extradata_size > 0) { + latmctx->initialized = !ret; + } else { + latmctx->initialized = 0; + } + + return ret; +} + + AVCodec aac_decoder = { "aac", AVMEDIA_TYPE_AUDIO, @@ -2113,3 +2372,23 @@ AVCodec aac_decoder = { }, .channel_layouts = aac_channel_layout, }; + +/* + Note: This decoder filter is intended to decode LATM streams transferred + in MPEG transport streams which only contain one program. + To do a more complex LATM demuxing a separate LATM demuxer should be used. +*/ +AVCodec aac_latm_decoder = { + .name = "aac_latm", + .type = CODEC_TYPE_AUDIO, + .id = CODEC_ID_AAC_LATM, + .priv_data_size = sizeof(struct LATMContext), + .init = latm_decode_init, + .close = aac_decode_close, + .decode = latm_decode_frame, + .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"), + .sample_fmts = (const enum SampleFormat[]) { + SAMPLE_FMT_S16,SAMPLE_FMT_NONE + }, + .channel_layouts = aac_channel_layout, +}; |