| @@ -0,0 +1,429 @@ |
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Xuewei Meng |
| 3 | + * |
| 4 | + * This file is part of FFmpeg. |
| 5 | + * |
| 6 | + * FFmpeg is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 2.1 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * FFmpeg is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with FFmpeg; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +#include "libavutil/imgutils.h" |
| 22 | +#include "libavutil/opt.h" |
| 23 | +#include "libavutil/pixdesc.h" |
| 24 | +#include "avfilter.h" |
| 25 | +#include "formats.h" |
| 26 | +#include "framesync.h" |
| 27 | +#include "internal.h" |
| 28 | +#include "video.h" |
| 29 | + |
| 30 | +typedef struct GuidedContext { |
| 31 | + const AVClass *class; |
| 32 | + FFFrameSync fs; |
| 33 | + |
| 34 | + int radius; |
| 35 | + float eps; |
| 36 | + |
| 37 | + int planes; |
| 38 | + |
| 39 | + int width; |
| 40 | + int height; |
| 41 | + |
| 42 | + int nb_planes; |
| 43 | + int depth; |
| 44 | + int planewidth[4]; |
| 45 | + int planeheight[4]; |
| 46 | + |
| 47 | + int (*box_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
| 48 | +} GuidedContext; |
| 49 | + |
| 50 | +#define OFFSET(x) offsetof(GuidedContext, x) |
| 51 | +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
| 52 | + |
| 53 | +static const AVOption guided_options[] = { |
| 54 | + { "radius", "set the box radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=3 }, 1, 20, FLAGS }, |
| 55 | + { "eps", "set the regularization parameter (with square)", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl=0.01 }, 0.0, 1, FLAGS }, |
| 56 | + { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=1 }, 0, 0xF, FLAGS }, |
| 57 | + { NULL } |
| 58 | +}; |
| 59 | + |
| 60 | +AVFILTER_DEFINE_CLASS(guided); |
| 61 | + |
| 62 | +typedef struct ThreadData { |
| 63 | + int width; |
| 64 | + int height; |
| 65 | + float *src; |
| 66 | + float *dst; |
| 67 | + int srcStride; |
| 68 | + int dstStride; |
| 69 | +} ThreadData; |
| 70 | + |
| 71 | +static int box_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
| 72 | +{ |
| 73 | + GuidedContext *s = ctx->priv; |
| 74 | + ThreadData *t = arg; |
| 75 | + |
| 76 | + const int width = t->width; |
| 77 | + const int height = t->height; |
| 78 | + const int src_stride = t->srcStride; |
| 79 | + const int dst_stride = t->dstStride; |
| 80 | + const int slice_start = (height * jobnr) / nb_jobs; |
| 81 | + const int slice_end = (height * (jobnr + 1)) / nb_jobs; |
| 82 | + const int radius = s->radius; |
| 83 | + const float *src = t->src; |
| 84 | + float *dst = t->dst; |
| 85 | + |
| 86 | + int w; |
| 87 | + int numPix; |
| 88 | + w = (radius << 1) + 1; |
| 89 | + numPix = w * w; |
| 90 | + for (int i = slice_start;i < slice_end;i++) { |
| 91 | + for (int j = 0;j < width;j++) { |
| 92 | + float temp = 0.0; |
| 93 | + for (int row = -radius;row <= radius;row++) { |
| 94 | + for (int col = -radius;col <= radius;col++) { |
| 95 | + int x = i + row; |
| 96 | + int y = j + col; |
| 97 | + x = (x < 0) ? 0 : (x >= height ? height - 1 : x); |
| 98 | + y = (y < 0) ? 0 : (y >= width ? width - 1 : y); |
| 99 | + temp += src[x * src_stride + y]; |
| 100 | + } |
| 101 | + } |
| 102 | + dst[i * dst_stride + j] = temp / numPix; |
| 103 | + } |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +static int query_formats(AVFilterContext *ctx) |
| 109 | +{ |
| 110 | + static const enum AVPixelFormat pix_fmts[] = { |
| 111 | + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, |
| 112 | + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, |
| 113 | + AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, |
| 114 | + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, |
| 115 | + AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, |
| 116 | + AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
| 117 | + AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
| 118 | + AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, |
| 119 | + AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, |
| 120 | + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
| 121 | + AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, |
| 122 | + AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, |
| 123 | + AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, |
| 124 | + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
| 125 | + AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
| 126 | + AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, |
| 127 | + AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, |
| 128 | + AV_PIX_FMT_NONE |
| 129 | + }; |
| 130 | + |
| 131 | + return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
| 132 | +} |
| 133 | + |
| 134 | +static int config_input(AVFilterLink *inlink) |
| 135 | +{ |
| 136 | + AVFilterContext *ctx = inlink->dst; |
| 137 | + GuidedContext *s = ctx->priv; |
| 138 | + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
| 139 | + |
| 140 | + if (ctx->inputs[0]->w != ctx->inputs[1]->w || |
| 141 | + ctx->inputs[0]->h != ctx->inputs[1]->h) { |
| 142 | + av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n"); |
| 143 | + return AVERROR(EINVAL); |
| 144 | + } |
| 145 | + if (ctx->inputs[0]->format != ctx->inputs[1]->format) { |
| 146 | + av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n"); |
| 147 | + return AVERROR(EINVAL); |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + s->depth = desc->comp[0].depth; |
| 152 | + s->width = ctx->inputs[0]->w; |
| 153 | + s->height = ctx->inputs[0]->h; |
| 154 | + |
| 155 | + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); |
| 156 | + s->planewidth[0] = s->planewidth[3] = inlink->w; |
| 157 | + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); |
| 158 | + s->planeheight[0] = s->planeheight[3] = inlink->h; |
| 159 | + |
| 160 | + s->nb_planes = av_pix_fmt_count_planes(inlink->format); |
| 161 | + s->box_slice = box_slice; |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
| 165 | +#define GUIDED(type, name) \ |
| 166 | +static int guided_##name(AVFilterContext *ctx, GuidedContext *s, \ |
| 167 | + const uint8_t *ssrc, const uint8_t *ssrcRef, \ |
| 168 | + uint8_t *ddst, int radius, float eps, int width, int height, \ |
| 169 | + int src_stride, int src_ref_stride, int dst_stride, \ |
| 170 | + float maxval) \ |
| 171 | +{ \ |
| 172 | + int ret = 0; \ |
| 173 | + type *dst = (type *)ddst; \ |
| 174 | + const type *src = (const type *)ssrc; \ |
| 175 | + const type *srcRef = (const type *)ssrcRef; \ |
| 176 | + \ |
| 177 | + ThreadData t; \ |
| 178 | + const int nb_threads = ff_filter_get_nb_threads(ctx); \ |
| 179 | + float *I; \ |
| 180 | + float *II; \ |
| 181 | + float *P; \ |
| 182 | + float *IP; \ |
| 183 | + float *meanI; \ |
| 184 | + float *meanII; \ |
| 185 | + float *meanP; \ |
| 186 | + float *meanIP; \ |
| 187 | + float *A; \ |
| 188 | + float *B; \ |
| 189 | + float *meanA; \ |
| 190 | + float *meanB; \ |
| 191 | + \ |
| 192 | + I = av_calloc(width * height, sizeof(float)); \ |
| 193 | + II = av_calloc(width * height, sizeof(float)); \ |
| 194 | + P = av_calloc(width * height, sizeof(float)); \ |
| 195 | + IP = av_calloc(width * height, sizeof(float)); \ |
| 196 | + meanI = av_calloc(width * height, sizeof(float)); \ |
| 197 | + meanII = av_calloc(width * height, sizeof(float)); \ |
| 198 | + meanP = av_calloc(width * height, sizeof(float)); \ |
| 199 | + meanIP = av_calloc(width * height, sizeof(float)); \ |
| 200 | + \ |
| 201 | + A = av_calloc(width * height, sizeof(float)); \ |
| 202 | + B = av_calloc(width * height, sizeof(float)); \ |
| 203 | + meanA = av_calloc(width * height, sizeof(float)); \ |
| 204 | + meanB = av_calloc(width * height, sizeof(float)); \ |
| 205 | + \ |
| 206 | + if (!I || !II || !P || !IP || !meanI || !meanII || !meanP || \ |
| 207 | + !meanIP || !A || !B || !meanA || !meanB){ \ |
| 208 | + ret = AVERROR(ENOMEM); \ |
| 209 | + goto end; \ |
| 210 | + } \ |
| 211 | + for (int i = 0;i < height;i++) { \ |
| 212 | + for (int j = 0;j < width;j++) { \ |
| 213 | + int x = i * width + j; \ |
| 214 | + I[x] = src[i * src_stride + j] / maxval; \ |
| 215 | + II[x] = I[x] * I[x]; \ |
| 216 | + P[x] = srcRef[i * src_ref_stride + j] / maxval; \ |
| 217 | + IP[x] = I[x] * P[x]; \ |
| 218 | + } \ |
| 219 | + } \ |
| 220 | + \ |
| 221 | + t.width = width; \ |
| 222 | + t.height = height; \ |
| 223 | + t.srcStride = width; \ |
| 224 | + t.dstStride = width; \ |
| 225 | + t.src = I; \ |
| 226 | + t.dst = meanI; \ |
| 227 | + ctx->internal->execute(ctx, s->box_slice, &t, NULL, FFMIN(height, nb_threads)); \ |
| 228 | + t.src = II; \ |
| 229 | + t.dst = meanII; \ |
| 230 | + ctx->internal->execute(ctx, s->box_slice, &t, NULL, FFMIN(height, nb_threads)); \ |
| 231 | + t.src = P; \ |
| 232 | + t.dst = meanP; \ |
| 233 | + ctx->internal->execute(ctx, s->box_slice, &t, NULL, FFMIN(height, nb_threads)); \ |
| 234 | + t.src = IP; \ |
| 235 | + t.dst = meanIP; \ |
| 236 | + ctx->internal->execute(ctx, s->box_slice, &t, NULL, FFMIN(height, nb_threads)); \ |
| 237 | + \ |
| 238 | + for (int i = 0;i < height;i++) { \ |
| 239 | + for (int j = 0;j < width;j++) { \ |
| 240 | + int x = i * width + j; \ |
| 241 | + float varI = meanII[x] - (meanI[x] * meanI[x]); \ |
| 242 | + float covIP = meanIP[x] - (meanI[x] * meanP[x]); \ |
| 243 | + A[x] = covIP / (varI + eps); \ |
| 244 | + B[x] = meanP[x] - A[x] * meanI[x]; \ |
| 245 | + } \ |
| 246 | + } \ |
| 247 | + \ |
| 248 | + t.src = A; \ |
| 249 | + t.dst = meanA; \ |
| 250 | + ctx->internal->execute(ctx, s->box_slice, &t, NULL, FFMIN(height, nb_threads)); \ |
| 251 | + t.src = B; \ |
| 252 | + t.dst = meanB; \ |
| 253 | + ctx->internal->execute(ctx, s->box_slice, &t, NULL, FFMIN(height, nb_threads)); \ |
| 254 | + \ |
| 255 | + for (int i = 0;i < height;i++) { \ |
| 256 | + for (int j = 0;j < width;j++) { \ |
| 257 | + int x = i * width + j; \ |
| 258 | + dst[i * dst_stride + j] = meanA[x] * src[i * src_stride + j] + \ |
| 259 | + meanB[x] * maxval; \ |
| 260 | + } \ |
| 261 | + } \ |
| 262 | +end: \ |
| 263 | + av_freep(&I); \ |
| 264 | + av_freep(&II); \ |
| 265 | + av_freep(&P); \ |
| 266 | + av_freep(&IP); \ |
| 267 | + av_freep(&meanI); \ |
| 268 | + av_freep(&meanII); \ |
| 269 | + av_freep(&meanP); \ |
| 270 | + av_freep(&meanIP); \ |
| 271 | + av_freep(&A); \ |
| 272 | + av_freep(&B); \ |
| 273 | + av_freep(&meanA); \ |
| 274 | + av_freep(&meanB); \ |
| 275 | + return ret; \ |
| 276 | +} |
| 277 | + |
| 278 | +GUIDED(uint8_t, byte) |
| 279 | +GUIDED(uint16_t, word) |
| 280 | + |
| 281 | +static int process_frame(FFFrameSync *fs) |
| 282 | +{ |
| 283 | + AVFilterContext *ctx = fs->parent; |
| 284 | + GuidedContext *s = fs->opaque; |
| 285 | + AVFilterLink *outlink = ctx->outputs[0]; |
| 286 | + AVFrame *out_frame = NULL, *main_frame = NULL, *ref_frame = NULL; |
| 287 | + int ret; |
| 288 | + |
| 289 | + ret = ff_framesync_dualinput_get(fs, &main_frame, &ref_frame); |
| 290 | + if (ret < 0) |
| 291 | + return ret; |
| 292 | + |
| 293 | + out_frame = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
| 294 | + if (!out_frame) { |
| 295 | + av_frame_free(&main_frame); |
| 296 | + av_frame_free(&ref_frame); |
| 297 | + return AVERROR(ENOMEM); |
| 298 | + } |
| 299 | + av_frame_copy_props(out_frame, main_frame); |
| 300 | + |
| 301 | + if (ctx->is_disabled || !ref_frame) { |
| 302 | + av_frame_copy_props(ref_frame, main_frame); |
| 303 | + } |
| 304 | + |
| 305 | + for (int plane = 0; plane < s->nb_planes; plane++) { |
| 306 | + if (!(s->planes & (1 << plane))) { |
| 307 | + av_image_copy_plane(out_frame->data[plane], out_frame->linesize[plane], |
| 308 | + main_frame->data[plane], main_frame->linesize[plane], |
| 309 | + s->planewidth[plane] * ((s->depth + 7) / 8), s->planeheight[plane]); |
| 310 | + continue; |
| 311 | + } |
| 312 | + if (s->depth <= 8) |
| 313 | + guided_byte(ctx, s, main_frame->data[plane], ref_frame->data[plane], out_frame->data[plane], s->radius, s->eps, |
| 314 | + s->planewidth[plane], s->planeheight[plane], |
| 315 | + main_frame->linesize[plane], ref_frame->linesize[plane], out_frame->linesize[plane], (1 << s->depth) - 1.f); |
| 316 | + else |
| 317 | + guided_word(ctx, s, main_frame->data[plane], ref_frame->data[plane], out_frame->data[plane], s->radius, s->eps, |
| 318 | + s->planewidth[plane], s->planeheight[plane], |
| 319 | + main_frame->linesize[plane] / 2, ref_frame->linesize[plane] / 2, out_frame->linesize[plane] / 2, (1 << s->depth) - 1.f); |
| 320 | + } |
| 321 | + |
| 322 | + return ff_filter_frame(outlink, out_frame); |
| 323 | +} |
| 324 | + |
| 325 | + |
| 326 | +static int config_output(AVFilterLink *outlink) |
| 327 | +{ |
| 328 | + AVFilterContext *ctx = outlink->src; |
| 329 | + |
| 330 | + GuidedContext *s = ctx->priv; |
| 331 | + AVFilterLink *mainlink = ctx->inputs[0]; |
| 332 | + FFFrameSyncIn *in; |
| 333 | + int ret; |
| 334 | + |
| 335 | + |
| 336 | + outlink->w = mainlink->w; |
| 337 | + outlink->h = mainlink->h; |
| 338 | + outlink->time_base = mainlink->time_base; |
| 339 | + outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio; |
| 340 | + outlink->frame_rate = mainlink->frame_rate; |
| 341 | + if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0) |
| 342 | + return ret; |
| 343 | + |
| 344 | + outlink->time_base = s->fs.time_base; |
| 345 | + |
| 346 | + in = s->fs.in; |
| 347 | + in[0].time_base = mainlink->time_base; |
| 348 | + in[1].time_base = ctx->inputs[1]->time_base; |
| 349 | + in[0].sync = 2; |
| 350 | + in[0].before = EXT_INFINITY; |
| 351 | + in[0].after = EXT_INFINITY; |
| 352 | + in[1].sync = 1; |
| 353 | + in[1].before = EXT_INFINITY; |
| 354 | + in[1].after = EXT_INFINITY; |
| 355 | + s->fs.opaque = s; |
| 356 | + s->fs.on_event = process_frame; |
| 357 | + |
| 358 | + return ff_framesync_configure(&s->fs); |
| 359 | +} |
| 360 | + |
| 361 | +static int activate(AVFilterContext *ctx) |
| 362 | +{ |
| 363 | + GuidedContext *s = ctx->priv; |
| 364 | + return ff_framesync_activate(&s->fs); |
| 365 | +} |
| 366 | + |
| 367 | +static av_cold int init(AVFilterContext *ctx) |
| 368 | +{ |
| 369 | + return 0; |
| 370 | +} |
| 371 | + |
| 372 | +static av_cold void uninit(AVFilterContext *ctx) |
| 373 | +{ |
| 374 | + GuidedContext *s = ctx->priv; |
| 375 | + ff_framesync_uninit(&s->fs); |
| 376 | + return; |
| 377 | +} |
| 378 | + |
| 379 | + |
| 380 | +static int process_command(AVFilterContext *ctx, |
| 381 | + const char *cmd, |
| 382 | + const char *arg, |
| 383 | + char *res, |
| 384 | + int res_len, |
| 385 | + int flags) |
| 386 | +{ |
| 387 | + int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags); |
| 388 | + |
| 389 | + if (ret < 0) |
| 390 | + return ret; |
| 391 | + |
| 392 | + return 0; |
| 393 | +} |
| 394 | + |
| 395 | +static const AVFilterPad guided_inputs[] = { |
| 396 | + { |
| 397 | + .name = "main", |
| 398 | + .type = AVMEDIA_TYPE_VIDEO, |
| 399 | + },{ |
| 400 | + .name = "reference", |
| 401 | + .type = AVMEDIA_TYPE_VIDEO, |
| 402 | + .config_props = config_input, |
| 403 | + }, |
| 404 | + { NULL } |
| 405 | +}; |
| 406 | + |
| 407 | +static const AVFilterPad guided_outputs[] = { |
| 408 | + { |
| 409 | + .name = "default", |
| 410 | + .type = AVMEDIA_TYPE_VIDEO, |
| 411 | + .config_props = config_output, |
| 412 | + }, |
| 413 | + { NULL } |
| 414 | +}; |
| 415 | + |
| 416 | +AVFilter ff_vf_guided = { |
| 417 | + .name = "guided", |
| 418 | + .description = NULL_IF_CONFIG_SMALL("Apply Guided filter."), |
| 419 | + .init = init, |
| 420 | + .uninit = uninit, |
| 421 | + .query_formats = query_formats, |
| 422 | + .priv_size = sizeof(GuidedContext), |
| 423 | + .priv_class = &guided_class, |
| 424 | + .activate = activate, |
| 425 | + .inputs = guided_inputs, |
| 426 | + .outputs = guided_outputs, |
| 427 | + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, |
| 428 | + .process_command = process_command, |
| 429 | +}; |