]> git.sesse.net Git - ffmpeg/blob - libavformat/latmenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / latmenc.c
1 /*
2  * LATM/LOAS muxer
3  * Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
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"
27 #include "avformat.h"
28 #include "rawenc.h"
29
30 typedef struct {
31     AVClass *av_class;
32     int off;
33     int channel_conf;
34     int object_type;
35     int counter;
36     int mod;
37 } LATMContext;
38
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},
42     {NULL},
43 };
44
45 static const AVClass latm_muxer_class = {
46     .class_name = "LATM/LOAS muxer",
47     .item_name  = av_default_item_name,
48     .option     = options,
49     .version    = LIBAVUTIL_VERSION_INT,
50 };
51
52 static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
53 {
54     MPEG4AudioConfig m4ac;
55
56     ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
57     if (ctx->off < 0)
58         return ctx->off;
59
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;
64     }
65     /* FIXME: are any formats not allowed in LATM? */
66
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;
70     }
71     ctx->channel_conf = m4ac.chan_config;
72     ctx->object_type  = m4ac.object_type;
73
74     return 0;
75 }
76
77 static int latm_write_header(AVFormatContext *s)
78 {
79     LATMContext *ctx = s->priv_data;
80     AVCodecContext *avctx = s->streams[0]->codec;
81
82     if (avctx->codec_id == CODEC_ID_AAC_LATM)
83         return 0;
84
85     if (avctx->extradata_size > 0 &&
86         latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
87         return AVERROR_INVALIDDATA;
88
89     return 0;
90 }
91
92 static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
93 {
94     LATMContext *ctx = s->priv_data;
95     AVCodecContext *avctx = s->streams[0]->codec;
96     GetBitContext gb;
97     int header_size;
98
99     /* AudioMuxElement */
100     put_bits(bs, 1, !!ctx->counter);
101
102     if (!ctx->counter) {
103         init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
104
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 */
111
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);
116         } else {
117             avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
118
119             if (!ctx->channel_conf) {
120                 avpriv_copy_pce_data(bs, &gb);
121             }
122         }
123
124         put_bits(bs, 3, 0); /* frameLengthType */
125         put_bits(bs, 8, 0xff); /* latmBufferFullness */
126
127         put_bits(bs, 1, 0); /* otherDataPresent */
128         put_bits(bs, 1, 0); /* crcCheckPresent */
129     }
130
131     ctx->counter++;
132     ctx->counter %= ctx->mod;
133 }
134
135 static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
136 {
137     AVIOContext *pb = s->pb;
138     PutBitContext bs;
139     int i, len;
140     uint8_t loas_header[] = "\x56\xe0\x00";
141     uint8_t *buf = NULL;
142
143     if (s->streams[0]->codec->codec_id == CODEC_ID_AAC_LATM)
144         return ff_raw_write_packet(s, pkt);
145
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;
149     }
150     if (pkt->size > 0x1fff)
151         goto too_large;
152
153     buf = av_malloc(pkt->size+1024);
154     if (!buf)
155         return AVERROR(ENOMEM);
156
157     init_put_bits(&bs, buf, pkt->size+1024);
158
159     latm_write_frame_header(s, &bs);
160
161     /* PayloadLengthInfo() */
162     for (i = 0; i <= pkt->size-255; i+=255)
163         put_bits(&bs, 8, 255);
164
165     put_bits(&bs, 8, pkt->size-i);
166
167     /* The LATM payload is written unaligned */
168
169     /* PayloadMux() */
170     for (i = 0; i < pkt->size; i++)
171         put_bits(&bs, 8, pkt->data[i]);
172
173     avpriv_align_put_bits(&bs);
174     flush_put_bits(&bs);
175
176     len = put_bits_count(&bs) >> 3;
177
178     if (len > 0x1fff)
179         goto too_large;
180
181     loas_header[1] |= (len >> 8) & 0x1f;
182     loas_header[2] |= len & 0xff;
183
184     avio_write(pb, loas_header, 3);
185     avio_write(pb, buf, len);
186
187     av_free(buf);
188
189     return 0;
190
191 too_large:
192     av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
193     av_free(buf);
194     return AVERROR_INVALIDDATA;
195 }
196
197 AVOutputFormat ff_latm_muxer = {
198     .name           = "latm",
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,
208 };