<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="content-type"
 content="text/html; charset=ISO-8859-1">
  <title>Cropping and scaling</title>
</head>
<body>
<p>
Recall
from the previous section that the final picture size you
encode should be a multiple of 16 (in both width and height). This can
be achieved by cropping, scaling, or a combination of both.
</p>
<p>
When cropping, there are a few guidelines that must be followed to
avoid damaging your movie. The normal YUV format, 4:2:0, stores chroma
(color) information subsampled, i.e. chroma is only sampled half as
often in each direction as luma (intensity) information. Observe this
diagram, where L indicates luma sampling points and C chroma.
</p>
<table style="text-align: center; width: 100%;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
    </tr>
    <tr>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
    </tr>
    <tr>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
    </tr>
    <tr>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
    </tr>
    <tr>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
    </tr>
    <tr>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
    </tr>
  </tbody>
</table>
<p>
As you can see, rows and columns of the image naturally come in pairs.
Thus your crop offsets and dimensions MUST be even numbers. If they
are not, the chroma will no longer line up correctly with the luma. In
theory, it's possible to crop with odd offsets, but it requires
resampling the chroma which is potentially a lossy operation and not
supported by the crop filter.
</p>
<p>
Further, interlaced video is sampled as follows:</p>
<table style="text-align: center; width: 100%;" border="1">
  <tbody>
    <tr>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
    </tr>
    <tr>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
    </tr>
    <tr>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
    </tr>
    <tr>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
    </tr>
    <tr>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
    </tr>
    <tr>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
    </tr>
    <tr>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
    </tr>
    <tr>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
    </tr>
    <tr>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
    </tr>
    <tr>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
      <td colspan="2" rowspan="1">C</td>
    </tr>
    <tr>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td><br>
      </td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
      <td>L</td>
    </tr>
  </tbody>
</table>
<p>
As you can see, the pattern does not repeat until after 4 lines. So
for interlaced video, your y-offset and height for cropping must be
multiples of 4.
</p>
<p>
So how do you determine a crop rectangle to begin with? Sometimes you
can guess, but the cropdetect filter in MPlayer can make it easy. Run
MPlayer with -vf cropdetect and it will print out the crop settings to
remove the borders. You should let the movie run long enough that the
whole picture area is used, in order to get accurate crop values.
Then, test the values you get with MPlayer, using the command line
cropdetect printed, and adjust the rectangle as needed. The rectangle
filter can help by allowing you to interactively position the crop
rectangle over your movie. Remember to follow the above divisibility
guidelines so that you do not misalign the chroma planes.
</p>
<p>
If you will be scaling your movie, it's usually best to crop only the
black borders and noise, then scale so that the resulting dimensions
are multiples of 16. This can slightly distort the aspect ratio of
your movie, but in practice the error cannot be seen. It's certainly
much less visible than the MPEG artifacts you will see from failing to
crop & scale well.
</p>
<p>
In certain cases, scaling may be undesirable. Scaling in the vertical
direction is difficult with interlaced video, and if you wish to
preserve the interlacing, you should usually refrain from scaling. If
you will not be scaling but you still want to use multiple-of-16
dimensions, you will have to overcrop. Do not undercrop, since black
borders are very bad for encoding!
</p>
</body>
</html>