blakeblackshear / frigate

NVR with realtime local object detection for IP cameras
https://frigate.video
MIT License
18.93k stars 1.73k forks source link

AMD Ryzen hardware acceleration not supported #311

Closed rb2k closed 3 years ago

rb2k commented 3 years ago

Describe the bug A lot of the AMD Ryzen APUs have an integrated GPU that can be used to accelerate video.

Usually I would have expected to use this env far: LIBVA_DRIVER_NAME=radeonsi

But it turns out, the container doesn't have it which causes ffmpeg to crash:

root@edd635788772:/opt/frigate# ls /usr/lib/x86_64-linux-gnu/dri/
i965_drv_video.so
iHD_drv_video.so

On my host system you can see the radeonsi drivers:

$ ls -lash /usr/lib64/dri/
total 243M
   0 drwxr-xr-x 1 root root  424 Nov 22 19:23 .
   0 drwxr-xr-x 1 root root  37K Nov 24 13:14 ..
 14M -rwxr-xr-x 4 root root  14M Nov 12 14:07 i915_dri.so
 14M -rwxr-xr-x 4 root root  14M Nov 12 14:07 i965_dri.so
 21M -rwxr-xr-x 8 root root  21M Nov 12 14:07 iris_dri.so
 21M -rwxr-xr-x 8 root root  21M Nov 12 14:07 kms_swrast_dri.so
9.0M -rwxr-xr-x 3 root root 8.9M Nov 12 14:07 nouveau_drv_video.so
 14M -rwxr-xr-x 4 root root  14M Nov 12 14:07 r200_dri.so
 21M -rwxr-xr-x 8 root root  21M Nov 12 14:07 r300_dri.so
 21M -rwxr-xr-x 8 root root  21M Nov 12 14:07 r600_dri.so
9.0M -rwxr-xr-x 3 root root 8.9M Nov 12 14:07 r600_drv_video.so
 14M -rwxr-xr-x 4 root root  14M Nov 12 14:07 radeon_dri.so
 21M -rwxr-xr-x 8 root root  21M Nov 12 14:07 radeonsi_dri.so
9.0M -rwxr-xr-x 3 root root 8.9M Nov 12 14:07 radeonsi_drv_video.so
 21M -rwxr-xr-x 8 root root  21M Nov 12 14:07 swrast_dri.so
 21M -rwxr-xr-x 8 root root  21M Nov 12 14:07 virtio_gpu_dri.so
 21M -rwxr-xr-x 8 root root  21M Nov 12 14:07 vmwgfx_dri.so

This is where they come from:

$ rpm -q --whatprovides /usr/lib64/dri/radeon*
Mesa-dri-20.2.2-266.1.x86_64
Mesa-dri-20.2.2-266.1.x86_64
Mesa-libva-20.2.2-266.1.x86_64

I can successfully encode:

ffmpeg -i input.mp4 -vaapi_device /dev/dri/renderD128 -vcodec h264_vaapi -vf format='nv12|vaapi,hwupload' output.mp4

or decode:

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -i rtsp://192.168.30.176/live0 derp.mp4

I forwarded /dev/dri/ to the container.

Version of frigate latest docker

Config file Include your full config file wrapped in triple back ticks.

  hwaccel_args:
    - -hwaccel
    - vaapi
    - -hwaccel_device
    - /dev/dri/renderD128

Computer Hardware

rb2k commented 3 years ago

I connected to the container and ran:

apt update
apt install mesa-va-drivers

and it seems to have stopped crashing and is working. Not 100% sure how I can verify that it is indeed using hardware acceleration though :)

rb2k commented 3 years ago

Not the best picture, but it seems to be working:

Screen Shot 2020-11-24 at 3 30 53 PM
blakeblackshear commented 3 years ago

So installing that driver and setting the env var is all you need? Should be easy enough to add the driver to the build.

rb2k commented 3 years ago

Yes, that is all I had to do (aside from the yaml config entry)

I can 'fix' the container by running

apt update; apt install -y mesa-va-drivers

Having it in the build would be great!

rb2k commented 3 years ago

This works great now btw. Closing the issue.

y2kdread commented 1 year ago

Would you be willing to share your config.yml for this setup? I am working on setting it up and not finding much documentation on hardware acceleration with Ryzen CPUs with integrated GPU.

rb2k commented 1 year ago

