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.

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