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.

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