   \input texinfo

MPEG1 Video creation Example
----------------------------

   `> lav2yuv editlist.eli | mpeg2enc -b 2000 -r 24 -q 6 -o video.m1v'

   mpeg2enc creates an video with an bitrate of 2000kBit/s (or
2048000Bit/s) but the -q flag activates the variable bitrate and a
quality factor of 6. It uses a search radius of 24.

   `Explanation:'when mpeg2enc is invoked without the 'q' flag it
creates "constantbit-rate" MPEG streams. Where (loosely speaking) the
strength of compression (and hence picture quality) is adjusted to
ensure that on average each frame of video has exactly the specified
number of bits. Such constant bit-rate streams are needed for
broadcasting and for low-cost hardware like DVD and VCD players which
use slow fixed-speed player hardware.

   Obviously this is fairly inefficient as it means inactive scenes use
up bits that could better be "spent" on rapidly changing scenes.
Setting the 'q' flag tells mpeg2enc to generate variable bit-rate
streams. For such streams the bit-rate specified is simply the maximum
permissible. The 'q' parameter specifies the minimum degree of
compression to be applied by specifying how exactly picture information
is recorded. Typically, 'q' would be set so that quiet scenes would use
less than the specified maximum (around 6 or 8) but fast moving scenes
would still be bit-rate limited. For archival purposes setting a
maximum bit-rate high enough never to be reached (e.g. 10Mbps) and a q
of 2 or 3 are reasonable choices.

   Example:

   `> lav2yuv stream.avi | yuvscaler -I ACTIVE_352x240+0+24 | mpeg2enc
-b 1152 -r 16 -4 1 -2 1 -o video.m1v'

   Usually there is at the top and at the bottom a nearly black border
and a lot of bandwidth is used for something you do not like. The
yuvscaler -I ACTIVE option sets everything that is not in the described
area to black, but the imagesize (352x288) is not changed.  So you have
a real black border the encoder only uses a few bits for encoding them.
You are still compatible with the VCD's format in this example.  To
determine the active window extract one frame to the jpeg format:

   `> lavtrans -f i -i 100 -o frame.jpg test.avi'

   Than use your favorite graphic display program to determine the
active size.  The -4 1 and -2 1 options improves the quality about 10%
but conversion is slower.

   At the size of 352x288 (1/2 PAL size, created when using the -d 2
option when recording) the needed bitrate is/should be between 1000 -
1500kBit/s. For NTSC it should be about the same, because the image is
smaller, but there are more frames per second than in PAL.

   Anyways, the major factor is quality of the original and the degree
of filtering. Poor quality unfiltered material typically needs a higher
rate to avoid visible artifacts.  If you want to reduce bit-rate
without annoying artifacts when compressing broadcast material you
should try one (or more) of the noise filters.

   Example:

   `> lav2yuv stream.avi | mpeg2enc -b 1500 -n s -g 6 -G 20 -P -o
video.m1v'

   Here the stream.avi will be encoded with:

`-b 1500'
     a Bitrate of 1500kBit/sec

`-n s'
     the input Video norm is forced to SECAM

`-P'
     This ensures that 2 B frames appear between adjacent I/P frames.
     Several common MPEG-1 decoders can't handle streams that do not
     have 2 B-frames between I/P frames

`-g 6 -G 20'
     the encoder can dynamically change the group-of-pictures size to
     reflect scene changes. This is done by setting a maximum GOP (-G
     flag) size larger than the minimum (-g flag).  For VCDs sensible
     values might be a minimum of 9 and a maximum of 15.  For SVCD 9
     and 15 would be good values. If you only want to play it back on
     SW player you can use other min-max values.

   Example

   `> lav2yuv stream*.avi | mpeg2enc -b 1500 -r 16 -4 1 -2 1 -S 630 -B
260 -o video_n1_1500_r16_41_21_S630_B240.m1v'

   lav2yuv processes all the stream files. Then mpeg2enc is given some
options that make the encoded stream look nicer. Using `-S 630' means
that mpeg2enc marks the stream so that mplex generates a new stream
every 630MB. One important thing is the use of the `-B' option which
specifies the non-video (audio and mplex information) bitrate. The `-B'
value of 260 should be fine for audio with 224kBit and mplex
information. For further information take a look at the encoding
scripts in the scripts directory.

MPEG1 Multiplexing Example
--------------------------

   Example

   ` >mplex sound.mp2 video.m1v -o my_video.mpg'

   Puts the sound.mp2 and the video.m1v stream together to my_video.mpg.
It only works that easy if you have CBR (the -q option was not used with
mpeg2enc).

   Example

   `mplex -V -r 1740 audio.mp2 video_vbr.m1v -o vbr_stream.mpg'

   Here we multiplex a variable bitrate stream. mplex is now a single
pass multiplexer so it can't detect the maximal bitrate and we have to
specify it. The data rate for the output stream is: `audio bitrate +
peak videobitrate + 1-2% for mplex information'. If audio (-b 224) has
224kBit, video has 1500kBit (was encoded with -b 1500 -q 9) then we
have 1724 * 1.01 or about 1740kBit.

