History

avfilter/vf_lenscorrection: add support for commands

Paul B Mahol committed 5 years ago

Commit 46f170e1 parent 33f6d8e2 files · +65 −27Browse files

Unified · Split

2 files
  1. doc/filters.texi +4 −0
  2. libavfilter/vf_lenscorrection.c +61 −27
doc/filters.texi+4−0
@@ -13468,6 +13468,10 @@ The formula that generates the correction is: · 13467 unmodified lines
1346813468 where @var{r_0} is halve of the image diagonal and @var{r_src} and @var{r_tgt} are the
1346913469 distances from the focal point in the source and target images, respectively.
1347013470
13471+@subsection Commands
13472+
13473+This filter supports the all above options as @ref{commands}.
13474+
1347113475 @section lensfun
1347213476
1347313477 Apply lens correction via the lensfun library (@url{http://lensfun.sourceforge.net/}).
libavfilter/vf_lenscorrection.c+61−27
@@ -54,7 +54,7 @@ typedef struct LenscorrectionCtx { · 53 unmodified lines
5454 } LenscorrectionCtx;
5555
5656 #define OFFSET(x) offsetof(LenscorrectionCtx, x)
57−#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
57+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
5858 static const AVOption lenscorrection_options[] = {
5959 { "cx", "set relative center x", OFFSET(cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
6060 { "cy", "set relative center y", OFFSET(cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
@@ -224,7 +224,33 @@ static av_cold void uninit(AVFilterContext *ctx) · 163 unmodified lines
224224 }
225225 }
226226
227−static int config_props(AVFilterLink *outlink)
227+static void calc_correction(AVFilterContext *ctx, int plane)
228+{
229+ LenscorrectionCtx *rect = ctx->priv;
230+ int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
231+ int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
232+ int w = AV_CEIL_RSHIFT(rect->width, hsub);
233+ int h = AV_CEIL_RSHIFT(rect->height, vsub);
234+ int xcenter = rect->cx * w;
235+ int ycenter = rect->cy * h;
236+ int k1 = rect->k1 * (1<<24);
237+ int k2 = rect->k2 * (1<<24);
238+ const int64_t r2inv = (4LL<<60) / (w * w + h * h);
239+
240+ for (int j = 0; j < h; j++) {
241+ const int off_y = j - ycenter;
242+ const int off_y2 = off_y * off_y;
243+ for (int i = 0; i < w; i++) {
244+ const int off_x = i - xcenter;
245+ const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
246+ const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
247+ const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
248+ rect->correction[plane][j * w + i] = radius_mult;
249+ }
250+ }
251+}
252+
253+static int config_output(AVFilterLink *outlink)
228254 {
229255 AVFilterContext *ctx = outlink->src;
230256 LenscorrectionCtx *rect = ctx->priv;
@@ -257,7 +283,21 @@ static int config_props(AVFilterLink *outlink) · 26 unmodified lines
257283 rect->fill_color[2] = RGB_TO_V_BT709(rect->fill_rgba[0], rect->fill_rgba[1], rect->fill_rgba[2], 0) * factor;
258284 rect->fill_color[3] = rect->fill_rgba[3] * factor;
259285 }
260− return 0;
286+
287+ for (int plane = 0; plane < rect->nb_planes; plane++) {
288+ int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
289+ int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
290+ int w = AV_CEIL_RSHIFT(rect->width, hsub);
291+ int h = AV_CEIL_RSHIFT(rect->height, vsub);
292+
293+ if (!rect->correction[plane])
294+ rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
295+ if (!rect->correction[plane])
296+ return AVERROR(ENOMEM);
297+ calc_correction(ctx, plane);
298+ }
299+
300+ return 0;
261301 }
262302
263303 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
@@ -282,8 +322,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 18 unmodified lines
282322 int h = AV_CEIL_RSHIFT(rect->height, vsub);
283323 int xcenter = rect->cx * w;
284324 int ycenter = rect->cy * h;
285− int k1 = rect->k1 * (1<<24);
286− int k2 = rect->k2 * (1<<24);
287325 ThreadData td = {
288326 .in = in,
289327 .out = out,
@@ -294,29 +332,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 4 unmodified lines
294332 .plane = plane,
295333 .depth = rect->depth,
296334 .fill_color = rect->fill_color[plane],
335+ .correction = rect->correction[plane],
297336 };
298337
299− if (!rect->correction[plane]) {
300− int i,j;
301− const int64_t r2inv = (4LL<<60) / (w * w + h * h);
302−
303− rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
304− if (!rect->correction[plane])
305− return AVERROR(ENOMEM);
306− for (j = 0; j < h; j++) {
307− const int off_y = j - ycenter;
308− const int off_y2 = off_y * off_y;
309− for (i = 0; i < w; i++) {
310− const int off_x = i - xcenter;
311− const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
312− const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
313− const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
314− rect->correction[plane][j * w + i] = radius_mult;
315− }
316− }
317− }
318−
319− td.correction = rect->correction[plane];
320338 ctx->internal->execute(ctx, rect->filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
321339 }
322340
@@ -324,6 +342,21 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 1 unmodified line
324342 return ff_filter_frame(outlink, out);
325343 }
326344
345+static int process_command(AVFilterContext *ctx,
346+ const char *cmd,
347+ const char *arg,
348+ char *res,
349+ int res_len,
350+ int flags)
351+{
352+ int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
353+
354+ if (ret < 0)
355+ return ret;
356+
357+ return config_output(ctx->outputs[0]);
358+}
359+
327360 static const AVFilterPad lenscorrection_inputs[] = {
328361 {
329362 .name = "default",
@@ -337,7 +370,7 @@ static const AVFilterPad lenscorrection_outputs[] = { · 7 unmodified lines
337370 {
338371 .name = "default",
339372 .type = AVMEDIA_TYPE_VIDEO,
340− .config_props = config_props,
373+ .config_props = config_output,
341374 },
342375 { NULL }
343376 };
@@ -352,4 +385,5 @@ AVFilter ff_vf_lenscorrection = { · 8 unmodified lines
352385 .priv_class = &lenscorrection_class,
353386 .uninit = uninit,
354387 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
388+ .process_command = process_command,
355389 };

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