Skip to content
ReferenceType edited this page Mar 13, 2025 · 8 revisions

H264Encoder Class

The H264Encoder class provides an H.264 encoder based on Cisco's OpenH264 library. It allows you to encode RGB and YUV images into H.264 video streams.

Table of Contents


Overview

The H264Encoder class wraps the Cisco OpenH264 library and acts as a facade for ease of use. It supports various initialization modes, runtime configuration adjustments, and different input image formats.

Features:

  • Encodes RGB,BGR,RGBA,BGRA and YUV(I420Planar or NV12 interleaved) images to H.264.
  • Supports runtime configuration adjustments (bitrate, FPS, options).
  • Provides multiple initialization methods for different scenarios.
  • Implements IDisposable for proper resource management.

Constructors

ConstructorDescription
H264Encoder()Creates a new instance using the default Cisco DLL name which is defined in. Defines.CiscoDllName Will throw exception if library cant be found.
H264Encoder(string ciscoDllPath)Creates a new instance using the specified Cisco DLL path.(LoadLibraryW on windows,dlopen with RTLD_LAZY on linux)Will throw exception if library cant be found.

Properties

PropertyTypeDescription
EnableDebugPrintsboolEnables or disables debug prints during initialization.

Methods

MethodReturn TypeDescription
GetDefaultParameters()TagEncParamExtRetrieves the default advanced configuration parameters.
Initialize(int width, int height, int bitrate, int fps, ConfigType configType)intInitializes the encoder with basic parameters and a pre-configured configuration type.
Initialize(TagEncParamBase param)intInitializes the encoder with base encoding parameters.
Initialize(TagEncParamExt param)intInitializes the encoder with advanced encoding parameters.
ForceIntraFrame()boolForces an intra frame(IDR) on the next encode.
SetMaxBitrate(int target)voidSets the maximum bitrate.
SetTargetFps(float target)voidSets the target frames per second.
GetOption<T>(ENCODER_OPTION option, out T value)boolGets an encoder option.
GetOptionRef<T>(ENCODER_OPTION option, ref T value)boolGets an encoder option, allowing reuse of the value.
SetOption<T>(ENCODER_OPTION option, T value)boolSets an encoder option.
Encode(RgbImage im, out EncodedData[] ed)boolEncodes an RGB image.
Encode(YUVNV12ImagePointer yuv, out EncodedData[] ed)boolEncodes a YUV NV12 image.
Encode(YUVImagePointer yuv, out EncodedData[] ed)boolEncodes a YUV I420P image.
Encode(YuvImage yuv, out EncodedData[] ed)boolEncodes a YUV I420P image from reference.
Dispose()voidDisposes of the encoder and releases native resources.

Premade Configurations

Enum MemberDescription
CameraBasicStandard setting for camera capture.
ScreenCaptureBasicStandard setting for screen capture.
CameraCaptureAdvancedAdvanced configuration aiming for more quality and complexity for camera capture.
ScreenCaptureAdvancedAdvanced configuration aiming for more quality and complexity for screen capture.
CameraCaptureAdvancedHPAdvanced camera capture with parallel encoder(might sacrifice quality).
ScreenCaptureAdvancedHpAdvanced screen capture with parallel encoder(might sacrifice quality).

Usage Examples

Encoder(also Decoder) is a stateful object and you should manage the lifetimes on your program.

Creating and Initializing an Encoder with Basic Parameters

using(varencoder=newH264Encoder()){intwidth=1280;intheight=720;intbitrate=2000000;// 2 Mbpsintfps=30;ConfigTypeconfigType=ConfigType.CameraCaptureAdvanced;encoder.Initialize(width,height,bitrate,fps,configType);// Encode an RgbImageRgbImageimage=newRgbImage(ImageFormat.Rgb,width,height);if(encoder.Encode(image,outEncodedData[]encodedData)){// Process encoded data}}

Creating and Initializing an Encoder with Advanced Parameters

using(varencoder=newH264Encoder()){varparam=encoder.GetDefaultParameters();// modify the parameters..encoder.Initialize(param);// Encode an YUV NV12(emulate some source)varnv12=newYUVNV12ImagePointer(...);if(encoder.Encode(nv12,outEncodedData[]encodedData)){// Process encoded data}}

Setting Runtime Configuration

encoder.SetMaxBitrate(2000000);// 2 Mbpsencoder.SetTargetFps(30.0f);encoder.ForceIntraFrame();

Getting and Setting Options

intidrInterval;if(encoder.GetOption(ENCODER_OPTION.ENCODER_OPTION_IDR_INTERVAL,outidrInterval)){if(idrInterval<120){encoder.SetOption(ENCODER_OPTION.ENCODER_OPTION_IDR_INTERVAL,120);}}

Remarks

  • encoder.Encode(...) Return Value:

    • The bool value = encoder.Encode(..., out EncodedData[] encodedData) method returns a boolean value indicating whether encoding was performed.
    • A return value of false signifies that the frame was skipped and encodedData will be empty, typically to maintain the target bitrate.
  • EncodedData for IDR Frames (Single Layer):

    • For IDR frames in a single-layer encoding scenario (standard usage), the Encode method produces multiple EncodedData entries.
    • The first EncodedData entry contains metadata and will not produce an image or an error when decoded.
  • Decoder Input:

    • The decoder supports both frame-by-frame and merged input(EncodedData elements from single encode operation).
    • You can provide encoded frames individually or concatenate them into a single contiguous byte array for one-shot decoding.
  • Encoder Input Format:

    • The Cisco encoder exclusively supports YUV I420 planar format.
    • RGB and YUV NV12 (interleaved UV) input formats are automatically converted to YUV I420 internally.
    • Encoding is more efficient when the source is already in YUV I420 or NV12 format, as it minimizes conversion overhead.

Clone this wiki locally