Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardware acceleration with FFmpeg for RK3328 #224

Open
gusarg81 opened this issue Aug 28, 2020 · 15 comments
Open

Hardware acceleration with FFmpeg for RK3328 #224

gusarg81 opened this issue Aug 28, 2020 · 15 comments

Comments

@gusarg81
Copy link

gusarg81 commented Aug 28, 2020

Hi,

Is amazing that being there for a quite time that is no possible out of the box, easily, to have hardware acceleration with FFmpeg, Which is a complete, cross-platform solution to record, convert and stream audio and video so popular within these days, used in so many apps.

That being said, I have Rock64 SBC (with RK3328 CPU), that I want only to stream 3 RTSP cams using FFmpeg (currently running Armbian Focal with legacy kernel, 4.4.213).
What is important is that one of those cams is to monitor my baby that needs 24hs supervision because his health condition and these days we need to use what we have because nothing is cheap (and event less, here in Argentina).

So, there many supposed howto's but everyone is far for being complete steps to make it work. What I need is all the steps, libs from rockchip repos (and others if needed) that needs to be compiled (all its necessary compilation flags ) to make hardware acceleration to work with FFmpeg (and besides --enable-rkmpp flag, if something else needed here).

So far what I know what is necessary is (besides the supported kernels, 4.4.x and 4.19.x) and correct me is something missing or that is not necessary from this list:

Again, remember that this is to make FFmpeg hardware acceleration to work (and I know, if I am not wrong, that rockchip only support decoding though FFmpeg and encoding must be done though gstreamer).

I know this is not the best place to report this, but I think there is no other that comes from Rockchip.

Thanks in advance.

@gusarg81
Copy link
Author

Still no reply to this?...

@andir
Copy link

andir commented Nov 18, 2020

I am in a similar boat as you. I'm using a RockPi 4B and for all I care I'd like to play h246 content with hardware acceleration. Recently I stumbled upon the v4l driver in mainline kernels staging section and https://github.com/Kwiboo/FFmpeg/commits/v4l2-request-hwaccel-4.2.2 from @Kwiboo. I am not yet entire sure what combination of custom tools I'll need to test this or get this going.

I hope all I need is the v4l user space API, a recent enough kernel (currently running 5.10-rc2) and maybe a patched ffmpeg (until upstream accepts those patches).

@gusarg81
Copy link
Author

gusarg81 commented Dec 1, 2020

