FFmpeg
Loading...
Searching...
No Matches
vorbis_parser.c
Go to the documentation of this file.
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"
37
39 .class_name = "Vorbis parser",
40 .item_name = av_default_item_name,
41 .version = LIBAVUTIL_VERSION_INT,
42};
43
45 const uint8_t *buf, int buf_size)
46{
47 /* Id header should be 30 bytes */
48 if (buf_size < 30) {
49 av_log(s, AV_LOG_ERROR, "Id header is too short\n");
51 }
52
53 /* make sure this is the Id header */
54 if (buf[0] != 1) {
55 av_log(s, AV_LOG_ERROR, "Wrong packet type in Id header\n");
57 }
58
59 /* check for header signature */
60 if (memcmp(&buf[1], "vorbis", 6)) {
61 av_log(s, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
63 }
64
65 if (!(buf[29] & 0x1)) {
66 av_log(s, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
68 }
69
70 s->blocksize[0] = 1 << (buf[28] & 0xF);
71 s->blocksize[1] = 1 << (buf[28] >> 4);
72
73 return 0;
74}
75
77 const uint8_t *buf, int buf_size)
78{
79 GetBitContext gb, gb0;
80 uint8_t *rev_buf;
81 int i, ret = 0;
82 int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
83
84 /* avoid overread */
85 if (buf_size < 7) {
86 av_log(s, AV_LOG_ERROR, "Setup header is too short\n");
88 }
89
90 /* make sure this is the Setup header */
91 if (buf[0] != 5) {
92 av_log(s, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
94 }
95
96 /* check for header signature */
97 if (memcmp(&buf[1], "vorbis", 6)) {
98 av_log(s, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
100 }
101
102 /* reverse bytes so we can easily read backwards with get_bits() */
103 if (!(rev_buf = av_malloc(buf_size))) {
104 av_log(s, AV_LOG_ERROR, "Out of memory\n");
105 return AVERROR(ENOMEM);
106 }
107 for (i = 0; i < buf_size; i++)
108 rev_buf[i] = buf[buf_size - 1 - i];
109 init_get_bits(&gb, rev_buf, buf_size * 8);
110
111 got_framing_bit = 0;
112 while (get_bits_left(&gb) > 97) {
113 if (get_bits1(&gb)) {
114 got_framing_bit = get_bits_count(&gb);
115 break;
116 }
117 }
118 if (!got_framing_bit) {
119 av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
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 mode_count = 0;
131 got_mode_header = 0;
132 while (get_bits_left(&gb) >= 97) {
133 if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
134 break;
135 skip_bits(&gb, 1);
136 mode_count++;
137 if (mode_count > 64)
138 break;
139 gb0 = gb;
140 if (get_bits(&gb0, 6) + 1 == mode_count) {
141 got_mode_header = 1;
142 last_mode_count = mode_count;
143 }
144 }
145 if (!got_mode_header) {
146 av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
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 if (last_mode_count > 2) {
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 if (last_mode_count > 63) {
163 av_log(s, AV_LOG_ERROR, "Unsupported mode count: %d\n",
164 last_mode_count);
166 goto bad_header;
167 }
168 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 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 s->prev_mask = (s->mode_mask | 0x1) + 1;
174
175 init_get_bits(&gb, rev_buf, buf_size * 8);
176 skip_bits_long(&gb, got_framing_bit);
177 for (i = mode_count - 1; i >= 0; i--) {
178 skip_bits_long(&gb, 40);
179 s->mode_blocksize[i] = get_bits1(&gb);
180 }
181
182bad_header:
183 av_free(rev_buf);
184 return ret;
185}
186
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 s->class = &vorbis_parser_class;
195 s->extradata_parsed = 1;
196
197 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 if ((ret = parse_id_header(s, header_start[0], header_len[0])) < 0)
205 return ret;
206
207 if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
208 return ret;
209
210 s->valid_extradata = 1;
211 s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
212
213 return 0;
214}
215
217 int buf_size, int *flags)
218{
219 int duration = 0;
220
221 if (s->valid_extradata && buf_size > 0) {
222 int mode, current_blocksize;
223 int previous_blocksize = s->previous_blocksize;
224
225 if (buf[0] & 1) {
226 /* If the user doesn't care about special packets, it's a bad one. */
227 if (!flags)
228 goto bad_packet;
229
230 /* Set the flag for which kind of special packet it is. */
231 if (buf[0] == 1)
233 else if (buf[0] == 3)
235 else if (buf[0] == 5)
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 return 0;
243
244bad_packet:
245 av_log(s, AV_LOG_ERROR, "Invalid packet\n");
246 return AVERROR_INVALIDDATA;
247 }
248 if (s->mode_count == 1)
249 mode = 0;
250 else
251 mode = (buf[0] & s->mode_mask) >> 1;
252 if (mode >= s->mode_count) {
253 av_log(s, AV_LOG_ERROR, "Invalid mode in packet\n");
254 return AVERROR_INVALIDDATA;
255 }
256 if(s->mode_blocksize[mode]){
257 int flag = !!(buf[0] & s->prev_mask);
258 previous_blocksize = s->blocksize[flag];
259 }
260 current_blocksize = s->blocksize[s->mode_blocksize[mode]];
261 duration = (previous_blocksize + current_blocksize) >> 2;
262 s->previous_blocksize = current_blocksize;
263 }
264
265 return duration;
266}
267
269 int buf_size)
270{
271 return av_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
272}
273
275{
276 if (s->valid_extradata)
277 s->previous_blocksize = s->blocksize[0];
278}
279
284
286 int extradata_size)
287{
288 AVVorbisParseContext *s = av_mallocz(sizeof(*s));
289 int ret;
290
291 if (!s)
292 return NULL;
293
294 ret = ff_vorbis_parse_init(s, extradata, extradata_size);
295 if (ret < 0) {
297 return NULL;
298 }
299
300 return s;
301}
302
303#if CONFIG_VORBIS_PARSER
304
305static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
306 const uint8_t **poutbuf, int *poutbuf_size,
307 const uint8_t *buf, int buf_size)
308{
310 int duration;
311
312 if (!s->valid_extradata && avctx->extradata && avctx->extradata_size) {
314 }
315 if (!s->valid_extradata)
316 goto end;
317
318 if ((duration = av_vorbis_parse_frame(s, buf, buf_size)) >= 0)
319 s1->duration = duration;
320
321end:
322 /* always return the full packet. this parser isn't doing any splitting or
323 combining, only packet analysis */
324 *poutbuf = buf;
325 *poutbuf_size = buf_size;
326 return buf_size;
327}
328
331 .priv_data_size = sizeof(AVVorbisParseContext),
332 .parse = vorbis_parse,
333};
334#endif /* CONFIG_VORBIS_PARSER */
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
#define flag(name)
Definition cbs_h264.c:60
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static int64_t duration
Definition ffplay.c:330
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:458
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_log2
Definition intmath.h:84
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
#define PARSER_CODEC_LIST(...)
const FFCodecParser ff_vorbis_parser
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int duration
Duration of the current frame.
Definition avcodec.h:2731
Definition swscale.c:71
#define av_free(p)
#define av_mallocz(s)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
void av_vorbis_parse_free(AVVorbisParseContext **s)
Free the parser and everything associated with it.
int av_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf, int buf_size, int *flags)
Get the duration for a Vorbis packet.
static const AVClass vorbis_parser_class
int av_vorbis_parse_frame(AVVorbisParseContext *s, const uint8_t *buf, int buf_size)
Get the duration for a Vorbis packet.
int ff_vorbis_parse_init(AVVorbisParseContext *s, const uint8_t *extradata, int extradata_size)
static int parse_id_header(AVVorbisParseContext *s, const uint8_t *buf, int buf_size)
AVVorbisParseContext * av_vorbis_parse_init(const uint8_t *extradata, int extradata_size)
Allocate and initialize the Vorbis parser using headers in the extradata.
void av_vorbis_parse_reset(AVVorbisParseContext *s)
static int parse_setup_header(AVVorbisParseContext *s, const uint8_t *buf, int buf_size)
#define VORBIS_FLAG_SETUP
#define VORBIS_FLAG_COMMENT
#define VORBIS_FLAG_HEADER
Vorbis audio parser.
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use.
Definition xiph.c:26