For Ubuntu machine with NVIDIA graphics card, here’s how to implement hardware acceleration for video playback in Firefox web browser.
Firefox so far only supports VA-API for GPU decoding to offload CPU and save power. Both Intel and AMD GPUs support VA-API. However, NVIDIA so far supports the api only through the open-source Nouveau driver.
If you have only NVIDIA GPU running with proprietary driver, then hardware video acceleration does not work out-of-the-box for Firefox.
For choice, there are libvdpau-va-gl1
driver (h.264 only) or libva-vdpau-driver, but both seems no longer updated. The best choice so far is the free open-source nvidia vaapi driver.
NOTE 1: This tutorial so far does not work on Optimus laptop, see this bug report for details.
NOTE 2: This is so far for Firefox only, since other players such as MPV support NVIDIA’s NVDEC out-of-the-box.
NOTE 3: It’s not working for the pre-installed Firefox Snap in my test, since it runs in sandbox. Try installing the official deb package or Mozilla PPA package.
Step 1: Install NVIDIA VAAPI driver
The driver package has been made into Ubuntu repository since Ubuntu 23.10.
So current Ubuntu 23.10 & Ubuntu 24.04 users can simply press Ctrl+Alt+T
to open terminal, and run command to install it:
sudo apt install nvidia-vaapi-driver
For most recent version, there’s an unofficial PPA contains the package for Ubuntu 22.04, Ubuntu 23.10, and Ubuntu 24.04.
- First, add the PPA by running command:
sudo add-apt-repository ppa:ubuntuhandbook1/nvidia-vaapi
Type user password when it asks (no asterisk feedback) for sudo authentication and hit Enter to continue.
- Then refresh system package cache by running command:
sudo apt update
- Finally, install the driver package:
sudo apt install nvidia-vaapi-driver
Then, also install the vainfo
to verify the vaapi info:
sudo apt install vainfo
Step 2: Verify the VA-API info
Now in terminal window, run command:
vainfo
For NVIDIA driver higher than version 525, you need to run export NVD_BACKEND=direct
first to enable direct backend control.
It should outputs something like the screenshot below shows you, including va-api version, and driver version (VA-API NVDEC driver), along with supported codecs.
You can try playing a video file from command line, using MPV (higher than v0.36) to verify if va-api works:
mpv --hwdec=vaapi /path/to/video-file
It shout outputs something looks like Using hardware decoding (vaapi).
Step 3: Config Firefox
Now launch Firefox, type about:config
and hit Enter, then click “Accept the Risk and Continue” to go settings page.
Next, type following keys one by one in filter box, and set them to true:
media.ffmpeg.vaapi.enabled
gfx.x11-egl.force-enabled
widget.dmabuf.force-enabled
Step 4: Setup Environment Variable
In terminal window (Ctrl+Alt+T), run following commands one by one to setup the environment variable for Firefox va-api to work on NVIDIA:
- Specify driver name:
export LIBVA_DRIVER_NAME=nvidia
- Disable RDD process sandbox:
export MOZ_DISABLE_RDD_SANDBOX=1
- For NVIDIA 525 and higher, also set direct control:
export NVD_BACKEND=direct
- For legacy NVIDIA 470, specify the EGL vendor library name:
export __EGL_VENDOR_LIBRARY_FILENAMES=/usr/share/glvnd/egl_vendor.d/10_nvidia.json
- For choice, you may also enable log for debugging purpose:
export NVD_LOG=1
Finally, start Firefox from same terminal window /usr/bin/firefox
and play a video to verify (see next step).
If it works, you can make the environment variables permanent via following steps:
- First, open home folder, press
Ctrl+h
, find out and click edit the .profile file. - When file opens, add the previous
export
commands that you run (exclude the NVD_LOG) into end as new lines Finally, save the file.
You need to log out and back in to make the .profile
file changes to work.
Step 5: Verify
While playing a video using your Firefox web browser, you may using nvtop
command to verify.
- First, open terminal (
Ctrl+Alt+T
) and run command to install nvtop:sudo apt install nvtop
- Then, run
nvtop
command in terminal while video playback. If the GPU is decoding, then there’s a line in bottom marked as Compute type, with info of which process and command. In the case, it should be something to do with firefox.
For choice, you may also run nvidia-smi pmon
instead, there should be type C in output for corresponding process while playback.
For more, see nvidia-vaapi-driver in github page.
Uninstall:
If the driver somehow does not work for you, you may remove it by running command in terminal:
sudo apt remove --autoremove nvidia-vaapi-driver
Also, remove the PPA (if added) via command:
sudo add-apt-repository --remove ppa:ubuntuhandbook1/nvidia-vaapi
You may also go to about:config
page in Firefox to undo the changes you did, and remove the export
(if set) lines in .profiles
file.