084eb95f |
/*
* 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" |
95d312d6 |
#include "avformat.h" |
0589da0a |
#include "url.h" |
084eb95f |
#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
};
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++) |
e52a9145 |
err |= ffurl_close(nodes[i].uc); |
084eb95f |
av_freep(&data->nodes);
return err < 0 ? -1 : 0;
}
static av_cold int concat_open(URLContext *h, const char *uri, int flags)
{ |
5626f994 |
char *node_uri = NULL; |
084eb95f |
int err = 0;
int64_t size; |
95d312d6 |
size_t len, i; |
084eb95f |
URLContext *uc; |
7e580505 |
struct concat_data *data = h->priv_data; |
084eb95f |
struct concat_nodes *nodes;
|
8e32d014 |
if (!av_strstart(uri, "concat:", &uri)) {
av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
return AVERROR(EINVAL);
} |
084eb95f |
|
95d312d6 |
for (i = 0, len = 1; uri[i]; i++) {
if (uri[i] == *AV_CAT_SEPARATOR) { |
084eb95f |
/* integer overflow */
if (++len == UINT_MAX / sizeof(*nodes)) {
av_freep(&h->priv_data);
return AVERROR(ENAMETOOLONG);
} |
95d312d6 |
}
} |
084eb95f |
|
95d312d6 |
if (!(nodes = av_realloc(NULL, sizeof(*nodes) * len))) |
084eb95f |
return AVERROR(ENOMEM); |
95d312d6 |
else |
084eb95f |
data->nodes = nodes;
/* handle input */
if (!*uri)
err = AVERROR(ENOENT);
for (i = 0; *uri; i++) {
/* parsing uri */
len = strcspn(uri, AV_CAT_SEPARATOR); |
5626f994 |
if ((err = av_reallocp(&node_uri, len + 1)) < 0) |
084eb95f |
break; |
95d312d6 |
av_strlcpy(node_uri, uri, len + 1);
uri += len + strspn(uri + len, AV_CAT_SEPARATOR); |
084eb95f |
/* creating URLContext */ |
fe3fed0b |
err = ffurl_open_whitelist(&uc, node_uri, flags,
&h->interrupt_callback, NULL, h->protocol_whitelist);
if (err < 0) |
084eb95f |
break;
/* creating size */ |
32a97d46 |
if ((size = ffurl_size(uc)) < 0) { |
e52a9145 |
ffurl_close(uc); |
084eb95f |
err = AVERROR(ENOSYS);
break;
}
/* assembling */
nodes[i].uc = uc;
nodes[i].size = size;
}
av_free(node_uri);
data->length = i;
if (err < 0)
concat_close(h); |
ad2a0838 |
else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) { |
084eb95f |
concat_close(h); |
ad2a0838 |
err = AVERROR(ENOMEM);
} else |
084eb95f |
data->nodes = nodes;
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; |
95d312d6 |
size_t i = data->current; |
084eb95f |
while (size > 0) { |
bc371aca |
result = ffurl_read(nodes[i].uc, buf, size); |
084eb95f |
if (result < 0)
return total ? total : result; |
95d312d6 |
if (!result) { |
084eb95f |
if (i + 1 == data->length || |
58a48c65 |
ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0) |
084eb95f |
break; |
95d312d6 |
} |
084eb95f |
total += result;
buf += result;
size -= result;
}
data->current = i;
return total;
}
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;
switch (whence) {
case SEEK_END: |
95d312d6 |
for (i = data->length - 1; i && pos < -nodes[i].size; i--) |
ae2c6943 |
pos += nodes[i].size; |
084eb95f |
break;
case SEEK_CUR:
/* get the absolute position */
for (i = 0; i != data->current; i++)
pos += nodes[i].size; |
58a48c65 |
pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR); |
084eb95f |
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);
}
|
58a48c65 |
result = ffurl_seek(nodes[i].uc, pos, whence); |
084eb95f |
if (result >= 0) {
data->current = i;
while (i) |
35eaadcb |
result += nodes[--i].size; |
084eb95f |
}
return result;
}
|
66355be3 |
URLProtocol ff_concat_protocol = { |
c3b05d21 |
.name = "concat",
.url_open = concat_open,
.url_read = concat_read,
.url_seek = concat_seek,
.url_close = concat_close, |
7e580505 |
.priv_data_size = sizeof(struct concat_data), |
fe3fed0b |
.default_whitelist = "concat,file,subfile", |
084eb95f |
}; |