]> git.sesse.net Git - ffmpeg/blob - libavformat/adtsenc.c
avformat: Constify all muxer/demuxers
[ffmpeg] / libavformat / adtsenc.c
1 /*
2  * ADTS muxer.
3  * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4  *                    Mans Rullgard <mans@mansr.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavcodec/get_bits.h"
24 #include "libavcodec/put_bits.h"
25 #include "libavcodec/avcodec.h"
26 #include "libavcodec/mpeg4audio.h"
27 #include "libavutil/opt.h"
28 #include "avformat.h"
29 #include "apetag.h"
30 #include "id3v2.h"
31
32 #define ADTS_HEADER_SIZE 7
33
34 typedef struct ADTSContext {
35     AVClass *class;
36     int write_adts;
37     int objecttype;
38     int sample_rate_index;
39     int channel_conf;
40     int pce_size;
41     int apetag;
42     int id3v2tag;
43     int mpeg_id;
44     uint8_t pce_data[MAX_PCE_SIZE];
45 } ADTSContext;
46
47 #define ADTS_MAX_FRAME_BYTES ((1 << 14) - 1)
48
49 static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, const uint8_t *buf, int size)
50 {
51     GetBitContext gb;
52     PutBitContext pb;
53     MPEG4AudioConfig m4ac;
54     int off;
55
56     init_get_bits(&gb, buf, size * 8);
57     off = avpriv_mpeg4audio_get_config2(&m4ac, buf, size, 1, s);
58     if (off < 0)
59         return off;
60     skip_bits_long(&gb, off);
61     adts->objecttype        = m4ac.object_type - 1;
62     adts->sample_rate_index = m4ac.sampling_index;
63     adts->channel_conf      = m4ac.chan_config;
64
65     if (adts->objecttype > 3U) {
66         av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
67         return AVERROR_INVALIDDATA;
68     }
69     if (adts->sample_rate_index == 15) {
70         av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
71         return AVERROR_INVALIDDATA;
72     }
73     if (get_bits(&gb, 1)) {
74         av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
75         return AVERROR_INVALIDDATA;
76     }
77     if (get_bits(&gb, 1)) {
78         av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
79         return AVERROR_INVALIDDATA;
80     }
81     if (get_bits(&gb, 1)) {
82         av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
83         return AVERROR_INVALIDDATA;
84     }
85     if (!adts->channel_conf) {
86         init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
87
88         put_bits(&pb, 3, 5); //ID_PCE
89         adts->pce_size = (ff_copy_pce_data(&pb, &gb) + 3) / 8;
90         flush_put_bits(&pb);
91     }
92
93     adts->write_adts = 1;
94
95     return 0;
96 }
97
98 static int adts_init(AVFormatContext *s)
99 {
100     ADTSContext *adts = s->priv_data;
101     AVCodecParameters *par = s->streams[0]->codecpar;
102
103     if (par->codec_id != AV_CODEC_ID_AAC) {
104         av_log(s, AV_LOG_ERROR, "Only AAC streams can be muxed by the ADTS muxer\n");
105         return AVERROR(EINVAL);
106     }
107     if (par->extradata_size > 0)
108         return adts_decode_extradata(s, adts, par->extradata,
109                                      par->extradata_size);
110
111     return 0;
112 }
113
114 static int adts_write_header(AVFormatContext *s)
115 {
116     ADTSContext *adts = s->priv_data;
117
118     if (adts->id3v2tag)
119         ff_id3v2_write_simple(s, 4, ID3v2_DEFAULT_MAGIC);
120
121     return 0;
122 }
123
124 static int adts_write_frame_header(ADTSContext *ctx,
125                                    uint8_t *buf, int size, int pce_size)
126 {
127     PutBitContext pb;
128
129     unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
130     if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
131         av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
132                full_frame_size, ADTS_MAX_FRAME_BYTES);
133         return AVERROR_INVALIDDATA;
134     }
135
136     init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
137
138     /* adts_fixed_header */
139     put_bits(&pb, 12, 0xfff);   /* syncword */
140     put_bits(&pb, 1, ctx->mpeg_id); /* ID */
141     put_bits(&pb, 2, 0);        /* layer */
142     put_bits(&pb, 1, 1);        /* protection_absent */
143     put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
144     put_bits(&pb, 4, ctx->sample_rate_index);
145     put_bits(&pb, 1, 0);        /* private_bit */
146     put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
147     put_bits(&pb, 1, 0);        /* original_copy */
148     put_bits(&pb, 1, 0);        /* home */
149
150     /* adts_variable_header */
151     put_bits(&pb, 1, 0);        /* copyright_identification_bit */
152     put_bits(&pb, 1, 0);        /* copyright_identification_start */
153     put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
154     put_bits(&pb, 11, 0x7ff);   /* adts_buffer_fullness */
155     put_bits(&pb, 2, 0);        /* number_of_raw_data_blocks_in_frame */
156
157     flush_put_bits(&pb);
158
159     return 0;
160 }
161
162 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
163 {
164     ADTSContext *adts = s->priv_data;
165     AVCodecParameters *par = s->streams[0]->codecpar;
166     AVIOContext *pb = s->pb;
167     uint8_t buf[ADTS_HEADER_SIZE];
168
169     if (!pkt->size)
170         return 0;
171     if (!par->extradata_size) {
172         uint8_t *side_data;
173         size_t side_data_size;
174         int ret;
175
176         side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
177                                             &side_data_size);
178         if (side_data_size) {
179             ret = adts_decode_extradata(s, adts, side_data, side_data_size);
180             if (ret < 0)
181                 return ret;
182             ret = ff_alloc_extradata(par, side_data_size);
183             if (ret < 0)
184                 return ret;
185             memcpy(par->extradata, side_data, side_data_size);
186         }
187     }
188     if (adts->write_adts) {
189         int err = adts_write_frame_header(adts, buf, pkt->size,
190                                              adts->pce_size);
191         if (err < 0)
192             return err;
193         avio_write(pb, buf, ADTS_HEADER_SIZE);
194         if (adts->pce_size) {
195             avio_write(pb, adts->pce_data, adts->pce_size);
196             adts->pce_size = 0;
197         }
198     }
199     avio_write(pb, pkt->data, pkt->size);
200
201     return 0;
202 }
203
204 static int adts_write_trailer(AVFormatContext *s)
205 {
206     ADTSContext *adts = s->priv_data;
207
208     if (adts->apetag)
209         ff_ape_write_tag(s);
210
211     return 0;
212 }
213
214 #define ENC AV_OPT_FLAG_ENCODING_PARAM
215 #define OFFSET(obj) offsetof(ADTSContext, obj)
216 static const AVOption options[] = {
217     { "write_id3v2",  "Enable ID3v2 tag writing",   OFFSET(id3v2tag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
218     { "write_apetag", "Enable APE tag writing",     OFFSET(apetag),   AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
219     { "write_mpeg2",  "Set MPEG version to MPEG-2", OFFSET(mpeg_id),  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
220     { NULL },
221 };
222
223 static const AVClass adts_muxer_class = {
224     .class_name     = "ADTS muxer",
225     .item_name      = av_default_item_name,
226     .option         = options,
227     .version        = LIBAVUTIL_VERSION_INT,
228 };
229
230 const AVOutputFormat ff_adts_muxer = {
231     .name              = "adts",
232     .long_name         = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
233     .mime_type         = "audio/aac",
234     .extensions        = "aac,adts",
235     .priv_data_size    = sizeof(ADTSContext),
236     .audio_codec       = AV_CODEC_ID_AAC,
237     .video_codec       = AV_CODEC_ID_NONE,
238     .init              = adts_init,
239     .write_header      = adts_write_header,
240     .write_packet      = adts_write_packet,
241     .write_trailer     = adts_write_trailer,
242     .priv_class        = &adts_muxer_class,
243     .flags             = AVFMT_NOTIMESTAMPS,
244 };