]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
Maxis XA demuxer and decoder.
[ffmpeg] / libavformat / avio.c
1 /*
2  * Unbuffered io for ffmpeg system
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #include "avformat.h"
22 #include "avstring.h"
23 #include "opt.h"
24
25 #if LIBAVFORMAT_VERSION_MAJOR >= 53
26 /** @name Logging context. */
27 /*@{*/
28 static const char *urlcontext_to_name(void *ptr)
29 {
30     URLContext *h = (URLContext *)ptr;
31     if(h->prot) return h->prot->name;
32     else        return "NULL";
33 }
34 static const AVOption options[] = {{NULL}};
35 static const AVClass urlcontext_class =
36         { "URLContext", urlcontext_to_name, options };
37 /*@}*/
38 #endif
39
40 static int default_interrupt_cb(void);
41
42 URLProtocol *first_protocol = NULL;
43 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
44
45 URLProtocol *av_protocol_next(URLProtocol *p)
46 {
47     if(p) return p->next;
48     else  return first_protocol;
49 }
50
51 int register_protocol(URLProtocol *protocol)
52 {
53     URLProtocol **p;
54     p = &first_protocol;
55     while (*p != NULL) p = &(*p)->next;
56     *p = protocol;
57     protocol->next = NULL;
58     return 0;
59 }
60
61 int url_open(URLContext **puc, const char *filename, int flags)
62 {
63     URLContext *uc;
64     URLProtocol *up;
65     const char *p;
66     char proto_str[128], *q;
67     int err;
68
69     p = filename;
70     q = proto_str;
71     while (*p != '\0' && *p != ':') {
72         /* protocols can only contain alphabetic chars */
73         if (!isalpha(*p))
74             goto file_proto;
75         if ((q - proto_str) < sizeof(proto_str) - 1)
76             *q++ = *p;
77         p++;
78     }
79     /* if the protocol has length 1, we consider it is a dos drive */
80     if (*p == '\0' || (q - proto_str) <= 1) {
81     file_proto:
82         strcpy(proto_str, "file");
83     } else {
84         *q = '\0';
85     }
86
87     up = first_protocol;
88     while (up != NULL) {
89         if (!strcmp(proto_str, up->name))
90             goto found;
91         up = up->next;
92     }
93     err = AVERROR(ENOENT);
94     goto fail;
95  found:
96     uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
97     if (!uc) {
98         err = AVERROR(ENOMEM);
99         goto fail;
100     }
101 #if LIBAVFORMAT_VERSION_MAJOR >= 53
102     uc->av_class = &urlcontext_class;
103 #endif
104     uc->filename = (char *) &uc[1];
105     strcpy(uc->filename, filename);
106     uc->prot = up;
107     uc->flags = flags;
108     uc->is_streamed = 0; /* default = not streamed */
109     uc->max_packet_size = 0; /* default: stream file */
110     err = up->url_open(uc, filename, flags);
111     if (err < 0) {
112         av_free(uc);
113         *puc = NULL;
114         return err;
115     }
116     *puc = uc;
117     return 0;
118  fail:
119     *puc = NULL;
120     return err;
121 }
122
123 int url_read(URLContext *h, unsigned char *buf, int size)
124 {
125     int ret;
126     if (h->flags & URL_WRONLY)
127         return AVERROR(EIO);
128     ret = h->prot->url_read(h, buf, size);
129     return ret;
130 }
131
132 int url_write(URLContext *h, unsigned char *buf, int size)
133 {
134     int ret;
135     if (!(h->flags & (URL_WRONLY | URL_RDWR)))
136         return AVERROR(EIO);
137     /* avoid sending too big packets */
138     if (h->max_packet_size && size > h->max_packet_size)
139         return AVERROR(EIO);
140     ret = h->prot->url_write(h, buf, size);
141     return ret;
142 }
143
144 offset_t url_seek(URLContext *h, offset_t pos, int whence)
145 {
146     offset_t ret;
147
148     if (!h->prot->url_seek)
149         return AVERROR(EPIPE);
150     ret = h->prot->url_seek(h, pos, whence);
151     return ret;
152 }
153
154 int url_close(URLContext *h)
155 {
156     int ret = 0;
157     if (!h) return 0; /* can happen when url_open fails */
158
159     if (h->prot->url_close)
160         ret = h->prot->url_close(h);
161     av_free(h);
162     return ret;
163 }
164
165 int url_exist(const char *filename)
166 {
167     URLContext *h;
168     if (url_open(&h, filename, URL_RDONLY) < 0)
169         return 0;
170     url_close(h);
171     return 1;
172 }
173
174 offset_t url_filesize(URLContext *h)
175 {
176     offset_t pos, size;
177
178     size= url_seek(h, 0, AVSEEK_SIZE);
179     if(size<0){
180         pos = url_seek(h, 0, SEEK_CUR);
181         if ((size = url_seek(h, -1, SEEK_END)) < 0)
182             return size;
183         size++;
184         url_seek(h, pos, SEEK_SET);
185     }
186     return size;
187 }
188
189 int url_get_max_packet_size(URLContext *h)
190 {
191     return h->max_packet_size;
192 }
193
194 void url_get_filename(URLContext *h, char *buf, int buf_size)
195 {
196     av_strlcpy(buf, h->filename, buf_size);
197 }
198
199
200 static int default_interrupt_cb(void)
201 {
202     return 0;
203 }
204
205 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
206 {
207     if (!interrupt_cb)
208         interrupt_cb = default_interrupt_cb;
209     url_interrupt_cb = interrupt_cb;
210 }
211
212 int av_url_read_pause(URLContext *h, int pause)
213 {
214     if (!h->prot->url_read_pause)
215         return AVERROR(ENOSYS);
216     return h->prot->url_read_pause(h, pause);
217 }
218
219 offset_t av_url_read_seek(URLContext *h,
220         int stream_index, int64_t timestamp, int flags)
221 {
222     if (!h->prot->url_read_seek)
223         return AVERROR(ENOSYS);
224     return h->prot->url_read_seek(h, stream_index, timestamp, flags);
225 }