diff --git a/libavformat/aacdec.c b/libavformat/aacdec.c
index 00ca2319ca..ba3f5ccc6d 100644
--- a/libavformat/aacdec.c
+++ b/libavformat/aacdec.c
@@ -141,7 +141,6 @@ static int handle_id3(AVFormatContext *s, AVPacket *pkt)
 
     ret = av_append_packet(s->pb, pkt, ff_id3v2_tag_len(pkt->data) - pkt->size);
     if (ret < 0) {
-        av_packet_unref(pkt);
         return ret;
     }
 
@@ -174,7 +173,6 @@ retry:
         return ret;
 
     if (ret < ADTS_HEADER_SIZE) {
-        av_packet_unref(pkt);
         return AVERROR(EIO);
     }
 
@@ -185,7 +183,6 @@ retry:
         av_assert2(append > 0);
         ret = av_append_packet(s->pb, pkt, append);
         if (ret != append) {
-            av_packet_unref(pkt);
             return AVERROR(EIO);
         }
         if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) {
@@ -201,13 +198,10 @@ retry:
 
     fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF;
     if (fsize < ADTS_HEADER_SIZE) {
-        av_packet_unref(pkt);
         return AVERROR_INVALIDDATA;
     }
 
     ret = av_append_packet(s->pb, pkt, fsize - pkt->size);
-    if (ret < 0)
-        av_packet_unref(pkt);
 
     return ret;
 }
diff --git a/libavformat/adp.c b/libavformat/adp.c
index 56f302acfd..8668c78fe4 100644
--- a/libavformat/adp.c
+++ b/libavformat/adp.c
@@ -78,7 +78,6 @@ static int adp_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     if (ret != size) {
         if (ret < 0) {
-            av_packet_unref(pkt);
             return ret;
         }
         av_shrink_packet(pkt, ret);
diff --git a/libavformat/adxdec.c b/libavformat/adxdec.c
index f80b4b80f0..ccd5049acd 100644
--- a/libavformat/adxdec.c
+++ b/libavformat/adxdec.c
@@ -65,11 +65,9 @@ static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = av_get_packet(s->pb, pkt, size);
     if (ret != size) {
-        av_packet_unref(pkt);
         return ret < 0 ? ret : AVERROR(EIO);
     }
     if (AV_RB16(pkt->data) & 0x8000) {
-        av_packet_unref(pkt);
         return AVERROR_EOF;
     }
     pkt->size     = size;
diff --git a/libavformat/amr.c b/libavformat/amr.c
index 42840a50a3..b19ad18e04 100644
--- a/libavformat/amr.c
+++ b/libavformat/amr.c
@@ -154,7 +154,6 @@ static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
     read              = avio_read(s->pb, pkt->data + 1, size - 1);
 
     if (read != size - 1) {
-        av_packet_unref(pkt);
         if (read < 0)
             return read;
         return AVERROR(EIO);
diff --git a/libavformat/ape.c b/libavformat/ape.c
index e31a00dc96..ed6752a415 100644
--- a/libavformat/ape.c
+++ b/libavformat/ape.c
@@ -419,7 +419,6 @@ static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
     AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
     ret = avio_read(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
     if (ret < 0) {
-        av_packet_unref(pkt);
         return ret;
     }
 
diff --git a/libavformat/avs.c b/libavformat/avs.c
index 47fa41017d..3a2d84aa19 100644
--- a/libavformat/avs.c
+++ b/libavformat/avs.c
@@ -114,7 +114,6 @@ avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
     pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
     ret = avio_read(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
     if (ret < size) {
-        av_packet_unref(pkt);
         return AVERROR(EIO);
     }
 
diff --git a/libavformat/brstm.c b/libavformat/brstm.c
index 1470690731..ca965ed7e1 100644
--- a/libavformat/brstm.c
+++ b/libavformat/brstm.c
@@ -422,8 +422,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
             dst += size;
             avio_skip(s->pb, skip);
             if (ret != size) {
-                av_packet_unref(pkt);
-                break;
+                return AVERROR(EIO);
             }
         }
         pkt->duration = samples;
diff --git a/libavformat/c93.c b/libavformat/c93.c
index 8aa80b5e0b..256b9800ca 100644
--- a/libavformat/c93.c
+++ b/libavformat/c93.c
@@ -158,22 +158,19 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = avio_read(pb, pkt->data + 1, datasize);
     if (ret < datasize) {
-        ret = AVERROR(EIO);
-        goto fail;
+        return AVERROR(EIO);
     }
 
     datasize = avio_rl16(pb); /* palette size */
     if (datasize) {
         if (datasize != 768) {
             av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize);
-            ret = AVERROR_INVALIDDATA;
-            goto fail;
+            return AVERROR_INVALIDDATA;
         }
         pkt->data[0] |= C93_HAS_PALETTE;
         ret = avio_read(pb, pkt->data + pkt->size, datasize);
         if (ret < datasize) {
-            ret = AVERROR(EIO);
-            goto fail;
+            return AVERROR(EIO);
         }
         pkt->size += 768;
     }
@@ -186,10 +183,6 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
         pkt->data[0] |= C93_FIRST_FRAME;
     }
     return 0;
-
-    fail:
-    av_packet_unref(pkt);
-    return ret;
 }
 
 AVInputFormat ff_c93_demuxer = {
diff --git a/libavformat/cdxl.c b/libavformat/cdxl.c
index 31f7cb823a..5718fc3e21 100644
--- a/libavformat/cdxl.c
+++ b/libavformat/cdxl.c
@@ -207,7 +207,6 @@ static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
         memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
         ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
         if (ret < 0) {
-            av_packet_unref(pkt);
             return ret;
         }
         av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
diff --git a/libavformat/concatdec.c b/libavformat/concatdec.c
index e826821241..2173911ce4 100644
--- a/libavformat/concatdec.c
+++ b/libavformat/concatdec.c
@@ -542,7 +542,6 @@ static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
         if (ret < 0) {
             av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb filter "
                    "failed to send input packet\n");
-            av_packet_unref(pkt);
             return ret;
         }
 
