Mercurial > libavformat.hg
annotate aviobuf.c @ 311:e07654b1049c libavformat
initial seek support
author | bellard |
---|---|
date | Mon, 10 Nov 2003 18:48:33 +0000 |
parents | a313e1080322 |
children | 0d74e8abcb3d |
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 | |
312 unsigned int get_le16(ByteIOContext *s) | |
313 { | |
314 unsigned int val; | |
315 val = get_byte(s); | |
316 val |= get_byte(s) << 8; | |
317 return val; | |
318 } | |
319 | |
320 unsigned int get_le32(ByteIOContext *s) | |
321 { | |
322 unsigned int val; | |
323 val = get_byte(s); | |
324 val |= get_byte(s) << 8; | |
325 val |= get_byte(s) << 16; | |
326 val |= get_byte(s) << 24; | |
327 return val; | |
328 } | |
329 | |
65 | 330 uint64_t get_le64(ByteIOContext *s) |
0 | 331 { |
65 | 332 uint64_t val; |
333 val = (uint64_t)get_le32(s); | |
334 val |= (uint64_t)get_le32(s) << 32; | |
0 | 335 return val; |
336 } | |
337 | |
338 unsigned int get_be16(ByteIOContext *s) | |
339 { | |
340 unsigned int val; | |
341 val = get_byte(s) << 8; | |
342 val |= get_byte(s); | |
343 return val; | |
344 } | |
345 | |
346 unsigned int get_be32(ByteIOContext *s) | |
347 { | |
348 unsigned int val; | |
349 val = get_byte(s) << 24; | |
350 val |= get_byte(s) << 16; | |
351 val |= get_byte(s) << 8; | |
352 val |= get_byte(s); | |
353 return val; | |
354 } | |
355 | |
356 double get_be64_double(ByteIOContext *s) | |
357 { | |
358 union { | |
359 double d; | |
65 | 360 uint64_t ull; |
0 | 361 } u; |
362 | |
363 u.ull = get_be64(s); | |
364 return u.d; | |
365 } | |
366 | |
367 char *get_strz(ByteIOContext *s, char *buf, int maxlen) | |
368 { | |
369 int i = 0; | |
370 char c; | |
371 | |
372 while ((c = get_byte(s))) { | |
373 if (i < maxlen-1) | |
374 buf[i++] = c; | |
375 } | |
376 | |
377 buf[i] = 0; /* Ensure null terminated, but may be truncated */ | |
378 | |
379 return buf; | |
380 } | |
381 | |
65 | 382 uint64_t get_be64(ByteIOContext *s) |
0 | 383 { |
65 | 384 uint64_t val; |
385 val = (uint64_t)get_be32(s) << 32; | |
386 val |= (uint64_t)get_be32(s); | |
0 | 387 return val; |
388 } | |
389 | |
390 /* link with avio functions */ | |
391 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
392 #ifdef CONFIG_ENCODERS |
65 | 393 static void url_write_packet(void *opaque, uint8_t *buf, int buf_size) |
0 | 394 { |
395 URLContext *h = opaque; | |
396 url_write(h, buf, buf_size); | |
397 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
398 #else |
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
399 #define url_write_packet NULL |
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
400 #endif //CONFIG_ENCODERS |
0 | 401 |
65 | 402 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size) |
0 | 403 { |
404 URLContext *h = opaque; | |
405 return url_read(h, buf, buf_size); | |
406 } | |
407 | |
65 | 408 static int url_seek_packet(void *opaque, int64_t offset, int whence) |
0 | 409 { |
410 URLContext *h = opaque; | |
411 url_seek(h, offset, whence); | |
412 return 0; | |
413 } | |
414 | |
415 int url_fdopen(ByteIOContext *s, URLContext *h) | |
416 { | |
65 | 417 uint8_t *buffer; |
0 | 418 int buffer_size, max_packet_size; |
419 | |
420 | |
421 max_packet_size = url_get_max_packet_size(h); | |
422 if (max_packet_size) { | |
423 buffer_size = max_packet_size; /* no need to bufferize more than one packet */ | |
424 } else { | |
425 buffer_size = IO_BUFFER_SIZE; | |
426 } | |
427 buffer = av_malloc(buffer_size); | |
428 if (!buffer) | |
429 return -ENOMEM; | |
430 | |
431 if (init_put_byte(s, buffer, buffer_size, | |
432 (h->flags & URL_WRONLY) != 0, h, | |
433 url_read_packet, url_write_packet, url_seek_packet) < 0) { | |
434 av_free(buffer); | |
435 return -EIO; | |
436 } | |
437 s->is_streamed = h->is_streamed; | |
438 s->max_packet_size = max_packet_size; | |
439 return 0; | |
440 } | |
441 | |
442 /* XXX: must be called before any I/O */ | |
443 int url_setbufsize(ByteIOContext *s, int buf_size) | |
444 { | |
65 | 445 uint8_t *buffer; |
0 | 446 buffer = av_malloc(buf_size); |
447 if (!buffer) | |
448 return -ENOMEM; | |
449 | |
450 av_free(s->buffer); | |
451 s->buffer = buffer; | |
452 s->buffer_size = buf_size; | |
453 s->buf_ptr = buffer; | |
454 if (!s->write_flag) | |
455 s->buf_end = buffer; | |
456 else | |
457 s->buf_end = buffer + buf_size; | |
458 return 0; | |
459 } | |
460 | |
461 /* NOTE: when opened as read/write, the buffers are only used for | |
462 reading */ | |
463 int url_fopen(ByteIOContext *s, const char *filename, int flags) | |
464 { | |
465 URLContext *h; | |
466 int err; | |
467 | |
468 err = url_open(&h, filename, flags); | |
469 if (err < 0) | |
470 return err; | |
471 err = url_fdopen(s, h); | |
472 if (err < 0) { | |
473 url_close(h); | |
474 return err; | |
475 } | |
476 return 0; | |
477 } | |
478 | |
479 int url_fclose(ByteIOContext *s) | |
480 { | |
481 URLContext *h = s->opaque; | |
482 | |
483 av_free(s->buffer); | |
484 memset(s, 0, sizeof(ByteIOContext)); | |
485 return url_close(h); | |
486 } | |
487 | |
488 URLContext *url_fileno(ByteIOContext *s) | |
489 { | |
490 return s->opaque; | |
491 } | |
492 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
493 #ifdef CONFIG_ENCODERS |
0 | 494 /* XXX: currently size is limited */ |
495 int url_fprintf(ByteIOContext *s, const char *fmt, ...) | |
496 { | |
497 va_list ap; | |
498 char buf[4096]; | |
499 int ret; | |
500 | |
501 va_start(ap, fmt); | |
502 ret = vsnprintf(buf, sizeof(buf), fmt, ap); | |
503 va_end(ap); | |
504 put_buffer(s, buf, strlen(buf)); | |
505 return ret; | |
506 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
507 #endif //CONFIG_ENCODERS |
0 | 508 |
509 /* note: unlike fgets, the EOL character is not returned and a whole | |
510 line is parsed. return NULL if first char read was EOF */ | |
511 char *url_fgets(ByteIOContext *s, char *buf, int buf_size) | |
512 { | |
513 int c; | |
514 char *q; | |
515 | |
516 c = url_fgetc(s); | |
517 if (c == EOF) | |
518 return NULL; | |
519 q = buf; | |
520 for(;;) { | |
521 if (c == EOF || c == '\n') | |
522 break; | |
523 if ((q - buf) < buf_size - 1) | |
524 *q++ = c; | |
525 c = url_fgetc(s); | |
526 } | |
527 if (buf_size > 0) | |
528 *q = '\0'; | |
529 return buf; | |
530 } | |
531 | |
532 /* | |
533 * Return the maximum packet size associated to packetized buffered file | |
534 * handle. If the file is not packetized (stream like http or file on | |
535 * disk), then 0 is returned. | |
536 * | |
537 * @param h buffered file handle | |
538 * @return maximum packet size in bytes | |
539 */ | |
540 int url_fget_max_packet_size(ByteIOContext *s) | |
541 { | |
542 return s->max_packet_size; | |
543 } | |
544 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
545 #ifdef CONFIG_ENCODERS |
0 | 546 /* buffer handling */ |
65 | 547 int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags) |
0 | 548 { |
549 return init_put_byte(s, buf, buf_size, | |
550 (flags & URL_WRONLY) != 0, NULL, NULL, NULL, NULL); | |
551 } | |
552 | |
553 /* return the written or read size */ | |
554 int url_close_buf(ByteIOContext *s) | |
555 { | |
556 put_flush_packet(s); | |
557 return s->buf_ptr - s->buffer; | |
558 } | |
559 | |
560 /* output in a dynamic buffer */ | |
561 | |
562 typedef struct DynBuffer { | |
563 int pos, size, allocated_size; | |
65 | 564 uint8_t *buffer; |
0 | 565 int io_buffer_size; |
65 | 566 uint8_t io_buffer[1]; |
0 | 567 } DynBuffer; |
568 | |
65 | 569 static void dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) |
0 | 570 { |
571 DynBuffer *d = opaque; | |
572 int new_size, new_allocated_size; | |
573 | |
574 /* reallocate buffer if needed */ | |
575 new_size = d->pos + buf_size; | |
576 new_allocated_size = d->allocated_size; | |
577 while (new_size > new_allocated_size) { | |
578 if (!new_allocated_size) | |
579 new_allocated_size = new_size; | |
580 else | |
581 new_allocated_size = (new_allocated_size * 3) / 2 + 1; | |
582 } | |
583 | |
584 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
|
585 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
|
586 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
|
587 return ; |
0 | 588 d->allocated_size = new_allocated_size; |
589 } | |
590 memcpy(d->buffer + d->pos, buf, buf_size); | |
591 d->pos = new_size; | |
592 if (d->pos > d->size) | |
593 d->size = d->pos; | |
594 } | |
595 | |
65 | 596 static void dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size) |
0 | 597 { |
598 unsigned char buf1[4]; | |
599 | |
600 /* packetized write: output the header */ | |
601 buf1[0] = (buf_size >> 24); | |
602 buf1[1] = (buf_size >> 16); | |
603 buf1[2] = (buf_size >> 8); | |
604 buf1[3] = (buf_size); | |
605 dyn_buf_write(opaque, buf1, 4); | |
606 | |
607 /* then the data */ | |
608 dyn_buf_write(opaque, buf, buf_size); | |
609 } | |
610 | |
611 static int dyn_buf_seek(void *opaque, offset_t offset, int whence) | |
612 { | |
613 DynBuffer *d = opaque; | |
614 | |
615 if (whence == SEEK_CUR) | |
616 offset += d->pos; | |
617 else if (whence == SEEK_END) | |
618 offset += d->size; | |
619 if (offset < 0 || offset > 0x7fffffffLL) | |
620 return -1; | |
621 d->pos = offset; | |
622 return 0; | |
623 } | |
624 | |
625 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size) | |
626 { | |
627 DynBuffer *d; | |
628 int io_buffer_size, ret; | |
629 | |
630 if (max_packet_size) | |
631 io_buffer_size = max_packet_size; | |
632 else | |
633 io_buffer_size = 1024; | |
634 | |
635 d = av_malloc(sizeof(DynBuffer) + io_buffer_size); | |
636 if (!d) | |
637 return -1; | |
638 d->io_buffer_size = io_buffer_size; | |
639 d->buffer = NULL; | |
640 d->pos = 0; | |
641 d->size = 0; | |
642 d->allocated_size = 0; | |
643 ret = init_put_byte(s, d->io_buffer, io_buffer_size, | |
644 1, d, NULL, | |
645 max_packet_size ? dyn_packet_buf_write : dyn_buf_write, | |
646 max_packet_size ? NULL : dyn_buf_seek); | |
647 if (ret == 0) { | |
648 s->max_packet_size = max_packet_size; | |
649 } | |
650 return ret; | |
651 } | |
652 | |
653 /* | |
654 * Open a write only memory stream. | |
655 * | |
656 * @param s new IO context | |
657 * @return zero if no error. | |
658 */ | |
659 int url_open_dyn_buf(ByteIOContext *s) | |
660 { | |
661 return url_open_dyn_buf_internal(s, 0); | |
662 } | |
663 | |
664 /* | |
665 * Open a write only packetized memory stream with a maximum packet | |
666 * size of 'max_packet_size'. The stream is stored in a memory buffer | |
667 * with a big endian 4 byte header giving the packet size in bytes. | |
668 * | |
669 * @param s new IO context | |
670 * @param max_packet_size maximum packet size (must be > 0) | |
671 * @return zero if no error. | |
672 */ | |
673 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size) | |
674 { | |
675 if (max_packet_size <= 0) | |
676 return -1; | |
677 return url_open_dyn_buf_internal(s, max_packet_size); | |
678 } | |
679 | |
680 /* | |
681 * Return the written size and a pointer to the buffer. The buffer | |
682 * must be freed with av_free(). | |
683 * @param s IO context | |
684 * @param pointer to a byte buffer | |
685 * @return the length of the byte buffer | |
686 */ | |
65 | 687 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer) |
0 | 688 { |
689 DynBuffer *d = s->opaque; | |
690 int size; | |
691 | |
692 put_flush_packet(s); | |
693 | |
694 *pbuffer = d->buffer; | |
695 size = d->size; | |
696 av_free(d); | |
697 return size; | |
698 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
699 #endif //CONFIG_ENCODERS |