1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2017, ITU/ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | #ifndef __TCOMCHROMAFORMAT__ |
---|
35 | #define __TCOMCHROMAFORMAT__ |
---|
36 | |
---|
37 | #include "CommonDef.h" |
---|
38 | #include "TComRectangle.h" |
---|
39 | #include "ContextTables.h" |
---|
40 | #include "TComRom.h" |
---|
41 | #include <iostream> |
---|
42 | #include <vector> |
---|
43 | #include <assert.h> |
---|
44 | #include "Debug.h" |
---|
45 | |
---|
46 | //====================================================================================================================== |
---|
47 | //Chroma format utility functions ===================================================================================== |
---|
48 | //====================================================================================================================== |
---|
49 | |
---|
50 | class TComDataCU; |
---|
51 | |
---|
52 | |
---|
53 | static inline ChannelType toChannelType (const ComponentID id) { return (id==COMPONENT_Y)? CHANNEL_TYPE_LUMA : CHANNEL_TYPE_CHROMA; } |
---|
54 | static inline Bool isLuma (const ComponentID id) { return (id==COMPONENT_Y); } |
---|
55 | static inline Bool isLuma (const ChannelType id) { return (id==CHANNEL_TYPE_LUMA); } |
---|
56 | static inline Bool isChroma (const ComponentID id) { return (id!=COMPONENT_Y); } |
---|
57 | static inline Bool isChroma (const ChannelType id) { return (id!=CHANNEL_TYPE_LUMA); } |
---|
58 | static inline UInt getChannelTypeScaleX (const ChannelType id, const ChromaFormat fmt) { return (isLuma(id) || (fmt==CHROMA_444)) ? 0 : 1; } |
---|
59 | static inline UInt getChannelTypeScaleY (const ChannelType id, const ChromaFormat fmt) { return (isLuma(id) || (fmt!=CHROMA_420)) ? 0 : 1; } |
---|
60 | static inline UInt getComponentScaleX (const ComponentID id, const ChromaFormat fmt) { return getChannelTypeScaleX(toChannelType(id), fmt); } |
---|
61 | static inline UInt getComponentScaleY (const ComponentID id, const ChromaFormat fmt) { return getChannelTypeScaleY(toChannelType(id), fmt); } |
---|
62 | static inline UInt getNumberValidChannelTypes(const ChromaFormat fmt) { return (fmt==CHROMA_400) ? 1 : MAX_NUM_CHANNEL_TYPE; } |
---|
63 | static inline UInt getNumberValidComponents (const ChromaFormat fmt) { return (fmt==CHROMA_400) ? 1 : MAX_NUM_COMPONENT; } |
---|
64 | static inline Bool isChromaEnabled (const ChromaFormat fmt) { return fmt!=CHROMA_400; } |
---|
65 | static inline ComponentID getFirstComponentOfChannel(const ChannelType id) { return (isLuma(id) ? COMPONENT_Y : COMPONENT_Cb); } |
---|
66 | |
---|
67 | InputColourSpaceConversion stringToInputColourSpaceConvert(const std::string &value, const Bool bIsForward); |
---|
68 | std::string getListOfColourSpaceConverts(const Bool bIsForward); |
---|
69 | |
---|
70 | //------------------------------------------------ |
---|
71 | |
---|
72 | static inline UInt getTotalSamples(const UInt width, const UInt height, const ChromaFormat format) |
---|
73 | { |
---|
74 | const UInt samplesPerChannel = width * height; |
---|
75 | |
---|
76 | switch (format) |
---|
77 | { |
---|
78 | case CHROMA_400: return samplesPerChannel; break; |
---|
79 | case CHROMA_420: return (samplesPerChannel * 3) >> 1; break; |
---|
80 | case CHROMA_422: return samplesPerChannel * 2; break; |
---|
81 | case CHROMA_444: return samplesPerChannel * 3; break; |
---|
82 | default: |
---|
83 | std::cerr << "ERROR: Unrecognised chroma format in getTotalSamples()" << std::endl; |
---|
84 | exit(1); |
---|
85 | break; |
---|
86 | } |
---|
87 | |
---|
88 | return MAX_UINT; |
---|
89 | } |
---|
90 | |
---|
91 | //------------------------------------------------ |
---|
92 | |
---|
93 | static inline UInt getTotalBits(const UInt width, const UInt height, const ChromaFormat format, const Int bitDepths[MAX_NUM_CHANNEL_TYPE]) |
---|
94 | { |
---|
95 | const UInt samplesPerChannel = width * height; |
---|
96 | |
---|
97 | switch (format) |
---|
98 | { |
---|
99 | case CHROMA_400: return samplesPerChannel * bitDepths[CHANNEL_TYPE_LUMA]; break; |
---|
100 | case CHROMA_420: return (samplesPerChannel * (bitDepths[CHANNEL_TYPE_LUMA]*2 + bitDepths[CHANNEL_TYPE_CHROMA]) ) >> 1; break; |
---|
101 | case CHROMA_422: return samplesPerChannel * (bitDepths[CHANNEL_TYPE_LUMA] + bitDepths[CHANNEL_TYPE_CHROMA]); break; |
---|
102 | case CHROMA_444: return samplesPerChannel * (bitDepths[CHANNEL_TYPE_LUMA] + 2*bitDepths[CHANNEL_TYPE_CHROMA]); break; |
---|
103 | default: |
---|
104 | std::cerr << "ERROR: Unrecognised chroma format in getTotalSamples()" << std::endl; |
---|
105 | exit(1); |
---|
106 | break; |
---|
107 | } |
---|
108 | |
---|
109 | return MAX_UINT; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | //------------------------------------------------ |
---|
114 | |
---|
115 | // In HM, a CU only has one chroma intra prediction direction, that corresponds to the top left luma intra prediction |
---|
116 | // even if the NxN PU split occurs when 4 sub-TUs exist for chroma. |
---|
117 | // Use this function to allow NxN PU splitting for chroma. |
---|
118 | |
---|
119 | static inline Bool enable4ChromaPUsInIntraNxNCU(const ChromaFormat chFmt) |
---|
120 | { |
---|
121 | return (chFmt == CHROMA_444); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | //------------------------------------------------ |
---|
126 | |
---|
127 | //returns the part index of the luma region that is co-located with the specified chroma region |
---|
128 | |
---|
129 | static inline UInt |
---|
130 | getChromasCorrespondingPULumaIdx(const UInt lumaZOrderIdxInCtu, |
---|
131 | const ChromaFormat chFmt, |
---|
132 | const Int partsPerMinCU // 1<<(2*(sps->getMaxTotalCUDepth() - sps->getLog2DiffMaxMinCodingBlockSize())) |
---|
133 | ) |
---|
134 | { |
---|
135 | return enable4ChromaPUsInIntraNxNCU(chFmt) ? lumaZOrderIdxInCtu : lumaZOrderIdxInCtu & (~(partsPerMinCU-1)); |
---|
136 | } |
---|
137 | |
---|
138 | //------------------------------------------------ |
---|
139 | |
---|
140 | // If chroma format is 4:2:2 and a chroma-square-sub-tu is possible for the smallest TU, then increase the depth by 1 to allow for more parts. |
---|
141 | |
---|
142 | static inline UInt getMaxCUDepthOffset(const ChromaFormat chFmt, const UInt quadtreeTULog2MinSize) |
---|
143 | { |
---|
144 | return (chFmt==CHROMA_422 && quadtreeTULog2MinSize>2) ? 1 : 0; |
---|
145 | } |
---|
146 | |
---|
147 | //====================================================================================================================== |
---|
148 | //Intra prediction ==================================================================================================== |
---|
149 | //====================================================================================================================== |
---|
150 | |
---|
151 | static inline Bool filterIntraReferenceSamples (const ChannelType chType, const ChromaFormat chFmt, const Bool intraReferenceSmoothingDisabled) |
---|
152 | { |
---|
153 | return (!intraReferenceSmoothingDisabled) && (isLuma(chType) || (chFmt == CHROMA_444)); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | //====================================================================================================================== |
---|
158 | //Transform and Quantisation ========================================================================================== |
---|
159 | //====================================================================================================================== |
---|
160 | |
---|
161 | static inline Bool TUCompRectHasAssociatedTransformSkipFlag(const TComRectangle &rectSamples, const UInt transformSkipLog2MaxSize) |
---|
162 | { |
---|
163 | return (rectSamples.width <= (1<<transformSkipLog2MaxSize)); |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | //------------------------------------------------ |
---|
168 | |
---|
169 | static inline Int getTransformShift(const Int channelBitDepth, const UInt uiLog2TrSize, const Int maxLog2TrDynamicRange) |
---|
170 | { |
---|
171 | return maxLog2TrDynamicRange - channelBitDepth - uiLog2TrSize; |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | //------------------------------------------------ |
---|
176 | |
---|
177 | static inline Int getScaledChromaQP(Int unscaledChromaQP, const ChromaFormat chFmt) |
---|
178 | { |
---|
179 | return g_aucChromaScale[chFmt][Clip3(0, (chromaQPMappingTableSize - 1), unscaledChromaQP)]; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | //====================================================================================================================== |
---|
184 | //Scaling lists ======================================================================================================= |
---|
185 | //====================================================================================================================== |
---|
186 | |
---|
187 | static inline Int getScalingListType(const PredMode predMode, const ComponentID compID) |
---|
188 | { |
---|
189 | return ((predMode != MODE_INTER) ? 0 : MAX_NUM_COMPONENT) + compID; |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | //------------------------------------------------ |
---|
194 | |
---|
195 | |
---|
196 | //====================================================================================================================== |
---|
197 | //Context variable selection ========================================================================================== |
---|
198 | //====================================================================================================================== |
---|
199 | |
---|
200 | //context variable source tables |
---|
201 | |
---|
202 | static const UInt significanceMapContextStartTable[MAX_NUM_CHANNEL_TYPE] = {FIRST_SIG_FLAG_CTX_LUMA, FIRST_SIG_FLAG_CTX_CHROMA}; |
---|
203 | static const UInt contextSetStartTable [MAX_NUM_CHANNEL_TYPE] = {FIRST_CTX_SET_LUMA, FIRST_CTX_SET_CHROMA }; |
---|
204 | static const UInt CBFContextStartTable [MAX_NUM_CHANNEL_TYPE] = {FIRST_CBF_CTX_LUMA, FIRST_CBF_CTX_CHROMA }; |
---|
205 | |
---|
206 | |
---|
207 | //------------------------------------------------ |
---|
208 | |
---|
209 | //Function for last-significant-coefficient context selection parameters |
---|
210 | |
---|
211 | static inline Void getLastSignificantContextParameters (const ComponentID component, |
---|
212 | const Int width, |
---|
213 | const Int height, |
---|
214 | Int &result_offsetX, |
---|
215 | Int &result_offsetY, |
---|
216 | Int &result_shiftX, |
---|
217 | Int &result_shiftY) |
---|
218 | { |
---|
219 | const UInt convertedWidth = g_aucConvertToBit[width]; |
---|
220 | const UInt convertedHeight = g_aucConvertToBit[height]; |
---|
221 | |
---|
222 | result_offsetX = (isChroma(component)) ? 0 : ((convertedWidth * 3) + ((convertedWidth + 1) >> 2)); |
---|
223 | result_offsetY = (isChroma(component)) ? 0 : ((convertedHeight * 3) + ((convertedHeight + 1) >> 2)); |
---|
224 | result_shiftX = (isChroma(component)) ? convertedWidth : ((convertedWidth + 3) >> 2); |
---|
225 | result_shiftY = (isChroma(component)) ? convertedHeight : ((convertedHeight + 3) >> 2); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | //------------------------------------------------ |
---|
230 | |
---|
231 | //Function for significance map context index offset selection |
---|
232 | |
---|
233 | static inline UInt getSignificanceMapContextOffset (const ComponentID component) |
---|
234 | { |
---|
235 | return significanceMapContextStartTable[toChannelType(component)]; |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | //------------------------------------------------ |
---|
240 | |
---|
241 | // Function for greater-than-one map/greater-than-two map context set selection |
---|
242 | |
---|
243 | static inline UInt getContextSetIndex (const ComponentID component, |
---|
244 | const UInt subsetIndex, |
---|
245 | const Bool foundACoefficientGreaterThan1) |
---|
246 | { |
---|
247 | const UInt notFirstSubsetOffset = (isLuma(component) && (subsetIndex > 0)) ? 2 : 0; |
---|
248 | const UInt foundAGreaterThan1Offset = foundACoefficientGreaterThan1 ? 1 : 0; |
---|
249 | |
---|
250 | return contextSetStartTable[toChannelType(component)] + notFirstSubsetOffset + foundAGreaterThan1Offset; |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | //------------------------------------------------ |
---|
255 | |
---|
256 | //Function for CBF context index offset |
---|
257 | |
---|
258 | static inline UInt getCBFContextOffset (const ComponentID component) |
---|
259 | { |
---|
260 | return CBFContextStartTable[toChannelType(component)]; |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | //====================================================================================================================== |
---|
265 | //Entropy coding parameters ============================================================================================ |
---|
266 | //====================================================================================================================== |
---|
267 | |
---|
268 | Void getTUEntropyCodingParameters( TUEntropyCodingParameters &result, |
---|
269 | class TComTU &rTu, |
---|
270 | const ComponentID component); |
---|
271 | |
---|
272 | |
---|
273 | //====================================================================================================================== |
---|
274 | //End ================================================================================================================= |
---|
275 | //====================================================================================================================== |
---|
276 | |
---|
277 | #endif |
---|