[PATCH] Add rotate90 filter.

Stefano Sabatini stefano.sabatini-lala
Tue Nov 23 20:59:46 CET 2010


---
 doc/filters.texi          |   21 +++++
 libavfilter/Makefile      |    1 +
 libavfilter/allfilters.c  |    1 +
 libavfilter/vf_rotate90.c |  208 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 231 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_rotate90.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 1cba2d6..62005a1 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -505,6 +505,27 @@ format=monow, pixdesctest
 
 can be used to test the monowhite pixel format descriptor definition.
 
+ at section rotate90
+
+Rotate the input video of an angle multiple of 90 degrees.
+
+Accepts a number for the rotation angle as parameter. If the value
+for angle is positive, rotate counterclockwise, if negative rotate
+clockwise, if 0 is specified, behave as the null filter. If the value
+angle is not specified, a default value of 90 is assumed.
+
+Examples:
+ at example
+# rotate by 90 degrees counterclockwise
+rotate=90
+
+# rotate by 90 degrees clockwise
+rotate=90
+
+# as the null filter
+rotate=0
+ at end example
+
 @section scale
 
 Scale the input video to @var{width}:@var{height} and/or convert the image format.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 72cbe4b..788aae5 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -33,6 +33,7 @@ OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
 OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
+OBJS-$(CONFIG_ROTATE90_FILTER)               += transpose.o vf_rotate90.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SETDAR_FILTER)                 += vf_aspect.o
 OBJS-$(CONFIG_SETPTS_FILTER)                 += vf_setpts.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 9e3ba14..760d490 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -54,6 +54,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (OVERLAY,     overlay,     vf);
     REGISTER_FILTER (PAD,         pad,         vf);
     REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
+    REGISTER_FILTER (ROTATE90,    rotate90,    vf);
     REGISTER_FILTER (SCALE,       scale,       vf);
     REGISTER_FILTER (SETDAR,      setdar,      vf);
     REGISTER_FILTER (SETPTS,      setpts,      vf);
