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 #include "dict.h"
00033 #include "log.h"
00034 #include "parseutils.h"
00035
00036 #if FF_API_FIND_OPT
00037
00038 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
00039 {
00040 const AVOption *o = NULL;
00041
00042 while ((o = av_next_option(v, o))) {
00043 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
00044 return o;
00045 }
00046 return NULL;
00047 }
00048 #endif
00049
00050 #if FF_API_OLD_AVOPTIONS
00051 const AVOption *av_next_option(void *obj, const AVOption *last)
00052 {
00053 return av_opt_next(obj, last);
00054 }
00055 #endif
00056
00057 const AVOption *av_opt_next(void *obj, const AVOption *last)
00058 {
00059 AVClass *class = *(AVClass**)obj;
00060 if (!last && class->option[0].name) return class->option;
00061 if (last && last[1].name) return ++last;
00062 return NULL;
00063 }
00064
00065 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
00066 {
00067 switch (o->type) {
00068 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
00069 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
00070 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
00071 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
00072 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
00073 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
00074 *den = ((AVRational*)dst)->den;
00075 return 0;
00076 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
00077 }
00078 return AVERROR(EINVAL);
00079 }
00080
00081 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
00082 {
00083 if (o->max*den < num*intnum || o->min*den > num*intnum) {
00084 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n", num*intnum/den, o->name);
00085 return AVERROR(ERANGE);
00086 }
00087
00088 switch (o->type) {
00089 case AV_OPT_TYPE_FLAGS:
00090 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
00091 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
00092 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
00093 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
00094 case AV_OPT_TYPE_RATIONAL:
00095 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
00096 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
00097 break;
00098 default:
00099 return AVERROR(EINVAL);
00100 }
00101 return 0;
00102 }
00103
00104 static const double const_values[] = {
00105 M_PI,
00106 M_E,
00107 FF_QP2LAMBDA,
00108 0
00109 };
00110
00111 static const char * const const_names[] = {
00112 "PI",
00113 "E",
00114 "QP2LAMBDA",
00115 0
00116 };
00117
00118 static int hexchar2int(char c) {
00119 if (c >= '0' && c <= '9') return c - '0';
00120 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
00121 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
00122 return -1;
00123 }
00124
00125 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
00126 {
00127 int *lendst = (int *)(dst + 1);
00128 uint8_t *bin, *ptr;
00129 int len = strlen(val);
00130
00131 av_freep(dst);
00132 *lendst = 0;
00133
00134 if (len & 1)
00135 return AVERROR(EINVAL);
00136 len /= 2;
00137
00138 ptr = bin = av_malloc(len);
00139 while (*val) {
00140 int a = hexchar2int(*val++);
00141 int b = hexchar2int(*val++);
00142 if (a < 0 || b < 0) {
00143 av_free(bin);
00144 return AVERROR(EINVAL);
00145 }
00146 *ptr++ = (a << 4) | b;
00147 }
00148 *dst = bin;
00149 *lendst = len;
00150
00151 return 0;
00152 }
00153
00154 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
00155 {
00156 av_freep(dst);
00157 *dst = av_strdup(val);
00158 return 0;
00159 }
00160
00161 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
00162 {
00163 int ret = 0, notfirst = 0;
00164 for (;;) {
00165 int i, den = 1;
00166 char buf[256];
00167 int cmd = 0;
00168 double d, num = 1;
00169 int64_t intnum = 1;
00170
00171 if (*val == '+' || *val == '-')
00172 cmd = *(val++);
00173
00174 for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
00175 buf[i] = val[i];
00176 buf[i] = 0;
00177
00178 {
00179 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
00180 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
00181 d = o_named->default_val.dbl;
00182 else if (!strcmp(buf, "default")) d = o->default_val.dbl;
00183 else if (!strcmp(buf, "max" )) d = o->max;
00184 else if (!strcmp(buf, "min" )) d = o->min;
00185 else if (!strcmp(buf, "none" )) d = 0;
00186 else if (!strcmp(buf, "all" )) d = ~0;
00187 else {
00188 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
00189 if (res < 0) {
00190 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
00191 return res;
00192 }
00193 }
00194 }
00195 if (o->type == AV_OPT_TYPE_FLAGS) {
00196 read_number(o, dst, NULL, NULL, &intnum);
00197 if (cmd == '+') d = intnum | (int64_t)d;
00198 else if (cmd == '-') d = intnum &~(int64_t)d;
00199 } else {
00200 read_number(o, dst, &num, &den, &intnum);
00201 if (cmd == '+') d = notfirst*num*intnum/den + d;
00202 else if (cmd == '-') d = notfirst*num*intnum/den - d;
00203 }
00204
00205 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
00206 return ret;
00207 val += i;
00208 if (!*val)
00209 return 0;
00210 notfirst = 1;
00211 }
00212
00213 return 0;
00214 }
00215
00216 #if FF_API_OLD_AVOPTIONS
00217 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
00218 {
00219 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
00220 if (o_out)
00221 *o_out = o;
00222 return av_opt_set(obj, name, val, 0);
00223 }
00224 #endif
00225
00226 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
00227 {
00228 int ret;
00229 void *dst, *target_obj;
00230 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
00231 if (!o || !target_obj)
00232 return AVERROR_OPTION_NOT_FOUND;
00233 if (!val && o->type != AV_OPT_TYPE_STRING)
00234 return AVERROR(EINVAL);
00235
00236 dst = ((uint8_t*)target_obj) + o->offset;
00237 switch (o->type) {
00238 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
00239 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
00240 case AV_OPT_TYPE_FLAGS:
00241 case AV_OPT_TYPE_INT:
00242 case AV_OPT_TYPE_INT64:
00243 case AV_OPT_TYPE_FLOAT:
00244 case AV_OPT_TYPE_DOUBLE:
00245 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
00246 case AV_OPT_TYPE_IMAGE_SIZE:
00247 ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
00248 if (ret < 0)
00249 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
00250 return ret;
00251 }
00252
00253 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
00254 return AVERROR(EINVAL);
00255 }
00256
00257 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
00258 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
00259 {\
00260 if (!o || o->type != opttype)\
00261 return AVERROR(EINVAL);\
00262 return set_string_number(obj, o, val, name ## _out);\
00263 }
00264
00265 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
00266 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
00267 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
00268 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
00269 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
00270 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
00271
00272 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
00273 int search_flags)
00274 {
00275 void *dst, *target_obj;
00276 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
00277
00278 if (!o || !target_obj)
00279 return AVERROR_OPTION_NOT_FOUND;
00280
00281 dst = ((uint8_t*)target_obj) + o->offset;
00282 return write_number(obj, o, dst, num, den, intnum);
00283 }
00284
00285 #if FF_API_OLD_AVOPTIONS
00286 const AVOption *av_set_double(void *obj, const char *name, double n)
00287 {
00288 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
00289 if (set_number(obj, name, n, 1, 1, 0) < 0)
00290 return NULL;
00291 return o;
00292 }
00293
00294 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
00295 {
00296 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
00297 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
00298 return NULL;
00299 return o;
00300 }
00301
00302 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
00303 {
00304 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
00305 if (set_number(obj, name, 1, 1, n, 0) < 0)
00306 return NULL;
00307 return o;
00308 }
00309 #endif
00310
00311 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
00312 {
00313 return set_number(obj, name, 1, 1, val, search_flags);
00314 }
00315
00316 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
00317 {
00318 return set_number(obj, name, val, 1, 1, search_flags);
00319 }
00320
00321 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
00322 {
00323 return set_number(obj, name, val.num, val.den, 1, search_flags);
00324 }
00325
00326 #if FF_API_OLD_AVOPTIONS
00327
00332 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
00333 {
00334 const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
00335 void *dst;
00336 uint8_t *bin;
00337 int len, i;
00338 if (!o)
00339 return NULL;
00340 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
00341 return NULL;
00342
00343 dst= ((uint8_t*)obj) + o->offset;
00344 if (o_out) *o_out= o;
00345
00346 switch (o->type) {
00347 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
00348 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
00349 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
00350 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
00351 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
00352 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
00353 case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
00354 case AV_OPT_TYPE_STRING: return *(void**)dst;
00355 case AV_OPT_TYPE_BINARY:
00356 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
00357 if (len >= (buf_len + 1)/2) return NULL;
00358 bin = *(uint8_t**)dst;
00359 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
00360 break;
00361 default: return NULL;
00362 }
00363 return buf;
00364 }
00365 #endif
00366
00367 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
00368 {
00369 void *dst, *target_obj;
00370 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
00371 uint8_t *bin, buf[128];
00372 int len, i, ret;
00373
00374 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
00375 return AVERROR_OPTION_NOT_FOUND;
00376
00377 dst = (uint8_t*)target_obj + o->offset;
00378
00379 buf[0] = 0;
00380 switch (o->type) {
00381 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
00382 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
00383 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
00384 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
00385 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
00386 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
00387 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
00388 case AV_OPT_TYPE_STRING:
00389 if (*(uint8_t**)dst)
00390 *out_val = av_strdup(*(uint8_t**)dst);
00391 else
00392 *out_val = av_strdup("");
00393 return 0;
00394 case AV_OPT_TYPE_BINARY:
00395 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
00396 if ((uint64_t)len*2 + 1 > INT_MAX)
00397 return AVERROR(EINVAL);
00398 if (!(*out_val = av_malloc(len*2 + 1)))
00399 return AVERROR(ENOMEM);
00400 bin = *(uint8_t**)dst;
00401 for (i = 0; i < len; i++)
00402 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
00403 return 0;
00404 case AV_OPT_TYPE_IMAGE_SIZE:
00405 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
00406 break;
00407 default:
00408 return AVERROR(EINVAL);
00409 }
00410
00411 if (ret >= sizeof(buf))
00412 return AVERROR(EINVAL);
00413 *out_val = av_strdup(buf);
00414 return 0;
00415 }
00416
00417 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
00418 int search_flags)
00419 {
00420 void *dst, *target_obj;
00421 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
00422 if (!o || !target_obj)
00423 goto error;
00424
00425 dst = ((uint8_t*)target_obj) + o->offset;
00426
00427 if (o_out) *o_out= o;
00428
00429 return read_number(o, dst, num, den, intnum);
00430
00431 error:
00432 *den=*intnum=0;
00433 return -1;
00434 }
00435
00436 #if FF_API_OLD_AVOPTIONS
00437 double av_get_double(void *obj, const char *name, const AVOption **o_out)
00438 {
00439 int64_t intnum=1;
00440 double num=1;
00441 int den=1;
00442
00443 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
00444 return NAN;
00445 return num*intnum/den;
00446 }
00447
00448 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
00449 {
00450 int64_t intnum=1;
00451 double num=1;
00452 int den=1;
00453
00454 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
00455 return (AVRational){0, 0};
00456 if (num == 1.0 && (int)intnum == intnum)
00457 return (AVRational){intnum, den};
00458 else
00459 return av_d2q(num*intnum/den, 1<<24);
00460 }
00461
00462 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
00463 {
00464 int64_t intnum=1;
00465 double num=1;
00466 int den=1;
00467
00468 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
00469 return -1;
00470 return num*intnum/den;
00471 }
00472 #endif
00473
00474 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
00475 {
00476 int64_t intnum = 1;
00477 double num = 1;
00478 int ret, den = 1;
00479
00480 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
00481 return ret;
00482 *out_val = num*intnum/den;
00483 return 0;
00484 }
00485
00486 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
00487 {
00488 int64_t intnum = 1;
00489 double num = 1;
00490 int ret, den = 1;
00491
00492 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
00493 return ret;
00494 *out_val = num*intnum/den;
00495 return 0;
00496 }
00497
00498 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
00499 {
00500 int64_t intnum = 1;
00501 double num = 1;
00502 int ret, den = 1;
00503
00504 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
00505 return ret;
00506
00507 if (num == 1.0 && (int)intnum == intnum)
00508 *out_val = (AVRational){intnum, den};
00509 else
00510 *out_val = av_d2q(num*intnum/den, 1<<24);
00511 return 0;
00512 }
00513
00514 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
00515 {
00516 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
00517 const AVOption *flag = av_opt_find(obj, flag_name,
00518 field ? field->unit : NULL, 0, 0);
00519 int64_t res;
00520
00521 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
00522 av_opt_get_int(obj, field_name, 0, &res) < 0)
00523 return 0;
00524 return res & (int) flag->default_val.dbl;
00525 }
00526
00527 static void opt_list(void *obj, void *av_log_obj, const char *unit,
00528 int req_flags, int rej_flags)
00529 {
00530 const AVOption *opt=NULL;
00531
00532 while ((opt = av_opt_next(obj, opt))) {
00533 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
00534 continue;
00535
00536
00537
00538
00539
00540 if (!unit && opt->type==AV_OPT_TYPE_CONST)
00541 continue;
00542 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
00543 continue;
00544 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
00545 continue;
00546 else if (unit && opt->type == AV_OPT_TYPE_CONST)
00547 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
00548 else
00549 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
00550
00551 switch (opt->type) {
00552 case AV_OPT_TYPE_FLAGS:
00553 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
00554 break;
00555 case AV_OPT_TYPE_INT:
00556 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
00557 break;
00558 case AV_OPT_TYPE_INT64:
00559 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
00560 break;
00561 case AV_OPT_TYPE_DOUBLE:
00562 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
00563 break;
00564 case AV_OPT_TYPE_FLOAT:
00565 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
00566 break;
00567 case AV_OPT_TYPE_STRING:
00568 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
00569 break;
00570 case AV_OPT_TYPE_RATIONAL:
00571 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
00572 break;
00573 case AV_OPT_TYPE_BINARY:
00574 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
00575 break;
00576 case AV_OPT_TYPE_IMAGE_SIZE:
00577 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<image_size>");
00578 break;
00579 case AV_OPT_TYPE_CONST:
00580 default:
00581 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
00582 break;
00583 }
00584 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
00585 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
00586 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
00587 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
00588 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
00589
00590 if (opt->help)
00591 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
00592 av_log(av_log_obj, AV_LOG_INFO, "\n");
00593 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
00594 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
00595 }
00596 }
00597 }
00598
00599 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
00600 {
00601 if (!obj)
00602 return -1;
00603
00604 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
00605
00606 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
00607
00608 return 0;
00609 }
00610
00611 void av_opt_set_defaults(void *s)
00612 {
00613 #if FF_API_OLD_AVOPTIONS
00614 av_opt_set_defaults2(s, 0, 0);
00615 }
00616
00617 void av_opt_set_defaults2(void *s, int mask, int flags)
00618 {
00619 #endif
00620 const AVOption *opt = NULL;
00621 while ((opt = av_opt_next(s, opt)) != NULL) {
00622 #if FF_API_OLD_AVOPTIONS
00623 if ((opt->flags & mask) != flags)
00624 continue;
00625 #endif
00626 switch (opt->type) {
00627 case AV_OPT_TYPE_CONST:
00628
00629 break;
00630 case AV_OPT_TYPE_FLAGS:
00631 case AV_OPT_TYPE_INT: {
00632 int val;
00633 val = opt->default_val.dbl;
00634 av_opt_set_int(s, opt->name, val, 0);
00635 }
00636 break;
00637 case AV_OPT_TYPE_INT64:
00638 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
00639 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
00640 av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
00641 break;
00642 case AV_OPT_TYPE_DOUBLE:
00643 case AV_OPT_TYPE_FLOAT: {
00644 double val;
00645 val = opt->default_val.dbl;
00646 av_opt_set_double(s, opt->name, val, 0);
00647 }
00648 break;
00649 case AV_OPT_TYPE_RATIONAL: {
00650 AVRational val;
00651 val = av_d2q(opt->default_val.dbl, INT_MAX);
00652 av_opt_set_q(s, opt->name, val, 0);
00653 }
00654 break;
00655 case AV_OPT_TYPE_STRING:
00656 case AV_OPT_TYPE_IMAGE_SIZE:
00657 av_opt_set(s, opt->name, opt->default_val.str, 0);
00658 break;
00659 case AV_OPT_TYPE_BINARY:
00660
00661 break;
00662 default:
00663 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
00664 }
00665 }
00666 }
00667
00685 static int parse_key_value_pair(void *ctx, const char **buf,
00686 const char *key_val_sep, const char *pairs_sep)
00687 {
00688 char *key = av_get_token(buf, key_val_sep);
00689 char *val;
00690 int ret;
00691
00692 if (*key && strspn(*buf, key_val_sep)) {
00693 (*buf)++;
00694 val = av_get_token(buf, pairs_sep);
00695 } else {
00696 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
00697 av_free(key);
00698 return AVERROR(EINVAL);
00699 }
00700
00701 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
00702
00703 ret = av_opt_set(ctx, key, val, 0);
00704 if (ret == AVERROR_OPTION_NOT_FOUND)
00705 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
00706
00707 av_free(key);
00708 av_free(val);
00709 return ret;
00710 }
00711
00712 int av_set_options_string(void *ctx, const char *opts,
00713 const char *key_val_sep, const char *pairs_sep)
00714 {
00715 int ret, count = 0;
00716
00717 if (!opts)
00718 return 0;
00719
00720 while (*opts) {
00721 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
00722 return ret;
00723 count++;
00724
00725 if (*opts)
00726 opts++;
00727 }
00728
00729 return count;
00730 }
00731
00732 void av_opt_free(void *obj)
00733 {
00734 const AVOption *o = NULL;
00735 while ((o = av_opt_next(obj, o)))
00736 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
00737 av_freep((uint8_t *)obj + o->offset);
00738 }
00739
00740 int av_opt_set_dict(void *obj, AVDictionary **options)
00741 {
00742 AVDictionaryEntry *t = NULL;
00743 AVDictionary *tmp = NULL;
00744 int ret = 0;
00745
00746 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
00747 ret = av_opt_set(obj, t->key, t->value, 0);
00748 if (ret == AVERROR_OPTION_NOT_FOUND)
00749 av_dict_set(&tmp, t->key, t->value, 0);
00750 else if (ret < 0) {
00751 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
00752 break;
00753 }
00754 ret = 0;
00755 }
00756 av_dict_free(options);
00757 *options = tmp;
00758 return ret;
00759 }
00760
00761 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
00762 int opt_flags, int search_flags)
00763 {
00764 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
00765 }
00766
00767 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
00768 int opt_flags, int search_flags, void **target_obj)
00769 {
00770 const AVClass *c;
00771 const AVOption *o = NULL;
00772
00773 if(!obj)
00774 return NULL;
00775
00776 c= *(AVClass**)obj;
00777
00778 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
00779 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
00780 const AVClass *child = NULL;
00781 while (child = av_opt_child_class_next(c, child))
00782 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
00783 return o;
00784 } else {
00785 void *child = NULL;
00786 while (child = av_opt_child_next(obj, child))
00787 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
00788 return o;
00789 }
00790 }
00791
00792 while (o = av_opt_next(obj, o)) {
00793 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
00794 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
00795 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
00796 if (target_obj) {
00797 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
00798 *target_obj = obj;
00799 else
00800 *target_obj = NULL;
00801 }
00802 return o;
00803 }
00804 }
00805 return NULL;
00806 }
00807
00808 void *av_opt_child_next(void *obj, void *prev)
00809 {
00810 const AVClass *c = *(AVClass**)obj;
00811 if (c->child_next)
00812 return c->child_next(obj, prev);
00813 return NULL;
00814 }
00815
00816 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
00817 {
00818 if (parent->child_class_next)
00819 return parent->child_class_next(prev);
00820 return NULL;
00821 }
00822
00823 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
00824 {
00825 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
00826 if(!opt)
00827 return NULL;
00828 return (uint8_t*)obj + opt->offset;
00829 }
00830
00831 #ifdef TEST
00832
00833 #undef printf
00834
00835 typedef struct TestContext
00836 {
00837 const AVClass *class;
00838 int num;
00839 int toggle;
00840 char *string;
00841 int flags;
00842 AVRational rational;
00843 } TestContext;
00844
00845 #define OFFSET(x) offsetof(TestContext, x)
00846
00847 #define TEST_FLAG_COOL 01
00848 #define TEST_FLAG_LAME 02
00849 #define TEST_FLAG_MU 04
00850
00851 static const AVOption test_options[]= {
00852 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
00853 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
00854 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
00855 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
00856 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
00857 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
00858 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
00859 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
00860 {NULL},
00861 };
00862
00863 static const char *test_get_name(void *ctx)
00864 {
00865 return "test";
00866 }
00867
00868 static const AVClass test_class = {
00869 "TestContext",
00870 test_get_name,
00871 test_options
00872 };
00873
00874 int main(void)
00875 {
00876 int i;
00877
00878 printf("\nTesting av_set_options_string()\n");
00879 {
00880 TestContext test_ctx;
00881 const char *options[] = {
00882 "",
00883 ":",
00884 "=",
00885 "foo=:",
00886 ":=foo",
00887 "=foo",
00888 "foo=",
00889 "foo",
00890 "foo=val",
00891 "foo==val",
00892 "toggle=:",
00893 "string=:",
00894 "toggle=1 : foo",
00895 "toggle=100",
00896 "toggle==1",
00897 "flags=+mu-lame : num=42: toggle=0",
00898 "num=42 : string=blahblah",
00899 "rational=0 : rational=1/2 : rational=1/-1",
00900 "rational=-1/0",
00901 };
00902
00903 test_ctx.class = &test_class;
00904 av_opt_set_defaults(&test_ctx);
00905 test_ctx.string = av_strdup("default");
00906
00907 av_log_set_level(AV_LOG_DEBUG);
00908
00909 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
00910 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
00911 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
00912 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
00913 printf("\n");
00914 }
00915 }
00916
00917 return 0;
00918 }
00919
00920 #endif