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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "avcodec.h"
24 
25 /* parser definition */
26 typedef struct DVDSubParseContext {
27  uint8_t *packet;
31 
33 {
34  return 0;
35 }
36 
38  AVCodecContext *avctx,
39  const uint8_t **poutbuf, int *poutbuf_size,
40  const uint8_t *buf, int buf_size)
41 {
43 
44  if (pc->packet_index == 0) {
45  if (buf_size < 2)
46  return 0;
47  pc->packet_len = AV_RB16(buf);
48  if (pc->packet_len == 0) /* HD-DVD subpicture packet */
49  pc->packet_len = AV_RB32(buf+2);
50  av_freep(&pc->packet);
51  pc->packet = av_malloc(pc->packet_len);
52  }
53  if (pc->packet) {
54  if (pc->packet_index + buf_size <= pc->packet_len) {
55  memcpy(pc->packet + pc->packet_index, buf, buf_size);
56  pc->packet_index += buf_size;
57  if (pc->packet_index >= pc->packet_len) {
58  *poutbuf = pc->packet;
59  *poutbuf_size = pc->packet_len;
60  pc->packet_index = 0;
61  return buf_size;
62  }
63  } else {
64  /* erroneous size */
65  pc->packet_index = 0;
66  }
67  }
68  *poutbuf = NULL;
69  *poutbuf_size = 0;
70  return buf_size;
71 }
72 
74 {
76  av_freep(&pc->packet);
77 }
78 
81  .priv_data_size = sizeof(DVDSubParseContext),
82  .parser_init = dvdsub_parse_init,
83  .parser_parse = dvdsub_parse,
84  .parser_close = dvdsub_parse_close,
85 };
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_WB24 AV_RB16
Definition: bytestream.h:89
AV_WL32 AV_WL24 AV_WL16 AV_RB32
Definition: bytestream.h:89
int codec_ids[5]
Definition: avcodec.h:4542
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:37
static av_cold int dvdsub_parse_init(AVCodecParserContext *s)
Definition: dvdsub_parser.c:32
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
#define av_cold
Definition: attributes.h:71
struct DVDSubParseContext DVDSubParseContext
AVCodecParser ff_dvdsub_parser
Definition: dvdsub_parser.c:79
NULL
Definition: eval.c:50
external API header
main external API structure.
Definition: avcodec.h:1329
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
static av_cold void dvdsub_parse_close(AVCodecParserContext *s)
Definition: dvdsub_parser.c:73