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.

891 lines
28KB

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