opt.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. #include "samplefmt.h"
  37. #include <float.h>
  38. #if FF_API_FIND_OPT
  39. //FIXME order them and do a bin search
  40. const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
  41. {
  42. const AVOption *o = NULL;
  43. while ((o = av_next_option(v, o))) {
  44. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
  45. return o;
  46. }
  47. return NULL;
  48. }
  49. #endif
  50. #if FF_API_OLD_AVOPTIONS
  51. const AVOption *av_next_option(void *obj, const AVOption *last)
  52. {
  53. return av_opt_next(obj, last);
  54. }
  55. #endif
  56. const AVOption *av_opt_next(void *obj, const AVOption *last)
  57. {
  58. AVClass *class = *(AVClass**)obj;
  59. if (!last && class->option && class->option[0].name)
  60. return class->option;
  61. if (last && last[1].name)
  62. return ++last;
  63. return NULL;
  64. }
  65. static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
  66. {
  67. switch (o->type) {
  68. case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
  69. case AV_OPT_TYPE_PIXEL_FMT:
  70. case AV_OPT_TYPE_SAMPLE_FMT:
  71. case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
  72. case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
  73. case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
  74. case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
  75. case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
  76. *den = ((AVRational*)dst)->den;
  77. return 0;
  78. case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
  79. }
  80. return AVERROR(EINVAL);
  81. }
  82. static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  83. {
  84. if (o->max*den < num*intnum || o->min*den > num*intnum) {
  85. av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
  86. num*intnum/den, o->name, o->min, o->max);
  87. return AVERROR(ERANGE);
  88. }
  89. switch (o->type) {
  90. case AV_OPT_TYPE_FLAGS:
  91. case AV_OPT_TYPE_PIXEL_FMT:
  92. case AV_OPT_TYPE_SAMPLE_FMT:
  93. case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  94. case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  95. case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  96. case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  97. case AV_OPT_TYPE_RATIONAL:
  98. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  99. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  100. break;
  101. default:
  102. return AVERROR(EINVAL);
  103. }
  104. return 0;
  105. }
  106. static const double const_values[] = {
  107. M_PI,
  108. M_E,
  109. FF_QP2LAMBDA,
  110. 0
  111. };
  112. static const char * const const_names[] = {
  113. "PI",
  114. "E",
  115. "QP2LAMBDA",
  116. 0
  117. };
  118. static int hexchar2int(char c) {
  119. if (c >= '0' && c <= '9') return c - '0';
  120. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  121. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  122. return -1;
  123. }
  124. static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  125. {
  126. int *lendst = (int *)(dst + 1);
  127. uint8_t *bin, *ptr;
  128. int len = strlen(val);
  129. av_freep(dst);
  130. *lendst = 0;
  131. if (len & 1)
  132. return AVERROR(EINVAL);
  133. len /= 2;
  134. ptr = bin = av_malloc(len);
  135. while (*val) {
  136. int a = hexchar2int(*val++);
  137. int b = hexchar2int(*val++);
  138. if (a < 0 || b < 0) {
  139. av_free(bin);
  140. return AVERROR(EINVAL);
  141. }
  142. *ptr++ = (a << 4) | b;
  143. }
  144. *dst = bin;
  145. *lendst = len;
  146. return 0;
  147. }
  148. static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  149. {
  150. av_freep(dst);
  151. *dst = av_strdup(val);
  152. return 0;
  153. }
  154. #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
  155. opt->type == AV_OPT_TYPE_CONST || \
  156. opt->type == AV_OPT_TYPE_FLAGS || \
  157. opt->type == AV_OPT_TYPE_INT) ? \
  158. opt->default_val.i64 : opt->default_val.dbl)
  159. static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
  160. {
  161. int ret = 0, notfirst = 0;
  162. for (;;) {
  163. int i, den = 1;
  164. char buf[256];
  165. int cmd = 0;
  166. double d, num = 1;
  167. int64_t intnum = 1;
  168. i = 0;
  169. if (*val == '+' || *val == '-') {
  170. if (o->type == AV_OPT_TYPE_FLAGS)
  171. cmd = *(val++);
  172. else if (!notfirst)
  173. buf[i++] = *val;
  174. }
  175. for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  176. buf[i] = val[i];
  177. buf[i] = 0;
  178. {
  179. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  180. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  181. d = DEFAULT_NUMVAL(o_named);
  182. else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
  183. else if (!strcmp(buf, "max" )) d = o->max;
  184. else if (!strcmp(buf, "min" )) d = o->min;
  185. else if (!strcmp(buf, "none" )) d = 0;
  186. else if (!strcmp(buf, "all" )) d = ~0;
  187. else {
  188. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  189. if (res < 0) {
  190. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  191. return res;
  192. }
  193. }
  194. }
  195. if (o->type == AV_OPT_TYPE_FLAGS) {
  196. read_number(o, dst, NULL, NULL, &intnum);
  197. if (cmd == '+') d = intnum | (int64_t)d;
  198. else if (cmd == '-') d = intnum &~(int64_t)d;
  199. } else {
  200. read_number(o, dst, &num, &den, &intnum);
  201. if (cmd == '+') d = notfirst*num*intnum/den + d;
  202. else if (cmd == '-') d = notfirst*num*intnum/den - d;
  203. }
  204. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  205. return ret;
  206. val += i;
  207. if (!*val)
  208. return 0;
  209. notfirst = 1;
  210. }
  211. return 0;
  212. }
  213. #if FF_API_OLD_AVOPTIONS
  214. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  215. {
  216. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  217. if (o_out)
  218. *o_out = o;
  219. return av_opt_set(obj, name, val, 0);
  220. }
  221. #endif
  222. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  223. {
  224. int ret = 0;
  225. void *dst, *target_obj;
  226. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  227. if (!o || !target_obj)
  228. return AVERROR_OPTION_NOT_FOUND;
  229. if (!val && (o->type != AV_OPT_TYPE_STRING &&
  230. o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
  231. o->type != AV_OPT_TYPE_IMAGE_SIZE))
  232. return AVERROR(EINVAL);
  233. dst = ((uint8_t*)target_obj) + o->offset;
  234. switch (o->type) {
  235. case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  236. case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  237. case AV_OPT_TYPE_FLAGS:
  238. case AV_OPT_TYPE_INT:
  239. case AV_OPT_TYPE_INT64:
  240. case AV_OPT_TYPE_FLOAT:
  241. case AV_OPT_TYPE_DOUBLE:
  242. case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  243. case AV_OPT_TYPE_IMAGE_SIZE:
  244. if (!val || !strcmp(val, "none")) {
  245. *(int *)dst = *((int *)dst + 1) = 0;
  246. return 0;
  247. }
  248. ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
  249. if (ret < 0)
  250. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
  251. return ret;
  252. case AV_OPT_TYPE_PIXEL_FMT:
  253. if (!val || !strcmp(val, "none")) {
  254. ret = AV_PIX_FMT_NONE;
  255. } else {
  256. ret = av_get_pix_fmt(val);
  257. if (ret == AV_PIX_FMT_NONE) {
  258. char *tail;
  259. ret = strtol(val, &tail, 0);
  260. if (*tail || (unsigned)ret >= AV_PIX_FMT_NB) {
  261. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
  262. return AVERROR(EINVAL);
  263. }
  264. }
  265. }
  266. *(enum AVPixelFormat *)dst = ret;
  267. return 0;
  268. case AV_OPT_TYPE_SAMPLE_FMT:
  269. if (!val || !strcmp(val, "none")) {
  270. ret = AV_SAMPLE_FMT_NONE;
  271. } else {
  272. ret = av_get_sample_fmt(val);
  273. if (ret == AV_SAMPLE_FMT_NONE) {
  274. char *tail;
  275. ret = strtol(val, &tail, 0);
  276. if (*tail || (unsigned)ret >= AV_SAMPLE_FMT_NB) {
  277. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as sample format\n", val);
  278. return AVERROR(EINVAL);
  279. }
  280. }
  281. }
  282. *(enum AVSampleFormat *)dst = ret;
  283. return 0;
  284. }
  285. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  286. return AVERROR(EINVAL);
  287. }
  288. #define OPT_EVAL_NUMBER(name, opttype, vartype)\
  289. int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
  290. {\
  291. if (!o || o->type != opttype)\
  292. return AVERROR(EINVAL);\
  293. return set_string_number(obj, o, val, name ## _out);\
  294. }
  295. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  296. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  297. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  298. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  299. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  300. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  301. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  302. int search_flags)
  303. {
  304. void *dst, *target_obj;
  305. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  306. if (!o || !target_obj)
  307. return AVERROR_OPTION_NOT_FOUND;
  308. dst = ((uint8_t*)target_obj) + o->offset;
  309. return write_number(obj, o, dst, num, den, intnum);
  310. }
  311. #if FF_API_OLD_AVOPTIONS
  312. const AVOption *av_set_double(void *obj, const char *name, double n)
  313. {
  314. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  315. if (set_number(obj, name, n, 1, 1, 0) < 0)
  316. return NULL;
  317. return o;
  318. }
  319. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  320. {
  321. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  322. if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
  323. return NULL;
  324. return o;
  325. }
  326. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  327. {
  328. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  329. if (set_number(obj, name, 1, 1, n, 0) < 0)
  330. return NULL;
  331. return o;
  332. }
  333. #endif
  334. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  335. {
  336. return set_number(obj, name, 1, 1, val, search_flags);
  337. }
  338. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  339. {
  340. return set_number(obj, name, val, 1, 1, search_flags);
  341. }
  342. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  343. {
  344. return set_number(obj, name, val.num, val.den, 1, search_flags);
  345. }
  346. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  347. {
  348. void *target_obj;
  349. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  350. uint8_t *ptr;
  351. uint8_t **dst;
  352. int *lendst;
  353. if (!o || !target_obj)
  354. return AVERROR_OPTION_NOT_FOUND;
  355. if (o->type != AV_OPT_TYPE_BINARY)
  356. return AVERROR(EINVAL);
  357. ptr = av_malloc(len);
  358. if (!ptr)
  359. return AVERROR(ENOMEM);
  360. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  361. lendst = (int *)(dst + 1);
  362. av_free(*dst);
  363. *dst = ptr;
  364. *lendst = len;
  365. memcpy(ptr, val, len);
  366. return 0;
  367. }
  368. int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
  369. {
  370. void *target_obj;
  371. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  372. if (!o || !target_obj)
  373. return AVERROR_OPTION_NOT_FOUND;
  374. if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
  375. av_log(obj, AV_LOG_ERROR,
  376. "The value set by option '%s' is not an image size.\n", o->name);
  377. return AVERROR(EINVAL);
  378. }
  379. if (w<0 || h<0) {
  380. av_log(obj, AV_LOG_ERROR,
  381. "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
  382. return AVERROR(EINVAL);
  383. }
  384. *(int *)(((uint8_t *)target_obj) + o->offset) = w;
  385. *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
  386. return 0;
  387. }
  388. static int set_format(void *obj, const char *name, int fmt, int search_flags,
  389. enum AVOptionType type, const char *desc, int nb_fmts)
  390. {
  391. void *target_obj;
  392. const AVOption *o = av_opt_find2(obj, name, NULL, 0,
  393. search_flags, &target_obj);
  394. int min, max;
  395. const AVClass *class = *(AVClass **)obj;
  396. if (!o || !target_obj)
  397. return AVERROR_OPTION_NOT_FOUND;
  398. if (o->type != type) {
  399. av_log(obj, AV_LOG_ERROR,
  400. "The value set by option '%s' is not a %s format", name, desc);
  401. return AVERROR(EINVAL);
  402. }
  403. #if LIBAVUTIL_VERSION_MAJOR < 53
  404. if (class->version && class->version < AV_VERSION_INT(52, 11, 100)) {
  405. min = -1;
  406. max = nb_fmts-1;
  407. } else
  408. #endif
  409. {
  410. min = FFMIN(o->min, -1);
  411. max = FFMAX(o->max, nb_fmts-1);
  412. }
  413. if (fmt < min || fmt > max) {
  414. av_log(obj, AV_LOG_ERROR,
  415. "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
  416. fmt, name, desc, min, max);
  417. return AVERROR(ERANGE);
  418. }
  419. *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
  420. return 0;
  421. }
  422. int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
  423. {
  424. return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
  425. }
  426. int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
  427. {
  428. return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
  429. }
  430. #if FF_API_OLD_AVOPTIONS
  431. /**
  432. *
  433. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  434. * @param buf_len allocated length in bytes of buf
  435. */
  436. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  437. {
  438. const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  439. void *dst;
  440. uint8_t *bin;
  441. int len, i;
  442. if (!o)
  443. return NULL;
  444. if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
  445. return NULL;
  446. dst= ((uint8_t*)obj) + o->offset;
  447. if (o_out) *o_out= o;
  448. switch (o->type) {
  449. case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  450. case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  451. case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  452. case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  453. case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  454. case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  455. case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
  456. case AV_OPT_TYPE_STRING: return *(void**)dst;
  457. case AV_OPT_TYPE_BINARY:
  458. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  459. if (len >= (buf_len + 1)/2) return NULL;
  460. bin = *(uint8_t**)dst;
  461. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  462. break;
  463. default: return NULL;
  464. }
  465. return buf;
  466. }
  467. #endif
  468. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  469. {
  470. void *dst, *target_obj;
  471. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  472. uint8_t *bin, buf[128];
  473. int len, i, ret;
  474. if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
  475. return AVERROR_OPTION_NOT_FOUND;
  476. dst = (uint8_t*)target_obj + o->offset;
  477. buf[0] = 0;
  478. switch (o->type) {
  479. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  480. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  481. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  482. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  483. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  484. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  485. case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
  486. case AV_OPT_TYPE_STRING:
  487. if (*(uint8_t**)dst)
  488. *out_val = av_strdup(*(uint8_t**)dst);
  489. else
  490. *out_val = av_strdup("");
  491. return 0;
  492. case AV_OPT_TYPE_BINARY:
  493. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  494. if ((uint64_t)len*2 + 1 > INT_MAX)
  495. return AVERROR(EINVAL);
  496. if (!(*out_val = av_malloc(len*2 + 1)))
  497. return AVERROR(ENOMEM);
  498. bin = *(uint8_t**)dst;
  499. for (i = 0; i < len; i++)
  500. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  501. return 0;
  502. case AV_OPT_TYPE_IMAGE_SIZE:
  503. ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
  504. break;
  505. case AV_OPT_TYPE_PIXEL_FMT:
  506. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
  507. break;
  508. case AV_OPT_TYPE_SAMPLE_FMT:
  509. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
  510. break;
  511. default:
  512. return AVERROR(EINVAL);
  513. }
  514. if (ret >= sizeof(buf))
  515. return AVERROR(EINVAL);
  516. *out_val = av_strdup(buf);
  517. return 0;
  518. }
  519. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  520. int search_flags)
  521. {
  522. void *dst, *target_obj;
  523. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  524. if (!o || !target_obj)
  525. goto error;
  526. dst = ((uint8_t*)target_obj) + o->offset;
  527. if (o_out) *o_out= o;
  528. return read_number(o, dst, num, den, intnum);
  529. error:
  530. *den=*intnum=0;
  531. return -1;
  532. }
  533. #if FF_API_OLD_AVOPTIONS
  534. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  535. {
  536. int64_t intnum=1;
  537. double num=1;
  538. int den=1;
  539. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  540. return NAN;
  541. return num*intnum/den;
  542. }
  543. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  544. {
  545. int64_t intnum=1;
  546. double num=1;
  547. int den=1;
  548. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  549. return (AVRational){0, 0};
  550. if (num == 1.0 && (int)intnum == intnum)
  551. return (AVRational){intnum, den};
  552. else
  553. return av_d2q(num*intnum/den, 1<<24);
  554. }
  555. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  556. {
  557. int64_t intnum=1;
  558. double num=1;
  559. int den=1;
  560. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  561. return -1;
  562. return num*intnum/den;
  563. }
  564. #endif
  565. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  566. {
  567. int64_t intnum = 1;
  568. double num = 1;
  569. int ret, den = 1;
  570. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  571. return ret;
  572. *out_val = num*intnum/den;
  573. return 0;
  574. }
  575. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  576. {
  577. int64_t intnum = 1;
  578. double num = 1;
  579. int ret, den = 1;
  580. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  581. return ret;
  582. *out_val = num*intnum/den;
  583. return 0;
  584. }
  585. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  586. {
  587. int64_t intnum = 1;
  588. double num = 1;
  589. int ret, den = 1;
  590. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  591. return ret;
  592. if (num == 1.0 && (int)intnum == intnum)
  593. *out_val = (AVRational){intnum, den};
  594. else
  595. *out_val = av_d2q(num*intnum/den, 1<<24);
  596. return 0;
  597. }
  598. int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
  599. {
  600. void *dst, *target_obj;
  601. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  602. if (!o || !target_obj)
  603. return AVERROR_OPTION_NOT_FOUND;
  604. if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
  605. av_log(obj, AV_LOG_ERROR,
  606. "The value for option '%s' is not an image size.\n", name);
  607. return AVERROR(EINVAL);
  608. }
  609. dst = ((uint8_t*)target_obj) + o->offset;
  610. if (w_out) *w_out = *(int *)dst;
  611. if (h_out) *h_out = *((int *)dst+1);
  612. return 0;
  613. }
  614. static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
  615. enum AVOptionType type, const char *desc)
  616. {
  617. void *dst, *target_obj;
  618. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  619. if (!o || !target_obj)
  620. return AVERROR_OPTION_NOT_FOUND;
  621. if (o->type != type) {
  622. av_log(obj, AV_LOG_ERROR,
  623. "The value for option '%s' is not a %s format.\n", desc, name);
  624. return AVERROR(EINVAL);
  625. }
  626. dst = ((uint8_t*)target_obj) + o->offset;
  627. *out_fmt = *(int *)dst;
  628. return 0;
  629. }
  630. int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
  631. {
  632. return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
  633. }
  634. int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
  635. {
  636. return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
  637. }
  638. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  639. {
  640. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  641. const AVOption *flag = av_opt_find(obj, flag_name,
  642. field ? field->unit : NULL, 0, 0);
  643. int64_t res;
  644. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  645. av_opt_get_int(obj, field_name, 0, &res) < 0)
  646. return 0;
  647. return res & flag->default_val.i64;
  648. }
  649. static void log_value(void *av_log_obj, int level, double d)
  650. {
  651. if (d == INT_MAX) {
  652. av_log(av_log_obj, level, "INT_MAX");
  653. } else if (d == INT_MIN) {
  654. av_log(av_log_obj, level, "INT_MIN");
  655. } else if (d == (double)INT64_MAX) {
  656. av_log(av_log_obj, level, "I64_MAX");
  657. } else if (d == INT64_MIN) {
  658. av_log(av_log_obj, level, "I64_MIN");
  659. } else if (d == FLT_MAX) {
  660. av_log(av_log_obj, level, "FLT_MAX");
  661. } else if (d == FLT_MIN) {
  662. av_log(av_log_obj, level, "FLT_MIN");
  663. } else {
  664. av_log(av_log_obj, level, "%g", d);
  665. }
  666. }
  667. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  668. int req_flags, int rej_flags)
  669. {
  670. const AVOption *opt=NULL;
  671. AVOptionRanges *r;
  672. int i;
  673. while ((opt = av_opt_next(obj, opt))) {
  674. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  675. continue;
  676. /* Don't print CONST's on level one.
  677. * Don't print anything but CONST's on level two.
  678. * Only print items from the requested unit.
  679. */
  680. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  681. continue;
  682. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  683. continue;
  684. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  685. continue;
  686. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  687. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  688. else
  689. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  690. switch (opt->type) {
  691. case AV_OPT_TYPE_FLAGS:
  692. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
  693. break;
  694. case AV_OPT_TYPE_INT:
  695. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
  696. break;
  697. case AV_OPT_TYPE_INT64:
  698. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
  699. break;
  700. case AV_OPT_TYPE_DOUBLE:
  701. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
  702. break;
  703. case AV_OPT_TYPE_FLOAT:
  704. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
  705. break;
  706. case AV_OPT_TYPE_STRING:
  707. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
  708. break;
  709. case AV_OPT_TYPE_RATIONAL:
  710. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
  711. break;
  712. case AV_OPT_TYPE_BINARY:
  713. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
  714. break;
  715. case AV_OPT_TYPE_IMAGE_SIZE:
  716. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
  717. break;
  718. case AV_OPT_TYPE_PIXEL_FMT:
  719. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
  720. break;
  721. case AV_OPT_TYPE_SAMPLE_FMT:
  722. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
  723. break;
  724. case AV_OPT_TYPE_CONST:
  725. default:
  726. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
  727. break;
  728. }
  729. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  730. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  731. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
  732. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  733. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  734. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  735. if (opt->help)
  736. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  737. if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
  738. switch (opt->type) {
  739. case AV_OPT_TYPE_INT:
  740. case AV_OPT_TYPE_INT64:
  741. case AV_OPT_TYPE_DOUBLE:
  742. case AV_OPT_TYPE_FLOAT:
  743. case AV_OPT_TYPE_RATIONAL:
  744. for (i = 0; i < r->nb_ranges; i++) {
  745. av_log(av_log_obj, AV_LOG_INFO, " (from ");
  746. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
  747. av_log(av_log_obj, AV_LOG_INFO, " to ");
  748. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
  749. av_log(av_log_obj, AV_LOG_INFO, ")");
  750. }
  751. break;
  752. }
  753. av_opt_freep_ranges(&r);
  754. }
  755. av_log(av_log_obj, AV_LOG_INFO, "\n");
  756. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  757. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  758. }
  759. }
  760. }
  761. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  762. {
  763. if (!obj)
  764. return -1;
  765. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  766. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  767. return 0;
  768. }
  769. void av_opt_set_defaults(void *s)
  770. {
  771. #if FF_API_OLD_AVOPTIONS
  772. av_opt_set_defaults2(s, 0, 0);
  773. }
  774. void av_opt_set_defaults2(void *s, int mask, int flags)
  775. {
  776. #endif
  777. const AVClass *class = *(AVClass **)s;
  778. const AVOption *opt = NULL;
  779. while ((opt = av_opt_next(s, opt)) != NULL) {
  780. #if FF_API_OLD_AVOPTIONS
  781. if ((opt->flags & mask) != flags)
  782. continue;
  783. #endif
  784. switch (opt->type) {
  785. case AV_OPT_TYPE_CONST:
  786. /* Nothing to be done here */
  787. break;
  788. case AV_OPT_TYPE_FLAGS:
  789. case AV_OPT_TYPE_INT:
  790. case AV_OPT_TYPE_INT64:
  791. av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
  792. break;
  793. case AV_OPT_TYPE_DOUBLE:
  794. case AV_OPT_TYPE_FLOAT: {
  795. double val;
  796. val = opt->default_val.dbl;
  797. av_opt_set_double(s, opt->name, val, 0);
  798. }
  799. break;
  800. case AV_OPT_TYPE_RATIONAL: {
  801. AVRational val;
  802. val = av_d2q(opt->default_val.dbl, INT_MAX);
  803. av_opt_set_q(s, opt->name, val, 0);
  804. }
  805. break;
  806. case AV_OPT_TYPE_STRING:
  807. case AV_OPT_TYPE_IMAGE_SIZE:
  808. av_opt_set(s, opt->name, opt->default_val.str, 0);
  809. break;
  810. case AV_OPT_TYPE_PIXEL_FMT:
  811. #if LIBAVUTIL_VERSION_MAJOR < 53
  812. if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
  813. av_opt_set(s, opt->name, opt->default_val.str, 0);
  814. else
  815. #endif
  816. av_opt_set_pixel_fmt(s, opt->name, opt->default_val.i64, 0);
  817. break;
  818. case AV_OPT_TYPE_SAMPLE_FMT:
  819. #if LIBAVUTIL_VERSION_MAJOR < 53
  820. if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
  821. av_opt_set(s, opt->name, opt->default_val.str, 0);
  822. else
  823. #endif
  824. av_opt_set_sample_fmt(s, opt->name, opt->default_val.i64, 0);
  825. break;
  826. case AV_OPT_TYPE_BINARY:
  827. /* Cannot set default for binary */
  828. break;
  829. default:
  830. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  831. }
  832. }
  833. }
  834. /**
  835. * Store the value in the field in ctx that is named like key.
  836. * ctx must be an AVClass context, storing is done using AVOptions.
  837. *
  838. * @param buf the string to parse, buf will be updated to point at the
  839. * separator just after the parsed key/value pair
  840. * @param key_val_sep a 0-terminated list of characters used to
  841. * separate key from value
  842. * @param pairs_sep a 0-terminated list of characters used to separate
  843. * two pairs from each other
  844. * @return 0 if the key/value pair has been successfully parsed and
  845. * set, or a negative value corresponding to an AVERROR code in case
  846. * of error:
  847. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  848. * the error code issued by av_opt_set() if the key/value pair
  849. * cannot be set
  850. */
  851. static int parse_key_value_pair(void *ctx, const char **buf,
  852. const char *key_val_sep, const char *pairs_sep)
  853. {
  854. char *key = av_get_token(buf, key_val_sep);
  855. char *val;
  856. int ret;
  857. if (*key && strspn(*buf, key_val_sep)) {
  858. (*buf)++;
  859. val = av_get_token(buf, pairs_sep);
  860. } else {
  861. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  862. av_free(key);
  863. return AVERROR(EINVAL);
  864. }
  865. av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
  866. ret = av_opt_set(ctx, key, val, 0);
  867. if (ret == AVERROR_OPTION_NOT_FOUND)
  868. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  869. av_free(key);
  870. av_free(val);
  871. return ret;
  872. }
  873. int av_set_options_string(void *ctx, const char *opts,
  874. const char *key_val_sep, const char *pairs_sep)
  875. {
  876. int ret, count = 0;
  877. if (!opts)
  878. return 0;
  879. while (*opts) {
  880. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  881. return ret;
  882. count++;
  883. if (*opts)
  884. opts++;
  885. }
  886. return count;
  887. }
  888. #define WHITESPACES " \n\t"
  889. static int is_key_char(char c)
  890. {
  891. return (unsigned)((c | 32) - 'a') < 26 ||
  892. (unsigned)(c - '0') < 10 ||
  893. c == '-' || c == '_' || c == '/' || c == '.';
  894. }
  895. /**
  896. * Read a key from a string.
  897. *
  898. * The key consists of is_key_char characters and must be terminated by a
  899. * character from the delim string; spaces are ignored.
  900. *
  901. * @return 0 for success (even with ellipsis), <0 for failure
  902. */
  903. static int get_key(const char **ropts, const char *delim, char **rkey)
  904. {
  905. const char *opts = *ropts;
  906. const char *key_start, *key_end;
  907. key_start = opts += strspn(opts, WHITESPACES);
  908. while (is_key_char(*opts))
  909. opts++;
  910. key_end = opts;
  911. opts += strspn(opts, WHITESPACES);
  912. if (!*opts || !strchr(delim, *opts))
  913. return AVERROR(EINVAL);
  914. opts++;
  915. if (!(*rkey = av_malloc(key_end - key_start + 1)))
  916. return AVERROR(ENOMEM);
  917. memcpy(*rkey, key_start, key_end - key_start);
  918. (*rkey)[key_end - key_start] = 0;
  919. *ropts = opts;
  920. return 0;
  921. }
  922. int av_opt_get_key_value(const char **ropts,
  923. const char *key_val_sep, const char *pairs_sep,
  924. unsigned flags,
  925. char **rkey, char **rval)
  926. {
  927. int ret;
  928. char *key = NULL, *val;
  929. const char *opts = *ropts;
  930. if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
  931. !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
  932. return AVERROR(EINVAL);
  933. if (!(val = av_get_token(&opts, pairs_sep))) {
  934. av_free(key);
  935. return AVERROR(ENOMEM);
  936. }
  937. *ropts = opts;
  938. *rkey = key;
  939. *rval = val;
  940. return 0;
  941. }
  942. int av_opt_set_from_string(void *ctx, const char *opts,
  943. const char *const *shorthand,
  944. const char *key_val_sep, const char *pairs_sep)
  945. {
  946. int ret, count = 0;
  947. const char *dummy_shorthand = NULL;
  948. char *av_uninit(parsed_key), *av_uninit(value);
  949. const char *key;
  950. if (!opts)
  951. return 0;
  952. if (!shorthand)
  953. shorthand = &dummy_shorthand;
  954. while (*opts) {
  955. ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
  956. *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  957. &parsed_key, &value);
  958. if (ret < 0) {
  959. if (ret == AVERROR(EINVAL))
  960. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
  961. else
  962. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
  963. av_err2str(ret));
  964. return ret;
  965. }
  966. if (*opts)
  967. opts++;
  968. if (parsed_key) {
  969. key = parsed_key;
  970. while (*shorthand) /* discard all remaining shorthand */
  971. shorthand++;
  972. } else {
  973. key = *(shorthand++);
  974. }
  975. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  976. if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
  977. if (ret == AVERROR_OPTION_NOT_FOUND)
  978. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  979. av_free(value);
  980. av_free(parsed_key);
  981. return ret;
  982. }
  983. av_free(value);
  984. av_free(parsed_key);
  985. count++;
  986. }
  987. return count;
  988. }
  989. void av_opt_free(void *obj)
  990. {
  991. const AVOption *o = NULL;
  992. while ((o = av_opt_next(obj, o)))
  993. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  994. av_freep((uint8_t *)obj + o->offset);
  995. }
  996. int av_opt_set_dict(void *obj, AVDictionary **options)
  997. {
  998. AVDictionaryEntry *t = NULL;
  999. AVDictionary *tmp = NULL;
  1000. int ret = 0;
  1001. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  1002. ret = av_opt_set(obj, t->key, t->value, 0);
  1003. if (ret == AVERROR_OPTION_NOT_FOUND)
  1004. av_dict_set(&tmp, t->key, t->value, 0);
  1005. else if (ret < 0) {
  1006. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  1007. break;
  1008. }
  1009. ret = 0;
  1010. }
  1011. av_dict_free(options);
  1012. *options = tmp;
  1013. return ret;
  1014. }
  1015. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  1016. int opt_flags, int search_flags)
  1017. {
  1018. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  1019. }
  1020. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  1021. int opt_flags, int search_flags, void **target_obj)
  1022. {
  1023. const AVClass *c;
  1024. const AVOption *o = NULL;
  1025. if(!obj)
  1026. return NULL;
  1027. c= *(AVClass**)obj;
  1028. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  1029. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  1030. const AVClass *child = NULL;
  1031. while (child = av_opt_child_class_next(c, child))
  1032. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  1033. return o;
  1034. } else {
  1035. void *child = NULL;
  1036. while (child = av_opt_child_next(obj, child))
  1037. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  1038. return o;
  1039. }
  1040. }
  1041. while (o = av_opt_next(obj, o)) {
  1042. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  1043. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  1044. (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
  1045. if (target_obj) {
  1046. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  1047. *target_obj = obj;
  1048. else
  1049. *target_obj = NULL;
  1050. }
  1051. return o;
  1052. }
  1053. }
  1054. return NULL;
  1055. }
  1056. void *av_opt_child_next(void *obj, void *prev)
  1057. {
  1058. const AVClass *c = *(AVClass**)obj;
  1059. if (c->child_next)
  1060. return c->child_next(obj, prev);
  1061. return NULL;
  1062. }
  1063. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  1064. {
  1065. if (parent->child_class_next)
  1066. return parent->child_class_next(prev);
  1067. return NULL;
  1068. }
  1069. void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
  1070. {
  1071. const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
  1072. if(!opt)
  1073. return NULL;
  1074. return (uint8_t*)obj + opt->offset;
  1075. }
  1076. int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1077. {
  1078. const AVClass *c = *(AVClass**)obj;
  1079. int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
  1080. if (c->version > (52 << 16 | 11 << 8))
  1081. callback = c->query_ranges;
  1082. if (!callback)
  1083. callback = av_opt_query_ranges_default;
  1084. return callback(ranges_arg, obj, key, flags);
  1085. }
  1086. int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1087. {
  1088. AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
  1089. AVOptionRange **range_array = av_mallocz(sizeof(void*));
  1090. AVOptionRange *range = av_mallocz(sizeof(*range));
  1091. const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
  1092. int ret;
  1093. *ranges_arg = NULL;
  1094. if (!ranges || !range || !range_array || !field) {
  1095. ret = AVERROR(ENOMEM);
  1096. goto fail;
  1097. }
  1098. ranges->range = range_array;
  1099. ranges->range[0] = range;
  1100. ranges->nb_ranges = 1;
  1101. range->is_range = 1;
  1102. range->value_min = field->min;
  1103. range->value_max = field->max;
  1104. switch (field->type) {
  1105. case AV_OPT_TYPE_INT:
  1106. case AV_OPT_TYPE_INT64:
  1107. case AV_OPT_TYPE_PIXEL_FMT:
  1108. case AV_OPT_TYPE_SAMPLE_FMT:
  1109. case AV_OPT_TYPE_FLOAT:
  1110. case AV_OPT_TYPE_DOUBLE:
  1111. break;
  1112. case AV_OPT_TYPE_STRING:
  1113. range->component_min = 0;
  1114. range->component_max = 0x10FFFF; // max unicode value
  1115. range->value_min = -1;
  1116. range->value_max = INT_MAX;
  1117. break;
  1118. case AV_OPT_TYPE_RATIONAL:
  1119. range->component_min = INT_MIN;
  1120. range->component_max = INT_MAX;
  1121. break;
  1122. case AV_OPT_TYPE_IMAGE_SIZE:
  1123. range->component_min = 0;
  1124. range->component_max = INT_MAX/128/8;
  1125. range->value_min = 0;
  1126. range->value_max = INT_MAX/8;
  1127. break;
  1128. default:
  1129. ret = AVERROR(ENOSYS);
  1130. goto fail;
  1131. }
  1132. *ranges_arg = ranges;
  1133. return 0;
  1134. fail:
  1135. av_free(ranges);
  1136. av_free(range);
  1137. av_free(range_array);
  1138. return ret;
  1139. }
  1140. void av_opt_freep_ranges(AVOptionRanges **rangesp)
  1141. {
  1142. int i;
  1143. AVOptionRanges *ranges = *rangesp;
  1144. for (i = 0; i < ranges->nb_ranges; i++) {
  1145. AVOptionRange *range = ranges->range[i];
  1146. if (range) {
  1147. av_freep(&range->str);
  1148. av_freep(&ranges->range[i]);
  1149. }
  1150. }
  1151. av_freep(&ranges->range);
  1152. av_freep(rangesp);
  1153. }
  1154. #ifdef TEST
  1155. typedef struct TestContext
  1156. {
  1157. const AVClass *class;
  1158. int num;
  1159. int toggle;
  1160. char *string;
  1161. int flags;
  1162. AVRational rational;
  1163. int w, h;
  1164. enum AVPixelFormat pix_fmt;
  1165. enum AVSampleFormat sample_fmt;
  1166. } TestContext;
  1167. #define OFFSET(x) offsetof(TestContext, x)
  1168. #define TEST_FLAG_COOL 01
  1169. #define TEST_FLAG_LAME 02
  1170. #define TEST_FLAG_MU 04
  1171. static const AVOption test_options[]= {
  1172. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  1173. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  1174. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  1175. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  1176. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  1177. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  1178. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  1179. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  1180. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
  1181. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
  1182. {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
  1183. {NULL},
  1184. };
  1185. static const char *test_get_name(void *ctx)
  1186. {
  1187. return "test";
  1188. }
  1189. static const AVClass test_class = {
  1190. "TestContext",
  1191. test_get_name,
  1192. test_options
  1193. };
  1194. int main(void)
  1195. {
  1196. int i;
  1197. printf("\nTesting av_set_options_string()\n");
  1198. {
  1199. TestContext test_ctx = { 0 };
  1200. const char *options[] = {
  1201. "",
  1202. ":",
  1203. "=",
  1204. "foo=:",
  1205. ":=foo",
  1206. "=foo",
  1207. "foo=",
  1208. "foo",
  1209. "foo=val",
  1210. "foo==val",
  1211. "toggle=:",
  1212. "string=:",
  1213. "toggle=1 : foo",
  1214. "toggle=100",
  1215. "toggle==1",
  1216. "flags=+mu-lame : num=42: toggle=0",
  1217. "num=42 : string=blahblah",
  1218. "rational=0 : rational=1/2 : rational=1/-1",
  1219. "rational=-1/0",
  1220. "size=1024x768",
  1221. "size=pal",
  1222. "size=bogus",
  1223. "pix_fmt=yuv420p",
  1224. "pix_fmt=2",
  1225. "pix_fmt=bogus",
  1226. "sample_fmt=s16",
  1227. "sample_fmt=2",
  1228. "sample_fmt=bogus",
  1229. };
  1230. test_ctx.class = &test_class;
  1231. av_opt_set_defaults(&test_ctx);
  1232. test_ctx.string = av_strdup("default");
  1233. av_log_set_level(AV_LOG_DEBUG);
  1234. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1235. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1236. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  1237. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1238. printf("\n");
  1239. }
  1240. av_freep(&test_ctx.string);
  1241. }
  1242. printf("\nTesting av_opt_set_from_string()\n");
  1243. {
  1244. TestContext test_ctx = { 0 };
  1245. const char *options[] = {
  1246. "",
  1247. "5",
  1248. "5:hello",
  1249. "5:hello:size=pal",
  1250. "5:size=pal:hello",
  1251. ":",
  1252. "=",
  1253. " 5 : hello : size = pal ",
  1254. "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
  1255. };
  1256. const char *shorthand[] = { "num", "string", NULL };
  1257. test_ctx.class = &test_class;
  1258. av_opt_set_defaults(&test_ctx);
  1259. test_ctx.string = av_strdup("default");
  1260. av_log_set_level(AV_LOG_DEBUG);
  1261. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1262. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1263. if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
  1264. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1265. printf("\n");
  1266. }
  1267. av_freep(&test_ctx.string);
  1268. }
  1269. return 0;
  1270. }
  1271. #endif