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.

866 lines
27KB

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