From patchwork Sat Jun 26 16:56:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 28717 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp476864ios; Sat, 26 Jun 2021 10:06:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOrt46sRwGbB5YVUitIiBDKhS5aq+4+69SfxsfBAKcG3pRtRduNfvInqYgppc0KwqTF3Zw X-Received: by 2002:a17:907:1607:: with SMTP id hb7mr16645112ejc.255.1624727170885; Sat, 26 Jun 2021 10:06:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624727170; cv=none; d=google.com; s=arc-20160816; b=RDe/1IPTlpU/IUesb+ZJnvKiQdvCh/rFtBmUkLnWTz/NMVqFvK9AKPzbOHaWcqhoTr nxI0xV7luX4pLsvu064WN+JWA6Bs3N1PCPRyczY3gh74t1wsLyI97sKJdzN+lDH5Q7Bt gQCs3+0X+c3XZjjXaLd7ymfDAyfC+7EjsNQV8/XJrPq1wUlTjyYmfvMrwrAZZ0fBkqzw SXZY+IhbhKCfabN4QTAlmEIFYKAcJhk1fnVWvbjH2YHM9KaMA1T6HBTi5kDh7ntHGczY CIgYTkZOpCll673CeqkzV65Z3C5hGmxIEHniKdPMpdroP5MiHoi+WyKABn5efQ82rKqv zzMQ== 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=WYg9UoYJkUl5yGSOl7wmWi+qamTckqhnzhJAmXyR0uY=; b=vdgmAiZ5x9v9Z1BegHnO/4ABlEpnqwCRKwsI5O2xbqgf6fv+rJdoQB70leZuyFZW7r mSjF/HJB3kFBsOWqwA3uGmeLUx4eOpc6/XhDkJ2cIH9F6z399vx9/6AgWWBkvhhmTu3g PyuS6BiBUWXtJX2dZreYe/kV+cCnatSYuWYcwFywd4oObTZSyaFYBb+UFTo455tfnQ3o a0cNsyr6pRP9aLCfLZqFpwPnPoYyeCkCp4vOCTIdsYaltT5UzW2GYi/umqhqKqxDoz8M pOchMy3YuswaOYV2QcWEGJCStZSp6GGRTizP8VmxVKHypWU6FN0+6dnrhV5A208cFjuK FNcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=I5M7jhWZ; 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 ka11si9748997ejc.367.2021.06.26.10.06.09; Sat, 26 Jun 2021 10:06:10 -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=I5M7jhWZ; 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 50F06687FED; Sat, 26 Jun 2021 20:06:06 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CD890687FED for ; Sat, 26 Jun 2021 20:05:58 +0300 (EEST) Received: by mail-pl1-f180.google.com with SMTP id y13so6380668plc.8 for ; Sat, 26 Jun 2021 10:05:58 -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=SZXEKKA6A3f12/sjabNDkxLMqDO9pl24uTpih87FhVc=; b=I5M7jhWZpNJrbWDk+TxojOjqAP5u4KZA1v6poBz7/K81yhJZpMSHJoxvqwKRbl+lqc 9oPd2nhIv5/kx+Y8gdXJPtiEG+M7EztHX72z6Tto5d7dOF1U89Y0pJeWp9bz2zOt7N6c wTt5gr8qMBcqp9OHyDf5pW1JeFsGDnEE+TvQRxJhLciXm+CThb89wD80Q+gMc27BUg8R pUumTbwysJck17+mMTY+bNC86sgWyJVPF2TQrmeAopcDHh+vxD3z8fkV4YZbJLnAk9T+ 3bUYrqfZGPvOmf84SRbbOBd3XflWAY0K6pJcUfF+EMLV8eHQigAXFs48Kwx7esTisPjG m7iA== 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=SZXEKKA6A3f12/sjabNDkxLMqDO9pl24uTpih87FhVc=; b=B3f3DczYJuxRe2szoOzdx/Lzc5ro4n1mJ0zBmwzBv+8+m1wngh5ePsufhpwjwSvDxW 8zCPvWvo+59DbwAT8WrLIJWhEaH2hboJsY77y6vbn02hEgIpHls+bXnlVVRHYyCY7Z95 52Uo42gSw30E0EaGabm+0GzzJcOiO9HI/hQshB7pqAJzhW2cpw7CT8e5XKTCRACgbaf8 sLaxWAIY5ZDRHckWIi2aHOXtWT1E0g9+NRvbgjhUEUjE22oguVioMLN/tml+0QUXSnlV 8poU1YBg2e6CnaowB2f6qsFz4sEeDRNvlDkjT0PVgIjjlTJkGzYaPzCinJ/bOrSI/Lzd YxWA== X-Gm-Message-State: AOAM530S3ol8OduQBZaN30VDJleGhOrgIwtPWrWPi6kXEp6kWNMLfPkb 7A3EdlUI15faVR/fno0gpLPQSdRP0yQ= X-Received: by 2002:a62:8fc8:0:b029:2ec:9b7a:f59e with SMTP id n191-20020a628fc80000b02902ec9b7af59emr16437017pfd.39.1624726694179; Sat, 26 Jun 2021 09:58:14 -0700 (PDT) Received: from localhost.localdomain ([191.83.222.21]) by smtp.gmail.com with ESMTPSA id d13sm8812721pfn.136.2021.06.26.09.58.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Jun 2021 09:58:13 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 26 Jun 2021 13:56:57 -0300 Message-Id: <20210626165657.2066-1-jamrial@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210623125648.1092-1-jamrial@gmail.com> References: <20210623125648.1092-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] 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: xuqVuTQ028Mj Suggested-by: ffmpeg@fb.com Signed-off-by: James Almer --- Updated documentation, and line breaks can now be part of the filename. doc/protocols.texi | 33 +++++++++ libavformat/Makefile | 1 + libavformat/concat.c | 146 ++++++++++++++++++++++++++++++++++++++++ libavformat/protocols.c | 1 + 4 files changed, 181 insertions(+) diff --git a/doc/protocols.texi b/doc/protocols.texi index ccdfb6e439..11de674225 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -215,6 +215,39 @@ 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 + +Note that if any of the entries in the list contain a line break as part +of their name, you'll need to escape it with a preceding "\" character. + @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..b66e3b9e01 100644 --- a/libavformat/concat.c +++ b/libavformat/concat.c @@ -22,9 +22,11 @@ */ #include "libavutil/avstring.h" +#include "libavutil/bprint.h" #include "libavutil/mem.h" #include "avformat.h" +#include "avio_internal.h" #include "url.h" #define AV_CAT_SEPARATOR "|" @@ -56,6 +58,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 +127,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 +192,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 +202,144 @@ const URLProtocol ff_concat_protocol = { .priv_data_size = sizeof(struct concat_data), .default_whitelist = "concat,file,subfile", }; +#endif + +#if CONFIG_CONCATF_PROTOCOL +// Custom ff_read_line_to_bprint() implementation where line breaks can be +// part of the line being read if escaped. +static int64_t read_line_to_bprint(AVIOContext *s, AVBPrint *bp) +{ + int len, end; + int64_t read = 0; + char tmp[1024]; + char c; + + do { + len = 0; + do { + char escape = c = avio_r8(s); + if (c == '\\') + c = avio_r8(s); + end = (c == '\r' || c == '\n' || c == '\0'); + if (end && escape == '\\') { + if (c != '\0') { + tmp[len++] = c; + end = 0; + } else + tmp[len++] = escape; + } else if (!end) { + if (escape == '\\') { + tmp[len++] = escape; + avio_skip(s, -1); + } else + tmp[len++] = c; + } + } while (!end && len < sizeof(tmp)); + av_bprint_append_data(bp, tmp, len); + read += len; + } while (!end); + + if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s)) + avio_skip(s, -1); + + if (!c && s->error) + return s->error; + + if (!c && !read && avio_feof(s)) + return AVERROR_EOF; + + return read; +} + +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; + 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++) { + size_t len = i; + + av_bprint_clear(&bp); + if ((err = read_line_to_bprint(in, &bp)) <= 0) { + if (err == 0 && i == 0) + err = AVERROR_INVALIDDATA; + else if (err == AVERROR_EOF) + err = 0; + break; + } + + if (++len == SIZE_MAX / sizeof(*nodes)) { + err = AVERROR(ENAMETOOLONG); + break; + } + + /* creating URLContext */ + err = ffurl_open_whitelist(&uc, bp.str, flags, + &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h); + 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;