parseutils.c 23 KB

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