diff --git a/libavformat/adxdec.c b/libavformat/adxdec.c
index 1038a0d67e..f80b4b80f0 100644
--- a/libavformat/adxdec.c
+++ b/libavformat/adxdec.c
@@ -83,7 +83,7 @@ static int adx_read_header(AVFormatContext *s)
 {
     ADXDemuxerContext *c = s->priv_data;
     AVCodecParameters *par;
-
+    int ret;
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
@@ -94,8 +94,8 @@ static int adx_read_header(AVFormatContext *s)
     c->header_size = avio_rb16(s->pb) + 4;
     avio_seek(s->pb, -4, SEEK_CUR);
 
-    if (ff_get_extradata(s, par, s->pb, c->header_size) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0)
+        return ret;
 
     if (par->extradata_size < 12) {
         av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
diff --git a/libavformat/afc.c b/libavformat/afc.c
index 542cb168fc..2da04eb5dc 100644
--- a/libavformat/afc.c
+++ b/libavformat/afc.c
@@ -31,6 +31,7 @@ static int afc_read_header(AVFormatContext *s)
 {
     AFCDemuxContext *c = s->priv_data;
     AVStream *st;
+    int ret;
 
     st = avformat_new_stream(s, NULL);
     if (!st)
@@ -40,8 +41,8 @@ static int afc_read_header(AVFormatContext *s)
     st->codecpar->channels   = 2;
     st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
 
-    if (ff_alloc_extradata(st->codecpar, 1))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, 1)) < 0)
+        return ret;
     st->codecpar->extradata[0] = 8 * st->codecpar->channels;
 
     c->data_end = avio_rb32(s->pb) + 32LL;
diff --git a/libavformat/aiffdec.c b/libavformat/aiffdec.c
index a42987c15f..cb2f1b60fb 100644
--- a/libavformat/aiffdec.c
+++ b/libavformat/aiffdec.c
@@ -301,8 +301,8 @@ static int aiff_read_header(AVFormatContext *s)
         case MKTAG('w', 'a', 'v', 'e'):
             if ((uint64_t)size > (1<<30))
                 return -1;
-            if (ff_get_extradata(s, st->codecpar, pb, size) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
+                return ret;
             if (   (st->codecpar->codec_id == AV_CODEC_ID_QDMC || st->codecpar->codec_id == AV_CODEC_ID_QDM2)
                 && size>=12*4 && !st->codecpar->block_align) {
                 st->codecpar->block_align = AV_RB32(st->codecpar->extradata+11*4);
@@ -325,8 +325,8 @@ static int aiff_read_header(AVFormatContext *s)
             }
             break;
         case MKTAG('C','H','A','N'):
-            if(ff_mov_read_chan(s, pb, st, size) < 0)
-                return AVERROR_INVALIDDATA;
+            if ((ret = ff_mov_read_chan(s, pb, st, size)) < 0)
+                return ret;
             break;
         case MKTAG('A','P','C','M'): /* XA ADPCM compressed sound chunk */
             st->codecpar->codec_id = AV_CODEC_ID_ADPCM_XA;
diff --git a/libavformat/apc.c b/libavformat/apc.c
index 571726affb..7210bfbb56 100644
--- a/libavformat/apc.c
+++ b/libavformat/apc.c
@@ -37,6 +37,7 @@ static int apc_read_header(AVFormatContext *s)
 {
     AVIOContext *pb = s->pb;
     AVStream *st;
+    int ret;
 
     avio_rl32(pb); /* CRYO */
     avio_rl32(pb); /* _APC */
@@ -53,8 +54,8 @@ static int apc_read_header(AVFormatContext *s)
     st->codecpar->sample_rate = avio_rl32(pb);
 
     /* initial predictor values for adpcm decoder */
-    if (ff_get_extradata(s, st->codecpar, pb, 2 * 4) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = ff_get_extradata(s, st->codecpar, pb, 2 * 4)) < 0)
+        return ret;
 
     if (avio_rl32(pb)) {
         st->codecpar->channels       = 2;
diff --git a/libavformat/ape.c b/libavformat/ape.c
index 977e6f3d18..e31a00dc96 100644
--- a/libavformat/ape.c
+++ b/libavformat/ape.c
@@ -163,7 +163,7 @@ static int ape_read_header(AVFormatContext * s)
     APEContext *ape = s->priv_data;
     AVStream *st;
     uint32_t tag;
-    int i;
+    int i, ret;
     int total_blocks, final_size = 0;
     int64_t pts, file_size;
 
@@ -358,8 +358,8 @@ static int ape_read_header(AVFormatContext * s)
     st->duration  = total_blocks;
     avpriv_set_pts_info(st, 64, 1, ape->samplerate);
 
-    if (ff_alloc_extradata(st->codecpar, APE_EXTRADATA_SIZE))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, APE_EXTRADATA_SIZE)) < 0)
+        return ret;
     AV_WL16(st->codecpar->extradata + 0, ape->fileversion);
     AV_WL16(st->codecpar->extradata + 2, ape->compressiontype);
     AV_WL16(st->codecpar->extradata + 4, ape->formatflags);
@@ -386,14 +386,16 @@ static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
     int nblocks;
     APEContext *ape = s->priv_data;
     uint32_t extra_size = 8;
+    int64_t ret64;
 
     if (avio_feof(s->pb))
         return AVERROR_EOF;
     if (ape->currentframe >= ape->totalframes)
         return AVERROR_EOF;
 
-    if (avio_seek(s->pb, ape->frames[ape->currentframe].pos, SEEK_SET) < 0)
-        return AVERROR(EIO);
+    ret64 = avio_seek(s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
+    if (ret64 < 0)
+        return ret64;
 
     /* Calculate how many blocks there are in this frame */
     if (ape->currentframe == (ape->totalframes - 1))
@@ -409,8 +411,9 @@ static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
         return AVERROR(EIO);
     }
 
-    if (av_new_packet(pkt,  ape->frames[ape->currentframe].size + extra_size) < 0)
-        return AVERROR(ENOMEM);
+    ret = av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size);
+    if (ret < 0)
+        return ret;
 
     AV_WL32(pkt->data    , nblocks);
     AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
@@ -447,12 +450,13 @@ static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
     AVStream *st = s->streams[stream_index];
     APEContext *ape = s->priv_data;
     int index = av_index_search_timestamp(st, timestamp, flags);
+    int64_t ret;
 
     if (index < 0)
         return -1;
 
-    if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
-        return -1;
+    if ((ret = avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET)) < 0)
+        return ret;
     ape->currentframe = index;
     return 0;
 }
diff --git a/libavformat/apetag.c b/libavformat/apetag.c
index 4e19f49bf1..454c6c688b 100644
--- a/libavformat/apetag.c
+++ b/libavformat/apetag.c
@@ -96,8 +96,8 @@ static int ape_tag_read_field(AVFormatContext *s)
             st->attached_pic.stream_index = st->index;
             st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
         } else {
-            if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
+                return ret;
             st->codecpar->codec_type = AVMEDIA_TYPE_ATTACHMENT;
         }
     } else {
diff --git a/libavformat/asfenc.c b/libavformat/asfenc.c
index 9f54173bf9..44e11fc763 100644
--- a/libavformat/asfenc.c
+++ b/libavformat/asfenc.c
@@ -357,12 +357,12 @@ static int asf_write_markers(AVFormatContext *s)
         int64_t pres_time = av_rescale_q(c->start, c->time_base, scale);
         uint64_t offset;
         int32_t send_time = get_send_time(asf, pres_time, &offset);
-        int len = 0;
+        int len = 0, ret;
         uint8_t *buf;
         AVIOContext *dyn_buf;
         if (t) {
-            if (avio_open_dyn_buf(&dyn_buf) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
+                return ret;
             avio_put_str16le(dyn_buf, t->value);
             len = avio_close_dyn_buf(dyn_buf, &buf);
         }
@@ -579,12 +579,12 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
 
     /* title and other info */
     if (has_title) {
-        int len;
+        int len, ret;
         uint8_t *buf;
         AVIOContext *dyn_buf;
 
-        if (avio_open_dyn_buf(&dyn_buf) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
+            return ret;
 
         hpos = put_header(pb, &ff_asf_comment_header);
 
@@ -714,10 +714,10 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
         if (desc) {
             AVIOContext *dyn_buf;
             uint8_t *buf;
-            int len;
+            int len, ret;
 
-            if (avio_open_dyn_buf(&dyn_buf) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
+                return ret;
 
             avio_put_str16le(dyn_buf, desc);
             len = avio_close_dyn_buf(dyn_buf, &buf);
diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index 5cf67a4771..21871feb8d 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -772,8 +772,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
                             av_log(s, AV_LOG_WARNING, "New extradata in strf chunk, freeing previous one.\n");
                             av_freep(&st->codecpar->extradata);
                         }
-                        if (ff_get_extradata(s, st->codecpar, pb, st->codecpar->extradata_size) < 0)
-                            return AVERROR(ENOMEM);
+                        ret = ff_get_extradata(s, st->codecpar, pb,
+                                               st->codecpar->extradata_size);
+                        if (ret < 0)
+                            return ret;
                     }
 
                     // FIXME: check if the encoder really did this correctly
@@ -932,8 +934,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
                         av_log(s, AV_LOG_WARNING, "New extradata in strd chunk, freeing previous one.\n");
                         av_freep(&st->codecpar->extradata);
                     }
-                    if (ff_get_extradata(s, st->codecpar, pb, size) < 0)
-                        return AVERROR(ENOMEM);
+                    if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
+                        return ret;
                 }
 
                 if (st->codecpar->extradata_size & 1) //FIXME check if the encoder really did this correctly
diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
index 5dfe94ae0c..55a2efd884 100644
--- a/libavformat/avisynth.c
+++ b/libavformat/avisynth.c
@@ -640,7 +640,7 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
     AVS_VideoFrame *frame;
     unsigned char *dst_p;
     const unsigned char *src_p;
-    int n, i, plane, rowsize, planeheight, pitch, bits;
+    int n, i, plane, rowsize, planeheight, pitch, bits, ret;
     const char *error;
     int avsplus av_unused;
 
@@ -676,8 +676,8 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
     if (!pkt->size)
         return AVERROR_UNKNOWN;
 
-    if (av_new_packet(pkt, pkt->size) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(pkt, pkt->size)) < 0)
+        return ret;
 
     pkt->pts      = n;
     pkt->dts      = n;
@@ -739,7 +739,7 @@ static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
 {
     AviSynthContext *avs = s->priv_data;
     AVRational fps, samplerate;
-    int samples;
+    int samples, ret;
     int64_t n;
     const char *error;
 
@@ -782,8 +782,8 @@ static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
     if (!pkt->size)
         return AVERROR_UNKNOWN;
 
-    if (av_new_packet(pkt, pkt->size) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(pkt, pkt->size)) < 0)
+        return ret;
 
     pkt->pts      = n;
     pkt->dts      = n;
diff --git a/libavformat/bink.c b/libavformat/bink.c
index 631b8c4d7d..a1422e3f35 100644
--- a/libavformat/bink.c
+++ b/libavformat/bink.c
@@ -150,8 +150,8 @@ static int read_header(AVFormatContext *s)
         vst->codecpar->codec_id = AV_CODEC_ID_NONE;
     }
 
