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.

877 lines
27KB

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