3 * Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
31 #define MAX_EXTRADATA_SIZE 1024
33 typedef struct LATMContext {
40 uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
43 static const AVOption options[] = {
44 {"smc-interval", "StreamMuxConfig interval.",
45 offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
49 static const AVClass latm_muxer_class = {
50 .class_name = "LATM/LOAS muxer",
51 .item_name = av_default_item_name,
53 .version = LIBAVUTIL_VERSION_INT,
56 static int latm_decode_extradata(AVFormatContext *s, uint8_t *buf, int size)
58 LATMContext *ctx = s->priv_data;
59 MPEG4AudioConfig m4ac;
61 if (size > MAX_EXTRADATA_SIZE) {
62 av_log(s, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
63 return AVERROR_INVALIDDATA;
65 ctx->off = avpriv_mpeg4audio_get_config2(&m4ac, buf, size, 1, s);
69 if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
70 // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
71 av_log(s, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
72 return AVERROR_INVALIDDATA;
74 /* FIXME: are any formats not allowed in LATM? */
76 if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
77 av_log(s, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
78 return AVERROR_INVALIDDATA;
80 ctx->channel_conf = m4ac.chan_config;
81 ctx->object_type = m4ac.object_type;
86 static int latm_write_header(AVFormatContext *s)
88 AVCodecParameters *par = s->streams[0]->codecpar;
90 if (par->codec_id == AV_CODEC_ID_AAC_LATM)
92 if (par->codec_id != AV_CODEC_ID_AAC && par->codec_id != AV_CODEC_ID_MP4ALS) {
93 av_log(s, AV_LOG_ERROR, "Only AAC, LATM and ALS are supported\n");
94 return AVERROR(EINVAL);
97 if (par->extradata_size > 0 &&
98 latm_decode_extradata(s, par->extradata, par->extradata_size) < 0)
99 return AVERROR_INVALIDDATA;
104 static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
106 LATMContext *ctx = s->priv_data;
107 AVCodecParameters *par = s->streams[0]->codecpar;
110 /* AudioMuxElement */
111 put_bits(bs, 1, !!ctx->counter);
114 /* StreamMuxConfig */
115 put_bits(bs, 1, 0); /* audioMuxVersion */
116 put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
117 put_bits(bs, 6, 0); /* numSubFrames */
118 put_bits(bs, 4, 0); /* numProgram */
119 put_bits(bs, 3, 0); /* numLayer */
121 /* AudioSpecificConfig */
122 if (ctx->object_type == AOT_ALS) {
123 header_size = par->extradata_size-(ctx->off >> 3);
124 avpriv_copy_bits(bs, &par->extradata[ctx->off >> 3], header_size);
126 // + 3 assumes not scalable and dependsOnCoreCoder == 0,
127 // see decode_ga_specific_config in libavcodec/aacdec.c
128 avpriv_copy_bits(bs, par->extradata, ctx->off + 3);
130 if (!ctx->channel_conf) {
132 int ret = init_get_bits8(&gb, par->extradata, par->extradata_size);
133 av_assert0(ret >= 0); // extradata size has been checked already, so this should not fail
134 skip_bits_long(&gb, ctx->off + 3);
135 ff_copy_pce_data(bs, &gb);
139 put_bits(bs, 3, 0); /* frameLengthType */
140 put_bits(bs, 8, 0xff); /* latmBufferFullness */
142 put_bits(bs, 1, 0); /* otherDataPresent */
143 put_bits(bs, 1, 0); /* crcCheckPresent */
147 ctx->counter %= ctx->mod;
150 static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
152 LATMContext *ctx = s->priv_data;
153 AVCodecParameters *par = s->streams[0]->codecpar;
154 AVIOContext *pb = s->pb;
157 uint8_t loas_header[] = "\x56\xe0\x00";
159 if (par->codec_id == AV_CODEC_ID_AAC_LATM)
160 return ff_raw_write_packet(s, pkt);
162 if (!par->extradata) {
163 if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
164 (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
165 return ff_raw_write_packet(s, pkt);
168 int side_data_size, ret;
170 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
172 if (side_data_size) {
173 if (latm_decode_extradata(s, side_data, side_data_size) < 0)
174 return AVERROR_INVALIDDATA;
175 ret = ff_alloc_extradata(par, side_data_size);
178 memcpy(par->extradata, side_data, side_data_size);
180 return AVERROR_INVALIDDATA;
184 if (pkt->size > 0x1fff)
187 init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
189 latm_write_frame_header(s, &bs);
191 /* PayloadLengthInfo() */
192 for (i = 0; i <= pkt->size-255; i+=255)
193 put_bits(&bs, 8, 255);
195 put_bits(&bs, 8, pkt->size-i);
197 /* The LATM payload is written unaligned */
200 if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
201 // Convert byte-aligned DSE to non-aligned.
202 // Due to the input format encoding we know that
203 // it is naturally byte-aligned in the input stream,
204 // so there are no padding bits to account for.
205 // To avoid having to add padding bits and rearrange
206 // the whole stream we just remove the byte-align flag.
207 // This allows us to remux our FATE AAC samples into latm
208 // files that are still playable with minimal effort.
209 put_bits(&bs, 8, pkt->data[0] & 0xfe);
210 avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
212 avpriv_copy_bits(&bs, pkt->data, 8*pkt->size);
214 avpriv_align_put_bits(&bs);
217 len = put_bits_count(&bs) >> 3;
222 loas_header[1] |= (len >> 8) & 0x1f;
223 loas_header[2] |= len & 0xff;
225 avio_write(pb, loas_header, 3);
226 avio_write(pb, ctx->buffer, len);
231 av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
232 return AVERROR_INVALIDDATA;
235 static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
238 AVStream *st = s->streams[pkt->stream_index];
240 if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
241 if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
242 ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
248 AVOutputFormat ff_latm_muxer = {
250 .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
251 .mime_type = "audio/MP4A-LATM",
252 .extensions = "latm,loas",
253 .priv_data_size = sizeof(LATMContext),
254 .audio_codec = AV_CODEC_ID_AAC,
255 .video_codec = AV_CODEC_ID_NONE,
256 .write_header = latm_write_header,
257 .write_packet = latm_write_packet,
258 .priv_class = &latm_muxer_class,
259 .check_bitstream= latm_check_bitstream,
260 .flags = AVFMT_NOTIMESTAMPS,