FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_scale.c
Go to the documentation of this file.
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 /**
22  * @file
23  * scale video filter
24  */
25 
26 #include <stdio.h>
27 #include <string.h>
28 
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/eval.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/parseutils.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/avassert.h"
42 #include "libswscale/swscale.h"
43 
44 static const char *const var_names[] = {
45  "in_w", "iw",
46  "in_h", "ih",
47  "out_w", "ow",
48  "out_h", "oh",
49  "a",
50  "sar",
51  "dar",
52  "hsub",
53  "vsub",
54  NULL
55 };
56 
57 enum var_name {
68 };
69 
70 typedef struct {
71  const AVClass *class;
72  struct SwsContext *sws; ///< software scaler context
73  struct SwsContext *isws[2]; ///< software scaler context for interlaced material
74 
75  /**
76  * New dimensions. Special values are:
77  * 0 = original width/height
78  * -1 = keep original aspect
79  */
80  int w, h;
81  char *size_str;
82  unsigned int flags; ///sws flags
83 
84  int hsub, vsub; ///< chroma subsampling
85  int slice_y; ///< top of current output slice
86  int input_is_pal; ///< set to 1 if the input format is paletted
87  int output_is_pal; ///< set to 1 if the output format is paletted
89 
90  char *w_expr; ///< width expression string
91  char *h_expr; ///< height expression string
92  char *flags_str;
93 } ScaleContext;
94 
95 static av_cold int init(AVFilterContext *ctx)
96 {
97  ScaleContext *scale = ctx->priv;
98  int ret;
99 
100  if (scale->size_str && (scale->w_expr || scale->h_expr)) {
101  av_log(ctx, AV_LOG_ERROR,
102  "Size and width/height expressions cannot be set at the same time.\n");
103  return AVERROR(EINVAL);
104  }
105 
106  if (scale->w_expr && !scale->h_expr)
107  FFSWAP(char *, scale->w_expr, scale->size_str);
108 
109  if (scale->size_str) {
110  char buf[32];
111  if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
112  av_log(ctx, AV_LOG_ERROR,
113  "Invalid size '%s'\n", scale->size_str);
114  return ret;
115  }
116  snprintf(buf, sizeof(buf)-1, "%d", scale->w);
117  av_opt_set(scale, "w", buf, 0);
118  snprintf(buf, sizeof(buf)-1, "%d", scale->h);
119  av_opt_set(scale, "h", buf, 0);
120  }
121  if (!scale->w_expr)
122  av_opt_set(scale, "w", "iw", 0);
123  if (!scale->h_expr)
124  av_opt_set(scale, "h", "ih", 0);
125 
126  av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
127  scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
128 
129  scale->flags = SWS_BILINEAR;
130 
131  if (scale->flags_str) {
132  const AVClass *class = sws_get_class();
133  const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
135  int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
136  if (ret < 0)
137  return ret;
138  }
139 
140  return 0;
141 }
142 
143 static av_cold void uninit(AVFilterContext *ctx)
144 {
145  ScaleContext *scale = ctx->priv;
146  sws_freeContext(scale->sws);
147  sws_freeContext(scale->isws[0]);
148  sws_freeContext(scale->isws[1]);
149  scale->sws = NULL;
150  av_opt_free(scale);
151 }
152 
154 {
156  enum AVPixelFormat pix_fmt;
157  int ret;
158 
159  if (ctx->inputs[0]) {
160  formats = NULL;
161  for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
162  if ((sws_isSupportedInput(pix_fmt) ||
164  && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
165  ff_formats_unref(&formats);
166  return ret;
167  }
168  ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
169  }
170  if (ctx->outputs[0]) {
171  formats = NULL;
172  for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
173  if ((sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8 ||
175  && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
176  ff_formats_unref(&formats);
177  return ret;
178  }
179  ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
180  }
181 
182  return 0;
183 }
184 
185 static int config_props(AVFilterLink *outlink)
186 {
187  AVFilterContext *ctx = outlink->src;
188  AVFilterLink *inlink = outlink->src->inputs[0];
189  enum AVPixelFormat outfmt = outlink->format;
190  ScaleContext *scale = ctx->priv;
191  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
192  int64_t w, h;
193  double var_values[VARS_NB], res;
194  char *expr;
195  int ret;
196 
197  var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
198  var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
199  var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
200  var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
201  var_values[VAR_A] = (double) inlink->w / inlink->h;
202  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
203  (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
204  var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
205  var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
206  var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
207 
208  /* evaluate width and height */
209  av_expr_parse_and_eval(&res, (expr = scale->w_expr),
210  var_names, var_values,
211  NULL, NULL, NULL, NULL, NULL, 0, ctx);
212  scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
213  if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
214  var_names, var_values,
215  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
216  goto fail;
217  scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
218  /* evaluate again the width, as it may depend on the output height */
219  if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
220  var_names, var_values,
221  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
222  goto fail;
223  scale->w = res;
224 
225  w = scale->w;
226  h = scale->h;
227 
228  /* sanity check params */
229  if (w < -1 || h < -1) {
230  av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
231  return AVERROR(EINVAL);
232  }
233  if (w == -1 && h == -1)
234  scale->w = scale->h = 0;
235 
236  if (!(w = scale->w))
237  w = inlink->w;
238  if (!(h = scale->h))
239  h = inlink->h;
240  if (w == -1)
241  w = av_rescale(h, inlink->w, inlink->h);
242  if (h == -1)
243  h = av_rescale(w, inlink->h, inlink->w);
244 
245  if (w > INT_MAX || h > INT_MAX ||
246  (h * inlink->w) > INT_MAX ||
247  (w * inlink->h) > INT_MAX)
248  av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
249 
250  outlink->w = w;
251  outlink->h = h;
252 
253  /* TODO: make algorithm configurable */
254 
255  scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
257  if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
260 
261  if (scale->sws)
262  sws_freeContext(scale->sws);
263  if (inlink->w == outlink->w && inlink->h == outlink->h &&
264  inlink->format == outlink->format)
265  scale->sws = NULL;
266  else {
267  scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
268  outlink->w, outlink->h, outfmt,
269  scale->flags, NULL, NULL, NULL);
270  if (scale->isws[0])
271  sws_freeContext(scale->isws[0]);
272  scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
273  outlink->w, outlink->h/2, outfmt,
274  scale->flags, NULL, NULL, NULL);
275  if (scale->isws[1])
276  sws_freeContext(scale->isws[1]);
277  scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
278  outlink->w, outlink->h/2, outfmt,
279  scale->flags, NULL, NULL, NULL);
280  if (!scale->sws || !scale->isws[0] || !scale->isws[1])
281  return AVERROR(EINVAL);
282  }
283 
284  if (inlink->sample_aspect_ratio.num){
285  outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
286  } else
287  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
288 
289  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",
290  inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
292  outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
293  outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
294  scale->flags);
295  return 0;
296 
297 fail:
298  av_log(NULL, AV_LOG_ERROR,
299  "Error when evaluating the expression '%s'.\n"
300  "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
301  expr, scale->w_expr, scale->h_expr);
302  return ret;
303 }
304 
305 static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
306 {
307  ScaleContext *scale = link->dst->priv;
308  const uint8_t *in[4];
309  uint8_t *out[4];
310  int in_stride[4],out_stride[4];
311  int i;
312 
313  for(i=0; i<4; i++){
314  int vsub= ((i+1)&2) ? scale->vsub : 0;
315  in_stride[i] = cur_pic->linesize[i] * mul;
316  out_stride[i] = out_buf->linesize[i] * mul;
317  in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
318  out[i] = out_buf->data[i] + field * out_buf->linesize[i];
319  }
320  if(scale->input_is_pal)
321  in[1] = cur_pic->data[1];
322  if(scale->output_is_pal)
323  out[1] = out_buf->data[1];
324 
325  return sws_scale(sws, in, in_stride, y/mul, h,
326  out,out_stride);
327 }
328 
329 static int filter_frame(AVFilterLink *link, AVFrame *in)
330 {
331  ScaleContext *scale = link->dst->priv;
332  AVFilterLink *outlink = link->dst->outputs[0];
333  AVFrame *out;
334  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
335  char buf[32];
336 
337  if( in->width != link->w
338  || in->height != link->h
339  || in->format != link->format) {
340  int ret;
341  snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
342  av_opt_set(scale, "w", buf, 0);
343  snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
344  av_opt_set(scale, "h", buf, 0);
345 
346  link->dst->inputs[0]->format = in->format;
347  link->dst->inputs[0]->w = in->width;
348  link->dst->inputs[0]->h = in->height;
349 
350  if ((ret = config_props(outlink)) < 0)
351  return ret;
352  }
353 
354  if (!scale->sws)
355  return ff_filter_frame(outlink, in);
356 
357  scale->hsub = desc->log2_chroma_w;
358  scale->vsub = desc->log2_chroma_h;
359 
360  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
361  if (!out) {
362  av_frame_free(&in);
363  return AVERROR(ENOMEM);
364  }
365 
366  av_frame_copy_props(out, in);
367  out->width = outlink->w;
368  out->height = outlink->h;
369 
370  if(scale->output_is_pal)
371  avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
372 
374  (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
375  (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
376  INT_MAX);
377 
378  if(scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)){
379  scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
380  scale_slice(link, out, in, scale->isws[1], 0, link->h /2, 2, 1);
381  }else{
382  scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
383  }
384 
385  av_frame_free(&in);
386  return ff_filter_frame(outlink, out);
387 }
388 
389 #define OFFSET(x) offsetof(ScaleContext, x)
390 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
391 
392 static const AVOption scale_options[] = {
393  { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
394  { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
395  { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
396  { "height","Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
397  { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
398  { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
399  { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
400  { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
401  { NULL },
402 };
403 
405 
407  {
408  .name = "default",
409  .type = AVMEDIA_TYPE_VIDEO,
410  .filter_frame = filter_frame,
411  },
412  { NULL }
413 };
414 
416  {
417  .name = "default",
418  .type = AVMEDIA_TYPE_VIDEO,
419  .config_props = config_props,
420  },
421  { NULL }
422 };
423 
425  .name = "scale",
426  .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
427 
428  .init = init,
429  .uninit = uninit,
430 
431  .query_formats = query_formats,
432 
433  .priv_size = sizeof(ScaleContext),
434  .priv_class = &scale_class,
435 
436  .inputs = avfilter_vf_scale_inputs,
437  .outputs = avfilter_vf_scale_outputs,
438 };