diff --git a/go.mod b/go.mod index 29da67ef6..18107be44 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( codeberg.org/gruf/go-debug v1.3.0 codeberg.org/gruf/go-errors/v2 v2.3.2 codeberg.org/gruf/go-fastcopy v1.1.3 - codeberg.org/gruf/go-ffmpreg v0.3.1 + codeberg.org/gruf/go-ffmpreg v0.4.2 codeberg.org/gruf/go-iotools v0.0.0-20240710125620-934ae9c654cf codeberg.org/gruf/go-kv v1.6.5 codeberg.org/gruf/go-list v0.0.0-20240425093752-494db03d641f diff --git a/go.sum b/go.sum index 3fdccd6fd..3c1d822c1 100644 --- a/go.sum +++ b/go.sum @@ -46,8 +46,8 @@ codeberg.org/gruf/go-fastcopy v1.1.3 h1:Jo9VTQjI6KYimlw25PPc7YLA3Xm+XMQhaHwKnM7x codeberg.org/gruf/go-fastcopy v1.1.3/go.mod h1:GDDYR0Cnb3U/AIfGM3983V/L+GN+vuwVMvrmVABo21s= codeberg.org/gruf/go-fastpath/v2 v2.0.0 h1:iAS9GZahFhyWEH0KLhFEJR+txx1ZhMXxYzu2q5Qo9c0= codeberg.org/gruf/go-fastpath/v2 v2.0.0/go.mod h1:3pPqu5nZjpbRrOqvLyAK7puS1OfEtQvjd6342Cwz56Q= -codeberg.org/gruf/go-ffmpreg v0.3.1 h1:5qE6sHQbLCbQ4RO7ZL4OKZBN4ViAYfDm9ExT8N0ZE7s= -codeberg.org/gruf/go-ffmpreg v0.3.1/go.mod h1:Ar5nbt3tB2Wr0uoaqV3wDBNwAx+H+AB/mV7Kw7NlZTI= +codeberg.org/gruf/go-ffmpreg v0.4.2 h1:HKkPapm/PWkxsnUdjyQOGpwl5Qoa2EBrUQ09s4R4/FA= +codeberg.org/gruf/go-ffmpreg v0.4.2/go.mod h1:Ar5nbt3tB2Wr0uoaqV3wDBNwAx+H+AB/mV7Kw7NlZTI= codeberg.org/gruf/go-iotools v0.0.0-20240710125620-934ae9c654cf h1:84s/ii8N6lYlskZjHH+DG6jyia8w2mXMZlRwFn8Gs3A= codeberg.org/gruf/go-iotools v0.0.0-20240710125620-934ae9c654cf/go.mod h1:zZAICsp5rY7+hxnws2V0ePrWxE0Z2Z/KXcN3p/RQCfk= codeberg.org/gruf/go-kv v1.6.5 h1:ttPf0NA8F79pDqBttSudPTVCZmGncumeNIxmeM9ztz0= diff --git a/internal/media/ffmpeg/wasm.go b/internal/media/ffmpeg/wasm.go index 4a230eec7..b23809d93 100644 --- a/internal/media/ffmpeg/wasm.go +++ b/internal/media/ffmpeg/wasm.go @@ -27,13 +27,8 @@ import ( ffprobelib "codeberg.org/gruf/go-ffmpreg/embed/ffprobe" "codeberg.org/gruf/go-ffmpreg/wasm" "github.com/tetratelabs/wazero" - "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" ) -// Use all core features required by ffmpeg / ffprobe -// (these should be the same but we OR just in case). -const corefeatures = wasm.CoreFeatures - var ( // shared WASM runtime instance. runtime wazero.Runtime @@ -91,38 +86,26 @@ func compileFfprobe(ctx context.Context) error { // initRuntime initializes the global wazero.Runtime, // if already initialized this function is a no-op. -func initRuntime(ctx context.Context) error { +func initRuntime(ctx context.Context) (err error) { if runtime != nil { return nil } - var cache wazero.CompilationCache + // Create new runtime config. + cfg := wazero.NewRuntimeConfig() if dir := os.Getenv("GTS_WAZERO_COMPILATION_CACHE"); dir != "" { - var err error - // Use on-filesystem compilation cache given by env. - cache, err = wazero.NewCompilationCacheWithDir(dir) + cache, err := wazero.NewCompilationCacheWithDir(dir) if err != nil { return err } + + // Update runtime config with cache. + cfg = cfg.WithCompilationCache(cache) } - // Prepare config with cache. - cfg := wazero.NewRuntimeConfig() - cfg = cfg.WithCoreFeatures(corefeatures) - cfg = cfg.WithCompilationCache(cache) - - // Instantiate runtime with prepared config. - rt := wazero.NewRuntimeWithConfig(ctx, cfg) - - // Instantiate wasi snapshot preview features into runtime. - _, err := wasi_snapshot_preview1.Instantiate(ctx, rt) - if err != nil { - return err - } - - // Set runtime. - runtime = rt - return nil + // Initialize new runtime from config. + runtime, err = wasm.NewRuntime(ctx, cfg) + return } diff --git a/vendor/codeberg.org/gruf/go-ffmpreg/embed/ffmpeg/ffmpeg.wasm b/vendor/codeberg.org/gruf/go-ffmpreg/embed/ffmpeg/ffmpeg.wasm index e2083fffd..9d1faa3ed 100644 Binary files a/vendor/codeberg.org/gruf/go-ffmpreg/embed/ffmpeg/ffmpeg.wasm and b/vendor/codeberg.org/gruf/go-ffmpreg/embed/ffmpeg/ffmpeg.wasm differ diff --git a/vendor/codeberg.org/gruf/go-ffmpreg/embed/ffprobe/ffprobe.wasm b/vendor/codeberg.org/gruf/go-ffmpreg/embed/ffprobe/ffprobe.wasm index b460fa0f9..0094c53f4 100644 Binary files a/vendor/codeberg.org/gruf/go-ffmpreg/embed/ffprobe/ffprobe.wasm and b/vendor/codeberg.org/gruf/go-ffmpreg/embed/ffprobe/ffprobe.wasm differ diff --git a/vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.go b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.go new file mode 100644 index 000000000..a809ff120 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.go @@ -0,0 +1,74 @@ +package wasm + +import ( + "context" + + "github.com/tetratelabs/wazero/api" + "github.com/tetratelabs/wazero/experimental" +) + +type snapshotskey struct{} + +// withSetjmpLongjmp updates the context to contain wazero/experimental.Snapshotter{} support, +// and embeds the necessary snapshots map required for later calls to Setjmp() / Longjmp(). +func withSetjmpLongjmp(ctx context.Context) context.Context { + snapshots := make(map[uint32]experimental.Snapshot, 10) + ctx = experimental.WithSnapshotter(ctx) + ctx = context.WithValue(ctx, snapshotskey{}, snapshots) + return ctx +} + +func getSnapshots(ctx context.Context) map[uint32]experimental.Snapshot { + v, _ := ctx.Value(snapshotskey{}).(map[uint32]experimental.Snapshot) + return v +} + +// setjmp implements the C function: setjmp(env jmp_buf) +func setjmp(ctx context.Context, mod api.Module, stack []uint64) { + + // Input arguments. + envptr := api.DecodeU32(stack[0]) + + // Take snapshot of current execution environment. + snapshotter := experimental.GetSnapshotter(ctx) + snapshot := snapshotter.Snapshot() + + // Get stored snapshots map. + snapshots := getSnapshots(ctx) + if snapshots == nil { + panic("setjmp / longjmp not supported") + } + + // Set latest snapshot in map. + snapshots[envptr] = snapshot + + // Set return. + stack[0] = 0 +} + +// longjmp implements the C function: int longjmp(env jmp_buf, value int) +func longjmp(ctx context.Context, mod api.Module, stack []uint64) { + + // Input arguments. + envptr := api.DecodeU32(stack[0]) + // val := stack[1] + + // Get stored snapshots map. + snapshots := getSnapshots(ctx) + if snapshots == nil { + panic("setjmp / longjmp not supported") + } + + // Get snapshot stored in map. + snapshot := snapshots[envptr] + if snapshot == nil { + panic("must first call setjmp") + } + + // Set return. + stack[0] = 0 + + // Restore execution and + // return passed value arg. + snapshot.Restore(stack[1:]) +} diff --git a/vendor/codeberg.org/gruf/go-ffmpreg/wasm/instance.go b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/run.go similarity index 82% rename from vendor/codeberg.org/gruf/go-ffmpreg/wasm/instance.go rename to vendor/codeberg.org/gruf/go-ffmpreg/wasm/run.go index d2eccd9e9..62ce2bc25 100644 --- a/vendor/codeberg.org/gruf/go-ffmpreg/wasm/instance.go +++ b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/run.go @@ -6,19 +6,9 @@ import ( "unsafe" "github.com/tetratelabs/wazero" - "github.com/tetratelabs/wazero/api" "github.com/tetratelabs/wazero/sys" ) -// CoreFeatures are the WebAssembly Core specification -// features our embedded binaries are compiled with. -const CoreFeatures = api.CoreFeatureSIMD | - api.CoreFeatureBulkMemoryOperations | - api.CoreFeatureNonTrappingFloatToIntConversion | - api.CoreFeatureMutableGlobal | - api.CoreFeatureReferenceTypes | - api.CoreFeatureSignExtensionOps - // Args encompasses a common set of // configuration options often passed to // wazero.Runtime on module instantiation. @@ -64,6 +54,9 @@ func Run( modcfg = args.Config(modcfg) } + // Enable setjmp longjmp. + ctx = withSetjmpLongjmp(ctx) + // Instantiate the module from precompiled wasm module data. mod, err := runtime.InstantiateModule(ctx, module, modcfg) diff --git a/vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go new file mode 100644 index 000000000..328a26193 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go @@ -0,0 +1,67 @@ +package wasm + +import ( + "context" + + "github.com/tetratelabs/wazero" + "github.com/tetratelabs/wazero/api" + "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" +) + +// CoreFeatures are the WebAssembly Core specification +// features our embedded binaries are compiled with. +const CoreFeatures = api.CoreFeatureSIMD | + api.CoreFeatureBulkMemoryOperations | + api.CoreFeatureNonTrappingFloatToIntConversion | + api.CoreFeatureMutableGlobal | + api.CoreFeatureReferenceTypes | + api.CoreFeatureSignExtensionOps + +// NewRuntime returns a new WebAssembly wazero.Runtime compatible with go-ffmpreg. +func NewRuntime(ctx context.Context, cfg wazero.RuntimeConfig) (wazero.Runtime, error) { + var err error + + if cfg == nil { + // Ensure runtime config is set. + cfg = wazero.NewRuntimeConfig() + } + + // Set core features ffmpeg compiled with. + cfg = cfg.WithCoreFeatures(CoreFeatures) + + // Instantiate runtime with prepared config. + rt := wazero.NewRuntimeWithConfig(ctx, cfg) + + // Prepare default "env" host module. + env := rt.NewHostModuleBuilder("env") + + // Register setjmp host function. + env = env.NewFunctionBuilder(). + WithGoModuleFunction( + api.GoModuleFunc(setjmp), + []api.ValueType{api.ValueTypeI32}, + []api.ValueType{api.ValueTypeI32}, + ).Export("setjmp") + + // Register longjmp host function. + env = env.NewFunctionBuilder(). + WithGoModuleFunction( + api.GoModuleFunc(longjmp), + []api.ValueType{api.ValueTypeI32, api.ValueTypeI32}, + []api.ValueType{}, + ).Export("longjmp") + + // Instantiate "env" module. + _, err = env.Instantiate(ctx) + if err != nil { + return nil, err + } + + // Instantiate the wasi snapshot preview 1 in runtime. + _, err = wasi_snapshot_preview1.Instantiate(ctx, rt) + if err != nil { + return nil, err + } + + return rt, nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 97e6f84c6..89e3a715e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -24,7 +24,7 @@ codeberg.org/gruf/go-fastcopy # codeberg.org/gruf/go-fastpath/v2 v2.0.0 ## explicit; go 1.14 codeberg.org/gruf/go-fastpath/v2 -# codeberg.org/gruf/go-ffmpreg v0.3.1 +# codeberg.org/gruf/go-ffmpreg v0.4.2 ## explicit; go 1.22.0 codeberg.org/gruf/go-ffmpreg/embed/ffmpeg codeberg.org/gruf/go-ffmpreg/embed/ffprobe