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 "config_components.h"
25 
26 #include "libavutil/avstring.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/mem.h"
29 
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "url.h"
33 
34 #define AV_CAT_SEPARATOR "|"
35 
36 struct concat_nodes {
37  URLContext *uc; ///< node's URLContext
38  int64_t size; ///< url filesize
39 };
40 
41 struct concat_data {
42  struct concat_nodes *nodes; ///< list of nodes to concat
43  size_t length; ///< number of cat'ed nodes
44  size_t current; ///< index of currently read node
45  uint64_t total_size;
46 };
47 
49 {
50  int err = 0;
51  size_t i;
52  struct concat_data *data = h->priv_data;
53  struct concat_nodes *nodes = data->nodes;
54 
55  for (i = 0; i != data->length; i++)
56  err |= ffurl_closep(&nodes[i].uc);
57 
58  av_freep(&data->nodes);
59 
60  return err < 0 ? -1 : 0;
61 }
62 
63 #if CONFIG_CONCAT_PROTOCOL
64 static av_cold int concat_open(URLContext *h, const char *uri, int flags)
65 {
66  char *node_uri = NULL;
67  int err = 0;
68  int64_t size, total_size = 0;
69  size_t len, i;
70  URLContext *uc;
71  struct concat_data *data = h->priv_data;
72  struct concat_nodes *nodes;
73 
74  if (!av_strstart(uri, "concat:", &uri)) {
75  av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
76  return AVERROR(EINVAL);
77  }
78 
79  for (i = 0, len = 1; uri[i]; i++) {
80  if (uri[i] == *AV_CAT_SEPARATOR) {
81  len++;
82  }
83  }
84 
85  if (!(nodes = av_realloc_array(NULL, len, sizeof(*nodes))))
86  return AVERROR(ENOMEM);
87  else
88  data->nodes = nodes;
89 
90  /* handle input */
91  if (!*uri)
92  err = AVERROR(ENOENT);
93  for (i = 0; *uri; i++) {
94  /* parsing uri */
95  len = strcspn(uri, AV_CAT_SEPARATOR);
96  if ((err = av_reallocp(&node_uri, len + 1)) < 0)
97  break;
98  av_strlcpy(node_uri, uri, len + 1);
99  uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
100 
101  /* creating URLContext */
102  err = ffurl_open_whitelist(&uc, node_uri, flags,
103  &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
104  if (err < 0)
105  break;
106 
107  /* creating size */
108  if ((size = ffurl_size(uc)) < 0) {
109  ffurl_close(uc);
110  err = AVERROR(ENOSYS);
111  break;
112  }
113 
114  /* assembling */
115  nodes[i].uc = uc;
116  nodes[i].size = size;
117  total_size += size;
118  }
119  av_free(node_uri);
120  data->length = i;
121 
122  if (err < 0)
123  concat_close(h);
124  else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
125  concat_close(h);
126  err = AVERROR(ENOMEM);
127  } else
128  data->nodes = nodes;
129  data->total_size = total_size;
130  return err;
131 }
132 #endif
133 
134 static int concat_read(URLContext *h, unsigned char *buf, int size)
135 {
136  int result, total = 0;
137  struct concat_data *data = h->priv_data;
138  struct concat_nodes *nodes = data->nodes;
139  size_t i = data->current;
140 
141  while (size > 0) {
142  result = ffurl_read(nodes[i].uc, buf, size);
143  if (result == AVERROR_EOF) {
144  if (i + 1 == data->length ||
145  ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
146  break;
147  result = 0;
148  }
149  if (result < 0)
150  return total ? total : result;
151  total += result;
152  buf += result;
153  size -= result;
154  }
155  data->current = i;
156  return total ? total : result;
157 }
158 
159 static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
160 {
161  int64_t result;
162  struct concat_data *data = h->priv_data;
163  struct concat_nodes *nodes = data->nodes;
164  size_t i;
165 
166  if ((whence & AVSEEK_SIZE))
167  return data->total_size;
168  switch (whence) {
169  case SEEK_END:
170  for (i = data->length - 1; i && pos < -nodes[i].size; i--)
171  pos += nodes[i].size;
172  break;
173  case SEEK_CUR:
174  /* get the absolute position */
175  for (i = 0; i != data->current; i++)
176  pos += nodes[i].size;
177  pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
178  whence = SEEK_SET;
179  /* fall through with the absolute position */
180  case SEEK_SET:
181  for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
182  pos -= nodes[i].size;
183  break;
184  default:
185  return AVERROR(EINVAL);
186  }
187 
188  result = ffurl_seek(nodes[i].uc, pos, whence);
189  if (result >= 0) {
190  data->current = i;
191  while (i)
192  result += nodes[--i].size;
193  }
194  return result;
195 }
196 
197 #if CONFIG_CONCAT_PROTOCOL
199  .name = "concat",
200  .url_open = concat_open,
201  .url_read = concat_read,
202  .url_seek = concat_seek,
203  .url_close = concat_close,
204  .priv_data_size = sizeof(struct concat_data),
205  .default_whitelist = "concat,file,subfile",
206 };
207 #endif
208 
209 #if CONFIG_CONCATF_PROTOCOL
210 static av_cold int concatf_open(URLContext *h, const char *uri, int flags)
211 {
212  AVBPrint bp;
213  struct concat_data *data = h->priv_data;
214  AVIOContext *in = NULL;
215  const char *cursor;
216  int64_t total_size = 0;
217  unsigned int nodes_size = 0;
218  size_t i = 0;
219  int err;
220 
221  if (!av_strstart(uri, "concatf:", &uri)) {
222  av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
223  return AVERROR(EINVAL);
224  }
225 
226  /* handle input */
227  if (!*uri)
228  return AVERROR(ENOENT);
229 
230  err = ffio_open_whitelist(&in, uri, AVIO_FLAG_READ, &h->interrupt_callback,
231  NULL, h->protocol_whitelist, h->protocol_blacklist);
232  if (err < 0)
233  return err;
234 
236  err = avio_read_to_bprint(in, &bp, SIZE_MAX);
237  avio_closep(&in);
238  if (err < 0) {
239  av_bprint_finalize(&bp, NULL);
240  return err;
241  }
242 
243  cursor = bp.str;
244  while (*cursor) {
245  struct concat_nodes *nodes;
246  URLContext *uc;
247  char *node_uri;
248  int64_t size;
249  size_t len = i;
250  int leading_spaces = strspn(cursor, " \n\t\r");
251 
252  if (!cursor[leading_spaces])
253  break;
254 
255  node_uri = av_get_token(&cursor, "\r\n");
256  if (!node_uri) {
257  err = AVERROR(ENOMEM);
258  break;
259  }
260  if (*cursor)
261  cursor++;
262 
263  if (++len == SIZE_MAX / sizeof(*nodes)) {
264  av_free(node_uri);
265  err = AVERROR(ENAMETOOLONG);
266  break;
267  }
268 
269  /* creating URLContext */
270  err = ffurl_open_whitelist(&uc, node_uri, flags,
271  &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
272  av_free(node_uri);
273  if (err < 0)
274  break;
275 
276  /* creating size */
277  if ((size = ffurl_size(uc)) < 0) {
278  ffurl_close(uc);
279  err = AVERROR(ENOSYS);
280  break;
281  }
282 
283  nodes = av_fast_realloc(data->nodes, &nodes_size, sizeof(*nodes) * len);
284  if (!nodes) {
285  ffurl_close(uc);
286  err = AVERROR(ENOMEM);
287  break;
288  }
289  data->nodes = nodes;
290 
291  /* assembling */
292  data->nodes[i].uc = uc;
293  data->nodes[i++].size = size;
294  total_size += size;
295  }
296  av_bprint_finalize(&bp, NULL);
297  data->length = i;
298 
299  if (err < 0)
300  concat_close(h);
301 
302  data->total_size = total_size;
303  return err;
304 }
305 
307  .name = "concatf",
308  .url_open = concatf_open,
309  .url_read = concat_read,
310  .url_seek = concat_seek,
311  .url_close = concat_close,
312  .priv_data_size = sizeof(struct concat_data),
313  .default_whitelist = "concatf,concat,file,subfile",
314 };
315 #endif
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
concat_data::length
size_t length
number of cat'ed nodes
Definition: concat.c:43
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:235
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
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:154
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:45
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:479
data
const char data[16]
Definition: mxf.c:146
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:1228
ffurl_close
int ffurl_close(URLContext *h)
Definition: avio.c:461
URLProtocol
Definition: url.h:53
AV_CAT_SEPARATOR
#define AV_CAT_SEPARATOR
Definition: concat.c:34
concat_nodes::size
int64_t size
url filesize
Definition: concat.c:38
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:505
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:1355
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:225
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:153
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
size
int size
Definition: twinvq_data.h:10344
concat_data::nodes
struct concat_nodes * nodes
list of nodes to concat
Definition: concat.c:42
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:186
URLProtocol::name
const char * name
Definition: url.h:54
concat_read
static int concat_read(URLContext *h, unsigned char *buf, int size)
Definition: concat.c:134
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:37
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:1288
bprint.h
URLContext
Definition: url.h:37
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
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:44
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:159
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
concat_nodes::uc
URLContext * uc
node's URLContext
Definition: concat.c:37
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:628
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
concat_data
Definition: concat.c:41
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:86
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
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:48
avstring.h
concat_nodes
Definition: concat.c:36