opt.c 29 KB

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