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.

1271 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 AVOption *opt = NULL;
  719. while ((opt = av_opt_next(s, opt)) != NULL) {
  720. #if FF_API_OLD_AVOPTIONS
  721. if ((opt->flags & mask) != flags)
  722. continue;
  723. #endif
  724. switch (opt->type) {
  725. case AV_OPT_TYPE_CONST:
  726. /* Nothing to be done here */
  727. break;
  728. case AV_OPT_TYPE_FLAGS:
  729. case AV_OPT_TYPE_INT:
  730. case AV_OPT_TYPE_INT64:
  731. av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
  732. break;
  733. case AV_OPT_TYPE_DOUBLE:
  734. case AV_OPT_TYPE_FLOAT: {
  735. double val;
  736. val = opt->default_val.dbl;
  737. av_opt_set_double(s, opt->name, val, 0);
  738. }
  739. break;
  740. case AV_OPT_TYPE_RATIONAL: {
  741. AVRational val;
  742. val = av_d2q(opt->default_val.dbl, INT_MAX);
  743. av_opt_set_q(s, opt->name, val, 0);
  744. }
  745. break;
  746. case AV_OPT_TYPE_STRING:
  747. case AV_OPT_TYPE_IMAGE_SIZE:
  748. case AV_OPT_TYPE_PIXEL_FMT:
  749. case AV_OPT_TYPE_SAMPLE_FMT:
  750. av_opt_set(s, opt->name, opt->default_val.str, 0);
  751. break;
  752. case AV_OPT_TYPE_BINARY:
  753. /* Cannot set default for binary */
  754. break;
  755. default:
  756. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  757. }
  758. }
  759. }
  760. /**
  761. * Store the value in the field in ctx that is named like key.
  762. * ctx must be an AVClass context, storing is done using AVOptions.
  763. *
  764. * @param buf the string to parse, buf will be updated to point at the
  765. * separator just after the parsed key/value pair
  766. * @param key_val_sep a 0-terminated list of characters used to
  767. * separate key from value
  768. * @param pairs_sep a 0-terminated list of characters used to separate
  769. * two pairs from each other
  770. * @return 0 if the key/value pair has been successfully parsed and
  771. * set, or a negative value corresponding to an AVERROR code in case
  772. * of error:
  773. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  774. * the error code issued by av_opt_set() if the key/value pair
  775. * cannot be set
  776. */
  777. static int parse_key_value_pair(void *ctx, const char **buf,
  778. const char *key_val_sep, const char *pairs_sep)
  779. {
  780. char *key = av_get_token(buf, key_val_sep);
  781. char *val;
  782. int ret;
  783. if (*key && strspn(*buf, key_val_sep)) {
  784. (*buf)++;
  785. val = av_get_token(buf, pairs_sep);
  786. } else {
  787. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  788. av_free(key);
  789. return AVERROR(EINVAL);
  790. }
  791. av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
  792. ret = av_opt_set(ctx, key, val, 0);
  793. if (ret == AVERROR_OPTION_NOT_FOUND)
  794. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  795. av_free(key);
  796. av_free(val);
  797. return ret;
  798. }
  799. int av_set_options_string(void *ctx, const char *opts,
  800. const char *key_val_sep, const char *pairs_sep)
  801. {
  802. int ret, count = 0;
  803. if (!opts)
  804. return 0;
  805. while (*opts) {
  806. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  807. return ret;
  808. count++;
  809. if (*opts)
  810. opts++;
  811. }
  812. return count;
  813. }
  814. #define WHITESPACES " \n\t"
  815. static int is_key_char(char c)
  816. {
  817. return (unsigned)((c | 32) - 'a') < 26 ||
  818. (unsigned)(c - '0') < 10 ||
  819. c == '-' || c == '_' || c == '/' || c == '.';
  820. }
  821. /**
  822. * Read a key from a string.
  823. *
  824. * The key consists of is_key_char characters and must be terminated by a
  825. * character from the delim string; spaces are ignored.
  826. *
  827. * @return 0 for success (even with ellipsis), <0 for failure
  828. */
  829. static int get_key(const char **ropts, const char *delim, char **rkey)
  830. {
  831. const char *opts = *ropts;
  832. const char *key_start, *key_end;
  833. key_start = opts += strspn(opts, WHITESPACES);
  834. while (is_key_char(*opts))
  835. opts++;
  836. key_end = opts;
  837. opts += strspn(opts, WHITESPACES);
  838. if (!*opts || !strchr(delim, *opts))
  839. return AVERROR(EINVAL);
  840. opts++;
  841. if (!(*rkey = av_malloc(key_end - key_start + 1)))
  842. return AVERROR(ENOMEM);
  843. memcpy(*rkey, key_start, key_end - key_start);
  844. (*rkey)[key_end - key_start] = 0;
  845. *ropts = opts;
  846. return 0;
  847. }
  848. int av_opt_get_key_value(const char **ropts,
  849. const char *key_val_sep, const char *pairs_sep,
  850. unsigned flags,
  851. char **rkey, char **rval)
  852. {
  853. int ret;
  854. char *key = NULL, *val;
  855. const char *opts = *ropts;
  856. if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
  857. !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
  858. return AVERROR(EINVAL);
  859. if (!(val = av_get_token(&opts, pairs_sep))) {
  860. av_free(key);
  861. return AVERROR(ENOMEM);
  862. }
  863. *ropts = opts;
  864. *rkey = key;
  865. *rval = val;
  866. return 0;
  867. }
  868. int av_opt_set_from_string(void *ctx, const char *opts,
  869. const char *const *shorthand,
  870. const char *key_val_sep, const char *pairs_sep)
  871. {
  872. int ret, count = 0;
  873. const char *dummy_shorthand = NULL;
  874. char *av_uninit(parsed_key), *av_uninit(value);
  875. const char *key;
  876. if (!opts)
  877. return 0;
  878. if (!shorthand)
  879. shorthand = &dummy_shorthand;
  880. while (*opts) {
  881. ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
  882. *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  883. &parsed_key, &value);
  884. if (ret < 0) {
  885. if (ret == AVERROR(EINVAL))
  886. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
  887. else
  888. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
  889. av_err2str(ret));
  890. return ret;
  891. }
  892. if (*opts)
  893. opts++;
  894. if (parsed_key) {
  895. key = parsed_key;
  896. while (*shorthand) /* discard all remaining shorthand */
  897. shorthand++;
  898. } else {
  899. key = *(shorthand++);
  900. }
  901. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  902. if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
  903. if (ret == AVERROR_OPTION_NOT_FOUND)
  904. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  905. av_free(value);
  906. av_free(parsed_key);
  907. return ret;
  908. }
  909. av_free(value);
  910. av_free(parsed_key);
  911. count++;
  912. }
  913. return count;
  914. }
  915. void av_opt_free(void *obj)
  916. {
  917. const AVOption *o = NULL;
  918. while ((o = av_opt_next(obj, o)))
  919. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  920. av_freep((uint8_t *)obj + o->offset);
  921. }
  922. int av_opt_set_dict(void *obj, AVDictionary **options)
  923. {
  924. AVDictionaryEntry *t = NULL;
  925. AVDictionary *tmp = NULL;
  926. int ret = 0;
  927. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  928. ret = av_opt_set(obj, t->key, t->value, 0);
  929. if (ret == AVERROR_OPTION_NOT_FOUND)
  930. av_dict_set(&tmp, t->key, t->value, 0);
  931. else if (ret < 0) {
  932. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  933. break;
  934. }
  935. ret = 0;
  936. }
  937. av_dict_free(options);
  938. *options = tmp;
  939. return ret;
  940. }
  941. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  942. int opt_flags, int search_flags)
  943. {
  944. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  945. }
  946. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  947. int opt_flags, int search_flags, void **target_obj)
  948. {
  949. const AVClass *c;
  950. const AVOption *o = NULL;
  951. if(!obj)
  952. return NULL;
  953. c= *(AVClass**)obj;
  954. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  955. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  956. const AVClass *child = NULL;
  957. while (child = av_opt_child_class_next(c, child))
  958. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  959. return o;
  960. } else {
  961. void *child = NULL;
  962. while (child = av_opt_child_next(obj, child))
  963. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  964. return o;
  965. }
  966. }
  967. while (o = av_opt_next(obj, o)) {
  968. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  969. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  970. (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
  971. if (target_obj) {
  972. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  973. *target_obj = obj;
  974. else
  975. *target_obj = NULL;
  976. }
  977. return o;
  978. }
  979. }
  980. return NULL;
  981. }
  982. void *av_opt_child_next(void *obj, void *prev)
  983. {
  984. const AVClass *c = *(AVClass**)obj;
  985. if (c->child_next)
  986. return c->child_next(obj, prev);
  987. return NULL;
  988. }
  989. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  990. {
  991. if (parent->child_class_next)
  992. return parent->child_class_next(prev);
  993. return NULL;
  994. }
  995. void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
  996. {
  997. const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
  998. if(!opt)
  999. return NULL;
  1000. return (uint8_t*)obj + opt->offset;
  1001. }
  1002. #ifdef TEST
  1003. #undef printf
  1004. typedef struct TestContext
  1005. {
  1006. const AVClass *class;
  1007. int num;
  1008. int toggle;
  1009. char *string;
  1010. int flags;
  1011. AVRational rational;
  1012. int w, h;
  1013. enum AVPixelFormat pix_fmt;
  1014. enum AVSampleFormat sample_fmt;
  1015. } TestContext;
  1016. #define OFFSET(x) offsetof(TestContext, x)
  1017. #define TEST_FLAG_COOL 01
  1018. #define TEST_FLAG_LAME 02
  1019. #define TEST_FLAG_MU 04
  1020. static const AVOption test_options[]= {
  1021. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  1022. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  1023. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  1024. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  1025. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  1026. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  1027. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  1028. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  1029. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
  1030. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT,{0}, 0, 0 },
  1031. {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT,{0}, 0, 0 },
  1032. {NULL},
  1033. };
  1034. static const char *test_get_name(void *ctx)
  1035. {
  1036. return "test";
  1037. }
  1038. static const AVClass test_class = {
  1039. "TestContext",
  1040. test_get_name,
  1041. test_options
  1042. };
  1043. int main(void)
  1044. {
  1045. int i;
  1046. printf("\nTesting av_set_options_string()\n");
  1047. {
  1048. TestContext test_ctx = { 0 };
  1049. const char *options[] = {
  1050. "",
  1051. ":",
  1052. "=",
  1053. "foo=:",
  1054. ":=foo",
  1055. "=foo",
  1056. "foo=",
  1057. "foo",
  1058. "foo=val",
  1059. "foo==val",
  1060. "toggle=:",
  1061. "string=:",
  1062. "toggle=1 : foo",
  1063. "toggle=100",
  1064. "toggle==1",
  1065. "flags=+mu-lame : num=42: toggle=0",
  1066. "num=42 : string=blahblah",
  1067. "rational=0 : rational=1/2 : rational=1/-1",
  1068. "rational=-1/0",
  1069. "size=1024x768",
  1070. "size=pal",
  1071. "size=bogus",
  1072. "pix_fmt=yuv420p",
  1073. "pix_fmt=2",
  1074. "pix_fmt=bogus",
  1075. "sample_fmt=s16",
  1076. "sample_fmt=2",
  1077. "sample_fmt=bogus",
  1078. };
  1079. test_ctx.class = &test_class;
  1080. av_opt_set_defaults(&test_ctx);
  1081. test_ctx.string = av_strdup("default");
  1082. av_log_set_level(AV_LOG_DEBUG);
  1083. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1084. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1085. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  1086. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1087. printf("\n");
  1088. }
  1089. av_freep(&test_ctx.string);
  1090. }
  1091. printf("\nTesting av_opt_set_from_string()\n");
  1092. {
  1093. TestContext test_ctx = { 0 };
  1094. const char *options[] = {
  1095. "",
  1096. "5",
  1097. "5:hello",
  1098. "5:hello:size=pal",
  1099. "5:size=pal:hello",
  1100. ":",
  1101. "=",
  1102. " 5 : hello : size = pal ",
  1103. "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
  1104. };
  1105. const char *shorthand[] = { "num", "string", NULL };
  1106. test_ctx.class = &test_class;
  1107. av_opt_set_defaults(&test_ctx);
  1108. test_ctx.string = av_strdup("default");
  1109. av_log_set_level(AV_LOG_DEBUG);
  1110. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1111. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1112. if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
  1113. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1114. printf("\n");
  1115. }
  1116. av_freep(&test_ctx.string);
  1117. }
  1118. return 0;
  1119. }
  1120. #endif