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.

923 lines
29KB

  1. /*
  2. * AVOptions
  3. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * AVOptions
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "avutil.h"
  27. #include "avstring.h"
  28. #include "common.h"
  29. #include "opt.h"
  30. #include "eval.h"
  31. #include "dict.h"
  32. #include "log.h"
  33. #include "mathematics.h"
  34. #if FF_API_FIND_OPT
  35. //FIXME order them and do a bin search
  36. const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
  37. {
  38. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  39. const AVOption *o= c->option;
  40. for (; o && o->name; o++) {
  41. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
  42. return o;
  43. }
  44. return NULL;
  45. }
  46. #endif
  47. #if FF_API_OLD_AVOPTIONS
  48. const AVOption *av_next_option(void *obj, const AVOption *last)
  49. {
  50. return av_opt_next(obj, last);
  51. }
  52. #endif
  53. const AVOption *av_opt_next(void *obj, const AVOption *last)
  54. {
  55. AVClass *class = *(AVClass**)obj;
  56. if (!last && class->option[0].name) return class->option;
  57. if (last && last[1].name) return ++last;
  58. return NULL;
  59. }
  60. static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
  61. {
  62. switch (o->type) {
  63. case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
  64. case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
  65. case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
  66. case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
  67. case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
  68. case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
  69. *den = ((AVRational*)dst)->den;
  70. return 0;
  71. }
  72. return AVERROR(EINVAL);
  73. }
  74. static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  75. {
  76. if (o->max*den < num*intnum || o->min*den > num*intnum) {
  77. av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n",
  78. num*intnum/den, o->name);
  79. return AVERROR(ERANGE);
  80. }
  81. switch (o->type) {
  82. case AV_OPT_TYPE_FLAGS:
  83. case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  84. case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  85. case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  86. case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  87. case AV_OPT_TYPE_RATIONAL:
  88. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  89. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  90. break;
  91. default:
  92. return AVERROR(EINVAL);
  93. }
  94. return 0;
  95. }
  96. static const double const_values[] = {
  97. M_PI,
  98. M_E,
  99. FF_QP2LAMBDA,
  100. 0
  101. };
  102. static const char * const const_names[] = {
  103. "PI",
  104. "E",
  105. "QP2LAMBDA",
  106. 0
  107. };
  108. static int hexchar2int(char c) {
  109. if (c >= '0' && c <= '9') return c - '0';
  110. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  111. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  112. return -1;
  113. }
  114. static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  115. {
  116. int *lendst = (int *)(dst + 1);
  117. uint8_t *bin, *ptr;
  118. int len = strlen(val);
  119. av_freep(dst);
  120. *lendst = 0;
  121. if (len & 1)
  122. return AVERROR(EINVAL);
  123. len /= 2;
  124. ptr = bin = av_malloc(len);
  125. while (*val) {
  126. int a = hexchar2int(*val++);
  127. int b = hexchar2int(*val++);
  128. if (a < 0 || b < 0) {
  129. av_free(bin);
  130. return AVERROR(EINVAL);
  131. }
  132. *ptr++ = (a << 4) | b;
  133. }
  134. *dst = bin;
  135. *lendst = len;
  136. return 0;
  137. }
  138. static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  139. {
  140. av_freep(dst);
  141. *dst = av_strdup(val);
  142. return 0;
  143. }
  144. #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
  145. opt->type == AV_OPT_TYPE_CONST || \
  146. opt->type == AV_OPT_TYPE_FLAGS || \
  147. opt->type == AV_OPT_TYPE_INT) ? \
  148. opt->default_val.i64 : opt->default_val.dbl)
  149. static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
  150. {
  151. int ret = 0, notfirst = 0;
  152. for (;;) {
  153. int i, den = 1;
  154. char buf[256];
  155. int cmd = 0;
  156. double d, num = 1;
  157. int64_t intnum = 1;
  158. if (*val == '+' || *val == '-')
  159. cmd = *(val++);
  160. for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  161. buf[i] = val[i];
  162. buf[i] = 0;
  163. {
  164. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  165. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  166. d = DEFAULT_NUMVAL(o_named);
  167. else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
  168. else if (!strcmp(buf, "max" )) d = o->max;
  169. else if (!strcmp(buf, "min" )) d = o->min;
  170. else if (!strcmp(buf, "none" )) d = 0;
  171. else if (!strcmp(buf, "all" )) d = ~0;
  172. else {
  173. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  174. if (res < 0) {
  175. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  176. return res;
  177. }
  178. }
  179. }
  180. if (o->type == AV_OPT_TYPE_FLAGS) {
  181. read_number(o, dst, NULL, NULL, &intnum);
  182. if (cmd == '+') d = intnum | (int64_t)d;
  183. else if (cmd == '-') d = intnum &~(int64_t)d;
  184. } else {
  185. read_number(o, dst, &num, &den, &intnum);
  186. if (cmd == '+') d = notfirst*num*intnum/den + d;
  187. else if (cmd == '-') d = notfirst*num*intnum/den - d;
  188. }
  189. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  190. return ret;
  191. val += i;
  192. if (!*val)
  193. return 0;
  194. notfirst = 1;
  195. }
  196. return 0;
  197. }
  198. #if FF_API_OLD_AVOPTIONS
  199. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  200. {
  201. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  202. if (o_out)
  203. *o_out = o;
  204. return av_opt_set(obj, name, val, 0);
  205. }
  206. #endif
  207. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  208. {
  209. void *dst, *target_obj;
  210. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  211. if (!o || !target_obj)
  212. return AVERROR_OPTION_NOT_FOUND;
  213. if (!val)
  214. return AVERROR(EINVAL);
  215. dst = ((uint8_t*)target_obj) + o->offset;
  216. switch (o->type) {
  217. case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  218. case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  219. case AV_OPT_TYPE_FLAGS:
  220. case AV_OPT_TYPE_INT:
  221. case AV_OPT_TYPE_INT64:
  222. case AV_OPT_TYPE_FLOAT:
  223. case AV_OPT_TYPE_DOUBLE:
  224. case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  225. }
  226. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  227. return AVERROR(EINVAL);
  228. }
  229. #define OPT_EVAL_NUMBER(name, opttype, vartype)\
  230. int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
  231. {\
  232. if (!o || o->type != opttype)\
  233. return AVERROR(EINVAL);\
  234. return set_string_number(obj, o, val, name ## _out);\
  235. }
  236. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  237. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  238. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  239. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  240. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  241. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  242. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  243. int search_flags)
  244. {
  245. void *dst, *target_obj;
  246. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  247. if (!o || !target_obj)
  248. return AVERROR_OPTION_NOT_FOUND;
  249. dst = ((uint8_t*)target_obj) + o->offset;
  250. return write_number(obj, o, dst, num, den, intnum);
  251. }
  252. #if FF_API_OLD_AVOPTIONS
  253. const AVOption *av_set_double(void *obj, const char *name, double n)
  254. {
  255. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  256. if (set_number(obj, name, n, 1, 1, 0) < 0)
  257. return NULL;
  258. return o;
  259. }
  260. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  261. {
  262. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  263. if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
  264. return NULL;
  265. return o;
  266. }
  267. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  268. {
  269. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  270. if (set_number(obj, name, 1, 1, n, 0) < 0)
  271. return NULL;
  272. return o;
  273. }
  274. #endif
  275. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  276. {
  277. return set_number(obj, name, 1, 1, val, search_flags);
  278. }
  279. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  280. {
  281. return set_number(obj, name, val, 1, 1, search_flags);
  282. }
  283. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  284. {
  285. return set_number(obj, name, val.num, val.den, 1, search_flags);
  286. }
  287. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  288. {
  289. void *target_obj;
  290. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  291. uint8_t *ptr;
  292. uint8_t **dst;
  293. int *lendst;
  294. if (!o || !target_obj)
  295. return AVERROR_OPTION_NOT_FOUND;
  296. if (o->type != AV_OPT_TYPE_BINARY)
  297. return AVERROR(EINVAL);
  298. ptr = av_malloc(len);
  299. if (!ptr)
  300. return AVERROR(ENOMEM);
  301. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  302. lendst = (int *)(dst + 1);
  303. av_free(*dst);
  304. *dst = ptr;
  305. *lendst = len;
  306. memcpy(ptr, val, len);
  307. return 0;
  308. }
  309. #if FF_API_OLD_AVOPTIONS
  310. /**
  311. *
  312. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  313. * @param buf_len allocated length in bytes of buf
  314. */
  315. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  316. {
  317. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  318. void *dst;
  319. uint8_t *bin;
  320. int len, i;
  321. if (!o)
  322. return NULL;
  323. if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
  324. return NULL;
  325. dst= ((uint8_t*)obj) + o->offset;
  326. if (o_out) *o_out= o;
  327. switch (o->type) {
  328. case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  329. case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  330. case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  331. case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  332. case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  333. case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  334. case AV_OPT_TYPE_STRING: return *(void**)dst;
  335. case AV_OPT_TYPE_BINARY:
  336. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  337. if (len >= (buf_len + 1)/2) return NULL;
  338. bin = *(uint8_t**)dst;
  339. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  340. break;
  341. default: return NULL;
  342. }
  343. return buf;
  344. }
  345. #endif
  346. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  347. {
  348. void *dst, *target_obj;
  349. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  350. uint8_t *bin, buf[128];
  351. int len, i, ret;
  352. if (!o || !target_obj)
  353. return AVERROR_OPTION_NOT_FOUND;
  354. dst = (uint8_t*)target_obj + o->offset;
  355. buf[0] = 0;
  356. switch (o->type) {
  357. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  358. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  359. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  360. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  361. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  362. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  363. case AV_OPT_TYPE_STRING:
  364. if (*(uint8_t**)dst)
  365. *out_val = av_strdup(*(uint8_t**)dst);
  366. else
  367. *out_val = av_strdup("");
  368. return 0;
  369. case AV_OPT_TYPE_BINARY:
  370. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  371. if ((uint64_t)len*2 + 1 > INT_MAX)
  372. return AVERROR(EINVAL);
  373. if (!(*out_val = av_malloc(len*2 + 1)))
  374. return AVERROR(ENOMEM);
  375. bin = *(uint8_t**)dst;
  376. for (i = 0; i < len; i++)
  377. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  378. return 0;
  379. default:
  380. return AVERROR(EINVAL);
  381. }
  382. if (ret >= sizeof(buf))
  383. return AVERROR(EINVAL);
  384. *out_val = av_strdup(buf);
  385. return 0;
  386. }
  387. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  388. int search_flags)
  389. {
  390. void *dst, *target_obj;
  391. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  392. if (!o || !target_obj)
  393. goto error;
  394. dst = ((uint8_t*)target_obj) + o->offset;
  395. if (o_out) *o_out= o;
  396. return read_number(o, dst, num, den, intnum);
  397. error:
  398. *den=*intnum=0;
  399. return -1;
  400. }
  401. #if FF_API_OLD_AVOPTIONS
  402. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  403. {
  404. int64_t intnum=1;
  405. double num=1;
  406. int den=1;
  407. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  408. return NAN;
  409. return num*intnum/den;
  410. }
  411. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  412. {
  413. int64_t intnum=1;
  414. double num=1;
  415. int den=1;
  416. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  417. return (AVRational){0, 0};
  418. if (num == 1.0 && (int)intnum == intnum)
  419. return (AVRational){intnum, den};
  420. else
  421. return av_d2q(num*intnum/den, 1<<24);
  422. }
  423. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  424. {
  425. int64_t intnum=1;
  426. double num=1;
  427. int den=1;
  428. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  429. return -1;
  430. return num*intnum/den;
  431. }
  432. #endif
  433. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  434. {
  435. int64_t intnum = 1;
  436. double num = 1;
  437. int ret, den = 1;
  438. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  439. return ret;
  440. *out_val = num*intnum/den;
  441. return 0;
  442. }
  443. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  444. {
  445. int64_t intnum = 1;
  446. double num = 1;
  447. int ret, den = 1;
  448. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  449. return ret;
  450. *out_val = num*intnum/den;
  451. return 0;
  452. }
  453. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  454. {
  455. int64_t intnum = 1;
  456. double num = 1;
  457. int ret, den = 1;
  458. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  459. return ret;
  460. if (num == 1.0 && (int)intnum == intnum)
  461. *out_val = (AVRational){intnum, den};
  462. else
  463. *out_val = av_d2q(num*intnum/den, 1<<24);
  464. return 0;
  465. }
  466. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  467. {
  468. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  469. const AVOption *flag = av_opt_find(obj, flag_name,
  470. field ? field->unit : NULL, 0, 0);
  471. int64_t res;
  472. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  473. av_opt_get_int(obj, field_name, 0, &res) < 0)
  474. return 0;
  475. return res & flag->default_val.i64;
  476. }
  477. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  478. int req_flags, int rej_flags)
  479. {
  480. const AVOption *opt=NULL;
  481. while ((opt = av_opt_next(obj, opt))) {
  482. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  483. continue;
  484. /* Don't print CONST's on level one.
  485. * Don't print anything but CONST's on level two.
  486. * Only print items from the requested unit.
  487. */
  488. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  489. continue;
  490. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  491. continue;
  492. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  493. continue;
  494. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  495. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  496. else
  497. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  498. switch (opt->type) {
  499. case AV_OPT_TYPE_FLAGS:
  500. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  501. break;
  502. case AV_OPT_TYPE_INT:
  503. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  504. break;
  505. case AV_OPT_TYPE_INT64:
  506. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  507. break;
  508. case AV_OPT_TYPE_DOUBLE:
  509. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  510. break;
  511. case AV_OPT_TYPE_FLOAT:
  512. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  513. break;
  514. case AV_OPT_TYPE_STRING:
  515. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  516. break;
  517. case AV_OPT_TYPE_RATIONAL:
  518. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  519. break;
  520. case AV_OPT_TYPE_BINARY:
  521. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  522. break;
  523. case AV_OPT_TYPE_CONST:
  524. default:
  525. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  526. break;
  527. }
  528. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  529. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  530. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  531. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  532. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  533. if (opt->help)
  534. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  535. av_log(av_log_obj, AV_LOG_INFO, "\n");
  536. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  537. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  538. }
  539. }
  540. }
  541. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  542. {
  543. if (!obj)
  544. return -1;
  545. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  546. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  547. return 0;
  548. }
  549. void av_opt_set_defaults(void *s)
  550. {
  551. #if FF_API_OLD_AVOPTIONS
  552. av_opt_set_defaults2(s, 0, 0);
  553. }
  554. void av_opt_set_defaults2(void *s, int mask, int flags)
  555. {
  556. #endif
  557. const AVOption *opt = NULL;
  558. while ((opt = av_opt_next(s, opt)) != NULL) {
  559. #if FF_API_OLD_AVOPTIONS
  560. if ((opt->flags & mask) != flags)
  561. continue;
  562. #endif
  563. switch (opt->type) {
  564. case AV_OPT_TYPE_CONST:
  565. /* Nothing to be done here */
  566. break;
  567. case AV_OPT_TYPE_FLAGS:
  568. case AV_OPT_TYPE_INT:
  569. case AV_OPT_TYPE_INT64:
  570. av_opt_set_int(s, opt->name, opt->default_val.i64, 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, {.i64 = 0}, 0, 100 },
  744. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  745. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 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, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  748. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  749. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  750. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = 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