function [ frame_encoded, u ,v  ] = encode_P_frame( frame_in, ref, B_size, MB_size, Q, search_width )
[Y,chroma] = RGB_to_YCbCr(frame_in);
[Y_ref, chroma_ref] = RGB_to_YCbCr(ref);

Nmb = size(Y)/MB_size;

Y_mb = mat2cell(Y, repmat(MB_size,1,Nmb(1)), repmat(MB_size,1,Nmb(2)));
Y_encoded = cell(size(Y_mb));

chroma_mb = mat2cell(chroma, repmat(MB_size/2,1,Nmb(1)), repmat(MB_size/2,1,Nmb(2)),2);
chroma_encoded = cell(size(chroma_mb));

u = zeros(size(Y_mb));
v = zeros(size(Y_mb));
for n = 1 : Nmb(1)
    for m = 1 : Nmb(2)

        [u(n,m), v(n,m)]  = get_motion_vector( Y_mb{n,m}, n,m, Y_ref, MB_size, search_width );
        Y_encoded{n,m} =  quantize(  motion_prediction( Y_mb{n,m}, Y_ref, u(n,m), v(n,m), n, m ), MB_size, B_size, Q );        
        chroma_encoded{n,m} =  quantize(  motion_prediction( chroma_mb{n,m}, chroma_ref, u(n,m), v(n,m), n, m ), MB_size, B_size, Q );
    end
end
Y_encoded = cell2mat(Y_encoded);
chroma_encoded = cell2mat(chroma_encoded);
frame_encoded = struct('encoded_luma',Y_encoded,'encoded_chroma',chroma_encoded);

end

