View Full Version : How-To: Determine the maximum number of Reference Frames


showergel
1st November 2009, 15:40
This question has been bugging me for years and I've never gotten a definitive answer from anyone. There was no website or easily identifiable way to figure this out. So, I found a copy of the h.264 spec and did some light reading and updated the h.264 article on Wikipedia (http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC#Reference_Frames).

In short, to determine the maximum number of reference frames, you choose which level you're encoding at, refer to the chart, and specify the target x & y resolution of the encoded video, then do the math. And always round down to the nearest whole number-- meaning 4.989 rounds down to 4. :)

Refer to the article on Wikipedia for the chart & formula.

shon3i
1st November 2009, 16:38
In H264 specs look at Table A-1, you will find MaxDPB. AFAIK formula should be

Int(MaxDPB * 1024 / 1.5 / (w * h)

where is 1.5 for YV12 colorspace, i don't know for others.

showergel
1st November 2009, 17:14
In H264 specs look at Table A-1, you will find MaxDPB. AFAIK formula should be

Int(MaxDPB * 1024 / 1.5 / (w * h)

where is 1.5 for YV12 colorspace, i don't know for others.

Actually that formula is incorrect according to the spec. It's not width * height, it's (width in macroblocks) * (height in macroblocks) * 384 (According to section A.3.1.h). You have to divide the (x*y) pixel value by 256 to obtain the number of macroblocks in a given resolution for the equation to work itself out properly.

But... all of that is in the wikipedia article I linked to. :)

The intended audience for this info is for the end-user who is just trying to figure out what to put into MeGUI's x264 editor (or the x264 command line). Since there isn't a mechanism in either application to offer "suggestions" for Reference Frames (MeGUI only has the Levels Checker), the formula will make apparent what choices can be made; even with a non-mod-16 resolution. The lowest-value whole number will always be returned and should always result in a valid option.

I realize the forumla (as-is) isn't intended to be applied in an application because it doesn't validate a number of things; it works under the assumption a user has created a valid mod-16 .avs for encoding via MeGUI's "Clever Anamorphic encoding Option" or Resize with "Suggested Resolution (mod16)" selected.

LoRd_MuldeR
1st November 2009, 17:23
Well, non-mod16 resolutions are padded to mod16 anyway. So you must use the actual padded resolution in shon3i's formula.

To make shon3i's formula to work with non-padded non-mod16 resolutions, it must be extended like this:
floor(MaxDPB * 1024 / 1.5 / ([ceiling(w / 16) * 16] * [ceiling(h / 16) * 16]))

Adub
1st November 2009, 21:44
Wait does it really matter? Frankly I think you should use the maximum number of ref frames possible for your application (PC, Xbox, iPod, PS3, etc) as X264 will automatically choose the optimal number.

LoRd_MuldeR
1st November 2009, 21:52
Frankly I think you should use the maximum number of ref frames possible for your application (PC, Xbox, iPod, PS3, etc)

Correct. And that is defined by the H.264 Level supported by the individual device. And the Level defines the MaxDPB size.

Finally from the MaxDPB size the maximum number of ref frames can be calculated, depending of the resolution of the individual video clip.

as X264 will automatically choose the optimal number.

No, it won't. I will use the numer of ref frames you specify. x264 can't know for which device you are encoding, can it?

J_Darnley
1st November 2009, 21:56
No, it won't. I will use the numer of ref frames you specify. x264 can't know for which device you are encoding, can it?
If you specify --ref it will, otherwise let x264 know the DPB size with --level and it will limit ref count for you.

popper
1st November 2009, 21:58
Wait does it really matter? Frankly I think you should use the maximum number of ref frames possible for your application (PC, Xbox, iPod, PS3, etc) as X264 will automatically choose the optimal number.

remember though, this is "the maximum number" of refs allowed, that DOES NOT mean you should or must use the MAX refs.

infact its seems nothing more than ref=3 gives you anything werth mentioning quality wise , and might even stop smooth playback on some players, so tell them to Always use Ref=3 on their HD 1080P HP L4.1 and Be Happy when it also plays on most other players too.

just because You Can use a higher Value On anything, Does Not mean You Should.

Dark Shikari
1st November 2009, 21:59
infact its seems nothing more than ref=3 gives you anything werth mentioning quality wise , and might even stop smooth playback on some players[citation needed]

showergel
1st November 2009, 23:34
If you specify --ref it will, otherwise let x264 know the DPB size with --level and it will limit ref count for you.

While true, consider MeGUI in advanced mode doesn't allow you to omit the --ref setting, it must be specified & doesn't provide the user any guidance.

that DOES NOT mean you should or must use the MAX refs.

In the case of HD, 1920x1080 at Level 4.1 (In the PS3/Xbox360/Blu-Ray class of things), the Max is 4 frames. Would you really want to use fewer? I get the feeling from --fullhelp that it will just assume [3] no matter what- at least my encode times suggest both specifying & not specifying --ref 3 produces identially timed & sized encodes. In my experience and opinion it's one of those things where if the device supports a certain level, we really should make encodes that fully utilize the playback hardware's capability.

J_Darnley
1st November 2009, 23:44
While true, consider MeGUI in advanced mode doesn't allow you to omit the --ref setting, it must be specified & doesn't provide the user any guidance.

x264 cannot prevent GUIs from being stupid. If this was supposed to help people who use one, perhaps it would have been better to post this in the GUI forum. I thought megui omitted options that were left at their default settings. Perhaps it now just specifies everything to make it "easier".

Blue_MiSfit
1st November 2009, 23:50
x264's behavior seems optimal to this.

If you select a speed:quality preset that has more than 4 references, but also specify --level 41, x264 will obey level 4.1 and keep it at 4.

Unless of course you manually specify --ref 6 or something - in which case this takes precedence - overriding level 4.1

I think using the max number of refs supported by your hardware is the way to go - provided you can afford the encoding time. There are indeed benefits all the way up to 16 for certain content. That being said, more than 5 seems to have quite limited benefit for most film content AFAIK.

~MiSfit

showergel
2nd November 2009, 00:49
perhaps it would have been better to post this in the GUI forum.

That isn't my intent for this thread; it was merely an example. The point of this thread is to give people a way to calculate the maximum reference frame setting for themselves.

Snowknight26
2nd November 2009, 01:51
floor(MaxDPB * 1024 / 1.5 / ([ceiling(w / 16) * 16] * [ceiling(h / 16) * 16]))

Could replace

([ceiling(w / 16) * 16] * [ceiling(h / 16) * 16]))
with
((width+(16-(width%16)))*(height+(16-(height%16))))
in the case that you want to use less 'word' functions. ;)

