1 /*
2 * Concat URL protocol
3 * Copyright (c) 2006 Steve Lhomme
4 * Copyright (c) 2007 Wolfram Gloger
5 * Copyright (c) 2010 Michele Orrù
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/mem.h"
26
27 #include "avformat.h"
28 #include "url.h"
29
30 #define AV_CAT_SEPARATOR "|"
31
32 struct concat_nodes {
33 URLContext *uc; ///< node's URLContext
34 int64_t size; ///< url filesize
35 };
36
37 struct concat_data {
38 struct concat_nodes *nodes; ///< list of nodes to concat
39 size_t length; ///< number of cat'ed nodes
40 size_t current; ///< index of currently read node
41 uint64_t total_size;
42 };
43
concat_close(URLContext * h)44 static av_cold int concat_close(URLContext *h)
45 {
46 int err = 0;
47 size_t i;
48 struct concat_data *data = h->priv_data;
49 struct concat_nodes *nodes = data->nodes;
50
51 for (i = 0; i != data->length; i++)
52 err |= ffurl_close(nodes[i].uc);
53
54 av_freep(&data->nodes);
55
56 return err < 0 ? -1 : 0;
57 }
58
concat_open(URLContext * h,const char * uri,int flags)59 static av_cold int concat_open(URLContext *h, const char *uri, int flags)
60 {
61 char *node_uri = NULL;
62 int err = 0;
63 int64_t size, total_size = 0;
64 size_t len, i;
65 URLContext *uc;
66 struct concat_data *data = h->priv_data;
67 struct concat_nodes *nodes;
68
69 if (!av_strstart(uri, "concat:", &uri)) {
70 av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
71 return AVERROR(EINVAL);
72 }
73
74 for (i = 0, len = 1; uri[i]; i++) {
75 if (uri[i] == *AV_CAT_SEPARATOR) {
76 /* integer overflow */
77 if (++len == UINT_MAX / sizeof(*nodes)) {
78 av_freep(&h->priv_data);
79 return AVERROR(ENAMETOOLONG);
80 }
81 }
82 }
83
84 if (!(nodes = av_realloc(NULL, sizeof(*nodes) * len)))
85 return AVERROR(ENOMEM);
86 else
87 data->nodes = nodes;
88
89 /* handle input */
90 if (!*uri)
91 err = AVERROR(ENOENT);
92 for (i = 0; *uri; i++) {
93 /* parsing uri */
94 len = strcspn(uri, AV_CAT_SEPARATOR);
95 if ((err = av_reallocp(&node_uri, len + 1)) < 0)
96 break;
97 av_strlcpy(node_uri, uri, len + 1);
98 uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
99
100 /* creating URLContext */
101 err = ffurl_open_whitelist(&uc, node_uri, flags,
102 &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
103 if (err < 0)
104 break;
105
106 /* creating size */
107 if ((size = ffurl_size(uc)) < 0) {
108 ffurl_close(uc);
109 err = AVERROR(ENOSYS);
110 break;
111 }
112
113 /* assembling */
114 nodes[i].uc = uc;
115 nodes[i].size = size;
116 total_size += size;
117 }
118 av_free(node_uri);
119 data->length = i;
120
121 if (err < 0)
122 concat_close(h);
123 else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
124 concat_close(h);
125 err = AVERROR(ENOMEM);
126 } else
127 data->nodes = nodes;
128 data->total_size = total_size;
129 return err;
130 }
131
concat_read(URLContext * h,unsigned char * buf,int size)132 static int concat_read(URLContext *h, unsigned char *buf, int size)
133 {
134 int result, total = 0;
135 struct concat_data *data = h->priv_data;
136 struct concat_nodes *nodes = data->nodes;
137 size_t i = data->current;
138
139 while (size > 0) {
140 result = ffurl_read(nodes[i].uc, buf, size);
141 if (result == AVERROR_EOF) {
142 if (i + 1 == data->length ||
143 ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
144 break;
145 result = 0;
146 }
147 if (result < 0)
148 return total ? total : result;
149 total += result;
150 buf += result;
151 size -= result;
152 }
153 data->current = i;
154 return total ? total : result;
155 }
156
concat_seek(URLContext * h,int64_t pos,int whence)157 static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
158 {
159 int64_t result;
160 struct concat_data *data = h->priv_data;
161 struct concat_nodes *nodes = data->nodes;
162 size_t i;
163
164 if ((whence & AVSEEK_SIZE))
165 return data->total_size;
166 switch (whence) {
167 case SEEK_END:
168 for (i = data->length - 1; i && pos < -nodes[i].size; i--)
169 pos += nodes[i].size;
170 break;
171 case SEEK_CUR:
172 /* get the absolute position */
173 for (i = 0; i != data->current; i++)
174 pos += nodes[i].size;
175 pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
176 whence = SEEK_SET;
177 /* fall through with the absolute position */
178 case SEEK_SET:
179 for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
180 pos -= nodes[i].size;
181 break;
182 default:
183 return AVERROR(EINVAL);
184 }
185
186 result = ffurl_seek(nodes[i].uc, pos, whence);
187 if (result >= 0) {
188 data->current = i;
189 while (i)
190 result += nodes[--i].size;
191 }
192 return result;
193 }
194
195 const URLProtocol ff_concat_protocol = {
196 .name = "concat",
197 .url_open = concat_open,
198 .url_read = concat_read,
199 .url_seek = concat_seek,
200 .url_close = concat_close,
201 .priv_data_size = sizeof(struct concat_data),
202 .default_whitelist = "concat,file,subfile",
203 };
204