From aaae5b0f4926065136f287876c6bc41631bae692 Mon Sep 17 00:00:00 2001 From: Marko Viitanen Date: Mon, 3 Jul 2023 16:02:31 +0300 Subject: [PATCH] Fix a bug when requesting encoder_headers before any frame has been pushed in, Fixes #360 - state->frame->max_qp_delta_depth was uninitialized and caused different PPS to be outputted than after pushing frames in - Caused problems e.g. in FFmpeg integration where the requested PPS was pushed to MP4 headers and causing decoding errors in some decoders --- src/encoder_state-bitstream.c | 4 +++- src/encoder_state-ctors_dtors.c | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/encoder_state-bitstream.c b/src/encoder_state-bitstream.c index 9fe092ef..e4b41fe1 100644 --- a/src/encoder_state-bitstream.c +++ b/src/encoder_state-bitstream.c @@ -504,7 +504,9 @@ static void encoder_state_write_bitstream_pic_parameter_set(bitstream_t* stream, WRITE_U(stream, 0, 1, "constrained_intra_pred_flag"); WRITE_U(stream, encoder->cfg.trskip_enable, 1, "transform_skip_enabled_flag"); - if (state->frame->max_qp_delta_depth >= 0) { + // Check all the conditions for setting cu_qp_delta_enabled_flag here, since state->frame->max_qp_delta_depth might not be set yet. + if (encoder->cfg.target_bitrate > 0 || encoder->cfg.erp_aqp || encoder->cfg.roi.file_path || + encoder->cfg.set_qp_in_cu || encoder->cfg.vaq || (state->tile->frame->source && state->tile->frame->source->roi.roi_array) ) { // Use separate QP for each LCU when rate control is enabled. WRITE_U(stream, 1, 1, "cu_qp_delta_enabled_flag"); WRITE_UE(stream, state->frame->max_qp_delta_depth, "diff_cu_qp_delta_depth"); diff --git a/src/encoder_state-ctors_dtors.c b/src/encoder_state-ctors_dtors.c index a6e92d99..c367b74d 100644 --- a/src/encoder_state-ctors_dtors.c +++ b/src/encoder_state-ctors_dtors.c @@ -68,6 +68,9 @@ static int encoder_state_config_frame_init(encoder_state_t * const state) { state->frame->rc_beta = -1.367; state->frame->icost = 0; + // Reset max_qp_delta_depth here, was causing problems when headers are requested before input is fed in + state->frame->max_qp_delta_depth = 0; + const encoder_control_t * const encoder = state->encoder_control; const int num_lcus = encoder->in.width_in_lcu * encoder->in.height_in_lcu; state->frame->lcu_stats = calloc(num_lcus, sizeof(lcu_stats_t));