Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/xwd_parser.c
Line
Count
Source
1
/*
2
 * XWD parser
3
 * Copyright (c) 2022 Paul B Mahol
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
 * XWD parser
25
 **/
26
27
#include "libavutil/intreadwrite.h"
28
#include "parser.h"
29
#include "parser_internal.h"
30
#include "xwd.h"
31
32
typedef struct XWDParseContext {
33
    ParseContext  pc;
34
    int           left;
35
    int           idx;
36
    uint8_t       hdr[XWD_HEADER_SIZE];
37
} XWDParseContext;
38
39
static int xwd_parse(AVCodecParserContext *s, AVCodecContext *avctx,
40
                     const uint8_t **poutbuf, int *poutbuf_size,
41
                     const uint8_t *buf, int buf_size)
42
46.4k
{
43
46.4k
    XWDParseContext *t = s->priv_data;
44
46.4k
    ParseContext *pc   = &t->pc;
45
46.4k
    int next           = END_NOT_FOUND;
46
47
46.4k
    s->pict_type = AV_PICTURE_TYPE_NONE;
48
49
46.4k
    *poutbuf      = NULL;
50
46.4k
    *poutbuf_size = 0;
51
52
46.4k
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
53
0
        next = buf_size;
54
46.4k
    } else {
55
28.9M
        for (int i = 0; i < buf_size; i++) {
56
28.8M
            if (t->left > 0) {
57
5.29M
                t->left--;
58
5.29M
                if (t->left == 0) {
59
4.45k
                    next = i;
60
4.45k
                    break;
61
4.45k
                }
62
5.29M
                continue;
63
5.29M
            }
64
65
23.5M
            if (t->idx >= 100) {
66
21.0M
                t->idx = 99;
67
21.0M
                memmove(&t->hdr[0], &t->hdr[1], XWD_HEADER_SIZE-1);
68
21.0M
            }
69
70
23.5M
            t->hdr[t->idx++] = buf[i];
71
72
23.5M
            if (t->idx >= 100 && AV_RB32(t->hdr + 4) == XWD_VERSION) {
73
24.7k
                uint32_t header_size  = AV_RB32(t->hdr + 0);
74
24.7k
                uint32_t height       = AV_RB32(t->hdr + 20);
75
24.7k
                uint32_t lsize        = AV_RB32(t->hdr + 48);
76
24.7k
                uint32_t ncolors      = AV_RB32(t->hdr + 76);
77
24.7k
                uint32_t size         = header_size + ncolors * XWD_CMAP_SIZE + height * lsize;
78
24.7k
                pc->frame_start_found = 1;
79
24.7k
                t->left               = size - XWD_HEADER_SIZE + 1;
80
24.7k
                t->idx                = 0;
81
24.7k
                memset(t->hdr, 0, sizeof(t->hdr));
82
24.7k
            }
83
23.5M
        }
84
85
46.4k
        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0)
86
41.8k
            return buf_size;
87
46.4k
    }
88
89
4.58k
    *poutbuf      = buf;
90
4.58k
    *poutbuf_size = buf_size;
91
92
4.58k
    s->pict_type = AV_PICTURE_TYPE_I;
93
4.58k
    s->key_frame = 1;
94
4.58k
    s->duration  = 1;
95
96
4.58k
    return next;
97
46.4k
}
98
99
const FFCodecParser ff_xwd_parser = {
100
    PARSER_CODEC_LIST(AV_CODEC_ID_XWD),
101
    .priv_data_size = sizeof(XWDParseContext),
102
    .parse          = xwd_parse,
103
    .close          = ff_parse_close,
104
};