Kodi Community Forum

Full Version: Another question on aspect ratio
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello all,

i'm new to xbmc, and loving it! :thumbsup:

i was reading some posts about aspect ratio, and i'm still trying to figure out what i'm doing wrong with one kind of transcoding.

firstly, if i rip a typical ntsc movie off a dvd, it ends up at 720x480, but the "real" aspect ratio should be 16:9. therefore, if the rendering display has square pixels (x dpi == y dpi), this image must be stretched to 853x480 to be displayed at the correct aspect ratio.

if i encode to mp4 (using xvid and ffmpeg) and keep the same size (720x480), when i run xbmc in stretch 16:9, i get exactly the correct aspect ratio. that is, it works!

now, the video is really letterboxed in the 720x480 res. if i crop the top and bottom, the video is actually 720x360. because the rip is from a production movie, the actual aspect ratio of the "movie strip" is about 2.38 - but i'll settle for 2.37 - which is 720 stretched to 853 x 360 = 2.37.

so, i crop out the top 60 and bottom 60 lines from my transcode to produce a 720x360 xvid mp4 file. since i was using ffmpeg, i selected the option -aspect 2.37. also, i needed to use virtualdubmod to add the ac3 audio track back into the stream, so the xvid output is using the avi container (ffmpeg switch -f avi).

when i play this 720x360 file in xbmc, it doesn't seem to "see" the -aspect 2.37 option i gave to ffmpeg. maybe that option doesn't properly set the right flags in the xvid header, or perhaps there's something fishy using an avi container instead of an mp4 container.

does anyone have any suggestion on how i can use ffmpeg's xvid transcoder and also properly tag the aspect ratio correctly to xbmc reads the tag and properly scales the video? for now, i'll just do them at 720x480 - cause that seems to work, but i'd prefer to have the video's the correct size without the letterboxing in the stream.

thanks,
bb.
one assumes that you are running xbmc in "normal" view mode?

also, do you know whether ffmpeg sets the aspect ratio in the xvid stream, or in the container? because avi does not have a flag for aspect ratio.
yes, normal mode is what i try first.  then i try all the view modes to see if i can get it right.

i think this is an ffmpeg issue.  even though i specify the ffmpeg command line option "-aspect 2.37", i don't know if this is making it to the xvid stream or not.

i tried using virtualdubmod instead, and used the direct xvid compressor.  when i do this, and use the aspect setting in the xvid compressor setup, then xbmc sees the setting and adjusts perfectly.  so i suspect that ffmpeg isn't really passing the -aspect option into the xvid code or i'm not specifying it correct.  i might try another attempt with "-aspect 237:100" instead of '-aspect 2.37".

bb.
ok, this is definitely an ffmpeg issue.

i tried making a pure mp4 file with ffmpeg using "-f mp4". when i set the "-aspect 237:100" or "-aspect 2.37" options, this was embedded in the stream and xbmc happily scaled the display correctly.

but, when i make an avi file with ffmpeg using "-f avi", the "-aspect" setting does not effect the avi file at all. i tried setting different aspect ratios, but all the resulting avi files were identical at the binary level, hence no aspect information.

this is interesting because the xvid encoder (being run from virtualdub) does correctly get the ar information into an avi file. that is, i can set the ar info in the xvid encoder settings, and the resulting avi file will have that information which xbmc happily obeys.

so, seems that ffmpeg (-vcodec xvid -f avi) ignores the -aspect option completely. one mystery solved, infinity to go.

bb.
for anyone following this thread, here's some followup information.

i'm pretty sure ffmpeg does not add the par information to the mpeg4 stream, or it simply defaults it to 1:1.

i found a handy utility at http://www.moitah.net called mpeg4-modifier.

it can open an mpeg4 stream in an avi file (not the newer opendml format though), and it lets you modify the ar of the stream.  when you save the file, there's no transcoding, just the ar information is updated.

so i opened one of my 720x360 .avi files, and lo - there was no ar information.  so i set my par to 32:27 and wrote a new copy of the .avi file.  played it in xbmc and it now displays at the correct 2.37:1 aspect ratio.

read on for the maths...

where does a par of 32:27 come from?  well, the original ntsc dvd is 720x480, but it needs to be displayed at 16:9.  if this was scaled correctly, then the display would be 853.333x480.  but in my .avi file, i cropped off the top and bottom 60 pixel, effectively masking the video to 720x360.  i didn't rescale the video, so to display my 720x360 video correctly, it would have to be rendered at 853.333x360.  now, 853.333/360 = 2.37 display aspect ratio.  but an mpeg4 stream is encoded with a pixel aspect ratio.  in this case, the par represents 853.333/720=1.185, that is, how much horizontal anamorphic scaling is required to display the image correctly.  the 2 values set in mpeg4-modifier must be < 255.  so it works out that 32/27=1.185 and both numbers are < 255 and factored down to be as small as possible.

good tech info here: http://www.seemoredigital.net/03_video_info.html

bb.
the exact factor should be:

17280/14217 = 1.215.

incidentally, to get good fractional approximation, you use the following method (continued fractions)

first get rid of any whole numbers (1) to leave 0.215

then invert it 1/0.215 = 4.65116. drop the whole number (4) and invert again gives 1/0.65116 = 1.53572. repeat as long as you like.

to construct the fraction, you then use the whole numbers:

1, 4, 1 in this case.

the fraction is then:

1 + 1/(4 + 1/1) = 1+1/5 = 6/5.

continuing on in this fashion we'd get the whole numbers: 1, 4, 1, 1, 1.

1 + 1/(4+1/(1+1/(1+1/1))) = 17/14 which is around 1.214 - pretty good approximation.

this method always gives you the best possible approximation for the cases you are dealing with.

maybe if i have some spare time, i'll do up the continued fractions for the 4 dvd pixel ratios and see how they come out.

pal 4x3 is easy: 128/117 is exact.

cheers,
jonathan
just found an online continued fraction calculator Smile here: http://www.mcs.surrey.ac.uk/personal/r.k...fcalc.html

best fractions for each pixel ratio are:

pal 4x3: 128/117 (128/117)

pal 16x9: 159/109 (512/351)

ntsc 4x3: 134/147 (4320/4739)

ntsc 16x9: 79/65 (17280/14217)

these make sure it fits into the 255 limits for both numerator and denominator. exact figures are shown in brackets.

cheers,
jonathan