FFmpeg
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 
32 #include "get_bits.h"
33 #include "parser.h"
34 #include "xiph.h"
35 #include "vorbis_parser_internal.h"
36 
37 static const AVClass vorbis_parser_class = {
38  .class_name = "Vorbis parser",
39  .item_name = av_default_item_name,
40  .version = LIBAVUTIL_VERSION_INT,
41 };
42 
44  const uint8_t *buf, int buf_size)
45 {
46  /* Id header should be 30 bytes */
47  if (buf_size < 30) {
48  av_log(s, AV_LOG_ERROR, "Id header is too short\n");
49  return AVERROR_INVALIDDATA;
50  }
51 
52  /* make sure this is the Id header */
53  if (buf[0] != 1) {
54  av_log(s, AV_LOG_ERROR, "Wrong packet type in Id header\n");
55  return AVERROR_INVALIDDATA;
56  }
57 
58  /* check for header signature */
59  if (memcmp(&buf[1], "vorbis", 6)) {
60  av_log(s, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
61  return AVERROR_INVALIDDATA;
62  }
63 
64  if (!(buf[29] & 0x1)) {
65  av_log(s, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
66  return AVERROR_INVALIDDATA;
67  }
68 
69  s->blocksize[0] = 1 << (buf[28] & 0xF);
70  s->blocksize[1] = 1 << (buf[28] >> 4);
71 
72  return 0;
73 }
74 
76  const uint8_t *buf, int buf_size)
77 {
78  GetBitContext gb, gb0;
79  uint8_t *rev_buf;
80  int i, ret = 0;
81  int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
82 
83  /* avoid overread */
84  if (buf_size < 7) {
85  av_log(s, AV_LOG_ERROR, "Setup header is too short\n");
86  return AVERROR_INVALIDDATA;
87  }
88 
89  /* make sure this is the Setup header */
90  if (buf[0] != 5) {
91  av_log(s, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
92  return AVERROR_INVALIDDATA;
93  }
94 
95  /* check for header signature */
96  if (memcmp(&buf[1], "vorbis", 6)) {
97  av_log(s, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
98  return AVERROR_INVALIDDATA;
99  }
100 
101  /* reverse bytes so we can easily read backwards with get_bits() */
102  if (!(rev_buf = av_malloc(buf_size))) {
103  av_log(s, AV_LOG_ERROR, "Out of memory\n");
104  return AVERROR(ENOMEM);
105  }
106  for (i = 0; i < buf_size; i++)
107  rev_buf[i] = buf[buf_size - 1 - i];
108  init_get_bits(&gb, rev_buf, buf_size * 8);
109 
110  got_framing_bit = 0;
111  while (get_bits_left(&gb) > 97) {
112  if (get_bits1(&gb)) {
113  got_framing_bit = get_bits_count(&gb);
114  break;
115  }
116  }
117  if (!got_framing_bit) {
118  av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
120  goto bad_header;
121  }
122 
123  /* Now we search backwards to find possible valid mode counts. This is not
124  * fool-proof because we could have false positive matches and read too
125  * far, but there isn't really any way to be sure without parsing through
126  * all the many variable-sized fields before the modes. This approach seems
127  * to work well in testing, and it is similar to how it is handled in
128  * liboggz. */
129  mode_count = 0;
130  got_mode_header = 0;
131  while (get_bits_left(&gb) >= 97) {
132  if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
133  break;
134  skip_bits(&gb, 1);
135  mode_count++;
136  if (mode_count > 64)
137  break;
138  gb0 = gb;
139  if (get_bits(&gb0, 6) + 1 == mode_count) {
140  got_mode_header = 1;
141  last_mode_count = mode_count;
142  }
143  }
144  if (!got_mode_header) {
145  av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
147  goto bad_header;
148  }
149  /* All samples I've seen use <= 2 modes, so ask for a sample if we find
150  * more than that, as it is most likely a false positive. If we get any
151  * we may need to approach this the long way and parse the whole Setup
152  * header, but I hope very much that it never comes to that. */
153  if (last_mode_count > 2) {
155  "%d modes (either a false positive or a "
156  "sample from an unknown encoder)",
157  last_mode_count);
158  }
159  /* We're limiting the mode count to 63 so that we know that the previous
160  * block flag will be in the first packet byte. */
161  if (last_mode_count > 63) {
162  av_log(s, AV_LOG_ERROR, "Unsupported mode count: %d\n",
163  last_mode_count);
165  goto bad_header;
166  }
167  s->mode_count = mode_count = last_mode_count;
168  /* Determine the number of bits required to code the mode and turn that
169  * into a bitmask to directly access the mode from the first frame byte. */
170  s->mode_mask = ((1 << (av_log2(mode_count - 1) + 1)) - 1) << 1;
171  /* The previous window flag is the next bit after the mode */
172  s->prev_mask = (s->mode_mask | 0x1) + 1;
173 
174  init_get_bits(&gb, rev_buf, buf_size * 8);
175  skip_bits_long(&gb, got_framing_bit);
176  for (i = mode_count - 1; i >= 0; i--) {
177  skip_bits_long(&gb, 40);
178  s->mode_blocksize[i] = get_bits1(&gb);
179  }
180 
181 bad_header:
182  av_free(rev_buf);
183  return ret;
184 }
185 
187  const uint8_t *extradata, int extradata_size)
188 {
189  const uint8_t *header_start[3];
190  int header_len[3];
191  int ret;
192 
193  s->class = &vorbis_parser_class;
194  s->extradata_parsed = 1;
195 
196  if ((ret = avpriv_split_xiph_headers(extradata,
197  extradata_size, 30,
198  header_start, header_len)) < 0) {
199  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
200  return ret;
201  }
202 
203  if ((ret = parse_id_header(s, header_start[0], header_len[0])) < 0)
204  return ret;
205 
206  if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
207  return ret;
208 
209  s->valid_extradata = 1;
210  s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
211 
212  return 0;
213 }
214 
216  int buf_size, int *flags)
217 {
218  int duration = 0;
219 
220  if (s->valid_extradata && buf_size > 0) {
221  int mode, current_blocksize;
222  int previous_blocksize = s->previous_blocksize;
223 
224  if (buf[0] & 1) {
225  /* If the user doesn't care about special packets, it's a bad one. */
226  if (!flags)
227  goto bad_packet;
228 
229  /* Set the flag for which kind of special packet it is. */
230  if (buf[0] == 1)
232  else if (buf[0] == 3)
234  else if (buf[0] == 5)
236  else
237  goto bad_packet;
238 
239  /* Special packets have no duration. */
240  return 0;
241 
242 bad_packet:
243  av_log(s, AV_LOG_ERROR, "Invalid packet\n");
244  return AVERROR_INVALIDDATA;
245  }
246  if (s->mode_count == 1)
247  mode = 0;
248  else
249  mode = (buf[0] & s->mode_mask) >> 1;
250  if (mode >= s->mode_count) {
251  av_log(s, AV_LOG_ERROR, "Invalid mode in packet\n");
252  return AVERROR_INVALIDDATA;
253  }
254  if(s->mode_blocksize[mode]){
255  int flag = !!(buf[0] & s->prev_mask);
256  previous_blocksize = s->blocksize[flag];
257  }
258  current_blocksize = s->blocksize[s->mode_blocksize[mode]];
259  duration = (previous_blocksize + current_blocksize) >> 2;
260  s->previous_blocksize = current_blocksize;
261  }
262 
263  return duration;
264 }
265 
267  int buf_size)
268 {
269  return av_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
270 }
271 
273 {
274  if (s->valid_extradata)
275  s->previous_blocksize = s->blocksize[0];
276 }
277 
279 {
280  av_freep(s);
281 }
282 
283 AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
284  int extradata_size)
285 {
286  AVVorbisParseContext *s = av_mallocz(sizeof(*s));
287  int ret;
288 
289  if (!s)
290  return NULL;
291 
292  ret = vorbis_parse_init(s, extradata, extradata_size);
293  if (ret < 0) {
295  return NULL;
296  }
297 
298  return s;
299 }
300 
301 #if CONFIG_VORBIS_PARSER
302 
303 typedef struct VorbisParseContext {
305 } VorbisParseContext;
306 
307 static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
308  const uint8_t **poutbuf, int *poutbuf_size,
309  const uint8_t *buf, int buf_size)
310 {
311  VorbisParseContext *s = s1->priv_data;
312  int duration;
313 
314  if (!s->vp && avctx->extradata && avctx->extradata_size) {
315  s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
316  }
317  if (!s->vp)
318  goto end;
319 
320  if ((duration = av_vorbis_parse_frame(s->vp, buf, buf_size)) >= 0)
321  s1->duration = duration;
322 
323 end:
324  /* always return the full packet. this parser isn't doing any splitting or
325  combining, only packet analysis */
326  *poutbuf = buf;
327  *poutbuf_size = buf_size;
328  return buf_size;
329 }
330 
331 static void vorbis_parser_close(AVCodecParserContext *ctx)
332 {
333  VorbisParseContext *s = ctx->priv_data;
334  av_vorbis_parse_free(&s->vp);
335 }
336 
339  .priv_data_size = sizeof(VorbisParseContext),
340  .parser_parse = vorbis_parse,
341  .parser_close = vorbis_parser_close,
342 };
343 #endif /* CONFIG_VORBIS_PARSER */
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
av_vorbis_parse_free
void av_vorbis_parse_free(AVVorbisParseContext **s)
Free the parser and everything associated with it.
Definition: vorbis_parser.c:278
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVVorbisParseContext
Definition: vorbis_parser_internal.h:34
av_vorbis_parse_frame_flags
int av_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf, int buf_size, int *flags)
Get the duration for a Vorbis packet.
Definition: vorbis_parser.c:215
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
av_vorbis_parse_frame
int av_vorbis_parse_frame(AVVorbisParseContext *s, const uint8_t *buf, int buf_size)
Get the duration for a Vorbis packet.
Definition: vorbis_parser.c:266
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:649
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
xiph.h
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
GetBitContext
Definition: get_bits.h:61
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
duration
int64_t duration
Definition: movenc.c:64
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:500
s
#define s(width, name)
Definition: cbs_vp9.c:256
s1
#define s1
Definition: regdef.h:38
VORBIS_FLAG_COMMENT
#define VORBIS_FLAG_COMMENT
Definition: vorbis_parser.h:45
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
parse_id_header
static int parse_id_header(AVVorbisParseContext *s, const uint8_t *buf, int buf_size)
Definition: vorbis_parser.c:43
VORBIS_FLAG_HEADER
#define VORBIS_FLAG_HEADER
Definition: vorbis_parser.h:44
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2960
vorbis_parser_internal.h
vorbis_parser_class
static const AVClass vorbis_parser_class
Definition: vorbis_parser.c:37
flag
#define flag(name)
Definition: cbs_av1.c:553
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:499
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:264
parser.h
vorbis_parse_init
static int vorbis_parse_init(AVVorbisParseContext *s, const uint8_t *extradata, int extradata_size)
Definition: vorbis_parser.c:186
AVCodecParserContext
Definition: avcodec.h:2800
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
av_vorbis_parse_init
AVVorbisParseContext * av_vorbis_parse_init(const uint8_t *extradata, int extradata_size)
Allocate and initialize the Vorbis parser using headers in the extradata.
Definition: vorbis_parser.c:283
AVCodecContext
main external API structure.
Definition: avcodec.h:398
VORBIS_FLAG_SETUP
#define VORBIS_FLAG_SETUP
Definition: vorbis_parser.h:46
mode
mode
Definition: ebur128.h:83
avpriv_split_xiph_headers
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
ff_vorbis_parser
const AVCodecParser ff_vorbis_parser
av_vorbis_parse_reset
void av_vorbis_parse_reset(AVVorbisParseContext *s)
Definition: vorbis_parser.c:272
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVCodecParser
Definition: avcodec.h:2959
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_VORBIS
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:440
parse_setup_header
static int parse_setup_header(AVVorbisParseContext *s, const uint8_t *buf, int buf_size)
Definition: vorbis_parser.c:75
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1244
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26