[PATCH 2/2] Add rotate filter.

Stefano Sabatini stefano.sabatini-lala
Sun Oct 3 12:20:14 CEST 2010


---
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_rotate.c  |  423 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 425 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_rotate.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index a0407a5..177a3f1 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -35,6 +35,7 @@ OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PIXELASPECT_FILTER)            += vf_aspect.o
+OBJS-$(CONFIG_ROTATE_FILTER)                 += vf_rotate.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SLICIFY_FILTER)                += vf_slicify.o
 OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index c5da648..944c7ec 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -54,6 +54,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (PAD,         pad,         vf);
     REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
     REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
+    REGISTER_FILTER (ROTATE,      rotate,      vf);
     REGISTER_FILTER (SCALE,       scale,       vf);
     REGISTER_FILTER (SLICIFY,     slicify,     vf);
     REGISTER_FILTER (UNSHARP,     unsharp,     vf);
diff --git a/libavfilter/vf_rotate.c b/libavfilter/vf_rotate.c
new file mode 100644
index 0000000..cce39f6
--- /dev/null
+++ b/libavfilter/vf_rotate.c
@@ -0,0 +1,423 @@
+/*
+ * Copyright (C) 2010 Stefano Sabatini
+ * Copyright (C) 2008 Vitor Sessak
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * rotation filter
+ *
+ * @todo handle planar pixel and more packed formats in the non-float path
+*/
+
+#include "libavutil/eval.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "drawutils.h"
+#include "parseutils.h"
+
+static const char *var_names[] = {
+    "E",
+    "PHI",
+    "PI",
+    "w",            ///< width  of the input video
+    "h",            ///< height of the input video
+    "n",            ///< number of frame
+    "t",            ///< timestamp expressed in seconds
+    NULL
+};
+
+enum var_name {
+    VAR_E,
+    VAR_PHI,
+    VAR_PI,
+    VAR_W,
+    VAR_H,
+    VAR_N,
+    VAR_T,
+    VAR_VARS_NB
+};
+
+#define FIXP (1<<16)
+#define INT_PI 205887 //(M_PI * FIXP)
+
+/**
+ * Compute the power of a a^p using integer values.
+ * Input and output values are scaled by FIXP.
+ */
+static int64_t int_pow(int64_t a, int p)
+{
+    int64_t v = FIXP;
+
+    for (; p; p--) {
+        v *= a;
+        v /= FIXP;
+    }
+
+    return v;
+}
+
+/**
+ * Compute the sin of a using integer values.
+ * Input and output values are scaled by FIXP.
+ */
+static int64_t int_sin(int64_t a)
+{
+    if (a < 0) a = INT_PI-a; // 0..inf
+    a %= 2 * INT_PI;         // 0..2PI
+
+    if (a >= INT_PI*3/2) a -= 2*INT_PI;  // -PI/2 .. 3PI/2
+    if (a >= INT_PI/2  ) a = INT_PI - a; // -PI/2 ..  PI/2
+
+    return a - int_pow(a, 3)/6 + int_pow(a, 5)/120 - int_pow(a, 7)/5040;
+}
+
+/**
+ * Interpolate the color in src at position x and y using bilinear
+ * interpolation.
+ *
+ * @param dst_color put here the destination color
+ */
+static uint8_t *ipol(uint8_t *dst_color,
+                     const uint8_t *src, const int src_linesize, int x, int y,
+                     int max_x, int max_y)
+{
+    int int_x = x>>16;
+    int int_y = y>>16;
+    int frac_x = x&0xFFFF;
+    int frac_y = y&0xFFFF;
+    int i;
+
+    for (i = 0; i < 3; i++) {
+        int s00 = src[3 * int_x                + i + src_linesize * int_y];
+        int s01 = src[3 * FFMIN(int_x+1,max_x) + i + src_linesize * int_y];
+        int s10 = src[3 * int_x                + i + src_linesize * FFMIN(int_y+1, max_y)];
+        int s11 = src[3 * FFMIN(int_x+1,max_x) + i + src_linesize * FFMIN(int_y+1, max_y)];
+        int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01)>>8;
+        int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11)>>8;
+
+        dst_color[i] = (((1<<16) - frac_y)*s0 + frac_y*s1)>>24;
+    }
+
+    return dst_color;
+}
+
+typedef struct {
+    const AVClass *class;
+    int angle;
+    char *angle_expr;       ///< expression for the angle
+    AVExpr *angle_pexpr;    ///< parsed expression for the angle
+    uint8_t bgcolor[4];     ///< color expressed either in YUVA or RGBA colorspace for the padding area
+    char *bgcolor_str;
+    int hsub, vsub;
+    int use_float;
+    int use_bilinear;
+    int keep_same_size;
+    uint8_t *line[4];
+    int      line_step[4];
+    float transx, transy; ///< how much to translate (in pixels)
+    float sinx, cosx;
+    int output_h, output_w;
+    double var_values[VAR_VARS_NB];
+} RotContext;
+
+#define OFFSET(x) offsetof(RotContext, x)
+
+static const AVOption rot_options[]= {
+    {"angle",    "set angle expression", OFFSET(angle_expr),     FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
+    {"bgcolor",  "set background color", OFFSET(bgcolor_str),    FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
+    {"float",    "use float path",       OFFSET(use_float),      FF_OPT_TYPE_INT,    0,         0,        1 },
+    {"ss",       "keep same size",       OFFSET(keep_same_size), FF_OPT_TYPE_INT,    0,         0,        1 },
+    {"bilinear", "use bilinear interpolation", OFFSET(use_bilinear), FF_OPT_TYPE_INT, 1,        0,        1 },
+    {NULL},
+};
+
+static const char *rot_get_name(void *ctx)
+{
+    return "rot";
+}
+
+static const AVClass rot_class = {
+    "RotContext",
+    rot_get_name,
+    rot_options
+};
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    RotContext *rot = ctx->priv;
+    int ret;
+
+    rot->class = &rot_class;
+    av_opt_set_defaults2(rot, 0, 0);
+    rot->bgcolor_str = av_strdup("black");
+    rot->angle_expr  = av_strdup("45");
+
+    if (args && (ret = (av_set_options_string(rot, args, "=", ":"))) < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
+        return ret;
+    }
+
+    if ((ret = av_parse_color(rot->bgcolor, rot->bgcolor_str, ctx)) < 0)
+        return ret;
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    RotContext *rot = ctx->priv;
+    int i;
+
+    for (i = 0; i < 4; i++) {
+        av_freep(&rot->line[i]);
+        rot->line_step[i] = 0;
+    }
+
+    av_freep(&rot->bgcolor_str);
+    av_freep(&rot->angle_expr);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    RotContext *rot = ctx->priv;
+
+    static enum PixelFormat pix_fmts_float[] = {
+        PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
+        PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
+        PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
+        PIX_FMT_YUV440P,  PIX_FMT_YUVJ440P,
+        PIX_FMT_NONE
+    };
+    static enum PixelFormat pix_fmts_non_float[] = {
+        PIX_FMT_RGB24, PIX_FMT_NONE
+    };
+
+    avfilter_set_common_formats(
+        ctx, avfilter_make_format_list(rot->use_float ? pix_fmts_float : pix_fmts_non_float));
+    return 0;
+}
+
+static int config_props_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    RotContext *rot = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+    uint8_t rgba_color[4];
+    int is_packed_rgba, ret;
+    double res;
+
+    rot->var_values[VAR_E]   = M_E;
+    rot->var_values[VAR_PHI] = M_PHI;
+    rot->var_values[VAR_PI]  = M_PI;
+    rot->var_values[VAR_W]   = ctx->inputs[0]->w;
+    rot->var_values[VAR_H]   = ctx->inputs[0]->h;
+    rot->var_values[VAR_N]   = 0;
+    rot->var_values[VAR_T]   = NAN;
+
+    if ((ret = av_parse_expr(&rot->angle_pexpr, rot->angle_expr, var_names,
+                             NULL, NULL, NULL, NULL, 0, ctx)) < 0)
+        return ret;
+    if (!rot->keep_same_size) {
+        res = av_eval_expr(rot->angle_pexpr, rot->var_values, NULL);
+        if (isnan(res)) {
+            av_log(ctx, AV_LOG_ERROR, "Invalid angle expression '%s', evaluates to nan.\n", rot->angle_expr);
+            return AVERROR(EINVAL);
+        }
+        rot->angle = res;
+    }
+
+    rot->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+    rot->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+
+    if (rot->keep_same_size) {
+        outlink->w = inlink->w;
+        outlink->h = inlink->h;
+    } else {
+        rot->sinx = sin(rot->angle*M_PI/180.0);
+        rot->cosx = cos(rot->angle*M_PI/180.0);
+
+        rot->transx = FFMAX(0,  outlink->src->inputs[0]->h * rot->sinx) +
+                      FFMAX(0, -outlink->src->inputs[0]->w * rot->cosx);
+        rot->transy = FFMAX(0, -outlink->src->inputs[0]->h * rot->cosx) +
+                      FFMAX(0, -outlink->src->inputs[0]->w * rot->sinx);
+        rot->output_w = rot->transx + FFMAX(0, rot->cosx * outlink->src->inputs[0]->w) +
+                                      FFMAX(0, -rot->sinx * outlink->src->inputs[0]->h);
+        rot->output_h = rot->transy + FFMAX(0, rot->cosx * outlink->src->inputs[0]->h) +
+                                      FFMAX(0,  rot->sinx * outlink->src->inputs[0]->w);
+        outlink->w = rot->output_w;
+        outlink->h = rot->output_h;
+    }
+
+    memcpy(rgba_color, rot->bgcolor, sizeof(rgba_color));
+    ff_fill_line_with_color(rot->line, rot->line_step, outlink->w, rot->bgcolor,
+                            outlink->format, rgba_color, &is_packed_rgba);
+
+    av_log(ctx, AV_LOG_INFO, "angle:%d in_h:%d out_h:%d -> out_w:%d out_h:%d bgcolor:0x%02X%02X%02X%02X[%s]\n",
+           rot->angle, inlink->w, inlink->h, outlink->w, outlink->h,
+           rot->bgcolor[0], rot->bgcolor[1], rot->bgcolor[2], rot->bgcolor[3],
+           is_packed_rgba ? "rgba" : "yuva");
+
+    return 0;
+}
+
+static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+{
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFilterBufferRef *outpicref;
+
+    outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    outpicref = outlink->out_buf;
+    avfilter_copy_buffer_ref_props(outpicref, picref);
+    outpicref->video->w = outlink->w;
+    outpicref->video->h = outlink->h;
+
+    avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
+}
+
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+
+static void end_frame(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    RotContext *rot = ctx->priv;
+    AVFilterBufferRef *in  = inlink->cur_buf;
+    AVFilterBufferRef *out = ctx->outputs[0]->out_buf;
+
+    if (rot->keep_same_size) {
+        double res;
+        rot->var_values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : (double)in->pts / AV_TIME_BASE;
+        res = av_eval_expr(rot->angle_pexpr, rot->var_values, NULL);
+        if (!isnan(res))
+            rot->angle = res;
+        rot->var_values[VAR_N] += 1.0;
+    }
+
+    if (rot->use_float) {
+        int i, j, plane;
+        /* luma plane */
+        for (i = 0; i < rot->output_h; i++)
+            for (j = 0; j < rot->output_w; j++) {
+                int line   = (i - rot->transy)*rot->sinx +
+                             (j - rot->transx)*rot->cosx + 0.5;
+                int column = (i - rot->transy)*rot->cosx -
+                             (j - rot->transx)*rot->sinx + 0.5;
+                if (line < 0 || line >= in->video->w || column < 0 || column >= in->video->h)
+                    *(out->data[0] +   i*out->linesize[0] + j) = rot->bgcolor[0];
+                else
+                    *(out->data[0] +      i*out->linesize[0] + j) =
+                    *( in->data[0] + column*in ->linesize[0] + line);
+            }
+
+        /* chroma planes */
+        for (plane = 1; plane < 3; plane ++)
+            for (i = 0 >> rot->vsub; i < rot->output_h >> rot->vsub; i++)
+                for (j = 0; j < rot->output_w >> rot->hsub; j++) {
+                    int i2 = (i + rot->vsub/2) << rot->vsub;
+                    int j2 = (j + rot->hsub/2) << rot->hsub;
+                    int line =   (i2 - rot->transy)*rot->sinx +
+                        (j2 - rot->transx)*rot->cosx + 0.5;
+                    int column = (i2 - rot->transy)*rot->cosx -
+                        (j2 - rot->transx)*rot->sinx + 0.5;
+                    if (line < 0 || line >= in->video->w || column < 0 || column >= in->video->h) {
+                        *(out->data[plane] +   i*out->linesize[plane] + j) =
+                            rot->bgcolor[plane];
+                    } else {
+                        line   = (line   + rot->hsub/2) >> rot->hsub;
+                        column = (column + rot->vsub/2) >> rot->vsub;
+
+                        *(out->data[plane] +   i*out->linesize[plane] + j) =
+                            *(in->data[plane] + column*in->linesize[plane] + line);
+                    }
+                }
+    } else {
+        int radian =  2 * rot->angle * INT_PI/360;
+        const int s = int_sin(radian           );
+        const int c = int_sin(radian + INT_PI/2);
+        const int xi = -out->video->w/2 * c;
+        const int yi =  out->video->w/2 * s;
+        const int xj = -out->video->h/2 * s;
+        const int yj = -out->video->h/2 * c;
+        int xprime = xj;
+        int yprime = yj;
+        int i, j, x, y;
+
+        /* fill with the backcolor */
+        ff_draw_rectangle(out->data, out->linesize,
+                          rot->line, rot->line_step, rot->hsub, rot->vsub,
+                          0, 0, out->video->w, out->video->h);
+
+        for (j = 0; j < out->video->h; j++) {
+            x = xprime + xi + FIXP*in->video->w/2;
+            y = yprime + yi + FIXP*in->video->h/2;
+
+            for (i = 0; i < out->video->w; i++) {
+                int32_t v;
+                int x1, y1;
+                uint8_t *pin, *pout;
+                x += c;
+                y -= s;
+                x1 = x>>16;
+                y1 = y>>16;
+                if (x1 >= 0 && x1 < in->video->w && y1 >= 0 && y1 < in->video->h) {
+                    av_unused uint8_t inp_inv[3]; /* interpolated input value */
+                    pout = out->data[0] + j  * out->linesize[0] + i  * 3;
+                    if (rot->use_bilinear)
+                        pin = ipol(inp_inv, in->data[0], in->linesize[0], x, y, in->video->w-1, in->video->h-1);
+                    else
+                        pin  = in ->data[0] + y1 * in ->linesize[0] + x1 * 3;
+                    v = AV_RB24(pin);
+                    AV_WB24(pout, v);
+                }
+            }
+
+            xprime += s;
+            yprime += c;
+        }
+    }
+
+    avfilter_unref_buffer(in);
+    avfilter_draw_slice(ctx->outputs[0], 0, out->video->h, 1);
+    avfilter_end_frame(ctx->outputs[0]);
+    avfilter_unref_buffer(out);
+}
+
+AVFilter avfilter_vf_rotate = {
+    .name      = "rotate",
+    .description = NULL_IF_CONFIG_SMALL("Rotate the input image."),
+    .init      = init,
+    .uninit    = uninit,
+
+    .priv_size = sizeof(RotContext),
+
+    .query_formats = query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .start_frame     = start_frame,
+                                    .draw_slice      = null_draw_slice,
+                                    .end_frame       = end_frame,
+                                    .min_perms       = AV_PERM_READ, },
+                                  { .name = NULL}},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .config_props    = config_props_output,
+                                    .type            = AVMEDIA_TYPE_VIDEO, },
+                                  { .name = NULL}},
+};
-- 
1.7.1


--jI8keyz6grp/JLjh--



More information about the ffmpeg-devel mailing list