Notes on building ffmpeg and mpv to use the hardware H.264 decoder

Edit: Removed a patch that is now included in the repo, and added a checkout command to select the right branch.

As I have fiddled with this in the last days and finally had some success, I am writing down some notes here in case others want to reproduce hardware decoded H.264 playback on MNT Reform / i.MX8MQ with mpv:

git clone https://github.com/martinetd/FFmpeg
cd FFmpeg
git checkout v4l2-request
./configure --enable-v4l2-request --enable-libdrm --enable-libudev --enable-shared --enable-hwaccel=h264_v4l2request

Before you can successfully build, a few changes have to be made. First, make sure that the line declaring EXTRALIBS-avcodec in ffbuild/config.mak includes -ludev.

Then, you need to have some newer video-related Linux UAPI headers than Debian ships. I did the following brutal things:

wget https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/include/uapi/linux/v4l2-controls.h
sudo cp v4l2-controls.h /usr/include/linux/

And added the following line after line 736 in /usr/include/linux/videodev2.h:

#define V4L2_PIX_FMT_H264_SLICE v4l2_fourcc('S', '2', '6', '4') /* H264 parsed slices */

Afterwards, you can build ffmpeg and install it to /usr/local:

make -j4
sudo make install
ldconfig /usr/local/lib

Then, clone and build mpv:

git clone https://github.com/mpv-player/mpv
cd mpv
./bootstrap.py
./waf configure
./waf
./waf install

And play a video like this:

mpv --hwdec=auto ~/Videos/video.mp4

Check if the HW decoder was in use:

cat /proc/interrupts

The following line should have a positive number in the second column:

 58:       2983          0          0          0     GPCv2   7 Level     38300000.video-codec
1 Like

You can also use this with streamlink if you create a config file for mpv (~/.config/mpv/mpv.conf) and put the line hwdec=auto in there.

A problem is that 1080p60 does not work properly (frame drops). I believe this is because we are not getting zero copies (rendering directly to a GPU plane), but instead going through a texture.

1 Like

works nicely for me :slight_smile:
and even gamestreaming with moonlight works just awesome in 1080p/60fps after installing the ffmpeg upgrade! recompling moonlight is a must… just checkout from github and following compile guide (i used the embedded variant).

1 Like

Pretty cool, thanks! Reading this raised two questions for me:

  1. I’m more familiar with vaapi from Intel-land. How does v2l2-request fit into the whole accelerated codec puzzle?

  2. Does the i.MX8MQ support encoding as well?

There is no hardware video encoder, but two decoders (Hantro G1 for H.264/VP8 and G2 for H.265/VP9).

I’m not familiar enough with the Linux V4L2 zoo to explain the pros and cons of the v4l2-request API. I know there’s a difference between stateful and stateless codecs, maybe that helps.

Here’s a new thing from the PinePhone camp that’s worth a try if someone has time: Hardware Accelerated Video Decoding on the PinePhone with Clapper | Brian Daniels

Edit: Some technical info: Hardware acceleration · Rafostar/clapper Wiki · GitHub