00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "avutil.h"
00029 #include "avstring.h"
00030 #include "opt.h"
00031 #include "eval.h"
00032
00033
00034 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
00035 {
00036 AVClass *c= *(AVClass**)v;
00037 const AVOption *o= c->option;
00038
00039 for (; o && o->name; o++) {
00040 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
00041 return o;
00042 }
00043 return NULL;
00044 }
00045
00046 const AVOption *av_next_option(void *obj, const AVOption *last)
00047 {
00048 if (last && last[1].name) return ++last;
00049 else if (last) return NULL;
00050 else return (*(AVClass**)obj)->option;
00051 }
00052
00053 static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out)
00054 {
00055 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
00056 void *dst;
00057 if (o_out)
00058 *o_out= o;
00059 if (!o || o->offset<=0)
00060 return AVERROR(ENOENT);
00061
00062 if (o->max*den < num*intnum || o->min*den > num*intnum) {
00063 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
00064 return AVERROR(ERANGE);
00065 }
00066
00067 dst= ((uint8_t*)obj) + o->offset;
00068
00069 switch (o->type) {
00070 case FF_OPT_TYPE_FLAGS:
00071 case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
00072 case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
00073 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
00074 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
00075 case FF_OPT_TYPE_RATIONAL:
00076 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
00077 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
00078 break;
00079 default:
00080 return AVERROR(EINVAL);
00081 }
00082 return 0;
00083 }
00084
00085 static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum)
00086 {
00087 const AVOption *o = NULL;
00088 if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
00089 return NULL;
00090 else
00091 return o;
00092 }
00093
00094 static const double const_values[] = {
00095 M_PI,
00096 M_E,
00097 FF_QP2LAMBDA,
00098 0
00099 };
00100
00101 static const char * const const_names[] = {
00102 "PI",
00103 "E",
00104 "QP2LAMBDA",
00105 0
00106 };
00107
00108 static int hexchar2int(char c) {
00109 if (c >= '0' && c <= '9') return c - '0';
00110 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
00111 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
00112 return -1;
00113 }
00114
00115 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
00116 {
00117 int ret;
00118 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
00119 if (o_out)
00120 *o_out = o;
00121 if (!o)
00122 return AVERROR(ENOENT);
00123 if (!val || o->offset<=0)
00124 return AVERROR(EINVAL);
00125
00126 if (o->type == FF_OPT_TYPE_BINARY) {
00127 uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
00128 int *lendst = (int *)(dst + 1);
00129 uint8_t *bin, *ptr;
00130 int len = strlen(val);
00131 av_freep(dst);
00132 *lendst = 0;
00133 if (len & 1) return AVERROR(EINVAL);
00134 len /= 2;
00135 ptr = bin = av_malloc(len);
00136 while (*val) {
00137 int a = hexchar2int(*val++);
00138 int b = hexchar2int(*val++);
00139 if (a < 0 || b < 0) {
00140 av_free(bin);
00141 return AVERROR(EINVAL);
00142 }
00143 *ptr++ = (a << 4) | b;
00144 }
00145 *dst = bin;
00146 *lendst = len;
00147 return 0;
00148 }
00149 if (o->type != FF_OPT_TYPE_STRING) {
00150 int notfirst=0;
00151 for (;;) {
00152 int i;
00153 char buf[256];
00154 int cmd=0;
00155 double d;
00156
00157 if (*val == '+' || *val == '-')
00158 cmd= *(val++);
00159
00160 for (i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
00161 buf[i]= val[i];
00162 buf[i]=0;
00163
00164 {
00165 const AVOption *o_named= av_find_opt(obj, buf, o->unit, 0, 0);
00166 if (o_named && o_named->type == FF_OPT_TYPE_CONST)
00167 d= o_named->default_val;
00168 else if (!strcmp(buf, "default")) d= o->default_val;
00169 else if (!strcmp(buf, "max" )) d= o->max;
00170 else if (!strcmp(buf, "min" )) d= o->min;
00171 else if (!strcmp(buf, "none" )) d= 0;
00172 else if (!strcmp(buf, "all" )) d= ~0;
00173 else {
00174 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
00175 if (res < 0) {
00176 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
00177 return res;
00178 }
00179 }
00180 }
00181 if (o->type == FF_OPT_TYPE_FLAGS) {
00182 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
00183 else if (cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
00184 } else {
00185 if (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
00186 else if (cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
00187 }
00188
00189 if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
00190 return ret;
00191 val+= i;
00192 if (!*val)
00193 return 0;
00194 notfirst=1;
00195 }
00196 return AVERROR(EINVAL);
00197 }
00198
00199 if (alloc) {
00200 av_free(*(void**)(((uint8_t*)obj) + o->offset));
00201 val= av_strdup(val);
00202 }
00203
00204 memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
00205 return 0;
00206 }
00207
00208 const AVOption *av_set_double(void *obj, const char *name, double n)
00209 {
00210 return av_set_number(obj, name, n, 1, 1);
00211 }
00212
00213 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
00214 {
00215 return av_set_number(obj, name, n.num, n.den, 1);
00216 }
00217
00218 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
00219 {
00220 return av_set_number(obj, name, 1, 1, n);
00221 }
00222
00228 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
00229 {
00230 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
00231 void *dst;
00232 uint8_t *bin;
00233 int len, i;
00234 if (!o || o->offset<=0)
00235 return NULL;
00236 if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
00237 return NULL;
00238
00239 dst= ((uint8_t*)obj) + o->offset;
00240 if (o_out) *o_out= o;
00241
00242 switch (o->type) {
00243 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
00244 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
00245 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
00246 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
00247 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
00248 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
00249 case FF_OPT_TYPE_STRING: return *(void**)dst;
00250 case FF_OPT_TYPE_BINARY:
00251 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
00252 if (len >= (buf_len + 1)/2) return NULL;
00253 bin = *(uint8_t**)dst;
00254 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
00255 break;
00256 default: return NULL;
00257 }
00258 return buf;
00259 }
00260
00261 static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
00262 {
00263 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
00264 void *dst;
00265 if (!o || o->offset<=0)
00266 goto error;
00267
00268 dst= ((uint8_t*)obj) + o->offset;
00269
00270 if (o_out) *o_out= o;
00271
00272 switch (o->type) {
00273 case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
00274 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
00275 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
00276 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
00277 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
00278 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
00279 *den = ((AVRational*)dst)->den;
00280 return 0;
00281 }
00282 error:
00283 *den=*intnum=0;
00284 return -1;
00285 }
00286
00287 double av_get_double(void *obj, const char *name, const AVOption **o_out)
00288 {
00289 int64_t intnum=1;
00290 double num=1;
00291 int den=1;
00292
00293 av_get_number(obj, name, o_out, &num, &den, &intnum);
00294 return num*intnum/den;
00295 }
00296
00297 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
00298 {
00299 int64_t intnum=1;
00300 double num=1;
00301 int den=1;
00302
00303 av_get_number(obj, name, o_out, &num, &den, &intnum);
00304 if (num == 1.0 && (int)intnum == intnum)
00305 return (AVRational){intnum, den};
00306 else
00307 return av_d2q(num*intnum/den, 1<<24);
00308 }
00309
00310 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
00311 {
00312 int64_t intnum=1;
00313 double num=1;
00314 int den=1;
00315
00316 av_get_number(obj, name, o_out, &num, &den, &intnum);
00317 return num*intnum/den;
00318 }
00319
00320 static void opt_list(void *obj, void *av_log_obj, const char *unit,
00321 int req_flags, int rej_flags)
00322 {
00323 const AVOption *opt=NULL;
00324
00325 while ((opt= av_next_option(obj, opt))) {
00326 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
00327 continue;
00328
00329
00330
00331
00332
00333 if (!unit && opt->type==FF_OPT_TYPE_CONST)
00334 continue;
00335 else if (unit && opt->type!=FF_OPT_TYPE_CONST)
00336 continue;
00337 else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
00338 continue;
00339 else if (unit && opt->type == FF_OPT_TYPE_CONST)
00340 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
00341 else
00342 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
00343
00344 switch (opt->type) {
00345 case FF_OPT_TYPE_FLAGS:
00346 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
00347 break;
00348 case FF_OPT_TYPE_INT:
00349 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
00350 break;
00351 case FF_OPT_TYPE_INT64:
00352 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
00353 break;
00354 case FF_OPT_TYPE_DOUBLE:
00355 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
00356 break;
00357 case FF_OPT_TYPE_FLOAT:
00358 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
00359 break;
00360 case FF_OPT_TYPE_STRING:
00361 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
00362 break;
00363 case FF_OPT_TYPE_RATIONAL:
00364 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
00365 break;
00366 case FF_OPT_TYPE_BINARY:
00367 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
00368 break;
00369 case FF_OPT_TYPE_CONST:
00370 default:
00371 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
00372 break;
00373 }
00374 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
00375 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
00376 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
00377 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
00378 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
00379
00380 if (opt->help)
00381 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
00382 av_log(av_log_obj, AV_LOG_INFO, "\n");
00383 if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
00384 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
00385 }
00386 }
00387 }
00388
00389 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
00390 {
00391 if (!obj)
00392 return -1;
00393
00394 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
00395
00396 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
00397
00398 return 0;
00399 }
00400
00407 void av_opt_set_defaults2(void *s, int mask, int flags)
00408 {
00409 const AVOption *opt = NULL;
00410 while ((opt = av_next_option(s, opt)) != NULL) {
00411 if ((opt->flags & mask) != flags)
00412 continue;
00413 switch (opt->type) {
00414 case FF_OPT_TYPE_CONST:
00415
00416 break;
00417 case FF_OPT_TYPE_FLAGS:
00418 case FF_OPT_TYPE_INT: {
00419 int val;
00420 val = opt->default_val;
00421 av_set_int(s, opt->name, val);
00422 }
00423 break;
00424 case FF_OPT_TYPE_INT64:
00425 if ((double)(opt->default_val+0.6) == opt->default_val)
00426 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
00427 av_set_int(s, opt->name, opt->default_val);
00428 break;
00429 case FF_OPT_TYPE_DOUBLE:
00430 case FF_OPT_TYPE_FLOAT: {
00431 double val;
00432 val = opt->default_val;
00433 av_set_double(s, opt->name, val);
00434 }
00435 break;
00436 case FF_OPT_TYPE_RATIONAL: {
00437 AVRational val;
00438 val = av_d2q(opt->default_val, INT_MAX);
00439 av_set_q(s, opt->name, val);
00440 }
00441 break;
00442 case FF_OPT_TYPE_STRING:
00443 case FF_OPT_TYPE_BINARY:
00444
00445 break;
00446 default:
00447 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
00448 }
00449 }
00450 }
00451
00452 void av_opt_set_defaults(void *s)
00453 {
00454 av_opt_set_defaults2(s, 0, 0);
00455 }
00456
00474 static int parse_key_value_pair(void *ctx, const char **buf,
00475 const char *key_val_sep, const char *pairs_sep)
00476 {
00477 char *key = av_get_token(buf, key_val_sep);
00478 char *val;
00479 int ret;
00480
00481 if (*key && strspn(*buf, key_val_sep)) {
00482 (*buf)++;
00483 val = av_get_token(buf, pairs_sep);
00484 } else {
00485 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
00486 av_free(key);
00487 return AVERROR(EINVAL);
00488 }
00489
00490 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
00491
00492 ret = av_set_string3(ctx, key, val, 1, NULL);
00493 if (ret == AVERROR(ENOENT))
00494 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
00495
00496 av_free(key);
00497 av_free(val);
00498 return ret;
00499 }
00500
00501 int av_set_options_string(void *ctx, const char *opts,
00502 const char *key_val_sep, const char *pairs_sep)
00503 {
00504 int ret, count = 0;
00505
00506 while (*opts) {
00507 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
00508 return ret;
00509 count++;
00510
00511 if (*opts)
00512 opts++;
00513 }
00514
00515 return count;
00516 }
00517
00518 #ifdef TEST
00519
00520 #undef printf
00521
00522 typedef struct TestContext
00523 {
00524 const AVClass *class;
00525 int num;
00526 int toggle;
00527 char *string;
00528 int flags;
00529 AVRational rational;
00530 } TestContext;
00531
00532 #define OFFSET(x) offsetof(TestContext, x)
00533
00534 #define TEST_FLAG_COOL 01
00535 #define TEST_FLAG_LAME 02
00536 #define TEST_FLAG_MU 04
00537
00538 static const AVOption test_options[]= {
00539 {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, 0, 0, 100 },
00540 {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, 0, 0, 1 },
00541 {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, 0, 0, 10 },
00542 {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
00543 {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, 0, 0, INT_MAX, 0, "flags" },
00544 {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_COOL, INT_MIN, INT_MAX, 0, "flags" },
00545 {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_LAME, INT_MIN, INT_MAX, 0, "flags" },
00546 {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_MU, INT_MIN, INT_MAX, 0, "flags" },
00547 {NULL},
00548 };
00549
00550 static const char *test_get_name(void *ctx)
00551 {
00552 return "test";
00553 }
00554
00555 static const AVClass test_class = {
00556 "TestContext",
00557 test_get_name,
00558 test_options
00559 };
00560
00561 int main(void)
00562 {
00563 int i;
00564
00565 printf("\nTesting av_set_options_string()\n");
00566 {
00567 TestContext test_ctx;
00568 const char *options[] = {
00569 "",
00570 ":",
00571 "=",
00572 "foo=:",
00573 ":=foo",
00574 "=foo",
00575 "foo=",
00576 "foo",
00577 "foo=val",
00578 "foo==val",
00579 "toggle=:",
00580 "string=:",
00581 "toggle=1 : foo",
00582 "toggle=100",
00583 "toggle==1",
00584 "flags=+mu-lame : num=42: toggle=0",
00585 "num=42 : string=blahblah",
00586 "rational=0 : rational=1/2 : rational=1/-1",
00587 "rational=-1/0",
00588 };
00589
00590 test_ctx.class = &test_class;
00591 av_opt_set_defaults2(&test_ctx, 0, 0);
00592 test_ctx.string = av_strdup("default");
00593
00594 av_log_set_level(AV_LOG_DEBUG);
00595
00596 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
00597 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
00598 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
00599 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
00600 printf("\n");
00601 }
00602 }
00603
00604 return 0;
00605 }
00606
00607 #endif