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.

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