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.

657 lines
21KB

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