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.

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