opt.c 28 KB

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