FFmpeg  4.0
parseutils.c
Go to the documentation of this file.
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 #define TEST
20 #include "libavutil/parseutils.c"
21 
22 #include <stdint.h>
23 #include <stdio.h>
24 
25 #include "libavutil/common.h"
26 #include "libavutil/log.h"
27 #include "libavutil/rational.h"
28 
29 static uint32_t randomv = MKTAG('L','A','V','U');
30 
31 static uint32_t av_get_random_seed_deterministic(void)
32 {
33  return randomv = randomv * 1664525 + 1013904223;
34 }
35 
36 static void test_av_parse_video_rate(void)
37 {
38  int i;
39  static const char *const rates[] = {
40  "-inf",
41  "inf",
42  "nan",
43  "123/0",
44  "-123 / 0",
45  "",
46  "/",
47  " 123 / 321",
48  "foo/foo",
49  "foo/1",
50  "1/foo",
51  "0/0",
52  "/0",
53  "1/",
54  "1",
55  "0",
56  "-123/123",
57  "-foo",
58  "123.23",
59  ".23",
60  "-.23",
61  "-0.234",
62  "-0.0000001",
63  " 21332.2324 ",
64  " -21332.2324 ",
65  };
66 
67  for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) {
68  int ret;
69  AVRational q = { 0, 0 };
70  ret = av_parse_video_rate(&q, rates[i]);
71  printf("'%s' -> %d/%d %s\n",
72  rates[i], q.num, q.den, ret ? "ERROR" : "OK");
73  }
74 }
75 
76 static void test_av_parse_color(void)
77 {
78  int i;
79  uint8_t rgba[4];
80  static const char *const color_names[] = {
81  "bikeshed",
82  "RaNdOm",
83  "foo",
84  "red",
85  "Red ",
86  "RED",
87  "Violet",
88  "Yellow",
89  "Red",
90  "0x000000",
91  "0x0000000",
92  "0xff000000",
93  "0x3e34ff",
94  "0x3e34ffaa",
95  "0xffXXee",
96  "0xfoobar",
97  "0xffffeeeeeeee",
98  "#ff0000",
99  "#ffXX00",
100  "ff0000",
101  "ffXX00",
102  "red@foo",
103  "random@10",
104  "0xff0000@1.0",
105  "red@",
106  "red@0xfff",
107  "red@0xf",
108  "red@2",
109  "red@0.1",
110  "red@-1",
111  "red@0.5",
112  "red@1.0",
113  "red@256",
114  "red@10foo",
115  "red@-1.0",
116  "red@-0.0",
117  };
118 
120 
121  for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) {
122  if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0)
123  printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
124  color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
125  else
126  printf("%s -> error\n", color_names[i]);
127  }
128 }
129 
130 static void test_av_small_strptime(void)
131 {
132  int i;
133  struct tm tm = { 0 };
134  struct fmt_timespec_entry {
135  const char *fmt, *timespec;
136  } fmt_timespec_entries[] = {
137  { "%Y-%m-%d", "2012-12-21" },
138  { "%Y - %m - %d", "2012-12-21" },
139  { "%Y-%m-%d %H:%M:%S", "2012-12-21 20:12:21" },
140  { " %Y - %m - %d %H : %M : %S", " 2012 - 12 - 21 20 : 12 : 21" },
141  { " %Y - %b - %d %H : %M : %S", " 2012 - nOV - 21 20 : 12 : 21" },
142  { " %Y - %B - %d %H : %M : %S", " 2012 - nOVemBeR - 21 20 : 12 : 21" },
143  { " %Y - %B%d %H : %M : %S", " 2012 - may21 20 : 12 : 21" },
144  { " %Y - %B%d %H : %M : %S", " 2012 - mby21 20 : 12 : 21" },
145  { " %Y - %B - %d %H : %M : %S", " 2012 - JunE - 21 20 : 12 : 21" },
146  { " %Y - %B - %d %H : %M : %S", " 2012 - Jane - 21 20 : 12 : 21" },
147  { " %Y - %B - %d %H : %M : %S", " 2012 - January - 21 20 : 12 : 21" },
148  };
149 
151  for (i = 0; i < FF_ARRAY_ELEMS(fmt_timespec_entries); i++) {
152  char *p;
153  struct fmt_timespec_entry *e = &fmt_timespec_entries[i];
154  printf("fmt:'%s' spec:'%s' -> ", e->fmt, e->timespec);
155  p = av_small_strptime(e->timespec, e->fmt, &tm);
156  if (p) {
157  printf("%04d-%02d-%2d %02d:%02d:%02d\n",
158  1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday,
159  tm.tm_hour, tm.tm_min, tm.tm_sec);
160  } else {
161  printf("error\n");
162  }
163  }
164 }
165 
166 static void test_av_parse_time(void)
167 {
168  int i;
169  int64_t tv;
170  time_t tvi;
171  struct tm *tm;
172  static char tzstr[] = "TZ=CET-1";
173  static const char * const time_string[] = {
174  "now",
175  "12:35:46",
176  "2000-12-20 0:02:47.5z",
177  "2012 - 02-22 17:44:07",
178  "2000-12-20T010247.6",
179  "2000-12-12 1:35:46+05:30",
180  "2002-12-12 22:30:40-02",
181  };
182  static const char * const duration_string[] = {
183  "2:34:56.79",
184  "-1:23:45.67",
185  "42.1729",
186  "-1729.42",
187  "12:34",
188  };
189 
191  putenv(tzstr);
192  printf("(now is 2012-03-17 09:14:13.2 +0100, local time is UTC+1)\n");
193  for (i = 0; i < FF_ARRAY_ELEMS(time_string); i++) {
194  printf("%-24s -> ", time_string[i]);
195  if (av_parse_time(&tv, time_string[i], 0)) {
196  printf("error\n");
197  } else {
198  tvi = tv / 1000000;
199  tm = gmtime(&tvi);
200  printf("%14"PRIi64".%06d = %04d-%02d-%02dT%02d:%02d:%02dZ\n",
201  tv / 1000000, (int)(tv % 1000000),
202  tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
203  tm->tm_hour, tm->tm_min, tm->tm_sec);
204  }
205  }
206  for (i = 0; i < FF_ARRAY_ELEMS(duration_string); i++) {
207  printf("%-24s -> ", duration_string[i]);
208  if (av_parse_time(&tv, duration_string[i], 1)) {
209  printf("error\n");
210  } else {
211  printf("%+21"PRIi64"\n", tv);
212  }
213  }
214 }
215 
217 {
218  int i;
219  const uint8_t *rgba;
220  const char *color;
221 
222  for (i = 0; i < FF_ARRAY_ELEMS(color_table); ++i) {
223  color = av_get_known_color_name(i, &rgba);
224  if (color)
225  printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
226  color, rgba[0], rgba[1], rgba[2], rgba[3]);
227  else
228  printf("Color ID: %d not found\n", i);
229  }
230 }
231 
232 static void test_av_find_info_tag(void)
233 {
234  static const char args[] = "?tag1=val1&tag2=val2&tag3=val3&tag41=value 41&tag42=random1";
235  static const char *tags[] = {"tag1", "tag2", "tag3", "tag4", "tag41", "41", "random1"};
236  char buff[16];
237  int i;
238 
239  for (i = 0; i < FF_ARRAY_ELEMS(tags); ++i) {
240  if (av_find_info_tag(buff, sizeof(buff), tags[i], args))
241  printf("%d. %s found: %s\n", i, tags[i], buff);
242  else
243  printf("%d. %s not found\n", i, tags[i]);
244  }
245 }
246 
247 int main(void)
248 {
249  printf("Testing av_parse_video_rate()\n");
251 
252  printf("\nTesting av_parse_color()\n");
254 
255  printf("\nTesting av_small_strptime()\n");
257 
258  printf("\nTesting av_parse_time()\n");
260 
261  printf("\nTesting av_get_known_color_name()\n");
263 
264  printf("\nTesting av_find_info_tag()\n");
266  return 0;
267 }
static uint32_t av_get_random_seed_deterministic(void)
Definition: parseutils.c:31
#define NULL
Definition: coverity.c:32
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:179
const char * fmt
Definition: avisynth_c.h:769
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:587
void av_log_set_level(int level)
Set the log level.
Definition: log.c:385
misc parsing utilities
int num
Numerator.
Definition: rational.h:59
static void test_av_parse_video_rate(void)
Definition: parseutils.c:36
uint8_t
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:92
char * av_small_strptime(const char *p, const char *fmt, struct tm *dt)
Simplified version of strptime.
Definition: parseutils.c:489
static void test_av_parse_color(void)
Definition: parseutils.c:76
int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
Attempt to find a specific tag in a URL.
Definition: parseutils.c:743
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
Definition: parseutils.c:354
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const char * av_get_known_color_name(int color_idx, const uint8_t **rgbp)
Get the name of a color from the internal table of hard-coded named colors.
Definition: parseutils.c:434
static uint32_t randomv
Definition: parseutils.c:29
const char AVS_Value args
Definition: avisynth_c.h:780
static void test_av_small_strptime(void)
Definition: parseutils.c:130
#define FF_ARRAY_ELEMS(a)
static void test_av_parse_time(void)
Definition: parseutils.c:166
static const ColorEntry color_table[]
Definition: parseutils.c:204
static const char * color_names[NB_RANGES]
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int main(void)
Definition: parseutils.c:247
static void test_av_get_known_color_name(void)
Definition: parseutils.c:216
static void test_av_find_info_tag(void)
Definition: parseutils.c:232
common internal and external API header
Utilties for rational number calculation.
int den
Denominator.
Definition: rational.h:60
static const int rates[]
Definition: avresample.c:176
#define MKTAG(a, b, c, d)
Definition: common.h:366