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.

977 lines
36KB

  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * scale video filter
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "scale_eval.h"
  30. #include "video.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/eval.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/opt.h"
  36. #include "libavutil/parseutils.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "libavutil/imgutils.h"
  39. #include "libavutil/avassert.h"
  40. #include "libswscale/swscale.h"
  41. static const char *const var_names[] = {
  42. "in_w", "iw",
  43. "in_h", "ih",
  44. "out_w", "ow",
  45. "out_h", "oh",
  46. "a",
  47. "sar",
  48. "dar",
  49. "hsub",
  50. "vsub",
  51. "ohsub",
  52. "ovsub",
  53. "main_w",
  54. "main_h",
  55. "main_a",
  56. "main_sar",
  57. "main_dar", "mdar",
  58. "main_hsub",
  59. "main_vsub",
  60. NULL
  61. };
  62. enum var_name {
  63. VAR_IN_W, VAR_IW,
  64. VAR_IN_H, VAR_IH,
  65. VAR_OUT_W, VAR_OW,
  66. VAR_OUT_H, VAR_OH,
  67. VAR_A,
  68. VAR_SAR,
  69. VAR_DAR,
  70. VAR_HSUB,
  71. VAR_VSUB,
  72. VAR_OHSUB,
  73. VAR_OVSUB,
  74. VAR_S2R_MAIN_W,
  75. VAR_S2R_MAIN_H,
  76. VAR_S2R_MAIN_A,
  77. VAR_S2R_MAIN_SAR,
  78. VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,
  79. VAR_S2R_MAIN_HSUB,
  80. VAR_S2R_MAIN_VSUB,
  81. VARS_NB
  82. };
  83. enum EvalMode {
  84. EVAL_MODE_INIT,
  85. EVAL_MODE_FRAME,
  86. EVAL_MODE_NB
  87. };
  88. typedef struct ScaleContext {
  89. const AVClass *class;
  90. struct SwsContext *sws; ///< software scaler context
  91. struct SwsContext *isws[2]; ///< software scaler context for interlaced material
  92. AVDictionary *opts;
  93. /**
  94. * New dimensions. Special values are:
  95. * 0 = original width/height
  96. * -1 = keep original aspect
  97. * -N = try to keep aspect but make sure it is divisible by N
  98. */
  99. int w, h;
  100. char *size_str;
  101. unsigned int flags; ///sws flags
  102. double param[2]; // sws params
  103. int hsub, vsub; ///< chroma subsampling
  104. int slice_y; ///< top of current output slice
  105. int input_is_pal; ///< set to 1 if the input format is paletted
  106. int output_is_pal; ///< set to 1 if the output format is paletted
  107. int interlaced;
  108. char *w_expr; ///< width expression string
  109. char *h_expr; ///< height expression string
  110. AVExpr *w_pexpr;
  111. AVExpr *h_pexpr;
  112. double var_values[VARS_NB];
  113. char *flags_str;
  114. char *in_color_matrix;
  115. char *out_color_matrix;
  116. int in_range;
  117. int out_range;
  118. int out_h_chr_pos;
  119. int out_v_chr_pos;
  120. int in_h_chr_pos;
  121. int in_v_chr_pos;
  122. int force_original_aspect_ratio;
  123. int force_divisible_by;
  124. int nb_slices;
  125. int eval_mode; ///< expression evaluation mode
  126. } ScaleContext;
  127. AVFilter ff_vf_scale2ref;
  128. static int config_props(AVFilterLink *outlink);
  129. static int check_exprs(AVFilterContext *ctx)
  130. {
  131. ScaleContext *scale = ctx->priv;
  132. unsigned vars_w[VARS_NB] = { 0 }, vars_h[VARS_NB] = { 0 };
  133. if (!scale->w_pexpr && !scale->h_pexpr)
  134. return AVERROR(EINVAL);
  135. if (scale->w_pexpr)
  136. av_expr_count_vars(scale->w_pexpr, vars_w, VARS_NB);
  137. if (scale->h_pexpr)
  138. av_expr_count_vars(scale->h_pexpr, vars_h, VARS_NB);
  139. if (vars_w[VAR_OUT_W] || vars_w[VAR_OW]) {
  140. av_log(ctx, AV_LOG_ERROR, "Width expression cannot be self-referencing: '%s'.\n", scale->w_expr);
  141. return AVERROR(EINVAL);
  142. }
  143. if (vars_h[VAR_OUT_H] || vars_h[VAR_OH]) {
  144. av_log(ctx, AV_LOG_ERROR, "Height expression cannot be self-referencing: '%s'.\n", scale->h_expr);
  145. return AVERROR(EINVAL);
  146. }
  147. if ((vars_w[VAR_OUT_H] || vars_w[VAR_OH]) &&
  148. (vars_h[VAR_OUT_W] || vars_h[VAR_OW])) {
  149. av_log(ctx, AV_LOG_ERROR, "Circular expressions invalid for width '%s' and height '%s'.\n", scale->w_expr, scale->h_expr);
  150. return AVERROR(EINVAL);
  151. }
  152. if (ctx->filter != &ff_vf_scale2ref &&
  153. (vars_w[VAR_S2R_MAIN_W] || vars_h[VAR_S2R_MAIN_W] ||
  154. vars_w[VAR_S2R_MAIN_H] || vars_h[VAR_S2R_MAIN_H] ||
  155. vars_w[VAR_S2R_MAIN_A] || vars_h[VAR_S2R_MAIN_A] ||
  156. vars_w[VAR_S2R_MAIN_SAR] || vars_h[VAR_S2R_MAIN_SAR] ||
  157. vars_w[VAR_S2R_MAIN_DAR] || vars_h[VAR_S2R_MAIN_DAR] ||
  158. vars_w[VAR_S2R_MDAR] || vars_h[VAR_S2R_MDAR] ||
  159. vars_w[VAR_S2R_MAIN_HSUB] || vars_h[VAR_S2R_MAIN_HSUB] ||
  160. vars_w[VAR_S2R_MAIN_VSUB] || vars_h[VAR_S2R_MAIN_VSUB]) ) {
  161. av_log(ctx, AV_LOG_ERROR, "Expressions with scale2ref variables are not valid in scale filter.\n");
  162. return AVERROR(EINVAL);
  163. }
  164. return 0;
  165. }
  166. static int scale_parse_expr(AVFilterContext *ctx, char *str_expr, AVExpr **pexpr_ptr, const char *var, const char *args)
  167. {
  168. ScaleContext *scale = ctx->priv;
  169. int ret, is_inited = 0;
  170. char *old_str_expr = NULL;
  171. AVExpr *old_pexpr = NULL;
  172. if (str_expr) {
  173. old_str_expr = av_strdup(str_expr);
  174. if (!old_str_expr)
  175. return AVERROR(ENOMEM);
  176. av_opt_set(scale, var, args, 0);
  177. }
  178. if (*pexpr_ptr) {
  179. old_pexpr = *pexpr_ptr;
  180. *pexpr_ptr = NULL;
  181. is_inited = 1;
  182. }
  183. ret = av_expr_parse(pexpr_ptr, args, var_names,
  184. NULL, NULL, NULL, NULL, 0, ctx);
  185. if (ret < 0) {
  186. av_log(ctx, AV_LOG_ERROR, "Cannot parse expression for %s: '%s'\n", var, args);
  187. goto revert;
  188. }
  189. ret = check_exprs(ctx);
  190. if (ret < 0)
  191. goto revert;
  192. if (is_inited && (ret = config_props(ctx->outputs[0])) < 0)
  193. goto revert;
  194. av_expr_free(old_pexpr);
  195. old_pexpr = NULL;
  196. av_freep(&old_str_expr);
  197. return 0;
  198. revert:
  199. av_expr_free(*pexpr_ptr);
  200. *pexpr_ptr = NULL;
  201. if (old_str_expr) {
  202. av_opt_set(scale, var, old_str_expr, 0);
  203. av_free(old_str_expr);
  204. }
  205. if (old_pexpr)
  206. *pexpr_ptr = old_pexpr;
  207. return ret;
  208. }
  209. static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
  210. {
  211. ScaleContext *scale = ctx->priv;
  212. int ret;
  213. if (scale->size_str && (scale->w_expr || scale->h_expr)) {
  214. av_log(ctx, AV_LOG_ERROR,
  215. "Size and width/height expressions cannot be set at the same time.\n");
  216. return AVERROR(EINVAL);
  217. }
  218. if (scale->w_expr && !scale->h_expr)
  219. FFSWAP(char *, scale->w_expr, scale->size_str);
  220. if (scale->size_str) {
  221. char buf[32];
  222. if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
  223. av_log(ctx, AV_LOG_ERROR,
  224. "Invalid size '%s'\n", scale->size_str);
  225. return ret;
  226. }
  227. snprintf(buf, sizeof(buf)-1, "%d", scale->w);
  228. av_opt_set(scale, "w", buf, 0);
  229. snprintf(buf, sizeof(buf)-1, "%d", scale->h);
  230. av_opt_set(scale, "h", buf, 0);
  231. }
  232. if (!scale->w_expr)
  233. av_opt_set(scale, "w", "iw", 0);
  234. if (!scale->h_expr)
  235. av_opt_set(scale, "h", "ih", 0);
  236. ret = scale_parse_expr(ctx, NULL, &scale->w_pexpr, "width", scale->w_expr);
  237. if (ret < 0)
  238. return ret;
  239. ret = scale_parse_expr(ctx, NULL, &scale->h_pexpr, "height", scale->h_expr);
  240. if (ret < 0)
  241. return ret;
  242. av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
  243. scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
  244. scale->flags = 0;
  245. if (scale->flags_str) {
  246. const AVClass *class = sws_get_class();
  247. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  248. AV_OPT_SEARCH_FAKE_OBJ);
  249. int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
  250. if (ret < 0)
  251. return ret;
  252. }
  253. scale->opts = *opts;
  254. *opts = NULL;
  255. return 0;
  256. }
  257. static av_cold void uninit(AVFilterContext *ctx)
  258. {
  259. ScaleContext *scale = ctx->priv;
  260. av_expr_free(scale->w_pexpr);
  261. av_expr_free(scale->h_pexpr);
  262. scale->w_pexpr = scale->h_pexpr = NULL;
  263. sws_freeContext(scale->sws);
  264. sws_freeContext(scale->isws[0]);
  265. sws_freeContext(scale->isws[1]);
  266. scale->sws = NULL;
  267. av_dict_free(&scale->opts);
  268. }
  269. static int query_formats(AVFilterContext *ctx)
  270. {
  271. AVFilterFormats *formats;
  272. enum AVPixelFormat pix_fmt;
  273. int ret;
  274. if (ctx->inputs[0]) {
  275. const AVPixFmtDescriptor *desc = NULL;
  276. formats = NULL;
  277. while ((desc = av_pix_fmt_desc_next(desc))) {
  278. pix_fmt = av_pix_fmt_desc_get_id(desc);
  279. if ((sws_isSupportedInput(pix_fmt) ||
  280. sws_isSupportedEndiannessConversion(pix_fmt))
  281. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  282. return ret;
  283. }
  284. }
  285. if ((ret = ff_formats_ref(formats, &ctx->inputs[0]->out_formats)) < 0)
  286. return ret;
  287. }
  288. if (ctx->outputs[0]) {
  289. const AVPixFmtDescriptor *desc = NULL;
  290. formats = NULL;
  291. while ((desc = av_pix_fmt_desc_next(desc))) {
  292. pix_fmt = av_pix_fmt_desc_get_id(desc);
  293. if ((sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8 ||
  294. sws_isSupportedEndiannessConversion(pix_fmt))
  295. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  296. return ret;
  297. }
  298. }
  299. if ((ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
  300. return ret;
  301. }
  302. return 0;
  303. }
  304. static const int *parse_yuv_type(const char *s, enum AVColorSpace colorspace)
  305. {
  306. if (!s)
  307. s = "bt601";
  308. if (s && strstr(s, "bt709")) {
  309. colorspace = AVCOL_SPC_BT709;
  310. } else if (s && strstr(s, "fcc")) {
  311. colorspace = AVCOL_SPC_FCC;
  312. } else if (s && strstr(s, "smpte240m")) {
  313. colorspace = AVCOL_SPC_SMPTE240M;
  314. } else if (s && (strstr(s, "bt601") || strstr(s, "bt470") || strstr(s, "smpte170m"))) {
  315. colorspace = AVCOL_SPC_BT470BG;
  316. } else if (s && strstr(s, "bt2020")) {
  317. colorspace = AVCOL_SPC_BT2020_NCL;
  318. }
  319. if (colorspace < 1 || colorspace > 10 || colorspace == 8) {
  320. colorspace = AVCOL_SPC_BT470BG;
  321. }
  322. return sws_getCoefficients(colorspace);
  323. }
  324. static int scale_eval_dimensions(AVFilterContext *ctx)
  325. {
  326. ScaleContext *scale = ctx->priv;
  327. const char scale2ref = ctx->filter == &ff_vf_scale2ref;
  328. const AVFilterLink *inlink = scale2ref ? ctx->inputs[1] : ctx->inputs[0];
  329. const AVFilterLink *outlink = ctx->outputs[0];
  330. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  331. const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
  332. char *expr;
  333. int eval_w, eval_h;
  334. int ret;
  335. double res;
  336. const AVPixFmtDescriptor *main_desc;
  337. const AVFilterLink *main_link;
  338. if (scale2ref) {
  339. main_link = ctx->inputs[0];
  340. main_desc = av_pix_fmt_desc_get(main_link->format);
  341. }
  342. scale->var_values[VAR_IN_W] = scale->var_values[VAR_IW] = inlink->w;
  343. scale->var_values[VAR_IN_H] = scale->var_values[VAR_IH] = inlink->h;
  344. scale->var_values[VAR_OUT_W] = scale->var_values[VAR_OW] = NAN;
  345. scale->var_values[VAR_OUT_H] = scale->var_values[VAR_OH] = NAN;
  346. scale->var_values[VAR_A] = (double) inlink->w / inlink->h;
  347. scale->var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  348. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  349. scale->var_values[VAR_DAR] = scale->var_values[VAR_A] * scale->var_values[VAR_SAR];
  350. scale->var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  351. scale->var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  352. scale->var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
  353. scale->var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
  354. if (scale2ref) {
  355. scale->var_values[VAR_S2R_MAIN_W] = main_link->w;
  356. scale->var_values[VAR_S2R_MAIN_H] = main_link->h;
  357. scale->var_values[VAR_S2R_MAIN_A] = (double) main_link->w / main_link->h;
  358. scale->var_values[VAR_S2R_MAIN_SAR] = main_link->sample_aspect_ratio.num ?
  359. (double) main_link->sample_aspect_ratio.num / main_link->sample_aspect_ratio.den : 1;
  360. scale->var_values[VAR_S2R_MAIN_DAR] = scale->var_values[VAR_S2R_MDAR] =
  361. scale->var_values[VAR_S2R_MAIN_A] * scale->var_values[VAR_S2R_MAIN_SAR];
  362. scale->var_values[VAR_S2R_MAIN_HSUB] = 1 << main_desc->log2_chroma_w;
  363. scale->var_values[VAR_S2R_MAIN_VSUB] = 1 << main_desc->log2_chroma_h;
  364. }
  365. res = av_expr_eval(scale->w_pexpr, scale->var_values, NULL);
  366. eval_w = scale->var_values[VAR_OUT_W] = scale->var_values[VAR_OW] = (int) res == 0 ? inlink->w : (int) res;
  367. res = av_expr_eval(scale->h_pexpr, scale->var_values, NULL);
  368. if (isnan(res)) {
  369. expr = scale->h_expr;
  370. ret = AVERROR(EINVAL);
  371. goto fail;
  372. }
  373. eval_h = scale->var_values[VAR_OUT_H] = scale->var_values[VAR_OH] = (int) res == 0 ? inlink->h : (int) res;
  374. res = av_expr_eval(scale->w_pexpr, scale->var_values, NULL);
  375. if (isnan(res)) {
  376. expr = scale->w_expr;
  377. ret = AVERROR(EINVAL);
  378. goto fail;
  379. }
  380. eval_w = scale->var_values[VAR_OUT_W] = scale->var_values[VAR_OW] = (int) res == 0 ? inlink->w : (int) res;
  381. scale->w = eval_w;
  382. scale->h = eval_h;
  383. return 0;
  384. fail:
  385. av_log(ctx, AV_LOG_ERROR,
  386. "Error when evaluating the expression '%s'.\n", expr);
  387. return ret;
  388. }
  389. static int config_props(AVFilterLink *outlink)
  390. {
  391. AVFilterContext *ctx = outlink->src;
  392. AVFilterLink *inlink0 = outlink->src->inputs[0];
  393. AVFilterLink *inlink = ctx->filter == &ff_vf_scale2ref ?
  394. outlink->src->inputs[1] :
  395. outlink->src->inputs[0];
  396. enum AVPixelFormat outfmt = outlink->format;
  397. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  398. ScaleContext *scale = ctx->priv;
  399. int ret;
  400. if ((ret = scale_eval_dimensions(ctx)) < 0)
  401. goto fail;
  402. ff_scale_adjust_dimensions(inlink, &scale->w, &scale->h,
  403. scale->force_original_aspect_ratio,
  404. scale->force_divisible_by);
  405. if (scale->w > INT_MAX ||
  406. scale->h > INT_MAX ||
  407. (scale->h * inlink->w) > INT_MAX ||
  408. (scale->w * inlink->h) > INT_MAX)
  409. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  410. outlink->w = scale->w;
  411. outlink->h = scale->h;
  412. /* TODO: make algorithm configurable */
  413. scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL;
  414. if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
  415. scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PAL ||
  416. av_pix_fmt_desc_get(outfmt)->flags & FF_PSEUDOPAL;
  417. if (scale->sws)
  418. sws_freeContext(scale->sws);
  419. if (scale->isws[0])
  420. sws_freeContext(scale->isws[0]);
  421. if (scale->isws[1])
  422. sws_freeContext(scale->isws[1]);
  423. scale->isws[0] = scale->isws[1] = scale->sws = NULL;
  424. if (inlink0->w == outlink->w &&
  425. inlink0->h == outlink->h &&
  426. !scale->out_color_matrix &&
  427. scale->in_range == scale->out_range &&
  428. inlink0->format == outlink->format)
  429. ;
  430. else {
  431. struct SwsContext **swscs[3] = {&scale->sws, &scale->isws[0], &scale->isws[1]};
  432. int i;
  433. for (i = 0; i < 3; i++) {
  434. int in_v_chr_pos = scale->in_v_chr_pos, out_v_chr_pos = scale->out_v_chr_pos;
  435. struct SwsContext **s = swscs[i];
  436. *s = sws_alloc_context();
  437. if (!*s)
  438. return AVERROR(ENOMEM);
  439. av_opt_set_int(*s, "srcw", inlink0 ->w, 0);
  440. av_opt_set_int(*s, "srch", inlink0 ->h >> !!i, 0);
  441. av_opt_set_int(*s, "src_format", inlink0->format, 0);
  442. av_opt_set_int(*s, "dstw", outlink->w, 0);
  443. av_opt_set_int(*s, "dsth", outlink->h >> !!i, 0);
  444. av_opt_set_int(*s, "dst_format", outfmt, 0);
  445. av_opt_set_int(*s, "sws_flags", scale->flags, 0);
  446. av_opt_set_int(*s, "param0", scale->param[0], 0);
  447. av_opt_set_int(*s, "param1", scale->param[1], 0);
  448. if (scale->in_range != AVCOL_RANGE_UNSPECIFIED)
  449. av_opt_set_int(*s, "src_range",
  450. scale->in_range == AVCOL_RANGE_JPEG, 0);
  451. if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
  452. av_opt_set_int(*s, "dst_range",
  453. scale->out_range == AVCOL_RANGE_JPEG, 0);
  454. if (scale->opts) {
  455. AVDictionaryEntry *e = NULL;
  456. while ((e = av_dict_get(scale->opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
  457. if ((ret = av_opt_set(*s, e->key, e->value, 0)) < 0)
  458. return ret;
  459. }
  460. }
  461. /* Override YUV420P default settings to have the correct (MPEG-2) chroma positions
  462. * MPEG-2 chroma positions are used by convention
  463. * XXX: support other 4:2:0 pixel formats */
  464. if (inlink0->format == AV_PIX_FMT_YUV420P && scale->in_v_chr_pos == -513) {
  465. in_v_chr_pos = (i == 0) ? 128 : (i == 1) ? 64 : 192;
  466. }
  467. if (outlink->format == AV_PIX_FMT_YUV420P && scale->out_v_chr_pos == -513) {
  468. out_v_chr_pos = (i == 0) ? 128 : (i == 1) ? 64 : 192;
  469. }
  470. av_opt_set_int(*s, "src_h_chr_pos", scale->in_h_chr_pos, 0);
  471. av_opt_set_int(*s, "src_v_chr_pos", in_v_chr_pos, 0);
  472. av_opt_set_int(*s, "dst_h_chr_pos", scale->out_h_chr_pos, 0);
  473. av_opt_set_int(*s, "dst_v_chr_pos", out_v_chr_pos, 0);
  474. if ((ret = sws_init_context(*s, NULL, NULL)) < 0)
  475. return ret;
  476. if (!scale->interlaced)
  477. break;
  478. }
  479. }
  480. if (inlink0->sample_aspect_ratio.num){
  481. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink0->w, outlink->w * inlink0->h}, inlink0->sample_aspect_ratio);
  482. } else
  483. outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
  484. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d fmt:%s sar:%d/%d flags:0x%0x\n",
  485. inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
  486. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
  487. outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
  488. outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
  489. scale->flags);
  490. return 0;
  491. fail:
  492. return ret;
  493. }
  494. static int config_props_ref(AVFilterLink *outlink)
  495. {
  496. AVFilterLink *inlink = outlink->src->inputs[1];
  497. outlink->w = inlink->w;
  498. outlink->h = inlink->h;
  499. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  500. outlink->time_base = inlink->time_base;
  501. outlink->frame_rate = inlink->frame_rate;
  502. return 0;
  503. }
  504. static int request_frame(AVFilterLink *outlink)
  505. {
  506. return ff_request_frame(outlink->src->inputs[0]);
  507. }
  508. static int request_frame_ref(AVFilterLink *outlink)
  509. {
  510. return ff_request_frame(outlink->src->inputs[1]);
  511. }
  512. static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
  513. {
  514. ScaleContext *scale = link->dst->priv;
  515. const uint8_t *in[4];
  516. uint8_t *out[4];
  517. int in_stride[4],out_stride[4];
  518. int i;
  519. for (i=0; i<4; i++) {
  520. int vsub= ((i+1)&2) ? scale->vsub : 0;
  521. in_stride[i] = cur_pic->linesize[i] * mul;
  522. out_stride[i] = out_buf->linesize[i] * mul;
  523. in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
  524. out[i] = out_buf->data[i] + field * out_buf->linesize[i];
  525. }
  526. if (scale->input_is_pal)
  527. in[1] = cur_pic->data[1];
  528. if (scale->output_is_pal)
  529. out[1] = out_buf->data[1];
  530. return sws_scale(sws, in, in_stride, y/mul, h,
  531. out,out_stride);
  532. }
  533. static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out)
  534. {
  535. AVFilterContext *ctx = link->dst;
  536. ScaleContext *scale = ctx->priv;
  537. AVFilterLink *outlink = ctx->outputs[0];
  538. AVFrame *out;
  539. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  540. char buf[32];
  541. int in_range;
  542. int frame_changed;
  543. *frame_out = NULL;
  544. if (in->colorspace == AVCOL_SPC_YCGCO)
  545. av_log(link->dst, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
  546. frame_changed = in->width != link->w ||
  547. in->height != link->h ||
  548. in->format != link->format ||
  549. in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||
  550. in->sample_aspect_ratio.num != link->sample_aspect_ratio.num;
  551. if (frame_changed ||
  552. (scale->eval_mode == EVAL_MODE_FRAME &&
  553. ctx->filter == &ff_vf_scale2ref) ) {
  554. int ret;
  555. if (scale->eval_mode == EVAL_MODE_INIT) {
  556. snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
  557. av_opt_set(scale, "w", buf, 0);
  558. snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
  559. av_opt_set(scale, "h", buf, 0);
  560. ret = scale_parse_expr(ctx, NULL, &scale->w_pexpr, "width", scale->w_expr);
  561. if (ret < 0)
  562. return ret;
  563. ret = scale_parse_expr(ctx, NULL, &scale->h_pexpr, "height", scale->h_expr);
  564. if (ret < 0)
  565. return ret;
  566. }
  567. link->dst->inputs[0]->format = in->format;
  568. link->dst->inputs[0]->w = in->width;
  569. link->dst->inputs[0]->h = in->height;
  570. link->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
  571. link->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
  572. if ((ret = config_props(outlink)) < 0)
  573. return ret;
  574. }
  575. if (!scale->sws) {
  576. *frame_out = in;
  577. return 0;
  578. }
  579. scale->hsub = desc->log2_chroma_w;
  580. scale->vsub = desc->log2_chroma_h;
  581. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  582. if (!out) {
  583. av_frame_free(&in);
  584. return AVERROR(ENOMEM);
  585. }
  586. *frame_out = out;
  587. av_frame_copy_props(out, in);
  588. out->width = outlink->w;
  589. out->height = outlink->h;
  590. if (scale->output_is_pal)
  591. avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
  592. in_range = in->color_range;
  593. if ( scale->in_color_matrix
  594. || scale->out_color_matrix
  595. || scale-> in_range != AVCOL_RANGE_UNSPECIFIED
  596. || in_range != AVCOL_RANGE_UNSPECIFIED
  597. || scale->out_range != AVCOL_RANGE_UNSPECIFIED) {
  598. int in_full, out_full, brightness, contrast, saturation;
  599. const int *inv_table, *table;
  600. sws_getColorspaceDetails(scale->sws, (int **)&inv_table, &in_full,
  601. (int **)&table, &out_full,
  602. &brightness, &contrast, &saturation);
  603. if (scale->in_color_matrix)
  604. inv_table = parse_yuv_type(scale->in_color_matrix, in->colorspace);
  605. if (scale->out_color_matrix)
  606. table = parse_yuv_type(scale->out_color_matrix, AVCOL_SPC_UNSPECIFIED);
  607. else if (scale->in_color_matrix)
  608. table = inv_table;
  609. if (scale-> in_range != AVCOL_RANGE_UNSPECIFIED)
  610. in_full = (scale-> in_range == AVCOL_RANGE_JPEG);
  611. else if (in_range != AVCOL_RANGE_UNSPECIFIED)
  612. in_full = (in_range == AVCOL_RANGE_JPEG);
  613. if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
  614. out_full = (scale->out_range == AVCOL_RANGE_JPEG);
  615. sws_setColorspaceDetails(scale->sws, inv_table, in_full,
  616. table, out_full,
  617. brightness, contrast, saturation);
  618. if (scale->isws[0])
  619. sws_setColorspaceDetails(scale->isws[0], inv_table, in_full,
  620. table, out_full,
  621. brightness, contrast, saturation);
  622. if (scale->isws[1])
  623. sws_setColorspaceDetails(scale->isws[1], inv_table, in_full,
  624. table, out_full,
  625. brightness, contrast, saturation);
  626. out->color_range = out_full ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  627. }
  628. av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
  629. (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
  630. (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
  631. INT_MAX);
  632. if (scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)) {
  633. scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
  634. scale_slice(link, out, in, scale->isws[1], 0, link->h /2, 2, 1);
  635. } else if (scale->nb_slices) {
  636. int i, slice_h, slice_start, slice_end = 0;
  637. const int nb_slices = FFMIN(scale->nb_slices, link->h);
  638. for (i = 0; i < nb_slices; i++) {
  639. slice_start = slice_end;
  640. slice_end = (link->h * (i+1)) / nb_slices;
  641. slice_h = slice_end - slice_start;
  642. scale_slice(link, out, in, scale->sws, slice_start, slice_h, 1, 0);
  643. }
  644. } else {
  645. scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
  646. }
  647. av_frame_free(&in);
  648. return 0;
  649. }
  650. static int filter_frame(AVFilterLink *link, AVFrame *in)
  651. {
  652. AVFilterContext *ctx = link->dst;
  653. AVFilterLink *outlink = ctx->outputs[0];
  654. AVFrame *out;
  655. int ret;
  656. ret = scale_frame(link, in, &out);
  657. if (out)
  658. return ff_filter_frame(outlink, out);
  659. return ret;
  660. }
  661. static int filter_frame_ref(AVFilterLink *link, AVFrame *in)
  662. {
  663. AVFilterLink *outlink = link->dst->outputs[1];
  664. int frame_changed;
  665. frame_changed = in->width != link->w ||
  666. in->height != link->h ||
  667. in->format != link->format ||
  668. in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||
  669. in->sample_aspect_ratio.num != link->sample_aspect_ratio.num;
  670. if (frame_changed) {
  671. link->format = in->format;
  672. link->w = in->width;
  673. link->h = in->height;
  674. link->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
  675. link->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
  676. config_props_ref(outlink);
  677. }
  678. return ff_filter_frame(outlink, in);
  679. }
  680. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  681. char *res, int res_len, int flags)
  682. {
  683. ScaleContext *scale = ctx->priv;
  684. char *str_expr;
  685. AVExpr **pexpr_ptr;
  686. int ret, w, h;
  687. w = !strcmp(cmd, "width") || !strcmp(cmd, "w");
  688. h = !strcmp(cmd, "height") || !strcmp(cmd, "h");
  689. if (w || h) {
  690. str_expr = w ? scale->w_expr : scale->h_expr;
  691. pexpr_ptr = w ? &scale->w_pexpr : &scale->h_pexpr;
  692. ret = scale_parse_expr(ctx, str_expr, pexpr_ptr, cmd, args);
  693. } else
  694. ret = AVERROR(ENOSYS);
  695. if (ret < 0)
  696. av_log(ctx, AV_LOG_ERROR, "Failed to process command. Continuing with existing parameters.\n");
  697. return ret;
  698. }
  699. static const AVClass *child_class_next(const AVClass *prev)
  700. {
  701. return prev ? NULL : sws_get_class();
  702. }
  703. #define OFFSET(x) offsetof(ScaleContext, x)
  704. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  705. #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  706. static const AVOption scale_options[] = {
  707. { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = TFLAGS },
  708. { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = TFLAGS },
  709. { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = TFLAGS },
  710. { "height","Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = TFLAGS },
  711. { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
  712. { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_BOOL, {.i64 = 0 }, -1, 1, FLAGS },
  713. { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  714. { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  715. { "in_color_matrix", "set input YCbCr type", OFFSET(in_color_matrix), AV_OPT_TYPE_STRING, { .str = "auto" }, .flags = FLAGS, "color" },
  716. { "out_color_matrix", "set output YCbCr type", OFFSET(out_color_matrix), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS, "color"},
  717. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .str = "auto" }, 0, 0, FLAGS, "color" },
  718. { "bt601", NULL, 0, AV_OPT_TYPE_CONST, { .str = "bt601" }, 0, 0, FLAGS, "color" },
  719. { "bt470", NULL, 0, AV_OPT_TYPE_CONST, { .str = "bt470" }, 0, 0, FLAGS, "color" },
  720. { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, { .str = "smpte170m" }, 0, 0, FLAGS, "color" },
  721. { "bt709", NULL, 0, AV_OPT_TYPE_CONST, { .str = "bt709" }, 0, 0, FLAGS, "color" },
  722. { "fcc", NULL, 0, AV_OPT_TYPE_CONST, { .str = "fcc" }, 0, 0, FLAGS, "color" },
  723. { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, { .str = "smpte240m" }, 0, 0, FLAGS, "color" },
  724. { "bt2020", NULL, 0, AV_OPT_TYPE_CONST, { .str = "bt2020" }, 0, 0, FLAGS, "color" },
  725. { "in_range", "set input color range", OFFSET( in_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
  726. { "out_range", "set output color range", OFFSET(out_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
  727. { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 0, FLAGS, "range" },
  728. { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 0, FLAGS, "range" },
  729. { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  730. { "limited",NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  731. { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  732. { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  733. { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  734. { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  735. { "in_v_chr_pos", "input vertical chroma position in luma grid/256" , OFFSET(in_v_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },
  736. { "in_h_chr_pos", "input horizontal chroma position in luma grid/256", OFFSET(in_h_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },
  737. { "out_v_chr_pos", "output vertical chroma position in luma grid/256" , OFFSET(out_v_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },
  738. { "out_h_chr_pos", "output horizontal chroma position in luma grid/256", OFFSET(out_h_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },
  739. { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 2, FLAGS, "force_oar" },
  740. { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
  741. { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
  742. { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
  743. { "force_divisible_by", "enforce that the output resolution is divisible by a defined integer when force_original_aspect_ratio is used", OFFSET(force_divisible_by), AV_OPT_TYPE_INT, { .i64 = 1}, 1, 256, FLAGS },
  744. { "param0", "Scaler param 0", OFFSET(param[0]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
  745. { "param1", "Scaler param 1", OFFSET(param[1]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
  746. { "nb_slices", "set the number of slices (debug purpose only)", OFFSET(nb_slices), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  747. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  748. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  749. { "frame", "eval expressions during initialization and per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  750. { NULL }
  751. };
  752. static const AVClass scale_class = {
  753. .class_name = "scale",
  754. .item_name = av_default_item_name,
  755. .option = scale_options,
  756. .version = LIBAVUTIL_VERSION_INT,
  757. .category = AV_CLASS_CATEGORY_FILTER,
  758. .child_class_next = child_class_next,
  759. };
  760. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  761. {
  762. .name = "default",
  763. .type = AVMEDIA_TYPE_VIDEO,
  764. .filter_frame = filter_frame,
  765. },
  766. { NULL }
  767. };
  768. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  769. {
  770. .name = "default",
  771. .type = AVMEDIA_TYPE_VIDEO,
  772. .config_props = config_props,
  773. },
  774. { NULL }
  775. };
  776. AVFilter ff_vf_scale = {
  777. .name = "scale",
  778. .description = NULL_IF_CONFIG_SMALL("Scale the input video size and/or convert the image format."),
  779. .init_dict = init_dict,
  780. .uninit = uninit,
  781. .query_formats = query_formats,
  782. .priv_size = sizeof(ScaleContext),
  783. .priv_class = &scale_class,
  784. .inputs = avfilter_vf_scale_inputs,
  785. .outputs = avfilter_vf_scale_outputs,
  786. .process_command = process_command,
  787. };
  788. static const AVClass scale2ref_class = {
  789. .class_name = "scale2ref",
  790. .item_name = av_default_item_name,
  791. .option = scale_options,
  792. .version = LIBAVUTIL_VERSION_INT,
  793. .category = AV_CLASS_CATEGORY_FILTER,
  794. .child_class_next = child_class_next,
  795. };
  796. static const AVFilterPad avfilter_vf_scale2ref_inputs[] = {
  797. {
  798. .name = "default",
  799. .type = AVMEDIA_TYPE_VIDEO,
  800. .filter_frame = filter_frame,
  801. },
  802. {
  803. .name = "ref",
  804. .type = AVMEDIA_TYPE_VIDEO,
  805. .filter_frame = filter_frame_ref,
  806. },
  807. { NULL }
  808. };
  809. static const AVFilterPad avfilter_vf_scale2ref_outputs[] = {
  810. {
  811. .name = "default",
  812. .type = AVMEDIA_TYPE_VIDEO,
  813. .config_props = config_props,
  814. .request_frame= request_frame,
  815. },
  816. {
  817. .name = "ref",
  818. .type = AVMEDIA_TYPE_VIDEO,
  819. .config_props = config_props_ref,
  820. .request_frame= request_frame_ref,
  821. },
  822. { NULL }
  823. };
  824. AVFilter ff_vf_scale2ref = {
  825. .name = "scale2ref",
  826. .description = NULL_IF_CONFIG_SMALL("Scale the input video size and/or convert the image format to the given reference."),
  827. .init_dict = init_dict,
  828. .uninit = uninit,
  829. .query_formats = query_formats,
  830. .priv_size = sizeof(ScaleContext),
  831. .priv_class = &scale2ref_class,
  832. .inputs = avfilter_vf_scale2ref_inputs,
  833. .outputs = avfilter_vf_scale2ref_outputs,
  834. .process_command = process_command,
  835. };