Libav
xwdenc.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/intreadwrite.h"
24 #include "libavutil/pixdesc.h"
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "internal.h"
28 #include "xwd.h"
29 
30 #define WINDOW_NAME "lavcxwdenc"
31 #define WINDOW_NAME_SIZE 11
32 
34 {
35  avctx->coded_frame = av_frame_alloc();
36  if (!avctx->coded_frame)
37  return AVERROR(ENOMEM);
38 
39  return 0;
40 }
41 
42 static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
43  const AVFrame *p, int *got_packet)
44 {
45  enum AVPixelFormat pix_fmt = avctx->pix_fmt;
46  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
47  uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
48  uint32_t rgb[3] = { 0 }, bitorder = 0;
49  uint32_t header_size;
50  int i, out_size, ret;
51  uint8_t *ptr, *buf;
52 
53  pixdepth = av_get_bits_per_pixel(desc);
54  if (desc->flags & AV_PIX_FMT_FLAG_BE)
55  be = 1;
56  switch (pix_fmt) {
57  case AV_PIX_FMT_ARGB:
58  case AV_PIX_FMT_BGRA:
59  case AV_PIX_FMT_RGBA:
60  case AV_PIX_FMT_ABGR:
61  if (pix_fmt == AV_PIX_FMT_ARGB ||
62  pix_fmt == AV_PIX_FMT_ABGR)
63  be = 1;
64  if (pix_fmt == AV_PIX_FMT_ABGR ||
65  pix_fmt == AV_PIX_FMT_RGBA) {
66  rgb[0] = 0xFF;
67  rgb[1] = 0xFF00;
68  rgb[2] = 0xFF0000;
69  } else {
70  rgb[0] = 0xFF0000;
71  rgb[1] = 0xFF00;
72  rgb[2] = 0xFF;
73  }
74  bpp = 32;
75  pixdepth = 24;
76  vclass = XWD_TRUE_COLOR;
77  bpad = 32;
78  break;
79  case AV_PIX_FMT_BGR24:
80  case AV_PIX_FMT_RGB24:
81  if (pix_fmt == AV_PIX_FMT_RGB24)
82  be = 1;
83  bpp = 24;
84  vclass = XWD_TRUE_COLOR;
85  bpad = 32;
86  rgb[0] = 0xFF0000;
87  rgb[1] = 0xFF00;
88  rgb[2] = 0xFF;
89  break;
94  if (pix_fmt == AV_PIX_FMT_BGR565LE ||
95  pix_fmt == AV_PIX_FMT_BGR565BE) {
96  rgb[0] = 0x1F;
97  rgb[1] = 0x7E0;
98  rgb[2] = 0xF800;
99  } else {
100  rgb[0] = 0xF800;
101  rgb[1] = 0x7E0;
102  rgb[2] = 0x1F;
103  }
104  bpp = 16;
105  vclass = XWD_TRUE_COLOR;
106  bpad = 16;
107  break;
108  case AV_PIX_FMT_RGB555LE:
109  case AV_PIX_FMT_RGB555BE:
110  case AV_PIX_FMT_BGR555LE:
111  case AV_PIX_FMT_BGR555BE:
112  if (pix_fmt == AV_PIX_FMT_BGR555LE ||
113  pix_fmt == AV_PIX_FMT_BGR555BE) {
114  rgb[0] = 0x1F;
115  rgb[1] = 0x3E0;
116  rgb[2] = 0x7C00;
117  } else {
118  rgb[0] = 0x7C00;
119  rgb[1] = 0x3E0;
120  rgb[2] = 0x1F;
121  }
122  bpp = 16;
123  vclass = XWD_TRUE_COLOR;
124  bpad = 16;
125  break;
126  case AV_PIX_FMT_RGB8:
127  case AV_PIX_FMT_BGR8:
130  case AV_PIX_FMT_PAL8:
131  bpp = 8;
132  vclass = XWD_PSEUDO_COLOR;
133  bpad = 8;
134  ncolors = 256;
135  break;
137  be = 1;
138  bitorder = 1;
139  bpp = 1;
140  bpad = 8;
141  vclass = XWD_STATIC_GRAY;
142  break;
143  default:
144  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
145  return AVERROR(EINVAL);
146  }
147 
148  lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
149  header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
150  out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
151 
152  if ((ret = ff_alloc_packet(pkt, out_size)) < 0) {
153  av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
154  return ret;
155  }
156  buf = pkt->data;
157 
158  avctx->coded_frame->key_frame = 1;
160 
161  bytestream_put_be32(&buf, header_size);
162  bytestream_put_be32(&buf, XWD_VERSION); // file version
163  bytestream_put_be32(&buf, XWD_Z_PIXMAP); // pixmap format
164  bytestream_put_be32(&buf, pixdepth); // pixmap depth in pixels
165  bytestream_put_be32(&buf, avctx->width); // pixmap width in pixels
166  bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
167  bytestream_put_be32(&buf, 0); // bitmap x offset
168  bytestream_put_be32(&buf, be); // byte order
169  bytestream_put_be32(&buf, 32); // bitmap unit
170  bytestream_put_be32(&buf, bitorder); // bit-order of image data
171  bytestream_put_be32(&buf, bpad); // bitmap scan-line pad in bits
172  bytestream_put_be32(&buf, bpp); // bits per pixel
173  bytestream_put_be32(&buf, lsize); // bytes per scan-line
174  bytestream_put_be32(&buf, vclass); // visual class
175  bytestream_put_be32(&buf, rgb[0]); // red mask
176  bytestream_put_be32(&buf, rgb[1]); // green mask
177  bytestream_put_be32(&buf, rgb[2]); // blue mask
178  bytestream_put_be32(&buf, 8); // size of each bitmask in bits
179  bytestream_put_be32(&buf, ncolors); // number of colors
180  bytestream_put_be32(&buf, ncolors); // number of entries in color map
181  bytestream_put_be32(&buf, avctx->width); // window width
182  bytestream_put_be32(&buf, avctx->height); // window height
183  bytestream_put_be32(&buf, 0); // window upper left X coordinate
184  bytestream_put_be32(&buf, 0); // window upper left Y coordinate
185  bytestream_put_be32(&buf, 0); // window border width
186  bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
187 
188  for (i = 0; i < ncolors; i++) {
189  uint32_t val;
190  uint8_t red, green, blue;
191 
192  val = AV_RN32A(p->data[1] + i * 4);
193  red = (val >> 16) & 0xFF;
194  green = (val >> 8) & 0xFF;
195  blue = val & 0xFF;
196 
197  bytestream_put_be32(&buf, i); // colormap entry number
198  bytestream_put_be16(&buf, red << 8);
199  bytestream_put_be16(&buf, green << 8);
200  bytestream_put_be16(&buf, blue << 8);
201  bytestream_put_byte(&buf, 0x7); // bitmask flag
202  bytestream_put_byte(&buf, 0); // padding
203  }
204 
205  ptr = p->data[0];
206  for (i = 0; i < avctx->height; i++) {
207  bytestream_put_buffer(&buf, ptr, lsize);
208  ptr += p->linesize[0];
209  }
210 
211  pkt->flags |= AV_PKT_FLAG_KEY;
212  *got_packet = 1;
213  return 0;
214 }
215 
217 {
218  av_freep(&avctx->coded_frame);
219 
220  return 0;
221 }
222 
224  .name = "xwd",
225  .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
226  .type = AVMEDIA_TYPE_VIDEO,
227  .id = AV_CODEC_ID_XWD,
228  .init = xwd_encode_init,
229  .encode2 = xwd_encode_frame,
230  .close = xwd_encode_close,
231  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA,
251  AV_PIX_FMT_NONE },
252 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1507
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2506
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:1480
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:118
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition: pixfmt.h:121
AVCodec.
Definition: avcodec.h:2755
static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: xwdenc.c:42
#define FFALIGN(x, a)
Definition: common.h:62
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:198
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:116
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:88
#define AV_RN32A(p)
Definition: intreadwrite.h:446
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define XWD_STATIC_GRAY
Definition: xwd.h:34
AVCodec ff_xwd_encoder
Definition: xwdenc.c:223
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:115
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:97
uint8_t * data
Definition: avcodec.h:973
#define XWD_HEADER_SIZE
Definition: xwd.h:27
#define XWD_CMAP_SIZE
Definition: xwd.h:28
#define XWD_VERSION
Definition: xwd.h:26
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
static av_cold int xwd_encode_init(AVCodecContext *avctx)
Definition: xwdenc.c:33
#define XWD_Z_PIXMAP
Definition: xwd.h:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
#define WINDOW_NAME_SIZE
Definition: xwdenc.c:31
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:98
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:95
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:96
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:168
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:91
int width
picture width / height.
Definition: avcodec.h:1217
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1125
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
enum AVPixelFormat pix_fmt
Definition: movenc.c:821
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition: pixfmt.h:120
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:86
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
uint8_t flags
Definition: pixdesc.h:78
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:57
main external API structure.
Definition: avcodec.h:1054
#define WINDOW_NAME
Definition: xwdenc.c:30
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), little-endian, most significant bit to 1 ...
Definition: pixfmt.h:123
static av_cold int xwd_encode_close(AVCodecContext *avctx)
Definition: xwdenc.c:216
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:117
#define XWD_TRUE_COLOR
Definition: xwd.h:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
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
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:89
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition: pixdesc.h:91
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:327
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:163
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), big-endian, most significant bit to 1 ...
Definition: pixfmt.h:122
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950