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.

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