From patchwork Mon Aug 9 22:22:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 29382 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6c0f:0:0:0:0:0 with SMTP id a15csp3253924ioh; Mon, 9 Aug 2021 15:23:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxzKj73qNV80MQAdjdkk+PLP0mlaJaofEq46oe43ZfMFqh1LA/+suz/b60Z2kgO/8WYLYNl X-Received: by 2002:aa7:dd08:: with SMTP id i8mr685259edv.163.1628547801948; Mon, 09 Aug 2021 15:23:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628547801; cv=none; d=google.com; s=arc-20160816; b=EvDKxnfyQi3ECCLRi0Zq1V3zEIzqmCAIKShrn5LQgqwWmjuFQymcc2m8xY/+eLgAC6 BLZXO4oK0J/rWtHf1G7w+S/AJmeLNu6rEssW0mBO66bf4eAtggUydzkG2H9QBrkUHar4 e2ftHf+doW7dvmH/RshwedytgDsbjHiDPM6R7isswDXR2XRksI6rTz0goAGIv+cVnjyp hzYEvQx/oMswpwKx4v8CDNSrTkZgqy6p748RD7zGnywjpY71VrhBFOgNkTn0cModhfSf SmizS1d4jq7OsOA56rBc00+/qUNSgqObOCD0a7+VWqsyGI/5NSKNpqNP5ubKUfqBEN21 CD6g== 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:message-id:date:to:from:dkim-signature :delivered-to; bh=e82tGGpysE5/3SNVDV1fd9zJqDxSYgyZBVvPt14uFSo=; b=qfaBkIIoATOq5GICBs8eRx2dyXUVOD/zQd4TEavCYApB86sNrUgnuO1AC6/6aidnvl MPH7z64vwKAu1x6V1LwjzYJAY84s66oaegqLTYn/WRVx3BWwB8XxpQJNcBB00IEQpyYC F7JOsMBefI1j+o7Mb6mWHDDVsus+lp8wQdDLGfEJGxpJLvHMEIfjFx4wjOSciJtCw4LG 6D3cNPWbOXt2T/6m0/UmtvZUaP/JB/IkveAZs+JAkHattzrIkX5ZAqoH86msV4Cu35Kh IemfzsOVrRVkpMHYbj29IUkrkNeCXeDZILpTxctJJpKXmiyujyqpMC31N9jsL0s14Mpo iiUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=GtTXdmEc; 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=QUARANTINE 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 i14si4971546ejc.361.2021.08.09.15.23.21; Mon, 09 Aug 2021 15:23:21 -0700 (PDT) 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=GtTXdmEc; 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=QUARANTINE 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 6EAB668A318; Tue, 10 Aug 2021 01:23:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1B823680730 for ; Tue, 10 Aug 2021 01:23:11 +0300 (EEST) Received: by mail-ej1-f46.google.com with SMTP id c25so17787727ejb.3 for ; Mon, 09 Aug 2021 15:23:11 -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=qQAqdoe0ffeRyinbbEo6vgaAGmOO0+ddZWd2h8AsZCo=; b=GtTXdmEcBJoLV08XgHYDMgesy2QFzrfblWairk7hJHWGYmLfyGVx2iMQPJRrjs83kc vuo/NL4cyokWTWki4Qs8ljhkOJ0Wpj0dpbIFCxhRErdwA6DTUcDRsvp6bna1kfdtpsMS DbjS2ezt8/nRSxruJ8KXjRn0GRpm0bqhVE87WJpgBfttunjLYAjXtoDgqEnw2JSihSKN vXc5qhAhkb7G+LfBU0AQrcvj9VnyXvfZ3HNcfAHOl375bLKdlr+GiF8A0tCLcVOOasf5 KFhsZ/FUGH3WD7E7B7v1Gn8EJ/B5kmDYtowg9lEJ3bjuL3BFTuclETIYU9V9un0y8cDj 8oFA== 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=qQAqdoe0ffeRyinbbEo6vgaAGmOO0+ddZWd2h8AsZCo=; b=NruFyRRCb7Ug7sT2BemQiJxDZamLH7/SJPcrQVdwvSa8c11RfTlId9ej8DcYVtdGzm vYN24zheR2Lt+KVuAi7V9TThPFPhd0mOIL2AtSOH3/JM7rYz8E9lv4hqikEns6gOvHjt SeypgJ5ej2dank4OCSP3alO7dpfA3h530BuwCzh0T+T/hJr3k/iG5eakq+869M9OjgqE nGmhOQ7GzPHcy+HcmxX29db2AzTPLNQSZvI1G8BZDwfTGF9xINxTompW59lgFFxstJ+X MjJVHWHWQ8UA4QMPzHkn/YK4W4ECqizGwdiRB82n0xyCUN7q6oSY/CWUA1We+B3QleQt Gf9w== X-Gm-Message-State: AOAM530R4yqpRBMSjIi7qIsEK/NzHCMw7DrUpaBr9Gh2zUpqJaUViSLi 7SOdc1Cnb+3YI+wCERsx8+K4mKf1kuQ= X-Received: by 2002:a17:906:1701:: with SMTP id c1mr24360795eje.425.1628547790283; Mon, 09 Aug 2021 15:23:10 -0700 (PDT) Received: from localhost.localdomain ([95.168.116.18]) by smtp.gmail.com with ESMTPSA id h19sm8735880edt.87.2021.08.09.15.23.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 15:23:09 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Aug 2021 00:22:58 +0200 Message-Id: <20210809222258.10052-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add (a)segment filters X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: HUnM4ueSLRFN Signed-off-by: Paul B Mahol --- doc/filters.texi | 34 ++++ libavfilter/Makefile | 2 + libavfilter/allfilters.c | 2 + libavfilter/f_segment.c | 335 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 373 insertions(+) create mode 100644 libavfilter/f_segment.c diff --git a/doc/filters.texi b/doc/filters.texi index bdeb3fedfd..b65afbda65 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -25665,6 +25665,40 @@ A processing speed faster than what is possible without these filters cannot be achieved. @end table +@section segment, asegment + +Split single input stream into multiple streams. + +This filter does opposite of concat filters. + +@code{segment} works on video frames, @code{asegment} on audio samples. + +This filter accepts the following options: + +@table @option +@item timestamps +Timestamps of output segments separated by '|'. The first segment will run +from the beginning of the input stream. The last segment will run until +the end of the input stream + +@item frames, samples +Exact frame/sample count to split the segments. +@end table + +@subsection Examples + +@itemize + +@item +Split input audio stream into three output audio streams, starting at start of input audio stream +and storing that in 1st output audio stream, then following at 60th second and storing than in 2nd +output audio stream, and last after 120th second of input audio stream store in 3rd output audio stream: +@example +asegment=timestamps="60 | 120" +@end example + +@end itemize + @anchor{select} @section select, aselect diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 49c0c8342b..102ce7beff 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -78,6 +78,7 @@ OBJS-$(CONFIG_AREALTIME_FILTER) += f_realtime.o OBJS-$(CONFIG_ARESAMPLE_FILTER) += af_aresample.o OBJS-$(CONFIG_AREVERSE_FILTER) += f_reverse.o OBJS-$(CONFIG_ARNNDN_FILTER) += af_arnndn.o +OBJS-$(CONFIG_ASEGMENT_FILTER) += f_segment.o OBJS-$(CONFIG_ASELECT_FILTER) += f_select.o OBJS-$(CONFIG_ASENDCMD_FILTER) += f_sendcmd.o OBJS-$(CONFIG_ASETNSAMPLES_FILTER) += af_asetnsamples.o @@ -404,6 +405,7 @@ OBJS-$(CONFIG_SCALE_VULKAN_FILTER) += vf_scale_vulkan.o vulkan.o OBJS-$(CONFIG_SCALE2REF_FILTER) += vf_scale.o scale_eval.o OBJS-$(CONFIG_SCDET_FILTER) += vf_scdet.o OBJS-$(CONFIG_SCROLL_FILTER) += vf_scroll.o +OBJS-$(CONFIG_SEGMENT_FILTER) += f_segment.o OBJS-$(CONFIG_SELECT_FILTER) += f_select.o OBJS-$(CONFIG_SELECTIVECOLOR_FILTER) += vf_selectivecolor.o OBJS-$(CONFIG_SENDCMD_FILTER) += f_sendcmd.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index ae74f9c891..73040d2824 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -71,6 +71,7 @@ extern const AVFilter ff_af_arealtime; extern const AVFilter ff_af_aresample; extern const AVFilter ff_af_areverse; extern const AVFilter ff_af_arnndn; +extern const AVFilter ff_af_asegment; extern const AVFilter ff_af_aselect; extern const AVFilter ff_af_asendcmd; extern const AVFilter ff_af_asetnsamples; @@ -385,6 +386,7 @@ extern const AVFilter ff_vf_scale_vulkan; extern const AVFilter ff_vf_scale2ref; extern const AVFilter ff_vf_scdet; extern const AVFilter ff_vf_scroll; +extern const AVFilter ff_vf_segment; extern const AVFilter ff_vf_select; extern const AVFilter ff_vf_selectivecolor; extern const AVFilter ff_vf_sendcmd; diff --git a/libavfilter/f_segment.c b/libavfilter/f_segment.c new file mode 100644 index 0000000000..7e0521b3c1 --- /dev/null +++ b/libavfilter/f_segment.c @@ -0,0 +1,335 @@ +/* + * 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 + +#include "libavutil/avstring.h" +#include "libavutil/channel_layout.h" +#include "libavutil/common.h" +#include "libavutil/log.h" +#include "libavutil/mathematics.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/samplefmt.h" + +#include "audio.h" +#include "avfilter.h" +#include "filters.h" +#include "internal.h" + +typedef struct SegmentContext { + const AVClass *class; + + char *timestamps_str; + char *points_str; + int use_timestamps; + + int current_point; + int nb_points; + + int64_t *points; +} SegmentContext; + +static void count_points(char *item_str, int *nb_items) +{ + char *p; + + if (!item_str) + return; + + *nb_items = 1; + for (p = item_str; *p; p++) { + if (*p == '|') + (*nb_items)++; + } +} + +static int parse_points(AVFilterContext *ctx, char *item_str, int nb_points, int64_t *points) +{ + SegmentContext *s = ctx->priv; + char *arg, *p = item_str; + char *saveptr = NULL; + int64_t ref, cur = 0; + int ret = 0; + + for (int i = 0; i < nb_points; i++) { + if (!(arg = av_strtok(p, "|", &saveptr))) + return AVERROR(EINVAL); + + p = NULL; + ref = 0; + if (*arg == '+') { + ref = cur; + arg++; + } + + if (s->use_timestamps) { + ret = av_parse_time(&points[i], arg, s->use_timestamps); + } else { + if (sscanf(arg, "%"SCNd64, &points[i]) != 1) + ret = AVERROR(EINVAL); + } + + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, "Invalid splits supplied: %s\n", arg); + return AVERROR(EINVAL); + } + + cur = points[i]; + points[i] += ref; + } + + return 0; +} + +static av_cold int init(AVFilterContext *ctx, enum AVMediaType type) +{ + SegmentContext *s = ctx->priv; + char *split_str; + int ret; + + if (s->timestamps_str && s->points_str) { + av_log(ctx, AV_LOG_ERROR, "Both timestamps and counts supplied.\n"); + return AVERROR(EINVAL); + } else if (s->timestamps_str) { + s->use_timestamps = 1; + split_str = s->timestamps_str; + } else if (s->points_str) { + split_str = s->points_str; + } else { + av_log(ctx, AV_LOG_ERROR, "Neither timestamps nor durations nor counts supplied.\n"); + return AVERROR(EINVAL); + } + + count_points(split_str, &s->nb_points); + s->nb_points++; + + s->points = av_calloc(s->nb_points, sizeof(*s->points)); + if (!s->points) + return AVERROR(ENOMEM); + + ret = parse_points(ctx, split_str, s->nb_points - 1, s->points); + if (ret < 0) + return ret; + + s->points[s->nb_points - 1] = INT64_MAX; + + for (int i = 0; i < s->nb_points; i++) { + AVFilterPad pad = { 0 }; + + pad.type = type; + pad.name = av_asprintf("output%d", i); + if (!pad.name) + return AVERROR(ENOMEM); + + if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) { + av_freep(&pad.name); + return ret; + } + } + + return 0; +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + SegmentContext *s = ctx->priv; + AVRational tb = inlink->time_base; + + if (s->use_timestamps) { + for (int i = 0; i < s->nb_points - 1; i++) + s->points[i] = av_rescale_q(s->points[i], AV_TIME_BASE_Q, tb); + } + + return 0; +} + +static int current_segment_finished(AVFilterContext *ctx, AVFrame *frame) +{ + SegmentContext *s = ctx->priv; + AVFilterLink *inlink = ctx->inputs[0]; + int ret = 0; + + if (s->use_timestamps) { + ret = frame->pts >= s->points[s->current_point]; + } else { + switch (inlink->type) { + case AVMEDIA_TYPE_VIDEO: + ret = inlink->frame_count_out - 1 >= s->points[s->current_point]; + break; + case AVMEDIA_TYPE_AUDIO: + ret = inlink->sample_count_out - frame->nb_samples >= s->points[s->current_point]; + break; + } + } + + return ret; +} + +static int activate(AVFilterContext *ctx) +{ + AVFilterLink *inlink = ctx->inputs[0]; + SegmentContext *s = ctx->priv; + AVFrame *frame = NULL; + int ret, status; + int max_samples; + int64_t diff; + int64_t pts; + + for (int i = s->current_point; i < s->nb_points; i++) { + FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx); + } + + switch (inlink->type) { + case AVMEDIA_TYPE_VIDEO: + ret = ff_inlink_consume_frame(inlink, &frame); + break; + case AVMEDIA_TYPE_AUDIO: + diff = s->points[s->current_point] - inlink->sample_count_out; + if (s->use_timestamps) { + max_samples = av_rescale_q(diff, av_make_q(1, inlink->sample_rate), inlink->time_base); + } else { + max_samples = FFMAX(1, FFMIN(diff, INT_MAX)); + } + ret = ff_inlink_consume_samples(inlink, 1, max_samples, &frame); + break; + } + + if (ret > 0) { + while (current_segment_finished(ctx, frame)) { + ff_outlink_set_status(ctx->outputs[s->current_point], AVERROR_EOF, frame->pts); + s->current_point++; + } + + if (s->current_point >= s->nb_points) { + av_frame_free(&frame); + return AVERROR(EINVAL); + } + + ret = ff_filter_frame(ctx->outputs[s->current_point], frame); + } + + if (ret < 0) { + return ret; + } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { + for (int i = s->current_point; i < s->nb_points; i++) + ff_outlink_set_status(ctx->outputs[i], status, pts); + return 0; + } else { + for (int i = s->current_point; i < s->nb_points; i++) { + if (ff_outlink_frame_wanted(ctx->outputs[i])) + ff_inlink_request_frame(inlink); + } + return 0; + } +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + SegmentContext *s = ctx->priv; + + av_freep(&s->points); + + for (unsigned i = 0; i < ctx->nb_outputs; i++) + av_freep(&ctx->output_pads[i].name); +} + +#define OFFSET(x) offsetof(SegmentContext, x) +#define COMMON_OPTS \ + { "timestamps", "timestamps of input at which to split input", OFFSET(timestamps_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, \ + +#if CONFIG_SEGMENT_FILTER + +static av_cold int video_init(AVFilterContext *ctx) +{ + return init(ctx, AVMEDIA_TYPE_VIDEO); +} + +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM +static const AVOption segment_options[] = { + COMMON_OPTS + { "frames", "frames at which to split input", OFFSET(points_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, + { NULL } +}; +#undef FLAGS + +AVFILTER_DEFINE_CLASS(segment); + +static const AVFilterPad segment_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + }, + { NULL } +}; + +const AVFilter ff_vf_segment = { + .name = "segment", + .description = NULL_IF_CONFIG_SMALL("Segment video stream."), + .init = video_init, + .uninit = uninit, + .priv_size = sizeof(SegmentContext), + .priv_class = &segment_class, + .activate = activate, + .inputs = segment_inputs, + .outputs = NULL, + .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS, +}; +#endif // CONFIG_SEGMENT_FILTER + +#if CONFIG_ASEGMENT_FILTER + +static av_cold int audio_init(AVFilterContext *ctx) +{ + return init(ctx, AVMEDIA_TYPE_AUDIO); +} + +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM +static const AVOption asegment_options[] = { + COMMON_OPTS + { "samples", "samples at which to split input", OFFSET(points_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, + { NULL } +}; +#undef FLAGS + +AVFILTER_DEFINE_CLASS(asegment); + +static const AVFilterPad asegment_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_input, + }, + { NULL } +}; + +const AVFilter ff_af_asegment = { + .name = "asegment", + .description = NULL_IF_CONFIG_SMALL("Segment audio stream."), + .init = audio_init, + .uninit = uninit, + .priv_size = sizeof(SegmentContext), + .priv_class = &asegment_class, + .activate = activate, + .inputs = asegment_inputs, + .outputs = NULL, + .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS, +}; +#endif // CONFIG_ASEGMENT_FILTER