Skip to content

Commit c0f7b29

Browse files
committed
Encoder set bitrate during encoding
1 parent 0382ef9 commit c0f7b29

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

openh264/src/encoder.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::formats::YUVSource;
55
use crate::{Error, OpenH264API, Timestamp};
66
use openh264_sys2::{
77
videoFormatI420, ELevelIdc, EProfileIdc, EUsageType, EVideoFormatType, ISVCEncoder, ISVCEncoderVtbl, SEncParamBase,
8-
SEncParamExt, SFrameBSInfo, SLayerBSInfo, SSourcePicture, API, DEBLOCKING_IDC_0, ENCODER_OPTION, ENCODER_OPTION_DATAFORMAT,
9-
ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, ENCODER_OPTION_TRACE_LEVEL, RC_MODES, SM_SINGLE_SLICE, SM_SIZELIMITED_SLICE,
10-
VIDEO_CODING_LAYER, WELS_LOG_DETAIL, WELS_LOG_QUIET,
8+
SEncParamExt, SFrameBSInfo, SLayerBSInfo, SSourcePicture, API, DEBLOCKING_IDC_0, ENCODER_OPTION, ENCODER_OPTION_BITRATE,
9+
ENCODER_OPTION_DATAFORMAT, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, ENCODER_OPTION_TRACE_LEVEL, RC_MODES, SM_SINGLE_SLICE,
10+
SM_SIZELIMITED_SLICE, VIDEO_CODING_LAYER, WELS_LOG_DETAIL, WELS_LOG_QUIET,
1111
};
1212
use std::os::raw::{c_int, c_uchar, c_void};
1313
use std::ptr::{addr_of_mut, from_mut, null, null_mut};
@@ -806,6 +806,35 @@ impl Encoder {
806806
self.previous_dimensions.is_some()
807807
}
808808

809+
/// Sets the target bit rate "runtime".
810+
///
811+
/// If the encoder is already running, this uses the openh264 API to change it without
812+
/// reinitializing the encoder.
813+
///
814+
/// # Errors
815+
///
816+
/// This might error for various reasons, many of which aren't clearly documented in OpenH264.
817+
pub fn set_target_bitrate(&mut self, bps: BitRate) -> Result<(), Error> {
818+
if self.config.target_bitrate == bps {
819+
return Ok(());
820+
}
821+
822+
self.config.target_bitrate = bps;
823+
824+
// If raw_api is already initialized, we do a runtime change, otherwise
825+
// rely on the bitrate being set when calling initialize_ext()
826+
if self.is_initialized() {
827+
unsafe {
828+
let mut bps = bps;
829+
self.raw_api
830+
.set_option(ENCODER_OPTION_BITRATE, from_mut(&mut bps.0) as *mut c_void)
831+
.ok()?;
832+
}
833+
}
834+
835+
Ok(())
836+
}
837+
809838
/// Obtain the raw API for advanced use cases.
810839
///
811840
/// When resorting to this call, please consider filing an issue / PR.

0 commit comments

Comments
 (0)