extracted yuv4mpeg from img.c (untested)
[libav.git] / libavformat / avio.c
CommitLineData
de6d9b64
FB
1/*
2 * Unbuffered io for ffmpeg system
19720f15 3 * Copyright (c) 2001 Fabrice Bellard
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20
21URLProtocol *first_protocol = NULL;
22
23int register_protocol(URLProtocol *protocol)
24{
25 URLProtocol **p;
26 p = &first_protocol;
27 while (*p != NULL) p = &(*p)->next;
28 *p = protocol;
29 protocol->next = NULL;
30 return 0;
31}
32
33int url_open(URLContext **puc, const char *filename, int flags)
34{
35 URLContext *uc;
36 URLProtocol *up;
37 const char *p;
38 char proto_str[128], *q;
39 int err;
40
41 p = filename;
42 q = proto_str;
43 while (*p != '\0' && *p != ':') {
44 if ((q - proto_str) < sizeof(proto_str) - 1)
45 *q++ = *p;
46 p++;
47 }
a74127c0
FB
48 /* if the protocol has length 1, we consider it is a dos drive */
49 if (*p == '\0' || (q - proto_str) <= 1) {
de6d9b64
FB
50 strcpy(proto_str, "file");
51 } else {
52 *q = '\0';
53 }
54
55 up = first_protocol;
56 while (up != NULL) {
a74127c0 57 if (!strcmp(proto_str, up->name))
de6d9b64
FB
58 goto found;
59 up = up->next;
60 }
8a9488b5
FB
61 err = -ENOENT;
62 goto fail;
de6d9b64 63 found:
1ea4f593 64 uc = av_malloc(sizeof(URLContext));
8a9488b5
FB
65 if (!uc) {
66 err = -ENOMEM;
67 goto fail;
68 }
de6d9b64
FB
69 uc->prot = up;
70 uc->flags = flags;
71 uc->is_streamed = 0; /* default = not streamed */
8a9488b5 72 uc->max_packet_size = 0; /* default: stream file */
de6d9b64
FB
73 err = up->url_open(uc, filename, flags);
74 if (err < 0) {
1ea4f593 75 av_free(uc);
de6d9b64
FB
76 *puc = NULL;
77 return err;
78 }
79 *puc = uc;
80 return 0;
8a9488b5
FB
81 fail:
82 *puc = NULL;
83 return err;
de6d9b64
FB
84}
85
86int url_read(URLContext *h, unsigned char *buf, int size)
87{
88 int ret;
89 if (h->flags & URL_WRONLY)
90 return -EIO;
91 ret = h->prot->url_read(h, buf, size);
92 return ret;
93}
94
95int url_write(URLContext *h, unsigned char *buf, int size)
96{
97 int ret;
8a9488b5 98 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
de6d9b64 99 return -EIO;
8a9488b5
FB
100 /* avoid sending too big packets */
101 if (h->max_packet_size && size > h->max_packet_size)
102 return -EIO;
de6d9b64
FB
103 ret = h->prot->url_write(h, buf, size);
104 return ret;
105}
106
107offset_t url_seek(URLContext *h, offset_t pos, int whence)
108{
109 offset_t ret;
110
111 if (!h->prot->url_seek)
112 return -EPIPE;
113 ret = h->prot->url_seek(h, pos, whence);
114 return ret;
115}
116
de6d9b64
FB
117int url_close(URLContext *h)
118{
119 int ret;
120
121 ret = h->prot->url_close(h);
1ea4f593 122 av_free(h);
de6d9b64
FB
123 return ret;
124}
125
126int url_exist(const char *filename)
127{
128 URLContext *h;
129 if (url_open(&h, filename, URL_RDONLY) < 0)
130 return 0;
131 url_close(h);
132 return 1;
133}
134
135offset_t url_filesize(URLContext *h)
136{
137 offset_t pos, size;
138
139 pos = url_seek(h, 0, SEEK_CUR);
140 size = url_seek(h, 0, SEEK_END);
141 url_seek(h, pos, SEEK_SET);
142 return size;
143}
8a9488b5
FB
144
145/*
146 * Return the maximum packet size associated to packetized file
147 * handle. If the file is not packetized (stream like http or file on
148 * disk), then 0 is returned.
149 *
150 * @param h file handle
151 * @return maximum packet size in bytes
152 */
153int url_get_max_packet_size(URLContext *h)
154{
155 return h->max_packet_size;
156}