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