parseutils.c 28 KB

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