@@ -592,7 +591,6 @@ static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
         if (ret < 0)
             return ret;
         if ((ret = match_streams(avf)) < 0) {
-            av_packet_unref(pkt);
             return ret;
         }
         if (packet_after_outpoint(cat, pkt)) {
@@ -608,7 +606,7 @@ static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
         }
         break;
     }
-    if ((ret = filter_packet(avf, cs, pkt)))
+    if ((ret = filter_packet(avf, cs, pkt)) < 0)
         return ret;
 
     st = cat->avf->streams[pkt->stream_index];
diff --git a/libavformat/dfa.c b/libavformat/dfa.c
index d667cd6277..bd4ef2dafe 100644
--- a/libavformat/dfa.c
+++ b/libavformat/dfa.c
@@ -93,7 +93,6 @@ static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
         if (!first) {
             ret = av_append_packet(pb, pkt, 12);
             if (ret < 0) {
-                av_packet_unref(pkt);
                 return ret;
             }
         } else
@@ -101,7 +100,6 @@ 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);
-            av_packet_unref(pkt);
             return AVERROR(EIO);
         }
         if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
@@ -115,7 +113,6 @@ static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
         }
         ret = av_append_packet(pb, pkt, frame_size);
         if (ret < 0) {
-            av_packet_unref(pkt);
             return ret;
         }
     }
diff --git a/libavformat/dsicin.c b/libavformat/dsicin.c
index 244622ee39..b18f43b9a0 100644
--- a/libavformat/dsicin.c
+++ b/libavformat/dsicin.c
@@ -200,7 +200,6 @@ static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
 
         ret = avio_read(pb, &pkt->data[4], pkt_size);
         if (ret < 0) {
-            av_packet_unref(pkt);
             return ret;
         }
         if (ret < pkt_size)
