mpeg2enc(1)

NAME
mpeg2enc - MPEG 1/2 encoder

SYNOPSIS
mpeg2enc [-v|--verbose num] [-M|--multithread num_CPU] [-f|--format mpeg_profile] [-b|--video-bitrate bitrate_kbps] [-V|--video-buffer video_buf_size_kB] [-q|--quantisation quantization] [-r|--motion-search-radius motion_search_radius] [-Q|--quantisation-reduction 0.0-4.0] [-p|--3-2-pulldown] [-I|--interlace-mode 0..2] [-s|--sequence-header-every-gop] [-g|--min-gop-size min_gop_size] [-G|--max-gop-size max_gop_size] [-P|--force-b-b-p] [-n|--video-norm n|p|s] [-F|--frame-rate frame_rate_code] [-a|--aspect aspect_ratio_code] [-z|--playback-field-order b|t] [-4|--reduction-4x4 1..4] [-2|--reduction-2x2 1..4] [-S|--sequence-length size_MB] [-B|--nonvideo-bitrate bitrate_kbps] [-N|--reduce-hf] [-h|--keep-hf] [-d|--no-dummy-svcd-SOF] [-C|--correct-svcd-hds] [--hack-dxr2-bug] [-?|--help] -o output_filename

DESCRIPTION
mpeg2enc is heavily enhanced derivative of the MPEG Software Simulation Group's MPEG-2 reference encoder. It accepts streams in a simple planar YUV format "YUV4MPEG" produced by the lav2yuv and related filters (e.g. yuvscaler(1)) from the mjpegtools(1) package. A output plug-in to the mpeg2dec(1) MPEG decoder is available to permit its use in transcoding applications. The encoder currently fully supports the generation of elementary MPEG-1, progressive and interlaced frame MPEG-2 streams. Field encoded MPEG-2 is also possible but is not currently maintained or supported. For most purposes this elementary stream output will need to be multiplexed with one or more audio streams into a program/systems stream using the mplex(1) tool. Note that although this manual page aims to explain how mpeg2enc can be used effectively it is not intended as an introduction to MPEG-1/2 video which is a fairly complex topic in its own right. The MPEG video format is a somewhat baroque standard with many many options, not all of which necessarily easy to explain or even particular useful in the context of a software encoder. Much useful practical information for novices can be found in the mjpeg-HOWTO document that should have been installed with mjpegtools(1) package. Further information and useful supporting software can be found on the mjpegtools web-site:

    http://mjpeg.sourceforge.net

SELECTING THE MPEG PROFILE

Set the MPEG profile to use. The MPEG standards support a vast number of options. In practice, different applications of the MPEG format place additional constraints of the form the MPEG stream. Setting this flag selects the kind of stream to produce.

-f|--format 0
    -    Generic MPEG1.

    A basic MPEG-1 profile that lets most parameters
    be adjusted for particular applications using the other flags.
    Typical    applications would be to produce a variable bit-rate MPEG-1
    stream with big buffers and a high data-rate software playback
    on a computer.

-f|--format 1
    -    Standard VCD.

An MPEG1 profile exactly to the VCD2.0 specification. Flag settings that would result in a non-standard stream structure are simply ignored.

-f|--format 2
    -    User VCD.
As for profile 2 but bit-rate and video buffer size can be set to non-standard values. Frame size may also be non-standard.

-f|--format 3
    -    Generic MPEG2.

A basic MPEG-1 profile that lets most parameters be adjusted for particular applications using the other flags. Typical applications would be to produce a MPEG-2 stream with big buffers and long GOP's for software playback on a computer.

-f|--format 4
    -    Standard VCD.

An MPEG-2 profile exactly to the SVCD2.0 specification. Flag settings that would result in a non-standard stream structure are simply ignored.

-f|--format 5
    -    Non-standard SVCD.

As for profile 4 but bit-rate, video buffer size, GOP sizes and structure can be set to non-standard values. Frame size may also be non-standard.

-f|--format 6
    -    VCD Stills sequence.

Encodes the special style of MPEG stream used for still images on VCDs. To use this profile you must set the target size you wish to compress the images to using the -T flag. Reasonable values are around 35KB for standard resolution stills (352 wide) and 120KB for high resolution stills (704 wide).

