Closed untergeek closed 8 months ago
This will be a bit long, but hopefully helpful…
Thanks for the report! First, other than on macOS, hardware acceleration should be considered a best-effort/barely-supported feature. I need to get a lot more field testing in before I’m comfortable saying “everyone should use this by default”. Given the significant disparities in user hardware…I hope you can appreciate the task at hand.
Which leads to QSV. I chose to add support for QSV because it has a wide footprint across the CPU/GPU landscape and would provide the biggest “user bang” for the effort.
Which leads to FFmpeg. HBUP includes a bundled version of FFmpeg, as do most of the Homebridge solutions out there - ffmpeg-for-homebridge. That’s what provides the foundation here. We’ve been experimenting with adding QSV support there and are continuing to run into some challenges and gaps along the way. While the version of FFmpeg that comes bundled with HBUP provides some of the hooks for QSV, it doesn’t currently provide all of them.
I’m working through that.
In the meantime…if you’d like to get HBUP working with QSV hardware acceleration, you’ll need to compile/acquire your own version of FFmpeg that includes support for fdk_aac and QSV (either MFX or VPL support). Once you do, you can tell HBUP about the path to your version of FFmpeg and it will use that instead of what’s bundled (in the HBUP webUI: settings -> additional settings -> video processor).
Hope that helps…I know it’s an imperfect solution, but that’s the best I can do at the moment here.
Thanks for the prompt response. I get how hard it is to guarantee feature continuity with a statically compiled binary.
I will see about compiling my own FFmpeg and report the results back here. Thanks for the suggestion!
Super confused here:
Compiled ffmpeg and test shows which hardware acceleration methods are enabled:
❯ /usr/local/ffmpeg/bin/ffmpeg -hwaccels 2>/dev/null
Hardware acceleration methods:
vaapi
qsv
drm
opencl
❯ /usr/local/ffmpeg/bin/ffmpeg -encoders 2>/dev/null | grep fdk
A....D libfdk_aac Fraunhofer FDK AAC (codec aac)
❯ /usr/local/ffmpeg/bin/ffmpeg -encoders 2>/dev/null | grep vaapi
V....D av1_vaapi AV1 (VAAPI) (codec av1)
V....D h264_vaapi H.264/AVC (VAAPI) (codec h264)
V....D hevc_vaapi H.265/HEVC (VAAPI) (codec hevc)
V....D mjpeg_vaapi MJPEG (VAAPI) (codec mjpeg)
V....D mpeg2_vaapi MPEG-2 (VAAPI) (codec mpeg2video)
V....D vp8_vaapi VP8 (VAAPI) (codec vp8)
V....D vp9_vaapi VP9 (VAAPI) (codec vp9)
And sure enough, this test also shows it should work:
/usr/local/ffmpeg/bin/ffmpeg -vaapi_device /dev/dri/renderD128
ffmpeg version N-112534-ge5f774268a Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 11 (Ubuntu 11.4.0-1ubuntu1~22.04)
configuration: --prefix=/usr/local/ffmpeg --extra-cflags=-I/opt/intel/mediasdk/include --extra-ldflags=-L/opt/intel/mediasdk/lib --extra-ldflags=-L/opt/intel/mediasdk/plugins --enable-libmfx --enable-vaapi --enable-opencl --disable-debug --enable-libvorbis --enable-libvpx --enable-libdrm --enable-gpl --cpu=native --enable-libfdk-aac --enable-libx264 --enable-libx265 --extra-libs=-lpthread --enable-nonfree
libavutil 58. 28.100 / 58. 28.100
libavcodec 60. 30.102 / 60. 30.102
libavformat 60. 15.101 / 60. 15.101
libavdevice 60. 2.101 / 60. 2.101
libavfilter 9. 11.100 / 9. 11.100
libswscale 7. 4.100 / 7. 4.100
libswresample 4. 11.100 / 4. 11.100
libpostproc 57. 2.100 / 57. 2.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
And when I link to this compiled version of ffmpeg, it actually now says that not only are vaapi
and qsv
not working, but drm
and opencl
as well (drm
and opencl
are not mentioned in the bundled version, so may not have been compiled in):
[10/28/2023, 10:01:23 AM] [UniFi Protect] Using FFmpeg version: N-112534-ge5f774268a.
[10/28/2023, 10:01:23 AM] [UniFi Protect] Hardware-accelerated decoding and encoding using vaapi will be unavailable: unable to successfully validate capabilities.
[10/28/2023, 10:01:23 AM] [UniFi Protect] Hardware-accelerated decoding and encoding using qsv will be unavailable: unable to successfully validate capabilities.
[10/28/2023, 10:01:23 AM] [UniFi Protect] Hardware-accelerated decoding and encoding using drm will be unavailable: unable to successfully validate capabilities.
[10/28/2023, 10:01:23 AM] [UniFi Protect] Hardware-accelerated decoding and encoding using opencl will be unavailable: unable to successfully validate capabilities.
I've tried manually specifying h264_vaapi
as the codec and this is what came back in debug logging:
[10/28/2023, 10:13:37 AM] [UniFi Protect] UDM-Pro [UDM-PRO] Front Door [G4 Doorbell]: FFmpeg process ended unexpectedly with an exit code of 8.
[10/28/2023, 10:13:37 AM] [UniFi Protect] UDM-Pro [UDM-PRO] Front Door [G4 Doorbell]: FFmpeg (N-112534-ge5f774268a) command that errored out was: /usr/local/ffmpeg/bin/ffmpeg -hide_banner -nostats -fflags +discardcorrupt -probesize 16384 -max_delay 500000 -r 30 -rtsp_transport tcp -i rtsps://REDACTED:7441/SP7i0WBMe2Px1IDk?enableSrtp -map 0:v:0 -c:v h264_vaapi -preset veryfast -profile:v main -level:v 4.0 -noautoscale -bf 0 -filter:v format=yuvj420p, scale=-2:min(ih\,480) -g:v 60 -bufsize 328k -maxrate 228k -crf 20 -payload_type 99 -ssrc 13805113 -f rtp -srtp_out_suite AES_CM_128_HMAC_SHA1_80 -srtp_out_params Rcg40hIRzsWOsYZsi/nVPM0AP/GG0amyM1EuTlcw srtp://REDACTED:52842?rtcpport=52842&pkt_size=564 -map 0:a:0? -acodec libfdk_aac -afterburner 1 -eld_sbr 1 -eld_v2 1 -profile:a 38 -flags +global_header -f null -ar 24k -b:a 24k -bufsize 48k -ac 1 -af afftdn=nt=w:om=o:tn=1:tr=1:nr=90, highpass=f=200, lowpass=f=1000 -payload_type 110 -ssrc 6357058 -f rtp -srtp_out_suite AES_CM_128_HMAC_SHA1_80 -srtp_out_params ELhI8UI1LFIVB5GRZpeCKes2cHr8Ef9k8vkEfGr/ srtp://REDACTED:58719?rtcpport=58719&pkt_size=188 -loglevel level+verbose
I don't need hw acceleration to work, so I will happily go back to using the bundled ffmpeg and let it work. I figured this was worth reporting here though for future reference.
I tried using the h264_qsv
codec at the command-line:
❯ /usr/local/ffmpeg/bin/ffmpeg -vaapi_device /dev/dri/renderD128 -i /plex/tmp/SOURCE.mp4 -vcodec h264_qsv buh.mp4
ffmpeg version N-112534-ge5f774268a Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 11 (Ubuntu 11.4.0-1ubuntu1~22.04)
configuration: --prefix=/usr/local/ffmpeg --extra-cflags=-I/opt/intel/mediasdk/include --extra-ldflags=-L/opt/intel/mediasdk/lib --extra-ldflags=-L/opt/intel/mediasdk/plugins --enable-libmfx --enable-vaapi --enable-opencl --disable-debug --enable-libvorbis --enable-libvpx --enable-libdrm --enable-gpl --cpu=native --enable-libfdk-aac --enable-libx264 --enable-libx265 --extra-libs=-lpthread --enable-nonfree
libavutil 58. 28.100 / 58. 28.100
libavcodec 60. 30.102 / 60. 30.102
libavformat 60. 15.101 / 60. 15.101
libavdevice 60. 2.101 / 60. 2.101
libavfilter 9. 11.100 / 9. 11.100
libswscale 7. 4.100 / 7. 4.100
libswresample 4. 11.100 / 4. 11.100
libpostproc 57. 2.100 / 57. 2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/plex/tmp/SOURCE.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isom
creation_time : 2023-02-20T05:12:11.000000Z
Duration: 01:36:58.98, start: 0.000000, bitrate: 5745 kb/s
Stream #0:0[0x1](eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 126 kb/s (default)
Metadata:
creation_time : 2023-02-20T05:12:13.000000Z
vendor_id : [0][0][0][0]
Stream #0:1[0x2](eng): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 384 kb/s
Metadata:
creation_time : 2023-02-20T05:14:55.000000Z
vendor_id : [0][0][0][0]
Side data:
audio service type: main
Stream #0:2[0x3](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1912x1076 [SAR 1:1 DAR 478:269], 5231 kb/s, 23.98 fps, 23.98 tbr, 24k tbn (default)
Metadata:
creation_time : 2023-02-20T05:16:01.000000Z
vendor_id : [0][0][0][0]
encoder : H.264
Stream #0:3[0x4](eng): Subtitle: eia_608 (c608 / 0x38303663), 0 kb/s
Metadata:
creation_time : 2023-02-20T05:17:21.000000Z
File 'buh.mp4' already exists. Overwrite? [y/N] y
Stream mapping:
Stream #0:2 -> #0:0 (h264 (native) -> h264 (h264_qsv))
Stream #0:0 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, mp4, to 'buh.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isom
encoder : Lavf60.15.101
Stream #0:0(eng): Video: h264 (avc1 / 0x31637661), nv12(tv, bt709, progressive), 1912x1076 [SAR 1:1 DAR 478:269], q=2-31, 1000 kb/s, 23.98 fps, 24k tbn (default)
Metadata:
creation_time : 2023-02-20T05:16:01.000000Z
vendor_id : [0][0][0][0]
encoder : Lavc60.30.102 h264_qsv
Side data:
cpb: bitrate max/min/avg: 0/0/1000000 buffer size: 0 vbv_delay: N/A
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
creation_time : 2023-02-20T05:12:13.000000Z
vendor_id : [0][0][0][0]
encoder : Lavc60.30.102 aac
[out#0/mp4 @ 0x5592b37b22c0] video:38843kB audio:5251kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.489895%
frame= 8051 fps=247 q=35.0 Lsize= 44310kB time=00:05:37.23 bitrate=1076.4kbits/s dup=1 drop=0 speed=10.3x
[aac @ 0x5592b36201c0] Qavg: 2202.490
Exiting normally, received signal 2.
❯ file buh.mp4
buh.mp4: ISO Media, MP4 Base Media v1 [ISO 14496-12:2003]
And for giggles, I tried the same with the bundled ffmpeg. It errored out:
[h264_qsv @ 0x7f0a0870d400] Error creating a MFX session: -9.
[vost#0:0/h264_qsv @ 0x7f0a08816040] Error initializing output stream: Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
[aac @ 0x7f0a07b8b040] Qavg: 65536.000
[aac @ 0x7f0a07b8b040] 2 frames left in the queue on closing
Conversion failed!
So, I'm back to using bundled. Hope these reports are helpful. If you want me to try settings or edits to files with what seems like a QSV functional version of ffmpeg, I'm game. Just let me know.
Super helpful. Can you try the same test command line without specifying the -vaapi_device
parameter using your own FFmpeg (/usr/local/bin
in your case)?
Yeah. I should have removed that. It doesn't change the output, though:
/usr/local/ffmpeg/bin/ffmpeg -i /plex/tmp/SOURCE.mp4 -vcodec h264_qsv buh.mp4
ffmpeg version N-112534-ge5f774268a Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 11 (Ubuntu 11.4.0-1ubuntu1~22.04)
configuration: --prefix=/usr/local/ffmpeg --extra-cflags=-I/opt/intel/mediasdk/include --extra-ldflags=-L/opt/intel/mediasdk/lib --extra-ldflags=-L/opt/intel/mediasdk/plugins --enable-libmfx --enable-vaapi --enable-opencl --disable-debug --enable-libvorbis --enable-libvpx --enable-libdrm --enable-gpl --cpu=native --enable-libfdk-aac --enable-libx264 --enable-libx265 --extra-libs=-lpthread --enable-nonfree
libavutil 58. 28.100 / 58. 28.100
libavcodec 60. 30.102 / 60. 30.102
libavformat 60. 15.101 / 60. 15.101
libavdevice 60. 2.101 / 60. 2.101
libavfilter 9. 11.100 / 9. 11.100
libswscale 7. 4.100 / 7. 4.100
libswresample 4. 11.100 / 4. 11.100
libpostproc 57. 2.100 / 57. 2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/plex/tmp/SOURCE.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isom
creation_time : 2023-02-20T05:12:11.000000Z
Duration: 01:36:58.98, start: 0.000000, bitrate: 5745 kb/s
Stream #0:0[0x1](eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 126 kb/s (default)
Metadata:
creation_time : 2023-02-20T05:12:13.000000Z
vendor_id : [0][0][0][0]
Stream #0:1[0x2](eng): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 384 kb/s
Metadata:
creation_time : 2023-02-20T05:14:55.000000Z
vendor_id : [0][0][0][0]
Side data:
audio service type: main
Stream #0:2[0x3](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1912x1076 [SAR 1:1 DAR 478:269], 5231 kb/s, 23.98 fps, 23.98 tbr, 24k tbn (default)
Metadata:
creation_time : 2023-02-20T05:16:01.000000Z
vendor_id : [0][0][0][0]
encoder : H.264
Stream #0:3[0x4](eng): Subtitle: eia_608 (c608 / 0x38303663), 0 kb/s
Metadata:
creation_time : 2023-02-20T05:17:21.000000Z
Stream mapping:
Stream #0:2 -> #0:0 (h264 (native) -> h264 (h264_qsv))
Stream #0:0 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, mp4, to 'buh.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isom
encoder : Lavf60.15.101
Stream #0:0(eng): Video: h264 (avc1 / 0x31637661), nv12(tv, bt709, progressive), 1912x1076 [SAR 1:1 DAR 478:269], q=2-31, 1000 kb/s, 23.98 fps, 24k tbn (default)
Metadata:
creation_time : 2023-02-20T05:16:01.000000Z
vendor_id : [0][0][0][0]
encoder : Lavc60.30.102 h264_qsv
Side data:
cpb: bitrate max/min/avg: 0/0/1000000 buffer size: 0 vbv_delay: N/A
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
creation_time : 2023-02-20T05:12:13.000000Z
vendor_id : [0][0][0][0]
encoder : Lavc60.30.102 aac
frame= 3464 fps=256 q=45.0 size= 18688kB time=00:02:26.15 bitrate=1047.5kbits/s dup=1 drop=0 speed=10.8x
(I copy/pasted before waiting for it to finish)
I'm running some experiments. The QSV is definitely working.
Preliminary results of libx264 have the same source file averaging 60fps The same source file using h264_qsv is hovering between 245fps and 249fps
I will share the comparative results after the fact.
Glad to hear it…if you don’t mind, can you ping me on the Homebridge Discord? I would love to collaborate with you on this and try to bring this to the masses.
Sure thing. I will hop over when I can. I'm about to head out.
So, here's the performance with the libx264:
Average fps: 56
frame=139517 fps= 56 q=-1.0 Lsize= 3938290kB time=01:36:58.94 bitrate=5544.4kbits/s dup=1 drop=0 speed=2.32x
[libx264 @ 0x55643ffc5c00] frame I:1546 Avg QP:20.49 size: 98797
[libx264 @ 0x55643ffc5c00] frame P:42247 Avg QP:23.32 size: 43923
[libx264 @ 0x55643ffc5c00] frame B:95724 Avg QP:25.16 size: 20124
[libx264 @ 0x55643ffc5c00] consecutive B-frames: 4.5% 11.4% 1.9% 82.1%
[libx264 @ 0x55643ffc5c00] mb I I16..4: 5.2% 85.0% 9.7%
[libx264 @ 0x55643ffc5c00] mb P I16..4: 1.1% 13.2% 0.7% P16..4: 54.3% 13.9% 5.5% 0.0% 0.0% skip:11.3%
[libx264 @ 0x55643ffc5c00] mb B I16..4: 0.2% 2.0% 0.1% B16..8: 48.5% 5.5% 0.7% direct: 5.2% skip:37.9% L0:49.3% L1:47.3% BI: 3.4%
[libx264 @ 0x55643ffc5c00] 8x8 transform intra:87.7% inter:86.5%
[libx264 @ 0x55643ffc5c00] coded y,uvDC,uvAC intra: 74.3% 58.9% 8.6% inter: 27.3% 16.1% 0.1%
[libx264 @ 0x55643ffc5c00] i16 v,h,dc,p: 22% 21% 6% 51%
[libx264 @ 0x55643ffc5c00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 17% 17% 20% 6% 8% 8% 8% 8% 8%
[libx264 @ 0x55643ffc5c00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 25% 20% 12% 6% 9% 8% 8% 6% 5%
[libx264 @ 0x55643ffc5c00] i8c dc,h,v,p: 57% 20% 18% 5%
[libx264 @ 0x55643ffc5c00] Weighted P-Frames: Y:3.3% UV:0.8%
[libx264 @ 0x55643ffc5c00] ref P L0: 54.2% 13.4% 22.6% 9.6% 0.3%
[libx264 @ 0x55643ffc5c00] ref B L0: 81.5% 13.5% 5.0%
[libx264 @ 0x55643ffc5c00] ref B L1: 95.7% 4.3%
[libx264 @ 0x55643ffc5c00] kb/s:5409.41
[aac @ 0x55643fe88080] Qavg: 787.807
Will start up the h264_qsv on my way out.
And the h264_qsv performance:
Average fps: 249
frame=139517 fps=249 q=26.0 Lsize= 773324kB time=01:36:58.94 bitrate=1088.7kbits/s dup=1 drop=0 speed=10.4x
[aac @ 0x560bbc889f40] Qavg: 787.807
I had the presence of mind to run this with time
:
2077.50s user 73.58s system 383% cpu 9:20.48 total
So much faster. Will join Discord soon.
I’m not worried in the least about performance of hardware acceleration. That I already know will be fine through my own testing…I’m more keen to ensure it gets properly working across a broad set of user environments. Hence wanting to connect. The stats are nice…but candidly…already knew all that. 😄
Now let’s talk about getting it working.
Okay. I'm on Discord now as Untergeek
This issue is locked to prevent necroposting on closed issues. Please create a new issue for related support requests, bug reports, or feature suggestions.
Describe The Problem:
vainfo
shows that vaapi should be available to ffmpeg for hardware encoding and decoding, but it isn't for some reason.It should be noted that the kernel version mentioned below was installed in order to get the hardware encoding/decoding to work with Plex, which is also hosted on this machine.
To Reproduce: Clean install of homebridge + homebridge-unifi-protect on Ubuntu 22.04
Logs:
Homebridge Configuration:
Screenshots: N/A
Environment: