From 4984e0737984fd0d31894d5d5ebc8e89a248c3ab Mon Sep 17 00:00:00 2001
From: Romain Vimont
Date: Thu, 18 Apr 2019 19:31:51 +0200
Subject: [PATCH] Clamp motion vectors at frame level
This fixes bitstream corruption!
Lost hours here: many.
---
src/context.rs | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/src/context.rs b/src/context.rs
index 3cbf0f2..cdcf1d7 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -2610,16 +2610,20 @@ impl<'a> ContextWriter<'a> {
/* TODO: Handle single reference frame extension */
+ let frame_bo = BlockOffset {
+ x: self.bc.blocks.x() + bo.x,
+ y: self.bc.blocks.y() + bo.y,
+ };
// clamp mvs
for mv in mv_stack {
let blk_w = bsize.width();
let blk_h = bsize.height();
let border_w = 128 + blk_w as isize * 8;
let border_h = 128 + blk_h as isize * 8;
- let mvx_min = -(bo.x as isize) * (8 * MI_SIZE) as isize - border_w;
- let mvx_max = (self.bc.blocks.cols() - bo.x - blk_w / MI_SIZE) as isize * (8 * MI_SIZE) as isize + border_w;
- let mvy_min = -(bo.y as isize) * (8 * MI_SIZE) as isize - border_h;
- let mvy_max = (self.bc.blocks.rows() - bo.y - blk_h / MI_SIZE) as isize * (8 * MI_SIZE) as isize + border_h;
+ let mvx_min = -(frame_bo.x as isize) * (8 * MI_SIZE) as isize - border_w;
+ let mvx_max = (self.bc.blocks.frame_cols - frame_bo.x - blk_w / MI_SIZE) as isize * (8 * MI_SIZE) as isize + border_w;
+ let mvy_min = -(frame_bo.y as isize) * (8 * MI_SIZE) as isize - border_h;
+ let mvy_max = (self.bc.blocks.frame_rows - frame_bo.y - blk_h / MI_SIZE) as isize * (8 * MI_SIZE) as isize + border_h;
mv.this_mv.row = (mv.this_mv.row as isize).max(mvy_min).min(mvy_max) as i16;
mv.this_mv.col = (mv.this_mv.col as isize).max(mvx_min).min(mvx_max) as i16;
mv.comp_mv.row = (mv.comp_mv.row as isize).max(mvy_min).min(mvy_max) as i16;
--
2.24.1