kern2011 / Freenas-Quicksync

How to guide for getting Intel Quicksync working on Freenas
72 stars 7 forks source link

FreeNAS-Quicksync

How to guide for getting Intel Quicksync working on FreeNAS

Plex states that using hardware transcode on FreeBSD requires FreeBSD 12.0 or later. After some testing, it turns out it will work in FreeBSD 11.3 as well, although devfs is not as stable as in FreeBSD 12.x.

Note that for good results, you will need Intel gen 4 (Haswell) or later for 1080p transcode, and Intel gen 7 (Kaby Lake) or later for 4k transcode.

Test Results

Hardware requirements Intel Xeon / Intel server C2xx chipsets

echo '[devfsrules_bpfjail=101] add path 'bpf*' unhide

[plex_drm=10] add include $devfsrules_hide_all add include $devfsrules_unhide_basic add include $devfsrules_unhide_login add include $devfsrules_jail add include $devfsrules_bpfjail add path 'dri' unhide add path 'dri/' unhide add path 'drm' unhide add path 'drm/' unhide' >> /etc/devfs.rules

service devfs restart

kldload /boot/modules/i915kms.ko

- Make the script executable

`chmod +x /root/scripts/plex-ruleset.sh`

- Execute the script

`/root/scripts/plex-ruleset.sh`

##### Now we need to make this happen on boot

- In the UI go to Tasks>Init/Shutdown Scripts>Add

Type: Script Script: /root/scripts/plex-ruleset.sh When: Post Init



##### From the Plex jail console:

- add plex to the “video” group

`pw groupmod -n video -m plex`

##### Now we have to edit some jail settings. This can be done in the UI.

- Stop the jail. Use the arrow on the jail to expand the options, then click edit.

- Expand the “Jail Properties” header
Change `devfs_ruleset` to `10`

- Save your settings

- Start the Jail

## Testing

##### From the Jail console:

`ls /dev/d*`

If everything worked; you should be able to find “dri” and “drm” folders in the output.

- Now go into your Plex settings, go to the “Transcoder” tab and make sure “Use hardware acceleration when available” is ticked.

- Play back a video at a resolution or bitrate that is different than the source file. 

- From your Plex dashboard you should see transcode with `(hw)` at the end. If you do then congratulations, it worked!

##### Troubleshooting

- `/dev/dri` needs to be visible in FreeBSD itself. If it isn't there, you can't make it visible in the jail. Check that your hardware is supported by your version of FreeNAS/TrueNAS, and that the UEFI/BIOS is set to enable the iGPU. You can use the `lspci` command from FreeNAS console to show the hardware detected by FreeBSD. If this does not show human-readable output, try `lspci -q`. You are looking for a Display Controller entry for Intel, for example for Kaby Lake Xeon: "Display controller: Intel Corporation HD Graphics P630 (rev 04)". If that is missing, either your CPU doesn't have an iGPU, your chipset does not support it, the BIOS doesn't support it, or it's not enabled in BIOS.
- There is a known issue with FreeBSD 11.3 where restarting a jail may cause it to lose access to devfs and /dev/dri may no longer be accessible in the jail. There are two known workarounds. One, create a dummy jail that also uses ruleset 10 and have it autostart. That way, there is always a jail that uses the ruleset, and restarting the Plex jail won't trigger the bug. Two, you could "iocage stop plex; service devfs restart; iocage start plex". In testing, this worked, but eventually crashed FreeNAS. Some care needed. TrueNAS 12.0 Core has been tested to not exhibit the issue, and no workarounds are needed
- The workarounds below for Plex 1.17.0 are *not* needed in Plex 1.18.8 or later.
- As of Plex 1.19.5, there is a [bug in Intel drivers](https://github.com/intel/media-driver/issues/752) that can cause "blocky" video in high-contrast VC1.
- There is a bug that causes "blocky" video on Gemini Lake (J4xx5/J5xx5 Celeron/Pentium) iGPUs.
- There is a bug present in Plex 1.19 and 1.20 that can stop hardware transcode from starting when using PGS/VOBSUB subtitles, TrueNAS Core 12.0, and a Kaby Lake CPU.

##### Links
- Intel QuickSync versions and their capabilities can be found here: https://en.wikipedia.org/wiki/Intel_Quick_Sync_Video
- Intel iGPUs supported by FreeBSD version: https://wiki.freebsd.org/Graphics/Intel-GPU-Matrix 
- Intel iGPUs supported by Linux kernel version: https://github.com/Intel-Media-SDK/MediaSDK/wiki/Intel-Graphics-Support-in-Linux-Kernels

## Notes
- 11/29/19- Plex Version 1.17.0.1709 was broken and disabled hardware transcode. If that version won't work for you there is a work around. Note this is *not* required for Plex 1.18.8 or later.

In your Plex jail backup:

`/usr/local/share/plexmediaserver-plexpass/lib/libva-drm.so.2`

`/usr/local/share/plexmediaserver-plexpass/lib/libdrm.so.2`

`/usr/local/share/plexmediaserver-plexpass/lib/libva.so.2`

Then replace the files with good versions by running (From your Plex jail):

`cp -a /usr/local/lib/libva-drm.so.2.500.0 /usr/local/share/plexmediaserver-plexpass/lib/libva-drm.so.2`

`cp -a /usr/local/lib/libdrm.so.2.4.0 /usr/local/share/plexmediaserver-plexpass/lib/libdrm.so.2`

`cp -a /usr/local/lib/libva.so.2.500.0 /usr/local/share/plexmediaserver-plexpass/lib/libva.so.2`

Also copy your driver to Plex

`cp -a /usr/local/lib/dri/i965_drv_video.so /usr/local/share/plexmediaserver-plexpass/lib/dri/`

- 11/29/19- In testing, after rebooting Freenas the Plex Jail could not see “dri” and “drm”, but after restarting the jail everything worked fine. This may have been related to the order of operations in the script, or to devfs in FreeBSD 11.3. This behavior has not been observed again after the 3/16/2020 changes.

- 12/1/19- Fixed a mistake in the "Testing" section.

- 26/2/19- Added compatibility (or lack thereof) notice about Gen 8 and Gen 9 support

- 3/16/20 - Added devfs restart and a few more notes about GPU and CPU generations. Added a note about Plex 1.18.8. Moved kldload to bottom of script because of issues encountered (no FreeNAS UI) when it was at the top.

- 3/29/20 - Added a troubleshooting section, clarified CPU generations, added compatible SM motherboards, made headings that explain which shell to be in more visible, changed ruleset to 10 to help work around a FreeBSD 11.3 issue.

- 4/8/20 - Added a note about HEVC and iGPU generations; added some more Xeon chipsets

- 4/15/20 - Removed Intel media driver and allow_mount_devfs notes, as it turns out neither of those is required for hw transcode to work

- 4/26/20 - Calling out codec explicitly for test results; some language cleanup; added a note about hardware acceleration disabling KVM in IPMI. Added a few more explicit instructions for people who are not at home on CLI.

- 6/22/20 - Added note about incompatible Dell models; mention scripts that support hardware transcode; added to troubleshooting section.

- 9/03/20 - Additional compatibility notes and links