Patches attached. I also tried to sort equal length codes in such a way that codes that start and end with 1 bits are paired with values that occur less frequently as suggested by Michael in https://ffmpeg.org/pipermail/ffmpeg-devel/2025-April/341917.html. Yet strangely this worsened compression (for the majority of the fate tests affected by it). The patch can be found here: https://github.com/mkver/FFmpeg/commits/mjpegenc_huffman/ - Andreas