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.

1869 lines
61KB

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