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.

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