libavfilter/vf_transpose.c
43945b27
 /*
3fa77bde
  * Copyright (c) 2010 Stefano Sabatini
  * Copyright (c) 2008 Vitor Sessak
43945b27
  *
  * 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
  * Based on MPlayer libmpcodecs/vf_rotate.c.
  */
 
1d9c2dc8
 #include <stdio.h>
 
7ffe76e5
 #include "libavutil/imgutils.h"
1d9c2dc8
 #include "libavutil/internal.h"
6ee55c7b
 #include "libavutil/intreadwrite.h"
0c2466de
 #include "libavutil/opt.h"
6ee55c7b
 #include "libavutil/pixdesc.h"
 
43945b27
 #include "avfilter.h"
b74a1da4
 #include "formats.h"
9d0bfc50
 #include "internal.h"
803391f7
 #include "video.h"
43945b27
 
c053f486
 typedef enum {
     TRANSPOSE_PT_TYPE_NONE,
     TRANSPOSE_PT_TYPE_LANDSCAPE,
     TRANSPOSE_PT_TYPE_PORTRAIT,
 } PassthroughType;
 
0c2466de
 enum TransposeDir {
     TRANSPOSE_CCLOCK_FLIP,
     TRANSPOSE_CLOCK,
     TRANSPOSE_CCLOCK,
     TRANSPOSE_CLOCK_FLIP,
 };
 
58400ac1
 typedef struct TransContext {
63000627
     const AVClass *class;
43945b27
     int hsub, vsub;
     int pixsteps[4];
 
984c230c
     int passthrough;    ///< PassthroughType, landscape passthrough mode enabled
     int dir;            ///< TransposeDir
7b19e76a
 
     void (*transpose_8x8)(uint8_t *src, ptrdiff_t src_linesize,
                           uint8_t *dst, ptrdiff_t dst_linesize);
     void (*transpose_block)(uint8_t *src, ptrdiff_t src_linesize,
                             uint8_t *dst, ptrdiff_t dst_linesize,
                             int w, int h);
43945b27
 } TransContext;
 
 static int query_formats(AVFilterContext *ctx)
 {
34126cd0
     AVFilterFormats *pix_fmts = NULL;
6aaac24d
     int fmt, ret;
34126cd0
 
515e8aed
     for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
34126cd0
         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
c7c71f95
         if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
               desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
               desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
6aaac24d
               desc->log2_chroma_w != desc->log2_chroma_h) &&
             (ret = ff_add_format(&pix_fmts, fmt)) < 0)
             return ret;
34126cd0
     }
 
 
a0854c08
     return ff_set_common_formats(ctx, pix_fmts);
43945b27
 }
 
7b19e76a
 static inline void transpose_block_8_c(uint8_t *src, ptrdiff_t src_linesize,
                                        uint8_t *dst, ptrdiff_t dst_linesize,
                                        int w, int h)
 {
     int x, y;
     for (y = 0; y < h; y++, dst += dst_linesize, src++)
         for (x = 0; x < w; x++)
             dst[x] = src[x*src_linesize];
 }
 
 static void transpose_8x8_8_c(uint8_t *src, ptrdiff_t src_linesize,
                               uint8_t *dst, ptrdiff_t dst_linesize)
 {
     transpose_block_8_c(src, src_linesize, dst, dst_linesize, 8, 8);
 }
 
 static inline void transpose_block_16_c(uint8_t *src, ptrdiff_t src_linesize,
                                         uint8_t *dst, ptrdiff_t dst_linesize,
                                         int w, int h)
 {
     int x, y;
     for (y = 0; y < h; y++, dst += dst_linesize, src += 2)
         for (x = 0; x < w; x++)
             *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*src_linesize));
 }
 
 static void transpose_8x8_16_c(uint8_t *src, ptrdiff_t src_linesize,
                                uint8_t *dst, ptrdiff_t dst_linesize)
 {
     transpose_block_16_c(src, src_linesize, dst, dst_linesize, 8, 8);
 }
 
 static inline void transpose_block_24_c(uint8_t *src, ptrdiff_t src_linesize,
                                         uint8_t *dst, ptrdiff_t dst_linesize,
                                         int w, int h)
 {
     int x, y;
     for (y = 0; y < h; y++, dst += dst_linesize) {
         for (x = 0; x < w; x++) {
             int32_t v = AV_RB24(src + x*src_linesize + y*3);
             AV_WB24(dst + 3*x, v);
         }
     }
 }
 
 static void transpose_8x8_24_c(uint8_t *src, ptrdiff_t src_linesize,
                                uint8_t *dst, ptrdiff_t dst_linesize)
 {
     transpose_block_24_c(src, src_linesize, dst, dst_linesize, 8, 8);
 }
 
 static inline void transpose_block_32_c(uint8_t *src, ptrdiff_t src_linesize,
                                         uint8_t *dst, ptrdiff_t dst_linesize,
                                         int w, int h)
 {
     int x, y;
     for (y = 0; y < h; y++, dst += dst_linesize, src += 4) {
         for (x = 0; x < w; x++)
             *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*src_linesize));
     }
 }
 
 static void transpose_8x8_32_c(uint8_t *src, ptrdiff_t src_linesize,
                                uint8_t *dst, ptrdiff_t dst_linesize)
 {
     transpose_block_32_c(src, src_linesize, dst, dst_linesize, 8, 8);
 }
 
 static inline void transpose_block_48_c(uint8_t *src, ptrdiff_t src_linesize,
                                         uint8_t *dst, ptrdiff_t dst_linesize,
                                         int w, int h)
 {
     int x, y;
     for (y = 0; y < h; y++, dst += dst_linesize, src += 6) {
         for (x = 0; x < w; x++) {
             int64_t v = AV_RB48(src + x*src_linesize);
             AV_WB48(dst + 6*x, v);
         }
     }
 }
 
 static void transpose_8x8_48_c(uint8_t *src, ptrdiff_t src_linesize,
                                uint8_t *dst, ptrdiff_t dst_linesize)
 {
     transpose_block_48_c(src, src_linesize, dst, dst_linesize, 8, 8);
 }
 
 static inline void transpose_block_64_c(uint8_t *src, ptrdiff_t src_linesize,
                                         uint8_t *dst, ptrdiff_t dst_linesize,
                                         int w, int h)
 {
     int x, y;
     for (y = 0; y < h; y++, dst += dst_linesize, src += 8)
         for (x = 0; x < w; x++)
             *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*src_linesize));
 }
 
 static void transpose_8x8_64_c(uint8_t *src, ptrdiff_t src_linesize,
                                uint8_t *dst, ptrdiff_t dst_linesize)
 {
     transpose_block_64_c(src, src_linesize, dst, dst_linesize, 8, 8);
 }
 
