The branch, master has been updated
       via  ee623a43e366e998e09da726dda2ba8f065ce224 (commit)
      from  c732564d2e0e9c4c285126f04bb9a81bdaf845f2 (commit)


- Log -----------------------------------------------------------------
commit ee623a43e366e998e09da726dda2ba8f065ce224
Author:     James Almer <[email protected]>
AuthorDate: Sat Nov 15 15:21:25 2025 -0300
Commit:     James Almer <[email protected]>
CommitDate: Mon Nov 24 12:07:15 2025 -0300

    avformat: don't return EIO on demuxer errors
    
    Demuxers should not generate this error code when they encounter truncated
    or otherwise invalid files. It's a code the underlying protocol should 
generate
    when there are legitimate reading errors.
    
    Signed-off-by: James Almer <[email protected]>

diff --git a/libavformat/4xm.c b/libavformat/4xm.c
index 218ea837c5..d2442f3160 100644
--- a/libavformat/4xm.c
+++ b/libavformat/4xm.c
@@ -32,6 +32,7 @@
 #include "libavutil/mem.h"
 #include "libavcodec/internal.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -239,9 +240,10 @@ static int fourxm_read_header(AVFormatContext *s)
     header = av_malloc(header_size);
     if (!header)
         return AVERROR(ENOMEM);
-    if (avio_read(pb, header, header_size) != header_size) {
+    ret = ffio_read_size(pb, header, header_size);
+    if (ret < 0) {
         av_free(header);
-        return AVERROR(EIO);
+        return ret;
     }
 
     /* take the lazy approach and search for any and all vtrk and strk chunks 
*/
@@ -312,7 +314,7 @@ static int fourxm_read_packet(AVFormatContext *s,
         fourcc_tag = AV_RL32(&header[0]);
         size       = AV_RL32(&header[4]);
         if (avio_feof(pb))
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         switch (fourcc_tag) {
         case LIST_TAG:
             /* this is a good time to bump the video pts */
diff --git a/libavformat/aacdec.c b/libavformat/aacdec.c
index 38ac9dcbe7..fef3c69f0b 100644
--- a/libavformat/aacdec.c
+++ b/libavformat/aacdec.c
@@ -175,7 +175,7 @@ retry:
         return ret;
 
     if (ret < ADTS_HEADER_SIZE)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     if ((AV_RB16(pkt->data) >> 4) != 0xfff) {
         // Parse all the ID3 headers between frames
@@ -184,7 +184,7 @@ retry:
         av_assert2(append > 0);
         ret = av_append_packet(s->pb, pkt, append);
         if (ret != append)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) {
             av_packet_unref(pkt);
             ret = adts_aac_resync(s);
diff --git a/libavformat/aaxdec.c b/libavformat/aaxdec.c
index 40a088a35b..9926be0f4b 100644
--- a/libavformat/aaxdec.c
+++ b/libavformat/aaxdec.c
@@ -345,9 +345,10 @@ static int aax_read_packet(AVFormatContext *s, AVPacket 
*pkt)
             extradata = av_malloc(extradata_size + 
AV_INPUT_BUFFER_PADDING_SIZE);
             if (!extradata)
                 return AVERROR(ENOMEM);
-            if (avio_read(pb, extradata, extradata_size) != extradata_size) {
+            ret = ffio_read_size(pb, extradata, extradata_size);
+            if (ret < 0) {
                 av_free(extradata);
-                return AVERROR(EIO);
+                return ret;
             }
             memset(extradata + extradata_size, 0, 
AV_INPUT_BUFFER_PADDING_SIZE);
         }
@@ -356,7 +357,7 @@ static int aax_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     ret = av_get_packet(pb, pkt, size);
     if (ret != size) {
         av_free(extradata);
-        return ret < 0 ? ret : AVERROR(EIO);
+        return ret < 0 ? ret : AVERROR_INVALIDDATA;
     }
     pkt->duration = 1;
     pkt->stream_index = 0;
diff --git a/libavformat/adxdec.c b/libavformat/adxdec.c
index 00b652315f..b9936799f9 100644
--- a/libavformat/adxdec.c
+++ b/libavformat/adxdec.c
@@ -75,7 +75,7 @@ static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
         av_shrink_packet(pkt, size);
         pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
     } else if (ret < size) {
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     } else {
         size = ret;
     }
diff --git a/libavformat/aiffdec.c b/libavformat/aiffdec.c
index d9d580bdb5..ff47d8dc7b 100644
--- a/libavformat/aiffdec.c
+++ b/libavformat/aiffdec.c
@@ -60,7 +60,7 @@ static int64_t get_tag(AVIOContext *pb, uint32_t * tag)
     int64_t size;
 
     if (avio_feof(pb))
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     *tag = avio_rl32(pb);
     size = avio_rb32(pb);
diff --git a/libavformat/alp.c b/libavformat/alp.c
index ad8e160223..18cc636296 100644
--- a/libavformat/alp.c
+++ b/libavformat/alp.c
@@ -24,6 +24,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "mux.h"
@@ -90,10 +91,8 @@ static int alp_read_header(AVFormatContext *s)
         return AVERROR_INVALIDDATA;
     }
 
-    if ((ret = avio_read(s->pb, hdr->adpcm, sizeof(hdr->adpcm))) < 0)
+    if ((ret = ffio_read_size(s->pb, hdr->adpcm, sizeof(hdr->adpcm))) < 0)
         return ret;
-    else if (ret != sizeof(hdr->adpcm))
-        return AVERROR(EIO);
 
     if (strncmp("ADPCM", hdr->adpcm, sizeof(hdr->adpcm)) != 0)
         return AVERROR_INVALIDDATA;
diff --git a/libavformat/apc.c b/libavformat/apc.c
index d24f57d021..da3d80bb9e 100644
--- a/libavformat/apc.c
+++ b/libavformat/apc.c
@@ -73,8 +73,11 @@ static int apc_read_header(AVFormatContext *s)
 
 static int apc_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    if (av_get_packet(s->pb, pkt, MAX_READ_SIZE) <= 0)
-        return AVERROR(EIO);
+    int ret = av_get_packet(s->pb, pkt, MAX_READ_SIZE);
+    if (ret < 0)
+        return ret;
+    else if (ret == 0)
+        return AVERROR_INVALIDDATA;
     pkt->stream_index = 0;
     return 0;
 }
diff --git a/libavformat/ape.c b/libavformat/ape.c
index f86ca5e894..7e6bf12961 100644
--- a/libavformat/ape.c
+++ b/libavformat/ape.c
@@ -395,7 +395,7 @@ static int ape_read_packet(AVFormatContext * s, AVPacket * 
pkt)
         av_log(s, AV_LOG_ERROR, "invalid packet size: %8"PRId64"\n",
                ape->frames[ape->currentframe].size);
         ape->currentframe++;
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     ret = av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size);
diff --git a/libavformat/apm.c b/libavformat/apm.c
index b3716c1d80..76ae9fd844 100644
--- a/libavformat/apm.c
+++ b/libavformat/apm.c
@@ -23,6 +23,7 @@
 #include "config_components.h"
 
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "mux.h"
@@ -153,10 +154,8 @@ static int apm_read_header(AVFormatContext *s)
                                  (int64_t)par->sample_rate *
                                  par->bits_per_coded_sample;
 
-    if ((ret = avio_read(s->pb, buf, APM_FILE_EXTRADATA_SIZE)) < 0)
+    if ((ret = ffio_read_size(s->pb, buf, APM_FILE_EXTRADATA_SIZE)) < 0)
         return ret;
-    else if (ret != APM_FILE_EXTRADATA_SIZE)
-        return AVERROR(EIO);
 
     apm_parse_extradata(&extradata, buf);
 
diff --git a/libavformat/argo_asf.c b/libavformat/argo_asf.c
index e08f029f80..116367516a 100644
--- a/libavformat/argo_asf.c
+++ b/libavformat/argo_asf.c
@@ -24,6 +24,7 @@
 
 #include "libavutil/avstring.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "mux.h"
@@ -188,10 +189,8 @@ static int argo_asf_read_header(AVFormatContext *s)
     if (!(st = avformat_new_stream(s, NULL)))
         return AVERROR(ENOMEM);
 
-    if ((ret = avio_read(pb, buf, ASF_FILE_HEADER_SIZE)) < 0)
+    if ((ret = ffio_read_size(pb, buf, ASF_FILE_HEADER_SIZE)) < 0)
         return ret;
-    else if (ret != ASF_FILE_HEADER_SIZE)
-        return AVERROR(EIO);
 
     ff_argo_asf_parse_file_header(&asf->fhdr, buf);
 
@@ -205,10 +204,8 @@ static int argo_asf_read_header(AVFormatContext *s)
     if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 
0)
         return ret;
 
