Files
SimpleRemoter/client/X264Encoder.cpp
T
yuanyuanxiang ca4e85826f Feature: remote desktop tuning flags, on by default with per-item opt-out
Adds "Advanced -> Remote desktop tuning" with three independently
checkable items, all enabled by default:

  x264 bitrate cap (VBV)       TUNING_X264_VBV        0x1
  GDI capture: no slice delay  TUNING_GDI_NO_SLEEP    0x2
  Send pacing FPS floor        TUNING_PACE_FPS_FLOOR  0x4

Unticking an item clears its bit and restores the pre-commit behaviour
exactly, so the escape hatch is one click per measure -- no rebuild, no
config edit. Each bit is independent; a problem with one does not force
turning all three off.

Plumbing
--------
Menu state -> [settings] Tuning* -> CMy2015RemoteDlg::GetScreenTuningFlags()
  -> CScreenSpyDlg::SendScreenTuning()  (right after TOKEN_BITMAPINFO)
  -> CMD_SCREEN_TUNING = 178 [cmd:1][flags:4 LE]
  -> CScreenManager::OnReceive -> m_nTuningFlags -> ScreenCapture::SetTuningFlags

Why it is safe to send to old clients: CScreenManager::OnReceive has no
default branch in its switch, so an unknown command falls straight out
(verified). 178 was unassigned (177 = COMMAND_SET_PROTO, 199 =
TOKEN_DECRYPT), and the enum re-anchors explicitly so nothing auto-
increments onto it.

Flags are sent even when all-zero on purpose: the client's flag lives in
CScreenManager, which survives closing and reopening the remote desktop
window on the same connection. Without the zero send, unticking the menu
would leave the previous flags in effect.

Per-measure notes
-----------------
VBV: capped CRF (peak = target, 0.5s buffer, init 0.9). The hardware
paths already cap via rc_max_rate (1.5x, 1s buffer); this is tighter and
only affects x264. After an IDR the buffer drains and takes up to 0.5s
to refill, so a brief quality dip follows every keyframe -- if that shows
up, relaxing to 1.5x/1s would match NVENC/QSV.

GDI: keeps the 50-line BitBlt slicing (large single BitBlts hit driver
limits) and only drops the 5ms sleep between slices -- 1080p is 22 slices
= a forced 110ms per frame against a 50ms budget at 20fps.

Pacing: clamps s0 to 2x the base interval instead of 4x (2fps -> 4fps),
taken as max(2x base, 1000/MaxFPS) so it never exceeds the configured
MaxFPS. The growth/decay code itself is untouched. This one works against
the loop's own congestion signal, so it is the first to untick if a bad
link feels laggier rather than smoother.

SetTuningFlags only rebuilds the encoder when TUNING_ENCODER_BITS
(currently just VBV) changes -- the GDI and pacing bits are read live by
the capture and send threads, and rebuilding for them would drop an IDR
and visibly flash the picture.

Defaults: the on-by-default value lives in the third argument of
THIS_CFG.GetInt(), which only applies when the key is absent. A machine
that already has an explicit 0 keeps it, so the flip is not retroactive
and a test machine can end up with a mixed state.

Verified on the Debug build: with no Tuning* keys all three items start
checked; unticking VBV alone leaves the other two checked and survives a
restart; en_US/de_DE/zh_TW translations all resolve. Not yet measured end
to end on a real remote desktop session.
2026-11-04 14:47:40 +01:00

