Showing the changes to codec/decoder/core/src/utils.cpp (1 of 308 files). Show all
Unified · Split
| @@ -0,0 +1,307 @@ | |||
| 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 | |||
| 41 | #include <string.h> | ||
| 42 | #include <stdlib.h> | ||
| 43 | #include <math.h> | ||
| 44 | #include <time.h> | ||
| 45 | #if defined(WIN32) | ||
| 46 | #include <windows.h> | ||
| 47 | #include <sys/types.h> | ||
| 48 | #include <sys/timeb.h> | ||
| 49 | #else | ||
| 50 | #include <sys/time.h> | ||
| 51 | #include <sys/timeb.h> | ||
| 52 | #endif | ||
| 53 | |||
| 54 | #include "utils.h" | ||
| 55 | #include "macros.h" | ||
| 56 | #include "wels_const.h" | ||
| 57 | #include "cpu_core.h" | ||
| 58 | #include "decoder_context.h" | ||
| 59 | #include "crt_util_safe_x.h" // Safe CRT routines like utils for cross platforms | ||
| 60 | #include "mem_align.h" | ||
| 61 | |||
| 62 | namespace WelsDec { | ||
| 63 | |||
| 64 | // cache line size | ||
| 65 | uint32_t g_uiCacheLineSize = 16; | ||
| 66 | // to fill default routines | ||
| 67 | PWelsLogCallbackFunc g_pLog = NULL; | ||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | void_t WelsLog(void_t *pPtr, int32_t iLevel, const char *kpFmt, ...) | ||
| 72 | { | ||
| 73 | va_list pVl; | ||
| 74 | |||
| 75 | PWelsDecoderContext pCtx = (PWelsDecoderContext)pPtr; | ||
| 76 | |||
| 77 | va_start(pVl, kpFmt); | ||
| 78 | g_pLog(pCtx->pTraceHandle, iLevel, kpFmt, pVl); | ||
| 79 | va_end(pVl); | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | #if defined(WIN32) | ||
| 84 | |||
| 85 | #if defined(_MSC_VER) && (_MSC_VER>=1500) | ||
| 86 | |||
| 87 | int32_t WelsSnprintf(str_t * pBuffer, int32_t iSizeOfBuffer, const str_t * kpFormat, ...) | ||
| 88 | { | ||
| 89 | va_list pArgPtr; | ||
| 90 | int32_t iRc; | ||
| 91 | |||
| 92 | va_start(pArgPtr, kpFormat); | ||
| 93 | |||
| 94 | iRc = vsnprintf_s(pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr); | ||
| 95 | |||
| 96 | va_end(pArgPtr); | ||
| 97 | |||
| 98 | return iRc; | ||
| 99 | } | ||
| 100 | |||
| 101 | str_t* WelsStrncpy(str_t * pDest, int32_t iSizeInBytes, const str_t * kpSrc, int32_t iCount) | ||
| 102 | { | ||
| 103 | strncpy_s(pDest, iSizeInBytes, kpSrc, iCount); | ||
| 104 | |||
| 105 | return pDest; | ||
| 106 | } | ||
| 107 | |||
| 108 | int32_t WelsStrnlen(const str_t * kpStr, int32_t iMaxlen) | ||
| 109 | { | ||
| 110 | return strnlen_s(kpStr, iMaxlen); | ||
| 111 | } | ||
| 112 | |||
| 113 | int32_t WelsVsprintf(str_t * pBuffer, int32_t iSizeOfBuffer, const str_t * kpFormat, va_list pArgPtr) | ||
| 114 | { | ||
| 115 | return vsprintf_s(pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); | ||
| 116 | } | ||
| 117 | |||
| 118 | WelsFileHandle* WelsFopen(const str_t * kpFilename, const str_t * kpMode) | ||
| 119 | { | ||
| 120 | WelsFileHandle* pFp = NULL; | ||
| 121 | if( fopen_s(&pFp, kpFilename, kpMode) != 0 ){ | ||
| 122 | return NULL; | ||
| 123 | } | ||
| 124 | |||
| 125 | return pFp; | ||
| 126 | } | ||
| 127 | |||
| 128 | int32_t WelsFclose(WelsFileHandle* pFp) | ||
| 129 | { | ||
| 130 | return fclose(pFp); | ||
| 131 | } | ||
| 132 | |||
| 133 | int32_t WelsGetTimeOfDay(SWelsTime * pTp) | ||
| 134 | { | ||
| 135 | return _ftime_s(pTp); | ||
| 136 | } | ||
| 137 | |||
| 138 | int32_t WelsStrftime(str_t * pBuffer, int32_t iSize, const str_t * kpFormat, const SWelsTime * kpTp) | ||
| 139 | { | ||
| 140 | struct tm sTimeNow; | ||
| 141 | |||
| 142 | localtime_s(&sTimeNow, &kpTp->time); | ||
| 143 | |||
| 144 | return strftime(pBuffer, iSize, kpFormat, &sTimeNow); | ||
| 145 | } | ||
| 146 | |||
| 147 | #else | ||
| 148 | |||
| 149 | int32_t WelsSnprintf(str_t * pBuffer, int32_t iSizeOfBuffer, const str_t * kpFormat, ...) | ||
| 150 | { | ||
| 151 | va_list pArgPtr; | ||
| 152 | int32_t iRc; | ||
| 153 | |||
| 154 | va_start(pArgPtr, kpFormat); | ||
| 155 | |||
| 156 | iRc = vsprintf(pBuffer, kpFormat, pArgPtr);//confirmed_safe_unsafe_usage | ||
| 157 | |||
| 158 | va_end(pArgPtr); | ||
| 159 | |||
| 160 | return iRc; | ||
| 161 | } | ||
| 162 | |||
| 163 | str_t* WelsStrncpy(str_t * pDest, int32_t iSizeInBytes, const str_t * kpSrc, int32_t iCount) | ||
| 164 | { | ||
| 165 | strncpy(pDest, kpSrc, iCount);//confirmed_safe_unsafe_usage | ||
| 166 | |||
| 167 | return pDest; | ||
| 168 | } | ||
| 169 | |||
| 170 | int32_t WelsStrnlen(const str_t * kpStr, int32_t iMaxlen) | ||
| 171 | { | ||
| 172 | return strlen(kpStr);//confirmed_safe_unsafe_usage | ||
| 173 | } | ||
| 174 | |||
| 175 | int32_t WelsVsprintf(str_t * pBuffer, int32_t iSizeOfBuffer, const str_t * kpFormat, va_list pArgPtr) | ||
| 176 | { | ||
| 177 | return vsprintf(pBuffer, kpFormat, pArgPtr);//confirmed_safe_unsafe_usage | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | WelsFileHandle* WelsFopen(const str_t * kpFilename, const str_t * kpMode) | ||
| 182 | { | ||
| 183 | return fopen(kpFilename, kpMode); | ||
| 184 | } | ||
| 185 | |||
| 186 | int32_t WelsFclose(WelsFileHandle* pFp) | ||
| 187 | { | ||
| 188 | return fclose(pFp); | ||
| 189 | } | ||
| 190 | |||
| 191 | int32_t WelsGetTimeOfDay(SWelsTime * pTp) | ||
| 192 | { | ||
| 193 | return _ftime(pTp); | ||
| 194 | } | ||
| 195 | |||
| 196 | int32_t WelsStrftime(str_t * pBuffer, int32_t iSize, const str_t * kpFormat, const SWelsTime * kpTp) | ||
| 197 | { | ||
| 198 | struct tm * pTnow; | ||
| 199 | |||
| 200 | pTnow = localtime(&kpTp->time); | ||
| 201 | |||
| 202 | return strftime(pBuffer, iSize, kpFormat, pTnow); | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | #endif // _MSC_VER | ||
| 207 | |||
| 208 | #else //GCC | ||
| 209 | |||
| 210 | int32_t WelsSnprintf(str_t * pBuffer, int32_t iSizeOfBuffer, const str_t * kpFormat, ...) | ||
| 211 | { | ||
| 212 | va_list pArgPtr; | ||
| 213 | int32_t iRc; | ||
| 214 | |||
| 215 | va_start(pArgPtr, kpFormat); | ||
| 216 | |||
| 217 | iRc = vsnprintf(pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); | ||
| 218 | |||
| 219 | va_end(pArgPtr); | ||
| 220 | |||
| 221 | return iRc; | ||
| 222 | } | ||
| 223 | |||
| 224 | str_t* WelsStrncpy(str_t * pDest, int32_t iSizeInBytes, const str_t * kpSrc, int32_t iCount) | ||
| 225 | { | ||
| 226 | return strncpy(pDest, kpSrc, iCount);//confirmed_safe_unsafe_usage | ||
| 227 | } | ||
| 228 | |||
| 229 | #if !defined(MACOS) && !defined(UNIX) && !defined(APPLE_IOS) | ||
| 230 | int32_t WelsStrnlen(const str_t * kpStr, int32_t iMaxlen) | ||
| 231 | { | ||
| 232 | return strnlen(kpStr, iMaxlen);//confirmed_safe_unsafe_usage | ||
| 233 | } | ||
| 234 | #else | ||
| 235 | int32_t WelsStrnlen(const str_t *kpString, int32_t iMaxlen) | ||
| 236 | { | ||
| 237 | // In mac os, there is no strnlen in string.h, we can only use strlen instead of strnlen or | ||
| 238 | // implement strnlen by ourself | ||
| 239 | |||
| 240 | #if 1 | ||
| 241 | return strlen(pString);//confirmed_safe_unsafe_usage | ||
| 242 | #else | ||
| 243 | const str_t *kpSrc; | ||
| 244 | for (kpSrc = kpString; iMaxlen-- && *kpSrc != '\0'; ++kpSrc) | ||
| 245 | return kpSrc - kpString; | ||
| 246 | #endif | ||
| 247 | |||
| 248 | } | ||
| 249 | #endif | ||
| 250 | |||
| 251 | int32_t WelsVsprintf(str_t * pBuffer, int32_t iSizeOfBuffer, const str_t * kpFormat, va_list pArgPtr) | ||
| 252 | { | ||
| 253 | return vsprintf(pBuffer, kpFormat, pArgPtr);//confirmed_safe_unsafe_usage | ||
| 254 | } | ||
| 255 | |||
| 256 | WelsFileHandle* WelsFopen(const str_t * kpFilename, const str_t * kpMode) | ||
| 257 | { | ||
| 258 | return fopen(kpFilename, kpMode); | ||
| 259 | } | ||
| 260 | |||
| 261 | int32_t WelsFclose(WelsFileHandle * pFp) | ||
| 262 | { | ||
| 263 | return fclose(pFp); | ||
| 264 | } | ||
| 265 | |||
| 266 | int32_t WelsGetTimeOfDay(SWelsTime * pTp) | ||
| 267 | { | ||
| 268 | struct timeval sTv; | ||
| 269 | |||
| 270 | if( gettimeofday(&sTv, NULL) ){ | ||
| 271 | return -1; | ||
| 272 | } | ||
| 273 | |||
| 274 | pTp->time = sTv.tv_sec; | ||
| 275 | pTp->millitm = (uint16_t)sTv.tv_usec/1000; | ||
| 276 | |||
| 277 | return 0; | ||
| 278 | } | ||
| 279 | |||
| 280 | int32_t WelsStrftime(str_t * pBuffer, int32_t iSize, const str_t * kpFormat, const SWelsTime * kpTp) | ||
| 281 | { | ||
| 282 | struct tm * pTnow; | ||
| 283 | |||
| 284 | pTnow = localtime(&kpTp->time); | ||
| 285 | |||
| 286 | return strftime(pBuffer, iSize, kpFormat, pTnow); | ||
| 287 | } | ||
| 288 | |||
| 289 | #endif | ||
| 290 | |||
| 291 | |||
| 292 | int32_t WelsFwrite(const void_t * kpBuffer, int32_t iSize, int32_t iCount, WelsFileHandle* pFp) | ||
| 293 | { | ||
| 294 | return fwrite(kpBuffer, iSize, iCount, pFp); | ||
| 295 | } | ||
| 296 | |||
| 297 | uint16_t WelsGetMillsecond(const SWelsTime * kpTp) | ||
| 298 | { | ||
| 299 | return kpTp->millitm; | ||
| 300 | } | ||
| 301 | |||
| 302 | int32_t WelsFflush(WelsFileHandle* pFp) | ||
| 303 | { | ||
| 304 | return fflush(pFp); | ||
| 305 | } | ||
| 306 | |||
| 307 | } // namespace WelsDec | ||