1 /*!
2 * \copy
3 * Copyright (c) 2009-2013, Cisco Systems
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * \file utils.c
33 *
34 * \brief common tool/function utilization
35 *
36 * \date 03/10/2009 Created
37 *
38 *************************************************************************************
39 */
40 #include "utils.h"
41 #include "crt_util_safe_x.h" // Safe CRT routines like utils for cross platforms
42 #include "codec_app_def.h"
43 float WelsCalcPsnr (const void* kpTarPic,
44 const int32_t kiTarStride,
45 const void* kpRefPic,
46 const int32_t kiRefStride,
47 const int32_t kiWidth,
48 const int32_t kiHeight);
49
50
WelsLog(SLogContext * logCtx,int32_t iLevel,const char * kpFmt,...)51 void WelsLog (SLogContext* logCtx, int32_t iLevel, const char* kpFmt, ...) {
52 va_list vl;
53 char pTraceTag[MAX_LOG_SIZE] = {0};
54 switch (iLevel) {
55 case WELS_LOG_ERROR:
56 WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Error:", logCtx->pCodecInstance);
57 break;
58 case WELS_LOG_WARNING:
59 WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Warning:", logCtx->pCodecInstance);
60 break;
61 case WELS_LOG_INFO:
62 WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Info:", logCtx->pCodecInstance);
63 break;
64 case WELS_LOG_DEBUG:
65 WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Debug:", logCtx->pCodecInstance);
66 break;
67 default:
68 WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Detail:", logCtx->pCodecInstance);
69 break;
70 }
71 WelsStrcat (pTraceTag, MAX_LOG_SIZE, kpFmt);
72 va_start (vl, kpFmt);
73 logCtx->pfLog (logCtx->pLogCtx, iLevel, pTraceTag, vl);
74 va_end (vl);
75 }
76
77 #ifndef CALC_PSNR
78 #define CONST_FACTOR_PSNR (10.0 / log(10.0)) // for good computation
79 #define CALC_PSNR(w, h, s) ((float)(CONST_FACTOR_PSNR * log( 65025.0 * w * h / s )))
80 #endif//CALC_PSNR
81
82 /*
83 * PSNR calculation routines
84 */
85 /*!
86 *************************************************************************************
87 * \brief PSNR calculation utilization in Wels
88 *
89 * \param pTarPic target picture to be calculated in Picture pData format
90 * \param iTarStride stride of target picture pData pBuffer
91 * \param pRefPic base referencing picture samples
92 * \param iRefStride stride of reference picture pData pBuffer
93 * \param iWidth picture iWidth in pixel
94 * \param iHeight picture iHeight in pixel
95 *
96 * \return actual PSNR result;
97 *
98 * \note N/A
99 *************************************************************************************
100 */
WelsCalcPsnr(const void * kpTarPic,const int32_t kiTarStride,const void * kpRefPic,const int32_t kiRefStride,const int32_t kiWidth,const int32_t kiHeight)101 float WelsCalcPsnr (const void* kpTarPic,
102 const int32_t kiTarStride,
103 const void* kpRefPic,
104 const int32_t kiRefStride,
105 const int32_t kiWidth,
106 const int32_t kiHeight) {
107 int64_t iSqe = 0;
108 int32_t x, y;
109 uint8_t* pTar = (uint8_t*)kpTarPic;
110 uint8_t* pRef = (uint8_t*)kpRefPic;
111
112 if (NULL == pTar || NULL == pRef)
113 return (-1.0f);
114
115 for (y = 0; y < kiHeight; ++ y) { // OPTable !!
116 for (x = 0; x < kiWidth; ++ x) {
117 const int32_t kiT = pTar[y * kiTarStride + x] - pRef[y * kiRefStride + x];
118 iSqe += kiT * kiT;
119 }
120 }
121 if (0 == iSqe) {
122 return (99.99f);
123 }
124 return CALC_PSNR (kiWidth, kiHeight, iSqe);
125 }
126
127