3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4 * Mans Rullgard <mans@mansr.com>
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavcodec/get_bits.h"
24 #include "libavcodec/put_bits.h"
25 #include "libavcodec/codec_id.h"
26 #include "libavcodec/codec_par.h"
27 #include "libavcodec/packet.h"
28 #include "libavcodec/mpeg4audio.h"
29 #include "libavcodec/mpeg4audio_copy_pce.h"
30 #include "libavutil/opt.h"
36 #define ADTS_HEADER_SIZE 7
38 typedef struct ADTSContext
{
42 int sample_rate_index
;
48 uint8_t pce_data
[MAX_PCE_SIZE
];
51 #define ADTS_MAX_FRAME_BYTES ((1 << 14) - 1)
53 static int adts_decode_extradata(AVFormatContext
*s
, ADTSContext
*adts
, const uint8_t *buf
, int size
)
57 MPEG4AudioConfig m4ac
;
60 ret
= init_get_bits8(&gb
, buf
, size
);
63 off
= avpriv_mpeg4audio_get_config2(&m4ac
, buf
, size
, 1, s
);
66 skip_bits_long(&gb
, off
);
67 adts
->objecttype
= m4ac
.object_type
- 1;
68 adts
->sample_rate_index
= m4ac
.sampling_index
;
69 adts
->channel_conf
= m4ac
.chan_config
;
71 if (adts
->objecttype
> 3U) {
72 av_log(s
, AV_LOG_ERROR
, "MPEG-4 AOT %d is not allowed in ADTS\n", adts
->objecttype
+1);
73 return AVERROR_INVALIDDATA
;
75 if (adts
->sample_rate_index
== 15) {
76 av_log(s
, AV_LOG_ERROR
, "Escape sample rate index illegal in ADTS\n");
77 return AVERROR_INVALIDDATA
;
79 if (get_bits(&gb
, 1)) {
80 av_log(s
, AV_LOG_ERROR
, "960/120 MDCT window is not allowed in ADTS\n");
81 return AVERROR_INVALIDDATA
;
83 if (get_bits(&gb
, 1)) {
84 av_log(s
, AV_LOG_ERROR
, "Scalable configurations are not allowed in ADTS\n");
85 return AVERROR_INVALIDDATA
;
87 if (get_bits(&gb
, 1)) {
88 av_log(s
, AV_LOG_ERROR
, "Extension flag is not allowed in ADTS\n");
89 return AVERROR_INVALIDDATA
;
91 if (!adts
->channel_conf
) {
92 init_put_bits(&pb
, adts
->pce_data
, MAX_PCE_SIZE
);
94 put_bits(&pb
, 3, 5); //ID_PCE
95 adts
->pce_size
= (ff_copy_pce_data(&pb
, &gb
) + 3) / 8;
104 static int adts_init(AVFormatContext
*s
)
106 ADTSContext
*adts
= s
->priv_data
;
107 AVCodecParameters
*par
= s
->streams
[0]->codecpar
;
109 if (par
->extradata_size
> 0)
110 return adts_decode_extradata(s
, adts
, par
->extradata
,
111 par
->extradata_size
);
116 static int adts_write_header(AVFormatContext
*s
)
118 ADTSContext
*adts
= s
->priv_data
;
121 ff_id3v2_write_simple(s
, 4, ID3v2_DEFAULT_MAGIC
);
126 static int adts_write_frame_header(AVFormatContext
*s
, ADTSContext
*ctx
,
127 uint8_t *buf
, int size
, int pce_size
)
131 unsigned full_frame_size
= (unsigned)ADTS_HEADER_SIZE
+ size
+ pce_size
;
132 if (full_frame_size
> ADTS_MAX_FRAME_BYTES
) {
133 av_log(s
, AV_LOG_ERROR
, "frame size too large: %u (max %d)\n",
134 full_frame_size
, ADTS_MAX_FRAME_BYTES
);
135 return AVERROR_INVALIDDATA
;
138 init_put_bits(&pb
, buf
, ADTS_HEADER_SIZE
);
140 /* adts_fixed_header */
141 put_bits(&pb
, 12, 0xfff); /* syncword */
142 put_bits(&pb
, 1, ctx
->mpeg_id
); /* ID */
143 put_bits(&pb
, 2, 0); /* layer */
144 put_bits(&pb
, 1, 1); /* protection_absent */
145 put_bits(&pb
, 2, ctx
->objecttype
); /* profile_objecttype */
146 put_bits(&pb
, 4, ctx
->sample_rate_index
);
147 put_bits(&pb
, 1, 0); /* private_bit */
148 put_bits(&pb
, 3, ctx
->channel_conf
); /* channel_configuration */
149 put_bits(&pb
, 1, 0); /* original_copy */
150 put_bits(&pb
, 1, 0); /* home */
152 /* adts_variable_header */
153 put_bits(&pb
, 1, 0); /* copyright_identification_bit */
154 put_bits(&pb
, 1, 0); /* copyright_identification_start */
155 put_bits(&pb
, 13, full_frame_size
); /* aac_frame_length */
156 put_bits(&pb
, 11, 0x7ff); /* adts_buffer_fullness */
157 put_bits(&pb
, 2, 0); /* number_of_raw_data_blocks_in_frame */
164 static int adts_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
166 ADTSContext
*adts
= s
->priv_data
;
167 AVCodecParameters
*par
= s
->streams
[0]->codecpar
;
168 AVIOContext
*pb
= s
->pb
;
169 uint8_t buf
[ADTS_HEADER_SIZE
];
173 if (!par
->extradata_size
) {
175 size_t side_data_size
;
178 side_data
= av_packet_get_side_data(pkt
, AV_PKT_DATA_NEW_EXTRADATA
,
180 if (side_data_size
) {
181 ret
= adts_decode_extradata(s
, adts
, side_data
, side_data_size
);
184 ret
= ff_alloc_extradata(par
, side_data_size
);
187 memcpy(par
->extradata
, side_data
, side_data_size
);
190 if (adts
->write_adts
) {
191 int err
= adts_write_frame_header(s
, adts
, buf
, pkt
->size
,
195 avio_write(pb
, buf
, ADTS_HEADER_SIZE
);
196 if (adts
->pce_size
) {
197 avio_write(pb
, adts
->pce_data
, adts
->pce_size
);
201 avio_write(pb
, pkt
->data
, pkt
->size
);
206 static int adts_write_trailer(AVFormatContext
*s
)
208 ADTSContext
*adts
= s
->priv_data
;
216 #define ENC AV_OPT_FLAG_ENCODING_PARAM
217 #define OFFSET(obj) offsetof(ADTSContext, obj)
218 static const AVOption options
[] = {
219 { "write_id3v2", "Enable ID3v2 tag writing", OFFSET(id3v2tag
), AV_OPT_TYPE_BOOL
, {.i64
= 0}, 0, 1, ENC
},
220 { "write_apetag", "Enable APE tag writing", OFFSET(apetag
), AV_OPT_TYPE_BOOL
, {.i64
= 0}, 0, 1, ENC
},
221 { "write_mpeg2", "Set MPEG version to MPEG-2", OFFSET(mpeg_id
), AV_OPT_TYPE_BOOL
, {.i64
= 0}, 0, 1, ENC
},
225 static const AVClass adts_muxer_class
= {
226 .class_name
= "ADTS muxer",
227 .item_name
= av_default_item_name
,
229 .version
= LIBAVUTIL_VERSION_INT
,
232 const FFOutputFormat ff_adts_muxer
= {
234 .p
.long_name
= NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
235 .p
.mime_type
= "audio/aac",
236 .p
.extensions
= "aac,adts",
237 .priv_data_size
= sizeof(ADTSContext
),
238 .p
.audio_codec
= AV_CODEC_ID_AAC
,
239 .p
.video_codec
= AV_CODEC_ID_NONE
,
240 .p
.subtitle_codec
= AV_CODEC_ID_NONE
,
241 .flags_internal
= FF_OFMT_FLAG_MAX_ONE_OF_EACH
|
242 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
,
244 .write_header
= adts_write_header
,
245 .write_packet
= adts_write_packet
,
246 .write_trailer
= adts_write_trailer
,
247 .p
.priv_class
= &adts_muxer_class
,
248 .p
.flags
= AVFMT_NOTIMESTAMPS
,