From c37d3016011a9e2a3814fa8633fcbd316b164667 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Thu, 10 Jul 2025 11:30:17 +0800
Subject: [PATCH] Fix for ffmpeg5

---
 src/modules/FFmpeg/FFDec.cpp         | 5 ++---
 src/modules/FFmpeg/FFDec.hpp         | 6 +++++-
 src/modules/FFmpeg/FormatContext.cpp | 5 +++--
 src/modules/FFmpeg/OggHelper.cpp     | 2 ++
 src/qmplay2/QMPlay2Core.cpp          | 3 ++-
 5 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/modules/FFmpeg/FFDec.cpp b/src/modules/FFmpeg/FFDec.cpp
index d258cf4f..53d22310 100644
--- a/src/modules/FFmpeg/FFDec.cpp
+++ b/src/modules/FFmpeg/FFDec.cpp
@@ -54,7 +54,7 @@ void FFDec::destroyDecoder()
     av_packet_free(&packet);
     if (codecIsOpen)
     {
-        avcodec_close(codec_ctx);
+        avcodec_free_context(&codec_ctx);
         codecIsOpen = false;
     }
     av_freep(&codec_ctx);
@@ -67,10 +67,9 @@ void FFDec::clearFrames()
     m_frames.clear();
 }
 
-
 AVCodec *FFDec::init(StreamInfo &streamInfo)
 {
-    AVCodec *codec = avcodec_find_decoder_by_name(streamInfo.codec_name);
+    auto codec = const_cast< AVCodec *>(avcodec_find_decoder_by_name(streamInfo.codec_name));
     if (codec)
     {
         codec_ctx = avcodec_alloc_context3(codec);
diff --git a/src/modules/FFmpeg/FFDec.hpp b/src/modules/FFmpeg/FFDec.hpp
index 5bde3a24..f60d1a25 100644
--- a/src/modules/FFmpeg/FFDec.hpp
+++ b/src/modules/FFmpeg/FFDec.hpp
@@ -23,7 +23,11 @@
 #include <QString>
 #include <QList>
 
-struct AVCodecContext;
+extern "C"
+{
+    #include <libavcodec/avcodec.h>
+}
+
 struct AVPacket;
 struct AVCodec;
 struct AVFrame;
diff --git a/src/modules/FFmpeg/FormatContext.cpp b/src/modules/FFmpeg/FormatContext.cpp
index 71b2a266..18b87019 100644
--- a/src/modules/FFmpeg/FormatContext.cpp
+++ b/src/modules/FFmpeg/FormatContext.cpp
@@ -29,6 +29,7 @@
 extern "C"
 {
     #include <libavformat/avformat.h>
+    #include <libavcodec/avcodec.h>
     #include <libavutil/replaygain.h>
     #include <libavutil/spherical.h>
     #include <libavutil/pixdesc.h>
@@ -591,7 +592,7 @@ bool FormatContext::read(Packet &encoded, int &idx)
 
     if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && stream->codecpar->format == AV_PIX_FMT_PAL8)
     {
-        int size = 0;
+        size_t size = 0;
         const auto data = av_packet_get_side_data(packet, AV_PKT_DATA_PALETTE, &size);
         if (size > 0 && data)
             encoded.palette = QByteArray((const char *)data, size);
@@ -704,7 +705,7 @@ bool FormatContext::open(const QString &_url, const QString &param)
         isLocal = true;
     else
     {
-        inputFmt = av_find_input_format(scheme);
+        inputFmt = const_cast<AVInputFormat *>(av_find_input_format(scheme));
         if (inputFmt)
             url = _url.right(_url.length() - scheme.length() - 3);
         isLocal = false;
diff --git a/src/modules/FFmpeg/OggHelper.cpp b/src/modules/FFmpeg/OggHelper.cpp
index a85c9294..6d0460d8 100644
--- a/src/modules/FFmpeg/OggHelper.cpp
+++ b/src/modules/FFmpeg/OggHelper.cpp
@@ -21,6 +21,7 @@
 extern "C"
 {
     #include <libavformat/avio.h>
+    #include <libavutil/mem.h>
 }
 
 static int readPacket(void *opaque, uint8_t *buf, int bufSize)
@@ -29,6 +30,7 @@ static int readPacket(void *opaque, uint8_t *buf, int bufSize)
     const int64_t pos = avio_tell(oggHelper->io);
     return avio_read(oggHelper->io, buf, oggHelper->size > 0 ? qMin<int>(bufSize, oggHelper->size - pos) : bufSize);
 }
+
 static int64_t seekPacket(void *opaque, int64_t offset, int whence)
 {
     OggHelper *oggHelper = (OggHelper *)opaque;
diff --git a/src/qmplay2/QMPlay2Core.cpp b/src/qmplay2/QMPlay2Core.cpp
index 56a04b28..cd93266f 100644
--- a/src/qmplay2/QMPlay2Core.cpp
+++ b/src/qmplay2/QMPlay2Core.cpp
@@ -251,7 +251,8 @@ void QMPlay2CoreClass::init(bool loadModules, bool modulesInSubdirs, const QStri
     av_log_set_level(AV_LOG_ERROR);
     av_log_set_callback(avQMPlay2LogHandler);
 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
-    av_register_all();
+// For w/e reason the check does not work correctly.
+//    av_register_all();
 #endif
     avformat_network_init();
 
-- 
2.48.0