diff --git a/libavfilter/vf_rotate90.c b/libavfilter/vf_rotate90.c
new file mode 100644
index 0000000..1fdb811
--- /dev/null
+++ b/libavfilter/vf_rotate90.c
@@ -0,0 +1,208 @@
+/*
+ * Copyright (c) 2010 Stefano Sabatini
+ * 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
+ * rotate video by 90 degrees multiple
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "libavutil/pixdesc.h"
+#include "libavcore/imgutils.h"
+#include "avfilter.h"
+#include "transpose.h"
+
+typedef struct {
+    int angle;
+    int hsub, vsub;
+    int pixsteps[4];
+} Rotate90Context;
+
+static void invert(uint8_t *out_planes[4], int out_linesizes[4],
+                   uint8_t *in_planes [4], int in_linesizes [4],
+                   int outw0, int inh0,
+                   int pixsteps[4], int hsub0, int vsub0)
+{
+    int plane;
+
+    for (plane = 0; plane < 4 && out_planes[plane]; plane++) {
+        int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
+        int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
+        int pixstep = pixsteps[plane];
+        int outw = outw0>>hsub;
+        int outh = inh0 >>vsub;
+        uint8_t *out, *in;
+        int outlinesize, inlinesize;
+        int x, y;
+
+        out = out_planes[plane]; outlinesize = out_linesizes[plane];
+        in  = in_planes [plane]; inlinesize  = in_linesizes [plane];
+
+        for (y = 0; y < outh; y++) {
+            int y1 = outh -1 -y;
+
+            switch (pixstep) {
+            case 1:
+                for (x = 0; x < outw; x++)
+                    *(out + x) = *(in + y1*inlinesize + outw -1 -x);
+                break;
+            case 2:
+                for (x = 0; x < outw; x++)
+                    *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + y1*inlinesize + (outw -1 -x)*2));
+                break;
+            case 3:
+                for (x = 0; x < outw; x++) {
+                    int32_t v = AV_RB24(in + y1*inlinesize + (outw -1 -x)*3);
+                    AV_WB24(out + 3*x, v);
+                }
+                break;
+            case 4:
+                for (x = 0; x < outw; x++)
+                    *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + y1*inlinesize + (outw -1 -x)*4));
+                break;
+            }
+            out += outlinesize;
+        }
+    }
+}
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    Rotate90Context *rot90 = ctx->priv;
+    rot90->angle = 90;
+
+    if (args)
+        sscanf(args, "%d", &rot90->angle);
+
+    if (rot90->angle % 90) {
+        av_log(ctx, AV_LOG_ERROR, "Invalid angle %d, is not a multiple of 90.\n",
+               rot90->angle);
+        return AVERROR(EINVAL);
+    }
+
+    rot90->angle %= 360;
+    if (rot90->angle < 0)
+        rot90->angle += 360;
+    return 0;
+}
+
+static int config_output_props(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    Rotate90Context *rot90 = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+    const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[outlink->format];
+    rot90->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+    rot90->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+
+    av_image_fill_max_pixsteps(rot90->pixsteps, NULL, pixdesc);
+
+    switch (rot90->angle) {
+    case 90:
+    case 270:
+        outlink->w = inlink->h;
+        outlink->h = inlink->w;
+        break;
+    default:
+        outlink->w = inlink->w;
+        outlink->h = inlink->h;
+    }
+
+    av_log(ctx, AV_LOG_INFO, "w:%d h:%d angle:%d -> w:%d h:%d\n",
+           inlink->w, inlink->h, rot90->angle, outlink->w, outlink->h);
+    return 0;
+}
+
+static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+{
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    Rotate90Context *rot90 = inlink->dst->priv;
+
+    if (!rot90->angle) {
+        avfilter_null_start_frame(inlink, picref);
+        return;
+    }
+
+    outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
+                                                 outlink->w, outlink->h);
+    outlink->out_buf->pts = picref->pts;
+
+    if (picref->video->pixel_aspect.num == 0 || rot90->angle == 0 || rot90->angle == 180) {
+        outlink->out_buf->video->pixel_aspect = picref->video->pixel_aspect;
+    } else {
+        outlink->out_buf->video->pixel_aspect.num = picref->video->pixel_aspect.den;
+        outlink->out_buf->video->pixel_aspect.den = picref->video->pixel_aspect.num;
+    }
+
+    avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
+}
+
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
+{
+    Rotate90Context *rot90 = inlink->dst->priv;
+
+    if (!rot90->angle)
+        avfilter_default_draw_slice(inlink, y, h, slice_dir);
+}
+
+static void end_frame(AVFilterLink *inlink)
+{
+    Rotate90Context *rot90 = inlink->dst->priv;
+    AVFilterBufferRef *inpic  = inlink->cur_buf;
+    AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+
+    if (rot90->angle == 0) {
+        avfilter_null_end_frame(inlink);
+        return;
+    } else if (rot90->angle == 90 || rot90->angle == 270) {
+        ff_transpose(outpic->data, outpic->linesize, inpic->data, inpic->linesize,
+                     outpic->video->w, outpic->video->h, rot90->pixsteps,
+                     rot90->hsub, rot90->vsub, rot90->angle == 90 ? 2 : 1);
+    } else if (rot90->angle == 180)
+        invert(outpic->data, outpic->linesize, inpic->data, inpic->linesize,
+               outpic->video->w, outpic->video->h, rot90->pixsteps,
+               rot90->hsub, rot90->vsub);
+
+    avfilter_unref_buffer(inpic);
+    avfilter_draw_slice(outlink, 0, outpic->video->h, 1);
+    avfilter_end_frame(outlink);
+    avfilter_unref_buffer(outpic);
+}
+
+AVFilter avfilter_vf_rotate90 = {
+    .name      = "rotate90",
+    .description = NULL_IF_CONFIG_SMALL("Rotate input video by 90 degrees multiple."),
+    .init = init,
+
+    .priv_size = sizeof(Rotate90Context),
+    .query_formats = ff_transpose_query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .start_frame      = start_frame,
+                                    .draw_slice       = draw_slice,
+                                    .end_frame        = end_frame },
+                                  { .name = NULL}},
+
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .config_props     = config_output_props },
+                                  { .name = NULL}},
+};
-- 
1.7.1


--U+BazGySraz5kW0T--



More information about the ffmpeg-devel mailing list