[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, |
---|
[1287] | 69 | const BitDepths &bitDepths, |
---|
[1029] | 70 | const UInt iPartUnitIdx, |
---|
| 71 | const UInt uiWidth, |
---|
| 72 | const UInt uiHeight, |
---|
| 73 | const WPScalingParam *const wp0, |
---|
| 74 | const WPScalingParam *const wp1, |
---|
| 75 | TComYuv *const rpcYuvDst, |
---|
[1287] | 76 | const Bool bRoundLuma) |
---|
[313] | 77 | { |
---|
| 78 | |
---|
[1029] | 79 | const Bool enableRounding[MAX_NUM_COMPONENT]={ bRoundLuma, true, true }; |
---|
[313] | 80 | |
---|
[1029] | 81 | const UInt numValidComponent = pcYuvSrc0->getNumberValidComponents(); |
---|
| 82 | |
---|
| 83 | for(Int componentIndex=0; componentIndex<numValidComponent; componentIndex++) |
---|
[313] | 84 | { |
---|
[1029] | 85 | const ComponentID compID=ComponentID(componentIndex); |
---|
[313] | 86 | |
---|
[1029] | 87 | const Pel* pSrc0 = pcYuvSrc0->getAddr( compID, iPartUnitIdx ); |
---|
| 88 | const Pel* pSrc1 = pcYuvSrc1->getAddr( compID, iPartUnitIdx ); |
---|
| 89 | Pel* pDst = rpcYuvDst->getAddr( compID, iPartUnitIdx ); |
---|
[313] | 90 | |
---|
[1029] | 91 | // Luma : -------------------------------------------- |
---|
| 92 | const Int w0 = wp0[compID].w; |
---|
| 93 | const Int offset = wp0[compID].offset; |
---|
[1287] | 94 | const Int clipBD = bitDepths.recon[toChannelType(compID)]; |
---|
[1029] | 95 | const Int shiftNum = std::max<Int>(2, (IF_INTERNAL_PREC - clipBD)); |
---|
| 96 | const Int shift = wp0[compID].shift + shiftNum; |
---|
| 97 | const Int round = (enableRounding[compID] && (shift > 0)) ? (1<<(shift-1)) : 0; |
---|
| 98 | const Int w1 = wp1[compID].w; |
---|
| 99 | const UInt csx = pcYuvSrc0->getComponentScaleX(compID); |
---|
| 100 | const UInt csy = pcYuvSrc0->getComponentScaleY(compID); |
---|
| 101 | const Int iHeight = uiHeight>>csy; |
---|
| 102 | const Int iWidth = uiWidth>>csx; |
---|
[313] | 103 | |
---|
[1029] | 104 | const UInt iSrc0Stride = pcYuvSrc0->getStride(compID); |
---|
| 105 | const UInt iSrc1Stride = pcYuvSrc1->getStride(compID); |
---|
| 106 | const UInt iDstStride = rpcYuvDst->getStride(compID); |
---|
[313] | 107 | |
---|
[1029] | 108 | for ( Int y = iHeight-1; y >= 0; y-- ) |
---|
[313] | 109 | { |
---|
[1029] | 110 | // do it in batches of 4 (partial unroll) |
---|
| 111 | Int x = iWidth-1; |
---|
| 112 | for ( ; x >= 3; ) |
---|
| 113 | { |
---|
| 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 | pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--; |
---|
| 118 | } |
---|
| 119 | for( ; x >= 0; x-- ) |
---|
| 120 | { |
---|
| 121 | pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | pSrc0 += iSrc0Stride; |
---|
| 125 | pSrc1 += iSrc1Stride; |
---|
| 126 | pDst += iDstStride; |
---|
| 127 | } // y loop |
---|
| 128 | } // compID loop |
---|
[313] | 129 | } |
---|
| 130 | |
---|
[1029] | 131 | |
---|
[1260] | 132 | //! weighted averaging for uni-pred |
---|
[1029] | 133 | Void TComWeightPrediction::addWeightUni( const TComYuv *const pcYuvSrc0, |
---|
[1287] | 134 | const BitDepths &bitDepths, |
---|
[1029] | 135 | const UInt iPartUnitIdx, |
---|
| 136 | const UInt uiWidth, |
---|
| 137 | const UInt uiHeight, |
---|
| 138 | const WPScalingParam *const wp0, |
---|
| 139 | TComYuv *const pcYuvDst ) |
---|
[313] | 140 | { |
---|
[1029] | 141 | const UInt numValidComponent = pcYuvSrc0->getNumberValidComponents(); |
---|
[313] | 142 | |
---|
[1029] | 143 | for(Int componentIndex=0; componentIndex<numValidComponent; componentIndex++) |
---|
[313] | 144 | { |
---|
[1029] | 145 | const ComponentID compID=ComponentID(componentIndex); |
---|
[313] | 146 | |
---|
[1029] | 147 | const Pel* pSrc0 = pcYuvSrc0->getAddr( compID, iPartUnitIdx ); |
---|
| 148 | Pel* pDst = pcYuvDst->getAddr( compID, iPartUnitIdx ); |
---|
[313] | 149 | |
---|
[1029] | 150 | // Luma : -------------------------------------------- |
---|
| 151 | const Int w0 = wp0[compID].w; |
---|
| 152 | const Int offset = wp0[compID].offset; |
---|
[1287] | 153 | const Int clipBD = bitDepths.recon[toChannelType(compID)]; |
---|
[1029] | 154 | const Int shiftNum = std::max<Int>(2, (IF_INTERNAL_PREC - clipBD)); |
---|
| 155 | const Int shift = wp0[compID].shift + shiftNum; |
---|
| 156 | const Int round = (shift > 0) ? (1<<(shift-1)) : 0; |
---|
| 157 | const UInt iSrc0Stride = pcYuvSrc0->getStride(compID); |
---|
| 158 | const UInt iDstStride = pcYuvDst->getStride(compID); |
---|
| 159 | const UInt csx = pcYuvSrc0->getComponentScaleX(compID); |
---|
| 160 | const UInt csy = pcYuvSrc0->getComponentScaleY(compID); |
---|
| 161 | const Int iHeight = uiHeight>>csy; |
---|
| 162 | const Int iWidth = uiWidth>>csx; |
---|
| 163 | |
---|
| 164 | for (Int y = iHeight-1; y >= 0; y-- ) |
---|
[313] | 165 | { |
---|
[1029] | 166 | Int x = iWidth-1; |
---|
| 167 | for ( ; x >= 3; ) |
---|
| 168 | { |
---|
| 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 | pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--; |
---|
| 172 | pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--; |
---|
| 173 | } |
---|
| 174 | for( ; x >= 0; x--) |
---|
| 175 | { |
---|
| 176 | pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); |
---|
| 177 | } |
---|
| 178 | pSrc0 += iSrc0Stride; |
---|
| 179 | pDst += iDstStride; |
---|
[313] | 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
[1029] | 184 | |
---|
[313] | 185 | //======================================================= |
---|
| 186 | // getWpScaling() |
---|
| 187 | //======================================================= |
---|
[1260] | 188 | //! derivation of wp tables |
---|
[1029] | 189 | Void TComWeightPrediction::getWpScaling( TComDataCU *const pcCU, |
---|
| 190 | const Int iRefIdx0, |
---|
| 191 | const Int iRefIdx1, |
---|
| 192 | WPScalingParam *&wp0, |
---|
| 193 | WPScalingParam *&wp1) |
---|
[313] | 194 | { |
---|
| 195 | assert(iRefIdx0 >= 0 || iRefIdx1 >= 0); |
---|
| 196 | |
---|
[1029] | 197 | TComSlice *const pcSlice = pcCU->getSlice(); |
---|
| 198 | const Bool wpBiPred = pcCU->getSlice()->getPPS()->getWPBiPred(); |
---|
| 199 | const Bool bBiDir = (iRefIdx0>=0 && iRefIdx1>=0); |
---|
| 200 | const Bool bUniDir = !bBiDir; |
---|
| 201 | |
---|
[313] | 202 | if ( bUniDir || wpBiPred ) |
---|
| 203 | { // explicit -------------------- |
---|
| 204 | if ( iRefIdx0 >= 0 ) |
---|
| 205 | { |
---|
| 206 | pcSlice->getWpScaling(REF_PIC_LIST_0, iRefIdx0, wp0); |
---|
| 207 | } |
---|
| 208 | if ( iRefIdx1 >= 0 ) |
---|
| 209 | { |
---|
| 210 | pcSlice->getWpScaling(REF_PIC_LIST_1, iRefIdx1, wp1); |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | else |
---|
| 214 | { |
---|
| 215 | assert(0); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | if ( iRefIdx0 < 0 ) |
---|
| 219 | { |
---|
| 220 | wp0 = NULL; |
---|
| 221 | } |
---|
| 222 | if ( iRefIdx1 < 0 ) |
---|
| 223 | { |
---|
| 224 | wp1 = NULL; |
---|
| 225 | } |
---|
| 226 | |
---|
[1029] | 227 | const UInt numValidComponent = pcCU->getPic()->getNumberValidComponents(); |
---|
[1316] | 228 | const Bool bUseHighPrecisionPredictionWeighting = pcSlice->getSPS()->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag(); |
---|
[1029] | 229 | |
---|
[313] | 230 | if ( bBiDir ) |
---|
| 231 | { // Bi-Dir case |
---|
[1029] | 232 | for ( Int yuv=0 ; yuv<numValidComponent ; yuv++ ) |
---|
[313] | 233 | { |
---|
[1287] | 234 | #if SVC_EXTENSION |
---|
| 235 | const Int bitDepth = pcSlice->getBitDepth(toChannelType(ComponentID(yuv))); |
---|
| 236 | #else |
---|
| 237 | const Int bitDepth = pcSlice->getSPS()->getBitDepth(toChannelType(ComponentID(yuv))); |
---|
| 238 | #endif |
---|
[1029] | 239 | const Int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (bitDepth-8)); |
---|
| 240 | |
---|
[313] | 241 | wp0[yuv].w = wp0[yuv].iWeight; |
---|
| 242 | wp1[yuv].w = wp1[yuv].iWeight; |
---|
[1029] | 243 | wp0[yuv].o = wp0[yuv].iOffset * offsetScalingFactor; |
---|
| 244 | wp1[yuv].o = wp1[yuv].iOffset * offsetScalingFactor; |
---|
[313] | 245 | wp0[yuv].offset = wp0[yuv].o + wp1[yuv].o; |
---|
| 246 | wp0[yuv].shift = wp0[yuv].uiLog2WeightDenom + 1; |
---|
| 247 | wp0[yuv].round = (1 << wp0[yuv].uiLog2WeightDenom); |
---|
| 248 | wp1[yuv].offset = wp0[yuv].offset; |
---|
| 249 | wp1[yuv].shift = wp0[yuv].shift; |
---|
| 250 | wp1[yuv].round = wp0[yuv].round; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | else |
---|
| 254 | { // Unidir |
---|
[1029] | 255 | WPScalingParam *const pwp = (iRefIdx0>=0) ? wp0 : wp1 ; |
---|
| 256 | |
---|
| 257 | for ( Int yuv=0 ; yuv<numValidComponent ; yuv++ ) |
---|
[313] | 258 | { |
---|
[1287] | 259 | #if SVC_EXTENSION |
---|
| 260 | const Int bitDepth = pcSlice->getBitDepth(toChannelType(ComponentID(yuv))); |
---|
| 261 | #else |
---|
| 262 | const Int bitDepth = pcSlice->getSPS()->getBitDepth(toChannelType(ComponentID(yuv))); |
---|
| 263 | #endif |
---|
[1029] | 264 | const Int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (bitDepth-8)); |
---|
| 265 | |
---|
[313] | 266 | pwp[yuv].w = pwp[yuv].iWeight; |
---|
[1029] | 267 | pwp[yuv].offset = pwp[yuv].iOffset * offsetScalingFactor; |
---|
[313] | 268 | pwp[yuv].shift = pwp[yuv].uiLog2WeightDenom; |
---|
| 269 | pwp[yuv].round = (pwp[yuv].uiLog2WeightDenom>=1) ? (1 << (pwp[yuv].uiLog2WeightDenom-1)) : (0); |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
[1029] | 274 | |
---|
[1260] | 275 | //! weighted prediction for bi-pred |
---|
[1029] | 276 | Void TComWeightPrediction::xWeightedPredictionBi( TComDataCU *const pcCU, |
---|
| 277 | const TComYuv *const pcYuvSrc0, |
---|
| 278 | const TComYuv *const pcYuvSrc1, |
---|
| 279 | const Int iRefIdx0, |
---|
| 280 | const Int iRefIdx1, |
---|
| 281 | const UInt uiPartIdx, |
---|
| 282 | const Int iWidth, |
---|
| 283 | const Int iHeight, |
---|
| 284 | TComYuv *rpcYuvDst ) |
---|
[313] | 285 | { |
---|
[1029] | 286 | WPScalingParam *pwp0; |
---|
| 287 | WPScalingParam *pwp1; |
---|
[313] | 288 | |
---|
[1029] | 289 | assert(pcCU->getSlice()->getPPS()->getWPBiPred()); |
---|
| 290 | |
---|
[313] | 291 | getWpScaling(pcCU, iRefIdx0, iRefIdx1, pwp0, pwp1); |
---|
| 292 | |
---|
| 293 | if( iRefIdx0 >= 0 && iRefIdx1 >= 0 ) |
---|
| 294 | { |
---|
[1287] | 295 | #if SVC_EXTENSION |
---|
| 296 | addWeightBi(pcYuvSrc0, pcYuvSrc1, pcCU->getSlice()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp0, pwp1, rpcYuvDst ); |
---|
| 297 | #else |
---|
| 298 | addWeightBi(pcYuvSrc0, pcYuvSrc1, pcCU->getSlice()->getSPS()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp0, pwp1, rpcYuvDst ); |
---|
| 299 | #endif |
---|
[313] | 300 | } |
---|
| 301 | else if ( iRefIdx0 >= 0 && iRefIdx1 < 0 ) |
---|
| 302 | { |
---|
[1287] | 303 | #if SVC_EXTENSION |
---|
| 304 | addWeightUni( pcYuvSrc0, pcCU->getSlice()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp0, rpcYuvDst ); |
---|
| 305 | #else |
---|
| 306 | addWeightUni( pcYuvSrc0, pcCU->getSlice()->getSPS()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp0, rpcYuvDst ); |
---|
| 307 | #endif |
---|
[313] | 308 | } |
---|
| 309 | else if ( iRefIdx0 < 0 && iRefIdx1 >= 0 ) |
---|
| 310 | { |
---|
[1287] | 311 | #if SVC_EXTENSION |
---|
| 312 | addWeightUni( pcYuvSrc1, pcCU->getSlice()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp1, rpcYuvDst ); |
---|
| 313 | #else |
---|
| 314 | addWeightUni( pcYuvSrc1, pcCU->getSlice()->getSPS()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp1, rpcYuvDst ); |
---|
| 315 | #endif |
---|
[313] | 316 | } |
---|
| 317 | else |
---|
| 318 | { |
---|
| 319 | assert (0); |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
[1029] | 323 | |
---|
[1260] | 324 | //! weighted prediction for uni-pred |
---|
[1029] | 325 | Void TComWeightPrediction::xWeightedPredictionUni( TComDataCU *const pcCU, |
---|
| 326 | const TComYuv *const pcYuvSrc, |
---|
| 327 | const UInt uiPartAddr, |
---|
| 328 | const Int iWidth, |
---|
| 329 | const Int iHeight, |
---|
| 330 | const RefPicList eRefPicList, |
---|
| 331 | TComYuv *pcYuvPred, |
---|
| 332 | const Int iRefIdx_input) |
---|
| 333 | { |
---|
| 334 | WPScalingParam *pwp, *pwpTmp; |
---|
| 335 | |
---|
| 336 | Int iRefIdx=iRefIdx_input; |
---|
[313] | 337 | if ( iRefIdx < 0 ) |
---|
| 338 | { |
---|
| 339 | iRefIdx = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr ); |
---|
| 340 | } |
---|
| 341 | assert (iRefIdx >= 0); |
---|
| 342 | |
---|
| 343 | if ( eRefPicList == REF_PIC_LIST_0 ) |
---|
| 344 | { |
---|
| 345 | getWpScaling(pcCU, iRefIdx, -1, pwp, pwpTmp); |
---|
| 346 | } |
---|
| 347 | else |
---|
| 348 | { |
---|
| 349 | getWpScaling(pcCU, -1, iRefIdx, pwpTmp, pwp); |
---|
| 350 | } |
---|
[1287] | 351 | #if SVC_EXTENSION |
---|
| 352 | addWeightUni( pcYuvSrc, pcCU->getSlice()->getBitDepths(), uiPartAddr, iWidth, iHeight, pwp, pcYuvPred ); |
---|
| 353 | #else |
---|
| 354 | addWeightUni( pcYuvSrc, pcCU->getSlice()->getSPS()->getBitDepths(), uiPartAddr, iWidth, iHeight, pwp, pcYuvPred ); |
---|
| 355 | #endif |
---|
[313] | 356 | } |
---|