Download

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

PartSupportedNot supported
ContainerBink 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 fileBink 2 (KB2) files, SMUSH-wrapped files
VideoBink 1 revisions f, g, h, i and k: every block type, the 16x16 scaled blocks, motion compensation, the integer inverse DCT, the alpha planeThe old revision b
AudioBink audio, DCT variant, mono or stereo, any sample rateThe 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.

Checked against real files: revision 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.

The Bink 1 containerFrom the start of the file: a 44-byte header, the audio track table with 12 bytes per track, the frame index with 4 bytes per frame, then the frames. One frame opens up into an audio packet for each track, each a 4-byte size and its data, followed by the video packet, which fills the rest of the frame.The file, start to endHeader44 bytesAudio tracks12 bytes eachFrame index4 bytes a frameFrame 0Frame 1…"BIK" + revisionfile sizeframe countlargest framewidth, heightframe rate (num/den)video flagsaudio track countlargest decodedsample rateflags: stereo,DCT or RDFTtrack ideach frame'soffset; the lowbit marks akeyframeOne frameAudioone packet a trackVideo packetrest of the frameEach audio packet is a 4-byte size, then its data.Revision k adds one more4-byte field after thetrack count.The reader checks every size against the header and the file before it reads.
The container, as the decoder's 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.

The Bink 1 video decode pipelineA video packet is decoded plane by plane. For each plane the decoder reads nine bundles: block types, sub-block types, colours, patterns, x offsets, y offsets, intra DC, inter DC and runs. Each 8x8 block has one of ten types: skip, scaled, motion, run, residue, intra, fill, inter, pattern and raw. Motion compensation copies from the previous picture, and an integer 8x8 inverse DCT decodes the transformed blocks. The result is a planar YUV 4:2:0 picture.Video packetone per framePlane by planealpha (if any), Y, then the two colour planes at half sizeBundles · Huffman coded, one of 16 trees eachblock typessub-block typescolourspatternsx, y offsetsintra DCinter DCrunsEach 8x8 block, by its typeSkipcopy, last frameMotioncopy, offsetResiduemotion + valuesIntermotion + DCTIntraDCTFillone colourPatterntwo colours, maskRunruns of coloursRaw64 valuesScaled16x16 from oneMotion copiesfrom the previous pictureInteger 8x8 inverse DCTput, or added to a copyPictureYUV 4:2:0, BT.601
A scaled block decodes one 8x8 block by a sub-block type (run, intra, fill, pattern or raw) and doubles it to 16x16. The decoder lays its planes out with FFmpeg's strides, so a motion vector that reaches past a row's end reads exactly what FFmpeg reads.

Audio: from packet to samples

The Bink audio decode path, DCT variantAn audio packet holds blocks. For each block and channel the decoder reads the first two coefficients, a quantiser for each of up to 25 frequency bands, then run-length coded coefficients scaled by their band's quantiser. An inverse DCT of 512, 1024 or 2048 points, chosen by the sample rate, turns them into samples, and the first sixteenth of each block crossfades with the end of the previous one. The output is interleaved 32-bit float samples.Audio packetone per track per frameBand quantisersup to 25 bands, per channelCoefficientsrun-length coded, scaledInverse DCT512, 1024 or 2048 pointsOverlapfirst 1/16 crossfadesSamplesinterleaved 32-bit floatTransform size by sample rate: 512 points under 22,050 Hz, 1024 under 44,100 Hz, 2048 from 44,100 Hz.
The DCT variant of Bink audio. FFmpeg computes the inverse transform in single precision and this decoder in double precision, so the samples agree to rounding, not bit for bit.

How it was checked

  • Against FFmpeg, frame by frame. On a set of 56 revision i files (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.

Processor800 x 600720 x 480
A current x86-64 server coreabout 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 2014about 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 corebink-decoderFFmpeg 9.0.2
Movie 1 (2,940 frames): video only0.354 ms0.371 ms
Movie 1: video, then planar YUV 4:2:0 out0.373 ms0.395 ms
Movie 1: video and stereo audio0.381 ms0.396 ms
Movie 2 (7,314 frames): video only0.263 ms0.282 ms
Movie 2: video, then planar YUV 4:2:0 out0.279 ms0.305 ms
Movie 2: video and stereo audio0.280 ms0.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.