libx264 encoding experiment

This commit is contained in:
Ali 2022-02-08 23:35:50 +04:00
parent ff07040ad6
commit 090673b4c9
13 changed files with 1386 additions and 9 deletions

3
.gitmodules vendored
View File

@ -26,3 +26,6 @@ url=../tgcalls.git
[submodule "third-party/webrtc/webrtc"]
path = third-party/webrtc/webrtc
url = https://github.com/ali-fareed/webrtc.git
[submodule "third-party/libx264/x264"]
path = third-party/libx264/x264
url = https://github.com/mirror/x264.git

View File

@ -1419,7 +1419,7 @@ public final class PresentationGroupCallImpl: PresentationGroupCall {
strongSelf.requestCall(movingFromBroadcastToRtc: false)
}
}
}, outgoingAudioBitrateKbit: outgoingAudioBitrateKbit, videoContentType: self.isVideoEnabled ? .generic : .none, enableNoiseSuppression: false)
}, outgoingAudioBitrateKbit: outgoingAudioBitrateKbit, videoContentType: self.isVideoEnabled ? .generic : .none, enableNoiseSuppression: false, preferX264: self.accountContext.sharedContext.immediateExperimentalUISettings.preferredVideoCodec == "H264")
self.genericCallContext = genericCallContext
self.stateVersionValue += 1
@ -2668,7 +2668,7 @@ public final class PresentationGroupCallImpl: PresentationGroupCall {
self.hasScreencast = true
let screencastCallContext = OngoingGroupCallContext(video: self.screencastCapturer, requestMediaChannelDescriptions: { _, _ in EmptyDisposable }, audioStreamData: nil, rejoinNeeded: { }, outgoingAudioBitrateKbit: nil, videoContentType: .screencast, enableNoiseSuppression: false)
let screencastCallContext = OngoingGroupCallContext(video: self.screencastCapturer, requestMediaChannelDescriptions: { _, _ in EmptyDisposable }, audioStreamData: nil, rejoinNeeded: { }, outgoingAudioBitrateKbit: nil, videoContentType: .screencast, enableNoiseSuppression: false, preferX264: false)
self.screencastCallContext = screencastCallContext
self.screencastJoinDisposable.set((screencastCallContext.joinPayload

View File

@ -363,7 +363,7 @@ public final class OngoingGroupCallContext {
private var broadcastPartsSource: BroadcastPartSource?
init(queue: Queue, inputDeviceId: String, outputDeviceId: String, video: OngoingCallVideoCapturer?, requestMediaChannelDescriptions: @escaping (Set<UInt32>, @escaping ([MediaChannelDescription]) -> Void) -> Disposable, audioStreamData: AudioStreamData?, rejoinNeeded: @escaping () -> Void, outgoingAudioBitrateKbit: Int32?, videoContentType: VideoContentType, enableNoiseSuppression: Bool) {
init(queue: Queue, inputDeviceId: String, outputDeviceId: String, video: OngoingCallVideoCapturer?, requestMediaChannelDescriptions: @escaping (Set<UInt32>, @escaping ([MediaChannelDescription]) -> Void) -> Disposable, audioStreamData: AudioStreamData?, rejoinNeeded: @escaping () -> Void, outgoingAudioBitrateKbit: Int32?, videoContentType: VideoContentType, enableNoiseSuppression: Bool, preferX264: Bool) {
self.queue = queue
var networkStateUpdatedImpl: ((GroupCallNetworkState) -> Void)?
@ -473,7 +473,8 @@ public final class OngoingGroupCallContext {
},
outgoingAudioBitrateKbit: outgoingAudioBitrateKbit ?? 32,
videoContentType: _videoContentType,
enableNoiseSuppression: enableNoiseSuppression
enableNoiseSuppression: enableNoiseSuppression,
preferX264: preferX264
)
let queue = self.queue
@ -873,10 +874,10 @@ public final class OngoingGroupCallContext {
}
}
public init(inputDeviceId: String = "", outputDeviceId: String = "", video: OngoingCallVideoCapturer?, requestMediaChannelDescriptions: @escaping (Set<UInt32>, @escaping ([MediaChannelDescription]) -> Void) -> Disposable, audioStreamData: AudioStreamData?, rejoinNeeded: @escaping () -> Void, outgoingAudioBitrateKbit: Int32?, videoContentType: VideoContentType, enableNoiseSuppression: Bool) {
public init(inputDeviceId: String = "", outputDeviceId: String = "", video: OngoingCallVideoCapturer?, requestMediaChannelDescriptions: @escaping (Set<UInt32>, @escaping ([MediaChannelDescription]) -> Void) -> Disposable, audioStreamData: AudioStreamData?, rejoinNeeded: @escaping () -> Void, outgoingAudioBitrateKbit: Int32?, videoContentType: VideoContentType, enableNoiseSuppression: Bool, preferX264: Bool) {
let queue = self.queue
self.impl = QueueLocalObject(queue: queue, generate: {
return Impl(queue: queue, inputDeviceId: inputDeviceId, outputDeviceId: outputDeviceId, video: video, requestMediaChannelDescriptions: requestMediaChannelDescriptions, audioStreamData: audioStreamData, rejoinNeeded: rejoinNeeded, outgoingAudioBitrateKbit: outgoingAudioBitrateKbit, videoContentType: videoContentType, enableNoiseSuppression: enableNoiseSuppression)
return Impl(queue: queue, inputDeviceId: inputDeviceId, outputDeviceId: outputDeviceId, video: video, requestMediaChannelDescriptions: requestMediaChannelDescriptions, audioStreamData: audioStreamData, rejoinNeeded: rejoinNeeded, outgoingAudioBitrateKbit: outgoingAudioBitrateKbit, videoContentType: videoContentType, enableNoiseSuppression: enableNoiseSuppression, preferX264: preferX264)
})
}

View File

@ -84,6 +84,7 @@ objc_library(
"//submodules/ffmpeg:ffmpeg",
"//third-party/rnnoise:rnnoise",
"//third-party/libyuv:libyuv",
"//third-party/libx264:libx264",
],
sdk_frameworks = [
"Foundation",

View File

@ -349,7 +349,8 @@ typedef NS_ENUM(int32_t, OngoingGroupCallRequestedVideoQuality) {
requestVideoBroadcastPart:(id<OngoingGroupCallBroadcastPartTask> _Nonnull (^ _Nonnull)(int64_t, int64_t, int32_t, OngoingGroupCallRequestedVideoQuality, void (^ _Nonnull)(OngoingGroupCallBroadcastPart * _Nullable)))requestVideoBroadcastPart
outgoingAudioBitrateKbit:(int32_t)outgoingAudioBitrateKbit
videoContentType:(OngoingGroupCallVideoContentType)videoContentType
enableNoiseSuppression:(bool)enableNoiseSuppression;
enableNoiseSuppression:(bool)enableNoiseSuppression
preferX264:(bool)preferX264;
- (void)stop;

View File

@ -1363,11 +1363,14 @@ private:
requestVideoBroadcastPart:(id<OngoingGroupCallBroadcastPartTask> _Nonnull (^ _Nonnull)(int64_t, int64_t, int32_t, OngoingGroupCallRequestedVideoQuality, void (^ _Nonnull)(OngoingGroupCallBroadcastPart * _Nullable)))requestVideoBroadcastPart
outgoingAudioBitrateKbit:(int32_t)outgoingAudioBitrateKbit
videoContentType:(OngoingGroupCallVideoContentType)videoContentType
enableNoiseSuppression:(bool)enableNoiseSuppression {
enableNoiseSuppression:(bool)enableNoiseSuppression
preferX264:(bool)preferX264 {
self = [super init];
if (self != nil) {
_queue = queue;
tgcalls::PlatformInterface::SharedInstance()->preferX264 = preferX264;
_sinks = [[NSMutableDictionary alloc] init];
_networkStateUpdated = [networkStateUpdated copy];

@ -1 +1 @@
Subproject commit ac28964554b5f2d8cd5685f3b66c75a282978cdb
Subproject commit 20860ca29147b4faa4f0b75a0da58517d9d4856c

211
third-party/libx264/BUILD vendored Normal file
View File

@ -0,0 +1,211 @@
headers = [
"x264.h",
"x264_config.h",
]
libs = [
"x264",
]
filegroup(
name = "libx264_sources",
srcs = glob([
"x264/**/*"
], exclude = [
"x264/.git/**/*"
]),
)
genrule(
name = "lib264_build",
srcs = [
"build-libx264-bazel.sh",
":libx264_sources",
],
cmd_bash =
"""
set -ex
if [ "$(TARGET_CPU)" == "ios_armv7" ]; then
BUILD_ARCH="armv7"
PLATFORM_HEADER_DIR="armv7-darwin-gcc"
elif [ "$(TARGET_CPU)" == "ios_arm64" ]; then
BUILD_ARCH="arm64"
PLATFORM_HEADER_DIR="arm64-darwin-gcc"
elif [ "$(TARGET_CPU)" == "ios_sim_arm64" ]; then
BUILD_ARCH="sim_arm64"
PLATFORM_HEADER_DIR="arm64-iphonesimulator-gcc"
elif [ "$(TARGET_CPU)" == "ios_x86_64" ]; then
BUILD_ARCH="x86_64"
PLATFORM_HEADER_DIR="x86_64-iphonesimulator-gcc"
else
echo "Unsupported architecture $(TARGET_CPU)"
fi
BUILD_DIR="$(RULEDIR)/build_$${BUILD_ARCH}"
rm -rf "$$BUILD_DIR"
mkdir -p "$$BUILD_DIR"
YASM_DIR="$$BUILD_DIR/yasm"
rm -rf "$$YASM_DIR"
mkdir -p "$$YASM_DIR"
tar -xf "$(location //third-party/yasm:yasm.tar)" -C "$$YASM_DIR"
ABS_YASM_DIR="$$(pwd)/$$(dirname $$YASM_DIR)/$$(basename $$YASM_DIR)"
cp $(location :build-libx264-bazel.sh) "$$BUILD_DIR/"
SOURCE_PATH="third-party/libx264/x264"
cp -R "$$SOURCE_PATH" "$$BUILD_DIR/"
mkdir -p "$$BUILD_DIR/Public/libx264"
PATH="$$PATH:$$ABS_YASM_DIR" sh $$BUILD_DIR/build-libx264-bazel.sh $$BUILD_ARCH "$$BUILD_DIR/x264" "$$BUILD_DIR"
""" +
"\n".join([
"cp -f \"$$BUILD_DIR/scratch/thin/include/{}\" \"$(location Public/libx264/{})\"".format(header, header) for header in headers
]) +
"\n" +
"\n".join([
"cp -f \"$$BUILD_DIR/scratch/thin/lib/lib{}.a\" \"$(location Public/libx264/lib{}.a)\"".format(lib, lib) for lib in libs
]) +
"\n",
outs = ["Public/libx264/" + x for x in headers] +
["Public/libx264/lib{}.a".format(x) for x in libs],
tools = [
"//third-party/yasm:yasm.tar",
],
visibility = [
"//visibility:public",
]
)
cc_library(
name = "libx264_lib",
srcs = [":Public/libx264/lib" + x + ".a" for x in libs],
)
cc_library(
name = "libx264",
srcs = [
"x264/common/osdep.c",
"x264/common/base.c",
"x264/common/cpu.c",
"x264/common/tables.c",
"x264/encoder/api.c",
"x264/common/mc.c",
"x264/common/predict.c",
"x264/common/pixel.c",
"x264/common/macroblock.c",
"x264/common/frame.c",
"x264/common/dct.c",
"x264/common/cabac.c",
"x264/common/common.c",
"x264/common/rectangle.c",
"x264/common/set.c",
"x264/common/quant.c",
"x264/common/deblock.c",
"x264/common/vlc.c",
"x264/common/mvpred.c",
"x264/common/bitstream.c",
"x264/encoder/analyse.c",
"x264/encoder/me.c",
"x264/encoder/ratecontrol.c",
"x264/encoder/set.c",
"x264/encoder/macroblock.c",
"x264/encoder/cabac.c",
"x264/encoder/cavlc.c",
"x264/encoder/encoder.c",
"x264/encoder/lookahead.c",
"x264/common/threadpool.c",
"x264/encoder/analyse.h",
"x264/encoder/macroblock.h",
"x264/encoder/me.h",
"x264/encoder/ratecontrol.h",
"x264/encoder/set.h",
"x264/common/base.h",
"x264/common/osdep.h",
"x264/common/cpu.h",
"x264/common/tables.h",
"x264/common/common.h",
"x264/common/cabac.h",
"x264/common/bitstream.h",
"x264/common/set.h",
"x264/common/predict.h",
"x264/common/pixel.h",
"x264/common/mc.h",
"x264/common/frame.h",
"x264/common/dct.h",
"x264/common/quant.h",
"x264/common/threadpool.h",
"x264/common/macroblock.h",
"x264/common/rectangle.h",
"Public/libx264/x264_config.h",
"config/config.h",
"x264/x264.h",
"x264/common/aarch64/asm-offsets.h",
"x264/common/aarch64/bitstream.h",
"x264/common/aarch64/deblock.h",
"x264/common/aarch64/dct.h",
"x264/common/aarch64/mc.h",
"x264/common/aarch64/predict.h",
"x264/common/aarch64/quant.h",
"x264/common/aarch64/pixel.h",
"x264/common/aarch64/cabac-a.S",
"x264/common/aarch64/dct-a.S",
"x264/common/aarch64/deblock-a.S",
"x264/common/aarch64/mc-a.S",
"x264/common/aarch64/pixel-a.S",
"x264/common/aarch64/predict-a.S",
"x264/common/aarch64/quant-a.S",
"x264/common/aarch64/bitstream-a.S",
"x264/common/aarch64/asm-offsets.c",
"x264/common/aarch64/mc-c.c",
"x264/common/aarch64/predict-c.c",
],
hdrs = [
"x264/encoder/rdo.c",
"x264/encoder/cavlc.c",
"x264/encoder/cabac.c",
"x264/encoder/slicetype.c",
"x264/common/aarch64/asm.S",
],
includes = [
"Public",
],
copts = [
"-Ithird-party/libx264/x264",
"-Ithird-party/libx264/config",
"-Ithird-party/libx264/Public/libx264",
"-DBIT_DEPTH=8",
"-DHIGH_BIT_DEPTH=0",
"-DPREFIX",
"-Dprivate_prefix=x264_8",
],
visibility = [
"//visibility:public",
],
)
objc_library(
name = "libx264_old",
module_name = "libx264_old",
enable_modules = True,
hdrs = [":Public/libx264/" + x for x in headers],
includes = [
"Public",
],
deps = [
":libx264_lib",
":libx264_from_source",
],
visibility = [
"//visibility:public",
],
)

1019
third-party/libx264/Public/libx264/x264.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,6 @@
#define X264_GPL 1
#define X264_INTERLACED 1
#define X264_BIT_DEPTH 8
#define X264_CHROMA_FORMAT 0
#define X264_VERSION ""
#define X264_POINTVER "0.164.x"

89
third-party/libx264/build-libx264-bazel.sh vendored Executable file
View File

@ -0,0 +1,89 @@
#! /bin/sh
set -e
set -x
RAW_ARCH="$1"
SOURCE_DIR=$(echo "$(cd "$(dirname "$2")"; pwd -P)/$(basename "$2")")
BUILD_DIR=$(echo "$(cd "$(dirname "$3")"; pwd -P)/$(basename "$3")")
SCRATCH="$BUILD_DIR/scratch"
#set -e
#devnull='> /dev/null 2>&1'
DEPLOYMENT_TARGET="9.0"
CONFIGURE_FLAGS="--enable-static --enable-pic --disable-cli --bit-depth=8 --disable-opencl"
echo "building $RAW_ARCH..."
mkdir -p "$SCRATCH/$RAW_ARCH"
cd "$SCRATCH/$RAW_ARCH"
ASFLAGS=
if [ "$RAW_ARCH" = "i386" -o "$RAW_ARCH" = "x86_64" ]
then
ARCH="$RAW_ARCH"
PLATFORM="iPhoneSimulator"
CPU=""
CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"
HOST="--host=i386-apple-darwin"
elif [ "$RAW_ARCH" = "sim_arm64" ]; then
ARCH="arm64"
PLATFORM="iPhoneSimulator"
CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET --target=arm64-apple-ios$DEPLOYMENT_TARGET-simulator"
HOST="--host=aarch64-apple-darwin"
else
ARCH="$RAW_ARCH"
PLATFORM="iPhoneOS"
HOST="--host=aarch64-apple-darwin"
XARCH="-arch aarch64"
CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET"
ASFLAGS="$CFLAGS"
if [ "$RAW_ARCH" = "arm64" ]
then
EXPORT="GASPP_FIX_XCODE5=1"
fi
fi
CFLAGS="-arch $ARCH"
#if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" -o "$ARCH" = "" ]; then
# PLATFORM="iPhoneSimulator"
# CPU=
# if [ "$ARCH" = "x86_64" ]
# then
# CFLAGS="$CFLAGS -mios-simulator-version-min=7.0"
# HOST=
# else
# CFLAGS="$CFLAGS -mios-simulator-version-min=5.0"
# HOST="--host=i386-apple-darwin"
# fi
#else
# PLATFORM="iPhoneOS"
# if [ $ARCH = "arm64" ]
# then
# HOST="--host=aarch64-apple-darwin"
# XARCH="-arch aarch64"
# else
# HOST="--host=arm-apple-darwin"
# XARCH="-arch arm"
# fi
# CFLAGS="$CFLAGS -fembed-bitcode -mios-version-min=7.0"
# ASFLAGS="$CFLAGS"
#fi
XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
CC="xcrun -sdk $XCRUN_SDK clang"
if [ $PLATFORM = "iPhoneOS" ]
then
export AS="$SOURCE_DIR/tools/gas-preprocessor.pl $XARCH -- $CC"
else
export -n AS
fi
CXXFLAGS="$CFLAGS"
LDFLAGS="$CFLAGS"
CC=$CC $SOURCE_DIR/configure $CONFIGURE_FLAGS $HOST --extra-cflags="$CFLAGS" --extra-asflags="$ASFLAGS" --extra-ldflags="$LDFLAGS" --prefix="$SCRATCH/thin" || exit 1
make -j3 install || exit 1

42
third-party/libx264/config/config.h vendored Normal file
View File

@ -0,0 +1,42 @@
#define HAVE_AARCH64 1
#define HAVE_NEON 1
#define ARCH_AARCH64 1
#define SYS_MACOSX 1
#define STACK_ALIGNMENT 16
#define HAVE_POSIXTHREAD 1
#define HAVE_THREAD 1
#define HAVE_LOG2F 1
#define HAVE_STRTOK_R 1
#define HAVE_CLOCK_GETTIME 1
#define HAVE_MMAP 1
#define HAVE_VECTOREXT 1
#define fseek fseeko
#define ftell ftello
#define HAVE_BITDEPTH8 1
#define HAVE_GPL 1
#define HAVE_INTERLACED 1
#define HAVE_MALLOC_H 0
#define HAVE_ALTIVEC 0
#define HAVE_ALTIVEC_H 0
#define HAVE_MMX 0
#define HAVE_ARMV6 0
#define HAVE_ARMV6T2 0
#define HAVE_BEOSTHREAD 0
#define HAVE_WIN32THREAD 0
#define HAVE_SWSCALE 0
#define HAVE_LAVF 0
#define HAVE_FFMS 0
#define HAVE_GPAC 0
#define HAVE_AVS 0
#define HAVE_CPU_COUNT 0
#define HAVE_OPENCL 0
#define HAVE_THP 0
#define HAVE_LSMASH 0
#define HAVE_X86_INLINE_ASM 0
#define HAVE_AS_FUNC 0
#define HAVE_INTEL_DISPATCHER 0
#define HAVE_MSA 0
#define HAVE_WINRT 0
#define HAVE_VSX 0
#define HAVE_ARM_INLINE_ASM 0
#define HAVE_BITDEPTH10 0

1
third-party/libx264/x264 vendored Submodule

@ -0,0 +1 @@
Subproject commit 5585eafe31c2299163dcb206e05e18be7dd93098