LCOV - code coverage report
Current view: top level - libavfilter - vf_rotate.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 129 268 48.1 %
Date: 2017-12-13 02:34:56 Functions: 9 15 60.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 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             :  * rotation filter, partially based on the tests/rotozoom.c program
      25             : */
      26             : 
      27             : #include "libavutil/avstring.h"
      28             : #include "libavutil/eval.h"
      29             : #include "libavutil/opt.h"
      30             : #include "libavutil/intreadwrite.h"
      31             : #include "libavutil/parseutils.h"
      32             : #include "libavutil/pixdesc.h"
      33             : 
      34             : #include "avfilter.h"
      35             : #include "drawutils.h"
      36             : #include "internal.h"
      37             : #include "video.h"
      38             : 
      39             : #include <float.h>
      40             : 
      41             : static const char * const var_names[] = {
      42             :     "in_w" , "iw",  ///< width of the input video
      43             :     "in_h" , "ih",  ///< height of the input video
      44             :     "out_w", "ow",  ///< width of the input video
      45             :     "out_h", "oh",  ///< height of the input video
      46             :     "hsub", "vsub",
      47             :     "n",            ///< number of frame
      48             :     "t",            ///< timestamp expressed in seconds
      49             :     NULL
      50             : };
      51             : 
      52             : enum var_name {
      53             :     VAR_IN_W , VAR_IW,
      54             :     VAR_IN_H , VAR_IH,
      55             :     VAR_OUT_W, VAR_OW,
      56             :     VAR_OUT_H, VAR_OH,
      57             :     VAR_HSUB, VAR_VSUB,
      58             :     VAR_N,
      59             :     VAR_T,
      60             :     VAR_VARS_NB
      61             : };
      62             : 
      63             : typedef struct RotContext {
      64             :     const AVClass *class;
      65             :     double angle;
      66             :     char *angle_expr_str;   ///< expression for the angle
      67             :     AVExpr *angle_expr;     ///< parsed expression for the angle
      68             :     char *outw_expr_str, *outh_expr_str;
      69             :     int outh, outw;
      70             :     uint8_t fillcolor[4];   ///< color expressed either in YUVA or RGBA colorspace for the padding area
      71             :     char *fillcolor_str;
      72             :     int fillcolor_enable;
      73             :     int hsub, vsub;
      74             :     int nb_planes;
      75             :     int use_bilinear;
      76             :     float sinx, cosx;
      77             :     double var_values[VAR_VARS_NB];
      78             :     FFDrawContext draw;
      79             :     FFDrawColor color;
      80             :     uint8_t *(*interpolate_bilinear)(uint8_t *dst_color,
      81             :                                     const uint8_t *src, int src_linesize, int src_linestep,
      82             :                                     int x, int y, int max_x, int max_y);
      83             : } RotContext;
      84             : 
      85             : typedef struct ThreadData {
      86             :     AVFrame *in, *out;
      87             :     int inw,  inh;
      88             :     int outw, outh;
      89             :     int plane;
      90             :     int xi, yi;
      91             :     int xprime, yprime;
      92             :     int c, s;
      93             : } ThreadData;
      94             : 
      95             : #define OFFSET(x) offsetof(RotContext, x)
      96             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      97             : 
      98             : static const AVOption rotate_options[] = {
      99             :     { "angle",     "set angle (in radians)",       OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
     100             :     { "a",         "set angle (in radians)",       OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
     101             :     { "out_w",     "set output width expression",  OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
     102             :     { "ow",        "set output width expression",  OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
     103             :     { "out_h",     "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
     104             :     { "oh",        "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
     105             :     { "fillcolor", "set background fill color",    OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
     106             :     { "c",         "set background fill color",    OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
     107             :     { "bilinear",  "use bilinear interpolation",   OFFSET(use_bilinear),  AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, .flags=FLAGS },
     108             :     { NULL }
     109             : };
     110             : 
     111             : AVFILTER_DEFINE_CLASS(rotate);
     112             : 
     113          35 : static av_cold int init(AVFilterContext *ctx)
     114             : {
     115          35 :     RotContext *rot = ctx->priv;
     116             : 
     117          35 :     if (!strcmp(rot->fillcolor_str, "none"))
     118           0 :         rot->fillcolor_enable = 0;
     119          35 :     else if (av_parse_color(rot->fillcolor, rot->fillcolor_str, -1, ctx) >= 0)
     120          35 :         rot->fillcolor_enable = 1;
     121             :     else
     122           0 :         return AVERROR(EINVAL);
     123          35 :     return 0;
     124             : }
     125             : 
     126          35 : static av_cold void uninit(AVFilterContext *ctx)
     127             : {
     128          35 :     RotContext *rot = ctx->priv;
     129             : 
     130          35 :     av_expr_free(rot->angle_expr);
     131          35 :     rot->angle_expr = NULL;
     132          35 : }
     133             : 
     134          35 : static int query_formats(AVFilterContext *ctx)
     135             : {
     136             :     static const enum AVPixelFormat pix_fmts[] = {
     137             :         AV_PIX_FMT_GBRP,   AV_PIX_FMT_GBRAP,
     138             :         AV_PIX_FMT_ARGB,   AV_PIX_FMT_RGBA,
     139             :         AV_PIX_FMT_ABGR,   AV_PIX_FMT_BGRA,
     140             :         AV_PIX_FMT_0RGB,   AV_PIX_FMT_RGB0,
     141             :         AV_PIX_FMT_0BGR,   AV_PIX_FMT_BGR0,
     142             :         AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
     143             :         AV_PIX_FMT_GRAY8,
     144             :         AV_PIX_FMT_YUV410P,
     145             :         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P,
     146             :         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUVJ420P,
     147             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
     148             :         AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
     149             :         AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
     150             :         AV_PIX_FMT_YUV420P12LE,
     151             :         AV_PIX_FMT_YUV444P12LE,
     152             :         AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
     153             :         AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
     154             :         AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUVA444P9LE,
     155             :         AV_PIX_FMT_YUV420P9LE, AV_PIX_FMT_YUVA420P9LE,
     156             :         AV_PIX_FMT_NONE
     157             :     };
     158             : 
     159          35 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     160          35 :     if (!fmts_list)
     161           0 :         return AVERROR(ENOMEM);
     162          35 :     return ff_set_common_formats(ctx, fmts_list);
     163             : }
     164             : 
     165           0 : static double get_rotated_w(void *opaque, double angle)
     166             : {
     167           0 :     RotContext *rot = opaque;
     168           0 :     double inw = rot->var_values[VAR_IN_W];
     169           0 :     double inh = rot->var_values[VAR_IN_H];
     170           0 :     float sinx = sin(angle);
     171           0 :     float cosx = cos(angle);
     172             : 
     173           0 :     return FFMAX(0, inh * sinx) + FFMAX(0, -inw * cosx) +
     174           0 :            FFMAX(0, inw * cosx) + FFMAX(0, -inh * sinx);
     175             : }
     176             : 
     177           0 : static double get_rotated_h(void *opaque, double angle)
     178             : {
     179           0 :     RotContext *rot = opaque;
     180           0 :     double inw = rot->var_values[VAR_IN_W];
     181           0 :     double inh = rot->var_values[VAR_IN_H];
     182           0 :     float sinx = sin(angle);
     183           0 :     float cosx = cos(angle);
     184             : 
     185           0 :     return FFMAX(0, -inh * cosx) + FFMAX(0, -inw * sinx) +
     186           0 :            FFMAX(0,  inh * cosx) + FFMAX(0,  inw * sinx);
     187             : }
     188             : 
     189             : static double (* const func1[])(void *, double) = {
     190             :     get_rotated_w,
     191             :     get_rotated_h,
     192             :     NULL
     193             : };
     194             : 
     195             : static const char * const func1_names[] = {
     196             :     "rotw",
     197             :     "roth",
     198             :     NULL
     199             : };
     200             : 
     201             : #define FIXP (1<<16)
     202             : #define FIXP2 (1<<20)
     203             : #define INT_PI 3294199 //(M_PI * FIXP2)
     204             : 
     205             : /**
     206             :  * Compute the sin of a using integer values.
     207             :  * Input is scaled by FIXP2 and output values are scaled by FIXP.
     208             :  */
     209          68 : static int64_t int_sin(int64_t a)
     210             : {
     211          68 :     int64_t a2, res = 0;
     212             :     int i;
     213          68 :     if (a < 0) a = INT_PI-a; // 0..inf
     214          68 :     a %= 2 * INT_PI;         // 0..2PI
     215             : 
     216          68 :     if (a >= INT_PI*3/2) a -= 2*INT_PI;  // -PI/2 .. 3PI/2
     217          68 :     if (a >= INT_PI/2  ) a = INT_PI - a; // -PI/2 ..  PI/2
     218             : 
     219             :     /* compute sin using Taylor series approximated to the fifth term */
     220          68 :     a2 = (a*a)/(FIXP2);
     221         408 :     for (i = 2; i < 11; i += 2) {
     222         340 :         res += a;
     223         340 :         a = -a*a2 / (FIXP2*i*(i+1));
     224             :     }
     225          68 :     return (res + 8)>>4;
     226             : }
     227             : 
     228             : /**
     229             :  * Interpolate the color in src at position x and y using bilinear
     230             :  * interpolation.
     231             :  */
     232           0 : static uint8_t *interpolate_bilinear8(uint8_t *dst_color,
     233             :                                       const uint8_t *src, int src_linesize, int src_linestep,
     234             :                                       int x, int y, int max_x, int max_y)
     235             : {
     236           0 :     int int_x = av_clip(x>>16, 0, max_x);
     237           0 :     int int_y = av_clip(y>>16, 0, max_y);
     238           0 :     int frac_x = x&0xFFFF;
     239           0 :     int frac_y = y&0xFFFF;
     240             :     int i;
     241           0 :     int int_x1 = FFMIN(int_x+1, max_x);
     242           0 :     int int_y1 = FFMIN(int_y+1, max_y);
     243             : 
     244           0 :     for (i = 0; i < src_linestep; i++) {
     245           0 :         int s00 = src[src_linestep * int_x  + i + src_linesize * int_y ];
     246           0 :         int s01 = src[src_linestep * int_x1 + i + src_linesize * int_y ];
     247           0 :         int s10 = src[src_linestep * int_x  + i + src_linesize * int_y1];
     248           0 :         int s11 = src[src_linestep * int_x1 + i + src_linesize * int_y1];
     249           0 :         int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
     250           0 :         int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
     251             : 
     252           0 :         dst_color[i] = ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32;
     253             :     }
     254             : 
     255           0 :     return dst_color;
     256             : }
     257             : 
     258             : /**
     259             :  * Interpolate the color in src at position x and y using bilinear
     260             :  * interpolation.
     261             :  */
     262           0 : static uint8_t *interpolate_bilinear16(uint8_t *dst_color,
     263             :                                        const uint8_t *src, int src_linesize, int src_linestep,
     264             :                                        int x, int y, int max_x, int max_y)
     265             : {
     266           0 :     int int_x = av_clip(x>>16, 0, max_x);
     267           0 :     int int_y = av_clip(y>>16, 0, max_y);
     268           0 :     int frac_x = x&0xFFFF;
     269           0 :     int frac_y = y&0xFFFF;
     270             :     int i;
     271           0 :     int int_x1 = FFMIN(int_x+1, max_x);
     272           0 :     int int_y1 = FFMIN(int_y+1, max_y);
     273             : 
     274           0 :     for (i = 0; i < src_linestep; i+=2) {
     275           0 :         int s00 = AV_RL16(&src[src_linestep * int_x  + i + src_linesize * int_y ]);
     276           0 :         int s01 = AV_RL16(&src[src_linestep * int_x1 + i + src_linesize * int_y ]);
     277           0 :         int s10 = AV_RL16(&src[src_linestep * int_x  + i + src_linesize * int_y1]);
     278           0 :         int s11 = AV_RL16(&src[src_linestep * int_x1 + i + src_linesize * int_y1]);
     279           0 :         int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
     280           0 :         int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
     281             : 
     282           0 :         AV_WL16(&dst_color[i], ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32);
     283             :     }
     284             : 
     285           0 :     return dst_color;
     286             : }
     287             : 
     288          34 : static int config_props(AVFilterLink *outlink)
     289             : {
     290          34 :     AVFilterContext *ctx = outlink->src;
     291          34 :     RotContext *rot = ctx->priv;
     292          34 :     AVFilterLink *inlink = ctx->inputs[0];
     293          34 :     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
     294             :     int ret;
     295             :     double res;
     296             :     char *expr;
     297             : 
     298          34 :     ff_draw_init(&rot->draw, inlink->format, 0);
     299          34 :     ff_draw_color(&rot->draw, &rot->color, rot->fillcolor);
     300             : 
     301          34 :     rot->hsub = pixdesc->log2_chroma_w;
     302          34 :     rot->vsub = pixdesc->log2_chroma_h;
     303             : 
     304          34 :     if (pixdesc->comp[0].depth == 8)
     305          20 :         rot->interpolate_bilinear = interpolate_bilinear8;
     306             :     else
     307          14 :         rot->interpolate_bilinear = interpolate_bilinear16;
     308             : 
     309          34 :     rot->var_values[VAR_IN_W] = rot->var_values[VAR_IW] = inlink->w;
     310          34 :     rot->var_values[VAR_IN_H] = rot->var_values[VAR_IH] = inlink->h;
     311          34 :     rot->var_values[VAR_HSUB] = 1<<rot->hsub;
     312          34 :     rot->var_values[VAR_VSUB] = 1<<rot->vsub;
     313          34 :     rot->var_values[VAR_N] = NAN;
     314          34 :     rot->var_values[VAR_T] = NAN;
     315          34 :     rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = NAN;
     316          34 :     rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = NAN;
     317             : 
     318          34 :     av_expr_free(rot->angle_expr);
     319          34 :     rot->angle_expr = NULL;
     320          34 :     if ((ret = av_expr_parse(&rot->angle_expr, expr = rot->angle_expr_str, var_names,
     321             :                              func1_names, func1, NULL, NULL, 0, ctx)) < 0) {
     322           0 :         av_log(ctx, AV_LOG_ERROR,
     323             :                "Error occurred parsing angle expression '%s'\n", rot->angle_expr_str);
     324           0 :         return ret;
     325             :     }
     326             : 
     327             : #define SET_SIZE_EXPR(name, opt_name) do {                                         \
     328             :     ret = av_expr_parse_and_eval(&res, expr = rot->name##_expr_str,                \
     329             :                                  var_names, rot->var_values,                       \
     330             :                                  func1_names, func1, NULL, NULL, rot, 0, ctx);     \
     331             :     if (ret < 0 || isnan(res) || isinf(res) || res <= 0) {                         \
     332             :         av_log(ctx, AV_LOG_ERROR,                                                  \
     333             :                "Error parsing or evaluating expression for option %s: "            \
     334             :                "invalid expression '%s' or non-positive or indefinite value %f\n", \
     335             :                opt_name, expr, res);                                               \
     336             :         return ret;                                                                \
     337             :     }                                                                              \
     338             : } while (0)
     339             : 
     340             :     /* evaluate width and height */
     341          34 :     av_expr_parse_and_eval(&res, expr = rot->outw_expr_str, var_names, rot->var_values,
     342             :                            func1_names, func1, NULL, NULL, rot, 0, ctx);
     343          34 :     rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
     344          34 :     rot->outw = res + 0.5;
     345          34 :     SET_SIZE_EXPR(outh, "out_h");
     346          34 :     rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = res;
     347          34 :     rot->outh = res + 0.5;
     348             : 
     349             :     /* evaluate the width again, as it may depend on the evaluated output height */
     350          34 :     SET_SIZE_EXPR(outw, "out_w");
     351          34 :     rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
     352          34 :     rot->outw = res + 0.5;
     353             : 
     354             :     /* compute number of planes */
     355          34 :     rot->nb_planes = av_pix_fmt_count_planes(inlink->format);
     356          34 :     outlink->w = rot->outw;
     357          34 :     outlink->h = rot->outh;
     358          34 :     return 0;
     359             : }
     360             : 
     361           0 : static av_always_inline void copy_elem(uint8_t *pout, const uint8_t *pin, int elem_size)
     362             : {
     363             :     int v;
     364           0 :     switch (elem_size) {
     365           0 :     case 1:
     366           0 :         *pout = *pin;
     367           0 :         break;
     368           0 :     case 2:
     369           0 :         *((uint16_t *)pout) = *((uint16_t *)pin);
     370           0 :         break;
     371           0 :     case 3:
     372           0 :         v = AV_RB24(pin);
     373           0 :         AV_WB24(pout, v);
     374           0 :         break;
     375           0 :     case 4:
     376           0 :         *((uint32_t *)pout) = *((uint32_t *)pin);
     377           0 :         break;
     378           0 :     default:
     379           0 :         memcpy(pout, pin, elem_size);
     380           0 :         break;
     381             :     }
     382           0 : }
     383             : 
     384       22320 : static av_always_inline void simple_rotate_internal(uint8_t *dst, const uint8_t *src, int src_linesize, int angle, int elem_size, int len)
     385             : {
     386             :     int i;
     387       22320 :     switch(angle) {
     388       22320 :     case 0:
     389       22320 :         memcpy(dst, src, elem_size * len);
     390       22320 :         break;
     391           0 :     case 1:
     392           0 :         for (i = 0; i<len; i++)
     393           0 :             copy_elem(dst + i*elem_size, src + (len-i-1)*src_linesize, elem_size);
     394           0 :         break;
     395           0 :     case 2:
     396           0 :         for (i = 0; i<len; i++)
     397           0 :             copy_elem(dst + i*elem_size, src + (len-i-1)*elem_size, elem_size);
     398           0 :         break;
     399           0 :     case 3:
     400           0 :         for (i = 0; i<len; i++)
     401           0 :             copy_elem(dst + i*elem_size, src + i*src_linesize, elem_size);
     402           0 :         break;
     403             :     }
     404       22320 : }
     405             : 
     406       22320 : static av_always_inline void simple_rotate(uint8_t *dst, const uint8_t *src, int src_linesize, int angle, int elem_size, int len)
     407             : {
     408       22320 :     switch(elem_size) {
     409        7632 :     case 1 : simple_rotate_internal(dst, src, src_linesize, angle, 1, len); break;
     410       11808 :     case 2 : simple_rotate_internal(dst, src, src_linesize, angle, 2, len); break;
     411         576 :     case 3 : simple_rotate_internal(dst, src, src_linesize, angle, 3, len); break;
     412        2304 :     case 4 : simple_rotate_internal(dst, src, src_linesize, angle, 4, len); break;
     413           0 :     default: simple_rotate_internal(dst, src, src_linesize, angle, elem_size, len); break;
     414             :     }
     415       22320 : }
     416             : 
     417             : #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
     418             : 
     419          89 : static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
     420             : {
     421          89 :     ThreadData *td = arg;
     422          89 :     AVFrame *in = td->in;
     423          89 :     AVFrame *out = td->out;
     424          89 :     RotContext *rot = ctx->priv;
     425          89 :     const int outw = td->outw, outh = td->outh;
     426          89 :     const int inw = td->inw, inh = td->inh;
     427          89 :     const int plane = td->plane;
     428          89 :     const int xi = td->xi, yi = td->yi;
     429          89 :     const int c = td->c, s = td->s;
     430          89 :     const int start = (outh *  job   ) / nb_jobs;
     431          89 :     const int end   = (outh * (job+1)) / nb_jobs;
     432          89 :     int xprime = td->xprime + start * s;
     433          89 :     int yprime = td->yprime + start * c;
     434             :     int i, j, x, y;
     435             : 
     436       22409 :     for (j = start; j < end; j++) {
     437       22320 :         x = xprime + xi + FIXP*(inw-1)/2;
     438       22320 :         y = yprime + yi + FIXP*(inh-1)/2;
     439             : 
     440       22320 :         if (fabs(rot->angle - 0) < FLT_EPSILON && outw == inw && outh == inh) {
     441       44640 :             simple_rotate(out->data[plane] + j * out->linesize[plane],
     442       22320 :                            in->data[plane] + j *  in->linesize[plane],
     443             :                           in->linesize[plane], 0, rot->draw.pixelstep[plane], outw);
     444           0 :         } else if (fabs(rot->angle - M_PI/2) < FLT_EPSILON && outw == inh && outh == inw) {
     445           0 :             simple_rotate(out->data[plane] + j * out->linesize[plane],
     446           0 :                            in->data[plane] + j * rot->draw.pixelstep[plane],
     447             :                           in->linesize[plane], 1, rot->draw.pixelstep[plane], outw);
     448           0 :         } else if (fabs(rot->angle - M_PI) < FLT_EPSILON && outw == inw && outh == inh) {
     449           0 :             simple_rotate(out->data[plane] + j * out->linesize[plane],
     450           0 :                            in->data[plane] + (outh-j-1) *  in->linesize[plane],
     451             :                           in->linesize[plane], 2, rot->draw.pixelstep[plane], outw);
     452           0 :         } else if (fabs(rot->angle - 3*M_PI/2) < FLT_EPSILON && outw == inh && outh == inw) {
     453           0 :             simple_rotate(out->data[plane] + j * out->linesize[plane],
     454           0 :                            in->data[plane] + (outh-j-1) * rot->draw.pixelstep[plane],
     455             :                           in->linesize[plane], 3, rot->draw.pixelstep[plane], outw);
     456             :         } else {
     457             : 
     458           0 :         for (i = 0; i < outw; i++) {
     459             :             int32_t v;
     460             :             int x1, y1;
     461             :             uint8_t *pin, *pout;
     462           0 :             x1 = x>>16;
     463           0 :             y1 = y>>16;
     464             : 
     465             :             /* the out-of-range values avoid border artifacts */
     466           0 :             if (x1 >= -1 && x1 <= inw && y1 >= -1 && y1 <= inh) {
     467             :                 uint8_t inp_inv[4]; /* interpolated input value */
     468           0 :                 pout = out->data[plane] + j * out->linesize[plane] + i * rot->draw.pixelstep[plane];
     469           0 :                 if (rot->use_bilinear) {
     470           0 :                     pin = rot->interpolate_bilinear(inp_inv,
     471           0 :                                                     in->data[plane], in->linesize[plane], rot->draw.pixelstep[plane],
     472             :                                                     x, y, inw-1, inh-1);
     473             :                 } else {
     474           0 :                     int x2 = av_clip(x1, 0, inw-1);
     475           0 :                     int y2 = av_clip(y1, 0, inh-1);
     476           0 :                     pin = in->data[plane] + y2 * in->linesize[plane] + x2 * rot->draw.pixelstep[plane];
     477             :                 }
     478           0 :                 switch (rot->draw.pixelstep[plane]) {
     479           0 :                 case 1:
     480           0 :                     *pout = *pin;
     481           0 :                     break;
     482           0 :                 case 2:
     483           0 :                     v = AV_RL16(pin);
     484           0 :                     AV_WL16(pout, v);
     485           0 :                     break;
     486           0 :                 case 3:
     487           0 :                     v = AV_RB24(pin);
     488           0 :                     AV_WB24(pout, v);
     489           0 :                     break;
     490           0 :                 case 4:
     491           0 :                     *((uint32_t *)pout) = *((uint32_t *)pin);
     492           0 :                     break;
     493           0 :                 default:
     494           0 :                     memcpy(pout, pin, rot->draw.pixelstep[plane]);
     495           0 :                     break;
     496             :                 }
     497             :             }
     498           0 :             x += c;
     499           0 :             y -= s;
     500             :         }
     501             :         }
     502       22320 :         xprime += s;
     503       22320 :         yprime += c;
     504             :     }
     505             : 
     506          89 :     return 0;
     507             : }
     508             : 
     509          34 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     510             : {
     511          34 :     AVFilterContext *ctx = inlink->dst;
     512          34 :     AVFilterLink *outlink = ctx->outputs[0];
     513             :     AVFrame *out;
     514          34 :     RotContext *rot = ctx->priv;
     515             :     int angle_int, s, c, plane;
     516             :     double res;
     517             : 
     518          34 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     519          34 :     if (!out) {
     520           0 :         av_frame_free(&in);
     521           0 :         return AVERROR(ENOMEM);
     522             :     }
     523          34 :     av_frame_copy_props(out, in);
     524             : 
     525          34 :     rot->var_values[VAR_N] = inlink->frame_count_out;
     526          34 :     rot->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
     527          34 :     rot->angle = res = av_expr_eval(rot->angle_expr, rot->var_values, rot);
     528             : 
     529          34 :     av_log(ctx, AV_LOG_DEBUG, "n:%f time:%f angle:%f/PI\n",
     530          34 :            rot->var_values[VAR_N], rot->var_values[VAR_T], rot->angle/M_PI);
     531             : 
     532          34 :     angle_int = res * FIXP * 16;
     533          34 :     s = int_sin(angle_int);
     534          34 :     c = int_sin(angle_int + INT_PI/2);
     535             : 
     536             :     /* fill background */
     537          34 :     if (rot->fillcolor_enable)
     538          34 :         ff_fill_rectangle(&rot->draw, &rot->color, out->data, out->linesize,
     539             :                           0, 0, outlink->w, outlink->h);
     540             : 
     541         123 :     for (plane = 0; plane < rot->nb_planes; plane++) {
     542          89 :         int hsub = plane == 1 || plane == 2 ? rot->hsub : 0;
     543          89 :         int vsub = plane == 1 || plane == 2 ? rot->vsub : 0;
     544          89 :         const int outw = AV_CEIL_RSHIFT(outlink->w, hsub);
     545          89 :         const int outh = AV_CEIL_RSHIFT(outlink->h, vsub);
     546         623 :         ThreadData td = { .in = in,   .out  = out,
     547          89 :                           .inw  = AV_CEIL_RSHIFT(inlink->w, hsub),
     548          89 :                           .inh  = AV_CEIL_RSHIFT(inlink->h, vsub),
     549             :                           .outh = outh, .outw = outw,
     550         178 :                           .xi = -(outw-1) * c / 2, .yi =  (outw-1) * s / 2,
     551          89 :                           .xprime = -(outh-1) * s / 2,
     552          89 :                           .yprime = -(outh-1) * c / 2,
     553             :                           .plane = plane, .c = c, .s = s };
     554             : 
     555             : 
     556          89 :         ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outh, ff_filter_get_nb_threads(ctx)));
     557             :     }
     558             : 
     559          34 :     av_frame_free(&in);
     560          34 :     return ff_filter_frame(outlink, out);
     561             : }
     562             : 
     563           0 : static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
     564             :                            char *res, int res_len, int flags)
     565             : {
     566           0 :     RotContext *rot = ctx->priv;
     567             :     int ret;
     568             : 
     569           0 :     if (!strcmp(cmd, "angle") || !strcmp(cmd, "a")) {
     570           0 :         AVExpr *old = rot->angle_expr;
     571           0 :         ret = av_expr_parse(&rot->angle_expr, args, var_names,
     572             :                             NULL, NULL, NULL, NULL, 0, ctx);
     573           0 :         if (ret < 0) {
     574           0 :             av_log(ctx, AV_LOG_ERROR,
     575             :                    "Error when parsing the expression '%s' for angle command\n", args);
     576           0 :             rot->angle_expr = old;
     577           0 :             return ret;
     578             :         }
     579           0 :         av_expr_free(old);
     580             :     } else
     581           0 :         ret = AVERROR(ENOSYS);
     582             : 
     583           0 :     return ret;
     584             : }
     585             : 
     586             : static const AVFilterPad rotate_inputs[] = {
     587             :     {
     588             :         .name         = "default",
     589             :         .type         = AVMEDIA_TYPE_VIDEO,
     590             :         .filter_frame = filter_frame,
     591             :     },
     592             :     { NULL }
     593             : };
     594             : 
     595             : static const AVFilterPad rotate_outputs[] = {
     596             :     {
     597             :         .name         = "default",
     598             :         .type         = AVMEDIA_TYPE_VIDEO,
     599             :         .config_props = config_props,
     600             :     },
     601             :     { NULL }
     602             : };
     603             : 
     604             : AVFilter ff_vf_rotate = {
     605             :     .name          = "rotate",
     606             :     .description   = NULL_IF_CONFIG_SMALL("Rotate the input image."),
     607             :     .priv_size     = sizeof(RotContext),
     608             :     .init          = init,
     609             :     .uninit        = uninit,
     610             :     .query_formats = query_formats,
     611             :     .process_command = process_command,
     612             :     .inputs        = rotate_inputs,
     613             :     .outputs       = rotate_outputs,
     614             :     .priv_class    = &rotate_class,
     615             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
     616             : };

Generated by: LCOV version 1.13