Browse code

Implement a physical concatenation protocol.

Patch by Michele Orrù reverse(<moc.liamg@yp.rekam>).

Originally committed as revision 21666 to svn://svn.ffmpeg.org/ffmpeg/trunk

Michele Orrù authored on 2010/02/07 19:25:58
Showing 5 changed files
... ...
@@ -54,6 +54,7 @@ version <next>:
54 54
 - Bink demuxer and Bink Audio decoder
55 55
 - enable symbol versioning by default for linkers that support it
56 56
 - IFF PBM/ILBM bitmap decoder
57
+- concat protocol
57 58
 
58 59
 
59 60
 
... ...
@@ -269,6 +269,7 @@ OBJS-$(CONFIG_RTMP_PROTOCOL)             += rtmpproto.o rtmppkt.o
269 269
 OBJS-$(CONFIG_RTP_PROTOCOL)              += rtpproto.o
270 270
 OBJS-$(CONFIG_TCP_PROTOCOL)              += tcp.o
271 271
 OBJS-$(CONFIG_UDP_PROTOCOL)              += udp.o
272
+OBJS-$(CONFIG_CONCAT_PROTOCOL)           += concat.o
272 273
 
273 274
 # libavdevice dependencies
274 275
 OBJS-$(CONFIG_JACK_INDEV)                += timefilter.o
... ...
@@ -221,4 +221,5 @@ void av_register_all(void)
221 221
     REGISTER_PROTOCOL (RTP, rtp);
222 222
     REGISTER_PROTOCOL (TCP, tcp);
223 223
     REGISTER_PROTOCOL (UDP, udp);
224
+    REGISTER_PROTOCOL (CONCAT, concat);
224 225
 }
... ...
@@ -22,7 +22,7 @@
22 22
 #define AVFORMAT_AVFORMAT_H
23 23
 
24 24
 #define LIBAVFORMAT_VERSION_MAJOR 52
25
-#define LIBAVFORMAT_VERSION_MINOR 50
25
+#define LIBAVFORMAT_VERSION_MINOR 51
26 26
 #define LIBAVFORMAT_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
29 29
new file mode 100644
... ...
@@ -0,0 +1,198 @@
0
+/*
1
+ * Concat URL protocol
2
+ * Copyright (c) 2006 Steve Lhomme
3
+ * Copyright (c) 2007 Wolfram Gloger
4
+ * Copyright (c) 2010 Michele Orrù
5
+ *
6
+ * This file is part of FFmpeg.
7
+ *
8
+ * FFmpeg is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * FFmpeg is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with FFmpeg; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ */
22
+
23
+#include "avformat.h"
24
+#include "libavutil/avstring.h"
25
+#include "libavutil/mem.h"
26
+
27
+#define AV_CAT_SEPARATOR "|"
28
+
29
+struct concat_nodes {
30
+    URLContext *uc;                ///< node's URLContext
31
+    int64_t     size;              ///< url filesize
32
+};
33
+
34
+struct concat_data {
35
+    struct concat_nodes *nodes;    ///< list of nodes to concat
36
+    size_t               length;   ///< number of cat'ed nodes
37
+    size_t               current;  ///< index of currently read node
38
+};
39
+
40
+static av_cold int concat_close(URLContext *h)
41
+{
42
+    int err = 0;
43
+    size_t i;
44
+    struct concat_data  *data  = h->priv_data;
45
+    struct concat_nodes *nodes = data->nodes;
46
+
47
+    for (i = 0; i != data->length; i++)
48
+        err |= url_close(nodes[i].uc);
49
+
50
+    av_freep(&data->nodes);
51
+    av_freep(&h->priv_data);
52
+
53
+    return err < 0 ? -1 : 0;
54
+}
55
+
56
+static av_cold int concat_open(URLContext *h, const char *uri, int flags)
57
+{
58
+    char *node_uri = NULL, *tmp_uri;
59
+    int err = 0;
60
+    int64_t size;
61
+    size_t  len, i;
62
+    URLContext *uc;
63
+    struct concat_data  *data;
64
+    struct concat_nodes *nodes;
65
+
66
+    av_strstart(uri, "concat:", &uri);
67
+
68
+    /* creating data */
69
+    if (!(data = av_mallocz(sizeof(*data))))
70
+        return AVERROR(ENOMEM);
71
+    h->priv_data = data;
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
+    if (!(nodes = av_malloc(sizeof(*nodes) * len))) {
82
+        av_freep(&h->priv_data);
83
+        return AVERROR(ENOMEM);
84
+    } else
85
+        data->nodes = nodes;
86
+
87
+    /* handle input */
88
+    if (!*uri)
89
+        err = AVERROR(ENOENT);
90
+    for (i = 0; *uri; i++) {
91
+        /* parsing uri */
92
+        len = strcspn(uri, AV_CAT_SEPARATOR);
93
+        if (!(tmp_uri = av_realloc(node_uri, len+1))) {
94
+            err = AVERROR(ENOMEM);
95
+            break;
96
+        } else
97
+            node_uri = tmp_uri;
98
+        av_strlcpy(node_uri, uri, len+1);
99
+        uri += len + strspn(uri+len, AV_CAT_SEPARATOR);
100
+
101
+        /* creating URLContext */
102
+        if ((err = url_open(&uc, node_uri, flags)) < 0)
103
+            break;
104
+
105
+        /* creating size */
106
+        if ((size = url_filesize(uc)) < 0) {
107
+            url_close(uc);
108
+            err = AVERROR(ENOSYS);
109
+            break;
110
+        }
111
+
112
+        /* assembling */
113
+        nodes[i].uc   = uc;
114
+        nodes[i].size = size;
115
+    }
116
+    av_free(node_uri);
117
+    data->length = i;
118
+
119
+    if (err < 0)
120
+        concat_close(h);
121
+    else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
122
+        concat_close(h);
123
+        err = AVERROR(ENOMEM);
124
+    } else
125
+        data->nodes = nodes;
126
+    return err;
127
+}
128
+
129
+static int concat_read(URLContext *h, unsigned char *buf, int size)
130
+{
131
+    int result, total = 0;
132
+    struct concat_data  *data  = h->priv_data;
133
+    struct concat_nodes *nodes = data->nodes;
134
+    size_t i = data->current;
135
+
136
+    while (size > 0) {
137
+        result = url_read(nodes[i].uc, buf, size);
138
+        if (result < 0)
139
+            return total ? total : result;
140
+        if (!result)
141
+            if (i + 1 == data->length ||
142
+                url_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
143
+                break;
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;
162
+             i && pos < -nodes[i-1].size;
163
+             i--)
164
+            pos += nodes[i-1].size;
165
+        break;
166
+    case SEEK_CUR:
167
+        /* get the absolute position */
168
+        for (i = 0; i != data->current; i++)
169
+            pos += nodes[i].size;
170
+        pos += url_seek(nodes[i].uc, 0, SEEK_CUR);
171
+        whence = SEEK_SET;
172
+        /* fall through with the absolute position */
173
+    case SEEK_SET:
174
+        for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
175
+            pos -= nodes[i].size;
176
+        break;
177
+    default:
178
+        return AVERROR(EINVAL);
179
+    }
180
+
181
+    result = url_seek(nodes[i].uc, pos, whence);
182
+    if (result >= 0) {
183
+        data->current = i;
184
+        while (i)
185
+            result += nodes[i--].size;
186
+    }
187
+    return result;
188
+}
189
+
190
+URLProtocol concat_protocol = {
191
+    "concat",
192
+    concat_open,
193
+    concat_read,
194
+    NULL,
195
+    concat_seek,
196
+    concat_close,
197
+};