[PATCH] Add transpose filter.

Stefano Sabatini stefano.sabatini-lala
Mon Oct 11 14:16:35 CEST 2010


---
 doc/filters.texi           |    4 +
 libavfilter/Makefile       |    1 +
 libavfilter/allfilters.c   |    1 +
 libavfilter/vf_transpose.c |  170 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 176 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_transpose.c

diff --git a/doc/filters.texi b/doc/filters.texi
index e4c1851..9452c9b 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -443,6 +443,10 @@ not specified it will use the default value of 16.
 Adding this in the beginning of filter chains should make filtering
 faster due to better use of the memory cache.
 
+ at section transpose
+
+Transpose lines with columns in the input video.
+
 @section unsharp
 
 Sharpen or blur the input video.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 74e55bb..76d122a 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -36,6 +36,7 @@ OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PIXELASPECT_FILTER)            += vf_aspect.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SLICIFY_FILTER)                += vf_slicify.o
+OBJS-$(CONFIG_TRANSPOSE_FILTER)              += vf_transpose.o
 OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
 OBJS-$(CONFIG_YADIF_FILTER)                  += vf_yadif.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index c5da648..6260594 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -56,6 +56,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
     REGISTER_FILTER (SCALE,       scale,       vf);
     REGISTER_FILTER (SLICIFY,     slicify,     vf);
+    REGISTER_FILTER (TRANSPOSE,   transpose,   vf);
     REGISTER_FILTER (UNSHARP,     unsharp,     vf);
     REGISTER_FILTER (VFLIP,       vflip,       vf);
     REGISTER_FILTER (YADIF,       yadif,       vf);
diff --git a/libavfilter/vf_transpose.c b/libavfilter/vf_transpose.c
new file mode 100644
index 0000000..80c0e3d
--- /dev/null
+++ b/libavfilter/vf_transpose.c
@@ -0,0 +1,170 @@
+/*
+ * 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
+ * transposition filter
+ *
+ * @todo handle packed pixel formats
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "libavutil/pixdesc.h"
+#include "libavcore/imgutils.h"
+#include "avfilter.h"
+
+typedef struct {
+    int hsub, vsub;
+    int nb_planes;
+    int pixsteps[4];
+} TransContext;
+
+static int config_props_input(AVFilterLink *inlink)
+{
+    TransContext *trans = inlink->dst->priv;
+
+    trans->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+    trans->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    enum PixelFormat pix_fmts[] = {
+        PIX_FMT_ARGB,     PIX_FMT_RGBA,
+        PIX_FMT_ABGR,     PIX_FMT_BGRA,
+        PIX_FMT_RGB24,    PIX_FMT_BGR24,
+        PIX_FMT_YUV444P,  PIX_FMT_YUV422P,
+        PIX_FMT_YUV420P,  PIX_FMT_YUVJ420P,
+        PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
+        PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
+        PIX_FMT_YUV440P,  PIX_FMT_YUVJ440P,
+        PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
+        PIX_FMT_NONE
+    };
+
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_props_output(AVFilterLink *outlink)
+{
+    TransContext *trans = outlink->src->priv;
+    const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[outlink->format];
+    int i;
+    outlink->w = outlink->src->inputs[0]->h;
+    outlink->h = outlink->src->inputs[0]->w;
+
+    trans->nb_planes = 0;
+    for (i = 0; i < 4; i++) {
+        const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
+        trans->nb_planes = FFMAX(trans->nb_planes, comp->plane);
+    }
+    trans->nb_planes++;
+
+    av_image_fill_max_pixsteps(trans->pixsteps, NULL, pixdesc);
+
+    return 0;
+}
+
+static void end_frame(AVFilterLink *inlink)
+{
+    TransContext *trans = inlink->dst->priv;
+    AVFilterBufferRef *in  = inlink->cur_buf;
+    AVFilterBufferRef *out = inlink->dst->outputs[0]->out_buf;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    int i, j, plane;
+
+    for (plane = 0; plane < trans->nb_planes; plane++) {
+        int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
+        int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
+        int pixstep = trans->pixsteps[plane];
+        for (i = 0; i < out->video->h>>vsub; i++) {
+            uint8_t *pout = out->data[plane] + i * out->linesize[plane];
+            for (j = 0; j < out->video->w>>hsub; j++, pout += pixstep) {
+                uint8_t *pin = in ->data[plane] + j * in ->linesize[plane] + i * pixstep;
+                int32_t v;
+
+                switch (pixstep) {
+                case 1:
+                    *pout = *pin;
+                    break;
+                case 2:
+                    *((uint16_t *)pout) = *((uint16_t *)pin);
+                    break;
+                case 3:
+                    v = AV_RB24(pin);
+                    AV_WB24(pout, v);
+                    break;
+                case 4:
+                    *((uint32_t *)pout) = *((uint32_t *)pin);
+                    break;
+                }
+            }
+        }
+    }
+
+    avfilter_unref_buffer(in);
+    avfilter_draw_slice(outlink, 0, out->video->h, 1);
+    avfilter_end_frame(outlink);
+    avfilter_unref_buffer(out);
+}
+
+static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+{
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+
+    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) {
+        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));
+}
+
+AVFilter avfilter_vf_transpose = {
+    .name      = "transpose",
+    .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
+
+    .priv_size = sizeof(TransContext),
+
+    .query_formats = query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .start_frame     = start_frame,
+                                    .end_frame       = end_frame,
+                                    .config_props    = config_props_input,
+                                    .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


--YiEDa0DAkWCtVeE4--



More information about the ffmpeg-devel mailing list