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.

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