Diary Of An x264 Developer

09/02/2009 (10:58 pm)

The hidden cycle-eating demon: L1 cache misses

Filed under: Intel,speed,x264 ::

Most of the resources out there about optimizing cache access talk about L2 cache misses.  Which is sensible–the cost of an L2 miss is extraordinary, taking hundreds of cycles for an access to main memory.  By comparison, an L1 miss, costing just a dozen cycles, is nothing.  This is true on-chip as well; the memory->L2 prefetcher in modern processors is extremely sophisticated and is very good at avoiding cache misses.  It is also very efficient, making reasonably good use of the limited memory bandwidth available.  There are also dedicated prefetch instructions to hint the prefetcher to avoid future L2 misses.

But what about L1 misses?  There’s vastly less literature on them and the L2->L1 prefetchers are often barely documented or not even mentioned in official processor literature.  Explicit prefetch instructions are vastly less useful because the cost of the misses is low enough that the extra overhead of sending off a set of prefetches is often not worth it.  And yet in many cases–such as in x264–much more time is wasted on L1 misses than L2 misses.

Read More…

08/24/2009 (7:54 pm)

Announcing ARM support

Filed under: ARM,assembly,GSOC,speed,x264 ::

Thanks to our Google Summer of Code student David Conrad (aka Yuvi), we now have ARM support in x264, along with a significant amount of SIMD acceleration via NEON, available on the Cortex A8 and A9 chips.  Yes, that’s right, x264 can now run on an iPhone.  Total performance increase from the NEON optimizations (so far) is about 280% on default settings.

With low power becoming more important and ARM chips increasing in speed dramatically (multi-core chips are already hitting silicon), being able to do high quality, high-speed realtime video encoding on ARM chips will become more and more important.  Staying ahead of the game as always, x264 will be the premiere encoder on ARM as well.

One situation showing the usefulness of low-power encoding was brought up a month or two ago: a remote-control airplane enthusiast wanted to make his airplane broadcast camera footage over the cell network so that he can remote control it many miles away from his current location.  The cell network is generally low bandwidth, so he needs a high-efficiency video encoder.  But he can’t afford a powerful system; his airplane is already extremely low power and he needs an encoder that is both low-power and low-weight.  The ARM chip is perfect: it uses a fraction of a watt, almost no space, and now, he can run x264 on it.

Special thanks to Mans Rullgard for helping with lots of assembly questions and contributing the NEON deblocking code, originally used in the ffmpeg H.264 decoder.

Want to play with x264 on an ARM?  Get a Beagleboard.

Commits: 1 2 3 4 5 6 7 8 9 10

08/09/2009 (10:41 pm)

Encoding animation

Filed under: benchmark,H.264,ratecontrol,SSIM,x264 ::

Note: I originally posted this a day earlier, but quickly retracted it when it was pointed out that I made a rather egregious error in the ffmpeg tests’ settings, so I re-did those and added a few more codecs.

Encoder comparisons are a dime a dozen these days, but there’s one thing I’ve almost never seen tested: cartoon sources.  Animated material has totally different characteristics from film and presents a whole separate set of encoding challenges.

First, we’ll start with what makes such video easy to compress.  Animation is mostly static; backgrounds are completely static with characters placed in front of them.  The characters themselves are mostly static as well; modern animation is usually at a significantly lower framerate than the actual video.  Furthermore, characters may stand still with only their mouths moving, dramatically reducing complexity.  Finally, animation is usually very clean, without any film grain.  All of this combines to seemingly make animation compression a very simple task.

Read More…

08/06/2009 (11:36 pm)

A tree of thought

Filed under: development,GSOC,ratecontrol,x264 ::

“There is nothing like looking, if you want to find something… You certainly usually find something, if you look, but it is not always quite the something you were after.”

– J.R.R Tolkien

About a year and a half ago, I had an idea: what if we made a graph of how each block of the video referenced other blocks temporally and used this graph to increase quality on blocks which are referenced a lot and lower it on those which are referenced less?  Clearly this would greatly improve average quality… but when I thought through it, the problem became messier and messier.  I decided to put it off to later. I ended up making it a Google Summer of Code project for 2008, but that student disappeared after a few weeks of relative non-work and a hardly-working initial patch.  I mostly forgot about it; it was in the same category as explicit weighted prediction and MBAFF: messy things that might help, but I didn’t want to do.  This idea in particular got filed away under the name “MB-tree.”

Read More…

07/16/2009 (8:47 pm)

Cacheline splits, take two

It has been well over a year since the original cacheline-split patch and my subsequent cacheline-split patch for qpel interpolation.  I never implemented it for chroma, despite the potential benefit, because it required four extra registers, something that chroma MC was in serious short supply of.  Furthermore, chroma was only width-8 and width-4, and the lower the width, the lower the percentage of loads which crossed cachelines, so the less the overall possible benefit relative to the overhead of cacheline-split detection.

