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.

892 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. #if FF_API_OLD_AVOPTIONS
  280. /**
  281. *
  282. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  283. * @param buf_len allocated length in bytes of buf
  284. */
  285. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  286. {
  287. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  288. void *dst;
  289. uint8_t *bin;
  290. int len, i;
  291. if (!o)
  292. return NULL;
  293. if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
  294. return NULL;
  295. dst= ((uint8_t*)obj) + o->offset;
  296. if (o_out) *o_out= o;
  297. switch (o->type) {
  298. case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  299. case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  300. case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  301. case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  302. case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  303. case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  304. case AV_OPT_TYPE_STRING: return *(void**)dst;
  305. case AV_OPT_TYPE_BINARY:
  306. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  307. if (len >= (buf_len + 1)/2) return NULL;
  308. bin = *(uint8_t**)dst;
  309. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  310. break;
  311. default: return NULL;
  312. }
  313. return buf;
  314. }
  315. #endif
  316. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  317. {
  318. void *dst, *target_obj;
  319. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  320. uint8_t *bin, buf[128];
  321. int len, i, ret;
  322. if (!o || !target_obj)
  323. return AVERROR_OPTION_NOT_FOUND;
  324. dst = (uint8_t*)target_obj + o->offset;
  325. buf[0] = 0;
  326. switch (o->type) {
  327. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  328. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  329. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  330. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  331. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  332. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  333. case AV_OPT_TYPE_STRING:
  334. if (*(uint8_t**)dst)
  335. *out_val = av_strdup(*(uint8_t**)dst);
  336. else
  337. *out_val = av_strdup("");
  338. return 0;
  339. case AV_OPT_TYPE_BINARY:
  340. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  341. if ((uint64_t)len*2 + 1 > INT_MAX)
  342. return AVERROR(EINVAL);
  343. if (!(*out_val = av_malloc(len*2 + 1)))
  344. return AVERROR(ENOMEM);
  345. bin = *(uint8_t**)dst;
  346. for (i = 0; i < len; i++)
  347. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  348. return 0;
  349. default:
  350. return AVERROR(EINVAL);
  351. }
  352. if (ret >= sizeof(buf))
  353. return AVERROR(EINVAL);
  354. *out_val = av_strdup(buf);
  355. return 0;
  356. }
  357. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  358. int search_flags)
  359. {
  360. void *dst, *target_obj;
  361. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  362. if (!o || !target_obj)
  363. goto error;
  364. dst = ((uint8_t*)target_obj) + o->offset;
  365. if (o_out) *o_out= o;
  366. return read_number(o, dst, num, den, intnum);
  367. error:
  368. *den=*intnum=0;
  369. return -1;
  370. }
  371. #if FF_API_OLD_AVOPTIONS
  372. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  373. {
  374. int64_t intnum=1;
  375. double num=1;
  376. int den=1;
  377. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  378. return NAN;
  379. return num*intnum/den;
  380. }
  381. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  382. {
  383. int64_t intnum=1;
  384. double num=1;
  385. int den=1;
  386. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  387. return (AVRational){0, 0};
  388. if (num == 1.0 && (int)intnum == intnum)
  389. return (AVRational){intnum, den};
  390. else
  391. return av_d2q(num*intnum/den, 1<<24);
  392. }
  393. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  394. {
  395. int64_t intnum=1;
  396. double num=1;
  397. int den=1;
  398. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  399. return -1;
  400. return num*intnum/den;
  401. }
  402. #endif
  403. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  404. {
  405. int64_t intnum = 1;
  406. double num = 1;
  407. int ret, den = 1;
  408. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  409. return ret;
  410. *out_val = num*intnum/den;
  411. return 0;
  412. }
  413. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  414. {
  415. int64_t intnum = 1;
  416. double num = 1;
  417. int ret, den = 1;
  418. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  419. return ret;
  420. *out_val = num*intnum/den;
  421. return 0;
  422. }
  423. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  424. {
  425. int64_t intnum = 1;
  426. double num = 1;
  427. int ret, den = 1;
  428. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  429. return ret;
  430. if (num == 1.0 && (int)intnum == intnum)
  431. *out_val = (AVRational){intnum, den};
  432. else
  433. *out_val = av_d2q(num*intnum/den, 1<<24);
  434. return 0;
  435. }
  436. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  437. {
  438. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  439. const AVOption *flag = av_opt_find(obj, flag_name,
  440. field ? field->unit : NULL, 0, 0);
  441. int64_t res;
  442. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  443. av_opt_get_int(obj, field_name, 0, &res) < 0)
  444. return 0;
  445. return res & (int) flag->default_val.dbl;
  446. }
  447. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  448. int req_flags, int rej_flags)
  449. {
  450. const AVOption *opt=NULL;
  451. while ((opt = av_opt_next(obj, opt))) {
  452. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  453. continue;
  454. /* Don't print CONST's on level one.
  455. * Don't print anything but CONST's on level two.
  456. * Only print items from the requested unit.
  457. */
  458. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  459. continue;
  460. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  461. continue;
  462. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  463. continue;
  464. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  465. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  466. else
  467. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  468. switch (opt->type) {
  469. case AV_OPT_TYPE_FLAGS:
  470. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  471. break;
  472. case AV_OPT_TYPE_INT:
  473. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  474. break;
  475. case AV_OPT_TYPE_INT64:
  476. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  477. break;
  478. case AV_OPT_TYPE_DOUBLE:
  479. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  480. break;
  481. case AV_OPT_TYPE_FLOAT:
  482. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  483. break;
  484. case AV_OPT_TYPE_STRING:
  485. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  486. break;
  487. case AV_OPT_TYPE_RATIONAL:
  488. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  489. break;
  490. case AV_OPT_TYPE_BINARY:
  491. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  492. break;
  493. case AV_OPT_TYPE_CONST:
  494. default:
  495. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  496. break;
  497. }
  498. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  499. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  500. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  501. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  502. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  503. if (opt->help)
  504. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  505. av_log(av_log_obj, AV_LOG_INFO, "\n");
  506. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  507. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  508. }
  509. }
  510. }
  511. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  512. {
  513. if (!obj)
  514. return -1;
  515. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  516. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  517. return 0;
  518. }
  519. void av_opt_set_defaults(void *s)
  520. {
  521. #if FF_API_OLD_AVOPTIONS
  522. av_opt_set_defaults2(s, 0, 0);
  523. }
  524. void av_opt_set_defaults2(void *s, int mask, int flags)
  525. {
  526. #endif
  527. const AVOption *opt = NULL;
  528. while ((opt = av_opt_next(s, opt)) != NULL) {
  529. #if FF_API_OLD_AVOPTIONS
  530. if ((opt->flags & mask) != flags)
  531. continue;
  532. #endif
  533. switch (opt->type) {
  534. case AV_OPT_TYPE_CONST:
  535. /* Nothing to be done here */
  536. break;
  537. case AV_OPT_TYPE_FLAGS:
  538. case AV_OPT_TYPE_INT: {
  539. int val;
  540. val = opt->default_val.dbl;
  541. av_opt_set_int(s, opt->name, val, 0);
  542. }
  543. break;
  544. case AV_OPT_TYPE_INT64:
  545. if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
  546. av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
  547. av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
  548. break;
  549. case AV_OPT_TYPE_DOUBLE:
  550. case AV_OPT_TYPE_FLOAT: {
  551. double val;
  552. val = opt->default_val.dbl;
  553. av_opt_set_double(s, opt->name, val, 0);
  554. }
  555. break;
  556. case AV_OPT_TYPE_RATIONAL: {
  557. AVRational val;
  558. val = av_d2q(opt->default_val.dbl, INT_MAX);
  559. av_opt_set_q(s, opt->name, val, 0);
  560. }
  561. break;
  562. case AV_OPT_TYPE_STRING:
  563. av_opt_set(s, opt->name, opt->default_val.str, 0);
  564. break;
  565. case AV_OPT_TYPE_BINARY:
  566. /* Cannot set default for binary */
  567. break;
  568. default:
  569. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  570. }
  571. }
  572. }
  573. /**
  574. * Store the value in the field in ctx that is named like key.
  575. * ctx must be an AVClass context, storing is done using AVOptions.
  576. *
  577. * @param buf the string to parse, buf will be updated to point at the
  578. * separator just after the parsed key/value pair
  579. * @param key_val_sep a 0-terminated list of characters used to
  580. * separate key from value
  581. * @param pairs_sep a 0-terminated list of characters used to separate
  582. * two pairs from each other
  583. * @return 0 if the key/value pair has been successfully parsed and
  584. * set, or a negative value corresponding to an AVERROR code in case
  585. * of error:
  586. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  587. * the error code issued by av_opt_set() if the key/value pair
  588. * cannot be set
  589. */
  590. static int parse_key_value_pair(void *ctx, const char **buf,
  591. const char *key_val_sep, const char *pairs_sep)
  592. {
  593. char *key = av_get_token(buf, key_val_sep);
  594. char *val;
  595. int ret;
  596. if (*key && strspn(*buf, key_val_sep)) {
  597. (*buf)++;
  598. val = av_get_token(buf, pairs_sep);
  599. } else {
  600. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  601. av_free(key);
  602. return AVERROR(EINVAL);
  603. }
  604. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  605. ret = av_opt_set(ctx, key, val, 0);
  606. if (ret == AVERROR_OPTION_NOT_FOUND)
  607. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  608. av_free(key);
  609. av_free(val);
  610. return ret;
  611. }
  612. int av_set_options_string(void *ctx, const char *opts,
  613. const char *key_val_sep, const char *pairs_sep)
  614. {
  615. int ret, count = 0;
  616. if (!opts)
  617. return 0;
  618. while (*opts) {
  619. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  620. return ret;
  621. count++;
  622. if (*opts)
  623. opts++;
  624. }
  625. return count;
  626. }
  627. void av_opt_free(void *obj)
  628. {
  629. const AVOption *o = NULL;
  630. while ((o = av_opt_next(obj, o)))
  631. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  632. av_freep((uint8_t *)obj + o->offset);
  633. }
  634. int av_opt_set_dict(void *obj, AVDictionary **options)
  635. {
  636. AVDictionaryEntry *t = NULL;
  637. AVDictionary *tmp = NULL;
  638. int ret = 0;
  639. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  640. ret = av_opt_set(obj, t->key, t->value, 0);
  641. if (ret == AVERROR_OPTION_NOT_FOUND)
  642. av_dict_set(&tmp, t->key, t->value, 0);
  643. else if (ret < 0) {
  644. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  645. break;
  646. }
  647. ret = 0;
  648. }
  649. av_dict_free(options);
  650. *options = tmp;
  651. return ret;
  652. }
  653. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  654. int opt_flags, int search_flags)
  655. {
  656. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  657. }
  658. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  659. int opt_flags, int search_flags, void **target_obj)
  660. {
  661. const AVClass *c = *(AVClass**)obj;
  662. const AVOption *o = NULL;
  663. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  664. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  665. const AVClass *child = NULL;
  666. while (child = av_opt_child_class_next(c, child))
  667. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  668. return o;
  669. } else {
  670. void *child = NULL;
  671. while (child = av_opt_child_next(obj, child))
  672. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  673. return o;
  674. }
  675. }
  676. while (o = av_opt_next(obj, o)) {
  677. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  678. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  679. (unit && o->unit && !strcmp(o->unit, unit)))) {
  680. if (target_obj) {
  681. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  682. *target_obj = obj;
  683. else
  684. *target_obj = NULL;
  685. }
  686. return o;
  687. }
  688. }
  689. return NULL;
  690. }
  691. void *av_opt_child_next(void *obj, void *prev)
  692. {
  693. const AVClass *c = *(AVClass**)obj;
  694. if (c->child_next)
  695. return c->child_next(obj, prev);
  696. return NULL;
  697. }
  698. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  699. {
  700. if (parent->child_class_next)
  701. return parent->child_class_next(prev);
  702. return NULL;
  703. }
  704. #ifdef TEST
  705. #undef printf
  706. typedef struct TestContext
  707. {
  708. const AVClass *class;
  709. int num;
  710. int toggle;
  711. char *string;
  712. int flags;
  713. AVRational rational;
  714. } TestContext;
  715. #define OFFSET(x) offsetof(TestContext, x)
  716. #define TEST_FLAG_COOL 01
  717. #define TEST_FLAG_LAME 02
  718. #define TEST_FLAG_MU 04
  719. static const AVOption test_options[]= {
  720. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
  721. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
  722. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
  723. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  724. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
  725. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  726. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  727. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  728. {NULL},
  729. };
  730. static const char *test_get_name(void *ctx)
  731. {
  732. return "test";
  733. }
  734. static const AVClass test_class = {
  735. "TestContext",
  736. test_get_name,
  737. test_options
  738. };
  739. int main(void)
  740. {
  741. int i;
  742. printf("\nTesting av_set_options_string()\n");
  743. {
  744. TestContext test_ctx;
  745. const char *options[] = {
  746. "",
  747. ":",
  748. "=",
  749. "foo=:",
  750. ":=foo",
  751. "=foo",
  752. "foo=",
  753. "foo",
  754. "foo=val",
  755. "foo==val",
  756. "toggle=:",
  757. "string=:",
  758. "toggle=1 : foo",
  759. "toggle=100",
  760. "toggle==1",
  761. "flags=+mu-lame : num=42: toggle=0",
  762. "num=42 : string=blahblah",
  763. "rational=0 : rational=1/2 : rational=1/-1",
  764. "rational=-1/0",
  765. };
  766. test_ctx.class = &test_class;
  767. av_opt_set_defaults(&test_ctx);
  768. test_ctx.string = av_strdup("default");
  769. av_log_set_level(AV_LOG_DEBUG);
  770. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  771. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  772. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  773. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  774. printf("\n");
  775. }
  776. }
  777. return 0;
  778. }
  779. #endif