diff --git a/libavformat/dss.c b/libavformat/dss.c
index d7f9cafe47..8e0b72fbe5 100644
--- a/libavformat/dss.c
+++ b/libavformat/dss.c
@@ -259,14 +259,12 @@ static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
     dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
 
     if (ctx->dss_sp_swap_byte < 0) {
-        ret = AVERROR(EAGAIN);
-        goto error_eof;
+        return AVERROR(EAGAIN);
     }
 
     return pkt->size;
 
 error_eof:
-    av_packet_unref(pkt);
     return ret < 0 ? ret : AVERROR_EOF;
 }
 
@@ -308,7 +306,6 @@ static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
         ret = avio_read(s->pb, pkt->data + offset,
                         size2 - offset);
         if (ret < size2 - offset) {
-            av_packet_unref(pkt);
             return ret < 0 ? ret : AVERROR_EOF;
         }
 
@@ -318,7 +315,6 @@ static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = avio_read(s->pb, pkt->data + offset, size - offset);
     if (ret < size - offset) {
-        av_packet_unref(pkt);
         return ret < 0 ? ret : AVERROR_EOF;
     }
 
diff --git a/libavformat/dxa.c b/libavformat/dxa.c
index 994078e633..27fa6afb6a 100644
--- a/libavformat/dxa.c
+++ b/libavformat/dxa.c
@@ -210,7 +210,6 @@ static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
             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){
-                av_packet_unref(pkt);
                 return AVERROR(EIO);
             }
             if(pal_size) memcpy(pkt->data, pal, pal_size);
diff --git a/libavformat/electronicarts.c b/libavformat/electronicarts.c
index c894663c29..2ee5e1b6fa 100644
--- a/libavformat/electronicarts.c
+++ b/libavformat/electronicarts.c
@@ -633,7 +633,6 @@ static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
             case AV_CODEC_ID_ADPCM_EA_R3:
                 if (pkt->size < 4) {
                     av_log(s, AV_LOG_ERROR, "Packet is too short\n");
-                    av_packet_unref(pkt);
                     return AVERROR_INVALIDDATA;
                 }
                 if (ea->audio_codec == AV_CODEC_ID_ADPCM_EA_R3)
@@ -736,8 +735,6 @@ get_video_packet:
         }
     }
 
-    if (ret < 0 && partial_packet)
-        av_packet_unref(pkt);
     if (ret >= 0 && hit_end && !packet_read)
         return AVERROR(EAGAIN);
 
diff --git a/libavformat/fitsdec.c b/libavformat/fitsdec.c
index c7d1edec60..e917fc2375 100644
--- a/libavformat/fitsdec.c
+++ b/libavformat/fitsdec.c
@@ -183,7 +183,6 @@ static int fits_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = av_bprint_finalize(&avbuf, &buf);
     if (ret < 0) {
-        av_packet_unref(pkt);
         return ret;
     }
 
@@ -192,7 +191,6 @@ static int fits_read_packet(AVFormatContext *s, AVPacket *pkt)
     av_freep(&buf);
     ret = avio_read(s->pb, pkt->data + pkt->size, size);
     if (ret < 0) {
-        av_packet_unref(pkt);
         return ret;
     }
 
