Showing the changes to codec/encoder/core/src/utils.cpp (1 of 225 files). Show all
Unified · Split
| @@ -1,513 +1,482 @@ | |||
| 1 | /*! | 1 | /*! |
| 2 | * \copy | 2 | * \copy |
| 3 | * Copyright (c) 2009-2013, Cisco Systems | 3 | * Copyright (c) 2009-2013, Cisco Systems |
| 4 | * All rights reserved. | 4 | * All rights reserved. |
| 5 | * | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without | 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions | 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: | 8 | * are met: |
| 9 | * | 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright | 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * | 12 | * |
| 13 | * * Redistributions in binary form must reproduce the above copyright | 13 | * * Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in | 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the | 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. | 16 | * distribution. |
| 17 | * | 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | 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 | 28 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. | 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | * | 30 | * |
| 31 | * | 31 | * |
| 32 | * \file utils.c | 32 | * \file utils.c |
| 33 | * | 33 | * |
| 34 | * \brief common tool/function utilization | 34 | * \brief common tool/function utilization |
| 35 | * | 35 | * |
| 36 | * \date 03/10/2009 Created | 36 | * \date 03/10/2009 Created |
| 37 | * | 37 | * |
| 38 | ************************************************************************************* | 38 | ************************************************************************************* |
| 39 | */ | 39 | */ |
| 40 | #include <string.h> | 40 | #include <string.h> |
| 41 | #include <stdlib.h> | 41 | #include <stdlib.h> |
| 42 | #include <assert.h> | 42 | #include <assert.h> |
| 43 | #include <math.h> | 43 | #include <math.h> |
| 44 | #include <time.h> | 44 | #include <time.h> |
| 45 | #if defined(WIN32) | 45 | #if defined(WIN32) |
| 46 | #include <windows.h> | 46 | #include <windows.h> |
| 47 | #include <sys/types.h> | 47 | #include <sys/types.h> |
| 48 | #include <sys/timeb.h> | 48 | #include <sys/timeb.h> |
| 49 | #else | 49 | #else |
| 50 | #include <sys/time.h> | 50 | #include <sys/time.h> |
| 51 | #endif | 51 | #endif |
| 52 | 52 | ||
| 53 | #include "utils.h" | 53 | #include "utils.h" |
| 54 | #include "macros.h" | 54 | #include "macros.h" |
| 55 | #include "wels_const.h" | 55 | #include "wels_const.h" |
| 56 | #include "property.h" | 56 | #include "property.h" |
| 57 | #include "cpu_core.h" | 57 | #include "cpu_core.h" |
| 58 | #include "encoder_context.h" | 58 | #include "encoder_context.h" |
| 59 | #include "as264_common.h" | 59 | #include "as264_common.h" |
| 60 | #include "property.h" | 60 | #include "property.h" |
| 61 | #include "crt_util_safe_x.h" // Safe CRT routines like utils for cross platforms | 61 | #include "crt_util_safe_x.h" // Safe CRT routines like utils for cross platforms |
| 62 | 62 | ||
| 63 | 63 | ||
| 64 | namespace WelsSVCEnc { | 64 | namespace WelsSVCEnc { |
| 65 | 65 | ||
| 66 | void WelsLogDefault( void *pCtx, const int32_t kiLevel, const str_t *kpFmtStr, va_list argv ); | 66 | void WelsLogDefault (void* pCtx, const int32_t kiLevel, const str_t* kpFmtStr, va_list argv); |
| 67 | void WelsLogNil( void *pCtx, const int32_t kiLevel, const str_t *kpFmtStr, va_list argv ); | 67 | void WelsLogNil (void* pCtx, const int32_t kiLevel, const str_t* kpFmtStr, va_list argv); |
| 68 | 68 | ||
| 69 | real32_t WelsCalcPsnr( const void *kpTarPic, | 69 | real32_t WelsCalcPsnr (const void* kpTarPic, |
| 70 | const int32_t kiTarStride, | 70 | const int32_t kiTarStride, |
| 71 | const void *kpRefPic, | 71 | const void* kpRefPic, |
| 72 | const int32_t kiRefStride, | 72 | const int32_t kiRefStride, |
| 73 | const int32_t kiWidth, | 73 | const int32_t kiWidth, |
| 74 | const int32_t kiHeight ); | 74 | const int32_t kiHeight); |
| 75 | 75 | ||
| 76 | // to fill default routines | 76 | // to fill default routines |
| 77 | #ifdef ENABLE_TRACE_FILE | 77 | #ifdef ENABLE_TRACE_FILE |
| 78 | PWelsLogCallbackFunc wlog = WelsLogDefault; | 78 | PWelsLogCallbackFunc wlog = WelsLogDefault; |
| 79 | #else | 79 | #else |
| 80 | PWelsLogCallbackFunc wlog = WelsLogNil; | 80 | PWelsLogCallbackFunc wlog = WelsLogNil; |
| 81 | #endif | 81 | #endif |
| 82 | 82 | ||
| 83 | iWelsLogLevel g_iLevelLog = WELS_LOG_DEFAULT; // default log iLevel | 83 | iWelsLogLevel g_iLevelLog = WELS_LOG_DEFAULT; // default log iLevel |
| 84 | int32_t g_iSizeLogBuf = 1024; // pBuffer size for each log output | 84 | int32_t g_iSizeLogBuf = 1024; // pBuffer size for each log output |
| 85 | 85 | ||
| 86 | /* | 86 | /* |
| 87 | * Log output routines | 87 | * Log output routines |
| 88 | */ | 88 | */ |
| 89 | 89 | ||
| 90 | /*! | 90 | /*! |
| 91 | * \brief get log tag | 91 | * \brief get log tag |
| 92 | * \param kiLevel log iLevel | 92 | * \param kiLevel log iLevel |
| 93 | * \return tag of log iLevel | 93 | * \return tag of log iLevel |
| 94 | */ | 94 | */ |
| 95 | static inline str_t *GetLogTag( const int32_t kiLevel, int32_t *pBit ) | 95 | static inline str_t* GetLogTag (const int32_t kiLevel, int32_t* pBit) { |
| 96 | { | 96 | int32_t iShift = 0; |
| 97 | int32_t iShift = 0; | 97 | int32_t iVal = 0; |
| 98 | int32_t iVal = 0; | 98 | bool_t bFound = false; |
| 99 | bool_t bFound = false; | 99 | |
| 100 | 100 | if (kiLevel <= 0 || kiLevel > (1 << (WELS_LOG_LEVEL_COUNT - 1)) || NULL == pBit) | |
| 101 | if ( kiLevel <= 0 || kiLevel > (1 << (WELS_LOG_LEVEL_COUNT-1)) || NULL == pBit ) | 101 | return NULL; |
| 102 | return NULL; | 102 | |
| 103 | 103 | for (;;) { | |
| 104 | for(;;) | 104 | if (iShift >= WELS_LOG_LEVEL_COUNT) |
| 105 | { | 105 | break; |
| 106 | if ( iShift >= WELS_LOG_LEVEL_COUNT ) | 106 | iVal = (1 << iShift); |
| 107 | break; | 107 | if (iVal == kiLevel) { |
| 108 | iVal = (1 << iShift); | 108 | bFound = true; |
| 109 | if ( iVal == kiLevel ) | 109 | break; |
| 110 | { | 110 | } |
| 111 | bFound = true; | 111 | ++ iShift; |
| 112 | break; | 112 | } |
| 113 | } | 113 | |
| 114 | ++ iShift; | 114 | if (bFound) { |
| 115 | } | 115 | *pBit = iShift; |
| 116 | 116 | return (str_t*)g_sWelsLogTags[iShift]; | |
| 117 | if ( bFound ) | 117 | } |
| 118 | { | 118 | return NULL; |
| 119 | *pBit = iShift; | 119 | } |
| 120 | return (str_t *)g_sWelsLogTags[iShift]; | 120 | |
| 121 | } | 121 | /*! |
| 122 | return NULL; | 122 | ************************************************************************************* |
| 123 | } | 123 | * \brief System trace log output in Wels |
| 124 | 124 | * | |
| 125 | /*! | 125 | * \param pCtx instance pointer |
| 126 | ************************************************************************************* | 126 | * \param kiLevel log iLevel ( WELS_LOG_QUIET, ERROR, WARNING, INFO, DEBUG ) |
| 127 | * \brief System trace log output in Wels | 127 | * \param kpFmtStr formated string to mount |
| 128 | * | 128 | * \param argv pData string argument |
| 129 | * \param pCtx instance pointer | 129 | * |
| 130 | * \param kiLevel log iLevel ( WELS_LOG_QUIET, ERROR, WARNING, INFO, DEBUG ) | 130 | * \return NONE |
| 131 | * \param kpFmtStr formated string to mount | 131 | * |
| 132 | * \param argv pData string argument | 132 | * \note N/A |
| 133 | * | 133 | ************************************************************************************* |
| 134 | * \return NONE | 134 | */ |
| 135 | * | 135 | void WelsLogDefault (void* pCtx, const int32_t kiLevel, const str_t* kpFmtStr, va_list argv) { |
| 136 | * \note N/A | 136 | sWelsEncCtx* pEncCtx = (sWelsEncCtx*)pCtx; |
| 137 | ************************************************************************************* | 137 | iWelsLogLevel iVal = (kiLevel & g_iLevelLog); |
| 138 | */ | 138 | |
| 139 | void WelsLogDefault( void *pCtx, const int32_t kiLevel, const str_t *kpFmtStr, va_list argv ) | 139 | if (0 == iVal || NULL == pEncCtx) { // such iLevel not enabled |
| 140 | { | 140 | return; |
| 141 | sWelsEncCtx *pEncCtx = (sWelsEncCtx *)pCtx; | 141 | } else { |
| 142 | iWelsLogLevel iVal = (kiLevel & g_iLevelLog); | 142 | str_t pBuf[WELS_LOG_BUF_SIZE + 1] = {0}; |
| 143 | 143 | const int32_t kiBufSize = sizeof (pBuf) / sizeof (pBuf[0]) - 1; | |
| 144 | if ( 0 == iVal || NULL == pEncCtx ) // such iLevel not enabled | 144 | int32_t iCurUsed = 0; |
| 145 | { | 145 | int32_t iBufUsed = 0; |
| 146 | return; | 146 | int32_t iBufLeft = kiBufSize - iBufUsed; |
| 147 | } | 147 | |
| 148 | else | 148 | if (pEncCtx) { |
| 149 | { | 149 | time_t l_time; |
| 150 | str_t pBuf[WELS_LOG_BUF_SIZE+1] = {0}; | 150 | #if defined(WIN32) |
| 151 | const int32_t kiBufSize = sizeof(pBuf) / sizeof(pBuf[0]) - 1; | 151 | #if defined(_MSC_VER) |
| 152 | int32_t iCurUsed = 0; | 152 | #if _MSC_VER >= 1500 |
| 153 | int32_t iBufUsed = 0; | 153 | struct tm t_now; |
| 154 | int32_t iBufLeft = kiBufSize - iBufUsed; | 154 | #else//VC6 |
| 155 | 155 | struct tm* t_now; | |
| 156 | if ( pEncCtx ){ | 156 | #endif//_MSC_VER >= 1500 |
| 157 | time_t l_time; | 157 | #endif//_MSC_VER |
| 158 | #if defined(WIN32) | 158 | #else//__GNUC__ |
| 159 | #if defined(_MSC_VER) | 159 | struct tm* t_now; |
| 160 | #if _MSC_VER >= 1500 | 160 | #endif//WIN32 |
| 161 | struct tm t_now; | 161 | |
| 162 | #else//VC6 | 162 | #if defined( WIN32 ) |
| 163 | struct tm* t_now; | 163 | struct _timeb tb; |
| 164 | #endif//_MSC_VER >= 1500 | 164 | |
| 165 | #endif//_MSC_VER | 165 | time (&l_time); |
| 166 | #else//__GNUC__ | 166 | #ifdef _MSC_VER |
| 167 | struct tm* t_now; | 167 | #if _MSC_VER >= 1500 |
| 168 | #endif//WIN32 | 168 | LOCALTIME (&t_now, &l_time); |
| 169 | 169 | #else | |
| 170 | #if defined( WIN32 ) | 170 | t_now = LOCALTIME (&l_time); |
| 171 | struct _timeb tb; | 171 | if (NULL == t_now) { |
| 172 | 172 | return; | |
| 173 | time(&l_time); | 173 | } |
| 174 | #ifdef _MSC_VER | 174 | #endif//_MSC_VER >= 1500 |
| 175 | #if _MSC_VER >= 1500 | 175 | #endif//_MSC_VER |
| 176 | LOCALTIME(&t_now, &l_time); | 176 | FTIME (&tb); |
| 177 | #else | 177 | #elif defined( __GNUC__ ) |
| 178 | t_now = LOCALTIME(&l_time); | 178 | struct timeval tv; |
| 179 | if ( NULL == t_now ) | 179 | time (&l_time); |
| 180 | { | 180 | t_now = (struct tm*)LOCALTIME (&l_time); |
| 181 | return; | 181 | gettimeofday (&tv, NULL); |
| 182 | } | 182 | #endif//WIN32 |
| 183 | #endif//_MSC_VER >= 1500 | 183 | if (iBufLeft > 0) { |
| 184 | #endif//_MSC_VER | 184 | #ifdef _MSC_VER |
| 185 | FTIME(&tb); | 185 | #if _MSC_VER >= 1500 |
| 186 | #elif defined( __GNUC__ ) | 186 | iCurUsed = SNPRINTF (&pBuf[iBufUsed], iBufLeft, iBufLeft, "[0x%p @ ", pEncCtx); // confirmed_safe_unsafe_usage |
| 187 | struct timeval tv; | 187 | #else |
| 188 | time(&l_time); | 188 | iCurUsed = SNPRINTF (&pBuf[iBufUsed], iBufLeft, "[0x%p @ ", pEncCtx); // confirmed_safe_unsafe_usage |
| 189 | t_now = (struct tm *)LOCALTIME(&l_time); | 189 | #endif//_MSC_VER >= 1500 |
| 190 | gettimeofday(&tv,NULL); | 190 | #endif//_MSC_VER |
| 191 | #endif//WIN32 | 191 | if (iCurUsed >= 0) { |
| 192 | if (iBufLeft > 0){ | 192 | iBufUsed += iCurUsed; |
| 193 | #ifdef _MSC_VER | 193 | iBufLeft -= iCurUsed; |
| 194 | #if _MSC_VER >= 1500 | 194 | } |
| 195 | iCurUsed = SNPRINTF( &pBuf[iBufUsed], iBufLeft, iBufLeft, "[0x%p @ ", pEncCtx ); // confirmed_safe_unsafe_usage | 195 | } else { |
| 196 | #else | 196 | return; |
| 197 | iCurUsed = SNPRINTF( &pBuf[iBufUsed], iBufLeft, "[0x%p @ ", pEncCtx ); // confirmed_safe_unsafe_usage | 197 | } |
| 198 | #endif//_MSC_VER >= 1500 | 198 | |
| 199 | #endif//_MSC_VER | 199 | if (iBufLeft > 0) { |
| 200 | if (iCurUsed >= 0){ | 200 | iCurUsed = GetCodeName (&pBuf[iBufUsed], iBufLeft); |
| 201 | iBufUsed += iCurUsed; | 201 | if (iCurUsed > 0) { |
| 202 | iBufLeft -= iCurUsed; | 202 | iBufUsed += iCurUsed; |
| 203 | } | 203 | iBufLeft -= iCurUsed; |
| 204 | } | 204 | } |
| 205 | else{ | 205 | pBuf[iBufUsed] = ' '; |
| 206 | return; | 206 | ++ iBufUsed; |
| 207 | } | 207 | -- iBufLeft; |
| 208 | 208 | ||
| 209 | if ( iBufLeft > 0 ){ | 209 | iCurUsed = GetLibName (&pBuf[iBufUsed], iBufLeft); |
| 210 | iCurUsed = GetCodeName( &pBuf[iBufUsed], iBufLeft ); | 210 | if (iCurUsed > 0) { |
| 211 | if ( iCurUsed > 0 ){ | 211 | iBufUsed += iCurUsed; |
| 212 | iBufUsed += iCurUsed; | 212 | iBufLeft -= iCurUsed; |
| 213 | iBufLeft -= iCurUsed; | 213 | } |
| 214 | } | 214 | pBuf[iBufUsed] = ' '; |
| 215 | pBuf[iBufUsed] = ' '; | 215 | ++ iBufUsed; |
| 216 | ++ iBufUsed; | 216 | -- iBufLeft; |
| 217 | -- iBufLeft; | 217 | |
| 218 | 218 | pBuf[iBufUsed] = 'v'; | |
| 219 | iCurUsed = GetLibName( &pBuf[iBufUsed], iBufLeft ); | 219 | ++ iBufUsed; |
| 220 | if ( iCurUsed > 0 ){ | 220 | -- iBufLeft; |
| 221 | iBufUsed += iCurUsed; | 221 | iCurUsed = GetVerNum (&pBuf[iBufUsed], iBufLeft); |
| 222 | iBufLeft -= iCurUsed; | 222 | if (iCurUsed > 0) { |
| 223 | } | 223 | iBufUsed += iCurUsed; |
| 224 | pBuf[iBufUsed] = ' '; | 224 | iBufLeft -= iCurUsed; |
| 225 | ++ iBufUsed; | 225 | } |
| 226 | -- iBufLeft; | 226 | pBuf[iBufUsed] = ' '; |
| 227 | 227 | ++ iBufUsed; | |
| 228 | pBuf[iBufUsed] = 'v'; | 228 | -- iBufLeft; |
| 229 | ++ iBufUsed; | 229 | } |
| 230 | -- iBufLeft; | 230 | |
| 231 | iCurUsed = GetVerNum( &pBuf[iBufUsed], iBufLeft ); | 231 | if (iBufLeft > 0) { |
| 232 | if ( iCurUsed > 0 ){ | 232 | #if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1500) |
| 233 | iBufUsed += iCurUsed; | 233 | iCurUsed = strftime (&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", &t_now); |
| 234 | iBufLeft -= iCurUsed; | 234 | #else |
| 235 | } | 235 | iCurUsed = strftime (&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", t_now); |
| 236 | pBuf[iBufUsed] = ' '; | 236 | #endif//WIN32.. |
| 237 | ++ iBufUsed; | 237 | if (iCurUsed > 0) { |
| 238 | -- iBufLeft; | 238 | iBufUsed += iCurUsed; |
| 239 | } | 239 | iBufLeft -= iCurUsed; |
| 240 | 240 | } | |
| 241 | if (iBufLeft > 0){ | 241 | } else { |
| 242 | #if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1500) | 242 | return; |
| 243 | iCurUsed = strftime(&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", &t_now); | 243 | } |
| 244 | #else | 244 | |
| 245 | iCurUsed = strftime(&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", t_now); | 245 | if (iBufLeft > 0) { |
| 246 | #endif//WIN32.. | 246 | #if defined (WIN32) |
| 247 | if (iCurUsed > 0){ | 247 | #ifdef _MSC_VER |
| 248 | iBufUsed += iCurUsed; | 248 | #if _MSC_VER >= 1500 |
| 249 | iBufLeft -= iCurUsed; | 249 | iCurUsed = SNPRINTF (&pBuf[iBufUsed], iBufLeft, iBufLeft, ".%03.3u]: ", tb.millitm); // confirmed_safe_unsafe_usage |
| 250 | } | 250 | #else |
| 251 | } | 251 | iCurUsed = SNPRINTF (&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tb.millitm); // confirmed_safe_unsafe_usage |
| 252 | else{ | 252 | #endif//_MSC_VER >= 1500 |
| 253 | return; | 253 | #endif//_MSC_VER |
| 254 | } | 254 | #elif defined (__GNUC__) |
| 255 | 255 | iCurUsed = SNPRINTF (&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tv.tv_usec / 1000); // confirmed_safe_unsafe_usage | |
| 256 | if (iBufLeft > 0){ | 256 | #endif//WIN32 |
| 257 | #if defined (WIN32) | 257 | if (iCurUsed >= 0) { |
| 258 | #ifdef _MSC_VER | 258 | iBufUsed += iCurUsed; |
| 259 | #if _MSC_VER >= 1500 | 259 | iBufLeft -= iCurUsed; |
| 260 | iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, iBufLeft, ".%03.3u]: ", tb.millitm); // confirmed_safe_unsafe_usage | 260 | } |
| 261 | #else | 261 | } else { |
| 262 | iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tb.millitm); // confirmed_safe_unsafe_usage | 262 | return; |
| 263 | #endif//_MSC_VER >= 1500 | 263 | } |
| 264 | #endif//_MSC_VER | 264 | } |
| 265 | #elif defined (__GNUC__) | 265 | |
| 266 | iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tv.tv_usec/1000); // confirmed_safe_unsafe_usage | 266 | // fixed stack corruption issue on vs2008 |
| 267 | #endif//WIN32 | 267 | if (iBufLeft > 0) { |
| 268 | if (iCurUsed >= 0){ | 268 | int32_t i_shift = 0; |
| 269 | iBufUsed += iCurUsed; | 269 | str_t* pStr = NULL; |
| 270 | iBufLeft -= iCurUsed; | 270 | pStr = GetLogTag (kiLevel, &i_shift); |
| 271 | } | 271 | if (NULL != pCtx) { |
| 272 | } | 272 | int32_t iLenTag = STRNLEN (pStr, 8); // confirmed_safe_unsafe_usage |
| 273 | else{ | 273 | STRCAT (&pBuf[iBufUsed], iBufLeft, pStr); // confirmed_safe_unsafe_usage |
| 274 | return; | 274 | iBufUsed += iLenTag; |
| 275 | } | 275 | pBuf[iBufUsed] = ' '; |
| 276 | } | 276 | iBufUsed++; |
| 277 | 277 | ++iLenTag; | |
| 278 | // fixed stack corruption issue on vs2008 | 278 | iBufLeft -= iLenTag; |
| 279 | if ( iBufLeft > 0 ){ | 279 | } |
| 280 | int32_t i_shift = 0; | 280 | } |
| 281 | str_t *pStr = NULL; | 281 | if (iBufLeft > 0) { |
| 282 | pStr = GetLogTag( kiLevel, &i_shift ); | 282 | #if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1500) |
| 283 | if ( NULL != pCtx){ | 283 | int32_t len = 0; |
| 284 | int32_t iLenTag = STRNLEN( pStr, 8 ); // confirmed_safe_unsafe_usage | 284 | len = _vscprintf (kpFmtStr, argv) // _vscprintf doesn't count |
| 285 | STRCAT( &pBuf[iBufUsed], iBufLeft, pStr ); // confirmed_safe_unsafe_usage | 285 | + 1; // terminating '\0' |
| 286 | iBufUsed += iLenTag; | 286 | iCurUsed = VSPRINTF (&pBuf[iBufUsed], len, kpFmtStr, argv); // confirmed_safe_unsafe_usage |
| 287 | pBuf[iBufUsed] = ' '; | 287 | #else |
| 288 | iBufUsed++; | 288 | iCurUsed = VSPRINTF (&pBuf[iBufUsed], kpFmtStr, argv); // confirmed_safe_unsafe_usage |
| 289 | ++iLenTag; | 289 | #endif//WIN32.. |
| 290 | iBufLeft -= iLenTag; | 290 | if (iCurUsed > 0) { |
| 291 | } | 291 | iBufUsed += iCurUsed; |
| 292 | } | 292 | iBufLeft -= iCurUsed; |
| 293 | if (iBufLeft > 0){ | 293 | } |
| 294 | #if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1500) | 294 | } |
| 295 | int32_t len = 0; | 295 | #ifdef ENABLE_TRACE_FILE |
| 296 | len = _vscprintf( kpFmtStr, argv ) // _vscprintf doesn't count | 296 | if (NULL != pEncCtx && NULL != pEncCtx->pFileLog) { |
| 297 | + 1; // terminating '\0' | 297 | if (pEncCtx->uiSizeLog > MAX_TRACE_LOG_SIZE) { |
| 298 | iCurUsed = VSPRINTF(&pBuf[iBufUsed], len, kpFmtStr, argv); // confirmed_safe_unsafe_usage | 298 | if (0 == fseek (pEncCtx->pFileLog, 0L, SEEK_SET)) |
| 299 | #else | 299 | pEncCtx->uiSizeLog = 0; |
| 300 | iCurUsed = VSPRINTF(&pBuf[iBufUsed], kpFmtStr, argv); // confirmed_safe_unsafe_usage | 300 | } |
| 301 | #endif//WIN32.. | 301 | if (iBufUsed > 0 && iBufUsed < WELS_LOG_BUF_SIZE) { |
| 302 | if (iCurUsed > 0){ | 302 | iCurUsed = fwrite (pBuf, 1, iBufUsed, pEncCtx->pFileLog); |
| 303 | iBufUsed += iCurUsed; | 303 | fflush (pEncCtx->pFileLog); |
| 304 | iBufLeft -= iCurUsed; | 304 | if (iCurUsed == iBufUsed) |
| 305 | } | 305 | pEncCtx->uiSizeLog += iBufUsed; |
| 306 | } | 306 | } |
| 307 | #ifdef ENABLE_TRACE_FILE | 307 | } else { |
| 308 | if (NULL != pEncCtx && NULL != pEncCtx->pFileLog){ | 308 | #if defined(WIN32) && defined(_DEBUG) |
| 309 | if ( pEncCtx->uiSizeLog > MAX_TRACE_LOG_SIZE){ | 309 | OutputDebugStringA (pBuf); |
| 310 | if (0 == fseek(pEncCtx->pFileLog, 0L, SEEK_SET)) | 310 | #endif |
| 311 | pEncCtx->uiSizeLog = 0; | 311 | } |
| 312 | } | 312 | #endif//ENABLE_TRACE_FILE |
| 313 | if ( iBufUsed > 0 && iBufUsed < WELS_LOG_BUF_SIZE ) | 313 | } |
| 314 | { | 314 | } |
| 315 | iCurUsed = fwrite(pBuf, 1, iBufUsed, pEncCtx->pFileLog); | 315 | void WelsLogNil (void* pCtx, const int32_t kiLevel, const str_t* kpFmtStr, va_list argv) { |
| 316 | fflush( pEncCtx->pFileLog ); | 316 | // NULL implementation |
| 317 | if ( iCurUsed == iBufUsed ) | 317 | } |
| 318 | pEncCtx->uiSizeLog += iBufUsed; | 318 | |
| 319 | } | 319 | /*! |
| 320 | } | 320 | ************************************************************************************* |
| 321 | else{ | 321 | * \brief reopen log file when finish setting current path |
| 322 | #if defined(WIN32) && defined(_DEBUG) | 322 | * |
| 323 | OutputDebugStringA(pBuf); | 323 | * \param pCtx context pCtx |
| 324 | #endif | 324 | * \param pCurPath current path string |
| 325 | } | 325 | * |
| 326 | #endif//ENABLE_TRACE_FILE | 326 | * \return NONE |
| 327 | } | 327 | * |
| 328 | } | 328 | * \note N/A |
| 329 | void WelsLogNil( void *pCtx, const int32_t kiLevel, const str_t *kpFmtStr, va_list argv ) | 329 | ************************************************************************************* |
| 330 | { | 330 | */ |
| 331 | // NULL implementation | 331 | void WelsReopenTraceFile (void* pCtx, str_t* pCurPath) { |
| 332 | } | 332 | #ifdef ENABLE_TRACE_FILE |
| 333 | 333 | sWelsEncCtx* pEncCtx = (sWelsEncCtx*)pCtx; | |
| 334 | /*! | 334 | if (wlog == WelsLogDefault) { |
| 335 | ************************************************************************************* | 335 | str_t strTraceFile[MAX_FNAME_LEN] = {0}; |
| 336 | * \brief reopen log file when finish setting current path | 336 | int32_t len = 0; |
| 337 | * | 337 | if (pEncCtx->pFileLog != NULL) { |
| 338 | * \param pCtx context pCtx | 338 | fclose (pEncCtx->pFileLog); |
| 339 | * \param pCurPath current path string | 339 | pEncCtx->pFileLog = NULL; |
| 340 | * | 340 | } |
| 341 | * \return NONE | 341 | pEncCtx->uiSizeLog = 0; |
| 342 | * | 342 | len = STRNLEN (pCurPath, MAX_FNAME_LEN - 1); // confirmed_safe_unsafe_usage |
| 343 | * \note N/A | 343 | if (len >= MAX_FNAME_LEN) |
| 344 | ************************************************************************************* | 344 | return; |
| 345 | */ | 345 | STRNCPY (strTraceFile, MAX_FNAME_LEN, pCurPath, len); // confirmed_safe_unsafe_usage |
| 346 | void WelsReopenTraceFile( void *pCtx, str_t *pCurPath ) | 346 | #ifdef __GNUC__ |
| 347 | { | 347 | STRCAT (strTraceFile, MAX_FNAME_LEN - len, "/wels_encoder_trace.txt"); // confirmed_safe_unsafe_usage |
| 348 | #ifdef ENABLE_TRACE_FILE | 348 | pEncCtx->pFileLog = FOPEN (strTraceFile, "wt+"); // confirmed_safe_unsafe_usage |
| 349 | sWelsEncCtx *pEncCtx = (sWelsEncCtx *)pCtx; | 349 | #elif WIN32 |
| 350 | if (wlog == WelsLogDefault) | 350 | STRCAT (strTraceFile, MAX_FNAME_LEN - len, "\\wels_encoder_trace.txt"); // confirmed_safe_unsafe_usage |
| 351 | { | 351 | #if _MSC_VER >= 1500 |
| 352 | str_t strTraceFile[MAX_FNAME_LEN] = {0}; | 352 | FOPEN (&pEncCtx->pFileLog, strTraceFile, "wt+"); // confirmed_safe_unsafe_usage |
| 353 | int32_t len = 0; | 353 | #else |
| 354 | if (pEncCtx->pFileLog != NULL) | 354 | pEncCtx->pFileLog = FOPEN (strTraceFile, "wt+"); // confirmed_safe_unsafe_usage |
| 355 | { | 355 | #endif//_MSC_VER>=1500 |
| 356 | fclose(pEncCtx->pFileLog); | 356 | #else |
| 357 | pEncCtx->pFileLog = NULL; | 357 | #endif//__GNUC__ |
| 358 | } | 358 | } |
| 359 | pEncCtx->uiSizeLog = 0; | 359 | #endif//ENABLE_TRACE_FILE |
| 360 | len = STRNLEN( pCurPath, MAX_FNAME_LEN-1 ); // confirmed_safe_unsafe_usage | 360 | } |
| 361 | if (len >= MAX_FNAME_LEN) | 361 | |
| 362 | return; | 362 | /*! |
| 363 | STRNCPY(strTraceFile, MAX_FNAME_LEN, pCurPath, len); // confirmed_safe_unsafe_usage | 363 | ************************************************************************************* |
| 364 | #ifdef __GNUC__ | 364 | * \brief set log iLevel from external call |
| 365 | STRCAT(strTraceFile, MAX_FNAME_LEN-len, "/wels_encoder_trace.txt"); // confirmed_safe_unsafe_usage | 365 | * |
| 366 | pEncCtx->pFileLog = FOPEN(strTraceFile, "wt+"); // confirmed_safe_unsafe_usage | 366 | * \param iLevel iLevel of log |
| 367 | #elif WIN32 | 367 | * |
| 368 | STRCAT(strTraceFile, MAX_FNAME_LEN-len, "\\wels_encoder_trace.txt");// confirmed_safe_unsafe_usage | 368 | * \return NONE |
| 369 | #if _MSC_VER >= 1500 | 369 | * |
| 370 | FOPEN(&pEncCtx->pFileLog, strTraceFile, "wt+"); // confirmed_safe_unsafe_usage | 370 | * \note can be able to control log iLevel dynamically |
| 371 | #else | 371 | ************************************************************************************* |
| 372 | pEncCtx->pFileLog = FOPEN(strTraceFile, "wt+"); // confirmed_safe_unsafe_usage | 372 | */ |
| 373 | #endif//_MSC_VER>=1500 | 373 | void WelsSetLogLevel (const int32_t kiLevel) { |
| 374 | #else | 374 | iWelsLogLevel iVal = 0; |
| 375 | #endif//__GNUC__ | 375 | if (kiLevel & WELS_LOG_ERROR) { |
| 376 | } | 376 | iVal |= WELS_LOG_ERROR; |
| 377 | #endif//ENABLE_TRACE_FILE | 377 | } |
| 378 | } | 378 | if (kiLevel & WELS_LOG_WARNING) { |
| 379 | 379 | iVal |= WELS_LOG_WARNING; | |
| 380 | /*! | 380 | } |
| 381 | ************************************************************************************* | 381 | if (kiLevel & WELS_LOG_INFO) { |
| 382 | * \brief set log iLevel from external call | 382 | iVal |= WELS_LOG_INFO; |
| 383 | * | 383 | } |
| 384 | * \param iLevel iLevel of log | 384 | if (kiLevel & WELS_LOG_DEBUG) { |
| 385 | * | 385 | iVal |= WELS_LOG_DEBUG; |
| 386 | * \return NONE | 386 | } |
| 387 | * | 387 | g_iLevelLog = iVal; |
| 388 | * \note can be able to control log iLevel dynamically | 388 | } |
| 389 | ************************************************************************************* | 389 | |
| 390 | */ | 390 | /*! |
| 391 | void WelsSetLogLevel( const int32_t kiLevel ) | 391 | ************************************************************************************* |
| 392 | { | 392 | * \brief get log iLevel from external call |
| 393 | iWelsLogLevel iVal = 0; | 393 | * |
| 394 | if ( kiLevel & WELS_LOG_ERROR ) | 394 | * \param N/A |
| 395 | { | 395 | * |
| 396 | iVal |= WELS_LOG_ERROR; | 396 | * \return current iLevel of log used in codec internal |
| 397 | } | 397 | * |
| 398 | if ( kiLevel & WELS_LOG_WARNING ) | 398 | * \note can be able to get log iLevel of internal codec applicable |
| 399 | { | 399 | ************************************************************************************* |
| 400 | iVal |= WELS_LOG_WARNING; | 400 | */ |
| 401 | } | 401 | int32_t WelsGetLogLevel (void) { |
| 402 | if ( kiLevel & WELS_LOG_INFO ) | 402 | return g_iLevelLog; |
| 403 | { | 403 | } |
| 404 | iVal |= WELS_LOG_INFO; | 404 | |
| 405 | } | 405 | /*! |
| 406 | if ( kiLevel & WELS_LOG_DEBUG ) | 406 | ************************************************************************************* |
| 407 | { | 407 | * \brief set log callback from external call |
| 408 | iVal |= WELS_LOG_DEBUG; | 408 | * |
| 409 | } | 409 | * \param _log log function routine |
| 410 | g_iLevelLog = iVal; | 410 | * |
| 411 | } | 411 | * \return NONE |
| 412 | 412 | * | |
| 413 | /*! | 413 | * \note N/A |
| 414 | ************************************************************************************* | 414 | ************************************************************************************* |
| 415 | * \brief get log iLevel from external call | 415 | */ |
| 416 | * | 416 | void WelsSetLogCallback (PWelsLogCallbackFunc _log) { |
| 417 | * \param N/A | 417 | wlog = _log; |
| 418 | * | 418 | } |
| 419 | * \return current iLevel of log used in codec internal | 419 | |
| 420 | * | 420 | void WelsLogCall (void* pCtx, int32_t iLevel, const str_t* kpFmt, va_list vl) { |
| 421 | * \note can be able to get log iLevel of internal codec applicable | 421 | wlog (pCtx, iLevel, kpFmt, vl); |
| 422 | ************************************************************************************* | 422 | } |
| 423 | */ | 423 | |
| 424 | int32_t WelsGetLogLevel( void ) | 424 | void WelsLog (void* pCtx, int32_t iLevel, const str_t* kpFmt, ...) { |
| 425 | { | 425 | va_list vl; |
| 426 | return g_iLevelLog; | 426 | va_start (vl, kpFmt); |
| 427 | } | 427 | WelsLogCall (pCtx, iLevel, kpFmt, vl); |
| 428 | 428 | va_end (vl); | |
| 429 | /*! | 429 | } |
| 430 | ************************************************************************************* | 430 | |
| 431 | * \brief set log callback from external call | 431 | #ifndef CALC_PSNR |
| 432 | * | 432 | #define CONST_FACTOR_PSNR (10.0 / log(10.0)) // for good computation |
| 433 | * \param _log log function routine | 433 | #define CALC_PSNR(w, h, s) ((real32_t)(CONST_FACTOR_PSNR * log( 65025.0 * w * h / iSqe ))) |
| 434 | * | 434 | #endif//CALC_PSNR |
| 435 | * \return NONE | 435 | |
| 436 | * | 436 | /* |
| 437 | * \note N/A | 437 | * PSNR calculation routines |
| 438 | ************************************************************************************* | 438 | */ |
| 439 | */ | 439 | /*! |
| 440 | void WelsSetLogCallback( PWelsLogCallbackFunc _log ) | 440 | ************************************************************************************* |
| 441 | { | 441 | * \brief PSNR calculation utilization in Wels |
| 442 | wlog = _log; | 442 | * |
| 443 | } | 443 | * \param pTarPic target picture to be calculated in Picture pData format |
| 444 | 444 | * \param iTarStride stride of target picture pData pBuffer | |
| 445 | void WelsLogCall(void *pCtx, int32_t iLevel, const str_t *kpFmt, va_list vl) | 445 | * \param pRefPic base referencing picture samples |
| 446 | { | 446 | * \param iRefStride stride of reference picture pData pBuffer |
| 447 | wlog(pCtx, iLevel, kpFmt, vl); | 447 | * \param iWidth picture iWidth in pixel |
| 448 | } | 448 | * \param iHeight picture iHeight in pixel |
| 449 | 449 | * | |
| 450 | void WelsLog(void *pCtx, int32_t iLevel, const str_t *kpFmt, ...) | 450 | * \return actual PSNR result; |
| 451 | { | 451 | * |
| 452 | va_list vl; | 452 | * \note N/A |
| 453 | va_start(vl, kpFmt); | 453 | ************************************************************************************* |
| 454 | WelsLogCall(pCtx, iLevel, kpFmt, vl); | 454 | */ |
| 455 | va_end(vl); | 455 | real32_t WelsCalcPsnr (const void* kpTarPic, |
| 456 | } | 456 | const int32_t kiTarStride, |
| 457 | 457 | const void* kpRefPic, | |
| 458 | #ifndef CALC_PSNR | 458 | const int32_t kiRefStride, |
| 459 | #define CONST_FACTOR_PSNR (10.0 / log(10.0)) // for good computation | 459 | const int32_t kiWidth, |
| 460 | #define CALC_PSNR(w, h, s) ((real32_t)(CONST_FACTOR_PSNR * log( 65025.0 * w * h / iSqe ))) | 460 | const int32_t kiHeight) { |
| 461 | #endif//CALC_PSNR | 461 | int64_t iSqe = 0; |
| 462 | 462 | int32_t x, y; | |
| 463 | /* | 463 | uint8_t* pTar = (uint8_t*)kpTarPic; |
| 464 | * PSNR calculation routines | 464 | uint8_t* pRef = (uint8_t*)kpRefPic; |
| 465 | */ | 465 | |
| 466 | /*! | 466 | if (NULL == pTar || NULL == pRef) |
| 467 | ************************************************************************************* | 467 | return (-1.0f); |
| 468 | * \brief PSNR calculation utilization in Wels | 468 | |
| 469 | * | 469 | for (y = 0; y < kiHeight; ++ y) { // OPTable !! |
| 470 | * \param pTarPic target picture to be calculated in Picture pData format | 470 | for (x = 0; x < kiWidth; ++ x) { |
| 471 | * \param iTarStride stride of target picture pData pBuffer | 471 | const int32_t kiT = pTar[y * kiTarStride + x] - pRef[y * kiRefStride + x]; |
| 472 | * \param pRefPic base referencing picture samples | 472 | iSqe += kiT * kiT; |
| 473 | * \param iRefStride stride of reference picture pData pBuffer | 473 | } |
| 474 | * \param iWidth picture iWidth in pixel | 474 | } |
| 475 | * \param iHeight picture iHeight in pixel | 475 | if (0 == iSqe) { |
| 476 | * | 476 | return (99.99f); |
| 477 | * \return actual PSNR result; | 477 | } |
| 478 | * | 478 | return CALC_PSNR (kiWidth, kiHeight, iSqe); |
| 479 | * \note N/A | 479 | } |
| 480 | ************************************************************************************* | 480 | |
| 481 | */ | 481 | |
| 482 | real32_t WelsCalcPsnr( const void *kpTarPic, | 482 | } |
| 483 | const int32_t kiTarStride, | ||
| 484 | const void *kpRefPic, | ||
| 485 | const int32_t kiRefStride, | ||
| 486 | const int32_t kiWidth, | ||
| 487 | const int32_t kiHeight ) | ||
| 488 | { | ||
| 489 | int64_t iSqe = 0; | ||
| 490 | int32_t x, y; | ||
| 491 | uint8_t *pTar = (uint8_t *)kpTarPic; | ||
| 492 | uint8_t *pRef = (uint8_t *)kpRefPic; | ||
| 493 | |||
| 494 | if ( NULL == pTar || NULL == pRef ) | ||
| 495 | return (-1.0f); | ||
| 496 | |||
| 497 | for ( y = 0; y < kiHeight; ++ y ) // OPTable !! | ||
| 498 | { | ||
| 499 | for ( x = 0; x < kiWidth; ++ x ) | ||
| 500 | { | ||
| 501 | const int32_t kiT = pTar[y*kiTarStride+x] - pRef[y*kiRefStride+x]; | ||
| 502 | iSqe += kiT * kiT; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | if ( 0 == iSqe ) | ||
| 506 | { | ||
| 507 | return (99.99f); | ||
| 508 | } | ||
| 509 | return CALC_PSNR( kiWidth, kiHeight, iSqe ); | ||
| 510 | } | ||
| 511 | |||
| 512 | |||
| 513 | } | ||