diff mbox series

[FFmpeg-devel,5/8] avcodec/mpeg12dec: Don't initialize IDCT more than once

Message ID AS8P250MB0744540C8B4E6A4FBF32E0CB8FC5A@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit bc7de8b63c424c79dda67f2abb2425133bf218e4
Headers show
Series [FFmpeg-devel,1/4] avcodec/mpegvideo_dec: Check for existence of planes before accesses | 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. 2, 2023, 10:51 a.m. UTC
Before 998c9f15d1ca8c7489775ebcca51623b915988f1, the IDCTDSPContext
has only been initialized in ff_mpv_common_init() which is deferred
until immediately before decoding a picture; to nevertheless parse
the quant matrices in sequence headers or quant matrix extensions,
a dummy (identity) permutation has been stored in the codec's init
function; after ff_mpv_common_init() which could change the permutation
the matrices were repermutated.

Yet since said commit, the IDCTDSPContext is initialized during init
and does not change afterwards (unless the user forces different CPU
flags), so there is no need to reinitialize it; the repermutation code
can be removed as well.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpeg12dec.c | 26 --------------------------
 1 file changed, 26 deletions(-)

Comments

Andreas Rheinhardt Oct. 3, 2023, 10:14 p.m. UTC | #1
Andreas Rheinhardt:
> Before 998c9f15d1ca8c7489775ebcca51623b915988f1, the IDCTDSPContext
> has only been initialized in ff_mpv_common_init() which is deferred
> until immediately before decoding a picture; to nevertheless parse
> the quant matrices in sequence headers or quant matrix extensions,
> a dummy (identity) permutation has been stored in the codec's init
> function; after ff_mpv_common_init() which could change the permutation
> the matrices were repermutated.
> 
> Yet since said commit, the IDCTDSPContext is initialized during init
> and does not change afterwards (unless the user forces different CPU
> flags), so there is no need to reinitialize it; the repermutation code
> can be removed as well.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavcodec/mpeg12dec.c | 26 --------------------------
>  1 file changed, 26 deletions(-)
> 
> diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
> index 92ef6944fa..d2dbcd5b61 100644
> --- a/libavcodec/mpeg12dec.c
> +++ b/libavcodec/mpeg12dec.c
> @@ -1057,8 +1057,6 @@ static av_cold int mpeg_decode_init(AVCodecContext *avctx)
>          avctx->coded_width = avctx->coded_height = 0; // do not trust dimensions from input
>      ff_mpv_decode_init(s2, avctx);
>  
> -    /* we need some permutation to store matrices,
> -     * until the decoder sets the real permutation. */
>      ff_mpv_idct_init(s2);
>      ff_mpeg12_init_vlcs();
>  
> @@ -1093,18 +1091,6 @@ static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
>  }
>  #endif
>  
> -static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
> -                                 const uint8_t *new_perm)
> -{
> -    uint16_t temp_matrix[64];
> -    int i;
> -
> -    memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
> -
> -    for (i = 0; i < 64; i++)
> -        matrix[new_perm[i]] = temp_matrix[old_perm[i]];
> -}
> -
>  static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
>  #if CONFIG_MPEG1_NVDEC_HWACCEL
>      AV_PIX_FMT_CUDA,
> @@ -1177,7 +1163,6 @@ static int mpeg_decode_postinit(AVCodecContext *avctx)
>  {
>      Mpeg1Context *s1  = avctx->priv_data;
>      MpegEncContext *s = &s1->mpeg_enc_ctx;
> -    uint8_t old_permutation[64];
>      int ret;
>  
>      if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
> @@ -1297,19 +1282,9 @@ FF_ENABLE_DEPRECATION_WARNINGS
>  
>          avctx->pix_fmt = mpeg_get_pixelformat(avctx);
>  
> -        /* Quantization matrices may need reordering
> -         * if DCT permutation is changed. */
> -        memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
> -
> -        ff_mpv_idct_init(s);
>          if ((ret = ff_mpv_common_init(s)) < 0)
>              return ret;
>  
> -        quant_matrix_rebuild(s->intra_matrix,        old_permutation, s->idsp.idct_permutation);
> -        quant_matrix_rebuild(s->inter_matrix,        old_permutation, s->idsp.idct_permutation);
> -        quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
> -        quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
> -
>          s1->mpeg_enc_ctx_allocated = 1;
>      }
>      return 0;
> @@ -2169,7 +2144,6 @@ static int vcr2_init_sequence(AVCodecContext *avctx)
>  
>      avctx->pix_fmt = mpeg_get_pixelformat(avctx);
>  
> -    ff_mpv_idct_init(s);
>      if ((ret = ff_mpv_common_init(s)) < 0)
>          return ret;
>      s1->mpeg_enc_ctx_allocated = 1;

Will apply patches 5-8 tomorrow unless there are objections.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 92ef6944fa..d2dbcd5b61 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -1057,8 +1057,6 @@  static av_cold int mpeg_decode_init(AVCodecContext *avctx)
         avctx->coded_width = avctx->coded_height = 0; // do not trust dimensions from input
     ff_mpv_decode_init(s2, avctx);
 
-    /* we need some permutation to store matrices,
-     * until the decoder sets the real permutation. */
     ff_mpv_idct_init(s2);
     ff_mpeg12_init_vlcs();
 
@@ -1093,18 +1091,6 @@  static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
 }
 #endif
 
-static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
-                                 const uint8_t *new_perm)
-{
-    uint16_t temp_matrix[64];
-    int i;
-
-    memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
-
-    for (i = 0; i < 64; i++)
-        matrix[new_perm[i]] = temp_matrix[old_perm[i]];
-}
-
 static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
 #if CONFIG_MPEG1_NVDEC_HWACCEL
     AV_PIX_FMT_CUDA,
@@ -1177,7 +1163,6 @@  static int mpeg_decode_postinit(AVCodecContext *avctx)
 {
     Mpeg1Context *s1  = avctx->priv_data;
     MpegEncContext *s = &s1->mpeg_enc_ctx;
-    uint8_t old_permutation[64];
     int ret;
 
     if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
@@ -1297,19 +1282,9 @@  FF_ENABLE_DEPRECATION_WARNINGS
 
         avctx->pix_fmt = mpeg_get_pixelformat(avctx);
 
-        /* Quantization matrices may need reordering
-         * if DCT permutation is changed. */
-        memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
-
-        ff_mpv_idct_init(s);
         if ((ret = ff_mpv_common_init(s)) < 0)
             return ret;
 
-        quant_matrix_rebuild(s->intra_matrix,        old_permutation, s->idsp.idct_permutation);
-        quant_matrix_rebuild(s->inter_matrix,        old_permutation, s->idsp.idct_permutation);
-        quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
-        quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
-
         s1->mpeg_enc_ctx_allocated = 1;
     }
     return 0;
@@ -2169,7 +2144,6 @@  static int vcr2_init_sequence(AVCodecContext *avctx)
 
     avctx->pix_fmt = mpeg_get_pixelformat(avctx);
 
-    ff_mpv_idct_init(s);
     if ((ret = ff_mpv_common_init(s)) < 0)
         return ret;
     s1->mpeg_enc_ctx_allocated = 1;