Support single input for guided filter by adding guidance mode. If the guidance mode is off, single input is required. And edge-preserving smoothing is conducted. If the mode is on, two inputs are needed. The second input serves as the guidance. For this mode, more tasks are supported, such as detail enhancement, dehazing and so on.
Unified · Split
| @@ -12995,8 +12995,6 @@ greyedge=difford=1:minknorm=0:sigma=2 · 12994 unmodified lines | ||
| 12995 | 12995 | |
| 12996 | 12996 | @section guided |
| 12997 | 12997 | Apply guided filter for edge-preserving smoothing, dehazing and so on. |
| 12998 | −This filter requires two inputs of same resolution and pixel format. | |
| 12999 | −The second input serves as the reference. | |
| 13000 | 12998 | |
| 13001 | 12999 | The filter accepts the following options: |
| 13002 | 13000 | @table @option |
| @@ -13017,6 +13015,12 @@ Set subsampling ratio for @code{fast} mode. · 14 unmodified lines | ||
| 13017 | 13015 | Range is 2 to 64. Default is 4. |
| 13018 | 13016 | No subsampling occurs in @code{basic} mode. |
| 13019 | 13017 | |
| 13018 | +@item guidance | |
| 13019 | +Set guidance mode. Can be @code{off} or @code{on}. Default is @code{off}. | |
| 13020 | +If @code{off}, single input is required. | |
| 13021 | +If @code{on}, two inputs of the same resolution and pixel format are required. | |
| 13022 | +The second input serves as the guidance. | |
| 13023 | + | |
| 13020 | 13024 | @item planes |
| 13021 | 13025 | Set planes to filter. Default is first only. |
| 13022 | 13026 | @end table |
| @@ -13029,7 +13033,7 @@ This filter supports the all above options as @ref{commands}. · 6 unmodified lines | ||
| 13029 | 13033 | @item |
| 13030 | 13034 | Edge-preserving smoothing with guided filter: |
| 13031 | 13035 | @example |
| 13032 | −ffmpeg -i in.png -i in.png -filter_complex guided out.png | |
| 13036 | +ffmpeg -i in.png -vf guided out.png | |
| 13033 | 13037 | @end example |
| 13034 | 13038 | |
| 13035 | 13039 | @item |
| @@ -13037,7 +13041,7 @@ Dehazing, structure-transferring filtering, detail enhancement with guided filte · 1 unmodified line | ||
| 13037 | 13041 | For the generation of guidance image, refer to paper "Guided Image Filtering". |
| 13038 | 13042 | See: @url{http://kaiminghe.com/publications/pami12guidedfilter.pdf}. |
| 13039 | 13043 | @example |
| 13040 | −ffmpeg -i in.png -i guidance.png -filter_complex guided out.png | |
| 13044 | +ffmpeg -i in.png -i guidance.png -filter_complex guided=guidance=on out.png | |
| 13041 | 13045 | @end example |
| 13042 | 13046 | |
| 13043 | 13047 | @end itemize |
| @@ -22,6 +22,7 @@ · 21 unmodified lines | ||
| 22 | 22 | #include "libavutil/opt.h" |
| 23 | 23 | #include "libavutil/pixdesc.h" |
| 24 | 24 | #include "avfilter.h" |
| 25 | +#include "filters.h" | |
| 25 | 26 | #include "formats.h" |
| 26 | 27 | #include "framesync.h" |
| 27 | 28 | #include "internal.h" |
| @@ -33,6 +34,12 @@ enum FilterModes { · 5 unmodified lines | ||
| 33 | 34 | NB_MODES, |
| 34 | 35 | }; |
| 35 | 36 | |
| 37 | +enum GuidanceModes { | |
| 38 | + OFF, | |
| 39 | + ON, | |
| 40 | + NB_GUIDANCE_MODES, | |
| 41 | +}; | |
| 42 | + | |
| 36 | 43 | typedef struct GuidedContext { |
| 37 | 44 | const AVClass *class; |
| 38 | 45 | FFFrameSync fs; |
| @@ -41,7 +48,7 @@ typedef struct GuidedContext { · 2 unmodified lines | ||
| 41 | 48 | float eps; |
| 42 | 49 | int mode; |
| 43 | 50 | int sub; |
| 44 | − | |
| 51 | + int guidance; | |
| 45 | 52 | int planes; |
| 46 | 53 | |
| 47 | 54 | int width; |
| @@ -59,13 +66,16 @@ typedef struct GuidedContext { · 11 unmodified lines | ||
| 59 | 66 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
| 60 | 67 | |
| 61 | 68 | static const AVOption guided_options[] = { |
| 62 | − { "radius", "set the box radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64 = 3 }, 1, 20, FLAGS }, | |
| 63 | − { "eps", "set the regularization parameter (with square)", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl = 0.01 }, 0.0, 1, FLAGS }, | |
| 64 | − { "mode", "set filtering mode (0: basic mode; 1: fast mode)", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = BASIC}, BASIC, NB_MODES - 1, FLAGS, "mode" }, | |
| 65 | − { "basic", "basic guided filter", 0, AV_OPT_TYPE_CONST, {.i64 = BASIC}, 0, 0, FLAGS, "mode" }, | |
| 66 | − { "fast", "fast guided filter", 0, AV_OPT_TYPE_CONST, {.i64 = FAST }, 0, 0, FLAGS, "mode" }, | |
| 67 | − { "sub", "subsampling ratio for fast mode", OFFSET(sub), AV_OPT_TYPE_INT, {.i64 = 4 }, 2, 64, FLAGS }, | |
| 68 | − { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=1 }, 0, 0xF, FLAGS }, | |
| 69 | + { "radius", "set the box radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64 = 3 }, 1, 20, FLAGS }, | |
| 70 | + { "eps", "set the regularization parameter (with square)", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl = 0.01 }, 0.0, 1, FLAGS }, | |
| 71 | + { "mode", "set filtering mode (0: basic mode; 1: fast mode)", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = BASIC}, BASIC, NB_MODES - 1, FLAGS, "mode" }, | |
| 72 | + { "basic", "basic guided filter", 0, AV_OPT_TYPE_CONST, {.i64 = BASIC}, 0, 0, FLAGS, "mode" }, | |
| 73 | + { "fast", "fast guided filter", 0, AV_OPT_TYPE_CONST, {.i64 = FAST }, 0, 0, FLAGS, "mode" }, | |
| 74 | + { "sub", "subsampling ratio for fast mode", OFFSET(sub), AV_OPT_TYPE_INT, {.i64 = 4 }, 2, 64, FLAGS }, | |
| 75 | + { "guidance", "set guidance mode (0: off mode; 1: on mode)", OFFSET(guidance), AV_OPT_TYPE_INT, {.i64 = OFF }, OFF, NB_GUIDANCE_MODES - 1, FLAGS, "guidance" }, | |
| 76 | + { "off", "only one input is enabled", 0, AV_OPT_TYPE_CONST, {.i64 = OFF }, 0, 0, FLAGS, "guidance" }, | |
| 77 | + { "on", "two inputs are required", 0, AV_OPT_TYPE_CONST, {.i64 = ON }, 0, 0, FLAGS, "guidance" }, | |
| 78 | + { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 0xF, FLAGS }, | |
| 69 | 79 | { NULL } |
| 70 | 80 | }; |
| 71 | 81 | |
| @@ -149,16 +159,6 @@ static int config_input(AVFilterLink *inlink) · 77 unmodified lines | ||
| 149 | 159 | GuidedContext *s = ctx->priv; |
| 150 | 160 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
| 151 | 161 | |
| 152 | − if (ctx->inputs[0]->w != ctx->inputs[1]->w || | |
| 153 | − ctx->inputs[0]->h != ctx->inputs[1]->h) { | |
| 154 | − av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n"); | |
| 155 | − return AVERROR(EINVAL); | |
| 156 | − } | |
| 157 | − if (ctx->inputs[0]->format != ctx->inputs[1]->format) { | |
| 158 | − av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n"); | |
| 159 | − return AVERROR(EINVAL); | |
| 160 | − } | |
| 161 | − | |
| 162 | 162 | if (s->mode == BASIC) { |
| 163 | 163 | s->sub = 1; |
| 164 | 164 | } |
| @@ -230,7 +230,7 @@ static int guided_##name(AVFilterContext *ctx, GuidedContext *s, · 65 unmodified lines | ||
| 230 | 230 | meanB = av_calloc(w * h, sizeof(float)); \ |
| 231 | 231 | \ |
| 232 | 232 | if (!I || !II || !P || !IP || !meanI || !meanII || !meanP || \ |
| 233 | − !meanIP || !A || !B || !meanA || !meanB){ \ | |
| 233 | + !meanIP || !A || !B || !meanA || !meanB) { \ | |
| 234 | 234 | ret = AVERROR(ENOMEM); \ |
| 235 | 235 | goto end; \ |
| 236 | 236 | } \ |
| @@ -304,47 +304,54 @@ end: · 67 unmodified lines | ||
| 304 | 304 | GUIDED(uint8_t, byte) |
| 305 | 305 | GUIDED(uint16_t, word) |
| 306 | 306 | |
| 307 | −static int process_frame(FFFrameSync *fs) | |
| 307 | +static int filter_frame(AVFilterContext *ctx, AVFrame **out, AVFrame *in, AVFrame *ref) | |
| 308 | 308 | { |
| 309 | − AVFilterContext *ctx = fs->parent; | |
| 310 | − GuidedContext *s = fs->opaque; | |
| 309 | + GuidedContext *s = ctx->priv; | |
| 311 | 310 | AVFilterLink *outlink = ctx->outputs[0]; |
| 312 | − AVFrame *out_frame = NULL, *main_frame = NULL, *ref_frame = NULL; | |
| 313 | − int ret; | |
| 314 | − | |
| 315 | − ret = ff_framesync_dualinput_get(fs, &main_frame, &ref_frame); | |
| 316 | − if (ret < 0) | |
| 317 | − return ret; | |
| 318 | − | |
| 319 | − out_frame = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 320 | − if (!out_frame) { | |
| 321 | − av_frame_free(&main_frame); | |
| 311 | + *out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 312 | + if (!*out) | |
| 322 | 313 | return AVERROR(ENOMEM); |
| 323 | − } | |
| 324 | − av_frame_copy_props(out_frame, main_frame); | |
| 314 | + av_frame_copy_props(*out, in); | |
| 325 | 315 | |
| 326 | 316 | for (int plane = 0; plane < s->nb_planes; plane++) { |
| 327 | 317 | if (!(s->planes & (1 << plane))) { |
| 328 | − av_image_copy_plane(out_frame->data[plane], out_frame->linesize[plane], | |
| 329 | − main_frame->data[plane], main_frame->linesize[plane], | |
| 318 | + av_image_copy_plane((*out)->data[plane], (*out)->linesize[plane], | |
| 319 | + in->data[plane], in->linesize[plane], | |
| 330 | 320 | s->planewidth[plane] * ((s->depth + 7) / 8), s->planeheight[plane]); |
| 331 | 321 | continue; |
| 332 | 322 | } |
| 333 | 323 | if (s->depth <= 8) |
| 334 | − guided_byte(ctx, s, main_frame->data[plane], ref_frame->data[plane], out_frame->data[plane], s->radius, s->eps, | |
| 324 | + guided_byte(ctx, s, in->data[plane], ref->data[plane], (*out)->data[plane], s->radius, s->eps, | |
| 335 | 325 | s->planewidth[plane], s->planeheight[plane], |
| 336 | − main_frame->linesize[plane], ref_frame->linesize[plane], out_frame->linesize[plane], (1 << s->depth) - 1.f); | |
| 326 | + in->linesize[plane], ref->linesize[plane], (*out)->linesize[plane], (1 << s->depth) - 1.f); | |
| 337 | 327 | else |
| 338 | − guided_word(ctx, s, main_frame->data[plane], ref_frame->data[plane], out_frame->data[plane], s->radius, s->eps, | |
| 328 | + guided_word(ctx, s, in->data[plane], ref->data[plane], (*out)->data[plane], s->radius, s->eps, | |
| 339 | 329 | s->planewidth[plane], s->planeheight[plane], |
| 340 | − main_frame->linesize[plane] / 2, ref_frame->linesize[plane] / 2, out_frame->linesize[plane] / 2, (1 << s->depth) - 1.f); | |
| 330 | + in->linesize[plane] / 2, ref->linesize[plane] / 2, (*out)->linesize[plane] / 2, (1 << s->depth) - 1.f); | |
| 331 | + } | |
| 332 | + | |
| 333 | + return 0; | |
| 334 | +} | |
| 335 | + | |
| 336 | +static int process_frame(FFFrameSync *fs) | |
| 337 | +{ | |
| 338 | + AVFilterContext *ctx = fs->parent; | |
| 339 | + AVFilterLink *outlink = ctx->outputs[0]; | |
| 340 | + AVFrame *out_frame = NULL, *main_frame = NULL, *ref_frame = NULL; | |
| 341 | + int ret; | |
| 342 | + ret = ff_framesync_dualinput_get(fs, &main_frame, &ref_frame); | |
| 343 | + if (ret < 0) | |
| 344 | + return ret; | |
| 345 | + | |
| 346 | + ret = filter_frame(ctx, &out_frame, main_frame, ref_frame); | |
| 347 | + if (ret < 0) { | |
| 348 | + return ret; | |
| 341 | 349 | } |
| 342 | 350 | av_frame_free(&main_frame); |
| 343 | 351 | |
| 344 | 352 | return ff_filter_frame(outlink, out_frame); |
| 345 | 353 | } |
| 346 | 354 | |
| 347 | − | |
| 348 | 355 | static int config_output(AVFilterLink *outlink) |
| 349 | 356 | { |
| 350 | 357 | AVFilterContext *ctx = outlink->src; |
| @@ -354,12 +361,27 @@ static int config_output(AVFilterLink *outlink) · 3 unmodified lines | ||
| 354 | 361 | FFFrameSyncIn *in; |
| 355 | 362 | int ret; |
| 356 | 363 | |
| 364 | + if (s->guidance == ON) { | |
| 365 | + if (ctx->inputs[0]->w != ctx->inputs[1]->w || | |
| 366 | + ctx->inputs[0]->h != ctx->inputs[1]->h) { | |
| 367 | + av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n"); | |
| 368 | + return AVERROR(EINVAL); | |
| 369 | + } | |
| 370 | + if (ctx->inputs[0]->format != ctx->inputs[1]->format) { | |
| 371 | + av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n"); | |
| 372 | + return AVERROR(EINVAL); | |
| 373 | + } | |
| 374 | + } | |
| 357 | 375 | |
| 358 | 376 | outlink->w = mainlink->w; |
| 359 | 377 | outlink->h = mainlink->h; |
| 360 | 378 | outlink->time_base = mainlink->time_base; |
| 361 | 379 | outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio; |
| 362 | 380 | outlink->frame_rate = mainlink->frame_rate; |
| 381 | + | |
| 382 | + if (s->guidance == OFF) | |
| 383 | + return 0; | |
| 384 | + | |
| 363 | 385 | if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0) |
| 364 | 386 | return ret; |
| 365 | 387 | |
| @@ -383,22 +405,66 @@ static int config_output(AVFilterLink *outlink) · 17 unmodified lines | ||
| 383 | 405 | static int activate(AVFilterContext *ctx) |
| 384 | 406 | { |
| 385 | 407 | GuidedContext *s = ctx->priv; |
| 386 | − return ff_framesync_activate(&s->fs); | |
| 408 | + AVFrame *frame = NULL; | |
| 409 | + AVFrame *out = NULL; | |
| 410 | + int ret, status; | |
| 411 | + int64_t pts; | |
| 412 | + if (s->guidance) | |
| 413 | + return ff_framesync_activate(&s->fs); | |
| 414 | + | |
| 415 | + FF_FILTER_FORWARD_STATUS_BACK(ctx->outputs[0], ctx->inputs[0]); | |
| 416 | + | |
| 417 | + if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &frame)) > 0) { | |
| 418 | + ret = filter_frame(ctx, &out, frame, frame); | |
| 419 | + av_frame_free(&frame); | |
| 420 | + if (ret < 0) | |
| 421 | + return ret; | |
| 422 | + ret = ff_filter_frame(ctx->outputs[0], out); | |
| 423 | + } | |
| 424 | + if (ret < 0) | |
| 425 | + return ret; | |
| 426 | + if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) { | |
| 427 | + ff_outlink_set_status(ctx->outputs[0], status, pts); | |
| 428 | + return 0; | |
| 429 | + } | |
| 430 | + if (ff_outlink_frame_wanted(ctx->outputs[0])) | |
| 431 | + ff_inlink_request_frame(ctx->inputs[0]); | |
| 432 | + return 0; | |
| 387 | 433 | } |
| 388 | 434 | |
| 389 | 435 | static av_cold int init(AVFilterContext *ctx) |
| 390 | 436 | { |
| 437 | + GuidedContext *s = ctx->priv; | |
| 438 | + AVFilterPad pad = { 0 }; | |
| 439 | + int ret; | |
| 440 | + | |
| 441 | + pad.type = AVMEDIA_TYPE_VIDEO; | |
| 442 | + pad.name = "source"; | |
| 443 | + pad.config_props = config_input; | |
| 444 | + | |
| 445 | + if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0) | |
| 446 | + return ret; | |
| 447 | + | |
| 448 | + if (s->guidance == ON) { | |
| 449 | + pad.type = AVMEDIA_TYPE_VIDEO; | |
| 450 | + pad.name = "guidance"; | |
| 451 | + pad.config_props = NULL; | |
| 452 | + | |
| 453 | + if ((ret = ff_insert_inpad(ctx, 1, &pad)) < 0) | |
| 454 | + return ret; | |
| 455 | + } | |
| 456 | + | |
| 391 | 457 | return 0; |
| 392 | 458 | } |
| 393 | 459 | |
| 394 | 460 | static av_cold void uninit(AVFilterContext *ctx) |
| 395 | 461 | { |
| 396 | 462 | GuidedContext *s = ctx->priv; |
| 397 | − ff_framesync_uninit(&s->fs); | |
| 463 | + if (s->guidance == ON) | |
| 464 | + ff_framesync_uninit(&s->fs); | |
| 398 | 465 | return; |
| 399 | 466 | } |
| 400 | 467 | |
| 401 | − | |
| 402 | 468 | static int process_command(AVFilterContext *ctx, |
| 403 | 469 | const char *cmd, |
| 404 | 470 | const char *arg, |
| @@ -414,18 +480,6 @@ static int process_command(AVFilterContext *ctx, · 9 unmodified lines | ||
| 414 | 480 | return 0; |
| 415 | 481 | } |
| 416 | 482 | |
| 417 | −static const AVFilterPad guided_inputs[] = { | |
| 418 | − { | |
| 419 | − .name = "main", | |
| 420 | − .type = AVMEDIA_TYPE_VIDEO, | |
| 421 | − },{ | |
| 422 | − .name = "reference", | |
| 423 | − .type = AVMEDIA_TYPE_VIDEO, | |
| 424 | − .config_props = config_input, | |
| 425 | − }, | |
| 426 | − { NULL } | |
| 427 | −}; | |
| 428 | − | |
| 429 | 483 | static const AVFilterPad guided_outputs[] = { |
| 430 | 484 | { |
| 431 | 485 | .name = "default", |
| @@ -444,7 +498,7 @@ const AVFilter ff_vf_guided = { · 12 unmodified lines | ||
| 444 | 498 | .priv_size = sizeof(GuidedContext), |
| 445 | 499 | .priv_class = &guided_class, |
| 446 | 500 | .activate = activate, |
| 447 | − .inputs = guided_inputs, | |
| 501 | + .inputs = NULL, | |
| 448 | 502 | .outputs = guided_outputs, |
| 449 | 503 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, |
| 450 | 504 | .process_command = process_command, |