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/mem.h"
30 #define AV_CAT_SEPARATOR "|"
33 URLContext
*uc
; ///< node's URLContext
34 int64_t size
; ///< url filesize
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
44 static av_cold
int concat_close(URLContext
*h
)
48 struct concat_data
*data
= h
->priv_data
;
49 struct concat_nodes
*nodes
= data
->nodes
;
51 for (i
= 0; i
!= data
->length
; i
++)
52 err
|= ffurl_closep(&nodes
[i
].uc
);
54 av_freep(&data
->nodes
);
56 return err
< 0 ? -1 : 0;
59 static av_cold
int concat_open(URLContext
*h
, const char *uri
, int flags
)
61 char *node_uri
= NULL
;
63 int64_t size
, total_size
= 0;
66 struct concat_data
*data
= h
->priv_data
;
67 struct concat_nodes
*nodes
;
69 if (!av_strstart(uri
, "concat:", &uri
)) {
70 av_log(h
, AV_LOG_ERROR
, "URL %s lacks prefix\n", uri
);
71 return AVERROR(EINVAL
);
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 return AVERROR(ENAMETOOLONG
);
83 if (!(nodes
= av_realloc(NULL
, sizeof(*nodes
) * len
)))
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
;
131 static int concat_read(URLContext
*h
, unsigned char *buf
, int size
)
133 int result
, total
= 0;
134 struct concat_data
*data
= h
->priv_data
;
135 struct concat_nodes
*nodes
= data
->nodes
;
136 size_t i
= data
->current
;
139 result
= ffurl_read(nodes
[i
].uc
, buf
, size
);
140 if (result
== AVERROR_EOF
) {
141 if (i
+ 1 == data
->length
||
142 ffurl_seek(nodes
[++i
].uc
, 0, SEEK_SET
) < 0)
147 return total
? total
: result
;
153 return total
? total
: result
;
156 static int64_t concat_seek(URLContext
*h
, int64_t pos
, int whence
)
159 struct concat_data
*data
= h
->priv_data
;
160 struct concat_nodes
*nodes
= data
->nodes
;
163 if ((whence
& AVSEEK_SIZE
))
164 return data
->total_size
;
167 for (i
= data
->length
- 1; i
&& pos
< -nodes
[i
].size
; i
--)
168 pos
+= nodes
[i
].size
;
171 /* get the absolute position */
172 for (i
= 0; i
!= data
->current
; i
++)
173 pos
+= nodes
[i
].size
;
174 pos
+= ffurl_seek(nodes
[i
].uc
, 0, SEEK_CUR
);
176 /* fall through with the absolute position */
178 for (i
= 0; i
!= data
->length
- 1 && pos
>= nodes
[i
].size
; i
++)
179 pos
-= nodes
[i
].size
;
182 return AVERROR(EINVAL
);
185 result
= ffurl_seek(nodes
[i
].uc
, pos
, whence
);
189 result
+= nodes
[--i
].size
;
194 const URLProtocol ff_concat_protocol
= {
196 .url_open
= concat_open
,
197 .url_read
= concat_read
,
198 .url_seek
= concat_seek
,
199 .url_close
= concat_close
,
200 .priv_data_size
= sizeof(struct concat_data
),
201 .default_whitelist
= "concat,file,subfile",