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.

901 lines
28KB

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