LCOV - code coverage report
Current view: top level - libavfilter - vf_transpose.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 85 159 53.5 %
Date: 2017-12-17 11:58:42 Functions: 6 17 35.3 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2010 Stefano Sabatini
       3             :  * Copyright (c) 2008 Vitor Sessak
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * transposition filter
      25             :  * Based on MPlayer libmpcodecs/vf_rotate.c.
      26             :  */
      27             : 
      28             : #include <stdio.h>
      29             : 
      30             : #include "libavutil/imgutils.h"
      31             : #include "libavutil/internal.h"
      32             : #include "libavutil/intreadwrite.h"
      33             : #include "libavutil/opt.h"
      34             : #include "libavutil/pixdesc.h"
      35             : 
      36             : #include "avfilter.h"
      37             : #include "formats.h"
      38             : #include "internal.h"
      39             : #include "video.h"
      40             : 
      41             : typedef enum {
      42             :     TRANSPOSE_PT_TYPE_NONE,
      43             :     TRANSPOSE_PT_TYPE_LANDSCAPE,
      44             :     TRANSPOSE_PT_TYPE_PORTRAIT,
      45             : } PassthroughType;
      46             : 
      47             : enum TransposeDir {
      48             :     TRANSPOSE_CCLOCK_FLIP,
      49             :     TRANSPOSE_CLOCK,
      50             :     TRANSPOSE_CCLOCK,
      51             :     TRANSPOSE_CLOCK_FLIP,
      52             : };
      53             : 
      54             : typedef struct TransContext {
      55             :     const AVClass *class;
      56             :     int hsub, vsub;
      57             :     int pixsteps[4];
      58             : 
      59             :     int passthrough;    ///< PassthroughType, landscape passthrough mode enabled
      60             :     int dir;            ///< TransposeDir
      61             : 
      62             :     void (*transpose_8x8)(uint8_t *src, ptrdiff_t src_linesize,
      63             :                           uint8_t *dst, ptrdiff_t dst_linesize);
      64             :     void (*transpose_block)(uint8_t *src, ptrdiff_t src_linesize,
      65             :                             uint8_t *dst, ptrdiff_t dst_linesize,
      66             :                             int w, int h);
      67             : } TransContext;
      68             : 
      69           1 : static int query_formats(AVFilterContext *ctx)
      70             : {
      71           1 :     AVFilterFormats *pix_fmts = NULL;
      72             :     int fmt, ret;
      73             : 
      74         184 :     for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
      75         183 :         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
      76         528 :         if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
      77         349 :               desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
      78         167 :               desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
      79         292 :               desc->log2_chroma_w != desc->log2_chroma_h) &&
      80         129 :             (ret = ff_add_format(&pix_fmts, fmt)) < 0)
      81           0 :             return ret;
      82             :     }
      83             : 
      84             : 
      85           1 :     return ff_set_common_formats(ctx, pix_fmts);
      86             : }
      87             : 
      88      100300 : static inline void transpose_block_8_c(uint8_t *src, ptrdiff_t src_linesize,
      89             :                                        uint8_t *dst, ptrdiff_t dst_linesize,
      90             :                                        int w, int h)
      91             : {
      92             :     int x, y;
      93      898300 :     for (y = 0; y < h; y++, dst += dst_linesize, src++)
      94     8401200 :         for (x = 0; x < w; x++)
      95     7603200 :             dst[x] = src[x*src_linesize];
      96      100300 : }
      97             : 
      98       99000 : static void transpose_8x8_8_c(uint8_t *src, ptrdiff_t src_linesize,
      99             :                               uint8_t *dst, ptrdiff_t dst_linesize)
     100             : {
     101       99000 :     transpose_block_8_c(src, src_linesize, dst, dst_linesize, 8, 8);
     102       99000 : }
     103             : 
     104           0 : static inline void transpose_block_16_c(uint8_t *src, ptrdiff_t src_linesize,
     105             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     106             :                                         int w, int h)
     107             : {
     108             :     int x, y;
     109           0 :     for (y = 0; y < h; y++, dst += dst_linesize, src += 2)
     110           0 :         for (x = 0; x < w; x++)
     111           0 :             *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*src_linesize));
     112           0 : }
     113             : 
     114           0 : static void transpose_8x8_16_c(uint8_t *src, ptrdiff_t src_linesize,
     115             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     116             : {
     117           0 :     transpose_block_16_c(src, src_linesize, dst, dst_linesize, 8, 8);
     118           0 : }
     119             : 
     120           0 : static inline void transpose_block_24_c(uint8_t *src, ptrdiff_t src_linesize,
     121             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     122             :                                         int w, int h)
     123             : {
     124             :     int x, y;
     125           0 :     for (y = 0; y < h; y++, dst += dst_linesize) {
     126           0 :         for (x = 0; x < w; x++) {
     127           0 :             int32_t v = AV_RB24(src + x*src_linesize + y*3);
     128           0 :             AV_WB24(dst + 3*x, v);
     129             :         }
     130             :     }
     131           0 : }
     132             : 
     133           0 : static void transpose_8x8_24_c(uint8_t *src, ptrdiff_t src_linesize,
     134             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     135             : {
     136           0 :     transpose_block_24_c(src, src_linesize, dst, dst_linesize, 8, 8);
     137           0 : }
     138             : 
     139           0 : static inline void transpose_block_32_c(uint8_t *src, ptrdiff_t src_linesize,
     140             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     141             :                                         int w, int h)
     142             : {
     143             :     int x, y;
     144           0 :     for (y = 0; y < h; y++, dst += dst_linesize, src += 4) {
     145           0 :         for (x = 0; x < w; x++)
     146           0 :             *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*src_linesize));
     147             :     }
     148           0 : }
     149             : 
     150           0 : static void transpose_8x8_32_c(uint8_t *src, ptrdiff_t src_linesize,
     151             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     152             : {
     153           0 :     transpose_block_32_c(src, src_linesize, dst, dst_linesize, 8, 8);
     154           0 : }
     155             : 
     156           0 : static inline void transpose_block_48_c(uint8_t *src, ptrdiff_t src_linesize,
     157             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     158             :                                         int w, int h)
     159             : {
     160             :     int x, y;
     161           0 :     for (y = 0; y < h; y++, dst += dst_linesize, src += 6) {
     162           0 :         for (x = 0; x < w; x++) {
     163           0 :             int64_t v = AV_RB48(src + x*src_linesize);
     164           0 :             AV_WB48(dst + 6*x, v);
     165             :         }
     166             :     }
     167           0 : }
     168             : 
     169           0 : static void transpose_8x8_48_c(uint8_t *src, ptrdiff_t src_linesize,
     170             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     171             : {
     172           0 :     transpose_block_48_c(src, src_linesize, dst, dst_linesize, 8, 8);
     173           0 : }
     174             : 
     175           0 : static inline void transpose_block_64_c(uint8_t *src, ptrdiff_t src_linesize,
     176             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     177             :                                         int w, int h)
     178             : {
     179             :     int x, y;
     180           0 :     for (y = 0; y < h; y++, dst += dst_linesize, src += 8)
     181           0 :         for (x = 0; x < w; x++)
     182           0 :             *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*src_linesize));
     183           0 : }
     184             : 
     185           0 : static void transpose_8x8_64_c(uint8_t *src, ptrdiff_t src_linesize,
     186             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     187             : {
     188           0 :     transpose_block_64_c(src, src_linesize, dst, dst_linesize, 8, 8);
     189           0 : }
     190             : 
     191           1 : static int config_props_output(AVFilterLink *outlink)
     192             : {
     193           1 :     AVFilterContext *ctx = outlink->src;
     194           1 :     TransContext *s = ctx->priv;
     195           1 :     AVFilterLink *inlink = ctx->inputs[0];
     196           1 :     const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
     197           1 :     const AVPixFmtDescriptor *desc_in  = av_pix_fmt_desc_get(inlink->format);
     198             : 
     199           1 :     if (s->dir&4) {
     200           0 :         av_log(ctx, AV_LOG_WARNING,
     201             :                "dir values greater than 3 are deprecated, use the passthrough option instead\n");
     202           0 :         s->dir &= 3;
     203           0 :         s->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
     204             :     }
     205             : 
     206           2 :     if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
     207           1 :         (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
     208           0 :         av_log(ctx, AV_LOG_VERBOSE,
     209             :                "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
     210             :                inlink->w, inlink->h, inlink->w, inlink->h);
     211           0 :         return 0;
     212             :     } else {
     213           1 :         s->passthrough = TRANSPOSE_PT_TYPE_NONE;
     214             :     }
     215             : 
     216           1 :     s->hsub = desc_in->log2_chroma_w;
     217           1 :     s->vsub = desc_in->log2_chroma_h;
     218             : 
     219           1 :     av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out);
     220             : 
     221           1 :     outlink->w = inlink->h;
     222           1 :     outlink->h = inlink->w;
     223             : 
     224           1 :     if (inlink->sample_aspect_ratio.num)
     225           0 :         outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
     226             :                                                 inlink->sample_aspect_ratio);
     227             :     else
     228           1 :         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
     229             : 
     230           1 :     switch (s->pixsteps[0]) {
     231           1 :     case 1: s->transpose_block = transpose_block_8_c;
     232           1 :             s->transpose_8x8   = transpose_8x8_8_c;  break;
     233           0 :     case 2: s->transpose_block = transpose_block_16_c;
     234           0 :             s->transpose_8x8   = transpose_8x8_16_c; break;
     235           0 :     case 3: s->transpose_block = transpose_block_24_c;
     236           0 :             s->transpose_8x8   = transpose_8x8_24_c; break;
     237           0 :     case 4: s->transpose_block = transpose_block_32_c;
     238           0 :             s->transpose_8x8   = transpose_8x8_32_c; break;
     239           0 :     case 6: s->transpose_block = transpose_block_48_c;
     240           0 :             s->transpose_8x8   = transpose_8x8_48_c; break;
     241           0 :     case 8: s->transpose_block = transpose_block_64_c;
     242           0 :             s->transpose_8x8   = transpose_8x8_64_c; break;
     243             :     }
     244             : 
     245           2 :     av_log(ctx, AV_LOG_VERBOSE,
     246             :            "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
     247             :            inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
     248           2 :            s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
     249           1 :            s->dir == 0 || s->dir == 3);
     250           1 :     return 0;
     251             : }
     252             : 
     253           0 : static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
     254             : {
     255           0 :     TransContext *s = inlink->dst->priv;
     256             : 
     257           0 :     return s->passthrough ?
     258           0 :         ff_null_get_video_buffer   (inlink, w, h) :
     259             :         ff_default_get_video_buffer(inlink, w, h);
     260             : }
     261             : 
     262             : typedef struct ThreadData {
     263             :     AVFrame *in, *out;
     264             : } ThreadData;
     265             : 
     266         450 : static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr,
     267             :                         int nb_jobs)
     268             : {
     269         450 :     TransContext *s = ctx->priv;
     270         450 :     ThreadData *td = arg;
     271         450 :     AVFrame *out = td->out;
     272         450 :     AVFrame *in = td->in;
     273             :     int plane;
     274             : 
     275        1800 :     for (plane = 0; out->data[plane]; plane++) {
     276        1350 :         int hsub    = plane == 1 || plane == 2 ? s->hsub : 0;
     277        1350 :         int vsub    = plane == 1 || plane == 2 ? s->vsub : 0;
     278        1350 :         int pixstep = s->pixsteps[plane];
     279        1350 :         int inh     = AV_CEIL_RSHIFT(in->height, vsub);
     280        1350 :         int outw    = AV_CEIL_RSHIFT(out->width,  hsub);
     281        1350 :         int outh    = AV_CEIL_RSHIFT(out->height, vsub);
     282        1350 :         int start   = (outh *  jobnr   ) / nb_jobs;
     283        1350 :         int end     = (outh * (jobnr+1)) / nb_jobs;
     284             :         uint8_t *dst, *src;
     285             :         int dstlinesize, srclinesize;
     286             :         int x, y;
     287             : 
     288        1350 :         dstlinesize = out->linesize[plane];
     289        1350 :         dst         = out->data[plane] + start * dstlinesize;
     290        1350 :         src         = in->data[plane];
     291        1350 :         srclinesize = in->linesize[plane];
     292             : 
     293        1350 :         if (s->dir & 1) {
     294           0 :             src         += in->linesize[plane] * (inh - 1);
     295           0 :             srclinesize *= -1;
     296             :         }
     297             : 
     298        1350 :         if (s->dir & 2) {
     299           0 :             dst          = out->data[plane] + dstlinesize * (outh - start - 1);
     300           0 :             dstlinesize *= -1;
     301             :         }
     302             : 
     303        5000 :         for (y = start; y < end - 7; y += 8) {
     304      102650 :             for (x = 0; x < outw - 7; x += 8) {
     305      198000 :                 s->transpose_8x8(src + x * srclinesize + y * pixstep,
     306             :                                  srclinesize,
     307       99000 :                                  dst + (y - start) * dstlinesize + x * pixstep,
     308             :                                  dstlinesize);
     309             :             }
     310        3650 :             if (outw - x > 0 && end - y > 0)
     311           0 :                 s->transpose_block(src + x * srclinesize + y * pixstep,
     312             :                                    srclinesize,
     313           0 :                                    dst + (y - start) * dstlinesize + x * pixstep,
     314             :                                    dstlinesize, outw - x, end - y);
     315             :         }
     316             : 
     317        1350 :         if (end - y > 0)
     318        2600 :             s->transpose_block(src + 0 * srclinesize + y * pixstep,
     319             :                                srclinesize,
     320        1300 :                                dst + (y - start) * dstlinesize + 0 * pixstep,
     321             :                                dstlinesize, outw, end - y);
     322             :     }
     323             : 
     324         450 :     return 0;
     325             : }
     326             : 
     327          50 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     328             : {
     329          50 :     AVFilterContext *ctx = inlink->dst;
     330          50 :     TransContext *s = ctx->priv;
     331          50 :     AVFilterLink *outlink = ctx->outputs[0];
     332             :     ThreadData td;
     333             :     AVFrame *out;
     334             : 
     335          50 :     if (s->passthrough)
     336           0 :         return ff_filter_frame(outlink, in);
     337             : 
     338          50 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     339          50 :     if (!out) {
     340           0 :         av_frame_free(&in);
     341           0 :         return AVERROR(ENOMEM);
     342             :     }
     343          50 :     av_frame_copy_props(out, in);
     344             : 
     345          50 :     if (in->sample_aspect_ratio.num == 0) {
     346          50 :         out->sample_aspect_ratio = in->sample_aspect_ratio;
     347             :     } else {
     348           0 :         out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
     349           0 :         out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
     350             :     }
     351             : 
     352          50 :     td.in = in, td.out = out;
     353          50 :     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
     354          50 :     av_frame_free(&in);
     355          50 :     return ff_filter_frame(outlink, out);
     356             : }
     357             : 
     358             : #define OFFSET(x) offsetof(TransContext, x)
     359             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
     360             : 
     361             : static const AVOption transpose_options[] = {
     362             :     { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, "dir" },
     363             :         { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
     364             :         { "clock",       "rotate clockwise",                            0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK       }, .flags=FLAGS, .unit = "dir" },
     365             :         { "cclock",      "rotate counter-clockwise",                    0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK      }, .flags=FLAGS, .unit = "dir" },
     366             :         { "clock_flip",  "rotate clockwise with vertical flip",         0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP  }, .flags=FLAGS, .unit = "dir" },
     367             : 
     368             :     { "passthrough", "do not apply transposition if the input matches the specified geometry",
     369             :       OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE},  0, INT_MAX, FLAGS, "passthrough" },
     370             :         { "none",      "always apply transposition",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE},      INT_MIN, INT_MAX, FLAGS, "passthrough" },
     371             :         { "portrait",  "preserve portrait geometry",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT},  INT_MIN, INT_MAX, FLAGS, "passthrough" },
     372             :         { "landscape", "preserve landscape geometry",  0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
     373             : 
     374             :     { NULL }
     375             : };
     376             : 
     377             : AVFILTER_DEFINE_CLASS(transpose);
     378             : 
     379             : static const AVFilterPad avfilter_vf_transpose_inputs[] = {
     380             :     {
     381             :         .name         = "default",
     382             :         .type         = AVMEDIA_TYPE_VIDEO,
     383             :         .get_video_buffer = get_video_buffer,
     384             :         .filter_frame = filter_frame,
     385             :     },
     386             :     { NULL }
     387             : };
     388             : 
     389             : static const AVFilterPad avfilter_vf_transpose_outputs[] = {
     390             :     {
     391             :         .name         = "default",
     392             :         .config_props = config_props_output,
     393             :         .type         = AVMEDIA_TYPE_VIDEO,
     394             :     },
     395             :     { NULL }
     396             : };
     397             : 
     398             : AVFilter ff_vf_transpose = {
     399             :     .name          = "transpose",
     400             :     .description   = NULL_IF_CONFIG_SMALL("Transpose input video."),
     401             :     .priv_size     = sizeof(TransContext),
     402             :     .priv_class    = &transpose_class,
     403             :     .query_formats = query_formats,
     404             :     .inputs        = avfilter_vf_transpose_inputs,
     405             :     .outputs       = avfilter_vf_transpose_outputs,
     406             :     .flags         = AVFILTER_FLAG_SLICE_THREADS,
     407             : };

Generated by: LCOV version 1.13