]> git.sesse.net Git - ffmpeg/blob - libavcodec/xwdenc.c
xwdenc: switch to encode2().
[ffmpeg] / libavcodec / xwdenc.c
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 av_cold int xwd_encode_init(AVCodecContext *avctx)
34 {
35     avctx->coded_frame = avcodec_alloc_frame();
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 PixelFormat pix_fmt = avctx->pix_fmt;
46     uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
47     uint32_t rgb[3] = { 0 };
48     uint32_t header_size;
49     int i, out_size, ret;
50     uint8_t *ptr, *buf;
51
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)
54         be = 1;
55     switch (pix_fmt) {
56     case PIX_FMT_ARGB:
57     case PIX_FMT_BGRA:
58     case PIX_FMT_RGBA:
59     case PIX_FMT_ABGR:
60         if (pix_fmt == PIX_FMT_ARGB ||
61             pix_fmt == PIX_FMT_ABGR)
62             be = 1;
63         if (pix_fmt == PIX_FMT_ABGR ||
64             pix_fmt == PIX_FMT_RGBA) {
65             rgb[0] = 0xFF;
66             rgb[1] = 0xFF00;
67             rgb[2] = 0xFF0000;
68         } else {
69             rgb[0] = 0xFF0000;
70             rgb[1] = 0xFF00;
71             rgb[2] = 0xFF;
72         }
73         bpp      = 32;
74         pixdepth = 24;
75         vclass   = XWD_TRUE_COLOR;
76         bpad     = 32;
77         break;
78     case PIX_FMT_BGR24:
79     case PIX_FMT_RGB24:
80         if (pix_fmt == PIX_FMT_RGB24)
81             be = 1;
82         bpp      = 24;
83         vclass   = XWD_TRUE_COLOR;
84         bpad     = 32;
85         rgb[0]   = 0xFF0000;
86         rgb[1]   = 0xFF00;
87         rgb[2]   = 0xFF;
88         break;
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) {
95             rgb[0] = 0x1F;
96             rgb[1] = 0x7E0;
97             rgb[2] = 0xF800;
98         } else {
99             rgb[0] = 0xF800;
100             rgb[1] = 0x7E0;
101             rgb[2] = 0x1F;
102         }
103         bpp      = 16;
104         vclass   = XWD_TRUE_COLOR;
105         bpad     = 16;
106         break;
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) {
113             rgb[0] = 0x1F;
114             rgb[1] = 0x3E0;
115             rgb[2] = 0x7C00;
116         } else {
117             rgb[0] = 0x7C00;
118             rgb[1] = 0x3E0;
119             rgb[2] = 0x1F;
120         }
121         bpp      = 16;
122         vclass   = XWD_TRUE_COLOR;
123         bpad     = 16;
124         break;
125     case PIX_FMT_RGB8:
126     case PIX_FMT_BGR8:
127     case PIX_FMT_RGB4_BYTE:
128     case PIX_FMT_BGR4_BYTE:
129     case PIX_FMT_PAL8:
130         bpp      = 8;
131         vclass   = XWD_PSEUDO_COLOR;
132         bpad     = 8;
133         ncolors  = 256;
134         break;
135     case PIX_FMT_MONOWHITE:
136         bpp      = 1;
137         bpad     = 8;
138         vclass   = XWD_STATIC_GRAY;
139         break;
140     default:
141         av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
142         return AVERROR(EINVAL);
143     }
144
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;
148
149     if ((ret = ff_alloc_packet(pkt, out_size)) < 0) {
150         av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
151         return ret;
152     }
153     buf = pkt->data;
154
155     avctx->coded_frame->key_frame = 1;
156     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
157
158     bytestream_put_be32(&buf, header_size);
159     bytestream_put_be32(&buf, XWD_VERSION);   // file version
160     bytestream_put_be32(&buf, XWD_Z_PIXMAP);  // pixmap format
161     bytestream_put_be32(&buf, pixdepth);      // pixmap depth in pixels
162     bytestream_put_be32(&buf, avctx->width);  // pixmap width in pixels
163     bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
164     bytestream_put_be32(&buf, 0);             // bitmap x offset
165     bytestream_put_be32(&buf, be);            // byte order
166     bytestream_put_be32(&buf, 32);            // bitmap unit
167     bytestream_put_be32(&buf, be);            // bit-order of image data
168     bytestream_put_be32(&buf, bpad);          // bitmap scan-line pad in bits
169     bytestream_put_be32(&buf, bpp);           // bits per pixel
170     bytestream_put_be32(&buf, lsize);         // bytes per scan-line
171     bytestream_put_be32(&buf, vclass);        // visual class
172     bytestream_put_be32(&buf, rgb[0]);        // red mask
173     bytestream_put_be32(&buf, rgb[1]);        // green mask
174     bytestream_put_be32(&buf, rgb[2]);        // blue mask
175     bytestream_put_be32(&buf, 8);             // size of each bitmask in bits
176     bytestream_put_be32(&buf, ncolors);       // number of colors
177     bytestream_put_be32(&buf, ncolors);       // number of entries in color map
178     bytestream_put_be32(&buf, avctx->width);  // window width
179     bytestream_put_be32(&buf, avctx->height); // window height
180     bytestream_put_be32(&buf, 0);             // window upper left X coordinate
181     bytestream_put_be32(&buf, 0);             // window upper left Y coordinate
182     bytestream_put_be32(&buf, 0);             // window border width
183     bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
184
185     for (i = 0; i < ncolors; i++) {
186         uint32_t val;
187         uint8_t red, green, blue;
188
189         val   = AV_RN32A(p->data[1] + i * 4);
190         red   = (val >> 16) & 0xFF;
191         green = (val >>  8) & 0xFF;
192         blue  =  val        & 0xFF;
193
194         bytestream_put_be32(&buf, i);         // colormap entry number
195         bytestream_put_be16(&buf, red   << 8);
196         bytestream_put_be16(&buf, green << 8);
197         bytestream_put_be16(&buf, blue  << 8);
198         bytestream_put_byte(&buf, 0x7);       // bitmask flag
199         bytestream_put_byte(&buf, 0);         // padding
200     }
201
202     ptr = p->data[0];
203     for (i = 0; i < avctx->height; i++) {
204         bytestream_put_buffer(&buf, ptr, lsize);
205         ptr += p->linesize[0];
206     }
207
208     pkt->flags |= AV_PKT_FLAG_KEY;
209     *got_packet = 1;
210     return 0;
211 }
212
213 static av_cold int xwd_encode_close(AVCodecContext *avctx)
214 {
215     av_freep(&avctx->coded_frame);
216
217     return 0;
218 }
219
220 AVCodec ff_xwd_encoder = {
221     .name         = "xwd",
222     .type         = AVMEDIA_TYPE_VIDEO,
223     .id           = CODEC_ID_XWD,
224     .init         = xwd_encode_init,
225     .encode2      = xwd_encode_frame,
226     .close        = xwd_encode_close,
227     .pix_fmts     = (const enum PixelFormat[]) { PIX_FMT_BGRA,
228                                                  PIX_FMT_RGBA,
229                                                  PIX_FMT_ARGB,
230                                                  PIX_FMT_ABGR,
231                                                  PIX_FMT_RGB24,
232                                                  PIX_FMT_BGR24,
233                                                  PIX_FMT_RGB565BE,
234                                                  PIX_FMT_RGB565LE,
235                                                  PIX_FMT_BGR565BE,
236                                                  PIX_FMT_BGR565LE,
237                                                  PIX_FMT_RGB555BE,
238                                                  PIX_FMT_RGB555LE,
239                                                  PIX_FMT_BGR555BE,
240                                                  PIX_FMT_BGR555LE,
241                                                  PIX_FMT_RGB8,
242                                                  PIX_FMT_BGR8,
243                                                  PIX_FMT_RGB4_BYTE,
244                                                  PIX_FMT_BGR4_BYTE,
245                                                  PIX_FMT_PAL8,
246                                                  PIX_FMT_MONOWHITE,
247                                                  PIX_FMT_NONE },
248     .long_name    = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
249 };