Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Video Encoding > MPEG-4 AVC / H.264

Reply
 
Thread Tools Search this Thread Display Modes
Old 1st November 2009, 15:40   #1  |  Link
showergel
Registered User
 
Join Date: Jan 2007
Posts: 27
How-To: Determine the maximum number of Reference Frames

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.

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.
showergel is offline   Reply With Quote
Old 1st November 2009, 16:38   #2  |  Link
shon3i
BluRay Maniac
 
shon3i's Avatar
 
Join Date: Dec 2005
Posts: 2,426
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.
shon3i is offline   Reply With Quote
Old 1st November 2009, 17:14   #3  |  Link
showergel
Registered User
 
Join Date: Jan 2007
Posts: 27
Quote:
Originally Posted by shon3i View Post
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.

Last edited by showergel; 1st November 2009 at 18:50. Reason: forgot something.
showergel is offline   Reply With Quote
Old 1st November 2009, 17:23   #4  |  Link
LoRd_MuldeR
Software Developer
 
LoRd_MuldeR's Avatar
 
Join Date: Jun 2005
Location: Last House on Slunk Street
Posts: 13,134
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]))
__________________
There was of course no way of knowing whether you were being watched at any given moment.
How often, or on what system, the Thought Police plugged in on any individual wire was guesswork.


LoRd_MuldeR is offline   Reply With Quote
Old 1st November 2009, 21:44   #5  |  Link
Adub
Fighting spam with a fish
 
Adub's Avatar
 
Join Date: Sep 2005
Posts: 2,685
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.
__________________
FAQs:Bond's AVC/H.264 FAQ
Site:Adubvideo
Adub is offline   Reply With Quote
Old 1st November 2009, 21:52   #6  |  Link
LoRd_MuldeR
Software Developer
 
LoRd_MuldeR's Avatar
 
Join Date: Jun 2005
Location: Last House on Slunk Street
Posts: 13,134
Quote:
Originally Posted by Adub View Post
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.

Quote:
Originally Posted by Adub View Post
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?
__________________
There was of course no way of knowing whether you were being watched at any given moment.
How often, or on what system, the Thought Police plugged in on any individual wire was guesswork.



Last edited by LoRd_MuldeR; 1st November 2009 at 21:55.
LoRd_MuldeR is offline   Reply With Quote
Old 1st November 2009, 21:56   #7  |  Link
J_Darnley
Registered User
 
J_Darnley's Avatar
 
Join Date: May 2006
Posts: 959
Quote:
Originally Posted by LoRd_MuldeR View Post
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.
__________________
x264 log explained || x264 deblocking how-to
preset -> tune -> user set options -> fast first pass -> profile -> level
Doom10 - Of course it's better, it's one more.
J_Darnley is offline   Reply With Quote
Old 1st November 2009, 21:58   #8  |  Link
popper
Registered User
 
Join Date: Mar 2006
Posts: 272
Quote:
Originally Posted by Adub View Post
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.

Last edited by popper; 1st November 2009 at 22:01.
popper is offline   Reply With Quote
Old 1st November 2009, 21:59   #9  |  Link
Dark Shikari
x264 developer
 
Dark Shikari's Avatar
 
Join Date: Sep 2005
Posts: 8,688
Quote:
Originally Posted by popper View Post
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]
Dark Shikari is offline   Reply With Quote
Old 1st November 2009, 23:34   #10  |  Link
showergel
Registered User
 
Join Date: Jan 2007
Posts: 27
Quote:
Originally Posted by J_Darnley View Post
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.

Quote:
Originally Posted by popper;
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.

Last edited by showergel; 1st November 2009 at 23:55.
showergel is offline   Reply With Quote
Old 1st November 2009, 23:44   #11  |  Link
J_Darnley
Registered User
 
J_Darnley's Avatar
 
Join Date: May 2006
Posts: 959
Quote:
Originally Posted by showergel View Post
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".
__________________
x264 log explained || x264 deblocking how-to
preset -> tune -> user set options -> fast first pass -> profile -> level
Doom10 - Of course it's better, it's one more.
J_Darnley is offline   Reply With Quote
Old 1st November 2009, 23:50   #12  |  Link
Blue_MiSfit
Derek Prestegard IRL
 
Blue_MiSfit's Avatar
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 5,695
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
Blue_MiSfit is offline   Reply With Quote
Old 2nd November 2009, 00:49   #13  |  Link
showergel
Registered User
 
Join Date: Jan 2007
Posts: 27
Quote:
Originally Posted by J_Darnley View Post
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.
showergel is offline   Reply With Quote
Old 2nd November 2009, 01:51   #14  |  Link
Snowknight26
Registered User
 
Join Date: Aug 2007
Posts: 1,395
Quote:
Originally Posted by LoRd_MuldeR View Post
floor(MaxDPB * 1024 / 1.5 / ([ceiling(w / 16) * 16] * [ceiling(h / 16) * 16]))
Could replace

Quote:
([ceiling(w / 16) * 16] * [ceiling(h / 16) * 16]))
with
Quote:
((width+(16-(width%16)))*(height+(16-(height%16))))
in the case that you want to use less 'word' functions.
Snowknight26 is offline   Reply With Quote
Old 2nd November 2009, 02:21   #15  |  Link
J_Darnley
Registered User
 
J_Darnley's Avatar
 
Join Date: May 2006
Posts: 959
@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
__________________
x264 log explained || x264 deblocking how-to
preset -> tune -> user set options -> fast first pass -> profile -> level
Doom10 - Of course it's better, it's one more.

Last edited by J_Darnley; 2nd November 2009 at 02:36.
J_Darnley is offline   Reply With Quote
Old 2nd November 2009, 06:54   #16  |  Link
Snowknight26
Registered User
 
Join Date: Aug 2007
Posts: 1,395
You're right. I changed my original equation from
Quote:
((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.
Snowknight26 is offline   Reply With Quote
Old 2nd November 2009, 07:12   #17  |  Link
Boolsheet
Registered User
 
Join Date: Apr 2009
Location: Switzerland
Posts: 69
Just throw another modulo in. :P
Quote:
(width+(16-width%16)%16) * (height+(16-height%16)%16)
__________________
My nightmares are horrifying, they're all interlaced!

Last edited by Boolsheet; 2nd November 2009 at 07:31. Reason: Ah, very clever kemuri ;)
Boolsheet is offline   Reply With Quote
Old 2nd November 2009, 07:30   #18  |  Link
kemuri-_9
Compiling Encoder
 
kemuri-_9's Avatar
 
Join Date: Jan 2007
Posts: 1,348
Code:
([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.
__________________
custom x264 builds & patches | F@H | My Specs

Last edited by kemuri-_9; 2nd November 2009 at 07:42.
kemuri-_9 is offline   Reply With Quote
Old 2nd November 2009, 12:19   #19  |  Link
Shevach
Video compressionist
 
Join Date: Jun 2009
Location: Israel
Posts: 126
Quote:
Originally Posted by LoRd_MuldeR View Post
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.
Shevach is offline   Reply With Quote
Old 4th June 2010, 01:55   #20  |  Link
asarian
Registered User
 
Join Date: May 2005
Posts: 1,372
So, what is the maximum number of reference frames the PS3 can handle then? 4 officially, it would seem; can it handle more too?
__________________
Gorgeous, delicious, deculture!
asarian is offline   Reply With Quote
Reply

Tags
h.264, maximum, reference frames, wikipedia, x264

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 23:05.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.