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.

1143 lines
36KB

  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. #if FF_API_FIND_OPT
  38. //FIXME order them and do a bin search
  39. const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
  40. {
  41. const AVOption *o = NULL;
  42. while ((o = av_next_option(v, o))) {
  43. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
  44. return o;
  45. }
  46. return NULL;
  47. }
  48. #endif
  49. #if FF_API_OLD_AVOPTIONS
  50. const AVOption *av_next_option(void *obj, const AVOption *last)
  51. {
  52. return av_opt_next(obj, last);
  53. }
  54. #endif
  55. const AVOption *av_opt_next(void *obj, const AVOption *last)
  56. {
  57. AVClass *class = *(AVClass**)obj;
  58. if (!last && class->option && class->option[0].name)
  59. return class->option;
  60. if (last && last[1].name) return ++last;
  61. return NULL;
  62. }
  63. static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
  64. {
  65. switch (o->type) {
  66. case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
  67. case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
  68. case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
  69. case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
  70. case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
  71. case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
  72. *den = ((AVRational*)dst)->den;
  73. return 0;
  74. case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
  75. }
  76. return AVERROR(EINVAL);
  77. }
  78. static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  79. {
  80. if (o->max*den < num*intnum || o->min*den > num*intnum) {
  81. av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
  82. num*intnum/den, o->name);
  83. return AVERROR(ERANGE);
  84. }
  85. switch (o->type) {
  86. case AV_OPT_TYPE_FLAGS:
  87. case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  88. case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  89. case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  90. case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  91. case AV_OPT_TYPE_RATIONAL:
  92. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  93. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  94. break;
  95. default:
  96. return AVERROR(EINVAL);
  97. }
  98. return 0;
  99. }
  100. static const double const_values[] = {
  101. M_PI,
  102. M_E,
  103. FF_QP2LAMBDA,
  104. 0
  105. };
  106. static const char * const const_names[] = {
  107. "PI",
  108. "E",
  109. "QP2LAMBDA",
  110. 0
  111. };
  112. static int hexchar2int(char c) {
  113. if (c >= '0' && c <= '9') return c - '0';
  114. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  115. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  116. return -1;
  117. }
  118. static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  119. {
  120. int *lendst = (int *)(dst + 1);
  121. uint8_t *bin, *ptr;
  122. int len = strlen(val);
  123. av_freep(dst);
  124. *lendst = 0;
  125. if (len & 1)
  126. return AVERROR(EINVAL);
  127. len /= 2;
  128. ptr = bin = av_malloc(len);
  129. while (*val) {
  130. int a = hexchar2int(*val++);
  131. int b = hexchar2int(*val++);
  132. if (a < 0 || b < 0) {
  133. av_free(bin);
  134. return AVERROR(EINVAL);
  135. }
  136. *ptr++ = (a << 4) | b;
  137. }
  138. *dst = bin;
  139. *lendst = len;
  140. return 0;
  141. }
  142. static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  143. {
  144. av_freep(dst);
  145. *dst = av_strdup(val);
  146. return 0;
  147. }
  148. #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
  149. opt->type == AV_OPT_TYPE_CONST || \
  150. opt->type == AV_OPT_TYPE_FLAGS || \
  151. opt->type == AV_OPT_TYPE_INT) ? \
  152. opt->default_val.i64 : opt->default_val.dbl)
  153. static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
  154. {
  155. int ret = 0, notfirst = 0;
  156. for (;;) {
  157. int i, den = 1;
  158. char buf[256];
  159. int cmd = 0;
  160. double d, num = 1;
  161. int64_t intnum = 1;
  162. if (*val == '+' || *val == '-')
  163. cmd = *(val++);
  164. for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  165. buf[i] = val[i];
  166. buf[i] = 0;
  167. {
  168. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  169. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  170. d = DEFAULT_NUMVAL(o_named);
  171. else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
  172. else if (!strcmp(buf, "max" )) d = o->max;
  173. else if (!strcmp(buf, "min" )) d = o->min;
  174. else if (!strcmp(buf, "none" )) d = 0;
  175. else if (!strcmp(buf, "all" )) d = ~0;
  176. else {
  177. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  178. if (res < 0) {
  179. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  180. return res;
  181. }
  182. }
  183. }
  184. if (o->type == AV_OPT_TYPE_FLAGS) {
  185. read_number(o, dst, NULL, NULL, &intnum);
  186. if (cmd == '+') d = intnum | (int64_t)d;
  187. else if (cmd == '-') d = intnum &~(int64_t)d;
  188. } else {
  189. read_number(o, dst, &num, &den, &intnum);
  190. if (cmd == '+') d = notfirst*num*intnum/den + d;
  191. else if (cmd == '-') d = notfirst*num*intnum/den - d;
  192. }
  193. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  194. return ret;
  195. val += i;
  196. if (!*val)
  197. return 0;
  198. notfirst = 1;
  199. }
  200. return 0;
  201. }
  202. #if FF_API_OLD_AVOPTIONS
  203. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  204. {
  205. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  206. if (o_out)
  207. *o_out = o;
  208. return av_opt_set(obj, name, val, 0);
  209. }
  210. #endif
  211. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  212. {
  213. int ret;
  214. void *dst, *target_obj;
  215. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  216. if (!o || !target_obj)
  217. return AVERROR_OPTION_NOT_FOUND;
  218. if (!val && (o->type != AV_OPT_TYPE_STRING &&
  219. o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
  220. o->type != AV_OPT_TYPE_IMAGE_SIZE))
  221. return AVERROR(EINVAL);
  222. dst = ((uint8_t*)target_obj) + o->offset;
  223. switch (o->type) {
  224. case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  225. case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  226. case AV_OPT_TYPE_FLAGS:
  227. case AV_OPT_TYPE_INT:
  228. case AV_OPT_TYPE_INT64:
  229. case AV_OPT_TYPE_FLOAT:
  230. case AV_OPT_TYPE_DOUBLE:
  231. case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  232. case AV_OPT_TYPE_IMAGE_SIZE:
  233. if (!val || !strcmp(val, "none")) {
  234. *(int *)dst = *((int *)dst + 1) = 0;
  235. return 0;
  236. }
  237. ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
  238. if (ret < 0)
  239. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
  240. return ret;
  241. case AV_OPT_TYPE_PIXEL_FMT:
  242. if (!val || !strcmp(val, "none")) {
  243. ret = AV_PIX_FMT_NONE;
  244. } else {
  245. ret = av_get_pix_fmt(val);
  246. if (ret == AV_PIX_FMT_NONE) {
  247. char *tail;
  248. ret = strtol(val, &tail, 0);
  249. if (*tail || (unsigned)ret >= AV_PIX_FMT_NB) {
  250. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
  251. return AVERROR(EINVAL);
  252. }
  253. }
  254. }
  255. *(enum AVPixelFormat *)dst = ret;
  256. return 0;
  257. case AV_OPT_TYPE_SAMPLE_FMT:
  258. if (!val || !strcmp(val, "none")) {
  259. ret = AV_SAMPLE_FMT_NONE;
  260. } else {
  261. ret = av_get_sample_fmt(val);
  262. if (ret == AV_SAMPLE_FMT_NONE) {
  263. char *tail;
  264. ret = strtol(val, &tail, 0);
  265. if (*tail || (unsigned)ret >= AV_SAMPLE_FMT_NB) {
  266. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as sample format\n", val);
  267. return AVERROR(EINVAL);
  268. }
  269. }
  270. }
  271. *(enum AVSampleFormat *)dst = ret;
  272. return 0;
  273. }
  274. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  275. return AVERROR(EINVAL);
  276. }
  277. #define OPT_EVAL_NUMBER(name, opttype, vartype)\
  278. int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
  279. {\
  280. if (!o || o->type != opttype)\
  281. return AVERROR(EINVAL);\
  282. return set_string_number(obj, o, val, name ## _out);\
  283. }
  284. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  285. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  286. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  287. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  288. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  289. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  290. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  291. int search_flags)
  292. {
  293. void *dst, *target_obj;
  294. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  295. if (!o || !target_obj)
  296. return AVERROR_OPTION_NOT_FOUND;
  297. dst = ((uint8_t*)target_obj) + o->offset;
  298. return write_number(obj, o, dst, num, den, intnum);
  299. }
  300. #if FF_API_OLD_AVOPTIONS
  301. const AVOption *av_set_double(void *obj, const char *name, double n)
  302. {
  303. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  304. if (set_number(obj, name, n, 1, 1, 0) < 0)
  305. return NULL;
  306. return o;
  307. }
  308. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  309. {
  310. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  311. if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
  312. return NULL;
  313. return o;
  314. }
  315. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  316. {
  317. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  318. if (set_number(obj, name, 1, 1, n, 0) < 0)
  319. return NULL;
  320. return o;
  321. }
  322. #endif
  323. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  324. {
  325. return set_number(obj, name, 1, 1, val, search_flags);
  326. }
  327. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  328. {
  329. return set_number(obj, name, val, 1, 1, search_flags);
  330. }
  331. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  332. {
  333. return set_number(obj, name, val.num, val.den, 1, search_flags);
  334. }
  335. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  336. {
  337. void *target_obj;
  338. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  339. uint8_t *ptr;
  340. uint8_t **dst;
  341. int *lendst;
  342. if (!o || !target_obj)
  343. return AVERROR_OPTION_NOT_FOUND;
  344. if (o->type != AV_OPT_TYPE_BINARY)
  345. return AVERROR(EINVAL);
  346. ptr = av_malloc(len);
  347. if (!ptr)
  348. return AVERROR(ENOMEM);
  349. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  350. lendst = (int *)(dst + 1);
  351. av_free(*dst);
  352. *dst = ptr;
  353. *lendst = len;
  354. memcpy(ptr, val, len);
  355. return 0;
  356. }
  357. #if FF_API_OLD_AVOPTIONS
  358. /**
  359. *
  360. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  361. * @param buf_len allocated length in bytes of buf
  362. */
  363. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  364. {
  365. const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  366. void *dst;
  367. uint8_t *bin;
  368. int len, i;
  369. if (!o)
  370. return NULL;
  371. if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
  372. return NULL;
  373. dst= ((uint8_t*)obj) + o->offset;
  374. if (o_out) *o_out= o;
  375. switch (o->type) {
  376. case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  377. case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  378. case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  379. case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  380. case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  381. case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  382. case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
  383. case AV_OPT_TYPE_STRING: return *(void**)dst;
  384. case AV_OPT_TYPE_BINARY:
  385. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  386. if (len >= (buf_len + 1)/2) return NULL;
  387. bin = *(uint8_t**)dst;
  388. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  389. break;
  390. default: return NULL;
  391. }
  392. return buf;
  393. }
  394. #endif
  395. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  396. {
  397. void *dst, *target_obj;
  398. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  399. uint8_t *bin, buf[128];
  400. int len, i, ret;
  401. if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
  402. return AVERROR_OPTION_NOT_FOUND;
  403. dst = (uint8_t*)target_obj + o->offset;
  404. buf[0] = 0;
  405. switch (o->type) {
  406. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  407. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  408. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  409. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  410. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  411. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  412. case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
  413. case AV_OPT_TYPE_STRING:
  414. if (*(uint8_t**)dst)
  415. *out_val = av_strdup(*(uint8_t**)dst);
  416. else
  417. *out_val = av_strdup("");
  418. return 0;
  419. case AV_OPT_TYPE_BINARY:
  420. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  421. if ((uint64_t)len*2 + 1 > INT_MAX)
  422. return AVERROR(EINVAL);
  423. if (!(*out_val = av_malloc(len*2 + 1)))
  424. return AVERROR(ENOMEM);
  425. bin = *(uint8_t**)dst;
  426. for (i = 0; i < len; i++)
  427. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  428. return 0;
  429. case AV_OPT_TYPE_IMAGE_SIZE:
  430. ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
  431. break;
  432. case AV_OPT_TYPE_PIXEL_FMT:
  433. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
  434. break;
  435. case AV_OPT_TYPE_SAMPLE_FMT:
  436. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
  437. break;
  438. default:
  439. return AVERROR(EINVAL);
  440. }
  441. if (ret >= sizeof(buf))
  442. return AVERROR(EINVAL);
  443. *out_val = av_strdup(buf);
  444. return 0;
  445. }
  446. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  447. int search_flags)
  448. {
  449. void *dst, *target_obj;
  450. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  451. if (!o || !target_obj)
  452. goto error;
  453. dst = ((uint8_t*)target_obj) + o->offset;
  454. if (o_out) *o_out= o;
  455. return read_number(o, dst, num, den, intnum);
  456. error:
  457. *den=*intnum=0;
  458. return -1;
  459. }
  460. #if FF_API_OLD_AVOPTIONS
  461. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  462. {
  463. int64_t intnum=1;
  464. double num=1;
  465. int den=1;
  466. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  467. return NAN;
  468. return num*intnum/den;
  469. }
  470. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  471. {
  472. int64_t intnum=1;
  473. double num=1;
  474. int den=1;
  475. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  476. return (AVRational){0, 0};
  477. if (num == 1.0 && (int)intnum == intnum)
  478. return (AVRational){intnum, den};
  479. else
  480. return av_d2q(num*intnum/den, 1<<24);
  481. }
  482. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  483. {
  484. int64_t intnum=1;
  485. double num=1;
  486. int den=1;
  487. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  488. return -1;
  489. return num*intnum/den;
  490. }
  491. #endif
  492. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  493. {
  494. int64_t intnum = 1;
  495. double num = 1;
  496. int ret, den = 1;
  497. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  498. return ret;
  499. *out_val = num*intnum/den;
  500. return 0;
  501. }
  502. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  503. {
  504. int64_t intnum = 1;
  505. double num = 1;
  506. int ret, den = 1;
  507. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  508. return ret;
  509. *out_val = num*intnum/den;
  510. return 0;
  511. }
  512. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  513. {
  514. int64_t intnum = 1;
  515. double num = 1;
  516. int ret, den = 1;
  517. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  518. return ret;
  519. if (num == 1.0 && (int)intnum == intnum)
  520. *out_val = (AVRational){intnum, den};
  521. else
  522. *out_val = av_d2q(num*intnum/den, 1<<24);
  523. return 0;
  524. }
  525. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  526. {
  527. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  528. const AVOption *flag = av_opt_find(obj, flag_name,
  529. field ? field->unit : NULL, 0, 0);
  530. int64_t res;
  531. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  532. av_opt_get_int(obj, field_name, 0, &res) < 0)
  533. return 0;
  534. return res & flag->default_val.i64;
  535. }
  536. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  537. int req_flags, int rej_flags)
  538. {
  539. const AVOption *opt=NULL;
  540. while ((opt = av_opt_next(obj, opt))) {
  541. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  542. continue;
  543. /* Don't print CONST's on level one.
  544. * Don't print anything but CONST's on level two.
  545. * Only print items from the requested unit.
  546. */
  547. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  548. continue;
  549. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  550. continue;
  551. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  552. continue;
  553. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  554. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  555. else
  556. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  557. switch (opt->type) {
  558. case AV_OPT_TYPE_FLAGS:
  559. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  560. break;
  561. case AV_OPT_TYPE_INT:
  562. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  563. break;
  564. case AV_OPT_TYPE_INT64:
  565. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  566. break;
  567. case AV_OPT_TYPE_DOUBLE:
  568. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  569. break;
  570. case AV_OPT_TYPE_FLOAT:
  571. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  572. break;
  573. case AV_OPT_TYPE_STRING:
  574. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  575. break;
  576. case AV_OPT_TYPE_RATIONAL:
  577. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  578. break;
  579. case AV_OPT_TYPE_BINARY:
  580. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  581. break;
  582. case AV_OPT_TYPE_IMAGE_SIZE:
  583. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<image_size>");
  584. break;
  585. case AV_OPT_TYPE_PIXEL_FMT:
  586. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<pix_fmt>");
  587. break;
  588. case AV_OPT_TYPE_SAMPLE_FMT:
  589. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<sample_fmt>");
  590. break;
  591. case AV_OPT_TYPE_CONST:
  592. default:
  593. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  594. break;
  595. }
  596. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  597. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  598. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
  599. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  600. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  601. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  602. if (opt->help)
  603. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  604. av_log(av_log_obj, AV_LOG_INFO, "\n");
  605. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  606. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  607. }
  608. }
  609. }
  610. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  611. {
  612. if (!obj)
  613. return -1;
  614. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  615. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  616. return 0;
  617. }
  618. void av_opt_set_defaults(void *s)
  619. {
  620. #if FF_API_OLD_AVOPTIONS
  621. av_opt_set_defaults2(s, 0, 0);
  622. }
  623. void av_opt_set_defaults2(void *s, int mask, int flags)
  624. {
  625. #endif
  626. const AVOption *opt = NULL;
  627. while ((opt = av_opt_next(s, opt)) != NULL) {
  628. #if FF_API_OLD_AVOPTIONS
  629. if ((opt->flags & mask) != flags)
  630. continue;
  631. #endif
  632. switch (opt->type) {
  633. case AV_OPT_TYPE_CONST:
  634. /* Nothing to be done here */
  635. break;
  636. case AV_OPT_TYPE_FLAGS:
  637. case AV_OPT_TYPE_INT:
  638. case AV_OPT_TYPE_INT64:
  639. av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
  640. break;
  641. case AV_OPT_TYPE_DOUBLE:
  642. case AV_OPT_TYPE_FLOAT: {
  643. double val;
  644. val = opt->default_val.dbl;
  645. av_opt_set_double(s, opt->name, val, 0);
  646. }
  647. break;
  648. case AV_OPT_TYPE_RATIONAL: {
  649. AVRational val;
  650. val = av_d2q(opt->default_val.dbl, INT_MAX);
  651. av_opt_set_q(s, opt->name, val, 0);
  652. }
  653. break;
  654. case AV_OPT_TYPE_STRING:
  655. case AV_OPT_TYPE_IMAGE_SIZE:
  656. case AV_OPT_TYPE_PIXEL_FMT:
  657. case AV_OPT_TYPE_SAMPLE_FMT:
  658. av_opt_set(s, opt->name, opt->default_val.str, 0);
  659. break;
  660. case AV_OPT_TYPE_BINARY:
  661. /* Cannot set default for binary */
  662. break;
  663. default:
  664. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  665. }
  666. }
  667. }
  668. /**
  669. * Store the value in the field in ctx that is named like key.
  670. * ctx must be an AVClass context, storing is done using AVOptions.
  671. *
  672. * @param buf the string to parse, buf will be updated to point at the
  673. * separator just after the parsed key/value pair
  674. * @param key_val_sep a 0-terminated list of characters used to
  675. * separate key from value
  676. * @param pairs_sep a 0-terminated list of characters used to separate
  677. * two pairs from each other
  678. * @return 0 if the key/value pair has been successfully parsed and
  679. * set, or a negative value corresponding to an AVERROR code in case
  680. * of error:
  681. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  682. * the error code issued by av_opt_set() if the key/value pair
  683. * cannot be set
  684. */
  685. static int parse_key_value_pair(void *ctx, const char **buf,
  686. const char *key_val_sep, const char *pairs_sep)
  687. {
  688. char *key = av_get_token(buf, key_val_sep);
  689. char *val;
  690. int ret;
  691. if (*key && strspn(*buf, key_val_sep)) {
  692. (*buf)++;
  693. val = av_get_token(buf, pairs_sep);
  694. } else {
  695. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  696. av_free(key);
  697. return AVERROR(EINVAL);
  698. }
  699. av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
  700. ret = av_opt_set(ctx, key, val, 0);
  701. if (ret == AVERROR_OPTION_NOT_FOUND)
  702. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  703. av_free(key);
  704. av_free(val);
  705. return ret;
  706. }
  707. int av_set_options_string(void *ctx, const char *opts,
  708. const char *key_val_sep, const char *pairs_sep)
  709. {
  710. int ret, count = 0;
  711. if (!opts)
  712. return 0;
  713. while (*opts) {
  714. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  715. return ret;
  716. count++;
  717. if (*opts)
  718. opts++;
  719. }
  720. return count;
  721. }
  722. #define WHITESPACES " \n\t"
  723. static int is_key_char(char c)
  724. {
  725. return (unsigned)((c | 32) - 'a') < 26 ||
  726. (unsigned)(c - '0') < 10 ||
  727. c == '-' || c == '_' || c == '/' || c == '.';
  728. }
  729. /**
  730. * Read a key from a string.
  731. *
  732. * The key consists of is_key_char characters and must be terminated by a
  733. * character from the delim string; spaces are ignored.
  734. *
  735. * @return 0 for success (even with ellipsis), <0 for failure
  736. */
  737. static int get_key(const char **ropts, const char *delim, char **rkey)
  738. {
  739. const char *opts = *ropts;
  740. const char *key_start, *key_end;
  741. key_start = opts += strspn(opts, WHITESPACES);
  742. while (is_key_char(*opts))
  743. opts++;
  744. key_end = opts;
  745. opts += strspn(opts, WHITESPACES);
  746. if (!*opts || !strchr(delim, *opts))
  747. return AVERROR(EINVAL);
  748. opts++;
  749. if (!(*rkey = av_malloc(key_end - key_start + 1)))
  750. return AVERROR(ENOMEM);
  751. memcpy(*rkey, key_start, key_end - key_start);
  752. (*rkey)[key_end - key_start] = 0;
  753. *ropts = opts;
  754. return 0;
  755. }
  756. int av_opt_set_from_string(void *ctx, const char *opts,
  757. const char *const *shorthand,
  758. const char *key_val_sep, const char *pairs_sep)
  759. {
  760. int ret, count = 0;
  761. const char *dummy_shorthand = NULL;
  762. char *parsed_key, *value;
  763. const char *key;
  764. if (!opts)
  765. return 0;
  766. if (!shorthand)
  767. shorthand = &dummy_shorthand;
  768. while (*opts) {
  769. parsed_key = NULL; /* so we can free it anyway */
  770. if ((ret = get_key(&opts, key_val_sep, &parsed_key)) < 0) {
  771. if (*shorthand) {
  772. key = *(shorthand++);
  773. } else {
  774. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
  775. return AVERROR(EINVAL);
  776. }
  777. } else {
  778. key = parsed_key;
  779. while (*shorthand) /* discard all remaining shorthand */
  780. shorthand++;
  781. }
  782. if (!(value = av_get_token(&opts, pairs_sep)))
  783. return AVERROR(ENOMEM);
  784. if (*opts && strchr(pairs_sep, *opts))
  785. opts++;
  786. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  787. if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
  788. if (ret == AVERROR_OPTION_NOT_FOUND)
  789. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  790. av_free(value);
  791. av_free(parsed_key);
  792. return ret;
  793. }
  794. av_free(value);
  795. av_free(parsed_key);
  796. count++;
  797. }
  798. return count;
  799. }
  800. void av_opt_free(void *obj)
  801. {
  802. const AVOption *o = NULL;
  803. while ((o = av_opt_next(obj, o)))
  804. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  805. av_freep((uint8_t *)obj + o->offset);
  806. }
  807. int av_opt_set_dict(void *obj, AVDictionary **options)
  808. {
  809. AVDictionaryEntry *t = NULL;
  810. AVDictionary *tmp = NULL;
  811. int ret = 0;
  812. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  813. ret = av_opt_set(obj, t->key, t->value, 0);
  814. if (ret == AVERROR_OPTION_NOT_FOUND)
  815. av_dict_set(&tmp, t->key, t->value, 0);
  816. else if (ret < 0) {
  817. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  818. break;
  819. }
  820. ret = 0;
  821. }
  822. av_dict_free(options);
  823. *options = tmp;
  824. return ret;
  825. }
  826. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  827. int opt_flags, int search_flags)
  828. {
  829. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  830. }
  831. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  832. int opt_flags, int search_flags, void **target_obj)
  833. {
  834. const AVClass *c;
  835. const AVOption *o = NULL;
  836. if(!obj)
  837. return NULL;
  838. c= *(AVClass**)obj;
  839. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  840. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  841. const AVClass *child = NULL;
  842. while (child = av_opt_child_class_next(c, child))
  843. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  844. return o;
  845. } else {
  846. void *child = NULL;
  847. while (child = av_opt_child_next(obj, child))
  848. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  849. return o;
  850. }
  851. }
  852. while (o = av_opt_next(obj, o)) {
  853. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  854. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  855. (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
  856. if (target_obj) {
  857. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  858. *target_obj = obj;
  859. else
  860. *target_obj = NULL;
  861. }
  862. return o;
  863. }
  864. }
  865. return NULL;
  866. }
  867. void *av_opt_child_next(void *obj, void *prev)
  868. {
  869. const AVClass *c = *(AVClass**)obj;
  870. if (c->child_next)
  871. return c->child_next(obj, prev);
  872. return NULL;
  873. }
  874. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  875. {
  876. if (parent->child_class_next)
  877. return parent->child_class_next(prev);
  878. return NULL;
  879. }
  880. void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
  881. {
  882. const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
  883. if(!opt)
  884. return NULL;
  885. return (uint8_t*)obj + opt->offset;
  886. }
  887. #ifdef TEST
  888. #undef printf
  889. typedef struct TestContext
  890. {
  891. const AVClass *class;
  892. int num;
  893. int toggle;
  894. char *string;
  895. int flags;
  896. AVRational rational;
  897. int w, h;
  898. enum AVPixelFormat pix_fmt;
  899. enum AVSampleFormat sample_fmt;
  900. } TestContext;
  901. #define OFFSET(x) offsetof(TestContext, x)
  902. #define TEST_FLAG_COOL 01
  903. #define TEST_FLAG_LAME 02
  904. #define TEST_FLAG_MU 04
  905. static const AVOption test_options[]= {
  906. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  907. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  908. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  909. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  910. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  911. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  912. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  913. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  914. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
  915. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT,{0}, 0, 0 },
  916. {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT,{0}, 0, 0 },
  917. {NULL},
  918. };
  919. static const char *test_get_name(void *ctx)
  920. {
  921. return "test";
  922. }
  923. static const AVClass test_class = {
  924. "TestContext",
  925. test_get_name,
  926. test_options
  927. };
  928. int main(void)
  929. {
  930. int i;
  931. printf("\nTesting av_set_options_string()\n");
  932. {
  933. TestContext test_ctx = { 0 };
  934. const char *options[] = {
  935. "",
  936. ":",
  937. "=",
  938. "foo=:",
  939. ":=foo",
  940. "=foo",
  941. "foo=",
  942. "foo",
  943. "foo=val",
  944. "foo==val",
  945. "toggle=:",
  946. "string=:",
  947. "toggle=1 : foo",
  948. "toggle=100",
  949. "toggle==1",
  950. "flags=+mu-lame : num=42: toggle=0",
  951. "num=42 : string=blahblah",
  952. "rational=0 : rational=1/2 : rational=1/-1",
  953. "rational=-1/0",
  954. "size=1024x768",
  955. "size=pal",
  956. "size=bogus",
  957. "pix_fmt=yuv420p",
  958. "pix_fmt=2",
  959. "pix_fmt=bogus",
  960. "sample_fmt=s16",
  961. "sample_fmt=2",
  962. "sample_fmt=bogus",
  963. };
  964. test_ctx.class = &test_class;
  965. av_opt_set_defaults(&test_ctx);
  966. test_ctx.string = av_strdup("default");
  967. av_log_set_level(AV_LOG_DEBUG);
  968. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  969. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  970. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  971. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  972. printf("\n");
  973. }
  974. av_freep(&test_ctx.string);
  975. }
  976. printf("\nTesting av_opt_set_from_string()\n");
  977. {
  978. TestContext test_ctx = { 0 };
  979. const char *options[] = {
  980. "",
  981. "5",
  982. "5:hello",
  983. "5:hello:size=pal",
  984. "5:size=pal:hello",
  985. ":",
  986. "=",
  987. " 5 : hello : size = pal ",
  988. "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
  989. };
  990. const char *shorthand[] = { "num", "string", NULL };
  991. test_ctx.class = &test_class;
  992. av_opt_set_defaults(&test_ctx);
  993. test_ctx.string = av_strdup("default");
  994. av_log_set_level(AV_LOG_DEBUG);
  995. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  996. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  997. if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
  998. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  999. printf("\n");
  1000. }
  1001. av_freep(&test_ctx.string);
  1002. }
  1003. return 0;
  1004. }
  1005. #endif