From 82f59b24698704dec07238c5fb98164ea5f4c06c Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Fri, 30 Oct 2026 11:04:44 +0100 Subject: [PATCH] Fix: Make the x264 opt-out real, and unchain libyuv from it DISABLE_X264_FOR_TEST never actually removed the x264 dependency. Two separate reasons: 1. X264Encoder.h included unconditionally and only pulled in common/config.h afterwards, so the macro was not even visible at the point where it mattered. The class also carried x264_t / x264_picture_t / x264_param_t members, so the header was required to compile regardless of the switch. Moving the config.h include to the top and splitting the class declaration in two fixes it: with the switch on, the class is a stub with no x264 types at all, and open() returns false so EncoderFactory falls through. The open(x264_param_t*) overload is dropped in that branch; it had no callers outside X264Encoder.cpp. 2. libyuv was collateral damage. Its only #pragma comment(lib) lived inside the #else branch of the x264 switch, but CFFmpegAV1Encoder.cpp and CFFmpegH264Encoder.cpp both call libyuv::RGB24ToI420 / ARGBToNV12 / I420ToNV12 without carrying a pragma of their own. Turning x264 off therefore dropped libyuv too and the link failed with LNK2001 on those three symbols. libyuv is BSD-3 and unrelated to x264: the pragma is now unconditional in X264Encoder.cpp, and CFFmpegH264Encoder.cpp declares it alongside its ffmpeg libs so the encoder pair is self-sufficient. Verified with Release x64 builds of both ServerDll and ghost: switches 0/0, compress/ intact -> builds switch x264=1, compress/x264 deleted -> builds (was LNK2001 before) switches 1/1, both dirs deleted -> builds So a compliant build needs neither the x264 library nor its headers. Note for anyone flipping these switches: do a full rebuild, not an incremental one. config.h reaches X264Encoder.cpp indirectly through X264Encoder.h, which the .tlog dependency tracking does not record, so an incremental build happily links a stale object against a freshly compiled one and reports bogus LNK2001 errors. --- client/CFFmpegH264Encoder.cpp | 4 +++ client/X264Encoder.cpp | 17 ++++++++++--- client/X264Encoder.h | 46 +++++++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/client/CFFmpegH264Encoder.cpp b/client/CFFmpegH264Encoder.cpp index dceeb9e..b85ec95 100644 --- a/client/CFFmpegH264Encoder.cpp +++ b/client/CFFmpegH264Encoder.cpp @@ -21,6 +21,10 @@ extern "C" { #pragma comment(lib,"ffmpeg/libavcodec_x64.lib") #pragma comment(lib,"ffmpeg/libavutil_x64.lib") #pragma comment(lib,"ffmpeg/libswresample_x64.lib") +// libyuv 不属于 ffmpeg:硬编前要把 BGRA/RGB24 转成 I420/NV12。以前这条链接声明 +// 只写在 X264Encoder.cpp 里,一旦 DISABLE_X264_FOR_TEST=1 关掉 x264 就跟着消失, +// 硬编 obj 会报 LNK2001。CFFmpegAV1Encoder.cpp 复用本文件的 pragma,这里补上即可。 +#pragma comment(lib,"libyuv/libyuv_x64.lib") // dav1d (AV1 软解,C 项目) —— 不分 Debug/Release。 // build 时启用了 --enable-libdav1d,libavcodec 内部 av1 decoder 引用了 dav1d 符号。 #pragma comment(lib,"ffmpeg/dav1d_x64.lib") diff --git a/client/X264Encoder.cpp b/client/X264Encoder.cpp index e1d829f..8f241b0 100644 --- a/client/X264Encoder.cpp +++ b/client/X264Encoder.cpp @@ -2,21 +2,30 @@ #include #include +// 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 -CX264Encoder::CX264Encoder() { memset(&m_Param, 0, sizeof(m_Param)); m_pCodec = NULL; m_pPicIn = NULL; m_pPicOut = NULL; m_forceIDR = false; } +// 合规构建的桩实现:头文件已不含 x264 类型,这里也不能碰 m_Param / x264_param_t。 +CX264Encoder::CX264Encoder() { m_forceIDR = false; } CX264Encoder::~CX264Encoder() {} bool CX264Encoder::open(int, int, int, int) { return false; } -bool CX264Encoder::open(x264_param_t*) { 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,"libyuv/libyuv_x64.lib") #pragma comment(lib,"x264/libx264_x64.lib") #else -#pragma comment(lib,"libyuv/libyuv.lib") #pragma comment(lib,"x264/libx264.lib") #endif diff --git a/client/X264Encoder.h b/client/X264Encoder.h index 356a63b..38b3f9e 100644 --- a/client/X264Encoder.h +++ b/client/X264Encoder.h @@ -1,14 +1,54 @@ #pragma once #include "VideoEncoderBase.h" +// 必须在下面的条件编译之前:DISABLE_X264_FOR_TEST 定义在这里。 +// 原来它排在 x264 头文件之后,导致开关无论怎么设,x264.h 都已经被拉进来了。 +#include "common/config.h" + +#if DISABLE_X264_FOR_TEST + +// 合规构建(DISABLE_X264_FOR_TEST=1):不包含 x264 / libyuv 头文件,不声明任何 +// x264 类型,整棵源码树不含 GPL 头文件,也不链接 libx264。 +// +// 类仍保留基类接口(EncoderFactory::CreateEncoder 通过 VideoEncoderBase 指针创建 +// 并调用),但 open() 恒返回 false,由 CreateEncoder 自然回退到硬编路径; +// 若硬编也不可用则返回 nullptr,调用方按“无编码器”处理。 +class CX264Encoder : public VideoEncoderBase +{ +private: + bool m_forceIDR; // 保留成员以对齐两套实现的行为,桩里不使用 +public: + // 旧签名保留:与非合规构建保持同一份对外接口 + bool open(int width, int height, int fps, int crf); + + // VideoEncoderBase + bool open(const EncoderParams& params) override; + void close() override; + int encode( + uint8_t * rgb, + uint8_t bpp, + uint32_t stride, + uint32_t width, + uint32_t height, + uint8_t ** lppData, + uint32_t * lpSize, + int direction = 1 + ) override; + void forceIDR() override { m_forceIDR = true; } + VideoCodec codec() const override { return VideoCodec::H264; } + const char* backendName() const override { return "x264"; } + + CX264Encoder(); + ~CX264Encoder() override; +}; + +#else extern "C" { #include #include } -#include "common/config.h" - class CX264Encoder : public VideoEncoderBase { private: @@ -42,3 +82,5 @@ public: CX264Encoder(); ~CX264Encoder() override; }; + +#endif // DISABLE_X264_FOR_TEST