function [ output ] = motion_compensation(  act_mb, ref, u, v, n, m  )

MB_size = size( act_mb, 1 );
ind_y = (n-1)*MB_size + 1 + v : n*MB_size + v;
ind_x = (m-1)*MB_size + 1 + u : m*MB_size + u;
ind_x(ind_x<=0) = 1;     ind_x(ind_x>=size(ref,2)) = size(ref,2);
ind_y(ind_y<=0) = 1;     ind_y(ind_y>=size(ref,1)) = size(ref,1);

output = act_mb +...
            ref( ind_y ,   ind_x , : );

end

