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)
54 MPEG4AudioConfig m4ac;
56 ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
60 if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
61 // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
62 av_log(ctx, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
63 return AVERROR_INVALIDDATA;
65 /* FIXME: are any formats not allowed in LATM? */
67 if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
68 av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
69 return AVERROR_INVALIDDATA;
71 ctx->channel_conf = m4ac.chan_config;
72 ctx->object_type = m4ac.object_type;
77 static int latm_write_header(AVFormatContext *s)
79 LATMContext *ctx = s->priv_data;
80 AVCodecContext *avctx = s->streams[0]->codec;
82 if (avctx->codec_id == CODEC_ID_AAC_LATM)
85 if (avctx->extradata_size > 0 &&
86 latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
87 return AVERROR_INVALIDDATA;
92 static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
94 LATMContext *ctx = s->priv_data;
95 AVCodecContext *avctx = s->streams[0]->codec;
100 put_bits(bs, 1, !!ctx->counter);
103 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
105 /* StreamMuxConfig */
106 put_bits(bs, 1, 0); /* audioMuxVersion */
107 put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
108 put_bits(bs, 6, 0); /* numSubFrames */
109 put_bits(bs, 4, 0); /* numProgram */
110 put_bits(bs, 3, 0); /* numLayer */
112 /* AudioSpecificConfig */
113 if (ctx->object_type == AOT_ALS) {
114 header_size = avctx->extradata_size-(ctx->off >> 3);
115 avpriv_copy_bits(bs, &avctx->extradata[ctx->off >> 3], header_size);
117 avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
119 if (!ctx->channel_conf) {
120 avpriv_copy_pce_data(bs, &gb);
124 put_bits(bs, 3, 0); /* frameLengthType */
125 put_bits(bs, 8, 0xff); /* latmBufferFullness */
127 put_bits(bs, 1, 0); /* otherDataPresent */
128 put_bits(bs, 1, 0); /* crcCheckPresent */
132 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;
150 if (pkt->size > 0x1fff)
153 buf = av_malloc(pkt->size+1024);
155 return AVERROR(ENOMEM);
157 init_put_bits(&bs, buf, pkt->size+1024);
159 latm_write_frame_header(s, &bs);
161 /* PayloadLengthInfo() */
162 for (i = 0; i <= pkt->size-255; i+=255)
163 put_bits(&bs, 8, 255);
165 put_bits(&bs, 8, pkt->size-i);
167 /* The LATM payload is written unaligned */
170 for (i = 0; i < pkt->size; i++)
171 put_bits(&bs, 8, pkt->data[i]);
173 avpriv_align_put_bits(&bs);
176 len = put_bits_count(&bs) >> 3;
181 loas_header[1] |= (len >> 8) & 0x1f;
182 loas_header[2] |= len & 0xff;
184 avio_write(pb, loas_header, 3);
185 avio_write(pb, buf, len);
192 av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
194 return AVERROR_INVALIDDATA;
197 AVOutputFormat ff_latm_muxer = {
199 .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
200 .mime_type = "audio/MP4A-LATM",
201 .extensions = "latm,loas",
202 .priv_data_size = sizeof(LATMContext),
203 .audio_codec = CODEC_ID_AAC,
204 .video_codec = CODEC_ID_NONE,
205 .write_header = latm_write_header,
206 .write_packet = latm_write_packet,
207 .priv_class = &latm_muxer_class,