diff mbox series

[FFmpeg-devel,41/57] avcodec/mpegvideo_dec: Add close function for mpegvideo-decoders

Message ID AS8P250MB0744719E81F3E6DDBC07DF088F1B2@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State New
Headers show
Series [FFmpeg-devel,01/14] avcodec/get_buffer: Remove redundant check | expand

Commit Message

Andreas Rheinhardt April 29, 2024, 9:14 p.m. UTC
Currently identical to the H.261 and H.263 close functions
(which it replaces). It will be extended in future commits.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/flvdec.c        |  4 ++--
 libavcodec/h261dec.c       | 11 +----------
 libavcodec/h263dec.c       | 12 ++----------
 libavcodec/h263dec.h       |  1 -
 libavcodec/intelh263dec.c  |  2 +-
 libavcodec/mpeg12dec.c     |  3 +--
 libavcodec/mpeg4videodec.c |  2 +-
 libavcodec/mpegvideo_dec.c |  8 ++++++++
 libavcodec/mpegvideodec.h  |  1 +
 libavcodec/msmpeg4dec.c    |  9 +++++----
 libavcodec/rv10.c          | 12 ++----------
 libavcodec/rv30.c          |  1 +
 libavcodec/rv34.c          |  8 +++-----
 libavcodec/rv40.c          |  1 +
 libavcodec/vc1dec.c        | 18 ++++++++++++------
 libavcodec/wmv2dec.c       |  3 ++-
 16 files changed, 43 insertions(+), 53 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/flvdec.c b/libavcodec/flvdec.c
index 8baaed06a8..1bb1b12917 100644
--- a/libavcodec/flvdec.c
+++ b/libavcodec/flvdec.c
@@ -24,7 +24,7 @@ 
 #include "flvdec.h"
 #include "h263dec.h"
 #include "mpegvideo.h"
