| @@ -54,7 +54,7 @@ typedef struct LenscorrectionCtx { · 53 unmodified lines |
| 54 | 54 | } LenscorrectionCtx; |
| 55 | 55 | |
| 56 | 56 | #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 |
| 58 | 58 | static const AVOption lenscorrection_options[] = { |
| 59 | 59 | { "cx", "set relative center x", OFFSET(cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS }, |
| 60 | 60 | { "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 |
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | 226 | |
| 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) |
| 228 | 254 | { |
| 229 | 255 | AVFilterContext *ctx = outlink->src; |
| 230 | 256 | LenscorrectionCtx *rect = ctx->priv; |
| @@ -257,7 +283,21 @@ static int config_props(AVFilterLink *outlink) · 26 unmodified lines |
| 257 | 283 | rect->fill_color[2] = RGB_TO_V_BT709(rect->fill_rgba[0], rect->fill_rgba[1], rect->fill_rgba[2], 0) * factor; |
| 258 | 284 | rect->fill_color[3] = rect->fill_rgba[3] * factor; |
| 259 | 285 | } |
| 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; |
| 261 | 301 | } |
| 262 | 302 | |
| 263 | 303 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
| @@ -282,8 +322,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 18 unmodified lines |
| 282 | 322 | int h = AV_CEIL_RSHIFT(rect->height, vsub); |
| 283 | 323 | int xcenter = rect->cx * w; |
| 284 | 324 | int ycenter = rect->cy * h; |
| 285 | | − int k1 = rect->k1 * (1<<24); |
| 286 | | − int k2 = rect->k2 * (1<<24); |
| 287 | 325 | ThreadData td = { |
| 288 | 326 | .in = in, |
| 289 | 327 | .out = out, |
| @@ -294,29 +332,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 4 unmodified lines |
| 294 | 332 | .plane = plane, |
| 295 | 333 | .depth = rect->depth, |
| 296 | 334 | .fill_color = rect->fill_color[plane], |
| 335 | + .correction = rect->correction[plane], |
| 297 | 336 | }; |
| 298 | 337 | |
| 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]; |
| 320 | 338 | ctx->internal->execute(ctx, rect->filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx))); |
| 321 | 339 | } |
| 322 | 340 | |
| @@ -324,6 +342,21 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 1 unmodified line |
| 324 | 342 | return ff_filter_frame(outlink, out); |
| 325 | 343 | } |
| 326 | 344 | |
| 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 | + |
| 327 | 360 | static const AVFilterPad lenscorrection_inputs[] = { |
| 328 | 361 | { |
| 329 | 362 | .name = "default", |
| @@ -337,7 +370,7 @@ static const AVFilterPad lenscorrection_outputs[] = { · 7 unmodified lines |
| 337 | 370 | { |
| 338 | 371 | .name = "default", |
| 339 | 372 | .type = AVMEDIA_TYPE_VIDEO, |
| 340 | | − .config_props = config_props, |
| 373 | + .config_props = config_output, |
| 341 | 374 | }, |
| 342 | 375 | { NULL } |
| 343 | 376 | }; |
| @@ -352,4 +385,5 @@ AVFilter ff_vf_lenscorrection = { · 8 unmodified lines |
| 352 | 385 | .priv_class = &lenscorrection_class, |
| 353 | 386 | .uninit = uninit, |
| 354 | 387 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, |
| 388 | + .process_command = process_command, |
| 355 | 389 | }; |