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 
33 static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
34  const AVFrame *p, int *got_packet)
35 {
36  enum AVPixelFormat pix_fmt = avctx->pix_fmt;
38  uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
39  uint32_t rgb[3] = { 0 }, bitorder = 0;
40  uint32_t header_size;
41  int i, out_size, ret;
42  uint8_t *ptr, *buf;
43 
44  pixdepth = av_get_bits_per_pixel(desc);
45  if (desc->flags & AV_PIX_FMT_FLAG_BE)
46  be = 1;
47  switch (pix_fmt) {
48  case AV_PIX_FMT_ARGB:
49  case AV_PIX_FMT_BGRA:
50  case AV_PIX_FMT_RGBA:
51  case AV_PIX_FMT_ABGR:
52  if (pix_fmt == AV_PIX_FMT_ARGB ||
53  pix_fmt == AV_PIX_FMT_ABGR)
54  be = 1;
55  if (pix_fmt == AV_PIX_FMT_ABGR ||
56  pix_fmt == AV_PIX_FMT_RGBA) {
57  rgb[0] = 0xFF;
58  rgb[1] = 0xFF00;
59  rgb[2] = 0xFF0000;
60  } else {
61  rgb[0] = 0xFF0000;
62  rgb[1] = 0xFF00;
63  rgb[2] = 0xFF;
64  }
65  bpp = 32;
66  pixdepth = 24;
67  vclass = XWD_TRUE_COLOR;
68  bpad = 32;
69  break;
70  case AV_PIX_FMT_BGR24:
71  case AV_PIX_FMT_RGB24:
72  if (pix_fmt == AV_PIX_FMT_RGB24)
73  be = 1;
74  bpp = 24;
75  vclass = XWD_TRUE_COLOR;
76  bpad = 32;
77  rgb[0] = 0xFF0000;
78  rgb[1] = 0xFF00;
79  rgb[2] = 0xFF;
80  break;
85  if (pix_fmt == AV_PIX_FMT_BGR565LE ||
86  pix_fmt == AV_PIX_FMT_BGR565BE) {
87  rgb[0] = 0x1F;
88  rgb[1] = 0x7E0;
89  rgb[2] = 0xF800;
90  } else {
91  rgb[0] = 0xF800;
92  rgb[1] = 0x7E0;
93  rgb[2] = 0x1F;
94  }
95  bpp = 16;
96  vclass = XWD_TRUE_COLOR;
97  bpad = 16;
98  break;
100  case AV_PIX_FMT_RGB555BE:
101  case AV_PIX_FMT_BGR555LE:
102  case AV_PIX_FMT_BGR555BE:
103  if (pix_fmt == AV_PIX_FMT_BGR555LE ||
104  pix_fmt == AV_PIX_FMT_BGR555BE) {
105  rgb[0] = 0x1F;
106  rgb[1] = 0x3E0;
107  rgb[2] = 0x7C00;
108  } else {
109  rgb[0] = 0x7C00;
110  rgb[1] = 0x3E0;
111  rgb[2] = 0x1F;
112  }
113  bpp = 16;
114  vclass = XWD_TRUE_COLOR;
115  bpad = 16;
116  break;
117  case AV_PIX_FMT_RGB8:
118  case AV_PIX_FMT_BGR8:
121  case AV_PIX_FMT_PAL8:
122  bpp = 8;
123  vclass = XWD_PSEUDO_COLOR;
124  bpad = 8;
125  ncolors = 256;
126  break;
128  be = 1;
129  bitorder = 1;
130  bpp = 1;
131  bpad = 8;
132  vclass = XWD_STATIC_GRAY;
133  break;
134  default:
135  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
136  return AVERROR(EINVAL);
137  }
138 
139  lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
140  header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
141  out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
142 
143  if ((ret = ff_alloc_packet(pkt, out_size)) < 0) {
144  av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
145  return ret;
146  }
147  buf = pkt->data;
148 
149 #if FF_API_CODED_FRAME
151  avctx->coded_frame->key_frame = 1;
154 #endif
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, bitorder); // 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  pkt->flags |= AV_PKT_FLAG_KEY;
207  *got_packet = 1;
208  return 0;
209 }
210 
212  .name = "xwd",
213  .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
214  .type = AVMEDIA_TYPE_VIDEO,
215  .id = AV_CODEC_ID_XWD,
216  .encode2 = xwd_encode_frame,
217  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA,
237  AV_PIX_FMT_NONE },
238 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:61
const char * desc
Definition: nvenc.c:101
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:1740
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:112
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition: pixfmt.h:115
int out_size
Definition: movenc.c:55
AVCodec.
Definition: avcodec.h:3120
static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: xwdenc.c:33
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:110
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:82
#define AV_RN32A(p)
Definition: intreadwrite.h:457
uint8_t
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:70
#define XWD_STATIC_GRAY
Definition: xwd.h:34
AVCodec ff_xwd_encoder
Definition: xwdenc.c:211
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:109
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:91
uint8_t * data
Definition: avcodec.h:1346
#define XWD_HEADER_SIZE
Definition: xwd.h:27
#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_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
#define XWD_Z_PIXMAP
Definition: xwd.h:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#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:148
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:92
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:89
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:90
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:105
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:85
int width
picture width / height.
Definition: avcodec.h:1580
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1211
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:62
enum AVPixelFormat pix_fmt
Definition: movenc.c:853
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition: pixfmt.h:114
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:80
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:80
main external API structure.
Definition: avcodec.h:1409
#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:117
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:111
#define XWD_TRUE_COLOR
Definition: xwd.h:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
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:68
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2797
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:83
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition: pixdesc.h:130
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:365
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), big-endian, most significant bit to 1 ...
Definition: pixfmt.h:116
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323