Skip to content

Commit

Permalink
+) Enable vaapi hw acceleration for h264 and HEVC
Browse files Browse the repository at this point in the history
+) Add ffmpeg patches for invalid reference frames in vaapi hevc/ h264
  • Loading branch information
grill2010 committed May 5, 2024
1 parent 34289d4 commit 8295409
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ffmpeg/cppbuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
;;
Expand Down Expand Up @@ -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
;;
Expand Down
50 changes: 50 additions & 0 deletions ffmpeg/ffmpeg-lavc-vaapi_h264-Fixup-invalid-references.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
From 9d4b9b0b963f5375317636f4f527a203e21e89b9 Mon Sep 17 00:00:00 2001
From: David Rosca <nowrep@gmail.com>
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

48 changes: 48 additions & 0 deletions ffmpeg/ffmpeg-lavc-vaapi_hevc-Fixup-invalid-references.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
From 1b516ca017fa45888d4962ebf97a78987e696209 Mon Sep 17 00:00:00 2001
From: David Rosca <nowrep@gmail.com>
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

0 comments on commit 8295409

Please sign in to comment.