FFmpeg
Loading...
Searching...
No Matches
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 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
24#include "libavutil/pixdesc.h"
25#include "avcodec.h"
26#include "bytestream.h"
27#include "codec_internal.h"
28#include "encode.h"
29#include "xwd.h"
30
31#define WINDOW_NAME "lavcxwdenc"
32#define WINDOW_NAME_SIZE 11
33
35 const AVFrame *p, int *got_packet)
36{
37 enum AVPixelFormat pix_fmt = avctx->pix_fmt;
39 uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
40 uint32_t rgb[3] = { 0 }, bitorder = 0;
41 uint32_t header_size;
42 int i, out_size, ret;
43 const uint8_t *ptr;
44 uint8_t *buf;
45 uint32_t pal[256];
46
47 pixdepth = av_get_bits_per_pixel(desc);
48 if (desc->flags & AV_PIX_FMT_FLAG_BE)
49 be = 1;
50 switch (pix_fmt) {
51 case AV_PIX_FMT_ARGB:
52 case AV_PIX_FMT_BGRA:
53 case AV_PIX_FMT_RGBA:
54 case AV_PIX_FMT_ABGR:
55 if (pix_fmt == AV_PIX_FMT_ARGB ||
57 be = 1;
58 if (pix_fmt == AV_PIX_FMT_ABGR ||
60 rgb[0] = 0xFF;
61 rgb[1] = 0xFF00;
62 rgb[2] = 0xFF0000;
63 } else {
64 rgb[0] = 0xFF0000;
65 rgb[1] = 0xFF00;
66 rgb[2] = 0xFF;
67 }
68 bpp = 32;
69 pixdepth = 24;
70 vclass = XWD_TRUE_COLOR;
71 bpad = 32;
72 break;
76 be = 1;
77 bpp = 24;
78 vclass = XWD_TRUE_COLOR;
79 bpad = 32;
80 rgb[0] = 0xFF0000;
81 rgb[1] = 0xFF00;
82 rgb[2] = 0xFF;
83 break;
90 rgb[0] = 0x1F;
91 rgb[1] = 0x7E0;
92 rgb[2] = 0xF800;
93 } else {
94 rgb[0] = 0xF800;
95 rgb[1] = 0x7E0;
96 rgb[2] = 0x1F;
97 }
98 bpp = 16;
99 vclass = XWD_TRUE_COLOR;
100 bpad = 16;
101 break;
108 rgb[0] = 0x1F;
109 rgb[1] = 0x3E0;
110 rgb[2] = 0x7C00;
111 } else {
112 rgb[0] = 0x7C00;
113 rgb[1] = 0x3E0;
114 rgb[2] = 0x1F;
115 }
116 bpp = 16;
117 vclass = XWD_TRUE_COLOR;
118 bpad = 16;
119 break;
120 case AV_PIX_FMT_RGB8:
121 case AV_PIX_FMT_BGR8:
124 case AV_PIX_FMT_PAL8:
125 bpp = 8;
126 vclass = XWD_PSEUDO_COLOR;
127 bpad = 8;
128 ncolors = 256;
129 break;
130 case AV_PIX_FMT_GRAY8:
131 bpp = 8;
132 bpad = 8;
133 vclass = XWD_STATIC_GRAY;
134 break;
136 be = 1;
137 bitorder = 1;
138 bpp = 1;
139 bpad = 8;
140 vclass = XWD_STATIC_GRAY;
141 break;
142 default:
143 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
144 return AVERROR(EINVAL);
145 }
146
147 lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
148 header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
149 out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
150
151 if ((ret = ff_get_encode_buffer(avctx, pkt, out_size, 0)) < 0)
152 return ret;
153 buf = pkt->data;
154
155 bytestream_put_be32(&buf, header_size);
156 bytestream_put_be32(&buf, XWD_VERSION); // file version
157 bytestream_put_be32(&buf, XWD_Z_PIXMAP); // pixmap format
158 bytestream_put_be32(&buf, pixdepth); // pixmap depth in pixels
159 bytestream_put_be32(&buf, avctx->width); // pixmap width in pixels
160 bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
161 bytestream_put_be32(&buf, 0); // bitmap x offset
162 bytestream_put_be32(&buf, be); // byte order
163 bytestream_put_be32(&buf, 32); // bitmap unit
164 bytestream_put_be32(&buf, bitorder); // bit-order of image data
165 bytestream_put_be32(&buf, bpad); // bitmap scan-line pad in bits
166 bytestream_put_be32(&buf, bpp); // bits per pixel
167 bytestream_put_be32(&buf, lsize); // bytes per scan-line
168 bytestream_put_be32(&buf, vclass); // visual class
169 bytestream_put_be32(&buf, rgb[0]); // red mask
170 bytestream_put_be32(&buf, rgb[1]); // green mask
171 bytestream_put_be32(&buf, rgb[2]); // blue mask
172 bytestream_put_be32(&buf, 8); // size of each bitmask in bits
173 bytestream_put_be32(&buf, ncolors); // number of colors
174 bytestream_put_be32(&buf, ncolors); // number of entries in color map
175 bytestream_put_be32(&buf, avctx->width); // window width
176 bytestream_put_be32(&buf, avctx->height); // window height
177 bytestream_put_be32(&buf, 0); // window upper left X coordinate
178 bytestream_put_be32(&buf, 0); // window upper left Y coordinate
179 bytestream_put_be32(&buf, 0); // window border width
181
182 if (pix_fmt == AV_PIX_FMT_PAL8) {
183 memcpy(pal, p->data[1], sizeof(pal));
184 } else {
186 }
187
188 for (i = 0; i < ncolors; i++) {
189 uint32_t val;
190 uint8_t red, green, blue;
191
192 val = pal[i];
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 *got_packet = 1;
212 return 0;
213}
214
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_xwd_encoder
Definition xwdenc.c:215
Libavcodec external API header.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition bytestream.h:372
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
static AVPacket * pkt
static enum AVPixelFormat pix_fmt
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_XWD
Definition codec_id.h:207
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition imgutils.c:178
const char * desc
Definition libsvtav1.c:83
#define FFALIGN(x, a)
Definition macros.h:78
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:3412
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition pixdesc.h:116
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_BGR565BE
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition pixfmt.h:117
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_RGB555BE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition pixfmt.h:114
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_RGB8
packed RGB 3:3:2, 8bpp, (msb)3R 3G 2B(lsb)
Definition pixfmt.h:93
@ AV_PIX_FMT_BGR8
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition pixfmt.h:90
@ AV_PIX_FMT_RGB4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition pixfmt.h:95
@ AV_PIX_FMT_BGR4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition pixfmt.h:92
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_RGB565LE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition pixfmt.h:113
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition pixfmt.h:115
@ AV_PIX_FMT_BGR555BE
packed BGR 5:5:5, 16bpp, (msb)1X 5B 5G 5R(lsb), big-endian , X=unused/undefined
Definition pixfmt.h:119
@ AV_PIX_FMT_RGB565BE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition pixfmt.h:112
@ AV_PIX_FMT_BGR555LE
packed BGR 5:5:5, 16bpp, (msb)1X 5B 5G 5R(lsb), little-endian, X=unused/undefined
Definition pixfmt.h:120
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_BGR565LE
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition pixfmt.h:118
@ 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:82
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Definition rpzaenc.c:60
#define av_log(a,...)
static int out_size
Definition movenc.c:56
#define XWD_VERSION
Definition xwd.h:26
#define XWD_Z_PIXMAP
Definition xwd.h:32
#define XWD_HEADER_SIZE
Definition xwd.h:27
#define XWD_STATIC_GRAY
Definition xwd.h:34
#define XWD_TRUE_COLOR
Definition xwd.h:38
#define XWD_PSEUDO_COLOR
Definition xwd.h:37
#define XWD_CMAP_SIZE
Definition xwd.h:28
#define WINDOW_NAME
Definition xwdenc.c:31
#define WINDOW_NAME_SIZE
Definition xwdenc.c:32
static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition xwdenc.c:34