Hardware video acceleration in Web browsers

From Linux Gaming
Jump to navigation Jump to search

Introduction

Hardware accelerated video makes it possible for the video card to decode/encode video, thus offloading the CPU and saving power.

This article gives detail on enabling hardware video acceleration in browsers used in linux. This article details them for Firefox and Chromium, but similar instructions apply to other browsers based on the above two.

Firefox

Firefox currently supports hardware acceleration on both x11 and wayland. This is tested on firefox 96 (nightly).The following settings need to be changed in about:config:

media.ffmpeg.vaapi.enabled  true
gfx.webrender.all           true
media.ffvpx.enabled         false

In addition, while running X11, firefox should be launched with variable MOZ_X11_EGL=1 . Also, users should ensure that vaapi is available by installing/running vainfo in terminal. In cases where gpu only supports h.264 decode, its beneficial to install h.264ify.

What works:

  1. both 30fps/60fps or higher videos play smoothly at resolutions supported by gpu decoder.
  2. CPU usage is significantly reduced while playing videos

The problems:

  1. The colour of videos appears washed out. (0,0,0) is mapped to (16,16,16) and (255,255,255) to (235,235,235). Bug 1726186 (Fixed on Firefox 96+!)
  2. Nvidia users out of luck (for now)

Chromium

Chromium also supports hardware acceleration in x11 and wayland. This too was tested in chromium-dev 97. To enable it, first set chrome://flags/#ignore-gpu-blocklist to disabled. Then launch chromium with the following flags:

--use-gl=desktop --enable-features=VaapiVideoDecoder

In theory, --use-gl=desktop should not be required in wayland, but the wiki editor couldn't get it to work without it. Also h.264ify will be required as stated above.

What works:

  1. both 30fps/60fps or higher videos play smoothly at resolutions supported by gpu decoder, unless vulkan backend is enabled through flags. However there is little reason to enable it for now
  2. Videos are played colour accurately

The problems:

  1. CPU usage reduction is minimal. I could not observe significant improvement in reduction of cpu load or improved battery life ( on different machine) despite confirming that hardware video acceleration was working while playing videos
  2. Nvidia users out of luck again

Google chrome and other flavors

Same should work for google chrome (and other chromium-based browsers like Brave, Vivaldi, edge etc.)

There is a config file for google chrome located at $HOME/.config/chrome-flags.conf (if file does not exist, create it), where you can put flags that chrome should start with by default (because starting browser with parameters sometimes can be hard/pain).


NEEDS SPECIFYING: Does this file work for other chromium based browsers and chromium itself?


$HOME/.config/chrome-flags.conf:

--enable-features=VaapiVideoDecoder
--use-gl=desktop

nVidia GPUs

For full GPU acceleration with nVidia GPUs it's required to set 2 additional flags:

--ignore-gpu-blocklist --enable-zero-copy

Optimizing performance

For better performance it is recommended to set 2 additional flags:

--enable-gpu-rasterization --canvas-oop-rasterization

Epiphany/Gnome Web

Based on GTKwebkit, it provides another option for users that may be not as powerful as chrome or firefox, but still fairly usable for modern web. To enable hardware accelerated video, first install gstreamer-vaapi for amd/intel gpus or gst-plugins-bad and nvidia-utils for nvidia GPUs. Then run:

gsettings set org.gnome.Epiphany.web:/ hardware-acceleration-policy 'always'

What works:

  1. both 30fps/60fps or higher videos play smoothly at resolutions supported by gpu decoder
  2. Videos are color accurate
  3. Works for nVidia users
  4. Good reduction in CPU usage

The problems:

  1. Lack of extensions
  2. No simple way to limit video codec to h.264, hence older GPUs will lack hardware video acceleration in most videos with codecs other than h.264

Alternatives

VLC and MPV both support proper hardware accelerated video in linux. They can be set to launch when a video is played in a browser. This can be done in firefox with Open in vlc or ff2mpv extension or other similar extensions. Similarly for chromium, Play with MPV or Play with VLC or similar.

What works:

  1. CPU utilisation better than firefox and chrome
  2. Videos are played with accurate colours
  3. Works for NVIDIA users

The problems:

  1. Many sites with DRM (such as most paid streaming services) and those using custom video players may not work.

Further Reading