FFmpeg
concat.c
Go to the documentation of this file.
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/bprint.h"
26 #include "libavutil/mem.h"
27 
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "url.h"
31 
32 #define AV_CAT_SEPARATOR "|"
33 
34 struct concat_nodes {
35  URLContext *uc; ///< node's URLContext
36  int64_t size; ///< url filesize
37 };
38 
39 struct concat_data {
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
43  uint64_t total_size;
44 };
45 
47 {
48  int err = 0;
49  size_t i;
50  struct concat_data *data = h->priv_data;
51  struct concat_nodes *nodes = data->nodes;
52 
53  for (i = 0; i != data->length; i++)
54  err |= ffurl_closep(&nodes[i].uc);
55 
56  av_freep(&data->nodes);
57 
58  return err < 0 ? -1 : 0;
59 }
60 
61 #if CONFIG_CONCAT_PROTOCOL
62 static av_cold int concat_open(URLContext *h, const char *uri, int flags)
63 {
64  char *node_uri = NULL;
65  int err = 0;
66  int64_t size, total_size = 0;
67  size_t len, i;
68  URLContext *uc;
69  struct concat_data *data = h->priv_data;
70  struct concat_nodes *nodes;
71 
72  if (!av_strstart(uri, "concat:", &uri)) {
73  av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
74  return AVERROR(EINVAL);
75  }
76 
77  for (i = 0, len = 1; uri[i]; i++) {
78  if (uri[i] == *AV_CAT_SEPARATOR) {
79  len++;
80  }
81  }
82 
83  if (!(nodes = av_realloc_array(NULL, len, sizeof(*nodes))))
84  return AVERROR(ENOMEM);
85  else
86  data->nodes = nodes;
87 
88  /* handle input */
89  if (!*uri)
90  err = AVERROR(ENOENT);
91  for (i = 0; *uri; i++) {
92  /* parsing uri */
93  len = strcspn(uri, AV_CAT_SEPARATOR);
94  if ((err = av_reallocp(&node_uri, len + 1)) < 0)
95  break;
96  av_strlcpy(node_uri, uri, len + 1);
97  uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
98 
99  /* creating URLContext */
100  err = ffurl_open_whitelist(&uc, node_uri, flags,
101  &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
102  if (err < 0)
103  break;
104 
105  /* creating size */
106  if ((size = ffurl_size(uc)) < 0) {
107  ffurl_close(uc);
108  err = AVERROR(ENOSYS);
109  break;
110  }
111 
112  /* assembling */
113  nodes[i].uc = uc;
114  nodes[i].size = size;
115  total_size += size;
116  }
117  av_free(node_uri);
118  data->length = i;
119 
120  if (err < 0)
121  concat_close(h);
122  else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
123  concat_close(h);
124  err = AVERROR(ENOMEM);
125  } else
126  data->nodes = nodes;
127  data->total_size = total_size;
128  return err;
129 }
130 #endif
131 
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 
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 #if CONFIG_CONCAT_PROTOCOL
197  .name = "concat",
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",
204 };
205 #endif
206 
207 #if CONFIG_CONCATF_PROTOCOL
208 static av_cold int concatf_open(URLContext *h, const char *uri, int flags)
209 {
210  AVBPrint bp;
211  struct concat_data *data = h->priv_data;
212  AVIOContext *in = NULL;
213  const char *cursor;
214  int64_t total_size = 0;
215  unsigned int nodes_size = 0;
216  size_t i = 0;
217  int err;
218 
219  if (!av_strstart(uri, "concatf:", &uri)) {
220  av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
221  return AVERROR(EINVAL);
222  }
223 
224  /* handle input */
225  if (!*uri)
226  return AVERROR(ENOENT);
227 
228  err = ffio_open_whitelist(&in, uri, AVIO_FLAG_READ, &h->interrupt_callback,
229  NULL, h->protocol_whitelist, h->protocol_blacklist);
230  if (err < 0)
231  return err;
232 
234  err = avio_read_to_bprint(in, &bp, SIZE_MAX);
235  avio_closep(&in);
236  if (err < 0) {
237  av_bprint_finalize(&bp, NULL);
238  return err;
239  }
240 
241  cursor = bp.str;
242  while (*cursor) {
243  struct concat_nodes *nodes;
244  URLContext *uc;
245  char *node_uri;
246  int64_t size;
247  size_t len = i;
248  int leading_spaces = strspn(cursor, " \n\t\r");
249 
250  if (!cursor[leading_spaces])
251  break;
252 
253  node_uri = av_get_token(&cursor, "\r\n");
254  if (!node_uri) {
255  err = AVERROR(ENOMEM);
256  break;
257  }
258  if (*cursor)
259  cursor++;
260 
261  if (++len == SIZE_MAX / sizeof(*nodes)) {
262  av_free(node_uri);
263  err = AVERROR(ENAMETOOLONG);
264  break;
265  }
266 
267  /* creating URLContext */
268  err = ffurl_open_whitelist(&uc, node_uri, flags,
269  &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
270  av_free(node_uri);
271  if (err < 0)
272  break;
273 
274  /* creating size */
275  if ((size = ffurl_size(uc)) < 0) {
276  ffurl_close(uc);
277  err = AVERROR(ENOSYS);
278  break;
279  }
280 
281  nodes = av_fast_realloc(data->nodes, &nodes_size, sizeof(*nodes) * len);
282  if (!nodes) {
283  ffurl_close(uc);
284  err = AVERROR(ENOMEM);
285  break;
286  }
287  data->nodes = nodes;
288 
289  /* assembling */
290  data->nodes[i].uc = uc;
291  data->nodes[i++].size = size;
292  total_size += size;
293  }
294  av_bprint_finalize(&bp, NULL);
295  data->length = i;
296 
297  if (err < 0)
298  concat_close(h);
299 
300  data->total_size = total_size;
301  return err;
302 }
303 
305  .name = "concatf",
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",
312 };
313 #endif
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
concat_data::length
size_t length
number of cat'ed nodes
Definition: concat.c:41
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:234
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:68
av_get_token
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:151
ffurl_seek
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h.
Definition: avio.c:428
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
concat_data::total_size
uint64_t total_size
Definition: concat.c:43
AVSEEK_SIZE
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:478
data
const char data[16]
Definition: mxf.c:143
ffio_open_whitelist
int ffio_open_whitelist(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist)
Definition: aviobuf.c:1225
ffurl_close
int ffurl_close(URLContext *h)
Definition: avio.c:461
URLProtocol
Definition: url.h:54
AV_CAT_SEPARATOR
#define AV_CAT_SEPARATOR
Definition: concat.c:32
concat_nodes::size
int64_t size
url filesize
Definition: concat.c:36
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ffurl_open_whitelist
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition: avio.c:306
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:504
avio_read_to_bprint
int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size)
Read contents of h into print buffer, up to max_size bytes, or up to EOF.
Definition: aviobuf.c:1343
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:224
ff_concat_protocol
const URLProtocol ff_concat_protocol
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:152
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
size
int size
Definition: twinvq_data.h:10344
concat_data::nodes
struct concat_nodes * nodes
list of nodes to concat
Definition: concat.c:40
av_reallocp
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:185
URLProtocol::name
const char * name
Definition: url.h:55
concat_read
static int concat_read(URLContext *h, unsigned char *buf, int size)
Definition: concat.c:132
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
ff_concatf_protocol
const URLProtocol ff_concatf_protocol
avio_closep
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:1285
bprint.h
URLContext
Definition: url.h:38
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
avio_internal.h
url.h
len
int len
Definition: vorbis_enc_data.h:426
concat_data::current
size_t current
index of currently read node
Definition: concat.c:42
ffurl_closep
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:438
concat_seek
static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
Definition: concat.c:157
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
concat_nodes::uc
URLContext * uc
node's URLContext
Definition: concat.c:35
ffurl_read
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: avio.c:401
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:621
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
concat_data
Definition: concat.c:39
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_strlcpy
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ffurl_size
int64_t ffurl_size(URLContext *h)
Return the filesize of the resource accessed by h, AVERROR(ENOSYS) if the operation is not supported ...
Definition: avio.c:605
h
h
Definition: vp9dsp_template.c:2038
concat_close
static av_cold int concat_close(URLContext *h)
Definition: concat.c:46
avstring.h
concat_nodes
Definition: concat.c:34