Creating MPEG2 Videos
=====================

   MPEG2 is recommended for sources with a greater picture than 352x240
for NTSC and 352x288 for PAL. MPEG2 can also handle interlaced sources
like recording from TV at full resolution.

   MPEG2 allows the usage of mpeg layer 3 (mp3) sound. So you can use
your favorite mp3encoder for the creation of the sound.  However, MP3
audio is not valid for DVDs.  It is best to use MP2 (Layer 2) audio.
The audio can also be a VBR Stream.

   MPEG2 is usually a VBR Stream. MPEG2 creation with optimization
requires a lot of CPU power.  A film with the double resolution is NOT
4 times larger than an MPEG1 Stream. Depending on your quality settings
it will be about 1.5 up to 3 times larger than the MPEG1 Stream at its
lower resolution.

MPEG2 Audio creation Example
----------------------------

   `> lav2wav editlist.eli | mp2enc -o sound.mp2'

   This will fit the MPEG2 quite well. You can save some bits by telling
mp2enc to use a lower bitrate (-b option) like 160 or 192 kBit/s.  I
hope I don't need to explain the usage of an MP3 Encoder.  But you
should not use all the fancy options that are available.

MPEG2 Video creation Example
----------------------------

   `> lav2yuv editlist.eli | mpeg2enc -f 3 -b 3000 -q 9 -o video.m2v'

   A very simple example for MPEG2 Video.  The most important option is
the -f 3. That tells mpeg2enc that it should create a MPEG2 stream.
Because it is a generic MPEG2 you have to use the -b bitrate options.
And should use the -q option because you usually want a space saving
VBR Stream. When using VBR streams the -b option tells mpeg2enc the
maximum bitrate that can be used. The -q option tell mpeg2enc what
quality the streams should have.  The bitrate has an upper bound of the
value specified by -b.

   `> lav2yuv editlist.eli | mpeg2enc -f 3 -4 1 -2 1 -q7 -b 4500 -V 300
-P -g 6 -G 18 -I 1 -o video.m2v'

   This will generate a higher quality MPEG2 stream because the -4 1 and
-2 1 options were used.  With -b 4500 -q 7 you tell mpeg2enc the
maximal bitrate and the quality factor.  -V is the video buffer size
used for decoding the stream. For SW playback it can be much higher
than the default. Dynamic GOP is set with -g and -G.  A larger GOP size
can help reduce the bit-rate required for a given quality but very
large sizes can introduce artifacts due to DCT/iDCT accumulated
rounding errors.  The -P option also ensures that 2 B frames appear
between adjacent I/P frames. The -I 1 option tells mpeg2enc that the
source is a interlaced material like videos. There is (time consuming)
interlaced motion compensation logic present in mpeg2enc.  Mpeg2enc
will use that logic if the size of the frames you encode is larger than
the VCD size for your TV Norm.

   If you denoise the images with yuvdenoise and use the deinterlacing
(-F) option you should tell mpeg2enc that it does not need to do motion
estimation for interlaced material. You have to use the -I 0 option of
mpeg2enc to say that the frames are already deinterlaced.  This will
save a lot of time when encoding. If you don't use -I 0 it will not
cause problems, the encoding will just take longer.

   You can also use scaling an options that optimize (denoise) the
images to get smaller streams.  These options are explained in detail
in the following sections.

Which values should be used for VBR Encoding
............................................

   The -q option controls the minimum quantization of the output stream.
Quantization controls the precision with which image information is
encoded. The lower the value the better the image quality.  Values
below 4 are extremes and should only be used if you know what you are
doing

   Usually you have to set up a maximum bitrate with the -b option.
The tricky task is to set a value for the -q option and the -b option
that produces a nice movie without using too much bandwidth and does not
introduce too many artifacts.

   A quality factor should be chosen that way that the mplex output of
Peak bit-rate and average bit-rate differ by about 20-25%.  If the
difference is very small (less than < 10%) it is likely that you will
begin to see artifacts in high motion scenes.  The most common cause of
the average rate being too close (or equal) to the maximum rate is
wrong value for the maximal bitrate or a quality factor that is too
high.

   A combination that will produce more artifacts than you can count is
a SVCD with a maximal video bitrate of 2500kBit and a qualitfactor set
to 1 or 2.  For SVCD with a video limit of 2500kBit a quality factor of
7-11 fits quite good (8 is the default). If you use filter programs or
have a very good source like digital TV, DVD like material or rendered
pictures you can use a quality factor of 6 when creating SVCDs.  If
your SVCD/DVD player supports higher bitrates than the official
2788kBit/sec for the video and audio.   When using a higher bitrate and
quality factor action scenes will look much better but of course the
playing time of the disc will be less.

   The same (7-11) quality factor for a full size picture and a top
bitrate of 3500 to 4000 kBit will produce few artifacts.

   For SVCD/DVD you can expect a result like the one described if the
maximal bitrate is not set too low:

   q <= 6 real sharp pictures, and good quality    q <= 8 good quality
 q >= 10 average quality    q >= 11 not that good    q >= 13 here even
still sequences might look blocky

Encoding destination TV (interlaced) or Monitor (progressive)
.............................................................

   MPEG2 supports interlaced data in addition to the progressive format.
A MPEG2 movie can be interlaced or progressive. It depends on the source
(film or broadcast) and on the viewing device.

   If you encode a film both fields should be the same. Deinterlace the
stream with yuvdenoise -F, or if you have a high quality source, and
don't need to use the denoiser, with yuvcorrect -T NOT_INTERLACED. Also
set the mpeg2enc interlace-mode (-I) option to 0. This means that there
is no interlacing.  We do not really need deinterlacing here because
there is no motion between the fields of the frame. We only need to
unite the two fields into a single progressive frame.

   This movie should play back an any device (TV or Monitor) without
problems.

   If you have an interlaced source (broadcast) you can encode it as
interlaced stream. Or deinterlace the stream and encode it as
progressive stream. If you deinterlace it with yuvdenoise -F, you will
lose details.  But if you plan to play the recorded stream on your DVD
player and your TV it would not be wise to perform deinterlacing.  If
you only want to play it back on the Monitor (progressive display) the
picture looks better when playing it back if it is deinterlaced. If the
player you use can do deinterlacing it does not matter if your encoded
video has interlaced frames or progressive frames.

   If you plan to deinterlace the stream you can only do this with
yuvdenoise -F, and set the mpeg2enc -I 0. If you do not want to
deinterlace the stream you do not need to set any special option (do
not use yuvdenoise -F and mpeg2enc -I 0)

   If you like to pause the stream and look on the still you should
deinterlace. Because then the image is flicker free when pausing.

   If you have a film (progressive) with parts from a broadcast
(interlaced) mixed together (like in a documentary where some parts
from a speaker are recorded interlaced and other parts are filmed) you
have to choose between good film sequences with average still images or
average looking film sequences with good still images.

   For good film with average stills do not deinterlace.  For average
film sequences with good stills then deinterlace (using -F and -I 0).

MPEG2 Multiplexing Example
--------------------------

   `> mplex -f 3 -b 300 -r 4750 -V audio.mp3 video.mp3 -o final.mpg'

   Now both streams (a mp3 audio and a mpeg2 video) are multiplex into a
