Commit | Line | Data |
---|---|---|
084eb95f MO |
1 | /* |
2 | * Concat URL protocol | |
3 | * Copyright (c) 2006 Steve Lhomme | |
4 | * Copyright (c) 2007 Wolfram Gloger | |
5 | * Copyright (c) 2010 Michele Orrù | |
6 | * | |
2912e87a | 7 | * This file is part of Libav. |
084eb95f | 8 | * |
2912e87a | 9 | * Libav is free software; you can redistribute it and/or |
084eb95f MO |
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 | * | |
2912e87a | 14 | * Libav is distributed in the hope that it will be useful, |
084eb95f MO |
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 | |
2912e87a | 20 | * License along with Libav; if not, write to the Free Software |
084eb95f MO |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | */ | |
23 | ||
084eb95f MO |
24 | #include "libavutil/avstring.h" |
25 | #include "libavutil/mem.h" | |
95d312d6 GD |
26 | |
27 | #include "avformat.h" | |
0589da0a | 28 | #include "url.h" |
084eb95f MO |
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 | }; | |
42 | ||
43 | static av_cold int concat_close(URLContext *h) | |
44 | { | |
45 | int err = 0; | |
46 | size_t i; | |
47 | struct concat_data *data = h->priv_data; | |
48 | struct concat_nodes *nodes = data->nodes; | |
49 | ||
50 | for (i = 0; i != data->length; i++) | |
e52a9145 | 51 | err |= ffurl_close(nodes[i].uc); |
084eb95f MO |
52 | |
53 | av_freep(&data->nodes); | |
084eb95f MO |
54 | |
55 | return err < 0 ? -1 : 0; | |
56 | } | |
57 | ||
58 | static av_cold int concat_open(URLContext *h, const char *uri, int flags) | |
59 | { | |
5626f994 | 60 | char *node_uri = NULL; |
084eb95f MO |
61 | int err = 0; |
62 | int64_t size; | |
95d312d6 | 63 | size_t len, i; |
084eb95f | 64 | URLContext *uc; |
7e580505 | 65 | struct concat_data *data = h->priv_data; |
084eb95f MO |
66 | struct concat_nodes *nodes; |
67 | ||
68 | av_strstart(uri, "concat:", &uri); | |
69 | ||
95d312d6 GD |
70 | for (i = 0, len = 1; uri[i]; i++) { |
71 | if (uri[i] == *AV_CAT_SEPARATOR) { | |
084eb95f MO |
72 | /* integer overflow */ |
73 | if (++len == UINT_MAX / sizeof(*nodes)) { | |
74 | av_freep(&h->priv_data); | |
75 | return AVERROR(ENAMETOOLONG); | |
76 | } | |
95d312d6 GD |
77 | } |
78 | } | |
084eb95f | 79 | |
95d312d6 | 80 | if (!(nodes = av_realloc(NULL, sizeof(*nodes) * len))) |
084eb95f | 81 | return AVERROR(ENOMEM); |
95d312d6 | 82 | else |
084eb95f MO |
83 | data->nodes = nodes; |
84 | ||
85 | /* handle input */ | |
86 | if (!*uri) | |
87 | err = AVERROR(ENOENT); | |
88 | for (i = 0; *uri; i++) { | |
89 | /* parsing uri */ | |
90 | len = strcspn(uri, AV_CAT_SEPARATOR); | |
5626f994 | 91 | if ((err = av_reallocp(&node_uri, len + 1)) < 0) |
084eb95f | 92 | break; |
95d312d6 GD |
93 | av_strlcpy(node_uri, uri, len + 1); |
94 | uri += len + strspn(uri + len, AV_CAT_SEPARATOR); | |
084eb95f MO |
95 | |
96 | /* creating URLContext */ | |
ddffc2fd AK |
97 | if ((err = ffurl_open(&uc, node_uri, flags, |
98 | &h->interrupt_callback, NULL)) < 0) | |
084eb95f MO |
99 | break; |
100 | ||
101 | /* creating size */ | |
32a97d46 | 102 | if ((size = ffurl_size(uc)) < 0) { |
e52a9145 | 103 | ffurl_close(uc); |
084eb95f MO |
104 | err = AVERROR(ENOSYS); |
105 | break; | |
106 | } | |
107 | ||
108 | /* assembling */ | |
109 | nodes[i].uc = uc; | |
110 | nodes[i].size = size; | |
111 | } | |
112 | av_free(node_uri); | |
113 | data->length = i; | |
114 | ||
115 | if (err < 0) | |
116 | concat_close(h); | |
5626f994 | 117 | else if ((err = av_reallocp(&nodes, data->length * sizeof(*nodes))) < 0) |
084eb95f | 118 | concat_close(h); |
5626f994 | 119 | else |
084eb95f MO |
120 | data->nodes = nodes; |
121 | return err; | |
122 | } | |
123 | ||
124 | static int concat_read(URLContext *h, unsigned char *buf, int size) | |
125 | { | |
126 | int result, total = 0; | |
127 | struct concat_data *data = h->priv_data; | |
128 | struct concat_nodes *nodes = data->nodes; | |
95d312d6 | 129 | size_t i = data->current; |
084eb95f MO |
130 | |
131 | while (size > 0) { | |
bc371aca | 132 | result = ffurl_read(nodes[i].uc, buf, size); |
084eb95f MO |
133 | if (result < 0) |
134 | return total ? total : result; | |
95d312d6 | 135 | if (!result) { |
084eb95f | 136 | if (i + 1 == data->length || |
58a48c65 | 137 | ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0) |
084eb95f | 138 | break; |
95d312d6 | 139 | } |
084eb95f MO |
140 | total += result; |
141 | buf += result; | |
142 | size -= result; | |
143 | } | |
144 | data->current = i; | |
145 | return total; | |
146 | } | |
147 | ||
148 | static int64_t concat_seek(URLContext *h, int64_t pos, int whence) | |
149 | { | |
150 | int64_t result; | |
151 | struct concat_data *data = h->priv_data; | |
152 | struct concat_nodes *nodes = data->nodes; | |
153 | size_t i; | |
154 | ||
155 | switch (whence) { | |
156 | case SEEK_END: | |
95d312d6 | 157 | for (i = data->length - 1; i && pos < -nodes[i].size; i--) |
ae2c6943 | 158 | pos += nodes[i].size; |
084eb95f MO |
159 | break; |
160 | case SEEK_CUR: | |
161 | /* get the absolute position */ | |
162 | for (i = 0; i != data->current; i++) | |
163 | pos += nodes[i].size; | |
58a48c65 | 164 | pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR); |
084eb95f MO |
165 | whence = SEEK_SET; |
166 | /* fall through with the absolute position */ | |
167 | case SEEK_SET: | |
168 | for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++) | |
169 | pos -= nodes[i].size; | |
170 | break; | |
171 | default: | |
172 | return AVERROR(EINVAL); | |
173 | } | |
174 | ||
58a48c65 | 175 | result = ffurl_seek(nodes[i].uc, pos, whence); |
084eb95f MO |
176 | if (result >= 0) { |
177 | data->current = i; | |
178 | while (i) | |
35eaadcb | 179 | result += nodes[--i].size; |
084eb95f MO |
180 | } |
181 | return result; | |
182 | } | |
183 | ||
c6610a21 | 184 | URLProtocol ff_concat_protocol = { |
c3b05d21 MS |
185 | .name = "concat", |
186 | .url_open = concat_open, | |
187 | .url_read = concat_read, | |
188 | .url_seek = concat_seek, | |
189 | .url_close = concat_close, | |
7e580505 | 190 | .priv_data_size = sizeof(struct concat_data), |
084eb95f | 191 | }; |