diff --git a/libavformat/flic.c b/libavformat/flic.c
index 615d6b25c5..da7b732388 100644
--- a/libavformat/flic.c
+++ b/libavformat/flic.c
@@ -226,7 +226,6 @@ static int flic_read_packet(AVFormatContext *s,
             ret = avio_read(pb, pkt->data + FLIC_PREAMBLE_SIZE,
                 size - FLIC_PREAMBLE_SIZE);
             if (ret != size - FLIC_PREAMBLE_SIZE) {
-                av_packet_unref(pkt);
                 ret = AVERROR(EIO);
             }
             packet_read = 1;
@@ -244,8 +243,8 @@ static int flic_read_packet(AVFormatContext *s,
             ret = avio_read(pb, pkt->data, size);
 
             if (ret != size) {
-                av_packet_unref(pkt);
                 ret = AVERROR(EIO);
+                break;
             }
 
             packet_read = 1;
diff --git a/libavformat/g723_1.c b/libavformat/g723_1.c
index 27c8c3951b..3af4809347 100644
--- a/libavformat/g723_1.c
+++ b/libavformat/g723_1.c
@@ -69,7 +69,6 @@ static int g723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = avio_read(s->pb, pkt->data + 1, size - 1);
     if (ret < size - 1) {
-        av_packet_unref(pkt);
         return ret < 0 ? ret : AVERROR_EOF;
     }
 
diff --git a/libavformat/gdv.c b/libavformat/gdv.c
index b698497a6a..2ecbb535e7 100644
--- a/libavformat/gdv.c
+++ b/libavformat/gdv.c
@@ -182,7 +182,6 @@ static int gdv_read_packet(AVFormatContext *ctx, AVPacket *pkt)
             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
                                           AVPALETTE_SIZE);
             if (!pal) {
-                av_packet_unref(pkt);
                 return AVERROR(ENOMEM);
             }
             memcpy(pal, gdv->pal, AVPALETTE_SIZE);
diff --git a/libavformat/gsmdec.c b/libavformat/gsmdec.c
index 1044cde317..ec6b2e924f 100644
--- a/libavformat/gsmdec.c
+++ b/libavformat/gsmdec.c
@@ -62,7 +62,6 @@ static int gsm_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = av_get_packet(s->pb, pkt, size);
     if (ret < GSM_BLOCK_SIZE) {
-        av_packet_unref(pkt);
         return ret < 0 ? ret : AVERROR(EIO);
     }
     pkt->duration = 1;
diff --git a/libavformat/hls.c b/libavformat/hls.c
index 538af0dbf8..5766857303 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -2214,7 +2214,6 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
         if (ist->codecpar->codec_id != st->codecpar->codec_id) {
             ret = set_stream_info_from_input_stream(st, pls, ist);
             if (ret < 0) {
-                av_packet_unref(pkt);
                 return ret;
             }
         }
diff --git a/libavformat/icodec.c b/libavformat/icodec.c
index 98684e5e74..b47fa98f80 100644
--- a/libavformat/icodec.c
+++ b/libavformat/icodec.c
@@ -185,7 +185,6 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
         bytestream_put_le32(&buf, 0);
 
         if ((ret = avio_read(pb, buf, image->size)) != image->size) {
-            av_packet_unref(pkt);
             return ret < 0 ? ret : AVERROR_INVALIDDATA;
         }
 
diff --git a/libavformat/idcin.c b/libavformat/idcin.c
index 0b1058171b..5a6a15aa81 100644
--- a/libavformat/idcin.c
+++ b/libavformat/idcin.c
@@ -313,7 +313,6 @@ static int idcin_read_packet(AVFormatContext *s,
             return ret;
         else if (ret != chunk_size) {
             av_log(s, AV_LOG_ERROR, "incomplete packet\n");
-            av_packet_unref(pkt);
             return AVERROR(EIO);
         }
         if (command == 1) {
@@ -322,7 +321,6 @@ static int idcin_read_packet(AVFormatContext *s,
             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
                                           AVPALETTE_SIZE);
             if (!pal) {
-                av_packet_unref(pkt);
                 return AVERROR(ENOMEM);
             }
             memcpy(pal, palette, AVPALETTE_SIZE);
diff --git a/libavformat/idroqdec.c b/libavformat/idroqdec.c
index 1db4cce6f0..56ab83424f 100644
--- a/libavformat/idroqdec.c
+++ b/libavformat/idroqdec.c
@@ -223,8 +223,7 @@ static int roq_read_packet(AVFormatContext *s,
             ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
                 chunk_size);
             if (ret != chunk_size) {
-                av_packet_unref(pkt);
-                ret = AVERROR(EIO);
+                return AVERROR(EIO);
             }
 
             packet_read = 1;
diff --git a/libavformat/ilbc.c b/libavformat/ilbc.c
index ba11953b59..95a631158d 100644
--- a/libavformat/ilbc.c
+++ b/libavformat/ilbc.c
@@ -112,7 +112,6 @@ static int ilbc_read_packet(AVFormatContext *s,
     pkt->pos = avio_tell(s->pb);
     pkt->duration = par->block_align == 38 ? 160 : 240;
     if ((ret = avio_read(s->pb, pkt->data, par->block_align)) != par->block_align) {
-        av_packet_unref(pkt);
         return ret < 0 ? ret : AVERROR(EIO);
     }
 
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index f8b4a655a5..1739628bac 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -504,7 +504,6 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
     }
 
     if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
-        av_packet_unref(pkt);
         if (ret[0] < 0) {
             res = ret[0];
         } else if (ret[1] < 0) {
diff --git a/libavformat/iv8.c b/libavformat/iv8.c
index 449a422347..e25f24eeb9 100644
--- a/libavformat/iv8.c
+++ b/libavformat/iv8.c
@@ -92,7 +92,6 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
             ret = av_append_packet(s->pb, pkt, size);
             if (ret < 0) {
                 av_log(s, AV_LOG_ERROR, "failed to grow packet\n");
-                av_packet_unref(pkt);
                 return ret;
             }
         }
diff --git a/libavformat/libmodplug.c b/libavformat/libmodplug.c
index 6a32618e6f..6e567f5f98 100644
--- a/libavformat/libmodplug.c
+++ b/libavformat/libmodplug.c
@@ -327,7 +327,6 @@ static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     pkt->size = ModPlug_Read(modplug->f, pkt->data, AUDIO_PKT_SIZE);
     if (pkt->size <= 0) {
-        av_packet_unref(pkt);
         return pkt->size == 0 ? AVERROR_EOF : AVERROR(EIO);
     }
     return 0;
diff --git a/libavformat/lxfdec.c b/libavformat/lxfdec.c
index 434518fc59..fa84ceea78 100644
--- a/libavformat/lxfdec.c
+++ b/libavformat/lxfdec.c
@@ -316,7 +316,6 @@ static int lxf_read_packet(AVFormatContext *s, AVPacket *pkt)
         return ret2;
 
     if ((ret2 = avio_read(pb, pkt->data, ret)) != ret) {
-        av_packet_unref(pkt);
         return ret2 < 0 ? ret2 : AVERROR_EOF;
     }
 
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 890c6e85b8..3dcd458fd3 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -7882,7 +7882,6 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = cenc_filter(mov, st, sc, pkt, current_index);
     if (ret < 0) {
-        av_packet_unref(pkt);
         return ret;
     }
 
diff --git a/libavformat/mpc.c b/libavformat/mpc.c
index a7b2e116ed..85036cd118 100644
--- a/libavformat/mpc.c
+++ b/libavformat/mpc.c
@@ -169,7 +169,6 @@ static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
     if(c->curbits)
         avio_seek(s->pb, -4, SEEK_CUR);
     if(ret < size){
-        av_packet_unref(pkt);
         return ret < 0 ? ret : AVERROR(EIO);
     }
     pkt->size = ret + 4;
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 5c850bc1e5..a90900127c 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -3133,7 +3133,6 @@ static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
     ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
     pkt->pos = avio_tell(s->pb);
     if (ret < 0) {
-        av_packet_unref(pkt);
         return ret;
     }
     if (data != pkt->data)
diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c
index c79a39c69d..1e2ab0db1a 100644
--- a/libavformat/mpjpegdec.c
+++ b/libavformat/mpjpegdec.c
@@ -359,8 +359,6 @@ static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
         /* error or EOF occurred */
         if (ret == AVERROR_EOF) {
             ret = pkt->size > 0 ? pkt->size : AVERROR_EOF;
-        } else {
-            av_packet_unref(pkt);
         }
     }
 
diff --git a/libavformat/ncdec.c b/libavformat/ncdec.c
index bc3d3e82bf..f2066b485a 100644
--- a/libavformat/ncdec.c
+++ b/libavformat/ncdec.c
@@ -83,7 +83,6 @@ static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = av_get_packet(s->pb, pkt, size);
     if (ret != size) {
-        if (ret > 0) av_packet_unref(pkt);
         return AVERROR(EIO);
     }
 
diff --git a/libavformat/nuv.c b/libavformat/nuv.c
index bef0ae4860..d99770d41d 100644
--- a/libavformat/nuv.c
+++ b/libavformat/nuv.c
@@ -284,7 +284,6 @@ static int nuv_packet(AVFormatContext *s, AVPacket *pkt)
             memcpy(pkt->data, hdr, copyhdrsize);
             ret = avio_read(pb, pkt->data + copyhdrsize, size);
             if (ret < 0) {
-                av_packet_unref(pkt);
                 return ret;
             }
             if (ret < size)
diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index e815f42134..4f4b5fe386 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -851,7 +851,7 @@ retry:
                                                      AV_PKT_DATA_SKIP_SAMPLES,
                                                      10);
         if(!side_data)
-            goto fail;
+            return AVERROR(ENOMEM);
         AV_WL32(side_data + 4, os->end_trimming);
         os->end_trimming = 0;
     }
