From 8eecf1f1d2e3ba2611117891314a3903f92450d0 Mon Sep 17 00:00:00 2001
From: graysky <therealgraysky@protonmail.com>
Date: Sat, 7 May 2022 06:33:07 -0400
Subject: [PATCH] alarm/kodi-rpi to 19.4-2

Incorporate dav1d 1.0.0 fixes for ffmpeg build and use current *FLAGS
---
 ...01-mcpu-cortex-application-to-ffmpeg.patch | 11 ++-
 ...0002-add-dav1d-patch-to-build-system.patch | 24 ++++++
 alarm/kodi-rpi/PKGBUILD                       | 26 ++++---
 .../kodi-rpi/build-fix-for-dav1d-1.0.0.patch  | 78 +++++++++++++++++++
 .../use-mcpu-avoiding-march-and-mtune.patch   |  2 +-
 5 files changed, 125 insertions(+), 16 deletions(-)
 create mode 100644 alarm/kodi-rpi/0002-add-dav1d-patch-to-build-system.patch
 create mode 100644 alarm/kodi-rpi/build-fix-for-dav1d-1.0.0.patch

diff --git a/alarm/kodi-rpi/0001-mcpu-cortex-application-to-ffmpeg.patch b/alarm/kodi-rpi/0001-mcpu-cortex-application-to-ffmpeg.patch
index 22e51cb43..98e8f9df1 100644
--- a/alarm/kodi-rpi/0001-mcpu-cortex-application-to-ffmpeg.patch
+++ b/alarm/kodi-rpi/0001-mcpu-cortex-application-to-ffmpeg.patch
@@ -1,10 +1,9 @@
-From 666fd7f69f684d7dd23b1ee665b0902e29953b76 Mon Sep 17 00:00:00 2001
-From: graysky <graysky@archlinux.us>
-Date: Sat, 12 Dec 2020 09:29:51 -0500
-Subject: [PATCH] mcpu=cortex-xxx application to ffmpeg
+From 11071b94eff58eeaf69f2b81afe317b72cd22b54 Mon Sep 17 00:00:00 2001
+From: graysky <therealgraysky@proton.me>
+Date: Sat, 7 May 2022 06:10:06 -0400
+Subject: [PATCH 1/2] mcpu=cortex-xxx application to ffmpeg
 
 Get build system to patch ffmpeg source removing the -march= flag
-
 ---
  cmake/modules/FindFFMPEG.cmake | 1 +
  1 file changed, 1 insertion(+)
@@ -22,5 +21,5 @@ index 9ce807c..a18b21c 100644
                       )
  
 -- 
-2.29.2
+2.36.0
 
diff --git a/alarm/kodi-rpi/0002-add-dav1d-patch-to-build-system.patch b/alarm/kodi-rpi/0002-add-dav1d-patch-to-build-system.patch
new file mode 100644
index 000000000..3038637ad
--- /dev/null
+++ b/alarm/kodi-rpi/0002-add-dav1d-patch-to-build-system.patch
@@ -0,0 +1,24 @@
+From 9bc4045471dd0f6a23023db6801173b0c624e127 Mon Sep 17 00:00:00 2001
+From: graysky <therealgraysky@proton.me>
+Date: Sat, 7 May 2022 06:11:27 -0400
+Subject: [PATCH 2/2] add dav1d patch to build system
+
+---
+ cmake/modules/FindFFMPEG.cmake | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/cmake/modules/FindFFMPEG.cmake b/cmake/modules/FindFFMPEG.cmake
+index a18b21c..fb7326f 100644
+--- a/cmake/modules/FindFFMPEG.cmake
++++ b/cmake/modules/FindFFMPEG.cmake
+@@ -280,6 +280,7 @@ if(NOT FFMPEG_FOUND)
+                                     <SOURCE_DIR> &&
+                                     patch -p1 < ${CMAKE_SOURCE_DIR}/tools/depends/target/ffmpeg/0001-rpi-Add-hevc-acceleration.patch &&
+                                     patch -p1 < ${CMAKE_SOURCE_DIR}/tools/depends/target/ffmpeg/0002-use-mcpu-avoiding-march-and-mtune.patch &&
++                                    patch -p1 < ${CMAKE_SOURCE_DIR}/tools/depends/target/ffmpeg/0003-add-dav1d-patch-to-build-system.patch &&
+                                     echo "########################################## patched ffmpeg ##############################"
+                      )
+ 
+-- 
+2.36.0
+
diff --git a/alarm/kodi-rpi/PKGBUILD b/alarm/kodi-rpi/PKGBUILD
index 16ebebe09..6778d637c 100644
--- a/alarm/kodi-rpi/PKGBUILD
+++ b/alarm/kodi-rpi/PKGBUILD
@@ -1,4 +1,4 @@
-# Contributor graysky <therealgraysky ATprotonmail DOT com>
+# Contributor graysky <therealgraysky AT proton DOT me>
 # Contributor: BlackIkeEagle < ike DOT devolder AT gmail DOT com>
 # Contributor Kevin Mihelich <kevin@archlinuxarm.org>
 # Contributor BlackIkeEagle < ike DOT devolder AT gmail DOT com >
