硬體視頻加速
硬體視頻加速讓顯卡進行視頻的解碼和編碼,以減少CPU的負載並節省電力。
Linux 中有多種方式可以實現視頻加速:
- 視頻加速API (Video Acceleration API, 縮寫為VA-API) 是一套 Intel 提供的視頻硬體編解碼的開源庫和標準。
- Unix視頻解碼及演示 API (Video Decode and Presentation API for Unix,縮寫為VDPAU) (VDPAU) 是一套 Nvidia 提供的把部分視頻解碼和後期處理工作轉移到顯卡的開源庫和 API。
- Advanced Media Framework SDK (AMF) 是 AMD 開發的 GPU 開源框架,可以用 AMDGPU PRO 技術棧實現最優的處理。
- NVDEC/NVENC NVIDIA 的 費米、克卜勒、Maxwell 和 Pascal 系列顯卡所使用的專有 API。
- Vulkan Video is an extension of the Vulkan graphics API designed to support hardware-accelerated video encoding and decoding.
大致的總覽對比和應用程式支持,參見頁面下方的對照表。
Intel graphics 開源驅動支持 VA-API:
- Broadwell (~2014) 及之後的 HD Graphics 系列(例如英特爾 Arc 系列顯卡),請使用 intel-media-driver包 。
- GMA 4500 (2008) 到 Coffee Lake (2017) 的 GPU, 請使用 libva-intel-driver包 。
- Haswell Refresh 到 Skylake 的 VP9 解碼,以及 Broadwell 到 Skylake 的混合 VP8 編碼,請使用 intel-hybrid-codec-driver-gitAUR.
參閱 VAAPI 支持的硬體和功能。
ANV 開源 vulkan 驅動通過 vulkan-intel包 提供 Vulkan 視頻支持。
對於 Intel VPL ,請 安裝 基礎庫 libvpl包 ,並確保安裝下列之一的運行時環境:
- vpl-gpu-rt包 :為 Tiger Lake 和更新的GPU提供支持
- intel-media-sdk包 (已終止後續支持):為舊 Intel GPU 提供支持
Nouveau 開源驅動支持 VA-API 和 VDPAU:
- GeForce 8 到 GeForce GTX 750 的硬體,請使用 mesa包
- 還需要 nouveau-fwAUR 固件包(目前是從NVIDIA二進制驅動解壓出來的)
NVIDIA 專有驅動通過 nvidia-utils包 支持以下硬體:
- GeForce 8 系列 和新 GPU 上的 VDPAU;
- Kepler 和更新的GPU上的 NVENC ;
AMDGPU 和 ATI 開源驅動通過 mesa包 支持 VA-API 和 VDPAU:
- VA-API on Radeon HD 2000 and newer GPUs.
- VDPAU on Radeon R600 and newer GPUs.
RADV 開源 vulkan 驅動通過 vulkan-vadeon包 提供支持。
AMDGPU PRO 專有驅動是基於 AMDGPU 驅動的,所以支持 VA-API、VDPAU 和 AMF。
- Fiji 和更新的 GPUs 通過 amf-amdgpu-proAUR 支持 AMF。
- 你也許需要強制你的應用程式使用 AMDGPU PRO Vulkan 驅動。
- HEVC 可能在 Navi 之前的 GPU 上無法使用。
- libvdpau-va-gl — OpenGL/VAAPI 做後端的 VDPAU 驅動,僅支持 H.264.
- nvidia-vaapi-driver — CUDA NVDEC 作為後端的 VA-API.
你的系統也許不需要任何配置就可以完美地開箱即用。因此,建議閱讀本節,以確認是否確實如此。
- 有命令行支持的 mpv 是測試硬體加速的絕佳工具。查看
mpv --hwdec=auto video_filename的日誌並參見 hwdec 以獲得更多信息。 - 對於 Intel GPU ,例如使用 intel-gpu-tools包 並以 root 運行
intel_gpu_top以監控視頻播放期間的 GPU 活動。若 「Video」 欄超過 0%,則表示正在使用 GPU 進行視頻解碼/編碼。(如果你使用的是 新的 Xe 驅動 來運行intel_gpu_top會拋出錯誤,所以檢查並運行gputop,若 vcs 欄位 超過 0%,則表示正在使用 GPU 進行視頻解碼/編碼。) - 對於 AMD,Intel 和 Nvidia ,在播放視頻時使用 nvtop包 來查看(在頂部幾行) "DEC"(解碼器)的利用率。
- 對於 AMD GPU ,使用 radeontop包 查看 GPU 利用率。不同於 intel-gpu-tools包 ,當前沒有任何方法在 radeontop包 [3] 上查看編碼器/解碼器的利用率。
- 無論是哪種 GPU,你都可以使用 htop包 等工具對比 CPU 使用率。特別是對於更高解析度(4k+)的視頻,如果啟用了並正常使用 VA-API,CPU 占用應顯著降低,尤其是在筆記本等低功耗設備上。
運行 libva-utils包 包提供的 vainfo 來檢查 VA-API 的設置:
$ vainfo
libva info: VA-API version 0.39.4
libva info: va_getDriverName() returns 0
libva info: Trying to open /usr/lib/dri/i965_drv_video.so
libva info: Found init function __vaDriverInit_0_39
libva info: va_openDriver() returns 0
vainfo: VA-API version: 0.39 (libva 1.7.3)
vainfo: Driver version: Intel i965 driver for Intel(R) Skylake - 1.7.3
vainfo: Supported profile and entrypoints
VAProfileMPEG2Simple : VAEntrypointVLD
VAProfileMPEG2Simple : VAEntrypointEncSlice
VAProfileMPEG2Main : VAEntrypointVLD
VAProfileMPEG2Main : VAEntrypointEncSlice
VAProfileH264ConstrainedBaseline: VAEntrypointVLD
VAProfileH264ConstrainedBaseline: VAEntrypointEncSlice
VAProfileH264ConstrainedBaseline: VAEntrypointEncSliceLP
VAProfileH264Main : VAEntrypointVLD
VAProfileH264Main : VAEntrypointEncSlice
VAProfileH264Main : VAEntrypointEncSliceLP
VAProfileH264High : VAEntrypointVLD
VAProfileH264High : VAEntrypointEncSlice
VAProfileH264High : VAEntrypointEncSliceLP
VAProfileH264MultiviewHigh : VAEntrypointVLD
VAProfileH264MultiviewHigh : VAEntrypointEncSlice
VAProfileH264StereoHigh : VAEntrypointVLD
VAProfileH264StereoHigh : VAEntrypointEncSlice
VAProfileVC1Simple : VAEntrypointVLD
VAProfileVC1Main : VAEntrypointVLD
VAProfileVC1Advanced : VAEntrypointVLD
VAProfileNone : VAEntrypointVideoProc
VAProfileJPEGBaseline : VAEntrypointVLD
VAProfileJPEGBaseline : VAEntrypointEncPicture
VAProfileVP8Version0_3 : VAEntrypointVLD
VAProfileVP8Version0_3 : VAEntrypointEncSlice
VAProfileHEVCMain : VAEntrypointVLD
VAProfileHEVCMain : VAEntrypointEncSlice
VAEntrypointVLD 指的是你的顯卡能夠解碼這個格式,VAEntrypointEncSlice 指的是你的顯卡可以編碼這個格式。
上面的例子裡,i965 驅動正在運行,所以你可以看見這一行:
libva info: Trying to open /usr/lib/dri/i965_drv_video.so
如果運行 vainfo 的時候出現了下面的錯誤提示:
libva info: va_openDriver() returns -1 vaInitialize failed with error code -1 (unknown libva error),exit
你要重新配置正確的驅動,詳見Hardware video acceleration#Configuring VA-API.
安裝 vdpauinfo包 以驗證 VDPAU 驅動是否正確加載,並獲取完整的配置信息報告:
$ vdpauinfo
display: :0 screen: 0 API version: 1 Information string: G3DVL VDPAU Driver Shared Library version 1.0 Video surface: name width height types ------------------------------------------- 420 16384 16384 NV12 YV12 422 16384 16384 UYVY YUYV 444 16384 16384 Y8U8V8A8 V8U8Y8A8 Decoder capabilities: name level macbs width height ---------------------------------------------------- MPEG1 --- not supported --- MPEG2_SIMPLE 3 9216 2048 1152 MPEG2_MAIN 3 9216 2048 1152 H264_BASELINE 41 9216 2048 1152 H264_MAIN 41 9216 2048 1152 H264_HIGH 41 9216 2048 1152 VC1_SIMPLE 1 9216 2048 1152 VC1_MAIN 2 9216 2048 1152 VC1_ADVANCED 4 9216 2048 1152 ...
Install vulkan-tools包 and use vulkaninfo to verify if the video processing extensions are available:
$ vulkaninfo | grep VK_KHR_video_
VK_KHR_video_decode_av1 : extension revision 1 VK_KHR_video_decode_h264 : extension revision 9 VK_KHR_video_decode_h265 : extension revision 8 VK_KHR_video_decode_queue : extension revision 8 VK_KHR_video_encode_h264 : extension revision 14 VK_KHR_video_encode_h265 : extension revision 14 VK_KHR_video_encode_queue : extension revision 12 VK_KHR_video_maintenance1 : extension revision 1 VK_KHR_video_queue : extension revision 8
Although the video driver should automatically enable hardware video acceleration support for both VA-API and VDPAU, it may be needed to configure VA-API/VDPAU manually. Only continue to this section if you went through #Verification.
The default driver names, used if there is no other configuration present, are guessed by the system. However, they are often hacked together and may not work. The guessed value will be printed in the Xorg log file, which is ~/.local/share/xorg/Xorg.0.log if rootless, or /var/log/Xorg.0.log if Xorg is running as root. To search the log file for the values of interest:
$ grep -iE 'vdpau | dri driver' xorg_log_file
(II) RADEON(0): [DRI2] DRI driver: radeonsi (II) RADEON(0): [DRI2] VDPAU driver: radeonsi
In this case radeonsi is the default for both VA-API and VDPAU.
journalctl -b --grep='vdpau | dri driver' 。This does not represent the configuration however. The values above will not change even if you override them.
You can override the driver for VA-API by using the LIBVA_DRIVER_NAME environment variable:
-
Intel graphics:
- For libva-intel-driver包 use
i965. - For intel-media-driver包 use
iHD.
- For libva-intel-driver包 use
- NVIDIA:
- AMD:
- For AMDGPU driver use
radeonsi.
- For AMDGPU driver use
- You can find the installed drivers in
/usr/lib/dri/. They are used as/usr/lib/dri/${LIBVA_DRIVER_NAME}_drv_video.so. - Some drivers are installed several times under different names for compatibility reasons. You can see which by running
sha1sum /usr/lib/dri/* | sort. -
LIBVA_DRIVERS_PATHcan be used to overrule the VA-API drivers location. - Since version 12.0.1 Mesa provides
radeonsiinstead ofgallium.
You can override the driver for VDPAU by using the VDPAU_DRIVER environment variable.
The correct driver name depends on your setup:
- For Intel graphics you need to set it to
va_gl. - For the open source AMD driver set it to
radeonsi. - For the open source Nouveau driver set it to
nouveau. - For NVIDIA's proprietary version set it to
nvidia.
- You can find the installed drivers in
/usr/lib/vdpau/. They are used as/usr/lib/vdpau/libvdpau_${VDPAU_DRIVER}.so. - Some drivers are installed several times under different names for compatibility reasons. You can see which by running
sha1sum /usr/lib/vdpau/*. - For hybrid setups (both NVIDIA and AMD), it may be necessary to set the
DRI_PRIMEenvironment variable. For more information see PRIME.
-
Intel graphics: Vulkan Video support in vulkan-intel包 can be enabled with the
ANV_DEBUG=video-decode,video-encodeenvironment variable.
-
AMD: Vulkan Video support in vulkan-radeon包 is enabled by default for VCN 2, 3, and 4+ since Mesa 25. To force-enable support on older cards, set the
RADV_PERFTESTenvironment variable tovideo_decode,video_encode.
多媒體框架:
視頻播放器:
- Kodi#Hardware video acceleration
- MPlayer#Hardware video acceleration
- mpv#Hardware video acceleration
- VLC media player#Hardware video acceleration
網頁瀏覽器:
多媒體錄製/串流:
You need to set VDPAU_DRIVER variable to point to correct driver. See #配置 VDPAU.
An error along the lines of libva: /usr/lib/dri/i965_drv_video.so init failed is encountered. This can happen because of improper detection of Wayland. One solution is to unset $DISPLAY so that mpv, MPlayer, VLC, etc. do not assume it is X11. Another mpv-specific solution is to add the parameter --gpu-context=wayland.
This error can also occur if you installed the wrong VA-API driver for your hardware.
When experiencing video decoding corruption or distortion with AMDGPU driver, set allow_rgb10_configs=false as an environment variable. [4]
| Codec | libva-intel-driver包 [5] | intel-media-driver包 [6] | mesa包 [7] [8] |
libva-nvidia-driver包 (NVDEC adapter) |
|---|---|---|---|---|
| Decoding | ||||
| MPEG-2 | GMA 4500 and newer | Broadwell and newer | Radeon HD 6000 and newer GeForce 8 and newer1 |
See #NVIDIA driver only |
| H.263/MPEG-4 Visual3 | 否 | 否 | Radeon HD 6000 and newer | |
| VC-1 | Sandy Bridge and newer | Broadwell and newer | Radeon HD 2000 and newer GeForce 9300 and newer1 |
|
| H.264/MPEG-4 AVC | Ironlake and newer | Radeon HD 2000 and newer GeForce 8 and newer1 |
||
| H.265/HEVC 8bit | Cherryview/Braswell and newer | Skylake and newer | Radeon R9 Fury and newer | |
| H.265/HEVC 10bit | Broxton and newer | Broxton/Apollo Lake and newer | Radeon 400 and newer | |
| VP8 | Broadwell and newer | Broadwell and newer | 否 | |
| VP9 8bit | Broxton and newer Hybrid: Haswell refresh to Skylake2 |
Broxton/Apollo Lake and newer | Raven Ridge + Radeon RX 5000 and newer | |
| VP9 10bit & 12bit | Kaby Lake and newer | Kaby Lake and newer | ||
| AV1 8bit & 10bit | 否 | Tiger Lake and newer | Radeon RX 6600 and higher/newer | |
| Encoding | ||||
| MPEG-2 | Ivy Bridge and newer | Broadwell and newer except Broxton/Apollo Lake |
否 | 否4 |
| H.264/MPEG-4 AVC | Sandy Bridge and newer | Broadwell and newer | Radeon HD 7000 and newer | |
| H.265/HEVC 8bit | Skylake and newer | Skylake and newer | Radeon 400 and newer | |
| H.265/HEVC 10bit | Kaby Lake and newer | Kaby Lake and newer | Raven Ridge + Radeon RX 5000 and newer | |
| VP8 | Cherryview/Braswell and newer Hybrid: Haswell to Skylake2 |
否 | ||
| VP9 8bit | Kaby Lake and newer | Ice Lake and newer | ||
| VP9 10bit & 12bit | 否 | |||
| AV1 8bit & 10bit | Alchemist and newer | Radeon RX 7900 and higher/newer | ||
- Up until GeForce GTX 750.
- Hybrid VP8 encoder and VP9 decoder supported by intel-hybrid-codec-driver-gitAUR.
- MPEG-4 Part 2 is disabled by default due to VAAPI limitations. Set the environment variable
VAAPI_MPEG4_ENABLED=trueto try to use it anyway. - NVIDIA CUDA adapter codec support is in active development and susceptible to change [9].
| Codec | Color depth |
mesa包 [10] [11] | nvidia-utils包 |
libvdpau-va-gl包 (VA-API adapter) |
|---|---|---|---|---|
| Decoding | ||||
| MPEG-2 | 8bit | Radeon R600 and newer GeForce 8 and newer1 |
GeForce 8 and newer | 否 |
| H.263/MPEG-4 Visual | 8bit | Radeon HD 6000 and newer GeForce 200 and newer1 |
GeForce 200 and newer | |
| VC-1 | 8bit | Radeon HD 2000 and newer GeForce 9300 and newer1 |
GeForce 8 and newer2 | |
| H.264/MPEG-4 AVC | 8bit | Radeon HD 2000 and newer GeForce 8 and newer1 |
GeForce 8 and newer | See #VA-API drivers |
| H.265/HEVC | 8bit | Radeon R9 Fury and newer | GeForce 900 and newer3 | 否 |
| 10bit | Radeon 400 and newer | 否4 | ||
| VP9 | 8bit | Raven Ridge + Radeon RX 5000 and newer | GeForce 900 and newer3 | |
| 10bit/12bit | 否4 | |||
| AV1 | 8bit | Radeon RX 6600 and higher/newer | GeForce 30 and newer5 | |
| 10bit | 否4 | |||
- Up until GeForce GTX 750.
- Except GeForce 8800 Ultra, 8800 GTX, 8800 GTS (320/640 MB).
- Except GeForce GTX 970 and GTX 980.
- NVIDIA implementation is limited to 8bit streams [12] [13].
- Starting with driver version 510.[14]
| Codec | nvidia-utils包 [15] | |
|---|---|---|
| NVDEC | NVENC | |
| MPEG-2 | Fermi and newer1 | 否 |
| VC-1 | ||
| H.264/MPEG-4 AVC | Kepler and newer2 | |
| H.265/HEVC 8bit | Maxwell (GM206) and newer | Maxwell (2nd Gen) and newer |
| H.265/HEVC 10bit | Pascal and newer | |
| VP8 | Maxwell (2nd Gen) and newer | 否 |
| VP9 8bit | Maxwell (GM206) and newer | |
| VP9 10bit & 12bit | Pascal and newer | |
| AV1 8bit & 10bit | Ampere and newer3 | Ada Lovelace and newer |
- Except GM108 (not supported)
- Except GM108 and GP108 (not supported)
- Except A100 (not supported)
| 應用程式 | 解碼 | 編碼 | 文檔 | |||||
|---|---|---|---|---|---|---|---|---|
| VA-API | VDPAU | NVDEC | Vulkan | VA-API | NVENC | Vulkan | ||
| FFmpeg | 是 | 是 | 是 | 是 | 是 | 是 | 是 | FFmpeg#Hardware video acceleration |
| GStreamer | 是 | 否 | 是 | 是 | 是 | 是 | 否 | GStreamer#Hardware video acceleration |
| Kodi | 是 | 是 | 否 | 否 | – | – | – | Kodi#Hardware video acceleration |
| mpv | 是 | 是 | 是 | 是 | – | – | – | mpv#Hardware video acceleration |
| VLC media player | 是 | 是 | 否 | – | – | VLC media player#Hardware video acceleration | ||
| MPlayer | 否 | 是 | 否 | 否 | – | – | – | MPlayer#Hardware video acceleration |
| Chromium | 是 | 否 | 否 | 否 | 是 | 否 | 否 | Chromium#Hardware video acceleration |
| Firefox | 是 | 否 | 否 | 否 | 否 | 否 | Firefox#Hardware video acceleration | |
| GNOME/Web | GStreamer | ? | ? | ? | GNOME/Web#Video | |||