1 /*****************************************************************************
2  * Copyright (C) 2013-2020 MulticoreWare, Inc
3  *
4  * Authors: Steve Borho <steve@borho.org>
5  *          Min Chen <chenm003@163.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
20  *
21  * This program is also available under a commercial proprietary license.
22  * For more information, contact us at license @ x265.com.
23  *****************************************************************************/
24 
25 #ifndef X265_MOTIONESTIMATE_H
26 #define X265_MOTIONESTIMATE_H
27 
28 #include "primitives.h"
29 #include "reference.h"
30 #include "mv.h"
31 #include "bitcost.h"
32 #include "yuv.h"
33 
34 namespace X265_NS {
35 // private x265 namespace
36 
37 class MotionEstimate : public BitCost
38 {
39 protected:
40 
41     intptr_t blockOffset;
42 
43     int ctuAddr;
44     int absPartIdx;  // part index of PU, including CU offset within CTU
45 
46     int searchMethod;
47     int searchMethodL0;
48     int searchMethodL1;
49     int subpelRefine;
50 
51     int blockwidth;
52     int blockheight;
53 
54     pixelcmp_t sad;
55     pixelcmp_x3_t sad_x3;
56     pixelcmp_x4_t sad_x4;
57     pixelcmp_ads_t ads;
58     pixelcmp_t satd;
59     pixelcmp_t chromaSatd;
60 
61     MotionEstimate& operator =(const MotionEstimate&);
62 
63 public:
64 
65     static const int COST_MAX = 1 << 28;
66 
67     uint32_t* integral[INTEGRAL_PLANE_NUM];
68     Yuv fencPUYuv;
69     int partEnum;
70     bool bChromaSATD;
71 
72     MotionEstimate();
73     ~MotionEstimate();
74 
75     static void initScales();
76     static int hpelIterationCount(int subme);
77     void init(int csp);
78 
79     /* Methods called at slice setup */
80 
81     void setSourcePU(pixel *fencY, intptr_t stride, intptr_t offset, int pwidth, int pheight, const int searchMethod, const int searchL0, const int searchL1, const int subpelRefine);
82     void setSourcePU(const Yuv& srcFencYuv, int ctuAddr, int cuPartIdx, int puPartIdx, int pwidth, int pheight, const int searchMethod, const int subpelRefine, bool bChroma);
83 
84     /* buf*() and motionEstimate() methods all use cached fenc pixels and thus
85      * require setSourcePU() to be called prior. */
86 
bufSAD(const pixel * fref,intptr_t stride)87     inline int bufSAD(const pixel* fref, intptr_t stride)  { return sad(fencPUYuv.m_buf[0], FENC_STRIDE, fref, stride); }
88 
bufSATD(const pixel * fref,intptr_t stride)89     inline int bufSATD(const pixel* fref, intptr_t stride) { return satd(fencPUYuv.m_buf[0], FENC_STRIDE, fref, stride); }
90 
bufChromaSATD(const Yuv & refYuv,int puPartIdx)91     inline int bufChromaSATD(const Yuv& refYuv, int puPartIdx)
92     {
93         return chromaSatd(refYuv.getCbAddr(puPartIdx), refYuv.m_csize, fencPUYuv.m_buf[1], fencPUYuv.m_csize) +
94                chromaSatd(refYuv.getCrAddr(puPartIdx), refYuv.m_csize, fencPUYuv.m_buf[2], fencPUYuv.m_csize);
95     }
96 
97     void refineMV(ReferencePlanes* ref, const MV& mvmin, const MV& mvmax, const MV& qmvp, MV& outQMv);
98     int motionEstimate(ReferencePlanes* ref, const MV & mvmin, const MV & mvmax, const MV & qmvp, int numCandidates, const MV * mvc, int merange, MV & outQMv, uint32_t maxSlices, pixel *srcReferencePlane = 0);
99 
100     int subpelCompare(ReferencePlanes* ref, const MV &qmv, pixelcmp_t);
101 
102 protected:
103 
104     inline void StarPatternSearch(ReferencePlanes *ref,
105                                   const MV &       mvmin,
106                                   const MV &       mvmax,
107                                   MV &             bmv,
108                                   int &            bcost,
109                                   int &            bPointNr,
110                                   int &            bDistance,
111                                   int              earlyExitIters,
112                                   int              merange,
113                                   int              hme);
114 };
115 }
116 
117 #endif // ifndef X265_MOTIONESTIMATE_H
118