1 /*
2 * Copyright (c) 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_av1_brc_init_packet.h
24 //! \brief    Defines the implementation of av1 brc init packet
25 //!
26 
27 #ifndef __CODECHAL_AV1_BRC_INIT_PACKET_H__
28 #define __CODECHAL_AV1_BRC_INIT_PACKET_H__
29 
30 #include "media_cmd_packet.h"
31 #include "encode_huc.h"
32 #include "media_pipeline.h"
33 #include "encode_utils.h"
34 #include "encode_av1_vdenc_pipeline.h"
35 #include "encode_av1_basic_feature.h"
36 
37 namespace encode
38 {
39     struct VdencAv1HucBrcInitDmem
40     {
41         uint32_t     BRCFunc;                    // 0: Init; 2: Reset
42 
43         uint32_t     INIT_ProfileLevelMaxFrame;  // Limit on maximum frame nuamber based on selected profile and level, could be user defined
44         uint32_t     INIT_InitBufFullness;       // Initial buffer fullness
45         uint32_t     INIT_BufSize;               // Buffer Size
46         uint32_t     INIT_TargetBitrate;         // Average (target) bit rate
47         uint32_t     INIT_MaxRate;               // Maximum bit rate
48         uint32_t     INIT_MinRate;               // Minimum bit rate
49         uint32_t     INIT_FrameRateM;            // FrameRateM
50         uint32_t     INIT_FrameRateD;            // :ud, FrameRateD
51         uint32_t     RSVD32[8];                  // mbz
52 
53         uint16_t     INIT_BRCFlag;               // BRC flag 0: ACQP, 0x10: CBR, 0x20: VBR, 0x40: AVBR, 0x80: CQL
54         uint16_t     Reserved1;                  // mbz
55         uint16_t     INIT_GopP;                  // number of P frames in the GOP
56         uint16_t     INIT_GopB;                  // number of B frames in the GOP
57         uint16_t     INIT_FrameWidth;            // frame width
58         uint16_t     INIT_FrameHeight;           // frame height
59         uint16_t     INIT_MinQP;                 // minimum QP
60         uint16_t     INIT_MaxQP;                 // maximum QP
61         uint16_t     INIT_LevelQP;               // level QP
62         uint16_t     INIT_GoldenFrameInterval;   // golden frame interval
63         uint16_t     INIT_EnableScaling;         // enable resolution scaling
64         uint16_t     INIT_OvershootCBR_pct;      // default: 115, CBR overshoot percentage
65         uint16_t     INIT_GopB1;                  // number of B1 frames in the GOP
66         uint16_t     INIT_GopB2;                  // number of B2 frames in the GOP
67         uint16_t     INIT_GopB3;                  // number of B3 frames in the GOP
68         uint16_t     RSVD16[7];                   // mbz
69 
70         int8_t       INIT_InstRateThreshP0[4];   // instant rate threshold for P frame, 4 elements
71         int8_t       Reserved2[4];               // mbz
72         int8_t       INIT_InstRateThreshI0[4];   // instant rate threshold 0 for I frame, 4 elements
73         int8_t       INIT_DevThreshPB0[8];       // deviation threshold for P and B frame, 8 elements
74         int8_t       INIT_DevThreshVBR0[8];      // deviation threshold for VBR control, 8 elements
75         int8_t       INIT_DevThreshI0[8];        // deviation threshold for I frame, 8 elements
76         uint8_t      INIT_InitQPP;
77         uint8_t      INIT_InitQPI;
78         uint8_t      INIT_SegMapGenerating;      // Cmodel sets the flag only in BRC Init() as of now
79         uint8_t      INIT_Total_Level;
80         uint8_t      INIT_MaxLevel_Ratio[16];    // level ratio
81         uint8_t      INIT_SLIDINGWINDOW_ENABLE;
82         uint8_t      INIT_SLIDINGWINDOW_SIZE;
83         uint8_t      RSVD8[22];                  // mbz
84     };
85 
86     class Av1BrcInitPkt : public EncodeHucPkt
87     {
88     public:
Av1BrcInitPkt(MediaPipeline * pipeline,MediaTask * task,CodechalHwInterfaceNext * hwInterface)89         Av1BrcInitPkt(MediaPipeline *pipeline, MediaTask *task, CodechalHwInterfaceNext *hwInterface) :
90             EncodeHucPkt(pipeline, task, hwInterface)
91         {
92         }
93 
~Av1BrcInitPkt()94         virtual ~Av1BrcInitPkt() {}
95 
96         virtual MOS_STATUS Init() override;
97 
98         MOS_STATUS Submit(MOS_COMMAND_BUFFER *commandBuffer, uint8_t packetPhase = otherPacket) override;
99 
100         //!
101         //! \brief  Calculate Command Size
102         //!
103         //! \param  [in, out] commandBufferSize
104         //!         requested size
105         //! \param  [in, out] requestedPatchListSize
106         //!         requested size
107         //! \return MOS_STATUS
108         //!         status
109         //!
110         virtual MOS_STATUS CalculateCommandSize(
111             uint32_t &commandBufferSize,
112             uint32_t &requestedPatchListSize) override;
113 
114         //!
115         //! \brief  Get Packet Name
116         //! \return std::string
117         //!
GetPacketName()118         virtual std::string GetPacketName() override
119         {
120             return "BRCINIT";
121         }
122 
123     protected:
124         virtual MOS_STATUS AllocateResources() override;
125 
126         MHW_SETPAR_DECL_HDR(HUC_IMEM_STATE);
127         MHW_SETPAR_DECL_HDR(HUC_DMEM_STATE);
128         MHW_SETPAR_DECL_HDR(HUC_VIRTUAL_ADDR_STATE);
129         MHW_SETPAR_DECL_HDR(VD_PIPELINE_FLUSH);
130 
131 #if USE_CODECHAL_DEBUG_TOOL
132     virtual MOS_STATUS DumpInput() override;
133 #endif
134         static constexpr uint32_t m_vdboxHucAv1BrcInitKernelDescriptor = 18;//!< Huc Av1 Brc init kernel descriptor
135 
136         uint32_t m_vdencBrcInitDmemBufferSize = sizeof(VdencAv1HucBrcInitDmem);
137         MOS_RESOURCE m_vdencBrcInitDmemBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM] = {}; //!< VDEnc BrcInit DMEM buffer
138 
139         Av1BasicFeature* m_basicFeature = nullptr;  //!< Av1 Basic Feature used in each frame
140 
141     MEDIA_CLASS_DEFINE_END(encode__Av1BrcInitPkt)
142     };
143 
144 }  // namespace encode
145 #endif
146