878
|
1 /*
|
|
2 * Buffered I/O for ffmpeg system
|
|
3 * Copyright (c) 2000,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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 */
|
|
19 #include "avformat.h"
|
|
20 #include "avio.h"
|
|
21 #include <stdarg.h>
|
|
22
|
|
23 #define IO_BUFFER_SIZE 32768
|
|
24
|
|
25 int init_put_byte(ByteIOContext *s,
|
|
26 unsigned char *buffer,
|
|
27 int buffer_size,
|
|
28 int write_flag,
|
|
29 void *opaque,
|
|
30 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
|
|
31 void (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
|
|
32 int (*seek)(void *opaque, offset_t offset, int whence))
|
|
33 {
|
|
34 s->buffer = buffer;
|
|
35 s->buffer_size = buffer_size;
|
|
36 s->buf_ptr = buffer;
|
|
37 s->write_flag = write_flag;
|
|
38 if (!s->write_flag)
|
|
39 s->buf_end = buffer;
|
|
40 else
|
|
41 s->buf_end = buffer + buffer_size;
|
|
42 s->opaque = opaque;
|
|
43 s->write_packet = write_packet;
|
|
44 s->read_packet = read_packet;
|
|
45 s->seek = seek;
|
|
46 s->pos = 0;
|
|
47 s->must_flush = 0;
|
|
48 s->eof_reached = 0;
|
|
49 s->is_streamed = 0;
|
|
50 s->max_packet_size = 0;
|
|
51 return 0;
|
|
52 }
|
|
53
|
|
54 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
|
|
55 {
|
|
56 offset_t offset1;
|
|
57
|
|
58 if (whence != SEEK_CUR && whence != SEEK_SET)
|
|
59 return -EINVAL;
|
|
60
|
|
61 {
|
|
62 if (whence == SEEK_CUR) {
|
|
63 offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer);
|
|
64 if (offset == 0)
|
|
65 return offset1;
|
|
66 offset += offset1;
|
|
67 }
|
|
68 offset1 = offset - (s->pos - (s->buf_end - s->buffer));
|
|
69 if (offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
|
|
70 /* can do the seek inside the buffer */
|
|
71 s->buf_ptr = s->buffer + offset1;
|
|
72 } else {
|
|
73 if (!s->seek)
|
|
74 return -EPIPE;
|
|
75 s->buf_ptr = s->buffer;
|
|
76 s->buf_end = s->buffer;
|
|
77 s->seek(s->opaque, offset, SEEK_SET);
|
|
78 s->pos = offset;
|
|
79 }
|
|
80 s->eof_reached = 0;
|
|
81 }
|
|
82 return offset;
|
|
83 }
|
|
84
|
|
85 void url_fskip(ByteIOContext *s, offset_t offset)
|
|
86 {
|
|
87 url_fseek(s, offset, SEEK_CUR);
|
|
88 }
|
|
89
|
|
90 offset_t url_ftell(ByteIOContext *s)
|
|
91 {
|
|
92 return url_fseek(s, 0, SEEK_CUR);
|
|
93 }
|
|
94
|
|
95 int url_feof(ByteIOContext *s)
|
|
96 {
|
|
97 return s->eof_reached;
|
|
98 }
|
|
99
|
|
100 /* Input stream */
|
|
101
|
|
102 static void fill_buffer(ByteIOContext *s)
|
|
103 {
|
|
104 int len;
|
|
105
|
|
106 /* no need to do anything if EOF already reached */
|
|
107 if (s->eof_reached)
|
|
108 return;
|
|
109 len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
|
|
110 if (len <= 0) {
|
|
111 /* do not modify buffer if EOF reached so that a seek back can
|
|
112 be done without rereading data */
|
|
113 s->eof_reached = 1;
|
|
114 } else {
|
|
115 s->pos += len;
|
|
116 s->buf_ptr = s->buffer;
|
|
117 s->buf_end = s->buffer + len;
|
|
118 }
|
|
119 }
|
|
120
|
|
121 /* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
|
|
122 necessary */
|
|
123 /* XXX: put an inline version */
|
|
124 int get_byte(ByteIOContext *s)
|
|
125 {
|
|
126 if (s->buf_ptr < s->buf_end) {
|
|
127 return *s->buf_ptr++;
|
|
128 } else {
|
|
129 fill_buffer(s);
|
|
130 if (s->buf_ptr < s->buf_end)
|
|
131 return *s->buf_ptr++;
|
|
132 else
|
|
133 return 0;
|
|
134 }
|
|
135 }
|
|
136
|
|
137 /* NOTE: return URL_EOF (-1) if EOF */
|
|
138 int url_fgetc(ByteIOContext *s)
|
|
139 {
|
|
140 if (s->buf_ptr < s->buf_end) {
|
|
141 return *s->buf_ptr++;
|
|
142 } else {
|
|
143 fill_buffer(s);
|
|
144 if (s->buf_ptr < s->buf_end)
|
|
145 return *s->buf_ptr++;
|
|
146 else
|
|
147 return URL_EOF;
|
|
148 }
|
|
149 }
|
|
150
|
|
151 int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
|
|
152 {
|
|
153 int len, size1;
|
|
154
|
|
155 size1 = size;
|
|
156 while (size > 0) {
|
|
157 len = s->buf_end - s->buf_ptr;
|
|
158 if (len > size)
|
|
159 len = size;
|
|
160 if (len == 0) {
|
|
161 fill_buffer(s);
|
|
162 len = s->buf_end - s->buf_ptr;
|
|
163 if (len == 0)
|
|
164 break;
|
|
165 } else {
|
|
166 memcpy(buf, s->buf_ptr, len);
|
|
167 buf += len;
|
|
168 s->buf_ptr += len;
|
|
169 size -= len;
|
|
170 }
|
|
171 }
|
|
172 return size1 - size;
|
|
173 }
|
|
174
|
|
175 unsigned int get_le16(ByteIOContext *s)
|
|
176 {
|
|
177 unsigned int val;
|
|
178 val = get_byte(s);
|
|
179 val |= get_byte(s) << 8;
|
|
180 return val;
|
|
181 }
|
|
182
|
|
183 unsigned int get_le32(ByteIOContext *s)
|
|
184 {
|
|
185 unsigned int val;
|
|
186 val = get_byte(s);
|
|
187 val |= get_byte(s) << 8;
|
|
188 val |= get_byte(s) << 16;
|
|
189 val |= get_byte(s) << 24;
|
|
190 return val;
|
|
191 }
|
|
192
|
|
193 uint64_t get_le64(ByteIOContext *s)
|
|
194 {
|
|
195 uint64_t val;
|
|
196 val = (uint64_t)get_le32(s);
|
|
197 val |= (uint64_t)get_le32(s) << 32;
|
|
198 return val;
|
|
199 }
|
|
200
|
|
201 unsigned int get_be16(ByteIOContext *s)
|
|
202 {
|
|
203 unsigned int val;
|
|
204 val = get_byte(s) << 8;
|
|
205 val |= get_byte(s);
|
|
206 return val;
|
|
207 }
|
|
208
|
|
209 unsigned int get_be32(ByteIOContext *s)
|
|
210 {
|
|
211 unsigned int val;
|
|
212 val = get_byte(s) << 24;
|
|
213 val |= get_byte(s) << 16;
|
|
214 val |= get_byte(s) << 8;
|
|
215 val |= get_byte(s);
|
|
216 return val;
|
|
217 }
|
|
218
|
|
219 double get_be64_double(ByteIOContext *s)
|
|
220 {
|
|
221 union {
|
|
222 double d;
|
|
223 uint64_t ull;
|
|
224 } u;
|
|
225
|
|
226 u.ull = get_be64(s);
|
|
227 return u.d;
|
|
228 }
|
|
229
|
|
230 char *get_strz(ByteIOContext *s, char *buf, int maxlen)
|
|
231 {
|
|
232 int i = 0;
|
|
233 char c;
|
|
234
|
|
235 while ((c = get_byte(s))) {
|
|
236 if (i < maxlen-1)
|
|
237 buf[i++] = c;
|
|
238 }
|
|
239
|
|
240 buf[i] = 0; /* Ensure null terminated, but may be truncated */
|
|
241
|
|
242 return buf;
|
|
243 }
|
|
244
|
|
245 uint64_t get_be64(ByteIOContext *s)
|
|
246 {
|
|
247 uint64_t val;
|
|
248 val = (uint64_t)get_be32(s) << 32;
|
|
249 val |= (uint64_t)get_be32(s);
|
|
250 return val;
|
|
251 }
|
|
252
|
|
253 /* link with avio functions */
|
|
254
|
|
255 #define url_write_packet NULL
|
|
256
|
|
257 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
|
|
258 {
|
|
259 URLContext *h = opaque;
|
|
260 return url_read(h, buf, buf_size);
|
|
261 }
|
|
262
|
|
263 static int url_seek_packet(void *opaque, int64_t offset, int whence)
|
|
264 {
|
|
265 URLContext *h = opaque;
|
|
266 url_seek(h, offset, whence);
|
|
267 return 0;
|
|
268 }
|
|
269
|
|
270 int url_fdopen(ByteIOContext *s, URLContext *h)
|
|
271 {
|
|
272 uint8_t *buffer;
|
|
273 int buffer_size, max_packet_size;
|
|
274
|
|
275 max_packet_size = url_get_max_packet_size(h);
|
|
276 if (max_packet_size) {
|
|
277 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
|
|
278 } else {
|
|
279 buffer_size = IO_BUFFER_SIZE;
|
|
280 }
|
|
281 buffer = av_mallocz(buffer_size);
|
|
282 if (!buffer)
|
|
283 return -ENOMEM;
|
|
284
|
|
285 if (init_put_byte(s, buffer, buffer_size,
|
|
286 (h->flags & URL_WRONLY) != 0, h,
|
|
287 url_read_packet, url_write_packet, url_seek_packet) < 0) {
|
|
288 free(buffer);
|
|
289 return -EIO;
|
|
290 }
|
|
291 s->is_streamed = h->is_streamed;
|
|
292 s->max_packet_size = max_packet_size;
|
|
293 return 0;
|
|
294 }
|
|
295
|
|
296 /* XXX: must be called before any I/O */
|
|
297 int url_setbufsize(ByteIOContext *s, int buf_size)
|
|
298 {
|
|
299 uint8_t *buffer;
|
|
300 buffer = av_malloc(buf_size);
|
|
301 if (!buffer)
|
|
302 return -ENOMEM;
|
|
303
|
|
304 free(s->buffer);
|
|
305 s->buffer = buffer;
|
|
306 s->buffer_size = buf_size;
|
|
307 s->buf_ptr = buffer;
|
|
308 if (!s->write_flag)
|
|
309 s->buf_end = buffer;
|
|
310 else
|
|
311 s->buf_end = buffer + buf_size;
|
|
312 return 0;
|
|
313 }
|
|
314
|
|
315 /* NOTE: when opened as read/write, the buffers are only used for
|
|
316 reading */
|
|
317 int url_fopen(ByteIOContext *s, const char *filename, int flags)
|
|
318 {
|
|
319 URLContext *h;
|
|
320 int err;
|
|
321
|
|
322 err = url_open(&h, filename, flags);
|
|
323 if (err < 0)
|
|
324 return err;
|
|
325 err = url_fdopen(s, h);
|
|
326 if (err < 0) {
|
|
327 url_close(h);
|
|
328 return err;
|
|
329 }
|
|
330 return 0;
|
|
331 }
|
|
332
|
|
333 int url_vfdopen(ByteIOContext *s, VFSFile *fd)
|
|
334 {
|
|
335 URLContext *h;
|
|
336 int err;
|
|
337
|
|
338 err = url_vopen(&h, fd);
|
|
339 if (err < 0)
|
|
340 return err;
|
|
341 err = url_fdopen(s, h);
|
|
342 if (err < 0) {
|
|
343 return err;
|
|
344 }
|
|
345 return 0;
|
|
346 }
|
|
347
|
|
348 int url_fclose(ByteIOContext *s)
|
|
349 {
|
|
350 URLContext *h = s->opaque;
|
|
351
|
|
352 free(s->buffer);
|
|
353 memset(s, 0, sizeof(ByteIOContext));
|
|
354 return url_close(h);
|
|
355 }
|
|
356
|
|
357 URLContext *url_fileno(ByteIOContext *s)
|
|
358 {
|
|
359 return s->opaque;
|
|
360 }
|
|
361
|
|
362 /* note: unlike fgets, the EOL character is not returned and a whole
|
|
363 line is parsed. return NULL if first char read was EOF */
|
|
364 char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
|
|
365 {
|
|
366 int c;
|
|
367 char *q;
|
|
368
|
|
369 c = url_fgetc(s);
|
|
370 if (c == EOF)
|
|
371 return NULL;
|
|
372 q = buf;
|
|
373 for(;;) {
|
|
374 if (c == EOF || c == '\n')
|
|
375 break;
|
|
376 if ((q - buf) < buf_size - 1)
|
|
377 *q++ = c;
|
|
378 c = url_fgetc(s);
|
|
379 }
|
|
380 if (buf_size > 0)
|
|
381 *q = '\0';
|
|
382 return buf;
|
|
383 }
|
|
384
|
|
385 /*
|
|
386 * Return the maximum packet size associated to packetized buffered file
|
|
387 * handle. If the file is not packetized (stream like http or file on
|
|
388 * disk), then 0 is returned.
|
|
389 *
|
|
390 * @param h buffered file handle
|
|
391 * @return maximum packet size in bytes
|
|
392 */
|
|
393 int url_fget_max_packet_size(ByteIOContext *s)
|
|
394 {
|
|
395 return s->max_packet_size;
|
|
396 }
|
|
397
|