parseutils.c 27 KB

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