]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_lenscorrection.c
avfilter/vf_lenscorrection: simplify options
[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     int32_t *correction[4];
45 } LenscorrectionCtx;
46
47 #define OFFSET(x) offsetof(LenscorrectionCtx, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 static const AVOption lenscorrection_options[] = {
50     { "cx", "set relative center x", OFFSET(cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
51     { "cy", "set relative center y", OFFSET(cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
52     { "k1", "set quadratic distortion factor", OFFSET(k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
53     { "k2", "set double quadratic distortion factor", OFFSET(k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
54     { NULL }
55 };
56
57 AVFILTER_DEFINE_CLASS(lenscorrection);
58
59 typedef struct ThreadData {
60     AVFrame *in, *out;
61     int w, h;
62     int plane;
63     int xcenter, ycenter;
64     int32_t *correction;
65 } ThreadData;
66
67 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
68 {
69     ThreadData *td = arg;
70     AVFrame *in = td->in;
71     AVFrame *out = td->out;
72
73     const int w = td->w, h = td->h;
74     const int xcenter = td->xcenter;
75     const int ycenter = td->ycenter;
76     const int start = (h *  job   ) / nb_jobs;
77     const int end   = (h * (job+1)) / nb_jobs;
78     const int plane = td->plane;
79     const int inlinesize = in->linesize[plane];
80     const int outlinesize = out->linesize[plane];
81     const uint8_t *indata = in->data[plane];
82     uint8_t *outrow = out->data[plane] + start * outlinesize;
83     int i;
84     for (i = start; i < end; i++, outrow += outlinesize) {
85         const int off_y = i - ycenter;
86         uint8_t *out = outrow;
87         int j;
88         for (j = 0; j < w; j++) {
89             const int off_x = j - xcenter;
90             const int64_t radius_mult = td->correction[j + i*w];
91             const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24);
92             const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24);
93             const char isvalid = x > 0 && x < w - 1 && y > 0 && y < h - 1;
94             *out++ =  isvalid ? indata[y * inlinesize + x] : 0;
95         }
96     }
97     return 0;
98 }
99
100 static int query_formats(AVFilterContext *ctx)
101 {
102     static const enum AVPixelFormat pix_fmts[] = {
103         AV_PIX_FMT_YUV410P,
104         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P,
105         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUVJ420P,
106         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
107         AV_PIX_FMT_YUV422P,
108         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
109         AV_PIX_FMT_NONE
110     };
111     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
112     if (!fmts_list)
113         return AVERROR(ENOMEM);
114     return ff_set_common_formats(ctx, fmts_list);
115 }
116
117 static av_cold void uninit(AVFilterContext *ctx)
118 {
119     LenscorrectionCtx *rect = ctx->priv;
120     int i;
121
122     for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
123         av_freep(&rect->correction[i]);
124     }
125 }
126
127 static int config_props(AVFilterLink *outlink)
128 {
129     AVFilterContext *ctx = outlink->src;
130     LenscorrectionCtx *rect = ctx->priv;
131     AVFilterLink *inlink = ctx->inputs[0];
132     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
133     rect->hsub = pixdesc->log2_chroma_w;
134     rect->vsub = pixdesc->log2_chroma_h;
135     outlink->w = rect->width = inlink->w;
136     outlink->h = rect->height = inlink->h;
137     rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
138     return 0;
139 }
140
141 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
142 {
143     AVFilterContext *ctx = inlink->dst;
144     AVFilterLink *outlink = ctx->outputs[0];
145     LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv;
146     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
147     int plane;
148
149     if (!out) {
150         av_frame_free(&in);
151         return AVERROR(ENOMEM);
152     }
153
154     av_frame_copy_props(out, in);
155
156     for (plane = 0; plane < rect->nb_planes; ++plane) {
157         int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
158         int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
159         int w = AV_CEIL_RSHIFT(rect->width, hsub);
160         int h = AV_CEIL_RSHIFT(rect->height, vsub);
161         int xcenter = rect->cx * w;
162         int ycenter = rect->cy * h;
163         int k1 = rect->k1 * (1<<24);
164         int k2 = rect->k2 * (1<<24);
165         ThreadData td = {
166             .in = in,
167             .out  = out,
168             .w  = w,
169             .h  = h,
170             .xcenter = xcenter,
171             .ycenter = ycenter,
172             .plane = plane};
173
174         if (!rect->correction[plane]) {
175             int i,j;
176             const int64_t r2inv = (4LL<<60) / (w * w + h * h);
177
178             rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
179             if (!rect->correction[plane])
180                 return AVERROR(ENOMEM);
181             for (j = 0; j < h; j++) {
182                 const int off_y = j - ycenter;
183                 const int off_y2 = off_y * off_y;
184                 for (i = 0; i < w; i++) {
185                     const int off_x = i - xcenter;
186                     const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
187                     const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
188                     const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
189                     rect->correction[plane][j * w + i] = radius_mult;
190                 }
191             }
192         }
193
194         td.correction = rect->correction[plane];
195         ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
196     }
197
198     av_frame_free(&in);
199     return ff_filter_frame(outlink, out);
200 }
201
202 static const AVFilterPad lenscorrection_inputs[] = {
203     {
204         .name         = "default",
205         .type         = AVMEDIA_TYPE_VIDEO,
206         .filter_frame = filter_frame,
207     },
208     { NULL }
209 };
210
211 static const AVFilterPad lenscorrection_outputs[] = {
212     {
213         .name         = "default",
214         .type         = AVMEDIA_TYPE_VIDEO,
215         .config_props = config_props,
216     },
217     { NULL }
218 };
219
220 AVFilter ff_vf_lenscorrection = {
221     .name          = "lenscorrection",
222     .description   = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
223     .priv_size     = sizeof(LenscorrectionCtx),
224     .query_formats = query_formats,
225     .inputs        = lenscorrection_inputs,
226     .outputs       = lenscorrection_outputs,
227     .priv_class    = &lenscorrection_class,
228     .uninit        = uninit,
229     .flags         = AVFILTER_FLAG_SLICE_THREADS,
230 };