diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index 4d0abb1..9855e40 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -939,16 +939,14 @@ static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
 
 static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
 {
-    int ch;
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->buffer.samples, s->channels, 3 * 1024 * sizeof(s->buffer.samples[0]), alloc_fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->cpe, s->chan_map[0], sizeof(ChannelElement), alloc_fail);
+    int ret, ch;
+    FF_ALLOCZ_ARRAY_OR_GOTO(s->buffer.samples, s->channels, 3 * 1024 * sizeof(s->buffer.samples[0]), ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(s->cpe, s->chan_map[0], sizeof(ChannelElement), ret, return ret);
 
     for(ch = 0; ch < s->channels; ch++)
         s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
 
     return 0;
-alloc_fail:
-    return AVERROR(ENOMEM);
 }
 
 static av_cold void aac_encode_init_tables(void)
diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index ddbc7b2..19434d4 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -2322,60 +2322,60 @@ static av_cold void set_bandwidth(AC3EncodeContext *s)
 
 static av_cold int allocate_buffers(AC3EncodeContext *s)
 {
-    AVCodecContext *avctx = s->avctx;
     int blk, ch;
     int channels = s->channels + 1; /* includes coupling channel */
     int channel_blocks = channels * s->num_blocks;
     int total_coefs    = AC3_MAX_COEFS * channel_blocks;
+    int ret;
+
+    if (ret = s->allocate_sample_buffers(s))
+        return ret;
 
-    if (s->allocate_sample_buffers(s))
-        goto alloc_fail;
-
-    FF_ALLOC_ARRAY_OR_GOTO(avctx, s->bap_buffer, total_coefs,
-                     sizeof(*s->bap_buffer), alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(avctx, s->bap1_buffer, total_coefs,
-                     sizeof(*s->bap1_buffer), alloc_fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->mdct_coef_buffer, total_coefs,
-                      sizeof(*s->mdct_coef_buffer), alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(avctx, s->exp_buffer, total_coefs,
-                     sizeof(*s->exp_buffer), alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(avctx, s->grouped_exp_buffer, channel_blocks, 128 *
-                     sizeof(*s->grouped_exp_buffer), alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(avctx, s->psd_buffer, total_coefs,
-                     sizeof(*s->psd_buffer), alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(avctx, s->band_psd_buffer, channel_blocks, 64 *
-                     sizeof(*s->band_psd_buffer), alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(avctx, s->mask_buffer, channel_blocks, 64 *
-                     sizeof(*s->mask_buffer), alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(avctx, s->qmant_buffer, total_coefs,
-                     sizeof(*s->qmant_buffer), alloc_fail);
+    FF_ALLOC_ARRAY_OR_GOTO(s->bap_buffer, total_coefs,
+                     sizeof(*s->bap_buffer), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->bap1_buffer, total_coefs,
+                     sizeof(*s->bap1_buffer), ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(s->mdct_coef_buffer, total_coefs,
+                      sizeof(*s->mdct_coef_buffer), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->exp_buffer, total_coefs,
+                     sizeof(*s->exp_buffer), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->grouped_exp_buffer, channel_blocks, 128 *
+                     sizeof(*s->grouped_exp_buffer), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->psd_buffer, total_coefs,
+                     sizeof(*s->psd_buffer), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->band_psd_buffer, channel_blocks, 64 *
+                     sizeof(*s->band_psd_buffer), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->mask_buffer, channel_blocks, 64 *
+                     sizeof(*s->mask_buffer), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->qmant_buffer, total_coefs,
+                     sizeof(*s->qmant_buffer), ret, return ret);
     if (s->cpl_enabled) {
-        FF_ALLOC_ARRAY_OR_GOTO(avctx, s->cpl_coord_exp_buffer, channel_blocks, 16 *
-                         sizeof(*s->cpl_coord_exp_buffer), alloc_fail);
-        FF_ALLOC_ARRAY_OR_GOTO(avctx, s->cpl_coord_mant_buffer, channel_blocks, 16 *
-                         sizeof(*s->cpl_coord_mant_buffer), alloc_fail);
+        FF_ALLOC_ARRAY_OR_GOTO(s->cpl_coord_exp_buffer, channel_blocks, 16 *
+                         sizeof(*s->cpl_coord_exp_buffer), ret, return ret);
+        FF_ALLOC_ARRAY_OR_GOTO(s->cpl_coord_mant_buffer, channel_blocks, 16 *
+                         sizeof(*s->cpl_coord_mant_buffer), ret, return ret);
     }
     for (blk = 0; blk < s->num_blocks; blk++) {
         AC3Block *block = &s->blocks[blk];
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->mdct_coef, channels, sizeof(*block->mdct_coef),
-                          alloc_fail);
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->exp, channels, sizeof(*block->exp),
-                          alloc_fail);
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->grouped_exp, channels, sizeof(*block->grouped_exp),
-                          alloc_fail);
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->psd, channels, sizeof(*block->psd),
-                          alloc_fail);
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->band_psd, channels, sizeof(*block->band_psd),
-                          alloc_fail);
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->mask, channels, sizeof(*block->mask),
-                          alloc_fail);
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->qmant, channels, sizeof(*block->qmant),
-                          alloc_fail);
+        FF_ALLOCZ_ARRAY_OR_GOTO(block->mdct_coef, channels, sizeof(*block->mdct_coef),
+                          ret, return ret);
+        FF_ALLOCZ_ARRAY_OR_GOTO(block->exp, channels, sizeof(*block->exp),
+                          ret, return ret);
+        FF_ALLOCZ_ARRAY_OR_GOTO(block->grouped_exp, channels, sizeof(*block->grouped_exp),
+                          ret, return ret);
+        FF_ALLOCZ_ARRAY_OR_GOTO(block->psd, channels, sizeof(*block->psd),
+                          ret, return ret);
+        FF_ALLOCZ_ARRAY_OR_GOTO(block->band_psd, channels, sizeof(*block->band_psd),
+                          ret, return ret);
+        FF_ALLOCZ_ARRAY_OR_GOTO(block->mask, channels, sizeof(*block->mask),
+                          ret, return ret);
+        FF_ALLOCZ_ARRAY_OR_GOTO(block->qmant, channels, sizeof(*block->qmant),
+                          ret, return ret);
         if (s->cpl_enabled) {
-            FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->cpl_coord_exp, channels, sizeof(*block->cpl_coord_exp),
-                              alloc_fail);
-            FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->cpl_coord_mant, channels, sizeof(*block->cpl_coord_mant),
-                              alloc_fail);
+            FF_ALLOCZ_ARRAY_OR_GOTO(block->cpl_coord_exp, channels, sizeof(*block->cpl_coord_exp),
+                              ret, return ret);
+            FF_ALLOCZ_ARRAY_OR_GOTO(block->cpl_coord_mant, channels, sizeof(*block->cpl_coord_mant),
+                              ret, return ret);
         }
 
         for (ch = 0; ch < channels; ch++) {
@@ -2397,28 +2397,26 @@ static av_cold int allocate_buffers(AC3EncodeContext *s)
     }
 
     if (!s->fixed_point) {
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->fixed_coef_buffer, total_coefs,
-                          sizeof(*s->fixed_coef_buffer), alloc_fail);
+        FF_ALLOCZ_ARRAY_OR_GOTO(s->fixed_coef_buffer, total_coefs,
+                          sizeof(*s->fixed_coef_buffer), ret, return ret);
         for (blk = 0; blk < s->num_blocks; blk++) {
             AC3Block *block = &s->blocks[blk];
-            FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->fixed_coef, channels,
-                              sizeof(*block->fixed_coef), alloc_fail);
+            FF_ALLOCZ_ARRAY_OR_GOTO(block->fixed_coef, channels,
+                              sizeof(*block->fixed_coef), ret, return ret);
             for (ch = 0; ch < channels; ch++)
                 block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
         }
     } else {
         for (blk = 0; blk < s->num_blocks; blk++) {
             AC3Block *block = &s->blocks[blk];
-            FF_ALLOCZ_ARRAY_OR_GOTO(avctx, block->fixed_coef, channels,
-                              sizeof(*block->fixed_coef), alloc_fail);
+            FF_ALLOCZ_ARRAY_OR_GOTO(block->fixed_coef, channels,
+                              sizeof(*block->fixed_coef), ret, return ret);
             for (ch = 0; ch < channels; ch++)
                 block->fixed_coef[ch] = (int32_t *)block->mdct_coef[ch];
         }
     }
 
     return 0;
