Mercurial > libavformat.hg
annotate avio.c @ 124:a1ad0f8b75bf libavformat
first pass at a new VP3 video decoder
author | tmmm |
---|---|
date | Mon, 05 May 2003 02:54:15 +0000 |
parents | 22e4d9d88e25 |
children | 16c4e43f34e5 |
rev | line source |
---|---|
0 | 1 /* |
2 * Unbuffered io for ffmpeg system | |
3 * Copyright (c) 2001 Fabrice Bellard | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 #include "avformat.h" | |
67
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
20 #include <ctype.h> |
0 | 21 |
22 URLProtocol *first_protocol = NULL; | |
23 | |
24 int register_protocol(URLProtocol *protocol) | |
25 { | |
26 URLProtocol **p; | |
27 p = &first_protocol; | |
28 while (*p != NULL) p = &(*p)->next; | |
29 *p = protocol; | |
30 protocol->next = NULL; | |
31 return 0; | |
32 } | |
33 | |
34 int url_open(URLContext **puc, const char *filename, int flags) | |
35 { | |
36 URLContext *uc; | |
37 URLProtocol *up; | |
38 const char *p; | |
39 char proto_str[128], *q; | |
40 int err; | |
41 | |
42 p = filename; | |
43 q = proto_str; | |
44 while (*p != '\0' && *p != ':') { | |
67
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
45 /* protocols can only contain alphabetic chars */ |
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
46 if (!isalpha(*p)) |
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
47 goto file_proto; |
0 | 48 if ((q - proto_str) < sizeof(proto_str) - 1) |
49 *q++ = *p; | |
50 p++; | |
51 } | |
52 /* if the protocol has length 1, we consider it is a dos drive */ | |
53 if (*p == '\0' || (q - proto_str) <= 1) { | |
67
22e4d9d88e25
avoid false URL protocol detection when using ':' in filenames
bellard
parents:
19
diff
changeset
|
54 file_proto: |
0 | 55 strcpy(proto_str, "file"); |
56 } else { | |
57 *q = '\0'; | |
58 } | |
59 | |
60 up = first_protocol; | |
61 while (up != NULL) { | |
62 if (!strcmp(proto_str, up->name)) | |
63 goto found; | |
64 up = up->next; | |
65 } | |
66 err = -ENOENT; | |
67 goto fail; | |
68 found: | |
19 | 69 uc = av_malloc(sizeof(URLContext) + strlen(filename)); |
0 | 70 if (!uc) { |
71 err = -ENOMEM; | |
72 goto fail; | |
73 } | |
19 | 74 strcpy(uc->filename, filename); |
0 | 75 uc->prot = up; |
76 uc->flags = flags; | |
77 uc->is_streamed = 0; /* default = not streamed */ | |
78 uc->max_packet_size = 0; /* default: stream file */ | |
79 err = up->url_open(uc, filename, flags); | |
80 if (err < 0) { | |
81 av_free(uc); | |
82 *puc = NULL; | |
83 return err; | |
84 } | |
85 *puc = uc; | |
86 return 0; | |
87 fail: | |
88 *puc = NULL; | |
89 return err; | |
90 } | |
91 | |
92 int url_read(URLContext *h, unsigned char *buf, int size) | |
93 { | |
94 int ret; | |
95 if (h->flags & URL_WRONLY) | |
96 return -EIO; | |
97 ret = h->prot->url_read(h, buf, size); | |
98 return ret; | |
99 } | |
100 | |
101 int url_write(URLContext *h, unsigned char *buf, int size) | |
102 { | |
103 int ret; | |
104 if (!(h->flags & (URL_WRONLY | URL_RDWR))) | |
105 return -EIO; | |
106 /* avoid sending too big packets */ | |
107 if (h->max_packet_size && size > h->max_packet_size) | |
108 return -EIO; | |
109 ret = h->prot->url_write(h, buf, size); | |
110 return ret; | |
111 } | |
112 | |
113 offset_t url_seek(URLContext *h, offset_t pos, int whence) | |
114 { | |
115 offset_t ret; | |
116 | |
117 if (!h->prot->url_seek) | |
118 return -EPIPE; | |
119 ret = h->prot->url_seek(h, pos, whence); | |
120 return ret; | |
121 } | |
122 | |
123 int url_close(URLContext *h) | |
124 { | |
125 int ret; | |
126 | |
127 ret = h->prot->url_close(h); | |
128 av_free(h); | |
129 return ret; | |
130 } | |
131 | |
132 int url_exist(const char *filename) | |
133 { | |
134 URLContext *h; | |
135 if (url_open(&h, filename, URL_RDONLY) < 0) | |
136 return 0; | |
137 url_close(h); | |
138 return 1; | |
139 } | |
140 | |
141 offset_t url_filesize(URLContext *h) | |
142 { | |
143 offset_t pos, size; | |
144 | |
145 pos = url_seek(h, 0, SEEK_CUR); | |
146 size = url_seek(h, 0, SEEK_END); | |
147 url_seek(h, pos, SEEK_SET); | |
148 return size; | |
149 } | |
150 | |
151 /* | |
152 * Return the maximum packet size associated to packetized file | |
153 * handle. If the file is not packetized (stream like http or file on | |
154 * disk), then 0 is returned. | |
155 * | |
156 * @param h file handle | |
157 * @return maximum packet size in bytes | |
158 */ | |
159 int url_get_max_packet_size(URLContext *h) | |
160 { | |
161 return h->max_packet_size; | |
162 } | |
19 | 163 |
164 void url_get_filename(URLContext *h, char *buf, int buf_size) | |
165 { | |
166 pstrcpy(buf, buf_size, h->filename); | |
167 } |