1 /*
2 * Copyright (c) 2012 Justin Ruggles
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg 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 FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Vorbis audio parser
24 *
25 * Determines the duration for each packet.
26 */
27
28 #include "config_components.h"
29
31
36
41 };
42
44 const uint8_t *buf, int buf_size)
45 {
46 /* Id header should be 30 bytes */
47 if (buf_size < 30) {
50 }
51
52 /* make sure this is the Id header */
53 if (buf[0] != 1) {
56 }
57
58 /* check for header signature */
59 if (memcmp(&buf[1], "vorbis", 6)) {
62 }
63
64 if (!(buf[29] & 0x1)) {
67 }
68
69 s->blocksize[0] = 1 << (buf[28] & 0xF);
70 s->blocksize[1] = 1 << (buf[28] >> 4);
71
72 return 0;
73 }
74
76 const uint8_t *buf, int buf_size)
77 {
79 uint8_t *rev_buf;
81 int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
82
83 /* avoid overread */
84 if (buf_size < 7) {
87 }
88
89 /* make sure this is the Setup header */
90 if (buf[0] != 5) {
93 }
94
95 /* check for header signature */
96 if (memcmp(&buf[1], "vorbis", 6)) {
99 }
100
101 /* reverse bytes so we can easily read backwards with get_bits() */
105 }
106 for (
i = 0;
i < buf_size;
i++)
107 rev_buf[
i] = buf[buf_size - 1 -
i];
109
110 got_framing_bit = 0;
114 break;
115 }
116 }
117 if (!got_framing_bit) {
120 goto bad_header;
121 }
122
123 /* Now we search backwards to find possible valid mode counts. This is not
124 * fool-proof because we could have false positive matches and read too
125 * far, but there isn't really any way to be sure without parsing through
126 * all the many variable-sized fields before the modes. This approach seems
127 * to work well in testing, and it is similar to how it is handled in
128 * liboggz. */
129 mode_count = 0;
130 got_mode_header = 0;
133 break;
135 mode_count++;
136 if (mode_count > 64)
137 break;
138 gb0 = gb;
139 if (
get_bits(&gb0, 6) + 1 == mode_count) {
140 got_mode_header = 1;
141 last_mode_count = mode_count;
142 }
143 }
144 if (!got_mode_header) {
147 goto bad_header;
148 }
149 /* All samples I've seen use <= 2 modes, so ask for a sample if we find
150 * more than that, as it is most likely a false positive. If we get any
151 * we may need to approach this the long way and parse the whole Setup
152 * header, but I hope very much that it never comes to that. */
153 if (last_mode_count > 2) {
155 "%d modes (either a false positive or a "
156 "sample from an unknown encoder)",
157 last_mode_count);
158 }
159 /* We're limiting the mode count to 63 so that we know that the previous
160 * block flag will be in the first packet byte. */
161 if (last_mode_count > 63) {
163 last_mode_count);
165 goto bad_header;
166 }
167 s->mode_count = mode_count = last_mode_count;
168 /* Determine the number of bits required to code the mode and turn that
169 * into a bitmask to directly access the mode from the first frame byte. */
170 s->mode_mask = ((1 << (
av_log2(mode_count - 1) + 1)) - 1) << 1;
171 /* The previous window flag is the next bit after the mode */
172 s->prev_mask = (
s->mode_mask | 0x1) + 1;
173
176 for (
i = mode_count - 1;
i >= 0;
i--) {
179 }
180
181 bad_header:
184 }
185
187 const uint8_t *extradata, int extradata_size)
188 {
189 const uint8_t *header_start[3];
190 int header_len[3];
192
194 s->extradata_parsed = 1;
195
197 extradata_size, 30,
198 header_start, header_len)) < 0) {
201 }
202
205
208
209 s->valid_extradata = 1;
210 s->previous_blocksize =
s->blocksize[
s->mode_blocksize[0]];
211
212 return 0;
213 }
214
216 int buf_size,
int *
flags)
217 {
219
220 if (
s->valid_extradata && buf_size > 0) {
221 int mode, current_blocksize;
222 int previous_blocksize =
s->previous_blocksize;
223
224 if (buf[0] & 1) {
225 /* If the user doesn't care about special packets, it's a bad one. */
227 goto bad_packet;
228
229 /* Set the flag for which kind of special packet it is. */
230 if (buf[0] == 1)
232 else if (buf[0] == 3)
234 else if (buf[0] == 5)
236 else
237 goto bad_packet;
238
239 /* Special packets have no duration. */
240 return 0;
241
242 bad_packet:
245 }
246 if (
s->mode_count == 1)
248 else
249 mode = (buf[0] &
s->mode_mask) >> 1;
250 if (
mode >=
s->mode_count) {
253 }
254 if(
s->mode_blocksize[
mode]){
255 int flag = !!(buf[0] &
s->prev_mask);
256 previous_blocksize =
s->blocksize[
flag];
257 }
258 current_blocksize =
s->blocksize[
s->mode_blocksize[
mode]];
259 duration = (previous_blocksize + current_blocksize) >> 2;
260 s->previous_blocksize = current_blocksize;
261 }
262
264 }
265
267 int buf_size)
268 {
270 }
271
273 {
274 if (
s->valid_extradata)
275 s->previous_blocksize =
s->blocksize[0];
276 }
277
279 {
281 }
282
284 int extradata_size)
285 {
288
291
296 }
297
299 }
300
301 #if CONFIG_VORBIS_PARSER
302
303 typedef struct VorbisParseContext {
305 } VorbisParseContext;
306
308 const uint8_t **poutbuf, int *poutbuf_size,
309 const uint8_t *buf, int buf_size)
310 {
311 VorbisParseContext *
s =
s1->priv_data;
313
316 }
318 goto end;
319
322
323 end:
324 /* always return the full packet. this parser isn't doing any splitting or
325 combining, only packet analysis */
326 *poutbuf = buf;
327 *poutbuf_size = buf_size;
328 return buf_size;
329 }
330
332 {
335 }
336
339 .priv_data_size = sizeof(VorbisParseContext),
340 .parser_parse = vorbis_parse,
341 .parser_close = vorbis_parser_close,
342 };
343 #endif /* CONFIG_VORBIS_PARSER */