-alloc_fail:
-    return AVERROR(ENOMEM);
 }
 
 
diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c
index be65987..ec4e984 100644
--- a/libavcodec/ac3enc_template.c
+++ b/libavcodec/ac3enc_template.c
@@ -39,21 +39,20 @@
 
 int AC3_NAME(allocate_sample_buffers)(AC3EncodeContext *s)
 {
+    int ret;
     int ch;
 
-    FF_ALLOC_OR_GOTO(s->avctx, s->windowed_samples, AC3_WINDOW_SIZE *
-                     sizeof(*s->windowed_samples), alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(s->avctx, s->planar_samples, s->channels, sizeof(*s->planar_samples),
-                     alloc_fail);
+    FF_ALLOC_OR_GOTO(s->windowed_samples, AC3_WINDOW_SIZE *
+                     sizeof(*s->windowed_samples), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->planar_samples, s->channels, sizeof(*s->planar_samples),
+                     ret, return ret);
     for (ch = 0; ch < s->channels; ch++) {
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->planar_samples[ch],
+        FF_ALLOCZ_OR_GOTO(s->planar_samples[ch],
                           (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
-                          alloc_fail);
+                          ret, return ret);
     }
 
     return 0;
-alloc_fail:
-    return AVERROR(ENOMEM);
 }
 
 
diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
index 668939c..18d08fd 100644
--- a/libavcodec/adpcmenc.c
+++ b/libavcodec/adpcmenc.c
@@ -65,7 +65,7 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
     ADPCMEncodeContext *s = avctx->priv_data;
     uint8_t *extradata;
     int i;
-    int ret = AVERROR(ENOMEM);
+    int ret;
 
     if (avctx->channels > 2) {
         av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
@@ -80,14 +80,10 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
     if (avctx->trellis) {
         int frontier  = 1 << avctx->trellis;
         int max_paths =  frontier * FREEZE_INTERVAL;
-        FF_ALLOC_OR_GOTO(avctx, s->paths,
-                         max_paths * sizeof(*s->paths), error);
-        FF_ALLOC_OR_GOTO(avctx, s->node_buf,
-                         2 * frontier * sizeof(*s->node_buf),  error);
-        FF_ALLOC_OR_GOTO(avctx, s->nodep_buf,
-                         2 * frontier * sizeof(*s->nodep_buf), error);
-        FF_ALLOC_OR_GOTO(avctx, s->trellis_hash,
-                         65536 * sizeof(*s->trellis_hash), error);
+        FF_ALLOC_OR_GOTO(s->paths, max_paths * sizeof(*s->paths), ret, return ret);
+        FF_ALLOC_OR_GOTO(s->node_buf, 2 * frontier * sizeof(*s->node_buf), ret, return ret);
+        FF_ALLOC_OR_GOTO(s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), ret, return ret);
+        FF_ALLOC_OR_GOTO(s->trellis_hash, 65536 * sizeof(*s->trellis_hash), ret, return ret);
     }
 
     avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
@@ -114,7 +110,7 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
         avctx->bits_per_coded_sample = 4;
         avctx->block_align    = BLKSIZE;
         if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
-            goto error;
+            return AVERROR(ENOMEM);
         avctx->extradata_size = 32;
         extradata = avctx->extradata;
         bytestream_put_le16(&extradata, avctx->frame_size);
@@ -134,20 +130,15 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
             avctx->sample_rate != 44100) {
             av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
                    "22050 or 44100\n");
-            ret = AVERROR(EINVAL);
-            goto error;
+            return AVERROR(EINVAL);
         }
         avctx->frame_size = 512 * (avctx->sample_rate / 11025);
         break;
     default:
-        ret = AVERROR(EINVAL);
-        goto error;
+        return AVERROR(EINVAL);
     }
 
     return 0;
-error:
-    adpcm_encode_close(avctx);
-    return ret;
 }
 
 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
