From 82954095da61af4ed6bb5f6613edd3e2aad823d3 Mon Sep 17 00:00:00 2001 From: grill2010 Date: Mon, 6 May 2024 01:00:04 +0200 Subject: [PATCH] +) Enable vaapi hw acceleration for h264 and HEVC +) Add ffmpeg patches for invalid reference frames in vaapi hevc/ h264 --- ffmpeg/cppbuild.sh | 7 ++- ...-vaapi_h264-Fixup-invalid-references.patch | 50 +++++++++++++++++++ ...-vaapi_hevc-Fixup-invalid-references.patch | 48 ++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 ffmpeg/ffmpeg-lavc-vaapi_h264-Fixup-invalid-references.patch create mode 100644 ffmpeg/ffmpeg-lavc-vaapi_hevc-Fixup-invalid-references.patch diff --git a/ffmpeg/cppbuild.sh b/ffmpeg/cppbuild.sh index dbc412e6472..da377f2acaa 100755 --- a/ffmpeg/cppbuild.sh +++ b/ffmpeg/cppbuild.sh @@ -10,6 +10,7 @@ fi DISABLE="--disable-iconv --disable-opencl --disable-sdl2 --disable-bzlib --disable-lzma --disable-linux-perf --disable-xlib" ENABLE="--enable-shared --enable-version3 --enable-runtime-cpudetect --enable-zlib --enable-libmp3lame --enable-libspeex --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-openssl --enable-libopenh264 --enable-libvpx --enable-libfreetype --enable-libopus --enable-libxml2 --enable-libsrt --enable-libwebp --enable-libaom --enable-libsvtav1 --enable-libzimg" ENABLE_VULKAN="--enable-vulkan --enable-hwaccel=h264_vulkan --enable-hwaccel=hevc_vulkan --enable-hwaccel=av1_vulkan" +ENABLE_VAAPI="--enable-hwaccel=h264_vaapi --enable-hwaccel=hevc_vaapi" if [[ "$EXTENSION" == *gpl ]]; then # Enable GPLv3 modules @@ -119,6 +120,8 @@ patch -Np1 -d $LAME < ../../lame.patch patch -Np1 -d $OPENSSL < ../../openssl-android.patch patch -Np1 -d ffmpeg-$FFMPEG_VERSION < ../../ffmpeg.patch patch -Np1 -d ffmpeg-$FFMPEG_VERSION < ../../ffmpeg-vulkan.patch +patch -Np1 -d ffmpeg-$FFMPEG_VERSION < ../../ffmpeg-lavc-vaapi_h264-Fixup-invalid-references.patch +patch -Np1 -d ffmpeg-$FFMPEG_VERSION < ../../ffmpeg-lavc-vaapi_hevc-Fixup-invalid-references.patch # patch -Np1 -d ffmpeg-$FFMPEG_VERSION < ../../ffmpeg-flv-support-hevc-opus.patch sedinplace 's/bool bEnableavx512/bool bEnableavx512 = false/g' x265-*/source/common/param.h sedinplace 's/detect512()/false/g' x265-*/source/common/quant.cpp @@ -886,7 +889,7 @@ EOF make install cd .. cd ../ffmpeg-$FFMPEG_VERSION - LDEXEFLAGS='-Wl,-rpath,\$$ORIGIN/' PKG_CONFIG_PATH=../lib/pkgconfig/ ./configure --prefix=.. $DISABLE $ENABLE $ENABLE_VULKAN --enable-libdrm --enable-cuda --enable-cuvid --enable-nvenc --enable-pthreads --enable-libxcb --enable-libpulse --cc="gcc -m32 -D__ILP32__" --extra-cflags="-I../include/ -I../include/libxml2 -I../include/mfx -I../include/svt-av1" --extra-ldflags="-L../lib/" --extra-libs="-lstdc++ -lpthread -ldl -lz -lm $LIBS" || cat ffbuild/config.log + LDEXEFLAGS='-Wl,-rpath,\$$ORIGIN/' PKG_CONFIG_PATH=../lib/pkgconfig/ ./configure --prefix=.. $DISABLE $ENABLE $ENABLE_VULKAN --enable-libdrm $ENABLE_VAAPI --enable-cuda --enable-cuvid --enable-nvenc --enable-pthreads --enable-libxcb --enable-libpulse --cc="gcc -m32 -D__ILP32__" --extra-cflags="-I../include/ -I../include/libxml2 -I../include/mfx -I../include/svt-av1" --extra-ldflags="-L../lib/" --extra-libs="-lstdc++ -lpthread -ldl -lz -lm $LIBS" || cat ffbuild/config.log make -j $MAKEJ make install ;; @@ -1036,7 +1039,7 @@ EOF make install cd .. cd ../ffmpeg-$FFMPEG_VERSION - LDEXEFLAGS='-Wl,-rpath,\$$ORIGIN/' PKG_CONFIG_PATH=../lib/pkgconfig/ ./configure --prefix=.. $DISABLE $ENABLE $ENABLE_VULKAN --enable-libdrm --enable-cuda --enable-cuvid --enable-nvenc --enable-pthreads --enable-libxcb --enable-libpulse --cc="gcc -m64" --extra-cflags="-I../include/ -I../include/libxml2 -I../include/mfx -I../include/svt-av1" --extra-ldflags="-L../lib/" --extra-libs="-lstdc++ -lpthread -ldl -lz -lm $LIBS" || cat ffbuild/config.log + LDEXEFLAGS='-Wl,-rpath,\$$ORIGIN/' PKG_CONFIG_PATH=../lib/pkgconfig/ ./configure --prefix=.. $DISABLE $ENABLE $ENABLE_VULKAN --enable-libdrm $ENABLE_VAAPI --enable-cuda --enable-cuvid --enable-nvenc --enable-pthreads --enable-libxcb --enable-libpulse --cc="gcc -m64" --extra-cflags="-I../include/ -I../include/libxml2 -I../include/mfx -I../include/svt-av1" --extra-ldflags="-L../lib/" --extra-libs="-lstdc++ -lpthread -ldl -lz -lm $LIBS" || cat ffbuild/config.log make -j $MAKEJ make install ;; diff --git a/ffmpeg/ffmpeg-lavc-vaapi_h264-Fixup-invalid-references.patch b/ffmpeg/ffmpeg-lavc-vaapi_h264-Fixup-invalid-references.patch new file mode 100644 index 00000000000..83acc13c3aa --- /dev/null +++ b/ffmpeg/ffmpeg-lavc-vaapi_h264-Fixup-invalid-references.patch @@ -0,0 +1,50 @@ +From 9d4b9b0b963f5375317636f4f527a203e21e89b9 Mon Sep 17 00:00:00 2001 +From: David Rosca +Date: Mon, 8 Jan 2024 12:44:20 +0100 +Subject: [PATCH] lavc/vaapi_h264: Fixup invalid references + +--- + libavcodec/vaapi_h264.c | 20 +++++++++++++++++++- + 1 file changed, 19 insertions(+), 1 deletion(-) + +diff --git a/libavcodec/vaapi_h264.c b/libavcodec/vaapi_h264.c +index 55cf5a05ee..c935c88bad 100644 +--- a/libavcodec/vaapi_h264.c ++++ b/libavcodec/vaapi_h264.c +@@ -128,6 +128,8 @@ static int fill_vaapi_ReferenceFrames(VAPictureParameterBufferH264 *pic_param, + DPB dpb; + int i; + ++ const H264Picture *pic_good = NULL; ++ + dpb.size = 0; + dpb.max_size = FF_ARRAY_ELEMS(pic_param->ReferenceFrames); + dpb.va_pics = pic_param->ReferenceFrames; +@@ -136,7 +138,23 @@ static int fill_vaapi_ReferenceFrames(VAPictureParameterBufferH264 *pic_param, + + for (i = 0; i < h->short_ref_count; i++) { + const H264Picture *pic = h->short_ref[i]; +- if (pic && pic->reference && dpb_add(&dpb, pic) < 0) ++ if (!pic || !pic->reference) ++ continue; ++ if (pic->recovered) { ++ pic_good = pic; ++ } else { ++ if (pic_good) { ++ pic = pic_good; ++ } else { ++ for (int i = 0; i < h->short_ref_count; i++) { ++ if (h->short_ref[i] && h->short_ref[i]->reference && h->short_ref[i]->recovered) { ++ pic = h->short_ref[i]; ++ break; ++ } ++ } ++ } ++ } ++ if (dpb_add(&dpb, pic) < 0) + return -1; + } + +-- +2.43.0 + diff --git a/ffmpeg/ffmpeg-lavc-vaapi_hevc-Fixup-invalid-references.patch b/ffmpeg/ffmpeg-lavc-vaapi_hevc-Fixup-invalid-references.patch new file mode 100644 index 00000000000..4821f7ac774 --- /dev/null +++ b/ffmpeg/ffmpeg-lavc-vaapi_hevc-Fixup-invalid-references.patch @@ -0,0 +1,48 @@ +From 1b516ca017fa45888d4962ebf97a78987e696209 Mon Sep 17 00:00:00 2001 +From: David Rosca +Date: Sun, 10 Dec 2023 10:00:28 +0100 +Subject: [PATCH] lavc/vaapi_hevc: Fixup invalid references + +--- + libavcodec/vaapi_hevc.c | 18 ++++++++++++++++++ + 1 file changed, 18 insertions(+) + +diff --git a/libavcodec/vaapi_hevc.c b/libavcodec/vaapi_hevc.c +index 3bdd2dd1b8..d08fb50e37 100644 +--- a/libavcodec/vaapi_hevc.c ++++ b/libavcodec/vaapi_hevc.c +@@ -100,6 +100,8 @@ static void fill_vaapi_reference_frames(const HEVCContext *h, VAPictureParameter + const HEVCFrame *current_picture = h->ref; + int i, j, rps_type; + ++ const HEVCFrame *frame_good = NULL; ++ + for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) { + const HEVCFrame *frame = NULL; + +@@ -113,6 +115,22 @@ static void fill_vaapi_reference_frames(const HEVCContext *h, VAPictureParameter + init_vaapi_pic(&pp->ReferenceFrames[i]); + + if (frame) { ++ if (frame->sequence != HEVC_SEQUENCE_COUNTER_INVALID) { ++ frame_good = frame; ++ } else { ++ if (frame_good) { ++ frame = frame_good; ++ } else { ++ for (int k = 0; k < FF_ARRAY_ELEMS(h->DPB); k++) { ++ if ((&h->DPB[k] != current_picture || h->ps.pps->pps_curr_pic_ref_enabled_flag) && ++ (h->DPB[k].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)) && ++ h->DPB[k].sequence != HEVC_SEQUENCE_COUNTER_INVALID) { ++ frame = &h->DPB[k]; ++ break; ++ } ++ } ++ } ++ } + rps_type = find_frame_rps_type(h, frame); + fill_vaapi_pic(&pp->ReferenceFrames[i], frame, rps_type); + } +-- +2.43.0 +