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.

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