FFmpeg
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 "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 {
35  uint32_t version, header_size, vclass, ncolors;
36  uint32_t xoffset, be, bpp, lsize, rsize;
37  uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
38  uint32_t rgb[3];
39  uint8_t *ptr;
40  int width, height;
41  GetByteContext gb;
42  int ret;
43 
44  if (avpkt->size < XWD_HEADER_SIZE)
45  return AVERROR_INVALIDDATA;
46 
47  bytestream2_init(&gb, avpkt->data, avpkt->size);
48  header_size = bytestream2_get_be32u(&gb);
49 
50  version = bytestream2_get_be32u(&gb);
51  if (version != XWD_VERSION) {
52  av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
53  return AVERROR_INVALIDDATA;
54  }
55 
56  if (avpkt->size < header_size || header_size < XWD_HEADER_SIZE) {
57  av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
58  return AVERROR_INVALIDDATA;
59  }
60 
61  pixformat = bytestream2_get_be32u(&gb);
62  pixdepth = bytestream2_get_be32u(&gb);
63  width = bytestream2_get_be32u(&gb);
64  height = bytestream2_get_be32u(&gb);
65  xoffset = bytestream2_get_be32u(&gb);
66  be = bytestream2_get_be32u(&gb);
67  bunit = bytestream2_get_be32u(&gb);
68  bitorder = bytestream2_get_be32u(&gb);
69  bpad = bytestream2_get_be32u(&gb);
70  bpp = bytestream2_get_be32u(&gb);
71  lsize = bytestream2_get_be32u(&gb);
72  vclass = bytestream2_get_be32u(&gb);
73  rgb[0] = bytestream2_get_be32u(&gb);
74  rgb[1] = bytestream2_get_be32u(&gb);
75  rgb[2] = bytestream2_get_be32u(&gb);
76  bytestream2_skipu(&gb, 8);
77  ncolors = bytestream2_get_be32u(&gb);
78  bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20));
79 
80  if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
81  return ret;
82 
83  av_log(avctx, AV_LOG_DEBUG,
84  "pixformat %"PRIu32", pixdepth %"PRIu32", bunit %"PRIu32", bitorder %"PRIu32", bpad %"PRIu32"\n",
85  pixformat, pixdepth, bunit, bitorder, bpad);
86  av_log(avctx, AV_LOG_DEBUG,
87  "vclass %"PRIu32", ncolors %"PRIu32", bpp %"PRIu32", be %"PRIu32", lsize %"PRIu32", xoffset %"PRIu32"\n",
88  vclass, ncolors, bpp, be, lsize, xoffset);
89  av_log(avctx, AV_LOG_DEBUG,
90  "red %0"PRIx32", green %0"PRIx32", blue %0"PRIx32"\n",
91  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  avpriv_request_sample(avctx, "xoffset %"PRIu32"", 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 + (uint64_t)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  avpriv_report_missing_feature(avctx, "Pixmap format %"PRIu32, 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 && bpp != 8)
162  return AVERROR_INVALIDDATA;
163  if (bpp == 1 && pixdepth == 1) {
164  avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
165  } else if (bpp == 8 && pixdepth == 8) {
166  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
167  }
168  break;
169  case XWD_STATIC_COLOR:
170  case XWD_PSEUDO_COLOR:
171  if (bpp == 8)
172  avctx->pix_fmt = AV_PIX_FMT_PAL8;
173  break;
174  case XWD_TRUE_COLOR:
175  case XWD_DIRECT_COLOR:
176  if (bpp != 16 && bpp != 24 && bpp != 32)
177  return AVERROR_INVALIDDATA;
178  if (bpp == 16 && pixdepth == 15) {
179  if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
181  else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
183  } else if (bpp == 16 && pixdepth == 16) {
184  if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
186  else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
188  } else if (bpp == 24) {
189  if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
191  else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
193  } else if (bpp == 32) {
194  if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
196  else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
198  }
199  bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE);
200  break;
201  default:
202  av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
203  return AVERROR_INVALIDDATA;
204  }
205 
206  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
207  avpriv_request_sample(avctx,
208  "Unknown file: bpp %"PRIu32", pixdepth %"PRIu32", vclass %"PRIu32"",
209  bpp, pixdepth, vclass);
210  return AVERROR_PATCHWELCOME;
211  }
212 
213  if (avctx->skip_frame >= AVDISCARD_ALL)
214  return avpkt->size;
215 
216  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
217  return ret;
218 
219  p->key_frame = 1;
221 
222  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
223  uint32_t *dst = (uint32_t *)p->data[1];
224  uint8_t red, green, blue;
225 
226  for (int i = 0; i < ncolors; i++) {
227  bytestream2_skipu(&gb, 4); // skip colormap entry number
228  red = bytestream2_get_byteu(&gb);
229  bytestream2_skipu(&gb, 1);
230  green = bytestream2_get_byteu(&gb);
231  bytestream2_skipu(&gb, 1);
232  blue = bytestream2_get_byteu(&gb);
233  bytestream2_skipu(&gb, 3); // skip bitmask flag and padding
234 
235  dst[i] = 0xFFU << 24 | red << 16 | green << 8 | blue;
236  }
237  }
238 
239  ptr = p->data[0];
240  for (int i = 0; i < avctx->height; i++) {
241  bytestream2_get_bufferu(&gb, ptr, rsize);
242  bytestream2_skipu(&gb, lsize - rsize);
243  ptr += p->linesize[0];
244  }
245 
246  *got_frame = 1;
247 
248  return avpkt->size;
249 }
250 
252  .p.name = "xwd",
253  CODEC_LONG_NAME("XWD (X Window Dump) image"),
254  .p.type = AVMEDIA_TYPE_VIDEO,
255  .p.id = AV_CODEC_ID_XWD,
256  .p.capabilities = AV_CODEC_CAP_DR1,
257  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
259 };
be
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it be(in the first position) for now. Options ------- Then comes the options array. This is what will define the user accessible options. For example
XWD_GRAY_SCALE
#define XWD_GRAY_SCALE
Definition: xwd.h:35
GetByteContext
Definition: bytestream.h:33
bytestream2_skipu
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVPacket::data
uint8_t * data
Definition: packet.h:374
AV_PIX_FMT_MONOWHITE
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:75
FFCodec
Definition: codec_internal.h:119
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
ff_set_dimensions
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:91
XWD_Z_PIXMAP
#define XWD_Z_PIXMAP
Definition: xwd.h:32
AV_PIX_FMT_RGB555BE
@ AV_PIX_FMT_RGB555BE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition: pixfmt.h:107
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
rgb
Definition: rpzaenc.c:59
XWD_STATIC_COLOR
#define XWD_STATIC_COLOR
Definition: xwd.h:36
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1698
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:417
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
xwd.h
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:76
AV_PIX_FMT_RGB565LE
@ AV_PIX_FMT_RGB565LE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:106
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_XWD
@ AV_CODEC_ID_XWD
Definition: codec_id.h:210
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AV_PIX_FMT_BGR565LE
@ AV_PIX_FMT_BGR565LE
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition: pixfmt.h:111
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AV_PIX_FMT_BGR555BE
@ AV_PIX_FMT_BGR555BE
packed BGR 5:5:5, 16bpp, (msb)1X 5B 5G 5R(lsb), big-endian , X=unused/undefined
Definition: pixfmt.h:112
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:422
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1450
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
codec_internal.h
XWD_HEADER_SIZE
#define XWD_HEADER_SIZE
Definition: xwd.h:27
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: codec_internal.h:54
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
height
#define height
XWD_DIRECT_COLOR
#define XWD_DIRECT_COLOR
Definition: xwd.h:39
AV_PIX_FMT_BGR565BE
@ AV_PIX_FMT_BGR565BE
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition: pixfmt.h:110
version
version
Definition: libkvazaar.c:313
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
AV_PIX_FMT_RGB555LE
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:108
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
AVCodecContext::height
int height
Definition: avcodec.h:571
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:608
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
XWD_TRUE_COLOR
#define XWD_TRUE_COLOR
Definition: xwd.h:38
ret
ret
Definition: filter_design.txt:187
XWD_PSEUDO_COLOR
#define XWD_PSEUDO_COLOR
Definition: xwd.h:37
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:398
xwd_decode_frame
static int xwd_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: xwddec.c:32
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
XWD_STATIC_GRAY
#define XWD_STATIC_GRAY
Definition: xwd.h:34
AV_PIX_FMT_RGB565BE
@ AV_PIX_FMT_RGB565BE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:105
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
bytestream2_get_bufferu
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AV_PIX_FMT_BGR555LE
@ AV_PIX_FMT_BGR555LE
packed BGR 5:5:5, 16bpp, (msb)1X 5B 5G 5R(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:113
ff_xwd_decoder
const FFCodec ff_xwd_decoder
Definition: xwddec.c:251
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:351
XWD_VERSION
#define XWD_VERSION
Definition: xwd.h:26
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:571
bytestream.h
imgutils.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:370
XWD_CMAP_SIZE
#define XWD_CMAP_SIZE
Definition: xwd.h:28
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
av_image_check_size
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:318