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.

444 lines
14KB

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