FFmpeg
dvdsub_parser.c
Go to the documentation of this file.
1 /*
2  * DVD subtitle decoding
3  * Copyright (c) 2005 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <string.h>
23 
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
26 #include "avcodec.h"
27 
28 /* parser definition */
29 typedef struct DVDSubParseContext {
30  uint8_t *packet;
34 
36  AVCodecContext *avctx,
37  const uint8_t **poutbuf, int *poutbuf_size,
38  const uint8_t *buf, int buf_size)
39 {
40  DVDSubParseContext *pc = s->priv_data;
41 
42  *poutbuf = buf;
43  *poutbuf_size = buf_size;
44 
45  if (pc->packet_index == 0) {
46  if (buf_size < 2 || (AV_RB16(buf) == 0 && buf_size < 6)) {
47  if (buf_size)
48  av_log(avctx, AV_LOG_DEBUG, "Parser input %d too small\n", buf_size);
49  return buf_size;
50  }
51  pc->packet_len = AV_RB16(buf);
52  if (pc->packet_len == 0) /* HD-DVD subpicture packet */
53  pc->packet_len = AV_RB32(buf+2);
54  av_freep(&pc->packet);
55  if ((unsigned)pc->packet_len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
56  av_log(avctx, AV_LOG_ERROR, "packet length %d is invalid\n", pc->packet_len);
57  return buf_size;
58  }
60  }
61  if (pc->packet) {
62  if (pc->packet_index + buf_size <= pc->packet_len) {
63  memcpy(pc->packet + pc->packet_index, buf, buf_size);
64  pc->packet_index += buf_size;
65  if (pc->packet_index >= pc->packet_len) {
66  *poutbuf = pc->packet;
67  *poutbuf_size = pc->packet_len;
68  pc->packet_index = 0;
69  return buf_size;
70  }
71  } else {
72  /* erroneous size */
73  pc->packet_index = 0;
74  }
75  }
76  *poutbuf = NULL;
77  *poutbuf_size = 0;
78  return buf_size;
79 }
80 
82 {
83  DVDSubParseContext *pc = s->priv_data;
84  av_freep(&pc->packet);
85 }
86 
89  .priv_data_size = sizeof(DVDSubParseContext),
90  .parser_parse = dvdsub_parse,
91  .parser_close = dvdsub_parse_close,
92 };
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
dvdsub_parse
static int dvdsub_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: dvdsub_parser.c:35
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_DVD_SUBTITLE
@ AV_CODEC_ID_DVD_SUBTITLE
Definition: codec_id.h:544
DVDSubParseContext::packet_index
int packet_index
Definition: dvdsub_parser.c:32
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2972
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
DVDSubParseContext::packet_len
int packet_len
Definition: dvdsub_parser.c:31
dvdsub_parse_close
static av_cold void dvdsub_parse_close(AVCodecParserContext *s)
Definition: dvdsub_parser.c:81
ff_dvdsub_parser
const AVCodecParser ff_dvdsub_parser
Definition: dvdsub_parser.c:87
DVDSubParseContext
Definition: dvdsub_parser.c:29
avcodec.h
AVCodecParserContext
Definition: avcodec.h:2812
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
DVDSubParseContext::packet
uint8_t * packet
Definition: dvdsub_parser.c:30
AVCodecContext
main external API structure.
Definition: avcodec.h:426
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVCodecParser
Definition: avcodec.h:2971
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98