single stream (final.mpg). You have to use the -f 3 option to tell
mplex the output format. You also have to add the -b decoder buffers
size with the same value used when encoding the video. -r is that rate
of video + audio +1-2% of mplex information.

   The -V option tells that your source for mplexing is a VBR stream. If
you don't use this option mplex creates something like a CBR Stream
with the bitrate you have told it with the -r option.  These streams
are usually get BIG.

Creating Video-CD's
===================

   VCD is a constrained version of MEPG1 streams.  VCD format was
defined by Philips. The goal was to use a single speed CD-drive and
other cheap hardware (not flexible) to have a cheap HW-Player.  Because
of that there are some limitations on VCD's.  The bitrate for video is
1152kBit and for audio layer 2 audio 224kBit stereo.  You are not
allowed to use the -q option, dynamic GOP sizes and the video buffer is
limited to 46kB.  The image size is limited to 352x240 for NTSC, an to
352x288 for PAL.

   If you have no VCD (only) player and you plan to use your DVD player
then it is quite possible that the DVD player will be flexible enough
to allow higher bitrates, dynamic GOP sizes, larger video buffer and so
on

VCD Audio creation Example
--------------------------

   `> lav2wav stream.avi | mp2enc -V -o sound.mp2'

   `-V' force VCD compatible output (same as: -b 224 -r 44100 -s). For
hardware players, you should stick to 44.1 224kBps Stereo layer 2 Audio.

VCD Video creation Example
--------------------------

   `> lav2yuv stream.avi | yuvscaler -O VCD | mpeg2enc -f 1 -r 16 -o
video.mpg'

   For a VCD compatible output the -f 1 sets all options in mpeg2enc as
needed. It seems that many VCD players (Avex for example) are not able
to play MPEG streams that are encoded with a search radius greater than
16 so do not use the -r option to override the default of 16.

   `> lav2yuv streams.eli | mpeg2enc -f 1 -4 1 -2 1 -S 630 -B 260 -P -o
video.m1v'

   Using `'-S 630" means that mpeg2enc marks the stream so that mplex
generates a new stream every 630MB. One important thing is the use of
the `-B' option which specifies the non-video (audio and mplex
information) bitrate. The -B value of 260 should be fine for audio with
224kBit and mplex information. For further information take a look at
the encoding scripts in the scripts directory. So the multiplexed
streams should easily fit on a CD with 650MB.

   The default value (-B) is 700MB for the video. mpeg2enc marks
automatically every stream at that size if the -B option is not used to
set a different value.  If you have a CD where you can write more data
(perhaps as much as 800MB), you have to set the -S option or otherwise
mpeg2enc will mark the stream at 700 MB, and mplex will split the
stream there.  Which is almost certainly not what you want.

