10l: section "13.3.6. Example" talks about a telecined source, but the example given...
[mplayer/greg.git] / DOCS / xml / en / encoding-guide.xml
blob95b94ff0a2c66a44f30a526ebecbc59311bfc436
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <!-- $Revision$ -->
3 <chapter id="encoding-guide">
4 <title>Encoding with <application>MEncoder</application></title>
6 <sect1 id="menc-feat-dvd-mpeg4">
7 <title>Making a high quality MPEG-4 (&quot;DivX&quot;) rip of a DVD movie</title>
9 <para>
10   One frequently asked question is "How do I make the highest quality rip for
11   a given size?". Another question is "How do I make the highest quality DVD
12   rip possible? I do not care about file size, I just want the best quality."
13 </para>
15 <para>
16   The latter question is perhaps at least somewhat wrongly posed. After all, if
17   you do not care about file size, why not simply copy the entire MPEG-2 video
18   stream from the the DVD? Sure, your AVI will end up being 5GB, give
19   or take, but if you want the best quality and do not care about size,
20   this is certainly your best option.
21 </para>
23 <para>
24   In fact, the reason you want to transcode a DVD into MPEG-4 is
25   specifically because you <emphasis role="bold">do</emphasis> care about
26   file size.
27 </para>
29 <para>
30   It is difficult to offer a cookbook recipe on how to create a very high
31   quality DVD rip. There are several factors to consider, and you should
32   understand these details or else you are likely to end up disappointed
33   with your results. Below we will investigate some of these issues, and
34   then have a look at an example. We assume you are using
35   <systemitem class="library">libavcodec</systemitem> to encode the video,
36   although the theory applies to other codecs as well.
37 </para>
39 <para>
40   If this seems to be too much for you, you should probably use one of the
41   many fine frontends that are listed in the
42   <ulink url="http://mplayerhq.hu/homepage/design7/projects.html#mencoder_frontends">MEncoder section</ulink>
43   of our related projects page.
44   That way, you should be able to achieve high quality rips without too much
45   thinking, because most of those tools are designed to take clever decisions
46   for you.
47 </para>
49 <sect2 id="menc-feat-dvd-mpeg4-preparing-encode">
50 <title>Preparing to encode: Identifying source material and framerate</title>
51 <para>
52   Before you even think about encoding a movie, you need to take
53   several preliminary steps.
54 </para>
56 <para>
57   The first and most important step before you encode should be
58   determining what type of content you are dealing with.
59   If your source material comes from DVD or broadcast/cable/satellite
60   TV, it will be stored in one of two formats: NTSC for North
61   America and Japan, PAL for Europe, etc.
62   It is important to realize, however, that this is just the formatting for
63   presentation on a television, and often does
64   <emphasis role="bold">not</emphasis> correspond to the
65   original format of the movie.
66   Experience shows that NTSC material is a lot more difficult to encode,
67   because there more elements to identify in the source.
68   In order to produce a suitable encode, you need to know the original
69   format.
70   Failure to take this into account will result in various flaws in your
71   encode, including ugly combing (interlacing) artifacts and duplicated
72   or even lost frames.
73   Besides being ugly, the artifacts also harm coding efficiency:
74   You will get worse quality per unit bitrate.
75 </para>
77 <sect3 id="menc-feat-dvd-mpeg4-preparing-encode-fps">
78 <title>Identifying source framerate</title>
79 <para>
80   Here is a list of common types of source material, where you are
81   likely to find them, and their properties:
82 </para>
83 <itemizedlist>
84 <listitem><para>
85   <emphasis role="bold">Standard Film</emphasis>: Produced for
86   theatrical display at 24fps.
87 </para></listitem>
88 <listitem><para>
89   <emphasis role="bold">PAL video</emphasis>: Recorded with a PAL
90   video camera at 50 fields per second.
91   A field consists of just the odd- or even-numbered lines of a
92   frame.
93   Television was designed to refresh these in alternation as a
94   cheap form of analog compression.
95   The human eye supposedly compensates for this, but once you
96   understand interlacing you will learn to see it on TV too and
97   never enjoy TV again.
98   Two fields do <emphasis role="bold">not</emphasis> make a
99   complete frame, because they are captured 1/50 of a second apart
100   in time, and thus they do not line up unless there is no motion.
101 </para></listitem>
102 <listitem><para>
103   <emphasis role="bold">NTSC Video</emphasis>: Recorded with an
104   NTSC video camera at 60000/1001 fields per second, or 60 fields per
105   second in the pre-color era.
106   Otherwise similar to PAL.
107 </para></listitem>
108 <listitem><para>
109   <emphasis role="bold">Animation</emphasis>: Usually drawn at
110   24fps, but also comes in mixed-framerate varieties.
111 </para></listitem>
112 <listitem><para>
113   <emphasis role="bold">Computer Graphics (CG)</emphasis>: Can be
114   any framerate, but some are more common than others; 24 and
115   30 frames per second are typical for NTSC, and 25fps is typical
116   for PAL.
117 </para></listitem>
118 <listitem><para>
119   <emphasis role="bold">Old Film</emphasis>: Various lower
120   framerates.
121 </para></listitem>
122 </itemizedlist>
123 </sect3>
125 <sect3 id="menc-feat-dvd-mpeg4-preparing-encode-material">
126 <title>Identifying source material</title>
127 <para>
128   Movies consisting of frames are referred to as progressive,
129   while those consisting of independent fields are called
130   either interlaced or video - though this latter term is
131   ambiguous.
132 </para>
133 <para>
134   To further complicate matters, some movies will be a mix of
135   several of the above.
136 </para>
137 <para>
138   The most important distinction to make between all of these
139   formats is that some are frame-based, while others are
140   field-based.
141   <emphasis role="bold">Whenever</emphasis> a movie is prepared
142   for display on television (including DVD), it is converted to a
143   field-based format.
144   The various methods by which this can be done are collectively
145   referred to as "pulldown", of which the infamous NTSC
146   "3:2 telecine" is one variety.
147   Unless the original material was also field-based (and the same
148   fieldrate), you are getting the movie in a format other than the
149   original.
150 </para>
152 <itemizedlist>
153 <title>There are several common types of pulldown:</title>
154 <listitem><para>
155   <emphasis role="bold">PAL 2:2 pulldown</emphasis>: The nicest of
156   them all.
157   Each frame is shown for the duration of two fields, by extracting the
158   even and odd lines and showing them in alternation.
159   If the original material is 24fps, this process speeds up the
160   movie by 4%.
161 </para></listitem>
162 <listitem><para>
163   <emphasis role="bold">PAL 2:2:2:2:2:2:2:2:2:2:2:3 pulldown</emphasis>:
164   Every 12th frame is shown for the duration of three fields, instead of
165   just two.
166   This avoids the 4% speedup issue, but makes the process much
167   more difficult to reverse.
168   It is usually seen in musical productions where adjusting the
169   speed by 4% would seriously damage the musical score.
170 </para></listitem>
171 <listitem><para>
172   <emphasis role="bold">NTSC 3:2 telecine</emphasis>: Frames are
173   shown alternately for the duration of 3 fields or 2 fields.
174   This gives a fieldrate 2.5 times the original framerate.
175   The result is also slowed down very slightly from 60 fields per
176   second to 60000/1001 fields per second to maintain NTSC fieldrate.
177 </para></listitem>
178 <listitem><para>
179   <emphasis role="bold">NTSC 2:2 pulldown</emphasis>: Used for
180   showing 30fps material on NTSC.
181   Nice, just like 2:2 PAL pulldown.
182 </para></listitem>
183 </itemizedlist>
185 <para>
186   There are also methods for converting between NTSC and PAL video,
187   but such topics are beyond the scope of this guide.
188   If you encounter such a movie and want to encode it, your best
189   bet is to find a copy in the original format.
190   Conversion between these two formats is highly destructive and
191   cannot be reversed cleanly, so your encode will greatly suffer
192   if it is made from a converted source.
193 </para>
194 <para>
195   When video is stored on DVD, consecutive pairs of fields are
196   grouped as a frame, even though they are not intended to be shown
197   at the same moment in time.
198   The MPEG-2 standard used on DVD and digital TV provides a
199   way both to encode the original progressive frames and to store
200   the number of fields for which a frame should be shown in the
201   header of that frame.
202   If this method has been used, the movie will often be described
203   as "soft-telecined", since the process only directs the
204   DVD player to apply pulldown to the movie rather than altering
205   the movie itself.
206   This case is highly preferable since it can easily be reversed
207   (actually ignored) by the encoder, and since it preserves maximal
208   quality.
209   However, many DVD and broadcast production studios do not use
210   proper encoding techniques but instead produce movies with
211   "hard telecine", where fields are actually duplicated in the
212   encoded MPEG-2.
213 </para>
214 <para>
215   The procedures for dealing with these cases will be covered
216   <link linkend="menc-feat-telecine">later in this guide</link>.
217   For now, we leave you with some guides to identifying which type
218   of material you are dealing with:
219 </para>
221 <itemizedlist>
222 <title>NTSC regions:</title>
223 <listitem><para>
224   If <application>MPlayer</application> prints that the framerate
225   has changed to 24000/1001 when watching your movie, and never changes
226   back, it is almost certainly progressive content that has been
227   "soft telecined".
228 </para></listitem>
229 <listitem><para>
230   If <application>MPlayer</application> shows the framerate
231   switching back and forth between 24000/1001 and 30000/1001, and you see
232   "combing" at times, then there are several possibilities.
233   The 24000/1001 fps segments are almost certainly progressive
234   content, "soft telecined", but the 30000/1001 fps parts could be
235   either hard-telecined 24000/1001 fps content or 60000/1001 fields per second NTSC video.
236   Use the same guidelines as the following two cases to determine
237   which.
238 </para></listitem>
239 <listitem><para>
240   If <application>MPlayer</application> never shows the framerate
241   changing, and every single frame with motion appears combed, your
242   movie is NTSC video at 60000/1001 fields per second.
243 </para></listitem>
244 <listitem><para>
245   If <application>MPlayer</application> never shows the framerate
246   changing, and two frames out of every five appear combed, your
247   movie is "hard telecined" 24000/1001fps content.
248 </para></listitem>
249 </itemizedlist>
251 <itemizedlist>
252 <title>PAL regions:</title>
253 <listitem><para>
254   If you never see any combing, your movie is 2:2 pulldown.
255 </para></listitem>
256 <listitem><para>
257   If you see combing alternating in and out every half second,
258   then your movie is 2:2:2:2:2:2:2:2:2:2:2:3 pulldown.
259 </para></listitem>
260 <listitem><para>
261   If you always see combing during motion, then your movie is PAL
262   video at 50 fields per second.
263 </para></listitem>
264 </itemizedlist>
266 <note><title>Hint:</title>
267 <para>
268   <application>MPlayer</application> can slow down movie playback
269   with the -speed option or play it frame-by-frame.
270   Try using <option>-speed</option> 0.2 to watch the movie very
271   slowly or press the "<keycap>.</keycap>" key repeatedly to play one frame at a time
272   and identify the pattern, if you cannot see it at full speed.
273 </para>
274 </note>
275 </sect3>
276 </sect2>
278 <sect2 id="menc-feat-dvd-mpeg4-2pass">
279 <title>Constant quantizer vs. multipass</title>
281 <para>
282   It is possible to encode your movie at a wide range of qualities.
283   With modern video encoders and a bit of pre-codec compression
284   (downscaling and denoising), it is possible to achieve very good
285   quality at 700 MB, for a 90-110 minute widescreen movie.
286   Furthermore, all but the longest movies can be encoded with near-perfect
287   quality at 1400 MB.
288 </para>
290 <para>
291   There are three approaches to encoding the video: constant bitrate
292   (CBR), constant quantizer, and multipass (ABR, or average bitrate).
293 </para>
295 <para>
296   The complexity of the frames of a movie, and thus the number of bits
297   required to compress them, can vary greatly from one scene to another.
298   Modern video encoders can adjust to these needs as they go and vary
299   the bitrate.
300   In simple modes such as CBR, however, the encoders do not know the
301   bitrate needs of future scenes and so cannot exceed the requested
302   average bitrate for long stretches of time.
303   More advanced modes, such as multipass encode, can take into account
304   the statistics from previous passes; this fixes the problem mentioned
305   above.
306 </para>
308 <note><title>Note:</title>
309 <para>
310   Most codecs which support ABR encode only support two pass encode
311   while some others such as <systemitem class="library">x264</systemitem>,
312   <systemitem class="library">XviD</systemitem>
313   and <systemitem class="library">libavcodec</systemitem> support
314   multipass, which slightly improves quality at each pass,
315   yet this improvement is no longer measurable nor noticeable after the
316   4th or so pass.
317   Therefore, in this section, two pass and multipass will be used
318   interchangeably.
319 </para>
320 </note>
322 <para>
323   In each of these modes, the video codec (such as
324   <systemitem class="library">libavcodec</systemitem>)
325   breaks the video frame into 16x16 pixel macroblocks and then applies a
326   quantizer to each macroblock. The lower the quantizer, the better the
327   quality and higher the bitrate.
328   The method the movie encoder uses to determine
329   which quantizer to use for a given macroblock varies and is highly
330   tunable. (This is an extreme over-simplification of the actual
331   process, but the basic concept is useful to understand.)
332 </para>
334 <para>
335   When you specify a constant bitrate, the video codec will encode the video,
336   discarding
337   detail as much as necessary and as little as possible in order to remain
338   lower than the given bitrate. If you truly do not care about file size,
339   you could as well use CBR and specify a bitrate of infinity. (In
340   practice, this means a value high enough so that it poses no limit, like
341   10000Kbit.) With no real restriction on bitrate, the result is that
342   the codec will use the lowest
343   possible quantizer for each macroblock (as specified by
344   <option>vqmin</option> for 
345   <systemitem class="library">libavcodec</systemitem>, which is 2 by default).
346   As soon as you specify a
347   low enough bitrate that the codec
348   is forced to use a higher quantizer, then you are almost certainly ruining
349   the quality of your video.
350   In order to avoid that, you should probably downscale your video, according
351   to the method described later on in this guide.
352   In general, you should avoid CBR altogether if you care about quality.
353 </para>
355 <para>
356   With constant quantizer, the codec uses the same quantizer, as
357   specified by the <option>vqscale</option> option (for
358   <systemitem class="library">libavcodec</systemitem>), on every macroblock.
359   If you want the highest quality rip possible, again ignoring bitrate,
360   you can use <option>vqscale=2</option>.
361   This will yield the same bitrate and PSNR (peak signal-to-noise ratio)
362   as CBR with
363   <option>vbitrate</option>=infinity and the default <option>vqmin</option>
364   of 2.
365 </para>
367 <para>
368   The problem with constant quantizing is that it uses the given quantizer
369   whether the macroblock needs it or not. That is, it might be possible
370   to use a higher quantizer on a macroblock without sacrificing visual
371   quality. Why waste the bits on an unnecessarily low quantizer? Your
372   CPU has as many cycles as there is time, but there is only so many bits
373   on your hard disk.
374 </para>
376 <para>
377   With a two pass encode, the first pass will rip the movie as though it
378   were CBR, but it will keep a log of properties for each frame. This
379   data is then used during the second pass in order to make intelligent
380   decisions about which quantizers to use. During fast action or low
381   detail scenes, higher quantizers will likely be used, and during
382   slow moving or high detail scenes, lower quantizers will be used.
383 </para>
385 <para>
386   If you use <option>vqscale=2</option>, then you are wasting bits. If you
387   use <option>vqscale=3</option>, then you are not getting the highest
388   quality rip. Suppose you rip a DVD at <option>vqscale=3</option>, and
389   the result is 1800Kbit. If you do a two pass encode with
390   <option>vbitrate=1800</option>, the resulting video will have <emphasis
391   role="bold">higher quality</emphasis> for the
392   <emphasis role="bold">same bitrate</emphasis>.
393 </para>
395 <para>
396   Since you are now convinced that two pass is the way to go, the real
397   question now is what bitrate to use? The answer is that there is no
398   single answer. Ideally you want to choose a bitrate that yields the
399   best balance between quality and file size. This is going to vary
400   depending on the source video.
401 </para>
403 <para>
404   If size does not matter, a good starting point for a very high quality
405   rip is about 2000Kbit plus or minus 200Kbit.
406   For fast action or high detail source video, or if you just have a very
407   critical eye, you might decide on 2400 or 2600.
408   For some DVDs, you might not notice a difference at 1400Kbit. It is a
409   good idea to experiment with scenes at different bitrates to get a feel.
410 </para>
412 <para>
413   If you aim at a certain size, you will have to somehow calculate the bitrate.
414   But before that, you need to know how much space you should reserve for the
415   audio track(s), so you should <link linkend="menc-feat-dvd-mpeg4-audio">rip
416   those</link> first.
417   You can compute the bitrate with the following equation:
418   <systemitem>bitrate = (target_size_in_Mbytes - sound_size_in_Mbytes) *
419   1024 * 1024 / length_in_secs * 8 / 1000</systemitem>
420   For instance, to squeeze a two-hour movie onto a 702MB CD, with 60MB
421   of audio track, the video bitrate will have to be:
422   <systemitem>(702 - 60) * 1024 * 1024 / (120*60) * 8 / 1000
423   = 740kbps</systemitem>
424 </para>
426 </sect2>
429 <sect2 id="menc-feat-dvd-mpeg4-constraints">
430 <title>Constraints for efficient encoding</title>
432 <para>
433   Due to the nature of MPEG-type compression, there are various
434   constraints you should follow for maximal quality.
435   MPEG splits the video up into 16x16 squares called macroblocks,
436   each composed of 4 8x8 blocks of luma (intensity) information and two
437   half-resolution 8x8 chroma (color) blocks (one for red-cyan axis and
438   the other for the blue-yellow axis).
439   Even if your movie width and height are not multiples of 16, the
440   encoder will use enough 16x16 macroblocks to cover the whole picture
441   area, and the extra space will go to waste.
442   So in the interests of maximizing quality at a fixed filesize, it is
443   a bad idea to use dimensions that are not multiples of 16.
444 </para>
446 <para>
447   Most DVDs also have some degree of black borders at the edges. Leaving
448   these in place can hurt quality in several ways.
449 </para>
451 <orderedlist>
452 <listitem>
453 <para>
454   MPEG-type compression is also highly dependent on frequency domain
455   transformations, in particular the Discrete Cosine Transform (DCT),
456   which is similar to the Fourier transform. This sort of encoding is
457   efficient for representing patterns and smooth transitions, but it
458   has a hard time with sharp edges. In order to encode them it must
459   use many more bits, or else an artifact known as ringing will
460   appear.
461 </para>
463 <para>
464   The frequency transform (DCT) takes place separately on each
465   macroblock (actually each block), so this problem only applies when
466   the sharp edge is inside a block. If your black borders begin
467   exactly at multiple-of-16 pixel boundaries, this is not a problem.
468   However, the black borders on DVDs rarely come nicely aligned, so
469   in practice you will always need to crop to avoid this penalty.
470 </para>
471 </listitem>
472 </orderedlist>
474 <para>
475   In addition to frequency domain transforms, MPEG-type compression uses
476   motion vectors to represent the change from one frame to the next.
477   Motion vectors naturally work much less efficiently for new content
478   coming in from the edges of the picture, because it is not present in
479   the previous frame. As long as the picture extends all the way to the
480   edge of the encoded region, motion vectors have no problem with
481   content moving out the edges of the picture. However, in the presence
482   of black borders, there can be trouble:
483 </para>
485 <orderedlist continuation="continues">
486 <listitem>
487 <para>
488   For each macroblock, MPEG-type compression stores a vector
489   identifying which part of the previous frame should be copied into
490   this macroblock as a base for predicting the next frame. Only the
491   remaining differences need to be encoded. If a macroblock spans the
492   edge of the picture and contains part of the black border, then
493   motion vectors from other parts of the picture will overwrite the
494   black border. This means that lots of bits must be spent either
495   re-blackening the border that was overwritten, or (more likely) a
496   motion vector will not be used at all and all the changes in this
497   macroblock will have to be coded explicitly. Either way, encoding
498   efficiency is greatly reduced.
499 </para>
501 <para>
502   Again, this problem only applies if black borders do not line up on
503   multiple-of-16 boundaries.
504 </para>
505 </listitem>
507 <listitem>
508 <para>
509   Finally, suppose we have a macroblock in the interior of the
510   picture, and an object is moving into this block from near the edge
511   of the image. MPEG-type coding cannot say "copy the part that is
512   inside the picture but not the black border." So the black border
513   will get copied inside too, and lots of bits will have to be spent
514   encoding the part of the picture that is supposed to be there.
515 </para>
517 <para>
518   If the picture runs all the way to the edge of the encoded area,
519   MPEG has special optimizations to repeatedly copy the pixels at the
520   edge of the picture when a motion vector comes from outside the
521   encoded area. This feature becomes useless when the movie has black
522   borders. Unlike problems 1 and 2, aligning the borders at multiples
523   of 16 does not help here.
524 </para>
525 </listitem>
527 <listitem>
528 <para>
529   Despite the borders being entirely black and never changing, there
530   is at least a minimal amount of overhead involved in having more
531   macroblocks.
532 </para>
533 </listitem>
534 </orderedlist>
536 <para>
537   For all of these reasons, it is recommended to fully crop black
538   borders. Further, if there is an area of noise/distortion at the edge
539   of the picture, cropping this will improve encoding efficiency as
540   well. Videophile purists who want to preserve the original as close as
541   possible may object to this cropping, but unless you plan to encode at
542   constant quantizer, the quality you gain from cropping will
543   considerably exceed the amount of information lost at the edges.
544 </para>
545 </sect2>
548 <sect2 id="menc-feat-dvd-mpeg4-crop">
549 <title>Cropping and Scaling</title>
551 <para>
552   Recall from the previous section that the final picture size you
553   encode should be a multiple of 16 (in both width and height).
554   This can be achieved by cropping, scaling, or a combination of both.
555 </para>
557 <para>
558   When cropping, there are a few guidelines that must be followed to
559   avoid damaging your movie.
560   The normal YUV format, 4:2:0, stores chroma (color) information
561   subsampled, i.e. chroma is only sampled half as often in each
562   direction as luma (intensity) information.
563   Observe this diagram, where L indicates luma sampling points and C
564   chroma.
565 </para>
567 <informaltable>
568 <?dbhtml table-width="40%" ?>
569 <?dbfo table-width="40%" ?>
570 <tgroup cols="8" align="center">
571 <colspec colnum="1" colname="col1"/>
572 <colspec colnum="2" colname="col2"/>
573 <colspec colnum="3" colname="col3"/>
574 <colspec colnum="4" colname="col4"/>
575 <colspec colnum="5" colname="col5"/>
576 <colspec colnum="6" colname="col6"/>
577 <colspec colnum="7" colname="col7"/>
578 <colspec colnum="8" colname="col8"/>
579 <spanspec spanname="spa1-2" namest="col1" nameend="col2"/>
580 <spanspec spanname="spa3-4" namest="col3" nameend="col4"/>
581 <spanspec spanname="spa5-6" namest="col5" nameend="col6"/>
582 <spanspec spanname="spa7-8" namest="col7" nameend="col8"/>
583   <tbody>
584     <row>
585       <entry>L</entry>
586       <entry>L</entry>
587       <entry>L</entry>
588       <entry>L</entry>
589       <entry>L</entry>
590       <entry>L</entry>
591       <entry>L</entry>
592       <entry>L</entry>
593     </row>
594     <row>
595       <entry spanname="spa1-2">C</entry>
596       <entry spanname="spa3-4">C</entry>
597       <entry spanname="spa5-6">C</entry>
598       <entry spanname="spa7-8">C</entry>
599     </row>
600     <row>
601       <entry>L</entry>
602       <entry>L</entry>
603       <entry>L</entry>
604       <entry>L</entry>
605       <entry>L</entry>
606       <entry>L</entry>
607       <entry>L</entry>
608       <entry>L</entry>
609     </row>
610     <row>
611       <entry>L</entry>
612       <entry>L</entry>
613       <entry>L</entry>
614       <entry>L</entry>
615       <entry>L</entry>
616       <entry>L</entry>
617       <entry>L</entry>
618       <entry>L</entry>
619     </row>
620     <row>
621       <entry spanname="spa1-2">C</entry>
622       <entry spanname="spa3-4">C</entry>
623       <entry spanname="spa5-6">C</entry>
624       <entry spanname="spa7-8">C</entry>
625     </row>
626     <row>
627       <entry>L</entry>
628       <entry>L</entry>
629       <entry>L</entry>
630       <entry>L</entry>
631       <entry>L</entry>
632       <entry>L</entry>
633       <entry>L</entry>
634       <entry>L</entry>
635     </row>
636   </tbody>
637 </tgroup>
638 </informaltable>
640 <para>
641   As you can see, rows and columns of the image naturally come in pairs.
642   Thus your crop offsets and dimensions <emphasis>must</emphasis> be
643   even numbers.
644   If they are not, the chroma will no longer line up correctly with the
645   luma.
646   In theory, it is possible to crop with odd offsets, but it requires
647   resampling the chroma which is potentially a lossy operation and not
648   supported by the crop filter.
649 </para>
651 <para>
652   Further, interlaced video is sampled as follows:
653 </para>
655 <informaltable>
656 <?dbhtml table-width="80%" ?>
657 <?dbfo table-width="80%" ?>
658 <tgroup cols="16" align="center">
659 <colspec colnum="1"  colname="col1"/>
660 <colspec colnum="2"  colname="col2"/>
661 <colspec colnum="3"  colname="col3"/>
662 <colspec colnum="4"  colname="col4"/>
663 <colspec colnum="5"  colname="col5"/>
664 <colspec colnum="6"  colname="col6"/>
665 <colspec colnum="7"  colname="col7"/>
666 <colspec colnum="8"  colname="col8"/>
667 <colspec colnum="9"  colname="col9"/>
668 <colspec colnum="10" colname="col10"/>
669 <colspec colnum="11" colname="col11"/>
670 <colspec colnum="12" colname="col12"/>
671 <colspec colnum="13" colname="col13"/>
672 <colspec colnum="14" colname="col14"/>
673 <colspec colnum="15" colname="col15"/>
674 <colspec colnum="16" colname="col16"/>
675 <spanspec spanname="spa1-2"   namest="col1" nameend="col2"/>
676 <spanspec spanname="spa3-4"   namest="col3" nameend="col4"/>
677 <spanspec spanname="spa5-6"   namest="col5" nameend="col6"/>
678 <spanspec spanname="spa7-8"   namest="col7" nameend="col8"/>
679 <spanspec spanname="spa9-10"  namest="col9" nameend="col10"/>
680 <spanspec spanname="spa11-12" namest="col11" nameend="col12"/>
681 <spanspec spanname="spa13-14" namest="col13" nameend="col14"/>
682 <spanspec spanname="spa15-16" namest="col15" nameend="col16"/>
683   <tbody>
684     <row>
685       <entry namest="col1" nameend="col8">Top field</entry>
686       <entry namest="col9" nameend="col16">Bottom field</entry>
687     </row>
688     <row>
689       <entry>L</entry>
690       <entry>L</entry>
691       <entry>L</entry>
692       <entry>L</entry>
693       <entry>L</entry>
694       <entry>L</entry>
695       <entry>L</entry>
696       <entry>L</entry>
697       <entry></entry>
698       <entry></entry>
699       <entry></entry>
700       <entry></entry>
701       <entry></entry>
702       <entry></entry>
703       <entry></entry>
704       <entry></entry>
705     </row>
706     <row>
707       <entry spanname="spa1-2">C</entry>
708       <entry spanname="spa3-4">C</entry>
709       <entry spanname="spa5-6">C</entry>
710       <entry spanname="spa7-8">C</entry>
711       <entry></entry>
712       <entry></entry>
713       <entry></entry>
714       <entry></entry>
715       <entry></entry>
716       <entry></entry>
717       <entry></entry>
718       <entry></entry>
719     </row>
720     <row>
721       <entry></entry>
722       <entry></entry>
723       <entry></entry>
724       <entry></entry>
725       <entry></entry>
726       <entry></entry>
727       <entry></entry>
728       <entry></entry>
729       <entry>L</entry>
730       <entry>L</entry>
731       <entry>L</entry>
732       <entry>L</entry>
733       <entry>L</entry>
734       <entry>L</entry>
735       <entry>L</entry>
736       <entry>L</entry>
737     </row>
738     <row>
739       <entry>L</entry>
740       <entry>L</entry>
741       <entry>L</entry>
742       <entry>L</entry>
743       <entry>L</entry>
744       <entry>L</entry>
745       <entry>L</entry>
746       <entry>L</entry>
747       <entry></entry>
748       <entry></entry>
749       <entry></entry>
750       <entry></entry>
751       <entry></entry>
752       <entry></entry>
753       <entry></entry>
754       <entry></entry>
755     </row>
756     <row>
757       <entry></entry>
758       <entry></entry>
759       <entry></entry>
760       <entry></entry>
761       <entry></entry>
762       <entry></entry>
763       <entry></entry>
764       <entry></entry>
765       <entry spanname="spa9-10">C</entry>
766       <entry spanname="spa11-12">C</entry>
767       <entry spanname="spa13-14">C</entry>
768       <entry spanname="spa15-16">C</entry>
769     </row>
770     <row>
771       <entry></entry>
772       <entry></entry>
773       <entry></entry>
774       <entry></entry>
775       <entry></entry>
776       <entry></entry>
777       <entry></entry>
778       <entry></entry>
779       <entry>L</entry>
780       <entry>L</entry>
781       <entry>L</entry>
782       <entry>L</entry>
783       <entry>L</entry>
784       <entry>L</entry>
785       <entry>L</entry>
786       <entry>L</entry>
787     </row>
788     <row>
789       <entry>L</entry>
790       <entry>L</entry>
791       <entry>L</entry>
792       <entry>L</entry>
793       <entry>L</entry>
794       <entry>L</entry>
795       <entry>L</entry>
796       <entry>L</entry>
797       <entry></entry>
798       <entry></entry>
799       <entry></entry>
800       <entry></entry>
801       <entry></entry>
802       <entry></entry>
803       <entry></entry>
804       <entry></entry>
805     </row>
806     <row>
807       <entry spanname="spa1-2">C</entry>
808       <entry spanname="spa3-4">C</entry>
809       <entry spanname="spa5-6">C</entry>
810       <entry spanname="spa7-8">C</entry>
811       <entry></entry>
812       <entry></entry>
813       <entry></entry>
814       <entry></entry>
815       <entry></entry>
816       <entry></entry>
817       <entry></entry>
818       <entry></entry>
819     </row>
820     <row>
821       <entry></entry>
822       <entry></entry>
823       <entry></entry>
824       <entry></entry>
825       <entry></entry>
826       <entry></entry>
827       <entry></entry>
828       <entry></entry>
829       <entry>L</entry>
830       <entry>L</entry>
831       <entry>L</entry>
832       <entry>L</entry>
833       <entry>L</entry>
834       <entry>L</entry>
835       <entry>L</entry>
836       <entry>L</entry>
837     </row>
838     <row>
839       <entry>L</entry>
840       <entry>L</entry>
841       <entry>L</entry>
842       <entry>L</entry>
843       <entry>L</entry>
844       <entry>L</entry>
845       <entry>L</entry>
846       <entry>L</entry>
847       <entry></entry>
848       <entry></entry>
849       <entry></entry>
850       <entry></entry>
851       <entry></entry>
852       <entry></entry>
853       <entry></entry>
854       <entry></entry>
855     </row>
856     <row>
857       <entry></entry>
858       <entry></entry>
859       <entry></entry>
860       <entry></entry>
861       <entry></entry>
862       <entry></entry>
863       <entry></entry>
864       <entry></entry>
865       <entry spanname="spa9-10">C</entry>
866       <entry spanname="spa11-12">C</entry>
867       <entry spanname="spa13-14">C</entry>
868       <entry spanname="spa15-16">C</entry>
869     </row>
870     <row>
871       <entry></entry>
872       <entry></entry>
873       <entry></entry>
874       <entry></entry>
875       <entry></entry>
876       <entry></entry>
877       <entry></entry>
878       <entry></entry>
879       <entry>L</entry>
880       <entry>L</entry>
881       <entry>L</entry>
882       <entry>L</entry>
883       <entry>L</entry>
884       <entry>L</entry>
885       <entry>L</entry>
886       <entry>L</entry>
887     </row>
888   </tbody>
889 </tgroup>
890 </informaltable>
892 <para>
893   As you can see, the pattern does not repeat until after 4 lines.
894   So for interlaced video, your y-offset and height for cropping must
895   be multiples of 4.
896 </para>
898 <para>
899   Native DVD resolution is 720x480 for NTSC, and 720x576 for PAL, but
900   there is an aspect flag that specifies whether it is full-screen (4:3) or
901   wide-screen (16:9). Many (if not most) widescreen DVDs are not strictly
902   16:9, and will be either 1.85:1 or 2.35:1 (cinescope). This means that
903   there will be black bands in the video that will need to be cropped out.
904 </para>
906 <para>
907   <application>MPlayer</application> provides a crop detection filter that
908   will determine the crop rectangle (<option>-vf cropdetect</option>).
909   Run <application>MPlayer</application> with
910   <option>-vf cropdetect</option> and it will print out the crop
911   settings to remove the borders.
912   You should let the movie run long enough that the whole picture
913   area is used, in order to get accurate crop values.
914 </para>
916 <para>
917   Then, test the values you get with <application>MPlayer</application>,
918   using the command line which was printed by
919   <option>cropdetect</option>, and adjust the rectangle as needed.
920   The <option>rectangle</option> filter can help by allowing you to
921   interactively position the crop rectangle over your movie.
922   Remember to follow the above divisibility guidelines so that you
923   do not misalign the chroma planes.
924 </para>
926 <para>
927   In certain cases, scaling may be undesirable.
928   Scaling in the vertical direction is difficult with interlaced
929   video, and if you wish to preserve the interlacing, you should
930   usually refrain from scaling.
931   If you will not be scaling but you still want to use multiple-of-16
932   dimensions, you will have to overcrop.
933   Do not undercrop, since black borders are very bad for encoding!
934 </para>
936 <para>
937   Because MPEG-4 uses 16x16 macroblocks, you will want to make sure that each
938   dimension of the video you are encoding is a multiple of 16 or else you
939   will be degrading quality, especially at lower bitrates. You can do this
940   by rounding the width and height of the crop rectangle down to the nearest
941   multiple of 16.
942   As stated earlier, when cropping, you will want to increase the Y offset by
943   half the difference of the old and the new height so that the resulting
944   video is taken from the center of the frame. And because of the way DVD
945   video is sampled, make sure the offset is an even number. (In fact, as a
946   rule, never use odd values for any parameter when you are cropping and
947   scaling video.) If you are not comfortable throwing a few extra pixels
948   away, you might prefer instead to scale the video instead. We will look
949   at this in our example below.
950   You can actually let the <option>cropdetect</option> filter do all of the
951   above for you, as it has an optional <option>round</option> parameter that
952   is equal to 16 by default.
953 </para>
955 <para>
956   Also, be careful about "half black" pixels at the edges. Make sure you
957   crop these out too, or else you will be wasting bits there that
958   are better spent elsewhere.
959 </para>
961 <para>
962   After all is said and done, you will probably end up with video whose pixels
963   are not quite 1.85:1 or 2.35:1, but rather something close to that. You
964   could calculate the new aspect ratio manually, but
965   <application>MEncoder</application> offers an option for <systemitem
966   class="library">libavcodec</systemitem> called <option>autoaspect</option>
967   that will do this for you. Absolutely do not scale this video up in order to
968   square the pixels unless you like to waste your hard disk space. Scaling
969   should be done on playback, and the player will use the aspect stored in
970   the AVI to determine the correct resolution.
971   Unfortunately, not all players enforce this auto-scaling information,
972   therefore you may still want to rescale.
973 </para>
974 </sect2>
977 <sect2 id="menc-feat-dvd-mpeg4-resolution-bitrate">
978 <title>Choosing resolution and bitrate</title>
980 <para>
981   If you will not be encoding in constant quantizer mode, you need to
982   select a bitrate.
983   The concept of bitrate is quite simple.
984   It is the (average) number of bits that will be consumed to store your
985   movie, per second.
986   Normally bitrate is measured in kilobits (1000 bits) per second.
987   The size of your movie on disk is the bitrate times the length of the
988   movie in time, plus a small amount of "overhead" (see the section on
989   <link linkend="menc-feat-dvd-mpeg4-muxing-avi-limitations">the AVI container</link>
990   for instance).
991   Other parameters such as scaling, cropping, etc. will
992   <emphasis role="bold">not</emphasis> alter the file size unless you
993   change the bitrate as well!.
994 </para>
995 <para>
996   Bitrate does <emphasis role="bold">not</emphasis> scale proportionally
997   to resolution.
998   That is to say, a 320x240 file at 200 kbit/sec will not be the same
999   quality as the same movie at 640x480 and 800 kbit/sec!
1000   There are two reasons for this:
1001 <orderedlist>
1002   <listitem><para>
1003     <emphasis role="bold">Perceptual</emphasis>: You notice MPEG
1004     artifacts more if they are scaled up bigger!
1005     Artifacts appear on the scale of blocks (8x8).
1006     Your eye will not see errors in 4800 small blocks as easily as it
1007     sees errors in 1200 large blocks (assuming you will be scaling both
1008     to fullscreen).
1009   </para></listitem>
1010   <listitem><para>
1011     <emphasis role="bold">Theoretical</emphasis>: When you scale down
1012     an image but still use the same size (8x8) blocks for the frequency
1013     space transform, you move more data to the high frequency bands.
1014     Roughly speaking, each pixel contains more of the detail than it
1015     did before.
1016     So even though your scaled-down picture contains 1/4 the information
1017     in the spacial directions, it could still contain a large portion
1018     of the information in the frequency domain (assuming that the high
1019     frequencies were underutilized in the original 640x480 image).
1020   </para></listitem>
1021   </orderedlist>
1022 </para>
1023 <para>
1024   Past guides have recommended choosing a bitrate and resolution based
1025   on a "bits per pixel" approach, but this is usually not valid due to
1026   the above reasons.
1027   A better estimate seems to be that bitrates scale proportional to the
1028   square root of resolution, so that 320x240 and 400 kbit/sec would be
1029   comparable to 640x480 at 800 kbit/sec.
1030   However this has not been verified with theoretical or empirical
1031   rigor.
1032   Further, given that movies vary greatly with regard to noise, detail,
1033   degree of motion, etc., it is futile to make general recommendations
1034   for bits per length-of-diagonal (the analog of bits per pixel,
1035   using the square root).
1036 </para>
1037 <para>
1038   So far we have discussed the difficulty of choosing a bitrate and
1039   resolution.
1040 </para>
1043 <sect3 id="menc-feat-dvd-mpeg4-resolution-bitrate-compute">
1044 <title>Computing the resolution</title>
1045 <para>
1046   The following steps will guide you in computing the resolution of your
1047   encode without distorting the video too much, by taking into account several
1048   types of information about the source video.
1049   First, you should compute the encoded aspect ratio:
1050   <systemitem>ARc = (Wc x (ARa / PRdvd )) / Hc</systemitem>
1051 <itemizedlist>
1052 <title>where:</title>
1053 <listitem><para>
1054   Wc and Hc are the width and height of the cropped video,
1055 </para></listitem>
1056 <listitem><para>
1057   ARa is the displayed aspect ratio, which usually is 4/3 or 16/9,
1058 </para></listitem>
1059 <listitem><para>
1060   PRdvd is the pixel ratio of the DVD which is equal to 1.25=(720/576) for PAL
1061   DVDs and 1.5=(720/480) for NTSC DVDs,
1062 </para></listitem>
1063 </itemizedlist>
1064 </para>
1066 <para>
1067   Then, you can compute the X and Y resolution, according to a certain
1068   Compression Quality (CQ) factor:
1069   <systemitem>ResY = INT(SQRT( 1000*Bitrate/25/ARc/CQ )/16) * 16</systemitem>
1070   and
1071   <systemitem>ResX = INT( ResY * ARc / 16) * 16</systemitem>
1072 </para>
1074 <para>
1075   Okay, but what is the CQ?
1076   The CQ represents the number of bits per pixel and per frame of the encode.
1077   Roughly speaking, the greater the CQ, the less the likelihood to see
1078   encoding artifacts.
1079   However, if you have a target size for your movie (1 or 2 CDs for instance),
1080   there is a limited total number of bits that you can spend; therefore it is
1081   necessary to find a good tradeoff between compressibility and quality.
1082 </para>
1084 <para>
1085   The CQ depends on the bitrate, the video codec efficiency and the
1086   movie resolution.
1087   In order to raise the CQ, typically you would downscale the movie given that the
1088   bitrate is computed in function of the target size and the length of the
1089   movie, which are constant.
1090   With MPEG-4 ASP codecs such as <systemitem class="library">XviD</systemitem>
1091   and <systemitem class="library">libavcodec</systemitem>, a CQ below 0.18
1092   usually results in a pretty blocky picture, because there
1093   are not enough bits to code the information of each macroblock. (MPEG4, like
1094   many other codecs, groups pixels by blocks of several pixels to compress the
1095   image; if there are not enough bits, the edges of those blocks are
1096   visible.)
1097   It is therefore wise to take a CQ ranging from 0.20 to 0.22 for a 1 CD rip,
1098   and 0.26-0.28 for 2 CDs rip with standard encoding options.
1099   More advanced encoding options such as those listed here for
1100   <link linkend="menc-feat-mpeg4-lavc-example-settings"><systemitem class="library">libavcodec</systemitem></link>
1101   and
1102 <link linkend="menc-feat-xvid-example-settings"><systemitem class="library">XviD</systemitem></link>
1103   should make it possible to get the same quality with CQ ranging from
1104   0.18 to 0.20 for a 1 CD rip, and 0.24 to 0.26 for a 2 CD rip.
1105   With MPEG-4 ASP codecs such as <systemitem class="library">x264</systemitem>,
1106   you can use a CQ ranging from 0.14 to 0.16 with standard encoding options,
1107   and should be able to go as low as 0.10 to 0.12 with
1108   <link linkend="menc-feat-x264-example-settings"><systemitem class="library">x264</systemitem>'s advanced encoding settings</link>.
1109 </para>
1111 <para>
1112   Please take note that the CQ is just an indicative figure, as depending on
1113   the encoded content, a CQ of 0.18 may look just fine for a Bergman, contrary
1114   to a movie such as The Matrix, which contains many high-motion scenes.
1115   On the other hand, it is worthless to raise CQ higher than 0.30 as you would
1116   be wasting bits without any noticeable quality gain.
1117   Also note that as mentioned earlier in this guide, low resolution videos
1118   need a bigger CQ (compared to, for instance, DVD resolution) to look good.
1119 </para>
1120 </sect3>
1122 </sect2>
1124 <sect2 id="menc-feat-dvd-mpeg4-filtering">
1125 <title>Filtering</title>
1127 <para>
1128   Learning how to use <application>MEncoder</application>'s video filters
1129   is essential to producing good encodes.
1130   All video processing is performed through the filters -- cropping,
1131   scaling, color adjustment, noise removal, sharpening, deinterlacing,
1132   telecine, inverse telecine, and deblocking, just to name a few.
1133   Along with the vast number of supported input formats, the variety of
1134   filters available in <application>MEncoder</application> is one of its
1135   main advantages over other similar programs.
1136 </para>
1138 <para>
1139   Filters are loaded in a chain using the -vf option:
1141   <screen>-vf filter1=options,filter2=options,...</screen>
1143   Most filters take several numeric options separated by colons, but
1144   the syntax for options varies from filter to filter, so read the man
1145   page for details on the filters you wish to use.
1146 </para>
1148 <para>
1149   Filters operate on the video in the order they are loaded.
1150   For example, the following chain:
1152   <screen>-vf crop=688:464:12:4,scale=640:464</screen>
1154   will first crop the 688x464 region of the picture with upper-left
1155   corner at (12,4), and then scale the result down to 640x464.
1156 </para>
1158 <para>
1159   Certain filters need to be loaded at or near the beginning of the
1160   filter chain, in order to take advantage of information from the
1161   video decoder that will be lost or invalidated by other filters.
1162   The principal examples are <option>pp</option> (postprocessing, only
1163   when it is performing deblock or dering operations),
1164   <option>spp</option> (another postprocessor to remove MPEG artifacts),
1165   <option>pullup</option> (inverse telecine), and
1166   <option>softpulldown</option> (for converting soft telecine to hard
1167   telecine).
1168 </para>
1170 <para>
1171   In general, you want to do as little filtering as possible to the movie
1172   in order to remain close to the original DVD source. Cropping is often
1173   necessary (as described above), but avoid to scale the video. Although
1174   scaling down is sometimes preferred to using higher quantizers, we want
1175   to avoid both these things: remember that we decided from the start to
1176   trade bits for quality.
1177 </para>
1179 <para>
1180   Also, do not adjust gamma, contrast, brightness, etc. What looks good
1181   on your display may not look good on others. These adjustments should
1182   be done on playback only.
1183 </para>
1185 <para>
1186   One thing you might want to do, however, is pass the video through a
1187   very light denoise filter, such as <option>-vf hqdn3d=2:1:2</option>.
1188   Again, it is a matter of putting those bits to better use: why waste them
1189   encoding noise when you can just add that noise back in during playback?
1190   Increasing the parameters for <option>hqdn3d</option> will further
1191   improve compressibility, but if you increase the values too much, you
1192   risk degrading the image visibily. The suggested values above
1193   (<option>2:1:2</option>) are quite conservative; you should feel free to
1194   experiment with higher values and observe the results for yourself.
1195 </para>
1197 </sect2>
1200 <sect2 id="menc-feat-dvd-mpeg4-interlacing">
1201 <title>Interlacing and Telecine</title>
1203 <para>
1204   Almost all movies are shot at 24 fps. Because NTSC is 30000/1001 fps, some
1205   processing must be done to this 24 fps video to make it run at the correct
1206   NTSC framerate. The process is called 3:2 pulldown, commonly referred to
1207   as telecine (because pulldown is often applied during the telecine
1208   process), and, naively described, it works by slowing the film down to
1209   24000/1001 fps, and repeating every fourth frame.
1210 </para>
1212 <para>
1213   No special processing, however, is done to the video for PAL DVDs, which
1214   run at 25 fps. (Technically, PAL can be telecined, called 2:2 pulldown,
1215   but this does not become an issue in practice.) The 24 fps film is simply
1216   played back at 25 fps. The result is that the movie runs slightly faster,
1217   but unless you are an alien, you probably will not notice the difference.
1218   Most PAL DVDs have pitch-corrected audio, so when they are played back at
1219   25 fps things will sound right, even though the audio track (and hence the
1220   whole movie) has a running time that is 4% less than NTSC DVDs.
1221 </para>
1223 <para>
1224   Because the video in a PAL DVD has not been altered, you need not worry
1225   much about framerate. The source is 25 fps, and your rip will be 25
1226   fps. However, if you are ripping an NTSC DVD movie, you may need to
1227   apply inverse telecine.
1228 </para>
1230 <para>
1231   For movies shot at 24 fps, the video on the NTSC DVD is either telecined
1232   30000/1001, or else it is progressive 24000/1001 fps and intended to be telecined
1233   on-the-fly by a DVD player. On the other hand, TV series are usually
1234   only interlaced, not telecined. This is not a hard rule: some TV series
1235   are interlaced (such as Buffy the Vampire Slayer) whereas some are a
1236   mixture of progressive and interlaced (such as Angel, or 24).
1237 </para>
1239 <para>
1240   It is highly recommended that you read the section on
1241   <link linkend="menc-feat-telecine">How to deal with telecine and interlacing in NTSC DVDs</link>
1242   to learn how to handle the different possibilities.
1243 </para>
1245 <para>
1246   However, if you are mostly just ripping movies, likely you are either
1247   dealing with 24 fps progressive or telecined video, in which case you can
1248   use the <option>pullup</option> filter <option>-vf
1249   pullup,softskip</option>.
1250 </para>
1252 </sect2>
1254 <sect2 id="menc-feat-dvd-mpeg4-encoding-interlaced">
1255 <title>Encoding interlaced video</title>
1257 <para>
1258   If the movie you want to encode is interlaced (NTSC video or
1259   PAL video), you will need to choose whether you want to
1260   deinterlace or not.
1261   While deinterlacing will make your movie usable on progressive
1262   scan displays such a computer monitors and projectors, it comes
1263   at a cost: The fieldrate of 50 or 60000/1001 fields per second
1264   is halved to 25 or 30000/1001 frames per second, and roughly half of
1265   the information in your movie will be lost during scenes with
1266   significant motion.
1267 </para>
1269 <para>
1270   Therefore, if you are encoding for high quality archival purposes,
1271   it is recommended not to deinterlace.
1272   You can always deinterlace the movie at playback time when
1273   displaying it on progressive scan devices, and future players will
1274   be able to deinterlace to full fieldrate, interpolating 50 or
1275   60000/1001 entire frames per second from the interlaced video.
1276 </para>
1278 <para>
1279 Special care must be taken when working with interlaced video:
1280 </para>
1282 <orderedlist>
1283 <listitem><para>
1284   Crop height and y-offset must be multiples of 4.
1285 </para></listitem>
1286 <listitem><para>
1287   Any vertical scaling must be performed in interlaced mode.
1288 </para></listitem>
1289 <listitem><para>
1290   Postprocessing and denoising filters may not work as expected
1291   unless you take special care to operate them a field at a time,
1292   and they may damage the video if used incorrectly.
1293 </para></listitem>
1294 </orderedlist>
1296 <para>
1297 With these things in mind, here is our first example:
1298 </para>
1299 <screen>
1300   mencoder <replaceable>capture.avi</replaceable> -mc 0 -oac lavc -ovc lavc -lavcopts \
1301   vcodec=mpeg2video:vbitrate=6000:ilme:ildct:acodec=mp2:abitrate=224
1302 </screen>
1303 <para>
1304 Note the <option>ilme</option> and <option>ildct</option> options. 
1305 </para>
1306 </sect2>
1309 <sect2 id="menc-feat-dvd-mpeg4-av-sync">
1310 <title>Notes on Audio/Video synchronization</title>
1311 <para>
1312 <application>MEncoder</application>'s audio/video synchronization
1313 algorithms were designed with the intention of recovering files with
1314 broken sync.
1315 However, in some cases they can cause unnecessary skipping and duplication of
1316 frames, and possibly slight A/V desync, when used with proper input
1317 (of course, A/V sync issues apply only if you process or copy the
1318 audio track while transcoding the video, which is strongly encouraged).
1319 Therefore, you may have to switch to basic A/V sync with
1320 the <option>-mc 0</option> option, or put this in your
1321 <systemitem>~/.mplayer/mencoder</systemitem> config file, as long as
1322 you are only working with good sources (DVD, TV capture, high quality
1323 MPEG-4 rips, etc) and not broken ASF/RM/MOV files.
1324 </para>
1325 <para>
1326 If you want to further guard against strange frame skips and
1327 duplication, you can use both <option>-mc 0</option> and
1328 <option>-noskip</option>.
1329 This will prevent <emphasis>all</emphasis> A/V sync, and copy frames
1330 one-to-one, so you cannot use it if you will be using any filters that
1331 unpredictably add or drop frames, or if your input file has variable
1332 framerate!
1333 Therefore, using <option>-noskip</option> is not in general recommended.
1334 </para>
1335 <para>
1336 The so-called "three-pass" audio encoding which <application>MEncoder</application>
1337 supports has been reported to cause A/V desync.
1338 This will definitely happen if it is used in conjunction with certain
1339 filters, therefore, it is now recommended <emphasis>not</emphasis> to
1340 use three-pass audio mode.
1341 This feature is only left for compatibility purposes and for expert
1342 users who understand when it is safe to use and when it is not.
1343 If you have never heard of three-pass mode before, forget that we
1344 even mentioned it!
1345 </para>
1346 <para>
1347 There have also been reports of A/V desync when encoding from stdin
1348 with <application>MEncoder</application>.
1349 Do not do this! Always use a file or CD/DVD/etc device as input.
1350 </para>
1351 </sect2>
1353 <sect2 id="menc-feat-dvd-mpeg4-codec">
1354 <title>Choosing the video codec</title>
1356 <para>
1357   Choosing the video codec to use depends on several factors, some of
1358   which widely depend on personal taste and technical constraints.
1359 </para>
1360 <itemizedlist>
1361   <listitem><para>
1362   <emphasis role="bold">Compression efficiency</emphasis>:
1363   It is quite easy to understand that newer-generation codecs are made
1364   to yield better picture quality than previous generations.
1365   Therefore, you cannot go wrong
1366   <footnote id='fn-menc-feat-dvd-mpeg4-codec-cpu'>
1367   <para>Be careful, however: Decoding DVD-resolution MPEG-4 AVC videos
1368   requires a fast machine (i.e. a Pentium 4 over 1.5Ghz or a Pentium M
1369   over 1Ghz).
1370   </para></footnote>
1371   when choosing MPEG-4 AVC codecs like
1372   <systemitem class="library">x264</systemitem> instead of MPEG-4 ASP codecs
1373   such as <systemitem class="library">libavcodec</systemitem> MPEG-4 or
1374   <systemitem class="library">XviD</systemitem>.
1375   (To get a better grasp of what the fundamental differences between
1376   MPEG-4 ASP and MPEG-4 AVC are, you would be well advised to read the entry
1377   "<ulink url="http://guru.multimedia.cx/?p=10">15 reasons why MPEG4 sucks</ulink>"
1378   from Michael Niedermayer's blog.)
1379   Likewise, you should get better quality using MPEG-4 ASP instead
1380   of MPEG-2 codecs.
1381   </para>
1382   <para>
1383   However, newer codecs which are in heavy development can suffer from
1384   bugs which have not yet been noticed and which can ruin an encode.
1385   This is simply the tradeoff for using bleeding-edge technology.
1386   </para>
1387   <para>
1388   What is more, beginning to use a new codec requires that you spend some
1389   time becoming familiar with its options, so that you know what
1390   to adjust to achieve a desired picture quality.
1391   </para></listitem>
1393   <listitem><para>
1394   <emphasis role="bold">Hardware compatibility</emphasis>:
1395   It usually takes a long time for standalone video players to begin to
1396   include support for the latest video codecs.
1397   As a result, most only support MPEG-1 (like VCD, XVCD and KVCD), MPEG-2
1398   (like DVD, SVCD and KVCD) and MPEG-4 ASP (like DivX,
1399   <systemitem class="library">libavcodec</systemitem>'s LMP4 and
1400   <systemitem class="library">XviD</systemitem>)
1401   (Beware: Usually, not all MPEG-4 ASP features are supported).
1402   Please refer to the technical specs of your player (if they are available),
1403   or google around for more information.
1404   </para></listitem>
1406   <listitem><para>
1407   <emphasis role="bold">Best quality per encoding time</emphasis>:
1408   Codecs that have been around for some time (such as
1409   <systemitem class="library">libavcodec</systemitem> MPEG-4 and
1410   <systemitem class="library">XviD</systemitem>) are usually heavily
1411   optimized with all kinds of smart algorithms and SIMD assembly code.
1412   That is why they tend to yield the best quality quality per
1413   encoding time ratio.
1414   However, they may have some very advanced options that, if enabled,
1415   will make the encode really slow for marginal gains.
1416   </para>
1417   <para>
1418   If you are after blazing speed you should stick around the default
1419   settings of the video codec (which does not mean you should not experiment
1420   with some of the options which are mentioned in other sections
1421   of this guide).
1422   </para>
1423   <para>
1424   You may also consider choosing a codec which can do multi-threaded
1425   processing.
1426   <systemitem class="library">libavcodec</systemitem> MPEG-4 does
1427   allow that, resulting in small speed gains at the price of lower
1428   picture quality.
1429   <systemitem class="library">XviD</systemitem> has some experimental
1430   patches available to boost encoding speed, by about 40-60% in typical
1431   cases, with low picture degradation.
1432   <systemitem class="library">x264</systemitem> also allows multi-threaded
1433   encoding, which currently speeds-up encoding by 15-30% while lowering
1434   PSNR by about 0.05dB.
1435   </para></listitem>
1437   <listitem><para>
1438   <emphasis role="bold">Personal taste</emphasis>:
1439   This is where it gets almost irrational: For the same reason that some
1440   hung on to DivX&nbsp;3 for years when newer codecs were already doing wonders,
1441   some folks will prefer <systemitem class="library">XviD</systemitem>
1442   or <systemitem class="library">libavcodec</systemitem> MPEG-4 over
1443   <systemitem class="library">x264</systemitem>.
1444   </para>
1445   <para>
1446   Make your own judgment, and do not always listen to what some people will
1447   tell you to do or think: The best codec is the one you master the best,
1448   and the one that looks best to your eyes on your display
1449   <footnote id='fn-menc-feat-dvd-mpeg4-codec-playback'>
1450   <para>The same encode may not look the same on someone else's monitor or
1451   when played back by a different decoder, so future-proof your encodes by
1452   playing them back on different setups.</para></footnote>!
1453   </para></listitem>
1454 </itemizedlist>
1455 <para>
1456   Please refer to the section
1457   <link linkend="menc-feat-selecting-codec">selecting codecs and container formats</link>
1458   to get a list of supported codecs.
1459 </para>
1460 </sect2>
1462 <sect2 id="menc-feat-dvd-mpeg4-audio">
1463 <title>Audio</title>
1465 <para>
1466   Audio is a much simpler problem to solve: if you care about quality, just
1467   leave it as is.
1468   Even AC3 5.1 streams are at most 448Kbit/s, and they are worth every bit.
1469   You might be tempted to transcode the audio to high quality Vorbis, but
1470   just because you do not have an A/V receiver for AC3 pass-through today
1471   does not mean you will not have one tomorrow. Future-proof your DVD rips by
1472   preserving the AC3 stream.
1473   You can keep the AC3 stream either by copying it directly into the video
1474   stream <link linkend="menc-feat-mpeg4">during the encoding</link>.
1475   You can also extract the AC3 stream in order to mux it into containers such
1476   as NUT or Matroska.
1477   <screen>mplayer <replaceable>source_file.vob</replaceable> -aid 129 -dumpaudio -dumpfile <replaceable>sound.ac3</replaceable></screen>
1478   will dump into the file <replaceable>sound.ac3</replaceable> the
1479   audio track number 129 from the file
1480   <replaceable>source_file.vob</replaceable> (NB: DVD VOB files
1481   usually use a different audio numbering,
1482   which means that the VOB audio track 129 is the 2nd audio track of the file).
1483 </para>
1485 <para>
1486   But sometimes you truly have no choice but to further compress the
1487   sound so that more bits can be spent on the video.
1488   Most people choose to compress audio with either MP3 or Vorbis audio
1489   codecs.
1490   While the latter is a very space-efficient codec, MP3 is better supported
1491   by hardware players, although this trend is changing.
1492 </para>
1494 <para>
1495   Do <emphasis>not</emphasis> use <option>-nosound</option> when encoding
1496   a file with audio, even if you will be encoding and muxing audio
1497   separately later.
1498   Though it may work in ideal cases, using <option>-nosound</option> is
1499   likely to hide some problems in your encoding command line setting.
1500   In other words, having a soundtrack during your encode assures you that,
1501   provided you do not see messages such as
1502   <quote>Too many audio packets in the buffer</quote>, you will be able
1503   to get proper sync.
1504 </para>
1506 <para>
1507   You need to have <application>MEncoder</application> process the sound.
1508   You can for example copy the orignal soundtrack during the encode with
1509   <option>-oac copy</option> or convert it to a "light" 4 kHz mono WAV
1510   PCM with <option>-oac pcm -channels 1 -srate 4000</option>.
1511   Otherwise, in some cases, it will generate a video file that will not sync
1512   with the audio.
1513   Such cases are when the number of video frames in the source file does
1514   not match up to the total length of audio frames or whenever there
1515   are discontinuities/splices where there are missing or extra audio frames.
1516   The correct way to handle this kind of problem is to insert silence or
1517   cut audio at these points.
1518   However <application>MPlayer</application> cannot do that, so if you
1519   demux the AC3 audio and encode it with a separate app (or dump it to PCM with
1520   <application>MPlayer</application>), the splices will be left incorrect
1521   and the only way to correct them is to drop/dup video frames at the
1522   splice.
1523   As long as <application>MEncoder</application> sees the audio when it is
1524   encoding the video, it can do this dropping/duping (which is usually OK
1525   since it takes place at full black/scenechange, but if
1526   <application>MEncoder</application> cannot see the audio, it will just
1527   process all frames as-is and they will not fit the final audio stream when
1528   you for example merge your audio and video track into a Matroska file.
1529 </para>
1531 <para>
1532   First of all, you will have to convert the DVD sound into a WAV file that the
1533   audio codec can use as input.
1534   For example:
1535   <screen>mplayer <replaceable>source_file.vob</replaceable> -ao pcm:file=<replaceable>destination_sound.wav</replaceable> -vc dummy -aid 1 -vo null</screen>
1536   will dump the second audio track from the file
1537   <replaceable>source_file.vob</replaceable> into the file
1538   <replaceable>destination_sound.wav</replaceable>.
1539   You may want to normalize the sound before encoding, as DVD audio tracks
1540   are commonly recorded at low volumes.
1541   You can use the tool <application>normalize</application> for instance,
1542   which is available in most distributions.
1543   If you are using Windows, a tool such as <application>BeSweet</application>
1544   can do the same job.
1545   You will compress in either Vorbis or MP3.
1546   For example:
1547   <screen>oggenc -q1 <replaceable>destination_sound.wav</replaceable></screen>
1548   will encode <replaceable>destination_sound.wav</replaceable> with
1549   the encoding quality 1, which is roughly equivalent to 80Kb/s, and
1550   is the minimum quality at which you should encode if you care about
1551   quality.
1552   Please note that MEncoder currently cannot mux Vorbis audio tracks
1553   into the output file because it only supports AVI and MPEG
1554   containers as an output, each of which may lead to audio/video
1555   playback synchronization problems with some players when the AVI file
1556   contain VBR audio streams such as Vorbis.
1557   Do not worry, this document will show you how you can do that with third
1558   party programs.
1559 </para>
1561 </sect2>
1564 <sect2 id="menc-feat-dvd-mpeg4-muxing">
1565 <title>Muxing</title>
1566 <para>
1567   Now that you have encoded your video, you will most likely want
1568   to mux it with one or more audio tracks into a movie container, such
1569   as AVI, MPEG, Matroska or NUT.
1570   <application>MEncoder</application> is currently only able to natively output
1571   audio and video into MPEG and AVI container formats.
1572   for example:
1573   <screen>mencoder -oac copy -ovc copy  -o <replaceable>output_movie.avi</replaceable> -audiofile <replaceable>input_audio.mp2</replaceable> <replaceable>input_video.avi</replaceable></screen>
1574   This would merge the video file <replaceable>input_video.avi</replaceable>
1575   and the audio file <replaceable>input_audio.mp2</replaceable>
1576   into the AVI file <replaceable>output_movie.avi</replaceable>.
1577   This command works with MPEG-1 layer I, II and III (more commonly known
1578   as MP3) audio, WAV and a few other audio formats too.
1579 </para>
1581 <para>
1582   MEncoder features experimental support for
1583   <systemitem class="library">libavformat</systemitem>, which is a
1584   library from the FFmpeg project that supports muxing and demuxing
1585   a variety of containers.
1586   For example:
1587   <screen>mencoder -oac copy -ovc copy  -o <replaceable>output_movie.asf</replaceable> -audiofile <replaceable>input_audio.mp2</replaceable> <replaceable>input_video.avi</replaceable> -of lavf -lavfopts format=asf</screen>
1588   This will do the same thing as the previous example, except that
1589   the output container will be ASF.
1590   Please note that this support is highly experimental (but getting
1591   better every day), and will only work if you compiled
1592   <application>MPlayer</application> with the support for
1593   <systemitem class="library">libavformat</systemitem> enabled (which
1594   means that a pre-packaged binary version will not work in most cases).
1595 </para>
1598 <sect3 id="menc-feat-dvd-mpeg4-muxing-filter-issues">
1599 <title>Improving muxing and A/V sync reliability</title>
1600 <para>
1601   You may experience some serious A/V sync problems while trying to mux
1602   your video and some audio tracks, where no matter how you adjust the
1603   audio delay, you will never get proper sync.
1604   That may happen when you use some video filters that will drop or
1605   duplicate some frames, like the inverse telecine filters.
1606   It is strongly encouraged to append the <option>harddup</option> video
1607   filter at the end of the filter chain to avoid this kind of problem.
1608 </para>
1610 <para>
1611   Without <option>harddup</option>, if <application>MEncoder</application>
1612   wants to duplicate a frame, it relies on the muxer to put a mark on the
1613   container so that the last frame will be displayed again to maintain
1614   sync while writing no actual frame.
1615   With <option>harddup</option>, <application>MEncoder</application>
1616   will instead just push the last frame displayed again into the filter
1617   chain.
1618   This means that the encoder receives the <emphasis>exact</emphasis>
1619   same frame twice, and compresses it.
1620   This will result in a slightly bigger file, but will not cause problems
1621   when demuxing or remuxing into other container formats.
1622 </para>
1624 <para>
1625   You may also have no choice but to use <option>harddup</option> with
1626   container formats that are not too tightly linked with
1627   <application>MEncoder</application> such as the ones supported through
1628   <systemitem class="library">libavformat</systemitem>, which may not
1629   support frame duplication at the container level.
1630 </para>
1631 </sect3>
1634 <sect3 id="menc-feat-dvd-mpeg4-muxing-avi-limitations">
1635 <title>Limitations of the AVI container</title>
1636 <para>
1637   Although it is the most widely-supported container format after MPEG-1,
1638   AVI also has some major drawbacks.
1639   Perhaps the most obvious is the overhead.
1640   For each chunk of the AVI file, 24 bytes are wasted on headers and
1641   index.
1642   This translates into a little over 5 MB per hour, or 1-2.5%
1643   overhead for a 700 MB movie. This may not seem like much, but it could
1644   mean the difference between being able to use 700 kbit/sec video or
1645   714 kbit/sec, and every bit of quality counts.
1646 </para>
1648 <para>
1649   In addition this gross inefficiency, AVI also has the following major
1650   limitations:
1651 </para>
1653 <orderedlist>
1654 <listitem>
1655 <para>
1656   Only fixed-fps content can be stored. This is particularly limiting
1657   if the original material you want to encode is mixed content, for
1658   example a mix of NTSC video and film material.
1659   Actually there are hacks that can be used to store mixed-framerate
1660   content in AVI, but they increase the (already huge) overhead
1661   fivefold or more and so are not practical.
1662 </para>
1663 </listitem>
1664 <listitem>
1665 <para>
1666   Audio in AVI files must be either constant-bitrate (CBR) or
1667   constant-framesize (i.e. all frames decode to the same number of
1668   samples).
1669   Unfortunately, the most efficient codec, Vorbis, does not meet
1670   either of these requirements.
1671   Therefore, if you plan to store your movie in AVI, you will have to
1672   use a less efficient codec such as MP3 or AC3.
1673 </para>
1674 </listitem>
1675 </orderedlist>
1677 <para>
1678   Having said all that, <application>MEncoder</application> does not
1679   currently support variable-fps output or Vorbis encoding.
1680   Therefore, you may not see these as limitations if
1681   <application>MEncoder</application> is the
1682   only tool you will be using to produce your encodes.
1683   However, it is possible to use <application>MEncoder</application>
1684   only for video encoding, and then use external tools to encode
1685   audio and mux it into another container format.
1686 </para>
1687 </sect3>
1689 <sect3 id="menc-feat-dvd-mpeg4-muxing-matroska">
1690 <title>Muxing into the Matroska container</title>
1691 <para>
1692   Matroska is a free, open standard container format, aiming
1693   to offer a lot of advanced features, which older containers
1694   like AVI cannot handle.
1695   For example, Matroska supports variable bitrate audio content
1696   (VBR), variable framerates (VFR), chapters, file attachments,
1697   error detection code (EDC) and modern A/V Codecs like "Advanced Audio
1698   Coding" (AAC), "Vorbis" or "MPEG-4 AVC" (H.264), next to nothing
1699   handled by AVI.
1700 </para>
1702 <para>
1703   The tools required to create Matroska files are collectively called
1704   <application>mkvtoolnix</application>, and are available for most
1705   Unix platforms as well as <application>Windows</application>.
1706   Because Matroska is an open standard you may find other
1707   tools that suit you better, but since mkvtoolnix is the most
1708   common, and is supported by the Matroska team itself, we will
1709   only cover its usage.
1710 </para>
1712 <para>
1713   Probably the easiest way to get started with Matroska is to use 
1714   <application>MMG</application>, the graphical frontend shipped with
1715   <application>mkvtoolnix</application>, and follow the
1716   <ulink url="http://www.bunkus.org/videotools/mkvtoolnix/doc/mkvmerge-gui.html">guide to mkvmerge GUI (mmg)</ulink>
1717 </para>
1719 <para>
1720   You may also mux audio and video files using the command line:
1721   <screen>mkvmerge -o <replaceable>output.mkv</replaceable> <replaceable>input_video.avi</replaceable> <replaceable>input_audio1.mp3</replaceable> <replaceable>input_audio2.ac3</replaceable></screen>
1722   This would merge the video file <replaceable>input_video.avi</replaceable>
1723   and the two audio files <replaceable>input_audio1.mp3</replaceable>
1724   and <replaceable>input_audio2.ac3</replaceable> into the Matroska
1725   file <replaceable>output.mkv</replaceable>.
1726   Matroska, as mentioned earlier, is able to do much more than that, like
1727   multiple audio tracks (including fine-tuning of audio/video
1728   synchronization), chapters, subtitles, splitting, etc...
1729   Please refer to the documentation of those applications for
1730   more details.
1731 </para>
1733 </sect3>
1735 </sect2>
1737 </sect1>
1739 <sect1 id="menc-feat-telecine">
1740 <title>How to deal with telecine and interlacing within NTSC DVDs</title>
1742 <sect2 id="menc-feat-telecine-intro">
1743 <title>Introduction</title>
1744 <formalpara>
1745 <title>What is telecine?</title>
1746 <para>
1747   I suggest you visit this page if you do not understand much of what
1748   is written in this document:
1749   <ulink url="http://www.divx.com/support/guides/guide.php?gid=10">http://www.divx.com/support/guides/guide.php?gid=10</ulink>
1750   This URL links to an understandable and reasonably comprehensive
1751   description of what telecine is.
1752 </para></formalpara>
1754 <formalpara>
1755 <title>A note about the numbers.</title>
1756 <para>
1757   Many documents, including the guide linked above, refer to the fields
1758   per second value of NTSC video as 59.94 and the corresponding frames
1759   per second values as 29.97 (for telecined and interlaced) and 23.976
1760   (for progressive). For simplicity, some documents even round these
1761   numbers to 60, 30, and 24.
1762 </para></formalpara>
1764 <para>
1765   Strictly speaking, all those numbers are approximations. Black and
1766   white NTSC video was exactly 60 fields per second, but 60000/1001
1767   was later chosen to accomodate color data while remaining compatible
1768   with contemporary black and white televisions. Digital NTSC video
1769   (such as on a DVD) is also 60000/1001 fields per second. From this,
1770   interlaced and telecined video are derived to be 30000/1001 frames
1771   per second; progressive video is 24000/1001 frames per second.
1772 </para>
1774 <para>
1775   Older versions of the <application>MEncoder</application> documentation
1776   and many archived mailing list posts refer to 59.94, 29.97, and 23.976.
1777   All <application>MEncoder</application> documentation has been updated
1778   to use the fractional values, and you should use them too.
1779 </para>
1781 <para>
1782   <option>-ofps 23.976</option> is incorrect.
1783   <option>-ofps 24000/1001</option> should be used instead.
1784 </para>
1786 <formalpara>
1787 <title>How telecine is used.</title>
1788 <para>
1789   All video intended to be displayed on an NTSC
1790   television set must be 60000/1001 fields per second. Made-for-TV movies
1791 4  and shows are often filmed directly at 60000/1001 fields per second, but
1792   the majority of cinema is filmed at 24 or 24000/1001 frames per
1793   second. When cinematic movie DVDs are mastered, the video is then
1794   converted for television using a process called telecine.
1795 </para></formalpara>
1797 <para>
1798   On a DVD, the video is never actually stored as 60000/1001 fields per
1799   second. For video that was originally 60000/1001, each pair of fields is
1800   combined to form a frame, resulting in 30000/1001 frames per
1801   second. Hardware DVD players then read a flag embedded in the video
1802   stream to determine whether the odd- or even-numbered lines should
1803   form the first field.
1804 </para>
1806 <para>
1807   Usually, 24000/1001 frames per second content stays as it is when
1808   encoded for a DVD, and the DVD player must perform telecining
1809   on-the-fly. Sometimes, however, the video is telecined
1810   <emphasis>before</emphasis> being stored on the DVD; even though it
1811   was originally 24000/1001 frames per second, it becomes 60000/1001 fields per
1812   second. When it is stored on the DVD, pairs of fields are combined to form
1813   30000/1001 frames per second.
1814 </para>
1816 <para>
1817   When looking at individual frames formed from 60000/10001 fields per
1818   second video, telecined or otherwise, interlacing is clearly visible
1819   wherever there is any motion, because one field (say, the
1820   even-numbered lines) represents a moment in time 1/(60000/1001)
1821   seconds later than the other. Playing interlaced video on a computer
1822   looks ugly both because the monitor is higher resolution and because
1823   the video is shown frame-after-frame instead of field-after-field.
1824 </para>
1826 <itemizedlist>
1827 <title>Notes:</title>
1828 <listitem><para>
1829   This section only applies to NTSC DVDs, and not PAL.
1830   </para></listitem>
1831 <listitem><para>
1832   The example <application>MEncoder</application> lines throughout the
1833   document are <emphasis role="bold">not</emphasis> intended for
1834   actual use. They are simply the bare minimum required to encode the
1835   pertaining video category. How to make good DVD rips or fine-tune
1836   <systemitem class="library">libavcodec</systemitem> for maximal
1837   quality is not within the scope of this document.
1838   </para></listitem>
1839 <listitem><para>
1840   There are a couple footnotes specific to this guide, linked like this:
1841   <link linkend="menc-feat-telecine-footnotes">[1]</link>
1842   </para></listitem>
1843 </itemizedlist>
1844 </sect2>
1846 <sect2 id="menc-feat-telecine-ident">
1847 <title>How to tell what type of video you have</title>
1849 <sect3 id="menc-feat-telecine-ident-progressive">
1850 <title>Progressive</title>
1851 <para>
1852   Progressive video was originally filmed at 24000/1001 fps, and stored
1853   on the DVD without alteration.
1854 </para>
1856 <para>
1857   When you play a progressive DVD in <application>MPlayer</application>,
1858   <application>MPlayer</application> will print the following line as
1859   soon as the movie begins to play:
1861   <screen> demux_mpg: 24000/1001 fps progressive NTSC content detected, switching framerate.</screen>
1863   From this point forward, demux_mpg should never say it finds
1864   &quot;30000/1001 fps NTSC content.&quot;
1865 </para>
1867 <para>
1868   When you watch progressive video, you should never see any
1869   interlacing. Beware, however, because sometimes there is a tiny bit
1870   of telecine mixed in where you would not expect. I have encountered TV
1871   show DVDs that have one second of telecine at every scene change, or
1872   at seemingly random places. I once watched a DVD that had a
1873   progressive first half, and the second half was telecined. If you
1874   want to be <emphasis>really</emphasis> thorough, you can scan the
1875   entire movie:
1877   <screen>mplayer dvd://1 -nosound -vo null -benchmark</screen>
1879   Using <option>-benchmark</option> makes
1880   <application>MPlayer</application> play the movie as quickly as it
1881   possibly can; still, depending on your hardware, it can take a
1882   while. Every time demux_mpg reports a framerate change, the line
1883   immediately above will show you the time at which the change
1884   occurred.
1885 </para>
1887 <para>
1888   Sometimes progressive video on DVDs is referred to as
1889   &quot;soft-telecine&quot; because it is intended to
1890   be telecined by the DVD player.
1891 </para>
1892 </sect3>
1894 <sect3 id="menc-feat-telecine-ident-telecined">
1895 <title>Telecined</title>
1896 <para>
1897   Telecined video was originally filmed at 24000/1001, but was telecined
1898   <emphasis>before</emphasis> it was written to the DVD.
1899 </para>
1901 <para>
1902   <application>MPlayer</application> does not (ever) report any
1903   framerate changes when it plays telecined video.
1904 </para>
1906 <para>
1907   Watching a telecined video, you will see interlacing artifacts that
1908   seem to &quot;blink&quot;: they repeatedly appear and disappear.
1909   You can look closely at this by
1910   <orderedlist>
1911   <listitem>
1912     <screen>mplayer dvd://1</screen>
1913     </listitem>
1914   <listitem><para>
1915     Seek to a part with motion.
1916     </para></listitem>
1917   <listitem><para>
1918     Use the <keycap>.</keycap> key to step forward one frame at a time.
1919     </para></listitem>
1920   <listitem><para>
1921     Look at the pattern of interlaced-looking and progressive-looking
1922     frames. If the pattern you see is PPPII,PPPII,PPPII,... then the
1923     video is telecined. If you see some other pattern, then the video
1924     may have been telecined using some non-standard method;
1925     <application>MEncoder</application> cannot losslessly convert
1926     non-standard telecine to progressive. If you do not see any
1927     pattern at all, then it is most likely interlaced.
1928     </para></listitem>
1929   </orderedlist>
1930 </para>
1932 <para>
1933   Sometimes telecined video on DVDs is referred to as
1934   &quot;hard-telecine&quot;. Since hard-telecine is already 60000/1001 fields
1935   per second, the DVD player plays the video without any manipulation.
1936 </para>
1938 <para>
1939   Another way to tell if your source is telecined or not is to play
1940   the source with the <option>-vf pullup</option> and <option>-v</option>
1941   command line options to see how <option>pullup</option> matches frames.
1942   If the source is telecined, you should see on the console a 3:2 pattern
1943   with <systemitem>0+.1.+2</systemitem> and <systemitem>0++1</systemitem>
1944   alternating.
1945   This technique has the advantage that you do not need to watch the
1946   source to identify it, which could be useful if you wish to automate
1947   the encoding procedure, or to carry out said procedure remotely via
1948   a slow connection.
1949 </para>
1951 </sect3>
1953 <sect3 id="menc-feat-telecine-ident-interlaced">
1954 <title>Interlaced</title>
1955 <para>
1956   Interlaced video was originally filmed at 60000/1001 fields per second,
1957   and stored on the DVD as 30000/1001 frames per second. The interlacing effect
1958   (often called &quot;combing&quot;) is a result of combining pairs of
1959   fields into frames. Each field is supposed to be 1/(60000/1001) seconds apart,
1960   and when they are displayed simultaneously the difference is apparent.
1961 </para>
1963 <para>
1964   As with telecined video, <application>MPlayer</application> should
1965   not ever report any framerate changes when playing interlaced content.
1966 </para>
1968 <para>
1969   When you view an interlaced video closely by frame-stepping with the
1970   <keycap>.</keycap> key, you will see that every single frame is interlaced.
1971 </para>
1972 </sect3>
1974 <sect3 id="menc-feat-telecine-ident-mixedpt">
1975 <title>Mixed progressive and telecine</title>
1976 <para>
1977   All of a &quot;mixed progressive and telecine&quot; video was originally
1978   24000/1001 frames per second, but some parts of it ended up being telecined.
1979 </para>
1981 <para>
1982   When <application>MPlayer</application> plays this category, it will
1983   (often repeatedly) switch back and forth between &quot;30000/1001 fps NTSC&quot;
1984   and &quot;24000/1001 fps progressive NTSC&quot;. Watch the bottom of
1985   <application>MPlayer</application>'s output to see these messages.
1986 </para>
1988 <para>
1989   You should check the &quot;30000/1001 fps NTSC&quot; sections to make sure
1990   they are actually telecine, and not just interlaced.
1991 </para>
1992 </sect3>
1994 <sect3 id="menc-feat-telecine-ident-mixedpi">
1995 <title>Mixed progressive and interlaced</title>
1996 <para>
1997   In &quot;mixed progressive and interlaced&quot; content, progressive
1998   and interlaced video have been spliced together.
1999 </para>
2001 <para>
2002   This category looks just like &quot;mixed progressive and telecine&quot;,
2003   until you examine the 30000/1001 fps sections and see that they do not have the
2004   telecine pattern.
2005 </para>
2006 </sect3>
2008 </sect2>
2010 <sect2 id="menc-feat-telecine-encode">
2011 <title>How to encode each category</title>
2012 <para>
2013   As I mentioned in the beginning, example <application>MEncoder</application>
2014   lines below are <emphasis role="bold">not</emphasis> meant to actually be used;
2015   they only demonstrate the minimum parameters to properly encode each category.
2016 </para>
2018 <sect3 id="menc-feat-telecine-encode-progressive">
2019 <title>Progressive</title>
2020 <para>
2021   Progressive video requires no special filtering to encode. The only
2022   parameter you need to be sure to use is
2023   <option>-ofps 24000/1001</option>. Otherwise, <application>MEncoder</application>
2024   will try to encode at 30000/1001 fps and will duplicate frames.
2025 </para>
2027 <para>
2028   <screen>mencoder dvd://1 -oac copy -ovc lavc -ofps 24000/1001</screen>
2029 </para>
2031 <para>
2032   It is often the case, however, that a video that looks progressive
2033   actually has very short parts of telecine mixed in. Unless you are
2034   sure, it is safest to treat the video as
2035   <link linkend="menc-feat-telecine-encode-mixedpt">mixed progressive and telecine</link>.
2036   The performance loss is small
2037   <link linkend="menc-feat-telecine-footnotes">[3]</link>.
2038 </para>
2039 </sect3>
2041 <sect3 id="menc-feat-telecine-encode-telecined">
2042 <title>Telecined</title>
2043 <para>
2044   Telecine can be reversed to retrieve the original 24000/1001 content,
2045   using a process called inverse-telecine.
2046   <application>MPlayer</application> contains several filters to
2047   accomplish this; the best filter, <option>pullup</option>, is described
2048   in the <link linkend="menc-feat-telecine-encode-mixedpt">mixed
2049   progressive and telecine</link> section.
2050 </para>
2051 </sect3>
2053 <sect3 id="menc-feat-telecine-encode-interlaced">
2054 <title>Interlaced</title>
2055 <para>
2056   For most practical cases it is not possible to retrieve a complete
2057   progressive video from interlaced content. The only way to do so
2058   without losing half of the vertical resolution is to double the
2059   framerate and try to &quot;guess&quot; what ought to make up the
2060   corresponding lines for each field (this has drawbacks - see method
2061   3).
2062 </para>
2064 <orderedlist>
2065 <listitem><para>
2067   Encode the video in interlaced form. Normally, interlacing wreaks
2068   havoc with the encoder's ability to compress well, but
2069   <systemitem class="library">libavcodec</systemitem> has two
2070   parameters specifically for dealing with storing interlaced video a
2071   bit better: <option> ildct</option> and <option>ilme</option>. Also,
2072   using <option>mbd=2</option> is strongly recommended
2073   <link linkend="menc-feat-telecine-footnotes">[2] </link> because it
2074   will encode macroblocks as non-interlaced in places where there is
2075   no motion. Note that <option>-ofps</option> is NOT needed here.
2077   <screen>mencoder dvd://1 -oac copy -ovc lavc -lavcopts ildct:ilme:mbd=2</screen>
2078   </para></listitem>
2079 <listitem><para>
2080   Use a deinterlacing filter before encoding. There are several of
2081   these filters available to choose from, each with its own advantages
2082   and disadvantages. Consult <option>mplayer -pphelp</option> to see
2083   what is available (grep for &quot;deint&quot;), and search the
2084   <ulink url="http://www.mplayerhq.hu/homepage/design6/info.html#mailing_lists">
2085   MPlayer mailing lists</ulink> to find many discussions about the
2086   various filters. Again, the framerate is not changing, so no
2087   <option>-ofps</option>. Also, deinterlacing should be done after
2088   cropping <link linkend="menc-feat-telecine-footnotes">[1]</link> and
2089   before scaling.
2091   <screen>mencoder dvd://1 -oac copy -vf pp=lb -ovc lavc</screen>
2092   </para></listitem>
2093 <listitem><para>
2094   Unfortunately, this option is buggy with
2095   <application>MEncoder</application>; it ought to work well with
2096   <application>MEncoder G2</application>, but that is not here yet. You
2097   might experience crahes. Anyway, the purpose of <option> -vf
2098   tfields</option> is to create a full frame out of each field, which
2099   makes the framerate 60000/1001. The advantage of this approach is that no
2100   data is ever lost; however, since each frame comes from only one
2101   field, the missing lines have to be interpolated somehow. There are
2102   no very good methods of generating the missing data, and so the
2103   result will look a bit similar to when using some deinterlacing
2104   filters. Generating the missing lines creates other issues, as well,
2105   simply because the amount of data doubles. So, higher encoding
2106   bitrates are required to maintain quality, and more CPU power is
2107   used for both encoding and decoding. tfields has several different
2108   options for how to create the missing lines of each frame. If you
2109   use this method, then Reference the manual, and chose whichever
2110   option looks best for your material. Note that when using
2111   <option>tfields</option> you
2112   <emphasis role="bold">have to</emphasis> specify both
2113   <option>-fps</option> and <option>-ofps</option> to be twice the
2114   framerate of your original source.
2116   <screen>mencoder dvd://1 -oac copy -vf tfields=2 -ovc lavc -fps 60000/1001 -ofps 60000/1001</screen>
2117   </para></listitem>
2118 <listitem><para>
2119   If you plan on downscaling dramatically, you can extract and encode
2120   only one of the two fields. Of course, you will lose half the vertical
2121   resolution, but if you plan on downscaling to at most 1/2 of the
2122   original, the loss will not matter much. The result will be a
2123   progressive 30000/1001 frames per second file. The procedure is to use
2124   <option>-vf field</option>, then crop
2125   <link linkend="menc-feat-telecine-footnotes">[1]</link> and scale
2126   appropriately. Remember that you will have to adjust the scale to
2127   compensate for the vertical resolution being halved.
2128   <screen>mencoder dvd://1 -oac copy -vf field=0 -ovc lavc</screen>
2129   </para></listitem>
2130 </orderedlist>
2131 </sect3>
2133 <sect3 id="menc-feat-telecine-encode-mixedpt">
2134 <title>Mixed progressive and telecine</title>
2135 <para>
2136   In order to turn mixed progressive and telecine video into entirely
2137   progressive video, the telecined parts have to be
2138   inverse-telecined. There are three ways to accomplish this,
2139   described below. Note that you should
2140   <emphasis role="bold">always</emphasis> inverse-telecine before any
2141   rescaling; unless you really know what you are doing,
2142   inverse-telecine before cropping, too
2143   <link linkend="menc-feat-telecine-footnotes">[1]</link>.
2144   <option>-ofps 24000/1001</option> is needed here because the output video
2145   will be 24000/1001 frames per second.
2146 </para>
2148 <itemizedlist>
2149 <listitem><para>
2150   <option>-vf pullup</option> is designed to inverse-telecine
2151   telecined material while leaving progressive data alone. In order to
2152   work properly, <option>pullup</option> <emphasis role="bold">must</emphasis>
2153   be followed by the <option>softskip</option> filter or
2154   else <application>MEncoder</application> will crash.
2155   <option>pullup</option> is, however, the cleanest and most
2156   accurate method available for encoding both telecine and
2157   &quot;mixed progressive and telecine&quot;.
2159   <screen>mencoder dvd://1 -oac copy -vf pullup,softskip -ovc lavc -ofps 24000/1001</screen>
2160   </para>
2163   </listitem>
2164   <listitem><para>
2165   An older method
2166   is to, rather than inverse-telecine the telecined parts, telecine
2167   the non-telecined parts and then inverse-telecine the whole
2168   video. Sound confusing? softpulldown is a filter that goes through
2169   a video and makes the entire file telecined. If we follow
2170   softpulldown with either <option>detc</option> or
2171   <option>ivtc</option>, the final result will be entirely
2172   progressive. <option>-ofps 24000/1001</option> is needed.
2174   <screen>mencoder dvd://1 -oac copy -vf softpulldown,ivtc=1 -ovc lavc -ofps 24000/1001</screen>
2175   </para>
2176   </listitem>
2178 <listitem><para>
2179   I have not used <option>-vf filmdint</option> myself, but here is what
2180   D Richard Felker III has to say:
2182   <blockquote><para>It is OK, but IMO it tries to deinterlace rather
2183     than doing inverse telecine too often (much like settop DVD
2184     players &amp; progressive TVs) which gives ugly flickering and
2185     other artifacts. If you are going to use it, you at least need to
2186     spend some time tuning the options and watching the output first
2187     to make sure it is not messing up.</para></blockquote>
2188   </para></listitem>
2189 </itemizedlist>
2190 </sect3>
2192 <sect3 id="menc-feat-telecine-encode-mixedpi">
2193 <title>Mixed progressive and interlaced</title>
2194 <para>
2195   There are two options for dealing with this category, each of
2196   which is a compromise. You should decide based on the
2197   duration/location of each type.
2198 </para>
2200 <itemizedlist>
2201 <listitem><para>
2202   Treat it as progressive. The interlaced parts will look interlaced,
2203   and some of the interlaced fields will have to be dropped, resulting
2204   in a bit of uneven jumpiness. You can use a postprocessing filter if
2205   you want to, but it may slightly degrade the progressive parts.
2206   </para>
2208   <para>
2209   This option should definitely not be used if you want to eventually
2210   display the video on an interlaced device (with a TV card, for
2211   example). If you have interlaced frames in a 24000/1001 frames per
2212   second video, they will be telecined along with the progressive
2213   frames. Half of the interlaced "frames" will be displayed for three
2214   fields' duration (3/(60000/1001) seconds), resulting in a flicking
2215   &quot;jump back in time&quot; effect that looks quite bad. If you
2216   even attempt this, you <emphasis role="bold">must</emphasis> use a
2217   deinterlacing filter like <option>lb</option> or
2218   <option>l5</option>.
2219   </para>
2221   <para>
2222   It may also be a bad idea for progressive display, too. It will drop
2223   pairs of consecutive interlaced fields, resulting in a discontinuity
2224   that can be more visible than with the second method, which shows
2225   some progressive frames twice. 30000/1001 frames per second interlaced
2226   video is already a bit choppy because it really should be shown at
2227   60000/1001 fields per second, so the duplicate frames do not stand out as
2228   much.
2229   </para>
2231   <para>
2232   Either way, it is best to consider your content and how you intend to
2233   display it. If your video is 90% progressive and you never intend to
2234   show it on a TV, you should favor a progressive approach. If it is
2235   only half progressive, you probably want to encode it as if it is all
2236   interlaced.
2237   </para>
2238   </listitem>
2240 <listitem><para>
2241   Treat it as interlaced. Some frames of the progressive parts will
2242   need to be duplicated, resulting in uneven jumpiness. Again,
2243   deinterlacing filters may slightly degrade the progressive parts.
2244   </para></listitem>
2246 </itemizedlist>
2247 </sect3>
2249 </sect2>
2251 <sect2 id="menc-feat-telecine-footnotes">
2252 <title>Footnotes</title>
2253 <orderedlist>
2254 <listitem><formalpara>
2255   <title>About cropping:</title>
2256   <para>
2257   Video data on DVDs are stored in a format called YUV 4:2:0. In YUV
2258   video, luma (&quot;brightness&quot;) and chroma (&quot;color&quot;)
2259   are stored separately. Because the human eye is somewhat less
2260   sensitive to color than it is to brightness, in a YUV 4:2:0 picture
2261   there is only one chroma pixel for every four luma pixels. In a
2262   progressive picture, each square of four luma pixels (two on each
2263   side) has one common chroma pixel. You must crop progressive YUV
2264   4:2:0 to even resolutions, and use even offsets. For example,
2265   <option>crop=716:380:2:26</option> is OK but
2266   <option>crop=716:380:3:26 </option> is not.
2267   </para>
2268   </formalpara>
2270   <para>
2271   When you are dealing with interlaced YUV 4:2:0, the situation is a
2272   bit more complicated. Instead of every four luma pixels in the
2273   <emphasis>frame</emphasis> sharing a chroma pixel, every four luma
2274   pixels in each <emphasis> field</emphasis> share a chroma
2275   pixel. When fields are interlaced to form a frame, each scanline is
2276   one pixel high. Now, instead of all four luma pixels being in a
2277   square, there are two pixels side-by-side, and the other two pixels
2278   are side-by-side two scanlines down. The two luma pixels in the
2279   intermediate scanline are from the other field, and so share a
2280   different chroma pixel with two luma pixels two scanlines away. All
2281   this confusion makes it necessary to have vertical crop dimensions
2282   and offsets be multiples of four. Horizontal can stay even.
2283   </para>
2285   <para>
2286   For telecined video, I recommend that cropping take place after
2287   inverse telecining. Once the video is progressive you only need to
2288   crop by even numbers. If you really want to gain the slight speedup
2289   that cropping first may offer, you must crop vertically by multiples
2290   of four or else the inverse-telecine filter will not have proper data.
2291   </para>
2293   <para>
2294   For interlaced (not telecined) video, you must always crop
2295   vertically by multiples of four unless you use <option>-vf
2296   field</option> before cropping.
2297   </para>
2298   </listitem>
2300 <listitem><formalpara>
2301   <title>About encoding parameters and quality:</title>
2302   <para>
2303   Just because I recommend <option>mbd=2</option> here does not mean it
2304   should not be used elsewhere. Along with <option>trell</option>,
2305   <option>mbd=2</option> is one of the two
2306    <systemitem class="library">libavcodec</systemitem> options that
2307   increases quality the most, and you should always use at least those
2308   two unless the drop in encoding speed is prohibitive (e.g. realtime
2309   encoding). There are many other options to
2310   <systemitem class="library">libavcodec</systemitem> that increase
2311   encoding quality (and decrease encoding speed) but that is beyond
2312   the scope of this document.
2313   </para>
2314   </formalpara>
2315   </listitem>
2317 <listitem><formalpara>
2318   <title>About the performance of pullup:</title>
2319   <para>
2320   It is safe to use <option>pullup</option> (along with <option>softskip
2321   </option>) on progressive video, and is usually a good idea unless
2322   the source has been definitively verified to be entirely progressive.
2323   The performace loss is small for most cases. On a bare-minimum encode,
2324   <option>pullup</option> causes <application>MEncoder</application> to
2325   be 50% slower. Adding sound processing and advanced <option>lavcopts
2326   </option> overshadows that difference, bringing the performance
2327   decrease of using <option>pullup</option> down to 2%.
2328   </para>
2329   </formalpara>
2330   </listitem>
2332 </orderedlist>
2334 </sect2>
2336 </sect1>
2339 <sect1 id="menc-feat-enc-libavcodec">
2340 <title>Encoding with the <systemitem class="library">libavcodec</systemitem>
2341   codec family</title>
2343 <para>
2344 <link linkend="ffmpeg"><systemitem class="library">libavcodec</systemitem></link>
2345 provides simple encoding to a lot of interesting video and audio formats.
2346 You can encode to the following codecs (more or less up to date):
2347 </para>
2349 <sect2 id="menc-feat-enc-libavcodec-video-codecs">
2350 <title><systemitem class="library">libavcodec</systemitem>'s video codecs</title>
2352 <para>
2353 <informaltable frame="all">
2354 <tgroup cols="2">
2355 <thead>
2356 <row><entry>Video codec name</entry><entry>Description</entry></row>
2357 </thead>
2358 <tbody>
2359 <row><entry>mjpeg</entry><entry>
2360    Motion JPEG
2361   </entry></row>
2362 <row><entry>ljpeg</entry><entry>
2363    lossless JPEG
2364   </entry></row>
2365 <row><entry>h261</entry><entry>
2366   H.261
2367   </entry></row>
2368 <row><entry>h263</entry><entry>
2369   H.263
2370   </entry></row>
2371 <row><entry>h263p</entry><entry>
2372   H.263+
2373   </entry></row>
2374 <row><entry>mpeg4</entry><entry>
2375   ISO standard MPEG-4 (DivX 5, XviD compatible)
2376   </entry></row>
2377 <row><entry>msmpeg4</entry><entry>
2378   pre-standard MPEG-4 variant by MS, v3 (AKA DivX3)
2379   </entry></row>
2380 <row><entry>msmpeg4v2</entry><entry>
2381   pre-standard MPEG-4 by MS, v2 (used in old ASF files)
2382   </entry></row>
2383 <row><entry>wmv1</entry><entry>
2384   Windows Media Video, version 1 (AKA WMV7)
2385   </entry></row>
2386 <row><entry>wmv2</entry><entry>
2387   Windows Media Video, version 2 (AKA WMV8)
2388   </entry></row>
2389 <row><entry>rv10</entry><entry>
2390   RealVideo 1.0
2391   </entry></row>
2392 <row><entry>rv20</entry><entry>
2393   RealVideo 2.0
2394   </entry></row>
2395 <row><entry>mpeg1video</entry><entry>
2396   MPEG-1 video
2397   </entry></row>
2398 <row><entry>mpeg2video</entry><entry>
2399   MPEG-2 video
2400   </entry></row>
2401 <row><entry>huffyuv</entry><entry>
2402   lossless compression
2403   </entry></row>
2404 <row><entry>asv1</entry><entry>
2405   ASUS Video v1
2406   </entry></row>
2407 <row><entry>asv2</entry><entry>
2408   ASUS Video v2
2409   </entry></row>
2410 <row><entry>ffv1</entry><entry>
2411   FFmpeg's lossless video codec
2412   </entry></row>
2413 <row><entry>svq1</entry><entry>
2414   Sorenson video 1
2415   </entry></row>
2416 <row><entry>flv</entry><entry>
2417   Sorenson H.263 used in Flash Video
2418   </entry></row>
2419 <row><entry>dvvideo</entry><entry>
2420   Sony Digital Video
2421   </entry></row>
2422 <row><entry>snow</entry><entry>
2423   FFmpeg's experimental wavelet-based codec
2424   </entry></row>
2425 </tbody>
2426 </tgroup>
2427 </informaltable>
2429 The first column contains the codec names that should be passed after the
2430 <literal>vcodec</literal> config, like: <option>-lavcopts vcodec=msmpeg4</option>
2431 </para>
2432 <informalexample>
2433 <para>
2434 An example with MJPEG compression:
2435 <screen>mencoder dvd://2 -o title2.avi -ovc lavc -lavcopts vcodec=mjpeg -oac copy</screen>
2436 </para>
2437 </informalexample>
2438 </sect2>
2440 <sect2 id="menc-feat-enc-libavcodec-audio-codecs">
2441 <title><systemitem class="library">libavcodec</systemitem>'s audio codecs</title>
2442 <para>
2443 <informaltable frame="all">
2444 <tgroup cols="2">
2445 <thead>
2446 <row><entry>Audio codec name</entry><entry>Description</entry></row>
2447 </thead>
2448 <tbody>
2449   <row>
2450     <entry>mp2</entry>
2451     <entry>MPEG Layer 2</entry>
2452   </row>
2453   <row>
2454     <entry>ac3</entry>
2455     <entry>AC3, AKA Dolby Digital</entry>
2456   </row>
2457   <row>
2458     <entry>adpcm_ima_wav</entry>
2459     <entry>IMA adaptive PCM (4 bits per sample, 4:1 compression)</entry>
2460   </row>
2461   <row>
2462     <entry>sonic</entry>
2463     <entry>experimental lossy/lossless codec</entry>
2464   </row>
2465 </tbody>
2466 </tgroup>
2467 </informaltable>
2469 The first column contains the codec names that should be passed after the
2470 <literal>acodec</literal> option, like: <option>-lavcopts acodec=ac3</option>
2471 </para>
2473 <informalexample>
2474 <para>
2475 An example with AC3 compression:
2476 <screen>mencoder dvd://2 -o title2.avi -oac lavc -lavcopts acodec=ac3 -ovc copy</screen>
2477 </para>
2478 </informalexample>
2480 <para>
2481   Contrary to <systemitem class="library">libavcodec</systemitem>'s video
2482   codecs, its audio codecs do not make a wise usage of the bits they are
2483   given as they lack some minimal psychoacoustic model (if at all)
2484   which most other codec implementations feature.
2485   However, note that all these audio codecs are very fast and work
2486   out-of-the-box everywhere <application>MEncoder</application> has been
2487   compiled with <systemitem class="library">libavcodec</systemitem> (which
2488   is the case most of time), and do not depend on external libraries.
2489 </para>
2490 </sect2>
2493 <sect2 id="menc-feat-dvd-mpeg4-lavc-encoding-options">
2494 <title>Encoding options of libavcodec</title>
2496 <para>
2497   Ideally, you would probably want to be able to just tell the encoder to switch
2498   into "high quality" mode and move on.
2499   That would probably be nice, but unfortunately hard to implement as different
2500   encoding options yield different quality results depending on the source material.
2501   That is because compression depends on the visual properties of the video
2502   in question.
2503   For example, anime and live action have very different properties and
2504   thus require different options to obtain optimum encoding.
2505   The good news is that some options should never be left out, like
2506   <option>mbd=2</option>, <option>trell</option>, and <option>v4mv</option>.
2507   See below for a detailed description of common encoding options.
2508 </para>
2511 <itemizedlist>
2512 <title>Options to adjust:</title>
2513 <listitem><para>
2514   <emphasis role="bold">vmax_b_frames</emphasis>: 1 or 2 is good, depending on
2515   the movie.
2516   Note that if you need to have your encode be decodable by DivX5, you
2517   need to activate closed GOP support, using
2518   <systemitem class="library">libavcodec</systemitem>'s <option>cgop</option>
2519   option, but you need to deactivate scene detection, which
2520   is not a good idea as it will hurt encode efficiency a bit.
2521 </para></listitem>
2523 <listitem><para>
2524   <emphasis role="bold">vb_strategy=1</emphasis>: helps in high-motion scenes.
2525   On some videos, vmax_b_frames may hurt quality, but vmax_b_frames=2 along
2526   with vb_strategy=1 helps.
2527 </para></listitem>
2529 <listitem><para>
2530   <emphasis role="bold">dia</emphasis>: motion search range. Bigger is better
2531   and slower.
2532   Negative values are a completely different scale.
2533   Good values are -1 for a fast encode, or 2-4 for slower.
2534 </para></listitem>
2536 <listitem><para>
2537   <emphasis role="bold">predia</emphasis>: motion search pre-pass.
2538   Not as important as dia. Good values are 1 (default) to 4. Requires preme=2
2539   to really be useful.
2540 </para></listitem>
2542 <listitem><para>
2543   <emphasis role="bold">cmp, subcmp, precmp</emphasis>: Comparison function for
2544   motion estimation.
2545   Experiment with values of 0 (default), 2 (hadamard), 3 (dct), and 6 (rate
2546   distortion).
2547   0 is fastest, and sufficient for precmp.
2548   For cmp and subcmp, 2 is good for anime, and 3 is good for live action.
2549   6 may or may not be slightly better, but is slow.
2550 </para></listitem>
2552 <listitem><para>
2553   <emphasis role="bold">last_pred</emphasis>: Number of motion predictors to
2554   take from the previous frame.
2555   1-3 or so help at little speed cost.
2556   Higher values are slow for no extra gain.
2557 </para></listitem>
2559 <listitem><para>
2560   <emphasis role="bold">cbp, mv0</emphasis>: Controls the selection of macroblocks.
2561   Small speed cost for small quality gain.
2562 </para></listitem>
2564 <listitem><para>
2565   <emphasis role="bold">qprd</emphasis>: adaptive quantization based on the
2566   macroblock's complexity.
2567   May help or hurt depending on the video and other options.
2568   This can cause artifacts unless you set vqmax to some reasonably small value
2569   (6 is good, maybe as low as 4); vqmin=1 should also help.
2570 </para></listitem>
2572 <listitem><para>
2573   <emphasis role="bold">qns</emphasis>: very slow, especially when combined
2574   with qprd.
2575   This option will make the encoder minimize noise due to compression
2576   artifacts instead of making the encoded video strictly match the source.
2577   Do not use this unless you have already tweaked everything else as far as it
2578   will go and the results still are not good enough.
2579 </para></listitem>
2581 <listitem><para>
2582   <emphasis role="bold">vqcomp</emphasis>: Tweak ratecontrol.
2583   What values are good depends on the movie.
2584   You can safely leave this alone if you want.
2585   Reducing vqcomp puts more bits on low-complexity scenes, increasing it puts
2586   them on high-complexity scenes (default: 0.5, range: 0-1. recommended range:
2587   0.5-0.7).
2588 </para></listitem>
2590 <listitem><para>
2591   <emphasis role="bold">vlelim, vcelim</emphasis>: Sets the single coefficient
2592   elimination threshold for luminance and chroma planes.
2593   These are encoded separately in all MPEG-like algorithms.
2594   The idea behind these options is to use some good heuristics to determine
2595   when the change in a block is less than the threshold you specify, and in
2596   such a case, to just  encode the block as "no change".
2597   This saves bits and perhaps speeds up encoding. vlelim=-4 and vcelim=9
2598   seem to be good for live movies, but seem not to help with anime;
2599   when encoding animation, you should probably leave them unchanged.
2600 </para></listitem>
2602 <listitem><para>
2603   <emphasis role="bold">qpel</emphasis>: Quarter pixel motion estimation.
2604   MPEG-4 uses half pixel precision for its motion search by default,
2605   therefore this option comes with an overhead as more information will be
2606   stored in the encoded file.
2607   The compression gain/loss depends on the movie, but it is usually not very
2608   effective on anime.
2609   qpel always incurs a significant cost in CPU decode time (+25% in
2610   practice).
2611 </para></listitem>
2613 <listitem><para>
2614   <emphasis role="bold">psnr</emphasis>: does not affect the actual encoding,
2615   but writes a log file giving the type/size/quality of each frame, and
2616   prints a summary of PSNR (Peak Signal to Noise Ratio) at the end.
2617 </para></listitem>
2619 </itemizedlist>
2621 <itemizedlist>
2622 <title>Options not recommended to play with:</title>
2623 <listitem><para>
2624   <emphasis role="bold">vme</emphasis>: The default is best.
2625 </para></listitem>
2627 <listitem><para>
2628   <emphasis role="bold">lumi_mask, dark_mask</emphasis>: Psychovisual adaptive
2629   quantization.
2630   You do not want to play with those options if you care about quality.
2631   Reasonable values may be effective in your case, but be warned this is very
2632   subjective.
2633 </para></listitem>
2635 <listitem><para>
2636   <emphasis role="bold">scplx_mask</emphasis>: Tries to prevent blocky
2637   artifacts, but postprocessing is better.
2638 </para></listitem>
2639 </itemizedlist>
2640 </sect2>
2642 <sect2 id="menc-feat-mpeg4-lavc-example-settings">
2643 <title>Encoding setting examples</title>
2645 <para>
2646   The following settings are examples of different encoding
2647   option combinations that affect the speed vs quality tradeoff
2648   at the same target bitrate.
2649 </para>
2651 <para>
2652   All the encoding settings were tested on a 720x448 @30000/1001 fps
2653   video sample, the target bitrate was 900kbps, and the machine was an
2654   AMD-64 3400+ at 2400 Mhz in 64 bits mode.
2655   Each encoding setting features the measured encoding speed (in
2656   frames per second) and the PSNR loss (in dB) compared to the "very
2657   high quality" setting.
2658   Please understand that depending on your source, your machine type
2659   and development advancements, you may get very different results.
2660 </para>
2662 <para>
2663 <informaltable frame="all">
2664 <tgroup cols="4">
2665 <thead>
2666 <row><entry>Description</entry><entry>Encoding options</entry><entry>speed (in fps)</entry><entry>Relative PSNR loss (in dB)</entry></row>
2667 </thead>
2668 <tbody>
2669 <row>
2670   <entry>Very high quality</entry>
2671   <entry><option>vcodec=mpeg4:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:predia=2:dia=2:vmax_b_frames=2:vb_strategy=1:precmp=2:cmp=2:subcmp=2:preme=2:qns=2</option></entry>
2672   <entry>6fps</entry>
2673   <entry>0dB</entry>
2674 </row>
2675 <row>
2676   <entry>High quality</entry>
2677   <entry><option>vcodec=mpeg4:mbd=2:trell:v4mv:last_pred=2:dia=-1:vmax_b_frames=2:vb_strategy=1:cmp=3:subcmp=3:precmp=0:vqcomp=0.6:turbo</option></entry>
2678   <entry>15fps</entry>
2679   <entry>-0.5dB</entry>
2680 </row>
2681 <row>
2682   <entry>Fast</entry>
2683   <entry><option>vcodec=mpeg4:mbd=2:trell:v4mv:turbo</option></entry>
2684   <entry>42fps</entry>
2685   <entry>-0.74dB</entry>
2686 </row>
2687 <row>
2688   <entry>Realtime</entry>
2689   <entry><option>vcodec=mpeg4:mbd=2:turbo</option></entry>
2690   <entry>54fps</entry>
2691   <entry>-1.21dB</entry>
2692 </row>
2693 </tbody>
2694 </tgroup>
2695 </informaltable>
2696 </para>
2697 </sect2>
2699 <sect2 id="custommatrices"><title>Custom inter/intra matrices</title>
2701 <para>
2702 With this feature of
2703 <link linkend="ffmpeg"><systemitem class="library">libavcodec</systemitem></link>
2704 you are able to set custom inter (I-frames/keyframes) and intra
2705 (P-frames/predicted frames) matrices. It is supported by many of the codecs:
2706 <systemitem>mpeg1video</systemitem> and <systemitem>mpeg2video</systemitem>
2707 are reported as working.
2708 </para>
2710 <para>
2711 A typical usage of this feature is to set the matrices preferred by the
2712 <ulink url="http://www.kvcd.net/">KVCD</ulink> specifications.
2713 </para>
2715 <para>
2716 The <emphasis role="bold">KVCD &quot;Notch&quot; Quantization Matrix:</emphasis>
2717 </para>
2719 <para>
2720 Intra:
2721 <screen>
2722  8  9 12 22 26 27 29 34
2723  9 10 14 26 27 29 34 37
2724 12 14 18 27 29 34 37 38
2725 22 26 27 31 36 37 38 40
2726 26 27 29 36 39 38 40 48
2727 27 29 34 37 38 40 48 58
2728 29 34 37 38 40 48 58 69
2729 34 37 38 40 48 58 69 79
2730 </screen>
2732 Inter:
2733 <screen>
2734 16 18 20 22 24 26 28 30
2735 18 20 22 24 26 28 30 32
2736 20 22 24 26 28 30 32 34
2737 22 24 26 30 32 32 34 36
2738 24 26 28 32 34 34 36 38
2739 26 28 30 32 34 36 38 40
2740 28 30 32 34 36 38 42 42
2741 30 32 34 36 38 40 42 44
2742 </screen>
2743 </para>
2745 <para>
2746 Usage:
2747 <screen>
2748 $ mencoder <replaceable>input.avi</replaceable> -o <replaceable>output.avi</replaceable> -oac copy -ovc lavc -lavcopts inter_matrix=...:intra_matrix=...
2749 </screen>
2750 </para>
2752 <para>
2753 <screen>
2754 $ mencoder <replaceable>input.avi</replaceable> -ovc lavc -lavcopts
2755 vcodec=mpeg2video:intra_matrix=8,9,12,22,26,27,29,34,9,10,14,26,27,29,34,37,
2756 12,14,18,27,29,34,37,38,22,26,27,31,36,37,38,40,26,27,29,36,39,38,40,48,27,
2757 29,34,37,38,40,48,58,29,34,37,38,40,48,58,69,34,37,38,40,48,58,69,79
2758 :inter_matrix=16,18,20,22,24,26,28,30,18,20,22,24,26,28,30,32,20,22,24,26,
2759 28,30,32,34,22,24,26,30,32,32,34,36,24,26,28,32,34,34,36,38,26,28,30,32,34,
2760 36,38,40,28,30,32,34,36,38,42,42,30,32,34,36,38,40,42,44 -oac copy -o svcd.mpg
2761 </screen>
2762 </para>
2763 </sect2>
2766 <sect2 id="menc-feat-dvd-mpeg4-example">
2767 <title>Example</title>
2769 <para>
2770   So, you have just bought your shiny new copy of Harry Potter and the Chamber
2771   of Secrets (widescreen edition, of course), and you want to rip this DVD
2772   so that you can add it to your Home Theatre PC. This is a region 1 DVD,
2773   so it is NTSC. The example below will still apply to PAL, except you will
2774   omit <option>-ofps 24000/1001</option> (because the output framerate is the
2775   same as the input framerate), and of course the crop dimensions will be
2776   different.
2777 </para>
2779 <para>
2780   After running <option>mplayer dvd://1</option>, we follow the process
2781   detailed in the section <link linkend="menc-feat-telecine">How to deal
2782   with telecine and interlacing in NTSC DVDs</link> and discover that it is
2783   24000/1001 fps progressive video, which means that we need not use an inverse
2784   telecine filter, such as <option>pullup</option> or
2785   <option>filmdint</option>.
2786 </para>
2788 <para>
2789   Next, we want to determine the appropriate crop rectangle, so we use the
2790   cropdetect filter:
2792   <screen>mplayer dvd://1 -vf cropdetect</screen>
2794   Make sure you seek to a fully filled frame (such as a bright scene), and
2795   you will see in <application>MPlayer</application>'s console output:
2797   <screen>crop area: X: 0..719  Y: 57..419  (-vf crop=720:362:0:58)</screen>
2799   We then play the movie back with this filter to test its correctness:
2801   <screen>mplayer dvd://1 -vf crop=720:362:0:58</screen>
2803   And we see that it looks perfectly fine. Next, we ensure the width and
2804   height are a multiple of 16. The width is fine, however the height is
2805   not. Since we did not fail 7th grade math, we know that the nearest
2806   multiple of 16 lower than 362 is 352.
2807 </para>
2809 <para>
2810   We could just use <option>crop=720:352:0:58</option>, but it would be nice
2811   to take a little off the top and a little off the bottom so that we
2812   retain the center. We have shrunk the height by 10 pixels, but we do not
2813   want to increase the y-offset by 5-pixels since that is an odd number and
2814   will adversely affect quality. Instead, we will increase the y-offset by
2815   4 pixels:
2817   <screen>mplayer dvd://1 -vf crop=720:352:0:62</screen>
2819   Another reason to shave pixels from both the top and the bottom is that we
2820   ensure we have eliminated any half-black pixels if they exist. Note that if
2821   your video is telecined, make sure the <option>pullup</option> filter (or
2822   whichever inverse telecine filter you decide to use) appears in the filter
2823   chain before you crop. If it is interlaced, deinterlace before cropping.
2824   (If you choose to preserve the interlaced video, then make sure your
2825   vertical crop offset is a multiple of 4.)
2826 </para>
2828 <para>
2829   If you are really concerned about losing those 10 pixels, you might
2830   prefer instead to scale the dimensions down to the nearest multiple of 16.
2831   The filter chain would look like:
2833   <screen>-vf crop=720:362:0:58,scale=720:352</screen>
2835   Scaling the video down like this will mean that some small amount of
2836   detail is lost, though it probably will not be perceptible. Scaling up will
2837   result in lower quality (unless you increase the bitrate). Cropping
2838   discards those pixels altogether. It is a tradeoff that you will want to
2839   consider for each circumstance. For example, if the DVD video was made
2840   for television, you might want to avoid vertical scaling, since the line
2841   sampling corresponds to the way the content was originally recorded.
2842 </para>
2844 <para>
2845   On inspection, we see that our movie has a fair bit of action and high
2846   amounts of detail, so we pick 2400Kbit for our bitrate.
2847 </para>
2849 <para>
2850   We are now ready to do the two pass encode. Pass one:
2852   <screen>mencoder dvd://1 -ofps 24000/1001 -oac copy -vf pullup,softskip,crop=720:352:0:62,hqdn3d=2:1:2 -ovc lavc \
2853 -lavcopts vcodec=mpeg4:vbitrate=2400:v4mv:mbd=2:trell:cmp=3:subcmp=3:mbcmp=3:autoaspect:vpass=1 \
2854 -o Harry_Potter_2.avi</screen>
2856   And pass two is the same, except that we specify <option>vpass=2</option>:
2858   <screen>mencoder dvd://1 -ofps 24000/1001 -oac copy -vf pullup,softskip,crop=720:352:0:62,hqdn3d=2:1:2 -ovc lavc \
2859 -lavcopts vcodec=mpeg4:vbitrate=2400:v4mv:mbd=2:trell:cmp=3:subcmp=3:mbcmp=3:autoaspect:vpass=2 \
2860 -o Harry_Potter_2.avi</screen>
2861 </para>
2863 <para>
2864   The options <option>v4mv:mbd=2:trell</option> will greatly increase the
2865   quality at the expense of encoding time. There is little reason to leave
2866   these options out when the primary goal is quality. The options
2867   <option>cmp=3:subcmp=3:mbcmp=3</option> select a comparison function that
2868   yields higher quality than the defaults. You might try experimenting with
2869   this parameter (refer to the man page for the possible values) as
2870   different functions can have a large impact on quality depending on the
2871   source material. For example, if you find
2872   <systemitem class="library">libavcodec</systemitem> produces too much
2873   blocky artifacting, you could try selecting the experimental NSSE as
2874   comparison function via <option>*cmp=10</option>.
2875 </para>
2877 <para>
2878   For this movie, the resulting AVI will be 138 minutes long and nearly
2879   3GB. And because you said that file size does not matter, this is a
2880   perfectly acceptable size. However, if you had wanted it smaller, you
2881   could try a lower bitrate. Increasing bitrates have diminishing
2882   returns, so while we might clearly see an improvement from 1800Kbit to
2883   2000Kbit, it might not be so noticeable above 2000Kbit. Feel
2884   free to experiment until you are happy.
2885 </para>
2887 <para>
2888   Because we passed the source video through a denoise filter, you may want
2889   to add some of it back during playback. This, along with the
2890   <option>spp</option> post-processing filter, drastically improves the
2891   perception of quality and helps eliminate blocky artifacts in the video.
2892   With <application>MPlayer</application>'s <option>autoq</option> option,
2893   you can vary the amount of post-processing done by the spp filter
2894   depending on available CPU. Also, at this point, you may want to apply
2895   gamma and/or color correction to best suit your display. For example:
2897   <screen>mplayer Harry_Potter_2.avi -vf spp,noise=9ah:5ah,eq2=1.2 -autoq 3</screen>
2899 </para>
2900 </sect2>
2901 </sect1>
2904 <sect1 id="menc-feat-xvid">
2905 <title>Encoding with the <systemitem class="library">XviD</systemitem>
2906 codec</title>
2907 <para>
2908  <systemitem class="library">XviD</systemitem> is a free library for
2909  encoding MPEG-4 ASP video streams.
2910  Before starting to encode, you need to <link linkend="xvid">
2911  set up <application>MEncoder</application> to support it</link>.
2912 </para>
2913 <para>
2914  This guide mainly aims at featuring the same kind of information
2915  as x264's encoding guide.
2916  Therefore, please begin by reading
2917  <link linkend="menc-feat-x264-encoding-options-intro">the first part</link>
2918  of that guide.
2919 </para>
2922 <sect2 id="menc-feat-xvid-intro">
2923 <title>What options should I use to get the best results?</title>
2925 <para>
2926  Please begin by reviewing the
2927  <systemitem class="library">XviD</systemitem> section of
2928  <application>MPlayer</application>'s man page.
2929  This section is intended to be a supplement to the man page.
2930 </para>
2931 <para>
2932  The XviD default settings are already a good tradeoff between
2933  speed and quality, therefore you can safely stick to them if
2934  the following section puzzles you.
2935 </para>
2936 </sect2>
2938 <sect2 id="menc-feat-xvid-encoding-options">
2939 <title>Encoding options of <systemitem class="library">XviD</systemitem></title>
2941 <itemizedlist>
2942 <listitem><para>
2943  <emphasis role="bold">vhq</emphasis>
2944   This setting affects the macroblock decision algorithm, where the
2945   higher the setting, the wiser the decision.
2946   The default setting may be safely used for every encode, while
2947   higher settings always help PSNR but are significantly slower.
2948   Please note that a better PSNR does not necessarily mean
2949   that the picture will look better, but tells you that it is
2950   closer to the original.
2951   Turning it off will noticeably speed up encoding; if speed is
2952   critical for you, the tradeoff may be worth it.
2953 </para></listitem>
2955 <listitem><para>
2956  <emphasis role="bold">bvhq</emphasis>
2957   This does the same job as vhq, but does it on B-frames.
2958   It has a negligible impact on speed, and slightly improves quality
2959   (around +0.1dB PSNR).
2960 </para></listitem>
2962 <listitem><para>
2963  <emphasis role="bold">max_bframes</emphasis>
2964   A higher number of consecutive allowed B-frames usually improves
2965   compressibility, although it may also lead to more blocking artifacts.
2966   The default setting is a good tradeoff between compressibility and
2967   quality, but you may increase it up to 3 if you are bitrate-starved.
2968   You may also decrease it to 1 or 0 if you are aiming at perfect
2969   quality, though in that case you should make sure your
2970   target bitrate is high enough to ensure that the encoder does not
2971   have to increase quantizers to reach it.
2972 </para></listitem>
2974 <listitem><para>
2975  <emphasis role="bold">bf_threshold</emphasis>
2976   This controls the B-frame sensitivity of the encoder, where a higher
2977   value leads to more B-frames being used (and vice versa).
2978   This setting is to be used together with <option>max_bframes</option>;
2979   if you are bitrate-starved, you should increase both
2980   <option>max_bframes</option> and <option>bf_threshold</option>,
2981   while you may increase <option>max_bframes</option> and reduce
2982   <option>bf_threshold</option> so that the encoder may use more
2983   B-frames in places that only <emphasis role="bold">really</emphasis>
2984   need them.
2985   A low number of <option>max_bframes</option> and a high value of
2986   <option>bf_threshold</option> is probably not a wise choice as it
2987   will force the encoder to put B-frames in places that would not
2988   benefit from them, therefore reducing visual quality.
2989   However, if you need to be compatible with standalone players that
2990   only support old DivX profiles (which only supports up to 1
2991   consecutive B-frame), this would be your only way to
2992   increase compressibility through using B-frames.
2993 </para></listitem>
2995 <listitem><para>
2996  <emphasis role="bold">trellis</emphasis>
2997   Optimizes the quantization process to get an optimal tradeoff
2998   between PSNR and bitrate, which allows significant bit saving.
2999   These bits will in return be spent elsewhere on the video,
3000   raising overall visual quality.
3001   You should always leave it on as its impact on quality is huge.
3002   Even if you are looking for speed, do not disable it until you
3003   have turned down <option>vhq</option> and all other more
3004   CPU-hungry options to the minimum.
3005 </para></listitem>
3007 <listitem><para>
3008   <emphasis role="bold">hq_ac</emphasis>
3009   Activates a better coefficient cost estimation method, which slightly
3010   reduces filesize by around 0.15 to 0.19% (which corresponds to less
3011   than 0.01dB PSNR increase), while having a negligible impact on speed.
3012   It is therefore recommended to always leave it on.
3013 </para></listitem>
3015 <listitem><para>
3016  <emphasis role="bold">cartoon</emphasis>
3017   Designed to better encode cartoon content, and has no impact on
3018   speed as it just tunes the mode decision heuristics for this type
3019   of content.
3020 </para></listitem>
3022 <listitem><para>
3023  <emphasis role="bold">me_quality</emphasis>
3024   This setting is to control the precision of the motion estimation.
3025   The higher <option>me_quality</option>, the more
3026   precise the estimation of the original motion will be, and the
3027   better the resulting clip will capture the original motion.
3028  </para>
3029  <para>
3030   The default setting is best in all cases;
3031   thus it is not recommended to turn it down unless you are
3032   really looking for speed, as all the bits saved by a good motion
3033   estimation would be spent elsewhere, raising overall quality.
3034   Therefore, do not go any lower than 5, and even that only as a last
3035   resort.
3036 </para></listitem>
3038 <listitem><para>
3039  <emphasis role="bold">chroma_me</emphasis>
3040   Improves motion estimation by also taking the chroma (color)
3041   information into account, whereas <option>me_quality</option>
3042   alone only uses luma (grayscale).
3043   This slows down encoding by 5-10% but improves visual quality
3044   quite a bit by reducing blocking effects and reduces filesize by
3045   around 1.3%.
3046   If you are looking for speed, you should disable this option before
3047   starting to consider reducing <option>me_quality</option>.
3048 </para></listitem>
3050 <listitem><para>
3051   <emphasis role="bold">chroma_opt</emphasis>
3052     Is intended to increase chroma image quality around pure
3053     white/black edges, rather than improving compression.
3054     This can help to reduce the "red stairs" effect.
3055 </para></listitem>
3057 <listitem><para>
3058   <emphasis role="bold">lumi_mask</emphasis>
3059     Tries to give less bitrate to part of the picture that the
3060     human eye cannot see very well, which should allow the encoder
3061     to spend the saved bits on more important parts of the picture.
3062     The quality of the encode yielded by this option highly depends
3063     on personal preferences and on the type and monitor settings
3064     used to watch it (typically, it will not look as good if it is
3065     bright or if it is a TFT monitor).
3066 </para></listitem>
3068 <listitem><para>
3069   <emphasis role="bold">qpel</emphasis>
3070     Raise the number of candidate motion vectors by increasing
3071     the precision of the motion estimation from halfpel to
3072     quarterpel.
3073     The idea is to find better motion vectors which will in return
3074     reduce bitrate (hence increasing quality).
3075     However, motion vectors with quarterpel precision require a
3076     few extra bits to code, but the candidate vectors do not always
3077     give (much) better results.
3078     Quite often, the codec still spends bits on the extra precision,
3079     but little or no extra quality is gained in return.
3080     Unfortunately, there is no way to foresee the possible gains of
3081     <option>qpel</option>, so you need to actually encode with and
3082     without it to know for sure.
3083   </para><para>
3084     <option>qpel</option> can be almost double encoding time, and
3085     requires as much as 25% more processing power to decode.
3086     It is not supported by all standalone players.
3087 </para></listitem>
3089 <listitem><para>
3090   <emphasis role="bold">gmc</emphasis>
3091     Tries to save bits on panning scenes by using a single motion
3092     vector for the whole frame.
3093     This almost always raises PSNR, but significantly slows down
3094     encoding (as well as decoding).
3095     Therefore, you should only use it when you have turned
3096     <option>vhq</option> to the maximum.
3097     <systemitem class="library">XviD</systemitem>'s GMC is more
3098     sophisticated than DivX's, but is only supported by few
3099     standalone players.
3100 </para></listitem>
3102 </itemizedlist>
3103 </sect2>
3105 <sect2 id="menc-feat-xvid-encoding-profiles">
3106 <title>Encoding profiles</title>
3107 <para>
3108   XviD supports encoding profiles through the <option>profile</option> option,
3109   which are used to impose restrictions on the properties of the XviD video
3110   stream such that it will be playable on anything which supports the
3111   chosen profile.
3112   The restrictions relate to resolutions, bitrates and certain MPEG-4
3113   features.
3114   The following table shows what each profile supports.
3115 </para>
3116 <informaltable>
3117 <tgroup cols="16" align="center">
3118 <colspec colnum="1"  colname="col1"/>
3119 <colspec colnum="2"  colname="col2"/>
3120 <colspec colnum="3"  colname="col3"/>
3121 <colspec colnum="4"  colname="col4"/>
3122 <colspec colnum="5"  colname="col5"/>
3123 <colspec colnum="6"  colname="col6"/>
3124 <colspec colnum="7"  colname="col7"/>
3125 <colspec colnum="8"  colname="col8"/>
3126 <colspec colnum="9"  colname="col9"/>
3127 <colspec colnum="10" colname="col10"/>
3128 <colspec colnum="11" colname="col11"/>
3129 <colspec colnum="12" colname="col12"/>
3130 <colspec colnum="13" colname="col13"/>
3131 <colspec colnum="14" colname="col14"/>
3132 <colspec colnum="15" colname="col15"/>
3133 <colspec colnum="16" colname="col16"/>
3134 <colspec colnum="17" colname="col17"/>
3135 <spanspec spanname="spa2-5"   namest="col2" nameend="col5"/>
3136 <spanspec spanname="spa6-11"  namest="col6" nameend="col11"/>
3137 <spanspec spanname="spa12-17" namest="col12" nameend="col17"/>
3138   <tbody>
3139     <row>
3140       <entry></entry>
3141       <entry spanname="spa2-5">Simple</entry>
3142       <entry spanname="spa6-11">Advanced Simple</entry>
3143       <entry spanname="spa12-17">DivX</entry>
3144     </row>
3145     <row>
3146       <entry>Profile name</entry>
3147       <entry>0</entry>
3148       <entry>1</entry>
3149       <entry>2</entry>
3150       <entry>3</entry>
3151       <entry>0</entry>
3152       <entry>1</entry>
3153       <entry>2</entry>
3154       <entry>3</entry>
3155       <entry>4</entry>
3156       <entry>5</entry>
3157       <entry>Handheld</entry>
3158       <entry>Portable NTSC</entry>
3159       <entry>Portable PAL</entry>
3160       <entry>Home Theater NTSC</entry>
3161       <entry>Home Theater PAL</entry>
3162       <entry>HDTV</entry>
3163     </row>
3164     <row>
3165       <entry>Width [pixels]</entry>
3166       <entry>176</entry>
3167       <entry>176</entry>
3168       <entry>352</entry>
3169       <entry>352</entry>
3170       <entry>176</entry>
3171       <entry>176</entry>
3172       <entry>352</entry>
3173       <entry>352</entry>
3174       <entry>352</entry>
3175       <entry>720</entry>
3176       <entry>176</entry>
3177       <entry>352</entry>
3178       <entry>352</entry>
3179       <entry>720</entry>
3180       <entry>720</entry>
3181       <entry>1280</entry>
3182     </row>
3183     <row>
3184       <entry>Height [pixels]</entry>
3185       <entry>144</entry>
3186       <entry>144</entry>
3187       <entry>288</entry>
3188       <entry>288</entry>
3189       <entry>144</entry>
3190       <entry>144</entry>
3191       <entry>288</entry>
3192       <entry>288</entry>
3193       <entry>576</entry>
3194       <entry>576</entry>
3195       <entry>144</entry>
3196       <entry>240</entry>
3197       <entry>288</entry>
3198       <entry>480</entry>
3199       <entry>576</entry>
3200       <entry>720</entry>
3201     </row>
3202     <row>
3203       <entry>Frame rate [fps]</entry>
3204       <entry>15</entry>
3205       <entry>15</entry>
3206       <entry>15</entry>
3207       <entry>15</entry>
3208       <entry>30</entry>
3209       <entry>30</entry>
3210       <entry>15</entry>
3211       <entry>30</entry>
3212       <entry>30</entry>
3213       <entry>30</entry>
3214       <entry>15</entry>
3215       <entry>30</entry>
3216       <entry>25</entry>
3217       <entry>30</entry>
3218       <entry>25</entry>
3219       <entry>30</entry>
3220     </row>
3221     <row>
3222       <entry>Max average bitrate [kbps]</entry>
3223       <entry>64</entry>
3224       <entry>64</entry>
3225       <entry>128</entry>
3226       <entry>384</entry>
3227       <entry>128</entry>
3228       <entry>128</entry>
3229       <entry>384</entry>
3230       <entry>768</entry>
3231       <entry>3000</entry>
3232       <entry>8000</entry>
3233       <entry>537.6</entry>
3234       <entry>4854</entry>
3235       <entry>4854</entry>
3236       <entry>4854</entry>
3237       <entry>4854</entry>
3238       <entry>9708.4</entry>
3239     </row>
3240     <row>
3241       <entry>Peak average bitrate over 3 secs [kbps]</entry>
3242       <entry></entry>
3243       <entry></entry>
3244       <entry></entry>
3245       <entry></entry>
3246       <entry></entry>
3247       <entry></entry>
3248       <entry></entry>
3249       <entry></entry>
3250       <entry></entry>
3251       <entry></entry>
3252       <entry>800</entry>
3253       <entry>8000</entry>
3254       <entry>8000</entry>
3255       <entry>8000</entry>
3256       <entry>8000</entry>
3257       <entry>16000</entry>
3258     </row>
3259     <row>
3260       <entry>Max. B-frames</entry>
3261       <entry>0</entry>
3262       <entry>0</entry>
3263       <entry>0</entry>
3264       <entry>0</entry>
3265       <entry></entry>
3266       <entry></entry>
3267       <entry></entry>
3268       <entry></entry>
3269       <entry></entry>
3270       <entry></entry>
3271       <entry>0</entry>
3272       <entry>1</entry>
3273       <entry>1</entry>
3274       <entry>1</entry>
3275       <entry>1</entry>
3276       <entry>2</entry>
3277     </row>
3278     <row>
3279       <entry>MPEG quantization</entry>
3280       <entry></entry>
3281       <entry></entry>
3282       <entry></entry>
3283       <entry></entry>
3284       <entry>X</entry>
3285       <entry>X</entry>
3286       <entry>X</entry>
3287       <entry>X</entry>
3288       <entry>X</entry>
3289       <entry>X</entry>
3290       <entry></entry>
3291       <entry></entry>
3292       <entry></entry>
3293       <entry></entry>
3294       <entry></entry>
3295       <entry></entry>
3296     </row>
3297     <row>
3298       <entry>Adaptive quantization</entry>
3299       <entry></entry>
3300       <entry></entry>
3301       <entry></entry>
3302       <entry></entry>
3303       <entry>X</entry>
3304       <entry>X</entry>
3305       <entry>X</entry>
3306       <entry>X</entry>
3307       <entry>X</entry>
3308       <entry>X</entry>
3309       <entry>X</entry>
3310       <entry>X</entry>
3311       <entry>X</entry>
3312       <entry>X</entry>
3313       <entry>X</entry>
3314       <entry>X</entry>
3315     </row>
3316     <row>
3317       <entry>Interlaced encoding</entry>
3318       <entry></entry>
3319       <entry></entry>
3320       <entry></entry>
3321       <entry></entry>
3322       <entry>X</entry>
3323       <entry>X</entry>
3324       <entry>X</entry>
3325       <entry>X</entry>
3326       <entry>X</entry>
3327       <entry>X</entry>
3328       <entry></entry>
3329       <entry></entry>
3330       <entry></entry>
3331       <entry>X</entry>
3332       <entry>X</entry>
3333       <entry>X</entry>
3334     </row>
3335     <row>
3336       <entry>Quaterpixel</entry>
3337       <entry></entry>
3338       <entry></entry>
3339       <entry></entry>
3340       <entry></entry>
3341       <entry>X</entry>
3342       <entry>X</entry>
3343       <entry>X</entry>
3344       <entry>X</entry>
3345       <entry>X</entry>
3346       <entry>X</entry>
3347       <entry></entry>
3348       <entry></entry>
3349       <entry></entry>
3350       <entry></entry>
3351       <entry></entry>
3352       <entry></entry>
3353     </row>
3354     <row>
3355       <entry>Global motion compensation</entry>
3356       <entry></entry>
3357       <entry></entry>
3358       <entry></entry>
3359       <entry></entry>
3360       <entry>X</entry>
3361       <entry>X</entry>
3362       <entry>X</entry>
3363       <entry>X</entry>
3364       <entry>X</entry>
3365       <entry>X</entry>
3366       <entry></entry>
3367       <entry></entry>
3368       <entry></entry>
3369       <entry></entry>
3370       <entry></entry>
3371       <entry></entry>
3372     </row>
3373   </tbody>
3374 </tgroup>
3375 </informaltable>
3376 </sect2>
3378 <sect2 id="menc-feat-xvid-example-settings">
3379 <title>Encoding setting examples</title>
3381 <para>
3382   The following settings are examples of different encoding
3383   option combinations that affect the speed vs quality tradeoff
3384   at the same target bitrate.
3385 </para>
3387 <para>
3388   All the encoding settings were tested on a 720x448 @30000/1001 fps
3389   video sample, the target bitrate was 900kbps, and the machine was an
3390   AMD-64 3400+ at 2400 Mhz in 64 bits mode.
3391   Each encoding setting features the measured encoding speed (in
3392   frames per second) and the PSNR loss (in dB) compared to the "very
3393   high quality" setting.
3394   Please understand that depending on your source, your machine type
3395   and development advancements, you may get very different results.
3396 </para>
3398 <para>
3399 <informaltable frame="all">
3400 <tgroup cols="4">
3401 <thead>
3402 <row><entry>Description</entry><entry>Encoding options</entry><entry>speed (in fps)</entry><entry>Relative PSNR loss (in dB)</entry></row>
3403 </thead>
3404 <tbody>
3405 <row>
3406   <entry>Very high quality</entry>
3407   <entry><option>chroma_opt:vhq=4:bvhq=1:quant_type=mpeg</option></entry>
3408   <entry>16fps</entry>
3409   <entry>0dB</entry>
3410 </row>
3411 <row>
3412   <entry>High quality</entry>
3413   <entry><option>vhq=2:bvhq=1:chroma_opt:quant_type=mpeg</option></entry>
3414   <entry>18fps</entry>
3415   <entry>-0.1dB</entry>
3416 </row>
3417 <row>
3418   <entry>Fast</entry>
3419   <entry><option>turbo:vhq=0</option></entry>
3420   <entry>28fps</entry>
3421   <entry>-0.69dB</entry>
3422 </row>
3423 <row>
3424   <entry>Realtime</entry>
3425   <entry><option>turbo:nochroma_me:notrellis:max_bframes=0:vhq=0</option></entry>
3426   <entry>38fps</entry>
3427   <entry>-1.48dB</entry>
3428 </row>
3429 </tbody>
3430 </tgroup>
3431 </informaltable>
3432 </para>
3433 </sect2>
3435 </sect1>
3437 <sect1 id="menc-feat-x264">
3438 <title>Encoding with the <systemitem class="library">x264</systemitem> codec</title>
3439 <para>
3440   <systemitem class="library">x264</systemitem> is a free library for
3441   encoding H.264/AVC video streams.
3442   Before starting to encode, you need to <link linkend="codec-x264-encode">
3443   set up <application>MEncoder</application> to support it</link>.
3444 </para>
3446 <sect2 id="menc-feat-x264-encoding-options">
3447 <title>Encoding options of x264</title>
3449 <para>
3450   Please begin by reviewing the
3451   <systemitem class="library">x264</systemitem> section of
3452   <application>MPlayer</application>'s man page.
3453   This section is intended to be a supplement to the man page.
3454   Here you will find quick hints about which options are most
3455   likely to interest most people. The man page is more terse,
3456   but also more exhaustive, and it sometimes offers much better
3457   technical detail.
3458 </para>
3460 <sect3 id="menc-feat-x264-encoding-options-intro">
3461 <title>Introduction</title>
3462 <para>This guide considers two major categories of encoding options:</para>
3464 <orderedlist>
3465   <listitem><para>Options which mainly trade off encoding time vs. quality
3466   </para></listitem>
3467   <listitem><para>Options which may be useful for fulfilling various personal
3468   preferences and special requirements</para></listitem>
3469 </orderedlist>
3471 <para>
3472   Ultimately, only you can decide which options are best for your
3473   purposes. The decision for the first class of options is the simplest:
3474   you only have to decide whether you think the quality differences
3475   justify the speed differences. For the second class of options,
3476   preferences may be far more subjective, and more factors may be
3477   involved. Note that some of the "personal preferences and special
3478   requirements" options can still have large impacts on speed or quality,
3479   but that is not what they are primarily useful for. A couple of the
3480   "personal preference" options may even cause changes that look better
3481   to some people, but look worse to others.
3482 </para>
3484 <para>
3485   Before continuing, you need to understand that this guide uses only one
3486   quality metric: global PSNR.
3487   For a brief explanation of what PSNR is, see
3488   <ulink url="http://en.wikipedia.org/wiki/PSNR">the Wikipedia article on PSNR</ulink>.
3489   Global PSNR is the last PSNR number reported when you include
3490   the <option>psnr</option> option in <option>x264encopts</option>.
3491   Any time you read a claim about PSNR, one of the assumptions
3492   behind the claim is that equal bitrates are used.
3493 </para>
3495 <para>
3496   Nearly all of this guide's comments assume you are using
3497   two pass.
3498   When comparing options, there are two major reasons for using
3499   two pass encoding.
3500   First, using two pass often gains around 1dB PSNR, which is a
3501   very big difference.
3502   Secondly, testing options by doing direct quality comparisons
3503   with one pass encodes introduces a major confounding
3504   factor: bitrate often varies significantly with each encode.
3505   It is not always easy to tell whether quality changes are due
3506   mainly to changed options, or if they mostly reflect essentially
3507   random differences in the achieved bitrate.
3508 </para>
3510 </sect3>
3512 <sect3 id="menc-feat-x264-encoding-options-speedvquality">
3513 <title>Options which primarily affect speed and quality</title>
3515 <itemizedlist>
3516 <listitem><para>
3517   <emphasis role="bold">subq</emphasis>:
3518   Of the options which allow you to trade off speed for quality,
3519   <option>subq</option> and <option>frameref</option> (see below) are usually
3520   by far the most important.
3521   If you are interested in tweaking either speed or quality, these
3522   are the first options you should consider.
3523   On the speed dimension, the <option>frameref</option> and
3524   <option>subq</option> options interact with each other fairly
3525   strongly.
3526   Experience shows that, with one reference frame,
3527   <option>subq=5</option> (the default setting) takes about 35% more time than
3528   <option>subq=1</option>.
3529   With 6 reference frames, the penalty grows to over 60%.
3530   <option>subq</option>'s effect on PSNR seems fairly constant
3531   regardless of the number of reference frames.
3532   Typically, <option>subq=5</option> achieves 0.2-0.5 dB higher global
3533   PSNR in comparison <option>subq=1</option>.
3534   This is usually enough to be visible.
3535 </para>
3536 <para>
3537   <option>subq=6</option> is the slowest, highest quality mode.
3538   In comparison to <option>subq=5</option>, it usually gains 0.1-0.4 dB
3539   global PSNR with speed costs varying from 25%-100%.
3540   Unlike other levels of <option>subq</option>, the behavior of
3541   <option>subq=6</option> does not depend much on <option>frameref</option>
3542   and <option>me</option>.  Instead, the effectiveness of <option>subq=6
3543   </option> depends mostly upon the number of B-frames used. In normal
3544   usage, this means <option>subq=6</option> has a large impact on both speed
3545   and quality in complex, high motion scenes, but it may not have much effect
3546   in low-motion scenes. Note that it is still recommended to always set
3547   <option>bframes</option> to something other than zero (see below).
3548 </para></listitem>
3549 <listitem><para>
3550   <emphasis role="bold">frameref</emphasis>:
3551   <option>frameref</option> is set to 1 by default, but this
3552   should not be taken to imply that it is reasonable to set it
3553   to 1.
3554   Merely raising <option>frameref</option> to 2 gains around
3555   0.15dB PSNR with a 5-10% speed penalty; this seems like a
3556   good tradeoff.
3557   <option>frameref=3</option> gains around 0.25dB PSNR over
3558   <option>frameref=1</option>, which should be a visible
3559   difference.
3560   <option>frameref=3</option> is around 15% slower than
3561   <option>frameref=1</option>.
3562   Unfortunately, diminishing returns set in rapidly.
3563   <option>frameref=6</option> can be expected to gain only
3564   0.05-0.1 dB over <option>frameref=3</option> at an additional
3565   15% speed penalty.
3566   Above <option>frameref=6</option>, the quality gains are
3567   usually very small (although you should keep in mind throughout
3568   this whole discussion that it can vary quite a lot depending on
3569   your source).
3570   In a fairly typical case, <option>frameref=12</option>
3571   will improve global PSNR by a tiny 0.02dB over
3572   <option>frameref=6</option>, at a speed cost of 15%-20%.
3573   At such high <option>frameref</option> values, the only really
3574   good thing that can be said is that increasing it even further will
3575   almost certainly never <emphasis role="bold">harm</emphasis>
3576   PSNR, but the additional quality benefits are barely even
3577   measurable, let alone perceptible.
3578 </para>
3579 <note><title>Note:</title>
3580 <para>
3581   Raising <option>frameref</option> to unnecessarily high values
3582   <emphasis role="bold">can</emphasis> and
3583   <emphasis role="bold">usually does</emphasis>
3584   hurt coding efficiency if you turn CABAC off.
3585   With CABAC on (the default behavior), the possibility of setting
3586   <option>frameref</option> "too high" currently seems too remote
3587   to even worry about, and in the future, optimizations may remove
3588   the possibility altogether.
3589 </para>
3590 </note>
3591 <para>
3592   If you care about speed, a reasonable compromise is to use low
3593   <option>subq</option> and <option>frameref</option> values on
3594   the first pass, and then raise them on the second pass.
3595   Typically, this has a negligible negative effect on the final
3596   quality: You will probably lose well under 0.1dB PSNR, which
3597   should be much too small of a difference to see.
3598   However, different values of <option>frameref</option> can
3599   occasionally affect frametype decision.
3600   Most likely, these are rare outlying cases, but if you want to
3601   be pretty sure, consider whether your video has either
3602   fullscreen repetitive flashing patterns or very large temporary
3603   occlusions which might force an I-frame.
3604   Adjust the first-pass <option>frameref</option> so it is large
3605   enough to contain the duration of the flashing cycle (or occlusion).
3606   For example, if the scene flashes back and forth between two images
3607   over a duration of three frames, set the first pass
3608   <option>frameref</option> to 3 or higher.
3609   This issue is probably extremely rare in live action video material,
3610   but it does sometimes come up in video game captures.
3611 </para></listitem>
3613 <listitem><para>
3614   <emphasis role="bold">me</emphasis>:
3615   This option is for choosing the motion estimation search method.
3616   Altering this option provides a straightforward quality-vs-speed
3617   tradeoff. <option>me=1</option> is only a few percent faster than
3618   the default search, at a cost of under 0.1dB global PSNR. The 
3619   default setting (<option>me=2</option>) is a reasonable tradeoff
3620   between speed and quality. <option>me=3</option> gains a little under
3621   0.1dB global PSNR, with a speed penalty that varies depending on
3622   <option>frameref</option>.  At high values of
3623   <option>frameref</option> (e.g. 12 or so), <option>me=3</option>
3624   is about 40% slower than the default <option> me=2</option>. With
3625   <option>frameref=3</option>, the speed penalty incurred drops to
3626   25%-30%.
3627 </para>
3628 <para>
3629   <option>me=4</option> uses an exhaustive search that is too slow for
3630   practical use.
3631 </para>
3632 </listitem>
3634 <listitem><para>
3635   <emphasis role="bold">4x4mv</emphasis>:
3636   This option enables the use of 8x4, 4x8 and 4x4 subpartitions in
3637   predicted macroblocks. Enabling it results in a fairly consistent
3638   10%-15% loss of speed. This option is rather useless in source
3639   containing only low motion, however in some high-motion source,
3640   particularly source with lots of small moving objects, gains of
3641   about 0.1dB can be expected.
3642 </para>
3643 </listitem>
3645 <listitem><para>
3646   <emphasis role="bold">bframes</emphasis>:
3647   If you are used to encoding with other codecs, you may have found
3648   that B-frames are not always useful.
3649   In H.264, this has changed: there are new techniques and block
3650   types that are possible in B-frames.
3651   Usually, even a naive B-frame choice algorithm can have a
3652   significant PSNR benefit.
3653   It is interesting to note that using B-frames usually speeds up
3654   the second pass somewhat, and may also speed up a single
3655   pass encode if adaptive B-frame decision is turned off.
3656 </para>
3657 <para>
3658   With adaptive B-frame decision turned off
3659   (<option>x264encopts</option>'s <option>nob_adapt</option>),
3660   the optimal value for this setting is usually no more than
3661   <option>bframes=1</option>, or else high-motion scenes can suffer.
3662   With adaptive B-frame decision on (the default behavior), it is
3663   safe to use higher values; the encoder will reduce the use of
3664   B-frames in scenes where they would hurt compression.
3665   The encoder rarely chooses to use more than 3 or 4 B-frames;
3666   setting this option any higher will have little effect.
3667 </para></listitem>
3669 <listitem><para>
3670   <emphasis role="bold">b_adapt</emphasis>:
3671   Note: This is on by default.
3672 </para>
3673 <para>
3674   With this option enabled, the encoder will use a reasonably fast
3675   decision process to reduce the number of B-frames used in scenes that
3676   might not benefit from them as much.
3677   You can use <option>b_bias</option> to tweak how B-frame-happy
3678   the encoder is.
3679   The speed penalty of adaptive B-frames is currently rather modest,
3680   but so is the potential quality gain.
3681   It usually does not hurt, however.
3682   Note that this only affects speed and frametype decision on the
3683   first pass.
3684   <option>b_adapt</option> and <option>b_bias</option> have no
3685   effect on subsequent passes.
3686 </para></listitem>
3688 <listitem><para>
3689   <emphasis role="bold">b_pyramid</emphasis>:
3690   You might as well enable this option if you are using >=2 B-frames;
3691   as the man page says, you get a little quality improvement at no
3692   speed cost.
3693   Note that these videos cannot be read by libavcodec-based decoders
3694   older than about March 5, 2005.
3695 </para></listitem>
3697 <listitem><para>
3698   <emphasis role="bold">weight_b</emphasis>:
3699   In typical cases, there is not much gain with this option.
3700   However, in crossfades or fade-to-black scenes, weighted
3701   prediction gives rather large bitrate savings.
3702   In MPEG-4 ASP, a fade-to-black is usually best coded as a series
3703   of expensive I-frames; using weighted prediction in B-frames
3704   makes it possible to turn at least some of these into much smaller
3705   B-frames.
3706   Encoding time cost is minimal, as no extra decisions need to be made.
3707   Also, contrary to what some people seem to guess, the decoder
3708   CPU requirements are not much affected by weighted prediction,
3709   all else being equal.
3710 </para>
3711 <para>
3712   Unfortunately, the current adaptive B-frame decision algorithm
3713   has a strong tendency to avoid B-frames during fades.
3714   Until this changes, it may be a good idea to add
3715   <option>nob_adapt</option> to your x264encopts, if you expect
3716   fades to have a large effect in your particular video
3717   clip.
3718 </para></listitem>
3719 </itemizedlist>
3720 </sect3>
3722 <sect3 id="menc-feat-x264-encoding-options-misc-preferences">
3723 <title>Options pertaining to miscellaneous preferences</title>
3724 <itemizedlist>
3725 <listitem><para>
3726   <emphasis role="bold">Two pass encoding</emphasis>:
3727   Above, it was suggested to always use two pass encoding, but there
3728   are still reasons for not using it. For instance, if you are capturing
3729   live TV and encoding in realtime, you are forced to use single-pass.
3730   Also, one pass is obviously faster than two passes; if you use the
3731   exact same set of options on both passes, two pass encoding is almost
3732   twice as slow.
3733 </para>
3734 <para>
3735   Still, there are very good reasons for using two pass encoding. For
3736   one thing, single pass ratecontrol is not psychic, and it often makes
3737   unreasonable choices because it cannot see the big picture. For example,
3738   suppose you have a two minute long video consisting of two distinct
3739   halves.  The first half is a very high-motion scene lasting 60 seconds
3740   which, in isolation, requires about 2500kbps in order to look decent.
3741   Immediately following it is a much less demanding 60-second scene
3742   that looks good at 300kbps. Suppose you ask for 1400kbps on the theory
3743   that this is enough to accomodate both scenes. Single pass ratecontrol
3744   will make a couple of "mistakes" in such a case. First of all, it
3745   will target 1400kbps in both segments. The first segment may end up
3746   heavily overquantized, causing it to look unacceptably and unreasonably
3747   blocky. The second segment will be heavily underquantized; it may look
3748   perfect, but the bitrate cost of that perfection will be completely
3749   unreasonable. What is even harder to avoid is the problem at the
3750   transition between the two scenes. The first seconds of the low motion
3751   half will be hugely over-quantized, because the ratecontrol is still
3752   expecting the kind of bitrate requirements it met in the first half
3753   of the video. This "error period" of heavily over-quantized low motion
3754   will look jarringly bad, and will actually use less than the 300kbps
3755   it would have taken to make it look decent. There are ways to
3756   mitigate the pitfalls of single-pass encoding, but they may tend to
3757   increase bitrate misprediction.
3758 </para>
3759 <para>
3760   Multipass ratecontrol can offer huge advantages over a single pass.
3761   Using the statistics gathered from the first pass encode, the encoder
3762   can estimate, with reasonable accuracy, the "cost" (in bits) of
3763   encoding any given frame, at any given quantizer. This allows for
3764   a much more rational, better planned allocation of bits between the
3765   expensive (high-motion) and cheap (low-motion) scenes. See
3766   <option>qcomp</option> below for some ideas on how to tweak this
3767   allocation to your liking.
3768 </para>
3769 <para>
3770   Moreover, two passes need not take twice as long as one pass. You can
3771   tweak the options in the first pass for higher speed and lower quality.
3772   If you choose your options well, you can get a very fast first pass.
3773   The resulting quality in the second pass will be slightly lower because size
3774   prediction is less accurate, but the quality difference is normally much
3775   too small to be visible. Try, for example, adding
3776   <option>subq=1:frameref=1</option> to the first pass
3777   <option>x264encopts</option>. Then, on the second pass, use slower,
3778   higher-quality options:
3779   <option>subq=6:frameref=15:4x4mv:me=3</option>
3780 </para></listitem>
3781 <listitem><para>
3782   <emphasis role="bold">Three pass encoding</emphasis>?
3784   x264 offers the ability to make an arbitrary number of consecutive
3785   passes. If you specify <option>pass=1</option> on the first pass,
3786   then use <option>pass=3</option> on a subsequent pass, the subsequent
3787   pass will both read the statistics from the previous pass, and write
3788   its own statistics. An additional pass following this one will have
3789   a very good base from which to make highly accurate predictions of
3790   framesizes at a chosen quantizer. In practice, the overall quality
3791   gain from this is usually close to zero, and quite possibly a third
3792   pass will result in slightly worse global PSNR than the pass before
3793   it. In typical usage, three passes help if you get either bad bitrate
3794   prediction or bad looking scene transitions when using only two passes.
3795   This is somewhat likely to happen on extremely short clips. There are
3796   also a few special cases in which three (or more) passes are handy
3797   for advanced users, but for brevity, this guide omits discussing those
3798   special cases.
3800 </para></listitem>
3801 <listitem><para>
3802   <emphasis role="bold">qcomp</emphasis>:
3803   <option>qcomp</option> trades off the number of bits allocated
3804   to "expensive" high-motion versus "cheap" low-motion frames. At
3805   one extreme, <option>qcomp=0</option> aims for true constant
3806   bitrate. Typically this would make high-motion scenes look completely
3807   awful, while low-motion scenes would probably look absolutely
3808   perfect, but would also use many times more bitrate than they
3809   would need in order to look merely excellent. At the other extreme,
3810   <option>qcomp=1</option> achieves nearly constant quantization parameter
3811   (QP). Constant QP does not look bad, but most people think it is more
3812   reasonable to shave some bitrate off of the extremely expensive scenes
3813   (where the loss of quality is not as noticeable) and reallocate it to
3814   the scenes that are easier to encode at excellent quality.
3815   <option>qcomp</option> is set to 0.6 by default, which may be slightly
3816   low for many peoples' taste (0.7-0.8 are also commonly used).
3817 </para></listitem>
3818 <listitem><para>
3819   <emphasis role="bold">keyint</emphasis>:
3820   <option>keyint</option> is solely for trading off file seekability against
3821   coding efficiency. By default, <option>keyint</option> is set to 250. In
3822   25fps material, this guarantees the ability to seek to within 10 seconds
3823   precision. If you think it would be important and useful to be able to
3824   seek within 5 seconds of precision, set <option>keyint=125</option>;
3825   this will hurt quality/bitrate slightly. If you care only about quality
3826   and not about seekability, you can set it to much higher values
3827   (understanding that there are diminishing returns which may become
3828   vanishingly low, or even zero). The video stream will still have seekable
3829   points as long as there are some scene changes.
3830 </para></listitem> 
3831 <listitem><para>
3832   <emphasis role="bold">deblockalpha, deblockbeta</emphasis>:
3833   This topic is going to be a bit controversial.
3834 </para>
3835 <para>
3836   H.264 defines a simple deblocking procedure on I-blocks that uses
3837   pre-set strengths and thresholds depending on the QP of the block
3838   in question.
3839   By default, high QP blocks are filtered heavily, and low QP blocks
3840   are not deblocked at all.
3841   The pre-set strengths defined by the standard are well-chosen and
3842   the odds are very good that they are PSNR-optimal for whatever
3843   video you are trying to encode.
3844   The <option>deblockalpha</option> and <option>deblockbeta</option>
3845   parameters allow you to specify offsets to the preset deblocking
3846   thresholds.
3847 </para>
3848 <para>
3849   Many people seem to think it is a good idea to lower the deblocking
3850   filter strength by large amounts (say, -3).
3851   This is however almost never a good idea, and in most cases,
3852   people who are doing this do not understand very well how
3853   deblocking works by default.
3854 </para>
3855 <para>
3856   The first and most important thing to know about the in-loop
3857   deblocking filter is that the default thresholds are almost always
3858   PSNR-optimal.
3859   In the rare cases that they are not optimal, the ideal offset is
3860   plus or minus 1.
3861   Adjusting deblocking parameters by a larger amount is almost
3862   guaranteed to hurt PSNR.
3863   Strengthening the filter will smear more details; weakening the
3864   filter will increase the appearance of blockiness.
3865 </para>
3866 <para>
3867   It is definitely a bad idea to lower the deblocking thresholds if
3868   your source is mainly low in spacial complexity (i.e., not a lot
3869   of detail or noise).
3870   The in-loop filter does a rather excellent job of concealing
3871   the artifacts that occur.
3872   If the source is high in spacial complexity, however, artifacts
3873   are less noticeable.
3874   This is because the ringing tends to look like detail or noise.
3875   Human visual perception easily notices when detail is removed,
3876   but it does not so easily notice when the noise is wrongly
3877   represented.
3878   When it comes to subjective quality, noise and detail are somewhat
3879   interchangeable.
3880   By lowering the deblocking filter strength, you are most likely
3881   increasing error by adding ringing artifacts, but the eye does
3882   not notice because it confuses the artifacts with detail.
3883 </para>
3885 <para>
3886   This <emphasis role="bold">still</emphasis> does not justify
3887   lowering the deblocking filter strength, however.
3888   You can generally get better quality noise from postprocessing.
3889   If your H.264 encodes look too blurry or smeared, try playing with
3890   <option>-vf noise</option> when you play your encoded movie.
3891   <option>-vf noise=8a:4a</option> should conceal most mild
3892   artifacting.
3893   It will almost certainly look better than the results you
3894   would have gotten just by fiddling with the deblocking filter.
3895 </para></listitem>
3896 </itemizedlist>
3897 </sect3>
3898 </sect2>
3900 <sect2 id="menc-feat-x264-example-settings">
3901 <title>Encoding setting examples</title>
3903 <para>
3904   The following settings are examples of different encoding
3905   option combinations that affect the speed vs quality tradeoff
3906   at the same target bitrate.
3907 </para>
3909 <para>
3910   All the encoding settings were tested on a 720x448 @30000/1001 fps
3911   video sample, the target bitrate was 900kbps, and the machine was an
3912   AMD-64 3400+ at 2400 Mhz in 64 bits mode.
3913   Each encoding setting features the measured encoding speed (in
3914   frames per second) and the PSNR loss (in dB) compared to the "very
3915   high quality" setting.
3916   Please understand that depending on your source, your machine type
3917   and development advancements, you may get very different results.
3918 </para>
3920 <para>
3921 <informaltable frame="all">
3922 <tgroup cols="4">
3923 <thead>
3924 <row><entry>Description</entry><entry>Encoding options</entry><entry>speed (in fps)</entry><entry>Relative PSNR loss (in dB)</entry></row>
3925 </thead>
3926 <tbody>
3927 <row>
3928   <entry>Very high quality</entry>
3929   <entry><option>subq=6:4x4mv:8x8dct:me=3:frameref=5:bframes=3:b_pyramid:weight_b</option></entry>
3930   <entry>6fps</entry>
3931   <entry>0dB</entry>
3932 </row>
3933 <row>
3934   <entry>High quality</entry>
3935   <entry><option>subq=5:4x4mv:8x8dct:frameref=2:bframes=3:b_pyramid:weight_b</option></entry>
3936   <entry>13fps</entry>
3937   <entry>-0.89dB</entry>
3938 </row>
3939 <row>
3940   <entry>Fast</entry>
3941   <entry><option>subq=4:bframes=2:b_pyramid:weight_b</option></entry>
3942   <entry>17fps</entry>
3943   <entry>-1.48dB</entry>
3944 </row>
3945 </tbody>
3946 </tgroup>
3947 </informaltable>
3948 </para>
3949 </sect2>
3951 </sect1>
3953 <sect1 id="menc-feat-vcd-dvd">
3954 <title>Using MEncoder to create VCD/SVCD/DVD-compliant files.</title>
3956 <sect2 id="menc-feat-vcd-dvd-constraints">
3957 <title>Format Constraints</title>
3958 <para>
3959   <application>MEncoder</application> is capable of creating VCD, SCVD
3960   and DVD format MPEG files using the
3961   <systemitem class="library">libavcodec</systemitem> library.
3962   These files can then be used in conjunction with
3963   <ulink url="http://www.gnu.org/software/vcdimager/vcdimager.html">vcdimager</ulink>
3964   or
3965   <ulink url="http://dvdauthor.sourceforge.net/">dvdauthor</ulink>
3966   to create discs that will play on a standard set-top player.
3967 </para>
3969 <para>
3970   The DVD, SVCD, and VCD formats are subject to heavy constraints.
3971   Only a small selection of encoded picture sizes and aspect ratios are
3972   available.
3973   If your movie does not already meet these requirements, you may have
3974   to scale,crop or add black borders to the picture to make it
3975   compliant.
3976 </para>
3978 <sect3 id="menc-feat-vcd-dvd-constraints-resolution">
3979 <title>Format Constraints</title>
3981 <informaltable frame="all">
3982 <tgroup cols="9">
3983 <thead>
3984     <row>
3985         <entry>Format</entry>
3986         <entry>Resolution</entry>
3987         <entry>V. Codec</entry>
3988         <entry>V. Bitrate</entry>
3989         <entry>Sample Rate</entry>
3990         <entry>A. Codec</entry>
3991         <entry>A. Bitrate</entry>
3992         <entry>FPS</entry>
3993         <entry>Aspect</entry>
3994     </row>
3995 </thead>
3996 <tbody>
3997     <row>
3998         <entry>NTSC DVD</entry>
3999         <entry>720x480, 704x480, 352x480, 352x240</entry> 
4000         <entry>MPEG-2</entry>
4001         <entry>9800 kbps</entry>
4002         <entry>48000 Hz</entry>
4003         <entry>AC3,PCM</entry>
4004         <entry>1536 kbps (max)</entry>
4005         <entry>30000/1001, 24000/1001</entry>
4006         <entry>4:3, 16:9 (only for 720x480)</entry>
4007     </row>
4008     <row>
4009         <entry>NTSC DVD</entry>
4010         <entry>352x240<footnote id='fn-rare-resolutions'><para>
4011           These resolutions are rarely used for DVDs because
4012           they are fairly low quality.</para></footnote></entry> 
4013         <entry>MPEG-1</entry>
4014         <entry>1856 kbps</entry>
4015         <entry>48000 Hz</entry>
4016         <entry>AC3,PCM</entry>
4017         <entry>1536 kbps (max)</entry>
4018         <entry>30000/1001, 24000/1001</entry>
4019         <entry>4:3, 16:9</entry>
4020     </row>
4021     <row>
4022         <entry>NTSC SVCD</entry>
4023         <entry>480x480</entry>
4024         <entry>MPEG-2</entry>
4025         <entry>2600 kbps</entry>
4026         <entry>44100 Hz</entry>
4027         <entry>MP2</entry>
4028         <entry>384 kbps (max)</entry>
4029         <entry>30000/1001</entry>
4030         <entry>4:3</entry>
4031     </row>
4032     <row>
4033         <entry>NTSC VCD</entry>
4034         <entry>352x240</entry>
4035         <entry>MPEG-1</entry>
4036         <entry>1150 kbps</entry>
4037         <entry>44100 Hz</entry>
4038         <entry>MP2</entry>
4039         <entry>224 kbps</entry>
4040         <entry>24000/1001, 30000/1001</entry>
4041         <entry>4:3</entry>
4042     </row>
4043     <row>
4044         <entry>PAL DVD</entry>
4045         <entry>720x576, 704x576, 352x576, 352x288</entry>
4046         <entry>MPEG-2</entry>
4047         <entry>9800 kbps</entry>
4048         <entry>48000 Hz</entry>
4049         <entry>MP2,AC3,PCM</entry>
4050         <entry>1536 kbps (max)</entry>
4051         <entry>25</entry>
4052         <entry>4:3, 16:9 (only for 720x576)</entry>
4053     </row>
4054     <row>
4055         <entry>PAL DVD</entry>
4056         <entry>352x288<footnoteref linkend='fn-rare-resolutions'/></entry>
4057         <entry>MPEG-1</entry>
4058         <entry>1856 kbps</entry>
4059         <entry>48000 Hz</entry>
4060         <entry>MP2,AC3,PCM</entry>
4061         <entry>1536 kbps (max)</entry>
4062         <entry>25</entry>
4063         <entry>4:3, 16:9</entry>
4064     </row>
4065     <row>
4066         <entry>PAL SVCD</entry>
4067         <entry>480x576</entry>
4068         <entry>MPEG-2</entry>
4069         <entry>2600 kbps</entry>
4070         <entry>44100 Hz</entry>
4071         <entry>MP2</entry>
4072         <entry>384 kbps (max)</entry>
4073         <entry>25</entry>
4074         <entry>4:3</entry>
4075     </row>
4076     <row>
4077         <entry>PAL VCD</entry>
4078         <entry>352x288</entry>
4079         <entry>MPEG-1</entry>
4080         <entry>1152 kbps</entry>
4081         <entry>44100 Hz</entry>
4082         <entry>MP2</entry>
4083         <entry>224 kbps</entry>
4084         <entry>25</entry>
4085         <entry>4:3</entry>
4086     </row>
4087 </tbody>
4088 </tgroup>
4089 </informaltable>
4091 <para>
4092   If your movie has 2.35:1 aspect (most recent action movies), you will
4093   have to add black borders or crop the movie down to 16:9 to make a DVD
4094   or VCD.
4095   If you add black borders, try to align them at 16-pixel boundaries in
4096   order to minimize the impact on encoding performance.
4097   Thankfully DVD has sufficiently excessive bitrate that you do not have
4098   to worry too much about encoding efficiency, but SVCD and VCD are
4099   highly bitrate-starved and require effort to obtain acceptable quality.
4100 </para>
4101 </sect3>
4103 <sect3 id="menc-feat-vcd-dvd-constraints-gop">
4104 <title>GOP Size Constraints</title>
4105 <para>
4106   DVD, VCD, and SVCD also constrain you to relatively low 
4107   GOP (Group of Pictures) sizes.
4108   For 30 fps material the largest allowed GOP size is 18.
4109   For 25 or 24 fps, the maximum is 15.
4110   The GOP size is set using the <option>keyint</option> option.
4111 </para>
4112 </sect3>
4114 <sect3 id="menc-feat-vcd-dvd-constraints-bitrate">
4115 <title>Bitrate Constraints</title>
4116 <para>
4117   VCD video is required to be CBR at 1152 kbps.
4118   This highly limiting constraint also comes along with an extremly low vbv
4119   buffer size of 327 kilobits.
4120   SVCD allows varying video bitrates up to 2500 kbps, and a somewhat less 
4121   restrictive vbv buffer size of 917 kilobits is allowed.
4122   DVD video bitrates may range anywhere up to 9800 kbps (though typical
4123   bitrates are about half that), and the vbv buffer size is 1835 kilobits.
4124 </para>
4125 </sect3>
4126 </sect2>
4128 <sect2 id="menc-feat-vcd-dvd-output">
4129 <title>Output Options</title>
4130 <para>
4131   <application>MEncoder</application> has options to control the output
4132   format.
4133   Using these options we can instruct it to create the correct type of
4134   file.
4135 </para>
4137 <para>
4138   The options for VCD and SVCD are called xvcd and xsvcd, because they
4139   are extended formats.
4140   They are not strictly compliant, mainly because the output does not
4141   contain scan offsets.
4142   If you need to generate an SVCD image, you  should pass the output file
4143   to
4144   <ulink url="http://www.gnu.org/software/vcdimager/vcdimager.html">vcdimager</ulink>.
4145 </para>
4147 <para>
4148   VCD:
4149   <screen>
4150   -of mpeg -mpegopts format=xvcd
4151   </screen>
4152 </para>
4154 <para>
4155   SVCD:
4156   <screen>
4157   -of mpeg -mpegopts format=xsvcd
4158   </screen>
4159 </para>
4161 <para>
4162   DVD:
4163   <screen>
4164   -of mpeg -mpegopts format=dvd
4165   </screen>
4166 </para>
4168 <para>
4169   DVD with NTSC Pullup:
4170   <screen>
4171   -of mpeg -mpegopts format=dvd:telecine -ofps 24000/1001
4172   </screen>
4173   This allows 24000/1001 fps progressive content to be encoded at 30000/1001 
4174   fps whilst maintaing DVD-compliance.
4175 </para>
4177 <sect3 id="menc-feat-vcd-dvd-output-aspect">
4178 <title>Aspect Ratio</title>
4179 <para>
4180   The aspect argument of <option>-lavcopts</option> is used to encode
4181   the aspect ratio of the file.
4182   During playback the aspect ratio is used to restore the video to the
4183   correct size.
4184 </para>
4186 <para>
4187   16:9 or "Widescreen"
4188   <screen>
4189   -lavcopts aspect=16/9
4190   </screen>
4191 </para>
4193 <para>
4194   4:3 or "Fullscreen"
4195   <screen>
4196   -lavcopts aspect=4/3
4197   </screen>
4198 </para>
4200 <para>
4201   2.35:1 or "Cinemascope" NTSC
4202   <screen>
4203   -vf scale=720:368,expand=720:480 -lavcopts aspect=16/9
4204   </screen>
4205   To calculate the correct scaling size, use the expanded NTSC width of
4206   854/2.35 = 368
4207 </para>
4209 <para>
4210   2.35:1 or "Cinemascope" PAL
4211   <screen>
4212   -vf scale="720:432,expand=720:576 -lavcopts aspect=16/9
4213   </screen>
4214   To calculate the correct scaling size, use the expanded PAL width of
4215   1024/2.35 = 432
4216 </para>
4218 </sect3>
4220 <sect3 id="menc-feat-vcd-dvd-a-v-sync">
4221 <title>Maintaining A/V sync</title>
4222 <para>
4223   In order to maintain audio/video synchronization throughout the encode,
4224   <application>MEncoder</application> has to drop or duplicate frames.
4225   This works rather well when muxing into an AVI file, but is almost
4226   guaranteed to fail to maintain A/V sync with other muxers such as MPEG.
4227   This is why it is necessary to append the
4228   <option>harddup</option> video filter at the end of the filter chain
4229   to avoid this kind of problem.
4230   You can find more technical information about <option>harddup</option>
4231   in the section
4232   <link linkend="menc-feat-dvd-mpeg4-muxing-filter-issues">Improving muxing and A/V sync reliability</link>
4233   or in the manual page.
4234 </para>
4235 </sect3>
4237 <sect3 id="menc-feat-vcd-dvd-output-srate">
4238 <title>Sample Rate Conversion</title>
4239 <para>
4240   If the audio sample rate in the original file is not the same as
4241   required by the target format, sample rate conversion is required.
4242   This is achieved using the <option>-srate</option> option and 
4243   the <option>-af lavcresample</option> audio filter together.
4244   </para>
4245   <para>
4246   DVD:
4247   <screen>
4248   -srate 48000 -af lavcresample=48000
4249   </screen>
4250 </para>
4251 <para>
4252   VCD and SVCD:
4253   <screen>
4254   -srate 44100 -af lavcresample=44100
4255   </screen>
4256   </para>
4257 </sect3>
4258 </sect2>
4260 <sect2 id="menc-feat-vcd-dvd-lavc">
4261 <title>Using libavcodec for VCD/SVCD/DVD Encoding</title>
4263 <sect3 id="menc-feat-vcd-dvd-lavc-intro">
4264 <title>Introduction</title>
4265 <para>
4266   <systemitem class="library">libavcodec</systemitem> can be used to
4267   create VCD/SVCD/DVD compliant video by using the appropriate options.
4268 </para>
4269 </sect3>
4271 <sect3 id="menc-feat-vcd-dvd-lavc-options">
4272 <title>lavcopts</title>
4273 <para>
4274   This is a list of fields in <option>-lavcopts</option> that you may
4275   be required to change in order to make a complaint movie for VCD, SVCD,
4276   or DVD:
4277 </para>
4279 <itemizedlist>
4280 <listitem><para>
4281   <emphasis role="bold">acodec</emphasis>:
4282   <option>mp2</option> for VCD, SVCD, or PAL DVD;
4283   <option>ac3</option> is most commonly used for DVD.
4284   PCM audio may also be used for DVD, but this is mostly a big waste of
4285   space.
4286   Note that MP3 audio is not compliant for any of these formats, but
4287   players often have no problem playing it anyway.
4288 </para></listitem>
4290 <listitem><para>
4291   <emphasis role="bold">abitrate</emphasis>:
4292   224 for VCD; up to 384 for SVCD; up to 1536 for  DVD, but commonly
4293   used values range from 192 kbps for stereo to 384 kbps for 5.1 channel
4294   sound.
4295 </para></listitem>
4297 <listitem><para>
4298   <emphasis role="bold">vcodec</emphasis>:
4299   <option>mpeg1video</option> for VCD;
4300   <option>mpeg2video</option> for SVCD;
4301   <option>mpeg2video</option> is usually used for DVD but you may also use
4302   <option>mpeg1video</option> for CIF resolutions.
4303 </para></listitem>
4305 <listitem><para>
4306   <emphasis role="bold">keyint</emphasis>:
4307   Used to set the GOP size.
4308   18 for 30fps material, or 15 for 25/24 fps material.
4309   Commercial producers seem to prefer keyframe intervals of 12.
4310   It is possible to make this much larger and still retain compatibility 
4311   with most players.
4312   A <option>keyint</option> of 25 should never cause any problems.
4313 </para></listitem>
4315 <listitem><para>
4316   <emphasis role="bold">vrc_buf_size</emphasis>:
4317   327 for VCD, 917 for SVCD, and 1835 for DVD.
4318 </para></listitem>
4320 <listitem><para>
4321   <emphasis role="bold">vrc_minrate</emphasis>:
4322   1152, for VCD. May be left alone for SVCD and DVD.
4323 </para></listitem>
4325 <listitem><para>
4326   <emphasis role="bold">vrc_maxrate</emphasis>:
4327   1152 for VCD; 2500 for SVCD; 9800 for DVD.
4328   For SVCD and DVD, you might wish to use lower values depending on your
4329   own personal preferences and requirements.
4330 </para></listitem>
4332 <listitem><para>
4333   <emphasis role="bold">vbitrate</emphasis>:
4334   1152 for VCD;
4335   up to 2500 for SVCD;
4336   up to 9800 for DVD.
4337   For the latter two formats, vbitrate should be set based on personal
4338   preference.
4339   For instance, if you insist on fitting 20 or so hours on a DVD, you
4340   could use vbitrate=400.
4341   The resulting video quality would probably be quite bad.
4342   If you are trying to squeeze out the maximum possible quality on a DVD,
4343   use vbitrate=9800, but be warned that this could constrain you to less
4344   than an hour of video on a single-layer DVD.
4345 </para></listitem>
4346 </itemizedlist>
4347 </sect3>
4349 <sect3 id="menc-feat-vcd-dvd-lavc-examples">
4350 <title>Examples</title>
4351 <para>
4352   This is a typical minimum set of <option>-lavcopts</option> for
4353   encoding video:
4354 </para>
4355 <para>
4356   VCD:
4357   <screen>
4358   -lavcopts vcodec=mpeg1video:vrc_buf_size=327:vrc_minrate=1152:\
4359   vrc_maxrate=1152:vbitrate=1152:keyint=15:acodec=mp2
4360   </screen>
4361 </para>
4363 <para>
4364   SVCD:
4365   <screen>
4366   -lavcopts vcodec=mpeg2video:vrc_buf_size=917:vrc_maxrate=2500:vbitrate=1800:\
4367   keyint=15:acodec=mp2
4368   </screen>
4369 </para>
4371 <para>
4372   DVD:
4373   <screen>
4374   -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:\
4375   keyint=15:acodec=ac3
4376   </screen>
4377 </para>
4379 </sect3>
4381 <sect3 id="menc-feat-vcd-dvd-lavc-advanced">
4382 <title>Advanced Options</title>
4383 <para>
4384   For higher quality encoding, you may also wish to add quality-enhancing
4385   options to lavcopts, such as <option>trell</option>,
4386   <option>mbd=2</option>, and others.
4387   Note that <option>qpel</option> and <option>v4mv</option>, while often
4388   useful with MPEG-4, are not usable with MPEG-1 or MPEG-2.
4389   Also, if you are trying to make a very high quality DVD encode, it may
4390   be useful to add <option>dc=10</option> to lavcopts.
4391   Doing so may help reduce the appearance of blocks in flat-colored areas.
4392   Putting it all together, this is an example of a set of lavcopts for a
4393   higher quality DVD:
4394 </para>
4396 <para>
4397   <screen>
4398   -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=8000:\
4399   keyint=15:trell:mbd=2:precmp=2:subcmp=2:cmp=2:dia=-10:predia=-10:cbp:mv0:\
4400   vqmin=1:lmin=1:dc=10
4401   </screen>
4402 </para>
4404 </sect3>
4405 </sect2>
4407 <sect2 id="menc-feat-vcd-dvd-audio">
4408 <title>Encoding Audio</title>
4409 <para>
4410   VCD and SVCD support MPEG-1 layer II audio, using one of
4411   <systemitem class="library">toolame</systemitem>,
4412   <systemitem class="library">twolame</systemitem>,
4413   or <systemitem class="library">libavcodec</systemitem>'s MP2 encoder.
4414   The libavcodec MP2 is far from being as good as the other two libraries,
4415   however it should always be available to use.
4416   VCD only supports constant bitrate audio (CBR) whereas SVCD supports
4417   variable bitrate (VBR), too.
4418   Be careful when using VBR because some bad standalone players might not
4419   support it too well.
4420 </para>
4422 <para>
4423   For DVD audio, <systemitem class="library">libavcodec</systemitem>'s
4424   AC3 codec is used.
4425 </para>
4427 <sect3 id="menc-feat-vcd-dvd-audio-toolame">
4428 <title>toolame</title>
4429 <para>
4430   For VCD and SVCD:
4431   <screen>
4432   -oac toolame -toolameopts br=224
4433   </screen>
4434 </para>
4435 </sect3>
4437 <sect3 id="menc-feat-vcd-dvd-audio-twolame">
4438 <title>twolame</title>
4439 <para>
4440   For VCD and SVCD:
4441   <screen>
4442   -oac twolame -twolameopts br=224
4443   </screen>
4444 </para>
4445 </sect3>
4447 <sect3 id="menc-feat-vcd-dvd-audio-lavc">
4448 <title>libavcodec</title>
4449 <para>
4450   For DVD with 2 channel sound:
4451   <screen>
4452   -oac lavc -lavcopts acodec=ac3:abitrate=192
4453   </screen>
4454 </para>
4455 <para>
4456   For DVD with 5.1 channel sound:
4457   <screen>
4458   -channels 6 -oac lavc -lavcopts acodec=ac3:abitrate=384
4459   </screen>
4460 </para>
4461 <para>
4462   For VCD and SVCD:
4463   <screen>
4464   -oac lavc -lavcopts acodec=mp2:abitrate=224
4465   </screen>
4466 </para>
4467 </sect3>
4469 </sect2>
4471 <sect2 id="menc-feat-vcd-dvd-all">
4472 <title>Putting it all Together</title>
4473 <para>
4474   This section shows some complete commands for creating VCD/SVCD/DVD
4475   compliant videos.
4476 </para>
4478 <sect3 id="menc-feat-vcd-dvd-all-pal-dvd">
4479 <title>PAL DVD</title>
4480 <para>
4481   <screen>
4482   mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd -vf scale=720:576,\
4483   harddup -srate 48000 -af lavcresample=48000 -lavcopts vcodec=mpeg2video:\
4484   vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:keyint=15:acodec=ac3:\
4485   abitrate=192:aspect=16/9 -ofps 25 \
4486   -o <replaceable>movie.mpg</replaceable> <replaceable>movie.avi</replaceable>
4487   </screen>
4488 </para>
4489 </sect3>
4491 <sect3 id="menc-feat-vcd-dvd-all-ntsc-dvd">
4492 <title>NTSC DVD</title>
4493 <para>
4494   <screen>
4495   mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd -vf scale=720:480,\
4496   harddup -srate 48000 -af lavcresample=48000 -lavcopts vcodec=mpeg2video:\
4497   vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:keyint=18:acodec=ac3:\
4498   abitrate=192:aspect=16/9 -ofps 30000/1001 \
4499   -o <replaceable>movie.mpg</replaceable> <replaceable>movie.avi</replaceable>
4500   </screen>
4501 </para>
4502 </sect3>
4504 <sect3 id="menc-feat-vcd-dvd-all-pal-ac3-copy">
4505 <title>PAL AVI Containing AC3 Audio to DVD</title>
4506 <para>
4507   If the source already has AC3 audio, use -oac copy instead of re-encoding it.
4508   <screen>
4509   mencoder -oac copy -ovc lavc -of mpeg -mpegopts format=dvd -vf scale=720:576,\
4510   harddup -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:\
4511   vbitrate=5000:keyint=15:aspect=16/9 -ofps 25 \
4512   -o <replaceable>movie.mpg</replaceable> <replaceable>movie.avi</replaceable>
4513   </screen>
4514 </para>
4515 </sect3>
4517 <sect3 id="menc-feat-vcd-dvd-all-ntsc-ac3-copy">
4518 <title>NTSC AVI Containing AC3 Audio to DVD</title>
4519 <para>
4520   If the source already has AC3 audio, and is NTSC @ 24000/1001 fps:
4521   <screen>
4522   mencoder -oac copy -ovc lavc -of mpeg -mpegopts format=dvd:telecine \
4523   -vf scale=720:480,harddup -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:\
4524   vrc_maxrate=9800:vbitrate=5000:keyint=15:aspect=16/9 -ofps 24000/1001 \
4525   -o <replaceable>movie.mpg</replaceable> <replaceable>movie.avi</replaceable>
4526   </screen>
4527 </para>
4528 </sect3>
4530 <sect3 id="menc-feat-vcd-dvd-all-pal-svcd">
4531 <title>PAL SVCD</title>
4532 <para>
4533   <screen>
4534   mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=xsvcd -vf \
4535   scale=480:576,harddup -srate 44100 -af lavcresample=44100 -lavcopts \
4536   vcodec=mpeg2video:mbd=2:keyint=15:vrc_buf_size=917:vrc_minrate=600:\
4537   vbitrate=2500:vrc_maxrate=2500:acodec=mp2:abitrate=224 -ofps 25 \
4538   -o <replaceable>movie.mpg</replaceable> <replaceable>movie.avi</replaceable>
4539   </screen>
4540 </para>
4541 </sect3>
4543 <sect3 id="menc-feat-vcd-dvd-all-ntsc-svcd">
4544 <title>NTSC SVCD</title>
4545 <para>
4546   <screen>
4547   mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=xsvcd  -vf \
4548   scale=480:480,harddup -srate 44100 -af lavcresample=44100 -lavcopts \
4549   vcodec=mpeg2video:mbd=2:keyint=18:vrc_buf_size=917:vrc_minrate=600:\
4550   vbitrate=2500:vrc_maxrate=2500:acodec=mp2:abitrate=224 -ofps 30000/1001 \
4551   -o <replaceable>movie.mpg</replaceable> <replaceable>movie.avi</replaceable>
4552   </screen>
4553 </para>
4554 </sect3>
4556 <sect3 id="menc-feat-vcd-dvd-all-pal-vcd">
4557 <title>PAL VCD</title>
4558 <para>
4559   <screen>
4560   mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=xvcd -vf \
4561   scale=352:288,harddup -srate 44100 -af lavcresample=44100 -lavcopts \
4562   vcodec=mpeg1video:keyint=15:vrc_buf_size=327:vrc_minrate=1152:vbitrate=1152:\
4563   vrc_maxrate=1152:acodec=mp2:abitrate=224 -ofps 25 \
4564   -o <replaceable>movie.mpg</replaceable> <replaceable>movie.avi</replaceable>
4565   </screen>
4566 </para>
4567 </sect3>
4569 <sect3 id="menc-feat-vcd-dvd-all-ntsc-vcd">
4570 <title>NTSC VCD</title>
4571 <para>
4572   <screen>
4573   mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=xvcd -vf \
4574   scale=352:240,harddup -srate 44100 -af lavcresample=44100 -lavcopts \
4575   vcodec=mpeg1video:keyint=18:vrc_buf_size=327:vrc_minrate=1152:vbitrate=1152:\
4576   vrc_maxrate=1152:acodec=mp2:abitrate=224 -ofps 30000/1001 \
4577   -o <replaceable>movie.mpg</replaceable> <replaceable>movie.avi</replaceable>
4578   </screen>
4579 </para>
4580 </sect3>
4582 </sect2>
4584 </sect1>
4586 </chapter>