| 1 | /* |
| 2 | * Copyright (c) 2023-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_av1_fastpass.cpp |
| 24 | //! \brief Fast Pass feature |
| 25 | //! |
| 26 | |
| 27 | #include "encode_av1_fastpass.h" |
| 28 | |
| 29 | namespace encode |
| 30 | { |
| 31 | Av1FastPass::Av1FastPass(MediaFeatureManager *featureManager, |
| 32 | EncodeAllocator *allocator, |
| 33 | CodechalHwInterfaceNext *hwInterface, |
| 34 | void *constSettings): |
| 35 | MediaFeature(constSettings, hwInterface ? hwInterface->GetOsInterface() : nullptr) |
| 36 | { |
| 37 | ENCODE_FUNC_CALL(); |
| 38 | |
| 39 | ENCODE_CHK_NULL_NO_STATUS_RETURN(featureManager); |
| 40 | |
| 41 | m_basicFeature = dynamic_cast<Av1BasicFeature *>(featureManager->GetFeature(Av1FeatureIDs::basicFeature)); |
| 42 | ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature); |
| 43 | |
| 44 | //regkey to control fast pass encode settings |
| 45 | MediaUserSetting::Value outValue; |
| 46 | MOS_STATUS statusKey = ReadUserSetting(m_userSettingPtr, |
| 47 | outValue, |
| 48 | "Enable Fast Pass Encode", |
| 49 | MediaUserSetting::Group::Sequence); |
| 50 | |
| 51 | if (statusKey == MOS_STATUS_SUCCESS) |
| 52 | { |
| 53 | m_enabled = outValue.Get<bool>(); |
| 54 | MediaUserSetting::Value outValue_ratio; |
| 55 | MediaUserSetting::Value outValue_type; |
| 56 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 57 | |
| 58 | ReadUserSetting(m_userSettingPtr, |
| 59 | outValue_ratio, |
| 60 | "Fast Pass Encode Downscale Ratio", |
| 61 | MediaUserSetting::Group::Sequence); |
| 62 | ReadUserSetting(m_userSettingPtr, |
| 63 | outValue_type, |
| 64 | "Fast Pass Encode Downscale Type", |
| 65 | MediaUserSetting::Group::Sequence); |
| 66 | #endif |
| 67 | |
| 68 | m_fastPassDownScaleRatio = (outValue_ratio.Get<int32_t>() == 1) ? 1 : 2; |
| 69 | m_fastPassDownScaleType = (uint8_t)outValue_type.Get<int32_t>(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | Av1FastPass::~Av1FastPass() |
| 74 | { |
| 75 | ENCODE_FUNC_CALL(); |
| 76 | } |
| 77 | |
| 78 | MOS_STATUS Av1FastPass::Update(void *params) |
| 79 | { |
| 80 | ENCODE_FUNC_CALL(); |
| 81 | ENCODE_CHK_NULL_RETURN(m_basicFeature); |
| 82 | PCODEC_AV1_ENCODE_PICTURE_PARAMS av1PicParams = m_basicFeature->m_av1PicParams; |
| 83 | ENCODE_CHK_NULL_RETURN(av1PicParams); |
| 84 | |
| 85 | if (!m_enabled) |
| 86 | { |
| 87 | return MOS_STATUS_SUCCESS; |
| 88 | } |
| 89 | |
| 90 | uint16_t numTiles = m_basicFeature->m_av1PicParams->tile_rows * m_basicFeature->m_av1PicParams->tile_cols; |
| 91 | if (numTiles > 1) |
| 92 | { |
| 93 | ENCODE_ASSERTMESSAGE("FastPass Encode does not support multi-tile."); |
| 94 | return MOS_STATUS_INVALID_PARAMETER; |
| 95 | } |
| 96 | |
| 97 | if (AV1_FAST_PASS_4X_DS(m_fastPassDownScaleRatio)) |
| 98 | { |
| 99 | // 4x down scale and av1 needs 8-pixels aligned |
| 100 | m_dsWidth = MOS_ALIGN_FLOOR((av1PicParams->frame_width_minus1 + 1) >> 2, 8); |
| 101 | m_dsHeight = MOS_ALIGN_FLOOR((av1PicParams->frame_height_minus1 + 1) >> 2, 8); |
| 102 | }else |
| 103 | { |
| 104 | // 2x down scale and av1 needs 8-pixels aligned |
| 105 | m_dsWidth = MOS_ALIGN_FLOOR((av1PicParams->frame_width_minus1 + 1) >> 1, 8); |
| 106 | m_dsHeight = MOS_ALIGN_FLOOR((av1PicParams->frame_height_minus1 + 1) >> 1, 8); |
| 107 | } |
| 108 | |
| 109 | return MOS_STATUS_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | MHW_SETPAR_DECL_SRC(VDENC_PIPE_MODE_SELECT, Av1FastPass) |
| 113 | { |
| 114 | ENCODE_FUNC_CALL(); |
| 115 | if (m_enabled) |
| 116 | { |
| 117 | params.fastPassEn = m_enabled; |
| 118 | params.fastPassScale = m_fastPassDownScaleRatio; // fastPassScale:0 indicates 4x4 ds, 1 indicates 2x2 ds |
| 119 | params.DownScaleType = m_fastPassDownScaleType; // DownScaleType:0 indicates bilinear, 1 indicates NN |
| 120 | |
| 121 | params.chromaType = AVP_CHROMA_FORMAT_YUV420; |
| 122 | params.bitDepthMinus8 = 0; |
| 123 | } |
| 124 | return MOS_STATUS_SUCCESS; |
| 125 | } |
| 126 | |
| 127 | MHW_SETPAR_DECL_SRC(VDENC_CMD2, Av1FastPass) |
| 128 | { |
| 129 | ENCODE_FUNC_CALL(); |
| 130 | |
| 131 | if (m_enabled) |
| 132 | { |
| 133 | params.width = m_dsWidth; |
| 134 | params.height = m_dsHeight; |
| 135 | } |
| 136 | return MOS_STATUS_SUCCESS; |
| 137 | } |
| 138 | |
| 139 | MHW_SETPAR_DECL_SRC(VDENC_HEVC_VP9_TILE_SLICE_STATE, Av1FastPass) |
| 140 | { |
| 141 | ENCODE_FUNC_CALL(); |
| 142 | |
| 143 | PCODEC_AV1_ENCODE_PICTURE_PARAMS av1PicParams = m_basicFeature->m_av1PicParams; |
| 144 | ENCODE_CHK_NULL_RETURN(av1PicParams); |
| 145 | bool m_enablePalette = av1PicParams->PicFlags.fields.PaletteModeEnable; |
| 146 | |
| 147 | if (m_enabled) |
| 148 | { |
| 149 | params.tileWidth = m_dsWidth; |
| 150 | params.tileHeight = m_dsHeight; |
| 151 | |
| 152 | if (m_enablePalette && m_basicFeature->m_is10Bit) |
| 153 | { |
| 154 | params.VdencHEVCVP9TileSlicePar5 -= 2; |
| 155 | } |
| 156 | } |
| 157 | return MOS_STATUS_SUCCESS; |
| 158 | } |
| 159 | |
| 160 | MHW_SETPAR_DECL_SRC(VDENC_WALKER_STATE, Av1FastPass) |
| 161 | { |
| 162 | ENCODE_FUNC_CALL(); |
| 163 | |
| 164 | if (m_enabled) |
| 165 | { |
| 166 | //one tile supported only |
| 167 | params.tileSliceStartLcuMbX = 0; |
| 168 | params.tileSliceStartLcuMbY = 0; |
| 169 | params.nextTileSliceStartLcuMbX = MOS_ROUNDUP_DIVIDE(m_dsWidth, av1SuperBlockWidth); |
| 170 | params.nextTileSliceStartLcuMbY = MOS_ROUNDUP_DIVIDE(m_dsHeight, av1SuperBlockHeight); |
| 171 | } |
| 172 | |
| 173 | return MOS_STATUS_SUCCESS; |
| 174 | } |
| 175 | |
| 176 | MHW_SETPAR_DECL_SRC(VDENC_REF_SURFACE_STATE, Av1FastPass) |
| 177 | { |
| 178 | ENCODE_FUNC_CALL(); |
| 179 | |
| 180 | if (m_enabled) |
| 181 | { |
| 182 | params.width = m_dsWidth; |
| 183 | params.height = m_dsHeight; |
| 184 | params.format = Format_NV12; |
| 185 | |
| 186 | if (m_basicFeature->m_chromaFormat == AVP_CHROMA_FORMAT_YUV444 |
| 187 | || m_basicFeature->m_chromaFormat == AVP_CHROMA_FORMAT_YUV422) |
| 188 | { |
| 189 | params.vOffset = params.uOffset; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return MOS_STATUS_SUCCESS; |
| 194 | } |
| 195 | |
| 196 | MHW_SETPAR_DECL_SRC(VDENC_DS_REF_SURFACE_STATE, Av1FastPass) |
| 197 | { |
| 198 | ENCODE_FUNC_CALL(); |
| 199 | |
| 200 | if (m_enabled) |
| 201 | { |
| 202 | uint32_t dsWidth4x = m_dsWidth / SCALE_FACTOR_4x; |
| 203 | uint32_t dsHeight4x = m_dsHeight / SCALE_FACTOR_4x; |
| 204 | |
| 205 | params.heightStage1 = dsHeight4x >> 1; |
| 206 | params.widthStage1 = dsWidth4x >> 1; |
| 207 | params.heightStage2 = dsHeight4x; |
| 208 | params.widthStage2 = dsWidth4x; |
| 209 | } |
| 210 | |
| 211 | return MOS_STATUS_SUCCESS; |
| 212 | } |
| 213 | |
| 214 | MHW_SETPAR_DECL_SRC(AVP_TILE_CODING, Av1FastPass) |
| 215 | { |
| 216 | ENCODE_FUNC_CALL(); |
| 217 | |
| 218 | if (m_enabled) |
| 219 | { |
| 220 | //one tile supported only |
| 221 | params.tileColPositionInSb = 0; |
| 222 | params.tileRowPositionInSb = 0; |
| 223 | params.tileWidthInSbMinus1 = MOS_ROUNDUP_DIVIDE(m_dsWidth, av1SuperBlockWidth) - 1; |
| 224 | params.tileHeightInSbMinus1 = MOS_ROUNDUP_DIVIDE(m_dsHeight, av1SuperBlockHeight) - 1; |
| 225 | } |
| 226 | |
| 227 | return MOS_STATUS_SUCCESS; |
| 228 | } |
| 229 | |
| 230 | MHW_SETPAR_DECL_SRC(AVP_PIC_STATE, Av1FastPass) |
| 231 | { |
| 232 | ENCODE_FUNC_CALL(); |
| 233 | |
| 234 | if (m_enabled) |
| 235 | { |
| 236 | params.frameWidthMinus1 = m_dsWidth - 1; |
| 237 | params.frameHeightMinus1 = m_dsHeight - 1; |
| 238 | for (auto i = 0; i <= av1NumInterRefFrames; i++) |
| 239 | { |
| 240 | params.refFrameRes[i] = CAT2SHORTS(m_dsWidth - 1, m_dsHeight - 1); |
| 241 | } |
| 242 | params.chromaFormat = AVP_CHROMA_FORMAT_YUV420; |
| 243 | params.bitDepthIdc = 0; |
| 244 | } |
| 245 | |
| 246 | return MOS_STATUS_SUCCESS; |
| 247 | } |
| 248 | |
| 249 | MHW_SETPAR_DECL_SRC(AVP_SURFACE_STATE, Av1FastPass) |
| 250 | { |
| 251 | ENCODE_FUNC_CALL(); |
| 252 | |
| 253 | if (m_enabled) |
| 254 | { |
| 255 | if (m_basicFeature->m_chromaFormat == AVP_CHROMA_FORMAT_YUV444 |
| 256 | || m_basicFeature->m_chromaFormat == AVP_CHROMA_FORMAT_YUV422) |
| 257 | { |
| 258 | params.vOffset = params.uOffset; |
| 259 | } |
| 260 | params.srcFormat = mhw::vdbox::avp::SURFACE_FORMAT::SURFACE_FORMAT_PLANAR4208; |
| 261 | params.bitDepthLumaMinus8 = 0; |
| 262 | } |
| 263 | |
| 264 | return MOS_STATUS_SUCCESS; |
| 265 | } |
| 266 | |
| 267 | } // namespace encode |