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.

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