From 430b16ce56a6e5dc97cd606aa126e97af5abd9af Mon Sep 17 00:00:00 2001 From: Henry Ruhs Date: Sat, 9 May 2026 11:05:30 +0200 Subject: [PATCH] introduce opus and vpx to libraries (#1091) --- facefusion/libraries/opus.py | 44 ++++++++++++++++++++++++++++++ facefusion/libraries/vpx.py | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 facefusion/libraries/opus.py create mode 100644 facefusion/libraries/vpx.py diff --git a/facefusion/libraries/opus.py b/facefusion/libraries/opus.py new file mode 100644 index 00000000..1adb6abb --- /dev/null +++ b/facefusion/libraries/opus.py @@ -0,0 +1,44 @@ +import ctypes +from functools import lru_cache +from typing import Optional + +from facefusion.common_helper import is_linux, is_macos, is_windows + + +def resolve_library_file() -> Optional[str]: + if is_linux(): + return 'libopus.so.0' + if is_macos(): + return 'libopus.dylib' + if is_windows(): + return 'opus.dll' + return None + + +@lru_cache +def create_static_library() -> Optional[ctypes.CDLL]: + library_file = resolve_library_file() + + if library_file: + library = ctypes.CDLL(library_file) + + if library: + return init_ctypes(library) + + return None + + +def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL: + library.opus_encoder_create.argtypes = [ ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int) ] + library.opus_encoder_create.restype = ctypes.c_void_p + + library.opus_encode_float.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_int ] + library.opus_encode_float.restype = ctypes.c_int + + library.opus_encoder_destroy.argtypes = [ ctypes.c_void_p ] + library.opus_encoder_destroy.restype = None + + library.opus_encoder_ctl.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.c_int ] + library.opus_encoder_ctl.restype = ctypes.c_int + + return library diff --git a/facefusion/libraries/vpx.py b/facefusion/libraries/vpx.py new file mode 100644 index 00000000..8a62cf4e --- /dev/null +++ b/facefusion/libraries/vpx.py @@ -0,0 +1,53 @@ +import ctypes +from functools import lru_cache +from typing import Optional + +from facefusion.common_helper import is_linux, is_macos, is_windows + + +def resolve_library_file() -> Optional[str]: + if is_linux(): + return 'libvpx.so' + if is_macos(): + return 'libvpx.dylib' + if is_windows(): + return 'vpx.dll' + return None + + +@lru_cache +def create_static_library() -> Optional[ctypes.CDLL]: + library_file = resolve_library_file() + + if library_file: + library = ctypes.CDLL(library_file) + + if library: + return init_ctypes(library) + + return None + + +def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL: + library.vpx_codec_enc_config_default.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint ] + library.vpx_codec_enc_config_default.restype = ctypes.c_int + + library.vpx_codec_enc_init_ver.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_long, ctypes.c_int ] + library.vpx_codec_enc_init_ver.restype = ctypes.c_int + + library.vpx_codec_encode.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int64, ctypes.c_ulong, ctypes.c_long, ctypes.c_ulong ] + library.vpx_codec_encode.restype = ctypes.c_int + + library.vpx_codec_get_cx_data.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p) ] + library.vpx_codec_get_cx_data.restype = ctypes.c_void_p + + library.vpx_codec_destroy.argtypes = [ ctypes.c_void_p ] + library.vpx_codec_destroy.restype = ctypes.c_int + + library.vpx_img_wrap.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_void_p ] + library.vpx_img_wrap.restype = ctypes.c_void_p + + library.vpx_codec_control_.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.c_int ] + library.vpx_codec_control_.restype = ctypes.c_int + + return library