Libav
opt.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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"
23 #include "libavutil/error.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/rational.h"
27 #include "libavutil/opt.h"
28 
29 typedef struct TestContext {
30  const AVClass *class;
31  int num;
32  int toggle;
33  char *string;
34  int flags;
36 } TestContext;
37 
38 #define OFFSET(x) offsetof(TestContext, x)
39 
40 #define TEST_FLAG_COOL 01
41 #define TEST_FLAG_LAME 02
42 #define TEST_FLAG_MU 04
43 
44 static const AVOption test_options[] = {
45  { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100 },
46  { "toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1 },
47  { "rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, 10 },
48  { "string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { 0 }, CHAR_MIN, CHAR_MAX },
49  { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, 0, "flags"},
50  { "cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 0, "flags"},
51  { "lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 0, "flags"},
52  { "mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 0, "flags"},
53  { NULL },
54 };
55 
56 static const char *test_get_name(void *ctx)
57 {
58  return "test";
59 }
60 
61 static const AVClass test_class = {
62  "TestContext",
64  test_options
65 };
66 
67 int main(void)
68 {
69  int i;
70  TestContext test_ctx = { .class = &test_class };
71  static const char *options[] = {
72  "",
73  ":",
74  "=",
75  "foo=:",
76  ":=foo",
77  "=foo",
78  "foo=",
79  "foo",
80  "foo=val",
81  "foo==val",
82  "toggle=:",
83  "string=:",
84  "toggle=1 : foo",
85  "toggle=100",
86  "toggle==1",
87  "flags=+mu-lame : num=42: toggle=0",
88  "num=42 : string=blahblah",
89  "rational=0 : rational=1/2 : rational=1/-1",
90  "rational=-1/0",
91  };
92 
93  printf("\nTesting av_set_options_string()\n");
94 
95  av_opt_set_defaults(&test_ctx);
96  test_ctx.string = av_strdup("default");
97  if (!test_ctx.string)
98  return AVERROR(ENOMEM);
99 
101 
102  for (i = 0; i < FF_ARRAY_ELEMS(options); i++) {
103  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
104  if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
105  av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
106  printf("\n");
107  }
108 
109  return 0;
110 }
#define OFFSET(x)
Definition: opt.c:38
AVOption.
Definition: opt.h:234
memory handling functions
void av_log_set_level(int level)
Set the log level.
Definition: log.c:205
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:599
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:695
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int toggle
Definition: opt.c:32
#define FF_ARRAY_ELEMS(a)
AVOptions.
int flags
Definition: opt.c:34
const OptionDef options[]
Definition: avconv_opt.c:2447
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
error code definitions
AVRational rational
Definition: opt.c:35
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
AVFormatContext * ctx
Definition: movenc.c:48
NULL
Definition: eval.c:55
static const AVClass test_class
Definition: opt.c:61
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:219
static const AVOption test_options[]
Definition: opt.c:44
#define TEST_FLAG_LAME
Definition: opt.c:41
Describe the class of an AVClass context structure.
Definition: log.h:34
rational number numerator/denominator
Definition: rational.h:43
const AVClass * class
Definition: opt.c:30
#define TEST_FLAG_MU
Definition: opt.c:42
char * string
Definition: opt.c:33
static const char * test_get_name(void *ctx)
Definition: opt.c:56
int main(void)
Definition: opt.c:67
#define TEST_FLAG_COOL
Definition: opt.c:40
common internal and external API header
rational numbers
int num
Definition: opt.c:31