PDA

View Full Version : Sample Aspect Ratio (SAR) in x264


Randall
22nd June 2007, 16:55
So does anybody know if it is possible to set the sample aspect ratio of a x264 encode AFTER the fact? I know you can do this during the encode by specifing --sar X:Y (which in my case, a 2.20:1 movie aspect @ 704 x 336, should be --sar 21:20... please correct me if I'm wrong :p). But is there a way to specify the sample aspect ratio after the encode is finished? I ask this because I forgot to set it, and at 2 passes (Insane HQ), 20 hours later I don't feel like encoding again. Is there a way to set this without encoding again?

It's my belief that running --sar X:Y in x264 will set the ratio for the raw stream, and not for the container format like MP4box does. This is what I'm looking for, since if the SAR is not set, it seems like mplayer ignores the pixel aspect ratio I specified during muxing with MP4box.

I've read through this thread (http://forum.doom9.org/showthread.php?t=100519) and others talking about setting aspect ratios, but I haven't found any way to set it after encoding that works. When I run "MP4Box -par 1=21/20" I get a very tall and skinny picture that does not look right in Nero, WMP, etc. But in mplayer it gets ignored all together. What am I doing wrong and how do I fix it? Thanks.

SealTooGreat
22nd June 2007, 17:24
When I run "MP4Box -par 1=21/20" I get a very tall and skinny picture that does not look right in Nero, WMP, etc.
Obviously PAR's numbers, you are using, are wrong.
If your source is NTSC than PAR should be 32:27, for PAL it's 64:45. Did you try these settings? Use it only on anamorphic DVDs (16:9 DAR). Your DVD picture must not be resized, only crop is allowed if you intend to use PAR and have correct aspect ratio upon playback.

MP4Box.exe -par 1=X:Y YourVideo.mp4

..where PAR=X:Y.

Latest Yamb 2.0 also have support for custom PAR.

SeeMoreDigital
22nd June 2007, 17:38
Obviously PAR's numbers, you are using, are wrong.
If your source is NTSC than PAR should be 32:27, for PAL it's 64:45. Did you try these settings? Your DVD picture must not be resized only crop is allowed if you intend to use PAR and have correct aspect ratio upon playback.

MP4Box.exe -par 1=X:Y YourVideo.mp4

..where PAR=X:Y.

Latest Yamb 2.0 also have support for custom PAR. Provided Randall's 704x366 encode contains nothing but image pixels (ie: there are no black mattes), then adding a PAR value of 21:20 to the MPEG-4 stream "will" allow his movie to be displayed at 2.20:1.


Cheers

SealTooGreat
22nd June 2007, 17:44
Provided Randall's 704x366 encode contains nothing but image pixels (ie: there are no black mattes), then adding a PAR value of 21:20 to the MPEG-4 stream "will" allow his movie to be displayed at 2.20:1.

SMD, you are wrong, as long as DVD image is not resized, you can crop as much as you like, PAR's numbers that I provided will stil/always give correct AR during playback. I've explained it here (http://forum.doom9.org/showthread.php?p=1008923#post1008923)

SeeMoreDigital
22nd June 2007, 18:49
Actually no I'm not... And here's the proof: -

704x336 with ARS@21:20 = MAR 2.20:1 (http://82.10.220.174/Uploaded_Files/Doom9_Forum_files/704x336_with_ARS@21.20=MAR@2.20.1.zip).

You are quoting MPEG-2 DVD DAR values, which have fixed aspect ratio signalling values depending on whether the MPEG-2 DVD video streams are either PAL 704/720x576, or NTSC 704/720x480. These "fixed" DAR values are specified in-accordance to the MPEG-2 DVD standard.

By contrast MPEG-4 video streams can be encoded at just about any "pixel frame size" (or resolution) you want. And then the pixels shape can be corrected (or tweaked) by adding the correct level of PAR signalling, afterwards!


Regards

Randall
22nd June 2007, 19:17
Provided Randall's 704x366 encode contains nothing but image pixels (ie: there are no black mattes), then adding a PAR value of 21:20 to the MPEG-4 stream "will" allow his movie to be displayed at 2.20:1.


Cheers
Thanks SMD, am I correct in assuming that sample aspect ratio (SAR) and pixel aspect ratio (PAR) are one in the same? Do you know if x264 stores this information in the raw video stream or in the container (mp4, mkv, etc.)? I know that MP4Box puts the PAR information into the container, but I'm wondering if x264 puts the SAR info directly into the raw video stream itself, and if so, I wonder if a tool exists that would allow someone to add or modify existing SAR info in the H.264 stream.

SeeMoreDigital
22nd June 2007, 19:26
Thanks SMD, am I correct in assuming that sample aspect ratio (SAR) and pixel aspect ratio (PAR) are one in the same?Yes, they are the same

Do you know if x264 stores this information in the raw video stream or in the container (mp4, mkv, etc.)? The aspect ratio signalling information is stored within the MPEG-4 stream

I know that MP4Box puts the PAR information into the containerActually no it does not. It puts it in the stream.

I used MP4box (with YAMB) to create the provided sample for you :)


Cheers

SealTooGreat
22nd June 2007, 21:26
@Randall
Could you give more info about source (PAL or NTSC)? Are you doing only crop or both crop and resize? If it isn't problem could you upload small sample (1-4mb)?

Randall
23rd June 2007, 02:22
@Randall
Could you give more info about source (PAL or NTSC)? Are you doing only crop or both crop and resize? If it isn't problem could you upload small sample (1-4mb)?It is an NTSC source that I cropped, no resize, but I don't see how that would make a difference. I am not sure how to "cut" a section of the file that would be that small, so sorry I can't upload it. I can post my avisynth script if you would like though. The movie is Ghostbusters, which upon further inspection has a MAR of 2.35:1, so I am using a PAR of 83:74 now! I don't know what I did differently, but it's working perfectly now! Thanks for your help guys.

# Load filter plugins
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\DGDecode.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\decomb.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\RemoveGrainSSE3.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\dustv5.dll")
Import("C:\Program Files\AviSynth 2.5\plugins\LimitedSharpen.avs")

# MPEG-2 film source
mpeg2source("H:\GHOSTBUSTERS\VIDEO_TS\ghostbusters.d2v")

# Inverse telecline 29.97 fps -> 23.97 fps
Telecide(guide=1).Decimate()

# Crop black borders and edges (720x480) -> (704x336)
crop(8,72,-8,-72)

# Denoise
RemoveGrain(mode=1)

converttoyuy2()
pixiedust(limit=5)
converttoyv12()

# Sharpen
LimitedSharpen(ss_x=2,ss_y=2,Smode=3,strength=100)

# Denoise again
RemoveGrain(mode=1)

SealTooGreat
23rd June 2007, 03:31
The movie is Ghostbusters, which upon further inspection has a MAR of 2.35:1, so I am using a PAR of 83:74 now! I don't know what I did differently, but it's working perfectly now! Thanks for your help guys.

There's no need for "further inspection". If source is NTSC, and you don't resize just crop, use PAR=32:27 - cause it's "fixed" standard for NTSC DVD and it's always constant regardless how much you crop. On the contrary DAR(or MAR how SMD prefer) is not "fixed" and depends on PAR. So finding DAR first, and than PAR(when one is already known) is very wrong, unless you don't know PAR you have to visually guess DAR.

And please could you explain what is "further inspection"?
My "further inspection" is visually guessing of movie DAR (*), only when It's wrongly resized by someone and don't know origin of source. But this step is very wrong and really unnecessary if PAR is known, and this is your case.

(*) visually guessing of movie DAR -> load movie into Virtual Dub ,use resize to determinate the most visual AR proportion pleasant, divide width/height and you will get DAR=width:height. Do this only in case when source origin(PAR) is unknown. And than use ARS tool for finding PAR upon (originally wrong not VD resized) resolution and previous visually guessed DAR(MAR).

SeeMoreDigital
23rd June 2007, 10:14
SealTooGreat, with respect you are over complicating Randall's original request.

As he has stated, he has generated his cropped encode at 704x336 pixels. And would to add the correct level of aspect ratio signalling so it can be displayed at 2.20:1 during playback.

Randall did not ask for other methods in how to generate his encode!


Cheers

SealTooGreat
23rd June 2007, 12:14
SMD, really I don't what are you reading, I've never stated how Randall should generate his own encode. Respect to you, but method "visually guessing of movie DAR" has nothing to do with encoding and it's only in case when PAR is unknown. He can skip reading all of this and just use PAR=32:27, because I was intrigued about his "further inspection" I just feel the need to state my own thinking about this.

SealTooGreat
23rd June 2007, 12:38
The movie is Ghostbusters, which upon further inspection has a MAR of 2.35:1, so I am using a PAR of 83:74 now!
SMD pleas read carefully this Randall's statement. He first determines DAR=2.35:1 (using "further inspection") and than PAR=83:74. There's no need for this step cause PAR is already 32:27 (the source is NTSC).

SeeMoreDigital
23rd June 2007, 15:43
SMD pleas read carefully this Randall's statement. He first determines DAR=2.35:1 (using "further inspection") and than PAR=83:74. There's no need for this step cause PAR is already 32:27 (the source is NTSC).Indeed.... If the "movie aspect ratio" is 2.35:1 (and not 2.20:1 as previously stated). And his encode has been encoded using a "pixel frame size" of 704x336. Then yes, the "aspect ratio signalling" value required "is": -

http://i18.tinypic.com/54n6zjk.png

SealTooGreat
23rd June 2007, 16:12
SeeMoreDigital, you are really a hard nut to crack :D. Do you really want me to send you small encoded sample where your process is obviously wrong. I've already proved(same link as above mentioned example) (http://forum.doom9.org/showthread.php?p=1008923#post1008923) that DAR guessing is indeed unnecessary when PAR is known. You are still making PAR upon guessed DAR... THERE'S NO NEED FOR THIS WHEN PAR IS ALREADY KNOWN

PAR=83:74=1,122
PAR=32:27=1,185

We have 2 PARs , NTSC source which is only croped, not resized... What do you think which one of those 2 PARs are the most suitable for Randall's encode? Answer to these question is really important to me for judging your AR signaling competence. Sorry for this!,,,but I had to write cause you still don't understand what I'm saying.

SeeMoreDigital
23rd June 2007, 17:21
Okay...

You work out the ARS/PAR values your way. And I'll do it mine!

Inventive Software
23rd June 2007, 18:03
I'll follow SMD's way too... it's worked so that the DAR is correct every time. Not like Xvid's so-called PAR! ;)

Most common PAR setting I use for a 720x576 PAL 16:9 image: 64:45. Gives the DAR 16:9.

You are after a value that multiplies the image width / height value to the resultant DAR. Example, 1.25 to 1.7777777 (etc). The multiple in this case is 64/45, or 1.4222222 (etc). Try it and see. ;)


