Mercurial > libavformat.hg
annotate aviobuf.c @ 441:6635c1e75087 libavformat
bit-exact
author | michael |
---|---|
date | Mon, 12 Apr 2004 20:06:23 +0000 |
parents | a35263593560 |
children | 0fdc96c2f2fe |
rev | line source |
---|---|
0 | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 #include "avformat.h" | |
64 | 20 #include "avio.h" |
0 | 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, | |
65 | 30 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size), |
31 void (*write_packet)(void *opaque, uint8_t *buf, int buf_size), | |
0 | 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; | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
51 s->update_checksum= NULL; |
0 | 52 return 0; |
53 } | |
54 | |
55 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
56 #ifdef CONFIG_ENCODERS |
0 | 57 static void flush_buffer(ByteIOContext *s) |
58 { | |
59 if (s->buf_ptr > s->buffer) { | |
60 if (s->write_packet) | |
61 s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer); | |
421 | 62 if(s->update_checksum){ |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
63 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr); |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
64 s->checksum_ptr= s->buffer; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
65 } |
0 | 66 s->pos += s->buf_ptr - s->buffer; |
67 } | |
68 s->buf_ptr = s->buffer; | |
69 } | |
70 | |
71 void put_byte(ByteIOContext *s, int b) | |
72 { | |
73 *(s->buf_ptr)++ = b; | |
74 if (s->buf_ptr >= s->buf_end) | |
75 flush_buffer(s); | |
76 } | |
77 | |
78 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size) | |
79 { | |
80 int len; | |
81 | |
82 while (size > 0) { | |
83 len = (s->buf_end - s->buf_ptr); | |
84 if (len > size) | |
85 len = size; | |
86 memcpy(s->buf_ptr, buf, len); | |
87 s->buf_ptr += len; | |
88 | |
89 if (s->buf_ptr >= s->buf_end) | |
90 flush_buffer(s); | |
91 | |
92 buf += len; | |
93 size -= len; | |
94 } | |
95 } | |
96 | |
97 void put_flush_packet(ByteIOContext *s) | |
98 { | |
99 flush_buffer(s); | |
100 s->must_flush = 0; | |
101 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
102 #endif //CONFIG_ENCODERS |
0 | 103 |
104 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence) | |
105 { | |
106 offset_t offset1; | |
107 | |
108 if (whence != SEEK_CUR && whence != SEEK_SET) | |
109 return -EINVAL; | |
110 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
111 #ifdef CONFIG_ENCODERS |
0 | 112 if (s->write_flag) { |
113 if (whence == SEEK_CUR) { | |
114 offset1 = s->pos + (s->buf_ptr - s->buffer); | |
115 if (offset == 0) | |
116 return offset1; | |
117 offset += offset1; | |
118 } | |
119 offset1 = offset - s->pos; | |
120 if (!s->must_flush && | |
121 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) { | |
122 /* can do the seek inside the buffer */ | |
123 s->buf_ptr = s->buffer + offset1; | |
124 } else { | |
125 if (!s->seek) | |
126 return -EPIPE; | |
127 flush_buffer(s); | |
128 s->must_flush = 1; | |
129 s->buf_ptr = s->buffer; | |
130 s->seek(s->opaque, offset, SEEK_SET); | |
131 s->pos = offset; | |
132 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
133 } else |
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
134 #endif //CONFIG_ENCODERS |
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
135 { |
0 | 136 if (whence == SEEK_CUR) { |
137 offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer); | |
138 if (offset == 0) | |
139 return offset1; | |
140 offset += offset1; | |
141 } | |
142 offset1 = offset - (s->pos - (s->buf_end - s->buffer)); | |
143 if (offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) { | |
144 /* can do the seek inside the buffer */ | |
145 s->buf_ptr = s->buffer + offset1; | |
146 } else { | |
147 if (!s->seek) | |
148 return -EPIPE; | |
149 s->buf_ptr = s->buffer; | |
150 s->buf_end = s->buffer; | |
151 s->seek(s->opaque, offset, SEEK_SET); | |
152 s->pos = offset; | |
153 } | |
154 s->eof_reached = 0; | |
155 } | |
156 return offset; | |
157 } | |
158 | |
159 void url_fskip(ByteIOContext *s, offset_t offset) | |
160 { | |
161 url_fseek(s, offset, SEEK_CUR); | |
162 } | |
163 | |
164 offset_t url_ftell(ByteIOContext *s) | |
165 { | |
166 return url_fseek(s, 0, SEEK_CUR); | |
167 } | |
168 | |
169 int url_feof(ByteIOContext *s) | |
170 { | |
171 return s->eof_reached; | |
172 } | |
173 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
174 #ifdef CONFIG_ENCODERS |
0 | 175 void put_le32(ByteIOContext *s, unsigned int val) |
176 { | |
177 put_byte(s, val); | |
178 put_byte(s, val >> 8); | |
179 put_byte(s, val >> 16); | |
180 put_byte(s, val >> 24); | |
181 } | |
182 | |
183 void put_be32(ByteIOContext *s, unsigned int val) | |
184 { | |
185 put_byte(s, val >> 24); | |
186 put_byte(s, val >> 16); | |
187 put_byte(s, val >> 8); | |
188 put_byte(s, val); | |
189 } | |
190 | |
191 /* IEEE format is assumed */ | |
192 void put_be64_double(ByteIOContext *s, double val) | |
193 { | |
194 union { | |
195 double d; | |
65 | 196 uint64_t ull; |
0 | 197 } u; |
198 u.d = val; | |
199 put_be64(s, u.ull); | |
200 } | |
201 | |
202 void put_strz(ByteIOContext *s, const char *str) | |
203 { | |
204 if (str) | |
205 put_buffer(s, (const unsigned char *) str, strlen(str) + 1); | |
206 else | |
207 put_byte(s, 0); | |
208 } | |
209 | |
65 | 210 void put_le64(ByteIOContext *s, uint64_t val) |
0 | 211 { |
65 | 212 put_le32(s, (uint32_t)(val & 0xffffffff)); |
213 put_le32(s, (uint32_t)(val >> 32)); | |
0 | 214 } |
215 | |
65 | 216 void put_be64(ByteIOContext *s, uint64_t val) |
0 | 217 { |
65 | 218 put_be32(s, (uint32_t)(val >> 32)); |
219 put_be32(s, (uint32_t)(val & 0xffffffff)); | |
0 | 220 } |
221 | |
222 void put_le16(ByteIOContext *s, unsigned int val) | |
223 { | |
224 put_byte(s, val); | |
225 put_byte(s, val >> 8); | |
226 } | |
227 | |
228 void put_be16(ByteIOContext *s, unsigned int val) | |
229 { | |
230 put_byte(s, val >> 8); | |
231 put_byte(s, val); | |
232 } | |
233 | |
234 void put_tag(ByteIOContext *s, const char *tag) | |
235 { | |
236 while (*tag) { | |
237 put_byte(s, *tag++); | |
238 } | |
239 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
240 #endif //CONFIG_ENCODERS |
0 | 241 |
242 /* Input stream */ | |
243 | |
244 static void fill_buffer(ByteIOContext *s) | |
245 { | |
246 int len; | |
247 | |
248 /* no need to do anything if EOF already reached */ | |
249 if (s->eof_reached) | |
250 return; | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
251 |
421 | 252 if(s->update_checksum){ |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
253 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr); |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
254 s->checksum_ptr= s->buffer; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
255 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
256 |
0 | 257 len = s->read_packet(s->opaque, s->buffer, s->buffer_size); |
258 if (len <= 0) { | |
259 /* do not modify buffer if EOF reached so that a seek back can | |
260 be done without rereading data */ | |
261 s->eof_reached = 1; | |
262 } else { | |
263 s->pos += len; | |
264 s->buf_ptr = s->buffer; | |
265 s->buf_end = s->buffer + len; | |
266 } | |
267 } | |
268 | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
269 unsigned long get_checksum(ByteIOContext *s){ |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
270 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr); |
421 | 271 s->update_checksum= NULL; |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
272 return s->checksum; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
273 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
274 |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
275 void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum){ |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
276 s->update_checksum= update_checksum; |
421 | 277 if(s->update_checksum){ |
278 s->checksum= s->update_checksum(checksum, NULL, 0); | |
279 s->checksum_ptr= s->buf_ptr; | |
280 } | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
281 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
282 |
0 | 283 /* NOTE: return 0 if EOF, so you cannot use it if EOF handling is |
284 necessary */ | |
285 /* XXX: put an inline version */ | |
286 int get_byte(ByteIOContext *s) | |
287 { | |
288 if (s->buf_ptr < s->buf_end) { | |
289 return *s->buf_ptr++; | |
290 } else { | |
291 fill_buffer(s); | |
292 if (s->buf_ptr < s->buf_end) | |
293 return *s->buf_ptr++; | |
294 else | |
295 return 0; | |
296 } | |
297 } | |
298 | |
299 /* NOTE: return URL_EOF (-1) if EOF */ | |
300 int url_fgetc(ByteIOContext *s) | |
301 { | |
302 if (s->buf_ptr < s->buf_end) { | |
303 return *s->buf_ptr++; | |
304 } else { | |
305 fill_buffer(s); | |
306 if (s->buf_ptr < s->buf_end) | |
307 return *s->buf_ptr++; | |
308 else | |
309 return URL_EOF; | |
310 } | |
311 } | |
312 | |
313 int get_buffer(ByteIOContext *s, unsigned char *buf, int size) | |
314 { | |
315 int len, size1; | |
316 | |
317 size1 = size; | |
318 while (size > 0) { | |
319 len = s->buf_end - s->buf_ptr; | |
320 if (len > size) | |
321 len = size; | |
322 if (len == 0) { | |
323 fill_buffer(s); | |
324 len = s->buf_end - s->buf_ptr; | |
325 if (len == 0) | |
326 break; | |
327 } else { | |
328 memcpy(buf, s->buf_ptr, len); | |
329 buf += len; | |
330 s->buf_ptr += len; | |
331 size -= len; | |
332 } | |
333 } | |
334 return size1 - size; | |
335 } | |
336 | |
389
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
337 int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size) |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
338 { |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
339 int len; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
340 |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
341 len = s->buf_end - s->buf_ptr; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
342 if (len == 0) { |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
343 fill_buffer(s); |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
344 len = s->buf_end - s->buf_ptr; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
345 } |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
346 if (len > size) |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
347 len = size; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
348 memcpy(buf, s->buf_ptr, len); |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
349 s->buf_ptr += len; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
350 return len; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
351 } |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
352 |
0 | 353 unsigned int get_le16(ByteIOContext *s) |
354 { | |
355 unsigned int val; | |
356 val = get_byte(s); | |
357 val |= get_byte(s) << 8; | |
358 return val; | |
359 } | |
360 | |
361 unsigned int get_le32(ByteIOContext *s) | |
362 { | |
363 unsigned int val; | |
364 val = get_byte(s); | |
365 val |= get_byte(s) << 8; | |
366 val |= get_byte(s) << 16; | |
367 val |= get_byte(s) << 24; | |
368 return val; | |
369 } | |
370 | |
65 | 371 uint64_t get_le64(ByteIOContext *s) |
0 | 372 { |
65 | 373 uint64_t val; |
374 val = (uint64_t)get_le32(s); | |
375 val |= (uint64_t)get_le32(s) << 32; | |
0 | 376 return val; |
377 } | |
378 | |
379 unsigned int get_be16(ByteIOContext *s) | |
380 { | |
381 unsigned int val; | |
382 val = get_byte(s) << 8; | |
383 val |= get_byte(s); | |
384 return val; | |
385 } | |
386 | |
387 unsigned int get_be32(ByteIOContext *s) | |
388 { | |
389 unsigned int val; | |
390 val = get_byte(s) << 24; | |
391 val |= get_byte(s) << 16; | |
392 val |= get_byte(s) << 8; | |
393 val |= get_byte(s); | |
394 return val; | |
395 } | |
396 | |
397 double get_be64_double(ByteIOContext *s) | |
398 { | |
399 union { | |
400 double d; | |
65 | 401 uint64_t ull; |
0 | 402 } u; |
403 | |
404 u.ull = get_be64(s); | |
405 return u.d; | |
406 } | |
407 | |
408 char *get_strz(ByteIOContext *s, char *buf, int maxlen) | |
409 { | |
410 int i = 0; | |
411 char c; | |
412 | |
413 while ((c = get_byte(s))) { | |
414 if (i < maxlen-1) | |
415 buf[i++] = c; | |
416 } | |
417 | |
418 buf[i] = 0; /* Ensure null terminated, but may be truncated */ | |
419 | |
420 return buf; | |
421 } | |
422 | |
65 | 423 uint64_t get_be64(ByteIOContext *s) |
0 | 424 { |
65 | 425 uint64_t val; |
426 val = (uint64_t)get_be32(s) << 32; | |
427 val |= (uint64_t)get_be32(s); | |
0 | 428 return val; |
429 } | |
430 | |
431 /* link with avio functions */ | |
432 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
433 #ifdef CONFIG_ENCODERS |
65 | 434 static void url_write_packet(void *opaque, uint8_t *buf, int buf_size) |
0 | 435 { |
436 URLContext *h = opaque; | |
437 url_write(h, buf, buf_size); | |
438 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
439 #else |
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
440 #define url_write_packet NULL |
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
441 #endif //CONFIG_ENCODERS |
0 | 442 |
65 | 443 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size) |
0 | 444 { |
445 URLContext *h = opaque; | |
446 return url_read(h, buf, buf_size); | |
447 } | |
448 | |
65 | 449 static int url_seek_packet(void *opaque, int64_t offset, int whence) |
0 | 450 { |
451 URLContext *h = opaque; | |
452 url_seek(h, offset, whence); | |
453 return 0; | |
454 } | |
455 | |
456 int url_fdopen(ByteIOContext *s, URLContext *h) | |
457 { | |
65 | 458 uint8_t *buffer; |
0 | 459 int buffer_size, max_packet_size; |
460 | |
461 | |
462 max_packet_size = url_get_max_packet_size(h); | |
463 if (max_packet_size) { | |
464 buffer_size = max_packet_size; /* no need to bufferize more than one packet */ | |
465 } else { | |
466 buffer_size = IO_BUFFER_SIZE; | |
467 } | |
468 buffer = av_malloc(buffer_size); | |
469 if (!buffer) | |
470 return -ENOMEM; | |
471 | |
472 if (init_put_byte(s, buffer, buffer_size, | |
364
0d74e8abcb3d
avio patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
277
diff
changeset
|
473 (h->flags & URL_WRONLY || h->flags & URL_RDWR), h, |
0 | 474 url_read_packet, url_write_packet, url_seek_packet) < 0) { |
475 av_free(buffer); | |
476 return -EIO; | |
477 } | |
478 s->is_streamed = h->is_streamed; | |
479 s->max_packet_size = max_packet_size; | |
480 return 0; | |
481 } | |
482 | |
483 /* XXX: must be called before any I/O */ | |
484 int url_setbufsize(ByteIOContext *s, int buf_size) | |
485 { | |
65 | 486 uint8_t *buffer; |
0 | 487 buffer = av_malloc(buf_size); |
488 if (!buffer) | |
489 return -ENOMEM; | |
490 | |
491 av_free(s->buffer); | |
492 s->buffer = buffer; | |
493 s->buffer_size = buf_size; | |
494 s->buf_ptr = buffer; | |
495 if (!s->write_flag) | |
496 s->buf_end = buffer; | |
497 else | |
498 s->buf_end = buffer + buf_size; | |
499 return 0; | |
500 } | |
501 | |
502 /* NOTE: when opened as read/write, the buffers are only used for | |
503 reading */ | |
504 int url_fopen(ByteIOContext *s, const char *filename, int flags) | |
505 { | |
506 URLContext *h; | |
507 int err; | |
508 | |
509 err = url_open(&h, filename, flags); | |
510 if (err < 0) | |
511 return err; | |
512 err = url_fdopen(s, h); | |
513 if (err < 0) { | |
514 url_close(h); | |
515 return err; | |
516 } | |
517 return 0; | |
518 } | |
519 | |
520 int url_fclose(ByteIOContext *s) | |
521 { | |
522 URLContext *h = s->opaque; | |
523 | |
524 av_free(s->buffer); | |
525 memset(s, 0, sizeof(ByteIOContext)); | |
526 return url_close(h); | |
527 } | |
528 | |
529 URLContext *url_fileno(ByteIOContext *s) | |
530 { | |
531 return s->opaque; | |
532 } | |
533 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
534 #ifdef CONFIG_ENCODERS |
0 | 535 /* XXX: currently size is limited */ |
536 int url_fprintf(ByteIOContext *s, const char *fmt, ...) | |
537 { | |
538 va_list ap; | |
539 char buf[4096]; | |
540 int ret; | |
541 | |
542 va_start(ap, fmt); | |
543 ret = vsnprintf(buf, sizeof(buf), fmt, ap); | |
544 va_end(ap); | |
545 put_buffer(s, buf, strlen(buf)); | |
546 return ret; | |
547 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
548 #endif //CONFIG_ENCODERS |
0 | 549 |
550 /* note: unlike fgets, the EOL character is not returned and a whole | |
551 line is parsed. return NULL if first char read was EOF */ | |
552 char *url_fgets(ByteIOContext *s, char *buf, int buf_size) | |
553 { | |
554 int c; | |
555 char *q; | |
556 | |
557 c = url_fgetc(s); | |
558 if (c == EOF) | |
559 return NULL; | |
560 q = buf; | |
561 for(;;) { | |
562 if (c == EOF || c == '\n') | |
563 break; | |
564 if ((q - buf) < buf_size - 1) | |
565 *q++ = c; | |
566 c = url_fgetc(s); | |
567 } | |
568 if (buf_size > 0) | |
569 *q = '\0'; | |
570 return buf; | |
571 } | |
572 | |
573 /* | |
574 * Return the maximum packet size associated to packetized buffered file | |
575 * handle. If the file is not packetized (stream like http or file on | |
576 * disk), then 0 is returned. | |
577 * | |
578 * @param h buffered file handle | |
579 * @return maximum packet size in bytes | |
580 */ | |
581 int url_fget_max_packet_size(ByteIOContext *s) | |
582 { | |
583 return s->max_packet_size; | |
584 } | |
585 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
586 #ifdef CONFIG_ENCODERS |
0 | 587 /* buffer handling */ |
65 | 588 int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags) |
0 | 589 { |
590 return init_put_byte(s, buf, buf_size, | |
364
0d74e8abcb3d
avio patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
277
diff
changeset
|
591 (flags & URL_WRONLY || flags & URL_RDWR), |
0d74e8abcb3d
avio patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
277
diff
changeset
|
592 NULL, NULL, NULL, NULL); |
0 | 593 } |
594 | |
595 /* return the written or read size */ | |
596 int url_close_buf(ByteIOContext *s) | |
597 { | |
598 put_flush_packet(s); | |
599 return s->buf_ptr - s->buffer; | |
600 } | |
601 | |
602 /* output in a dynamic buffer */ | |
603 | |
604 typedef struct DynBuffer { | |
605 int pos, size, allocated_size; | |
65 | 606 uint8_t *buffer; |
0 | 607 int io_buffer_size; |
65 | 608 uint8_t io_buffer[1]; |
0 | 609 } DynBuffer; |
610 | |
65 | 611 static void dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) |
0 | 612 { |
613 DynBuffer *d = opaque; | |
614 int new_size, new_allocated_size; | |
615 | |
616 /* reallocate buffer if needed */ | |
617 new_size = d->pos + buf_size; | |
618 new_allocated_size = d->allocated_size; | |
619 while (new_size > new_allocated_size) { | |
620 if (!new_allocated_size) | |
621 new_allocated_size = new_size; | |
622 else | |
623 new_allocated_size = (new_allocated_size * 3) / 2 + 1; | |
624 } | |
625 | |
626 if (new_allocated_size > d->allocated_size) { | |
93
69ed49c151bf
ffserver deallocate ctx->streams on closing patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
65
diff
changeset
|
627 d->buffer = av_realloc(d->buffer, new_allocated_size); |
69ed49c151bf
ffserver deallocate ctx->streams on closing patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
65
diff
changeset
|
628 if(d->buffer == NULL) |
69ed49c151bf
ffserver deallocate ctx->streams on closing patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
65
diff
changeset
|
629 return ; |
0 | 630 d->allocated_size = new_allocated_size; |
631 } | |
632 memcpy(d->buffer + d->pos, buf, buf_size); | |
633 d->pos = new_size; | |
634 if (d->pos > d->size) | |
635 d->size = d->pos; | |
636 } | |
637 | |
65 | 638 static void dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size) |
0 | 639 { |
640 unsigned char buf1[4]; | |
641 | |
642 /* packetized write: output the header */ | |
643 buf1[0] = (buf_size >> 24); | |
644 buf1[1] = (buf_size >> 16); | |
645 buf1[2] = (buf_size >> 8); | |
646 buf1[3] = (buf_size); | |
647 dyn_buf_write(opaque, buf1, 4); | |
648 | |
649 /* then the data */ | |
650 dyn_buf_write(opaque, buf, buf_size); | |
651 } | |
652 | |
653 static int dyn_buf_seek(void *opaque, offset_t offset, int whence) | |
654 { | |
655 DynBuffer *d = opaque; | |
656 | |
657 if (whence == SEEK_CUR) | |
658 offset += d->pos; | |
659 else if (whence == SEEK_END) | |
660 offset += d->size; | |
661 if (offset < 0 || offset > 0x7fffffffLL) | |
662 return -1; | |
663 d->pos = offset; | |
664 return 0; | |
665 } | |
666 | |
667 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size) | |
668 { | |
669 DynBuffer *d; | |
670 int io_buffer_size, ret; | |
671 | |
672 if (max_packet_size) | |
673 io_buffer_size = max_packet_size; | |
674 else | |
675 io_buffer_size = 1024; | |
676 | |
677 d = av_malloc(sizeof(DynBuffer) + io_buffer_size); | |
678 if (!d) | |
679 return -1; | |
680 d->io_buffer_size = io_buffer_size; | |
681 d->buffer = NULL; | |
682 d->pos = 0; | |
683 d->size = 0; | |
684 d->allocated_size = 0; | |
685 ret = init_put_byte(s, d->io_buffer, io_buffer_size, | |
686 1, d, NULL, | |
687 max_packet_size ? dyn_packet_buf_write : dyn_buf_write, | |
688 max_packet_size ? NULL : dyn_buf_seek); | |
689 if (ret == 0) { | |
690 s->max_packet_size = max_packet_size; | |
691 } | |
692 return ret; | |
693 } | |
694 | |
695 /* | |
696 * Open a write only memory stream. | |
697 * | |
698 * @param s new IO context | |
699 * @return zero if no error. | |
700 */ | |
701 int url_open_dyn_buf(ByteIOContext *s) | |
702 { | |
703 return url_open_dyn_buf_internal(s, 0); | |
704 } | |
705 | |
706 /* | |
707 * Open a write only packetized memory stream with a maximum packet | |
708 * size of 'max_packet_size'. The stream is stored in a memory buffer | |
709 * with a big endian 4 byte header giving the packet size in bytes. | |
710 * | |
711 * @param s new IO context | |
712 * @param max_packet_size maximum packet size (must be > 0) | |
713 * @return zero if no error. | |
714 */ | |
715 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size) | |
716 { | |
717 if (max_packet_size <= 0) | |
718 return -1; | |
719 return url_open_dyn_buf_internal(s, max_packet_size); | |
720 } | |
721 | |
722 /* | |
723 * Return the written size and a pointer to the buffer. The buffer | |
724 * must be freed with av_free(). | |
725 * @param s IO context | |
726 * @param pointer to a byte buffer | |
727 * @return the length of the byte buffer | |
728 */ | |
65 | 729 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer) |
0 | 730 { |
731 DynBuffer *d = s->opaque; | |
732 int size; | |
733 | |
734 put_flush_packet(s); | |
735 | |
736 *pbuffer = d->buffer; | |
737 size = d->size; | |
738 av_free(d); | |
739 return size; | |
740 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
741 #endif //CONFIG_ENCODERS |