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.

850 lines
27KB

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