4 * Copyright (c) 2012 Paul B Mahol
5 * Copyright (c) 2017 Paras Chadha
7 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/parseutils.h"
25 #include "libavutil/avstring.h"
29 typedef struct XPMContext {
36 typedef struct ColorEntry {
37 const char *name; ///< a string representing the name of the color
38 uint32_t rgb_color; ///< RGB values for the color
41 static int color_table_compare(const void *lhs, const void *rhs)
43 return av_strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
46 static const ColorEntry color_table[] = {
47 { "AliceBlue", 0xFFF0F8FF },
48 { "AntiqueWhite", 0xFFFAEBD7 },
49 { "Aqua", 0xFF00FFFF },
50 { "Aquamarine", 0xFF7FFFD4 },
51 { "Azure", 0xFFF0FFFF },
52 { "Beige", 0xFFF5F5DC },
53 { "Bisque", 0xFFFFE4C4 },
54 { "Black", 0xFF000000 },
55 { "BlanchedAlmond", 0xFFFFEBCD },
56 { "Blue", 0xFF0000FF },
57 { "BlueViolet", 0xFF8A2BE2 },
58 { "Brown", 0xFFA52A2A },
59 { "BurlyWood", 0xFFDEB887 },
60 { "CadetBlue", 0xFF5F9EA0 },
61 { "Chartreuse", 0xFF7FFF00 },
62 { "Chocolate", 0xFFD2691E },
63 { "Coral", 0xFFFF7F50 },
64 { "CornflowerBlue", 0xFF6495ED },
65 { "Cornsilk", 0xFFFFF8DC },
66 { "Crimson", 0xFFDC143C },
67 { "Cyan", 0xFF00FFFF },
68 { "DarkBlue", 0xFF00008B },
69 { "DarkCyan", 0xFF008B8B },
70 { "DarkGoldenRod", 0xFFB8860B },
71 { "DarkGray", 0xFFA9A9A9 },
72 { "DarkGreen", 0xFF006400 },
73 { "DarkKhaki", 0xFFBDB76B },
74 { "DarkMagenta", 0xFF8B008B },
75 { "DarkOliveGreen", 0xFF556B2F },
76 { "Darkorange", 0xFFFF8C00 },
77 { "DarkOrchid", 0xFF9932CC },
78 { "DarkRed", 0xFF8B0000 },
79 { "DarkSalmon", 0xFFE9967A },
80 { "DarkSeaGreen", 0xFF8FBC8F },
81 { "DarkSlateBlue", 0xFF483D8B },
82 { "DarkSlateGray", 0xFF2F4F4F },
83 { "DarkTurquoise", 0xFF00CED1 },
84 { "DarkViolet", 0xFF9400D3 },
85 { "DeepPink", 0xFFFF1493 },
86 { "DeepSkyBlue", 0xFF00BFFF },
87 { "DimGray", 0xFF696969 },
88 { "DodgerBlue", 0xFF1E90FF },
89 { "FireBrick", 0xFFB22222 },
90 { "FloralWhite", 0xFFFFFAF0 },
91 { "ForestGreen", 0xFF228B22 },
92 { "Fuchsia", 0xFFFF00FF },
93 { "Gainsboro", 0xFFDCDCDC },
94 { "GhostWhite", 0xFFF8F8FF },
95 { "Gold", 0xFFFFD700 },
96 { "GoldenRod", 0xFFDAA520 },
97 { "Gray", 0xFFBEBEBE },
98 { "Green", 0xFF00FF00 },
99 { "GreenYellow", 0xFFADFF2F },
100 { "HoneyDew", 0xFFF0FFF0 },
101 { "HotPink", 0xFFFF69B4 },
102 { "IndianRed", 0xFFCD5C5C },
103 { "Indigo", 0xFF4B0082 },
104 { "Ivory", 0xFFFFFFF0 },
105 { "Khaki", 0xFFF0E68C },
106 { "Lavender", 0xFFE6E6FA },
107 { "LavenderBlush", 0xFFFFF0F5 },
108 { "LawnGreen", 0xFF7CFC00 },
109 { "LemonChiffon", 0xFFFFFACD },
110 { "LightBlue", 0xFFADD8E6 },
111 { "LightCoral", 0xFFF08080 },
112 { "LightCyan", 0xFFE0FFFF },
113 { "LightGoldenRodYellow", 0xFFFAFAD2 },
114 { "LightGreen", 0xFF90EE90 },
115 { "LightGrey", 0xFFD3D3D3 },
116 { "LightPink", 0xFFFFB6C1 },
117 { "LightSalmon", 0xFFFFA07A },
118 { "LightSeaGreen", 0xFF20B2AA },
119 { "LightSkyBlue", 0xFF87CEFA },
120 { "LightSlateGray", 0xFF778899 },
121 { "LightSteelBlue", 0xFFB0C4DE },
122 { "LightYellow", 0xFFFFFFE0 },
123 { "Lime", 0xFF00FF00 },
124 { "LimeGreen", 0xFF32CD32 },
125 { "Linen", 0xFFFAF0E6 },
126 { "Magenta", 0xFFFF00FF },
127 { "Maroon", 0xFFB03060 },
128 { "MediumAquaMarine", 0xFF66CDAA },
129 { "MediumBlue", 0xFF0000CD },
130 { "MediumOrchid", 0xFFBA55D3 },
131 { "MediumPurple", 0xFF9370D8 },
132 { "MediumSeaGreen", 0xFF3CB371 },
133 { "MediumSlateBlue", 0xFF7B68EE },
134 { "MediumSpringGreen", 0xFF00FA9A },
135 { "MediumTurquoise", 0xFF48D1CC },
136 { "MediumVioletRed", 0xFFC71585 },
137 { "MidnightBlue", 0xFF191970 },
138 { "MintCream", 0xFFF5FFFA },
139 { "MistyRose", 0xFFFFE4E1 },
140 { "Moccasin", 0xFFFFE4B5 },
141 { "NavajoWhite", 0xFFFFDEAD },
142 { "Navy", 0xFF000080 },
143 { "None", 0x00000000 },
144 { "OldLace", 0xFFFDF5E6 },
145 { "Olive", 0xFF808000 },
146 { "OliveDrab", 0xFF6B8E23 },
147 { "Orange", 0xFFFFA500 },
148 { "OrangeRed", 0xFFFF4500 },
149 { "Orchid", 0xFFDA70D6 },
150 { "PaleGoldenRod", 0xFFEEE8AA },
151 { "PaleGreen", 0xFF98FB98 },
152 { "PaleTurquoise", 0xFFAFEEEE },
153 { "PaleVioletRed", 0xFFD87093 },
154 { "PapayaWhip", 0xFFFFEFD5 },
155 { "PeachPuff", 0xFFFFDAB9 },
156 { "Peru", 0xFFCD853F },
157 { "Pink", 0xFFFFC0CB },
158 { "Plum", 0xFFDDA0DD },
159 { "PowderBlue", 0xFFB0E0E6 },
160 { "Purple", 0xFFA020F0 },
161 { "Red", 0xFFFF0000 },
162 { "RosyBrown", 0xFFBC8F8F },
163 { "RoyalBlue", 0xFF4169E1 },
164 { "SaddleBrown", 0xFF8B4513 },
165 { "Salmon", 0xFFFA8072 },
166 { "SandyBrown", 0xFFF4A460 },
167 { "SeaGreen", 0xFF2E8B57 },
168 { "SeaShell", 0xFFFFF5EE },
169 { "Sienna", 0xFFA0522D },
170 { "Silver", 0xFFC0C0C0 },
171 { "SkyBlue", 0xFF87CEEB },
172 { "SlateBlue", 0xFF6A5ACD },
173 { "SlateGray", 0xFF708090 },
174 { "Snow", 0xFFFFFAFA },
175 { "SpringGreen", 0xFF00FF7F },
176 { "SteelBlue", 0xFF4682B4 },
177 { "Tan", 0xFFD2B48C },
178 { "Teal", 0xFF008080 },
179 { "Thistle", 0xFFD8BFD8 },
180 { "Tomato", 0xFFFF6347 },
181 { "Turquoise", 0xFF40E0D0 },
182 { "Violet", 0xFFEE82EE },
183 { "Wheat", 0xFFF5DEB3 },
184 { "White", 0xFFFFFFFF },
185 { "WhiteSmoke", 0xFFF5F5F5 },
186 { "Yellow", 0xFFFFFF00 },
187 { "YellowGreen", 0xFF9ACD32 }
190 static unsigned hex_char_to_number(uint8_t x)
192 if (x >= 'a' && x <= 'f')
194 else if (x >= 'A' && x <= 'F')
196 else if (x >= '0' && x <= '9')
204 * Function same as strcspn but ignores characters if they are inside a C style comments
206 static size_t mod_strcspn(const char *string, const char *reject)
210 for (i = 0; string && string[i]; i++) {
211 if (string[i] == '/' && string[i+1] == '*') {
213 while ( string && string[i] && (string[i] != '*' || string[i+1] != '/') )
216 } else if (string[i] == '/' && string[i+1] == '/') {
218 while ( string && string[i] && string[i] != '\n' )
221 for (j = 0; reject && reject[j]; j++) {
222 if (string[i] == reject[j])
225 if (reject && reject[j])
232 static uint32_t color_string_to_rgba(const char *p, int len)
234 uint32_t ret = 0xFF000000;
235 const ColorEntry *entry;
236 char color_name[100];
238 len = FFMIN(FFMAX(len, 0), sizeof(color_name) - 1);
244 ret |= (hex_char_to_number(p[2]) << 4) |
245 (hex_char_to_number(p[1]) << 12) |
246 (hex_char_to_number(p[0]) << 20);
247 } else if (len == 4) {
248 ret = (hex_char_to_number(p[3]) << 4) |
249 (hex_char_to_number(p[2]) << 12) |
250 (hex_char_to_number(p[1]) << 20) |
251 (hex_char_to_number(p[0]) << 28);
252 } else if (len == 6) {
253 ret |= hex_char_to_number(p[5]) |
254 (hex_char_to_number(p[4]) << 4) |
255 (hex_char_to_number(p[3]) << 8) |
256 (hex_char_to_number(p[2]) << 12) |
257 (hex_char_to_number(p[1]) << 16) |
258 (hex_char_to_number(p[0]) << 20);
259 } else if (len == 8) {
260 ret = hex_char_to_number(p[7]) |
261 (hex_char_to_number(p[6]) << 4) |
262 (hex_char_to_number(p[5]) << 8) |
263 (hex_char_to_number(p[4]) << 12) |
264 (hex_char_to_number(p[3]) << 16) |
265 (hex_char_to_number(p[2]) << 20) |
266 (hex_char_to_number(p[1]) << 24) |
267 (hex_char_to_number(p[0]) << 28);
270 strncpy(color_name, p, len);
271 color_name[len] = '\0';
273 entry = bsearch(color_name,
275 FF_ARRAY_ELEMS(color_table),
277 color_table_compare);
282 ret = entry->rgb_color;
287 static int ascii2index(const uint8_t *cpixel, int cpp)
289 const uint8_t *p = cpixel;
292 for (i = 0; i < cpp; i++) {
293 if (*p < ' ' || *p > '~')
294 return AVERROR_INVALIDDATA;
295 n += (*p++ - ' ') * m;
301 static int xpm_decode_frame(AVCodecContext *avctx, void *data,
302 int *got_frame, AVPacket *avpkt)
304 XPMDecContext *x = avctx->priv_data;
306 const uint8_t *end, *ptr;
307 int ncolors, cpp, ret, i, j;
311 avctx->pix_fmt = AV_PIX_FMT_BGRA;
313 av_fast_padded_malloc(&x->buf, &x->buf_size, avpkt->size);
315 return AVERROR(ENOMEM);
316 memcpy(x->buf, avpkt->data, avpkt->size);
317 x->buf[avpkt->size] = 0;
320 end = x->buf + avpkt->size;
321 while (end - ptr > 9 && memcmp(ptr, "/* XPM */", 9))
324 if (end - ptr <= 9) {
325 av_log(avctx, AV_LOG_ERROR, "missing signature\n");
326 return AVERROR_INVALIDDATA;
329 ptr += mod_strcspn(ptr, "\"");
330 if (sscanf(ptr, "\"%u %u %u %u\",",
331 &avctx->width, &avctx->height, &ncolors, &cpp) != 4) {
332 av_log(avctx, AV_LOG_ERROR, "missing image parameters\n");
333 return AVERROR_INVALIDDATA;
336 if ((ret = ff_set_dimensions(avctx, avctx->width, avctx->height)) < 0)
339 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
342 if (cpp <= 0 || cpp >= 5) {
343 av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of chars per pixel: %d\n", cpp);
344 return AVERROR_INVALIDDATA;
348 for (i = 0; i < cpp; i++)
351 if (ncolors <= 0 || ncolors > size) {
352 av_log(avctx, AV_LOG_ERROR, "invalid number of colors: %d\n", ncolors);
353 return AVERROR_INVALIDDATA;
358 av_fast_padded_malloc(&x->pixels, &x->pixels_size, size);
360 return AVERROR(ENOMEM);
362 ptr += mod_strcspn(ptr, ",") + 1;
364 return AVERROR_INVALIDDATA;
366 for (i = 0; i < ncolors; i++) {
367 const uint8_t *index;
370 ptr += mod_strcspn(ptr, "\"") + 1;
372 return AVERROR_INVALIDDATA;
376 ptr = strstr(ptr, "c ");
380 return AVERROR_INVALIDDATA;
383 len = strcspn(ptr, "\" ");
385 if ((ret = ascii2index(index, cpp)) < 0)
388 x->pixels[ret] = color_string_to_rgba(ptr, len);
389 ptr += mod_strcspn(ptr, ",") + 1;
391 return AVERROR_INVALIDDATA;
394 for (i = 0; i < avctx->height; i++) {
395 dst = (uint32_t *)(p->data[0] + i * p->linesize[0]);
397 return AVERROR_INVALIDDATA;
398 ptr += mod_strcspn(ptr, "\"") + 1;
400 return AVERROR_INVALIDDATA;
402 for (j = 0; j < avctx->width; j++) {
404 return AVERROR_INVALIDDATA;
406 if ((ret = ascii2index(ptr, cpp)) < 0)
409 *dst++ = x->pixels[ret];
412 ptr += mod_strcspn(ptr, ",") + 1;
416 p->pict_type = AV_PICTURE_TYPE_I;
423 static av_cold int xpm_decode_close(AVCodecContext *avctx)
425 XPMDecContext *x = avctx->priv_data;
426 av_freep(&x->pixels);
434 AVCodec ff_xpm_decoder = {
436 .type = AVMEDIA_TYPE_VIDEO,
437 .id = AV_CODEC_ID_XPM,
438 .priv_data_size = sizeof(XPMDecContext),
439 .close = xpm_decode_close,
440 .decode = xpm_decode_frame,
441 .capabilities = AV_CODEC_CAP_DR1,
442 .long_name = NULL_IF_CONFIG_SMALL("XPM (X PixMap) image")