opt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. * AVOptions
  3. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  37. const AVOption *o= c->option;
  38. for (; o && o->name; 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. if (last && last[1].name) return ++last;
  54. else if (last) return NULL;
  55. else return (*(AVClass**)obj)->option;
  56. }
  57. static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
  58. {
  59. switch (o->type) {
  60. case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
  61. case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
  62. case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
  63. case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
  64. case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
  65. case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
  66. *den = ((AVRational*)dst)->den;
  67. return 0;
  68. }
  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)
  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, 0);
  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_STRING: return *(void**)dst;
  304. case AV_OPT_TYPE_BINARY:
  305. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  306. if (len >= (buf_len + 1)/2) return NULL;
  307. bin = *(uint8_t**)dst;
  308. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  309. break;
  310. default: return NULL;
  311. }
  312. return buf;
  313. }
  314. #endif
  315. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  316. {
  317. void *dst, *target_obj;
  318. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  319. uint8_t *bin, buf[128];
  320. int len, i, ret;
  321. if (!o || !target_obj)
  322. return AVERROR_OPTION_NOT_FOUND;
  323. dst = (uint8_t*)target_obj + o->offset;
  324. buf[0] = 0;
  325. switch (o->type) {
  326. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  327. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  328. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  329. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  330. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  331. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  332. case AV_OPT_TYPE_STRING:
  333. if (*(uint8_t**)dst)
  334. *out_val = av_strdup(*(uint8_t**)dst);
  335. else
  336. *out_val = av_strdup("");
  337. return 0;
  338. case AV_OPT_TYPE_BINARY:
  339. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  340. if ((uint64_t)len*2 + 1 > INT_MAX)
  341. return AVERROR(EINVAL);
  342. if (!(*out_val = av_malloc(len*2 + 1)))
  343. return AVERROR(ENOMEM);
  344. bin = *(uint8_t**)dst;
  345. for (i = 0; i < len; i++)
  346. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  347. return 0;
  348. default:
  349. return AVERROR(EINVAL);
  350. }
  351. if (ret >= sizeof(buf))
  352. return AVERROR(EINVAL);
  353. *out_val = av_strdup(buf);
  354. return 0;
  355. }
  356. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  357. int search_flags)
  358. {
  359. void *dst, *target_obj;
  360. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  361. if (!o || !target_obj)
  362. goto error;
  363. dst = ((uint8_t*)target_obj) + o->offset;
  364. if (o_out) *o_out= o;
  365. return read_number(o, dst, num, den, intnum);
  366. error:
  367. *den=*intnum=0;
  368. return -1;
  369. }
  370. #if FF_API_OLD_AVOPTIONS
  371. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  372. {
  373. int64_t intnum=1;
  374. double num=1;
  375. int den=1;
  376. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  377. return NAN;
  378. return num*intnum/den;
  379. }
  380. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  381. {
  382. int64_t intnum=1;
  383. double num=1;
  384. int den=1;
  385. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  386. return (AVRational){0, 0};
  387. if (num == 1.0 && (int)intnum == intnum)
  388. return (AVRational){intnum, den};
  389. else
  390. return av_d2q(num*intnum/den, 1<<24);
  391. }
  392. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  393. {
  394. int64_t intnum=1;
  395. double num=1;
  396. int den=1;
  397. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  398. return -1;
  399. return num*intnum/den;
  400. }
  401. #endif
  402. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  403. {
  404. int64_t intnum = 1;
  405. double num = 1;
  406. int ret, den = 1;
  407. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  408. return ret;
  409. *out_val = num*intnum/den;
  410. return 0;
  411. }
  412. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  413. {
  414. int64_t intnum = 1;
  415. double num = 1;
  416. int ret, den = 1;
  417. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  418. return ret;
  419. *out_val = num*intnum/den;
  420. return 0;
  421. }
  422. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  423. {
  424. int64_t intnum = 1;
  425. double num = 1;
  426. int ret, den = 1;
  427. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  428. return ret;
  429. if (num == 1.0 && (int)intnum == intnum)
  430. *out_val = (AVRational){intnum, den};
  431. else
  432. *out_val = av_d2q(num*intnum/den, 1<<24);
  433. return 0;
  434. }
  435. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  436. {
  437. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  438. const AVOption *flag = av_opt_find(obj, flag_name, NULL, 0, 0);
  439. int64_t res;
  440. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  441. av_opt_get_int(obj, field_name, 0, &res) < 0)
  442. return 0;
  443. return res & (int) flag->default_val.dbl;
  444. }
  445. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  446. int req_flags, int rej_flags)
  447. {
  448. const AVOption *opt=NULL;
  449. while ((opt = av_opt_next(obj, opt))) {
  450. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  451. continue;
  452. /* Don't print CONST's on level one.
  453. * Don't print anything but CONST's on level two.
  454. * Only print items from the requested unit.
  455. */
  456. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  457. continue;
  458. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  459. continue;
  460. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  461. continue;
  462. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  463. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  464. else
  465. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  466. switch (opt->type) {
  467. case AV_OPT_TYPE_FLAGS:
  468. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  469. break;
  470. case AV_OPT_TYPE_INT:
  471. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  472. break;
  473. case AV_OPT_TYPE_INT64:
  474. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  475. break;
  476. case AV_OPT_TYPE_DOUBLE:
  477. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  478. break;
  479. case AV_OPT_TYPE_FLOAT:
  480. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  481. break;
  482. case AV_OPT_TYPE_STRING:
  483. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  484. break;
  485. case AV_OPT_TYPE_RATIONAL:
  486. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  487. break;
  488. case AV_OPT_TYPE_BINARY:
  489. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  490. break;
  491. case AV_OPT_TYPE_CONST:
  492. default:
  493. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  494. break;
  495. }
  496. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  497. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  498. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  499. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  500. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  501. if (opt->help)
  502. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  503. av_log(av_log_obj, AV_LOG_INFO, "\n");
  504. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  505. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  506. }
  507. }
  508. }
  509. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  510. {
  511. if (!obj)
  512. return -1;
  513. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  514. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  515. return 0;
  516. }
  517. void av_opt_set_defaults(void *s)
  518. {
  519. #if FF_API_OLD_AVOPTIONS
  520. av_opt_set_defaults2(s, 0, 0);
  521. }
  522. void av_opt_set_defaults2(void *s, int mask, int flags)
  523. {
  524. #endif
  525. const AVOption *opt = NULL;
  526. while ((opt = av_opt_next(s, opt)) != NULL) {
  527. #if FF_API_OLD_AVOPTIONS
  528. if ((opt->flags & mask) != flags)
  529. continue;
  530. #endif
  531. switch (opt->type) {
  532. case AV_OPT_TYPE_CONST:
  533. /* Nothing to be done here */
  534. break;
  535. case AV_OPT_TYPE_FLAGS:
  536. case AV_OPT_TYPE_INT: {
  537. int val;
  538. val = opt->default_val.dbl;
  539. av_opt_set_int(s, opt->name, val, 0);
  540. }
  541. break;
  542. case AV_OPT_TYPE_INT64:
  543. if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
  544. av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
  545. av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
  546. break;
  547. case AV_OPT_TYPE_DOUBLE:
  548. case AV_OPT_TYPE_FLOAT: {
  549. double val;
  550. val = opt->default_val.dbl;
  551. av_opt_set_double(s, opt->name, val, 0);
  552. }
  553. break;
  554. case AV_OPT_TYPE_RATIONAL: {
  555. AVRational val;
  556. val = av_d2q(opt->default_val.dbl, INT_MAX);
  557. av_opt_set_q(s, opt->name, val, 0);
  558. }
  559. break;
  560. case AV_OPT_TYPE_STRING:
  561. av_opt_set(s, opt->name, opt->default_val.str, 0);
  562. break;
  563. case AV_OPT_TYPE_BINARY:
  564. /* Cannot set default for binary */
  565. break;
  566. default:
  567. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  568. }
  569. }
  570. }
  571. /**
  572. * Store the value in the field in ctx that is named like key.
  573. * ctx must be an AVClass context, storing is done using AVOptions.
  574. *
  575. * @param buf the string to parse, buf will be updated to point at the
  576. * separator just after the parsed key/value pair
  577. * @param key_val_sep a 0-terminated list of characters used to
  578. * separate key from value
  579. * @param pairs_sep a 0-terminated list of characters used to separate
  580. * two pairs from each other
  581. * @return 0 if the key/value pair has been successfully parsed and
  582. * set, or a negative value corresponding to an AVERROR code in case
  583. * of error:
  584. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  585. * the error code issued by av_opt_set() if the key/value pair
  586. * cannot be set
  587. */
  588. static int parse_key_value_pair(void *ctx, const char **buf,
  589. const char *key_val_sep, const char *pairs_sep)
  590. {
  591. char *key = av_get_token(buf, key_val_sep);
  592. char *val;
  593. int ret;
  594. if (*key && strspn(*buf, key_val_sep)) {
  595. (*buf)++;
  596. val = av_get_token(buf, pairs_sep);
  597. } else {
  598. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  599. av_free(key);
  600. return AVERROR(EINVAL);
  601. }
  602. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  603. ret = av_opt_set(ctx, key, val, 0);
  604. if (ret == AVERROR_OPTION_NOT_FOUND)
  605. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  606. av_free(key);
  607. av_free(val);
  608. return ret;
  609. }
  610. int av_set_options_string(void *ctx, const char *opts,
  611. const char *key_val_sep, const char *pairs_sep)
  612. {
  613. int ret, count = 0;
  614. if (!opts)
  615. return 0;
  616. while (*opts) {
  617. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  618. return ret;
  619. count++;
  620. if (*opts)
  621. opts++;
  622. }
  623. return count;
  624. }
  625. void av_opt_free(void *obj)
  626. {
  627. const AVOption *o = NULL;
  628. while ((o = av_opt_next(obj, o)))
  629. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  630. av_freep((uint8_t *)obj + o->offset);
  631. }
  632. int av_opt_set_dict(void *obj, AVDictionary **options)
  633. {
  634. AVDictionaryEntry *t = NULL;
  635. AVDictionary *tmp = NULL;
  636. int ret = 0;
  637. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  638. ret = av_opt_set(obj, t->key, t->value, 0);
  639. if (ret == AVERROR_OPTION_NOT_FOUND)
  640. av_dict_set(&tmp, t->key, t->value, 0);
  641. else if (ret < 0) {
  642. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  643. break;
  644. }
  645. ret = 0;
  646. }
  647. av_dict_free(options);
  648. *options = tmp;
  649. return ret;
  650. }
  651. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  652. int opt_flags, int search_flags)
  653. {
  654. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  655. }
  656. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  657. int opt_flags, int search_flags, void **target_obj)
  658. {
  659. const AVClass *c = *(AVClass**)obj;
  660. const AVOption *o = NULL;
  661. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  662. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  663. const AVClass *child = NULL;
  664. while (child = av_opt_child_class_next(c, child))
  665. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  666. return o;
  667. } else {
  668. void *child = NULL;
  669. while (child = av_opt_child_next(obj, child))
  670. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  671. return o;
  672. }
  673. }
  674. while (o = av_opt_next(obj, o)) {
  675. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  676. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  677. (unit && o->unit && !strcmp(o->unit, unit)))) {
  678. if (target_obj) {
  679. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  680. *target_obj = obj;
  681. else
  682. *target_obj = NULL;
  683. }
  684. return o;
  685. }
  686. }
  687. return NULL;
  688. }
  689. void *av_opt_child_next(void *obj, void *prev)
  690. {
  691. const AVClass *c = *(AVClass**)obj;
  692. if (c->child_next)
  693. return c->child_next(obj, prev);
  694. return NULL;
  695. }
  696. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  697. {
  698. if (parent->child_class_next)
  699. return parent->child_class_next(prev);
  700. return NULL;
  701. }
  702. #ifdef TEST
  703. #undef printf
  704. typedef struct TestContext
  705. {
  706. const AVClass *class;
  707. int num;
  708. int toggle;
  709. char *string;
  710. int flags;
  711. AVRational rational;
  712. } TestContext;
  713. #define OFFSET(x) offsetof(TestContext, x)
  714. #define TEST_FLAG_COOL 01
  715. #define TEST_FLAG_LAME 02
  716. #define TEST_FLAG_MU 04
  717. static const AVOption test_options[]= {
  718. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
  719. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
  720. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
  721. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  722. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
  723. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  724. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  725. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  726. {NULL},
  727. };
  728. static const char *test_get_name(void *ctx)
  729. {
  730. return "test";
  731. }
  732. static const AVClass test_class = {
  733. "TestContext",
  734. test_get_name,
  735. test_options
  736. };
  737. int main(void)
  738. {
  739. int i;
  740. printf("\nTesting av_set_options_string()\n");
  741. {
  742. TestContext test_ctx;
  743. const char *options[] = {
  744. "",
  745. ":",
  746. "=",
  747. "foo=:",
  748. ":=foo",
  749. "=foo",
  750. "foo=",
  751. "foo",
  752. "foo=val",
  753. "foo==val",
  754. "toggle=:",
  755. "string=:",
  756. "toggle=1 : foo",
  757. "toggle=100",
  758. "toggle==1",
  759. "flags=+mu-lame : num=42: toggle=0",
  760. "num=42 : string=blahblah",
  761. "rational=0 : rational=1/2 : rational=1/-1",
  762. "rational=-1/0",
  763. };
  764. test_ctx.class = &test_class;
  765. av_opt_set_defaults(&test_ctx);
  766. test_ctx.string = av_strdup("default");
  767. av_log_set_level(AV_LOG_DEBUG);
  768. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  769. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  770. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  771. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  772. printf("\n");
  773. }
  774. }
  775. return 0;
  776. }
  777. #endif