FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/xwdenc.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 116 131 88.5%
Functions: 1 1 100.0%
Branches: 24 36 66.7%

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