* Buffered I/O for ffmpeg system
* Copyright (c) 2000,2001 Fabrice Bellard
*
- * This file is part of FFmpeg.
+ * This file is part of Libav.
*
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avio.h"
#include "avio_internal.h"
#include "internal.h"
+#include "url.h"
#include <stdarg.h>
#define IO_BUFFER_SIZE 32768
#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 ffio_init_context(AVIOContext *s,
unsigned char *buffer,
s->buffer_size = buffer_size;
s->buf_ptr = buffer;
s->opaque = opaque;
- url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
+ url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
s->write_packet = write_packet;
s->read_packet = read_packet;
s->seek = seek;
s->must_flush = 0;
s->eof_reached = 0;
s->error = 0;
+#if FF_API_OLD_AVIO
s->is_streamed = 0;
+#endif
+ s->seekable = AVIO_SEEKABLE_NORMAL;
s->max_packet_size = 0;
s->update_checksum= NULL;
if(!read_packet && !write_flag){
int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
AVIOContext *s = av_mallocz(sizeof(AVIOContext));
+ if (!s)
+ return NULL;
ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
read_packet, write_packet, seek);
return s;
}
}
-void put_flush_packet(AVIOContext *s)
+void avio_flush(AVIOContext *s)
{
flush_buffer(s);
s->must_flush = 0;
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 ||
+ } else if ((!s->seekable ||
offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
!s->write_flag && offset1 >= 0 &&
(whence != SEEK_END || force)) {
}
#endif
-int64_t url_fsize(AVIOContext *s)
+int64_t avio_size(AVIOContext *s)
{
int64_t size;
return size;
}
+#if FF_API_OLD_AVIO
int url_feof(AVIOContext *s)
{
if(!s)
return 0;
return s->error;
}
+#endif
void avio_wl32(AVIOContext *s, unsigned int val)
{
{
return avio_seek(s, offset, whence);
}
+int64_t url_fsize(AVIOContext *s)
+{
+ return avio_size(s);
+}
+int url_setbufsize(AVIOContext *s, int buf_size)
+{
+ return ffio_set_buf_size(s, buf_size);
+}
+int url_fprintf(AVIOContext *s, const char *fmt, ...)
+{
+ va_list ap;
+ char buf[4096];
+ int ret;
+
+ va_start(ap, fmt);
+ ret = vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+ avio_write(s, buf, strlen(buf));
+ return ret;
+}
+void put_flush_packet(AVIOContext *s)
+{
+ avio_flush(s);
+}
+int av_url_read_fpause(AVIOContext *s, int pause)
+{
+ return avio_pause(s, pause);
+}
+int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
+ int64_t timestamp, int flags)
+{
+ return avio_seek_time(s, stream_index, timestamp, flags);
+}
+void init_checksum(AVIOContext *s,
+ unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
+ unsigned long checksum)
+{
+ ffio_init_checksum(s, update_checksum, checksum);
+}
+unsigned long get_checksum(AVIOContext *s)
+{
+ return ffio_get_checksum(s);
+}
+int url_open_dyn_buf(AVIOContext **s)
+{
+ return avio_open_dyn_buf(s);
+}
+int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
+{
+ return ffio_open_dyn_packet_buf(s, max_packet_size);
+}
+int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
+{
+ return avio_close_dyn_buf(s, pbuffer);
+}
+int url_fdopen(AVIOContext **s, URLContext *h)
+{
+ return ffio_fdopen(s, h);
+}
#endif
int avio_put_str(AVIOContext *s, const char *str)
/* make buffer smaller in case it ended up large after probing */
if (s->buffer_size > max_buffer_size) {
- url_setbufsize(s, max_buffer_size);
+ ffio_set_buf_size(s, max_buffer_size);
s->checksum_ptr = dst = s->buffer;
len = s->buffer_size;
return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
}
-unsigned long get_checksum(AVIOContext *s)
+unsigned long ffio_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(AVIOContext *s,
+void ffio_init_checksum(AVIOContext *s,
unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
unsigned long checksum)
{
return 0;
}
+#if FF_API_OLD_AVIO
int url_fgetc(AVIOContext *s)
{
if (s->buf_ptr >= s->buf_end)
return *s->buf_ptr++;
return URL_EOF;
}
+#endif
int avio_read(AVIOContext *s, unsigned char *buf, int size)
{
}
}
if (size1 == size) {
- if (url_ferror(s)) return url_ferror(s);
- if (url_feof(s)) return AVERROR_EOF;
+ if (s->error) return s->error;
+ if (s->eof_reached) return AVERROR_EOF;
}
return size1 - size;
}
memcpy(buf, s->buf_ptr, len);
s->buf_ptr += len;
if (!len) {
- if (url_ferror(s)) return url_ferror(s);
- if (url_feof(s)) return AVERROR_EOF;
+ if (s->error) return s->error;
+ if (s->eof_reached) return AVERROR_EOF;
}
return len;
}
return val;
}
-uint64_t ff_get_v(AVIOContext *bc){
+uint64_t ffio_read_varlen(AVIOContext *bc){
uint64_t val = 0;
int tmp;
return val;
}
-int url_fdopen(AVIOContext **s, URLContext *h)
+int ffio_fdopen(AVIOContext **s, URLContext *h)
{
uint8_t *buffer;
int buffer_size, max_packet_size;
- max_packet_size = url_get_max_packet_size(h);
+ max_packet_size = h->max_packet_size;
if (max_packet_size) {
buffer_size = max_packet_size; /* no need to bufferize more than one packet */
} else {
if (!buffer)
return AVERROR(ENOMEM);
- *s = av_mallocz(sizeof(AVIOContext));
- if(!*s) {
+ *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
+ ffurl_read, ffurl_write, ffurl_seek);
+ if (!*s) {
av_free(buffer);
return AVERROR(ENOMEM);
}
-
- if (ffio_init_context(*s, buffer, buffer_size,
- (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
- url_read, url_write, url_seek) < 0) {
- av_free(buffer);
- av_freep(s);
- return AVERROR(EIO);
- }
+#if FF_API_OLD_AVIO
(*s)->is_streamed = h->is_streamed;
+#endif
+ (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
(*s)->max_packet_size = max_packet_size;
if(h->prot) {
(*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
return 0;
}
-int url_setbufsize(AVIOContext *s, int buf_size)
+int ffio_set_buf_size(AVIOContext *s, int buf_size)
{
uint8_t *buffer;
buffer = av_malloc(buf_size);
s->buffer = buffer;
s->buffer_size = buf_size;
s->buf_ptr = buffer;
- url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
+ url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
return 0;
}
-#if FF_API_URL_RESETBUF
-int url_resetbuf(AVIOContext *s, int flags)
-#else
static int url_resetbuf(AVIOContext *s, int flags)
-#endif
{
-#if FF_API_URL_RESETBUF
- if (flags & URL_RDWR)
- return AVERROR(EINVAL);
-#else
- assert(flags == URL_WRONLY || flags == URL_RDONLY);
-#endif
+ assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
- if (flags & URL_WRONLY) {
+ if (flags & AVIO_FLAG_WRITE) {
s->buf_end = s->buffer + s->buffer_size;
s->write_flag = 1;
} else {
return 0;
}
-int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
+int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
{
int64_t buffer_start;
int buffer_size;
URLContext *h;
int err;
- err = url_open(&h, filename, flags);
+ err = ffurl_open(&h, filename, flags);
if (err < 0)
return err;
- err = url_fdopen(s, h);
+ err = ffio_fdopen(s, h);
if (err < 0) {
- url_close(h);
+ ffurl_close(h);
return err;
}
return 0;
av_free(s->buffer);
av_free(s);
- return url_close(h);
+ return ffurl_close(h);
}
+#if FF_API_OLD_AVIO
URLContext *url_fileno(AVIOContext *s)
{
return s->opaque;
}
+#endif
-#if CONFIG_MUXERS
-int url_fprintf(AVIOContext *s, const char *fmt, ...)
+int avio_printf(AVIOContext *s, const char *fmt, ...)
{
va_list ap;
char buf[4096];
avio_write(s, buf, strlen(buf));
return ret;
}
-#endif //CONFIG_MUXERS
#if FF_API_OLD_AVIO
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
int c;
char *q;
- c = url_fgetc(s);
- if (c == EOF)
+ c = avio_r8(s);
+ if (s->eof_reached)
return NULL;
q = buf;
for(;;) {
- if (c == EOF || c == '\n')
+ if (s->eof_reached || c == '\n')
break;
if ((q - buf) < buf_size - 1)
*q++ = c;
- c = url_fgetc(s);
+ c = avio_r8(s);
}
if (buf_size > 0)
*q = '\0';
return buf;
}
-#endif
int url_fget_max_packet_size(AVIOContext *s)
{
return s->max_packet_size;
}
+#endif
-int av_url_read_fpause(AVIOContext *s, int pause)
+int avio_pause(AVIOContext *s, int pause)
{
if (!s->read_pause)
return AVERROR(ENOSYS);
return s->read_pause(s->opaque, pause);
}
-int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
- int64_t timestamp, int flags)
+int64_t avio_seek_time(AVIOContext *s, int stream_index,
+ int64_t timestamp, int flags)
{
URLContext *h = s->opaque;
int64_t ret;
return ret;
}
-/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
- * back to the server even if CONFIG_MUXERS is false. */
-#if CONFIG_MUXERS || CONFIG_NETWORK
/* buffer handling */
+#if FF_API_OLD_AVIO
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
{
int ret;
if(!*s)
return AVERROR(ENOMEM);
ret = ffio_init_context(*s, buf, buf_size,
- (flags & URL_WRONLY || flags & URL_RDWR),
+ flags & AVIO_FLAG_WRITE,
NULL, NULL, NULL, NULL);
if(ret != 0)
av_freep(s);
int url_close_buf(AVIOContext *s)
{
- put_flush_packet(s);
+ avio_flush(s);
return s->buf_ptr - s->buffer;
}
+#endif
/* output in a dynamic buffer */
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
{
DynBuffer *d;
- int ret;
unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
if (!d)
return AVERROR(ENOMEM);
- *s = av_mallocz(sizeof(AVIOContext));
+ d->io_buffer_size = io_buffer_size;
+ *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
+ max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
+ max_packet_size ? NULL : dyn_buf_seek);
if(!*s) {
av_free(d);
return AVERROR(ENOMEM);
}
- d->io_buffer_size = io_buffer_size;
- ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
- 1, d, NULL,
- max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
- max_packet_size ? NULL : dyn_buf_seek);
- if (ret == 0) {
- (*s)->max_packet_size = max_packet_size;
- } else {
- av_free(d);
- av_freep(s);
- }
- return ret;
+ (*s)->max_packet_size = max_packet_size;
+ return 0;
}
-int url_open_dyn_buf(AVIOContext **s)
+int avio_open_dyn_buf(AVIOContext **s)
{
return url_open_dyn_buf_internal(s, 0);
}
-int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
+int ffio_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(AVIOContext *s, uint8_t **pbuffer)
+int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
{
DynBuffer *d = s->opaque;
int size;
padding = FF_INPUT_BUFFER_PADDING_SIZE;
}
- put_flush_packet(s);
+ avio_flush(s);
*pbuffer = d->buffer;
size = d->size;
av_free(s);
return size - padding;
}
-#endif /* CONFIG_MUXERS || CONFIG_NETWORK */