From patchwork Wed Jun 23 12:56:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 28606 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2042:0:0:0:0 with SMTP id z2csp3977466iod; Wed, 23 Jun 2021 05:57:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNSB+/84kH+cnwEtimYI/1PtPJIIVASxAyUe3UmouTLVcFS+gAPTBl3X7xtsjwoQkTcNkE X-Received: by 2002:a17:907:98c4:: with SMTP id kd4mr9925880ejc.119.1624453050668; Wed, 23 Jun 2021 05:57:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624453050; cv=none; d=google.com; s=arc-20160816; b=bSyCLTm/OalccsgnJ33e17c2OjwTxIFMQXuzG8AZdPTnkpvSO1Bxd4jlHW5kcOs9cc k4MhmdtvUkbWJR/z2EtXz33B1YYuvWTTsP3tLfDi1qXa0o5lK0qA6H14hLSlLvc5GjLg bMkHHw3A4QyYi00eUWlfBiMD5RsXJvIYjkZ4/Rw8J5qDGccQ2THKjaPn0uvO7WlXlRDS ojKJhUWFwa5UpZzflf1mrYO5EZ1EoiZW2DvgL4cSA47dAhGvsay3/lAcZ/rk2oKGcKQy uX73SCDpSNvb5cXhRFs3/qOoUWXMfVbFeVxyBOhVFBGPy5TU3xxmyDip/Qz0EhApAuNz Mybg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=Sv4bWXWFNb6SR6WH7dlSp0EfrLDJ9ud9jyg/1lKfhaw=; b=p1k8W/l/hNftxBzBlVitJlcOzBmICa0ZwwjlcnzIuhSoqvnH57X9m1YDy45/+EvY2C dI/Tr8iiaqLXX4X/m8rZUVUhmk+1ifU9dHrLM1YiX0qvJ896qVZJNEWqzO/EC8fMRGz8 lWtRfmOyAQHbaYyzoQzSrEEi/8xF0KB8mXhuWQUxtlG96IxGQP433iNBLbMT96MyW6Gh R2Vsr5+x5rJjSPCzf8BcfQSksAS1bihZWF5+vCsT3tqnlxbHbmVEIyTsQJ4GM0/JOvp3 bdF73hlLq3bIYhzPUZZ+jeu76Al2bikGPQSvxTe/e5GTMS+7GtShK8svXD+Sy2AiXsGK 1/Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=ei7mFKyK; 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 nc23si2305832ejc.167.2021.06.23.05.57.29; Wed, 23 Jun 2021 05:57:30 -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=ei7mFKyK; 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 AD820689F8E; Wed, 23 Jun 2021 15:57:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E06FE680BEB for ; Wed, 23 Jun 2021 15:57:19 +0300 (EEST) Received: by mail-qt1-f169.google.com with SMTP id t9so1939169qtw.7 for ; Wed, 23 Jun 2021 05:57:19 -0700 (PDT) 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:mime-version :content-transfer-encoding; bh=jJFJ3ewD9d3nl89HTW0xTGMBajSY1+Zspa1d6Y5P6MM=; b=ei7mFKyKgADyJ0kMqMMAjL5r667yOJYsknmHjbV4ptgWuSaNQ4i7wiZ+BG4Rzpb73y nL2UInN5AregCLhyJPyIXtNLOeXhlbtsLpI3HTd4j7HKUnBwrU3njjbUi374EAGHSv6U WpG/WZjsksq29BTohUA/B1MrnDz349gnzeRR01I8a7jvjEi4AaCA0lyqLi7UC5wUGERF 9j1Kip3DTCNf3nNi2hp1eV+fiUqffXgYX+qRV8EFppP191RKHK64cV9NoaH0yA+Ymoff 7UdAV17YH4qJIjVS7MUS7MNOBn7eInQX+J6KRUtxLGOyM8iOGfjEoe08K2OXx3yxYxbn B6tA== 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:mime-version:content-transfer-encoding; bh=jJFJ3ewD9d3nl89HTW0xTGMBajSY1+Zspa1d6Y5P6MM=; b=X7DCBwuFSrEsrt0kIQ6Kd7h3gWgcLPKwoA7KPQfsJKfHygHF/fT173iXJKibH8p6Dg RGco+7q1Lq9R1ngkn213Wrxesx2/qw1FWiM9NVke/1JQg4SHP4vBXNtubQHazPQw95zt wrIODzSraYtcg+JVqSwfNYEpn6NMfNaSBshPGg6kAA0cPbQ4YHwIycd/MYFvW3IcCSaK gQtCnuxZxD2PpP5RSEI8ulOeZM4CkG4FdIDPu0FbOvy1bv+CpGhQmXKorcKfNqGs1ops 611557oPrt8FiwI+XbAj7ULzz6vPNFJU+Sn1ykI1kEJLodYQKB89rqu509G4ni5dE7V9 8SpA== X-Gm-Message-State: AOAM532Ub65cib13CONbwPS73lgjfQElGMDItRfpgPrHtSGSscDqiPa4 2K82xIs7M1VHJK9AOplLeB6l6eaJ90A= X-Received: by 2002:ac8:5a0e:: with SMTP id n14mr3759339qta.302.1624453037422; Wed, 23 Jun 2021 05:57:17 -0700 (PDT) Received: from localhost.localdomain ([181.23.90.141]) by smtp.gmail.com with ESMTPSA id h6sm3734500qta.65.2021.06.23.05.57.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Jun 2021 05:57:16 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 23 Jun 2021 09:56:48 -0300 Message-Id: <20210623125648.1092-1-jamrial@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210619191301.3115-1-jamrial@gmail.com> References: <20210619191301.3115-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat: add a concat protocol that takes a line break delimited list of resources 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: pDfhSp1Cz8PB Suggested-by: ffmpeg@fb.com Signed-off-by: James Almer --- doc/protocols.texi | 30 +++++++++++ libavformat/Makefile | 1 + libavformat/concat.c | 111 ++++++++++++++++++++++++++++++++++++++++ libavformat/protocols.c | 1 + 4 files changed, 143 insertions(+) diff --git a/doc/protocols.texi b/doc/protocols.texi index ccdfb6e439..2b8ce1b7d5 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -215,6 +215,36 @@ ffplay concat:split1.mpeg\|split2.mpeg\|split3.mpeg Note that you may need to escape the character "|" which is special for many shells. +@section concatf + +Physical concatenation protocol using a line break delimited list of +resources. + +Read and seek from many resources in sequence as if they were +a unique resource. + +A URL accepted by this protocol has the syntax: +@example +concatf:@var{URL} +@end example + +where @var{URL} is the url containing a line break delimited list of +resources to be concatenated, each one possibly specifying a distinct +protocol. + +For example to read a sequence of files @file{split1.mpeg}, +@file{split2.mpeg}, @file{split3.mpeg} listed in separate lines within +a file @file{split.txt} with @command{ffplay} use the command: +@example +ffplay concatf:split.txt +@end example +Where @file{split.txt} contains the lines: +@example +split1.mpeg +split2.mpeg +split3.mpeg +@end example + @section crypto AES-encrypted stream reading protocol. diff --git a/libavformat/Makefile b/libavformat/Makefile index c9ef564523..caca95802a 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -616,6 +616,7 @@ OBJS-$(CONFIG_APPLEHTTP_PROTOCOL) += hlsproto.o OBJS-$(CONFIG_BLURAY_PROTOCOL) += bluray.o OBJS-$(CONFIG_CACHE_PROTOCOL) += cache.o OBJS-$(CONFIG_CONCAT_PROTOCOL) += concat.o +OBJS-$(CONFIG_CONCATF_PROTOCOL) += concat.o OBJS-$(CONFIG_CRYPTO_PROTOCOL) += crypto.o OBJS-$(CONFIG_DATA_PROTOCOL) += data_uri.o OBJS-$(CONFIG_FFRTMPCRYPT_PROTOCOL) += rtmpcrypt.o rtmpdigest.o rtmpdh.o diff --git a/libavformat/concat.c b/libavformat/concat.c index 278afd997d..d224b51db0 100644 --- a/libavformat/concat.c +++ b/libavformat/concat.c @@ -22,9 +22,12 @@ */ #include "libavutil/avstring.h" +#include "libavutil/bprint.h" #include "libavutil/mem.h" #include "avformat.h" +#include "avio_internal.h" +#include "internal.h" #include "url.h" #define AV_CAT_SEPARATOR "|" @@ -56,6 +59,7 @@ static av_cold int concat_close(URLContext *h) return err < 0 ? -1 : 0; } +#if CONFIG_CONCAT_PROTOCOL static av_cold int concat_open(URLContext *h, const char *uri, int flags) { char *node_uri = NULL; @@ -124,6 +128,7 @@ static av_cold int concat_open(URLContext *h, const char *uri, int flags) data->total_size = total_size; return err; } +#endif static int concat_read(URLContext *h, unsigned char *buf, int size) { @@ -188,6 +193,7 @@ static int64_t concat_seek(URLContext *h, int64_t pos, int whence) return result; } +#if CONFIG_CONCAT_PROTOCOL const URLProtocol ff_concat_protocol = { .name = "concat", .url_open = concat_open, @@ -197,3 +203,108 @@ const URLProtocol ff_concat_protocol = { .priv_data_size = sizeof(struct concat_data), .default_whitelist = "concat,file,subfile", }; +#endif + +#if CONFIG_CONCATF_PROTOCOL +static av_cold int concatf_open(URLContext *h, const char *uri, int flags) +{ + AVBPrint bp; + struct concat_data *data = h->priv_data; + struct concat_nodes *nodes = NULL; + AVIOContext *in = NULL; + URLContext *uc; + char *node_uri = NULL; + int64_t size, total_size = 0; + unsigned int nodes_size = 0; + size_t i = 0; + int err = 0; + + if (!av_strstart(uri, "concatf:", &uri)) { + av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri); + return AVERROR(EINVAL); + } + + /* handle input */ + if (!*uri) + return AVERROR(ENOENT); + + err = ffio_open_whitelist(&in, uri, AVIO_FLAG_READ, &h->interrupt_callback, + NULL, h->protocol_whitelist, h->protocol_blacklist); + if (err < 0) + return err; + + av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED); + + for (i = 0; !avio_feof(in); i++) { + uint8_t *cursor; + size_t len = i; + + if ((err = ff_read_line_to_bprint_overwrite(in, &bp)) <= 0) { + if (err == 0 && i == 0) + err = AVERROR_INVALIDDATA; + else if (err == AVERROR_EOF) + err = 0; + break; + } + + cursor = bp.str; + node_uri = av_get_token((const char **)&cursor, "\t\r\n"); + if (!node_uri) { + err = AVERROR(ENOMEM); + break; + } + + if (++len == SIZE_MAX / sizeof(*nodes)) { + av_freep(&node_uri); + err = AVERROR(ENAMETOOLONG); + break; + } + + /* creating URLContext */ + err = ffurl_open_whitelist(&uc, node_uri, flags, + &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h); + av_freep(&node_uri); + if (err < 0) + break; + + /* creating size */ + if ((size = ffurl_size(uc)) < 0) { + ffurl_close(uc); + err = AVERROR(ENOSYS); + break; + } + + nodes = av_fast_realloc(data->nodes, &nodes_size, sizeof(*nodes) * len); + if (!nodes) { + ffurl_close(uc); + err = AVERROR(ENOMEM); + break; + } + data->nodes = nodes; + + /* assembling */ + data->nodes[i].uc = uc; + data->nodes[i].size = size; + total_size += size; + } + avio_closep(&in); + av_bprint_finalize(&bp, NULL); + data->length = i; + + if (err < 0) + concat_close(h); + + data->total_size = total_size; + return err; +} + +const URLProtocol ff_concatf_protocol = { + .name = "concatf", + .url_open = concatf_open, + .url_read = concat_read, + .url_seek = concat_seek, + .url_close = concat_close, + .priv_data_size = sizeof(struct concat_data), + .default_whitelist = "concatf,concat,file,subfile", +}; +#endif diff --git a/libavformat/protocols.c b/libavformat/protocols.c index 4b6b1c8e98..7f08f151b6 100644 --- a/libavformat/protocols.c +++ b/libavformat/protocols.c @@ -27,6 +27,7 @@ extern const URLProtocol ff_async_protocol; extern const URLProtocol ff_bluray_protocol; extern const URLProtocol ff_cache_protocol; extern const URLProtocol ff_concat_protocol; +extern const URLProtocol ff_concatf_protocol; extern const URLProtocol ff_crypto_protocol; extern const URLProtocol ff_data_protocol; extern const URLProtocol ff_ffrtmpcrypt_protocol;