parseutils.c 27 KB

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