mattmartini / itivo

Automatically exported from code.google.com/p/itivo
Other
0 stars 0 forks source link

Major encoding improvements with anamorphic video and HandBrake #62

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Standard NTSC video downloaded off the Tivo comes in *anamorphic* MPEG-2 -- 
that is, the 
downloaded video always has 480 pixels of vertical resolution, but even though 
it's designed to 
display at a 4:3 aspect ratio, it doesn't have a full 640 pixels of horizontal 
resolution.  Rather, 
the horizontal dimension is compressed into a smaller number of pixels, and 
then the video 
specifies a non-square pixel aspect ratio (PAR) to tell the video player to 
stretch it the 
appropriate amount when playing.  In other words, it squeezes the width of each 
frame down to 
a smaller width in the stored video (at a bit of a quality loss), so it takes 
up less space, then 
stretches the video back out when displaying it.  For details on anamorphic 
encoding, see http://trac.handbrake.fr/wiki/AnamorphicGuide

For shows recorded in Best or High quality, the video is stored as 480x480 PAR 
4:3 (so the 
frames are stretched before output to 1.33 times their stored width, meaning 
you get 640x480 
video displayed).  For shows recorded in Medium or Basic quality, the video is 
in 352x480 PAR 
20:11 (which, again, expands to display at 640x480).

iTivo's current settings, however, don't take advantage of anamorphic encoding. 
 The default 
mencoder settings (-vf dsize=480:320:0,scale=-8:-8) will hard-stretch the video 
to 480x320 
and then encode frames at this size.*  This means that if you try to encode, 
say, a show that was 
recorded in Medium quality on the Tivo, the encoded video will be stored with 
frames that are 
480 pixels wide, whereas the source data only had 352 pixels of horizontal 
resolution; in other 
words, we're storing more pixels then we need to (meaning either it takes up 
more space, or it's 
lower quality because you have fewer bits per pixel).  A much better solution 
is to continue with 
storing only 352 pixels of width, but set the PAR so that it stretches out to 
the proper width.

(* Note, by the way, that 480x320 distorts the video width by about 12%.  To 
maintain the show's 
4:3 aspect ratio, they should be scaled to 426x320.  But that's not great 
either, because H.264 is 
designed for dimensions divisible by 16, and x264 warns that otherwise 
"compression will 
suffer."  So what should really be done there is 432x320.  When played back on 
the iPhone, it'll 
actually give you the option of stretching this up to fit the full screen by 
double-tapping the 
video.)

A much better solution is to continue making use of anamorphic encoding in our 
output files.  So 
if the input video is Medium quality, coming in at 352x480 PAR 20:11, we want 
to encode video 
frames at 352x320 PAR 213:176.  This will play back at 426x320 (which is the 
correct 4:3 aspect 
ratio), but will only need to store about 112k pixels per frame, whereas 
hard-stretching the 
video on 426x320 before encoding required about 136k pixels -- a savings of 
about 17%, which 
is huge.

HandBrake is capable of doing this, but the built-in "iPhone & iPod touch" 
preset currently does 
not.  (Why?  Because HandBrake was originally designed to encode DVDs, which 
always had much 
higher resolution than an iPhone display, so they never had any need to use 
anamorphic 
encoding on the iPhone).  To enable it, we just have to tell HandBrake to 
override some of the 
default iPhone presets to use anamorphic encoding and specify the proper 
width/height, like 
this:

-Z "iPhone & iPod Touch" --loosePixelratio --maxWidth 480 --maxHeight 360

This should generate much smaller files (if you continue using constant-quality 
encoding, as the 
preset does) or much higher quality at the same file size (if you use an 
average bitrate).  (Note: 
it's important that you specify both maxWidth *and* maxHeight, or HandBrake 
will wind up 
unnecessarily encoding the full 480 pixels of frame height, due to a bug 
discussed at http://forum.handbrake.fr/viewtopic.php?f=12&t=8346 .)

Separately, there are a number of other HandBrake iPhone settings that we 
should be changing 
when encoding Tivo video.  For instance, unlike most DVDs, Tivo video is 
interlaced, so we need 
to turn on the --decomb filter (this is very important!).  Similarly, the 
iPhone preset leaves the 
keyframe interval at the default of 250, meaning that when fast-forwarding or 
rewinding, you 
can only stop at 250/29.97 = about 8-second intervals.  This might be desirable 
when skipping 
through a long DVD, but when watching a short TV show, you probably want to be 
able to seek 
to about one-second precision, which would be a keyint of 30.  Finally, the 
iPhone preset uses 
constant-quality encoding, which makes sense when ripping from a full-quality 
DVD, but 
doesn't make sense when encoding from a Tivo (keeping 60% quality from a show 
recorded at 
Basic quality is very different from 60% quality a show recorded at Best 
quality).

I therefore urge that we switch the HandBrake encoder options to the following 
(some of which 
follows the built-in iPhone preset, but with several changes):

./HandBrakeCLI --format mp4 --markers --ipod-atom --encoder x264 --x264opts 
level=30:keyint=30:cabac=0:ref=1:analyse=all:me=umh:subq=6:no-fast-pskip=1 --vb 
384 --
aencoder faac --ab 128 --mixdown stereo --drc 2.5 --crop 0:0:0:0 
--loosePixelratio --
maxWidth 480 --maxHeight 320 --decomb -i [InputFile] -o [OutputFile]

A few values to highlight here: "--vb 384" sets the video bitrate to 384 kbps 
and "--ab 128"; I 
know you've thought about making these easily user-adjustable at some point.  
(I often tune 
these down much lower, particularly for shows without much motion like The 
Daily Show, and 
especially with the video quality improvements from anamorphic encoding.)  
Also, "keyint=30" 
sets the keyframe frequency (30 is one per second), which also controls the 
seek precision when 
rewinding or fast-fowarding, and which also helps minimize distortion when 
there's heavy 
movement (because the full image gets refreshed every 30 frames).  And 
"--loosePixelratio --
maxWidth 480 --maxHeight 320" lets us take advantage of the major gains from 
anamorphic 
encoding.

I know HandBrake support is still experimental, but this should be a pretty 
major improvement 
over the default settings with mencoder -- try it with a couple different 
bitrates and compare for 
yourself :-)

-Scott

Original issue reported on code.google.com by snov...@gmail.com on 14 Dec 2008 at 11:26

GoogleCodeExporter commented 9 years ago
First of all.. WOW that's one heck of a long bug report  :)  But thanks for 
taking
the time to do this.

  One big question before I make the change: does this setting work right for inputs
from low-quality SD to 1920x1080 HD input?  (i.e. does 720p input mess up 
decomb?). 
As you can tell I know nothing about these encoders, and have been pretty much 
going
off the goodwill of people on the net to figure out what settings to use.  The 
only
annoyance is that I don't know in advance anything about the input, so I need 
it to
be 'generic' working for all possible inputs from the tivo.

  Also, since I'm guessing you're playing a lot with the formats, In case you're
using 1.6..  1.7b2 adds support for saving your format to a file, which might 
make
your life easy while you're testing things out...

Original comment by yoav.yer...@gmail.com on 15 Dec 2008 at 2:42

GoogleCodeExporter commented 9 years ago
You folks are FAR too advanced for a simple guy like me, and I still need help. 
My downloads (obviously in medium quality) arrive as 352x480 in MP4 format. 
What do I have to do to manually edit the program in something like iMovie, and 
then burn it to a DVD and have it come out in proper 4:3?  I find the above 
VERY confusing.

Original comment by DRy...@huntoil.com on 22 Jun 2010 at 4:58