Microsoft MPEG-2 Video Encoder

[The feature associated with this page, DirectShow, is a legacy feature. It has been superseded by MediaPlayer, IMFMediaEngine, and Audio/Video Capture in Media Foundation. Those features have been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use MediaPlayer, IMFMediaEngine and Audio/Video Capture in Media Foundation instead of DirectShow, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]

The Microsoft MPEG-2 Video Encoder filter encodes MPEG-2 and MPEG-1 video.

To encode and multiplex audio/video streams, use the Microsoft MPEG-2 Encoder filter, which encapsulates the functions of both this filter and the Microsoft MPEG-2 Audio Encoder filter.

Note

This filter is not supported on IA-64-based platforms.

 

Filter Information

Filter Interfaces

IBaseFilter
ICodecAPI
IEncoderAPI
IMediaSeeking
IVideoEncoder

Input Pin Media Types

MEDIATYPE_Video, MEDIASUBTYPE_I420
MEDIATYPE_Video, MEDIASUBTYPE_IYUV
MEDIATYPE_Video, MEDIASUBTYPE_RGB24
MEDIATYPE_Video, MEDIASUBTYPE_UYVY
MEDIATYPE_Video, MEDIASUBTYPE_YUY2
MEDIATYPE_Video, MEDIASUBTYPE_YV12

Input Pin Interfaces

IMemInputPin
IPin
IQualityControl

Output Pin Media Types

MEDIATYPE_Stream, MEDIASUBTYPE_MPEG2_VIDEO
MEDIATYPE_Stream, MEDIASUBTYPE_MPEG2_PROGRAM
MEDIATYPE_Stream, MEDIASUBTYPE_MPEG2_TRANSPORT
MEDIATYPE_Video, MEDIASUBTYPE_MPEG2_VIDEO

Output Pin Interfaces

IMediaSeeking
IPin
IQualityControl

Filter CLSID

CLSID_CMPEG2EncoderVideoDS (declared in wmcodecdsp.h)

Executable

msmpeg2enc.dll

Merit

MERIT_DO_NOT_USE

Filter Category

CLSID_LegacyAmFilterCategory

 

Remarks

The MPEG-2 Video Encoder can produce the following kinds of output:

  • Video elementary stream
  • Video in an MPEG-2 program stream
  • Video in an MPEG-2 transport stream

It supports the following MPEG-2 profiles and levels:

Profile Levels Remarks
Simple Profile Main
Main Profile Low, Main, High, High-1440
High Profile Main, High, High-1440 No scalability or 4:2:2/4:4:4 support (only 4:2:0)
4:2:2 Profile Main, High No scalability or 4:2:2 support (only 4:2:0)

 

Codec Properties

The filter supports the following properties through ICodecAPI.

Property Default Supported Values
AVEncCodecType MPEG-2 video CODECAPI_GUID_AVEncMPEG1Video
CODECAPI_GUID_AVEncMPEG2Video
AVEncCommonBufferInLevel 12222464 bits
AVEncCommonBufferOutLevel 12222464 bits
AVEncCommonBufferSize 12222464 bits
AVEncCommonFormatConstraint Unspecified CODECAPI_GUID_AVEncCommonFormatUnSpecified (No format constraint)
CODECAPI_GUID_AVEncCommonFormatDVD_V (DVD-Video)
CODECAPI_GUID_AVEncCommonFormatVCD (Video CD)
AVEncCommonMaxBitRate 9800000 (9.8 Mbits/second)
AVEncCommonMeanBitRate 7000000 (7.0 Mbits/second)
AVEncCommonMinBitRate 128
AVEncCommonMultipassMode 1 1
AVEncCommonQuality 100 1 — 100
AVEncCommonQualityVsSpeed 75 0 — 100
AVEncCommonRateControlMode CBR eAVEncCommonRateControlMode_CBR
eAVEncCommonRateControlMode_PeakConstrainedVBR
eAVEncCommonRateControlMode_Quality
AVEncInputVideoSystem Unspecified eAVEncInputVideoSystem_Unspecified
eAVEncInputVideoSystem_PAL
eAVEncInputVideoSystem_NTSC
AVEncMPVDefaultBPictureCount 2 0 — 2
AVEncMPVFrameFieldMode Frame mode
AVEncMPVGenerateHeaderSeqDispExt TRUE
AVEncMPVGenerateHeaderSeqExt TRUE
AVEncMPVGOPOpen FALSE
AVEncMPVGOPSInSeq 1 0 — 1
AVEncMPVGOPSize 18 frames (36 fields) for NTSC; 15 frames (30 fields) otherwise. 1 — 30; see Remarks
AVEncMPVIntraDCPrecision 9 8 — 10
AVEncMPVLevel High
AVEncMPVProfile Main
AVEncVideoDefaultUpperFieldDominant TRUE
AVEncVideoForceSourceScanType Interlaced eAVEncVideoSourceScan_Interlaced
eAVEncVideoSourceScan_Progressive
AVEncVideoInputChromaResolution 4:2:0 eAVEncVideoChromaResolution_420 (4:2:0)
eAVEncVideoChromaResolution_SameAsSource
AVEncVideoInputChromaSubsampling Same as source
AVEncVideoInputColorNominalRange Same as source
AVEncVideoInputColorPrimaries Same as source
AVEncVideoInputColorTransferFunction Same as source
AVEncVideoInputColorTransferMatrix Same as source
AVEncVideoMaxKeyframeDistance AVEncMPVGOPSize - 1 0 or AVEncMPVGOPSize - 1
AVEncVideoNoOfFieldsToEncode 0
AVEncVideoOutputChromaResolution 4:2:0 eAVEncVideoChromaResolution_420 (4:2:0)
eAVEncVideoChromaResolution_SameAsSource
AVEncVideoOutputFrameRate Must be the same as the input frame rate.
AVEncVideoOutputScanType Same as input eAVEncVideoOutputScan_SameAsInput
AVEncVideoPixelAspectRatio 1:1

 

