2010-02-07 12:25:58 +02:00
|
|
|
/*
|
|
|
|
* Concat URL protocol
|
|
|
|
* Copyright (c) 2006 Steve Lhomme
|
|
|
|
* Copyright (c) 2007 Wolfram Gloger
|
|
|
|
* Copyright (c) 2010 Michele Orrù
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2023-09-06 18:46:30 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2022-02-23 14:56:49 +02:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2010-02-07 12:25:58 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2021-06-29 17:07:44 +02:00
|
|
|
#include "libavutil/bprint.h"
|
2023-09-06 18:46:30 +02:00
|
|
|
#include "libavutil/error.h"
|
2010-02-07 12:25:58 +02:00
|
|
|
#include "libavutil/mem.h"
|
2014-09-23 17:29:57 +03:00
|
|
|
|
2021-06-29 17:07:44 +02:00
|
|
|
#include "avio_internal.h"
|
2011-03-31 17:25:10 +03:00
|
|
|
#include "url.h"
|
2010-02-07 12:25:58 +02:00
|
|
|
|
|
|
|
#define AV_CAT_SEPARATOR "|"
|
|
|
|
|
|
|
|
struct concat_nodes {
|
|
|
|
URLContext *uc; ///< node's URLContext
|
|
|
|
int64_t size; ///< url filesize
|
|
|
|
};
|
|
|
|
|
|
|
|
struct concat_data {
|
|
|
|
struct concat_nodes *nodes; ///< list of nodes to concat
|
|
|
|
size_t length; ///< number of cat'ed nodes
|
|
|
|
size_t current; ///< index of currently read node
|
2019-07-19 14:22:15 +02:00
|
|
|
uint64_t total_size;
|
2010-02-07 12:25:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static av_cold int concat_close(URLContext *h)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
size_t i;
|
|
|
|
struct concat_data *data = h->priv_data;
|
|
|
|
struct concat_nodes *nodes = data->nodes;
|
|
|
|
|
|
|
|
for (i = 0; i != data->length; i++)
|
2020-04-03 17:03:38 +02:00
|
|
|
err |= ffurl_closep(&nodes[i].uc);
|
2010-02-07 12:25:58 +02:00
|
|
|
|
|
|
|
av_freep(&data->nodes);
|
|
|
|
|
|
|
|
return err < 0 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:07:44 +02:00
|
|
|
#if CONFIG_CONCAT_PROTOCOL
|
2010-02-07 12:25:58 +02:00
|
|
|
static av_cold int concat_open(URLContext *h, const char *uri, int flags)
|
|
|
|
{
|
2013-09-18 19:12:36 +03:00
|
|
|
char *node_uri = NULL;
|
2010-02-07 12:25:58 +02:00
|
|
|
int err = 0;
|
2019-07-19 14:22:15 +02:00
|
|
|
int64_t size, total_size = 0;
|
2014-09-23 17:29:57 +03:00
|
|
|
size_t len, i;
|
2010-02-07 12:25:58 +02:00
|
|
|
URLContext *uc;
|
2011-12-01 12:44:21 +03:00
|
|
|
struct concat_data *data = h->priv_data;
|
2010-02-07 12:25:58 +02:00
|
|
|
struct concat_nodes *nodes;
|
|
|
|
|
2016-01-20 12:10:27 +02:00
|
|
|
if (!av_strstart(uri, "concat:", &uri)) {
|
|
|
|
av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2010-02-07 12:25:58 +02:00
|
|
|
|
2014-09-23 17:29:57 +03:00
|
|
|
for (i = 0, len = 1; uri[i]; i++) {
|
|
|
|
if (uri[i] == *AV_CAT_SEPARATOR) {
|
2021-02-22 10:54:23 +02:00
|
|
|
len++;
|
2014-09-23 17:29:57 +03:00
|
|
|
}
|
|
|
|
}
|
2010-02-07 12:25:58 +02:00
|
|
|
|
2021-02-22 10:54:23 +02:00
|
|
|
if (!(nodes = av_realloc_array(NULL, len, sizeof(*nodes))))
|
2010-02-07 12:25:58 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2014-09-23 17:29:57 +03:00
|
|
|
else
|
2010-02-07 12:25:58 +02:00
|
|
|
data->nodes = nodes;
|
|
|
|
|
|
|
|
/* handle input */
|
|
|
|
if (!*uri)
|
|
|
|
err = AVERROR(ENOENT);
|
|
|
|
for (i = 0; *uri; i++) {
|
|
|
|
/* parsing uri */
|
|
|
|
len = strcspn(uri, AV_CAT_SEPARATOR);
|
2013-09-18 19:12:36 +03:00
|
|
|
if ((err = av_reallocp(&node_uri, len + 1)) < 0)
|
2010-02-07 12:25:58 +02:00
|
|
|
break;
|
2014-09-23 17:29:57 +03:00
|
|
|
av_strlcpy(node_uri, uri, len + 1);
|
|
|
|
uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
|
2010-02-07 12:25:58 +02:00
|
|
|
|
|
|
|
/* creating URLContext */
|
2016-01-30 03:17:51 +02:00
|
|
|
err = ffurl_open_whitelist(&uc, node_uri, flags,
|
2016-04-21 16:55:09 +02:00
|
|
|
&h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
|
2016-01-30 03:17:51 +02:00
|
|
|
if (err < 0)
|
2010-02-07 12:25:58 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* creating size */
|
2011-03-31 18:46:00 +03:00
|
|
|
if ((size = ffurl_size(uc)) < 0) {
|
2011-03-31 18:36:06 +03:00
|
|
|
ffurl_close(uc);
|
2010-02-07 12:25:58 +02:00
|
|
|
err = AVERROR(ENOSYS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* assembling */
|
|
|
|
nodes[i].uc = uc;
|
|
|
|
nodes[i].size = size;
|
2019-07-19 14:22:15 +02:00
|
|
|
total_size += size;
|
2010-02-07 12:25:58 +02:00
|
|
|
}
|
|
|
|
av_free(node_uri);
|
|
|
|
data->length = i;
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
concat_close(h);
|
2013-09-19 15:51:46 +03:00
|
|
|
else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
|
2010-02-07 12:25:58 +02:00
|
|
|
concat_close(h);
|
2013-09-19 15:51:46 +03:00
|
|
|
err = AVERROR(ENOMEM);
|
|
|
|
} else
|
2010-02-07 12:25:58 +02:00
|
|
|
data->nodes = nodes;
|
2019-07-19 14:22:15 +02:00
|
|
|
data->total_size = total_size;
|
2010-02-07 12:25:58 +02:00
|
|
|
return err;
|
|
|
|
}
|
2021-06-29 17:07:44 +02:00
|
|
|
#endif
|
2010-02-07 12:25:58 +02:00
|
|
|
|
|
|
|
static int concat_read(URLContext *h, unsigned char *buf, int size)
|
|
|
|
{
|
|
|
|
int result, total = 0;
|
|
|
|
struct concat_data *data = h->priv_data;
|
|
|
|
struct concat_nodes *nodes = data->nodes;
|
2014-09-23 17:29:57 +03:00
|
|
|
size_t i = data->current;
|
2010-02-07 12:25:58 +02:00
|
|
|
|
|
|
|
while (size > 0) {
|
2011-03-31 17:31:43 +03:00
|
|
|
result = ffurl_read(nodes[i].uc, buf, size);
|
2017-10-17 10:29:30 +02:00
|
|
|
if (result == AVERROR_EOF) {
|
2010-02-07 12:25:58 +02:00
|
|
|
if (i + 1 == data->length ||
|
2011-03-31 18:30:31 +03:00
|
|
|
ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
|
2010-02-07 12:25:58 +02:00
|
|
|
break;
|
2017-10-17 10:29:30 +02:00
|
|
|
result = 0;
|
2014-09-23 17:29:57 +03:00
|
|
|
}
|
2017-10-17 10:29:30 +02:00
|
|
|
if (result < 0)
|
|
|
|
return total ? total : result;
|
2010-02-07 12:25:58 +02:00
|
|
|
total += result;
|
|
|
|
buf += result;
|
|
|
|
size -= result;
|
|
|
|
}
|
|
|
|
data->current = i;
|
2017-10-17 10:29:30 +02:00
|
|
|
return total ? total : result;
|
2010-02-07 12:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
|
|
|
|
{
|
|
|
|
int64_t result;
|
|
|
|
struct concat_data *data = h->priv_data;
|
|
|
|
struct concat_nodes *nodes = data->nodes;
|
|
|
|
size_t i;
|
|
|
|
|
2019-07-19 14:22:15 +02:00
|
|
|
if ((whence & AVSEEK_SIZE))
|
|
|
|
return data->total_size;
|
2010-02-07 12:25:58 +02:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_END:
|
2014-09-23 17:29:57 +03:00
|
|
|
for (i = data->length - 1; i && pos < -nodes[i].size; i--)
|
2010-03-08 02:40:22 +02:00
|
|
|
pos += nodes[i].size;
|
2010-02-07 12:25:58 +02:00
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
/* get the absolute position */
|
|
|
|
for (i = 0; i != data->current; i++)
|
|
|
|
pos += nodes[i].size;
|
2011-03-31 18:30:31 +03:00
|
|
|
pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
|
2010-02-07 12:25:58 +02:00
|
|
|
whence = SEEK_SET;
|
|
|
|
/* fall through with the absolute position */
|
|
|
|
case SEEK_SET:
|
|
|
|
for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
|
|
|
|
pos -= nodes[i].size;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2011-03-31 18:30:31 +03:00
|
|
|
result = ffurl_seek(nodes[i].uc, pos, whence);
|
2010-02-07 12:25:58 +02:00
|
|
|
if (result >= 0) {
|
|
|
|
data->current = i;
|
|
|
|
while (i)
|
2010-03-08 02:22:03 +02:00
|
|
|
result += nodes[--i].size;
|
2010-02-07 12:25:58 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:07:44 +02:00
|
|
|
#if CONFIG_CONCAT_PROTOCOL
|
2016-02-19 11:39:29 +02:00
|
|
|
const URLProtocol ff_concat_protocol = {
|
2011-12-01 12:53:57 +03:00
|
|
|
.name = "concat",
|
|
|
|
.url_open = concat_open,
|
|
|
|
.url_read = concat_read,
|
|
|
|
.url_seek = concat_seek,
|
|
|
|
.url_close = concat_close,
|
2011-12-01 12:44:21 +03:00
|
|
|
.priv_data_size = sizeof(struct concat_data),
|
2016-01-30 03:17:51 +02:00
|
|
|
.default_whitelist = "concat,file,subfile",
|
2010-02-07 12:25:58 +02:00
|
|
|
};
|
2021-06-29 17:07:44 +02:00
|
|
|
#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;
|
2021-12-07 13:56:05 +02:00
|
|
|
int leading_spaces = strspn(cursor, " \n\t\r");
|
|
|
|
|
|
|
|
if (!cursor[leading_spaces])
|
|
|
|
break;
|
2021-06-29 17:07:44 +02:00
|
|
|
|
|
|
|
node_uri = av_get_token(&cursor, "\r\n");
|
|
|
|
if (!node_uri) {
|
|
|
|
err = AVERROR(ENOMEM);
|
|
|
|
break;
|
|
|
|
}
|
2021-07-27 17:00:03 +02:00
|
|
|
if (*cursor)
|
|
|
|
cursor++;
|
2021-06-29 17:07:44 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-04-13 03:52:05 +02:00
|
|
|
if (!data->length)
|
|
|
|
err = AVERROR_INVALIDDATA;
|
2021-06-29 17:07:44 +02:00
|
|
|
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
|