function [ frame_out ] = decode_B_frame( frame_encoded, ref_forward, ref_backward ,MB_size, B_size, Q, u_f, v_f, u_b, v_b )

[Y_ref_f, chroma_ref_f] = RGB_to_YCbCr(ref_forward);
[Y_ref_b, chroma_ref_b] = RGB_to_YCbCr(ref_backward);

enc_luma = frame_encoded.encoded_luma;
enc_chroma = frame_encoded.encoded_chroma;

Nmb = size(enc_luma)/MB_size;

Y_mb = mat2cell(enc_luma, repmat(MB_size,1,Nmb(1)), repmat(MB_size,1,Nmb(2)));
Y_decoded = cell(size(Y_mb));

chroma_mb = mat2cell(enc_chroma, repmat(MB_size/2,1,Nmb(1)), repmat(MB_size/2,1,Nmb(2)),2);
chroma_decoded = cell(size(chroma_mb));

for n = 1 : Nmb(1)
    for m = 1 : Nmb(2)
        Y_inv_q = inverse_quantize( Y_mb{n,m}, MB_size, B_size, Q );
        chroma_inv_q = inverse_quantize( chroma_mb{n,m}, MB_size, B_size, Q );
        
        
        Y_decoded{n,m} = 0.5*motion_compensation( Y_inv_q, Y_ref_f, u_f(n,m), v_f(n,m), n, m  )+...
                         0.5*motion_compensation( Y_inv_q, Y_ref_b, u_b(n,m), v_b(n,m), n, m  );
        chroma_decoded{n,m} = 0.5*motion_compensation( chroma_inv_q, chroma_ref_f, u_b(n,m), v_f(n,m), n, m  )+...
                              0.5*motion_compensation( chroma_inv_q, chroma_ref_b, u_b(n,m), v_b(n,m), n, m  );
        
    end
end

Y_decoded = cell2mat(Y_decoded);
chroma_decoded = cell2mat(chroma_decoded);

frame_out = YCbCr_to_RGB( Y_decoded, chroma_decoded );


end

