2 * Copyright (c) 2013 Stefano Sabatini
3 * Copyright (c) 2008 Vitor Sessak
5 * This file is part of FFmpeg.
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.
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.
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
24 * rotation filter, partially based on the tests/rotozoom.c program
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"
35 #include "drawutils.h"
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
47 "n", ///< number of frame
48 "t", ///< timestamp expressed in seconds
63 typedef struct RotContext {
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;
70 uint8_t fillcolor[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
77 double var_values[VAR_VARS_NB];
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);
85 typedef struct ThreadData {
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
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 },
112 AVFILTER_DEFINE_CLASS(rotate);
114 static av_cold int init(AVFilterContext *ctx)
116 RotContext *rot = ctx->priv;
118 if (!strcmp(rot->fillcolor_str, "none"))
119 rot->fillcolor_enable = 0;
120 else if (av_parse_color(rot->fillcolor, rot->fillcolor_str, -1, ctx) >= 0)
121 rot->fillcolor_enable = 1;
123 return AVERROR(EINVAL);
127 static av_cold void uninit(AVFilterContext *ctx)
129 RotContext *rot = ctx->priv;
131 av_expr_free(rot->angle_expr);
132 rot->angle_expr = NULL;
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,
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,
158 static double get_rotated_w(void *opaque, double angle)
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);
166 return FFMAX(0, inh * sinx) + FFMAX(0, -inw * cosx) +
167 FFMAX(0, inw * cosx) + FFMAX(0, -inh * sinx);
170 static double get_rotated_h(void *opaque, double angle)
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);
178 return FFMAX(0, -inh * cosx) + FFMAX(0, -inw * sinx) +
179 FFMAX(0, inh * cosx) + FFMAX(0, inw * sinx);
182 static double (* const func1[])(void *, double) = {
188 static const char * const func1_names[] = {
195 #define FIXP2 (1<<20)
196 #define INT_PI 3294199 //(M_PI * FIXP2)
199 * Compute the sin of a using integer values.
200 * Input is scaled by FIXP2 and output values are scaled by FIXP.
202 static int64_t int_sin(int64_t a)
206 if (a < 0) a = INT_PI-a; // 0..inf
207 a %= 2 * INT_PI; // 0..2PI
209 if (a >= INT_PI*3/2) a -= 2*INT_PI; // -PI/2 .. 3PI/2
210 if (a >= INT_PI/2 ) a = INT_PI - a; // -PI/2 .. PI/2
212 /* compute sin using Taylor series approximated to the fifth term */
214 for (i = 2; i < 11; i += 2) {
216 a = -a*a2 / (FIXP2*i*(i+1));
222 * Interpolate the color in src at position x and y using bilinear
225 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)
229 int int_x = av_clip(x>>16, 0, max_x);
230 int int_y = av_clip(y>>16, 0, max_y);
231 int frac_x = x&0xFFFF;
232 int frac_y = y&0xFFFF;
234 int int_x1 = FFMIN(int_x+1, max_x);
235 int int_y1 = FFMIN(int_y+1, max_y);
237 for (i = 0; i < src_linestep; i++) {
238 int s00 = src[src_linestep * int_x + i + src_linesize * int_y ];
239 int s01 = src[src_linestep * int_x1 + i + src_linesize * int_y ];
240 int s10 = src[src_linestep * int_x + i + src_linesize * int_y1];
241 int s11 = src[src_linestep * int_x1 + i + src_linesize * int_y1];
242 int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
243 int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
245 dst_color[i] = ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32;
252 * Interpolate the color in src at position x and y using bilinear
255 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)
259 int int_x = av_clip(x>>16, 0, max_x);
260 int int_y = av_clip(y>>16, 0, max_y);
261 int64_t frac_x = x&0xFFFF;
262 int64_t frac_y = y&0xFFFF;
264 int int_x1 = FFMIN(int_x+1, max_x);
265 int int_y1 = FFMIN(int_y+1, max_y);
267 for (i = 0; i < src_linestep; i+=2) {
268 int s00 = AV_RL16(&src[src_linestep * int_x + i + src_linesize * int_y ]);
269 int s01 = AV_RL16(&src[src_linestep * int_x1 + i + src_linesize * int_y ]);
270 int s10 = AV_RL16(&src[src_linestep * int_x + i + src_linesize * int_y1]);
271 int s11 = AV_RL16(&src[src_linestep * int_x1 + i + src_linesize * int_y1]);
272 int64_t s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
273 int64_t s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
275 AV_WL16(&dst_color[i], (((1<<16) - frac_y)*s0 + frac_y*s1) >> 32);
281 static int config_props(AVFilterLink *outlink)
283 AVFilterContext *ctx = outlink->src;
284 RotContext *rot = ctx->priv;
285 AVFilterLink *inlink = ctx->inputs[0];
286 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
291 ff_draw_init(&rot->draw, inlink->format, 0);
292 ff_draw_color(&rot->draw, &rot->color, rot->fillcolor);
294 rot->hsub = pixdesc->log2_chroma_w;
295 rot->vsub = pixdesc->log2_chroma_h;
297 if (pixdesc->comp[0].depth == 8)
298 rot->interpolate_bilinear = interpolate_bilinear8;
300 rot->interpolate_bilinear = interpolate_bilinear16;
302 rot->var_values[VAR_IN_W] = rot->var_values[VAR_IW] = inlink->w;
303 rot->var_values[VAR_IN_H] = rot->var_values[VAR_IH] = inlink->h;
304 rot->var_values[VAR_HSUB] = 1<<rot->hsub;
305 rot->var_values[VAR_VSUB] = 1<<rot->vsub;
306 rot->var_values[VAR_N] = NAN;
307 rot->var_values[VAR_T] = NAN;
308 rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = NAN;
309 rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = NAN;
311 av_expr_free(rot->angle_expr);
312 rot->angle_expr = NULL;
313 if ((ret = av_expr_parse(&rot->angle_expr, expr = rot->angle_expr_str, var_names,
314 func1_names, func1, NULL, NULL, 0, ctx)) < 0) {
315 av_log(ctx, AV_LOG_ERROR,
316 "Error occurred parsing angle expression '%s'\n", rot->angle_expr_str);
320 #define SET_SIZE_EXPR(name, opt_name) do { \
321 ret = av_expr_parse_and_eval(&res, expr = rot->name##_expr_str, \
322 var_names, rot->var_values, \
323 func1_names, func1, NULL, NULL, rot, 0, ctx); \
324 if (ret < 0 || isnan(res) || isinf(res) || res <= 0) { \
325 av_log(ctx, AV_LOG_ERROR, \
326 "Error parsing or evaluating expression for option %s: " \
327 "invalid expression '%s' or non-positive or indefinite value %f\n", \
328 opt_name, expr, res); \
333 /* evaluate width and height */
334 av_expr_parse_and_eval(&res, expr = rot->outw_expr_str, var_names, rot->var_values,
335 func1_names, func1, NULL, NULL, rot, 0, ctx);
336 rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
337 rot->outw = res + 0.5;
338 SET_SIZE_EXPR(outh, "out_h");
339 rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = res;
340 rot->outh = res + 0.5;
342 /* evaluate the width again, as it may depend on the evaluated output height */
343 SET_SIZE_EXPR(outw, "out_w");
344 rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
345 rot->outw = res + 0.5;
347 /* compute number of planes */
348 rot->nb_planes = av_pix_fmt_count_planes(inlink->format);
349 outlink->w = rot->outw;
350 outlink->h = rot->outh;
354 static av_always_inline void copy_elem(uint8_t *pout, const uint8_t *pin, int elem_size)
362 *((uint16_t *)pout) = *((uint16_t *)pin);
369 *((uint32_t *)pout) = *((uint32_t *)pin);
372 memcpy(pout, pin, elem_size);
377 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)
382 memcpy(dst, src, elem_size * len);
385 for (i = 0; i<len; i++)
386 copy_elem(dst + i*elem_size, src + (len-i-1)*src_linesize, elem_size);
389 for (i = 0; i<len; i++)
390 copy_elem(dst + i*elem_size, src + (len-i-1)*elem_size, elem_size);
393 for (i = 0; i<len; i++)
394 copy_elem(dst + i*elem_size, src + i*src_linesize, elem_size);
399 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 case 1 : simple_rotate_internal(dst, src, src_linesize, angle, 1, len); break;
403 case 2 : simple_rotate_internal(dst, src, src_linesize, angle, 2, len); break;
404 case 3 : simple_rotate_internal(dst, src, src_linesize, angle, 3, len); break;
405 case 4 : simple_rotate_internal(dst, src, src_linesize, angle, 4, len); break;
406 default: simple_rotate_internal(dst, src, src_linesize, angle, elem_size, len); break;
410 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
412 ThreadData *td = arg;
413 AVFrame *in = td->in;
414 AVFrame *out = td->out;
415 RotContext *rot = ctx->priv;
416 const int outw = td->outw, outh = td->outh;
417 const int inw = td->inw, inh = td->inh;
418 const int plane = td->plane;
419 const int xi = td->xi, yi = td->yi;
420 const int c = td->c, s = td->s;
421 const int start = (outh * job ) / nb_jobs;
422 const int end = (outh * (job+1)) / nb_jobs;
423 int xprime = td->xprime + start * s;
424 int yprime = td->yprime + start * c;
427 for (j = start; j < end; j++) {
428 x = xprime + xi + FIXP*(inw-1)/2;
429 y = yprime + yi + FIXP*(inh-1)/2;
431 if (fabs(rot->angle - 0) < FLT_EPSILON && outw == inw && outh == inh) {
432 simple_rotate(out->data[plane] + j * out->linesize[plane],
433 in->data[plane] + j * in->linesize[plane],
434 in->linesize[plane], 0, rot->draw.pixelstep[plane], outw);
435 } else if (fabs(rot->angle - M_PI/2) < FLT_EPSILON && outw == inh && outh == inw) {
436 simple_rotate(out->data[plane] + j * out->linesize[plane],
437 in->data[plane] + j * rot->draw.pixelstep[plane],
438 in->linesize[plane], 1, rot->draw.pixelstep[plane], outw);
439 } else if (fabs(rot->angle - M_PI) < FLT_EPSILON && outw == inw && outh == inh) {
440 simple_rotate(out->data[plane] + j * out->linesize[plane],
441 in->data[plane] + (outh-j-1) * in->linesize[plane],
442 in->linesize[plane], 2, rot->draw.pixelstep[plane], outw);
443 } else if (fabs(rot->angle - 3*M_PI/2) < FLT_EPSILON && outw == inh && outh == inw) {
444 simple_rotate(out->data[plane] + j * out->linesize[plane],
445 in->data[plane] + (outh-j-1) * rot->draw.pixelstep[plane],
446 in->linesize[plane], 3, rot->draw.pixelstep[plane], outw);
449 for (i = 0; i < outw; i++) {
456 /* the out-of-range values avoid border artifacts */
457 if (x1 >= -1 && x1 <= inw && y1 >= -1 && y1 <= inh) {
458 uint8_t inp_inv[4]; /* interpolated input value */
459 pout = out->data[plane] + j * out->linesize[plane] + i * rot->draw.pixelstep[plane];
460 if (rot->use_bilinear) {
461 pin = rot->interpolate_bilinear(inp_inv,
462 in->data[plane], in->linesize[plane], rot->draw.pixelstep[plane],
465 int x2 = av_clip(x1, 0, inw-1);
466 int y2 = av_clip(y1, 0, inh-1);
467 pin = in->data[plane] + y2 * in->linesize[plane] + x2 * rot->draw.pixelstep[plane];
469 switch (rot->draw.pixelstep[plane]) {
482 *((uint32_t *)pout) = *((uint32_t *)pin);
485 memcpy(pout, pin, rot->draw.pixelstep[plane]);
500 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
502 AVFilterContext *ctx = inlink->dst;
503 AVFilterLink *outlink = ctx->outputs[0];
505 RotContext *rot = ctx->priv;
506 int angle_int, s, c, plane;
509 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
512 return AVERROR(ENOMEM);
514 av_frame_copy_props(out, in);
516 rot->var_values[VAR_N] = inlink->frame_count_out;
517 rot->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
518 rot->angle = res = av_expr_eval(rot->angle_expr, rot->var_values, rot);
520 av_log(ctx, AV_LOG_DEBUG, "n:%f time:%f angle:%f/PI\n",
521 rot->var_values[VAR_N], rot->var_values[VAR_T], rot->angle/M_PI);
523 angle_int = res * FIXP * 16;
524 s = int_sin(angle_int);
525 c = int_sin(angle_int + INT_PI/2);
527 /* fill background */
528 if (rot->fillcolor_enable)
529 ff_fill_rectangle(&rot->draw, &rot->color, out->data, out->linesize,
530 0, 0, outlink->w, outlink->h);
532 for (plane = 0; plane < rot->nb_planes; plane++) {
533 int hsub = plane == 1 || plane == 2 ? rot->hsub : 0;
534 int vsub = plane == 1 || plane == 2 ? rot->vsub : 0;
535 const int outw = AV_CEIL_RSHIFT(outlink->w, hsub);
536 const int outh = AV_CEIL_RSHIFT(outlink->h, vsub);
537 ThreadData td = { .in = in, .out = out,
538 .inw = AV_CEIL_RSHIFT(inlink->w, hsub),
539 .inh = AV_CEIL_RSHIFT(inlink->h, vsub),
540 .outh = outh, .outw = outw,
541 .xi = -(outw-1) * c / 2, .yi = (outw-1) * s / 2,
542 .xprime = -(outh-1) * s / 2,
543 .yprime = -(outh-1) * c / 2,
544 .plane = plane, .c = c, .s = s };
546 ff_filter_execute(ctx, filter_slice, &td, NULL,
547 FFMIN(outh, ff_filter_get_nb_threads(ctx)));
551 return ff_filter_frame(outlink, out);
554 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
555 char *res, int res_len, int flags)
557 RotContext *rot = ctx->priv;
560 if (!strcmp(cmd, "angle") || !strcmp(cmd, "a")) {
561 AVExpr *old = rot->angle_expr;
562 ret = av_expr_parse(&rot->angle_expr, args, var_names,
563 NULL, NULL, NULL, NULL, 0, ctx);
565 av_log(ctx, AV_LOG_ERROR,
566 "Error when parsing the expression '%s' for angle command\n", args);
567 rot->angle_expr = old;
572 ret = AVERROR(ENOSYS);
577 static const AVFilterPad rotate_inputs[] = {
580 .type = AVMEDIA_TYPE_VIDEO,
581 .filter_frame = filter_frame,
585 static const AVFilterPad rotate_outputs[] = {
588 .type = AVMEDIA_TYPE_VIDEO,
589 .config_props = config_props,
593 const AVFilter ff_vf_rotate = {
595 .description = NULL_IF_CONFIG_SMALL("Rotate the input image."),
596 .priv_size = sizeof(RotContext),
599 .process_command = process_command,
600 FILTER_INPUTS(rotate_inputs),
601 FILTER_OUTPUTS(rotate_outputs),
602 FILTER_PIXFMTS_ARRAY(pix_fmts),
603 .priv_class = &rotate_class,
604 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,