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.

776 lines
24KB

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