summaryrefslogtreecommitdiff
path: root/libavformat/concat.c
diff options
context:
space:
mode:
authorJames Almer <jamrial@gmail.com>2021-06-29 12:07:44 -0300
committerJames Almer <jamrial@gmail.com>2021-07-02 10:21:06 -0300
commit8c2c0135e5f497a5ecbbcd237620002f791f37c3 (patch)
tree2b7e8453050ace67c6a183e584889294924d50db /libavformat/concat.c
parentda0abbbb01cae21039e2db028467d0f489c9c9ae (diff)
avformat: add a concat protocol that takes a line break delimited list of resources
Suggested-by: ffmpeg@fb.com Reviewed-by: Nicolas George <george@nsup.org> Signed-off-by: James Almer <jamrial@gmail.com>
Diffstat (limited to 'libavformat/concat.c')
-rw-r--r--libavformat/concat.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/libavformat/concat.c b/libavformat/concat.c
index 278afd997d..aec1f52d8e 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,107 @@ 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;
+ AVIOContext *in = NULL;
+ const char *cursor;
+ int64_t total_size = 0;
+ unsigned int nodes_size = 0;
+ size_t i = 0;
+ int err;
+
+ 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);
+ err = avio_read_to_bprint(in, &bp, SIZE_MAX);
+ avio_closep(&in);
+ if (err < 0) {
+ av_bprint_finalize(&bp, NULL);
+ return err;
+ }
+
+ cursor = bp.str;
+ while (*cursor) {
+ struct concat_nodes *nodes;
+ URLContext *uc;
+ char *node_uri;
+ int64_t size;
+ size_t len = i;
+
+ node_uri = av_get_token(&cursor, "\r\n");
+ if (!node_uri) {
+ err = AVERROR(ENOMEM);
+ break;
+ }
+ cursor++;
+
+ if (++len == SIZE_MAX / sizeof(*nodes)) {
+ av_free(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_free(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;
+ }
+ 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