Quality optimization concerning Cropping and De-Anamorphing

General questions or discussion about HandBrake, Video and/or audio transcoding, trends etc.
Post Reply
porg
Posts: 3
Joined: Fri May 28, 2010 12:56 am

Quality optimization concerning Cropping and De-Anamorphing

Post by porg » Fri May 28, 2010 1:25 am

Consider the following thoughts. Comments/corrections are very welcome!
Note: I have limited English vocabulary in the fields of Mathematics and Logic, I hope you can nevertheless understand my reasoning.

CROPPING
1) If your source video contains a "letterbox" or "pillarbox" or even worse both of them aka "postage-stamping" you very likely want to crop it, as:
a) this unnecessary information would just waste storage space (if you have set an average bitrate) or decrease quality (if you have set a fixed target size).
b) the handling of this files is complicated in many SW/HW media players (auto fullscreen fill functions won't work as expected).

So you will crop it, but at the same time, you want to achieve the optimal ratio of quality/storage-requirement!

As far as I know from the documentation:
a) To fully utilize the codec, the picture size must be a multiple of the codec's macroblock size (16x16 for MPEG formats).
b) The anamorphic settings "loose" and "none" ensure that the picture size is a macroblock multiple.
Assuming this I conclude the following:
1.1) If you need to crop your picture, and want to achieve the best result with the rescaling algorithm, which also follows the rules of mathematics, then crop to a size of:
a) Best, as this needs least or only little interpolation: "2^n * m"
b) Still ok, needs little interpolation: "n * m" Except of course those which match (a)
c) Less good, needs much interpolation: "n" Except of course those which match (a) or (b)

Legend:
n = Positive integer = 0,1,2,3,...
m = Macroblock size = 16


DE-ANAMORPHING (Achieve a video file native 1:1 PAR)
2) If you intend to mainly use the video file on computer monitors then I strongly prefer a 1:1 PAR rather than an anamorphic PAR because:
a) Computer display devices mostly use native 1:1 pixel aspect ratios, no stretching/compression processing at runtime needed (even if that anyhow happens on the GPU).
b) So why not bring the video into a PAR of 1:1 ONCE AND ONLY while its initial generation process, compared to EACH RUNTIME if you open it in your media playing software, which needs to un-distort it for usage within your 1:1 PAR medium? I think the first one is far more efficient!

2.1) Hence you will use "Anamorphic: None" and as well you likely want to "Keep aspect ratio: Yes".
If you then use certain crop sizes in combination with certain picture sizes, this necessarily results in a slight distortion compared to the original aspect ratio.
The smaller the target picture size, the more distortion recognisable, as the worst possible ratio of the jump intervals (= macroblock size) to the picture dimensions grows.
The larger the target picture size, the less distortion recognisable, as the worst possible ratio of the jump intervals (= macroblock size) to the picture dimensions shrinks then.

FEATURE SUGGESTION
2.2) Currently Handbrake shows this in the preview window:
Source: NxN, Output: NxN
For informing the user better, how far s/he manipulates the aspect ratio under the current settings, it would be nice if Handbrake would showt:
Source: NxN, Output: NxN, Output PAR: 1:1.xxxx, Difference to original PAR: None | +X.XX% (to fat) | -X.XX% (to thin)

creamyhorror
Regular User
Posts: 134
Joined: Mon Jan 04, 2010 2:00 pm

Re: Quality optimization concerning Cropping and De-Anamorph

Post by creamyhorror » Fri May 28, 2010 10:36 am

No. If a source is anamorphic, for best quality you completely avoid resizing it during the encode stage. It's useless to resize to square pixels since it gets resized anyway during fullscreen playback. So optimally you would limit the resizing to one stage only: the decoding.

Also, mod16 resolutions are generally overrated in terms of compression advantage. They're largely only useful in cases where the player is incapable of handling non-mod16 resolutions.

In other words, Strict Anamorphic is the way to go.

User avatar
Rodeo
HandBrake Team
Posts: 12160
Joined: Tue Mar 03, 2009 8:55 pm

Re: Quality optimization concerning Cropping and De-Anamorph

Post by Rodeo » Fri May 28, 2010 1:37 pm

porg wrote:DE-ANAMORPHING (Achieve a video file native 1:1 PAR)
2) If you intend to mainly use the video file on computer monitors then I strongly prefer a 1:1 PAR rather than an anamorphic PAR because:
a) Computer display devices mostly use native 1:1 pixel aspect ratios, no stretching/compression processing at runtime needed (even if that anyhow happens on the GPU).
b) So why not bring the video into a PAR of 1:1 ONCE AND ONLY while its initial generation process, compared to EACH RUNTIME if you open it in your media playing software, which needs to un-distort it for usage within your 1:1 PAR medium? I think the first one is far more efficient!
Unless your videos match your monitor resolution exactly, there will always be scaling, regardless of PAR (sometimes, devices will scale even if the monitor and video resolutions match). Except with your suggestion, scaling happens twice (once before encoding, once during playback) as opposed to just once (during playback).

So it's less efficient.
porg wrote:2.1) Hence you will use "Anamorphic: None" and as well you likely want to "Keep aspect ratio: Yes".
If you then use certain crop sizes in combination with certain picture sizes, this necessarily results in a slight distortion compared to the original aspect ratio.
The smaller the target picture size, the more distortion recognisable, as the worst possible ratio of the jump intervals (= macroblock size) to the picture dimensions grows.
The larger the target picture size, the less distortion recognisable, as the worst possible ratio of the jump intervals (= macroblock size) to the picture dimensions shrinks then.
The nightlies allow you to specify a modulus in None to minimize this problem.
porg wrote:FEATURE SUGGESTION
2.2) Currently Handbrake shows this in the preview window:
Source: NxN, Output: NxN
For informing the user better, how far s/he manipulates the aspect ratio under the current settings, it would be nice if Handbrake would showt:
Source: NxN, Output: NxN, Output PAR: 1:1.xxxx, Difference to original PAR: None | +X.XX% (to fat) | -X.XX% (to thin)
That might be nice, but requires a certain amount of code (the logic needed to calculate difference to PAR and difference to DAR) for minimal benefit.

