/work/vvenc/source/Lib/EncoderLib/EncSlice.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 | | /** \file EncSlice.h |
43 | | \brief slice encoder class (header) |
44 | | */ |
45 | | |
46 | | #pragma once |
47 | | |
48 | | #include "EncCu.h" |
49 | | #include "EncSampleAdaptiveOffset.h" |
50 | | #include "CommonLib/CommonDef.h" |
51 | | #include "CommonLib/Picture.h" |
52 | | #include "InterSearch.h" |
53 | | |
54 | | #include <atomic> |
55 | | |
56 | | //! \ingroup EncoderLib |
57 | | //! \{ |
58 | | |
59 | | namespace vvenc { |
60 | | |
61 | | struct SAOStatData; |
62 | | class EncSampleAdaptiveOffset; |
63 | | class EncAdaptiveLoopFilter; |
64 | | class EncPicture; |
65 | | class NoMallocThreadPool; |
66 | | struct WaitCounter; |
67 | | |
68 | | // ==================================================================================================================== |
69 | | // Class definition |
70 | | // ==================================================================================================================== |
71 | | |
72 | | struct TileLineEncRsrc; |
73 | | struct PerThreadRsrc; |
74 | | struct CtuEncParam; |
75 | | |
76 | | enum TaskType { |
77 | | CTU_ENCODE = 0, |
78 | | LF_VER, |
79 | | LF_HOR, |
80 | | SAO_FILTER, |
81 | | ALF_GET_STATISTICS, |
82 | | ALF_DERIVE_FILTER, |
83 | | ALF_RECONSTRUCT, |
84 | | CCALF_GET_STATISTICS, |
85 | | CCALF_DERIVE_FILTER, |
86 | | CCALF_RECONSTRUCT, |
87 | | FINISH_SLICE, |
88 | | PROCESS_DONE |
89 | | }; |
90 | | |
91 | | using ProcessCtuState = std::atomic<TaskType>; |
92 | | |
93 | | /// CU encoders and scratch buffers for CTU tasks, one set per worker thread. |
94 | | /// A worker runs one task at a time, so the slice encoders of all pictures that are encoded in |
95 | | /// parallel can share these sets instead of owning one each. A set takes about 12 MB with 128x128 |
96 | | /// CTUs at the medium preset, and sharing saves one set per thread for every extra parallel frame. |
97 | | class ThreadRsrcPool |
98 | | { |
99 | | public: |
100 | 1.15k | ThreadRsrcPool() = default; |
101 | | ~ThreadRsrcPool(); |
102 | | |
103 | | ThreadRsrcPool( const ThreadRsrcPool& ) = delete; |
104 | | ThreadRsrcPool& operator=( const ThreadRsrcPool& ) = delete; |
105 | | |
106 | | void init( const VVEncCfg& encCfg, const SPS& sps, const PPS& pps, std::vector<int>* const globalCtuQpVector, RateCtrl& rateCtrl, const int numSets ); |
107 | | PerThreadRsrc& get ( const int threadIdx ) |
108 | 27.9k | { |
109 | 27.9k | CHECK( threadIdx < 0 || threadIdx >= ( int ) m_sets.size(), "no thread resources for this worker thread" ); |
110 | 27.9k | return *m_sets[threadIdx]; |
111 | 27.9k | } |
112 | | |
113 | | private: |
114 | | std::vector<PerThreadRsrc*> m_sets; |
115 | | }; |
116 | | |
117 | | /// slice encoder class |
118 | | class EncSlice |
119 | | { |
120 | | private: |
121 | | // encoder configuration |
122 | | const VVEncCfg* m_pcEncCfg; ///< encoder configuration class |
123 | | |
124 | | ThreadRsrcPool* m_threadRsrc; |
125 | | PicEncState m_picEncState; ///< state that worker threads apply to their CU encoders |
126 | | std::vector<int>* m_globalCtuQpVector; |
127 | | std::vector<TileLineEncRsrc*>m_TileLineEncRsrc; |
128 | | NoMallocThreadPool* m_threadPool; |
129 | | WaitCounter* m_ctuTasksDoneCounter; |
130 | | std::vector<ProcessCtuState> m_processStates; |
131 | | int m_ctuEncDelay; |
132 | | |
133 | | LoopFilter* m_pLoopFilter; |
134 | | EncAdaptiveLoopFilter* m_pALF; |
135 | | RateCtrl* m_pcRateCtrl; |
136 | | BinEncoder m_BinEncoder; |
137 | | CABACWriter m_CABACWriter; |
138 | | |
139 | | Ctx m_entropyCodingSyncContextState; ///< context storage for state of contexts at the wavefront/WPP/entropy-coding-sync second CTU of tile-row used for writing |
140 | | std::vector<Ctx> m_syncPicCtx; ///< context storage for state of contexts at the wavefront/WPP/entropy-coding-sync second CTU of tile-row used for estimation |
141 | | SliceType m_encCABACTableIdx; |
142 | | unsigned m_alfDeriveCtu; |
143 | | unsigned m_ccalfDeriveCtu; |
144 | | |
145 | | double m_saoDisabledRate[ MAX_NUM_COMP ][ VVENC_MAX_TLAYER ]; |
146 | | bool m_saoEnabled[ MAX_NUM_COMP ]; |
147 | | bool m_saoAllDisabled; |
148 | | std::vector<SAOBlkParam> m_saoReconParams; |
149 | | std::vector<SAOStatData**> m_saoStatData; |
150 | | SaoPreFilterEdges m_saoPreFilterEdges; |
151 | | std::vector<CtuEncParam> ctuEncParams; |
152 | | std::vector<int> m_ctuAddrMap; |
153 | | |
154 | | public: |
155 | | EncSlice(); |
156 | | virtual ~EncSlice(); |
157 | | |
158 | | void init ( const VVEncCfg& encCfg, |
159 | | const SPS& sps, |
160 | | const PPS& pps, |
161 | | std::vector<int>* const globalCtuQpVector, |
162 | | LoopFilter& loopFilter, |
163 | | EncAdaptiveLoopFilter& alf, |
164 | | RateCtrl& rateCtrl, |
165 | | NoMallocThreadPool* threadPool, |
166 | | ThreadRsrcPool& threadRsrc, |
167 | | WaitCounter* ctuTasksDoneCounter ); |
168 | | |
169 | | void initPic ( Picture* pic ); |
170 | | |
171 | | // compress and encode slice |
172 | | void compressSlice ( Picture* pic ); ///< analysis stage of slices |
173 | | void encodeSliceData ( Picture* pic ); |
174 | | void saoDisabledRate ( CodingStructure& cs, SAOBlkParam* reconParams ); |
175 | | void finishCompressSlice ( Picture* pic, Slice& slice ); |
176 | | |
177 | | private: |
178 | | void xInitSliceLambdaQP ( Slice* slice ); |
179 | | double xCalculateLambda ( const Slice* slice, const int depth, const double refQP, const double dQP, int& iQP ); |
180 | | void xProcessCtus ( Picture* pic, const unsigned startCtuTsAddr, const unsigned boundingCtuTsAddr ); |
181 | | template<bool checkReadyState=false> |
182 | | static bool xProcessCtuTask ( int taskIdx, void* taskParam ); |
183 | | |
184 | | int xGetQPForPicture ( const Slice* slice ); |
185 | | }; |
186 | | |
187 | | } // namespace vvenc |
188 | | |
189 | | //! \} |
190 | | |