mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 15:26:04 +00:00
fix(remote-play): the decoder reports a changed stream size, so fullscreen streams are not cropped to a corner (#3095)
* fix(remote-play): the decoder reports a changed stream size, so fullscreen streams are not cropped to a corner * fix(remote-play): capture buffers are sized for large streams up front and a size change reopens only the app decoder * fix(remote-play): reject out-of-range scaling deltas and an SPS that ends before its frame flag
This commit is contained in:
1 parent
b0a8c0898e
commit
15b1e196af
3 files changed
+338
-11
No files matched your search
@@ -113,12 +113,12 @@ int main(void) {
|
||||
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;
|
||||
if (bpl != 4096 || size != 4096 * 2304 * 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 };
|
||||
struct create_dumb c = { .height = 2304, .width = 4096, .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;
|
||||
@@ -148,7 +148,7 @@ int main(void) {
|
||||
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);
|
||||
printf("%ld %u %c%c %u\n", (long)cb.timestamp.tv_usec, cb.index, m[0], m[(size_t)bpl * 2304], cp.bytesused == size);
|
||||
munmap(m, size);
|
||||
pf = (struct pollfd){ .fd = vfd, .events = 0x306 };
|
||||
if (poll(&pf, 1, 0) != 1) return 26;
|
||||
@@ -166,7 +166,7 @@ int main(void) {
|
||||
size = f.fmt.pix_mp.plane_fmt[0].sizeimage;
|
||||
r = (struct v4l2_requestbuffers){ .count = 1, .type = types[1], .memory = V4L2_MEMORY_DMABUF };
|
||||
if (call(VIDIOC_REQBUFS, &r)) return 31;
|
||||
struct create_dumb big = { .height = 1088, .width = 1920, .bpp = 16 };
|
||||
struct create_dumb big = { .height = 2304, .width = 4096, .bpp = 16 };
|
||||
if (drm_ioctl(card, 0xc02064b2, &big)) return 32;
|
||||
struct prime_handle bh = { .handle = big.handle, .flags = O_CLOEXEC };
|
||||
if (drm_ioctl(card, 0xc00c642d, &bh)) return 33;
|
||||
@@ -183,7 +183,7 @@ int main(void) {
|
||||
struct v4l2_buffer db = { .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &dp };
|
||||
if (call(VIDIOC_DQBUF, &db)) return 37;
|
||||
unsigned char *m = mmap(NULL, size, PROT_READ, MAP_SHARED, bh.fd, 0);
|
||||
printf("%ld %u %c%c\n", (long)db.timestamp.tv_usec, db.index, m[0], m[(size_t)1920 * 1088]);
|
||||
printf("%ld %u %c%c\n", (long)db.timestamp.tv_usec, db.index, m[0], m[(size_t)4096 * 2304]);
|
||||
dd_vdec_closed(vfd);
|
||||
return 0;
|
||||
}
|
||||
@@ -261,7 +261,7 @@ class VdecEmulatorTest(unittest.TestCase):
|
||||
self.assertEqual(done.stdout.split('\n')[:6],
|
||||
['1 0 bu 1', '2 1 cu 1', '3 2 du 1', '4 0 eu 1', '5 1 fu 1', '6 0 zu'])
|
||||
self.assertEqual([c[1:6] for c in server.configs],
|
||||
[(1, 1280, 720, 1280, 736), (1, 1280, 720, 1920, 1088)])
|
||||
[(1, 1280, 720, 4096, 2304), (1, 1280, 720, 4096, 2304)])
|
||||
self.assertEqual([p[:3] for p in server.packets],
|
||||
[(1, 101, 0), (2, 102, 1), (3, 103, 2), (0, 104, 0), (1, 105, 1), (2, 50, 0)])
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
VDEC = ROOT / 'tools/linuxfs/preload/vdec.c'
|
||||
|
||||
|
||||
class Bits:
|
||||
def __init__(self):
|
||||
self.bits = []
|
||||
|
||||
def u(self, value, count):
|
||||
self.bits += [(value >> (count - 1 - i)) & 1 for i in range(count)]
|
||||
|
||||
def ue(self, value):
|
||||
value += 1
|
||||
length = value.bit_length()
|
||||
self.u(0, length - 1)
|
||||
self.u(value, length)
|
||||
|
||||
def bytes(self):
|
||||
bits = self.bits + [1]
|
||||
while len(bits) % 8:
|
||||
bits.append(0)
|
||||
raw = bytes(int(''.join(map(str, bits[i:i + 8])), 2) for i in range(0, len(bits), 8))
|
||||
out = bytearray()
|
||||
for i, byte in enumerate(raw):
|
||||
if len(out) >= 2 and out[-1] == 0 and out[-2] == 0 and byte <= 3:
|
||||
out.append(3)
|
||||
out.append(byte)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def sps(width_mbs, height_units, profile=100, frame_mbs_only=1, poc_type=2):
|
||||
b = Bits()
|
||||
b.u(profile, 8)
|
||||
b.u(0, 8)
|
||||
b.u(51, 8)
|
||||
b.ue(0)
|
||||
if profile in (100, 110, 122, 244):
|
||||
b.ue(1)
|
||||
b.ue(0)
|
||||
b.ue(0)
|
||||
b.u(0, 1)
|
||||
b.u(0, 1)
|
||||
b.ue(0)
|
||||
b.ue(poc_type)
|
||||
if poc_type == 0:
|
||||
b.ue(4)
|
||||
b.ue(1)
|
||||
b.u(0, 1)
|
||||
b.ue(width_mbs - 1)
|
||||
b.ue(height_units - 1)
|
||||
b.u(frame_mbs_only, 1)
|
||||
if not frame_mbs_only:
|
||||
b.u(0, 1)
|
||||
b.u(1, 1)
|
||||
b.u(0, 1)
|
||||
return b'\x00\x00\x00\x01\x67' + b.bytes() + b'\x00\x00\x00\x01\x65\x88\x84\x00'
|
||||
|
||||
|
||||
def hevc_sps(width, height, sub_layers_minus1=0):
|
||||
b = Bits()
|
||||
b.u(0, 4)
|
||||
b.u(sub_layers_minus1, 3)
|
||||
b.u(1, 1)
|
||||
b.u(0, 88)
|
||||
b.u(93, 8)
|
||||
for _ in range(sub_layers_minus1):
|
||||
b.u(0, 2)
|
||||
if sub_layers_minus1:
|
||||
for _ in range(sub_layers_minus1, 8):
|
||||
b.u(0, 2)
|
||||
b.ue(0)
|
||||
b.ue(1)
|
||||
b.ue(width)
|
||||
b.ue(height)
|
||||
b.u(0, 1)
|
||||
return b'\x00\x00\x00\x01\x42\x01' + b.bytes() + b'\x00\x00\x00\x01\x26\x01\xaf'
|
||||
|
||||
|
||||
DRIVER = r'''
|
||||
#include "vdec.c"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FILE *f = fopen(argv[1], "rb");
|
||||
static uint8_t data[4096];
|
||||
size_t n = fread(data, 1, sizeof data, f);
|
||||
uint32_t w = 0, h = 0;
|
||||
int rc = argc > 2 ? vd_stream_size_h265(data, n, &w, &h) : vd_stream_size_h264(data, n, &w, &h);
|
||||
printf("%d %u %u\n", rc, w, h);
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class StreamSizeTest(unittest.TestCase):
|
||||
def run_driver(self, data, hevc=False):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmp = Path(tmp)
|
||||
(tmp / 'driver.c').write_text(DRIVER)
|
||||
(tmp / 'stream.bin').write_bytes(data)
|
||||
exe = tmp / 'driver'
|
||||
subprocess.run(['gcc', '-std=gnu11', '-DDD_VDEC_ANY_ARCH', '-I', str(VDEC.parent), '-o', str(exe), str(tmp / 'driver.c'), str(VDEC.parent / 'venc.c'), '-ldl', '-lpthread'],
|
||||
check=True, capture_output=True, text=True)
|
||||
return subprocess.run([str(exe), str(tmp / 'stream.bin')] + (['h265'] if hevc else []), check=True, capture_output=True, text=True).stdout.split()
|
||||
|
||||
def test_reads_an_ultrawide_stream_size(self):
|
||||
self.assertEqual(['0', '3440', '1440'], self.run_driver(sps(215, 90)))
|
||||
|
||||
def test_reads_a_small_stream_size(self):
|
||||
self.assertEqual(['0', '800', '464'], self.run_driver(sps(50, 29)))
|
||||
|
||||
def test_interlaced_streams_double_the_height(self):
|
||||
self.assertEqual(['0', '1920', '1088'], self.run_driver(sps(120, 34, frame_mbs_only=0)))
|
||||
|
||||
def test_baseline_profile_has_no_chroma_fields(self):
|
||||
self.assertEqual(['0', '1280', '720'], self.run_driver(sps(80, 45, profile=66)))
|
||||
|
||||
def test_emulation_prevention_bytes_are_removed(self):
|
||||
self.assertEqual(['0', '256', '256'], self.run_driver(sps(16, 16, profile=66)))
|
||||
|
||||
def test_reads_an_h265_stream_size(self):
|
||||
self.assertEqual(['0', '3440', '1440'], self.run_driver(hevc_sps(3440, 1440), hevc=True))
|
||||
|
||||
def test_h265_sizes_round_up_to_sixteen(self):
|
||||
self.assertEqual(['0', '1920', '1088'], self.run_driver(hevc_sps(1920, 1080, sub_layers_minus1=2), hevc=True))
|
||||
|
||||
def test_a_truncated_sps_is_rejected(self):
|
||||
self.assertEqual('-1', self.run_driver(b'\x00\x00\x00\x01\x67\x64\x00\x33\xac\xb4\x02\x80\x2d')[0])
|
||||
|
||||
def test_a_stream_without_an_sps_is_left_alone(self):
|
||||
self.assertEqual('-1', self.run_driver(b'\x00\x00\x00\x01\x65\x88\x84\x00\x00\x01')[0])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user