-T num
    Set the target size for VCD still images in KB.

-f|--format 7
    -    SVCD Stills sequence.

Encodes the special style of MPEG stream used for still images on SVCDs. Both standard (480 wide) and high resolution (704 wide) images are supported.

-f|--format 8
    -    DVD MPEG-2.

Just a very basic implementation. Useful with DXR2 board and similar hardware that can decode MPEG-2 only if it is presented in a DVD like form.

GENERAL FUNCTION LETTERS

-v|--verbose num

Set verbosity level to num. 0 = warnings and errors only, 1 = information as well, 2=really verbose.

-?|--help

Display a synopsis of the command syntax.

FUNCTION LETTERS ADJUSTING THE SELECTED PROFILE
N.b. If the profile you have selected sets particular values for these parameters it will over-ride these adjustment flags. In particular, there is almost nothing that can be adjusted for the standard VCD and SVCD profiles.

-b|--video-bitrate num

The bit-rate of the output video stream in k Bits/sec. The default is exactly the bit-rate required for VCD streams. A sensible bit-rate to set for SVCD is 2500. If variable bit-rate mode has been selected (see the -q option) this is the maximum bit-rate of the stream.

-V|--video-buffer num

The maximum video buffer usage required to decode the stream in KBytes. The default is 46KB the (tiny) size specified for VCD. The size to use for SVCD is the (more reasonable) 230KB. If you are encoding for a half-decent software decoder it makes sense to push this up to 500K or more.

-s|--sequence-header-every-gop

This flag forces the encoder to generate a "sequence header" at the start of every group-of-pictures. This is needed by some player hardware to support fast forward/rewind/random access functions but is a waste of bits otherwise.

-d|--no-dummy-svcd-SOF

The SVCD MPEG-2 profile demands that special "Scan OFfset" which are (in effect) pointers to the place on the final SVCD disk where the video for 0.5 and around 5-10 seconds behind and ahead in the stream is located. The intended use of this information is to support"Fast forward/Rewind" functions. Unfortunately, at the time mpeg2enc encodes the video it doesn't know where the video is going to finally end up. So special dummy "Scan OFfset" values are written which are intended to be filled in during the creation of the SVCD image. Currently the GNU vcdimager tool handles this task. However, in some circumstances the dummy offsets can cause problems. This flags stops mpeg2enc generating them.

--correct-svcd-hds

In the official SVCD standards the field in the MPEG-2 header information that passes on the encoders "recommended" horizontal resolution to decode the stream to is supposed to take the values 540 (for 4:3 sequences) or 720 (for 16:9 sequences). In practice many players don't work unless the value is 480. This flag, forces mpeg2enc to follow the official standard. It is worth trying if 16:9 sequences play at 4:3 aspect ratio.

--hack-dxr2-bug

This flag, as its name suggests, works around what appears to be a bug in MPEG-2 decoding in the firmware of the DXR2 board. Useful for encoding DVD-like streams for playback using a DXR2.

OPTION LETTERS CONTROLLING VIDEO PARAMETERS

-n|--video-norm n|p|s Force the input stream to be treated as NTSC|PAL|SECAM regardless of what the stream header might suggest. Basically this just sets the defaults for a bunch of other options.

-F|--frame-rate num Set the frame-rate of the output-stream. Currently only the standard MPEG rates are supported. Eventually more-or-less arbitrary rates will be possible.
 0 - illegal
 1 - 24000.0/1001.0 (NTSC 3:2 pulldown converted FILM)
 2 - 24.0 (NATIVE FILM)
 3 - 25.0 (PAL/SECAM VIDEO / converted FILM)
 4 - 30000.0/1001.0 (NTSC VIDEO)
 5 - 30.0
 6 - 50.0 (PAL FIELD RATE)
 7 - 60000.0/1001.0 (NTSC FIELD RATE)
 8 - 60.0

-a|--aspect num Set the playback aspect ratio code of the encoded video.
 1 - 1 - 1:1 display
 2 - 2 - 4:3 display
 3 - 3 - 16:9 display
 4 - 4 - 2.21:1 display

    For MPEG-2 the specified aspect ratios are used directly. For MPEG-1 mpeg2enc infers the MPEG-1 pixel aspect code from the video norm specified and the specified playback aspect ratio.

