跳转到内容

硬件视频加速

来自 Arch Linux 中文维基

硬件视频加速让显卡进行视频的解码和编码,以减少CPU的负载并节省电力。

Linux 中有多种方式可以实现视频加速:

大致的总览对比和应用程序支持,参见页面下方的对照表。

安装

[编辑 | 编辑源代码]

VA-API

[编辑 | 编辑源代码]

Intel graphics 开源驱动支持 VA-API:

注意:Skylake 及之后的硬件还需要 linux-firmware.

参阅 VAAPI 支持的硬件和功能


Vulkan

[编辑 | 编辑源代码]

ANV 开源 vulkan 驱动通过 vulkan-intel 提供 Vulkan 视频支持。

注意:启用 Vulkan 视频支持需要额外配置,参见 #Configuring Vulkan Video

Intel 视频处理库 (Intel VPL)

[编辑 | 编辑源代码]

对于 Intel VPL ,请 安装 基础库 libvpl ,并确保安装下列之一的运行时环境:

  • vpl-gpu-rt :为 Tiger Lake 和更新的GPU提供支持
  • intel-media-sdk (已终止后续支持):为旧 Intel GPU 提供支持

NVIDIA

[编辑 | 编辑源代码]

Nouveau 开源驱动支持 VA-API 和 VDPAU:

  • GeForce 8 到 GeForce GTX 750 的硬件,请使用 mesa
  • 还需要 nouveau-fwAUR 固件包(目前是从NVIDIA二进制驱动解压出来的)

NVIDIA 专有驱动通过 nvidia-utils 支持以下硬件:

  • Kepler 和更新的GPU上的 NVENC ;
  • Pascal 和更新的GPU [2] 上的 Vulkan 视频。

AMD/ATI

[编辑 | 编辑源代码]

AMDGPUATI 开源驱动通过 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。

注意:
  • 你也许需要强制你的应用程序使用 AMDGPU PRO Vulkan 驱动。
  • HEVC 可能在 Navi 之前的 GPU 上无法使用。

翻译层

[编辑 | 编辑源代码]
  • libvdpau-va-gl — OpenGL/VAAPI 做后端的 VDPAU 驱动,仅支持 H.264.
https://github.com/i-rinat/libvdpau-va-gl || libvdpau-va-gl
  • nvidia-vaapi-driverCUDA NVDEC 作为后端的 VA-API.
https://github.com/elFarto/nvidia-vaapi-driver/ || libva-nvidia-driver
注意:尽管 libva-nvidia-driver 的实现确实启用了硬件视频解码,但由于当前 NVIDIA 的电源管理限制,它实际上 比使用 CPU 进行视频解码消耗更多的电力

验证

[编辑 | 编辑源代码]

你的系统也许不需要任何配置就可以完美地开箱即用。因此,建议阅读本节,以确认是否确实如此。

提示:
  • 有命令行支持的 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 占用应显著降低,尤其是在笔记本等低功耗设备上。

验证 VA-API

[编辑 | 编辑源代码]

运行 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.

验证 VDPAU

[编辑 | 编辑源代码]

安装 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
...

Verifying Vulkan Video

[编辑 | 编辑源代码]

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.

注意:如果你使用 GDM, 请以 root 权限运行 journalctl -b --grep='vdpau | dri driver'

This does not represent the configuration however. The values above will not change even if you override them.

配置 VA-API

[编辑 | 编辑源代码]

You can override the driver for VA-API by using the LIBVA_DRIVER_NAME environment variable:

注意:
  • 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_PATH can be used to overrule the VA-API drivers location.
  • Since version 12.0.1 Mesa provides radeonsi instead of gallium.

配置 VDPAU

[编辑 | 编辑源代码]

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_PRIME environment variable. For more information see PRIME.

Configuring Vulkan Video

[编辑 | 编辑源代码]
  • 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_PERFTEST environment variable to video_decode,video_encode.

配置应用程序

[编辑 | 编辑源代码]

多媒体框架:

视频播放器:

网页浏览器:

多媒体录制/串流:

疑难解答

[编辑 | 编辑源代码]

打开 VDPAU 后端失败

[编辑 | 编辑源代码]

You need to set VDPAU_DRIVER variable to point to correct driver. See #配置 VDPAU.

VA-API 初始化失败

[编辑 | 编辑源代码]

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.

使用 AMDGPU 驱动时视频解码损坏或失真

[编辑 | 编辑源代码]

When experiencing video decoding corruption or distortion with AMDGPU driver, set allow_rgb10_configs=false as an environment variable. [4]

对照表

[编辑 | 编辑源代码]

VA-API 驱动

[编辑 | 编辑源代码]
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
  1. Up until GeForce GTX 750.
  2. Hybrid VP8 encoder and VP9 decoder supported by intel-hybrid-codec-driver-gitAUR.
  3. MPEG-4 Part 2 is disabled by default due to VAAPI limitations. Set the environment variable VAAPI_MPEG4_ENABLED=true to try to use it anyway.
  4. NVIDIA CUDA adapter codec support is in active development and susceptible to change [9].

VDPAU 驱动

[编辑 | 编辑源代码]
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
  1. Up until GeForce GTX 750.
  2. Except GeForce 8800 Ultra, 8800 GTX, 8800 GTS (320/640 MB).
  3. Except GeForce GTX 970 and GTX 980.
  4. NVIDIA implementation is limited to 8bit streams [12] [13].
  5. Starting with driver version 510.[14]

NVIDIA driver only

[编辑 | 编辑源代码]
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
  1. Except GM108 (not supported)
  2. Except GM108 and GP108 (not supported)
  3. 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