mirror of
1
Fork 0

[chore] pull in go-ffmpreg v0.4.1 (#3485)

* pull in go-ffmpreg v0.4.1

* bring back GTS_WAZERO_COMPILATION_CACHE
This commit is contained in:
kim 2024-10-28 10:55:48 +00:00 committed by GitHub
parent 8f4a96c7cd
commit e86592bc32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 158 additions and 41 deletions

2
go.mod
View File

@ -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

4
go.sum generated
View File

@ -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=

View File

@ -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
}

Binary file not shown.

Binary file not shown.

74
vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.go generated vendored Normal file
View File

@ -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:])
}

View File

@ -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)

67
vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go generated vendored Normal file
View File

@ -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
}

2
vendor/modules.txt vendored
View File

@ -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