Unverified Commit aaf45dd0 authored by fbossen's avatar fbossen Committed by GitHub

Refactor: move SAD computation into a separate function (#517)

* Move SAD computation into a separate function

* Always inline SAD function for speed
parent 8e161adb
...@@ -15,6 +15,23 @@ use context::BlockOffset; ...@@ -15,6 +15,23 @@ use context::BlockOffset;
use plane::*; use plane::*;
use context::BLOCK_TO_PLANE_SHIFT; use context::BLOCK_TO_PLANE_SHIFT;
#[inline(always)]
pub fn get_sad(plane_org: &mut PlaneSlice, plane_ref: &mut PlaneSlice, blk_h: usize, blk_w: usize) -> u32 {
let mut sum = 0 as u32;
for _r in 0..blk_h {
{
let slice_org = plane_org.as_slice_w_width(blk_w);
let slice_ref = plane_ref.as_slice_w_width(blk_w);
sum += slice_org.iter().zip(slice_ref).map(|(&a, &b)| (a as i32 - b as i32).abs() as u32).sum::<u32>();
}
plane_org.y += 1;
plane_ref.y += 1;
}
sum
}
pub fn motion_estimation(fi: &FrameInvariants, fs: &mut FrameState, bsize: BlockSize, pub fn motion_estimation(fi: &FrameInvariants, fs: &mut FrameState, bsize: BlockSize,
bo: &BlockOffset, ref_frame: usize) -> MotionVector { bo: &BlockOffset, ref_frame: usize) -> MotionVector {
...@@ -34,19 +51,10 @@ pub fn motion_estimation(fi: &FrameInvariants, fs: &mut FrameState, bsize: Block ...@@ -34,19 +51,10 @@ pub fn motion_estimation(fi: &FrameInvariants, fs: &mut FrameState, bsize: Block
for y in (y_lo..y_hi).step_by(1) { for y in (y_lo..y_hi).step_by(1) {
for x in (x_lo..x_hi).step_by(1) { for x in (x_lo..x_hi).step_by(1) {
let mut sad = 0 as u32;
let mut plane_org = fs.input.planes[0].slice(&po); let mut plane_org = fs.input.planes[0].slice(&po);
let mut plane_ref = rec.frame.planes[0].slice(&PlaneOffset { x: x, y: y }); let mut plane_ref = rec.frame.planes[0].slice(&PlaneOffset { x: x, y: y });
for _r in 0..blk_h { let sad = get_sad(&mut plane_org, &mut plane_ref, blk_h, blk_w);
{
let slice_org = plane_org.as_slice_w_width(blk_w);
let slice_ref = plane_ref.as_slice_w_width(blk_w);
sad += slice_org.iter().zip(slice_ref).map(|(&a, &b)| (a as i32 - b as i32).abs() as u32).sum::<u32>();
}
plane_org.y += 1;
plane_ref.y += 1;
}
if sad < lowest_sad { if sad < lowest_sad {
lowest_sad = sad; lowest_sad = sad;
...@@ -80,19 +88,10 @@ pub fn motion_estimation(fi: &FrameInvariants, fs: &mut FrameState, bsize: Block ...@@ -80,19 +88,10 @@ pub fn motion_estimation(fi: &FrameInvariants, fs: &mut FrameState, bsize: Block
mode.predict_inter(fi, 0, &po, tmp_slice, blk_w, blk_h, ref_frame, &cand_mv, 8); mode.predict_inter(fi, 0, &po, tmp_slice, blk_w, blk_h, ref_frame, &cand_mv, 8);
} }
let mut sad = 0 as u32;
let mut plane_org = fs.input.planes[0].slice(&po); let mut plane_org = fs.input.planes[0].slice(&po);
let mut plane_ref = tmp_plane.slice(&PlaneOffset { x:0, y:0 }); let mut plane_ref = tmp_plane.slice(&PlaneOffset { x:0, y:0 });
for _r in 0..blk_h { let sad = get_sad(&mut plane_org, &mut plane_ref, blk_h, blk_w);
{
let slice_org = plane_org.as_slice_w_width(blk_w);
let slice_ref = plane_ref.as_slice_w_width(blk_w);
sad += slice_org.iter().zip(slice_ref).map(|(&a, &b)| (a as i32 - b as i32).abs() as u32).sum::<u32>();
}
plane_org.y += 1;
plane_ref.y += 1;
}
if sad < lowest_sad { if sad < lowest_sad {
lowest_sad = sad; lowest_sad = sad;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment