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.

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