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.

1745 lines
57KB

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