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.

781 lines
24KB

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