mjpeg-howto: Optimizing the stream

 
 8 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.
 
    yuvdenoise denoises interlaced if the input is interlaced.  You can
 of course change the denoiser threshold (-g/t).  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). For this you can use the scaler
 
    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 should lower the filter to
 levels like that: -g 0,255,255 -t 2,2,2.  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 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.
 

Menu