-#include "mpegvideodata.h"
+#include "mpegvideodec.h"
 
 int ff_flv_decode_picture_header(MpegEncContext *s)
 {
@@ -118,8 +118,8 @@  const FFCodec ff_flv_decoder = {
     .p.id           = AV_CODEC_ID_FLV1,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_h263_decode_init,
-    .close          = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
     .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .p.max_lowres   = 3,
diff --git a/libavcodec/h261dec.c b/libavcodec/h261dec.c
index 9acfd984ee..d8d0dcf3cf 100644
--- a/libavcodec/h261dec.c
+++ b/libavcodec/h261dec.c
@@ -660,15 +660,6 @@  static int h261_decode_frame(AVCodecContext *avctx, AVFrame *pict,
     return get_consumed_bytes(s, buf_size);
 }
 
-static av_cold int h261_decode_end(AVCodecContext *avctx)
-{
-    H261DecContext *const h = avctx->priv_data;
-    MpegEncContext *s = &h->s;
-
-    ff_mpv_common_end(s);
-    return 0;
-}
-
 const FFCodec ff_h261_decoder = {
     .p.name         = "h261",
     CODEC_LONG_NAME("H.261"),
@@ -676,8 +667,8 @@  const FFCodec ff_h261_decoder = {
     .p.id           = AV_CODEC_ID_H261,
     .priv_data_size = sizeof(H261DecContext),
     .init           = h261_decode_init,
-    .close          = h261_decode_end,
     FF_CODEC_DECODE_CB(h261_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DR1,
     .p.max_lowres   = 3,
 };
diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c
index 4fe4a30000..b8db4ffc98 100644
--- a/libavcodec/h263dec.c
+++ b/libavcodec/h263dec.c
@@ -159,14 +159,6 @@  av_cold int ff_h263_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
-av_cold int ff_h263_decode_end(AVCodecContext *avctx)
-{
-    MpegEncContext *s = avctx->priv_data;
-
-    ff_mpv_common_end(s);
-    return 0;
-}
-
 /**
  * Return the number of bytes consumed for building the current frame.
  */
@@ -702,8 +694,8 @@  const FFCodec ff_h263_decoder = {
     .p.id           = AV_CODEC_ID_H263,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_h263_decode_init,
-    .close          = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
                       AV_CODEC_CAP_DELAY,
     .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
@@ -719,8 +711,8 @@  const FFCodec ff_h263p_decoder = {
     .p.id           = AV_CODEC_ID_H263P,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_h263_decode_init,
-    .close          = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
                       AV_CODEC_CAP_DELAY,
     .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
diff --git a/libavcodec/h263dec.h b/libavcodec/h263dec.h
index a01acc0834..633d4aa577 100644
--- a/libavcodec/h263dec.h
+++ b/libavcodec/h263dec.h
@@ -47,7 +47,6 @@  int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code);
 int ff_h263_decode_init(AVCodecContext *avctx);
 int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *frame,
                          int *got_frame, AVPacket *avpkt);
-int ff_h263_decode_end(AVCodecContext *avctx);
 void ff_h263_decode_init_vlc(void);
 int ff_h263_decode_picture_header(MpegEncContext *s);
 int ff_h263_decode_gob_header(MpegEncContext *s);
diff --git a/libavcodec/intelh263dec.c b/libavcodec/intelh263dec.c
index 5d34892ef7..d4051c36f1 100644
--- a/libavcodec/intelh263dec.c
+++ b/libavcodec/intelh263dec.c
@@ -132,8 +132,8 @@  const FFCodec ff_h263i_decoder = {
     .p.id           = AV_CODEC_ID_H263I,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_h263_decode_init,
-    .close          = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
     .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
 };
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 0e2012b324..cf8493ba51 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -2595,9 +2595,8 @@  static av_cold int mpeg_decode_end(AVCodecContext *avctx)
 {
     Mpeg1Context *s = avctx->priv_data;
 
-    ff_mpv_common_end(&s->mpeg_enc_ctx);
     av_buffer_unref(&s->a53_buf_ref);
-    return 0;
+    return ff_mpv_decode_close(avctx);
 }
 
 const FFCodec ff_mpeg1video_decoder = {
diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c
index b138c94df9..58896250ce 100644
--- a/libavcodec/mpeg4videodec.c
+++ b/libavcodec/mpeg4videodec.c
@@ -3856,8 +3856,8 @@  const FFCodec ff_mpeg4_decoder = {
     .p.id                  = AV_CODEC_ID_MPEG4,
     .priv_data_size        = sizeof(Mpeg4DecContext),
     .init                  = decode_init,
-    .close                 = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close                 = ff_mpv_decode_close,
     .p.capabilities        = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
                              AV_CODEC_CAP_DELAY | AV_CODEC_CAP_FRAME_THREADS,
     .caps_internal         = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index 71a6c0ad67..c8952f4831 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -173,6 +173,14 @@  do {\
     return 0;
 }
 
+int ff_mpv_decode_close(AVCodecContext *avctx)
+{
+    MpegEncContext *s = avctx->priv_data;
+
+    ff_mpv_common_end(s);
+    return 0;
+}
+
 int ff_mpv_common_frame_size_change(MpegEncContext *s)
 {
     int err = 0;
diff --git a/libavcodec/mpegvideodec.h b/libavcodec/mpegvideodec.h
index 4259d5a02d..35e9081d2c 100644
--- a/libavcodec/mpegvideodec.h
+++ b/libavcodec/mpegvideodec.h
@@ -63,6 +63,7 @@  int ff_mpv_export_qp_table(const MpegEncContext *s, AVFrame *f,
 int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src);
 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h);
 void ff_mpeg_flush(AVCodecContext *avctx);
+int ff_mpv_decode_close(AVCodecContext *avctx);
 
 void ff_print_debug_info(const MpegEncContext *s, const MPVPicture *p, AVFrame *pict);
 
diff --git a/libavcodec/msmpeg4dec.c b/libavcodec/msmpeg4dec.c
index a7b3fc4603..20d735a152 100644
--- a/libavcodec/msmpeg4dec.c
+++ b/libavcodec/msmpeg4dec.c
@@ -28,6 +28,7 @@ 
 #include "codec_internal.h"
 #include "mpegutils.h"
 #include "mpegvideo.h"
+#include "mpegvideodec.h"
 #include "msmpeg4.h"
 #include "msmpeg4dec.h"
 #include "libavutil/imgutils.h"
@@ -848,8 +849,8 @@  const FFCodec ff_msmpeg4v1_decoder = {
     .p.id           = AV_CODEC_ID_MSMPEG4V1,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_msmpeg4_decode_init,
-    .close          = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
     .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .p.max_lowres   = 3,
@@ -862,8 +863,8 @@  const FFCodec ff_msmpeg4v2_decoder = {
     .p.id           = AV_CODEC_ID_MSMPEG4V2,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_msmpeg4_decode_init,
-    .close          = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
     .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .p.max_lowres   = 3,
@@ -876,8 +877,8 @@  const FFCodec ff_msmpeg4v3_decoder = {
     .p.id           = AV_CODEC_ID_MSMPEG4V3,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_msmpeg4_decode_init,
-    .close          = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
     .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .p.max_lowres   = 3,
@@ -890,8 +891,8 @@  const FFCodec ff_wmv1_decoder = {
     .p.id           = AV_CODEC_ID_WMV1,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_msmpeg4_decode_init,
-    .close          = ff_h263_decode_end,
     FF_CODEC_DECODE_CB(ff_h263_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
     .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .p.max_lowres   = 3,
diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c
index 201e7ed6d0..6ece6a5a25 100644
--- a/libavcodec/rv10.c
+++ b/libavcodec/rv10.c
@@ -416,14 +416,6 @@  static av_cold int rv10_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
-static av_cold int rv10_decode_end(AVCodecContext *avctx)
-{
-    MpegEncContext *s = avctx->priv_data;
-
-    ff_mpv_common_end(s);
-    return 0;
-}
-
 static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf,
                               int buf_size, int buf_size2, int whole_size)
 {
@@ -666,8 +658,8 @@  const FFCodec ff_rv10_decoder = {
     .p.id           = AV_CODEC_ID_RV10,
     .priv_data_size = sizeof(RVDecContext),
     .init           = rv10_decode_init,
-    .close          = rv10_decode_end,
     FF_CODEC_DECODE_CB(rv10_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DR1,
     .p.max_lowres   = 3,
 };
@@ -679,8 +671,8 @@  const FFCodec ff_rv20_decoder = {
     .p.id           = AV_CODEC_ID_RV20,
     .priv_data_size = sizeof(RVDecContext),
     .init           = rv10_decode_init,
-    .close          = rv10_decode_end,
     FF_CODEC_DECODE_CB(rv10_decode_frame),
+    .close          = ff_mpv_decode_close,
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
     .flush          = ff_mpeg_flush,
     .p.max_lowres   = 3,
diff --git a/libavcodec/rv30.c b/libavcodec/rv30.c
index 9c8bb966e9..5e1dd01aa1 100644
--- a/libavcodec/rv30.c
+++ b/libavcodec/rv30.c
@@ -302,6 +302,7 @@  const FFCodec ff_rv30_decoder = {
     FF_CODEC_DECODE_CB(ff_rv34_decode_frame),
     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
                              AV_CODEC_CAP_FRAME_THREADS,
+    .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP,
     .flush                 = ff_mpeg_flush,
     UPDATE_THREAD_CONTEXT(ff_rv34_decode_update_thread_context),
 };
diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c
index d935c261b5..90296d7de3 100644
--- a/libavcodec/rv34.c
+++ b/libavcodec/rv34.c
@@ -1520,10 +1520,9 @@  av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
 
     ff_h264_pred_init(&r->h, AV_CODEC_ID_RV40, 8, 1);
 
-    if ((ret = rv34_decoder_alloc(r)) < 0) {
-        ff_mpv_common_end(&r->s);
+    ret = rv34_decoder_alloc(r);
+    if (ret < 0)
         return ret;
-    }
 
     ff_thread_once(&init_static_once, rv34_init_tables);
 
@@ -1821,8 +1820,7 @@  av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
 {
     RV34DecContext *r = avctx->priv_data;
 
-    ff_mpv_common_end(&r->s);
     rv34_decoder_free(r);
 
-    return 0;
+    return ff_mpv_decode_close(avctx);
 }
diff --git a/libavcodec/rv40.c b/libavcodec/rv40.c
index 536bbc9623..0a5136d129 100644
--- a/libavcodec/rv40.c
+++ b/libavcodec/rv40.c
@@ -580,6 +580,7 @@  const FFCodec ff_rv40_decoder = {
     FF_CODEC_DECODE_CB(ff_rv34_decode_frame),
     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
                              AV_CODEC_CAP_FRAME_THREADS,
+    .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP,
     .flush                 = ff_mpeg_flush,
     UPDATE_THREAD_CONTEXT(ff_rv34_decode_update_thread_context),
 };
diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c
index 9bbd5cecf6..ea4f015acd 100644
--- a/libavcodec/vc1dec.c
+++ b/libavcodec/vc1dec.c
@@ -449,6 +449,8 @@  static enum AVPixelFormat vc1_get_format(AVCodecContext *avctx)
     return ff_get_format(avctx, vc1_hwaccel_pixfmt_list_420);
 }
 
+static void vc1_decode_reset(AVCodecContext *avctx);
+
 av_cold int ff_vc1_decode_init(AVCodecContext *avctx)
 {
     VC1Context *const v = avctx->priv_data;
@@ -477,7 +479,7 @@  av_cold int ff_vc1_decode_init(AVCodecContext *avctx)
 
     ret = vc1_decode_init_alloc_tables(v);
     if (ret < 0) {
-        ff_vc1_decode_end(avctx);
+        vc1_decode_reset(avctx);
         return ret;
     }
     return 0;
@@ -774,10 +776,7 @@  static av_cold int vc1_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
-/** Close a VC1/WMV3 decoder
- * @warning Initial try at using MpegEncContext stuff
- */
-av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
+static av_cold void vc1_decode_reset(AVCodecContext *avctx)
 {
     VC1Context *v = avctx->priv_data;
     int i;
@@ -803,9 +802,16 @@  av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
     av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
     av_freep(&v->luma_mv_base);
     ff_intrax8_common_end(&v->x8);
-    return 0;
 }
 
+/**
+ * Close a MSS2/VC1/WMV3 decoder
+ */
+av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
+{
+    vc1_decode_reset(avctx);
+    return ff_mpv_decode_close(avctx);
+}
 
 /** Decode a VC1/WMV3 frame
  * @todo TODO: Handle VC-1 IDUs (Transport level?)
diff --git a/libavcodec/wmv2dec.c b/libavcodec/wmv2dec.c
index bb3829dcd6..5c91006169 100644
--- a/libavcodec/wmv2dec.c
+++ b/libavcodec/wmv2dec.c
@@ -27,6 +27,7 @@ 
 #include "mathops.h"
 #include "mpegutils.h"
 #include "mpegvideo.h"
+#include "mpegvideodec.h"
 #include "msmpeg4.h"
 #include "msmpeg4_vc1_data.h"
 #include "msmpeg4dec.h"
@@ -584,7 +585,7 @@  static av_cold int wmv2_decode_end(AVCodecContext *avctx)
     WMV2DecContext *const w = avctx->priv_data;
 
     ff_intrax8_common_end(&w->x8);
-    return ff_h263_decode_end(avctx);
+    return ff_mpv_decode_close(avctx);
 }
 
 const FFCodec ff_wmv2_decoder = {