]> 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     GetBitContext gb;
55     MPEG4AudioConfig m4ac;
56
57     init_get_bits(&gb, buf, size * 8);
58     ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
59     if (ctx->off < 0)
60         return ctx->off;
61     skip_bits_long(&gb, ctx->off);
62
63     /* FIXME: are any formats not allowed in LATM? */
64
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;
68     }
69     ctx->channel_conf = m4ac.chan_config;
70     ctx->object_type  = m4ac.object_type;
71
72     return 0;
73 }
74
75 static int latm_write_header(AVFormatContext *s)
76 {
77     LATMContext *ctx = s->priv_data;
78     AVCodecContext *avctx = s->streams[0]->codec;
79
80     if (avctx->codec_id == CODEC_ID_AAC_LATM)
81         return 0;
82
83     if (avctx->extradata_size > 0 &&
84         latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
85         return AVERROR_INVALIDDATA;
86
87     return 0;
88 }
89
90 static int latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
91 {
92     LATMContext *ctx = s->priv_data;
93     AVCodecContext *avctx = s->streams[0]->codec;
94     GetBitContext gb;
95     int header_size;
96
97     /* AudioMuxElement */
98     put_bits(bs, 1, !!ctx->counter);
99
100     if (!ctx->counter) {
101         init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
102
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 */
109
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);
114         } else {
115             avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
116
117             if (!ctx->channel_conf) {
118                 avpriv_copy_pce_data(bs, &gb);
119             }
120         }
121
122         put_bits(bs, 3, 0); /* frameLengthType */
123         put_bits(bs, 8, 0xff); /* latmBufferFullness */
124
125         put_bits(bs, 1, 0); /* otherDataPresent */
126         put_bits(bs, 1, 0); /* crcCheckPresent */
127     }
128
129     ctx->counter++;
130     ctx->counter %= ctx->mod;
131
132     return 0;
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;
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
151     buf = av_malloc(pkt->size+1024);
152     if (!buf)
153         return AVERROR(ENOMEM);
154
155     init_put_bits(&bs, buf, pkt->size+1024);
156
157     latm_write_frame_header(s, &bs);
158
159     /* PayloadLengthInfo() */
160     for (i = 0; i <= pkt->size-255; i+=255)
161         put_bits(&bs, 8, 255);
162
163     put_bits(&bs, 8, pkt->size-i);
164
165     /* The LATM payload is written unaligned */
166
167     /* PayloadMux() */
168     for (i = 0; i < pkt->size; i++)
169         put_bits(&bs, 8, pkt->data[i]);
170
171     avpriv_align_put_bits(&bs);
172     flush_put_bits(&bs);
173
174     len = put_bits_count(&bs) >> 3;
175
176     loas_header[1] |= (len >> 8) & 0x1f;
177     loas_header[2] |= len & 0xff;
178
179     avio_write(pb, loas_header, 3);
180     avio_write(pb, buf, len);
181
182     av_free(buf);
183
184     return 0;
185 }
186
187 AVOutputFormat ff_latm_muxer = {
188     .name           = "latm",
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,
198 };