1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
3 *
4 * Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23 
24 #ifndef X265_PREDICT_H
25 #define X265_PREDICT_H
26 
27 #include "common.h"
28 #include "frame.h"
29 #include "quant.h"
30 #include "shortyuv.h"
31 #include "yuv.h"
32 
33 namespace X265_NS {
34 
35 class CUData;
36 class Slice;
37 struct CUGeom;
38 
39 struct PredictionUnit
40 {
41     uint32_t     ctuAddr;      // raster index of current CTU within its picture
42     uint32_t     cuAbsPartIdx; // z-order offset of current CU within its CTU
43     uint32_t     puAbsPartIdx; // z-order offset of current PU with its CU
44     int          width;
45     int          height;
46 
47     PredictionUnit(const CUData& cu, const CUGeom& cuGeom, int puIdx);
48 };
49 
50 class Predict
51 {
52 public:
53 
54     enum { ADI_BUF_STRIDE = (2 * MAX_CU_SIZE + 1 + 15) }; // alignment to 16 bytes
55 
56     /* Weighted prediction scaling values built from slice parameters (bitdepth scaled) */
57     struct WeightValues
58     {
59         int w, o, offset, shift, round;
60     };
61 
62     struct IntraNeighbors
63     {
64         int      numIntraNeighbor;
65         int      totalUnits;
66         int      aboveUnits;
67         int      leftUnits;
68         int      unitWidth;
69         int      unitHeight;
70         int      log2TrSize;
71         bool     bNeighborFlags[4 * MAX_NUM_SPU_W + 1];
72     };
73 
74     ShortYuv  m_predShortYuv[2]; /* temporary storage for weighted prediction */
75     int16_t*  m_immedVals;
76 
77     // Unfiltered/filtered neighbours of the current partition.
78     pixel     intraNeighbourBuf[2][258];
79 
80     /* Slice information */
81     int       m_csp;
82     int       m_hChromaShift;
83     int       m_vChromaShift;
84 
85     Predict();
86     ~Predict();
87 
88     bool allocBuffers(int csp);
89 
90     // motion compensation functions
91     void predInterLumaPixel(const PredictionUnit& pu, Yuv& dstYuv, const PicYuv& refPic, const MV& mv) const;
92     void predInterChromaPixel(const PredictionUnit& pu, Yuv& dstYuv, const PicYuv& refPic, const MV& mv) const;
93 
94     void predInterLumaShort(const PredictionUnit& pu, ShortYuv& dstSYuv, const PicYuv& refPic, const MV& mv) const;
95     void predInterChromaShort(const PredictionUnit& pu, ShortYuv& dstSYuv, const PicYuv& refPic, const MV& mv) const;
96 
97     void addWeightBi(const PredictionUnit& pu, Yuv& predYuv, const ShortYuv& srcYuv0, const ShortYuv& srcYuv1, const WeightValues wp0[3], const WeightValues wp1[3], bool bLuma, bool bChroma) const;
98     void addWeightUni(const PredictionUnit& pu, Yuv& predYuv, const ShortYuv& srcYuv, const WeightValues wp[3], bool bLuma, bool bChroma) const;
99 
100     void motionCompensation(const CUData& cu, const PredictionUnit& pu, Yuv& predYuv, bool bLuma, bool bChroma);
101 
102     /* Angular Intra */
103     void predIntraLumaAng(uint32_t dirMode, pixel* pred, intptr_t stride, uint32_t log2TrSize);
104     void predIntraChromaAng(uint32_t dirMode, pixel* pred, intptr_t stride, uint32_t log2TrSizeC);
105     void initAdiPattern(const CUData& cu, const CUGeom& cuGeom, uint32_t puAbsPartIdx, const IntraNeighbors& intraNeighbors, int dirMode);
106     void initAdiPatternChroma(const CUData& cu, const CUGeom& cuGeom, uint32_t puAbsPartIdx, const IntraNeighbors& intraNeighbors, uint32_t chromaId);
107 
108     /* Intra prediction helper functions */
109     static void initIntraNeighbors(const CUData& cu, uint32_t absPartIdx, uint32_t tuDepth, bool isLuma, IntraNeighbors *IntraNeighbors);
110     static void fillReferenceSamples(const pixel* adiOrigin, intptr_t picStride, const IntraNeighbors& intraNeighbors, pixel dst[258]);
111     template<bool cip>
112     static bool isAboveLeftAvailable(const CUData& cu, uint32_t partIdxLT);
113     template<bool cip>
114     static int  isAboveAvailable(const CUData& cu, uint32_t partIdxLT, uint32_t partIdxRT, bool* bValidFlags);
115     template<bool cip>
116     static int  isLeftAvailable(const CUData& cu, uint32_t partIdxLT, uint32_t partIdxLB, bool* bValidFlags);
117     template<bool cip>
118     static int  isAboveRightAvailable(const CUData& cu, uint32_t partIdxRT, bool* bValidFlags, uint32_t numUnits);
119     template<bool cip>
120     static int  isBelowLeftAvailable(const CUData& cu, uint32_t partIdxLB, bool* bValidFlags, uint32_t numUnits);
121 };
122 }
123 
124 #endif // ifndef X265_PREDICT_H
125