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.

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