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.

1672 lines
54KB

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