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-2015, 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 getChromasCorrespondingPULumaIdx(const UInt lumaZOrderIdxInCtu, const ChromaFormat chFmt) |
---|
130 | { |
---|
131 | return enable4ChromaPUsInIntraNxNCU(chFmt) ? lumaZOrderIdxInCtu : lumaZOrderIdxInCtu & (~((1<<(2*g_uiAddCUDepth))-1)); //(lumaZOrderIdxInCtu/numParts)*numParts; |
---|
132 | } |
---|
133 | |
---|
134 | //------------------------------------------------ |
---|
135 | |
---|
136 | // 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. |
---|
137 | |
---|
138 | static inline UInt getMaxCUDepthOffset(const ChromaFormat chFmt, const UInt quadtreeTULog2MinSize) |
---|
139 | { |
---|
140 | return (chFmt==CHROMA_422 && quadtreeTULog2MinSize>2) ? 1 : 0; |
---|
141 | } |
---|
142 | |
---|
143 | //====================================================================================================================== |
---|
144 | //Intra prediction ==================================================================================================== |
---|
145 | //====================================================================================================================== |
---|
146 | |
---|
147 | static inline Bool filterIntraReferenceSamples (const ChannelType chType, const ChromaFormat chFmt, const Bool intraReferenceSmoothingDisabled) |
---|
148 | { |
---|
149 | return (!intraReferenceSmoothingDisabled) && (isLuma(chType) || (chFmt == CHROMA_444)); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | //====================================================================================================================== |
---|
154 | //Transform and Quantisation ========================================================================================== |
---|
155 | //====================================================================================================================== |
---|
156 | |
---|
157 | static inline Bool TUCompRectHasAssociatedTransformSkipFlag(const TComRectangle &rectSamples, const UInt transformSkipLog2MaxSize) |
---|
158 | { |
---|
159 | return (rectSamples.width <= (1<<transformSkipLog2MaxSize)); |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | //------------------------------------------------ |
---|
164 | |
---|
165 | static inline Int getTransformShift(const Int channelBitDepth, const UInt uiLog2TrSize, const Int maxLog2TrDynamicRange) |
---|
166 | { |
---|
167 | return maxLog2TrDynamicRange - channelBitDepth - uiLog2TrSize; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | //------------------------------------------------ |
---|
172 | |
---|
173 | static inline Int getScaledChromaQP(Int unscaledChromaQP, const ChromaFormat chFmt) |
---|
174 | { |
---|
175 | return g_aucChromaScale[chFmt][Clip3(0, (chromaQPMappingTableSize - 1), unscaledChromaQP)]; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | //====================================================================================================================== |
---|
180 | //Scaling lists ======================================================================================================= |
---|
181 | //====================================================================================================================== |
---|
182 | |
---|
183 | static inline Int getScalingListType(const PredMode predMode, const ComponentID compID) |
---|
184 | { |
---|
185 | return ((predMode != MODE_INTER) ? 0 : MAX_NUM_COMPONENT) + compID; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | //------------------------------------------------ |
---|
190 | |
---|
191 | |
---|
192 | //====================================================================================================================== |
---|
193 | //Context variable selection ========================================================================================== |
---|
194 | //====================================================================================================================== |
---|
195 | |
---|
196 | //context variable source tables |
---|
197 | |
---|
198 | static const UInt significanceMapContextStartTable[MAX_NUM_CHANNEL_TYPE] = {FIRST_SIG_FLAG_CTX_LUMA, FIRST_SIG_FLAG_CTX_CHROMA}; |
---|
199 | static const UInt contextSetStartTable [MAX_NUM_CHANNEL_TYPE] = {FIRST_CTX_SET_LUMA, FIRST_CTX_SET_CHROMA }; |
---|
200 | static const UInt CBFContextStartTable [MAX_NUM_CHANNEL_TYPE] = {FIRST_CBF_CTX_LUMA, FIRST_CBF_CTX_CHROMA }; |
---|
201 | |
---|
202 | |
---|
203 | //------------------------------------------------ |
---|
204 | |
---|
205 | //Function for last-significant-coefficient context selection parameters |
---|
206 | |
---|
207 | static inline Void getLastSignificantContextParameters (const ComponentID component, |
---|
208 | const Int width, |
---|
209 | const Int height, |
---|
210 | Int &result_offsetX, |
---|
211 | Int &result_offsetY, |
---|
212 | Int &result_shiftX, |
---|
213 | Int &result_shiftY) |
---|
214 | { |
---|
215 | const UInt convertedWidth = g_aucConvertToBit[width]; |
---|
216 | const UInt convertedHeight = g_aucConvertToBit[height]; |
---|
217 | |
---|
218 | result_offsetX = (isChroma(component)) ? 0 : ((convertedWidth * 3) + ((convertedWidth + 1) >> 2)); |
---|
219 | result_offsetY = (isChroma(component)) ? 0 : ((convertedHeight * 3) + ((convertedHeight + 1) >> 2)); |
---|
220 | result_shiftX = (isChroma(component)) ? convertedWidth : ((convertedWidth + 3) >> 2); |
---|
221 | result_shiftY = (isChroma(component)) ? convertedHeight : ((convertedHeight + 3) >> 2); |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | //------------------------------------------------ |
---|
226 | |
---|
227 | //Function for significance map context index offset selection |
---|
228 | |
---|
229 | static inline UInt getSignificanceMapContextOffset (const ComponentID component) |
---|
230 | { |
---|
231 | return significanceMapContextStartTable[toChannelType(component)]; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | //------------------------------------------------ |
---|
236 | |
---|
237 | // Function for greater-than-one map/greater-than-two map context set selection |
---|
238 | |
---|
239 | static inline UInt getContextSetIndex (const ComponentID component, |
---|
240 | const UInt subsetIndex, |
---|
241 | const Bool foundACoefficientGreaterThan1) |
---|
242 | { |
---|
243 | const UInt notFirstSubsetOffset = (isLuma(component) && (subsetIndex > 0)) ? 2 : 0; |
---|
244 | const UInt foundAGreaterThan1Offset = foundACoefficientGreaterThan1 ? 1 : 0; |
---|
245 | |
---|
246 | return contextSetStartTable[toChannelType(component)] + notFirstSubsetOffset + foundAGreaterThan1Offset; |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | //------------------------------------------------ |
---|
251 | |
---|
252 | //Function for CBF context index offset |
---|
253 | |
---|
254 | static inline UInt getCBFContextOffset (const ComponentID component) |
---|
255 | { |
---|
256 | return CBFContextStartTable[toChannelType(component)]; |
---|
257 | } |
---|
258 | |
---|
259 | |
---|
260 | //====================================================================================================================== |
---|
261 | //Entropy coding parameters ============================================================================================ |
---|
262 | //====================================================================================================================== |
---|
263 | |
---|
264 | Void getTUEntropyCodingParameters( TUEntropyCodingParameters &result, |
---|
265 | class TComTU &rTu, |
---|
266 | const ComponentID component); |
---|
267 | |
---|
268 | |
---|
269 | //====================================================================================================================== |
---|
270 | //End ================================================================================================================= |
---|
271 | //====================================================================================================================== |
---|
272 | |
---|
273 | #endif |
---|