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.

607 lines
19KB

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