+}
+
+MotionEstimate::MotionEstimate()
+{
+ ctuAddr = -1;
+ absPartIdx = -1;
+ searchMethod = X265_HEX_SEARCH;
+ subpelRefine = 2;
+ bChromaSATD = false;
+ chromaSatd = NULL;
+}
+
+void MotionEstimate::init(int method, int refine, int csp)
+{
+ if (!sizeScale[0])
+ initScales();
+
+ searchMethod = method;
+ subpelRefine = refine;
+ fencPUYuv.create(FENC_STRIDE, csp);
+}
+
+MotionEstimate::~MotionEstimate()
+{
+ fencPUYuv.destroy();
+}
+
+/* Called by lookahead, luma only, no use of PicYuv */
+void MotionEstimate::setSourcePU(pixel *fencY, intptr_t stride, intptr_t offset, int pwidth, int pheight)
+{
+ partEnum = partitionFromSizes(pwidth, pheight);
+ X265_CHECK(LUMA_4x4 != partEnum, "4x4 inter partition detected!\n");
+ sad = primitives.sad[partEnum];
+ satd = primitives.satd[partEnum];
+ sad_x3 = primitives.sad_x3[partEnum];
+ sad_x4 = primitives.sad_x4[partEnum];
+
+ blockwidth = pwidth;
+ blockOffset = offset;
+ absPartIdx = ctuAddr = -1;
+
+ /* copy PU block into cache */
+ primitives.luma_copy_pp[partEnum](fencPUYuv.m_buf[0], FENC_STRIDE, fencY + offset, stride);
+ X265_CHECK(!bChromaSATD, "chroma distortion measurements impossible in this code path\n");
+}
+
+/* Called by Search::predInterSearch() or --pme equivalent, chroma residual might be considered */
+void MotionEstimate::setSourcePU(const Yuv& srcFencYuv, int _ctuAddr, int cuPartIdx, int puPartIdx, int pwidth, int pheight)
+{
+ partEnum = partitionFromSizes(pwidth, pheight);
+ X265_CHECK(LUMA_4x4 != partEnum, "4x4 inter partition detected!\n");
+ sad = primitives.sad[partEnum];
+ satd = primitives.satd[partEnum];
+ sad_x3 = primitives.sad_x3[partEnum];
+ sad_x4 = primitives.sad_x4[partEnum];
+ chromaSatd = primitives.chroma[fencPUYuv.m_csp].satd[partEnum];
+
+ /* Enable chroma residual cost if subpelRefine level is greater than 2 and chroma block size
+ * is an even multiple of 4x4 pixels (indicated by non-null chromaSatd pointer) */
+ bChromaSATD = subpelRefine > 2 && chromaSatd;
+ X265_CHECK(!(bChromaSATD && !workload[subpelRefine].hpel_satd), "Chroma SATD cannot be used with SAD hpel\n");
+
+ ctuAddr = _ctuAddr;
+ absPartIdx = cuPartIdx + puPartIdx;
+ blockwidth = pwidth;
+ blockOffset = 0;
+
+ /* copy PU from CU Yuv */
+ fencPUYuv.copyPUFromYuv(srcFencYuv, puPartIdx, partEnum, bChromaSATD);
+}
+