/src/openh264/codec/common/src/utils.cpp
Line | Count | Source |
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 | | |
51 | 10.5M | void WelsLog (SLogContext* logCtx, int32_t iLevel, const char* kpFmt, ...) { |
52 | 10.5M | va_list vl; |
53 | 10.5M | char pTraceTag[MAX_LOG_SIZE] = {0}; |
54 | 10.5M | switch (iLevel) { |
55 | 158k | case WELS_LOG_ERROR: |
56 | 158k | WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Error:", logCtx->pCodecInstance); |
57 | 158k | break; |
58 | 5.76M | case WELS_LOG_WARNING: |
59 | 5.76M | WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Warning:", logCtx->pCodecInstance); |
60 | 5.76M | break; |
61 | 480k | case WELS_LOG_INFO: |
62 | 480k | WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Info:", logCtx->pCodecInstance); |
63 | 480k | break; |
64 | 4.12M | case WELS_LOG_DEBUG: |
65 | 4.12M | WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Debug:", logCtx->pCodecInstance); |
66 | 4.12M | break; |
67 | 0 | default: |
68 | 0 | WelsSnprintf (pTraceTag, MAX_LOG_SIZE, "[OpenH264] this = 0x%p, Detail:", logCtx->pCodecInstance); |
69 | 0 | break; |
70 | 10.5M | } |
71 | 10.5M | WelsStrcat (pTraceTag, MAX_LOG_SIZE, kpFmt); |
72 | 10.5M | va_start (vl, kpFmt); |
73 | 10.5M | logCtx->pfLog (logCtx->pLogCtx, iLevel, pTraceTag, vl); |
74 | 10.5M | va_end (vl); |
75 | 10.5M | } |
76 | | |
77 | | #ifndef CALC_PSNR |
78 | 0 | #define CONST_FACTOR_PSNR (10.0 / log(10.0)) // for good computation |
79 | 0 | #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 | | */ |
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 | 0 | const int32_t kiHeight) { |
107 | 0 | int64_t iSqe = 0; |
108 | 0 | int32_t x, y; |
109 | 0 | uint8_t* pTar = (uint8_t*)kpTarPic; |
110 | 0 | uint8_t* pRef = (uint8_t*)kpRefPic; |
111 | |
|
112 | 0 | if (NULL == pTar || NULL == pRef) |
113 | 0 | return (-1.0f); |
114 | | |
115 | 0 | for (y = 0; y < kiHeight; ++ y) { // OPTable !! |
116 | 0 | for (x = 0; x < kiWidth; ++ x) { |
117 | 0 | const int32_t kiT = pTar[y * kiTarStride + x] - pRef[y * kiRefStride + x]; |
118 | 0 | iSqe += kiT * kiT; |
119 | 0 | } |
120 | 0 | } |
121 | 0 | if (0 == iSqe) { |
122 | 0 | return (99.99f); |
123 | 0 | } |
124 | 0 | return CALC_PSNR (kiWidth, kiHeight, iSqe); |
125 | 0 | } |
126 | | |