@@ -861,7 +861,7 @@ retry:
                                                      AV_PKT_DATA_METADATA_UPDATE,
                                                      os->new_metadata_size);
         if(!side_data)
-            goto fail;
+            return AVERROR(ENOMEM);
 
         memcpy(side_data, os->new_metadata, os->new_metadata_size);
         av_freep(&os->new_metadata);
@@ -869,9 +869,6 @@ retry:
     }
 
     return psize;
-fail:
-    av_packet_unref(pkt);
-    return AVERROR(ENOMEM);
 }
 
 static int64_t ogg_read_timestamp(AVFormatContext *s, int stream_index,
diff --git a/libavformat/redspark.c b/libavformat/redspark.c
index f1f2b3156d..0ce8915311 100644
--- a/libavformat/redspark.c
+++ b/libavformat/redspark.c
@@ -140,7 +140,6 @@ static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     ret = av_get_packet(s->pb, pkt, size);
     if (ret != size) {
-        av_packet_unref(pkt);
         return AVERROR(EIO);
     }
 
diff --git a/libavformat/rl2.c b/libavformat/rl2.c
index 9e10155838..cfde23a945 100644
--- a/libavformat/rl2.c
+++ b/libavformat/rl2.c
@@ -256,7 +256,6 @@ static int rl2_read_packet(AVFormatContext *s,
     /** fill the packet */
     ret = av_get_packet(pb, pkt, sample->size);
     if(ret != sample->size){
-        av_packet_unref(pkt);
         return AVERROR(EIO);
     }
 
diff --git a/libavformat/rpl.c b/libavformat/rpl.c
index 6afd373810..208c50f00c 100644
--- a/libavformat/rpl.c
+++ b/libavformat/rpl.c
@@ -338,7 +338,6 @@ static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
         if (ret < 0)
             return ret;
         if (ret != frame_size) {
-            av_packet_unref(pkt);
             return AVERROR(EIO);
         }
         pkt->duration = 1;
@@ -355,7 +354,6 @@ static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
         if (ret < 0)
             return ret;
         if (ret != index_entry->size) {
-            av_packet_unref(pkt);
             return AVERROR(EIO);
         }
 
diff --git a/libavformat/s337m.c b/libavformat/s337m.c
index 48ab66a6da..d7d9b4ce88 100644
--- a/libavformat/s337m.c
+++ b/libavformat/s337m.c
@@ -174,7 +174,6 @@ static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->pos = pos;
 
     if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
-        av_packet_unref(pkt);
         return AVERROR_EOF;
     }
 
@@ -186,7 +185,6 @@ static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
     if (!s->nb_streams) {
         AVStream *st = avformat_new_stream(s, NULL);
         if (!st) {
-            av_packet_unref(pkt);
             return AVERROR(ENOMEM);
         }
         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
diff --git a/libavformat/sapdec.c b/libavformat/sapdec.c
index f8bc0d281c..b3644b436b 100644
--- a/libavformat/sapdec.c
+++ b/libavformat/sapdec.c
@@ -225,7 +225,6 @@ static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
             int i = s->nb_streams;
             AVStream *st = avformat_new_stream(s, NULL);
             if (!st) {
-                av_packet_unref(pkt);
                 return AVERROR(ENOMEM);
             }
             st->id = i;
diff --git a/libavformat/sdr2.c b/libavformat/sdr2.c
index 8893f260d2..3743d59e58 100644
--- a/libavformat/sdr2.c
+++ b/libavformat/sdr2.c
@@ -95,7 +95,6 @@ static int sdr2_read_packet(AVFormatContext *s, AVPacket *pkt)
         memcpy(pkt->data, header, 24);
         ret = avio_read(s->pb, pkt->data + 24, next - 52);
         if (ret < 0) {
-            av_packet_unref(pkt);
             return ret;
         }
         av_shrink_packet(pkt, ret + 24);
diff --git a/libavformat/sierravmd.c b/libavformat/sierravmd.c
index 8c2322eda6..531fc41531 100644
--- a/libavformat/sierravmd.c
+++ b/libavformat/sierravmd.c
@@ -295,7 +295,6 @@ static int vmd_read_packet(AVFormatContext *s,
             frame->frame_size);
 
     if (ret != frame->frame_size) {
-        av_packet_unref(pkt);
         ret = AVERROR(EIO);
     }
     pkt->stream_index = frame->stream_index;
diff --git a/libavformat/siff.c b/libavformat/siff.c
index 56c5b33c76..f6815b2f26 100644
--- a/libavformat/siff.c
+++ b/libavformat/siff.c
@@ -220,7 +220,6 @@ static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
             if (c->gmcsize)
                 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
             if (avio_read(s->pb, pkt->data + 2 + c->gmcsize, size) != size) {
-                av_packet_unref(pkt);
                 return AVERROR_INVALIDDATA;
             }
             pkt->stream_index = 0;
diff --git a/libavformat/spdifdec.c b/libavformat/spdifdec.c
index d74f58d82b..1808fa9d65 100644
--- a/libavformat/spdifdec.c
+++ b/libavformat/spdifdec.c
@@ -197,15 +197,13 @@ int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
 
     if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
-        av_packet_unref(pkt);
         return AVERROR_EOF;
     }
     ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
 
     ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
                                      &offset, &codec_id);