EDIT: Actually, looking over it, that's a pretty simple way of looking at it. The PAR is the multiple between the source and the Display Aspect Ratio.

Excellent. You learn something new everyday. :D

SealTooGreat
23rd June 2007, 19:19
Okay...

You work out the ARS/PAR values your way. And I'll do it mine!
Man, I love you! :D

little homework (for those who have Photoshop(PS)):

Let's say we have PAL DVD. Take a snapshot to get anamorphic still image (ie:720x576). Load it into PS. Go to "Image->Pixel Aspect Ratio->Custom Pixel Aspect Ratio". Name it for example "Generic PAR 64:45 - PAL DVD" and in option "Factor" type 1.422 (cause 64:45=1.422). Click OK to apply new PAR preset, so the next time when you go to "Image->Pixel Aspect Ratio" you will find new preset "Generic PAR 64:45 - PAL DVD (1.42)"

Than try various crops on anamorphic PAL snapshot(with different width/height proportion). You will notice that the same preset ("Generic PAR 64:45 - PAL DVD (1.42)" ) will always give correct AR no matter how weird the crop is made.(You can also use Lasso tool :D to make it more odd).

You can try this PS example on the NTSC source, of course PAR should be 32:27=1.185

The point is if the source is known(PAL or NTSC) and if it's cropped, not resized (not resized is very important) than all you need is to apply known PAR values (64:45 for PAL and 32:27 for NTSC).

