Skip to content

Commit

Permalink
[Encode] Adaptive TU Enable
Browse files Browse the repository at this point in the history
* [Encode] adaptive TU for AVC/HEVC/AV1

adaptive tu
  • Loading branch information
Bossonor authored and intel-mediadev committed Oct 14, 2024
1 parent 4fe64bd commit 1290bba
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ typedef struct _CODEC_AV1_ENCODE_PICTURE_PARAMS
uint8_t ref_frame_idx[7]; // [0..6]
uint8_t HierarchLevelPlus1;
uint8_t primary_ref_frame; // [0..7]
uint8_t Reserved8b3;
uint8_t AdaptiveTUEnabled;
uint8_t Reserved8b4;
uint8_t order_hint;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,8 @@ typedef struct _CODEC_AVC_ENCODE_PIC_PARAMS
*/
uint8_t QpModulationStrength;

uint8_t AdaptiveTUEnabled;

/*! \brief StatusReportEnable
*
* Request features to be enabled at status report.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,8 @@ typedef struct _CODEC_HEVC_ENCODE_PICTURE_PARAMS
} fields;
uint8_t value;
} QualityInfoSupportFlags;

uint8_t AdaptiveTUEnabled;
} CODEC_HEVC_ENCODE_PICTURE_PARAMS, *PCODEC_HEVC_ENCODE_PICTURE_PARAMS;

/*! \brief Slice-level parameters of a compressed picture for HEVC encoding.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ namespace encode
"Encode RateControl Method",
m_rcMode,
MediaUserSetting::Group::Sequence);

ENCODE_CHK_NULL_RETURN(m_basicFeature->m_av1PicParams);
MediaUserSetting::Value outValue;
ReadUserSetting(
m_userSettingPtr,
outValue,
"Adaptive TU Enable",
MediaUserSetting::Group::Sequence);
m_basicFeature->m_av1PicParams->AdaptiveTUEnabled |= outValue.Get<uint8_t>();
#endif
return MOS_STATUS_SUCCESS;
}
Expand Down Expand Up @@ -211,6 +220,7 @@ namespace encode
dmem->UPD_CurHeight = (uint16_t)m_basicFeature->m_oriFrameHeight;
dmem->UPD_Asyn = 0;
dmem->UPD_EnableAdaptiveRounding = (m_basicFeature->m_roundingMethod == RoundingMethod::adaptiveRounding);
dmem->UPD_AdaptiveTUEnabled = picParams->AdaptiveTUEnabled;

if (seqParams->GopRefDist == 16 && m_rcMode == RATECONTROL_CQL)
dmem->UPD_MaxBRCLevel = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,19 @@ namespace encode
{
// VDEnc read batch buffer (input for HuC FW)
allocParamsForBufferLinear.dwBytes = MOS_ALIGN_CEIL(m_hwInterface->m_vdencReadBatchBufferSize, CODECHAL_PAGE_SIZE);
allocParamsForBufferLinear.pBufName = "VDENC Read Batch Buffer";
allocParamsForBufferLinear.pBufName = "VDENC Read Origin Batch Buffer";
allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_WRITE;
allocatedbuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true);
ENCODE_CHK_NULL_RETURN(allocatedbuffer);
m_vdencReadBatchBuffer[k][i] = *allocatedbuffer;
m_vdencReadBatchBufferOrigin[k][i] = *allocatedbuffer;

// VDEnc read batch buffer (input for HuC FW)
allocParamsForBufferLinear.dwBytes = MOS_ALIGN_CEIL(m_hwInterface->m_vdencReadBatchBufferSize, CODECHAL_PAGE_SIZE);
allocParamsForBufferLinear.pBufName = "VDENC Read TU7 Batch Buffer";
allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_WRITE;
allocatedbuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true);
ENCODE_CHK_NULL_RETURN(allocatedbuffer);
m_vdencReadBatchBufferTU7[k][i] = *allocatedbuffer;

// BRC update DMEM
allocParamsForBufferLinear.dwBytes = MOS_ALIGN_CEIL(m_vdencBrcUpdateDmemBufferSize, CODECHAL_CACHELINE_SIZE);
Expand Down Expand Up @@ -156,8 +164,19 @@ namespace encode

ENCODE_CHK_NULL_RETURN(m_basicFeature);
ENCODE_CHK_NULL_RETURN(m_basicFeature->m_recycleBuf);
ENCODE_CHK_NULL_RETURN(m_basicFeature->m_av1PicParams);
ENCODE_CHK_NULL_RETURN(m_basicFeature->m_av1SeqParams);

ENCODE_CHK_STATUS_RETURN(ConstructBatchBufferHuCBRC(&m_vdencReadBatchBufferOrigin[m_pipeline->m_currRecycledBufIdx][m_pipeline->GetCurrentPass()]));

if (m_basicFeature->m_av1PicParams->AdaptiveTUEnabled != 0)
{
auto original_TU = m_basicFeature->m_targetUsage;
m_basicFeature->m_targetUsage = m_basicFeature->m_av1SeqParams->TargetUsage = 7;
ENCODE_CHK_STATUS_RETURN(ConstructBatchBufferHuCBRC(&m_vdencReadBatchBufferTU7[m_pipeline->m_currRecycledBufIdx][m_pipeline->GetCurrentPass()]));
m_basicFeature->m_targetUsage = m_basicFeature->m_av1SeqParams->TargetUsage = original_TU;
}

ENCODE_CHK_STATUS_RETURN(ConstructBatchBufferHuCBRC(&m_vdencReadBatchBuffer[m_pipeline->m_currRecycledBufIdx][m_pipeline->GetCurrentPass()]));
ENCODE_CHK_STATUS_RETURN(ConstructPakInsertHucBRC(&m_vdencPakInsertBatchBuffer[m_pipeline->m_currRecycledBufIdx]));

bool firstTaskInPhase = packetPhase & firstPacket;
Expand Down Expand Up @@ -502,11 +521,15 @@ namespace encode

ENCODE_CHK_STATUS_RETURN(DumpRegion(0, "_BrcHistory", true, hucRegionDumpUpdate, 6080));
ENCODE_CHK_STATUS_RETURN(DumpRegion(1, "_VdencStats", true, hucRegionDumpUpdate, 48*4));
ENCODE_CHK_STATUS_RETURN(DumpRegion(3, "_InputSLBB", true, hucRegionDumpUpdate, 600*4));
ENCODE_CHK_STATUS_RETURN(DumpRegion(3, "_InputSLBB_Origin", true, hucRegionDumpUpdate, 600*4));
ENCODE_CHK_STATUS_RETURN(DumpRegion(5, "_ConstData", true, hucRegionDumpUpdate, MOS_ALIGN_CEIL(m_vdencBrcConstDataBufferSize, CODECHAL_PAGE_SIZE)));
ENCODE_CHK_STATUS_RETURN(DumpRegion(7, "_PakMmio", true, hucRegionDumpUpdate, 16*4));
ENCODE_CHK_STATUS_RETURN(DumpRegion(8, "_InputPakInsert", true, hucRegionDumpUpdate, 100));
ENCODE_CHK_STATUS_RETURN(DumpRegion(10, "_InputCdfTable", true, hucRegionDumpUpdate, 4 * MOS_ALIGN_CEIL(m_basicFeature->m_cdfMaxNumBytes, CODECHAL_CACHELINE_SIZE)));
if (m_basicFeature->m_av1PicParams->AdaptiveTUEnabled != 0)
{
ENCODE_CHK_STATUS_RETURN(DumpRegion(12, "_InputSLBB_TU7", true, hucRegionDumpUpdate, 600 * 4));
}

return MOS_STATUS_SUCCESS;
}
Expand Down Expand Up @@ -598,8 +621,8 @@ namespace encode
// Region 1 - VDenc Stats Buffer (Input)
params.regionParams[1].presRegion = resTileBasedStatisticsBuffer;
params.regionParams[1].dwOffset = offset;
// Region 3 - Input SLB Buffer (Input)
params.regionParams[3].presRegion = const_cast<PMOS_RESOURCE>(&m_vdencReadBatchBuffer[bufIdx][currentPass]);
// Region 3 - Input SLB Buffer (Input Origin)
params.regionParams[3].presRegion = const_cast<PMOS_RESOURCE>(&m_vdencReadBatchBufferOrigin[bufIdx][currentPass]);
// Region 4 - BRC Data for next frame's width/height - (Output)
params.regionParams[4].presRegion = resBrcDataBuffer;
params.regionParams[4].isWritable = true;
Expand All @@ -620,6 +643,11 @@ namespace encode
// Region 11 - CDF (output)
params.regionParams[11].presRegion = m_basicFeature->m_defaultCdfBufferInUse;
params.regionParams[11].isWritable = true;
// Region 12 - Input SLB Buffer (Input TU7)
if (m_basicFeature->m_av1PicParams->AdaptiveTUEnabled != 0)
{
params.regionParams[12].presRegion = const_cast<PMOS_RESOURCE>(&m_vdencReadBatchBufferTU7[bufIdx][currentPass]);
}

return MOS_STATUS_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ namespace encode
uint8_t UPD_CQMEnabled;
uint8_t UPD_TempCurrentlayer;
uint8_t UPD_TempScalable;
uint8_t UPD_AdaptiveTUEnabled;

uint8_t RSVD8[62];
uint8_t RSVD8[61];
};

typedef struct _HUC_MODE_COST
Expand Down Expand Up @@ -235,7 +236,8 @@ namespace encode
static constexpr uint32_t m_vdboxHucAv1BrcUpdateKernelDescriptor = 19;//!< Huc AV1 Brc init kernel descriptor

// Batch Buffer for VDEnc
MOS_RESOURCE m_vdencReadBatchBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][VDENC_BRC_NUM_OF_PASSES] = {}; //!< VDEnc read batch buffer
MOS_RESOURCE m_vdencReadBatchBufferOrigin[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][VDENC_BRC_NUM_OF_PASSES] = {}; //!< VDEnc read batch buffer
MOS_RESOURCE m_vdencReadBatchBufferTU7[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][VDENC_BRC_NUM_OF_PASSES] = {}; //!< VDEnc read batch buffer
MOS_RESOURCE m_vdencPakInsertBatchBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM] = {}; //!< VDEnc read batch buffer
MOS_RESOURCE m_vdencBrcConstDataBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM] = {}; //!< VDEnc brc constant data buffer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ MOS_STATUS AvcEncodeBRC::Update(void *params)
"Encode RateControl Method",
m_rcMode,
MediaUserSetting::Group::Sequence);

MediaUserSetting::Value outValue;
ReadUserSetting(
m_userSettingPtr,
outValue,
"Adaptive TU Enable",
MediaUserSetting::Group::Sequence);
m_basicFeature->m_picParam->AdaptiveTUEnabled |= outValue.Get<uint8_t>();
#endif
return MOS_STATUS_SUCCESS;
}
Expand Down Expand Up @@ -640,6 +648,8 @@ MOS_STATUS AvcEncodeBRC::SetDmemForUpdate(void *params, uint16_t currPass, bool
hucVdencBrcUpdateDmem->UPD_PAKPassNum_U8 = (uint8_t)currPass;
hucVdencBrcUpdateDmem->UPD_MaxNumPass_U8 = m_featureManager->GetNumPass();

hucVdencBrcUpdateDmem->UPD_AdaptiveTUEnabled = avcPicParams->AdaptiveTUEnabled;

uint32_t numP = 0;
if (avcSeqParams->GopRefDist && (avcSeqParams->GopPicSize > 0))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,15 @@ MOS_STATUS AvcHucBrcUpdatePkt::AllocateResources()
allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE;
allocatedbuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true);
ENCODE_CHK_NULL_RETURN(allocatedbuffer);
m_vdencBrcImageStatesReadBuffer[k] = allocatedbuffer;
m_vdencBrcImageStatesReadBufferOrigin[k] = allocatedbuffer;

// VDENC IMG STATE read buffer
allocParamsForBufferLinear.dwBytes = m_brcFeature->GetVdencBRCImgStateBufferSize();
allocParamsForBufferLinear.pBufName = "VDENC BRC IMG State Read Buffer";
allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE;
allocatedbuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true);
ENCODE_CHK_NULL_RETURN(allocatedbuffer);
m_vdencBrcImageStatesReadBufferTU7[k] = allocatedbuffer;

for (auto i = 0; i < VDENC_BRC_NUM_OF_PASSES; i++)
{
Expand Down Expand Up @@ -334,8 +342,18 @@ MOS_STATUS AvcHucBrcUpdatePkt::Execute(PMOS_COMMAND_BUFFER cmdBuffer, bool store
MOS_STATUS AvcHucBrcUpdatePkt::Submit(MOS_COMMAND_BUFFER *commandBuffer, uint8_t packetPhase)
{
ENCODE_FUNC_CALL();
ENCODE_CHK_NULL_RETURN(m_basicFeature->m_picParam);
ENCODE_CHK_NULL_RETURN(m_basicFeature->m_seqParam);

ENCODE_CHK_STATUS_RETURN(ConstructImageStateReadBuffer(m_vdencBrcImageStatesReadBufferOrigin[m_pipeline->m_currRecycledBufIdx]));

ENCODE_CHK_STATUS_RETURN(ConstructImageStateReadBuffer(m_vdencBrcImageStatesReadBuffer[m_pipeline->m_currRecycledBufIdx]));
if (m_basicFeature->m_picParam->AdaptiveTUEnabled != 0)
{
auto original_TU = m_basicFeature->m_targetUsage;
m_basicFeature->m_targetUsage = m_basicFeature->m_seqParam->TargetUsage = 7;
ENCODE_CHK_STATUS_RETURN(ConstructImageStateReadBuffer(m_vdencBrcImageStatesReadBufferTU7[m_pipeline->m_currRecycledBufIdx]));
m_basicFeature->m_targetUsage = m_basicFeature->m_seqParam->TargetUsage = original_TU;
}

bool firstTaskInPhase = packetPhase & firstPacket;
bool requestProlog = false;
Expand Down Expand Up @@ -525,7 +543,7 @@ MHW_SETPAR_DECL_SRC(HUC_VIRTUAL_ADDR_STATE, AvcHucBrcUpdatePkt)
// Input regions
params.regionParams[1].presRegion = m_basicFeature->m_recycleBuf->GetBuffer(VdencStatsBuffer, 0);
params.regionParams[2].presRegion = m_basicFeature->m_recycleBuf->GetBuffer(BrcPakStatisticBuffer, 0);
params.regionParams[3].presRegion = m_vdencBrcImageStatesReadBuffer[m_pipeline->m_currRecycledBufIdx];
params.regionParams[3].presRegion = m_vdencBrcImageStatesReadBufferOrigin[m_pipeline->m_currRecycledBufIdx];
params.regionParams[5].presRegion = m_vdencBrcConstDataBuffer[GetCurrConstDataBufIdx()];
params.regionParams[7].presRegion = m_basicFeature->m_recycleBuf->GetBuffer(PakSliceSizeStreamOutBuffer, m_pipeline->GetCurrentPass() ?
m_basicFeature->m_frameNum : m_basicFeature->m_frameNum ? m_basicFeature->m_frameNum-1 : 0); // use stats from previous frame for pass 0
Expand All @@ -542,6 +560,11 @@ MHW_SETPAR_DECL_SRC(HUC_VIRTUAL_ADDR_STATE, AvcHucBrcUpdatePkt)
params.regionParams[8].presRegion = m_basicFeature->m_recycleBuf->GetBuffer(RecycleResId::HucRoiMapBuffer, m_basicFeature->m_frameNum);
}

if (m_basicFeature->m_picParam->AdaptiveTUEnabled != 0)
{
params.regionParams[12].presRegion = m_vdencBrcImageStatesReadBufferTU7[m_pipeline->m_currRecycledBufIdx];
}

return MOS_STATUS_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ struct VdencAvcHucBrcUpdateDmem
uint16_t UPD_NumSlicesForRounding;
uint32_t UPD_UserMaxFramePB; // In Bytes
uint8_t UPD_ExtCurrFrameType; // correctly calculated FrameType for all cases (including hierarchy golden BGops)
uint8_t RSVD2[3];
uint8_t UPD_AdaptiveTUEnabled;
uint8_t RSVD2[2];
};

struct VdencAvcHucBrcConstantData
Expand Down Expand Up @@ -248,7 +249,8 @@ class AvcHucBrcUpdatePkt : public EncodeHucPkt
uint32_t m_vdencBrcUpdateDmemBufferSize = sizeof(VdencAvcHucBrcUpdateDmem); //!< Offset of BRC update DMEM buffer
uint32_t m_vdencBrcConstDataBufferSize = sizeof(VdencAvcHucBrcConstantData); //!< Offset of BRC const data buffer

PMOS_RESOURCE m_vdencBrcImageStatesReadBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM] = {}; //!< Read-only VDENC+PAK IMG STATE buffer.
PMOS_RESOURCE m_vdencBrcImageStatesReadBufferOrigin[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM] = {}; //!< Read-only VDENC+PAK IMG STATE buffer.
PMOS_RESOURCE m_vdencBrcImageStatesReadBufferTU7[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM] = {}; //!< Read-only VDENC+PAK IMG STATE buffer.
PMOS_RESOURCE m_vdencBrcUpdateDmemBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][VDENC_BRC_NUM_OF_PASSES] = {}; //!< Brc Update DMEM Buffer Array.
PMOS_RESOURCE m_vdencBrcConstDataBuffer[CODECHAL_ENCODE_VDENC_BRC_CONST_BUFFER_NUM] = {}; //!< BRC Const Data Buffer for each frame type.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ namespace encode
"HEVC VDEnc ACQP Enable",
m_hevcVDEncAcqpEnabled,
MediaUserSetting::Group::Sequence);

ENCODE_CHK_NULL_RETURN(m_basicFeature->m_hevcPicParams);
MediaUserSetting::Value outValue;
ReadUserSetting(
m_userSettingPtr,
outValue,
"Adaptive TU Enable",
MediaUserSetting::Group::Sequence);
m_basicFeature->m_hevcPicParams->AdaptiveTUEnabled |= outValue.Get<uint8_t>();
#endif
return MOS_STATUS_SUCCESS;
}
Expand Down Expand Up @@ -286,6 +295,8 @@ namespace encode

hucVdencBrcUpdateDmem->UPD_Randomaccess = m_basicFeature->m_hevcSeqParams->LowDelayMode == 1 ? 0 : 1;

hucVdencBrcUpdateDmem->UPD_AdaptiveTUEnabled = m_basicFeature->m_hevcPicParams->AdaptiveTUEnabled;

return MOS_STATUS_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,10 @@ namespace encode
secondLevelBatchBufferUsed->dwOffset,
false,
m_basicFeature->m_vdencBatchBufferPerSlicePart2Size[currSlcIdx]);
if (m_basicFeature->m_hevcPicParams->AdaptiveTUEnabled == 0)
{
SETPAR_AND_ADDCMD(VDENC_HEVC_VP9_TILE_SLICE_STATE, m_vdencItf, &cmdBuffer);
}
}
else
{
Expand All @@ -1412,8 +1416,10 @@ namespace encode
ENCODE_CHK_STATUS_RETURN(AddAllCmds_HCP_PAK_INSERT_OBJECT(&cmdBuffer));

SETPAR_AND_ADDCMD(VDENC_WEIGHTSOFFSETS_STATE, m_vdencItf, &cmdBuffer);

SETPAR_AND_ADDCMD(VDENC_HEVC_VP9_TILE_SLICE_STATE, m_vdencItf, &cmdBuffer);
}
SETPAR_AND_ADDCMD(VDENC_HEVC_VP9_TILE_SLICE_STATE, m_vdencItf, &cmdBuffer);

SETPAR_AND_ADDCMD(VDENC_WALKER_STATE, m_vdencItf, &cmdBuffer);
return eStatus;
}
Expand Down
Loading

0 comments on commit 1290bba

Please sign in to comment.