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.

762 lines
24KB

  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. #if FF_API_OLD_AVOPTIONS
  167. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  168. {
  169. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  170. if (o_out)
  171. *o_out = o;
  172. return av_opt_set(obj, name, val, 0);
  173. }
  174. #endif
  175. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  176. {
  177. void *dst, *target_obj;
  178. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  179. if (!o || !target_obj)
  180. return AVERROR_OPTION_NOT_FOUND;
  181. if (!val)
  182. return AVERROR(EINVAL);
  183. dst = ((uint8_t*)target_obj) + o->offset;
  184. switch (o->type) {
  185. case FF_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  186. case FF_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  187. case FF_OPT_TYPE_FLAGS:
  188. case FF_OPT_TYPE_INT:
  189. case FF_OPT_TYPE_INT64:
  190. case FF_OPT_TYPE_FLOAT:
  191. case FF_OPT_TYPE_DOUBLE:
  192. case FF_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
  193. }
  194. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  195. return AVERROR(EINVAL);
  196. }
  197. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  198. int search_flags)
  199. {
  200. void *dst, *target_obj;
  201. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  202. if (!o || !target_obj)
  203. return AVERROR_OPTION_NOT_FOUND;
  204. dst = ((uint8_t*)target_obj) + o->offset;
  205. return write_number(obj, o, dst, num, den, intnum);
  206. }
  207. #if FF_API_OLD_AVOPTIONS
  208. const AVOption *av_set_double(void *obj, const char *name, double n)
  209. {
  210. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  211. if (set_number(obj, name, n, 1, 1, 0) < 0)
  212. return NULL;
  213. return o;
  214. }
  215. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  216. {
  217. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  218. if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
  219. return NULL;
  220. return o;
  221. }
  222. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  223. {
  224. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  225. if (set_number(obj, name, 1, 1, n, 0) < 0)
  226. return NULL;
  227. return o;
  228. }
  229. #endif
  230. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  231. {
  232. return set_number(obj, name, 1, 1, val, search_flags);
  233. }
  234. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  235. {
  236. return set_number(obj, name, val, 1, 1, search_flags);
  237. }
  238. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  239. {
  240. return set_number(obj, name, val.num, val.den, 1, search_flags);
  241. }
  242. /**
  243. *
  244. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  245. * @param buf_len allocated length in bytes of buf
  246. */
  247. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  248. {
  249. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  250. void *dst;
  251. uint8_t *bin;
  252. int len, i;
  253. if (!o)
  254. return NULL;
  255. if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  256. return NULL;
  257. dst= ((uint8_t*)obj) + o->offset;
  258. if (o_out) *o_out= o;
  259. switch (o->type) {
  260. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  261. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  262. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  263. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  264. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  265. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  266. case FF_OPT_TYPE_STRING: return *(void**)dst;
  267. case FF_OPT_TYPE_BINARY:
  268. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  269. if (len >= (buf_len + 1)/2) return NULL;
  270. bin = *(uint8_t**)dst;
  271. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  272. break;
  273. default: return NULL;
  274. }
  275. return buf;
  276. }
  277. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
  278. {
  279. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  280. void *dst;
  281. if (!o)
  282. goto error;
  283. dst= ((uint8_t*)obj) + o->offset;
  284. if (o_out) *o_out= o;
  285. switch (o->type) {
  286. case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
  287. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  288. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  289. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  290. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  291. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  292. *den = ((AVRational*)dst)->den;
  293. return 0;
  294. }
  295. error:
  296. *den=*intnum=0;
  297. return -1;
  298. }
  299. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  300. {
  301. int64_t intnum=1;
  302. double num=1;
  303. int den=1;
  304. if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  305. return NAN;
  306. return num*intnum/den;
  307. }
  308. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  309. {
  310. int64_t intnum=1;
  311. double num=1;
  312. int den=1;
  313. if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  314. return (AVRational){0, 0};
  315. if (num == 1.0 && (int)intnum == intnum)
  316. return (AVRational){intnum, den};
  317. else
  318. return av_d2q(num*intnum/den, 1<<24);
  319. }
  320. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  321. {
  322. int64_t intnum=1;
  323. double num=1;
  324. int den=1;
  325. if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
  326. return -1;
  327. return num*intnum/den;
  328. }
  329. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  330. {
  331. const AVOption *field = av_find_opt(obj, field_name, NULL, 0, 0);
  332. const AVOption *flag = av_find_opt(obj, flag_name, NULL, 0, 0);
  333. if (!field || !flag || flag->type != FF_OPT_TYPE_CONST)
  334. return 0;
  335. return av_get_int(obj, field_name, NULL) & (int) flag->default_val.dbl;
  336. }
  337. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  338. int req_flags, int rej_flags)
  339. {
  340. const AVOption *opt=NULL;
  341. while ((opt= av_next_option(obj, opt))) {
  342. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  343. continue;
  344. /* Don't print CONST's on level one.
  345. * Don't print anything but CONST's on level two.
  346. * Only print items from the requested unit.
  347. */
  348. if (!unit && opt->type==FF_OPT_TYPE_CONST)
  349. continue;
  350. else if (unit && opt->type!=FF_OPT_TYPE_CONST)
  351. continue;
  352. else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  353. continue;
  354. else if (unit && opt->type == FF_OPT_TYPE_CONST)
  355. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  356. else
  357. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  358. switch (opt->type) {
  359. case FF_OPT_TYPE_FLAGS:
  360. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
  361. break;
  362. case FF_OPT_TYPE_INT:
  363. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
  364. break;
  365. case FF_OPT_TYPE_INT64:
  366. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
  367. break;
  368. case FF_OPT_TYPE_DOUBLE:
  369. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
  370. break;
  371. case FF_OPT_TYPE_FLOAT:
  372. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
  373. break;
  374. case FF_OPT_TYPE_STRING:
  375. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
  376. break;
  377. case FF_OPT_TYPE_RATIONAL:
  378. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
  379. break;
  380. case FF_OPT_TYPE_BINARY:
  381. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
  382. break;
  383. case FF_OPT_TYPE_CONST:
  384. default:
  385. av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
  386. break;
  387. }
  388. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  389. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  390. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  391. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  392. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  393. if (opt->help)
  394. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  395. av_log(av_log_obj, AV_LOG_INFO, "\n");
  396. if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
  397. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  398. }
  399. }
  400. }
  401. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  402. {
  403. if (!obj)
  404. return -1;
  405. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  406. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  407. return 0;
  408. }
  409. void av_opt_set_defaults(void *s)
  410. {
  411. #if FF_API_OLD_AVOPTIONS
  412. av_opt_set_defaults2(s, 0, 0);
  413. }
  414. void av_opt_set_defaults2(void *s, int mask, int flags)
  415. {
  416. #endif
  417. const AVOption *opt = NULL;
  418. while ((opt = av_next_option(s, opt)) != NULL) {
  419. #if FF_API_OLD_AVOPTIONS
  420. if ((opt->flags & mask) != flags)
  421. continue;
  422. #endif
  423. switch (opt->type) {
  424. case FF_OPT_TYPE_CONST:
  425. /* Nothing to be done here */
  426. break;
  427. case FF_OPT_TYPE_FLAGS:
  428. case FF_OPT_TYPE_INT: {
  429. int val;
  430. val = opt->default_val.dbl;
  431. av_set_int(s, opt->name, val);
  432. }
  433. break;
  434. case FF_OPT_TYPE_INT64:
  435. if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
  436. av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
  437. av_set_int(s, opt->name, opt->default_val.dbl);
  438. break;
  439. case FF_OPT_TYPE_DOUBLE:
  440. case FF_OPT_TYPE_FLOAT: {
  441. double val;
  442. val = opt->default_val.dbl;
  443. av_set_double(s, opt->name, val);
  444. }
  445. break;
  446. case FF_OPT_TYPE_RATIONAL: {
  447. AVRational val;
  448. val = av_d2q(opt->default_val.dbl, INT_MAX);
  449. av_set_q(s, opt->name, val);
  450. }
  451. break;
  452. case FF_OPT_TYPE_STRING:
  453. av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
  454. break;
  455. case FF_OPT_TYPE_BINARY:
  456. /* Cannot set default for binary */
  457. break;
  458. default:
  459. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  460. }
  461. }
  462. }
  463. /**
  464. * Store the value in the field in ctx that is named like key.
  465. * ctx must be an AVClass context, storing is done using AVOptions.
  466. *
  467. * @param buf the string to parse, buf will be updated to point at the
  468. * separator just after the parsed key/value pair
  469. * @param key_val_sep a 0-terminated list of characters used to
  470. * separate key from value
  471. * @param pairs_sep a 0-terminated list of characters used to separate
  472. * two pairs from each other
  473. * @return 0 if the key/value pair has been successfully parsed and
  474. * set, or a negative value corresponding to an AVERROR code in case
  475. * of error:
  476. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  477. * the error code issued by av_set_string3() if the key/value pair
  478. * cannot be set
  479. */
  480. static int parse_key_value_pair(void *ctx, const char **buf,
  481. const char *key_val_sep, const char *pairs_sep)
  482. {
  483. char *key = av_get_token(buf, key_val_sep);
  484. char *val;
  485. int ret;
  486. if (*key && strspn(*buf, key_val_sep)) {
  487. (*buf)++;
  488. val = av_get_token(buf, pairs_sep);
  489. } else {
  490. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  491. av_free(key);
  492. return AVERROR(EINVAL);
  493. }
  494. av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
  495. ret = av_set_string3(ctx, key, val, 1, NULL);
  496. if (ret == AVERROR_OPTION_NOT_FOUND)
  497. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  498. av_free(key);
  499. av_free(val);
  500. return ret;
  501. }
  502. int av_set_options_string(void *ctx, const char *opts,
  503. const char *key_val_sep, const char *pairs_sep)
  504. {
  505. int ret, count = 0;
  506. while (*opts) {
  507. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  508. return ret;
  509. count++;
  510. if (*opts)
  511. opts++;
  512. }
  513. return count;
  514. }
  515. void av_opt_free(void *obj)
  516. {
  517. const AVOption *o = NULL;
  518. while ((o = av_next_option(obj, o)))
  519. if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
  520. av_freep((uint8_t *)obj + o->offset);
  521. }
  522. int av_opt_set_dict(void *obj, AVDictionary **options)
  523. {
  524. AVDictionaryEntry *t = NULL;
  525. AVDictionary *tmp = NULL;
  526. int ret = 0;
  527. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  528. ret = av_set_string3(obj, t->key, t->value, 1, NULL);
  529. if (ret == AVERROR_OPTION_NOT_FOUND)
  530. av_dict_set(&tmp, t->key, t->value, 0);
  531. else if (ret < 0) {
  532. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  533. break;
  534. }
  535. ret = 0;
  536. }
  537. av_dict_free(options);
  538. *options = tmp;
  539. return ret;
  540. }
  541. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  542. int opt_flags, int search_flags)
  543. {
  544. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  545. }
  546. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  547. int opt_flags, int search_flags, void **target_obj)
  548. {
  549. const AVClass *c = *(AVClass**)obj;
  550. const AVOption *o = NULL;
  551. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  552. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  553. const AVClass *child = NULL;
  554. while (child = av_opt_child_class_next(c, child))
  555. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  556. return o;
  557. } else {
  558. void *child = NULL;
  559. while (child = av_opt_child_next(obj, child))
  560. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  561. return o;
  562. }
  563. }
  564. while (o = av_next_option(obj, o)) {
  565. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  566. ((!unit && o->type != FF_OPT_TYPE_CONST) ||
  567. (unit && o->unit && !strcmp(o->unit, unit)))) {
  568. if (target_obj) {
  569. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  570. *target_obj = obj;
  571. else
  572. *target_obj = NULL;
  573. }
  574. return o;
  575. }
  576. }
  577. return NULL;
  578. }
  579. void *av_opt_child_next(void *obj, void *prev)
  580. {
  581. const AVClass *c = *(AVClass**)obj;
  582. if (c->child_next)
  583. return c->child_next(obj, prev);
  584. return NULL;
  585. }
  586. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  587. {
  588. if (parent->child_class_next)
  589. return parent->child_class_next(prev);
  590. return NULL;
  591. }
  592. #ifdef TEST
  593. #undef printf
  594. typedef struct TestContext
  595. {
  596. const AVClass *class;
  597. int num;
  598. int toggle;
  599. char *string;
  600. int flags;
  601. AVRational rational;
  602. } TestContext;
  603. #define OFFSET(x) offsetof(TestContext, x)
  604. #define TEST_FLAG_COOL 01
  605. #define TEST_FLAG_LAME 02
  606. #define TEST_FLAG_MU 04
  607. static const AVOption test_options[]= {
  608. {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, {0}, 0, 100 },
  609. {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, {0}, 0, 1 },
  610. {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, {0}, 0, 10 },
  611. {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
  612. {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
  613. {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  614. {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  615. {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  616. {NULL},
  617. };
  618. static const char *test_get_name(void *ctx)
  619. {
  620. return "test";
  621. }
  622. static const AVClass test_class = {
  623. "TestContext",
  624. test_get_name,
  625. test_options
  626. };
  627. int main(void)
  628. {
  629. int i;
  630. printf("\nTesting av_set_options_string()\n");
  631. {
  632. TestContext test_ctx;
  633. const char *options[] = {
  634. "",
  635. ":",
  636. "=",
  637. "foo=:",
  638. ":=foo",
  639. "=foo",
  640. "foo=",
  641. "foo",
  642. "foo=val",
  643. "foo==val",
  644. "toggle=:",
  645. "string=:",
  646. "toggle=1 : foo",
  647. "toggle=100",
  648. "toggle==1",
  649. "flags=+mu-lame : num=42: toggle=0",
  650. "num=42 : string=blahblah",
  651. "rational=0 : rational=1/2 : rational=1/-1",
  652. "rational=-1/0",
  653. };
  654. test_ctx.class = &test_class;
  655. av_opt_set_defaults(&test_ctx);
  656. test_ctx.string = av_strdup("default");
  657. av_log_set_level(AV_LOG_DEBUG);
  658. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  659. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  660. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  661. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  662. printf("\n");
  663. }
  664. }
  665. return 0;
  666. }
  667. #endif