opt.c 28 KB

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