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.

951 lines
30KB

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