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
28KB

  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 "video.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/eval.h"
  32. #include "libavutil/internal.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "libavutil/imgutils.h"
  38. #include "libavutil/avassert.h"
  39. #include "libswscale/swscale.h"
  40. static const char *const var_names[] = {
  41. "in_w", "iw",
  42. "in_h", "ih",
  43. "out_w", "ow",
  44. "out_h", "oh",
  45. "a",
  46. "sar",
  47. "dar",
  48. "hsub",
  49. "vsub",
  50. "ohsub",
  51. "ovsub",
  52. NULL
  53. };
  54. enum var_name {
  55. VAR_IN_W, VAR_IW,
  56. VAR_IN_H, VAR_IH,
  57. VAR_OUT_W, VAR_OW,
  58. VAR_OUT_H, VAR_OH,
  59. VAR_A,
  60. VAR_SAR,
  61. VAR_DAR,
  62. VAR_HSUB,
  63. VAR_VSUB,
  64. VAR_OHSUB,
  65. VAR_OVSUB,
  66. VARS_NB
  67. };
  68. typedef struct ScaleContext {
  69. const AVClass *class;
  70. struct SwsContext *sws; ///< software scaler context
  71. struct SwsContext *isws[2]; ///< software scaler context for interlaced material
  72. AVDictionary *opts;
  73. /**
  74. * New dimensions. Special values are:
  75. * 0 = original width/height
  76. * -1 = keep original aspect
  77. * -N = try to keep aspect but make sure it is divisible by N
  78. */
  79. int w, h;
  80. char *size_str;
  81. unsigned int flags; ///sws flags
  82. double param[2]; // sws params
  83. int hsub, vsub; ///< chroma subsampling
  84. int slice_y; ///< top of current output slice
  85. int input_is_pal; ///< set to 1 if the input format is paletted
  86. int output_is_pal; ///< set to 1 if the output format is paletted
  87. int interlaced;
  88. char *w_expr; ///< width expression string
  89. char *h_expr; ///< height expression string
  90. char *flags_str;
  91. char *in_color_matrix;
  92. char *out_color_matrix;
  93. int in_range;
  94. int out_range;
  95. int out_h_chr_pos;
  96. int out_v_chr_pos;
  97. int in_h_chr_pos;
  98. int in_v_chr_pos;
  99. int force_original_aspect_ratio;
  100. int nb_slices;
  101. } ScaleContext;
  102. AVFilter ff_vf_scale2ref;
  103. static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
  104. {
  105. ScaleContext *scale = ctx->priv;
  106. int ret;
  107. if (scale->size_str && (scale->w_expr || scale->h_expr)) {
  108. av_log(ctx, AV_LOG_ERROR,
  109. "Size and width/height expressions cannot be set at the same time.\n");
  110. return AVERROR(EINVAL);
  111. }
  112. if (scale->w_expr && !scale->h_expr)
  113. FFSWAP(char *, scale->w_expr, scale->size_str);
  114. if (scale->size_str) {
  115. char buf[32];
  116. if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
  117. av_log(ctx, AV_LOG_ERROR,
  118. "Invalid size '%s'\n", scale->size_str);
  119. return ret;
  120. }
  121. snprintf(buf, sizeof(buf)-1, "%d", scale->w);
  122. av_opt_set(scale, "w", buf, 0);
  123. snprintf(buf, sizeof(buf)-1, "%d", scale->h);
  124. av_opt_set(scale, "h", buf, 0);
  125. }
  126. if (!scale->w_expr)
  127. av_opt_set(scale, "w", "iw", 0);
  128. if (!scale->h_expr)
  129. av_opt_set(scale, "h", "ih", 0);
  130. av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
  131. scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
  132. scale->flags = 0;
  133. if (scale->flags_str) {
  134. const AVClass *class = sws_get_class();
  135. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  136. AV_OPT_SEARCH_FAKE_OBJ);
  137. int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
  138. if (ret < 0)
  139. return ret;
  140. }
  141. scale->opts = *opts;
  142. *opts = NULL;
  143. return 0;
  144. }
  145. static av_cold void uninit(AVFilterContext *ctx)
  146. {
  147. ScaleContext *scale = ctx->priv;
  148. sws_freeContext(scale->sws);
  149. sws_freeContext(scale->isws[0]);
  150. sws_freeContext(scale->isws[1]);
  151. scale->sws = NULL;
  152. av_dict_free(&scale->opts);
  153. }
  154. static int query_formats(AVFilterContext *ctx)
  155. {
  156. AVFilterFormats *formats;
  157. enum AVPixelFormat pix_fmt;
  158. int ret;
  159. if (ctx->inputs[0]) {
  160. const AVPixFmtDescriptor *desc = NULL;
  161. formats = NULL;
  162. while ((desc = av_pix_fmt_desc_next(desc))) {
  163. pix_fmt = av_pix_fmt_desc_get_id(desc);
  164. if ((sws_isSupportedInput(pix_fmt) ||
  165. sws_isSupportedEndiannessConversion(pix_fmt))
  166. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  167. return ret;
  168. }
  169. }
  170. if ((ret = ff_formats_ref(formats, &ctx->inputs[0]->out_formats)) < 0)
  171. return ret;
  172. }
  173. if (ctx->outputs[0]) {
  174. const AVPixFmtDescriptor *desc = NULL;
  175. formats = NULL;
  176. while ((desc = av_pix_fmt_desc_next(desc))) {
  177. pix_fmt = av_pix_fmt_desc_get_id(desc);
  178. if ((sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8 ||
  179. sws_isSupportedEndiannessConversion(pix_fmt))
  180. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  181. return ret;
  182. }
  183. }
  184. if ((ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static const int *parse_yuv_type(const char *s, enum AVColorSpace colorspace)
  190. {
  191. if (!s)
  192. s = "bt601";
  193. if (s && strstr(s, "bt709")) {
  194. colorspace = AVCOL_SPC_BT709;
  195. } else if (s && strstr(s, "fcc")) {
  196. colorspace = AVCOL_SPC_FCC;
  197. } else if (s && strstr(s, "smpte240m")) {
  198. colorspace = AVCOL_SPC_SMPTE240M;
  199. } else if (s && (strstr(s, "bt601") || strstr(s, "bt470") || strstr(s, "smpte170m"))) {
  200. colorspace = AVCOL_SPC_BT470BG;
  201. }
  202. if (colorspace < 1 || colorspace > 7) {
  203. colorspace = AVCOL_SPC_BT470BG;
  204. }
  205. return sws_getCoefficients(colorspace);
  206. }
  207. static int config_props(AVFilterLink *outlink)
  208. {
  209. AVFilterContext *ctx = outlink->src;
  210. AVFilterLink *inlink0 = outlink->src->inputs[0];
  211. AVFilterLink *inlink = ctx->filter == &ff_vf_scale2ref ?
  212. outlink->src->inputs[1] :
  213. outlink->src->inputs[0];
  214. enum AVPixelFormat outfmt = outlink->format;
  215. ScaleContext *scale = ctx->priv;
  216. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  217. const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
  218. int64_t w, h;
  219. double var_values[VARS_NB], res;
  220. char *expr;
  221. int ret;
  222. int factor_w, factor_h;
  223. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  224. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  225. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  226. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  227. var_values[VAR_A] = (double) inlink->w / inlink->h;
  228. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  229. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  230. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  231. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  232. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  233. var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
  234. var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
  235. /* evaluate width and height */
  236. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  237. var_names, var_values,
  238. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  239. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  240. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  241. var_names, var_values,
  242. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  243. goto fail;
  244. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  245. /* evaluate again the width, as it may depend on the output height */
  246. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  247. var_names, var_values,
  248. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  249. goto fail;
  250. scale->w = res;
  251. w = scale->w;
  252. h = scale->h;
  253. /* Check if it is requested that the result has to be divisible by a some
  254. * factor (w or h = -n with n being the factor). */
  255. factor_w = 1;
  256. factor_h = 1;
  257. if (w < -1) {
  258. factor_w = -w;
  259. }
  260. if (h < -1) {
  261. factor_h = -h;
  262. }
  263. if (w < 0 && h < 0)
  264. scale->w = scale->h = 0;
  265. if (!(w = scale->w))
  266. w = inlink->w;
  267. if (!(h = scale->h))
  268. h = inlink->h;
  269. /* Make sure that the result is divisible by the factor we determined
  270. * earlier. If no factor was set, it is nothing will happen as the default
  271. * factor is 1 */
  272. if (w < 0)
  273. w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
  274. if (h < 0)
  275. h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
  276. /* Note that force_original_aspect_ratio may overwrite the previous set
  277. * dimensions so that it is not divisible by the set factors anymore. */
  278. if (scale->force_original_aspect_ratio) {
  279. int tmp_w = av_rescale(h, inlink->w, inlink->h);
  280. int tmp_h = av_rescale(w, inlink->h, inlink->w);
  281. if (scale->force_original_aspect_ratio == 1) {
  282. w = FFMIN(tmp_w, w);
  283. h = FFMIN(tmp_h, h);
  284. } else {
  285. w = FFMAX(tmp_w, w);
  286. h = FFMAX(tmp_h, h);
  287. }
  288. }
  289. if (w > INT_MAX || h > INT_MAX ||
  290. (h * inlink->w) > INT_MAX ||
  291. (w * inlink->h) > INT_MAX)
  292. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  293. outlink->w = w;
  294. outlink->h = h;
  295. /* TODO: make algorithm configurable */
  296. scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
  297. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  298. if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
  299. scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PAL ||
  300. av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  301. if (scale->sws)
  302. sws_freeContext(scale->sws);
  303. if (scale->isws[0])
  304. sws_freeContext(scale->isws[0]);
  305. if (scale->isws[1])
  306. sws_freeContext(scale->isws[1]);
  307. scale->isws[0] = scale->isws[1] = scale->sws = NULL;
  308. if (inlink0->w == outlink->w &&
  309. inlink0->h == outlink->h &&
  310. !scale->out_color_matrix &&
  311. scale->in_range == scale->out_range &&
  312. inlink0->format == outlink->format)
  313. ;
  314. else {
  315. struct SwsContext **swscs[3] = {&scale->sws, &scale->isws[0], &scale->isws[1]};
  316. int i;
  317. for (i = 0; i < 3; i++) {
  318. struct SwsContext **s = swscs[i];
  319. *s = sws_alloc_context();
  320. if (!*s)
  321. return AVERROR(ENOMEM);
  322. av_opt_set_int(*s, "srcw", inlink0 ->w, 0);
  323. av_opt_set_int(*s, "srch", inlink0 ->h >> !!i, 0);
  324. av_opt_set_int(*s, "src_format", inlink0->format, 0);
  325. av_opt_set_int(*s, "dstw", outlink->w, 0);
  326. av_opt_set_int(*s, "dsth", outlink->h >> !!i, 0);
  327. av_opt_set_int(*s, "dst_format", outfmt, 0);
  328. av_opt_set_int(*s, "sws_flags", scale->flags, 0);
  329. av_opt_set_int(*s, "param0", scale->param[0], 0);
  330. av_opt_set_int(*s, "param1", scale->param[1], 0);
  331. if (scale->in_range != AVCOL_RANGE_UNSPECIFIED)
  332. av_opt_set_int(*s, "src_range",
  333. scale->in_range == AVCOL_RANGE_JPEG, 0);
  334. if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
  335. av_opt_set_int(*s, "dst_range",
  336. scale->out_range == AVCOL_RANGE_JPEG, 0);
  337. if (scale->opts) {
  338. AVDictionaryEntry *e = NULL;
  339. while ((e = av_dict_get(scale->opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
  340. if ((ret = av_opt_set(*s, e->key, e->value, 0)) < 0)
  341. return ret;
  342. }
  343. }
  344. /* Override YUV420P default settings to have the correct (MPEG-2) chroma positions
  345. * MPEG-2 chroma positions are used by convention
  346. * XXX: support other 4:2:0 pixel formats */
  347. if (inlink0->format == AV_PIX_FMT_YUV420P && scale->in_v_chr_pos == -513) {
  348. scale->in_v_chr_pos = (i == 0) ? 128 : (i == 1) ? 64 : 192;
  349. }
  350. if (outlink->format == AV_PIX_FMT_YUV420P && scale->out_v_chr_pos == -513) {
  351. scale->out_v_chr_pos = (i == 0) ? 128 : (i == 1) ? 64 : 192;
  352. }
  353. av_opt_set_int(*s, "src_h_chr_pos", scale->in_h_chr_pos, 0);
  354. av_opt_set_int(*s, "src_v_chr_pos", scale->in_v_chr_pos, 0);
  355. av_opt_set_int(*s, "dst_h_chr_pos", scale->out_h_chr_pos, 0);
  356. av_opt_set_int(*s, "dst_v_chr_pos", scale->out_v_chr_pos, 0);
  357. if ((ret = sws_init_context(*s, NULL, NULL)) < 0)
  358. return ret;
  359. if (!scale->interlaced)
  360. break;
  361. }
  362. }
  363. if (inlink->sample_aspect_ratio.num){
  364. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
  365. } else
  366. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  367. 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",
  368. inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
  369. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
  370. outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
  371. outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
  372. scale->flags);
  373. return 0;
  374. fail:
  375. av_log(NULL, AV_LOG_ERROR,
  376. "Error when evaluating the expression '%s'.\n"
  377. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  378. expr, scale->w_expr, scale->h_expr);
  379. return ret;
  380. }
  381. static int config_props_ref(AVFilterLink *outlink)
  382. {
  383. AVFilterLink *inlink = outlink->src->inputs[1];
  384. outlink->w = inlink->w;
  385. outlink->h = inlink->h;
  386. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  387. outlink->time_base = inlink->time_base;
  388. return 0;
  389. }
  390. static int request_frame(AVFilterLink *outlink)
  391. {
  392. return ff_request_frame(outlink->src->inputs[0]);
  393. }
  394. static int request_frame_ref(AVFilterLink *outlink)
  395. {
  396. return ff_request_frame(outlink->src->inputs[1]);
  397. }
  398. static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
  399. {
  400. ScaleContext *scale = link->dst->priv;
  401. const uint8_t *in[4];
  402. uint8_t *out[4];
  403. int in_stride[4],out_stride[4];
  404. int i;
  405. for(i=0; i<4; i++){
  406. int vsub= ((i+1)&2) ? scale->vsub : 0;
  407. in_stride[i] = cur_pic->linesize[i] * mul;
  408. out_stride[i] = out_buf->linesize[i] * mul;
  409. in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
  410. out[i] = out_buf->data[i] + field * out_buf->linesize[i];
  411. }
  412. if(scale->input_is_pal)
  413. in[1] = cur_pic->data[1];
  414. if(scale->output_is_pal)
  415. out[1] = out_buf->data[1];
  416. return sws_scale(sws, in, in_stride, y/mul, h,
  417. out,out_stride);
  418. }
  419. static int filter_frame(AVFilterLink *link, AVFrame *in)
  420. {
  421. ScaleContext *scale = link->dst->priv;
  422. AVFilterLink *outlink = link->dst->outputs[0];
  423. AVFrame *out;
  424. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  425. char buf[32];
  426. int in_range;
  427. if (av_frame_get_colorspace(in) == AVCOL_SPC_YCGCO)
  428. av_log(link->dst, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
  429. if( in->width != link->w
  430. || in->height != link->h
  431. || in->format != link->format) {
  432. int ret;
  433. snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
  434. av_opt_set(scale, "w", buf, 0);
  435. snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
  436. av_opt_set(scale, "h", buf, 0);
  437. link->dst->inputs[0]->format = in->format;
  438. link->dst->inputs[0]->w = in->width;
  439. link->dst->inputs[0]->h = in->height;
  440. if ((ret = config_props(outlink)) < 0)
  441. return ret;
  442. }
  443. if (!scale->sws)
  444. return ff_filter_frame(outlink, in);
  445. scale->hsub = desc->log2_chroma_w;
  446. scale->vsub = desc->log2_chroma_h;
  447. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  448. if (!out) {
  449. av_frame_free(&in);
  450. return AVERROR(ENOMEM);
  451. }
  452. av_frame_copy_props(out, in);
  453. out->width = outlink->w;
  454. out->height = outlink->h;
  455. if(scale->output_is_pal)
  456. avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
  457. in_range = av_frame_get_color_range(in);
  458. if ( scale->in_color_matrix
  459. || scale->out_color_matrix
  460. || scale-> in_range != AVCOL_RANGE_UNSPECIFIED
  461. || in_range != AVCOL_RANGE_UNSPECIFIED
  462. || scale->out_range != AVCOL_RANGE_UNSPECIFIED) {
  463. int in_full, out_full, brightness, contrast, saturation;
  464. const int *inv_table, *table;
  465. sws_getColorspaceDetails(scale->sws, (int **)&inv_table, &in_full,
  466. (int **)&table, &out_full,
  467. &brightness, &contrast, &saturation);
  468. if (scale->in_color_matrix)
  469. inv_table = parse_yuv_type(scale->in_color_matrix, av_frame_get_colorspace(in));
  470. if (scale->out_color_matrix)
  471. table = parse_yuv_type(scale->out_color_matrix, AVCOL_SPC_UNSPECIFIED);
  472. else if (scale->in_color_matrix)
  473. table = inv_table;
  474. if (scale-> in_range != AVCOL_RANGE_UNSPECIFIED)
  475. in_full = (scale-> in_range == AVCOL_RANGE_JPEG);
  476. else if (in_range != AVCOL_RANGE_UNSPECIFIED)
  477. in_full = (in_range == AVCOL_RANGE_JPEG);
  478. if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
  479. out_full = (scale->out_range == AVCOL_RANGE_JPEG);
  480. sws_setColorspaceDetails(scale->sws, inv_table, in_full,
  481. table, out_full,
  482. brightness, contrast, saturation);
  483. if (scale->isws[0])
  484. sws_setColorspaceDetails(scale->isws[0], inv_table, in_full,
  485. table, out_full,
  486. brightness, contrast, saturation);
  487. if (scale->isws[1])
  488. sws_setColorspaceDetails(scale->isws[1], inv_table, in_full,
  489. table, out_full,
  490. brightness, contrast, saturation);
  491. av_frame_set_color_range(out, out_full ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG);
  492. }
  493. av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
  494. (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
  495. (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
  496. INT_MAX);
  497. if(scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)){
  498. scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
  499. scale_slice(link, out, in, scale->isws[1], 0, link->h /2, 2, 1);
  500. }else if (scale->nb_slices) {
  501. int i, slice_h, slice_start, slice_end = 0;
  502. const int nb_slices = FFMIN(scale->nb_slices, link->h);
  503. for (i = 0; i < nb_slices; i++) {
  504. slice_start = slice_end;
  505. slice_end = (link->h * (i+1)) / nb_slices;
  506. slice_h = slice_end - slice_start;
  507. scale_slice(link, out, in, scale->sws, slice_start, slice_h, 1, 0);
  508. }
  509. }else{
  510. scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
  511. }
  512. av_frame_free(&in);
  513. return ff_filter_frame(outlink, out);
  514. }
  515. static int filter_frame_ref(AVFilterLink *link, AVFrame *in)
  516. {
  517. AVFilterLink *outlink = link->dst->outputs[1];
  518. return ff_filter_frame(outlink, in);
  519. }
  520. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  521. char *res, int res_len, int flags)
  522. {
  523. ScaleContext *scale = ctx->priv;
  524. int ret;
  525. if ( !strcmp(cmd, "width") || !strcmp(cmd, "w")
  526. || !strcmp(cmd, "height") || !strcmp(cmd, "h")) {
  527. int old_w = scale->w;
  528. int old_h = scale->h;
  529. AVFilterLink *outlink = ctx->outputs[0];
  530. av_opt_set(scale, cmd, args, 0);
  531. if ((ret = config_props(outlink)) < 0) {
  532. scale->w = old_w;
  533. scale->h = old_h;
  534. }
  535. } else
  536. ret = AVERROR(ENOSYS);
  537. return ret;
  538. }
  539. static const AVClass *child_class_next(const AVClass *prev)
  540. {
  541. return prev ? NULL : sws_get_class();
  542. }
  543. #define OFFSET(x) offsetof(ScaleContext, x)
  544. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  545. static const AVOption scale_options[] = {
  546. { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  547. { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  548. { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  549. { "height","Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  550. { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
  551. { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_BOOL, {.i64 = 0 }, -1, 1, FLAGS },
  552. { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  553. { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  554. { "in_color_matrix", "set input YCbCr type", OFFSET(in_color_matrix), AV_OPT_TYPE_STRING, { .str = "auto" }, .flags = FLAGS },
  555. { "out_color_matrix", "set output YCbCr type", OFFSET(out_color_matrix), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
  556. { "in_range", "set input color range", OFFSET( in_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
  557. { "out_range", "set output color range", OFFSET(out_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
  558. { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 0, FLAGS, "range" },
  559. { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  560. { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  561. { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  562. { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  563. { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  564. { "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 },
  565. { "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 },
  566. { "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 },
  567. { "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 },
  568. { "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" },
  569. { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
  570. { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
  571. { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
  572. { "param0", "Scaler param 0", OFFSET(param[0]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
  573. { "param1", "Scaler param 1", OFFSET(param[1]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
  574. { "nb_slices", "set the number of slices (debug purpose only)", OFFSET(nb_slices), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  575. { NULL }
  576. };
  577. static const AVClass scale_class = {
  578. .class_name = "scale",
  579. .item_name = av_default_item_name,
  580. .option = scale_options,
  581. .version = LIBAVUTIL_VERSION_INT,
  582. .category = AV_CLASS_CATEGORY_FILTER,
  583. .child_class_next = child_class_next,
  584. };
  585. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  586. {
  587. .name = "default",
  588. .type = AVMEDIA_TYPE_VIDEO,
  589. .filter_frame = filter_frame,
  590. },
  591. { NULL }
  592. };
  593. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  594. {
  595. .name = "default",
  596. .type = AVMEDIA_TYPE_VIDEO,
  597. .config_props = config_props,
  598. },
  599. { NULL }
  600. };
  601. AVFilter ff_vf_scale = {
  602. .name = "scale",
  603. .description = NULL_IF_CONFIG_SMALL("Scale the input video size and/or convert the image format."),
  604. .init_dict = init_dict,
  605. .uninit = uninit,
  606. .query_formats = query_formats,
  607. .priv_size = sizeof(ScaleContext),
  608. .priv_class = &scale_class,
  609. .inputs = avfilter_vf_scale_inputs,
  610. .outputs = avfilter_vf_scale_outputs,
  611. .process_command = process_command,
  612. };
  613. static const AVClass scale2ref_class = {
  614. .class_name = "scale2ref",
  615. .item_name = av_default_item_name,
  616. .option = scale_options,
  617. .version = LIBAVUTIL_VERSION_INT,
  618. .category = AV_CLASS_CATEGORY_FILTER,
  619. .child_class_next = child_class_next,
  620. };
  621. static const AVFilterPad avfilter_vf_scale2ref_inputs[] = {
  622. {
  623. .name = "default",
  624. .type = AVMEDIA_TYPE_VIDEO,
  625. .filter_frame = filter_frame,
  626. },
  627. {
  628. .name = "ref",
  629. .type = AVMEDIA_TYPE_VIDEO,
  630. .filter_frame = filter_frame_ref,
  631. },
  632. { NULL }
  633. };
  634. static const AVFilterPad avfilter_vf_scale2ref_outputs[] = {
  635. {
  636. .name = "default",
  637. .type = AVMEDIA_TYPE_VIDEO,
  638. .config_props = config_props,
  639. .request_frame= request_frame,
  640. },
  641. {
  642. .name = "ref",
  643. .type = AVMEDIA_TYPE_VIDEO,
  644. .config_props = config_props_ref,
  645. .request_frame= request_frame_ref,
  646. },
  647. { NULL }
  648. };
  649. AVFilter ff_vf_scale2ref = {
  650. .name = "scale2ref",
  651. .description = NULL_IF_CONFIG_SMALL("Scale the input video size and/or convert the image format to the given reference."),
  652. .init_dict = init_dict,
  653. .uninit = uninit,
  654. .query_formats = query_formats,
  655. .priv_size = sizeof(ScaleContext),
  656. .priv_class = &scale2ref_class,
  657. .inputs = avfilter_vf_scale2ref_inputs,
  658. .outputs = avfilter_vf_scale2ref_outputs,
  659. .process_command = process_command,
  660. };