]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/aviobuf.c
Fix indentation after r21257.
[ffmpeg] / libavformat / aviobuf.c
index e19e70beb4509689d45288b6522ddced2daa169f..06c7a107493eeafc0a1a8abca9dcb18a5fd2a75f 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/crc.h"
+#include "libavutil/intreadwrite.h"
 #include "avformat.h"
 #include "avio.h"
 #include <stdarg.h>
@@ -27,6 +28,9 @@
 #define IO_BUFFER_SIZE 32768
 
 static void fill_buffer(ByteIOContext *s);
+#if LIBAVFORMAT_VERSION_MAJOR >= 53
+static int url_resetbuf(ByteIOContext *s, int flags);
+#endif
 
 int init_put_byte(ByteIOContext *s,
                   unsigned char *buffer,
@@ -40,8 +44,8 @@ int init_put_byte(ByteIOContext *s,
     s->buffer = buffer;
     s->buffer_size = buffer_size;
     s->buf_ptr = buffer;
-    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
     s->opaque = opaque;
+    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
     s->write_packet = write_packet;
     s->read_packet = read_packet;
     s->seek = seek;
@@ -68,7 +72,8 @@ ByteIOContext *av_alloc_put_byte(
                   void *opaque,
                   int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
                   int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
-                  int64_t (*seek)(void *opaque, int64_t offset, int whence)) {
+                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
+{
     ByteIOContext *s = av_mallocz(sizeof(ByteIOContext));
     init_put_byte(s, buffer, buffer_size, write_flag, opaque,
                   read_packet, write_packet, seek);
@@ -102,12 +107,8 @@ void put_byte(ByteIOContext *s, int b)
 
 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
 {
-    int len;
-
     while (size > 0) {
-        len = (s->buf_end - s->buf_ptr);
-        if (len > size)
-            len = size;
+        int len = FFMIN(s->buf_end - s->buf_ptr, size);
         memcpy(s->buf_ptr, buf, len);
         s->buf_ptr += len;
 
@@ -146,7 +147,7 @@ int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence)
     }
     offset1 = offset - pos;
     if (!s->must_flush &&
-        offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
+        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 &&
@@ -159,12 +160,12 @@ int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence)
     } else {
         int64_t res = AVERROR(EPIPE);
 
-#if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK)
+#if CONFIG_MUXERS || CONFIG_NETWORK
         if (s->write_flag) {
             flush_buffer(s);
             s->must_flush = 1;
         }
-#endif /* defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK) */
+#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
         if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
             return res;
         if (!s->write_flag)
@@ -322,17 +323,23 @@ static void fill_buffer(ByteIOContext *s)
     }
 }
 
-unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len){
+unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
+                                    unsigned int len)
+{
     return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
 }
 
-unsigned long get_checksum(ByteIOContext *s){
+unsigned long get_checksum(ByteIOContext *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, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum){
+void init_checksum(ByteIOContext *s,
+                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
+                   unsigned long checksum)
+{
     s->update_checksum= update_checksum;
     if(s->update_checksum){
         s->checksum= checksum;
@@ -407,6 +414,10 @@ int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
             size -= len;
         }
     }
+    if (size1 == size) {
+        if (url_ferror(s)) return url_ferror(s);
+        if (url_feof(s))   return AVERROR_EOF;
+    }
     return size1 - size;
 }
 
@@ -426,6 +437,10 @@ int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
         len = 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;
+    }
     return len;
 }
 
@@ -571,11 +586,19 @@ int url_setbufsize(ByteIOContext *s, int buf_size)
     return 0;
 }
 
+#if LIBAVFORMAT_VERSION_MAJOR < 53
 int url_resetbuf(ByteIOContext *s, int flags)
+#else
+static int url_resetbuf(ByteIOContext *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))
         return AVERROR(EINVAL);
+#else
+    assert(flags == URL_WRONLY || flags == URL_RDONLY);
+#endif
 
     if (flags & URL_WRONLY) {
         s->buf_end = s->buffer + s->buffer_size;
@@ -617,7 +640,7 @@ URLContext *url_fileno(ByteIOContext *s)
     return s->opaque;
 }
 
-#ifdef CONFIG_MUXERS
+#if CONFIG_MUXERS
 int url_fprintf(ByteIOContext *s, const char *fmt, ...)
 {
     va_list ap;
@@ -665,8 +688,8 @@ int av_url_read_fpause(ByteIOContext *s, int pause)
     return s->read_pause(s->opaque, pause);
 }
 
-int64_t av_url_read_fseek(ByteIOContext *s,
-        int stream_index, int64_t timestamp, int flags)
+int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
+                          int64_t timestamp, int flags)
 {
     URLContext *h = s->opaque;
     int64_t ret;
@@ -681,8 +704,8 @@ int64_t av_url_read_fseek(ByteIOContext *s,
 }
 
 /* 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 not set. */
-#if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK)
+ * 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)
 {
@@ -733,7 +756,7 @@ static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
     if (new_allocated_size > d->allocated_size) {
         d->buffer = av_realloc(d->buffer, new_allocated_size);
         if(d->buffer == NULL)
-             return -1234;
+             return AVERROR(ENOMEM);
         d->allocated_size = new_allocated_size;
     }
     memcpy(d->buffer + d->pos, buf, buf_size);
@@ -749,10 +772,7 @@ static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
     int ret;
 
     /* packetized write: output the header */
-    buf1[0] = (buf_size >> 24);
-    buf1[1] = (buf_size >> 16);
-    buf1[2] = (buf_size >> 8);
-    buf1[3] = (buf_size);
+    AV_WB32(buf1, buf_size);
     ret= dyn_buf_write(opaque, buf1, 4);
     if(ret < 0)
         return ret;
@@ -778,28 +798,20 @@ static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
 static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
 {
     DynBuffer *d;
-    int io_buffer_size, ret;
-
-    if (max_packet_size)
-        io_buffer_size = max_packet_size;
-    else
-        io_buffer_size = 1024;
+    int ret;
+    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
 
     if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
         return -1;
-    d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
+    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
     if (!d)
-        return -1;
+        return AVERROR(ENOMEM);
     *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,
                         1, d, NULL,
                         max_packet_size ? dyn_packet_buf_write : dyn_buf_write,