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.

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