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.

829 lines
25KB

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