diff aviobuf.c @ 2771:d52c718e83f9 libavformat

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
author andoma
date Wed, 21 Nov 2007 07:41:00 +0000
parents dfcac0102c34
children 1a9db30c1d1c
line wrap: on
line diff
--- a/aviobuf.c	Mon Nov 19 20:28:11 2007 +0000
+++ b/aviobuf.c	Wed Nov 21 07:41:00 2007 +0000
@@ -111,7 +111,12 @@
 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 size;
 
+    if(!s)
+        return AVERROR(EINVAL);
+
     if (!s->seek)
         return AVERROR(EPIPE);
     size = s->seek(s->opaque, 0, AVSEEK_SIZE);
@@ -181,11 +189,15 @@
 
 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 @@
     //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 @@
     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 @@
     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 @@
     URLContext *h = s->opaque;
 
     av_free(s->buffer);
-    memset(s, 0, sizeof(ByteIOContext));
+    av_free(s);
     return url_close(h);
 }
 
@@ -641,11 +660,18 @@
  * 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 @@
     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 @@
     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 @@
     *pbuffer = d->buffer;
     size = d->size;
     av_free(d);
+    av_free(s);
     return size;
 }
 #endif /* CONFIG_MUXERS || CONFIG_NETWORK */