Browse code

XWD encoder and decoder

Signed-off-by: Kostya Shishkov <kostya.shishkov@gmail.com>

Paul B Mahol authored on 2012/01/22 02:45:52
Showing 10 changed files
... ...
@@ -6,6 +6,7 @@ version 0.8:
6 6
 
7 7
 - GSM audio parser
8 8
 - SMJPEG muxer
9
+- XWD encoder and decoder
9 10
 
10 11
 
11 12
 version 0.8_beta2:
... ...
@@ -379,6 +379,8 @@ following image formats are supported:
379 379
     @tab YUV, JPEG and some extension is not supported yet.
380 380
 @item Truevision Targa  @tab X @tab X
381 381
     @tab Targa (.TGA) image format
382
+@item XWD  @tab X @tab X
383
+    @tab X Window Dump image format
382 384
 @end multitable
383 385
 
384 386
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -435,6 +435,8 @@ OBJS-$(CONFIG_XAN_WC4_DECODER)         += xxan.o
435 435
 OBJS-$(CONFIG_XL_DECODER)              += xl.o
436 436
 OBJS-$(CONFIG_XSUB_DECODER)            += xsubdec.o
437 437
 OBJS-$(CONFIG_XSUB_ENCODER)            += xsubenc.o
438
+OBJS-$(CONFIG_XWD_DECODER)             += xwddec.o
439
+OBJS-$(CONFIG_XWD_ENCODER)             += xwdenc.o
438 440
 OBJS-$(CONFIG_YOP_DECODER)             += yop.o
439 441
 OBJS-$(CONFIG_ZLIB_DECODER)            += lcldec.o
440 442
 OBJS-$(CONFIG_ZLIB_ENCODER)            += lclenc.o
... ...
@@ -230,6 +230,7 @@ void avcodec_register_all(void)
230 230
     REGISTER_DECODER (XAN_WC3, xan_wc3);
231 231
     REGISTER_DECODER (XAN_WC4, xan_wc4);
232 232
     REGISTER_DECODER (XL, xl);
233
+    REGISTER_ENCDEC  (XWD, xwd);
233 234
     REGISTER_DECODER (YOP, yop);
234 235
     REGISTER_ENCDEC  (ZLIB, zlib);
235 236
     REGISTER_ENCDEC  (ZMBV, zmbv);
