You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
6.8KB

  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. };
  131. av_log_set_level(AV_LOG_DEBUG);
  132. for (i = 0; i < FF_ARRAY_ELEMS(fmt_timespec_entries); i++) {
  133. char *p;
  134. struct fmt_timespec_entry *e = &fmt_timespec_entries[i];
  135. printf("fmt:'%s' spec:'%s' -> ", e->fmt, e->timespec);
  136. p = av_small_strptime(e->timespec, e->fmt, &tm);
  137. if (p) {
  138. printf("%04d-%02d-%2d %02d:%02d:%02d\n",
  139. 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday,
  140. tm.tm_hour, tm.tm_min, tm.tm_sec);
  141. } else {
  142. printf("error\n");
  143. }
  144. }
  145. }
  146. static void test_av_parse_time(void)
  147. {
  148. int i;
  149. int64_t tv;
  150. time_t tvi;
  151. struct tm *tm;
  152. static char tzstr[] = "TZ=CET-1";
  153. static const char * const time_string[] = {
  154. "now",
  155. "12:35:46",
  156. "2000-12-20 0:02:47.5z",
  157. "2012 - 02-22 17:44:07",
  158. "2000-12-20T010247.6",
  159. "2000-12-12 1:35:46+05:30",
  160. "2002-12-12 22:30:40-02",
  161. };
  162. static const char * const duration_string[] = {
  163. "2:34:56.79",
  164. "-1:23:45.67",
  165. "42.1729",
  166. "-1729.42",
  167. "12:34",
  168. };
  169. av_log_set_level(AV_LOG_DEBUG);
  170. putenv(tzstr);
  171. printf("(now is 2012-03-17 09:14:13.2 +0100, local time is UTC+1)\n");
  172. for (i = 0; i < FF_ARRAY_ELEMS(time_string); i++) {
  173. printf("%-24s -> ", time_string[i]);
  174. if (av_parse_time(&tv, time_string[i], 0)) {
  175. printf("error\n");
  176. } else {
  177. tvi = tv / 1000000;
  178. tm = gmtime(&tvi);
  179. printf("%14"PRIi64".%06d = %04d-%02d-%02dT%02d:%02d:%02dZ\n",
  180. tv / 1000000, (int)(tv % 1000000),
  181. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  182. tm->tm_hour, tm->tm_min, tm->tm_sec);
  183. }
  184. }
  185. for (i = 0; i < FF_ARRAY_ELEMS(duration_string); i++) {
  186. printf("%-24s -> ", duration_string[i]);
  187. if (av_parse_time(&tv, duration_string[i], 1)) {
  188. printf("error\n");
  189. } else {
  190. printf("%+21"PRIi64"\n", tv);
  191. }
  192. }
  193. }
  194. static void test_av_get_known_color_name(void)
  195. {
  196. int i;
  197. const uint8_t *rgba;
  198. const char *color;
  199. for (i = 0; i < FF_ARRAY_ELEMS(color_table); ++i) {
  200. color = av_get_known_color_name(i, &rgba);
  201. if (color)
  202. printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
  203. color, rgba[0], rgba[1], rgba[2], rgba[3]);
  204. else
  205. printf("Color ID: %d not found\n", i);
  206. }
  207. }
  208. static void test_av_find_info_tag(void)
  209. {
  210. static const char args[] = "?tag1=val1&tag2=val2&tag3=val3&tag41=value 41&tag42=random1";
  211. static const char *tags[] = {"tag1", "tag2", "tag3", "tag4", "tag41", "41", "random1"};
  212. char buff[16];
  213. int i;
  214. for (i = 0; i < FF_ARRAY_ELEMS(tags); ++i) {
  215. if (av_find_info_tag(buff, sizeof(buff), tags[i], args))
  216. printf("%d. %s found: %s\n", i, tags[i], buff);
  217. else
  218. printf("%d. %s not found\n", i, tags[i]);
  219. }
  220. }
  221. int main(void)
  222. {
  223. printf("Testing av_parse_video_rate()\n");
  224. test_av_parse_video_rate();
  225. printf("\nTesting av_parse_color()\n");
  226. test_av_parse_color();
  227. printf("\nTesting av_small_strptime()\n");
  228. test_av_small_strptime();
  229. printf("\nTesting av_parse_time()\n");
  230. test_av_parse_time();
  231. printf("\nTesting av_get_known_color_name()\n");
  232. test_av_get_known_color_name();
  233. printf("\nTesting av_find_info_tag()\n");
  234. test_av_find_info_tag();
  235. return 0;
  236. }