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.

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