diff mbox series

[FFmpeg-devel,6/9] avcodec/snow: Split ff_snow_get_buffer()

Message ID AS8P250MB07445E095A140693F39559048FFCA@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 418332e01ce99d43f218504e4e4e1b4ea89c9a49
Headers show
Series [FFmpeg-devel,1/9] avfilter/bwdif: Add proper BWDIFDSPContext | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 25, 2023, 6:04 p.m. UTC
The part of said function that is common to both encoder and decoder
is negligible since c954cf1e1b766a0d1992d5be0a8be0055a8e1a6a
and more than offset by the costs of "Am I an encoder?" checks.

So move allocating the frames to the encoder and decoder directly.
Also rename ff_snow_frame_start() to ff_snow_frames_prepare(),
because a frame without a buffer has not been properly started.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/snow.c    | 46 +++++---------------------------------------
 libavcodec/snow.h    |  3 +--
 libavcodec/snowdec.c |  9 ++++++++-
 libavcodec/snowenc.c | 29 ++++++++++++++++++++++++++--
 4 files changed, 41 insertions(+), 46 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index 5eb3ee1e9e..09f2d60f47 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -22,7 +22,6 @@ 
 #include "libavutil/thread.h"
 #include "avcodec.h"
 #include "decode.h"
-#include "encode.h"
 #include "snow_dwt.h"
 #include "snow.h"
 #include "snowdata.h"
@@ -61,36 +60,6 @@  void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_
     }
 }
 
-int ff_snow_get_buffer(SnowContext *s, AVFrame *frame)
-{
-    int ret, i;
-    int edges_needed = av_codec_is_encoder(s->avctx->codec);
-
-    frame->width  = s->avctx->width ;
-    frame->height = s->avctx->height;
-    if (edges_needed) {
-        frame->width  += 2 * EDGE_WIDTH;
-        frame->height += 2 * EDGE_WIDTH;
-
-        ret = ff_encode_alloc_frame(s->avctx, frame);
-    } else
-        ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF);
-    if (ret < 0)
-        return ret;
-    if (edges_needed) {
-        for (i = 0; frame->data[i]; i++) {
-            int offset = (EDGE_WIDTH >> (i ? s->chroma_v_shift : 0)) *
-                            frame->linesize[i] +
-                            (EDGE_WIDTH >> (i ? s->chroma_h_shift : 0));
-            frame->data[i] += offset;
-        }
-        frame->width  = s->avctx->width;
-        frame->height = s->avctx->height;
-    }
-
-    return 0;
-}
-
 void ff_snow_reset_contexts(SnowContext *s){ //FIXME better initial contexts
     int plane_index, level, orientation;
 
@@ -589,20 +558,21 @@  void ff_snow_release_buffer(AVCodecContext *avctx)
     }
 }
 
-int ff_snow_frame_start(SnowContext *s){
+int ff_snow_frames_prepare(SnowContext *s)
+{
    AVFrame *tmp;
-   int i, ret;
 
     ff_snow_release_buffer(s->avctx);
 
     tmp= s->last_picture[s->max_ref_frames-1];
-    for(i=s->max_ref_frames-1; i>0; i--)
+    for (int i = s->max_ref_frames - 1; i > 0; i--)
         s->last_picture[i] = s->last_picture[i-1];
     s->last_picture[0] = s->current_picture;
     s->current_picture = tmp;
 
     if(s->keyframe){
         s->ref_frames= 0;
+        s->current_picture->flags |= AV_FRAME_FLAG_KEY;
     }else{
         int i;
         for(i=0; i<s->max_ref_frames && s->last_picture[i]->data[0]; i++)
@@ -613,14 +583,8 @@  int ff_snow_frame_start(SnowContext *s){
             av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
             return AVERROR_INVALIDDATA;
         }
-    }
-    if ((ret = ff_snow_get_buffer(s, s->current_picture)) < 0)
-        return ret;
-
-    if (s->keyframe)
-        s->current_picture->flags |= AV_FRAME_FLAG_KEY;
-    else
         s->current_picture->flags &= ~AV_FRAME_FLAG_KEY;
+    }
 
     return 0;
 }