211 lines
6.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "X264Encoder.h"
#include "common/logger.h"
#include <string.h>
#include <stdio.h>
// libyuv 与 x264 是两个独立依赖,必须分开处理:硬编路径(CFFmpegAV1Encoder /
// CFFmpegH264Encoder)也拿 libyuv 做色彩空间转换,但这两个文件自己没有
// #pragma comment(lib),一直靠这里引入。所以 libyuv 必须留在 x264 开关之外——
// 否则 DISABLE_X264_FOR_TEST=1 会把 libyuv 一起丢掉,链接时立刻报
// LNK2001: RGB24ToI420 / ARGBToNV12 / I420ToNV12。
#ifdef _WIN64
#pragma comment(lib,"libyuv/libyuv_x64.lib")
#else
#pragma comment(lib,"libyuv/libyuv.lib")
#endif
#if DISABLE_X264_FOR_TEST
// 合规构建的桩实现:头文件已不含 x264 类型,这里也不能碰 m_Param / x264_param_t。
CX264Encoder::CX264Encoder() { m_forceIDR = false; }
CX264Encoder::~CX264Encoder() {}
bool CX264Encoder::open(int, int, int, int, int) { return false; }
bool CX264Encoder::open(const EncoderParams&) { return false; }
void CX264Encoder::close() {}
int CX264Encoder::encode(uint8_t*, uint8_t, uint32_t, uint32_t, uint32_t, uint8_t**, uint32_t*, int) { return -1; }
#else
#ifdef _WIN64
#pragma comment(lib,"x264/libx264_x64.lib")
#else
#pragma comment(lib,"x264/libx264.lib")
#endif
CX264Encoder::CX264Encoder()
{
memset(&m_Param, 0, sizeof(m_Param));
m_pCodec = NULL;
m_pPicIn = NULL;
m_pPicOut = NULL;
m_forceIDR = false;
}
CX264Encoder::~CX264Encoder()
{
close();
}
bool CX264Encoder::open(int width, int height, int fps, int crf, int vbv_kbps)
{
x264_param_t param = { 0 };
x264_param_default_preset(&param, "ultrafast", "zerolatency");
param.i_width = width & 0xfffffffe;
param.i_height = height & 0xfffffffe;
//x264_LOG_NONE
param.i_log_level = X264_LOG_NONE;
param.i_threads = 1;
param.i_frame_total = 0;
param.i_keyint_max = fps * 15; // 15秒一个IDR,场景突变由scenecut自动插入
param.i_bframe = 0; //不启用b帧
param.b_open_gop = 0;
param.i_fps_num = fps;
param.i_csp = X264_CSP_I420;
// CRF: 静态画面自动降至极低码率,动态画面按质量需要分配
param.rc.i_rc_method = X264_RC_CRF;
param.rc.f_rf_constant = (float)crf;
// capped CRF:CRF 保证画质,VBV 保证峰值不失控。纯 CRF 下复杂帧(视频、切大窗口)
// 单帧能冲到目标码率好几倍,跨洲际 2Mbps 链路上一个 500KB 的帧要传 2 秒,
// 后续帧全排队 → 用户看到的是"卡住几秒然后画面猛跳"。默认不开(vbv_kbps=0),
// 由主控端「高级 → 远程桌面调优」勾选后下发 TUNING_X264_VBV 打开。
if (vbv_kbps > 0) {
param.rc.i_vbv_max_bitrate = vbv_kbps; // 峰值 = 目标码率
param.rc.i_vbv_buffer_size = vbv_kbps / 2; // 0.5 秒缓冲:够压住尖峰,又不至于抖动
param.rc.f_vbv_buffer_init = 0.9f; // 从接近满缓冲起步,避免首帧质量塌
}
//设置profile.
if (x264_param_apply_profile(&param, x264_profile_names[0])) {
return false;
}
return open(&param);
}
bool CX264Encoder::open(x264_param_t * param)
{
// x264_encoder_open 在 0×0 时已完成 x264_param_strdup 才报错,需在此拦截
if (param->i_width < 2 || param->i_height < 2) return false;
m_pPicIn = (x264_picture_t*)calloc(1, sizeof(x264_picture_t));
m_pPicOut = (x264_picture_t*)calloc(1, sizeof(x264_picture_t));
//input pic.
x264_picture_init(m_pPicIn);
x264_picture_alloc(
m_pPicIn,
X264_CSP_I420,
param->i_width,
param->i_height);
//create codec instance.
m_pCodec = x264_encoder_open(param);
if (m_pCodec == NULL) {
return false;
}
memcpy(&m_Param, param, sizeof(m_Param));
return true;
}
bool CX264Encoder::open(const EncoderParams& params)
{
// x264 软编只支持 CRF;调用方走 BITRATE 时降级为 CRF=23(与 BitRateToCRF 默认一致)
int crf = (params.rc == RateControl::CRF) ? params.crf : 23;
// vbv_cap 打开时用 bitrate_kbps 当峰值上限;bitrate 未知(<=0)则不加,退回纯 CRF
int vbv = params.vbv_cap ? params.bitrate_kbps : 0;
if (vbv > 0) {
Mprintf("=> x264: capped CRF, crf=%d, vbv_max=%dkbps, buffer=%dkb\n",
crf, vbv, vbv / 2);
}
return open(params.width, params.height, params.fps, crf, vbv);
}
void CX264Encoder::close()
{
if (m_pCodec) {
x264_encoder_close(m_pCodec);
m_pCodec = NULL;
}
if (m_pPicIn) {
x264_picture_clean(m_pPicIn);
free(m_pPicIn);
m_pPicIn = NULL;
}
if (m_pPicOut) {
free(m_pPicOut);
m_pPicOut = NULL;
}
}
int CX264Encoder::encode(
uint8_t * rgb,
uint8_t bpp,
uint32_t stride,
uint32_t width,
uint32_t height,
uint8_t ** lppData,
uint32_t * lpSize,
int direction)
{
int encode_size = 0;
x264_nal_t *pNal = NULL;
int iNal;
if ((width & 0xfffffffe) != m_Param.i_width ||
(height & 0xfffffffe) != m_Param.i_height) {
return -1;
}
switch (bpp) {
case 24:
libyuv::RGB24ToI420((uint8_t*)rgb, stride,
m_pPicIn->img.plane[0], m_pPicIn->img.i_stride[0],
m_pPicIn->img.plane[1], m_pPicIn->img.i_stride[1],
m_pPicIn->img.plane[2], m_pPicIn->img.i_stride[2],
m_Param.i_width, direction * m_Param.i_height);
break;
case 32:
libyuv::ARGBToI420((uint8_t*)rgb, stride,
m_pPicIn->img.plane[0], m_pPicIn->img.i_stride[0],
m_pPicIn->img.plane[1], m_pPicIn->img.i_stride[1],
m_pPicIn->img.plane[2], m_pPicIn->img.i_stride[2],
m_Param.i_width, direction * m_Param.i_height);
break;
default:
return -2;
}
if (m_forceIDR) {
m_pPicIn->i_type = X264_TYPE_IDR;
m_forceIDR = false;
} else {
m_pPicIn->i_type = X264_TYPE_AUTO;
}
encode_size = x264_encoder_encode(
m_pCodec,
&pNal,
&iNal,
m_pPicIn,
m_pPicOut);
if (encode_size < 0) {
return -3;
}
*lppData = pNal->p_payload;
*lpSize = encode_size;
return 0;
}
#endif