FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xwddec.c
Go to the documentation of this file.
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 "internal.h"
29 #include "xwd.h"
30 
31 static int xwd_decode_frame(AVCodecContext *avctx, void *data,
32  int *got_frame, AVPacket *avpkt)
33 {
34  AVFrame *p = data;
35  const uint8_t *buf = avpkt->data;
36  int i, ret, buf_size = avpkt->size;
37  uint32_t version, header_size, vclass, ncolors;
38  uint32_t xoffset, be, bpp, lsize, rsize;
39  uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
40  uint32_t rgb[3];
41  uint8_t *ptr;
42  int width, height;
43  GetByteContext gb;
44 
45  if (buf_size < XWD_HEADER_SIZE)
46  return AVERROR_INVALIDDATA;
47 
48  bytestream2_init(&gb, buf, buf_size);
49  header_size = bytestream2_get_be32u(&gb);
50 
51  version = bytestream2_get_be32u(&gb);
52  if (version != XWD_VERSION) {
53  av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
54  return AVERROR_INVALIDDATA;
55  }
56 
57  if (buf_size < header_size || header_size < XWD_HEADER_SIZE) {
58  av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
59  return AVERROR_INVALIDDATA;
60  }
61 
62  pixformat = bytestream2_get_be32u(&gb);
63  pixdepth = bytestream2_get_be32u(&gb);
64  width = bytestream2_get_be32u(&gb);
65  height = bytestream2_get_be32u(&gb);
66  xoffset = bytestream2_get_be32u(&gb);
67  be = bytestream2_get_be32u(&gb);
68  bunit = bytestream2_get_be32u(&gb);
69  bitorder = bytestream2_get_be32u(&gb);
70  bpad = bytestream2_get_be32u(&gb);
71  bpp = bytestream2_get_be32u(&gb);
72  lsize = bytestream2_get_be32u(&gb);
73  vclass = bytestream2_get_be32u(&gb);
74  rgb[0] = bytestream2_get_be32u(&gb);
75  rgb[1] = bytestream2_get_be32u(&gb);
76  rgb[2] = bytestream2_get_be32u(&gb);
77  bytestream2_skipu(&gb, 8);
78  ncolors = bytestream2_get_be32u(&gb);
79  bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20));
80 
81  if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
82  return ret;
83 
84  av_log(avctx, AV_LOG_DEBUG,
85  "pixformat %"PRIu32", pixdepth %"PRIu32", bunit %"PRIu32", bitorder %"PRIu32", bpad %"PRIu32"\n",
86  pixformat, pixdepth, bunit, bitorder, bpad);
87  av_log(avctx, AV_LOG_DEBUG,
88  "vclass %"PRIu32", ncolors %"PRIu32", bpp %"PRIu32", be %"PRIu32", lsize %"PRIu32", xoffset %"PRIu32"\n",
89  vclass, ncolors, bpp, be, lsize, xoffset);
90  av_log(avctx, AV_LOG_DEBUG,
91  "red %0"PRIx32", green %0"PRIx32", blue %0"PRIx32"\n",
92  rgb[0], rgb[1], rgb[2]);
93 
94  if (pixformat > XWD_Z_PIXMAP) {
95  av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
96  return AVERROR_INVALIDDATA;
97  }
98 
99  if (pixdepth == 0 || pixdepth > 32) {
100  av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
101  return AVERROR_INVALIDDATA;
102  }
103 
104  if (xoffset) {
105  avpriv_request_sample(avctx, "xoffset %"PRIu32"", xoffset);
106  return AVERROR_PATCHWELCOME;
107  }
108 
109  if (be > 1) {
110  av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
111  return AVERROR_INVALIDDATA;
112  }
113 
114  if (bitorder > 1) {
115  av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
116  return AVERROR_INVALIDDATA;
117  }
118 
119  if (bunit != 8 && bunit != 16 && bunit != 32) {
120  av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
121  return AVERROR_INVALIDDATA;
122  }
123 
124  if (bpad != 8 && bpad != 16 && bpad != 32) {
125  av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
126  return AVERROR_INVALIDDATA;
127  }
128 
129  if (bpp == 0 || bpp > 32) {
130  av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
131  return AVERROR_INVALIDDATA;
132  }
133 
134  if (ncolors > 256) {
135  av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
136  return AVERROR_INVALIDDATA;
137  }
138 
139  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
140  return ret;
141 
142  rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
143  if (lsize < rsize) {
144  av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
145  return AVERROR_INVALIDDATA;
146  }
147 
148  if (bytestream2_get_bytes_left(&gb) < ncolors * XWD_CMAP_SIZE + (uint64_t)avctx->height * lsize) {
149  av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
150  return AVERROR_INVALIDDATA;
151  }
152 
153  if (pixformat != XWD_Z_PIXMAP) {
154  avpriv_report_missing_feature(avctx, "Pixmap format %"PRIu32, pixformat);
155  return AVERROR_PATCHWELCOME;
156  }
157 
158  avctx->pix_fmt = AV_PIX_FMT_NONE;
159  switch (vclass) {
160  case XWD_STATIC_GRAY:
161  case XWD_GRAY_SCALE:
162  if (bpp != 1 && bpp != 8)
163  return AVERROR_INVALIDDATA;
164  if (bpp == 1 && pixdepth == 1) {
165  avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
166  } else if (bpp == 8 && pixdepth == 8) {
167  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
168  }
169  break;
170  case XWD_STATIC_COLOR:
171  case XWD_PSEUDO_COLOR:
172  if (bpp == 8)
173  avctx->pix_fmt = AV_PIX_FMT_PAL8;
174  break;
175  case XWD_TRUE_COLOR:
176  case XWD_DIRECT_COLOR:
177  if (bpp != 16 && bpp != 24 && bpp != 32)
178  return AVERROR_INVALIDDATA;
179  if (bpp == 16 && pixdepth == 15) {
180  if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
182  else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
184  } else if (bpp == 16 && pixdepth == 16) {
185  if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
187  else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
189  } else if (bpp == 24) {
190  if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
191  avctx->pix_fmt = be ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
192  else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
193  avctx->pix_fmt = be ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
194  } else if (bpp == 32) {
195  if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
196  avctx->pix_fmt = be ? AV_PIX_FMT_ARGB : AV_PIX_FMT_BGRA;
197  else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
198  avctx->pix_fmt = be ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA;
199  }
200  bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE);
201  break;
202  default:
203  av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
204  return AVERROR_INVALIDDATA;
205  }
206 
207  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
208  avpriv_request_sample(avctx,
209  "Unknown file: bpp %"PRIu32", pixdepth %"PRIu32", vclass %"PRIu32"",
210  bpp, pixdepth, vclass);
211  return AVERROR_PATCHWELCOME;
212  }
213 
214  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
215  return ret;
216 
217  p->key_frame = 1;
219 
220  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
221  uint32_t *dst = (uint32_t *)p->data[1];
222  uint8_t red, green, blue;
223 
224  for (i = 0; i < ncolors; i++) {
225 
226  bytestream2_skipu(&gb, 4); // skip colormap entry number
227  red = bytestream2_get_byteu(&gb);
228  bytestream2_skipu(&gb, 1);
229  green = bytestream2_get_byteu(&gb);
230  bytestream2_skipu(&gb, 1);
231  blue = bytestream2_get_byteu(&gb);
232  bytestream2_skipu(&gb, 3); // skip bitmask flag and padding
233 
234  dst[i] = 0xFFU << 24 | red << 16 | green << 8 | blue;
235  }
236  }
237 
238  ptr = p->data[0];
239  for (i = 0; i < avctx->height; i++) {
240  bytestream2_get_bufferu(&gb, ptr, rsize);
241  bytestream2_skipu(&gb, lsize - rsize);
242  ptr += p->linesize[0];
243  }
244 
245  *got_frame = 1;
246 
247  return buf_size;
248 }
249 
251  .name = "xwd",
252  .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
253  .type = AVMEDIA_TYPE_VIDEO,
254  .id = AV_CODEC_ID_XWD,
255  .decode = xwd_decode_frame,
256  .capabilities = AV_CODEC_CAP_DR1,
257 };
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
#define XWD_GRAY_SCALE
Definition: xwd.h:35
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined ...
Definition: pixfmt.h:104
int size
Definition: avcodec.h:1432
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1728
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition: pixfmt.h:107
int version
Definition: avisynth_c.h:766
AVCodec.
Definition: avcodec.h:3409
#define XWD_DIRECT_COLOR
Definition: xwd.h:39
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:273
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:102
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
#define XWD_STATIC_GRAY
Definition: xwd.h:34
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:101
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:90
#define height
uint8_t * data
Definition: avcodec.h:1431
#define XWD_HEADER_SIZE
Definition: xwd.h:27
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:170
#define XWD_CMAP_SIZE
Definition: xwd.h:28
#define XWD_VERSION
Definition: xwd.h:26
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define XWD_Z_PIXMAP
Definition: xwd.h:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:91
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
uint16_t width
Definition: gdv.c:47
const char * name
Name of the codec implementation.
Definition: avcodec.h:3416
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:88
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:89
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:282
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
int width
picture width / height.
Definition: avcodec.h:1691
static int xwd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xwddec.c:31
AVCodec ff_xwd_decoder
Definition: xwddec.c:250
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition: pixfmt.h:106
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
main external API structure.
Definition: avcodec.h:1519
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1891
void * buf
Definition: avisynth_c.h:690
packed BGR 5:5:5, 16bpp, (msb)1X 5B 5G 5R(lsb), little-endian, X=unused/undefined ...
Definition: pixfmt.h:109
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define XWD_STATIC_COLOR
Definition: xwd.h:36
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition: pixfmt.h:103
#define XWD_TRUE_COLOR
Definition: xwd.h:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
Y , 8bpp.
Definition: pixfmt.h:70
common internal api header.
#define XWD_PSEUDO_COLOR
Definition: xwd.h:37
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:71
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
packed BGR 5:5:5, 16bpp, (msb)1X 5B 5G 5R(lsb), big-endian , X=unused/undefined
Definition: pixfmt.h:108
This structure stores compressed data.
Definition: avcodec.h:1408
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:960
for(j=16;j >0;--j)