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.

1736 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 == UINT32_MAX) {
  814. av_log(av_log_obj, level, "UINT32_MAX");
  815. } else if (d == (double)INT64_MAX) {
  816. av_log(av_log_obj, level, "I64_MAX");
  817. } else if (d == INT64_MIN) {
  818. av_log(av_log_obj, level, "I64_MIN");
  819. } else if (d == FLT_MAX) {
  820. av_log(av_log_obj, level, "FLT_MAX");
  821. } else if (d == FLT_MIN) {
  822. av_log(av_log_obj, level, "FLT_MIN");
  823. } else if (d == -FLT_MAX) {
  824. av_log(av_log_obj, level, "-FLT_MAX");
  825. } else if (d == -FLT_MIN) {
  826. av_log(av_log_obj, level, "-FLT_MIN");
  827. } else if (d == DBL_MAX) {
  828. av_log(av_log_obj, level, "DBL_MAX");
  829. } else if (d == DBL_MIN) {
  830. av_log(av_log_obj, level, "DBL_MIN");
  831. } else if (d == -DBL_MAX) {
  832. av_log(av_log_obj, level, "-DBL_MAX");
  833. } else if (d == -DBL_MIN) {
  834. av_log(av_log_obj, level, "-DBL_MIN");
  835. } else {
  836. av_log(av_log_obj, level, "%g", d);
  837. }
  838. }
  839. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  840. int req_flags, int rej_flags)
  841. {
  842. const AVOption *opt=NULL;
  843. AVOptionRanges *r;
  844. int i;
  845. while ((opt = av_opt_next(obj, opt))) {
  846. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  847. continue;
  848. /* Don't print CONST's on level one.
  849. * Don't print anything but CONST's on level two.
  850. * Only print items from the requested unit.
  851. */
  852. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  853. continue;
  854. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  855. continue;
  856. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  857. continue;
  858. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  859. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  860. else
  861. av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
  862. (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
  863. opt->name);
  864. switch (opt->type) {
  865. case AV_OPT_TYPE_FLAGS:
  866. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
  867. break;
  868. case AV_OPT_TYPE_INT:
  869. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
  870. break;
  871. case AV_OPT_TYPE_INT64:
  872. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
  873. break;
  874. case AV_OPT_TYPE_DOUBLE:
  875. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
  876. break;
  877. case AV_OPT_TYPE_FLOAT:
  878. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
  879. break;
  880. case AV_OPT_TYPE_STRING:
  881. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
  882. break;
  883. case AV_OPT_TYPE_RATIONAL:
  884. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
  885. break;
  886. case AV_OPT_TYPE_BINARY:
  887. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
  888. break;
  889. case AV_OPT_TYPE_IMAGE_SIZE:
  890. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
  891. break;
  892. case AV_OPT_TYPE_VIDEO_RATE:
  893. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
  894. break;
  895. case AV_OPT_TYPE_PIXEL_FMT:
  896. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
  897. break;
  898. case AV_OPT_TYPE_SAMPLE_FMT:
  899. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
  900. break;
  901. case AV_OPT_TYPE_DURATION:
  902. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
  903. break;
  904. case AV_OPT_TYPE_COLOR:
  905. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
  906. break;
  907. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  908. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
  909. break;
  910. case AV_OPT_TYPE_CONST:
  911. default:
  912. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
  913. break;
  914. }
  915. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  916. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  917. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
  918. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  919. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  920. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  921. if (opt->help)
  922. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  923. if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
  924. switch (opt->type) {
  925. case AV_OPT_TYPE_INT:
  926. case AV_OPT_TYPE_INT64:
  927. case AV_OPT_TYPE_DOUBLE:
  928. case AV_OPT_TYPE_FLOAT:
  929. case AV_OPT_TYPE_RATIONAL:
  930. for (i = 0; i < r->nb_ranges; i++) {
  931. av_log(av_log_obj, AV_LOG_INFO, " (from ");
  932. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
  933. av_log(av_log_obj, AV_LOG_INFO, " to ");
  934. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
  935. av_log(av_log_obj, AV_LOG_INFO, ")");
  936. }
  937. break;
  938. }
  939. av_opt_freep_ranges(&r);
  940. }
  941. if (opt->type != AV_OPT_TYPE_CONST &&
  942. opt->type != AV_OPT_TYPE_BINARY &&
  943. !((opt->type == AV_OPT_TYPE_COLOR ||
  944. opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
  945. opt->type == AV_OPT_TYPE_STRING ||
  946. opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
  947. !opt->default_val.str)) {
  948. av_log(av_log_obj, AV_LOG_INFO, " (default ");
  949. switch (opt->type) {
  950. case AV_OPT_TYPE_FLAGS:
  951. av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
  952. break;
  953. case AV_OPT_TYPE_DURATION:
  954. case AV_OPT_TYPE_INT:
  955. case AV_OPT_TYPE_INT64:
  956. log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
  957. break;
  958. case AV_OPT_TYPE_DOUBLE:
  959. case AV_OPT_TYPE_FLOAT:
  960. log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
  961. break;
  962. case AV_OPT_TYPE_RATIONAL: {
  963. AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
  964. av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
  965. break;
  966. case AV_OPT_TYPE_PIXEL_FMT:
  967. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
  968. break;
  969. case AV_OPT_TYPE_SAMPLE_FMT:
  970. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
  971. break;
  972. case AV_OPT_TYPE_COLOR:
  973. case AV_OPT_TYPE_IMAGE_SIZE:
  974. case AV_OPT_TYPE_STRING:
  975. case AV_OPT_TYPE_VIDEO_RATE:
  976. av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
  977. break;
  978. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  979. av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
  980. break;
  981. }
  982. av_log(av_log_obj, AV_LOG_INFO, ")");
  983. }
  984. av_log(av_log_obj, AV_LOG_INFO, "\n");
  985. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  986. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  987. }
  988. }
  989. }
  990. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  991. {
  992. if (!obj)
  993. return -1;
  994. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  995. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  996. return 0;
  997. }
  998. void av_opt_set_defaults(void *s)
  999. {
  1000. #if FF_API_OLD_AVOPTIONS
  1001. av_opt_set_defaults2(s, 0, 0);
  1002. }
  1003. void av_opt_set_defaults2(void *s, int mask, int flags)
  1004. {
  1005. #endif
  1006. const AVClass *class = *(AVClass **)s;
  1007. const AVOption *opt = NULL;
  1008. while ((opt = av_opt_next(s, opt)) != NULL) {
  1009. void *dst = ((uint8_t*)s) + opt->offset;
  1010. #if FF_API_OLD_AVOPTIONS
  1011. if ((opt->flags & mask) != flags)
  1012. continue;
  1013. #endif
  1014. switch (opt->type) {
  1015. case AV_OPT_TYPE_CONST:
  1016. /* Nothing to be done here */
  1017. break;
  1018. case AV_OPT_TYPE_FLAGS:
  1019. case AV_OPT_TYPE_INT:
  1020. case AV_OPT_TYPE_INT64:
  1021. case AV_OPT_TYPE_DURATION:
  1022. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1023. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1024. break;
  1025. case AV_OPT_TYPE_DOUBLE:
  1026. case AV_OPT_TYPE_FLOAT: {
  1027. double val;
  1028. val = opt->default_val.dbl;
  1029. write_number(s, opt, dst, val, 1, 1);
  1030. }
  1031. break;
  1032. case AV_OPT_TYPE_RATIONAL: {
  1033. AVRational val;
  1034. val = av_d2q(opt->default_val.dbl, INT_MAX);
  1035. write_number(s, opt, dst, 1, val.den, val.num);
  1036. }
  1037. break;
  1038. case AV_OPT_TYPE_COLOR:
  1039. set_string_color(s, opt, opt->default_val.str, dst);
  1040. break;
  1041. case AV_OPT_TYPE_STRING:
  1042. set_string(s, opt, opt->default_val.str, dst);
  1043. break;
  1044. case AV_OPT_TYPE_IMAGE_SIZE:
  1045. set_string_image_size(s, opt, opt->default_val.str, dst);
  1046. break;
  1047. case AV_OPT_TYPE_VIDEO_RATE:
  1048. set_string_video_rate(s, opt, opt->default_val.str, dst);
  1049. break;
  1050. case AV_OPT_TYPE_PIXEL_FMT:
  1051. #if LIBAVUTIL_VERSION_MAJOR < 54
  1052. if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
  1053. av_opt_set(s, opt->name, opt->default_val.str, 0);
  1054. else
  1055. #endif
  1056. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1057. break;
  1058. case AV_OPT_TYPE_SAMPLE_FMT:
  1059. #if LIBAVUTIL_VERSION_MAJOR < 54
  1060. if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
  1061. av_opt_set(s, opt->name, opt->default_val.str, 0);
  1062. else
  1063. #endif
  1064. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1065. break;
  1066. case AV_OPT_TYPE_BINARY:
  1067. /* Cannot set default for binary */
  1068. break;
  1069. default:
  1070. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  1071. }
  1072. }
  1073. }
  1074. /**
  1075. * Store the value in the field in ctx that is named like key.
  1076. * ctx must be an AVClass context, storing is done using AVOptions.
  1077. *
  1078. * @param buf the string to parse, buf will be updated to point at the
  1079. * separator just after the parsed key/value pair
  1080. * @param key_val_sep a 0-terminated list of characters used to
  1081. * separate key from value
  1082. * @param pairs_sep a 0-terminated list of characters used to separate
  1083. * two pairs from each other
  1084. * @return 0 if the key/value pair has been successfully parsed and
  1085. * set, or a negative value corresponding to an AVERROR code in case
  1086. * of error:
  1087. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  1088. * the error code issued by av_opt_set() if the key/value pair
  1089. * cannot be set
  1090. */
  1091. static int parse_key_value_pair(void *ctx, const char **buf,
  1092. const char *key_val_sep, const char *pairs_sep)
  1093. {
  1094. char *key = av_get_token(buf, key_val_sep);
  1095. char *val;
  1096. int ret;
  1097. if (!key)
  1098. return AVERROR(ENOMEM);
  1099. if (*key && strspn(*buf, key_val_sep)) {
  1100. (*buf)++;
  1101. val = av_get_token(buf, pairs_sep);
  1102. if (!val) {
  1103. av_freep(&key);
  1104. return AVERROR(ENOMEM);
  1105. }
  1106. } else {
  1107. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  1108. av_free(key);
  1109. return AVERROR(EINVAL);
  1110. }
  1111. av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
  1112. ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
  1113. if (ret == AVERROR_OPTION_NOT_FOUND)
  1114. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  1115. av_free(key);
  1116. av_free(val);
  1117. return ret;
  1118. }
  1119. int av_set_options_string(void *ctx, const char *opts,
  1120. const char *key_val_sep, const char *pairs_sep)
  1121. {
  1122. int ret, count = 0;
  1123. if (!opts)
  1124. return 0;
  1125. while (*opts) {
  1126. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  1127. return ret;
  1128. count++;
  1129. if (*opts)
  1130. opts++;
  1131. }
  1132. return count;
  1133. }
  1134. #define WHITESPACES " \n\t"
  1135. static int is_key_char(char c)
  1136. {
  1137. return (unsigned)((c | 32) - 'a') < 26 ||
  1138. (unsigned)(c - '0') < 10 ||
  1139. c == '-' || c == '_' || c == '/' || c == '.';
  1140. }
  1141. /**
  1142. * Read a key from a string.
  1143. *
  1144. * The key consists of is_key_char characters and must be terminated by a
  1145. * character from the delim string; spaces are ignored.
  1146. *
  1147. * @return 0 for success (even with ellipsis), <0 for failure
  1148. */
  1149. static int get_key(const char **ropts, const char *delim, char **rkey)
  1150. {
  1151. const char *opts = *ropts;
  1152. const char *key_start, *key_end;
  1153. key_start = opts += strspn(opts, WHITESPACES);
  1154. while (is_key_char(*opts))
  1155. opts++;
  1156. key_end = opts;
  1157. opts += strspn(opts, WHITESPACES);
  1158. if (!*opts || !strchr(delim, *opts))
  1159. return AVERROR(EINVAL);
  1160. opts++;
  1161. if (!(*rkey = av_malloc(key_end - key_start + 1)))
  1162. return AVERROR(ENOMEM);
  1163. memcpy(*rkey, key_start, key_end - key_start);
  1164. (*rkey)[key_end - key_start] = 0;
  1165. *ropts = opts;
  1166. return 0;
  1167. }
  1168. int av_opt_get_key_value(const char **ropts,
  1169. const char *key_val_sep, const char *pairs_sep,
  1170. unsigned flags,
  1171. char **rkey, char **rval)
  1172. {
  1173. int ret;
  1174. char *key = NULL, *val;
  1175. const char *opts = *ropts;
  1176. if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
  1177. !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
  1178. return AVERROR(EINVAL);
  1179. if (!(val = av_get_token(&opts, pairs_sep))) {
  1180. av_free(key);
  1181. return AVERROR(ENOMEM);
  1182. }
  1183. *ropts = opts;
  1184. *rkey = key;
  1185. *rval = val;
  1186. return 0;
  1187. }
  1188. int av_opt_set_from_string(void *ctx, const char *opts,
  1189. const char *const *shorthand,
  1190. const char *key_val_sep, const char *pairs_sep)
  1191. {
  1192. int ret, count = 0;
  1193. const char *dummy_shorthand = NULL;
  1194. char *av_uninit(parsed_key), *av_uninit(value);
  1195. const char *key;
  1196. if (!opts)
  1197. return 0;
  1198. if (!shorthand)
  1199. shorthand = &dummy_shorthand;
  1200. while (*opts) {
  1201. ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
  1202. *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  1203. &parsed_key, &value);
  1204. if (ret < 0) {
  1205. if (ret == AVERROR(EINVAL))
  1206. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
  1207. else
  1208. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
  1209. av_err2str(ret));
  1210. return ret;
  1211. }
  1212. if (*opts)
  1213. opts++;
  1214. if (parsed_key) {
  1215. key = parsed_key;
  1216. while (*shorthand) /* discard all remaining shorthand */
  1217. shorthand++;
  1218. } else {
  1219. key = *(shorthand++);
  1220. }
  1221. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  1222. if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
  1223. if (ret == AVERROR_OPTION_NOT_FOUND)
  1224. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  1225. av_free(value);
  1226. av_free(parsed_key);
  1227. return ret;
  1228. }
  1229. av_free(value);
  1230. av_free(parsed_key);
  1231. count++;
  1232. }
  1233. return count;
  1234. }
  1235. void av_opt_free(void *obj)
  1236. {
  1237. const AVOption *o = NULL;
  1238. while ((o = av_opt_next(obj, o)))
  1239. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  1240. av_freep((uint8_t *)obj + o->offset);
  1241. }
  1242. int av_opt_set_dict(void *obj, AVDictionary **options)
  1243. {
  1244. AVDictionaryEntry *t = NULL;
  1245. AVDictionary *tmp = NULL;
  1246. int ret = 0;
  1247. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  1248. ret = av_opt_set(obj, t->key, t->value, 0);
  1249. if (ret == AVERROR_OPTION_NOT_FOUND)
  1250. av_dict_set(&tmp, t->key, t->value, 0);
  1251. else if (ret < 0) {
  1252. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  1253. break;
  1254. }
  1255. ret = 0;
  1256. }
  1257. av_dict_free(options);
  1258. *options = tmp;
  1259. return ret;
  1260. }
  1261. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  1262. int opt_flags, int search_flags)
  1263. {
  1264. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  1265. }
  1266. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  1267. int opt_flags, int search_flags, void **target_obj)
  1268. {
  1269. const AVClass *c;
  1270. const AVOption *o = NULL;
  1271. if(!obj)
  1272. return NULL;
  1273. c= *(AVClass**)obj;
  1274. if (!c)
  1275. return NULL;
  1276. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  1277. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  1278. const AVClass *child = NULL;
  1279. while (child = av_opt_child_class_next(c, child))
  1280. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  1281. return o;
  1282. } else {
  1283. void *child = NULL;
  1284. while (child = av_opt_child_next(obj, child))
  1285. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  1286. return o;
  1287. }
  1288. }
  1289. while (o = av_opt_next(obj, o)) {
  1290. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  1291. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  1292. (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
  1293. if (target_obj) {
  1294. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  1295. *target_obj = obj;
  1296. else
  1297. *target_obj = NULL;
  1298. }
  1299. return o;
  1300. }
  1301. }
  1302. return NULL;
  1303. }
  1304. void *av_opt_child_next(void *obj, void *prev)
  1305. {
  1306. const AVClass *c = *(AVClass**)obj;
  1307. if (c->child_next)
  1308. return c->child_next(obj, prev);
  1309. return NULL;
  1310. }
  1311. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  1312. {
  1313. if (parent->child_class_next)
  1314. return parent->child_class_next(prev);
  1315. return NULL;
  1316. }
  1317. void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
  1318. {
  1319. const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
  1320. if(!opt)
  1321. return NULL;
  1322. return (uint8_t*)obj + opt->offset;
  1323. }
  1324. int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1325. {
  1326. const AVClass *c = *(AVClass**)obj;
  1327. int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
  1328. if (c->version > (52 << 16 | 11 << 8))
  1329. callback = c->query_ranges;
  1330. if (!callback)
  1331. callback = av_opt_query_ranges_default;
  1332. return callback(ranges_arg, obj, key, flags);
  1333. }
  1334. int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1335. {
  1336. AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
  1337. AVOptionRange **range_array = av_mallocz(sizeof(void*));
  1338. AVOptionRange *range = av_mallocz(sizeof(*range));
  1339. const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
  1340. int ret;
  1341. *ranges_arg = NULL;
  1342. if (!ranges || !range || !range_array || !field) {
  1343. ret = AVERROR(ENOMEM);
  1344. goto fail;
  1345. }
  1346. ranges->range = range_array;
  1347. ranges->range[0] = range;
  1348. ranges->nb_ranges = 1;
  1349. range->is_range = 1;
  1350. range->value_min = field->min;
  1351. range->value_max = field->max;
  1352. switch (field->type) {
  1353. case AV_OPT_TYPE_INT:
  1354. case AV_OPT_TYPE_INT64:
  1355. case AV_OPT_TYPE_PIXEL_FMT:
  1356. case AV_OPT_TYPE_SAMPLE_FMT:
  1357. case AV_OPT_TYPE_FLOAT:
  1358. case AV_OPT_TYPE_DOUBLE:
  1359. case AV_OPT_TYPE_DURATION:
  1360. case AV_OPT_TYPE_COLOR:
  1361. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1362. break;
  1363. case AV_OPT_TYPE_STRING:
  1364. range->component_min = 0;
  1365. range->component_max = 0x10FFFF; // max unicode value
  1366. range->value_min = -1;
  1367. range->value_max = INT_MAX;
  1368. break;
  1369. case AV_OPT_TYPE_RATIONAL:
  1370. range->component_min = INT_MIN;
  1371. range->component_max = INT_MAX;
  1372. break;
  1373. case AV_OPT_TYPE_IMAGE_SIZE:
  1374. range->component_min = 0;
  1375. range->component_max = INT_MAX/128/8;
  1376. range->value_min = 0;
  1377. range->value_max = INT_MAX/8;
  1378. break;
  1379. case AV_OPT_TYPE_VIDEO_RATE:
  1380. range->component_min = 1;
  1381. range->component_max = INT_MAX;
  1382. range->value_min = 1;
  1383. range->value_max = INT_MAX;
  1384. break;
  1385. default:
  1386. ret = AVERROR(ENOSYS);
  1387. goto fail;
  1388. }
  1389. *ranges_arg = ranges;
  1390. return 0;
  1391. fail:
  1392. av_free(ranges);
  1393. av_free(range);
  1394. av_free(range_array);
  1395. return ret;
  1396. }
  1397. void av_opt_freep_ranges(AVOptionRanges **rangesp)
  1398. {
  1399. int i;
  1400. AVOptionRanges *ranges = *rangesp;
  1401. for (i = 0; i < ranges->nb_ranges; i++) {
  1402. AVOptionRange *range = ranges->range[i];
  1403. av_freep(&range->str);
  1404. av_freep(&ranges->range[i]);
  1405. }
  1406. av_freep(&ranges->range);
  1407. av_freep(rangesp);
  1408. }
  1409. #ifdef TEST
  1410. typedef struct TestContext
  1411. {
  1412. const AVClass *class;
  1413. int num;
  1414. int toggle;
  1415. char *string;
  1416. int flags;
  1417. AVRational rational;
  1418. AVRational video_rate;
  1419. int w, h;
  1420. enum AVPixelFormat pix_fmt;
  1421. enum AVSampleFormat sample_fmt;
  1422. int64_t duration;
  1423. uint8_t color[4];
  1424. int64_t channel_layout;
  1425. } TestContext;
  1426. #define OFFSET(x) offsetof(TestContext, x)
  1427. #define TEST_FLAG_COOL 01
  1428. #define TEST_FLAG_LAME 02
  1429. #define TEST_FLAG_MU 04
  1430. static const AVOption test_options[]= {
  1431. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  1432. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  1433. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  1434. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
  1435. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  1436. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  1437. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  1438. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  1439. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
  1440. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX},
  1441. {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, INT_MAX},
  1442. {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
  1443. {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
  1444. {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
  1445. {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
  1446. {NULL},
  1447. };
  1448. static const char *test_get_name(void *ctx)
  1449. {
  1450. return "test";
  1451. }
  1452. static const AVClass test_class = {
  1453. "TestContext",
  1454. test_get_name,
  1455. test_options
  1456. };
  1457. int main(void)
  1458. {
  1459. int i;
  1460. printf("\nTesting av_set_options_string()\n");
  1461. {
  1462. TestContext test_ctx = { 0 };
  1463. static const char * const options[] = {
  1464. "",
  1465. ":",
  1466. "=",
  1467. "foo=:",
  1468. ":=foo",
  1469. "=foo",
  1470. "foo=",
  1471. "foo",
  1472. "foo=val",
  1473. "foo==val",
  1474. "toggle=:",
  1475. "string=:",
  1476. "toggle=1 : foo",
  1477. "toggle=100",
  1478. "toggle==1",
  1479. "flags=+mu-lame : num=42: toggle=0",
  1480. "num=42 : string=blahblah",
  1481. "rational=0 : rational=1/2 : rational=1/-1",
  1482. "rational=-1/0",
  1483. "size=1024x768",
  1484. "size=pal",
  1485. "size=bogus",
  1486. "pix_fmt=yuv420p",
  1487. "pix_fmt=2",
  1488. "pix_fmt=bogus",
  1489. "sample_fmt=s16",
  1490. "sample_fmt=2",
  1491. "sample_fmt=bogus",
  1492. "video_rate=pal",
  1493. "video_rate=25",
  1494. "video_rate=30000/1001",
  1495. "video_rate=30/1.001",
  1496. "video_rate=bogus",
  1497. "duration=bogus",
  1498. "duration=123.45",
  1499. "duration=1\\:23\\:45.67",
  1500. "color=blue",
  1501. "color=0x223300",
  1502. "color=0x42FF07AA",
  1503. "cl=stereo+downmix",
  1504. "cl=foo",
  1505. };
  1506. test_ctx.class = &test_class;
  1507. av_opt_set_defaults(&test_ctx);
  1508. av_log_set_level(AV_LOG_DEBUG);
  1509. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1510. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1511. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  1512. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1513. printf("\n");
  1514. }
  1515. av_opt_free(&test_ctx);
  1516. }
  1517. printf("\nTesting av_opt_set_from_string()\n");
  1518. {
  1519. TestContext test_ctx = { 0 };
  1520. static const char * const options[] = {
  1521. "",
  1522. "5",
  1523. "5:hello",
  1524. "5:hello:size=pal",
  1525. "5:size=pal:hello",
  1526. ":",
  1527. "=",
  1528. " 5 : hello : size = pal ",
  1529. "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
  1530. };
  1531. static const char * const shorthand[] = { "num", "string", NULL };
  1532. test_ctx.class = &test_class;
  1533. av_opt_set_defaults(&test_ctx);
  1534. av_log_set_level(AV_LOG_DEBUG);
  1535. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1536. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1537. if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
  1538. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1539. printf("\n");
  1540. }
  1541. av_opt_free(&test_ctx);
  1542. }
  1543. return 0;
  1544. }
  1545. #endif