]> git.sesse.net Git - ffmpeg/blob - libavformat/latmenc.c
pcmdec: use unique classes for all pcm demuxers.
[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
29 typedef struct {
30     int off;
31     int channel_conf;
32     int object_type;
33     int counter;
34     int mod;
35 } LATMContext;
36
37 static const AVOption options[] = {
38     {"smc-interval", "StreamMuxConfig interval.",
39      offsetof(LATMContext, mod), FF_OPT_TYPE_INT, {.dbl = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
40     {NULL},
41 };
42
43 static const AVClass latm_muxer_class = {
44     .class_name = "LATM/LOAS muxer",
45     .item_name  = av_default_item_name,
46     .option     = options,
47     .version    = LIBAVUTIL_VERSION_INT,
48 };
49
50 static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
51 {
52     GetBitContext gb;
53     MPEG4AudioConfig m4ac;
54
55     init_get_bits(&gb, buf, size * 8);
56     ctx->off = ff_mpeg4audio_get_config(&m4ac, buf, size);
57     if (ctx->off < 0)
58         return ctx->off;
59     skip_bits_long(&gb, ctx->off);
60
61     /* FIXME: are any formats not allowed in LATM? */
62
63     if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
64         av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
65         return AVERROR_INVALIDDATA;
66     }
67     ctx->channel_conf = m4ac.chan_config;
68     ctx->object_type  = m4ac.object_type;
69
70     return 0;
71 }
72
73 static int latm_write_header(AVFormatContext *s)
74 {
75     LATMContext *ctx = s->priv_data;
76     AVCodecContext *avctx = s->streams[0]->codec;
77
78     if (avctx->extradata_size > 0 &&
79         latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
80         return AVERROR_INVALIDDATA;
81
82     return 0;
83 }
84
85 static int latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
86 {
87     LATMContext *ctx = s->priv_data;
88     AVCodecContext *avctx = s->streams[0]->codec;
89     GetBitContext gb;
90     int header_size;
91
92     /* AudioMuxElement */
93     put_bits(bs, 1, !!ctx->counter);
94
95     if (!ctx->counter) {
96         init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
97
98         /* StreamMuxConfig */
99         put_bits(bs, 1, 0); /* audioMuxVersion */
100         put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
101         put_bits(bs, 6, 0); /* numSubFrames */
102         put_bits(bs, 4, 0); /* numProgram */
103         put_bits(bs, 3, 0); /* numLayer */
104
105         /* AudioSpecificConfig */
106         if (ctx->object_type == AOT_ALS) {
107             header_size = avctx->extradata_size-(ctx->off + 7) >> 3;
108             ff_copy_bits(bs, &avctx->extradata[ctx->off], header_size);
109         } else {
110             ff_copy_bits(bs, avctx->extradata, ctx->off + 3);
111
112             if (!ctx->channel_conf) {
113                 ff_copy_pce_data(bs, &gb);
114             }
115         }
116
117         put_bits(bs, 3, 0); /* frameLengthType */
118         put_bits(bs, 8, 0); /* latmBufferFullness */
119
120         put_bits(bs, 1, 0); /* otherDataPresent */
121         put_bits(bs, 1, 0); /* crcCheckPresent */
122     }
123
124     ctx->counter++;
125     ctx->counter %= ctx->mod;
126
127     return 0;
128 }
129
130 static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
131 {
132     AVIOContext *pb = s->pb;
133     PutBitContext bs;
134     int i, len;
135     uint8_t loas_header[] = "\x56\xe0\x00";
136     uint8_t *buf;
137
138     if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
139         av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
140         return AVERROR_INVALIDDATA;
141     }
142
143     buf = av_malloc(pkt->size+1024);
144     if (!buf)
145         return AVERROR(ENOMEM);
146
147     init_put_bits(&bs, buf, pkt->size+1024);
148
149     latm_write_frame_header(s, &bs);
150
151     /* PayloadLengthInfo() */
152     for (i = 0; i <= pkt->size-255; i+=255)
153         put_bits(&bs, 8, 255);
154
155     put_bits(&bs, 8, pkt->size-i);
156
157     /* The LATM payload is written unaligned */
158
159     /* PayloadMux() */
160     for (i = 0; i < pkt->size; i++)
161         put_bits(&bs, 8, pkt->data[i]);
162
163     align_put_bits(&bs);
164     flush_put_bits(&bs);
165
166     len = put_bits_count(&bs) >> 3;
167
168     loas_header[1] |= (len >> 8) & 0x1f;
169     loas_header[2] |= len & 0xff;
170
171     avio_write(pb, loas_header, 3);
172     avio_write(pb, buf, len);
173
174     av_free(buf);
175
176     return 0;
177 }
178
179 AVOutputFormat ff_latm_muxer = {
180     .name           = "latm",
181     .long_name      = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
182     .mime_type      = "audio/MP4A-LATM",
183     .extensions     = "latm",
184     .priv_data_size = sizeof(LATMContext),
185     .audio_codec    = CODEC_ID_AAC,
186     .video_codec    = CODEC_ID_NONE,
187     .write_header   = latm_write_header,
188     .write_packet   = latm_write_packet,
189     .priv_class     = &latm_muxer_class,
190 };