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.

741 lines
23KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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
  21. */
  22. #include <time.h>
  23. #include "avstring.h"
  24. #include "avutil.h"
  25. #include "common.h"
  26. #include "eval.h"
  27. #include "log.h"
  28. #include "random_seed.h"
  29. #include "parseutils.h"
  30. typedef struct {
  31. const char *abbr;
  32. int width, height;
  33. } VideoSizeAbbr;
  34. typedef struct {
  35. const char *abbr;
  36. AVRational rate;
  37. } VideoRateAbbr;
  38. static const VideoSizeAbbr video_size_abbrs[] = {
  39. { "ntsc", 720, 480 },
  40. { "pal", 720, 576 },
  41. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  42. { "qpal", 352, 288 }, /* VCD compliant PAL */
  43. { "sntsc", 640, 480 }, /* square pixel NTSC */
  44. { "spal", 768, 576 }, /* square pixel PAL */
  45. { "film", 352, 240 },
  46. { "ntsc-film", 352, 240 },
  47. { "sqcif", 128, 96 },
  48. { "qcif", 176, 144 },
  49. { "cif", 352, 288 },
  50. { "4cif", 704, 576 },
  51. { "16cif", 1408,1152 },
  52. { "qqvga", 160, 120 },
  53. { "qvga", 320, 240 },
  54. { "vga", 640, 480 },
  55. { "svga", 800, 600 },
  56. { "xga", 1024, 768 },
  57. { "uxga", 1600,1200 },
  58. { "qxga", 2048,1536 },
  59. { "sxga", 1280,1024 },
  60. { "qsxga", 2560,2048 },
  61. { "hsxga", 5120,4096 },
  62. { "wvga", 852, 480 },
  63. { "wxga", 1366, 768 },
  64. { "wsxga", 1600,1024 },
  65. { "wuxga", 1920,1200 },
  66. { "woxga", 2560,1600 },
  67. { "wqsxga", 3200,2048 },
  68. { "wquxga", 3840,2400 },
  69. { "whsxga", 6400,4096 },
  70. { "whuxga", 7680,4800 },
  71. { "cga", 320, 200 },
  72. { "ega", 640, 350 },
  73. { "hd480", 852, 480 },
  74. { "hd720", 1280, 720 },
  75. { "hd1080", 1920,1080 },
  76. };
  77. static const VideoRateAbbr video_rate_abbrs[]= {
  78. { "ntsc", { 30000, 1001 } },
  79. { "pal", { 25, 1 } },
  80. { "qntsc", { 30000, 1001 } }, /* VCD compliant NTSC */
  81. { "qpal", { 25, 1 } }, /* VCD compliant PAL */
  82. { "sntsc", { 30000, 1001 } }, /* square pixel NTSC */
  83. { "spal", { 25, 1 } }, /* square pixel PAL */
  84. { "film", { 24, 1 } },
  85. { "ntsc-film", { 24000, 1001 } },
  86. };
  87. int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
  88. {
  89. int i;
  90. int n = FF_ARRAY_ELEMS(video_size_abbrs);
  91. char *p;
  92. int width = 0, height = 0;
  93. for (i = 0; i < n; i++) {
  94. if (!strcmp(video_size_abbrs[i].abbr, str)) {
  95. width = video_size_abbrs[i].width;
  96. height = video_size_abbrs[i].height;
  97. break;
  98. }
  99. }
  100. if (i == n) {
  101. width = strtol(str, &p, 10);
  102. if (*p)
  103. p++;
  104. height = strtol(p, &p, 10);
  105. }
  106. if (width <= 0 || height <= 0)
  107. return AVERROR(EINVAL);
  108. *width_ptr = width;
  109. *height_ptr = height;
  110. return 0;
  111. }
  112. int av_parse_video_rate(AVRational *rate, const char *arg)
  113. {
  114. int i, ret;
  115. int n = FF_ARRAY_ELEMS(video_rate_abbrs);
  116. double res;
  117. /* First, we check our abbreviation table */
  118. for (i = 0; i < n; ++i)
  119. if (!strcmp(video_rate_abbrs[i].abbr, arg)) {
  120. *rate = video_rate_abbrs[i].rate;
  121. return 0;
  122. }
  123. /* Then, we try to parse it as fraction */
  124. if ((ret = av_expr_parse_and_eval(&res, arg, NULL, NULL, NULL, NULL, NULL, NULL,
  125. NULL, 0, NULL)) < 0)
  126. return ret;
  127. *rate = av_d2q(res, 1001000);
  128. if (rate->num <= 0 || rate->den <= 0)
  129. return AVERROR(EINVAL);
  130. return 0;
  131. }
  132. typedef struct {
  133. const char *name; ///< a string representing the name of the color
  134. uint8_t rgb_color[3]; ///< RGB values for the color
  135. } ColorEntry;
  136. static ColorEntry color_table[] = {
  137. { "AliceBlue", { 0xF0, 0xF8, 0xFF } },
  138. { "AntiqueWhite", { 0xFA, 0xEB, 0xD7 } },
  139. { "Aqua", { 0x00, 0xFF, 0xFF } },
  140. { "Aquamarine", { 0x7F, 0xFF, 0xD4 } },
  141. { "Azure", { 0xF0, 0xFF, 0xFF } },
  142. { "Beige", { 0xF5, 0xF5, 0xDC } },
  143. { "Bisque", { 0xFF, 0xE4, 0xC4 } },
  144. { "Black", { 0x00, 0x00, 0x00 } },
  145. { "BlanchedAlmond", { 0xFF, 0xEB, 0xCD } },
  146. { "Blue", { 0x00, 0x00, 0xFF } },
  147. { "BlueViolet", { 0x8A, 0x2B, 0xE2 } },
  148. { "Brown", { 0xA5, 0x2A, 0x2A } },
  149. { "BurlyWood", { 0xDE, 0xB8, 0x87 } },
  150. { "CadetBlue", { 0x5F, 0x9E, 0xA0 } },
  151. { "Chartreuse", { 0x7F, 0xFF, 0x00 } },
  152. { "Chocolate", { 0xD2, 0x69, 0x1E } },
  153. { "Coral", { 0xFF, 0x7F, 0x50 } },
  154. { "CornflowerBlue", { 0x64, 0x95, 0xED } },
  155. { "Cornsilk", { 0xFF, 0xF8, 0xDC } },
  156. { "Crimson", { 0xDC, 0x14, 0x3C } },
  157. { "Cyan", { 0x00, 0xFF, 0xFF } },
  158. { "DarkBlue", { 0x00, 0x00, 0x8B } },
  159. { "DarkCyan", { 0x00, 0x8B, 0x8B } },
  160. { "DarkGoldenRod", { 0xB8, 0x86, 0x0B } },
  161. { "DarkGray", { 0xA9, 0xA9, 0xA9 } },
  162. { "DarkGreen", { 0x00, 0x64, 0x00 } },
  163. { "DarkKhaki", { 0xBD, 0xB7, 0x6B } },
  164. { "DarkMagenta", { 0x8B, 0x00, 0x8B } },
  165. { "DarkOliveGreen", { 0x55, 0x6B, 0x2F } },
  166. { "Darkorange", { 0xFF, 0x8C, 0x00 } },
  167. { "DarkOrchid", { 0x99, 0x32, 0xCC } },
  168. { "DarkRed", { 0x8B, 0x00, 0x00 } },
  169. { "DarkSalmon", { 0xE9, 0x96, 0x7A } },
  170. { "DarkSeaGreen", { 0x8F, 0xBC, 0x8F } },
  171. { "DarkSlateBlue", { 0x48, 0x3D, 0x8B } },
  172. { "DarkSlateGray", { 0x2F, 0x4F, 0x4F } },
  173. { "DarkTurquoise", { 0x00, 0xCE, 0xD1 } },
  174. { "DarkViolet", { 0x94, 0x00, 0xD3 } },
  175. { "DeepPink", { 0xFF, 0x14, 0x93 } },
  176. { "DeepSkyBlue", { 0x00, 0xBF, 0xFF } },
  177. { "DimGray", { 0x69, 0x69, 0x69 } },
  178. { "DodgerBlue", { 0x1E, 0x90, 0xFF } },
  179. { "FireBrick", { 0xB2, 0x22, 0x22 } },
  180. { "FloralWhite", { 0xFF, 0xFA, 0xF0 } },
  181. { "ForestGreen", { 0x22, 0x8B, 0x22 } },
  182. { "Fuchsia", { 0xFF, 0x00, 0xFF } },
  183. { "Gainsboro", { 0xDC, 0xDC, 0xDC } },
  184. { "GhostWhite", { 0xF8, 0xF8, 0xFF } },
  185. { "Gold", { 0xFF, 0xD7, 0x00 } },
  186. { "GoldenRod", { 0xDA, 0xA5, 0x20 } },
  187. { "Gray", { 0x80, 0x80, 0x80 } },
  188. { "Green", { 0x00, 0x80, 0x00 } },
  189. { "GreenYellow", { 0xAD, 0xFF, 0x2F } },
  190. { "HoneyDew", { 0xF0, 0xFF, 0xF0 } },
  191. { "HotPink", { 0xFF, 0x69, 0xB4 } },
  192. { "IndianRed", { 0xCD, 0x5C, 0x5C } },
  193. { "Indigo", { 0x4B, 0x00, 0x82 } },
  194. { "Ivory", { 0xFF, 0xFF, 0xF0 } },
  195. { "Khaki", { 0xF0, 0xE6, 0x8C } },
  196. { "Lavender", { 0xE6, 0xE6, 0xFA } },
  197. { "LavenderBlush", { 0xFF, 0xF0, 0xF5 } },
  198. { "LawnGreen", { 0x7C, 0xFC, 0x00 } },
  199. { "LemonChiffon", { 0xFF, 0xFA, 0xCD } },
  200. { "LightBlue", { 0xAD, 0xD8, 0xE6 } },
  201. { "LightCoral", { 0xF0, 0x80, 0x80 } },
  202. { "LightCyan", { 0xE0, 0xFF, 0xFF } },
  203. { "LightGoldenRodYellow", { 0xFA, 0xFA, 0xD2 } },
  204. { "LightGrey", { 0xD3, 0xD3, 0xD3 } },
  205. { "LightGreen", { 0x90, 0xEE, 0x90 } },
  206. { "LightPink", { 0xFF, 0xB6, 0xC1 } },
  207. { "LightSalmon", { 0xFF, 0xA0, 0x7A } },
  208. { "LightSeaGreen", { 0x20, 0xB2, 0xAA } },
  209. { "LightSkyBlue", { 0x87, 0xCE, 0xFA } },
  210. { "LightSlateGray", { 0x77, 0x88, 0x99 } },
  211. { "LightSteelBlue", { 0xB0, 0xC4, 0xDE } },
  212. { "LightYellow", { 0xFF, 0xFF, 0xE0 } },
  213. { "Lime", { 0x00, 0xFF, 0x00 } },
  214. { "LimeGreen", { 0x32, 0xCD, 0x32 } },
  215. { "Linen", { 0xFA, 0xF0, 0xE6 } },
  216. { "Magenta", { 0xFF, 0x00, 0xFF } },
  217. { "Maroon", { 0x80, 0x00, 0x00 } },
  218. { "MediumAquaMarine", { 0x66, 0xCD, 0xAA } },
  219. { "MediumBlue", { 0x00, 0x00, 0xCD } },
  220. { "MediumOrchid", { 0xBA, 0x55, 0xD3 } },
  221. { "MediumPurple", { 0x93, 0x70, 0xD8 } },
  222. { "MediumSeaGreen", { 0x3C, 0xB3, 0x71 } },
  223. { "MediumSlateBlue", { 0x7B, 0x68, 0xEE } },
  224. { "MediumSpringGreen", { 0x00, 0xFA, 0x9A } },
  225. { "MediumTurquoise", { 0x48, 0xD1, 0xCC } },
  226. { "MediumVioletRed", { 0xC7, 0x15, 0x85 } },
  227. { "MidnightBlue", { 0x19, 0x19, 0x70 } },
  228. { "MintCream", { 0xF5, 0xFF, 0xFA } },
  229. { "MistyRose", { 0xFF, 0xE4, 0xE1 } },
  230. { "Moccasin", { 0xFF, 0xE4, 0xB5 } },
  231. { "NavajoWhite", { 0xFF, 0xDE, 0xAD } },
  232. { "Navy", { 0x00, 0x00, 0x80 } },
  233. { "OldLace", { 0xFD, 0xF5, 0xE6 } },
  234. { "Olive", { 0x80, 0x80, 0x00 } },
  235. { "OliveDrab", { 0x6B, 0x8E, 0x23 } },
  236. { "Orange", { 0xFF, 0xA5, 0x00 } },
  237. { "OrangeRed", { 0xFF, 0x45, 0x00 } },
  238. { "Orchid", { 0xDA, 0x70, 0xD6 } },
  239. { "PaleGoldenRod", { 0xEE, 0xE8, 0xAA } },
  240. { "PaleGreen", { 0x98, 0xFB, 0x98 } },
  241. { "PaleTurquoise", { 0xAF, 0xEE, 0xEE } },
  242. { "PaleVioletRed", { 0xD8, 0x70, 0x93 } },
  243. { "PapayaWhip", { 0xFF, 0xEF, 0xD5 } },
  244. { "PeachPuff", { 0xFF, 0xDA, 0xB9 } },
  245. { "Peru", { 0xCD, 0x85, 0x3F } },
  246. { "Pink", { 0xFF, 0xC0, 0xCB } },
  247. { "Plum", { 0xDD, 0xA0, 0xDD } },
  248. { "PowderBlue", { 0xB0, 0xE0, 0xE6 } },
  249. { "Purple", { 0x80, 0x00, 0x80 } },
  250. { "Red", { 0xFF, 0x00, 0x00 } },
  251. { "RosyBrown", { 0xBC, 0x8F, 0x8F } },
  252. { "RoyalBlue", { 0x41, 0x69, 0xE1 } },
  253. { "SaddleBrown", { 0x8B, 0x45, 0x13 } },
  254. { "Salmon", { 0xFA, 0x80, 0x72 } },
  255. { "SandyBrown", { 0xF4, 0xA4, 0x60 } },
  256. { "SeaGreen", { 0x2E, 0x8B, 0x57 } },
  257. { "SeaShell", { 0xFF, 0xF5, 0xEE } },
  258. { "Sienna", { 0xA0, 0x52, 0x2D } },
  259. { "Silver", { 0xC0, 0xC0, 0xC0 } },
  260. { "SkyBlue", { 0x87, 0xCE, 0xEB } },
  261. { "SlateBlue", { 0x6A, 0x5A, 0xCD } },
  262. { "SlateGray", { 0x70, 0x80, 0x90 } },
  263. { "Snow", { 0xFF, 0xFA, 0xFA } },
  264. { "SpringGreen", { 0x00, 0xFF, 0x7F } },
  265. { "SteelBlue", { 0x46, 0x82, 0xB4 } },
  266. { "Tan", { 0xD2, 0xB4, 0x8C } },
  267. { "Teal", { 0x00, 0x80, 0x80 } },
  268. { "Thistle", { 0xD8, 0xBF, 0xD8 } },
  269. { "Tomato", { 0xFF, 0x63, 0x47 } },
  270. { "Turquoise", { 0x40, 0xE0, 0xD0 } },
  271. { "Violet", { 0xEE, 0x82, 0xEE } },
  272. { "Wheat", { 0xF5, 0xDE, 0xB3 } },
  273. { "White", { 0xFF, 0xFF, 0xFF } },
  274. { "WhiteSmoke", { 0xF5, 0xF5, 0xF5 } },
  275. { "Yellow", { 0xFF, 0xFF, 0x00 } },
  276. { "YellowGreen", { 0x9A, 0xCD, 0x32 } },
  277. };
  278. static int color_table_compare(const void *lhs, const void *rhs)
  279. {
  280. return av_strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
  281. }
  282. #define ALPHA_SEP '@'
  283. int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen,
  284. void *log_ctx)
  285. {
  286. char *tail, color_string2[128];
  287. const ColorEntry *entry;
  288. int len, hex_offset = 0;
  289. if (color_string[0] == '#') {
  290. hex_offset = 1;
  291. } else if (!strncmp(color_string, "0x", 2))
  292. hex_offset = 2;
  293. if (slen < 0)
  294. slen = strlen(color_string);
  295. av_strlcpy(color_string2, color_string + hex_offset,
  296. FFMIN(slen-hex_offset+1, sizeof(color_string2)));
  297. if ((tail = strchr(color_string2, ALPHA_SEP)))
  298. *tail++ = 0;
  299. len = strlen(color_string2);
  300. rgba_color[3] = 255;
  301. if (!av_strcasecmp(color_string2, "random") || !av_strcasecmp(color_string2, "bikeshed")) {
  302. int rgba = av_get_random_seed();
  303. rgba_color[0] = rgba >> 24;
  304. rgba_color[1] = rgba >> 16;
  305. rgba_color[2] = rgba >> 8;
  306. rgba_color[3] = rgba;
  307. } else if (hex_offset ||
  308. strspn(color_string2, "0123456789ABCDEFabcdef") == len) {
  309. char *tail;
  310. unsigned int rgba = strtoul(color_string2, &tail, 16);
  311. if (*tail || (len != 6 && len != 8)) {
  312. av_log(log_ctx, AV_LOG_ERROR, "Invalid 0xRRGGBB[AA] color string: '%s'\n", color_string2);
  313. return AVERROR(EINVAL);
  314. }
  315. if (len == 8) {
  316. rgba_color[3] = rgba;
  317. rgba >>= 8;
  318. }
  319. rgba_color[0] = rgba >> 16;
  320. rgba_color[1] = rgba >> 8;
  321. rgba_color[2] = rgba;
  322. } else {
  323. entry = bsearch(color_string2,
  324. color_table,
  325. FF_ARRAY_ELEMS(color_table),
  326. sizeof(ColorEntry),
  327. color_table_compare);
  328. if (!entry) {
  329. av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string2);
  330. return AVERROR(EINVAL);
  331. }
  332. memcpy(rgba_color, entry->rgb_color, 3);
  333. }
  334. if (tail) {
  335. double alpha;
  336. const char *alpha_string = tail;
  337. if (!strncmp(alpha_string, "0x", 2)) {
  338. alpha = strtoul(alpha_string, &tail, 16);
  339. } else {
  340. alpha = 255 * strtod(alpha_string, &tail);
  341. }
  342. if (tail == alpha_string || *tail || alpha > 255 || alpha < 0) {
  343. av_log(log_ctx, AV_LOG_ERROR, "Invalid alpha value specifier '%s' in '%s'\n",
  344. alpha_string, color_string);
  345. return AVERROR(EINVAL);
  346. }
  347. rgba_color[3] = alpha;
  348. }
  349. return 0;
  350. }
  351. /* get a positive number between n_min and n_max, for a maximum length
  352. of len_max. Return -1 if error. */
  353. static int date_get_num(const char **pp,
  354. int n_min, int n_max, int len_max)
  355. {
  356. int i, val, c;
  357. const char *p;
  358. p = *pp;
  359. val = 0;
  360. for(i = 0; i < len_max; i++) {
  361. c = *p;
  362. if (!isdigit(c))
  363. break;
  364. val = (val * 10) + c - '0';
  365. p++;
  366. }
  367. /* no number read ? */
  368. if (p == *pp)
  369. return -1;
  370. if (val < n_min || val > n_max)
  371. return -1;
  372. *pp = p;
  373. return val;
  374. }
  375. static const char *small_strptime(const char *p, const char *fmt, struct tm *dt)
  376. {
  377. int c, val;
  378. for(;;) {
  379. c = *fmt++;
  380. if (c == '\0') {
  381. return p;
  382. } else if (c == '%') {
  383. c = *fmt++;
  384. switch(c) {
  385. case 'H':
  386. val = date_get_num(&p, 0, 23, 2);
  387. if (val == -1)
  388. return NULL;
  389. dt->tm_hour = val;
  390. break;
  391. case 'M':
  392. val = date_get_num(&p, 0, 59, 2);
  393. if (val == -1)
  394. return NULL;
  395. dt->tm_min = val;
  396. break;
  397. case 'S':
  398. val = date_get_num(&p, 0, 59, 2);
  399. if (val == -1)
  400. return NULL;
  401. dt->tm_sec = val;
  402. break;
  403. case 'Y':
  404. val = date_get_num(&p, 0, 9999, 4);
  405. if (val == -1)
  406. return NULL;
  407. dt->tm_year = val - 1900;
  408. break;
  409. case 'm':
  410. val = date_get_num(&p, 1, 12, 2);
  411. if (val == -1)
  412. return NULL;
  413. dt->tm_mon = val - 1;
  414. break;
  415. case 'd':
  416. val = date_get_num(&p, 1, 31, 2);
  417. if (val == -1)
  418. return NULL;
  419. dt->tm_mday = val;
  420. break;
  421. case '%':
  422. goto match;
  423. default:
  424. return NULL;
  425. }
  426. } else {
  427. match:
  428. if (c != *p)
  429. return NULL;
  430. p++;
  431. }
  432. }
  433. }
  434. time_t av_timegm(struct tm *tm)
  435. {
  436. time_t t;
  437. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  438. if (m < 3) {
  439. m += 12;
  440. y--;
  441. }
  442. t = 86400 *
  443. (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
  444. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  445. return t;
  446. }
  447. int av_parse_time(int64_t *timeval, const char *timestr, int duration)
  448. {
  449. const char *p;
  450. int64_t t;
  451. struct tm dt = { 0 };
  452. int i;
  453. static const char * const date_fmt[] = {
  454. "%Y-%m-%d",
  455. "%Y%m%d",
  456. };
  457. static const char * const time_fmt[] = {
  458. "%H:%M:%S",
  459. "%H%M%S",
  460. };
  461. const char *q;
  462. int is_utc, len;
  463. char lastch;
  464. int negative = 0;
  465. time_t now = time(0);
  466. len = strlen(timestr);
  467. if (len > 0)
  468. lastch = timestr[len - 1];
  469. else
  470. lastch = '\0';
  471. is_utc = (lastch == 'z' || lastch == 'Z');
  472. p = timestr;
  473. q = NULL;
  474. if (!duration) {
  475. if (!av_strncasecmp(timestr, "now", len)) {
  476. *timeval = (int64_t) now * 1000000;
  477. return 0;
  478. }
  479. /* parse the year-month-day part */
  480. for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
  481. q = small_strptime(p, date_fmt[i], &dt);
  482. if (q) {
  483. break;
  484. }
  485. }
  486. /* if the year-month-day part is missing, then take the
  487. * current year-month-day time */
  488. if (!q) {
  489. if (is_utc) {
  490. dt = *gmtime(&now);
  491. } else {
  492. dt = *localtime(&now);
  493. }
  494. dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
  495. } else {
  496. p = q;
  497. }
  498. if (*p == 'T' || *p == 't' || *p == ' ')
  499. p++;
  500. /* parse the hour-minute-second part */
  501. for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) {
  502. q = small_strptime(p, time_fmt[i], &dt);
  503. if (q) {
  504. break;
  505. }
  506. }
  507. } else {
  508. /* parse timestr as a duration */
  509. if (p[0] == '-') {
  510. negative = 1;
  511. ++p;
  512. }
  513. /* parse timestr as HH:MM:SS */
  514. q = small_strptime(p, time_fmt[0], &dt);
  515. if (!q) {
  516. /* parse timestr as S+ */
  517. dt.tm_sec = strtol(p, (char **)&q, 10);
  518. if (q == p) {
  519. /* the parsing didn't succeed */
  520. *timeval = INT64_MIN;
  521. return AVERROR(EINVAL);
  522. }
  523. dt.tm_min = 0;
  524. dt.tm_hour = 0;
  525. }
  526. }
  527. /* Now we have all the fields that we can get */
  528. if (!q) {
  529. *timeval = INT64_MIN;
  530. return AVERROR(EINVAL);
  531. }
  532. if (duration) {
  533. t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
  534. } else {
  535. dt.tm_isdst = -1; /* unknown */
  536. if (is_utc) {
  537. t = av_timegm(&dt);
  538. } else {
  539. t = mktime(&dt);
  540. }
  541. }
  542. t *= 1000000;
  543. /* parse the .m... part */
  544. if (*q == '.') {
  545. int val, n;
  546. q++;
  547. for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
  548. if (!isdigit(*q))
  549. break;
  550. val += n * (*q - '0');
  551. }
  552. t += val;
  553. }
  554. *timeval = negative ? -t : t;
  555. return 0;
  556. }
  557. int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
  558. {
  559. const char *p;
  560. char tag[128], *q;
  561. p = info;
  562. if (*p == '?')
  563. p++;
  564. for(;;) {
  565. q = tag;
  566. while (*p != '\0' && *p != '=' && *p != '&') {
  567. if ((q - tag) < sizeof(tag) - 1)
  568. *q++ = *p;
  569. p++;
  570. }
  571. *q = '\0';
  572. q = arg;
  573. if (*p == '=') {
  574. p++;
  575. while (*p != '&' && *p != '\0') {
  576. if ((q - arg) < arg_size - 1) {
  577. if (*p == '+')
  578. *q++ = ' ';
  579. else
  580. *q++ = *p;
  581. }
  582. p++;
  583. }
  584. }
  585. *q = '\0';
  586. if (!strcmp(tag, tag1))
  587. return 1;
  588. if (*p != '&')
  589. break;
  590. p++;
  591. }
  592. return 0;
  593. }
  594. #ifdef TEST
  595. int main(void)
  596. {
  597. printf("Testing av_parse_video_rate()\n");
  598. {
  599. int i;
  600. static const char *const rates[] = {
  601. "-inf",
  602. "inf",
  603. "nan",
  604. "123/0",
  605. "-123 / 0",
  606. "",
  607. "/",
  608. " 123 / 321",
  609. "foo/foo",
  610. "foo/1",
  611. "1/foo",
  612. "0/0",
  613. "/0",
  614. "1/",
  615. "1",
  616. "0",
  617. "-123/123",
  618. "-foo",
  619. "123.23",
  620. ".23",
  621. "-.23",
  622. "-0.234",
  623. "-0.0000001",
  624. " 21332.2324 ",
  625. " -21332.2324 ",
  626. };
  627. for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) {
  628. int ret;
  629. AVRational q = { 0, 0 };
  630. ret = av_parse_video_rate(&q, rates[i]);
  631. printf("'%s' -> %d/%d %s\n",
  632. rates[i], q.num, q.den, ret ? "ERROR" : "OK");
  633. }
  634. }
  635. printf("\nTesting av_parse_color()\n");
  636. {
  637. int i;
  638. uint8_t rgba[4];
  639. static const char *const color_names[] = {
  640. "foo",
  641. "red",
  642. "Red ",
  643. "RED",
  644. "Violet",
  645. "Yellow",
  646. "Red",
  647. "0x000000",
  648. "0x0000000",
  649. "0xff000000",
  650. "0x3e34ff",
  651. "0x3e34ffaa",
  652. "0xffXXee",
  653. "0xfoobar",
  654. "0xffffeeeeeeee",
  655. "#ff0000",
  656. "#ffXX00",
  657. "ff0000",
  658. "ffXX00",
  659. "red@foo",
  660. "random@10",
  661. "0xff0000@1.0",
  662. "red@",
  663. "red@0xfff",
  664. "red@0xf",
  665. "red@2",
  666. "red@0.1",
  667. "red@-1",
  668. "red@0.5",
  669. "red@1.0",
  670. "red@256",
  671. "red@10foo",
  672. "red@-1.0",
  673. "red@-0.0",
  674. };
  675. av_log_set_level(AV_LOG_DEBUG);
  676. for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) {
  677. if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0)
  678. printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
  679. color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
  680. }
  681. }
  682. return 0;
  683. }
  684. #endif /* TEST */