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
32
35 {
36 /* Id header should be 30 bytes */
37 if (buf_size < 30) {
40 }
41
42 /* make sure this is the Id header */
43 if (buf[0] != 1) {
46 }
47
48 /* check for header signature */
49 if (memcmp(&buf[1], "vorbis", 6)) {
52 }
53
54 if (!(buf[29] & 0x1)) {
57 }
58
61
62 return 0;
63 }
64
67 {
71 int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
72
73 /* avoid overread */
74 if (buf_size < 7) {
77 }
78
79 /* make sure this is the Setup header */
80 if (buf[0] != 5) {
83 }
84
85 /* check for header signature */
86 if (memcmp(&buf[1], "vorbis", 6)) {
89 }
90
91 /* reverse bytes so we can easily read backwards with get_bits() */
95 }
96 for (i = 0; i < buf_size; i++)
97 rev_buf[i] = buf[buf_size - 1 - i];
99
100 got_framing_bit = 0;
104 break;
105 }
106 }
107 if (!got_framing_bit) {
110 goto bad_header;
111 }
112
113 /* Now we search backwards to find possible valid mode counts. This is not
114 * fool-proof because we could have false positive matches and read too
115 * far, but there isn't really any way to be sure without parsing through
116 * all the many variable-sized fields before the modes. This approach seems
117 * to work well in testing, and it is similar to how it is handled in
118 * liboggz. */
119 mode_count = 0;
120 got_mode_header = 0;
123 break;
125 mode_count++;
126 if (mode_count > 64)
127 break;
128 gb0 = gb;
129 if (
get_bits(&gb0, 6) + 1 == mode_count) {
130 got_mode_header = 1;
131 last_mode_count = mode_count;
132 }
133 }
134 if (!got_mode_header) {
137 goto bad_header;
138 }
139 /* All samples I've seen use <= 2 modes, so ask for a sample if we find
140 * more than that, as it is most likely a false positive. If we get any
141 * we may need to approach this the long way and parse the whole Setup
142 * header, but I hope very much that it never comes to that. */
143 if (last_mode_count > 2) {
145 "%d modes (either a false positive or a "
146 "sample from an unknown encoder)",
147 last_mode_count);
148 }
149 /* We're limiting the mode count to 63 so that we know that the previous
150 * block flag will be in the first packet byte. */
151 if (last_mode_count > 63) {
153 last_mode_count);
155 goto bad_header;
156 }
158 /* Determine the number of bits required to code the mode and turn that
159 * into a bitmask to directly access the mode from the first frame byte. */
161 /* The previous window flag is the next bit after the mode */
163
166 for (i = mode_count - 1; i >= 0; i--) {
169 }
170
171 bad_header:
174 }
175
177 {
179 int header_len[3];
181
184
187 header_start, header_len)) < 0) {
190 }
191
192 if ((ret =
parse_id_header(avctx, s, header_start[0], header_len[0])) < 0)
194
197
200
201 return 0;
202 }
203
205 int buf_size)
206 {
208
210 int mode, current_blocksize;
212
213 if (buf[0] & 1) {
216 }
218 mode = 0;
219 else
224 }
228 }
230 duration = (previous_blocksize + current_blocksize) >> 2;
232 }
233
235 }
236
238 {
241 }
242
243 #if CONFIG_VORBIS_PARSER
245 const uint8_t **poutbuf,
int *poutbuf_size,
247 {
250
254
257
259 /* always return the full packet. this parser isn't doing any splitting or
260 combining, only packet analysis */
262 *poutbuf_size = buf_size;
263 return buf_size;
264 }
265
269 .parser_parse = vorbis_parse,
270 };
271 #endif /* CONFIG_VORBIS_PARSER */