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.

982 lines
31KB

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