Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/xwddec.c
Line
Count
Source
1
/*
2
 * XWD image format
3
 *
4
 * Copyright (c) 2012 Paul B Mahol
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#include <inttypes.h>
24
25
#include "libavutil/imgutils.h"
26
#include "avcodec.h"
27
#include "bytestream.h"
28
#include "codec_internal.h"
29
#include "decode.h"
30
#include "xwd.h"
31
32
static int xwd_decode_frame(AVCodecContext *avctx, AVFrame *p,
33
                            int *got_frame, AVPacket *avpkt)
34
328k
{
35
328k
    uint32_t version, header_size, vclass, ncolors;
36
328k
    uint32_t xoffset, be, bpp, lsize, rsize;
37
328k
    uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
38
328k
    uint32_t rgb[3];
39
328k
    uint8_t *ptr;
40
328k
    int width, height;
41
328k
    GetByteContext gb;
42
328k
    int ret;
43
44
328k
    if (avpkt->size < XWD_HEADER_SIZE)
45
242k
        return AVERROR_INVALIDDATA;
46
47
85.4k
    bytestream2_init(&gb, avpkt->data, avpkt->size);
48
85.4k
    header_size = bytestream2_get_be32u(&gb);
49
50
85.4k
    version = bytestream2_get_be32u(&gb);
51
85.4k
    if (version != XWD_VERSION) {
52
5.19k
        av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
53
5.19k
        return AVERROR_INVALIDDATA;
54
5.19k
    }
55
56
80.2k
    if (avpkt->size < header_size || header_size < XWD_HEADER_SIZE) {
57
848
        av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
58
848
        return AVERROR_INVALIDDATA;
59
848
    }
60
61
79.4k
    pixformat     = bytestream2_get_be32u(&gb);
62
79.4k
    pixdepth      = bytestream2_get_be32u(&gb);
63
79.4k
    width         = bytestream2_get_be32u(&gb);
64
79.4k
    height        = bytestream2_get_be32u(&gb);
65
79.4k
    xoffset       = bytestream2_get_be32u(&gb);
66
79.4k
    be            = bytestream2_get_be32u(&gb);
67
79.4k
    bunit         = bytestream2_get_be32u(&gb);
68
79.4k
    bitorder      = bytestream2_get_be32u(&gb);
69
79.4k
    bpad          = bytestream2_get_be32u(&gb);
70
79.4k
    bpp           = bytestream2_get_be32u(&gb);
71
79.4k
    lsize         = bytestream2_get_be32u(&gb);
72
79.4k
    vclass        = bytestream2_get_be32u(&gb);
73
79.4k
    rgb[0]        = bytestream2_get_be32u(&gb);
74
79.4k
    rgb[1]        = bytestream2_get_be32u(&gb);
75
79.4k
    rgb[2]        = bytestream2_get_be32u(&gb);
76
79.4k
    bytestream2_skipu(&gb, 8);
77
79.4k
    ncolors       = bytestream2_get_be32u(&gb);
78
79.4k
    bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20));
79
80
79.4k
    if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
81
2.37k
        return ret;
82
83
77.0k
    av_log(avctx, AV_LOG_DEBUG,
84
77.0k
           "pixformat %"PRIu32", pixdepth %"PRIu32", bunit %"PRIu32", bitorder %"PRIu32", bpad %"PRIu32"\n",
85
77.0k
           pixformat, pixdepth, bunit, bitorder, bpad);
86
77.0k
    av_log(avctx, AV_LOG_DEBUG,
87
77.0k
           "vclass %"PRIu32", ncolors %"PRIu32", bpp %"PRIu32", be %"PRIu32", lsize %"PRIu32", xoffset %"PRIu32"\n",
88
77.0k
           vclass, ncolors, bpp, be, lsize, xoffset);
89
77.0k
    av_log(avctx, AV_LOG_DEBUG,
90
77.0k
           "red %0"PRIx32", green %0"PRIx32", blue %0"PRIx32"\n",
91
77.0k
           rgb[0], rgb[1], rgb[2]);
92
93
77.0k
    if (pixformat > XWD_Z_PIXMAP) {
94
419
        av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
95
419
        return AVERROR_INVALIDDATA;
96
419
    }
97
98
76.6k
    if (pixdepth == 0 || pixdepth > 32) {
99
549
        av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
100
549
        return AVERROR_INVALIDDATA;
101
549
    }
102
103
76.0k
    if (xoffset) {
104
388
        avpriv_request_sample(avctx, "xoffset %"PRIu32"", xoffset);
105
388
        return AVERROR_PATCHWELCOME;
106
388
    }
107
108
75.6k
    if (be > 1) {
109
294
        av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
110
294
        return AVERROR_INVALIDDATA;
111
294
    }
112
113
75.3k
    if (bitorder > 1) {
114
653
        av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
115
653
        return AVERROR_INVALIDDATA;
116
653
    }
117
118
74.7k
    if (bunit != 8 && bunit != 16 && bunit != 32) {
119
500
        av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
120
500
        return AVERROR_INVALIDDATA;
121
500
    }
122
123
74.2k
    if (bpad != 8 && bpad != 16 && bpad != 32) {
124
601
        av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
125
601
        return AVERROR_INVALIDDATA;
126
601
    }
127
128
73.6k
    if (bpp == 0 || bpp > 32) {
129
760
        av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
130
760
        return AVERROR_INVALIDDATA;
131
760
    }
132
133
72.8k
    if (ncolors > 256) {
134
756
        av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
135
756
        return AVERROR_INVALIDDATA;
136
756
    }
137
138
72.1k
    if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
139
0
        return ret;
140
141
72.1k
    rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
142
72.1k
    if (lsize < rsize) {
143
291
        av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
144
291
        return AVERROR_INVALIDDATA;
145
291
    }
146
147
71.8k
    if (bytestream2_get_bytes_left(&gb) < ncolors * XWD_CMAP_SIZE + (uint64_t)avctx->height * lsize) {
148
1.15k
        av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
149
1.15k
        return AVERROR_INVALIDDATA;
150
1.15k
    }
151
152
70.6k
    if (pixformat != XWD_Z_PIXMAP) {
153
195
        avpriv_report_missing_feature(avctx, "Pixmap format %"PRIu32, pixformat);
154
195
        return AVERROR_PATCHWELCOME;
155
195
    }
156
157
70.4k
    avctx->pix_fmt = AV_PIX_FMT_NONE;
158
70.4k
    switch (vclass) {
159
58.5k
    case XWD_STATIC_GRAY:
160
60.0k
    case XWD_GRAY_SCALE:
161
60.0k
        if (bpp != 1 && bpp != 8)
162
204
            return AVERROR_INVALIDDATA;
163
59.8k
        if (bpp == 1 && pixdepth == 1) {
164
1.59k
            avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
165
58.2k
        } else if (bpp == 8 && pixdepth == 8) {
166
57.8k
            avctx->pix_fmt = AV_PIX_FMT_GRAY8;
167
57.8k
        }
168
59.8k
        break;
169
1.25k
    case XWD_STATIC_COLOR:
170
1.47k
    case XWD_PSEUDO_COLOR:
171
1.47k
        if (bpp == 8)
172
1.27k
            avctx->pix_fmt = AV_PIX_FMT_PAL8;
173
1.47k
        break;
174
8.34k
    case XWD_TRUE_COLOR:
175
8.69k
    case XWD_DIRECT_COLOR:
176
8.69k
        if (bpp != 16 && bpp != 24 && bpp != 32)
177
204
            return AVERROR_INVALIDDATA;
178
8.49k
        if (bpp == 16 && pixdepth == 15) {
179
1.86k
            if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
180
389
                avctx->pix_fmt = be ? AV_PIX_FMT_RGB555BE : AV_PIX_FMT_RGB555LE;
181
1.47k
            else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
182
195
                avctx->pix_fmt = be ? AV_PIX_FMT_BGR555BE : AV_PIX_FMT_BGR555LE;
183
6.62k
        } else if (bpp == 16 && pixdepth == 16) {
184
1.70k
            if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
185
196
                avctx->pix_fmt = be ? AV_PIX_FMT_RGB565BE : AV_PIX_FMT_RGB565LE;
186
1.50k
            else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
187
209
                avctx->pix_fmt = be ? AV_PIX_FMT_BGR565BE : AV_PIX_FMT_BGR565LE;
188
4.91k
        } else if (bpp == 24) {
189
1.83k
            if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
190
394
                avctx->pix_fmt = be ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
191
1.44k
            else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
192
202
                avctx->pix_fmt = be ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
193
3.08k
        } else if (bpp == 32) {
194
2.87k
            if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
195
287
                avctx->pix_fmt = be ? AV_PIX_FMT_ARGB : AV_PIX_FMT_BGRA;
196
2.59k
            else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
197
1.30k
                avctx->pix_fmt = be ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA;
198
2.87k
        }
199
8.49k
        bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE);
200
8.49k
        break;
201
230
    default:
202
230
        av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
203
230
        return AVERROR_INVALIDDATA;
204
70.4k
    }
205
206
69.8k
    if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
207
5.90k
        avpriv_request_sample(avctx,
208
5.90k
                              "Unknown file: bpp %"PRIu32", pixdepth %"PRIu32", vclass %"PRIu32"",
209
5.90k
                              bpp, pixdepth, vclass);
210
5.90k
        return AVERROR_PATCHWELCOME;
211
5.90k
    }
212
213
63.9k
    if (avctx->skip_frame >= AVDISCARD_ALL)
214
567
        return avpkt->size;
215
216
63.3k
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
217
0
        return ret;
218
219
63.3k
    if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
220
1.26k
        uint32_t *dst = (uint32_t *)p->data[1];
221
1.26k
        uint8_t red, green, blue;
222
223
3.42k
        for (int i = 0; i < ncolors; i++) {
224
2.16k
            bytestream2_skipu(&gb, 4); // skip colormap entry number
225
2.16k
            red    = bytestream2_get_byteu(&gb);
226
2.16k
            bytestream2_skipu(&gb, 1);
227
2.16k
            green  = bytestream2_get_byteu(&gb);
228
2.16k
            bytestream2_skipu(&gb, 1);
229
2.16k
            blue   = bytestream2_get_byteu(&gb);
230
2.16k
            bytestream2_skipu(&gb, 3); // skip bitmask flag and padding
231
232
2.16k
            dst[i] = 0xFFU << 24 | red << 16 | green << 8 | blue;
233
2.16k
        }
234
1.26k
    }
235
236
63.3k
    ptr = p->data[0];
237
340k
    for (int i = 0; i < avctx->height; i++) {
238
276k
        bytestream2_get_bufferu(&gb, ptr, rsize);
239
276k
        bytestream2_skipu(&gb, lsize - rsize);
240
276k
        ptr += p->linesize[0];
241
276k
    }
242
243
63.3k
    *got_frame       = 1;
244
245
63.3k
    return avpkt->size;
246
63.3k
}
247
248
const FFCodec ff_xwd_decoder = {
249
    .p.name         = "xwd",
250
    CODEC_LONG_NAME("XWD (X Window Dump) image"),
251
    .p.type         = AVMEDIA_TYPE_VIDEO,
252
    .p.id           = AV_CODEC_ID_XWD,
253
    .p.capabilities = AV_CODEC_CAP_DR1,
254
    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
255
    FF_CODEC_DECODE_CB(xwd_decode_frame),
256
};