It is recommended to set properties in the following order:

  1. AVEncCommonFormatConstraint
  2. AVEncCodecType
  3. AVEncMPVProfile
  4. AVEncMPVLevel
  5. AVEncInputVideoSystem

Set the remaining properties in any order. (However, see GOP Structure.)

It is possible to set properties while the filter graph is running. There is a delay of at least one GOP before the new settings take effect.

Encoder Operation

When encoding MPEG-1 video, the encoder automatically sets the 1-bit constrained_parameters_flag code in the sequence header if all constraints are met.

If needed, the encoder rounds up the input video dimensions so that the output video dimensions match the MPEG requirements. For progressive video, the output dimensions are rounded up to a multiple of 16 in both width and height. For interlaced video, width is rounded up to a multiple of 16, and height is rounded up to a multiple of 32. This round-up operation uses padding as needed.

If the video is interlaced, the encoder performs automatic telecine (3:2 pull-down) detection. The input video can contain field picture pairs, in addition to interlaced frames.

The encoder's internal format is 4:2:0 IYUV (identical to I420). It can perform color conversion from YUY2, YV12, UYVY, and RGB-24 video formats.

To constrain the bitstream to a target format (DVD or VCD), set the AVEncCommonFormatConstraint property. If this property has a value other than GUID_AVEncCommonFormatUnSpecified, the encoder limits the MPEG syntax to that allowed by the target format.

For live encoding, set the AVEncCommonQualityVsSpeed property to zero. This causes the encoder to optimize for speed.

Encoding Modes

The encoder supports several encoding modes:

  • One-pass constant bit rate (CBR).
  • One-pass quality-based variable bit rate (VBR), using a constant quantizer step size. In this mode, the encoder attempts to meet a target quality level, up to a maximum bit rate.
  • One-pass peak-constrained VBR. In this mode, the encoder attempts to achieve a target average bit rate within certain internal limits.

To configure the encoding mode, set the following properties:

Mode Properties
CBR AVEncCommonRateControlMode = eAVEncCommonRateControlMode_CBR
AVEncCommonQualityVsSpeed
AVEncCommonMeanBitRate
Quality-based VBR AVEncCommonRateControlMode = eAVEncCommonRateControlMode_Quality
AVEncCommonQuality
AVEncCommonMaxBitRate
Note: In this mode, the AVEncCommonMeanBitRate and AVEncCommonMinBitRate properties are not used. The minimum bit rate is assumed to be zero.
Peak-constrained VBR AVEncCommonRateControlMode = eAVEncCommonRateControlMode_PeakConstrainedVBR
AVEncCommonMultipassMode = 1
AVEncCommonMinBitRate
AVEncCommonMaxBitRate
AVEncCommonMeanBitRate

 

Note

Two-pass VBR is not supported.

 

Aspect Ratio

The display aspect ratio and pixel aspect ratio (PAR) are related by the following formula:

Display aspect ratio = PAR × (picture width / picture height)

The encoder uses this formula to calculate the value of pel_aspect_ratio for MPEG-1 bitstreams or aspect_ratio_information for MPEG-2 bitstreams. (See ISO/IEC 11172 and ISO/IEC 138181-2, respectively.)

