]> git.sesse.net Git - ffmpeg/blob - libavcodec/dirac_parser.c
Check *data_size in decode_frame()
[ffmpeg] / libavcodec / dirac_parser.c
1 /*
2  * Dirac parser
3  *
4  * Copyright (c) 2007 Marco Gerards <marco@gnu.org>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file dirac_parser.c
25  * Dirac Parser
26  * @author Marco Gerards <marco@gnu.org>
27  */
28
29 #include "parser.h"
30
31 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
32
33 /**
34  * Finds the end of the current frame in the bitstream.
35  * @return the position of the first byte of the next frame or -1
36  */
37 static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
38 {
39     uint32_t state = pc->state;
40     int i;
41
42     for (i = 0; i < buf_size; i++) {
43         state = (state << 8) | buf[i];
44         if (state == DIRAC_PARSE_INFO_PREFIX) {
45             pc->frame_start_found ^= 1;
46             if (!pc->frame_start_found) {
47                 pc->state = -1;
48                 return i - 3;
49             }
50         }
51     }
52
53     pc->state = state;
54
55     return END_NOT_FOUND;
56 }
57
58 static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
59                        const uint8_t **poutbuf, int *poutbuf_size,
60                        const uint8_t *buf, int buf_size)
61 {
62     ParseContext *pc = s->priv_data;
63     int next;
64
65     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
66         next = buf_size;
67     }else{
68         next = find_frame_end(pc, buf, buf_size);
69
70         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
71             *poutbuf = NULL;
72             *poutbuf_size = 0;
73             return buf_size;
74         }
75     }
76
77     *poutbuf = buf;
78     *poutbuf_size = buf_size;
79     return next;
80 }
81
82 AVCodecParser dirac_parser = {
83     { CODEC_ID_DIRAC },
84     sizeof(ParseContext),
85     NULL,
86     dirac_parse,
87     ff_parse_close,
88 };