| 33 | # Main Transcoder class |
| 34 | # --------------------------------------------------------------------------- |
| 35 | class Transcoder: |
| 36 | def __init__(self, backend=TranscoderBackend.AUTO): |
| 37 | self._native = None |
| 38 | self._wasm = None |
| 39 | self.backend_name = None |
| 40 | self.backend = None |
| 41 | |
| 42 | use_native = False |
| 43 | |
| 44 | # ------------------------------------------------------------------ |
| 45 | # Try native backend first if AUTO or NATIVE |
| 46 | # ------------------------------------------------------------------ |
| 47 | if backend in (TranscoderBackend.AUTO, TranscoderBackend.NATIVE): |
| 48 | try: |
| 49 | native_mod = importlib.import_module("basisu_py.basisu_transcoder_python") |
| 50 | native_mod.init() |
| 51 | self._native = native_mod |
| 52 | self.backend = native_mod |
| 53 | self.backend_name = "NATIVE" |
| 54 | use_native = True |
| 55 | print("[Transcoder] Using native backend") |
| 56 | except Exception as e: |
| 57 | if backend == TranscoderBackend.NATIVE: |
| 58 | # Caller explicitly requested native - fail hard |
| 59 | raise RuntimeError(f"Native transcoder backend failed: {e}") |
| 60 | print("[Transcoder] Native backend unavailable, reason:", e) |
| 61 | self._native = None |
| 62 | |
| 63 | # ------------------------------------------------------------------ |
| 64 | # Fallback to WASM if native is not being used |
| 65 | # ------------------------------------------------------------------ |
| 66 | if not use_native: |
| 67 | try: |
| 68 | from basisu_py.wasm.wasm_transcoder import BasisuWasmTranscoder |
| 69 | except Exception as e: |
| 70 | raise RuntimeError( |
| 71 | f"WASM backend cannot be imported: {e}\n" |
| 72 | "Ensure that:\n" |
| 73 | " - 'wasmtime' is installed\n" |
| 74 | " - basisu_py/wasm/*.wasm files are present in the install\n" |
| 75 | ) |
| 76 | |
| 77 | wasm_path = Path(__file__).parent / "wasm" / "basisu_transcoder_module_st.wasm" |
| 78 | self._wasm = BasisuWasmTranscoder(str(wasm_path)) |
| 79 | self._wasm.load() |
| 80 | self.backend = self._wasm |
| 81 | self.backend_name = "WASM" |
| 82 | print("[Transcoder] Using WASM backend") |
| 83 | |
| 84 | # Finally, bind the unified API to whichever backend we chose |
| 85 | self._bind_backend(self.backend) |
| 86 | |
| 87 | # ----------------------------------------------------------------------- |
| 88 | # Unified backend binding (native or wasm) |
| 89 | # ----------------------------------------------------------------------- |
| 90 | def _bind_backend(self, b): |
| 91 | self.backend = b |
| 92 |
no outgoing calls