Add vhost-user Video decode
v2 cover Add vhost-user Video decode
v1: https://patchew.org/QEMU/[email protected]/
v1 -> v2:
- Address the feedback found in the original patch
- Rebase patch so that it applies to latest master
- Add PoC implementation for dmabuf sharing (using udmabuf for creation)
- Test the patch on a real HW codec
This series adds support for virtio-video decoder devices in Qemu and also provides a vhost-user-video vmm implementation.
The vhost-user-video vmm currently parses virtio-video v3 protocol (as that still was what the Linux frontend driver prototype implemented at the timei, we kept working over this).
It then converts that to a v4l2 mem2mem stateful decoder device. Currently, this has been tested using v4l2 vicodec test driver in Linux [1], and also with a real HW codec, i.e., QCom Venus driver, which implements a v4l2 stateful decoder/encoder. In order to support vicodec driver, the virtio backend supports the FWHT format.
The primary goal so far has been to allow testing with Qemu. Using vicodec on the host allows a purely virtual dev env, and for CI integration in the future by kernelci, etc.
This series adds the virtio_video.h header for testing purposes, but the driver code has not been upstreamed yet, as the specs are still under discussion.
Therefore, the commit does not need to be reviewed at this stage.
This version includes a proof of concept implementation of dma-buf sharing mechanism. Since the infrastructure is not yet available in Qemu, we employed udmabuf device to create new dma-buf structures when the driver asks for a shared object memory stream, and then we copy the contents of the shared memory buffer into the newly created buffer. Also, note that this PoC was necessary for making the patch work with QC Venus driver as it does not support USERPTR memory in its latest release. In any case, a thorough review of these two commits is not necessary in its current stage, as the implementation is subjected to big changes.
We tested this VMM using v4l2-ctl from v4l2 utils in the guest to do a video decode to a file. This can then be validated using ffplay v4l2-compliance tool in the guest has also been run which stresses the interface and issues lots of syscall level tests.
See the vhost-user-video.rst for example commands on how to configure guest kernel and do a video decode using Qemu, vicodec using this VMM.
Next steps:
Recently there was a big update for virtio-video specs being discussed in the virtio-dev mailing list [2], with a proposal for moving the specs closer to the v4l2 framework.
Following these news, the next step for this series is to update the implementation to the new specs approach and join efforts in verifying the driver implementation [3], and the specs themselves.
Linux virtio-video frontend driver code:
https://github.com/aesteve-rh/linux
(adds FWHT support on top of https://github.com/Gnurou/linux)
Qemu vmm code:
https://github.com/aesteve-rh/qemu/tree/virtio_video_v3
Applies cleanly to:git://git.qemu.org/qemu.git master(c283ff89d11ff123efc9af49128ef58511f73012)
[1] https://lwn.net/Articles/760650/
[2] https://www.mail-archive.com/[email protected]/msg09126.html
[3] https://github.com/Gnurou/linux/blob/virtio-v4l2/drivers/media/virtio-v4l2/virtio_v4l2_driver.c
Albert Esteve (5):
vhost-user.json: add video type
tests/qtest: add virtio-video test
vhost-user-video: add dev_type to CLI
vhost-user-video-udmabuf: add udmabuf helpers
Add support for v4l2_memory_dmabuf
Peter Griffin (7):
docs: Add a vhost-virtio-video rst file
MAINTAINERS: Add virtio-video section
vhost-user-video: boiler plate code for vhost-user-video device
vhost-user-video: add meson subdir build logic
standard-headers: Add virtio_video.h
hw/display: add vhost-user-video-pci tools/vhost-user-video: Add initial vhost-user-video vmm
MAINTAINERS | 8 + docs/interop/vhost-user.json | 2 + docs/system/devices/vhost-user-video.rst | 124 ++ hw/display/Kconfig | 5 + hw/display/meson.build | 3 + hw/display/vhost-user-video-pci.c | 82 + hw/display/vhost-user-video.c | 419 ++++ include/hw/virtio/vhost-user-video.h | 43 + include/standard-headers/linux/virtio_video.h | 513 +++++ tests/qtest/libqos/meson.build | 1 + tests/qtest/libqos/virtio-video.c | 179 ++ tests/qtest/libqos/virtio-video.h | 39 + tests/qtest/vhost-user-test.c | 32 + tools/meson.build | 7 + tools/vhost-user-video/50-qemu-video.json.in | 5 + tools/vhost-user-video/meson.build | 10 + tools/vhost-user-video/v4l2_backend.c | 1838 +++++++++++++++++ tools/vhost-user-video/v4l2_backend.h | 111 + tools/vhost-user-video/vhost-user-video.c | 1787 ++++++++++++++++ tools/vhost-user-video/virtio_video_helpers.c | 466 +++++ tools/vhost-user-video/virtio_video_helpers.h | 198 ++ tools/vhost-user-video/virtio_video_udmabuf.c | 180 ++ tools/vhost-user-video/vuvideo.h | 51 + 23 files changed, 6103 insertions(+) create mode 100644 docs/system/devices/vhost-user-video.rst create mode 100644 hw/display/vhost-user-video-pci.c create mode 100644 hw/display/vhost-user-video.c create mode 100644 include/hw/virtio/vhost-user-video.h create mode 100644 include/standard-headers/linux/virtio_video.h create mode 100644 tests/qtest/libqos/virtio-video.c create mode 100644 tests/qtest/libqos/virtio-video.h create mode 100644 tools/vhost-user-video/50-qemu-video.json.in create mode 100644 tools/vhost-user-video/meson.build create mode 100644 tools/vhost-user-video/v4l2_backend.c create mode 100644 tools/vhost-user-video/v4l2_backend.h create mode 100644 tools/vhost-user-video/vhost-user-video.c create mode 100644 tools/vhost-user-video/virtio_video_helpers.c create mode 100644 tools/vhost-user-video/virtio_video_helpers.h create mode 100644 tools/vhost-user-video/virtio_video_udmabuf.c create mode 100644 tools/vhost-user-video/vuvideo.h
Signature · 1 line
2.39.2
PATCH v2 1/12 docs: Add a vhost-virtio-video rst file
From: Peter Griffin <[email protected]>
Add rst file with a brief description of the
daemon and a cheat sheet of commands.
--- docs/system/devices/vhost-user-video.rst | 124 +++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 docs/system/devices/vhost-user-video.rst
docs/system/devices/vhost-user-video.rst new file +124 -0
@@ -0,0 +1,124 @@ +===================== +QEMU vhost-user-video +===================== + +Overview +-------- + +This vmm translates from virtio-video v3 protocol and writes +to a v4l2 mem2mem stateful decoder/encoder device [1]. v3 was +chosen as that is what the virtio-video Linux frontend driver +currently implements. + +The primary goal so far is to enable development of virtio-video +frontend driver using purely open source software. Using vicodec +v4l2 stateful decoder on the host for testing then allows a pure +virtual environment for development and testing. + +Currently the vmm only supports v4l2 stateful devices, and the +intention is it will be used with Arm SoCs that implement stateful +decode/encode devices such as Qcom Venus, RPi, MediaTek etc. + +A Qemu + vicodec setup for virtio-video should also allow for +CI systems like kernelci, lkft to test the virtio-video interface +easily. + +Currently support for VAAPI or decoding via libavcodec or similar +libraries is not implemented, but this could be added in the future. + +Some example commands are provided below on how to run the daemon +and achieve a video decode using vicodec and a link to some test +content. + +[1] https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/dev-decoder.html + +[2] https://lwn.net/Articles/760650/ + +Examples +-------- + +Guest Linux kernel modules: + +:: + + CONFIG_MEDIA_SUPPORT=y + CONFIG_MEDIA_TEST_SUPPORT=y + CONFIG_V4L_TEST_DRIVERS=y + CONFIG_VIRTIO_VIDEO=y + CONFIG_GDB_SCRIPTS=y + CONFIG_DRM_VIRTIO_GPU=y + +Host kernel modules: + +:: + + CONFIG_MEDIA_SUPPORT=y + CONFIG_MEDIA_TEST_SUPPORT=y + CONFIG_V4L_TEST_DRIVERS=y + CONFIG_VIDEO_VICODEC=y + +Note: Vicodec has been recently included in the Fedora kernel releases, +but it is not yet set on the default Debian kernel. + +The daemon should be started first (video3 typically is the stateful video): + +:: + + host# vhost-user-video --socket-path=/tmp/video.sock --v4l2-device=/dev/video3 + +The QEMU invocation needs to create a chardev socket the device can +use to communicate as well as share the guests memory over a memfd. + +:: + + host# qemu-system \ + -device vhost-user-video-pci,chardev=video,id=video \ + -chardev socket,path=/tmp//video.sock,id=video \ + -m 4096 \ + -object memory-backend-file,id=mem,size=4G,mem-path=/dev/shm,share=on \ + -numa node,memdev=mem \ + ... + +After booting, the device should be available at /dev/video0: + +:: + + guest# v4l2-ctl -d/dev/video0 --info + Driver Info: + Driver name : virtio-video + Card type : + Bus info : virtio:stateful-decoder + Driver version : 6.1.0 + Capabilities : 0x84204000 + Video Memory-to-Memory Multiplanar + Streaming + Extended Pix Format + Device Capabilities + Device Caps : 0x04204000 + Video Memory-to-Memory Multiplanar + Streaming + Extended Pix Format + +Example v4l2-ctl decode command: + +:: + + guest# wget https://people.linaro.org/~peter.griffin/jelly_640_480-420P.fwht + guest# v4l2-ctl -d0 -x width=640,height=480 -v width=640,height=480,pixelformat=YU12 \ + --stream-mmap --stream-out-mmap --stream-from jelly_640_480-420P.fwht \ + --stream-to out-jelly-640-480.YU12 + +Play the raw decoded video with ffplay or mplayer + +:: + + guest# ffplay -loglevel warning -v info -f rawvideo -pixel_format yuv420p \ + -video_size "640x480" ./out-jelly-640-480.YU12 + guest# mplayer -demuxer rawvideo -rawvideo \ + format=i420:w=640:h=480:fps=25 out-jelly-640-480.YU12 + +Enable v4l2 debug in virtio-video driver + +:: + + # echo 0x1f > /sys/class/video4linux/videoX/dev_debug
Signature · 1 line
2.39.2
PATCH v2 2/12 MAINTAINERS: Add virtio-video section
In reply to Albert EsteveFrom: Peter Griffin <[email protected]>
Add myself as maintainer of the virtio-video files added in this series.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -2176,6 +2176,14 @@ F: hw/virtio/vhost-user-gpio* F: include/hw/virtio/vhost-user-gpio.h F: tests/qtest/libqos/virtio-gpio.* +virtio-video +M: Albert Esteve <[email protected]> +S: Supported +F: hw/display/vhost-user-video.c +F: hw/display/vhost-user-video-pci.c +F: include/hw/virtio/vhost-user-video.h +F: tools/vhost-user-video/* + virtio-crypto M: Gonglei <[email protected]> S: Supported
Signature · 1 line
2.39.2
Re: [PATCH 02/12] MAINTAINERS: Add virtio-video section
Quoted 16 lines · Albert Esteve
From: Peter Griffin <[email protected]>
Add myself as maintainer of the virtio-video files added in this series.
Signed-off-by Peter Griffin <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)MAINTAINERS +0
-0
MAINTAINERS F: hw/virtio/vhost-user-gpio* · line 2185
Quoted 10 lines
MAINTAINERS +7
-0@@ -2176,6 +2176,14 @@ F: hw/virtio/vhost-user-gpio* F: include/hw/virtio/vhost-user-gpio.h F: tests/qtest/libqos/virtio-gpio.* +virtio-video +M: Albert Esteve <[email protected]> +S: Supported +F: hw/display/vhost-user-video.c +F: hw/display/vhost-user-video-pci.c +F: include/hw/virtio/vhost-user-video.h +F: tools/vhost-user-video/*
Please add tests/qtest/libqos/virtio-video.* here, too.
Thomas
Re: [PATCH 02/12] MAINTAINERS: Add virtio-video section
Quoted 30 lines · Thomas Huth
Quoted 16 lines · Albert Esteve
From: Peter Griffin <[email protected]>
Add myself as maintainer of the virtio-video files added in this series.
Signed-off-by Peter Griffin <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)MAINTAINERS +0
-0MAINTAINERS F: hw/virtio/vhost-user-gpio* · line 2185
Quoted 10 lines
MAINTAINERS +7
-0@@ -2176,6 +2176,14 @@ F: hw/virtio/vhost-user-gpio* F: include/hw/virtio/vhost-user-gpio.h F: tests/qtest/libqos/virtio-gpio.* +virtio-video +M: Albert Esteve <[email protected]> +S: Supported +F: hw/display/vhost-user-video.c +F: hw/display/vhost-user-video-pci.c +F: include/hw/virtio/vhost-user-video.h +F: tools/vhost-user-video/*Please add tests/qtest/libqos/virtio-video.* here, too.
Quoted 4 lines
Thomas
Right. Will do for the next version, thanks!
PATCH v2 3/12 vhost-user-video: boiler plate code for vhost-user-video device
In reply to Albert EsteveFrom: Peter Griffin <[email protected]>
--- hw/display/Kconfig | 5 + hw/display/meson.build | 3 + hw/display/vhost-user-video.c | 410 +++++++++++++++++++++++++++ include/hw/virtio/vhost-user-video.h | 42 +++ 4 files changed, 460 insertions(+) create mode 100644 hw/display/vhost-user-video.c create mode 100644 include/hw/virtio/vhost-user-video.h
hw/display/Kconfig +5 -0
@@ -118,6 +118,11 @@ config VHOST_USER_VGA default y depends on VIRTIO_VGA && VHOST_USER_GPU +config VHOST_USER_VIDEO + bool + default y + depends on VIRTIO && VHOST_USER + config DPCD bool select AUX
hw/display/meson.build +3 -0
@@ -37,6 +37,9 @@ softmmu_ss.add(when: 'CONFIG_MACFB', if_true: files('macfb.c')) softmmu_ss.add(when: 'CONFIG_NEXTCUBE', if_true: files('next-fb.c')) specific_ss.add(when: 'CONFIG_VGA', if_true: files('vga.c')) +specific_ss.add(when: 'CONFIG_VHOST_USER_VIDEO', if_true: files('vhost-user-video.c')) +specific_ss.add(when: ['CONFIG_VHOST_USER_VIDEO', 'CONFIG_VIRTIO_PCI' ], + if_true: files('vhost-user-video-pci.c')) if (config_all_devices.has_key('CONFIG_VGA_CIRRUS') or config_all_devices.has_key('CONFIG_VGA_PCI') or
hw/display/vhost-user-video.c new file +410 -0
410 lines not on this page · raw message
include/hw/virtio/vhost-user-video.h new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v2 4/12 vhost-user-video: add meson subdir build logic
In reply to Albert EsteveFrom: Peter Griffin <[email protected]>
--- tools/meson.build | 7 +++++++ 1 file changed, 7 insertions(+)
tools/meson.build +7 -0
@@ -0,0 +1,7 @@ +have_virtiovideo = (targetos == 'linux' and + have_tools and + have_vhost_user) + +if have_virtiovideo + subdir('vhost-user-video') +endif
Signature · 1 line
2.39.2
PATCH v2 5/12 standard-headers: Add virtio_video.h
In reply to Albert EsteveFrom: Peter Griffin <[email protected]>
Message-Id: <[email protected]>
--- include/standard-headers/linux/virtio_video.h | 513 ++++++++++++++++++ 1 file changed, 513 insertions(+) create mode 100644 include/standard-headers/linux/virtio_video.h
include/standard-headers/linux/virtio_video.h new file +513 -0
@@ -0,0 +1,513 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* +* Virtio Video Device +* +* This header is BSD licensed so anyone can use the definitions +* to implement compatible drivers/servers: +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. Neither the name of IBM nor the names of its contributors +* may be used to endorse or promote products derived from this software +* without specific prior written permission. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR +* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +* SUCH DAMAGE. +* +* Copyright (C) 2019 OpenSynergy GmbH. +*/ + +#ifndef _UAPI_LINUX_VIRTIO_VIDEO_H +#define _UAPI_LINUX_VIRTIO_VIDEO_H + +#include <linux/types.h> +#include <linux/virtio_config.h> + +/* +* Feature bits +*/ + +/* Guest pages can be used for video buffers. */ +#define VIRTIO_VIDEO_F_RESOURCE_GUEST_PAGES 0 +/* +* The host can process buffers even if they are non-contiguous memory such as +* scatter-gather lists. +*/ +#define VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG 1 +/* Objects exported by another virtio device can be used for video buffers */ +#define VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT 2 + +/* +* Image formats +*/ + +enum virtio_video_format { +/* Raw formats */ +VIRTIO_VIDEO_FORMAT_RAW_MIN =1 , +VIRTIO_VIDEO_FORMAT_ARGB8888 =VIRTIO_VIDEO_FORMAT_RAW_MIN , +VIRTIO_VIDEO_FORMAT_BGRA8888 , +VIRTIO_VIDEO_FORMAT_NV12 ,/* 12 Y/CbCr 4:2:0 */ +VIRTIO_VIDEO_FORMAT_YUV420 ,/* 12 YUV 4:2:0 */ +VIRTIO_VIDEO_FORMAT_YVU420 ,/* 12 YVU 4:2:0 */ +VIRTIO_VIDEO_FORMAT_RAW_MAX =VIRTIO_VIDEO_FORMAT_YVU420 , + +/* Coded formats */ +VIRTIO_VIDEO_FORMAT_CODED_MIN =0x1000 , +VIRTIO_VIDEO_FORMAT_MPEG2 = +VIRTIO_VIDEO_FORMAT_CODED_MIN ,/* MPEG-2 Part 2 */ +VIRTIO_VIDEO_FORMAT_MPEG4 ,/* MPEG-4 Part 2 */ +VIRTIO_VIDEO_FORMAT_H264 ,/* H.264 */ +VIRTIO_VIDEO_FORMAT_HEVC ,/* HEVC aka H.265*/ +VIRTIO_VIDEO_FORMAT_VP8 ,/* VP8 */ +VIRTIO_VIDEO_FORMAT_VP9 ,/* VP9 */ +VIRTIO_VIDEO_FORMAT_FWHT ,/* FWHT used by vicodec */ +VIRTIO_VIDEO_FORMAT_CODED_MAX =VIRTIO_VIDEO_FORMAT_FWHT , +}; + +enum virtio_video_profile { +/* H.264 */ +VIRTIO_VIDEO_PROFILE_H264_MIN =0x100 , +VIRTIO_VIDEO_PROFILE_H264_BASELINE =VIRTIO_VIDEO_PROFILE_H264_MIN , +VIRTIO_VIDEO_PROFILE_H264_MAIN , +VIRTIO_VIDEO_PROFILE_H264_EXTENDED , +VIRTIO_VIDEO_PROFILE_H264_HIGH , +VIRTIO_VIDEO_PROFILE_H264_HIGH10PROFILE , +VIRTIO_VIDEO_PROFILE_H264_HIGH422PROFILE , +VIRTIO_VIDEO_PROFILE_H264_HIGH444PREDICTIVEPROFILE , +VIRTIO_VIDEO_PROFILE_H264_SCALABLEBASELINE , +VIRTIO_VIDEO_PROFILE_H264_SCALABLEHIGH , +VIRTIO_VIDEO_PROFILE_H264_STEREOHIGH , +VIRTIO_VIDEO_PROFILE_H264_MULTIVIEWHIGH , +VIRTIO_VIDEO_PROFILE_H264_MAX =VIRTIO_VIDEO_PROFILE_H264_MULTIVIEWHIGH , + +/* HEVC */ +VIRTIO_VIDEO_PROFILE_HEVC_MIN =0x200 , +VIRTIO_VIDEO_PROFILE_HEVC_MAIN =VIRTIO_VIDEO_PROFILE_HEVC_MIN , +VIRTIO_VIDEO_PROFILE_HEVC_MAIN10 , +VIRTIO_VIDEO_PROFILE_HEVC_MAIN_STILL_PICTURE , +VIRTIO_VIDEO_PROFILE_HEVC_MAX = +VIRTIO_VIDEO_PROFILE_HEVC_MAIN_STILL_PICTURE , + +/* VP8 */ +VIRTIO_VIDEO_PROFILE_VP8_MIN =0x300 , +VIRTIO_VIDEO_PROFILE_VP8_PROFILE0 =VIRTIO_VIDEO_PROFILE_VP8_MIN , +VIRTIO_VIDEO_PROFILE_VP8_PROFILE1 , +VIRTIO_VIDEO_PROFILE_VP8_PROFILE2 , +VIRTIO_VIDEO_PROFILE_VP8_PROFILE3 , +VIRTIO_VIDEO_PROFILE_VP8_MAX =VIRTIO_VIDEO_PROFILE_VP8_PROFILE3 , + +/* VP9 */ +VIRTIO_VIDEO_PROFILE_VP9_MIN =0x400 , +VIRTIO_VIDEO_PROFILE_VP9_PROFILE0 =VIRTIO_VIDEO_PROFILE_VP9_MIN , +VIRTIO_VIDEO_PROFILE_VP9_PROFILE1 , +VIRTIO_VIDEO_PROFILE_VP9_PROFILE2 , +VIRTIO_VIDEO_PROFILE_VP9_PROFILE3 , +VIRTIO_VIDEO_PROFILE_VP9_MAX =VIRTIO_VIDEO_PROFILE_VP9_PROFILE3 , +}; + +enum virtio_video_level { +/* H.264 */ +VIRTIO_VIDEO_LEVEL_H264_MIN =0x100 , +VIRTIO_VIDEO_LEVEL_H264_1_0 =VIRTIO_VIDEO_LEVEL_H264_MIN , +VIRTIO_VIDEO_LEVEL_H264_1_1 , +VIRTIO_VIDEO_LEVEL_H264_1_2 , +VIRTIO_VIDEO_LEVEL_H264_1_3 , +VIRTIO_VIDEO_LEVEL_H264_2_0 , +VIRTIO_VIDEO_LEVEL_H264_2_1 , +VIRTIO_VIDEO_LEVEL_H264_2_2 , +VIRTIO_VIDEO_LEVEL_H264_3_0 , +VIRTIO_VIDEO_LEVEL_H264_3_1 , +VIRTIO_VIDEO_LEVEL_H264_3_2 , +VIRTIO_VIDEO_LEVEL_H264_4_0 , +VIRTIO_VIDEO_LEVEL_H264_4_1 , +VIRTIO_VIDEO_LEVEL_H264_4_2 , +VIRTIO_VIDEO_LEVEL_H264_5_0 , +VIRTIO_VIDEO_LEVEL_H264_5_1 , +VIRTIO_VIDEO_LEVEL_H264_MAX =VIRTIO_VIDEO_LEVEL_H264_5_1 , +}; + +enum virtio_video_bitrate_mode { +VIRTIO_VIDEO_BITRATE_MODE_VBR =0 , +VIRTIO_VIDEO_BITRATE_MODE_CBR =1 , +}; + +/* +* Config +*/ + +struct virtio_video_config { +__le32 version; +__le32 max_caps_length; +__le32 max_resp_length; +__u8 device_name[32 ]; +}; + +/* +* Commands +*/ + +enum virtio_video_cmd_type { +/* Command */ +VIRTIO_VIDEO_CMD_QUERY_CAPABILITY =0x0100 , +VIRTIO_VIDEO_CMD_STREAM_CREATE , +VIRTIO_VIDEO_CMD_STREAM_DESTROY , +VIRTIO_VIDEO_CMD_STREAM_DRAIN , +VIRTIO_VIDEO_CMD_RESOURCE_CREATE , +VIRTIO_VIDEO_CMD_RESOURCE_QUEUE , +VIRTIO_VIDEO_CMD_RESOURCE_DESTROY_ALL , +VIRTIO_VIDEO_CMD_QUEUE_CLEAR , +/* GET/SET_PARAMS are being replaced with GET/SET_PARAMS_EXT */ +VIRTIO_VIDEO_CMD_GET_PARAMS__UNUSED , +VIRTIO_VIDEO_CMD_SET_PARAMS__UNUSED , +VIRTIO_VIDEO_CMD_QUERY_CONTROL , +VIRTIO_VIDEO_CMD_GET_CONTROL , +VIRTIO_VIDEO_CMD_SET_CONTROL , +VIRTIO_VIDEO_CMD_GET_PARAMS_EXT , +VIRTIO_VIDEO_CMD_SET_PARAMS_EXT , + +/* Response */ +VIRTIO_VIDEO_RESP_OK_NODATA =0x0200 , +VIRTIO_VIDEO_RESP_OK_QUERY_CAPABILITY , +VIRTIO_VIDEO_RESP_OK_RESOURCE_QUEUE , +VIRTIO_VIDEO_RESP_OK_GET_PARAMS , +VIRTIO_VIDEO_RESP_OK_QUERY_CONTROL , +VIRTIO_VIDEO_RESP_OK_GET_CONTROL , + +VIRTIO_VIDEO_RESP_ERR_INVALID_OPERATION =0x0300 , +VIRTIO_VIDEO_RESP_ERR_OUT_OF_MEMORY , +VIRTIO_VIDEO_RESP_ERR_INVALID_STREAM_ID , +VIRTIO_VIDEO_RESP_ERR_INVALID_RESOURCE_ID , +VIRTIO_VIDEO_RESP_ERR_INVALID_PARAMETER , +VIRTIO_VIDEO_RESP_ERR_UNSUPPORTED_CONTROL , +}; + +struct virtio_video_cmd_hdr { +__le32 type;/* One of enum virtio_video_cmd_type */ +__le32 stream_id; +}; + +/* VIRTIO_VIDEO_CMD_QUERY_CAPABILITY */ +enum virtio_video_queue_type { +VIRTIO_VIDEO_QUEUE_TYPE_INPUT =0x100 , +VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT , +}; + +struct virtio_video_query_capability { +struct virtio_video_cmd_hdr hdr; +__le32 queue_type;/* One of VIRTIO_VIDEO_QUEUE_TYPE_* types */ +__u8 padding[4 ]; +}; + +enum virtio_video_planes_layout_flag { +VIRTIO_VIDEO_PLANES_LAYOUT_SINGLE_BUFFER =1 <<0 , +VIRTIO_VIDEO_PLANES_LAYOUT_PER_PLANE =1 <<1 , +}; + +struct virtio_video_format_range { +__le32 min; +__le32 max; +__le32 step; +__u8 padding[4 ]; +}; + +struct virtio_video_format_frame { +struct virtio_video_format_range width; +struct virtio_video_format_range height; +__le32 num_rates; +__u8 padding[4 ]; +/* Followed by struct virtio_video_format_range frame_rates[] */ +}; + +struct virtio_video_format_desc { +__le64 mask; +__le32 format;/* One of VIRTIO_VIDEO_FORMAT_* types */ +__le32 planes_layout;/* Bitmask with VIRTIO_VIDEO_PLANES_LAYOUT_* */ +__le32 plane_align; +__le32 num_frames; +/* Followed by struct virtio_video_format_frame frames[] */ +}; + +struct virtio_video_query_capability_resp { +struct virtio_video_cmd_hdr hdr; +__le32 num_descs; +__u8 padding[4 ]; +/* Followed by struct virtio_video_format_desc descs[] */ +}; + +/* VIRTIO_VIDEO_CMD_STREAM_CREATE */ +enum virtio_video_mem_type { +VIRTIO_VIDEO_MEM_TYPE_GUEST_PAGES , +VIRTIO_VIDEO_MEM_TYPE_VIRTIO_OBJECT , +}; + +struct virtio_video_stream_create { +struct virtio_video_cmd_hdr hdr; +__le32 in_mem_type;/* One of VIRTIO_VIDEO_MEM_TYPE_* types */ +__le32 out_mem_type;/* One of VIRTIO_VIDEO_MEM_TYPE_* types */ +__le32 coded_format;/* One of VIRTIO_VIDEO_FORMAT_* types */ +__u8 padding[4 ]; +__u8 tag[64 ]; +}; + +/* VIRTIO_VIDEO_CMD_STREAM_DESTROY */ +struct virtio_video_stream_destroy { +struct virtio_video_cmd_hdr hdr; +}; + +/* VIRTIO_VIDEO_CMD_STREAM_DRAIN */ +struct virtio_video_stream_drain { +struct virtio_video_cmd_hdr hdr; +}; + +/* VIRTIO_VIDEO_CMD_RESOURCE_CREATE */ +struct virtio_video_mem_entry { +__le64 addr; +__le32 length; +__u8 padding[4 ]; +}; + +struct virtio_video_object_entry { +__u8 uuid[16 ]; +}; + +#define VIRTIO_VIDEO_MAX_PLANES 8 + +struct virtio_video_resource_create { +struct virtio_video_cmd_hdr hdr; +__le32 queue_type;/* One of VIRTIO_VIDEO_QUEUE_TYPE_* types */ +__le32 resource_id; +__le32 planes_layout; +__le32 num_planes; +__le32 plane_offsets[VIRTIO_VIDEO_MAX_PLANES ]; +__le32 num_entries[VIRTIO_VIDEO_MAX_PLANES ]; +/** +* Followed by either +* - struct virtio_video_mem_entry entries[] +* for VIRTIO_VIDEO_MEM_TYPE_GUEST_PAGES +* - struct virtio_video_object_entry entries[] +* for VIRTIO_VIDEO_MEM_TYPE_VIRTIO_OBJECT +*/ +}; + +/* VIRTIO_VIDEO_CMD_RESOURCE_QUEUE */ +struct virtio_video_resource_queue { +struct virtio_video_cmd_hdr hdr; +__le32 queue_type;/* One of VIRTIO_VIDEO_QUEUE_TYPE_* types */ +__le32 resource_id; +__le64 timestamp; +__le32 num_data_sizes; +__le32 data_sizes[VIRTIO_VIDEO_MAX_PLANES ]; +__u8 padding[4 ]; +}; + +enum virtio_video_buffer_flag { +VIRTIO_VIDEO_BUFFER_FLAG_ERR =0x0001 , +VIRTIO_VIDEO_BUFFER_FLAG_EOS =0x0002 , + +/* Encoder only */ +VIRTIO_VIDEO_BUFFER_FLAG_IFRAME =0x0004 , +VIRTIO_VIDEO_BUFFER_FLAG_PFRAME =0x0008 , +VIRTIO_VIDEO_BUFFER_FLAG_BFRAME =0x0010 , +}; + +struct virtio_video_resource_queue_resp { +struct virtio_video_cmd_hdr hdr; +__le64 timestamp; +__le32 flags;/* One of VIRTIO_VIDEO_BUFFER_FLAG_* flags */ +__le32 size;/* Encoded size */ +}; + +/* VIRTIO_VIDEO_CMD_RESOURCE_DESTROY_ALL */ +struct virtio_video_resource_destroy_all { +struct virtio_video_cmd_hdr hdr; +__le32 queue_type;/* One of VIRTIO_VIDEO_QUEUE_TYPE_* types */ +__u8 padding[4 ]; +}; + +/* VIRTIO_VIDEO_CMD_QUEUE_CLEAR */ +struct virtio_video_queue_clear { +struct virtio_video_cmd_hdr hdr; +__le32 queue_type;/* One of VIRTIO_VIDEO_QUEUE_TYPE_* types */ +__u8 padding[4 ]; +}; + +/* VIRTIO_VIDEO_CMD_GET_PARAMS */ +struct virtio_video_plane_format { +__le32 plane_size; +__le32 stride; +}; + +struct virtio_video_crop { +__le32 left; +__le32 top; +__le32 width; +__le32 height; +}; + +struct virtio_video_params { +__le32 queue_type;/* One of VIRTIO_VIDEO_QUEUE_TYPE_* types */ +__le32 format;/* One of VIRTIO_VIDEO_FORMAT_* types */ +__le32 frame_width; +__le32 frame_height; +__le32 min_buffers; +__le32 max_buffers; +struct virtio_video_crop crop; +__le32 frame_rate; +__le32 num_planes; +struct virtio_video_plane_format plane_formats[VIRTIO_VIDEO_MAX_PLANES ]; +__le32 resource_type;/* One of VIRTIO_VIDEO_MEM_TYPE_* types */ +__u8 padding[4 ]; +}; + +struct virtio_video_get_params { +struct virtio_video_cmd_hdr hdr; +__le32 queue_type;/* One of VIRTIO_VIDEO_QUEUE_TYPE_* types */ +__u8 padding[4 ]; +}; + +struct virtio_video_get_params_resp { +struct virtio_video_cmd_hdr hdr; +struct virtio_video_params params; +}; + +/* VIRTIO_VIDEO_CMD_SET_PARAMS */ +struct virtio_video_set_params { +struct virtio_video_cmd_hdr hdr; +struct virtio_video_params params; +}; + +/* VIRTIO_VIDEO_CMD_QUERY_CONTROL */ +enum virtio_video_control_type { +VIRTIO_VIDEO_CONTROL_BITRATE =1 , +VIRTIO_VIDEO_CONTROL_PROFILE , +VIRTIO_VIDEO_CONTROL_LEVEL , +VIRTIO_VIDEO_CONTROL_FORCE_KEYFRAME , +VIRTIO_VIDEO_CONTROL_BITRATE_MODE , +VIRTIO_VIDEO_CONTROL_BITRATE_PEAK , +VIRTIO_VIDEO_CONTROL_PREPEND_SPSPPS_TO_IDR , +}; + +struct virtio_video_query_control_profile { +__le32 format;/* One of VIRTIO_VIDEO_FORMAT_* */ +__u8 padding[4 ]; +}; + +struct virtio_video_query_control_level { +__le32 format;/* One of VIRTIO_VIDEO_FORMAT_* */ +__u8 padding[4 ]; +}; + +struct virtio_video_query_control { +struct virtio_video_cmd_hdr hdr; +__le32 control;/* One of VIRTIO_VIDEO_CONTROL_* types */ +__u8 padding[4 ]; +/* +* Followed by a value of struct virtio_video_query_control_* +* in accordance with the value of control. +*/ +}; + +struct virtio_video_query_control_resp_profile { +__le32 num; +__u8 padding[4 ]; +/* Followed by an array le32 profiles[] */ +}; + +struct virtio_video_query_control_resp_level { +__le32 num; +__u8 padding[4 ]; +/* Followed by an array le32 level[] */ +}; + +struct virtio_video_query_control_resp { +struct virtio_video_cmd_hdr hdr; +/* Followed by one of struct virtio_video_query_control_resp_* */ +}; + +/* VIRTIO_VIDEO_CMD_GET_CONTROL */ +struct virtio_video_get_control { +struct virtio_video_cmd_hdr hdr; +__le32 control;/* One of VIRTIO_VIDEO_CONTROL_* types */ +__u8 padding[4 ]; +}; + +struct virtio_video_control_val_bitrate { +__le32 bitrate; +__u8 padding[4 ]; +}; + +struct virtio_video_control_val_bitrate_peak { +__le32 bitrate_peak; +__u8 padding[4 ]; +}; + +struct virtio_video_control_val_bitrate_mode { +__le32 bitrate_mode; +__u8 padding[4 ]; +}; + +struct virtio_video_control_val_profile { +__le32 profile; +__u8 padding[4 ]; +}; + +struct virtio_video_control_val_level { +__le32 level; +__u8 padding[4 ]; +}; + +struct virtio_video_control_val_prepend_spspps_to_idr { +__le32 prepend_spspps_to_idr; +__u8 padding[4 ]; +}; + +struct virtio_video_get_control_resp { +struct virtio_video_cmd_hdr hdr; +/* Followed by one of struct virtio_video_control_val_* */ +}; + +/* VIRTIO_VIDEO_CMD_SET_CONTROL */ +struct virtio_video_set_control { +struct virtio_video_cmd_hdr hdr; +__le32 control;/* One of VIRTIO_VIDEO_CONTROL_* types */ +__u8 padding[4 ]; +/* Followed by one of struct virtio_video_control_val_* */ +}; + +struct virtio_video_set_control_resp { +struct virtio_video_cmd_hdr hdr; +}; + +/* +* Events +*/ + +enum virtio_video_event_type { +/* For all devices */ +VIRTIO_VIDEO_EVENT_ERROR =0x0100 , + +/* For decoder only */ +VIRTIO_VIDEO_EVENT_DECODER_RESOLUTION_CHANGED =0x0200 , +}; + +struct virtio_video_event { +__le32 event_type;/* One of VIRTIO_VIDEO_EVENT_* types */ +__le32 stream_id; +}; + +#endif /* _UAPI_LINUX_VIRTIO_VIDEO_H */
Signature · 1 line
2.39.2
PATCH v2 6/12 hw/display: add vhost-user-video-pci
In reply to Albert EsteveFrom: Peter Griffin <[email protected]>
Add boiler plate code for vhost-user-video-pci.
Example
-device vhost-user-video-pci,chardev=video,id=video -chardev socket,path=video.sock,id=video
Message-Id: <[email protected]>
--- hw/display/vhost-user-video-pci.c | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 hw/display/vhost-user-video-pci.c
hw/display/vhost-user-video-pci.c new file +82 -0
@@ -0,0 +1,82 @@ +/* +* Vhost-user VIDEO virtio device PCI glue +* +* Copyright (c) 2021 Linaro Ltd +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/vhost-user-video.h" +#include "hw/virtio/virtio-pci.h" + +struct VHostUserVIDEOPCI { +VirtIOPCIProxy parent_obj; +VHostUserVIDEO vdev; +}; + +typedef struct VHostUserVIDEOPCI VHostUserVIDEOPCI ; + +#define TYPE_VHOST_USER_VIDEO_PCI "vhost-user-video-pci-base" + +#define VHOST_USER_VIDEO_PCI (obj) \ + OBJECT_CHECK(VHostUserVIDEOPCI, (obj), TYPE_VHOST_USER_VIDEO_PCI) + +static Property vuvideo_pci_properties[] = { +DEFINE_PROP_BIT ("ioeventfd" , VirtIOPCIProxy, flags, +VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT , true), +DEFINE_PROP_UINT32 ("vectors" , VirtIOPCIProxy, nvectors, +DEV_NVECTORS_UNSPECIFIED ), +DEFINE_PROP_END_OF_LIST (), +}; + +static void vuvideo_pci_realize (VirtIOPCIProxy *vpci_dev,Error **errp) +{ +VHostUserVIDEOPCI *dev =VHOST_USER_VIDEO_PCI (vpci_dev); +DeviceState *vdev =DEVICE (&dev->vdev); + +if (vpci_dev->nvectors ==DEV_NVECTORS_UNSPECIFIED ) { + vpci_dev->nvectors =1 ; + } + +qdev_set_parent_bus (vdev,BUS (&vpci_dev->bus), errp); +object_property_set_bool (OBJECT (vdev),"realized" , true, errp); +} + +static void vuvideo_pci_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +VirtioPCIClass *k =VIRTIO_PCI_CLASS (klass); +PCIDeviceClass *pcidev_k =PCI_DEVICE_CLASS (klass); + k->realize = vuvideo_pci_realize; +set_bit (DEVICE_CATEGORY_STORAGE , dc->categories); +device_class_set_props (dc, vuvideo_pci_properties); + pcidev_k->vendor_id =PCI_VENDOR_ID_REDHAT_QUMRANET ; + pcidev_k->device_id =0 ;/* Set by virtio-pci based on virtio id */ + pcidev_k->revision =0x00 ; + pcidev_k->class_id =PCI_CLASS_STORAGE_OTHER ; +} + +static void vuvideo_pci_instance_init (Object *obj) +{ +VHostUserVIDEOPCI *dev =VHOST_USER_VIDEO_PCI (obj); + +virtio_instance_init_common (obj, &dev->vdev,sizeof (dev->vdev), +TYPE_VHOST_USER_VIDEO ); +} + +static const VirtioPCIDeviceTypeInfo vuvideo_pci_info = { + .base_name =TYPE_VHOST_USER_VIDEO_PCI , + .non_transitional_name ="vhost-user-video-pci" , + .instance_size =sizeof (VHostUserVIDEOPCI), + .instance_init = vuvideo_pci_instance_init, + .class_init = vuvideo_pci_class_init, +}; + +static void vuvideo_pci_register (void ) +{ +virtio_pci_types_register (&vuvideo_pci_info); +} + +type_init (vuvideo_pci_register);
Signature · 1 line
2.39.2
PATCH v2 7/12 vhost-user.json: add video type
In reply to Albert Esteve--- docs/interop/vhost-user.json | 2 ++ 1 file changed, 2 insertions(+)
docs/interop/vhost-user.json +2 -0
@@ -31,6 +31,7 @@ # @rpmsg: virtio remote processor messaging # @rproc-serial: virtio remoteproc serial link # @scsi: virtio scsi +# @video: virtio video # @vsock: virtio vsock transport # @fs: virtio fs (since 4.2) #
@@ -52,6 +53,7 @@ 'rpmsg', 'rproc-serial', 'scsi', + 'video', 'vsock', 'fs' ]
Signature · 1 line
2.39.2
PATCH v2 8/12 tools/vhost-user-video: Add initial vhost-user-video vmm
In reply to Albert EsteveFrom: Peter Griffin <[email protected]>
This vmm translates from virtio-video v3 protocol and writes to a v4l2 mem2mem stateful decoder/encoder device [1]. v3 was chosen as that is what the virtio-video Linux frontend driver implements.
This allows for testing with the v4l2 vicodec test codec [2] module in the Linux kernel, and is intended to also be used with Arm SoCs that implement a v4l2 stateful decoder/encoder drivers.
The advantage of developing & testing with vicodec is that is allows quick development on a purely virtual setup with qemu and a host Linux kernel. Also it allows ci systems like lkft, kernelci to easily test the virtio interface.
Currently conversion from virtio-video to v4l2 stateless m2m codec driver or VAAPI drivers is consiered out ot scope as is emulation of a decoder device using a something like ffmpeg. Although this could be added in the future.
Note some virtio & v4l2 helpers were based off virtio-video Linux frontend driver and yavta utility, both GPL v2.
Example host commands
sudo modprobe vicodec multiplanar=1
vhost-user-video --v4l2-device=/dev/video3 -v --socket-path=video.sock
Run Qemu with
-device vhost-user-video-pci,chardev=video,id=video
Guest decoder
v4l2-ctl -d0 -x width=640,height=480 -v width=640,height=480,pixelformat=YU12 \
--stream-mmap --stream-out-mmap --stream-from jelly_640_480-420P.fwht \
--stream-to out-jelly-640-480.YU12
[1] https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/dev-decoder.html
[2] https://lwn.net/Articles/760650/
--- tools/vhost-user-video/50-qemu-video.json.in | 5 + tools/vhost-user-video/meson.build | 10 + tools/vhost-user-video/v4l2_backend.c | 1752 ++++++++++++++++ tools/vhost-user-video/v4l2_backend.h | 104 + tools/vhost-user-video/vhost-user-video.c | 1779 +++++++++++++++++ tools/vhost-user-video/virtio_video_helpers.c | 466 +++++ tools/vhost-user-video/virtio_video_helpers.h | 168 ++ tools/vhost-user-video/vuvideo.h | 43 + 8 files changed, 4327 insertions(+) create mode 100644 tools/vhost-user-video/50-qemu-video.json.in create mode 100644 tools/vhost-user-video/meson.build create mode 100644 tools/vhost-user-video/v4l2_backend.c create mode 100644 tools/vhost-user-video/v4l2_backend.h create mode 100644 tools/vhost-user-video/vhost-user-video.c create mode 100644 tools/vhost-user-video/virtio_video_helpers.c create mode 100644 tools/vhost-user-video/virtio_video_helpers.h create mode 100644 tools/vhost-user-video/vuvideo.h
tools/vhost-user-video/50-qemu-video.json.in new file +5 -0
@@ -0,0 +1,5 @@ +{ + "description": "QEMU vhost-user-video", + "type": "video", + "binary": "@libexecdir@/vhost-user-video" +}
tools/vhost-user-video/meson.build new file +10 -0
@@ -0,0 +1,10 @@ +executable('vhost-user-video', files( + 'vhost-user-video.c', 'v4l2_backend.c', 'virtio_video_helpers.c'), + dependencies: [qemuutil, glib, gio, vhost_user], + install: true, + install_dir: get_option('libexecdir')) + +configure_file(input: '50-qemu-video.json.in', + output: '50-qemu-video.json', + configuration: config_host, + install_dir: qemu_datadir / 'vhost-user')
tools/vhost-user-video/v4l2_backend.c new file +1752 -0
1,752 lines not on this page · raw message
tools/vhost-user-video/v4l2_backend.h new file +104 -0
104 lines not on this page · raw message
tools/vhost-user-video/vhost-user-video.c new file +1779 -0
1,129 lines not on this page · raw message
tools/vhost-user-video/virtio_video_helpers.c new file +466 -0
466 lines not on this page · raw message
tools/vhost-user-video/virtio_video_helpers.h new file +168 -0
168 lines not on this page · raw message
tools/vhost-user-video/vuvideo.h new file +43 -0
43 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.39.2
Part of the diff is not shown here. View the raw message.
PATCH v2 9/12 tests/qtest: add virtio-video test
In reply to Albert EsteveAdd some minimal testing of the vhost-user-video
backend similar to what is tested for other
vhost-user devices. For now it only stresses
the backend instantiation and some message
passing through the control socket.
--- tests/qtest/libqos/meson.build | 1 + tests/qtest/libqos/virtio-video.c | 179 ++++++++++++++++++++++++++++++ tests/qtest/libqos/virtio-video.h | 39 +++++++ tests/qtest/vhost-user-test.c | 32 ++++++ 4 files changed, 251 insertions(+) create mode 100644 tests/qtest/libqos/virtio-video.c create mode 100644 tests/qtest/libqos/virtio-video.h
tests/qtest/libqos/meson.build +1 -0
@@ -46,6 +46,7 @@ libqos_srcs = files( 'virtio-serial.c', 'virtio-iommu.c', 'virtio-gpio.c', + 'virtio-video.c', 'generic-pcihost.c', # qgraph machines:
tests/qtest/libqos/virtio-video.c new file +179 -0
179 lines not on this page · raw message
tests/qtest/libqos/virtio-video.h new file +39 -0
@@ -0,0 +1,39 @@ +/* +* libqos virtio-video definitions +* +* Copyright (c) 2023 Red Hat Inc. +* +* Authors: +* Albert Esteve <[email protected]> +* (based on virtio-gpio.h) +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef TESTS_LIBQOS_VIRTIO_VIDEO_H +#define TESTS_LIBQOS_VIRTIO_VIDEO_H + +#include "qgraph.h" +#include "virtio.h" +#include "virtio-pci.h" + +typedef struct QVhostUserVideo QVhostUserVideo ; +typedef struct QVhostUserVideoPCI QVhostUserVideoPCI ; +typedef struct QVhostUserVideoDevice QVhostUserVideoDevice ; + +struct QVhostUserVideo { +QVirtioDevice *vdev; +QVirtQueue **queues; +}; + +struct QVhostUserVideoPCI { +QVirtioPCIDevice pci_vdev; +QVhostUserVideo video; +}; + +struct QVhostUserVideoDevice { +QOSGraphObject obj; +QVhostUserVideo video; +}; + +#endif /* TESTS_LIBQOS_VIRTIO_VIDEO_H */
tests/qtest/vhost-user-test.c +32 -0
@@ -33,6 +33,7 @@#include "standard-headers/linux/virtio_ids.h" #include "standard-headers/linux/virtio_net.h" #include "standard-headers/linux/virtio_gpio.h" +#include "standard-headers/linux/virtio_video.h" #ifdef CONFIG_LINUX #include <sys/vfs.h>
@@ -145,6 +146,7 @@ enum {enum {VHOST_USER_NET ,VHOST_USER_GPIO , +VHOST_USER_VIDEO , }; typedef struct TestServer {
@@ -1156,3 +1158,33 @@ static void register_vhost_gpio_test(void) "vhost-user-gpio", test_read_guest_mem, &opts); }libqos_init (register_vhost_gpio_test); + +static uint64_t vu_video_get_features (TestServer *s) +{ +return 0x1ULL <<VHOST_USER_F_PROTOCOL_FEATURES ; +} + +static struct vhost_user_ops g_vu_video_ops = { + .type =VHOST_USER_VIDEO , + + .append_opts = append_vhost_gpio_opts, + + .get_features = vu_video_get_features, + .set_features = vu_net_set_features, + .get_protocol_features = vu_gpio_get_protocol_features, +}; + +static void register_vhost_video_test (void ) +{ +QOSGraphTestOptions opts = { + .before = vhost_user_test_setup, + .subprocess = true, + .arg = &g_vu_video_ops, + }; + +qemu_add_opts (&qemu_chardev_opts); + +qos_add_test ("read-guest-mem/memfile" , +"vhost-user-video" , test_read_guest_mem, &opts); +} +libqos_init (register_vhost_video_test);
Signature · 1 line
2.39.2
PATCH v2 10/12 vhost-user-video: add dev_type to CLI
In reply to Albert EsteveAdd dev_type to the command line interface. This way we can select dev_type=encoder or dev_type=decoder and assign the virtio ID appropiately. If ommited, default virtio ID used is decoder's ID.
Example:
-device vhost-user-video-pci,chardev=video,dev_type=encoder,id=video
--- docs/system/devices/vhost-user-video.rst | 2 +- hw/display/vhost-user-video.c | 13 +++++++++++-- include/hw/virtio/vhost-user-video.h | 1 + 3 files changed, 13 insertions(+), 3 deletions(-)
docs/system/devices/vhost-user-video.rst +1 -1
@@ -72,7 +72,7 @@ use to communicate as well as share the guests memory over a memfd. :: host# qemu-system \ - -device vhost-user-video-pci,chardev=video,id=video \ + -device vhost-user-video-pci,chardev=video,dev_type=decoder,id=video \ -chardev socket,path=/tmp//video.sock,id=video \ -m 4096 \ -object memory-backend-file,id=mem,size=4G,mem-path=/dev/shm,share=on \
hw/display/vhost-user-video.c +11 -2
@@ -300,8 +300,16 @@ static void vhost_user_video_device_realize(DeviceState *dev, Error **errp)return ; } -/* TODO Implement VIDEO_ENC, currently only support VIDEO_DEC */ -virtio_init (vdev,VIRTIO_ID_VIDEO_DECODER ,sizeof (struct virtio_video_config )); +if (video->conf.type ==NULL || !strcmp (video->conf.type,"decoder" )) { +virtio_init (vdev,VIRTIO_ID_VIDEO_DECODER , +sizeof (struct virtio_video_config )); + }else if (!strcmp (video->conf.type,"encoder" )) { +virtio_init (vdev,VIRTIO_ID_VIDEO_ENCODER , +sizeof (struct virtio_video_config )); + }else { +error_report ("invalid type received: %s" , video->conf.type); + goto vhost_dev_init_failed; + }/* one command queue and one event queue */ video->vhost_dev.nvqs =2 ;
@@ -375,6 +383,7 @@ static const VMStateDescription vhost_user_video_vmstate = {static Property vhost_user_video_properties[] = {DEFINE_PROP_CHR ("chardev" , VHostUserVIDEO, conf.chardev), +DEFINE_PROP_STRING ("dev_type" , VHostUserVIDEO, conf.type),DEFINE_PROP_END_OF_LIST (), };
include/hw/virtio/vhost-user-video.h +1 -0
@@ -22,6 +22,7 @@typedef struct {CharBackend chardev; +char *type;struct virtio_video_config config; }VHostUserVIDEOConf ;
Signature · 1 line
2.39.2
PATCH v2 11/12 vhost-user-video-udmabuf: add udmabuf helpers
In reply to Albert EsteveAdd helper for handling user-created DMA buffers
through the udmabuf device. Buffers are stored in
a hash table and associated to an UUID, so that
they can be looked up later from the backend.
--- tools/vhost-user-video/meson.build | 2 +- tools/vhost-user-video/virtio_video_helpers.h | 32 +++- tools/vhost-user-video/virtio_video_udmabuf.c | 180 ++++++++++++++++++ tools/vhost-user-video/vuvideo.h | 10 +- 4 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 tools/vhost-user-video/virtio_video_udmabuf.c
tools/vhost-user-video/meson.build +1 -1
@@ -1,5 +1,5 @@ executable('vhost-user-video', files( - 'vhost-user-video.c', 'v4l2_backend.c', 'virtio_video_helpers.c'), + 'vhost-user-video.c', 'v4l2_backend.c', 'virtio_video_helpers.c', 'virtio_video_udmabuf.c'), dependencies: [qemuutil, glib, gio, vhost_user], install: true, install_dir: get_option('libexecdir'))
tools/vhost-user-video/virtio_video_helpers.h +31 -1
@@ -21,17 +21,37 @@#include <linux/videodev2.h> #include "libvhost-user-glib.h" #include "libvhost-user.h" +#include "qemu/uuid.h" +#include "qemu/queue.h" /* * Structure to track internal state of VIDEO Device */ +struct resource ; +struct VuVideoDMABuf ; + +struct vuvbm_device { +bool opened; +int fd; + +bool (*alloc_bm)(struct VuVideoDMABuf *buf); +void (*free_bm)(struct VuVideoDMABuf *buf); +int (*get_fd)(struct VuVideoDMABuf *buf); +bool (*map_bm)(struct VuVideoDMABuf *buf); +void (*unmap_bm)(struct VuVideoDMABuf *buf); +void (*device_destroy)(struct vuvbm_device *dev); + +GHashTable *resource_uuids; +}; +typedef struct VuVideo {VugDev dev;struct virtio_video_config virtio_config;GMainLoop *loop;struct v4l2_device *v4l2_dev;GList *streams; +struct vuvbm_device *bm_dev; }VuVideo ;struct v4l2_device {
@@ -56,10 +76,18 @@ struct vu_video_ctrl_command { }; +typedef struct VuVideoDMABuf { +struct vuvbm_device *dev; +int memfd; +int dmafd; + +void *start; +size_t length; +}VuVideoDMABuf ; +/* * Structure to track internal state of a Stream */ -struct stream {struct virtio_video_stream_create vio_stream;uint32_t stream_id;
@@ -89,11 +117,13 @@ struct stream {struct resource {uint32_t stream_id; +QemuUUID uuid;struct virtio_video_resource_create vio_resource;struct virtio_video_resource_queue vio_res_q;struct iovec *iov;uint32_t iov_count;uint32_t v4l2_index; +struct VuVideoDMABuf *buf;enum v4l2_buf_type type;struct vu_video_ctrl_command *vio_q_cmd;bool queued;
tools/vhost-user-video/virtio_video_udmabuf.c new file +180 -0
180 lines not on this page · raw message
tools/vhost-user-video/vuvideo.h +9 -1
@@ -18,7 +18,6 @@#include "virtio_video_helpers.h" #include "v4l2_backend.h" -#include "vuvideo.h" GList *get_resource_list (struct stream *s,uint32_t queue_type);void send_qclear_res_reply (gpointer data,gpointer user_data);
@@ -40,4 +39,13 @@ void remove_all_resources(struct stream *s, uint32_t queue_type);void handle_queue_clear_cmd (struct VuVideo *v,struct vu_video_ctrl_command *vio_cmd); +/* virtio_video_udmabuf.c */ +bool vuvbm_buffer_create (struct vuvbm_device *dev, +struct VuVideoDMABuf *buffer, +uint32_t len); +void vuvbm_init_device (struct vuvbm_device *dev); +struct VuVideoDMABuf *vuvbm_lookup (struct vuvbm_device *dev,QemuUUID uuid); +void vuvbm_buffer_destroy (struct VuVideoDMABuf *buffer); +void vuvbm_device_destroy (struct vuvbm_device *dev); +#endif
Signature · 1 line
2.39.2
PATCH v2 12/12 Add support for v4l2_memory_dmabuf
In reply to Albert EsteveSupport VIRTIO_VIDEO_MEM_TYPE_VIRTIO_OBJECT feature, by importing DMA buffers using the v4l2 API.
In this patch, we do not import the buffer from any other virtio device, but just create a DMA buffer through udmabuf, and import it.
However, in preparation for the case where we actually share buffers between different devices, we store the UUID and keep buffers associated to them in a hash table.
--- tools/vhost-user-video/v4l2_backend.c | 118 +++++++++++++++++++--- tools/vhost-user-video/v4l2_backend.h | 9 +- tools/vhost-user-video/vhost-user-video.c | 34 ++++--- 3 files changed, 131 insertions(+), 30 deletions(-)
tools/vhost-user-video/v4l2_backend.c +102 -16
@@ -888,11 +888,55 @@ int video_streamoff(struct stream *s, enum v4l2_buf_type type)return ret; } +int v4l2_dmabuf_lookup_mplane (struct vuvbm_device *dev, +struct resource *res, +unsigned int iov_cnt) +{ +struct VuVideoDMABuf *buf =vuvbm_lookup (dev, res->uuid); +if (!buf) { +g_debug ("Buffer not found. Creating." ); + res->buf =g_malloc0 (sizeof (struct VuVideoDMABuf ) * iov_cnt); +for (int i =0 ; i < iov_cnt; i++) { +if (!vuvbm_buffer_create (dev, &res->buf[i], res->iov[i].iov_len)) { +return -1 ; + } + } +g_debug ("Inserting buffer into the table." ); +g_hash_table_insert (dev->resource_uuids, res->buf, &res->uuid); + }else { + res->buf = buf; +g_debug ("Buffer found." ); + } + +return 0 ; +} + +int v4l2_dmabuf_lookup (struct vuvbm_device *dev, +struct resource *res, +unsigned int iov_len) +{ +struct VuVideoDMABuf *buf =vuvbm_lookup (dev, res->uuid); +if (!buf) { +g_debug ("Buffer not found. Creating." ); + res->buf = g_new0(struct VuVideoDMABuf, 1); +if (!vuvbm_buffer_create (dev, res->buf, iov_len)) { +return -1 ; + } +g_debug ("Inserting buffer into the table." ); +g_hash_table_insert (dev->resource_uuids, res->buf, &res->uuid); + }else { + res->buf = buf; +g_debug ("Buffer found." ); + } + +return 0 ; +} +int v4l2_queue_buffer (enum v4l2_buf_type type,enum v4l2_memory memory,struct virtio_video_resource_queue *qcmd,struct resource *res,struct stream *s, -struct v4l2_device *dev) +struct v4l2_device *dev,struct vuvbm_device *bm_dev) {struct v4l2_buffer vbuf;int ret =0 ;
@@ -911,22 +955,64 @@ int v4l2_queue_buffer(enum v4l2_buf_type type,convert_to_timeval (le64toh (qcmd->timestamp), &vbuf.timestamp); -if (V4L2_TYPE_IS_MULTIPLANAR (type)) { -/* for mplane length field is number of elements in planes array */ - vbuf.length = res->vio_resource.num_planes; - vbuf.m.planes =g_malloc0 (sizeof (struct v4l2_plane ) - * res->vio_resource.num_planes); +if (memory ==V4L2_MEMORY_USERPTR ) { +if (V4L2_TYPE_IS_MULTIPLANAR (type)) { +/* for mplane length field is number of elements in planes array */ + vbuf.length = res->vio_resource.num_planes; + vbuf.m.planes =g_malloc0 (sizeof (struct v4l2_plane ) + * res->vio_resource.num_planes); -for (int i =0 ; i < vbuf.length; i++) { - vbuf.m.planes[i].m.userptr = (unsigned long )res->iov[i].iov_base; - vbuf.m.planes[i].length = (unsigned long )res->iov[i].iov_len; +for (int i =0 ; i < vbuf.length; i++) { + vbuf.m.planes[i].m.userptr = + (unsigned long )res->iov[i].iov_base; + vbuf.m.planes[i].length = (unsigned long )res->iov[i].iov_len; + } + }else { +assert (res->iov !=NULL ); + vbuf.m.userptr = (unsigned long )res->iov[0 ].iov_base; + vbuf.length = res->iov[0 ].iov_len; +g_debug ("%s: iov_base = 0x%p" , __func__, res->iov[0 ].iov_base); +g_debug ("%s: iov_len = 0x%lx" , __func__, res->iov[0 ].iov_len); + } + }else { +assert (memory ==V4L2_MEMORY_DMABUF ); +if (V4L2_TYPE_IS_MULTIPLANAR (type)) { + vbuf.length = res->vio_resource.num_planes; + vbuf.m.planes =g_malloc0 (sizeof (struct v4l2_plane ) * vbuf.length); +if (!res->buf) { + ret =v4l2_dmabuf_lookup_mplane (bm_dev, res, vbuf.length); +if (ret <0 ) { +g_warning ("Buffer create failed." ); +return ret; + } + } + +for (int i =0 ; i < vbuf.length; i++) { + vbuf.m.planes[i].m.fd = res->buf->dev->get_fd (&res->buf[i]); + vbuf.m.planes[i].length = (unsigned long )res->iov[i].iov_len; +/* Copy virtio shared memory contents to DMA buffer */ +memcpy (res->buf[i].start, + res->iov[i].iov_base, res->iov[i].iov_len); + } + }else { +if (!res->buf) { + ret =ioctl (fd,VIDIOC_QUERYBUF , &vbuf); +if (ret == -EINVAL ) { +g_printerr ("VIDIOC_QUERYBUF failed: %s (%d)\n" , +g_strerror (errno), errno); +return ret; + } + + ret =v4l2_dmabuf_lookup (bm_dev, res, vbuf.length); +if (ret <0 ) { +g_warning ("Buffer create failed." ); +return ret; + } + } + vbuf.m.fd = res->buf->dev->get_fd (res->buf); +/* Copy virtio shared memory contents to DMA buffer */ +memcpy (res->buf->start, res->iov[0 ].iov_base, res->iov[0 ].iov_len); } - }else if (res->iov !=NULL ) { -/* m is a union of userptr, *planes and fd */ - vbuf.m.userptr = (unsigned long )res->iov[0 ].iov_base; - vbuf.length = res->iov[0 ].iov_len; -g_debug ("%s: iov_base = 0x%p" , __func__, res->iov[0 ].iov_base); -g_debug ("%s: iov_len = 0x%lx" , __func__, res->iov[0 ].iov_len); }if (V4L2_TYPE_IS_OUTPUT (type)) {
@@ -979,7 +1065,7 @@ int v4l2_dequeue_buffer(int fd, enum v4l2_buf_type type,memset (&vbuf,0 ,sizeof (vbuf)); vbuf.type = type; - vbuf.memory = memory; + vbuf.memory = memory; vbuf.field =V4L2_FIELD_NONE ; vbuf.flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ;
tools/vhost-user-video/v4l2_backend.h +8 -1
@@ -69,11 +69,18 @@ int v4l2_video_set_format(int fd, enum v4l2_buf_type type,int v4l2_set_pixel_format (int fd,enum v4l2_buf_type buf_type,uint32_t pixelformat); +int v4l2_dmabuf_lookup_mplane (struct vuvbm_device *dev, +struct resource *res, +unsigned int iov_cnt); +int v4l2_dmabuf_lookup (struct vuvbm_device *dev, +struct resource *res, +unsigned int iov_len);int v4l2_queue_buffer (enum v4l2_buf_type type,enum v4l2_memory memory,struct virtio_video_resource_queue *qcmd,struct resource *res,struct stream *s, -struct v4l2_device *dev); +struct v4l2_device *dev, +struct vuvbm_device *bm_dev);int v4l2_dequeue_buffer (int fd,enum v4l2_buf_type type,enum v4l2_memory memory,struct stream *s);
tools/vhost-user-video/vhost-user-video.c +21 -13
@@ -391,6 +391,9 @@ void remove_all_resources(struct stream *s, uint32_t queue_type)/* free resource memory allocated in resource_create() */ g_free (r->iov); +if (r->buf !=NULL ) { +vuvbm_buffer_destroy (r->buf); + }g_free (r); *resource_list =g_list_delete_link (*resource_list, l); }
@@ -880,10 +883,16 @@ handle_resource_create_cmd(struct VuVideo *v, res->iov_count = total_entries; }break ;case VIRTIO_VIDEO_MEM_TYPE_VIRTIO_OBJECT : -g_critical ("%s: VIRTIO_OBJECT not implemented!" , __func__); -/* TODO implement VIRTIO_OBJECT support */ - cmd->hdr.type =VIRTIO_VIDEO_RESP_ERR_INVALID_PARAMETER ; - goto out_unlock; + { +struct virtio_video_object_entry *ent; + ent = (void *)cmd +sizeof (struct virtio_video_resource_create ); + +memcpy (&res->uuid, ent->uuid,sizeof (ent->uuid)); +g_debug ("%s: create resource uuid(%s)" , + __func__,qemu_uuid_unparse_strdup (&res->uuid)); + +vuvbm_init_device (v->bm_dev); + }break ; } cmd->hdr.type =VIRTIO_VIDEO_RESP_OK_NODATA ;
@@ -974,7 +983,7 @@ handle_resource_queue_cmd(struct VuVideo *v,get_queue_mem_type (s, cmd->queue_type);enum v4l2_memory memory =get_v4l2_memory (mem_type); - ret =v4l2_queue_buffer (buf_type, memory, cmd, res, s, v->v4l2_dev); + ret =v4l2_queue_buffer (buf_type, memory, cmd, res, s, v->v4l2_dev, v->bm_dev);if (ret <0 ) {g_critical ("%s: v4l2_queue_buffer failed" , __func__);/* virtio error set by v4l2_queue_buffer */
@@ -1064,12 +1073,7 @@ handle_stream_create_cmd(struct VuVideo *v, req_stream_id = cmd->hdr.stream_id; coded_format =le32toh (cmd->coded_format); -if ((le32toh (cmd->in_mem_type) ==VIRTIO_VIDEO_MEM_TYPE_VIRTIO_OBJECT ) || - (le32toh (cmd->out_mem_type) ==VIRTIO_VIDEO_MEM_TYPE_VIRTIO_OBJECT )) { -/* TODO implement VIRTIO_VIDEO_MEM_TYPE_VIRTIO_OBJECT */ -g_printerr ("%s: MEM_TYPE_VIRTIO_OBJECT not supported yet" , __func__); - cmd->hdr.type =VIRTIO_VIDEO_RESP_ERR_INVALID_PARAMETER ; - }else if (find_stream (v, req_stream_id)) { +if (find_stream (v, req_stream_id)) {g_debug ("%s: Stream ID in use - " , __func__); cmd->hdr.type =VIRTIO_VIDEO_RESP_ERR_INVALID_STREAM_ID ; }else {
@@ -1212,7 +1216,6 @@ handle_stream_destroy_cmd(struct VuVideo *v, buftype = s->has_mplane ?V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :V4L2_BUF_TYPE_VIDEO_CAPTURE ; - mem_type =get_queue_mem_type (s,VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT );video_free_buffers (s->fd, buftype,get_v4l2_memory (mem_type));remove_all_resources (s,VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT );
@@ -1644,7 +1647,7 @@ static void video_destroy(VuVideo *v)if (socket_path) {unlink (socket_path); } - +vuvbm_device_destroy (v->bm_dev);v4l2_backend_free (v->v4l2_dev); }
@@ -1717,6 +1720,11 @@ int main(int argc, char *argv[]) } } +/* +* Create a new Buffer Memory device to handle DMA buffers. +*/ + video.bm_dev = g_new0(struct vuvbm_device, 1); +/* * Now create a vhost-user socket that we will receive messages * on. Once we have our handler set up we can enter the glib main
Signature · 1 line
2.39.2