From patchwork Tue Oct 30 09:56:39 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: 10847 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 F069F44C6C5 for ; Tue, 30 Oct 2018 11:56:54 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5E36B68A73D; Tue, 30 Oct 2018 11:56:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1879D68A277 for ; Tue, 30 Oct 2018 11:56:20 +0200 (EET) Received: by mail-ed1-f67.google.com with SMTP id c1-v6so9899271ede.5 for ; Tue, 30 Oct 2018 02:56:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=4GWgAXW80oiYqFt7D8ePXhGwEtKk07K2aAG2Uc8hkB4=; b=SPWyeK2SIuLpcxei31cWQDARosNf/FcvrMg7gafZ919qY8/cZBZFXxCxq/ryTPJQj5 AU8fdxEq8lp8ssD/1kIwpb3J31WS2nw5muE3uSMKyYOQMO2obvVf/Cy9z0Nw+XosARw4 hxzhlJxy0e4fW+PwFc0J9hKHPsgugGH+GunZC49UoCORrcDb2kjBtSnvcNusxvpY/z9v bj4fkhUSENndUHmc0xIu1JXcmFwNRpfm9MbLCzDwk7VEW1AZxFnYTpLbo+N6cERwdi3A DRqUW7ui6QS8TtEOJf/wx3yhJKJDrDm9AZZ1uaLnu47THN6+lBtCmNA1mzlB77te+k0V XJyg== 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=4GWgAXW80oiYqFt7D8ePXhGwEtKk07K2aAG2Uc8hkB4=; b=nqeKBCpnvTaKbMQek9/SDGprwoVdaH/+vkFzOSOO3Fm7yPr+l2PG0ksKX1yKRcK68n l+bl+D+jMvThw15TZbvMkX4c5gKwHYO7lrbH0cTqw9jkm53uPC4wTLir7xbxCvdiyca+ b5oKjnny+cSsD+gk1913S1TxXfE+uz5PUknJDh4l688vgF6KkKHJNXzYaRqtAnsoqp3r 6cobmz9aN1mqh2ZUZwodaZ1mF1ThuI9KnRvhiD6rn50yiVaPLHDRkB5fbOVr8aYRUT5w zwpnJIYbODCJxAxivSgk7UfNhNUH/riOBjm5eBDvgLs/n2tPB4/ZE6osfk/LV0TAK6jp UD0A== X-Gm-Message-State: AGRZ1gLAhCEcQcOblq8Bs4gv2oXSpSCecaTK6Mq9SUMk9GxCvscMnFVh YDxYpZ5RgsmBwr0+22APUzsyOvap9uA= X-Google-Smtp-Source: AJdET5cO2NpNodiKX4Coss6naQUXdbnKjgc3cULwZqE/+b7ShGr43lA9uSsq+r/GK1SNURk+ra90PQ== X-Received: by 2002:a17:906:1659:: with SMTP id n25-v6mr12905715ejd.24.1540893410533; Tue, 30 Oct 2018 02:56:50 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id l2-v6sm581213edn.82.2018.10.30.02.56.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Oct 2018 02:56:49 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 30 Oct 2018 10:56:39 +0100 Message-Id: <20181030095639.14690-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add tpad 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 | 22 ++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_tpad.c | 166 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 libavfilter/vf_tpad.c diff --git a/doc/filters.texi b/doc/filters.texi index 9b84b1145b..6dda780aa0 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -16676,6 +16676,28 @@ embedded peak information in display metadata is not reliable or when tone mapping from a lower range to a higher range. @end table +@section tpad + +Temporarily pad video frames. + +The filter accepts the following options: + +@table @option +@item start +Specify number of delay frames before input video stream. + +@item stop +Specify number of padding frames after input video stream. +Set to -1 to pad indefinitely. + +@item color +Specify the color of the padded area. For the syntax of this option, +check the @ref{color syntax,,"Color" section in the ffmpeg-utils +manual,ffmpeg-utils}. + +The default value of @var{color} is "black". +@end table + @anchor{transpose} @section transpose diff --git a/libavfilter/Makefile b/libavfilter/Makefile index c35cd8f422..51e48efc2e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -383,6 +383,7 @@ OBJS-$(CONFIG_TMIX_FILTER) += vf_mix.o framesync.o OBJS-$(CONFIG_TONEMAP_FILTER) += vf_tonemap.o colorspace.o OBJS-$(CONFIG_TONEMAP_OPENCL_FILTER) += vf_tonemap_opencl.o colorspace.o opencl.o \ opencl/tonemap.o opencl/colorspace_common.o +OBJS-$(CONFIG_TPAD_FILTER) += vf_tpad.o OBJS-$(CONFIG_TRANSPOSE_FILTER) += vf_transpose.o OBJS-$(CONFIG_TRANSPOSE_NPP_FILTER) += vf_transpose_npp.o OBJS-$(CONFIG_TRIM_FILTER) += trim.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index d5a211bda5..6052cb8c3c 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -365,6 +365,7 @@ extern AVFilter ff_vf_tlut2; extern AVFilter ff_vf_tmix; extern AVFilter ff_vf_tonemap; extern AVFilter ff_vf_tonemap_opencl; +extern AVFilter ff_vf_tpad; extern AVFilter ff_vf_transpose; extern AVFilter ff_vf_transpose_npp; extern AVFilter ff_vf_trim; diff --git a/libavfilter/vf_tpad.c b/libavfilter/vf_tpad.c new file mode 100644 index 0000000000..e59037ca56 --- /dev/null +++ b/libavfilter/vf_tpad.c @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2018 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/avassert.h" +#include "libavutil/channel_layout.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "audio.h" +#include "filters.h" +#include "internal.h" +#include "formats.h" +#include "drawutils.h" + +typedef struct TPadContext { + const AVClass *class; + int pad_start; + int pad_stop; + uint8_t rgba_color[4]; ///< color for the padding area + + FFDrawContext draw; + FFDrawColor color; + int64_t pts; + int eof; +} TPadContext; + +#define OFFSET(x) offsetof(TPadContext, x) +#define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption tpad_options[] = { + { "start", "set the number of frames to delay input", OFFSET(pad_start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, VF }, + { "stop", "set the number of frames to add after input finished", OFFSET(pad_stop), AV_OPT_TYPE_INT, {.i64=0}, -1, INT_MAX, VF }, + { "color", "set the color of the added frames", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, VF }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(tpad); + +static int query_formats(AVFilterContext *ctx) +{ + return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0)); +} + +static int activate(AVFilterContext *ctx) +{ + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; + TPadContext *s = ctx->priv; + AVFrame *frame = NULL; + int ret, status; + int64_t pts; + + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); + + if (s->pad_start > 0 && ff_outlink_frame_wanted(outlink)) { + frame = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!frame) + return AVERROR(ENOMEM); + ff_fill_rectangle(&s->draw, &s->color, + frame->data, frame->linesize, + 0, 0, frame->width, frame->height); + frame->pts = s->pts; + s->pts += av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base); + s->pad_start--; + return ff_filter_frame(outlink, frame); + } + + if (!s->eof && !s->pad_start) { + ret = ff_inlink_consume_frame(inlink, &frame); + if (ret < 0) + return ret; + if (ret > 0) { + frame->pts += s->pts; + return ff_filter_frame(outlink, frame); + } + } + + if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) { + if (status == AVERROR_EOF) { + if (!s->pad_stop) { + ff_outlink_set_status(outlink, status, pts); + return 0; + } + s->eof = 1; + s->pts += pts; + } + } + + if (s->eof) { + if (!s->pad_stop) { + ff_outlink_set_status(outlink, AVERROR_EOF, s->pts); + return 0; + } + frame = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!frame) + return AVERROR(ENOMEM); + ff_fill_rectangle(&s->draw, &s->color, + frame->data, frame->linesize, + 0, 0, frame->width, frame->height); + frame->pts = s->pts; + s->pts += av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base); + if (s->pad_stop > 0) + s->pad_stop--; + return ff_filter_frame(outlink, frame); + } + + if (!s->pad_start) + FF_FILTER_FORWARD_WANTED(outlink, inlink); + + return FFERROR_NOT_READY; +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + TPadContext *s = ctx->priv; + + ff_draw_init(&s->draw, inlink->format, 0); + ff_draw_color(&s->draw, &s->color, s->rgba_color); + + return 0; +} + +static const AVFilterPad tpad_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad tpad_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter ff_vf_tpad = { + .name = "tpad", + .description = NULL_IF_CONFIG_SMALL("Temporarily pad video frames."), + .priv_size = sizeof(TPadContext), + .priv_class = &tpad_class, + .query_formats = query_formats, + .inputs = tpad_inputs, + .outputs = tpad_outputs, + .activate = activate, +};