There's not a lot that you have to do:

  1. Set ffmpeg

    ffmpeg:
    hwaccel_args: preset-vaapi
  2. Make sure your docker container has device /dev/dri/renderD128 forwarded (and that it exists on your system)

  3. Set LIBVA_DRIVER_NAME environment variable for your docker to 'radeonsi'

NickM-27 commented 1 year ago

Would you be willing to share your config.yml for this setup? I am working on setting it up and not finding much documentation on hardware acceleration with Ryzen CPUs with integrated GPU.

it's all right in the documentation https://docs.frigate.video/configuration/hardware_acceleration#amdati-gpus-radeon-hd-2000-and-newer-gpus-via-libva-mesa-driver

y2kdread commented 1 year ago

I feel like I am missing something obvious, but this is what I have in my configs:

config.yml

    devices:
      - /dev/bus/usb:/dev/bus/usb
      - /dev/dri/renderD128:/dev/dri/renderD128
    environment:
      LIBVA_DRIVER_NAME: radeonsi

docker_compose

cameras:
  FrontEast:
    ffmpeg:
      hwaccel_args: preset-vaapi

errors:

[2023-04-24 14:34:30] ffmpeg.FrontEast.detect        ERROR   : [NULL @ 0x561b63a29c80] Unable to find a suitable output format for 'preset-vaapi'
[2023-04-24 14:34:30] ffmpeg.FrontEast.detect        ERROR   : preset-vaapi: Invalid argument
[2023-04-24 14:34:33] frigate.video                  ERROR   : FrontEast: Unable to read frames from ffmpeg process.
[2023-04-24 14:34:33] frigate.video                  ERROR   : FrontEast: ffmpeg process is not running. exiting capture thread...
[2023-04-24 14:34:40] watchdog.FrontEast             ERROR   : Ffmpeg process crashed unexpectedly for FrontEast.
[2023-04-24 14:34:40] watchdog.FrontEast             ERROR   : The following ffmpeg logs include the last 100 lines prior to exit.

I confirmed that /dev/dri/renderD128 does exist on my server and mesa-va-drivers is installed.

NickM-27 commented 1 year ago

@y2kdread looks like you are not running frigate 0.12, you are probably on 0.11.

y2kdread commented 1 year ago

@NickM-27 you are right, I was actually on 0.10. I was pulling from the blakeblackshear/frigate:stable-amd64.

Thanks for the guidance!

juagicre commented 7 months ago

2. Make sure your docker container has device /dev/dri/renderD128 forwarded (and that it exists on your system)

I'm using a Synology DS723+ as host for my Frigate it has a CPU AMD Ryzen R1600 and I cannot see any dri folder under dev so probably I need to admit that I don't have GPU :( On the other hand at the AMD web page announces those CPUs with 3 Vega Graphics CU's and 4K encode/decode. Not sure if in my case I should/could do anything.

I'm using also the google Coral, but I guess the GPU is still used/needed at some point, right?

stefankleff commented 7 months ago

@juagicre Same here. If you've found a solution please let me know

GalGavu commented 6 months ago

@juagicre @stefankleff did you found out any solution for hwaccel_args in DS723+?

rb2k commented 6 months ago

The R1600 in newer synology models does not have a GPU.

There are different versions of the R1600 series that do, e.g. https://www.amd.com/en/product/8426 , but not the one Synology used

GalGavu commented 6 months ago

@rb2k does it mean that there is no way for hwaccel_args on DS723+ and better not use DS723+ at all? (Obviously I have one and do not want to change :/)

rb2k commented 6 months ago

Yes, there is no hardware that would accelerate the video decoding/encoding from what I understand. You might still be able to get a few streams going, but it’s a pretty slow CPU, so don’t expect miracles

juagicre commented 6 months ago

For now i'm using no argument for that, true. Anyway for now with Coral and 1 camera it does not have any CPU peak. What I read from this CPU is that it has integrated some sort of GPU, but, probably a weak one, but it's a multimedia server, so I believe is not that weak... and also for now i got no bottlenecks, and I expect to setup some more cameras, so if I remember I can set here my impressions.

GalGavu commented 6 months ago

I've done the same and it's working pretty good. The problem I had originally was that I was using frigate as addon in HA with Virtual Machine and the interference speed was really high. I changed frigate to be container and interference speed is now reasonable at 30ms.