diff mbox series

[FFmpeg-devel,14/20] avcodec/snow: Only allocate mconly_picture for the decoder

Message ID AS8P250MB074465289EA5C2D4BB9B4B028F209@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State New
Headers show
Series [FFmpeg-devel,1/8] configure: Remove dependencies of inexistant rtjpeg decoder | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt Oct. 10, 2022, 3:13 a.m. UTC
And use its presence to check whether buffers should be allocated
for it in ff_snow_common_init_after_header().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/snow.c    | 18 +++++++++---------
 libavcodec/snowdec.c | 15 ++++++++++++++-
 2 files changed, 23 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index f55b3310ac..7fb1660dd6 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -468,9 +468,8 @@  av_cold int ff_snow_common_init(AVCodecContext *avctx){
             return AVERROR(ENOMEM);
     }
 
-    s->mconly_picture = av_frame_alloc();
     s->current_picture = av_frame_alloc();
-    if (!s->mconly_picture || !s->current_picture)
+    if (!s->current_picture)
         return AVERROR(ENOMEM);
 
     ff_thread_once(&init_static_once, snow_static_init);
@@ -481,22 +480,24 @@  av_cold int ff_snow_common_init(AVCodecContext *avctx){
 int ff_snow_common_init_after_header(AVCodecContext *avctx) {
     SnowContext *s = avctx->priv_data;
     int plane_index, level, orientation;
-    int ret, emu_buf_size;
+    int ret;
 
     if(!s->scratchbuf) {
-        if (av_codec_is_decoder(avctx->codec)) {
+        size_t size = 2U * avctx->width + 256;
+
+        if (s->mconly_picture) {
             if ((ret = ff_get_buffer(s->avctx, s->mconly_picture,
                                      AV_GET_BUFFER_FLAG_REF)) < 0)
                 return ret;
+            size = FFMAX(FFABS(s->mconly_picture->linesize[0]), size);
         }
 
-        emu_buf_size = FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * (2 * MB_SIZE + HTAPS_MAX - 1);
-        if (!FF_ALLOCZ_TYPED_ARRAY(s->scratchbuf,      FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * 7 * MB_SIZE) ||
-            !FF_ALLOCZ_TYPED_ARRAY(s->emu_edge_buffer, emu_buf_size))
+        if (!(s->scratchbuf = av_calloc(size, 7 * MB_SIZE)) ||
+            !(s->emu_edge_buffer = av_calloc(size, 2 * MB_SIZE + HTAPS_MAX - 1)))
             return AVERROR(ENOMEM);
     }
 
-    if (av_codec_is_decoder(avctx->codec) &&
+    if (s->mconly_picture &&
         s->mconly_picture->format != avctx->pix_fmt) {
         av_log(avctx, AV_LOG_ERROR, "pixel format changed\n");
         return AVERROR_INVALIDDATA;
@@ -626,6 +627,5 @@  av_cold void ff_snow_common_end(SnowContext *s)
             }
         }
     }
-    av_frame_free(&s->mconly_picture);
     av_frame_free(&s->current_picture);
 }
diff --git a/libavcodec/snowdec.c b/libavcodec/snowdec.c
index 3a835650a5..96221b9802 100644
--- a/libavcodec/snowdec.c
+++ b/libavcodec/snowdec.c
@@ -794,19 +794,32 @@  static av_cold int decode_end(AVCodecContext *avctx)
 
     ff_snow_common_end(s);
 
+    av_frame_free(&s->mconly_picture);
+
     s->avmv_size = 0;
     av_freep(&s->avmv);
 
     return 0;
 }
 
+static av_cold int snow_decode_init(AVCodecContext *avctx)
+{
+    SnowContext *const s = avctx->priv_data;
+
+    s->mconly_picture = av_frame_alloc();
+    if (!s->mconly_picture)
+        return AVERROR(ENOMEM);
+
+    return ff_snow_common_init(avctx);
+}
+
 const FFCodec ff_snow_decoder = {
     .p.name         = "snow",
     CODEC_LONG_NAME("Snow"),
     .p.type         = AVMEDIA_TYPE_VIDEO,
     .p.id           = AV_CODEC_ID_SNOW,
     .priv_data_size = sizeof(SnowContext),
-    .init           = ff_snow_common_init,
+    .init           = snow_decode_init,
     .close          = decode_end,
     FF_CODEC_DECODE_CB(decode_frame),
     .p.capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,