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.

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