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.

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