@@ -509,7 +500,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
         /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
         if (avctx->trellis > 0) {
-            FF_ALLOC_ARRAY_OR_GOTO(avctx, buf, avctx->channels, blocks * 8, error);
+            FF_ALLOC_ARRAY_OR_GOTO(buf, avctx->channels, blocks * 8, ret, return ret);
             for (ch = 0; ch < avctx->channels; ch++) {
                 adpcm_compress_trellis(avctx, &samples_p[ch][1],
                                        buf + ch * blocks * 8, &c->status[ch],
@@ -588,7 +579,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         }
 
         if (avctx->trellis > 0) {
-            FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
+            FF_ALLOC_OR_GOTO(buf, 2 * n, ret, return ret);
             adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
                                    &c->status[0], n, avctx->channels);
             if (avctx->channels == 2)
@@ -636,7 +627,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
         if (avctx->trellis > 0) {
             n = avctx->block_align - 7 * avctx->channels;
-            FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
+            FF_ALLOC_OR_GOTO(buf, 2 * n, ret, return ret);
             if (avctx->channels == 1) {
                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
                                        avctx->channels);
@@ -663,7 +654,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     case AV_CODEC_ID_ADPCM_YAMAHA:
         n = frame->nb_samples / 2;
         if (avctx->trellis > 0) {
-            FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 2, error);
+            FF_ALLOC_OR_GOTO(buf, 2 * n * 2, ret, return ret);
             n *= 2;
             if (avctx->channels == 1) {
                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
@@ -694,8 +685,6 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     avpkt->size = pkt_size;
     *got_packet_ptr = 1;
     return 0;
-error:
-    return AVERROR(ENOMEM);
 }
 
 static const enum AVSampleFormat sample_fmts[] = {
diff --git a/libavcodec/alac.c b/libavcodec/alac.c
index c8c0422..e7245b8 100644
--- a/libavcodec/alac.c
+++ b/libavcodec/alac.c
@@ -487,7 +487,7 @@ static av_cold int alac_decode_close(AVCodecContext *avctx)
 
 static int allocate_buffers(ALACContext *alac)
 {
-    int ch;
+    int ch, ret;
     unsigned buf_size = alac->max_samples_per_frame * sizeof(int32_t);
 
     for (ch = 0; ch < 2; ch++) {
@@ -497,22 +497,19 @@ static int allocate_buffers(ALACContext *alac)
     }
 
     for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
-        FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch],
-                         buf_size, buf_alloc_fail);
+        FF_ALLOC_OR_GOTO(alac->predict_error_buffer[ch],
+                         buf_size, ret, return ret);
 
         alac->direct_output = alac->sample_size > 16;
         if (!alac->direct_output) {
-            FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch],
-                             buf_size + AV_INPUT_BUFFER_PADDING_SIZE, buf_alloc_fail);
+            FF_ALLOC_OR_GOTO(alac->output_samples_buffer[ch],
+                             buf_size + AV_INPUT_BUFFER_PADDING_SIZE, ret, return ret);
         }
 
-        FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
-                         buf_size + AV_INPUT_BUFFER_PADDING_SIZE, buf_alloc_fail);
+        FF_ALLOC_OR_GOTO(alac->extra_bits_buffer[ch],
+                         buf_size + AV_INPUT_BUFFER_PADDING_SIZE, ret, return ret);
     }
     return 0;
-buf_alloc_fail:
-    alac_decode_close(alac->avctx);
-    return AVERROR(ENOMEM);
 }
 
 static int alac_set_info(ALACContext *alac)
