3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4 * Mans Rullgard <mru@inprovide.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "bitstream.h"
23 #define ADTS_HEADER_SIZE 7
28 int sample_rate_index;
32 static int decode_extradata(ADTSContext *adts, uint8_t *buf, int size)
36 init_get_bits(&gb, buf, size * 8);
37 adts->objecttype = get_bits(&gb, 5) - 1;
38 adts->sample_rate_index = get_bits(&gb, 4);
39 adts->channel_conf = get_bits(&gb, 4);
46 static int adts_write_header(AVFormatContext *s)
48 ADTSContext *adts = s->priv_data;
49 AVCodecContext *avc = s->streams[0]->codec;
51 if(avc->extradata_size > 0)
52 decode_extradata(adts, avc->extradata, avc->extradata_size);
57 static int adts_write_frame_header(AVFormatContext *s, int size)
59 ADTSContext *ctx = s->priv_data;
61 uint8_t buf[ADTS_HEADER_SIZE];
63 init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
65 /* adts_fixed_header */
66 put_bits(&pb, 12, 0xfff); /* syncword */
67 put_bits(&pb, 1, 0); /* ID */
68 put_bits(&pb, 2, 0); /* layer */
69 put_bits(&pb, 1, 1); /* protection_absent */
70 put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
71 put_bits(&pb, 4, ctx->sample_rate_index);
72 put_bits(&pb, 1, 0); /* private_bit */
73 put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
74 put_bits(&pb, 1, 0); /* original_copy */
75 put_bits(&pb, 1, 0); /* home */
77 /* adts_variable_header */
78 put_bits(&pb, 1, 0); /* copyright_identification_bit */
79 put_bits(&pb, 1, 0); /* copyright_identification_start */
80 put_bits(&pb, 13, ADTS_HEADER_SIZE + size); /* aac_frame_length */
81 put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
82 put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
85 put_buffer(&s->pb, buf, ADTS_HEADER_SIZE);
90 static int adts_write_trailer(AVFormatContext *s)
95 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
97 ADTSContext *adts = s->priv_data;
98 ByteIOContext *pb = &s->pb;
103 adts_write_frame_header(s, pkt->size);
104 put_buffer(pb, pkt->data, pkt->size);
105 put_flush_packet(pb);
110 AVOutputFormat adts_muxer = {