Contents
Kiln: an H.264 encoder in C#, and what the spec didn't tell me
Writing a video encoder in managed code turned out to be possible. Noticing when it was wrong was the hard part.
26 Aug 2026 · Updated 2 Oct 2026 · 4 min · Ernest
Media & codecs, CorrectnessContents
Proxeno, my game-streaming server, needed to encode H.264 inside a .NET process. Every option I found was either a native codec to cross-compile, ship and keep patched on every platform, or a GPL or LGPL one I didn't want to link. So I wrote one. Kiln is plain C#, written from the ITU-T spec, and it does baseline profile only: no B-frames, no CABAC, just the subset a low-latency stream uses.
Most of it was written in a few days in late August. Speed turned out not to be the hard part.
Wrong slowly
Deblocking is the filter that smooths block edges after reconstruction. Encoder and decoder have to apply it identically, because the encoder predicts the next frame from the picture it believes the decoder has.
I had two bugs in it. The luma filter ignored the spec's rule for averaging QP across a macroblock edge, and my boundary-strength rule had its precedence backwards: coded coefficients are supposed to beat motion-vector differences, and mine let the motion vectors win.
Neither bug produced an invalid stream. Every decoder played it. My reconstruction just drifted a little further from every real decoder's with each frame, because the encoder was predicting from a picture nobody else had.
I found them because every stream Kiln produces is decoded by ffmpeg and by Apple's VideoToolbox, two decoders I didn't write, and compared against what they reconstruct. A test I'd written to match my own reading of the spec would have passed, because my reading was the bug.
After the fix, reconstruction is byte-exact against both. The bug only ever touched streams with per-macroblock QP, or constant QPs like 31 and 32 where the spec distinguishes two boundary strengths, so the defaults were never affected. That's a smaller blast radius than "the deblocking filter was wrong" sounds like.
Kiln is on NuGet as Proxeno.Kiln, pre-release, and the APIs will change.