diff --git a/libavcodec/apedec.c b/libavcodec/apedec.c
index 2d19250..7db4467 100644
--- a/libavcodec/apedec.c
+++ b/libavcodec/apedec.c
@@ -215,7 +215,7 @@ static av_cold int ape_decode_close(AVCodecContext *avctx)
 static av_cold int ape_decode_init(AVCodecContext *avctx)
 {
     APEContext *s = avctx->priv_data;
-    int i;
+    int i, ret;
 
     if (avctx->extradata_size != 6) {
         av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
@@ -260,9 +260,9 @@ static av_cold int ape_decode_init(AVCodecContext *avctx)
     for (i = 0; i < APE_FILTER_LEVELS; i++) {
         if (!ape_filter_orders[s->fset][i])
             break;
-        FF_ALLOC_OR_GOTO(avctx, s->filterbuf[i],
+        FF_ALLOC_OR_GOTO(s->filterbuf[i],
                          (ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4,
-                         filter_alloc_fail);
+                         ret, return ret);
     }
 
     if (s->fileversion < 3860) {
@@ -298,9 +298,6 @@ static av_cold int ape_decode_init(AVCodecContext *avctx)
     avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
 
     return 0;
-filter_alloc_fail:
-    ape_decode_close(avctx);
-    return AVERROR(ENOMEM);
 }
 
 /**
diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
index 32ac90f..79d3f3f 100644
--- a/libavcodec/dnxhdenc.c
+++ b/libavcodec/dnxhdenc.c
@@ -205,15 +205,16 @@ static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
 {
     int i, j, level, run;
     int max_level = 1 << (ctx->bit_depth + 2);
+    int ret;
 
-    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->orig_vlc_codes,
-                      max_level, 4 * sizeof(*ctx->orig_vlc_codes), fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->orig_vlc_bits,
-                      max_level, 4 * sizeof(*ctx->orig_vlc_bits), fail);
-    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_codes,
-                      63 * 2, fail);
-    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_bits,
-                      63, fail);
+    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->orig_vlc_codes,
+                      max_level, 4 * sizeof(*ctx->orig_vlc_codes), ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->orig_vlc_bits,
+                      max_level, 4 * sizeof(*ctx->orig_vlc_bits), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(ctx->run_codes,
+                      63 * 2, ret, return ret);
+    FF_ALLOCZ_OR_GOTO(ctx->run_bits,
+                      63, ret, return ret);
 
     ctx->vlc_codes = ctx->orig_vlc_codes + max_level * 2;
     ctx->vlc_bits  = ctx->orig_vlc_bits + max_level * 2;
@@ -258,8 +259,6 @@ static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
         ctx->run_bits[run]  = ctx->cid_table->run_bits[i];
     }
     return 0;
-fail:
-    return AVERROR(ENOMEM);
 }
 
 static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
@@ -269,17 +268,18 @@ static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
     int qscale, i;
     const uint8_t *luma_weight_table   = ctx->cid_table->luma_weight;
     const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
+    int ret;
 
-    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l,
-                      (ctx->m.avctx->qmax + 1), 64 * sizeof(int), fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c,
-                      (ctx->m.avctx->qmax + 1), 64 * sizeof(int), fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l16,
+    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->qmatrix_l,
+                      (ctx->m.avctx->qmax + 1), 64 * sizeof(int), ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->qmatrix_c,
+                      (ctx->m.avctx->qmax + 1), 64 * sizeof(int), ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->qmatrix_l16,
                       (ctx->m.avctx->qmax + 1), 64 * 2 * sizeof(uint16_t),
-                      fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c16,
+                      ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->qmatrix_c16,
                       (ctx->m.avctx->qmax + 1), 64 * 2 * sizeof(uint16_t),
-                      fail);
+                      ret, return ret);
 
     if (ctx->bit_depth == 8) {
         for (i = 1; i < 64; i++) {
@@ -338,27 +338,24 @@ static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
     ctx->m.q_intra_matrix          = ctx->qmatrix_l;
 
     return 0;
-fail:
-    return AVERROR(ENOMEM);
 }
 
 static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
 {
-    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->mb_rc, (ctx->m.avctx->qmax + 1),
-                          ctx->m.mb_num * sizeof(RCEntry), fail);
+    int ret;
+    FF_ALLOCZ_ARRAY_OR_GOTO(ctx->mb_rc, (ctx->m.avctx->qmax + 1),
+                          ctx->m.mb_num * sizeof(RCEntry), ret, return ret);
     if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD) {
-        FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->mb_cmp,
-                          ctx->m.mb_num, sizeof(RCCMPEntry), fail);
-        FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->mb_cmp_tmp,
-                          ctx->m.mb_num, sizeof(RCCMPEntry), fail);
+        FF_ALLOCZ_ARRAY_OR_GOTO(ctx->mb_cmp,
+                          ctx->m.mb_num, sizeof(RCCMPEntry), ret, return ret);
+        FF_ALLOCZ_ARRAY_OR_GOTO(ctx->mb_cmp_tmp,
+                          ctx->m.mb_num, sizeof(RCCMPEntry), ret, return ret);
     }
     ctx->frame_bits = (ctx->coding_unit_size -
                        ctx->data_offset - 4 - ctx->min_padding) * 8;
     ctx->qscale = 1;
     ctx->lambda = 2 << LAMBDA_FRAC_BITS; // qscale 2
     return 0;
-fail:
-    return AVERROR(ENOMEM);
 }
 
 static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
@@ -509,14 +506,14 @@ static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
     if ((ret = dnxhd_init_rc(ctx)) < 0)
         return ret;
 
-    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size,
-                      ctx->m.mb_height * sizeof(uint32_t), fail);
-    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs,
-                      ctx->m.mb_height * sizeof(uint32_t), fail);
-    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits,
-                      ctx->m.mb_num * sizeof(uint16_t), fail);
-    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale,
-                      ctx->m.mb_num * sizeof(uint8_t), fail);
+    FF_ALLOCZ_OR_GOTO(ctx->slice_size,
+                      ctx->m.mb_height * sizeof(uint32_t), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(ctx->slice_offs,
+                      ctx->m.mb_height * sizeof(uint32_t), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(ctx->mb_bits,
+                      ctx->m.mb_num * sizeof(uint16_t), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(ctx->mb_qscale,
+                      ctx->m.mb_num * sizeof(uint8_t), ret, return ret);
 
 #if FF_API_CODED_FRAME
 FF_DISABLE_DEPRECATION_WARNINGS
@@ -542,14 +539,12 @@ FF_ENABLE_DEPRECATION_WARNINGS
         for (i = 1; i < avctx->thread_count; i++) {
             ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
             if (!ctx->thread[i])
-                goto fail;
+                return AVERROR(ENOMEM);
             memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
         }
     }
 
     return 0;
-fail:  // for FF_ALLOCZ_OR_GOTO
-    return AVERROR(ENOMEM);
 }
 
 static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index 4c355fe..0bcc60f 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -181,39 +181,40 @@ int ff_h264_alloc_tables(H264Context *h)
     const int big_mb_num = h->mb_stride * (h->mb_height + 1);
     const int row_mb_num = 2*h->mb_stride*FFMAX(h->nb_slice_ctx, 1);
     int x, y;
+    int ret;
 
-    FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
-                      row_mb_num, 8 * sizeof(uint8_t), fail)
+    FF_ALLOCZ_ARRAY_OR_GOTO(h->intra4x4_pred_mode,
+                      row_mb_num, 8 * sizeof(uint8_t), ret, return ret)
     h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
 
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
-                      big_mb_num * 48 * sizeof(uint8_t), fail)
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
-                      (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
-                      big_mb_num * sizeof(uint16_t), fail)
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
-                      big_mb_num * sizeof(uint8_t), fail)
-    FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->mvd_table[0],
-                      row_mb_num, 16 * sizeof(uint8_t), fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->mvd_table[1],
-                      row_mb_num, 16 * sizeof(uint8_t), fail);
+    FF_ALLOCZ_OR_GOTO(h->non_zero_count,
+                      big_mb_num * 48 * sizeof(uint8_t), ret, return ret)
+    FF_ALLOCZ_OR_GOTO(h->slice_table_base,
+                      (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), ret, return ret)
+    FF_ALLOCZ_OR_GOTO(h->cbp_table,
+                      big_mb_num * sizeof(uint16_t), ret, return ret)
+    FF_ALLOCZ_OR_GOTO(h->chroma_pred_mode_table,
+                      big_mb_num * sizeof(uint8_t), ret, return ret)
+    FF_ALLOCZ_ARRAY_OR_GOTO(h->mvd_table[0],
+                      row_mb_num, 16 * sizeof(uint8_t), ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(h->mvd_table[1],
+                      row_mb_num, 16 * sizeof(uint8_t), ret, return ret);
     h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
     h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
 
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
-                      4 * big_mb_num * sizeof(uint8_t), fail);
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
-                      big_mb_num * sizeof(uint8_t), fail)
+    FF_ALLOCZ_OR_GOTO(h->direct_table,
+                      4 * big_mb_num * sizeof(uint8_t), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(h->list_counts,
+                      big_mb_num * sizeof(uint8_t), ret, return ret)
 
     memset(h->slice_table_base, -1,
            (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
     h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
 
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
-                      big_mb_num * sizeof(uint32_t), fail);
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
-                      big_mb_num * sizeof(uint32_t), fail);
+    FF_ALLOCZ_OR_GOTO(h->mb2b_xy,
+                      big_mb_num * sizeof(uint32_t), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(h->mb2br_xy,
+                      big_mb_num * sizeof(uint32_t), ret, return ret);
     for (y = 0; y < h->mb_height; y++)
         for (x = 0; x < h->mb_width; x++) {
             const int mb_xy = x + y * h->mb_stride;
@@ -224,10 +225,6 @@ int ff_h264_alloc_tables(H264Context *h)
         }
 
     return 0;
-
-fail:
-    ff_h264_free_tables(h);
-    return AVERROR(ENOMEM);
 }
 
 /**
@@ -242,6 +239,7 @@ int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
     int c_size  = h->mb_stride * (h->mb_height + 1);
     int yc_size = y_size + 2   * c_size;
     int x, y, i;
+    int ret;
 
     sl->ref_cache[0][scan8[5]  + 1] =
     sl->ref_cache[0][scan8[7]  + 1] =
@@ -268,8 +266,8 @@ int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
         er->b8_stride   = h->mb_width * 2 + 1;
 
         // error resilience code looks cleaner with this
-        FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy,
-                          (h->mb_num + 1) * sizeof(int), fail);
+        FF_ALLOCZ_OR_GOTO(er->mb_index2xy,
+                          (h->mb_num + 1) * sizeof(int), ret, return ret);
 
         for (y = 0; y < h->mb_height; y++)
             for (x = 0; x < h->mb_width; x++)
@@ -278,14 +276,14 @@ int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
         er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
                                                       h->mb_stride + h->mb_width;
 
-        FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
-                          mb_array_size * sizeof(uint8_t), fail);
+        FF_ALLOCZ_OR_GOTO(er->error_status_table,
+                          mb_array_size * sizeof(uint8_t), ret, return ret);
 
-        FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
-                         h->mb_height * h->mb_stride * (4*sizeof(int) + 1), fail);
+        FF_ALLOC_OR_GOTO(er->er_temp_buffer,
+                         h->mb_height * h->mb_stride * (4*sizeof(int) + 1), ret, return ret);
 
-        FF_ALLOCZ_OR_GOTO(h->avctx, sl->dc_val_base,
-                          yc_size * sizeof(int16_t), fail);
+        FF_ALLOCZ_OR_GOTO(sl->dc_val_base,
+                          yc_size * sizeof(int16_t), ret, return ret);
         er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
         er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
         er->dc_val[2] = er->dc_val[1] + c_size;
@@ -294,9 +292,6 @@ int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
     }
 
     return 0;
-
-fail:
-    return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
 }
 
 static int h264_init_context(AVCodecContext *avctx, H264Context *h)
diff --git a/libavcodec/iirfilter.c b/libavcodec/iirfilter.c
index b202515..e00cf39 100644
--- a/libavcodec/iirfilter.c
+++ b/libavcodec/iirfilter.c
@@ -171,12 +171,12 @@ av_cold struct FFIIRFilterCoeffs *ff_iir_filter_init_coeffs(void *avc,
     if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
         return NULL;
 
-    FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
-                      init_fail);
-    FF_ALLOC_OR_GOTO(avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
-                     init_fail);
-    FF_ALLOC_OR_GOTO(avc, c->cy, sizeof(c->cy[0]) * order,
-                     init_fail);
+    FF_ALLOCZ_OR_GOTO(c, sizeof(FFIIRFilterCoeffs),
+                      ret, goto init_fail);
+    FF_ALLOC_OR_GOTO(c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
+                     ret, goto init_fail);
+    FF_ALLOC_OR_GOTO(c->cy, sizeof(c->cy[0]) * order,
+                     ret, goto init_fail);
     c->order = order;
 
     switch (filt_type) {
diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c
index 5fce25e..7d70cc8 100644
--- a/libavcodec/mpegpicture.c
+++ b/libavcodec/mpegpicture.c
@@ -60,6 +60,7 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
 {
 #   define EMU_EDGE_HEIGHT (4 * 70)
     int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
+    int ret;
 
     if (avctx->hwaccel)
         return 0;
@@ -78,11 +79,11 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
     // at uvlinesize. It supports only YUV420 so 24x24 is enough
     // linesize * interlaced * MBsize
     // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size, EMU_EDGE_HEIGHT,
-                      fail);
+    FF_ALLOCZ_ARRAY_OR_GOTO(sc->edge_emu_buffer, alloc_size, EMU_EDGE_HEIGHT,
+                      ret, goto fail);
 
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, me->scratchpad, alloc_size, 4 * 16 * 2,
-                      fail)
+    FF_ALLOCZ_ARRAY_OR_GOTO(me->scratchpad, alloc_size, 4 * 16 * 2,
+                      ret, goto fail)
     me->temp            = me->scratchpad;
     sc->rd_scratchpad   = me->scratchpad;
     sc->b_scratchpad    = me->scratchpad;
@@ -91,7 +92,7 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
     return 0;
 fail:
     av_freep(&sc->edge_emu_buffer);
-    return AVERROR(ENOMEM);
+    return ret;
 }
 
 /**
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 49fd1c9..aaf1b40 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -360,6 +360,7 @@ static int init_duplicate_context(MpegEncContext *s)
     int c_size = s->mb_stride * (s->mb_height + 1);
     int yc_size = y_size + 2 * c_size;
     int i;
+    int ret;
 
     if (s->mb_height & 1)
         yc_size += 2*s->b8_stride + 2*s->mb_stride;
@@ -372,25 +373,22 @@ static int init_duplicate_context(MpegEncContext *s)
     s->sc.obmc_scratchpad = NULL;
 
     if (s->encoding) {
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
-                          ME_MAP_SIZE * sizeof(uint32_t), fail)
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
-                          ME_MAP_SIZE * sizeof(uint32_t), fail)
+        FF_ALLOCZ_OR_GOTO(s->me.map, ME_MAP_SIZE * sizeof(uint32_t), ret, return ret)
+        FF_ALLOCZ_OR_GOTO(s->me.score_map, ME_MAP_SIZE * sizeof(uint32_t), ret, return ret)
         if (s->noise_reduction) {
-            FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
-                              2 * 64 * sizeof(int), fail)
+            FF_ALLOCZ_OR_GOTO(s->dct_error_sum, 2 * 64 * sizeof(int), ret, return ret)
         }
     }
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
+    FF_ALLOCZ_OR_GOTO(s->blocks, 64 * 12 * 2 * sizeof(int16_t), ret, return ret)
     s->block = s->blocks[0];
 
     for (i = 0; i < 12; i++) {
         s->pblocks[i] = &s->block[i];
     }
 
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->block32, sizeof(*s->block32), fail)
+    FF_ALLOCZ_OR_GOTO(s->block32, sizeof(*s->block32), ret, return ret)
     s->dpcm_direction = 0;
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->dpcm_macroblock, sizeof(*s->dpcm_macroblock), fail)
+    FF_ALLOCZ_OR_GOTO(s->dpcm_macroblock, sizeof(*s->dpcm_macroblock), ret, return ret)
 
     if (s->avctx->codec_tag == AV_RL32("VCR2")) {
         // exchange uv
@@ -399,16 +397,13 @@ static int init_duplicate_context(MpegEncContext *s)
 
     if (s->out_format == FMT_H263) {
         /* ac values */
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
-                          yc_size * sizeof(int16_t) * 16, fail);
+        FF_ALLOCZ_OR_GOTO(s->ac_val_base, yc_size * sizeof(int16_t) * 16, ret, return ret);
         s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
         s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
         s->ac_val[2] = s->ac_val[1] + c_size;
     }
 
     return 0;
-fail:
-    return AVERROR(ENOMEM); // free() through ff_mpv_common_end()
 }
 
 static void free_duplicate_context(MpegEncContext *s)
