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.

451 lines
17KB

  1. /*
  2. * copyright (c) 2009 Stefano Sabatini
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * parsing utils
  22. */
  23. #include <strings.h>
  24. #include "libavutil/avutil.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/random_seed.h"
  27. #include "parseutils.h"
  28. typedef struct {
  29. const char *name; ///< a string representing the name of the color
  30. uint8_t rgb_color[3]; ///< RGB values for the color
  31. } ColorEntry;
  32. static ColorEntry color_table[] = {
  33. { "AliceBlue", { 0xF0, 0xF8, 0xFF } },
  34. { "AntiqueWhite", { 0xFA, 0xEB, 0xD7 } },
  35. { "Aqua", { 0x00, 0xFF, 0xFF } },
  36. { "Aquamarine", { 0x7F, 0xFF, 0xD4 } },
  37. { "Azure", { 0xF0, 0xFF, 0xFF } },
  38. { "Beige", { 0xF5, 0xF5, 0xDC } },
  39. { "Bisque", { 0xFF, 0xE4, 0xC4 } },
  40. { "Black", { 0x00, 0x00, 0x00 } },
  41. { "BlanchedAlmond", { 0xFF, 0xEB, 0xCD } },
  42. { "Blue", { 0x00, 0x00, 0xFF } },
  43. { "BlueViolet", { 0x8A, 0x2B, 0xE2 } },
  44. { "Brown", { 0xA5, 0x2A, 0x2A } },
  45. { "BurlyWood", { 0xDE, 0xB8, 0x87 } },
  46. { "CadetBlue", { 0x5F, 0x9E, 0xA0 } },
  47. { "Chartreuse", { 0x7F, 0xFF, 0x00 } },
  48. { "Chocolate", { 0xD2, 0x69, 0x1E } },
  49. { "Coral", { 0xFF, 0x7F, 0x50 } },
  50. { "CornflowerBlue", { 0x64, 0x95, 0xED } },
  51. { "Cornsilk", { 0xFF, 0xF8, 0xDC } },
  52. { "Crimson", { 0xDC, 0x14, 0x3C } },
  53. { "Cyan", { 0x00, 0xFF, 0xFF } },
  54. { "DarkBlue", { 0x00, 0x00, 0x8B } },
  55. { "DarkCyan", { 0x00, 0x8B, 0x8B } },
  56. { "DarkGoldenRod", { 0xB8, 0x86, 0x0B } },
  57. { "DarkGray", { 0xA9, 0xA9, 0xA9 } },
  58. { "DarkGreen", { 0x00, 0x64, 0x00 } },
  59. { "DarkKhaki", { 0xBD, 0xB7, 0x6B } },
  60. { "DarkMagenta", { 0x8B, 0x00, 0x8B } },
  61. { "DarkOliveGreen", { 0x55, 0x6B, 0x2F } },
  62. { "Darkorange", { 0xFF, 0x8C, 0x00 } },
  63. { "DarkOrchid", { 0x99, 0x32, 0xCC } },
  64. { "DarkRed", { 0x8B, 0x00, 0x00 } },
  65. { "DarkSalmon", { 0xE9, 0x96, 0x7A } },
  66. { "DarkSeaGreen", { 0x8F, 0xBC, 0x8F } },
  67. { "DarkSlateBlue", { 0x48, 0x3D, 0x8B } },
  68. { "DarkSlateGray", { 0x2F, 0x4F, 0x4F } },
  69. { "DarkTurquoise", { 0x00, 0xCE, 0xD1 } },
  70. { "DarkViolet", { 0x94, 0x00, 0xD3 } },
  71. { "DeepPink", { 0xFF, 0x14, 0x93 } },
  72. { "DeepSkyBlue", { 0x00, 0xBF, 0xFF } },
  73. { "DimGray", { 0x69, 0x69, 0x69 } },
  74. { "DodgerBlue", { 0x1E, 0x90, 0xFF } },
  75. { "FireBrick", { 0xB2, 0x22, 0x22 } },
  76. { "FloralWhite", { 0xFF, 0xFA, 0xF0 } },
  77. { "ForestGreen", { 0x22, 0x8B, 0x22 } },
  78. { "Fuchsia", { 0xFF, 0x00, 0xFF } },
  79. { "Gainsboro", { 0xDC, 0xDC, 0xDC } },
  80. { "GhostWhite", { 0xF8, 0xF8, 0xFF } },
  81. { "Gold", { 0xFF, 0xD7, 0x00 } },
  82. { "GoldenRod", { 0xDA, 0xA5, 0x20 } },
  83. { "Gray", { 0x80, 0x80, 0x80 } },
  84. { "Green", { 0x00, 0x80, 0x00 } },
  85. { "GreenYellow", { 0xAD, 0xFF, 0x2F } },
  86. { "HoneyDew", { 0xF0, 0xFF, 0xF0 } },
  87. { "HotPink", { 0xFF, 0x69, 0xB4 } },
  88. { "IndianRed", { 0xCD, 0x5C, 0x5C } },
  89. { "Indigo", { 0x4B, 0x00, 0x82 } },
  90. { "Ivory", { 0xFF, 0xFF, 0xF0 } },
  91. { "Khaki", { 0xF0, 0xE6, 0x8C } },
  92. { "Lavender", { 0xE6, 0xE6, 0xFA } },
  93. { "LavenderBlush", { 0xFF, 0xF0, 0xF5 } },
  94. { "LawnGreen", { 0x7C, 0xFC, 0x00 } },
  95. { "LemonChiffon", { 0xFF, 0xFA, 0xCD } },
  96. { "LightBlue", { 0xAD, 0xD8, 0xE6 } },
  97. { "LightCoral", { 0xF0, 0x80, 0x80 } },
  98. { "LightCyan", { 0xE0, 0xFF, 0xFF } },
  99. { "LightGoldenRodYellow", { 0xFA, 0xFA, 0xD2 } },
  100. { "LightGrey", { 0xD3, 0xD3, 0xD3 } },
  101. { "LightGreen", { 0x90, 0xEE, 0x90 } },
  102. { "LightPink", { 0xFF, 0xB6, 0xC1 } },
  103. { "LightSalmon", { 0xFF, 0xA0, 0x7A } },
  104. { "LightSeaGreen", { 0x20, 0xB2, 0xAA } },
  105. { "LightSkyBlue", { 0x87, 0xCE, 0xFA } },
  106. { "LightSlateGray", { 0x77, 0x88, 0x99 } },
  107. { "LightSteelBlue", { 0xB0, 0xC4, 0xDE } },
  108. { "LightYellow", { 0xFF, 0xFF, 0xE0 } },
  109. { "Lime", { 0x00, 0xFF, 0x00 } },
  110. { "LimeGreen", { 0x32, 0xCD, 0x32 } },
  111. { "Linen", { 0xFA, 0xF0, 0xE6 } },
  112. { "Magenta", { 0xFF, 0x00, 0xFF } },
  113. { "Maroon", { 0x80, 0x00, 0x00 } },
  114. { "MediumAquaMarine", { 0x66, 0xCD, 0xAA } },
  115. { "MediumBlue", { 0x00, 0x00, 0xCD } },
  116. { "MediumOrchid", { 0xBA, 0x55, 0xD3 } },
  117. { "MediumPurple", { 0x93, 0x70, 0xD8 } },
  118. { "MediumSeaGreen", { 0x3C, 0xB3, 0x71 } },
  119. { "MediumSlateBlue", { 0x7B, 0x68, 0xEE } },
  120. { "MediumSpringGreen", { 0x00, 0xFA, 0x9A } },
  121. { "MediumTurquoise", { 0x48, 0xD1, 0xCC } },
  122. { "MediumVioletRed", { 0xC7, 0x15, 0x85 } },
  123. { "MidnightBlue", { 0x19, 0x19, 0x70 } },
  124. { "MintCream", { 0xF5, 0xFF, 0xFA } },
  125. { "MistyRose", { 0xFF, 0xE4, 0xE1 } },
  126. { "Moccasin", { 0xFF, 0xE4, 0xB5 } },
  127. { "NavajoWhite", { 0xFF, 0xDE, 0xAD } },
  128. { "Navy", { 0x00, 0x00, 0x80 } },
  129. { "OldLace", { 0xFD, 0xF5, 0xE6 } },
  130. { "Olive", { 0x80, 0x80, 0x00 } },
  131. { "OliveDrab", { 0x6B, 0x8E, 0x23 } },
  132. { "Orange", { 0xFF, 0xA5, 0x00 } },
  133. { "OrangeRed", { 0xFF, 0x45, 0x00 } },
  134. { "Orchid", { 0xDA, 0x70, 0xD6 } },
  135. { "PaleGoldenRod", { 0xEE, 0xE8, 0xAA } },
  136. { "PaleGreen", { 0x98, 0xFB, 0x98 } },
  137. { "PaleTurquoise", { 0xAF, 0xEE, 0xEE } },
  138. { "PaleVioletRed", { 0xD8, 0x70, 0x93 } },
  139. { "PapayaWhip", { 0xFF, 0xEF, 0xD5 } },
  140. { "PeachPuff", { 0xFF, 0xDA, 0xB9 } },
  141. { "Peru", { 0xCD, 0x85, 0x3F } },
  142. { "Pink", { 0xFF, 0xC0, 0xCB } },
  143. { "Plum", { 0xDD, 0xA0, 0xDD } },
  144. { "PowderBlue", { 0xB0, 0xE0, 0xE6 } },
  145. { "Purple", { 0x80, 0x00, 0x80 } },
  146. { "Red", { 0xFF, 0x00, 0x00 } },
  147. { "RosyBrown", { 0xBC, 0x8F, 0x8F } },
  148. { "RoyalBlue", { 0x41, 0x69, 0xE1 } },
  149. { "SaddleBrown", { 0x8B, 0x45, 0x13 } },
  150. { "Salmon", { 0xFA, 0x80, 0x72 } },
  151. { "SandyBrown", { 0xF4, 0xA4, 0x60 } },
  152. { "SeaGreen", { 0x2E, 0x8B, 0x57 } },
  153. { "SeaShell", { 0xFF, 0xF5, 0xEE } },
  154. { "Sienna", { 0xA0, 0x52, 0x2D } },
  155. { "Silver", { 0xC0, 0xC0, 0xC0 } },
  156. { "SkyBlue", { 0x87, 0xCE, 0xEB } },
  157. { "SlateBlue", { 0x6A, 0x5A, 0xCD } },
  158. { "SlateGray", { 0x70, 0x80, 0x90 } },
  159. { "Snow", { 0xFF, 0xFA, 0xFA } },
  160. { "SpringGreen", { 0x00, 0xFF, 0x7F } },
  161. { "SteelBlue", { 0x46, 0x82, 0xB4 } },
  162. { "Tan", { 0xD2, 0xB4, 0x8C } },
  163. { "Teal", { 0x00, 0x80, 0x80 } },
  164. { "Thistle", { 0xD8, 0xBF, 0xD8 } },
  165. { "Tomato", { 0xFF, 0x63, 0x47 } },
  166. { "Turquoise", { 0x40, 0xE0, 0xD0 } },
  167. { "Violet", { 0xEE, 0x82, 0xEE } },
  168. { "Wheat", { 0xF5, 0xDE, 0xB3 } },
  169. { "White", { 0xFF, 0xFF, 0xFF } },
  170. { "WhiteSmoke", { 0xF5, 0xF5, 0xF5 } },
  171. { "Yellow", { 0xFF, 0xFF, 0x00 } },
  172. { "YellowGreen", { 0x9A, 0xCD, 0x32 } },
  173. };
  174. static int color_table_compare(const void *lhs, const void *rhs)
  175. {
  176. return strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
  177. }
  178. #define ALPHA_SEP '@'
  179. int av_parse_color(uint8_t *rgba_color, const char *color_string, void *log_ctx)
  180. {
  181. char *tail, color_string2[128];
  182. const ColorEntry *entry;
  183. av_strlcpy(color_string2, color_string, sizeof(color_string2));
  184. if ((tail = strchr(color_string2, ALPHA_SEP)))
  185. *tail++ = 0;
  186. rgba_color[3] = 255;
  187. if (!strcasecmp(color_string2, "random") || !strcasecmp(color_string2, "bikeshed")) {
  188. int rgba = av_get_random_seed();
  189. rgba_color[0] = rgba >> 24;
  190. rgba_color[1] = rgba >> 16;
  191. rgba_color[2] = rgba >> 8;
  192. rgba_color[3] = rgba;
  193. } else if (!strncmp(color_string2, "0x", 2)) {
  194. char *tail;
  195. int len = strlen(color_string2);
  196. unsigned int rgba = strtoul(color_string2, &tail, 16);
  197. if (*tail || (len != 8 && len != 10)) {
  198. av_log(log_ctx, AV_LOG_ERROR, "Invalid 0xRRGGBB[AA] color string: '%s'\n", color_string2);
  199. return AVERROR(EINVAL);
  200. }
  201. if (len == 10) {
  202. rgba_color[3] = rgba;
  203. rgba >>= 8;
  204. }
  205. rgba_color[0] = rgba >> 16;
  206. rgba_color[1] = rgba >> 8;
  207. rgba_color[2] = rgba;
  208. } else {
  209. entry = bsearch(color_string2,
  210. color_table,
  211. FF_ARRAY_ELEMS(color_table),
  212. sizeof(ColorEntry),
  213. color_table_compare);
  214. if (!entry) {
  215. av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string2);
  216. return AVERROR(EINVAL);
  217. }
  218. memcpy(rgba_color, entry->rgb_color, 3);
  219. }
  220. if (tail) {
  221. unsigned long int alpha;
  222. const char *alpha_string = tail;
  223. if (!strncmp(alpha_string, "0x", 2)) {
  224. alpha = strtoul(alpha_string, &tail, 16);
  225. } else {
  226. alpha = strtoul(alpha_string, &tail, 10);
  227. if (*tail) {
  228. double d = strtod(alpha_string, &tail);
  229. alpha = d * 255;
  230. }
  231. }
  232. if (tail == alpha_string || *tail || alpha > 255) {
  233. av_log(log_ctx, AV_LOG_ERROR, "Invalid alpha value specifier '%s' in '%s'\n",
  234. alpha_string, color_string);
  235. return AVERROR(EINVAL);
  236. }
  237. rgba_color[3] = alpha;
  238. }
  239. return 0;
  240. }
  241. /**
  242. * Store the value in the field in ctx that is named like key.
  243. * ctx must be an AVClass context, storing is done using AVOptions.
  244. *
  245. * @param buf the string to parse, buf will be updated to point at the
  246. * separator just after the parsed key/value pair
  247. * @param key_val_sep a 0-terminated list of characters used to
  248. * separate key from value
  249. * @param pairs_sep a 0-terminated list of characters used to separate
  250. * two pairs from each other
  251. * @return 0 if the key/value pair has been successfully parsed and
  252. * set, or a negative value corresponding to an AVERROR code in case
  253. * of error:
  254. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  255. * the error code issued by av_set_string3() if the key/value pair
  256. * cannot be set
  257. */
  258. static int parse_key_value_pair(void *ctx, const char **buf,
  259. const char *key_val_sep, const char *pairs_sep)
  260. {
  261. char *key = av_get_token(buf, key_val_sep);
  262. char *val;
  263. int ret;
  264. if (*key && strspn(*buf, key_val_sep)) {
  265. (*buf)++;
  266. val = av_get_token(buf, pairs_sep);
  267. } else {
  268. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  269. av_free(key);
  270. return AVERROR(EINVAL);
  271. }
  272. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  273. ret = av_set_string3(ctx, key, val, 1, NULL);
  274. if (ret == AVERROR(ENOENT))
  275. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  276. av_free(key);
  277. av_free(val);
  278. return ret;
  279. }
  280. int av_set_options_string(void *ctx, const char *opts,
  281. const char *key_val_sep, const char *pairs_sep)
  282. {
  283. int ret, count = 0;
  284. while (*opts) {
  285. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  286. return ret;
  287. count++;
  288. if (*opts)
  289. opts++;
  290. }
  291. return count;
  292. }
  293. #ifdef TEST
  294. #undef printf
  295. typedef struct TestContext
  296. {
  297. const AVClass *class;
  298. int num;
  299. int toggle;
  300. char *string;
  301. int flags;
  302. AVRational rational;
  303. } TestContext;
  304. #define OFFSET(x) offsetof(TestContext, x)
  305. #define TEST_FLAG_COOL 01
  306. #define TEST_FLAG_LAME 02
  307. #define TEST_FLAG_MU 04
  308. static const AVOption test_options[]= {
  309. {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, 0, 0, 100 },
  310. {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, 0, 0, 1 },
  311. {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, 0, 0, 10 },
  312. {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  313. {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, 0, 0, INT_MAX, 0, "flags" },
  314. {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_COOL, INT_MIN, INT_MAX, 0, "flags" },
  315. {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_LAME, INT_MIN, INT_MAX, 0, "flags" },
  316. {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_MU, INT_MIN, INT_MAX, 0, "flags" },
  317. {NULL},
  318. };
  319. static const char *test_get_name(void *ctx)
  320. {
  321. return "test";
  322. }
  323. static const AVClass test_class = {
  324. "TestContext",
  325. test_get_name,
  326. test_options
  327. };
  328. int main(void)
  329. {
  330. int i;
  331. printf("\nTesting av_parse_color()\n");
  332. {
  333. uint8_t rgba[4];
  334. const char *color_names[] = {
  335. "bikeshed",
  336. "RaNdOm",
  337. "foo",
  338. "red",
  339. "Red ",
  340. "RED",
  341. "Violet",
  342. "Yellow",
  343. "Red",
  344. "0x000000",
  345. "0x0000000",
  346. "0xff000000",
  347. "0x3e34ff",
  348. "0x3e34ffaa",
  349. "0xffXXee",
  350. "0xfoobar",
  351. "0xffffeeeeeeee",
  352. "red@foo",
  353. "random@10",
  354. "0xff0000@1.0",
  355. "red@",
  356. "red@0xfff",
  357. "red@0xf",
  358. "red@2",
  359. "red@0.1",
  360. "red@-1",
  361. "red@0.5",
  362. "red@1.0",
  363. "red@256",
  364. "red@10foo",
  365. "red@-1.0",
  366. "red@-0.0",
  367. };
  368. av_log_set_level(AV_LOG_DEBUG);
  369. for (int i = 0; i < FF_ARRAY_ELEMS(color_names); i++) {
  370. if (av_parse_color(rgba, color_names[i], NULL) >= 0)
  371. printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
  372. }
  373. }
  374. printf("\nTesting av_set_options_string()\n");
  375. {
  376. TestContext test_ctx;
  377. const char *options[] = {
  378. "",
  379. ":",
  380. "=",
  381. "foo=:",
  382. ":=foo",
  383. "=foo",
  384. "foo=",
  385. "foo",
  386. "foo=val",
  387. "foo==val",
  388. "toggle=:",
  389. "string=:",
  390. "toggle=1 : foo",
  391. "toggle=100",
  392. "toggle==1",
  393. "flags=+mu-lame : num=42: toggle=0",
  394. "num=42 : string=blahblah",
  395. "rational=0 : rational=1/2 : rational=1/-1",
  396. "rational=-1/0",
  397. };
  398. test_ctx.class = &test_class;
  399. av_opt_set_defaults2(&test_ctx, 0, 0);
  400. test_ctx.string = av_strdup("default");
  401. av_log_set_level(AV_LOG_DEBUG);
  402. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  403. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  404. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  405. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  406. printf("\n");
  407. }
  408. }
  409. return 0;
  410. }
  411. #endif