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.

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