parseutils.c 27 KB

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