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.

1881 lines
61KB

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