Change eval API to take parent log context and log level offset.
[libav.git] / libavcodec / opt.c
CommitLineData
860a40c8
MN
1/*
2 * AVOptions
3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
860a40c8
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
860a40c8 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
860a40c8
MN
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
860a40c8 20 */
115329f1 21
860a40c8 22/**
ba87f080 23 * @file
860a40c8
MN
24 * AVOptions
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
115329f1 27
860a40c8 28#include "avcodec.h"
6fd46414 29#include "opt.h"
4d7b4613 30#include "eval.h"
860a40c8
MN
31
32//FIXME order them and do a bin search
bc2a1c34 33const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags){
860a40c8 34 AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
f8a32f45 35 const AVOption *o= c->option;
115329f1 36
860a40c8 37 for(;o && o->name; o++){
a4072c45 38 if(!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags )
860a40c8
MN
39 return o;
40 }
41 return NULL;
42}
43
f8a32f45 44const AVOption *av_next_option(void *obj, const AVOption *last){
3735e82f
MN
45 if(last && last[1].name) return ++last;
46 else if(last) return NULL;
47 else return (*(AVClass**)obj)->option;
48}
49
301cc4f3 50static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out){
bc2a1c34 51 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
860a40c8 52 void *dst;
301cc4f3
SS
53 if(o_out)
54 *o_out= o;
115329f1 55 if(!o || o->offset<=0)
301cc4f3 56 return AVERROR(ENOENT);
115329f1 57
fcc9b956 58 if(o->max*den < num*intnum || o->min*den > num*intnum) {
50f68bde 59 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
301cc4f3 60 return AVERROR(ERANGE);
fcc9b956 61 }
115329f1 62
860a40c8
MN
63 dst= ((uint8_t*)obj) + o->offset;
64
65 switch(o->type){
115329f1 66 case FF_OPT_TYPE_FLAGS:
378774e9
MN
67 case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
68 case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
8bbf6db9
MN
69 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
70 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
860a40c8 71 case FF_OPT_TYPE_RATIONAL:
8bbf6db9
MN
72 if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
73 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
603972fc 74 break;
860a40c8 75 default:
301cc4f3 76 return AVERROR(EINVAL);
860a40c8 77 }
301cc4f3
SS
78 return 0;
79}
80
81static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
82 const AVOption *o = NULL;
83 if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
84 return NULL;
85 else
86 return o;
860a40c8
MN
87}
88
cf2baeb3 89static const double const_values[]={
95c99430
PI
90 M_PI,
91 M_E,
92 FF_QP2LAMBDA,
93 0
94};
95
cf2baeb3 96static const char * const const_names[]={
95c99430
PI
97 "PI",
98 "E",
99 "QP2LAMBDA",
100 0
101};
102
841f39a7
RD
103static int hexchar2int(char c) {
104 if (c >= '0' && c <= '9') return c - '0';
105 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
106 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
107 return -1;
108}
109
7b1454c4
SS
110int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out){
111 int ret;
bc2a1c34 112 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
7b1454c4
SS
113 if (o_out)
114 *o_out = o;
1fe4abf3 115 if(!o)
7b1454c4
SS
116 return AVERROR(ENOENT);
117 if(!val || o->offset<=0)
118 return AVERROR(EINVAL);
119
841f39a7
RD
120 if(o->type == FF_OPT_TYPE_BINARY){
121 uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
122 int *lendst = (int *)(dst + 1);
123 uint8_t *bin, *ptr;
124 int len = strlen(val);
125 av_freep(dst);
126 *lendst = 0;
7b1454c4 127 if (len & 1) return AVERROR(EINVAL);
841f39a7
RD
128 len /= 2;
129 ptr = bin = av_malloc(len);
130 while (*val) {
131 int a = hexchar2int(*val++);
132 int b = hexchar2int(*val++);
133 if (a < 0 || b < 0) {
134 av_free(bin);
7b1454c4 135 return AVERROR(EINVAL);
841f39a7
RD
136 }
137 *ptr++ = (a << 4) | b;
138 }
139 *dst = bin;
140 *lendst = len;
7b1454c4 141 return 0;
841f39a7 142 }
860a40c8 143 if(o->type != FF_OPT_TYPE_STRING){
9d2e6e84 144 int notfirst=0;
860a40c8
MN
145 for(;;){
146 int i;
95c99430 147 char buf[256];
233f6f88
MN
148 int cmd=0;
149 double d;
860a40c8 150
233f6f88
MN
151 if(*val == '+' || *val == '-')
152 cmd= *(val++);
115329f1 153
233f6f88 154 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
860a40c8
MN
155 buf[i]= val[i];
156 buf[i]=0;
115329f1 157
2b65bb45 158 {
bc2a1c34 159 const AVOption *o_named= av_find_opt(obj, buf, o->unit, 0, 0);
115329f1 160 if(o_named && o_named->type == FF_OPT_TYPE_CONST)
233f6f88
MN
161 d= o_named->default_val;
162 else if(!strcmp(buf, "default")) d= o->default_val;
163 else if(!strcmp(buf, "max" )) d= o->max;
164 else if(!strcmp(buf, "min" )) d= o->min;
226d6947
LW
165 else if(!strcmp(buf, "none" )) d= 0;
166 else if(!strcmp(buf, "all" )) d= ~0;
95c99430 167 else {
2b65bb45
MN
168 d = ff_parse_and_eval_expr(buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
169 if (isnan(d)){
170 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
7b1454c4 171 return AVERROR(EINVAL);
2b65bb45 172 }
95c99430 173 }
860a40c8 174 }
233f6f88
MN
175 if(o->type == FF_OPT_TYPE_FLAGS){
176 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
177 else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
4a13a2d4 178 }else{
9d2e6e84
MN
179 if (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
180 else if(cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
4a13a2d4 181 }
860a40c8 182
7b1454c4
SS
183 if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
184 return ret;
9203741a 185 val+= i;
860a40c8 186 if(!*val)
7b1454c4 187 return 0;
9d2e6e84 188 notfirst=1;
860a40c8 189 }
7b1454c4 190 return AVERROR(EINVAL);
860a40c8 191 }
115329f1 192
8dbee653 193 if(alloc){
f9690589 194 av_free(*(void**)(((uint8_t*)obj) + o->offset));
8dbee653
MN
195 val= av_strdup(val);
196 }
197
24cdc39e 198 memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
7b1454c4
SS
199 return 0;
200}
201
64e74b68 202#if LIBAVCODEC_VERSION_MAJOR < 53
7b1454c4
SS
203const AVOption *av_set_string2(void *obj, const char *name, const char *val, int alloc){
204 const AVOption *o;
205 if (av_set_string3(obj, name, val, alloc, &o) < 0)
206 return NULL;
8bbf6db9 207 return o;
860a40c8
MN
208}
209
8dbee653 210const AVOption *av_set_string(void *obj, const char *name, const char *val){
f16dd7e6
SS
211 const AVOption *o;
212 if (av_set_string3(obj, name, val, 0, &o) < 0)
213 return NULL;
214 return o;
8dbee653 215}
64e74b68 216#endif
8dbee653 217
f8a32f45 218const AVOption *av_set_double(void *obj, const char *name, double n){
860a40c8
MN
219 return av_set_number(obj, name, n, 1, 1);
220}
221
f8a32f45 222const AVOption *av_set_q(void *obj, const char *name, AVRational n){
860a40c8
MN
223 return av_set_number(obj, name, n.num, n.den, 1);
224}
225
f8a32f45 226const AVOption *av_set_int(void *obj, const char *name, int64_t n){
860a40c8
MN
227 return av_set_number(obj, name, 1, 1, n);
228}
229
8bbf6db9 230/**
115329f1 231 *
8bbf6db9
MN
232 * @param buf a buffer which is used for returning non string values as strings, can be NULL
233 * @param buf_len allocated length in bytes of buf
234 */
f8a32f45 235const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len){
bc2a1c34 236 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
8bbf6db9 237 void *dst;
841f39a7
RD
238 uint8_t *bin;
239 int len, i;
860a40c8
MN
240 if(!o || o->offset<=0)
241 return NULL;
8bbf6db9 242 if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
860a40c8
MN
243 return NULL;
244
8bbf6db9
MN
245 dst= ((uint8_t*)obj) + o->offset;
246 if(o_out) *o_out= o;
115329f1 247
8bbf6db9 248 switch(o->type){
233f6f88 249 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
8bbf6db9 250 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
4733abcb 251 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
8bbf6db9
MN
252 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
253 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
254 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
b81f2b3d 255 case FF_OPT_TYPE_STRING: return *(void**)dst;
841f39a7
RD
256 case FF_OPT_TYPE_BINARY:
257 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
258 if(len >= (buf_len + 1)/2) return NULL;
259 bin = *(uint8_t**)dst;
260 for(i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
261 break;
8bbf6db9
MN
262 default: return NULL;
263 }
264 return buf;
860a40c8
MN
265}
266
f8a32f45 267static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum){
bc2a1c34 268 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
860a40c8
MN
269 void *dst;
270 if(!o || o->offset<=0)
8bbf6db9 271 goto error;
860a40c8
MN
272
273 dst= ((uint8_t*)obj) + o->offset;
274
8bbf6db9
MN
275 if(o_out) *o_out= o;
276
860a40c8 277 switch(o->type){
378774e9 278 case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
8bbf6db9
MN
279 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
280 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
281 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
282 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
115329f1 283 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
8bbf6db9
MN
284 *den = ((AVRational*)dst)->den;
285 return 0;
860a40c8 286 }
8bbf6db9
MN
287error:
288 *den=*intnum=0;
289 return -1;
290}
291
f8a32f45 292double av_get_double(void *obj, const char *name, const AVOption **o_out){
8bbf6db9
MN
293 int64_t intnum=1;
294 double num=1;
295 int den=1;
296
297 av_get_number(obj, name, o_out, &num, &den, &intnum);
298 return num*intnum/den;
299}
300
f8a32f45 301AVRational av_get_q(void *obj, const char *name, const AVOption **o_out){
8bbf6db9
MN
302 int64_t intnum=1;
303 double num=1;
304 int den=1;
305
306 av_get_number(obj, name, o_out, &num, &den, &intnum);
307 if(num == 1.0 && (int)intnum == intnum)
308 return (AVRational){intnum, den};
309 else
310 return av_d2q(num*intnum/den, 1<<24);
311}
312
f8a32f45 313int64_t av_get_int(void *obj, const char *name, const AVOption **o_out){
8bbf6db9
MN
314 int64_t intnum=1;
315 double num=1;
316 int den=1;
317
318 av_get_number(obj, name, o_out, &num, &den, &intnum);
319 return num*intnum/den;
320}
321
f8a32f45 322static void opt_list(void *obj, void *av_log_obj, const char *unit)
d70ef6d7 323{
f8a32f45 324 const AVOption *opt=NULL;
115329f1 325
8bbf6db9
MN
326 while((opt= av_next_option(obj, opt))){
327 if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
328 continue;
115329f1 329
d70ef6d7
PI
330 /* Don't print CONST's on level one.
331 * Don't print anything but CONST's on level two.
332 * Only print items from the requested unit.
333 */
334 if (!unit && opt->type==FF_OPT_TYPE_CONST)
335 continue;
336 else if (unit && opt->type!=FF_OPT_TYPE_CONST)
337 continue;
338 else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
339 continue;
340 else if (unit && opt->type == FF_OPT_TYPE_CONST)
341 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
342 else
343 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
88d6903e
AC
344
345 switch( opt->type )
346 {
347 case FF_OPT_TYPE_FLAGS:
348 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>" );
349 break;
350 case FF_OPT_TYPE_INT:
351 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int>" );
352 break;
353 case FF_OPT_TYPE_INT64:
354 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>" );
355 break;
356 case FF_OPT_TYPE_DOUBLE:
357 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<double>" );
358 break;
359 case FF_OPT_TYPE_FLOAT:
360 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<float>" );
361 break;
362 case FF_OPT_TYPE_STRING:
363 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<string>" );
364 break;
365 case FF_OPT_TYPE_RATIONAL:
366 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>" );
367 break;
841f39a7
RD
368 case FF_OPT_TYPE_BINARY:
369 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>" );
370 break;
88d6903e
AC
371 case FF_OPT_TYPE_CONST:
372 default:
373 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "" );
374 break;
375 }
a1689e2f
MN
376 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
377 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
378 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
379 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
380 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
115329f1 381
d2752a0d
MN
382 if(opt->help)
383 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
384 av_log(av_log_obj, AV_LOG_INFO, "\n");
d70ef6d7
PI
385 if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
386 opt_list(obj, av_log_obj, opt->unit);
387 }
8bbf6db9 388 }
d70ef6d7
PI
389}
390
391int av_opt_show(void *obj, void *av_log_obj){
392 if(!obj)
393 return -1;
394
395 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
396
397 opt_list(obj, av_log_obj, NULL);
398
8bbf6db9 399 return 0;
860a40c8 400}
73a8ceaa 401
493f7857
PI
402/** Set the values of the AVCodecContext or AVFormatContext structure.
403 * They are set to the defaults specified in the according AVOption options
404 * array default_val field.
405 *
406 * @param s AVCodecContext or AVFormatContext for which the defaults will be set
407 */
20e708cb 408void av_opt_set_defaults2(void *s, int mask, int flags)
73a8ceaa 409{
f8a32f45 410 const AVOption *opt = NULL;
73a8ceaa 411 while ((opt = av_next_option(s, opt)) != NULL) {
20e708cb
MN
412 if((opt->flags & mask) != flags)
413 continue;
73a8ceaa
PI
414 switch(opt->type) {
415 case FF_OPT_TYPE_CONST:
416 /* Nothing to be done here */
417 break;
418 case FF_OPT_TYPE_FLAGS:
419 case FF_OPT_TYPE_INT: {
420 int val;
421 val = opt->default_val;
422 av_set_int(s, opt->name, val);
423 }
424 break;
36d5b175
MN
425 case FF_OPT_TYPE_INT64:
426 if((double)(opt->default_val+0.6) == opt->default_val)
e8a9d018 427 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
36d5b175
MN
428 av_set_int(s, opt->name, opt->default_val);
429 break;
73a8ceaa
PI
430 case FF_OPT_TYPE_FLOAT: {
431 double val;
432 val = opt->default_val;
433 av_set_double(s, opt->name, val);
434 }
435 break;
436 case FF_OPT_TYPE_RATIONAL: {
437 AVRational val;
438 val = av_d2q(opt->default_val, INT_MAX);
439 av_set_q(s, opt->name, val);
440 }
441 break;
442 case FF_OPT_TYPE_STRING:
841f39a7 443 case FF_OPT_TYPE_BINARY:
73a8ceaa
PI
444 /* Cannot set default for string as default_val is of type * double */
445 break;
446 default:
447 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
448 }
449 }
450}
451
20e708cb
MN
452void av_opt_set_defaults(void *s){
453 av_opt_set_defaults2(s, 0, 0);
454}
455