-    if (ret) {
-        av_packet_unref(pkt);
+    if (ret < 0) {
         return ret;
     }
 
@@ -216,7 +214,6 @@ int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
         /* first packet, create a stream */
         AVStream *st = avformat_new_stream(s, NULL);
         if (!st) {
-            av_packet_unref(pkt);
             return AVERROR(ENOMEM);
         }
         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
diff --git a/libavformat/swfdec.c b/libavformat/swfdec.c
index a9358f09a9..9a0b27bd8c 100644
--- a/libavformat/swfdec.c
+++ b/libavformat/swfdec.c
@@ -399,7 +399,6 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
 
             if (linesize * height > pkt->size) {
                 res = AVERROR_INVALIDDATA;
-                av_packet_unref(pkt);
                 goto bitmap_end;
             }
 
@@ -489,7 +488,6 @@ bitmap_end_skip:
             if ((res = av_new_packet(pkt, len)) < 0)
                 return res;
             if (avio_read(pb, pkt->data, 4) != 4) {
-                av_packet_unref(pkt);
                 return AVERROR_INVALIDDATA;
             }
             if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
@@ -506,7 +504,6 @@ bitmap_end_skip:
             }
             if (res != pkt->size) {
                 if (res < 0) {
-                    av_packet_unref(pkt);
                     return res;
                 }
                 av_shrink_packet(pkt, res);
diff --git a/libavformat/thp.c b/libavformat/thp.c
index ee5c78b6d3..332ed79128 100644
--- a/libavformat/thp.c
+++ b/libavformat/thp.c
@@ -181,7 +181,6 @@ static int thp_read_packet(AVFormatContext *s,
         if (ret < 0)
             return ret;
         if (ret != size) {
-            av_packet_unref(pkt);
             return AVERROR(EIO);
         }
 
@@ -191,7 +190,6 @@ static int thp_read_packet(AVFormatContext *s,
         if (ret < 0)
             return ret;
         if (ret != thp->audiosize) {
-            av_packet_unref(pkt);
             return AVERROR(EIO);
         }
 
diff --git a/libavformat/vivo.c b/libavformat/vivo.c
index 9a07c43849..4039cd0fe5 100644
--- a/libavformat/vivo.c
+++ b/libavformat/vivo.c
@@ -273,32 +273,28 @@ restart:
     }
 
     if ((ret = av_get_packet(pb, pkt, vivo->length)) < 0)
-        goto fail;
+        return ret;
 
     // get next packet header
     if ((ret = vivo_get_packet_header(s)) < 0)
-        goto fail;
+        return ret;
 
     while (vivo->sequence == old_sequence &&
            (((vivo->type - 1) >> 1) == ((old_type - 1) >> 1))) {
         if (avio_feof(pb)) {
-            ret = AVERROR_EOF;
-            break;
+            return AVERROR_EOF;
         }
 
         if ((ret = av_append_packet(pb, pkt, vivo->length)) < 0)
-            break;
+            return ret;
 
         // get next packet header
         if ((ret = vivo_get_packet_header(s)) < 0)
-            break;
+            return ret;
     }
 
     pkt->stream_index = stream_index;
 
-fail:
-    if (ret < 0)
-        av_packet_unref(pkt);
     return ret;
 }
 
diff --git a/libavformat/vpk.c b/libavformat/vpk.c
index 255d6030b0..dd9aeb6101 100644
--- a/libavformat/vpk.c
+++ b/libavformat/vpk.c
@@ -93,9 +93,7 @@ static int vpk_read_packet(AVFormatContext *s, AVPacket *pkt)
             ret = avio_read(s->pb, pkt->data + i * size, size);
             avio_skip(s->pb, skip);
             if (ret != size) {
-                av_packet_unref(pkt);
-                ret = AVERROR(EIO);
-                break;
+                return AVERROR(EIO);
             }
         }
         pkt->stream_index = 0;
