1 | /*
|
---|
2 | * ADTS muxer.
|
---|
3 | * Copyright (c) 2006 Baptiste Coudurier <[email protected]>
|
---|
4 | * Mans Rullgard <[email protected]>
|
---|
5 | *
|
---|
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.
|
---|
10 | *
|
---|
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.
|
---|
15 | *
|
---|
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
|
---|
19 | */
|
---|
20 | #include "avformat.h"
|
---|
21 | #include "bitstream.h"
|
---|
22 |
|
---|
23 | #define ADTS_HEADER_SIZE 7
|
---|
24 |
|
---|
25 | typedef struct {
|
---|
26 | int write_adts;
|
---|
27 | int objecttype;
|
---|
28 | int sample_rate_index;
|
---|
29 | int channel_conf;
|
---|
30 | } ADTSContext;
|
---|
31 |
|
---|
32 | static int decode_extradata(ADTSContext *adts, uint8_t *buf, int size)
|
---|
33 | {
|
---|
34 | GetBitContext gb;
|
---|
35 |
|
---|
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);
|
---|
40 |
|
---|
41 | adts->write_adts = 1;
|
---|
42 |
|
---|
43 | return 0;
|
---|
44 | }
|
---|
45 |
|
---|
46 | static int adts_write_header(AVFormatContext *s)
|
---|
47 | {
|
---|
48 | ADTSContext *adts = s->priv_data;
|
---|
49 | AVCodecContext *avc = s->streams[0]->codec;
|
---|
50 |
|
---|
51 | if(avc->extradata_size > 0)
|
---|
52 | decode_extradata(adts, avc->extradata, avc->extradata_size);
|
---|
53 |
|
---|
54 | return 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | static int adts_write_frame_header(AVFormatContext *s, int size)
|
---|
58 | {
|
---|
59 | ADTSContext *ctx = s->priv_data;
|
---|
60 | PutBitContext pb;
|
---|
61 | uint8_t buf[ADTS_HEADER_SIZE];
|
---|
62 |
|
---|
63 | init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
|
---|
64 |
|
---|
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 */
|
---|
76 |
|
---|
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 */
|
---|
83 |
|
---|
84 | flush_put_bits(&pb);
|
---|
85 | put_buffer(&s->pb, buf, ADTS_HEADER_SIZE);
|
---|
86 |
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | static int adts_write_trailer(AVFormatContext *s)
|
---|
91 | {
|
---|
92 | return 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
96 | {
|
---|
97 | ADTSContext *adts = s->priv_data;
|
---|
98 | ByteIOContext *pb = &s->pb;
|
---|
99 |
|
---|
100 | if (!pkt->size)
|
---|
101 | return 0;
|
---|
102 | if(adts->write_adts)
|
---|
103 | adts_write_frame_header(s, pkt->size);
|
---|
104 | put_buffer(pb, pkt->data, pkt->size);
|
---|
105 | put_flush_packet(pb);
|
---|
106 |
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | static AVOutputFormat adts_muxer = {
|
---|
111 | "adts",
|
---|
112 | "ADTS AAC",
|
---|
113 | "audio/aac",
|
---|
114 | "aac",
|
---|
115 | sizeof(ADTSContext),
|
---|
116 | CODEC_ID_AAC,
|
---|
117 | CODEC_ID_NONE,
|
---|
118 | adts_write_header,
|
---|
119 | adts_write_packet,
|
---|
120 | adts_write_trailer,
|
---|
121 | };
|
---|
122 |
|
---|
123 | int ff_adts_init(void)
|
---|
124 | {
|
---|
125 | av_register_output_format(&adts_muxer);
|
---|
126 | return 0;
|
---|
127 | }
|
---|