parseutils.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * misc parsing utilities for libavcore
  21. */
  22. #include <strings.h>
  23. #include "parseutils.h"
  24. #include "libavutil/avutil.h"
  25. #include "libavutil/eval.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/random_seed.h"
  28. typedef struct {
  29. const char *abbr;
  30. int width, height;
  31. } VideoSizeAbbr;
  32. typedef struct {
  33. const char *abbr;
  34. AVRational rate;
  35. } VideoRateAbbr;
  36. static const VideoSizeAbbr video_size_abbrs[] = {
  37. { "ntsc", 720, 480 },
  38. { "pal", 720, 576 },
  39. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  40. { "qpal", 352, 288 }, /* VCD compliant PAL */
  41. { "sntsc", 640, 480 }, /* square pixel NTSC */
  42. { "spal", 768, 576 }, /* square pixel PAL */
  43. { "film", 352, 240 },
  44. { "ntsc-film", 352, 240 },
  45. { "sqcif", 128, 96 },
  46. { "qcif", 176, 144 },
  47. { "cif", 352, 288 },
  48. { "4cif", 704, 576 },
  49. { "16cif", 1408,1152 },
  50. { "qqvga", 160, 120 },
  51. { "qvga", 320, 240 },
  52. { "vga", 640, 480 },
  53. { "svga", 800, 600 },
  54. { "xga", 1024, 768 },
  55. { "uxga", 1600,1200 },
  56. { "qxga", 2048,1536 },
  57. { "sxga", 1280,1024 },
  58. { "qsxga", 2560,2048 },
  59. { "hsxga", 5120,4096 },
  60. { "wvga", 852, 480 },
  61. { "wxga", 1366, 768 },
  62. { "wsxga", 1600,1024 },
  63. { "wuxga", 1920,1200 },
  64. { "woxga", 2560,1600 },
  65. { "wqsxga", 3200,2048 },
  66. { "wquxga", 3840,2400 },
  67. { "whsxga", 6400,4096 },
  68. { "whuxga", 7680,4800 },
  69. { "cga", 320, 200 },
  70. { "ega", 640, 350 },
  71. { "hd480", 852, 480 },
  72. { "hd720", 1280, 720 },
  73. { "hd1080", 1920,1080 },
  74. };
  75. static const VideoRateAbbr video_rate_abbrs[]= {
  76. { "ntsc", { 30000, 1001 } },
  77. { "pal", { 25, 1 } },
  78. { "qntsc", { 30000, 1001 } }, /* VCD compliant NTSC */
  79. { "qpal", { 25, 1 } }, /* VCD compliant PAL */
  80. { "sntsc", { 30000, 1001 } }, /* square pixel NTSC */
  81. { "spal", { 25, 1 } }, /* square pixel PAL */
  82. { "film", { 24, 1 } },
  83. { "ntsc-film", { 24000, 1001 } },
  84. };
  85. int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
  86. {
  87. int i;
  88. int n = FF_ARRAY_ELEMS(video_size_abbrs);
  89. char *p;
  90. int width = 0, height = 0;
  91. for (i = 0; i < n; i++) {
  92. if (!strcmp(video_size_abbrs[i].abbr, str)) {
  93. width = video_size_abbrs[i].width;
  94. height = video_size_abbrs[i].height;
  95. break;
  96. }
  97. }
  98. if (i == n) {
  99. p = str;
  100. width = strtol(p, &p, 10);
  101. if (*p)
  102. p++;
  103. height = strtol(p, &p, 10);
  104. }
  105. if (width <= 0 || height <= 0)
  106. return AVERROR(EINVAL);
  107. *width_ptr = width;
  108. *height_ptr = height;
  109. return 0;
  110. }
  111. int av_parse_video_rate(AVRational *rate, const char *arg)
  112. {
  113. int i, ret;
  114. int n = FF_ARRAY_ELEMS(video_rate_abbrs);
  115. double res;
  116. /* First, we check our abbreviation table */
  117. for (i = 0; i < n; ++i)
  118. if (!strcmp(video_rate_abbrs[i].abbr, arg)) {
  119. *rate = video_rate_abbrs[i].rate;
  120. return 0;
  121. }
  122. /* Then, we try to parse it as fraction */
  123. if ((ret = av_expr_parse_and_eval(&res, arg, NULL, NULL, NULL, NULL, NULL, NULL,
  124. NULL, 0, NULL)) < 0)
  125. return ret;
  126. *rate = av_d2q(res, 1001000);
  127. if (rate->num <= 0 || rate->den <= 0)
  128. return AVERROR(EINVAL);
  129. return 0;
  130. }
  131. typedef struct {
  132. const char *name; ///< a string representing the name of the color
  133. uint8_t rgb_color[3]; ///< RGB values for the color
  134. } ColorEntry;
  135. static ColorEntry color_table[] = {
  136. { "AliceBlue", { 0xF0, 0xF8, 0xFF } },
  137. { "AntiqueWhite", { 0xFA, 0xEB, 0xD7 } },
  138. { "Aqua", { 0x00, 0xFF, 0xFF } },
  139. { "Aquamarine", { 0x7F, 0xFF, 0xD4 } },
  140. { "Azure", { 0xF0, 0xFF, 0xFF } },
  141. { "Beige", { 0xF5, 0xF5, 0xDC } },
  142. { "Bisque", { 0xFF, 0xE4, 0xC4 } },
  143. { "Black", { 0x00, 0x00, 0x00 } },
  144. { "BlanchedAlmond", { 0xFF, 0xEB, 0xCD } },
  145. { "Blue", { 0x00, 0x00, 0xFF } },
  146. { "BlueViolet", { 0x8A, 0x2B, 0xE2 } },
  147. { "Brown", { 0xA5, 0x2A, 0x2A } },
  148. { "BurlyWood", { 0xDE, 0xB8, 0x87 } },
  149. { "CadetBlue", { 0x5F, 0x9E, 0xA0 } },
  150. { "Chartreuse", { 0x7F, 0xFF, 0x00 } },
  151. { "Chocolate", { 0xD2, 0x69, 0x1E } },
  152. { "Coral", { 0xFF, 0x7F, 0x50 } },
  153. { "CornflowerBlue", { 0x64, 0x95, 0xED } },
  154. { "Cornsilk", { 0xFF, 0xF8, 0xDC } },
  155. { "Crimson", { 0xDC, 0x14, 0x3C } },
  156. { "Cyan", { 0x00, 0xFF, 0xFF } },
  157. { "DarkBlue", { 0x00, 0x00, 0x8B } },
  158. { "DarkCyan", { 0x00, 0x8B, 0x8B } },
  159. { "DarkGoldenRod", { 0xB8, 0x86, 0x0B } },
  160. { "DarkGray", { 0xA9, 0xA9, 0xA9 } },
  161. { "DarkGreen", { 0x00, 0x64, 0x00 } },
  162. { "DarkKhaki", { 0xBD, 0xB7, 0x6B } },
  163. { "DarkMagenta", { 0x8B, 0x00, 0x8B } },
  164. { "DarkOliveGreen", { 0x55, 0x6B, 0x2F } },
  165. { "Darkorange", { 0xFF, 0x8C, 0x00 } },
  166. { "DarkOrchid", { 0x99, 0x32, 0xCC } },
  167. { "DarkRed", { 0x8B, 0x00, 0x00 } },
  168. { "DarkSalmon", { 0xE9, 0x96, 0x7A } },
  169. { "DarkSeaGreen", { 0x8F, 0xBC, 0x8F } },
  170. { "DarkSlateBlue", { 0x48, 0x3D, 0x8B } },
  171. { "DarkSlateGray", { 0x2F, 0x4F, 0x4F } },
  172. { "DarkTurquoise", { 0x00, 0xCE, 0xD1 } },
  173. { "DarkViolet", { 0x94, 0x00, 0xD3 } },
  174. { "DeepPink", { 0xFF, 0x14, 0x93 } },
  175. { "DeepSkyBlue", { 0x00, 0xBF, 0xFF } },
  176. { "DimGray", { 0x69, 0x69, 0x69 } },
  177. { "DodgerBlue", { 0x1E, 0x90, 0xFF } },
  178. { "FireBrick", { 0xB2, 0x22, 0x22 } },
  179. { "FloralWhite", { 0xFF, 0xFA, 0xF0 } },
  180. { "ForestGreen", { 0x22, 0x8B, 0x22 } },
  181. { "Fuchsia", { 0xFF, 0x00, 0xFF } },
  182. { "Gainsboro", { 0xDC, 0xDC, 0xDC } },
  183. { "GhostWhite", { 0xF8, 0xF8, 0xFF } },
  184. { "Gold", { 0xFF, 0xD7, 0x00 } },
  185. { "GoldenRod", { 0xDA, 0xA5, 0x20 } },
  186. { "Gray", { 0x80, 0x80, 0x80 } },
  187. { "Green", { 0x00, 0x80, 0x00 } },
  188. { "GreenYellow", { 0xAD, 0xFF, 0x2F } },
  189. { "HoneyDew", { 0xF0, 0xFF, 0xF0 } },
  190. { "HotPink", { 0xFF, 0x69, 0xB4 } },
  191. { "IndianRed", { 0xCD, 0x5C, 0x5C } },
  192. { "Indigo", { 0x4B, 0x00, 0x82 } },
  193. { "Ivory", { 0xFF, 0xFF, 0xF0 } },
  194. { "Khaki", { 0xF0, 0xE6, 0x8C } },
  195. { "Lavender", { 0xE6, 0xE6, 0xFA } },
  196. { "LavenderBlush", { 0xFF, 0xF0, 0xF5 } },
  197. { "LawnGreen", { 0x7C, 0xFC, 0x00 } },
  198. { "LemonChiffon", { 0xFF, 0xFA, 0xCD } },
  199. { "LightBlue", { 0xAD, 0xD8, 0xE6 } },
  200. { "LightCoral", { 0xF0, 0x80, 0x80 } },
  201. { "LightCyan", { 0xE0, 0xFF, 0xFF } },
  202. { "LightGoldenRodYellow", { 0xFA, 0xFA, 0xD2 } },
  203. { "LightGrey", { 0xD3, 0xD3, 0xD3 } },
  204. { "LightGreen", { 0x90, 0xEE, 0x90 } },
  205. { "LightPink", { 0xFF, 0xB6, 0xC1 } },
  206. { "LightSalmon", { 0xFF, 0xA0, 0x7A } },
  207. { "LightSeaGreen", { 0x20, 0xB2, 0xAA } },
  208. { "LightSkyBlue", { 0x87, 0xCE, 0xFA } },
  209. { "LightSlateGray", { 0x77, 0x88, 0x99 } },
  210. { "LightSteelBlue", { 0xB0, 0xC4, 0xDE } },
  211. { "LightYellow", { 0xFF, 0xFF, 0xE0 } },
  212. { "Lime", { 0x00, 0xFF, 0x00 } },
  213. { "LimeGreen", { 0x32, 0xCD, 0x32 } },
  214. { "Linen", { 0xFA, 0xF0, 0xE6 } },
  215. { "Magenta", { 0xFF, 0x00, 0xFF } },
  216. { "Maroon", { 0x80, 0x00, 0x00 } },
  217. { "MediumAquaMarine", { 0x66, 0xCD, 0xAA } },
  218. { "MediumBlue", { 0x00, 0x00, 0xCD } },
  219. { "MediumOrchid", { 0xBA, 0x55, 0xD3 } },
  220. { "MediumPurple", { 0x93, 0x70, 0xD8 } },
  221. { "MediumSeaGreen", { 0x3C, 0xB3, 0x71 } },
  222. { "MediumSlateBlue", { 0x7B, 0x68, 0xEE } },
  223. { "MediumSpringGreen", { 0x00, 0xFA, 0x9A } },
  224. { "MediumTurquoise", { 0x48, 0xD1, 0xCC } },
  225. { "MediumVioletRed", { 0xC7, 0x15, 0x85 } },
  226. { "MidnightBlue", { 0x19, 0x19, 0x70 } },
  227. { "MintCream", { 0xF5, 0xFF, 0xFA } },
  228. { "MistyRose", { 0xFF, 0xE4, 0xE1 } },
  229. { "Moccasin", { 0xFF, 0xE4, 0xB5 } },
  230. { "NavajoWhite", { 0xFF, 0xDE, 0xAD } },
  231. { "Navy", { 0x00, 0x00, 0x80 } },
  232. { "OldLace", { 0xFD, 0xF5, 0xE6 } },
  233. { "Olive", { 0x80, 0x80, 0x00 } },
  234. { "OliveDrab", { 0x6B, 0x8E, 0x23 } },
  235. { "Orange", { 0xFF, 0xA5, 0x00 } },
  236. { "OrangeRed", { 0xFF, 0x45, 0x00 } },
  237. { "Orchid", { 0xDA, 0x70, 0xD6 } },
  238. { "PaleGoldenRod", { 0xEE, 0xE8, 0xAA } },
  239. { "PaleGreen", { 0x98, 0xFB, 0x98 } },
  240. { "PaleTurquoise", { 0xAF, 0xEE, 0xEE } },
  241. { "PaleVioletRed", { 0xD8, 0x70, 0x93 } },
  242. { "PapayaWhip", { 0xFF, 0xEF, 0xD5 } },
  243. { "PeachPuff", { 0xFF, 0xDA, 0xB9 } },
  244. { "Peru", { 0xCD, 0x85, 0x3F } },
  245. { "Pink", { 0xFF, 0xC0, 0xCB } },
  246. { "Plum", { 0xDD, 0xA0, 0xDD } },
  247. { "PowderBlue", { 0xB0, 0xE0, 0xE6 } },
  248. { "Purple", { 0x80, 0x00, 0x80 } },
  249. { "Red", { 0xFF, 0x00, 0x00 } },
  250. { "RosyBrown", { 0xBC, 0x8F, 0x8F } },
  251. { "RoyalBlue", { 0x41, 0x69, 0xE1 } },
  252. { "SaddleBrown", { 0x8B, 0x45, 0x13 } },
  253. { "Salmon", { 0xFA, 0x80, 0x72 } },
  254. { "SandyBrown", { 0xF4, 0xA4, 0x60 } },
  255. { "SeaGreen", { 0x2E, 0x8B, 0x57 } },
  256. { "SeaShell", { 0xFF, 0xF5, 0xEE } },
  257. { "Sienna", { 0xA0, 0x52, 0x2D } },
  258. { "Silver", { 0xC0, 0xC0, 0xC0 } },
  259. { "SkyBlue", { 0x87, 0xCE, 0xEB } },
  260. { "SlateBlue", { 0x6A, 0x5A, 0xCD } },
  261. { "SlateGray", { 0x70, 0x80, 0x90 } },
  262. { "Snow", { 0xFF, 0xFA, 0xFA } },
  263. { "SpringGreen", { 0x00, 0xFF, 0x7F } },
  264. { "SteelBlue", { 0x46, 0x82, 0xB4 } },
  265. { "Tan", { 0xD2, 0xB4, 0x8C } },
  266. { "Teal", { 0x00, 0x80, 0x80 } },
  267. { "Thistle", { 0xD8, 0xBF, 0xD8 } },
  268. { "Tomato", { 0xFF, 0x63, 0x47 } },
  269. { "Turquoise", { 0x40, 0xE0, 0xD0 } },
  270. { "Violet", { 0xEE, 0x82, 0xEE } },
  271. { "Wheat", { 0xF5, 0xDE, 0xB3 } },
  272. { "White", { 0xFF, 0xFF, 0xFF } },
  273. { "WhiteSmoke", { 0xF5, 0xF5, 0xF5 } },
  274. { "Yellow", { 0xFF, 0xFF, 0x00 } },
  275. { "YellowGreen", { 0x9A, 0xCD, 0x32 } },
  276. };
  277. static int color_table_compare(const void *lhs, const void *rhs)
  278. {
  279. return strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
  280. }
  281. #define ALPHA_SEP '@'
  282. int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen,
  283. void *log_ctx)
  284. {
  285. char *tail, color_string2[128];
  286. const ColorEntry *entry;
  287. int len, hex_offset = 0;
  288. if (color_string[0] == '#') {
  289. hex_offset = 1;
  290. } else if (!strncmp(color_string, "0x", 2))
  291. hex_offset = 2;
  292. if (slen < 0)
  293. slen = strlen(color_string);
  294. av_strlcpy(color_string2, color_string + hex_offset,
  295. FFMIN(slen-hex_offset+1, sizeof(color_string2)));
  296. if ((tail = strchr(color_string2, ALPHA_SEP)))
  297. *tail++ = 0;
  298. len = strlen(color_string2);
  299. rgba_color[3] = 255;
  300. if (!strcasecmp(color_string2, "random") || !strcasecmp(color_string2, "bikeshed")) {
  301. int rgba = av_get_random_seed();
  302. rgba_color[0] = rgba >> 24;
  303. rgba_color[1] = rgba >> 16;
  304. rgba_color[2] = rgba >> 8;
  305. rgba_color[3] = rgba;
  306. } else if (hex_offset ||
  307. strspn(color_string2, "0123456789ABCDEFabcdef") == len) {
  308. char *tail;
  309. unsigned int rgba = strtoul(color_string2, &tail, 16);
  310. if (*tail || (len != 6 && len != 8)) {
  311. av_log(log_ctx, AV_LOG_ERROR, "Invalid 0xRRGGBB[AA] color string: '%s'\n", color_string2);
  312. return AVERROR(EINVAL);
  313. }
  314. if (len == 8) {
  315. rgba_color[3] = rgba;
  316. rgba >>= 8;
  317. }
  318. rgba_color[0] = rgba >> 16;
  319. rgba_color[1] = rgba >> 8;
  320. rgba_color[2] = rgba;
  321. } else {
  322. entry = bsearch(color_string2,
  323. color_table,
  324. FF_ARRAY_ELEMS(color_table),
  325. sizeof(ColorEntry),
  326. color_table_compare);
  327. if (!entry) {
  328. av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string2);
  329. return AVERROR(EINVAL);
  330. }
  331. memcpy(rgba_color, entry->rgb_color, 3);
  332. }
  333. if (tail) {
  334. unsigned long int alpha;
  335. const char *alpha_string = tail;
  336. if (!strncmp(alpha_string, "0x", 2)) {
  337. alpha = strtoul(alpha_string, &tail, 16);
  338. } else {
  339. alpha = 255 * strtod(alpha_string, &tail);
  340. }
  341. if (tail == alpha_string || *tail || alpha > 255) {
  342. av_log(log_ctx, AV_LOG_ERROR, "Invalid alpha value specifier '%s' in '%s'\n",
  343. alpha_string, color_string);
  344. return AVERROR(EINVAL);
  345. }
  346. rgba_color[3] = alpha;
  347. }
  348. return 0;
  349. }
  350. #ifdef TEST
  351. #undef printf
  352. int main(void)
  353. {
  354. printf("Testing av_parse_video_rate()\n");
  355. {
  356. int i;
  357. const char *rates[] = {
  358. "-inf",
  359. "inf",
  360. "nan",
  361. "123/0",
  362. "-123 / 0",
  363. "",
  364. "/",
  365. " 123 / 321",
  366. "foo/foo",
  367. "foo/1",
  368. "1/foo",
  369. "0/0",
  370. "/0",
  371. "1/",
  372. "1",
  373. "0",
  374. "-123/123",
  375. "-foo",
  376. "123.23",
  377. ".23",
  378. "-.23",
  379. "-0.234",
  380. "-0.0000001",
  381. " 21332.2324 ",
  382. " -21332.2324 ",
  383. };
  384. for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) {
  385. int ret;
  386. AVRational q = (AVRational){0, 0};
  387. ret = av_parse_video_rate(&q, rates[i]),
  388. printf("'%s' -> %d/%d ret:%d\n",
  389. rates[i], q.num, q.den, ret);
  390. }
  391. }
  392. printf("\nTesting av_parse_color()\n");
  393. {
  394. int i;
  395. uint8_t rgba[4];
  396. const char *color_names[] = {
  397. "bikeshed",
  398. "RaNdOm",
  399. "foo",
  400. "red",
  401. "Red ",
  402. "RED",
  403. "Violet",
  404. "Yellow",
  405. "Red",
  406. "0x000000",
  407. "0x0000000",
  408. "0xff000000",
  409. "0x3e34ff",
  410. "0x3e34ffaa",
  411. "0xffXXee",
  412. "0xfoobar",
  413. "0xffffeeeeeeee",
  414. "#ff0000",
  415. "#ffXX00",
  416. "ff0000",
  417. "ffXX00",
  418. "red@foo",
  419. "random@10",
  420. "0xff0000@1.0",
  421. "red@",
  422. "red@0xfff",
  423. "red@0xf",
  424. "red@2",
  425. "red@0.1",
  426. "red@-1",
  427. "red@0.5",
  428. "red@1.0",
  429. "red@256",
  430. "red@10foo",
  431. "red@-1.0",
  432. "red@-0.0",
  433. };
  434. av_log_set_level(AV_LOG_DEBUG);
  435. for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) {
  436. if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0)
  437. printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
  438. }
  439. }
  440. return 0;
  441. }
  442. #endif /* TEST */