2 * Buffered I/O for ffmpeg system
3 * Copyright (c) 2000,2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/crc.h"
27 #define IO_BUFFER_SIZE 32768
29 static void fill_buffer(ByteIOContext
*s
);
31 int init_put_byte(ByteIOContext
*s
,
32 unsigned char *buffer
,
36 int (*read_packet
)(void *opaque
, uint8_t *buf
, int buf_size
),
37 int (*write_packet
)(void *opaque
, uint8_t *buf
, int buf_size
),
38 int64_t (*seek
)(void *opaque
, int64_t offset
, int whence
))
41 s
->buffer_size
= buffer_size
;
44 url_resetbuf(s
, write_flag ? URL_WRONLY
: URL_RDONLY
);
45 s
->write_packet
= write_packet
;
46 s
->read_packet
= read_packet
;
53 s
->max_packet_size
= 0;
54 s
->update_checksum
= NULL
;
55 if(!read_packet
&& !write_flag
){
57 s
->buf_end
= s
->buffer
+ buffer_size
;
64 ByteIOContext
*av_alloc_put_byte(
65 unsigned char *buffer
,
69 int (*read_packet
)(void *opaque
, uint8_t *buf
, int buf_size
),
70 int (*write_packet
)(void *opaque
, uint8_t *buf
, int buf_size
),
71 int64_t (*seek
)(void *opaque
, int64_t offset
, int whence
))
73 ByteIOContext
*s
= av_mallocz(sizeof(ByteIOContext
));
74 init_put_byte(s
, buffer
, buffer_size
, write_flag
, opaque
,
75 read_packet
, write_packet
, seek
);
79 static void flush_buffer(ByteIOContext
*s
)
81 if (s
->buf_ptr
> s
->buffer
) {
82 if (s
->write_packet
&& !s
->error
){
83 int ret
= s
->write_packet(s
->opaque
, s
->buffer
, s
->buf_ptr
- s
->buffer
);
88 if(s
->update_checksum
){
89 s
->checksum
= s
->update_checksum(s
->checksum
, s
->checksum_ptr
, s
->buf_ptr
- s
->checksum_ptr
);
90 s
->checksum_ptr
= s
->buffer
;
92 s
->pos
+= s
->buf_ptr
- s
->buffer
;
94 s
->buf_ptr
= s
->buffer
;
97 void put_byte(ByteIOContext
*s
, int b
)
100 if (s
->buf_ptr
>= s
->buf_end
)
104 void put_buffer(ByteIOContext
*s
, const unsigned char *buf
, int size
)
109 len
= (s
->buf_end
- s
->buf_ptr
);
112 memcpy(s
->buf_ptr
, buf
, len
);
115 if (s
->buf_ptr
>= s
->buf_end
)
123 void put_flush_packet(ByteIOContext
*s
)
129 int64_t url_fseek(ByteIOContext
*s
, int64_t offset
, int whence
)
135 return AVERROR(EINVAL
);
137 pos
= s
->pos
- (s
->write_flag ?
0 : (s
->buf_end
- s
->buffer
));
139 if (whence
!= SEEK_CUR
&& whence
!= SEEK_SET
)
140 return AVERROR(EINVAL
);
142 if (whence
== SEEK_CUR
) {
143 offset1
= pos
+ (s
->buf_ptr
- s
->buffer
);
148 offset1
= offset
- pos
;
149 if (!s
->must_flush
&&
150 offset1
>= 0 && offset1
< (s
->buf_end
- s
->buffer
)) {
151 /* can do the seek inside the buffer */
152 s
->buf_ptr
= s
->buffer
+ offset1
;
153 } else if(s
->is_streamed
&& !s
->write_flag
&&
154 offset1
>= 0 && offset1
< (s
->buf_end
- s
->buffer
) + (1<<16)){
155 while(s
->pos
< offset
&& !s
->eof_reached
)
158 return AVERROR(EPIPE
);
159 s
->buf_ptr
= s
->buf_end
+ offset
- s
->pos
;
161 int64_t res
= AVERROR(EPIPE
);
163 #if CONFIG_MUXERS || CONFIG_NETWORK
168 #endif /* CONFIG_MUXERS || CONFIG_NETWORK */
169 if (!s
->seek
|| (res
= s
->seek(s
->opaque
, offset
, SEEK_SET
)) < 0)
172 s
->buf_end
= s
->buffer
;
173 s
->buf_ptr
= s
->buffer
;
180 void url_fskip(ByteIOContext
*s
, int64_t offset
)
182 url_fseek(s
, offset
, SEEK_CUR
);
185 int64_t url_ftell(ByteIOContext
*s
)
187 return url_fseek(s
, 0, SEEK_CUR
);
190 int64_t url_fsize(ByteIOContext
*s
)
195 return AVERROR(EINVAL
);
198 return AVERROR(EPIPE
);
199 size
= s
->seek(s
->opaque
, 0, AVSEEK_SIZE
);
201 if ((size
= s
->seek(s
->opaque
, -1, SEEK_END
)) < 0)
204 s
->seek(s
->opaque
, s
->pos
, SEEK_SET
);
209 int url_feof(ByteIOContext
*s
)
213 return s
->eof_reached
;
216 int url_ferror(ByteIOContext
*s
)
223 void put_le32(ByteIOContext
*s
, unsigned int val
)
226 put_byte(s
, val
>> 8);
227 put_byte(s
, val
>> 16);
228 put_byte(s
, val
>> 24);
231 void put_be32(ByteIOContext
*s
, unsigned int val
)
233 put_byte(s
, val
>> 24);
234 put_byte(s
, val
>> 16);
235 put_byte(s
, val
>> 8);
239 void put_strz(ByteIOContext
*s
, const char *str
)
242 put_buffer(s
, (const unsigned char *) str
, strlen(str
) + 1);
247 void put_le64(ByteIOContext
*s
, uint64_t val
)
249 put_le32(s
, (uint32_t)(val
& 0xffffffff));
250 put_le32(s
, (uint32_t)(val
>> 32));
253 void put_be64(ByteIOContext
*s
, uint64_t val
)
255 put_be32(s
, (uint32_t)(val
>> 32));
256 put_be32(s
, (uint32_t)(val
& 0xffffffff));
259 void put_le16(ByteIOContext
*s
, unsigned int val
)
262 put_byte(s
, val
>> 8);
265 void put_be16(ByteIOContext
*s
, unsigned int val
)
267 put_byte(s
, val
>> 8);
271 void put_le24(ByteIOContext
*s
, unsigned int val
)
273 put_le16(s
, val
& 0xffff);
274 put_byte(s
, val
>> 16);
277 void put_be24(ByteIOContext
*s
, unsigned int val
)
279 put_be16(s
, val
>> 8);
283 void put_tag(ByteIOContext
*s
, const char *tag
)
292 static void fill_buffer(ByteIOContext
*s
)
294 uint8_t *dst
= !s
->max_packet_size
&& s
->buf_end
- s
->buffer
< s
->buffer_size ? s
->buf_ptr
: s
->buffer
;
295 int len
= s
->buffer_size
- (dst
- s
->buffer
);
297 assert(s
->buf_ptr
== s
->buf_end
);
299 /* no need to do anything if EOF already reached */
303 if(s
->update_checksum
&& dst
== s
->buffer
){
304 if(s
->buf_end
> s
->checksum_ptr
)
305 s
->checksum
= s
->update_checksum(s
->checksum
, s
->checksum_ptr
, s
->buf_end
- s
->checksum_ptr
);
306 s
->checksum_ptr
= s
->buffer
;
310 len
= s
->read_packet(s
->opaque
, dst
, len
);
314 /* do not modify buffer if EOF reached so that a seek back can
315 be done without rereading data */
322 s
->buf_end
= dst
+ len
;
326 unsigned long ff_crc04C11DB7_update(unsigned long checksum
, const uint8_t *buf
,
329 return av_crc(av_crc_get_table(AV_CRC_32_IEEE
), checksum
, buf
, len
);
332 unsigned long get_checksum(ByteIOContext
*s
)
334 s
->checksum
= s
->update_checksum(s
->checksum
, s
->checksum_ptr
, s
->buf_ptr
- s
->checksum_ptr
);
335 s
->update_checksum
= NULL
;
339 void init_checksum(ByteIOContext
*s
,
340 unsigned long (*update_checksum
)(unsigned long c
, const uint8_t *p
, unsigned int len
),
341 unsigned long checksum
)
343 s
->update_checksum
= update_checksum
;
344 if(s
->update_checksum
){
345 s
->checksum
= checksum
;
346 s
->checksum_ptr
= s
->buf_ptr
;
350 /* XXX: put an inline version */
351 int get_byte(ByteIOContext
*s
)
353 if (s
->buf_ptr
< s
->buf_end
) {
354 return *s
->buf_ptr
++;
357 if (s
->buf_ptr
< s
->buf_end
)
358 return *s
->buf_ptr
++;
364 int url_fgetc(ByteIOContext
*s
)
366 if (s
->buf_ptr
< s
->buf_end
) {
367 return *s
->buf_ptr
++;
370 if (s
->buf_ptr
< s
->buf_end
)
371 return *s
->buf_ptr
++;
377 int get_buffer(ByteIOContext
*s
, unsigned char *buf
, int size
)
383 len
= s
->buf_end
- s
->buf_ptr
;
387 if(size
> s
->buffer_size
&& !s
->update_checksum
){
389 len
= s
->read_packet(s
->opaque
, buf
, size
);
391 /* do not modify buffer if EOF reached so that a seek back can
392 be done without rereading data */
401 s
->buf_ptr
= s
->buffer
;
402 s
->buf_end
= s
->buffer
/* + len*/;
406 len
= s
->buf_end
- s
->buf_ptr
;
411 memcpy(buf
, s
->buf_ptr
, len
);
420 int get_partial_buffer(ByteIOContext
*s
, unsigned char *buf
, int size
)
427 len
= s
->buf_end
- s
->buf_ptr
;
430 len
= s
->buf_end
- s
->buf_ptr
;
434 memcpy(buf
, s
->buf_ptr
, len
);
439 unsigned int get_le16(ByteIOContext
*s
)
443 val
|= get_byte(s
) << 8;
447 unsigned int get_le24(ByteIOContext
*s
)
451 val
|= get_byte(s
) << 16;
455 unsigned int get_le32(ByteIOContext
*s
)
459 val
|= get_le16(s
) << 16;
463 uint64_t get_le64(ByteIOContext
*s
)
466 val
= (uint64_t)get_le32(s
);
467 val
|= (uint64_t)get_le32(s
) << 32;
471 unsigned int get_be16(ByteIOContext
*s
)
474 val
= get_byte(s
) << 8;
479 unsigned int get_be24(ByteIOContext
*s
)
482 val
= get_be16(s
) << 8;
486 unsigned int get_be32(ByteIOContext
*s
)
489 val
= get_be16(s
) << 16;
494 char *get_strz(ByteIOContext
*s
, char *buf
, int maxlen
)
499 while ((c
= get_byte(s
))) {
504 buf
[i
] = 0; /* Ensure null terminated, but may be truncated */
509 uint64_t get_be64(ByteIOContext
*s
)
512 val
= (uint64_t)get_be32(s
) << 32;
513 val
|= (uint64_t)get_be32(s
);
517 uint64_t ff_get_v(ByteIOContext
*bc
){
523 val
= (val
<<7) + (tmp
&127);
528 int url_fdopen(ByteIOContext
**s
, URLContext
*h
)
531 int buffer_size
, max_packet_size
;
534 max_packet_size
= url_get_max_packet_size(h
);
535 if (max_packet_size
) {
536 buffer_size
= max_packet_size
; /* no need to bufferize more than one packet */
538 buffer_size
= IO_BUFFER_SIZE
;
540 buffer
= av_malloc(buffer_size
);
542 return AVERROR(ENOMEM
);
544 *s
= av_mallocz(sizeof(ByteIOContext
));
547 return AVERROR(ENOMEM
);
550 if (init_put_byte(*s
, buffer
, buffer_size
,
551 (h
->flags
& URL_WRONLY
|| h
->flags
& URL_RDWR
), h
,
552 url_read
, url_write
, url_seek
) < 0) {
557 (*s
)->is_streamed
= h
->is_streamed
;
558 (*s
)->max_packet_size
= max_packet_size
;
560 (*s
)->read_pause
= (int (*)(void *, int))h
->prot
->url_read_pause
;
561 (*s
)->read_seek
= (int64_t (*)(void *, int, int64_t, int))h
->prot
->url_read_seek
;
566 int url_setbufsize(ByteIOContext
*s
, int buf_size
)
569 buffer
= av_malloc(buf_size
);
571 return AVERROR(ENOMEM
);
575 s
->buffer_size
= buf_size
;
577 url_resetbuf(s
, s
->write_flag ? URL_WRONLY
: URL_RDONLY
);
581 int url_resetbuf(ByteIOContext
*s
, int flags
)
583 URLContext
*h
= s
->opaque
;
584 if ((flags
& URL_RDWR
) || (h
&& h
->flags
!= flags
&& !h
->flags
& URL_RDWR
))
585 return AVERROR(EINVAL
);
587 if (flags
& URL_WRONLY
) {
588 s
->buf_end
= s
->buffer
+ s
->buffer_size
;
591 s
->buf_end
= s
->buffer
;
597 int url_fopen(ByteIOContext
**s
, const char *filename
, int flags
)
602 err
= url_open(&h
, filename
, flags
);
605 err
= url_fdopen(s
, h
);
613 int url_fclose(ByteIOContext
*s
)
615 URLContext
*h
= s
->opaque
;
622 URLContext
*url_fileno(ByteIOContext
*s
)
628 int url_fprintf(ByteIOContext
*s
, const char *fmt
, ...)
635 ret
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
637 put_buffer(s
, buf
, strlen(buf
));
640 #endif //CONFIG_MUXERS
642 char *url_fgets(ByteIOContext
*s
, char *buf
, int buf_size
)
652 if (c
== EOF
|| c
== '\n')
654 if ((q
- buf
) < buf_size
- 1)
663 int url_fget_max_packet_size(ByteIOContext
*s
)
665 return s
->max_packet_size
;
668 int av_url_read_fpause(ByteIOContext
*s
, int pause
)
671 return AVERROR(ENOSYS
);
672 return s
->read_pause(s
->opaque
, pause
);
675 int64_t av_url_read_fseek(ByteIOContext
*s
, int stream_index
,
676 int64_t timestamp
, int flags
)
678 URLContext
*h
= s
->opaque
;
681 return AVERROR(ENOSYS
);
682 ret
= s
->read_seek(h
, stream_index
, timestamp
, flags
);
684 s
->buf_ptr
= s
->buf_end
; // Flush buffer
685 s
->pos
= s
->seek(h
, 0, SEEK_CUR
);
690 /* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
691 * back to the server even if CONFIG_MUXERS is false. */
692 #if CONFIG_MUXERS || CONFIG_NETWORK
693 /* buffer handling */
694 int url_open_buf(ByteIOContext
**s
, uint8_t *buf
, int buf_size
, int flags
)
697 *s
= av_mallocz(sizeof(ByteIOContext
));
699 return AVERROR(ENOMEM
);
700 ret
= init_put_byte(*s
, buf
, buf_size
,
701 (flags
& URL_WRONLY
|| flags
& URL_RDWR
),
702 NULL
, NULL
, NULL
, NULL
);
708 int url_close_buf(ByteIOContext
*s
)
711 return s
->buf_ptr
- s
->buffer
;
714 /* output in a dynamic buffer */
716 typedef struct DynBuffer
{
717 int pos
, size
, allocated_size
;
720 uint8_t io_buffer
[1];
723 static int dyn_buf_write(void *opaque
, uint8_t *buf
, int buf_size
)
725 DynBuffer
*d
= opaque
;
726 unsigned new_size
, new_allocated_size
;
728 /* reallocate buffer if needed */
729 new_size
= d
->pos
+ buf_size
;
730 new_allocated_size
= d
->allocated_size
;
731 if(new_size
< d
->pos
|| new_size
> INT_MAX
/2)
733 while (new_size
> new_allocated_size
) {
734 if (!new_allocated_size
)
735 new_allocated_size
= new_size
;
737 new_allocated_size
+= new_allocated_size
/ 2 + 1;
740 if (new_allocated_size
> d
->allocated_size
) {
741 d
->buffer
= av_realloc(d
->buffer
, new_allocated_size
);
742 if(d
->buffer
== NULL
)
743 return AVERROR(ENOMEM
);
744 d
->allocated_size
= new_allocated_size
;
746 memcpy(d
->buffer
+ d
->pos
, buf
, buf_size
);
748 if (d
->pos
> d
->size
)
753 static int dyn_packet_buf_write(void *opaque
, uint8_t *buf
, int buf_size
)
755 unsigned char buf1
[4];
758 /* packetized write: output the header */
759 buf1
[0] = (buf_size
>> 24);
760 buf1
[1] = (buf_size
>> 16);
761 buf1
[2] = (buf_size
>> 8);
762 buf1
[3] = (buf_size
);
763 ret
= dyn_buf_write(opaque
, buf1
, 4);
768 return dyn_buf_write(opaque
, buf
, buf_size
);
771 static int64_t dyn_buf_seek(void *opaque
, int64_t offset
, int whence
)
773 DynBuffer
*d
= opaque
;
775 if (whence
== SEEK_CUR
)
777 else if (whence
== SEEK_END
)
779 if (offset
< 0 || offset
> 0x7fffffffLL
)
785 static int url_open_dyn_buf_internal(ByteIOContext
**s
, int max_packet_size
)
788 int io_buffer_size
, ret
;
791 io_buffer_size
= max_packet_size
;
793 io_buffer_size
= 1024;
795 if(sizeof(DynBuffer
) + io_buffer_size
< io_buffer_size
)
797 d
= av_malloc(sizeof(DynBuffer
) + io_buffer_size
);
800 *s
= av_mallocz(sizeof(ByteIOContext
));
803 return AVERROR(ENOMEM
);
805 d
->io_buffer_size
= io_buffer_size
;
809 d
->allocated_size
= 0;
810 ret
= init_put_byte(*s
, d
->io_buffer
, io_buffer_size
,
812 max_packet_size ? dyn_packet_buf_write
: dyn_buf_write
,
813 max_packet_size ? NULL
: dyn_buf_seek
);
815 (*s
)->max_packet_size
= max_packet_size
;
823 int url_open_dyn_buf(ByteIOContext
**s
)
825 return url_open_dyn_buf_internal(s
, 0);
828 int url_open_dyn_packet_buf(ByteIOContext
**s
, int max_packet_size
)
830 if (max_packet_size
<= 0)
832 return url_open_dyn_buf_internal(s
, max_packet_size
);
835 int url_close_dyn_buf(ByteIOContext
*s
, uint8_t **pbuffer
)
837 DynBuffer
*d
= s
->opaque
;
842 *pbuffer
= d
->buffer
;
848 #endif /* CONFIG_MUXERS || CONFIG_NETWORK */