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.

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