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.

1699 lines
55KB

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