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.

694 lines
22KB

  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 write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  51. {
  52. if (o->max*den < num*intnum || o->min*den > num*intnum) {
  53. av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, o->name);
  54. return AVERROR(ERANGE);
  55. }
  56. switch (o->type) {
  57. case FF_OPT_TYPE_FLAGS:
  58. case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  59. case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  60. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  61. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  62. case FF_OPT_TYPE_RATIONAL:
  63. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  64. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  65. break;
  66. default:
  67. return AVERROR(EINVAL);
  68. }
  69. return 0;
  70. }
  71. static const double const_values[] = {
  72. M_PI,
  73. M_E,
  74. FF_QP2LAMBDA,
  75. 0
  76. };
  77. static const char * const const_names[] = {
  78. "PI",
  79. "E",
  80. "QP2LAMBDA",
  81. 0
  82. };
  83. static int hexchar2int(char c) {
  84. if (c >= '0' && c <= '9') return c - '0';
  85. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  86. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  87. return -1;
  88. }
  89. static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  90. {
  91. int *lendst = (int *)(dst + 1);
  92. uint8_t *bin, *ptr;
  93. int len = strlen(val);
  94. av_freep(dst);
  95. *lendst = 0;
  96. if (len & 1)
  97. return AVERROR(EINVAL);
  98. len /= 2;
  99. ptr = bin = av_malloc(len);
  100. while (*val) {
  101. int a = hexchar2int(*val++);
  102. int b = hexchar2int(*val++);
  103. if (a < 0 || b < 0) {
  104. av_free(bin);
  105. return AVERROR(EINVAL);
  106. }
  107. *ptr++ = (a << 4) | b;
  108. }
  109. *dst = bin;
  110. *lendst = len;
  111. return 0;
  112. }
  113. static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  114. {
  115. av_freep(dst);
  116. *dst = av_strdup(val);
  117. return 0;
  118. }
  119. static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
  120. {
  121. int ret = 0, notfirst = 0;
  122. for (;;) {
  123. int i;
  124. char buf[256];
  125. int cmd = 0;
  126. double d;
  127. if (*val == '+' || *val == '-')
  128. cmd = *(val++);
  129. for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  130. buf[i] = val[i];
  131. buf[i] = 0;
  132. {
  133. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  134. if (o_named && o_named->type == FF_OPT_TYPE_CONST)
  135. d = o_named->default_val.dbl;
  136. else if (!strcmp(buf, "default")) d = o->default_val.dbl;
  137. else if (!strcmp(buf, "max" )) d = o->max;
  138. else if (!strcmp(buf, "min" )) d = o->min;
  139. else if (!strcmp(buf, "none" )) d = 0;
  140. else if (!strcmp(buf, "all" )) d = ~0;
  141. else {
  142. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  143. if (res < 0) {
  144. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  145. return res;
  146. }
  147. }
  148. }
  149. if (o->type == FF_OPT_TYPE_FLAGS) {
  150. if (cmd == '+') d = av_get_int(obj, o->name, NULL) | (int64_t)d;
  151. else if (cmd == '-') d = av_get_int(obj, o->name, NULL) &~(int64_t)d;
  152. } else {
  153. if (cmd == '+') d = notfirst*av_get_double(obj, o->name, NULL) + d;
  154. else if (cmd == '-') d = notfirst*av_get_double(obj, o->name, NULL) - d;
  155. }
  156. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  157. return ret;
  158. val += i;
  159. if (!*val)
  160. return 0;
  161. notfirst = 1;
  162. }
  163. return 0;
  164. }
  165. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  166. {
  167. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  168. void *dst;
  169. if (o_out)
  170. *o_out = o;
  171. if (!o)
  172. return AVERROR_OPTION_NOT_FOUND;
  173. if (!val && o->type != FF_OPT_TYPE_STRING)
  174. return AVERROR(EINVAL);
  175. dst = ((uint8_t*)obj) + o->offset;
  176. switch (o->type) {
  177. case FF_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  178. case FF_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  179. case FF_OPT_TYPE_FLAGS:
  180. case FF_OPT_TYPE_INT:
  181. case FF_OPT_TYPE_INT64:
  182. case FF_OPT_TYPE_FLOAT:
  183. case FF_OPT_TYPE_DOUBLE:
  184. case FF_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  185. }
  186. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  187. return AVERROR(EINVAL);
  188. }
  189. static const AVOption *set_number(void *obj, const char *name, double num, int den, int64_t intnum)
  190. {
  191. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  192. void *dst;
  193. if (!o)
  194. return NULL;
  195. dst = ((uint8_t*)obj) + o->offset;
  196. if (write_number(obj, o, dst, num, den, intnum) < 0)
  197. return NULL;
  198. else
  199. return o;
  200. }
  201. const AVOption *av_set_double(void *obj, const char *name, double n)
  202. {
  203. return set_number(obj, name, n, 1, 1);
  204. }
  205. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  206. {
  207. return set_number(obj, name, n.num, n.den, 1);
  208. }
  209. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  210. {
  211. return set_number(obj, name, 1, 1, n);
  212. }
  213. /**
  214. *
  215. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  216. * @param buf_len allocated length in bytes of buf
  217. */
  218. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  219. {
  220. const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  221. void *dst;
  222. uint8_t *bin;
  223. int len, i;
  224. if (!o)
  225. return NULL;
  226. if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  227. return NULL;
  228. dst= ((uint8_t*)obj) + o->offset;
  229. if (o_out) *o_out= o;
  230. switch (o->type) {
  231. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  232. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  233. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  234. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  235. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  236. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  237. case FF_OPT_TYPE_STRING: return *(void**)dst;
  238. case FF_OPT_TYPE_BINARY:
  239. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  240. if (len >= (buf_len + 1)/2) return NULL;
  241. bin = *(uint8_t**)dst;
  242. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  243. break;
  244. default: return NULL;
  245. }
  246. return buf;
  247. }
  248. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
  249. {
  250. const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  251. void *dst;
  252. if (!o || (o->offset<=0 && o->type != FF_OPT_TYPE_CONST))
  253. goto error;
  254. dst= ((uint8_t*)obj) + o->offset;
  255. if (o_out) *o_out= o;
  256. switch (o->type) {
  257. case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
  258. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  259. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  260. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  261. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  262. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  263. *den = ((AVRational*)dst)->den;
  264. return 0;
  265. case FF_OPT_TYPE_CONST: *intnum= o->default_val.dbl;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. if (!opts)
  479. return 0;
  480. while (*opts) {
  481. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  482. return ret;
  483. count++;
  484. if (*opts)
  485. opts++;
  486. }
  487. return count;
  488. }
  489. void av_opt_free(void *obj)
  490. {
  491. const AVOption *o = NULL;
  492. while ((o = av_next_option(obj, o)))
  493. if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
  494. av_freep((uint8_t *)obj + o->offset);
  495. }
  496. int av_opt_set_dict(void *obj, AVDictionary **options)
  497. {
  498. AVDictionaryEntry *t = NULL;
  499. AVDictionary *tmp = NULL;
  500. int ret = 0;
  501. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  502. ret = av_set_string3(obj, t->key, t->value, 1, NULL);
  503. if (ret == AVERROR_OPTION_NOT_FOUND)
  504. av_dict_set(&tmp, t->key, t->value, 0);
  505. else if (ret < 0) {
  506. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  507. break;
  508. }
  509. ret = 0;
  510. }
  511. av_dict_free(options);
  512. *options = tmp;
  513. return ret;
  514. }
  515. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  516. int opt_flags, int search_flags)
  517. {
  518. AVClass *c = *(AVClass**)obj;
  519. const AVOption *o = NULL;
  520. if (c->opt_find && search_flags & AV_OPT_SEARCH_CHILDREN &&
  521. (o = c->opt_find(obj, name, unit, opt_flags, search_flags)))
  522. return o;
  523. while (o = av_next_option(obj, o)) {
  524. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  525. ((!unit && o->type != FF_OPT_TYPE_CONST) ||
  526. (unit && o->type == FF_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit))))
  527. return o;
  528. }
  529. return NULL;
  530. }
  531. #ifdef TEST
  532. #undef printf
  533. typedef struct TestContext
  534. {
  535. const AVClass *class;
  536. int num;
  537. int toggle;
  538. char *string;
  539. int flags;
  540. AVRational rational;
  541. } TestContext;
  542. #define OFFSET(x) offsetof(TestContext, x)
  543. #define TEST_FLAG_COOL 01
  544. #define TEST_FLAG_LAME 02
  545. #define TEST_FLAG_MU 04
  546. static const AVOption test_options[]= {
  547. {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, {0}, 0, 100 },
  548. {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, {0}, 0, 1 },
  549. {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, {0}, 0, 10 },
  550. {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  551. {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
  552. {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  553. {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  554. {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  555. {NULL},
  556. };
  557. static const char *test_get_name(void *ctx)
  558. {
  559. return "test";
  560. }
  561. static const AVClass test_class = {
  562. "TestContext",
  563. test_get_name,
  564. test_options
  565. };
  566. int main(void)
  567. {
  568. int i;
  569. printf("\nTesting av_set_options_string()\n");
  570. {
  571. TestContext test_ctx;
  572. const char *options[] = {
  573. "",
  574. ":",
  575. "=",
  576. "foo=:",
  577. ":=foo",
  578. "=foo",
  579. "foo=",
  580. "foo",
  581. "foo=val",
  582. "foo==val",
  583. "toggle=:",
  584. "string=:",
  585. "toggle=1 : foo",
  586. "toggle=100",
  587. "toggle==1",
  588. "flags=+mu-lame : num=42: toggle=0",
  589. "num=42 : string=blahblah",
  590. "rational=0 : rational=1/2 : rational=1/-1",
  591. "rational=-1/0",
  592. };
  593. test_ctx.class = &test_class;
  594. av_opt_set_defaults(&test_ctx);
  595. test_ctx.string = av_strdup("default");
  596. av_log_set_level(AV_LOG_DEBUG);
  597. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  598. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  599. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  600. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  601. printf("\n");
  602. }
  603. }
  604. return 0;
  605. }
  606. #endif