1 /*
2 * Copyright (c) 2020-2021, 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_avc_huc_brc_init_packet.h
24 //! \brief    Defines the implementation of avc huc init packet
25 //!
26 
27 #ifndef __CODECHAL_AVC_HUC_BRC_INIT_PACKET_H__
28 #define __CODECHAL_AVC_HUC_BRC_INIT_PACKET_H__
29 
30 #include "encode_huc.h"
31 #include "encode_avc_basic_feature.h"
32 
33 namespace encode
34 {
35 
36 struct VdencAvcHucBrcInitDmem
37 {
38     uint8_t     BRCFunc_U8;                           // 0: Init; 2: Reset
39     uint8_t     OpenSourceEnable_U8;                  // 0: disable opensource, 1: enable opensource
40     uint8_t     RVSD[2];
41     uint16_t    INIT_BRCFlag_U16;                     // ICQ or CQP with slice size control: 0x00 CBR: 0x10; VBR: 0x20; VCM: 0x40; LOWDELAY: 0x80.
42     uint16_t    Reserved;
43     uint16_t    INIT_FrameWidth_U16;                  // Luma width in bytes
44     uint16_t    INIT_FrameHeight_U16;                 // Luma height in bytes
45     uint32_t    INIT_TargetBitrate_U32;               // target bitrate, set by application
46     uint32_t    INIT_MinRate_U32;                     // 0
47     uint32_t    INIT_MaxRate_U32;                     // Maximum bit rate in bits per second (bps).
48     uint32_t    INIT_BufSize_U32;                     // buffer size
49     uint32_t    INIT_InitBufFull_U32;                 // initial buffer fullness
50     uint32_t    INIT_ProfileLevelMaxFrame_U32;        // user defined. refer to AVC BRC for conformance check and correction
51     uint32_t    INIT_FrameRateM_U32;                  // FrameRateM is the number of frames in FrameRateD
52     uint32_t    INIT_FrameRateD_U32;                  // If driver gets this FrameRateD from VUI, it is the num_units_in_tick field (32 bits unsigned integer).
53     uint16_t    INIT_GopP_U16;                        // number of P frames in a GOP
54     uint16_t    INIT_GopB_U16;                        // number of B frames in a GOP
55     uint16_t    INIT_MinQP_U16;                       // 10
56     uint16_t    INIT_MaxQP_U16;                       // 51
57     int8_t      INIT_DevThreshPB0_S8[8];              // lowdelay ? (-45, -33, -23, -15, -8, 0, 15, 25) : (-46, -38, -30, -23, 23, 30, 40, 46)
58     int8_t      INIT_DevThreshVBR0_S8[8];             // lowdelay ? (-45, -35, -25, -15, -8, 0, 20, 40) : (-46, -40, -32, -23, 56, 64, 83, 93)
59     int8_t      INIT_DevThreshI0_S8[8];               // lowdelay ? (-40, -30, -17, -10, -5, 0, 10, 20) : (-43, -36, -25, -18, 18, 28, 38, 46)
60     uint8_t     INIT_InitQPIP;                        // Initial QP for I and P
61 
62     uint8_t     INIT_NotUseRhoDm_U8;                  // Reserved
63     uint8_t     INIT_InitQPB;                         // Initial QP for B
64     uint8_t     INIT_MbQpCtrl_U8;                     // Enable MB level QP control (global)
65     uint8_t     INIT_SliceSizeCtrlEn_U8;              // Enable slice size control
66     int8_t      INIT_IntraQPDelta_I8[3];              // set to zero for all by default
67     int8_t      INIT_SkipQPDelta_I8;                  // Reserved
68     int8_t      INIT_DistQPDelta_I8[4];               // lowdelay ? (-5, -2, 2, 5) : (0, 0, 0, 0)
69     uint8_t     INIT_OscillationQpDelta_U8;           // BRCFLAG_ISVCM ? 16 : 0
70     uint8_t     INIT_HRDConformanceCheckDisable_U8;   // BRCFLAG_ISAVBR ? 1 : 0
71     uint8_t     INIT_SkipFrameEnableFlag;
72     uint8_t     INIT_TopQPDeltaThrForAdapt2Pass_U8;   // =1. QP Delta threshold for second pass.
73     uint8_t     INIT_TopFrmSzThrForAdapt2Pass_U8;     // lowdelay ? 10 : 50. Top frame size threshold for second pass
74     uint8_t     INIT_BotFrmSzThrForAdapt2Pass_U8;     // lowdelay ? 10 : 200. Bottom frame size threshold for second pass
75     uint8_t     INIT_QPSelectForFirstPass_U8;         // lowdelay ? 0 : 1. =0 to use previous frame final QP; or =1 to use (targetQP + previousQP) / 2.
76     uint8_t     INIT_MBHeaderCompensation_U8;         // Reserved
77     uint8_t     INIT_OverShootCarryFlag_U8;           // set to zero by default
78     uint8_t     INIT_OverShootSkipFramePct_U8;        // set to zero by default
79     uint8_t     INIT_EstRateThreshP0_U8[7];           // 4, 8, 12, 16, 20, 24, 28
80     uint8_t     INIT_EstRateThreshB0_U8[7];           // 4, 8, 12, 16, 20, 24, 28
81     uint8_t     INIT_EstRateThreshI0_U8[7];           // 4, 8, 12, 16, 20, 24, 28
82     uint8_t     INIT_FracQPEnable_U8;                 // ExtendedRhoDomainEn from par file
83     uint8_t     INIT_ScenarioInfo_U8;                 // 0: UNKNOWN, 1: DISPLAYREMOTING, 2: VIDEOCONFERENCE, 3: ARCHIVE, 4: LIVESTREAMING.
84     uint8_t     INIT_StaticRegionStreamIn_U8;         // should be programmed from par file
85     uint8_t     INIT_DeltaQP_Adaptation_U8;           // =1, should be programmed from par file
86     uint8_t     INIT_MaxCRFQualityFactor_U8;          // =52, should be programmed from par file
87     uint8_t     INIT_CRFQualityFactor_U8;             // =25, should be programmed from par file
88     uint8_t     INIT_BotQPDeltaThrForAdapt2Pass_U8;   // =1. QP Delta threshold for second pass.
89     uint8_t     INIT_SlidingWindowSize_U8;            // =30, the window size (in frames) used to compute bit rate
90     uint8_t     INIT_SlidingWidowRCEnable_U8;         // =0, sliding window based rate control (SWRC) disabled, 1: enabled
91     uint8_t     INIT_SlidingWindowMaxRateRatio_U8;    // =120, ratio between the max rate within the window and average target bitrate
92     uint8_t     INIT_LowDelayGoldenFrameBoost_U8;     // only for lowdelay mode, 0 (default): no boost for I and scene change frames, 1: boost
93     uint8_t     INIT_AdaptiveCostEnable_U8;           // 0: disabled, 1: enabled
94     uint8_t     INIT_AdaptiveHMEExtensionEnable_U8;   // 0: disabled, 1: enabled
95     uint8_t     INIT_ICQReEncode_U8;                  // 0: disabled, 1: enabled
96     uint8_t     INIT_LookaheadDepth_U8;               // Lookahead depth in unit of frames [0, 127]
97     uint8_t     INIT_SinglePassOnly;                  // 0: disabled, 1: enabled
98     uint8_t     INIT_New_DeltaQP_Adaptation_U8;       // = 1 to enable new delta QP adaption
99     uint8_t     INIT_ExtMaxBrcLevel_U8;               // max hierarchy level of B-frames
100     uint16_t    INIT_ExtGopP_U16;                     // correctly calculated number of P  frames in a GOP
101     uint16_t    INIT_ExtGopB_U16;                     // correctly calculated number of B  frames in a GOP
102     uint16_t    INIT_ExtGopB1_U16;                    // correctly calculated number of B1 frames in a GOP
103     uint16_t    INIT_ExtGopB2_U16;                    // correctly calculated number of B2 frames in a GOP
104 
105     uint8_t Rsvd2[46];  //!< reserved for 64 bytes aligned, total structure size 192 bytes
106 };
107 
108 class AvcHucBrcInitPkt : public EncodeHucPkt
109 {
110 public:
AvcHucBrcInitPkt(MediaPipeline * pipeline,MediaTask * task,CodechalHwInterfaceNext * hwInterface)111     AvcHucBrcInitPkt(MediaPipeline *pipeline, MediaTask *task, CodechalHwInterfaceNext *hwInterface) :
112         EncodeHucPkt(pipeline, task, hwInterface) {}
113 
~AvcHucBrcInitPkt()114     virtual ~AvcHucBrcInitPkt() {}
115 
116     virtual MOS_STATUS Init() override;
117 
118     virtual MOS_STATUS Submit(MOS_COMMAND_BUFFER *commandBuffer, uint8_t packetPhase = otherPacket) override;
119 
120     virtual MOS_STATUS DumpOutput() override;
121 
122     //!
123     //! \brief  Calculate Command Size
124     //!
125     //! \param  [in, out] commandBufferSize
126     //!         requested size
127     //! \param  [in, out] requestedPatchListSize
128     //!         requested size
129     //! \return MOS_STATUS
130     //!         status
131     //!
132     virtual MOS_STATUS CalculateCommandSize(
133         uint32_t &commandBufferSize,
134         uint32_t &requestedPatchListSize) override;
135 
136     //!
137     //! \brief  Get Packet Name
138     //! \return std::string
139     //!
GetPacketName()140     virtual std::string GetPacketName() override
141     {
142         return "BRCINIT";
143     }
144 
145 protected:
146     virtual MOS_STATUS AllocateResources() override;
147     virtual MOS_STATUS AddForceWakeup(MOS_COMMAND_BUFFER &cmdBuffer) override;
148 
149     MHW_SETPAR_DECL_HDR(HUC_IMEM_STATE);
150     MHW_SETPAR_DECL_HDR(HUC_DMEM_STATE);
151     MHW_SETPAR_DECL_HDR(HUC_VIRTUAL_ADDR_STATE);
152 
153     virtual MOS_STATUS SetDmemBuffer()const;
154 
155 #if USE_CODECHAL_DEBUG_TOOL
156     virtual MOS_STATUS DumpHucBrcInit(bool isInput);
157 #endif
158 
159     AvcBasicFeature *m_basicFeature = nullptr;                                        //!< Avc Basic Feature used in each frame
160 
161     uint32_t m_vdencBrcInitDmemBufferSize = sizeof(VdencAvcHucBrcInitDmem);           //!< Brc Init-Dmem Buffer Size
162     PMOS_RESOURCE m_vdencBrcInitDmemBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM] = {};  //!< Brc Init DMEM Buffer Array
163 
164 MEDIA_CLASS_DEFINE_END(encode__AvcHucBrcInitPkt)
165 };
166 
167 }  // namespace encode
168 
169 #endif   // !__CODECHAL_AVC_HUC_BRC_INIT_PACKET_H__
170