parseutils.c 23 KB

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