opt.c 28 KB

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