H.264 Video Encoder

The Microsoft Media Foundation H.264 video encoder is a Media Foundation transform that supports the following H.264 profiles:

  • Baseline Profile
  • Main Profile
  • High profile (requires Windows 8)

The H.264 video encoder exposes the following interfaces:

Input Types

The input media type must have one of the following subtypes:

  • MFVideoFormat_I420
  • MFVideoFormat_IYUV
  • MFVideoFormat_NV12
  • MFVideoFormat_YUY2
  • MFVideoFormat_YV12

For more information about these subtypes, see Video Subtype GUIDs.

The output type must be set before the input type. Until the output type is set, the encoder's IMFTransform::SetInputType method returns MF_E_TRANSFORM_TYPE_NOT_SET.

Output Types

The encoder supports a single output subtype:

  • MFVideoFormat_H264

Set the following attributes on the output media type.

Attribute Description
MF_MT_MAJOR_TYPE Major type. Must be MFMediaType_Video.
MF_MT_SUBTYPE Video subtype. Must be MFVideoFormat_H264.
MF_MT_AVG_BITRATE Average encoded bit rate, in bits per second. Must be greater than zero.
MF_MT_FRAME_RATE Frame rate.
MF_MT_FRAME_SIZE Frame size.
MF_MT_INTERLACE_MODE Interlace mode.
MF_MT_MPEG2_PROFILE H.264 encoding profile.
The supported values are:
  • eAVEncH264VProfile_Base (default)
  • eAVEncH264VProfile_Main
  • eAVEncH264VProfile_High (requires Windows 8)
MF_MT_MPEG2_LEVEL Optional. Specifies the H.264 encoding level.
The default value is –1, indicating that the encoder will select the encoding level.
It is recommended not to set the level in the media type, and allow the encoder to select the level. The encoder can derive the proper level for a given video stream, taking into account the format constraints and the characteristics of the video. For more information about profile and level constraints, refer to Annex A of ITU-T H.264.
MF_MT_PIXEL_ASPECT_RATIO Optional. Specifies the pixel aspect ratio. The default value is 1:1.

 

After the output type is set, the video encoder updates the type by adding the MF_MT_MPEG_SEQUENCE_HEADER attribute. This attribute contains the sequence header.

Codec Properties

The H.264 encoder implements the ICodecAPI interface for setting encoding parameters. It supports the following properties.

For the codec requirements for HCK encoder certification, see the Certified Hardware Encoder section below.

The following properties are supported in Windows 7.

Property Description
CODECAPI_AVEncCommonRateControlMode Sets the rate control mode. See Remarks. The default mode is unconstrained variable bit rate (VBR).
CODECAPI_AVEncCommonQuality Sets the quality level. This property applies when the rate control mode is quality-based VBR (eAVEncCommonRateControlMode_Quality). The valid range is 1–100. The default value is 70.
To set this parameter, set the property before calling IMFTransform::SetOutputType.
To set this parameter in Windows 7, set the property before calling IMFTransform::SetOutputType. The encoder ignores changes after the output type is set.
In Windows 8, this property can be set at any time during encoding. Changes are applied starting at the next input frame.
Internally, the encoder converts this property to an AVEncVideoEncodeQP value.

 

The following properties require Windows 8.

Property Description
CODECAPI_AVEncAdaptiveMode Sets the adaptive encoding mode. The H.264 encoder supports the following modes in Windows 8:
  • eAVEncAdaptiveMode_None. No adaptive encoding. (Default.)
  • eAVEncAdaptiveMode_FrameRate. Adaptively change the frame rate.

CODECAPI_AVEncCommonBufferSize Sets the buffer size, in bytes, for constant bit rate (CBR) encoding.
The valid range is [1 ... 2³²–1].
Requires Windows 8.
CODECAPI_AVEncCommonMaxBitRate For constrained VBR encoding, specifies the rate at which the "leaky bucket" is drained, in bits per second. This property applies when the rate control mode is eAVEncCommonRateControlMode_PeakConstrainedVBR.
The valid range is [1 ... 2³²–1].
CODECAPI_AVEncCommonMeanBitRate Sets the average bit rate for the encoded bit stream, in bits per second. This property is ignored if the rate control mode is eAVEncCommonRateControlMode_Quality.
The valid range is [1 ... 2³²–1].
In CBR and unconstrained VBR modes, the average bit rate determines the final size of the file. In CBR mode, the average bit rate is also the rate at which compressed bits are drained from the "leaky bucket." (For more information, see The Leaky Bucket Buffer Model.)
In Windows 7, the average bit rate is specified by the MF_MT_AVG_BITRATE attribute on the media type.
In Windows 8, you can set the average bit rate using either the MF_MT_AVG_BITRATE attribute or the CODECAPI_AVEncCommonMeanBitRate property. If both are set, CODECAPI_AVEncCommonMeanBitRate overrides. In Windows 8, you can set the average bit rate during encoding. If the bit rate changes, the encoder uses adaptive encoding.
CODECAPI_AVEncCommonQualityVsSpeed Sets the quality/speed tradeoff. Valid range:
  • 0–33: Low complexity
  • 34–66: Medium complexity (default)
  • 67–100: High complexity

