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.

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