You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

448 lines
15KB

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