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.

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