../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
106 / 151
70.2%
Branches:
54 / 86
62.8%
Functions:
9 / 9
100.0%

libavcodec/vorbis_parser.c
Line Branch Exec Source
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
30 #include "libavutil/log.h"
31 #include "libavutil/mem.h"
32
33 #include "get_bits.h"
34 #include "parser_internal.h"
35 #include "xiph.h"
36 #include "vorbis_parser_internal.h"
37
38 static const AVClass vorbis_parser_class = {
39 .class_name = "Vorbis parser",
40 .item_name = av_default_item_name,
41 .version = LIBAVUTIL_VERSION_INT,
42 };
43
44 57 static int parse_id_header(AVVorbisParseContext *s,
45 const uint8_t *buf, int buf_size)
46 {
47 /* Id header should be 30 bytes */
48
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 57 times.
57 if (buf_size < 30) {
49 ✗ av_log(s, AV_LOG_ERROR, "Id header is too short\n");
50 ✗ return AVERROR_INVALIDDATA;
51 }
52
53 /* make sure this is the Id header */
54
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 57 times.
57 if (buf[0] != 1) {
55 ✗ av_log(s, AV_LOG_ERROR, "Wrong packet type in Id header\n");
56 ✗ return AVERROR_INVALIDDATA;
57 }
58
59 /* check for header signature */
60
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 57 times.
57 if (memcmp(&buf[1], "vorbis", 6)) {
61 ✗ av_log(s, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
62 ✗ return AVERROR_INVALIDDATA;
63 }
64
65
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 57 times.
57 if (!(buf[29] & 0x1)) {
66 ✗ av_log(s, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
67 ✗ return AVERROR_INVALIDDATA;
68 }
69
70 57 s->blocksize[0] = 1 << (buf[28] & 0xF);
71 57 s->blocksize[1] = 1 << (buf[28] >> 4);
72
73 57 return 0;
74 }
75
76 57 static int parse_setup_header(AVVorbisParseContext *s,
77 const uint8_t *buf, int buf_size)
78 {
79 GetBitContext gb, gb0;
80 uint8_t *rev_buf;
81 57 int i, ret = 0;
82 57 int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
83
84 /* avoid overread */
85
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 57 times.
57 if (buf_size < 7) {
86 ✗ av_log(s, AV_LOG_ERROR, "Setup header is too short\n");
87 ✗ return AVERROR_INVALIDDATA;
88 }
89
90 /* make sure this is the Setup header */
91
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 57 times.
57 if (buf[0] != 5) {
92 ✗ av_log(s, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
93 ✗ return AVERROR_INVALIDDATA;
94 }
95
96 /* check for header signature */
97
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 57 times.
57 if (memcmp(&buf[1], "vorbis", 6)) {
98 ✗ av_log(s, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
99 ✗ return AVERROR_INVALIDDATA;
100 }
101
102 /* reverse bytes so we can easily read backwards with get_bits() */
103
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 57 times.
57 if (!(rev_buf = av_malloc(buf_size))) {
104 ✗ av_log(s, AV_LOG_ERROR, "Out of memory\n");
105 ✗ return AVERROR(ENOMEM);
106 }
107
2/2
✓ Branch 17 → 16 taken 314866 times.
✓ Branch 17 → 18 taken 57 times.
314923 for (i = 0; i < buf_size; i++)
108 314866 rev_buf[i] = buf[buf_size - 1 - i];
109 57 init_get_bits(&gb, rev_buf, buf_size * 8);
110
111 57 got_framing_bit = 0;
112
1/2
✓ Branch 25 → 20 taken 293 times.
✗ Branch 25 → 26 not taken.
293 while (get_bits_left(&gb) > 97) {
113
2/2
✓ Branch 21 → 22 taken 57 times.
✓ Branch 21 → 24 taken 236 times.
293 if (get_bits1(&gb)) {
114 57 got_framing_bit = get_bits_count(&gb);
115 57 break;
116 }
117 }
118
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 29 taken 57 times.
57 if (!got_framing_bit) {
119 ✗ av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
120 ✗ ret = AVERROR_INVALIDDATA;
121 ✗ goto bad_header;
122 }
123
124 /* Now we search backwards to find possible valid mode counts. This is not
125 * fool-proof because we could have false positive matches and read too
126 * far, but there isn't really any way to be sure without parsing through
127 * all the many variable-sized fields before the modes. This approach seems
128 * to work well in testing, and it is similar to how it is handled in
129 * liboggz. */
130 57 mode_count = 0;
131 57 got_mode_header = 0;
132
1/2
✓ Branch 44 → 30 taken 171 times.
✗ Branch 44 → 45 not taken.
171 while (get_bits_left(&gb) >= 97) {
133
5/6
✓ Branch 31 → 32 taken 171 times.
✗ Branch 31 → 45 not taken.
✓ Branch 33 → 34 taken 115 times.
✓ Branch 33 → 45 taken 56 times.
✓ Branch 35 → 36 taken 114 times.
✓ Branch 35 → 45 taken 1 time.
171 if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
134 break;
135 114 skip_bits(&gb, 1);
136 114 mode_count++;
137
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 39 taken 114 times.
114 if (mode_count > 64)
138 ✗ break;
139 114 gb0 = gb;
140
1/2
✓ Branch 40 → 41 taken 114 times.
✗ Branch 40 → 42 not taken.
114 if (get_bits(&gb0, 6) + 1 == mode_count) {
141 114 got_mode_header = 1;
142 114 last_mode_count = mode_count;
143 }
144 }
145
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 48 taken 57 times.
57 if (!got_mode_header) {
146 ✗ av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
147 ✗ ret = AVERROR_INVALIDDATA;
148 ✗ goto bad_header;
149 }
150 /* All samples I've seen use <= 2 modes, so ask for a sample if we find
151 * more than that, as it is most likely a false positive. If we get any
152 * we may need to approach this the long way and parse the whole Setup
153 * header, but I hope very much that it never comes to that. */
154
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 57 times.
57 if (last_mode_count > 2) {
155 ✗ avpriv_request_sample(s,
156 "%d modes (either a false positive or a "
157 "sample from an unknown encoder)",
158 last_mode_count);
159 }
160 /* We're limiting the mode count to 63 so that we know that the previous
161 * block flag will be in the first packet byte. */
162
1/2
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 53 taken 57 times.
57 if (last_mode_count > 63) {
163 ✗ av_log(s, AV_LOG_ERROR, "Unsupported mode count: %d\n",
164 last_mode_count);
165 ✗ ret = AVERROR_INVALIDDATA;
166 ✗ goto bad_header;
167 }
168 57 s->mode_count = mode_count = last_mode_count;
169 /* Determine the number of bits required to code the mode and turn that
170 * into a bitmask to directly access the mode from the first frame byte. */
171 57 s->mode_mask = ((1 << (av_log2(mode_count - 1) + 1)) - 1) << 1;
172 /* The previous window flag is the next bit after the mode */
173 57 s->prev_mask = (s->mode_mask | 0x1) + 1;
174
175 57 init_get_bits(&gb, rev_buf, buf_size * 8);
176 57 skip_bits_long(&gb, got_framing_bit);
177
2/2
✓ Branch 59 → 56 taken 114 times.
✓ Branch 59 → 60 taken 57 times.
171 for (i = mode_count - 1; i >= 0; i--) {
178 114 skip_bits_long(&gb, 40);
179 114 s->mode_blocksize[i] = get_bits1(&gb);
180 }
181
182 57 bad_header:
183 57 av_free(rev_buf);
184 57 return ret;
185 }
186
187 57 int ff_vorbis_parse_init(AVVorbisParseContext *s,
188 const uint8_t *extradata, int extradata_size)
189 {
190 const uint8_t *header_start[3];
191 int header_len[3];
192 int ret;
193
194 57 s->class = &vorbis_parser_class;
195 57 s->extradata_parsed = 1;
196
197
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 57 times.
57 if ((ret = avpriv_split_xiph_headers(extradata,
198 extradata_size, 30,
199 header_start, header_len)) < 0) {
200 ✗ av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
201 ✗ return ret;
202 }
203
204
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 57 times.
57 if ((ret = parse_id_header(s, header_start[0], header_len[0])) < 0)
205 ✗ return ret;
206
207
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 57 times.
57 if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
208 ✗ return ret;
209
210 57 s->valid_extradata = 1;
211 57 s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
212
213 57 return 0;
214 }
215
216 13234 int av_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf,
217 int buf_size, int *flags)
218 {
219 13234 int duration = 0;
220
221
3/4
✓ Branch 2 → 3 taken 13234 times.
✗ Branch 2 → 26 not taken.
✓ Branch 3 → 4 taken 13225 times.
✓ Branch 3 → 26 taken 9 times.
13234 if (s->valid_extradata && buf_size > 0) {
222 int mode, current_blocksize;
223 13225 int previous_blocksize = s->previous_blocksize;
224
225
2/2
✓ Branch 4 → 5 taken 23 times.
✓ Branch 4 → 17 taken 13202 times.
13225 if (buf[0] & 1) {
226 /* If the user doesn't care about special packets, it's a bad one. */
227
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 23 times.
23 if (!flags)
228 ✗ goto bad_packet;
229
230 /* Set the flag for which kind of special packet it is. */
231
2/2
✓ Branch 7 → 8 taken 7 times.
✓ Branch 7 → 9 taken 16 times.
23 if (buf[0] == 1)
232 7 *flags |= VORBIS_FLAG_HEADER;
233
2/2
✓ Branch 9 → 10 taken 8 times.
✓ Branch 9 → 11 taken 8 times.
16 else if (buf[0] == 3)
234 8 *flags |= VORBIS_FLAG_COMMENT;
235
1/2
✓ Branch 11 → 12 taken 8 times.
✗ Branch 11 → 13 not taken.
8 else if (buf[0] == 5)
236 8 *flags |= VORBIS_FLAG_SETUP;
237 else
238 ✗ av_log(s, AV_LOG_VERBOSE, "Ignoring packet with unknown type %u\n",
239 ✗ buf[0]);
240
241 /* Special packets have no duration. */
242 23 return 0;
243
244 ✗ bad_packet:
245 ✗ av_log(s, AV_LOG_ERROR, "Invalid packet\n");
246 ✗ return AVERROR_INVALIDDATA;
247 }
248
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 13202 times.
13202 if (s->mode_count == 1)
249 ✗ mode = 0;
250 else
251 13202 mode = (buf[0] & s->mode_mask) >> 1;
252
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 23 taken 13202 times.
13202 if (mode >= s->mode_count) {
253 ✗ av_log(s, AV_LOG_ERROR, "Invalid mode in packet\n");
254 ✗ return AVERROR_INVALIDDATA;
255 }
256
2/2
✓ Branch 23 → 24 taken 8275 times.
✓ Branch 23 → 25 taken 4927 times.
13202 if(s->mode_blocksize[mode]){
257 8275 int flag = !!(buf[0] & s->prev_mask);
258 8275 previous_blocksize = s->blocksize[flag];
259 }
260 13202 current_blocksize = s->blocksize[s->mode_blocksize[mode]];
261 13202 duration = (previous_blocksize + current_blocksize) >> 2;
262 13202 s->previous_blocksize = current_blocksize;
263 }
264
265 13211 return duration;
266 }
267
268 582 int av_vorbis_parse_frame(AVVorbisParseContext *s, const uint8_t *buf,
269 int buf_size)
270 {
271 582 return av_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
272 }
273
274 103 void av_vorbis_parse_reset(AVVorbisParseContext *s)
275 {
276
1/2
✓ Branch 2 → 3 taken 103 times.
✗ Branch 2 → 4 not taken.
103 if (s->valid_extradata)
277 103 s->previous_blocksize = s->blocksize[0];
278 103 }
279
280 46 void av_vorbis_parse_free(AVVorbisParseContext **s)
281 {
282 46 av_freep(s);
283 46 }
284
285 46 AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
286 int extradata_size)
287 {
288 46 AVVorbisParseContext *s = av_mallocz(sizeof(*s));
289 int ret;
290
291
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 46 times.
46 if (!s)
292 ✗ return NULL;
293
294 46 ret = ff_vorbis_parse_init(s, extradata, extradata_size);
295
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 46 times.
46 if (ret < 0) {
296 ✗ av_vorbis_parse_free(&s);
297 ✗ return NULL;
298 }
299
300 46 return s;
301 }
302
303 #if CONFIG_VORBIS_PARSER
304
305 582 static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
306 const uint8_t **poutbuf, int *poutbuf_size,
307 const uint8_t *buf, int buf_size)
308 {
309 582 AVVorbisParseContext *s = s1->priv_data;
310 int duration;
311
312
4/6
✓ Branch 2 → 3 taken 11 times.
✓ Branch 2 → 6 taken 571 times.
✓ Branch 3 → 4 taken 11 times.
✗ Branch 3 → 6 not taken.
✓ Branch 4 → 5 taken 11 times.
✗ Branch 4 → 6 not taken.
582 if (!s->extradata_parsed && avctx->extradata && avctx->extradata_size) {
313 11 ff_vorbis_parse_init(s, avctx->extradata, avctx->extradata_size);
314 }
315
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 582 times.
582 if (!s->valid_extradata)
316 ✗ goto end;
317
318
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 582 times.
582 if ((duration = av_vorbis_parse_frame(s, buf, buf_size)) >= 0)
319 582 s1->duration = duration;
320
321 ✗ end:
322 /* always return the full packet. this parser isn't doing any splitting or
323 combining, only packet analysis */
324 582 *poutbuf = buf;
325 582 *poutbuf_size = buf_size;
326 582 return buf_size;
327 }
328
329 const FFCodecParser ff_vorbis_parser = {
330 PARSER_CODEC_LIST(AV_CODEC_ID_VORBIS),
331 .priv_data_size = sizeof(AVVorbisParseContext),
332 .parse = vorbis_parse,
333 };
334 #endif /* CONFIG_VORBIS_PARSER */
335