| 1 |
/* |
| 2 |
* Copyright (c) 2021-2024, Intel Corporation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included |
| 12 |
* in all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
//! |
| 23 |
//! \file encode_aqm_feature.cpp |
| 24 |
//! \brief Defines the common interface for aqm feature |
| 25 |
//! |
| 26 |
|
| 27 |
#include <iomanip> |
| 28 |
|
| 29 |
#include "encode_aqm_feature.h" |
| 30 |
#include "encode_feature_manager.h" |
| 31 |
#include "encode_utils.h" |
| 32 |
#include "media_perf_profiler.h" |
| 33 |
|
| 34 |
namespace encode |
| 35 |
{ |
| 36 |
EncodeAqmFeature::EncodeAqmFeature( |
| 37 |
MediaFeatureManager *featureManager, |
| 38 |
EncodeAllocator * allocator, |
| 39 |
CodechalHwInterfaceNext *hwInterface, |
| 40 |
void * constSettings) : MediaFeature(constSettings), |
| 41 |
m_hwInterface(hwInterface), |
| 42 |
m_allocator(allocator) |
| 43 |
{ |
| 44 |
ENCODE_FUNC_CALL(); |
| 45 |
|
| 46 |
m_featureManager = featureManager; |
| 47 |
|
| 48 |
auto encFeatureManager = dynamic_cast<EncodeFeatureManager *>(featureManager); |
| 49 |
ENCODE_CHK_NULL_NO_STATUS_RETURN(encFeatureManager); |
| 50 |
|
| 51 |
if (hwInterface) |
| 52 |
{ |
| 53 |
m_mosCtx = hwInterface->GetOsInterface()->pOsContext; |
| 54 |
m_userSettingPtr = hwInterface->GetOsInterface()->pfnGetUserSettingInstance(hwInterface->GetOsInterface()); |
| 55 |
} |
| 56 |
|
| 57 |
m_basicFeature = dynamic_cast<EncodeBasicFeature *>(encFeatureManager->GetFeature(FeatureIDs::basicFeature)); |
| 58 |
ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature); |
| 59 |
|
| 60 |
#if (_DEBUG || _RELEASE_INTERNAL) |
| 61 |
MediaUserSetting::Value outValue; |
| 62 |
ReadUserSetting( |
| 63 |
m_userSettingPtr, |
| 64 |
outValue, |
| 65 |
"VDAQM Enable", |
| 66 |
MediaUserSetting::Group::Sequence); |
| 67 |
|
| 68 |
m_metricsDumpMode = outValue.Get<uint8_t>(); |
| 69 |
|
| 70 |
#endif |
| 71 |
|
| 72 |
if (m_metricsDumpMode > 0) |
| 73 |
{ |
| 74 |
m_enabled = true; |
| 75 |
} |
| 76 |
#if _MEDIA_RESERVED |
| 77 |
#if USE_CODECHAL_DEBUG_TOOL |
| 78 |
MetricsDump(); |
| 79 |
#endif |
| 80 |
#endif |
| 81 |
} |
| 82 |
|
| 83 |
EncodeAqmFeature::~EncodeAqmFeature() |
| 84 |
{ |
| 85 |
if (m_enabled) |
| 86 |
{ |
| 87 |
FreeResources(); |
| 88 |
#if _MEDIA_RESERVED |
| 89 |
#if USE_CODECHAL_DEBUG_TOOL |
| 90 |
CloseDumpFiles(); |
| 91 |
#endif |
| 92 |
#endif |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
uint32_t EncodeAqmFeature::EncodeAqmFeatureFunction0(uint32_t frameWidth, uint32_t frameHeight, uint8_t index) |
| 97 |
{ |
| 98 |
uint32_t width = (frameWidth + (1 << index) - 1) >> index; |
| 99 |
uint32_t height = (frameHeight + (1 << index) - 1) >> index; |
| 100 |
return (MOS_ALIGN_CEIL(MOS_ALIGN_CEIL(width, 4) / 4, CL_SIZE_BYTES) * (MOS_ALIGN_CEIL(height, 4) / 4)); |
| 101 |
} |
| 102 |
|
| 103 |
MOS_STATUS EncodeAqmFeature::Update(void *params) |
| 104 |
{ |
| 105 |
if (m_enabled) |
| 106 |
{ |
| 107 |
ENCODE_CHK_STATUS_RETURN(AllocateResources()); |
| 108 |
} |
| 109 |
|
| 110 |
return MOS_STATUS_SUCCESS; |
| 111 |
} |
| 112 |
|
| 113 |
MOS_STATUS EncodeAqmFeature::AllocateResources() |
| 114 |
{ |
| 115 |
ENCODE_FUNC_CALL(); |
| 116 |
|
| 117 |
if (m_AllocatedResources) |
| 118 |
{ |
| 119 |
return MOS_STATUS_SUCCESS; |
| 120 |
} |
| 121 |
|
| 122 |
MOS_ALLOC_GFXRES_PARAMS allocParamsForBufferLinear; |
| 123 |
MOS_ZeroMemory(&allocParamsForBufferLinear, sizeof(MOS_ALLOC_GFXRES_PARAMS)); |
| 124 |
|
| 125 |
allocParamsForBufferLinear.Type = MOS_GFXRES_BUFFER; |
| 126 |
allocParamsForBufferLinear.TileType = MOS_TILE_LINEAR; |
| 127 |
allocParamsForBufferLinear.Format = Format_Buffer; |
| 128 |
allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ; |
| 129 |
uint32_t rowstoreBufferSize0 = m_useFastPass ? (m_dsWidth / 4 + 1) : (m_basicFeature->m_oriFrameWidth / 4 + 1); |
| 130 |
uint32_t rowstoreBufferSize[5] = {rowstoreBufferSize0, 3391, 1665, 833, 417}; |
| 131 |
for (int i = 0; i < 5; i++) |
| 132 |
{ |
| 133 |
std::string bufName = "Index" + std::to_string(i) + "LineRowstoreBuffer"; |
| 134 |
allocParamsForBufferLinear.dwBytes = rowstoreBufferSize[i] * CL_SIZE_BYTES * m_numRowStore; |
| 135 |
allocParamsForBufferLinear.pBufName = &*bufName.begin(); |
| 136 |
EncodeAqmFeatureMember0[i] = m_allocator->AllocateResource(allocParamsForBufferLinear, false); |
| 137 |
EncodeAqmFeatureMember1[i] = rowstoreBufferSize[i] * CL_SIZE_BYTES; |
| 138 |
} |
| 139 |
|
| 140 |
EncodeAqmFeatureMember2 = ((sizeof(AQM_Ouput_Format) + CL_SIZE_BYTES - 1) / CL_SIZE_BYTES) * CL_SIZE_BYTES * m_numTiles; |
| 141 |
|
| 142 |
uint32_t width = m_useFastPass ? m_dsWidth : m_basicFeature->m_oriFrameWidth; |
| 143 |
uint32_t height = m_useFastPass ? m_dsHeight : m_basicFeature->m_oriFrameHeight; |
| 144 |
EncodeAqmFeatureMember3[0] = 0; |
| 145 |
EncodeAqmFeatureMember3[1] = EncodeAqmFeatureFunction0(width, height, 1); |
| 146 |
EncodeAqmFeatureMember3[2] = EncodeAqmFeatureFunction0(width, height, 2); |
| 147 |
EncodeAqmFeatureMember3[3] = EncodeAqmFeatureFunction0(width, height, 3); |
| 148 |
EncodeAqmFeatureMember3[4] = EncodeAqmFeatureFunction0(width, height, 4); |
| 149 |
|
| 150 |
// allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE; |
| 151 |
|
| 152 |
allocParamsForBufferLinear.dwBytes = EncodeAqmFeatureMember2; |
| 153 |
allocParamsForBufferLinear.pBufName = "VdaqmBuffer0"; |
| 154 |
m_basicFeature->m_recycleBuf->RegisterResource(RecycleResId::VdaqmBuffer0, allocParamsForBufferLinear, EncodeBasicFeature::m_uncompressedSurfaceNum); |
| 155 |
|
| 156 |
allocParamsForBufferLinear.dwBytes = EncodeAqmFeatureMember3[1]; |
| 157 |
allocParamsForBufferLinear.pBufName = "VdaqmBuffer1"; |
| 158 |
m_basicFeature->m_recycleBuf->RegisterResource(RecycleResId::VdaqmBuffer1, allocParamsForBufferLinear, EncodeBasicFeature::m_uncompressedSurfaceNum); |
| 159 |
|
| 160 |
allocParamsForBufferLinear.dwBytes = EncodeAqmFeatureMember3[2]; |
| 161 |
allocParamsForBufferLinear.pBufName = "VdaqmBuffer2"; |
| 162 |
m_basicFeature->m_recycleBuf->RegisterResource(RecycleResId::VdaqmBuffer2, allocParamsForBufferLinear, EncodeBasicFeature::m_uncompressedSurfaceNum); |
| 163 |
|
| 164 |
allocParamsForBufferLinear.dwBytes = EncodeAqmFeatureMember3[3]; |
| 165 |
allocParamsForBufferLinear.pBufName = "VdaqmBuffer3"; |
| 166 |
m_basicFeature->m_recycleBuf->RegisterResource(RecycleResId::VdaqmBuffer3, allocParamsForBufferLinear, EncodeBasicFeature::m_uncompressedSurfaceNum); |
| 167 |
|
| 168 |
allocParamsForBufferLinear.dwBytes = EncodeAqmFeatureMember3[4]; |
| 169 |
allocParamsForBufferLinear.pBufName = "VdaqmBuffer4"; |
| 170 |
m_basicFeature->m_recycleBuf->RegisterResource(RecycleResId::VdaqmBuffer4, allocParamsForBufferLinear, EncodeBasicFeature::m_uncompressedSurfaceNum); |
| 171 |
|
| 172 |
m_AllocatedResources = true; |
| 173 |
|
| 174 |
return MOS_STATUS_SUCCESS; |
| 175 |
} |
| 176 |
|
| 177 |
MOS_STATUS EncodeAqmFeature::FreeResources() |
| 178 |
{ |
| 179 |
ENCODE_FUNC_CALL(); |
| 180 |
|
| 181 |
if (m_AllocatedResources) |
| 182 |
{ |
| 183 |
for (uint8_t index = 0; index < AQM_INDEX; index++) |
| 184 |
m_allocator->DestroyResource(EncodeAqmFeatureMember0[index]); |
| 185 |
} |
| 186 |
|
| 187 |
m_AllocatedResources = false; |
| 188 |
|
| 189 |
return MOS_STATUS_SUCCESS; |
| 190 |
} |
| 191 |
|
| 192 |
MHW_SETPAR_DECL_SRC(AQM_FRAME_START, EncodeAqmFeature) |
| 193 |
{ |
| 194 |
if (m_enabled) |
| 195 |
{ |
| 196 |
params.aqmFrameStart = 1; |
| 197 |
} |
| 198 |
|
| 199 |
return MOS_STATUS_SUCCESS; |
| 200 |
} |
| 201 |
|
| 202 |
MHW_SETPAR_DECL_SRC(AQM_PIC_STATE, EncodeAqmFeature) |
| 203 |
{ |
| 204 |
if (m_enabled) |
| 205 |
{ |
| 206 |
params.frameWidthInPixelMinus1 = m_useFastPass ? (MOS_ALIGN_CEIL(m_dsWidth, 8) - 1) : (MOS_ALIGN_CEIL(m_basicFeature->m_oriFrameWidth, 8) - 1); |
| 207 |
params.FrameHeightInPixelMinus1 = m_useFastPass ? (MOS_ALIGN_CEIL(m_dsHeight, 8) - 1) : (MOS_ALIGN_CEIL(m_basicFeature->m_oriFrameHeight, 8) - 1); |
| 208 |
params.vdaqmEnable = m_enabled; |
| 209 |
params.tileBasedEngine = m_tileBasedEngine; |
| 210 |
params.chromasubsampling = m_basicFeature->m_chromaFormat - 1; |
| 211 |
params.aqmMode = m_aqmMode; |
| 212 |
params.sseEnable = true; |
| 213 |
#if _MEDIA_RESERVED |
| 214 |
#define AQM_PIC_STATE_SETPAR_EXT |
| 215 |
#include "encode_aqm_feature_ext.h" |
| 216 |
#undef AQM_PIC_STATE_SETPAR_EXT |
| 217 |
#else |
| 218 |
params.extSettings.emplace_back( |
| 219 |
[this](uint32_t *data) { |
| 220 |
data[2] |= 0x1e; |
| 221 |
data[3] |= 0x1a334d66; |
| 222 |
data[4] |= 0x809ab3cd; |
| 223 |
data[5] |= 0xe6000000; |
| 224 |
data[6] |= 0x80001; |
| 225 |
data[7] |= 0x700025; |
| 226 |
data[8] |= 0x11000da; |
| 227 |
data[9] |= 0x80001; |
| 228 |
data[10] |= 0x700025; |
| 229 |
data[11] |= 0x11000da; |
| 230 |
|
| 231 |
return MOS_STATUS_SUCCESS; |
| 232 |
}); |
| 233 |
#endif |
| 234 |
if (m_basicFeature->m_bitDepth == 8) |
| 235 |
{ |
| 236 |
params.pixelbitdepth = 0; |
| 237 |
} |
| 238 |
else if (m_basicFeature->m_bitDepth == 10) |
| 239 |
{ |
| 240 |
params.pixelbitdepth = 1; |
| 241 |
} |
| 242 |
else if (m_basicFeature->m_bitDepth == 12) |
| 243 |
{ |
| 244 |
params.pixelbitdepth = 2; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
return MOS_STATUS_SUCCESS; |
| 249 |
} |
| 250 |
|
| 251 |
MHW_SETPAR_DECL_SRC(AQM_PIPE_BUF_ADDR_STATE, EncodeAqmFeature) |
| 252 |
{ |
| 253 |
if (m_enabled) |
| 254 |
{ |
| 255 |
auto bIdx = m_basicFeature->m_currOriginalPic.FrameIdx; |
| 256 |
|
| 257 |
for (uint8_t index = 0; index < AQM_INDEX; index++) |
| 258 |
{ |
| 259 |
params.AqmPipeBufAddrStatePar0[index] = EncodeAqmFeatureMember0[index]; |
| 260 |
params.AqmPipeBufAddrStatePar1[index] = EncodeAqmFeatureMember1[index] * m_currPipeNum; |
| 261 |
} |
| 262 |
|
| 263 |
params.AqmPipeBufAddrStatePar4[0] = nullptr; |
| 264 |
params.AqmPipeBufAddrStatePar4[1] = m_basicFeature->m_recycleBuf->GetBuffer(RecycleResId::VdaqmBuffer1, bIdx); |
| 265 |
params.AqmPipeBufAddrStatePar4[2] = m_basicFeature->m_recycleBuf->GetBuffer(RecycleResId::VdaqmBuffer2, bIdx); |
| 266 |
params.AqmPipeBufAddrStatePar4[3] = m_basicFeature->m_recycleBuf->GetBuffer(RecycleResId::VdaqmBuffer3, bIdx); |
| 267 |
params.AqmPipeBufAddrStatePar4[4] = m_basicFeature->m_recycleBuf->GetBuffer(RecycleResId::VdaqmBuffer4, bIdx); |
| 268 |
params.AqmPipeBufAddrStatePar2 = m_basicFeature->m_recycleBuf->GetBuffer(RecycleResId::VdaqmBuffer0, bIdx); |
| 269 |
} |
| 270 |
|
| 271 |
return MOS_STATUS_SUCCESS; |
| 272 |
} |
| 273 |
|
| 274 |
#if USE_CODECHAL_DEBUG_TOOL |
| 275 |
MOS_STATUS EncodeAqmFeature::UpdateFrameDisplayOrder(const uint16_t pictureCodingType, const uint32_t framePOC, const uint32_t gopPicSize) |
| 276 |
{ |
| 277 |
if (pictureCodingType == I_TYPE) |
| 278 |
{ |
| 279 |
m_frameNumPrevious += m_gopSizePrevious; |
| 280 |
} |
| 281 |
uint32_t displayOrderInGOP = framePOC; |
| 282 |
uint32_t displayOrderInSeq = displayOrderInGOP + m_frameNumPrevious; |
| 283 |
m_gopSizePrevious = gopPicSize; |
| 284 |
m_frameIdxQueue.push(displayOrderInSeq); |
| 285 |
return MOS_STATUS_SUCCESS; |
| 286 |
} |
| 287 |
#endif |
| 288 |
|
| 289 |
#if _MEDIA_RESERVED |
| 290 |
#define AQM_FEATURE_SOURCE_EXT |
| 291 |
#include "encode_aqm_feature_ext.h" |
| 292 |
#undef AQM_FEATURE_SOURCE_EXT |
| 293 |
#endif |
| 294 |
|
| 295 |
MOS_STATUS EncodeAqmFeature::GetFrameMSE(AQM_Ouput_Format* pDataFrame, uint32_t (&MSE)[3]) |
| 296 |
{ |
| 297 |
ENCODE_CHK_NULL_RETURN(pDataFrame); |
| 298 |
|
| 299 |
uint64_t SSE[3] = {}; |
| 300 |
uint32_t areaSum = 0; |
| 301 |
|
| 302 |
// Get Tile MSE |
| 303 |
uint32_t offsetAqmTile = MOS_ALIGN_CEIL(sizeof(AQM_Ouput_Format), CL_SIZE_BYTES); |
| 304 |
for (uint32_t tileIdx = 0; tileIdx < m_numTiles && tileIdx < ENCODE_VDENC_MAX_TILE_NUM; tileIdx++) |
| 305 |
{ |
| 306 |
AQM_Ouput_Format* pTileVdaqmInfo = (AQM_Ouput_Format*)((char*)pDataFrame + offsetAqmTile * tileIdx); |
| 307 |
ENCODE_CHK_NULL_RETURN(pTileVdaqmInfo); |
| 308 |
|
| 309 |
uint32_t area = m_tile_width[tileIdx] * m_tile_height[tileIdx]; |
| 310 |
SSE[0] += (uint64_t)pTileVdaqmInfo->SSEY * (uint64_t)area; |
| 311 |
SSE[1] += (uint64_t)pTileVdaqmInfo->SSEU * (uint64_t)area; |
| 312 |
SSE[2] += (uint64_t)pTileVdaqmInfo->SSEV * (uint64_t)area; |
| 313 |
areaSum += area; |
| 314 |
} |
| 315 |
|
| 316 |
if (areaSum == 0) |
| 317 |
{ |
| 318 |
ENCODE_ASSERTMESSAGE("frame pixel num cal by each tile is zero, sth must be wrong!"); |
| 319 |
return MOS_STATUS_INVALID_PARAMETER; |
| 320 |
} |
| 321 |
|
| 322 |
MSE[0] = (uint32_t)(SSE[0] / (uint64_t)areaSum); |
| 323 |
MSE[1] = (uint32_t)(SSE[1] / (uint64_t)areaSum); |
| 324 |
MSE[2] = (uint32_t)(SSE[2] / (uint64_t)areaSum); |
| 325 |
|
| 326 |
return MOS_STATUS_SUCCESS; |
| 327 |
} |
| 328 |
|
| 329 |
MOS_STATUS EncodeAqmFeature::ReportQualityInfoFrame(uint32_t statBufIdx, EncodeStatusReportData& statusReportData) |
| 330 |
{ |
| 331 |
ENCODE_FUNC_CALL(); |
| 332 |
|
| 333 |
MOS_STATUS eStatus = MOS_STATUS_SUCCESS; |
| 334 |
|
| 335 |
PMOS_RESOURCE pBuffer = m_basicFeature->m_recycleBuf->GetBuffer(RecycleResId::VdaqmBuffer0, statBufIdx); |
| 336 |
ENCODE_CHK_NULL_RETURN(pBuffer); |
| 337 |
|
| 338 |
AQM_Ouput_Format* pDataFrameStas = (AQM_Ouput_Format*)m_allocator->LockResourceForRead(pBuffer); |
| 339 |
ENCODE_CHK_NULL_RETURN(pDataFrameStas); |
| 340 |
|
| 341 |
ENCODE_CHK_STATUS_RETURN(GetFrameMSE(pDataFrameStas, statusReportData.MSE)); |
| 342 |
|
| 343 |
ENCODE_CHK_STATUS_RETURN(m_allocator->UnLock(pBuffer)); |
| 344 |
|
| 345 |
return eStatus; |
| 346 |
} |
| 347 |
} // namespace encode |