diff --git a/libavformat/vqf.c b/libavformat/vqf.c
index b43725b3c1..15660e9fd8 100644
--- a/libavformat/vqf.c
+++ b/libavformat/vqf.c
@@ -249,7 +249,6 @@ static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
     ret = avio_read(s->pb, pkt->data+2, size);
 
     if (ret != size) {
-        av_packet_unref(pkt);
         return AVERROR(EIO);
     }
 
diff --git a/libavformat/wvdec.c b/libavformat/wvdec.c
index f2bb4c60ba..0aa581534d 100644
--- a/libavformat/wvdec.c
+++ b/libavformat/wvdec.c
@@ -287,25 +287,21 @@ static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
     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) {
-        av_packet_unref(pkt);
         return AVERROR(EIO);
     }
     while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
         if ((ret = wv_read_block_header(s, s->pb)) < 0) {
-            av_packet_unref(pkt);
             return ret;
         }
 
         off = pkt->size;
         if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
-            av_packet_unref(pkt);
             return ret;
         }
         memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
 
         ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
         if (ret != wc->header.blocksize) {
-            av_packet_unref(pkt);
             return (ret < 0) ? ret : AVERROR_EOF;
         }
     }
diff --git a/libavformat/yuv4mpegdec.c b/libavformat/yuv4mpegdec.c
index eceb945bb1..b8e2980a3d 100644
--- a/libavformat/yuv4mpegdec.c
+++ b/libavformat/yuv4mpegdec.c
@@ -310,7 +310,6 @@ static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
     if (ret < 0)
         return ret;
     else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN) {
-        av_packet_unref(pkt);
         return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
     }
     pkt->stream_index = 0;
