You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

781 lines
25KB

  1. /*
  2. * AVOptions
  3. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  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
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * AVOptions
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "avutil.h"
  27. #include "avstring.h"
  28. #include "common.h"
  29. #include "opt.h"
  30. #include "eval.h"
  31. #include "dict.h"
  32. #include "log.h"
  33. #include "mathematics.h"
  34. const AVOption *av_opt_next(void *obj, const AVOption *last)
  35. {
  36. AVClass *class = *(AVClass**)obj;
  37. if (!last && class->option && class->option[0].name)
  38. return class->option;
  39. if (last && last[1].name)
  40. return ++last;
  41. return NULL;
  42. }
  43. static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
  44. {
  45. switch (o->type) {
  46. case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
  47. case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
  48. case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
  49. case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
  50. case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
  51. case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
  52. *den = ((AVRational*)dst)->den;
  53. return 0;
  54. }
  55. return AVERROR(EINVAL);
  56. }
  57. static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  58. {
  59. if (o->max*den < num*intnum || o->min*den > num*intnum) {
  60. av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
  61. num*intnum/den, o->name);
  62. return AVERROR(ERANGE);
  63. }
  64. switch (o->type) {
  65. case AV_OPT_TYPE_FLAGS:
  66. case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  67. case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  68. case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  69. case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  70. case AV_OPT_TYPE_RATIONAL:
  71. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  72. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  73. break;
  74. default:
  75. return AVERROR(EINVAL);
  76. }
  77. return 0;
  78. }
  79. static const double const_values[] = {
  80. M_PI,
  81. M_E,
  82. FF_QP2LAMBDA,
  83. 0
  84. };
  85. static const char * const const_names[] = {
  86. "PI",
  87. "E",
  88. "QP2LAMBDA",
  89. 0
  90. };
  91. static int hexchar2int(char c) {
  92. if (c >= '0' && c <= '9') return c - '0';
  93. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  94. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  95. return -1;
  96. }
  97. static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  98. {
  99. int *lendst = (int *)(dst + 1);
  100. uint8_t *bin, *ptr;
  101. int len = strlen(val);
  102. av_freep(dst);
  103. *lendst = 0;
  104. if (len & 1)
  105. return AVERROR(EINVAL);
  106. len /= 2;
  107. ptr = bin = av_malloc(len);
  108. while (*val) {
  109. int a = hexchar2int(*val++);
  110. int b = hexchar2int(*val++);
  111. if (a < 0 || b < 0) {
  112. av_free(bin);
  113. return AVERROR(EINVAL);
  114. }
  115. *ptr++ = (a << 4) | b;
  116. }
  117. *dst = bin;
  118. *lendst = len;
  119. return 0;
  120. }
  121. static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  122. {
  123. av_freep(dst);
  124. *dst = av_strdup(val);
  125. return 0;
  126. }
  127. #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
  128. opt->type == AV_OPT_TYPE_CONST || \
  129. opt->type == AV_OPT_TYPE_FLAGS || \
  130. opt->type == AV_OPT_TYPE_INT) ? \
  131. opt->default_val.i64 : opt->default_val.dbl)
  132. static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
  133. {
  134. int ret = 0, notfirst = 0;
  135. for (;;) {
  136. int i, den = 1;
  137. char buf[256];
  138. int cmd = 0;
  139. double d, num = 1;
  140. int64_t intnum = 1;
  141. i = 0;
  142. if (*val == '+' || *val == '-') {
  143. if (o->type == AV_OPT_TYPE_FLAGS)
  144. cmd = *(val++);
  145. else if (!notfirst)
  146. buf[i++] = *val;
  147. }
  148. for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  149. buf[i] = val[i];
  150. buf[i] = 0;
  151. {
  152. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  153. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  154. d = DEFAULT_NUMVAL(o_named);
  155. else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
  156. else if (!strcmp(buf, "max" )) d = o->max;
  157. else if (!strcmp(buf, "min" )) d = o->min;
  158. else if (!strcmp(buf, "none" )) d = 0;
  159. else if (!strcmp(buf, "all" )) d = ~0;
  160. else {
  161. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  162. if (res < 0) {
  163. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  164. return res;
  165. }
  166. }
  167. }
  168. if (o->type == AV_OPT_TYPE_FLAGS) {
  169. read_number(o, dst, NULL, NULL, &intnum);
  170. if (cmd == '+') d = intnum | (int64_t)d;
  171. else if (cmd == '-') d = intnum &~(int64_t)d;
  172. } else {
  173. read_number(o, dst, &num, &den, &intnum);
  174. if (cmd == '+') d = notfirst*num*intnum/den + d;
  175. else if (cmd == '-') d = notfirst*num*intnum/den - d;
  176. }
  177. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  178. return ret;
  179. val += i;
  180. if (!*val)
  181. return 0;
  182. notfirst = 1;
  183. }
  184. return 0;
  185. }
  186. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  187. {
  188. void *dst, *target_obj;
  189. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  190. if (!o || !target_obj)
  191. return AVERROR_OPTION_NOT_FOUND;
  192. if (!val)
  193. return AVERROR(EINVAL);
  194. dst = ((uint8_t*)target_obj) + o->offset;
  195. switch (o->type) {
  196. case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  197. case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  198. case AV_OPT_TYPE_FLAGS:
  199. case AV_OPT_TYPE_INT:
  200. case AV_OPT_TYPE_INT64:
  201. case AV_OPT_TYPE_FLOAT:
  202. case AV_OPT_TYPE_DOUBLE:
  203. case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  204. }
  205. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  206. return AVERROR(EINVAL);
  207. }
  208. #define OPT_EVAL_NUMBER(name, opttype, vartype)\
  209. int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
  210. {\
  211. if (!o || o->type != opttype)\
  212. return AVERROR(EINVAL);\
  213. return set_string_number(obj, o, val, name ## _out);\
  214. }
  215. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  216. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  217. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  218. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  219. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  220. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  221. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  222. int search_flags)
  223. {
  224. void *dst, *target_obj;
  225. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  226. if (!o || !target_obj)
  227. return AVERROR_OPTION_NOT_FOUND;
  228. dst = ((uint8_t*)target_obj) + o->offset;
  229. return write_number(obj, o, dst, num, den, intnum);
  230. }
  231. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  232. {
  233. return set_number(obj, name, 1, 1, val, search_flags);
  234. }
  235. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  236. {
  237. return set_number(obj, name, val, 1, 1, search_flags);
  238. }
  239. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  240. {
  241. return set_number(obj, name, val.num, val.den, 1, search_flags);
  242. }
  243. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  244. {
  245. void *target_obj;
  246. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  247. uint8_t *ptr;
  248. uint8_t **dst;
  249. int *lendst;
  250. if (!o || !target_obj)
  251. return AVERROR_OPTION_NOT_FOUND;
  252. if (o->type != AV_OPT_TYPE_BINARY)
  253. return AVERROR(EINVAL);
  254. ptr = av_malloc(len);
  255. if (!ptr)
  256. return AVERROR(ENOMEM);
  257. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  258. lendst = (int *)(dst + 1);
  259. av_free(*dst);
  260. *dst = ptr;
  261. *lendst = len;
  262. memcpy(ptr, val, len);
  263. return 0;
  264. }
  265. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  266. {
  267. void *dst, *target_obj;
  268. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  269. uint8_t *bin, buf[128];
  270. int len, i, ret;
  271. if (!o || !target_obj)
  272. return AVERROR_OPTION_NOT_FOUND;
  273. dst = (uint8_t*)target_obj + o->offset;
  274. buf[0] = 0;
  275. switch (o->type) {
  276. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  277. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  278. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  279. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  280. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  281. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  282. case AV_OPT_TYPE_STRING:
  283. if (*(uint8_t**)dst)
  284. *out_val = av_strdup(*(uint8_t**)dst);
  285. else
  286. *out_val = av_strdup("");
  287. return 0;
  288. case AV_OPT_TYPE_BINARY:
  289. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  290. if ((uint64_t)len*2 + 1 > INT_MAX)
  291. return AVERROR(EINVAL);
  292. if (!(*out_val = av_malloc(len*2 + 1)))
  293. return AVERROR(ENOMEM);
  294. bin = *(uint8_t**)dst;
  295. for (i = 0; i < len; i++)
  296. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  297. return 0;
  298. default:
  299. return AVERROR(EINVAL);
  300. }
  301. if (ret >= sizeof(buf))
  302. return AVERROR(EINVAL);
  303. *out_val = av_strdup(buf);
  304. return 0;
  305. }
  306. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  307. int search_flags)
  308. {
  309. void *dst, *target_obj;
  310. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  311. if (!o || !target_obj)
  312. goto error;
  313. dst = ((uint8_t*)target_obj) + o->offset;
  314. if (o_out) *o_out= o;
  315. return read_number(o, dst, num, den, intnum);
  316. error:
  317. *den=*intnum=0;
  318. return -1;
  319. }
  320. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  321. {
  322. int64_t intnum = 1;
  323. double num = 1;
  324. int ret, den = 1;
  325. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  326. return ret;
  327. *out_val = num*intnum/den;
  328. return 0;
  329. }
  330. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  331. {
  332. int64_t intnum = 1;
  333. double num = 1;
  334. int ret, den = 1;
  335. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  336. return ret;
  337. *out_val = num*intnum/den;
  338. return 0;
  339. }
  340. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  341. {
  342. int64_t intnum = 1;
  343. double num = 1;
  344. int ret, den = 1;
  345. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  346. return ret;
  347. if (num == 1.0 && (int)intnum == intnum)
  348. *out_val = (AVRational){intnum, den};
  349. else
  350. *out_val = av_d2q(num*intnum/den, 1<<24);
  351. return 0;
  352. }
  353. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  354. {
  355. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  356. const AVOption *flag = av_opt_find(obj, flag_name,
  357. field ? field->unit : NULL, 0, 0);
  358. int64_t res;
  359. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  360. av_opt_get_int(obj, field_name, 0, &res) < 0)
  361. return 0;
  362. return res & flag->default_val.i64;
  363. }
  364. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  365. int req_flags, int rej_flags)
  366. {
  367. const AVOption *opt=NULL;
  368. while ((opt = av_opt_next(obj, opt))) {
  369. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  370. continue;
  371. /* Don't print CONST's on level one.
  372. * Don't print anything but CONST's on level two.
  373. * Only print items from the requested unit.
  374. */
  375. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  376. continue;
  377. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  378. continue;
  379. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  380. continue;
  381. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  382. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  383. else
  384. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  385. switch (opt->type) {
  386. case AV_OPT_TYPE_FLAGS:
  387. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  388. break;
  389. case AV_OPT_TYPE_INT:
  390. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  391. break;
  392. case AV_OPT_TYPE_INT64:
  393. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  394. break;
  395. case AV_OPT_TYPE_DOUBLE:
  396. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  397. break;
  398. case AV_OPT_TYPE_FLOAT:
  399. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  400. break;
  401. case AV_OPT_TYPE_STRING:
  402. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  403. break;
  404. case AV_OPT_TYPE_RATIONAL:
  405. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  406. break;
  407. case AV_OPT_TYPE_BINARY:
  408. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  409. break;
  410. case AV_OPT_TYPE_CONST:
  411. default:
  412. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  413. break;
  414. }
  415. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  416. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  417. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  418. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  419. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  420. if (opt->help)
  421. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  422. av_log(av_log_obj, AV_LOG_INFO, "\n");
  423. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  424. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  425. }
  426. }
  427. }
  428. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  429. {
  430. if (!obj)
  431. return -1;
  432. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  433. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  434. return 0;
  435. }
  436. void av_opt_set_defaults(void *s)
  437. {
  438. const AVOption *opt = NULL;
  439. while ((opt = av_opt_next(s, opt)) != NULL) {
  440. switch (opt->type) {
  441. case AV_OPT_TYPE_CONST:
  442. /* Nothing to be done here */
  443. break;
  444. case AV_OPT_TYPE_FLAGS:
  445. case AV_OPT_TYPE_INT:
  446. case AV_OPT_TYPE_INT64:
  447. av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
  448. break;
  449. case AV_OPT_TYPE_DOUBLE:
  450. case AV_OPT_TYPE_FLOAT: {
  451. double val;
  452. val = opt->default_val.dbl;
  453. av_opt_set_double(s, opt->name, val, 0);
  454. }
  455. break;
  456. case AV_OPT_TYPE_RATIONAL: {
  457. AVRational val;
  458. val = av_d2q(opt->default_val.dbl, INT_MAX);
  459. av_opt_set_q(s, opt->name, val, 0);
  460. }
  461. break;
  462. case AV_OPT_TYPE_STRING:
  463. av_opt_set(s, opt->name, opt->default_val.str, 0);
  464. break;
  465. case AV_OPT_TYPE_BINARY:
  466. /* Cannot set default for binary */
  467. break;
  468. default:
  469. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  470. }
  471. }
  472. }
  473. /**
  474. * Store the value in the field in ctx that is named like key.
  475. * ctx must be an AVClass context, storing is done using AVOptions.
  476. *
  477. * @param buf the string to parse, buf will be updated to point at the
  478. * separator just after the parsed key/value pair
  479. * @param key_val_sep a 0-terminated list of characters used to
  480. * separate key from value
  481. * @param pairs_sep a 0-terminated list of characters used to separate
  482. * two pairs from each other
  483. * @return 0 if the key/value pair has been successfully parsed and
  484. * set, or a negative value corresponding to an AVERROR code in case
  485. * of error:
  486. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  487. * the error code issued by av_opt_set() if the key/value pair
  488. * cannot be set
  489. */
  490. static int parse_key_value_pair(void *ctx, const char **buf,
  491. const char *key_val_sep, const char *pairs_sep)
  492. {
  493. char *key = av_get_token(buf, key_val_sep);
  494. char *val;
  495. int ret;
  496. if (*key && strspn(*buf, key_val_sep)) {
  497. (*buf)++;
  498. val = av_get_token(buf, pairs_sep);
  499. } else {
  500. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  501. av_free(key);
  502. return AVERROR(EINVAL);
  503. }
  504. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  505. ret = av_opt_set(ctx, key, val, 0);
  506. if (ret == AVERROR_OPTION_NOT_FOUND)
  507. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  508. av_free(key);
  509. av_free(val);
  510. return ret;
  511. }
  512. int av_set_options_string(void *ctx, const char *opts,
  513. const char *key_val_sep, const char *pairs_sep)
  514. {
  515. int ret, count = 0;
  516. if (!opts)
  517. return 0;
  518. while (*opts) {
  519. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  520. return ret;
  521. count++;
  522. if (*opts)
  523. opts++;
  524. }
  525. return count;
  526. }
  527. void av_opt_free(void *obj)
  528. {
  529. const AVOption *o = NULL;
  530. while ((o = av_opt_next(obj, o)))
  531. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  532. av_freep((uint8_t *)obj + o->offset);
  533. }
  534. int av_opt_set_dict(void *obj, AVDictionary **options)
  535. {
  536. AVDictionaryEntry *t = NULL;
  537. AVDictionary *tmp = NULL;
  538. int ret = 0;
  539. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  540. ret = av_opt_set(obj, t->key, t->value, 0);
  541. if (ret == AVERROR_OPTION_NOT_FOUND)
  542. av_dict_set(&tmp, t->key, t->value, 0);
  543. else if (ret < 0) {
  544. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  545. break;
  546. }
  547. ret = 0;
  548. }
  549. av_dict_free(options);
  550. *options = tmp;
  551. return ret;
  552. }
  553. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  554. int opt_flags, int search_flags)
  555. {
  556. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  557. }
  558. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  559. int opt_flags, int search_flags, void **target_obj)
  560. {
  561. const AVClass *c = *(AVClass**)obj;
  562. const AVOption *o = NULL;
  563. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  564. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  565. const AVClass *child = NULL;
  566. while (child = av_opt_child_class_next(c, child))
  567. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  568. return o;
  569. } else {
  570. void *child = NULL;
  571. while (child = av_opt_child_next(obj, child))
  572. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  573. return o;
  574. }
  575. }
  576. while (o = av_opt_next(obj, o)) {
  577. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  578. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  579. (unit && o->unit && !strcmp(o->unit, unit)))) {
  580. if (target_obj) {
  581. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  582. *target_obj = obj;
  583. else
  584. *target_obj = NULL;
  585. }
  586. return o;
  587. }
  588. }
  589. return NULL;
  590. }
  591. void *av_opt_child_next(void *obj, void *prev)
  592. {
  593. const AVClass *c = *(AVClass**)obj;
  594. if (c->child_next)
  595. return c->child_next(obj, prev);
  596. return NULL;
  597. }
  598. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  599. {
  600. if (parent->child_class_next)
  601. return parent->child_class_next(prev);
  602. return NULL;
  603. }
  604. #ifdef TEST
  605. typedef struct TestContext
  606. {
  607. const AVClass *class;
  608. int num;
  609. int toggle;
  610. char *string;
  611. int flags;
  612. AVRational rational;
  613. } TestContext;
  614. #define OFFSET(x) offsetof(TestContext, x)
  615. #define TEST_FLAG_COOL 01
  616. #define TEST_FLAG_LAME 02
  617. #define TEST_FLAG_MU 04
  618. static const AVOption test_options[]= {
  619. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  620. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  621. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  622. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  623. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  624. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  625. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  626. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  627. {NULL},
  628. };
  629. static const char *test_get_name(void *ctx)
  630. {
  631. return "test";
  632. }
  633. static const AVClass test_class = {
  634. "TestContext",
  635. test_get_name,
  636. test_options
  637. };
  638. int main(void)
  639. {
  640. int i;
  641. printf("\nTesting av_set_options_string()\n");
  642. {
  643. TestContext test_ctx;
  644. const char *options[] = {
  645. "",
  646. ":",
  647. "=",
  648. "foo=:",
  649. ":=foo",
  650. "=foo",
  651. "foo=",
  652. "foo",
  653. "foo=val",
  654. "foo==val",
  655. "toggle=:",
  656. "string=:",
  657. "toggle=1 : foo",
  658. "toggle=100",
  659. "toggle==1",
  660. "flags=+mu-lame : num=42: toggle=0",
  661. "num=42 : string=blahblah",
  662. "rational=0 : rational=1/2 : rational=1/-1",
  663. "rational=-1/0",
  664. };
  665. test_ctx.class = &test_class;
  666. av_opt_set_defaults(&test_ctx);
  667. test_ctx.string = av_strdup("default");
  668. av_log_set_level(AV_LOG_DEBUG);
  669. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  670. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  671. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  672. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  673. printf("\n");
  674. }
  675. }
  676. return 0;
  677. }
  678. #endif