parseutils.c 28 KB

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