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-2014, 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 | /** \file TEncAnalyze.h |
---|
35 | \brief encoder analyzer class (header) |
---|
36 | */ |
---|
37 | |
---|
38 | #ifndef __TENCANALYZE__ |
---|
39 | #define __TENCANALYZE__ |
---|
40 | |
---|
41 | #if _MSC_VER > 1000 |
---|
42 | #pragma once |
---|
43 | #endif // _MSC_VER > 1000 |
---|
44 | |
---|
45 | #include <stdio.h> |
---|
46 | #include <memory.h> |
---|
47 | #include <assert.h> |
---|
48 | #include "TLibCommon/CommonDef.h" |
---|
49 | #include "TLibCommon/TComChromaFormat.h" |
---|
50 | #include "math.h" |
---|
51 | |
---|
52 | //! \ingroup TLibEncoder |
---|
53 | //! \{ |
---|
54 | |
---|
55 | // ==================================================================================================================== |
---|
56 | // Class definition |
---|
57 | // ==================================================================================================================== |
---|
58 | |
---|
59 | /// encoder analyzer class |
---|
60 | class TEncAnalyze |
---|
61 | { |
---|
62 | private: |
---|
63 | Double m_dPSNRSum[MAX_NUM_COMPONENT]; |
---|
64 | Double m_dAddBits; |
---|
65 | UInt m_uiNumPic; |
---|
66 | Double m_dFrmRate; //--CFG_KDY |
---|
67 | Double m_MSEyuvframe[MAX_NUM_COMPONENT]; // sum of MSEs |
---|
68 | |
---|
69 | public: |
---|
70 | virtual ~TEncAnalyze() {} |
---|
71 | TEncAnalyze() { clear(); } |
---|
72 | |
---|
73 | Void addResult( Double psnr[MAX_NUM_COMPONENT], Double bits, const Double MSEyuvframe[MAX_NUM_COMPONENT]) |
---|
74 | { |
---|
75 | m_dAddBits += bits; |
---|
76 | for(UInt i=0; i<MAX_NUM_COMPONENT; i++) |
---|
77 | { |
---|
78 | m_dPSNRSum[i] += psnr[i]; |
---|
79 | m_MSEyuvframe[i] += MSEyuvframe[i]; |
---|
80 | } |
---|
81 | |
---|
82 | m_uiNumPic++; |
---|
83 | } |
---|
84 | |
---|
85 | Double getPsnr(ComponentID compID) const { return m_dPSNRSum[compID]; } |
---|
86 | Double getBits() const { return m_dAddBits; } |
---|
87 | Void setBits(Double numBits) { m_dAddBits=numBits; } |
---|
88 | UInt getNumPic() const { return m_uiNumPic; } |
---|
89 | |
---|
90 | Void setFrmRate (Double dFrameRate) { m_dFrmRate = dFrameRate; } //--CFG_KDY |
---|
91 | Void clear() |
---|
92 | { |
---|
93 | m_dAddBits = 0; |
---|
94 | for(UInt i=0; i<MAX_NUM_COMPONENT; i++) |
---|
95 | { |
---|
96 | m_dPSNRSum[i] = 0; |
---|
97 | m_MSEyuvframe[i] = 0; |
---|
98 | } |
---|
99 | m_uiNumPic = 0; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | Void calculateCombinedValues(const ChromaFormat chFmt, Double &PSNRyuv, Double &MSEyuv) |
---|
104 | { |
---|
105 | MSEyuv = 0; |
---|
106 | Int scale = 0; |
---|
107 | |
---|
108 | Int maximumBitDepth = g_bitDepth[0]; |
---|
109 | for (UInt channelTypeIndex = 1; channelTypeIndex < MAX_NUM_CHANNEL_TYPE; channelTypeIndex++) |
---|
110 | if (g_bitDepth[channelTypeIndex] > maximumBitDepth) |
---|
111 | maximumBitDepth = g_bitDepth[channelTypeIndex]; |
---|
112 | |
---|
113 | const UInt maxval = 255 << (maximumBitDepth - 8); |
---|
114 | const UInt numberValidComponents = getNumberValidComponents(chFmt); |
---|
115 | |
---|
116 | for (UInt comp=0; comp<numberValidComponents; comp++) |
---|
117 | { |
---|
118 | const ComponentID compID = ComponentID(comp); |
---|
119 | const UInt csx = getComponentScaleX(compID, chFmt); |
---|
120 | const UInt csy = getComponentScaleY(compID, chFmt); |
---|
121 | const Int scaleChan = (4>>(csx+csy)); |
---|
122 | const UInt bitDepthShift = 2 * (maximumBitDepth - g_bitDepth[toChannelType(compID)]); //*2 because this is a squared number |
---|
123 | |
---|
124 | const Double channelMSE = (m_MSEyuvframe[compID] * Double(1 << bitDepthShift)) / Double(getNumPic()); |
---|
125 | |
---|
126 | scale += scaleChan; |
---|
127 | MSEyuv += scaleChan * channelMSE; |
---|
128 | } |
---|
129 | |
---|
130 | MSEyuv /= Double(scale); // i.e. divide by 6 for 4:2:0, 8 for 4:2:2 etc. |
---|
131 | PSNRyuv = (MSEyuv==0 ? 999.99 : 10*log10((maxval*maxval)/MSEyuv)); |
---|
132 | } |
---|
133 | |
---|
134 | #if SVC_EXTENSION |
---|
135 | Void printOut ( Char cDelim, const ChromaFormat chFmt, const Bool printMSEBasedSNR, const Bool printSequenceMSE, UInt layer ) |
---|
136 | #else |
---|
137 | Void printOut ( Char cDelim, const ChromaFormat chFmt, const Bool printMSEBasedSNR, const Bool printSequenceMSE ) |
---|
138 | #endif |
---|
139 | { |
---|
140 | Double dFps = m_dFrmRate; //--CFG_KDY |
---|
141 | Double dScale = dFps / 1000 / (Double)m_uiNumPic; |
---|
142 | |
---|
143 | Double MSEBasedSNR[MAX_NUM_COMPONENT]; |
---|
144 | if (printMSEBasedSNR) |
---|
145 | { |
---|
146 | for (UInt componentIndex = 0; componentIndex < MAX_NUM_COMPONENT; componentIndex++) |
---|
147 | { |
---|
148 | const ComponentID compID = ComponentID(componentIndex); |
---|
149 | |
---|
150 | if (getNumPic() == 0) MSEBasedSNR[compID] = 0 * dScale; // this is the same calculation that will be evaluated for any other statistic when there are no frames (it should result in NaN). We use it here so all the output is consistent. |
---|
151 | else |
---|
152 | { |
---|
153 | //NOTE: this is not the true maximum value for any bitDepth other than 8. It comes from the original HM PSNR calculation |
---|
154 | const UInt maxval = 255 << (g_bitDepth[toChannelType(compID)] - 8); |
---|
155 | const Double MSE = m_MSEyuvframe[compID]; |
---|
156 | |
---|
157 | MSEBasedSNR[compID] = (MSE == 0) ? 999.99 : (10 * log10((maxval * maxval) / (MSE / (Double)getNumPic()))); |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | switch (chFmt) |
---|
163 | { |
---|
164 | case CHROMA_400: |
---|
165 | if (printMSEBasedSNR) |
---|
166 | { |
---|
167 | printf( " \tTotal Frames | " "Bitrate " "Y-PSNR" ); |
---|
168 | |
---|
169 | if (printSequenceMSE) printf( " Y-MSE\n" ); |
---|
170 | else printf("\n"); |
---|
171 | |
---|
172 | //printf( "\t------------ " " ----------" " -------- " " -------- " " --------\n" ); |
---|
173 | printf( "Average: \t %8d %c " "%12.4lf " "%8.4lf", |
---|
174 | getNumPic(), cDelim, |
---|
175 | getBits() * dScale, |
---|
176 | getPsnr(COMPONENT_Y) / (Double)getNumPic() ); |
---|
177 | |
---|
178 | if (printSequenceMSE) |
---|
179 | { |
---|
180 | printf( " %8.4lf\n", m_MSEyuvframe[COMPONENT_Y ] / (Double)getNumPic() ); |
---|
181 | } |
---|
182 | else printf("\n"); |
---|
183 | |
---|
184 | printf( "From MSE:\t %8d %c " "%12.4lf " "%8.4lf\n", |
---|
185 | getNumPic(), cDelim, |
---|
186 | getBits() * dScale, |
---|
187 | MSEBasedSNR[COMPONENT_Y] ); |
---|
188 | } |
---|
189 | else |
---|
190 | { |
---|
191 | printf( "\tTotal Frames | " "Bitrate " "Y-PSNR" ); |
---|
192 | |
---|
193 | if (printSequenceMSE) printf( " Y-MSE\n" ); |
---|
194 | else printf("\n"); |
---|
195 | |
---|
196 | //printf( "\t------------ " " ----------" " -------- " " -------- " " --------\n" ); |
---|
197 | #if SVC_EXTENSION |
---|
198 | printf( " L%d\t %8d %c " "%12.4lf " "%8.4lf", |
---|
199 | layer, |
---|
200 | #else |
---|
201 | printf( "\t %8d %c " "%12.4lf " "%8.4lf", |
---|
202 | #endif |
---|
203 | getNumPic(), cDelim, |
---|
204 | getBits() * dScale, |
---|
205 | getPsnr(COMPONENT_Y) / (Double)getNumPic() ); |
---|
206 | |
---|
207 | if (printSequenceMSE) |
---|
208 | { |
---|
209 | printf( " %8.4lf\n", m_MSEyuvframe[COMPONENT_Y ] / (Double)getNumPic() ); |
---|
210 | } |
---|
211 | else printf("\n"); |
---|
212 | } |
---|
213 | break; |
---|
214 | case CHROMA_420: |
---|
215 | case CHROMA_422: |
---|
216 | case CHROMA_444: |
---|
217 | { |
---|
218 | Double PSNRyuv = MAX_DOUBLE; |
---|
219 | Double MSEyuv = MAX_DOUBLE; |
---|
220 | |
---|
221 | calculateCombinedValues(chFmt, PSNRyuv, MSEyuv); |
---|
222 | |
---|
223 | if (printMSEBasedSNR) |
---|
224 | { |
---|
225 | printf( " \tTotal Frames | " "Bitrate " "Y-PSNR " "U-PSNR " "V-PSNR " "YUV-PSNR " ); |
---|
226 | |
---|
227 | if (printSequenceMSE) printf( " Y-MSE " "U-MSE " "V-MSE " "YUV-MSE \n" ); |
---|
228 | else printf("\n"); |
---|
229 | |
---|
230 | //printf( "\t------------ " " ----------" " -------- " " -------- " " --------\n" ); |
---|
231 | printf( "Average: \t %8d %c " "%12.4lf " "%8.4lf " "%8.4lf " "%8.4lf " "%8.4lf", |
---|
232 | getNumPic(), cDelim, |
---|
233 | getBits() * dScale, |
---|
234 | getPsnr(COMPONENT_Y) / (Double)getNumPic(), |
---|
235 | getPsnr(COMPONENT_Cb) / (Double)getNumPic(), |
---|
236 | getPsnr(COMPONENT_Cr) / (Double)getNumPic(), |
---|
237 | PSNRyuv ); |
---|
238 | |
---|
239 | if (printSequenceMSE) |
---|
240 | { |
---|
241 | printf( " %8.4lf " "%8.4lf " "%8.4lf " "%8.4lf\n", |
---|
242 | m_MSEyuvframe[COMPONENT_Y ] / (Double)getNumPic(), |
---|
243 | m_MSEyuvframe[COMPONENT_Cb] / (Double)getNumPic(), |
---|
244 | m_MSEyuvframe[COMPONENT_Cr] / (Double)getNumPic(), |
---|
245 | MSEyuv ); |
---|
246 | } |
---|
247 | else printf("\n"); |
---|
248 | |
---|
249 | printf( "From MSE:\t %8d %c " "%12.4lf " "%8.4lf " "%8.4lf " "%8.4lf " "%8.4lf\n", |
---|
250 | getNumPic(), cDelim, |
---|
251 | getBits() * dScale, |
---|
252 | MSEBasedSNR[COMPONENT_Y], |
---|
253 | MSEBasedSNR[COMPONENT_Cb], |
---|
254 | MSEBasedSNR[COMPONENT_Cr], |
---|
255 | PSNRyuv ); |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | printf( "\tTotal Frames | " "Bitrate " "Y-PSNR " "U-PSNR " "V-PSNR " "YUV-PSNR " ); |
---|
260 | |
---|
261 | if (printSequenceMSE) printf( " Y-MSE " "U-MSE " "V-MSE " "YUV-MSE \n" ); |
---|
262 | else printf("\n"); |
---|
263 | |
---|
264 | //printf( "\t------------ " " ----------" " -------- " " -------- " " --------\n" ); |
---|
265 | printf( "\t %8d %c " "%12.4lf " "%8.4lf " "%8.4lf " "%8.4lf " "%8.4lf", |
---|
266 | getNumPic(), cDelim, |
---|
267 | getBits() * dScale, |
---|
268 | getPsnr(COMPONENT_Y) / (Double)getNumPic(), |
---|
269 | getPsnr(COMPONENT_Cb) / (Double)getNumPic(), |
---|
270 | getPsnr(COMPONENT_Cr) / (Double)getNumPic(), |
---|
271 | PSNRyuv ); |
---|
272 | |
---|
273 | if (printSequenceMSE) |
---|
274 | { |
---|
275 | printf( " %8.4lf " "%8.4lf " "%8.4lf " "%8.4lf\n", |
---|
276 | m_MSEyuvframe[COMPONENT_Y ] / (Double)getNumPic(), |
---|
277 | m_MSEyuvframe[COMPONENT_Cb] / (Double)getNumPic(), |
---|
278 | m_MSEyuvframe[COMPONENT_Cr] / (Double)getNumPic(), |
---|
279 | MSEyuv ); |
---|
280 | } |
---|
281 | else printf("\n"); |
---|
282 | } |
---|
283 | } |
---|
284 | break; |
---|
285 | default: |
---|
286 | fprintf(stderr, "Unknown format during print out\n"); |
---|
287 | exit(1); |
---|
288 | break; |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | Void printSummary(const ChromaFormat chFmt, const Bool printSequenceMSE, Char ch='T') |
---|
294 | { |
---|
295 | FILE* pFile = NULL; |
---|
296 | |
---|
297 | switch( ch ) |
---|
298 | { |
---|
299 | case 'T': |
---|
300 | pFile = fopen ("summaryTotal.txt", "at"); |
---|
301 | break; |
---|
302 | case 'I': |
---|
303 | pFile = fopen ("summary_I.txt", "at"); |
---|
304 | break; |
---|
305 | case 'P': |
---|
306 | pFile = fopen ("summary_P.txt", "at"); |
---|
307 | break; |
---|
308 | case 'B': |
---|
309 | pFile = fopen ("summary_B.txt", "at"); |
---|
310 | break; |
---|
311 | default: |
---|
312 | assert(0); |
---|
313 | return; |
---|
314 | break; |
---|
315 | } |
---|
316 | |
---|
317 | Double dFps = m_dFrmRate; //--CFG_KDY |
---|
318 | Double dScale = dFps / 1000 / (Double)m_uiNumPic; |
---|
319 | switch (chFmt) |
---|
320 | { |
---|
321 | case CHROMA_400: |
---|
322 | fprintf(pFile, "%f\t %f\n", |
---|
323 | getBits() * dScale, |
---|
324 | getPsnr(COMPONENT_Y) / (Double)getNumPic() ); |
---|
325 | break; |
---|
326 | case CHROMA_420: |
---|
327 | case CHROMA_422: |
---|
328 | case CHROMA_444: |
---|
329 | { |
---|
330 | Double PSNRyuv = MAX_DOUBLE; |
---|
331 | Double MSEyuv = MAX_DOUBLE; |
---|
332 | |
---|
333 | calculateCombinedValues(chFmt, PSNRyuv, MSEyuv); |
---|
334 | |
---|
335 | fprintf(pFile, "%f\t %f\t %f\t %f\t %f", |
---|
336 | getBits() * dScale, |
---|
337 | getPsnr(COMPONENT_Y) / (Double)getNumPic(), |
---|
338 | getPsnr(COMPONENT_Cb) / (Double)getNumPic(), |
---|
339 | getPsnr(COMPONENT_Cr) / (Double)getNumPic(), |
---|
340 | PSNRyuv ); |
---|
341 | |
---|
342 | if (printSequenceMSE) |
---|
343 | { |
---|
344 | fprintf(pFile, "\t %f\t %f\t %f\t %f\n", |
---|
345 | m_MSEyuvframe[COMPONENT_Y ] / (Double)getNumPic(), |
---|
346 | m_MSEyuvframe[COMPONENT_Cb] / (Double)getNumPic(), |
---|
347 | m_MSEyuvframe[COMPONENT_Cr] / (Double)getNumPic(), |
---|
348 | MSEyuv ); |
---|
349 | } |
---|
350 | else fprintf(pFile, "\n"); |
---|
351 | |
---|
352 | break; |
---|
353 | } |
---|
354 | |
---|
355 | default: |
---|
356 | fprintf(stderr, "Unknown format during print out\n"); |
---|
357 | exit(1); |
---|
358 | break; |
---|
359 | } |
---|
360 | |
---|
361 | fclose(pFile); |
---|
362 | } |
---|
363 | }; |
---|
364 | |
---|
365 | #if SVC_EXTENSION |
---|
366 | extern TEncAnalyze m_gcAnalyzeAll [MAX_LAYERS]; |
---|
367 | extern TEncAnalyze m_gcAnalyzeI [MAX_LAYERS]; |
---|
368 | extern TEncAnalyze m_gcAnalyzeP [MAX_LAYERS]; |
---|
369 | extern TEncAnalyze m_gcAnalyzeB [MAX_LAYERS]; |
---|
370 | #else |
---|
371 | extern TEncAnalyze m_gcAnalyzeAll; |
---|
372 | extern TEncAnalyze m_gcAnalyzeI; |
---|
373 | extern TEncAnalyze m_gcAnalyzeP; |
---|
374 | extern TEncAnalyze m_gcAnalyzeB; |
---|
375 | #endif |
---|
376 | |
---|
377 | extern TEncAnalyze m_gcAnalyzeAll_in; |
---|
378 | |
---|
379 | //! \} |
---|
380 | |
---|
381 | #endif // !defined(AFX_TENCANALYZE_H__C79BCAA2_6AC8_4175_A0FE_CF02F5829233__INCLUDED_) |
---|