-p|--3-2-pulldown

Setting -p only makes sense for 24frame/sec Movie source material. It sets flags in the output stream that tell the decoder to play the movie as NTSC 60field/sec video using "3:2 pulldown". This is vastly more efficient than encoding as 60field/sec video. The classic application is to transcode a PAL-encoded movie (24fps played too fast at 25 fps!) into NTSC (see the -f flag).

OPTION LETTERS FOR CONTROLLING COMPRESSION AND SPEED

-M|--multithread num_cpu

MPEG encoding is a task that can be split over a small number of CPU's quite efficiently. Mpeg2enc can be internally set to split major processing tasks between a number of concurrent threads. This flag adjusts the multi-threading to the optimum to utilise the specified number of CPU's.

It should be noted that even with 1 CPU present some multi-threading is performed: frame input takes place in parallel with encoding. This allows good performance to be achieved when when a seperate machine is being used for pre-processing (decoding from MJPEG, scaling, denoising etc) with the final result pipe to mpeg2enc (e.g. using rsh or ssh).

Setting -M 0 disables all multithreading. This is sometimes useful for debugging or to achieve maximum CPU efficiency on a shared machine. Setting -M 3 on a dual-CPU machine will produce slightly faster results than -M 2 at the price of slightly less CPU efficiency. This is useful if nothing else needs to be done on the encoding machine. In practice there is little point setting -M greater than 4 even if the CPU's are available due to the fairly coarse-grained parallelism used.

-q|--quantisation 2..31

Minimum quantisation of the output stream. Quantisation controls the precision with which image information is encoded. The lower the number the higher the quality but the greater the required data-rate. For VCD resolution anything below 8 or so produces pretty good quality video. For SVCD the equivalent level is around 10. If this option is set a variable bit rate stream is produced. This is more efficient overall but variable bit-rate MPEG-1 cannot be played by many hardware decoders and/or DVD/(S)VCD drives. If you intend to use a software decoder you'd be insane not to use variable bit-rate. If this option is set without a maximum bit-rate being specified then quantisation is fixed at the specified value.

-I|--interlace-mode 0|1|2

This sets the sequences picture structure and block encoding type for MPEG-2 streams. Setting 0 encodes frame-by-frame with support for interlaced video turned off. This is the most efficient option for encoding material that is not interlaced (e.g. movies in PAL 25 frame/sec or NTSC 24 frames/sec in 3:2 pulldown format). However, it produces rotten results for interlaced video material. For such material use -I 1 which encodes frame-by-frame with interlace-adapted motion compensation and block encoding. Alternatively, for highly textured interlaced material with lots of motion you may with to try -I 2 which switches to field-by-field encoding. This is generally less efficient than frame-by-frame encoding but will produce more accurate results.

    Note that setting -I 1 for non-interlaced material will not do any harm but the encoder will waste a lot of time on calculations that aren't needed.

-g|--min-gop-size num

-G|--max-gop-size num

These flags set the minimum and maximum group-of-picture (GOP) size for the output MPEG stream. By default both are set to 12 to suit VCD encoding. If the two values are not identical mpeg2enc applies a simple scene-change detection procedure to try to select GOP sizes that ensure big changes of image coincide with the fully-encoded I-frame starting a new GOP. This can help prevent transient "blockiness". For VCD/SVCD/DVD the largest GOP size is 15 for PAL or 18 for NTSC. Reasonable minimum GOP sizes are 6 or 9. A larger GOP size can help reduce the bit-rate required for a given quality. However, this really only applies to high-quality source material with little noise (e.g. digital video). For broadcast material there is little point setting GOP size much beyond 21 or 24. Even with good source material diminishing returns set in quite rapidly. Note: Mpeg2enc is currently hard-wired to produce 2 B frames between each I/P frame unless the GOP size forces less. This is reasonable for medium to high bit-rates (>= 1Mbps) but probably sub-optimal for low-bit-rate encoding.

-P|--force-b-b-p

This flag forces the GOP size selection to choose sizes that ensure 2 B frames appear between adjacent I/P frames. Several common MPEG-1 decoders can't handle streams where less than 2 B-frames appear between I/P frames.

-Q|--quantisation-reduction 0.0..4.0

