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.

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