FFmpeg
png_parser.c
Go to the documentation of this file.
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 #include "png.h"
29 
30 typedef struct PNGParseContext {
32  uint32_t chunk_pos; ///< position inside current chunk
33  uint32_t chunk_length; ///< length of the current chunk
34  uint32_t remaining_size; ///< remaining size of the current chunk
36 
38  const uint8_t **poutbuf, int *poutbuf_size,
39  const uint8_t *buf, int buf_size)
40 {
41  PNGParseContext *ppc = s->priv_data;
42  int next = END_NOT_FOUND;
43  int i = 0;
44 
45  s->pict_type = AV_PICTURE_TYPE_NONE;
46 
47  *poutbuf_size = 0;
48  *poutbuf = NULL;
49 
50  if (!ppc->pc.frame_start_found) {
51  uint64_t state64 = ppc->pc.state64;
52  for (; i < buf_size; i++) {
53  state64 = (state64 << 8) | buf[i];
54  if (state64 == PNGSIG || state64 == MNGSIG) {
55  i++;
56  ppc->pc.frame_start_found = 1;
57  break;
58  }
59  }
60  ppc->pc.state64 = state64;
61  } else if (ppc->remaining_size) {
62  i = FFMIN(ppc->remaining_size, buf_size);
63  ppc->remaining_size -= i;
64  if (ppc->remaining_size)
65  goto flush;
66  if (ppc->chunk_pos == -1) {
67  next = i;
68  goto flush;
69  }
70  }
71 
72  for (; ppc->pc.frame_start_found && i < buf_size; i++) {
73  ppc->pc.state = (ppc->pc.state << 8) | buf[i];
74  if (ppc->chunk_pos == 3) {
75  ppc->chunk_length = ppc->pc.state;
76  if (ppc->chunk_length > 0x7fffffff) {
77  ppc->chunk_pos = ppc->pc.frame_start_found = 0;
78  goto flush;
79  }
80  ppc->chunk_length += 4;
81  } else if (ppc->chunk_pos == 7) {
82  if (ppc->chunk_length >= buf_size - i)
83  ppc->remaining_size = ppc->chunk_length - buf_size + i + 1;
84  if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) {
85  if (ppc->remaining_size)
86  ppc->chunk_pos = -1;
87  else
88  next = ppc->chunk_length + i + 1;
89  break;
90  } else {
91  ppc->chunk_pos = 0;
92  if (ppc->remaining_size)
93  break;
94  else
95  i += ppc->chunk_length;
96  continue;
97  }
98  }
99  ppc->chunk_pos++;
100  }
101 
102 flush:
103  if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
104  return buf_size;
105 
106  ppc->chunk_pos = ppc->pc.frame_start_found = 0;
107 
108  *poutbuf = buf;
109  *poutbuf_size = buf_size;
110  return next;
111 }
112 
114  .codec_ids = { AV_CODEC_ID_PNG },
115  .priv_data_size = sizeof(PNGParseContext),
116  .parser_parse = png_parse,
117  .parser_close = ff_parse_close,
118 };
ff_png_parser
AVCodecParser ff_png_parser
Definition: png_parser.c:113
PNGParseContext::pc
ParseContext pc
Definition: png_parser.c:31
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:323
PNGParseContext::chunk_pos
uint32_t chunk_pos
position inside current chunk
Definition: png_parser.c:32
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
ParseContext
Definition: parser.h:28
PNGParseContext::chunk_length
uint32_t chunk_length
length of the current chunk
Definition: png_parser.c:33
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:110
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:592
NULL
#define NULL
Definition: coverity.c:32
AVCodecParser::codec_ids
int codec_ids[5]
Definition: avcodec.h:3545
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
PNGParseContext::remaining_size
uint32_t remaining_size
remaining size of the current chunk
Definition: png_parser.c:34
png_parse
static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: png_parser.c:37
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: common.h:479
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:273
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
PNGSIG
#define PNGSIG
Definition: png.h:49
ff_combine_frame
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:238
i
int i
Definition: input.c:407
PNGParseContext
Definition: png_parser.c:30
uint8_t
uint8_t
Definition: audio_convert.c:194
parser.h
AVCodecParserContext
Definition: avcodec.h:3377
AVCodecContext
main external API structure.
Definition: avcodec.h:536
ParseContext::state64
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
png.h
AVCodecParser
Definition: avcodec.h:3544
MNGSIG
#define MNGSIG
Definition: png.h:50