<div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">From de4dc0bc67d648c8efd7fecab8d378d44317c5f0 Mon Sep 17 00:00:00 2001</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">From: Mr-Z-2697 <74594146+Mr-Z-2697@users.noreply.github.com></div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">Date: Sun, 13 Jul 2025 16:09:53 +0800</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">Subject: [PATCH] Ensuring the mvdLX is compliant</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;"><br  /></div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">The value of the mvdLX should be in the range of [-2^15, 2^15-1], as specified in H.265 7.4.9.9 Motion vector difference semantics.</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">However there was no safety check in this area, the mvdLX have been observed to exceed the range, under some rare circumstances.</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;"><br  /></div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">This casting will cause overflow and underflow, should the mvd exceeds the int16_t range.</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">But because of the decoding process Equation 8-94 to 8-97, and the MV itself having the range of [-2^15, 2^15-1], the decoded MV will still be correct, and mvdLX will be in the legal range.</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">---</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;"> source/encoder/entropy.cpp | 5 +++--</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;"> 1 file changed, 3 insertions(+), 2 deletions(-)</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;"><br  /></div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">diff --git a/source/encoder/entropy.cpp b/source/encoder/entropy.cpp</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">index d4bf73d53..bc1c0d8e0 100644</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">--- a/source/encoder/entropy.cpp</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">+++ b/source/encoder/entropy.cpp</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">@@ -2104,8 +2104,9 @@ void Entropy::codeRefFrmIdx(const CUData& cu, uint32_t absPartIdx, int list)</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;"> void Entropy::codeMvd(const CUData& cu, uint32_t absPartIdx, int list)</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;"> {</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">     const MV& mvd = cu.m_mvd[list][absPartIdx];</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">-    const int hor = mvd.x;</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">-    const int ver = mvd.y;</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">+    // to ensure the mvdLX is in the range of [-2^15, 2^15-1]</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">+    const int16_t hor = (int16_t)mvd.x;</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">+    const int16_t ver = (int16_t)mvd.y;</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;"> </div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">     encodeBin(hor != 0 ? 1 : 0, m_contextState[OFF_MV_RES_CTX]);</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">     encodeBin(ver != 0 ? 1 : 0, m_contextState[OFF_MV_RES_CTX]);</div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">-- </div><div style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0); line-height: 1.43;">2.50.1.windows.1</div><div style="line-height: 1.43;"><span style="font-family: -apple-system, system-ui; font-size: 14px; color: rgb(0, 0, 0);"><br  /></span><br  /></div><article style="line-height: 1.43;"><article style="line-height:1.43"></article></article>