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.

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