FFmpeg  4.4.6
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  len++;
77  }
78  }
79 
80  if (!(nodes = av_realloc_array(NULL, len, sizeof(*nodes))))
81  return AVERROR(ENOMEM);
82  else
83  data->nodes = nodes;
84 
85  /* handle input */
86  if (!*uri)
87  err = AVERROR(ENOENT);
88  for (i = 0; *uri; i++) {
89  /* parsing uri */
90  len = strcspn(uri, AV_CAT_SEPARATOR);
91  if ((err = av_reallocp(&node_uri, len + 1)) < 0)
92  break;
93  av_strlcpy(node_uri, uri, len + 1);
94  uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
95 
96  /* creating URLContext */
97  err = ffurl_open_whitelist(&uc, node_uri, flags,
98  &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
99  if (err < 0)
100  break;
101 
102  /* creating size */
103  if ((size = ffurl_size(uc)) < 0) {
104  ffurl_close(uc);
105  err = AVERROR(ENOSYS);
106  break;
107  }
108 
109  /* assembling */
110  nodes[i].uc = uc;
111  nodes[i].size = size;
112  total_size += size;
113  }
114  av_free(node_uri);
115  data->length = i;
116 
117  if (err < 0)
118  concat_close(h);
119  else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
120  concat_close(h);
121  err = AVERROR(ENOMEM);
122  } else
123  data->nodes = nodes;
124  data->total_size = total_size;
125  return err;
126 }
127 
128 static int concat_read(URLContext *h, unsigned char *buf, int size)
129 {
130  int result, total = 0;
131  struct concat_data *data = h->priv_data;
132  struct concat_nodes *nodes = data->nodes;
133  size_t i = data->current;
134 
135  while (size > 0) {
136  result = ffurl_read(nodes[i].uc, buf, size);
137  if (result == AVERROR_EOF) {
138  if (i + 1 == data->length ||
139  ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
140  break;
141  result = 0;
142  }
143  if (result < 0)
144  return total ? total : result;
145  total += result;
146  buf += result;
147  size -= result;
148  }
149  data->current = i;
150  return total ? total : result;
151 }
152 
153 static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
154 {
155  int64_t result;
156  struct concat_data *data = h->priv_data;
157  struct concat_nodes *nodes = data->nodes;
158  size_t i;
159 
160  if ((whence & AVSEEK_SIZE))
161  return data->total_size;
162  switch (whence) {
163  case SEEK_END:
164  for (i = data->length - 1; i && pos < -nodes[i].size; i--)
165  pos += nodes[i].size;
166  break;
167  case SEEK_CUR:
168  /* get the absolute position */
169  for (i = 0; i != data->current; i++)
170  pos += nodes[i].size;
171  pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
172  whence = SEEK_SET;
173  /* fall through with the absolute position */
174  case SEEK_SET:
175  for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
176  pos -= nodes[i].size;
177  break;
178  default:
179  return AVERROR(EINVAL);
180  }
181 
182  result = ffurl_seek(nodes[i].uc, pos, whence);
183  if (result >= 0) {
184  data->current = i;
185  while (i)
186  result += nodes[--i].size;
187  }
188  return result;
189 }
190 
192  .name = "concat",
193  .url_open = concat_open,
194  .url_read = concat_read,
195  .url_seek = concat_seek,
196  .url_close = concat_close,
197  .priv_data_size = sizeof(struct concat_data),
198  .default_whitelist = "concat,file,subfile",
199 };
#define av_cold
Definition: attributes.h:88
Main libavformat public API header.
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:404
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:309
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:431
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:608
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:441
int ffurl_close(URLContext *h)
Definition: avio.c:464
#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
#define flags(name, subs,...)
Definition: cbs_av1.c:572
static int concat_read(URLContext *h, unsigned char *buf, int size)
Definition: concat.c:128
static av_cold int concat_close(URLContext *h)
Definition: concat.c:44
#define AV_CAT_SEPARATOR
Definition: concat.c:30
const URLProtocol ff_concat_protocol
Definition: concat.c:191
static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
Definition: concat.c:153
static av_cold int concat_open(URLContext *h, const char *uri, int flags)
Definition: concat.c:59
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
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
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array.
Definition: mem.c:198
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
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
int i
Definition: input.c:407
Memory handling functions.
const char data[16]
Definition: mxf.c:142
unsigned int pos
Definition: spdifenc.c:412
Definition: url.h:38
const char * name
Definition: url.h:55
size_t length
number of cat'ed nodes
Definition: concat.c:39
uint64_t total_size
Definition: concat.c:41
size_t current
index of currently read node
Definition: concat.c:40
struct concat_nodes * nodes
list of nodes to concat
Definition: concat.c:38
URLContext * uc
node's URLContext
Definition: concat.c:33
int64_t size
url filesize
Definition: concat.c:34
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
int size
unbuffered private I/O API
int len