2 * Copyright (c) 2012 Justin Ruggles
4 * This file is part of Libav.
6 * Libav 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.
11 * Libav 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Determines the duration for each packet.
31 #include "vorbis_parser.h"
33 static int parse_id_header(AVCodecContext *avctx, VorbisParseContext *s,
34 const uint8_t *buf, int buf_size)
36 /* Id header should be 30 bytes */
38 av_log(avctx, AV_LOG_ERROR, "Id header is too short\n");
39 return AVERROR_INVALIDDATA;
42 /* make sure this is the Id header */
44 av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Id header\n");
45 return AVERROR_INVALIDDATA;
48 /* check for header signature */
49 if (memcmp(&buf[1], "vorbis", 6)) {
50 av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
51 return AVERROR_INVALIDDATA;
54 if (!(buf[29] & 0x1)) {
55 av_log(avctx, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
56 return AVERROR_INVALIDDATA;
59 s->blocksize[0] = 1 << (buf[28] & 0xF);
60 s->blocksize[1] = 1 << (buf[28] >> 4);
65 static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
66 const uint8_t *buf, int buf_size)
68 GetBitContext gb, gb0;
71 int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
75 av_log(avctx, AV_LOG_ERROR, "Setup header is too short\n");
76 return AVERROR_INVALIDDATA;
79 /* make sure this is the Setup header */
81 av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
82 return AVERROR_INVALIDDATA;
85 /* check for header signature */
86 if (memcmp(&buf[1], "vorbis", 6)) {
87 av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
88 return AVERROR_INVALIDDATA;
91 /* reverse bytes so we can easily read backwards with get_bits() */
92 if (!(rev_buf = av_malloc(buf_size))) {
93 av_log(avctx, AV_LOG_ERROR, "Out of memory\n");
94 return AVERROR(ENOMEM);
96 for (i = 0; i < buf_size; i++)
97 rev_buf[i] = buf[buf_size - 1 - i];
98 init_get_bits(&gb, rev_buf, buf_size * 8);
101 while (get_bits_left(&gb) > 97) {
102 if (get_bits1(&gb)) {
103 got_framing_bit = get_bits_count(&gb);
107 if (!got_framing_bit) {
108 av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
109 ret = AVERROR_INVALIDDATA;
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
121 while (get_bits_left(&gb) >= 97) {
122 if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
129 if (get_bits(&gb0, 6) + 1 == mode_count) {
131 last_mode_count = mode_count;
134 if (!got_mode_header) {
135 av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
136 ret = AVERROR_INVALIDDATA;
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) {
144 av_log_ask_for_sample(avctx, "%d modes found. This is either a false "
145 "positive or a sample from an unknown encoder.\n",
148 /* We're limiting the mode count to 63 so that we know that the previous
149 * block flag will be in the first packet byte. */
150 if (last_mode_count > 63) {
151 av_log(avctx, AV_LOG_ERROR, "Unsupported mode count: %d\n",
153 ret = AVERROR_INVALIDDATA;
156 s->mode_count = mode_count = last_mode_count;
157 /* Determine the number of bits required to code the mode and turn that
158 * into a bitmask to directly access the mode from the first frame byte. */
159 s->mode_mask = ((1 << (av_log2(mode_count - 1) + 1)) - 1) << 1;
160 /* The previous window flag is the next bit after the mode */
161 s->prev_mask = (s->mode_mask | 0x1) + 1;
163 init_get_bits(&gb, rev_buf, buf_size * 8);
164 skip_bits_long(&gb, got_framing_bit);
165 for (i = mode_count - 1; i >= 0; i--) {
166 skip_bits_long(&gb, 40);
167 s->mode_blocksize[i] = s->blocksize[get_bits1(&gb)];
175 int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s)
177 uint8_t *header_start[3];
182 s->extradata_parsed = 1;
184 if ((ret = avpriv_split_xiph_headers(avctx->extradata,
185 avctx->extradata_size, 30,
186 header_start, header_len)) < 0) {
187 av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n");
191 if ((ret = parse_id_header(avctx, s, header_start[0], header_len[0])) < 0)
194 if ((ret = parse_setup_header(avctx, s, header_start[2], header_len[2])) < 0)
197 s->valid_extradata = 1;
198 s->previous_blocksize = s->mode_blocksize[0];
203 int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
208 if (s->valid_extradata && buf_size > 0) {
209 int mode, current_blocksize;
210 int previous_blocksize = s->previous_blocksize;
213 av_log(s->avctx, AV_LOG_ERROR, "Invalid packet\n");
214 return AVERROR_INVALIDDATA;
216 if (s->mode_count == 1)
219 mode = (buf[0] & s->mode_mask) >> 1;
220 if (mode >= s->mode_count) {
221 av_log(s->avctx, AV_LOG_ERROR, "Invalid mode in packet\n");
222 return AVERROR_INVALIDDATA;
225 int flag = !!(buf[0] & s->prev_mask);
226 previous_blocksize = s->blocksize[flag];
228 current_blocksize = s->mode_blocksize[mode];
229 duration = (previous_blocksize + current_blocksize) >> 2;
230 s->previous_blocksize = current_blocksize;
236 void avpriv_vorbis_parse_reset(VorbisParseContext *s)
238 if (s->valid_extradata)
239 s->previous_blocksize = s->mode_blocksize[0];
242 #if CONFIG_VORBIS_PARSER
243 static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
244 const uint8_t **poutbuf, int *poutbuf_size,
245 const uint8_t *buf, int buf_size)
247 VorbisParseContext *s = s1->priv_data;
250 if (!s->extradata_parsed && avctx->extradata && avctx->extradata_size)
251 if (avpriv_vorbis_parse_extradata(avctx, s))
254 if ((duration = avpriv_vorbis_parse_frame(s, buf, buf_size)) >= 0)
255 s1->duration = duration;
258 /* always return the full packet. this parser isn't doing any splitting or
259 combining, only packet analysis */
261 *poutbuf_size = buf_size;
265 AVCodecParser ff_vorbis_parser = {
266 .codec_ids = { CODEC_ID_VORBIS },
267 .priv_data_size = sizeof(VorbisParseContext),
268 .parser_parse = vorbis_parse,
270 #endif /* CONFIG_VORBIS_PARSER */