This flag sets the amount quantisation is reduced for blocks containing unusually large amounts of sharp image detail. Setting this value 0.0 produces the most efficient use of bits but may cause visible/artefacting around detailed sections. A larger value may help suppress artefacts, however, on noisy source material this may cause a "swimming" effect on textured backgrounds as the noise cause blocks to be boosted at random. The default is 0.0 (off).

-r|--motion-search-radius num

This flag sets the motion estimation search radius. For most purposes the default (16) should be just fine. For high-resolution MPEG-2 and active scenes it may be worth bumping it up. However, this will make encoding significantly slower. There is little point reducing the radius. Speed gains are not huge and the impact on quality can be marked.

-4|--reduction-4x4 1 .. 4

-2|--reduction-2x2 1 .. 4

These options control how radical the encoder is in throwing away apparently poor candidate estimates during motion estimation. A setting of 1 means very few blocks are discarded early which makes for slow encoding but quality as good as it gets. A setting of 4 makes for fast encoding but can impact quality. The -4 flag controls discarding during the initial 4*4 sub-sampled search stage, the -2 flag controls discarding during the secondary 2*2 sub-sampled stage.

    These flags are useful as the speed quality trade-off is markedly different depending on which CPU you have. On modern machines the impact on speed is around a factor 2 on older machines a factor 3. The impact on quality is around 10% quantisation (0.2 of a bit of precision in encoding textures). For most purposes the default settings will be fine. However on P-III Katmai etc -4 2 -2 1 gives a good near-optimum quality setting with reasonably speed.

-N|--reduce-HF

Setting this flag adjusts the way texture detail is quantised to reduce the precision with which of high-frequency information encoded. This is very useful for mildy noisy sources. If you have really noisy material the filtering tools available in mjpegtools are a much better bet.

-h|--keep-hf

Setting this flag makes the encoder encode as much high-frequency information as possible. This is a good setting for maximising quality at VCD resolution with good quality low-noise source material. It can also help with "swimmy" material if you can spare the bit-rate!

OPTION LETTERS FOR CHUNKING THE OUTPUT STREAM

-S|--sequence-length num

This flag allows the target size of individual sequences in the final multiplexed stream to be set in MBytes. If set mpeg2enc keeps track of how large the eventual stream is getting and inserts a sequence split (actually: sequence end / sequence start) into the output stream each time it reaches the specified limit. The multiplexor mplex(1) can recognise these splits and start a new multiplexed output file each time it encounters one. In this way it is easy to automatically ensure each component sequence file can be burnt onto a CD-R and still be played as a stand-alone MPEG sequence.

-B|-nonvideo-bitrate num

Since mpeg2enc can't read minds it cannot know in advance what other material will be multiplexed with the output video stream. Thus to get its calculations of where to insert split point right it needs to be told the combined data-rate of the other material that is eventually to be multiplexed with the video. This flag allows this rate to be specified in K bits/sec. A good rule of thumb is to use the total rate of all the other streams plus 1% of the total rate including video.

SSE, 3D-Now!, MMX
mpeg2enc makes extensive use of these SIMD instruction set extension on x86 family CPU's. The routines used are determined dynamically at run-time. It should be noted that using SSE requires operating system support. Old 2.2.x Linux kernels (unless patched ones like RedHat) do not have this and so SSE, although physically present, won't be activated.

BUGS
There should be an option to force GOP sizes that permit 2 B frames between I/P frames. Some decoders (even software) can't handle the case where I/P frames come back to back or with only 1 B frame between them. There really should be some kind of dynamic noise-reduction algorithm someplace in the mpegtools tool chain. There needs to be a facility for writing dummy user-data fields so that the multiplexer/imager can insert forward/backward pointers when muxing/imaging an SVCD. Is there some kind soul out there with source-code for a good SSE (not MMX) DCT and iDCT?

AUTHOR
This man page was written by Andrew Stevens. If you have questions, remarks, problems or you just want to contact the developers, the main mailing list for the MJPEG-tools is:
  mjpeg-users@lists.sourceforge.net For more info, see our website at
  http://mjpeg.sourceforge.net

SEE ALSO
mplex(1), mp2enc(1), lavrec(1), lavplay(1), lav2yuv(1), lav2wav(1), yuvscaler(1), yuvdenoise(1),mjpegtools(1)