opt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. #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. const AVOption *o = NULL;
  36. while ((o = av_next_option(v, o))) {
  37. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
  38. return o;
  39. }
  40. return NULL;
  41. }
  42. #endif
  43. const AVOption *av_next_option(void *obj, const AVOption *last)
  44. {
  45. if (last && last[1].name) return ++last;
  46. else if (last || !(*(AVClass**)obj)->option->name) return NULL;
  47. else return (*(AVClass**)obj)->option;
  48. }
  49. static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out)
  50. {
  51. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  52. void *dst;
  53. if (o_out)
  54. *o_out= o;
  55. if (!o || o->offset<=0)
  56. return AVERROR_OPTION_NOT_FOUND;
  57. if (o->max*den < num*intnum || o->min*den > num*intnum) {
  58. av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
  59. return AVERROR(ERANGE);
  60. }
  61. dst= ((uint8_t*)obj) + o->offset;
  62. switch (o->type) {
  63. case FF_OPT_TYPE_FLAGS:
  64. case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  65. case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  66. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  67. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  68. case FF_OPT_TYPE_RATIONAL:
  69. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  70. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  71. break;
  72. default:
  73. return AVERROR(EINVAL);
  74. }
  75. return 0;
  76. }
  77. static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum)
  78. {
  79. const AVOption *o = NULL;
  80. if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
  81. return NULL;
  82. else
  83. return o;
  84. }
  85. static const double const_values[] = {
  86. M_PI,
  87. M_E,
  88. FF_QP2LAMBDA,
  89. 0
  90. };
  91. static const char * const const_names[] = {
  92. "PI",
  93. "E",
  94. "QP2LAMBDA",
  95. 0
  96. };
  97. static int hexchar2int(char c) {
  98. if (c >= '0' && c <= '9') return c - '0';
  99. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  100. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  101. return -1;
  102. }
  103. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  104. {
  105. int ret;
  106. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  107. if (o_out)
  108. *o_out = o;
  109. if (!o)
  110. return AVERROR_OPTION_NOT_FOUND;
  111. if ((!val && o->type != FF_OPT_TYPE_STRING) || o->offset<=0)
  112. return AVERROR(EINVAL);
  113. if (o->type == FF_OPT_TYPE_BINARY) {
  114. uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
  115. int *lendst = (int *)(dst + 1);
  116. uint8_t *bin, *ptr;
  117. int len = strlen(val);
  118. av_freep(dst);
  119. *lendst = 0;
  120. if (len & 1) return AVERROR(EINVAL);
  121. len /= 2;
  122. ptr = bin = av_malloc(len);
  123. while (*val) {
  124. int a = hexchar2int(*val++);
  125. int b = hexchar2int(*val++);
  126. if (a < 0 || b < 0) {
  127. av_free(bin);
  128. return AVERROR(EINVAL);
  129. }
  130. *ptr++ = (a << 4) | b;
  131. }
  132. *dst = bin;
  133. *lendst = len;
  134. return 0;
  135. }
  136. if (o->type != FF_OPT_TYPE_STRING) {
  137. int notfirst=0;
  138. for (;;) {
  139. int i;
  140. char buf[256];
  141. int cmd=0;
  142. double d;
  143. if (*val == '+' || *val == '-')
  144. cmd= *(val++);
  145. for (i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  146. buf[i]= val[i];
  147. buf[i]=0;
  148. {
  149. const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
  150. if (o_named && o_named->type == FF_OPT_TYPE_CONST)
  151. d= o_named->default_val.dbl;
  152. else if (!strcmp(buf, "default")) d= o->default_val.dbl;
  153. else if (!strcmp(buf, "max" )) d= o->max;
  154. else if (!strcmp(buf, "min" )) d= o->min;
  155. else if (!strcmp(buf, "none" )) d= 0;
  156. else if (!strcmp(buf, "all" )) d= ~0;
  157. else {
  158. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  159. if (res < 0) {
  160. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  161. return res;
  162. }
  163. }
  164. }
  165. if (o->type == FF_OPT_TYPE_FLAGS) {
  166. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  167. else if (cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  168. } else {
  169. if (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
  170. else if (cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
  171. }
  172. if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
  173. return ret;
  174. val+= i;
  175. if (!*val)
  176. return 0;
  177. notfirst=1;
  178. }
  179. return AVERROR(EINVAL);
  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 && o->type != FF_OPT_TYPE_CONST))
  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. case FF_OPT_TYPE_CONST: *intnum= o->default_val.dbl;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. if (!opts)
  468. return 0;
  469. while (*opts) {
  470. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  471. return ret;
  472. count++;
  473. if (*opts)
  474. opts++;
  475. }
  476. return count;
  477. }
  478. void av_opt_free(void *obj)
  479. {
  480. const AVOption *o = NULL;
  481. while ((o = av_next_option(obj, o)))
  482. if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
  483. av_freep((uint8_t *)obj + o->offset);
  484. }
  485. int av_opt_set_dict(void *obj, AVDictionary **options)
  486. {
  487. AVDictionaryEntry *t = NULL;
  488. AVDictionary *tmp = NULL;
  489. int ret = 0;
  490. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  491. ret = av_set_string3(obj, t->key, t->value, 1, NULL);
  492. if (ret == AVERROR_OPTION_NOT_FOUND)
  493. av_dict_set(&tmp, t->key, t->value, 0);
  494. else if (ret < 0) {
  495. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  496. break;
  497. }
  498. ret = 0;
  499. }
  500. av_dict_free(options);
  501. *options = tmp;
  502. return ret;
  503. }
  504. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  505. int opt_flags, int search_flags)
  506. {
  507. AVClass *c = *(AVClass**)obj;
  508. const AVOption *o = NULL;
  509. if (c->opt_find && search_flags & AV_OPT_SEARCH_CHILDREN &&
  510. (o = c->opt_find(obj, name, unit, opt_flags, search_flags)))
  511. return o;
  512. while (o = av_next_option(obj, o)) {
  513. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) &&
  514. (o->flags & opt_flags) == opt_flags)
  515. return o;
  516. }
  517. return NULL;
  518. }
  519. #ifdef TEST
  520. #undef printf
  521. typedef struct TestContext
  522. {
  523. const AVClass *class;
  524. int num;
  525. int toggle;
  526. char *string;
  527. int flags;
  528. AVRational rational;
  529. } TestContext;
  530. #define OFFSET(x) offsetof(TestContext, x)
  531. #define TEST_FLAG_COOL 01
  532. #define TEST_FLAG_LAME 02
  533. #define TEST_FLAG_MU 04
  534. static const AVOption test_options[]= {
  535. {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, 0, 0, 100 },
  536. {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, 0, 0, 1 },
  537. {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, 0, 0, 10 },
  538. {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  539. {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, 0, 0, INT_MAX, 0, "flags" },
  540. {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_COOL, INT_MIN, INT_MAX, 0, "flags" },
  541. {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_LAME, INT_MIN, INT_MAX, 0, "flags" },
  542. {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, TEST_FLAG_MU, INT_MIN, INT_MAX, 0, "flags" },
  543. {NULL},
  544. };
  545. static const char *test_get_name(void *ctx)
  546. {
  547. return "test";
  548. }
  549. static const AVClass test_class = {
  550. "TestContext",
  551. test_get_name,
  552. test_options
  553. };
  554. int main(void)
  555. {
  556. int i;
  557. printf("\nTesting av_set_options_string()\n");
  558. {
  559. TestContext test_ctx;
  560. const char *options[] = {
  561. "",
  562. ":",
  563. "=",
  564. "foo=:",
  565. ":=foo",
  566. "=foo",
  567. "foo=",
  568. "foo",
  569. "foo=val",
  570. "foo==val",
  571. "toggle=:",
  572. "string=:",
  573. "toggle=1 : foo",
  574. "toggle=100",
  575. "toggle==1",
  576. "flags=+mu-lame : num=42: toggle=0",
  577. "num=42 : string=blahblah",
  578. "rational=0 : rational=1/2 : rational=1/-1",
  579. "rational=-1/0",
  580. };
  581. test_ctx.class = &test_class;
  582. av_opt_set_defaults2(&test_ctx, 0, 0);
  583. test_ctx.string = av_strdup("default");
  584. av_log_set_level(AV_LOG_DEBUG);
  585. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  586. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  587. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  588. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  589. printf("\n");
  590. }
  591. }
  592. return 0;
  593. }
  594. #endif