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.

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