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.

1269 lines
41KB

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