vf_scale.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include "avfilter.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/pixdesc.h"
31 #include "libswscale/swscale.h"
32 
33 static const char *var_names[] = {
34  "PI",
35  "PHI",
36  "E",
37  "in_w", "iw",
38  "in_h", "ih",
39  "out_w", "ow",
40  "out_h", "oh",
41  "a", "dar",
42  "sar",
43  "hsub",
44  "vsub",
45  NULL
46 };
47 
48 enum var_name {
61 };
62 
63 typedef struct {
64  struct SwsContext *sws;
65 
71  int w, h;
72  unsigned int flags;
73 
74  int hsub, vsub;
75  int slice_y;
77 
78  char w_expr[256];
79  char h_expr[256];
80 } ScaleContext;
81 
82 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
83 {
84  ScaleContext *scale = ctx->priv;
85  const char *p;
86 
87  av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
88  av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
89 
90  scale->flags = SWS_BILINEAR;
91  if (args) {
92  sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
93  p = strstr(args,"flags=");
94  if (p) scale->flags = strtoul(p+6, NULL, 0);
95  }
96 
97  return 0;
98 }
99 
100 static av_cold void uninit(AVFilterContext *ctx)
101 {
102  ScaleContext *scale = ctx->priv;
103  sws_freeContext(scale->sws);
104  scale->sws = NULL;
105 }
106 
108 {
109  AVFilterFormats *formats;
110  enum PixelFormat pix_fmt;
111  int ret;
112 
113  if (ctx->inputs[0]) {
114  formats = NULL;
115  for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
116  if ( sws_isSupportedInput(pix_fmt)
117  && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
118  avfilter_formats_unref(&formats);
119  return ret;
120  }
121  avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
122  }
123  if (ctx->outputs[0]) {
124  formats = NULL;
125  for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
126  if ( sws_isSupportedOutput(pix_fmt)
127  && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
128  avfilter_formats_unref(&formats);
129  return ret;
130  }
131  avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
132  }
133 
134  return 0;
135 }
136 
137 static int config_props(AVFilterLink *outlink)
138 {
139  AVFilterContext *ctx = outlink->src;
140  AVFilterLink *inlink = outlink->src->inputs[0];
141  ScaleContext *scale = ctx->priv;
142  int64_t w, h;
143  double var_values[VARS_NB], res;
144  char *expr;
145  int ret;
146 
147  var_values[VAR_PI] = M_PI;
148  var_values[VAR_PHI] = M_PHI;
149  var_values[VAR_E] = M_E;
150  var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
151  var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
152  var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
153  var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
154  var_values[VAR_DAR] = var_values[VAR_A] = (double) inlink->w / inlink->h;
155  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
156  (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
157  var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
158  var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
159 
160  /* evaluate width and height */
161  av_expr_parse_and_eval(&res, (expr = scale->w_expr),
162  var_names, var_values,
163  NULL, NULL, NULL, NULL, NULL, 0, ctx);
164  scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
165  if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
166  var_names, var_values,
167  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
168  goto fail;
169  scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
170  /* evaluate again the width, as it may depend on the output height */
171  if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
172  var_names, var_values,
173  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
174  goto fail;
175  scale->w = res;
176 
177  w = scale->w;
178  h = scale->h;
179 
180  /* sanity check params */
181  if (w < -1 || h < -1) {
182  av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
183  return AVERROR(EINVAL);
184  }
185  if (w == -1 && h == -1)
186  scale->w = scale->h = 0;
187 
188  if (!(w = scale->w))
189  w = inlink->w;
190  if (!(h = scale->h))
191  h = inlink->h;
192  if (w == -1)
193  w = av_rescale(h, inlink->w, inlink->h);
194  if (h == -1)
195  h = av_rescale(w, inlink->h, inlink->w);
196 
197  if (w > INT_MAX || h > INT_MAX ||
198  (h * inlink->w) > INT_MAX ||
199  (w * inlink->h) > INT_MAX)
200  av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
201 
202  outlink->w = w;
203  outlink->h = h;
204 
205  /* TODO: make algorithm configurable */
206  av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
207  inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
208  outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
209  scale->flags);
210 
212 
213  if (scale->sws)
214  sws_freeContext(scale->sws);
215  scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
216  outlink->w, outlink->h, outlink->format,
217  scale->flags, NULL, NULL, NULL);
218  if (!scale->sws)
219  return AVERROR(EINVAL);
220 
221 
222  if (inlink->sample_aspect_ratio.num)
223  outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
224  outlink->w*inlink->h},
225  inlink->sample_aspect_ratio);
226  else
227  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
228 
229  return 0;
230 
231 fail:
233  "Error when evaluating the expression '%s'\n", expr);
234  return ret;
235 }
236 
237 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
238 {
239  ScaleContext *scale = link->dst->priv;
240  AVFilterLink *outlink = link->dst->outputs[0];
241  AVFilterBufferRef *outpicref;
242 
245 
246  outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
247  avfilter_copy_buffer_ref_props(outpicref, picref);
248  outpicref->video->w = outlink->w;
249  outpicref->video->h = outlink->h;
250 
251  outlink->out_buf = outpicref;
252 
253  av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
254  (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
255  (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
256  INT_MAX);
257 
258  scale->slice_y = 0;
259  avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
260 }
261 
262 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
263 {
264  ScaleContext *scale = link->dst->priv;
265  int out_h;
266  AVFilterBufferRef *cur_pic = link->cur_buf;
267  const uint8_t *data[4];
268 
269  if (scale->slice_y == 0 && slice_dir == -1)
270  scale->slice_y = link->dst->outputs[0]->h;
271 
272  data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
273  data[1] = scale->input_is_pal ?
274  cur_pic->data[1] :
275  cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
276  data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
277  data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
278 
279  out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
280  link->dst->outputs[0]->out_buf->data,
281  link->dst->outputs[0]->out_buf->linesize);
282 
283  if (slice_dir == -1)
284  scale->slice_y -= out_h;
285  avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
286  if (slice_dir == 1)
287  scale->slice_y += out_h;
288 }
289 
291  .name = "scale",
292  .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
293 
294  .init = init,
295  .uninit = uninit,
296 
297  .query_formats = query_formats,
298 
299  .priv_size = sizeof(ScaleContext),
300 
301  .inputs = (AVFilterPad[]) {{ .name = "default",
302  .type = AVMEDIA_TYPE_VIDEO,
303  .start_frame = start_frame,
304  .draw_slice = draw_slice,
305  .min_perms = AV_PERM_READ, },
306  { .name = NULL}},
307  .outputs = (AVFilterPad[]) {{ .name = "default",
308  .type = AVMEDIA_TYPE_VIDEO,
309  .config_props = config_props, },
310  { .name = NULL}},
311 };