From patchwork Sat Nov 17 22:54:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 11059 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 75F9444E2B7 for ; Sun, 18 Nov 2018 00:54:21 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ECC01680CAA; Sun, 18 Nov 2018 00:54:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 53C82680A5D for ; Sun, 18 Nov 2018 00:54:15 +0200 (EET) Received: by mail-wm1-f67.google.com with SMTP id p2-v6so1877123wmc.2 for ; Sat, 17 Nov 2018 14:54:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=krTvKNsLZq7oq3G/uxVcl7Hq4deKyWy2Z5RA82rCF2Y=; b=kUaYF51kujJ+TlfRHENfx0ICk0EVd0LAjFihba3ibGlM8/BcUud7PxAV0z2NZr/K4k 3xdjL9NN+X82kKS+weMyXqDz+SVj/xMh3IK8V9TEu9zA6EwSk5Y+rIcByScUVaDjR7kn 9v0KPoFjLZNFeoS6tjBb0muLgU7hvGic1MvbO+/G+au79CMta5hV3dN1UVC3HL0Bfsxb 6Pd7L20h4OltvX6QTvnMcMNtdcyy1HZUCEEzIlvECxB0MWq6k3s64Io3wY+04778RmH+ S2oouYUdIlWw7WcU4IlnuarO11wUuJJZRaBzQft10gEtia8+k4hNUebnOiyuCxGBhCm/ 7kaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=krTvKNsLZq7oq3G/uxVcl7Hq4deKyWy2Z5RA82rCF2Y=; b=LYIt1LoLiLxv53BGw4Lmzj263f4zNPxX95b7t8ViEM6voYK/8J5EXbHSrp20me9h5e 3rCQfxVcGHnHGdcwy7ikPwymopE8QJU56SOfTllp/N+HseGKjQTbbW/0r1LtJ5C7H/co N4ksfxVgVsuWgICSSxkjQM8OfXZgYpqLaejE5J+97nqYB+S0f/6DAHWxIn01t24zVIrJ FXlDVsKQlft7NpbokYZM1zTiyxALHtseU9NewaDheWNzBVx/9q/YNVaLMe+mQYhAzoSF zE6HMhPSnREzukJPJDSIA0vXoxG+KpZB+RuiK/L7KCIjcnVxJiZIZVSjC4fldAboLD4r nVcQ== X-Gm-Message-State: AA+aEWYpgfw768cQFfn4vrgZcmluzM9JyPmU/vx1oeov8s9PgDxgFU5k 4FipUSzw9glz20rpycMYkvmLMgBX X-Google-Smtp-Source: AFSGD/UUPgiPL4qWc9t3Tc22QmIhYYngTc2gJi9Xg4kzp9kx/H/Mcn6xa/caLfxuJsyCcsfVhEY5Aw== X-Received: by 2002:a1c:5448:: with SMTP id p8mr2735202wmi.124.1542495255578; Sat, 17 Nov 2018 14:54:15 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id i7-v6sm37598496wrb.3.2018.11.17.14.54.14 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Nov 2018 14:54:15 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Nov 2018 23:54:05 +0100 Message-Id: <20181117225405.28957-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add mcompensate filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- Works only after mestimate or when motion vectors are exported. --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_mcompensate.c | 173 +++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 libavfilter/vf_mcompensate.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 6058fb97bc..8a3fc541d2 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -278,6 +278,7 @@ OBJS-$(CONFIG_LUTYUV_FILTER) += vf_lut.o OBJS-$(CONFIG_MASKEDCLAMP_FILTER) += vf_maskedclamp.o framesync.o OBJS-$(CONFIG_MASKEDMERGE_FILTER) += vf_maskedmerge.o framesync.o OBJS-$(CONFIG_MCDEINT_FILTER) += vf_mcdeint.o +OBJS-$(CONFIG_MCOMPENSATE_FILTER) += vf_mcompensate.o OBJS-$(CONFIG_MERGEPLANES_FILTER) += vf_mergeplanes.o framesync.o OBJS-$(CONFIG_MESTIMATE_FILTER) += vf_mestimate.o motion_estimation.o OBJS-$(CONFIG_METADATA_FILTER) += f_metadata.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 8dfa572fde..35afafb364 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -264,6 +264,7 @@ extern AVFilter ff_vf_lutyuv; extern AVFilter ff_vf_maskedclamp; extern AVFilter ff_vf_maskedmerge; extern AVFilter ff_vf_mcdeint; +extern AVFilter ff_vf_mcompensate; extern AVFilter ff_vf_mergeplanes; extern AVFilter ff_vf_mestimate; extern AVFilter ff_vf_metadata; diff --git a/libavfilter/vf_mcompensate.c b/libavfilter/vf_mcompensate.c new file mode 100644 index 0000000000..3fc2ca3947 --- /dev/null +++ b/libavfilter/vf_mcompensate.c @@ -0,0 +1,173 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "motion_estimation.h" +#include "libavcodec/mathops.h" +#include "libavutil/avassert.h" +#include "libavutil/common.h" +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/motion_vector.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct MCContext { + const AVClass *class; + int direction; + + int width[4], height[4]; + int hsub, vsub; + int nb_planes; + int depth; +} MCContext; + +#define OFFSET(x) offsetof(MCContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM +#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit } + +static const AVOption mcompensate_options[] = { + { "dir", "set compensate direction", OFFSET(direction), AV_OPT_TYPE_INT, {.i64=0}, -1, 1, FLAGS, "dir" }, + { "forward", 0, 0, AV_OPT_TYPE_CONST, {.i64= 1}, .unit = "dir" }, + { "backward", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, .unit = "dir" }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(mcompensate); + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, + AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, + AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, + AV_PIX_FMT_YUVJ411P, + AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, + AV_PIX_FMT_GRAY8, + AV_PIX_FMT_NONE + }; + + AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); + if (!fmts_list) + return AVERROR(ENOMEM); + return ff_set_common_formats(ctx, fmts_list); +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + MCContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + + s->hsub = desc->log2_chroma_w; + s->vsub = desc->log2_chroma_h; + s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->vsub); + s->height[0] = s->height[3] = inlink->h; + s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, s->hsub); + s->width[0] = s->width[3] = inlink->w; + + s->depth = desc->comp[0].depth; + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + MCContext *s = ctx->priv; + AVFrameSideData *sd; + AVFrame *out = ff_get_video_buffer(outlink, in->width, in->height); + + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + av_frame_copy(out, in); + sd = av_frame_get_side_data(in, AV_FRAME_DATA_MOTION_VECTORS); + if (sd) { + const AVMotionVector *mvs = (const AVMotionVector *)sd->data; + + for (int i = 0; i < sd->size / sizeof(*mvs); i++) { + const AVMotionVector *mv = &mvs[i]; + const int direction = mv->source; + int w = mv->w, h = mv->h; + int sy, sx, dy, dx; + + if (mv->src_y == mv->dst_y && mv->src_x == mv->dst_x) + continue; + + sy = av_clip(mv->src_y, 0, inlink->h - h - 1); + sx = av_clip(mv->src_x, 0, inlink->w - w - 1); + dy = av_clip(mv->dst_y, 0, inlink->h - h - 1); + dx = av_clip(mv->dst_x, 0, inlink->w - w - 1); + + if (direction == s->direction) { + for (int p = 0; p < s->nb_planes; p++) { + const int vsub = p ? s->vsub : 0; + const int hsub = p ? s->hsub : 0; + const uint8_t *src = in->data[p]; + uint8_t *dst = out->data[p]; + + av_image_copy_plane(dst + (dy >> vsub) * out->linesize[p] + (dx >> hsub), + out->linesize[p], + src + (sy >> vsub) * in->linesize[p] + (sx >> hsub), + in->linesize[p], + w >> hsub, h >> vsub); + } + } + } + } + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter ff_vf_mcompensate = { + .name = "mcompensate", + .description = NULL_IF_CONFIG_SMALL("Motion compensate frames."), + .priv_size = sizeof(MCContext), + .priv_class = &mcompensate_class, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, +};