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.

790 lines
25KB

  1. /*
  2. * AVOptions
  3. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * AVOptions
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "avutil.h"
  27. #include "avstring.h"
  28. #include "common.h"
  29. #include "opt.h"
  30. #include "eval.h"
  31. #include "dict.h"
  32. #include "log.h"
  33. #include "mathematics.h"
  34. const AVOption *av_opt_next(void *obj, const AVOption *last)
  35. {
  36. AVClass *class = *(AVClass**)obj;
  37. if (!last && class->option && class->option[0].name)
  38. return class->option;
  39. if (last && last[1].name)
  40. return ++last;
  41. return NULL;
  42. }
  43. static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
  44. {
  45. switch (o->type) {
  46. case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
  47. case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
  48. case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
  49. case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
  50. case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
  51. case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
  52. *den = ((AVRational*)dst)->den;
  53. return 0;
  54. }
  55. return AVERROR(EINVAL);
  56. }
  57. static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  58. {
  59. if (o->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)
  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)\
  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. dst = ((uint8_t*)target_obj) + o->offset;
  230. return write_number(obj, o, dst, num, den, intnum);
  231. }
  232. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  233. {
  234. return set_number(obj, name, 1, 1, val, search_flags);
  235. }
  236. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  237. {
  238. return set_number(obj, name, val, 1, 1, search_flags);
  239. }
  240. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  241. {
  242. return set_number(obj, name, val.num, val.den, 1, search_flags);
  243. }
  244. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  245. {
  246. void *target_obj;
  247. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  248. uint8_t *ptr;
  249. uint8_t **dst;
  250. int *lendst;
  251. if (!o || !target_obj)
  252. return AVERROR_OPTION_NOT_FOUND;
  253. if (o->type != AV_OPT_TYPE_BINARY)
  254. return AVERROR(EINVAL);
  255. ptr = av_malloc(len);
  256. if (!ptr)
  257. return AVERROR(ENOMEM);
  258. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  259. lendst = (int *)(dst + 1);
  260. av_free(*dst);
  261. *dst = ptr;
  262. *lendst = len;
  263. memcpy(ptr, val, len);
  264. return 0;
  265. }
  266. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  267. {
  268. void *dst, *target_obj;
  269. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  270. uint8_t *bin, buf[128];
  271. int len, i, ret;
  272. if (!o || !target_obj)
  273. return AVERROR_OPTION_NOT_FOUND;
  274. dst = (uint8_t*)target_obj + o->offset;
  275. buf[0] = 0;
  276. switch (o->type) {
  277. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  278. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  279. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  280. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  281. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  282. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  283. case AV_OPT_TYPE_STRING:
  284. if (*(uint8_t**)dst)
  285. *out_val = av_strdup(*(uint8_t**)dst);
  286. else
  287. *out_val = av_strdup("");
  288. return 0;
  289. case AV_OPT_TYPE_BINARY:
  290. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  291. if ((uint64_t)len*2 + 1 > INT_MAX)
  292. return AVERROR(EINVAL);
  293. if (!(*out_val = av_malloc(len*2 + 1)))
  294. return AVERROR(ENOMEM);
  295. bin = *(uint8_t**)dst;
  296. for (i = 0; i < len; i++)
  297. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  298. return 0;
  299. default:
  300. return AVERROR(EINVAL);
  301. }
  302. if (ret >= sizeof(buf))
  303. return AVERROR(EINVAL);
  304. *out_val = av_strdup(buf);
  305. return 0;
  306. }
  307. static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum,
  308. int search_flags)
  309. {
  310. void *dst, *target_obj;
  311. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  312. if (!o || !target_obj)
  313. goto error;
  314. dst = ((uint8_t*)target_obj) + o->offset;
  315. return read_number(o, dst, num, den, intnum);
  316. error:
  317. *den=*intnum=0;
  318. return -1;
  319. }
  320. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  321. {
  322. int64_t intnum = 1;
  323. double num = 1;
  324. int ret, den = 1;
  325. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  326. return ret;
  327. *out_val = num*intnum/den;
  328. return 0;
  329. }
  330. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  331. {
  332. int64_t intnum = 1;
  333. double num = 1;
  334. int ret, den = 1;
  335. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  336. return ret;
  337. *out_val = num*intnum/den;
  338. return 0;
  339. }
  340. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  341. {
  342. int64_t intnum = 1;
  343. double num = 1;
  344. int ret, den = 1;
  345. if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
  346. return ret;
  347. if (num == 1.0 && (int)intnum == intnum)
  348. *out_val = (AVRational){intnum, den};
  349. else
  350. *out_val = av_d2q(num*intnum/den, 1<<24);
  351. return 0;
  352. }
  353. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  354. {
  355. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  356. const AVOption *flag = av_opt_find(obj, flag_name,
  357. field ? field->unit : NULL, 0, 0);
  358. int64_t res;
  359. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  360. av_opt_get_int(obj, field_name, 0, &res) < 0)
  361. return 0;
  362. return res & flag->default_val.i64;
  363. }
  364. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  365. int req_flags, int rej_flags)
  366. {
  367. const AVOption *opt=NULL;
  368. while ((opt = av_opt_next(obj, opt))) {
  369. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  370. continue;
  371. /* Don't print CONST's on level one.
  372. * Don't print anything but CONST's on level two.
  373. * Only print items from the requested unit.
  374. */
  375. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  376. continue;
  377. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  378. continue;
  379. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  380. continue;
  381. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  382. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  383. else
  384. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  385. switch (opt->type) {
  386. case AV_OPT_TYPE_FLAGS:
  387. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  388. break;
  389. case AV_OPT_TYPE_INT:
  390. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  391. break;
  392. case AV_OPT_TYPE_INT64:
  393. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  394. break;
  395. case AV_OPT_TYPE_DOUBLE:
  396. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  397. break;
  398. case AV_OPT_TYPE_FLOAT:
  399. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  400. break;
  401. case AV_OPT_TYPE_STRING:
  402. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  403. break;
  404. case AV_OPT_TYPE_RATIONAL:
  405. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  406. break;
  407. case AV_OPT_TYPE_BINARY:
  408. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  409. break;
  410. case AV_OPT_TYPE_CONST:
  411. default:
  412. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  413. break;
  414. }
  415. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  416. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  417. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  418. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  419. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  420. if (opt->help)
  421. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  422. av_log(av_log_obj, AV_LOG_INFO, "\n");
  423. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  424. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  425. }
  426. }
  427. }
  428. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  429. {
  430. if (!obj)
  431. return -1;
  432. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  433. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  434. return 0;
  435. }
  436. void av_opt_set_defaults(void *s)
  437. {
  438. const AVOption *opt = NULL;
  439. while ((opt = av_opt_next(s, opt)) != NULL) {
  440. switch (opt->type) {
  441. case AV_OPT_TYPE_CONST:
  442. /* Nothing to be done here */
  443. break;
  444. case AV_OPT_TYPE_FLAGS:
  445. case AV_OPT_TYPE_INT:
  446. case AV_OPT_TYPE_INT64:
  447. av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
  448. break;
  449. case AV_OPT_TYPE_DOUBLE:
  450. case AV_OPT_TYPE_FLOAT: {
  451. double val;
  452. val = opt->default_val.dbl;
  453. av_opt_set_double(s, opt->name, val, 0);
  454. }
  455. break;
  456. case AV_OPT_TYPE_RATIONAL: {
  457. AVRational val;
  458. val = av_d2q(opt->default_val.dbl, INT_MAX);
  459. av_opt_set_q(s, opt->name, val, 0);
  460. }
  461. break;
  462. case AV_OPT_TYPE_STRING:
  463. av_opt_set(s, opt->name, opt->default_val.str, 0);
  464. break;
  465. case AV_OPT_TYPE_BINARY:
  466. /* Cannot set default for binary */
  467. break;
  468. default:
  469. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  470. }
  471. }
  472. }
  473. /**
  474. * Store the value in the field in ctx that is named like key.
  475. * ctx must be an AVClass context, storing is done using AVOptions.
  476. *
  477. * @param buf the string to parse, buf will be updated to point at the
  478. * separator just after the parsed key/value pair
  479. * @param key_val_sep a 0-terminated list of characters used to
  480. * separate key from value
  481. * @param pairs_sep a 0-terminated list of characters used to separate
  482. * two pairs from each other
  483. * @return 0 if the key/value pair has been successfully parsed and
  484. * set, or a negative value corresponding to an AVERROR code in case
  485. * of error:
  486. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  487. * the error code issued by av_opt_set() if the key/value pair
  488. * cannot be set
  489. */
  490. static int parse_key_value_pair(void *ctx, const char **buf,
  491. const char *key_val_sep, const char *pairs_sep)
  492. {
  493. char *key = av_get_token(buf, key_val_sep);
  494. char *val;
  495. int ret;
  496. if (!key)
  497. return AVERROR(ENOMEM);
  498. if (*key && strspn(*buf, key_val_sep)) {
  499. (*buf)++;
  500. val = av_get_token(buf, pairs_sep);
  501. if (!val) {
  502. av_freep(&key);
  503. return AVERROR(ENOMEM);
  504. }
  505. } else {
  506. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  507. av_free(key);
  508. return AVERROR(EINVAL);
  509. }
  510. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  511. ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
  512. if (ret == AVERROR_OPTION_NOT_FOUND)
  513. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  514. av_free(key);
  515. av_free(val);
  516. return ret;
  517. }
  518. int av_set_options_string(void *ctx, const char *opts,
  519. const char *key_val_sep, const char *pairs_sep)
  520. {
  521. int ret, count = 0;
  522. if (!opts)
  523. return 0;
  524. while (*opts) {
  525. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  526. return ret;
  527. count++;
  528. if (*opts)
  529. opts++;
  530. }
  531. return count;
  532. }
  533. void av_opt_free(void *obj)
  534. {
  535. const AVOption *o = NULL;
  536. while ((o = av_opt_next(obj, o)))
  537. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  538. av_freep((uint8_t *)obj + o->offset);
  539. }
  540. int av_opt_set_dict(void *obj, AVDictionary **options)
  541. {
  542. AVDictionaryEntry *t = NULL;
  543. AVDictionary *tmp = NULL;
  544. int ret = 0;
  545. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  546. ret = av_opt_set(obj, t->key, t->value, 0);
  547. if (ret == AVERROR_OPTION_NOT_FOUND)
  548. av_dict_set(&tmp, t->key, t->value, 0);
  549. else if (ret < 0) {
  550. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  551. break;
  552. }
  553. ret = 0;
  554. }
  555. av_dict_free(options);
  556. *options = tmp;
  557. return ret;
  558. }
  559. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  560. int opt_flags, int search_flags)
  561. {
  562. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  563. }
  564. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  565. int opt_flags, int search_flags, void **target_obj)
  566. {
  567. const AVClass *c = *(AVClass**)obj;
  568. const AVOption *o = NULL;
  569. if (!c)
  570. return NULL;
  571. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  572. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  573. const AVClass *child = NULL;
  574. while (child = av_opt_child_class_next(c, child))
  575. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  576. return o;
  577. } else {
  578. void *child = NULL;
  579. while (child = av_opt_child_next(obj, child))
  580. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  581. return o;
  582. }
  583. }
  584. while (o = av_opt_next(obj, o)) {
  585. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  586. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  587. (unit && o->unit && !strcmp(o->unit, unit)))) {
  588. if (target_obj) {
  589. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  590. *target_obj = obj;
  591. else
  592. *target_obj = NULL;
  593. }
  594. return o;
  595. }
  596. }
  597. return NULL;
  598. }
  599. void *av_opt_child_next(void *obj, void *prev)
  600. {
  601. const AVClass *c = *(AVClass**)obj;
  602. if (c->child_next)
  603. return c->child_next(obj, prev);
  604. return NULL;
  605. }
  606. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  607. {
  608. if (parent->child_class_next)
  609. return parent->child_class_next(prev);
  610. return NULL;
  611. }
  612. #ifdef TEST
  613. typedef struct TestContext
  614. {
  615. const AVClass *class;
  616. int num;
  617. int toggle;
  618. char *string;
  619. int flags;
  620. AVRational rational;
  621. } TestContext;
  622. #define OFFSET(x) offsetof(TestContext, x)
  623. #define TEST_FLAG_COOL 01
  624. #define TEST_FLAG_LAME 02
  625. #define TEST_FLAG_MU 04
  626. static const AVOption test_options[]= {
  627. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  628. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  629. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  630. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  631. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  632. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  633. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  634. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  635. {NULL},
  636. };
  637. static const char *test_get_name(void *ctx)
  638. {
  639. return "test";
  640. }
  641. static const AVClass test_class = {
  642. "TestContext",
  643. test_get_name,
  644. test_options
  645. };
  646. int main(void)
  647. {
  648. int i;
  649. printf("\nTesting av_set_options_string()\n");
  650. {
  651. TestContext test_ctx;
  652. const char *options[] = {
  653. "",
  654. ":",
  655. "=",
  656. "foo=:",
  657. ":=foo",
  658. "=foo",
  659. "foo=",
  660. "foo",
  661. "foo=val",
  662. "foo==val",
  663. "toggle=:",
  664. "string=:",
  665. "toggle=1 : foo",
  666. "toggle=100",
  667. "toggle==1",
  668. "flags=+mu-lame : num=42: toggle=0",
  669. "num=42 : string=blahblah",
  670. "rational=0 : rational=1/2 : rational=1/-1",
  671. "rational=-1/0",
  672. };
  673. test_ctx.class = &test_class;
  674. av_opt_set_defaults(&test_ctx);
  675. test_ctx.string = av_strdup("default");
  676. av_log_set_level(AV_LOG_DEBUG);
  677. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  678. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  679. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  680. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  681. printf("\n");
  682. }
  683. }
  684. return 0;
  685. }
  686. #endif