[FFmpeg-soc] [soc]: r1794 - in libavfilter: Makefile allfilters.h avfilter.c vf_rotate.c

vitor subversion at mplayerhq.hu
Mon Jan 7 20:13:39 CET 2008


Author: vitor
Date: Mon Jan  7 20:13:38 2008
New Revision: 1794

Log:
Rotation filter

Added:
   libavfilter/vf_rotate.c
Modified:
   libavfilter/Makefile
   libavfilter/allfilters.h
   libavfilter/avfilter.c

Modified: libavfilter/Makefile
==============================================================================
--- libavfilter/Makefile	(original)
+++ libavfilter/Makefile	Mon Jan  7 20:13:38 2008
@@ -16,6 +16,7 @@ OBJS-yes = vf_crop.o \
            vf_negate.o \
            vf_format.o \
            vf_overlay.o \
+           vf_rotate.o \
            vf_scale.o \
            vf_slicify.o \
            vf_split.o \

Modified: libavfilter/allfilters.h
==============================================================================
--- libavfilter/allfilters.h	(original)
+++ libavfilter/allfilters.h	Mon Jan  7 20:13:38 2008
@@ -32,6 +32,7 @@ extern AVFilter avfilter_vf_hflip;
 extern AVFilter avfilter_vf_negate;
 extern AVFilter avfilter_vf_noformat;
 extern AVFilter avfilter_vf_overlay;
+extern AVFilter avfilter_vf_rotate;
 extern AVFilter avfilter_vf_scale;
 extern AVFilter avfilter_vf_slicify;
 extern AVFilter avfilter_vf_split;

Modified: libavfilter/avfilter.c
==============================================================================
--- libavfilter/avfilter.c	(original)
+++ libavfilter/avfilter.c	Mon Jan  7 20:13:38 2008
@@ -301,6 +301,7 @@ void avfilter_init(void)
     avfilter_register(&avfilter_vf_negate);
     avfilter_register(&avfilter_vf_noformat);
     avfilter_register(&avfilter_vf_overlay);
+    avfilter_register(&avfilter_vf_rotate);
     avfilter_register(&avfilter_vf_scale);
     avfilter_register(&avfilter_vf_slicify);
     avfilter_register(&avfilter_vf_split);

Added: libavfilter/vf_rotate.c
==============================================================================
--- (empty file)
+++ libavfilter/vf_rotate.c	Mon Jan  7 20:13:38 2008
@@ -0,0 +1,178 @@
+/*
+ * rotation filter
+ * 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 vf_rotate.c
+ * rotation filter
+ *
+ * @todo Copy code from rotozoom.c to remove use of floating-point
+ * @todo Handle packed pixel formats
+ * @todo Make backcolor configurable
+*/
+
+#include <math.h>
+#include "avfilter.h"
+
+typedef struct
+{
+    int ang;
+    int hsub, vsub;
+    float transx, transy; ///< How much to translate (in pixels)
+    float sinx, cosx;
+    int output_h, output_w;
+    int backcolor[3];
+} RotContext;
+
+static int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    RotContext *rot = ctx->priv;
+
+    /* default to 45 degrees */
+    rot->ang = 45;
+
+    if(args)
+        sscanf(args, "%d", &rot->ang);
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    avfilter_set_common_formats(ctx,
+        avfilter_make_format_list(10,
+                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));
+    return 0;
+}
+
+static int config_props_input(AVFilterLink *link)
+{
+    RotContext *rot = link->dst->priv;
+
+    avcodec_get_chroma_sub_sample(link->format, &rot->hsub, &rot->vsub);
+
+    rot->backcolor[0] = 16;
+    rot->backcolor[1] = 128;
+    rot->backcolor[2] = 128;
+    return 0;
+}
+
+static int config_props_output(AVFilterLink *link)
+{
+    RotContext *rot = link->src->priv;
+
+    rot->sinx = sin(rot->ang*M_PI/180.);
+    rot->cosx = cos(rot->ang*M_PI/180.);
+
+    rot->transx = FFMAX(0,  link->src->inputs[0]->h * rot->sinx) +
+        FFMAX(0, -link->src->inputs[0]->w*rot->cosx);
+
+    rot->transy = FFMAX(0, -link->src->inputs[0]->h * rot->cosx) +
+        FFMAX(0, -link->src->inputs[0]->w*rot->sinx);
+
+    rot->output_w = rot->transx + FFMAX(0, rot->cosx*link->src->inputs[0]->w) +
+        FFMAX(0, -rot->sinx*link->src->inputs[0]->h);
+
+    rot->output_h = rot->transy + FFMAX(0, rot->cosx*link->src->inputs[0]->h) +
+        FFMAX(0,  rot->sinx*link->src->inputs[0]->w);
+
+    link->w = rot->output_w;
+    link->h = rot->output_h;
+
+    return 0;
+}
+
+static void draw_slice(AVFilterLink *link, int y, int h)
+{
+    RotContext *rot = link->dst->priv;
+    AVFilterPicRef *in  = link->cur_pic;
+    AVFilterPicRef *out = link->dst->outputs[0]->outpic;
+    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 >= link->w || column < y || column >= 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 >= link->w || column < y || column >= 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_draw_slice(link->dst->outputs[0], y, h);
+}
+
+AVFilter avfilter_vf_rotate =
+{
+    .name      = "rotate",
+    .author    = "Vitor Sessak",
+
+    .init      = init,
+
+    .priv_size = sizeof(RotContext),
+
+    .query_formats = query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AV_PAD_VIDEO,
+                                    .draw_slice      = draw_slice,
+                                    .config_props    = config_props_input,
+                                    .min_perms       = AV_PERM_READ, },
+                                  { .name = NULL}},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .config_props    = config_props_output,
+                                    .type            = AV_PAD_VIDEO, },
+                                  { .name = NULL}},
+};
+



More information about the FFmpeg-soc mailing list