[313] | 1 | /* The copyright in this software is being made available under the BSD |
---|
| 2 | * License, included below. This software may be subject to other third party |
---|
| 3 | * and contributor rights, including patent rights, and no such rights are |
---|
[1029] | 4 | * granted under this license. |
---|
[313] | 5 | * |
---|
[1259] | 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
[313] | 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright notice, |
---|
| 13 | * this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
| 15 | * this list of conditions and the following disclaimer in the documentation |
---|
| 16 | * and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
| 18 | * be used to endorse or promote products derived from this software without |
---|
| 19 | * specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
| 31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** \file TComWeightPrediction.h |
---|
| 35 | \brief weighting prediction class (header) |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | // Include files |
---|
[1262] | 39 | #include "CommonDef.h" |
---|
| 40 | #include "TComYuv.h" |
---|
| 41 | #include "TComPic.h" |
---|
| 42 | #include "TComInterpolationFilter.h" |
---|
[313] | 43 | #include "TComWeightPrediction.h" |
---|
| 44 | |
---|
[1029] | 45 | |
---|
| 46 | static inline Pel weightBidir( Int w0, Pel P0, Int w1, Pel P1, Int round, Int shift, Int offset, Int clipBD) |
---|
[313] | 47 | { |
---|
[1029] | 48 | return ClipBD( ( (w0*(P0 + IF_INTERNAL_OFFS) + w1*(P1 + IF_INTERNAL_OFFS) + round + (offset << (shift-1))) >> shift ), clipBD ); |
---|
[313] | 49 | } |
---|
| 50 | |
---|
[1029] | 51 | |
---|
| 52 | static inline Pel weightUnidir( Int w0, Pel P0, Int round, Int shift, Int offset, Int clipBD) |
---|
[313] | 53 | { |
---|
[1029] | 54 | return ClipBD( ( (w0*(P0 + IF_INTERNAL_OFFS) + round) >> shift ) + offset, clipBD ); |
---|
[313] | 55 | } |
---|
| 56 | |
---|
| 57 | // ==================================================================================================================== |
---|
| 58 | // Class definition |
---|
| 59 | // ==================================================================================================================== |
---|
[1029] | 60 | |
---|
[313] | 61 | TComWeightPrediction::TComWeightPrediction() |
---|
| 62 | { |
---|
| 63 | } |
---|
| 64 | |
---|
[1029] | 65 | |
---|
[1260] | 66 | //! weighted averaging for bi-pred |
---|
[1029] | 67 | Void TComWeightPrediction::addWeightBi( const TComYuv *pcYuvSrc0, |
---|
| 68 | const TComYuv *pcYuvSrc1, |
---|
| 69 | const UInt iPartUnitIdx, |
---|
| 70 | const UInt uiWidth, |
---|
| 71 | const UInt uiHeight, |
---|
| 72 | const WPScalingParam *const wp0, |
---|
| 73 | const WPScalingParam *const wp1, |
---|
| 74 | TComYuv *const rpcYuvDst, |
---|
| 75 | const Bool bRoundLuma ) |
---|
[313] | 76 | { |
---|
| 77 | |
---|
[1029] | 78 | const Bool enableRounding[MAX_NUM_COMPONENT]={ bRoundLuma, true, true }; |
---|
[313] | 79 | |
---|
[1029] | 80 | const UInt numValidComponent = pcYuvSrc0->getNumberValidComponents(); |
---|
| 81 | |
---|
| 82 | for(Int componentIndex=0; componentIndex<numValidComponent; componentIndex++) |
---|
[313] | 83 | { |
---|
[1029] | 84 | const ComponentID compID=ComponentID(componentIndex); |
---|
[313] | 85 | |
---|
[1029] | 86 | const Pel* pSrc0 = pcYuvSrc0->getAddr( compID, iPartUnitIdx ); |
---|
| 87 | const Pel* pSrc1 = pcYuvSrc1->getAddr( compID, iPartUnitIdx ); |
---|
| 88 | Pel* pDst = rpcYuvDst->getAddr( compID, iPartUnitIdx ); |
---|
[313] | 89 | |
---|
[1029] | 90 | // Luma : -------------------------------------------- |
---|
| 91 | const Int w0 = wp0[compID].w; |
---|
| 92 | const Int offset = wp0[compID].offset; |
---|
| 93 | const Int clipBD = g_bitDepth[toChannelType(compID)]; |
---|
| 94 | const Int shiftNum = std::max<Int>(2, (IF_INTERNAL_PREC - clipBD)); |
---|
| 95 | const Int shift = wp0[compID].shift + shiftNum; |
---|
| 96 | const Int round = (enableRounding[compID] && (shift > 0)) ? (1<<(shift-1)) : 0; |
---|
| 97 | const Int w1 = wp1[compID].w; |
---|
| 98 | const UInt csx = pcYuvSrc0->getComponentScaleX(compID); |
---|
| 99 | const UInt csy = pcYuvSrc0->getComponentScaleY(compID); |
---|
| 100 | const Int iHeight = uiHeight>>csy; |
---|
| 101 | const Int iWidth = uiWidth>>csx; |
---|
[313] | 102 | |
---|
[1029] | 103 | const UInt iSrc0Stride = pcYuvSrc0->getStride(compID); |
---|
| 104 | const UInt iSrc1Stride = pcYuvSrc1->getStride(compID); |
---|
| 105 | const UInt iDstStride = rpcYuvDst->getStride(compID); |
---|
[313] | 106 | |
---|
[1029] | 107 | for ( Int y = iHeight-1; y >= 0; y-- ) |
---|
[313] | 108 | { |
---|
[1029] | 109 | // do it in batches of 4 (partial unroll) |
---|
| 110 | Int x = iWidth-1; |
---|
| 111 | for ( ; x >= 3; ) |
---|
| 112 | { |
---|
| 113 | pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--; |
---|
| 114 | pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--; |
---|
| 115 | pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--; |
---|
| 116 | pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--; |
---|
| 117 | } |
---|
| 118 | for( ; x >= 0; x-- ) |
---|
| 119 | { |
---|
| 120 | pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | pSrc0 += iSrc0Stride; |
---|
| 124 | pSrc1 += iSrc1Stride; |
---|
| 125 | pDst += iDstStride; |
---|
| 126 | } // y loop |
---|
| 127 | } // compID loop |
---|
[313] | 128 | } |
---|
| 129 | |
---|
[1029] | 130 | |
---|
[1260] | 131 | //! weighted averaging for uni-pred |
---|
[1029] | 132 | Void TComWeightPrediction::addWeightUni( const TComYuv *const pcYuvSrc0, |
---|
| 133 | const UInt iPartUnitIdx, |
---|
| 134 | const UInt uiWidth, |
---|
| 135 | const UInt uiHeight, |
---|
| 136 | const WPScalingParam *const wp0, |
---|
| 137 | TComYuv *const pcYuvDst ) |
---|
[313] | 138 | { |
---|
[1029] | 139 | const UInt numValidComponent = pcYuvSrc0->getNumberValidComponents(); |
---|
[313] | 140 | |
---|
[1029] | 141 | for(Int componentIndex=0; componentIndex<numValidComponent; componentIndex++) |
---|
[313] | 142 | { |
---|
[1029] | 143 | const ComponentID compID=ComponentID(componentIndex); |
---|
[313] | 144 | |
---|
[1029] | 145 | const Pel* pSrc0 = pcYuvSrc0->getAddr( compID, iPartUnitIdx ); |
---|
| 146 | Pel* pDst = pcYuvDst->getAddr( compID, iPartUnitIdx ); |
---|
[313] | 147 | |
---|
[1029] | 148 | // Luma : -------------------------------------------- |
---|
| 149 | const Int w0 = wp0[compID].w; |
---|
| 150 | const Int offset = wp0[compID].offset; |
---|
| 151 | const Int clipBD = g_bitDepth[toChannelType(compID)]; |
---|
| 152 | const Int shiftNum = std::max<Int>(2, (IF_INTERNAL_PREC - clipBD)); |
---|
| 153 | const Int shift = wp0[compID].shift + shiftNum; |
---|
| 154 | const Int round = (shift > 0) ? (1<<(shift-1)) : 0; |
---|
| 155 | const UInt iSrc0Stride = pcYuvSrc0->getStride(compID); |
---|
| 156 | const UInt iDstStride = pcYuvDst->getStride(compID); |
---|
| 157 | const UInt csx = pcYuvSrc0->getComponentScaleX(compID); |
---|
| 158 | const UInt csy = pcYuvSrc0->getComponentScaleY(compID); |
---|
| 159 | const Int iHeight = uiHeight>>csy; |
---|
| 160 | const Int iWidth = uiWidth>>csx; |
---|
| 161 | |
---|
| 162 | for (Int y = iHeight-1; y >= 0; y-- ) |
---|
[313] | 163 | { |
---|
[1029] | 164 | Int x = iWidth-1; |
---|
| 165 | for ( ; x >= 3; ) |
---|
| 166 | { |
---|
| 167 | pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--; |
---|
| 168 | pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--; |
---|
| 169 | pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--; |
---|
| 170 | pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--; |
---|
| 171 | } |
---|
| 172 | for( ; x >= 0; x--) |
---|
| 173 | { |
---|
| 174 | pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); |
---|
| 175 | } |
---|
| 176 | pSrc0 += iSrc0Stride; |
---|
| 177 | pDst += iDstStride; |
---|
[313] | 178 | } |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
[1029] | 182 | |
---|
[313] | 183 | //======================================================= |
---|
| 184 | // getWpScaling() |
---|
| 185 | //======================================================= |
---|
[1260] | 186 | //! derivation of wp tables |
---|
[1029] | 187 | Void TComWeightPrediction::getWpScaling( TComDataCU *const pcCU, |
---|
| 188 | const Int iRefIdx0, |
---|
| 189 | const Int iRefIdx1, |
---|
| 190 | WPScalingParam *&wp0, |
---|
| 191 | WPScalingParam *&wp1) |
---|
[313] | 192 | { |
---|
| 193 | assert(iRefIdx0 >= 0 || iRefIdx1 >= 0); |
---|
| 194 | |
---|
[1029] | 195 | TComSlice *const pcSlice = pcCU->getSlice(); |
---|
| 196 | const Bool wpBiPred = pcCU->getSlice()->getPPS()->getWPBiPred(); |
---|
| 197 | const Bool bBiDir = (iRefIdx0>=0 && iRefIdx1>=0); |
---|
| 198 | const Bool bUniDir = !bBiDir; |
---|
| 199 | |
---|
[313] | 200 | if ( bUniDir || wpBiPred ) |
---|
| 201 | { // explicit -------------------- |
---|
| 202 | if ( iRefIdx0 >= 0 ) |
---|
| 203 | { |
---|
| 204 | pcSlice->getWpScaling(REF_PIC_LIST_0, iRefIdx0, wp0); |
---|
| 205 | } |
---|
| 206 | if ( iRefIdx1 >= 0 ) |
---|
| 207 | { |
---|
| 208 | pcSlice->getWpScaling(REF_PIC_LIST_1, iRefIdx1, wp1); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | else |
---|
| 212 | { |
---|
| 213 | assert(0); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if ( iRefIdx0 < 0 ) |
---|
| 217 | { |
---|
| 218 | wp0 = NULL; |
---|
| 219 | } |
---|
| 220 | if ( iRefIdx1 < 0 ) |
---|
| 221 | { |
---|
| 222 | wp1 = NULL; |
---|
| 223 | } |
---|
| 224 | |
---|
[1029] | 225 | const UInt numValidComponent = pcCU->getPic()->getNumberValidComponents(); |
---|
| 226 | const Bool bUseHighPrecisionPredictionWeighting = pcSlice->getSPS()->getUseHighPrecisionPredictionWeighting(); |
---|
| 227 | |
---|
[313] | 228 | if ( bBiDir ) |
---|
| 229 | { // Bi-Dir case |
---|
[1029] | 230 | for ( Int yuv=0 ; yuv<numValidComponent ; yuv++ ) |
---|
[313] | 231 | { |
---|
[1029] | 232 | const Int bitDepth = g_bitDepth[toChannelType(ComponentID(yuv))]; |
---|
| 233 | const Int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (bitDepth-8)); |
---|
| 234 | |
---|
[313] | 235 | wp0[yuv].w = wp0[yuv].iWeight; |
---|
| 236 | wp1[yuv].w = wp1[yuv].iWeight; |
---|
[1029] | 237 | wp0[yuv].o = wp0[yuv].iOffset * offsetScalingFactor; |
---|
| 238 | wp1[yuv].o = wp1[yuv].iOffset * offsetScalingFactor; |
---|
[313] | 239 | wp0[yuv].offset = wp0[yuv].o + wp1[yuv].o; |
---|
| 240 | wp0[yuv].shift = wp0[yuv].uiLog2WeightDenom + 1; |
---|
| 241 | wp0[yuv].round = (1 << wp0[yuv].uiLog2WeightDenom); |
---|
| 242 | wp1[yuv].offset = wp0[yuv].offset; |
---|
| 243 | wp1[yuv].shift = wp0[yuv].shift; |
---|
| 244 | wp1[yuv].round = wp0[yuv].round; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | else |
---|
| 248 | { // Unidir |
---|
[1029] | 249 | WPScalingParam *const pwp = (iRefIdx0>=0) ? wp0 : wp1 ; |
---|
| 250 | |
---|
| 251 | for ( Int yuv=0 ; yuv<numValidComponent ; yuv++ ) |
---|
[313] | 252 | { |
---|
[1029] | 253 | const Int bitDepth = g_bitDepth[toChannelType(ComponentID(yuv))]; |
---|
| 254 | const Int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (bitDepth-8)); |
---|
| 255 | |
---|
[313] | 256 | pwp[yuv].w = pwp[yuv].iWeight; |
---|
[1029] | 257 | pwp[yuv].offset = pwp[yuv].iOffset * offsetScalingFactor; |
---|
[313] | 258 | pwp[yuv].shift = pwp[yuv].uiLog2WeightDenom; |
---|
| 259 | pwp[yuv].round = (pwp[yuv].uiLog2WeightDenom>=1) ? (1 << (pwp[yuv].uiLog2WeightDenom-1)) : (0); |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | |
---|
[1029] | 264 | |
---|
[1260] | 265 | //! weighted prediction for bi-pred |
---|
[1029] | 266 | Void TComWeightPrediction::xWeightedPredictionBi( TComDataCU *const pcCU, |
---|
| 267 | const TComYuv *const pcYuvSrc0, |
---|
| 268 | const TComYuv *const pcYuvSrc1, |
---|
| 269 | const Int iRefIdx0, |
---|
| 270 | const Int iRefIdx1, |
---|
| 271 | const UInt uiPartIdx, |
---|
| 272 | const Int iWidth, |
---|
| 273 | const Int iHeight, |
---|
| 274 | TComYuv *rpcYuvDst ) |
---|
[313] | 275 | { |
---|
[1029] | 276 | WPScalingParam *pwp0; |
---|
| 277 | WPScalingParam *pwp1; |
---|
[313] | 278 | |
---|
[1029] | 279 | assert(pcCU->getSlice()->getPPS()->getWPBiPred()); |
---|
| 280 | |
---|
[313] | 281 | getWpScaling(pcCU, iRefIdx0, iRefIdx1, pwp0, pwp1); |
---|
| 282 | |
---|
| 283 | if( iRefIdx0 >= 0 && iRefIdx1 >= 0 ) |
---|
| 284 | { |
---|
| 285 | addWeightBi(pcYuvSrc0, pcYuvSrc1, uiPartIdx, iWidth, iHeight, pwp0, pwp1, rpcYuvDst ); |
---|
| 286 | } |
---|
| 287 | else if ( iRefIdx0 >= 0 && iRefIdx1 < 0 ) |
---|
| 288 | { |
---|
| 289 | addWeightUni( pcYuvSrc0, uiPartIdx, iWidth, iHeight, pwp0, rpcYuvDst ); |
---|
| 290 | } |
---|
| 291 | else if ( iRefIdx0 < 0 && iRefIdx1 >= 0 ) |
---|
| 292 | { |
---|
| 293 | addWeightUni( pcYuvSrc1, uiPartIdx, iWidth, iHeight, pwp1, rpcYuvDst ); |
---|
| 294 | } |
---|
| 295 | else |
---|
| 296 | { |
---|
| 297 | assert (0); |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
[1029] | 301 | |
---|
[1260] | 302 | //! weighted prediction for uni-pred |
---|
[1029] | 303 | Void TComWeightPrediction::xWeightedPredictionUni( TComDataCU *const pcCU, |
---|
| 304 | const TComYuv *const pcYuvSrc, |
---|
| 305 | const UInt uiPartAddr, |
---|
| 306 | const Int iWidth, |
---|
| 307 | const Int iHeight, |
---|
| 308 | const RefPicList eRefPicList, |
---|
| 309 | TComYuv *pcYuvPred, |
---|
| 310 | const Int iRefIdx_input) |
---|
| 311 | { |
---|
| 312 | WPScalingParam *pwp, *pwpTmp; |
---|
| 313 | |
---|
| 314 | Int iRefIdx=iRefIdx_input; |
---|
[313] | 315 | if ( iRefIdx < 0 ) |
---|
| 316 | { |
---|
| 317 | iRefIdx = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr ); |
---|
| 318 | } |
---|
| 319 | assert (iRefIdx >= 0); |
---|
| 320 | |
---|
| 321 | if ( eRefPicList == REF_PIC_LIST_0 ) |
---|
| 322 | { |
---|
| 323 | getWpScaling(pcCU, iRefIdx, -1, pwp, pwpTmp); |
---|
| 324 | } |
---|
| 325 | else |
---|
| 326 | { |
---|
| 327 | getWpScaling(pcCU, -1, iRefIdx, pwpTmp, pwp); |
---|
| 328 | } |
---|
[1029] | 329 | addWeightUni( pcYuvSrc, uiPartAddr, iWidth, iHeight, pwp, pcYuvPred ); |
---|
[313] | 330 | } |
---|