#include "libavutil/intreadwrite.h"
#include "avformat.h"
#include "avio.h"
+#include "internal.h"
#include <stdarg.h>
#define IO_BUFFER_SIZE 32768
-static void fill_buffer(ByteIOContext *s);
-#if LIBAVFORMAT_VERSION_MAJOR >= 53
-static int url_resetbuf(ByteIOContext *s, int flags);
+/**
+ * Do seeks within this distance ahead of the current buffer by skipping
+ * data instead of calling the protocol seek function, for seekable
+ * protocols.
+ */
+#define SHORT_SEEK_THRESHOLD 4096
+
+static void fill_buffer(AVIOContext *s);
+#if !FF_API_URL_RESETBUF
+static int url_resetbuf(AVIOContext *s, int flags);
#endif
-int init_put_byte(ByteIOContext *s,
+int init_put_byte(AVIOContext *s,
unsigned char *buffer,
int buffer_size,
int write_flag,
return 0;
}
-ByteIOContext *av_alloc_put_byte(
+AVIOContext *av_alloc_put_byte(
unsigned char *buffer,
int buffer_size,
int write_flag,
int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
- ByteIOContext *s = av_mallocz(sizeof(ByteIOContext));
+ AVIOContext *s = av_mallocz(sizeof(AVIOContext));
init_put_byte(s, buffer, buffer_size, write_flag, opaque,
read_packet, write_packet, seek);
return s;
}
-static void flush_buffer(ByteIOContext *s)
+static void flush_buffer(AVIOContext *s)
{
if (s->buf_ptr > s->buffer) {
if (s->write_packet && !s->error){
s->buf_ptr = s->buffer;
}
-void put_byte(ByteIOContext *s, int b)
+void put_byte(AVIOContext *s, int b)
{
*(s->buf_ptr)++ = b;
if (s->buf_ptr >= s->buf_end)
flush_buffer(s);
}
-void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
+void put_nbyte(AVIOContext *s, int b, int count)
+{
+ while (count > 0) {
+ int len = FFMIN(s->buf_end - s->buf_ptr, count);
+ memset(s->buf_ptr, b, len);
+ s->buf_ptr += len;
+
+ if (s->buf_ptr >= s->buf_end)
+ flush_buffer(s);
+
+ count -= len;
+ }
+}
+
+void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
{
while (size > 0) {
int len = FFMIN(s->buf_end - s->buf_ptr, size);
}
}
-void put_flush_packet(ByteIOContext *s)
+void put_flush_packet(AVIOContext *s)
{
flush_buffer(s);
s->must_flush = 0;
}
-int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence)
+int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
{
int64_t offset1;
int64_t pos;
+ int force = whence & AVSEEK_FORCE;
+ whence &= ~AVSEEK_FORCE;
if(!s)
return AVERROR(EINVAL);
offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
/* can do the seek inside the buffer */
s->buf_ptr = s->buffer + offset1;
- } else if(s->is_streamed && !s->write_flag && offset1 >= 0 &&
- ( offset1 < (s->buf_end - s->buffer) + (1<<16)
- || (whence & AVSEEK_FORCE))){
+ } else if ((s->is_streamed ||
+ offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
+ !s->write_flag && offset1 >= 0 &&
+ (whence != SEEK_END || force)) {
while(s->pos < offset && !s->eof_reached)
fill_buffer(s);
if (s->eof_reached)
- return AVERROR(EPIPE);
+ return AVERROR_EOF;
s->buf_ptr = s->buf_end + offset - s->pos;
} else {
- int64_t res = AVERROR(EPIPE);
+ int64_t res;
#if CONFIG_MUXERS || CONFIG_NETWORK
if (s->write_flag) {
s->must_flush = 1;
}
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
- if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
+ if (!s->seek)
+ return AVERROR(EPIPE);
+ if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
return res;
if (!s->write_flag)
s->buf_end = s->buffer;
return offset;
}
-void url_fskip(ByteIOContext *s, int64_t offset)
+int url_fskip(AVIOContext *s, int64_t offset)
{
- url_fseek(s, offset, SEEK_CUR);
+ int64_t ret = url_fseek(s, offset, SEEK_CUR);
+ return ret < 0 ? ret : 0;
}
-int64_t url_ftell(ByteIOContext *s)
+int64_t url_ftell(AVIOContext *s)
{
return url_fseek(s, 0, SEEK_CUR);
}
-int64_t url_fsize(ByteIOContext *s)
+int64_t url_fsize(AVIOContext *s)
{
int64_t size;
return AVERROR(EINVAL);
if (!s->seek)
- return AVERROR(EPIPE);
+ return AVERROR(ENOSYS);
size = s->seek(s->opaque, 0, AVSEEK_SIZE);
if(size<0){
if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
return size;
}
-int url_feof(ByteIOContext *s)
+int url_feof(AVIOContext *s)
{
if(!s)
return 0;
return s->eof_reached;
}
-int url_ferror(ByteIOContext *s)
+int url_ferror(AVIOContext *s)
{
if(!s)
return 0;
return s->error;
}
-void put_le32(ByteIOContext *s, unsigned int val)
+void put_le32(AVIOContext *s, unsigned int val)
{
put_byte(s, val);
put_byte(s, val >> 8);
put_byte(s, val >> 24);
}
-void put_be32(ByteIOContext *s, unsigned int val)
+void put_be32(AVIOContext *s, unsigned int val)
{
put_byte(s, val >> 24);
put_byte(s, val >> 16);
put_byte(s, val);
}
-void put_strz(ByteIOContext *s, const char *str)
+#if FF_API_OLD_AVIO
+void put_strz(AVIOContext *s, const char *str)
{
- if (str)
- put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
- else
+ avio_put_str(s, str);
+}
+#endif
+
+int avio_put_str(AVIOContext *s, const char *str)
+{
+ int len = 1;
+ if (str) {
+ len += strlen(str);
+ put_buffer(s, (const unsigned char *) str, len);
+ } else
put_byte(s, 0);
+ return len;
}
-void put_le64(ByteIOContext *s, uint64_t val)
+int avio_put_str16le(AVIOContext *s, const char *str)
+{
+ const uint8_t *q = str;
+ int ret = 0;
+
+ while (*q) {
+ uint32_t ch;
+ uint16_t tmp;
+
+ GET_UTF8(ch, *q++, break;)
+ PUT_UTF16(ch, tmp, put_le16(s, tmp);ret += 2;)
+ }
+ put_le16(s, 0);
+ ret += 2;
+ return ret;
+}
+
+int ff_get_v_length(uint64_t val){
+ int i=1;
+
+ while(val>>=7)
+ i++;
+
+ return i;
+}
+
+void ff_put_v(AVIOContext *bc, uint64_t val){
+ int i= ff_get_v_length(val);
+
+ while(--i>0)
+ put_byte(bc, 128 | (val>>(7*i)));
+
+ put_byte(bc, val&127);
+}
+
+void put_le64(AVIOContext *s, uint64_t val)
{
put_le32(s, (uint32_t)(val & 0xffffffff));
put_le32(s, (uint32_t)(val >> 32));
}
-void put_be64(ByteIOContext *s, uint64_t val)
+void put_be64(AVIOContext *s, uint64_t val)
{
put_be32(s, (uint32_t)(val >> 32));
put_be32(s, (uint32_t)(val & 0xffffffff));
}
-void put_le16(ByteIOContext *s, unsigned int val)
+void put_le16(AVIOContext *s, unsigned int val)
{
put_byte(s, val);
put_byte(s, val >> 8);
}
-void put_be16(ByteIOContext *s, unsigned int val)
+void put_be16(AVIOContext *s, unsigned int val)
{
put_byte(s, val >> 8);
put_byte(s, val);
}
-void put_le24(ByteIOContext *s, unsigned int val)
+void put_le24(AVIOContext *s, unsigned int val)
{
put_le16(s, val & 0xffff);
put_byte(s, val >> 16);
}
-void put_be24(ByteIOContext *s, unsigned int val)
+void put_be24(AVIOContext *s, unsigned int val)
{
put_be16(s, val >> 8);
put_byte(s, val);
}
-void put_tag(ByteIOContext *s, const char *tag)
+void put_tag(AVIOContext *s, const char *tag)
{
while (*tag) {
put_byte(s, *tag++);
/* Input stream */
-static void fill_buffer(ByteIOContext *s)
+static void fill_buffer(AVIOContext *s)
{
uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
int len= s->buffer_size - (dst - s->buffer);
-
- assert(s->buf_ptr == s->buf_end);
+ int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
/* no need to do anything if EOF already reached */
if (s->eof_reached)
s->checksum_ptr= s->buffer;
}
+ /* make buffer smaller in case it ended up large after probing */
+ if (s->buffer_size > max_buffer_size) {
+ url_setbufsize(s, max_buffer_size);
+
+ s->checksum_ptr = dst = s->buffer;
+ len = s->buffer_size;
+ }
+
if(s->read_packet)
len = s->read_packet(s->opaque, dst, len);
else
return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
}
-unsigned long get_checksum(ByteIOContext *s)
+unsigned long get_checksum(AVIOContext *s)
{
s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
s->update_checksum= NULL;
return s->checksum;
}
-void init_checksum(ByteIOContext *s,
+void init_checksum(AVIOContext *s,
unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
unsigned long checksum)
{
}
/* XXX: put an inline version */
-int get_byte(ByteIOContext *s)
+int get_byte(AVIOContext *s)
{
- if (s->buf_ptr < s->buf_end) {
- return *s->buf_ptr++;
- } else {
+ if (s->buf_ptr >= s->buf_end)
fill_buffer(s);
- if (s->buf_ptr < s->buf_end)
- return *s->buf_ptr++;
- else
- return 0;
- }
+ if (s->buf_ptr < s->buf_end)
+ return *s->buf_ptr++;
+ return 0;
}
-int url_fgetc(ByteIOContext *s)
+int url_fgetc(AVIOContext *s)
{
- if (s->buf_ptr < s->buf_end) {
- return *s->buf_ptr++;
- } else {
+ if (s->buf_ptr >= s->buf_end)
fill_buffer(s);
- if (s->buf_ptr < s->buf_end)
- return *s->buf_ptr++;
- else
- return URL_EOF;
- }
+ if (s->buf_ptr < s->buf_end)
+ return *s->buf_ptr++;
+ return URL_EOF;
}
-int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
+int get_buffer(AVIOContext *s, unsigned char *buf, int size)
{
int len, size1;
return size1 - size;
}
-int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
+int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
{
int len;
return len;
}
-unsigned int get_le16(ByteIOContext *s)
+unsigned int get_le16(AVIOContext *s)
{
unsigned int val;
val = get_byte(s);
return val;
}
-unsigned int get_le24(ByteIOContext *s)
+unsigned int get_le24(AVIOContext *s)
{
unsigned int val;
val = get_le16(s);
return val;
}
-unsigned int get_le32(ByteIOContext *s)
+unsigned int get_le32(AVIOContext *s)
{
unsigned int val;
val = get_le16(s);
return val;
}
-uint64_t get_le64(ByteIOContext *s)
+uint64_t get_le64(AVIOContext *s)
{
uint64_t val;
val = (uint64_t)get_le32(s);
return val;
}
-unsigned int get_be16(ByteIOContext *s)
+unsigned int get_be16(AVIOContext *s)
{
unsigned int val;
val = get_byte(s) << 8;
return val;
}
-unsigned int get_be24(ByteIOContext *s)
+unsigned int get_be24(AVIOContext *s)
{
unsigned int val;
val = get_be16(s) << 8;
val |= get_byte(s);
return val;
}
-unsigned int get_be32(ByteIOContext *s)
+unsigned int get_be32(AVIOContext *s)
{
unsigned int val;
val = get_be16(s) << 16;
return val;
}
-char *get_strz(ByteIOContext *s, char *buf, int maxlen)
+char *get_strz(AVIOContext *s, char *buf, int maxlen)
{
int i = 0;
char c;
return buf;
}
-uint64_t get_be64(ByteIOContext *s)
+int ff_get_line(AVIOContext *s, char *buf, int maxlen)
+{
+ int i = 0;
+ char c;
+
+ do {
+ c = get_byte(s);
+ if (c && i < maxlen-1)
+ buf[i++] = c;
+ } while (c != '\n' && c);
+
+ buf[i] = 0;
+ return i;
+}
+
+#define GET_STR16(type, read) \
+ int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
+{\
+ char* q = buf;\
+ int ret = 0;\
+ while (ret + 1 < maxlen) {\
+ uint8_t tmp;\
+ uint32_t ch;\
+ GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
+ if (!ch)\
+ break;\
+ PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
+ }\
+ *q = 0;\
+ return ret;\
+}\
+
+GET_STR16(le, get_le16)
+GET_STR16(be, get_be16)
+
+#undef GET_STR16
+
+uint64_t get_be64(AVIOContext *s)
{
uint64_t val;
val = (uint64_t)get_be32(s) << 32;
return val;
}
-uint64_t ff_get_v(ByteIOContext *bc){
+uint64_t ff_get_v(AVIOContext *bc){
uint64_t val = 0;
int tmp;
return val;
}
-int url_fdopen(ByteIOContext **s, URLContext *h)
+int url_fdopen(AVIOContext **s, URLContext *h)
{
uint8_t *buffer;
int buffer_size, max_packet_size;
if (!buffer)
return AVERROR(ENOMEM);
- *s = av_mallocz(sizeof(ByteIOContext));
+ *s = av_mallocz(sizeof(AVIOContext));
if(!*s) {
av_free(buffer);
return AVERROR(ENOMEM);
return 0;
}
-int url_setbufsize(ByteIOContext *s, int buf_size)
+int url_setbufsize(AVIOContext *s, int buf_size)
{
uint8_t *buffer;
buffer = av_malloc(buf_size);
return 0;
}
-#if LIBAVFORMAT_VERSION_MAJOR < 53
-int url_resetbuf(ByteIOContext *s, int flags)
+#if FF_API_URL_RESETBUF
+int url_resetbuf(AVIOContext *s, int flags)
#else
-static int url_resetbuf(ByteIOContext *s, int flags)
+static int url_resetbuf(AVIOContext *s, int flags)
#endif
{
-#if LIBAVFORMAT_VERSION_MAJOR < 53
- URLContext *h = s->opaque;
- if ((flags & URL_RDWR) || (h && h->flags != flags && !h->flags & URL_RDWR))
+#if FF_API_URL_RESETBUF
+ if (flags & URL_RDWR)
return AVERROR(EINVAL);
#else
assert(flags == URL_WRONLY || flags == URL_RDONLY);
return 0;
}
-int url_fopen(ByteIOContext **s, const char *filename, int flags)
+int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
+{
+ int64_t buffer_start;
+ int buffer_size;
+ int overlap, new_size, alloc_size;
+
+ if (s->write_flag)
+ return AVERROR(EINVAL);
+
+ buffer_size = s->buf_end - s->buffer;
+
+ /* the buffers must touch or overlap */
+ if ((buffer_start = s->pos - buffer_size) > buf_size)
+ return AVERROR(EINVAL);
+
+ overlap = buf_size - buffer_start;
+ new_size = buf_size + buffer_size - overlap;
+
+ alloc_size = FFMAX(s->buffer_size, new_size);
+ if (alloc_size > buf_size)
+ if (!(buf = av_realloc(buf, alloc_size)))
+ return AVERROR(ENOMEM);
+
+ if (new_size > buf_size) {
+ memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
+ buf_size = new_size;
+ }
+
+ av_free(s->buffer);
+ s->buf_ptr = s->buffer = buf;
+ s->buffer_size = alloc_size;
+ s->pos = buf_size;
+ s->buf_end = s->buf_ptr + buf_size;
+ s->eof_reached = 0;
+ s->must_flush = 0;
+
+ return 0;
+}
+
+int url_fopen(AVIOContext **s, const char *filename, int flags)
{
URLContext *h;
int err;
return 0;
}
-int url_fclose(ByteIOContext *s)
+int url_fclose(AVIOContext *s)
{
URLContext *h = s->opaque;
return url_close(h);
}
-URLContext *url_fileno(ByteIOContext *s)
+URLContext *url_fileno(AVIOContext *s)
{
return s->opaque;
}
#if CONFIG_MUXERS
-int url_fprintf(ByteIOContext *s, const char *fmt, ...)
+int url_fprintf(AVIOContext *s, const char *fmt, ...)
{
va_list ap;
char buf[4096];
}
#endif //CONFIG_MUXERS
-char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
+char *url_fgets(AVIOContext *s, char *buf, int buf_size)
{
int c;
char *q;
return buf;
}
-int url_fget_max_packet_size(ByteIOContext *s)
+int url_fget_max_packet_size(AVIOContext *s)
{
return s->max_packet_size;
}
-int av_url_read_fpause(ByteIOContext *s, int pause)
+int av_url_read_fpause(AVIOContext *s, int pause)
{
if (!s->read_pause)
return AVERROR(ENOSYS);
return s->read_pause(s->opaque, pause);
}
-int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
+int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
int64_t timestamp, int flags)
{
URLContext *h = s->opaque;
return AVERROR(ENOSYS);
ret = s->read_seek(h, stream_index, timestamp, flags);
if(ret >= 0) {
+ int64_t pos;
s->buf_ptr = s->buf_end; // Flush buffer
- s->pos = s->seek(h, 0, SEEK_CUR);
+ pos = s->seek(h, 0, SEEK_CUR);
+ if (pos >= 0)
+ s->pos = pos;
+ else if (pos != AVERROR(ENOSYS))
+ ret = pos;
}
return ret;
}
* back to the server even if CONFIG_MUXERS is false. */
#if CONFIG_MUXERS || CONFIG_NETWORK
/* buffer handling */
-int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
+int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
{
int ret;
- *s = av_mallocz(sizeof(ByteIOContext));
+ *s = av_mallocz(sizeof(AVIOContext));
if(!*s)
return AVERROR(ENOMEM);
ret = init_put_byte(*s, buf, buf_size,
return ret;
}
-int url_close_buf(ByteIOContext *s)
+int url_close_buf(AVIOContext *s)
{
put_flush_packet(s);
return s->buf_ptr - s->buffer;
return 0;
}
-static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
+static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
{
DynBuffer *d;
int ret;
d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
if (!d)
return AVERROR(ENOMEM);
- *s = av_mallocz(sizeof(ByteIOContext));
+ *s = av_mallocz(sizeof(AVIOContext));
if(!*s) {
av_free(d);
return AVERROR(ENOMEM);
return ret;
}
-int url_open_dyn_buf(ByteIOContext **s)
+int url_open_dyn_buf(AVIOContext **s)
{
return url_open_dyn_buf_internal(s, 0);
}
-int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
+int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
{
if (max_packet_size <= 0)
return -1;
return url_open_dyn_buf_internal(s, max_packet_size);
}
-int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
+int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
{
DynBuffer *d = s->opaque;
int size;
+ static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
+ int padding = 0;
+
+ /* don't attempt to pad fixed-size packet buffers */
+ if (!s->max_packet_size) {
+ put_buffer(s, padbuf, sizeof(padbuf));
+ padding = FF_INPUT_BUFFER_PADDING_SIZE;
+ }
put_flush_packet(s);
size = d->size;
av_free(d);
av_free(s);
- return size;
+ return size - padding;
}
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */