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