From ee2d713903d78262c0b1c7700318bf0ec5ff1aa7 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Mar 2025 02:13:31 +0100
Subject: [PATCH 11/77] avcodec/mjpegenc: Make mjpeg_encode_init() call
 ff_mpv_encode_init()

Right now, ff_mpv_encode_init() is set as FFCodec.init and
calls ff_speedhq_encode_init(). The opposite is more natural,
avoids a non-static function and allows to reuse e.g.
slice_context_count instead of duplicating the logic from
ff_mpv_common_init().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mjpegenc.c      | 37 ++++++++++++++++++-------------------
 libavcodec/mjpegenc.h      |  1 -
 libavcodec/mpegvideo_enc.c |  2 --
 3 files changed, 18 insertions(+), 22 deletions(-)

diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c
index 0a2d6eebd3..f34e89235d 100644
--- a/libavcodec/mjpegenc.c
+++ b/libavcodec/mjpegenc.c
@@ -283,10 +283,6 @@ static int alloc_huffman(MpegEncContext *s)
     size_t num_mbs, num_blocks, num_codes;
     int blocks_per_mb;
 
-    // We need to init this here as the mjpeg init is called before the common init,
-    s->mb_width  = (s->width  + 15) / 16;
-    s->mb_height = (s->height + 15) / 16;
-
     switch (s->chroma_format) {
     case CHROMA_420: blocks_per_mb =  6; break;
     case CHROMA_422: blocks_per_mb =  8; break;
@@ -305,34 +301,30 @@ static int alloc_huffman(MpegEncContext *s)
     return 0;
 }
 
-av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
+static av_cold int mjpeg_encode_init(AVCodecContext *avctx)
 {
-    MJpegContext *const m = &((MJPEGEncContext*)s)->mjpeg;
-    int ret, use_slices;
+    MJPEGEncContext *const m2 = avctx->priv_data;
+    MJpegContext    *const m  = &m2->mjpeg;
+    MpegEncContext  *const s  = &m2->mpeg.s;
+    int ret;
 
     s->mjpeg_ctx = m;
-    use_slices = s->avctx->slices > 0 ? s->avctx->slices > 1 :
-                 (s->avctx->active_thread_type & FF_THREAD_SLICE) &&
-                 s->avctx->thread_count > 1;
-
-    if (use_slices)
-        m->huffman = HUFFMAN_TABLE_DEFAULT;
 
     if (s->mpv_flags & FF_MPV_FLAG_QP_RD) {
         // Used to produce garbage with MJPEG.
-        av_log(s->avctx, AV_LOG_ERROR,
+        av_log(avctx, AV_LOG_ERROR,
                "QP RD is no longer compatible with MJPEG or AMV\n");
         return AVERROR(EINVAL);
     }
 
     /* The following check is automatically true for AMV,
      * but it doesn't hurt either. */
-    ret = ff_mjpeg_encode_check_pix_fmt(s->avctx);
+    ret = ff_mjpeg_encode_check_pix_fmt(avctx);
     if (ret < 0)
         return ret;
 
-    if (s->width > 65500 || s->height > 65500) {
-        av_log(s->avctx, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
+    if (avctx->width > 65500 || avctx->height > 65500) {
+        av_log(avctx, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
         return AVERROR(EINVAL);
     }
 
@@ -366,9 +358,16 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
     s->intra_chroma_ac_vlc_length      =
     s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
 
+    ret = ff_mpv_encode_init(avctx);
+    if (ret < 0)
+        return ret;
+
     // Buffers start out empty.
     m->huff_ncode = 0;
 
+    if (s->slice_context_count > 1)
+        m->huffman = HUFFMAN_TABLE_DEFAULT;
+
     if (m->huffman == HUFFMAN_TABLE_OPTIMAL)
         return alloc_huffman(s);
 
@@ -681,7 +680,7 @@ const FFCodec ff_mjpeg_encoder = {
     .p.type         = AVMEDIA_TYPE_VIDEO,
     .p.id           = AV_CODEC_ID_MJPEG,
     .priv_data_size = sizeof(MJPEGEncContext),
-    .init           = ff_mpv_encode_init,
+    .init           = mjpeg_encode_init,
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = mjpeg_encode_close,
     .p.capabilities = AV_CODEC_CAP_DR1 |
@@ -711,7 +710,7 @@ const FFCodec ff_amv_encoder = {
     .p.id           = AV_CODEC_ID_AMV,
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
     .priv_data_size = sizeof(MJPEGEncContext),
-    .init           = ff_mpv_encode_init,
+    .init           = mjpeg_encode_init,
     FF_CODEC_ENCODE_CB(amv_encode_picture),
     .close          = mjpeg_encode_close,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
diff --git a/libavcodec/mjpegenc.h b/libavcodec/mjpegenc.h
index 1ac0d6de7e..2c98de057a 100644
--- a/libavcodec/mjpegenc.h
+++ b/libavcodec/mjpegenc.h
@@ -94,7 +94,6 @@ static inline void put_marker(PutBitContext *p, enum JpegMarker code)
 
 typedef struct MpegEncContext MpegEncContext;
 
-int  ff_mjpeg_encode_init(MpegEncContext *s);
 void ff_mjpeg_amv_encode_picture_header(MpegEncContext *s);
 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64]);
 int  ff_mjpeg_encode_stuffing(MpegEncContext *s);
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 1f55512674..d2e3ed1d8c 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -765,8 +765,6 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
     case AV_CODEC_ID_AMV:
         s->out_format = FMT_MJPEG;
         s->intra_only = 1; /* force intra only for jpeg */
-        if ((ret = ff_mjpeg_encode_init(s)) < 0)
-            return ret;
         avctx->delay = 0;
         s->low_delay = 1;
         break;
-- 
2.45.2