ffmpeg_wasi/
lib.rs

1//! FFMPEG low-level bindings for Rust, precompiled for WebAssembly/WASI.
2//!
3//! This crate bundles FFMPEG's `avcodec` and `avformat` libraries, precompiled for WebAssembly. No native installation required.
4//!
5//! Compatible with Fastly Compute.
6//!
7//! These are *low-level* bindings, directly exposing the original C functions to Rust.
8//!
9//! ## Usage
10//!
11//! ```toml
12//! [dependencies]
13//! ffmpeg-wasi = "0"
14//! ```
15
16#![allow(
17    non_camel_case_types,
18    clashing_extern_declarations,
19    non_upper_case_globals,
20    non_snake_case,
21    improper_ctypes
22)]
23
24pub mod avcodec;
25pub mod avfilter;
26pub mod avformat;
27pub mod avutil;
28pub mod swscale;
29
30use std::alloc::{GlobalAlloc, Layout};
31
32/// Allocator backed by the ffmpeg memory allocation functions.
33///
34/// Must be set as a global allocator with:
35///
36/// ```rust
37/// #[global_allocator]
38/// static ALLOCATOR: FFMpegAllocator = FFMpegAllocator;
39/// ```
40pub struct FFMpegAllocator;
41
42unsafe impl GlobalAlloc for FFMpegAllocator {
43    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
44        avutil::av_malloc(layout.size()) as *mut _
45    }
46    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
47        avutil::av_free(ptr as *mut _)
48    }
49}
50
51#[global_allocator]
52static ALLOCATOR: FFMpegAllocator = FFMpegAllocator;
53
54#[test]
55pub fn test_ffmpeg() {
56    unsafe {
57        avcodec::avcodec_register_all();
58    }
59}