Efficient and optimal block matching for motion estimation