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.

1435 lines
46KB

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