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.

925 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 "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. #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64) ? \
  145. opt->default_val.i64 : opt->default_val.dbl)
  146. static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
  147. {
  148. int ret = 0, notfirst = 0;
  149. for (;;) {
  150. int i, den = 1;
  151. char buf[256];
  152. int cmd = 0;
  153. double d, num = 1;
  154. int64_t intnum = 1;
  155. if (*val == '+' || *val == '-')
  156. cmd = *(val++);
  157. for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  158. buf[i] = val[i];
  159. buf[i] = 0;
  160. {
  161. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  162. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  163. d = DEFAULT_NUMVAL(o_named);
  164. else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
  165. else if (!strcmp(buf, "max" )) d = o->max;
  166. else if (!strcmp(buf, "min" )) d = o->min;
  167. else if (!strcmp(buf, "none" )) d = 0;
  168. else if (!strcmp(buf, "all" )) d = ~0;
  169. else {
  170. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  171. if (res < 0) {
  172. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  173. return res;
  174. }
  175. }
  176. }
  177. if (o->type == AV_OPT_TYPE_FLAGS) {
  178. read_number(o, dst, NULL, NULL, &intnum);
  179. if (cmd == '+') d = intnum | (int64_t)d;
  180. else if (cmd == '-') d = intnum &~(int64_t)d;
  181. } else {
  182. read_number(o, dst, &num, &den, &intnum);
  183. if (cmd == '+') d = notfirst*num*intnum/den + d;
  184. else if (cmd == '-') d = notfirst*num*intnum/den - d;
  185. }
  186. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  187. return ret;
  188. val += i;
  189. if (!*val)
  190. return 0;
  191. notfirst = 1;
  192. }
  193. return 0;
  194. }
  195. #if FF_API_OLD_AVOPTIONS
  196. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  197. {
  198. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  199. if (o_out)
  200. *o_out = o;
  201. return av_opt_set(obj, name, val, 0);
  202. }
  203. #endif
  204. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  205. {
  206. void *dst, *target_obj;
  207. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  208. if (!o || !target_obj)
  209. return AVERROR_OPTION_NOT_FOUND;
  210. if (!val)
  211. return AVERROR(EINVAL);
  212. dst = ((uint8_t*)target_obj) + o->offset;
  213. switch (o->type) {
  214. case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  215. case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  216. case AV_OPT_TYPE_FLAGS:
  217. case AV_OPT_TYPE_INT:
  218. case AV_OPT_TYPE_INT64:
  219. case AV_OPT_TYPE_FLOAT:
  220. case AV_OPT_TYPE_DOUBLE:
  221. case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  222. }
  223. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  224. return AVERROR(EINVAL);
  225. }
  226. #define OPT_EVAL_NUMBER(name, opttype, vartype)\
  227. int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
  228. {\
  229. if (!o || o->type != opttype)\
  230. return AVERROR(EINVAL);\
  231. return set_string_number(obj, o, val, name ## _out);\
  232. }
  233. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  234. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  235. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  236. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  237. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  238. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  239. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  240. int search_flags)
  241. {
  242. void *dst, *target_obj;
  243. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  244. if (!o || !target_obj)
  245. return AVERROR_OPTION_NOT_FOUND;
  246. dst = ((uint8_t*)target_obj) + o->offset;
  247. return write_number(obj, o, dst, num, den, intnum);
  248. }
  249. #if FF_API_OLD_AVOPTIONS
  250. const AVOption *av_set_double(void *obj, const char *name, double n)
  251. {
  252. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  253. if (set_number(obj, name, n, 1, 1, 0) < 0)
  254. return NULL;
  255. return o;
  256. }
  257. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  258. {
  259. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  260. if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
  261. return NULL;
  262. return o;
  263. }
  264. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  265. {
  266. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  267. if (set_number(obj, name, 1, 1, n, 0) < 0)
  268. return NULL;
  269. return o;
  270. }
  271. #endif
  272. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  273. {
  274. return set_number(obj, name, 1, 1, val, search_flags);
  275. }
  276. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  277. {
  278. return set_number(obj, name, val, 1, 1, search_flags);
  279. }
  280. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  281. {
  282. return set_number(obj, name, val.num, val.den, 1, search_flags);
  283. }
  284. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  285. {
  286. void *target_obj;
  287. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  288. uint8_t *ptr;
  289. uint8_t **dst;
  290. int *lendst;
  291. if (!o || !target_obj)
  292. return AVERROR_OPTION_NOT_FOUND;
  293. if (o->type != AV_OPT_TYPE_BINARY)
  294. return AVERROR(EINVAL);
  295. ptr = av_malloc(len);
  296. if (!ptr)
  297. return AVERROR(ENOMEM);
  298. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  299. lendst = (int *)(dst + 1);
  300. av_free(*dst);
  301. *dst = ptr;
  302. *lendst = len;
  303. memcpy(ptr, val, len);
  304. return 0;
  305. }
  306. #if FF_API_OLD_AVOPTIONS
  307. /**
  308. *
  309. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  310. * @param buf_len allocated length in bytes of buf
  311. */
  312. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  313. {
  314. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  315. void *dst;
  316. uint8_t *bin;
  317. int len, i;
  318. if (!o)
  319. return NULL;
  320. if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
  321. return NULL;
  322. dst= ((uint8_t*)obj) + o->offset;
  323. if (o_out) *o_out= o;
  324. switch (o->type) {
  325. case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  326. case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  327. case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  328. case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  329. case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  330. case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  331. case AV_OPT_TYPE_STRING: return *(void**)dst;
  332. case AV_OPT_TYPE_BINARY:
  333. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  334. if (len >= (buf_len + 1)/2) return NULL;
  335. bin = *(uint8_t**)dst;
  336. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  337. break;
  338. default: return NULL;
  339. }
  340. return buf;
  341. }
  342. #endif
  343. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  344. {
  345. void *dst, *target_obj;
  346. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  347. uint8_t *bin, buf[128];
  348. int len, i, ret;
  349. if (!o || !target_obj)
  350. return AVERROR_OPTION_NOT_FOUND;
  351. dst = (uint8_t*)target_obj + o->offset;
  352. buf[0] = 0;
  353. switch (o->type) {
  354. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  355. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  356. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  357. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  358. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  359. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  360. case AV_OPT_TYPE_STRING:
  361. if (*(uint8_t**)dst)
  362. *out_val = av_strdup(*(uint8_t**)dst);
  363. else
  364. *out_val = av_strdup("");
  365. return 0;
  366. case AV_OPT_TYPE_BINARY:
  367. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  368. if ((uint64_t)len*2 + 1 > INT_MAX)
  369. return AVERROR(EINVAL);
  370. if (!(*out_val = av_malloc(len*2 + 1)))
  371. return AVERROR(ENOMEM);
  372. bin = *(uint8_t**)dst;
  373. for (i = 0; i < len; i++)
  374. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  375. return 0;
  376. default:
  377. return AVERROR(EINVAL);
  378. }
  379. if (ret >= sizeof(buf))
  380. return AVERROR(EINVAL);
  381. *out_val = av_strdup(buf);
  382. return 0;
  383. }
  384. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  385. int search_flags)
  386. {
  387. void *dst, *target_obj;
  388. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  389. if (!o || !target_obj)
  390. goto error;
  391. dst = ((uint8_t*)target_obj) + o->offset;
  392. if (o_out) *o_out= o;
  393. return read_number(o, dst, num, den, intnum);
  394. error:
  395. *den=*intnum=0;
  396. return -1;
  397. }
  398. #if FF_API_OLD_AVOPTIONS
  399. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  400. {
  401. int64_t intnum=1;
  402. double num=1;
  403. int den=1;
  404. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  405. return NAN;
  406. return num*intnum/den;
  407. }
  408. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  409. {
  410. int64_t intnum=1;
  411. double num=1;
  412. int den=1;
  413. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  414. return (AVRational){0, 0};
  415. if (num == 1.0 && (int)intnum == intnum)
  416. return (AVRational){intnum, den};
  417. else
  418. return av_d2q(num*intnum/den, 1<<24);
  419. }
  420. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  421. {
  422. int64_t intnum=1;
  423. double num=1;
  424. int den=1;
  425. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  426. return -1;
  427. return num*intnum/den;
  428. }
  429. #endif
  430. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  431. {
  432. int64_t intnum = 1;
  433. double num = 1;
  434. int ret, den = 1;
  435. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  436. return ret;
  437. *out_val = num*intnum/den;
  438. return 0;
  439. }
  440. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  441. {
  442. int64_t intnum = 1;
  443. double num = 1;
  444. int ret, den = 1;
  445. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  446. return ret;
  447. *out_val = num*intnum/den;
  448. return 0;
  449. }
  450. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  451. {
  452. int64_t intnum = 1;
  453. double num = 1;
  454. int ret, den = 1;
  455. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  456. return ret;
  457. if (num == 1.0 && (int)intnum == intnum)
  458. *out_val = (AVRational){intnum, den};
  459. else
  460. *out_val = av_d2q(num*intnum/den, 1<<24);
  461. return 0;
  462. }
  463. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  464. {
  465. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  466. const AVOption *flag = av_opt_find(obj, flag_name,
  467. field ? field->unit : NULL, 0, 0);
  468. int64_t res;
  469. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  470. av_opt_get_int(obj, field_name, 0, &res) < 0)
  471. return 0;
  472. return res & (int) flag->default_val.dbl;
  473. }
  474. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  475. int req_flags, int rej_flags)
  476. {
  477. const AVOption *opt=NULL;
  478. while ((opt = av_opt_next(obj, opt))) {
  479. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  480. continue;
  481. /* Don't print CONST's on level one.
  482. * Don't print anything but CONST's on level two.
  483. * Only print items from the requested unit.
  484. */
  485. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  486. continue;
  487. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  488. continue;
  489. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  490. continue;
  491. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  492. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  493. else
  494. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  495. switch (opt->type) {
  496. case AV_OPT_TYPE_FLAGS:
  497. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  498. break;
  499. case AV_OPT_TYPE_INT:
  500. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  501. break;
  502. case AV_OPT_TYPE_INT64:
  503. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  504. break;
  505. case AV_OPT_TYPE_DOUBLE:
  506. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  507. break;
  508. case AV_OPT_TYPE_FLOAT:
  509. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  510. break;
  511. case AV_OPT_TYPE_STRING:
  512. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  513. break;
  514. case AV_OPT_TYPE_RATIONAL:
  515. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  516. break;
  517. case AV_OPT_TYPE_BINARY:
  518. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  519. break;
  520. case AV_OPT_TYPE_CONST:
  521. default:
  522. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  523. break;
  524. }
  525. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  526. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  527. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  528. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  529. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  530. if (opt->help)
  531. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  532. av_log(av_log_obj, AV_LOG_INFO, "\n");
  533. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  534. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  535. }
  536. }
  537. }
  538. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  539. {
  540. if (!obj)
  541. return -1;
  542. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  543. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  544. return 0;
  545. }
  546. void av_opt_set_defaults(void *s)
  547. {
  548. #if FF_API_OLD_AVOPTIONS
  549. av_opt_set_defaults2(s, 0, 0);
  550. }
  551. void av_opt_set_defaults2(void *s, int mask, int flags)
  552. {
  553. #endif
  554. const AVOption *opt = NULL;
  555. while ((opt = av_opt_next(s, opt)) != NULL) {
  556. #if FF_API_OLD_AVOPTIONS
  557. if ((opt->flags & mask) != flags)
  558. continue;
  559. #endif
  560. switch (opt->type) {
  561. case AV_OPT_TYPE_CONST:
  562. /* Nothing to be done here */
  563. break;
  564. case AV_OPT_TYPE_FLAGS:
  565. case AV_OPT_TYPE_INT: {
  566. int val;
  567. val = opt->default_val.dbl;
  568. av_opt_set_int(s, opt->name, val, 0);
  569. }
  570. break;
  571. case AV_OPT_TYPE_INT64:
  572. av_opt_set_int(s, opt->name, opt->default_val.i64, 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