Diary Of An x264 Developer

05/10/2008 (10:58 pm)

Introducing Photon

Filed under: codec,ffmpeg,photon ::

A few days ago I was somewhat bored; no particular good ideas for quick x264 improvements, no good games to play, and most of my finals out of the way. So I decided to make my own codec, called Photon.

The basic goal is to make a very fast MPEG-2-like format with better compression and speed than MPEG-2, but without the complexities of things like interlacing and B-frames. My eventual plans for Photon are a bit more fancy than what I have so far, of course; currently the encoder and decoder are intra-only, for example. And honestly, I’ll be shocked if I manage to reach my goals; my main purpose in this is to learn how to write a codec and bitstream format from the ground up and experiment with all sorts of features in the process.

The main philosophy behind Photon is to keep everything as simple as possible; the fewer special cases needed, the better. As such, every macroblock is divided up into 8×8 blocks; 4 for luma and 2 for chroma (the codec uses YV12 colorspace). Every single 8×8 block, luma or chroma, is treated exactly the same with the exact same set of code; the bitstream does not even distinguish them. This makes it possible to have a single loop for decoding all of these blocks. For the transform, the H.264 transform/quant/zigzag process is used.

Preceding the blocks is a 6-bit element, with one bit for each block. For each block, its associated bit in the Coded Block Pattern (CBP) is set to 0 if there’s nothing in the block and set to 1 if there is.

For each block with a
CBP of 1:

1. A delta-quantizer element. Yes, that’s right: each 8×8 block has its own quantizer! This increases the effectiveness of adaptive quantization. The delta-quantizer is done with respect to the same numbered block in the previous macroblock and is coded in unary with one bit for the sign (total bit cost: 1 bit to not code a delta quant, N+1 bits to code a delta quant, where N is the delta).
2. A transform element. Set to 0 if the block uses 4x4dct, set to 1 if the block uses 8x8dct.
3. A 4-bit CBP for the 4 4x4dct blocks, set in the same manner as the macroblock CBP. Omitted in the case of 8x8dct.
4. Residual for the 8×8 block, or for each 4×4 block with a CBP (in raster scan order) as follows:
a. The first coefficient in the block, coded as a signed exponential golomb code.
b. Is this the last coefficient? If so, a 1 is coded and the residual coding ends. Otherwise, a 0 is coded.
c. The run length of 0s until the next coefficient, coded as an unsigned exponential golomb code.
This loop repeats to code the entire block.

The macroblock itself has a 2-to-4-bit header specifying the luma and chroma prediction modes to use, and the frame has a header specifying the frametype and frame quantizer.

… and that’s it. That’s the whole thing, so far. Yes, the entropy coding is absurdly suboptimal and would benefit dramatically from custom variable-length codes instead of universal codes. Yes, I’m not using an ounce of assembly whatsoever and the encoding and decoding are far slower than they should be (decoding is still realtime for most sane resolutions though). But it works.

For those who want to see my hackneyed, half-copy-pasted-from-x264′s-common-library code, the diff so far is here.

4 Responses to “Introducing Photon”

  1. imcold Says:

    I wonder if you measured how much the dc/h/v spatial prediction helps? I think I will try it as well, but I handle the mb pixels differently in my codec – I load them from frame to separate memory, instead of just adjusting the fenc pointers, so it will be a little bit more complicated… (I thought it’s faster this way, but never actually benchmarked it). My codec is quite simple too. Intra coding is even simpler than mpeg-1, I don’t use CBP (I tried, but as it was stored directly instead of some vlc, it wasted bits on low quants); quant is fixed and passed in frame header, no intra pred; quantization and even (intra) matrix like mpeg-1. With entropy coding I chose almost the same way as you have: exp-golomb for coefs, but rice k2 for zero runs. P-frames – are you going to use some partitioning? I’m thinking about 4mv as mpeg-4 has, but I didn’t code it yet… I store the mvd (top/left/mixed prediction) as exp-golomb codes, too. With inter coding, things really start to be interesting :) Good luck!

  2. Dark Shikari Says:

    I didn’t measure how much prediction helps; DC likely helps a whole lot; adding H and V improved compression by about 3%.

    IMO there’s no reason to use MPEG-1 style quant/DCT; H.264 is superior in every way, both in terms of speed and in terms of simply making sense.

  3. imcold Says:

    I agree, and in addition its quantization is very coarse, too. But the code it’s quite straightforward and I haven’t really had a look at h.264 quantization process, yet. I know that it avoids division, which is the main quant speed killer in my case. I don’t know if decoding would benefit too, the dequant speed is already very fast. Well, that’s more things for me to test!

  4. FLag Says:

    Hello,

    Sorry for this off topic comment, I have seen on the nVidia forums that you were trying to use the CUDA framework to speed up x264 encoding.
    I have done some stuff on CUDA when it was in beta and I still follow developments in the framework, I may help you on that aspect ?

Leave a Reply