3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4 * Mans Rullgard <mans@mansr.com>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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/avcodec.h"
26 #include "libavcodec/mpeg4audio.h"
30 #define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1)
32 int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
36 MPEG4AudioConfig m4ac;
39 init_get_bits(&gb, buf, size * 8);
40 off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
43 skip_bits_long(&gb, off);
44 adts->objecttype = m4ac.object_type - 1;
45 adts->sample_rate_index = m4ac.sampling_index;
46 adts->channel_conf = m4ac.chan_config;
48 if (adts->objecttype > 3U) {
49 av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
52 if (adts->sample_rate_index == 15) {
53 av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
56 if (get_bits(&gb, 1)) {
57 av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
60 if (get_bits(&gb, 1)) {
61 av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
64 if (get_bits(&gb, 1)) {
65 av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
68 if (!adts->channel_conf) {
69 init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
71 put_bits(&pb, 3, 5); //ID_PCE
72 adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8;
81 static int adts_write_header(AVFormatContext *s)
83 ADTSContext *adts = s->priv_data;
84 AVCodecContext *avc = s->streams[0]->codec;
86 if (avc->extradata_size > 0 &&
87 ff_adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
93 int ff_adts_write_frame_header(ADTSContext *ctx,
94 uint8_t *buf, int size, int pce_size)
98 unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
99 if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
100 av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
101 full_frame_size, ADTS_MAX_FRAME_BYTES);
102 return AVERROR_INVALIDDATA;
105 init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
107 /* adts_fixed_header */
108 put_bits(&pb, 12, 0xfff); /* syncword */
109 put_bits(&pb, 1, 0); /* ID */
110 put_bits(&pb, 2, 0); /* layer */
111 put_bits(&pb, 1, 1); /* protection_absent */
112 put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
113 put_bits(&pb, 4, ctx->sample_rate_index);
114 put_bits(&pb, 1, 0); /* private_bit */
115 put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
116 put_bits(&pb, 1, 0); /* original_copy */
117 put_bits(&pb, 1, 0); /* home */
119 /* adts_variable_header */
120 put_bits(&pb, 1, 0); /* copyright_identification_bit */
121 put_bits(&pb, 1, 0); /* copyright_identification_start */
122 put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
123 put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
124 put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
131 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
133 ADTSContext *adts = s->priv_data;
134 AVIOContext *pb = s->pb;
135 uint8_t buf[ADTS_HEADER_SIZE];
139 if (adts->write_adts) {
140 int err = ff_adts_write_frame_header(adts, buf, pkt->size,
144 avio_write(pb, buf, ADTS_HEADER_SIZE);
145 if (adts->pce_size) {
146 avio_write(pb, adts->pce_data, adts->pce_size);
150 avio_write(pb, pkt->data, pkt->size);
156 AVOutputFormat ff_adts_muxer = {
158 .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC"),
159 .mime_type = "audio/aac",
160 .extensions = "aac,adts",
161 .priv_data_size = sizeof(ADTSContext),
162 .audio_codec = CODEC_ID_AAC,
163 .video_codec = CODEC_ID_NONE,
164 .write_header = adts_write_header,
165 .write_packet = adts_write_packet,