diff options
author | Björn Axelsson <gecko@acc.umu.se> | 2007-11-21 07:41:00 +0000 |
---|---|---|
committer | Andreas Öman <andreas@lonelycoder.com> | 2007-11-21 07:41:00 +0000 |
commit | 899681cd1dbf4cd7c3b86af23bca25e20a54f4d0 (patch) | |
tree | 6f4556497efab1d703d1289b170c936154c6bbd5 /libavformat/aviobuf.c | |
parent | 79815f622d90499f882ad968a1351134535cbbab (diff) | |
download | ffmpeg-899681cd1dbf4cd7c3b86af23bca25e20a54f4d0.tar.gz |
Use dynamically allocated ByteIOContext in AVFormatContext
patch by: Björn Axelsson, bjorn d axelsson a intinor d se
thread: [PATCH] Remove static ByteIOContexts, 06 nov 2007
Originally committed as revision 11071 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'libavformat/aviobuf.c')
-rw-r--r-- | libavformat/aviobuf.c | 67 |
1 files changed, 51 insertions, 16 deletions
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 8bf6c063ea..c79f51fe65 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -111,7 +111,12 @@ void put_flush_packet(ByteIOContext *s) offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence) { offset_t offset1; - offset_t pos= s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer)); + offset_t pos; + + if(!s) + return AVERROR(EINVAL); + + pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer)); if (whence != SEEK_CUR && whence != SEEK_SET) return AVERROR(EINVAL); @@ -167,6 +172,9 @@ offset_t url_fsize(ByteIOContext *s) { offset_t size; + if(!s) + return AVERROR(EINVAL); + if (!s->seek) return AVERROR(EPIPE); size = s->seek(s->opaque, 0, AVSEEK_SIZE); @@ -181,11 +189,15 @@ offset_t url_fsize(ByteIOContext *s) int url_feof(ByteIOContext *s) { + if(!s) + return 0; return s->eof_reached; } int url_ferror(ByteIOContext *s) { + if(!s) + return 0; return s->error; } @@ -508,7 +520,7 @@ static offset_t url_seek_packet(void *opaque, offset_t offset, int whence) //return 0; } -int url_fdopen(ByteIOContext *s, URLContext *h) +int url_fdopen(ByteIOContext **s, URLContext *h) { uint8_t *buffer; int buffer_size, max_packet_size; @@ -524,14 +536,21 @@ int url_fdopen(ByteIOContext *s, URLContext *h) if (!buffer) return AVERROR(ENOMEM); - if (init_put_byte(s, buffer, buffer_size, + *s = av_mallocz(sizeof(ByteIOContext)); + if(!*s) { + av_free(buffer); + return AVERROR(ENOMEM); + } + + if (init_put_byte(*s, buffer, buffer_size, (h->flags & URL_WRONLY || h->flags & URL_RDWR), h, url_read_packet, url_write_packet, url_seek_packet) < 0) { av_free(buffer); + av_freep(s); return AVERROR(EIO); } - s->is_streamed = h->is_streamed; - s->max_packet_size = max_packet_size; + (*s)->is_streamed = h->is_streamed; + (*s)->max_packet_size = max_packet_size; return 0; } @@ -566,7 +585,7 @@ int url_resetbuf(ByteIOContext *s, int flags) return 0; } -int url_fopen(ByteIOContext *s, const char *filename, int flags) +int url_fopen(ByteIOContext **s, const char *filename, int flags) { URLContext *h; int err; @@ -587,7 +606,7 @@ int url_fclose(ByteIOContext *s) URLContext *h = s->opaque; av_free(s->buffer); - memset(s, 0, sizeof(ByteIOContext)); + av_free(s); return url_close(h); } @@ -641,11 +660,18 @@ int url_fget_max_packet_size(ByteIOContext *s) * back to the server even if CONFIG_MUXERS is not set. */ #if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK) /* buffer handling */ -int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags) +int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags) { - return init_put_byte(s, buf, buf_size, - (flags & URL_WRONLY || flags & URL_RDWR), - NULL, NULL, NULL, NULL); + int ret; + *s = av_mallocz(sizeof(ByteIOContext)); + if(!*s) + return AVERROR(ENOMEM); + ret = init_put_byte(*s, buf, buf_size, + (flags & URL_WRONLY || flags & URL_RDWR), + NULL, NULL, NULL, NULL); + if(ret != 0) + av_freep(s); + return ret; } int url_close_buf(ByteIOContext *s) @@ -725,7 +751,7 @@ static offset_t dyn_buf_seek(void *opaque, offset_t offset, int whence) return 0; } -static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size) +static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size) { DynBuffer *d; int io_buffer_size, ret; @@ -740,27 +766,35 @@ static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size) d = av_malloc(sizeof(DynBuffer) + io_buffer_size); if (!d) return -1; + *s = av_mallocz(sizeof(ByteIOContext)); + if(!*s) { + av_free(d); + return AVERROR(ENOMEM); + } d->io_buffer_size = io_buffer_size; d->buffer = NULL; d->pos = 0; d->size = 0; d->allocated_size = 0; - ret = init_put_byte(s, d->io_buffer, io_buffer_size, + ret = init_put_byte(*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; + (*s)->max_packet_size = max_packet_size; + } else { + av_free(d); + av_freep(s); } return ret; } -int url_open_dyn_buf(ByteIOContext *s) +int url_open_dyn_buf(ByteIOContext **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(ByteIOContext **s, int max_packet_size) { if (max_packet_size <= 0) return -1; @@ -777,6 +811,7 @@ int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer) *pbuffer = d->buffer; size = d->size; av_free(d); + av_free(s); return size; } #endif /* CONFIG_MUXERS || CONFIG_NETWORK */ |