From 7763bffda79dc3329bc72f0ded7fa876a2e3d2e5 Mon Sep 17 00:00:00 2001
From: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
Date: Wed, 20 Jul 2022 08:04:02 -0300
Subject: [PATCH] misc: bump ffmpeg to n5.0

Change-Id: Ib9c7799cbfe4db0c429f9f8287122b47dceac296
---
 AudioFilter/CMakeLists.txt             |  2 ++
 AudioFilter/FilterAudioSubscriber.cpp  | 28 ++++++++++++++++++-------
 AudioFilter/ffmpeg/package.json        | 10 ++++-----
 AudioFilter/ffmpeg/rules.mak           |  2 +-
 GreenScreen/CMakeLists.txt             |  4 ++++
 GreenScreen/ffmpeg/package.json        | 10 ++++-----
 GreenScreen/ffmpeg/rules.mak           |  2 +-
 GreenScreen/pluginProcessor.cpp        | 29 +++++++++++++++++++-------
 WaterMark/WatermarkVideoSubscriber.cpp | 27 ++++++++++++++++++------
 WaterMark/ffmpeg/package.json          | 10 ++++-----
 WaterMark/ffmpeg/rules.mak             |  2 +-
 11 files changed, 86 insertions(+), 40 deletions(-)

diff --git a/AudioFilter/CMakeLists.txt b/AudioFilter/CMakeLists.txt
index 344b471..5167413 100644
--- a/AudioFilter/CMakeLists.txt
+++ b/AudioFilter/CMakeLists.txt
@@ -75,6 +75,8 @@ target_link_libraries(${ProjectName} PUBLIC libavfilter libswscale libswresample
 add_custom_command(
     TARGET ${ProjectName}
     PRE_BUILD
+    COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/ffmpeg/ ${CONTRIB_PATH}/src/ffmpeg
+    COMMAND python ${DAEMON}/compat/msvc/winmake.py -fb ffmpeg
     COMMAND python ${PROJECT_SOURCE_DIR}/../SDK/jplManipulation.py --preassemble --plugin=${ProjectName}
     COMMENT "Assembling Plugin files"
 )
diff --git a/AudioFilter/FilterAudioSubscriber.cpp b/AudioFilter/FilterAudioSubscriber.cpp
index db45491..e3d2825 100644
--- a/AudioFilter/FilterAudioSubscriber.cpp
+++ b/AudioFilter/FilterAudioSubscriber.cpp
@@ -116,18 +116,17 @@ FilterAudioSubscriber::setIRAVFrame()
 {
     int ret, got_frame;
     AVCodecContext* pCodecCtx;
-    AVCodec* pCodec;
     AVPacket* packet;
 
     FILE* pFile = fopen(irFile_.c_str(), "rb");
 
-    pCodecCtx = pFormatCtx_->streams[audioStream_]->codec;
-    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
+    const AVCodec* pCodec = avcodec_find_decoder(pFormatCtx_->streams[audioStream_]->codecpar->codec_id);
     if (pCodec == NULL) {
         Plog::log(Plog::LogPriority::INFO, TAG, "Codec not found.");
         return;
     }
 
+    pCodecCtx = avcodec_alloc_context3(pCodec);
     // Open codec
     if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
         Plog::log(Plog::LogPriority::INFO, TAG, "Could not open codec.");
@@ -151,16 +150,29 @@ FilterAudioSubscriber::setIRAVFrame()
     packet->size = static_cast<int>(fread(inbuf, 1, AUDIO_INBUF_SIZE, pFile));
     int idx = 0;
     AVFrame* pFrame = av_frame_alloc();
+
+    if (avcodec_send_packet(pCodecCtx, packet) < 0) {
+        avcodec_close(pCodecCtx);
+        av_frame_unref(pFrame);
+        av_frame_free(&pFrame);
+        av_packet_free(&packet);
+        avformat_close_input(&pFormatCtx_);
+        avformat_free_context(pFormatCtx_);
+        Plog::log(Plog::LogPriority::INFO, TAG, "Error submitting the packet to the decoder");
+        return;
+    }
+
     while (packet->size > 0 && idx < frames) {
         idx++;
         got_frame = 0;
-        int len = avcodec_decode_audio4(pCodecCtx, pFrame, &got_frame, packet);
-        if (len < 0) {
+
+        if (avcodec_receive_frame(pCodecCtx, pFrame) < 0) {
             break;
         }
-        if (got_frame) {
-            reverbFilter_.feedInput(pFrame, "ir0");
-        }
+
+        auto len = av_get_bytes_per_sample(pCodecCtx->sample_fmt);
+
+        reverbFilter_.feedInput(pFrame, "ir0");
         packet->size -= len;
         packet->data += len;
     }
diff --git a/AudioFilter/ffmpeg/package.json b/AudioFilter/ffmpeg/package.json
index 6c67cfe..69c2a52 100644
--- a/AudioFilter/ffmpeg/package.json
+++ b/AudioFilter/ffmpeg/package.json
@@ -1,7 +1,7 @@
 {
     "name": "ffmpeg",
-    "version": "n4.4.2",
-    "url": "https://github.com/FFmpeg/FFmpeg/archive/__VERSION__.tar.gz",
+    "version": "n5.0",
+    "url": "https://git.ffmpeg.org/gitweb/ffmpeg.git/snapshot/__VERSION__.tar.gz",
     "deps": [
         "vpx",
         "x264",
@@ -13,13 +13,13 @@
         "change-RTCP-ratio.patch",
         "rtp_ext_abs_send_time.patch",
         "libopusenc-reload-packet-loss-at-encode.patch",
-        "libopusdec-enable-FEC.patch"
-    ],
-    "win_patches": [
+        "libopusdec-enable-FEC.patch",
         "windows-configure.patch",
         "windows-configure-ffnvcodec.patch",
         "windows-configure-libmfx.patch"
     ],
+    "win_patches": [
+    ],
     "project_paths": [],
     "with_env" : "10.0.16299.0",
     "custom_scripts": {
diff --git a/AudioFilter/ffmpeg/rules.mak b/AudioFilter/ffmpeg/rules.mak
index ec18862..a2bf7d9 100644
--- a/AudioFilter/ffmpeg/rules.mak
+++ b/AudioFilter/ffmpeg/rules.mak
@@ -1,4 +1,4 @@
-FFMPEG_HASH := n4.4.2
+FFMPEG_HASH := n5.0
 FFMPEG_URL := https://git.ffmpeg.org/gitweb/ffmpeg.git/snapshot/$(FFMPEG_HASH).tar.gz
 
 PKGS+=ffmpeg
diff --git a/GreenScreen/CMakeLists.txt b/GreenScreen/CMakeLists.txt
index 2b8af88..15f1802 100644
--- a/GreenScreen/CMakeLists.txt
+++ b/GreenScreen/CMakeLists.txt
@@ -99,6 +99,10 @@ endif()
 add_custom_command(
     TARGET ${ProjectName}
     PRE_BUILD
+    COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/zlib ${CONTRIB_PATH}/src/zlib
+    COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/ffmpeg/ ${CONTRIB_PATH}/src/ffmpeg
+    COMMAND python ${DAEMON}/compat/msvc/winmake.py -fb zlib
+    COMMAND python ${DAEMON}/compat/msvc/winmake.py -fb ffmpeg
     COMMAND python ${PROJECT_SOURCE_DIR}/../SDK/jplManipulation.py --preassemble --plugin=${ProjectName}
     COMMAND ${CMAKE_COMMAND} -E copy ${ONNX_DIR}/onnxruntime.lib ${JPL_DIRECTORY}/lib/${CONTRIB_PLATFORM}
     COMMAND ${CMAKE_COMMAND} -E copy ${ONNX_DIR}/onnxruntime.dll ${JPL_DIRECTORY}/lib/${CONTRIB_PLATFORM}
diff --git a/GreenScreen/ffmpeg/package.json b/GreenScreen/ffmpeg/package.json
index fe7cfe8..5740af3 100644
--- a/GreenScreen/ffmpeg/package.json
+++ b/GreenScreen/ffmpeg/package.json
@@ -1,7 +1,7 @@
 {
     "name": "ffmpeg",
-    "version": "n4.4.2",
-    "url": "https://github.com/FFmpeg/FFmpeg/archive/__VERSION__.tar.gz",
+    "version": "n5.0",
+    "url": "https://git.ffmpeg.org/gitweb/ffmpeg.git/snapshot/__VERSION__.tar.gz",
     "deps": [
         "zlib",
         "vpx",
@@ -14,13 +14,13 @@
         "change-RTCP-ratio.patch",
         "rtp_ext_abs_send_time.patch",
         "libopusenc-reload-packet-loss-at-encode.patch",
-        "libopusdec-enable-FEC.patch"
-    ],
-    "win_patches": [
+        "libopusdec-enable-FEC.patch",
         "windows-configure.patch",
         "windows-configure-ffnvcodec.patch",
         "windows-configure-libmfx.patch"
     ],
+    "win_patches": [
+    ],
     "project_paths": [],
     "with_env" : "10.0.16299.0",
     "custom_scripts": {
diff --git a/GreenScreen/ffmpeg/rules.mak b/GreenScreen/ffmpeg/rules.mak
index 33bdb42..c97236a 100644
--- a/GreenScreen/ffmpeg/rules.mak
+++ b/GreenScreen/ffmpeg/rules.mak
@@ -1,4 +1,4 @@
-FFMPEG_HASH := n4.4.2
+FFMPEG_HASH := n5.0
 FFMPEG_URL := https://git.ffmpeg.org/gitweb/ffmpeg.git/snapshot/$(FFMPEG_HASH).tar.gz
 
 PKGS+=ffmpeg
diff --git a/GreenScreen/pluginProcessor.cpp b/GreenScreen/pluginProcessor.cpp
index 5a10145..737ebfe 100644
--- a/GreenScreen/pluginProcessor.cpp
+++ b/GreenScreen/pluginProcessor.cpp
@@ -340,11 +340,10 @@ PluginProcessor::loadBackground()
 
     int got_frame;
     AVCodecContext* pCodecCtx;
-    AVCodec* pCodec;
     AVPacket* packet;
 
-    pCodecCtx = pFormatCtx_->streams[videoStream_]->codec;
-    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
+    const AVCodec* pCodec = avcodec_find_decoder(
+        pFormatCtx_->streams[videoStream_]->codecpar->codec_id);
     if (pCodec == nullptr) {
         mainFilter_.clean();
         pFormatCtx_.reset();
@@ -352,6 +351,7 @@ PluginProcessor::loadBackground()
         return;
     }
 
+    pCodecCtx = avcodec_alloc_context3(pCodec);
     // Open codec
     if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
         mainFilter_.clean();
@@ -370,13 +370,26 @@ PluginProcessor::loadBackground()
         return;
     }
 
+    if (avcodec_send_packet(pCodecCtx, packet) < 0) {
+        avcodec_close(pCodecCtx);
+        av_packet_free(&packet);
+        pFormatCtx_.reset();
+        Plog::log(Plog::LogPriority::INFO, TAG, "Could not send packet no codec.");
+        return;
+    }
+
     AVFrame* bgImage = av_frame_alloc();
-    avcodec_decode_video2(pCodecCtx, bgImage, &got_frame, packet);
-    if (got_frame) {
-        mainFilter_.feedInput(bgImage, "background");
-        mainFilter_.feedEOF("background");
-    } else
+    if (avcodec_receive_frame(pCodecCtx, bgImage) < 0) {
+        avcodec_close(pCodecCtx);
+        av_packet_free(&packet);
+        pFormatCtx_.reset();
         mainFilter_.clean();
+        Plog::log(Plog::LogPriority::INFO, TAG, "Could not read packet from codec.");
+        return;
+    }
+
+    mainFilter_.feedInput(bgImage, "background");
+    mainFilter_.feedEOF("background");
 
     frameFree(bgImage);
     avcodec_close(pCodecCtx);
diff --git a/WaterMark/WatermarkVideoSubscriber.cpp b/WaterMark/WatermarkVideoSubscriber.cpp
index ace33b9..e32c2e3 100644
--- a/WaterMark/WatermarkVideoSubscriber.cpp
+++ b/WaterMark/WatermarkVideoSubscriber.cpp
@@ -125,11 +125,10 @@ WatermarkVideoSubscriber::loadMarkLogo()
 
     int got_frame;
     AVCodecContext* pCodecCtx;
-    AVCodec* pCodec;
     AVPacket* packet;
 
-    pCodecCtx = pFormatCtx_->streams[videoStream_]->codec;
-    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
+    const AVCodec* pCodec = avcodec_find_decoder(
+        pFormatCtx_->streams[videoStream_]->codecpar->codec_id);
     if (pCodec == nullptr) {
         pFormatCtx_.reset();
         Plog::log(Plog::LogPriority::INFO, TAG, "Codec not found.");
@@ -137,6 +136,7 @@ WatermarkVideoSubscriber::loadMarkLogo()
         return;
     }
 
+    pCodecCtx = avcodec_alloc_context3(pCodec);
     // Open codec
     if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
         pFormatCtx_.reset();
@@ -155,11 +155,26 @@ WatermarkVideoSubscriber::loadMarkLogo()
         return;
     }
 
+    if (avcodec_send_packet(pCodecCtx, packet) < 0) {
+        avcodec_close(pCodecCtx);
+        av_packet_free(&packet);
+        pFormatCtx_.reset();
+        Plog::log(Plog::LogPriority::INFO, TAG, "Could not send packet no codec.");
+        validLogo_ = false;
+        return;
+    }
+
     AVFrame* logoImage = av_frame_alloc();
-    avcodec_decode_video2(pCodecCtx, logoImage, &got_frame, packet);
-    if (got_frame) {
-        logoFilter_.feedInput(logoImage, "logo");
+    if (avcodec_receive_frame(pCodecCtx, logoImage) < 0) {
+        avcodec_close(pCodecCtx);
+        av_packet_free(&packet);
+        pFormatCtx_.reset();
+        Plog::log(Plog::LogPriority::INFO, TAG, "Could not read packet from codec.");
+        validLogo_ = false;
+        return;
     }
+
+    logoFilter_.feedInput(logoImage, "logo");
     logoFilter_.feedEOF("logo");
 
     frameFree(logoImage);
diff --git a/WaterMark/ffmpeg/package.json b/WaterMark/ffmpeg/package.json
index e427103..112a56e 100644
--- a/WaterMark/ffmpeg/package.json
+++ b/WaterMark/ffmpeg/package.json
@@ -1,7 +1,7 @@
 {
     "name": "ffmpeg",
-    "version": "n4.4.2",
-    "url": "https://github.com/FFmpeg/FFmpeg/archive/__VERSION__.tar.gz",
+    "version": "n5.0",
+    "url": "https://git.ffmpeg.org/gitweb/ffmpeg.git/snapshot/__VERSION__.tar.gz",
     "deps": [
         "zlib",
         "freetype",
@@ -15,13 +15,13 @@
         "change-RTCP-ratio.patch",
         "rtp_ext_abs_send_time.patch",
         "libopusenc-reload-packet-loss-at-encode.patch",
-        "libopusdec-enable-FEC.patch"
-    ],
-    "win_patches": [
+        "libopusdec-enable-FEC.patch",
         "windows-configure.patch",
         "windows-configure-ffnvcodec.patch",
         "windows-configure-libmfx.patch"
     ],
+    "win_patches": [
+    ],
     "project_paths": [],
     "with_env" : "10.0.16299.0",
     "custom_scripts": {
diff --git a/WaterMark/ffmpeg/rules.mak b/WaterMark/ffmpeg/rules.mak
index 9c26aa0..c68f2c5 100644
--- a/WaterMark/ffmpeg/rules.mak
+++ b/WaterMark/ffmpeg/rules.mak
@@ -1,4 +1,4 @@
-FFMPEG_HASH := n4.4.2
+FFMPEG_HASH := n5.0
 FFMPEG_URL := https://git.ffmpeg.org/gitweb/ffmpeg.git/snapshot/$(FFMPEG_HASH).tar.gz
 
 PKGS+=ffmpeg
-- 
GitLab