function [ u, v ] = get_frame_motion_vectors( P_mb, I_mb, MB_size, search_width )

u = zeros(size(P_mb));
v = zeros(size(P_mb));
I = cell2mat(I_mb);
I_dummy = [I 256*ones(size(I,1),1);  255*ones(1, size(I,2)+1)];
for n = 1 : size(P_mb,1)
    for m = 1 : size(P_mb,2)
        
        act = P_mb{n,m};
        
        [i,j] = meshgrid((n-1)*MB_size+1-search_width:n*MB_size+search_width,(m-1)*MB_size+1-search_width:m*MB_size+search_width);
        i( i <= 0 ) = size(I_dummy,1);  i( i > size(I,1) ) = size(I_dummy,1);
        j( j <= 0 ) = size(I_dummy,2);  j( j > size(I,2) ) = size(I_dummy,2);
        
        search_area = reshape ( I_dummy( sub2ind( size(I_dummy),i(:),j(:)  ) ) , MB_size+2*search_width, MB_size+2*search_width)';
        
        
        
        shift_parameter =  (size(search_area)-size(act))+1;
        cost_fun = zeros(shift_parameter);
        for k = 1 : shift_parameter(1)
            for l = 1 : shift_parameter(2)
                cost_fun(k,l) = sum( sum(  ( act - search_area( k:k+MB_size-1, l:l+MB_size-1 )    ).^2  ) );
            end
        end
        [~,ind] = min(cost_fun(:));
        [N,M] = ind2sub(size(cost_fun),ind);
        
        u(n,m) = (M - search_width- 1);
        v(n,m) = (N - search_width  -1);
        
    end
end


end

