3 * Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavcodec/get_bits.h"
23 #include "libavcodec/put_bits.h"
24 #include "libavcodec/avcodec.h"
25 #include "libavcodec/mpeg4audio.h"
26 #include "libavutil/opt.h"
39 static const AVOption options[] = {
40 {"smc-interval", "StreamMuxConfig interval.",
41 offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.dbl = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
45 static const AVClass latm_muxer_class = {
46 .class_name = "LATM/LOAS muxer",
47 .item_name = av_default_item_name,
49 .version = LIBAVUTIL_VERSION_INT,
52 static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
55 MPEG4AudioConfig m4ac;
57 init_get_bits(&gb, buf, size * 8);
58 ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size);
61 skip_bits_long(&gb, ctx->off);
63 /* FIXME: are any formats not allowed in LATM? */
65 if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
66 av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
67 return AVERROR_INVALIDDATA;
69 ctx->channel_conf = m4ac.chan_config;
70 ctx->object_type = m4ac.object_type;
75 static int latm_write_header(AVFormatContext *s)
77 LATMContext *ctx = s->priv_data;
78 AVCodecContext *avctx = s->streams[0]->codec;
80 if (avctx->codec_id == CODEC_ID_AAC_LATM)
83 if (avctx->extradata_size > 0 &&
84 latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
85 return AVERROR_INVALIDDATA;
90 static int latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
92 LATMContext *ctx = s->priv_data;
93 AVCodecContext *avctx = s->streams[0]->codec;
98 put_bits(bs, 1, !!ctx->counter);
101 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
103 /* StreamMuxConfig */
104 put_bits(bs, 1, 0); /* audioMuxVersion */
105 put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
106 put_bits(bs, 6, 0); /* numSubFrames */
107 put_bits(bs, 4, 0); /* numProgram */
108 put_bits(bs, 3, 0); /* numLayer */
110 /* AudioSpecificConfig */
111 if (ctx->object_type == AOT_ALS) {
112 header_size = avctx->extradata_size-(ctx->off + 7) >> 3;
113 avpriv_copy_bits(bs, &avctx->extradata[ctx->off], header_size);
115 avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
117 if (!ctx->channel_conf) {
118 avpriv_copy_pce_data(bs, &gb);
122 put_bits(bs, 3, 0); /* frameLengthType */
123 put_bits(bs, 8, 0xff); /* latmBufferFullness */
125 put_bits(bs, 1, 0); /* otherDataPresent */
126 put_bits(bs, 1, 0); /* crcCheckPresent */
130 ctx->counter %= ctx->mod;
135 static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
137 AVIOContext *pb = s->pb;
140 uint8_t loas_header[] = "\x56\xe0\x00";
143 if (s->streams[0]->codec->codec_id == CODEC_ID_AAC_LATM)
144 return ff_raw_write_packet(s, pkt);
146 if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
147 av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
148 return AVERROR_INVALIDDATA;
151 buf = av_malloc(pkt->size+1024);
153 return AVERROR(ENOMEM);
155 init_put_bits(&bs, buf, pkt->size+1024);
157 latm_write_frame_header(s, &bs);
159 /* PayloadLengthInfo() */
160 for (i = 0; i <= pkt->size-255; i+=255)
161 put_bits(&bs, 8, 255);
163 put_bits(&bs, 8, pkt->size-i);
165 /* The LATM payload is written unaligned */
168 for (i = 0; i < pkt->size; i++)
169 put_bits(&bs, 8, pkt->data[i]);
171 avpriv_align_put_bits(&bs);
174 len = put_bits_count(&bs) >> 3;
176 loas_header[1] |= (len >> 8) & 0x1f;
177 loas_header[2] |= len & 0xff;
179 avio_write(pb, loas_header, 3);
180 avio_write(pb, buf, len);
187 AVOutputFormat ff_latm_muxer = {
189 .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
190 .mime_type = "audio/MP4A-LATM",
191 .extensions = "latm,loas",
192 .priv_data_size = sizeof(LATMContext),
193 .audio_codec = CODEC_ID_AAC,
194 .video_codec = CODEC_ID_NONE,
195 .write_header = latm_write_header,
196 .write_packet = latm_write_packet,
197 .priv_class = &latm_muxer_class,