Showing the changes to codec/processing/src/downsample/downsamplefuncs.cpp (1 of 124 files). Show all
Unified · Split
| @@ -0,0 +1,234 @@ | |||
| 1 | /*! | ||
| 2 | * \copy | ||
| 3 | * Copyright (c) 2008-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 | * downsample_yuv.c | ||
| 32 | * | ||
| 33 | * Abstract | ||
| 34 | * Implementation for source yuv data downsampling used before spatial encoding. | ||
| 35 | * | ||
| 36 | * History | ||
| 37 | * 10/24/2008 Created | ||
| 38 | * | ||
| 39 | *****************************************************************************/ | ||
| 40 | |||
| 41 | #include "../common/typedef.h" | ||
| 42 | #include "../common/util.h" | ||
| 43 | #include "downsample.h" | ||
| 44 | |||
| 45 | |||
| 46 | WELSVP_NAMESPACE_BEGIN | ||
| 47 | |||
| 48 | |||
| 49 | void DyadicBilinearDownsampler_c (uint8_t* pDst, const int32_t kiDstStride, | ||
| 50 | uint8_t* pSrc, const int32_t kiSrcStride, | ||
| 51 | const int32_t kiSrcWidth, const int32_t kiSrcHeight) | ||
| 52 | |||
| 53 | { | ||
| 54 | uint8_t* pDstLine = pDst; | ||
| 55 | uint8_t* pSrcLine = pSrc; | ||
| 56 | const int32_t kiSrcStridex2 = kiSrcStride << 1; | ||
| 57 | const int32_t kiDstWidth = kiSrcWidth >> 1; | ||
| 58 | const int32_t kiDstHeight = kiSrcHeight >> 1; | ||
| 59 | |||
| 60 | for (int32_t j = 0; j < kiDstHeight; j ++) { | ||
| 61 | for (int32_t i = 0; i < kiDstWidth; i ++) { | ||
| 62 | const int32_t kiSrcX = i << 1; | ||
| 63 | const int32_t kiTempRow1 = (pSrcLine[kiSrcX] + pSrcLine[kiSrcX + 1] + 1) >> 1; | ||
| 64 | const int32_t kiTempRow2 = (pSrcLine[kiSrcX + kiSrcStride] + pSrcLine[kiSrcX + kiSrcStride + 1] + 1) >> 1; | ||
| 65 | |||
| 66 | pDstLine[i] = (uint8_t) ((kiTempRow1 + kiTempRow2 + 1) >> 1); | ||
| 67 | } | ||
| 68 | pDstLine += kiDstStride; | ||
| 69 | pSrcLine += kiSrcStridex2; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | void GeneralBilinearFastDownsampler_c (uint8_t* pDst, const int32_t kiDstStride, const int32_t kiDstWidth, | ||
| 74 | const int32_t kiDstHeight, | ||
| 75 | uint8_t* pSrc, const int32_t kiSrcStride, const int32_t kiSrcWidth, const int32_t kiSrcHeight) { | ||
| 76 | const uint32_t kuiScaleBitWidth = 16, kuiScaleBitHeight = 15; | ||
| 77 | const uint32_t kuiScaleWidth = (1 << kuiScaleBitWidth), kuiScaleHeight = (1 << kuiScaleBitHeight); | ||
| 78 | int32_t fScalex = (int32_t) ((float_t)kiSrcWidth / (float_t)kiDstWidth * kuiScaleWidth); | ||
| 79 | int32_t fScaley = (int32_t) ((float_t)kiSrcHeight / (float_t)kiDstHeight * kuiScaleHeight); | ||
| 80 | uint32_t x; | ||
| 81 | int32_t iYInverse, iXInverse; | ||
| 82 | |||
| 83 | uint8_t* pByDst = pDst; | ||
| 84 | uint8_t* pByLineDst = pDst; | ||
| 85 | |||
| 86 | iYInverse = 1 << (kuiScaleBitHeight - 1); | ||
| 87 | for (int32_t i = 0; i < kiDstHeight - 1; i++) { | ||
| 88 | int32_t iYy = iYInverse >> kuiScaleBitHeight; | ||
| 89 | int32_t fv = iYInverse & (kuiScaleHeight - 1); | ||
| 90 | |||
| 91 | uint8_t* pBySrc = pSrc + iYy * kiSrcStride; | ||
| 92 | |||
| 93 | pByDst = pByLineDst; | ||
| 94 | iXInverse = 1 << (kuiScaleBitWidth - 1); | ||
| 95 | for (int32_t j = 0; j < kiDstWidth - 1; j++) { | ||
| 96 | int32_t iXx = iXInverse >> kuiScaleBitWidth; | ||
| 97 | int32_t iFu = iXInverse & (kuiScaleWidth - 1); | ||
| 98 | |||
| 99 | uint8_t* pByCurrent = pBySrc + iXx; | ||
| 100 | uint8_t a, b, c, d; | ||
| 101 | |||
| 102 | a = *pByCurrent; | ||
| 103 | b = * (pByCurrent + 1); | ||
| 104 | c = * (pByCurrent + kiSrcStride); | ||
| 105 | d = * (pByCurrent + kiSrcStride + 1); | ||
| 106 | |||
| 107 | x = (((uint32_t) (kuiScaleWidth - 1 - iFu)) * (kuiScaleHeight - 1 - fv) >> kuiScaleBitWidth) * a; | ||
| 108 | x += (((uint32_t) (iFu)) * (kuiScaleHeight - 1 - fv) >> kuiScaleBitWidth) * b; | ||
| 109 | x += (((uint32_t) (kuiScaleWidth - 1 - iFu)) * (fv) >> kuiScaleBitWidth) * c; | ||
| 110 | x += (((uint32_t) (iFu)) * (fv) >> kuiScaleBitWidth) * d; | ||
| 111 | x >>= (kuiScaleBitHeight - 1); | ||
| 112 | x += 1; | ||
| 113 | x >>= 1; | ||
| 114 | //x = (((__int64)(SCALE_BIG - 1 - iFu))*(SCALE_BIG - 1 - fv)*a + ((__int64)iFu)*(SCALE_BIG - 1 -fv)*b + ((__int64)(SCALE_BIG - 1 -iFu))*fv*c + | ||
| 115 | // ((__int64)iFu)*fv*d + (1 << (2*SCALE_BIT_BIG-1)) ) >> (2*SCALE_BIT_BIG); | ||
| 116 | x = WELS_CLAMP (x, 0, 255); | ||
| 117 | *pByDst++ = (uint8_t)x; | ||
| 118 | |||
| 119 | iXInverse += fScalex; | ||
| 120 | } | ||
| 121 | *pByDst = * (pBySrc + (iXInverse >> kuiScaleBitWidth)); | ||
| 122 | pByLineDst += kiDstStride; | ||
| 123 | iYInverse += fScaley; | ||
| 124 | } | ||
| 125 | |||
| 126 | // last row special | ||
| 127 | { | ||
| 128 | int32_t iYy = iYInverse >> kuiScaleBitHeight; | ||
| 129 | uint8_t* pBySrc = pSrc + iYy * kiSrcStride; | ||
| 130 | |||
| 131 | pByDst = pByLineDst; | ||
| 132 | iXInverse = 1 << (kuiScaleBitWidth - 1); | ||
| 133 | for (int32_t j = 0; j < kiDstWidth; j++) { | ||
| 134 | int32_t iXx = iXInverse >> kuiScaleBitWidth; | ||
| 135 | *pByDst++ = * (pBySrc + iXx); | ||
| 136 | |||
| 137 | iXInverse += fScalex; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | void GeneralBilinearAccurateDownsampler_c (uint8_t* pDst, const int32_t kiDstStride, const int32_t kiDstWidth, | ||
| 143 | const int32_t kiDstHeight, | ||
| 144 | uint8_t* pSrc, const int32_t kiSrcStride, const int32_t kiSrcWidth, const int32_t kiSrcHeight) { | ||
| 145 | const int32_t kiScaleBit = 15; | ||
| 146 | const int32_t kiScale = (1 << kiScaleBit); | ||
| 147 | int32_t iScalex = (int32_t) ((float_t)kiSrcWidth / (float_t)kiDstWidth * kiScale); | ||
| 148 | int32_t iScaley = (int32_t) ((float_t)kiSrcHeight / (float_t)kiDstHeight * kiScale); | ||
| 149 | int64_t x; | ||
| 150 | int32_t iYInverse, iXInverse; | ||
| 151 | |||
| 152 | uint8_t* pByDst = pDst; | ||
| 153 | uint8_t* pByLineDst = pDst; | ||
| 154 | |||
| 155 | iYInverse = 1 << (kiScaleBit - 1); | ||
| 156 | for (int32_t i = 0; i < kiDstHeight - 1; i++) { | ||
| 157 | int32_t iYy = iYInverse >> kiScaleBit; | ||
| 158 | int32_t iFv = iYInverse & (kiScale - 1); | ||
| 159 | |||
| 160 | uint8_t* pBySrc = pSrc + iYy * kiSrcStride; | ||
| 161 | |||
| 162 | pByDst = pByLineDst; | ||
| 163 | iXInverse = 1 << (kiScaleBit - 1); | ||
| 164 | for (int32_t j = 0; j < kiDstWidth - 1; j++) { | ||
| 165 | int32_t iXx = iXInverse >> kiScaleBit; | ||
| 166 | int32_t iFu = iXInverse & (kiScale - 1); | ||
| 167 | |||
| 168 | uint8_t* pByCurrent = pBySrc + iXx; | ||
| 169 | uint8_t a, b, c, d; | ||
| 170 | |||
| 171 | a = *pByCurrent; | ||
| 172 | b = * (pByCurrent + 1); | ||
| 173 | c = * (pByCurrent + kiSrcStride); | ||
| 174 | d = * (pByCurrent + kiSrcStride + 1); | ||
| 175 | |||
| 176 | x = (((int64_t) (kiScale - 1 - iFu)) * (kiScale - 1 - iFv) * a + ((int64_t)iFu) * (kiScale - 1 - iFv) * b + ((int64_t) ( | ||
| 177 | kiScale - 1 - iFu)) * iFv * c + | ||
| 178 | ((int64_t)iFu) * iFv * d + (int64_t) (1 << (2 * kiScaleBit - 1))) >> (2 * kiScaleBit); | ||
| 179 | x = WELS_CLAMP (x, 0, 255); | ||
| 180 | *pByDst++ = (uint8_t)x; | ||
| 181 | |||
| 182 | iXInverse += iScalex; | ||
| 183 | } | ||
| 184 | *pByDst = * (pBySrc + (iXInverse >> kiScaleBit)); | ||
| 185 | pByLineDst += kiDstStride; | ||
| 186 | iYInverse += iScaley; | ||
| 187 | } | ||
| 188 | |||
| 189 | // last row special | ||
| 190 | { | ||
| 191 | int32_t iYy = iYInverse >> kiScaleBit; | ||
| 192 | uint8_t* pBySrc = pSrc + iYy * kiSrcStride; | ||
| 193 | |||
| 194 | pByDst = pByLineDst; | ||
| 195 | iXInverse = 1 << (kiScaleBit - 1); | ||
| 196 | for (int32_t j = 0; j < kiDstWidth; j++) { | ||
| 197 | int32_t iXx = iXInverse >> kiScaleBit; | ||
| 198 | *pByDst++ = * (pBySrc + iXx); | ||
| 199 | |||
| 200 | iXInverse += iScalex; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | #ifdef X86_ASM | ||
| 207 | //void GeneralBilinearFastDownsamplerWrap_sse2 (uint8_t* pDst, const int32_t kiDstStride, const int32_t kiDstWidth, | ||
| 208 | // const int32_t kiDstHeight, | ||
| 209 | // uint8_t* pSrc, const int32_t kiSrcStride, const int32_t kiSrcWidth, const int32_t kiSrcHeight) { | ||
| 210 | // const int32_t kiScaleBitWidth = 16, kiScaleBitHeight = 15; | ||
| 211 | // const uint32_t kuiScaleWidth = (1 << kiScaleBitWidth), kuiScaleHeight = (1 << kiScaleBitHeight); | ||
| 212 | // | ||
| 213 | // uint32_t uiScalex = (uint32_t) ((float_t)kiSrcWidth / (float_t)kiDstWidth * kuiScaleWidth); | ||
| 214 | // uint32_t uiScaley = (uint32_t) ((float_t)kiSrcHeight / (float_t)kiDstHeight * kuiScaleHeight); | ||
| 215 | // | ||
| 216 | // GeneralBilinearFastDownsampler_sse2 (pDst, kiDstStride, kiDstWidth, kiDstHeight, | ||
| 217 | // pSrc, kiSrcStride, kiSrcWidth, kiSrcHeight, uiScalex, uiScaley); | ||
| 218 | //} | ||
| 219 | // | ||
| 220 | //void GeneralBilinearAccurateDownsamplerWrap_sse2 (uint8_t* pDst, const int32_t kiDstStride, const int32_t kiDstWidth, | ||
| 221 | // const int32_t kiDstHeight, | ||
| 222 | // uint8_t* pSrc, const int32_t kiSrcStride, const int32_t kiSrcWidth, const int32_t kiSrcHeight) { | ||
| 223 | // const int32_t kiScaleBit = 15; | ||
| 224 | // const uint32_t kuiScale = (1 << kiScaleBit); | ||
| 225 | // | ||
| 226 | // uint32_t uiScalex = (uint32_t) ((float_t)kiSrcWidth / (float_t)kiDstWidth * kuiScale); | ||
| 227 | // uint32_t uiScaley = (uint32_t) ((float_t)kiSrcHeight / (float_t)kiDstHeight * kuiScale); | ||
| 228 | // | ||
| 229 | // GeneralBilinearAccurateDownsampler_sse2 (pDst, kiDstStride, kiDstWidth, kiDstHeight, | ||
| 230 | // pSrc, kiSrcStride, kiSrcWidth, kiSrcHeight, uiScalex, uiScaley); | ||
| 231 | //} | ||
| 232 | #endif //X86_ASM | ||
| 233 | |||
| 234 | WELSVP_NAMESPACE_END | ||