-    if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
+    if ((ret = ffio_read_size(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
         return ret;
-    else if (ret != ASF_CHUNK_HEADER_SIZE)
-        return AVERROR(EIO);
 
     ff_argo_asf_parse_chunk_header(&asf->ckhdr, buf);
 
diff --git a/libavformat/argo_cvg.c b/libavformat/argo_cvg.c
index 03ae6fa59e..932ec8b966 100644
--- a/libavformat/argo_cvg.c
+++ b/libavformat/argo_cvg.c
@@ -25,6 +25,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "mux.h"
@@ -139,10 +140,8 @@ static int argo_cvg_read_checksum(AVIOContext *pb, const 
ArgoCVGHeader *cvg, uin
         return ret;
 
     /* NB: Not using avio_rl32() because no error checking. */
-    if ((ret = avio_read(pb, buf, sizeof(buf))) < 0)
+    if ((ret = ffio_read_size(pb, buf, sizeof(buf))) < 0)
         return ret;
-    else if (ret != sizeof(buf))
-        return AVERROR(EIO);
 
     if ((ret = avio_seek(pb, ARGO_CVG_HEADER_SIZE, SEEK_SET)) < 0)
         return ret;
@@ -163,10 +162,8 @@ static int argo_cvg_read_header(AVFormatContext *s)
     if (!(st = avformat_new_stream(s, NULL)))
         return AVERROR(ENOMEM);
 
-    if ((ret = avio_read(s->pb, buf, ARGO_CVG_HEADER_SIZE)) < 0)
+    if ((ret = ffio_read_size(s->pb, buf, ARGO_CVG_HEADER_SIZE)) < 0)
         return ret;
-    else if (ret != ARGO_CVG_HEADER_SIZE)
-        return AVERROR(EIO);
 
     ctx->header.size   = AV_RL32(buf + 0);
     ctx->header.loop   = AV_RL32(buf + 4);
diff --git a/libavformat/avs.c b/libavformat/avs.c
index 3cd814836b..84cd1267e6 100644
--- a/libavformat/avs.c
+++ b/libavformat/avs.c
@@ -26,6 +26,7 @@
  */
 
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "voc.h"
 
@@ -113,10 +114,9 @@ avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
     pkt->data[palette_size + 1] = type;
     pkt->data[palette_size + 2] = size & 0xFF;
     pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
-    ret = avio_read(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
-    if (ret < size) {
-        return AVERROR(EIO);
-    }
+    ret = ffio_read_size(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
+    if (ret < 0)
+        return ret;
 
     pkt->size = ret + palette_size;
     pkt->stream_index = avs->st_video->index;
@@ -168,7 +168,7 @@ static int avs_read_packet(AVFormatContext * s, AVPacket * 
pkt)
     while (1) {
         if (avs->remaining_frame_size <= 0) {
             if (!avio_rl16(s->pb))    /* found EOF */
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
             avs->remaining_frame_size = avio_rl16(s->pb) - 4;
         }
 
@@ -184,9 +184,9 @@ static int avs_read_packet(AVFormatContext * s, AVPacket * 
pkt)
             case AVS_PALETTE:
                 if (size - 4 > sizeof(palette))
                     return AVERROR_INVALIDDATA;
-                ret = avio_read(s->pb, palette, size - 4);
-                if (ret < size - 4)
-                    return AVERROR(EIO);
+                ret = ffio_read_size(s->pb, palette, size - 4);
+                if (ret < 0)
+                    return ret;
                 palette_size = size;
                 break;
 
diff --git a/libavformat/bethsoftvid.c b/libavformat/bethsoftvid.c
index e3c4758f30..f118f668f1 100644
--- a/libavformat/bethsoftvid.c
+++ b/libavformat/bethsoftvid.c
@@ -32,6 +32,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "libavcodec/bethsoftvideo.h"
@@ -147,10 +148,9 @@ static int read_frame(BVID_DemuxContext *vid, AVIOContext 
*pb, AVPacket *pkt,
 
     // set the y offset if it exists (decoder header data should be in data 
section)
     if(block_type == VIDEO_YOFF_P_FRAME){
-        if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
-            ret = AVERROR(EIO);
+        ret = ffio_read_size(pb, &vidbuf_start[vidbuf_nbytes], 2);
+        if (ret < 0)
             goto fail;
-        }
         vidbuf_nbytes += 2;
     }
 
@@ -170,10 +170,9 @@ static int read_frame(BVID_DemuxContext *vid, AVIOContext 
*pb, AVPacket *pkt,
             if(block_type == VIDEO_I_FRAME)
                 vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
         } else if(code){ // plain sequence
-            if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
-                ret = AVERROR(EIO);
+            ret = ffio_read_size(pb, &vidbuf_start[vidbuf_nbytes], code);
+            if (ret < 0)
                 goto fail;
-            }
             vidbuf_nbytes += code;
         }
         bytes_copied += code & 0x7F;
@@ -238,9 +237,9 @@ static int vid_read_packet(AVFormatContext *s,
                 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
                 vid->has_palette = 0;
             }
-            if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != 
BVID_PALETTE_SIZE) {
-                return AVERROR(EIO);
-            }
+            ret_value = ffio_read_size(pb, vid->palette, BVID_PALETTE_SIZE);
+            if (ret_value < 0)
+                return ret_value;
             vid->has_palette = 1;
             return vid_read_packet(s, pkt);
 
@@ -268,7 +267,7 @@ static int vid_read_packet(AVFormatContext *s,
                 if (ret_value < 0)
                     return ret_value;
                 av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
             }
             pkt->stream_index = vid->audio_index;
             pkt->duration     = audio_length;
@@ -284,7 +283,7 @@ static int vid_read_packet(AVFormatContext *s,
             if(vid->nframes != 0)
                 av_log(s, AV_LOG_VERBOSE, "reached terminating character but 
not all frames read.\n");
             vid->is_finished = 1;
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         default:
             av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = 
%d, hex = %x)!!!\n",
                    block_type, block_type, block_type);
diff --git a/libavformat/bfi.c b/libavformat/bfi.c
index 06bf5d2c17..e6ae726404 100644
--- a/libavformat/bfi.c
+++ b/libavformat/bfi.c
@@ -131,7 +131,7 @@ static int bfi_read_packet(AVFormatContext * s, AVPacket * 
pkt)
         uint32_t state = 0;
         while(state != MKTAG('S','A','V','I')){
             if (avio_feof(pb))
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
             state = 256*state + avio_r8(pb);
         }
         /* Now that the chunk's location is confirmed, we proceed... */
diff --git a/libavformat/bink.c b/libavformat/bink.c
index 0632d390a2..18eaeba738 100644
--- a/libavformat/bink.c
+++ b/libavformat/bink.c
@@ -120,13 +120,13 @@ static int read_header(AVFormatContext *s)
 
     if (vst->duration > 1000000) {
         av_log(s, AV_LOG_ERROR, "invalid header: more than 1000000 frames\n");
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     if (avio_rl32(pb) > bink->file_size) {
         av_log(s, AV_LOG_ERROR,
                "invalid header: largest frame size greater than file size\n");
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     avio_skip(pb, 4);
@@ -140,7 +140,7 @@ static int read_header(AVFormatContext *s)
         av_log(s, AV_LOG_ERROR,
                "invalid header: invalid fps (%"PRIu32"/%"PRIu32")\n",
                fps_num, fps_den);
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
     avpriv_set_pts_info(vst, 64, fps_den, fps_num);
     vst->avg_frame_rate = av_inv_q(vst->time_base);
@@ -162,7 +162,7 @@ static int read_header(AVFormatContext *s)
         av_log(s, AV_LOG_ERROR,
                "invalid header: more than 
"AV_STRINGIFY(BINK_MAX_AUDIO_TRACKS)" audio tracks (%"PRIu32")\n",
                bink->num_audio_tracks);
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     signature = (vst->codecpar->codec_tag & 0xFFFFFF);
@@ -217,7 +217,7 @@ static int read_header(AVFormatContext *s)
 
         if (next_pos <= pos) {
             av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
         if ((ret = av_add_index_entry(vst, pos, i, next_pos - pos, 0,
                                       keyframe ? AVINDEX_KEYFRAME : 0)) < 0)
@@ -253,7 +253,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
             av_log(s, AV_LOG_ERROR,
                    "could not find index entry for frame %"PRId64"\n",
                    bink->video_pts);
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
 
         bink->remain_packet_size = sti->index_entries[index_entry].size;
@@ -267,7 +267,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
             av_log(s, AV_LOG_ERROR,
                    "frame %"PRId64": audio size in header (%"PRIu32") > size 
of packet left (%"PRIu32")\n",
                    bink->video_pts, audio_size, bink->remain_packet_size);
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
         bink->remain_packet_size -= 4 + audio_size;
         bink->current_track++;
diff --git a/libavformat/binka.c b/libavformat/binka.c
index cc5f2555ca..df853890c1 100644
--- a/libavformat/binka.c
+++ b/libavformat/binka.c
@@ -75,7 +75,7 @@ static int binka_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     avio_skip(pb, 2);
     pkt_size = avio_rl16(pb) + 4;
     if (pkt_size <= 4)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     ret = av_new_packet(pkt, pkt_size);
     if (ret < 0)
         return ret;
diff --git a/libavformat/bintext.c b/libavformat/bintext.c
index c96c14ccd9..5439323cb3 100644
--- a/libavformat/bintext.c
+++ b/libavformat/bintext.c
@@ -36,6 +36,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "sauce.h"
@@ -99,8 +100,8 @@ static int next_tag_read(AVFormatContext *avctx, uint64_t 
*fsize)
         return AVERROR_INVALIDDATA;
 
     avio_seek(pb, start_pos - 256, SEEK_SET);
-    if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
-        return -1;
+    if ((len = ffio_read_size(pb, buf, sizeof(next_magic))) < 0)
+        return len;
     if (memcmp(buf, next_magic, sizeof(next_magic)))
         return -1;
     if (avio_r8(pb) != 0x01)
@@ -244,8 +245,8 @@ static int xbin_read_header(AVFormatContext *s)
         return ret;
     st->codecpar->extradata[0] = fontheight;
     st->codecpar->extradata[1] = flags;
-    if (avio_read(pb, st->codecpar->extradata + 2, 
st->codecpar->extradata_size - 2) < 0)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2, 
st->codecpar->extradata_size - 2)) < 0)
+        return ret;
 
     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
         int64_t fsize =  avio_size(pb);
@@ -281,13 +282,13 @@ static int adf_read_header(AVFormatContext *s)
     st->codecpar->extradata[0] = 16;
     st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
 
-    if (avio_read(pb, st->codecpar->extradata + 2, 24) < 0)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2, 24)) < 0)
+        return ret;
     avio_skip(pb, 144);
-    if (avio_read(pb, st->codecpar->extradata + 2 + 24, 24) < 0)
-        return AVERROR(EIO);
-    if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2 + 24, 24)) < 0)
+        return ret;
+    if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2 + 48, 4096)) < 0)
+        return ret;
 
     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
         int got_width = 0;
@@ -330,7 +331,7 @@ static int idf_read_header(AVFormatContext *s)
     int64_t fsize;
 
     if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     st = init_stream(s);
     if (!st)
@@ -349,10 +350,10 @@ static int idf_read_header(AVFormatContext *s)
 
     avio_seek(pb, bin->fsize + 12, SEEK_SET);
 
-    if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
-        return AVERROR(EIO);
-    if (avio_read(pb, st->codecpar->extradata + 2, 48) < 0)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2 + 48, 4096)) < 0)
+        return ret;
+    if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2, 48)) < 0)
+        return ret;
 
     ff_sauce_read(s, &bin->fsize, &got_width, 0);
     if (st->codecpar->width < 8)
