History

avfilter/vf_lenscorrection: add bilinear interpolation

Paul B Mahol committed 5 years ago

Commit e41a9291 parent 4f5a1bc2 files · +70 −1Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/vf_lenscorrection.c+67−1
@@ -41,7 +41,11 @@ typedef struct LenscorrectionCtx { · 40 unmodified lines
4141 int hsub, vsub;
4242 int nb_planes;
4343 double cx, cy, k1, k2;
44+ int interpolation;
45+
4446 int32_t *correction[4];
47+
48+ int (*filter_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
4549 } LenscorrectionCtx;
4650
4751 #define OFFSET(x) offsetof(LenscorrectionCtx, x)
@@ -51,6 +55,9 @@ static const AVOption lenscorrection_options[] = { · 3 unmodified lines
5155 { "cy", "set relative center y", OFFSET(cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
5256 { "k1", "set quadratic distortion factor", OFFSET(k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
5357 { "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" },
5461 { NULL }
5562 };
5663
@@ -97,6 +104,62 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs) · 40 unmodified lines
97104 return 0;
98105 }
99106
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+
100163 static int query_formats(AVFilterContext *ctx)
101164 {
102165 static const enum AVPixelFormat pix_fmts[] = {
@@ -135,6 +198,9 @@ static int config_props(AVFilterLink *outlink) · 32 unmodified lines
135198 outlink->w = rect->width = inlink->w;
136199 outlink->h = rect->height = inlink->h;
137200 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;
138204 return 0;
139205 }
140206
@@ -192,7 +258,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 51 unmodified lines
192258 }
193259
194260 td.correction = rect->correction[plane];
195− ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
261+ ctx->internal->execute(ctx, rect->filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
196262 }
197263
198264 av_frame_free(&in);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t