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.

1524 lines
49KB

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