43945b27
 static int config_props_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
f6e598af
     TransContext *s = ctx->priv;
43945b27
     AVFilterLink *inlink = ctx->inputs[0];
59ee9f78
     const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
     const AVPixFmtDescriptor *desc_in  = av_pix_fmt_desc_get(inlink->format);
43945b27
 
f6e598af
     if (s->dir&4) {
c053f486
         av_log(ctx, AV_LOG_WARNING,
                "dir values greater than 3 are deprecated, use the passthrough option instead\n");
f6e598af
         s->dir &= 3;
         s->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
c053f486
     }
 
f6e598af
     if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
         (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
c053f486
         av_log(ctx, AV_LOG_VERBOSE,
                "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
                inlink->w, inlink->h, inlink->w, inlink->h);
         return 0;
     } else {
f6e598af
         s->passthrough = TRANSPOSE_PT_TYPE_NONE;
9de76229
     }
 
f6e598af
     s->hsub = desc_in->log2_chroma_w;
     s->vsub = desc_in->log2_chroma_h;
43945b27
 
f6e598af
     av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out);
43945b27
 
     outlink->w = inlink->h;
     outlink->h = inlink->w;
 
6ee55c7b
     if (inlink->sample_aspect_ratio.num)
         outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
                                                 inlink->sample_aspect_ratio);
     else
14b171cd
         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
 
7b19e76a
     switch (s->pixsteps[0]) {
     case 1: s->transpose_block = transpose_block_8_c;
             s->transpose_8x8   = transpose_8x8_8_c;  break;
     case 2: s->transpose_block = transpose_block_16_c;
             s->transpose_8x8   = transpose_8x8_16_c; break;
     case 3: s->transpose_block = transpose_block_24_c;
             s->transpose_8x8   = transpose_8x8_24_c; break;
     case 4: s->transpose_block = transpose_block_32_c;
             s->transpose_8x8   = transpose_8x8_32_c; break;
     case 6: s->transpose_block = transpose_block_48_c;
             s->transpose_8x8   = transpose_8x8_48_c; break;
     case 8: s->transpose_block = transpose_block_64_c;
             s->transpose_8x8   = transpose_8x8_64_c; break;
     }
 
6ee55c7b
     av_log(ctx, AV_LOG_VERBOSE,
            "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
f6e598af
            inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
            s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
            s->dir == 0 || s->dir == 3);
43945b27
     return 0;
 }
 
a05a44e2
 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
9de76229
 {
f6e598af
     TransContext *s = inlink->dst->priv;
9de76229
 
f6e598af
     return s->passthrough ?
a05a44e2
         ff_null_get_video_buffer   (inlink, w, h) :
         ff_default_get_video_buffer(inlink, w, h);
9de76229
 }
 
e74a5acb
 typedef struct ThreadData {
     AVFrame *in, *out;
 } ThreadData;
 
 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr,
                         int nb_jobs)