@@ -687,6 +682,7 @@ void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
 static int init_context_frame(MpegEncContext *s)
 {
     int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
+    int ret;
 
     s->mb_width   = (s->width + 15) / 16;
     s->mb_stride  = s->mb_width + 1;
@@ -715,8 +711,8 @@ static int init_context_frame(MpegEncContext *s)
     if (s->mb_height & 1)
         yc_size += 2*s->b8_stride + 2*s->mb_stride;
 
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
-                      fail); // error resilience code looks cleaner with this
+    FF_ALLOCZ_OR_GOTO(s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
+                      ret, return ret); // error resilience code looks cleaner with this
     for (y = 0; y < s->mb_height; y++)
         for (x = 0; x < s->mb_width; x++)
             s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
@@ -725,12 +721,12 @@ static int init_context_frame(MpegEncContext *s)
 
     if (s->encoding) {
         /* Allocate MV tables */
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,                 mv_table_size * 2 * sizeof(int16_t), fail)
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), fail)
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), fail)
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), fail)
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), fail)
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,          mv_table_size * 2 * sizeof(int16_t), fail)
+        FF_ALLOCZ_OR_GOTO(s->p_mv_table_base,                 mv_table_size * 2 * sizeof(int16_t), ret, return ret)
+        FF_ALLOCZ_OR_GOTO(s->b_forw_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), ret, return ret)
+        FF_ALLOCZ_OR_GOTO(s->b_back_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), ret, return ret)
+        FF_ALLOCZ_OR_GOTO(s->b_bidir_forw_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), ret, return ret)
+        FF_ALLOCZ_OR_GOTO(s->b_bidir_back_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), ret, return ret)
+        FF_ALLOCZ_OR_GOTO(s->b_direct_mv_table_base,          mv_table_size * 2 * sizeof(int16_t), ret, return ret)
         s->p_mv_table            = s->p_mv_table_base + s->mb_stride + 1;
         s->b_forw_mv_table       = s->b_forw_mv_table_base + s->mb_stride + 1;
         s->b_back_mv_table       = s->b_back_mv_table_base + s->mb_stride + 1;
