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 <x264/x264.h> 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.
This commit is contained in:
yuanyuanxiang committed 2026-10-30 11:04:44 +01:00
1 parent 1d875ddbd1
commit 82f59b2469
3 files changed
+61 -6

No files matched your search

+44 -2
View File
@@ -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 <libyuv\libyuv.h>
#include <x264\x264.h>
}
#include "common/config.h"
class CX264Encoder : public VideoEncoderBase
{
private:
@@ -42,3 +82,5 @@ public:
CX264Encoder();
~CX264Encoder() override;
};
#endif // DISABLE_X264_FOR_TEST