VCD Multiplexing Example
------------------------

   `> mplex -f 1 sound.mp2 video.mpg -o vcd_out.mpg'

   The -f 1 option turns on a lot of weird stuff that otherwise has no
place in a respectable multiplexer!

Creating the CD
---------------

   The multiplexed streams have to be converted to an VCD compatible.
This is done by vcdimager http://www.vcdimager.org/

   `> vcdimager testvideo.mpg'

   Creates a `videocd.bin', the data file, and a `videocd.cue' which is
used as control file for cdrdao.

   You use cdrdao to burn the image. Cdrdao is yet another fine
Sourceforge project which is found at: http://cdrdao.sourceforge.net/

Notes
-----

   For MPEG-1 encoding a typical (45 minute running time) show or 90 odd
minute movie from an analog broadcast a constant bit-rate of around
1800 kBit/sec should be ideal. The resulting files are around 700M for
45 minutes which fits nicely as a raw XA MODE2 data track on a CD-R.
For pure digital sources (DTV or DVD streams and similar) VCD 1152
works fine.

   `Note:' If you encode VBR MPEG1 (-q) remember the Hardware was
probably not designed to do the playback because it is not in the
specifications. If it works be very happy. I've noticed that it helps
when you have an MPEG1 Stream to tell vcdimager that it is an SVCD.
vcdimager complains (but only with a warning and not a fatal error) but
you should be able to burn it. This could convince the player to use
different routines in its firmware and play it back correct, but there
is no guarantee of that.

Storing MPEGs
-------------

   If you record the data as XA mode 2 tracks you can fit appreciably
more on a CD (at the expense of error correction/detection). You can
use vcdimager to do this and vcdxrip (part of the vcdimager package) to
extract ("rip") the resulting files. For better Quality there are SVCD
and XVCD and DVD.

   Currently SVCD is fully supported with a pre-set format in mplex and
tools to create disks. MPEG streams that can be played by DVD player
hardware and software can readily produced using mpeg2enc/mplex

   If your player doesn't support SVCD you may well find it can handle
VCD streams that have much higher than standard bit-rates. Often as
much as 2500kBit/sec is possible. The several brands of DVD players can
also play wilding out of spec SVCD and VCD discs.  With higher
bit-rates and good quality source material it is worth trying
mpeg2enc's -h flag which produce a stream that is as sharp as the
limits of the VCD standard permits.

   However, if your player supports it and you have the patience for the
longer encoding times SVCD is a much better alternative. Using a more
efficient MPEG format SVCD more than doubles VCD's resolution while
typically producing files that are less than twice as big.

Creating SVCD
=============

   Super Video CD (SVCD) is an enhancement to Video CD that was
developed by a Chinese government-backed committee of manufacturers and
researchers.  The final SVCD spec was announced in September 1998.  A
good explanation of the SVCD format from Philips can be found here:
http://licensing.philips.com/information/cd/video/documents575.html.

   Record at full TV resolution (means: -d 1 for PAL this is 720x576)
The resolution is for NTSC is 480x480 of PAL 480x576, so you know why
you should record at full size.

SVCD Audio creation Example
---------------------------

   `> lav2wav stream.avi | mp2enc -V -e -o sound.mp2'

   The SVCD specifications permit a much wider choice of audio rates,
it is not necessary to use 224 kBit/sec. Any audio rate between 32 and
384 kBit/sec is permitted. The audio may be VBR (Variable Bit Rate).
The -e enables the CRC error protection for the audio. The CRC has to
be enabled to be SVCD conform. But it seems that most players don't pay
attention to the CRC information. The CRC information need 2 bytes per
Audio frame

   The approximate frame length formula for MPEG-1 layer-II is:

   [frame length in bytes) = 144 * (byte rate) / (sample rate]

   If you have the typical VCD settings the CRC data needs about 0,27%
of the whole data. In the worst case, where you have a MONO 32k Bitrate
stream, the CRC data needs 1,92%.

SVCD Video creation Example
---------------------------

   `> lav2yuv stream.avi | yuvscaler -O SVCD | mpeg2enc -f 4 -q 7 -I 1
-V 200 -o video.m2v'

`-f 4'
     sets the options for mpeg2enc to SVCD