@@ -739,14 +735,14 @@ static int init_context_frame(MpegEncContext *s)
         s->b_direct_mv_table     = s->b_direct_mv_table_base + s->mb_stride + 1;
 
         /* Allocate MB type table */
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
+        FF_ALLOCZ_OR_GOTO(s->mb_type, mb_array_size * sizeof(uint16_t), ret, return ret) // needed for encoding
 
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
+        FF_ALLOCZ_OR_GOTO(s->lambda_table, mb_array_size * sizeof(int), ret, return ret)
 
-        FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
-                         mb_array_size * sizeof(float), fail);
-        FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
-                         mb_array_size * sizeof(float), fail);
+        FF_ALLOC_OR_GOTO(s->cplx_tab,
+                         mb_array_size * sizeof(float), ret, return ret);
+        FF_ALLOC_OR_GOTO(s->bits_tab,
+                         mb_array_size * sizeof(float), ret, return ret);
 
     }
 
@@ -757,34 +753,33 @@ static int init_context_frame(MpegEncContext *s)
             int j, k;
             for (j = 0; j < 2; j++) {
                 for (k = 0; k < 2; k++) {
-                    FF_ALLOCZ_OR_GOTO(s->avctx,
-                                      s->b_field_mv_table_base[i][j][k],
+                    FF_ALLOCZ_OR_GOTO(s->b_field_mv_table_base[i][j][k],
                                       mv_table_size * 2 * sizeof(int16_t),
-                                      fail);
+                                      ret, return ret);
                     s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
                                                    s->mb_stride + 1;
                 }
-                FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
-                FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
+                FF_ALLOCZ_OR_GOTO(s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), ret, return ret)
+                FF_ALLOCZ_OR_GOTO(s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), ret, return ret)
                 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
             }
-            FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
+            FF_ALLOCZ_OR_GOTO(s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), ret, return ret)
         }
     }
     if (s->out_format == FMT_H263) {
         /* cbp values */
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
+        FF_ALLOCZ_OR_GOTO(s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, ret, return ret);
         s->coded_block = s->coded_block_base + s->b8_stride + 1;
 
         /* cbp, ac_pred, pred_dir */
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table     , mb_array_size * sizeof(uint8_t), fail);
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
+        FF_ALLOCZ_OR_GOTO(s->cbp_table     , mb_array_size * sizeof(uint8_t), ret, return ret);
+        FF_ALLOCZ_OR_GOTO(s->pred_dir_table, mb_array_size * sizeof(uint8_t), ret, return ret);
     }
 
     if (s->h263_pred || s->h263_plus || !s->encoding) {
         /* dc values */
         // MN: we need these for error resilience of intra-frames
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
+        FF_ALLOCZ_OR_GOTO(s->dc_val_base, yc_size * sizeof(int16_t), ret, return ret);
         s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
         s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
         s->dc_val[2] = s->dc_val[1] + c_size;
@@ -793,16 +788,14 @@ static int init_context_frame(MpegEncContext *s)
     }
 
     /* which mb is an intra block */
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
+    FF_ALLOCZ_OR_GOTO(s->mbintra_table, mb_array_size, ret, return ret);
     memset(s->mbintra_table, 1, mb_array_size);
 
     /* init macroblock skip table */
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
+    FF_ALLOCZ_OR_GOTO(s->mbskip_table, mb_array_size + 2, ret, return ret);
     // Note the + 1 is for a quicker MPEG-4 slice_end detection
 
     return ff_mpeg_er_init(s);
-fail:
-    return AVERROR(ENOMEM);
 }
 
 static void clear_context(MpegEncContext *s)
@@ -934,8 +927,7 @@ av_cold int ff_mpv_common_init(MpegEncContext *s)
     if (ret)
         return ret;
 
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
-                      MAX_PICTURE_COUNT * sizeof(Picture), fail_nomem);
+    FF_ALLOCZ_OR_GOTO(s->picture, MAX_PICTURE_COUNT * sizeof(Picture), ret, return ret);
     for (i = 0; i < MAX_PICTURE_COUNT; i++) {
         s->picture[i].f = av_frame_alloc();
         if (!s->picture[i].f)
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 50ae57e..bc0fc4f 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -924,27 +924,23 @@ FF_ENABLE_DEPRECATION_WARNINGS
     ff_qpeldsp_init(&s->qdsp);
 
     if (s->msmpeg4_version) {
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
-                          2 * 2 * (MAX_LEVEL + 1) *
-                          (MAX_RUN + 1) * 2 * sizeof(int), fail);
+        FF_ALLOCZ_OR_GOTO(s->ac_stats, 2 * 2 * (MAX_LEVEL + 1) *
+                          (MAX_RUN + 1) * 2 * sizeof(int), ret, return ret);
     }
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
+    FF_ALLOCZ_OR_GOTO(s->avctx->stats_out, 256, ret, return ret);
 
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,   64 * 32 * sizeof(int), fail);
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail);
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,   64 * 32 * sizeof(int), fail);
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
-                      MAX_PICTURE_COUNT * sizeof(Picture *), fail);
-    FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
-                      MAX_PICTURE_COUNT * sizeof(Picture *), fail);
+    FF_ALLOCZ_OR_GOTO(s->q_intra_matrix,   64 * 32 * sizeof(int), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(s->q_inter_matrix,   64 * 32 * sizeof(int), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), ret, return ret);
+    FF_ALLOCZ_OR_GOTO(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), ret, return ret);
 
 
     if (s->noise_reduction) {
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
-                          2 * 64 * sizeof(uint16_t), fail);
+        FF_ALLOCZ_OR_GOTO(s->dct_offset, 2 * 64 * sizeof(uint16_t), ret, return ret);
     }
 
     ff_dct_encode_init(s);
