Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/png_parser.c
Line
Count
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
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
38.3k
{
42
38.3k
    PNGParseContext *ppc = s->priv_data;
43
38.3k
    int next = END_NOT_FOUND;
44
38.3k
    int i = 0;
45
46
38.3k
    s->pict_type = AV_PICTURE_TYPE_NONE;
47
48
38.3k
    *poutbuf_size = 0;
49
38.3k
    *poutbuf = NULL;
50
51
38.3k
    if (!ppc->pc.frame_start_found) {
52
20.8k
        uint64_t state64 = ppc->pc.state64;
53
14.5M
        for (; i < buf_size; i++) {
54
14.4M
            state64 = (state64 << 8) | buf[i];
55
14.4M
            if (state64 == PNGSIG || state64 == MNGSIG) {
56
8.13k
                i++;
57
8.13k
                ppc->pc.frame_start_found = 1;
58
8.13k
                break;
59
8.13k
            }
60
14.4M
        }
61
20.8k
        ppc->pc.state64 = state64;
62
20.8k
    } else if (ppc->remaining_size) {
63
17.0k
        i = FFMIN(ppc->remaining_size, buf_size);
64
17.0k
        ppc->remaining_size -= i;
65
17.0k
        if (ppc->remaining_size)
66
15.2k
            goto flush;
67
1.86k
        if (ppc->chunk_pos == -1) {
68
695
            next = i;
69
695
            goto flush;
70
695
        }
71
1.86k
    }
72
73
543k
    for (; ppc->pc.frame_start_found && i < buf_size; i++) {
74
529k
        ppc->pc.state = (ppc->pc.state << 8) | buf[i];
75
529k
        if (ppc->chunk_pos == 3) {
76
66.6k
            ppc->chunk_length = ppc->pc.state;
77
66.6k
            if (ppc->chunk_length > 0x7fffffff) {
78
878
                ppc->chunk_pos = ppc->pc.frame_start_found = 0;
79
878
                goto flush;
80
878
            }
81
65.8k
            ppc->chunk_length += 4;
82
463k
        } else if (ppc->chunk_pos == 7) {
83
65.7k
            if (ppc->chunk_length >= buf_size - i)
84
2.15k
                ppc->remaining_size = ppc->chunk_length - buf_size + i + 1;
85
65.7k
            if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) {
86
6.95k
                if (ppc->remaining_size)
87
725
                    ppc->chunk_pos = -1;
88
6.23k
                else
89
6.23k
                    next = ppc->chunk_length + i + 1;
90
6.95k
                break;
91
58.8k
            } else {
92
58.8k
                ppc->chunk_pos = 0;
93
58.8k
                if (ppc->remaining_size)
94
1.42k
                    break;
95
57.4k
                else
96
57.4k
                    i += ppc->chunk_length;
97
57.4k
                continue;
98
58.8k
            }
99
65.7k
        }
100
463k
        ppc->chunk_pos++;
101
463k
    }
102
103
38.3k
flush:
104
38.3k
    if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
105
31.0k
        return buf_size;
106
107
7.30k
    ppc->chunk_pos = ppc->pc.frame_start_found = 0;
108
109
7.30k
    *poutbuf      = buf;
110
7.30k
    *poutbuf_size = buf_size;
111
7.30k
    return next;
112
38.3k
}
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
};