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.

886 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. }
  223. int av_opt_set(void *obj, const char *name, const char *val, 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 (!val || o->flags & AV_OPT_FLAG_READONLY)
  230. return AVERROR(EINVAL);
  231. dst = ((uint8_t *)target_obj) + o->offset;
  232. switch (o->type) {
  233. case AV_OPT_TYPE_STRING:
  234. return set_string(obj, o, val, dst);
  235. case AV_OPT_TYPE_BINARY:
  236. return set_string_binary(obj, o, val, dst);
  237. case AV_OPT_TYPE_FLAGS:
  238. case AV_OPT_TYPE_INT:
  239. case AV_OPT_TYPE_INT64:
  240. case AV_OPT_TYPE_FLOAT:
  241. case AV_OPT_TYPE_DOUBLE:
  242. case AV_OPT_TYPE_RATIONAL:
  243. return set_string_number(obj, target_obj, o, val, dst);
  244. }
  245. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  246. return AVERROR(EINVAL);
  247. }
  248. #define OPT_EVAL_NUMBER(name, opttype, vartype) \
  249. int av_opt_eval_ ## name(void *obj, const AVOption *o, \
  250. const char *val, vartype *name ## _out) \
  251. { \
  252. if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
  253. return AVERROR(EINVAL); \
  254. return set_string_number(obj, obj, o, val, name ## _out); \
  255. }
  256. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  257. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  258. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  259. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  260. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  261. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  262. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  263. int search_flags)
  264. {
  265. void *dst, *target_obj;
  266. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  267. if (!o || !target_obj)
  268. return AVERROR_OPTION_NOT_FOUND;
  269. if (o->flags & AV_OPT_FLAG_READONLY)
  270. return AVERROR(EINVAL);
  271. dst = ((uint8_t *)target_obj) + o->offset;
  272. return write_number(obj, o, dst, num, den, intnum);
  273. }
  274. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  275. {
  276. return set_number(obj, name, 1, 1, val, search_flags);
  277. }
  278. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  279. {
  280. return set_number(obj, name, val, 1, 1, search_flags);
  281. }
  282. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  283. {
  284. return set_number(obj, name, val.num, val.den, 1, search_flags);
  285. }
  286. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  287. {
  288. void *target_obj;
  289. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  290. uint8_t *ptr;
  291. uint8_t **dst;
  292. int *lendst;
  293. if (!o || !target_obj)
  294. return AVERROR_OPTION_NOT_FOUND;
  295. if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
  296. return AVERROR(EINVAL);
  297. ptr = av_malloc(len);
  298. if (!ptr)
  299. return AVERROR(ENOMEM);
  300. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  301. lendst = (int *)(dst + 1);
  302. av_free(*dst);
  303. *dst = ptr;
  304. *lendst = len;
  305. memcpy(ptr, val, len);
  306. return 0;
  307. }
  308. int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
  309. int search_flags)
  310. {
  311. void *target_obj;
  312. AVDictionary **dst;
  313. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  314. if (!o || !target_obj)
  315. return AVERROR_OPTION_NOT_FOUND;
  316. if (o->flags & AV_OPT_FLAG_READONLY)
  317. return AVERROR(EINVAL);
  318. dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
  319. av_dict_free(dst);
  320. av_dict_copy(dst, val, 0);
  321. return 0;
  322. }
  323. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  324. {
  325. void *dst, *target_obj;
  326. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  327. uint8_t *bin, buf[128];
  328. int len, i, ret;
  329. if (!o || !target_obj)
  330. return AVERROR_OPTION_NOT_FOUND;
  331. dst = (uint8_t *)target_obj + o->offset;
  332. buf[0] = 0;
  333. switch (o->type) {
  334. case AV_OPT_TYPE_FLAGS:
  335. ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
  336. break;
  337. case AV_OPT_TYPE_INT:
  338. ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
  339. break;
  340. case AV_OPT_TYPE_INT64:
  341. ret = snprintf(buf, sizeof(buf), "%" PRId64, *(int64_t *)dst);
  342. break;
  343. case AV_OPT_TYPE_FLOAT:
  344. ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
  345. break;
  346. case AV_OPT_TYPE_DOUBLE:
  347. ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
  348. break;
  349. case AV_OPT_TYPE_RATIONAL:
  350. ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num,
  351. ((AVRational *)dst)->den);
  352. break;
  353. case AV_OPT_TYPE_STRING:
  354. if (*(uint8_t **)dst)
  355. *out_val = av_strdup(*(uint8_t **)dst);
  356. else
  357. *out_val = av_strdup("");
  358. return *out_val ? 0 : AVERROR(ENOMEM);
  359. case AV_OPT_TYPE_BINARY:
  360. len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
  361. if ((uint64_t)len * 2 + 1 > INT_MAX)
  362. return AVERROR(EINVAL);
  363. if (!(*out_val = av_malloc(len * 2 + 1)))
  364. return AVERROR(ENOMEM);
  365. bin = *(uint8_t **)dst;
  366. for (i = 0; i < len; i++)
  367. snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
  368. return 0;
  369. default:
  370. return AVERROR(EINVAL);
  371. }
  372. if (ret >= sizeof(buf))
  373. return AVERROR(EINVAL);
  374. *out_val = av_strdup(buf);
  375. return *out_val ? 0 : AVERROR(ENOMEM);
  376. }
  377. static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum,
  378. int search_flags)
  379. {
  380. void *dst, *target_obj;
  381. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  382. if (!o || !target_obj)
  383. goto error;
  384. dst = ((uint8_t *)target_obj) + o->offset;
  385. return read_number(o, dst, num, den, intnum);
  386. error:
  387. *den =
  388. *intnum = 0;
  389. return -1;
  390. }
  391. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  392. {
  393. int64_t intnum = 1;
  394. double num = 1;
  395. int ret, den = 1;
  396. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  397. return ret;
  398. *out_val = num * intnum / den;
  399. return 0;
  400. }
  401. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  402. {
  403. int64_t intnum = 1;
  404. double num = 1;
  405. int ret, den = 1;
  406. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  407. return ret;
  408. *out_val = num * intnum / den;
  409. return 0;
  410. }
  411. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  412. {
  413. int64_t intnum = 1;
  414. double num = 1;
  415. int ret, den = 1;
  416. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  417. return ret;
  418. if (num == 1.0 && (int)intnum == intnum)
  419. *out_val = (AVRational) { intnum, den };
  420. else
  421. *out_val = av_d2q(num * intnum / den, 1 << 24);
  422. return 0;
  423. }
  424. int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
  425. {
  426. void *target_obj;
  427. AVDictionary *src;
  428. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  429. if (!o || !target_obj)
  430. return AVERROR_OPTION_NOT_FOUND;
  431. if (o->type != AV_OPT_TYPE_DICT)
  432. return AVERROR(EINVAL);
  433. src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
  434. av_dict_copy(out_val, src, 0);
  435. return 0;
  436. }
  437. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  438. {
  439. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  440. const AVOption *flag = av_opt_find(obj, flag_name,
  441. field ? field->unit : NULL, 0, 0);
  442. int64_t res;
  443. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  444. av_opt_get_int(obj, field_name, 0, &res) < 0)
  445. return 0;
  446. return res & flag->default_val.i64;
  447. }
  448. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  449. int req_flags, int rej_flags)
  450. {
  451. const AVOption *opt = NULL;
  452. while ((opt = av_opt_next(obj, opt))) {
  453. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  454. continue;
  455. /* Don't print CONST's on level one.
  456. * Don't print anything but CONST's on level two.
  457. * Only print items from the requested unit.
  458. */
  459. if (!unit && opt->type == AV_OPT_TYPE_CONST)
  460. continue;
  461. else if (unit && opt->type != AV_OPT_TYPE_CONST)
  462. continue;
  463. else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  464. continue;
  465. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  466. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  467. else
  468. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  469. switch (opt->type) {
  470. case AV_OPT_TYPE_FLAGS:
  471. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  472. break;
  473. case AV_OPT_TYPE_INT:
  474. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  475. break;
  476. case AV_OPT_TYPE_INT64:
  477. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  478. break;
  479. case AV_OPT_TYPE_DOUBLE:
  480. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  481. break;
  482. case AV_OPT_TYPE_FLOAT:
  483. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  484. break;
  485. case AV_OPT_TYPE_STRING:
  486. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  487. break;
  488. case AV_OPT_TYPE_RATIONAL:
  489. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  490. break;
  491. case AV_OPT_TYPE_BINARY:
  492. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  493. break;
  494. case AV_OPT_TYPE_CONST:
  495. default:
  496. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  497. break;
  498. }
  499. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  500. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  501. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM) ? 'V' : '.');
  502. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM) ? 'A' : '.');
  503. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  504. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
  505. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
  506. if (opt->help)
  507. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  508. av_log(av_log_obj, AV_LOG_INFO, "\n");
  509. if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
  510. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  511. }
  512. }
  513. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  514. {
  515. if (!obj)
  516. return -1;
  517. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
  518. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  519. return 0;
  520. }
  521. void av_opt_set_defaults(void *s)
  522. {
  523. const AVOption *opt = NULL;
  524. while ((opt = av_opt_next(s, opt))) {
  525. if (opt->flags & AV_OPT_FLAG_READONLY)
  526. continue;
  527. switch (opt->type) {
  528. case AV_OPT_TYPE_CONST:
  529. /* Nothing to be done here */
  530. break;
  531. case AV_OPT_TYPE_FLAGS:
  532. case AV_OPT_TYPE_INT:
  533. case AV_OPT_TYPE_INT64:
  534. av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
  535. break;
  536. case AV_OPT_TYPE_DOUBLE:
  537. case AV_OPT_TYPE_FLOAT:
  538. {
  539. double val;
  540. val = opt->default_val.dbl;
  541. av_opt_set_double(s, opt->name, val, 0);
  542. }
  543. break;
  544. case AV_OPT_TYPE_RATIONAL:
  545. {
  546. AVRational val;
  547. val = av_d2q(opt->default_val.dbl, INT_MAX);
  548. av_opt_set_q(s, opt->name, val, 0);
  549. }
  550. break;
  551. case AV_OPT_TYPE_STRING:
  552. av_opt_set(s, opt->name, opt->default_val.str, 0);
  553. break;
  554. case AV_OPT_TYPE_BINARY:
  555. case AV_OPT_TYPE_DICT:
  556. /* Cannot set defaults for these types */
  557. break;
  558. default:
  559. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
  560. opt->type, opt->name);
  561. }
  562. }
  563. }
  564. /**
  565. * Store the value in the field in ctx that is named like key.
  566. * ctx must be an AVClass context, storing is done using AVOptions.
  567. *
  568. * @param buf the string to parse, buf will be updated to point at the
  569. * separator just after the parsed key/value pair
  570. * @param key_val_sep a 0-terminated list of characters used to
  571. * separate key from value
  572. * @param pairs_sep a 0-terminated list of characters used to separate
  573. * two pairs from each other
  574. * @return 0 if the key/value pair has been successfully parsed and
  575. * set, or a negative value corresponding to an AVERROR code in case
  576. * of error:
  577. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  578. * the error code issued by av_opt_set() if the key/value pair
  579. * cannot be set
  580. */
  581. static int parse_key_value_pair(void *ctx, const char **buf,
  582. const char *key_val_sep, const char *pairs_sep)
  583. {
  584. char *key = av_get_token(buf, key_val_sep);
  585. char *val;
  586. int ret;
  587. if (!key)
  588. return AVERROR(ENOMEM);
  589. if (*key && strspn(*buf, key_val_sep)) {
  590. (*buf)++;
  591. val = av_get_token(buf, pairs_sep);
  592. if (!val) {
  593. av_freep(&key);
  594. return AVERROR(ENOMEM);
  595. }
  596. } else {
  597. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  598. av_free(key);
  599. return AVERROR(EINVAL);
  600. }
  601. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  602. ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
  603. if (ret == AVERROR_OPTION_NOT_FOUND)
  604. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  605. av_free(key);
  606. av_free(val);
  607. return ret;
  608. }
  609. int av_set_options_string(void *ctx, const char *opts,
  610. const char *key_val_sep, const char *pairs_sep)
  611. {
  612. int ret, count = 0;
  613. if (!opts)
  614. return 0;
  615. while (*opts) {
  616. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  617. return ret;
  618. count++;
  619. if (*opts)
  620. opts++;
  621. }
  622. return count;
  623. }
  624. void av_opt_free(void *obj)
  625. {
  626. const AVOption *o = NULL;
  627. while ((o = av_opt_next(obj, o))) {
  628. switch (o->type) {
  629. case AV_OPT_TYPE_STRING:
  630. case AV_OPT_TYPE_BINARY:
  631. av_freep((uint8_t *)obj + o->offset);
  632. break;
  633. case AV_OPT_TYPE_DICT:
  634. av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
  635. break;
  636. default:
  637. break;
  638. }
  639. }
  640. }
  641. int av_opt_set_dict(void *obj, AVDictionary **options)
  642. {
  643. AVDictionaryEntry *t = NULL;
  644. AVDictionary *tmp = NULL;
  645. int ret = 0;
  646. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  647. ret = av_opt_set(obj, t->key, t->value, 0);
  648. if (ret == AVERROR_OPTION_NOT_FOUND)
  649. av_dict_set(&tmp, t->key, t->value, 0);
  650. else if (ret < 0) {
  651. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  652. break;
  653. }
  654. ret = 0;
  655. }
  656. av_dict_free(options);
  657. *options = tmp;
  658. return ret;
  659. }
  660. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  661. int opt_flags, int search_flags)
  662. {
  663. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  664. }
  665. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  666. int opt_flags, int search_flags, void **target_obj)
  667. {
  668. const AVClass *c = *(AVClass **)obj;
  669. const AVOption *o = NULL;
  670. if (!c)
  671. return NULL;
  672. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  673. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  674. const AVClass *child = NULL;
  675. while (child = av_opt_child_class_next(c, child))
  676. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  677. return o;
  678. } else {
  679. void *child = NULL;
  680. while (child = av_opt_child_next(obj, child))
  681. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  682. return o;
  683. }
  684. }
  685. while (o = av_opt_next(obj, o)) {
  686. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  687. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  688. (unit && o->unit && !strcmp(o->unit, unit)))) {
  689. if (target_obj) {
  690. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  691. *target_obj = obj;
  692. else
  693. *target_obj = NULL;
  694. }
  695. return o;
  696. }
  697. }
  698. return NULL;
  699. }
  700. void *av_opt_child_next(void *obj, void *prev)
  701. {
  702. const AVClass *c = *(AVClass **)obj;
  703. if (c->child_next)
  704. return c->child_next(obj, prev);
  705. return NULL;
  706. }
  707. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  708. {
  709. if (parent->child_class_next)
  710. return parent->child_class_next(prev);
  711. return NULL;
  712. }
  713. static int opt_size(enum AVOptionType type)
  714. {
  715. switch (type) {
  716. case AV_OPT_TYPE_INT:
  717. case AV_OPT_TYPE_FLAGS:
  718. return sizeof(int);
  719. case AV_OPT_TYPE_INT64:
  720. return sizeof(int64_t);
  721. case AV_OPT_TYPE_DOUBLE:
  722. return sizeof(double);
  723. case AV_OPT_TYPE_FLOAT:
  724. return sizeof(float);
  725. case AV_OPT_TYPE_STRING:
  726. return sizeof(uint8_t *);
  727. case AV_OPT_TYPE_RATIONAL:
  728. return sizeof(AVRational);
  729. case AV_OPT_TYPE_BINARY:
  730. return sizeof(uint8_t *) + sizeof(int);
  731. }
  732. return AVERROR(EINVAL);
  733. }
  734. int av_opt_copy(void *dst, const void *src)
  735. {
  736. const AVOption *o = NULL;
  737. const AVClass *c;
  738. int ret = 0;
  739. if (!src)
  740. return AVERROR(EINVAL);
  741. c = *(AVClass **)src;
  742. if (!c || c != *(AVClass **)dst)
  743. return AVERROR(EINVAL);
  744. while ((o = av_opt_next(src, o))) {
  745. void *field_dst = (uint8_t *)dst + o->offset;
  746. void *field_src = (uint8_t *)src + o->offset;
  747. uint8_t **field_dst8 = (uint8_t **)field_dst;
  748. uint8_t **field_src8 = (uint8_t **)field_src;
  749. if (o->type == AV_OPT_TYPE_STRING) {
  750. set_string(dst, o, *field_src8, field_dst8);
  751. if (*field_src8 && !*field_dst8)
  752. ret = AVERROR(ENOMEM);
  753. } else if (o->type == AV_OPT_TYPE_BINARY) {
  754. int len = *(int *)(field_src8 + 1);
  755. if (*field_dst8 != *field_src8)
  756. av_freep(field_dst8);
  757. if (len) {
  758. *field_dst8 = av_malloc(len);
  759. if (!*field_dst8) {
  760. ret = AVERROR(ENOMEM);
  761. len = 0;
  762. }
  763. memcpy(*field_dst8, *field_src8, len);
  764. } else {
  765. *field_dst8 = NULL;
  766. }
  767. *(int *)(field_dst8 + 1) = len;
  768. } else if (o->type == AV_OPT_TYPE_CONST) {
  769. // do nothing
  770. } else {
  771. int size = opt_size(o->type);
  772. if (size < 0)
  773. ret = size;
  774. else
  775. memcpy(field_dst, field_src, size);
  776. }
  777. }
  778. return ret;
  779. }