diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index 7fb1660dd6..7e93005492 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -456,10 +456,7 @@ av_cold int ff_snow_common_init(AVCodecContext *avctx){
     height= s->avctx->height;
 
     if (!FF_ALLOCZ_TYPED_ARRAY(s->spatial_idwt_buffer, width * height) ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->spatial_dwt_buffer,  width * height) ||  //FIXME this does not belong here
-        !FF_ALLOCZ_TYPED_ARRAY(s->temp_dwt_buffer,     width)          ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->temp_idwt_buffer,    width)          ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->run_buffer, ((width + 1) >> 1) * ((height + 1) >> 1)))
+        !FF_ALLOCZ_TYPED_ARRAY(s->temp_idwt_buffer,    width))
         return AVERROR(ENOMEM);
 
     for(i=0; i<MAX_REF_FRAMES; i++) {
@@ -492,8 +489,7 @@ int ff_snow_common_init_after_header(AVCodecContext *avctx) {
             size = FFMAX(FFABS(s->mconly_picture->linesize[0]), size);
         }
 
-        if (!(s->scratchbuf = av_calloc(size, 7 * MB_SIZE)) ||
-            !(s->emu_edge_buffer = av_calloc(size, 2 * MB_SIZE + HTAPS_MAX - 1)))
+        if (!(s->scratchbuf = av_calloc(size, 7 * MB_SIZE)))
             return AVERROR(ENOMEM);
     }
 
@@ -517,8 +513,8 @@ int ff_snow_common_init_after_header(AVCodecContext *avctx) {
         for(level=s->spatial_decomposition_count-1; level>=0; level--){
             for(orientation=level ? 1 : 0; orientation<4; orientation++){
                 SubBand *b= &s->plane[plane_index].band[level][orientation];
+                ptrdiff_t offset = 0;
 
-                b->buf= s->spatial_dwt_buffer;
                 b->level= level;
                 b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
                 b->width = (w + !(orientation&1))>>1;
@@ -529,17 +525,20 @@ int ff_snow_common_init_after_header(AVCodecContext *avctx) {
                 b->buf_y_offset = 0;
 
                 if(orientation&1){
-                    b->buf += (w+1)>>1;
+                    offset += (w + 1) >> 1;
                     b->buf_x_offset = (w+1)>>1;
                 }
                 if(orientation>1){
-                    b->buf += b->stride>>1;
+                    offset += b->stride >> 1;
                     b->buf_y_offset = b->stride_line >> 1;
                 }
-                b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
+                b->ibuf = s->spatial_idwt_buffer + offset;
 
                 if(level)
                     b->parent= &s->plane[plane_index].band[level-1][orientation];
+                if (s->spatial_dwt_buffer) { /* Equivalently: if s is encoder */
+                    b->buf = s->spatial_dwt_buffer + offset;
+                }
                 //FIXME avoid this realloc
                 av_freep(&b->x_coeff);
                 b->x_coeff = av_calloc((b->width + 1) * b->height + 1,
@@ -601,15 +600,11 @@ av_cold void ff_snow_common_end(SnowContext *s)
 {
     int plane_index, level, orientation, i;
 
-    av_freep(&s->spatial_dwt_buffer);
-    av_freep(&s->temp_dwt_buffer);
     av_freep(&s->spatial_idwt_buffer);
     av_freep(&s->temp_idwt_buffer);
-    av_freep(&s->run_buffer);
 
     av_freep(&s->block);
     av_freep(&s->scratchbuf);
-    av_freep(&s->emu_edge_buffer);
 
     for(i=0; i<MAX_REF_FRAMES; i++){
         if(s->last_picture[i] && s->last_picture[i]->data[0]) {
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index fec061a8c5..b8148cbfce 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -104,7 +104,7 @@ static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2)
 static av_cold int encode_init(AVCodecContext *avctx)
 {
     SnowContext *s = avctx->priv_data;
-    int plane_index, ret;
+    int plane_index, ret, height, width;
     int i;
 
     if(s->pred == DWT_97
@@ -164,6 +164,16 @@ static av_cold int encode_init(AVCodecContext *avctx)
 
     ff_snow_alloc_blocks(s);
 
+    width  = avctx->width;
+    height = avctx->height;
+    if(!FF_ALLOCZ_TYPED_ARRAY(s->spatial_dwt_buffer,  width * height) ||
+       !FF_ALLOCZ_TYPED_ARRAY(s->temp_dwt_buffer,     width)          ||
+       !FF_ALLOCZ_TYPED_ARRAY(s->run_buffer, ((width + 1) >> 1) * ((height + 1) >> 1)))
+       return AVERROR(ENOMEM);
+    s->emu_edge_buffer = av_calloc(width + 128U, 2 * (2 * MB_SIZE + HTAPS_MAX - 1));
+    if (!s->emu_edge_buffer)
+        return AVERROR(ENOMEM);
+
     s->version=0;
 
     s->m.avctx   = avctx;
@@ -1986,6 +1996,10 @@ static av_cold int encode_end(AVCodecContext *avctx)
     ff_snow_common_end(s);
     ff_rate_control_uninit(&s->m);
     av_frame_free(&s->input_picture);
+    av_freep(&s->emu_edge_buffer);
+    av_freep(&s->spatial_dwt_buffer);
+    av_freep(&s->temp_dwt_buffer);
+    av_freep(&s->run_buffer);
 
     for (int i = 0; i < MAX_REF_FRAMES; i++) {
         av_freep(&s->ref_mvs[i]);
