r/ffmpeg • • 14d ago

GAIL/libgail: a GPU JPEG 2000 encoder that makes DCI-compliant DCP picture in FFmpeg

Hi I'm Joseph,

TL;DR: I made libgail / GAIL (MIT, v1.0.1), a GPU DCI JPEG 2000 encoder. Today you can use it in FFmpeg as `-c:v libgail` to go from a master file to DCI-compliant `.j2c` frames and then a SMPTE DCP. A native DCP-o-matic integration is in progress and we expect it to land there soon as a much faster encode option. Feedback/testing from people who actually make/play DCPs would be hugely welcome.

Disclosure: I'm the author.

Repo: https://github.com/Josephur/libgail

What it is:

GAIL is a JPEG 2000 encoder built specifically for cinema: ISO/IEC 15444-1 with the Amd.1 cinema profiles, SMPTE ST 428-1 / ST 429-4, 12-bit X'Y'Z', 2K (1998x1080 / 2048x858 / 2048x1080) and 4K (3996x2160 / 4096x1716 / 4096x2160), 24/25/30/48+ fps, capped under the 250 Mbit/s DCI limit (230 Mbit/s target by default).

To my knowledge there is no other open source and free to use GPU accelerated DCI JPEG 2000 encoder in existence. There are a few commercial ones however. This project was born out out of my desire to help the community have free and open software, and named after my lovely girlfriend Gail (who happens to work in Cinema).

Everything compute-heavy runs on the GPU, including EBCOT Tier-1 entropy coding — that's the part that normally makes JPEG 2000 encoding so slow. Backends are CUDA (fastest on NVIDIA, Turing/GTX 16+), Vulkan compute (NVIDIA/AMD/Intel), and a CPU reference backend that is bit-identical to both GPU backends. It also does multi-GPU: frames are handed to whichever card is free. Color handling (BT.709/BT.2020/P3, SDR and HDR PQ/HLG) matches DCP-o-matic's rendering by default.

We need more Intel, AMD and Mac testers!

Why bother, when FFmpeg already has OpenJPEG?

Speed, and compliance without babysitting settings. On an RTX 3090 (Ryzen 5900X test rig):

* 4K CUDA: ~171 fps single GPU, ~247 fps with a second card added... end-to-end from an H.264 file with decode included is still ~127 fps at 4K

* 2K CUDA: ~329 fps single GPU

* CPU reference for comparison: ~8.6 fps at 4K; OpenJPEG in DCI mode: ~1.5 fps at 4K on all 24 threads, and in testing it could overshoot the 250 Mbit/s cap by a few bytes on some frames. GAIL stays under it and measured slightly higher PSNR (+0.03–0.05 dB) on the same frames.

So roughly up to 17x faster than FFmpeg's OpenJPEG encoder at 4K in practice, and every frame passes a built-in DCI checker as it's encoded.

How you use it with FFmpeg today

It's not in stock FFmpeg downloads yet — you build libgail, patch it into your FFmpeg tree once, and build FFmpeg:

# 1. Build/install libgail (CMake >=3.20, Vulkan SDK; CUDA auto-detected)

cmake -S . -B build -G Ninja && cmake --build build && cmake --install build

# 2. Add the encoder to FFmpeg and build (tested with FFmpeg 9.0.2)

integrations/ffmpeg/add-to-ffmpeg.sh /path/to/ffmpeg

cd /path/to/ffmpeg && ./configure --enable-libgail [your usual options] && make -j

# 3. Encode to DCI JPEG 2000 frames — always set a DCI frame rate

ffmpeg -i film.mkv -r 24 -c:v libgail -f image2 j2c/%06d.j2c

# Optional scale example (centred in the 2K Flat container):

ffmpeg -i film.mkv -r 25 -vf scale=1920:1080 -c:v libgail -f image2 j2c/%06d.j2c

FFmpeg just decodes — Y'CbCr (4:2:0/4:2:2/4:4:4, 8/10/12-bit), planar R'G'B', rgb48le and xyz12le pass through and are converted on the GPU, so e.g. ProRes 422 stays 4:2:2. Useful options: -dci_profile 2k|4k, -container flat|scope|full, -mbps (<=250), -backend cuda|vulkan|cpu, -gpus list to see devices.

There's also a standalone tool if you don't want to rebuild FFmpeg:

./build/gail-enc -i input.mkv -o frames/ --profile 4k --fps 24

Getting to an actual DCP

