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.

673 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. return AVERROR(EINVAL);
  181. }
  182. if (alloc) {
  183. av_free(*(void**)(((uint8_t*)obj) + o->offset));
  184. val= av_strdup(val);
  185. }
  186. memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
  187. return 0;
  188. }
  189. const AVOption *av_set_double(void *obj, const char *name, double n)
  190. {
  191. return av_set_number(obj, name, n, 1, 1);
  192. }
  193. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  194. {
  195. return av_set_number(obj, name, n.num, n.den, 1);
  196. }
  197. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  198. {
  199. return av_set_number(obj, name, 1, 1, n);
  200. }
  201. /**
  202. *
  203. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  204. * @param buf_len allocated length in bytes of buf
  205. */
  206. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  207. {
  208. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  209. void *dst;
  210. uint8_t *bin;
  211. int len, i;
  212. if (!o || o->offset<=0)
  213. return NULL;
  214. if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  215. return NULL;
  216. dst= ((uint8_t*)obj) + o->offset;
  217. if (o_out) *o_out= o;
  218. switch (o->type) {
  219. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  220. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  221. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  222. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  223. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  224. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  225. case FF_OPT_TYPE_STRING: return *(void**)dst;
  226. case FF_OPT_TYPE_BINARY:
  227. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  228. if (len >= (buf_len + 1)/2) return NULL;
  229. bin = *(uint8_t**)dst;
  230. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  231. break;
  232. default: return NULL;
  233. }
  234. return buf;
  235. }
  236. static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
  237. {
  238. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  239. void *dst;
  240. if (!o || o->offset<=0)
  241. goto error;
  242. dst= ((uint8_t*)obj) + o->offset;
  243. if (o_out) *o_out= o;
  244. switch (o->type) {
  245. case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
  246. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  247. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  248. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  249. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  250. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  251. *den = ((AVRational*)dst)->den;
  252. 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 (av_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 (av_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 (av_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. /** Set the values of the AVCodecContext or AVFormatContext structure.
  369. * They are set to the defaults specified in the according AVOption options
  370. * array default_val field.
  371. *
  372. * @param s AVCodecContext or AVFormatContext for which the defaults will be set
  373. */
  374. void av_opt_set_defaults2(void *s, int mask, int flags)
  375. {
  376. const AVOption *opt = NULL;
  377. while ((opt = av_next_option(s, opt)) != NULL) {
  378. if ((opt->flags & mask) != flags)
  379. continue;
  380. switch (opt->type) {
  381. case FF_OPT_TYPE_CONST:
  382. /* Nothing to be done here */
  383. break;
  384. case FF_OPT_TYPE_FLAGS:
  385. case FF_OPT_TYPE_INT: {
  386. int val;
  387. val = opt->default_val.dbl;
  388. av_set_int(s, opt->name, val);
  389. }
  390. break;
  391. case FF_OPT_TYPE_INT64:
  392. if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
  393. av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
  394. av_set_int(s, opt->name, opt->default_val.dbl);
  395. break;
  396. case FF_OPT_TYPE_DOUBLE:
  397. case FF_OPT_TYPE_FLOAT: {
  398. double val;
  399. val = opt->default_val.dbl;
  400. av_set_double(s, opt->name, val);
  401. }
  402. break;
  403. case FF_OPT_TYPE_RATIONAL: {
  404. AVRational val;
  405. val = av_d2q(opt->default_val.dbl, INT_MAX);
  406. av_set_q(s, opt->name, val);
  407. }
  408. break;
  409. case FF_OPT_TYPE_STRING:
  410. av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
  411. break;
  412. case FF_OPT_TYPE_BINARY:
  413. /* Cannot set default for binary */
  414. break;
  415. default:
  416. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  417. }
  418. }
  419. }
  420. void av_opt_set_defaults(void *s)
  421. {
  422. av_opt_set_defaults2(s, 0, 0);
  423. }
  424. /**
  425. * Store the value in the field in ctx that is named like key.
  426. * ctx must be an AVClass context, storing is done using AVOptions.
  427. *
  428. * @param buf the string to parse, buf will be updated to point at the
  429. * separator just after the parsed key/value pair
  430. * @param key_val_sep a 0-terminated list of characters used to
  431. * separate key from value
  432. * @param pairs_sep a 0-terminated list of characters used to separate
  433. * two pairs from each other
  434. * @return 0 if the key/value pair has been successfully parsed and
  435. * set, or a negative value corresponding to an AVERROR code in case
  436. * of error:
  437. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  438. * the error code issued by av_set_string3() if the key/value pair
  439. * cannot be set
  440. */
  441. static int parse_key_value_pair(void *ctx, const char **buf,
  442. const char *key_val_sep, const char *pairs_sep)
  443. {
  444. char *key = av_get_token(buf, key_val_sep);
  445. char *val;
  446. int ret;
  447. if (*key && strspn(*buf, key_val_sep)) {
  448. (*buf)++;
  449. val = av_get_token(buf, pairs_sep);
  450. } else {
  451. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  452. av_free(key);
  453. return AVERROR(EINVAL);
  454. }
  455. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  456. ret = av_set_string3(ctx, key, val, 1, NULL);
  457. if (ret == AVERROR_OPTION_NOT_FOUND)
  458. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  459. av_free(key);
  460. av_free(val);
  461. return ret;
  462. }
  463. int av_set_options_string(void *ctx, const char *opts,
  464. const char *key_val_sep, const char *pairs_sep)
  465. {
  466. int ret, count = 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) && (!unit || (o->unit && !strcmp(o->unit, unit))) &&
  512. (o->flags & opt_flags) == opt_flags)
  513. return o;
  514. }
  515. return NULL;
  516. }
  517. #ifdef TEST
  518. #undef printf
  519. typedef struct TestContext
  520. {
  521. const AVClass *class;
  522. int num;
  523. int toggle;
  524. char *string;
  525. int flags;
  526. AVRational rational;
  527. } TestContext;
  528. #define OFFSET(x) offsetof(TestContext, x)
  529. #define TEST_FLAG_COOL 01
  530. #define TEST_FLAG_LAME 02
  531. #define TEST_FLAG_MU 04
  532. static const AVOption test_options[]= {
  533. {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, 0, 0, 100 },
  534. {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, 0, 0, 1 },
  535. {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, 0, 0, 10 },
  536. {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  537. {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, 0, 0, INT_MAX, 0, "flags" },
  538. {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_COOL, INT_MIN, INT_MAX, 0, "flags" },
  539. {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_LAME, INT_MIN, INT_MAX, 0, "flags" },
  540. {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_MU, INT_MIN, INT_MAX, 0, "flags" },
  541. {NULL},
  542. };
  543. static const char *test_get_name(void *ctx)
  544. {
  545. return "test";
  546. }
  547. static const AVClass test_class = {
  548. "TestContext",
  549. test_get_name,
  550. test_options
  551. };
  552. int main(void)
  553. {
  554. int i;
  555. printf("\nTesting av_set_options_string()\n");
  556. {
  557. TestContext test_ctx;
  558. const char *options[] = {
  559. "",
  560. ":",
  561. "=",
  562. "foo=:",
  563. ":=foo",
  564. "=foo",
  565. "foo=",
  566. "foo",
  567. "foo=val",
  568. "foo==val",
  569. "toggle=:",
  570. "string=:",
  571. "toggle=1 : foo",
  572. "toggle=100",
  573. "toggle==1",
  574. "flags=+mu-lame : num=42: toggle=0",
  575. "num=42 : string=blahblah",
  576. "rational=0 : rational=1/2 : rational=1/-1",
  577. "rational=-1/0",
  578. };
  579. test_ctx.class = &test_class;
  580. av_opt_set_defaults2(&test_ctx, 0, 0);
  581. test_ctx.string = av_strdup("default");
  582. av_log_set_level(AV_LOG_DEBUG);
  583. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  584. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  585. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  586. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  587. printf("\n");
  588. }
  589. }
  590. return 0;
  591. }
  592. #endif