SeeMoreDigital's steps are OK if you don't know the source PAR or encode is encoded with the wrong resized AR.

Wilbert
23rd June 2007, 19:20
SealTooGreat, i agree with you. I don't understand why SMD gets a different value:

((480 * 16) / 9) / 720 = 1.185 (non-ITU)

SeeMoreDigital
23rd June 2007, 19:45
SealTooGreat, i agree with you. I don't understand why SMD gets a different value:

((480 * 16) / 9) / 720 = 1.185 (non-ITU)Eh... Who gets a different value?

http://i13.tinypic.com/6cpbpsj.png

32 divided by 27 = 1.18511851185

SealTooGreat
23rd June 2007, 19:53
SMD, So what would PAR be for croped 720x480->crop(8,72,-8,-72)->704x336....... 32:27 or 83:74 ? ;)

Wilbert
23rd June 2007, 20:13
Ok, i see what the problem is. The actual aspect ratio (or MAR as SMD calls it) is not 2.35 (which is listed on the disc probably, or on the internet), it's actually 2.483.

If SMD plugs that ratio in under "Custom MAR" he will get the same PAR as we do:

720x336: 336/1.185 = 283.5 => 704/283.5 = 2.483 (actual AR; without the black borders)
2.483/(704/336) = 1.185

Yes, i realize that this reasoning is circular, but it is just to show that the MAR of 2.35 is probably wrong.

