]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_lenscorrection.c
avfilter/vf_lenscorrection: reduce overhead calling execute for every plane
[ffmpeg] / libavfilter / vf_lenscorrection.c
1 /*
2  * Copyright (C) 2007 Richard Spindler (author of frei0r plugin from which this was derived)
3  * Copyright (C) 2014 Daniel Oberhoff
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  * Lenscorrection filter, algorithm from the frei0r plugin with the same name
25 */
26 #include <stdlib.h>
27 #include <math.h>
28
29 #include "libavutil/colorspace.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/intreadwrite.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 typedef struct LenscorrectionCtx {
40     const AVClass *av_class;
41     int width;
42     int height;
43     int planewidth[4];
44     int planeheight[4];
45     int depth;
46     int nb_planes;
47     double cx, cy, k1, k2;
48     int interpolation;
49     uint8_t fill_rgba[4];
50     int fill_color[4];
51
52     int32_t *correction[4];
53
54     int (*filter_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs, int plane);
55 } LenscorrectionCtx;
56
57 #define OFFSET(x) offsetof(LenscorrectionCtx, x)
58 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
59 static const AVOption lenscorrection_options[] = {
60     { "cx", "set relative center x", OFFSET(cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
61     { "cy", "set relative center y", OFFSET(cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
62     { "k1", "set quadratic distortion factor", OFFSET(k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
63     { "k2", "set double quadratic distortion factor", OFFSET(k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
64     { "i",  "set interpolation type", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=0}, 0, 64, .flags=FLAGS, "i" },
65     {  "nearest",  "nearest neighbour", 0,                   AV_OPT_TYPE_CONST, {.i64=0},0, 0, .flags=FLAGS, "i" },
66     {  "bilinear", "bilinear",          0,                   AV_OPT_TYPE_CONST, {.i64=1},0, 0, .flags=FLAGS, "i" },
67     { "fc", "set the color of the unmapped pixels", OFFSET(fill_rgba), AV_OPT_TYPE_COLOR, {.str="black@0"}, .flags = FLAGS },
68     { NULL }
69 };
70
71 AVFILTER_DEFINE_CLASS(lenscorrection);
72
73 typedef struct ThreadData {
74     AVFrame *in, *out;
75 } ThreadData;
76
77 #define NEAREST(type, name)                                                    \
78 static int filter##name##_slice(AVFilterContext *ctx, void *arg, int job,      \
79                                 int nb_jobs, int plane)                        \
80 {                                                                              \
81     LenscorrectionCtx *rect = ctx->priv;                                       \
82     ThreadData *td = arg;                                                      \
83     AVFrame *in = td->in;                                                      \
84     AVFrame *out = td->out;                                                    \
85                                                                                \
86     const int32_t *correction = rect->correction[plane];                       \
87     const int fill_color = rect->fill_color[plane];                            \
88     const int w = rect->planewidth[plane], h = rect->planeheight[plane];       \
89     const int xcenter = rect->cx * w;                                          \
90     const int ycenter = rect->cy * h;                                          \
91     const int start = (h *  job   ) / nb_jobs;                                 \
92     const int end   = (h * (job+1)) / nb_jobs;                                 \
93     const int inlinesize = in->linesize[plane] / sizeof(type);                 \
94     const int outlinesize = out->linesize[plane] / sizeof(type);               \
95     const type *indata = (const type *)in->data[plane];                        \
96     type *outrow = (type *)out->data[plane] + start * outlinesize;             \
97     for (int i = start; i < end; i++, outrow += outlinesize) {                 \
98         const int off_y = i - ycenter;                                         \
99         type *out = outrow;                                                    \
100         for (int j = 0; j < w; j++) {                                          \
101             const int off_x = j - xcenter;                                     \
102             const int64_t radius_mult = correction[j + i*w];                   \
103             const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24);     \
104             const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24);     \
105             const char isvalid = x >= 0 && x < w && y >= 0 && y < h;           \
106             *out++ =  isvalid ? indata[y * inlinesize + x] : fill_color;       \
107         }                                                                      \
108     }                                                                          \
109     return 0;                                                                  \
110 }
111
112
113 NEAREST(uint8_t, 8)
114 NEAREST(uint16_t, 16)
115
116 #define BILINEAR(type, name)                                                   \
117 static int filter##name##_slice_bilinear(AVFilterContext *ctx, void *arg,      \
118                                          int job, int nb_jobs, int plane)      \
119 {                                                                              \
120     LenscorrectionCtx *rect = ctx->priv;                                       \
121     ThreadData *td = arg;                                                      \
122     AVFrame *in = td->in;                                                      \
123     AVFrame *out = td->out;                                                    \
124                                                                                \
125     const int32_t *correction = rect->correction[plane];                       \
126     const int fill_color = rect->fill_color[plane];                            \
127     const int depth = rect->depth;                                             \
128     const uint64_t max = (1 << 24) - 1;                                        \
129     const uint64_t add = (1 << 23);                                            \
130     const int w = rect->planewidth[plane], h = rect->planeheight[plane];       \
131     const int xcenter = rect->cx * w;                                          \
132     const int ycenter = rect->cy * h;                                          \
133     const int start = (h *  job   ) / nb_jobs;                                 \
134     const int end   = (h * (job+1)) / nb_jobs;                                 \
135     const int inlinesize = in->linesize[plane] / sizeof(type);                 \
136     const int outlinesize = out->linesize[plane] / sizeof(type);               \
137     const type *indata = (const type *)in->data[plane];                        \
138     type *outrow = (type *)out->data[plane] + start * outlinesize;             \
139                                                                                \
140     for (int i = start; i < end; i++, outrow += outlinesize) {                 \
141         const int off_y = i - ycenter;                                         \
142         type *out = outrow;                                                    \
143                                                                                \
144         for (int j = 0; j < w; j++) {                                          \
145             const int off_x = j - xcenter;                                     \
146             const int64_t radius_mult = correction[j + i*w];                   \
147             const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24);   \
148             const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24);   \
149             const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1; \
150                                                                                \
151             if (isvalid) {                                                     \
152                 const int nx = FFMIN(x + 1, w - 1);                            \
153                 const int ny = FFMIN(y + 1, h - 1);                            \
154                 const uint64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max); \
155                 const uint64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max); \
156                 const uint64_t p0 = indata[ y * inlinesize +  x];              \
157                 const uint64_t p1 = indata[ y * inlinesize + nx];              \
158                 const uint64_t p2 = indata[ny * inlinesize +  x];              \
159                 const uint64_t p3 = indata[ny * inlinesize + nx];              \
160                 uint64_t sum = 0;                                              \
161                                                                                \
162                 sum += (max - du) * (max - dv) * p0;                           \
163                 sum += (      du) * (max - dv) * p1;                           \
164                 sum += (max - du) * (      dv) * p2;                           \
165                 sum += (      du) * (      dv) * p3;                           \
166                                                                                \
167                 out[j] = av_clip_uintp2_c((sum + (1ULL << 47)) >> 48, depth);  \
168             } else {                                                           \
169                 out[j] = fill_color;                                           \
170             }                                                                  \
171         }                                                                      \
172     }                                                                          \
173                                                                                \
174     return 0;                                                                  \
175 }
176
177 BILINEAR(uint8_t, 8)
178 BILINEAR(uint16_t, 16)
179
180 static int query_formats(AVFilterContext *ctx)
181 {
182     static const enum AVPixelFormat pix_fmts[] = {
183         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
184         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
185         AV_PIX_FMT_GRAY16,
186         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
187         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
188         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
189         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
190         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
191         AV_PIX_FMT_YUVJ411P,
192         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
193         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
194         AV_PIX_FMT_YUV440P10,
195         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
196         AV_PIX_FMT_YUV440P12,
197         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
198         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
199         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
200         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
201         AV_PIX_FMT_YUVA420P,  AV_PIX_FMT_YUVA422P,   AV_PIX_FMT_YUVA444P,
202         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
203         AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
204         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
205         AV_PIX_FMT_GBRAP,     AV_PIX_FMT_GBRAP10,    AV_PIX_FMT_GBRAP12,    AV_PIX_FMT_GBRAP16,
206         AV_PIX_FMT_NONE
207     };
208     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
209     if (!fmts_list)
210         return AVERROR(ENOMEM);
211     return ff_set_common_formats(ctx, fmts_list);
212 }
213
214 static av_cold void uninit(AVFilterContext *ctx)
215 {
216     LenscorrectionCtx *rect = ctx->priv;
217     int i;
218
219     for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
220         av_freep(&rect->correction[i]);
221     }
222 }
223
224 static void calc_correction(AVFilterContext *ctx, int plane)
225 {
226     LenscorrectionCtx *rect = ctx->priv;
227     int w = rect->planewidth[plane];
228     int h = rect->planeheight[plane];
229     int xcenter = rect->cx * w;
230     int ycenter = rect->cy * h;
231     int k1 = rect->k1 * (1<<24);
232     int k2 = rect->k2 * (1<<24);
233     const int64_t r2inv = (4LL<<60) / (w * w + h * h);
234
235     for (int j = 0; j < h; j++) {
236         const int off_y = j - ycenter;
237         const int off_y2 = off_y * off_y;
238         for (int i = 0; i < w; i++) {
239             const int off_x = i - xcenter;
240             const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
241             const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
242             const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
243             rect->correction[plane][j * w + i] = radius_mult;
244         }
245     }
246 }
247
248 static int config_output(AVFilterLink *outlink)
249 {
250     AVFilterContext *ctx = outlink->src;
251     LenscorrectionCtx *rect = ctx->priv;
252     AVFilterLink *inlink = ctx->inputs[0];
253     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
254     int is_rgb = !!(pixdesc->flags & AV_PIX_FMT_FLAG_RGB);
255     uint8_t rgba_map[4];
256     int factor;
257
258     ff_fill_rgba_map(rgba_map, inlink->format);
259     rect->depth = pixdesc->comp[0].depth;
260     factor = 1 << (rect->depth - 8);
261     rect->planeheight[1] = rect->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, pixdesc->log2_chroma_h);
262     rect->planeheight[0] = rect->planeheight[3] = inlink->h;
263     rect->planewidth[1]  = rect->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, pixdesc->log2_chroma_w);
264     rect->planewidth[0]  = rect->planewidth[3]  = inlink->w;
265     outlink->w = rect->width = inlink->w;
266     outlink->h = rect->height = inlink->h;
267     rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
268     rect->filter_slice = rect->depth <= 8 ? filter8_slice : filter16_slice;
269     if (rect->interpolation)
270         rect->filter_slice = rect->depth <= 8 ? filter8_slice_bilinear : filter16_slice_bilinear;
271
272     if (is_rgb) {
273         rect->fill_color[rgba_map[0]] = rect->fill_rgba[0] * factor;
274         rect->fill_color[rgba_map[1]] = rect->fill_rgba[1] * factor;
275         rect->fill_color[rgba_map[2]] = rect->fill_rgba[2] * factor;
276         rect->fill_color[rgba_map[3]] = rect->fill_rgba[3] * factor;
277     } else {
278         rect->fill_color[0] = RGB_TO_Y_BT709(rect->fill_rgba[0], rect->fill_rgba[1], rect->fill_rgba[2]) * factor;
279         rect->fill_color[1] = RGB_TO_U_BT709(rect->fill_rgba[0], rect->fill_rgba[1], rect->fill_rgba[2], 0) * factor;
280         rect->fill_color[2] = RGB_TO_V_BT709(rect->fill_rgba[0], rect->fill_rgba[1], rect->fill_rgba[2], 0) * factor;
281         rect->fill_color[3] = rect->fill_rgba[3] * factor;
282     }
283
284     for (int plane = 0; plane < rect->nb_planes; plane++) {
285         int w = rect->planewidth[plane];
286         int h = rect->planeheight[plane];
287
288         if (!rect->correction[plane])
289             rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
290         if (!rect->correction[plane])
291             return AVERROR(ENOMEM);
292         calc_correction(ctx, plane);
293     }
294
295     return 0;
296 }
297
298 static int filter_slice(AVFilterContext *ctx, void *arg, int job,
299                         int nb_jobs)
300 {
301     LenscorrectionCtx *rect = ctx->priv;
302
303     for (int plane = 0; plane < rect->nb_planes; plane++)
304         rect->filter_slice(ctx, arg, job, nb_jobs, plane);
305
306     return 0;
307 }
308
309 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
310 {
311     AVFilterContext *ctx = inlink->dst;
312     AVFilterLink *outlink = ctx->outputs[0];
313     LenscorrectionCtx *rect = ctx->priv;
314     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
315     ThreadData td;
316
317     if (!out) {
318         av_frame_free(&in);
319         return AVERROR(ENOMEM);
320     }
321
322     av_frame_copy_props(out, in);
323
324     td.in = in; td.out = out;
325     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(rect->planeheight[1], ff_filter_get_nb_threads(ctx)));
326
327     av_frame_free(&in);
328     return ff_filter_frame(outlink, out);
329 }
330
331 static int process_command(AVFilterContext *ctx,
332                            const char *cmd,
333                            const char *arg,
334                            char *res,
335                            int res_len,
336                            int flags)
337 {
338     int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
339
340     if (ret < 0)
341         return ret;
342
343     return config_output(ctx->outputs[0]);
344 }
345
346 static const AVFilterPad lenscorrection_inputs[] = {
347     {
348         .name         = "default",
349         .type         = AVMEDIA_TYPE_VIDEO,
350         .filter_frame = filter_frame,
351     },
352     { NULL }
353 };
354
355 static const AVFilterPad lenscorrection_outputs[] = {
356     {
357         .name         = "default",
358         .type         = AVMEDIA_TYPE_VIDEO,
359         .config_props = config_output,
360     },
361     { NULL }
362 };
363
364 AVFilter ff_vf_lenscorrection = {
365     .name          = "lenscorrection",
366     .description   = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
367     .priv_size     = sizeof(LenscorrectionCtx),
368     .query_formats = query_formats,
369     .inputs        = lenscorrection_inputs,
370     .outputs       = lenscorrection_outputs,
371     .priv_class    = &lenscorrection_class,
372     .uninit        = uninit,
373     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
374     .process_command = process_command,
375 };