... ...
@@ -254,6 +254,7 @@ enum CodecID {
254 254
     CODEC_ID_VBLE,
255 255
     CODEC_ID_DXTORY,
256 256
     CODEC_ID_V410,
257
+    CODEC_ID_XWD,
257 258
 
258 259
     /* various PCM "codecs" */
259 260
     CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR 34
24
+#define LIBAVCODEC_VERSION_MINOR 35
25 25
 #define LIBAVCODEC_VERSION_MICRO  0
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
28 28
new file mode 100644
... ...
@@ -0,0 +1,41 @@
0
+/*
1
+ * XWD image format
2
+ *
3
+ * Copyright (c) 2012 Paul B Mahol
4
+ *
5
+ * This file is part of Libav.
6
+ *
7
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AVCODEC_XWD_H
23
+#define AVCODEC_XWD_H
24
+
25
+#define XWD_VERSION         7
26
+#define XWD_HEADER_SIZE     100
27
+#define XWD_CMAP_SIZE       12
28
+
29
+#define XWD_XY_BITMAP       0
30
+#define XWD_XY_PIXMAP       1
31
+#define XWD_Z_PIXMAP        2
32
+
33
+#define XWD_STATIC_GRAY     0
34
+#define XWD_GRAY_SCALE      1
35
+#define XWD_STATIC_COLOR    2
36
+#define XWD_PSEUDO_COLOR    3
37
+#define XWD_TRUE_COLOR      4
38
+#define XWD_DIRECT_COLOR    5
39
+
40
+#endif /* AVCODEC_XWD_H */
0 41
new file mode 100644
... ...
@@ -0,0 +1,267 @@
0
+/*
1
+ * XWD image format
2
+ *
3
+ * Copyright (c) 2012 Paul B Mahol
4
+ *
5
+ * This file is part of Libav.
6
+ *
7
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "libavutil/imgutils.h"
23
+#include "avcodec.h"
24
+#include "bytestream.h"
25
+#include "xwd.h"
26
+
27
+static av_cold int xwd_decode_init(AVCodecContext *avctx)
28
+{
29
+    avctx->coded_frame = avcodec_alloc_frame();
30
+    if (!avctx->coded_frame)
31
+        return AVERROR(ENOMEM);
32
+
33
+    return 0;
34
+}
35
+
36
+static int xwd_decode_frame(AVCodecContext *avctx, void *data,
37
+                            int *data_size, AVPacket *avpkt)
38
+{
39
+    AVFrame *p = avctx->coded_frame;
40
+    const uint8_t *buf = avpkt->data;
41
+    int i, ret, buf_size = avpkt->size;
42
+    uint32_t version, header_size, vclass, ncolors;
43
+    uint32_t xoffset, be, bpp, lsize, rsize;
44
+    uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
45
+    uint32_t rgb[3];
46
+    uint8_t *ptr;
47
+
48
+    if (buf_size < XWD_HEADER_SIZE)
49
+        return AVERROR_INVALIDDATA;
50
+
51
+    header_size = bytestream_get_be32(&buf);
52
+    if (buf_size < header_size)
53
+        return AVERROR_INVALIDDATA;
54
+
55
+    version = bytestream_get_be32(&buf);
56
+    if (version != XWD_VERSION) {
57
+        av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
58
+        return AVERROR_INVALIDDATA;
59
+    }
60
+
61
+    if (header_size < XWD_HEADER_SIZE) {
62
+        av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
63
+        return AVERROR_INVALIDDATA;
64
+    }
65
+
66
+    pixformat     = bytestream_get_be32(&buf);
67
+    pixdepth      = bytestream_get_be32(&buf);
68
+    avctx->width  = bytestream_get_be32(&buf);
69
+    avctx->height = bytestream_get_be32(&buf);
70
+    xoffset       = bytestream_get_be32(&buf);
71
+    be            = bytestream_get_be32(&buf);
72
+    bunit         = bytestream_get_be32(&buf);
73
+    bitorder      = bytestream_get_be32(&buf);
74
+    bpad          = bytestream_get_be32(&buf);
75
+    bpp           = bytestream_get_be32(&buf);
76
+    lsize         = bytestream_get_be32(&buf);
77
+    vclass        = bytestream_get_be32(&buf);
78
+    rgb[0]        = bytestream_get_be32(&buf);
79
+    rgb[1]        = bytestream_get_be32(&buf);
80
+    rgb[2]        = bytestream_get_be32(&buf);
81
+    buf          += 8;
82
+    ncolors       = bytestream_get_be32(&buf);
83
+    buf          += header_size - (XWD_HEADER_SIZE - 20);
84
+
85
+    av_log(avctx, AV_LOG_DEBUG, "pixformat %d, pixdepth %d, bunit %d, bitorder %d, bpad %d\n",
86
+           pixformat, pixdepth, bunit, bitorder, bpad);
87
+    av_log(avctx, AV_LOG_DEBUG, "vclass %d, ncolors %d, bpp %d, be %d, lsize %d, xoffset %d\n",
88
+           vclass, ncolors, bpp, be, lsize, xoffset);
89
+    av_log(avctx, AV_LOG_DEBUG, "red %0x, green %0x, blue %0x\n", rgb[0], rgb[1], rgb[2]);
90
+
91
+    if (pixformat > XWD_Z_PIXMAP) {
92
+        av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
93
+        return AVERROR_INVALIDDATA;
94
+    }
95
+
96
+    if (pixdepth == 0 || pixdepth > 32) {
97
+        av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
98
+        return AVERROR_INVALIDDATA;
99
+    }
100
+
101
+    if (xoffset) {
102
+        av_log_ask_for_sample(avctx, "unsupported xoffset %d\n", xoffset);
103
+        return AVERROR_PATCHWELCOME;
104
+    }
105
+
106
+    if (be > 1) {
107
+        av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
108
+        return AVERROR_INVALIDDATA;
109
+    }
110
+
111
+    if (bitorder > 1) {
112
+        av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
113
+        return AVERROR_INVALIDDATA;
114
+    }
115
+
116
+    if (bunit != 8 && bunit != 16 && bunit != 32) {
117
+        av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
118
+        return AVERROR_INVALIDDATA;
119
+    }
120
+
121
+    if (bpad != 8 && bpad != 16 && bpad != 32) {
122
+        av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
123
+        return AVERROR_INVALIDDATA;
124
+    }
125
+
126
+    if (bpp == 0 || bpp > 32) {
127
+        av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
128
+        return AVERROR_INVALIDDATA;
129
+    }
130
+
131
+    if (ncolors > 256) {
132
+        av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
133
+        return AVERROR_INVALIDDATA;
134
+    }
135
+
136
+    if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
137
+        return ret;
138
+
139
+    rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
140
+    if (lsize < rsize) {
141
+        av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
142
+        return AVERROR_INVALIDDATA;
143
+    }
144
+
145
+    if (buf_size < header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize) {
146
+        av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
147
+        return AVERROR_INVALIDDATA;
148
+    }
149
+
150
+    if (pixformat != XWD_Z_PIXMAP) {
151
+        av_log(avctx, AV_LOG_ERROR, "pixmap format %d unsupported\n", pixformat);
152
+        return AVERROR_PATCHWELCOME;
153
+    }
154
+
155
+    avctx->pix_fmt = PIX_FMT_NONE;
156
+    switch (vclass) {
157
+    case XWD_STATIC_GRAY:
158
+    case XWD_GRAY_SCALE:
159
+        if (bpp != 1)
160
+            return AVERROR_INVALIDDATA;
161
+        if (pixdepth == 1)
162
+            avctx->pix_fmt = PIX_FMT_MONOWHITE;
163
+        break;
164
+    case XWD_STATIC_COLOR:
165
+    case XWD_PSEUDO_COLOR:
166
+        if (bpp == 8)
167
+            avctx->pix_fmt = PIX_FMT_PAL8;
168
+        break;
169
+    case XWD_TRUE_COLOR:
170
+    case XWD_DIRECT_COLOR:
171
+        if (bpp != 16 && bpp != 24 && bpp != 32)
172
+            return AVERROR_INVALIDDATA;
173
+        if (bpp == 16 && pixdepth == 15) {
174
+            if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
175
+                avctx->pix_fmt = be ? PIX_FMT_RGB555BE : PIX_FMT_RGB555LE;
176
+            else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
177
+                avctx->pix_fmt = be ? PIX_FMT_BGR555BE : PIX_FMT_BGR555LE;
178
+        } else if (bpp == 16 && pixdepth == 16) {
179
+            if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
180
+                avctx->pix_fmt = be ? PIX_FMT_RGB565BE : PIX_FMT_RGB565LE;
181
+            else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
182
+                avctx->pix_fmt = be ? PIX_FMT_BGR565BE : PIX_FMT_BGR565LE;
183
+        } else if (bpp == 24) {
184
+            if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
185
+                avctx->pix_fmt = be ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
186
+            else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
187
+                avctx->pix_fmt = be ? PIX_FMT_BGR24 : PIX_FMT_RGB24;
188
+        } else if (bpp == 32) {
189
+            if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
190
+                avctx->pix_fmt = be ? PIX_FMT_ARGB : PIX_FMT_BGRA;
191
+            else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
192
+                avctx->pix_fmt = be ? PIX_FMT_ABGR : PIX_FMT_RGBA;
193
+        }
194
+        buf += ncolors * XWD_CMAP_SIZE;
195
+        break;
196
+    default:
197
+        av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
198
+        return AVERROR_INVALIDDATA;
199
+    }
200
+
201
+    if (avctx->pix_fmt == PIX_FMT_NONE) {
202
+        av_log_ask_for_sample(avctx, "unknown file: bpp %d, pixdepth %d, vclass %d\n", bpp, pixdepth, vclass);
203
+        return AVERROR_PATCHWELCOME;
204
+    }
205
+
206
+    if (p->data[0])
207
+        avctx->release_buffer(avctx, p);
208
+
209
+    p->reference = 0;
210
+    if ((ret = avctx->get_buffer(avctx, p)) < 0) {
211
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
212
+        return ret;
213
+    }
214
+
215
+    p->key_frame = 1;
216
+    p->pict_type = AV_PICTURE_TYPE_I;
217
+
218
+    if (avctx->pix_fmt == PIX_FMT_PAL8) {
219
+        uint32_t *dst = (uint32_t *)p->data[1];
220
+        uint8_t red, green, blue;
221
+
222
+        for (i = 0; i < ncolors; i++) {
223
+
224
+            buf   += 4;  // skip colormap entry number
225
+            red    = *buf; buf += 2;
226
+            green  = *buf; buf += 2;
227
+            blue   = *buf; buf += 2;
228
+            buf   += 2;  // skip bitmask flag and padding
229
+
230
+            dst[i] = red << 16 | green << 8 | blue;
231
+        }
232
+    }
233
+
234
+    ptr = p->data[0];
235
+    for (i = 0; i < avctx->height; i++) {
236
+        bytestream_get_buffer(&buf, ptr, rsize);
237
+        buf += lsize - rsize;
238
+        ptr += p->linesize[0];
239
+    }
240
+
241
+    *data_size = sizeof(AVFrame);
242
+    *(AVFrame *)data = *p;
243
+
244
+    return buf_size;
245
+}
246
+
247
+static av_cold int xwd_decode_close(AVCodecContext *avctx)
248
+{
249
+    if (avctx->coded_frame->data[0])
250
+        avctx->release_buffer(avctx, avctx->coded_frame);
251
+
252
+    av_freep(&avctx->coded_frame);
253
+
254
+    return 0;
255
+}
256
+
257
+AVCodec ff_xwd_decoder = {
258
+    .name           = "xwd",
259
+    .type           = AVMEDIA_TYPE_VIDEO,
260
+    .id             = CODEC_ID_XWD,
261
+    .init           = xwd_decode_init,
262
+    .close          = xwd_decode_close,
263
+    .decode         = xwd_decode_frame,
264
+    .capabilities   = CODEC_CAP_DR1,
265
+    .long_name      = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
266
+};
0 267
new file mode 100644
... ...
@@ -0,0 +1,246 @@
0
+/*
1
+ * XWD image format
2
+ *
3
+ * Copyright (c) 2012 Paul B Mahol
4
+ *
5
+ * This file is part of Libav.
6
+ *
7
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "libavutil/intreadwrite.h"
23
+#include "libavutil/pixdesc.h"
24
+#include "avcodec.h"
25
+#include "bytestream.h"
26
+#include "xwd.h"
27
+
28
+#define WINDOW_NAME         "lavcxwdenc"
29
+#define WINDOW_NAME_SIZE    11
30
+
31
+static av_cold int xwd_encode_init(AVCodecContext *avctx)
32
+{
33
+    avctx->coded_frame = avcodec_alloc_frame();
34
+    if (!avctx->coded_frame)
35
+        return AVERROR(ENOMEM);
36
+
37
+    return 0;
38
+}
39
+
40
+static int xwd_encode_frame(AVCodecContext *avctx, uint8_t *buf,
41
+                            int buf_size, void *data)
42
+{
43
+    AVFrame *p = data;
44
+    enum PixelFormat pix_fmt = avctx->pix_fmt;
45
+    uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
46
+    uint32_t rgb[3] = { 0 };
47
+    uint32_t header_size;
48
+    int i, out_size;
49
+    uint8_t *ptr;
50
+
51
+    pixdepth = av_get_bits_per_pixel(&av_pix_fmt_descriptors[pix_fmt]);
52
+    if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BE)
53
+        be = 1;
54
+    switch (pix_fmt) {
55
+    case PIX_FMT_ARGB:
56
+    case PIX_FMT_BGRA:
57
+    case PIX_FMT_RGBA:
58
+    case PIX_FMT_ABGR:
59
+        if (pix_fmt == PIX_FMT_ARGB ||
60
+            pix_fmt == PIX_FMT_ABGR)
61
+            be = 1;
62
+        if (pix_fmt == PIX_FMT_ABGR ||
63
+            pix_fmt == PIX_FMT_RGBA) {
64
+            rgb[0] = 0xFF;
65
+            rgb[1] = 0xFF00;
66
+            rgb[2] = 0xFF0000;
67
+        } else {
68
+            rgb[0] = 0xFF0000;
69
+            rgb[1] = 0xFF00;
70
+            rgb[2] = 0xFF;
71
+        }
72
+        bpp      = 32;
73
+        pixdepth = 24;
74
+        vclass   = XWD_TRUE_COLOR;
75
+        bpad     = 32;
76
+        break;
77
+    case PIX_FMT_BGR24:
78
+    case PIX_FMT_RGB24:
79
+        if (pix_fmt == PIX_FMT_RGB24)
80
+            be = 1;
81
+        bpp      = 24;
82
+        vclass   = XWD_TRUE_COLOR;
83
+        bpad     = 32;
84
+        rgb[0]   = 0xFF0000;
85
+        rgb[1]   = 0xFF00;
86
+        rgb[2]   = 0xFF;
87
+        break;
88
+    case PIX_FMT_RGB565LE:
89
+    case PIX_FMT_RGB565BE:
90
+    case PIX_FMT_BGR565LE:
91
+    case PIX_FMT_BGR565BE:
92
+        if (pix_fmt == PIX_FMT_BGR565LE ||
93
+            pix_fmt == PIX_FMT_BGR565BE) {
94
+            rgb[0] = 0x1F;
95
+            rgb[1] = 0x7E0;
96
+            rgb[2] = 0xF800;
97
+        } else {
98
+            rgb[0] = 0xF800;
99
+            rgb[1] = 0x7E0;
100
+            rgb[2] = 0x1F;
101
+        }
102
+        bpp      = 16;
103
+        vclass   = XWD_TRUE_COLOR;
104
+        bpad     = 16;
105
+        break;
106
+    case PIX_FMT_RGB555LE:
107
+    case PIX_FMT_RGB555BE:
108
+    case PIX_FMT_BGR555LE:
109
+    case PIX_FMT_BGR555BE:
110
+        if (pix_fmt == PIX_FMT_BGR555LE ||
111
+            pix_fmt == PIX_FMT_BGR555BE) {
112
+            rgb[0] = 0x1F;
113
+            rgb[1] = 0x3E0;
114
+            rgb[2] = 0x7C00;
115
+        } else {
116
+            rgb[0] = 0x7C00;
117
+            rgb[1] = 0x3E0;
118
+            rgb[2] = 0x1F;
119
+        }
120
+        bpp      = 16;
121
+        vclass   = XWD_TRUE_COLOR;
122
+        bpad     = 16;
123
+        break;
124
+    case PIX_FMT_RGB8:
125
+    case PIX_FMT_BGR8:
126
+    case PIX_FMT_RGB4_BYTE:
127
+    case PIX_FMT_BGR4_BYTE:
128
+    case PIX_FMT_PAL8:
129
+        bpp      = 8;
130
+        vclass   = XWD_PSEUDO_COLOR;
131
+        bpad     = 8;
132
+        ncolors  = 256;
133
+        break;
134
+    case PIX_FMT_MONOWHITE:
135
+        bpp      = 1;
136
+        bpad     = 8;
137
+        vclass   = XWD_STATIC_GRAY;
138
+        break;
139
+    default:
140
+        av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
141
+        return AVERROR(EINVAL);
142
+    }
143
+
144
+    lsize       = FFALIGN(bpp * avctx->width, bpad) / 8;
145
+    header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
146
+    out_size    = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
147
+
148
+    if (buf_size < out_size) {
149
+        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
150
+        return AVERROR(ENOMEM);
151
+    }
152
+
153
+    avctx->coded_frame->key_frame = 1;
154
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
155
+
156
+    bytestream_put_be32(&buf, header_size);
157
+    bytestream_put_be32(&buf, XWD_VERSION);   // file version
158
+    bytestream_put_be32(&buf, XWD_Z_PIXMAP);  // pixmap format
159
+    bytestream_put_be32(&buf, pixdepth);      // pixmap depth in pixels
160
+    bytestream_put_be32(&buf, avctx->width);  // pixmap width in pixels
161
+    bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
162
+    bytestream_put_be32(&buf, 0);             // bitmap x offset
163
+    bytestream_put_be32(&buf, be);            // byte order
164
+    bytestream_put_be32(&buf, 32);            // bitmap unit
165
+    bytestream_put_be32(&buf, be);            // bit-order of image data
166
+    bytestream_put_be32(&buf, bpad);          // bitmap scan-line pad in bits
167
+    bytestream_put_be32(&buf, bpp);           // bits per pixel
168
+    bytestream_put_be32(&buf, lsize);         // bytes per scan-line
169
+    bytestream_put_be32(&buf, vclass);        // visual class
170
+    bytestream_put_be32(&buf, rgb[0]);        // red mask
171
+    bytestream_put_be32(&buf, rgb[1]);        // green mask
172
+    bytestream_put_be32(&buf, rgb[2]);        // blue mask
173
+    bytestream_put_be32(&buf, 8);             // size of each bitmask in bits
174
+    bytestream_put_be32(&buf, ncolors);       // number of colors
175
+    bytestream_put_be32(&buf, ncolors);       // number of entries in color map
176
+    bytestream_put_be32(&buf, avctx->width);  // window width
177
+    bytestream_put_be32(&buf, avctx->height); // window height
178
+    bytestream_put_be32(&buf, 0);             // window upper left X coordinate
179
+    bytestream_put_be32(&buf, 0);             // window upper left Y coordinate
180
+    bytestream_put_be32(&buf, 0);             // window border width
181
+    bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
182
+
183
+    for (i = 0; i < ncolors; i++) {
184
+        uint32_t val;
185
+        uint8_t red, green, blue;
186
+
187
+        val   = AV_RN32A(p->data[1] + i * 4);
188
+        red   = (val >> 16) & 0xFF;
189
+        green = (val >>  8) & 0xFF;
190
+        blue  =  val        & 0xFF;
191
+
192
+        bytestream_put_be32(&buf, i);         // colormap entry number
193
+        bytestream_put_be16(&buf, red   << 8);
194
+        bytestream_put_be16(&buf, green << 8);
195
+        bytestream_put_be16(&buf, blue  << 8);
196
+        bytestream_put_byte(&buf, 0x7);       // bitmask flag
197
+        bytestream_put_byte(&buf, 0);         // padding
198
+    }
199
+
200
+    ptr = p->data[0];
201
+    for (i = 0; i < avctx->height; i++) {
202
+        bytestream_put_buffer(&buf, ptr, lsize);
203
+        ptr += p->linesize[0];
204
+    }
205
+
206
+    return out_size;
207
+}
208
+
209
+static av_cold int xwd_encode_close(AVCodecContext *avctx)
210
+{
211
+    av_freep(&avctx->coded_frame);
212
+
213
+    return 0;
214
+}
215
+
216
+AVCodec ff_xwd_encoder = {
217
+    .name         = "xwd",
218
+    .type         = AVMEDIA_TYPE_VIDEO,
219
+    .id           = CODEC_ID_XWD,
220
+    .init         = xwd_encode_init,
221
+    .encode       = xwd_encode_frame,
222
+    .close        = xwd_encode_close,
223
+    .pix_fmts     = (const enum PixelFormat[]) { PIX_FMT_BGRA,
224
+                                                 PIX_FMT_RGBA,
225
+                                                 PIX_FMT_ARGB,
226
+                                                 PIX_FMT_ABGR,
227
+                                                 PIX_FMT_RGB24,
228
+                                                 PIX_FMT_BGR24,
229
+                                                 PIX_FMT_RGB565BE,
230
+                                                 PIX_FMT_RGB565LE,
231
+                                                 PIX_FMT_BGR565BE,
232
+                                                 PIX_FMT_BGR565LE,
233
+                                                 PIX_FMT_RGB555BE,
234
+                                                 PIX_FMT_RGB555LE,
235
+                                                 PIX_FMT_BGR555BE,
236
+                                                 PIX_FMT_BGR555LE,
237
+                                                 PIX_FMT_RGB8,
238
+                                                 PIX_FMT_BGR8,
239
+                                                 PIX_FMT_RGB4_BYTE,
240
+                                                 PIX_FMT_BGR4_BYTE,
241
+                                                 PIX_FMT_PAL8,
242
+                                                 PIX_FMT_MONOWHITE,
243
+                                                 PIX_FMT_NONE },
244
+    .long_name    = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
245
+};
... ...
@@ -85,6 +85,7 @@ static const IdStrMap img_tags[] = {
85 85
     { CODEC_ID_JPEG2000  , "jpc"},
86 86
     { CODEC_ID_DPX       , "dpx"},
87 87
     { CODEC_ID_PICTOR    , "pic"},
88
+    { CODEC_ID_XWD       , "xwd"},
88 89
     { CODEC_ID_NONE      , NULL}
89 90
 };
90 91
 
... ...
@@ -501,7 +502,7 @@ AVOutputFormat ff_image2_muxer = {
501 501
     .name           = "image2",
502 502
     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
503 503
     .extensions     = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
504
-                      "ppm,sgi,tga,tif,tiff,jp2",
504
+                      "ppm,sgi,tga,tif,tiff,jp2,xwd",
505 505
     .priv_data_size = sizeof(VideoData),
506 506
     .video_codec    = CODEC_ID_MJPEG,
507 507
     .write_header   = write_header,