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.

836 lines
26KB

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