The encoder tries the following settings, in order:

  1. If the application sets the AVEncVideoPixelAspectRatio property at any time before the filter graph runs, this property is used for the PAR.
  2. Otherwise, if the dwPictAspectRatioX and dwPictAspectRatioY members of the VIDEOINFOHEADER2 structure are non-zero, these members are used for the display aspect ratio, and the PAR is calculated from the display aspect ratio.
  3. If none of these values is present, the PAR is assumed to be 1.0, and the display aspect ratio is calculated accordingly.

In live encoding mode (AVEncCommonQualityVsSpeed equal to zero), the display aspect ratio must be either 4:3 or 16:9, with a default value of 4:3. If the computed display aspect ratio is not 4:3 or 16:9, the encoder uses the value 4:3.

GOP Structure

To specify the group of picture (GOP) structure, set the following properties in order:

  1. AVEncMPVGOPSize
  2. AVEncVideoMaxKeyframeDistance
  3. AVEncMPVDefaultBPictureCount

Based on these settings, the encoder produces one of the following GOP structures:

AVEncVideoMaxKeyframeDistance AVEncMPVDefaultBPictureCount GOP Structure
0 0 IIII...
AVEncMPVGOPSize - 1 0 IPPP...
AVEncMPVGOPSize - 1 1 IBPBP...
AVEncMPVGOPSize - 1 2 IBBPBBP...

 

The default GOP structure is IBBPBBP... with a GOP size of 15 frames.

If the application constrains the target format to DVD (through the AVEncCommonFormatConstraint property) and sets the AVEncInputVideoSystem property to NTSC or PAL, the encoder supports the following GOP sizes:

Video System Valid GOP Sizes Default GOP Size
NTSC 1-18 18 (36 fields)
PAL 1-15 15 (30 fields)

 

Codec Property Change Lists

Setting the value of one codec property can change the valid range of another property. (For example, constraining the target format restricts the average bit rate.) Whenever the application sets a property, the encoder checks if any other properties now fall outside their valid range. If so, the encoder resets that property to its new default value. To receive notifications when this occurs, do the following:

  1. Call ICodecAPI::RegisterForEvent with the value CODECAPI_CHANGELISTS.
  2. Use the IMediaEventEx interface to monitor events from the filter graph.
  3. If the range or default value of a property changes, the encoder sends an EC_CODECAPI_EVENT event with a list of changed properties.

IEncoderAPI Support

For backward compatibility, the filter supports the following properties through the IEncoderAPI interface:

Property Description
ENCAPIPARAM_BITRATE Equivalent to AVEncCommonMeanBitRate.
ENCAPIPARAM_PEAK_BITRATE Equivalent to AVEncCommonMaxBitRate.
ENCAPIPARAM_BITRATE_MODE Equivalent to AVEncCommonRateControlMode.

 

When setting the ENCAPIPARAM_BITRATE_MODE property, the values are mapped as follows:

ENCAPIPARAM_BITRATE_MODE AVEncCommonRateControlMode
ConstantBitRate eAVEncCommonRateControlMode_CBR
VariableBitRateAverage See Note.
VariableBitRatePeak eAVEncCommonRateControlMode_PeakConstrainedVBR

 

Note

Currently, the MPEG-2 video encoder does not support the VariableBitRateAverage encoding mode. If you set this value, the encoder defaults to CBR encoding (eAVEncCommonRateControlMode_CBR).

 

When getting the ENCAPIPARAM_BITRATE_MODE property, the values are mapped as follows:

AVEncCommonRateControlMode ENCAPIPARAM_BITRATE_MODE
eAVEncCommonRateControlMode_CBR ConstantBitRate
eAVEncCommonRateControlMode_Quality VariableBitRatePeak
eAVEncCommonRateControlMode_PeakConstrainedVBR VariableBitRatePeak

 

Limitations

Currently the encoder does not support any of the following features:

  • Generation of packetized elementary stream (PES) packets.
  • Frame-rate conversion. The input stream must have a frame rate that is valid for an MPEG-2 bitstream.
  • Frame-rate extensions for MPEG-2 (frame_rate_extension_n, frame_rate_extension_d).
  • Entry/exit buffer (VBV) positions for a clip.
  • Insertion of line-21 data (Closed Caption information) into the video elementary stream.
  • Setting the 25-bit time_code field in the GOP header for MPEG-2.
  • Denoise filter.
  • Digital rights management (DRM).

The encoder introduces an encoding latency of at least one GOP.

Requirements

Requirement Value
Minimum supported client
Windows Vista Home Premium, Windows Vista Ultimate, Windows 7 Home Premium, Windows 7 Professional, Windows 7 Enterprise, Windows 7 Ultimate [desktop apps only]
Minimum supported server
None supported
Header
Wmcodecdsp.h

See also

DirectShow Filters

MPEG-2 Demultiplexer Media Types