Research-2318: Python package bindings design and usage documentation¶
Question¶
How should the modernized VMAFx C API (libvmafx.so.1) be exposed, consumed, and documented in Python (vmafx), supporting zero-copy frame ingestion, explicit library loading, and safe error propagation without foreign C stack unwinding?
Sources¶
- Single-source API definition:
core/api/vmafx.toml - Codegen script:
scripts/codegen/vmafx_api/(ADR-1852) - C ABI headers:
core/include/vmafx/ - Python ctypes binding:
bindings/python/vmafx/_api.pyandbindings/python/pyproject.toml - Prior research: Research-2158 (section 1.1 / 1.2 on Python CLI reliance vs direct bindings)
Findings¶
- Python buffer protocol: Zero-copy frame ingestion is supported for contiguous buffer-protocol objects (
memoryview.contiguous) whose plane dimensions and strides matchFrameDescgeometry (_buffer_view()and_validate_one_plane()). Non-contiguous buffers or invalid geometries are rejected withValueError. - Layout verification:
_check_layout()runs at import time, validating the size and field offsets of the generatedLAYOUTmapping against thectypes.sizeofand field offsets of the ctypes structs, raisingImportErroron any discrepancy before dynamic library loading. - Library loading:
load()requires either an explicit path argument or theVMAFX_LIBRARYenvironment variable. The binding does not implement an implicit path search fallback; if given a bare soname (such aslibvmafx.so.1), resolution is handled by the platform dynamic linker using standard system search paths. - Context and resource cleanup: Resource-owning classes (
Context,Model,ModelSet,Device,Frame,FramePool,FrameConverter,Options,ReportFile,Window,WindowClock,DnnSession,McpServer) implement the context-manager protocol (__enter__/__exit__).Libraryrepresents the loaded library handle and does not implement__enter__/__exit__. - Error and callback safety: Nonzero C status codes raise
VmafxErrororVmafxPending. In asynchronous tumbling window operations,WindowClock.frame()andWindowClock.finish()returnNonewhen a window span has not yet completed (Status.PENDING). Python callbacks (log_callbackinContextorLibrary.context(), andon_completeinContext.window_submit()) intercept Python exceptions at the FFI boundary, storing them in_CallbackStateto prevent foreign unwinding into C.
Alternatives explored¶
- Implementing
__enter__/__exit__onLibrary:Libraryhas noclose()method or native destroy handle sincectypes.CDLLhandles are managed by Python's garbage collector and OS dlopen reference counts. Adding a no-op context manager was rejected in favor of keeping the context manager protocol restricted to actual resource-owning handles. - Passing callbacks in dataclasses (
ContextConfig/WindowRequest): C ABI structs require C callback function pointers and user data contexts. Instead of polluting frozen dataclasses with Python callables, Python callbacks are passed as keyword arguments to methods (Library.context(log_callback=...)andContext.window_submit(on_complete=...)), where they are wrapped in exception-trapping thunks.
Open questions¶
- Wheel distribution: Prebuilt wheels on PyPI for Linux, macOS, and Windows are tracked under issue #2318 for the upcoming 0.1.0 release.