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.

2129 lines
68KB

  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 "avassert.h"
  28. #include "avstring.h"
  29. #include "channel_layout.h"
  30. #include "common.h"
  31. #include "dict.h"
  32. #include "eval.h"
  33. #include "log.h"
  34. #include "parseutils.h"
  35. #include "pixdesc.h"
  36. #include "mathematics.h"
  37. #include "opt.h"
  38. #include "samplefmt.h"
  39. #include "bprint.h"
  40. #include <float.h>
  41. const AVOption *av_opt_next(const void *obj, const AVOption *last)
  42. {
  43. const AVClass *class;
  44. if (!obj)
  45. return NULL;
  46. class = *(const AVClass**)obj;
  47. if (!last && class && class->option && class->option[0].name)
  48. return class->option;
  49. if (last && last[1].name)
  50. return ++last;
  51. return NULL;
  52. }
  53. static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
  54. {
  55. switch (o->type) {
  56. case AV_OPT_TYPE_FLAGS:
  57. *intnum = *(unsigned int*)dst;
  58. return 0;
  59. case AV_OPT_TYPE_PIXEL_FMT:
  60. *intnum = *(enum AVPixelFormat *)dst;
  61. return 0;
  62. case AV_OPT_TYPE_SAMPLE_FMT:
  63. *intnum = *(enum AVSampleFormat *)dst;
  64. return 0;
  65. case AV_OPT_TYPE_BOOL:
  66. case AV_OPT_TYPE_INT:
  67. *intnum = *(int *)dst;
  68. return 0;
  69. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  70. case AV_OPT_TYPE_DURATION:
  71. case AV_OPT_TYPE_INT64:
  72. case AV_OPT_TYPE_UINT64:
  73. *intnum = *(int64_t *)dst;
  74. return 0;
  75. case AV_OPT_TYPE_FLOAT:
  76. *num = *(float *)dst;
  77. return 0;
  78. case AV_OPT_TYPE_DOUBLE:
  79. *num = *(double *)dst;
  80. return 0;
  81. case AV_OPT_TYPE_RATIONAL:
  82. *intnum = ((AVRational *)dst)->num;
  83. *den = ((AVRational *)dst)->den;
  84. return 0;
  85. case AV_OPT_TYPE_CONST:
  86. *num = o->default_val.dbl;
  87. return 0;
  88. }
  89. return AVERROR(EINVAL);
  90. }
  91. static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  92. {
  93. if (o->type != AV_OPT_TYPE_FLAGS &&
  94. (!den || o->max * den < num * intnum || o->min * den > num * intnum)) {
  95. num = den ? num * intnum / den : (num && intnum ? INFINITY : NAN);
  96. av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
  97. num, o->name, o->min, o->max);
  98. return AVERROR(ERANGE);
  99. }
  100. if (o->type == AV_OPT_TYPE_FLAGS) {
  101. double d = num*intnum/den;
  102. if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
  103. av_log(obj, AV_LOG_ERROR,
  104. "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
  105. num*intnum/den, o->name);
  106. return AVERROR(ERANGE);
  107. }
  108. }
  109. switch (o->type) {
  110. case AV_OPT_TYPE_PIXEL_FMT:
  111. *(enum AVPixelFormat *)dst = llrint(num / den) * intnum;
  112. break;
  113. case AV_OPT_TYPE_SAMPLE_FMT:
  114. *(enum AVSampleFormat *)dst = llrint(num / den) * intnum;
  115. break;
  116. case AV_OPT_TYPE_BOOL:
  117. case AV_OPT_TYPE_FLAGS:
  118. case AV_OPT_TYPE_INT:
  119. *(int *)dst = llrint(num / den) * intnum;
  120. break;
  121. case AV_OPT_TYPE_DURATION:
  122. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  123. case AV_OPT_TYPE_INT64:{
  124. double d = num / den;
  125. if (intnum == 1 && d == (double)INT64_MAX) {
  126. *(int64_t *)dst = INT64_MAX;
  127. } else
  128. *(int64_t *)dst = llrint(d) * intnum;
  129. break;}
  130. case AV_OPT_TYPE_UINT64:{
  131. double d = num / den;
  132. // We must special case uint64_t here as llrint() does not support values
  133. // outside the int64_t range and there is no portable function which does
  134. // "INT64_MAX + 1ULL" is used as it is representable exactly as IEEE double
  135. // while INT64_MAX is not
  136. if (intnum == 1 && d == (double)UINT64_MAX) {
  137. *(uint64_t *)dst = UINT64_MAX;
  138. } else if (d > INT64_MAX + 1ULL) {
  139. *(uint64_t *)dst = (llrint(d - (INT64_MAX + 1ULL)) + (INT64_MAX + 1ULL))*intnum;
  140. } else {
  141. *(uint64_t *)dst = llrint(d) * intnum;
  142. }
  143. break;}
  144. case AV_OPT_TYPE_FLOAT:
  145. *(float *)dst = num * intnum / den;
  146. break;
  147. case AV_OPT_TYPE_DOUBLE:
  148. *(double *)dst = num * intnum / den;
  149. break;
  150. case AV_OPT_TYPE_RATIONAL:
  151. case AV_OPT_TYPE_VIDEO_RATE:
  152. if ((int) num == num)
  153. *(AVRational *)dst = (AVRational) { num *intnum, den };
  154. else
  155. *(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24);
  156. break;
  157. default:
  158. return AVERROR(EINVAL);
  159. }
  160. return 0;
  161. }
  162. static int hexchar2int(char c) {
  163. if (c >= '0' && c <= '9')
  164. return c - '0';
  165. if (c >= 'a' && c <= 'f')
  166. return c - 'a' + 10;
  167. if (c >= 'A' && c <= 'F')
  168. return c - 'A' + 10;
  169. return -1;
  170. }
  171. static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  172. {
  173. int *lendst = (int *)(dst + 1);
  174. uint8_t *bin, *ptr;
  175. int len;
  176. av_freep(dst);
  177. *lendst = 0;
  178. if (!val || !(len = strlen(val)))
  179. return 0;
  180. if (len & 1)
  181. return AVERROR(EINVAL);
  182. len /= 2;
  183. ptr = bin = av_malloc(len);
  184. if (!ptr)
  185. return AVERROR(ENOMEM);
  186. while (*val) {
  187. int a = hexchar2int(*val++);
  188. int b = hexchar2int(*val++);
  189. if (a < 0 || b < 0) {
  190. av_free(bin);
  191. return AVERROR(EINVAL);
  192. }
  193. *ptr++ = (a << 4) | b;
  194. }
  195. *dst = bin;
  196. *lendst = len;
  197. return 0;
  198. }
  199. static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  200. {
  201. av_freep(dst);
  202. *dst = av_strdup(val);
  203. return *dst ? 0 : AVERROR(ENOMEM);
  204. }
  205. #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
  206. opt->type == AV_OPT_TYPE_UINT64 || \
  207. opt->type == AV_OPT_TYPE_CONST || \
  208. opt->type == AV_OPT_TYPE_FLAGS || \
  209. opt->type == AV_OPT_TYPE_INT) \
  210. ? opt->default_val.i64 \
  211. : opt->default_val.dbl)
  212. static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
  213. {
  214. int ret = 0;
  215. if (o->type == AV_OPT_TYPE_RATIONAL || o->type == AV_OPT_TYPE_VIDEO_RATE) {
  216. int num, den;
  217. char c;
  218. if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
  219. if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
  220. return ret;
  221. ret = 0;
  222. }
  223. }
  224. for (;;) {
  225. int i = 0;
  226. char buf[256];
  227. int cmd = 0;
  228. double d;
  229. int64_t intnum = 1;
  230. if (o->type == AV_OPT_TYPE_FLAGS) {
  231. if (*val == '+' || *val == '-')
  232. cmd = *(val++);
  233. for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  234. buf[i] = val[i];
  235. buf[i] = 0;
  236. }
  237. {
  238. int res;
  239. int ci = 0;
  240. double const_values[64];
  241. const char * const_names[64];
  242. int search_flags = (o->flags & AV_OPT_FLAG_CHILD_CONSTS) ? AV_OPT_SEARCH_CHILDREN : 0;
  243. const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, search_flags);
  244. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  245. d = DEFAULT_NUMVAL(o_named);
  246. else {
  247. if (o->unit) {
  248. for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
  249. if (o_named->type == AV_OPT_TYPE_CONST &&
  250. o_named->unit &&
  251. !strcmp(o_named->unit, o->unit)) {
  252. if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
  253. av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
  254. return AVERROR_PATCHWELCOME;
  255. }
  256. const_names [ci ] = o_named->name;
  257. const_values[ci++] = DEFAULT_NUMVAL(o_named);
  258. }
  259. }
  260. }
  261. const_names [ci ] = "default";
  262. const_values[ci++] = DEFAULT_NUMVAL(o);
  263. const_names [ci ] = "max";
  264. const_values[ci++] = o->max;
  265. const_names [ci ] = "min";
  266. const_values[ci++] = o->min;
  267. const_names [ci ] = "none";
  268. const_values[ci++] = 0;
  269. const_names [ci ] = "all";
  270. const_values[ci++] = ~0;
  271. const_names [ci] = NULL;
  272. const_values[ci] = 0;
  273. res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
  274. const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  275. if (res < 0) {
  276. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  277. return res;
  278. }
  279. }
  280. }
  281. if (o->type == AV_OPT_TYPE_FLAGS) {
  282. read_number(o, dst, NULL, NULL, &intnum);
  283. if (cmd == '+')
  284. d = intnum | (int64_t)d;
  285. else if (cmd == '-')
  286. d = intnum &~(int64_t)d;
  287. }
  288. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  289. return ret;
  290. val += i;
  291. if (!i || !*val)
  292. return 0;
  293. }
  294. }
  295. static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
  296. {
  297. int ret;
  298. if (!val || !strcmp(val, "none")) {
  299. dst[0] =
  300. dst[1] = 0;
  301. return 0;
  302. }
  303. ret = av_parse_video_size(dst, dst + 1, val);
  304. if (ret < 0)
  305. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
  306. return ret;
  307. }
  308. static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
  309. {
  310. int ret = av_parse_video_rate(dst, val);
  311. if (ret < 0)
  312. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
  313. return ret;
  314. }
  315. static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
  316. {
  317. int ret;
  318. if (!val) {
  319. return 0;
  320. } else {
  321. ret = av_parse_color(dst, val, -1, obj);
  322. if (ret < 0)
  323. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
  324. return ret;
  325. }
  326. return 0;
  327. }
  328. static const char *get_bool_name(int val)
  329. {
  330. if (val < 0)
  331. return "auto";
  332. return val ? "true" : "false";
  333. }
  334. static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
  335. {
  336. int n;
  337. if (!val)
  338. return 0;
  339. if (!strcmp(val, "auto")) {
  340. n = -1;
  341. } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
  342. n = 1;
  343. } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
  344. n = 0;
  345. } else {
  346. char *end = NULL;
  347. n = strtol(val, &end, 10);
  348. if (val + strlen(val) != end)
  349. goto fail;
  350. }
  351. if (n < o->min || n > o->max)
  352. goto fail;
  353. *dst = n;
  354. return 0;
  355. fail:
  356. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
  357. return AVERROR(EINVAL);
  358. }
  359. static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
  360. int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
  361. {
  362. int fmt, min, max;
  363. if (!val || !strcmp(val, "none")) {
  364. fmt = -1;
  365. } else {
  366. fmt = get_fmt(val);
  367. if (fmt == -1) {
  368. char *tail;
  369. fmt = strtol(val, &tail, 0);
  370. if (*tail || (unsigned)fmt >= fmt_nb) {
  371. av_log(obj, AV_LOG_ERROR,
  372. "Unable to parse option value \"%s\" as %s\n", val, desc);
  373. return AVERROR(EINVAL);
  374. }
  375. }
  376. }
  377. min = FFMAX(o->min, -1);
  378. max = FFMIN(o->max, fmt_nb-1);
  379. // hack for compatibility with old ffmpeg
  380. if(min == 0 && max == 0) {
  381. min = -1;
  382. max = fmt_nb-1;
  383. }
  384. if (fmt < min || fmt > max) {
  385. av_log(obj, AV_LOG_ERROR,
  386. "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
  387. fmt, o->name, desc, min, max);
  388. return AVERROR(ERANGE);
  389. }
  390. *(int *)dst = fmt;
  391. return 0;
  392. }
  393. static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
  394. {
  395. return set_string_fmt(obj, o, val, dst,
  396. AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
  397. }
  398. static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
  399. {
  400. return set_string_fmt(obj, o, val, dst,
  401. AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
  402. }
  403. static int set_string_dict(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  404. {
  405. AVDictionary *options = NULL;
  406. if (val) {
  407. int ret = av_dict_parse_string(&options, val, "=", ":", 0);
  408. if (ret < 0) {
  409. av_dict_free(&options);
  410. return ret;
  411. }
  412. }
  413. av_dict_free((AVDictionary **)dst);
  414. *dst = (uint8_t *)options;
  415. return 0;
  416. }
  417. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  418. {
  419. int ret = 0;
  420. void *dst, *target_obj;
  421. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  422. if (!o || !target_obj)
  423. return AVERROR_OPTION_NOT_FOUND;
  424. if (!val && (o->type != AV_OPT_TYPE_STRING &&
  425. o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
  426. o->type != AV_OPT_TYPE_IMAGE_SIZE &&
  427. o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
  428. o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL))
  429. return AVERROR(EINVAL);
  430. if (o->flags & AV_OPT_FLAG_READONLY)
  431. return AVERROR(EINVAL);
  432. if (o->flags & AV_OPT_FLAG_DEPRECATED)
  433. av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
  434. dst = ((uint8_t *)target_obj) + o->offset;
  435. switch (o->type) {
  436. case AV_OPT_TYPE_BOOL:
  437. return set_string_bool(obj, o, val, dst);
  438. case AV_OPT_TYPE_STRING:
  439. return set_string(obj, o, val, dst);
  440. case AV_OPT_TYPE_BINARY:
  441. return set_string_binary(obj, o, val, dst);
  442. case AV_OPT_TYPE_FLAGS:
  443. case AV_OPT_TYPE_INT:
  444. case AV_OPT_TYPE_INT64:
  445. case AV_OPT_TYPE_UINT64:
  446. case AV_OPT_TYPE_FLOAT:
  447. case AV_OPT_TYPE_DOUBLE:
  448. case AV_OPT_TYPE_RATIONAL:
  449. return set_string_number(obj, target_obj, o, val, dst);
  450. case AV_OPT_TYPE_IMAGE_SIZE:
  451. return set_string_image_size(obj, o, val, dst);
  452. case AV_OPT_TYPE_VIDEO_RATE: {
  453. AVRational tmp;
  454. ret = set_string_video_rate(obj, o, val, &tmp);
  455. if (ret < 0)
  456. return ret;
  457. return write_number(obj, o, dst, 1, tmp.den, tmp.num);
  458. }
  459. case AV_OPT_TYPE_PIXEL_FMT:
  460. return set_string_pixel_fmt(obj, o, val, dst);
  461. case AV_OPT_TYPE_SAMPLE_FMT:
  462. return set_string_sample_fmt(obj, o, val, dst);
  463. case AV_OPT_TYPE_DURATION:
  464. {
  465. int64_t usecs = 0;
  466. if (val) {
  467. if ((ret = av_parse_time(&usecs, val, 1)) < 0) {
  468. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
  469. return ret;
  470. }
  471. }
  472. if (usecs < o->min || usecs > o->max) {
  473. av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
  474. usecs / 1000000.0, o->name, o->min / 1000000.0, o->max / 1000000.0);
  475. return AVERROR(ERANGE);
  476. }
  477. *(int64_t *)dst = usecs;
  478. return 0;
  479. }
  480. case AV_OPT_TYPE_COLOR:
  481. return set_string_color(obj, o, val, dst);
  482. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  483. if (!val || !strcmp(val, "none")) {
  484. *(int64_t *)dst = 0;
  485. } else {
  486. int64_t cl = av_get_channel_layout(val);
  487. if (!cl) {
  488. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
  489. ret = AVERROR(EINVAL);
  490. }
  491. *(int64_t *)dst = cl;
  492. return ret;
  493. }
  494. break;
  495. case AV_OPT_TYPE_DICT:
  496. return set_string_dict(obj, o, val, dst);
  497. }
  498. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  499. return AVERROR(EINVAL);
  500. }
  501. #define OPT_EVAL_NUMBER(name, opttype, vartype) \
  502. int av_opt_eval_ ## name(void *obj, const AVOption *o, \
  503. const char *val, vartype *name ## _out) \
  504. { \
  505. if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
  506. return AVERROR(EINVAL); \
  507. return set_string_number(obj, obj, o, val, name ## _out); \
  508. }
  509. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  510. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  511. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  512. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  513. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  514. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  515. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  516. int search_flags)
  517. {
  518. void *dst, *target_obj;
  519. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  520. if (!o || !target_obj)
  521. return AVERROR_OPTION_NOT_FOUND;
  522. if (o->flags & AV_OPT_FLAG_READONLY)
  523. return AVERROR(EINVAL);
  524. dst = ((uint8_t *)target_obj) + o->offset;
  525. return write_number(obj, o, dst, num, den, intnum);
  526. }
  527. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  528. {
  529. return set_number(obj, name, 1, 1, val, search_flags);
  530. }
  531. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  532. {
  533. return set_number(obj, name, val, 1, 1, search_flags);
  534. }
  535. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  536. {
  537. return set_number(obj, name, val.num, val.den, 1, search_flags);
  538. }
  539. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  540. {
  541. void *target_obj;
  542. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  543. uint8_t *ptr;
  544. uint8_t **dst;
  545. int *lendst;
  546. if (!o || !target_obj)
  547. return AVERROR_OPTION_NOT_FOUND;
  548. if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
  549. return AVERROR(EINVAL);
  550. ptr = len ? av_malloc(len) : NULL;
  551. if (len && !ptr)
  552. return AVERROR(ENOMEM);
  553. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  554. lendst = (int *)(dst + 1);
  555. av_free(*dst);
  556. *dst = ptr;
  557. *lendst = len;
  558. if (len)
  559. memcpy(ptr, val, len);
  560. return 0;
  561. }
  562. int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
  563. {
  564. void *target_obj;
  565. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  566. if (!o || !target_obj)
  567. return AVERROR_OPTION_NOT_FOUND;
  568. if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
  569. av_log(obj, AV_LOG_ERROR,
  570. "The value set by option '%s' is not an image size.\n", o->name);
  571. return AVERROR(EINVAL);
  572. }
  573. if (w<0 || h<0) {
  574. av_log(obj, AV_LOG_ERROR,
  575. "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
  576. return AVERROR(EINVAL);
  577. }
  578. *(int *)(((uint8_t *)target_obj) + o->offset) = w;
  579. *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
  580. return 0;
  581. }
  582. int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
  583. {
  584. void *target_obj;
  585. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  586. if (!o || !target_obj)
  587. return AVERROR_OPTION_NOT_FOUND;
  588. if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
  589. av_log(obj, AV_LOG_ERROR,
  590. "The value set by option '%s' is not a video rate.\n", o->name);
  591. return AVERROR(EINVAL);
  592. }
  593. if (val.num <= 0 || val.den <= 0)
  594. return AVERROR(EINVAL);
  595. return set_number(obj, name, val.num, val.den, 1, search_flags);
  596. }
  597. static int set_format(void *obj, const char *name, int fmt, int search_flags,
  598. enum AVOptionType type, const char *desc, int nb_fmts)
  599. {
  600. void *target_obj;
  601. const AVOption *o = av_opt_find2(obj, name, NULL, 0,
  602. search_flags, &target_obj);
  603. int min, max;
  604. if (!o || !target_obj)
  605. return AVERROR_OPTION_NOT_FOUND;
  606. if (o->type != type) {
  607. av_log(obj, AV_LOG_ERROR,
  608. "The value set by option '%s' is not a %s format", name, desc);
  609. return AVERROR(EINVAL);
  610. }
  611. min = FFMAX(o->min, -1);
  612. max = FFMIN(o->max, nb_fmts-1);
  613. if (fmt < min || fmt > max) {
  614. av_log(obj, AV_LOG_ERROR,
  615. "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
  616. fmt, name, desc, min, max);
  617. return AVERROR(ERANGE);
  618. }
  619. *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
  620. return 0;
  621. }
  622. int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
  623. {
  624. return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
  625. }
  626. int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
  627. {
  628. return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
  629. }
  630. int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
  631. {
  632. void *target_obj;
  633. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  634. if (!o || !target_obj)
  635. return AVERROR_OPTION_NOT_FOUND;
  636. if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
  637. av_log(obj, AV_LOG_ERROR,
  638. "The value set by option '%s' is not a channel layout.\n", o->name);
  639. return AVERROR(EINVAL);
  640. }
  641. *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
  642. return 0;
  643. }
  644. int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
  645. int search_flags)
  646. {
  647. void *target_obj;
  648. AVDictionary **dst;
  649. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  650. if (!o || !target_obj)
  651. return AVERROR_OPTION_NOT_FOUND;
  652. if (o->flags & AV_OPT_FLAG_READONLY)
  653. return AVERROR(EINVAL);
  654. dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
  655. av_dict_free(dst);
  656. av_dict_copy(dst, val, 0);
  657. return 0;
  658. }
  659. static void format_duration(char *buf, size_t size, int64_t d)
  660. {
  661. char *e;
  662. av_assert0(size >= 25);
  663. if (d < 0 && d != INT64_MIN) {
  664. *(buf++) = '-';
  665. size--;
  666. d = -d;
  667. }
  668. if (d == INT64_MAX)
  669. snprintf(buf, size, "INT64_MAX");
  670. else if (d == INT64_MIN)
  671. snprintf(buf, size, "INT64_MIN");
  672. else if (d > (int64_t)3600*1000000)
  673. snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
  674. (int)((d / 60000000) % 60),
  675. (int)((d / 1000000) % 60),
  676. (int)(d % 1000000));
  677. else if (d > 60*1000000)
  678. snprintf(buf, size, "%d:%02d.%06d",
  679. (int)(d / 60000000),
  680. (int)((d / 1000000) % 60),
  681. (int)(d % 1000000));
  682. else
  683. snprintf(buf, size, "%d.%06d",
  684. (int)(d / 1000000),
  685. (int)(d % 1000000));
  686. e = buf + strlen(buf);
  687. while (e > buf && e[-1] == '0')
  688. *(--e) = 0;
  689. if (e > buf && e[-1] == '.')
  690. *(--e) = 0;
  691. }
  692. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  693. {
  694. void *dst, *target_obj;
  695. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  696. uint8_t *bin, buf[128];
  697. int len, i, ret;
  698. int64_t i64;
  699. if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
  700. return AVERROR_OPTION_NOT_FOUND;
  701. if (o->flags & AV_OPT_FLAG_DEPRECATED)
  702. av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
  703. dst = (uint8_t *)target_obj + o->offset;
  704. buf[0] = 0;
  705. switch (o->type) {
  706. case AV_OPT_TYPE_BOOL:
  707. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
  708. break;
  709. case AV_OPT_TYPE_FLAGS:
  710. ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
  711. break;
  712. case AV_OPT_TYPE_INT:
  713. ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
  714. break;
  715. case AV_OPT_TYPE_INT64:
  716. ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
  717. break;
  718. case AV_OPT_TYPE_UINT64:
  719. ret = snprintf(buf, sizeof(buf), "%"PRIu64, *(uint64_t *)dst);
  720. break;
  721. case AV_OPT_TYPE_FLOAT:
  722. ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
  723. break;
  724. case AV_OPT_TYPE_DOUBLE:
  725. ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
  726. break;
  727. case AV_OPT_TYPE_VIDEO_RATE:
  728. case AV_OPT_TYPE_RATIONAL:
  729. ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
  730. break;
  731. case AV_OPT_TYPE_CONST:
  732. ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
  733. break;
  734. case AV_OPT_TYPE_STRING:
  735. if (*(uint8_t **)dst) {
  736. *out_val = av_strdup(*(uint8_t **)dst);
  737. } else if (search_flags & AV_OPT_ALLOW_NULL) {
  738. *out_val = NULL;
  739. return 0;
  740. } else {
  741. *out_val = av_strdup("");
  742. }
  743. return *out_val ? 0 : AVERROR(ENOMEM);
  744. case AV_OPT_TYPE_BINARY:
  745. if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
  746. *out_val = NULL;
  747. return 0;
  748. }
  749. len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
  750. if ((uint64_t)len * 2 + 1 > INT_MAX)
  751. return AVERROR(EINVAL);
  752. if (!(*out_val = av_malloc(len * 2 + 1)))
  753. return AVERROR(ENOMEM);
  754. if (!len) {
  755. *out_val[0] = '\0';
  756. return 0;
  757. }
  758. bin = *(uint8_t **)dst;
  759. for (i = 0; i < len; i++)
  760. snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
  761. return 0;
  762. case AV_OPT_TYPE_IMAGE_SIZE:
  763. ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
  764. break;
  765. case AV_OPT_TYPE_PIXEL_FMT:
  766. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
  767. break;
  768. case AV_OPT_TYPE_SAMPLE_FMT:
  769. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
  770. break;
  771. case AV_OPT_TYPE_DURATION:
  772. i64 = *(int64_t *)dst;
  773. format_duration(buf, sizeof(buf), i64);
  774. ret = strlen(buf); // no overflow possible, checked by an assert
  775. break;
  776. case AV_OPT_TYPE_COLOR:
  777. ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
  778. (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
  779. (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
  780. break;
  781. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  782. i64 = *(int64_t *)dst;
  783. ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
  784. break;
  785. case AV_OPT_TYPE_DICT:
  786. if (!*(AVDictionary **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
  787. *out_val = NULL;
  788. return 0;
  789. }
  790. return av_dict_get_string(*(AVDictionary **)dst, (char **)out_val, '=', ':');
  791. default:
  792. return AVERROR(EINVAL);
  793. }
  794. if (ret >= sizeof(buf))
  795. return AVERROR(EINVAL);
  796. *out_val = av_strdup(buf);
  797. return *out_val ? 0 : AVERROR(ENOMEM);
  798. }
  799. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  800. int search_flags)
  801. {
  802. void *dst, *target_obj;
  803. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  804. if (!o || !target_obj)
  805. goto error;
  806. dst = ((uint8_t *)target_obj) + o->offset;
  807. if (o_out) *o_out= o;
  808. return read_number(o, dst, num, den, intnum);
  809. error:
  810. *den =
  811. *intnum = 0;
  812. return -1;
  813. }
  814. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  815. {
  816. int64_t intnum = 1;
  817. double num = 1;
  818. int ret, den = 1;
  819. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  820. return ret;
  821. *out_val = num * intnum / den;
  822. return 0;
  823. }
  824. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  825. {
  826. int64_t intnum = 1;
  827. double num = 1;
  828. int ret, den = 1;
  829. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  830. return ret;
  831. *out_val = num * intnum / den;
  832. return 0;
  833. }
  834. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  835. {
  836. int64_t intnum = 1;
  837. double num = 1;
  838. int ret, den = 1;
  839. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  840. return ret;
  841. if (num == 1.0 && (int)intnum == intnum)
  842. *out_val = (AVRational){intnum, den};
  843. else
  844. *out_val = av_d2q(num*intnum/den, 1<<24);
  845. return 0;
  846. }
  847. int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
  848. {
  849. void *dst, *target_obj;
  850. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  851. if (!o || !target_obj)
  852. return AVERROR_OPTION_NOT_FOUND;
  853. if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
  854. av_log(obj, AV_LOG_ERROR,
  855. "The value for option '%s' is not an image size.\n", name);
  856. return AVERROR(EINVAL);
  857. }
  858. dst = ((uint8_t*)target_obj) + o->offset;
  859. if (w_out) *w_out = *(int *)dst;
  860. if (h_out) *h_out = *((int *)dst+1);
  861. return 0;
  862. }
  863. int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
  864. {
  865. int64_t intnum = 1;
  866. double num = 1;
  867. int ret, den = 1;
  868. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  869. return ret;
  870. if (num == 1.0 && (int)intnum == intnum)
  871. *out_val = (AVRational) { intnum, den };
  872. else
  873. *out_val = av_d2q(num * intnum / den, 1 << 24);
  874. return 0;
  875. }
  876. static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
  877. enum AVOptionType type, const char *desc)
  878. {
  879. void *dst, *target_obj;
  880. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  881. if (!o || !target_obj)
  882. return AVERROR_OPTION_NOT_FOUND;
  883. if (o->type != type) {
  884. av_log(obj, AV_LOG_ERROR,
  885. "The value for option '%s' is not a %s format.\n", desc, name);
  886. return AVERROR(EINVAL);
  887. }
  888. dst = ((uint8_t*)target_obj) + o->offset;
  889. *out_fmt = *(int *)dst;
  890. return 0;
  891. }
  892. int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
  893. {
  894. return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
  895. }
  896. int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
  897. {
  898. return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
  899. }
  900. int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
  901. {
  902. void *dst, *target_obj;
  903. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  904. if (!o || !target_obj)
  905. return AVERROR_OPTION_NOT_FOUND;
  906. if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
  907. av_log(obj, AV_LOG_ERROR,
  908. "The value for option '%s' is not a channel layout.\n", name);
  909. return AVERROR(EINVAL);
  910. }
  911. dst = ((uint8_t*)target_obj) + o->offset;
  912. *cl = *(int64_t *)dst;
  913. return 0;
  914. }
  915. int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
  916. {
  917. void *target_obj;
  918. AVDictionary *src;
  919. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  920. if (!o || !target_obj)
  921. return AVERROR_OPTION_NOT_FOUND;
  922. if (o->type != AV_OPT_TYPE_DICT)
  923. return AVERROR(EINVAL);
  924. src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
  925. av_dict_copy(out_val, src, 0);
  926. return 0;
  927. }
  928. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  929. {
  930. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  931. const AVOption *flag = av_opt_find(obj, flag_name,
  932. field ? field->unit : NULL, 0, 0);
  933. int64_t res;
  934. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  935. av_opt_get_int(obj, field_name, 0, &res) < 0)
  936. return 0;
  937. return res & flag->default_val.i64;
  938. }
  939. static void log_int_value(void *av_log_obj, int level, int64_t i)
  940. {
  941. if (i == INT_MAX) {
  942. av_log(av_log_obj, level, "INT_MAX");
  943. } else if (i == INT_MIN) {
  944. av_log(av_log_obj, level, "INT_MIN");
  945. } else if (i == UINT32_MAX) {
  946. av_log(av_log_obj, level, "UINT32_MAX");
  947. } else if (i == INT64_MAX) {
  948. av_log(av_log_obj, level, "I64_MAX");
  949. } else if (i == INT64_MIN) {
  950. av_log(av_log_obj, level, "I64_MIN");
  951. } else {
  952. av_log(av_log_obj, level, "%"PRId64, i);
  953. }
  954. }
  955. static void log_value(void *av_log_obj, int level, double d)
  956. {
  957. if (d == INT_MAX) {
  958. av_log(av_log_obj, level, "INT_MAX");
  959. } else if (d == INT_MIN) {
  960. av_log(av_log_obj, level, "INT_MIN");
  961. } else if (d == UINT32_MAX) {
  962. av_log(av_log_obj, level, "UINT32_MAX");
  963. } else if (d == (double)INT64_MAX) {
  964. av_log(av_log_obj, level, "I64_MAX");
  965. } else if (d == INT64_MIN) {
  966. av_log(av_log_obj, level, "I64_MIN");
  967. } else if (d == FLT_MAX) {
  968. av_log(av_log_obj, level, "FLT_MAX");
  969. } else if (d == FLT_MIN) {
  970. av_log(av_log_obj, level, "FLT_MIN");
  971. } else if (d == -FLT_MAX) {
  972. av_log(av_log_obj, level, "-FLT_MAX");
  973. } else if (d == -FLT_MIN) {
  974. av_log(av_log_obj, level, "-FLT_MIN");
  975. } else if (d == DBL_MAX) {
  976. av_log(av_log_obj, level, "DBL_MAX");
  977. } else if (d == DBL_MIN) {
  978. av_log(av_log_obj, level, "DBL_MIN");
  979. } else if (d == -DBL_MAX) {
  980. av_log(av_log_obj, level, "-DBL_MAX");
  981. } else if (d == -DBL_MIN) {
  982. av_log(av_log_obj, level, "-DBL_MIN");
  983. } else {
  984. av_log(av_log_obj, level, "%g", d);
  985. }
  986. }
  987. static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
  988. {
  989. const AVOption *opt = NULL;
  990. if (!unit)
  991. return NULL;
  992. while ((opt = av_opt_next(obj, opt)))
  993. if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
  994. opt->default_val.i64 == value)
  995. return opt->name;
  996. return NULL;
  997. }
  998. static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
  999. {
  1000. const AVOption *opt = NULL;
  1001. char flags[512];
  1002. flags[0] = 0;
  1003. if (!unit)
  1004. return NULL;
  1005. while ((opt = av_opt_next(obj, opt))) {
  1006. if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
  1007. opt->default_val.i64 & value) {
  1008. if (flags[0])
  1009. av_strlcatf(flags, sizeof(flags), "+");
  1010. av_strlcatf(flags, sizeof(flags), "%s", opt->name);
  1011. }
  1012. }
  1013. if (flags[0])
  1014. return av_strdup(flags);
  1015. return NULL;
  1016. }
  1017. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  1018. int req_flags, int rej_flags, enum AVOptionType parent_type)
  1019. {
  1020. const AVOption *opt = NULL;
  1021. AVOptionRanges *r;
  1022. int i;
  1023. while ((opt = av_opt_next(obj, opt))) {
  1024. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  1025. continue;
  1026. /* Don't print CONST's on level one.
  1027. * Don't print anything but CONST's on level two.
  1028. * Only print items from the requested unit.
  1029. */
  1030. if (!unit && opt->type == AV_OPT_TYPE_CONST)
  1031. continue;
  1032. else if (unit && opt->type != AV_OPT_TYPE_CONST)
  1033. continue;
  1034. else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  1035. continue;
  1036. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  1037. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  1038. else
  1039. av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
  1040. (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
  1041. opt->name);
  1042. switch (opt->type) {
  1043. case AV_OPT_TYPE_FLAGS:
  1044. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
  1045. break;
  1046. case AV_OPT_TYPE_INT:
  1047. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
  1048. break;
  1049. case AV_OPT_TYPE_INT64:
  1050. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
  1051. break;
  1052. case AV_OPT_TYPE_UINT64:
  1053. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<uint64>");
  1054. break;
  1055. case AV_OPT_TYPE_DOUBLE:
  1056. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
  1057. break;
  1058. case AV_OPT_TYPE_FLOAT:
  1059. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
  1060. break;
  1061. case AV_OPT_TYPE_STRING:
  1062. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
  1063. break;
  1064. case AV_OPT_TYPE_RATIONAL:
  1065. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
  1066. break;
  1067. case AV_OPT_TYPE_BINARY:
  1068. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
  1069. break;
  1070. case AV_OPT_TYPE_DICT:
  1071. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<dictionary>");
  1072. break;
  1073. case AV_OPT_TYPE_IMAGE_SIZE:
  1074. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
  1075. break;
  1076. case AV_OPT_TYPE_VIDEO_RATE:
  1077. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
  1078. break;
  1079. case AV_OPT_TYPE_PIXEL_FMT:
  1080. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
  1081. break;
  1082. case AV_OPT_TYPE_SAMPLE_FMT:
  1083. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
  1084. break;
  1085. case AV_OPT_TYPE_DURATION:
  1086. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
  1087. break;
  1088. case AV_OPT_TYPE_COLOR:
  1089. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
  1090. break;
  1091. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1092. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
  1093. break;
  1094. case AV_OPT_TYPE_BOOL:
  1095. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
  1096. break;
  1097. case AV_OPT_TYPE_CONST:
  1098. if (parent_type == AV_OPT_TYPE_INT)
  1099. av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", opt->default_val.i64);
  1100. else
  1101. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
  1102. break;
  1103. default:
  1104. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
  1105. break;
  1106. }
  1107. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  1108. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  1109. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
  1110. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  1111. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  1112. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  1113. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
  1114. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
  1115. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_BSF_PARAM) ? 'B' : '.');
  1116. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_RUNTIME_PARAM) ? 'T' : '.');
  1117. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DEPRECATED) ? 'P' : '.');
  1118. if (opt->help)
  1119. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  1120. if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
  1121. switch (opt->type) {
  1122. case AV_OPT_TYPE_INT:
  1123. case AV_OPT_TYPE_INT64:
  1124. case AV_OPT_TYPE_UINT64:
  1125. case AV_OPT_TYPE_DOUBLE:
  1126. case AV_OPT_TYPE_FLOAT:
  1127. case AV_OPT_TYPE_RATIONAL:
  1128. for (i = 0; i < r->nb_ranges; i++) {
  1129. av_log(av_log_obj, AV_LOG_INFO, " (from ");
  1130. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
  1131. av_log(av_log_obj, AV_LOG_INFO, " to ");
  1132. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
  1133. av_log(av_log_obj, AV_LOG_INFO, ")");
  1134. }
  1135. break;
  1136. }
  1137. av_opt_freep_ranges(&r);
  1138. }
  1139. if (opt->type != AV_OPT_TYPE_CONST &&
  1140. opt->type != AV_OPT_TYPE_BINARY &&
  1141. !((opt->type == AV_OPT_TYPE_COLOR ||
  1142. opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
  1143. opt->type == AV_OPT_TYPE_STRING ||
  1144. opt->type == AV_OPT_TYPE_DICT ||
  1145. opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
  1146. !opt->default_val.str)) {
  1147. av_log(av_log_obj, AV_LOG_INFO, " (default ");
  1148. switch (opt->type) {
  1149. case AV_OPT_TYPE_BOOL:
  1150. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
  1151. break;
  1152. case AV_OPT_TYPE_FLAGS: {
  1153. char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
  1154. if (def_flags) {
  1155. av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
  1156. av_freep(&def_flags);
  1157. } else {
  1158. av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
  1159. }
  1160. break;
  1161. }
  1162. case AV_OPT_TYPE_DURATION: {
  1163. char buf[25];
  1164. format_duration(buf, sizeof(buf), opt->default_val.i64);
  1165. av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
  1166. break;
  1167. }
  1168. case AV_OPT_TYPE_INT:
  1169. case AV_OPT_TYPE_UINT64:
  1170. case AV_OPT_TYPE_INT64: {
  1171. const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
  1172. if (def_const)
  1173. av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
  1174. else
  1175. log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
  1176. break;
  1177. }
  1178. case AV_OPT_TYPE_DOUBLE:
  1179. case AV_OPT_TYPE_FLOAT:
  1180. log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
  1181. break;
  1182. case AV_OPT_TYPE_RATIONAL: {
  1183. AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
  1184. av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
  1185. break;
  1186. case AV_OPT_TYPE_PIXEL_FMT:
  1187. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
  1188. break;
  1189. case AV_OPT_TYPE_SAMPLE_FMT:
  1190. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
  1191. break;
  1192. case AV_OPT_TYPE_COLOR:
  1193. case AV_OPT_TYPE_IMAGE_SIZE:
  1194. case AV_OPT_TYPE_STRING:
  1195. case AV_OPT_TYPE_DICT:
  1196. case AV_OPT_TYPE_VIDEO_RATE:
  1197. av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
  1198. break;
  1199. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1200. av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
  1201. break;
  1202. }
  1203. av_log(av_log_obj, AV_LOG_INFO, ")");
  1204. }
  1205. av_log(av_log_obj, AV_LOG_INFO, "\n");
  1206. if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
  1207. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags, opt->type);
  1208. }
  1209. }
  1210. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  1211. {
  1212. if (!obj)
  1213. return -1;
  1214. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
  1215. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags, -1);
  1216. return 0;
  1217. }
  1218. void av_opt_set_defaults(void *s)
  1219. {
  1220. av_opt_set_defaults2(s, 0, 0);
  1221. }
  1222. void av_opt_set_defaults2(void *s, int mask, int flags)
  1223. {
  1224. const AVOption *opt = NULL;
  1225. while ((opt = av_opt_next(s, opt))) {
  1226. void *dst = ((uint8_t*)s) + opt->offset;
  1227. if ((opt->flags & mask) != flags)
  1228. continue;
  1229. if (opt->flags & AV_OPT_FLAG_READONLY)
  1230. continue;
  1231. switch (opt->type) {
  1232. case AV_OPT_TYPE_CONST:
  1233. /* Nothing to be done here */
  1234. break;
  1235. case AV_OPT_TYPE_BOOL:
  1236. case AV_OPT_TYPE_FLAGS:
  1237. case AV_OPT_TYPE_INT:
  1238. case AV_OPT_TYPE_INT64:
  1239. case AV_OPT_TYPE_UINT64:
  1240. case AV_OPT_TYPE_DURATION:
  1241. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1242. case AV_OPT_TYPE_PIXEL_FMT:
  1243. case AV_OPT_TYPE_SAMPLE_FMT:
  1244. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1245. break;
  1246. case AV_OPT_TYPE_DOUBLE:
  1247. case AV_OPT_TYPE_FLOAT: {
  1248. double val;
  1249. val = opt->default_val.dbl;
  1250. write_number(s, opt, dst, val, 1, 1);
  1251. }
  1252. break;
  1253. case AV_OPT_TYPE_RATIONAL: {
  1254. AVRational val;
  1255. val = av_d2q(opt->default_val.dbl, INT_MAX);
  1256. write_number(s, opt, dst, 1, val.den, val.num);
  1257. }
  1258. break;
  1259. case AV_OPT_TYPE_COLOR:
  1260. set_string_color(s, opt, opt->default_val.str, dst);
  1261. break;
  1262. case AV_OPT_TYPE_STRING:
  1263. set_string(s, opt, opt->default_val.str, dst);
  1264. break;
  1265. case AV_OPT_TYPE_IMAGE_SIZE:
  1266. set_string_image_size(s, opt, opt->default_val.str, dst);
  1267. break;
  1268. case AV_OPT_TYPE_VIDEO_RATE:
  1269. set_string_video_rate(s, opt, opt->default_val.str, dst);
  1270. break;
  1271. case AV_OPT_TYPE_BINARY:
  1272. set_string_binary(s, opt, opt->default_val.str, dst);
  1273. break;
  1274. case AV_OPT_TYPE_DICT:
  1275. set_string_dict(s, opt, opt->default_val.str, dst);
  1276. break;
  1277. default:
  1278. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
  1279. opt->type, opt->name);
  1280. }
  1281. }
  1282. }
  1283. /**
  1284. * Store the value in the field in ctx that is named like key.
  1285. * ctx must be an AVClass context, storing is done using AVOptions.
  1286. *
  1287. * @param buf the string to parse, buf will be updated to point at the
  1288. * separator just after the parsed key/value pair
  1289. * @param key_val_sep a 0-terminated list of characters used to
  1290. * separate key from value
  1291. * @param pairs_sep a 0-terminated list of characters used to separate
  1292. * two pairs from each other
  1293. * @return 0 if the key/value pair has been successfully parsed and
  1294. * set, or a negative value corresponding to an AVERROR code in case
  1295. * of error:
  1296. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  1297. * the error code issued by av_opt_set() if the key/value pair
  1298. * cannot be set
  1299. */
  1300. static int parse_key_value_pair(void *ctx, const char **buf,
  1301. const char *key_val_sep, const char *pairs_sep)
  1302. {
  1303. char *key = av_get_token(buf, key_val_sep);
  1304. char *val;
  1305. int ret;
  1306. if (!key)
  1307. return AVERROR(ENOMEM);
  1308. if (*key && strspn(*buf, key_val_sep)) {
  1309. (*buf)++;
  1310. val = av_get_token(buf, pairs_sep);
  1311. if (!val) {
  1312. av_freep(&key);
  1313. return AVERROR(ENOMEM);
  1314. }
  1315. } else {
  1316. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  1317. av_free(key);
  1318. return AVERROR(EINVAL);
  1319. }
  1320. av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
  1321. ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
  1322. if (ret == AVERROR_OPTION_NOT_FOUND)
  1323. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  1324. av_free(key);
  1325. av_free(val);
  1326. return ret;
  1327. }
  1328. int av_set_options_string(void *ctx, const char *opts,
  1329. const char *key_val_sep, const char *pairs_sep)
  1330. {
  1331. int ret, count = 0;
  1332. if (!opts)
  1333. return 0;
  1334. while (*opts) {
  1335. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  1336. return ret;
  1337. count++;
  1338. if (*opts)
  1339. opts++;
  1340. }
  1341. return count;
  1342. }
  1343. #define WHITESPACES " \n\t\r"
  1344. static int is_key_char(char c)
  1345. {
  1346. return (unsigned)((c | 32) - 'a') < 26 ||
  1347. (unsigned)(c - '0') < 10 ||
  1348. c == '-' || c == '_' || c == '/' || c == '.';
  1349. }
  1350. /**
  1351. * Read a key from a string.
  1352. *
  1353. * The key consists of is_key_char characters and must be terminated by a
  1354. * character from the delim string; spaces are ignored.
  1355. *
  1356. * @return 0 for success (even with ellipsis), <0 for failure
  1357. */
  1358. static int get_key(const char **ropts, const char *delim, char **rkey)
  1359. {
  1360. const char *opts = *ropts;
  1361. const char *key_start, *key_end;
  1362. key_start = opts += strspn(opts, WHITESPACES);
  1363. while (is_key_char(*opts))
  1364. opts++;
  1365. key_end = opts;
  1366. opts += strspn(opts, WHITESPACES);
  1367. if (!*opts || !strchr(delim, *opts))
  1368. return AVERROR(EINVAL);
  1369. opts++;
  1370. if (!(*rkey = av_malloc(key_end - key_start + 1)))
  1371. return AVERROR(ENOMEM);
  1372. memcpy(*rkey, key_start, key_end - key_start);
  1373. (*rkey)[key_end - key_start] = 0;
  1374. *ropts = opts;
  1375. return 0;
  1376. }
  1377. int av_opt_get_key_value(const char **ropts,
  1378. const char *key_val_sep, const char *pairs_sep,
  1379. unsigned flags,
  1380. char **rkey, char **rval)
  1381. {
  1382. int ret;
  1383. char *key = NULL, *val;
  1384. const char *opts = *ropts;
  1385. if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
  1386. !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
  1387. return AVERROR(EINVAL);
  1388. if (!(val = av_get_token(&opts, pairs_sep))) {
  1389. av_free(key);
  1390. return AVERROR(ENOMEM);
  1391. }
  1392. *ropts = opts;
  1393. *rkey = key;
  1394. *rval = val;
  1395. return 0;
  1396. }
  1397. int av_opt_set_from_string(void *ctx, const char *opts,
  1398. const char *const *shorthand,
  1399. const char *key_val_sep, const char *pairs_sep)
  1400. {
  1401. int ret, count = 0;
  1402. const char *dummy_shorthand = NULL;
  1403. char *av_uninit(parsed_key), *av_uninit(value);
  1404. const char *key;
  1405. if (!opts)
  1406. return 0;
  1407. if (!shorthand)
  1408. shorthand = &dummy_shorthand;
  1409. while (*opts) {
  1410. ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
  1411. *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  1412. &parsed_key, &value);
  1413. if (ret < 0) {
  1414. if (ret == AVERROR(EINVAL))
  1415. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
  1416. else
  1417. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
  1418. av_err2str(ret));
  1419. return ret;
  1420. }
  1421. if (*opts)
  1422. opts++;
  1423. if (parsed_key) {
  1424. key = parsed_key;
  1425. while (*shorthand) /* discard all remaining shorthand */
  1426. shorthand++;
  1427. } else {
  1428. key = *(shorthand++);
  1429. }
  1430. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  1431. if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
  1432. if (ret == AVERROR_OPTION_NOT_FOUND)
  1433. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  1434. av_free(value);
  1435. av_free(parsed_key);
  1436. return ret;
  1437. }
  1438. av_free(value);
  1439. av_free(parsed_key);
  1440. count++;
  1441. }
  1442. return count;
  1443. }
  1444. void av_opt_free(void *obj)
  1445. {
  1446. const AVOption *o = NULL;
  1447. while ((o = av_opt_next(obj, o))) {
  1448. switch (o->type) {
  1449. case AV_OPT_TYPE_STRING:
  1450. case AV_OPT_TYPE_BINARY:
  1451. av_freep((uint8_t *)obj + o->offset);
  1452. break;
  1453. case AV_OPT_TYPE_DICT:
  1454. av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
  1455. break;
  1456. default:
  1457. break;
  1458. }
  1459. }
  1460. }
  1461. int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
  1462. {
  1463. AVDictionaryEntry *t = NULL;
  1464. AVDictionary *tmp = NULL;
  1465. int ret = 0;
  1466. if (!options)
  1467. return 0;
  1468. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  1469. ret = av_opt_set(obj, t->key, t->value, search_flags);
  1470. if (ret == AVERROR_OPTION_NOT_FOUND)
  1471. ret = av_dict_set(&tmp, t->key, t->value, 0);
  1472. if (ret < 0) {
  1473. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  1474. av_dict_free(&tmp);
  1475. return ret;
  1476. }
  1477. ret = 0;
  1478. }
  1479. av_dict_free(options);
  1480. *options = tmp;
  1481. return ret;
  1482. }
  1483. int av_opt_set_dict(void *obj, AVDictionary **options)
  1484. {
  1485. return av_opt_set_dict2(obj, options, 0);
  1486. }
  1487. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  1488. int opt_flags, int search_flags)
  1489. {
  1490. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  1491. }
  1492. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  1493. int opt_flags, int search_flags, void **target_obj)
  1494. {
  1495. const AVClass *c;
  1496. const AVOption *o = NULL;
  1497. if(!obj)
  1498. return NULL;
  1499. c= *(AVClass**)obj;
  1500. if (!c)
  1501. return NULL;
  1502. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  1503. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  1504. void *iter = NULL;
  1505. const AVClass *child;
  1506. while (child = av_opt_child_class_iterate(c, &iter))
  1507. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  1508. return o;
  1509. } else {
  1510. void *child = NULL;
  1511. while (child = av_opt_child_next(obj, child))
  1512. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  1513. return o;
  1514. }
  1515. }
  1516. while (o = av_opt_next(obj, o)) {
  1517. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  1518. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  1519. (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
  1520. if (target_obj) {
  1521. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  1522. *target_obj = obj;
  1523. else
  1524. *target_obj = NULL;
  1525. }
  1526. return o;
  1527. }
  1528. }
  1529. return NULL;
  1530. }
  1531. void *av_opt_child_next(void *obj, void *prev)
  1532. {
  1533. const AVClass *c = *(AVClass **)obj;
  1534. if (c->child_next)
  1535. return c->child_next(obj, prev);
  1536. return NULL;
  1537. }
  1538. #if FF_API_CHILD_CLASS_NEXT
  1539. FF_DISABLE_DEPRECATION_WARNINGS
  1540. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  1541. {
  1542. if (parent->child_class_next)
  1543. return parent->child_class_next(prev);
  1544. return NULL;
  1545. }
  1546. FF_ENABLE_DEPRECATION_WARNINGS
  1547. #endif
  1548. const AVClass *av_opt_child_class_iterate(const AVClass *parent, void **iter)
  1549. {
  1550. if (parent->child_class_iterate)
  1551. return parent->child_class_iterate(iter);
  1552. #if FF_API_CHILD_CLASS_NEXT
  1553. FF_DISABLE_DEPRECATION_WARNINGS
  1554. if (parent->child_class_next) {
  1555. *iter = parent->child_class_next(*iter);
  1556. return *iter;
  1557. }
  1558. FF_ENABLE_DEPRECATION_WARNINGS
  1559. #endif
  1560. return NULL;
  1561. }
  1562. void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
  1563. {
  1564. const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
  1565. if(!opt)
  1566. return NULL;
  1567. return (uint8_t*)obj + opt->offset;
  1568. }
  1569. static int opt_size(enum AVOptionType type)
  1570. {
  1571. switch(type) {
  1572. case AV_OPT_TYPE_BOOL:
  1573. case AV_OPT_TYPE_INT:
  1574. case AV_OPT_TYPE_FLAGS:
  1575. return sizeof(int);
  1576. case AV_OPT_TYPE_DURATION:
  1577. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1578. case AV_OPT_TYPE_INT64:
  1579. case AV_OPT_TYPE_UINT64:
  1580. return sizeof(int64_t);
  1581. case AV_OPT_TYPE_DOUBLE:
  1582. return sizeof(double);
  1583. case AV_OPT_TYPE_FLOAT:
  1584. return sizeof(float);
  1585. case AV_OPT_TYPE_STRING:
  1586. return sizeof(uint8_t*);
  1587. case AV_OPT_TYPE_VIDEO_RATE:
  1588. case AV_OPT_TYPE_RATIONAL:
  1589. return sizeof(AVRational);
  1590. case AV_OPT_TYPE_BINARY:
  1591. return sizeof(uint8_t*) + sizeof(int);
  1592. case AV_OPT_TYPE_IMAGE_SIZE:
  1593. return sizeof(int[2]);
  1594. case AV_OPT_TYPE_PIXEL_FMT:
  1595. return sizeof(enum AVPixelFormat);
  1596. case AV_OPT_TYPE_SAMPLE_FMT:
  1597. return sizeof(enum AVSampleFormat);
  1598. case AV_OPT_TYPE_COLOR:
  1599. return 4;
  1600. }
  1601. return AVERROR(EINVAL);
  1602. }
  1603. int av_opt_copy(void *dst, const void *src)
  1604. {
  1605. const AVOption *o = NULL;
  1606. const AVClass *c;
  1607. int ret = 0;
  1608. if (!src)
  1609. return AVERROR(EINVAL);
  1610. c = *(AVClass **)src;
  1611. if (!c || c != *(AVClass **)dst)
  1612. return AVERROR(EINVAL);
  1613. while ((o = av_opt_next(src, o))) {
  1614. void *field_dst = (uint8_t *)dst + o->offset;
  1615. void *field_src = (uint8_t *)src + o->offset;
  1616. uint8_t **field_dst8 = (uint8_t **)field_dst;
  1617. uint8_t **field_src8 = (uint8_t **)field_src;
  1618. if (o->type == AV_OPT_TYPE_STRING) {
  1619. if (*field_dst8 != *field_src8)
  1620. av_freep(field_dst8);
  1621. *field_dst8 = av_strdup(*field_src8);
  1622. if (*field_src8 && !*field_dst8)
  1623. ret = AVERROR(ENOMEM);
  1624. } else if (o->type == AV_OPT_TYPE_BINARY) {
  1625. int len = *(int *)(field_src8 + 1);
  1626. if (*field_dst8 != *field_src8)
  1627. av_freep(field_dst8);
  1628. *field_dst8 = av_memdup(*field_src8, len);
  1629. if (len && !*field_dst8) {
  1630. ret = AVERROR(ENOMEM);
  1631. len = 0;
  1632. }
  1633. *(int *)(field_dst8 + 1) = len;
  1634. } else if (o->type == AV_OPT_TYPE_CONST) {
  1635. // do nothing
  1636. } else if (o->type == AV_OPT_TYPE_DICT) {
  1637. AVDictionary **sdict = (AVDictionary **) field_src;
  1638. AVDictionary **ddict = (AVDictionary **) field_dst;
  1639. if (*sdict != *ddict)
  1640. av_dict_free(ddict);
  1641. *ddict = NULL;
  1642. av_dict_copy(ddict, *sdict, 0);
  1643. if (av_dict_count(*sdict) != av_dict_count(*ddict))
  1644. ret = AVERROR(ENOMEM);
  1645. } else {
  1646. int size = opt_size(o->type);
  1647. if (size < 0)
  1648. ret = size;
  1649. else
  1650. memcpy(field_dst, field_src, size);
  1651. }
  1652. }
  1653. return ret;
  1654. }
  1655. int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1656. {
  1657. int ret;
  1658. const AVClass *c = *(AVClass**)obj;
  1659. int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
  1660. if (c->version > (52 << 16 | 11 << 8))
  1661. callback = c->query_ranges;
  1662. if (!callback)
  1663. callback = av_opt_query_ranges_default;
  1664. ret = callback(ranges_arg, obj, key, flags);
  1665. if (ret >= 0) {
  1666. if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
  1667. ret = 1;
  1668. (*ranges_arg)->nb_components = ret;
  1669. }
  1670. return ret;
  1671. }
  1672. int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1673. {
  1674. AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
  1675. AVOptionRange **range_array = av_mallocz(sizeof(void*));
  1676. AVOptionRange *range = av_mallocz(sizeof(*range));
  1677. const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
  1678. int ret;
  1679. *ranges_arg = NULL;
  1680. if (!ranges || !range || !range_array || !field) {
  1681. ret = AVERROR(ENOMEM);
  1682. goto fail;
  1683. }
  1684. ranges->range = range_array;
  1685. ranges->range[0] = range;
  1686. ranges->nb_ranges = 1;
  1687. ranges->nb_components = 1;
  1688. range->is_range = 1;
  1689. range->value_min = field->min;
  1690. range->value_max = field->max;
  1691. switch (field->type) {
  1692. case AV_OPT_TYPE_BOOL:
  1693. case AV_OPT_TYPE_INT:
  1694. case AV_OPT_TYPE_INT64:
  1695. case AV_OPT_TYPE_UINT64:
  1696. case AV_OPT_TYPE_PIXEL_FMT:
  1697. case AV_OPT_TYPE_SAMPLE_FMT:
  1698. case AV_OPT_TYPE_FLOAT:
  1699. case AV_OPT_TYPE_DOUBLE:
  1700. case AV_OPT_TYPE_DURATION:
  1701. case AV_OPT_TYPE_COLOR:
  1702. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1703. break;
  1704. case AV_OPT_TYPE_STRING:
  1705. range->component_min = 0;
  1706. range->component_max = 0x10FFFF; // max unicode value
  1707. range->value_min = -1;
  1708. range->value_max = INT_MAX;
  1709. break;
  1710. case AV_OPT_TYPE_RATIONAL:
  1711. range->component_min = INT_MIN;
  1712. range->component_max = INT_MAX;
  1713. break;
  1714. case AV_OPT_TYPE_IMAGE_SIZE:
  1715. range->component_min = 0;
  1716. range->component_max = INT_MAX/128/8;
  1717. range->value_min = 0;
  1718. range->value_max = INT_MAX/8;
  1719. break;
  1720. case AV_OPT_TYPE_VIDEO_RATE:
  1721. range->component_min = 1;
  1722. range->component_max = INT_MAX;
  1723. range->value_min = 1;
  1724. range->value_max = INT_MAX;
  1725. break;
  1726. default:
  1727. ret = AVERROR(ENOSYS);
  1728. goto fail;
  1729. }
  1730. *ranges_arg = ranges;
  1731. return 1;
  1732. fail:
  1733. av_free(ranges);
  1734. av_free(range);
  1735. av_free(range_array);
  1736. return ret;
  1737. }
  1738. void av_opt_freep_ranges(AVOptionRanges **rangesp)
  1739. {
  1740. int i;
  1741. AVOptionRanges *ranges = *rangesp;
  1742. if (!ranges)
  1743. return;
  1744. for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
  1745. AVOptionRange *range = ranges->range[i];
  1746. if (range) {
  1747. av_freep(&range->str);
  1748. av_freep(&ranges->range[i]);
  1749. }
  1750. }
  1751. av_freep(&ranges->range);
  1752. av_freep(rangesp);
  1753. }
  1754. int av_opt_is_set_to_default(void *obj, const AVOption *o)
  1755. {
  1756. int64_t i64;
  1757. double d, d2;
  1758. float f;
  1759. AVRational q;
  1760. int ret, w, h;
  1761. char *str;
  1762. void *dst;
  1763. if (!o || !obj)
  1764. return AVERROR(EINVAL);
  1765. dst = ((uint8_t*)obj) + o->offset;
  1766. switch (o->type) {
  1767. case AV_OPT_TYPE_CONST:
  1768. return 1;
  1769. case AV_OPT_TYPE_BOOL:
  1770. case AV_OPT_TYPE_FLAGS:
  1771. case AV_OPT_TYPE_PIXEL_FMT:
  1772. case AV_OPT_TYPE_SAMPLE_FMT:
  1773. case AV_OPT_TYPE_INT:
  1774. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1775. case AV_OPT_TYPE_DURATION:
  1776. case AV_OPT_TYPE_INT64:
  1777. case AV_OPT_TYPE_UINT64:
  1778. read_number(o, dst, NULL, NULL, &i64);
  1779. return o->default_val.i64 == i64;
  1780. case AV_OPT_TYPE_STRING:
  1781. str = *(char **)dst;
  1782. if (str == o->default_val.str) //2 NULLs
  1783. return 1;
  1784. if (!str || !o->default_val.str) //1 NULL
  1785. return 0;
  1786. return !strcmp(str, o->default_val.str);
  1787. case AV_OPT_TYPE_DOUBLE:
  1788. read_number(o, dst, &d, NULL, NULL);
  1789. return o->default_val.dbl == d;
  1790. case AV_OPT_TYPE_FLOAT:
  1791. read_number(o, dst, &d, NULL, NULL);
  1792. f = o->default_val.dbl;
  1793. d2 = f;
  1794. return d2 == d;
  1795. case AV_OPT_TYPE_RATIONAL:
  1796. q = av_d2q(o->default_val.dbl, INT_MAX);
  1797. return !av_cmp_q(*(AVRational*)dst, q);
  1798. case AV_OPT_TYPE_BINARY: {
  1799. struct {
  1800. uint8_t *data;
  1801. int size;
  1802. } tmp = {0};
  1803. int opt_size = *(int *)((void **)dst + 1);
  1804. void *opt_ptr = *(void **)dst;
  1805. if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
  1806. return 1;
  1807. if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
  1808. return 0;
  1809. if (opt_size != strlen(o->default_val.str) / 2)
  1810. return 0;
  1811. ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
  1812. if (!ret)
  1813. ret = !memcmp(opt_ptr, tmp.data, tmp.size);
  1814. av_free(tmp.data);
  1815. return ret;
  1816. }
  1817. case AV_OPT_TYPE_DICT: {
  1818. AVDictionary *dict1 = NULL;
  1819. AVDictionary *dict2 = *(AVDictionary **)dst;
  1820. AVDictionaryEntry *en1 = NULL;
  1821. AVDictionaryEntry *en2 = NULL;
  1822. ret = av_dict_parse_string(&dict1, o->default_val.str, "=", ":", 0);
  1823. if (ret < 0) {
  1824. av_dict_free(&dict1);
  1825. return ret;
  1826. }
  1827. do {
  1828. en1 = av_dict_get(dict1, "", en1, AV_DICT_IGNORE_SUFFIX);
  1829. en2 = av_dict_get(dict2, "", en2, AV_DICT_IGNORE_SUFFIX);
  1830. } while (en1 && en2 && !strcmp(en1->key, en2->key) && !strcmp(en1->value, en2->value));
  1831. av_dict_free(&dict1);
  1832. return (!en1 && !en2);
  1833. }
  1834. case AV_OPT_TYPE_IMAGE_SIZE:
  1835. if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
  1836. w = h = 0;
  1837. else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
  1838. return ret;
  1839. return (w == *(int *)dst) && (h == *((int *)dst+1));
  1840. case AV_OPT_TYPE_VIDEO_RATE:
  1841. q = (AVRational){0, 0};
  1842. if (o->default_val.str) {
  1843. if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
  1844. return ret;
  1845. }
  1846. return !av_cmp_q(*(AVRational*)dst, q);
  1847. case AV_OPT_TYPE_COLOR: {
  1848. uint8_t color[4] = {0, 0, 0, 0};
  1849. if (o->default_val.str) {
  1850. if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
  1851. return ret;
  1852. }
  1853. return !memcmp(color, dst, sizeof(color));
  1854. }
  1855. default:
  1856. av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
  1857. break;
  1858. }
  1859. return AVERROR_PATCHWELCOME;
  1860. }
  1861. int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
  1862. {
  1863. const AVOption *o;
  1864. void *target;
  1865. if (!obj)
  1866. return AVERROR(EINVAL);
  1867. o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
  1868. if (!o)
  1869. return AVERROR_OPTION_NOT_FOUND;
  1870. return av_opt_is_set_to_default(target, o);
  1871. }
  1872. int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
  1873. const char key_val_sep, const char pairs_sep)
  1874. {
  1875. const AVOption *o = NULL;
  1876. uint8_t *buf;
  1877. AVBPrint bprint;
  1878. int ret, cnt = 0;
  1879. const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
  1880. if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
  1881. pairs_sep == '\\' || key_val_sep == '\\') {
  1882. av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
  1883. return AVERROR(EINVAL);
  1884. }
  1885. if (!obj || !buffer)
  1886. return AVERROR(EINVAL);
  1887. *buffer = NULL;
  1888. av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
  1889. while (o = av_opt_next(obj, o)) {
  1890. if (o->type == AV_OPT_TYPE_CONST)
  1891. continue;
  1892. if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
  1893. continue;
  1894. else if (((o->flags & opt_flags) != opt_flags))
  1895. continue;
  1896. if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
  1897. continue;
  1898. if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
  1899. av_bprint_finalize(&bprint, NULL);
  1900. return ret;
  1901. }
  1902. if (buf) {
  1903. if (cnt++)
  1904. av_bprint_append_data(&bprint, &pairs_sep, 1);
  1905. av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
  1906. av_bprint_append_data(&bprint, &key_val_sep, 1);
  1907. av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
  1908. av_freep(&buf);
  1909. }
  1910. }
  1911. ret = av_bprint_finalize(&bprint, buffer);
  1912. if (ret < 0)
  1913. return ret;
  1914. return 0;
  1915. }