JoshLambert.xyz

Small businesses, independent infrastructure, and a stubborn bet that things get better.

bink-decoder: A Rust Decoder for Bink Video

bink-decoder: A Rust Decoder for Bink Video

Star Wars: Battlefront, the 2004 game, plays movies behind its menus and flyovers of its maps. They are Bink files, and the game keeps its menu movies back to back inside one archive in the game folder.

Battlefront Eternal is a fan-made engine for that game, written in Rust and published by my company, Centreville Tech. You bring your own copy of the game and it plays from your files. Early previews handled the movies by converting every one of them to WebM while the game was being prepared, which meant a conversion step and a second copy of each movie on disk.

I wanted the engine to open the game folder and play what was already there. So I ported FFmpeg’s Bink demuxer and its Bink video and audio decoders to Rust, and released the port by itself as bink-decoder.

What it is

Bink is a proprietary video format for games. Its owner is Epic Games Tools, formerly RAD Game Tools. bink-decoder is an independent project, not affiliated with or endorsed by them or by the FFmpeg project. It is a port of the decoders in FFmpeg 7.1, and every ported file keeps FFmpeg’s copyright notice.

The crate is safe Rust, with unsafe forbidden for the whole crate, and it depends on nothing outside the standard library. You hand it anything that can read and seek, and it gives back one frame at a time:

let f = File::open("movie.bik")?;
let f = BufReader::new(f);
let mut bik = Decoder::new(f)?;

while let Some(frame) =
    bik.next_frame()?
{
    // frame.picture
    // frame.audio
    // frame.time
}

Pictures come out as planar YUV 4:2:0 and sound as 32-bit float samples. The decoder never loads the whole file. If the movie sits in the middle of a bigger file, you seek to its first byte before you make the decoder, and that is how Battlefront Eternal plays a movie out of the game’s archive without copying it anywhere.

How a frame gets decoded

A diagram titled “From a .bik file to pictures and sound.” A strip shows the file from start to end: a 44-byte header, the audio tracks at 12 bytes each, the frame index at 4 bytes a frame, then the frames. One frame is opened up into a 4-byte size, an audio packet for each track, and a video packet that fills the rest. Four numbered steps follow. One, BinkReader reads the header, track table and frame index. Two, it reads one frame from any Read and Seek source. Three, AudioDecoder and VideoDecoder take their packets. Four, your code gets one Frame: a planar YUV 4:2:0 picture, 32-bit float samples for each track, the frame’s time, and a flag if its video was damaged.
The path of one frame through the crate’s three layers. Each layer is public, so you can use the reader or either decoder alone.

A Bink 1 file starts with a fixed header, a table of audio tracks and an index with one entry for each frame. Here is the start of one, a small test file from the repository that the tests write bit by bit.

A table titled “The first 64 bytes of a Bink file,” from a 360-byte test file in the repository. Each row shows the stored bytes in hex and what the reader makes of them: the signature BIK with revision letter i, a file size of 352, 1 frame, a largest frame of 300 bytes, an unused field, a width and height of 32 by 32 pixels, a frame rate of 30 over 1, video flags of 0, 1 audio track, a 12-byte track table giving 48,000 Hz stereo DCT audio, a frame index entry placing frame 0 at byte 60 with the keyframe bit set, and the 172-byte size of the first audio packet.
The header, track table and frame index of fuzz/seeds/decoder/with_rich_audio, read the way BinkReader reads them.

Most of the work is in the video packet. Each plane of the picture is cut into 8x8 blocks. The packet first carries nine streams of values, each one Huffman coded, and then every block draws itself from those streams according to its type, and there are ten.

A grid titled “Ten ways to draw an 8x8 block,” with a small sketch for each block type. Skip keeps the block from the previous picture. Scaled decodes one 8x8 block and doubles it to 16x16. Motion copies a block from the previous picture, moved by an x and y offset. Run lays runs of colours along one of 16 scan orders. Residue is a motion copy with small corrections added. Intra turns DCT coefficients into pixels with the inverse DCT. Fill is one colour. Inter is a motion copy with an inverse DCT added on top. Pattern is two colours and a mask of one bit per pixel. Raw is all 64 values as stored.
The ten block types of Bink 1 video, numbered as they are in the stream. The sketches are mine and are not decoder output.

