yading@11: /* yading@11: * LATM/LOAS muxer yading@11: * Copyright (c) 2011 Kieran Kunhya yading@11: * yading@11: * This file is part of Libav. yading@11: * yading@11: * Libav is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * Libav is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with Libav; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "libavcodec/get_bits.h" yading@11: #include "libavcodec/put_bits.h" yading@11: #include "libavcodec/avcodec.h" yading@11: #include "libavcodec/mpeg4audio.h" yading@11: #include "libavutil/opt.h" yading@11: #include "avformat.h" yading@11: #include "rawenc.h" yading@11: yading@11: #define MAX_EXTRADATA_SIZE 1024 yading@11: yading@11: typedef struct { yading@11: AVClass *av_class; yading@11: int off; yading@11: int channel_conf; yading@11: int object_type; yading@11: int counter; yading@11: int mod; yading@11: uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024]; yading@11: } LATMContext; yading@11: yading@11: static const AVOption options[] = { yading@11: {"smc-interval", "StreamMuxConfig interval.", yading@11: offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM}, yading@11: {NULL}, yading@11: }; yading@11: yading@11: static const AVClass latm_muxer_class = { yading@11: .class_name = "LATM/LOAS muxer", yading@11: .item_name = av_default_item_name, yading@11: .option = options, yading@11: .version = LIBAVUTIL_VERSION_INT, yading@11: }; yading@11: yading@11: static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size) yading@11: { yading@11: MPEG4AudioConfig m4ac; yading@11: yading@11: if (size > MAX_EXTRADATA_SIZE) { yading@11: av_log(ctx, AV_LOG_ERROR, "Extradata is larger than currently supported.\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1); yading@11: if (ctx->off < 0) yading@11: return ctx->off; yading@11: yading@11: if (ctx->object_type == AOT_ALS && (ctx->off & 7)) { yading@11: // as long as avpriv_mpeg4audio_get_config works correctly this is impossible yading@11: av_log(ctx, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: /* FIXME: are any formats not allowed in LATM? */ yading@11: yading@11: if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) { yading@11: av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: ctx->channel_conf = m4ac.chan_config; yading@11: ctx->object_type = m4ac.object_type; yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int latm_write_header(AVFormatContext *s) yading@11: { yading@11: LATMContext *ctx = s->priv_data; yading@11: AVCodecContext *avctx = s->streams[0]->codec; yading@11: yading@11: if (avctx->codec_id == AV_CODEC_ID_AAC_LATM) yading@11: return 0; yading@11: yading@11: if (avctx->extradata_size > 0 && yading@11: latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0) yading@11: return AVERROR_INVALIDDATA; yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs) yading@11: { yading@11: LATMContext *ctx = s->priv_data; yading@11: AVCodecContext *avctx = s->streams[0]->codec; yading@11: int header_size; yading@11: yading@11: /* AudioMuxElement */ yading@11: put_bits(bs, 1, !!ctx->counter); yading@11: yading@11: if (!ctx->counter) { yading@11: /* StreamMuxConfig */ yading@11: put_bits(bs, 1, 0); /* audioMuxVersion */ yading@11: put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */ yading@11: put_bits(bs, 6, 0); /* numSubFrames */ yading@11: put_bits(bs, 4, 0); /* numProgram */ yading@11: put_bits(bs, 3, 0); /* numLayer */ yading@11: yading@11: /* AudioSpecificConfig */ yading@11: if (ctx->object_type == AOT_ALS) { yading@11: header_size = avctx->extradata_size-(ctx->off >> 3); yading@11: avpriv_copy_bits(bs, &avctx->extradata[ctx->off >> 3], header_size); yading@11: } else { yading@11: // + 3 assumes not scalable and dependsOnCoreCoder == 0, yading@11: // see decode_ga_specific_config in libavcodec/aacdec.c yading@11: avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3); yading@11: yading@11: if (!ctx->channel_conf) { yading@11: GetBitContext gb; yading@11: init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8); yading@11: skip_bits_long(&gb, ctx->off + 3); yading@11: avpriv_copy_pce_data(bs, &gb); yading@11: } yading@11: } yading@11: yading@11: put_bits(bs, 3, 0); /* frameLengthType */ yading@11: put_bits(bs, 8, 0xff); /* latmBufferFullness */ yading@11: yading@11: put_bits(bs, 1, 0); /* otherDataPresent */ yading@11: put_bits(bs, 1, 0); /* crcCheckPresent */ yading@11: } yading@11: yading@11: ctx->counter++; yading@11: ctx->counter %= ctx->mod; yading@11: } yading@11: yading@11: static int latm_write_packet(AVFormatContext *s, AVPacket *pkt) yading@11: { yading@11: LATMContext *ctx = s->priv_data; yading@11: AVIOContext *pb = s->pb; yading@11: PutBitContext bs; yading@11: int i, len; yading@11: uint8_t loas_header[] = "\x56\xe0\x00"; yading@11: yading@11: if (s->streams[0]->codec->codec_id == AV_CODEC_ID_AAC_LATM) yading@11: return ff_raw_write_packet(s, pkt); yading@11: yading@11: if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) { yading@11: av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: if (!s->streams[0]->codec->extradata) { yading@11: if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe && yading@11: (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size) yading@11: return ff_raw_write_packet(s, pkt); yading@11: else yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: if (pkt->size > 0x1fff) yading@11: goto too_large; yading@11: yading@11: init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE); yading@11: yading@11: latm_write_frame_header(s, &bs); yading@11: yading@11: /* PayloadLengthInfo() */ yading@11: for (i = 0; i <= pkt->size-255; i+=255) yading@11: put_bits(&bs, 8, 255); yading@11: yading@11: put_bits(&bs, 8, pkt->size-i); yading@11: yading@11: /* The LATM payload is written unaligned */ yading@11: yading@11: /* PayloadMux() */ yading@11: if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) { yading@11: // Convert byte-aligned DSE to non-aligned. yading@11: // Due to the input format encoding we know that yading@11: // it is naturally byte-aligned in the input stream, yading@11: // so there are no padding bits to account for. yading@11: // To avoid having to add padding bits and rearrange yading@11: // the whole stream we just remove the byte-align flag. yading@11: // This allows us to remux our FATE AAC samples into latm yading@11: // files that are still playable with minimal effort. yading@11: put_bits(&bs, 8, pkt->data[0] & 0xfe); yading@11: avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8); yading@11: } else yading@11: avpriv_copy_bits(&bs, pkt->data, 8*pkt->size); yading@11: yading@11: avpriv_align_put_bits(&bs); yading@11: flush_put_bits(&bs); yading@11: yading@11: len = put_bits_count(&bs) >> 3; yading@11: yading@11: if (len > 0x1fff) yading@11: goto too_large; yading@11: yading@11: loas_header[1] |= (len >> 8) & 0x1f; yading@11: loas_header[2] |= len & 0xff; yading@11: yading@11: avio_write(pb, loas_header, 3); yading@11: avio_write(pb, ctx->buffer, len); yading@11: yading@11: return 0; yading@11: yading@11: too_large: yading@11: av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: AVOutputFormat ff_latm_muxer = { yading@11: .name = "latm", yading@11: .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"), yading@11: .mime_type = "audio/MP4A-LATM", yading@11: .extensions = "latm,loas", yading@11: .priv_data_size = sizeof(LATMContext), yading@11: .audio_codec = AV_CODEC_ID_AAC, yading@11: .video_codec = AV_CODEC_ID_NONE, yading@11: .write_header = latm_write_header, yading@11: .write_packet = latm_write_packet, yading@11: .priv_class = &latm_muxer_class, yading@11: };