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.

653 lines
21KB

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