~ubuntu-branches/ubuntu/utopic/libav/utopic

« back to all changes in this revision

Viewing changes to libavformat/concat.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-03-20 12:09:31 UTC
  • Revision ID: james.westby@ubuntu.com-20110320120931-nfhi9tiok27gxhw1
Tags: upstream-0.6.2
Import upstream version 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "avformat.h"
 
25
#include "libavutil/avstring.h"
 
26
#include "libavutil/mem.h"
 
27
 
 
28
#define AV_CAT_SEPARATOR "|"
 
29
 
 
30
struct concat_nodes {
 
31
    URLContext *uc;                ///< node's URLContext
 
32
    int64_t     size;              ///< url filesize
 
33
};
 
34
 
 
35
struct concat_data {
 
36
    struct concat_nodes *nodes;    ///< list of nodes to concat
 
37
    size_t               length;   ///< number of cat'ed nodes
 
38
    size_t               current;  ///< index of currently read node
 
39
};
 
40
 
 
41
static av_cold int concat_close(URLContext *h)
 
42
{
 
43
    int err = 0;
 
44
    size_t i;
 
45
    struct concat_data  *data  = h->priv_data;
 
46
    struct concat_nodes *nodes = data->nodes;
 
47
 
 
48
    for (i = 0; i != data->length; i++)
 
49
        err |= url_close(nodes[i].uc);
 
50
 
 
51
    av_freep(&data->nodes);
 
52
    av_freep(&h->priv_data);
 
53
 
 
54
    return err < 0 ? -1 : 0;
 
55
}
 
56
 
 
57
static av_cold int concat_open(URLContext *h, const char *uri, int flags)
 
58
{
 
59
    char *node_uri = NULL, *tmp_uri;
 
60
    int err = 0;
 
61
    int64_t size;
 
62
    size_t  len, i;
 
63
    URLContext *uc;
 
64
    struct concat_data  *data;
 
65
    struct concat_nodes *nodes;
 
66
 
 
67
    av_strstart(uri, "concat:", &uri);
 
68
 
 
69
    /* creating data */
 
70
    if (!(data = av_mallocz(sizeof(*data))))
 
71
        return AVERROR(ENOMEM);
 
72
    h->priv_data = data;
 
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
                av_freep(&h->priv_data);
 
79
                return AVERROR(ENAMETOOLONG);
 
80
            }
 
81
 
 
82
    if (!(nodes = av_malloc(sizeof(*nodes) * len))) {
 
83
        av_freep(&h->priv_data);
 
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 (!(tmp_uri = av_realloc(node_uri, len+1))) {
 
95
            err = AVERROR(ENOMEM);
 
96
            break;
 
97
        } else
 
98
            node_uri = tmp_uri;
 
99
        av_strlcpy(node_uri, uri, len+1);
 
100
        uri += len + strspn(uri+len, AV_CAT_SEPARATOR);
 
101
 
 
102
        /* creating URLContext */
 
103
        if ((err = url_open(&uc, node_uri, flags)) < 0)
 
104
            break;
 
105
 
 
106
        /* creating size */
 
107
        if ((size = url_filesize(uc)) < 0) {
 
108
            url_close(uc);
 
109
            err = AVERROR(ENOSYS);
 
110
            break;
 
111
        }
 
112
 
 
113
        /* assembling */
 
114
        nodes[i].uc   = uc;
 
115
        nodes[i].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
    return err;
 
128
}
 
129
 
 
130
static int concat_read(URLContext *h, unsigned char *buf, int size)
 
131
{
 
132
    int result, total = 0;
 
133
    struct concat_data  *data  = h->priv_data;
 
134
    struct concat_nodes *nodes = data->nodes;
 
135
    size_t i = data->current;
 
136
 
 
137
    while (size > 0) {
 
138
        result = url_read(nodes[i].uc, buf, size);
 
139
        if (result < 0)
 
140
            return total ? total : result;
 
141
        if (!result)
 
142
            if (i + 1 == data->length ||
 
143
                url_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
 
144
                break;
 
145
        total += result;
 
146
        buf   += result;
 
147
        size  -= result;
 
148
    }
 
149
    data->current = i;
 
150
    return total;
 
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
    switch (whence) {
 
161
    case SEEK_END:
 
162
        for (i = data->length - 1;
 
163
             i && pos < -nodes[i].size;
 
164
             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 += url_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 = url_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
 
 
191
URLProtocol concat_protocol = {
 
192
    "concat",
 
193
    concat_open,
 
194
    concat_read,
 
195
    NULL,
 
196
    concat_seek,
 
197
    concat_close,
 
198
};