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/mem.h"
26 
27 #include "avformat.h"
28 #include "url.h"
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  uint64_t total_size;
42 };
43 
45 {
46  int err = 0;
47  size_t i;
48  struct concat_data *data = h->priv_data;
49  struct concat_nodes *nodes = data->nodes;
50 
51  for (i = 0; i != data->length; i++)
52  err |= ffurl_closep(&nodes[i].uc);
53 
54  av_freep(&data->nodes);
55 
56  return err < 0 ? -1 : 0;
57 }
58 
59 static av_cold int concat_open(URLContext *h, const char *uri, int flags)
60 {
61  char *node_uri = NULL;
62  int err = 0;
63  int64_t size, total_size = 0;
64  size_t len, i;
65  URLContext *uc;
66  struct concat_data *data = h->priv_data;
67  struct concat_nodes *nodes;
68 
69  if (!av_strstart(uri, "concat:", &uri)) {
70  av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
71  return AVERROR(EINVAL);
72  }
73 
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);
79  }
80  }
81  }
82 
83  if (!(nodes = av_realloc(NULL, sizeof(*nodes) * len)))
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 
131 static int concat_read(URLContext *h, unsigned char *buf, int size)
132 {
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;
137 
138  while (size > 0) {
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)
143  break;
144  result = 0;
145  }
146  if (result < 0)
147  return total ? total : result;
148  total += result;
149  buf += result;
150  size -= result;
151  }
152  data->current = i;
153  return total ? total : result;
154 }
155 
156 static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
157 {
158  int64_t result;
159  struct concat_data *data = h->priv_data;
160  struct concat_nodes *nodes = data->nodes;
161  size_t i;
162 
163  if ((whence & AVSEEK_SIZE))
164  return data->total_size;
165  switch (whence) {
166  case SEEK_END:
167  for (i = data->length - 1; i && pos < -nodes[i].size; i--)
168  pos += nodes[i].size;
169  break;
170  case SEEK_CUR:
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);
175  whence = SEEK_SET;
176  /* fall through with the absolute position */
177  case SEEK_SET:
178  for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
179  pos -= nodes[i].size;
180  break;
181  default:
182  return AVERROR(EINVAL);
183  }
184 
185  result = ffurl_seek(nodes[i].uc, pos, whence);
186  if (result >= 0) {
187  data->current = i;
188  while (i)
189  result += nodes[--i].size;
190  }
191  return result;
192 }
193 
195  .name = "concat",
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",
202 };
concat_data::length
size_t length
number of cat'ed nodes
Definition: concat.c:39
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
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:436
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
concat_data::total_size
uint64_t total_size
Definition: concat.c:41
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:531
data
const char data[16]
Definition: mxf.c:91
ffurl_close
int ffurl_close(URLContext *h)
Definition: avio.c:469
URLProtocol
Definition: url.h:54
AV_CAT_SEPARATOR
#define AV_CAT_SEPARATOR
Definition: concat.c:30
concat_nodes::size
int64_t size
url filesize
Definition: concat.c:34
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:307
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
size
int size
Definition: twinvq_data.h:11134
concat_data::nodes
struct concat_nodes * nodes
list of nodes to concat
Definition: concat.c:38
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:161
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:131
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
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
URLContext
Definition: url.h:38
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
concat_open
static av_cold int concat_open(URLContext *h, const char *uri, int flags)
Definition: concat.c:59
url.h
len
int len
Definition: vorbis_enc_data.h:452
concat_data::current
size_t current
index of currently read node
Definition: concat.c:40
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:446
concat_seek
static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
Definition: concat.c:156
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
concat_nodes::uc
URLContext * uc
node's URLContext
Definition: concat.c:33
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:409
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:37
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
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:613
ff_concat_protocol
const URLProtocol ff_concat_protocol
Definition: concat.c:194
h
h
Definition: vp9dsp_template.c:2038
concat_close
static av_cold int concat_close(URLContext *h)
Definition: concat.c:44
avstring.h
concat_nodes
Definition: concat.c:32