A summary of recent revisions
I haven’t written a post here for quite some time, so I figured I’d get back into the swing of things with an update on some recent revisions. Further information can be found on the official git repository page.
r891: Instead of checking for the end of the bitstream on every single write, check before encoding each macroblock–and instead of silently truncating the frame upon reaching the end of the malloced memory, reallocate with more room. (patch by me)
r892: A patch by Gabriel for compilation on MSVC and other crappy compiles that don’t maintain mod16 stack alignment–disable all functions that require these on such platforms. This results in a visible CPU flag (“slow_mod4_stack”) to warn the user that their build is crippled.
r893: Various micro-optimizations by me, along with a very cool little idea that saves quite a bit of time in probe_skip. The idea here is that the probe_skip function *almost never* terminates during the chroma check, but you can’t skip it completely, since if you choose to skip a block before looking at the chroma, and it turned out there was significant detail there that you ignored, there might be noticeable artifacting as a result. The solution was to do a quick Sum of Squared Differences (SSD) check before doing the actual probe_skip on each chroma plane; if low enough, skip the check. Even an incredibly low threshold was sufficient for skipping the check nearly half the time yet never changing the results.
r894: Assembly for the lowres interpolation filter used for frametype decision–drastically increases its speed. The C was modified in order to match the assembly version, which had slightly different rounding. I originally wrote an MMX, SSE2, SSE3, and SSSE3 version of this patch, but Loren found an even faster method of doing it and rewrote most of it from scratch before committing it.
r895: Fix a bug in adaptive quantization stemming from the original implementation. One subtle issue with this patch is that on win32, GCC incorrectly puts arrays of all zeroes into .bss even when told to align them–despite the fact that .bss is only labeled as 4-byte aligned. This causes a crash, of course. We resolved this with an ugly hack–tack a “1″ onto the end of the array to stop GCC from putting it in .bss.
r896: Improve the permutation macros for easier use throughout x264′s assembly. (patch by Loren)
r897: Port the noise reduction assembly from libavcodec and improve it. Speed up the C version too. The SSSE3 assembly is roughly 25 times faster than the original C code. (patch by me with help from Loren and Holger)
r898-r899: Update the copyright headers across x264. (patch by me)
r900: Fix a subtle bug in Loren’s frame_init_lowres patch; in some cases the FPU might not be re-initialized with “emms” after running frame_init_lowres, so an emms was added just in case.
r901: Lots of mini-optimizations, one by holger, the rest by me.
r902: Considerable cleanup of the SSD assembly functions to simplify the code. (patch by Loren)
r903: A complete rewrite of the bitstream functions from the ground up. This vastly faster bitstream writer uses 32-bit and 64-bit instead of 8-bit chunks. This eliminates the need for any loop in bitstream writing. The 32-bit writer is quite similar to ffmpeg’s 32-bit writer. The 64-bit writer uses a particularly ingenious system whereby the writer only writes 32 bits at a time, but keeps up to 64 bits in a storage chunk–this means that any variable-length code can be written unconditionally to the chunk, since there’s always at least 32 bits of space left: the write to the bitstream is done afterwards. Because of this, the 64-bit bitstream writer only needs a single if statement in its write function; not even an else. (patch by me with help from Loren)
r904: The golomb functions were far more complicated than necessary–with the new bitstream writer changes many of the special cases were no longer useful speed-wise. Additionally, many of the branches were not necessary in most calls to those functions. (patch by Loren with some changes by me)
r905: When a large static table is stuck in a .h file, its duplicated in every single C file that includes and uses it. This is a waste of memory–its better to declare it once in a C file and use “extern” to address it from other locations. This diff de-duplicates VLC tables and additionally reduces them from using 16-bit to using 8-bit codes, since none of the VLC codes have more than 8 significant bits. (patch by Loren, change to 8-bit by me)
r906: Add support for PCM macroblocks. This type of macroblock is *completely* uncompressed and bypasses the CABAC entropy coder. As such, each PCM block is always 384 bytes. The advantage of this is that sometimes, given the current state of the CABAC encoder, at very low QPs (0-5) and in lossless mode, its possible that a block will exceed 384 bytes in size–so PCM would have been a better option. When rate-distortion optimization is enabled, PCM is considered as a possible option by x264. (patch by me)
July 10th, 2008 at 4:07 pm
Nice! Even if i sometimes don’t have a single clue what the fuck is going on there I’m always enjoying reading your Blog.
Well, if i ever find time to train my coding skills i hope i can maybe help at x264 development as well.
July 10th, 2008 at 6:47 pm
Now that you mentioned lossless mode, is there a difference between QP 0 and lossless? I think there is; however you can’t specify QP 0 via the interface because that gets interpreted as lossless.
It caught my eye when I saw that using very low QPs and depending on the IP-ratio I-frames would end up with QP 0.
Another question, just out of curiosity, about non-PCM lossless mode. I understand it skips the spatial transform and the quantization, but it can still use standard temporal prediction (i.e. P- and B-type MBs). Does it still have spatial prediction from neighborhood MBs? Does it have some kind of “local”/internal predictor, like PNG? (I’m thinking the usual x=b-a+c)
July 10th, 2008 at 6:54 pm
Correct–QP0 is not necessarily lossless (its only lossless if the header bit that states “qp0 is interpreted as lossless” is set).
Non-PCM lossless mode has *no pixel prediction*, only spatial prediction and temporal prediction; this is a serious problem and sacrifices a lot of compression. Adding paeth prediction would improve compression by 15% or more, I believe.
July 10th, 2008 at 7:14 pm
Is there any evidence that Paeth is better than an ideal predictor? Paeth is “do the ideal prediction, then choose the closest matching one”. That last step sounds very nonperformant, as it involves 3 comparisons and a 3-way conditional choice. (I don’t know if there’s some implementation trick that can be used to speed it up)
The theory with Paeth was that for reduced color images it’d pick “likely” colors instead of generating “new” ones, reducing the entropy load. In practice, I’m willing to bet an ideal predictor would achieve better results on photorealistic images, whereas on synthetic images no filtering at all performs best (at least when backed with the Deflate algorithm that PNG uses.)
It’d be really easy to hack up a patch for libpng that skips the Paeth choice part of the filter to check it out. It wouldn’t be the first compression-related suboptimal choice in PNG (the lack of inter-channel color prediction comes to mind: MNG added a (G-R)(G)(G-B) mode that compressed a 10% better, even when the data was feed interlaced to the Deflate backend.)
July 11th, 2008 at 7:49 am
You’re absolutely right, and for that reason most lossless video codecs (huffyuv, ffv1, snow, lagarith, jpeg-ls, …) use median(l,t,l+t-tl). But afaik that form has no official name, so I was just being lazy when I called it “paeth”.
Actually, when I measured the 15% (or however much it was) improvement in h264 lossless, that wasn’t median, it was just l+t-tl. Furthermore I restricted it to within each 4×4 block to make it a drop-in replacement for DCT. If you fix those, even more improvement is possible.