/work/vvenc/source/Lib/EncoderLib/EncSampleAdaptiveOffset.h
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | The copyright in this software is being made available under the Clear BSD |
3 | | License, included below. No patent rights, trademark rights and/or |
4 | | other Intellectual Property Rights other than the copyrights concerning |
5 | | the Software are granted under this license. |
6 | | |
7 | | The Clear BSD License |
8 | | |
9 | | Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors. |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without modification, |
13 | | are permitted (subject to the limitations in the disclaimer below) provided that |
14 | | the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the copyright holder nor the names of its |
24 | | contributors may be used to endorse or promote products derived from this |
25 | | software without specific prior written permission. |
26 | | |
27 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
28 | | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
29 | | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
31 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
32 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
33 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
34 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
35 | | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
36 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | |
41 | | ------------------------------------------------------------------------------------------- */ |
42 | | /** |
43 | | \file EncSampleAdaptiveOffset.h |
44 | | \brief estimation part of sample adaptive offset class (header) |
45 | | */ |
46 | | |
47 | | #pragma once |
48 | | |
49 | | #include "CABACWriter.h" |
50 | | #include "CommonLib/SampleAdaptiveOffset.h" |
51 | | |
52 | | //! \ingroup EncoderLib |
53 | | //! \{ |
54 | | |
55 | | namespace vvenc { |
56 | | |
57 | | // ==================================================================================================================== |
58 | | // Class definition |
59 | | // ==================================================================================================================== |
60 | | |
61 | | struct SAOStatData //data structure for SAO statistics |
62 | | { |
63 | | int64_t diff[MAX_NUM_SAO_CLASSES]; |
64 | | int64_t count[MAX_NUM_SAO_CLASSES]; |
65 | | |
66 | 210k | SAOStatData(){} |
67 | 210k | ~SAOStatData(){} |
68 | | void reset() |
69 | 52.5k | { |
70 | 52.5k | ::memset(diff, 0, sizeof(int64_t)*MAX_NUM_SAO_CLASSES); |
71 | 52.5k | ::memset(count, 0, sizeof(int64_t)*MAX_NUM_SAO_CLASSES); |
72 | 52.5k | } |
73 | | const SAOStatData& operator=(const SAOStatData& src) |
74 | 0 | { |
75 | 0 | ::memcpy(diff, src.diff, sizeof(int64_t)*MAX_NUM_SAO_CLASSES); |
76 | 0 | ::memcpy(count, src.count, sizeof(int64_t)*MAX_NUM_SAO_CLASSES); |
77 | 0 | return *this; |
78 | 0 | } |
79 | | const SAOStatData& operator+= (const SAOStatData& src) |
80 | 0 | { |
81 | 0 | for(int i=0; i< MAX_NUM_SAO_CLASSES; i++) |
82 | 0 | { |
83 | 0 | diff[i] += src.diff[i]; |
84 | 0 | count[i] += src.count[i]; |
85 | 0 | } |
86 | 0 | return *this; |
87 | 0 | } |
88 | | }; |
89 | | |
90 | | // SIMD SAO reads up to 16 samples past the right edge of a CTU whose width is not a multiple of 16. |
91 | | static constexpr int SAO_CTU_BUF_MARGIN = 16; |
92 | | |
93 | | // The pre-SAO samples a CTU reads from neighbours that SAO filters before it: the bottom row of the |
94 | | // CTU row above and the right column of the CTU to the left. One set per picture. |
95 | | struct SaoPreFilterEdges |
96 | | { |
97 | | void create( const ChromaFormat chFmt, const PreCalcValues& pcv ); |
98 | | |
99 | | std::vector<Pel> bottomRows[MAX_NUM_COMP]; // a picture-wide line for every CTU row |
100 | | std::vector<Pel> rightCols [MAX_NUM_COMP]; // a CTU-high column for every CTU row, rewritten along the row |
101 | | int width [MAX_NUM_COMP] = { 0, }; |
102 | | int ctuHeight [MAX_NUM_COMP] = { 0, }; |
103 | | }; |
104 | | |
105 | | class EncSampleAdaptiveOffset : public SampleAdaptiveOffset |
106 | | { |
107 | | public: |
108 | | EncSampleAdaptiveOffset(); |
109 | | virtual ~EncSampleAdaptiveOffset(); |
110 | | |
111 | | //interface |
112 | | void init ( const VVEncCfg& encCfg ); |
113 | | void initSlice ( const Slice* slice ); |
114 | | void setCtuEncRsrc ( CABACWriter* cabacEstimator, CtxCache* ctxCache, PelStorage* ctuPreSaoBuf ); |
115 | | |
116 | | static void disabledRate ( CodingStructure& cs, double saoDisabledRate[ MAX_NUM_COMP ][ VVENC_MAX_TLAYER ], SAOBlkParam* reconParams, const double saoEncodingRate, const double saoEncodingRateChroma, const ChromaFormat& chromaFormat ); |
117 | | static void decidePicParams( const CodingStructure& cs, double saoDisabledRate[ MAX_NUM_COMP ][ VVENC_MAX_TLAYER ], bool saoEnabled[ MAX_NUM_COMP ], const double saoEncodingRate, const double saoEncodingRateChroma, const ChromaFormat& chromaFormat ); |
118 | | |
119 | | void loadCtuPreSao ( CodingStructure& cs, const UnitArea& ctuArea, const int ctuY, SaoPreFilterEdges& edges ); |
120 | | void getCtuStatistics ( CodingStructure& cs, std::vector<SAOStatData**>& saoStatistics, const UnitArea& ctuArea, const int ctuRsAddr ); |
121 | | void decideCtuParams ( CodingStructure& cs, const std::vector<SAOStatData**>& saoStatistics, const bool saoEnabled[ MAX_NUM_COMP ], const bool allBlksDisabled, const UnitArea& ctuArea, const int ctuRsAddr, SAOBlkParam* reconParams, SAOBlkParam* codedParams ); |
122 | | |
123 | | private: |
124 | | void getStatistics (std::vector<SAOStatData**>& blkStats, PelUnitBuf& orgYuv, PelUnitBuf& srcYuv, CodingStructure& cs); |
125 | | void getBlkStats (const ComponentID compIdx, const int channelBitDepth, SAOStatData* statsDataTypes, Pel* srcBlk, Pel* orgBlk, int srcStride, int orgStride, int width, int height, bool isLeftAvail, bool isRightAvail, bool isAboveAvail, bool isBelowAvail, bool isAboveLeftAvail, bool isAboveRightAvail ); |
126 | | void deriveModeNewRDO (const BitDepths &bitDepths, int ctuRsAddr, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES], const bool* sliceEnabled, const std::vector<SAOStatData**>& blkStats, SAOBlkParam& modeParam, double& modeNormCost ); |
127 | | void deriveModeMergeRDO (const BitDepths &bitDepths, int ctuRsAddr, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES], const bool* sliceEnabled, const std::vector<SAOStatData**>& blkStats, SAOBlkParam& modeParam, double& modeNormCost ); |
128 | | int64_t getDistortion (const int channelBitDepth, int typeIdc, int typeAuxInfo, int* offsetVal, SAOStatData& statData); |
129 | | void deriveOffsets (ComponentID compIdx, const int channelBitDepth, int typeIdc, SAOStatData& statData, int* quantOffsets, int& typeAuxInfo); |
130 | | void deriveLoopFilterBoundaryAvailibility(CodingStructure& cs, const Position& pos, bool& isLeftAvail, bool& isAboveAvail, bool& isAboveLeftAvail) const; |
131 | | inline int64_t estSaoDist (int64_t count, int64_t offset, int64_t diffSum, int shift); |
132 | | inline int estIterOffset (int typeIdx, double lambda, int offsetInput, int64_t count, int64_t diffSum, int shift, int bitIncrease, int64_t& bestDist, double& bestCost, int offsetTh ); |
133 | | |
134 | | private: |
135 | | |
136 | | const VVEncCfg* m_EncCfg; |
137 | | |
138 | | //for RDO |
139 | | CABACWriter* m_CABACEstimator; |
140 | | CtxCache* m_CtxCache; |
141 | | PelStorage* m_ctuPreSao; ///< pre-SAO samples of the current CTU with one sample of border, see loadCtuPreSao |
142 | | double m_lambda[ MAX_NUM_COMP ]; |
143 | | }; |
144 | | |
145 | | } // namespace vvenc |
146 | | |
147 | | //! \} |
148 | | |