Files
jellyfin-ffmpeg/libavformat/concat.c
T

203 lines
5.7 KiB
C
Raw Normal View History

2014-03-27 13:53:19 +01: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
*/
#include "libavutil/avstring.h"
#include "libavutil/mem.h"
2014-12-04 23:19:02 +01:00
#include "avformat.h"
2014-03-27 13:53:19 +01:00
#include "url.h"
#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
2020-07-15 18:29:00 +08:00
uint64_t total_size;
2014-03-27 13:53:19 +01: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-07-15 18:29:00 +08:00
err |= ffurl_closep(&nodes[i].uc);
2014-03-27 13:53:19 +01:00
av_freep(&data->nodes);
return err < 0 ? -1 : 0;
}
static av_cold int concat_open(URLContext *h, const char *uri, int flags)
{
char *node_uri = NULL;
int err = 0;
2020-07-15 18:29:00 +08:00
int64_t size, total_size = 0;
2014-12-04 23:19:02 +01:00
size_t len, i;
2014-03-27 13:53:19 +01:00
URLContext *uc;
struct concat_data *data = h->priv_data;
struct concat_nodes *nodes;
2016-02-02 23:07:19 +01:00
if (!av_strstart(uri, "concat:", &uri)) {
av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
return AVERROR(EINVAL);
}
2014-03-27 13:53:19 +01:00
2014-12-04 23:19:02 +01:00
for (i = 0, len = 1; uri[i]; i++) {
if (uri[i] == *AV_CAT_SEPARATOR) {
2014-03-27 13:53:19 +01:00
/* integer overflow */
if (++len == UINT_MAX / sizeof(*nodes)) {
return AVERROR(ENAMETOOLONG);
}
2014-12-04 23:19:02 +01:00
}
}
2014-03-27 13:53:19 +01:00
2014-12-04 23:19:02 +01:00
if (!(nodes = av_realloc(NULL, sizeof(*nodes) * len)))
2014-03-27 13:53:19 +01:00
return AVERROR(ENOMEM);
2014-12-04 23:19:02 +01:00
else
2014-03-27 13:53:19 +01:00
data->nodes = nodes;
/* handle input */
if (!*uri)
err = AVERROR(ENOENT);
for (i = 0; *uri; i++) {
/* parsing uri */
len = strcspn(uri, AV_CAT_SEPARATOR);
if ((err = av_reallocp(&node_uri, len + 1)) < 0)
break;
2014-12-04 23:19:02 +01:00
av_strlcpy(node_uri, uri, len + 1);
uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
2014-03-27 13:53:19 +01:00
/* creating URLContext */
2016-03-07 00:15:29 +01:00
err = ffurl_open_whitelist(&uc, node_uri, flags,
2016-07-12 01:58:18 +02:00
&h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
2016-03-07 00:15:29 +01:00
if (err < 0)
2014-03-27 13:53:19 +01:00
break;
/* creating size */
if ((size = ffurl_size(uc)) < 0) {
ffurl_close(uc);
err = AVERROR(ENOSYS);
break;
}
/* assembling */
nodes[i].uc = uc;
nodes[i].size = size;
2020-07-15 18:29:00 +08:00
total_size += size;
2014-03-27 13:53:19 +01:00
}
av_free(node_uri);
data->length = i;
if (err < 0)
concat_close(h);
else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
concat_close(h);
err = AVERROR(ENOMEM);
} else
data->nodes = nodes;
2020-07-15 18:29:00 +08:00
data->total_size = total_size;
2014-03-27 13:53:19 +01:00
return err;
}
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-12-04 23:19:02 +01:00
size_t i = data->current;
2014-03-27 13:53:19 +01:00
while (size > 0) {
result = ffurl_read(nodes[i].uc, buf, size);
2018-01-14 00:26:02 +00:00
if (result == AVERROR_EOF) {
2014-03-27 13:53:19 +01:00
if (i + 1 == data->length ||
ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
break;
2018-01-14 00:26:02 +00:00
result = 0;
2014-12-04 23:19:02 +01:00
}
2018-01-14 00:26:02 +00:00
if (result < 0)
return total ? total : result;
2014-03-27 13:53:19 +01:00
total += result;
buf += result;
size -= result;
}
data->current = i;
2018-01-14 00:26:02 +00:00
return total ? total : result;
2014-03-27 13:53:19 +01: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;
2020-07-15 18:29:00 +08:00
if ((whence & AVSEEK_SIZE))
return data->total_size;
2014-03-27 13:53:19 +01:00
switch (whence) {
case SEEK_END:
2014-12-04 23:19:02 +01:00
for (i = data->length - 1; i && pos < -nodes[i].size; i--)
2014-03-27 13:53:19 +01:00
pos += nodes[i].size;
break;
case SEEK_CUR:
/* get the absolute position */
for (i = 0; i != data->current; i++)
pos += nodes[i].size;
pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
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);
}
result = ffurl_seek(nodes[i].uc, pos, whence);
if (result >= 0) {
data->current = i;
while (i)
result += nodes[--i].size;
}
return result;
}
2016-07-12 01:58:18 +02:00
const URLProtocol ff_concat_protocol = {
2014-03-27 13:53:19 +01:00
.name = "concat",
.url_open = concat_open,
.url_read = concat_read,
.url_seek = concat_seek,
.url_close = concat_close,
.priv_data_size = sizeof(struct concat_data),
2016-03-07 00:15:29 +01:00
.default_whitelist = "concat,file,subfile",
2014-03-27 13:53:19 +01:00
};