Personally, I think there are a few bugs - particularly in anamorphic Custom - that would benefit from being fixed before new, minor features such as this one are added.

porg
Posts: 3
Joined: Fri May 28, 2010 12:56 am

Re: Quality optimization concerning Cropping and De-Anamorph

Post by porg » Fri May 28, 2010 2:06 pm

I continued my tests and thinking, considered @creamyhorror, and came to this

SUMMARY for optimal crop/anamorphic settings:

1) If you don't have the need to crop

Use this anamorphic setting:
a) "Strict": Conserves the full horizontal AND vertical information.
b) "Loose": Reduce the picture size, keeping/loosing horizontal AND vertical info at the (almost) same rate.
c) "None": Reduce the picture size, loosing none or some horizontal AND EVEN MORE vertical info.

2) If you need to crop:

2.1) Concerning the cropped size, consider the rescale algorithms! The ratio to the original size shall at best be 2^n*m or at least n*m, else tougher interpolation required!

2.2) Use this anamorphic setting:
a) "Loose": More original pic data conserved. Needs more runtime un-distorting.
b) "None": You get the computer displays native 1:1 PAR, can be a benefit for some applications. But less original pic data is conserved. But only because HandBrake does not allow upsampling the width (the same which is done while decoding/playback of anamorphic videos) so that the full resolution of the height is conserved. Of course it would require more storage space but at the benefit of less un-distorting processing need (compared to anamorphic PAR).

LIMITATIONS of this preferred order:

As @creamyhorror mentioned, rescaling must anyhow be done for fullscreen playback, so the de-anamorphing (un-distorting) is not much processing compared to it, and I guess even less compared to the required processing of an advanced codec such as h264! Right?

SO you can use my preferred order, BUT if you know your playback application supports anamorphic and non-macroblock-size playback/editing/whatever, don't use the described methods whose sole benefit is to avoid these problems, but rather use those methods, which preserve the most horizontal and vertical information!

QUESTION @creamyhorror:
  • For what dimensions does cropping make sense by means of codec/storage efficiency?
  • I guess for intensively letter/pillar-boxed video, with large top/bottom/left/right black mattes, it really makes sense to crop them, both for storage-size/codec-quality reasons.
  • But how is it with some narrow "garbage area" on the top, bottom, left or right, resulting from a bad film to video, or video tape to digital conversion, which you would like to cut off for aesthetic reasons, or in order that the fullscreen playback can utilize the maximum height/width for real picture information rather than wasting its edge areas for "garbage or black mattes"?

User avatar
JohnAStebbins
HandBrake Team
Posts: 5440
Joined: Sat Feb 09, 2008 7:21 pm

Re: Quality optimization concerning Cropping and De-Anamorph

Post by JohnAStebbins » Fri May 28, 2010 3:20 pm

# For what dimensions does cropping make sense by means of codec/storage efficiency?
# I guess for intensively letter/pillar-boxed video, with large top/bottom/left/right black mattes, it really makes sense to crop them, both for storage-size/codec-quality reasons.
Cropping almost always makes sense when there are black borders. The sharp edge at the border hurts compression efficiency a lot, so should always be cropped. The only time you would not do this is if your target player did not respect aspect ratio settings in the encoded video and you have to force matting around the image to make it look right. This case is very very rare.

mduell
Veteran User
Posts: 6626
Joined: Sat Apr 21, 2007 8:54 pm

Re: Quality optimization concerning Cropping and De-Anamorph

Post by mduell » Fri May 28, 2010 4:08 pm

The only reason to deanamorph is if your playback device doesn't support anamorphic. Even if you save a few CPU cycles on the resizing, you're hurting storage efficiency (if you preserve height) or quality (if you preserve width).

creamyhorror
Regular User
Posts: 134
Joined: Mon Jan 04, 2010 2:00 pm

Re: Quality optimization concerning Cropping and De-Anamorph

Post by creamyhorror » Sun Jun 20, 2010 6:44 am

porg wrote: QUESTION @creamyhorror:
  • For what dimensions does cropping make sense by means of codec/storage efficiency?
  • I guess for intensively letter/pillar-boxed video, with large top/bottom/left/right black mattes, it really makes sense to crop them, both for storage-size/codec-quality reasons.
  • But how is it with some narrow "garbage area" on the top, bottom, left or right, resulting from a bad film to video, or video tape to digital conversion, which you would like to cut off for aesthetic reasons, or in order that the fullscreen playback can utilize the maximum height/width for real picture information rather than wasting its edge areas for "garbage or black mattes"?
As Mr Stebbins said, always crop, unless your player doesn't support anamorphic playback.
2.1) Concerning the cropped size, consider the rescale algorithms! The ratio to the original size shall at best be 2^n*m or at least n*m, else tougher interpolation required!
I don't understand this, or the reasoning for it.
2.2) Use this anamorphic setting:
a) "Loose": More original pic data conserved. Needs more runtime un-distorting.
b) "None": You get the computer displays native 1:1 PAR, can be a benefit for some applications. But less original pic data is conserved. But only because HandBrake does not allow upsampling the width (the same which is done while decoding/playback of anamorphic videos) so that the full resolution of the height is conserved. Of course it would require more storage space but at the benefit of less un-distorting processing need (compared to anamorphic PAR).
With respect to quality, best of all is Strict Anamorphic, with cropping or not.

Post Reply