High Efficiency Video Coding

From LinuxReviews
Jump to navigationJump to search
HEVC / H.265 / MPEG-H Part 2
(High efficiency video coding)
OrganizationITU-T, ISO, IEC
Developed byITU-T Study Group 16, VCEG, MPEG
Announced2013
Type of formatVideo coding format
Contained byMP4
Extended toH.266 (VVC)
StandardH.261, H.262, H.263, H.264, MPEG-1
Websitehttps://www.itu.int/rec/T-REC-H.265

High Efficiency Video Coding (HEVC, better known as H265) is a fairly efficient non-free patent-riddled video compression standard developed by the ISO/IEC Moving Picture Experts Group. It is a widely used standard in proprietary products and solutions like IPTV, video on demand and round metallic coasters used to distribute movies through retail stores.

Internet Adoption[edit]

The HEVC format is covered by myriad of patents held by a number of corporations who have a hard time agreeing on anything. The result of this is that there are two patent pools. Anyone who wants to create commercial products capable of encoding or playing HEVC or distribute video encoded in the HEVC format will have to pay royalty fees to both patent pools. The result of the patent mess is that some big players not invested in MPEG and their patent-riddled standards, namely Google, refuse to have anything to do with HEVC. The biggest video distribution platform, Google-controlled YouTube, is using the competing free and open VP9 codec to distribute higher-resolution 1440p and 4K video.

Google controls the Chromium web browser codebase the vast majority of web browsers are based on. Their HEVC resistance means that both Chromium-based web browsers can not play HEVC video.

The only web browsers that are capable of playing HEVC video are those found on Android devices and those made by some American fruit company who makes overpriced social media interaction devices. VP9, on the other hand, can be played by every web modern web browser except those made by said American fruit company.

Video formats supported by web browsers
Browser H.264/MPEG-4 AVC (MP4) HEVC (MP4) VP8 (WebM) VP9 (WebM) AV1 (WebM)
Android browsers Dialog-ok.svg Dialog-ok.svg Dialog-ok.svg Dialog-ok.svg Dialog-ok.svg
Chromium Blond-anime-girl-with-red-questionmark.png Depends on the build Dialog-cancel.svg Dialog-ok.svg Dialog-ok.svg Dialog-ok.svg
Google Chrome Dialog-ok.svg Dialog-cancel.svg Dialog-ok.svg Dialog-ok.svg Dialog-ok.svg
Microsoft Edge
(Modern Chromium based versions)
Dialog-ok.svg Blond-anime-girl-with-red-questionmark.png Only if hardware decoding is available Dialog-ok.svg Dialog-ok.svg Dialog-ok.svg
Falkon Dialog-cancel.svg Dialog-cancel.svg Dialog-ok.svg Dialog-ok.svg Dialog-ok.svg
Mozilla Firefox Dialog-ok.svg Blond-anime-girl-with-red-questionmark.png if ffmpeg supports it Dialog-cancel.svg Dialog-ok.svg Dialog-ok.svg
Safari (macOS/iOS) Dialog-ok.svg Dialog-ok.svg Construction.png only for WebRTC Dialog-cancel.svg Dialog-cancel.svg
GNOME Web Dialog-ok.svg Blond-anime-girl-with-red-questionmark.png if gstreamer codecs are present Blond-anime-girl-with-red-questionmark.png if gstreamer codecs are present Dialog-ok.svg Dialog-ok.svg

Linux Tools And Compatibility[edit]

GNU/Linux distributions do not include tools and libraries capable of playing or encoding HEVC video. This default severe lack of ability to handle HEVC files means that GNU/Linux users are, out of the box, unable to play the majority of the high resolution video files found on local friendly BitTorrent sites.

Most distributions require a user-configured third party repository to be configured in order to play and encode HEVC video. As an example, the FFmpeg library available in the rpmfusion Fedora repository can be used to both decode and encode HEVC video. The FFmpeg library found in Fedora's own repositories can't. mpv and other media players who utilize the FFmpeg library will only be able to play HEVC if such a repository is enabled. The same applies to VLC, the plain "stock" version included in modern GNU/Linux is typically compiled without HEVC support.

Creating HEVC files with ffmpeg[edit]

FFmpeg is a nice tool for converting video to HEVC (or x265, which is what you need to call it when you use ffmpeg).

The basic arguments you need to use for good quality using CPU software encoding are:

parameter function
-c:v libx265 Select HEVC/x265
-crf 23 crf sets the picture quality target. Higher numbers mean better quality and larger file sizes. 23 is good for high quality, 27 is a nice value for medium quality.
-preset medium Can also be slow, fast, superfast. Medium works well for most use-cases.
-x265-params wpp:pmode Enabled more parallel processing than default.
-c:a aac Select AAC for audio. You must use AAC audio if you use a MP4 container. You can mix HEVC with Opus and other audio codecs if you use a container like MPV.
-b:a 192k Sets audio bitrate.
-strict experimental ffmpeg refuses to do anything if you don't enable supposedly "experimental" features.

Combining the above in a script may or may not be useful.

File: ffmpeg-encode-x265-HQ.sh
#!/bin/bash
inputf="$*"
inputnoext="${inputf%.*}"
outputf="${inputnoext}.x265-crf-23.mp4"

nice -n 19 ffmpeg -i "$*" \
  -c:v libx265 -preset medium -crf 23 -x265-params wpp:pmode \
  -c:a aac -strict experimental -b:a 192k \
  "${outputf}"

Hardware Encoding[edit]

Skylake and newer Intel CPUs with iGPUs and Polaris and newer AMD graphics chips support HEVC hardware encoding (and decoding). This requires several parameters that are completely different from those used for software (CPU) encoding. The important ones for doing hardware video encoding are:

parameter function
-vaapi_device /dev/dri/renderD128 Selects the first available graphics card as the render device. A rendering device must be defined before any filter graph option (-cf).
-c:v hevc_vaapi Use HEVC VAAPI as video codec
-b:v 6M Sets bitrate to variable bitrate targeting 6Mbps.
You can't use a -crf target with VAAPI.
-vf format=nv12,hwupload -vf enables filter-graphs. hwupload tells ffmpeg to move the video from regular system memory to GPU memory. The hardware encoders will only accept input as VAAPI surfaces. nv12 selects the right surface for 8-bit HEVC video. You can use p010 for 10-bit video if your hardware supports it.

You may want, but do not need, a few additional parameters:

parameter function
-maxrate 6M Caps the max bitrate to 6M. Useful if you use -b:v 6M and you never want the bitrate to go above 6M (but it can go below). Can be useful when streaming from a limited Internet connection.
-b:a 192k Set max audio bitrate to 192k

The above parameters can be combined into a simple script so you do not have to type them all the time:

File: ffmpeg-encode-x265-HW.sh
#!/bin/bash
inputf="$*"
inputnoext="${inputf%.*}"
outputf="${inputnoext}.x265.mp4"

ffmpeg -i "$*" \
  -vaapi_device /dev/dri/renderD128 \
  -c:v hevc_vaapi \
  -b:v 6M
  -vf 'format=nv12,hwupload' \
  "${outputf}"

The ffmpeg wiki has some VAAPI examples, mostly not related to HEVC, at trac.ffmpeg.org/wiki/Hardware/VAAPI.

Competing Video Codecs[edit]


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.