@@ -1059,9 +1055,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
     cpb_props->buffer_size = avctx->rc_buffer_size;
 
     return 0;
-fail:
-    ff_mpv_encode_end(avctx);
-    return AVERROR_UNKNOWN;
 }
 
 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index a3e6afc..07c3d5e 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -431,6 +431,7 @@ av_cold int ff_snow_common_init(AVCodecContext *avctx){
     SnowContext *s = avctx->priv_data;
     int width, height;
     int i, j;
+    int ret;
 
     s->avctx= avctx;
     s->max_ref_frames=1; //just make sure it's not an invalid value in case of no initial keyframe
@@ -487,28 +488,26 @@ av_cold int ff_snow_common_init(AVCodecContext *avctx){
     width= s->avctx->width;
     height= s->avctx->height;
 
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->spatial_idwt_buffer, width, height * sizeof(IDWTELEM), fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->spatial_dwt_buffer,  width, height * sizeof(DWTELEM),  fail); //FIXME this does not belong here
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->temp_dwt_buffer,     width, sizeof(DWTELEM),  fail);
-    FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->temp_idwt_buffer,    width, sizeof(IDWTELEM), fail);
-    FF_ALLOC_ARRAY_OR_GOTO(avctx,  s->run_buffer,          ((width + 1) >> 1), ((height + 1) >> 1) * sizeof(*s->run_buffer), fail);
+    FF_ALLOCZ_ARRAY_OR_GOTO(s->spatial_idwt_buffer, width, height * sizeof(IDWTELEM), ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(s->spatial_dwt_buffer,  width, height * sizeof(DWTELEM),  ret, return ret); //FIXME this does not belong here
+    FF_ALLOCZ_ARRAY_OR_GOTO(s->temp_dwt_buffer,     width, sizeof(DWTELEM), ret, return ret);
+    FF_ALLOCZ_ARRAY_OR_GOTO(s->temp_idwt_buffer,    width, sizeof(IDWTELEM), ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(s->run_buffer,          ((width + 1) >> 1), ((height + 1) >> 1) * sizeof(*s->run_buffer), ret, return ret);
 
     for(i=0; i<MAX_REF_FRAMES; i++) {
         for(j=0; j<MAX_REF_FRAMES; j++)
             ff_scale_mv_ref[i][j] = 256*(i+1)/(j+1);
         s->last_picture[i] = av_frame_alloc();
         if (!s->last_picture[i])
-            goto fail;
+            return AVERROR(ENOMEM);
     }
 
     s->mconly_picture = av_frame_alloc();
     s->current_picture = av_frame_alloc();
     if (!s->mconly_picture || !s->current_picture)
-        goto fail;
+        return AVERROR(ENOMEM);
 
     return 0;
-fail:
-    return AVERROR(ENOMEM);
 }
 
 int ff_snow_common_init_after_header(AVCodecContext *avctx) {
@@ -520,9 +519,9 @@ int ff_snow_common_init_after_header(AVCodecContext *avctx) {
         if ((ret = ff_get_buffer(s->avctx, s->mconly_picture,
                                  AV_GET_BUFFER_FLAG_REF)) < 0)
             return ret;
-        FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->scratchbuf, FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256), 7*MB_SIZE, fail);
+        FF_ALLOCZ_ARRAY_OR_GOTO(s->scratchbuf, FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256), 7*MB_SIZE, ret, return ret);
         emu_buf_size = FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * (2 * MB_SIZE + HTAPS_MAX - 1);
-        FF_ALLOC_OR_GOTO(avctx, s->emu_edge_buffer, emu_buf_size, fail);
+        FF_ALLOC_OR_GOTO(s->emu_edge_buffer, emu_buf_size, ret, return ret);
     }
 
     if(s->mconly_picture->format != avctx->pix_fmt) {
@@ -571,7 +570,7 @@ int ff_snow_common_init_after_header(AVCodecContext *avctx) {
                 av_freep(&b->x_coeff);
                 b->x_coeff=av_mallocz_array(((b->width+1) * b->height+1), sizeof(x_and_coeff));
                 if (!b->x_coeff)
-                    goto fail;
+                    return AVERROR(ENOMEM);
             }
             w= (w+1)>>1;
             h= (h+1)>>1;
@@ -579,8 +578,6 @@ int ff_snow_common_init_after_header(AVCodecContext *avctx) {
     }
 
     return 0;
-fail:
-    return AVERROR(ENOMEM);
 }
 
 #define USE_HALFPEL_PLANE 0
diff --git a/libavcodec/twinvq.c b/libavcodec/twinvq.c
index 34ca184..74a3ba7 100644
--- a/libavcodec/twinvq.c
+++ b/libavcodec/twinvq.c
@@ -546,24 +546,24 @@ static av_cold int init_mdct_win(TwinVQContext *tctx)
             return ret;
     }
 
-    FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->tmp_buf,
-                     mtab->size, sizeof(*tctx->tmp_buf), alloc_fail);
+    FF_ALLOC_ARRAY_OR_GOTO(tctx->tmp_buf,
+                     mtab->size, sizeof(*tctx->tmp_buf), ret, return ret);
 
-    FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->spectrum,
+    FF_ALLOC_ARRAY_OR_GOTO(tctx->spectrum,
                      2 * mtab->size, channels * sizeof(*tctx->spectrum),
-                     alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->curr_frame,
+                     ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(tctx->curr_frame,
                      2 * mtab->size, channels * sizeof(*tctx->curr_frame),
-                     alloc_fail);
-    FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->prev_frame,
+                     ret, return ret);
+    FF_ALLOC_ARRAY_OR_GOTO(tctx->prev_frame,
                      2 * mtab->size, channels * sizeof(*tctx->prev_frame),
-                     alloc_fail);
+                     ret, return ret);
 
     for (i = 0; i < 3; i++) {
         int m       = 4 * mtab->size / mtab->fmode[i].sub;
         double freq = 2 * M_PI / m;
-        FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->cos_tabs[i],
-                         (m / 4), sizeof(*tctx->cos_tabs[i]), alloc_fail);
+        FF_ALLOC_ARRAY_OR_GOTO(tctx->cos_tabs[i],
+                         (m / 4), sizeof(*tctx->cos_tabs[i]), ret, return ret);
 
         for (j = 0; j <= m / 8; j++)
             tctx->cos_tabs[i][j] = cos((2 * j + 1) * freq);
@@ -576,9 +576,6 @@ static av_cold int init_mdct_win(TwinVQContext *tctx)
     ff_init_ff_sine_windows(av_log2(mtab->size));
 
     return 0;
