FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/png_parser.c
Date: 2026-08-30 00:57:52
Exec Total Coverage
Lines: 48 53 90.6%
Functions: 1 1 100.0%
Branches: 28 34 82.4%

Line Branch Exec Source
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
31 typedef struct PNGParseContext {
32 ParseContext pc;
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
36 } PNGParseContext;
37
38 956 static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx,
39 const uint8_t **poutbuf, int *poutbuf_size,
40 const uint8_t *buf, int buf_size)
41 {
42 956 PNGParseContext *ppc = s->priv_data;
43 956 int next = END_NOT_FOUND;
44 956 int i = 0;
45
46 956 s->pict_type = AV_PICTURE_TYPE_NONE;
47
48 956 *poutbuf_size = 0;
49 956 *poutbuf = NULL;
50
51
2/2
✓ Branch 0 taken 317 times.
✓ Branch 1 taken 639 times.
956 if (!ppc->pc.frame_start_found) {
52 317 uint64_t state64 = ppc->pc.state64;
53
2/2
✓ Branch 0 taken 2136 times.
✓ Branch 1 taken 50 times.
2186 for (; i < buf_size; i++) {
54 2136 state64 = (state64 << 8) | buf[i];
55
3/4
✓ Branch 0 taken 1869 times.
✓ Branch 1 taken 267 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1869 times.
2136 if (state64 == PNGSIG || state64 == MNGSIG) {
56 267 i++;
57 267 ppc->pc.frame_start_found = 1;
58 267 break;
59 }
60 }
61 317 ppc->pc.state64 = state64;
62
1/2
✓ Branch 0 taken 639 times.
✗ Branch 1 not taken.
639 } else if (ppc->remaining_size) {
63 639 i = FFMIN(ppc->remaining_size, buf_size);
64 639 ppc->remaining_size -= i;
65
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 451 times.
639 if (ppc->remaining_size)
66 188 goto flush;
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 451 times.
451 if (ppc->chunk_pos == -1) {
68 next = i;
69 goto flush;
70 }
71 }
72
73
3/4
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 64376 times.
✓ Branch 2 taken 64376 times.
✗ Branch 3 not taken.
64426 for (; ppc->pc.frame_start_found && i < buf_size; i++) {
74 64376 ppc->pc.state = (ppc->pc.state << 8) | buf[i];
75
2/2
✓ Branch 0 taken 8047 times.
✓ Branch 1 taken 56329 times.
64376 if (ppc->chunk_pos == 3) {
76 8047 ppc->chunk_length = ppc->pc.state;
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8047 times.
8047 if (ppc->chunk_length > 0x7fffffff) {
78 ppc->chunk_pos = ppc->pc.frame_start_found = 0;
79 goto flush;
80 }
81 8047 ppc->chunk_length += 4;
82
2/2
✓ Branch 0 taken 8047 times.
✓ Branch 1 taken 48282 times.
56329 } else if (ppc->chunk_pos == 7) {
83
2/2
✓ Branch 0 taken 452 times.
✓ Branch 1 taken 7595 times.
8047 if (ppc->chunk_length >= buf_size - i)
84 452 ppc->remaining_size = ppc->chunk_length - buf_size + i + 1;
85
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 7781 times.
8047 if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266 times.
266 if (ppc->remaining_size)
87 ppc->chunk_pos = -1;
88 else
89 266 next = ppc->chunk_length + i + 1;
90 266 break;
91 } else {
92 7781 ppc->chunk_pos = 0;
93
2/2
✓ Branch 0 taken 452 times.
✓ Branch 1 taken 7329 times.
7781 if (ppc->remaining_size)
94 452 break;
95 else
96 7329 i += ppc->chunk_length;
97 7329 continue;
98 }
99 }
100 56329 ppc->chunk_pos++;
101 }
102
103 50 flush:
104
2/2
✓ Branch 1 taken 639 times.
✓ Branch 2 taken 317 times.
956 if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
105 639 return buf_size;
106
107 317 ppc->chunk_pos = ppc->pc.frame_start_found = 0;
108
109 317 *poutbuf = buf;
110 317 *poutbuf_size = buf_size;
111 317 return next;
112 }
113
114 const FFCodecParser ff_png_parser = {
115 PARSER_CODEC_LIST(AV_CODEC_ID_PNG),
116 .priv_data_size = sizeof(PNGParseContext),
117 .parse = png_parse,
118 .close = ff_parse_close,
119 };
120