Built for Battlefront Eternal · GPL-3.0
bink-decoder
A decoder for Bink 1 video and Bink audio, written in safe Rust with no dependencies beyond the standard library. It is a port of FFmpeg's Bink demuxer and decoders, and it is how Battlefront Eternal plays the games' own movies straight from your files.
What it decodes
| Part | Supported | Not supported |
|---|---|---|
| Container | Bink 1 files (BIK plus a revision letter): header, audio track table, frame index, each frame's audio and video packets, read from any seekable source, also in place inside a larger file | Bink 2 (KB2) files, SMUSH-wrapped files |
| Video | Bink 1 revisions f, g, h, i and k: every block type, the 16x16 scaled blocks, motion compensation, the integer inverse DCT, the alpha plane | The old revision b |
| Audio | Bink audio, DCT variant, mono or stereo, any sample rate | The RDFT variant, revision b audio |
Pictures come out as planar YUV 4:2:0 (BT.601; limited range, full range for revision k) and sound as interleaved 32-bit float samples.
i video and stereo DCT audio at 48 kHz, and for this page one movie at 44.1 kHz. The other revisions are ported as FFmpeg has them and have so far run only in the synthetic tests and under the fuzzers.How Battlefront Eternal uses it
Battlefront (2004) keeps its menu movies back to back inside one archive in the game folder, Movies/shell.mvs, with more in each add-on's own archive. The app's preparation only lists them: it reads each movie's Bink header and notes where it starts. When a menu plays a movie, the engine opens your game folder, seeks to that movie's first byte and hands the file to the decoder, which reads one frame at a time. Nothing is converted or copied.
Frames are decoded on a worker thread into a pool of three, so the menu never waits on a picture. The renderer draws each one with a Catmull-Rom upscale to the screen pixels it covers and FSR 1's contrast-adaptive sharpening. Earlier previews converted the movies to WebM during preparation; with the decoder in the engine, that step and its copies are gone.
Battlefront II (2005) stores its movies the same way, and every one checked is the same kind of file, revision i at 800 x 600. Its in-game and trailer movies carry 44.1 kHz sound; one of its trailers, checked against FFmpeg for this page, matched frame for frame, with its sound within 0.0000003.
Inside a Bink file
A Bink 1 file is a fixed header, a table of audio tracks, an index with one entry per frame, then the frames. Each frame holds one audio packet per track, then the video packet.
BinkReader reads it. Because it reads one frame at a time from any seekable source, a movie can stay inside a larger archive.Video: from packet to picture
Each plane (the alpha plane when there is one, then brightness, then the two half-size colour planes) is cut into 8x8 blocks. The packet first carries nine streams of values, called bundles, each coded with one of 16 Huffman trees. Every block then takes what it needs from them according to its type.
Audio: from packet to samples
How it was checked
- Against FFmpeg, frame by frame. On a set of 56 revision
ifiles (800 x 600, 720 x 480 and 640 x 480, 25 to 30 frames a second, 23 of them with stereo 48 kHz DCT audio), every video frame was equal to FFmpeg's output byte for byte, and the largest audio difference was 0.000006. - Synthetic tests on every change. The bit reader, the 16 Huffman trees, the inverse DCT against a floating point reference, the plane layout, Bink files built bit by bit from fill, skip, pattern and raw blocks, an audio packet, and every truncation and thousands of fixed corruptions of a file, all without a panic.
- Fuzzing. Five cargo-fuzz targets cover the whole decoder, the video and audio decoders alone, the container reader and the bit-level readers. Each ran for 30 minutes after the last fix and 20 more on the present code without a finding, reaching every revision, all ten block types and all five scaled sub-block types.
- What fuzzing found, and fixed. A header declaring a huge frame made the reader allocate that much before reading (frames are now read in steps); a zero or oversized picture size was accepted; and a decoder that was reset, or rewound after a damaged first frame, could show pixels left from before. Each has a test.
No test data in the repository comes from a commercial product: its test files are written bit by bit by the tests themselves.
Speed
The repository's own measurements, on one thread, decoding revision i video and packing each picture into plain planes. A movie at 30 frames a second needs a frame every 33 ms.
| Processor | 800 x 600 | 720 x 480 |
|---|---|---|
| A current x86-64 server core | about 0.85 ms a frame (slowest 3.4 ms) | about 0.85 ms a frame (slowest 2.5 ms) |
| AMD A8-6410, a low-end laptop of 2014 | about 2.4 ms a frame (slowest 8 ms) | not measured |
Beside FFmpeg
On one core, bink-decoder does the same work in a little less time than a current FFmpeg build: FFmpeg needed 4 to 9 percent longer per frame on two of Battlefront II's movies. Given several cores, FFmpeg draws level.
| Same movie, one core | bink-decoder | FFmpeg 9.0.2 |
|---|---|---|
| Movie 1 (2,940 frames): video only | 0.354 ms | 0.371 ms |
| Movie 1: video, then planar YUV 4:2:0 out | 0.373 ms | 0.395 ms |
| Movie 1: video and stereo audio | 0.381 ms | 0.396 ms |
| Movie 2 (7,314 frames): video only | 0.263 ms | 0.282 ms |
| Movie 2: video, then planar YUV 4:2:0 out | 0.279 ms | 0.305 ms |
| Movie 2: video and stereo audio | 0.280 ms | 0.305 ms |
Measured on one core of an AMD Ryzen AI 9 HX 370 (pinned with taskset, FFmpeg run with -threads 1), decoding two of Battlefront II's own 800 x 600 revision i movies with stereo sound, using the version of bink-decoder the engine uses, built with cargo build --release. FFmpeg 9.0.2 is a current Linux distribution's build. Figures are net milliseconds per frame, medians of 7 to 9 runs, with start-up and file probing timed separately and taken out. FFmpeg's Bink decoders are single-threaded too, but with its default threads its command line tool reads, decodes audio and writes on other cores: then the two are level on video, and FFmpeg is 1 to 2 percent ahead with audio. In a separate run against the widely used static release build of FFmpeg 7.0.2, FFmpeg took 23 to 30 percent longer per frame on these movies' video, on one core. Every frame matched FFmpeg's byte for byte. Either needs well under a millisecond of the 33 ms a 30 frames a second movie allows.
Use it
Add the crate from its repository and read a movie frame by frame. To play a movie stored inside a larger file, seek the source to the movie's first byte before making the decoder.
Cargo.toml
[dependencies]
bink-decoder = { git = "https://github.com/centrevilletech/bink-decoder" }Rust
use std::fs::File;
use std::io::BufReader;
fn main() -> Result<(), bink_decoder::Error> {
let file = BufReader::new(File::open("movie.bik")?);
let mut movie = bink_decoder::Decoder::new(file)?;
let mut planes: [Vec<u8>; 3] = Default::default();
while let Some(frame) = movie.next_frame()? {
frame.picture.copy_i420(&mut planes); // Y, U, V without padding
let _samples = frame.audio.first(); // this frame's samples of track 0
}
Ok(())
}Licence
bink-decoder is licensed under the GNU General Public License, version 3 only. It is a derived work of FFmpeg 7.1's Bink demuxer, Bink video and audio decoders and their tables, copyright Konstantin Shishkov, Peter Ross and Daniel Verkamp, which FFmpeg distributes under the GNU Lesser General Public License, version 2.1 or later. Section 3 of that licence allows a copy of such code to be placed under the GNU General Public License, which this port does; each ported file keeps FFmpeg's copyright notice.
bink-decoder is an independent project. It is not affiliated with or endorsed by Epic Games Tools (formerly RAD Game Tools), the owner of the Bink format, or by the FFmpeg project. Bink is a trademark of its owner.