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.

1722 lines
56KB

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