Skip to content
Permalink
Browse files

Clamp motion vectors at frame level

This fixes bitstream corruption!

Lost hours here: many.
  • Loading branch information...
rom1v authored and tdaede committed Apr 18, 2019
1 parent 888250e commit 4984e0737984fd0d31894d5d5ebc8e89a248c3ab
Showing with 8 additions and 4 deletions.
  1. +8 −4 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;

0 comments on commit 4984e07

Please sign in to comment.
You can’t perform that action at this time.