4 * Copyright (c) 2012 Paul B Mahol
6 * This file is part of Libav.
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.
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.
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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/pixdesc.h"
26 #include "bytestream.h"
29 #define WINDOW_NAME "lavcxwdenc"
30 #define WINDOW_NAME_SIZE 11
32 static av_cold
int xwd_encode_init(AVCodecContext
*avctx
)
34 avctx
->coded_frame
= avcodec_alloc_frame();
35 if (!avctx
->coded_frame
)
36 return AVERROR(ENOMEM
);
41 static int xwd_encode_frame(AVCodecContext
*avctx
, uint8_t *buf
,
42 int buf_size
, void *data
)
45 enum PixelFormat pix_fmt
= avctx
->pix_fmt
;
46 uint32_t pixdepth
, bpp
, bpad
, ncolors
= 0, lsize
, vclass
, be
= 0;
47 uint32_t rgb
[3] = { 0 };
52 pixdepth
= av_get_bits_per_pixel(&av_pix_fmt_descriptors
[pix_fmt
]);
53 if (av_pix_fmt_descriptors
[pix_fmt
].flags
& PIX_FMT_BE
)
60 if (pix_fmt
== PIX_FMT_ARGB
||
61 pix_fmt
== PIX_FMT_ABGR
)
63 if (pix_fmt
== PIX_FMT_ABGR
||
64 pix_fmt
== PIX_FMT_RGBA
) {
75 vclass
= XWD_TRUE_COLOR
;
80 if (pix_fmt
== PIX_FMT_RGB24
)
83 vclass
= XWD_TRUE_COLOR
;
89 case PIX_FMT_RGB565LE
:
90 case PIX_FMT_RGB565BE
:
91 case PIX_FMT_BGR565LE
:
92 case PIX_FMT_BGR565BE
:
93 if (pix_fmt
== PIX_FMT_BGR565LE
||
94 pix_fmt
== PIX_FMT_BGR565BE
) {
104 vclass
= XWD_TRUE_COLOR
;
107 case PIX_FMT_RGB555LE
:
108 case PIX_FMT_RGB555BE
:
109 case PIX_FMT_BGR555LE
:
110 case PIX_FMT_BGR555BE
:
111 if (pix_fmt
== PIX_FMT_BGR555LE
||
112 pix_fmt
== PIX_FMT_BGR555BE
) {
122 vclass
= XWD_TRUE_COLOR
;
127 case PIX_FMT_RGB4_BYTE
:
128 case PIX_FMT_BGR4_BYTE
:
131 vclass
= XWD_PSEUDO_COLOR
;
135 case PIX_FMT_MONOWHITE
:
138 vclass
= XWD_STATIC_GRAY
;
141 av_log(avctx
, AV_LOG_INFO
, "unsupported pixel format\n");
142 return AVERROR(EINVAL
);
145 lsize
= FFALIGN(bpp
* avctx
->width
, bpad
) / 8;
146 header_size
= XWD_HEADER_SIZE
+ WINDOW_NAME_SIZE
;
147 out_size
= header_size
+ ncolors
* XWD_CMAP_SIZE
+ avctx
->height
* lsize
;
149 if (buf_size
< out_size
) {
150 av_log(avctx
, AV_LOG_ERROR
, "output buffer too small\n");
151 return AVERROR(ENOMEM
);
154 avctx
->coded_frame
->key_frame
= 1;
155 avctx
->coded_frame
->pict_type
= AV_PICTURE_TYPE_I
;
157 bytestream_put_be32(&buf
, header_size
);
158 bytestream_put_be32(&buf
, XWD_VERSION
); // file version
159 bytestream_put_be32(&buf
, XWD_Z_PIXMAP
); // pixmap format
160 bytestream_put_be32(&buf
, pixdepth
); // pixmap depth in pixels
161 bytestream_put_be32(&buf
, avctx
->width
); // pixmap width in pixels
162 bytestream_put_be32(&buf
, avctx
->height
); // pixmap height in pixels
163 bytestream_put_be32(&buf
, 0); // bitmap x offset
164 bytestream_put_be32(&buf
, be
); // byte order
165 bytestream_put_be32(&buf
, 32); // bitmap unit
166 bytestream_put_be32(&buf
, be
); // bit-order of image data
167 bytestream_put_be32(&buf
, bpad
); // bitmap scan-line pad in bits
168 bytestream_put_be32(&buf
, bpp
); // bits per pixel
169 bytestream_put_be32(&buf
, lsize
); // bytes per scan-line
170 bytestream_put_be32(&buf
, vclass
); // visual class
171 bytestream_put_be32(&buf
, rgb
[0]); // red mask
172 bytestream_put_be32(&buf
, rgb
[1]); // green mask
173 bytestream_put_be32(&buf
, rgb
[2]); // blue mask
174 bytestream_put_be32(&buf
, 8); // size of each bitmask in bits
175 bytestream_put_be32(&buf
, ncolors
); // number of colors
176 bytestream_put_be32(&buf
, ncolors
); // number of entries in color map
177 bytestream_put_be32(&buf
, avctx
->width
); // window width
178 bytestream_put_be32(&buf
, avctx
->height
); // window height
179 bytestream_put_be32(&buf
, 0); // window upper left X coordinate
180 bytestream_put_be32(&buf
, 0); // window upper left Y coordinate
181 bytestream_put_be32(&buf
, 0); // window border width
182 bytestream_put_buffer(&buf
, WINDOW_NAME
, WINDOW_NAME_SIZE
);
184 for (i
= 0; i
< ncolors
; i
++) {
186 uint8_t red
, green
, blue
;
188 val
= AV_RN32A(p
->data
[1] + i
* 4);
189 red
= (val
>> 16) & 0xFF;
190 green
= (val
>> 8) & 0xFF;
193 bytestream_put_be32(&buf
, i
); // colormap entry number
194 bytestream_put_be16(&buf
, red
<< 8);
195 bytestream_put_be16(&buf
, green
<< 8);
196 bytestream_put_be16(&buf
, blue
<< 8);
197 bytestream_put_byte(&buf
, 0x7); // bitmask flag
198 bytestream_put_byte(&buf
, 0); // padding
202 for (i
= 0; i
< avctx
->height
; i
++) {
203 bytestream_put_buffer(&buf
, ptr
, lsize
);
204 ptr
+= p
->linesize
[0];
210 static av_cold
int xwd_encode_close(AVCodecContext
*avctx
)
212 av_freep(&avctx
->coded_frame
);
217 AVCodec ff_xwd_encoder
= {
219 .type
= AVMEDIA_TYPE_VIDEO
,
221 .init
= xwd_encode_init
,
222 .encode
= xwd_encode_frame
,
223 .close
= xwd_encode_close
,
224 .pix_fmts
= (const enum PixelFormat
[]) { PIX_FMT_BGRA
,
245 .long_name
= NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),