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.

1902 lines
62KB

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