This module provides high-level API for encoding and decoding H.264 video using the OpenH264 library. It wraps the low-level C API with Nim-friendly types and error handling.
Types
DecodedFrame = object planes*: array[3, ptr uint8] ## Y, U, V planes width*: int height*: int info*: SBufferInfo
- Represents a decoded video frame, including pointers to the YUV planes and associated metadata
Decoder = ref object
- Decoder wrapper type holding the underlying ISVCDecoder handle
DecoderError = object of OpenH264Error
Encoder = ref object
- Encoder wrapper type holding the underlying ISVCEncoder handle
EncoderError = object of OpenH264Error
OpenH264Error = object of CatchableError
Procs
proc codecVersion(): OpenH264Version {....raises: [], tags: [], forbids: [].}
- Return the linked openh264 library version
proc codecVersionString(): string {....raises: [], tags: [], forbids: [].}
- Return the linked openh264 library version as a string
proc decodeFrame(dec: Decoder; src: openArray[uint8]): DecodedFrame {. ...raises: [Exception, DecoderError], tags: [RootEffect], forbids: [].}
- Decode a single frame from the input bitstream. The input should be a complete NAL unit or frame.
proc decoderCapability(): SDecoderCapability {....raises: [DecoderError], tags: [], forbids: [].}
- Query decoder hardware/software capability
proc encodeFrame(enc: Encoder; pic: var SSourcePicture): SFrameBSInfo {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Encode a single frame. The input picture must be properly initialized with the correct format and data pointers.
proc encodeParameterSets(enc: Encoder): SFrameBSInfo {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Encode the parameter sets (SPS/PPS) for the current configuration. This is typically called before encoding the first frame to ensure the decoder has the necessary information.
proc flushFrame(dec: Decoder): DecodedFrame {....raises: [Exception, DecoderError], tags: [RootEffect], forbids: [].}
- Flush the decoder to retrieve any remaining frames after the end of the stream. This should be called after all input frames have been decoded to ensure all output is retrieved.
proc forceIntraFrame(enc: Encoder; idr: bool = true) {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Force the next frame to be an intra frame (IDR). This can be used to create keyframes on demand.
proc getDefaultParams(enc: Encoder): SEncParamExt {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Retrieve the default encoder parameters for the current configuration. This can be used as a starting point before modifying specific fields and re-initializing with initializeExt.
proc getOption(dec: Decoder; option: DECODER_OPTION; value: pointer) {. ...raises: [Exception, DecoderError], tags: [RootEffect], forbids: [].}
- Get a decoder option at runtime. The specific options and expected value types are defined in the OpenH264 API.
proc getOption(enc: Encoder; option: ENCODER_OPTION; value: pointer) {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Get an encoder option at runtime. The specific options and expected value types are defined in the OpenH264 API.
proc getStatistics(enc: Encoder): SEncoderStatistics {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Retrieve encoder statistics such as actual bitrate, frame rate, etc.
proc hasOutput(frame: DecodedFrame): bool {....raises: [], tags: [], forbids: [].}
- Returns true if the decoded frame contains valid output data
proc initialize(dec: Decoder; ecMode: ERROR_CON_IDC = ERROR_CON_FRAME_COPY; bsType: VIDEO_BITSTREAM_TYPE = VIDEO_BITSTREAM_AVC) {. ...raises: [Exception, DecoderError], tags: [RootEffect], forbids: [].}
- Initialize the decoder with the specified error concealment mode and bitstream type.
proc initialize(enc: Encoder; width, height, bitrate: int; frameRate: float = 30.0; usage: EUsageType = CAMERA_VIDEO_REAL_TIME) {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Initialize the encoder with the specified parameters. This must be called before encoding frames.
proc initializeExt(enc: Encoder; param: var SEncParamExt) {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Initialize the encoder with extended parameters. This allows for more fine-grained control over encoding settings.
proc newDecoder(): Decoder {....raises: [DecoderError], tags: [], forbids: [].}
- Create and return a new SVC decoder instance
proc newEncoder(): Encoder {....raises: [EncoderError], tags: [], forbids: [].}
- Create and return a new SVC encoder instance
proc setBitrate(enc: Encoder; bitrate: int) {....raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Convenience: change target bitrate at runtime
proc setFrameRate(enc: Encoder; fps: float32) {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Convenience: change frame rate at runtime
proc setOption(dec: Decoder; option: DECODER_OPTION; value: pointer) {. ...raises: [Exception, DecoderError], tags: [RootEffect], forbids: [].}
- Set a decoder option at runtime. The specific options and expected value types are defined in the OpenH264 API.
proc setOption(enc: Encoder; option: ENCODER_OPTION; value: pointer) {. ...raises: [Exception, EncoderError], tags: [RootEffect], forbids: [].}
- Set an encoder option at runtime. The specific options and expected value types are defined in the OpenH264 API.
Templates
template checkDecoder(code: clong; msg: string)
template checkEncoder(code: cint; msg: string)
Exports
-
LAYER_TYPE, PPISVCDecoder, SEncParamExt, ERR_TOOL, ISVCEncoder, AVERAGE_RTP_PAYLOAD_LEN, WelsCreateDecoder, SDeliveryStatus, SDecoderCapability, FRAME_NUM_PARAM_SET, EProfileIdc, WELS_LOG_DETAIL, WELS_LOG_WARNING, DECODER_OPTION, PRateThresholds, MAX_NAL_UNITS_IN_LAYER, SLevelInfo, EVideoFormatSPS, EParameterSetStrategy, DEBLOCKING_OFFSET, ISVCEncoderVtbl, SSpatialLayerConfig, VIDEO_BITSTREAM_TYPE, ET_RFS, WELS_LOG_RESV, ENCODER_OPTION, PPISVCEncoder, EVideoFormatType, SSysMEMBuffer, WelsCreateSVCEncoder, SLTRMarkingFeedback, LAYER_NUM, FRAME_NUM_IDR, SDumpLayer, ISVCEncoderObj, SEncoderStatistics, DECODING_STATE, ERROR_CON_IDC, ET_FEC_FULL, WELS_LOG_DEBUG, MAX_TEMPORAL_LAYER_NUM, SBitrateInfo, SProfileInfo, SFrameBSInfo, OpenH264Version, ET_FEC_HALF, WELS_LOG_QUIET, ET_IR_R1, SLTRConfig, MAX_LAYER_NUM_OF_FRAME, ISVCDecoderObj, SVideoProperty, IS_IDR_NAL, SDecodingParam, AUTO_REF_PIC_COUNT, IS_PARAMETER_SET_NAL, ESampleAspectRatio, SLTRRecoverRequest, ENalUnitType, EVideoFrameType, SDecoderStatistics, MAX_QUALITY_LAYER_NUM, ET_NONE, RC_MODES, ET_IR_R3, EColorMatrix, KEY_FRAME_REQUEST_TYPE, PParserBsInfo, ISVCDecoderVtbl, SBufferInfoUsrData, WelsGetDecoderCapability, MAX_RTP_PAYLOAD_LEN, WelsGetCodecVersion, WelsGetCodecVersionEx, ET_IP_SCALE, CM_RETURN, ET_FMO, SVuiSarInfo, SRateThresholds, SSliceArgument, WelsTraceCallback, PSliceInfo, ECOMPLEXITY_MODE, ET_IR_R2, UNSPECIFIED_BIT_RATE, TSliceInfo, kiKeyNumMultiple, ETransferCharacteristics, DEBLOCKING_OFFSET_MINUS, SSourcePicture, FEEDBACK_VCL_NAL_IN_AU, WelsDestroyDecoder, WELS_LOG_INFO, WELS_LOG_ERROR, ISVCDecoder, SParserBsInfo, EColorPrimaries, PVuiSarInfo, WELS_LOG_DEFAULT, MAX_SLICES_NUM_TMP, SAVED_NALUNIT_NUM_TMP, ELevelIdc, WelsDestroySVCEncoder, SLayerBSInfo, SBufferInfo, ENalPriority, MAX_SPATIAL_LAYER_NUM, SEncParamBase, SliceModeEnum, EDeblockingIdc, WELS_LOG_LEVEL_COUNT, EUsageType