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 };
AVFilterBufferRef * avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: avfilter.c:289
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:141
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:1431
int linesize[8]
number of bytes per line
Definition: avfilter.h:127
static void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
Copy properties of src to dst, without copying the actual data.
Definition: avfilter.h:148
Definition: vf_scale.c:51
enum PixelFormat pix_fmt
Definition: v4l.c:65
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst...
int num
numerator
Definition: rational.h:44
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:160
int hsub
sws flags
Definition: vf_scale.c:74
int vsub
chroma subsampling
Definition: vf_scale.c:74
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Definition: vf_scale.c:237
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:81
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:547
static int query_formats(AVFilterContext *ctx)
Definition: vf_scale.c:107
#define av_cold
Definition: attributes.h:71
char h_expr[256]
height expression string
Definition: vf_scale.c:79
const char data[16]
Definition: mxf.c:60
int avfilter_add_format(AVFilterFormats **avff, int fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:107
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
Definition: vf_scale.c:82
var_name
Definition: vf_boxblur.c:43
external api for the swscale stuff
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:109
const char * name
Definition: pixdesc.h:56
A filter pad used for either input or output.
Definition: avfilter.h:312
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:524
int h
image height
Definition: avfilter.h:108
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
char w_expr[256]
width expression string
Definition: vf_scale.c:78
#define AVERROR(e)
Definition: error.h:43
#define sws_isSupportedOutput(x)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
struct SwsContext * sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition: utils.c:1106
void * priv
private data for use by the filter
Definition: avfilter.h:553
#define PIX_FMT_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:87
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVFilter avfilter_vf_scale
Definition: vf_scale.c:290
const AVPixFmtDescriptor av_pix_fmt_descriptors[PIX_FMT_NB]
The array of all the pixel format descriptors.
Definition: pixdesc.c:119
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:64
int w
New dimensions.
Definition: vf_scale.c:71
static int config_props(AVFilterLink *outlink)
Definition: vf_scale.c:137
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:36
#define SWS_BILINEAR
Definition: swscale.h:76
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_scale.c:100
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:128
#define NAN
Definition: mathematics.h:54
#define M_E
Definition: ratecontrol.c:39
#define sws_isSupportedInput(x)
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Send a slice to the next filter.
Definition: avfilter.c:447
A reference to an AVFilterBuffer.
Definition: avfilter.h:124
NULL
Definition: eval.c:50
static const char * var_names[]
Definition: vf_scale.c:33
struct SwsContext * sws
software scaler context
Definition: vf_scale.c:64
uint8_t flags
Definition: pixdesc.h:76
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
void avfilter_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:139
Filter definition.
Definition: avfilter.h:497
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Add a new reference to a buffer.
Definition: avfilter.c:47
rational number numerator/denominator
Definition: rational.h:43
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Notify the next filter of the start of a frame.
Definition: avfilter.c:400
#define M_PHI
Definition: mathematics.h:42
const char * name
filter name
Definition: avfilter.h:498
int input_is_pal
set to 1 if the input format is paletted
Definition: vf_scale.c:76
unsigned int flags
Definition: vf_scale.c:72
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:551
static const uint16_t scale[4]
PixelFormat
Pixel format.
Definition: pixfmt.h:62
int den
denominator
Definition: rational.h:45
int slice_y
top of current output slice
Definition: vf_scale.c:75
Definition: vf_scale.c:56
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:82
void avfilter_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to...
Definition: formats.c:155
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:126
A list of supported formats for one end of a filter link.
Definition: avfilter.h:220
static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Definition: vf_scale.c:262
An instance of a filter.
Definition: avfilter.h:538
#define AV_LOG_INFO
Definition: log.h:119
#define M_PI
Definition: cos_tablegen.c:28
simple arithmetic expression evaluator