Browse Source

Vastly speed up cython; misc other changes

master
Theelgirl 7 months ago
parent
commit
69b64cca77
  1. 2
      fvid/cythonizer.py
  2. 26
      fvid/fvid.py
  3. 1465
      fvid/fvid_cython.cpp
  4. 33
      fvid/fvid_cython.pyx
  5. 4
      requirements.txt

2
fvid/cythonizer.py

@ -4,4 +4,4 @@ from distutils.core import Extension, setup
from Cython.Build import cythonize
ext = Extension(name="fvid_cython", sources=["fvid_cython.pyx"], include_dirs=['/root/fvid', '/root/fvid/tests'])
setup(ext_modules=cythonize(ext, compiler_directives={'language_level': 3, 'infer_types': True}))
setup(ext_modules=cythonize(ext, annotate=True, compiler_directives={'language_level': 3, 'infer_types': True}))

26
fvid/fvid.py

@ -23,7 +23,7 @@ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from Crypto.Cipher import AES
try:
from fvid_cython import cy_gbfi, cy_gbfi_h265
from fvid_cython import cy_gbfi, cy_gbfi_h265, cy_encode_zfec
use_cython = True
except (ImportError, ModuleNotFoundError):
@ -93,19 +93,21 @@ def encode_zfec(bit_array: BitArray) -> BitArray:
bits = bit_array.bin
# split bits into blocks of bits
byte_list = split_string_by_n(bits, BLOCK)
ecc_bytes = ""
if use_cython:
return BitArray(bytes=cy_encode_zfec(bits).encode('utf-8'))
else:
# split bits into blocks of bits
byte_list = split_string_by_n(bits, BLOCK)
ecc_bytes = ""
print("Applying Zfec Error Correction...")
print("Applying Zfec Error Correction...")
encoder = ef.Encoder(KVAL, MVAL)
for b in tqdm(byte_list):
ecc_bytes += ''.join(map(bytes.decode, encoder.encode(b.encode('utf-8'))))
encoder = ef.Encoder(KVAL, MVAL)
for b in tqdm(byte_list):
ecc_bytes += ''.join(map(bytes.decode, encoder.encode(b.encode('utf-8'))))
return BitArray(bytes=ecc_bytes.encode('utf-8'))
return BitArray(bytes=ecc_bytes.encode('utf-8'))
def get_bits_from_file(
filepath: str, key: bytes, zfec: bool
@ -424,7 +426,7 @@ def make_image_sequence(bitstring: BitArray, resolution: tuple = (1920, 1080)):
index += 1
def make_video(output_filepath: str, framerate: int = FRAMERATE, use_h265: bool = False, overwrite: bool= = False):
def make_video(output_filepath: str, framerate: int = FRAMERATE, use_h265: bool = False, overwrite: bool = False):
"""
Create video using ffmpeg

1465
fvid/fvid_cython.cpp
File diff suppressed because it is too large
View File

33
fvid/fvid_cython.pyx

@ -2,8 +2,10 @@
# cython: boundscheck=False
# cython: cdivision=True
# cython: wraparound=False
# cython: nonecheck=False
# cython: c_string_type=unicode, c_string_encoding=ascii
from tqdm import tqdm
from zfec import easyfec as ef
from libcpp.string cimport string
cpdef str cy_gbfi(image):
@ -37,4 +39,31 @@ cpdef str cy_gbfi_h265(image):
for x in range(width):
bits.append(b'1' if px[x, y] == 255 else b'0')
return bits
return bits
cpdef str cy_encode_zfec(string bits):
cdef int b, KVAL = 4, MVAL = 5, BLOCK = 16
cdef string ecc_bytes = b'', byte
cdef tuple byte_tuple
# cdef (string, string, string, string, string) temp
byte_tuple = split_string_by_n(bits, BLOCK)
print("Applying Zfec Error Correction...")
encoder = ef.Encoder(KVAL, MVAL)
for b in tqdm(range(len(byte_tuple))):
for byte in encoder.encode(byte_tuple[b].encode('utf-8')):
ecc_bytes.append(byte)
return ecc_bytes
cdef tuple split_string_by_n(str bitstring, int n):
cdef list bit_list = []
for i in range(0, len(bitstring), n):
bit_list.append(bitstring[i:i+n])
return tuple(bit_list)

4
requirements.txt

@ -1,5 +1,5 @@
Pillow>=7.0.0
tqdm>=4.25.0
bitstring
pycryptography
bitstring>=3.1.6
pycryptography>=3.1.1
zfec
Loading…
Cancel
Save