LCOV - code coverage report
Current view: top level - libavfilter - vf_transpose.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 146 164 89.0 %
Date: 2018-05-20 11:54:08 Functions: 17 17 100.0 %

          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/avassert.h"
      31             : #include "libavutil/imgutils.h"
      32             : #include "libavutil/internal.h"
      33             : #include "libavutil/intreadwrite.h"
      34             : #include "libavutil/opt.h"
      35             : #include "libavutil/pixdesc.h"
      36             : 
      37             : #include "avfilter.h"
      38             : #include "formats.h"
      39             : #include "internal.h"
      40             : #include "video.h"
      41             : 
      42             : typedef enum {
      43             :     TRANSPOSE_PT_TYPE_NONE,
      44             :     TRANSPOSE_PT_TYPE_LANDSCAPE,
      45             :     TRANSPOSE_PT_TYPE_PORTRAIT,
      46             : } PassthroughType;
      47             : 
      48             : enum TransposeDir {
      49             :     TRANSPOSE_CCLOCK_FLIP,
      50             :     TRANSPOSE_CLOCK,
      51             :     TRANSPOSE_CCLOCK,
      52             :     TRANSPOSE_CLOCK_FLIP,
      53             : };
      54             : 
      55             : typedef struct TransVtable {
      56             :     void (*transpose_8x8)(uint8_t *src, ptrdiff_t src_linesize,
      57             :                           uint8_t *dst, ptrdiff_t dst_linesize);
      58             :     void (*transpose_block)(uint8_t *src, ptrdiff_t src_linesize,
      59             :                             uint8_t *dst, ptrdiff_t dst_linesize,
      60             :                             int w, int h);
      61             : } TransVtable;
      62             : 
      63             : typedef struct TransContext {
      64             :     const AVClass *class;
      65             :     int hsub, vsub;
      66             :     int planes;
      67             :     int pixsteps[4];
      68             : 
      69             :     int passthrough;    ///< PassthroughType, landscape passthrough mode enabled
      70             :     int dir;            ///< TransposeDir
      71             : 
      72             :     TransVtable vtables[4];
      73             : } TransContext;
      74             : 
      75         114 : static int query_formats(AVFilterContext *ctx)
      76             : {
      77         114 :     AVFilterFormats *pix_fmts = NULL;
      78             :     int fmt, ret;
      79             : 
      80       21204 :     for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
      81       21090 :         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
      82       60876 :         if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
      83       40242 :               desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
      84       19266 :               desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
      85       33744 :               desc->log2_chroma_w != desc->log2_chroma_h) &&
      86       14934 :             (ret = ff_add_format(&pix_fmts, fmt)) < 0)
      87           0 :             return ret;
      88             :     }
      89             : 
      90             : 
      91         114 :     return ff_set_common_formats(ctx, pix_fmts);
      92             : }
      93             : 
      94      148810 : static inline void transpose_block_8_c(uint8_t *src, ptrdiff_t src_linesize,
      95             :                                        uint8_t *dst, ptrdiff_t dst_linesize,
      96             :                                        int w, int h)
      97             : {
      98             :     int x, y;
      99     1334890 :     for (y = 0; y < h; y++, dst += dst_linesize, src++)
     100    11893920 :         for (x = 0; x < w; x++)
     101    10707840 :             dst[x] = src[x*src_linesize];
     102      148810 : }
     103             : 
     104      147510 : static void transpose_8x8_8_c(uint8_t *src, ptrdiff_t src_linesize,
     105             :                               uint8_t *dst, ptrdiff_t dst_linesize)
     106             : {
     107      147510 :     transpose_block_8_c(src, src_linesize, dst, dst_linesize, 8, 8);
     108      147510 : }
     109             : 
     110      262152 : static inline void transpose_block_16_c(uint8_t *src, ptrdiff_t src_linesize,
     111             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     112             :                                         int w, int h)
     113             : {
     114             :     int x, y;
     115     2359368 :     for (y = 0; y < h; y++, dst += dst_linesize, src += 2)
     116    18874944 :         for (x = 0; x < w; x++)
     117    16777728 :             *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*src_linesize));
     118      262152 : }
     119             : 
     120      262152 : static void transpose_8x8_16_c(uint8_t *src, ptrdiff_t src_linesize,
     121             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     122             : {
     123      262152 :     transpose_block_16_c(src, src_linesize, dst, dst_linesize, 8, 8);
     124      262152 : }
     125             : 
     126        3168 : static inline void transpose_block_24_c(uint8_t *src, ptrdiff_t src_linesize,
     127             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     128             :                                         int w, int h)
     129             : {
     130             :     int x, y;
     131       28512 :     for (y = 0; y < h; y++, dst += dst_linesize) {
     132      228096 :         for (x = 0; x < w; x++) {
     133      202752 :             int32_t v = AV_RB24(src + x*src_linesize + y*3);
     134      202752 :             AV_WB24(dst + 3*x, v);
     135             :         }
     136             :     }
     137        3168 : }
     138             : 
     139        3168 : static void transpose_8x8_24_c(uint8_t *src, ptrdiff_t src_linesize,
     140             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     141             : {
     142        3168 :     transpose_block_24_c(src, src_linesize, dst, dst_linesize, 8, 8);
     143        3168 : }
     144             : 
     145       14256 : static inline void transpose_block_32_c(uint8_t *src, ptrdiff_t src_linesize,
     146             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     147             :                                         int w, int h)
     148             : {
     149             :     int x, y;
     150      128304 :     for (y = 0; y < h; y++, dst += dst_linesize, src += 4) {
     151     1026432 :         for (x = 0; x < w; x++)
     152      912384 :             *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*src_linesize));
     153             :     }
     154       14256 : }
     155             : 
     156       14256 : static void transpose_8x8_32_c(uint8_t *src, ptrdiff_t src_linesize,
     157             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     158             : {
     159       14256 :     transpose_block_32_c(src, src_linesize, dst, dst_linesize, 8, 8);
     160       14256 : }
     161             : 
     162        9504 : static inline void transpose_block_48_c(uint8_t *src, ptrdiff_t src_linesize,
     163             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     164             :                                         int w, int h)
     165             : {
     166             :     int x, y;
     167       85536 :     for (y = 0; y < h; y++, dst += dst_linesize, src += 6) {
     168      684288 :         for (x = 0; x < w; x++) {
     169      608256 :             int64_t v = AV_RB48(src + x*src_linesize);
     170      608256 :             AV_WB48(dst + 6*x, v);
     171             :         }
     172             :     }
     173        9504 : }
     174             : 
     175        9504 : static void transpose_8x8_48_c(uint8_t *src, ptrdiff_t src_linesize,
     176             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     177             : {
     178        9504 :     transpose_block_48_c(src, src_linesize, dst, dst_linesize, 8, 8);
     179        9504 : }
     180             : 
     181        7920 : static inline void transpose_block_64_c(uint8_t *src, ptrdiff_t src_linesize,
     182             :                                         uint8_t *dst, ptrdiff_t dst_linesize,
     183             :                                         int w, int h)
     184             : {
     185             :     int x, y;
     186       71280 :     for (y = 0; y < h; y++, dst += dst_linesize, src += 8)
     187      570240 :         for (x = 0; x < w; x++)
     188      506880 :             *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*src_linesize));
     189        7920 : }
     190             : 
     191        7920 : static void transpose_8x8_64_c(uint8_t *src, ptrdiff_t src_linesize,
     192             :                                uint8_t *dst, ptrdiff_t dst_linesize)
     193             : {
     194        7920 :     transpose_block_64_c(src, src_linesize, dst, dst_linesize, 8, 8);
     195        7920 : }
     196             : 
     197         113 : static int config_props_output(AVFilterLink *outlink)
     198             : {
     199         113 :     AVFilterContext *ctx = outlink->src;
     200         113 :     TransContext *s = ctx->priv;
     201         113 :     AVFilterLink *inlink = ctx->inputs[0];
     202         113 :     const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
     203         113 :     const AVPixFmtDescriptor *desc_in  = av_pix_fmt_desc_get(inlink->format);
     204             : 
     205         113 :     if (s->dir&4) {
     206           0 :         av_log(ctx, AV_LOG_WARNING,
     207             :                "dir values greater than 3 are deprecated, use the passthrough option instead\n");
     208           0 :         s->dir &= 3;
     209           0 :         s->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
     210             :     }
     211             : 
     212         226 :     if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
     213         113 :         (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
     214           0 :         av_log(ctx, AV_LOG_VERBOSE,
     215             :                "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
     216             :                inlink->w, inlink->h, inlink->w, inlink->h);
     217           0 :         return 0;
     218             :     } else {
     219         113 :         s->passthrough = TRANSPOSE_PT_TYPE_NONE;
     220             :     }
     221             : 
     222         113 :     s->hsub = desc_in->log2_chroma_w;
     223         113 :     s->vsub = desc_in->log2_chroma_h;
     224         113 :     s->planes = av_pix_fmt_count_planes(outlink->format);
     225             : 
     226         113 :     av_assert0(desc_in->nb_components == desc_out->nb_components);
     227             : 
     228             : 
     229         113 :     av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out);
     230             : 
     231         113 :     outlink->w = inlink->h;
     232         113 :     outlink->h = inlink->w;
     233             : 
     234         113 :     if (inlink->sample_aspect_ratio.num)
     235           0 :         outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
     236             :                                                 inlink->sample_aspect_ratio);
     237             :     else
     238         113 :         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
     239             : 
     240         565 :     for (int i = 0; i < 4; i++) {
     241         452 :         TransVtable *v = &s->vtables[i];
     242         452 :         switch (s->pixsteps[i]) {
     243          40 :         case 1: v->transpose_block = transpose_block_8_c;
     244          40 :                 v->transpose_8x8   = transpose_8x8_8_c;  break;
     245         191 :         case 2: v->transpose_block = transpose_block_16_c;
     246         191 :                 v->transpose_8x8   = transpose_8x8_16_c; break;
     247           2 :         case 3: v->transpose_block = transpose_block_24_c;
     248           2 :                 v->transpose_8x8   = transpose_8x8_24_c; break;
     249          12 :         case 4: v->transpose_block = transpose_block_32_c;
     250          12 :                 v->transpose_8x8   = transpose_8x8_32_c; break;
     251           6 :         case 6: v->transpose_block = transpose_block_48_c;
     252           6 :                 v->transpose_8x8   = transpose_8x8_48_c; break;
     253           5 :         case 8: v->transpose_block = transpose_block_64_c;
     254           5 :                 v->transpose_8x8   = transpose_8x8_64_c; break;
     255             :         }
     256             :     }
     257             : 
     258         226 :     av_log(ctx, AV_LOG_VERBOSE,
     259             :            "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
     260             :            inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
     261         226 :            s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
     262         113 :            s->dir == 0 || s->dir == 3);
     263         113 :     return 0;
     264             : }
     265             : 
     266         111 : static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
     267             : {
     268         111 :     TransContext *s = inlink->dst->priv;
     269             : 
     270         111 :     return s->passthrough ?
     271         111 :         ff_null_get_video_buffer   (inlink, w, h) :
     272             :         ff_default_get_video_buffer(inlink, w, h);
     273             : }
     274             : 
     275             : typedef struct ThreadData {
     276             :     AVFrame *in, *out;
     277             : } ThreadData;
     278             : 
     279         562 : static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr,
     280             :                         int nb_jobs)
     281             : {
     282         562 :     TransContext *s = ctx->priv;
     283         562 :     ThreadData *td = arg;
     284         562 :     AVFrame *out = td->out;
     285         562 :     AVFrame *in = td->in;
     286             :     int plane;
     287             : 
     288        2165 :     for (plane = 0; plane < s->planes; plane++) {
     289        1603 :         int hsub    = plane == 1 || plane == 2 ? s->hsub : 0;
     290        1603 :         int vsub    = plane == 1 || plane == 2 ? s->vsub : 0;
     291        1603 :         int pixstep = s->pixsteps[plane];
     292        1603 :         int inh     = AV_CEIL_RSHIFT(in->height, vsub);
     293        1603 :         int outw    = AV_CEIL_RSHIFT(out->width,  hsub);
     294        1603 :         int outh    = AV_CEIL_RSHIFT(out->height, vsub);
     295        1603 :         int start   = (outh *  jobnr   ) / nb_jobs;
     296        1603 :         int end     = (outh * (jobnr+1)) / nb_jobs;
     297             :         uint8_t *dst, *src;
     298             :         int dstlinesize, srclinesize;
     299             :         int x, y;
     300        1603 :         TransVtable *v = &s->vtables[plane];
     301             : 
     302        1603 :         dstlinesize = out->linesize[plane];
     303        1603 :         dst         = out->data[plane] + start * dstlinesize;
     304        1603 :         src         = in->data[plane];
     305        1603 :         srclinesize = in->linesize[plane];
     306             : 
     307        1603 :         if (s->dir & 1) {
     308           0 :             src         += in->linesize[plane] * (inh - 1);
     309           0 :             srclinesize *= -1;
     310             :         }
     311             : 
     312        1603 :         if (s->dir & 2) {
     313           0 :             dst          = out->data[plane] + dstlinesize * (outh - start - 1);
     314           0 :             dstlinesize *= -1;
     315             :         }
     316             : 
     317       15351 :         for (y = start; y < end - 7; y += 8) {
     318      458258 :             for (x = 0; x < outw - 7; x += 8) {
     319      889020 :                 v->transpose_8x8(src + x * srclinesize + y * pixstep,
     320             :                                  srclinesize,
     321      444510 :                                  dst + (y - start) * dstlinesize + x * pixstep,
     322             :                                  dstlinesize);
     323             :             }
     324       13748 :             if (outw - x > 0 && end - y > 0)
     325           0 :                 v->transpose_block(src + x * srclinesize + y * pixstep,
     326             :                                    srclinesize,
     327           0 :                                    dst + (y - start) * dstlinesize + x * pixstep,
     328             :                                    dstlinesize, outw - x, end - y);
     329             :         }
     330             : 
     331        1603 :         if (end - y > 0)
     332        2600 :             v->transpose_block(src + 0 * srclinesize + y * pixstep,
     333             :                                srclinesize,
     334        1300 :                                dst + (y - start) * dstlinesize + 0 * pixstep,
     335             :                                dstlinesize, outw, end - y);
     336             :     }
     337             : 
     338         562 :     return 0;
     339             : }
     340             : 
     341         162 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     342             : {
     343         162 :     AVFilterContext *ctx = inlink->dst;
     344         162 :     TransContext *s = ctx->priv;
     345         162 :     AVFilterLink *outlink = ctx->outputs[0];
     346             :     ThreadData td;
     347             :     AVFrame *out;
     348             : 
     349         162 :     if (s->passthrough)
     350           0 :         return ff_filter_frame(outlink, in);
     351             : 
     352         162 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     353         162 :     if (!out) {
     354           0 :         av_frame_free(&in);
     355           0 :         return AVERROR(ENOMEM);
     356             :     }
     357         162 :     av_frame_copy_props(out, in);
     358             : 
     359         162 :     if (in->sample_aspect_ratio.num == 0) {
     360         162 :         out->sample_aspect_ratio = in->sample_aspect_ratio;
     361             :     } else {
     362           0 :         out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
     363           0 :         out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
     364             :     }
     365             : 
     366         162 :     td.in = in, td.out = out;
     367         162 :     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
     368         162 :     av_frame_free(&in);
     369         162 :     return ff_filter_frame(outlink, out);
     370             : }
     371             : 
     372             : #define OFFSET(x) offsetof(TransContext, x)
     373             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
     374             : 
     375             : static const AVOption transpose_options[] = {
     376             :     { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, "dir" },
     377             :         { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
     378             :         { "clock",       "rotate clockwise",                            0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK       }, .flags=FLAGS, .unit = "dir" },
     379             :         { "cclock",      "rotate counter-clockwise",                    0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK      }, .flags=FLAGS, .unit = "dir" },
     380             :         { "clock_flip",  "rotate clockwise with vertical flip",         0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP  }, .flags=FLAGS, .unit = "dir" },
     381             : 
     382             :     { "passthrough", "do not apply transposition if the input matches the specified geometry",
     383             :       OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE},  0, INT_MAX, FLAGS, "passthrough" },
     384             :         { "none",      "always apply transposition",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE},      INT_MIN, INT_MAX, FLAGS, "passthrough" },
     385             :         { "portrait",  "preserve portrait geometry",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT},  INT_MIN, INT_MAX, FLAGS, "passthrough" },
     386             :         { "landscape", "preserve landscape geometry",  0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
     387             : 
     388             :     { NULL }
     389             : };
     390             : 
     391             : AVFILTER_DEFINE_CLASS(transpose);
     392             : 
     393             : static const AVFilterPad avfilter_vf_transpose_inputs[] = {
     394             :     {
     395             :         .name         = "default",
     396             :         .type         = AVMEDIA_TYPE_VIDEO,
     397             :         .get_video_buffer = get_video_buffer,
     398             :         .filter_frame = filter_frame,
     399             :     },
     400             :     { NULL }
     401             : };
     402             : 
     403             : static const AVFilterPad avfilter_vf_transpose_outputs[] = {
     404             :     {
     405             :         .name         = "default",
     406             :         .config_props = config_props_output,
     407             :         .type         = AVMEDIA_TYPE_VIDEO,
     408             :     },
     409             :     { NULL }
     410             : };
     411             : 
     412             : AVFilter ff_vf_transpose = {
     413             :     .name          = "transpose",
     414             :     .description   = NULL_IF_CONFIG_SMALL("Transpose input video."),
     415             :     .priv_size     = sizeof(TransContext),
     416             :     .priv_class    = &transpose_class,
     417             :     .query_formats = query_formats,
     418             :     .inputs        = avfilter_vf_transpose_inputs,
     419             :     .outputs       = avfilter_vf_transpose_outputs,
     420             :     .flags         = AVFILTER_FLAG_SLICE_THREADS,
     421             : };

Generated by: LCOV version 1.13