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.

985 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_FILTERING_PARAM)? 'F' : '.');
  560. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  561. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  562. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  563. if (opt->help)
  564. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  565. av_log(av_log_obj, AV_LOG_INFO, "\n");
  566. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  567. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  568. }
  569. }
  570. }
  571. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  572. {
  573. if (!obj)
  574. return -1;
  575. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  576. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  577. return 0;
  578. }
  579. void av_opt_set_defaults(void *s)
  580. {
  581. #if FF_API_OLD_AVOPTIONS
  582. av_opt_set_defaults2(s, 0, 0);
  583. }
  584. void av_opt_set_defaults2(void *s, int mask, int flags)
  585. {
  586. #endif
  587. const AVOption *opt = NULL;
  588. while ((opt = av_opt_next(s, opt)) != NULL) {
  589. #if FF_API_OLD_AVOPTIONS
  590. if ((opt->flags & mask) != flags)
  591. continue;
  592. #endif
  593. switch (opt->type) {
  594. case AV_OPT_TYPE_CONST:
  595. /* Nothing to be done here */
  596. break;
  597. case AV_OPT_TYPE_FLAGS:
  598. case AV_OPT_TYPE_INT: {
  599. int val;
  600. val = opt->default_val.dbl;
  601. av_opt_set_int(s, opt->name, val, 0);
  602. }
  603. break;
  604. case AV_OPT_TYPE_INT64:
  605. if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
  606. av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
  607. av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
  608. break;
  609. case AV_OPT_TYPE_DOUBLE:
  610. case AV_OPT_TYPE_FLOAT: {
  611. double val;
  612. val = opt->default_val.dbl;
  613. av_opt_set_double(s, opt->name, val, 0);
  614. }
  615. break;
  616. case AV_OPT_TYPE_RATIONAL: {
  617. AVRational val;
  618. val = av_d2q(opt->default_val.dbl, INT_MAX);
  619. av_opt_set_q(s, opt->name, val, 0);
  620. }
  621. break;
  622. case AV_OPT_TYPE_STRING:
  623. case AV_OPT_TYPE_IMAGE_SIZE:
  624. case AV_OPT_TYPE_PIXEL_FMT:
  625. av_opt_set(s, opt->name, opt->default_val.str, 0);
  626. break;
  627. case AV_OPT_TYPE_BINARY:
  628. /* Cannot set default for binary */
  629. break;
  630. default:
  631. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  632. }
  633. }
  634. }
  635. /**
  636. * Store the value in the field in ctx that is named like key.
  637. * ctx must be an AVClass context, storing is done using AVOptions.
  638. *
  639. * @param buf the string to parse, buf will be updated to point at the
  640. * separator just after the parsed key/value pair
  641. * @param key_val_sep a 0-terminated list of characters used to
  642. * separate key from value
  643. * @param pairs_sep a 0-terminated list of characters used to separate
  644. * two pairs from each other
  645. * @return 0 if the key/value pair has been successfully parsed and
  646. * set, or a negative value corresponding to an AVERROR code in case
  647. * of error:
  648. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  649. * the error code issued by av_opt_set() if the key/value pair
  650. * cannot be set
  651. */
  652. static int parse_key_value_pair(void *ctx, const char **buf,
  653. const char *key_val_sep, const char *pairs_sep)
  654. {
  655. char *key = av_get_token(buf, key_val_sep);
  656. char *val;
  657. int ret;
  658. if (*key && strspn(*buf, key_val_sep)) {
  659. (*buf)++;
  660. val = av_get_token(buf, pairs_sep);
  661. } else {
  662. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  663. av_free(key);
  664. return AVERROR(EINVAL);
  665. }
  666. av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
  667. ret = av_opt_set(ctx, key, val, 0);
  668. if (ret == AVERROR_OPTION_NOT_FOUND)
  669. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  670. av_free(key);
  671. av_free(val);
  672. return ret;
  673. }
  674. int av_set_options_string(void *ctx, const char *opts,
  675. const char *key_val_sep, const char *pairs_sep)
  676. {
  677. int ret, count = 0;
  678. if (!opts)
  679. return 0;
  680. while (*opts) {
  681. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  682. return ret;
  683. count++;
  684. if (*opts)
  685. opts++;
  686. }
  687. return count;
  688. }
  689. void av_opt_free(void *obj)
  690. {
  691. const AVOption *o = NULL;
  692. while ((o = av_opt_next(obj, o)))
  693. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  694. av_freep((uint8_t *)obj + o->offset);
  695. }
  696. int av_opt_set_dict(void *obj, AVDictionary **options)
  697. {
  698. AVDictionaryEntry *t = NULL;
  699. AVDictionary *tmp = NULL;
  700. int ret = 0;
  701. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  702. ret = av_opt_set(obj, t->key, t->value, 0);
  703. if (ret == AVERROR_OPTION_NOT_FOUND)
  704. av_dict_set(&tmp, t->key, t->value, 0);
  705. else if (ret < 0) {
  706. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  707. break;
  708. }
  709. ret = 0;
  710. }
  711. av_dict_free(options);
  712. *options = tmp;
  713. return ret;
  714. }
  715. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  716. int opt_flags, int search_flags)
  717. {
  718. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  719. }
  720. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  721. int opt_flags, int search_flags, void **target_obj)
  722. {
  723. const AVClass *c;
  724. const AVOption *o = NULL;
  725. if(!obj)
  726. return NULL;
  727. c= *(AVClass**)obj;
  728. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  729. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  730. const AVClass *child = NULL;
  731. while (child = av_opt_child_class_next(c, child))
  732. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  733. return o;
  734. } else {
  735. void *child = NULL;
  736. while (child = av_opt_child_next(obj, child))
  737. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  738. return o;
  739. }
  740. }
  741. while (o = av_opt_next(obj, o)) {
  742. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  743. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  744. (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
  745. if (target_obj) {
  746. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  747. *target_obj = obj;
  748. else
  749. *target_obj = NULL;
  750. }
  751. return o;
  752. }
  753. }
  754. return NULL;
  755. }
  756. void *av_opt_child_next(void *obj, void *prev)
  757. {
  758. const AVClass *c = *(AVClass**)obj;
  759. if (c->child_next)
  760. return c->child_next(obj, prev);
  761. return NULL;
  762. }
  763. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  764. {
  765. if (parent->child_class_next)
  766. return parent->child_class_next(prev);
  767. return NULL;
  768. }
  769. void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
  770. {
  771. const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
  772. if(!opt)
  773. return NULL;
  774. return (uint8_t*)obj + opt->offset;
  775. }
  776. #ifdef TEST
  777. #undef printf
  778. typedef struct TestContext
  779. {
  780. const AVClass *class;
  781. int num;
  782. int toggle;
  783. char *string;
  784. int flags;
  785. AVRational rational;
  786. int w, h;
  787. enum PixelFormat pix_fmt;
  788. } TestContext;
  789. #define OFFSET(x) offsetof(TestContext, x)
  790. #define TEST_FLAG_COOL 01
  791. #define TEST_FLAG_LAME 02
  792. #define TEST_FLAG_MU 04
  793. static const AVOption test_options[]= {
  794. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
  795. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
  796. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
  797. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  798. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
  799. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  800. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  801. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  802. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
  803. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT,{0}, 0, 0 },
  804. {NULL},
  805. };
  806. static const char *test_get_name(void *ctx)
  807. {
  808. return "test";
  809. }
  810. static const AVClass test_class = {
  811. "TestContext",
  812. test_get_name,
  813. test_options
  814. };
  815. int main(void)
  816. {
  817. int i;
  818. printf("\nTesting av_set_options_string()\n");
  819. {
  820. TestContext test_ctx = { 0 };
  821. const char *options[] = {
  822. "",
  823. ":",
  824. "=",
  825. "foo=:",
  826. ":=foo",
  827. "=foo",
  828. "foo=",
  829. "foo",
  830. "foo=val",
  831. "foo==val",
  832. "toggle=:",
  833. "string=:",
  834. "toggle=1 : foo",
  835. "toggle=100",
  836. "toggle==1",
  837. "flags=+mu-lame : num=42: toggle=0",
  838. "num=42 : string=blahblah",
  839. "rational=0 : rational=1/2 : rational=1/-1",
  840. "rational=-1/0",
  841. "size=1024x768",
  842. "size=pal",
  843. "size=bogus",
  844. "pix_fmt=yuv420p",
  845. "pix_fmt=2",
  846. "pix_fmt=bogus",
  847. };
  848. test_ctx.class = &test_class;
  849. av_opt_set_defaults(&test_ctx);
  850. test_ctx.string = av_strdup("default");
  851. av_log_set_level(AV_LOG_DEBUG);
  852. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  853. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  854. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  855. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  856. printf("\n");
  857. }
  858. av_freep(&test_ctx.string);
  859. }
  860. return 0;
  861. }
  862. #endif