-    if (ff_get_extradata(s, vst->codecpar, pb, 4) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = ff_get_extradata(s, vst->codecpar, pb, 4)) < 0)
+        return ret;
 
     bink->num_audio_tracks = avio_rl32(pb);
 
@@ -190,8 +190,8 @@ static int read_header(AVFormatContext *s)
                 ast->codecpar->channels       = 1;
                 ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
             }
-            if (ff_alloc_extradata(ast->codecpar, 4))
-                return AVERROR(ENOMEM);
+            if ((ret = ff_alloc_extradata(ast->codecpar, 4)) < 0)
+                return ret;
             AV_WL32(ast->codecpar->extradata, vst->codecpar->codec_tag);
         }
 
@@ -302,13 +302,15 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, in
 {
     BinkDemuxContext *bink = s->priv_data;
     AVStream *vst = s->streams[0];
+    int64_t ret;
 
     if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
         return -1;
 
     /* seek to the first frame */
-    if (avio_seek(s->pb, vst->index_entries[0].pos + bink->smush_size, SEEK_SET) < 0)
-        return -1;
+    ret = avio_seek(s->pb, vst->index_entries[0].pos + bink->smush_size, SEEK_SET);
+    if (ret < 0)
+        return ret;
 
     bink->video_pts = 0;
     memset(bink->audio_pts, 0, sizeof(bink->audio_pts));
diff --git a/libavformat/bintext.c b/libavformat/bintext.c
index 61b89f56d6..7dab5f377d 100644
--- a/libavformat/bintext.c
+++ b/libavformat/bintext.c
@@ -177,14 +177,14 @@ static int bintext_read_header(AVFormatContext *s)
 {
     BinDemuxContext *bin = s->priv_data;
     AVIOContext *pb = s->pb;
-
+    int ret;
     AVStream *st = init_stream(s);
     if (!st)
         return AVERROR(ENOMEM);
     st->codecpar->codec_id    = AV_CODEC_ID_BINTEXT;
 
-    if (ff_alloc_extradata(st->codecpar, 2))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
+        return ret;
     st->codecpar->extradata[0] = 16;
     st->codecpar->extradata[1] = 0;
 
@@ -222,7 +222,7 @@ static int xbin_read_header(AVFormatContext *s)
     BinDemuxContext *bin = s->priv_data;
     AVIOContext *pb = s->pb;
     char fontheight, flags;
-
+    int ret;
     AVStream *st = init_stream(s);
     if (!st)
         return AVERROR(ENOMEM);
@@ -241,8 +241,9 @@ static int xbin_read_header(AVFormatContext *s)
         st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
     st->codecpar->codec_id    = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
 
-    if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size))
-        return AVERROR(ENOMEM);
+    ret = ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size);
+    if (ret < 0)
+        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)
@@ -264,6 +265,7 @@ static int adf_read_header(AVFormatContext *s)
     BinDemuxContext *bin = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *st;
+    int ret;
 
     if (avio_r8(pb) != 1)
         return AVERROR_INVALIDDATA;
@@ -273,8 +275,8 @@ static int adf_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     st->codecpar->codec_id    = AV_CODEC_ID_BINTEXT;
 
-    if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
+        return ret;
     st->codecpar->extradata[0] = 16;
     st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
 
@@ -318,7 +320,7 @@ static int idf_read_header(AVFormatContext *s)
     BinDemuxContext *bin = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *st;
-    int got_width = 0;
+    int got_width = 0, ret;
 
     if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
         return AVERROR(EIO);
@@ -328,8 +330,8 @@ static int idf_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     st->codecpar->codec_id    = AV_CODEC_ID_IDF;
 
-    if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
+        return ret;
     st->codecpar->extradata[0] = 16;
     st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
 
diff --git a/libavformat/bit.c b/libavformat/bit.c
index 0aacfc7c38..2dc7d4f3f7 100644
--- a/libavformat/bit.c
+++ b/libavformat/bit.c
@@ -94,8 +94,8 @@ static int read_packet(AVFormatContext *s,
     if(ret != 8 * packet_size * sizeof(uint16_t))
         return AVERROR(EIO);
 
-    if (av_new_packet(pkt, packet_size) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(pkt, packet_size)) < 0)
+        return ret;
 
     init_put_bits(&pbo, pkt->data, packet_size);
     for(j=0; j < packet_size; j++)
diff --git a/libavformat/bmv.c b/libavformat/bmv.c
index ac567c21eb..9f03fba058 100644
--- a/libavformat/bmv.c
+++ b/libavformat/bmv.c
@@ -96,8 +96,8 @@ static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
                        audio_size, c->size);
                 return AVERROR_INVALIDDATA;
             }
-            if (av_new_packet(pkt, audio_size) < 0)
-                return AVERROR(ENOMEM);
+            if ((err = av_new_packet(pkt, audio_size)) < 0)
+                return err;
             memcpy(pkt->data, c->packet + 1, pkt->size);
             pkt->stream_index = 1;
             pkt->pts          = c->audio_pos;
@@ -108,8 +108,8 @@ static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
         } else
             break;
     }
-    if (av_new_packet(pkt, c->size + 1) < 0)
-        return AVERROR(ENOMEM);
+    if ((err = av_new_packet(pkt, c->size + 1)) < 0)
+        return err;
     pkt->stream_index = 0;
     c->get_next = 1;
     memcpy(pkt->data, c->packet, pkt->size);
diff --git a/libavformat/brstm.c b/libavformat/brstm.c
index e8a1eaa022..1470690731 100644
--- a/libavformat/brstm.c
+++ b/libavformat/brstm.c
@@ -403,8 +403,8 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
             (32 + 4 + size) > (INT_MAX / par->channels) ||
             (32 + 4 + size) * par->channels > INT_MAX - 8)
             return AVERROR_INVALIDDATA;
-        if (av_new_packet(pkt, 8 + (32 + 4 + size) * par->channels) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, 8 + (32 + 4 + size) * par->channels)) < 0)
+            return ret;
         dst = pkt->data;
         if (par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
             bytestream_put_le32(&dst, size * par->channels);
diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c
index 86228595c9..85356ec439 100644
--- a/libavformat/cafdec.c
+++ b/libavformat/cafdec.c
@@ -100,6 +100,7 @@ static int read_kuki_chunk(AVFormatContext *s, int64_t size)
 {
     AVIOContext *pb = s->pb;
     AVStream *st      = s->streams[0];
+    int ret;
 
     if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
         return -1;
@@ -135,8 +136,8 @@ static int read_kuki_chunk(AVFormatContext *s, int64_t size)
         }
 
         av_freep(&st->codecpar->extradata);
-        if (ff_alloc_extradata(st->codecpar, ALAC_HEADER))
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(st->codecpar, ALAC_HEADER)) < 0)
+            return ret;
 
         /* For the old style cookie, we skip 12 bytes, then read 36 bytes.
          * The new style cookie only contains the last 24 bytes of what was
@@ -176,8 +177,8 @@ static int read_kuki_chunk(AVFormatContext *s, int64_t size)
         avio_skip(pb, size);
     } else {
         av_freep(&st->codecpar->extradata);
-        if (ff_get_extradata(s, st->codecpar, pb, size) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
+            return ret;
     }
 
     return 0;
diff --git a/libavformat/cdxl.c b/libavformat/cdxl.c
index e675b2c8f1..31f7cb823a 100644
--- a/libavformat/cdxl.c
+++ b/libavformat/cdxl.c
@@ -202,8 +202,8 @@ static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
                 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
         }
 
-        if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, video_size + CDXL_HEADER_SIZE)) < 0)
+            return ret;
         memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
         ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
         if (ret < 0) {
diff --git a/libavformat/dfa.c b/libavformat/dfa.c
index a5ecfd9b15..d667cd6277 100644
--- a/libavformat/dfa.c
+++ b/libavformat/dfa.c
@@ -40,7 +40,7 @@ static int dfa_read_header(AVFormatContext *s)
 {
     AVIOContext *pb = s->pb;
     AVStream *st;
-    int frames;
+    int frames, ret;
     int version;
     uint32_t mspf;
 
@@ -69,8 +69,8 @@ static int dfa_read_header(AVFormatContext *s)
     avio_skip(pb, 128 - 16); // padding
     st->duration = frames;
 
-    if (ff_alloc_extradata(st->codecpar, 2))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
+        return ret;
     AV_WL16(st->codecpar->extradata, version);
     if (version == 0x100)
         st->sample_aspect_ratio = (AVRational){2, 1};
diff --git a/libavformat/dsfdec.c b/libavformat/dsfdec.c
index 9be206af84..52cddab2c8 100644
--- a/libavformat/dsfdec.c
+++ b/libavformat/dsfdec.c
@@ -169,8 +169,8 @@ static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
             if (packet_size <= 0 || skip_size <= 0)
                 return AVERROR_INVALIDDATA;
 
-            if (av_new_packet(pkt, packet_size) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = av_new_packet(pkt, packet_size)) < 0)
+                return ret;
             dst = pkt->data;
             for (ch = 0; ch < st->codecpar->channels; ch++) {
                 ret = avio_read(pb, dst,  packet_size / st->codecpar->channels);
diff --git a/libavformat/dxa.c b/libavformat/dxa.c
index 298cda05d7..994078e633 100644
--- a/libavformat/dxa.c
+++ b/libavformat/dxa.c
@@ -179,8 +179,8 @@ static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
         tag = AV_RL32(buf);
         switch (tag) {
         case MKTAG('N', 'U', 'L', 'L'):
-            if(av_new_packet(pkt, 4 + pal_size) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = av_new_packet(pkt, 4 + pal_size)) < 0)
+                return ret;
             pkt->stream_index = 0;
             if(pal_size) memcpy(pkt->data, pal, pal_size);
             memcpy(pkt->data + pal_size, buf, 4);
@@ -204,8 +204,9 @@ static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
                        size);
                 return AVERROR_INVALIDDATA;
             }
-            if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
-                return AVERROR(ENOMEM);
+            ret = av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size);
+            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){
diff --git a/libavformat/flic.c b/libavformat/flic.c
index d7844ce04f..4ba72fb037 100644
--- a/libavformat/flic.c
+++ b/libavformat/flic.c
@@ -89,7 +89,7 @@ static int flic_read_header(AVFormatContext *s)
     AVIOContext *pb = s->pb;
     unsigned char header[FLIC_HEADER_SIZE];
     AVStream *st, *ast;
-    int speed;
+    int speed, ret;
     int magic_number;
     unsigned char preamble[FLIC_PREAMBLE_SIZE];
 
@@ -125,8 +125,8 @@ static int flic_read_header(AVFormatContext *s)
     }
 
     /* send over the whole 128-byte FLIC header */
