parseutils.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #define TEST
  19. #include "libavutil/parseutils.c"
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include "libavutil/common.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/rational.h"
  25. static uint32_t randomv = MKTAG('L','A','V','U');
  26. static uint32_t av_get_random_seed_deterministic(void)
  27. {
  28. return randomv = randomv * 1664525 + 1013904223;
  29. }
  30. static void test_av_parse_video_rate(void)
  31. {
  32. int i;
  33. static const char *const rates[] = {
  34. "-inf",
  35. "inf",
  36. "nan",
  37. "123/0",
  38. "-123 / 0",
  39. "",
  40. "/",
  41. " 123 / 321",
  42. "foo/foo",
  43. "foo/1",
  44. "1/foo",
  45. "0/0",
  46. "/0",
  47. "1/",
  48. "1",
  49. "0",
  50. "-123/123",
  51. "-foo",
  52. "123.23",
  53. ".23",
  54. "-.23",
  55. "-0.234",
  56. "-0.0000001",
  57. " 21332.2324 ",
  58. " -21332.2324 ",
  59. };
  60. for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) {
  61. int ret;
  62. AVRational q = { 0, 0 };
  63. ret = av_parse_video_rate(&q, rates[i]);
  64. printf("'%s' -> %d/%d %s\n",
  65. rates[i], q.num, q.den, ret ? "ERROR" : "OK");
  66. }
  67. }
  68. static void test_av_parse_color(void)
  69. {
  70. int i;
  71. uint8_t rgba[4];
  72. static const char *const color_names[] = {
  73. "bikeshed",
  74. "RaNdOm",
  75. "foo",
  76. "red",
  77. "Red ",
  78. "RED",
  79. "Violet",
  80. "Yellow",
  81. "Red",
  82. "0x000000",
  83. "0x0000000",
  84. "0xff000000",
  85. "0x3e34ff",
  86. "0x3e34ffaa",
  87. "0xffXXee",
  88. "0xfoobar",
  89. "0xffffeeeeeeee",
  90. "#ff0000",
  91. "#ffXX00",
  92. "ff0000",
  93. "ffXX00",
  94. "red@foo",
  95. "random@10",
  96. "0xff0000@1.0",
  97. "red@",
  98. "red@0xfff",
  99. "red@0xf",
  100. "red@2",
  101. "red@0.1",
  102. "red@-1",
  103. "red@0.5",
  104. "red@1.0",
  105. "red@256",
  106. "red@10foo",
  107. "red@-1.0",
  108. "red@-0.0",
  109. };
  110. av_log_set_level(AV_LOG_DEBUG);
  111. for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) {
  112. if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0)
  113. printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
  114. color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
  115. else
  116. printf("%s -> error\n", color_names[i]);
  117. }
  118. }
  119. static void test_av_small_strptime(void)
  120. {
  121. int i;
  122. struct tm tm = { 0 };
  123. struct fmt_timespec_entry {
  124. const char *fmt, *timespec;
  125. } fmt_timespec_entries[] = {
  126. { "%Y-%m-%d", "2012-12-21" },
  127. { "%Y - %m - %d", "2012-12-21" },
  128. { "%Y-%m-%d %H:%M:%S", "2012-12-21 20:12:21" },
  129. { " %Y - %m - %d %H : %M : %S", " 2012 - 12 - 21 20 : 12 : 21" },
  130. { " %Y - %b - %d %H : %M : %S", " 2012 - nOV - 21 20 : 12 : 21" },
  131. { " %Y - %B - %d %H : %M : %S", " 2012 - nOVemBeR - 21 20 : 12 : 21" },
  132. { " %Y - %B%d %H : %M : %S", " 2012 - may21 20 : 12 : 21" },
  133. { " %Y - %B%d %H : %M : %S", " 2012 - mby21 20 : 12 : 21" },
  134. { " %Y - %B - %d %H : %M : %S", " 2012 - JunE - 21 20 : 12 : 21" },
  135. { " %Y - %B - %d %H : %M : %S", " 2012 - Jane - 21 20 : 12 : 21" },
  136. { " %Y - %B - %d %H : %M : %S", " 2012 - January - 21 20 : 12 : 21" },
  137. };
  138. av_log_set_level(AV_LOG_DEBUG);
  139. for (i = 0; i < FF_ARRAY_ELEMS(fmt_timespec_entries); i++) {
  140. char *p;
  141. struct fmt_timespec_entry *e = &fmt_timespec_entries[i];
  142. printf("fmt:'%s' spec:'%s' -> ", e->fmt, e->timespec);
  143. p = av_small_strptime(e->timespec, e->fmt, &tm);
  144. if (p) {
  145. printf("%04d-%02d-%2d %02d:%02d:%02d\n",
  146. 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday,
  147. tm.tm_hour, tm.tm_min, tm.tm_sec);
  148. } else {
  149. printf("error\n");
  150. }
  151. }
  152. }
  153. static void test_av_parse_time(void)
  154. {
  155. int i;
  156. int64_t tv;
  157. time_t tvi;
  158. struct tm *tm;
  159. static char tzstr[] = "TZ=CET-1";
  160. static const char * const time_string[] = {
  161. "now",
  162. "12:35:46",
  163. "2000-12-20 0:02:47.5z",
  164. "2012 - 02-22 17:44:07",
  165. "2000-12-20T010247.6",
  166. "2000-12-12 1:35:46+05:30",
  167. "2002-12-12 22:30:40-02",
  168. };
  169. static const char * const duration_string[] = {
  170. "2:34:56.79",
  171. "-1:23:45.67",
  172. "42.1729",
  173. "-1729.42",
  174. "12:34",
  175. "2147483648s",
  176. "4294967296ms",
  177. "8589934592us",
  178. "9223372036854775808us",
  179. };
  180. av_log_set_level(AV_LOG_DEBUG);
  181. putenv(tzstr);
  182. printf("(now is 2012-03-17 09:14:13.2 +0100, local time is UTC+1)\n");
  183. for (i = 0; i < FF_ARRAY_ELEMS(time_string); i++) {
  184. printf("%-24s -> ", time_string[i]);
  185. if (av_parse_time(&tv, time_string[i], 0)) {
  186. printf("error\n");
  187. } else {
  188. tvi = tv / 1000000;
  189. tm = gmtime(&tvi);
  190. printf("%14"PRIi64".%06d = %04d-%02d-%02dT%02d:%02d:%02dZ\n",
  191. tv / 1000000, (int)(tv % 1000000),
  192. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  193. tm->tm_hour, tm->tm_min, tm->tm_sec);
  194. }
  195. }
  196. for (i = 0; i < FF_ARRAY_ELEMS(duration_string); i++) {
  197. printf("%-24s -> ", duration_string[i]);
  198. if (av_parse_time(&tv, duration_string[i], 1)) {
  199. printf("error\n");
  200. } else {
  201. printf("%+21"PRIi64"\n", tv);
  202. }
  203. }
  204. }
  205. static void test_av_get_known_color_name(void)
  206. {
  207. int i;
  208. const uint8_t *rgba;
  209. const char *color;
  210. for (i = 0; i < FF_ARRAY_ELEMS(color_table); ++i) {
  211. color = av_get_known_color_name(i, &rgba);
  212. if (color)
  213. printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
  214. color, rgba[0], rgba[1], rgba[2], rgba[3]);
  215. else
  216. printf("Color ID: %d not found\n", i);
  217. }
  218. }
  219. static void test_av_find_info_tag(void)
  220. {
  221. static const char args[] = "?tag1=val1&tag2=val2&tag3=val3&tag41=value 41&tag42=random1";
  222. static const char *tags[] = {"tag1", "tag2", "tag3", "tag4", "tag41", "41", "random1"};
  223. char buff[16];
  224. int i;
  225. for (i = 0; i < FF_ARRAY_ELEMS(tags); ++i) {
  226. if (av_find_info_tag(buff, sizeof(buff), tags[i], args))
  227. printf("%d. %s found: %s\n", i, tags[i], buff);
  228. else
  229. printf("%d. %s not found\n", i, tags[i]);
  230. }
  231. }
  232. int main(void)
  233. {
  234. printf("Testing av_parse_video_rate()\n");
  235. test_av_parse_video_rate();
  236. printf("\nTesting av_parse_color()\n");
  237. test_av_parse_color();
  238. printf("\nTesting av_small_strptime()\n");
  239. test_av_small_strptime();
  240. printf("\nTesting av_parse_time()\n");
  241. test_av_parse_time();
  242. printf("\nTesting av_get_known_color_name()\n");
  243. test_av_get_known_color_name();
  244. printf("\nTesting av_find_info_tag()\n");
  245. test_av_find_info_tag();
  246. return 0;
  247. }