Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
23 | #include "libavutil/imgutils_internal.h" | ||
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 | |||
34 | 129 | static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
35 | const AVFrame *p, int *got_packet) | ||
36 | { | ||
37 | 129 | enum AVPixelFormat pix_fmt = avctx->pix_fmt; | |
38 | 129 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
39 | 129 | uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0; | |
40 | 129 | 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 | 129 | pixdepth = av_get_bits_per_pixel(desc); | |
48 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 103 times.
|
129 | if (desc->flags & AV_PIX_FMT_FLAG_BE) |
49 | 26 | be = 1; | |
50 |
7/8✓ Branch 0 taken 13 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 26 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
129 | switch (pix_fmt) { |
51 | 13 | case AV_PIX_FMT_ARGB: | |
52 | case AV_PIX_FMT_BGRA: | ||
53 | case AV_PIX_FMT_RGBA: | ||
54 | case AV_PIX_FMT_ABGR: | ||
55 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
|
13 | if (pix_fmt == AV_PIX_FMT_ARGB || |
56 | pix_fmt == AV_PIX_FMT_ABGR) | ||
57 | ✗ | be = 1; | |
58 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | if (pix_fmt == AV_PIX_FMT_ABGR || |
59 | pix_fmt == AV_PIX_FMT_RGBA) { | ||
60 | 13 | rgb[0] = 0xFF; | |
61 | 13 | rgb[1] = 0xFF00; | |
62 | 13 | rgb[2] = 0xFF0000; | |
63 | } else { | ||
64 | ✗ | rgb[0] = 0xFF0000; | |
65 | ✗ | rgb[1] = 0xFF00; | |
66 | ✗ | rgb[2] = 0xFF; | |
67 | } | ||
68 | 13 | bpp = 32; | |
69 | 13 | pixdepth = 24; | |
70 | 13 | vclass = XWD_TRUE_COLOR; | |
71 | 13 | bpad = 32; | |
72 | 13 | break; | |
73 | 38 | case AV_PIX_FMT_BGR24: | |
74 | case AV_PIX_FMT_RGB24: | ||
75 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | if (pix_fmt == AV_PIX_FMT_RGB24) |
76 | 38 | be = 1; | |
77 | 38 | bpp = 24; | |
78 | 38 | vclass = XWD_TRUE_COLOR; | |
79 | 38 | bpad = 32; | |
80 | 38 | rgb[0] = 0xFF0000; | |
81 | 38 | rgb[1] = 0xFF00; | |
82 | 38 | rgb[2] = 0xFF; | |
83 | 38 | break; | |
84 | 13 | case AV_PIX_FMT_RGB565LE: | |
85 | case AV_PIX_FMT_RGB565BE: | ||
86 | case AV_PIX_FMT_BGR565LE: | ||
87 | case AV_PIX_FMT_BGR565BE: | ||
88 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
|
13 | if (pix_fmt == AV_PIX_FMT_BGR565LE || |
89 | pix_fmt == AV_PIX_FMT_BGR565BE) { | ||
90 | ✗ | rgb[0] = 0x1F; | |
91 | ✗ | rgb[1] = 0x7E0; | |
92 | ✗ | rgb[2] = 0xF800; | |
93 | } else { | ||
94 | 13 | rgb[0] = 0xF800; | |
95 | 13 | rgb[1] = 0x7E0; | |
96 | 13 | rgb[2] = 0x1F; | |
97 | } | ||
98 | 13 | bpp = 16; | |
99 | 13 | vclass = XWD_TRUE_COLOR; | |
100 | 13 | bpad = 16; | |
101 | 13 | break; | |
102 | 13 | case AV_PIX_FMT_RGB555LE: | |
103 | case AV_PIX_FMT_RGB555BE: | ||
104 | case AV_PIX_FMT_BGR555LE: | ||
105 | case AV_PIX_FMT_BGR555BE: | ||
106 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
|
13 | if (pix_fmt == AV_PIX_FMT_BGR555LE || |
107 | pix_fmt == AV_PIX_FMT_BGR555BE) { | ||
108 | ✗ | rgb[0] = 0x1F; | |
109 | ✗ | rgb[1] = 0x3E0; | |
110 | ✗ | rgb[2] = 0x7C00; | |
111 | } else { | ||
112 | 13 | rgb[0] = 0x7C00; | |
113 | 13 | rgb[1] = 0x3E0; | |
114 | 13 | rgb[2] = 0x1F; | |
115 | } | ||
116 | 13 | bpp = 16; | |
117 | 13 | vclass = XWD_TRUE_COLOR; | |
118 | 13 | bpad = 16; | |
119 | 13 | break; | |
120 | 26 | case AV_PIX_FMT_RGB8: | |
121 | case AV_PIX_FMT_BGR8: | ||
122 | case AV_PIX_FMT_RGB4_BYTE: | ||
123 | case AV_PIX_FMT_BGR4_BYTE: | ||
124 | case AV_PIX_FMT_PAL8: | ||
125 | 26 | bpp = 8; | |
126 | 26 | vclass = XWD_PSEUDO_COLOR; | |
127 | 26 | bpad = 8; | |
128 | 26 | ncolors = 256; | |
129 | 26 | break; | |
130 | 13 | case AV_PIX_FMT_GRAY8: | |
131 | 13 | bpp = 8; | |
132 | 13 | bpad = 8; | |
133 | 13 | vclass = XWD_STATIC_GRAY; | |
134 | 13 | break; | |
135 | 13 | case AV_PIX_FMT_MONOWHITE: | |
136 | 13 | be = 1; | |
137 | 13 | bitorder = 1; | |
138 | 13 | bpp = 1; | |
139 | 13 | bpad = 8; | |
140 | 13 | vclass = XWD_STATIC_GRAY; | |
141 | 13 | break; | |
142 | ✗ | default: | |
143 | ✗ | av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); | |
144 | ✗ | return AVERROR(EINVAL); | |
145 | } | ||
146 | |||
147 | 129 | lsize = FFALIGN(bpp * avctx->width, bpad) / 8; | |
148 | 129 | header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE; | |
149 | 129 | out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize; | |
150 | |||
151 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
|
129 | if ((ret = ff_get_encode_buffer(avctx, pkt, out_size, 0)) < 0) |
152 | ✗ | return ret; | |
153 | 129 | buf = pkt->data; | |
154 | |||
155 | 129 | bytestream_put_be32(&buf, header_size); | |
156 | 129 | bytestream_put_be32(&buf, XWD_VERSION); // file version | |
157 | 129 | bytestream_put_be32(&buf, XWD_Z_PIXMAP); // pixmap format | |
158 | 129 | bytestream_put_be32(&buf, pixdepth); // pixmap depth in pixels | |
159 | 129 | bytestream_put_be32(&buf, avctx->width); // pixmap width in pixels | |
160 | 129 | bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels | |
161 | 129 | bytestream_put_be32(&buf, 0); // bitmap x offset | |
162 | 129 | bytestream_put_be32(&buf, be); // byte order | |
163 | 129 | bytestream_put_be32(&buf, 32); // bitmap unit | |
164 | 129 | bytestream_put_be32(&buf, bitorder); // bit-order of image data | |
165 | 129 | bytestream_put_be32(&buf, bpad); // bitmap scan-line pad in bits | |
166 | 129 | bytestream_put_be32(&buf, bpp); // bits per pixel | |
167 | 129 | bytestream_put_be32(&buf, lsize); // bytes per scan-line | |
168 | 129 | bytestream_put_be32(&buf, vclass); // visual class | |
169 | 129 | bytestream_put_be32(&buf, rgb[0]); // red mask | |
170 | 129 | bytestream_put_be32(&buf, rgb[1]); // green mask | |
171 | 129 | bytestream_put_be32(&buf, rgb[2]); // blue mask | |
172 | 129 | bytestream_put_be32(&buf, 8); // size of each bitmask in bits | |
173 | 129 | bytestream_put_be32(&buf, ncolors); // number of colors | |
174 | 129 | bytestream_put_be32(&buf, ncolors); // number of entries in color map | |
175 | 129 | bytestream_put_be32(&buf, avctx->width); // window width | |
176 | 129 | bytestream_put_be32(&buf, avctx->height); // window height | |
177 | 129 | bytestream_put_be32(&buf, 0); // window upper left X coordinate | |
178 | 129 | bytestream_put_be32(&buf, 0); // window upper left Y coordinate | |
179 | 129 | bytestream_put_be32(&buf, 0); // window border width | |
180 | 129 | bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE); | |
181 | |||
182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
|
129 | if (pix_fmt == AV_PIX_FMT_PAL8) { |
183 | ✗ | memcpy(pal, p->data[1], sizeof(pal)); | |
184 | } else { | ||
185 | 129 | avpriv_set_systematic_pal2(pal, pix_fmt); | |
186 | } | ||
187 | |||
188 |
2/2✓ Branch 0 taken 6656 times.
✓ Branch 1 taken 129 times.
|
6785 | for (i = 0; i < ncolors; i++) { |
189 | uint32_t val; | ||
190 | uint8_t red, green, blue; | ||
191 | |||
192 | 6656 | val = pal[i]; | |
193 | 6656 | red = (val >> 16) & 0xFF; | |
194 | 6656 | green = (val >> 8) & 0xFF; | |
195 | 6656 | blue = val & 0xFF; | |
196 | |||
197 | 6656 | bytestream_put_be32(&buf, i); // colormap entry number | |
198 | 6656 | bytestream_put_be16(&buf, red << 8); | |
199 | 6656 | bytestream_put_be16(&buf, green << 8); | |
200 | 6656 | bytestream_put_be16(&buf, blue << 8); | |
201 | 6656 | bytestream_put_byte(&buf, 0x7); // bitmask flag | |
202 | 6656 | bytestream_put_byte(&buf, 0); // padding | |
203 | } | ||
204 | |||
205 | 129 | ptr = p->data[0]; | |
206 |
2/2✓ Branch 0 taken 37152 times.
✓ Branch 1 taken 129 times.
|
37281 | for (i = 0; i < avctx->height; i++) { |
207 | 37152 | bytestream_put_buffer(&buf, ptr, lsize); | |
208 | 37152 | ptr += p->linesize[0]; | |
209 | } | ||
210 | |||
211 | 129 | *got_packet = 1; | |
212 | 129 | return 0; | |
213 | } | ||
214 | |||
215 | const FFCodec ff_xwd_encoder = { | ||
216 | .p.name = "xwd", | ||
217 | CODEC_LONG_NAME("XWD (X Window Dump) image"), | ||
218 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
219 | .p.id = AV_CODEC_ID_XWD, | ||
220 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
221 | FF_CODEC_ENCODE_CB(xwd_encode_frame), | ||
222 | .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA, | ||
223 | AV_PIX_FMT_RGBA, | ||
224 | AV_PIX_FMT_ARGB, | ||
225 | AV_PIX_FMT_ABGR, | ||
226 | AV_PIX_FMT_RGB24, | ||
227 | AV_PIX_FMT_BGR24, | ||
228 | AV_PIX_FMT_RGB565BE, | ||
229 | AV_PIX_FMT_RGB565LE, | ||
230 | AV_PIX_FMT_BGR565BE, | ||
231 | AV_PIX_FMT_BGR565LE, | ||
232 | AV_PIX_FMT_RGB555BE, | ||
233 | AV_PIX_FMT_RGB555LE, | ||
234 | AV_PIX_FMT_BGR555BE, | ||
235 | AV_PIX_FMT_BGR555LE, | ||
236 | AV_PIX_FMT_RGB8, | ||
237 | AV_PIX_FMT_BGR8, | ||
238 | AV_PIX_FMT_RGB4_BYTE, | ||
239 | AV_PIX_FMT_BGR4_BYTE, | ||
240 | AV_PIX_FMT_PAL8, | ||
241 | AV_PIX_FMT_GRAY8, | ||
242 | AV_PIX_FMT_MONOWHITE, | ||
243 | AV_PIX_FMT_NONE }, | ||
244 | }; | ||
245 |