Files
DroidDeck/tools/linuxfs/preload/vdec.c
T
2d25fb91c5 Run Easy Anti-Cheat and BattlEye games on x86-64 DroidDeck-Proton under FEX (#3641)
* FEX: emulate x86-64 ptrace, seccomp and /proc for anti-cheat launchers

* Guest hook: route raw execve/execveat through the exec wrapper and pass self-sent SIGSYS to the program

A program that execs with syscall(SYS_execveat, ...) bypassed the environment the
guest needs, so re-executing /proc/self/exe died in the loader. /proc/self/exe now
maps to the real program on exec as it already does on open.

A SIGSYS the program queues itself, or one from a filter for another architecture,
went to the Android-filter emulation instead of the program's handler.

* FEX: list only guest mappings in /proc/<pid>/maps on hosts below 47 address bits

Credit the anti-cheat emulation source in the patch header.

* Preloads: leave poll, ppoll and select buffers untouched when no emulated device is open

A bad user pointer now reaches the kernel and returns EFAULT instead of crashing the caller inside the wrapper.

* Emulate only host-architecture seccomp traps in the guest hook; report the calling thread's seccomp mode in FEX's /proc/self/status

A SIGSYS carrying another architecture's seccomp siginfo now takes its default action when the program has no handler, so a process whose emulated filter traps a syscall while SIGSYS is blocked or ignored dies as on Linux.

* FEX: read the host status file through the raw openat syscall

* README: credit StonkGuy's FEX anti-cheat emulation

* FEX: open the host status file with a direct system call so preloads cannot rewrite it

* FEX: hand a traced program's ptrace state across exec

* Credit StonkGuy in the FEX anti-cheat patches and README

Co-authored-by: StonkGuy <68372320+StonkGuy@users.noreply.github.com>

* FEX: open guest paths with the raw openat system call so host preloads cannot replace the file

* FEX: serve emulated proc files by the requested path when the host returns a memfd

* FEX: map fixed guest memory above a small host address space

Co-authored-by: StonkGuy <StonkGuy@users.noreply.github.com>

* FEX: serve decoder reads from fixed guest memory above the host limit

* Run games with anti-cheat files on an x86-64 DroidDeck-Proton under FEX

The Proton launchers look for Easy Anti-Cheat or BattlEye bridge files in
the game folder and, when an x86-64 DroidDeck-Proton and the matching Proton
runtime are installed, hand the launch to that Proton under FEX with the
game's own prefix. Picking the x86-64 tool directly also runs it under FEX.
DROIDDECK_ANTICHEAT=off keeps a game on ARM64.

* Keep x86-64 DroidDeck-Proton builds out of the ARM64 tool list

* droiddeck-fex: take the runtime from an adopted tool's original manifest

* FEX: show the guest command line when the argument area cannot be remapped

* FEX: describe the container's procfs in emulated mount tables

* FEX: order the anti-cheat series after the guest stack patch from main

* Test the container procfs view of FEX's emulated mount tables

* Docs: describe the anti-cheat signal mask fix and the container procfs view

* README: match the StonkGuy credit to the renumbered FEX patches

* Keep anti-cheat games on a tool the user chose instead of moving them to x86-64

* test(game-env): build the adopted-tool wrapper through extra_wrapper_sh

* Check anti-cheat runtime bridges, apply component choices under FEX and honour MAP_FIXED_NOREPLACE above the host limit

* Check the driver query is answered before each wrapper's call sites

* Keep x86-64 Proton builds out of the automatic default tool

* Keep high guest mappings intact when a fixed map or unmap fails

Check room in the region table, splits included, before a fixed map or
unmap removes anything, return the host munmap error without touching
the table entry, and keep write-protecting low shared aliases of
executable ranges that sit above the host address limit.

* Keep a requested x86-64 default and accept linked anti-cheat runtimes

A request for the current automatic x86-64 default now marks it as the
app's choice, so the next settle no longer moves it back to ARM64.
Anti-cheat runtime discovery follows a symlinked runtime directory.

* Route anti-cheat games to x86-64 Proton only on a capable FEX

The FEX build now lists its patches in share/fex-emu/.droiddeck-patches.
Anti-cheat routing needs the fixed guest memory patch in that list and
otherwise keeps the launch on ARM64, as an older FEX cannot map the
anti-cheat helper buffer above the host address limit. Installing a FEX
build without the list removes a list left by an earlier build.

* Update FEX before deciding anti-cheat routing

A device on an older FEX build never went through droiddeck-fex on the
ARM64 path, so the catalog FEX was never installed and the gate kept
anti-cheat games on ARM64 forever. dd_anticheat now runs the new
droiddeck-fex update action first, which installs the catalog build
under the same lock and retry window as other FEX launches.

* Leave MAP_FIXED_NOREPLACE above the host limit to the host

Wine finds the top of the address space with MAP_FIXED_NOREPLACE probes.
Claiming those probes above a 39-bit host limit made Wine place its stacks
in relocated memory, and every wineserver request then failed with EFAULT
because the iovec buffers it passes to writev are nested pointers that are
not translated. Only MAP_FIXED requests are relocated again, so the probes
see the real host limit while fixed mappings above it keep working.

* Translate only pointer syscall arguments into high guest memory

Pointer arguments are now picked from a per-syscall layout table, so
scalar values such as a truncate length are never rewritten. Sized
buffers must map to one contiguous host range or the call fails with
EFAULT, and new host backings are placed next to their guest
neighbours so adjacent mappings stay contiguous. readv, writev and the
preadv/pwritev family, sendmsg and recvmsg get a host-side copy of
their iovec and msghdr with translated bases, leaving the guest copies
untouched. Code invalidation for kernel writes maps the host buffer
back to its guest address, and an unmap checks the table before it
changes any host mapping.

* Claim high guest memory only with LSE atomics and invalidate every code alias

High guest mappings are claimed only when the JIT emits LSE atomics, and
exclusive load/store instructions are never single-stepped, so an atomic
on high memory cannot spin. A store into high code invalidates every
mirror of the written page through the tracked mapping. madvise and
mprotect on high memory apply to each region at its own host address.

* Reject wrapping lengths and drop stale code on discarded high memory

A nonzero length that wraps when page-aligned now fails madvise with
EINVAL and mprotect with ENOMEM, as on Linux. Discarding advice on
high memory invalidates translated code for the range whenever advice
reached any region.

* Add an end-to-end probe for guest memory above the host address limit

* Emulate exclusive pairs on high guest memory and release failed host unmaps later

* Size counted guest buffers by element and keep exclusive access within guest protection

poll, ppoll, epoll waits, mmsg calls and select bitmaps now translate the whole array their count describes, so a buffer that crosses into an unrelated host range fails with EFAULT and oversized counts cannot wrap. High regions remember their protection through mprotect, splitting at the range edges, and exclusive stores to read-only pages or loads from inaccessible pages reach the guest as a fault instead of being emulated.

* Refuse a high unmap when failed host unmaps could not all be retained

UnmapLocked counts the overlapping regions before it changes anything and returns ENOMEM when the retired list could not hold all of them, so a host range is never dropped from tracking while still mapped.

* Serve memory trim requests while syscall user dispatch blocks

A thread that runs with syscall user dispatch on and its selector set to
block (as x86-64 Wine does while in Windows code) took the trim signal
with the selector still blocking. Every raw syscall the handler made was
then dispatched to the program's SIGSYS handler, which either deadlocked
the thread or killed the process when SIGSYS was blocked, so the session
saw it as a deaf process and the game stalled during loading.

Track the selector each thread registers through prctl, allow syscalls
for the length of the handler and restore the selector before returning.

* Allow syscalls for a forwarded prctl while dispatch blocks

The prctl wrapper issues its syscall from the preload's own text, so a
thread whose selector blocks got SIGSYS where the plain libc call would
have been dispatched the same way by the caller's own rules. Allow
syscalls for the forwarded call and restore the selector afterwards.

The dispatch test now waits until the selector blocks before it sends
the trim request, and checks a prctl call made while it blocks.

* Merge high regions after protection changes and translate message batches

Protect now merges neighbouring high regions whose guest and host
ranges touch and whose protection and code state match, so toggling
page protections no longer fills the region table.

Exclusive loads on high memory accept write-only pages and exclusive
stores need only PROT_WRITE, matching x86-64 where writable pages are
readable.

sendmmsg and recvmmsg translate the name, control and io vectors of
every message in the batch, return msg_len for each processed message
and the received header fields for recvmmsg.

Counted arrays passed to getgroups, setgroups, semop, semtimedop,
io_submit, io_getevents, io_pgetevents, move_pages, futex_waitv and
listmount are now checked to stay within one host range. The unused
ToHost helper is removed.

* Translate fixed-size, string and in-array guest pointers in high memory

Give every syscall pointer without a length a fixed x86-64 size, a
length read through another argument, or a terminator scan for strings,
so spans crossing separately backed regions are refused before the
kernel sees them. Size message queue buffers as the type word plus
msgsz. Translate io_submit control blocks and their buffers, including
vectored reads and writes, through bounded host copies, restore the
guest words afterwards, and report guest iocb addresses back from
io_getevents. Translate move_pages page lists for the calling process
and futex_waitv addresses. Merge new mappings with matching neighbours.

* Size high-memory syscall spans by kernel widths and embedded sizes

Read counts with the width the kernel uses for each argument, so int
and unsigned int counts ignore the upper register bits and negative
counts reach the handler. Size capability data by its header version,
perf and scheduler attributes, file handles and mount requests by their
own size fields, and landlock rules by rule type. Keep the translated
prefix of a message batch, read headers that span separate backings one
by one, write results back through the current guest mapping, and stop
counting messages whose length cannot be stored. Revisit an entry moved
into an earlier slot while merging regions. Give newer syscalls
fallback numbers in the test harness.

* Send translated message batches one message at a time

Submit sendmmsg batches that need scratch headers one message per call,
storing each length before the next message goes out, so a length that
cannot be stored stops the batch where Linux would. Skip the name length
when no name was given and store flags before the control length. Check
the whole fixed file-handle header even when its size is invalid. Hand
capget and capset a private header so the checked data size matches the
version the kernel reads, and return a rewritten version to the guest.
Patch and restore AIO control blocks under the mapping lock and restore
only the block that was patched.

* Harden high-memory aio, capability and message batch write-backs

Restore patched aio buffers through /proc/self/mem when the control block
became read-only and keep the scratch vectors alive if that fails too,
fault capability calls whose header write-back fails, stop send batches
after a short message, and pin the batch socket with a private dup.

* Pin batch sockets, fall back to whole batches and reconcile pending aio restores

Follow-up message sends and the first dispatch share one duplicated
socket. When it cannot be duplicated, the whole translated batch goes
to the kernel in one call and lengths are written back up to the first
one that cannot be stored. Short sends compare against the iovec
lengths given to the kernel. Unrestorable aio_buf edits, including
unreadable control blocks, keep their storage in a bounded list that
later submissions reconcile.

* Stop message batches at unreadable vectors, send singly without a spare descriptor and reserve aio restore slots

A message whose iovec snapshot cannot be taken ends the prepared batch
instead of being sent with an unknown length; when it is the first
message the kernel handles the original array natively. When the
batch socket cannot be duplicated, messages still go one at a time on
the original descriptor with each length written before the next send.
Pending aio restore slots are reserved under the pending mutex before
the first guest edit and released or handed to the pending record, so
concurrent submissions cannot exceed the bound. Tests use a real
socket pair, injected descriptor exhaustion and concurrent
reservations.

* Send only the first message of a batch when the socket cannot be pinned

* Replay high-memory faults over the whole access and reconcile aio restores on unmap

* Keep high-memory traps ordered, fault-exact and locked against unmap

Protect, Map and low mmap/munmap/mremap/mprotect reconcile pending aio
restores at once, and io_submit reconciles them even when no high region
is mapped. Ordered loads and stores split across backings are emulated
between full fences instead of being byte-copied through the trampoline;
atomics and exclusives are still refused. Gather and scatter use
fault-safe copies, and a failing piece is re-faulted at the guest
instruction through a probe load, with SIGBUS redelivered the same way.
The VMA lock is held from planning until the scatter finishes, so unmap
and protect cannot race a resumed access. Sign-extending loads are no
longer classified as writes.

* Defer user signals while guest high memory mappings are held

Only the trampoline breakpoint completes a replay, user-sent SIGSEGV,
SIGBUS and SIGTRAP raised while the mapping lock is held are requeued
after it is released, and stranded control block words are restored
when their page turns readable again. The harness now runs under a
timeout.

* Rebase the FEX patch series onto FEX-2612

* FEX: restore stranded aio words before readable protection, report failures

Restore every pending aio word in the range before a readable guest
protection is applied, on both the high and low paths, and fail the guest
mprotect with the old protection kept when a word cannot be restored or its
window cannot be closed. Ignore user-sent SIGSEGV/SIGBUS after deferral,
read the cleared syscall info when resuming with guest GPRs and on restore,
and check exclusive acquisition from a separate thread in the harness.

* FEX: submit aio control blocks through private host copies

Stop editing guest IOCBs in place when aio_buf needs translation. io_submit
now hands the kernel a host copy of each such control block, maps
io_event.obj back to the guest pointer when the completion is reaped,
redirects io_cancel by guest pointer and context to the copy, and frees
copies on reap, on io_destroy, or when the kernel did not accept them.
The guest control block is never patched and its protection is never
touched, so the pending restore records, the protection windows and the
restore hooks in mmap, munmap, mremap and mprotect are removed with their
tests.

* fex: keep aio keys, cancels and reaped events native for copied control blocks

Guest aio_key is no longer cleared before dispatch: the slot is probed with a
same-value write and only the first n accepted copies get key 0 after the host
io_submit returns n. io_cancel redirects to a copy only when the guest block is
readable and its key is 0, so unreadable blocks fault in the kernel as before.
io_getevents and io_pgetevents reap into emulator storage, translate and
release copies there, then copy the events out, returning -EFAULT if that
fails. A successful io_setup clears the ring header magic so userspace ring
reapers fall back to io_getevents, where copies are translated and released.

* fex: leave aio rejections, keys and unwritable event buffers to the kernel

A copied control block whose key slot is read-only is now passed to the
kernel as the guest's own block at the end of the list, so the kernel reports
its native fd, eventfd or key error. After io_submit, guest keys follow the
private copies through the first rejected position, and they are stored
through a fresh translation of the guest address, skipped when it no longer
maps. io_getevents and io_pgetevents probe the guest event buffer and reap at
most its writable prefix, handing an unwritable buffer to the kernel so
completions are not consumed.

* Kept AIO completions and key stores stable against sibling remaps

Wrapped event buffers go to the kernel unchanged. Direct reaps report guest
control blocks and release copies only after the store lands. Guest stores and
reads hold the mapping lock across translation and copy. Completions whose
copy-out fails are stashed per context and delivered first on the next reap,
and dropped on io_destroy.

* fex: stage every AIO reap and keep stashed completions recoverable

Hold the guest mapping for every high address, restore the caller's
signal mask after thread-context holds, replace the fixed event stash
with a per-context table that reserves space before dispatch, stage
all reaps so control block identities are translated before copy-out,
keep collecting ready completions once the stash meets min_nr, and
restash events on every error except an interrupted delivery or a
destroyed context.

* fex: own AIO recovery storage before dispatch and bind it to the context lifetime

Allocate the recovery node with the event buffer before the kernel
consumes completions and hand the buffer itself to the stash when
copy-out fails, so settling never allocates. Track each context by a
generation that io_setup advances, so a destroy that finishes after a
replacement context reuses the ring address only releases the old
context's copies and stash. Drop the reservation cap so blocking reaps
keep their min_nr.

* fex: publish AIO contexts from private storage, keep their generation, wake blocked reapers for stashed completions

* fex: wait for AIO events in bounded slices instead of wake markers, roll back setup by its own generation, fault unreadable io_setup output

Blocked reapers wait in the kernel for at most 20 ms at a time and take stashed completions between waits until the guest minimum or timeout is met, so no eventfd or io_submit notification is needed. Deferred signals end the wait like EINTR. io_setup rollback uses the generation BeginContext installed, and an unreadable context output returns -EFAULT before dispatch.

---------

Co-authored-by: StonkGuy <68372320+StonkGuy@users.noreply.github.com>
Co-authored-by: StonkGuy <StonkGuy@users.noreply.github.com>
2026-12-05 02:26:42 -05:00

1167 lines
43 KiB
C

#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <poll.h>
#include <stddef.h>
#if defined(__aarch64__) || defined(DD_VDEC_ANY_ARCH)
#include <fcntl.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#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
#define VDEC_CAP_MIN_WIDTH 4096u
#define VDEC_CAP_MIN_HEIGHT 2304u
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;
int resizing;
uint32_t stream_w, stream_h;
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 dd_venc_connect(void);
static int vd_enabled(void) {
pthread_mutex_lock(&vd_lock);
if (vd_state == 0) {
const char *flag = getenv("DD_REMOTE_PLAY_DECODE"), *socket = getenv("DD_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 dd_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 = dd_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->stream_w ? v->stream_w : v->width ? v->width : v->cap_width,
.height = v->stream_h ? v->stream_h : 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);
*c = (struct vd_capture){ .fd = -1 };
if (v->last == (int)index) v->last = -1;
}
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;
t.stream_w = p->width;
t.stream_h = 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 < VDEC_CAP_MIN_WIDTH) w = VDEC_CAP_MIN_WIDTH;
if (h < VDEC_CAP_MIN_HEIGHT) h = VDEC_CAP_MIN_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 || (t.hw && (t.bpl != v->bpl || t.cap_height != v->cap_height))) vd_close_decoder(&t);
*v = t;
if (v->cap_width >= v->width && v->cap_height >= v->height) v->resizing = 0;
}
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) {
if (v->hw) vd_close_decoder(v);
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;
}
struct vd_bits {
const uint8_t *data;
size_t size, pos;
};
static uint32_t vd_get_bits(struct vd_bits *b, unsigned count) {
uint32_t value = 0;
while (count--) {
size_t byte = b->pos >> 3;
value = value << 1 | (byte < b->size ? (b->data[byte] >> (7 - (b->pos & 7))) & 1u : 0u);
b->pos++;
}
return value;
}
static uint32_t vd_get_ue(struct vd_bits *b) {
unsigned zeros = 0;
while (zeros < 32 && vd_get_bits(b, 1) == 0 && (b->pos >> 3) < b->size) zeros++;
if (zeros >= 32) return 0;
return ((1u << zeros) - 1) + (zeros ? vd_get_bits(b, zeros) : 0);
}
static int32_t vd_get_se(struct vd_bits *b) {
uint32_t k = vd_get_ue(b);
return (k & 1) ? (int32_t)((k + 1) / 2) : -(int32_t)(k / 2);
}
static int vd_skip_scaling_list(struct vd_bits *b, unsigned size) {
int32_t last = 8, next = 8;
for (unsigned i = 0; i < size; i++) {
if (next != 0) {
int32_t delta = vd_get_se(b);
if (delta < -128 || delta > 127) return -1;
next = (last + delta + 256) % 256;
}
last = next == 0 ? last : next;
}
return 0;
}
/* The coded size an H.264 SPS announces, rounded up to whole macroblocks, as a V4L2 decoder reports it. */
static int vd_parse_sps_h264(const uint8_t *rbsp, size_t size, uint32_t *width, uint32_t *height) {
uint8_t buf[512];
size_t n = 0;
for (size_t i = 0; i < size && n < sizeof buf; i++) {
if (i >= 2 && rbsp[i] == 3 && rbsp[i - 1] == 0 && rbsp[i - 2] == 0) continue;
buf[n++] = rbsp[i];
}
struct vd_bits b = { buf, n, 0 };
uint32_t profile = vd_get_bits(&b, 8);
vd_get_bits(&b, 16);
vd_get_ue(&b);
uint32_t chroma = 1;
if (profile == 100 || profile == 110 || profile == 122 || profile == 244 || profile == 44 || profile == 83 ||
profile == 86 || profile == 118 || profile == 128 || profile == 138 || profile == 139 || profile == 134 || profile == 135) {
chroma = vd_get_ue(&b);
if (chroma == 3) vd_get_bits(&b, 1);
vd_get_ue(&b);
vd_get_ue(&b);
vd_get_bits(&b, 1);
if (vd_get_bits(&b, 1)) {
for (unsigned i = 0; i < (chroma != 3 ? 8u : 12u); i++)
if (vd_get_bits(&b, 1) && vd_skip_scaling_list(&b, i < 6 ? 16 : 64) != 0) return -1;
}
}
vd_get_ue(&b);
uint32_t poc = vd_get_ue(&b);
if (poc == 0) {
vd_get_ue(&b);
} else if (poc == 1) {
vd_get_bits(&b, 1);
vd_get_se(&b);
vd_get_se(&b);
uint32_t cycle = vd_get_ue(&b);
for (uint32_t i = 0; i < cycle && i < 256; i++) vd_get_se(&b);
}
vd_get_ue(&b);
vd_get_bits(&b, 1);
uint32_t mbs_wide = vd_get_ue(&b) + 1, map_units_high = vd_get_ue(&b) + 1;
if (b.pos > b.size * 8 || mbs_wide > 1024 || map_units_high > 1024) return -1;
uint32_t frame_mbs_only = vd_get_bits(&b, 1);
if (b.pos > b.size * 8) return -1;
*width = mbs_wide * 16;
*height = map_units_high * 16 * (2 - frame_mbs_only);
return 0;
}
/* Finds the first SPS in an Annex B access unit; returns its size in the stream through the out parameters. */
static int vd_stream_size_h264(const uint8_t *data, size_t size, uint32_t *width, uint32_t *height) {
for (size_t i = 0; i + 4 < size; i++) {
if (data[i] != 0 || data[i + 1] != 0 || data[i + 2] != 1 || (data[i + 3] & 0x1f) != 7) continue;
size_t start = i + 4, end = start;
while (end + 2 < size && !(data[end] == 0 && data[end + 1] == 0 && (data[end + 2] == 1 || data[end + 2] == 0))) end++;
if (end + 2 >= size) end = size;
return vd_parse_sps_h264(data + start, end - start, width, height);
}
return -1;
}
/* The same for an H.265 SPS: the luma size in samples, rounded up to 16 as the client asks for it. */
static int vd_parse_sps_h265(const uint8_t *rbsp, size_t size, uint32_t *width, uint32_t *height) {
uint8_t buf[512];
size_t n = 0;
for (size_t i = 0; i < size && n < sizeof buf; i++) {
if (i >= 2 && rbsp[i] == 3 && rbsp[i - 1] == 0 && rbsp[i - 2] == 0) continue;
buf[n++] = rbsp[i];
}
struct vd_bits b = { buf, n, 0 };
vd_get_bits(&b, 4);
uint32_t sub_layers = vd_get_bits(&b, 3);
vd_get_bits(&b, 1);
for (unsigned i = 0; i < 88; i += 8) vd_get_bits(&b, 8);
vd_get_bits(&b, 8);
uint32_t profile_present = 0, level_present = 0;
for (uint32_t i = 0; i < sub_layers && i < 8; i++) {
profile_present |= vd_get_bits(&b, 1) << i;
level_present |= vd_get_bits(&b, 1) << i;
}
if (sub_layers > 0) for (uint32_t i = sub_layers; i < 8; i++) vd_get_bits(&b, 2);
for (uint32_t i = 0; i < sub_layers && i < 8; i++) {
if (profile_present >> i & 1) for (unsigned k = 0; k < 88; k += 8) vd_get_bits(&b, 8);
if (level_present >> i & 1) vd_get_bits(&b, 8);
}
vd_get_ue(&b);
if (vd_get_ue(&b) == 3) vd_get_bits(&b, 1);
uint32_t w = vd_get_ue(&b), h = vd_get_ue(&b);
if (b.pos > b.size * 8 || w < 16 || h < 16 || w > 16384 || h > 16384) return -1;
*width = (w + 15) & ~15u;
*height = (h + 15) & ~15u;
return 0;
}
static int vd_stream_size_h265(const uint8_t *data, size_t size, uint32_t *width, uint32_t *height) {
for (size_t i = 0; i + 5 < size; i++) {
if (data[i] != 0 || data[i + 1] != 0 || data[i + 2] != 1 || ((data[i + 3] >> 1) & 0x3f) != 33) continue;
size_t start = i + 5, end = start;
while (end + 2 < size && !(data[end] == 0 && data[end + 1] == 0 && (data[end + 2] == 1 || data[end + 2] == 0))) end++;
if (end + 2 >= size) end = size;
return vd_parse_sps_h265(data + start, end - start, width, height);
}
return -1;
}
/*
* A stream can change its picture size without telling the client: the host switches games or the window
* becomes fullscreen. A V4L2 decoder reports the new size through the format of its bitstream queue, which
* the client polls; the capture buffers it already allocated stay at the old size until it reconfigures.
*/
static int vd_follow_stream_size(struct vdec *v, const uint8_t *data, size_t size) {
uint32_t width, height;
int found = v->fourcc == V4L2_PIX_FMT_HEVC ? vd_stream_size_h265(data, size, &width, &height)
: vd_stream_size_h264(data, size, &width, &height);
if (found != 0) return 0;
uint32_t known_w = v->stream_w ? v->stream_w : v->width, known_h = v->stream_h ? v->stream_h : v->height;
if (width == known_w && height == known_h) return 0;
vd_log("the stream is now %ux%u, was %ux%u", width, height, known_w, known_h);
if (width <= v->cap_width && height <= v->cap_height) {
/* The client's capture buffers already hold the new size, so only the app decoder is reopened for it. */
v->stream_w = width;
v->stream_h = height;
if (v->hw) vd_close_decoder(v);
return 1;
}
v->width = v->stream_w = width;
v->height = v->stream_h = height;
v->resizing = 1;
v->n_cap_done = 0;
return 1;
}
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 && v->bitstream && bytes) vd_follow_stream_size(v, v->bitstream + (size_t)b->index * VDEC_BITSTREAM_SIZE, bytes);
if (v->out_on && bytes && !v->resizing) 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;
struct vd_done d;
for (;;) {
if (v->n_cap_done == 0) return -EAGAIN;
d = v->cap_done[0];
memmove(v->cap_done, v->cap_done + 1, --v->n_cap_done * sizeof d);
struct stat st;
struct vd_capture *c = &v->cap[d.index];
if (c->map && c->fd >= 0 && fstat(c->fd, &st) == 0 && st.st_ino == c->ino) break;
}
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 dd_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 dd_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 dd_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms,
int (*real)(struct pollfd *, nfds_t, int), int *ret) {
if (n != 1 || __atomic_load_n(&vd_open_count, __ATOMIC_ACQUIRE) == 0) return 0;
struct vdec *v = vd_find(fds[0].fd);
if (v == NULL) return 0;
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 dd_vdec_open(const char *path, int flags) {
(void)path;
(void)flags;
return -2;
}
__attribute__((visibility("hidden"))) int dd_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 dd_vdec_closed(int fd) { (void)fd; }
__attribute__((visibility("hidden"))) int dd_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