Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Ask for the HQ stream tiers; bilinear chroma upsampling

Rafael Abud Ferreirad31fad6791b4
diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cpp
index d57c496..b003b01 100644
--- a/src/app/stream_player.cpp
+++ b/src/app/stream_player.cpp
@@ -62,6 +62,7 @@ struct StreamPlayer::Impl {
uint64_t dumpUntilMs = 0;
std::mutex snapshotMutex;
std::string snapshotPath;
+ std::string screenPath; // video thread only
uint64_t lastKeyframeRequestMs = 0;
explicit Impl(xcloud::GssvClient& g) : gssv(g) {}
@@ -145,7 +146,7 @@ struct StreamPlayer::Impl {
continue;
}
++decodedFrames;
- saveSnapshotIfAsked(pic);
+ bool screenshot = saveSnapshotIfAsked(pic);
if (backlog > 2) { // behind: skip drawing to catch up
++droppedFrames;
continue;
@@ -174,6 +175,7 @@ struct StreamPlayer::Impl {
}
display::present();
uint64_t t3 = platform::nowUs();
+ if (screenshot) saveScreen();
drawUs += t3 - t2;
++drawCalls;
atomicMax(drawMaxUs, t3 - t2);
@@ -221,9 +223,15 @@ struct StreamPlayer::Impl {
cb.closed = [this](const std::string& reason) { end(reason); };
stream::StreamOptions opts;
- // 1440p is asked for like the xbox.com client does; the service only
- // grants it where Microsoft has enabled it (not every market).
- if (gssv.resolution() == xcloud::Resolution::P1440) opts.resolutionAlias = "1440";
+ // Each setting asks for the best tier of its resolution, like the
+ // xbox.com client: the HQ tiers (higher bitrate) and 1440p need Game
+ // Pass Ultimate and a market where Microsoft enabled them; elsewhere
+ // the service ignores the request and streams the plain tier.
+ switch (gssv.resolution()) {
+ case xcloud::Resolution::P1440: opts.resolutionAlias = "1440"; break;
+ case xcloud::Resolution::P1080: opts.resolutionAlias = "1080HQ"; break;
+ case xcloud::Resolution::P720: opts.resolutionAlias = "720HQ"; break;
+ }
opts.maxBitrate = gssv.resolution() == xcloud::Resolution::P720 ? 12000000
: gssv.resolution() == xcloud::Resolution::P1440 ? 40000000
: 25000000;
@@ -243,13 +251,16 @@ struct StreamPlayer::Impl {
return true;
}
- void saveSnapshotIfAsked(const media::Picture& pic) {
+ // Returns true when a snapshot was taken: the caller then saves what the
+ // screen shows too (saveScreen()).
+ bool saveSnapshotIfAsked(const media::Picture& pic) {
std::string path;
{
std::lock_guard<std::mutex> lock(snapshotMutex);
path.swap(snapshotPath);
}
- if (path.empty()) return;
+ if (path.empty()) return false;
+ screenPath = path.substr(0, path.rfind('/') + 1) + "screen.ppm";
int w = pic.width / 2, h = pic.height / 2;
std::vector<uint8_t> rgb(static_cast<size_t>(w) * h * 3);
for (int y = 0; y < h; ++y)
@@ -267,6 +278,17 @@ struct StreamPlayer::Impl {
data.append(reinterpret_cast<const char*>(rgb.data()), rgb.size());
bool ok = platform::writeFileAtomic(path, data);
XC_LOGI("snapshot %dx%d -> %s: %s", w, h, path.c_str(), ok ? "ok" : "failed");
+ return true;
+ }
+
+ // The scan-out buffer just presented, at full resolution.
+ void saveScreen() {
+ std::vector<uint8_t> rgb;
+ if (!display::readBackRgb(rgb)) return;
+ std::string data = "P6\n" + std::to_string(display::kWidth) + " " + std::to_string(display::kHeight) + "\n255\n";
+ data.append(reinterpret_cast<const char*>(rgb.data()), rgb.size());
+ bool ok = platform::writeFileAtomic(screenPath, data);
+ XC_LOGI("screen -> %s: %s", screenPath.c_str(), ok ? "ok" : "failed");
}
// Video thread only (data channel sends are thread-safe).
diff --git a/src/display/display.cpp b/src/display/display.cpp
index ba6fab5..6f64a29 100644
--- a/src/display/display.cpp
+++ b/src/display/display.cpp
@@ -216,6 +216,12 @@ struct YuvJob {
int width, height;
uint8_t* base;
std::vector<uint16_t> srcX, srcY; // screen -> picture coordinate
+ // Bilinear chroma: the two chroma columns/rows around each screen
+ // pixel and the weight of the first, in eighths (columns) and quarters
+ // (rows). H.264's default siting: chroma sits on the even luma columns
+ // and halfway between two luma rows.
+ std::vector<uint16_t> cx0, cx1, cy0, cy1;
+ std::vector<uint8_t> wx, wy;
};
inline uint8_t clamp8(int v) { return static_cast<uint8_t>(v < 0 ? 0 : v > 255 ? 255 : v); }
@@ -235,8 +241,15 @@ void convertTiles(const YuvJob& job, unsigned first, unsigned last) {
unsigned sx = job.srcX[x], sy = job.srcY[y];
// BT.709 limited range, 10-bit fixed point.
int c = (static_cast<int>(job.y[sy * job.strideY + sx]) - 16) * 1192;
- int d = static_cast<int>(job.u[(sy >> 1) * job.strideU + (sx >> 1)]) - 128;
- int e = static_cast<int>(job.v[(sy >> 1) * job.strideV + (sx >> 1)]) - 128;
+ const uint8_t* u0 = job.u + job.cy0[y] * job.strideU;
+ const uint8_t* u1 = job.u + job.cy1[y] * job.strideU;
+ const uint8_t* v0 = job.v + job.cy0[y] * job.strideV;
+ const uint8_t* v1 = job.v + job.cy1[y] * job.strideV;
+ unsigned ca = job.cx0[x], cb = job.cx1[x];
+ int wxa = job.wx[x], wxb = 2 - wxa, wya = job.wy[y], wyb = 4 - wya;
+ // Weights total 2 * 4 = 8.
+ int d = ((wya * (wxa * u0[ca] + wxb * u0[cb]) + wyb * (wxa * u1[ca] + wxb * u1[cb]) + 4) >> 3) - 128;
+ int e = ((wya * (wxa * v0[ca] + wxb * v0[cb]) + wyb * (wxa * v1[ca] + wxb * v1[cb]) + 4) >> 3) - 128;
uint32_t r = clamp8((c + 1836 * e + 512) >> 10);
uint32_t g = clamp8((c - 218 * d - 546 * e + 512) >> 10);
uint32_t b = clamp8((c + 2163 * d + 512) >> 10);
@@ -295,6 +308,22 @@ private:
} // namespace
+bool readBackRgb(std::vector<uint8_t>& rgb) {
+ if (!g_mappedMemory) return false;
+ const auto* base = static_cast<const uint8_t*>(g_mappedMemory) + (g_currentBuffer * kFrameBytes);
+ rgb.resize(static_cast<size_t>(kWidth) * kHeight * 3);
+ for (unsigned y = 0; y < kHeight; ++y)
+ for (unsigned x = 0; x < kWidth; ++x) {
+ uint32_t p;
+ std::memcpy(&p, base + tiledByteOffset(x, y), 4);
+ uint8_t* o = &rgb[(static_cast<size_t>(y) * kWidth + x) * 3];
+ o[0] = static_cast<uint8_t>(p);
+ o[1] = static_cast<uint8_t>(p >> 8);
+ o[2] = static_cast<uint8_t>(p >> 16);
+ }
+ return true;
+}
+
TilePool& tilePool() {
static TilePool pool;
return pool;
@@ -328,6 +357,37 @@ bool drawYuv420(const uint8_t* y, const uint8_t* u, const uint8_t* v, int stride
for (unsigned x = 0; x < kWidth; ++x) job.srcX[x] = static_cast<uint16_t>(x * static_cast<unsigned>(width) / kWidth);
for (unsigned yy = 0; yy < kHeight; ++yy)
job.srcY[yy] = static_cast<uint16_t>(yy * static_cast<unsigned>(height) / kHeight);
+ int cw = (width + 1) / 2, ch = (height + 1) / 2;
+ job.cx0.resize(kWidth);
+ job.cx1.resize(kWidth);
+ job.wx.resize(kWidth);
+ for (unsigned x = 0; x < kWidth; ++x) {
+ int sx = job.srcX[x];
+ int c0 = sx / 2;
+ bool between = (sx & 1) != 0; // odd luma column: halfway between two chroma samples
+ job.cx0[x] = static_cast<uint16_t>(c0);
+ job.cx1[x] = static_cast<uint16_t>(std::min(c0 + (between ? 1 : 0), cw - 1));
+ job.wx[x] = between ? 1 : 2; // of 2
+ }
+ job.cy0.resize(kHeight);
+ job.cy1.resize(kHeight);
+ job.wy.resize(kHeight);
+ for (unsigned yy = 0; yy < kHeight; ++yy) {
+ int sy = job.srcY[yy];
+ // Chroma row r sits at luma row 2r + 0.5: even rows take 1/4 of
+ // the row above and 3/4 of their own, odd rows 3/4 of their own
+ // and 1/4 of the row below.
+ int r = sy / 2;
+ if (sy & 1) {
+ job.cy0[yy] = static_cast<uint16_t>(r);
+ job.cy1[yy] = static_cast<uint16_t>(std::min(r + 1, ch - 1));
+ job.wy[yy] = 3; // of 4, for cy0
+ } else {
+ job.cy0[yy] = static_cast<uint16_t>(std::max(r - 1, 0));
+ job.cy1[yy] = static_cast<uint16_t>(r);
+ job.wy[yy] = 1;
+ }
+ }
}
job.y = y;
job.u = u;
@@ -357,6 +417,7 @@ void shutdown() {}
void present() {}
bool drawYuv420(const uint8_t*, const uint8_t*, const uint8_t*, int, int, int, int, int, bool) { return true; }
void drawRgba(const uint32_t*) {}
+bool readBackRgb(std::vector<uint8_t>&) { return false; }
} // namespace xc::display
#endif
diff --git a/src/display/display.h b/src/display/display.h
index dc5f369..16a621f 100644
--- a/src/display/display.h
+++ b/src/display/display.h
@@ -5,6 +5,7 @@
#include <cstdint>
#include <cstddef>
#include <mutex>
+#include <vector>
namespace xc::display {
@@ -28,4 +29,8 @@ void drawRgba(const uint32_t* pixels);
bool drawYuv420(const uint8_t* y, const uint8_t* u, const uint8_t* v, int strideY, int strideU, int strideV,
int width, int height, bool wait);
+// The back buffer last drawn, untiled, as 8-bit RGB rows (diagnostics:
+// what the TV shows). False without a display.
+bool readBackRgb(std::vector<uint8_t>& rgb);
+
} // namespace xc::display
diff --git a/tools/ps5/autotest.sh b/tools/ps5/autotest.sh
index d2dc080..f353904 100755
--- a/tools/ps5/autotest.sh
+++ b/tools/ps5/autotest.sh
@@ -37,7 +37,7 @@ import io, sys
from ftplib import FTP, error_perm, error_reply
host, port, d, line = sys.argv[1], int(sys.argv[2]), sys.argv[3], sys.argv[4]
f = FTP(); f.connect(host, port, timeout=15); f.login(); f.cwd(d)
-for name in ("xcloud.log", "frame.ppm", "frame2.ppm", "stream.aus", "ui.ppm", "launch.ppm"):
+for name in ("xcloud.log", "frame.ppm", "frame2.ppm", "stream.aus", "ui.ppm", "launch.ppm", "screen.ppm"):
try: f.sendcmd(f"DELE {name}")
except (error_perm, error_reply): pass
f.storbinary("STOR autoplay.txt", io.BytesIO(line.encode() + b"\n"))
@@ -82,7 +82,7 @@ PY
if [[ $status != *"$title_id"* ]]; then result=crashed; break; fi
fi
done
-fetch xcloud.log frame.ppm frame2.ppm stream.aus ui.ppm launch.ppm || true
+fetch xcloud.log frame.ppm frame2.ppm stream.aus ui.ppm launch.ppm screen.ppm || true
ctl kill "$title_id" >/dev/null 2>&1 || true
# Remove the request so a manual launch behaves normally.
python3 - "$PS5_HOST" "${FTP_PORT:-2121}" "/data/homebrew/$title_id" <<'PY'