opt.c 31 KB

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