parseutils.c 23 KB

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