`-q 7'
     tell mpeg2enc to generate a variable bitrate stream

`-I 1'
     tell mpeg2enc to assume that the original signal is field
     interlaced video where the odd rows of pixels are sampled a half
     frame interval after the even ones in each frame. The -I 0
     (progressive output (no field pictures)) option will also work for
     PAL

   You can use lower bitrates, but the SVCD standard limits `total
bit-rate (audio and video) to 2788800 Bit/sec'. So with 224Kbps audio
and overhead 2550 may already be marginally too tight. Since the SVCD
format permits any audio rate between 32 and 224 kBit/sec you can save
a few bits/sec by using 192k audio (or for non-musical material 160k).

   SVCD supports variable bitrate (VBR), because MPEG2 is usually VBR,
but with the top video bitrate limit of 2500kBit/sec. With the -f 4
flag the encoder also sets dynamic GOP with a low limit of -g 6 and a
high limit of -G 18. This saves a few bits/sec and improves the picture
quality during scene changes.  When encoding with -f 4 mpeg2enc ignores
the video bitrate (-b) and search radius (-r) options. If you use -f 5
you have to specify the bitrate and other options to mpeg2enc.

   Another possibility for movies in PAL (European style 25 frames/50
fields per sec) video is:

   `> lav2yuv stream.avi | yuvscaler -O SVCD | mpeg2enc -f 4 -I 0 -V
300 -o video.m2v'

   Movies are shot on film at 24 frames/sec. For PAL broadcast the film
is simply shown slightly "too fast" at 25 frame/sec (much to the pain
of people with an absolute pitch sense of pitch). The -I 0 flag turns
off the tedious calculations needed to compensate for field interlacing
giving much faster encoding.

   Unfortunately, movies broadcast in NTSC (US style 30 frames/60 fields
sec) video this will produce very poor compression. The "pulldown"
sampling used to produce 60 fields a second from a 24 frame a second
movie means half the frames in an NTSC *are* field interlaced.

   Don't forget the -S and -B options mentioned above. You want the
stream to fit on a CD don't you ?

SVCD Multiplexing Example
-------------------------

   `> mplex -f 4 -b 300 -r 2750 sound.mp2 video.m2v -o svcd_out.mpg'

`-f 4'
     tells mplex to mplex a SVCD