SealTooGreat
23rd June 2007, 20:37
Yes, i realize that this reasoning is circular, but it is just to show that the MAR of 2.35 is probably wrong.
Thx, Wilbert. This is the proof that dvd PAR is the only constant on which you can firmly rely on. DAR is assumption if PAR is unknown. DAR can be correctly calculated using source PAR and cropped resolution as a known values. And it's also prove that aspect ratio listed on disc often can be wrong and you can not always precisely crop dvd image to get base for correct aspect ratio signaling (listed on disc). So the PAR is mother :D

P.S.
DAR is under the influence of crop -> changing crop values, automatically changes DAR values.
PAR is independent of crop -> no matter how much you change crop values, PAR is always the same. In this case image must not be resized, only cropping is allowed.

SealTooGreat
23rd June 2007, 21:29
@SeeMoreDigital
I don't really know what I have to prove to make you understand that you are wrong. You are referring to ARS calculator as the ultimate authority which in fact have only DAR->PAR calculation. So in the very beginning the base for further calculation is DAR . But you have to understand that PAR is ultimate constant. So now you only cycling your self proving that you method is good, but it's not, you are fooling yourself - it's better to realize this mistake now.

If your ARS tool have capability for PAR->DAR calculation, using PAR as ultimate reference, you would soon realize, that your way you now suggestion, is veeeery wrong, if PAR is already known.

SMD, I assume you are the author of ARS tool, than please make PAR->DAR calculation available, instead of only one way DAR->PAR
- than I could say that ARS is ultimate authority. You will certainly realize what is all about, cause now you see only one half of truth (which is in fact worse than untruth)

Wilbert
23rd June 2007, 21:55
Thx, Wilbert. This is the proof that dvd PAR is the only constant on which you can firmly rely on. DAR is assumption if PAR is unknown. DAR can be correctly calculated using source PAR and cropped resolution as a known values. And it's also prove that aspect ratio listed on disc often can be wrong and you can not always precisely crop dvd image to get base for correct aspect ratio signaling (listed on disc). So the PAR is mothe
No, that's false. You are confused about DAR. There are two uses of DAR. The first one is the DAR as specified in the header of the DVD (which can be 4x3 or 16x9). This is the one people should be talking about, when they are talking about DAR. The second one is an artificial DAR, which is called MAR by SMD (but several programs, like MeQui, call it this also DAR).

So PAR is definitely not the mother, because it's not specified in the stream itself, but only DAR is. The PAR is calculated from the DAR in the following way (for a 720x480 NTSC disc):

PAR = (480 * DAR) / 720 (non-ITU)


Now, the reality is more complicated as the story above, because there is a standard called ITU-R BT.470-6. This standard basically says how the digital video should be converted to analog (basically it says that the 702x576 / 711x486 constitutes the 4x3 or 16x9 image). The problem is that this standard and the DAR setting in the mpeg-2 stream is conflicting, and hence once in a while there are long discussions on this forum on which of the two points of view are correct. It all boils down to how the majority of the dvds are mastered (are they ITU compliant or not), but nobody knows for sure.

You can read more about this ITU stuff and PAR in the analog capture guide.

SealTooGreat
24th June 2007, 00:17
No, that's false. You are confused about DAR. There are two uses of DAR. The first one is the DAR as specified in the header of the DVD (which can be 4x3 or 16x9). This is the one people should be talking about, when they are talking about DAR. The second one is an artificial DAR, which is called MAR by SMD (but several programs, like MeQui, call it this also DAR).

So PAR is definitely not the mother, because it's not specified in the stream itself, but only DAR is. The PAR is calculated from the DAR in the following way (for a 720x480 NTSC disc):

PAR = (480 * DAR) / 720 (non-ITU)

I'm not confused at all; I know that there is global DAR like 16:9 and 4:3 (whole picture with black borders usually for 16:9). But there's also second DAR (or MAR) which regards to only movie frame without black borders (like 1.85, 2.35, 2.40 etc). When I’ve said aspect ratio often can be wrong I was referring to MAR - it's not accurate as global DAR and some DVDs have black borders, not only at top and bottom, also at right and left sides as well which lower MAR accurate. Global DAR is constant for anamorphic 16:9 NTSC standards (it’s 16:9), so the resolution as well (It’s always 720x480). Now from 2 constant values (global dvd DAR and resolution) we getting next value - PAR, which is constant, too, cause is created from two constant values as you have described.

DAR is result of width /height in the stretched state for anamorphic movie or in normal state for square pixel, so it’s very depended from these values.
PAR concerns to pixel circle look, so it doesn’t depend on width and height.


Once you crop DVD’s black borders, the base for global DAR signaling is changed cause width and height are changed. Now you can’t use dvd global DAR (16:9) and you have to rely on second DAR(MAR) which is not accurate cause of mentioned reasons. Only value, in this cropped state, which doesn’t depend on the width and height is PAR.
So the PAR as constant value from original DVD is more reliable than MAR itself.
So when I saying PAR is mother I was not wrong especially for cropped encode AR signaling purpose.

Btw you are wrong when say MeGUI uses artificial DAR, cause once the black borders are cropped, MAR becoming global DAR (cause MAR can’t exist if there are not black borders) and global DAR takes MAR values(in SMD case, but in mine, global DAR is calculated from constant PAR and cropped resolution). For instance if anamorphic DVD doesn’t have black borders than MAR doesn’t exist or you can say that MAR is becoming global DAR.




Now, the reality is more complicated as the story above, because there is a standard called ITU-R BT.470-6. This standard basically says how the digital video should be converted to analog (basically it says that the 702x576 / 711x486 constitutes the 4x3 or 16x9 image). The problem is that this standard and the DAR setting in the mpeg-2 stream is conflicting, and hence once in a while there are long discussions on this forum on which of the two points of view are correct. It all boils down to how the majority of the dvds are mastered (are they ITU compliant or not), but nobody knows for sure.

You can read more about this ITU stuff and PAR in the analog capture guide.

Well you are right, but excuse me, this is questioning of DVD standard and it’s not the topic of this thread. Nowadays PARs like NTSC 32:27 or PAL 65:45 are fine. You already calculated that it’s ok in case if resolution is 720x480. From Randall’s AVS script we can see that his DVD res. is 720x480, so there’s no need to go in such deep exploration if DVD standard is OK or not.

