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.

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