1 /*
2 * Copyright (c) 2019-2022, 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_segmentation.h
24 //! \brief    Defines the common interface for encode av1 segmentation feature
25 //!
26 #ifndef __ENCODE_AV1_SEGMENTATION_H__
27 #define __ENCODE_AV1_SEGMENTATION_H__
28 
29 #include "encode_basic_feature.h"
30 #include "encode_av1_reference_frames.h"
31 #include "mhw_vdbox_vdenc_itf.h"
32 #include "mhw_vdbox_avp_itf.h"
33 
34 namespace encode
35 {
36 
37 struct VdencStreamInState;
38 class Av1StreamIn;
39 
40 class Av1Segmentation : public MediaFeature, public mhw::vdbox::vdenc::Itf::ParSetting, public mhw::vdbox::avp::Itf::ParSetting
41 {
42 public:
43     Av1Segmentation(
44         MediaFeatureManager *featureManager,
45         EncodeAllocator *allocator,
46         void *constSettings);
47 
48     virtual ~Av1Segmentation();
49 
50     //!
51     //! \brief  Update segment related parameter
52     //! \param  [in] params
53     //!         Pointer to encode parameter
54     //! \return MOS_STATUS
55     //!         MOS_STATUS_SUCCESS if success, else fail reason
56     //!
57     virtual MOS_STATUS Update(void *params) override;
58 
59     MHW_SETPAR_DECL_HDR(VDENC_PIPE_BUF_ADDR_STATE);
60 
61     MHW_SETPAR_DECL_HDR(AVP_PIC_STATE);
62 
63     MHW_SETPAR_DECL_HDR(AVP_SEGMENT_STATE);
64 
65     MHW_SETPAR_DECL_HDR(AVP_PIPE_BUF_ADDR_STATE);
66 
67     MHW_SETPAR_DECL_HDR(VDENC_CMD2);
68 
HasZeroSegmentQIndex()69     bool HasZeroSegmentQIndex() const { return m_hasZeroSegmentQIndex; }
70 
71 protected:
72     //!
73     //! \brief  Set segment id parameter
74     //! \param  [in] segmentParams
75     //!         Pointer to CodecAv1SegmentsParams
76     //! \return  MOS_STATUS
77     //!         MOS_STATUS_SUCCESS if success, else fail reason
78     //!
79     MOS_STATUS SetSegmentIdParams(
80         const PCODEC_AV1_ENCODE_PICTURE_PARAMS  ddiPicParams,
81         const CODEC_Intel_Seg_AV1              *ddiSegParams);
82 
83     MOS_STATUS CheckQPAndLossless();
84 
85     //!
86     //! \brief  Set up segment id stream in buffer
87     //! \return  MOS_STATUS
88     //!         MOS_STATUS_SUCCESS if success, else fail reason
89     //!
90     MOS_STATUS SetupSegmentationMap();
91 
92     //!
93     //! \brief  Check segmentation map
94     //! \return MOS_STATUS
95     //!         MOS_STATUS_SUCCESS if success, else fail reason
96     //!
97     MOS_STATUS CheckSegmentationMap() const;
98 
99     //!
100     //! \brief  Fill segmentation map into provided stream in buffer
101     //! \param  [in] streamInData
102     //!         pointer to stream in buffer locked address
103     //! \return MOS_STATUS
104     //!         MOS_STATUS_SUCCESS if success, else fail reason
105     //!
106     MOS_STATUS FillSegmentationMap(VdencStreamInState* streamInData) const;
107 
108     MOS_STATUS AllocateSegmentationMapBuffer(uint8_t segmentBufid);
109 
110     CodecAv1SegmentsParams m_segmentParams = {};             //!< Segment Params
111     uint8_t                m_segmentNum = 0;                 //!< Segment number
112 
113     Av1BasicFeature *m_basicFeature = nullptr;               //!< AV1 paramter
114     EncodeAllocator *m_allocator = nullptr;                  //!< AV1 allocator
115     uint8_t         *m_pSegmentMap = nullptr;                //!< segmentation map from APP
116     bool             m_segmentMapProvided = false;           //!< Flag to indicate APP's segmentation map provided or not
117     uint32_t         m_segmentMapDataSize = 0;               //!< segmentation map size from APP
118 
119     uint32_t                 m_segmentMapBlockSize = 0;      //!< segment map block size
120     uint8_t                  m_targetUsage = 0;              //!< Target Usage
121     static constexpr uint8_t m_imgStateImePredictors = 8;    //!< Number of predictors for IME
122 
123     Av1StreamIn* m_streamIn = nullptr;                       //!< The instance of stream in utility
124     bool m_hasZeroSegmentQIndex = false;                     //!< Indicates if any of segments has zero qIndex
125 
126     int8_t        m_segmenBufferinUse[av1TotalRefsPerFrame]  = {};          //!< Indicates the num of m_segmentMapBuffer uesed for DPB
127     int8_t        m_ucScalingIdtoSegID[CODEC_NUM_TRACKED_BUFFERS] = {};         //!< Map the ucscaling of DPB to segmentID of segmentMapBuffer,Array length follows ucscaling range
128     PMOS_RESOURCE m_segmentMapBuffer[av1TotalRefsPerFrame]   = {nullptr};   //!< Save the segmentMap of DPB
129 
130 MEDIA_CLASS_DEFINE_END(encode__Av1Segmentation)
131 };
132 
133 }  // namespace encode
134 
135 #endif  // !__ENCODE_AV1_SEGMENTATION_H__
136