Is a very badly documented of how to achieve this in different scenarios. And, if you made it possible, does not work right. For example, I made possible to use decoder with FFmpeg, and the decoded videos look so ugly, with so many artifacts... For now I give up till I have extra time to report this problem (yeah, because you don't even have a single reply).

@bdherouville
Copy link

Hi,

There is some dicussion about Rockchip acceleration here : blakeblackshear/frigate#1814

Hope we will be able to get the max performance of our boards.

Cheers,

@gusarg81
Copy link
Author

@bdherouville Hi!

Nice, I will take a look at it. Also, I've never heard about that NVR (Frigate). I use Shinobi (and in the past Motion/MotionEye).

I have my Rock64 stored around there just because I can't get hardware accel to work right (encoding/decoding). Because also I want to start a project, a IP Doorbell (with face recognition and many more) and the only harware to test (small) is this one.

In the time that we get Rockchip to work with Hwaccel, will become obsolete (which, kind the one from Rock64 already is).

@Maigre
Copy link

Maigre commented Oct 9, 2021

Ahah i am exactly in the same boat (but with RK3399)... hard to tell how to properly configure everything.. But following the issue !

@gusarg81
Copy link
Author

gusarg81 commented Oct 9, 2021

@Maigre read the long post from there blakeblackshear/frigate#1814 (comment). I am following those instructions.

scpcom pushed a commit to scpcom/linux that referenced this issue Nov 1, 2021
[ Upstream commit b36b242 ]

The userspace program could pass any values to the driver through
ioctl() interface. If the driver doesn't check the value of 'pixclock',
it may cause divide error.

Fix this by checking whether 'pixclock' is zero first.

The following log reveals it:

[   43.861711] divide error: 0000 [#1] PREEMPT SMP KASAN PTI
[   43.861737] CPU: 2 PID: 11764 Comm: i740 Not tainted 5.14.0-rc2-00513-gac532c9bbcfb-dirty rockchip-linux#224
[   43.861756] RIP: 0010:asiliantfb_check_var+0x4e/0x730
[   43.861843] Call Trace:
[   43.861848]  ? asiliantfb_remove+0x190/0x190
[   43.861858]  fb_set_var+0x2e4/0xeb0
[   43.861866]  ? fb_blank+0x1a0/0x1a0
[   43.861873]  ? lock_acquire+0x1ef/0x530
[   43.861884]  ? lock_release+0x810/0x810
[   43.861892]  ? lock_is_held_type+0x100/0x140
[   43.861903]  ? ___might_sleep+0x1ee/0x2d0
[   43.861914]  ? __mutex_lock+0x620/0x1190
[   43.861921]  ? do_fb_ioctl+0x313/0x700
[   43.861929]  ? mutex_lock_io_nested+0xfa0/0xfa0
[   43.861936]  ? __this_cpu_preempt_check+0x1d/0x30
[   43.861944]  ? _raw_spin_unlock_irqrestore+0x46/0x60
[   43.861952]  ? lockdep_hardirqs_on+0x59/0x100
[   43.861959]  ? _raw_spin_unlock_irqrestore+0x46/0x60
[   43.861967]  ? trace_hardirqs_on+0x6a/0x1c0
[   43.861978]  do_fb_ioctl+0x31e/0x700

Signed-off-by: Zheyu Ma <[email protected]>
Signed-off-by: Sam Ravnborg <[email protected]>
Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
Signed-off-by: Sasha Levin <[email protected]>
@gusarg81
Copy link
Author

gusarg81 commented Nov 2, 2021

Well, anyways seems the path is not rkmpp anymore, since that is for legacy kernels. New kernels 5.13 and newer is via v4l2-request and libdrm. But this is too green yet, still did not figure it out how to make it work correctly, there are to many forks of FFmpeg, not a single form of instruction of how to make it work, etc etc etc.

We have to wait, but in the meanwhile this board is getting old and old.

@avafinger
Copy link

@gusarg81
If you just want to stream RTSP, take a look at: https://github.com/avafinger/nanopi-r4s-minimal-image#h264-camera
The original code is for H264 since HTML5 does not support H265. This will work on any platform.

@gusarg81
Copy link
Author

gusarg81 commented Nov 2, 2021

@avafinger Hi,

Thanks, I will take a look (specially for videostreamer). By the way, seems decoding is working now (started from scratch with Armbian and following again the post from blakeblackshear/frigate#1814

Since that is solved, now comes the encoding part. My original idea is to use rtsp-simple-server (https://github.com/aler9/rtsp-simple-server), and for that I need to use encoding. So, is there a way to achieve hwaccel encoding?

@avafinger
Copy link

avafinger commented Nov 2, 2021

Encoding works on legacy kernel (GStreamer), not with FFmpeg. You need rockchip mpp, gstreamer, rga.

videostreamer is nice but no sound. If you want video + audio you need to make some changes to add the audio and change MP4 to MKV.

Regarding RTSP from cams, usually, you can get video + audio with VLC / FFPLAY from your host PC, or if you figured out how to decode, add the latest SDL2 and you can work with ffplay.
See the commands here: https://github.com/avafinger/onvif_ip_camera_dome
And if you stick to the legacy kernel you need to comment the line: libsdl-org/SDL#4879 (comment)

@avafinger
Copy link

@gusarg81
And if you want to stream video from a USB cam (MJPEG) to achieve 30 FPS, use mjpg-streamer and make sure your sensor delivers MJPEG, if it delivers JPEG you need a fix like this one:
avafinger/bananapi-zero-ubuntu-base-minimal#56 (comment)

@gusarg81
Copy link
Author

gusarg81 commented Nov 2, 2021

The USB UVC camera I use, does support MJPEG and YUYV:

v4l2-ctl --list-formats -d /dev/video1
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

    [0]: 'MJPG' (Motion-JPEG, compressed)
    [1]: 'YUYV' (YUYV 4:2:2)

And the idea, as mentioned once, is to use MJPEG because is the format that supports 30FPS in FHD resolution. So, using this format to stream as RTSP at first and also as HLS format (this last one, to make it easier to capture it via web in a platform that I will develop later). Thats why the idea was rtsp-simple-server.

EDIT: just to mention, the idea is not to stick with legacy kernel because there is no progress at all, and things works read bad; I've tested it many times, no even a decent hw decoding.

@avafinger
Copy link

RTSP:
Audio Codecs: AAC, AAC-LC, HE-AAC+ v1 & v2, MP3, Speex, Opus, Vorbis
Video Codecs: H.265 (preview), H.264, VP9, VP8

Read carefully what I have written, I am sure it will save you time.

I don't know the state of the HW encoder in the mainline.

@gusarg81
Copy link
Author

gusarg81 commented Nov 3, 2021

mjpg-streamer works nice by the way, and very low CPU usage.

Seems that will do for now (event http output is quite nice, even stream delay is minimal).

Now i must think for the sound part. This USB camera I use, which is a Arducam https://www.arducam.com/product-category/uvc-usb-camera-module/usb-uvc-cameras-night-vision/, does not have sound, so my idea is to add a USB Mic.

And, I way to mix the stream (mjpeg-streamer) and the audio input.

scpcom pushed a commit to scpcom/linux that referenced this issue Jul 23, 2022
[ Upstream commit ebe41da ]

Use after free is detected by kfence when disabling sriov. What was read
after being freed was vf->pci_dev: it was freed from pci_disable_sriov
and later read in efx_ef10_sriov_free_vf_vports, called from
efx_ef10_sriov_free_vf_vswitching.

Set the pointer to NULL at release time to not trying to read it later.

Reproducer and dmesg log (note that kfence doesn't detect it every time):
$ echo 1 > /sys/class/net/enp65s0f0np0/device/sriov_numvfs
$ echo 0 > /sys/class/net/enp65s0f0np0/device/sriov_numvfs

 BUG: KFENCE: use-after-free read in efx_ef10_sriov_free_vf_vswitching+0x82/0x170 [sfc]

 Use-after-free read at 0x00000000ff3c1ba5 (in kfence-rockchip-linux#224):
  efx_ef10_sriov_free_vf_vswitching+0x82/0x170 [sfc]
  efx_ef10_pci_sriov_disable+0x38/0x70 [sfc]
  efx_pci_sriov_configure+0x24/0x40 [sfc]
  sriov_numvfs_store+0xfe/0x140
  kernfs_fop_write_iter+0x11c/0x1b0
  new_sync_write+0x11f/0x1b0
  vfs_write+0x1eb/0x280
  ksys_write+0x5f/0xe0
  do_syscall_64+0x5c/0x80
  entry_SYSCALL_64_after_hwframe+0x44/0xae

 kfence-rockchip-linux#224: 0x00000000edb8ef95-0x00000000671f5ce1, size=2792, cache=kmalloc-4k

 allocated by task 6771 on cpu 10 at 3137.860196s:
  pci_alloc_dev+0x21/0x60
  pci_iov_add_virtfn+0x2a2/0x320
  sriov_enable+0x212/0x3e0
  efx_ef10_sriov_configure+0x67/0x80 [sfc]
  efx_pci_sriov_configure+0x24/0x40 [sfc]
  sriov_numvfs_store+0xba/0x140
  kernfs_fop_write_iter+0x11c/0x1b0
  new_sync_write+0x11f/0x1b0
  vfs_write+0x1eb/0x280
  ksys_write+0x5f/0xe0
  do_syscall_64+0x5c/0x80
  entry_SYSCALL_64_after_hwframe+0x44/0xae

 freed by task 6771 on cpu 12 at 3170.991309s:
  device_release+0x34/0x90
  kobject_cleanup+0x3a/0x130
  pci_iov_remove_virtfn+0xd9/0x120
  sriov_disable+0x30/0xe0
  efx_ef10_pci_sriov_disable+0x57/0x70 [sfc]
  efx_pci_sriov_configure+0x24/0x40 [sfc]
  sriov_numvfs_store+0xfe/0x140
  kernfs_fop_write_iter+0x11c/0x1b0
  new_sync_write+0x11f/0x1b0
  vfs_write+0x1eb/0x280
  ksys_write+0x5f/0xe0
  do_syscall_64+0x5c/0x80
  entry_SYSCALL_64_after_hwframe+0x44/0xae

Fixes: 3c5eb87 ("sfc: create vports for VFs and assign random MAC addresses")
Reported-by: Yanghang Liu <[email protected]>
Signed-off-by: Íñigo Huguet <[email protected]>
Acked-by: Martin Habets <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jakub Kicinski <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
scpcom pushed a commit to scpcom/linux that referenced this issue Oct 20, 2022
[ Upstream commit b36b242 ]

The userspace program could pass any values to the driver through
ioctl() interface. If the driver doesn't check the value of 'pixclock',
it may cause divide error.

Fix this by checking whether 'pixclock' is zero first.

The following log reveals it:

[   43.861711] divide error: 0000 [#1] PREEMPT SMP KASAN PTI
[   43.861737] CPU: 2 PID: 11764 Comm: i740 Not tainted 5.14.0-rc2-00513-gac532c9bbcfb-dirty rockchip-linux#224
[   43.861756] RIP: 0010:asiliantfb_check_var+0x4e/0x730
[   43.861843] Call Trace:
[   43.861848]  ? asiliantfb_remove+0x190/0x190
[   43.861858]  fb_set_var+0x2e4/0xeb0
[   43.861866]  ? fb_blank+0x1a0/0x1a0
[   43.861873]  ? lock_acquire+0x1ef/0x530
[   43.861884]  ? lock_release+0x810/0x810
[   43.861892]  ? lock_is_held_type+0x100/0x140
[   43.861903]  ? ___might_sleep+0x1ee/0x2d0
[   43.861914]  ? __mutex_lock+0x620/0x1190
[   43.861921]  ? do_fb_ioctl+0x313/0x700
[   43.861929]  ? mutex_lock_io_nested+0xfa0/0xfa0
[   43.861936]  ? __this_cpu_preempt_check+0x1d/0x30
[   43.861944]  ? _raw_spin_unlock_irqrestore+0x46/0x60
[   43.861952]  ? lockdep_hardirqs_on+0x59/0x100
[   43.861959]  ? _raw_spin_unlock_irqrestore+0x46/0x60
[   43.861967]  ? trace_hardirqs_on+0x6a/0x1c0
[   43.861978]  do_fb_ioctl+0x31e/0x700

Signed-off-by: Zheyu Ma <[email protected]>
Signed-off-by: Sam Ravnborg <[email protected]>
Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
Signed-off-by: Sasha Levin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants