#define IO_BUFFER_SIZE 32768
+/**
+ * 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(ByteIOContext *s);
-#if LIBAVFORMAT_VERSION_MAJOR >= 53
+#if !FF_API_URL_RESETBUF
static int url_resetbuf(ByteIOContext *s, int flags);
#endif
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 &&
+ } 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);
return offset;
}
-void url_fskip(ByteIOContext *s, int64_t offset)
+int url_fskip(ByteIOContext *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)
int len= s->buffer_size - (dst - s->buffer);
int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
- assert(s->buf_ptr == s->buf_end);
-
/* no need to do anything if EOF already reached */
if (s->eof_reached)
return;
/* XXX: put an inline version */
int get_byte(ByteIOContext *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)
{
- 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)
return buf;
}
+int ff_get_line(ByteIOContext *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;
+}
+
uint64_t get_be64(ByteIOContext *s)
{
uint64_t val;
return 0;
}
-#if LIBAVFORMAT_VERSION_MAJOR < 53
+#if FF_API_URL_RESETBUF
int url_resetbuf(ByteIOContext *s, int flags)
#else
static int url_resetbuf(ByteIOContext *s, int flags)
#endif
{
-#if LIBAVFORMAT_VERSION_MAJOR < 53
+#if FF_API_URL_RESETBUF
if (flags & URL_RDWR)
return AVERROR(EINVAL);
#else
{
int64_t buffer_start;
int buffer_size;
- int overlap, new_size;
+ int overlap, new_size, alloc_size;
if (s->write_flag)
return AVERROR(EINVAL);
overlap = buf_size - buffer_start;
new_size = buf_size + buffer_size - overlap;
- if (new_size > buf_size) {
- if (!(buf = av_realloc(buf, new_size)))
+ 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->pos = s->buffer_size = buf_size;
+ 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;