FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vorbis_parser.c
Date: 2026-08-19 01:31:13
Exec Total Coverage
Lines: 106 151 70.2%
Functions: 9 9 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 49 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 49 times.
49 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 49 times.
49 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 49 times.
49 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 49 times.
49 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 49 s->blocksize[0] = 1 << (buf[28] & 0xF);
71 49 s->blocksize[1] = 1 << (buf[28] >> 4);
72
73 49 return 0;
74 }
75
76 49 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 49 int i, ret = 0;
82 49 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 49 times.
49 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 49 times.
49 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 49 times.
49 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 49 times.
49 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 233260 times.
✓ Branch 1 taken 49 times.
233309 for (i = 0; i < buf_size; i++)
108 233260 rev_buf[i] = buf[buf_size - 1 - i];
109 49 init_get_bits(&gb, rev_buf, buf_size * 8);
110
111 49 got_framing_bit = 0;
112
1/2
✓ Branch 1 taken 253 times.
✗ Branch 2 not taken.
253 while (get_bits_left(&gb) > 97) {
113
2/2
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 204 times.
253 if (get_bits1(&gb)) {
114 49 got_framing_bit = get_bits_count(&gb);
115 49 break;
116 }
117 }
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 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 49 mode_count = 0;
131 49 got_mode_header = 0;
132
1/2
✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
147 while (get_bits_left(&gb) >= 97) {
133
5/6
✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 99 times.
✓ Branch 5 taken 48 times.
✓ Branch 7 taken 98 times.
✓ Branch 8 taken 1 times.
147 if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
134 break;
135 98 skip_bits(&gb, 1);
136 98 mode_count++;
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (mode_count > 64)
138 break;
139 98 gb0 = gb;
140
1/2
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
98 if (get_bits(&gb0, 6) + 1 == mode_count) {
141 98 got_mode_header = 1;
142 98 last_mode_count = mode_count;
143 }
144 }
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 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 49 times.
49 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 49 times.
49 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 49 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 49 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 49 s->prev_mask = (s->mode_mask | 0x1) + 1;
174
175 49 init_get_bits(&gb, rev_buf, buf_size * 8);
176 49 skip_bits_long(&gb, got_framing_bit);
177
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 49 times.
147 for (i = mode_count - 1; i >= 0; i--) {
178 98 skip_bits_long(&gb, 40);
179 98 s->mode_blocksize[i] = get_bits1(&gb);
180 }
181
182 49 bad_header:
183 49 av_free(rev_buf);
184 49 return ret;
185 }
186
187 49 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 49 s->class = &vorbis_parser_class;
195 49 s->extradata_parsed = 1;
196
197
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 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 49 times.
49 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 49 times.
49 if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
208 return ret;
209
210 49 s->valid_extradata = 1;
211 49 s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
212
213 49 return 0;
214 }
215
216 12303 int av_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf,
217 int buf_size, int *flags)
218 {
219 12303 int duration = 0;
220
221
3/4
✓ Branch 0 taken 12303 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12294 times.
✓ Branch 3 taken 9 times.
12303 if (s->valid_extradata && buf_size > 0) {
222 int mode, current_blocksize;
223 12294 int previous_blocksize = s->previous_blocksize;
224
225
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12282 times.
12294 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 12 times.
12 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 4 times.
✓ Branch 1 taken 8 times.
12 if (buf[0] == 1)
232 4 *flags |= VORBIS_FLAG_HEADER;
233
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 else if (buf[0] == 3)
234 4 *flags |= VORBIS_FLAG_COMMENT;
235
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 else if (buf[0] == 5)
236 4 *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 12 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 12282 times.
12282 if (s->mode_count == 1)
249 mode = 0;
250 else
251 12282 mode = (buf[0] & s->mode_mask) >> 1;
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12282 times.
12282 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 7684 times.
✓ Branch 1 taken 4598 times.
12282 if(s->mode_blocksize[mode]){
257 7684 int flag = !!(buf[0] & s->prev_mask);
258 7684 previous_blocksize = s->blocksize[flag];
259 }
260 12282 current_blocksize = s->blocksize[s->mode_blocksize[mode]];
261 12282 duration = (previous_blocksize + current_blocksize) >> 2;
262 12282 s->previous_blocksize = current_blocksize;
263 }
264
265 12291 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 72 void av_vorbis_parse_reset(AVVorbisParseContext *s)
275 {
276
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (s->valid_extradata)
277 72 s->previous_blocksize = s->blocksize[0];
278 72 }
279
280 38 void av_vorbis_parse_free(AVVorbisParseContext **s)
281 {
282 38 av_freep(s);
283 38 }
284
285 38 AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
286 int extradata_size)
287 {
288 38 AVVorbisParseContext *s = av_mallocz(sizeof(*s));
289 int ret;
290
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (!s)
292 return NULL;
293
294 38 ret = ff_vorbis_parse_init(s, extradata, extradata_size);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (ret < 0) {
296 av_vorbis_parse_free(&s);
297 return NULL;
298 }
299
300 38 return s;
301 }
302
303 #if CONFIG_VORBIS_PARSER
304
305 580 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 580 AVVorbisParseContext *s = s1->priv_data;
310 int duration;
311
312
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->valid_extradata && avctx->extradata && avctx->extradata_size) {
313 11 ff_vorbis_parse_init(s, avctx->extradata, avctx->extradata_size);
314 }
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 580 times.
580 if (!s->valid_extradata)
316 goto end;
317
318
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 580 times.
580 if ((duration = av_vorbis_parse_frame(s, buf, buf_size)) >= 0)
319 580 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 580 *poutbuf = buf;
325 580 *poutbuf_size = buf_size;
326 580 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