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.

522 lines
18KB

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