diff --git a/libavcodec/snow.h b/libavcodec/snow.h
index ed0f9abb42..73c4535ec1 100644
--- a/libavcodec/snow.h
+++ b/libavcodec/snow.h
@@ -245,11 +245,10 @@  void ff_snow_common_end(SnowContext *s);
 void ff_snow_release_buffer(AVCodecContext *avctx);
 void ff_snow_reset_contexts(SnowContext *s);
 int ff_snow_alloc_blocks(SnowContext *s);
-int ff_snow_frame_start(SnowContext *s);
+int ff_snow_frames_prepare(SnowContext *s);
 void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride,
                      int sx, int sy, int b_w, int b_h, const BlockNode *block,
                      int plane_index, int w, int h);
-int ff_snow_get_buffer(SnowContext *s, AVFrame *frame);
 /* common inline functions */
 //XXX doublecheck all of them should stay inlined
 
diff --git a/libavcodec/snowdec.c b/libavcodec/snowdec.c
index e014d5087f..489c09324e 100644
--- a/libavcodec/snowdec.c
+++ b/libavcodec/snowdec.c
@@ -24,6 +24,7 @@ 
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "codec_internal.h"
+#include "decode.h"
 #include "snow_dwt.h"
 #include "snow.h"
 
@@ -475,7 +476,13 @@  static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
 
     ff_snow_alloc_blocks(s);
 
-    if((res = ff_snow_frame_start(s)) < 0)
+    if ((res = ff_snow_frames_prepare(s)) < 0)
+        return res;
+
+    s->current_picture->width  = s->avctx->width;
+    s->current_picture->height = s->avctx->height;
+    res = ff_get_buffer(s->avctx, s->current_picture, AV_GET_BUFFER_FLAG_REF);
+    if (res < 0)
         return res;
 
     s->current_picture->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index 14a59ca67b..f954a686c1 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -39,6 +39,28 @@ 
 #include "mpegvideo.h"
 #include "h263enc.h"
 
+static int get_encode_buffer(SnowContext *s, AVFrame *frame)
+{
+    int ret;
+
+    frame->width  = s->avctx->width  + 2 * EDGE_WIDTH;
+    frame->height = s->avctx->height + 2 * EDGE_WIDTH;
+
+    ret = ff_encode_alloc_frame(s->avctx, frame);
+    if (ret < 0)
+        return ret;
+    for (int i = 0; frame->data[i]; i++) {
+        int offset = (EDGE_WIDTH >> (i ? s->chroma_v_shift : 0)) *
+                        frame->linesize[i] +
+                        (EDGE_WIDTH >> (i ? s->chroma_h_shift : 0));
+        frame->data[i] += offset;
+    }
+    frame->width  = s->avctx->width;
+    frame->height = s->avctx->height;
+
+    return 0;
+}
+
 static av_cold int encode_init(AVCodecContext *avctx)
 {
     SnowContext *s = avctx->priv_data;
@@ -140,7 +162,7 @@  static av_cold int encode_init(AVCodecContext *avctx)
     if (!s->input_picture)
         return AVERROR(ENOMEM);
 
-    if ((ret = ff_snow_get_buffer(s, s->input_picture)) < 0)
+    if ((ret = get_encode_buffer(s, s->input_picture)) < 0)
         return ret;
 
     if(s->motion_est == FF_ME_ITER){
@@ -1659,7 +1681,10 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         emms_c();
     }
 
-    ff_snow_frame_start(s);
+    ff_snow_frames_prepare(s);
+    ret = get_encode_buffer(s, s->current_picture);
+    if (ret < 0)
+        return ret;
 
     s->m.current_picture_ptr= &s->m.current_picture;
     s->m.current_picture.f = s->current_picture;