[PATCH 4/4] Add rotate90 filter.

Stefano Sabatini stefano.sabatini-lala
Thu Oct 14 21:03:19 CEST 2010


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

diff --git a/doc/filters.texi b/doc/filters.texi
index d4552da..d8a185a 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -438,6 +438,12 @@ 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, also
+negative values are accepted. If not specified a default value of 90
+is assumed.
+
 @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 532d1c1..1fb9416 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_ROTATE90_FILTER)               += transpose.o vf_rotate90.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SETTB_FILTER)                  += vf_settb.o
 OBJS-$(CONFIG_SLICIFY_FILTER)                += vf_slicify.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index bade1dd..52db2ea 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -55,6 +55,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (PAD,         pad,         vf);
     REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
     REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
+    REGISTER_FILTER (ROTATE90,    rotate90,    vf);
     REGISTER_FILTER (SCALE,       scale,       vf);
     REGISTER_FILTER (SETTB,       settb,       vf);
     REGISTER_FILTER (SLICIFY,     slicify,     vf);
diff --git a/libavfilter/vf_rotate90.c b/libavfilter/vf_rotate90.c
new file mode 100644
index 0000000..bae98fc
--- /dev/null
+++ b/libavfilter/vf_rotate90.c
@@ -0,0 +1,195 @@
+/*
+ * 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;
+
+    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 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) {
+        av_image_copy(outpic->data, outpic->linesize,
+                      inpic ->data, inpic ->linesize,
+                      inlink->format, inlink->w, inlink->h);
+    } 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,
+                                    .end_frame        = end_frame },
+                                  { .name = NULL}},
+
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .config_props     = config_output_props },
+                                  { .name = NULL}},
+};
-- 
1.7.1


--neYutvxvOLaeuPCA--



More information about the ffmpeg-devel mailing list