5b643db287478b1c854871d8c47aecd12bb50ca2
[libav.git] / libavcodec / opts.c
1 /*
2 * LGPL
3 */
4
5 /**
6 * @file opts.c
7 * options parser.
8 * typical parsed command line:
9 * msmpeg4:bitrate=720000:qmax=16
10 *
11 */
12
13 #include "avcodec.h"
14
15 #ifdef CONFIG_OS2
16 #define strcasecmp(s1,s2) stricmp(s1,s2)
17 #endif
18
19 const AVOption avoptions_common[] = {
20 AVOPTION_CODEC_FLAG("bit_exact", "use only bit-exact stuff", flags, CODEC_FLAG_BITEXACT, 0),
21 AVOPTION_CODEC_FLAG("mm_force", "force mm flags", dsp_mask, FF_MM_FORCE, 0),
22 #ifdef HAVE_MMX
23 AVOPTION_CODEC_FLAG("mm_mmx", "mask MMX feature", dsp_mask, FF_MM_MMX, 0),
24 AVOPTION_CODEC_FLAG("mm_3dnow", "mask 3DNow feature", dsp_mask, FF_MM_3DNOW, 0),
25 AVOPTION_CODEC_FLAG("mm_mmxext", "mask MMXEXT (MMX2) feature", dsp_mask, FF_MM_MMXEXT, 0),
26 AVOPTION_CODEC_FLAG("mm_sse", "mask SSE feature", dsp_mask, FF_MM_SSE, 0),
27 AVOPTION_CODEC_FLAG("mm_sse2", "mask SSE2 feature", dsp_mask, FF_MM_SSE2, 0),
28 #endif
29 AVOPTION_END()
30 };
31
32 const AVOption avoptions_workaround_bug[] = {
33 AVOPTION_CODEC_FLAG("bug_autodetect", "workaround bug autodetection", workaround_bugs, FF_BUG_AUTODETECT, 1),
34 AVOPTION_CODEC_FLAG("bug_old_msmpeg4", "workaround old msmpeg4 bug", workaround_bugs, FF_BUG_OLD_MSMPEG4, 0),
35 AVOPTION_CODEC_FLAG("bug_xvid_ilace", "workaround XviD interlace bug", workaround_bugs, FF_BUG_XVID_ILACE, 0),
36 AVOPTION_CODEC_FLAG("bug_ump4", "workaround ump4 bug", workaround_bugs, FF_BUG_UMP4, 0),
37 AVOPTION_CODEC_FLAG("bug_no_padding", "workaround padding bug", workaround_bugs, FF_BUG_NO_PADDING, 0),
38 AVOPTION_CODEC_FLAG("bug_ac_vlc", "workaround ac VLC bug", workaround_bugs, FF_BUG_AC_VLC, 0),
39 AVOPTION_CODEC_FLAG("bug_qpel_chroma", "workaround qpel chroma bug", workaround_bugs, FF_BUG_QPEL_CHROMA, 0),
40 AVOPTION_CODEC_FLAG("bug_std_qpel", "workaround std qpel bug", workaround_bugs, FF_BUG_STD_QPEL, 0),
41 AVOPTION_CODEC_FLAG("bug_qpel_chroma2", "workaround qpel chroma2 bug", workaround_bugs, FF_BUG_QPEL_CHROMA2, 0),
42 AVOPTION_CODEC_FLAG("bug_direct_blocksize", "workaround direct blocksize bug", workaround_bugs, FF_BUG_DIRECT_BLOCKSIZE, 0),
43 AVOPTION_END()
44 };
45
46
47 static int parse_bool(const AVOption *c, char *s, int *var)
48 {
49 int b = 1; /* by default -on- when present */
50 if (s) {
51 if (!strcasecmp(s, "off") || !strcasecmp(s, "false")
52 || !strcmp(s, "0"))
53 b = 0;
54 else if (!strcasecmp(s, "on") || !strcasecmp(s, "true")
55 || !strcmp(s, "1"))
56 b = 1;
57 else
58 return -1;
59 }
60
61 if (c->type == FF_OPT_TYPE_FLAG) {
62 if (b)
63 *var |= (int)c->min;
64 else
65 *var &= ~(int)c->min;
66 } else
67 *var = b;
68 return 0;
69 }
70
71 static int parse_double(const AVOption *c, char *s, double *var)
72 {
73 double d;
74 if (!s)
75 return -1;
76 d = atof(s);
77 if (c->min != c->max) {
78 if (d < c->min || d > c->max) {
79 fprintf(stderr, "Option: %s double value: %f out of range <%f, %f>\n",
80 c->name, d, c->min, c->max);
81 return -1;
82 }
83 }
84 *var = d;
85 return 0;
86 }
87
88 static int parse_int(const AVOption* c, char* s, int* var)
89 {
90 int i;
91 if (!s)
92 return -1;
93 i = atoi(s);
94 if (c->min != c->max) {
95 if (i < (int)c->min || i > (int)c->max) {
96 fprintf(stderr, "Option: %s integer value: %d out of range <%d, %d>\n",
97 c->name, i, (int)c->min, (int)c->max);
98 return -1;
99 }
100 }
101 *var = i;
102 return 0;
103 }
104
105 static int parse_string(const AVOption *c, char *s, void* strct, char **var)
106 {
107 if (!s)
108 return -1;
109
110 if (c->type == FF_OPT_TYPE_RCOVERRIDE) {
111 int sf, ef, qs;
112 float qf;
113 if (sscanf(s, "%d,%d,%d,%f", &sf, &ef, &qs, &qf) == 4 && sf < ef) {
114 AVCodecContext *avctx = (AVCodecContext *) strct;
115 RcOverride *o;
116 avctx->rc_override = av_realloc(avctx->rc_override,
117 sizeof(RcOverride) * (avctx->rc_override_count + 1));
118 o = avctx->rc_override + avctx->rc_override_count++;
119 o->start_frame = sf;
120 o->end_frame = ef;
121 o->qscale = qs;
122 o->quality_factor = qf;
123
124 //printf("parsed Rc: %d,%d,%d,%f (%d)\n", sf,ef,qs,qf, avctx->rc_override_count);
125 } else {
126 printf("incorrect/unparsable Rc: \"%s\"\n", s);
127 }
128 } else
129 *var = av_strdup(s);
130 return 0;
131 }
132
133 int avoption_parse(void* strct, const AVOption* list, const char *opts)
134 {
135 int r = 0;
136 char* dopts = av_strdup(opts);
137 if (dopts) {
138 char *str = dopts;
139
140 while (str && *str && r == 0) {
141 const AVOption *stack[FF_OPT_MAX_DEPTH];
142 const AVOption *c = list;
143 int depth = 0;
144 char* e = strchr(str, ':');
145 char* p;
146 if (e)
147 *e++ = 0;
148
149 p = strchr(str, '=');
150 if (p)
151 *p++ = 0;
152
153 // going through option structures
154 for (;;) {
155 if (!c->name) {
156 if (c->help) {
157 stack[depth++] = c;
158 c = (const AVOption*) c->help;
159 assert(depth > FF_OPT_MAX_DEPTH);
160 } else {
161 if (depth == 0)
162 break; // finished
163 c = stack[--depth];
164 c++;
165 }
166 } else {
167 if (!strcmp(c->name, str)) {
168 void* ptr = (char*)strct + c->offset;
169
170 switch (c->type & FF_OPT_TYPE_MASK) {
171 case FF_OPT_TYPE_BOOL:
172 r = parse_bool(c, p, (int*)ptr);
173 break;
174 case FF_OPT_TYPE_DOUBLE:
175 r = parse_double(c, p, (double*)ptr);
176 break;
177 case FF_OPT_TYPE_INT:
178 r = parse_int(c, p, (int*)ptr);
179 break;
180 case FF_OPT_TYPE_STRING:
181 r = parse_string(c, p, strct, (char**)ptr);
182 break;
183 default:
184 assert(0 == 1);
185 }
186 }
187 c++;
188 }
189 }
190 str = e;
191 }
192 av_free(dopts);
193 }
194 return r;
195 }