| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifndef __ENCODE_VP9_BRC_H__ |
| 27 |
#define __ENCODE_VP9_BRC_H__ |
| 28 |
|
| 29 |
#include "media_feature.h" |
| 30 |
#include "encode_allocator.h" |
| 31 |
#include "codec_hw_next.h" |
| 32 |
#include "encode_pipeline.h" |
| 33 |
#include "encode_vp9_basic_feature.h" |
| 34 |
#include "media_vp9_packet_defs.h" |
| 35 |
|
| 36 |
namespace encode |
| 37 |
{ |
| 38 |
#define CODECHAL_ENCODE_VP9_PIC_STATE_BUFFER_SIZE_PER_PASS 192 // 42 DWORDs for Pic State one uint32_t for BB End + 5 uint32_tS reserved to make it aligned for kernel read |
| 39 |
#define CODECHAL_ENCODE_VP9_CQP_NUM_OF_PASSES 2 |
| 40 |
#define CODECHAL_ENCODE_VP9_BRC_DEFAULT_NUM_OF_PASSES 2 // 2 Passes minimum so HuC is Run twice, second PAK is conditional. |
| 41 |
#define CODECHAL_ENCODE_VP9_BRC_CONSTANTSURFACE_SIZE 17792 |
| 42 |
#define CODECHAL_ENCODE_VP9_SEGMENT_STATE_BUFFER_SIZE 256 |
| 43 |
#define CODECHAL_ENCODE_VP9_BRC_BITSTREAM_SIZE_BUFFER_SIZE 16 |
| 44 |
#define CODECHAL_ENCODE_VP9_BRC_MSDK_PAK_BUFFER_SIZE 64 |
| 45 |
#define CODECHAL_ENCODE_VP9_PAK_INSERT_UNCOMPRESSED_HEADER 80 |
| 46 |
#define CODECHAL_ENCODE_VP9_VDENC_DATA_EXTENSION_SIZE 32 |
| 47 |
#define CODECHAL_ENCODE_VP9_HUC_BRC_DATA_BUFFER_SIZE (16*4) |
| 48 |
#define CODECHAL_ENCODE_VP9_BRC_MAX_NUM_OF_PASSES 4 |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
struct EncodeVp9BSBuffer |
| 54 |
{ |
| 55 |
uint32_t dwHcpBitstreamByteCountFrame; |
| 56 |
uint32_t dwHcpImageStatusControl; |
| 57 |
uint32_t Reserved[2]; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
struct HucBrcBuffers |
| 65 |
{ |
| 66 |
MOS_RESOURCE resBrcHistoryBuffer; |
| 67 |
MOS_RESOURCE resBrcConstantDataBuffer[2]; |
| 68 |
MOS_RESOURCE resBrcMsdkPakBuffer; |
| 69 |
MOS_RESOURCE resBrcMbEncCurbeWriteBuffer; |
| 70 |
MOS_RESOURCE resMbEncAdvancedDsh; |
| 71 |
MOS_RESOURCE resPicStateBrcReadBuffer; |
| 72 |
MOS_RESOURCE resPicStateBrcWriteHucReadBuffer; |
| 73 |
MOS_RESOURCE resPicStateHucWriteBuffer; |
| 74 |
MOS_RESOURCE resSegmentStateBrcReadBuffer; |
| 75 |
MOS_RESOURCE resSegmentStateBrcWriteBuffer; |
| 76 |
MOS_RESOURCE resBrcBitstreamSizeBuffer; |
| 77 |
MOS_RESOURCE resBrcHucDataBuffer; |
| 78 |
}; |
| 79 |
|
| 80 |
class Vp9EncodeBrc : public MediaFeature, public mhw::vdbox::huc::Itf::ParSetting, public mhw::vdbox::hcp::Itf::ParSetting, public mhw::vdbox::vdenc::Itf::ParSetting |
| 81 |
{ |
| 82 |
public: |
| 83 |
Vp9EncodeBrc(MediaFeatureManager *featureManager, EncodeAllocator *allocator, CodechalHwInterfaceNext *hwInterface, void *constSettings); |
| 84 |
|
| 85 |
virtual ~Vp9EncodeBrc(); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
MOS_STATUS Init(void *settings) override; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
MOS_STATUS Update(void *params) override; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
bool IsBrcEnabled() { return m_brcEnabled; } |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
bool IsVdencBrcEnabled() { return m_vdencBrcEnabled; } |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
void DisableBrcInitReset() { m_brcInit = m_brcReset = false; }; |
| 123 |
|
| 124 |
void BrcReset(bool val) { m_brcReset = val; }; |
| 125 |
bool IsBrcReset() { return m_brcReset; }; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
bool IsBrcInit() const { return m_brcInit; } |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
bool IsBrcInitRequired() { return m_vdencBrcEnabled & (m_brcInit || m_brcReset); } |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
bool IsBrcUpdateRequired() { return m_vdencBrcEnabled; } |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
bool IsRateControlBrc(uint8_t rc) |
| 155 |
{ |
| 156 |
return (rc == RATECONTROL_CBR) || |
| 157 |
(rc == RATECONTROL_VBR) || |
| 158 |
(rc == RATECONTROL_AVBR) || |
| 159 |
(rc == RATECONTROL_VCM) || |
| 160 |
(rc == RATECONTROL_ICQ) || |
| 161 |
(rc == RATECONTROL_QVBR); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
bool IsMultipassBrcSupported() const { return m_multipassBrcSupported; } |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
MOS_STATUS SetRegionsForBrcUpdate( |
| 179 |
mhw::vdbox::huc::HUC_VIRTUAL_ADDR_STATE_PAR ¶ms) const; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
MOS_STATUS SetDmemForUpdate(void *params, bool isFirstPass) const; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
MOS_STATUS SetDmemForInit(void *params) const; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
virtual MOS_STATUS GetHucBrcBuffers( |
| 209 |
HucBrcBuffers *&buffers); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
virtual MOS_STATUS GetBrcHistoryBufferSize( |
| 219 |
uint32_t &size); |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
virtual MOS_STATUS GetVdencBrcStatsBufferSize( |
| 229 |
uint32_t &size); |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
virtual MOS_STATUS GetVdencBrcPakStatsBufferSize( |
| 239 |
uint32_t &size); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
MOS_STATUS InitConstantDataBuffer() const; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
MHW_SETPAR_DECL_HDR(HCP_VP9_PIC_STATE); |
| 252 |
MHW_SETPAR_DECL_HDR(HCP_PIPE_BUF_ADDR_STATE); |
| 253 |
MHW_SETPAR_DECL_HDR(VDENC_PIPE_BUF_ADDR_STATE); |
| 254 |
|
| 255 |
|
| 256 |
static constexpr uint32_t m_brcMaxNumPasses = 3; |
| 257 |
static constexpr uint32_t m_brcStatsBufSize = ((48 + 256) * sizeof(uint32_t)); |
| 258 |
static constexpr uint32_t m_brcPakStatsBufSize = (64 * sizeof(uint32_t)); |
| 259 |
static constexpr uint32_t m_brcConstantSurfaceSize = 1664; |
| 260 |
|
| 261 |
mutable double m_inputBitsPerFrame = 0.0; |
| 262 |
mutable double m_curTargetFullness = 0.0; |
| 263 |
|
| 264 |
protected: |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
virtual MOS_STATUS AllocateResources() override; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
virtual MOS_STATUS FreeResources(); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
virtual MOS_STATUS SetBrcSettings(void *params) const; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
MOS_STATUS ComputeVDEncInitQP(int32_t &initQpI, int32_t &initQpP) const; |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
MOS_STATUS CalculateTemporalRatios( |
| 313 |
uint16_t numberOfLayers, |
| 314 |
uint32_t maxTemporalBitrate, |
| 315 |
FRAME_RATE maxTemporalFrameRate, |
| 316 |
uint8_t * maxLevelRatios) const; |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
uint32_t CalculateNormalizedDenominator( |
| 332 |
FRAME_RATE *frameRates, |
| 333 |
uint16_t numberOfLayers, |
| 334 |
uint32_t normalizedDenominator) const; |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
MOS_STATUS SetSequenceStructs(); |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
const MOS_RESOURCE* GetBrcConstantBuffer() const; |
| 351 |
|
| 352 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_ISCBR = 0x0010; |
| 353 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_ISVBR = 0x0020; |
| 354 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_ISAVBR = 0x0040; |
| 355 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_ISCQL = 0x0080; |
| 356 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_FIELD_PIC = 0x0100; |
| 357 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_ISICQ = 0x0200; |
| 358 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_ISVCM = 0x0400; |
| 359 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_IGNORE_PICTURE_HEADER_SIZE = 0x2000; |
| 360 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_ISQVBR = 0x4000; |
| 361 |
static constexpr uint32_t CODECHAL_ENCODE_BRCINIT_DISABLE_MBBRC = 0x8000; |
| 362 |
|
| 363 |
static const uint32_t m_brcConstData[2][416]; |
| 364 |
|
| 365 |
static constexpr float m_devStdFps = 30.0; |
| 366 |
static constexpr float m_bpsRatioLow = 0.1f; |
| 367 |
static constexpr float m_bpsRatioHigh = 3.5; |
| 368 |
|
| 369 |
static constexpr int32_t m_numInstRateThresholds = 4; |
| 370 |
static constexpr int32_t m_numDevThresholds = 8; |
| 371 |
static constexpr int32_t m_posMultPb = 50; |
| 372 |
static constexpr int32_t m_negMultPb = -50; |
| 373 |
static constexpr int32_t m_posMultVbr = 100; |
| 374 |
static constexpr int32_t m_negMultVbr = -50; |
| 375 |
|
| 376 |
static constexpr int8_t m_instRateThresholdI[m_numInstRateThresholds] = {30, 50, 90, 115}; |
| 377 |
static constexpr int8_t m_instRateThresholdP[m_numInstRateThresholds] = {30, 50, 70, 120}; |
| 378 |
static constexpr double m_devThresholdFpNegI[m_numDevThresholds / 2] = {0.80, 0.60, 0.34, 0.2}; |
| 379 |
static constexpr double m_devThresholdFpPosI[m_numDevThresholds / 2] = {0.2, 0.4, 0.66, 0.9}; |
| 380 |
static constexpr double m_devThresholdFpNegPB[m_numDevThresholds / 2] = {0.90, 0.66, 0.46, 0.3}; |
| 381 |
static constexpr double m_devThresholdFpPosPB[m_numDevThresholds / 2] = {0.3, 0.46, 0.70, 0.90}; |
| 382 |
static constexpr double m_devThresholdVbrNeg[m_numDevThresholds / 2] = {0.90, 0.70, 0.50, 0.3}; |
| 383 |
static constexpr double m_devThresholdVbrPos[m_numDevThresholds / 2] = {0.4, 0.5, 0.75, 0.90}; |
| 384 |
|
| 385 |
|
| 386 |
static constexpr uint32_t m_brcHistoryBufSize = 1152; |
| 387 |
|
| 388 |
CodechalHwInterfaceNext * m_hwInterface = nullptr; |
| 389 |
EncodeAllocator * m_allocator = nullptr; |
| 390 |
Vp9BasicFeature * m_basicFeature = nullptr; |
| 391 |
|
| 392 |
std::shared_ptr<mhw::vdbox::hcp::Itf> m_hcpInterfaceNew = nullptr; |
| 393 |
std::shared_ptr<mhw::vdbox::vdenc::Itf> m_vdencInterfaceNew = nullptr; |
| 394 |
|
| 395 |
bool m_brcInit = true; |
| 396 |
bool m_brcReset = false; |
| 397 |
bool m_brcEnabled = false; |
| 398 |
bool m_vdencEnabled = false; |
| 399 |
bool m_vdencBrcEnabled = false; |
| 400 |
|
| 401 |
|
| 402 |
HucBrcBuffers m_brcBuffers = {}; |
| 403 |
uint32_t m_brcHistoryBufferSize = 0; |
| 404 |
uint32_t m_vdencBrcStatsBufferSize = 0; |
| 405 |
MOS_RESOURCE m_resFrameStatStreamOutBuffer = {0}; |
| 406 |
uint32_t m_vdencBrcPakStatsBufferSize = 0; |
| 407 |
bool m_multipassBrcSupported = true; |
| 408 |
bool m_initBrcConstantDataBuffer = false; |
| 409 |
|
| 410 |
MEDIA_CLASS_DEFINE_END(encode__Vp9EncodeBrc) |
| 411 |
}; |
| 412 |
} |
| 413 |
|
| 414 |
#endif |