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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "xwd.h"
28 
30 {
32  if (!avctx->coded_frame)
33  return AVERROR(ENOMEM);
34 
35  return 0;
36 }
37 
38 static int xwd_decode_frame(AVCodecContext *avctx, void *data,
39  int *got_frame, AVPacket *avpkt)
40 {
41  AVFrame *p = avctx->coded_frame;
42  const uint8_t *buf = avpkt->data;
43  int i, ret, buf_size = avpkt->size;
44  uint32_t version, header_size, vclass, ncolors;
45  uint32_t xoffset, be, bpp, lsize, rsize;
46  uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
47  uint32_t rgb[3];
48  uint8_t *ptr;
49  GetByteContext gb;
50 
51  if (buf_size < XWD_HEADER_SIZE)
52  return AVERROR_INVALIDDATA;
53 
54  bytestream2_init(&gb, buf, buf_size);
55  header_size = bytestream2_get_be32u(&gb);
56 
57  version = bytestream2_get_be32u(&gb);
58  if (version != XWD_VERSION) {
59  av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
60  return AVERROR_INVALIDDATA;
61  }
62 
63  if (buf_size < header_size || header_size < XWD_HEADER_SIZE) {
64  av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
65  return AVERROR_INVALIDDATA;
66  }
67 
68  pixformat = bytestream2_get_be32u(&gb);
69  pixdepth = bytestream2_get_be32u(&gb);
70  avctx->width = bytestream2_get_be32u(&gb);
71  avctx->height = bytestream2_get_be32u(&gb);
72  xoffset = bytestream2_get_be32u(&gb);
73  be = bytestream2_get_be32u(&gb);
74  bunit = bytestream2_get_be32u(&gb);
75  bitorder = bytestream2_get_be32u(&gb);
76  bpad = bytestream2_get_be32u(&gb);
77  bpp = bytestream2_get_be32u(&gb);
78  lsize = bytestream2_get_be32u(&gb);
79  vclass = bytestream2_get_be32u(&gb);
80  rgb[0] = bytestream2_get_be32u(&gb);
81  rgb[1] = bytestream2_get_be32u(&gb);
82  rgb[2] = bytestream2_get_be32u(&gb);
83  bytestream2_skipu(&gb, 8);
84  ncolors = bytestream2_get_be32u(&gb);
85  bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20));
86 
87  av_log(avctx, AV_LOG_DEBUG, "pixformat %d, pixdepth %d, bunit %d, bitorder %d, bpad %d\n",
88  pixformat, pixdepth, bunit, bitorder, bpad);
89  av_log(avctx, AV_LOG_DEBUG, "vclass %d, ncolors %d, bpp %d, be %d, lsize %d, xoffset %d\n",
90  vclass, ncolors, bpp, be, lsize, xoffset);
91  av_log(avctx, AV_LOG_DEBUG, "red %0x, green %0x, blue %0x\n", rgb[0], rgb[1], rgb[2]);
92 
93  if (pixformat > XWD_Z_PIXMAP) {
94  av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
95  return AVERROR_INVALIDDATA;
96  }
97 
98  if (pixdepth == 0 || pixdepth > 32) {
99  av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
100  return AVERROR_INVALIDDATA;
101  }
102 
103  if (xoffset) {
104  av_log_ask_for_sample(avctx, "unsupported xoffset %d\n", xoffset);
105  return AVERROR_PATCHWELCOME;
106  }
107 
108  if (be > 1) {
109  av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
110  return AVERROR_INVALIDDATA;
111  }
112 
113  if (bitorder > 1) {
114  av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
115  return AVERROR_INVALIDDATA;
116  }
117 
118  if (bunit != 8 && bunit != 16 && bunit != 32) {
119  av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
120  return AVERROR_INVALIDDATA;
121  }
122 
123  if (bpad != 8 && bpad != 16 && bpad != 32) {
124  av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
125  return AVERROR_INVALIDDATA;
126  }
127 
128  if (bpp == 0 || bpp > 32) {
129  av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
130  return AVERROR_INVALIDDATA;
131  }
132 
133  if (ncolors > 256) {
134  av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
135  return AVERROR_INVALIDDATA;
136  }
137 
138  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
139  return ret;
140 
141  rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
142  if (lsize < rsize) {
143  av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
144  return AVERROR_INVALIDDATA;
145  }
146 
147  if (bytestream2_get_bytes_left(&gb) < ncolors * XWD_CMAP_SIZE + avctx->height * lsize) {
148  av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
149  return AVERROR_INVALIDDATA;
150  }
151 
152  if (pixformat != XWD_Z_PIXMAP) {
153  av_log(avctx, AV_LOG_ERROR, "pixmap format %d unsupported\n", pixformat);
154  return AVERROR_PATCHWELCOME;
155  }
156 
157  avctx->pix_fmt = AV_PIX_FMT_NONE;
158  switch (vclass) {
159  case XWD_STATIC_GRAY:
160  case XWD_GRAY_SCALE:
161  if (bpp != 1)
162  return AVERROR_INVALIDDATA;
163  if (pixdepth == 1)
164  avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
165  break;
166  case XWD_STATIC_COLOR:
167  case XWD_PSEUDO_COLOR:
168  if (bpp == 8)
169  avctx->pix_fmt = AV_PIX_FMT_PAL8;
170  break;
171  case XWD_TRUE_COLOR:
172  case XWD_DIRECT_COLOR:
173  if (bpp != 16 && bpp != 24 && bpp != 32)
174  return AVERROR_INVALIDDATA;
175  if (bpp == 16 && pixdepth == 15) {
176  if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
178  else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
180  } else if (bpp == 16 && pixdepth == 16) {
181  if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
183  else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
185  } else if (bpp == 24) {
186  if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
187  avctx->pix_fmt = be ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
188  else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
189  avctx->pix_fmt = be ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
190  } else if (bpp == 32) {
191  if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
192  avctx->pix_fmt = be ? AV_PIX_FMT_ARGB : AV_PIX_FMT_BGRA;
193  else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
194  avctx->pix_fmt = be ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA;
195  }
196  bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE);
197  break;
198  default:
199  av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
200  return AVERROR_INVALIDDATA;
201  }
202 
203  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
204  av_log_ask_for_sample(avctx, "unknown file: bpp %d, pixdepth %d, vclass %d\n", bpp, pixdepth, vclass);
205  return AVERROR_PATCHWELCOME;
206  }
207 
208  if (p->data[0])
209  avctx->release_buffer(avctx, p);
210 
211  p->reference = 0;
212  if ((ret = ff_get_buffer(avctx, p)) < 0) {
213  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
214  return ret;
215  }
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] = 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  *(AVFrame *)data = *p;
247 
248  return buf_size;
249 }
250 
252 {
253  if (avctx->coded_frame->data[0])
254  avctx->release_buffer(avctx, avctx->coded_frame);
255 
256  av_freep(&avctx->coded_frame);
257 
258  return 0;
259 }
260 
262  .name = "xwd",
263  .type = AVMEDIA_TYPE_VIDEO,
264  .id = AV_CODEC_ID_XWD,
265  .init = xwd_decode_init,
266  .close = xwd_decode_close,
267  .decode = xwd_decode_frame,
268  .capabilities = CODEC_CAP_DR1,
269  .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
270 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
#define XWD_GRAY_SCALE
Definition: xwd.h:35
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:114
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition: pixfmt.h:117
AVCodec.
Definition: avcodec.h:2960
#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:268
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:112
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define XWD_STATIC_GRAY
Definition: xwd.h:34
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:111
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
#define XWD_HEADER_SIZE
Definition: xwd.h:27
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:165
#define XWD_CMAP_SIZE
Definition: xwd.h:28
#define XWD_VERSION
Definition: xwd.h:26
#define XWD_Z_PIXMAP
Definition: xwd.h:32
void av_log_ask_for_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message asking for a sample.
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
static av_cold int xwd_decode_init(AVCodecContext *avctx)
Definition: xwddec.c:29
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:616
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:220
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
static int xwd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xwddec.c:38
AVCodec ff_xwd_decoder
Definition: xwddec.c:261
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition: pixfmt.h:116
static av_cold int xwd_decode_close(AVCodecContext *avctx)
Definition: xwddec.c:251
NULL
Definition: eval.c:52
external API header
version
Definition: ffv1enc.c:1069
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), little-endian, most significant bit to 1 ...
Definition: pixfmt.h:119
#define XWD_STATIC_COLOR
Definition: xwd.h:36
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:113
#define XWD_TRUE_COLOR
Definition: xwd.h:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
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:74
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), big-endian, most significant bit to 1 ...
Definition: pixfmt.h:118
This structure stores compressed data.
Definition: avcodec.h:898
for(j=16;j >0;--j)