yading@10
|
1 /*
|
yading@10
|
2 * Copyright (c) 2012 Justin Ruggles
|
yading@10
|
3 *
|
yading@10
|
4 * This file is part of Libav.
|
yading@10
|
5 *
|
yading@10
|
6 * Libav is free software; you can redistribute it and/or
|
yading@10
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
8 * License as published by the Free Software Foundation; either
|
yading@10
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
10 *
|
yading@10
|
11 * Libav is distributed in the hope that it will be useful,
|
yading@10
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
14 * Lesser General Public License for more details.
|
yading@10
|
15 *
|
yading@10
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
17 * License along with Libav; if not, write to the Free Software
|
yading@10
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
19 */
|
yading@10
|
20
|
yading@10
|
21 /**
|
yading@10
|
22 * @file
|
yading@10
|
23 * Vorbis audio parser
|
yading@10
|
24 *
|
yading@10
|
25 * Determines the duration for each packet.
|
yading@10
|
26 */
|
yading@10
|
27
|
yading@10
|
28 #include "get_bits.h"
|
yading@10
|
29 #include "parser.h"
|
yading@10
|
30 #include "xiph.h"
|
yading@10
|
31 #include "vorbis_parser.h"
|
yading@10
|
32
|
yading@10
|
33 static int parse_id_header(AVCodecContext *avctx, VorbisParseContext *s,
|
yading@10
|
34 const uint8_t *buf, int buf_size)
|
yading@10
|
35 {
|
yading@10
|
36 /* Id header should be 30 bytes */
|
yading@10
|
37 if (buf_size < 30) {
|
yading@10
|
38 av_log(avctx, AV_LOG_ERROR, "Id header is too short\n");
|
yading@10
|
39 return AVERROR_INVALIDDATA;
|
yading@10
|
40 }
|
yading@10
|
41
|
yading@10
|
42 /* make sure this is the Id header */
|
yading@10
|
43 if (buf[0] != 1) {
|
yading@10
|
44 av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Id header\n");
|
yading@10
|
45 return AVERROR_INVALIDDATA;
|
yading@10
|
46 }
|
yading@10
|
47
|
yading@10
|
48 /* check for header signature */
|
yading@10
|
49 if (memcmp(&buf[1], "vorbis", 6)) {
|
yading@10
|
50 av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
|
yading@10
|
51 return AVERROR_INVALIDDATA;
|
yading@10
|
52 }
|
yading@10
|
53
|
yading@10
|
54 if (!(buf[29] & 0x1)) {
|
yading@10
|
55 av_log(avctx, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
|
yading@10
|
56 return AVERROR_INVALIDDATA;
|
yading@10
|
57 }
|
yading@10
|
58
|
yading@10
|
59 s->blocksize[0] = 1 << (buf[28] & 0xF);
|
yading@10
|
60 s->blocksize[1] = 1 << (buf[28] >> 4);
|
yading@10
|
61
|
yading@10
|
62 return 0;
|
yading@10
|
63 }
|
yading@10
|
64
|
yading@10
|
65 static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
|
yading@10
|
66 const uint8_t *buf, int buf_size)
|
yading@10
|
67 {
|
yading@10
|
68 GetBitContext gb, gb0;
|
yading@10
|
69 uint8_t *rev_buf;
|
yading@10
|
70 int i, ret = 0;
|
yading@10
|
71 int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
|
yading@10
|
72
|
yading@10
|
73 /* avoid overread */
|
yading@10
|
74 if (buf_size < 7) {
|
yading@10
|
75 av_log(avctx, AV_LOG_ERROR, "Setup header is too short\n");
|
yading@10
|
76 return AVERROR_INVALIDDATA;
|
yading@10
|
77 }
|
yading@10
|
78
|
yading@10
|
79 /* make sure this is the Setup header */
|
yading@10
|
80 if (buf[0] != 5) {
|
yading@10
|
81 av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
|
yading@10
|
82 return AVERROR_INVALIDDATA;
|
yading@10
|
83 }
|
yading@10
|
84
|
yading@10
|
85 /* check for header signature */
|
yading@10
|
86 if (memcmp(&buf[1], "vorbis", 6)) {
|
yading@10
|
87 av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
|
yading@10
|
88 return AVERROR_INVALIDDATA;
|
yading@10
|
89 }
|
yading@10
|
90
|
yading@10
|
91 /* reverse bytes so we can easily read backwards with get_bits() */
|
yading@10
|
92 if (!(rev_buf = av_malloc(buf_size))) {
|
yading@10
|
93 av_log(avctx, AV_LOG_ERROR, "Out of memory\n");
|
yading@10
|
94 return AVERROR(ENOMEM);
|
yading@10
|
95 }
|
yading@10
|
96 for (i = 0; i < buf_size; i++)
|
yading@10
|
97 rev_buf[i] = buf[buf_size - 1 - i];
|
yading@10
|
98 init_get_bits(&gb, rev_buf, buf_size * 8);
|
yading@10
|
99
|
yading@10
|
100 got_framing_bit = 0;
|
yading@10
|
101 while (get_bits_left(&gb) > 97) {
|
yading@10
|
102 if (get_bits1(&gb)) {
|
yading@10
|
103 got_framing_bit = get_bits_count(&gb);
|
yading@10
|
104 break;
|
yading@10
|
105 }
|
yading@10
|
106 }
|
yading@10
|
107 if (!got_framing_bit) {
|
yading@10
|
108 av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
|
yading@10
|
109 ret = AVERROR_INVALIDDATA;
|
yading@10
|
110 goto bad_header;
|
yading@10
|
111 }
|
yading@10
|
112
|
yading@10
|
113 /* Now we search backwards to find possible valid mode counts. This is not
|
yading@10
|
114 * fool-proof because we could have false positive matches and read too
|
yading@10
|
115 * far, but there isn't really any way to be sure without parsing through
|
yading@10
|
116 * all the many variable-sized fields before the modes. This approach seems
|
yading@10
|
117 * to work well in testing, and it is similar to how it is handled in
|
yading@10
|
118 * liboggz. */
|
yading@10
|
119 mode_count = 0;
|
yading@10
|
120 got_mode_header = 0;
|
yading@10
|
121 while (get_bits_left(&gb) >= 97) {
|
yading@10
|
122 if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
|
yading@10
|
123 break;
|
yading@10
|
124 skip_bits(&gb, 1);
|
yading@10
|
125 mode_count++;
|
yading@10
|
126 if (mode_count > 64)
|
yading@10
|
127 break;
|
yading@10
|
128 gb0 = gb;
|
yading@10
|
129 if (get_bits(&gb0, 6) + 1 == mode_count) {
|
yading@10
|
130 got_mode_header = 1;
|
yading@10
|
131 last_mode_count = mode_count;
|
yading@10
|
132 }
|
yading@10
|
133 }
|
yading@10
|
134 if (!got_mode_header) {
|
yading@10
|
135 av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
|
yading@10
|
136 ret = AVERROR_INVALIDDATA;
|
yading@10
|
137 goto bad_header;
|
yading@10
|
138 }
|
yading@10
|
139 /* All samples I've seen use <= 2 modes, so ask for a sample if we find
|
yading@10
|
140 * more than that, as it is most likely a false positive. If we get any
|
yading@10
|
141 * we may need to approach this the long way and parse the whole Setup
|
yading@10
|
142 * header, but I hope very much that it never comes to that. */
|
yading@10
|
143 if (last_mode_count > 2) {
|
yading@10
|
144 avpriv_request_sample(avctx,
|
yading@10
|
145 "%d modes (either a false positive or a "
|
yading@10
|
146 "sample from an unknown encoder)",
|
yading@10
|
147 last_mode_count);
|
yading@10
|
148 }
|
yading@10
|
149 /* We're limiting the mode count to 63 so that we know that the previous
|
yading@10
|
150 * block flag will be in the first packet byte. */
|
yading@10
|
151 if (last_mode_count > 63) {
|
yading@10
|
152 av_log(avctx, AV_LOG_ERROR, "Unsupported mode count: %d\n",
|
yading@10
|
153 last_mode_count);
|
yading@10
|
154 ret = AVERROR_INVALIDDATA;
|
yading@10
|
155 goto bad_header;
|
yading@10
|
156 }
|
yading@10
|
157 s->mode_count = mode_count = last_mode_count;
|
yading@10
|
158 /* Determine the number of bits required to code the mode and turn that
|
yading@10
|
159 * into a bitmask to directly access the mode from the first frame byte. */
|
yading@10
|
160 s->mode_mask = ((1 << (av_log2(mode_count - 1) + 1)) - 1) << 1;
|
yading@10
|
161 /* The previous window flag is the next bit after the mode */
|
yading@10
|
162 s->prev_mask = (s->mode_mask | 0x1) + 1;
|
yading@10
|
163
|
yading@10
|
164 init_get_bits(&gb, rev_buf, buf_size * 8);
|
yading@10
|
165 skip_bits_long(&gb, got_framing_bit);
|
yading@10
|
166 for (i = mode_count - 1; i >= 0; i--) {
|
yading@10
|
167 skip_bits_long(&gb, 40);
|
yading@10
|
168 s->mode_blocksize[i] = get_bits1(&gb);
|
yading@10
|
169 }
|
yading@10
|
170
|
yading@10
|
171 bad_header:
|
yading@10
|
172 av_free(rev_buf);
|
yading@10
|
173 return ret;
|
yading@10
|
174 }
|
yading@10
|
175
|
yading@10
|
176 int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s)
|
yading@10
|
177 {
|
yading@10
|
178 uint8_t *header_start[3];
|
yading@10
|
179 int header_len[3];
|
yading@10
|
180 int ret;
|
yading@10
|
181
|
yading@10
|
182 s->avctx = avctx;
|
yading@10
|
183 s->extradata_parsed = 1;
|
yading@10
|
184
|
yading@10
|
185 if ((ret = avpriv_split_xiph_headers(avctx->extradata,
|
yading@10
|
186 avctx->extradata_size, 30,
|
yading@10
|
187 header_start, header_len)) < 0) {
|
yading@10
|
188 av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n");
|
yading@10
|
189 return ret;
|
yading@10
|
190 }
|
yading@10
|
191
|
yading@10
|
192 if ((ret = parse_id_header(avctx, s, header_start[0], header_len[0])) < 0)
|
yading@10
|
193 return ret;
|
yading@10
|
194
|
yading@10
|
195 if ((ret = parse_setup_header(avctx, s, header_start[2], header_len[2])) < 0)
|
yading@10
|
196 return ret;
|
yading@10
|
197
|
yading@10
|
198 s->valid_extradata = 1;
|
yading@10
|
199 s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
|
yading@10
|
200
|
yading@10
|
201 return 0;
|
yading@10
|
202 }
|
yading@10
|
203
|
yading@10
|
204 int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
|
yading@10
|
205 int buf_size)
|
yading@10
|
206 {
|
yading@10
|
207 int duration = 0;
|
yading@10
|
208
|
yading@10
|
209 if (s->valid_extradata && buf_size > 0) {
|
yading@10
|
210 int mode, current_blocksize;
|
yading@10
|
211 int previous_blocksize = s->previous_blocksize;
|
yading@10
|
212
|
yading@10
|
213 if (buf[0] & 1) {
|
yading@10
|
214 av_log(s->avctx, AV_LOG_ERROR, "Invalid packet\n");
|
yading@10
|
215 return AVERROR_INVALIDDATA;
|
yading@10
|
216 }
|
yading@10
|
217 if (s->mode_count == 1)
|
yading@10
|
218 mode = 0;
|
yading@10
|
219 else
|
yading@10
|
220 mode = (buf[0] & s->mode_mask) >> 1;
|
yading@10
|
221 if (mode >= s->mode_count) {
|
yading@10
|
222 av_log(s->avctx, AV_LOG_ERROR, "Invalid mode in packet\n");
|
yading@10
|
223 return AVERROR_INVALIDDATA;
|
yading@10
|
224 }
|
yading@10
|
225 if(s->mode_blocksize[mode]){
|
yading@10
|
226 int flag = !!(buf[0] & s->prev_mask);
|
yading@10
|
227 previous_blocksize = s->blocksize[flag];
|
yading@10
|
228 }
|
yading@10
|
229 current_blocksize = s->blocksize[s->mode_blocksize[mode]];
|
yading@10
|
230 duration = (previous_blocksize + current_blocksize) >> 2;
|
yading@10
|
231 s->previous_blocksize = current_blocksize;
|
yading@10
|
232 }
|
yading@10
|
233
|
yading@10
|
234 return duration;
|
yading@10
|
235 }
|
yading@10
|
236
|
yading@10
|
237 void avpriv_vorbis_parse_reset(VorbisParseContext *s)
|
yading@10
|
238 {
|
yading@10
|
239 if (s->valid_extradata)
|
yading@10
|
240 s->previous_blocksize = s->blocksize[0];
|
yading@10
|
241 }
|
yading@10
|
242
|
yading@10
|
243 #if CONFIG_VORBIS_PARSER
|
yading@10
|
244 static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
|
yading@10
|
245 const uint8_t **poutbuf, int *poutbuf_size,
|
yading@10
|
246 const uint8_t *buf, int buf_size)
|
yading@10
|
247 {
|
yading@10
|
248 VorbisParseContext *s = s1->priv_data;
|
yading@10
|
249 int duration;
|
yading@10
|
250
|
yading@10
|
251 if (!s->extradata_parsed && avctx->extradata && avctx->extradata_size)
|
yading@10
|
252 if (avpriv_vorbis_parse_extradata(avctx, s))
|
yading@10
|
253 goto end;
|
yading@10
|
254
|
yading@10
|
255 if ((duration = avpriv_vorbis_parse_frame(s, buf, buf_size)) >= 0)
|
yading@10
|
256 s1->duration = duration;
|
yading@10
|
257
|
yading@10
|
258 end:
|
yading@10
|
259 /* always return the full packet. this parser isn't doing any splitting or
|
yading@10
|
260 combining, only packet analysis */
|
yading@10
|
261 *poutbuf = buf;
|
yading@10
|
262 *poutbuf_size = buf_size;
|
yading@10
|
263 return buf_size;
|
yading@10
|
264 }
|
yading@10
|
265
|
yading@10
|
266 AVCodecParser ff_vorbis_parser = {
|
yading@10
|
267 .codec_ids = { AV_CODEC_ID_VORBIS },
|
yading@10
|
268 .priv_data_size = sizeof(VorbisParseContext),
|
yading@10
|
269 .parser_parse = vorbis_parse,
|
yading@10
|
270 };
|
yading@10
|
271 #endif /* CONFIG_VORBIS_PARSER */
|