Skip to content

Commit

Permalink
Merge pull request obsproject#52 from amazon-contributing/ruwen/nvenc…
Browse files Browse the repository at this point in the history
…-scenecut

Disable NVENC scenecut
  • Loading branch information
palana committed Sep 7, 2023
2 parents b94441f + 763a017 commit c522972
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 19 additions & 3 deletions plugins/obs-ffmpeg/obs-nvenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ struct nvenc_data {
int64_t packet_pts;
bool packet_keyframe;

bool disable_scenecut;
uint32_t gop_length;
uint32_t gop_counter;

ID3D11Device *device;
ID3D11DeviceContext *context;

Expand Down Expand Up @@ -460,6 +464,8 @@ static bool init_encoder_base(struct nvenc_data *enc, obs_data_t *settings,
enc->cx = obs_encoder_get_width(enc->encoder);
enc->cy = obs_encoder_get_height(enc->encoder);

enc->disable_scenecut = obs_data_get_bool(settings, "disable_scenecut");

/* -------------------------- */
/* get preset */

Expand Down Expand Up @@ -594,6 +600,8 @@ static bool init_encoder_base(struct nvenc_data *enc, obs_data_t *settings,
initialize_params(enc, &nv_preset, nv_tuning, voi->width, voi->height,
voi->fps_num, voi->fps_den);

enc->gop_length = gop_size;
enc->gop_counter = gop_size;
config->gopLength = gop_size;
config->frameIntervalP = 1 + bf;

Expand Down Expand Up @@ -725,7 +733,8 @@ static bool init_encoder_h264(struct nvenc_data *enc, obs_data_t *settings,
uint32_t gop_size =
(keyint_sec) ? keyint_sec * voi->fps_num / voi->fps_den : 250;

h264_config->idrPeriod = gop_size;
h264_config->idrPeriod =
enc->disable_scenecut ? NVENC_INFINITE_GOPLENGTH : gop_size;

bool repeat_headers = obs_data_get_bool(settings, "repeat_headers");
if (repeat_headers) {
Expand Down Expand Up @@ -814,7 +823,8 @@ static bool init_encoder_hevc(struct nvenc_data *enc, obs_data_t *settings,
uint32_t gop_size =
(keyint_sec) ? keyint_sec * voi->fps_num / voi->fps_den : 250;

hevc_config->idrPeriod = gop_size;
hevc_config->idrPeriod =
enc->disable_scenecut ? NVENC_INFINITE_GOPLENGTH : gop_size;

bool repeat_headers = obs_data_get_bool(settings, "repeat_headers");
if (repeat_headers) {
Expand Down Expand Up @@ -917,7 +927,8 @@ static bool init_encoder_av1(struct nvenc_data *enc, obs_data_t *settings,
uint32_t gop_size =
(keyint_sec) ? keyint_sec * voi->fps_num / voi->fps_den : 250;

av1_config->idrPeriod = gop_size;
av1_config->idrPeriod = enc->disable_scenecut ? NVENC_INFINITE_GOPLENGTH
: gop_size;

av1_config->useBFramesAsRef = NV_ENC_BFRAME_REF_MODE_DISABLED;

Expand Down Expand Up @@ -1447,6 +1458,11 @@ static bool nvenc_encode_tex(void *data, uint32_t handle, int64_t pts,
params.inputPitch = enc->cx;
params.outputBitstream = bs->ptr;

if (enc->disable_scenecut && ++enc->gop_counter >= enc->gop_length) {
params.encodePicFlags = NV_ENC_PIC_FLAG_FORCEIDR;
enc->gop_counter = 0;
}

err = nv.nvEncEncodePicture(enc->session, &params);
if (err != NV_ENC_SUCCESS && err != NV_ENC_ERR_NEED_MORE_INPUT) {
nv_failed(enc->encoder, err, __FUNCTION__,
Expand Down
2 changes: 2 additions & 0 deletions plugins/simulcast/src/simulcast-output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ static OBSEncoderAutoRelease create_video_encoder(DStr &name_buffer,
"keyInt_sec"));
}

obs_data_set_bool(encoder_config, "disable_scenecut", true);

OBSEncoderAutoRelease video_encoder = obs_video_encoder_create(
encoder_type, name_buffer, encoder_config, nullptr);
if (!video_encoder) {
Expand Down

0 comments on commit c522972

Please sign in to comment.