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
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];
82 typedef struct ThreadData {
92 #define OFFSET(x) offsetof(RotContext, x)
93 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
95 static const AVOption rotate_options[] = {
96 { "angle", "set angle (in radians)", OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
97 { "a", "set angle (in radians)", OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
98 { "out_w", "set output width expression", OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
99 { "ow", "set output width expression", OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
100 { "out_h", "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
101 { "oh", "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
102 { "fillcolor", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
103 { "c", "set background fill color", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
104 { "bilinear", "use bilinear interpolation", OFFSET(use_bilinear), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, .flags=FLAGS },
108 AVFILTER_DEFINE_CLASS(rotate);
110 static av_cold int init(AVFilterContext *ctx)
112 RotContext *rot = ctx->priv;
114 if (!strcmp(rot->fillcolor_str, "none"))
115 rot->fillcolor_enable = 0;
116 else if (av_parse_color(rot->fillcolor, rot->fillcolor_str, -1, ctx) >= 0)
117 rot->fillcolor_enable = 1;
119 return AVERROR(EINVAL);
123 static av_cold void uninit(AVFilterContext *ctx)
125 RotContext *rot = ctx->priv;
127 av_expr_free(rot->angle_expr);
128 rot->angle_expr = NULL;
131 static int query_formats(AVFilterContext *ctx)
133 static const enum AVPixelFormat pix_fmts[] = {
134 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
135 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
136 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
137 AV_PIX_FMT_0RGB, AV_PIX_FMT_RGB0,
138 AV_PIX_FMT_0BGR, AV_PIX_FMT_BGR0,
139 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
142 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
143 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
144 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
148 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
150 return AVERROR(ENOMEM);
151 return ff_set_common_formats(ctx, fmts_list);
154 static double get_rotated_w(void *opaque, double angle)
156 RotContext *rot = opaque;
157 double inw = rot->var_values[VAR_IN_W];
158 double inh = rot->var_values[VAR_IN_H];
159 float sinx = sin(angle);
160 float cosx = cos(angle);
162 return FFMAX(0, inh * sinx) + FFMAX(0, -inw * cosx) +
163 FFMAX(0, inw * cosx) + FFMAX(0, -inh * sinx);
166 static double get_rotated_h(void *opaque, double angle)
168 RotContext *rot = opaque;
169 double inw = rot->var_values[VAR_IN_W];
170 double inh = rot->var_values[VAR_IN_H];
171 float sinx = sin(angle);
172 float cosx = cos(angle);
174 return FFMAX(0, -inh * cosx) + FFMAX(0, -inw * sinx) +
175 FFMAX(0, inh * cosx) + FFMAX(0, inw * sinx);
178 static double (* const func1[])(void *, double) = {
184 static const char * const func1_names[] = {
190 static int config_props(AVFilterLink *outlink)
192 AVFilterContext *ctx = outlink->src;
193 RotContext *rot = ctx->priv;
194 AVFilterLink *inlink = ctx->inputs[0];
195 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
200 ff_draw_init(&rot->draw, inlink->format, 0);
201 ff_draw_color(&rot->draw, &rot->color, rot->fillcolor);
203 rot->hsub = pixdesc->log2_chroma_w;
204 rot->vsub = pixdesc->log2_chroma_h;
206 rot->var_values[VAR_IN_W] = rot->var_values[VAR_IW] = inlink->w;
207 rot->var_values[VAR_IN_H] = rot->var_values[VAR_IH] = inlink->h;
208 rot->var_values[VAR_HSUB] = 1<<rot->hsub;
209 rot->var_values[VAR_VSUB] = 1<<rot->vsub;
210 rot->var_values[VAR_N] = NAN;
211 rot->var_values[VAR_T] = NAN;
212 rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = NAN;
213 rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = NAN;
215 av_expr_free(rot->angle_expr);
216 rot->angle_expr = NULL;
217 if ((ret = av_expr_parse(&rot->angle_expr, expr = rot->angle_expr_str, var_names,
218 func1_names, func1, NULL, NULL, 0, ctx)) < 0) {
219 av_log(ctx, AV_LOG_ERROR,
220 "Error occurred parsing angle expression '%s'\n", rot->angle_expr_str);
224 #define SET_SIZE_EXPR(name, opt_name) do { \
225 ret = av_expr_parse_and_eval(&res, expr = rot->name##_expr_str, \
226 var_names, rot->var_values, \
227 func1_names, func1, NULL, NULL, rot, 0, ctx); \
228 if (ret < 0 || isnan(res) || isinf(res) || res <= 0) { \
229 av_log(ctx, AV_LOG_ERROR, \
230 "Error parsing or evaluating expression for option %s: " \
231 "invalid expression '%s' or non-positive or indefinite value %f\n", \
232 opt_name, expr, res); \
237 /* evaluate width and height */
238 av_expr_parse_and_eval(&res, expr = rot->outw_expr_str, var_names, rot->var_values,
239 func1_names, func1, NULL, NULL, rot, 0, ctx);
240 rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
241 rot->outw = res + 0.5;
242 SET_SIZE_EXPR(outh, "out_h");
243 rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = res;
244 rot->outh = res + 0.5;
246 /* evaluate the width again, as it may depend on the evaluated output height */
247 SET_SIZE_EXPR(outw, "out_w");
248 rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
249 rot->outw = res + 0.5;
251 /* compute number of planes */
252 rot->nb_planes = av_pix_fmt_count_planes(inlink->format);
253 outlink->w = rot->outw;
254 outlink->h = rot->outh;
259 #define FIXP2 (1<<20)
260 #define INT_PI 3294199 //(M_PI * FIXP2)
263 * Compute the sin of a using integer values.
264 * Input is scaled by FIXP2 and output values are scaled by FIXP.
266 static int64_t int_sin(int64_t a)
270 if (a < 0) a = INT_PI-a; // 0..inf
271 a %= 2 * INT_PI; // 0..2PI
273 if (a >= INT_PI*3/2) a -= 2*INT_PI; // -PI/2 .. 3PI/2
274 if (a >= INT_PI/2 ) a = INT_PI - a; // -PI/2 .. PI/2
276 /* compute sin using Taylor series approximated to the fifth term */
278 for (i = 2; i < 11; i += 2) {
280 a = -a*a2 / (FIXP2*i*(i+1));
286 * Interpolate the color in src at position x and y using bilinear
289 static uint8_t *interpolate_bilinear(uint8_t *dst_color,
290 const uint8_t *src, int src_linesize, int src_linestep,
291 int x, int y, int max_x, int max_y)
293 int int_x = av_clip(x>>16, 0, max_x);
294 int int_y = av_clip(y>>16, 0, max_y);
295 int frac_x = x&0xFFFF;
296 int frac_y = y&0xFFFF;
298 int int_x1 = FFMIN(int_x+1, max_x);
299 int int_y1 = FFMIN(int_y+1, max_y);
301 for (i = 0; i < src_linestep; i++) {
302 int s00 = src[src_linestep * int_x + i + src_linesize * int_y ];
303 int s01 = src[src_linestep * int_x1 + i + src_linesize * int_y ];
304 int s10 = src[src_linestep * int_x + i + src_linesize * int_y1];
305 int s11 = src[src_linestep * int_x1 + i + src_linesize * int_y1];
306 int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
307 int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
309 dst_color[i] = ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32;
315 static av_always_inline void copy_elem(uint8_t *pout, const uint8_t *pin, int elem_size)
323 *((uint16_t *)pout) = *((uint16_t *)pin);
330 *((uint32_t *)pout) = *((uint32_t *)pin);
333 memcpy(pout, pin, elem_size);
338 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)
343 memcpy(dst, src, elem_size * len);
346 for (i = 0; i<len; i++)
347 copy_elem(dst + i*elem_size, src + (len-i-1)*src_linesize, elem_size);
350 for (i = 0; i<len; i++)
351 copy_elem(dst + i*elem_size, src + (len-i-1)*elem_size, elem_size);
354 for (i = 0; i<len; i++)
355 copy_elem(dst + i*elem_size, src + i*src_linesize, elem_size);
360 static av_always_inline void simple_rotate(uint8_t *dst, const uint8_t *src, int src_linesize, int angle, int elem_size, int len)
363 case 1 : simple_rotate_internal(dst, src, src_linesize, angle, 1, len); break;
364 case 2 : simple_rotate_internal(dst, src, src_linesize, angle, 2, len); break;
365 case 3 : simple_rotate_internal(dst, src, src_linesize, angle, 3, len); break;
366 case 4 : simple_rotate_internal(dst, src, src_linesize, angle, 4, len); break;
367 default: simple_rotate_internal(dst, src, src_linesize, angle, elem_size, len); break;
371 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
373 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
375 ThreadData *td = arg;
376 AVFrame *in = td->in;
377 AVFrame *out = td->out;
378 RotContext *rot = ctx->priv;
379 const int outw = td->outw, outh = td->outh;
380 const int inw = td->inw, inh = td->inh;
381 const int plane = td->plane;
382 const int xi = td->xi, yi = td->yi;
383 const int c = td->c, s = td->s;
384 const int start = (outh * job ) / nb_jobs;
385 const int end = (outh * (job+1)) / nb_jobs;
386 int xprime = td->xprime + start * s;
387 int yprime = td->yprime + start * c;
390 for (j = start; j < end; j++) {
391 x = xprime + xi + FIXP*(inw-1)/2;
392 y = yprime + yi + FIXP*(inh-1)/2;
394 if (fabs(rot->angle - 0) < FLT_EPSILON && outw == inw && outh == inh) {
395 simple_rotate(out->data[plane] + j * out->linesize[plane],
396 in->data[plane] + j * in->linesize[plane],
397 in->linesize[plane], 0, rot->draw.pixelstep[plane], outw);
398 } else if (fabs(rot->angle - M_PI/2) < FLT_EPSILON && outw == inh && outh == inw) {
399 simple_rotate(out->data[plane] + j * out->linesize[plane],
400 in->data[plane] + j * rot->draw.pixelstep[plane],
401 in->linesize[plane], 1, rot->draw.pixelstep[plane], outw);
402 } else if (fabs(rot->angle - M_PI) < FLT_EPSILON && outw == inw && outh == inh) {
403 simple_rotate(out->data[plane] + j * out->linesize[plane],
404 in->data[plane] + (outh-j-1) * in->linesize[plane],
405 in->linesize[plane], 2, rot->draw.pixelstep[plane], outw);
406 } else if (fabs(rot->angle - 3*M_PI/2) < FLT_EPSILON && outw == inh && outh == inw) {
407 simple_rotate(out->data[plane] + j * out->linesize[plane],
408 in->data[plane] + (outh-j-1) * rot->draw.pixelstep[plane],
409 in->linesize[plane], 3, rot->draw.pixelstep[plane], outw);
412 for (i = 0; i < outw; i++) {
419 /* the out-of-range values avoid border artifacts */
420 if (x1 >= -1 && x1 <= inw && y1 >= -1 && y1 <= inh) {
421 uint8_t inp_inv[4]; /* interpolated input value */
422 pout = out->data[plane] + j * out->linesize[plane] + i * rot->draw.pixelstep[plane];
423 if (rot->use_bilinear) {
424 pin = interpolate_bilinear(inp_inv,
425 in->data[plane], in->linesize[plane], rot->draw.pixelstep[plane],
428 int x2 = av_clip(x1, 0, inw-1);
429 int y2 = av_clip(y1, 0, inh-1);
430 pin = in->data[plane] + y2 * in->linesize[plane] + x2 * rot->draw.pixelstep[plane];
432 switch (rot->draw.pixelstep[plane]) {
437 *((uint16_t *)pout) = *((uint16_t *)pin);
444 *((uint32_t *)pout) = *((uint32_t *)pin);
447 memcpy(pout, pin, rot->draw.pixelstep[plane]);
462 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
464 AVFilterContext *ctx = inlink->dst;
465 AVFilterLink *outlink = ctx->outputs[0];
467 RotContext *rot = ctx->priv;
468 int angle_int, s, c, plane;
471 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
474 return AVERROR(ENOMEM);
476 av_frame_copy_props(out, in);
478 rot->var_values[VAR_N] = inlink->frame_count;
479 rot->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
480 rot->angle = res = av_expr_eval(rot->angle_expr, rot->var_values, rot);
482 av_log(ctx, AV_LOG_DEBUG, "n:%f time:%f angle:%f/PI\n",
483 rot->var_values[VAR_N], rot->var_values[VAR_T], rot->angle/M_PI);
485 angle_int = res * FIXP * 16;
486 s = int_sin(angle_int);
487 c = int_sin(angle_int + INT_PI/2);
489 /* fill background */
490 if (rot->fillcolor_enable)
491 ff_fill_rectangle(&rot->draw, &rot->color, out->data, out->linesize,
492 0, 0, outlink->w, outlink->h);
494 for (plane = 0; plane < rot->nb_planes; plane++) {
495 int hsub = plane == 1 || plane == 2 ? rot->hsub : 0;
496 int vsub = plane == 1 || plane == 2 ? rot->vsub : 0;
497 const int outw = FF_CEIL_RSHIFT(outlink->w, hsub);
498 const int outh = FF_CEIL_RSHIFT(outlink->h, vsub);
499 ThreadData td = { .in = in, .out = out,
500 .inw = FF_CEIL_RSHIFT(inlink->w, hsub),
501 .inh = FF_CEIL_RSHIFT(inlink->h, vsub),
502 .outh = outh, .outw = outw,
503 .xi = -(outw-1) * c / 2, .yi = (outw-1) * s / 2,
504 .xprime = -(outh-1) * s / 2,
505 .yprime = -(outh-1) * c / 2,
506 .plane = plane, .c = c, .s = s };
509 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outh, ctx->graph->nb_threads));
513 return ff_filter_frame(outlink, out);
516 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
517 char *res, int res_len, int flags)
519 RotContext *rot = ctx->priv;
522 if (!strcmp(cmd, "angle") || !strcmp(cmd, "a")) {
523 AVExpr *old = rot->angle_expr;
524 ret = av_expr_parse(&rot->angle_expr, args, var_names,
525 NULL, NULL, NULL, NULL, 0, ctx);
527 av_log(ctx, AV_LOG_ERROR,
528 "Error when parsing the expression '%s' for angle command\n", args);
529 rot->angle_expr = old;
534 ret = AVERROR(ENOSYS);
539 static const AVFilterPad rotate_inputs[] = {
542 .type = AVMEDIA_TYPE_VIDEO,
543 .filter_frame = filter_frame,
548 static const AVFilterPad rotate_outputs[] = {
551 .type = AVMEDIA_TYPE_VIDEO,
552 .config_props = config_props,
557 AVFilter ff_vf_rotate = {
559 .description = NULL_IF_CONFIG_SMALL("Rotate the input image."),
560 .priv_size = sizeof(RotContext),
563 .query_formats = query_formats,
564 .process_command = process_command,
565 .inputs = rotate_inputs,
566 .outputs = rotate_outputs,
567 .priv_class = &rotate_class,
568 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,