[PATCH] Add different variants of a rotate filter.

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


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

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 74e55bb..d0bd98b 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -34,6 +34,9 @@ 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_ROTATE_SS_FILTER)              += vf_rotate.o
+OBJS-$(CONFIG_ROTATE_FLOAT_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..754b328 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -54,6 +54,9 @@ 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 (ROTATE_FLOAT,rotate_float,vf);
+    REGISTER_FILTER (ROTATE_SS,   rotate_ss,   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..2284836
--- /dev/null
+++ b/libavfilter/vf_rotate.c
@@ -0,0 +1,432 @@
+/*
+ * 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 packed pixel formats in the non-float path
+*/
+
+#include <math.h>
+#include "libavutil/colorspace.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "parseutils.h"
+
+enum { RED = 0, GREEN, BLUE, ALPHA };
+
+static int fill_line_with_color(uint8_t *line[4], int line_step[4], int w, uint8_t color[4],
+                                enum PixelFormat pix_fmt, uint8_t rgba_color[4], int *is_packed_rgba)
+{
+    uint8_t rgba_map[4] = {0};
+    int i;
+    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
+    int hsub = pix_desc->log2_chroma_w;
+
+    *is_packed_rgba = 1;
+    switch (pix_fmt) {
+    case PIX_FMT_ARGB:  rgba_map[ALPHA] = 0; rgba_map[RED  ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
+    case PIX_FMT_ABGR:  rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED  ] = 3; break;
+    case PIX_FMT_RGBA:
+    case PIX_FMT_RGB24: rgba_map[RED  ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
+    case PIX_FMT_BGRA:
+    case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED  ] = 2; rgba_map[ALPHA] = 3; break;
+    default:
+        *is_packed_rgba = 0;
+    }
+
+    if (*is_packed_rgba) {
+        line_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
+        for (i = 0; i < 4; i++)
+            color[rgba_map[i]] = rgba_color[i];
+
+        line[0] = av_malloc(w * line_step[0]);
+        for (i = 0; i < w; i++)
+            memcpy(line[0] + i * line_step[0], color, line_step[0]);
+    } else {
+        int plane;
+
+        color[RED  ] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
+        color[GREEN] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
+        color[BLUE ] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
+        color[ALPHA] = rgba_color[3];
+
+        for (plane = 0; plane < 4; plane++) {
+            int line_size;
+            int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
+
+            line_step[plane] = 1;
+            line_size = (w >> hsub1) * line_step[plane];
+            line[plane] = av_malloc(line_size);
+            memset(line[plane], color[plane], line_size);
+        }
+    }
+
+    return 0;
+}
+
+static void draw_rectangle(AVFilterBufferRef *outpic, uint8_t *line[4], int line_step[4],
+                           int hsub, int vsub, int x, int y, int w, int h)
+{
+    int i, plane;
+    uint8_t *p;
+
+    for (plane = 0; plane < 4 && outpic->data[plane]; plane++) {
+        int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
+        int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
+
+        p = outpic->data[plane] + (y >> vsub1) * outpic->linesize[plane];
+        for (i = 0; i < (h >> vsub1); i++) {
+            memcpy(p + (x >> hsub1) * line_step[plane], line[plane], (w >> hsub1) * line_step[plane]);
+            p += outpic->linesize[plane];
+        }
+    }
+}
+
+typedef struct {
+    int ang;
+    int hsub, vsub;
+    uint8_t backcolor[4];       ///< color expressed either in YUVA or RGBA colorspace for the padding area
+    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;
+} RotContext;
+
+#define FIXP (1<<16)
+#define INT_PI 205887 //(M_PI * FIXP)
+
+/**
+ * Compute the power of a p of a 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;
+}
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    RotContext *rot = ctx->priv;
+    char color_string[128] = "black";
+    rot->ang = 45;
+
+    if (args)
+        sscanf(args, "%d:%s", &rot->ang, color_string);
+
+    if (av_parse_color(rot->backcolor, color_string, ctx) < 0)
+        return AVERROR(EINVAL);
+
+    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;
+    }
+}
+
+static int query_formats_float(AVFilterContext *ctx)
+{
+    enum PixelFormat pix_fmts[] = {
+        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
+    };
+
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    enum PixelFormat pix_fmts[] = {
+        PIX_FMT_RGB24, PIX_FMT_NONE
+    };
+
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+    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;
+
+    rot->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+    rot->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+
+    if (!strcmp(ctx->filter->name, "rotate") ||
+        !strcmp(ctx->filter->name, "rotate_float")) {
+        rot->sinx = sin(rot->ang*M_PI/180.0);
+        rot->cosx = cos(rot->ang*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;
+    } else {
+        outlink->w = inlink->w;
+        outlink->h = inlink->h;
+    }
+
+    memcpy(rgba_color, rot->backcolor, sizeof(rgba_color));
+    fill_line_with_color(rot->line, rot->line_step, outlink->w, rot->backcolor,
+                         outlink->format, rgba_color, &is_packed_rgba);
+
+    av_log(ctx, AV_LOG_INFO, "ang:%d in_h:%d out_h:%d -> out_w:%d out_h:%d backcolor:0x%02X%02X%02X%02X[%s]\n",
+           rot->ang, inlink->w, inlink->h, outlink->w, outlink->h,
+           rot->backcolor[0], rot->backcolor[1], rot->backcolor[2], rot->backcolor[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_float(AVFilterLink *inlink)
+{
+    RotContext *rot = inlink->dst->priv;
+    AVFilterBufferRef *in  = inlink->cur_buf;
+    AVFilterBufferRef *out = inlink->dst->outputs[0]->out_buf;
+    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->backcolor[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->backcolor[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);
+                }
+            }
+
+    avfilter_unref_buffer(in);
+    avfilter_draw_slice(inlink->dst->outputs[0], 0, rot->output_h, 1);
+    avfilter_end_frame(inlink->dst->outputs[0]);
+    avfilter_unref_buffer(out);
+}
+
+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;
+
+    int radian =  2 * rot->ang * 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 */
+    draw_rectangle(out, 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) {
+                pin  = in ->data[0] + y1 * in ->linesize[0] + x1 * 3;
+                pout = out->data[0] + j  * out->linesize[0] + i  * 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,
+
+    .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}},
+};
+
+AVFilter avfilter_vf_rotate_float = {
+    .name      = "rotate_float",
+    .description = NULL_IF_CONFIG_SMALL("Rotate the input image, using float numbers."),
+    .init      = init,
+    .uninit    = uninit,
+
+    .priv_size = sizeof(RotContext),
+
+    .query_formats = query_formats_float,
+
+    .inputs    = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .start_frame     = start_frame,
+                                    .draw_slice      = null_draw_slice,
+                                    .end_frame       = end_frame_float,
+                                    .min_perms       = AV_PERM_READ, },
+                                  { .name = NULL}},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .config_props    = config_props_output,
+                                    .type            = AVMEDIA_TYPE_VIDEO, },
+                                  { .name = NULL}},
+};
+
+AVFilter avfilter_vf_rotate_ss = {
+    .name      = "rotate_ss",
+    .description = NULL_IF_CONFIG_SMALL("Rotate the input image, keeping the Same Size."),
+    .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


--/04w6evG8XlLl3ft--



More information about the ffmpeg-devel mailing list