| 1 | /* |
| 2 | * Copyright (c) 2020-2023, 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_vp9_pak.cpp |
| 24 | //! \brief Defines the common interface for vp9 vdenc\pak features |
| 25 | //! |
| 26 | |
| 27 | #include "encode_vp9_pak.h" |
| 28 | #include "codec_def_common.h" |
| 29 | #include "encode_vp9_vdenc_feature_manager.h" |
| 30 | #include "encode_vp9_vdenc_const_settings.h" |
| 31 | #include "encode_vp9_brc.h" |
| 32 | #include "encode_vp9_hpu.h" |
| 33 | #include "encode_vp9_segmentation.h" |
| 34 | #include "media_cmd_packet.h" |
| 35 | |
| 36 | namespace encode |
| 37 | { |
| 38 | Vp9EncodePak::Vp9EncodePak( |
| 39 | MediaFeatureManager *featureManager, |
| 40 | EncodeAllocator * allocator, |
| 41 | CodechalHwInterfaceNext *hwInterface, |
| 42 | void * constSettings) |
| 43 | : MediaFeature(constSettings), |
| 44 | m_hwInterface(hwInterface), |
| 45 | m_allocator(allocator) |
| 46 | { |
| 47 | ENCODE_FUNC_CALL(); |
| 48 | ENCODE_CHK_NULL_NO_STATUS_RETURN(featureManager); |
| 49 | |
| 50 | m_featureManager = featureManager; |
| 51 | auto encFeatureManager = dynamic_cast<EncodeVp9VdencFeatureManager *>(featureManager); |
| 52 | ENCODE_CHK_NULL_NO_STATUS_RETURN(encFeatureManager); |
| 53 | |
| 54 | m_basicFeature = dynamic_cast<Vp9BasicFeature *>(encFeatureManager->GetFeature(FeatureIDs::basicFeature)); |
| 55 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature); |
| 56 | |
| 57 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_hwInterface); |
| 58 | |
| 59 | m_hcpInterfaceNew = std::static_pointer_cast<mhw::vdbox::hcp::Itf>(m_hwInterface->GetHcpInterfaceNext()); |
| 60 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_hcpInterfaceNew); |
| 61 | |
| 62 | m_vdencInterfaceNew = std::static_pointer_cast<mhw::vdbox::vdenc::Itf>(m_hwInterface->GetVdencInterfaceNext()); |
| 63 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_vdencInterfaceNew); |
| 64 | |
| 65 | m_miItf = std::static_pointer_cast<mhw::mi::Itf>(m_hwInterface->GetMiInterfaceNext()); |
| 66 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_miItf); |
| 67 | } |
| 68 | |
| 69 | MOS_STATUS Vp9EncodePak::Init(void *settings) |
| 70 | { |
| 71 | ENCODE_FUNC_CALL(); |
| 72 | |
| 73 | m_enabled = true; |
| 74 | |
| 75 | ENCODE_CHK_STATUS_RETURN(m_hwInterface->GetVdencPictureSecondLevelCommandsSize( |
| 76 | CODECHAL_ENCODE_MODE_VP9, &m_vdencPicStateSecondLevelBatchBufferSize)); |
| 77 | |
| 78 | ENCODE_CHK_STATUS_RETURN(AllocateResources()); |
| 79 | |
| 80 | return MOS_STATUS_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | MOS_STATUS Vp9EncodePak::Update(void *params) |
| 84 | { |
| 85 | ENCODE_FUNC_CALL(); |
| 86 | ENCODE_CHK_NULL_RETURN(params); |
| 87 | |
| 88 | EncoderParams *encodeParams = (EncoderParams *)params; |
| 89 | |
| 90 | auto vp9SeqParams = static_cast<PCODEC_VP9_ENCODE_SEQUENCE_PARAMS>(encodeParams->pSeqParams); |
| 91 | ENCODE_CHK_NULL_RETURN(vp9SeqParams); |
| 92 | auto vp9PicParams = static_cast<PCODEC_VP9_ENCODE_PIC_PARAMS>(encodeParams->pPicParams); |
| 93 | ENCODE_CHK_NULL_RETURN(vp9PicParams); |
| 94 | |
| 95 | if (m_basicFeature->m_newSeq) |
| 96 | { |
| 97 | ENCODE_CHK_STATUS_RETURN(SetConstSettings()); |
| 98 | } |
| 99 | |
| 100 | // Reset picture state 2nd level batch buffer stage |
| 101 | for (auto i = 0; i < 3; ++i) |
| 102 | { |
| 103 | m_picState2ndLevelBBConstructed[i] = false; |
| 104 | } |
| 105 | |
| 106 | if (m_basicFeature->m_ref.DysRefFrameFlags() != DYS_REF_NONE) |
| 107 | { |
| 108 | ENCODE_CHK_STATUS_RETURN(PreparePakObjAndCuRecord()); |
| 109 | } |
| 110 | |
| 111 | return MOS_STATUS_SUCCESS; |
| 112 | } |
| 113 | |
| 114 | MOS_STATUS Vp9EncodePak::SetRegionsForBrcUpdate(mhw::vdbox::huc::HUC_VIRTUAL_ADDR_STATE_PAR ¶ms, uint32_t currPass) const |
| 115 | { |
| 116 | ENCODE_FUNC_CALL(); |
| 117 | |
| 118 | // Input SLBB (second level batch buffer) - IN |
| 119 | params.regionParams[3].presRegion = const_cast<PMOS_RESOURCE>(&m_resVdencPictureState2ndLevelBatchBufferRead[currPass][m_vdencPictureState2ndLevelBBIndex]); |
| 120 | |
| 121 | // Output SLBB - OUT |
| 122 | params.regionParams[6].presRegion = const_cast<PMOS_RESOURCE>(&m_resVdencPictureState2ndLevelBatchBufferWrite[0]); |
| 123 | params.regionParams[6].isWritable = true; |
| 124 | |
| 125 | return MOS_STATUS_SUCCESS; |
| 126 | } |
| 127 | |
| 128 | MOS_STATUS Vp9EncodePak::SetRegionsForHucProb(mhw::vdbox::huc::HUC_VIRTUAL_ADDR_STATE_PAR ¶ms, uint32_t currPass) const |
| 129 | { |
| 130 | ENCODE_FUNC_CALL(); |
| 131 | |
| 132 | auto brcFeature = dynamic_cast<Vp9EncodeBrc *>(m_featureManager->GetFeature(Vp9FeatureIDs::vp9BrcFeature)); |
| 133 | ENCODE_CHK_NULL_RETURN(brcFeature); |
| 134 | |
| 135 | // Input regions: 7, 8 |
| 136 | |
| 137 | // If BRC enabled, BRC Pass 2 output SLBB -> input SLBB for HPU on pass 2 (HPU pass 1 and 3. BRC Update pass 1 and 2) |
| 138 | // BRC Pass 1 output SLBB -> input SLBB for HPU on pass 1 |
| 139 | // If BRC not on, Driver prepared SLBB -> input to HPU on both passes |
| 140 | if (brcFeature->IsVdencBrcEnabled()) |
| 141 | { |
| 142 | params.regionParams[7].presRegion = const_cast<MOS_RESOURCE *>(&m_resVdencPictureState2ndLevelBatchBufferWrite[0]); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | params.regionParams[7].presRegion = const_cast<MOS_RESOURCE *>(&m_resVdencPictureState2ndLevelBatchBufferRead[currPass][m_vdencPictureState2ndLevelBBIndex]); |
| 147 | } |
| 148 | params.regionParams[8].presRegion = const_cast<MOS_RESOURCE *>(&m_resHucPakInsertUncompressedHeaderReadBuffer[m_basicFeature->m_currRecycledBufIdx]); |
| 149 | |
| 150 | // Output regions: 4, 5, 6, 10, 11 |
| 151 | |
| 152 | params.regionParams[4].presRegion = const_cast<MOS_RESOURCE *>(&m_resHucPakInsertUncompressedHeaderWriteBuffer); |
| 153 | params.regionParams[4].isWritable = true; |
| 154 | params.regionParams[5].presRegion = const_cast<MOS_RESOURCE *>(&m_resCompressedHeaderBuffer); |
| 155 | params.regionParams[5].isWritable = true; |
| 156 | params.regionParams[6].presRegion = const_cast<MOS_RESOURCE *>(&m_resVdencPictureState2ndLevelBatchBufferWrite[0]); |
| 157 | params.regionParams[6].isWritable = true; |
| 158 | params.regionParams[10].presRegion = const_cast<MOS_RESOURCE *>(&m_basicFeature->m_resBitstreamBuffer); |
| 159 | params.regionParams[10].isWritable = true; |
| 160 | params.regionParams[11].presRegion = const_cast<MOS_RESOURCE *>(&m_resVdencDataExtensionBuffer); |
| 161 | params.regionParams[11].isWritable = true; |
| 162 | |
| 163 | return MOS_STATUS_SUCCESS; |
| 164 | } |
| 165 | |
| 166 | MOS_STATUS Vp9EncodePak::ConstructPicStateBatchBuffer(EncodePipeline* pipeline) |
| 167 | { |
| 168 | ENCODE_FUNC_CALL(); |
| 169 | ENCODE_CHK_NULL_RETURN(pipeline); |
| 170 | ENCODE_CHK_NULL_RETURN(m_basicFeature); |
| 171 | |
| 172 | auto currPass = pipeline->GetCurrentPass(); |
| 173 | auto passNum = pipeline->GetPassNum(); |
| 174 | auto isFirstPipe = pipeline->IsFirstPipe(); |
| 175 | auto isLastPass = pipeline->IsLastPass(); |
| 176 | |
| 177 | auto dysRefFrameFlags = m_basicFeature->m_ref.DysRefFrameFlags(); |
| 178 | if ((dysRefFrameFlags == DYS_REF_NONE) && m_basicFeature->m_pakOnlyModeEnabledForLastPass) |
| 179 | { |
| 180 | // This flag sets PAK-only mode in SLBB for rePAK pass. In single-pass mode, this flag should be disabled. |
| 181 | m_basicFeature->m_vdencPakonlyMultipassEnabled = ((passNum > 1) && isLastPass) ? true : false; |
| 182 | } |
| 183 | |
| 184 | // This function will call by Vp9HucProbPkt or Vp9HucBrcUpdatePkt. |
| 185 | // Only construct once. |
| 186 | // Check if picture state 2nd level batch buffer already constrcuted, than skip. |
| 187 | if (currPass < 3 && m_picState2ndLevelBBConstructed[currPass]) |
| 188 | { |
| 189 | return MOS_STATUS_SUCCESS; |
| 190 | } |
| 191 | |
| 192 | PMOS_RESOURCE picStateBuffer = nullptr; |
| 193 | if (dysRefFrameFlags != DYS_REF_NONE && m_basicFeature->m_dysVdencMultiPassEnabled) |
| 194 | { |
| 195 | picStateBuffer = &m_resVdencDysPictureState2ndLevelBatchBuffer; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | if (!isFirstPipe) |
| 200 | { |
| 201 | return MOS_STATUS_SUCCESS; |
| 202 | } |
| 203 | |
| 204 | if (currPass >= 3) |
| 205 | { |
| 206 | ENCODE_ASSERTMESSAGE("Index exceeds the max number, when try to get m_resVdencPictureState2ndLevelBatchBufferRead;"); |
| 207 | return MOS_STATUS_INVALID_PARAMETER; |
| 208 | } |
| 209 | |
| 210 | picStateBuffer = &m_resVdencPictureState2ndLevelBatchBufferRead[currPass][m_vdencPictureState2ndLevelBBIndex]; |
| 211 | } |
| 212 | ENCODE_CHK_NULL_RETURN(picStateBuffer); |
| 213 | |
| 214 | MOS_STATUS eStatus = MOS_STATUS_SUCCESS; |
| 215 | |
| 216 | auto vp9SeqParams = static_cast<PCODEC_VP9_ENCODE_SEQUENCE_PARAMS>(m_basicFeature->m_vp9SeqParams); |
| 217 | ENCODE_CHK_NULL_RETURN(vp9SeqParams); |
| 218 | auto vp9PicParams = static_cast<PCODEC_VP9_ENCODE_PIC_PARAMS>(m_basicFeature->m_vp9PicParams); |
| 219 | ENCODE_CHK_NULL_RETURN(vp9PicParams); |
| 220 | auto vp9SegmentParams = static_cast<PCODEC_VP9_ENCODE_SEGMENT_PARAMS>(m_basicFeature->m_vp9SegmentParams); |
| 221 | ENCODE_CHK_NULL_RETURN(vp9SegmentParams); |
| 222 | auto osInterface = m_hwInterface->GetOsInterface(); |
| 223 | ENCODE_CHK_NULL_RETURN(osInterface); |
| 224 | |
| 225 | uint8_t *data = (uint8_t *)m_allocator->LockResourceForWrite(picStateBuffer); |
| 226 | ENCODE_CHK_NULL_RETURN(data); |
| 227 | |
| 228 | MOS_COMMAND_BUFFER constructedCmdBuf; |
| 229 | MOS_ZeroMemory(&constructedCmdBuf, sizeof(constructedCmdBuf)); |
| 230 | constructedCmdBuf.pCmdBase = (uint32_t *)data; |
| 231 | constructedCmdBuf.pCmdPtr = (uint32_t *)data; |
| 232 | constructedCmdBuf.iOffset = 0; |
| 233 | constructedCmdBuf.iRemaining = m_vdencPicStateSecondLevelBatchBufferSize; |
| 234 | |
| 235 | eStatus = AddVdencCmd1Command(constructedCmdBuf); |
| 236 | if (eStatus != MOS_STATUS_SUCCESS) |
| 237 | { |
| 238 | ENCODE_CHK_STATUS_RETURN(m_allocator->UnLock(picStateBuffer)); |
| 239 | ENCODE_ASSERTMESSAGE("Failed to add CODECHAL_CMD1 command."); |
| 240 | return eStatus; |
| 241 | } |
| 242 | |
| 243 | m_basicFeature->m_hucPicStateOffset = (uint16_t)constructedCmdBuf.iOffset; |
| 244 | |
| 245 | SETPAR_AND_ADDCMD(HCP_VP9_PIC_STATE, m_hcpInterfaceNew, &constructedCmdBuf); |
| 246 | |
| 247 | // HCP_VP9_SEGMENT_STATE |
| 248 | uint8_t segmentCount = (vp9PicParams->PicFlags.fields.segmentation_enabled) ? CODEC_VP9_MAX_SEGMENTS : 1; |
| 249 | |
| 250 | for (uint8_t i = 0; i < segmentCount; i++) |
| 251 | { |
| 252 | RUN_FEATURE_INTERFACE_RETURN(Vp9Segmentation, Vp9FeatureIDs::vp9Segmentation, SetSegmentId, i); |
| 253 | SETPAR_AND_ADDCMD(HCP_VP9_SEGMENT_STATE, m_hcpInterfaceNew, &constructedCmdBuf); |
| 254 | } |
| 255 | |
| 256 | // Adjust cmd buffer offset to have 8 segment state blocks |
| 257 | if (segmentCount < CODEC_VP9_MAX_SEGMENTS) |
| 258 | { |
| 259 | // Max 7 segments, 32 bytes each |
| 260 | uint8_t zeroBlock[Vp9Segmentation::m_segmentStateBlockSize * (CODEC_VP9_MAX_SEGMENTS - 1)]; |
| 261 | MOS_ZeroMemory(zeroBlock, sizeof(zeroBlock)); |
| 262 | Mhw_AddCommandCmdOrBB(osInterface, &constructedCmdBuf, nullptr, zeroBlock, (CODEC_VP9_MAX_SEGMENTS - segmentCount) * Vp9Segmentation::m_segmentStateBlockSize); |
| 263 | } |
| 264 | |
| 265 | m_basicFeature->m_slbbImgStateOffset = (uint16_t)constructedCmdBuf.iOffset; |
| 266 | |
| 267 | eStatus = AddVdencCmd2Command(constructedCmdBuf); |
| 268 | if (eStatus != MOS_STATUS_SUCCESS) |
| 269 | { |
| 270 | ENCODE_CHK_STATUS_RETURN(m_allocator->UnLock(picStateBuffer)); |
| 271 | ENCODE_ASSERTMESSAGE("Failed to add CODECHAL_CMD2 command."); |
| 272 | return eStatus; |
| 273 | } |
| 274 | |
| 275 | // BB_END |
| 276 | eStatus = m_miItf->AddMiBatchBufferEnd(&constructedCmdBuf, nullptr); |
| 277 | if (eStatus != MOS_STATUS_SUCCESS) |
| 278 | { |
| 279 | ENCODE_CHK_STATUS_RETURN(m_allocator->UnLock(picStateBuffer)); |
| 280 | ENCODE_ASSERTMESSAGE("Failed to add MI Batch Buffer End command."); |
| 281 | return eStatus; |
| 282 | } |
| 283 | |
| 284 | uint32_t padding = MOS_ALIGN_CEIL(constructedCmdBuf.iOffset, 64) - constructedCmdBuf.iOffset; |
| 285 | constructedCmdBuf.iOffset += padding; // padding for alignment on 64 |
| 286 | constructedCmdBuf.iRemaining -= padding; |
| 287 | if ((constructedCmdBuf.iOffset != m_vdencPicStateSecondLevelBatchBufferSize) && |
| 288 | (constructedCmdBuf.iRemaining < 0)) |
| 289 | { |
| 290 | ENCODE_ASSERTMESSAGE("Failed to constructed Second Level Batch Buffer: No space for padding bytes."); |
| 291 | return MOS_STATUS_NO_SPACE; |
| 292 | } |
| 293 | |
| 294 | m_basicFeature->m_hucSlbbSize = (uint16_t)constructedCmdBuf.iOffset; |
| 295 | |
| 296 | ENCODE_CHK_STATUS_RETURN(m_allocator->UnLock(picStateBuffer)); |
| 297 | |
| 298 | // Indicated picture state 2nd level batch buffer for this pass already constructed |
| 299 | m_picState2ndLevelBBConstructed[currPass] = true; |
| 300 | |
| 301 | return MOS_STATUS_SUCCESS; |
| 302 | } |
| 303 | |
| 304 | MOS_STATUS Vp9EncodePak::SetHucPakInsertObjBatchBuffer(MHW_BATCH_BUFFER &secondLevelBatchBuffer) |
| 305 | { |
| 306 | ENCODE_FUNC_CALL(); |
| 307 | |
| 308 | secondLevelBatchBuffer.dwOffset = 0; |
| 309 | secondLevelBatchBuffer.bSecondLevel = true; |
| 310 | |
| 311 | if (!m_basicFeature->m_hucEnabled) |
| 312 | { |
| 313 | secondLevelBatchBuffer.OsResource = m_resHucPakInsertUncompressedHeaderReadBuffer[m_basicFeature->m_currRecycledBufIdx]; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | secondLevelBatchBuffer.OsResource = m_resHucPakInsertUncompressedHeaderWriteBuffer; |
| 318 | } |
| 319 | |
| 320 | return MOS_STATUS_SUCCESS; |
| 321 | } |
| 322 | |
| 323 | MOS_STATUS Vp9EncodePak::SetVdencSecondLevelBatchBuffer(uint32_t currPass, MHW_BATCH_BUFFER &secondLevelBatchBuffer) |
| 324 | { |
| 325 | ENCODE_FUNC_CALL(); |
| 326 | |
| 327 | secondLevelBatchBuffer.dwOffset = 0; |
| 328 | secondLevelBatchBuffer.bSecondLevel = true; |
| 329 | |
| 330 | if (m_basicFeature->m_hucEnabled) |
| 331 | { |
| 332 | secondLevelBatchBuffer.OsResource = m_resVdencPictureState2ndLevelBatchBufferWrite[0]; |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | auto dysRefFrameFlags = m_basicFeature->m_ref.DysRefFrameFlags(); |
| 337 | auto dysVdencMultiPassEnabled = m_basicFeature->m_dysVdencMultiPassEnabled; |
| 338 | if ((dysRefFrameFlags != DYS_REF_NONE) && dysVdencMultiPassEnabled) |
| 339 | { |
| 340 | secondLevelBatchBuffer.OsResource = m_resVdencDysPictureState2ndLevelBatchBuffer; |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | if (currPass >= 3) |
| 345 | { |
| 346 | ENCODE_ASSERTMESSAGE("Index exceeds the max number, when try to set vdenc second level batch buffer;"); |
| 347 | return MOS_STATUS_INVALID_PARAMETER; |
| 348 | } |
| 349 | |
| 350 | secondLevelBatchBuffer.OsResource = m_resVdencPictureState2ndLevelBatchBufferRead[currPass][m_vdencPictureState2ndLevelBBIndex]; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return MOS_STATUS_SUCCESS; |
| 355 | } |
| 356 | |
| 357 | MOS_STATUS Vp9EncodePak::GetVdencPictureState2ndLevelBatchBufferSize(uint32_t &size) |
| 358 | { |
| 359 | ENCODE_FUNC_CALL(); |
| 360 | |
| 361 | size = m_vdencPicStateSecondLevelBatchBufferSize; |
| 362 | |
| 363 | return MOS_STATUS_SUCCESS; |
| 364 | } |
| 365 | |
| 366 | MOS_STATUS Vp9EncodePak::UpdateParameters() |
| 367 | { |
| 368 | ENCODE_FUNC_CALL(); |
| 369 | ENCODE_CHK_NULL_RETURN(m_basicFeature); |
| 370 | |
| 371 | if (m_basicFeature->m_hucEnabled) |
| 372 | { |
| 373 | // We save the index of the 2nd level batch buffer in case is a pass that needs |
| 374 | m_lastVdencPictureState2ndLevelBBIndex = m_vdencPictureState2ndLevelBBIndex; |
| 375 | } |
| 376 | m_vdencPictureState2ndLevelBBIndex = (m_vdencPictureState2ndLevelBBIndex + 1) % CODECHAL_VP9_ENCODE_RECYCLED_BUFFER_NUM; |
| 377 | |
| 378 | return MOS_STATUS(); |
| 379 | } |
| 380 | |
| 381 | MOS_STATUS Vp9EncodePak::PakConstructPicStateBatchBuffer(PMOS_RESOURCE picStateBuffer) |
| 382 | { |
| 383 | ENCODE_FUNC_CALL(); |
| 384 | ENCODE_CHK_NULL_RETURN(picStateBuffer); |
| 385 | |
| 386 | uint8_t *data = (uint8_t *)m_allocator->LockResourceForWrite(picStateBuffer); |
| 387 | ENCODE_CHK_NULL_RETURN(data); |
| 388 | |
| 389 | for (int i = 0; i < CODECHAL_ENCODE_VP9_BRC_MAX_NUM_OF_PASSES; ++i) |
| 390 | { |
| 391 | MOS_COMMAND_BUFFER constructedCmdBuf; |
| 392 | |
| 393 | constructedCmdBuf.pCmdBase = (uint32_t *)data; |
| 394 | constructedCmdBuf.pCmdPtr = (uint32_t *)(data + i * CODECHAL_ENCODE_VP9_PIC_STATE_BUFFER_SIZE_PER_PASS); |
| 395 | constructedCmdBuf.iOffset = 0; |
| 396 | constructedCmdBuf.iRemaining = CODECHAL_ENCODE_VP9_PIC_STATE_BUFFER_SIZE_PER_PASS; |
| 397 | |
| 398 | SETPAR_AND_ADDCMD(HCP_VP9_PIC_STATE, m_hcpInterfaceNew, &constructedCmdBuf); |
| 399 | |
| 400 | // After adding pic state cmds in above function, pCmdPtr is not at the end of the picState buffer, so adjust it. |
| 401 | // -1 to go back one uint32_t where BB end will be added |
| 402 | constructedCmdBuf.pCmdPtr = (uint32_t *)(data + (i + 1) * CODECHAL_ENCODE_VP9_PIC_STATE_BUFFER_SIZE_PER_PASS) - 1; |
| 403 | |
| 404 | ENCODE_CHK_STATUS_RETURN(m_miItf->AddMiBatchBufferEnd(&constructedCmdBuf, nullptr)); |
| 405 | } |
| 406 | |
| 407 | m_allocator->UnLock(picStateBuffer); |
| 408 | |
| 409 | return MOS_STATUS_SUCCESS; |
| 410 | } |
| 411 | |
| 412 | PMOS_RESOURCE Vp9EncodePak::GetCompressedHeaderBuffer() |
| 413 | { |
| 414 | ENCODE_FUNC_CALL(); |
| 415 | |
| 416 | return &m_resCompressedHeaderBuffer; |
| 417 | } |
| 418 | |
| 419 | MOS_STATUS Vp9EncodePak::AllocateResources() |
| 420 | { |
| 421 | ENCODE_FUNC_CALL(); |
| 422 | ENCODE_CHK_NULL_RETURN(m_basicFeature); |
| 423 | |
| 424 | MOS_RESOURCE *allocatedBuffer = nullptr; |
| 425 | // Initiate allocation parameters and lock flags |
| 426 | MOS_ALLOC_GFXRES_PARAMS allocParamsForBufferLinear; |
| 427 | MOS_ZeroMemory(&allocParamsForBufferLinear, sizeof(MOS_ALLOC_GFXRES_PARAMS)); |
| 428 | allocParamsForBufferLinear.Type = MOS_GFXRES_BUFFER; |
| 429 | allocParamsForBufferLinear.TileType = MOS_TILE_LINEAR; |
| 430 | allocParamsForBufferLinear.Format = Format_Buffer; |
| 431 | |
| 432 | // VDENC picture second level batch buffer (Read/Write) |
| 433 | allocParamsForBufferLinear.dwBytes = MOS_ALIGN_CEIL(m_vdencPicStateSecondLevelBatchBufferSize, CODECHAL_PAGE_SIZE); |
| 434 | allocParamsForBufferLinear.pBufName = "VDENC Picture Second Level Batch Buffer Read"; |
| 435 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE; |
| 436 | for (auto i = 0; i < CODECHAL_VP9_ENCODE_RECYCLED_BUFFER_NUM; ++i) |
| 437 | { |
| 438 | for (auto j = 0; j < 3; ++j) |
| 439 | { |
| 440 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 441 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 442 | m_resVdencPictureState2ndLevelBatchBufferRead[j][i] = *allocatedBuffer; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | allocParamsForBufferLinear.pBufName = "VDENC Picture Second Level Batch Buffer Write"; |
| 447 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE; |
| 448 | for (auto i = 0; i < CODECHAL_VP9_ENCODE_RECYCLED_BUFFER_NUM; ++i) |
| 449 | { |
| 450 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 451 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 452 | m_resVdencPictureState2ndLevelBatchBufferWrite[i] = *allocatedBuffer; |
| 453 | } |
| 454 | |
| 455 | // Huc VP9 pak insert uncompressed header |
| 456 | allocParamsForBufferLinear.dwBytes = CODECHAL_ENCODE_VP9_PAK_INSERT_UNCOMPRESSED_HEADER; |
| 457 | allocParamsForBufferLinear.pBufName = "HucPakInsertUncompressedHeaderReadBuffer"; |
| 458 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE; |
| 459 | for (auto i = 0; i < CODECHAL_ENCODE_RECYCLED_BUFFER_NUM; ++i) |
| 460 | { |
| 461 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 462 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 463 | m_resHucPakInsertUncompressedHeaderReadBuffer[i] = *allocatedBuffer; |
| 464 | } |
| 465 | |
| 466 | allocParamsForBufferLinear.dwBytes = CODECHAL_ENCODE_VP9_PAK_INSERT_UNCOMPRESSED_HEADER; |
| 467 | allocParamsForBufferLinear.pBufName = "HucPakInsertUncompressedHeaderWriteBuffer"; |
| 468 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE; |
| 469 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 470 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 471 | m_resHucPakInsertUncompressedHeaderWriteBuffer = *allocatedBuffer; |
| 472 | |
| 473 | // Compressed header buffer |
| 474 | allocParamsForBufferLinear.dwBytes = 32 * CODECHAL_CACHELINE_SIZE; |
| 475 | allocParamsForBufferLinear.pBufName = "CompressedHeaderBuffer"; |
| 476 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ; |
| 477 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 478 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 479 | m_resCompressedHeaderBuffer = *allocatedBuffer; |
| 480 | |
| 481 | // Allocate data extension buffer |
| 482 | allocParamsForBufferLinear.dwBytes = CODECHAL_ENCODE_VP9_VDENC_DATA_EXTENSION_SIZE; |
| 483 | allocParamsForBufferLinear.pBufName = "DataExtensionBuffer"; |
| 484 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ; |
| 485 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 486 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 487 | m_resVdencDataExtensionBuffer = *allocatedBuffer; |
| 488 | |
| 489 | // Metadata line buffer |
| 490 | uint32_t size = m_basicFeature->m_maxPicWidthInSb * 5 * CODECHAL_CACHELINE_SIZE; |
| 491 | allocParamsForBufferLinear.dwBytes = size; |
| 492 | allocParamsForBufferLinear.pBufName = "MetadataLineBuffer"; |
| 493 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ; |
| 494 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 495 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 496 | m_resMetadataLineBuffer = allocatedBuffer; |
| 497 | |
| 498 | // Metadata tile line buffer |
| 499 | size = m_basicFeature->m_maxPicWidthInSb * 5 * CODECHAL_CACHELINE_SIZE; |
| 500 | allocParamsForBufferLinear.dwBytes = size; |
| 501 | allocParamsForBufferLinear.pBufName = "MetadataTileLineBuffer"; |
| 502 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ; |
| 503 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 504 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 505 | m_resMetadataTileLineBuffer = allocatedBuffer; |
| 506 | |
| 507 | // Metadata tile column buffer |
| 508 | size = m_basicFeature->m_maxPicHeightInSb * 5 * CODECHAL_CACHELINE_SIZE; |
| 509 | allocParamsForBufferLinear.dwBytes = size; |
| 510 | allocParamsForBufferLinear.pBufName = "MetadataTileColumnBuffer"; |
| 511 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ; |
| 512 | allocatedBuffer = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 513 | ENCODE_CHK_NULL_RETURN(allocatedBuffer); |
| 514 | m_resMetadataTileColumnBuffer = allocatedBuffer; |
| 515 | |
| 516 | return MOS_STATUS_SUCCESS; |
| 517 | } |
| 518 | |
| 519 | MOS_STATUS Vp9EncodePak::ConstructPakInsertObjBatchBuffer() |
| 520 | { |
| 521 | ENCODE_FUNC_CALL(); |
| 522 | |
| 523 | ENCODE_CHK_NULL_RETURN(m_hwInterface); |
| 524 | uint32_t nalUnitSize = m_basicFeature->m_nalUnitParams[0]->uiSize; |
| 525 | uint32_t nalUnitOffset = m_basicFeature->m_nalUnitParams[0]->uiOffset; |
| 526 | ENCODE_ASSERT(nalUnitSize > 0 && nalUnitSize < CODECHAL_ENCODE_VP9_PAK_INSERT_UNCOMPRESSED_HEADER); |
| 527 | |
| 528 | uint8_t *data = (uint8_t *)m_allocator->LockResourceForWrite(&m_resHucPakInsertUncompressedHeaderReadBuffer[m_basicFeature->m_currRecycledBufIdx]); |
| 529 | ENCODE_CHK_NULL_RETURN(data); |
| 530 | |
| 531 | MOS_COMMAND_BUFFER constructedCmdBuf; |
| 532 | MOS_ZeroMemory(&constructedCmdBuf, sizeof(constructedCmdBuf)); |
| 533 | constructedCmdBuf.pCmdBase = (uint32_t *)data; |
| 534 | constructedCmdBuf.pCmdPtr = (uint32_t *)data; |
| 535 | constructedCmdBuf.iOffset = 0; |
| 536 | constructedCmdBuf.iRemaining = CODECHAL_ENCODE_VP9_PAK_INSERT_UNCOMPRESSED_HEADER; |
| 537 | |
| 538 | SETPAR_AND_ADDCMD(HCP_PAK_INSERT_OBJECT, m_hcpInterfaceNew, &constructedCmdBuf); |
| 539 | |
| 540 | uint32_t byteSize = (nalUnitSize * 8 + 7) >> 3; |
| 541 | |
| 542 | if (byteSize) |
| 543 | { |
| 544 | MHW_MI_CHK_NULL(&m_basicFeature->m_bsBuffer); |
| 545 | MHW_MI_CHK_NULL(m_basicFeature->m_bsBuffer.pBase); |
| 546 | uint8_t *data_bb = (uint8_t *)(m_basicFeature->m_bsBuffer.pBase + nalUnitOffset); |
| 547 | MHW_MI_CHK_STATUS(Mhw_AddCommandCmdOrBB(m_hwInterface->GetOsInterface(), &constructedCmdBuf, nullptr, data_bb, byteSize)); |
| 548 | } |
| 549 | |
| 550 | ENCODE_CHK_STATUS_RETURN(m_miItf->AddMiBatchBufferEnd(&constructedCmdBuf, nullptr)); |
| 551 | |
| 552 | ENCODE_CHK_STATUS_RETURN(m_allocator->UnLock(&m_resHucPakInsertUncompressedHeaderReadBuffer[m_basicFeature->m_currRecycledBufIdx])); |
| 553 | |
| 554 | return MOS_STATUS_SUCCESS; |
| 555 | } |
| 556 | |
| 557 | MOS_STATUS Vp9EncodePak::PreparePakObjAndCuRecord() |
| 558 | { |
| 559 | ENCODE_FUNC_CALL(); |
| 560 | |
| 561 | auto picWidthInSb = m_basicFeature->m_picWidthInSb; |
| 562 | auto picHeightInSb = m_basicFeature->m_picHeightInSb; |
| 563 | |
| 564 | // We use PAK to perform dynamic scaling for reference frame, basically if every CU is inter and skipped, the reconstructed picture will be |
| 565 | // the down scaled copy of reference frame. |
| 566 | // Here driver needs to prepare pak obj and cu record, since PAK has a limitation that input picture needs to be CU boundary aligned, |
| 567 | // and to simplify handling the boundary condition, we set each CU with size 8x8, inter and zero MV. |
| 568 | // Segment skip needs to be turned on also. |
| 569 | |
| 570 | auto oriFrameWidth = MOS_ALIGN_CEIL(m_basicFeature->m_oriFrameWidth, CODEC_VP9_MIN_BLOCK_WIDTH); |
| 571 | auto oriFrameHeight = MOS_ALIGN_CEIL(m_basicFeature->m_oriFrameHeight, CODEC_VP9_MIN_BLOCK_HEIGHT); |
| 572 | |
| 573 | uint32_t numCuLastSbCol = (oriFrameWidth / CODEC_VP9_MIN_BLOCK_WIDTH) - |
| 574 | (picWidthInSb - 1) * (CODEC_VP9_SUPER_BLOCK_WIDTH / CODEC_VP9_MIN_BLOCK_WIDTH); |
| 575 | uint32_t numCuLastSbRow = (oriFrameHeight / CODEC_VP9_MIN_BLOCK_HEIGHT) - |
| 576 | (picHeightInSb - 1) * (CODEC_VP9_SUPER_BLOCK_HEIGHT / CODEC_VP9_MIN_BLOCK_HEIGHT); |
| 577 | |
| 578 | auto data = (uint8_t *)m_allocator->LockResourceForWrite(m_basicFeature->m_resMbCodeBuffer); |
| 579 | ENCODE_CHK_NULL_RETURN(data); |
| 580 | |
| 581 | HcpPakObject *pakObjData = (HcpPakObject *)data; |
| 582 | CU_DATA * cuDataPtr = (CU_DATA *)(data + m_basicFeature->m_mvOffset); |
| 583 | |
| 584 | // Fill PAK object |
| 585 | HcpPakObject pakObj; |
| 586 | MOS_ZeroMemory(&pakObj, sizeof(pakObj)); |
| 587 | pakObj.DW0.Type = 0x03; |
| 588 | pakObj.DW0.Opcode = 0x27; |
| 589 | pakObj.DW0.SubOp = 0x35; |
| 590 | pakObj.DW0.DwordLength = 1; // Total 4 DW. But only 3 DW are actual fields |
| 591 | // DW0/DW1/DW2 excluded from this field as per spec definition |
| 592 | pakObj.DW1.Split_flag_level0 = 1; |
| 593 | pakObj.DW1.Split_flag_level1 = 0xF; |
| 594 | pakObj.DW1.Split_flag_level2_level1part0 = 0xF; |
| 595 | pakObj.DW1.Split_flag_level2_level1part1 = 0xF; |
| 596 | pakObj.DW1.Split_flag_level2_level1part2 = 0xF; |
| 597 | pakObj.DW1.Split_flag_level2_level1part3 = 0xF; |
| 598 | |
| 599 | // Fill CU data |
| 600 | CU_DATA cuData; |
| 601 | MOS_ZeroMemory(&cuData, sizeof(cuData)); |
| 602 | cuData.cu_size = 0; // 8x8 |
| 603 | cuData.cu_pred_mode0 = cuData.cu_pred_mode1 = 1; // Inter |
| 604 | cuData.refframe_part0_l0 = cuData.refframe_part1_l0 = m_basicFeature->m_ref.GetDysRefIndex(); |
| 605 | |
| 606 | for (uint32_t j = 0; j < picHeightInSb; ++j) |
| 607 | { |
| 608 | for (uint32_t i = 0; i < picWidthInSb; ++i) |
| 609 | { |
| 610 | if ((j == picHeightInSb - 1) && (i == picWidthInSb - 1)) |
| 611 | { |
| 612 | pakObj.DW1.CU_count_minus1 = numCuLastSbCol * numCuLastSbRow - 1; |
| 613 | pakObj.DW1.IsLastSBFrameflag = 1; |
| 614 | pakObj.DW1.IsLastSBTileflag = 1; |
| 615 | pakObj.Reserved_DW03 = 0x05000000; // add batch buffer end flag |
| 616 | } |
| 617 | else if (i == picWidthInSb - 1) |
| 618 | { |
| 619 | pakObj.DW1.CU_count_minus1 = numCuLastSbCol * 8 - 1; |
| 620 | } |
| 621 | else if (j == picHeightInSb - 1) |
| 622 | { |
| 623 | pakObj.DW1.CU_count_minus1 = numCuLastSbRow * 8 - 1; |
| 624 | } |
| 625 | else |
| 626 | { |
| 627 | pakObj.DW1.CU_count_minus1 = 63; |
| 628 | } |
| 629 | |
| 630 | pakObj.DW2.Current_SB_X_Addr = i; |
| 631 | pakObj.DW2.Current_SB_Y_Addr = j; |
| 632 | |
| 633 | *pakObjData++ = pakObj; |
| 634 | |
| 635 | for (unsigned int cuIdx = 0; cuIdx < 64; cuIdx++) |
| 636 | { |
| 637 | *cuDataPtr++ = cuData; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | ENCODE_CHK_STATUS_RETURN(m_allocator->UnLock(m_basicFeature->m_resMbCodeBuffer)); |
| 642 | |
| 643 | return MOS_STATUS_SUCCESS; |
| 644 | } |
| 645 | |
| 646 | MOS_STATUS Vp9EncodePak::AddVdencCmd1Command(MOS_COMMAND_BUFFER &cmdBuffer) |
| 647 | { |
| 648 | ENCODE_FUNC_CALL(); |
| 649 | |
| 650 | ENCODE_CHK_NULL_RETURN(m_vdencInterfaceNew); |
| 651 | |
| 652 | ENCODE_CHK_NULL_RETURN(m_basicFeature); |
| 653 | ENCODE_CHK_NULL_RETURN(m_basicFeature->m_vp9PicParams); |
| 654 | |
| 655 | SETPAR_AND_ADDCMD(VDENC_CMD1, m_vdencInterfaceNew, &cmdBuffer); |
| 656 | |
| 657 | return MOS_STATUS_SUCCESS; |
| 658 | } |
| 659 | |
| 660 | MOS_STATUS Vp9EncodePak::AddVdencCmd2Command(MOS_COMMAND_BUFFER &cmdBuffer) |
| 661 | { |
| 662 | ENCODE_FUNC_CALL(); |
| 663 | |
| 664 | ENCODE_CHK_NULL_RETURN(m_vdencInterfaceNew); |
| 665 | |
| 666 | SETPAR_AND_ADDCMD(VDENC_CMD2, m_vdencInterfaceNew, &cmdBuffer); |
| 667 | |
| 668 | return MOS_STATUS_SUCCESS; |
| 669 | } |
| 670 | |
| 671 | void Vp9EncodePak::CalculateQpLambdaValues(double &sadQpLambda, double &rdQpLambda) |
| 672 | { |
| 673 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature->m_vp9PicParams); |
| 674 | |
| 675 | auto qp = m_basicFeature->m_vp9PicParams->LumaACQIndex; |
| 676 | auto vp9FrameType = m_basicFeature->m_vp9PicParams->PicFlags.fields.frame_type; |
| 677 | double QPScale = (vp9FrameType == CODEC_VP9_KEY_FRAME) ? 0.31 : 0.33; |
| 678 | double lambda = QPScale * CODECHAL_VP9_QUANT_AC[qp] / 8; |
| 679 | |
| 680 | sadQpLambda = lambda * 4 + 0.5; |
| 681 | rdQpLambda = lambda * lambda * 4 + 0.5; |
| 682 | } |
| 683 | |
| 684 | MHW_SETPAR_DECL_SRC(HCP_PAK_INSERT_OBJECT, Vp9EncodePak) |
| 685 | { |
| 686 | ENCODE_FUNC_CALL(); |
| 687 | |
| 688 | uint32_t nalUnitSize = m_basicFeature->m_nalUnitParams[0]->uiSize; |
| 689 | ENCODE_ASSERT(nalUnitSize > 0 && nalUnitSize < CODECHAL_ENCODE_VP9_PAK_INSERT_UNCOMPRESSED_HEADER); |
| 690 | |
| 691 | uint32_t byteSize = (nalUnitSize * 8 + 7) >> 3; |
| 692 | uint32_t dataBitsInLastDw = nalUnitSize * 8 % 32; |
| 693 | if (dataBitsInLastDw == 0) |
| 694 | { |
| 695 | dataBitsInLastDw = 32; |
| 696 | } |
| 697 | |
| 698 | params.dwPadding = (MOS_ALIGN_CEIL(byteSize, sizeof(uint32_t))) / sizeof(uint32_t); |
| 699 | |
| 700 | params.bEmulationByteBitsInsert = false; |
| 701 | params.uiSkipEmulationCheckCount = m_basicFeature->m_nalUnitParams[0]->uiSkipEmulationCheckCount; |
| 702 | params.dataBitsInLastDw = dataBitsInLastDw; |
| 703 | params.bEndOfSlice = false; |
| 704 | params.bLastHeader = true; |
| 705 | |
| 706 | return MOS_STATUS_SUCCESS; |
| 707 | } |
| 708 | |
| 709 | MHW_SETPAR_DECL_SRC(HCP_IND_OBJ_BASE_ADDR_STATE, Vp9EncodePak) |
| 710 | { |
| 711 | ENCODE_FUNC_CALL(); |
| 712 | |
| 713 | params.presCompressedHeaderBuffer = const_cast<PMOS_RESOURCE>(&m_resCompressedHeaderBuffer); |
| 714 | params.dwCompressedHeaderSize = 32 * CODECHAL_CACHELINE_SIZE; |
| 715 | |
| 716 | return MOS_STATUS_SUCCESS; |
| 717 | } |
| 718 | |
| 719 | MHW_SETPAR_DECL_SRC(HCP_PIPE_BUF_ADDR_STATE, Vp9EncodePak) |
| 720 | { |
| 721 | ENCODE_FUNC_CALL(); |
| 722 | |
| 723 | params.presMetadataLineBuffer = m_resMetadataLineBuffer; |
| 724 | params.presMetadataTileLineBuffer = m_resMetadataTileLineBuffer; |
| 725 | params.presMetadataTileColumnBuffer = m_resMetadataTileColumnBuffer; |
| 726 | |
| 727 | return MOS_STATUS_SUCCESS; |
| 728 | } |
| 729 | |
| 730 | } // namespace encode |