From patchwork Wed Nov 29 19:16:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 6455 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp4873745jah; Wed, 29 Nov 2017 11:17:10 -0800 (PST) X-Google-Smtp-Source: AGs4zMZprTekKeOy1KfJjJ764uWX8l0CJpHJzm5nTqTlV2ReFkxErl9rHxnYG8FNcPT4X6tY/jix X-Received: by 10.223.160.111 with SMTP id l44mr3061541wrl.259.1511983030654; Wed, 29 Nov 2017 11:17:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511983030; cv=none; d=google.com; s=arc-20160816; b=LP+Pk0PEespYR8NeDSdHXJiFYvHOJIDUUtDp8xqfH6xm86H0Lo7Pc9f7XmbPeBx/H2 axVxP17HJyzEYsRBDxz/xMamFexdscezqb8oz/s6N3BWrnLHLwKVqCXUtoVzPeFpskNP 0zyA6zw9j6s83xfe5O+52rHGCPrVObPbtUamuuRiGLj8W1VtXy+AKHsmLyC579ENVMbY R4XrDecaY2SJhNDiGTQefx3ajyNpDVEvA55ekwQ6G/5IJtAqZG1EywcmU2E/sMK49cL0 +YXD87EU9Da+ITXvYBR19Ea5kfg6OcOHXzaUVeKA7LlL5kqjFfMiVXv5H/tCC9sRQ6Mc Rc5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=/f8Jshn7CTUGaBxalsQFqpNox/m0epHIDAKqTJkRVn4=; b=G2dzK7cGlmayJs54EoPah8N32t5PdG5FMS+P1WuMwE5f3q1VUW2aPxjYPNXlcaruaY cFqptdi0917SAiaCru83QcCVPVTgIo3+QPmS7aW/6+TKHmfFgLPeybG8evztFDFDkG2f kQVsSl3PaTspjwzYiCfUHJNlPdL/vjyMUJtU3e7w60ZQvzCxnBzQHFQ7glXvagQnnfrW WkuPTY7XqABD9h3FKc6HnZozv4cY8XaPrvyADyyDNOkSJ/BDhAXL+vpJEFVdpJRHwfCz VEdpIGiqc6XbScWz0AtHX0SOneeiE76zmhUObRlZH3HwaS1QlnyE2a2BwWiQnKBPH/5/ 6Z7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=GcPwv7wQ; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 56si1713527wrx.543.2017.11.29.11.17.10; Wed, 29 Nov 2017 11:17:10 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=GcPwv7wQ; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 343C268A548; Wed, 29 Nov 2017 21:17:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A61CB68A544 for ; Wed, 29 Nov 2017 21:17:00 +0200 (EET) Received: by mail-wm0-f67.google.com with SMTP id r78so8530136wme.5 for ; Wed, 29 Nov 2017 11:17:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=v4BTBGVXlvQUqRc44HNBvy0Cok4/0Tr43SQt8AJZQ84=; b=GcPwv7wQfUmw1tNRHKrKXBzxQoWLP50RuG2ub740tdUxIfyFwJqxRYHXJhFG3w1Y45 p7Nioj3fpdySpRFL+vX2ojN0UmCS8flhKObLELckWJRFBqNfSYRPO/BBharxkVdZAZ2R MyTOPg0KlKviEx76bqoME2YiljIXtOMiQ0RXG4hFsm/hpRvVpRHWibmLmJqxImH9RbyS jJdiKEtzscIRyYmS5gNUuU3vnUC8/NYRrnPx4tSxJfdXsjf3gR5RHFVOKOzeoAzS7cYp y3eEL1RYhke+XkKrbBkZyOfSIu6GvS6R4u5b/HChbpAJU+1ZMadke2HChekvufLVKL+Z 0KNA== 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:in-reply-to :references; bh=v4BTBGVXlvQUqRc44HNBvy0Cok4/0Tr43SQt8AJZQ84=; b=fKcKpKywvm6wYTjJsW+fYWD+evPgpF1r9rD61h+NwFm297Kxt7IDeacu3XH+R+jVfw Rop+nQ6YiDjPMlyObDgndKOofJXUYO7gXkT6uYCB+87aU9Jew+AhEb7Bdv7dQh7buUMD M8Y1cbY+zoScle7bb8N801nDbo+Fs4OHSX7CyJOD0wQt7uMY13lA1jYbWKJmzBPHVzxV Cjj468Y8NsRK0VObTRsa+Q5nTXt7dT3bcsmISW2JTsLX2C3e99Sy3QT6ECRzt52NViLF Qhh/MKdqfbw7Fjn44/YhL5YF64hyED9uoon36+Gi8xFx4I1Il1sWQstqJLmeMJB8nQbq W6ew== X-Gm-Message-State: AJaThX4T8nrEV14Ag49Ua2xr948UC1Mc2U/5ufefvBO8XbK41zcioPQF /CWsxoEKMEUTuea5y8XWzN2cYg== X-Received: by 10.28.184.130 with SMTP id i124mr3266939wmf.85.1511983022958; Wed, 29 Nov 2017 11:17:02 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id t135sm1815610wmt.24.2017.11.29.11.17.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Nov 2017 11:17:02 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Wed, 29 Nov 2017 20:16:16 +0100 Message-Id: <20171129191616.17868-1-onemda@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171129125608.22785-1-onemda@gmail.com> References: <20171129125608.22785-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH] avfilter: add fillborders 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 --- doc/filters.texi | 40 +++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_fillborders.c | 277 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 319 insertions(+) create mode 100644 libavfilter/vf_fillborders.c diff --git a/doc/filters.texi b/doc/filters.texi index 4a4efc70c8..1b115c2cb5 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -8610,6 +8610,46 @@ framework. It does not take parameters. +@section fillborders + +Fill borders of the input video, without changing video stream dimensions. +Sometimes video can have garbage at the four edges and you may not want to +crop video input to keep size multiple of some number. + +This filter accepts the following options: + +@table @option +@item left +Number of pixels to fill from left border. + +@item right +Number of pixels to fill from right border. + +@item top +Number of pixels to fill from top border. + +@item bottom +Number of pixels to fill from bottom border. + +@item mode +Set fill mode. + +It accepts the following values: +@table @samp +@item smear +fill pixels using outermost pixels + +@item mirror +fill pixels using mirroring + +@item fixed +fill pixels with constant value +@end table + +@item color +Set color for pixels in fixed mode. Defauls is @var{black}. +@end table + @section find_rect Find a rectangular object diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 0b77d7a01f..1c0cc1da80 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -192,6 +192,7 @@ OBJS-$(CONFIG_FIELD_FILTER) += vf_field.o OBJS-$(CONFIG_FIELDHINT_FILTER) += vf_fieldhint.o OBJS-$(CONFIG_FIELDMATCH_FILTER) += vf_fieldmatch.o OBJS-$(CONFIG_FIELDORDER_FILTER) += vf_fieldorder.o +OBJS-$(CONFIG_FILLBORDERS_FILTER) += vf_fillborders.o OBJS-$(CONFIG_FIND_RECT_FILTER) += vf_find_rect.o lavfutils.o OBJS-$(CONFIG_FLOODFILL_FILTER) += vf_floodfill.o OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 4c834f7381..fc212e58db 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -202,6 +202,7 @@ static void register_all(void) REGISTER_FILTER(FIELDHINT, fieldhint, vf); REGISTER_FILTER(FIELDMATCH, fieldmatch, vf); REGISTER_FILTER(FIELDORDER, fieldorder, vf); + REGISTER_FILTER(FILLBORDERS, fillborders, vf); REGISTER_FILTER(FIND_RECT, find_rect, vf); REGISTER_FILTER(FLOODFILL, floodfill, vf); REGISTER_FILTER(FORMAT, format, vf); diff --git a/libavfilter/vf_fillborders.c b/libavfilter/vf_fillborders.c new file mode 100644 index 0000000000..1254f2344b --- /dev/null +++ b/libavfilter/vf_fillborders.c @@ -0,0 +1,277 @@ +/* + * Copyright (c) 2017 Paul B Mahol + * + * 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 "libavutil/colorspace.h" +#include "libavutil/common.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +enum { Y, U, V, A }; +enum { R, G, B }; + +enum FillMode { FM_SMEAR, FM_MIRROR, FM_FIXED, FM_NB_MODES }; + +typedef struct Borders { + int left, right, top, bottom; +} Borders; + +typedef struct FillBordersContext { + const AVClass *class; + int left, right, top, bottom; + int mode; + + int nb_planes; + Borders borders[4]; + int planewidth[4]; + int planeheight[4]; + uint8_t fill[4]; + uint8_t yuv_color[4]; + uint8_t rgba_color[4]; + + void (*fillborders)(struct FillBordersContext *s, AVFrame *frame); +} FillBordersContext; + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, + AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P, + AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, + 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 void smear_borders(FillBordersContext *s, AVFrame *frame) +{ + int p, y; + + for (p = 0; p < s->nb_planes; p++) { + uint8_t *ptr = frame->data[p]; + int linesize = frame->linesize[p]; + + for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) { + memset(ptr + y * linesize, + *(ptr + y * linesize + s->borders[p].left), + s->borders[p].left); + memset(ptr + y * linesize + s->planewidth[p] - s->borders[p].right, + *(ptr + y * linesize + s->planewidth[p] - s->borders[p].right - 1), + s->borders[p].right); + } + + for (y = 0; y < s->borders[p].top; y++) { + memcpy(ptr + y * linesize, + ptr + s->borders[p].top * linesize, s->planewidth[p]); + } + + for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) { + memcpy(ptr + y * linesize, + ptr + (s->planeheight[p] - s->borders[p].bottom) * linesize, + s->planewidth[p]); + } + } +} + +static void mirror_borders(FillBordersContext *s, AVFrame *frame) +{ + int p, y, x; + + for (p = 0; p < s->nb_planes; p++) { + uint8_t *ptr = frame->data[p]; + int linesize = frame->linesize[p]; + + for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) { + for (x = 0; x < s->borders[p].left; x++) { + ptr[y * linesize + x] = ptr[y * linesize + s->borders[p].left * 2 - 1 - x]; + } + + for (x = 0; x < s->borders[p].right; x++) { + ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] = + ptr[y * linesize + s->planewidth[p] - s->borders[p].right - 1 - x]; + } + } + + for (y = 0; y < s->borders[p].top; y++) { + memcpy(ptr + y * linesize, + ptr + (s->borders[p].top * 2 - 1 - y) * linesize, + s->planewidth[p]); + } + + for (y = 0; y < s->borders[p].bottom; y++) { + memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize, + ptr + (s->planeheight[p] - s->borders[p].bottom - 1 - y) * linesize, + s->planewidth[p]); + } + } +} + +static void fixed_borders(FillBordersContext *s, AVFrame *frame) +{ + int p, y; + + for (p = 0; p < s->nb_planes; p++) { + uint8_t *ptr = frame->data[p]; + uint8_t fill = s->fill[p]; + int linesize = frame->linesize[p]; + + for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) { + memset(ptr + y * linesize, fill, s->borders[p].left); + memset(ptr + y * linesize + s->planewidth[p] - s->borders[p].right, fill, + s->borders[p].right); + } + + for (y = 0; y < s->borders[p].top; y++) { + memset(ptr + y * linesize, fill, s->planewidth[p]); + } + + for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) { + memset(ptr + y * linesize, fill, s->planewidth[p]); + } + } +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + FillBordersContext *s = inlink->dst->priv; + + s->fillborders(s, frame); + + return ff_filter_frame(inlink->dst->outputs[0], frame); +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + FillBordersContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + s->nb_planes = desc->nb_components; + + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->planeheight[0] = s->planeheight[3] = inlink->h; + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planewidth[0] = s->planewidth[3] = inlink->w; + + s->borders[0].left = s->borders[3].left = s->left; + s->borders[0].right = s->borders[3].right = s->right; + s->borders[0].top = s->borders[3].top = s->top; + s->borders[0].bottom = s->borders[3].bottom = s->bottom; + + s->borders[1].left = s->left >> desc->log2_chroma_w; + s->borders[1].right = s->right >> desc->log2_chroma_w; + s->borders[1].top = s->top >> desc->log2_chroma_h; + s->borders[1].bottom = s->bottom >> desc->log2_chroma_h; + + s->borders[2].left = s->left >> desc->log2_chroma_w; + s->borders[2].right = s->right >> desc->log2_chroma_w; + s->borders[2].top = s->top >> desc->log2_chroma_h; + s->borders[2].bottom = s->bottom >> desc->log2_chroma_h; + + if (inlink->w < s->left + s->right || + inlink->w <= s->left || + inlink->w <= s->right || + inlink->h < s->top + s->bottom || + inlink->h <= s->top || + inlink->h <= s->bottom || + inlink->w < s->left * 2 || + inlink->w < s->right * 2 || + inlink->h < s->top * 2 || + inlink->h < s->bottom * 2) { + av_log(ctx, AV_LOG_ERROR, "Borders are bigger than input frame size.\n"); + return AVERROR(EINVAL); + } + + switch (s->mode) { + case FM_SMEAR: s->fillborders = smear_borders; break; + case FM_MIRROR: s->fillborders = mirror_borders; break; + case FM_FIXED: s->fillborders = fixed_borders; break; + } + + s->yuv_color[Y] = RGB_TO_Y_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B]); + s->yuv_color[U] = RGB_TO_U_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B], 0); + s->yuv_color[V] = RGB_TO_V_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B], 0); + s->yuv_color[A] = s->rgba_color[A]; + + if (desc->flags & AV_PIX_FMT_FLAG_RGB) { + memcpy(s->fill, s->rgba_color, sizeof(s->rgba_color)); + } else { + memcpy(s->fill, s->yuv_color, sizeof(s->yuv_color)); + } + + return 0; +} + +#define OFFSET(x) offsetof(FillBordersContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption fillborders_options[] = { + { "left", "set the left fill border", OFFSET(left), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, + { "right", "set the right fill border", OFFSET(right), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, + { "top", "set the top fill border", OFFSET(top), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, + { "bottom", "set the bottom fill border", OFFSET(bottom), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, + { "mode", "set the fill borders mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=FM_SMEAR}, 0, FM_NB_MODES-1, FLAGS, "mode" }, + { "smear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_SMEAR}, 0, 0, FLAGS, "mode" }, + { "mirror", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_MIRROR}, 0, 0, FLAGS, "mode" }, + { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_FIXED}, 0, 0, FLAGS, "mode" }, + { "color", "set the color for the fixed mode", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(fillborders); + +static const AVFilterPad fillborders_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + .filter_frame = filter_frame, + .needs_writable = 1, + }, + { NULL } +}; + +static const AVFilterPad fillborders_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter ff_vf_fillborders = { + .name = "fillborders", + .description = NULL_IF_CONFIG_SMALL("Fill borders of the input video."), + .priv_size = sizeof(FillBordersContext), + .priv_class = &fillborders_class, + .query_formats = query_formats, + .inputs = fillborders_inputs, + .outputs = fillborders_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, +};