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