[FFmpeg-devel] [PATCH 6/6] Add rotate90 filter.

Stefano Sabatini stefano.sabatini-lala
Fri Oct 15 01:13:59 CEST 2010


---
 doc/filters.texi           |    6 ++
 libavfilter/Makefile       |    1 +
 libavfilter/allfilters.c   |    1 +
 libavfilter/vf_transpose.c |  173 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 181 insertions(+), 0 deletions(-)

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 38d2762..a2d42c6 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)               += vf_transpose.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_transpose.c b/libavfilter/vf_transpose.c
index a14fadd..166ee53 100644
--- a/libavfilter/vf_transpose.c
+++ b/libavfilter/vf_transpose.c
@@ -115,6 +115,8 @@ static int query_formats(AVFilterContext *ctx)
     return 0;
 }
 
+#if CONFIG_TRANSPOSE_FILTER
+
 typedef struct {
     int hsub, vsub;
     int nb_planes;
@@ -227,3 +229,174 @@ AVFilter avfilter_vf_transpose = {
                                     .type            = AVMEDIA_TYPE_VIDEO, },
                                   { .name = NULL}},
 };
+
+#endif /* CONFIG_TRANSPOSE_FILTER */
+
+#if CONFIG_ROTATE90_FILTER
+
+typedef struct {
+    int angle;
+    int hsub, vsub;
+    int pixsteps[4];
+} Rotate90Context;
+
+static void invert(uint8_t *dst[4], int dst_linesizes[4],
+                   uint8_t *src[4], int src_linesizes[4],
+                   int dstw, int dsth,
+                   int pixsteps[4], int hsub, int vsub)
+{
+    int plane;
+
+    for (plane = 0; plane < 4 && dst[plane]; plane++) {
+        int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
+        int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
+        int pixstep = pixsteps[plane];
+        int outw = dstw>>hsub1;
+        int outh = dsth>>vsub1;
+        uint8_t *out, *in;
+        int outlinesize, inlinesize;
+        int x, y;
+
+        out = dst[plane]; outlinesize = dst_linesizes[plane];
+        in  = src[plane]; inlinesize  = src_linesizes[plane];
+
+        for (y = 0; y < outh; y++) {
+            for (x = 0; x < outw; x++) {
+                int32_t v;
+                int x1 = outw -1 -x;
+                int y1 = outh -1 -y;
+
+                switch (pixstep) {
+                case 1:
+                    *(out + x) = *(in + y1*inlinesize + x1);
+                    break;
+                case 2:
+                    *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + y1*inlinesize + x1*2));
+                    break;
+                case 3:
+                    v = AV_RB24(in + y1*inlinesize + x1*3);
+                    AV_WB24(out + 3*x, v);
+                    break;
+                case 4:
+                    *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + y1*inlinesize + x1*4));
+                    break;
+                }
+            }
+            out += outlinesize;
+        }
+    }
+}
+
+static av_cold int rot90_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 rot90_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 rot90_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 rot90_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) {
+        transpose(outpic->data, outpic->linesize, inpic->data, inpic->linesize,
+                  outpic->video->w, outpic->video->h, rot90->pixsteps,
+                  rot90->hsub, rot90->vsub, rot90->angle == 90 ? 1 : 2);
+    } 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 = rot90_init,
+
+    .priv_size = sizeof(Rotate90Context),
+    .query_formats = query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .start_frame      = rot90_start_frame,
+                                    .end_frame        = rot90_end_frame },
+                                  { .name = NULL}},
+
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .config_props     = rot90_config_output_props },
+                                  { .name = NULL}},
+};
+
+#endif /* CONFIG_ROTATE90_FILTER */
-- 
1.7.1




More information about the ffmpeg-devel mailing list