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.

987 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->default_val.i64 : opt->default_val.dbl)
  149. static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
  150. {
  151. int ret = 0, notfirst = 0;
  152. for (;;) {
  153. int i, den = 1;
  154. char buf[256];
  155. int cmd = 0;
  156. double d, num = 1;
  157. int64_t intnum = 1;
  158. if (*val == '+' || *val == '-')
  159. cmd = *(val++);
  160. for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  161. buf[i] = val[i];
  162. buf[i] = 0;
  163. {
  164. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  165. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  166. d = DEFAULT_NUMVAL(o_named);
  167. else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
  168. else if (!strcmp(buf, "max" )) d = o->max;
  169. else if (!strcmp(buf, "min" )) d = o->min;
  170. else if (!strcmp(buf, "none" )) d = 0;
  171. else if (!strcmp(buf, "all" )) d = ~0;
  172. else {
  173. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  174. if (res < 0) {
  175. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  176. return res;
  177. }
  178. }
  179. }
  180. if (o->type == AV_OPT_TYPE_FLAGS) {
  181. read_number(o, dst, NULL, NULL, &intnum);
  182. if (cmd == '+') d = intnum | (int64_t)d;
  183. else if (cmd == '-') d = intnum &~(int64_t)d;
  184. } else {
  185. read_number(o, dst, &num, &den, &intnum);
  186. if (cmd == '+') d = notfirst*num*intnum/den + d;
  187. else if (cmd == '-') d = notfirst*num*intnum/den - d;
  188. }
  189. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  190. return ret;
  191. val += i;
  192. if (!*val)
  193. return 0;
  194. notfirst = 1;
  195. }
  196. return 0;
  197. }
  198. #if FF_API_OLD_AVOPTIONS
  199. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  200. {
  201. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  202. if (o_out)
  203. *o_out = o;
  204. return av_opt_set(obj, name, val, 0);
  205. }
  206. #endif
  207. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  208. {
  209. int ret;
  210. void *dst, *target_obj;
  211. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  212. if (!o || !target_obj)
  213. return AVERROR_OPTION_NOT_FOUND;
  214. if (!val && o->type != AV_OPT_TYPE_STRING)
  215. return AVERROR(EINVAL);
  216. dst = ((uint8_t*)target_obj) + o->offset;
  217. switch (o->type) {
  218. case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  219. case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  220. case AV_OPT_TYPE_FLAGS:
  221. case AV_OPT_TYPE_INT:
  222. case AV_OPT_TYPE_INT64:
  223. case AV_OPT_TYPE_FLOAT:
  224. case AV_OPT_TYPE_DOUBLE:
  225. case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  226. case AV_OPT_TYPE_IMAGE_SIZE:
  227. ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
  228. if (ret < 0)
  229. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
  230. return ret;
  231. case AV_OPT_TYPE_PIXEL_FMT:
  232. ret = av_get_pix_fmt(val);
  233. if (ret == PIX_FMT_NONE) {
  234. char *tail;
  235. ret = strtol(val, &tail, 0);
  236. if (*tail || (unsigned)ret >= PIX_FMT_NB) {
  237. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
  238. return AVERROR(EINVAL);
  239. }
  240. }
  241. *(enum PixelFormat *)dst = ret;
  242. return 0;
  243. }
  244. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  245. return AVERROR(EINVAL);
  246. }
  247. #define OPT_EVAL_NUMBER(name, opttype, vartype)\
  248. int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
  249. {\
  250. if (!o || o->type != opttype)\
  251. return AVERROR(EINVAL);\
  252. return set_string_number(obj, o, val, name ## _out);\
  253. }
  254. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  255. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  256. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  257. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  258. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  259. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  260. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  261. int search_flags)
  262. {
  263. void *dst, *target_obj;
  264. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  265. if (!o || !target_obj)
  266. return AVERROR_OPTION_NOT_FOUND;
  267. dst = ((uint8_t*)target_obj) + o->offset;
  268. return write_number(obj, o, dst, num, den, intnum);
  269. }
  270. #if FF_API_OLD_AVOPTIONS
  271. const AVOption *av_set_double(void *obj, const char *name, double n)
  272. {
  273. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  274. if (set_number(obj, name, n, 1, 1, 0) < 0)
  275. return NULL;
  276. return o;
  277. }
  278. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  279. {
  280. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  281. if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
  282. return NULL;
  283. return o;
  284. }
  285. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  286. {
  287. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  288. if (set_number(obj, name, 1, 1, n, 0) < 0)
  289. return NULL;
  290. return o;
  291. }
  292. #endif
  293. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  294. {
  295. return set_number(obj, name, 1, 1, val, search_flags);
  296. }
  297. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  298. {
  299. return set_number(obj, name, val, 1, 1, search_flags);
  300. }
  301. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  302. {
  303. return set_number(obj, name, val.num, val.den, 1, search_flags);
  304. }
  305. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  306. {
  307. void *target_obj;
  308. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  309. uint8_t *ptr;
  310. uint8_t **dst;
  311. int *lendst;
  312. if (!o || !target_obj)
  313. return AVERROR_OPTION_NOT_FOUND;
  314. if (o->type != AV_OPT_TYPE_BINARY)
  315. return AVERROR(EINVAL);
  316. ptr = av_malloc(len);
  317. if (!ptr)
  318. return AVERROR(ENOMEM);
  319. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  320. lendst = (int *)(dst + 1);
  321. av_free(*dst);
  322. *dst = ptr;
  323. *lendst = len;
  324. memcpy(ptr, val, len);
  325. return 0;
  326. }
  327. #if FF_API_OLD_AVOPTIONS
  328. /**
  329. *
  330. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  331. * @param buf_len allocated length in bytes of buf
  332. */
  333. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  334. {
  335. const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  336. void *dst;
  337. uint8_t *bin;
  338. int len, i;
  339. if (!o)
  340. return NULL;
  341. if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
  342. return NULL;
  343. dst= ((uint8_t*)obj) + o->offset;
  344. if (o_out) *o_out= o;
  345. switch (o->type) {
  346. case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  347. case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  348. case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  349. case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  350. case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  351. case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  352. case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
  353. case AV_OPT_TYPE_STRING: return *(void**)dst;
  354. case AV_OPT_TYPE_BINARY:
  355. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  356. if (len >= (buf_len + 1)/2) return NULL;
  357. bin = *(uint8_t**)dst;
  358. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  359. break;
  360. default: return NULL;
  361. }
  362. return buf;
  363. }
  364. #endif
  365. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  366. {
  367. void *dst, *target_obj;
  368. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  369. uint8_t *bin, buf[128];
  370. int len, i, ret;
  371. if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
  372. return AVERROR_OPTION_NOT_FOUND;
  373. dst = (uint8_t*)target_obj + o->offset;
  374. buf[0] = 0;
  375. switch (o->type) {
  376. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  377. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  378. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  379. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  380. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  381. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  382. case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
  383. case AV_OPT_TYPE_STRING:
  384. if (*(uint8_t**)dst)
  385. *out_val = av_strdup(*(uint8_t**)dst);
  386. else
  387. *out_val = av_strdup("");
  388. return 0;
  389. case AV_OPT_TYPE_BINARY:
  390. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  391. if ((uint64_t)len*2 + 1 > INT_MAX)
  392. return AVERROR(EINVAL);
  393. if (!(*out_val = av_malloc(len*2 + 1)))
  394. return AVERROR(ENOMEM);
  395. bin = *(uint8_t**)dst;
  396. for (i = 0; i < len; i++)
  397. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  398. return 0;
  399. case AV_OPT_TYPE_IMAGE_SIZE:
  400. ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
  401. break;
  402. case AV_OPT_TYPE_PIXEL_FMT:
  403. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum PixelFormat *)dst), "?"));
  404. break;
  405. default:
  406. return AVERROR(EINVAL);
  407. }
  408. if (ret >= sizeof(buf))
  409. return AVERROR(EINVAL);
  410. *out_val = av_strdup(buf);
  411. return 0;
  412. }
  413. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  414. int search_flags)
  415. {
  416. void *dst, *target_obj;
  417. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  418. if (!o || !target_obj)
  419. goto error;
  420. dst = ((uint8_t*)target_obj) + o->offset;
  421. if (o_out) *o_out= o;
  422. return read_number(o, dst, num, den, intnum);
  423. error:
  424. *den=*intnum=0;
  425. return -1;
  426. }
  427. #if FF_API_OLD_AVOPTIONS
  428. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  429. {
  430. int64_t intnum=1;
  431. double num=1;
  432. int den=1;
  433. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  434. return NAN;
  435. return num*intnum/den;
  436. }
  437. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  438. {
  439. int64_t intnum=1;
  440. double num=1;
  441. int den=1;
  442. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  443. return (AVRational){0, 0};
  444. if (num == 1.0 && (int)intnum == intnum)
  445. return (AVRational){intnum, den};
  446. else
  447. return av_d2q(num*intnum/den, 1<<24);
  448. }
  449. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  450. {
  451. int64_t intnum=1;
  452. double num=1;
  453. int den=1;
  454. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  455. return -1;
  456. return num*intnum/den;
  457. }
  458. #endif
  459. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  460. {
  461. int64_t intnum = 1;
  462. double num = 1;
  463. int ret, den = 1;
  464. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  465. return ret;
  466. *out_val = num*intnum/den;
  467. return 0;
  468. }
  469. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  470. {
  471. int64_t intnum = 1;
  472. double num = 1;
  473. int ret, den = 1;
  474. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  475. return ret;
  476. *out_val = num*intnum/den;
  477. return 0;
  478. }
  479. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  480. {
  481. int64_t intnum = 1;
  482. double num = 1;
  483. int ret, den = 1;
  484. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  485. return ret;
  486. if (num == 1.0 && (int)intnum == intnum)
  487. *out_val = (AVRational){intnum, den};
  488. else
  489. *out_val = av_d2q(num*intnum/den, 1<<24);
  490. return 0;
  491. }
  492. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  493. {
  494. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  495. const AVOption *flag = av_opt_find(obj, flag_name,
  496. field ? field->unit : NULL, 0, 0);
  497. int64_t res;
  498. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  499. av_opt_get_int(obj, field_name, 0, &res) < 0)
  500. return 0;
  501. return res & (int) flag->default_val.dbl;
  502. }
  503. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  504. int req_flags, int rej_flags)
  505. {
  506. const AVOption *opt=NULL;
  507. while ((opt = av_opt_next(obj, opt))) {
  508. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  509. continue;
  510. /* Don't print CONST's on level one.
  511. * Don't print anything but CONST's on level two.
  512. * Only print items from the requested unit.
  513. */
  514. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  515. continue;
  516. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  517. continue;
  518. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  519. continue;
  520. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  521. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  522. else
  523. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  524. switch (opt->type) {
  525. case AV_OPT_TYPE_FLAGS:
  526. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  527. break;
  528. case AV_OPT_TYPE_INT:
  529. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  530. break;
  531. case AV_OPT_TYPE_INT64:
  532. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  533. break;
  534. case AV_OPT_TYPE_DOUBLE:
  535. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  536. break;
  537. case AV_OPT_TYPE_FLOAT:
  538. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  539. break;
  540. case AV_OPT_TYPE_STRING:
  541. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  542. break;
  543. case AV_OPT_TYPE_RATIONAL:
  544. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  545. break;
  546. case AV_OPT_TYPE_BINARY:
  547. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  548. break;
  549. case AV_OPT_TYPE_IMAGE_SIZE:
  550. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<image_size>");
  551. break;
  552. case AV_OPT_TYPE_PIXEL_FMT:
  553. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<pix_fmt>");
  554. break;
  555. case AV_OPT_TYPE_CONST:
  556. default:
  557. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  558. break;
  559. }
  560. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  561. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  562. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
  563. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  564. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  565. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  566. if (opt->help)
  567. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  568. av_log(av_log_obj, AV_LOG_INFO, "\n");
  569. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  570. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  571. }
  572. }
  573. }
  574. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  575. {
  576. if (!obj)
  577. return -1;
  578. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  579. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  580. return 0;
  581. }
  582. void av_opt_set_defaults(void *s)
  583. {
  584. #if FF_API_OLD_AVOPTIONS
  585. av_opt_set_defaults2(s, 0, 0);
  586. }
  587. void av_opt_set_defaults2(void *s, int mask, int flags)
  588. {
  589. #endif
  590. const AVOption *opt = NULL;
  591. while ((opt = av_opt_next(s, opt)) != NULL) {
  592. #if FF_API_OLD_AVOPTIONS
  593. if ((opt->flags & mask) != flags)
  594. continue;
  595. #endif
  596. switch (opt->type) {
  597. case AV_OPT_TYPE_CONST:
  598. /* Nothing to be done here */
  599. break;
  600. case AV_OPT_TYPE_FLAGS:
  601. case AV_OPT_TYPE_INT: {
  602. int val;
  603. val = opt->default_val.dbl;
  604. av_opt_set_int(s, opt->name, val, 0);
  605. }
  606. break;
  607. case AV_OPT_TYPE_INT64:
  608. av_opt_set_int(s, opt->name, opt->default_val.i64, 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