Tim's blah blah blah

Hardware accelerated FFmpeg encoding on Intel/Ubuntu 20.04

Although hardware accelerated (video) encoding has been around for a long time, getting it to work on Ubuntu 20.04 took some trial and error. In the end, VAAPI works for me with stock ffmpeg when run as root, although it might have had to do something with the packages that I installed.

Configuration

Semi-random packages I installed found in various guides:

sudo apt install ffmpeg libav vaapi vainfo intel-media-va-driver-non-free x264 libva-dev libmfx-dev intel-media-va-driver-non-free x264 intel-media-va-driver-non-free i965-va-driver-shaders
export LIBVA_DRIVER_NAME=iHD

Method #1 - VAAPI

Encoding using VAAPI - seems fairly straightforward, simply add codec options as you please

file="test2.mp4-x264_aac.mp4"
user="tim"
sudo usermod -aG render "${user}"

# Get new group membership in effect
su "${user}"

ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 -i "${file}" -codec:v h264_vaapi  "${file}-x264_aac.mp4"

Method #2 - QSV

Follow Intel guide (github.com) to compile FFmpeg yourself. Encoding using QSV (=QuickSync) - seems very fragile/complex, changing any parameter seems to break stuff (e.g. # of frames converted)

sudo ./download/FFmpeg-n4.3.2/ffmpeg -init_hw_device qsv=hw -filter_hw_device hw -f rawvideo -pix_fmt yuv420p -s:v 176x144 -i "${file}" -vf hwupload=extra_hw_frames=64,format=qsv -c:v h264_qsv -b:v 5M -frames:v 10 -y "${file}"-aac.mp4 

Trial and error

Command

file="test2.mp4-x264_aac.mp4"
ffmpeg -i "${file}" -c:v h264_vaapi -c:a copy "${file}-x264_aac.mp4"

Gives error

Impossible to convert between the formats supported by the filter 'Parsed_null_0' and the filter 'auto_scaler_0'

Solution: add -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device options (see below)


Command

ffmpeg -i "${file}" -c:v h264_v4l2m2m -b:v 8M -c:a copy "${file}-aac_x264.mp4"

Gives error

[h264_v4l2m2m @ 0x5556d0714140] Could not find a valid device
[h264_v4l2m2m @ 0x5556d0714140] can't configure encoder
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

Solution: don’t use h264_v4l2m2m


Command

ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 -i "${file}" -codec:v h264_vaapi  "${file}-x264_aac.mp4"

Gives error

[AVHWDeviceContext @ 0x5598a5b91800] No VA display found for device /dev/dri/renderD128.
Device creation failed: -22.
Failed to set value '/dev/dri/renderD128' for option 'vaapi_device': Invalid argument

Solution: use sudo


Command:

./download/FFmpeg-n4.3.2/ffmpeg  -loglevel debug -init_hw_device qsv=hw     \
   -filter_hw_device hw -f rawvideo -pix_fmt  \
   yuv420p -s:v 176x144 -i "${file}" -vf     \
   hwupload=extra_hw_frames=64,format=qsv     \
   -c:v h264_qsv -b:v 5M -frames:v 10         \
   -y "${file}"-aac.mp4

Gives error

Device creation failed: -542398533.
Failed to set value 'qsv=hw' for option 'init_hw_device': Generic error in an external library

Solution: use sudo

Sources

#Server #Ubuntu