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.

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