FFmpeg
Loading...
Searching...
No Matches
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 "parser_internal.h"
29#include "png.h"
30
31typedef struct PNGParseContext {
33 uint32_t chunk_pos; ///< position inside current chunk
34 uint32_t chunk_length; ///< length of the current chunk
35 uint32_t remaining_size; ///< remaining size of the current chunk
37
39 const uint8_t **poutbuf, int *poutbuf_size,
40 const uint8_t *buf, int buf_size)
41{
42 PNGParseContext *ppc = s->priv_data;
43 int next = END_NOT_FOUND;
44 int i = 0;
45
46 s->pict_type = AV_PICTURE_TYPE_NONE;
47
48 *poutbuf_size = 0;
49 *poutbuf = NULL;
50
51 if (!ppc->pc.frame_start_found) {
52 uint64_t state64 = ppc->pc.state64;
53 for (; i < buf_size; i++) {
54 state64 = (state64 << 8) | buf[i];
55 if (state64 == PNGSIG || state64 == MNGSIG) {
56 i++;
57 ppc->pc.frame_start_found = 1;
58 break;
59 }
60 }
61 ppc->pc.state64 = state64;
62 } else if (ppc->remaining_size) {
63 i = FFMIN(ppc->remaining_size, buf_size);
64 ppc->remaining_size -= i;
65 if (ppc->remaining_size)
66 goto flush;
67 if (ppc->chunk_pos == -1) {
68 next = i;
69 goto flush;
70 }
71 }
72
73 for (; ppc->pc.frame_start_found && i < buf_size; i++) {
74 ppc->pc.state = (ppc->pc.state << 8) | buf[i];
75 if (ppc->chunk_pos == 3) {
76 ppc->chunk_length = ppc->pc.state;
77 if (ppc->chunk_length > 0x7fffffff) {
78 ppc->chunk_pos = ppc->pc.frame_start_found = 0;
79 goto flush;
80 }
81 ppc->chunk_length += 4;
82 } else if (ppc->chunk_pos == 7) {
83 if (ppc->chunk_length >= buf_size - i)
84 ppc->remaining_size = ppc->chunk_length - buf_size + i + 1;
85 if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) {
86 if (ppc->remaining_size)
87 ppc->chunk_pos = -1;
88 else
89 next = ppc->chunk_length + i + 1;
90 break;
91 } else {
92 ppc->chunk_pos = 0;
93 if (ppc->remaining_size)
94 break;
95 else
96 i += ppc->chunk_length;
97 continue;
98 }
99 }
100 ppc->chunk_pos++;
101 }
102
103flush:
104 if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
105 return buf_size;
106
107 ppc->chunk_pos = ppc->pc.frame_start_found = 0;
108
109 *poutbuf = buf;
110 *poutbuf_size = buf_size;
111 return next;
112}
113
116 .priv_data_size = sizeof(PNGParseContext),
117 .parse = png_parse,
119};
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
@ AV_CODEC_ID_PNG
Definition codec_id.h:111
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition avutil.h:277
#define FFMIN(a, b)
Definition macros.h:49
#define MKBETAG(a, b, c, d)
Definition macros.h:56
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
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:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
const FFCodecParser ff_png_parser
Definition png_parser.c:114
#define MNGSIG
Definition png.h:50
#define PNGSIG
Definition png.h:49
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:38
main external API structure.
Definition avcodec.h:443
ParseContext pc
Definition png_parser.c:32
uint32_t chunk_length
length of the current chunk
Definition png_parser.c:34
uint32_t remaining_size
remaining size of the current chunk
Definition png_parser.c:35
uint32_t chunk_pos
position inside current chunk
Definition png_parser.c:33
uint32_t state
contains the last few bytes in MSB order
Definition parser.h:33
uint64_t state64
contains the last 8 bytes in MSB order
Definition parser.h:37
int frame_start_found
Definition parser.h:34