-
-alloc_fail:
-    return AVERROR(ENOMEM);
 }
 
 /**
diff --git a/libavutil/internal.h b/libavutil/internal.h
index 4acbcf5..9cf5780 100644
--- a/libavutil/internal.h
+++ b/libavutil/internal.h
@@ -137,39 +137,39 @@
 #   define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_A(32, t, v, __VA_ARGS__,,))
 #endif
 
-#define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
+#define FF_ALLOC_OR_GOTO(p, size, ret, errstatement)\
 {\
     p = av_malloc(size);\
     if (!(p) && (size) != 0) {\
-        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
-        goto label;\
+        ret = AVERROR(ENOMEM);\
+        errstatement;\
     }\
 }
 
-#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
+#define FF_ALLOCZ_OR_GOTO(p, size, ret, errstatement)\
 {\
     p = av_mallocz(size);\
     if (!(p) && (size) != 0) {\
-        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
-        goto label;\
+        ret = AVERROR(ENOMEM);\
+        errstatement;\
     }\
 }
 
-#define FF_ALLOC_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)\
+#define FF_ALLOC_ARRAY_OR_GOTO(p, nelem, elsize, ret, errstatement)\
 {\
     p = av_malloc_array(nelem, elsize);\
     if (!p) {\
-        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
-        goto label;\
+        ret = AVERROR(ENOMEM);\
+        errstatement;\
     }\
 }
 
-#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)\
+#define FF_ALLOCZ_ARRAY_OR_GOTO(p, nelem, elsize, ret, errstatement)\
 {\
     p = av_mallocz_array(nelem, elsize);\
     if (!p) {\
-        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
-        goto label;\
+        ret = AVERROR(ENOMEM);\
+        errstatement;\
     }\
 }
 
diff --git a/libswscale/utils.c b/libswscale/utils.c
index 15c0a19..8ff6a9c 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -352,13 +352,13 @@ static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
     emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
 
     // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
-    FF_ALLOC_ARRAY_OR_GOTO(NULL, *filterPos, (dstW + 3), sizeof(**filterPos), fail);
+    FF_ALLOC_ARRAY_OR_GOTO(*filterPos, (dstW + 3), sizeof(**filterPos), ret, goto fail);
 
     if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
         int i;
         filterSize = 1;
-        FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter,
-                                dstW, sizeof(*filter) * filterSize, fail);
+        FF_ALLOCZ_ARRAY_OR_GOTO(filter,
+                                dstW, sizeof(*filter) * filterSize, ret, goto fail);
 
         for (i = 0; i < dstW; i++) {
             filter[i * filterSize] = fone;
@@ -368,8 +368,8 @@ static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
         int i;
         int64_t xDstInSrc;
         filterSize = 1;
-        FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
-                               dstW, sizeof(*filter) * filterSize, fail);
+        FF_ALLOC_ARRAY_OR_GOTO(filter,
+                               dstW, sizeof(*filter) * filterSize, ret, goto fail);
 
         xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
         for (i = 0; i < dstW; i++) {
@@ -384,8 +384,8 @@ static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
         int i;
         int64_t xDstInSrc;
         filterSize = 2;
-        FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
-                               dstW, sizeof(*filter) * filterSize, fail);
+        FF_ALLOC_ARRAY_OR_GOTO(filter,
+                               dstW, sizeof(*filter) * filterSize, ret, goto fail);
 
         xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
         for (i = 0; i < dstW; i++) {
@@ -425,8 +425,8 @@ static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
         filterSize = FFMIN(filterSize, srcW - 2);
         filterSize = FFMAX(filterSize, 1);
 
-        FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
-                               dstW, sizeof(*filter) * filterSize, fail);
+        FF_ALLOC_ARRAY_OR_GOTO(filter,
+                               dstW, sizeof(*filter) * filterSize, ret, goto fail);
 
         xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
         for (i = 0; i < dstW; i++) {
@@ -525,7 +525,7 @@ static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
     if (dstFilter)
         filter2Size += dstFilter->length - 1;
     av_assert0(filter2Size > 0);
-    FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter2, dstW, filter2Size * sizeof(*filter2), fail);
+    FF_ALLOCZ_ARRAY_OR_GOTO(filter2, dstW, filter2Size * sizeof(*filter2), ret, goto fail);
 
     for (i = 0; i < dstW; i++) {
         int j, k;
@@ -684,8 +684,8 @@ static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
 
     // Note the +1 is for the MMX scaler which reads over the end
     /* align at 16 for AltiVec (needed by hScale_altivec_real) */
-    FF_ALLOCZ_ARRAY_OR_GOTO(NULL, *outFilter,
-                            (dstW + 3), *outFilterSize * sizeof(int16_t), fail);
+    FF_ALLOCZ_ARRAY_OR_GOTO(*outFilter,
+                            (dstW + 3), *outFilterSize * sizeof(int16_t), ret, goto fail);
 
     /* normalize & store in outFilter */
     for (i = 0; i < dstW; i++) {
@@ -1415,7 +1415,7 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
     c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
     c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
 
-    FF_ALLOCZ_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
+    FF_ALLOCZ_OR_GOTO(c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, ret, goto fail);
 
     c->srcBpc = desc_src->comp[0].depth;
     if (c->srcBpc < 8)
@@ -1655,10 +1655,10 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
                 return AVERROR(ENOMEM);
             }
 
-            FF_ALLOCZ_OR_GOTO(c, c->hLumFilter,    (dstW           / 8 + 8) * sizeof(int16_t), fail);
-            FF_ALLOCZ_OR_GOTO(c, c->hChrFilter,    (c->chrDstW     / 4 + 8) * sizeof(int16_t), fail);
-            FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW       / 2 / 8 + 8) * sizeof(int32_t), fail);
-            FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
+            FF_ALLOCZ_OR_GOTO(c->hLumFilter,    (dstW           / 8 + 8) * sizeof(int16_t), ret, goto fail);
+            FF_ALLOCZ_OR_GOTO(c->hChrFilter,    (c->chrDstW     / 4 + 8) * sizeof(int16_t), ret, goto fail);
+            FF_ALLOCZ_OR_GOTO(c->hLumFilterPos, (dstW       / 2 / 8 + 8) * sizeof(int32_t), ret, goto fail);
+            FF_ALLOCZ_OR_GOTO(c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), ret, goto fail);
 
             ff_init_hscaler_mmxext(      dstW, c->lumXInc, c->lumMmxextFilterCode,
                                 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
@@ -1726,8 +1726,8 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
             goto fail;
 
 #if HAVE_ALTIVEC
-        FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH,    fail);
-        FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
+        FF_ALLOC_OR_GOTO(c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH,    fail);
+        FF_ALLOC_OR_GOTO(c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
 
         for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
             int j;
@@ -1746,7 +1746,7 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
     }
 
     for (i = 0; i < 4; i++)
-        FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
+        FF_ALLOCZ_OR_GOTO(c->dither_error[i], (c->dstW+2) * sizeof(int), ret, goto fail);
 
     c->needAlpha = (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) ? 1 : 0;
 
