]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_rotate.c
avfilter/vf_pixdesctest: use av_malloc_array()
[ffmpeg] / libavfilter / vf_rotate.c
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 static const char *var_names[] = {
40     "in_w" , "iw",  ///< width of the input video
41     "in_h" , "ih",  ///< height of the input video
42     "out_w", "ow",  ///< width of the input video
43     "out_h", "oh",  ///< height of the input video
44     "hsub", "vsub",
45     "n",            ///< number of frame
46     "t",            ///< timestamp expressed in seconds
47     NULL
48 };
49
50 enum var_name {
51     VAR_IN_W , VAR_IW,
52     VAR_IN_H , VAR_IH,
53     VAR_OUT_W, VAR_OW,
54     VAR_OUT_H, VAR_OH,
55     VAR_HSUB, VAR_VSUB,
56     VAR_N,
57     VAR_T,
58     VAR_VARS_NB
59 };
60
61 typedef struct {
62     const AVClass *class;
63     double angle;
64     char *angle_expr_str;   ///< expression for the angle
65     AVExpr *angle_expr;     ///< parsed expression for the angle
66     char *outw_expr_str, *outh_expr_str;
67     int outh, outw;
68     uint8_t fillcolor[4];   ///< color expressed either in YUVA or RGBA colorspace for the padding area
69     char *fillcolor_str;
70     int fillcolor_enable;
71     int hsub, vsub;
72     int nb_planes;
73     int use_bilinear;
74     float sinx, cosx;
75     double var_values[VAR_VARS_NB];
76     FFDrawContext draw;
77     FFDrawColor color;
78 } RotContext;
79
80 typedef struct ThreadData {
81     AVFrame *in, *out;
82     int inw,  inh;
83     int outw, outh;
84     int plane;
85     int xi, yi;
86     int xprime, yprime;
87     int c, s;
88 } ThreadData;
89
90 #define OFFSET(x) offsetof(RotContext, x)
91 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
92
93 static const AVOption rotate_options[] = {
94     { "angle",     "set angle (in radians)",       OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
95     { "a",         "set angle (in radians)",       OFFSET(angle_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
96     { "out_w",     "set output width expression",  OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
97     { "ow",        "set output width expression",  OFFSET(outw_expr_str), AV_OPT_TYPE_STRING, {.str="iw"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
98     { "out_h",     "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
99     { "oh",        "set output height expression", OFFSET(outh_expr_str), AV_OPT_TYPE_STRING, {.str="ih"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
100     { "fillcolor", "set background fill color",    OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
101     { "c",         "set background fill color",    OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, .flags=FLAGS },
102     { "bilinear",  "use bilinear interpolation",   OFFSET(use_bilinear),  AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags=FLAGS },
103     { NULL }
104 };
105
106 AVFILTER_DEFINE_CLASS(rotate);
107
108 static av_cold int init(AVFilterContext *ctx)
109 {
110     RotContext *rot = ctx->priv;
111
112     if (!strcmp(rot->fillcolor_str, "none"))
113         rot->fillcolor_enable = 0;
114     else if (av_parse_color(rot->fillcolor, rot->fillcolor_str, -1, ctx) >= 0)
115         rot->fillcolor_enable = 1;
116     else
117         return AVERROR(EINVAL);
118     return 0;
119 }
120
121 static av_cold void uninit(AVFilterContext *ctx)
122 {
123     RotContext *rot = ctx->priv;
124
125     av_expr_free(rot->angle_expr);
126     rot->angle_expr = NULL;
127 }
128
129 static int query_formats(AVFilterContext *ctx)
130 {
131     static enum PixelFormat pix_fmts[] = {
132         AV_PIX_FMT_GBRP,   AV_PIX_FMT_GBRAP,
133         AV_PIX_FMT_ARGB,   AV_PIX_FMT_RGBA,
134         AV_PIX_FMT_ABGR,   AV_PIX_FMT_BGRA,
135         AV_PIX_FMT_0RGB,   AV_PIX_FMT_RGB0,
136         AV_PIX_FMT_0BGR,   AV_PIX_FMT_BGR0,
137         AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
138         AV_PIX_FMT_GRAY8,
139         AV_PIX_FMT_YUV410P,
140         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P,
141         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUVJ420P,
142         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
143         AV_PIX_FMT_NONE
144     };
145
146     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
147     return 0;
148 }
149
150 static double get_rotated_w(void *opaque, double angle)
151 {
152     RotContext *rot = opaque;
153     double inw = rot->var_values[VAR_IN_W];
154     double inh = rot->var_values[VAR_IN_H];
155     float sinx = sin(angle);
156     float cosx = cos(angle);
157
158     return FFMAX(0, inh * sinx) + FFMAX(0, -inw * cosx) +
159            FFMAX(0, inw * cosx) + FFMAX(0, -inh * sinx);
160 }
161
162 static double get_rotated_h(void *opaque, double angle)
163 {
164     RotContext *rot = opaque;
165     double inw = rot->var_values[VAR_IN_W];
166     double inh = rot->var_values[VAR_IN_H];
167     float sinx = sin(angle);
168     float cosx = cos(angle);
169
170     return FFMAX(0, -inh * cosx) + FFMAX(0, -inw * sinx) +
171            FFMAX(0,  inh * cosx) + FFMAX(0,  inw * sinx);
172 }
173
174 static double (* const func1[])(void *, double) = {
175     get_rotated_w,
176     get_rotated_h,
177     NULL
178 };
179
180 static const char * const func1_names[] = {
181     "rotw",
182     "roth",
183     NULL
184 };
185
186 static int config_props(AVFilterLink *outlink)
187 {
188     AVFilterContext *ctx = outlink->src;
189     RotContext *rot = ctx->priv;
190     AVFilterLink *inlink = ctx->inputs[0];
191     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
192     int ret;
193     double res;
194     char *expr;
195
196     ff_draw_init(&rot->draw, inlink->format, 0);
197     ff_draw_color(&rot->draw, &rot->color, rot->fillcolor);
198
199     rot->hsub = pixdesc->log2_chroma_w;
200     rot->vsub = pixdesc->log2_chroma_h;
201
202     rot->var_values[VAR_IN_W] = rot->var_values[VAR_IW] = inlink->w;
203     rot->var_values[VAR_IN_H] = rot->var_values[VAR_IH] = inlink->h;
204     rot->var_values[VAR_HSUB] = 1<<rot->hsub;
205     rot->var_values[VAR_VSUB] = 1<<rot->vsub;
206     rot->var_values[VAR_N] = NAN;
207     rot->var_values[VAR_T] = NAN;
208     rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = NAN;
209     rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = NAN;
210
211     av_expr_free(rot->angle_expr);
212     rot->angle_expr = NULL;
213     if ((ret = av_expr_parse(&rot->angle_expr, expr = rot->angle_expr_str, var_names,
214                              func1_names, func1, NULL, NULL, 0, ctx)) < 0) {
215         av_log(ctx, AV_LOG_ERROR,
216                "Error occurred parsing angle expression '%s'\n", rot->angle_expr_str);
217         return ret;
218     }
219
220 #define SET_SIZE_EXPR(name, opt_name) do {                                         \
221     ret = av_expr_parse_and_eval(&res, expr = rot->name##_expr_str,                \
222                                  var_names, rot->var_values,                       \
223                                  func1_names, func1, NULL, NULL, rot, 0, ctx);     \
224     if (ret < 0 || isnan(res) || isinf(res) || res <= 0) {                         \
225         av_log(ctx, AV_LOG_ERROR,                                                  \
226                "Error parsing or evaluating expression for option %s: "            \
227                "invalid expression '%s' or non-positive or indefinite value %f\n", \
228                opt_name, expr, res);                                               \
229         return ret;                                                                \
230     }                                                                              \
231 } while (0)
232
233     /* evaluate width and height */
234     av_expr_parse_and_eval(&res, expr = rot->outw_expr_str, var_names, rot->var_values,
235                            func1_names, func1, NULL, NULL, rot, 0, ctx);
236     rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
237     rot->outw = res + 0.5;
238     SET_SIZE_EXPR(outh, "out_w");
239     rot->var_values[VAR_OUT_H] = rot->var_values[VAR_OH] = res;
240     rot->outh = res + 0.5;
241
242     /* evaluate the width again, as it may depend on the evaluated output height */
243     SET_SIZE_EXPR(outw, "out_h");
244     rot->var_values[VAR_OUT_W] = rot->var_values[VAR_OW] = res;
245     rot->outw = res + 0.5;
246
247     /* compute number of planes */
248     rot->nb_planes = av_pix_fmt_count_planes(inlink->format);
249     outlink->w = rot->outw;
250     outlink->h = rot->outh;
251     return 0;
252 }
253
254 #define FIXP (1<<16)
255 #define INT_PI 205887 //(M_PI * FIXP)
256
257 /**
258  * Compute the sin of a using integer values.
259  * Input and output values are scaled by FIXP.
260  */
261 static int64_t int_sin(int64_t a)
262 {
263     int64_t a2, res = 0;
264     int i;
265     if (a < 0) a = INT_PI-a; // 0..inf
266     a %= 2 * INT_PI;         // 0..2PI
267
268     if (a >= INT_PI*3/2) a -= 2*INT_PI;  // -PI/2 .. 3PI/2
269     if (a >= INT_PI/2  ) a = INT_PI - a; // -PI/2 ..  PI/2
270
271     /* compute sin using Taylor series approximated to the third term */
272     a2 = (a*a)/FIXP;
273     for (i = 2; i < 7; i += 2) {
274         res += a;
275         a = -a*a2 / (FIXP*i*(i+1));
276     }
277     return res;
278 }
279
280 /**
281  * Interpolate the color in src at position x and y using bilinear
282  * interpolation.
283  */
284 static uint8_t *interpolate_bilinear(uint8_t *dst_color,
285                                      const uint8_t *src, int src_linesize, int src_linestep,
286                                      int x, int y, int max_x, int max_y)
287 {
288     int int_x = av_clip(x>>16, 0, max_x);
289     int int_y = av_clip(y>>16, 0, max_y);
290     int frac_x = x&0xFFFF;
291     int frac_y = y&0xFFFF;
292     int i;
293     int int_x1 = FFMIN(int_x+1, max_x);
294     int int_y1 = FFMIN(int_y+1, max_y);
295
296     for (i = 0; i < src_linestep; i++) {
297         int s00 = src[src_linestep * int_x  + i + src_linesize * int_y ];
298         int s01 = src[src_linestep * int_x1 + i + src_linesize * int_y ];
299         int s10 = src[src_linestep * int_x  + i + src_linesize * int_y1];
300         int s11 = src[src_linestep * int_x1 + i + src_linesize * int_y1];
301         int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
302         int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
303
304         dst_color[i] = ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32;
305     }
306
307     return dst_color;
308 }
309
310 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
311
312 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
313 {
314     ThreadData *td = arg;
315     AVFrame *in = td->in;
316     AVFrame *out = td->out;
317     RotContext *rot = ctx->priv;
318     const int outw = td->outw, outh = td->outh;
319     const int inw = td->inw, inh = td->inh;
320     const int plane = td->plane;
321     const int xi = td->xi, yi = td->yi;
322     const int c = td->c, s = td->s;
323     const int start = (outh *  job   ) / nb_jobs;
324     const int end   = (outh * (job+1)) / nb_jobs;
325     int xprime = td->xprime + start * s;
326     int yprime = td->yprime + start * c;
327     int i, j, x, y;
328
329     for (j = start; j < end; j++) {
330         x = xprime + xi + FIXP*inw/2;
331         y = yprime + yi + FIXP*inh/2;
332
333         for (i = 0; i < outw; i++) {
334             int32_t v;
335             int x1, y1;
336             uint8_t *pin, *pout;
337             x += c;
338             y -= s;
339             x1 = x>>16;
340             y1 = y>>16;
341
342             /* the out-of-range values avoid border artifacts */
343             if (x1 >= -1 && x1 <= inw && y1 >= -1 && y1 <= inh) {
344                 uint8_t inp_inv[4]; /* interpolated input value */
345                 pout = out->data[plane] + j * out->linesize[plane] + i * rot->draw.pixelstep[plane];
346                 if (rot->use_bilinear) {
347                     pin = interpolate_bilinear(inp_inv,
348                                                in->data[plane], in->linesize[plane], rot->draw.pixelstep[plane],
349                                                x, y, inw-1, inh-1);
350                 } else {
351                     int x2 = av_clip(x1, 0, inw-1);
352                     int y2 = av_clip(y1, 0, inh-1);
353                     pin = in->data[plane] + y2 * in->linesize[plane] + x2 * rot->draw.pixelstep[plane];
354                 }
355                 switch (rot->draw.pixelstep[plane]) {
356                 case 1:
357                     *pout = *pin;
358                     break;
359                 case 2:
360                     *((uint16_t *)pout) = *((uint16_t *)pin);
361                     break;
362                 case 3:
363                     v = AV_RB24(pin);
364                     AV_WB24(pout, v);
365                     break;
366                 case 4:
367                     *((uint32_t *)pout) = *((uint32_t *)pin);
368                     break;
369                 default:
370                     memcpy(pout, pin, rot->draw.pixelstep[plane]);
371                     break;
372                 }
373             }
374         }
375         xprime += s;
376         yprime += c;
377     }
378
379     return 0;
380 }
381
382 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
383 {
384     AVFilterContext *ctx = inlink->dst;
385     AVFilterLink *outlink = ctx->outputs[0];
386     AVFrame *out;
387     RotContext *rot = ctx->priv;
388     int angle_int, s, c, plane;
389     double res;
390
391     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
392     if (!out) {
393         av_frame_free(&in);
394         return AVERROR(ENOMEM);
395     }
396     av_frame_copy_props(out, in);
397
398     rot->var_values[VAR_N] = inlink->frame_count;
399     rot->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
400     rot->angle = res = av_expr_eval(rot->angle_expr, rot->var_values, rot);
401
402     av_log(ctx, AV_LOG_DEBUG, "n:%f time:%f angle:%f/PI\n",
403            rot->var_values[VAR_N], rot->var_values[VAR_T], rot->angle/M_PI);
404
405     angle_int = res * FIXP;
406     s = int_sin(angle_int);
407     c = int_sin(angle_int + INT_PI/2);
408
409     /* fill background */
410     if (rot->fillcolor_enable)
411         ff_fill_rectangle(&rot->draw, &rot->color, out->data, out->linesize,
412                           0, 0, outlink->w, outlink->h);
413
414     for (plane = 0; plane < rot->nb_planes; plane++) {
415         int hsub = plane == 1 || plane == 2 ? rot->hsub : 0;
416         int vsub = plane == 1 || plane == 2 ? rot->vsub : 0;
417         const int outw = FF_CEIL_RSHIFT(outlink->w, hsub);
418         const int outh = FF_CEIL_RSHIFT(outlink->h, vsub);
419         ThreadData td = { .in = in,   .out  = out,
420                           .inw  = FF_CEIL_RSHIFT(inlink->w, hsub),
421                           .inh  = FF_CEIL_RSHIFT(inlink->h, vsub),
422                           .outh = outh, .outw = outw,
423                           .xi = -outw/2 * c, .yi =  outw/2 * s,
424                           .xprime = -outh/2 * s,
425                           .yprime = -outh/2 * c,
426                           .plane = plane, .c = c, .s = s };
427
428
429         ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outh, ctx->graph->nb_threads));
430     }
431
432     av_frame_free(&in);
433     return ff_filter_frame(outlink, out);
434 }
435
436 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
437                            char *res, int res_len, int flags)
438 {
439     RotContext *rot = ctx->priv;
440     int ret;
441
442     if (!strcmp(cmd, "angle") || !strcmp(cmd, "a")) {
443         AVExpr *old = rot->angle_expr;
444         ret = av_expr_parse(&rot->angle_expr, args, var_names,
445                             NULL, NULL, NULL, NULL, 0, ctx);
446         if (ret < 0) {
447             av_log(ctx, AV_LOG_ERROR,
448                    "Error when parsing the expression '%s' for angle command\n", args);
449             rot->angle_expr = old;
450             return ret;
451         }
452         av_expr_free(old);
453     } else
454         ret = AVERROR(ENOSYS);
455
456     return ret;
457 }
458
459 static const AVFilterPad rotate_inputs[] = {
460     {
461         .name         = "default",
462         .type         = AVMEDIA_TYPE_VIDEO,
463         .filter_frame = filter_frame,
464     },
465     { NULL }
466 };
467
468 static const AVFilterPad rotate_outputs[] = {
469     {
470         .name         = "default",
471         .type         = AVMEDIA_TYPE_VIDEO,
472         .config_props = config_props,
473     },
474     { NULL }
475 };
476
477 AVFilter ff_vf_rotate = {
478     .name          = "rotate",
479     .description   = NULL_IF_CONFIG_SMALL("Rotate the input image."),
480     .priv_size     = sizeof(RotContext),
481     .init          = init,
482     .uninit        = uninit,
483     .query_formats = query_formats,
484     .process_command = process_command,
485     .inputs        = rotate_inputs,
486     .outputs       = rotate_outputs,
487     .priv_class    = &rotate_class,
488     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
489 };