| 1 | /* |
| 2 | * Copyright (c) 2019-2020, 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_hevc_tile.cpp |
| 24 | //! \brief Defines the common interface for hevc tile |
| 25 | //! |
| 26 | |
| 27 | #include "encode_hevc_tile.h" |
| 28 | #include "encode_hevc_basic_feature.h" |
| 29 | #include "codec_def_common.h" |
| 30 | #include "encode_hevc_vdenc_feature_manager.h" |
| 31 | #include "encode_pipeline.h" |
| 32 | |
| 33 | namespace encode |
| 34 | { |
| 35 | HevcEncodeTile::HevcEncodeTile( |
| 36 | MediaFeatureManager *featureManager, |
| 37 | EncodeAllocator *allocator, |
| 38 | CodechalHwInterfaceNext *hwInterface, |
| 39 | void *constSettings) : |
| 40 | EncodeTile(featureManager, allocator, hwInterface, constSettings) |
| 41 | { |
| 42 | ENCODE_CHK_NULL_NO_STATUS_RETURN(hwInterface); |
| 43 | m_mosCtx = hwInterface->GetOsInterface()->pOsContext; |
| 44 | |
| 45 | auto encFeatureManager = dynamic_cast<EncodeHevcVdencFeatureManager *>(featureManager); |
| 46 | ENCODE_CHK_NULL_NO_STATUS_RETURN(encFeatureManager); |
| 47 | |
| 48 | m_basicFeature = dynamic_cast<EncodeBasicFeature *>(encFeatureManager->GetFeature(FeatureIDs::basicFeature)); |
| 49 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature); |
| 50 | |
| 51 | m_hcpItf = std::static_pointer_cast<mhw::vdbox::hcp::Itf>(hwInterface->GetHcpInterfaceNext()); |
| 52 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_hcpItf); |
| 53 | } |
| 54 | |
| 55 | MOS_STATUS HevcEncodeTile::Init(void* settings) |
| 56 | { |
| 57 | ENCODE_FUNC_CALL(); |
| 58 | ENCODE_CHK_NULL_RETURN(settings); |
| 59 | |
| 60 | ENCODE_CHK_STATUS_RETURN(EncodeTile::Init(settings)); |
| 61 | |
| 62 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 63 | // Tile Replay Enable should be passed from DDI, will change later when DDI is ready |
| 64 | MediaUserSetting::Value outValue; |
| 65 | ReadUserSetting( |
| 66 | m_userSettingPtr, |
| 67 | outValue, |
| 68 | "HEVC VDEnc TileReplay Enable", |
| 69 | MediaUserSetting::Group::Sequence); |
| 70 | m_enableTileReplay = outValue.Get<bool>(); |
| 71 | #else |
| 72 | m_enableTileReplay = false; |
| 73 | #endif |
| 74 | |
| 75 | return MOS_STATUS_SUCCESS; |
| 76 | } |
| 77 | |
| 78 | MOS_STATUS HevcEncodeTile::Update(void *params) |
| 79 | { |
| 80 | ENCODE_FUNC_CALL(); |
| 81 | ENCODE_CHK_NULL_RETURN(params); |
| 82 | |
| 83 | EncoderParams* encodeParams = (EncoderParams*)params;; |
| 84 | ENCODE_CHK_NULL_RETURN(encodeParams); |
| 85 | |
| 86 | PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams = |
| 87 | static_cast<PCODEC_HEVC_ENCODE_PICTURE_PARAMS>(encodeParams->pPicParams); |
| 88 | ENCODE_CHK_NULL_RETURN(hevcPicParams); |
| 89 | |
| 90 | m_enabled = hevcPicParams->tiles_enabled_flag ? true : false; |
| 91 | |
| 92 | |
| 93 | m_maxTileNumber = CODECHAL_GET_WIDTH_IN_BLOCKS(m_basicFeature->m_frameWidth, CODECHAL_HEVC_MIN_TILE_SIZE) * |
| 94 | CODECHAL_GET_HEIGHT_IN_BLOCKS(m_basicFeature->m_frameHeight, CODECHAL_HEVC_MIN_TILE_SIZE); |
| 95 | |
| 96 | ENCODE_CHK_STATUS_RETURN(EncodeTile::Update(params)); |
| 97 | |
| 98 | return MOS_STATUS_SUCCESS; |
| 99 | } |
| 100 | |
| 101 | bool HevcEncodeTile::IsLcuInTile( |
| 102 | uint32_t lcuX, |
| 103 | uint32_t lcuY, |
| 104 | EncodeTileData *currentTile) |
| 105 | { |
| 106 | ENCODE_FUNC_CALL(); |
| 107 | |
| 108 | if (m_basicFeature == nullptr) |
| 109 | { |
| 110 | ENCODE_ASSERTMESSAGE("Null basic feature, unexpected!"); |
| 111 | return false; |
| 112 | } |
| 113 | auto hevcBasicFeature = dynamic_cast<HevcBasicFeature *>(m_basicFeature); |
| 114 | if (hevcBasicFeature == nullptr) |
| 115 | { |
| 116 | ENCODE_ASSERTMESSAGE("Null hevc basic feature, unexpected!"); |
| 117 | return false; |
| 118 | } |
| 119 | auto hevcSeqParams = hevcBasicFeature->m_hevcSeqParams; |
| 120 | if (hevcSeqParams == nullptr) |
| 121 | { |
| 122 | ENCODE_ASSERTMESSAGE("Null seq parameters, unexpected!"); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | uint32_t shift = hevcSeqParams->log2_max_coding_block_size_minus3 - hevcSeqParams->log2_min_coding_block_size_minus3; |
| 127 | uint32_t residual = (1 << shift) - 1; |
| 128 | uint32_t tileColumnWidth = (currentTile->tileWidthInMinCbMinus1 + 1 + residual) >> shift; |
| 129 | uint32_t tileRowHeight = (currentTile->tileHeightInMinCbMinus1 + 1 + residual) >> shift; |
| 130 | |
| 131 | return (lcuX < currentTile->tileStartXInLCU || |
| 132 | lcuY < currentTile->tileStartYInLCU || |
| 133 | lcuX >= currentTile->tileStartXInLCU + tileColumnWidth || |
| 134 | lcuY >= currentTile->tileStartYInLCU + tileRowHeight); |
| 135 | } |
| 136 | |
| 137 | MOS_STATUS HevcEncodeTile::IsSliceInTile( |
| 138 | uint32_t sliceNumber, |
| 139 | EncodeTileData *currentTile, |
| 140 | bool * sliceInTile, |
| 141 | bool * lastSliceInTile) |
| 142 | { |
| 143 | ENCODE_FUNC_CALL(); |
| 144 | MOS_STATUS eStatus = MOS_STATUS_SUCCESS; |
| 145 | |
| 146 | ENCODE_CHK_NULL_RETURN(currentTile); |
| 147 | ENCODE_CHK_NULL_RETURN(sliceInTile); |
| 148 | ENCODE_CHK_NULL_RETURN(lastSliceInTile); |
| 149 | ENCODE_CHK_NULL_RETURN(m_basicFeature); |
| 150 | auto tmpHevcBasicFeature = dynamic_cast<HevcBasicFeature *>(m_basicFeature); |
| 151 | ENCODE_CHK_NULL_RETURN(tmpHevcBasicFeature); |
| 152 | |
| 153 | auto hevcPicParams = tmpHevcBasicFeature->m_hevcPicParams; |
| 154 | auto hevcSeqParams = tmpHevcBasicFeature->m_hevcSeqParams; |
| 155 | auto hevcSliceParams = tmpHevcBasicFeature->m_hevcSliceParams; |
| 156 | ENCODE_CHK_NULL_RETURN(hevcPicParams); |
| 157 | ENCODE_CHK_NULL_RETURN(hevcSeqParams); |
| 158 | ENCODE_CHK_NULL_RETURN(hevcSliceParams); |
| 159 | |
| 160 | if (!m_enabled) |
| 161 | { |
| 162 | *lastSliceInTile = *sliceInTile = true; |
| 163 | return eStatus; |
| 164 | } |
| 165 | |
| 166 | uint32_t shift = hevcSeqParams->log2_max_coding_block_size_minus3 - hevcSeqParams->log2_min_coding_block_size_minus3; |
| 167 | uint32_t residual = (1 << shift) - 1; |
| 168 | uint32_t frameWidthInLCU = (hevcSeqParams->wFrameWidthInMinCbMinus1 + 1 + residual) >> shift; |
| 169 | uint32_t frameHeightInLCU = (hevcSeqParams->wFrameHeightInMinCbMinus1 + 1 + residual) >> shift; |
| 170 | |
| 171 | uint32_t tileColumnWidth = (currentTile->tileWidthInMinCbMinus1 + 1 + residual) >> shift; |
| 172 | uint32_t tileRowHeight = (currentTile->tileHeightInMinCbMinus1 + 1 + residual) >> shift; |
| 173 | |
| 174 | const CODEC_HEVC_ENCODE_SLICE_PARAMS *hevcSlcParams = &hevcSliceParams[sliceNumber]; |
| 175 | uint32_t sliceStartLCU = hevcSlcParams->slice_segment_address; |
| 176 | uint32_t sliceLCUx = sliceStartLCU % frameWidthInLCU; |
| 177 | uint32_t sliceLCUy = sliceStartLCU / frameWidthInLCU; |
| 178 | |
| 179 | if (IsLcuInTile(sliceLCUx, sliceLCUy, currentTile)) |
| 180 | { |
| 181 | // slice start is not in the tile boundary |
| 182 | *lastSliceInTile = *sliceInTile = false; |
| 183 | return eStatus; |
| 184 | } |
| 185 | |
| 186 | sliceLCUx += (hevcSlcParams->NumLCUsInSlice - 1) % tileColumnWidth; |
| 187 | sliceLCUy += (hevcSlcParams->NumLCUsInSlice - 1) / tileColumnWidth; |
| 188 | |
| 189 | if (sliceLCUx >= currentTile->tileStartXInLCU + tileColumnWidth) |
| 190 | { |
| 191 | sliceLCUx -= tileColumnWidth; |
| 192 | sliceLCUy++; |
| 193 | } |
| 194 | |
| 195 | if (IsLcuInTile(sliceLCUx, sliceLCUy, currentTile)) |
| 196 | { |
| 197 | // last LCU of the slice is out of the tile boundary |
| 198 | *lastSliceInTile = *sliceInTile = false; |
| 199 | return eStatus; |
| 200 | } |
| 201 | |
| 202 | *sliceInTile = true; |
| 203 | |
| 204 | sliceLCUx++; |
| 205 | sliceLCUy++; |
| 206 | |
| 207 | // the end of slice is at the boundary of tile |
| 208 | *lastSliceInTile = (sliceLCUx == currentTile->tileStartXInLCU + tileColumnWidth && |
| 209 | sliceLCUy == currentTile->tileStartYInLCU + tileRowHeight); |
| 210 | |
| 211 | return eStatus; |
| 212 | } |
| 213 | |
| 214 | MOS_STATUS HevcEncodeTile::SetPipeNumber(uint32_t numPipes) |
| 215 | { |
| 216 | ENCODE_FUNC_CALL(); |
| 217 | |
| 218 | m_curTileCodingParams.NumberOfActiveBePipes = numPipes; |
| 219 | |
| 220 | return MOS_STATUS_SUCCESS; |
| 221 | } |
| 222 | |
| 223 | MOS_STATUS HevcEncodeTile::SetCurrentTileFromSliceIndex( |
| 224 | uint32_t slcCount, |
| 225 | EncodePipeline *pipeline) |
| 226 | { |
| 227 | ENCODE_FUNC_CALL(); |
| 228 | |
| 229 | bool lastSliceInTile = false, sliceInTile = false; |
| 230 | |
| 231 | EncodeTileData curTileData = {}; |
| 232 | |
| 233 | // Find the tileData corresponding to this slice |
| 234 | for (uint32_t tileRow = 0; tileRow < m_numTileRows; tileRow++) |
| 235 | { |
| 236 | for (uint32_t tileCol = 0; tileCol < m_numTileColumns; tileCol++) |
| 237 | { |
| 238 | ENCODE_CHK_STATUS_RETURN(SetCurrentTile(tileRow, tileCol, pipeline)); |
| 239 | ENCODE_CHK_STATUS_RETURN(GetCurrentTile(curTileData)); |
| 240 | ENCODE_CHK_STATUS_RETURN(IsSliceInTile(slcCount, &curTileData, &sliceInTile, &lastSliceInTile)); |
| 241 | |
| 242 | if (sliceInTile) |
| 243 | { |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | if (sliceInTile) |
| 248 | { |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return MOS_STATUS_SUCCESS; |
| 254 | } |
| 255 | |
| 256 | MOS_STATUS HevcEncodeTile::CalculateNumLcuByTiles(PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams) |
| 257 | { |
| 258 | ENCODE_FUNC_CALL(); |
| 259 | |
| 260 | ENCODE_CHK_NULL_RETURN(hevcPicParams); |
| 261 | |
| 262 | m_numLcuInPic = 0; |
| 263 | for (uint32_t numLcusInTiles = 0, i = 0; i < m_numTileRows; i++) |
| 264 | { |
| 265 | for (uint32_t j = 0; j < m_numTileColumns; j++) |
| 266 | { |
| 267 | m_numLcuInPic += hevcPicParams->tile_row_height[i] * hevcPicParams->tile_column_width[j]; |
| 268 | } |
| 269 | } |
| 270 | if (m_numLcuInPic == 0) |
| 271 | { |
| 272 | ENCODE_ASSERTMESSAGE("LCU num cal by each tile is zero, sth must be wrong!"); |
| 273 | return MOS_STATUS_INVALID_PARAMETER; |
| 274 | } |
| 275 | return MOS_STATUS_SUCCESS; |
| 276 | } |
| 277 | |
| 278 | MOS_STATUS HevcEncodeTile::CalculateTilesBoundary( |
| 279 | PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams, |
| 280 | uint32_t *rowBd, |
| 281 | uint32_t *colBd) |
| 282 | { |
| 283 | ENCODE_FUNC_CALL(); |
| 284 | |
| 285 | ENCODE_CHK_NULL_RETURN(hevcPicParams); |
| 286 | ENCODE_CHK_NULL_RETURN(rowBd); |
| 287 | ENCODE_CHK_NULL_RETURN(colBd); |
| 288 | |
| 289 | for (uint32_t i = 0; i < m_numTileColumns; i++) |
| 290 | { |
| 291 | colBd[i + 1] = colBd[i] + hevcPicParams->tile_column_width[i]; |
| 292 | } |
| 293 | |
| 294 | for (uint32_t i = 0; i < m_numTileRows; i++) |
| 295 | { |
| 296 | rowBd[i + 1] = rowBd[i] + hevcPicParams->tile_row_height[i]; |
| 297 | } |
| 298 | |
| 299 | return MOS_STATUS_SUCCESS; |
| 300 | |
| 301 | } |
| 302 | |
| 303 | MOS_STATUS HevcEncodeTile::CalculateTileWidthAndHeight( |
| 304 | PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams, |
| 305 | PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS hevcSeqParams, |
| 306 | uint32_t rowIndex, |
| 307 | uint32_t colIndex, |
| 308 | uint32_t *rowBd, |
| 309 | uint32_t *colBd) |
| 310 | { |
| 311 | ENCODE_FUNC_CALL(); |
| 312 | |
| 313 | ENCODE_CHK_NULL_RETURN(hevcPicParams); |
| 314 | ENCODE_CHK_NULL_RETURN(hevcSeqParams); |
| 315 | ENCODE_CHK_NULL_RETURN(rowBd); |
| 316 | ENCODE_CHK_NULL_RETURN(colBd); |
| 317 | |
| 318 | uint32_t idx = rowIndex * m_numTileColumns + colIndex; |
| 319 | int32_t frameWidthInMinCb = hevcSeqParams->wFrameWidthInMinCbMinus1 + 1; |
| 320 | int32_t frameHeightInMinCb = hevcSeqParams->wFrameHeightInMinCbMinus1 + 1; |
| 321 | int32_t shift = hevcSeqParams->log2_max_coding_block_size_minus3 - hevcSeqParams->log2_min_coding_block_size_minus3; |
| 322 | |
| 323 | if (colIndex != m_numTileColumns - 1) |
| 324 | { |
| 325 | m_tileData[idx].tileWidthInMinCbMinus1 = (hevcPicParams->tile_column_width[colIndex] << shift) - 1; |
| 326 | m_tileData[idx].isLastTileofRow = false; |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | m_tileData[idx].tileWidthInMinCbMinus1 = (frameWidthInMinCb - (colBd[colIndex] << shift)) - 1; |
| 331 | m_tileData[idx].isLastTileofRow = true; |
| 332 | } |
| 333 | |
| 334 | if (rowIndex != m_numTileRows - 1) |
| 335 | { |
| 336 | m_tileData[idx].isLastTileofColumn = false; |
| 337 | m_tileData[idx].tileHeightInMinCbMinus1 = (hevcPicParams->tile_row_height[rowIndex] << shift) - 1; |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | m_tileData[idx].tileHeightInMinCbMinus1 = (frameHeightInMinCb - (rowBd[rowIndex] << shift)) - 1; |
| 342 | m_tileData[idx].isLastTileofColumn = true; |
| 343 | } |
| 344 | |
| 345 | return MOS_STATUS_SUCCESS; |
| 346 | } |
| 347 | |
| 348 | MOS_STATUS HevcEncodeTile::GetTileInfo( |
| 349 | uint32_t xPosition, |
| 350 | uint32_t yPosition, |
| 351 | uint32_t &tileStartLcuX, |
| 352 | uint32_t &tileEndLcuX, |
| 353 | uint32_t &tileStartLcuY, |
| 354 | uint32_t &tileEndLcuY, |
| 355 | uint32_t &tileStreaminOffset) |
| 356 | { |
| 357 | ENCODE_FUNC_CALL(); |
| 358 | MOS_STATUS eStatus = MOS_STATUS_INVALID_PARAMETER; |
| 359 | |
| 360 | for (uint32_t i = 0; i < m_numTiles; i++) |
| 361 | { |
| 362 | tileStartLcuX = m_tileData[i].tileStartXInLCU; |
| 363 | tileStartLcuY = m_tileData[i].tileStartYInLCU; |
| 364 | tileEndLcuX = m_tileData[i].tileEndXInLCU; |
| 365 | tileEndLcuY = m_tileData[i].tileEndYInLCU; |
| 366 | |
| 367 | tileStreaminOffset = m_tileData[i].tileStreaminOffset; |
| 368 | |
| 369 | if ((xPosition >= (tileStartLcuX * 2)) && |
| 370 | (yPosition >= (tileStartLcuY * 2)) && |
| 371 | (xPosition < (tileEndLcuX * 2)) && |
| 372 | (yPosition < (tileEndLcuY * 2))) |
| 373 | { |
| 374 | eStatus = MOS_STATUS_SUCCESS; |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return eStatus; |
| 380 | } |
| 381 | |
| 382 | MOS_STATUS HevcEncodeTile::SetTileData(void *params) |
| 383 | { |
| 384 | ENCODE_FUNC_CALL(); |
| 385 | |
| 386 | MOS_STATUS eStatus = MOS_STATUS_SUCCESS; |
| 387 | |
| 388 | if (!m_enabled) |
| 389 | { |
| 390 | return eStatus; |
| 391 | } |
| 392 | |
| 393 | ENCODE_CHK_NULL_RETURN(params); |
| 394 | |
| 395 | EncoderParams* encodeParams = (EncoderParams*)params;; |
| 396 | ENCODE_CHK_NULL_RETURN(encodeParams); |
| 397 | |
| 398 | PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams = |
| 399 | static_cast<PCODEC_HEVC_ENCODE_PICTURE_PARAMS>(encodeParams->pPicParams); |
| 400 | ENCODE_CHK_NULL_RETURN(hevcPicParams); |
| 401 | |
| 402 | PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS hevcSeqParams = |
| 403 | static_cast<PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS>(encodeParams->pSeqParams); |
| 404 | ENCODE_CHK_NULL_RETURN(hevcSeqParams); |
| 405 | |
| 406 | PCODEC_HEVC_ENCODE_SLICE_PARAMS hevcSliceParams = |
| 407 | static_cast<PCODEC_HEVC_ENCODE_SLICE_PARAMS>(encodeParams->pSliceParams); |
| 408 | ENCODE_CHK_NULL_RETURN(hevcSliceParams); |
| 409 | |
| 410 | m_numTileRows = hevcPicParams->num_tile_rows_minus1 + 1; |
| 411 | m_numTileColumns = hevcPicParams->num_tile_columns_minus1 + 1; |
| 412 | |
| 413 | uint32_t colBd[m_maxTileBdNum] = {0}; |
| 414 | uint32_t rowBd[m_maxTileBdNum] = {0}; |
| 415 | ENCODE_CHK_STATUS_RETURN(CalculateTilesBoundary(hevcPicParams, &rowBd[0], &colBd[0])); |
| 416 | |
| 417 | m_numTiles = m_numTileRows * m_numTileColumns; |
| 418 | if (m_numTiles > CODECHAL_GET_WIDTH_IN_BLOCKS(m_basicFeature->m_frameWidth, CODECHAL_HEVC_VDENC_MIN_TILE_WIDTH_SIZE) * |
| 419 | CODECHAL_GET_HEIGHT_IN_BLOCKS(m_basicFeature->m_frameHeight, CODECHAL_HEVC_VDENC_MIN_TILE_HEIGHT_SIZE)) |
| 420 | { |
| 421 | return MOS_STATUS_INVALID_PARAMETER; |
| 422 | } |
| 423 | |
| 424 | bool is10Bit = m_basicFeature->m_is10Bit; |
| 425 | |
| 426 | uint32_t const numCuRecordTab[] = {1, 4, 16, 64}; //LCU: 8x8->1, 16x16->4, 32x32->16, 64x64->64 |
| 427 | uint32_t numCuRecord = numCuRecordTab[MOS_MIN(3, hevcSeqParams->log2_max_coding_block_size_minus3)]; |
| 428 | uint32_t maxBytePerLCU = 1 << (hevcSeqParams->log2_max_coding_block_size_minus3 + 3); |
| 429 | maxBytePerLCU = maxBytePerLCU * maxBytePerLCU; // number of pixels per LCU |
| 430 | maxBytePerLCU = maxBytePerLCU * 3 / (is10Bit ? 1 : 2); //assume 4:2:0 format |
| 431 | uint32_t bitstreamByteOffset = 0, saoRowstoreOffset = 0, cuLevelStreamoutOffset = 0, sseRowstoreOffset = 0; |
| 432 | int32_t frameWidthInMinCb = hevcSeqParams->wFrameWidthInMinCbMinus1 + 1; |
| 433 | int32_t frameHeightInMinCb = hevcSeqParams->wFrameHeightInMinCbMinus1 + 1; |
| 434 | int32_t shift = hevcSeqParams->log2_max_coding_block_size_minus3 - hevcSeqParams->log2_min_coding_block_size_minus3; |
| 435 | uint32_t ctbSize = 1 << (hevcSeqParams->log2_max_coding_block_size_minus3 + 3); |
| 436 | uint32_t streamInWidthinLCU = MOS_ROUNDUP_DIVIDE((frameWidthInMinCb << (hevcSeqParams->log2_min_coding_block_size_minus3 + 3)), ctbSize); |
| 437 | uint32_t tileStartLCUAddr = 0; |
| 438 | |
| 439 | ENCODE_CHK_STATUS_RETURN(CalculateNumLcuByTiles(hevcPicParams)); |
| 440 | |
| 441 | uint64_t activeBitstreamSize = (uint64_t)m_basicFeature->m_bitstreamSize; |
| 442 | // There would be padding at the end of last tile in CBR, reserve dedicated part in the BS buf |
| 443 | if (hevcSeqParams->RateControlMethod == RATECONTROL_CBR) |
| 444 | { |
| 445 | // Assume max padding num < target frame size derived from target bit rate and frame rate |
| 446 | if (hevcSeqParams->FrameRate.Denominator == 0) |
| 447 | { |
| 448 | return MOS_STATUS_INVALID_PARAMETER; |
| 449 | } |
| 450 | uint32_t actualFrameRate = hevcSeqParams->FrameRate.Numerator / hevcSeqParams->FrameRate.Denominator; |
| 451 | uint64_t reservedPart = (uint64_t)hevcSeqParams->TargetBitRate / 8 / (uint64_t)actualFrameRate * 1024; |
| 452 | |
| 453 | if (reservedPart > activeBitstreamSize) |
| 454 | { |
| 455 | ENCODE_ASSERTMESSAGE("Frame size cal from target Bit rate is larger than BS buf! Issues in CBR paras!"); |
| 456 | return MOS_STATUS_INVALID_PARAMETER; |
| 457 | } |
| 458 | |
| 459 | // Capping the reserved part to 1/10 of bs buf size |
| 460 | if (reservedPart > activeBitstreamSize / 10) |
| 461 | { |
| 462 | reservedPart = activeBitstreamSize / 10; |
| 463 | } |
| 464 | |
| 465 | activeBitstreamSize -= reservedPart; |
| 466 | } |
| 467 | |
| 468 | for (uint32_t numLcusInTiles = 0, i = 0; i < m_numTileRows; i++) |
| 469 | { |
| 470 | for (uint32_t j = 0; j < m_numTileColumns; j++) |
| 471 | { |
| 472 | uint32_t idx = i * m_numTileColumns + j; |
| 473 | uint32_t numLcuInTile = hevcPicParams->tile_row_height[i] * hevcPicParams->tile_column_width[j]; |
| 474 | |
| 475 | m_tileData[idx].tileStartXInLCU = colBd[j]; |
| 476 | m_tileData[idx].tileStartYInLCU = rowBd[i]; |
| 477 | |
| 478 | m_tileData[idx].tileColumnStoreSelect = j % 2; |
| 479 | m_tileData[idx].tileRowStoreSelect = i % 2; |
| 480 | |
| 481 | ENCODE_CHK_STATUS_RETURN(CalculateTileWidthAndHeight(hevcPicParams, hevcSeqParams, i, j, &rowBd[0], &colBd[0])); |
| 482 | |
| 483 | m_tileData[idx].numOfTilesInFrame = m_numTiles; |
| 484 | m_tileData[idx].numOfTileColumnsInFrame = m_numTileColumns; |
| 485 | m_tileData[idx].cuRecordOffset = MOS_ALIGN_CEIL(((numCuRecord * numLcusInTiles) * m_hcpItf->GetEncCuRecordSize()), |
| 486 | CODECHAL_CACHELINE_SIZE) / CODECHAL_CACHELINE_SIZE; |
| 487 | |
| 488 | m_tileData[idx].pakTileStatisticsOffset = 9 * idx; |
| 489 | m_tileData[idx].tileSizeStreamoutOffset = idx; |
| 490 | m_tileData[idx].vp9ProbabilityCounterStreamoutOffset = 0; |
| 491 | |
| 492 | m_tileData[idx].cuLevelStreamoutOffset = cuLevelStreamoutOffset; |
| 493 | m_tileData[idx].sseRowstoreOffset = sseRowstoreOffset; |
| 494 | m_tileData[idx].bitstreamByteOffset = bitstreamByteOffset; |
| 495 | m_tileData[idx].saoRowstoreOffset = saoRowstoreOffset; |
| 496 | |
| 497 | uint32_t tileHeightInLCU = MOS_ROUNDUP_DIVIDE(((m_tileData[idx].tileHeightInMinCbMinus1 + 1) << (hevcSeqParams->log2_min_coding_block_size_minus3 + 3)), ctbSize); |
| 498 | uint32_t tileWidthInLCU = MOS_ROUNDUP_DIVIDE(((m_tileData[idx].tileWidthInMinCbMinus1 + 1) << (hevcSeqParams->log2_min_coding_block_size_minus3 + 3)), ctbSize); |
| 499 | |
| 500 | m_tileData[idx].tileEndXInLCU = m_tileData[idx].tileStartXInLCU + tileWidthInLCU; |
| 501 | m_tileData[idx].tileEndYInLCU = m_tileData[idx].tileStartYInLCU + tileHeightInLCU; |
| 502 | |
| 503 | //StreamIn data is 4 CLs per LCU |
| 504 | m_tileData[idx].tileStreaminOffset = 4 * (m_tileData[idx].tileStartYInLCU * streamInWidthinLCU + m_tileData[idx].tileStartXInLCU * tileHeightInLCU); |
| 505 | m_tileData[idx].sliceSizeStreamoutOffset = tileStartLCUAddr; |
| 506 | |
| 507 | tileStartLCUAddr += (tileWidthInLCU * tileHeightInLCU); |
| 508 | |
| 509 | cuLevelStreamoutOffset += (m_tileData[idx].tileWidthInMinCbMinus1 + 1) * (m_tileData[idx].tileHeightInMinCbMinus1 + 1) * 16 / CODECHAL_CACHELINE_SIZE; |
| 510 | sseRowstoreOffset += ((hevcPicParams->tile_column_width[j] + 3) * ((HevcBasicFeature*)m_basicFeature)->m_sizeOfSseSrcPixelRowStoreBufferPerLcu) / CODECHAL_CACHELINE_SIZE; |
| 511 | saoRowstoreOffset += (MOS_ALIGN_CEIL(hevcPicParams->tile_column_width[j], 4) * CODECHAL_HEVC_SAO_STRMOUT_SIZE_PERLCU) / CODECHAL_CACHELINE_SIZE; |
| 512 | |
| 513 | uint64_t totalSizeTemp = (uint64_t)activeBitstreamSize * (uint64_t)numLcuInTile; |
| 514 | uint32_t bitStreamSizePerTile = (uint32_t)(totalSizeTemp / (uint64_t)m_numLcuInPic) + ((totalSizeTemp % (uint64_t)m_numLcuInPic) ? 1 : 0); |
| 515 | bitstreamByteOffset += MOS_ALIGN_CEIL(bitStreamSizePerTile, CODECHAL_CACHELINE_SIZE) / CODECHAL_CACHELINE_SIZE; |
| 516 | |
| 517 | numLcusInTiles += numLcuInTile; |
| 518 | } |
| 519 | |
| 520 | // same row store buffer for different tile rows. |
| 521 | saoRowstoreOffset = 0; |
| 522 | sseRowstoreOffset = 0; |
| 523 | } |
| 524 | |
| 525 | return eStatus; |
| 526 | } |
| 527 | |
| 528 | MOS_STATUS HevcEncodeTile::AllocateTileStatistics(void *params) |
| 529 | { |
| 530 | ENCODE_FUNC_CALL(); |
| 531 | |
| 532 | MOS_STATUS eStatus = MOS_STATUS_SUCCESS; |
| 533 | |
| 534 | ENCODE_CHK_NULL_RETURN(params); |
| 535 | |
| 536 | if (!m_enabled) |
| 537 | { |
| 538 | return eStatus; |
| 539 | } |
| 540 | |
| 541 | EncoderParams *encodeParams = (EncoderParams *)params; |
| 542 | ENCODE_CHK_NULL_RETURN(encodeParams); |
| 543 | |
| 544 | PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams = |
| 545 | static_cast<PCODEC_HEVC_ENCODE_PICTURE_PARAMS>(encodeParams->pPicParams); |
| 546 | ENCODE_CHK_NULL_RETURN(hevcPicParams); |
| 547 | |
| 548 | auto num_tile_rows = hevcPicParams->num_tile_rows_minus1 + 1; |
| 549 | auto num_tile_columns = hevcPicParams->num_tile_columns_minus1 + 1; |
| 550 | auto num_tiles = num_tile_rows * num_tile_columns; |
| 551 | |
| 552 | uint32_t maxLcuSize = 64; |
| 553 | if (m_numLcuInPic == 0) |
| 554 | { |
| 555 | ENCODE_ASSERTMESSAGE("LCU num cal by each tile is zero, sth must be wrong!"); |
| 556 | return MOS_STATUS_INVALID_PARAMETER; |
| 557 | } |
| 558 | |
| 559 | MOS_ZeroMemory(&m_hevcFrameStatsOffset, sizeof(HevcTileStatusInfo)); |
| 560 | MOS_ZeroMemory(&m_hevcTileStatsOffset, sizeof(HevcTileStatusInfo)); |
| 561 | MOS_ZeroMemory(&m_hevcStatsSize, sizeof(HevcTileStatusInfo)); |
| 562 | |
| 563 | // Set the maximum size based on frame level statistics. |
| 564 | m_hevcStatsSize.tileSizeRecord = CODECHAL_CACHELINE_SIZE; |
| 565 | m_hevcStatsSize.hevcPakStatistics = EncodeBasicFeature::m_sizeOfHcpPakFrameStats; |
| 566 | m_hevcStatsSize.vdencStatistics = CODECHAL_HEVC_VDENC_STATS_SIZE; |
| 567 | m_hevcStatsSize.hevcSliceStreamout = CODECHAL_CACHELINE_SIZE; |
| 568 | |
| 569 | // Maintain the offsets to use for patching addresses in to the HuC Pak Integration kernel Aggregated Frame Statistics Output Buffer |
| 570 | // Each offset needs to be page aligned as the combined region is fed into different page aligned HuC regions |
| 571 | m_hevcFrameStatsOffset.tileSizeRecord = 0; // Tile Size Record is not present in resHuCPakAggregatedFrameStatsBuffer |
| 572 | m_hevcFrameStatsOffset.hevcPakStatistics = 0; |
| 573 | m_hevcFrameStatsOffset.vdencStatistics = MOS_ALIGN_CEIL(m_hevcFrameStatsOffset.hevcPakStatistics + m_hevcStatsSize.hevcPakStatistics, CODECHAL_PAGE_SIZE); |
| 574 | m_hevcFrameStatsOffset.hevcSliceStreamout = MOS_ALIGN_CEIL(m_hevcFrameStatsOffset.vdencStatistics + m_hevcStatsSize.vdencStatistics, CODECHAL_PAGE_SIZE); |
| 575 | |
| 576 | // Frame level statistics |
| 577 | m_hwInterface->m_pakIntAggregatedFrameStatsSize = MOS_ALIGN_CEIL(m_hevcFrameStatsOffset.hevcSliceStreamout + (m_hevcStatsSize.hevcSliceStreamout * m_numLcuInPic), CODECHAL_PAGE_SIZE); |
| 578 | |
| 579 | // HEVC Frame Statistics Buffer - Output from HuC PAK Integration kernel |
| 580 | if (Mos_ResourceIsNull(&m_resHuCPakAggregatedFrameStatsBuffer)) |
| 581 | { |
| 582 | MOS_ALLOC_GFXRES_PARAMS allocParamsForBufferLinear; |
| 583 | MOS_ZeroMemory(&allocParamsForBufferLinear, sizeof(MOS_ALLOC_GFXRES_PARAMS)); |
| 584 | allocParamsForBufferLinear.Type = MOS_GFXRES_BUFFER; |
| 585 | allocParamsForBufferLinear.TileType = MOS_TILE_LINEAR; |
| 586 | allocParamsForBufferLinear.Format = Format_Buffer; |
| 587 | allocParamsForBufferLinear.dwBytes = m_hwInterface->m_pakIntAggregatedFrameStatsSize; |
| 588 | allocParamsForBufferLinear.pBufName = "HCP Aggregated Frame Statistics Streamout Buffer"; |
| 589 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE; |
| 590 | auto resource = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 591 | ENCODE_CHK_NULL_RETURN(resource); |
| 592 | m_resHuCPakAggregatedFrameStatsBuffer = *resource; |
| 593 | } |
| 594 | |
| 595 | // Maintain the offsets to use for patching addresses in to the Tile Based Statistics Buffer |
| 596 | // Each offset needs to be page aligned as the combined region is fed into different page aligned HuC regions |
| 597 | m_hevcTileStatsOffset.tileSizeRecord = 0; // TileRecord is a separated resource |
| 598 | m_hevcTileStatsOffset.hevcPakStatistics = 0; // PakStatistics is head of m_resTileBasedStatisticsBuffer; |
| 599 | m_hevcTileStatsOffset.vdencStatistics = MOS_ALIGN_CEIL(m_hevcTileStatsOffset.hevcPakStatistics + (m_hevcStatsSize.hevcPakStatistics * num_tiles), CODECHAL_PAGE_SIZE); |
| 600 | m_hevcTileStatsOffset.hevcSliceStreamout = MOS_ALIGN_CEIL(m_hevcTileStatsOffset.vdencStatistics + (m_hevcStatsSize.vdencStatistics * num_tiles), CODECHAL_PAGE_SIZE); |
| 601 | // Combined statistics size for all tiles |
| 602 | m_hwInterface->m_pakIntTileStatsSize = MOS_ALIGN_CEIL(m_hevcTileStatsOffset.hevcSliceStreamout + m_hevcStatsSize.hevcSliceStreamout * m_numLcuInPic, CODECHAL_PAGE_SIZE); |
| 603 | |
| 604 | // Tile size record size for all tiles |
| 605 | m_hwInterface->m_tileRecordSize = m_hevcStatsSize.tileSizeRecord * num_tiles; |
| 606 | |
| 607 | MOS_SURFACE surface; |
| 608 | MOS_ZeroMemory(&surface, sizeof(surface)); |
| 609 | surface.OsResource = m_resTileBasedStatisticsBuffer[m_statisticsBufIndex]; |
| 610 | if (!Mos_ResourceIsNull(&surface.OsResource)) |
| 611 | { |
| 612 | m_allocator->GetSurfaceInfo(&surface); |
| 613 | } |
| 614 | uint32_t curPakIntTileStatsSize = surface.dwHeight * surface.dwWidth; |
| 615 | |
| 616 | if (Mos_ResourceIsNull(&m_resTileBasedStatisticsBuffer[m_statisticsBufIndex]) || |
| 617 | curPakIntTileStatsSize < m_hwInterface->m_pakIntTileStatsSize) |
| 618 | { |
| 619 | if (!Mos_ResourceIsNull(&m_resTileBasedStatisticsBuffer[m_statisticsBufIndex])) |
| 620 | { |
| 621 | m_allocator->DestroyResource(&m_resTileBasedStatisticsBuffer[m_statisticsBufIndex]); |
| 622 | } |
| 623 | MOS_ALLOC_GFXRES_PARAMS allocParamsForBufferLinear; |
| 624 | MOS_ZeroMemory(&allocParamsForBufferLinear, sizeof(MOS_ALLOC_GFXRES_PARAMS)); |
| 625 | allocParamsForBufferLinear.Type = MOS_GFXRES_BUFFER; |
| 626 | allocParamsForBufferLinear.TileType = MOS_TILE_LINEAR; |
| 627 | allocParamsForBufferLinear.Format = Format_Buffer; |
| 628 | allocParamsForBufferLinear.dwBytes = m_hwInterface->m_pakIntTileStatsSize; |
| 629 | allocParamsForBufferLinear.pBufName = "HCP Tile Level Statistics Streamout Buffer"; |
| 630 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_NOCACHE; |
| 631 | auto resource = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 632 | ENCODE_CHK_NULL_RETURN(resource); |
| 633 | m_resTileBasedStatisticsBuffer[m_statisticsBufIndex] = *resource; |
| 634 | } |
| 635 | |
| 636 | // Allocate the updated tile size buffer for PAK integration kernel |
| 637 | if (Mos_ResourceIsNull(&m_tileRecordBuffer[m_statisticsBufIndex])) |
| 638 | { |
| 639 | MOS_ALLOC_GFXRES_PARAMS allocParamsForBufferLinear; |
| 640 | MOS_ZeroMemory(&allocParamsForBufferLinear, sizeof(MOS_ALLOC_GFXRES_PARAMS)); |
| 641 | allocParamsForBufferLinear.Type = MOS_GFXRES_BUFFER; |
| 642 | allocParamsForBufferLinear.TileType = MOS_TILE_LINEAR; |
| 643 | allocParamsForBufferLinear.Format = Format_Buffer; |
| 644 | allocParamsForBufferLinear.dwBytes = CODECHAL_CACHELINE_SIZE * num_tiles; |
| 645 | allocParamsForBufferLinear.pBufName = "Tile Record Buffer"; |
| 646 | allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_NOCACHE; |
| 647 | |
| 648 | auto resource = m_allocator->AllocateResource(allocParamsForBufferLinear, true); |
| 649 | ENCODE_CHK_NULL_RETURN(resource); |
| 650 | m_tileRecordBuffer[m_statisticsBufIndex] = *resource; |
| 651 | } |
| 652 | |
| 653 | return eStatus; |
| 654 | } |
| 655 | |
| 656 | MOS_STATUS HevcEncodeTile::GetTileStatusInfo( |
| 657 | HevcTileStatusInfo &hevcTileStatsOffset, |
| 658 | HevcTileStatusInfo &hevcFrameStatsOffset, |
| 659 | HevcTileStatusInfo &hevcStatsSize) |
| 660 | { |
| 661 | ENCODE_FUNC_CALL(); |
| 662 | if (!m_enabled) |
| 663 | { |
| 664 | return MOS_STATUS_SUCCESS; |
| 665 | } |
| 666 | |
| 667 | hevcTileStatsOffset = m_hevcTileStatsOffset; |
| 668 | hevcFrameStatsOffset = m_hevcFrameStatsOffset; |
| 669 | hevcStatsSize = m_hevcStatsSize; |
| 670 | |
| 671 | return MOS_STATUS_SUCCESS; |
| 672 | } |
| 673 | |
| 674 | MOS_STATUS HevcEncodeTile::GetTileInfo(HevcTileInfo *hevcTileInfo) const |
| 675 | { |
| 676 | ENCODE_FUNC_CALL(); |
| 677 | ENCODE_CHK_NULL_RETURN(hevcTileInfo); |
| 678 | |
| 679 | if (m_enabled) |
| 680 | { |
| 681 | hevcTileInfo->tileId = static_cast<uint16_t>(m_tileIdx); // Tile number in a frame |
| 682 | |
| 683 | const auto currTileData = m_tileData[m_tileIdx]; |
| 684 | |
| 685 | hevcTileInfo->tileColPositionInSb = static_cast<uint16_t>(currTileData.tileStartX); |
| 686 | hevcTileInfo->tileRowPositionInSb = static_cast<uint16_t>(currTileData.tileStartY); |
| 687 | |
| 688 | hevcTileInfo->tileWidthInSbMinus1 = currTileData.tileEndX - currTileData.tileStartX - 1; |
| 689 | hevcTileInfo->tileHeightInSbMinus1 = currTileData.tileEndY - currTileData.tileStartY - 1; |
| 690 | |
| 691 | hevcTileInfo->tileStartXInLCU = currTileData.tileStartX; |
| 692 | hevcTileInfo->tileStartYInLCU = currTileData.tileStartY; |
| 693 | hevcTileInfo->tileEndXInLCU = currTileData.tileEndX; |
| 694 | hevcTileInfo->tileEndYInLCU = currTileData.tileEndY; |
| 695 | hevcTileInfo->tileNum = static_cast<uint16_t>(m_tileIdx); |
| 696 | } |
| 697 | return MOS_STATUS_SUCCESS; |
| 698 | } |
| 699 | |
| 700 | MHW_SETPAR_DECL_SRC(VDENC_PIPE_MODE_SELECT, HevcEncodeTile) |
| 701 | { |
| 702 | ENCODE_FUNC_CALL(); |
| 703 | if (m_enabled) |
| 704 | { |
| 705 | params.tileBasedReplayMode = m_enableTileReplay; |
| 706 | } |
| 707 | else |
| 708 | { |
| 709 | params.tileBasedReplayMode = 0; |
| 710 | } |
| 711 | return MOS_STATUS_SUCCESS; |
| 712 | } |
| 713 | |
| 714 | MHW_SETPAR_DECL_SRC(VDENC_PIPE_BUF_ADDR_STATE, HevcEncodeTile) |
| 715 | { |
| 716 | ENCODE_FUNC_CALL(); |
| 717 | |
| 718 | if (!m_enabled) |
| 719 | { |
| 720 | return MOS_STATUS_SUCCESS; |
| 721 | } |
| 722 | |
| 723 | MOS_RESOURCE *tileStatisticsBuffer = const_cast<MOS_RESOURCE *>(& m_resTileBasedStatisticsBuffer[m_statisticsBufIndex]); |
| 724 | |
| 725 | if (!Mos_ResourceIsNull(tileStatisticsBuffer)) |
| 726 | { |
| 727 | params.streamOutBuffer = tileStatisticsBuffer; |
| 728 | params.streamOutOffset = m_hevcTileStatsOffset.vdencStatistics; |
| 729 | } |
| 730 | |
| 731 | return MOS_STATUS_SUCCESS; |
| 732 | } |
| 733 | |
| 734 | MHW_SETPAR_DECL_SRC(VDENC_WALKER_STATE, HevcEncodeTile) |
| 735 | { |
| 736 | ENCODE_FUNC_CALL(); |
| 737 | auto hevcBasicFeature = dynamic_cast<HevcBasicFeature *>(m_basicFeature); |
| 738 | ENCODE_CHK_NULL_RETURN(hevcBasicFeature); |
| 739 | auto picParams = hevcBasicFeature->m_hevcPicParams; |
| 740 | ENCODE_CHK_NULL_RETURN(picParams); |
| 741 | auto seqParams = hevcBasicFeature->m_hevcSeqParams; |
| 742 | ENCODE_CHK_NULL_RETURN(seqParams); |
| 743 | auto t_sliceParams = hevcBasicFeature->m_hevcSliceParams; |
| 744 | ENCODE_CHK_NULL_RETURN(t_sliceParams); |
| 745 | CODEC_HEVC_ENCODE_SLICE_PARAMS *sliceParams = (CODEC_HEVC_ENCODE_SLICE_PARAMS *)&t_sliceParams[hevcBasicFeature->m_curNumSlices]; |
| 746 | |
| 747 | uint32_t ctbSize = 1 << (seqParams->log2_max_coding_block_size_minus3 + 3); |
| 748 | uint32_t widthInPix = (1 << (seqParams->log2_min_coding_block_size_minus3 + 3)) * (seqParams->wFrameWidthInMinCbMinus1 + 1); |
| 749 | uint32_t widthInCtb = (widthInPix / ctbSize) + ((widthInPix % ctbSize) ? 1 : 0); // round up |
| 750 | uint32_t heightInPix = (1 << (seqParams->log2_min_coding_block_size_minus3 + 3)) * (seqParams->wFrameHeightInMinCbMinus1 + 1); |
| 751 | uint32_t heightInCtb = (heightInPix / ctbSize) + ((heightInPix % ctbSize) ? 1 : 0); // round up |
| 752 | uint32_t shift = seqParams->log2_max_coding_block_size_minus3 - seqParams->log2_min_coding_block_size_minus3; |
| 753 | |
| 754 | if (!m_enabled) |
| 755 | { |
| 756 | params.firstSuperSlice = 0; |
| 757 | // No tiling support |
| 758 | params.tileSliceStartLcuMbY = sliceParams->slice_segment_address / widthInCtb; |
| 759 | params.nextTileSliceStartLcuMbX = (sliceParams->slice_segment_address + sliceParams->NumLCUsInSlice) / heightInCtb; |
| 760 | params.nextTileSliceStartLcuMbY = (sliceParams->slice_segment_address + sliceParams->NumLCUsInSlice) / widthInCtb; |
| 761 | } |
| 762 | else |
| 763 | { |
| 764 | params.tileSliceStartLcuMbX = (&m_curTileCodingParams)->TileStartLCUX; |
| 765 | params.tileSliceStartLcuMbY = (&m_curTileCodingParams)->TileStartLCUY; |
| 766 | |
| 767 | // In HEVC vdnec, always first super slice in each tile |
| 768 | params.firstSuperSlice = 1; |
| 769 | |
| 770 | params.nextTileSliceStartLcuMbX = (&m_curTileCodingParams)->TileStartLCUX + ((&m_curTileCodingParams)->TileWidthInMinCbMinus1 >> shift) + 1; |
| 771 | params.nextTileSliceStartLcuMbY = (&m_curTileCodingParams)->TileStartLCUY + ((&m_curTileCodingParams)->TileHeightInMinCbMinus1 >> shift) + 1; |
| 772 | } |
| 773 | |
| 774 | return MOS_STATUS_SUCCESS; |
| 775 | } |
| 776 | |
| 777 | MHW_SETPAR_DECL_SRC(HCP_PIPE_MODE_SELECT, HevcEncodeTile) |
| 778 | { |
| 779 | ENCODE_FUNC_CALL(); |
| 780 | |
| 781 | // In single pipe mode, if TileBasedReplayMode is enabled, |
| 782 | // the bit stream for each tile will not be continuous |
| 783 | if (m_enabled) |
| 784 | { |
| 785 | params.bTileBasedReplayMode = m_enableTileReplay; |
| 786 | } |
| 787 | else |
| 788 | { |
| 789 | params.bTileBasedReplayMode = 0; |
| 790 | } |
| 791 | |
| 792 | return MOS_STATUS_SUCCESS; |
| 793 | } |
| 794 | |
| 795 | MHW_SETPAR_DECL_SRC(HCP_TILE_CODING, HevcEncodeTile) |
| 796 | { |
| 797 | ENCODE_FUNC_CALL(); |
| 798 | if (!m_enabled) |
| 799 | { |
| 800 | return MOS_STATUS_SUCCESS; |
| 801 | } |
| 802 | |
| 803 | params.numOfTileColumnsInFrame = m_curTileCodingParams.NumOfTileColumnsInFrame; |
| 804 | params.tileRowStoreSelect = m_curTileCodingParams.TileRowStoreSelect; |
| 805 | params.tileColumnStoreSelect = m_curTileCodingParams.TileColumnStoreSelect; |
| 806 | params.tileStartLCUX = m_curTileCodingParams.TileStartLCUX; |
| 807 | params.tileStartLCUY = m_curTileCodingParams.TileStartLCUY; |
| 808 | params.isLastTileofColumn = m_curTileCodingParams.IsLastTileofColumn; |
| 809 | params.isLastTileofRow = m_curTileCodingParams.IsLastTileofRow; |
| 810 | params.tileHeightInMinCbMinus1 = m_curTileCodingParams.TileHeightInMinCbMinus1; |
| 811 | params.tileWidthInMinCbMinus1 = m_curTileCodingParams.TileWidthInMinCbMinus1; |
| 812 | params.cuRecordOffset = m_curTileCodingParams.CuRecordOffset; |
| 813 | params.bitstreamByteOffset = m_curTileCodingParams.BitstreamByteOffset; |
| 814 | params.pakTileStatisticsOffset = m_curTileCodingParams.PakTileStatisticsOffset; |
| 815 | params.cuLevelStreamoutOffset = m_curTileCodingParams.CuLevelStreamoutOffset; |
| 816 | params.sliceSizeStreamoutOffset = m_curTileCodingParams.SliceSizeStreamoutOffset; |
| 817 | params.sseRowstoreOffset = m_curTileCodingParams.SseRowstoreOffset; |
| 818 | params.saoRowstoreOffset = m_curTileCodingParams.SaoRowstoreOffset; |
| 819 | params.tileSizeStreamoutOffset = m_curTileCodingParams.TileSizeStreamoutOffset; |
| 820 | params.vp9ProbabilityCounterStreamoutOffset = 0; |
| 821 | params.nonFirstPassTile = m_curTileCodingParams.bTileReplayEnable && (!m_curTileCodingParams.IsFirstPass); |
| 822 | params.bitstreamByteOffsetEnable = m_curTileCodingParams.bTileReplayEnable; |
| 823 | |
| 824 | return MOS_STATUS_SUCCESS; |
| 825 | } |
| 826 | |
| 827 | MHW_SETPAR_DECL_SRC(HCP_PIPE_BUF_ADDR_STATE, HevcEncodeTile) |
| 828 | { |
| 829 | ENCODE_FUNC_CALL(); |
| 830 | |
| 831 | if (m_enabled && m_curTileCodingParams.NumberOfActiveBePipes > 1) |
| 832 | { |
| 833 | MOS_RESOURCE *tileStatisticsBuffer = const_cast<PMOS_RESOURCE>(&m_resTileBasedStatisticsBuffer[m_statisticsBufIndex]); |
| 834 | if (!Mos_ResourceIsNull(tileStatisticsBuffer)) |
| 835 | { |
| 836 | params.presLcuBaseAddressBuffer = tileStatisticsBuffer; |
| 837 | params.dwLcuStreamOutOffset = m_hevcTileStatsOffset.hevcSliceStreamout; |
| 838 | params.presFrameStatStreamOutBuffer = tileStatisticsBuffer; |
| 839 | params.dwFrameStatStreamOutOffset = m_hevcTileStatsOffset.hevcPakStatistics; |
| 840 | } |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | params.presLcuBaseAddressBuffer = m_basicFeature->m_recycleBuf->GetBuffer(LcuBaseAddressBuffer, 0); |
| 845 | params.presFrameStatStreamOutBuffer = m_basicFeature->m_recycleBuf->GetBuffer(FrameStatStreamOutBuffer, 0); |
| 846 | } |
| 847 | |
| 848 | return MOS_STATUS_SUCCESS; |
| 849 | } |
| 850 | |
| 851 | MHW_SETPAR_DECL_SRC(HCP_SLICE_STATE, HevcEncodeTile) |
| 852 | { |
| 853 | ENCODE_FUNC_CALL(); |
| 854 | |
| 855 | if (!m_enabled) |
| 856 | { |
| 857 | return MOS_STATUS_SUCCESS; |
| 858 | } |
| 859 | |
| 860 | auto hevcFeature = dynamic_cast<HevcBasicFeature *>(m_basicFeature); |
| 861 | ENCODE_CHK_NULL_RETURN(hevcFeature); |
| 862 | |
| 863 | params.lastSliceInTile = hevcFeature->m_lastSliceInTile; |
| 864 | params.lastSliceInTileColumn = (hevcFeature->m_lastSliceInTile & m_tileData[m_tileIdx].isLastTileofColumn) ? true : false; |
| 865 | |
| 866 | return MOS_STATUS_SUCCESS; |
| 867 | } |
| 868 | |
| 869 | MHW_SETPAR_DECL_SRC(HCP_IND_OBJ_BASE_ADDR_STATE, HevcEncodeTile) |
| 870 | { |
| 871 | ENCODE_FUNC_CALL(); |
| 872 | |
| 873 | if (!m_enabled) |
| 874 | { |
| 875 | return MOS_STATUS_SUCCESS; |
| 876 | } |
| 877 | |
| 878 | MOS_RESOURCE *tileRecordBuffer = const_cast<PMOS_RESOURCE>(&m_tileRecordBuffer[m_statisticsBufIndex]); |
| 879 | if (!Mos_ResourceIsNull(tileRecordBuffer)) |
| 880 | { |
| 881 | params.presPakTileSizeStasBuffer = tileRecordBuffer; |
| 882 | params.dwPakTileSizeStasBufferSize = m_hwInterface->m_tileRecordSize; |
| 883 | params.dwPakTileSizeRecordOffset = m_hevcTileStatsOffset.tileSizeRecord; |
| 884 | } |
| 885 | |
| 886 | return MOS_STATUS_SUCCESS; |
| 887 | } |
| 888 | |
| 889 | MHW_SETPAR_DECL_SRC(VDENC_HEVC_VP9_TILE_SLICE_STATE, HevcEncodeTile) |
| 890 | { |
| 891 | ENCODE_FUNC_CALL(); |
| 892 | auto hevcBasicFeature = dynamic_cast<HevcBasicFeature *>(m_basicFeature); |
| 893 | ENCODE_CHK_NULL_RETURN(hevcBasicFeature); |
| 894 | auto picParams = hevcBasicFeature->m_hevcPicParams; |
| 895 | ENCODE_CHK_NULL_RETURN(picParams); |
| 896 | auto seqParams = hevcBasicFeature->m_hevcSeqParams; |
| 897 | ENCODE_CHK_NULL_RETURN(seqParams); |
| 898 | auto sliceParams = hevcBasicFeature->m_hevcSliceParams; |
| 899 | ENCODE_CHK_NULL_RETURN(sliceParams); |
| 900 | |
| 901 | uint32_t ctbSize = 1 << (seqParams->log2_max_coding_block_size_minus3 + 3); |
| 902 | uint32_t widthInPix = (1 << (seqParams->log2_min_coding_block_size_minus3 + 3)) * (seqParams->wFrameWidthInMinCbMinus1 + 1); |
| 903 | uint32_t heightInPix = (1 << (seqParams->log2_min_coding_block_size_minus3 + 3)) * (seqParams->wFrameHeightInMinCbMinus1 + 1); |
| 904 | uint32_t minCodingBlkSize = seqParams->log2_min_coding_block_size_minus3 + 3; |
| 905 | params.ctbSize = ctbSize; |
| 906 | |
| 907 | if (!m_enabled) |
| 908 | { |
| 909 | // No tiling support |
| 910 | params.tileWidth = widthInPix; |
| 911 | params.tileHeight = heightInPix; |
| 912 | } |
| 913 | else |
| 914 | { |
| 915 | params.tileStartLCUX = (&m_curTileCodingParams)->TileStartLCUX; |
| 916 | params.tileStartLCUY = (&m_curTileCodingParams)->TileStartLCUY; |
| 917 | |
| 918 | params.tileWidth = (((&m_curTileCodingParams)->TileWidthInMinCbMinus1 + 1) << minCodingBlkSize); |
| 919 | params.tileHeight = (((&m_curTileCodingParams)->TileHeightInMinCbMinus1 + 1) << minCodingBlkSize); |
| 920 | |
| 921 | // NumParEngine is not used by HW |
| 922 | //cmd.DW3.NumParEngine = tileSlcParams->dwNumberOfPipes; |
| 923 | |
| 924 | params.tileId = m_tileIdx; |
| 925 | params.tileRowStoreSelect = (&m_curTileCodingParams)->TileRowStoreSelect; |
| 926 | params.tileEnable = 1; |
| 927 | params.tileStreamInOffset = (&m_curTileCodingParams)->TileStreaminOffset; |
| 928 | |
| 929 | // PAK Object StreamOut Offset Computation |
| 930 | uint32_t tileLCUStreamOutByteOffset = 0; |
| 931 | if ((&m_curTileCodingParams)->TileStartLCUX != 0 || (&m_curTileCodingParams)->TileStartLCUY != 0) |
| 932 | { |
| 933 | uint32_t NumOfCUInLCU = (ctbSize >> 3) * (ctbSize >> 3); // Min CU size is 8 |
| 934 | uint32_t ImgWidthInLCU = (widthInPix + ctbSize - 1) / ctbSize; |
| 935 | uint32_t ImgHeightInLCU = (heightInPix + ctbSize - 1) / ctbSize; |
| 936 | uint32_t NumLCUsCurLocation = (&m_curTileCodingParams)->TileStartLCUY * ImgWidthInLCU + (&m_curTileCodingParams)->TileStartLCUX * |
| 937 | (((((&m_curTileCodingParams)->TileHeightInMinCbMinus1 + 1) << minCodingBlkSize) + ctbSize - 1) / ctbSize); |
| 938 | //For PAKObject Surface |
| 939 | tileLCUStreamOutByteOffset = 2 * BYTES_PER_DWORD * NumLCUsCurLocation * (NUM_PAK_DWS_PER_LCU + NumOfCUInLCU * NUM_DWS_PER_CU); |
| 940 | //Add 1 CL for size info at the beginning of each tile |
| 941 | tileLCUStreamOutByteOffset += MHW_CACHELINE_SIZE; |
| 942 | //CL alignment at end of every tile |
| 943 | tileLCUStreamOutByteOffset = MOS_ROUNDUP_DIVIDE(tileLCUStreamOutByteOffset, MHW_CACHELINE_SIZE); |
| 944 | } |
| 945 | |
| 946 | params.tileLCUStreamOutOffset = tileLCUStreamOutByteOffset; |
| 947 | params.tileRowstoreOffset = (params.tileStartLCUY == 0) ? (params.tileStartLCUX * params.ctbSize) / 32 : 0; |
| 948 | } |
| 949 | |
| 950 | return MOS_STATUS_SUCCESS; |
| 951 | } |
| 952 | |
| 953 | } // namespace encode |