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.

923 lines
28KB

  1. /*
  2. * AVOptions
  3. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "opt.h"
  29. #include "eval.h"
  30. #include "dict.h"
  31. #include "log.h"
  32. #include "mathematics.h"
  33. #if FF_API_FIND_OPT
  34. //FIXME order them and do a bin search
  35. const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
  36. {
  37. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  38. const AVOption *o= c->option;
  39. for (; o && o->name; o++) {
  40. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
  41. return o;
  42. }
  43. return NULL;
  44. }
  45. #endif
  46. #if FF_API_OLD_AVOPTIONS
  47. const AVOption *av_next_option(void *obj, const AVOption *last)
  48. {
  49. return av_opt_next(obj, last);
  50. }
  51. #endif
  52. const AVOption *av_opt_next(void *obj, const AVOption *last)
  53. {
  54. AVClass *class = *(AVClass**)obj;
  55. if (!last && class->option[0].name) return class->option;
  56. if (last && last[1].name) return ++last;
  57. return NULL;
  58. }
  59. static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
  60. {
  61. switch (o->type) {
  62. case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
  63. case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
  64. case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
  65. case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
  66. case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
  67. case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
  68. *den = ((AVRational*)dst)->den;
  69. return 0;
  70. }
  71. return AVERROR(EINVAL);
  72. }
  73. static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  74. {
  75. if (o->max*den < num*intnum || o->min*den > num*intnum) {
  76. av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n",
  77. num*intnum/den, o->name);
  78. return AVERROR(ERANGE);
  79. }
  80. switch (o->type) {
  81. case AV_OPT_TYPE_FLAGS:
  82. case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  83. case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  84. case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  85. case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  86. case AV_OPT_TYPE_RATIONAL:
  87. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  88. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  89. break;
  90. default:
  91. return AVERROR(EINVAL);
  92. }
  93. return 0;
  94. }
  95. static const double const_values[] = {
  96. M_PI,
  97. M_E,
  98. FF_QP2LAMBDA,
  99. 0
  100. };
  101. static const char * const const_names[] = {
  102. "PI",
  103. "E",
  104. "QP2LAMBDA",
  105. 0
  106. };
  107. static int hexchar2int(char c) {
  108. if (c >= '0' && c <= '9') return c - '0';
  109. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  110. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  111. return -1;
  112. }
  113. static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  114. {
  115. int *lendst = (int *)(dst + 1);
  116. uint8_t *bin, *ptr;
  117. int len = strlen(val);
  118. av_freep(dst);
  119. *lendst = 0;
  120. if (len & 1)
  121. return AVERROR(EINVAL);
  122. len /= 2;
  123. ptr = bin = av_malloc(len);
  124. while (*val) {
  125. int a = hexchar2int(*val++);
  126. int b = hexchar2int(*val++);
  127. if (a < 0 || b < 0) {
  128. av_free(bin);
  129. return AVERROR(EINVAL);
  130. }
  131. *ptr++ = (a << 4) | b;
  132. }
  133. *dst = bin;
  134. *lendst = len;
  135. return 0;
  136. }
  137. static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  138. {
  139. av_freep(dst);
  140. *dst = av_strdup(val);
  141. return 0;
  142. }
  143. static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
  144. {
  145. int ret = 0, notfirst = 0;
  146. for (;;) {
  147. int i, den = 1;
  148. char buf[256];
  149. int cmd = 0;
  150. double d, num = 1;
  151. int64_t intnum = 1;
  152. if (*val == '+' || *val == '-')
  153. cmd = *(val++);
  154. for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  155. buf[i] = val[i];
  156. buf[i] = 0;
  157. {
  158. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  159. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  160. d = o_named->default_val.dbl;
  161. else if (!strcmp(buf, "default")) d = o->default_val.dbl;
  162. else if (!strcmp(buf, "max" )) d = o->max;
  163. else if (!strcmp(buf, "min" )) d = o->min;
  164. else if (!strcmp(buf, "none" )) d = 0;
  165. else if (!strcmp(buf, "all" )) d = ~0;
  166. else {
  167. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  168. if (res < 0) {
  169. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  170. return res;
  171. }
  172. }
  173. }
  174. if (o->type == AV_OPT_TYPE_FLAGS) {
  175. read_number(o, dst, NULL, NULL, &intnum);
  176. if (cmd == '+') d = intnum | (int64_t)d;
  177. else if (cmd == '-') d = intnum &~(int64_t)d;
  178. } else {
  179. read_number(o, dst, &num, &den, &intnum);
  180. if (cmd == '+') d = notfirst*num*intnum/den + d;
  181. else if (cmd == '-') d = notfirst*num*intnum/den - d;
  182. }
  183. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  184. return ret;
  185. val += i;
  186. if (!*val)
  187. return 0;
  188. notfirst = 1;
  189. }
  190. return 0;
  191. }
  192. #if FF_API_OLD_AVOPTIONS
  193. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  194. {
  195. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  196. if (o_out)
  197. *o_out = o;
  198. return av_opt_set(obj, name, val, 0);
  199. }
  200. #endif
  201. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  202. {
  203. void *dst, *target_obj;
  204. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  205. if (!o || !target_obj)
  206. return AVERROR_OPTION_NOT_FOUND;
  207. if (!val)
  208. return AVERROR(EINVAL);
  209. dst = ((uint8_t*)target_obj) + o->offset;
  210. switch (o->type) {
  211. case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  212. case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  213. case AV_OPT_TYPE_FLAGS:
  214. case AV_OPT_TYPE_INT:
  215. case AV_OPT_TYPE_INT64:
  216. case AV_OPT_TYPE_FLOAT:
  217. case AV_OPT_TYPE_DOUBLE:
  218. case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  219. }
  220. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  221. return AVERROR(EINVAL);
  222. }
  223. #define OPT_EVAL_NUMBER(name, opttype, vartype)\
  224. int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
  225. {\
  226. if (!o || o->type != opttype)\
  227. return AVERROR(EINVAL);\
  228. return set_string_number(obj, o, val, name ## _out);\
  229. }
  230. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  231. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  232. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  233. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  234. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  235. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  236. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  237. int search_flags)
  238. {
  239. void *dst, *target_obj;
  240. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  241. if (!o || !target_obj)
  242. return AVERROR_OPTION_NOT_FOUND;
  243. dst = ((uint8_t*)target_obj) + o->offset;
  244. return write_number(obj, o, dst, num, den, intnum);
  245. }
  246. #if FF_API_OLD_AVOPTIONS
  247. const AVOption *av_set_double(void *obj, const char *name, double n)
  248. {
  249. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  250. if (set_number(obj, name, n, 1, 1, 0) < 0)
  251. return NULL;
  252. return o;
  253. }
  254. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  255. {
  256. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  257. if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
  258. return NULL;
  259. return o;
  260. }
  261. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  262. {
  263. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  264. if (set_number(obj, name, 1, 1, n, 0) < 0)
  265. return NULL;
  266. return o;
  267. }
  268. #endif
  269. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  270. {
  271. return set_number(obj, name, 1, 1, val, search_flags);
  272. }
  273. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  274. {
  275. return set_number(obj, name, val, 1, 1, search_flags);
  276. }
  277. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  278. {
  279. return set_number(obj, name, val.num, val.den, 1, search_flags);
  280. }
  281. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  282. {
  283. void *target_obj;
  284. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  285. uint8_t *ptr;
  286. uint8_t **dst;
  287. int *lendst;
  288. if (!o || !target_obj)
  289. return AVERROR_OPTION_NOT_FOUND;
  290. if (o->type != AV_OPT_TYPE_BINARY)
  291. return AVERROR(EINVAL);
  292. ptr = av_malloc(len);
  293. if (!ptr)
  294. return AVERROR(ENOMEM);
  295. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  296. lendst = (int *)(dst + 1);
  297. av_free(*dst);
  298. *dst = ptr;
  299. *lendst = len;
  300. memcpy(ptr, val, len);
  301. return 0;
  302. }
  303. #if FF_API_OLD_AVOPTIONS
  304. /**
  305. *
  306. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  307. * @param buf_len allocated length in bytes of buf
  308. */
  309. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  310. {
  311. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  312. void *dst;
  313. uint8_t *bin;
  314. int len, i;
  315. if (!o)
  316. return NULL;
  317. if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
  318. return NULL;
  319. dst= ((uint8_t*)obj) + o->offset;
  320. if (o_out) *o_out= o;
  321. switch (o->type) {
  322. case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  323. case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  324. case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  325. case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  326. case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  327. case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  328. case AV_OPT_TYPE_STRING: return *(void**)dst;
  329. case AV_OPT_TYPE_BINARY:
  330. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  331. if (len >= (buf_len + 1)/2) return NULL;
  332. bin = *(uint8_t**)dst;
  333. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  334. break;
  335. default: return NULL;
  336. }
  337. return buf;
  338. }
  339. #endif
  340. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  341. {
  342. void *dst, *target_obj;
  343. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  344. uint8_t *bin, buf[128];
  345. int len, i, ret;
  346. if (!o || !target_obj)
  347. return AVERROR_OPTION_NOT_FOUND;
  348. dst = (uint8_t*)target_obj + o->offset;
  349. buf[0] = 0;
  350. switch (o->type) {
  351. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  352. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  353. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  354. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  355. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  356. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  357. case AV_OPT_TYPE_STRING:
  358. if (*(uint8_t**)dst)
  359. *out_val = av_strdup(*(uint8_t**)dst);
  360. else
  361. *out_val = av_strdup("");
  362. return 0;
  363. case AV_OPT_TYPE_BINARY:
  364. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  365. if ((uint64_t)len*2 + 1 > INT_MAX)
  366. return AVERROR(EINVAL);
  367. if (!(*out_val = av_malloc(len*2 + 1)))
  368. return AVERROR(ENOMEM);
  369. bin = *(uint8_t**)dst;
  370. for (i = 0; i < len; i++)
  371. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  372. return 0;
  373. default:
  374. return AVERROR(EINVAL);
  375. }
  376. if (ret >= sizeof(buf))
  377. return AVERROR(EINVAL);
  378. *out_val = av_strdup(buf);
  379. return 0;
  380. }
  381. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  382. int search_flags)
  383. {
  384. void *dst, *target_obj;
  385. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  386. if (!o || !target_obj)
  387. goto error;
  388. dst = ((uint8_t*)target_obj) + o->offset;
  389. if (o_out) *o_out= o;
  390. return read_number(o, dst, num, den, intnum);
  391. error:
  392. *den=*intnum=0;
  393. return -1;
  394. }
  395. #if FF_API_OLD_AVOPTIONS
  396. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  397. {
  398. int64_t intnum=1;
  399. double num=1;
  400. int den=1;
  401. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  402. return NAN;
  403. return num*intnum/den;
  404. }
  405. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  406. {
  407. int64_t intnum=1;
  408. double num=1;
  409. int den=1;
  410. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  411. return (AVRational){0, 0};
  412. if (num == 1.0 && (int)intnum == intnum)
  413. return (AVRational){intnum, den};
  414. else
  415. return av_d2q(num*intnum/den, 1<<24);
  416. }
  417. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  418. {
  419. int64_t intnum=1;
  420. double num=1;
  421. int den=1;
  422. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  423. return -1;
  424. return num*intnum/den;
  425. }
  426. #endif
  427. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  428. {
  429. int64_t intnum = 1;
  430. double num = 1;
  431. int ret, den = 1;
  432. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  433. return ret;
  434. *out_val = num*intnum/den;
  435. return 0;
  436. }
  437. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  438. {
  439. int64_t intnum = 1;
  440. double num = 1;
  441. int ret, den = 1;
  442. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  443. return ret;
  444. *out_val = num*intnum/den;
  445. return 0;
  446. }
  447. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  448. {
  449. int64_t intnum = 1;
  450. double num = 1;
  451. int ret, den = 1;
  452. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  453. return ret;
  454. if (num == 1.0 && (int)intnum == intnum)
  455. *out_val = (AVRational){intnum, den};
  456. else
  457. *out_val = av_d2q(num*intnum/den, 1<<24);
  458. return 0;
  459. }
  460. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  461. {
  462. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  463. const AVOption *flag = av_opt_find(obj, flag_name,
  464. field ? field->unit : NULL, 0, 0);
  465. int64_t res;
  466. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  467. av_opt_get_int(obj, field_name, 0, &res) < 0)
  468. return 0;
  469. return res & (int) flag->default_val.dbl;
  470. }
  471. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  472. int req_flags, int rej_flags)
  473. {
  474. const AVOption *opt=NULL;
  475. while ((opt = av_opt_next(obj, opt))) {
  476. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  477. continue;
  478. /* Don't print CONST's on level one.
  479. * Don't print anything but CONST's on level two.
  480. * Only print items from the requested unit.
  481. */
  482. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  483. continue;
  484. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  485. continue;
  486. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  487. continue;
  488. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  489. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  490. else
  491. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  492. switch (opt->type) {
  493. case AV_OPT_TYPE_FLAGS:
  494. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  495. break;
  496. case AV_OPT_TYPE_INT:
  497. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  498. break;
  499. case AV_OPT_TYPE_INT64:
  500. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  501. break;
  502. case AV_OPT_TYPE_DOUBLE:
  503. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  504. break;
  505. case AV_OPT_TYPE_FLOAT:
  506. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  507. break;
  508. case AV_OPT_TYPE_STRING:
  509. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  510. break;
  511. case AV_OPT_TYPE_RATIONAL:
  512. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  513. break;
  514. case AV_OPT_TYPE_BINARY:
  515. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  516. break;
  517. case AV_OPT_TYPE_CONST:
  518. default:
  519. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  520. break;
  521. }
  522. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  523. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  524. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  525. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  526. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  527. if (opt->help)
  528. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  529. av_log(av_log_obj, AV_LOG_INFO, "\n");
  530. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  531. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  532. }
  533. }
  534. }
  535. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  536. {
  537. if (!obj)
  538. return -1;
  539. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  540. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  541. return 0;
  542. }
  543. void av_opt_set_defaults(void *s)
  544. {
  545. #if FF_API_OLD_AVOPTIONS
  546. av_opt_set_defaults2(s, 0, 0);
  547. }
  548. void av_opt_set_defaults2(void *s, int mask, int flags)
  549. {
  550. #endif
  551. const AVOption *opt = NULL;
  552. while ((opt = av_opt_next(s, opt)) != NULL) {
  553. #if FF_API_OLD_AVOPTIONS
  554. if ((opt->flags & mask) != flags)
  555. continue;
  556. #endif
  557. switch (opt->type) {
  558. case AV_OPT_TYPE_CONST:
  559. /* Nothing to be done here */
  560. break;
  561. case AV_OPT_TYPE_FLAGS:
  562. case AV_OPT_TYPE_INT: {
  563. int val;
  564. val = opt->default_val.dbl;
  565. av_opt_set_int(s, opt->name, val, 0);
  566. }
  567. break;
  568. case AV_OPT_TYPE_INT64:
  569. if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
  570. av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
  571. av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
  572. break;
  573. case AV_OPT_TYPE_DOUBLE:
  574. case AV_OPT_TYPE_FLOAT: {
  575. double val;
  576. val = opt->default_val.dbl;
  577. av_opt_set_double(s, opt->name, val, 0);
  578. }
  579. break;
  580. case AV_OPT_TYPE_RATIONAL: {
  581. AVRational val;
  582. val = av_d2q(opt->default_val.dbl, INT_MAX);
  583. av_opt_set_q(s, opt->name, val, 0);
  584. }
  585. break;
  586. case AV_OPT_TYPE_STRING:
  587. av_opt_set(s, opt->name, opt->default_val.str, 0);
  588. break;
  589. case AV_OPT_TYPE_BINARY:
  590. /* Cannot set default for binary */
  591. break;
  592. default:
  593. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  594. }
  595. }
  596. }
  597. /**
  598. * Store the value in the field in ctx that is named like key.
  599. * ctx must be an AVClass context, storing is done using AVOptions.
  600. *
  601. * @param buf the string to parse, buf will be updated to point at the
  602. * separator just after the parsed key/value pair
  603. * @param key_val_sep a 0-terminated list of characters used to
  604. * separate key from value
  605. * @param pairs_sep a 0-terminated list of characters used to separate
  606. * two pairs from each other
  607. * @return 0 if the key/value pair has been successfully parsed and
  608. * set, or a negative value corresponding to an AVERROR code in case
  609. * of error:
  610. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  611. * the error code issued by av_opt_set() if the key/value pair
  612. * cannot be set
  613. */
  614. static int parse_key_value_pair(void *ctx, const char **buf,
  615. const char *key_val_sep, const char *pairs_sep)
  616. {
  617. char *key = av_get_token(buf, key_val_sep);
  618. char *val;
  619. int ret;
  620. if (*key && strspn(*buf, key_val_sep)) {
  621. (*buf)++;
  622. val = av_get_token(buf, pairs_sep);
  623. } else {
  624. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  625. av_free(key);
  626. return AVERROR(EINVAL);
  627. }
  628. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  629. ret = av_opt_set(ctx, key, val, 0);
  630. if (ret == AVERROR_OPTION_NOT_FOUND)
  631. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  632. av_free(key);
  633. av_free(val);
  634. return ret;
  635. }
  636. int av_set_options_string(void *ctx, const char *opts,
  637. const char *key_val_sep, const char *pairs_sep)
  638. {
  639. int ret, count = 0;
  640. if (!opts)
  641. return 0;
  642. while (*opts) {
  643. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  644. return ret;
  645. count++;
  646. if (*opts)
  647. opts++;
  648. }
  649. return count;
  650. }
  651. void av_opt_free(void *obj)
  652. {
  653. const AVOption *o = NULL;
  654. while ((o = av_opt_next(obj, o)))
  655. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  656. av_freep((uint8_t *)obj + o->offset);
  657. }
  658. int av_opt_set_dict(void *obj, AVDictionary **options)
  659. {
  660. AVDictionaryEntry *t = NULL;
  661. AVDictionary *tmp = NULL;
  662. int ret = 0;
  663. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  664. ret = av_opt_set(obj, t->key, t->value, 0);
  665. if (ret == AVERROR_OPTION_NOT_FOUND)
  666. av_dict_set(&tmp, t->key, t->value, 0);
  667. else if (ret < 0) {
  668. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  669. break;
  670. }
  671. ret = 0;
  672. }
  673. av_dict_free(options);
  674. *options = tmp;
  675. return ret;
  676. }
  677. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  678. int opt_flags, int search_flags)
  679. {
  680. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  681. }
  682. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  683. int opt_flags, int search_flags, void **target_obj)
  684. {
  685. const AVClass *c = *(AVClass**)obj;
  686. const AVOption *o = NULL;
  687. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  688. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  689. const AVClass *child = NULL;
  690. while (child = av_opt_child_class_next(c, child))
  691. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  692. return o;
  693. } else {
  694. void *child = NULL;
  695. while (child = av_opt_child_next(obj, child))
  696. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  697. return o;
  698. }
  699. }
  700. while (o = av_opt_next(obj, o)) {
  701. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  702. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  703. (unit && o->unit && !strcmp(o->unit, unit)))) {
  704. if (target_obj) {
  705. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  706. *target_obj = obj;
  707. else
  708. *target_obj = NULL;
  709. }
  710. return o;
  711. }
  712. }
  713. return NULL;
  714. }
  715. void *av_opt_child_next(void *obj, void *prev)
  716. {
  717. const AVClass *c = *(AVClass**)obj;
  718. if (c->child_next)
  719. return c->child_next(obj, prev);
  720. return NULL;
  721. }
  722. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  723. {
  724. if (parent->child_class_next)
  725. return parent->child_class_next(prev);
  726. return NULL;
  727. }
  728. #ifdef TEST
  729. #undef printf
  730. typedef struct TestContext
  731. {
  732. const AVClass *class;
  733. int num;
  734. int toggle;
  735. char *string;
  736. int flags;
  737. AVRational rational;
  738. } TestContext;
  739. #define OFFSET(x) offsetof(TestContext, x)
  740. #define TEST_FLAG_COOL 01
  741. #define TEST_FLAG_LAME 02
  742. #define TEST_FLAG_MU 04
  743. static const AVOption test_options[]= {
  744. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
  745. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
  746. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
  747. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  748. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
  749. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  750. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  751. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  752. {NULL},
  753. };
  754. static const char *test_get_name(void *ctx)
  755. {
  756. return "test";
  757. }
  758. static const AVClass test_class = {
  759. "TestContext",
  760. test_get_name,
  761. test_options
  762. };
  763. int main(void)
  764. {
  765. int i;
  766. printf("\nTesting av_set_options_string()\n");
  767. {
  768. TestContext test_ctx;
  769. const char *options[] = {
  770. "",
  771. ":",
  772. "=",
  773. "foo=:",
  774. ":=foo",
  775. "=foo",
  776. "foo=",
  777. "foo",
  778. "foo=val",
  779. "foo==val",
  780. "toggle=:",
  781. "string=:",
  782. "toggle=1 : foo",
  783. "toggle=100",
  784. "toggle==1",
  785. "flags=+mu-lame : num=42: toggle=0",
  786. "num=42 : string=blahblah",
  787. "rational=0 : rational=1/2 : rational=1/-1",
  788. "rational=-1/0",
  789. };
  790. test_ctx.class = &test_class;
  791. av_opt_set_defaults(&test_ctx);
  792. test_ctx.string = av_strdup("default");
  793. av_log_set_level(AV_LOG_DEBUG);
  794. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  795. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  796. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  797. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  798. printf("\n");
  799. }
  800. }
  801. return 0;
  802. }
  803. #endif