The cacheline split implementations, as can be seen in the original post, vary greatly, but they all have one thing in common: they perform two aligned loads, one on either side of the split, and then use shifts (or palignr) to merge the data together accordingly.  However, there is another possible trick that can be used here.

Read More…

06/30/2009 (3:28 pm)

Chroma encoding revisited

Chroma has always been a ripe target for optimization. We have to perform transform+quantization on every block, but the vast majority of blocks end up having not a single nonzero coefficient to code, so it seems as if we wasted our time doing all that arithmetic only to find out that there was no information there anyways. But we can’t just skip it, because the few times that there are coefficients, they are very important. Part of this problem is unique to H.264, which has a quite curious method of encoding its chroma, which I will describe here for those not familiar with it.

For each chroma channel in the current macroblock, 4 4×4 transforms are performed on the residual, making up an 8×8 block. Then, the DC coefficients of each transform are collected and put into a separate 2×2 block, which is transformed again with a Hadamard transform. In the bitstream, the encoder can signal three modes, which apply to both chroma channels. The first mode, 0, simply says there is no chroma data. The second mode, 1, says there is DC data, but not AC data (the rest of the coefficients that weren’t put into that special 2×2 block). The third mode, 2, says that there is both DC and AC data. Since having AC but not DC data is extremely rare, there is no special mode for this.

Read More…

05/26/2009 (5:21 pm)

The art of commit messages

Filed under: assembly,development,speed,x264 ::

The commit message is one of the most important tools a developer has: in just a few lines, he can communicate a great deal of information to a great variety of people.  This group includes a vast swath of eager but relatively nontechnical users who merely want to know what was improved in the most recent update.  Additionally, this group includes a number of technical users who may look at the code from time to time and perhaps submit patches intermittently.  This group includes the other developers, who on a larger project may not be entirely aware of everything being worked on.  This group even includes the developer himself, as he will probably not remember today’s change in detail a year from now.

So what can a developer do to make a commit message relatively succint but still satisfy the needs of all of these people?  Let’s take the commit message I wrote for this year’s most significant patch so far, Holger’s overhaul of a large part of x264′s most important assembly code.

Read More…

03/25/2009 (9:01 pm)

An excerpt from objdump

Filed under: assembly,fail,ffmpeg,gcc ::

mov eax,esi
shr r10d,2
movsxd rax,[rax*4+0]

The tags should make this post self-explanatory.

(Yes, I’ll post something else soon.  Bug me about it on IRC or something.)

Update: the braindeadness continues.

cmp    edx,esi
cmovge edi,[esp+0xac]
cmp    edx,esi

11/29/2008 (2:32 pm)

Nehalem optimizations: the powerful new Core i7

Filed under: assembly,avail,benchmark,Intel,x264 ::

Here’s a piece I wrote for Avail Media to explain some of the Nehalem optimizations I made in the past month or two.

Pretty graph

Note: “X/Y” instruction timing means a latency of X clocks (after doing that instruction, one has to wait X clocks to get the results), and an inverse throughput of Y clocks (if one runs a ton of that instruction one after another, one can execute that instruction every Y clocks).

The Nehalem CPU has a number of benefits over the previous Intel generation, the Penryn processor.

First of all, the Nehalem has a much faster SSE unit than the Penryn. A huge number of SSE operations have had their throughput doubled:

Read More…

11/14/2008 (2:50 am)

A simple optimization

I’ll be posting about the Nehalem optimizations soon, but in the meantime, a short and simple post.

You want to find the last nonzero value in an array of 16 16-bit values (DCT coefficients, in this case). How do you do this really quickly, especially in the case that most of the array is expected to be zero? Well here’s my way (l[16] is the array):

i_last = 0;
if( *(uint64_t*)(l+i_last+8)|*(uint64_t*)(l+i_last+12) ) i_last += 8;
if( *(uint64_t*)(l+i_last+4) ) i_last += 4;
if( *(uint32_t*)(l+i_last+2) ) i_last += 2;
if( l[i_last+1] ) i_last++;

This assumes the array isn’t all zero, but where I used this code, we already knew that it wasn’t, so that wasn’t an issue.

Only 4 conditionals (cmov and setne in the compiled asm) to find the exact index of the coefficient. Over twice as fast as the previous code:

for( j = i_count - 4; j >= 4; j -= 4 ) if( *(uint64_t*)(l+j) ) break;
for( i = j; i < j+4; i++ ) if( l[i] ) i_last = i;

And I would guess yet another factor of 2 or similar faster than the naive implementation:

for( i = 0; i < 16; i++ ) if( l[i] ) i_last = i;

« Previous Page — Next Page »