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.

672 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. #if FF_API_FIND_OPT
  32. //FIXME order them and do a bin search
  33. const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
  34. {
  35. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  36. const AVOption *o= c->option;
  37. for (; o && o->name; 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) 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 || o->offset<=0)
  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->offset<=0)
  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, 0);
  208. void *dst;
  209. uint8_t *bin;
  210. int len, i;
  211. if (!o || o->offset<=0)
  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 av_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, 0);
  238. void *dst;
  239. if (!o || o->offset<=0)
  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. }
  253. error:
  254. *den=*intnum=0;
  255. return -1;
  256. }
  257. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  258. {
  259. int64_t intnum=1;
  260. double num=1;
  261. int den=1;
  262. if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  263. return NAN;
  264. return num*intnum/den;
  265. }
  266. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  267. {
  268. int64_t intnum=1;
  269. double num=1;
  270. int den=1;
  271. if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  272. return (AVRational){0, 0};
  273. if (num == 1.0 && (int)intnum == intnum)
  274. return (AVRational){intnum, den};
  275. else
  276. return av_d2q(num*intnum/den, 1<<24);
  277. }
  278. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  279. {
  280. int64_t intnum=1;
  281. double num=1;
  282. int den=1;
  283. if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  284. return -1;
  285. return num*intnum/den;
  286. }
  287. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  288. {
  289. const AVOption *field = av_find_opt(obj, field_name, NULL, 0, 0);
  290. const AVOption *flag = av_find_opt(obj, flag_name, NULL, 0, 0);
  291. if (!field || !flag || flag->type != FF_OPT_TYPE_CONST)
  292. return 0;
  293. return av_get_int(obj, field_name, NULL) & (int) flag->default_val.dbl;
  294. }
  295. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  296. int req_flags, int rej_flags)
  297. {
  298. const AVOption *opt=NULL;
  299. while ((opt= av_next_option(obj, opt))) {
  300. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  301. continue;
  302. /* Don't print CONST's on level one.
  303. * Don't print anything but CONST's on level two.
  304. * Only print items from the requested unit.
  305. */
  306. if (!unit && opt->type==FF_OPT_TYPE_CONST)
  307. continue;
  308. else if (unit && opt->type!=FF_OPT_TYPE_CONST)
  309. continue;
  310. else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  311. continue;
  312. else if (unit && opt->type == FF_OPT_TYPE_CONST)
  313. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  314. else
  315. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  316. switch (opt->type) {
  317. case FF_OPT_TYPE_FLAGS:
  318. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  319. break;
  320. case FF_OPT_TYPE_INT:
  321. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  322. break;
  323. case FF_OPT_TYPE_INT64:
  324. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  325. break;
  326. case FF_OPT_TYPE_DOUBLE:
  327. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  328. break;
  329. case FF_OPT_TYPE_FLOAT:
  330. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  331. break;
  332. case FF_OPT_TYPE_STRING:
  333. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  334. break;
  335. case FF_OPT_TYPE_RATIONAL:
  336. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  337. break;
  338. case FF_OPT_TYPE_BINARY:
  339. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  340. break;
  341. case FF_OPT_TYPE_CONST:
  342. default:
  343. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  344. break;
  345. }
  346. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  347. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  348. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  349. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  350. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  351. if (opt->help)
  352. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  353. av_log(av_log_obj, AV_LOG_INFO, "\n");
  354. if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
  355. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  356. }
  357. }
  358. }
  359. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  360. {
  361. if (!obj)
  362. return -1;
  363. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  364. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  365. return 0;
  366. }
  367. /** Set the values of the AVCodecContext or AVFormatContext structure.
  368. * They are set to the defaults specified in the according AVOption options
  369. * array default_val field.
  370. *
  371. * @param s AVCodecContext or AVFormatContext for which the defaults will be set
  372. */
  373. void av_opt_set_defaults2(void *s, int mask, int flags)
  374. {
  375. const AVOption *opt = NULL;
  376. while ((opt = av_next_option(s, opt)) != NULL) {
  377. if ((opt->flags & mask) != flags)
  378. continue;
  379. switch (opt->type) {
  380. case FF_OPT_TYPE_CONST:
  381. /* Nothing to be done here */
  382. break;
  383. case FF_OPT_TYPE_FLAGS:
  384. case FF_OPT_TYPE_INT: {
  385. int val;
  386. val = opt->default_val.dbl;
  387. av_set_int(s, opt->name, val);
  388. }
  389. break;
  390. case FF_OPT_TYPE_INT64:
  391. if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
  392. av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
  393. av_set_int(s, opt->name, opt->default_val.dbl);
  394. break;
  395. case FF_OPT_TYPE_DOUBLE:
  396. case FF_OPT_TYPE_FLOAT: {
  397. double val;
  398. val = opt->default_val.dbl;
  399. av_set_double(s, opt->name, val);
  400. }
  401. break;
  402. case FF_OPT_TYPE_RATIONAL: {
  403. AVRational val;
  404. val = av_d2q(opt->default_val.dbl, INT_MAX);
  405. av_set_q(s, opt->name, val);
  406. }
  407. break;
  408. case FF_OPT_TYPE_STRING:
  409. av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
  410. break;
  411. case FF_OPT_TYPE_BINARY:
  412. /* Cannot set default for binary */
  413. break;
  414. default:
  415. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  416. }
  417. }
  418. }
  419. void av_opt_set_defaults(void *s)
  420. {
  421. av_opt_set_defaults2(s, 0, 0);
  422. }
  423. /**
  424. * Store the value in the field in ctx that is named like key.
  425. * ctx must be an AVClass context, storing is done using AVOptions.
  426. *
  427. * @param buf the string to parse, buf will be updated to point at the
  428. * separator just after the parsed key/value pair
  429. * @param key_val_sep a 0-terminated list of characters used to
  430. * separate key from value
  431. * @param pairs_sep a 0-terminated list of characters used to separate
  432. * two pairs from each other
  433. * @return 0 if the key/value pair has been successfully parsed and
  434. * set, or a negative value corresponding to an AVERROR code in case
  435. * of error:
  436. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  437. * the error code issued by av_set_string3() if the key/value pair
  438. * cannot be set
  439. */
  440. static int parse_key_value_pair(void *ctx, const char **buf,
  441. const char *key_val_sep, const char *pairs_sep)
  442. {
  443. char *key = av_get_token(buf, key_val_sep);
  444. char *val;
  445. int ret;
  446. if (*key && strspn(*buf, key_val_sep)) {
  447. (*buf)++;
  448. val = av_get_token(buf, pairs_sep);
  449. } else {
  450. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  451. av_free(key);
  452. return AVERROR(EINVAL);
  453. }
  454. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  455. ret = av_set_string3(ctx, key, val, 1, NULL);
  456. if (ret == AVERROR_OPTION_NOT_FOUND)
  457. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  458. av_free(key);
  459. av_free(val);
  460. return ret;
  461. }
  462. int av_set_options_string(void *ctx, const char *opts,
  463. const char *key_val_sep, const char *pairs_sep)
  464. {
  465. int ret, count = 0;
  466. while (*opts) {
  467. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  468. return ret;
  469. count++;
  470. if (*opts)
  471. opts++;
  472. }
  473. return count;
  474. }
  475. void av_opt_free(void *obj)
  476. {
  477. const AVOption *o = NULL;
  478. while ((o = av_next_option(obj, o)))
  479. if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
  480. av_freep((uint8_t *)obj + o->offset);
  481. }
  482. int av_opt_set_dict(void *obj, AVDictionary **options)
  483. {
  484. AVDictionaryEntry *t = NULL;
  485. AVDictionary *tmp = NULL;
  486. int ret = 0;
  487. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  488. ret = av_set_string3(obj, t->key, t->value, 1, NULL);
  489. if (ret == AVERROR_OPTION_NOT_FOUND)
  490. av_dict_set(&tmp, t->key, t->value, 0);
  491. else if (ret < 0) {
  492. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  493. break;
  494. }
  495. ret = 0;
  496. }
  497. av_dict_free(options);
  498. *options = tmp;
  499. return ret;
  500. }
  501. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  502. int opt_flags, int search_flags)
  503. {
  504. AVClass *c = *(AVClass**)obj;
  505. const AVOption *o = NULL;
  506. if (c->opt_find && search_flags & AV_OPT_SEARCH_CHILDREN &&
  507. (o = c->opt_find(obj, name, unit, opt_flags, search_flags)))
  508. return o;
  509. while (o = av_next_option(obj, o)) {
  510. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) &&
  511. (o->flags & opt_flags) == opt_flags)
  512. return o;
  513. }
  514. return NULL;
  515. }
  516. #ifdef TEST
  517. #undef printf
  518. typedef struct TestContext
  519. {
  520. const AVClass *class;
  521. int num;
  522. int toggle;
  523. char *string;
  524. int flags;
  525. AVRational rational;
  526. } TestContext;
  527. #define OFFSET(x) offsetof(TestContext, x)
  528. #define TEST_FLAG_COOL 01
  529. #define TEST_FLAG_LAME 02
  530. #define TEST_FLAG_MU 04
  531. static const AVOption test_options[]= {
  532. {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, {0}, 0, 100 },
  533. {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, {0}, 0, 1 },
  534. {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, {0}, 0, 10 },
  535. {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  536. {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
  537. {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  538. {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  539. {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  540. {NULL},
  541. };
  542. static const char *test_get_name(void *ctx)
  543. {
  544. return "test";
  545. }
  546. static const AVClass test_class = {
  547. "TestContext",
  548. test_get_name,
  549. test_options
  550. };
  551. int main(void)
  552. {
  553. int i;
  554. printf("\nTesting av_set_options_string()\n");
  555. {
  556. TestContext test_ctx;
  557. const char *options[] = {
  558. "",
  559. ":",
  560. "=",
  561. "foo=:",
  562. ":=foo",
  563. "=foo",
  564. "foo=",
  565. "foo",
  566. "foo=val",
  567. "foo==val",
  568. "toggle=:",
  569. "string=:",
  570. "toggle=1 : foo",
  571. "toggle=100",
  572. "toggle==1",
  573. "flags=+mu-lame : num=42: toggle=0",
  574. "num=42 : string=blahblah",
  575. "rational=0 : rational=1/2 : rational=1/-1",
  576. "rational=-1/0",
  577. };
  578. test_ctx.class = &test_class;
  579. av_opt_set_defaults2(&test_ctx, 0, 0);
  580. test_ctx.string = av_strdup("default");
  581. av_log_set_level(AV_LOG_DEBUG);
  582. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  583. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  584. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  585. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  586. printf("\n");
  587. }
  588. }
  589. return 0;
  590. }
  591. #endif