This value affects how the encoder performs various encoding operations, such as motion compensation. At higher complexity levels, the encoder runs more slowly but produces better quality at the same bit rate.
CODECAPI_AVEncH264CABACEnable Enables or disables CABAC (context-adaptive binary arithmetic coding) for H.264 entropy coding. The default value is VARIANT_FALSE.
CABAC is not used for Baseline profile.
CODECAPI_AVEncH264SPSID Sets the value of seq_parameter_set_id in the SPS NAL unit of the H.264 bitstream.
CODECAPI_AVEncMPVDefaultBPictureCount Sets the maximum number of consecutive B frames in the output bitstream. Valid values are:
  • 0: Do not use B frames (default).
  • 1: Use one B frame.
  • 2: Use two B frames.
To set this parameter, set the property before calling IMFTransform::SetOutputType.
For Baseline profile, the number of B frames is always zero. The encoder will override nonzero values.
For other H.264 profiles, if this property is nonzero, the encoding pattern is IBBPBBP, where the maximum number of consecutive B frames is equal to CODECAPI_AVEncMPVDefaultBPictureCount.
CODECAPI_AVEncMPVGOPSize Sets the number of pictures from one GOP header to the next, including the leading anchor but not the following one.
The valid range is [0 ... 2³²–1]. If zero, the encoder selects the GOP size. The default value is zero.
CODECAPI_AVEncNumWorkerThreads Sets the number of worker threads used by a encoder.
The valid range is 0–16. If zero, the encoder selects the number of threads.
CODECAPI_AVEncVideoContentType Indicates the type of video content.
CODECAPI_AVEncVideoEncodeQP Valid range: 16–51. The default value is 24.
This property applies when the rate control mode is eAVEncCommonRateControlMode_Quality.
This property configures the same encoding setting as AVEncCommonQuality. However, AVEncVideoEncodeQP enables the application to specify the value of QP directly. If both properties are set, AVEncVideoEncodeQP overrides.
The default value of 24 corresponds to the default value of 70 for the AVEncCommonQuality setting.
CODECAPI_AVEncVideoForceKeyFrame Forces the encoder to code the next frame as a key frame.
CODECAPI_AVEncVideoMinQP Valid range: 0–51. The default value is 0.
This property applies to all rate control modes. The encoder should not produce a QP value lower than what is specified by the CODECAPI_AVEncVideoMinQP property.
CODECAPI_AVLowLatencyMode Enables or disables low-latency mode. See "Multithreading" in the Remarks section.

 

Remarks

The encoder supports the following rate control modes.

Mode Constant Description
Constant bit rate (CBR) eAVEncCommonRateControlMode_CBR The encoder tries to achieve a constant bit rate, using a "leaky bucket" model. The target bit rate is given by the CODECAPI_AVEncCommonMeanBitRate property.
Requires Windows 8.
Constrained variable bit rate (VBR) eAVEncCommonRateControlMode_PeakConstrainedVBR The encoder uses a "leaky bucket" model with a peak bit rate. The drain rate for the leaky bucket is given by the CODECAPI_AVEncCommonMaxBitRate property.
Requires Windows 8.
Quality-based variable bit rate (VBR) eAVEncCommonRateControlMode_Quality The encoder tries to achieve a constant quality level, given by the AVEncCommonQuality property.
Unconstrained VBR eAVEncCommonRateControlMode_UnconstrainedVBR The encoder tries to achieve the target bitrate given by the MF_MT_AVG_BITRATE attribute in the output media type. This is the default mode.

 

CBR and constrained VBR modes require Windows 8.

In Windows 8, the encoder sets the following attributes on the output samples:

Note

A previous version of the documentation incorrectly stated that the encoder is supported on Windows Server 2008 R2.

 

Multithreading

In Windows 8, the encoder supports two encoding modes:

  • Slice encoding. In this mode, slices are encoded in parallel. Each slice is encoded on a different thread. This mode has low latency, because a single picture is encoded in parallel. However, this approach does not scale as the number of cores increases, because the number of slices is bounded by the number of macroblock rows in the input picture.
  • Multi-frame encoding. In this mode, the encoder accepts multiple frames of input and encodes them in parallel. This mode scales better in a multicore environment, but introduces more latency.

The encoder defaults to slice encoding, to minimize latency. To enable multi-frame encoding, set the CODECAPI_AVLowLatencyMode property to VARIANT_FALSE.

To set the number of worker threads used by the encoder, set the CODECAPI_AVEncNumWorkerThreads property.

In Windows 7, the encoder always uses slice encoding.

Certified Hardware Encoder

If a certified hardware encoder is present, it will generally be used instead of the inbox system encoder for Media Foundation related scenarios. Certified encoders are required to support a certain set of ICodecAPI properties and can optionally support another set of properties. The certification process should guarantee that the required properties are properly supported and, if an optional property is supported, that it is also properly supported.

The following is the set of required and optional ICodecAPI properties for encoders to pass the HCK encoder certification.

The following Windows 8 and Windows 8.1 ICodecAPI properties are required:

The following Windows 8.1 ICodecAPI properties are optional, but are tested in HCK if supported.

The following Windows 8 and Windows 8.1 ICodecAPI properties are optional, but are tested in HCK if supported.

The following ICodecAPI properties are optional. They are not tested in HCK.

Requirements

Requirement Value
Minimum supported client
Windows 7 [desktop apps only]
Minimum supported server
None supported
DLL
Mfh264enc.dll

See also

Codec Objects

MPEG-4 Support in Media Foundation

Supported Media Formats in Media Foundation

Video Media Types