Chroma encoding optimizations
This post revolves around this diff.
The chroma encoding process is an often-neglected one; we focus all too much on the complexities of luma encoding while forgetting the chroma encoding process, which is exactly the same regardless of block type. The 8×8 residual blocks for the two chroma planes (from motion compensation or intra prediction) are DCT’d, quantized, and encoded. However, there are some aspects to chroma encoding that make it subtly different:
1. Like i16x16 blocks, chroma encoding is separated into the DC coefficients of each 4×4 DCT block and the AC coefficients. DC coefficients are “flat” frequencies, that is, a number representing a uniform change over the whole block. AC coefficients are non-flat, and represent a spatial frequency. In most block types, DC coefficients are not treated differently from AC; in chroma and i16x16, they are. In chroma in particular, the 4 DC coefficients (for each of the 4 DCT blocks in a color plane) form a 2×2 array, to which a Hadamard transform is applied, and then the result is quantized. This allows the overall “flat” change in color to be coded separately from any more complex change in color.
2. Chroma blocks are generally very simple and have nearly no residual data. This is part of the reason that 1) is how the H.264 standard implements chroma coding; nearly all the data is probably going to be very simple if it is there at all.
Now, this changes things slightly. First of all, it changes how DCT decimation works; normally, decimate works by picking a DCT block and setting its contents to zero if its contents are sufficiently simple that it would save a lot of bits just to not code it at all (and have a relatively small quality loss relative to the bit savings). For chroma in particular, decimate happens a *lot*, because usually there are nearly no AC coefficients.
But what if, like we often do, we decimate the AC coefficients… but there’s DC coefficients left over? We don’t decimate these; the visual effect could potentially be large to do so, and the bit savings is small. But we still have to decode the DC-only chroma residual data, that is, perform an inverse DCT, and store the resulting data to represent the decoded frame.
But if we know that there’s only a DC coefficient and no AC coefficients, why do an entire iDCT? That would be a waste of time, since having only a DC coefficient literally just means we’re adding the same value to every pixel in the block. So I ported the DC-only chroma iDCT from ffh264 and rewrote it to do all 4 4×4 blocks at once, so it can handle an entire 8×8 chroma block in one call. Now, whenever we have a DC-only chroma block, we can skip most of the iDCT process, saving a bit of time. I also cleaned up a lot of the decimate code in the function, speeding things up further. Thanks a lot to Loren for help with the assembly on this one.
May 9th, 2008 at 5:08 pm
Good to see you’re giving some optimization attention to the often overlooked chroma…
I have one question. I see a lot of diffs referenced in your “diary” (well-named, I must say), and I was wondering as to the commit frequency of these patches, and their general frequency of integration into GIT. It is sometimes confusing what actually does make it to GIT without browsing the code each time.
Has this one made it (yet)?
May 9th, 2008 at 5:14 pm
This one isn’t in yet, but generally most useful diffs end up in git at some point or another.
The one major diff that has been “indefinitely postponed” is frame_init_lowres asm, because pengvado insists that I try a horizontal loop instead of a vertical one for cacheline purposes to see if its any faster (and won’t commit it until I try that). I’ll probably do that sometime this summer.