parseutils.c 17 KB

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