<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Could you please send this as a patch file as well?</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks, </div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Kirithika</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> x265-devel <x265-devel-bounces@videolan.org> on behalf of Neil <neil11314@gmail.com><br>
<b>Sent:</b> Wednesday, May 6, 2026 9:17 PM<br>
<b>To:</b> x265-devel@videolan.org <x265-devel@videolan.org><br>
<b>Subject:</b> [x265] [PATCH 1/1] TME: fix threaded-me crashes from stale motion data</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">[Some people who received this message don't often get email from neil11314@gmail.com. Learn why this is important at
<a href="https://aka.ms/LearnAboutSenderIdentification">https://aka.ms/LearnAboutSenderIdentification</a> ]<br>
<br>
From: neil1123 <neil1123@users.noreply.github.com><br>
<br>
Threaded ME can leave stale or invalid data in Slice::m_ctuMV. When the<br>
main encoder later consumes that data, invalid ref indices or stale motion<br>
vectors can make predInterSearch() skip the regular ME path and use bad<br>
motion data instead.<br>
<br>
Fix this by clearing m_ctuMV when FrameData is reused and initializing both<br>
refs to REF_NOT_VALID. Validate the computed PU table index and L0/L1 ref<br>
indices before consuming a threaded-ME slot; if the slot is missing, stale,<br>
out of range, or does not describe a valid uni- or bi-pred result, fall<br>
back to the regular ME path.<br>
<br>
Also resize g_puStartIdx from [128][8] to<br>
[2 * MAX_CU_SIZE + 1][NUM_PART_SIZES]. g_puLookup includes a 64x64 PU, so<br>
width + height can be 128; the old table only had valid row indices<br>
0..127. Tying the second dimension to NUM_PART_SIZES also avoids another<br>
hard-coded partition-count assumption.<br>
<br>
Finally, move setSourcePU() before selectMVP() in puMotionEstimation().<br>
selectMVP() calls m_me.bufSAD(), which reads the cached source PU prepared<br>
by setSourcePU(); calling setSourcePU() afterwards allowed MVP selection to<br>
use stale source pixels from a previous PU.<br>
<br>
Validation on Windows 11 / MSYS2 CLANG64 / clang 22.1.4:<br>
- applies cleanly to x265 master 7b3d1f515318f73056abd9e99944e9f79db090bd with git am<br>
- git diff --check passes<br>
- checked/noasm CLI build passes<br>
- 16-frame Threaded ME smoke passes with --threaded-me --pools 32 --frame-threads 1 --no-wpp<br>
---<br>
source/common/framedata.cpp | 10 +++<br>
source/encoder/search.cpp | 145 ++++++++++++++++++----------------<br>
source/encoder/threadedme.cpp | 2 +-<br>
source/encoder/threadedme.h | 2 +-<br>
4 files changed, 91 insertions(+), 68 deletions(-)<br>
<br>
diff --git a/source/common/framedata.cpp b/source/common/framedata.cpp<br>
index aa317dfe8..611f40f9d 100644<br>
--- a/source/common/framedata.cpp<br>
+++ b/source/common/framedata.cpp<br>
@@ -90,6 +90,16 @@ void FrameData::reinit(const SPS& sps)<br>
{<br>
memset(m_cuStat, 0, sps.numCUsInFrame * sizeof(*m_cuStat));<br>
memset(m_rowStat, 0, sps.numCuInHeight * sizeof(*m_rowStat));<br>
+ if (m_param->bThreadedME)<br>
+ {<br>
+ uint32_t totalPUs = sps.numCuInWidth * sps.numCuInHeight * MAX_NUM_PUS_PER_CTU;<br>
+ memset(m_slice->m_ctuMV, 0, totalPUs * sizeof(*m_slice->m_ctuMV));<br>
+ for (uint32_t i = 0; i < totalPUs; i++)<br>
+ {<br>
+ m_slice->m_ctuMV[i].ref[0] = REF_NOT_VALID;<br>
+ m_slice->m_ctuMV[i].ref[1] = REF_NOT_VALID;<br>
+ }<br>
+ }<br>
if (m_param->bDynamicRefine)<br>
{<br>
memset(m_picCTU->m_collectCURd, 0, MAX_NUM_DYN_REFINE * sps.numCUsInFrame * sizeof(uint64_t));<br>
diff --git a/source/encoder/search.cpp b/source/encoder/search.cpp<br>
index 2fcbbd411..e922097ad 100644<br>
--- a/source/encoder/search.cpp<br>
+++ b/source/encoder/search.cpp<br>
@@ -278,6 +278,11 @@ void Search::puMotionEstimation(const Slice* slice, const CUGeom& cuGeom, CUData<br>
int mvpIdx = 0;<br>
<br>
bool bLowresMVP = false;<br>
+ PicYuv* recon = slice->m_mref[list][ref].reconPic;<br>
+ int offset = recon->getLumaAddr(cu.m_cuAddr, pu.cuAbsPartIdx + pu.puAbsPartIdx) - recon->getLumaAddr(0);<br>
+<br>
+ m_me.setSourcePU(fencPic->m_picOrg[0], fencPic->m_stride, offset, pu.width, pu.height, m_param->searchMethod, m_param->subpelRefine);<br>
+<br>
if (!isMVP)<br>
{<br>
for(int dir = MD_LEFT; dir <= MD_ABOVE_LEFT ; dir++)<br>
@@ -345,16 +350,12 @@ void Search::puMotionEstimation(const Slice* slice, const CUGeom& cuGeom, CUData<br>
}<br>
}<br>
<br>
- PicYuv* recon = slice->m_mref[list][ref].reconPic;<br>
- int offset = recon->getLumaAddr(cu.m_cuAddr, pu.cuAbsPartIdx + pu.puAbsPartIdx) - recon->getLumaAddr(0);<br>
-<br>
if (m_param->searchMethod == X265_SEA)<br>
{<br>
for (int planes = 0; planes < INTEGRAL_PLANE_NUM; planes++)<br>
m_me.integral[planes] = slice->m_refFrameList[list][ref]->m_encData->m_meIntegral[planes] + offset;<br>
}<br>
<br>
- m_me.setSourcePU(fencPic->m_picOrg[0], fencPic->m_stride, offset, pu.width, pu.height, m_param->searchMethod, m_param->subpelRefine);<br>
setSearchRange(cu, mvp, searchRange, mvmin, mvmax);<br>
<br>
if (isMVP)<br>
@@ -2600,8 +2601,59 @@ void Search::predInterSearch(Mode& interMode, const CUGeom& cuGeom, bool bChroma<br>
<br>
getBlkBits((PartSize)cu.m_partSize[0], slice->isInterP(), puIdx, lastMode, m_listSelBits);<br>
bool bDoUnidir = true;<br>
+ bool useThreadedME = false;<br>
+ bool threadedBidir = false;<br>
+ bool threadedUniL0 = false;<br>
+ bool threadedUniL1 = false;<br>
+ MEData threadedMEData;<br>
<br>
cu.getNeighbourMV(puIdx, pu.puAbsPartIdx, interMode.interNeighbours);<br>
+ if (m_param->bThreadedME)<br>
+ {<br>
+ int cuSize = 1 << cu.m_log2CUSize[0];<br>
+ int lookupWidth = pu.width;<br>
+ int lookupHeight = pu.height;<br>
+ bool isAmp = cu.m_partSize[0] >= SIZE_2NxnU;<br>
+<br>
+ if (isAmp)<br>
+ {<br>
+ if (cu.m_partSize[0] == SIZE_2NxnU || cu.m_partSize[0] == SIZE_2NxnD)<br>
+ lookupHeight = puIdx ? (pu.width - pu.height) : pu.height;<br>
+ else<br>
+ lookupWidth = puIdx ? (pu.height - pu.width) : pu.width;<br>
+ }<br>
+<br>
+ if (lookupWidth + lookupHeight <= 2 * MAX_CU_SIZE)<br>
+ {<br>
+ int startIdx = g_puStartIdx[lookupWidth + lookupHeight][static_cast<int>(cu.m_partSize[0])];<br>
+ int alignWidth = isAmp ? cuSize : pu.width;<br>
+ int alignHeight = isAmp ? cuSize : pu.height;<br>
+ int numPUX = m_param->maxCUSize / alignWidth;<br>
+ int numPUY = m_param->maxCUSize / alignHeight;<br>
+ int puOffset = isAmp ? (puIdx * numPUX * numPUY) : (cu.m_partSize[0] == SIZE_2NxN ? (puIdx * numPUX) : puIdx);<br>
+ int relX = (cu.m_cuPelX / alignWidth) % numPUX;<br>
+ int relY = (cu.m_cuPelY / alignHeight) % numPUY;<br>
+ int index = startIdx + (relY * numPUX + relX) + puOffset;<br>
+<br>
+ if (index >= 0 && index < MAX_NUM_PUS_PER_CTU)<br>
+ {<br>
+ int row = cu.m_cuAddr / m_slice->m_sps->numCuInWidth;<br>
+ int col = cu.m_cuAddr % m_slice->m_sps->numCuInWidth;<br>
+ int slotIdx = (col % m_slice->m_sps->numCuInWidth) * m_slice->m_sps->numCuInHeight + row;<br>
+<br>
+ threadedMEData = slice->m_ctuMV[slotIdx * MAX_NUM_PUS_PER_CTU + index];<br>
+<br>
+ bool validL0 = threadedMEData.ref[0] >= 0 && threadedMEData.ref[0] < numRefIdx[0];<br>
+ bool validL1 = numPredDir > 1 && threadedMEData.ref[1] >= 0 && threadedMEData.ref[1] < numRefIdx[1];<br>
+<br>
+ threadedBidir = validL0 && validL1;<br>
+ threadedUniL0 = validL0 && threadedMEData.ref[1] == REF_NOT_VALID;<br>
+ threadedUniL1 = validL1 && threadedMEData.ref[0] == REF_NOT_VALID;<br>
+ useThreadedME = threadedBidir || threadedUniL0 || threadedUniL1;<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
/* Uni-directional prediction */<br>
if ((m_param->analysisLoadReuseLevel > 1 && m_param->analysisLoadReuseLevel != 10)<br>
|| (m_param->analysisMultiPassRefine && m_param->rc.bStatRead) || (m_param->bAnalysisType == AVC_INFO) || (useAsMVP))<br>
@@ -2773,7 +2825,7 @@ void Search::predInterSearch(Mode& interMode, const CUGeom& cuGeom, bool bChroma<br>
/* if no peer threads were bonded, fall back to doing unidirectional<br>
* searches ourselves without overhead of singleMotionEstimation() */<br>
}<br>
- if (bDoUnidir && !m_param->bThreadedME)<br>
+ if (bDoUnidir && (!m_param->bThreadedME || !useThreadedME))<br>
{<br>
interMode.bestME[puIdx][0].ref = interMode.bestME[puIdx][1].ref = -1;<br>
uint32_t refMask = refMasks[puIdx] ? refMasks[puIdx] : (uint32_t)-1;<br>
@@ -2885,7 +2937,7 @@ void Search::predInterSearch(Mode& interMode, const CUGeom& cuGeom, bool bChroma<br>
<br>
if (slice->isInterB() && !cu.isBipredRestriction() && /* biprediction is possible for this PU */<br>
cu.m_partSize[pu.puAbsPartIdx] != SIZE_2Nx2N && /* 2Nx2N biprediction is handled elsewhere */<br>
- bestME[0].cost != MAX_UINT && bestME[1].cost != MAX_UINT && !m_param->bThreadedME)<br>
+ bestME[0].cost != MAX_UINT && bestME[1].cost != MAX_UINT && (!m_param->bThreadedME || !useThreadedME))<br>
{<br>
bidir[0] = bestME[0];<br>
bidir[1] = bestME[1];<br>
@@ -2991,65 +3043,28 @@ void Search::predInterSearch(Mode& interMode, const CUGeom& cuGeom, bool bChroma<br>
bool uniL0 = false;<br>
bool uniL1 = false;<br>
<br>
- if (m_param->bThreadedME)<br>
+ if (useThreadedME)<br>
{<br>
- int cuSize = 1 << cu.m_log2CUSize[0];<br>
-<br>
- int lookupWidth = pu.width;<br>
- int lookupHeight = pu.height;<br>
+ bestME[0].ref = threadedMEData.ref[0];<br>
+ bestME[1].ref = threadedMEData.ref[1];<br>
<br>
- bool isAmp = cu.m_partSize[0] >= SIZE_2NxnU;<br>
+ isBidir = threadedBidir;<br>
+ uniL0 = threadedUniL0;<br>
+ uniL1 = threadedUniL1;<br>
<br>
- if (isAmp)<br>
- {<br>
- if (cu.m_partSize[0] == SIZE_2NxnU || cu.m_partSize[0] == SIZE_2NxnD)<br>
- lookupHeight = (puIdx) ? (pu.width - pu.height) : pu.height;<br>
- else<br>
- lookupWidth = (puIdx) ? (pu.height - pu.width) : pu.width;<br>
- }<br>
-<br>
- int startIdx = g_puStartIdx[lookupWidth + lookupHeight][static_cast<int>(cu.m_partSize[0])];<br>
-<br>
- int alignWidth = isAmp ? cuSize : pu.width;<br>
- int alignHeight = isAmp ? cuSize : pu.height;<br>
-<br>
- int numPUX = m_param->maxCUSize / alignWidth;<br>
- int numPUY = m_param->maxCUSize / alignHeight;<br>
-<br>
- int puOffset = isAmp ? (puIdx * numPUX * numPUY) : (cu.m_partSize[0] == SIZE_2NxN ? (puIdx * numPUX) : puIdx);<br>
-<br>
- int relX = (cu.m_cuPelX / alignWidth) % numPUX;<br>
- int relY = (cu.m_cuPelY / alignHeight) % numPUY;<br>
-<br>
- int index = startIdx + (relY * numPUX + relX) + puOffset;<br>
-<br>
- int row = cu.m_cuAddr / m_slice->m_sps->numCuInWidth;<br>
- int col = cu.m_cuAddr % m_slice->m_sps->numCuInWidth;<br>
-<br>
- int slotIdx = (col % m_slice->m_sps->numCuInWidth) * m_slice->m_sps->numCuInHeight + row;<br>
-<br>
- MEData meData = slice->m_ctuMV[slotIdx * MAX_NUM_PUS_PER_CTU + index];<br>
-<br>
- bestME[0].ref = meData.ref[0];<br>
- bestME[1].ref = meData.ref[1];<br>
-<br>
- isBidir = (bestME[0].ref >= 0 && bestME[1].ref >= 0);<br>
- uniL0 = (bestME[0].ref >= 0 && bestME[1].ref == REF_NOT_VALID);<br>
- uniL1 = (bestME[1].ref >= 0 && bestME[0].ref == REF_NOT_VALID);<br>
-<br>
- if(isBidir)<br>
+ if (isBidir)<br>
{<br>
cu.getPMV(interMode.interNeighbours, 0, bestME[0].ref, interMode.amvpCand[0][bestME[0].ref], mvc);<br>
cu.getPMV(interMode.interNeighbours, 1, bestME[1].ref, interMode.amvpCand[1][bestME[1].ref], mvc);<br>
<br>
- bidir[0].mv = meData.mv[0];<br>
- bidir[1].mv = meData.mv[1];<br>
+ bidir[0].mv = threadedMEData.mv[0];<br>
+ bidir[1].mv = threadedMEData.mv[1];<br>
bidir[0].mvp = interMode.amvpCand[0][bestME[0].ref][0];<br>
bidir[1].mvp = interMode.amvpCand[1][bestME[1].ref][0];<br>
- bidir[0].mvCost = meData.mvCost[0];<br>
- bidir[1].mvCost = meData.mvCost[1];<br>
- bidirCost = meData.cost;<br>
- bidirBits = meData.bits;<br>
+ bidir[0].mvCost = threadedMEData.mvCost[0];<br>
+ bidir[1].mvCost = threadedMEData.mvCost[1];<br>
+ bidirCost = threadedMEData.cost;<br>
+ bidirBits = threadedMEData.bits;<br>
<br>
bestCost = bidirCost;<br>
}<br>
@@ -3057,11 +3072,11 @@ void Search::predInterSearch(Mode& interMode, const CUGeom& cuGeom, bool bChroma<br>
{<br>
cu.getPMV(interMode.interNeighbours, 0, bestME[0].ref, interMode.amvpCand[0][bestME[0].ref], mvc);<br>
<br>
- bestME[0].mv = meData.mv[0];<br>
+ bestME[0].mv = threadedMEData.mv[0];<br>
bestME[0].mvp = interMode.amvpCand[0][bestME[0].ref][0];<br>
- bestME[0].mvCost = meData.mvCost[0];<br>
- bestME[0].cost = meData.cost;<br>
- bestME[0].bits = meData.bits;<br>
+ bestME[0].mvCost = threadedMEData.mvCost[0];<br>
+ bestME[0].cost = threadedMEData.cost;<br>
+ bestME[0].bits = threadedMEData.bits;<br>
<br>
bestCost = bestME[0].cost;<br>
}<br>
@@ -3069,16 +3084,14 @@ void Search::predInterSearch(Mode& interMode, const CUGeom& cuGeom, bool bChroma<br>
{<br>
cu.getPMV(interMode.interNeighbours, 1, bestME[1].ref, interMode.amvpCand[1][bestME[1].ref], mvc);<br>
<br>
- bestME[1].mv = meData.mv[1];<br>
+ bestME[1].mv = threadedMEData.mv[1];<br>
bestME[1].mvp = interMode.amvpCand[1][bestME[1].ref][0];<br>
- bestME[1].mvCost = meData.mvCost[1];<br>
- bestME[1].cost = meData.cost;<br>
- bestME[1].bits = meData.bits;<br>
+ bestME[1].mvCost = threadedMEData.mvCost[1];<br>
+ bestME[1].cost = threadedMEData.cost;<br>
+ bestME[1].bits = threadedMEData.bits;<br>
<br>
bestCost = bestME[1].cost;<br>
}<br>
- else<br>
- x265_log(NULL, X265_LOG_ERROR, "Invalid ME mode");<br>
<br>
if (mrgCost < bestCost)<br>
isMerge = true;<br>
diff --git a/source/encoder/threadedme.cpp b/source/encoder/threadedme.cpp<br>
index 1028beb92..197f471de 100644<br>
--- a/source/encoder/threadedme.cpp<br>
+++ b/source/encoder/threadedme.cpp<br>
@@ -30,7 +30,7 @@<br>
#include <sstream><br>
<br>
namespace X265_NS {<br>
-int g_puStartIdx[128][8] = {0};<br>
+int g_puStartIdx[2 * MAX_CU_SIZE + 1][NUM_PART_SIZES] = {{0}};<br>
<br>
bool ThreadedME::create()<br>
{<br>
diff --git a/source/encoder/threadedme.h b/source/encoder/threadedme.h<br>
index 5e5fc4878..1adb545f5 100644<br>
--- a/source/encoder/threadedme.h<br>
+++ b/source/encoder/threadedme.h<br>
@@ -40,7 +40,7 @@<br>
<br>
namespace X265_NS {<br>
<br>
-extern int g_puStartIdx[128][8];<br>
+extern int g_puStartIdx[2 * MAX_CU_SIZE + 1][NUM_PART_SIZES];<br>
<br>
class Encoder;<br>
class Analysis;<br>
--<br>
2.54.0.windows.1<br>
<br>
_______________________________________________<br>
x265-devel mailing list<br>
x265-devel@videolan.org<br>
<a href="https://mailman.videolan.org/listinfo/x265-devel">https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmailman.videolan.org%2Flistinfo%2Fx265-devel&data=05%7C02%7Ckirithika%40multicorewareinc.com%7C4125244062c5451956c708deab86df8f%7Cffc5e88b3fa24d69a468344b6b766e7d%7C0%7C0%7C639136792994180708%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=CGwmnekFyEjZ8t1JsTaPNdPtWvV38hJvRypc9Lt7OvE%3D&reserved=0</a><br>
⚠️ External Email Warning: This message originated from outside the organization (McW).<br>
Be cautious - Never share passwords or click unexpected links/attachments.<br>
<br>
</div>
</span></font></div>
</body>
</html>