parseutils.c 25 KB

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