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.

1889 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. *(int64_t *)(((uint8_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. if (!len) {
  643. *out_val[0] = '\0';
  644. return 0;
  645. }
  646. bin = *(uint8_t**)dst;
  647. for (i = 0; i < len; i++)
  648. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  649. return 0;
  650. case AV_OPT_TYPE_IMAGE_SIZE:
  651. ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
  652. break;
  653. case AV_OPT_TYPE_PIXEL_FMT:
  654. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
  655. break;
  656. case AV_OPT_TYPE_SAMPLE_FMT:
  657. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
  658. break;
  659. case AV_OPT_TYPE_DURATION:
  660. i64 = *(int64_t *)dst;
  661. ret = snprintf(buf, sizeof(buf), "%"PRIi64":%02d:%02d.%06d",
  662. i64 / 3600000000, (int)((i64 / 60000000) % 60),
  663. (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
  664. break;
  665. case AV_OPT_TYPE_COLOR:
  666. ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
  667. (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
  668. (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
  669. break;
  670. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  671. i64 = *(int64_t *)dst;
  672. ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
  673. break;
  674. default:
  675. return AVERROR(EINVAL);
  676. }
  677. if (ret >= sizeof(buf))
  678. return AVERROR(EINVAL);
  679. *out_val = av_strdup(buf);
  680. return 0;
  681. }
  682. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  683. int search_flags)
  684. {
  685. void *dst, *target_obj;
  686. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  687. if (!o || !target_obj)
  688. goto error;
  689. dst = ((uint8_t*)target_obj) + o->offset;
  690. if (o_out) *o_out= o;
  691. return read_number(o, dst, num, den, intnum);
  692. error:
  693. *den=*intnum=0;
  694. return -1;
  695. }
  696. #if FF_API_OLD_AVOPTIONS
  697. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  698. {
  699. int64_t intnum=1;
  700. double num=1;
  701. int den=1;
  702. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  703. return NAN;
  704. return num*intnum/den;
  705. }
  706. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  707. {
  708. int64_t intnum=1;
  709. double num=1;
  710. int den=1;
  711. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  712. return (AVRational){0, 0};
  713. if (num == 1.0 && (int)intnum == intnum)
  714. return (AVRational){intnum, den};
  715. else
  716. return av_d2q(num*intnum/den, 1<<24);
  717. }
  718. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  719. {
  720. int64_t intnum=1;
  721. double num=1;
  722. int den=1;
  723. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  724. return -1;
  725. return num*intnum/den;
  726. }
  727. #endif
  728. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  729. {
  730. int64_t intnum = 1;
  731. double num = 1;
  732. int ret, den = 1;
  733. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  734. return ret;
  735. *out_val = num*intnum/den;
  736. return 0;
  737. }
  738. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  739. {
  740. int64_t intnum = 1;
  741. double num = 1;
  742. int ret, den = 1;
  743. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  744. return ret;
  745. *out_val = num*intnum/den;
  746. return 0;
  747. }
  748. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  749. {
  750. int64_t intnum = 1;
  751. double num = 1;
  752. int ret, den = 1;
  753. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  754. return ret;
  755. if (num == 1.0 && (int)intnum == intnum)
  756. *out_val = (AVRational){intnum, den};
  757. else
  758. *out_val = av_d2q(num*intnum/den, 1<<24);
  759. return 0;
  760. }
  761. int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
  762. {
  763. void *dst, *target_obj;
  764. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  765. if (!o || !target_obj)
  766. return AVERROR_OPTION_NOT_FOUND;
  767. if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
  768. av_log(obj, AV_LOG_ERROR,
  769. "The value for option '%s' is not an image size.\n", name);
  770. return AVERROR(EINVAL);
  771. }
  772. dst = ((uint8_t*)target_obj) + o->offset;
  773. if (w_out) *w_out = *(int *)dst;
  774. if (h_out) *h_out = *((int *)dst+1);
  775. return 0;
  776. }
  777. int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
  778. {
  779. int64_t intnum = 1;
  780. double num = 1;
  781. int ret, den = 1;
  782. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  783. return ret;
  784. if (num == 1.0 && (int)intnum == intnum)
  785. *out_val = (AVRational){intnum, den};
  786. else
  787. *out_val = av_d2q(num*intnum/den, 1<<24);
  788. return 0;
  789. }
  790. static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
  791. enum AVOptionType type, const char *desc)
  792. {
  793. void *dst, *target_obj;
  794. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  795. if (!o || !target_obj)
  796. return AVERROR_OPTION_NOT_FOUND;
  797. if (o->type != type) {
  798. av_log(obj, AV_LOG_ERROR,
  799. "The value for option '%s' is not a %s format.\n", desc, name);
  800. return AVERROR(EINVAL);
  801. }
  802. dst = ((uint8_t*)target_obj) + o->offset;
  803. *out_fmt = *(int *)dst;
  804. return 0;
  805. }
  806. int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
  807. {
  808. return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
  809. }
  810. int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
  811. {
  812. return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
  813. }
  814. int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
  815. {
  816. void *dst, *target_obj;
  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_CHANNEL_LAYOUT) {
  821. av_log(obj, AV_LOG_ERROR,
  822. "The value for option '%s' is not a channel layout.\n", name);
  823. return AVERROR(EINVAL);
  824. }
  825. dst = ((uint8_t*)target_obj) + o->offset;
  826. *cl = *(int64_t *)dst;
  827. return 0;
  828. }
  829. int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
  830. {
  831. void *target_obj;
  832. AVDictionary *src;
  833. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  834. if (!o || !target_obj)
  835. return AVERROR_OPTION_NOT_FOUND;
  836. if (o->type != AV_OPT_TYPE_DICT)
  837. return AVERROR(EINVAL);
  838. src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
  839. av_dict_copy(out_val, src, 0);
  840. return 0;
  841. }
  842. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  843. {
  844. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  845. const AVOption *flag = av_opt_find(obj, flag_name,
  846. field ? field->unit : NULL, 0, 0);
  847. int64_t res;
  848. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  849. av_opt_get_int(obj, field_name, 0, &res) < 0)
  850. return 0;
  851. return res & flag->default_val.i64;
  852. }
  853. static void log_value(void *av_log_obj, int level, double d)
  854. {
  855. if (d == INT_MAX) {
  856. av_log(av_log_obj, level, "INT_MAX");
  857. } else if (d == INT_MIN) {
  858. av_log(av_log_obj, level, "INT_MIN");
  859. } else if (d == UINT32_MAX) {
  860. av_log(av_log_obj, level, "UINT32_MAX");
  861. } else if (d == (double)INT64_MAX) {
  862. av_log(av_log_obj, level, "I64_MAX");
  863. } else if (d == INT64_MIN) {
  864. av_log(av_log_obj, level, "I64_MIN");
  865. } else if (d == FLT_MAX) {
  866. av_log(av_log_obj, level, "FLT_MAX");
  867. } else if (d == FLT_MIN) {
  868. av_log(av_log_obj, level, "FLT_MIN");
  869. } else if (d == -FLT_MAX) {
  870. av_log(av_log_obj, level, "-FLT_MAX");
  871. } else if (d == -FLT_MIN) {
  872. av_log(av_log_obj, level, "-FLT_MIN");
  873. } else if (d == DBL_MAX) {
  874. av_log(av_log_obj, level, "DBL_MAX");
  875. } else if (d == DBL_MIN) {
  876. av_log(av_log_obj, level, "DBL_MIN");
  877. } else if (d == -DBL_MAX) {
  878. av_log(av_log_obj, level, "-DBL_MAX");
  879. } else if (d == -DBL_MIN) {
  880. av_log(av_log_obj, level, "-DBL_MIN");
  881. } else {
  882. av_log(av_log_obj, level, "%g", d);
  883. }
  884. }
  885. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  886. int req_flags, int rej_flags)
  887. {
  888. const AVOption *opt=NULL;
  889. AVOptionRanges *r;
  890. int i;
  891. while ((opt = av_opt_next(obj, opt))) {
  892. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  893. continue;
  894. /* Don't print CONST's on level one.
  895. * Don't print anything but CONST's on level two.
  896. * Only print items from the requested unit.
  897. */
  898. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  899. continue;
  900. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  901. continue;
  902. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  903. continue;
  904. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  905. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  906. else
  907. av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
  908. (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
  909. opt->name);
  910. switch (opt->type) {
  911. case AV_OPT_TYPE_FLAGS:
  912. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
  913. break;
  914. case AV_OPT_TYPE_INT:
  915. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
  916. break;
  917. case AV_OPT_TYPE_INT64:
  918. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
  919. break;
  920. case AV_OPT_TYPE_DOUBLE:
  921. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
  922. break;
  923. case AV_OPT_TYPE_FLOAT:
  924. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
  925. break;
  926. case AV_OPT_TYPE_STRING:
  927. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
  928. break;
  929. case AV_OPT_TYPE_RATIONAL:
  930. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
  931. break;
  932. case AV_OPT_TYPE_BINARY:
  933. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
  934. break;
  935. case AV_OPT_TYPE_IMAGE_SIZE:
  936. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
  937. break;
  938. case AV_OPT_TYPE_VIDEO_RATE:
  939. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
  940. break;
  941. case AV_OPT_TYPE_PIXEL_FMT:
  942. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
  943. break;
  944. case AV_OPT_TYPE_SAMPLE_FMT:
  945. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
  946. break;
  947. case AV_OPT_TYPE_DURATION:
  948. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
  949. break;
  950. case AV_OPT_TYPE_COLOR:
  951. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
  952. break;
  953. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  954. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
  955. break;
  956. case AV_OPT_TYPE_CONST:
  957. default:
  958. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
  959. break;
  960. }
  961. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  962. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  963. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
  964. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  965. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  966. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  967. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
  968. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
  969. if (opt->help)
  970. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  971. if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
  972. switch (opt->type) {
  973. case AV_OPT_TYPE_INT:
  974. case AV_OPT_TYPE_INT64:
  975. case AV_OPT_TYPE_DOUBLE:
  976. case AV_OPT_TYPE_FLOAT:
  977. case AV_OPT_TYPE_RATIONAL:
  978. for (i = 0; i < r->nb_ranges; i++) {
  979. av_log(av_log_obj, AV_LOG_INFO, " (from ");
  980. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
  981. av_log(av_log_obj, AV_LOG_INFO, " to ");
  982. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
  983. av_log(av_log_obj, AV_LOG_INFO, ")");
  984. }
  985. break;
  986. }
  987. av_opt_freep_ranges(&r);
  988. }
  989. if (opt->type != AV_OPT_TYPE_CONST &&
  990. opt->type != AV_OPT_TYPE_BINARY &&
  991. !((opt->type == AV_OPT_TYPE_COLOR ||
  992. opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
  993. opt->type == AV_OPT_TYPE_STRING ||
  994. opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
  995. !opt->default_val.str)) {
  996. av_log(av_log_obj, AV_LOG_INFO, " (default ");
  997. switch (opt->type) {
  998. case AV_OPT_TYPE_FLAGS:
  999. av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
  1000. break;
  1001. case AV_OPT_TYPE_DURATION:
  1002. case AV_OPT_TYPE_INT:
  1003. case AV_OPT_TYPE_INT64:
  1004. log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
  1005. break;
  1006. case AV_OPT_TYPE_DOUBLE:
  1007. case AV_OPT_TYPE_FLOAT:
  1008. log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
  1009. break;
  1010. case AV_OPT_TYPE_RATIONAL: {
  1011. AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
  1012. av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
  1013. break;
  1014. case AV_OPT_TYPE_PIXEL_FMT:
  1015. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
  1016. break;
  1017. case AV_OPT_TYPE_SAMPLE_FMT:
  1018. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
  1019. break;
  1020. case AV_OPT_TYPE_COLOR:
  1021. case AV_OPT_TYPE_IMAGE_SIZE:
  1022. case AV_OPT_TYPE_STRING:
  1023. case AV_OPT_TYPE_VIDEO_RATE:
  1024. av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
  1025. break;
  1026. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1027. av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
  1028. break;
  1029. }
  1030. av_log(av_log_obj, AV_LOG_INFO, ")");
  1031. }
  1032. av_log(av_log_obj, AV_LOG_INFO, "\n");
  1033. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  1034. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  1035. }
  1036. }
  1037. }
  1038. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  1039. {
  1040. if (!obj)
  1041. return -1;
  1042. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  1043. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  1044. return 0;
  1045. }
  1046. void av_opt_set_defaults(void *s)
  1047. {
  1048. #if FF_API_OLD_AVOPTIONS
  1049. av_opt_set_defaults2(s, 0, 0);
  1050. }
  1051. void av_opt_set_defaults2(void *s, int mask, int flags)
  1052. {
  1053. #endif
  1054. const AVOption *opt = NULL;
  1055. while ((opt = av_opt_next(s, opt))) {
  1056. void *dst = ((uint8_t*)s) + opt->offset;
  1057. #if FF_API_OLD_AVOPTIONS
  1058. if ((opt->flags & mask) != flags)
  1059. continue;
  1060. #endif
  1061. if (opt->flags & AV_OPT_FLAG_READONLY)
  1062. continue;
  1063. switch (opt->type) {
  1064. case AV_OPT_TYPE_CONST:
  1065. /* Nothing to be done here */
  1066. break;
  1067. case AV_OPT_TYPE_FLAGS:
  1068. case AV_OPT_TYPE_INT:
  1069. case AV_OPT_TYPE_INT64:
  1070. case AV_OPT_TYPE_DURATION:
  1071. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1072. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1073. break;
  1074. case AV_OPT_TYPE_DOUBLE:
  1075. case AV_OPT_TYPE_FLOAT: {
  1076. double val;
  1077. val = opt->default_val.dbl;
  1078. write_number(s, opt, dst, val, 1, 1);
  1079. }
  1080. break;
  1081. case AV_OPT_TYPE_RATIONAL: {
  1082. AVRational val;
  1083. val = av_d2q(opt->default_val.dbl, INT_MAX);
  1084. write_number(s, opt, dst, 1, val.den, val.num);
  1085. }
  1086. break;
  1087. case AV_OPT_TYPE_COLOR:
  1088. set_string_color(s, opt, opt->default_val.str, dst);
  1089. break;
  1090. case AV_OPT_TYPE_STRING:
  1091. set_string(s, opt, opt->default_val.str, dst);
  1092. break;
  1093. case AV_OPT_TYPE_IMAGE_SIZE:
  1094. set_string_image_size(s, opt, opt->default_val.str, dst);
  1095. break;
  1096. case AV_OPT_TYPE_VIDEO_RATE:
  1097. set_string_video_rate(s, opt, opt->default_val.str, dst);
  1098. break;
  1099. case AV_OPT_TYPE_PIXEL_FMT:
  1100. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1101. break;
  1102. case AV_OPT_TYPE_SAMPLE_FMT:
  1103. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1104. break;
  1105. case AV_OPT_TYPE_BINARY:
  1106. case AV_OPT_TYPE_DICT:
  1107. /* Cannot set defaults for these types */
  1108. break;
  1109. default:
  1110. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  1111. }
  1112. }
  1113. }
  1114. /**
  1115. * Store the value in the field in ctx that is named like key.
  1116. * ctx must be an AVClass context, storing is done using AVOptions.
  1117. *
  1118. * @param buf the string to parse, buf will be updated to point at the
  1119. * separator just after the parsed key/value pair
  1120. * @param key_val_sep a 0-terminated list of characters used to
  1121. * separate key from value
  1122. * @param pairs_sep a 0-terminated list of characters used to separate
  1123. * two pairs from each other
  1124. * @return 0 if the key/value pair has been successfully parsed and
  1125. * set, or a negative value corresponding to an AVERROR code in case
  1126. * of error:
  1127. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  1128. * the error code issued by av_opt_set() if the key/value pair
  1129. * cannot be set
  1130. */
  1131. static int parse_key_value_pair(void *ctx, const char **buf,
  1132. const char *key_val_sep, const char *pairs_sep)
  1133. {
  1134. char *key = av_get_token(buf, key_val_sep);
  1135. char *val;
  1136. int ret;
  1137. if (!key)
  1138. return AVERROR(ENOMEM);
  1139. if (*key && strspn(*buf, key_val_sep)) {
  1140. (*buf)++;
  1141. val = av_get_token(buf, pairs_sep);
  1142. if (!val) {
  1143. av_freep(&key);
  1144. return AVERROR(ENOMEM);
  1145. }
  1146. } else {
  1147. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  1148. av_free(key);
  1149. return AVERROR(EINVAL);
  1150. }
  1151. av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
  1152. ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
  1153. if (ret == AVERROR_OPTION_NOT_FOUND)
  1154. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  1155. av_free(key);
  1156. av_free(val);
  1157. return ret;
  1158. }
  1159. int av_set_options_string(void *ctx, const char *opts,
  1160. const char *key_val_sep, const char *pairs_sep)
  1161. {
  1162. int ret, count = 0;
  1163. if (!opts)
  1164. return 0;
  1165. while (*opts) {
  1166. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  1167. return ret;
  1168. count++;
  1169. if (*opts)
  1170. opts++;
  1171. }
  1172. return count;
  1173. }
  1174. #define WHITESPACES " \n\t"
  1175. static int is_key_char(char c)
  1176. {
  1177. return (unsigned)((c | 32) - 'a') < 26 ||
  1178. (unsigned)(c - '0') < 10 ||
  1179. c == '-' || c == '_' || c == '/' || c == '.';
  1180. }
  1181. /**
  1182. * Read a key from a string.
  1183. *
  1184. * The key consists of is_key_char characters and must be terminated by a
  1185. * character from the delim string; spaces are ignored.
  1186. *
  1187. * @return 0 for success (even with ellipsis), <0 for failure
  1188. */
  1189. static int get_key(const char **ropts, const char *delim, char **rkey)
  1190. {
  1191. const char *opts = *ropts;
  1192. const char *key_start, *key_end;
  1193. key_start = opts += strspn(opts, WHITESPACES);
  1194. while (is_key_char(*opts))
  1195. opts++;
  1196. key_end = opts;
  1197. opts += strspn(opts, WHITESPACES);
  1198. if (!*opts || !strchr(delim, *opts))
  1199. return AVERROR(EINVAL);
  1200. opts++;
  1201. if (!(*rkey = av_malloc(key_end - key_start + 1)))
  1202. return AVERROR(ENOMEM);
  1203. memcpy(*rkey, key_start, key_end - key_start);
  1204. (*rkey)[key_end - key_start] = 0;
  1205. *ropts = opts;
  1206. return 0;
  1207. }
  1208. int av_opt_get_key_value(const char **ropts,
  1209. const char *key_val_sep, const char *pairs_sep,
  1210. unsigned flags,
  1211. char **rkey, char **rval)
  1212. {
  1213. int ret;
  1214. char *key = NULL, *val;
  1215. const char *opts = *ropts;
  1216. if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
  1217. !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
  1218. return AVERROR(EINVAL);
  1219. if (!(val = av_get_token(&opts, pairs_sep))) {
  1220. av_free(key);
  1221. return AVERROR(ENOMEM);
  1222. }
  1223. *ropts = opts;
  1224. *rkey = key;
  1225. *rval = val;
  1226. return 0;
  1227. }
  1228. int av_opt_set_from_string(void *ctx, const char *opts,
  1229. const char *const *shorthand,
  1230. const char *key_val_sep, const char *pairs_sep)
  1231. {
  1232. int ret, count = 0;
  1233. const char *dummy_shorthand = NULL;
  1234. char *av_uninit(parsed_key), *av_uninit(value);
  1235. const char *key;
  1236. if (!opts)
  1237. return 0;
  1238. if (!shorthand)
  1239. shorthand = &dummy_shorthand;
  1240. while (*opts) {
  1241. ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
  1242. *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  1243. &parsed_key, &value);
  1244. if (ret < 0) {
  1245. if (ret == AVERROR(EINVAL))
  1246. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
  1247. else
  1248. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
  1249. av_err2str(ret));
  1250. return ret;
  1251. }
  1252. if (*opts)
  1253. opts++;
  1254. if (parsed_key) {
  1255. key = parsed_key;
  1256. while (*shorthand) /* discard all remaining shorthand */
  1257. shorthand++;
  1258. } else {
  1259. key = *(shorthand++);
  1260. }
  1261. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  1262. if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
  1263. if (ret == AVERROR_OPTION_NOT_FOUND)
  1264. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  1265. av_free(value);
  1266. av_free(parsed_key);
  1267. return ret;
  1268. }
  1269. av_free(value);
  1270. av_free(parsed_key);
  1271. count++;
  1272. }
  1273. return count;
  1274. }
  1275. void av_opt_free(void *obj)
  1276. {
  1277. const AVOption *o = NULL;
  1278. while ((o = av_opt_next(obj, o))) {
  1279. switch (o->type) {
  1280. case AV_OPT_TYPE_STRING:
  1281. case AV_OPT_TYPE_BINARY:
  1282. av_freep((uint8_t *)obj + o->offset);
  1283. break;
  1284. case AV_OPT_TYPE_DICT:
  1285. av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
  1286. break;
  1287. default:
  1288. break;
  1289. }
  1290. }
  1291. }
  1292. int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
  1293. {
  1294. AVDictionaryEntry *t = NULL;
  1295. AVDictionary *tmp = NULL;
  1296. int ret = 0;
  1297. if (!options)
  1298. return 0;
  1299. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  1300. ret = av_opt_set(obj, t->key, t->value, search_flags);
  1301. if (ret == AVERROR_OPTION_NOT_FOUND)
  1302. av_dict_set(&tmp, t->key, t->value, 0);
  1303. else if (ret < 0) {
  1304. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  1305. break;
  1306. }
  1307. ret = 0;
  1308. }
  1309. av_dict_free(options);
  1310. *options = tmp;
  1311. return ret;
  1312. }
  1313. int av_opt_set_dict(void *obj, AVDictionary **options)
  1314. {
  1315. return av_opt_set_dict2(obj, options, 0);
  1316. }
  1317. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  1318. int opt_flags, int search_flags)
  1319. {
  1320. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  1321. }
  1322. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  1323. int opt_flags, int search_flags, void **target_obj)
  1324. {
  1325. const AVClass *c;
  1326. const AVOption *o = NULL;
  1327. if(!obj)
  1328. return NULL;
  1329. c= *(AVClass**)obj;
  1330. if (!c)
  1331. return NULL;
  1332. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  1333. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  1334. const AVClass *child = NULL;
  1335. while (child = av_opt_child_class_next(c, child))
  1336. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  1337. return o;
  1338. } else {
  1339. void *child = NULL;
  1340. while (child = av_opt_child_next(obj, child))
  1341. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  1342. return o;
  1343. }
  1344. }
  1345. while (o = av_opt_next(obj, o)) {
  1346. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  1347. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  1348. (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
  1349. if (target_obj) {
  1350. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  1351. *target_obj = obj;
  1352. else
  1353. *target_obj = NULL;
  1354. }
  1355. return o;
  1356. }
  1357. }
  1358. return NULL;
  1359. }
  1360. void *av_opt_child_next(void *obj, void *prev)
  1361. {
  1362. const AVClass *c = *(AVClass**)obj;
  1363. if (c->child_next)
  1364. return c->child_next(obj, prev);
  1365. return NULL;
  1366. }
  1367. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  1368. {
  1369. if (parent->child_class_next)
  1370. return parent->child_class_next(prev);
  1371. return NULL;
  1372. }
  1373. void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
  1374. {
  1375. const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
  1376. if(!opt)
  1377. return NULL;
  1378. return (uint8_t*)obj + opt->offset;
  1379. }
  1380. static int opt_size(enum AVOptionType type)
  1381. {
  1382. switch(type) {
  1383. case AV_OPT_TYPE_INT:
  1384. case AV_OPT_TYPE_FLAGS: return sizeof(int);
  1385. case AV_OPT_TYPE_DURATION:
  1386. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1387. case AV_OPT_TYPE_INT64: return sizeof(int64_t);
  1388. case AV_OPT_TYPE_DOUBLE: return sizeof(double);
  1389. case AV_OPT_TYPE_FLOAT: return sizeof(float);
  1390. case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
  1391. case AV_OPT_TYPE_VIDEO_RATE:
  1392. case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
  1393. case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
  1394. case AV_OPT_TYPE_IMAGE_SIZE:return sizeof(int[2]);
  1395. case AV_OPT_TYPE_PIXEL_FMT: return sizeof(enum AVPixelFormat);
  1396. case AV_OPT_TYPE_SAMPLE_FMT:return sizeof(enum AVSampleFormat);
  1397. case AV_OPT_TYPE_COLOR: return 4;
  1398. }
  1399. return 0;
  1400. }
  1401. int av_opt_copy(void *dst, void *src)
  1402. {
  1403. const AVOption *o = NULL;
  1404. const AVClass *c;
  1405. int ret = 0;
  1406. if (!src)
  1407. return 0;
  1408. c = *(AVClass**)src;
  1409. if (*(AVClass**)dst && c != *(AVClass**)dst)
  1410. return AVERROR(EINVAL);
  1411. while ((o = av_opt_next(src, o))) {
  1412. void *field_dst = ((uint8_t*)dst) + o->offset;
  1413. void *field_src = ((uint8_t*)src) + o->offset;
  1414. uint8_t **field_dst8 = (uint8_t**)field_dst;
  1415. uint8_t **field_src8 = (uint8_t**)field_src;
  1416. if (o->type == AV_OPT_TYPE_STRING) {
  1417. if (*field_dst8 != *field_src8)
  1418. av_freep(field_dst8);
  1419. *field_dst8 = av_strdup(*field_src8);
  1420. if (*field_src8 && !*field_dst8)
  1421. ret = AVERROR(ENOMEM);
  1422. } else if (o->type == AV_OPT_TYPE_BINARY) {
  1423. int len = *(int*)(field_src8 + 1);
  1424. if (*field_dst8 != *field_src8)
  1425. av_freep(field_dst8);
  1426. *field_dst8 = av_memdup(*field_src8, len);
  1427. if (len && !*field_dst8) {
  1428. ret = AVERROR(ENOMEM);
  1429. len = 0;
  1430. }
  1431. *(int*)(field_dst8 + 1) = len;
  1432. } else if (o->type == AV_OPT_TYPE_CONST) {
  1433. // do nothing
  1434. } else {
  1435. memcpy(field_dst, field_src, opt_size(o->type));
  1436. }
  1437. }
  1438. return ret;
  1439. }
  1440. int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1441. {
  1442. int ret;
  1443. const AVClass *c = *(AVClass**)obj;
  1444. int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
  1445. if (c->version > (52 << 16 | 11 << 8))
  1446. callback = c->query_ranges;
  1447. if (!callback)
  1448. callback = av_opt_query_ranges_default;
  1449. ret = callback(ranges_arg, obj, key, flags);
  1450. if (ret >= 0) {
  1451. if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
  1452. ret = 1;
  1453. (*ranges_arg)->nb_components = ret;
  1454. }
  1455. return ret;
  1456. }
  1457. int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1458. {
  1459. AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
  1460. AVOptionRange **range_array = av_mallocz(sizeof(void*));
  1461. AVOptionRange *range = av_mallocz(sizeof(*range));
  1462. const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
  1463. int ret;
  1464. *ranges_arg = NULL;
  1465. if (!ranges || !range || !range_array || !field) {
  1466. ret = AVERROR(ENOMEM);
  1467. goto fail;
  1468. }
  1469. ranges->range = range_array;
  1470. ranges->range[0] = range;
  1471. ranges->nb_ranges = 1;
  1472. ranges->nb_components = 1;
  1473. range->is_range = 1;
  1474. range->value_min = field->min;
  1475. range->value_max = field->max;
  1476. switch (field->type) {
  1477. case AV_OPT_TYPE_INT:
  1478. case AV_OPT_TYPE_INT64:
  1479. case AV_OPT_TYPE_PIXEL_FMT:
  1480. case AV_OPT_TYPE_SAMPLE_FMT:
  1481. case AV_OPT_TYPE_FLOAT:
  1482. case AV_OPT_TYPE_DOUBLE:
  1483. case AV_OPT_TYPE_DURATION:
  1484. case AV_OPT_TYPE_COLOR:
  1485. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1486. break;
  1487. case AV_OPT_TYPE_STRING:
  1488. range->component_min = 0;
  1489. range->component_max = 0x10FFFF; // max unicode value
  1490. range->value_min = -1;
  1491. range->value_max = INT_MAX;
  1492. break;
  1493. case AV_OPT_TYPE_RATIONAL:
  1494. range->component_min = INT_MIN;
  1495. range->component_max = INT_MAX;
  1496. break;
  1497. case AV_OPT_TYPE_IMAGE_SIZE:
  1498. range->component_min = 0;
  1499. range->component_max = INT_MAX/128/8;
  1500. range->value_min = 0;
  1501. range->value_max = INT_MAX/8;
  1502. break;
  1503. case AV_OPT_TYPE_VIDEO_RATE:
  1504. range->component_min = 1;
  1505. range->component_max = INT_MAX;
  1506. range->value_min = 1;
  1507. range->value_max = INT_MAX;
  1508. break;
  1509. default:
  1510. ret = AVERROR(ENOSYS);
  1511. goto fail;
  1512. }
  1513. *ranges_arg = ranges;
  1514. return 1;
  1515. fail:
  1516. av_free(ranges);
  1517. av_free(range);
  1518. av_free(range_array);
  1519. return ret;
  1520. }
  1521. void av_opt_freep_ranges(AVOptionRanges **rangesp)
  1522. {
  1523. int i;
  1524. AVOptionRanges *ranges = *rangesp;
  1525. if (!ranges)
  1526. return;
  1527. for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
  1528. AVOptionRange *range = ranges->range[i];
  1529. if (range) {
  1530. av_freep(&range->str);
  1531. av_freep(&ranges->range[i]);
  1532. }
  1533. }
  1534. av_freep(&ranges->range);
  1535. av_freep(rangesp);
  1536. }
  1537. #ifdef TEST
  1538. typedef struct TestContext
  1539. {
  1540. const AVClass *class;
  1541. int num;
  1542. int toggle;
  1543. char *string;
  1544. int flags;
  1545. AVRational rational;
  1546. AVRational video_rate;
  1547. int w, h;
  1548. enum AVPixelFormat pix_fmt;
  1549. enum AVSampleFormat sample_fmt;
  1550. int64_t duration;
  1551. uint8_t color[4];
  1552. int64_t channel_layout;
  1553. } TestContext;
  1554. #define OFFSET(x) offsetof(TestContext, x)
  1555. #define TEST_FLAG_COOL 01
  1556. #define TEST_FLAG_LAME 02
  1557. #define TEST_FLAG_MU 04
  1558. static const AVOption test_options[]= {
  1559. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  1560. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  1561. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  1562. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
  1563. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  1564. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  1565. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  1566. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  1567. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
  1568. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX},
  1569. {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, INT_MAX},
  1570. {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
  1571. {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
  1572. {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
  1573. {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
  1574. {NULL},
  1575. };
  1576. static const char *test_get_name(void *ctx)
  1577. {
  1578. return "test";
  1579. }
  1580. static const AVClass test_class = {
  1581. "TestContext",
  1582. test_get_name,
  1583. test_options
  1584. };
  1585. int main(void)
  1586. {
  1587. int i;
  1588. printf("\nTesting av_set_options_string()\n");
  1589. {
  1590. TestContext test_ctx = { 0 };
  1591. static const char * const options[] = {
  1592. "",
  1593. ":",
  1594. "=",
  1595. "foo=:",
  1596. ":=foo",
  1597. "=foo",
  1598. "foo=",
  1599. "foo",
  1600. "foo=val",
  1601. "foo==val",
  1602. "toggle=:",
  1603. "string=:",
  1604. "toggle=1 : foo",
  1605. "toggle=100",
  1606. "toggle==1",
  1607. "flags=+mu-lame : num=42: toggle=0",
  1608. "num=42 : string=blahblah",
  1609. "rational=0 : rational=1/2 : rational=1/-1",
  1610. "rational=-1/0",
  1611. "size=1024x768",
  1612. "size=pal",
  1613. "size=bogus",
  1614. "pix_fmt=yuv420p",
  1615. "pix_fmt=2",
  1616. "pix_fmt=bogus",
  1617. "sample_fmt=s16",
  1618. "sample_fmt=2",
  1619. "sample_fmt=bogus",
  1620. "video_rate=pal",
  1621. "video_rate=25",
  1622. "video_rate=30000/1001",
  1623. "video_rate=30/1.001",
  1624. "video_rate=bogus",
  1625. "duration=bogus",
  1626. "duration=123.45",
  1627. "duration=1\\:23\\:45.67",
  1628. "color=blue",
  1629. "color=0x223300",
  1630. "color=0x42FF07AA",
  1631. "cl=stereo+downmix",
  1632. "cl=foo",
  1633. };
  1634. test_ctx.class = &test_class;
  1635. av_opt_set_defaults(&test_ctx);
  1636. av_log_set_level(AV_LOG_DEBUG);
  1637. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1638. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1639. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  1640. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1641. printf("\n");
  1642. }
  1643. av_opt_free(&test_ctx);
  1644. }
  1645. printf("\nTesting av_opt_set_from_string()\n");
  1646. {
  1647. TestContext test_ctx = { 0 };
  1648. static const char * const options[] = {
  1649. "",
  1650. "5",
  1651. "5:hello",
  1652. "5:hello:size=pal",
  1653. "5:size=pal:hello",
  1654. ":",
  1655. "=",
  1656. " 5 : hello : size = pal ",
  1657. "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
  1658. };
  1659. static const char * const shorthand[] = { "num", "string", NULL };
  1660. test_ctx.class = &test_class;
  1661. av_opt_set_defaults(&test_ctx);
  1662. av_log_set_level(AV_LOG_DEBUG);
  1663. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1664. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1665. if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
  1666. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1667. printf("\n");
  1668. }
  1669. av_opt_free(&test_ctx);
  1670. }
  1671. return 0;
  1672. }
  1673. #endif