FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vorbis_parser.c
Date: 2026-01-23 14:02:51
Exec Total Coverage
Lines: 110 155 71.0%
Functions: 10 10 100.0%
Branches: 54 86 62.8%

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 39 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 0 not taken.
✓ Branch 1 taken 39 times.
39 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 0 not taken.
✓ Branch 1 taken 39 times.
39 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 0 not taken.
✓ Branch 1 taken 39 times.
39 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 0 not taken.
✓ Branch 1 taken 39 times.
39 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 39 s->blocksize[0] = 1 << (buf[28] & 0xF);
71 39 s->blocksize[1] = 1 << (buf[28] >> 4);
72
73 39 return 0;
74 }
75
76 39 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 39 int i, ret = 0;
82 39 int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
83
84 /* avoid overread */
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 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 0 not taken.
✓ Branch 1 taken 39 times.
39 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 0 not taken.
✓ Branch 1 taken 39 times.
39 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 1 not taken.
✓ Branch 2 taken 39 times.
39 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 0 taken 199836 times.
✓ Branch 1 taken 39 times.
199875 for (i = 0; i < buf_size; i++)
108 199836 rev_buf[i] = buf[buf_size - 1 - i];
109 39 init_get_bits(&gb, rev_buf, buf_size * 8);
110
111 39 got_framing_bit = 0;
112
1/2
✓ Branch 1 taken 191 times.
✗ Branch 2 not taken.
191 while (get_bits_left(&gb) > 97) {
113
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 152 times.
191 if (get_bits1(&gb)) {
114 39 got_framing_bit = get_bits_count(&gb);
115 39 break;
116 }
117 }
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 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 39 mode_count = 0;
131 39 got_mode_header = 0;
132
1/2
✓ Branch 1 taken 117 times.
✗ Branch 2 not taken.
117 while (get_bits_left(&gb) >= 97) {
133
5/6
✓ Branch 1 taken 117 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 79 times.
✓ Branch 5 taken 38 times.
✓ Branch 7 taken 78 times.
✓ Branch 8 taken 1 times.
117 if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
134 break;
135 78 skip_bits(&gb, 1);
136 78 mode_count++;
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (mode_count > 64)
138 break;
139 78 gb0 = gb;
140
1/2
✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
78 if (get_bits(&gb0, 6) + 1 == mode_count) {
141 78 got_mode_header = 1;
142 78 last_mode_count = mode_count;
143 }
144 }
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 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 0 not taken.
✓ Branch 1 taken 39 times.
39 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 0 not taken.
✓ Branch 1 taken 39 times.
39 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 39 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 39 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 39 s->prev_mask = (s->mode_mask | 0x1) + 1;
174
175 39 init_get_bits(&gb, rev_buf, buf_size * 8);
176 39 skip_bits_long(&gb, got_framing_bit);
177
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 39 times.
117 for (i = mode_count - 1; i >= 0; i--) {
178 78 skip_bits_long(&gb, 40);
179 78 s->mode_blocksize[i] = get_bits1(&gb);
180 }
181
182 39 bad_header:
183 39 av_free(rev_buf);
184 39 return ret;
185 }
186
187 39 static int 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 39 s->class = &vorbis_parser_class;
195 39 s->extradata_parsed = 1;
196
197
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 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 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = parse_id_header(s, header_start[0], header_len[0])) < 0)
205 return ret;
206
207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
208 return ret;
209
210 39 s->valid_extradata = 1;
211 39 s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
212
213 39 return 0;
214 }
215
216 165369 int av_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf,
217 int buf_size, int *flags)
218 {
219 165369 int duration = 0;
220
221
3/4
✓ Branch 0 taken 165369 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 165360 times.
✓ Branch 3 taken 9 times.
165369 if (s->valid_extradata && buf_size > 0) {
222 int mode, current_blocksize;
223 165360 int previous_blocksize = s->previous_blocksize;
224
225
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 165353 times.
165360 if (buf[0] & 1) {
226 /* If the user doesn't care about special packets, it's a bad one. */
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!flags)
228 goto bad_packet;
229
230 /* Set the flag for which kind of special packet it is. */
231
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (buf[0] == 1)
232 2 *flags |= VORBIS_FLAG_HEADER;
233
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 else if (buf[0] == 3)
234 2 *flags |= VORBIS_FLAG_COMMENT;
235
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 else if (buf[0] == 5)
236 3 *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 7 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 0 not taken.
✓ Branch 1 taken 165353 times.
165353 if (s->mode_count == 1)
249 mode = 0;
250 else
251 165353 mode = (buf[0] & s->mode_mask) >> 1;
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165353 times.
165353 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 0 taken 91536 times.
✓ Branch 1 taken 73817 times.
165353 if(s->mode_blocksize[mode]){
257 91536 int flag = !!(buf[0] & s->prev_mask);
258 91536 previous_blocksize = s->blocksize[flag];
259 }
260 165353 current_blocksize = s->blocksize[s->mode_blocksize[mode]];
261 165353 duration = (previous_blocksize + current_blocksize) >> 2;
262 165353 s->previous_blocksize = current_blocksize;
263 }
264
265 165362 return duration;
266 }
267
268 580 int av_vorbis_parse_frame(AVVorbisParseContext *s, const uint8_t *buf,
269 int buf_size)
270 {
271 580 return av_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
272 }
273
274 20666 void av_vorbis_parse_reset(AVVorbisParseContext *s)
275 {
276
1/2
✓ Branch 0 taken 20666 times.
✗ Branch 1 not taken.
20666 if (s->valid_extradata)
277 20666 s->previous_blocksize = s->blocksize[0];
278 20666 }
279
280 70 void av_vorbis_parse_free(AVVorbisParseContext **s)
281 {
282 70 av_freep(s);
283 70 }
284
285 39 AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
286 int extradata_size)
287 {
288 39 AVVorbisParseContext *s = av_mallocz(sizeof(*s));
289 int ret;
290
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (!s)
292 return NULL;
293
294 39 ret = vorbis_parse_init(s, extradata, extradata_size);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (ret < 0) {
296 av_vorbis_parse_free(&s);
297 return NULL;
298 }
299
300 39 return s;
301 }
302
303 #if CONFIG_VORBIS_PARSER
304
305 typedef struct VorbisParseContext {
306 AVVorbisParseContext *vp;
307 } VorbisParseContext;
308
309 580 static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
310 const uint8_t **poutbuf, int *poutbuf_size,
311 const uint8_t *buf, int buf_size)
312 {
313 580 VorbisParseContext *s = s1->priv_data;
314 int duration;
315
316
4/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 569 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
580 if (!s->vp && avctx->extradata && avctx->extradata_size) {
317 11 s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
318 }
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 580 times.
580 if (!s->vp)
320 goto end;
321
322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 580 times.
580 if ((duration = av_vorbis_parse_frame(s->vp, buf, buf_size)) >= 0)
323 580 s1->duration = duration;
324
325 end:
326 /* always return the full packet. this parser isn't doing any splitting or
327 combining, only packet analysis */
328 580 *poutbuf = buf;
329 580 *poutbuf_size = buf_size;
330 580 return buf_size;
331 }
332
333 42 static av_cold void vorbis_parser_close(AVCodecParserContext *ctx)
334 {
335 42 VorbisParseContext *s = ctx->priv_data;
336 42 av_vorbis_parse_free(&s->vp);
337 42 }
338
339 const FFCodecParser ff_vorbis_parser = {
340 PARSER_CODEC_LIST(AV_CODEC_ID_VORBIS),
341 .priv_data_size = sizeof(VorbisParseContext),
342 .parse = vorbis_parse,
343 .close = vorbis_parser_close,
344 };
345 #endif /* CONFIG_VORBIS_PARSER */
346