3 * Copyright (c) 2006 Steve Lhomme
4 * Copyright (c) 2007 Wolfram Gloger
5 * Copyright (c) 2010 Michele OrrĂ¹
7 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/avstring.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/mem.h"
29 #include "avio_internal.h"
32 #define AV_CAT_SEPARATOR "|"
35 URLContext
*uc
; ///< node's URLContext
36 int64_t size
; ///< url filesize
40 struct concat_nodes
*nodes
; ///< list of nodes to concat
41 size_t length
; ///< number of cat'ed nodes
42 size_t current
; ///< index of currently read node
46 static av_cold
int concat_close(URLContext
*h
)
50 struct concat_data
*data
= h
->priv_data
;
51 struct concat_nodes
*nodes
= data
->nodes
;
53 for (i
= 0; i
!= data
->length
; i
++)
54 err
|= ffurl_closep(&nodes
[i
].uc
);
56 av_freep(&data
->nodes
);
58 return err
< 0 ? -1 : 0;
61 #if CONFIG_CONCAT_PROTOCOL
62 static av_cold
int concat_open(URLContext
*h
, const char *uri
, int flags
)
64 char *node_uri
= NULL
;
66 int64_t size
, total_size
= 0;
69 struct concat_data
*data
= h
->priv_data
;
70 struct concat_nodes
*nodes
;
72 if (!av_strstart(uri
, "concat:", &uri
)) {
73 av_log(h
, AV_LOG_ERROR
, "URL %s lacks prefix\n", uri
);
74 return AVERROR(EINVAL
);
77 for (i
= 0, len
= 1; uri
[i
]; i
++) {
78 if (uri
[i
] == *AV_CAT_SEPARATOR
) {
83 if (!(nodes
= av_realloc_array(NULL
, len
, sizeof(*nodes
))))
84 return AVERROR(ENOMEM
);
90 err
= AVERROR(ENOENT
);
91 for (i
= 0; *uri
; i
++) {
93 len
= strcspn(uri
, AV_CAT_SEPARATOR
);
94 if ((err
= av_reallocp(&node_uri
, len
+ 1)) < 0)
96 av_strlcpy(node_uri
, uri
, len
+ 1);
97 uri
+= len
+ strspn(uri
+ len
, AV_CAT_SEPARATOR
);
99 /* creating URLContext */
100 err
= ffurl_open_whitelist(&uc
, node_uri
, flags
,
101 &h
->interrupt_callback
, NULL
, h
->protocol_whitelist
, h
->protocol_blacklist
, h
);
106 if ((size
= ffurl_size(uc
)) < 0) {
108 err
= AVERROR(ENOSYS
);
114 nodes
[i
].size
= size
;
122 else if (!(nodes
= av_realloc(nodes
, data
->length
* sizeof(*nodes
)))) {
124 err
= AVERROR(ENOMEM
);
127 data
->total_size
= total_size
;
132 static int concat_read(URLContext
*h
, unsigned char *buf
, int size
)
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
;
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)
148 return total
? total
: result
;
154 return total
? total
: result
;
157 static int64_t concat_seek(URLContext
*h
, int64_t pos
, int whence
)
160 struct concat_data
*data
= h
->priv_data
;
161 struct concat_nodes
*nodes
= data
->nodes
;
164 if ((whence
& AVSEEK_SIZE
))
165 return data
->total_size
;
168 for (i
= data
->length
- 1; i
&& pos
< -nodes
[i
].size
; i
--)
169 pos
+= nodes
[i
].size
;
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
);
177 /* fall through with the absolute position */
179 for (i
= 0; i
!= data
->length
- 1 && pos
>= nodes
[i
].size
; i
++)
180 pos
-= nodes
[i
].size
;
183 return AVERROR(EINVAL
);
186 result
= ffurl_seek(nodes
[i
].uc
, pos
, whence
);
190 result
+= nodes
[--i
].size
;
195 #if CONFIG_CONCAT_PROTOCOL
196 const URLProtocol ff_concat_protocol
= {
198 .url_open
= concat_open
,
199 .url_read
= concat_read
,
200 .url_seek
= concat_seek
,
201 .url_close
= concat_close
,
202 .priv_data_size
= sizeof(struct concat_data
),
203 .default_whitelist
= "concat,file,subfile",
207 #if CONFIG_CONCATF_PROTOCOL
208 static av_cold
int concatf_open(URLContext
*h
, const char *uri
, int flags
)
211 struct concat_data
*data
= h
->priv_data
;
212 AVIOContext
*in
= NULL
;
214 int64_t total_size
= 0;
215 unsigned int nodes_size
= 0;
219 if (!av_strstart(uri
, "concatf:", &uri
)) {
220 av_log(h
, AV_LOG_ERROR
, "URL %s lacks prefix\n", uri
);
221 return AVERROR(EINVAL
);
226 return AVERROR(ENOENT
);
228 err
= ffio_open_whitelist(&in
, uri
, AVIO_FLAG_READ
, &h
->interrupt_callback
,
229 NULL
, h
->protocol_whitelist
, h
->protocol_blacklist
);
233 av_bprint_init(&bp
, 0, AV_BPRINT_SIZE_UNLIMITED
);
234 err
= avio_read_to_bprint(in
, &bp
, SIZE_MAX
);
237 av_bprint_finalize(&bp
, NULL
);
243 struct concat_nodes
*nodes
;
248 int leading_spaces
= strspn(cursor
, " \n\t\r");
250 if (!cursor
[leading_spaces
])
253 node_uri
= av_get_token(&cursor
, "\r\n");
255 err
= AVERROR(ENOMEM
);
261 if (++len
== SIZE_MAX
/ sizeof(*nodes
)) {
263 err
= AVERROR(ENAMETOOLONG
);
267 /* creating URLContext */
268 err
= ffurl_open_whitelist(&uc
, node_uri
, flags
,
269 &h
->interrupt_callback
, NULL
, h
->protocol_whitelist
, h
->protocol_blacklist
, h
);
275 if ((size
= ffurl_size(uc
)) < 0) {
277 err
= AVERROR(ENOSYS
);
281 nodes
= av_fast_realloc(data
->nodes
, &nodes_size
, sizeof(*nodes
) * len
);
284 err
= AVERROR(ENOMEM
);
290 data
->nodes
[i
].uc
= uc
;
291 data
->nodes
[i
++].size
= size
;
294 av_bprint_finalize(&bp
, NULL
);
300 data
->total_size
= total_size
;
304 const URLProtocol ff_concatf_protocol
= {
306 .url_open
= concatf_open
,
307 .url_read
= concat_read
,
308 .url_seek
= concat_seek
,
309 .url_close
= concat_close
,
310 .priv_data_size
= sizeof(struct concat_data
),
311 .default_whitelist
= "concatf,concat,file,subfile",