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.

674 lines
21KB

  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 "opt.h"
  29. #include "eval.h"
  30. #include "dict.h"
  31. #include "log.h"
  32. #if FF_API_FIND_OPT
  33. //FIXME order them and do a bin search
  34. const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
  35. {
  36. const AVOption *o = NULL;
  37. while ((o = av_next_option(v, o))) {
  38. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
  39. return o;
  40. }
  41. return NULL;
  42. }
  43. #endif
  44. const AVOption *av_next_option(void *obj, const AVOption *last)
  45. {
  46. if (last && last[1].name) return ++last;
  47. else if (last || !(*(AVClass**)obj)->option->name) return NULL;
  48. else return (*(AVClass**)obj)->option;
  49. }
  50. static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out)
  51. {
  52. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  53. void *dst;
  54. if (o_out)
  55. *o_out= o;
  56. if (!o)
  57. return AVERROR_OPTION_NOT_FOUND;
  58. if (o->max*den < num*intnum || o->min*den > num*intnum) {
  59. av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
  60. return AVERROR(ERANGE);
  61. }
  62. dst= ((uint8_t*)obj) + o->offset;
  63. switch (o->type) {
  64. case FF_OPT_TYPE_FLAGS:
  65. case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  66. case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  67. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  68. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  69. case FF_OPT_TYPE_RATIONAL:
  70. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  71. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  72. break;
  73. default:
  74. return AVERROR(EINVAL);
  75. }
  76. return 0;
  77. }
  78. static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum)
  79. {
  80. const AVOption *o = NULL;
  81. if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
  82. return NULL;
  83. else
  84. return o;
  85. }
  86. static const double const_values[] = {
  87. M_PI,
  88. M_E,
  89. FF_QP2LAMBDA,
  90. 0
  91. };
  92. static const char * const const_names[] = {
  93. "PI",
  94. "E",
  95. "QP2LAMBDA",
  96. 0
  97. };
  98. static int hexchar2int(char c) {
  99. if (c >= '0' && c <= '9') return c - '0';
  100. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  101. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  102. return -1;
  103. }
  104. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  105. {
  106. int ret;
  107. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  108. if (o_out)
  109. *o_out = o;
  110. if (!o)
  111. return AVERROR_OPTION_NOT_FOUND;
  112. if (!val && o->type != FF_OPT_TYPE_STRING)
  113. return AVERROR(EINVAL);
  114. if (o->type == FF_OPT_TYPE_BINARY) {
  115. uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
  116. int *lendst = (int *)(dst + 1);
  117. uint8_t *bin, *ptr;
  118. int len = strlen(val);
  119. av_freep(dst);
  120. *lendst = 0;
  121. if (len & 1) return AVERROR(EINVAL);
  122. len /= 2;
  123. ptr = bin = av_malloc(len);
  124. while (*val) {
  125. int a = hexchar2int(*val++);
  126. int b = hexchar2int(*val++);
  127. if (a < 0 || b < 0) {
  128. av_free(bin);
  129. return AVERROR(EINVAL);
  130. }
  131. *ptr++ = (a << 4) | b;
  132. }
  133. *dst = bin;
  134. *lendst = len;
  135. return 0;
  136. }
  137. if (o->type != FF_OPT_TYPE_STRING) {
  138. int notfirst=0;
  139. for (;;) {
  140. int i;
  141. char buf[256];
  142. int cmd=0;
  143. double d;
  144. if (*val == '+' || *val == '-')
  145. cmd= *(val++);
  146. for (i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  147. buf[i]= val[i];
  148. buf[i]=0;
  149. {
  150. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  151. if (o_named && o_named->type == FF_OPT_TYPE_CONST)
  152. d= o_named->default_val.dbl;
  153. else if (!strcmp(buf, "default")) d= o->default_val.dbl;
  154. else if (!strcmp(buf, "max" )) d= o->max;
  155. else if (!strcmp(buf, "min" )) d= o->min;
  156. else if (!strcmp(buf, "none" )) d= 0;
  157. else if (!strcmp(buf, "all" )) d= ~0;
  158. else {
  159. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  160. if (res < 0) {
  161. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  162. return res;
  163. }
  164. }
  165. }
  166. if (o->type == FF_OPT_TYPE_FLAGS) {
  167. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  168. else if (cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  169. } else {
  170. if (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
  171. else if (cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
  172. }
  173. if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
  174. return ret;
  175. val+= i;
  176. if (!*val)
  177. return 0;
  178. notfirst=1;
  179. }
  180. }
  181. if (alloc) {
  182. av_free(*(void**)(((uint8_t*)obj) + o->offset));
  183. val= av_strdup(val);
  184. }
  185. memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
  186. return 0;
  187. }
  188. const AVOption *av_set_double(void *obj, const char *name, double n)
  189. {
  190. return av_set_number(obj, name, n, 1, 1);
  191. }
  192. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  193. {
  194. return av_set_number(obj, name, n.num, n.den, 1);
  195. }
  196. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  197. {
  198. return av_set_number(obj, name, 1, 1, n);
  199. }
  200. /**
  201. *
  202. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  203. * @param buf_len allocated length in bytes of buf
  204. */
  205. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  206. {
  207. const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  208. void *dst;
  209. uint8_t *bin;
  210. int len, i;
  211. if (!o)
  212. return NULL;
  213. if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  214. return NULL;
  215. dst= ((uint8_t*)obj) + o->offset;
  216. if (o_out) *o_out= o;
  217. switch (o->type) {
  218. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  219. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  220. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  221. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  222. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  223. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  224. case FF_OPT_TYPE_STRING: return *(void**)dst;
  225. case FF_OPT_TYPE_BINARY:
  226. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  227. if (len >= (buf_len + 1)/2) return NULL;
  228. bin = *(uint8_t**)dst;
  229. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  230. break;
  231. default: return NULL;
  232. }
  233. return buf;
  234. }
  235. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
  236. {
  237. const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  238. void *dst;
  239. if (!o || (o->offset<=0 && o->type != FF_OPT_TYPE_CONST))
  240. goto error;
  241. dst= ((uint8_t*)obj) + o->offset;
  242. if (o_out) *o_out= o;
  243. switch (o->type) {
  244. case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
  245. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  246. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  247. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  248. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  249. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  250. *den = ((AVRational*)dst)->den;
  251. return 0;
  252. case FF_OPT_TYPE_CONST: *intnum= o->default_val.dbl;return 0;
  253. }
  254. error:
  255. *den=*intnum=0;
  256. return -1;
  257. }
  258. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  259. {
  260. int64_t intnum=1;
  261. double num=1;
  262. int den=1;
  263. if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  264. return NAN;
  265. return num*intnum/den;
  266. }
  267. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  268. {
  269. int64_t intnum=1;
  270. double num=1;
  271. int den=1;
  272. if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  273. return (AVRational){0, 0};
  274. if (num == 1.0 && (int)intnum == intnum)
  275. return (AVRational){intnum, den};
  276. else
  277. return av_d2q(num*intnum/den, 1<<24);
  278. }
  279. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  280. {
  281. int64_t intnum=1;
  282. double num=1;
  283. int den=1;
  284. if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  285. return -1;
  286. return num*intnum/den;
  287. }
  288. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  289. {
  290. const AVOption *field = av_find_opt(obj, field_name, NULL, 0, 0);
  291. const AVOption *flag = av_find_opt(obj, flag_name, NULL, 0, 0);
  292. if (!field || !flag || flag->type != FF_OPT_TYPE_CONST)
  293. return 0;
  294. return av_get_int(obj, field_name, NULL) & (int) flag->default_val.dbl;
  295. }
  296. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  297. int req_flags, int rej_flags)
  298. {
  299. const AVOption *opt=NULL;
  300. while ((opt= av_next_option(obj, opt))) {
  301. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  302. continue;
  303. /* Don't print CONST's on level one.
  304. * Don't print anything but CONST's on level two.
  305. * Only print items from the requested unit.
  306. */
  307. if (!unit && opt->type==FF_OPT_TYPE_CONST)
  308. continue;
  309. else if (unit && opt->type!=FF_OPT_TYPE_CONST)
  310. continue;
  311. else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  312. continue;
  313. else if (unit && opt->type == FF_OPT_TYPE_CONST)
  314. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  315. else
  316. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  317. switch (opt->type) {
  318. case FF_OPT_TYPE_FLAGS:
  319. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  320. break;
  321. case FF_OPT_TYPE_INT:
  322. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  323. break;
  324. case FF_OPT_TYPE_INT64:
  325. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  326. break;
  327. case FF_OPT_TYPE_DOUBLE:
  328. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  329. break;
  330. case FF_OPT_TYPE_FLOAT:
  331. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  332. break;
  333. case FF_OPT_TYPE_STRING:
  334. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  335. break;
  336. case FF_OPT_TYPE_RATIONAL:
  337. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  338. break;
  339. case FF_OPT_TYPE_BINARY:
  340. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  341. break;
  342. case FF_OPT_TYPE_CONST:
  343. default:
  344. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  345. break;
  346. }
  347. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  348. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  349. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  350. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  351. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  352. if (opt->help)
  353. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  354. av_log(av_log_obj, AV_LOG_INFO, "\n");
  355. if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
  356. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  357. }
  358. }
  359. }
  360. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  361. {
  362. if (!obj)
  363. return -1;
  364. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  365. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  366. return 0;
  367. }
  368. void av_opt_set_defaults(void *s)
  369. {
  370. #if FF_API_OLD_AVOPTIONS
  371. av_opt_set_defaults2(s, 0, 0);
  372. }
  373. void av_opt_set_defaults2(void *s, int mask, int flags)
  374. {
  375. #endif
  376. const AVOption *opt = NULL;
  377. while ((opt = av_next_option(s, opt)) != NULL) {
  378. #if FF_API_OLD_AVOPTIONS
  379. if ((opt->flags & mask) != flags)
  380. continue;
  381. #endif
  382. switch (opt->type) {
  383. case FF_OPT_TYPE_CONST:
  384. /* Nothing to be done here */
  385. break;
  386. case FF_OPT_TYPE_FLAGS:
  387. case FF_OPT_TYPE_INT: {
  388. int val;
  389. val = opt->default_val.dbl;
  390. av_set_int(s, opt->name, val);
  391. }
  392. break;
  393. case FF_OPT_TYPE_INT64:
  394. if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
  395. av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
  396. av_set_int(s, opt->name, opt->default_val.dbl);
  397. break;
  398. case FF_OPT_TYPE_DOUBLE:
  399. case FF_OPT_TYPE_FLOAT: {
  400. double val;
  401. val = opt->default_val.dbl;
  402. av_set_double(s, opt->name, val);
  403. }
  404. break;
  405. case FF_OPT_TYPE_RATIONAL: {
  406. AVRational val;
  407. val = av_d2q(opt->default_val.dbl, INT_MAX);
  408. av_set_q(s, opt->name, val);
  409. }
  410. break;
  411. case FF_OPT_TYPE_STRING:
  412. av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
  413. break;
  414. case FF_OPT_TYPE_BINARY:
  415. /* Cannot set default for binary */
  416. break;
  417. default:
  418. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  419. }
  420. }
  421. }
  422. /**
  423. * Store the value in the field in ctx that is named like key.
  424. * ctx must be an AVClass context, storing is done using AVOptions.
  425. *
  426. * @param buf the string to parse, buf will be updated to point at the
  427. * separator just after the parsed key/value pair
  428. * @param key_val_sep a 0-terminated list of characters used to
  429. * separate key from value
  430. * @param pairs_sep a 0-terminated list of characters used to separate
  431. * two pairs from each other
  432. * @return 0 if the key/value pair has been successfully parsed and
  433. * set, or a negative value corresponding to an AVERROR code in case
  434. * of error:
  435. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  436. * the error code issued by av_set_string3() if the key/value pair
  437. * cannot be set
  438. */
  439. static int parse_key_value_pair(void *ctx, const char **buf,
  440. const char *key_val_sep, const char *pairs_sep)
  441. {
  442. char *key = av_get_token(buf, key_val_sep);
  443. char *val;
  444. int ret;
  445. if (*key && strspn(*buf, key_val_sep)) {
  446. (*buf)++;
  447. val = av_get_token(buf, pairs_sep);
  448. } else {
  449. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  450. av_free(key);
  451. return AVERROR(EINVAL);
  452. }
  453. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  454. ret = av_set_string3(ctx, key, val, 1, NULL);
  455. if (ret == AVERROR_OPTION_NOT_FOUND)
  456. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  457. av_free(key);
  458. av_free(val);
  459. return ret;
  460. }
  461. int av_set_options_string(void *ctx, const char *opts,
  462. const char *key_val_sep, const char *pairs_sep)
  463. {
  464. int ret, count = 0;
  465. if (!opts)
  466. return 0;
  467. while (*opts) {
  468. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  469. return ret;
  470. count++;
  471. if (*opts)
  472. opts++;
  473. }
  474. return count;
  475. }
  476. void av_opt_free(void *obj)
  477. {
  478. const AVOption *o = NULL;
  479. while ((o = av_next_option(obj, o)))
  480. if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
  481. av_freep((uint8_t *)obj + o->offset);
  482. }
  483. int av_opt_set_dict(void *obj, AVDictionary **options)
  484. {
  485. AVDictionaryEntry *t = NULL;
  486. AVDictionary *tmp = NULL;
  487. int ret = 0;
  488. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  489. ret = av_set_string3(obj, t->key, t->value, 1, NULL);
  490. if (ret == AVERROR_OPTION_NOT_FOUND)
  491. av_dict_set(&tmp, t->key, t->value, 0);
  492. else if (ret < 0) {
  493. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  494. break;
  495. }
  496. ret = 0;
  497. }
  498. av_dict_free(options);
  499. *options = tmp;
  500. return ret;
  501. }
  502. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  503. int opt_flags, int search_flags)
  504. {
  505. AVClass *c = *(AVClass**)obj;
  506. const AVOption *o = NULL;
  507. if (c->opt_find && search_flags & AV_OPT_SEARCH_CHILDREN &&
  508. (o = c->opt_find(obj, name, unit, opt_flags, search_flags)))
  509. return o;
  510. while (o = av_next_option(obj, o)) {
  511. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  512. ((!unit && o->type != FF_OPT_TYPE_CONST) ||
  513. (unit && o->type == FF_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit))))
  514. return o;
  515. }
  516. return NULL;
  517. }
  518. #ifdef TEST
  519. #undef printf
  520. typedef struct TestContext
  521. {
  522. const AVClass *class;
  523. int num;
  524. int toggle;
  525. char *string;
  526. int flags;
  527. AVRational rational;
  528. } TestContext;
  529. #define OFFSET(x) offsetof(TestContext, x)
  530. #define TEST_FLAG_COOL 01
  531. #define TEST_FLAG_LAME 02
  532. #define TEST_FLAG_MU 04
  533. static const AVOption test_options[]= {
  534. {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, {0}, 0, 100 },
  535. {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, {0}, 0, 1 },
  536. {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, {0}, 0, 10 },
  537. {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  538. {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
  539. {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  540. {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  541. {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  542. {NULL},
  543. };
  544. static const char *test_get_name(void *ctx)
  545. {
  546. return "test";
  547. }
  548. static const AVClass test_class = {
  549. "TestContext",
  550. test_get_name,
  551. test_options
  552. };
  553. int main(void)
  554. {
  555. int i;
  556. printf("\nTesting av_set_options_string()\n");
  557. {
  558. TestContext test_ctx;
  559. const char *options[] = {
  560. "",
  561. ":",
  562. "=",
  563. "foo=:",
  564. ":=foo",
  565. "=foo",
  566. "foo=",
  567. "foo",
  568. "foo=val",
  569. "foo==val",
  570. "toggle=:",
  571. "string=:",
  572. "toggle=1 : foo",
  573. "toggle=100",
  574. "toggle==1",
  575. "flags=+mu-lame : num=42: toggle=0",
  576. "num=42 : string=blahblah",
  577. "rational=0 : rational=1/2 : rational=1/-1",
  578. "rational=-1/0",
  579. };
  580. test_ctx.class = &test_class;
  581. av_opt_set_defaults(&test_ctx);
  582. test_ctx.string = av_strdup("default");
  583. av_log_set_level(AV_LOG_DEBUG);
  584. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  585. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  586. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  587. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  588. printf("\n");
  589. }
  590. }
  591. return 0;
  592. }
  593. #endif