[x264-devel] Restricting the number of bits in a macroblock

Jason Garrett-Glaser jason at x264.com
Fri Jan 14 19:07:57 CET 2011


On Fri, Jan 14, 2011 at 12:59 AM, Andrew Jackson
<alj at twintechnologies.co.uk> wrote:
> Jason
>
> Thank you for your prompt reply.
>
>>> I wrote in September asking about whether or not x264 restricts the
>>> number
>>> of bits in a macro block.  Loren (Merritt) was kind enough to reply.
>>>
>>> After further digging and debugging I find that the decoder which I'm
>>> using
>>> throws out frames (until the next key frame) when it sees a macroblock
>>> with
>>> too many bits in it.  I don't want to use non-psy RDO so please could
>>> someone point me at the right area of x264 code that I should look at in
>>> order to enforce this restriction?
>>
>> Enforcing this restriction is not too difficult.  You must simply save
>> the CABAC/CAVLC state before encoding an MB, and if the number of bits
>> is exceeded, re-encode as PCM (in the encode loop in encoder.c).  This
>> is already done for slice-max-size, but it doesn't copy the CABAC
>> state because you don't need to keep it when you're ending the slice.
>>
>> This limiting you mention isn't done for CABAC because this involves
>> copying the entire CABAC state for every single macroblock to be
>> encoded, so it's wasteful.  It could be done nearly for free in CAVLC,
>> but it isn't done there either for consistency.
>>
>> You could also just use a min QP and hope you can get away with it
>> (which you probably can).
>
> I'll experiment with these and let you know: if I come up with any code I'll
> send it in for consideration as a patch.

Feel free of course to drop by #x264dev IRC if you need any help, as I
wrote all the code related to this.

Jason


More information about the x264-devel mailing list