-    if (ff_alloc_extradata(st->codecpar, FLIC_HEADER_SIZE))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, FLIC_HEADER_SIZE)) < 0)
+        return ret;
     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 */
@@ -176,8 +176,8 @@ static int flic_read_header(AVFormatContext *s)
 
         /* send over abbreviated FLIC header chunk */
         av_freep(&st->codecpar->extradata);
-        if (ff_alloc_extradata(st->codecpar, 12))
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(st->codecpar, 12)) < 0)
+            return ret;
         memcpy(st->codecpar->extradata, header, 12);
 
     } else if (magic_number == FLIC_FILE_MAGIC_1) {
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 6bfe6248d6..03b0c22f9d 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -795,12 +795,13 @@ static int flv_read_close(AVFormatContext *s)
 
 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
 {
+    int ret;
     if (!size)
         return 0;
 
     av_freep(&st->codecpar->extradata);
-    if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
+        return ret;
     st->internal->need_context_update = 1;
     return 0;
 }
diff --git a/libavformat/fsb.c b/libavformat/fsb.c
index faad6b16fd..fd3e484371 100644
--- a/libavformat/fsb.c
+++ b/libavformat/fsb.c
@@ -41,6 +41,7 @@ static int fsb_read_header(AVFormatContext *s)
     int64_t offset;
     AVCodecParameters *par;
     AVStream *st = avformat_new_stream(s, NULL);
+    int ret;
 
     avio_skip(pb, 3); // "FSB"
     version = avio_r8(pb) - '0';
@@ -86,9 +87,9 @@ static int fsb_read_header(AVFormatContext *s)
             par->block_align = 8 * par->channels;
             if (par->channels > INT_MAX / 32)
                 return AVERROR_INVALIDDATA;
-            ff_alloc_extradata(par, 32 * par->channels);
-            if (!par->extradata)
-                return AVERROR(ENOMEM);
+            ret = ff_alloc_extradata(par, 32 * par->channels);
+            if (ret < 0)
+                return ret;
             avio_seek(pb, 0x68, SEEK_SET);
             for (c = 0; c < par->channels; c++) {
                 avio_read(pb, par->extradata + 32 * c, 32);
@@ -130,18 +131,18 @@ static int fsb_read_header(AVFormatContext *s)
 
         switch (par->codec_id) {
         case AV_CODEC_ID_XMA2:
-            ff_alloc_extradata(par, 34);
-            if (!par->extradata)
-                return AVERROR(ENOMEM);
+            ret = ff_alloc_extradata(par, 34);
+            if (ret < 0)
+                return ret;
             memset(par->extradata, 0, 34);
             par->block_align = 2048;
             break;
         case AV_CODEC_ID_ADPCM_THP:
             if (par->channels > INT_MAX / 32)
                 return AVERROR_INVALIDDATA;
-            ff_alloc_extradata(par, 32 * par->channels);
-            if (!par->extradata)
-                return AVERROR(ENOMEM);
+            ret = ff_alloc_extradata(par, 32 * par->channels);
+            if (ret < 0)
+                return ret;
             avio_seek(pb, 0x80, SEEK_SET);
             for (c = 0; c < par->channels; c++) {
                 avio_read(pb, par->extradata + 32 * c, 32);
diff --git a/libavformat/id3v2enc.c b/libavformat/id3v2enc.c
index 9040501869..ee0c4b28a1 100644
--- a/libavformat/id3v2enc.c
+++ b/libavformat/id3v2enc.c
@@ -65,11 +65,11 @@ static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
 static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
                           uint32_t tag, enum ID3v2Encoding enc)
 {
-    int len;
+    int len, ret;
     uint8_t *pb;
     AVIOContext *dyn_buf;
-    if (avio_open_dyn_buf(&dyn_buf) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
+        return ret;
 
     /* check if the strings are ASCII-only and use UTF16 only if
      * they're not */
@@ -103,7 +103,7 @@ static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *
  */
 static int id3v2_put_priv(ID3v2EncContext *id3, AVIOContext *avioc, const char *key, const char *data)
 {
-    int len;
+    int len, ret;
     uint8_t *pb;
     AVIOContext *dyn_buf;
 
@@ -111,8 +111,8 @@ static int id3v2_put_priv(ID3v2EncContext *id3, AVIOContext *avioc, const char *
         return 0;
     }
 
-    if (avio_open_dyn_buf(&dyn_buf) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
+        return ret;
 
     // owner + null byte.
     avio_write(dyn_buf, key, strlen(key) + 1);
@@ -359,7 +359,7 @@ int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
     const char  *mimetype = NULL, *desc = "";
     int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
                                   ID3v2_ENCODING_UTF8;
-    int i, len, type = 0;
+    int i, len, type = 0, ret;
 
     /* get the mimetype*/
     while (mime->id != AV_CODEC_ID_NONE) {
@@ -393,8 +393,8 @@ int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
         enc = ID3v2_ENCODING_ISO8859;
 
     /* start writing */
-    if (avio_open_dyn_buf(&dyn_buf) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
+        return ret;
 
     avio_w8(dyn_buf, enc);
     avio_put_str(dyn_buf, mimetype);
diff --git a/libavformat/jvdec.c b/libavformat/jvdec.c
index 17ada7b0f1..551f8069e6 100644
--- a/libavformat/jvdec.c
+++ b/libavformat/jvdec.c
@@ -168,6 +168,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     JVDemuxContext *jv = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *ast = s->streams[0];
+    int ret;
 
     while (!avio_feof(s->pb) && jv->pts < ast->nb_index_entries) {
         const AVIndexEntry *e   = ast->index_entries + jv->pts;
@@ -177,8 +178,8 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
         case JV_AUDIO:
             jv->state++;
             if (jvf->audio_size) {
-                if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
-                    return AVERROR(ENOMEM);
+                if ((ret = av_get_packet(s->pb, pkt, jvf->audio_size)) < 0)
+                    return ret;
                 pkt->stream_index = 0;
                 pkt->pts          = e->timestamp;
                 pkt->flags       |= AV_PKT_FLAG_KEY;
@@ -187,10 +188,9 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
         case JV_VIDEO:
             jv->state++;
             if (jvf->video_size || jvf->palette_size) {
-                int ret;
                 int size = jvf->video_size + jvf->palette_size;
-                if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
-                    return AVERROR(ENOMEM);
+                if ((ret = av_new_packet(pkt, size + JV_PREAMBLE_SIZE)) < 0)
+                    return ret;
 
                 AV_WL32(pkt->data, jvf->video_size);
                 pkt->data[4] = jvf->video_type;
diff --git a/libavformat/libmodplug.c b/libavformat/libmodplug.c
index d4f78d99b1..6a32618e6f 100644
--- a/libavformat/libmodplug.c
+++ b/libavformat/libmodplug.c
@@ -270,6 +270,7 @@ static void write_text(uint8_t *dst, const char *s, int linesize, int x, int y)
 static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     ModPlugContext *modplug = s->priv_data;
+    int ret;
 
     if (modplug->video_stream) {
         modplug->video_switch ^= 1; // one video packet for one audio packet
@@ -285,8 +286,8 @@ static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
             var_values[VAR_PATTERN] = ModPlug_GetCurrentPattern(modplug->f);
             var_values[VAR_ROW    ] = ModPlug_GetCurrentRow    (modplug->f);
 
-            if (av_new_packet(pkt, modplug->fsize) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = av_new_packet(pkt, modplug->fsize)) < 0)
+                return ret;
             pkt->stream_index = 1;
             memset(pkt->data, 0, modplug->fsize);
 
@@ -318,8 +319,8 @@ static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
         }
     }
 
-    if (av_new_packet(pkt, AUDIO_PKT_SIZE) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(pkt, AUDIO_PKT_SIZE)) < 0)
+        return ret;
 
     if (modplug->video_stream)
         pkt->pts = pkt->dts = modplug->packet_count++ * modplug->ts_per_packet;
diff --git a/libavformat/mm.c b/libavformat/mm.c
index 83b3c200c6..d40fd12acc 100644
--- a/libavformat/mm.c
+++ b/libavformat/mm.c
@@ -142,6 +142,7 @@ static int read_packet(AVFormatContext *s,
     AVIOContext *pb = s->pb;
     unsigned char preamble[MM_PREAMBLE_SIZE];
     unsigned int type, length;
+    int ret;
 
     while(1) {
 
@@ -161,8 +162,8 @@ static int read_packet(AVFormatContext *s,
         case MM_TYPE_INTRA_HHV :
         case MM_TYPE_INTER_HHV :
             /* output preamble + data */
-            if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
-                return AVERROR(ENOMEM);
+            if ((ret = av_new_packet(pkt, length + MM_PREAMBLE_SIZE)) < 0)
+                return ret;
             memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
             if (avio_read(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
                 return AVERROR(EIO);
@@ -174,8 +175,8 @@ static int read_packet(AVFormatContext *s,
             return 0;
 
         case MM_TYPE_AUDIO :
-            if (av_get_packet(s->pb, pkt, length)<0)
-                return AVERROR(ENOMEM);
+            if ((ret = av_get_packet(s->pb, pkt, length)) < 0)
+                return ret;
             pkt->stream_index = 1;
             pkt->pts = mm->audio_pts++;
             return 0;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 733f4e4a34..dd903f71a0 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -6673,6 +6673,7 @@ static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPa
 static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 {
     const int OPUS_SEEK_PREROLL_MS = 80;
+    int ret;
     AVStream *st;
     size_t size;
     uint16_t pre_skip;
@@ -6693,8 +6694,8 @@ static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     /* OpusSpecificBox size plus magic for Ogg OpusHead header. */
     size = atom.size + 8;
 
-    if (ff_alloc_extradata(st->codecpar, size))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
+        return ret;
 
     AV_WL32(st->codecpar->extradata, MKTAG('O','p','u','s'));
     AV_WL32(st->codecpar->extradata + 4, MKTAG('H','e','a','d'));
diff --git a/libavformat/mpc8.c b/libavformat/mpc8.c
index e452cd6878..dd13bbd0a4 100644
--- a/libavformat/mpc8.c
+++ b/libavformat/mpc8.c
@@ -212,7 +212,7 @@ static int mpc8_read_header(AVFormatContext *s)
     MPCContext *c = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *st;
-    int tag = 0;
+    int tag = 0, ret;
     int64_t size, pos;
 
     c->header_pos = avio_tell(pb);
@@ -253,8 +253,8 @@ static int mpc8_read_header(AVFormatContext *s)
     st->codecpar->codec_id = AV_CODEC_ID_MUSEPACK8;
     st->codecpar->bits_per_coded_sample = 16;
 
-    if (ff_get_extradata(s, st->codecpar, pb, 2) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = ff_get_extradata(s, st->codecpar, pb, 2)) < 0)
+        return ret;
 
     st->codecpar->channels = (st->codecpar->extradata[1] >> 4) + 1;
     st->codecpar->sample_rate = mpc8_rate[st->codecpar->extradata[0] >> 5];
@@ -277,7 +277,7 @@ static int mpc8_read_header(AVFormatContext *s)
 static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     MPCContext *c = s->priv_data;
-    int tag;
+    int tag, ret;
     int64_t pos, size;
 
     while(!avio_feof(s->pb)){
@@ -291,8 +291,8 @@ static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
         if (size < 0)
             return -1;
         if(tag == TAG_AUDIOPACKET){
-            if(av_get_packet(s->pb, pkt, size) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
+                return ret;
             pkt->stream_index = 0;
             pkt->duration     = 1;
             return 0;
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 587ed33327..5c850bc1e5 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -1845,7 +1845,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
     case 0x56: /* DVB teletext descriptor */
         {
             uint8_t *extradata = NULL;
-            int language_count = desc_len / 5;
+            int language_count = desc_len / 5, ret;
 
             if (desc_len > 0 && desc_len % 5 != 0)
                 return AVERROR_INVALIDDATA;
@@ -1855,9 +1855,9 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
                 av_assert0(language_count <= sizeof(language) / 4);
 
                 if (st->codecpar->extradata == NULL) {
-                    if (ff_alloc_extradata(st->codecpar, language_count * 2)) {
-                        return AVERROR(ENOMEM);
-                    }
+                    ret = ff_alloc_extradata(st->codecpar, language_count * 2);
+                    if (ret < 0)
+                        return ret;
                 }
 
                 if (st->codecpar->extradata_size < language_count * 2)
@@ -1890,7 +1890,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
              * subtitling_type (1 byte),
              * composition_page_id (2 bytes),
              * ancillary_page_id (2 bytes) */
-            int language_count = desc_len / 8;
+            int language_count = desc_len / 8, ret;
 
             if (desc_len > 0 && desc_len % 8 != 0)
                 return AVERROR_INVALIDDATA;
@@ -1906,9 +1906,9 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
                 av_assert0(language_count <= sizeof(language) / 4);
 
                 if (st->codecpar->extradata == NULL) {
-                    if (ff_alloc_extradata(st->codecpar, language_count * 5)) {
-                        return AVERROR(ENOMEM);
-                    }
+                    ret = ff_alloc_extradata(st->codecpar, language_count * 5);
+                    if (ret < 0)
+                        return ret;
                 }
 
                 if (st->codecpar->extradata_size < language_count * 5)
@@ -3128,8 +3128,8 @@ static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
     uint8_t pcr_buf[12];
     const uint8_t *data;
 
-    if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(pkt, TS_PACKET_SIZE)) < 0)
+        return ret;
     ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
     pkt->pos = avio_tell(s->pb);
     if (ret < 0) {
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index e8dd8b7d56..5b4694bfd1 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -1617,7 +1617,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
 
             ret = avio_open_dyn_buf(&ts_st->amux->pb);
             if (ret < 0)
-                return AVERROR(ENOMEM);
+                return ret;
 
             ret = av_write_frame(ts_st->amux, &pkt2);
             if (ret < 0) {
diff --git a/libavformat/mvi.c b/libavformat/mvi.c
index 9f90faf56b..ff5c08bf51 100644
--- a/libavformat/mvi.c
+++ b/libavformat/mvi.c
@@ -45,6 +45,7 @@ static int read_header(AVFormatContext *s)
     AVIOContext *pb = s->pb;
     AVStream *ast, *vst;
     unsigned int version, frames_count, msecs_per_frame, player_version;
+    int ret;
 
     ast = avformat_new_stream(s, NULL);
     if (!ast)
@@ -54,8 +55,8 @@ static int read_header(AVFormatContext *s)
     if (!vst)
         return AVERROR(ENOMEM);
 
-    if (ff_alloc_extradata(vst->codecpar, 2))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(vst->codecpar, 2)) < 0)
+        return ret;
 
     version                  = avio_r8(pb);
     vst->codecpar->extradata[0] = avio_r8(pb);
diff --git a/libavformat/nutdec.c b/libavformat/nutdec.c
index 2b5adcb111..3779dce2a8 100644
--- a/libavformat/nutdec.c
+++ b/libavformat/nutdec.c
@@ -427,8 +427,10 @@ static int decode_stream_header(NUTContext *nut)
 
     GET_V(st->codecpar->extradata_size, tmp < (1 << 30));
     if (st->codecpar->extradata_size) {
-        if (ff_get_extradata(s, st->codecpar, bc, st->codecpar->extradata_size) < 0)
-            return AVERROR(ENOMEM);
+        ret = ff_get_extradata(s, st->codecpar, bc,
+                               st->codecpar->extradata_size);
+        if (ret < 0)
+            return ret;
     }
 
     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
diff --git a/libavformat/nuv.c b/libavformat/nuv.c
index a1edbf88df..612f845b4b 100644
--- a/libavformat/nuv.c
+++ b/libavformat/nuv.c
@@ -74,7 +74,7 @@ static int get_codec_data(AVFormatContext *s, AVIOContext *pb, AVStream *vst,
     if (!vst && !myth)
         return 1; // no codec data needed
     while (!avio_feof(pb)) {
-        int size, subtype;
+        int size, subtype, ret;
 
         frametype = avio_r8(pb);
         switch (frametype) {
@@ -87,8 +87,8 @@ static int get_codec_data(AVFormatContext *s, AVIOContext *pb, AVStream *vst,
                     av_freep(&vst->codecpar->extradata);
                     vst->codecpar->extradata_size = 0;
                 }
-                if (ff_get_extradata(NULL, vst->codecpar, pb, size) < 0)
-                    return AVERROR(ENOMEM);
+                if ((ret = ff_get_extradata(NULL, vst->codecpar, pb, size)) < 0)
+                    return ret;
                 size = 0;
                 if (!myth)
                     return 0;
diff --git a/libavformat/oggparsecelt.c b/libavformat/oggparsecelt.c
index 9c438a096a..f7a88af616 100644
--- a/libavformat/oggparsecelt.c
+++ b/libavformat/oggparsecelt.c
@@ -37,6 +37,7 @@ static int celt_header(AVFormatContext *s, int idx)
     AVStream *st = s->streams[idx];
     struct oggcelt_private *priv = os->private;
     uint8_t *p = os->buf + os->pstart;
+    int ret;
 
     if (os->psize == 60 &&
         !memcmp(p, ff_celt_codec.magic, ff_celt_codec.magicsize)) {
@@ -48,9 +49,10 @@ static int celt_header(AVFormatContext *s, int idx)
         priv = av_malloc(sizeof(struct oggcelt_private));
         if (!priv)
             return AVERROR(ENOMEM);
-        if (ff_alloc_extradata(st->codecpar, 2 * sizeof(uint32_t)) < 0) {
+        ret = ff_alloc_extradata(st->codecpar, 2 * sizeof(uint32_t));
+        if (ret < 0) {
             av_free(priv);
-            return AVERROR(ENOMEM);
+            return ret;
         }
         version          = AV_RL32(p + 28);
         /* unused header size field skipped */
diff --git a/libavformat/oggparseflac.c b/libavformat/oggparseflac.c
index b5f1416a3c..a51ff251ad 100644
--- a/libavformat/oggparseflac.c
+++ b/libavformat/oggparseflac.c
@@ -34,7 +34,7 @@ flac_header (AVFormatContext * s, int idx)
     struct ogg_stream *os = ogg->streams + idx;
     AVStream *st = s->streams[idx];
     GetBitContext gb;
-    int mdt;
+    int mdt, ret;
 
     if (os->buf[os->pstart] == 0xff)
         return 0;
@@ -61,8 +61,8 @@ flac_header (AVFormatContext * s, int idx)
         st->codecpar->codec_id = AV_CODEC_ID_FLAC;
         st->need_parsing = AVSTREAM_PARSE_HEADERS;
 
-        if (ff_alloc_extradata(st->codecpar, FLAC_STREAMINFO_SIZE) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(st->codecpar, FLAC_STREAMINFO_SIZE)) < 0)
+            return ret;
         memcpy(st->codecpar->extradata, streaminfo_start, st->codecpar->extradata_size);
 
         samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
diff --git a/libavformat/oggparseogm.c b/libavformat/oggparseogm.c
index b07a5d55ba..f25efb8c2a 100644
--- a/libavformat/oggparseogm.c
+++ b/libavformat/oggparseogm.c
@@ -43,6 +43,7 @@ ogm_header(AVFormatContext *s, int idx)
     uint64_t time_unit;
     uint64_t spu;
     uint32_t size;
+    int ret;
 
     bytestream2_init(&p, os->buf + os->pstart, os->psize);
     if (!(bytestream2_peek_byte(&p) & 1))
@@ -109,8 +110,8 @@ ogm_header(AVFormatContext *s, int idx)
                 if (bytestream2_get_bytes_left(&p) < size)
                     return AVERROR_INVALIDDATA;
                 av_freep(&st->codecpar->extradata);
-                if (ff_alloc_extradata(st->codecpar, size) < 0)
-                    return AVERROR(ENOMEM);
+                if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
+                    return ret;
                 bytestream2_get_buffer(&p, st->codecpar->extradata, st->codecpar->extradata_size);
             }
         }
diff --git a/libavformat/oggparseopus.c b/libavformat/oggparseopus.c
index cd34cf23ba..38908dc2dd 100644
--- a/libavformat/oggparseopus.c
+++ b/libavformat/oggparseopus.c
@@ -42,6 +42,7 @@ static int opus_header(AVFormatContext *avf, int idx)
     AVStream *st                 = avf->streams[idx];
     struct oggopus_private *priv = os->private;
     uint8_t *packet              = os->buf + os->pstart;
+    int ret;
 
     if (!priv) {
         priv = os->private = av_mallocz(sizeof(*priv));
@@ -63,8 +64,8 @@ static int opus_header(AVFormatContext *avf, int idx)
         /*channel_map         = AV_RL8 (packet + 18);*/
 
         av_freep(&st->codecpar->extradata);
-        if (ff_alloc_extradata(st->codecpar, os->psize))
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(st->codecpar, os->psize)) < 0)
+            return ret;
 
         memcpy(st->codecpar->extradata, packet, os->psize);
 
diff --git a/libavformat/oggparsespeex.c b/libavformat/oggparsespeex.c
index 27fc99247d..c4fee7e076 100644
--- a/libavformat/oggparsespeex.c
+++ b/libavformat/oggparsespeex.c
@@ -46,6 +46,7 @@ static int speex_header(AVFormatContext *s, int idx) {
     struct speex_params *spxp = os->private;
     AVStream *st = s->streams[idx];
     uint8_t *p = os->buf + os->pstart;
+    int ret;
 
     if (!spxp) {
         spxp = av_mallocz(sizeof(*spxp));
@@ -92,8 +93,8 @@ static int speex_header(AVFormatContext *s, int idx) {
         if (frames_per_packet)
             spxp->packet_size *= frames_per_packet;
 
-        if (ff_alloc_extradata(st->codecpar, os->psize) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(st->codecpar, os->psize)) < 0)
+            return ret;
         memcpy(st->codecpar->extradata, p, st->codecpar->extradata_size);
 
         avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
diff --git a/libavformat/omadec.c b/libavformat/omadec.c
index 60cbf3a87f..9521b6d59e 100644
--- a/libavformat/omadec.c
+++ b/libavformat/omadec.c
@@ -459,8 +459,8 @@ static int oma_read_header(AVFormatContext *s)
 
         /* fake the ATRAC3 extradata
          * (wav format, makes stream copy to wav work) */
-        if (ff_alloc_extradata(st->codecpar, 14))
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(st->codecpar, 14)) < 0)
+            return ret;
 
         edata = st->codecpar->extradata;
         AV_WL16(&edata[0],  1);             // always 1
diff --git a/libavformat/paf.c b/libavformat/paf.c
index b3c8e786bc..a31d01502b 100644
--- a/libavformat/paf.c
+++ b/libavformat/paf.c
@@ -194,7 +194,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     PAFDemuxContext *p  = s->priv_data;
     AVIOContext     *pb = s->pb;
     uint32_t        count, offset;
-    int             size, i;
+    int             size, i, ret;
 
     if (p->current_frame >= p->nb_frames)
         return AVERROR_EOF;
@@ -203,8 +203,8 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
         return AVERROR_EOF;
 
     if (p->got_audio) {
-        if (av_new_packet(pkt, p->audio_size) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, p->audio_size)) < 0)
+            return ret;
 
         memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
         pkt->duration     = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
@@ -244,8 +244,8 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
 
     size = p->video_size - p->frames_offset_table[p->current_frame];
 
-    if (av_new_packet(pkt, size) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(pkt, size)) < 0)
+        return ret;
 
     pkt->stream_index = 0;
     pkt->duration     = 1;
diff --git a/libavformat/rawdec.c b/libavformat/rawdec.c
index 59b49e3f77..fee016cc7f 100644
--- a/libavformat/rawdec.c
+++ b/libavformat/rawdec.c
@@ -39,8 +39,8 @@ int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
 
     size = raw->raw_packet_size;
 
-    if (av_new_packet(pkt, size) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(pkt, size)) < 0)
+        return ret;
 
     pkt->pos= avio_tell(s->pb);
     pkt->stream_index = 0;
diff --git a/libavformat/rl2.c b/libavformat/rl2.c
index 07696965c7..9e10155838 100644
--- a/libavformat/rl2.c
+++ b/libavformat/rl2.c
@@ -127,8 +127,9 @@ static av_cold int rl2_read_header(AVFormatContext *s)
     if(signature == RLV3_TAG && back_size > 0)
         st->codecpar->extradata_size += back_size;
 
-    if(ff_get_extradata(s, st->codecpar, pb, st->codecpar->extradata_size) < 0)
-        return AVERROR(ENOMEM);
+    ret = ff_get_extradata(s, st->codecpar, pb, st->codecpar->extradata_size);
+    if (ret < 0)
+        return ret;
 
     /** setup audio stream if present */
     if(sound_rate){
diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c
index 088bd75b69..3c4b97d49f 100644
--- a/libavformat/rmdec.c
+++ b/libavformat/rmdec.c
@@ -87,9 +87,7 @@ static int rm_read_extradata(AVFormatContext *s, AVIOContext *pb, AVCodecParamet
         av_log(s, AV_LOG_ERROR, "extradata size %u too large\n", size);
         return -1;
     }
-    if (ff_get_extradata(s, par, pb, size) < 0)
-        return AVERROR(ENOMEM);
-    return 0;
+    return ff_get_extradata(s, par, pb, size);
 }
 
 static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
diff --git a/libavformat/rsd.c b/libavformat/rsd.c
index 396a431f34..e23c8abae5 100644
--- a/libavformat/rsd.c
+++ b/libavformat/rsd.c
@@ -97,9 +97,8 @@ static int rsd_read_header(AVFormatContext *s)
     switch (par->codec_id) {
     case AV_CODEC_ID_XMA2:
         par->block_align = 2048;
-        ff_alloc_extradata(par, 34);
-        if (!par->extradata)
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(par, 34)) < 0)
+            return ret;
         memset(par->extradata, 0, 34);
         break;
     case AV_CODEC_ID_ADPCM_PSX:
diff --git a/libavformat/rtpdec_ac3.c b/libavformat/rtpdec_ac3.c
index 56a379f86c..dd4a4e1054 100644
--- a/libavformat/rtpdec_ac3.c
+++ b/libavformat/rtpdec_ac3.c
@@ -62,9 +62,9 @@ static int ac3_handle_packet(AVFormatContext *ctx, PayloadContext *data,
             av_log(ctx, AV_LOG_ERROR, "Invalid AC3 packet data\n");
             return AVERROR_INVALIDDATA;
         }
-        if (av_new_packet(pkt, len)) {
+        if ((err = av_new_packet(pkt, len)) < 0) {
             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
-            return AVERROR(ENOMEM);
+            return err;
         }
 
         pkt->stream_index = st->index;
diff --git a/libavformat/rtpdec_amr.c b/libavformat/rtpdec_amr.c
index 35d3222811..988b7bddfd 100644
--- a/libavformat/rtpdec_amr.c
+++ b/libavformat/rtpdec_amr.c
@@ -51,7 +51,7 @@ static int amr_handle_packet(AVFormatContext *ctx, PayloadContext *data,
 {
     const uint8_t *frame_sizes = NULL;
     int frames;
-    int i;
+    int i, ret;
     const uint8_t *speech_data;
     uint8_t *ptr;
 
@@ -93,9 +93,9 @@ static int amr_handle_packet(AVFormatContext *ctx, PayloadContext *data,
     speech_data = buf + 1 + frames;
 
     /* Everything except the codec mode request byte should be output. */
-    if (av_new_packet(pkt, len - 1)) {
+    if ((ret = av_new_packet(pkt, len - 1)) < 0) {
         av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
-        return AVERROR(ENOMEM);
+        return ret;
     }
     pkt->stream_index = st->index;
     ptr = pkt->data;
diff --git a/libavformat/rtpdec_h263.c b/libavformat/rtpdec_h263.c
index 9b71ed7efe..1905b435f8 100644
--- a/libavformat/rtpdec_h263.c
+++ b/libavformat/rtpdec_h263.c
@@ -30,7 +30,7 @@ int ff_h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
 {
     uint8_t *ptr;
     uint16_t header;
-    int startcode, vrc, picture_header;
+    int startcode, vrc, picture_header, ret;
 
     if (len < 2) {
         av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
@@ -73,9 +73,9 @@ int ff_h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
         return AVERROR_INVALIDDATA;
     }
 
-    if (av_new_packet(pkt, len + startcode)) {
+    if ((ret = av_new_packet(pkt, len + startcode)) < 0) {
         av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
-        return AVERROR(ENOMEM);
+        return ret;
     }
     pkt->stream_index = st->index;
     ptr = pkt->data;
diff --git a/libavformat/rtpdec_latm.c b/libavformat/rtpdec_latm.c
index 9087d6bec5..de18e81ea9 100644
--- a/libavformat/rtpdec_latm.c
+++ b/libavformat/rtpdec_latm.c
@@ -116,8 +116,8 @@ static int parse_fmtp_config(AVStream *st, const char *value)
         goto end;
     }
     av_freep(&st->codecpar->extradata);
-    if (ff_alloc_extradata(st->codecpar, (get_bits_left(&gb) + 7)/8)) {
-        ret = AVERROR(ENOMEM);
+    ret = ff_alloc_extradata(st->codecpar, (get_bits_left(&gb) + 7)/8);
+    if (ret < 0) {
         goto end;
     }
     for (i = 0; i < st->codecpar->extradata_size; i++)
diff --git a/libavformat/rtpdec_mpa_robust.c b/libavformat/rtpdec_mpa_robust.c
index f4716edf74..c0355edec2 100644
--- a/libavformat/rtpdec_mpa_robust.c
+++ b/libavformat/rtpdec_mpa_robust.c
@@ -90,9 +90,9 @@ static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
             return AVERROR_INVALIDDATA;
         }
 
-        if (av_new_packet(pkt, adu_size)) {
+        if ((err = av_new_packet(pkt, adu_size)) < 0) {
             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
-            return AVERROR(ENOMEM);
+            return err;
         }
 
         pkt->stream_index = st->index;
@@ -120,9 +120,9 @@ static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
     if (!continuation && adu_size <= len) {
         /* One or more complete frames */
 
-        if (av_new_packet(pkt, adu_size)) {
+        if ((err = av_new_packet(pkt, adu_size)) < 0) {
             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
-            return AVERROR(ENOMEM);
+            return err;
         }
 
         pkt->stream_index = st->index;
diff --git a/libavformat/rtpdec_mpeg12.c b/libavformat/rtpdec_mpeg12.c
index 43d9d5854c..e640220ebe 100644
--- a/libavformat/rtpdec_mpeg12.c
+++ b/libavformat/rtpdec_mpeg12.c
@@ -29,6 +29,7 @@ static int mpeg_parse_packet(AVFormatContext *ctx, PayloadContext *data,
                              int flags)
 {
     unsigned int h;
+    int ret;
     if (len <= 4)
         return AVERROR_INVALIDDATA;
     h    = AV_RB32(buf);
@@ -41,8 +42,8 @@ static int mpeg_parse_packet(AVFormatContext *ctx, PayloadContext *data,
         buf += 4;
         len -= 4;
     }
-    if (av_new_packet(pkt, len) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(pkt, len)) < 0)
+        return ret;
     memcpy(pkt->data, buf, len);
     pkt->stream_index = st->index;
     return 0;
diff --git a/libavformat/rtpdec_mpeg4.c b/libavformat/rtpdec_mpeg4.c
index 08e5b982c1..45569c18d1 100644
--- a/libavformat/rtpdec_mpeg4.c
+++ b/libavformat/rtpdec_mpeg4.c
@@ -112,10 +112,10 @@ static void close_context(PayloadContext *data)
 static int parse_fmtp_config(AVCodecParameters *par, const char *value)
 {
     /* decode the hexa encoded parameter */
-    int len = ff_hex_to_data(NULL, value);
+    int len = ff_hex_to_data(NULL, value), ret;
     av_freep(&par->extradata);
-    if (ff_alloc_extradata(par, len))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(par, len)) < 0)
+        return ret;
     ff_hex_to_data(par->extradata, value);
     return 0;
 }
diff --git a/libavformat/rtpdec_qdm2.c b/libavformat/rtpdec_qdm2.c
index fa2b1b9302..a6a746becb 100644
--- a/libavformat/rtpdec_qdm2.c
+++ b/libavformat/rtpdec_qdm2.c
@@ -78,6 +78,7 @@ static int qdm2_parse_config(PayloadContext *qdm, AVStream *st,
                              const uint8_t *buf, const uint8_t *end)
 {
     const uint8_t *p = buf;
+    int ret;
 
     while (end - p >= 2) {
         unsigned int item_len = p[0], config_item = p[1];
@@ -105,8 +106,9 @@ static int qdm2_parse_config(PayloadContext *qdm, AVStream *st,
                 if (item_len < 30)
                     return AVERROR_INVALIDDATA;
                 av_freep(&st->codecpar->extradata);
-                if (ff_alloc_extradata(st->codecpar, 26 + item_len)) {
-                    return AVERROR(ENOMEM);
+                ret = ff_alloc_extradata(st->codecpar, 26 + item_len);
+                if (ret < 0) {
+                    return ret;
                 }
                 AV_WB32(st->codecpar->extradata, 12);
                 memcpy(st->codecpar->extradata + 4, "frma", 4);
diff --git a/libavformat/rtpdec_qt.c b/libavformat/rtpdec_qt.c
index 240f1a918c..aee76c42ef 100644
--- a/libavformat/rtpdec_qt.c
+++ b/libavformat/rtpdec_qt.c
@@ -49,13 +49,13 @@ static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt,
     GetBitContext gb;
     int packing_scheme, has_payload_desc, has_packet_info, alen,
         has_marker_bit = flags & RTP_FLAG_MARKER,
-        keyframe;
+        keyframe, ret;
 
     if (qt->remaining) {
         int num = qt->size / qt->bytes_per_frame;
 
-        if (av_new_packet(pkt, qt->bytes_per_frame))
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, qt->bytes_per_frame)) < 0)
+            return ret;
         pkt->stream_index = st->index;
         pkt->flags        = qt->flags;
         memcpy(pkt->data,
@@ -209,8 +209,8 @@ static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt,
             alen % qt->bytes_per_frame != 0)
             return AVERROR_INVALIDDATA; /* wrongly padded */
         qt->remaining = (alen / qt->bytes_per_frame) - 1;
-        if (av_new_packet(pkt, qt->bytes_per_frame))
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, qt->bytes_per_frame)) < 0)
+            return ret;
         memcpy(pkt->data, buf + avio_tell(&pb), qt->bytes_per_frame);
         pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
         pkt->stream_index = st->index;
diff --git a/libavformat/rtpdec_xiph.c b/libavformat/rtpdec_xiph.c
index 1bd48fa562..0afa35bc5e 100644
--- a/libavformat/rtpdec_xiph.c
+++ b/libavformat/rtpdec_xiph.c
@@ -63,7 +63,7 @@ static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                               int flags)
 {
 
-    int ident, fragmented, tdt, num_pkts, pkt_len;
+    int ident, fragmented, tdt, num_pkts, pkt_len, ret;
 
     if (!buf) {
         if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
@@ -77,9 +77,9 @@ static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data,
             av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
             return AVERROR_INVALIDDATA;
         }
-        if (av_new_packet(pkt, pkt_len)) {
+        if ((ret = av_new_packet(pkt, pkt_len)) < 0) {
             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
-            return AVERROR(ENOMEM);
+            return ret;
         }
         pkt->stream_index = st->index;
         memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
@@ -123,9 +123,9 @@ static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data,
     len -= 6;
 
     if (fragmented == 0) {
-        if (av_new_packet(pkt, pkt_len)) {
+        if ((ret = av_new_packet(pkt, pkt_len)) < 0) {
             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
-            return AVERROR(ENOMEM);
+            return ret;
         }
         pkt->stream_index = st->index;
         memcpy(pkt->data, buf, pkt_len);
@@ -228,6 +228,7 @@ parse_packed_headers(AVFormatContext *s,
 {
 
     unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
+    int ret;
     uint8_t *ptr;
 
     if (packed_headers_end - packed_headers < 9) {
@@ -263,9 +264,9 @@ parse_packed_headers(AVFormatContext *s,
      * -- one for the '2' marker */
     extradata_alloc = length + length / 255 + 3;
 
-    if (ff_alloc_extradata(par, extradata_alloc)) {
+    if ((ret = ff_alloc_extradata(par, extradata_alloc)) < 0) {
         av_log(s, AV_LOG_ERROR, "Out of memory\n");
-        return AVERROR(ENOMEM);
+        return ret;
     }
     ptr = par->extradata;
     *ptr++ = 2;
diff --git a/libavformat/sbgdec.c b/libavformat/sbgdec.c
index 4155395da0..de1de271bb 100644
--- a/libavformat/sbgdec.c
+++ b/libavformat/sbgdec.c
@@ -1327,7 +1327,7 @@ static int generate_intervals(void *log, struct sbg_script *s, int sample_rate,
 static int encode_intervals(struct sbg_script *s, AVCodecParameters *par,
                             struct ws_intervals *inter)
 {
-    int i, edata_size = 4;
+    int i, edata_size = 4, ret;
     uint8_t *edata;
 
     for (i = 0; i < inter->nb_inter; i++) {
@@ -1336,8 +1336,8 @@ static int encode_intervals(struct sbg_script *s, AVCodecParameters *par,
         if (edata_size < 0)
             return AVERROR(ENOMEM);
     }
-    if (ff_alloc_extradata(par, edata_size))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(par, edata_size)) < 0)
+        return ret;
     edata = par->extradata;
 
 #define ADD_EDATA32(v) do { AV_WL32(edata, (v)); edata += 4; } while(0)
@@ -1446,6 +1446,7 @@ fail:
 static int sbg_read_packet(AVFormatContext *avf, AVPacket *packet)
 {
     int64_t ts, end_ts;
+    int ret;
 
     ts = avf->streams[0]->cur_dts;
     end_ts = ts + avf->streams[0]->codecpar->frame_size;
@@ -1454,8 +1455,8 @@ static int sbg_read_packet(AVFormatContext *avf, AVPacket *packet)
                        end_ts);
     if (end_ts <= ts)
         return AVERROR_EOF;
-    if (av_new_packet(packet, 12) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = av_new_packet(packet, 12)) < 0)
+        return ret;
     packet->dts = packet->pts = ts;
     packet->duration = end_ts - ts;
     AV_WL64(packet->data + 0, ts);
diff --git a/libavformat/sdr2.c b/libavformat/sdr2.c
index 50abdf9397..8893f260d2 100644
--- a/libavformat/sdr2.c
+++ b/libavformat/sdr2.c
@@ -90,8 +90,8 @@ static int sdr2_read_packet(AVFormatContext *s, AVPacket *pkt)
     avio_skip(s->pb, 30);
 
     if (pos == FIRST) {
-        if (av_new_packet(pkt, next - 52 + 24) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, next - 52 + 24)) < 0)
+            return ret;
         memcpy(pkt->data, header, 24);
         ret = avio_read(s->pb, pkt->data + 24, next - 52);
         if (ret < 0) {
diff --git a/libavformat/sierravmd.c b/libavformat/sierravmd.c
index d586fc6ac0..8c2322eda6 100644
--- a/libavformat/sierravmd.c
+++ b/libavformat/sierravmd.c
@@ -127,8 +127,8 @@ static int vmd_read_header(AVFormatContext *s)
             vst->codecpar->width >>= 1;
             vst->codecpar->height >>= 1;
         }
-        if (ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE))
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE)) < 0)
+            return ret;
         memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
     }
 
@@ -283,8 +283,9 @@ static int vmd_read_packet(AVFormatContext *s,
 
     if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
         return AVERROR(EIO);
-    if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
-        return AVERROR(ENOMEM);
+    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)
diff --git a/libavformat/siff.c b/libavformat/siff.c
index 24d5ebb42d..56c5b33c76 100644
--- a/libavformat/siff.c
+++ b/libavformat/siff.c
@@ -192,6 +192,7 @@ static int siff_read_header(AVFormatContext *s)
 static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     SIFFContext *c = s->priv_data;
+    int ret;
 
     if (c->has_video) {
         unsigned int size;
@@ -213,8 +214,8 @@ static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
 
             size = c->pktsize - c->sndsize - c->gmcsize - 2;
             size = ffio_limit(s->pb, size);
-            if (av_new_packet(pkt, size + c->gmcsize + 2) < 0)
-                return AVERROR(ENOMEM);
+            if ((ret = av_new_packet(pkt, size + c->gmcsize + 2)) < 0)
+                return ret;
             AV_WL16(pkt->data, c->flags);
             if (c->gmcsize)
                 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
diff --git a/libavformat/smacker.c b/libavformat/smacker.c
index 239894dad0..6de0e7a0f1 100644
--- a/libavformat/smacker.c
+++ b/libavformat/smacker.c
@@ -233,13 +233,13 @@ static int smacker_read_header(AVFormatContext *s)
 
 
     /* load trees to extradata, they will be unpacked by decoder */
-    if(ff_alloc_extradata(st->codecpar, smk->treesize + 16)){
+    if ((ret = ff_alloc_extradata(st->codecpar, smk->treesize + 16)) < 0) {
         av_log(s, AV_LOG_ERROR,
                "Cannot allocate %"PRIu32" bytes of extradata\n",
                smk->treesize + 16);
         av_freep(&smk->frm_size);
         av_freep(&smk->frm_flags);
-        return AVERROR(ENOMEM);
+        return ret;
     }
     ret = avio_read(pb, st->codecpar->extradata + 16, st->codecpar->extradata_size - 16);
     if(ret != st->codecpar->extradata_size - 16){
@@ -353,8 +353,8 @@ static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
         }
         if (frame_size < 0 || frame_size >= INT_MAX/2)
             return AVERROR_INVALIDDATA;
-        if (av_new_packet(pkt, frame_size + 769))
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, frame_size + 769)) < 0)
+            return ret;
         if(smk->frm_size[smk->cur_frame] & 1)
             palchange |= 2;
         pkt->data[0] = palchange;
@@ -370,8 +370,8 @@ static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
     } else {
         if (smk->stream_id[smk->curstream] < 0 || !smk->bufs[smk->curstream])
             return AVERROR_INVALIDDATA;
-        if (av_new_packet(pkt, smk->buf_sizes[smk->curstream]))
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, smk->buf_sizes[smk->curstream])) < 0)
+            return ret;
         memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
         pkt->size = smk->buf_sizes[smk->curstream];
         pkt->stream_index = smk->stream_id[smk->curstream];
diff --git a/libavformat/smush.c b/libavformat/smush.c
index 20352adf94..962eb57ab2 100644
--- a/libavformat/smush.c
+++ b/libavformat/smush.c
@@ -51,6 +51,7 @@ static int smush_read_header(AVFormatContext *ctx)
     uint32_t magic, nframes, size, subversion, i;
     uint32_t width = 0, height = 0, got_audio = 0, read = 0;
     uint32_t sample_rate, channels, palette[256];
+    int ret;
 
     magic = avio_rb32(pb);
     avio_skip(pb, 4); // skip movie size
@@ -157,8 +158,8 @@ static int smush_read_header(AVFormatContext *ctx)
     vst->codecpar->height     = height;
 
     if (!smush->version) {
-        if (ff_alloc_extradata(vst->codecpar, 1024 + 2))
-            return AVERROR(ENOMEM);
+        if ((ret = ff_alloc_extradata(vst->codecpar, 1024 + 2)) < 0)
+            return ret;
 
         AV_WL16(vst->codecpar->extradata, subversion);
         for (i = 0; i < 256; i++)
diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index a3240d88a1..172da5de2b 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -211,11 +211,12 @@ void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q)
 int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
 {
     AVPacket *sub = q->subs + q->current_sub_idx;
+    int ret;
 
     if (q->current_sub_idx == q->nb_subs)
         return AVERROR_EOF;
-    if (av_packet_ref(pkt, sub) < 0) {
-        return AVERROR(ENOMEM);
+    if ((ret = av_packet_ref(pkt, sub)) < 0) {
+        return ret;
     }
 
     pkt->dts = pkt->pts;
diff --git a/libavformat/tiertexseq.c b/libavformat/tiertexseq.c
index a89a0a9d61..d7719e5acb 100644
--- a/libavformat/tiertexseq.c
+++ b/libavformat/tiertexseq.c
@@ -273,8 +273,10 @@ static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
 
         /* video packet */
         if (seq->current_pal_data_size + seq->current_video_data_size != 0) {
-            if (av_new_packet(pkt, 1 + seq->current_pal_data_size + seq->current_video_data_size))
-                return AVERROR(ENOMEM);
+            rc = av_new_packet(pkt, 1 + seq->current_pal_data_size
+                                      + seq->current_video_data_size);
+            if (rc < 0)
+                return rc;
 
             pkt->data[0] = 0;
             if (seq->current_pal_data_size) {
diff --git a/libavformat/ty.c b/libavformat/ty.c
index bbb2e28a93..738a22e7de 100644
--- a/libavformat/ty.c
+++ b/libavformat/ty.c
@@ -454,7 +454,7 @@ static int demux_video(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
     TYDemuxContext *ty = s->priv_data;
     const int subrec_type = rec_hdr->subrec_type;
     const int64_t rec_size = rec_hdr->rec_size;
-    int es_offset1;
+    int es_offset1, ret;
     int got_packet = 0;
 
     if (subrec_type != 0x02 && subrec_type != 0x0c &&
@@ -474,8 +474,8 @@ static int demux_video(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
                     int size = rec_hdr->rec_size - VIDEO_PES_LENGTH - es_offset1;
 
                     ty->cur_chunk_pos += VIDEO_PES_LENGTH + es_offset1;
-                    if (av_new_packet(pkt, size) < 0)
-                        return AVERROR(ENOMEM);
+                    if ((ret = av_new_packet(pkt, size)) < 0)
+                        return ret;
                     memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, size);
                     ty->cur_chunk_pos += size;
                     pkt->stream_index = 0;
@@ -498,8 +498,8 @@ static int demux_video(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
     }
 
     if (!got_packet) {
-        if (av_new_packet(pkt, rec_size) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, rec_size)) < 0)
+            return ret;
         memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size);
         ty->cur_chunk_pos += rec_size;
         pkt->stream_index = 0;
@@ -578,7 +578,7 @@ static int demux_audio(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
     TYDemuxContext *ty = s->priv_data;
     const int subrec_type = rec_hdr->subrec_type;
     const int64_t rec_size = rec_hdr->rec_size;
-    int es_offset1;
+    int es_offset1, ret;
 
     if (subrec_type == 2) {
         int need = 0;
@@ -621,8 +621,8 @@ static int demux_audio(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
             ty->pes_buf_cnt = 0;
 
         }
-        if (av_new_packet(pkt, rec_size - need) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, rec_size - need)) < 0)
+            return ret;
         memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size - need);
         ty->cur_chunk_pos += rec_size - need;
         pkt->stream_index = 1;
@@ -643,8 +643,8 @@ static int demux_audio(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
             }
         }
     } else if (subrec_type == 0x03) {
-        if (av_new_packet(pkt, rec_size) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, rec_size)) < 0)
+            return ret;
         memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size);
         ty->cur_chunk_pos += rec_size;
         pkt->stream_index = 1;
@@ -674,15 +674,15 @@ static int demux_audio(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
     } else if (subrec_type == 0x04) {
         /* SA Audio with no PES Header                      */
         /* ================================================ */
-        if (av_new_packet(pkt, rec_size) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, rec_size)) < 0)
+            return ret;
         memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size);
         ty->cur_chunk_pos += rec_size;
         pkt->stream_index = 1;
         pkt->pts = ty->last_audio_pts;
     } else if (subrec_type == 0x09) {
-        if (av_new_packet(pkt, rec_size) < 0)
-            return AVERROR(ENOMEM);
+        if ((ret = av_new_packet(pkt, rec_size)) < 0)
+            return ret;
         memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size);
         ty->cur_chunk_pos += rec_size ;
         pkt->stream_index = 1;
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 4d18880acb..bf248aea3b 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -5450,7 +5450,7 @@ int ff_generate_avci_extradata(AVStream *st)
     };
 
     const uint8_t *data = NULL;
-    int size            = 0;
+    int ret, size       = 0;
 
     if (st->codecpar->width == 1920) {
         if (st->codecpar->field_order == AV_FIELD_PROGRESSIVE) {
@@ -5480,8 +5480,8 @@ int ff_generate_avci_extradata(AVStream *st)
         return 0;
 
     av_freep(&st->codecpar->extradata);
-    if (ff_alloc_extradata(st->codecpar, size))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
+        return ret;
     memcpy(st->codecpar->extradata, data, size);
 
     return 0;
diff --git a/libavformat/vc1test.c b/libavformat/vc1test.c
index 3c677931fc..ff57f44b14 100644
--- a/libavformat/vc1test.c
+++ b/libavformat/vc1test.c
@@ -51,7 +51,7 @@ static int vc1t_read_header(AVFormatContext *s)
 {
     AVIOContext *pb = s->pb;
     AVStream *st;
-    int frames;
+    int frames, ret;
     uint32_t fps;
     uint32_t size;
 
@@ -67,8 +67,8 @@ static int vc1t_read_header(AVFormatContext *s)
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id = AV_CODEC_ID_WMV3;
 
-    if (ff_get_extradata(s, st->codecpar, pb, VC1_EXTRADATA_SIZE) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = ff_get_extradata(s, st->codecpar, pb, VC1_EXTRADATA_SIZE)) < 0)
+        return ret;
 
     avio_skip(pb, size - 4);
     st->codecpar->height = avio_rl32(pb);
diff --git a/libavformat/vividas.c b/libavformat/vividas.c
index 4ea29d85e3..4f54a4302e 100644
--- a/libavformat/vividas.c
+++ b/libavformat/vividas.c
@@ -278,7 +278,7 @@ static uint8_t *read_sb_block(AVIOContext *src, unsigned *size,
 
 static int track_header(VividasDemuxContext *viv, AVFormatContext *s,  uint8_t *buf, int size)
 {
-    int i,j;
+    int i, j, ret;
     int64_t off;
     int val_1;
     int num_video;
@@ -391,10 +391,9 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s,  uint8_t *
                 xd_size += len;
             }
 
-            st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;
-            if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size)) {
-                return AVERROR(ENOMEM);
-            }
+            ret = ff_alloc_extradata(st->codecpar, 64 + xd_size + xd_size / 255);
+            if (ret < 0)
+                return ret;
 
             p = st->codecpar->extradata;
             p[0] = 2;
diff --git a/libavformat/vqf.c b/libavformat/vqf.c
index 755849bac7..b43725b3c1 100644
--- a/libavformat/vqf.c
+++ b/libavformat/vqf.c
@@ -97,7 +97,7 @@ static int vqf_read_header(AVFormatContext *s)
     int rate_flag = -1;
     int header_size;
     int read_bitrate = 0;
-    int size;
+    int size, ret;
     uint8_t comm_chunk[12];
 
     if (!st)
@@ -222,8 +222,8 @@ static int vqf_read_header(AVFormatContext *s)
     avpriv_set_pts_info(st, 64, size, st->codecpar->sample_rate);
 
     /* put first 12 bytes of COMM chunk in extradata */
-    if (ff_alloc_extradata(st->codecpar, 12))
-        return AVERROR(ENOMEM);
+    if ((ret = ff_alloc_extradata(st->codecpar, 12)) < 0)
+        return ret;
     memcpy(st->codecpar->extradata, comm_chunk, 12);
 
     ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv);
diff --git a/libavformat/wavdec.c b/libavformat/wavdec.c
index 52194f54ef..cfaf3d61fe 100644
--- a/libavformat/wavdec.c
+++ b/libavformat/wavdec.c
@@ -181,7 +181,7 @@ static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
 static int wav_parse_xma2_tag(AVFormatContext *s, int64_t size, AVStream **st)
 {
     AVIOContext *pb = s->pb;
-    int version, num_streams, i, channels = 0;
+    int version, num_streams, i, channels = 0, ret;
 
     if (size < 36)
         return AVERROR_INVALIDDATA;
@@ -221,8 +221,8 @@ static int wav_parse_xma2_tag(AVFormatContext *s, int64_t size, AVStream **st)
 
     avio_seek(pb, -size, SEEK_CUR);
     av_freep(&(*st)->codecpar->extradata);
-    if (ff_get_extradata(s, (*st)->codecpar, pb, size) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = ff_get_extradata(s, (*st)->codecpar, pb, size)) < 0)
+        return ret;
 
     return 0;
 }
@@ -473,9 +473,9 @@ static int wav_read_header(AVFormatContext *s)
             vst->codecpar->codec_id = AV_CODEC_ID_SMVJPEG;
             vst->codecpar->width  = avio_rl24(pb);
             vst->codecpar->height = avio_rl24(pb);
-            if (ff_alloc_extradata(vst->codecpar, 4)) {
+            if ((ret = ff_alloc_extradata(vst->codecpar, 4)) < 0) {
                 av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n");
-                return AVERROR(ENOMEM);
+                return ret;
             }
             size = avio_rl24(pb);
             wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
diff --git a/libavformat/westwood_vqa.c b/libavformat/westwood_vqa.c
index c21a3e31f6..a0db854b1c 100644
--- a/libavformat/westwood_vqa.c
+++ b/libavformat/westwood_vqa.c
@@ -85,7 +85,7 @@ static int wsvqa_read_header(AVFormatContext *s)
     uint8_t scratch[VQA_PREAMBLE_SIZE];
     uint32_t chunk_tag;
     uint32_t chunk_size;
-    int fps;
+    int fps, ret;
 
     /* initialize the video decoder stream */
     st = avformat_new_stream(s, NULL);
@@ -101,8 +101,8 @@ static int wsvqa_read_header(AVFormatContext *s)
     avio_seek(pb, 20, SEEK_SET);
 
     /* the VQA header needs to go to the decoder */
-    if (ff_get_extradata(s, st->codecpar, pb, VQA_HEADER_SIZE) < 0)
-        return AVERROR(ENOMEM);
+    if ((ret = ff_get_extradata(s, st->codecpar, pb, VQA_HEADER_SIZE)) < 0)
+        return ret;
     header = st->codecpar->extradata;
     st->codecpar->width = AV_RL16(&header[6]);
     st->codecpar->height = AV_RL16(&header[8]);
@@ -214,8 +214,8 @@ static int wsvqa_read_packet(AVFormatContext *s,
                         break;
                     case SND2_TAG:
                         st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_WS;
-                        if (ff_alloc_extradata(st->codecpar, 2))
-                            return AVERROR(ENOMEM);
+                        if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
+                            return ret;
                         AV_WL16(st->codecpar->extradata, wsvqa->version);
                         break;
                     }
diff --git a/libavformat/wvdec.c b/libavformat/wvdec.c
index 649791d151..f2bb4c60ba 100644
--- a/libavformat/wvdec.c
+++ b/libavformat/wvdec.c
@@ -282,8 +282,8 @@ static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
     }
 
     pos = wc->pos;
-    if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
-        return AVERROR(ENOMEM);
+    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) {
diff --git a/libavformat/xwma.c b/libavformat/xwma.c
index b084449296..b5dcb850c0 100644
--- a/libavformat/xwma.c
+++ b/libavformat/xwma.c
@@ -130,15 +130,15 @@ static int xwma_read_header(AVFormatContext *s)
             avpriv_request_sample(s, "Unexpected extradata (%d bytes)",
                                   st->codecpar->extradata_size);
         } else if (st->codecpar->codec_id == AV_CODEC_ID_WMAPRO) {
-            if (ff_alloc_extradata(st->codecpar, 18))
-                return AVERROR(ENOMEM);
+            if ((ret = ff_alloc_extradata(st->codecpar, 18)) < 0)
+                return ret;
 
             memset(st->codecpar->extradata, 0, st->codecpar->extradata_size);
             st->codecpar->extradata[ 0] = st->codecpar->bits_per_coded_sample;
             st->codecpar->extradata[14] = 224;
         } else {
-            if (ff_alloc_extradata(st->codecpar, 6))
-                return AVERROR(ENOMEM);
+            if ((ret = ff_alloc_extradata(st->codecpar, 6)) < 0)
+                return ret;
 
             memset(st->codecpar->extradata, 0, st->codecpar->extradata_size);
             /* setup extradata with our experimentally obtained value */