@@ -13,7 +13,7 @@ pkgname=(
   'kodi-rpi-eventclients' 'kodi-rpi-tools-texturepacker' 'kodi-rpi-dev'
 )
 pkgver=19.4
-pkgrel=1
+pkgrel=2
 _commit=51a36cca9f6dd2bc717e41153fb2d0aa9caf11ca
 arch=('armv7h' 'aarch64')
 url="https://github.com/graysky2/xbmc/tree/gs-gbm_matrix"
@@ -55,8 +55,10 @@ source=(
   "http://mirrors.kodi.tv/build-deps/sources/libudfread-$_libudfread_version.tar.gz"
   "ArchARM-kodi-init-v$_init_version.tar.gz::https://github.com/graysky2/kodi-standalone-service/archive/v$_init_version.tar.gz"
   kodi.config.txt
+  build-fix-for-dav1d-1.0.0.patch
   use-mcpu-avoiding-march-and-mtune.patch
   0001-mcpu-cortex-application-to-ffmpeg.patch
+  0002-add-dav1d-patch-to-build-system.patch
 )
 backup=(boot/kodi.config.txt etc/conf.d/kodi-standalone)
 noextract=(
@@ -84,8 +86,10 @@ sha256sums=('ea57f7d2339bede8086082865afbb47c953f195dbfbaf54c43aeb4b46b8f9f2e'
             '5ad9f95c53e8f29853c1fefa0a20a301be4045e2c7ae49d1164bc74d94155627'
             'b94c70baa45e30346224ceecfab031dd183e09303b1f97d6522f9941da9b0067'
             '23f105f914f3372afa432251b69c7601e50c22d1a88168185105ee859005f5cd'
-            '16d6c79ff3a3d3653f21d77a36326a0335c8cec49a9c06dbc2de0ed143e50a9f'
-            '76eafede11af936bb554932b2be71ce91d4bf1c07107d847500ebbf3ff7d7245')
+            'bcde6299a805430c3ca1f2807101edc7001e77b1934088db9057f74d9a9dd8f6'
+            'c226f082349e6442b8920e1108005792c42b9461f7cec0e1576ae079520eed13'
+            'afe90ded82e70af8e5680347a72edcbdc1dcae498083a90cc453dce00b665fce'
+            '7f6e8e5c2084ab88d10ab2c55982212a8c6cbcf4101079be343763127f129a22')
 
 prepare() {
   [[ -d kodi-build ]] && rm -rf kodi-build
@@ -93,12 +97,16 @@ prepare() {
 
   cd "xbmc-$_commit"
 
-  # put patch in source tree so kodi build system can pick it up
+  # put patches in source tree so kodi build system can pick it up
   cp ../use-mcpu-avoiding-march-and-mtune.patch \
     tools/depends/target/ffmpeg/0002-use-mcpu-avoiding-march-and-mtune.patch
 
-  # patch kodi build system to apply the patch we just copied over
-  patch -Np1 -i ../0001-mcpu-cortex-application-to-ffmpeg.patch
+  cp ../build-fix-for-dav1d-1.0.0.patch \
+    tools/depends/target/ffmpeg/0003-add-dav1d-patch-to-build-system.patch
+
+  # patch kodi build system to apply the patches we just copied over
+  patch -p1 -i ../0001-mcpu-cortex-application-to-ffmpeg.patch
+  patch -p1 -i ../0002-add-dav1d-patch-to-build-system.patch
 }
 
 build() {
@@ -108,8 +116,8 @@ build() {
   # uses here so unset them and redefine below
   unset CFLAGS CXXFLAGS
 
-  CFLAGS="-O2 -pipe -fno-plt"
-  CXXFLAGS="${CFLAGS}"
+  CFLAGS="-O2 -pipe -fstack-protector-strong -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection"
+  CXXFLAGS="${CFLAGS} -Wp,-D_GLIBCXX_ASSERTIONS"
 
   if [[ $CARCH = "armv7h" ]]; then
     # we use -mcpu=cortex-a53 rather than cortex-a72 to maximize RPi 3B and RPi 4B/400 compatibility
diff --git a/alarm/kodi-rpi/build-fix-for-dav1d-1.0.0.patch b/alarm/kodi-rpi/build-fix-for-dav1d-1.0.0.patch
new file mode 100644
index 000000000..b3addd576
--- /dev/null
+++ b/alarm/kodi-rpi/build-fix-for-dav1d-1.0.0.patch
@@ -0,0 +1,78 @@
+From 2546e1ed27f92a840a2cf319e3c1833799974cf1 Mon Sep 17 00:00:00 2001
+From: BlackEagle <ike.devolder@gmail.com>
+Date: Fri, 29 Apr 2022 14:33:12 +0200
+Subject: [PATCH] add build fix for dav1d 1.0.0
+
+Taken from https://github.com/FFmpeg/FFmpeg/commit/e204846ec16c1ab34c7f3a681734cf5190433018
+
+add FF_DAV1D_VERSION_AT_LEAST
+
+Extracted from https://github.com/FFmpeg/FFmpeg/commit/7ee17ec7e46afef0e0af20af196292ec75f50b62
+
+Signed-off-by: BlackEagle <ike.devolder@gmail.com>
+---
+ libavcodec/libdav1d.c | 24 ++++++++++++++++++++++--
+ 1 file changed, 22 insertions(+), 2 deletions(-)
+
+diff --git a/libavcodec/libdav1d.c b/libavcodec/libdav1d.c
+index bbb3ec1e6c..08b4af8ac8 100644
+--- a/libavcodec/libdav1d.c
++++ b/libavcodec/libdav1d.c
+@@ -30,6 +30,9 @@
+ #include "decode.h"
+ #include "internal.h"
+ 
++#define FF_DAV1D_VERSION_AT_LEAST(x,y) \
++    (DAV1D_API_VERSION_MAJOR > (x) || DAV1D_API_VERSION_MAJOR == (x) && DAV1D_API_VERSION_MINOR >= (y))
++
+ typedef struct Libdav1dContext {
+     AVClass *class;
+     Dav1dContext *c;
+@@ -140,6 +143,15 @@ static av_cold int libdav1d_init(AVCodecContext *c)
+     if (dav1d->operating_point >= 0)
+         s.operating_point = dav1d->operating_point;
+ 
++#if FF_DAV1D_VERSION_AT_LEAST(6,0)
++    if (dav1d->frame_threads || dav1d->tile_threads)
++        s.n_threads = FFMAX(dav1d->frame_threads, dav1d->tile_threads);
++    else
++        s.n_threads = FFMIN(threads, DAV1D_MAX_THREADS);
++    s.max_frame_delay = (c->flags & AV_CODEC_FLAG_LOW_DELAY) ? 1 : s.n_threads;
++    av_log(c, AV_LOG_DEBUG, "Using %d threads, %d max_frame_delay\n",
++           s.n_threads, s.max_frame_delay);
++#else
+     s.n_tile_threads = dav1d->tile_threads
+                      ? dav1d->tile_threads
+                      : FFMIN(floor(sqrt(threads)), DAV1D_MAX_TILE_THREADS);
+@@ -148,6 +160,7 @@ static av_cold int libdav1d_init(AVCodecContext *c)
+                       : FFMIN(ceil(threads / s.n_tile_threads), DAV1D_MAX_FRAME_THREADS);
+     av_log(c, AV_LOG_DEBUG, "Using %d frame threads, %d tile threads\n",
+            s.n_frame_threads, s.n_tile_threads);
++#endif
+ 
+     res = dav1d_open(&dav1d->c, &s);
+     if (res < 0)
+@@ -384,11 +397,18 @@ static av_cold int libdav1d_close(AVCodecContext *c)
+     return 0;
+ }
+ 
++#ifndef DAV1D_MAX_FRAME_THREADS
++#define DAV1D_MAX_FRAME_THREADS DAV1D_MAX_THREADS
++#endif
++#ifndef DAV1D_MAX_TILE_THREADS
++#define DAV1D_MAX_TILE_THREADS DAV1D_MAX_THREADS
++#endif
++
+ #define OFFSET(x) offsetof(Libdav1dContext, x)
+ #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+ static const AVOption libdav1d_options[] = {
+-    { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_TILE_THREADS, VD },
+-    { "framethreads", "Frame threads", OFFSET(frame_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_FRAME_THREADS, VD },
++    { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_TILE_THREADS, VD | AV_OPT_FLAG_DEPRECATED },
++    { "framethreads", "Frame threads", OFFSET(frame_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_FRAME_THREADS, VD | AV_OPT_FLAG_DEPRECATED },
+     { "filmgrain", "Apply Film Grain", OFFSET(apply_grain), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
+     { "oppoint",  "Select an operating point of the scalable bitstream", OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 31, VD },
+     { "alllayers", "Output all spatial layers", OFFSET(all_layers), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
+-- 
+2.36.0
+
diff --git a/alarm/kodi-rpi/use-mcpu-avoiding-march-and-mtune.patch b/alarm/kodi-rpi/use-mcpu-avoiding-march-and-mtune.patch
index 2783b426b..961e5cdab 100644
--- a/alarm/kodi-rpi/use-mcpu-avoiding-march-and-mtune.patch
+++ b/alarm/kodi-rpi/use-mcpu-avoiding-march-and-mtune.patch
@@ -1,5 +1,5 @@
 From 2e2417894ac5d35bc3d93b86c18f4df299f4d933 Mon Sep 17 00:00:00 2001
-From: graysky <graysky@archlinux.us>
+From: graysky <therealgraysky@proton.me>
 Date: Sat, 12 Dec 2020 09:10:28 -0500
 Subject: [PATCH] use -mcpu= avoiding -march and -mtune for armv7h