Randall
24th June 2007, 16:53
@SealTooGreat So what is the bottom line here? Should I always and forever use PAR values of 32:27 for NTSC and 65:45 for PAL regardless of what I crop (top/bottom or sides) as long as I don't scale or resize? Do these values only apply too anamorphic wide screen, or do letterbox sources have the same values? How did you come up with these global values? Thx. :thanks:

SealTooGreat
25th June 2007, 00:30
@SealTooGreat So what is the bottom line here? Should I always and forever use PAR values of 32:27 for NTSC and 65:45 for PAL regardless of what I crop (top/bottom or sides) as long as I don't scale or resize?
Yes, you should always use these values only if PAL is 720x576 and NTSC is 720x480. As Wilbert have mentioned, there's some issue if source resolution is different like 702x576 / 711x486 and I would add that some older DVD release have the same issue. You don't have to be worried about as long the source resolution is as I described. Of course dvd image must not be resized, only cropping is allowed - these steps are very important.

Do these values only apply too anamorphic wide screen, or do letterbox sources have the same values?
Yes, these values only apply to anamorphic DVD wide screen. Letterbox sources are square pixel or in another word PAR is 1:1.

How did you come up with these global values? Thx. :thanks:
Wilbert had already explained for NTSC (it's the same math procedure for PAL)

((480 * 16) / 9) / 720 = 1.185 (non-ITU)

Equation (http://www.mir.com/DMG/aspect.html) is:

width/height = DAR/PAR

Anamorphic 16:9 DVDs nowadays have always 2 constant values: resolution (PAL=720x576 or NTSC=720x480) and DAR (=16:9 in both NTSC and PAL cases). So it's not hard with simple math to get these constant PAR values (32:27=1.185 for NTSC or 64:45=1.422 for PAL) via mentioned equation.

Once you crop your DVD source, DAR loses its importance cause it depends on frame's width and height. But PAR is still functional cause it depends on pixel dimension (http://en.wikipedia.org/wiki/Pixel_aspect_ratio), not on frame's width and height, that's why it's very important not to resize source, cause it would lose its source pixel dimension.

If you have Photoshop you can try it for yourself to see that no matter how much you crop the dvd source you will always get correct AR look with the same PAR preset, as I have described (http://forum.doom9.org/showthread.php?p=1018059#post1018059). Or you can encode the same sample with different crop values using the same PAR in each case. I suggest you to use extreme crop values cause than it would be obvious (http://forum.doom9.org/showthread.php?p=1008923#post1008923).

Brother John
25th June 2007, 18:00
[Slightly offtopic]
A question especially to you, SMD, because you're the generic PAR expert. What's the exact logic behind generic PAR for NTSC? Do you use the traditional full 486 vertical pixels or only the 480 DVD range? In other words: Would I resize an NTSC 16:9 DVD from 720x480 to
720 * 6/5 = 864x480 = DAR 1.8
or
720 * 32/27 = 853,333x480 = DAR 1.78?
Is there something like an agreed specification for generic PAR because I've seen both methods being used?
[/Slightly offtopic]

SeeMoreDigital
25th June 2007, 23:22
[Slightly offtopic]
A question especially to you, SMD, because you're the generic PAR expert. What's the exact logic behind generic PAR for NTSC? Do you use the traditional full 486 vertical pixels or only the 480 DVD range? In other words: Would I resize an NTSC 16:9 DVD from 720x480 to
720 * 6/5 = 864x480 = DAR 1.8
or
720 * 32/27 = 853,333x480 = DAR 1.78?
Is there something like an agreed specification for generic PAR because I've seen both methods being used?
[/Slightly offtopic]I would use the latter... to match the MPEG-2 DVD spec.

I can't imagine why anybody would use 864x480 pixels, unless they were generating a full resolution "square pixel" encode conforming to mod-16. I which case, 848x480 would be closer, offering a square pixel resolution of 1.76666:1, instead of 1.8:1.


Cheers

anger98
15th August 2011, 08:46
Here is a handy calculator to fix the aspect ratio of an image with the PAR using MP4Box
http://tools.rodrigopolo.com/mp4box_aspect_fix/