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.

924 lines
29KB

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