You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1299 lines
42KB

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