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.

888 lines
26KB

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