43945b27
 {
f6e598af
     TransContext *s = ctx->priv;
e74a5acb
     ThreadData *td = arg;
     AVFrame *out = td->out;
     AVFrame *in = td->in;
afded181
     int plane;
43945b27
 
afded181
     for (plane = 0; out->data[plane]; plane++) {
f6e598af
         int hsub    = plane == 1 || plane == 2 ? s->hsub : 0;
         int vsub    = plane == 1 || plane == 2 ? s->vsub : 0;
         int pixstep = s->pixsteps[plane];
21f94684
         int inh     = AV_CEIL_RSHIFT(in->height, vsub);
         int outw    = AV_CEIL_RSHIFT(out->width,  hsub);
         int outh    = AV_CEIL_RSHIFT(out->height, vsub);
10e931d5
         int start   = (outh *  jobnr   ) / nb_jobs;
         int end     = (outh * (jobnr+1)) / nb_jobs;
afded181
         uint8_t *dst, *src;
         int dstlinesize, srclinesize;
43945b27
         int x, y;
 
afded181
         dstlinesize = out->linesize[plane];
10e931d5
         dst         = out->data[plane] + start * dstlinesize;
6ee55c7b
         src         = in->data[plane];
afded181
         srclinesize = in->linesize[plane];
43945b27
 
f6e598af
         if (s->dir & 1) {
6ee55c7b
             src         += in->linesize[plane] * (inh - 1);
afded181
             srclinesize *= -1;
43945b27
         }
 
f6e598af
         if (s->dir & 2) {
10e931d5
             dst          = out->data[plane] + dstlinesize * (outh - start - 1);
afded181
             dstlinesize *= -1;
43945b27
         }
 
7b19e76a
         for (y = start; y < end - 7; y += 8) {
             for (x = 0; x < outw - 7; x += 8) {
                 s->transpose_8x8(src + x * srclinesize + y * pixstep,
                                  srclinesize,
                                  dst + (y - start) * dstlinesize + x * pixstep,
                                  dstlinesize);
99a28333
             }
7b19e76a
             if (outw - x > 0 && end - y > 0)
                 s->transpose_block(src + x * srclinesize + y * pixstep,
                                    srclinesize,
                                    dst + (y - start) * dstlinesize + x * pixstep,
                                    dstlinesize, outw - x, end - y);
43945b27
         }
7b19e76a
 
         if (end - y > 0)
             s->transpose_block(src + 0 * srclinesize + y * pixstep,
                                srclinesize,
                                dst + (y - start) * dstlinesize + 0 * pixstep,
                                dstlinesize, outw, end - y);
43945b27
     }
 
e74a5acb
     return 0;
 }
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx = inlink->dst;
f6e598af
     TransContext *s = ctx->priv;
e74a5acb
     AVFilterLink *outlink = ctx->outputs[0];
     ThreadData td;
     AVFrame *out;
 
f6e598af
     if (s->passthrough)
e74a5acb
         return ff_filter_frame(outlink, in);
 
     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
         av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
66f1de66
     av_frame_copy_props(out, in);
e74a5acb
 
     if (in->sample_aspect_ratio.num == 0) {
         out->sample_aspect_ratio = in->sample_aspect_ratio;
     } else {
         out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
         out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
     }
 
     td.in = in, td.out = out;
a0a57072
     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
7e350379
     av_frame_free(&in);
afded181
     return ff_filter_frame(outlink, out);
9de76229
 }
d9c23a0d
 
0c2466de
 #define OFFSET(x) offsetof(TransContext, x)
a4e0defa
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption transpose_options[] = {
75070d9f
     { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, "dir" },
571a3601
         { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
         { "clock",       "rotate clockwise",                            0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK       }, .flags=FLAGS, .unit = "dir" },
         { "cclock",      "rotate counter-clockwise",                    0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK      }, .flags=FLAGS, .unit = "dir" },
         { "clock_flip",  "rotate clockwise with vertical flip",         0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP  }, .flags=FLAGS, .unit = "dir" },
a4e0defa
 
     { "passthrough", "do not apply transposition if the input matches the specified geometry",
       OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE},  0, INT_MAX, FLAGS, "passthrough" },
028c7335
         { "none",      "always apply transposition",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE},      INT_MIN, INT_MAX, FLAGS, "passthrough" },
         { "portrait",  "preserve portrait geometry",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT},  INT_MIN, INT_MAX, FLAGS, "passthrough" },
         { "landscape", "preserve landscape geometry",  0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
a4e0defa
 
b211607b
     { NULL }
0c2466de
 };
 
a4e0defa
 AVFILTER_DEFINE_CLASS(transpose);
0c2466de
 
568c70e7
 static const AVFilterPad avfilter_vf_transpose_inputs[] = {
     {
6ee55c7b
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
b211607b
         .get_video_buffer = get_video_buffer,
565e4993
         .filter_frame = filter_frame,
568c70e7
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_vf_transpose_outputs[] = {
     {
         .name         = "default",
         .config_props = config_props_output,
         .type         = AVMEDIA_TYPE_VIDEO,
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_transpose = {
b211607b
     .name          = "transpose",
     .description   = NULL_IF_CONFIG_SMALL("Transpose input video."),
     .priv_size     = sizeof(TransContext),
     .priv_class    = &transpose_class,
43945b27
     .query_formats = query_formats,
b211607b
     .inputs        = avfilter_vf_transpose_inputs,
     .outputs       = avfilter_vf_transpose_outputs,
     .flags         = AVFILTER_FLAG_SLICE_THREADS,
43945b27
 };