[FFmpeg-devel] [PATCH 4/4] Make the crop filter accept parametric expressions.
Stefano Sabatini
stefano.sabatini-lala
Thu Jul 22 19:52:35 CEST 2010
---
doc/filters.texi | 45 +++++++++++++++++++++++++---------
libavfilter/vf_crop.c | 65 ++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 92 insertions(+), 18 deletions(-)
diff --git a/doc/filters.texi b/doc/filters.texi
index 7f0cb71..a767b2c 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -12,32 +12,53 @@ Below is a description of the currently available video filters.
Crop the input video to @var{x}:@var{y}:@var{width}:@var{height}.
- at example
-./ffmpeg -i in.avi -vf "crop=0:0:0:240" out.avi
- at end example
+The parameters are expressions containing one the following constants:
- at var{x} and @var{y} specify the position of the top-left corner of the
-output (non-cropped) area.
+ at table
+ at item E, PI, PHI
+Represent the corresponding mathematical approximated values for e
+(euler number), pi (greek PI), PHI (golden ratio).
-The default value of @var{x} and @var{y} is 0.
+ at item w, h
+Specify the input width and heigth.
+
+ at item x, y
+Represent the computed values for @var{x} and @var{y}.
+
+ at end table
+
+ at var{x} and @var{y} specify the expression for the position of the
+top-left corner of the output (non-cropped) area.
+
+The default value of @var{x} and @var{y} is "0".
-The @var{width} and @var{height} parameters specify the width and height
-of the output (non-cropped) area.
+The @var{width} and @var{height} parameters specify the expression for
+the width and height of the output (non-cropped) area.
-A value of 0 is interpreted as the maximum possible size contained in
-the area delimited by the top-left corner at position x:y.
+The default value of @var{width} is "w-x", and the default value of
+ at var{height} is "h-y", which specify the maximum possible size
+contained in the area delimited by the top-left corner at position
+x:y.
For example the parameters:
@example
-"crop=100:100:0:0"
+"crop=100:100"
@end example
will delimit the rectangle with the top-left corner placed at position
100:100 and the right-bottom corner corresponding to the right-bottom
corner of the input image.
-The default value of @var{width} and @var{height} is 0.
+Follow other examples:
+ at example:
+# crop 10 pixels from the lefth and right borders, and 20 pixels from
+# the top and bottom borders
+crop=10:20:w-2*x:h-2*y
+
+# keep only the bottom right quarter of the input image
+crop=w/2:h/2
+ at end example
@section format
diff --git a/libavfilter/vf_crop.c b/libavfilter/vf_crop.c
index 6a7ae70..df9b5a2 100644
--- a/libavfilter/vf_crop.c
+++ b/libavfilter/vf_crop.c
@@ -24,7 +24,31 @@
*/
#include "avfilter.h"
+#include "libavutil/eval.h"
#include "libavutil/pixdesc.h"
+#include "libavutil/avstring.h"
+
+static const char *var_names[] = {
+ "E",
+ "PHI",
+ "PI",
+ "x",
+ "y",
+ "w", ///< width of the input video
+ "h", ///< height of the input video
+ NULL
+};
+
+enum var_name {
+ E,
+ PHI,
+ PI,
+ X,
+ Y,
+ W,
+ H,
+ VARS_NB
+};
typedef struct {
int x; ///< x offset of the non-cropped area with respect to the input area
@@ -34,6 +58,8 @@ typedef struct {
int plane_max_step[4]; ///< the pixel step for each plane, expressed as a number of bytes
int hsub, vsub; ///< chroma subsampling
+
+ char x_expr[256], y_expr[256], w_expr[256], h_expr[256];
} CropContext;
static int query_formats(AVFilterContext *ctx)
@@ -72,8 +98,13 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
{
CropContext *crop = ctx->priv;
+ av_strlcpy(crop->x_expr, "0" , sizeof(crop->x_expr));
+ av_strlcpy(crop->y_expr, "0" , sizeof(crop->y_expr));
+ av_strlcpy(crop->w_expr, "w-x", sizeof(crop->w_expr));
+ av_strlcpy(crop->h_expr, "h-y", sizeof(crop->h_expr));
+
if (args)
- sscanf(args, "%d:%d:%d:%d", &crop->x, &crop->y, &crop->w, &crop->h);
+ sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]", crop->x_expr, crop->y_expr, crop->w_expr, crop->h_expr);
return 0;
}
@@ -83,7 +114,15 @@ static int config_input(AVFilterLink *link)
AVFilterContext *ctx = link->dst;
CropContext *crop = ctx->priv;
const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[link->format];
- int i;
+ int i, ret;
+ const char *expr;
+ double var_values[VARS_NB], res;
+
+ var_values[E ] = M_E;
+ var_values[PHI] = M_PHI;
+ var_values[PI ] = M_PI;
+ var_values[W ] = ctx->inputs[0]->w;
+ var_values[H ] = ctx->inputs[0]->h;
memset(crop->plane_max_step, 0, sizeof(crop->plane_max_step));
for (i = 0; i < 4; i++) {
@@ -95,13 +134,22 @@ static int config_input(AVFilterLink *link)
crop->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
crop->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
- if (crop->w == 0)
- crop->w = link->w - crop->x;
- if (crop->h == 0)
- crop->h = link->h - crop->y;
+#define PARSE_AND_EVAL(var, var_expr) \
+ if ((ret = av_parse_and_eval_expr(&res, (expr = var_expr), var_names, var_values, \
+ NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) \
+ goto fail_expr; \
+ var = res; \
+
+ PARSE_AND_EVAL(crop->x, crop->x_expr);
+ PARSE_AND_EVAL(crop->y, crop->y_expr);
crop->x &= ~((1 << crop->hsub) - 1);
crop->y &= ~((1 << crop->vsub) - 1);
+ var_values[X] = crop->x;
+ var_values[Y] = crop->y;
+
+ PARSE_AND_EVAL(crop->w, crop->w_expr);
+ PARSE_AND_EVAL(crop->h, crop->h_expr);
av_log(link->dst, AV_LOG_INFO, "x:%d y:%d w:%d h:%d\n",
crop->x, crop->y, crop->w, crop->h);
@@ -117,6 +165,11 @@ static int config_input(AVFilterLink *link)
}
return 0;
+
+fail_expr:
+ av_log(NULL, AV_LOG_ERROR,
+ "Error when evaluating the expression '%s'\n", expr);
+ return ret;
}
static int config_output(AVFilterLink *link)
--
1.6.0.4
More information about the ffmpeg-devel
mailing list