FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 };
42 
44 {
45  int err = 0;
46  size_t i;
47  struct concat_data *data = h->priv_data;
48  struct concat_nodes *nodes = data->nodes;
49 
50  for (i = 0; i != data->length; i++)
51  err |= ffurl_close(nodes[i].uc);
52 
53  av_freep(&data->nodes);
54 
55  return err < 0 ? -1 : 0;
56 }
57 
58 static av_cold int concat_open(URLContext *h, const char *uri, int flags)
59 {
60  char *node_uri = NULL;
61  int err = 0;
62  int64_t size;
63  size_t len, i;
64  URLContext *uc;
65  struct concat_data *data = h->priv_data;
66  struct concat_nodes *nodes;
67 
68  if (!av_strstart(uri, "concat:", &uri)) {
69  av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
70  return AVERROR(EINVAL);
71  }
72 
73  for (i = 0, len = 1; uri[i]; i++) {
74  if (uri[i] == *AV_CAT_SEPARATOR) {
75  /* integer overflow */
76  if (++len == UINT_MAX / sizeof(*nodes)) {
77  av_freep(&h->priv_data);
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  if ((err = ffurl_open(&uc, node_uri, flags,
101  &h->interrupt_callback, NULL)) < 0)
102  break;
103 
104  /* creating size */
105  if ((size = ffurl_size(uc)) < 0) {
106  ffurl_close(uc);
107  err = AVERROR(ENOSYS);
108  break;
109  }
110 
111  /* assembling */
112  nodes[i].uc = uc;
113  nodes[i].size = size;
114  }
115  av_free(node_uri);
116  data->length = i;
117 
118  if (err < 0)
119  concat_close(h);
120  else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
121  concat_close(h);
122  err = AVERROR(ENOMEM);
123  } else
124  data->nodes = nodes;
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 < 0)
138  return total ? total : result;
139  if (!result) {
140  if (i + 1 == data->length ||
141  ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
142  break;
143  }
144  total += result;
145  buf += result;
146  size -= result;
147  }
148  data->current = i;
149  return total;
150 }
151 
152 static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
153 {
154  int64_t result;
155  struct concat_data *data = h->priv_data;
156  struct concat_nodes *nodes = data->nodes;
157  size_t i;
158 
159  switch (whence) {
160  case SEEK_END:
161  for (i = data->length - 1; i && pos < -nodes[i].size; i--)
162  pos += nodes[i].size;
163  break;
164  case SEEK_CUR:
165  /* get the absolute position */
166  for (i = 0; i != data->current; i++)
167  pos += nodes[i].size;
168  pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
169  whence = SEEK_SET;
170  /* fall through with the absolute position */
171  case SEEK_SET:
172  for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
173  pos -= nodes[i].size;
174  break;
175  default:
176  return AVERROR(EINVAL);
177  }
178 
179  result = ffurl_seek(nodes[i].uc, pos, whence);
180  if (result >= 0) {
181  data->current = i;
182  while (i)
183  result += nodes[--i].size;
184  }
185  return result;
186 }
187 
189  .name = "concat",
190  .url_open = concat_open,
191  .url_read = concat_read,
192  .url_seek = concat_seek,
193  .url_close = concat_close,
194  .priv_data_size = sizeof(struct concat_data),
195 };
#define NULL
Definition: coverity.c:32
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static av_cold int concat_close(URLContext *h)
Definition: concat.c:43
memory handling functions
AVIOInterruptCB interrupt_callback
Definition: url.h:48
size_t length
number of cat'ed nodes
Definition: concat.c:39
struct concat_nodes * nodes
list of nodes to concat
Definition: concat.c:38
#define av_cold
Definition: attributes.h:74
static int concat_read(URLContext *h, unsigned char *buf, int size)
Definition: concat.c:128
#define AV_CAT_SEPARATOR
Definition: concat.c:30
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
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
static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
Definition: concat.c:152
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:563
static av_cold int concat_open(URLContext *h, const char *uri, int flags)
Definition: concat.c:58
void * buf
Definition: avisynth_c.h:553
Definition: url.h:39
void * priv_data
Definition: url.h:42
int64_t size
url filesize
Definition: concat.c:34
const char * name
Definition: url.h:53
static int flags
Definition: cpu.c:47
int ffurl_close(URLContext *h)
Definition: avio.c:419
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
Main libavformat public API header.
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:387
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:299
#define av_free(p)
int len
size_t current
index of currently read node
Definition: concat.c:40
#define av_freep(p)
unbuffered private I/O API
URLProtocol ff_concat_protocol
Definition: concat.c:188
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:360
URLContext * uc
node's URLContext
Definition: concat.c:33