]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_lenscorrection.c
avfilter/vf_lenscorrection: add bilinear interpolation
[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/opt.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/pixdesc.h"
32
33 #include "avfilter.h"
34 #include "internal.h"
35 #include "video.h"
36
37 typedef struct LenscorrectionCtx {
38     const AVClass *av_class;
39     int width;
40     int height;
41     int hsub, vsub;
42     int nb_planes;
43     double cx, cy, k1, k2;
44     int interpolation;
45
46     int32_t *correction[4];
47
48     int (*filter_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
49 } LenscorrectionCtx;
50
51 #define OFFSET(x) offsetof(LenscorrectionCtx, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53 static const AVOption lenscorrection_options[] = {
54     { "cx", "set relative center x", OFFSET(cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
55     { "cy", "set relative center y", OFFSET(cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
56     { "k1", "set quadratic distortion factor", OFFSET(k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
57     { "k2", "set double quadratic distortion factor", OFFSET(k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
58     { "i",  "set interpolation type", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=0}, 0, 64, .flags=FLAGS, "i" },
59     {  "nearest",  "nearest neighbour", 0,                   AV_OPT_TYPE_CONST, {.i64=0},0, 0, .flags=FLAGS, "i" },
60     {  "bilinear", "bilinear",          0,                   AV_OPT_TYPE_CONST, {.i64=1},0, 0, .flags=FLAGS, "i" },
61     { NULL }
62 };
63
64 AVFILTER_DEFINE_CLASS(lenscorrection);
65
66 typedef struct ThreadData {
67     AVFrame *in, *out;
68     int w, h;
69     int plane;
70     int xcenter, ycenter;
71     int32_t *correction;
72 } ThreadData;
73
74 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
75 {
76     ThreadData *td = arg;
77     AVFrame *in = td->in;
78     AVFrame *out = td->out;
79
80     const int w = td->w, h = td->h;
81     const int xcenter = td->xcenter;
82     const int ycenter = td->ycenter;
83     const int start = (h *  job   ) / nb_jobs;
84     const int end   = (h * (job+1)) / nb_jobs;
85     const int plane = td->plane;
86     const int inlinesize = in->linesize[plane];
87     const int outlinesize = out->linesize[plane];
88     const uint8_t *indata = in->data[plane];
89     uint8_t *outrow = out->data[plane] + start * outlinesize;
90     int i;
91     for (i = start; i < end; i++, outrow += outlinesize) {
92         const int off_y = i - ycenter;
93         uint8_t *out = outrow;
94         int j;
95         for (j = 0; j < w; j++) {
96             const int off_x = j - xcenter;
97             const int64_t radius_mult = td->correction[j + i*w];
98             const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24);
99             const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24);
100             const char isvalid = x > 0 && x < w - 1 && y > 0 && y < h - 1;
101             *out++ =  isvalid ? indata[y * inlinesize + x] : 0;
102         }
103     }
104     return 0;
105 }
106
107 static int filter_slice_bilinear(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
108 {
109     ThreadData *td = arg;
110     AVFrame *in = td->in;
111     AVFrame *out = td->out;
112
113     const int64_t max = (1 << 24) - 1;
114     const int64_t add = (1 << 23);
115     const int w = td->w, h = td->h;
116     const int xcenter = td->xcenter;
117     const int ycenter = td->ycenter;
118     const int start = (h *  job   ) / nb_jobs;
119     const int end   = (h * (job+1)) / nb_jobs;
120     const int plane = td->plane;
121     const int inlinesize = in->linesize[plane];
122     const int outlinesize = out->linesize[plane];
123     const uint8_t *indata = in->data[plane];
124     uint8_t *outrow = out->data[plane] + start * outlinesize;
125
126     for (int i = start; i < end; i++, outrow += outlinesize) {
127         const int off_y = i - ycenter;
128         uint8_t *out = outrow;
129
130         for (int j = 0; j < w; j++) {
131             const int off_x = j - xcenter;
132             const int64_t radius_mult = td->correction[j + i*w];
133             const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24);
134             const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24);
135             const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1;
136
137             if (isvalid) {
138                 const int nx = FFMIN(x + 1, w - 1);
139                 const int ny = FFMIN(y + 1, h - 1);
140                 const int64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max);
141                 const int64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max);
142                 const int64_t p0 = indata[ y * inlinesize +  x];
143                 const int64_t p1 = indata[ y * inlinesize + nx];
144                 const int64_t p2 = indata[ny * inlinesize +  x];
145                 const int64_t p3 = indata[ny * inlinesize + nx];
146                 int64_t sum = 0;
147
148                 sum += (max - du) * (max - dv) * p0;
149                 sum += (      du) * (max - dv) * p1;
150                 sum += (max - du) * (      dv) * p2;
151                 sum += (      du) * (      dv) * p3;
152
153                 out[j] = av_clip_uint8((sum + (1LL << 47)) >> 48);
154             } else {
155                 out[j] = 0;
156             }
157         }
158     }
159
160     return 0;
161 }
162
163 static int query_formats(AVFilterContext *ctx)
164 {
165     static const enum AVPixelFormat pix_fmts[] = {
166         AV_PIX_FMT_YUV410P,
167         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P,
168         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUVJ420P,
169         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
170         AV_PIX_FMT_YUV422P,
171         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
172         AV_PIX_FMT_NONE
173     };
174     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
175     if (!fmts_list)
176         return AVERROR(ENOMEM);
177     return ff_set_common_formats(ctx, fmts_list);
178 }
179
180 static av_cold void uninit(AVFilterContext *ctx)
181 {
182     LenscorrectionCtx *rect = ctx->priv;
183     int i;
184
185     for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
186         av_freep(&rect->correction[i]);
187     }
188 }
189
190 static int config_props(AVFilterLink *outlink)
191 {
192     AVFilterContext *ctx = outlink->src;
193     LenscorrectionCtx *rect = ctx->priv;
194     AVFilterLink *inlink = ctx->inputs[0];
195     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
196     rect->hsub = pixdesc->log2_chroma_w;
197     rect->vsub = pixdesc->log2_chroma_h;
198     outlink->w = rect->width = inlink->w;
199     outlink->h = rect->height = inlink->h;
200     rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
201     rect->filter_slice = filter_slice;
202     if (rect->interpolation)
203         rect->filter_slice = filter_slice_bilinear;
204     return 0;
205 }
206
207 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
208 {
209     AVFilterContext *ctx = inlink->dst;
210     AVFilterLink *outlink = ctx->outputs[0];
211     LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv;
212     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
213     int plane;
214
215     if (!out) {
216         av_frame_free(&in);
217         return AVERROR(ENOMEM);
218     }
219
220     av_frame_copy_props(out, in);
221
222     for (plane = 0; plane < rect->nb_planes; ++plane) {
223         int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
224         int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
225         int w = AV_CEIL_RSHIFT(rect->width, hsub);
226         int h = AV_CEIL_RSHIFT(rect->height, vsub);
227         int xcenter = rect->cx * w;
228         int ycenter = rect->cy * h;
229         int k1 = rect->k1 * (1<<24);
230         int k2 = rect->k2 * (1<<24);
231         ThreadData td = {
232             .in = in,
233             .out  = out,
234             .w  = w,
235             .h  = h,
236             .xcenter = xcenter,
237             .ycenter = ycenter,
238             .plane = plane};
239
240         if (!rect->correction[plane]) {
241             int i,j;
242             const int64_t r2inv = (4LL<<60) / (w * w + h * h);
243
244             rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
245             if (!rect->correction[plane])
246                 return AVERROR(ENOMEM);
247             for (j = 0; j < h; j++) {
248                 const int off_y = j - ycenter;
249                 const int off_y2 = off_y * off_y;
250                 for (i = 0; i < w; i++) {
251                     const int off_x = i - xcenter;
252                     const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
253                     const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
254                     const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
255                     rect->correction[plane][j * w + i] = radius_mult;
256                 }
257             }
258         }
259
260         td.correction = rect->correction[plane];
261         ctx->internal->execute(ctx, rect->filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
262     }
263
264     av_frame_free(&in);
265     return ff_filter_frame(outlink, out);
266 }
267
268 static const AVFilterPad lenscorrection_inputs[] = {
269     {
270         .name         = "default",
271         .type         = AVMEDIA_TYPE_VIDEO,
272         .filter_frame = filter_frame,
273     },
274     { NULL }
275 };
276
277 static const AVFilterPad lenscorrection_outputs[] = {
278     {
279         .name         = "default",
280         .type         = AVMEDIA_TYPE_VIDEO,
281         .config_props = config_props,
282     },
283     { NULL }
284 };
285
286 AVFilter ff_vf_lenscorrection = {
287     .name          = "lenscorrection",
288     .description   = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
289     .priv_size     = sizeof(LenscorrectionCtx),
290     .query_formats = query_formats,
291     .inputs        = lenscorrection_inputs,
292     .outputs       = lenscorrection_outputs,
293     .priv_class    = &lenscorrection_class,
294     .uninit        = uninit,
295     .flags         = AVFILTER_FLAG_SLICE_THREADS,
296 };