What it decodes

  • Container. Bink 1 files. Bink 2 files and SMUSH-wrapped files are not supported.
  • Video. Bink 1 revisions f, g, h, i and k, with every block type, motion compensation and the alpha plane. The old revision b is not supported.
  • Audio. The DCT variant of Bink audio, mono or stereo. The RDFT variant is not supported.

Only revision i video and stereo 48 kHz audio have been checked against real files. The code for the other revisions is ported as FFmpeg has it, and so far it has only run on the synthetic tests and under the fuzzers.

How I checked it

I checked it against FFmpeg. On a set of 56 revision i movies, every video frame from bink-decoder was equal to FFmpeg’s output byte for byte. Twenty-three of those files had sound, and the largest difference in any sample was 0.000006. The audio isn’t bit for bit because FFmpeg does that transform in single precision and this decoder does it in double.

Those are real movies, so they are not in the repository, and that comparison runs locally instead of in CI. Nothing in the repository comes from a commercial product. The tests that run on every push build their own tiny Bink files a bit at a time.

Terminal output titled “The tests that run on every push,” from the GitHub Actions log on Ubuntu for the command cargo test –release. The 13 unit tests pass. The 13 tests in tests/synthetic.rs follow by name, among them a_damaged_frame_keeps_the_last_picture, reads_a_movie_inside_a_larger_file, every_truncation_fails_cleanly and pseudo_random_mutations_do_not_panic. Twelve pass and one, write_fuzz_seeds, is ignored.
Part of the test output from the GitHub Actions run on the current commit. The same job runs on Windows and macOS.

A movie file can be damaged or hostile, so I also ran the decoder under five fuzzers: one for the whole decoder, one each for the video decoder, the audio decoder and the container reader, and one for the bit-level readers. They found three things. A header that declared a huge frame made the reader allocate that much memory before reading anything. The video decoder accepted a picture size of zero, or one far too large. And a decoder that was reset, or rewound after a damaged first frame, could show pixels left over from before. All three are fixed and each has a test.

After the last fix, each target ran for 30 minutes and then 20 more on the current code without finding anything else. A shorter run happens on every push.

Terminal output titled “Five fuzz targets, 30 seconds each,” from the fuzz job in the same GitHub Actions run. Each of the five targets starts from its seed files and finishes in 31 seconds: decoder after 58,371 runs, video after 57,141, audio after 64,128, reader after 742,470 and bits_and_tree after 334,184.
The short fuzz job that runs on every push, trimmed to the first and last lines for each target.

How fast it is

A bar chart titled “A 30 fps movie allows 33 ms a frame.” Each bar is drawn against the 33 millisecond budget. On a current x86-64 server core an 800x600 frame takes about 0.85 ms on average and 3.4 ms at the slowest, and a 720x480 frame takes about 0.85 ms and 2.5 ms at the slowest. On an AMD A8-6410 laptop from 2014 an 800x600 frame takes about 2.4 ms on average and 8 ms at the slowest.
Decode times from the README: one thread, video only, revision i movies.

A movie at 30 frames a second needs a new frame every 33 milliseconds. On one thread, a low-end laptop from 2014 decodes an 800x600 frame in about 2.4 milliseconds, and the slowest frame took 8. Old menu movies are not a hard workload, but the engine has a game to run in the other 31.

Where to get it

The code is on GitHub at version 0.1.0. There is no tagged release yet and it is not on crates.io, so you add it to a project as a git dependency. It needs Rust 1.75 or later.

The license is GPL 3.0 only. FFmpeg’s decoders are under the LGPL 2.1 or later, and that license lets a derived work be placed under the GPL, which is what this port does.

It is one of two Rust libraries that came out of Battlefront Eternal. The other is rar-reader, which reads RAR archives for the mod manager. Most of the other open source on this site is for phones and radios: Ti.Socketline and Ti.Paysheet for Titanium apps, and a SIM card generator for private LTE and 5G networks.

Do you have files in an old format and software that can no longer open them? That is custom software I take on. Send me an email and tell me what the files are.