`-r 2750'
     is the calculated Audio + Video Bitrate + 1-2% multiplex
     information

`-b 300'
     is the buffer available on the playback device (the same value as
     used for the video encoding (mpeg2enc's -V option).

SVCD Creating the CD
--------------------

   Example:

   `> vcdimager -t svcd testvideo.mpg'

   Creates a `videocd.bin', the data file, and a `videocd.cue' which is
used as control file for cdrdao.

   Use cdrdao to burn the image as mentioned earlier.

   `NOTE:'If you want to build "custom" VCD/SVCD you will need to use
the mplex -f 2 and -f 5 switches.

   `NOTE:'The VCD and SVCD stuff may work on your HW player or not.
There are many reports that it works quite well. Don't be worried if it
does not work. Nor am I responsible for unusable CDs. ("coasters")

Creating DVD's
==============

   Everything in this section is very new. So it can change every day.
The limitations I mention here might not exist in the current version.

   You need obviously a DVD writer. I own a Ricoh DVD+RW that works,
and I know of a DVD-RAM writer that is able to to burn DVD-R. That
disks also work with a DVD-Player. Which programs you use for burning
depends on the DVD writer drive.

   For the creation and writing of the VOB, IFO and BUP files we use
dvdauthor. Aviable from Sourceforge (you might have guessed it)
http://dvdauthor.sourceforge.net.

DVD Audio creation Example
--------------------------

   `> lav2wav stream.eli | mp2enc -r 48000 -o sound.mp2'

`-r 48000'
     The sample rate has to be 48kHz. The mp2enc -r option forces the
     sample rate to 48kHz. If it is not a 48kHz mp2enc will resample
     the audio to get the sample rate.  If the audio is recorded at
     48kHz then no resampling is needed and toolame can be used for the
     encoding (it is much faster than mp2enc).

DVD Video creation Example
--------------------------

   `> lav2yuv stream.eli | mpeg2enc -f 8 -o video.m2v'

`-f 8'
     This sets the options correctly for a MPEG-2 video that is
     compliant with the DVD standard.  The maximum bitrate is set to
     7500kBps and the video buffer size is set to 230KB. The default
     quality factor is set to 8.  mpeg2enc sets currenty no automatic
     sequence length as it does for VCD/SVCD.

   The other options to get a low bitrate and high quality stream can
also be used to override the default settings mentioned above.  You can
also use yuvdenoise to increase the picture quality if the input data is
noisy (from a VHS tape for example).  A typical command will look like
this:

   `lav2yuv moby.eli | yuvdenoise -F -l 2 | mpeg2enc -f 8 -q 7 -4 1 -2
1 -P -I 0 -N -o video_DVD.m2v'

DVD Mplex Example
-----------------

   `> mplex -f 8 sound.mp2 video.m2v -o my_dvdlikestream.mpg'

`-f 8'
     Here again we specify that we want to have DVD like MPEG stream.
     mplex cannot do all the fancy things allowed for a DVD, but it is
     close enough that the HW-DVD players accept it.

`-o'
     there we specify the output filename.

DVD creation Example
--------------------

   This topic will be covered by the documentation of the dvdauthor
program.  For questions please see dvdauthor In general it will work
like this:

   `> makedvd stream1.mpg stream2.mpg ... my_dvdlikestream.mpg'

   You will get a directory with AUDIO_TS and VIDEO_TS directories.
Burning the data from the disk to a DVD+R/+RW writer would be done like
this:

   `growisofs -Z /dev/scd2 -dvd-video mydvd/'

   If you own a DVD+RW/+R drive a good place for more information is:
DVD+RW/+R for Linux page. You also need a version of the cdrtools with
`dvd-video' support.  The cdrtools 1.11a27 is known to work but newer
versions already exist.

   For other writers the commands to write a DVD will be different. You
can get some more information in the dvdauthor package. There is no
guarantee that it will work at all !!!

Creating DIVX Videos
====================

lav2avi-sh
----------

   Another way of creating DIVX is the program `mencoder' which is from
the mplayer project.  http://www.mplayer.hu/.  For more information
about mencoder please read mencoder/mplayer help and documents. A first
and a second pass give at the end of pass hints for bitrate which can
be used for encoding to specific size (650 MB, 700 MB and 800 MB). The
script lav2avi.sh uses this information if provided (for short streams
it is omitted by mencoder).  Look for parameter `preferedSize' in the
script. You can also specify other parameters used for encoding with
`encoderParam' option in the script. For a description of the usable
parameters take a look in the mplayer/mencoder manual.

   The outputfilename is that name of your input file (first option)
but with the extension avi. If the size of file is less then specified
by `preferedSize' it's because the source was of very high quality (no
noise) and the specified bitrate was higher than required.  You usually
get 700MB for 1.5 hour film at half image size with bitrate around 900
that means for divx good quality (assuming good quality source material
of course).

   The script does a 3 step encoding:

   * 1st step - audio encoding

   * 2nd step - first video pass

   * 3rd step - second video pass

   The mplayer/mencoder documentation deprecates the use of the 3 pass
encoding method (it can cause A/V sync problems) and recommends the use
of the 2 pass method.   The mencoder/mplayer documentation is extensive
and has many helpful hints (and a bitrate calculator in the TOOLS/
directory).

   For encoding use the fast ffmpeg (lavc) codec. It gives nice results
together with high good performance. For audio encoding mp3 is used.
For encoding of all parts it uses unix pipes. This mean that you DO NOT
need additional space on your hard drive where all glav manipulations
will be done. For audio encoding the script uses a FIFO queue.

   If you want to tweak the script for your own needs, use these hints:
   * Output of 1st step is file called `frameno.avi' with encoded audio

   * 2nd step is using `frameno.avi' and output is text file called
     `lavc_stats.txt' with timing informations

   * 3rd step is using `frameno.avi' and `lavc_stats.txt' for encoding
     the stream to the output file `movie2.avi'

   * If you want change only video bitrate, keep the file `frameno.avi'
     comment out the 1st step encoding and repeate 2nd and 3rd step.
     Dont forget to remove the line where the `frameno.avi' is removed.

Optimizing the stream
*********************

   Using filters helps to increase the image quality of constant
bitrate (CBR) video streams. With VBR (variable bit rate) video the
filesize is reduced.

   Example:

   `> lav2yuv stream.avi | yuvmedianfilter | mpeg2enc -o video.m1v'

   Here the yuvmedianfilter program is used to improve the image. This
removes some of low frequence noise in the images. It also softens the
image a little. It takes a center pointer and averages the pixels
around it that fall within the specified threshold. It then replaces
the center pixel with this new value.  You can also use the -r (radius)
option for an other search radius.

   `NOTE:'a radius greater than the default value of 2 is horrendously
slow!

   yuvmedianfilter has separate settings for luma and chroma. You can
control the search radius and the trigger threshold independently.  If
you use a threshold of 0 then filtering is disabled (-t 0 disables luma
filtering, -T 0 disables chroma filtering).

   `> lav2yuv stream.avi | yuvmedianfilter -r 3 -t 4 -T 0 | mpeg2enc -o
video.m1v'

   This example uses a search radius of 3 pixels for the luma, a
threshold of 4 (the default is 2), and disables filtering for the
chroma components.   Sometimes, depending on the source material,
median filtering of the chroma can cause a slight color shift towards
green.   Filtering on the luma component (disabling the chroma
filtering) is the solution to that problem.

   Example:

   `> lav2yuv stream.avi | yuvdenoise | mpeg2enc -o video.m1v'

   Now we are using yuvdenoise to improve the image. The filter mainly
reduces color and luminance-noise and flickering due to phase errors
but is also effective at removing speckles.  If you want yuvdenoise to
deinterlace the stream use the -F option.

   By default yuvdenoise denoises interlaced if the input is interlaced.
You can of course change the denoiser threshold (-t), as well as the
different search radius (-r). You can also set a `active area' where
everything outside that area is set to real black.   Creating a black
border can lower the bitrate of the encoded stream because pure black
areas compress much better than noise (captures from analog sources such
as VHS and 8mm usually have several lines at the time and bottom that
are very noisy).

   yuvdenoise uses a different approach to filter the noise.  More
information about how yuvdenoise works as well as descriptions of its
options are found in the manpage.

   If you have a high quality source you can only use `fast mode'. In
the fast mode yuvdenoise only uses the bitnoise reduction.  You might
also use the mpeg2enc `-h/-keep-hf' option. That option tells mpeg2enc
to keep as much high frequency information as possible.   Using -h will
greatly increase the bitrate (filesize).  If the bitrate is too close to
the maximum (set with -b) the encoder will have to decrease the quality
to avoid exceeding the maximum bitrate.

   A builtin filter in mpeg2enc is the -N/-reduce-HF option.  This
option is not really filter in the usual sense.  Rather it changes how
exactly the high frequency information is encoded.  Often the high
frequency is noise.  You also have high frequencies on sharp borders or
transitions. The -N option can have values between 0.0 and 2.0 where 0.0
does nothing (disables the high frequency quantizer boost) and 2.0 gives
the maximum quantization boost.  The value to use depends on the desired
output quality and filesize.  Values of -N less than 0.5 are very subtle
while a value of 1.0 will achieve a good balance between bitrate
reduction and output quality.   Using -N values above 1.5 will
noticeably reduce the sharpness of the output picture and are normally
used only for poor quality sources (VHS tapes for example).

   Using yuvmedianfilter's capability to only filter the chroma (-T) is
moderately effective at reducing noise in dark scenes without softening
the image during normal (brighter) scenes.   Median filtering of the
luma (-t) will produce a lower bitrate but can cause loss of detail
(softening).  Chroma only medianfiltering is less agressive and is a
good choice to use in combination with yuvdenoise.

   Combining the filters yuvdenoise, yuvmedianfilter and the mpeg2enc -N
option gives a very fine degree of control over the bitrate (filesize).
The reduction (or increase) in the bitrate depends on the source
material and the exact encoding/filter options used.  So we can give no
exact numbers how much each option and combination will reduce the
filesize, only guidelines.

   Usually you should use the -N option in a range from 0.5 to 1.5.
Below 0.5 it does not reduce the bitrate very much (but does preserve
sharpness).  At 1.5 and higher you will notice a softening in the video
and possibly artifacts (halo/ringing) around edges of objects
(text/subtitles especially).  If you combine the filters you should use
yuvdenoise and maybe afterwards yuvmedianfilter.  Maybe yuvmedianfilter
even after scaling.  Having yuvmedianfilter in the chain does not
reduce the bitrate that much.  Often the use of yuvdenoise is enough.
The yuvmedianfilter helps much if you have low quality sources, and not
that much if you already have a rather good quality.  When you combine
the filter and option you will very likely reduce the filesize to about
the half of the filesize without using the options and programs.

   In general aggressive filtering will produce smaller files (lower
bitrate) but reduce the quality (details) of the picture.  Less
aggressive filtering/processing will preserve more detail but result in
larger files.

   Example:

   `> lav2yuv stream.avi | yuvkineco -F 1 | mpeg2enc -o video.m1v'

   yuvkineco is used for NTSC sources. It does the conversation from
30000.0/1001.0 (about 29.97) fps to 24000.0/1001.0 (about 23.976) fps,
you can call it "reverse 2-3 pulldown" more info about this in the
README.2-3pulldown. yuvkineco does only remove NTSC specific problems.

   If you want to improve the image you should also use yuvdenoise:

   `> lav2yuv stream.avi | yuvkineco | yuvdenoise | mpeg2enc -o
video.m1v'

   Example

   `> lav2yuv stream.avi | yuvycsnoise | mpeg2enc -o video.m1v'

   yuvycsnoise is also used for NTSC and is specialized for NTSC Y/C
separation noise. If video capture hardware has only a poor Y/C
separator then at vertical stripes (especially red/blue) noises appear
which seem checker flag and bright/dark invert per 1 frame.
yuvycsnoise reduces noises of this type. You can also use different
thresholds for luma/chroma and the optimizing method.  This filter is
not needed with working with DV (Digital Video) data.

   yuvycsnoise works only correct when we have NTSC with:

   * full height (480 lines)

   * full motion captured (29.97 fps)

   * captured with poor Y/C separator hardware

   For more information about the yuvkineco and yuvycsnoise read the
README in the yuvfilters directory.

   If you want to experiment to determine the optimal settings for the
denoiser, scaler and so on replace the mpeg2enc with yuvplay.  yuvplay
plays back the yuv frames so you can see if the options you have chosen
are making the thing better or worse.

   A command would look like this:

   `> lav2yuv stream.eli | yuvdenoise -options | yuvscaler -options |
yuvplay'

   If you are looking for a hardware device that can make the video look
better before you record it we currently know about two firms that
produce such boxes.

   One produced by SIMA: http://www.simacorp.com/scc.htm, that device
will work with NTSC.  And a one other produced by ELV (german
distributor): http://www.elv.de there you find in the SHOP area, a
section where you can take a look at their Video - Audio devices. Most
of the  ELV devices work only with PAL.

   If you want to know how much each tool lowers the average bitrate.
You can use this table to see what you can expect if you have a full
size video and want to create a DVD with a qality factor of 5 and the
allowed maximal bitrate of 8500kb/sec.

   * no denoising : 8300 kb/s (mostly hitting the upper bound)

   * yuvenoise : 7700 kb/s

   * mpeg2enc -reduce-hf : 7400 kb/s

   * yuvdenoise + yuvmedianfilter : 6000 kb/s

   * yuvdenoise + mpeg2enc -reduce-hf : 4900 kb/s

   * all of the above : 3600 kb/s

   While `-N|-reduce-hf' or yuvdenoise alone is only a modest
improvement, together they reduce the bitrate substantially.  There is
not really much visible difference between using yuvdenoise alone and
yuvdenoise with mpeg2enc -reduce-hf. The usefull values are between 0.0
and 1.5. Where you can say that the higher the quality factor you want,
the less this option improves. At a quality factor 4 you save using -N
1.0 about 1%. If you want a quality factor of 9 and use the -N 1.0 you
might save up to 40%. But you might save less, that depends on the
video you encode!!!

   If you ask yourself why not alyways use all of the above filters?
Hmmm, hard question. The image softens, and the encoding time increases.
Each filter needs about the same amount of time as mpeg2enc needs for
encoding the video.

   If you have very high quality material and want to keep every detail
you should try to use the mpeg2enc -keep-hf|-h on the other hand

   `Note:' The bitrate reduction you have depends on the material and
on the noise of the images.

   A other interresting mpeg2enc option is the `-E|-unit-coeff-elim'
option. This option is disabled by default. If you enable it, a special
"unit coefficient elimination" algorithm, is applied to the encoded
picture blocks.   Basically this proceedure forces blocks of a type
that do not carry much information (but use many bits to encode) to be
skipped. A negative value examines the base (DC) as well as the AC
coefficients. A positive value means that only texture (AC)
coefficients are examined and possibly zeroed.  The recommended values
lies between -20 and +20. You usually can expect that you have a 5%
decreased filesize. The amount the bitrate is reduced can vary
considerably, the range spans from not really noticable up to 20%.

   If you think a other quantization matrice will help use the
`-K|-custom-quant-matrices' option. You can try out your own
quanitsation matrice or use another builtin than the default.  You can
choose between kvcd, tmpgenc, hi-res, and your own. Using -K usually
makes the file smaller except the hi-res option (that makes files
considerably larger). Exact guidelines are hard to give, sometime a
other quanitsation matrix saves almost nothing, and the next time up to
20%. More than 20% is very unlikely, 10-15% at a moderate qualityfactor
(-q 8-10) are likely.  The higher the qualiy the less it saves, at a
quality factor of 4-6 the reduction in bitrate may only be 5%

   One thing to keep in mind is that the unit coefficient elimination
and the quantization matrix option are decreasing the bitrate while
maintaining the same visual quality.   At this point you can chose to
use the smaller file to increase the amount of video that will fit on
the disc media or you could chose to increase the quality even more by
lowering the -q value by 1 and make a larger (but higher quality) file.

Scaling and offset correction
=============================

   The basic scaling is described in the `Converting video' section

   The scaling, takes a part of the picture, and scales it to a larger
or smaler size. The scaling is done by yuvscaler:

   `lav2yuv test.eli | yuvscaler -I USE_400x400+50+100 | yuvplay'

   Here we only take part of the picture and scale it up to the size of
the original frame.  But yuvscaler also changes the pixel aspect ratio.
That means when you look at the stream using yuvplay it looks like a
square in our example.  After scaling, if the sample (pixel) aspect
ratio were not changed, the video would not display with the proper
aspect ratio.  Yuvscaler compensates by adjusting the sample aspect
ratio.  If you have a interlaced video, the height and HeightOffset
have to be a multiple by 4 if the video is interlaced. Else the values
(width, height, widthoffset, heightoffset) have to be a multiple of 2.

   A problem that cannot be solved easily with scaling is when the
picture is not centered horizontal. On one side you have no black
pixels and on the other you have 30 for example. Scaling is here is the
wrong solution.  y4mshift is the perfect solution because it can shift
the image to the left or right.

   `lav2yuv test.eli | y4mshift -n 20 | mpeg2enc -f 3 -b 4000 -q 10 -o
video.m2v'

   That will shift the image 20 pixels to the right. If you use a
negative the image is shift to the left. You have to use a even number.
The inserted pixels are set to black.

   Some might wonder why the image is not centered and there is a black
border around the image when you view what you have recorded. The
reason for the black border is the TV (CRT = Catode Ray Tube) and a
really interresting storry about how the TV standart was definden. But
tha topic is described in other books.

   The TV does not show the full picture. A part of the picture is not
shown because the TV sets overscan (sometimes as much as 10% but more
common today is 5%).  But when you capture the video with a card you
see the whole image including the border that TVs lose due to
overscanning.  A horizontal offset is usually not a problem of the
capture card. It is a problem when the film is broadcast and not well
synchronized with the image.  This means that the scan of the source
not exactly synchronized with the carrier signal, you wont see that on
TV.