FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
opt.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 #include <limits.h>
20 #include <stdio.h>
21 
22 #include "libavutil/common.h"
24 #include "libavutil/error.h"
25 #include "libavutil/log.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/rational.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 
31 typedef struct TestContext {
32  const AVClass *class;
33  int num;
34  int toggle;
35  char *string;
36  int flags;
39  int w, h;
42  int64_t duration;
44  int64_t channel_layout;
45  void *binary;
47  void *binary1;
49  void *binary2;
51  int64_t num64;
52  float flt;
53  double dbl;
54  char *escape;
55  int bool1;
56  int bool2;
57  int bool3;
58 } TestContext;
59 
60 #define OFFSET(x) offsetof(TestContext, x)
61 
62 #define TEST_FLAG_COOL 01
63 #define TEST_FLAG_LAME 02
64 #define TEST_FLAG_MU 04
65 
66 static const AVOption test_options[]= {
67  {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, 1 },
68  {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, 1 },
69  {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, 10, 1 },
70  {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { .str = "default" }, CHAR_MIN, CHAR_MAX, 1 },
71  {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, { .str = "\\=," }, CHAR_MIN, CHAR_MAX, 1 },
72  {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 1 }, 0, INT_MAX, 1, "flags" },
73  {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 1, "flags" },
74  {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 1, "flags" },
75  {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 1, "flags" },
76  {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str="200x300" }, 0, 0, 1 },
77  {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_0BGR }, -1, INT_MAX, 1 },
78  {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_S16 }, -1, INT_MAX, 1 },
79  {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, 1 },
80  {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 1000 }, 0, INT64_MAX, 1 },
81  {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, { .str = "pink" }, 0, 0, 1 },
82  {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, { .i64 = AV_CH_LAYOUT_HEXAGONAL }, 0, INT64_MAX, 1 },
83  {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, { .str="62696e00" }, 0, 0, 1 },
84  {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, { .str=NULL }, 0, 0, 1 },
85  {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, { .str="" }, 0, 0, 1 },
86  {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, { .i64 = 1 }, 0, 100, 1 },
87  {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 / 3 }, 0, 100, 1 },
88  {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 / 3 }, 0, 100, 1 },
89  {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, 1 },
90  {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, { .i64 = 1 }, -1, 1, 1 },
91  {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, 1 },
92  { NULL },
93 };
94 
95 static const char *test_get_name(void *ctx)
96 {
97  return "test";
98 }
99 
100 static const AVClass test_class = {
101  .class_name = "TestContext",
102  .item_name = test_get_name,
103  .option = test_options,
104 };
105 
106 static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
107 {
108  vfprintf(stdout, fmt, vl);
109 }
110 
111 int main(void)
112 {
113  int i;
114 
117 
118  printf("Testing default values\n");
119  {
120  TestContext test_ctx = { 0 };
121  test_ctx.class = &test_class;
122  av_opt_set_defaults(&test_ctx);
123 
124  printf("num=%d\n", test_ctx.num);
125  printf("toggle=%d\n", test_ctx.toggle);
126  printf("string=%s\n", test_ctx.string);
127  printf("escape=%s\n", test_ctx.escape);
128  printf("flags=%d\n", test_ctx.flags);
129  printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
130  printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
131  printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
132  printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
133  printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
134  printf("duration=%"PRId64"\n", test_ctx.duration);
135  printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
136  printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
137  if (test_ctx.binary)
138  printf("binary=%x %x %x %x\n", ((uint8_t*)test_ctx.binary)[0], ((uint8_t*)test_ctx.binary)[1], ((uint8_t*)test_ctx.binary)[2], ((uint8_t*)test_ctx.binary)[3]);
139  printf("binary_size=%d\n", test_ctx.binary_size);
140  printf("num64=%"PRId64"\n", test_ctx.num64);
141  printf("flt=%.6f\n", test_ctx.flt);
142  printf("dbl=%.6f\n", test_ctx.dbl);
143 
144  av_opt_show2(&test_ctx, NULL, -1, 0);
145 
146  av_opt_free(&test_ctx);
147  }
148 
149  printf("\nTesting av_opt_is_set_to_default()\n");
150  {
151  int ret;
152  TestContext test_ctx = { 0 };
153  const AVOption *o = NULL;
154  test_ctx.class = &test_class;
155 
157 
158  while (o = av_opt_next(&test_ctx, o)) {
159  ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
160  printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
161  }
162  av_opt_set_defaults(&test_ctx);
163  while (o = av_opt_next(&test_ctx, o)) {
164  ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
165  printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
166  }
167  av_opt_free(&test_ctx);
168  }
169 
170  printf("\nTest av_opt_serialize()\n");
171  {
172  TestContext test_ctx = { 0 };
173  char *buf;
174  test_ctx.class = &test_class;
175 
177 
178  av_opt_set_defaults(&test_ctx);
179  if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
180  printf("%s\n", buf);
181  av_opt_free(&test_ctx);
182  memset(&test_ctx, 0, sizeof(test_ctx));
183  test_ctx.class = &test_class;
184  av_set_options_string(&test_ctx, buf, "=", ",");
185  av_free(buf);
186  if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
187  printf("%s\n", buf);
188  av_free(buf);
189  }
190  }
191  av_opt_free(&test_ctx);
192  }
193 
194  printf("\nTesting av_set_options_string()\n");
195  {
196  TestContext test_ctx = { 0 };
197  static const char * const options[] = {
198  "",
199  ":",
200  "=",
201  "foo=:",
202  ":=foo",
203  "=foo",
204  "foo=",
205  "foo",
206  "foo=val",
207  "foo==val",
208  "toggle=:",
209  "string=:",
210  "toggle=1 : foo",
211  "toggle=100",
212  "toggle==1",
213  "flags=+mu-lame : num=42: toggle=0",
214  "num=42 : string=blahblah",
215  "rational=0 : rational=1/2 : rational=1/-1",
216  "rational=-1/0",
217  "size=1024x768",
218  "size=pal",
219  "size=bogus",
220  "pix_fmt=yuv420p",
221  "pix_fmt=2",
222  "pix_fmt=bogus",
223  "sample_fmt=s16",
224  "sample_fmt=2",
225  "sample_fmt=bogus",
226  "video_rate=pal",
227  "video_rate=25",
228  "video_rate=30000/1001",
229  "video_rate=30/1.001",
230  "video_rate=bogus",
231  "duration=bogus",
232  "duration=123.45",
233  "duration=1\\:23\\:45.67",
234  "color=blue",
235  "color=0x223300",
236  "color=0x42FF07AA",
237  "cl=stereo+downmix",
238  "cl=foo",
239  "bin=boguss",
240  "bin=111",
241  "bin=ffff",
242  "num64=bogus",
243  "num64=44",
244  "num64=44.4",
245  "num64=-1",
246  "num64=101",
247  "flt=bogus",
248  "flt=2",
249  "flt=2.2",
250  "flt=-1",
251  "flt=101",
252  "dbl=bogus",
253  "dbl=2",
254  "dbl=2.2",
255  "dbl=-1",
256  "dbl=101",
257  "bool1=true",
258  "bool2=auto",
259  };
260 
261  test_ctx.class = &test_class;
262  av_opt_set_defaults(&test_ctx);
263 
265 
266  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
267  int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
268  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
269  if (silence_log)
271  if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
272  printf("Error '%s'\n", options[i]);
273  else
274  printf("OK '%s'\n", options[i]);
276  }
277  av_opt_free(&test_ctx);
278  }
279 
280  printf("\nTesting av_opt_set_from_string()\n");
281  {
282  TestContext test_ctx = { 0 };
283  static const char * const options[] = {
284  "",
285  "5",
286  "5:hello",
287  "5:hello:size=pal",
288  "5:size=pal:hello",
289  ":",
290  "=",
291  " 5 : hello : size = pal ",
292  "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
293  };
294  static const char * const shorthand[] = { "num", "string", NULL };
295 
296  test_ctx.class = &test_class;
297  av_opt_set_defaults(&test_ctx);
298 
300 
301  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
302  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
303  if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
304  printf("Error '%s'\n", options[i]);
305  else
306  printf("OK '%s'\n", options[i]);
307  }
308  av_opt_free(&test_ctx);
309  }
310 
311  return 0;
312 }
#define NULL
Definition: coverity.c:32
char * escape
Definition: opt.c:54
static enum AVPixelFormat pix_fmt
#define OFFSET(x)
Definition: opt.c:60
AVOption.
Definition: opt.h:246
const char * fmt
Definition: avisynth_c.h:769
Memory handling functions.
void av_log_set_level(int level)
Set the log level.
Definition: log.c:385
enum AVPixelFormat pix_fmt
Definition: opt.c:40
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1305
int num
Numerator.
Definition: rational.h:59
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:1426
int toggle
Definition: opt.c:34
int w
Definition: opt.c:39
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:239
int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer, const char key_val_sep, const char pairs_sep)
Serialize object's options.
Definition: opt.c:1994
int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
Check if given option is set to its default value.
Definition: opt.c:1982
#define AV_LOG_QUIET
Print no output.
Definition: log.h:158
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
Definition: opt.c:1506
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
const char * name
Definition: opt.h:247
uint8_t
enum AVSampleFormat sample_fmt
Definition: opt.c:41
AVOptions.
int flags
Definition: opt.c:36
#define AV_CH_LAYOUT_HEXAGONAL
int64_t duration
Definition: movenc.c:63
int64_t num64
Definition: opt.c:51
#define av_log(a,...)
AVRational video_rate
Definition: opt.c:38
error code definitions
AVRational rational
Definition: opt.c:37
uint8_t color[4]
Definition: opt.c:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:45
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:49
static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
Definition: opt.c:106
int binary_size2
Definition: opt.c:50
audio channel layout utility functions
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition: log.c:400
uint8_t w
Definition: llviddspenc.c:38
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
int bool3
Definition: opt.c:57
#define FF_ARRAY_ELEMS(a)
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:229
void * binary2
Definition: opt.c:49
float flt
Definition: opt.c:52
int h
Definition: opt.c:39
static const AVClass test_class
Definition: opt.c:100
int bool2
Definition: opt.c:56
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
Definition: opt.c:1293
static const AVOption test_options[]
Definition: opt.c:66
int bool1
Definition: opt.c:55
int64_t channel_layout
Definition: opt.c:44
void * buf
Definition: avisynth_c.h:690
#define TEST_FLAG_LAME
Definition: opt.c:63
Describe the class of an AVClass context structure.
Definition: log.h:67
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int64_t duration
Definition: opt.c:42
offset must point to AVRational
Definition: opt.h:236
offset must point to two consecutive integers
Definition: opt.h:233
const AVClass * class
Definition: opt.c:32
#define TEST_FLAG_MU
Definition: opt.c:64
#define flags(name, subs,...)
Definition: cbs_av1.c:596
uint8_t level
Definition: svq3.c:207
char * string
Definition: opt.c:35
static const char * test_get_name(void *ctx)
Definition: opt.c:95
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1558
const OptionDef options[]
Definition: ffmpeg_opt.c:3324
int main(void)
Definition: opt.c:111
#define TEST_FLAG_COOL
Definition: opt.c:62
common internal and external API header
Utilties for rational number calculation.
signed 16 bits
Definition: samplefmt.h:61
int den
Denominator.
Definition: rational.h:60
#define av_free(p)
int binary_size
Definition: opt.c:46
void * binary
Definition: opt.c:45
int binary_size1
Definition: opt.c:48
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2362
void * binary1
Definition: opt.c:47
int num
Definition: opt.c:33
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
double dbl
Definition: opt.c:53