vf_crop.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 /* #define DEBUG */
27 
28 #include <stdio.h>
29 
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34 #include "libavutil/eval.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/libm.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/mathematics.h"
40 
41 static const char *const var_names[] = {
42  "E",
43  "PHI",
44  "PI",
45  "in_w", "iw",
46  "in_h", "ih",
47  "out_w", "ow",
48  "out_h", "oh",
49  "x",
50  "y",
51  "n",
52  "pos",
53  "t",
54  NULL
55 };
56 
57 enum var_name {
71 };
72 
73 typedef struct {
74  int x;
75  int y;
76  int w;
77  int h;
78 
79  int max_step[4];
80  int hsub, vsub;
81  char x_expr[256], y_expr[256], ow_expr[256], oh_expr[256];
82  AVExpr *x_pexpr, *y_pexpr; /* parsed expressions for x and y */
83  double var_values[VAR_VARS_NB];
84 } CropContext;
85 
87 {
88  static const enum AVPixelFormat pix_fmts[] = {
112  };
113 
115 
116  return 0;
117 }
118 
119 static av_cold int init(AVFilterContext *ctx, const char *args)
120 {
121  CropContext *crop = ctx->priv;
122 
123  av_strlcpy(crop->ow_expr, "iw", sizeof(crop->ow_expr));
124  av_strlcpy(crop->oh_expr, "ih", sizeof(crop->oh_expr));
125  av_strlcpy(crop->x_expr, "(in_w-out_w)/2", sizeof(crop->x_expr));
126  av_strlcpy(crop->y_expr, "(in_h-out_h)/2", sizeof(crop->y_expr));
127 
128  if (args)
129  sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]", crop->ow_expr, crop->oh_expr, crop->x_expr, crop->y_expr);
130 
131  return 0;
132 }
133 
134 static av_cold void uninit(AVFilterContext *ctx)
135 {
136  CropContext *crop = ctx->priv;
137 
138  av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
139  av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
140 }
141 
142 static inline int normalize_double(int *n, double d)
143 {
144  int ret = 0;
145 
146  if (isnan(d)) {
147  ret = AVERROR(EINVAL);
148  } else if (d > INT_MAX || d < INT_MIN) {
149  *n = d > INT_MAX ? INT_MAX : INT_MIN;
150  ret = AVERROR(EINVAL);
151  } else
152  *n = round(d);
153 
154  return ret;
155 }
156 
157 static int config_input(AVFilterLink *link)
158 {
159  AVFilterContext *ctx = link->dst;
160  CropContext *crop = ctx->priv;
161  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(link->format);
162  int ret;
163  const char *expr;
164  double res;
165 
166  crop->var_values[VAR_E] = M_E;
167  crop->var_values[VAR_PHI] = M_PHI;
168  crop->var_values[VAR_PI] = M_PI;
169  crop->var_values[VAR_IN_W] = crop->var_values[VAR_IW] = ctx->inputs[0]->w;
170  crop->var_values[VAR_IN_H] = crop->var_values[VAR_IH] = ctx->inputs[0]->h;
171  crop->var_values[VAR_X] = NAN;
172  crop->var_values[VAR_Y] = NAN;
173  crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = NAN;
174  crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = NAN;
175  crop->var_values[VAR_N] = 0;
176  crop->var_values[VAR_T] = NAN;
177  crop->var_values[VAR_POS] = NAN;
178 
179  av_image_fill_max_pixsteps(crop->max_step, NULL, pix_desc);
180  crop->hsub = pix_desc->log2_chroma_w;
181  crop->vsub = pix_desc->log2_chroma_h;
182 
183  if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
184  var_names, crop->var_values,
185  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
186  crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
187  if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_expr),
188  var_names, crop->var_values,
189  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
190  crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = res;
191  /* evaluate again ow as it may depend on oh */
192  if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
193  var_names, crop->var_values,
194  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
195  crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
196  if (normalize_double(&crop->w, crop->var_values[VAR_OUT_W]) < 0 ||
197  normalize_double(&crop->h, crop->var_values[VAR_OUT_H]) < 0) {
198  av_log(ctx, AV_LOG_ERROR,
199  "Too big value or invalid expression for out_w/ow or out_h/oh. "
200  "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
201  crop->ow_expr, crop->oh_expr);
202  return AVERROR(EINVAL);
203  }
204  crop->w &= ~((1 << crop->hsub) - 1);
205  crop->h &= ~((1 << crop->vsub) - 1);
206 
207  if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,
208  NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
209  (ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,
210  NULL, NULL, NULL, NULL, 0, ctx)) < 0)
211  return AVERROR(EINVAL);
212 
213  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
214  link->w, link->h, crop->w, crop->h);
215 
216  if (crop->w <= 0 || crop->h <= 0 ||
217  crop->w > link->w || crop->h > link->h) {
218  av_log(ctx, AV_LOG_ERROR,
219  "Invalid too big or non positive size for width '%d' or height '%d'\n",
220  crop->w, crop->h);
221  return AVERROR(EINVAL);
222  }
223 
224  /* set default, required in the case the first computed value for x/y is NAN */
225  crop->x = (link->w - crop->w) / 2;
226  crop->y = (link->h - crop->h) / 2;
227  crop->x &= ~((1 << crop->hsub) - 1);
228  crop->y &= ~((1 << crop->vsub) - 1);
229  return 0;
230 
231 fail_expr:
232  av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
233  return ret;
234 }
235 
236 static int config_output(AVFilterLink *link)
237 {
238  CropContext *crop = link->src->priv;
239 
240  link->w = crop->w;
241  link->h = crop->h;
242 
243  return 0;
244 }
245 
246 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
247 {
248  AVFilterContext *ctx = link->dst;
249  CropContext *crop = ctx->priv;
250  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
251  int i;
252 
253  frame->video->w = crop->w;
254  frame->video->h = crop->h;
255 
256  crop->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
257  NAN : frame->pts * av_q2d(link->time_base);
258  crop->var_values[VAR_POS] = frame->pos == -1 ? NAN : frame->pos;
259  crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
260  crop->var_values[VAR_Y] = av_expr_eval(crop->y_pexpr, crop->var_values, NULL);
261  crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
262 
263  normalize_double(&crop->x, crop->var_values[VAR_X]);
264  normalize_double(&crop->y, crop->var_values[VAR_Y]);
265 
266  if (crop->x < 0) crop->x = 0;
267  if (crop->y < 0) crop->y = 0;
268  if ((unsigned)crop->x + (unsigned)crop->w > link->w) crop->x = link->w - crop->w;
269  if ((unsigned)crop->y + (unsigned)crop->h > link->h) crop->y = link->h - crop->h;
270  crop->x &= ~((1 << crop->hsub) - 1);
271  crop->y &= ~((1 << crop->vsub) - 1);
272 
273  av_dlog(ctx, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
274  (int)crop->var_values[VAR_N], crop->var_values[VAR_T], crop->x,
275  crop->y, crop->x+crop->w, crop->y+crop->h);
276 
277  frame->data[0] += crop->y * frame->linesize[0];
278  frame->data[0] += crop->x * crop->max_step[0];
279 
280  if (!(desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL)) {
281  for (i = 1; i < 3; i ++) {
282  if (frame->data[i]) {
283  frame->data[i] += (crop->y >> crop->vsub) * frame->linesize[i];
284  frame->data[i] += (crop->x * crop->max_step[i]) >> crop->hsub;
285  }
286  }
287  }
288 
289  /* alpha plane */
290  if (frame->data[3]) {
291  frame->data[3] += crop->y * frame->linesize[3];
292  frame->data[3] += crop->x * crop->max_step[3];
293  }
294 
295  crop->var_values[VAR_N] += 1.0;
296 
297  return ff_filter_frame(link->dst->outputs[0], frame);
298 }
299 
301  {
302  .name = "default",
303  .type = AVMEDIA_TYPE_VIDEO,
304  .filter_frame = filter_frame,
305  .get_video_buffer = ff_null_get_video_buffer,
306  .config_props = config_input,
307  },
308  { NULL }
309 };
310 
312  {
313  .name = "default",
314  .type = AVMEDIA_TYPE_VIDEO,
315  .config_props = config_output,
316  },
317  { NULL }
318 };
319 
321  .name = "crop",
322  .description = NULL_IF_CONFIG_SMALL("Crop the input video to width:height:x:y."),
323 
324  .priv_size = sizeof(CropContext),
325 
327  .init = init,
328  .uninit = uninit,
329 
330  .inputs = avfilter_vf_crop_inputs,
331  .outputs = avfilter_vf_crop_outputs,
332 };
AVExpr * x_pexpr
Definition: vf_crop.c:82
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
static int config_input(AVFilterLink *link)
Definition: vf_crop.c:157
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:114
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition: pixfmt.h:117
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:489
Definition: vf_crop.c:60
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:125
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:112
Definition: vf_crop.c:63
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:86
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:30
const char * name
Pad name.
Definition: internal.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:426
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
int x
x offset of the non-cropped area with respect to the input area
Definition: vf_crop.c:74
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:109
char oh_expr[256]
Definition: vf_crop.c:81
int y
y offset of the non-cropped area with respect to the input area
Definition: vf_crop.c:75
static av_always_inline av_const int isnan(float x)
Definition: libm.h:85
Definition: vf_crop.c:58
int max_step[4]
max pixel step for each plane, expressed as a number of bytes
Definition: vf_crop.c:79
#define NAN
Definition: math.h:7
Definition: eval.c:125
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:111
static const AVFilterPad avfilter_vf_crop_outputs[]
Definition: vf_crop.c:311
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
AVFilter avfilter_vf_crop
Definition: vf_crop.c:320
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:101
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
Definition: vf_crop.c:62
static const AVFilterPad avfilter_vf_crop_inputs[]
Definition: vf_crop.c:300
var_name
Definition: vf_boxblur.c:47
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:375
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_crop.c:119
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
A filter pad used for either input or output.
Definition: internal.h:33
int hsub
Definition: vf_crop.c:80
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:548
int h
image height
Definition: avfilter.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
Definition: vf_crop.c:67
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian
Definition: pixfmt.h:130
double var_values[VAR_VARS_NB]
Definition: vf_crop.c:83
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
void * priv
private data for use by the filter
Definition: avfilter.h:439
#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:146
static av_always_inline av_const double round(double x)
Definition: libm.h:151
planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian
Definition: pixfmt.h:128
static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Definition: vf_crop.c:246
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
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:67
char y_expr[256]
Definition: vf_crop.c:81
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:140
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
common internal API header
char x_expr[256]
Definition: vf_crop.c:81
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:89
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
#define M_E
Definition: ratecontrol.c:39
char ow_expr[256]
Definition: vf_crop.c:81
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
AVExpr * y_pexpr
Definition: vf_crop.c:82
static int query_formats(AVFilterContext *ctx)
Definition: vf_crop.c:86
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:139
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition: pixfmt.h:116
int h
height of the cropped area
Definition: vf_crop.c:77
int vsub
chroma subsampling
Definition: vf_crop.c:80
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:84
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:126
Definition: vf_crop.c:65
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:192
uint8_t flags
Definition: pixdesc.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_crop.c:134
Definition: vf_crop.c:61
Replacements for frequently missing libm functions.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Filter definition.
Definition: avfilter.h:371
Y , 16bpp, big-endian.
Definition: pixfmt.h:98
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
#define M_PHI
Definition: mathematics.h:34
const char * name
filter name
Definition: avfilter.h:372
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), little-endian, most significant bit to 1 ...
Definition: pixfmt.h:119
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
Definition: vf_crop.c:64
static const char *const var_names[]
Definition: vf_crop.c:41
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:113
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:129
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
static int normalize_double(int *n, double d)
Definition: vf_crop.c:142
static int config_output(AVFilterLink *link)
Definition: vf_crop.c:236
Definition: vf_crop.c:66
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:108
planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:127
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:87
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
Definition: vf_crop.c:69
int64_t pos
byte position in stream, -1 if unknown
Definition: avfilter.h:168
#define PIX_FMT_PSEUDOPAL
The pixel format is "pseudo-paletted".
Definition: pixdesc.h:97
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:539
Y , 16bpp, little-endian.
Definition: pixfmt.h:99
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
An instance of a filter.
Definition: avfilter.h:418
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), big-endian, most significant bit to 1 ...
Definition: pixfmt.h:118
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
int w
width of the cropped area
Definition: vf_crop.c:76
internal API functions
AVFilterBufferRef * ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Definition: video.c:72
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
simple arithmetic expression evaluator