FFmpeg  4.0
xpmdec.c
Go to the documentation of this file.
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/parseutils.h"
25 #include "libavutil/avstring.h"
26 #include "avcodec.h"
27 #include "internal.h"
28 
29 typedef struct XPMContext {
30  uint32_t *pixels;
33  int buf_size;
35 
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
39 } ColorEntry;
40 
41 static int color_table_compare(const void *lhs, const void *rhs)
42 {
43  return av_strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
44 }
45 
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 }
188 };
189 
190 static unsigned hex_char_to_number(uint8_t x)
191 {
192  if (x >= 'a' && x <= 'f')
193  x -= 'a' - 10;
194  else if (x >= 'A' && x <= 'F')
195  x -= 'A' - 10;
196  else if (x >= '0' && x <= '9')
197  x -= '0';
198  else
199  x = 0;
200  return x;
201 }
202 
203 /*
204  * Function same as strcspn but ignores characters if they are inside a C style comments
205  */
206 static size_t mod_strcspn(const char *string, const char *reject)
207 {
208  int i, j;
209 
210  for (i = 0; string && string[i]; i++) {
211  if (string[i] == '/' && string[i+1] == '*') {
212  i += 2;
213  while ( string && string[i] && (string[i] != '*' || string[i+1] != '/') )
214  i++;
215  i++;
216  } else if (string[i] == '/' && string[i+1] == '/') {
217  i += 2;
218  while ( string && string[i] && string[i] != '\n' )
219  i++;
220  } else {
221  for (j = 0; reject && reject[j]; j++) {
222  if (string[i] == reject[j])
223  break;
224  }
225  if (reject && reject[j])
226  break;
227  }
228  }
229  return i;
230 }
231 
232 static uint32_t color_string_to_rgba(const char *p, int len)
233 {
234  uint32_t ret = 0xFF000000;
235  const ColorEntry *entry;
236  char color_name[100];
237 
238  len = FFMIN(FFMAX(len, 0), sizeof(color_name) - 1);
239 
240  if (*p == '#') {
241  p++;
242  len--;
243  if (len == 3) {
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);
268  }
269  } else {
270  strncpy(color_name, p, len);
271  color_name[len] = '\0';
272 
273  entry = bsearch(color_name,
274  color_table,
275  FF_ARRAY_ELEMS(color_table),
276  sizeof(ColorEntry),
278 
279  if (!entry)
280  return ret;
281 
282  ret = entry->rgb_color;
283  }
284  return ret;
285 }
286 
287 static int ascii2index(const uint8_t *cpixel, int cpp)
288 {
289  const uint8_t *p = cpixel;
290  int n = 0, m = 1, i;
291 
292  for (i = 0; i < cpp; i++) {
293  if (*p < ' ' || *p > '~')
294  return AVERROR_INVALIDDATA;
295  n += (*p++ - ' ') * m;
296  m *= 95;
297  }
298  return n;
299 }
300 
301 static int xpm_decode_frame(AVCodecContext *avctx, void *data,
302  int *got_frame, AVPacket *avpkt)
303 {
304  XPMDecContext *x = avctx->priv_data;
305  AVFrame *p=data;
306  const uint8_t *end, *ptr;
307  int ncolors, cpp, ret, i, j;
308  int64_t size;
309  uint32_t *dst;
310 
311  avctx->pix_fmt = AV_PIX_FMT_BGRA;
312 
313  av_fast_padded_malloc(&x->buf, &x->buf_size, avpkt->size);
314  if (!x->buf)
315  return AVERROR(ENOMEM);
316  memcpy(x->buf, avpkt->data, avpkt->size);
317  x->buf[avpkt->size] = 0;
318 
319  ptr = x->buf;
320  end = x->buf + avpkt->size;
321  while (end - ptr > 9 && memcmp(ptr, "/* XPM */", 9))
322  ptr++;
323 
324  if (end - ptr <= 9) {
325  av_log(avctx, AV_LOG_ERROR, "missing signature\n");
326  return AVERROR_INVALIDDATA;
327  }
328 
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;
334  }
335 
336  if ((ret = ff_set_dimensions(avctx, avctx->width, avctx->height)) < 0)
337  return ret;
338 
339  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
340  return ret;
341 
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;
345  }
346 
347  size = 1;
348  for (i = 0; i < cpp; i++)
349  size *= 95;
350 
351  if (ncolors <= 0 || ncolors > size) {
352  av_log(avctx, AV_LOG_ERROR, "invalid number of colors: %d\n", ncolors);
353  return AVERROR_INVALIDDATA;
354  }
355 
356  size *= 4;
357 
358  av_fast_padded_malloc(&x->pixels, &x->pixels_size, size);
359  if (!x->pixels)
360  return AVERROR(ENOMEM);
361 
362  ptr += mod_strcspn(ptr, ",") + 1;
363  if (end - ptr < 1)
364  return AVERROR_INVALIDDATA;
365 
366  for (i = 0; i < ncolors; i++) {
367  const uint8_t *index;
368  int len;
369 
370  ptr += mod_strcspn(ptr, "\"") + 1;
371  if (end - ptr < cpp)
372  return AVERROR_INVALIDDATA;
373  index = ptr;
374  ptr += cpp;
375 
376  ptr = strstr(ptr, "c ");
377  if (ptr) {
378  ptr += 2;
379  } else {
380  return AVERROR_INVALIDDATA;
381  }
382 
383  len = strcspn(ptr, "\" ");
384 
385  if ((ret = ascii2index(index, cpp)) < 0)
386  return ret;
387 
388  x->pixels[ret] = color_string_to_rgba(ptr, len);
389  ptr += mod_strcspn(ptr, ",") + 1;
390  if (end - ptr < 1)
391  return AVERROR_INVALIDDATA;
392  }
393 
394  for (i = 0; i < avctx->height; i++) {
395  dst = (uint32_t *)(p->data[0] + i * p->linesize[0]);
396  if (end - ptr < 1)
397  return AVERROR_INVALIDDATA;
398  ptr += mod_strcspn(ptr, "\"") + 1;
399  if (end - ptr < 1)
400  return AVERROR_INVALIDDATA;
401 
402  for (j = 0; j < avctx->width; j++) {
403  if (end - ptr < cpp)
404  return AVERROR_INVALIDDATA;
405 
406  if ((ret = ascii2index(ptr, cpp)) < 0)
407  return ret;
408 
409  *dst++ = x->pixels[ret];
410  ptr += cpp;
411  }
412  ptr += mod_strcspn(ptr, ",") + 1;
413  }
414 
415  p->key_frame = 1;
417 
418  *got_frame = 1;
419 
420  return avpkt->size;
421 }
422 
424 {
425  XPMDecContext *x = avctx->priv_data;
426  av_freep(&x->pixels);
427 
428  av_freep(&x->buf);
429  x->buf_size = 0;
430 
431  return 0;
432 }
433 
435  .name = "xpm",
436  .type = AVMEDIA_TYPE_VIDEO,
437  .id = AV_CODEC_ID_XPM,
438  .priv_data_size = sizeof(XPMDecContext),
439  .close = xpm_decode_close,
441  .capabilities = AV_CODEC_CAP_DR1,
442  .long_name = NULL_IF_CONFIG_SMALL("XPM (X PixMap) image")
443 };
const char * name
Definition: avisynth_c.h:775
static int xpm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xpmdec.c:301
static int color_table_compare(const void *lhs, const void *rhs)
Definition: xpmdec.c:41
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
int size
Definition: avcodec.h:1431
const char * name
a string representing the name of the color
Definition: xpmdec.c:37
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:70
int buf_size
Definition: xpmdec.c:33
AVCodec.
Definition: avcodec.h:3408
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
AVCodec ff_xpm_decoder
Definition: xpmdec.c:434
uint8_t
#define av_cold
Definition: attributes.h:82
static uint32_t color_string_to_rgba(const char *p, int len)
Definition: xpmdec.c:232
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
#define av_log(a,...)
uint32_t rgb_color
RGB values for the color.
Definition: xpmdec.c:38
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static av_cold int xpm_decode_close(AVCodecContext *avctx)
Definition: xpmdec.c:423
#define AVERROR(e)
Definition: error.h:43
static const ColorEntry color_table[]
Definition: xpmdec.c:46
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:91
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
#define FFMAX(a, b)
Definition: common.h:94
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
#define FFMIN(a, b)
Definition: common.h:96
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
int width
picture width / height.
Definition: avcodec.h:1690
int n
Definition: avisynth_c.h:684
#define FF_ARRAY_ELEMS(a)
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
main external API structure.
Definition: avcodec.h:1518
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1891
int index
Definition: gxfenc.c:89
int pixels_size
Definition: xpmdec.c:31
misc parsing utilities
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
static size_t mod_strcspn(const char *string, const char *reject)
Definition: xpmdec.c:206
common internal api header.
void * priv_data
Definition: avcodec.h:1545
int len
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
uint8_t * buf
Definition: xpmdec.c:32
#define av_freep(p)
static int ascii2index(const uint8_t *cpixel, int cpp)
Definition: xpmdec.c:287
uint32_t * pixels
Definition: xpmdec.c:30
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
static unsigned hex_char_to_number(uint8_t x)
Definition: xpmdec.c:190