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 #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 
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");
50  return AVERROR_INVALIDDATA;
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");
56  return AVERROR_INVALIDDATA;
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");
62  return AVERROR_INVALIDDATA;
63  }
64 
65  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  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");
87  return AVERROR_INVALIDDATA;
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");
93  return AVERROR_INVALIDDATA;
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");
99  return AVERROR_INVALIDDATA;
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 
182 bad_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 
244 bad_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 
281 {
282  av_freep(s);
283 }
284 
285 AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
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 = 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 
305 typedef struct VorbisParseContext {
307 } VorbisParseContext;
308 
309 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  VorbisParseContext *s = s1->priv_data;
314  int duration;
315 
316  if (!s->vp && avctx->extradata && avctx->extradata_size) {
317  s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
318  }
319  if (!s->vp)
320  goto end;
321 
322  if ((duration = av_vorbis_parse_frame(s->vp, buf, buf_size)) >= 0)
323  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  *poutbuf = buf;
329  *poutbuf_size = buf_size;
330  return buf_size;
331 }
332 
333 static av_cold void vorbis_parser_close(AVCodecParserContext *ctx)
334 {
335  VorbisParseContext *s = ctx->priv_data;
336  av_vorbis_parse_free(&s->vp);
337 }
338 
341  .priv_data_size = sizeof(VorbisParseContext),
342  .parse = vorbis_parse,
343  .close = vorbis_parser_close,
344 };
345 #endif /* CONFIG_VORBIS_PARSER */
flags
const SwsFlags flags[]
Definition: swscale.c:61
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:276
av_vorbis_parse_free
void av_vorbis_parse_free(AVVorbisParseContext **s)
Free the parser and everything associated with it.
Definition: vorbis_parser.c:280
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:689
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:216
AVCodecParserContext::duration
int duration
Duration of the current frame.
Definition: avcodec.h:2689
ff_vorbis_parser
const FFCodecParser ff_vorbis_parser
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:250
parser_internal.h
mode
Definition: swscale.c:56
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
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:268
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:512
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
xiph.h
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:379
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:333
GetBitContext
Definition: get_bits.h:109
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:106
duration
int64_t duration
Definition: movenc.c:65
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:515
s
#define s(width, name)
Definition: cbs_vp9.c:198
VORBIS_FLAG_COMMENT
#define VORBIS_FLAG_COMMENT
Definition: vorbis_parser.h:45
ctx
AVFormatContext * ctx
Definition: movenc.c:49
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:76
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:44
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:241
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:386
parse
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:46
vorbis_parser_internal.h
vorbis_parser_class
static const AVClass vorbis_parser_class
Definition: vorbis_parser.c:38
FFCodecParser
Definition: parser_internal.h:29
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:514
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:256
PARSER_CODEC_LIST
#define PARSER_CODEC_LIST(...)
Definition: parser_internal.h:76
vorbis_parse_init
static int vorbis_parse_init(AVVorbisParseContext *s, const uint8_t *extradata, int extradata_size)
Definition: vorbis_parser.c:187
AVCodecParserContext
Definition: avcodec.h:2575
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:81
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:285
flag
#define flag(name)
Definition: cbs_av1.c:496
AVCodecContext
main external API structure.
Definition: avcodec.h:431
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
mem.h
av_vorbis_parse_reset
void av_vorbis_parse_reset(AVVorbisParseContext *s)
Definition: vorbis_parser.c:274
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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
AVCodecParserContext::priv_data
void * priv_data
Definition: avcodec.h:2576
AV_CODEC_ID_VORBIS
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:464
parse_setup_header
static int parse_setup_header(AVVorbisParseContext *s, const uint8_t *buf, int buf_size)
Definition: vorbis_parser.c:76
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1292
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26