From c3e33d9885a3e4a31f657ef9b1fb52311ac5d921 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Sat, 1 Mar 2025 23:37:50 +0100
Subject: [PATCH 09/77] avcodec/speedhqenc: Make speedhq_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
and avoids a non-static function.

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

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 7eb381af36..1f55512674 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -774,10 +774,6 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
     case AV_CODEC_ID_SPEEDHQ:
         s->out_format = FMT_SPEEDHQ;
         s->intra_only = 1; /* force intra only for SHQ */
-        if (!CONFIG_SPEEDHQ_ENCODER)
-            return AVERROR_ENCODER_NOT_FOUND;
-        if ((ret = ff_speedhq_encode_init(s)) < 0)
-            return ret;
         avctx->delay = 0;
         s->low_delay = 1;
         break;
diff --git a/libavcodec/speedhqenc.c b/libavcodec/speedhqenc.c
index a0baa869cc..de1bc0cfca 100644
--- a/libavcodec/speedhqenc.c
+++ b/libavcodec/speedhqenc.c
@@ -95,26 +95,26 @@ static av_cold void speedhq_init_static_data(void)
                              ff_speedhq_vlc_table, uni_speedhq_ac_vlc_len);
 }
 
-av_cold int ff_speedhq_encode_init(MpegEncContext *s)
+static av_cold int speedhq_encode_init(AVCodecContext *avctx)
 {
     static AVOnce init_static_once = AV_ONCE_INIT;
+    MpegEncContext *const s = avctx->priv_data;
+    int ret;
 
-    if (s->width > 65500 || s->height > 65500) {
-        av_log(s->avctx, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n");
+    if (avctx->width > 65500 || avctx->height > 65500) {
+        av_log(avctx, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n");
         return AVERROR(EINVAL);
     }
 
     // border is not implemented correctly at the moment, see ticket #10078
-    if (s->width % 16) {
-        av_log(s->avctx, AV_LOG_ERROR, "width must be a multiple of 16\n");
+    if (avctx->width % 16) {
+        av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 16\n");
         return AVERROR_PATCHWELCOME;
     }
 
     s->min_qcoeff = -2048;
     s->max_qcoeff = 2047;
 
-    ff_thread_once(&init_static_once, speedhq_init_static_data);
-
     s->intra_ac_vlc_length      =
     s->intra_ac_vlc_last_length =
     s->intra_chroma_ac_vlc_length      =
@@ -123,6 +123,12 @@ av_cold int ff_speedhq_encode_init(MpegEncContext *s)
     s->y_dc_scale_table =
     s->c_dc_scale_table = ff_mpeg12_dc_scale_table[3];
 
+    ret = ff_mpv_encode_init(avctx);
+    if (ret < 0)
+        return ret;
+
+    ff_thread_once(&init_static_once, speedhq_init_static_data);
+
     switch (s->avctx->pix_fmt) {
     case AV_PIX_FMT_YUV420P:
         s->avctx->codec_tag = MKTAG('S','H','Q','0');
@@ -280,7 +286,7 @@ const FFCodec ff_speedhq_encoder = {
     .p.priv_class   = &ff_mpv_enc_class,
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
     .priv_data_size = sizeof(SpeedHQEncContext),
-    .init           = ff_mpv_encode_init,
+    .init           = speedhq_encode_init,
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
diff --git a/libavcodec/speedhqenc.h b/libavcodec/speedhqenc.h
index 15be9764d7..66ef7ee023 100644
--- a/libavcodec/speedhqenc.h
+++ b/libavcodec/speedhqenc.h
@@ -33,7 +33,6 @@
 
 #include "mpegvideo.h"
 
-int  ff_speedhq_encode_init(MpegEncContext *s);
 void ff_speedhq_encode_close(MpegEncContext *s);
 void ff_speedhq_encode_mb(MpegEncContext *s, int16_t block[12][64]);
 
-- 
2.45.2