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.

789 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, void *target_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(target_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, target_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, 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, 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. return read_number(o, dst, num, den, intnum);
  315. error:
  316. *den=*intnum=0;
  317. return -1;
  318. }
  319. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  320. {
  321. int64_t intnum = 1;
  322. double num = 1;
  323. int ret, den = 1;
  324. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  325. return ret;
  326. *out_val = num*intnum/den;
  327. return 0;
  328. }
  329. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  330. {
  331. int64_t intnum = 1;
  332. double num = 1;
  333. int ret, den = 1;
  334. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  335. return ret;
  336. *out_val = num*intnum/den;
  337. return 0;
  338. }
  339. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  340. {
  341. int64_t intnum = 1;
  342. double num = 1;
  343. int ret, den = 1;
  344. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  345. return ret;
  346. if (num == 1.0 && (int)intnum == intnum)
  347. *out_val = (AVRational){intnum, den};
  348. else
  349. *out_val = av_d2q(num*intnum/den, 1<<24);
  350. return 0;
  351. }
  352. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  353. {
  354. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  355. const AVOption *flag = av_opt_find(obj, flag_name,
  356. field ? field->unit : NULL, 0, 0);
  357. int64_t res;
  358. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  359. av_opt_get_int(obj, field_name, 0, &res) < 0)
  360. return 0;
  361. return res & flag->default_val.i64;
  362. }
  363. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  364. int req_flags, int rej_flags)
  365. {
  366. const AVOption *opt=NULL;
  367. while ((opt = av_opt_next(obj, opt))) {
  368. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  369. continue;
  370. /* Don't print CONST's on level one.
  371. * Don't print anything but CONST's on level two.
  372. * Only print items from the requested unit.
  373. */
  374. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  375. continue;
  376. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  377. continue;
  378. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  379. continue;
  380. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  381. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  382. else
  383. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  384. switch (opt->type) {
  385. case AV_OPT_TYPE_FLAGS:
  386. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  387. break;
  388. case AV_OPT_TYPE_INT:
  389. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  390. break;
  391. case AV_OPT_TYPE_INT64:
  392. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  393. break;
  394. case AV_OPT_TYPE_DOUBLE:
  395. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  396. break;
  397. case AV_OPT_TYPE_FLOAT:
  398. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  399. break;
  400. case AV_OPT_TYPE_STRING:
  401. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  402. break;
  403. case AV_OPT_TYPE_RATIONAL:
  404. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  405. break;
  406. case AV_OPT_TYPE_BINARY:
  407. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  408. break;
  409. case AV_OPT_TYPE_CONST:
  410. default:
  411. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  412. break;
  413. }
  414. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  415. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  416. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  417. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  418. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  419. if (opt->help)
  420. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  421. av_log(av_log_obj, AV_LOG_INFO, "\n");
  422. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  423. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  424. }
  425. }
  426. }
  427. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  428. {
  429. if (!obj)
  430. return -1;
  431. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  432. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  433. return 0;
  434. }
  435. void av_opt_set_defaults(void *s)
  436. {
  437. const AVOption *opt = NULL;
  438. while ((opt = av_opt_next(s, opt)) != NULL) {
  439. switch (opt->type) {
  440. case AV_OPT_TYPE_CONST:
  441. /* Nothing to be done here */
  442. break;
  443. case AV_OPT_TYPE_FLAGS:
  444. case AV_OPT_TYPE_INT:
  445. case AV_OPT_TYPE_INT64:
  446. av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
  447. break;
  448. case AV_OPT_TYPE_DOUBLE:
  449. case AV_OPT_TYPE_FLOAT: {
  450. double val;
  451. val = opt->default_val.dbl;
  452. av_opt_set_double(s, opt->name, val, 0);
  453. }
  454. break;
  455. case AV_OPT_TYPE_RATIONAL: {
  456. AVRational val;
  457. val = av_d2q(opt->default_val.dbl, INT_MAX);
  458. av_opt_set_q(s, opt->name, val, 0);
  459. }
  460. break;
  461. case AV_OPT_TYPE_STRING:
  462. av_opt_set(s, opt->name, opt->default_val.str, 0);
  463. break;
  464. case AV_OPT_TYPE_BINARY:
  465. /* Cannot set default for binary */
  466. break;
  467. default:
  468. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  469. }
  470. }
  471. }
  472. /**
  473. * Store the value in the field in ctx that is named like key.
  474. * ctx must be an AVClass context, storing is done using AVOptions.
  475. *
  476. * @param buf the string to parse, buf will be updated to point at the
  477. * separator just after the parsed key/value pair
  478. * @param key_val_sep a 0-terminated list of characters used to
  479. * separate key from value
  480. * @param pairs_sep a 0-terminated list of characters used to separate
  481. * two pairs from each other
  482. * @return 0 if the key/value pair has been successfully parsed and
  483. * set, or a negative value corresponding to an AVERROR code in case
  484. * of error:
  485. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  486. * the error code issued by av_opt_set() if the key/value pair
  487. * cannot be set
  488. */
  489. static int parse_key_value_pair(void *ctx, const char **buf,
  490. const char *key_val_sep, const char *pairs_sep)
  491. {
  492. char *key = av_get_token(buf, key_val_sep);
  493. char *val;
  494. int ret;
  495. if (!key)
  496. return AVERROR(ENOMEM);
  497. if (*key && strspn(*buf, key_val_sep)) {
  498. (*buf)++;
  499. val = av_get_token(buf, pairs_sep);
  500. if (!val) {
  501. av_freep(&key);
  502. return AVERROR(ENOMEM);
  503. }
  504. } else {
  505. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  506. av_free(key);
  507. return AVERROR(EINVAL);
  508. }
  509. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  510. ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
  511. if (ret == AVERROR_OPTION_NOT_FOUND)
  512. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  513. av_free(key);
  514. av_free(val);
  515. return ret;
  516. }
  517. int av_set_options_string(void *ctx, const char *opts,
  518. const char *key_val_sep, const char *pairs_sep)
  519. {
  520. int ret, count = 0;
  521. if (!opts)
  522. return 0;
  523. while (*opts) {
  524. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  525. return ret;
  526. count++;
  527. if (*opts)
  528. opts++;
  529. }
  530. return count;
  531. }
  532. void av_opt_free(void *obj)
  533. {
  534. const AVOption *o = NULL;
  535. while ((o = av_opt_next(obj, o)))
  536. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  537. av_freep((uint8_t *)obj + o->offset);
  538. }
  539. int av_opt_set_dict(void *obj, AVDictionary **options)
  540. {
  541. AVDictionaryEntry *t = NULL;
  542. AVDictionary *tmp = NULL;
  543. int ret = 0;
  544. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  545. ret = av_opt_set(obj, t->key, t->value, 0);
  546. if (ret == AVERROR_OPTION_NOT_FOUND)
  547. av_dict_set(&tmp, t->key, t->value, 0);
  548. else if (ret < 0) {
  549. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  550. break;
  551. }
  552. ret = 0;
  553. }
  554. av_dict_free(options);
  555. *options = tmp;
  556. return ret;
  557. }
  558. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  559. int opt_flags, int search_flags)
  560. {
  561. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  562. }
  563. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  564. int opt_flags, int search_flags, void **target_obj)
  565. {
  566. const AVClass *c = *(AVClass**)obj;
  567. const AVOption *o = NULL;
  568. if (!c)
  569. return NULL;
  570. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  571. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  572. const AVClass *child = NULL;
  573. while (child = av_opt_child_class_next(c, child))
  574. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  575. return o;
  576. } else {
  577. void *child = NULL;
  578. while (child = av_opt_child_next(obj, child))
  579. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  580. return o;
  581. }
  582. }
  583. while (o = av_opt_next(obj, o)) {
  584. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  585. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  586. (unit && o->unit && !strcmp(o->unit, unit)))) {
  587. if (target_obj) {
  588. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  589. *target_obj = obj;
  590. else
  591. *target_obj = NULL;
  592. }
  593. return o;
  594. }
  595. }
  596. return NULL;
  597. }
  598. void *av_opt_child_next(void *obj, void *prev)
  599. {
  600. const AVClass *c = *(AVClass**)obj;
  601. if (c->child_next)
  602. return c->child_next(obj, prev);
  603. return NULL;
  604. }
  605. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  606. {
  607. if (parent->child_class_next)
  608. return parent->child_class_next(prev);
  609. return NULL;
  610. }
  611. #ifdef TEST
  612. typedef struct TestContext
  613. {
  614. const AVClass *class;
  615. int num;
  616. int toggle;
  617. char *string;
  618. int flags;
  619. AVRational rational;
  620. } TestContext;
  621. #define OFFSET(x) offsetof(TestContext, x)
  622. #define TEST_FLAG_COOL 01
  623. #define TEST_FLAG_LAME 02
  624. #define TEST_FLAG_MU 04
  625. static const AVOption test_options[]= {
  626. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  627. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  628. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  629. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  630. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  631. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  632. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  633. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  634. {NULL},
  635. };
  636. static const char *test_get_name(void *ctx)
  637. {
  638. return "test";
  639. }
  640. static const AVClass test_class = {
  641. "TestContext",
  642. test_get_name,
  643. test_options
  644. };
  645. int main(void)
  646. {
  647. int i;
  648. printf("\nTesting av_set_options_string()\n");
  649. {
  650. TestContext test_ctx;
  651. const char *options[] = {
  652. "",
  653. ":",
  654. "=",
  655. "foo=:",
  656. ":=foo",
  657. "=foo",
  658. "foo=",
  659. "foo",
  660. "foo=val",
  661. "foo==val",
  662. "toggle=:",
  663. "string=:",
  664. "toggle=1 : foo",
  665. "toggle=100",
  666. "toggle==1",
  667. "flags=+mu-lame : num=42: toggle=0",
  668. "num=42 : string=blahblah",
  669. "rational=0 : rational=1/2 : rational=1/-1",
  670. "rational=-1/0",
  671. };
  672. test_ctx.class = &test_class;
  673. av_opt_set_defaults(&test_ctx);
  674. test_ctx.string = av_strdup("default");
  675. av_log_set_level(AV_LOG_DEBUG);
  676. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  677. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  678. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  679. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  680. printf("\n");
  681. }
  682. }
  683. return 0;
  684. }
  685. #endif