1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/*
* copyright (c) 2009 Stefano Sabatini
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* parsing utils
*/
#include <strings.h>
#include "libavutil/avutil.h"
#include "libavutil/random_seed.h"
#include "parseutils.h"
#define WHITESPACES " \n\t"
char *av_get_token(const char **buf, const char *term)
{
char *out = av_malloc(strlen(*buf) + 1);
char *ret= out, *end= out;
const char *p = *buf;
p += strspn(p, WHITESPACES);
while(*p && !strspn(p, term)) {
char c = *p++;
if(c == '\\' && *p){
*out++ = *p++;
end= out;
}else if(c == '\''){
while(*p && *p != '\'')
*out++ = *p++;
if(*p){
p++;
end= out;
}
}else{
*out++ = c;
}
}
do{
*out-- = 0;
}while(out >= end && strspn(out, WHITESPACES));
*buf = p;
return ret;
}
typedef struct {
const char *name; ///< a string representing the name of the color
uint8_t rgba_color[4]; ///< RGBA values for the color
} ColorEntry;
static ColorEntry color_table[] = {
{ "AliceBlue", { 0xF0, 0xF8, 0xFF } },
{ "AntiqueWhite", { 0xFA, 0xEB, 0xD7 } },
{ "Aqua", { 0x00, 0xFF, 0xFF } },
{ "Aquamarine", { 0x7F, 0xFF, 0xD4 } },
{ "Azure", { 0xF0, 0xFF, 0xFF } },
{ "Beige", { 0xF5, 0xF5, 0xDC } },
{ "Bisque", { 0xFF, 0xE4, 0xC4 } },
{ "Black", { 0x00, 0x00, 0x00 } },
{ "BlanchedAlmond", { 0xFF, 0xEB, 0xCD } },
{ "Blue", { 0x00, 0x00, 0xFF } },
{ "BlueViolet", { 0x8A, 0x2B, 0xE2 } },
{ "Brown", { 0xA5, 0x2A, 0x2A } },
{ "BurlyWood", { 0xDE, 0xB8, 0x87 } },
{ "CadetBlue", { 0x5F, 0x9E, 0xA0 } },
{ "Chartreuse", { 0x7F, 0xFF, 0x00 } },
{ "Chocolate", { 0xD2, 0x69, 0x1E } },
{ "Coral", { 0xFF, 0x7F, 0x50 } },
{ "CornflowerBlue", { 0x64, 0x95, 0xED } },
{ "Cornsilk", { 0xFF, 0xF8, 0xDC } },
{ "Crimson", { 0xDC, 0x14, 0x3C } },
{ "Cyan", { 0x00, 0xFF, 0xFF } },
{ "DarkBlue", { 0x00, 0x00, 0x8B } },
{ "DarkCyan", { 0x00, 0x8B, 0x8B } },
{ "DarkGoldenRod", { 0xB8, 0x86, 0x0B } },
{ "DarkGray", { 0xA9, 0xA9, 0xA9 } },
{ "DarkGreen", { 0x00, 0x64, 0x00 } },
{ "DarkKhaki", { 0xBD, 0xB7, 0x6B } },
{ "DarkMagenta", { 0x8B, 0x00, 0x8B } },
{ "DarkOliveGreen", { 0x55, 0x6B, 0x2F } },
{ "Darkorange", { 0xFF, 0x8C, 0x00 } },
{ "DarkOrchid", { 0x99, 0x32, 0xCC } },
{ "DarkRed", { 0x8B, 0x00, 0x00 } },
{ "DarkSalmon", { 0xE9, 0x96, 0x7A } },
{ "DarkSeaGreen", { 0x8F, 0xBC, 0x8F } },
{ "DarkSlateBlue", { 0x48, 0x3D, 0x8B } },
{ "DarkSlateGray", { 0x2F, 0x4F, 0x4F } },
{ "DarkTurquoise", { 0x00, 0xCE, 0xD1 } },
{ "DarkViolet", { 0x94, 0x00, 0xD3 } },
{ "DeepPink", { 0xFF, 0x14, 0x93 } },
{ "DeepSkyBlue", { 0x00, 0xBF, 0xFF } },
{ "DimGray", { 0x69, 0x69, 0x69 } },
{ "DodgerBlue", { 0x1E, 0x90, 0xFF } },
{ "FireBrick", { 0xB2, 0x22, 0x22 } },
{ "FloralWhite", { 0xFF, 0xFA, 0xF0 } },
{ "ForestGreen", { 0x22, 0x8B, 0x22 } },
{ "Fuchsia", { 0xFF, 0x00, 0xFF } },
{ "Gainsboro", { 0xDC, 0xDC, 0xDC } },
{ "GhostWhite", { 0xF8, 0xF8, 0xFF } },
{ "Gold", { 0xFF, 0xD7, 0x00 } },
{ "GoldenRod", { 0xDA, 0xA5, 0x20 } },
{ "Gray", { 0x80, 0x80, 0x80 } },
{ "Green", { 0x00, 0x80, 0x00 } },
{ "GreenYellow", { 0xAD, 0xFF, 0x2F } },
{ "HoneyDew", { 0xF0, 0xFF, 0xF0 } },
{ "HotPink", { 0xFF, 0x69, 0xB4 } },
{ "IndianRed", { 0xCD, 0x5C, 0x5C } },
{ "Indigo", { 0x4B, 0x00, 0x82 } },
{ "Ivory", { 0xFF, 0xFF, 0xF0 } },
{ "Khaki", { 0xF0, 0xE6, 0x8C } },
{ "Lavender", { 0xE6, 0xE6, 0xFA } },
{ "LavenderBlush", { 0xFF, 0xF0, 0xF5 } },
{ "LawnGreen", { 0x7C, 0xFC, 0x00 } },
{ "LemonChiffon", { 0xFF, 0xFA, 0xCD } },
{ "LightBlue", { 0xAD, 0xD8, 0xE6 } },
{ "LightCoral", { 0xF0, 0x80, 0x80 } },
{ "LightCyan", { 0xE0, 0xFF, 0xFF } },
{ "LightGoldenRodYellow", { 0xFA, 0xFA, 0xD2 } },
{ "LightGrey", { 0xD3, 0xD3, 0xD3 } },
{ "LightGreen", { 0x90, 0xEE, 0x90 } },
{ "LightPink", { 0xFF, 0xB6, 0xC1 } },
{ "LightSalmon", { 0xFF, 0xA0, 0x7A } },
{ "LightSeaGreen", { 0x20, 0xB2, 0xAA } },
{ "LightSkyBlue", { 0x87, 0xCE, 0xFA } },
{ "LightSlateGray", { 0x77, 0x88, 0x99 } },
{ "LightSteelBlue", { 0xB0, 0xC4, 0xDE } },
{ "LightYellow", { 0xFF, 0xFF, 0xE0 } },
{ "Lime", { 0x00, 0xFF, 0x00 } },
{ "LimeGreen", { 0x32, 0xCD, 0x32 } },
{ "Linen", { 0xFA, 0xF0, 0xE6 } },
{ "Magenta", { 0xFF, 0x00, 0xFF } },
{ "Maroon", { 0x80, 0x00, 0x00 } },
{ "MediumAquaMarine", { 0x66, 0xCD, 0xAA } },
{ "MediumBlue", { 0x00, 0x00, 0xCD } },
{ "MediumOrchid", { 0xBA, 0x55, 0xD3 } },
{ "MediumPurple", { 0x93, 0x70, 0xD8 } },
{ "MediumSeaGreen", { 0x3C, 0xB3, 0x71 } },
{ "MediumSlateBlue", { 0x7B, 0x68, 0xEE } },
{ "MediumSpringGreen", { 0x00, 0xFA, 0x9A } },
{ "MediumTurquoise", { 0x48, 0xD1, 0xCC } },
{ "MediumVioletRed", { 0xC7, 0x15, 0x85 } },
{ "MidnightBlue", { 0x19, 0x19, 0x70 } },
{ "MintCream", { 0xF5, 0xFF, 0xFA } },
{ "MistyRose", { 0xFF, 0xE4, 0xE1 } },
{ "Moccasin", { 0xFF, 0xE4, 0xB5 } },
{ "NavajoWhite", { 0xFF, 0xDE, 0xAD } },
{ "Navy", { 0x00, 0x00, 0x80 } },
{ "OldLace", { 0xFD, 0xF5, 0xE6 } },
{ "Olive", { 0x80, 0x80, 0x00 } },
{ "OliveDrab", { 0x6B, 0x8E, 0x23 } },
{ "Orange", { 0xFF, 0xA5, 0x00 } },
{ "OrangeRed", { 0xFF, 0x45, 0x00 } },
{ "Orchid", { 0xDA, 0x70, 0xD6 } },
{ "PaleGoldenRod", { 0xEE, 0xE8, 0xAA } },
{ "PaleGreen", { 0x98, 0xFB, 0x98 } },
{ "PaleTurquoise", { 0xAF, 0xEE, 0xEE } },
{ "PaleVioletRed", { 0xD8, 0x70, 0x93 } },
{ "PapayaWhip", { 0xFF, 0xEF, 0xD5 } },
{ "PeachPuff", { 0xFF, 0xDA, 0xB9 } },
{ "Peru", { 0xCD, 0x85, 0x3F } },
{ "Pink", { 0xFF, 0xC0, 0xCB } },
{ "Plum", { 0xDD, 0xA0, 0xDD } },
{ "PowderBlue", { 0xB0, 0xE0, 0xE6 } },
{ "Purple", { 0x80, 0x00, 0x80 } },
{ "Red", { 0xFF, 0x00, 0x00 } },
{ "RosyBrown", { 0xBC, 0x8F, 0x8F } },
{ "RoyalBlue", { 0x41, 0x69, 0xE1 } },
{ "SaddleBrown", { 0x8B, 0x45, 0x13 } },
{ "Salmon", { 0xFA, 0x80, 0x72 } },
{ "SandyBrown", { 0xF4, 0xA4, 0x60 } },
{ "SeaGreen", { 0x2E, 0x8B, 0x57 } },
{ "SeaShell", { 0xFF, 0xF5, 0xEE } },
{ "Sienna", { 0xA0, 0x52, 0x2D } },
{ "Silver", { 0xC0, 0xC0, 0xC0 } },
{ "SkyBlue", { 0x87, 0xCE, 0xEB } },
{ "SlateBlue", { 0x6A, 0x5A, 0xCD } },
{ "SlateGray", { 0x70, 0x80, 0x90 } },
{ "Snow", { 0xFF, 0xFA, 0xFA } },
{ "SpringGreen", { 0x00, 0xFF, 0x7F } },
{ "SteelBlue", { 0x46, 0x82, 0xB4 } },
{ "Tan", { 0xD2, 0xB4, 0x8C } },
{ "Teal", { 0x00, 0x80, 0x80 } },
{ "Thistle", { 0xD8, 0xBF, 0xD8 } },
{ "Tomato", { 0xFF, 0x63, 0x47 } },
{ "Turquoise", { 0x40, 0xE0, 0xD0 } },
{ "Violet", { 0xEE, 0x82, 0xEE } },
{ "Wheat", { 0xF5, 0xDE, 0xB3 } },
{ "White", { 0xFF, 0xFF, 0xFF } },
{ "WhiteSmoke", { 0xF5, 0xF5, 0xF5 } },
{ "Yellow", { 0xFF, 0xFF, 0x00 } },
{ "YellowGreen", { 0x9A, 0xCD, 0x32 } },
};
static int color_table_compare(const void *lhs, const void *rhs)
{
return strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
}
int av_parse_color(uint8_t *rgba_color, const char *color_string, void *log_ctx)
{
if (!strcasecmp(color_string, "random") || !strcasecmp(color_string, "bikeshed")) {
int rgba = ff_random_get_seed();
rgba_color[0] = rgba >> 24;
rgba_color[1] = rgba >> 16;
rgba_color[2] = rgba >> 8;
rgba_color[3] = rgba;
} else
if (!strncmp(color_string, "0x", 2)) {
char *tail;
int len = strlen(color_string);
unsigned int rgba = strtoul(color_string, &tail, 16);
if (*tail || (len != 8 && len != 10)) {
av_log(log_ctx, AV_LOG_ERROR, "Invalid 0xRRGGBB[AA] color string: '%s'\n", color_string);
return AVERROR(EINVAL);
}
if (len == 10) {
rgba_color[3] = rgba;
rgba >>= 8;
}
rgba_color[0] = rgba >> 16;
rgba_color[1] = rgba >> 8;
rgba_color[2] = rgba;
} else {
const ColorEntry *entry = bsearch(color_string,
color_table,
FF_ARRAY_ELEMS(color_table),
sizeof(ColorEntry),
color_table_compare);
if (!entry) {
av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string);
return AVERROR(EINVAL);
}
memcpy(rgba_color, entry->rgba_color, 4);
}
return 0;
}
/**
* Stores the value in the field in ctx that is named like key.
* ctx must be an AVClass context, storing is done using AVOptions.
*
* @param buf the string to parse, buf will be updated to point at the
* separator just after the parsed key/value pair
* @param key_val_sep a 0-terminated list of characters used to
* separate key from value
* @param pairs_sep a 0-terminated list of characters used to separate
* two pairs from each other
* @return 0 if the key/value pair has been successfully parsed and
* set, or a negative value corresponding to an AVERROR code in case
* of error:
* AVERROR(EINVAL) if the key/value pair cannot be parsed,
* the error code issued by av_set_string3() if the key/value pair
* cannot be set
*/
static int parse_key_value_pair(void *ctx, const char **buf,
const char *key_val_sep, const char *pairs_sep)
{
char *key = av_get_token(buf, key_val_sep);
char *val;
int ret;
if (*key && strspn(*buf, key_val_sep)) {
(*buf)++;
val = av_get_token(buf, pairs_sep);
} else {
av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
av_free(key);
return AVERROR(EINVAL);
}
av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
ret = av_set_string3(ctx, key, val, 1, NULL);
if (ret == AVERROR(ENOENT))
av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
av_free(key);
av_free(val);
return ret;
}
int av_set_options_string(void *ctx, const char *opts,
const char *key_val_sep, const char *pairs_sep)
{
int ret, count = 0;
while (*opts) {
if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
return ret;
count++;
if (*opts)
opts++;
}
return count;
}
#ifdef TEST
#undef printf
typedef struct TestContext
{
const AVClass *class;
int num;
int toggle;
char *string;
int flags;
AVRational rational;
} TestContext;
#define OFFSET(x) offsetof(TestContext, x)
#define TEST_FLAG_COOL 01
#define TEST_FLAG_LAME 02
#define TEST_FLAG_MU 04
static const AVOption test_options[]= {
{"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, 0, 0, 100 },
{"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, 0, 0, 1 },
{"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, 0, 0, 10 },
{"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
{"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, 0, 0, INT_MAX, 0, "flags" },
{"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_COOL, INT_MIN, INT_MAX, 0, "flags" },
{"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_LAME, INT_MIN, INT_MAX, 0, "flags" },
{"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_MU, INT_MIN, INT_MAX, 0, "flags" },
{NULL},
};
static const char *test_get_name(void *ctx)
{
return "test";
}
static const AVClass test_class = {
"TestContext",
test_get_name,
test_options
};
int main(void)
{
int i;
const char *strings[] = {
"''",
"",
":",
"\\",
"'",
" '' :",
" '' '' :",
"foo '' :",
"'foo'",
"foo ",
"foo\\",
"foo': blah:blah",
"foo\\: blah:blah",
"foo\'",
"'foo : ' :blahblah",
"\\ :blah",
" foo",
" foo ",
" foo \\ ",
"foo ':blah",
" foo bar : blahblah",
"\\f\\o\\o",
"'foo : \\ \\ ' : blahblah",
"'\\fo\\o:': blahblah",
"\\'fo\\o\\:': foo ' :blahblah"
};
for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
const char *p= strings[i];
printf("|%s|", p);
printf(" -> |%s|", av_get_token(&p, ":"));
printf(" + |%s|\n", p);
}
printf("\nTesting av_parse_color()\n");
{
uint8_t rgba[4];
const char *color_names[] = {
"bikeshed",
"RaNdOm",
"foo",
"red",
"Red ",
"RED",
"Violet",
"Yellow",
"Red",
"0x000000",
"0x0000000",
"0xff000000",
"0x3e34ff",
"0x3e34ffaa",
"0xffXXee",
"0xfoobar",
"0xffffeeeeeeee",
};
av_log_set_level(AV_LOG_DEBUG);
for (int i = 0; i < FF_ARRAY_ELEMS(color_names); i++) {
if (av_parse_color(rgba, color_names[i], NULL) >= 0)
printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
}
}
printf("\nTesting av_set_options_string()\n");
{
TestContext test_ctx;
const char *options[] = {
"",
":",
"=",
"foo=:",
":=foo",
"=foo",
"foo=",
"foo",
"foo=val",
"foo==val",
"toggle=:",
"string=:",
"toggle=1 : foo",
"toggle=100",
"toggle==1",
"flags=+mu-lame : num=42: toggle=0",
"num=42 : string=blahblah",
"rational=0 : rational=1/2 : rational=1/-1",
"rational=-1/0",
};
test_ctx.class = &test_class;
av_opt_set_defaults2(&test_ctx, 0, 0);
test_ctx.string = av_strdup("default");
av_log_set_level(AV_LOG_DEBUG);
for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
printf("\n");
}
}
return 0;
}
#endif
|