Tiny refactor of codecs (#1121)

* improve performance using pointers

* simplify decoder's collect

* simplify decoder's collect

* add threading to decoders

* fix test

* switch back to return bytes

* fix macos
This commit is contained in:
Henry Ruhs
2026-05-19 10:31:53 +02:00
committed by GitHub
parent c00ea92f35
commit fbacb24fcc
7 changed files with 128 additions and 169 deletions
+16 -22
View File
@@ -5,9 +5,11 @@ import pytest
from tests.assert_helper import get_test_example_file, get_test_examples_directory
from facefusion import state_manager
from facefusion.codecs.vpx_decoder import create, decode, destroy, read_resolution
from facefusion.codecs.vpx_decoder import create, decode, destroy
from facefusion.codecs.vpx_encoder import create as create_encoder, encode
from facefusion.common_helper import is_macos
from facefusion.download import conditional_download
from facefusion.hash_helper import create_hash
from facefusion.libraries import vpx as vpx_module
from facefusion.vision import read_video_frame
@@ -21,40 +23,32 @@ def before_all() -> None:
vpx_module.pre_check()
#TODO: needs review
def test_create() -> None:
assert create()
assert create(1)
with patch('facefusion.codecs.vpx_decoder.vpx_module.create_static_library', return_value = None):
assert create(1) is None
#TODO: needs review
def test_decode() -> None:
vision_frame = read_video_frame(get_test_example_file('target-240p.mp4'))
video_buffer = cv2.cvtColor(vision_frame, cv2.COLOR_BGR2YUV_I420).tobytes()
video_resolution = (vision_frame.shape[1], vision_frame.shape[0])
vpx_encoder = create_encoder(video_resolution, 1000, 1, 0)
encoded_buffer = encode(vpx_encoder, video_buffer, video_resolution, 0)
vpx_decoder = create()
vpx_pointer = decode(create(1), encoded_buffer)
assert len(decode(vpx_decoder, encoded_buffer)) == video_resolution[0] * video_resolution[1] * 3 // 2
assert decode(vpx_decoder, bytes()) == bytes()
assert vpx_pointer is not None
assert vpx_pointer.get('resolution') == video_resolution
assert len(vpx_pointer.get('buffer')) == video_resolution[0] * video_resolution[1] * 3 // 2
assert decode(create(1), bytes()) is None
if is_macos():
assert create_hash(bytes(vpx_pointer.get('buffer'))) == '87450f70'
#TODO: needs review
def test_read_resolution() -> None:
vision_frame = read_video_frame(get_test_example_file('target-240p.mp4'))
video_buffer = cv2.cvtColor(vision_frame, cv2.COLOR_BGR2YUV_I420).tobytes()
video_resolution = (vision_frame.shape[1], vision_frame.shape[0])
vpx_encoder = create_encoder(video_resolution, 1000, 1, 0)
encoded_buffer = encode(vpx_encoder, video_buffer, video_resolution, 0)
vpx_decoder = create()
assert read_resolution(vpx_decoder, encoded_buffer) == video_resolution
assert read_resolution(vpx_decoder, bytes()) is None
#TODO: needs review
def test_destroy() -> None:
vpx_decoder = create()
vpx_decoder = create(1)
with patch.object(vpx_module.create_static_library(), 'vpx_codec_destroy') as mock:
destroy(vpx_decoder)