function [ frame_quantized ] = quantize( MB_in, B_size, Q  )
% quantizes all blocks of a MB according to Q
%
% MB_in: MB to quantize
% B_size: dimensions of a DCT block (in pixels)
% Q: quantization matrix

if ((size(MB_in,3))~=1)
    if (size(MB_in,1)>=B_size)
        frame_DCT = mat2cell( ...
            MB_in, repmat(B_size,1, size(MB_in,1)/B_size ), repmat(B_size,1, size(MB_in,2)/B_size ),size(MB_in,3) );
    else
        frame_DCT = mat2cell( MB_in, size(MB_in,1), size(MB_in,1), size(MB_in,3) );
    end
else
    frame_DCT = mat2cell( MB_in, repmat(B_size,1, size(MB_in,1)/B_size ), repmat(B_size,1, size(MB_in,2)/B_size ) );
end
for n = 1 : size(frame_DCT,1)
    for m = 1 : size(frame_DCT,2)
        act_block = frame_DCT{n,m};
        for k = 1 : size(act_block,3)
            act_block(:,:,k) = dct2(act_block(:,:,k));
        end
        frame_DCT{n,m} = act_block;
    end
end
frame_quantized = round(  cell2mat(frame_DCT)./repmat( Q , size(MB_in,1)/B_size, size(MB_in,2)/B_size , size(MB_in,3)) );


end

