FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/xpmdec.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 0 158 0.0%
Functions: 0 7 0.0%
Branches: 0 118 0.0%

Line Branch Exec Source
1 /*
2 * XPM image format
3 *
4 * Copyright (c) 2012 Paul B Mahol
5 * Copyright (c) 2017 Paras Chadha
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "libavutil/avstring.h"
25 #include "avcodec.h"
26 #include "codec_internal.h"
27 #include "decode.h"
28
29 #define MIN_ELEMENT ' '
30 #define MAX_ELEMENT 0xfe
31 #define NB_ELEMENTS (MAX_ELEMENT - MIN_ELEMENT + 1)
32
33 typedef struct XPMContext {
34 uint32_t *pixels;
35 int pixels_size;
36 uint8_t *buf;
37 int buf_size;
38 } XPMDecContext;
39
40 typedef struct ColorEntry {
41 const char *name; ///< a string representing the name of the color
42 uint32_t rgb_color; ///< RGB values for the color
43 } ColorEntry;
44
45 static int color_table_compare(const void *lhs, const void *rhs)
46 {
47 return av_strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
48 }
49
50 static const ColorEntry color_table[] = {
51 { "AliceBlue", 0xFFF0F8FF },
52 { "AntiqueWhite", 0xFFFAEBD7 },
53 { "Aqua", 0xFF00FFFF },
54 { "Aquamarine", 0xFF7FFFD4 },
55 { "Azure", 0xFFF0FFFF },
56 { "Beige", 0xFFF5F5DC },
57 { "Bisque", 0xFFFFE4C4 },
58 { "Black", 0xFF000000 },
59 { "BlanchedAlmond", 0xFFFFEBCD },
60 { "Blue", 0xFF0000FF },
61 { "BlueViolet", 0xFF8A2BE2 },
62 { "Brown", 0xFFA52A2A },
63 { "BurlyWood", 0xFFDEB887 },
64 { "CadetBlue", 0xFF5F9EA0 },
65 { "Chartreuse", 0xFF7FFF00 },
66 { "Chocolate", 0xFFD2691E },
67 { "Coral", 0xFFFF7F50 },
68 { "CornflowerBlue", 0xFF6495ED },
69 { "Cornsilk", 0xFFFFF8DC },
70 { "Crimson", 0xFFDC143C },
71 { "Cyan", 0xFF00FFFF },
72 { "DarkBlue", 0xFF00008B },
73 { "DarkCyan", 0xFF008B8B },
74 { "DarkGoldenRod", 0xFFB8860B },
75 { "DarkGray", 0xFFA9A9A9 },
76 { "DarkGreen", 0xFF006400 },
77 { "DarkKhaki", 0xFFBDB76B },
78 { "DarkMagenta", 0xFF8B008B },
79 { "DarkOliveGreen", 0xFF556B2F },
80 { "Darkorange", 0xFFFF8C00 },
81 { "DarkOrchid", 0xFF9932CC },
82 { "DarkRed", 0xFF8B0000 },
83 { "DarkSalmon", 0xFFE9967A },
84 { "DarkSeaGreen", 0xFF8FBC8F },
85 { "DarkSlateBlue", 0xFF483D8B },
86 { "DarkSlateGray", 0xFF2F4F4F },
87 { "DarkTurquoise", 0xFF00CED1 },
88 { "DarkViolet", 0xFF9400D3 },
89 { "DeepPink", 0xFFFF1493 },
90 { "DeepSkyBlue", 0xFF00BFFF },
91 { "DimGray", 0xFF696969 },
92 { "DodgerBlue", 0xFF1E90FF },
93 { "FireBrick", 0xFFB22222 },
94 { "FloralWhite", 0xFFFFFAF0 },
95 { "ForestGreen", 0xFF228B22 },
96 { "Fuchsia", 0xFFFF00FF },
97 { "Gainsboro", 0xFFDCDCDC },
98 { "GhostWhite", 0xFFF8F8FF },
99 { "Gold", 0xFFFFD700 },
100 { "GoldenRod", 0xFFDAA520 },
101 { "Gray", 0xFFBEBEBE },
102 { "Green", 0xFF00FF00 },
103 { "GreenYellow", 0xFFADFF2F },
104 { "HoneyDew", 0xFFF0FFF0 },
105 { "HotPink", 0xFFFF69B4 },
106 { "IndianRed", 0xFFCD5C5C },
107 { "Indigo", 0xFF4B0082 },
108 { "Ivory", 0xFFFFFFF0 },
109 { "Khaki", 0xFFF0E68C },
110 { "Lavender", 0xFFE6E6FA },
111 { "LavenderBlush", 0xFFFFF0F5 },
112 { "LawnGreen", 0xFF7CFC00 },
113 { "LemonChiffon", 0xFFFFFACD },
114 { "LightBlue", 0xFFADD8E6 },
115 { "LightCoral", 0xFFF08080 },
116 { "LightCyan", 0xFFE0FFFF },
117 { "LightGoldenRodYellow", 0xFFFAFAD2 },
118 { "LightGreen", 0xFF90EE90 },
119 { "LightGrey", 0xFFD3D3D3 },
120 { "LightPink", 0xFFFFB6C1 },
121 { "LightSalmon", 0xFFFFA07A },
122 { "LightSeaGreen", 0xFF20B2AA },
123 { "LightSkyBlue", 0xFF87CEFA },
124 { "LightSlateGray", 0xFF778899 },
125 { "LightSteelBlue", 0xFFB0C4DE },
126 { "LightYellow", 0xFFFFFFE0 },
127 { "Lime", 0xFF00FF00 },
128 { "LimeGreen", 0xFF32CD32 },
129 { "Linen", 0xFFFAF0E6 },
130 { "Magenta", 0xFFFF00FF },
131 { "Maroon", 0xFFB03060 },
132 { "MediumAquaMarine", 0xFF66CDAA },
133 { "MediumBlue", 0xFF0000CD },
134 { "MediumOrchid", 0xFFBA55D3 },
135 { "MediumPurple", 0xFF9370D8 },
136 { "MediumSeaGreen", 0xFF3CB371 },
137 { "MediumSlateBlue", 0xFF7B68EE },
138 { "MediumSpringGreen", 0xFF00FA9A },
139 { "MediumTurquoise", 0xFF48D1CC },
140 { "MediumVioletRed", 0xFFC71585 },
141 { "MidnightBlue", 0xFF191970 },
142 { "MintCream", 0xFFF5FFFA },
143 { "MistyRose", 0xFFFFE4E1 },
144 { "Moccasin", 0xFFFFE4B5 },
145 { "NavajoWhite", 0xFFFFDEAD },
146 { "Navy", 0xFF000080 },
147 { "None", 0x00000000 },
148 { "OldLace", 0xFFFDF5E6 },
149 { "Olive", 0xFF808000 },
150 { "OliveDrab", 0xFF6B8E23 },
151 { "Orange", 0xFFFFA500 },
152 { "OrangeRed", 0xFFFF4500 },
153 { "Orchid", 0xFFDA70D6 },
154 { "PaleGoldenRod", 0xFFEEE8AA },
155 { "PaleGreen", 0xFF98FB98 },
156 { "PaleTurquoise", 0xFFAFEEEE },
157 { "PaleVioletRed", 0xFFD87093 },
158 { "PapayaWhip", 0xFFFFEFD5 },
159 { "PeachPuff", 0xFFFFDAB9 },
160 { "Peru", 0xFFCD853F },
161 { "Pink", 0xFFFFC0CB },
162 { "Plum", 0xFFDDA0DD },
163 { "PowderBlue", 0xFFB0E0E6 },
164 { "Purple", 0xFFA020F0 },
165 { "Red", 0xFFFF0000 },
166 { "RosyBrown", 0xFFBC8F8F },
167 { "RoyalBlue", 0xFF4169E1 },
168 { "SaddleBrown", 0xFF8B4513 },
169 { "Salmon", 0xFFFA8072 },
170 { "SandyBrown", 0xFFF4A460 },
171 { "SeaGreen", 0xFF2E8B57 },
172 { "SeaShell", 0xFFFFF5EE },
173 { "Sienna", 0xFFA0522D },
174 { "Silver", 0xFFC0C0C0 },
175 { "SkyBlue", 0xFF87CEEB },
176 { "SlateBlue", 0xFF6A5ACD },
177 { "SlateGray", 0xFF708090 },
178 { "Snow", 0xFFFFFAFA },
179 { "SpringGreen", 0xFF00FF7F },
180 { "SteelBlue", 0xFF4682B4 },
181 { "Tan", 0xFFD2B48C },
182 { "Teal", 0xFF008080 },
183 { "Thistle", 0xFFD8BFD8 },
184 { "Tomato", 0xFFFF6347 },
185 { "Turquoise", 0xFF40E0D0 },
186 { "Violet", 0xFFEE82EE },
187 { "Wheat", 0xFFF5DEB3 },
188 { "White", 0xFFFFFFFF },
189 { "WhiteSmoke", 0xFFF5F5F5 },
190 { "Yellow", 0xFFFFFF00 },
191 { "YellowGreen", 0xFF9ACD32 }
192 };
193
194 static unsigned hex_char_to_number(uint8_t x)
195 {
196 if (x >= 'a' && x <= 'f')
197 x -= 'a' - 10;
198 else if (x >= 'A' && x <= 'F')
199 x -= 'A' - 10;
200 else if (x >= '0' && x <= '9')
201 x -= '0';
202 else
203 x = 0;
204 return x;
205 }
206
207 /*
208 * Function same as strcspn but ignores characters if they are inside a C style comments
209 */
210 static size_t mod_strcspn(const char *string, const char *reject)
211 {
212 int i, j;
213
214 for (i = 0; string && string[i]; i++) {
215 if (string[i] == '/' && string[i+1] == '*') {
216 i += 2;
217 while ( string && string[i] && (string[i] != '*' || string[i+1] != '/') )
218 i++;
219 i++;
220 } else if (string[i] == '/' && string[i+1] == '/') {
221 i += 2;
222 while ( string && string[i] && string[i] != '\n' )
223 i++;
224 } else {
225 for (j = 0; reject && reject[j]; j++) {
226 if (string[i] == reject[j])
227 break;
228 }
229 if (reject && reject[j])
230 break;
231 }
232 }
233 return i;
234 }
235
236 static uint32_t color_string_to_rgba(const char *p, int len)
237 {
238 uint32_t ret = 0xFF000000;
239 const ColorEntry *entry;
240 char color_name[100];
241
242 len = FFMIN(FFMAX(len, 0), sizeof(color_name) - 1);
243
244 if (*p == '#') {
245 p++;
246 len--;
247 if (len == 3) {
248 ret |= (hex_char_to_number(p[2]) << 4) |
249 (hex_char_to_number(p[1]) << 12) |
250 (hex_char_to_number(p[0]) << 20);
251 } else if (len == 4) {
252 ret = (hex_char_to_number(p[3]) << 4) |
253 (hex_char_to_number(p[2]) << 12) |
254 (hex_char_to_number(p[1]) << 20) |
255 (hex_char_to_number(p[0]) << 28);
256 } else if (len == 6) {
257 ret |= hex_char_to_number(p[5]) |
258 (hex_char_to_number(p[4]) << 4) |
259 (hex_char_to_number(p[3]) << 8) |
260 (hex_char_to_number(p[2]) << 12) |
261 (hex_char_to_number(p[1]) << 16) |
262 (hex_char_to_number(p[0]) << 20);
263 } else if (len == 8) {
264 ret = hex_char_to_number(p[7]) |
265 (hex_char_to_number(p[6]) << 4) |
266 (hex_char_to_number(p[5]) << 8) |
267 (hex_char_to_number(p[4]) << 12) |
268 (hex_char_to_number(p[3]) << 16) |
269 (hex_char_to_number(p[2]) << 20) |
270 (hex_char_to_number(p[1]) << 24) |
271 (hex_char_to_number(p[0]) << 28);
272 }
273 } else {
274 strncpy(color_name, p, len);
275 color_name[len] = '\0';
276
277 entry = bsearch(color_name,
278 color_table,
279 FF_ARRAY_ELEMS(color_table),
280 sizeof(ColorEntry),
281 color_table_compare);
282
283 if (!entry)
284 return ret;
285
286 ret = entry->rgb_color;
287 }
288 return ret;
289 }
290
291 static int ascii2index(const uint8_t *cpixel, int cpp)
292 {
293 const uint8_t *p = cpixel;
294 int n = 0, m = 1, i;
295
296 for (i = 0; i < cpp; i++) {
297 if (*p < MIN_ELEMENT || *p > MAX_ELEMENT)
298 return AVERROR_INVALIDDATA;
299 n += (*p++ - MIN_ELEMENT) * m;
300 m *= NB_ELEMENTS;
301 }
302 return n;
303 }
304
305 static int xpm_decode_frame(AVCodecContext *avctx, AVFrame *p,
306 int *got_frame, AVPacket *avpkt)
307 {
308 XPMDecContext *x = avctx->priv_data;
309 const uint8_t *end, *ptr;
310 int ncolors, cpp, ret, i, j;
311 int64_t size;
312 uint32_t *dst;
313 int width, height;
314
315 avctx->pix_fmt = AV_PIX_FMT_BGRA;
316
317 av_fast_padded_malloc(&x->buf, &x->buf_size, avpkt->size);
318 if (!x->buf)
319 return AVERROR(ENOMEM);
320 memcpy(x->buf, avpkt->data, avpkt->size);
321 x->buf[avpkt->size] = 0;
322
323 ptr = x->buf;
324 end = x->buf + avpkt->size;
325 while (end - ptr > 9 && memcmp(ptr, "/* XPM */", 9))
326 ptr++;
327
328 if (end - ptr <= 9) {
329 av_log(avctx, AV_LOG_ERROR, "missing signature\n");
330 return AVERROR_INVALIDDATA;
331 }
332
333 ptr += mod_strcspn(ptr, "\"");
334 if (sscanf(ptr, "\"%u %u %u %u\",",
335 &width, &height, &ncolors, &cpp) != 4) {
336 av_log(avctx, AV_LOG_ERROR, "missing image parameters\n");
337 return AVERROR_INVALIDDATA;
338 }
339
340 if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
341 return ret;
342
343 if (cpp <= 0 || cpp >= 5) {
344 av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of chars per pixel: %d\n", cpp);
345 return AVERROR_INVALIDDATA;
346 }
347
348 size = 1;
349 for (i = 0; i < cpp; i++)
350 size *= NB_ELEMENTS;
351
352 if (ncolors <= 0 || ncolors > size) {
353 av_log(avctx, AV_LOG_ERROR, "invalid number of colors: %d\n", ncolors);
354 return AVERROR_INVALIDDATA;
355 }
356
357 size *= 4;
358
359 ptr += mod_strcspn(ptr, ",") + 1;
360 if (end - ptr < 1)
361 return AVERROR_INVALIDDATA;
362
363 if (avctx->skip_frame >= AVDISCARD_ALL)
364 return avpkt->size;
365
366 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
367 return ret;
368
369 av_fast_padded_malloc(&x->pixels, &x->pixels_size, size);
370 if (!x->pixels)
371 return AVERROR(ENOMEM);
372
373 for (i = 0; i < ncolors; i++) {
374 const uint8_t *index;
375 int len;
376
377 ptr += mod_strcspn(ptr, "\"") + 1;
378 if (end - ptr < cpp)
379 return AVERROR_INVALIDDATA;
380 index = ptr;
381 ptr += cpp;
382
383 ptr = strstr(ptr, "c ");
384 if (ptr) {
385 ptr += 2;
386 } else {
387 return AVERROR_INVALIDDATA;
388 }
389
390 len = strcspn(ptr, "\" ");
391
392 if ((ret = ascii2index(index, cpp)) < 0)
393 return ret;
394
395 x->pixels[ret] = color_string_to_rgba(ptr, len);
396 ptr += mod_strcspn(ptr, ",") + 1;
397 if (end - ptr < 1)
398 return AVERROR_INVALIDDATA;
399 }
400
401 for (i = 0; i < avctx->height; i++) {
402 dst = (uint32_t *)(p->data[0] + i * p->linesize[0]);
403 if (end - ptr < 1)
404 return AVERROR_INVALIDDATA;
405 ptr += mod_strcspn(ptr, "\"") + 1;
406 if (end - ptr < 1)
407 return AVERROR_INVALIDDATA;
408
409 for (j = 0; j < avctx->width; j++) {
410 if (end - ptr < cpp)
411 return AVERROR_INVALIDDATA;
412
413 if ((ret = ascii2index(ptr, cpp)) < 0)
414 return ret;
415
416 *dst++ = x->pixels[ret];
417 ptr += cpp;
418 }
419 ptr += mod_strcspn(ptr, ",") + 1;
420 }
421
422 p->key_frame = 1;
423 p->pict_type = AV_PICTURE_TYPE_I;
424
425 *got_frame = 1;
426
427 return avpkt->size;
428 }
429
430 static av_cold int xpm_decode_close(AVCodecContext *avctx)
431 {
432 XPMDecContext *x = avctx->priv_data;
433 av_freep(&x->pixels);
434
435 av_freep(&x->buf);
436 x->buf_size = 0;
437
438 return 0;
439 }
440
441 const FFCodec ff_xpm_decoder = {
442 .p.name = "xpm",
443 CODEC_LONG_NAME("XPM (X PixMap) image"),
444 .p.type = AVMEDIA_TYPE_VIDEO,
445 .p.id = AV_CODEC_ID_XPM,
446 .p.capabilities = AV_CODEC_CAP_DR1,
447 .priv_data_size = sizeof(XPMDecContext),
448 .close = xpm_decode_close,
449 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
450 FF_CODEC_DECODE_CB(xpm_decode_frame),
451 };
452