parseutils.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 <sys/time.h>
  23. #include <time.h>
  24. #include "avstring.h"
  25. #include "avutil.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. { "LightGrey", { 0xD3, 0xD3, 0xD3 } },
  231. { "LightGreen", { 0x90, 0xEE, 0x90 } },
  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. /**
  406. * Parse the input string p according to the format string fmt and
  407. * store its results in the structure dt.
  408. * This implementation supports only a subset of the formats supported
  409. * by the standard strptime().
  410. *
  411. * @return a pointer to the first character not processed in this
  412. * function call, or NULL in case the function fails to match all of
  413. * the fmt string and therefore an error occurred
  414. */
  415. static const char *small_strptime(const char *p, const char *fmt, struct tm *dt)
  416. {
  417. int c, val;
  418. for(;;) {
  419. c = *fmt++;
  420. if (c == '\0') {
  421. return p;
  422. } else if (c == '%') {
  423. c = *fmt++;
  424. switch(c) {
  425. case 'H':
  426. val = date_get_num(&p, 0, 23, 2);
  427. if (val == -1)
  428. return NULL;
  429. dt->tm_hour = val;
  430. break;
  431. case 'M':
  432. val = date_get_num(&p, 0, 59, 2);
  433. if (val == -1)
  434. return NULL;
  435. dt->tm_min = val;
  436. break;
  437. case 'S':
  438. val = date_get_num(&p, 0, 59, 2);
  439. if (val == -1)
  440. return NULL;
  441. dt->tm_sec = val;
  442. break;
  443. case 'Y':
  444. val = date_get_num(&p, 0, 9999, 4);
  445. if (val == -1)
  446. return NULL;
  447. dt->tm_year = val - 1900;
  448. break;
  449. case 'm':
  450. val = date_get_num(&p, 1, 12, 2);
  451. if (val == -1)
  452. return NULL;
  453. dt->tm_mon = val - 1;
  454. break;
  455. case 'd':
  456. val = date_get_num(&p, 1, 31, 2);
  457. if (val == -1)
  458. return NULL;
  459. dt->tm_mday = val;
  460. break;
  461. case '%':
  462. goto match;
  463. default:
  464. return NULL;
  465. }
  466. } else {
  467. match:
  468. if (c != *p)
  469. return NULL;
  470. p++;
  471. }
  472. }
  473. }
  474. time_t av_timegm(struct tm *tm)
  475. {
  476. time_t t;
  477. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  478. if (m < 3) {
  479. m += 12;
  480. y--;
  481. }
  482. t = 86400 *
  483. (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
  484. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  485. return t;
  486. }
  487. int av_parse_time(int64_t *timeval, const char *timestr, int duration)
  488. {
  489. const char *p, *q;
  490. int64_t t;
  491. time_t now;
  492. struct tm dt = { 0 };
  493. int today = 0, negative = 0, microseconds = 0;
  494. int i;
  495. static const char * const date_fmt[] = {
  496. "%Y-%m-%d",
  497. "%Y%m%d",
  498. };
  499. static const char * const time_fmt[] = {
  500. "%H:%M:%S",
  501. "%H%M%S",
  502. };
  503. p = timestr;
  504. q = NULL;
  505. *timeval = INT64_MIN;
  506. if (!duration) {
  507. now = time(0);
  508. if (!av_strcasecmp(timestr, "now")) {
  509. *timeval = (int64_t) now * 1000000;
  510. return 0;
  511. }
  512. /* parse the year-month-day part */
  513. for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
  514. q = small_strptime(p, date_fmt[i], &dt);
  515. if (q)
  516. break;
  517. }
  518. /* if the year-month-day part is missing, then take the
  519. * current year-month-day time */
  520. if (!q) {
  521. today = 1;
  522. q = p;
  523. }
  524. p = q;
  525. if (*p == 'T' || *p == 't' || *p == ' ')
  526. p++;
  527. /* parse the hour-minute-second part */
  528. for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) {
  529. q = small_strptime(p, time_fmt[i], &dt);
  530. if (q)
  531. break;
  532. }
  533. } else {
  534. /* parse timestr as a duration */
  535. if (p[0] == '-') {
  536. negative = 1;
  537. ++p;
  538. }
  539. /* parse timestr as HH:MM:SS */
  540. q = small_strptime(p, time_fmt[0], &dt);
  541. if (!q) {
  542. /* parse timestr as S+ */
  543. dt.tm_sec = strtol(p, (void *)&q, 10);
  544. if (q == p) /* the parsing didn't succeed */
  545. return AVERROR(EINVAL);
  546. dt.tm_min = 0;
  547. dt.tm_hour = 0;
  548. }
  549. }
  550. /* Now we have all the fields that we can get */
  551. if (!q)
  552. return AVERROR(EINVAL);
  553. /* parse the .m... part */
  554. if (*q == '.') {
  555. int n;
  556. q++;
  557. for (n = 100000; n >= 1; n /= 10, q++) {
  558. if (!isdigit(*q))
  559. break;
  560. microseconds += n * (*q - '0');
  561. }
  562. }
  563. if (duration) {
  564. t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
  565. } else {
  566. int is_utc = *q == 'Z' || *q == 'z';
  567. q += is_utc;
  568. if (today) { /* fill in today's date */
  569. struct tm dt2 = is_utc ? *gmtime(&now) : *localtime(&now);
  570. dt2.tm_hour = dt.tm_hour;
  571. dt2.tm_min = dt.tm_min;
  572. dt2.tm_sec = dt.tm_sec;
  573. dt = dt2;
  574. }
  575. t = is_utc ? av_timegm(&dt) : mktime(&dt);
  576. }
  577. /* Check that we are at the end of the string */
  578. if (*q)
  579. return AVERROR(EINVAL);
  580. t *= 1000000;
  581. t += microseconds;
  582. *timeval = negative ? -t : t;
  583. return 0;
  584. }
  585. int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
  586. {
  587. const char *p;
  588. char tag[128], *q;
  589. p = info;
  590. if (*p == '?')
  591. p++;
  592. for(;;) {
  593. q = tag;
  594. while (*p != '\0' && *p != '=' && *p != '&') {
  595. if ((q - tag) < sizeof(tag) - 1)
  596. *q++ = *p;
  597. p++;
  598. }
  599. *q = '\0';
  600. q = arg;
  601. if (*p == '=') {
  602. p++;
  603. while (*p != '&' && *p != '\0') {
  604. if ((q - arg) < arg_size - 1) {
  605. if (*p == '+')
  606. *q++ = ' ';
  607. else
  608. *q++ = *p;
  609. }
  610. p++;
  611. }
  612. }
  613. *q = '\0';
  614. if (!strcmp(tag, tag1))
  615. return 1;
  616. if (*p != '&')
  617. break;
  618. p++;
  619. }
  620. return 0;
  621. }
  622. #ifdef TEST
  623. static uint32_t random = MKTAG('L','A','V','U');
  624. static uint32_t av_get_random_seed_deterministic(void)
  625. {
  626. return random = random * 1664525 + 1013904223;
  627. }
  628. #undef printf
  629. int main(void)
  630. {
  631. printf("Testing av_parse_video_rate()\n");
  632. {
  633. int i;
  634. const char *rates[] = {
  635. "-inf",
  636. "inf",
  637. "nan",
  638. "123/0",
  639. "-123 / 0",
  640. "",
  641. "/",
  642. " 123 / 321",
  643. "foo/foo",
  644. "foo/1",
  645. "1/foo",
  646. "0/0",
  647. "/0",
  648. "1/",
  649. "1",
  650. "0",
  651. "-123/123",
  652. "-foo",
  653. "123.23",
  654. ".23",
  655. "-.23",
  656. "-0.234",
  657. "-0.0000001",
  658. " 21332.2324 ",
  659. " -21332.2324 ",
  660. };
  661. for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) {
  662. int ret;
  663. char err[1024];
  664. AVRational q = (AVRational){0, 0};
  665. ret = av_parse_video_rate(&q, rates[i]);
  666. av_strerror(ret, err, sizeof(err));
  667. printf("'%s' -> %d/%d ret:%s\n",
  668. rates[i], q.num, q.den, err);
  669. }
  670. }
  671. printf("\nTesting av_parse_color()\n");
  672. {
  673. int i;
  674. uint8_t rgba[4];
  675. const char *color_names[] = {
  676. "bikeshed",
  677. "RaNdOm",
  678. "foo",
  679. "red",
  680. "Red ",
  681. "RED",
  682. "Violet",
  683. "Yellow",
  684. "Red",
  685. "0x000000",
  686. "0x0000000",
  687. "0xff000000",
  688. "0x3e34ff",
  689. "0x3e34ffaa",
  690. "0xffXXee",
  691. "0xfoobar",
  692. "0xffffeeeeeeee",
  693. "#ff0000",
  694. "#ffXX00",
  695. "ff0000",
  696. "ffXX00",
  697. "red@foo",
  698. "random@10",
  699. "0xff0000@1.0",
  700. "red@",
  701. "red@0xfff",
  702. "red@0xf",
  703. "red@2",
  704. "red@0.1",
  705. "red@-1",
  706. "red@0.5",
  707. "red@1.0",
  708. "red@256",
  709. "red@10foo",
  710. "red@-1.0",
  711. "red@-0.0",
  712. };
  713. av_log_set_level(AV_LOG_DEBUG);
  714. for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) {
  715. if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0)
  716. printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
  717. else
  718. printf("%s -> error\n", color_names[i]);
  719. }
  720. }
  721. printf("\nTesting av_parse_time()\n");
  722. {
  723. int i;
  724. int64_t tv;
  725. time_t tvi;
  726. struct tm *tm;
  727. static char tzstr[] = "TZ=CET-1";
  728. const char *time_string[] = {
  729. "now",
  730. "12:35:46",
  731. "2000-12-20 0:02:47.5z",
  732. "2000-12-20T010247.6",
  733. };
  734. const char *duration_string[] = {
  735. "2:34:56.79",
  736. "-1:23:45.67",
  737. "42.1729",
  738. "-1729.42",
  739. "12:34",
  740. };
  741. av_log_set_level(AV_LOG_DEBUG);
  742. putenv(tzstr);
  743. printf("(now is 2012-03-17 09:14:13 +0100, local time is UTC+1)\n");
  744. for (i = 0; i < FF_ARRAY_ELEMS(time_string); i++) {
  745. printf("%-24s -> ", time_string[i]);
  746. if (av_parse_time(&tv, time_string[i], 0)) {
  747. printf("error\n");
  748. } else {
  749. tvi = tv / 1000000;
  750. tm = gmtime(&tvi);
  751. printf("%14"PRIi64".%06d = %04d-%02d-%02dT%02d:%02d:%02dZ\n",
  752. tv / 1000000, (int)(tv % 1000000),
  753. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  754. tm->tm_hour, tm->tm_min, tm->tm_sec);
  755. }
  756. }
  757. for (i = 0; i < FF_ARRAY_ELEMS(duration_string); i++) {
  758. printf("%-24s -> ", duration_string[i]);
  759. if (av_parse_time(&tv, duration_string[i], 1)) {
  760. printf("error\n");
  761. } else {
  762. printf("%+21"PRIi64"\n", tv);
  763. }
  764. }
  765. }
  766. return 0;
  767. }
  768. #endif /* TEST */