J_Darnley
2nd November 2009, 02:21
@Snowknight26, sorry, that is incorrect.
width = 720, height = 576
((width+(16-(width%16)))*(height+(16-(height%16))))
= ((720+(16-(720%16)))*(576+(16-(576%16))))
= ((720+(16-0))*(576+(16-0)))
= (720+16)*(576+16)
= 736 * 592
;)

Snowknight26
2nd November 2009, 06:54
You're right. I changed my original equation from
((width+(width%16)))*(height+(height%16))))
but that didn't cover all cases either. Guess it's harder than I thought. Guess you can't do it without a piece-wise function unless you use word functions.

Boolsheet
2nd November 2009, 07:12
Just throw another modulo in. :P
(width+(16-width%16)%16) * (height+(16-height%16)%16)

kemuri-_9
2nd November 2009, 07:30
([ceiling(w / 16) * 16] * [ceiling(h / 16) * 16])
->
(((width+15)&~15) * ((height+15)&~15))

though it's not readable to most non-programmers, the above style is how x264 does it in a number of places.
simplicity-wise, what LoRd_MuldeR has would be easiest to read and understand for most people though.

Shevach
2nd November 2009, 12:19
To make shon3i's formula to work with non-padded non-mod16 resolutions, it must be extended like this:
floor(MaxDPB * 1024 / 1.5 / ([ceiling(w / 16) * 16] * [ceiling(h / 16) * 16]))

My two coins into the discussion.

Firstly I suggest to replace 1.5 with 3/2, because different platforms use different standards of floating-point (on one platform the result of above formula might be 4 while on another 3).

In such case the formula is
floor(MaxDPB * 1024*3 / 2 / ([ceiling(w / 16) * 16] * [ceiling(h / 16) * 16]))

Secondly, I suggest for sanity check to add clipping, i.e. to clip the result between 1 and 16.

asarian
4th June 2010, 01:55
So, what is the maximum number of reference frames the PS3 can handle then? 4 officially, it would seem; can it handle more too?

Shevach
7th June 2010, 08:59
In the recent version of H.264 standard (dated 03/2009) the issue how to determine the maximal number of reference frames (or MaxDpbFrames) has been clarified and simplified.
For all profiles MaxDpbFrames equal to Min( MaxDpbMbs / ( PicWidthInMbs * FrameHeightInMbs ), 16 ), where MaxDpbMbs is taken from the table A-1.

Motenai Yoda
7th June 2010, 12:52
imho should be X*Y*ReferenceFrames < 8388608 beyond that some sap player don't play or don't play correctly.
also for play with ps3 or x360 I suggest you use the appropriate preset in MeGUI.