@@ -371,15 +372,15 @@ static int read_packet(AVFormatContext *s,
 
     if (bin->fsize > 0) {
         if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         bin->fsize = -1; /* done */
     } else if (!bin->fsize) {
         if (avio_feof(s->pb))
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
     } else {
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     pkt->flags |= AV_PKT_FLAG_KEY;
diff --git a/libavformat/bit.c b/libavformat/bit.c
index 5c3eb31c57..1f3dc31f38 100644
--- a/libavformat/bit.c
+++ b/libavformat/bit.c
@@ -22,6 +22,7 @@
 #include "config_components.h"
 
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "mux.h"
@@ -93,11 +94,9 @@ static int read_packet(AVFormatContext *s,
     if(packet_size > MAX_FRAME_SIZE)
         return AVERROR_INVALIDDATA;
 
-    ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
+    ret = ffio_read_size(pb, (uint8_t*)buf, (8 * packet_size) * 
sizeof(uint16_t));
     if(ret<0)
         return ret;
-    if(ret != 8 * packet_size * sizeof(uint16_t))
-        return AVERROR(EIO);
 
     if ((ret = av_new_packet(pkt, packet_size)) < 0)
         return ret;
diff --git a/libavformat/bmv.c b/libavformat/bmv.c
index 84ab2aac5a..db2b4076c0 100644
--- a/libavformat/bmv.c
+++ b/libavformat/bmv.c
@@ -22,6 +22,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/mem.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -88,8 +89,8 @@ static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
         if ((err = av_reallocp(&c->packet, c->size + 1)) < 0)
             return err;
         c->packet[0] = type;
-        if (avio_read(s->pb, c->packet + 1, c->size) != c->size)
-            return AVERROR(EIO);
+        if ((err = ffio_read_size(s->pb, c->packet + 1, c->size)) < 0)
+            return err;
         if (type & BMV_AUDIO) {
             int audio_size = c->packet[1] * 65 + 1;
             if (audio_size >= c->size) {
diff --git a/libavformat/brstm.c b/libavformat/brstm.c
index d29004155b..3fe19fff72 100644
--- a/libavformat/brstm.c
+++ b/libavformat/brstm.c
@@ -23,6 +23,7 @@
 #include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -425,11 +426,11 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
                                     (b->current_block - 1), 4 * channels);
 
         for (i = 0; i < channels; i++) {
-            ret = avio_read(s->pb, dst, size);
+            ret = ffio_read_size(s->pb, dst, size);
             dst += size;
             avio_skip(s->pb, skip);
-            if (ret != size) {
-                return AVERROR(EIO);
+            if (ret < 0) {
+                return ret;
             }
         }
         pkt->duration = samples;
@@ -441,7 +442,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->stream_index = 0;
 
     if (ret != size)
-        ret = AVERROR(EIO);
+        ret = AVERROR_INVALIDDATA;
 
     return ret;
 }
diff --git a/libavformat/c93.c b/libavformat/c93.c
index 933fe4a99e..1fbc093612 100644
--- a/libavformat/c93.c
+++ b/libavformat/c93.c
@@ -20,6 +20,7 @@
  */
 
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "voc.h"
@@ -157,9 +158,9 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->data[0] = 0;
     pkt->size = datasize + 1;
 
-    ret = avio_read(pb, pkt->data + 1, datasize);
-    if (ret < datasize) {
-        return AVERROR(EIO);
+    ret = ffio_read_size(pb, pkt->data + 1, datasize);
+    if (ret < 0) {
+        return ret;
     }
 
     datasize = avio_rl16(pb); /* palette size */
@@ -169,9 +170,9 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
             return AVERROR_INVALIDDATA;
         }
         pkt->data[0] |= C93_HAS_PALETTE;
-        ret = avio_read(pb, pkt->data + pkt->size, datasize);
-        if (ret < datasize) {
-            return AVERROR(EIO);
+        ret = ffio_read_size(pb, pkt->data + pkt->size, datasize);
+        if (ret < 0) {
+            return ret;
         }
         pkt->size += 768;
     }
diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c
index 5d7dbe8f41..99ae041364 100644
--- a/libavformat/cafdec.c
+++ b/libavformat/cafdec.c
@@ -28,6 +28,7 @@
 #include <inttypes.h>
 
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "isom.h"
@@ -142,9 +143,9 @@ static int read_kuki_chunk(AVFormatContext *s, int64_t size)
             avio_skip(pb, size);
             return AVERROR_INVALIDDATA;
         }
-        if (avio_read(pb, preamble, ALAC_PREAMBLE) != ALAC_PREAMBLE) {
+        if ((ret = ffio_read_size(pb, preamble, ALAC_PREAMBLE)) < 0) {
             av_log(s, AV_LOG_ERROR, "failed to read preamble\n");
-            return AVERROR_INVALIDDATA;
+            return ret;
         }
 
         if ((ret = ff_alloc_extradata(st->codecpar, ALAC_HEADER)) < 0)
@@ -443,7 +444,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
         if (!left)
             return AVERROR_EOF;
         if (left < 0)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
     }
 
     pkt_frames = caf->frames_per_packet;
@@ -461,12 +462,12 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
             pkt_size   = caf->num_bytes - 
sti->index_entries[caf->packet_cnt].pos;
             pkt_frames = st->duration   - 
sti->index_entries[caf->packet_cnt].timestamp;
         } else {
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
     }
 
     if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     res = av_get_packet(pb, pkt, pkt_size);
     if (res < 0)
diff --git a/libavformat/cinedec.c b/libavformat/cinedec.c
index cd13f132c3..7bbf198b19 100644
--- a/libavformat/cinedec.c
+++ b/libavformat/cinedec.c
@@ -354,7 +354,7 @@ static int cine_read_seek(AVFormatContext *avctx, int 
stream_index, int64_t time
         return AVERROR(ENOSYS);
 
     if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
-        return AVERROR(EIO);
+        return AVERROR(ENOSYS);
 
     cine->pts = timestamp;
     return 0;
diff --git a/libavformat/dfa.c b/libavformat/dfa.c
index 1d78c348b1..580e3ddd24 100644
--- a/libavformat/dfa.c
+++ b/libavformat/dfa.c
@@ -89,7 +89,7 @@ static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
         return AVERROR_EOF;
 
     if (av_get_packet(pb, pkt, 12) != 12)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     while (!avio_feof(pb)) {
         if (!first) {
             ret = av_append_packet(pb, pkt, 12);
@@ -101,7 +101,7 @@ static int dfa_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         frame_size = AV_RL32(pkt->data + pkt->size - 8);
         if (frame_size > INT_MAX - 4) {
             av_log(s, AV_LOG_ERROR, "Too large chunk size: %"PRIu32"\n", 
frame_size);
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
         if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
             if (frame_size) {
diff --git a/libavformat/dsicin.c b/libavformat/dsicin.c
index 6eff38e010..3c325f7cfd 100644
--- a/libavformat/dsicin.c
+++ b/libavformat/dsicin.c
@@ -153,7 +153,7 @@ static int cin_read_frame_header(CinDemuxContext *cin, 
AVIOContext *pb) {
     hdr->audio_frame_size = avio_rl32(pb);
 
     if (avio_feof(pb) || pb->error)
-        return AVERROR(EIO);
+        return pb->error ? pb->error : AVERROR_INVALIDDATA;
 
     if (avio_rl32(pb) != 0xAA55AA55)
         return AVERROR_INVALIDDATA;
diff --git a/libavformat/dss.c b/libavformat/dss.c
index 47c8f49d67..6cabdb5421 100644
--- a/libavformat/dss.c
+++ b/libavformat/dss.c
@@ -116,6 +116,7 @@ static int dss_read_header(AVFormatContext *s)
     DSSDemuxContext *ctx = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *st;
+    int64_t ret64;
     int ret, version;
 
     st = avformat_new_stream(s, NULL);
@@ -164,8 +165,8 @@ static int dss_read_header(AVFormatContext *s)
 
     /* Jump over header */
 
-    if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
-        return AVERROR(EIO);
+    if ((ret64 = avio_seek(pb, ctx->dss_header_size, SEEK_SET)) < 0)
+        return (int)ret64;
 
     ctx->counter = 0;
     ctx->swap    = 0;
diff --git a/libavformat/dv.c b/libavformat/dv.c
index 8af0d5a652..84677284bd 100644
--- a/libavformat/dv.c
+++ b/libavformat/dv.c
@@ -33,6 +33,7 @@
 
 #include <time.h>
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "libavcodec/dv_profile.h"
@@ -576,6 +577,7 @@ static int dv_read_header(AVFormatContext *s)
 {
     unsigned state, marker_pos = 0;
     RawDVContext *c = s->priv_data;
+    int64_t ret64;
     int ret;
 
     if ((ret = dv_init_demux(s, &c->dv_demux)) < 0)
@@ -598,10 +600,10 @@ static int dv_read_header(AVFormatContext *s)
     }
     AV_WB32(c->buf, state);
 
-    if (avio_read(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) != DV_PROFILE_BYTES 
- 4 ||
-        avio_seek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0) {
-        return AVERROR(EIO);
-    }
+    if ((ret = ffio_read_size(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4)) < 0)
+        return ret;
+    if ((ret64 = avio_seek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR)) < 0)
+        return (int)ret64;
 
     c->dv_demux.sys = av_dv_frame_profile(c->dv_demux.sys,
                                            c->buf,
@@ -633,13 +635,13 @@ static int dv_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         int ret;
         int64_t pos = avio_tell(s->pb);
         if (!c->dv_demux.sys)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         size = c->dv_demux.sys->frame_size;
         ret = avio_read(s->pb, c->buf, size);
         if (ret < 0) {
             return ret;
         } else if (ret == 0) {
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
 
         size = avpriv_dv_produce_packet(&c->dv_demux, pkt, c->buf, size, pos);
diff --git a/libavformat/dxa.c b/libavformat/dxa.c
index 56b19a7fca..76bc7a543d 100644
--- a/libavformat/dxa.c
+++ b/libavformat/dxa.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "riff.h"
@@ -170,7 +171,7 @@ static int dxa_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         ret = av_get_packet(s->pb, pkt, size);
         pkt->stream_index = 1;
         if(ret != size)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         c->bytes_left -= size;
         c->wavpos = avio_tell(s->pb);
         return 0;
@@ -214,10 +215,9 @@ static int dxa_read_packet(AVFormatContext *s, AVPacket 
*pkt)
             if (ret < 0)
                 return ret;
             memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
-            ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, 
size);
-            if(ret != size){
-                return AVERROR(EIO);
-            }
+            ret = ffio_read_size(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, 
size);
+            if (ret < 0)
+                return ret;
             if(pal_size) memcpy(pkt->data, pal, pal_size);
             pkt->stream_index = 0;
             c->frames--;
diff --git a/libavformat/electronicarts.c b/libavformat/electronicarts.c
index 04acf3a409..74a050fec6 100644
--- a/libavformat/electronicarts.c
+++ b/libavformat/electronicarts.c
@@ -537,7 +537,7 @@ static int ea_read_header(AVFormatContext *s)
     AVStream *st;
 
     if (process_ea_header(s)<=0)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     if (init_video_stream(s, &ea->video) || init_video_stream(s, &ea->alpha))
         return AVERROR(ENOMEM);
diff --git a/libavformat/filmstripdec.c b/libavformat/filmstripdec.c
index 5ce0af234c..1a3f45f61c 100644
--- a/libavformat/filmstripdec.c
+++ b/libavformat/filmstripdec.c
@@ -43,7 +43,7 @@ static int read_header(AVFormatContext *s)
     AVStream *st;
 
     if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
-        return AVERROR(EIO);
+        return AVERROR(ENOSYS);
 
     avio_seek(pb, avio_size(pb) - 36, SEEK_SET);
     if (avio_rb32(pb) != RAND_TAG) {
diff --git a/libavformat/flic.c b/libavformat/flic.c
index 41dfb4f39e..01cd4698cf 100644
--- a/libavformat/flic.c
+++ b/libavformat/flic.c
@@ -34,6 +34,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -97,8 +98,8 @@ static int flic_read_header(AVFormatContext *s)
     flic->frame_number = 0;
 
     /* load the whole header and pull out the width and height */
-    if (avio_read(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, header, FLIC_HEADER_SIZE)) < 0)
+        return ret;
 
     magic_number = AV_RL16(&header[4]);
     speed = AV_RL32(&header[0x10]);
@@ -131,9 +132,9 @@ static int flic_read_header(AVFormatContext *s)
     memcpy(st->codecpar->extradata, header, FLIC_HEADER_SIZE);
 
     /* peek at the preamble to detect TFTD videos - they seem to always start 
with an audio chunk */
-    if (avio_read(pb, preamble, FLIC_PREAMBLE_SIZE) != FLIC_PREAMBLE_SIZE) {
+    if ((ret = ffio_read_size(pb, preamble, FLIC_PREAMBLE_SIZE)) < 0) {
         av_log(s, AV_LOG_ERROR, "Failed to peek at preamble\n");
-        return AVERROR(EIO);
+        return ret;
     }
 
     avio_seek(pb, -FLIC_PREAMBLE_SIZE, SEEK_CUR);
@@ -206,11 +207,8 @@ static int flic_read_packet(AVFormatContext *s,
 
     while (!packet_read && !avio_feof(pb)) {
 
-        if ((ret = avio_read(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
-            FLIC_PREAMBLE_SIZE) {
-            ret = AVERROR(EIO);
+        if ((ret = ffio_read_size(pb, preamble, FLIC_PREAMBLE_SIZE)) < 0)
             break;
-        }
 
         size = AV_RL32(&preamble[0]);
         magic = AV_RL16(&preamble[4]);
@@ -222,11 +220,8 @@ static int flic_read_packet(AVFormatContext *s,
             pkt->stream_index = flic->video_stream_index;
             pkt->pos = pos;
             memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
-            ret = avio_read(pb, pkt->data + FLIC_PREAMBLE_SIZE,
+            ret = ffio_read_size(pb, pkt->data + FLIC_PREAMBLE_SIZE,
                 size - FLIC_PREAMBLE_SIZE);
-            if (ret != size - FLIC_PREAMBLE_SIZE) {
-                ret = AVERROR(EIO);
-            }
             pkt->flags = flic->frame_number == 0 ? AV_PKT_FLAG_KEY : 0;
             pkt->pts = flic->frame_number;
             if (flic->frame_number == 0)
@@ -243,12 +238,10 @@ static int flic_read_packet(AVFormatContext *s,
             pkt->stream_index = flic->audio_stream_index;
             pkt->pos = pos;
             pkt->flags = AV_PKT_FLAG_KEY;
-            ret = avio_read(pb, pkt->data, size);
+            ret = ffio_read_size(pb, pkt->data, size);
 
-            if (ret != size) {
-                ret = AVERROR(EIO);
+            if (ret < 0)
                 break;
-            }
 
             packet_read = 1;
         } else {
diff --git a/libavformat/gifdec.c b/libavformat/gifdec.c
index d5f06adc64..28722dc50b 100644
--- a/libavformat/gifdec.c
+++ b/libavformat/gifdec.c
@@ -118,6 +118,7 @@ static int gif_read_header(AVFormatContext *s)
     AVStream        *st;
     int type, width, height, ret, n, flags;
     int64_t nb_frames = 0, duration = 0, pos;
+    int64_t ret64;
 
     if ((ret = resync(pb)) < 0)
         return ret;
@@ -215,8 +216,9 @@ static int gif_read_header(AVFormatContext *s)
 
 skip:
     /* jump to start because gif decoder needs header data too */
-    if (avio_seek(pb, pos - 6, SEEK_SET) != pos - 6)
-        return AVERROR(EIO);
+    ret64 = avio_seek(pb, pos - 6, SEEK_SET);
+    if (ret64 < 0)
+        return (int)ret64;
 
     /* GIF format operates with time in "hundredths of second",
      * therefore timebase is 1/100 */
diff --git a/libavformat/gsmdec.c b/libavformat/gsmdec.c
index 10fba212e9..649d67c009 100644
--- a/libavformat/gsmdec.c
+++ b/libavformat/gsmdec.c
@@ -63,7 +63,7 @@ static int gsm_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = av_get_packet(s->pb, pkt, size);
     if (ret < GSM_BLOCK_SIZE) {
-        return ret < 0 ? ret : AVERROR(EIO);
+        return ret < 0 ? ret : AVERROR_INVALIDDATA;
     }
     pkt->duration = 1;
     pkt->pts      = pkt->pos / GSM_BLOCK_SIZE;
diff --git a/libavformat/hca.c b/libavformat/hca.c
index 713082f8b0..e24a21e081 100644
--- a/libavformat/hca.c
+++ b/libavformat/hca.c
@@ -24,6 +24,7 @@
 #include "libavcodec/bytestream.h"
 
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -76,9 +77,9 @@ static int hca_read_header(AVFormatContext *s)
     if (ret < 0)
         return ret;
 
-    ret = avio_read(pb, par->extradata + 8, par->extradata_size - 8 - 10);
-    if (ret < par->extradata_size - 8 - 10)
-        return AVERROR(EIO);
+    ret = ffio_read_size(pb, par->extradata + 8, par->extradata_size - 8 - 10);
+    if (ret < 0)
+        return AVERROR_INVALIDDATA;
     AV_WL32(par->extradata, MKTAG('H', 'C', 'A', 0));
     AV_WB16(par->extradata + 4, version);
     AV_WB16(par->extradata + 6, data_offset);
diff --git a/libavformat/icoenc.c b/libavformat/icoenc.c
index 7a7d839d84..9eba9e7926 100644
--- a/libavformat/icoenc.c
+++ b/libavformat/icoenc.c
@@ -122,7 +122,7 @@ static int ico_write_packet(AVFormatContext *s, AVPacket 
*pkt)
 
     if (ico->current_image >= ico->nb_images) {
         av_log(s, AV_LOG_ERROR, "ICO already contains %d images\n", 
ico->current_image);
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     image = &ico->images[ico->current_image++];
diff --git a/libavformat/idcin.c b/libavformat/idcin.c
index 561715d3d9..e2064acac3 100644
--- a/libavformat/idcin.c
+++ b/libavformat/idcin.c
@@ -267,7 +267,7 @@ static int idcin_read_packet(AVFormatContext *s,
     if (idcin->next_chunk_is_video) {
         command = avio_rl32(pb);
         if (command == 2) {
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         } else if (command == 1) {
             /* trigger a palette change */
             ret = avio_read(pb, palette_buffer, 768);
@@ -275,7 +275,7 @@ static int idcin_read_packet(AVFormatContext *s,
                 return ret;
             } else if (ret != 768) {
                 av_log(s, AV_LOG_ERROR, "incomplete packet\n");
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
             }
             /* scale the palette as necessary */
             palette_scale = 2;
@@ -312,7 +312,7 @@ static int idcin_read_packet(AVFormatContext *s,
             return ret;
         else if (ret != chunk_size) {
             av_log(s, AV_LOG_ERROR, "incomplete packet\n");
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
         if (command == 1) {
             uint8_t *pal;
diff --git a/libavformat/idroqdec.c b/libavformat/idroqdec.c
index 67bc1246e6..9c3aaec260 100644
--- a/libavformat/idroqdec.c
+++ b/libavformat/idroqdec.c
@@ -74,11 +74,11 @@ static int roq_read_header(AVFormatContext *s)
     RoqDemuxContext *roq = s->priv_data;
     AVIOContext *pb = s->pb;
     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
+    int ret;
 
     /* get the main header */
-    if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
-        RoQ_CHUNK_PREAMBLE_SIZE)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) < 0)
+        return ret;
     roq->frame_rate = AV_RL16(&preamble[6]);
 
     /* init private context parameters */
@@ -111,9 +111,8 @@ static int roq_read_packet(AVFormatContext *s,
             return AVERROR_EOF;
 
         /* get the next chunk preamble */
-        if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
-            RoQ_CHUNK_PREAMBLE_SIZE)
-            return AVERROR(EIO);
+        if ((ret = ffio_read_size(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) < 0)
+            return ret;
 
         chunk_type = AV_RL16(&preamble[0]);
         chunk_size = AV_RL32(&preamble[2]);
@@ -135,8 +134,8 @@ static int roq_read_packet(AVFormatContext *s,
                 st->codecpar->codec_id     = AV_CODEC_ID_ROQ;
                 st->codecpar->codec_tag    = 0;  /* no fourcc */
 
-                if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != 
RoQ_CHUNK_PREAMBLE_SIZE)
-                    return AVERROR(EIO);
+                if ((ret = ffio_read_size(pb, preamble, 
RoQ_CHUNK_PREAMBLE_SIZE)) < 0)
+                    return ret;
                 st->codecpar->width  = roq->width  = AV_RL16(preamble);
                 st->codecpar->height = roq->height = AV_RL16(preamble + 2);
                 break;
@@ -152,9 +151,8 @@ static int roq_read_packet(AVFormatContext *s,
             codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
             codebook_size = chunk_size;
             avio_skip(pb, codebook_size);
-            if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
-                RoQ_CHUNK_PREAMBLE_SIZE)
-                return AVERROR(EIO);
+            if ((ret = ffio_read_size(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) 
< 0)
+                return ret;
             chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
                 codebook_size;
 
@@ -167,7 +165,7 @@ static int roq_read_packet(AVFormatContext *s,
             /* load up the packet */
             ret= av_get_packet(pb, pkt, chunk_size);
             if (ret != chunk_size)
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
             pkt->stream_index = roq->video_stream_index;
             pkt->pts = roq->video_pts++;
 
@@ -220,11 +218,10 @@ static int roq_read_packet(AVFormatContext *s,
             }
 
             pkt->pos= avio_tell(pb);
-            ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
+            ret = ffio_read_size(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
                 chunk_size);
-            if (ret != chunk_size) {
-                return AVERROR(EIO);
-            }
+            if (ret < 0)
+                return ret;
 
             packet_read = 1;
             break;
diff --git a/libavformat/iff.c b/libavformat/iff.c
index 44ba5a9023..fc40ef1aea 100644
--- a/libavformat/iff.c
+++ b/libavformat/iff.c
@@ -37,6 +37,7 @@
 #include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "id3v2.h"
 #include "internal.h"
@@ -135,13 +136,14 @@ static int get_metadata(AVFormatContext *s,
                         const unsigned data_size)
 {
     uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
+    int res;
 
     if (!buf)
         return AVERROR(ENOMEM);
 
-    if (avio_read(s->pb, buf, data_size) != data_size) {
+    if ((res = ffio_read_size(s->pb, buf, data_size)) < 0) {
         av_free(buf);
-        return AVERROR(EIO);
+        return res;
     }
     buf[data_size] = 0;
     av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
@@ -563,10 +565,10 @@ static int iff_read_header(AVFormatContext *s)
                                      data_size + IFF_EXTRA_VIDEO_SIZE);
             if (res < 0)
                 return res;
-            if (avio_read(pb, stv->codecpar->extradata + IFF_EXTRA_VIDEO_SIZE, 
data_size) < 0) {
+            if ((res = avio_read(pb, stv->codecpar->extradata + 
IFF_EXTRA_VIDEO_SIZE, data_size)) < 0) {
                 av_freep(&stv->codecpar->extradata);
                 stv->codecpar->extradata_size = 0;
-                return AVERROR(EIO);
+                return res;
             }
             break;
 
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index 789f8b94c9..1f7e0fcce1 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -400,13 +400,13 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
                 !s->loop &&
                 !s->split_planes) {
                 f[i] = s1->pb;
-            } else if (s1->io_open(s1, &f[i], filename.str, AVIO_FLAG_READ, 
NULL) < 0) {
+            } else if ((res = s1->io_open(s1, &f[i], filename.str, 
AVIO_FLAG_READ, NULL)) < 0) {
                 if (i >= 1)
                     break;
                 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
                        filename.str);
                 av_bprint_finalize(&filename, NULL);
-                return AVERROR(EIO);
+                return res;
             }
             size[i] = avio_size(f[i]);
 
@@ -466,7 +466,7 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
         struct stat img_stat;
         av_assert0(!s->is_pipe); // The ts_from_file option is not supported 
by piped input demuxers
         if (stat(filename.str, &img_stat)) {
-            res = AVERROR(EIO);
+            res = AVERROR(errno);
             goto fail;
         }
         pkt->pts = (int64_t)img_stat.st_mtime;
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index d14bc5ea3f..fb51151090 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -194,9 +194,8 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
                 goto fail;
             }
         }
-        if (s->io_open(s, &pb[i], tmp[i] ? tmp[i] : filename.str, 
AVIO_FLAG_WRITE, &options) < 0) {
+        if ((ret = s->io_open(s, &pb[i], tmp[i] ? tmp[i] : filename.str, 
AVIO_FLAG_WRITE, &options)) < 0) {
             av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", tmp[i] ? 
tmp[i] : filename.str);
-            ret = AVERROR(EIO);
             goto fail;
         }
         if (options) {
diff --git a/libavformat/ingenientdec.c b/libavformat/ingenientdec.c
index 63624372a6..64b5e8a407 100644
--- a/libavformat/ingenientdec.c
+++ b/libavformat/ingenientdec.c
@@ -39,7 +39,7 @@ static int ingenient_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     int ret, size, w, h, unk1, unk2;
 
     if (avio_rl32(s->pb) != MKTAG('M', 'J', 'P', 'G'))
-        return AVERROR(EIO); // FIXME
+        return AVERROR_INVALIDDATA; // FIXME
 
     size = avio_rl32(s->pb);
 
diff --git a/libavformat/ipmovie.c b/libavformat/ipmovie.c
index 5a8abde842..1ef357e088 100644
--- a/libavformat/ipmovie.c
+++ b/libavformat/ipmovie.c
@@ -639,9 +639,8 @@ static int ipmovie_read_header(AVFormatContext *s)
 
     /* peek ahead to the next chunk-- if it is an init audio chunk, process
      * it; if it is the first video chunk, this is a silent file */
-    if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
-        CHUNK_PREAMBLE_SIZE)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE)) < 0)
+        return ret;
     chunk_type = AV_RL16(&chunk_preamble[2]);
     avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
 
@@ -688,7 +687,7 @@ static int ipmovie_read_packet(AVFormatContext *s,
         if (ret == CHUNK_BAD)
             ret = AVERROR_INVALIDDATA;
         else if (ret == CHUNK_EOF)
-            ret = AVERROR(EIO);
+            ret = AVERROR_INVALIDDATA;
         else if (ret == CHUNK_NOMEM)
             ret = AVERROR(ENOMEM);
         else if (ret == CHUNK_END || ret == CHUNK_SHUTDOWN)
diff --git a/libavformat/iss.c b/libavformat/iss.c
index 7a68fcaf63..b0e17994d4 100644
--- a/libavformat/iss.c
+++ b/libavformat/iss.c
@@ -136,7 +136,7 @@ static int iss_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     int ret = av_get_packet(s->pb, pkt, iss->packet_size);
 
     if(ret != iss->packet_size)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     pkt->stream_index = 0;
     pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
diff --git a/libavformat/jvdec.c b/libavformat/jvdec.c
index c4580b6a01..4f4566f64b 100644
--- a/libavformat/jvdec.c
+++ b/libavformat/jvdec.c
@@ -217,7 +217,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     if (s->pb->eof_reached)
         return AVERROR_EOF;
 
-    return AVERROR(EIO);
+    return AVERROR_INVALIDDATA;
 }
 
 static int read_seek(AVFormatContext *s, int stream_index,
diff --git a/libavformat/libmodplug.c b/libavformat/libmodplug.c
index 680a5fe9bc..01566d121e 100644
--- a/libavformat/libmodplug.c
+++ b/libavformat/libmodplug.c
@@ -348,7 +348,7 @@ static int modplug_read_packet(AVFormatContext *s, AVPacket 
*pkt)
 
     pkt->size = ModPlug_Read(modplug->f, pkt->data, AUDIO_PKT_SIZE);
     if (pkt->size <= 0) {
-        return pkt->size == 0 ? AVERROR_EOF : AVERROR(EIO);
+        return pkt->size == 0 ? AVERROR_EOF : AVERROR_EXTERNAL;
     }
     return 0;
 }
diff --git a/libavformat/lmlm4.c b/libavformat/lmlm4.c
index cec2f7ca05..aeb5580620 100644
--- a/libavformat/lmlm4.c
+++ b/libavformat/lmlm4.c
@@ -103,7 +103,7 @@ static int lmlm4_read_packet(AVFormatContext *s, AVPacket 
*pkt)
 
     frame_size  = packet_size - 8;
     if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0)
-        return ret < 0 ? ret : AVERROR(EIO);
+        return ret < 0 ? ret : AVERROR_INVALIDDATA;
 
     avio_skip(pb, padding);
 
diff --git a/libavformat/mca.c b/libavformat/mca.c
index e707de3c3b..74a33e25c3 100644
--- a/libavformat/mca.c
+++ b/libavformat/mca.c
@@ -105,7 +105,7 @@ static int read_header(AVFormatContext *s)
     if (version <= 4) {
         // version <= 4 needs to use the file size to calculate the offsets
         if (file_size < 0) {
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
         if (file_size - data_size > UINT32_MAX)
             return AVERROR_INVALIDDATA;
diff --git a/libavformat/mgsts.c b/libavformat/mgsts.c
index 07ea66163c..f8392888bd 100644
--- a/libavformat/mgsts.c
+++ b/libavformat/mgsts.c
@@ -44,7 +44,7 @@ static int read_header(AVFormatContext *s)
     avio_skip(pb, 4);
     chunk_size = avio_rb32(pb);
     if (chunk_size != 80)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     avio_skip(pb, 20);
 
     st = avformat_new_stream(s, 0);
@@ -84,7 +84,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     payload_size = avio_rb32(pb);
 
     if (chunk_size < payload_size + 16)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     ret = av_get_packet(pb, pkt, payload_size);
     if (ret < 0)
diff --git a/libavformat/mlvdec.c b/libavformat/mlvdec.c
index a0d5e7fb55..3a5d211085 100644
--- a/libavformat/mlvdec.c
+++ b/libavformat/mlvdec.c
@@ -548,7 +548,7 @@ static int read_packet(AVFormatContext *avctx, AVPacket 
*pkt)
     index = av_index_search_timestamp(st, mlv->pts, AVSEEK_FLAG_ANY);
     if (index < 0) {
         av_log(avctx, AV_LOG_ERROR, "could not find index entry for frame 
%"PRId64"\n", mlv->pts);
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     pb = mlv->pb[sti->index_entries[index].size];
@@ -611,7 +611,7 @@ static int read_seek(AVFormatContext *avctx, int 
stream_index, int64_t timestamp
         return AVERROR(ENOSYS);
 
     if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
-        return AVERROR(EIO);
+        return AVERROR(ENOSYS);
 
     mlv->pts = timestamp;
     return 0;
diff --git a/libavformat/mpc.c b/libavformat/mpc.c
index 1e0e170c7d..45ba76c703 100644
--- a/libavformat/mpc.c
+++ b/libavformat/mpc.c
@@ -171,7 +171,7 @@ static int mpc_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     if(c->curbits)
         avio_seek(s->pb, -4, SEEK_CUR);
     if(ret < size){
-        return ret < 0 ? ret : AVERROR(EIO);
+        return ret < 0 ? ret : AVERROR_INVALIDDATA;
     }
     pkt->size = ret + 4;
 
diff --git a/libavformat/mtv.c b/libavformat/mtv.c
index 01379a18e7..c888443b7a 100644
--- a/libavformat/mtv.c
+++ b/libavformat/mtv.c
@@ -105,6 +105,7 @@ static int mtv_read_header(AVFormatContext *s)
     AVIOContext   *pb  = s->pb;
     AVStream        *st;
     unsigned int    audio_subsegments;
+    int64_t ret64;
 
     avio_skip(pb, 3);
     mtv->file_size         = avio_rl32(pb);
@@ -190,8 +191,8 @@ static int mtv_read_header(AVFormatContext *s)
 
     // Jump over header
 
-    if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
-        return AVERROR(EIO);
+    if ((ret64 = avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET)) < 0)
+        return (int)ret64;
 
     return 0;
 
diff --git a/libavformat/mvdec.c b/libavformat/mvdec.c
index aa45d23b39..113f133687 100644
--- a/libavformat/mvdec.c
+++ b/libavformat/mvdec.c
@@ -489,7 +489,7 @@ static int mv_read_packet(AVFormatContext *avctx, AVPacket 
*pkt)
             avio_skip(pb, index->pos - pos);
         else if (index->pos < pos) {
             if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
-                return AVERROR(EIO);
+                return AVERROR(ENOSYS);
             ret = avio_seek(pb, index->pos, SEEK_SET);
             if (ret < 0)
                 return ret;
@@ -531,7 +531,7 @@ static int mv_read_seek(AVFormatContext *avctx, int 
stream_index,
         return AVERROR(ENOSYS);
 
     if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
-        return AVERROR(EIO);
+        return AVERROR(ENOSYS);
 
     frame = av_index_search_timestamp(st, timestamp, flags);
     if (frame < 0)
diff --git a/libavformat/mvi.c b/libavformat/mvi.c
index 05aa25f348..42127bbb5d 100644
--- a/libavformat/mvi.c
+++ b/libavformat/mvi.c
@@ -119,7 +119,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     if (mvi->video_frame_size == 0) {
         mvi->video_frame_size = (mvi->get_int)(pb);
         if (mvi->audio_size_left == 0)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         if (mvi->audio_size_counter + 512 > UINT64_MAX - mvi->audio_frame_size 
||
             mvi->audio_size_counter + 512 + mvi->audio_frame_size >= 
((uint64_t)INT32_MAX) << MVI_FRAC_BITS)
             return AVERROR_INVALIDDATA;
diff --git a/libavformat/ncdec.c b/libavformat/ncdec.c
index 050d98bf5d..6eb693093a 100644
--- a/libavformat/ncdec.c
+++ b/libavformat/ncdec.c
@@ -69,7 +69,7 @@ static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
     uint32_t state=-1;
     while (state != NC_VIDEO_FLAG) {
         if (avio_feof(s->pb))
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         state = (state<<8) + avio_r8(s->pb);
     }
 
@@ -84,7 +84,7 @@ static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = av_get_packet(s->pb, pkt, size);
     if (ret != size) {
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     pkt->stream_index = 0;
diff --git a/libavformat/nuv.c b/libavformat/nuv.c
index 49915ecf16..17a041b254 100644
--- a/libavformat/nuv.c
+++ b/libavformat/nuv.c
@@ -320,7 +320,7 @@ static int nuv_packet(AVFormatContext *s, AVPacket *pkt)
         }
     }
 
-    return AVERROR(EIO);
+    return AVERROR_INVALIDDATA;
 }
 
 /**
diff --git a/libavformat/pdvdec.c b/libavformat/pdvdec.c
index cd118f0e37..792188b019 100644
--- a/libavformat/pdvdec.c
+++ b/libavformat/pdvdec.c
@@ -112,7 +112,7 @@ static int pdv_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         return AVERROR_EOF;
 
     if (p->current_frame >= sti->nb_index_entries)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     pos   = sti->index_entries[p->current_frame].pos;
     flags = sti->index_entries[p->current_frame].flags;
diff --git a/libavformat/pp_bnk.c b/libavformat/pp_bnk.c
index 5360b7c5d7..15bb49ea26 100644
--- a/libavformat/pp_bnk.c
+++ b/libavformat/pp_bnk.c
@@ -20,6 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "libavutil/intreadwrite.h"
@@ -117,10 +118,8 @@ static int pp_bnk_read_header(AVFormatContext *s)
     uint8_t buf[FFMAX(PP_BNK_FILE_HEADER_SIZE, PP_BNK_TRACK_SIZE)];
     PPBnkHeader hdr;
 
-    if ((ret = avio_read(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
+    if ((ret = ffio_read_size(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
         return ret;
-    else if (ret != PP_BNK_FILE_HEADER_SIZE)
-        return AVERROR(EIO);
 
     pp_bnk_parse_header(&hdr, buf);
 
@@ -246,7 +245,7 @@ static int pp_bnk_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         if ((ret = avio_seek(s->pb, trk->data_offset + trk->bytes_read, 
SEEK_SET)) < 0)
             return ret;
         else if (ret != trk->data_offset + trk->bytes_read)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
 
         size = FFMIN(trk->data_size - trk->bytes_read, PP_BNK_MAX_READ_SIZE);
 
diff --git a/libavformat/psxstr.c b/libavformat/psxstr.c
index 0dd4e8d377..06148713b2 100644
--- a/libavformat/psxstr.c
+++ b/libavformat/psxstr.c
@@ -33,6 +33,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -134,11 +135,12 @@ static int str_read_header(AVFormatContext *s)
     StrDemuxContext *str = s->priv_data;
     unsigned char sector[RAW_CD_SECTOR_SIZE];
     int start;
+    int ret;
     int i;
 
     /* skip over any RIFF header */
-    if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, sector, RIFF_HEADER_SIZE)) < 0)
+        return ret;
     if (AV_RL32(&sector[0]) == RIFF_TAG)
         start = RIFF_HEADER_SIZE;
     else
@@ -173,7 +175,7 @@ static int str_read_packet(AVFormatContext *s,
             return AVERROR_EOF;
 
         if (read != RAW_CD_SECTOR_SIZE)
-            return AVERROR(EIO);
+            return read < 0 ? read : AVERROR_INVALIDDATA;
 
         channel = sector[0x11];
         if (channel >= 32)
@@ -287,7 +289,7 @@ static int str_read_packet(AVFormatContext *s,
         }
 
         if (avio_feof(pb))
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
     }
 }
 
diff --git a/libavformat/pva.c b/libavformat/pva.c
index 047c93c9c4..9de1a186a9 100644
--- a/libavformat/pva.c
+++ b/libavformat/pva.c
@@ -103,18 +103,18 @@ recover:
 
     if (syncword != PVA_MAGIC) {
         pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
     if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
         pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
     if (reserved != 0x55) {
         pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
     }
     if (length > PVA_MAX_PAYLOAD_LENGTH) {
         pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
@@ -145,7 +145,7 @@ recover:
                                           "trying to recover\n");
                 avio_skip(pb, length - 9);
                 if (!read_packet)
-                    return AVERROR(EIO);
+                    return AVERROR_INVALIDDATA;
                 goto recover;
             }
 
@@ -192,7 +192,7 @@ static int pva_read_packet(AVFormatContext *s, AVPacket 
*pkt) {
 
     if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
        (ret = av_get_packet(pb, pkt, length)) <= 0)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     pkt->stream_index = streamid - 1;
     pkt->pts = pva_pts;
diff --git a/libavformat/qoadec.c b/libavformat/qoadec.c
index a9632c46c3..6f8fe111ad 100644
--- a/libavformat/qoadec.c
+++ b/libavformat/qoadec.c
@@ -93,9 +93,9 @@ static int qoa_read_packet(AVFormatContext *s, AVPacket *pkt)
         return ret;
 
     memcpy(pkt->data, hdr, sizeof(hdr));
-    ret = avio_read(pb, pkt->data + sizeof(hdr), size - sizeof(hdr));
-    if (ret != size - sizeof(hdr))
-        return AVERROR(EIO);
+    ret = ffio_read_size(pb, pkt->data + sizeof(hdr), size - sizeof(hdr));
+    if (ret < 0)
+        return ret;
     pkt->stream_index = 0;
     pkt->pos = pos;
     pkt->duration = duration;
diff --git a/libavformat/redspark.c b/libavformat/redspark.c
index 2642d7af67..fded46ab43 100644
--- a/libavformat/redspark.c
+++ b/libavformat/redspark.c
@@ -141,7 +141,7 @@ static int redspark_read_packet(AVFormatContext *s, 
AVPacket *pkt)
 
     ret = av_get_packet(s->pb, pkt, size);
     if (ret != size) {
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     pkt->duration = 14;
diff --git a/libavformat/rl2.c b/libavformat/rl2.c
index aa59332783..e26e14f9cd 100644
--- a/libavformat/rl2.c
+++ b/libavformat/rl2.c
@@ -259,7 +259,7 @@ static int rl2_read_packet(AVFormatContext *s,
     /** fill the packet */
     ret = av_get_packet(pb, pkt, sample->size);
     if(ret != sample->size){
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     pkt->stream_index = stream_id;
diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c
index 85643a358f..2909698cda 100644
--- a/libavformat/rmdec.c
+++ b/libavformat/rmdec.c
@@ -569,7 +569,7 @@ static int rm_read_header(AVFormatContext *s)
         /* very old .ra format */
         return rm_read_header_old(s);
     } else if (tag != MKTAG('.', 'R', 'M', 'F') && tag != MKTAG('.', 'R', 'M', 
'P')) {
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     tag_size = avio_rb32(pb);
@@ -1064,7 +1064,7 @@ static int rm_read_packet(AVFormatContext *s, AVPacket 
*pkt)
             if (avio_feof(s->pb))
                 return AVERROR_EOF;
             if (len <= 0)
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
 
             res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
                                       &seq, flags, timestamp);
@@ -1410,7 +1410,7 @@ static int ivr_read_packet(AVFormatContext *s, AVPacket 
*pkt)
                 }
             } else {
                 av_log(s, AV_LOG_ERROR, "Unsupported opcode=%d at 
%"PRIX64"\n", opcode, avio_tell(pb) - 1);
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
             }
         }
 
diff --git a/libavformat/rpl.c b/libavformat/rpl.c
index 781dabf7ba..06e3f354fc 100644
--- a/libavformat/rpl.c
+++ b/libavformat/rpl.c
@@ -313,7 +313,7 @@ static int rpl_read_header(AVFormatContext *s)
     }
 
     if (error)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     return 0;
 }
@@ -341,8 +341,9 @@ static int rpl_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     index_entry = &sti->index_entries[rpl->chunk_number];
 
     if (rpl->frame_in_part == 0) {
-        if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
-            return AVERROR(EIO);
+        int64_t ret64 = avio_seek(pb, index_entry->pos, SEEK_SET);
+        if (ret64 < 0)
+            return (int)ret64;
     }
 
     if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
@@ -350,17 +351,20 @@ static int rpl_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         // We have to split Escape 124 frames because there are
         // multiple frames per chunk in Escape 124 samples.
         uint32_t frame_size;
+        int64_t ret64;
 
         avio_skip(pb, 4); /* flags */
         frame_size = avio_rl32(pb);
-        if (avio_feof(pb) || avio_seek(pb, -8, SEEK_CUR) < 0 || !frame_size)
-            return AVERROR(EIO);
+        if (avio_feof(pb) || !frame_size)
+            return AVERROR_INVALIDDATA;
+        if ((ret64 = avio_seek(pb, -8, SEEK_CUR)) < 0)
+            return (int)ret64;
 
         ret = av_get_packet(pb, pkt, frame_size);
         if (ret < 0)
             return ret;
         if (ret != frame_size)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
 
         pkt->duration = 1;
         pkt->pts = index_entry->timestamp + rpl->frame_in_part;
@@ -376,7 +380,7 @@ static int rpl_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         if (ret < 0)
             return ret;
         if (ret != index_entry->size)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
 
         if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
             // frames_per_chunk should always be one here; the header
diff --git a/libavformat/segafilm.c b/libavformat/segafilm.c
index e72d872f96..2b853017db 100644
--- a/libavformat/segafilm.c
+++ b/libavformat/segafilm.c
@@ -30,6 +30,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -95,20 +96,21 @@ static int film_read_header(AVFormatContext *s)
     unsigned int data_offset;
     unsigned int audio_frame_counter;
     unsigned int video_frame_counter;
+    int ret;
 
     film->sample_table = NULL;
 
     /* load the main FILM header */
-    if (avio_read(pb, scratch, 16) != 16)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, scratch, 16)) < 0)
+        return ret;
     data_offset = AV_RB32(&scratch[4]);
     film->version = AV_RB32(&scratch[8]);
 
     /* load the FDSC chunk */
     if (film->version == 0) {
         /* special case for Lemmings .film files; 20-byte header */
-        if (avio_read(pb, scratch, 20) != 20)
-            return AVERROR(EIO);
+        if ((ret = ffio_read_size(pb, scratch, 20)) < 0)
+            return ret;
         /* make some assumptions about the audio parameters */
         film->audio_type = AV_CODEC_ID_PCM_S8;
         film->audio_samplerate = 22050;
@@ -116,8 +118,8 @@ static int film_read_header(AVFormatContext *s)
         film->audio_bits = 8;
     } else {
         /* normal Saturn .cpk files; 32-byte header */
-        if (avio_read(pb, scratch, 32) != 32)
-            return AVERROR(EIO);
+        if ((ret = ffio_read_size(pb, scratch, 32)) < 0)
+            return ret;
         film->audio_samplerate = AV_RB16(&scratch[24]);
         film->audio_channels = scratch[21];
         film->audio_bits = scratch[22];
@@ -196,8 +198,8 @@ static int film_read_header(AVFormatContext *s)
     }
 
     /* load the sample table */
-    if (avio_read(pb, scratch, 16) != 16)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, scratch, 16)) < 0)
+        return ret;
     if (AV_RB32(&scratch[0]) != STAB_TAG)
         return AVERROR_INVALIDDATA;
     film->base_clock = AV_RB32(&scratch[8]);
@@ -217,8 +219,8 @@ static int film_read_header(AVFormatContext *s)
     audio_frame_counter = video_frame_counter = 0;
     for (i = 0; i < film->sample_count; i++) {
         /* load the next sample record and transfer it to an internal struct */
-        if (avio_read(pb, scratch, 16) != 16)
-            return AVERROR(EIO);
+        if ((ret = ffio_read_size(pb, scratch, 16)) < 0)
+            return ret;
         film->sample_table[i].sample_offset =
             data_offset + AV_RB32(&scratch[0]);
         film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
@@ -294,7 +296,7 @@ static int film_read_packet(AVFormatContext *s,
 
     ret = av_get_packet(pb, pkt, sample->sample_size);
     if (ret != sample->sample_size)
-        ret = AVERROR(EIO);
+        ret = AVERROR_INVALIDDATA;
 
     pkt->stream_index = sample->stream;
     pkt->dts = sample->pts;
diff --git a/libavformat/sierravmd.c b/libavformat/sierravmd.c
index 2103ff64db..bb1d1c5df7 100644
--- a/libavformat/sierravmd.c
+++ b/libavformat/sierravmd.c
@@ -31,6 +31,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "avio_internal.h"
@@ -102,8 +103,8 @@ static int vmd_read_header(AVFormatContext *s)
 
     /* fetch the main header, including the 2 header length bytes */
     avio_seek(pb, 0, SEEK_SET);
-    if (avio_read(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, vmd->vmd_header, VMD_HEADER_SIZE) < 0))
+        return ret;
 
     width = AV_RL16(&vmd->vmd_header[12]);
     height = AV_RL16(&vmd->vmd_header[14]);
@@ -192,11 +193,8 @@ static int vmd_read_header(AVFormatContext *s)
         ret = AVERROR(ENOMEM);
         goto error;
     }
-    if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
-        raw_frame_table_size) {
-        ret = AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, raw_frame_table, raw_frame_table_size)) < 0)
         goto error;
-    }
 
     total_frames = 0;
     for (i = 0; i < vmd->frame_count; i++) {
@@ -279,21 +277,18 @@ static int vmd_read_packet(AVFormatContext *s,
     avio_seek(pb, frame->frame_offset, SEEK_SET);
 
     if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     ret = av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD);
     if (ret < 0)
         return ret;
     pkt->pos= avio_tell(pb);
     memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
     if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
-        ret = avio_read(pb, pkt->data, frame->frame_size);
+        ret = ffio_read_size(pb, pkt->data, frame->frame_size);
     else
-        ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
+        ret = ffio_read_size(pb, pkt->data + BYTES_PER_FRAME_RECORD,
             frame->frame_size);
 
-    if (ret != frame->frame_size) {
-        ret = AVERROR(EIO);
-    }
     pkt->stream_index = frame->stream_index;
     pkt->pts = frame->pts;
     av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts 
%"PRId64"\n",
diff --git a/libavformat/siff.c b/libavformat/siff.c
index b33746d51d..b0fff80b09 100644
--- a/libavformat/siff.c
+++ b/libavformat/siff.c
@@ -232,7 +232,7 @@ static int siff_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         } else {
             int pktsize = av_get_packet(s->pb, pkt, c->sndsize - 4);
             if (pktsize < 0)
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
             pkt->stream_index = 1;
             pkt->duration     = pktsize;
             c->curstrm        = 0;
@@ -246,7 +246,7 @@ static int siff_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         if (!pktsize)
             return AVERROR_EOF;
         if (pktsize <= 0)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         pkt->duration = pktsize;
     }
     return pkt->size;
diff --git a/libavformat/smush.c b/libavformat/smush.c
index d380bfbff1..587125dbc0 100644
--- a/libavformat/smush.c
+++ b/libavformat/smush.c
@@ -235,7 +235,7 @@ static int smush_read_packet(AVFormatContext *ctx, AVPacket 
*pkt)
             if (size < 13)
                 return AVERROR_INVALIDDATA;
             if (av_get_packet(pb, pkt, size) < 13)
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
 
             pkt->stream_index = smush->audio_stream_index;
             pkt->flags       |= AV_PKT_FLAG_KEY;
diff --git a/libavformat/soxdec.c b/libavformat/soxdec.c
index ba349c870e..c710b9a152 100644
--- a/libavformat/soxdec.c
+++ b/libavformat/soxdec.c
@@ -34,6 +34,7 @@
 #include "libavutil/dict.h"
 #include "libavutil/mem.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "pcm.h"
@@ -106,11 +107,12 @@ static int sox_read_header(AVFormatContext *s)
 
     if (comment_size && comment_size < UINT_MAX) {
         char *comment = av_malloc(comment_size+1);
+        int ret;
         if(!comment)
             return AVERROR(ENOMEM);
-        if (avio_read(pb, comment, comment_size) != comment_size) {
+        if ((ret = ffio_read_size(pb, comment, comment_size)) < 0) {
             av_freep(&comment);
-            return AVERROR(EIO);
+            return ret;
         }
         comment[comment_size] = 0;
 
diff --git a/libavformat/swfdec.c b/libavformat/swfdec.c
index 29eefc68a2..1290f2c70f 100644
--- a/libavformat/swfdec.c
+++ b/libavformat/swfdec.c
@@ -174,10 +174,10 @@ static int swf_read_header(AVFormatContext *s)
         pb = swf->zpb;
 #else
         av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF 
compressed files\n");
-        return AVERROR(EIO);
+        return AVERROR(ENOSYS);
 #endif
     } else if (tag != MKBETAG('F', 'W', 'S', 0))
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     /* skip rectangle size */
     nbits = avio_r8(pb) >> 3;
     len = (4 * nbits - 3 + 7) / 8;
diff --git a/libavformat/thp.c b/libavformat/thp.c
index 76db7fc581..c1a7418a2c 100644
--- a/libavformat/thp.c
+++ b/libavformat/thp.c
@@ -193,7 +193,7 @@ static int thp_read_packet(AVFormatContext *s,
         if (ret < 0)
             return ret;
         if (ret != size) {
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
 
         pkt->stream_index = thp->video_stream_index;
@@ -202,7 +202,7 @@ static int thp_read_packet(AVFormatContext *s,
         if (ret < 0)
             return ret;
         if (ret != thp->audiosize) {
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         }
 
         pkt->stream_index = thp->audio_stream_index;
diff --git a/libavformat/tiertexseq.c b/libavformat/tiertexseq.c
index 844b98e182..5dc7d9f110 100644
--- a/libavformat/tiertexseq.c
+++ b/libavformat/tiertexseq.c
@@ -27,6 +27,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/mem.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -109,6 +110,7 @@ static int seq_init_frame_buffers(SeqDemuxContext *seq, 
AVIOContext *pb)
 static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int 
buffer_num, unsigned int data_offs, int data_size)
 {
     TiertexSeqFrameBuffer *seq_buffer;
+    int ret;
 
     if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
         return AVERROR_INVALIDDATA;
@@ -118,8 +120,8 @@ static int seq_fill_buffer(SeqDemuxContext *seq, 
AVIOContext *pb, int buffer_num
         return AVERROR_INVALIDDATA;
 
     avio_seek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
-    if (avio_read(pb, seq_buffer->data + seq_buffer->fill_size, data_size) != 
data_size)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, seq_buffer->data + seq_buffer->fill_size, 
data_size)) < 0)
+        return ret;
 
     seq_buffer->fill_size += data_size;
     return 0;
@@ -273,10 +275,11 @@ static int seq_read_packet(AVFormatContext *s, AVPacket 
*pkt)
 
             pkt->data[0] = 0;
             if (seq->current_pal_data_size) {
+                int ret;
                 pkt->data[0] |= 1;
                 avio_seek(pb, seq->current_frame_offs + 
seq->current_pal_data_offs, SEEK_SET);
-                if (avio_read(pb, &pkt->data[1], seq->current_pal_data_size) 
!= seq->current_pal_data_size)
-                    return AVERROR(EIO);
+                if ((ret = ffio_read_size(pb, &pkt->data[1], 
seq->current_pal_data_size)) < 0)
+                    return ret;
             }
             if (seq->current_video_data_size) {
                 pkt->data[0] |= 2;
@@ -295,7 +298,7 @@ static int seq_read_packet(AVFormatContext *s, AVPacket 
*pkt)
 
     /* audio packet */
     if (seq->current_audio_data_offs == 0) /* end of data reached */
-        return AVERROR(EIO);
+        return AVERROR_EOF;
 
     avio_seek(pb, seq->current_frame_offs + seq->current_audio_data_offs, 
SEEK_SET);
     rc = av_get_packet(pb, pkt, seq->current_audio_data_size);
diff --git a/libavformat/ty.c b/libavformat/ty.c
index f524b74bad..acd5e62157 100644
--- a/libavformat/ty.c
+++ b/libavformat/ty.c
@@ -303,7 +303,7 @@ static int ty_read_header(AVFormatContext *s)
     if (ty->tivo_series == TIVO_SERIES_UNKNOWN ||
         ty->audio_type == TIVO_AUDIO_UNKNOWN ||
         ty->tivo_type == TIVO_TYPE_UNKNOWN)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     st = avformat_new_stream(s, NULL);
     if (!st)
diff --git a/libavformat/vc1test.c b/libavformat/vc1test.c
index 394a70c1ac..239b35bf33 100644
--- a/libavformat/vc1test.c
+++ b/libavformat/vc1test.c
@@ -108,7 +108,7 @@ static int vc1t_read_packet(AVFormatContext *s,
         keyframe = 1;
     pts = avio_rl32(pb);
     if(av_get_packet(pb, pkt, frame_size) < 0)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     if(s->streams[0]->time_base.den == 1000)
         pkt->pts = pts;
     pkt->flags |= keyframe ? AV_PKT_FLAG_KEY : 0;
diff --git a/libavformat/vividas.c b/libavformat/vividas.c
index dd25539201..b708d71c65 100644
--- a/libavformat/vividas.c
+++ b/libavformat/vividas.c
@@ -601,7 +601,7 @@ static int viv_read_header(AVFormatContext *s)
         k2 = b22_key;
         buf = read_vblock(pb, &v, b22_key, &k2, 0);
         if (!buf)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
 
         av_free(buf);
     }
@@ -609,7 +609,7 @@ static int viv_read_header(AVFormatContext *s)
     k2 = key;
     buf = read_vblock(pb, &v, key, &k2, 0);
     if (!buf)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     ret = track_header(viv, s, buf, v);
     av_free(buf);
     if (ret < 0)
@@ -617,7 +617,7 @@ static int viv_read_header(AVFormatContext *s)
 
     buf = read_vblock(pb, &v, key, &k2, v);
     if (!buf)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     ret = track_index(viv, s, buf, v);
     av_free(buf);
     if (ret < 0)
@@ -643,7 +643,7 @@ static int viv_read_packet(AVFormatContext *s,
     int ret;
 
     if (!viv->sb_pb)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     if (avio_feof(viv->sb_pb))
         return AVERROR_EOF;
 
@@ -670,7 +670,7 @@ static int viv_read_packet(AVFormatContext *s,
 
     if (viv->current_sb_entry >= viv->n_sb_entries) {
         if (viv->current_sb+1 >= viv->n_sb_blocks)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
         viv->current_sb++;
 
         load_sb_block(s, viv, 0);
@@ -679,7 +679,7 @@ static int viv_read_packet(AVFormatContext *s,
 
     pb = viv->sb_pb;
     if (!pb)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     off = avio_tell(pb);
 
     if (viv->current_sb_entry >= viv->n_sb_entries)
diff --git a/libavformat/voc_packet.c b/libavformat/voc_packet.c
index 32f8b29aa7..cee6ac5746 100644
--- a/libavformat/voc_packet.c
+++ b/libavformat/voc_packet.c
@@ -53,7 +53,7 @@ ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream 
*st, int max_size)
         if (!voc->remaining_size) {
             int64_t filesize;
             if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
-                return AVERROR(EIO);
+                return AVERROR(ENOSYS);
             filesize = avio_size(pb);
             if (filesize - avio_tell(pb) > INT_MAX)
                 return AVERROR_INVALIDDATA;
diff --git a/libavformat/vpk.c b/libavformat/vpk.c
index 001ad33555..f6270a11ae 100644
--- a/libavformat/vpk.c
+++ b/libavformat/vpk.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -94,10 +95,10 @@ static int vpk_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         if (ret < 0)
             return ret;
         for (i = 0; i < par->ch_layout.nb_channels; i++) {
-            ret = avio_read(s->pb, pkt->data + i * size, size);
+            ret = ffio_read_size(s->pb, pkt->data + i * size, size);
             avio_skip(s->pb, skip);
-            if (ret != size) {
-                return AVERROR(EIO);
+            if (ret < 0) {
+                return ret;
             }
         }
         pkt->pos = pos;
diff --git a/libavformat/wavarc.c b/libavformat/wavarc.c
index 9d7029f209..6467d7d578 100644
--- a/libavformat/wavarc.c
+++ b/libavformat/wavarc.c
@@ -71,8 +71,8 @@ static int wavarc_read_header(AVFormatContext *s)
         return AVERROR_INVALIDDATA;
     id = avio_rl32(pb);
     w->data_end = avio_tell(pb);
-    if (avio_read(pb, data, sizeof(data)) != sizeof(data))
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, data, sizeof(data))) < 0)
+        return ret;
     w->data_end += 16LL + AV_RL32(data + 4);
     fmt_len = AV_RL32(data + 32);
     if (fmt_len < 12)
diff --git a/libavformat/wc3movie.c b/libavformat/wc3movie.c
index f4063353b6..b4e7f1e31d 100644
--- a/libavformat/wc3movie.c
+++ b/libavformat/wc3movie.c
@@ -33,6 +33,7 @@
 #include "libavutil/dict.h"
 #include "libavutil/mem.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -141,9 +142,9 @@ static int wc3_read_header(AVFormatContext *s)
             buffer = av_malloc(size+1);
             if (!buffer)
                 return AVERROR(ENOMEM);
-            if ((ret = avio_read(pb, buffer, size)) != size) {
+            if ((ret = ffio_read_size(pb, buffer, size)) < 0) {
                 av_freep(&buffer);
-                return AVERROR(EIO);
+                return ret;
             }
             buffer[size] = 0;
             av_dict_set(&s->metadata, "title", buffer,
@@ -172,7 +173,7 @@ static int wc3_read_header(AVFormatContext *s)
         /* chunk sizes are 16-bit aligned */
         size = (avio_rb32(pb) + 1) & (~1);
         if (avio_feof(pb))
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
 
     } while (fourcc_tag != BRCH_TAG);
 
@@ -223,7 +224,7 @@ static int wc3_read_packet(AVFormatContext *s,
         /* chunk sizes are 16-bit aligned */
         size = (avio_rb32(pb) + 1) & (~1);
         if (avio_feof(pb))
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
 
         switch (fourcc_tag) {
 
@@ -252,9 +253,9 @@ static int wc3_read_packet(AVFormatContext *s,
 
         case TEXT_TAG:
             /* subtitle chunk */
-            if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, 
size)) != size)
-                ret = AVERROR(EIO);
-            else {
+            if ((unsigned)size > sizeof(text))
+                ret = AVERROR_INVALIDDATA;
+            else if ((ret = ffio_read_size(pb, text, size)) == size) {
                 int i = 0;
                 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
                 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= 
size - i - 1)
diff --git a/libavformat/westwood_aud.c b/libavformat/westwood_aud.c
index f83913a22f..68b60be5b1 100644
--- a/libavformat/westwood_aud.c
+++ b/libavformat/westwood_aud.c
@@ -36,6 +36,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 
@@ -87,9 +88,10 @@ static int wsaud_read_header(AVFormatContext *s)
     AVStream *st;
     unsigned char header[AUD_HEADER_SIZE];
     int sample_rate, channels, codec;
+    int ret;
 
-    if (avio_read(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, header, AUD_HEADER_SIZE)) < 0)
+        return ret;
 
     sample_rate = AV_RL16(&header[0]);
     channels    = (header[10] & 0x1) + 1;
@@ -134,9 +136,8 @@ static int wsaud_read_packet(AVFormatContext *s,
     int ret = 0;
     AVStream *st = s->streams[0];
 
-    if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
-        AUD_CHUNK_PREAMBLE_SIZE)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE)) < 0)
+        return ret;
 
     /* validate the chunk */
     if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
@@ -152,8 +153,8 @@ static int wsaud_read_packet(AVFormatContext *s,
         int out_size = AV_RL16(&preamble[2]);
         if ((ret = av_new_packet(pkt, chunk_size + 4)) < 0)
             return ret;
-        if ((ret = avio_read(pb, &pkt->data[4], chunk_size)) != chunk_size)
-            return ret < 0 ? ret : AVERROR(EIO);
+        if ((ret = ffio_read_size(pb, &pkt->data[4], chunk_size)) < 0)
+            return ret;
         AV_WL16(&pkt->data[0], out_size);
         AV_WL16(&pkt->data[2], chunk_size);
 
@@ -161,7 +162,7 @@ static int wsaud_read_packet(AVFormatContext *s,
     } else {
         ret = av_get_packet(pb, pkt, chunk_size);
         if (ret != chunk_size)
-            return AVERROR(EIO);
+            return AVERROR_INVALIDDATA;
 
         if (st->codecpar->ch_layout.nb_channels <= 0) {
             av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n",
diff --git a/libavformat/westwood_vqa.c b/libavformat/westwood_vqa.c
index 9755fcc9c1..8e5179f697 100644
--- a/libavformat/westwood_vqa.c
+++ b/libavformat/westwood_vqa.c
@@ -138,8 +138,8 @@ static int wsvqa_read_header(AVFormatContext *s)
     /* there are 0 or more chunks before the FINF chunk; iterate until
      * FINF has been skipped and the file will be ready to be demuxed */
     do {
-        if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
-            return AVERROR(EIO);
+        if ((ret = ffio_read_size(pb, scratch, VQA_PREAMBLE_SIZE)) < 0)
+            return ret;
         chunk_tag = AV_RB32(&scratch[0]);
         chunk_size = AV_RB32(&scratch[4]);
 
@@ -211,7 +211,7 @@ static int wsvqa_read_packet(AVFormatContext *s,
 
             ret= av_get_packet(pb, pkt, chunk_size);
             if (ret<0)
-                return AVERROR(EIO);
+                return AVERROR_INVALIDDATA;
 
             switch (chunk_type) {
             case SND0_TAG:
@@ -272,20 +272,20 @@ static int wsvqa_read_packet(AVFormatContext *s,
                 /* if a new codebook is available inside an earlier a VQFL 
chunk then
                  * append it to 'pkt' */
                 if (wsvqa->vqfl_chunk_size > 0) {
-                    int64_t current_pos = pkt->pos;
+                    int64_t ret64, current_pos = pkt->pos;
 
-                    if (avio_seek(pb, wsvqa->vqfl_chunk_pos, SEEK_SET) < 0)
-                        return AVERROR(EIO);
+                    if ((ret64 = avio_seek(pb, wsvqa->vqfl_chunk_pos, 
SEEK_SET)) < 0)
+                        return (int)ret64;
 
                     /* the decoder expects chunks to be 16-bit aligned */
                     if (wsvqa->vqfl_chunk_size % 2 == 1)
                         wsvqa->vqfl_chunk_size++;
 
                     if (av_append_packet(pb, pkt, wsvqa->vqfl_chunk_size) < 0)
-                        return AVERROR(EIO);
+                        return AVERROR_INVALIDDATA;
 
-                    if (avio_seek(pb, current_pos, SEEK_SET) < 0)
-                        return AVERROR(EIO);
+                    if ((ret64 = avio_seek(pb, current_pos, SEEK_SET)) < 0)
+                        return (int)ret64;
 
                     wsvqa->vqfl_chunk_pos = 0;
                     wsvqa->vqfl_chunk_size = 0;
diff --git a/libavformat/wsddec.c b/libavformat/wsddec.c
index b0bf49cb04..f36c254621 100644
--- a/libavformat/wsddec.c
+++ b/libavformat/wsddec.c
@@ -24,6 +24,7 @@
 #include "libavutil/mem.h"
 #include "libavutil/timecode.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "rawdec.h"
 
@@ -73,6 +74,7 @@ static int wsd_to_av_channel_layoyt(AVFormatContext *s, int 
bit)
 static int get_metadata(AVFormatContext *s, const char *const tag, const 
unsigned size)
 {
     uint8_t *buf;
+    int ret;
     if (!(size + 1))
         return AVERROR(ENOMEM);
 
@@ -80,9 +82,9 @@ static int get_metadata(AVFormatContext *s, const char *const 
tag, const unsigne
     if (!buf)
         return AVERROR(ENOMEM);
 
-    if (avio_read(s->pb, buf, size) != size) {
+    if ((ret = avio_read(s->pb, buf, size)) < 0) {
         av_free(buf);
-        return AVERROR(EIO);
+        return ret;
     }
 
     if (empty_string(buf, size)) {
diff --git a/libavformat/wtvdec.c b/libavformat/wtvdec.c
index 9d26e35e22..1f299510c9 100644
--- a/libavformat/wtvdec.c
+++ b/libavformat/wtvdec.c
@@ -761,7 +761,7 @@ static int recover(WtvContext *wtv, uint64_t broken_pos)
             return 0;
          }
      }
-     return AVERROR(EIO);
+     return AVERROR_INVALIDDATA;
 }
 
 /**
diff --git a/libavformat/wvdec.c b/libavformat/wvdec.c
index e2a79957f7..e69f42baf5 100644
--- a/libavformat/wvdec.c
+++ b/libavformat/wvdec.c
@@ -23,6 +23,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "apetag.h"
@@ -295,9 +296,9 @@ static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
     if ((ret = av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE)) < 0)
         return ret;
     memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
-    ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
-    if (ret != wc->header.blocksize) {
-        return AVERROR(EIO);
+    ret = ffio_read_size(s->pb, pkt->data + WV_HEADER_SIZE, 
wc->header.blocksize);
+    if (ret < 0) {
+        return ret;
     }
     while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
         if ((ret = wv_read_block_header(s, s->pb)) < 0) {
diff --git a/libavformat/xmv.c b/libavformat/xmv.c
index ed59f7b85b..c0b402860e 100644
--- a/libavformat/xmv.c
+++ b/libavformat/xmv.c
@@ -31,6 +31,7 @@
 #include "libavutil/mem.h"
 
 #include "avformat.h"
+#include "avio_internal.h"
 #include "demux.h"
 #include "internal.h"
 #include "riff.h"
@@ -273,8 +274,8 @@ static int xmv_process_packet_header(AVFormatContext *s)
 
     /* Packet video header */
 
-    if (avio_read(pb, data, 8) != 8)
-        return AVERROR(EIO);
+    if ((ret = ffio_read_size(pb, data, 8)) < 0)
+        return ret;
 
     xmv->video.data_size     = AV_RL32(data) & 0x007FFFFF;
 
@@ -325,8 +326,8 @@ static int xmv_process_packet_header(AVFormatContext *s)
     for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) 
{
         XMVAudioPacket *packet = &xmv->audio[audio_track];
 
-        if (avio_read(pb, data, 4) != 4)
-            return AVERROR(EIO);
+        if ((ret = ffio_read_size(pb, data, 4)) < 0)
+            return ret;
 
         if (!packet->created) {
             AVStream *ast = avformat_new_stream(s, NULL);
@@ -417,12 +418,12 @@ static int xmv_fetch_new_packet(AVFormatContext *s)
     /* Seek to it */
     xmv->this_packet_offset = xmv->next_packet_offset;
     if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != 
xmv->this_packet_offset)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     /* Update the size */
     xmv->this_packet_size = xmv->next_packet_size;
     if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4))
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     /* Process the header */
     result = xmv_process_packet_header(s);
@@ -448,7 +449,7 @@ static int xmv_fetch_audio_packet(AVFormatContext *s,
 
     /* Seek to it */
     if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     if ((xmv->video.current_frame + 1) < xmv->video.frame_count)
         /* Not the last frame, get at most frame_size bytes. */
@@ -495,7 +496,7 @@ static int xmv_fetch_video_packet(AVFormatContext *s,
 
     /* Seek to it */
     if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     /* Read the frame header */
     frame_header = avio_rl32(pb);
@@ -504,7 +505,7 @@ static int xmv_fetch_video_packet(AVFormatContext *s,
     frame_timestamp = (frame_header >> 17);
 
     if ((frame_size + 4) > video->data_size)
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
 
     /* Get the packet data */
     result = av_get_packet(pb, pkt, frame_size);
diff --git a/libavformat/yuv4mpegdec.c b/libavformat/yuv4mpegdec.c
index 2b66a1e596..5c21858908 100644
--- a/libavformat/yuv4mpegdec.c
+++ b/libavformat/yuv4mpegdec.c
@@ -291,7 +291,7 @@ static int yuv4_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     if (ret < 0)
         return ret;
     else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN) {
-        return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
+        return s->pb->eof_reached ? AVERROR_EOF : AVERROR_INVALIDDATA;
     }
     pkt->stream_index = 0;
     pkt->pts = (off - ffformatcontext(s)->data_offset) / s->packet_size;
diff --git a/libavformat/yuv4mpegenc.c b/libavformat/yuv4mpegenc.c
index 35397cbde0..371d3745c1 100644
--- a/libavformat/yuv4mpegenc.c
+++ b/libavformat/yuv4mpegenc.c
@@ -282,7 +282,7 @@ static int yuv4_init(AVFormatContext *s)
                "gray9, gray10, gray12 "
                "and gray16 pixel formats. "
                "Use -pix_fmt to select one.\n");
-        return AVERROR(EIO);
+        return AVERROR_INVALIDDATA;
     }
 
     return 0;

-----------------------------------------------------------------------

Summary of changes:
 libavformat/4xm.c            |  8 +++++---
 libavformat/aacdec.c         |  4 ++--
 libavformat/aaxdec.c         |  7 ++++---
 libavformat/adxdec.c         |  2 +-
 libavformat/aiffdec.c        |  2 +-
 libavformat/alp.c            |  5 ++---
 libavformat/apc.c            |  7 +++++--
 libavformat/ape.c            |  2 +-
 libavformat/apm.c            |  5 ++---
 libavformat/argo_asf.c       |  9 +++------
 libavformat/argo_cvg.c       |  9 +++------
 libavformat/avs.c            | 16 ++++++++--------
 libavformat/bethsoftvid.c    | 21 ++++++++++-----------
 libavformat/bfi.c            |  2 +-
 libavformat/bink.c           | 14 +++++++-------
 libavformat/binka.c          |  2 +-
 libavformat/bintext.c        | 39 ++++++++++++++++++++-------------------
 libavformat/bit.c            |  5 ++---
 libavformat/bmv.c            |  5 +++--
 libavformat/brstm.c          |  9 +++++----
 libavformat/c93.c            | 13 +++++++------
 libavformat/cafdec.c         | 11 ++++++-----
 libavformat/cinedec.c        |  2 +-
 libavformat/dfa.c            |  4 ++--
 libavformat/dsicin.c         |  2 +-
 libavformat/dss.c            |  5 +++--
 libavformat/dv.c             | 14 ++++++++------
 libavformat/dxa.c            | 10 +++++-----
 libavformat/electronicarts.c |  2 +-
 libavformat/filmstripdec.c   |  2 +-
 libavformat/flic.c           | 25 +++++++++----------------
 libavformat/gifdec.c         |  6 ++++--
 libavformat/gsmdec.c         |  2 +-
 libavformat/hca.c            |  7 ++++---
 libavformat/icoenc.c         |  2 +-
 libavformat/idcin.c          |  6 +++---
 libavformat/idroqdec.c       | 29 +++++++++++++----------------
 libavformat/iff.c            | 10 ++++++----
 libavformat/img2dec.c        |  6 +++---
 libavformat/img2enc.c        |  3 +--
 libavformat/ingenientdec.c   |  2 +-
 libavformat/ipmovie.c        |  7 +++----
 libavformat/iss.c            |  2 +-
 libavformat/jvdec.c          |  2 +-
 libavformat/libmodplug.c     |  2 +-
 libavformat/lmlm4.c          |  2 +-
 libavformat/mca.c            |  2 +-
 libavformat/mgsts.c          |  4 ++--
 libavformat/mlvdec.c         |  4 ++--
 libavformat/mpc.c            |  2 +-
 libavformat/mtv.c            |  5 +++--
 libavformat/mvdec.c          |  4 ++--
 libavformat/mvi.c            |  2 +-
 libavformat/ncdec.c          |  4 ++--
 libavformat/nuv.c            |  2 +-
 libavformat/pdvdec.c         |  2 +-
 libavformat/pp_bnk.c         |  7 +++----
 libavformat/psxstr.c         | 10 ++++++----
 libavformat/pva.c            | 10 +++++-----
 libavformat/qoadec.c         |  6 +++---
 libavformat/redspark.c       |  2 +-
 libavformat/rl2.c            |  2 +-
 libavformat/rmdec.c          |  6 +++---
 libavformat/rpl.c            | 18 +++++++++++-------
 libavformat/segafilm.c       | 24 +++++++++++++-----------
 libavformat/sierravmd.c      | 19 +++++++------------
 libavformat/siff.c           |  4 ++--
 libavformat/smush.c          |  2 +-
 libavformat/soxdec.c         |  6 ++++--
 libavformat/swfdec.c         |  4 ++--
 libavformat/thp.c            |  4 ++--
 libavformat/tiertexseq.c     | 13 ++++++++-----
 libavformat/ty.c             |  2 +-
 libavformat/vc1test.c        |  2 +-
 libavformat/vividas.c        | 12 ++++++------
 libavformat/voc_packet.c     |  2 +-
 libavformat/vpk.c            |  7 ++++---
 libavformat/wavarc.c         |  4 ++--
 libavformat/wc3movie.c       | 15 ++++++++-------
 libavformat/westwood_aud.c   | 17 +++++++++--------
 libavformat/westwood_vqa.c   | 18 +++++++++---------
 libavformat/wsddec.c         |  6 ++++--
 libavformat/wtvdec.c         |  2 +-
 libavformat/wvdec.c          |  7 ++++---
 libavformat/xmv.c            | 19 ++++++++++---------
 libavformat/yuv4mpegdec.c    |  2 +-
 libavformat/yuv4mpegenc.c    |  2 +-
 87 files changed, 324 insertions(+), 312 deletions(-)


hooks/post-receive
-- 

_______________________________________________
ffmpeg-cvslog mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to