22 #include "libavutil/crc.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/avassert.h"
35 #define IO_BUFFER_SIZE 32768
42 #define SHORT_SEEK_THRESHOLD 4096
47 return prev ? NULL : s->
opaque;
78 int64_t (*seek)(
void *opaque, int64_t
offset,
int whence))
100 s->
pos = buffer_size;
110 unsigned char *buffer,
116 int64_t (*seek)(
void *opaque, int64_t
offset,
int whence))
204 whence &= ~AVSEEK_FORCE;
211 if (whence != SEEK_CUR && whence != SEEK_SET)
214 if (whence == SEEK_CUR) {
220 offset1 = offset - pos;
229 (whence != SEEK_END || force)) {
244 if ((res = s->
seek(s->
opaque, offset, SEEK_SET)) < 0)
272 if ((size = s->
seek(s->
opaque, -1, SEEK_END)) < 0)
312 avio_write(s, (
const unsigned char *) str, len);
357 avio_wl32(s, (uint32_t)(val & 0xffffffff));
364 avio_wb32(s, (uint32_t)(val & 0xffffffff));
424 len = max_buffer_size;
460 unsigned long (*update_checksum)(
unsigned long c,
const uint8_t *p,
unsigned int len),
461 unsigned long checksum)
639 if (c && i < maxlen-1)
641 }
while (c !=
'\n' && c);
654 buflen =
FFMIN(buflen - 1, maxlen);
655 for (i = 0; i < buflen; i++)
659 for (; i < maxlen; i++)
665 #define GET_STR16(type, read) \
666 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
671 return AVERROR(EINVAL); \
672 while (ret + 1 < maxlen) {\
675 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
678 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
703 val= (val<<7) + (tmp&127);
711 int buffer_size, max_packet_size;
714 if (max_packet_size) {
715 buffer_size = max_packet_size;
731 (*s)->max_packet_size = max_packet_size;
734 (*s)->read_seek = (int64_t (*)(
void *, int, int64_t, int))h->
prot->
url_read_seek;
748 if (buf_size < s->buffer_size || s->
seekable)
796 int64_t buffer_start;
798 int overlap, new_size, alloc_size;
809 if ((buffer_start = s->
pos - buffer_size) > buf_size) {
814 overlap = buf_size - buffer_start;
815 new_size = buf_size + buffer_size - overlap;
818 if (alloc_size > buf_size)
819 if (!(buf = (*bufp) =
av_realloc_f(buf, 1, alloc_size)))
822 if (new_size > buf_size) {
823 memcpy(buf + buf_size, s->
buffer + overlap, buffer_size - overlap);
840 return avio_open2(s, filename, flags, NULL, NULL);
849 err =
ffurl_open(&h, filename, flags, int_cb, options);
892 ret =
vsnprintf(buf,
sizeof(buf), fmt, ap);
906 int64_t timestamp,
int flags)
912 ret = s->
read_seek(h, stream_index, timestamp, flags);
916 pos = s->
seek(h, 0, SEEK_CUR);
919 else if (pos !=
AVERROR(ENOSYS))
937 unsigned new_size, new_allocated_size;
940 new_size = d->
pos + buf_size;
942 if (new_size < d->pos || new_size > INT_MAX/2)
944 while (new_size > new_allocated_size) {
945 if (!new_allocated_size)
946 new_allocated_size = new_size;
948 new_allocated_size += new_allocated_size / 2 + 1;
960 memcpy(d->
buffer + d->
pos, buf, buf_size);
969 unsigned char buf1[4];
986 if (whence == SEEK_CUR)
988 else if (whence == SEEK_END)
990 if (offset < 0 || offset > 0x7fffffffLL)
999 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1001 if (
sizeof(
DynBuffer) + io_buffer_size < io_buffer_size)
1014 (*s)->max_packet_size = max_packet_size;
1025 if (max_packet_size <= 0)
1049 return size - padding;
uint64_t ffio_read_varlen(AVIOContext *bc)
const char const char void * val
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
int64_t(* read_seek)(void *opaque, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp in stream with the specified stream_index.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
#define LIBAVUTIL_VERSION_INT
int writeout_count
writeout statistic This field is internal to libavformat and access from outside is not allowed...
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size)
Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file...
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
void * opaque
A private pointer, passed to the read/write/seek/...
int write_flag
true if open for writing
int is_streamed
true if streamed (no seek possible), default = false
struct URLProtocol * prot
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
int64_t(* seek)(void *opaque, int64_t offset, int whence)
static const AVOption ffio_url_options[]
static const AVClass * ffio_url_child_class_next(const AVClass *prev)
int ffio_ensure_seekback(AVIOContext *s, int buf_size)
Ensures that the requested seekback buffer size will be available.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
unsigned char * buffer
Start of the buffer.
const char * av_default_item_name(void *ctx)
Return the context name.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static void fill_buffer(AVIOContext *s)
static const uint8_t offset[511][2]
int64_t bytes_read
Bytes read statistic This field is internal to libavformat and access from outside is not allowed...
int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
Read size bytes from AVIOContext, returning a pointer.
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
const OptionDef options[]
static void flush_buffer(AVIOContext *s)
Callback for checking whether to abort blocking functions.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
int(* write_packet)(void *opaque, uint8_t *buf, int buf_size)
const AVIOInterruptCB int_cb
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
int(* read_packet)(void *opaque, uint8_t *buf, int buf_size)
int ffio_set_buf_size(AVIOContext *s, int buf_size)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
#define SHORT_SEEK_THRESHOLD
Do seeks within this distance ahead of the current buffer by skipping data instead of calling the pro...
int direct
avio_read and avio_write should if possible be satisfied directly instead of going through a buffer...
unsigned char * buf_end
End of the data, may be less than buffer+buffer_size if the read function returned less data than req...
int seek_count
seek statistic This field is internal to libavformat and access from outside is not allowed...
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
unsigned char * checksum_ptr
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
void ffio_fill(AVIOContext *s, int b, int count)
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
Open a write only packetized memory stream with a maximum packet size of 'max_packet_size'.
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length) av_pure
Calculate the CRC of a block.
int must_flush
true if the next seek should flush
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
int(* url_read_pause)(URLContext *h, int pause)
unsigned char * buf_ptr
Current position in the buffer.
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
#define GET_STR16(type, read)
unsigned long(* update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size)
int buffer_size
Maximum buffer size.
const AVClass ffio_url_class
int64_t(* url_read_seek)(URLContext *h, int stream_index, int64_t timestamp, int flags)
static int url_resetbuf(AVIOContext *s, int flags)
void * av_realloc_f(void *ptr, size_t nelem, size_t elsize)
Allocate or reallocate a block of memory.
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Describe the class of an AVClass context structure.
unsigned long ffio_get_checksum(AVIOContext *s)
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
int error
contains the error code or 0 if no error happened
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
int(* read_pause)(void *opaque, int pause)
Pause or resume playback for network streaming protocols - e.g.
int ff_get_v_length(uint64_t val)
Get the length in bytes which is needed to store val as v.
int ffurl_close(URLContext *h)
const AVClass ffurl_context_class
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h...
static void * ffio_url_child_next(void *obj, void *prev)
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
int64_t pos
position in the file of the current buffer
#define PUT_UTF16(val, tmp, PUT_16BIT)
int eof_reached
true if eof reached
void ff_put_v(AVIOContext *bc, uint64_t val)
Put val using a variable number of bytes.
static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
static void writeout(AVIOContext *s, const uint8_t *data, int len)
int ffio_fdopen(AVIOContext **s, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h...
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
int max_packet_size
if non zero, the stream is packetized with this max packet size
unbuffered private I/O API
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define GET_UTF8(val, GET_BYTE, ERROR)
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.