]> git.sesse.net Git - ffmpeg/blob - libavcodec/png_parser.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / png_parser.c
1 /*
2  * PNG parser
3  * Copyright (c) 2009 Peter Holik
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 /**
23  * @file
24  * PNG parser
25  */
26
27 #include "parser.h"
28
29 #define PNGSIG 0x89504e470d0a1a0a
30 #define MNGSIG 0x8a4d4e470d0a1a0a
31
32 typedef struct PNGParseContext
33 {
34     ParseContext pc;
35     uint32_t index;
36     uint32_t chunk_length;
37     uint32_t remaining_size;
38 } PNGParseContext;
39
40 static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx,
41                      const uint8_t **poutbuf, int *poutbuf_size,
42                      const uint8_t *buf, int buf_size)
43 {
44     PNGParseContext *ppc = s->priv_data;
45     int next = END_NOT_FOUND;
46     int i = 0;
47
48     s->pict_type = AV_PICTURE_TYPE_NONE;
49
50     *poutbuf_size = 0;
51     if (buf_size == 0)
52         return 0;
53
54     if (!ppc->pc.frame_start_found) {
55         uint64_t state64 = ppc->pc.state64;
56         for (; i < buf_size; i++) {
57             state64 = (state64 << 8) | buf[i];
58             if (state64 == PNGSIG || state64 == MNGSIG) {
59                 i++;
60                 ppc->pc.frame_start_found = 1;
61                 break;
62             }
63         }
64         ppc->pc.state64 = state64;
65     } else
66         if (ppc->remaining_size) {
67             i = FFMIN(ppc->remaining_size, buf_size);
68             ppc->remaining_size -= i;
69             if (ppc->remaining_size)
70                 goto flush;
71             if (ppc->index == -1) {
72                 next = i;
73                 goto flush;
74             }
75         }
76
77     for (;ppc->pc.frame_start_found && i < buf_size; i++) {
78         ppc->pc.state = (ppc->pc.state<<8) | buf[i];
79         if (ppc->index == 3) {
80             ppc->chunk_length = ppc->pc.state;
81             if (ppc->chunk_length > 0x7fffffff) {
82                 ppc->index = ppc->pc.frame_start_found = 0;
83                 goto flush;
84             }
85             ppc->chunk_length += 4;
86         } else if (ppc->index == 7) {
87             if (ppc->chunk_length >= buf_size - i)
88                     ppc->remaining_size = ppc->chunk_length - buf_size + i + 1;
89             if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) {
90                 if (ppc->remaining_size)
91                     ppc->index = -1;
92                 else
93                     next = ppc->chunk_length + i + 1;
94                 break;
95             } else {
96                 ppc->index = 0;
97                 if (ppc->remaining_size)
98                     break;
99                 else
100                     i += ppc->chunk_length;
101                 continue;
102             }
103         }
104         ppc->index++;
105     }
106 flush:
107     if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
108         return buf_size;
109
110     ppc->index = ppc->pc.frame_start_found = 0;
111
112     *poutbuf      = buf;
113     *poutbuf_size = buf_size;
114     return next;
115 }
116
117 AVCodecParser ff_png_parser = {
118     { CODEC_ID_PNG },
119     sizeof(PNGParseContext),
120     NULL,
121     png_parse,
122     ff_parse_close,
123 };