The .j2c frames still need wrapping/packaging (don't use FFmpeg's `-f mxf` for this — that's OP1a, not a DCP track file). The repo ships a script that wraps with asdcplib and writes a minimal SMPTE DCP you can verify and play:

python3 scripts/make_dcp.py frames/ dcp/ --fps 24 --title MY_FILM_FTR-1_F_51_4K_20261029_SMPTE_OV

dcpverify dcp/

Honest caveats: that script makes a picture-only test DCP, and GAIL itself is picture-only — audio is out of scope. For a finished feature DCP with sound/subtitles today, prepare the audio with FFmpeg as below, then take the picture frames/MXF and the audio into a DCP packaging tool (e.g. DCP-o-matic, which wraps the audio MXF and authors the CPL/PKL), or wait for the DCP-o-matic integration below. FFmpeg on its own cannot author a complete SMPTE DCP — its `-f mxf` output is OP1a, not a DCP track file, and a DCP is a package (picture MXF + audio MXF + CPL/PKL/ASSETMAP), not a single video file.

Audio with FFmpeg (DCI wants uncompressed PCM, 24-bit, 48 kHz — check your channel layout/order first, and don't blindly upmix/downmix):

# Inspect what you have

ffprobe -hide_banner -i film.mkv

# Stereo or multichannel master -> one 24-bit/48 kHz PCM WAV

ffmpeg -i film.mkv -map 0:a:0 -vn -c:a pcm_s24le -ar 48000 audio.wav

# Optional, for tools that want mono WAVs — split a 5.1(side) track

# (adjust the layout/labels if your source is 5.1 back: BL/BR instead of SL/SR)

ffmpeg -i film.mkv -filter_complex "[0:a:0]aresample=48000,channelsplit=channel_layout=5.1[FL][FR][FC][LFE][SL][SR]" -map "[FL]" -c:a pcm_s24le L.wav -map "[FR]" -c:a pcm_s24le R.wav -map "[FC]" -c:a pcm_s24le C.wav -map "[LFE]" -c:a pcm_s24le LFE.wav -map "[SL]" -c:a pcm_s24le Ls.wav -map "[SR]" -c:a pcm_s24le Rs.wav

Then bring j2c/ (or the picture MXF) plus audio.wav / the mono WAVs into DCP-o-matic (or wrap the WAV as a SMPTE audio MXF with asdcplib and author the CPL/PKL with a libdcp tool) to get the finished DCP with sound, and verify it as usual.

Is it actually cinema-compatible, or just "JPEG 2000"?

Validated beyond "it decodes": every frame checked by GAIL's own DCI checker and jpylyzer, cross-decoded with OpenJPEG/FFmpeg, wrapped with asdcp-wrap, and the resulting DCP passes libdcp dcpverify and DCP-o-matic's verifier with 0 errors/warnings. Encoded 4K DCPs have played back on Christie Solaria One / CP2210 / CP2220, NEC NC900C-A, and a GDC SR-1000. Full write-up is in the repo's VALIDATION doc.

DCP-o-matic:

The goal is for you not to need any of the manual steps above: we're working on GAIL as a JPEG 2000 encoder option inside DCP-o-matic and expect it to be added soon. In the meantime the FFmpeg route uses the same color defaults, so material should match what you'd get from DCP-o-matic today.

Links / trying it

Repo, docs, and prebuilt releases (Linux/Windows/macOS, library + gail-enc/gail-check/gail-bench tools) are on GitHub: https://github.com/Josephur/libgail and the FFmpeg build steps are in integrations/ffmpeg/README.md.

If you make DCPs for festivals/indie screenings and give it a try, I'd love to hear: what GPU/OS, what source, whether the DCP verified and played, and where it fell over. Bug reports with a gail-check result on a failing frame are golden contributions, just open an issue on the repo as needed.

P.S. If you are currently using one of the commercial products to do your encoding and find libgail saves you some dough, github sponsorship is an awesome way to show your appreciation.

Thanks all!

39 Upvotes

18 comments sorted by

6

u/_Gyan 14d ago

Are you planning to open a PR at ffmpeg for this?

2

u/Josephur 14d ago

I emailed the mailing list haven't heard anything yet

1

u/max_lapshin 14d ago

Did you do it for the sports realtime streaming?

1

u/Josephur 14d ago

eh? No?

1

u/nmkd 13d ago

Really interesting.

Claude really changes things

5

u/Josephur 13d ago

Absolutely, I actually started this over 2 years ago, and never thought I would ever get it to a finished state. Was trying to improve dcp-o-matic's speed LOL

1

u/jtkthx 13d ago

Thank you for this. I’ve been using and supporting DCP-O-Matic since it was called DVD-O-Matic. I use it all the time on my side of the industry, as do many many others. It’s hard to overstate how long-awaited GPU encoding has been, so it’s a big deal to see it finally arriving. Way to go.

1

u/Josephur 10d ago

You are welcome! If you wish to support the project the best way is through Github stars, or Github sponsorship! Currently I'm looking to acquire a 16GB / 256GB M-series Apple device so I can offer better support on Metal through MoltenVK. Problem is, I'm not a Mac user at all, so even to drop $400 on such a device is not high priority for myself, but if I had one I would at least make sure the thing compiles and runs as good as it can on that hardware.

1

u/airfyer_avif 12d ago

Why should we use this over https://docs.nvidia.com/cuda/nvjpeg2000/index.html ?

Habe you tried to compare with them, will ne useful.

Secondly; ffmpeg typically never accepts wrappers; so you will need this as codec inside ffmpeg. Create a PR and also do iteration of review with review bot in code.ffmpeg.

Having comparison and rationale over nvjpeg2000 will be good

2

u/Josephur 10d ago edited 10d ago

Why not nvJPEG2000:

- It's NVIDIA-only and closed source, I wanted open source and something the community could build upon. libgail runs on NVIDIA through CUDA, on NVIDIA, AMD and Intel through Vulkan, and on a bit-exact CPU fallback, all under MIT. A proprietary library can't go into a normal FFmpeg build, and it would lock DCP mastering to one vendor.

- It doesn't target DCI output. For example: no cinema-profile output, no per-frame rate control under the DCI byte cap, no X'Y'Z' color pipeline.

- It doesn't handle color. libgail takes BT.709 / BT.2020 / P3, SDR and HDR (PQ, HLG) and Y'CbCr input, and converts to X'Y'Z' on the GPU. nvJPEG2000 only encodes the pixels it's given.

Then if you look at OpenJPEG:

Against OpenJPEG (its DCI cinema mode, same X'Y'Z' frames, same 250 Mbit/s cap):

- Quality: libgail is +0.05 dB mean PSNR at 4K and +0.03 dB at 2K.

- Byte limit: libgail stays under it on every frame. OpenJPEG 2.5.4 goes over by up to 16 bytes on some frames.

- Speed: 183 fps at 4K for libgail on two GPUs, against 1.5 fps for OpenJPEG on 24 CPU threads.

1

u/chrish162 11d ago

Very interested in this project. Have you talked to Carl from DoM? Or are you forking the project to integrate this encoding option?

Either way, I'm happy to assist with testing in any way I can... I'm a cinema integrator and have access to the following equipment:

Barco: ICMP-X

Dolby (Doremi): IMS3000 IMS2000 ShowVault/IMB DCP2000

Dolby: DSS200/CAT862 DSS220/CAT745

GDC: SR-1000

1

u/Josephur 10d ago

Carl is going to implement it himself, you can follow that here: https://dcpomatic.com/bugs/view.php?id=3208

And I'm working on adding a decoder, I've never seen a DCP encoded video play back in VLC player smoothly.... until now! It's coming along nicely. If you sign up for an account on his MantisBT system there you could leave a comment that you are willing to help in testing, I'm sure he appreciates all the testing he can get!

1

u/chrish162 10d ago

Interesting -- VLC can open j2c .mxf files currently, I assume using the software jpeg2000 decoder in ffmpeg, and it works OKAY if your machine is beefy enough. But of course it's not for DCP, and it has no awareness of things like reels. Is it technically feasible for a decoder to handle all of the extra context required for DCP playback?

1

u/Josephur 10d ago

Yeah it works horribly if you ask me. And its compounded if you aren't using a refresh rate that is a division of the cinema FPS. The goal would be to get it so that it works smoothly on playback on even a low end system with say an RTX 1050, or even a Ryzen APU via Vulkan. Remember libgail is just a encoder/decoder so anything past that for extra feature/content/playlists a DCP might provide would have to be implemented by VLC itself. I just encode/decode video streams, that is it. But now that they can be decoded fast enough that it makes sense you may want to play them in VLC.

2

u/IGNblackShadow 14d ago

Does it support android

8

u/spryfigure 14d ago

Do you want to do encoding for professional cinema projectors on your smartphone? Ambitious.