diff --git a/README.md b/README.md index 690d76fbc4..8290b3ef6a 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,8 @@ Camera hand tracking runs Google's [MediaPipe](https://github.com/google-ai-edge The Store installs and runs apps with [Flatpak](https://github.com/flatpak/flatpak) (LGPL-2.1). Its sandbox goes through DroidDeck's own bubblewrap shim, which follows how Flatpak hands bwrap its environment. Chromium and Electron apps from Flathub start their browser processes through [zypak](https://github.com/refi64/zypak) by [@refi64](https://github.com/refi64) (BSD-3-Clause); its source was read to see that it always spawns them through Flatpak's spawn portal, which is why DroidDeck starts them directly without Chromium's sandbox instead. +Remote Play from a PC to the arm64 Steam client goes through a stand-in for the V4L2 decoder and the libdrm dumb buffers that Steam's `streaming_client` expects: each frame is decoded by Android's MediaCodec through the app, or by the [FFmpeg](https://ffmpeg.org) (LGPL-2.1-or-later) that ships in Steam's own runtime, into dma-heap buffers that its Vulkan renderer imports. The Qualcomm low-latency decoder settings follow [Moonlight](https://github.com/moonlight-stream/moonlight-android) (GPL-3.0). + Steam's Browse buttons open a GTK 3 file chooser through PyGObject (LGPL-2.1-or-later). It runs under Steam's own `reaper` with Steam's app ID, which [gamescope](https://github.com/ValveSoftware/gamescope) (BSD-2-Clause) reads from the process tree (`GetAppIdFromReaper` in `src/Utils/Process.cpp`) to show it over Steam. Black Ops III and Dark Souls Remastered start without [umu-protonfixes](https://github.com/Open-Wine-Components/umu-protonfixes) (BSD-2-Clause), whose fixes for them (`311210.py` sets ALSA audio, `570940.py` installs VC++ 2017) fail under ARM64 Proton. diff --git a/app/src/main/cpp/venc/venc_proto.h b/app/src/main/cpp/venc/venc_proto.h index 9622577119..47fb6ef241 100644 --- a/app/src/main/cpp/venc/venc_proto.h +++ b/app/src/main/cpp/venc/venc_proto.h @@ -9,7 +9,7 @@ #define VENC_MAX_BUFFERS 16 -enum venc_msg { VENC_CONFIG = 1, VENC_FRAME = 2, VENC_DONE = 3, VENC_BITRATE = 4, VENC_TOUCH = 5 }; +enum venc_msg { VENC_CONFIG = 1, VENC_FRAME = 2, VENC_DONE = 3, VENC_BITRATE = 4, VENC_TOUCH = 5, VDEC_CONFIG = 6, VDEC_CAPTURE = 7, VDEC_PACKET = 8, VDEC_RESULT = 9 }; enum venc_touch_action { VENC_TOUCH_DOWN = 0, VENC_TOUCH_MOVE = 1, VENC_TOUCH_UP = 2 }; enum venc_codec { VENC_H264 = 0, VENC_HEVC = 1 }; @@ -52,4 +52,30 @@ struct venc_touch { uint32_t x, y; }; +struct vdec_config { + uint32_t type; + uint32_t codec; + uint32_t width, height; + uint32_t stride, rows; + uint32_t bitstreams, bitstream_size; +}; + +struct vdec_capture { + uint32_t type; + uint32_t index; + uint32_t size; +}; + +struct vdec_packet { + uint32_t type; + uint32_t input, bytes, capture; + int64_t pts_us; +}; + +struct vdec_result { + uint32_t type; + int32_t status; + uint32_t frame; +}; + #endif diff --git a/app/src/main/cpp/venc/venc_server.cpp b/app/src/main/cpp/venc/venc_server.cpp index be484c898d..17a5d7eed1 100644 --- a/app/src/main/cpp/venc/venc_server.cpp +++ b/app/src/main/cpp/venc/venc_server.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -11,6 +12,7 @@ #include #include +#include #include #include #include @@ -45,6 +47,218 @@ int32_t avc_profile(int32_t v4l2) { } } +struct DmaBufSync { + uint64_t flags; +}; +constexpr unsigned long kDmaBufIoctlSync = _IOW('b', 0, DmaBufSync); +constexpr uint64_t kDmaBufSyncWrite = 2, kDmaBufSyncStart = 0, kDmaBufSyncEnd = 4; +constexpr int32_t kColorFormatYuv420Planar = 19; + +class Decoder { +public: + ~Decoder() { release(); } + + void release() { + if (codec_) { + AMediaCodec_stop(codec_); + AMediaCodec_delete(codec_); + codec_ = nullptr; + } + if (bitstream_) munmap((void *)bitstream_, bitstream_size_); + bitstream_ = nullptr; + for (auto &c : captures_) drop(c); + } + + bool configure(const vdec_config &c, int fd) { + release(); + if (c.width < 16 || c.height < 16 || c.stride < c.width || c.rows < c.height || c.bitstreams == 0 || + c.bitstreams > VENC_MAX_BUFFERS || c.bitstream_size == 0) { + LOG("bad decoder config: %ux%u stride %u rows %u, %u bitstreams", c.width, c.height, c.stride, c.rows, + c.bitstreams); + return false; + } + bitstream_size_ = (size_t)c.bitstreams * c.bitstream_size; + void *p = mmap(nullptr, bitstream_size_, PROT_READ, MAP_SHARED, fd, 0); + if (p == MAP_FAILED) { + LOG("could not map the bitstream buffers: %s", strerror(errno)); + return false; + } + bitstream_ = (const uint8_t *)p; + config_ = c; + codec_ = create(c, true); + if (!codec_) codec_ = create(c, false); + if (!codec_) { + LOG("no %s decoder for %ux%u", c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height); + release(); + return false; + } + src_stride_ = (int32_t)c.width; + src_rows_ = (int32_t)c.height; + src_width_ = (int32_t)c.width; + src_height_ = (int32_t)c.height; + src_planar_ = false; + shown_ = false; + using GetName = media_status_t (*)(AMediaCodec *, char **); + using ReleaseName = void (*)(AMediaCodec *, char *); + static auto get_name = (GetName)dlsym(RTLD_DEFAULT, "AMediaCodec_getName"); + static auto release_name = (ReleaseName)dlsym(RTLD_DEFAULT, "AMediaCodec_releaseName"); + char *name = nullptr; + if (get_name && get_name(codec_, &name) != AMEDIA_OK) name = nullptr; + LOG("decoding %s %ux%u with %s", c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height, + name ? name : "MediaCodec"); + if (name && release_name) release_name(codec_, name); + return true; + } + + void capture(const vdec_capture &c, int fd) { + if (c.index >= VENC_MAX_BUFFERS) { + close(fd); + return; + } + Capture &cap = captures_[c.index]; + drop(cap); + void *p = mmap(nullptr, c.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (p == MAP_FAILED) { + LOG("could not map capture buffer %u: %s", c.index, strerror(errno)); + close(fd); + return; + } + cap = {fd, (uint8_t *)p, c.size}; + } + + bool decode(const vdec_packet &p, bool *frame) { + *frame = false; + if (!codec_ || p.capture >= VENC_MAX_BUFFERS || !captures_[p.capture].data || p.input >= config_.bitstreams || + p.bytes == 0 || p.bytes > config_.bitstream_size) + return false; + auto start = std::chrono::steady_clock::now(); + ssize_t in = AMediaCodec_dequeueInputBuffer(codec_, 20000); + if (in < 0) return false; + size_t capacity = 0; + uint8_t *dst = AMediaCodec_getInputBuffer(codec_, (size_t)in, &capacity); + if (!dst || capacity < p.bytes) { + AMediaCodec_queueInputBuffer(codec_, (size_t)in, 0, 0, 0, 0); + return false; + } + memcpy(dst, bitstream_ + (size_t)p.input * config_.bitstream_size, p.bytes); + if (AMediaCodec_queueInputBuffer(codec_, (size_t)in, 0, p.bytes, (uint64_t)std::max(p.pts_us, 0), 0) != + AMEDIA_OK) + return false; + auto wait = std::chrono::milliseconds(shown_ ? 25 : 250); + while (std::chrono::steady_clock::now() - start < wait) { + AMediaCodecBufferInfo info; + ssize_t o = AMediaCodec_dequeueOutputBuffer(codec_, &info, 2000); + if (o == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) { + read_format(); + continue; + } + if (o < 0) continue; + size_t size = 0; + uint8_t *data = AMediaCodec_getOutputBuffer(codec_, (size_t)o, &size); + if (data && info.size > 0 && (size_t)info.offset + (size_t)info.size <= size) + *frame = copy(data + info.offset, (size_t)info.size, captures_[p.capture]) || *frame; + AMediaCodec_releaseOutputBuffer(codec_, (size_t)o, false); + if (info.presentationTimeUs >= p.pts_us) break; + } + shown_ = shown_ || *frame; + return true; + } + +private: + struct Capture { + int fd = -1; + uint8_t *data = nullptr; + size_t size = 0; + }; + + static void drop(Capture &c) { + if (c.data) munmap(c.data, c.size); + if (c.fd >= 0) close(c.fd); + c = Capture(); + } + + AMediaCodec *create(const vdec_config &c, bool low_latency) { + const char *mime = c.codec == VENC_HEVC ? "video/hevc" : "video/avc"; + AMediaCodec *codec = AMediaCodec_createDecoderByType(mime); + if (!codec) return nullptr; + AMediaFormat *f = AMediaFormat_new(); + AMediaFormat_setString(f, AMEDIAFORMAT_KEY_MIME, mime); + AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_WIDTH, (int32_t)c.width); + AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_HEIGHT, (int32_t)c.height); + AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_COLOR_FORMAT, kColorFormatYuv420SemiPlanar); + AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_MAX_INPUT_SIZE, (int32_t)c.bitstream_size); + if (low_latency) { + AMediaFormat_setInt32(f, "low-latency", 1); + AMediaFormat_setInt32(f, "vendor.qti-ext-dec-low-latency.enable", 1); + AMediaFormat_setInt32(f, "vendor.qti-ext-dec-picture-order.enable", 1); + AMediaFormat_setInt32(f, "priority", 0); + AMediaFormat_setFloat(f, "operating-rate", 120.0f); + } + media_status_t s = AMediaCodec_configure(codec, f, nullptr, nullptr, 0); + AMediaFormat_delete(f); + if (s == AMEDIA_OK) s = AMediaCodec_start(codec); + if (s != AMEDIA_OK) { + AMediaCodec_delete(codec); + return nullptr; + } + return codec; + } + + void read_format() { + AMediaFormat *f = AMediaCodec_getOutputFormat(codec_); + if (!f) return; + int32_t v; + if (AMediaFormat_getInt32(f, AMEDIAFORMAT_KEY_WIDTH, &v) && v > 0) src_width_ = v; + if (AMediaFormat_getInt32(f, AMEDIAFORMAT_KEY_HEIGHT, &v) && v > 0) src_height_ = v; + src_stride_ = AMediaFormat_getInt32(f, "stride", &v) && v >= src_width_ ? v : src_width_; + src_rows_ = AMediaFormat_getInt32(f, "slice-height", &v) && v >= src_height_ ? v : src_height_; + src_planar_ = AMediaFormat_getInt32(f, AMEDIAFORMAT_KEY_COLOR_FORMAT, &v) && v == kColorFormatYuv420Planar; + LOG("decoder output %dx%d stride %d rows %d %s", src_width_, src_height_, src_stride_, src_rows_, + src_planar_ ? "planar" : "semi-planar"); + AMediaFormat_delete(f); + } + + bool copy(const uint8_t *src, size_t size, const Capture &cap) { + size_t luma = (size_t)src_stride_ * src_rows_; + size_t chroma = src_planar_ ? 2 * (size_t)(src_stride_ / 2) * (src_rows_ / 2) : (size_t)src_stride_ * (src_rows_ / 2); + if (size < luma + chroma) return false; + uint32_t w = std::min((uint32_t)src_width_, config_.stride); + uint32_t h = std::min((uint32_t)src_height_, config_.rows); + if ((size_t)config_.stride * config_.rows * 3 / 2 > cap.size) return false; + DmaBufSync sync = {kDmaBufSyncStart | kDmaBufSyncWrite}; + ioctl(cap.fd, kDmaBufIoctlSync, &sync); + uint8_t *y = cap.data, *uv = cap.data + (size_t)config_.stride * config_.rows; + for (uint32_t row = 0; row < h; row++) + memcpy(y + (size_t)row * config_.stride, src + (size_t)row * src_stride_, w); + const uint8_t *su = src + luma; + for (uint32_t row = 0; row < h / 2; row++) { + uint8_t *d = uv + (size_t)row * config_.stride; + if (!src_planar_) { + memcpy(d, su + (size_t)row * src_stride_, w & ~1u); + continue; + } + const uint8_t *u = su + (size_t)row * (src_stride_ / 2); + const uint8_t *vv = su + (size_t)(src_stride_ / 2) * (src_rows_ / 2) + (size_t)row * (src_stride_ / 2); + for (uint32_t x = 0; x < w / 2; x++) { + d[2 * x] = u[x]; + d[2 * x + 1] = vv[x]; + } + } + sync.flags = kDmaBufSyncEnd | kDmaBufSyncWrite; + ioctl(cap.fd, kDmaBufIoctlSync, &sync); + return true; + } + + AMediaCodec *codec_ = nullptr; + vdec_config config_ = {}; + const uint8_t *bitstream_ = nullptr; + size_t bitstream_size_ = 0; + Capture captures_[VENC_MAX_BUFFERS]; + int32_t src_width_ = 0, src_height_ = 0, src_stride_ = 0, src_rows_ = 0; + bool src_planar_ = false; + bool shown_ = false; +}; + class Session { public: explicit Session(int fd) : fd_(fd) {} @@ -85,6 +299,24 @@ public: memcpy(&b, buf, sizeof b); set_bitrate(b.bitrate); for (int i = 0; i < nfds; i++) close(fds[i]); + } else if (type == VDEC_CONFIG && n == (ssize_t)sizeof(vdec_config)) { + vdec_config c; + memcpy(&c, buf, sizeof c); + bool ok = nfds == 1 && decoder_.configure(c, fds[0]); + for (int i = 0; i < nfds; i++) close(fds[i]); + decoded(ok ? 0 : -1, false); + } else if (type == VDEC_CAPTURE && n == (ssize_t)sizeof(vdec_capture)) { + vdec_capture c; + memcpy(&c, buf, sizeof c); + if (nfds == 1) decoder_.capture(c, fds[0]); + else for (int i = 0; i < nfds; i++) close(fds[i]); + } else if (type == VDEC_PACKET && n == (ssize_t)sizeof(vdec_packet)) { + vdec_packet p; + memcpy(&p, buf, sizeof p); + bool frame = false; + bool ok = decoder_.decode(p, &frame); + for (int i = 0; i < nfds; i++) close(fds[i]); + decoded(ok ? 0 : -1, frame); } else { LOG("unexpected message %u of %zd bytes; closing", type, n); for (int i = 0; i < nfds; i++) close(fds[i]); @@ -117,7 +349,13 @@ private: send(fd_, &d, sizeof d, MSG_NOSIGNAL); } + void decoded(int32_t status, bool frame) { + vdec_result r = {VDEC_RESULT, status, frame ? 1u : 0u}; + send(fd_, &r, sizeof r, MSG_NOSIGNAL); + } + void release() { + decoder_.release(); if (codec_) { AMediaCodec_stop(codec_); AMediaCodec_delete(codec_); @@ -292,6 +530,7 @@ private: int32_t dst_stride_ = 0, dst_rows_ = 0; std::vector inputs_, outputs_; std::vector header_; + Decoder decoder_; }; std::atomic g_listener{-1}; diff --git a/tools/linuxfs/preload/netif.c b/tools/linuxfs/preload/netif.c index 1782d0f0d1..872a877e11 100644 --- a/tools/linuxfs/preload/netif.c +++ b/tools/linuxfs/preload/netif.c @@ -407,6 +407,7 @@ int bl_kgsl_poll(int (*real)(int, unsigned long, void *), int fd, unsigned long int bl_ntsync_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); int bl_drm_gem_close(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); int bl_venc_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); +int bl_vdec_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); int bl_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg, void *caller, int *rc) __attribute__((visibility("hidden"))); int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg, @@ -423,6 +424,7 @@ int ioctl(int fd, unsigned long request, ...) { int rc; if (bl_ntsync_ioctl(fd, request, arg, &rc)) return rc; if (bl_venc_ioctl(fd, request, arg, &rc)) return rc; + if (bl_vdec_ioctl(fd, request, arg, &rc)) return rc; if (bl_drm_gem_close(fd, request, arg, &rc)) return rc; if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl"); if (bl_winebus_rumble_ioctl(real, fd, request, arg, __builtin_return_address(0), &rc)) return rc; diff --git a/tools/linuxfs/preload/ntsync.c b/tools/linuxfs/preload/ntsync.c index d49bb42b74..4fa0d6c2c5 100644 --- a/tools/linuxfs/preload/ntsync.c +++ b/tools/linuxfs/preload/ntsync.c @@ -1459,10 +1459,12 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags) { void bl_fsync_fds_closed(unsigned int first, unsigned int last) __attribute__((visibility("hidden"))); void bl_venc_closed(int fd) __attribute__((visibility("hidden"))); +void bl_vdec_closed(int fd) __attribute__((visibility("hidden"))); int close(int fd) { if (fd >= 0) bl_fsync_fds_closed((unsigned int)fd, (unsigned int)fd); bl_venc_closed(fd); + bl_vdec_closed(fd); uint64_t *t = __atomic_load_n(&ns_fdt, __ATOMIC_ACQUIRE); if (t && fd >= 0 && fd < NS_FDS && __atomic_load_n(&t[fd], __ATOMIC_ACQUIRE)) ns_forget(fd); return ns_close_fd(fd); diff --git a/tools/linuxfs/preload/opens.c b/tools/linuxfs/preload/opens.c index 269f013913..332f9d89a5 100644 --- a/tools/linuxfs/preload/opens.c +++ b/tools/linuxfs/preload/opens.c @@ -15,6 +15,7 @@ int bl_status_without_tracer(const char *path, int flags, const void *caller) __ int bl_writable_retry(int dirfd, const char *path, int flags) __attribute__((visibility("hidden"))); int bl_ntsync_open(const char *path, int flags) __attribute__((visibility("hidden"))); int bl_venc_open(const char *path, int flags) __attribute__((visibility("hidden"))); +int bl_vdec_open(const char *path, int flags) __attribute__((visibility("hidden"))); int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode, int (*real)(int, const char *, int, mode_t)) __attribute__((visibility("hidden"))); @@ -55,6 +56,7 @@ int open(const char *path, int flags, ...) { if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd; if ((fd = bl_ntsync_open(path, flags)) != -2) return fd; if ((fd = bl_venc_open(path, flags)) != -2) return fd; + if ((fd = bl_vdec_open(path, flags)) != -2) return fd; if (real_open == NULL) real_open = dlsym(RTLD_NEXT, "open"); if (real_open == NULL) { errno = ENOSYS; @@ -81,6 +83,7 @@ int openat(int dirfd, const char *path, int flags, ...) { if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd; if ((fd = bl_ntsync_open(path, flags)) != -2) return fd; if ((fd = bl_venc_open(path, flags)) != -2) return fd; + if ((fd = bl_vdec_open(path, flags)) != -2) return fd; if (real_openat == NULL) real_openat = dlsym(RTLD_NEXT, "openat"); if (real_openat == NULL) { errno = ENOSYS; diff --git a/tools/linuxfs/preload/vdec.c b/tools/linuxfs/preload/vdec.c new file mode 100644 index 0000000000..4606563611 --- /dev/null +++ b/tools/linuxfs/preload/vdec.c @@ -0,0 +1,973 @@ +#define _GNU_SOURCE +#include +#include +#include +#include + +#if defined(__aarch64__) || defined(BL_VDEC_ANY_ARCH) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../../app/src/main/cpp/venc/venc_proto.h" + +#define VDEC_DEVICE "/dev/video-dec0" +#define VDEC_INSTANCES 4 +#define VDEC_BUFFERS 16 +#define VDEC_BITSTREAM_SIZE (4u << 20) +#define VDEC_PADDING 64 +#define VDEC_DUMB 64 + +struct vd_dma_heap_alloc { + uint64_t len; + uint32_t fd; + uint32_t fd_flags; + uint64_t heap_flags; +}; +#define VD_DMA_HEAP_IOCTL_ALLOC _IOWR('H', 0x0, struct vd_dma_heap_alloc) + +struct vd_dma_buf_sync { + uint64_t flags; +}; +#define VD_DMA_BUF_SYNC_WRITE 2u +#define VD_DMA_BUF_SYNC_START 0u +#define VD_DMA_BUF_SYNC_END 4u +#define VD_DMA_BUF_IOCTL_SYNC _IOW('b', 0, struct vd_dma_buf_sync) + +struct vd_create_dumb { + uint32_t height, width, bpp, flags, handle, pitch; + uint64_t size; +}; +struct vd_destroy_dumb { + uint32_t handle; +}; +struct vd_prime_handle { + uint32_t handle, flags; + int32_t fd; +}; +#define VD_IOCTL_MODE_CREATE_DUMB _IOWR('d', 0xB2, struct vd_create_dumb) +#define VD_IOCTL_MODE_DESTROY_DUMB _IOWR('d', 0xB4, struct vd_destroy_dumb) +#define VD_IOCTL_PRIME_HANDLE_TO_FD _IOWR('d', 0x2d, struct vd_prime_handle) + +struct vd_mode_info { + uint32_t clock; + uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew; + uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan; + uint32_t vrefresh, flags, type; + char name[32]; +}; +struct vd_res { + int count_fbs; + uint32_t *fbs; + int count_crtcs; + uint32_t *crtcs; + int count_connectors; + uint32_t *connectors; + int count_encoders; + uint32_t *encoders; + uint32_t min_width, max_width, min_height, max_height; +}; +struct vd_connector { + uint32_t connector_id, encoder_id, connector_type, connector_type_id; + int connection; + uint32_t mm_width, mm_height; + int subpixel; + int count_modes; + struct vd_mode_info *modes; + int count_props; + uint32_t *props; + uint64_t *prop_values; + int count_encoders; + uint32_t *encoders; +}; +struct vd_encoder { + uint32_t encoder_id, encoder_type, crtc_id, possible_crtcs, possible_clones; +}; +struct vd_crtc { + uint32_t crtc_id, buffer_id, x, y, width, height; + int mode_valid; + struct vd_mode_info mode; + int gamma_size; +}; +struct vd_device { + char **nodes; + int available_nodes; + int bustype; + void *businfo; + void *deviceinfo; +}; + +struct vd_capture { + int fd; + ino_t ino; + uint8_t *map; + size_t len; +}; + +struct vd_done { + uint32_t index; + struct timeval timestamp; +}; + +struct vdec { + int used, fd; + uint32_t fourcc, width, height; + uint32_t cap_width, cap_height, bpl, cap_size; + uint8_t *bitstream; + uint32_t out_count; + uint32_t out_done[VDEC_BUFFERS], n_out_done; + struct vd_capture cap[VDEC_BUFFERS]; + uint32_t cap_count; + uint32_t cap_queue[VDEC_BUFFERS], n_cap_queue; + struct vd_done cap_done[VDEC_BUFFERS]; + uint32_t n_cap_done; + int out_on, cap_on; + int sock, hw, hw_failed; + int64_t pts; + void *ctx, *packet, *frame; + uint8_t *packet_data; + size_t packet_capacity; + int last; + uint32_t frames, slow; + double decode_ms; +}; + +static struct { + void *(*find_decoder_by_name)(const char *); + void *(*alloc_context3)(const void *); + int (*open2)(void *, const void *, void **); + void (*free_context)(void **); + int (*send_packet)(void *, const void *); + int (*receive_frame)(void *, void *); + void (*flush_buffers)(void *); + void *(*packet_alloc)(void); + void (*packet_free)(void **); + void *(*frame_alloc)(void); + void (*frame_free)(void **); + void (*frame_unref)(void *); + int (*dict_set)(void **, const char *, const char *, int); + void (*dict_free)(void **); + const char *(*pix_fmt_name)(int); +} av; + +static pthread_mutex_t vd_lock = PTHREAD_MUTEX_INITIALIZER; +static struct vdec vd_all[VDEC_INSTANCES]; +static int vd_open_count; +static int vd_state; +static int vd_ffmpeg; +static int vd_heap = -1; +static int vd_dumb_fd[VDEC_DUMB]; +static char vd_drm_handle; + +static void vd_log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); +static void vd_log(const char *fmt, ...) { + va_list ap; + char line[256]; + va_start(ap, fmt); + int n = vsnprintf(line, sizeof line - 1, fmt, ap); + va_end(ap); + if (n < 0) return; + if (n > (int)sizeof line - 2) n = sizeof line - 2; + line[n++] = '\n'; + if (write(2, "droiddeck-vdec: ", 16) < 0 || write(2, line, n) < 0) return; +} + +static int vd_load_ffmpeg(void) { + void *codec = dlopen("libavcodec.so.62", RTLD_LAZY | RTLD_LOCAL); + void *util = codec ? dlopen("libavutil.so.60", RTLD_LAZY | RTLD_LOCAL) : NULL; + if (codec == NULL || util == NULL) { + vd_log("FFmpeg 8 is not available (%s), Remote Play decoding stays off", dlerror()); + return 0; + } +#define VD_SYM(lib, field, name) \ + if ((*(void **)&av.field = dlsym(lib, name)) == NULL) { \ + vd_log("FFmpeg has no %s, Remote Play decoding stays off", name); \ + return 0; \ + } + VD_SYM(codec, find_decoder_by_name, "avcodec_find_decoder_by_name") + VD_SYM(codec, alloc_context3, "avcodec_alloc_context3") + VD_SYM(codec, open2, "avcodec_open2") + VD_SYM(codec, free_context, "avcodec_free_context") + VD_SYM(codec, send_packet, "avcodec_send_packet") + VD_SYM(codec, receive_frame, "avcodec_receive_frame") + VD_SYM(codec, flush_buffers, "avcodec_flush_buffers") + VD_SYM(codec, packet_alloc, "av_packet_alloc") + VD_SYM(codec, packet_free, "av_packet_free") + VD_SYM(util, frame_alloc, "av_frame_alloc") + VD_SYM(util, frame_free, "av_frame_free") + VD_SYM(util, frame_unref, "av_frame_unref") + VD_SYM(util, dict_set, "av_dict_set") + VD_SYM(util, dict_free, "av_dict_free") + VD_SYM(util, pix_fmt_name, "av_get_pix_fmt_name") +#undef VD_SYM + return 1; +} + +static int vd_have_ffmpeg(void) { + if (vd_ffmpeg == 0) vd_ffmpeg = vd_load_ffmpeg() ? 1 : -1; + return vd_ffmpeg > 0; +} + +__attribute__((visibility("hidden"))) int bl_venc_connect(void); + +static int vd_enabled(void) { + pthread_mutex_lock(&vd_lock); + if (vd_state == 0) { + const char *flag = getenv("BL_REMOTE_PLAY_DECODE"), *socket = getenv("BL_VENC_SOCKET"); + vd_state = -1; + if (strcmp(program_invocation_short_name, "streaming_client") == 0 && (flag == NULL || strcmp(flag, "0") != 0)) { + vd_heap = open("/dev/dma_heap/system", O_RDONLY | O_CLOEXEC); + if (vd_heap < 0) vd_log("no /dev/dma_heap/system (%s), Remote Play decoding stays off", strerror(errno)); + else if ((socket && *socket) || vd_have_ffmpeg()) vd_state = 1; + for (int i = 0; i < VDEC_DUMB; i++) vd_dumb_fd[i] = -1; + } + } + int on = vd_state > 0; + pthread_mutex_unlock(&vd_lock); + return on; +} + +static struct vdec *vd_find(int fd) { + if (fd < 0 || __atomic_load_n(&vd_open_count, __ATOMIC_ACQUIRE) == 0) return NULL; + for (int i = 0; i < VDEC_INSTANCES; i++) if (vd_all[i].used && vd_all[i].fd == fd) return &vd_all[i]; + return NULL; +} + +__attribute__((visibility("hidden"))) int bl_vdec_open(const char *path, int flags) { + (void)flags; + if (path == NULL || strcmp(path, VDEC_DEVICE) != 0 || !vd_enabled()) return -2; + int fd = memfd_create("droiddeck-vdec", 0); + if (fd < 0) return -1; + pthread_mutex_lock(&vd_lock); + struct vdec *v = NULL; + for (int i = 0; i < VDEC_INSTANCES && v == NULL; i++) if (!vd_all[i].used) v = &vd_all[i]; + if (v == NULL) { + pthread_mutex_unlock(&vd_lock); + close(fd); + errno = EBUSY; + return -1; + } + memset(v, 0, sizeof *v); + v->used = 1; + v->fd = fd; + v->fourcc = V4L2_PIX_FMT_H264; + v->last = -1; + v->sock = -1; + for (int i = 0; i < VDEC_BUFFERS; i++) v->cap[i].fd = -1; + __atomic_add_fetch(&vd_open_count, 1, __ATOMIC_RELEASE); + pthread_mutex_unlock(&vd_lock); + return fd; +} + +static void vd_close_decoder(struct vdec *v) { + if (v->frames) { + vd_log("%u frames, %.1f ms each on average, %u over 25 ms", v->frames, v->decode_ms / v->frames, v->slow); + } + if (v->sock >= 0) close(v->sock); + v->sock = -1; + v->hw = 0; + if (v->ctx) av.free_context(&v->ctx); + if (v->packet) av.packet_free(&v->packet); + if (v->frame) av.frame_free(&v->frame); + free(v->packet_data); + v->packet_data = NULL; + v->packet_capacity = 0; + v->frames = v->slow = 0; + v->decode_ms = 0; + v->last = -1; +} + +static int vd_send(int sock, const void *msg, size_t len, int fd) { + struct iovec iov = { .iov_base = (void *)msg, .iov_len = len }; + char control[CMSG_SPACE(sizeof(int))]; + struct msghdr m = { .msg_iov = &iov, .msg_iovlen = 1 }; + if (fd >= 0) { + memset(control, 0, sizeof control); + m.msg_control = control; + m.msg_controllen = sizeof control; + struct cmsghdr *c = CMSG_FIRSTHDR(&m); + c->cmsg_level = SOL_SOCKET; + c->cmsg_type = SCM_RIGHTS; + c->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(c), &fd, sizeof fd); + } + return sendmsg(sock, &m, MSG_NOSIGNAL) == (ssize_t)len ? 0 : -1; +} + +static void vd_send_capture(struct vdec *v, uint32_t index) { + struct vdec_capture c = { .type = VDEC_CAPTURE, .index = index, .size = (uint32_t)v->cap[index].len }; + if (v->sock >= 0 && v->cap[index].map && vd_send(v->sock, &c, sizeof c, v->cap[index].fd) != 0) + vd_log("could not hand capture buffer %u to the app: %s", index, strerror(errno)); +} + +static int vd_open_hardware(struct vdec *v) { + if (v->hw_failed) return -1; + int sock = bl_venc_connect(); + if (sock < 0) return -1; + struct vdec_config c = { + .type = VDEC_CONFIG, .codec = v->fourcc == V4L2_PIX_FMT_HEVC ? VENC_HEVC : VENC_H264, + .width = v->width ? v->width : v->cap_width, .height = v->height ? v->height : v->cap_height, + .stride = v->bpl, .rows = v->cap_height, .bitstreams = v->out_count, .bitstream_size = VDEC_BITSTREAM_SIZE, + }; + struct vdec_result r; + if (vd_send(sock, &c, sizeof c, v->fd) != 0 || recv(sock, &r, sizeof r, 0) != (ssize_t)sizeof r || + r.type != VDEC_RESULT || r.status != 0) { + vd_log("the app has no %s decoder for %ux%u", c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height); + close(sock); + v->hw_failed = 1; + return -1; + } + v->sock = sock; + v->hw = 1; + for (uint32_t i = 0; i < v->cap_count; i++) vd_send_capture(v, i); + vd_log("decoding %s %ux%u with the app's MediaCodec into %ux%u NV12 dma-bufs", + c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height, v->cap_width, v->cap_height); + return 0; +} + +static int vd_open_decoder(struct vdec *v) { + if (vd_open_hardware(v) == 0) return 0; + if (!vd_have_ffmpeg()) return -1; + const char *name = v->fourcc == V4L2_PIX_FMT_HEVC ? "hevc" : "h264"; + const void *codec = av.find_decoder_by_name(name); + if (codec == NULL) { + vd_log("FFmpeg has no %s decoder", name); + return -1; + } + v->ctx = av.alloc_context3(codec); + v->packet = av.packet_alloc(); + v->frame = av.frame_alloc(); + void *options = NULL; + long cores = sysconf(_SC_NPROCESSORS_ONLN); + char threads[8]; + snprintf(threads, sizeof threads, "%ld", cores > 4 ? 4L : cores < 1 ? 1L : cores); + av.dict_set(&options, "threads", threads, 0); + av.dict_set(&options, "thread_type", "slice", 0); + av.dict_set(&options, "flags", "+low_delay", 0); + av.dict_set(&options, "flags2", "+fast", 0); + int r = v->ctx && v->packet && v->frame ? av.open2(v->ctx, codec, &options) : -1; + av.dict_free(&options); + if (r < 0) { + vd_log("could not open the %s decoder (%d)", name, r); + vd_close_decoder(v); + return -1; + } + vd_log("decoding %s %ux%u in software into %ux%u NV12 dma-bufs", v->fourcc == V4L2_PIX_FMT_HEVC ? "HEVC" : "H.264", + v->width, v->height, v->cap_width, v->cap_height); + return 0; +} + +static void vd_unmap_capture(struct vdec *v) { + for (uint32_t i = 0; i < VDEC_BUFFERS; i++) { + if (v->cap[i].map) munmap(v->cap[i].map, v->cap[i].len); + v->cap[i] = (struct vd_capture){ .fd = -1 }; + } + v->cap_count = v->n_cap_queue = v->n_cap_done = 0; + v->last = -1; +} + +static void vd_sync(int fd, uint64_t flags) { + struct vd_dma_buf_sync s = { .flags = flags | VD_DMA_BUF_SYNC_WRITE }; + ioctl(fd, VD_DMA_BUF_IOCTL_SYNC, &s); +} + +static void vd_blank(struct vdec *v, struct vd_capture *c) { + size_t luma = (size_t)v->bpl * v->cap_height; + if (luma + luma / 2 > c->len) return; + vd_sync(c->fd, VD_DMA_BUF_SYNC_START); + memset(c->map, 16, luma); + memset(c->map + luma, 128, luma / 2); + vd_sync(c->fd, VD_DMA_BUF_SYNC_END); +} + +static int vd_map_capture(struct vdec *v, uint32_t index, int fd) { + struct stat st; + if (fd < 0 || fstat(fd, &st) != 0) return -EINVAL; + struct vd_capture *c = &v->cap[index]; + if (c->map && c->ino == st.st_ino) { + c->fd = fd; + return 0; + } + if (c->map) munmap(c->map, c->len); + off_t len = lseek(fd, 0, SEEK_END); + if (len < (off_t)v->cap_size) return -EINVAL; + void *p = mmap(NULL, (size_t)len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (p == MAP_FAILED) { + vd_log("could not map capture buffer %u: %s", index, strerror(errno)); + *c = (struct vd_capture){ .fd = -1 }; + return -EINVAL; + } + *c = (struct vd_capture){ .fd = fd, .ino = st.st_ino, .map = p, .len = (size_t)len }; + vd_blank(v, c); + if (v->hw) vd_send_capture(v, index); + return 0; +} + +static int vd_write_frame(struct vdec *v, struct vd_capture *c) { + uint8_t **data = (uint8_t **)v->frame; + int *linesize = (int *)((uint8_t *)v->frame + 64); + int width = *(int *)((uint8_t *)v->frame + 104), height = *(int *)((uint8_t *)v->frame + 108); + const char *format = av.pix_fmt_name(*(int *)((uint8_t *)v->frame + 116)); + if (format == NULL || width <= 0 || height <= 0) return -1; + int nv12 = strcmp(format, "nv12") == 0; + if (!nv12 && strcmp(format, "yuv420p") != 0 && strcmp(format, "yuvj420p") != 0) { + static int warned; + if (!warned++) vd_log("cannot show %s frames", format); + return -1; + } + uint32_t w = (uint32_t)width < v->bpl ? (uint32_t)width : v->bpl; + uint32_t h = (uint32_t)height < v->cap_height ? (uint32_t)height : v->cap_height; + uint8_t *y = c->map, *uv = c->map + (size_t)v->bpl * v->cap_height; + vd_sync(c->fd, VD_DMA_BUF_SYNC_START); + for (uint32_t row = 0; row < h; row++) memcpy(y + (size_t)row * v->bpl, data[0] + (size_t)row * linesize[0], w); + for (uint32_t row = 0; row < (h + 1) / 2; row++) { + uint8_t *dst = uv + (size_t)row * v->bpl; + if (nv12) { + memcpy(dst, data[1] + (size_t)row * linesize[1], w & ~1u); + continue; + } + const uint8_t *u = data[1] + (size_t)row * linesize[1], *vv = data[2] + (size_t)row * linesize[2]; + for (uint32_t x = 0; x < w / 2; x++) { + dst[2 * x] = u[x]; + dst[2 * x + 1] = vv[x]; + } + } + vd_sync(c->fd, VD_DMA_BUF_SYNC_END); + return 0; +} + +static void vd_copy_last(struct vdec *v, struct vd_capture *c) { + if (v->last < 0 || v->cap[v->last].map == NULL || &v->cap[v->last] == c) return; + size_t size = v->cap_size < c->len ? v->cap_size : c->len; + vd_sync(c->fd, VD_DMA_BUF_SYNC_START); + memcpy(c->map, v->cap[v->last].map, size); + vd_sync(c->fd, VD_DMA_BUF_SYNC_END); +} + +static double vd_now_ms(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1000.0 + ts.tv_nsec / 1e6; +} + +static void vd_deliver(struct vdec *v, int written, double start, struct timeval timestamp) { + uint32_t target = v->cap_queue[0]; + if (!written) { + if (v->last < 0) return; + vd_copy_last(v, &v->cap[target]); + } + double ms = vd_now_ms() - start; + v->frames++; + v->decode_ms += ms; + if (ms > 25.0) v->slow++; + memmove(v->cap_queue, v->cap_queue + 1, --v->n_cap_queue * sizeof v->cap_queue[0]); + v->cap_done[v->n_cap_done++] = (struct vd_done){ .index = target, .timestamp = timestamp }; + v->last = (int)target; +} + +static void vd_decode_hardware(struct vdec *v, uint32_t index, uint32_t bytes, struct timeval timestamp) { + double start = vd_now_ms(); + v->pts += 16667; + struct vdec_packet p = { .type = VDEC_PACKET, .input = index, .bytes = bytes, .capture = v->cap_queue[0], .pts_us = v->pts }; + struct vdec_result r; + if (vd_send(v->sock, &p, sizeof p, -1) != 0 || recv(v->sock, &r, sizeof r, 0) != (ssize_t)sizeof r || + r.type != VDEC_RESULT) { + vd_log("lost the app's decoder (%s), switching to software", strerror(errno)); + vd_close_decoder(v); + v->hw_failed = 1; + return; + } + vd_deliver(v, r.status == 0 && r.frame, start, timestamp); +} + +static void vd_decode(struct vdec *v, uint32_t index, uint32_t bytes, struct timeval timestamp) { + if (!v->cap_on || v->n_cap_queue == 0) return; + if (v->ctx == NULL && !v->hw && vd_open_decoder(v) != 0) return; + if (v->hw) { + vd_decode_hardware(v, index, bytes, timestamp); + return; + } + if (v->packet_capacity < bytes + VDEC_PADDING) { + free(v->packet_data); + v->packet_capacity = bytes + VDEC_PADDING + (64u << 10); + v->packet_data = malloc(v->packet_capacity); + if (v->packet_data == NULL) { + v->packet_capacity = 0; + return; + } + } + memcpy(v->packet_data, v->bitstream + (size_t)index * VDEC_BITSTREAM_SIZE, bytes); + memset(v->packet_data + bytes, 0, VDEC_PADDING); + *(uint8_t **)((uint8_t *)v->packet + 24) = v->packet_data; + *(int *)((uint8_t *)v->packet + 32) = (int)bytes; + double start = vd_now_ms(); + uint32_t target = v->cap_queue[0]; + struct vd_capture *c = &v->cap[target]; + int written = 0; + if (av.send_packet(v->ctx, v->packet) >= 0) { + while (av.receive_frame(v->ctx, v->frame) >= 0) { + if (vd_write_frame(v, c) == 0) written = 1; + av.frame_unref(v->frame); + } + } + *(uint8_t **)((uint8_t *)v->packet + 24) = NULL; + *(int *)((uint8_t *)v->packet + 32) = 0; + vd_deliver(v, written, start, timestamp); +} + +static int is_output(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || type == V4L2_BUF_TYPE_VIDEO_OUTPUT; } +static int is_capture(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || type == V4L2_BUF_TYPE_VIDEO_CAPTURE; } + +static void vd_fill_format(struct vdec *v, struct v4l2_format *f) { + struct v4l2_pix_format_mplane *p = &f->fmt.pix_mp; + memset(p, 0, sizeof *p); + p->field = V4L2_FIELD_NONE; + p->num_planes = 1; + p->colorspace = V4L2_COLORSPACE_REC709; + if (is_output(f->type)) { + p->width = v->width; + p->height = v->height; + p->pixelformat = v->fourcc; + p->plane_fmt[0].sizeimage = VDEC_BITSTREAM_SIZE; + } else { + p->width = v->cap_width; + p->height = v->cap_height; + p->pixelformat = V4L2_PIX_FMT_NV12; + p->plane_fmt[0].bytesperline = v->bpl; + p->plane_fmt[0].sizeimage = v->cap_size; + } +} + +static int vd_set_format(struct vdec *v, struct v4l2_format *f, int apply) { + struct v4l2_pix_format_mplane *p = &f->fmt.pix_mp; + if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) return -EINVAL; + struct vdec t = *v; + if (is_output(f->type)) { + if (p->pixelformat != V4L2_PIX_FMT_H264 && p->pixelformat != V4L2_PIX_FMT_HEVC) return -EINVAL; + t.fourcc = p->pixelformat; + t.width = p->width; + t.height = p->height; + } else { + uint32_t w = p->width > t.width ? p->width : t.width, h = p->height > t.height ? p->height : t.height; + if (w < 16 || h < 16 || w > 8192 || h > 8192) return -EINVAL; + t.cap_width = (w + 15) & ~15u; + t.cap_height = (h + 15) & ~15u; + t.bpl = t.cap_width; + t.cap_size = t.bpl * t.cap_height * 3 / 2; + } + if (apply) { + if (t.fourcc != v->fourcc) vd_close_decoder(&t); + *v = t; + } + vd_fill_format(&t, f); + return 0; +} + +static int vd_reqbufs(struct vdec *v, struct v4l2_requestbuffers *r) { + uint32_t n = r->count > VDEC_BUFFERS ? VDEC_BUFFERS : r->count; + if (r->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && r->memory == V4L2_MEMORY_MMAP) { + if (v->bitstream) munmap(v->bitstream, (size_t)v->out_count * VDEC_BITSTREAM_SIZE); + v->bitstream = NULL; + v->out_count = v->n_out_done = 0; + if (n && ftruncate(v->fd, (off_t)n * VDEC_BITSTREAM_SIZE) != 0) return -ENOMEM; + if (n) { + void *p = mmap(NULL, (size_t)n * VDEC_BITSTREAM_SIZE, PROT_READ, MAP_SHARED, v->fd, 0); + if (p == MAP_FAILED) return -ENOMEM; + v->bitstream = p; + } + v->out_count = n; + } else if (r->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && r->memory == V4L2_MEMORY_DMABUF) { + vd_unmap_capture(v); + v->cap_count = n; + } else { + return -EINVAL; + } + r->count = n; + r->capabilities = r->memory == V4L2_MEMORY_MMAP ? V4L2_BUF_CAP_SUPPORTS_MMAP : V4L2_BUF_CAP_SUPPORTS_DMABUF; + return 0; +} + +static int vd_querybuf(struct vdec *v, struct v4l2_buffer *b) { + if (b->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || b->index >= v->out_count || b->m.planes == NULL || b->length < 1) + return -EINVAL; + b->memory = V4L2_MEMORY_MMAP; + b->flags = V4L2_BUF_FLAG_MAPPED; + b->length = 1; + b->m.planes[0].length = VDEC_BITSTREAM_SIZE; + b->m.planes[0].m.mem_offset = b->index * VDEC_BITSTREAM_SIZE; + return 0; +} + +static int vd_qbuf(struct vdec *v, struct v4l2_buffer *b) { + if (b->m.planes == NULL || b->length < 1) return -EINVAL; + if (b->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { + if (b->index >= v->out_count || v->n_out_done >= VDEC_BUFFERS) return -EINVAL; + uint32_t bytes = b->m.planes[0].bytesused; + if (bytes > VDEC_BITSTREAM_SIZE) bytes = VDEC_BITSTREAM_SIZE; + if (v->out_on && bytes) vd_decode(v, b->index, bytes, b->timestamp); + v->out_done[v->n_out_done++] = b->index; + } else if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { + if (b->index >= v->cap_count || v->cap_size == 0) return -EINVAL; + int r = vd_map_capture(v, b->index, b->m.planes[0].m.fd); + if (r < 0) return r; + for (uint32_t i = 0; i < v->n_cap_queue; i++) if (v->cap_queue[i] == b->index) return 0; + if (v->n_cap_queue >= VDEC_BUFFERS) return -EINVAL; + v->cap_queue[v->n_cap_queue++] = b->index; + } else { + return -EINVAL; + } + b->flags |= V4L2_BUF_FLAG_QUEUED; + return 0; +} + +static int vd_dqbuf(struct vdec *v, struct v4l2_buffer *b) { + if (b->m.planes == NULL || b->length < 1) return -EINVAL; + if (b->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { + if (v->n_out_done == 0) return -EAGAIN; + b->index = v->out_done[0]; + memmove(v->out_done, v->out_done + 1, --v->n_out_done * sizeof v->out_done[0]); + b->memory = V4L2_MEMORY_MMAP; + b->flags = V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_MAPPED; + b->length = 1; + b->m.planes[0].bytesused = 0; + b->m.planes[0].length = VDEC_BITSTREAM_SIZE; + b->m.planes[0].m.mem_offset = b->index * VDEC_BITSTREAM_SIZE; + return 0; + } + if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) return -EINVAL; + if (v->n_cap_done == 0) return -EAGAIN; + struct vd_done d = v->cap_done[0]; + memmove(v->cap_done, v->cap_done + 1, --v->n_cap_done * sizeof d); + b->index = d.index; + b->memory = V4L2_MEMORY_DMABUF; + b->flags = V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_TIMESTAMP_COPY; + b->field = V4L2_FIELD_NONE; + b->timestamp = d.timestamp; + b->length = 1; + b->m.planes[0].bytesused = v->cap_size; + b->m.planes[0].length = v->cap_size; + b->m.planes[0].m.fd = v->cap[d.index].fd; + return 0; +} + +static int vd_ioctl(struct vdec *v, unsigned long req, void *arg) { + switch (req) { + case VIDIOC_QUERYCAP: { + struct v4l2_capability *c = arg; + memset(c, 0, sizeof *c); + strcpy((char *)c->driver, "droiddeck_vdec"); + strcpy((char *)c->card, "DroidDeck video decoder"); + strcpy((char *)c->bus_info, "platform:droiddeck-vdec"); + c->version = 0x00060000; + c->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; + c->capabilities = c->device_caps | V4L2_CAP_DEVICE_CAPS; + return 0; + } + case VIDIOC_ENUM_FMT: { + struct v4l2_fmtdesc *d = arg; + static const uint32_t coded[] = { V4L2_PIX_FMT_H264, V4L2_PIX_FMT_HEVC }; + if (is_capture(d->type) && d->index == 0) { + d->pixelformat = V4L2_PIX_FMT_NV12; + strcpy((char *)d->description, "Y/CbCr 4:2:0"); + d->flags = 0; + } else if (is_output(d->type) && d->index < 2) { + d->pixelformat = coded[d->index]; + strcpy((char *)d->description, d->index ? "HEVC" : "H.264"); + d->flags = V4L2_FMT_FLAG_COMPRESSED; + } else { + return -EINVAL; + } + return 0; + } + case VIDIOC_G_FMT: { + struct v4l2_format *f = arg; + if (!is_output(f->type) && !is_capture(f->type)) return -EINVAL; + vd_fill_format(v, f); + return 0; + } + case VIDIOC_S_FMT: return vd_set_format(v, arg, 1); + case VIDIOC_TRY_FMT: return vd_set_format(v, arg, 0); + case VIDIOC_REQBUFS: return vd_reqbufs(v, arg); + case VIDIOC_QUERYBUF: return vd_querybuf(v, arg); + case VIDIOC_QBUF: return vd_qbuf(v, arg); + case VIDIOC_DQBUF: return vd_dqbuf(v, arg); + case VIDIOC_STREAMON: + case VIDIOC_STREAMOFF: { + uint32_t type = *(uint32_t *)arg; + int on = req == VIDIOC_STREAMON; + if (is_output(type)) { + v->out_on = on; + if (!on) { + v->n_out_done = 0; + if (v->ctx) av.flush_buffers(v->ctx); + } + } else if (is_capture(type)) { + v->cap_on = on; + if (!on) v->n_cap_queue = v->n_cap_done = 0; + } else { + return -EINVAL; + } + if (v->out_on && v->cap_on && v->ctx == NULL && !v->hw && v->out_count && v->cap_size) vd_open_decoder(v); + return 0; + } + case VIDIOC_SUBSCRIBE_EVENT: + case VIDIOC_UNSUBSCRIBE_EVENT: + case VIDIOC_DECODER_CMD: + case VIDIOC_TRY_DECODER_CMD: + return 0; + case VIDIOC_DQEVENT: + return -ENOENT; + default: + vd_log("unhandled ioctl 0x%lx", req); + return -ENOTTY; + } +} + +__attribute__((visibility("hidden"))) int bl_vdec_ioctl(int fd, unsigned long req, void *arg, int *rc) { + struct vdec *v = vd_find(fd); + if (v == NULL) return 0; + pthread_mutex_lock(&vd_lock); + int r = arg ? vd_ioctl(v, req, arg) : -EFAULT; + pthread_mutex_unlock(&vd_lock); + if (r < 0) { + errno = -r; + *rc = -1; + } else { + *rc = 0; + } + return 1; +} + +__attribute__((visibility("hidden"))) void bl_vdec_closed(int fd) { + struct vdec *v = vd_find(fd); + if (v == NULL) return; + pthread_mutex_lock(&vd_lock); + vd_close_decoder(v); + vd_unmap_capture(v); + if (v->bitstream) munmap(v->bitstream, (size_t)v->out_count * VDEC_BITSTREAM_SIZE); + v->bitstream = NULL; + v->used = 0; + __atomic_sub_fetch(&vd_open_count, 1, __ATOMIC_RELEASE); + pthread_mutex_unlock(&vd_lock); +} + +__attribute__((visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms, + int (*real)(struct pollfd *, nfds_t, int), int *ret) { + if (n != 1 || vd_find(fds[0].fd) == NULL) return 0; + struct vdec *v = vd_find(fds[0].fd); + pthread_mutex_lock(&vd_lock); + short ready = (v->n_cap_done ? POLLIN | POLLRDNORM : 0) | (v->n_out_done ? POLLOUT | POLLWRNORM : 0); + pthread_mutex_unlock(&vd_lock); + fds[0].revents = ready & fds[0].events; + if (fds[0].revents == 0 && timeout_ms != 0) real(NULL, 0, timeout_ms < 0 || timeout_ms > 1000 ? 1000 : timeout_ms); + *ret = fds[0].revents ? 1 : 0; + return 1; +} + +static int drm_get_devices2(uint32_t flags, struct vd_device **devices, int max) { + (void)flags; + static char *nodes[3] = { "/dev/null", NULL, NULL }; + static struct vd_device device = { .nodes = nodes, .available_nodes = 1 }; + if (devices == NULL || max < 1) return 1; + devices[0] = &device; + return 1; +} + +static void drm_free_devices(struct vd_device **devices, int count) { (void)devices; (void)count; } + +static struct vd_mode_info drm_mode = { + .clock = 148500, .hdisplay = 1920, .hsync_start = 2008, .hsync_end = 2052, .htotal = 2200, + .vdisplay = 1080, .vsync_start = 1084, .vsync_end = 1089, .vtotal = 1125, .vrefresh = 60, .name = "1920x1080", +}; +static uint32_t drm_crtc_ids[1] = { 1 }, drm_connector_ids[1] = { 2 }, drm_encoder_ids[1] = { 3 }; + +static struct vd_res *drm_get_resources(int fd) { + (void)fd; + static struct vd_res res = { + .count_crtcs = 1, .crtcs = drm_crtc_ids, .count_connectors = 1, .connectors = drm_connector_ids, + .count_encoders = 1, .encoders = drm_encoder_ids, .max_width = 8192, .max_height = 8192, + }; + return &res; +} + +static struct vd_connector *drm_get_connector(int fd, uint32_t id) { + (void)fd; + static struct vd_connector connector = { + .connector_id = 2, .encoder_id = 3, .connector_type = 14, .connection = 1, .count_modes = 1, + .modes = &drm_mode, .count_encoders = 1, .encoders = drm_encoder_ids, + }; + return id == 2 ? &connector : NULL; +} + +static struct vd_encoder *drm_get_encoder(int fd, uint32_t id) { + (void)fd; + static struct vd_encoder encoder = { .encoder_id = 3, .encoder_type = 8, .crtc_id = 1, .possible_crtcs = 1 }; + return id == 3 ? &encoder : NULL; +} + +static struct vd_crtc *drm_get_crtc(int fd, uint32_t id) { + (void)fd; + static struct vd_crtc crtc; + if (id != 1) return NULL; + crtc = (struct vd_crtc){ .crtc_id = 1, .width = 1920, .height = 1080, .mode_valid = 1, .mode = drm_mode }; + return &crtc; +} + +static void drm_free(void *p) { (void)p; } +static void *drm_null(void) { return NULL; } +static int drm_fail(void) { return -EINVAL; } + +static int drm_dumb_slot(uint32_t handle) { return handle >= 1 && handle <= VDEC_DUMB ? (int)handle - 1 : -1; } + +static int drm_ioctl(int fd, unsigned long request, void *arg) { + (void)fd; + int r = -EINVAL; + pthread_mutex_lock(&vd_lock); + if (request == VD_IOCTL_MODE_CREATE_DUMB && arg) { + struct vd_create_dumb *c = arg; + uint64_t pitch = ((uint64_t)c->width * c->bpp + 7) / 8; + uint64_t size = (pitch * c->height + 4095) & ~4095ull; + int slot = -1; + for (int i = 0; i < VDEC_DUMB && slot < 0; i++) if (vd_dumb_fd[i] < 0) slot = i; + struct vd_dma_heap_alloc a = { .len = size, .fd_flags = O_RDWR | O_CLOEXEC }; + if (slot < 0 || size == 0 || size > (512ull << 20)) { + r = -ENOMEM; + } else if (ioctl(vd_heap, VD_DMA_HEAP_IOCTL_ALLOC, &a) != 0) { + r = -errno; + vd_log("dma-heap allocation of %llu bytes failed: %s", (unsigned long long)size, strerror(errno)); + } else { + vd_dumb_fd[slot] = (int)a.fd; + c->handle = (uint32_t)slot + 1; + c->pitch = (uint32_t)pitch; + c->size = size; + r = 0; + } + } else if (request == VD_IOCTL_PRIME_HANDLE_TO_FD && arg) { + struct vd_prime_handle *h = arg; + int slot = drm_dumb_slot(h->handle); + int dup_fd = slot >= 0 && vd_dumb_fd[slot] >= 0 ? fcntl(vd_dumb_fd[slot], F_DUPFD_CLOEXEC, 0) : -1; + if (dup_fd >= 0) { + h->fd = dup_fd; + r = 0; + } else { + r = slot >= 0 ? -errno : -ENOENT; + } + } else if (request == VD_IOCTL_MODE_DESTROY_DUMB && arg) { + int slot = drm_dumb_slot(((struct vd_destroy_dumb *)arg)->handle); + if (slot >= 0 && vd_dumb_fd[slot] >= 0) { + close(vd_dumb_fd[slot]); + vd_dumb_fd[slot] = -1; + r = 0; + } else { + r = -ENOENT; + } + } + pthread_mutex_unlock(&vd_lock); + if (r < 0) { + errno = -r; + return -1; + } + return 0; +} + +static const struct { + const char *name; + void *fn; +} drm_table[] = { + { "drmGetDevices2", (void *)drm_get_devices2 }, + { "drmFreeDevices", (void *)drm_free_devices }, + { "drmSetMaster", (void *)drm_fail }, + { "drmSetClientCap", (void *)drm_fail }, + { "drmModeGetResources", (void *)drm_get_resources }, + { "drmModeFreeResources", (void *)drm_free }, + { "drmModeGetConnector", (void *)drm_get_connector }, + { "drmModeFreeConnector", (void *)drm_free }, + { "drmModeGetEncoder", (void *)drm_get_encoder }, + { "drmModeFreeEncoder", (void *)drm_free }, + { "drmModeGetCrtc", (void *)drm_get_crtc }, + { "drmModeSetCrtc", (void *)drm_fail }, + { "drmModeFreeCrtc", (void *)drm_free }, + { "drmModeGetPlaneResources", (void *)drm_null }, + { "drmModeFreePlaneResources", (void *)drm_free }, + { "drmModeGetPlane", (void *)drm_null }, + { "drmModeFreePlane", (void *)drm_free }, + { "drmModeObjectGetProperties", (void *)drm_null }, + { "drmModeFreeObjectProperties", (void *)drm_free }, + { "drmModeGetProperty", (void *)drm_null }, + { "drmModeFreeProperty", (void *)drm_free }, + { "drmModeAddFB2", (void *)drm_fail }, + { "drmModeAddFB2WithModifiers", (void *)drm_fail }, + { "drmModeRmFB", (void *)drm_fail }, + { "drmModeAtomicAlloc", (void *)drm_null }, + { "drmModeAtomicFree", (void *)drm_free }, + { "drmModeAtomicAddProperty", (void *)drm_fail }, + { "drmModeAtomicCommit", (void *)drm_fail }, + { "drmIoctl", (void *)drm_ioctl }, +}; + +void *SDL_LoadObject(const char *name) { + static void *(*real)(const char *); + if (name && strcmp(name, "libdrm.so.2") == 0 && vd_enabled()) return &vd_drm_handle; + if (!real) real = (void *(*)(const char *))dlsym(RTLD_NEXT, "SDL_LoadObject"); + return real ? real(name) : NULL; +} + +void *SDL_LoadFunction(void *handle, const char *name) { + static void *(*real)(void *, const char *); + if (handle == &vd_drm_handle) { + for (size_t i = 0; name && i < sizeof drm_table / sizeof drm_table[0]; i++) + if (strcmp(drm_table[i].name, name) == 0) return drm_table[i].fn; + return NULL; + } + if (!real) real = (void *(*)(void *, const char *))dlsym(RTLD_NEXT, "SDL_LoadFunction"); + return real ? real(handle, name) : NULL; +} + +void SDL_UnloadObject(void *handle) { + static void (*real)(void *); + if (handle == &vd_drm_handle) return; + if (!real) real = (void (*)(void *))dlsym(RTLD_NEXT, "SDL_UnloadObject"); + if (real) real(handle); +} + +#else + +__attribute__((visibility("hidden"))) int bl_vdec_open(const char *path, int flags) { + (void)path; + (void)flags; + return -2; +} + +__attribute__((visibility("hidden"))) int bl_vdec_ioctl(int fd, unsigned long req, void *arg, int *rc) { + (void)fd; + (void)req; + (void)arg; + (void)rc; + return 0; +} + +__attribute__((visibility("hidden"))) void bl_vdec_closed(int fd) { (void)fd; } + +__attribute__((visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms, + int (*real)(struct pollfd *, nfds_t, int), int *ret) { + (void)fds; + (void)n; + (void)timeout_ms; + (void)real; + (void)ret; + return 0; +} + +#endif diff --git a/tools/linuxfs/preload/venc.c b/tools/linuxfs/preload/venc.c index ca610e85c3..9480e88126 100644 --- a/tools/linuxfs/preload/venc.c +++ b/tools/linuxfs/preload/venc.c @@ -625,9 +625,14 @@ static int vn_poll(struct pollfd *fds, nfds_t n, int timeout_ms, int (*real)(str return r + ready; } +__attribute__((weak, visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms, + int (*real)(struct pollfd *, nfds_t, int), int *ret); + int poll(struct pollfd *fds, nfds_t n, int timeout_ms) { static int (*real)(struct pollfd *, nfds_t, int); if (!real) real = (int (*)(struct pollfd *, nfds_t, int))dlsym(RTLD_NEXT, "poll"); + int ret; + if (bl_vdec_poll && bl_vdec_poll(fds, n, timeout_ms, real, &ret)) return ret; if (__atomic_load_n(&vn_open_count, __ATOMIC_ACQUIRE) == 0) return real(fds, n, timeout_ms); return vn_poll(fds, n, timeout_ms, real); } diff --git a/tools/tests/test_vdec_emulator.py b/tools/tests/test_vdec_emulator.py new file mode 100644 index 0000000000..a4c4d4e3dc --- /dev/null +++ b/tools/tests/test_vdec_emulator.py @@ -0,0 +1,258 @@ +import array +import mmap +import os +import socket +import struct +import subprocess +import tempfile +import threading +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +VDEC = ROOT / 'tools/linuxfs/preload/vdec.c' +VENC = ROOT / 'tools/linuxfs/preload/venc.c' + +DRIVER = r''' +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int bl_vdec_open(const char *path, int flags); +int bl_vdec_ioctl(int fd, unsigned long req, void *arg, int *rc); +void bl_vdec_closed(int fd); +void *SDL_LoadObject(const char *name); +void *SDL_LoadFunction(void *handle, const char *name); + +struct heap_alloc { uint64_t len; uint32_t fd, fd_flags; uint64_t heap_flags; }; +struct create_dumb { uint32_t height, width, bpp, flags, handle, pitch; uint64_t size; }; +struct prime_handle { uint32_t handle, flags; int32_t fd; }; +struct destroy_dumb { uint32_t handle; }; +struct drm_device { char **nodes; int available_nodes; }; + +static int heap = -1; + +int open(const char *path, int flags, ...) { + static int (*real)(const char *, int, ...); + va_list ap; + va_start(ap, flags); + int mode = va_arg(ap, int); + va_end(ap); + if (!real) real = dlsym(RTLD_NEXT, "open"); + if (strcmp(path, "/dev/dma_heap/system") == 0) return heap = memfd_create("heap", 0); + return real(path, flags, mode); +} + +int ioctl(int fd, unsigned long req, ...) { + static int (*real)(int, unsigned long, ...); + va_list ap; + va_start(ap, req); + void *arg = va_arg(ap, void *); + va_end(ap); + if (!real) real = dlsym(RTLD_NEXT, "ioctl"); + if (fd == heap && req == _IOWR('H', 0, struct heap_alloc)) { + struct heap_alloc *a = arg; + int buf = memfd_create("dmabuf", 0); + if (ftruncate(buf, (off_t)a->len) != 0) return -1; + a->fd = (uint32_t)buf; + return 0; + } + if (req == _IOW('b', 0, uint64_t)) return 0; + return real(fd, req, arg); +} + +static int vfd; +static int call(unsigned long req, void *arg) { + int rc; + if (!bl_vdec_ioctl(vfd, req, arg, &rc)) return -100; + return rc < 0 ? -errno : 0; +} + +int main(void) { + if (bl_vdec_open("/dev/video0", O_RDWR) != -2) return 2; + void *drm = SDL_LoadObject("libdrm.so.2"); + if (!drm) return 3; + int (*devices)(uint32_t, struct drm_device **, int) = SDL_LoadFunction(drm, "drmGetDevices2"); + int (*drm_ioctl)(int, unsigned long, void *) = SDL_LoadFunction(drm, "drmIoctl"); + if (!devices || !drm_ioctl || !SDL_LoadFunction(drm, "drmModeAtomicCommit")) return 4; + struct drm_device *dev[1]; + if (devices(0, NULL, 0) != 1 || devices(0, dev, 1) != 1 || !(dev[0]->available_nodes & 1)) return 5; + int card = open(dev[0]->nodes[0], O_RDWR | O_CLOEXEC); + if (card < 0) return 6; + + vfd = bl_vdec_open("/dev/video-dec0", O_RDWR); + if (vfd < 0) return 7; + struct v4l2_capability cap; + if (call(VIDIOC_QUERYCAP, &cap) || (cap.capabilities & 0x04004000) != 0x04004000) return 8; + struct v4l2_fmtdesc d = { .index = 1, .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE }; + if (call(VIDIOC_ENUM_FMT, &d) || d.pixelformat != V4L2_PIX_FMT_HEVC) return 9; + struct v4l2_format f = { .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE }; + f.fmt.pix_mp.width = 1280; f.fmt.pix_mp.height = 720; f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_HEVC; + if (call(VIDIOC_S_FMT, &f)) return 10; + struct v4l2_requestbuffers r = { .count = 4, .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, .memory = V4L2_MEMORY_MMAP }; + if (call(VIDIOC_REQBUFS, &r) || r.count != 4) return 11; + unsigned char *in[4]; + for (int i = 0; i < 4; i++) { + struct v4l2_plane p = { 0 }; + struct v4l2_buffer b = { .index = i, .type = r.type, .memory = V4L2_MEMORY_MMAP, .length = 1, .m.planes = &p }; + if (call(VIDIOC_QUERYBUF, &b)) return 12; + in[i] = mmap(NULL, p.length, PROT_READ | PROT_WRITE, MAP_SHARED, vfd, p.m.mem_offset); + if (in[i] == MAP_FAILED) return 13; + } + f = (struct v4l2_format){ .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE }; + f.fmt.pix_mp.width = 1280; f.fmt.pix_mp.height = 736; f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12; + if (call(VIDIOC_S_FMT, &f) || f.fmt.pix_mp.num_planes != 1) return 14; + uint32_t bpl = f.fmt.pix_mp.plane_fmt[0].bytesperline, size = f.fmt.pix_mp.plane_fmt[0].sizeimage; + if (bpl != 1280 || size != 1280 * 736 * 3 / 2) return 15; + r = (struct v4l2_requestbuffers){ .count = 3, .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, .memory = V4L2_MEMORY_DMABUF }; + if (call(VIDIOC_REQBUFS, &r) || r.count != 3) return 16; + int out[3]; + for (int i = 0; i < 3; i++) { + struct create_dumb c = { .height = 736, .width = 1280, .bpp = 16 }; + if (drm_ioctl(card, 0xc02064b2, &c) || !c.handle || c.size < size) return 17; + struct prime_handle h = { .handle = c.handle, .flags = O_CLOEXEC }; + if (drm_ioctl(card, 0xc00c642d, &h)) return 18; + struct destroy_dumb x = { .handle = c.handle }; + if (drm_ioctl(card, 0xc00464b4, &x)) return 19; + out[i] = h.fd; + } + unsigned types[2] = { V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE }; + if (call(VIDIOC_STREAMON, &types[0]) || call(VIDIOC_STREAMON, &types[1])) return 20; + for (int i = 0; i < 3; i++) { + struct v4l2_plane p = { .m.fd = out[i] }; + struct v4l2_buffer b = { .index = i, .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &p }; + if (call(VIDIOC_QBUF, &b)) return 21; + } + struct pollfd pf = { .fd = vfd, .events = 0xc9 }; + if (poll(&pf, 1, 0) != 0) return 22; + for (int frame = 1; frame <= 5; frame++) { + int slot = frame % 4; + memset(in[slot], 'a' + frame, 100 + frame); + struct v4l2_plane p = { .bytesused = 100 + frame }; + struct v4l2_buffer b = { .index = slot, .type = types[0], .memory = V4L2_MEMORY_MMAP, .flags = 0x4008, .length = 1, .m.planes = &p }; + b.timestamp.tv_usec = frame; + if (call(VIDIOC_QBUF, &b)) return 23; + pf = (struct pollfd){ .fd = vfd, .events = 0xc9 }; + if (poll(&pf, 1, 30) != 1 || !(pf.revents & POLLIN)) return 24; + struct v4l2_plane cp = { 0 }; + struct v4l2_buffer cb = { .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &cp }; + if (call(VIDIOC_DQBUF, &cb)) return 25; + unsigned char *m = mmap(NULL, size, PROT_READ, MAP_SHARED, out[cb.index], 0); + printf("%ld %u %c%c %u\n", (long)cb.timestamp.tv_usec, cb.index, m[0], m[(size_t)bpl * 736], cp.bytesused == size); + munmap(m, size); + pf = (struct pollfd){ .fd = vfd, .events = 0x306 }; + if (poll(&pf, 1, 0) != 1) return 26; + struct v4l2_plane op = { 0 }; + struct v4l2_buffer ob = { .type = types[0], .memory = V4L2_MEMORY_MMAP, .length = 1, .m.planes = &op }; + if (call(VIDIOC_DQBUF, &ob) || ob.index != (unsigned)slot) return 27; + struct v4l2_plane rp = { .m.fd = out[cb.index] }; + struct v4l2_buffer rb = { .index = cb.index, .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &rp }; + if (call(VIDIOC_QBUF, &rb)) return 28; + } + bl_vdec_closed(vfd); + return 0; +} +''' + +CONFIG = struct.Struct('