% read images


function [Y_encoded, PSNR, rate] = NoMC_2D_DCT(filename,endframe,movietype,Block_Size_x,Block_Size_y,Block_Size_t,Support_XY_begin,Support_XY_end, Support_t_begin,stepsize,outputfile,StartSupport);

Y = ReadVideo(filename, endframe, movietype);

Support_t_end = 0;

sizex = size(Y{1,1},1);
sizey = size(Y{1,1},2);

Y_encoded = cell(1,endframe);
PSNR = [];
rate=[];

DCTCOEFs = cell(sizex/Block_Size_x,sizey/Block_Size_y,endframe/Block_Size_t);
DCTCOEFsCompressed = cell(sizex/Block_Size_x,sizey/Block_Size_y,endframe/Block_Size_t);
Inverse_DCTCOEFs = cell(sizex/Block_Size_x,sizey/Block_Size_y,endframe/Block_Size_t);

for i=1:-Support_t_begin+Block_Size_t
    Y_encoded{1,i}=Y{1,i};
end

maxindexmat = -inf*ones(Block_Size_x, Block_Size_y);
minindexmat = inf*ones(Block_Size_x, Block_Size_y);

for k=-Support_t_begin+Block_Size_t:Block_Size_t:endframe-Block_Size_t
    k
    kk=1+(k-(-Support_t_begin+Block_Size_t))/Block_Size_t;
    
    % initialization of the current and previous matrices
    for i=1:Block_Size_t
      IC(:,:,i) = Y{1,k+i};
    end
    
    % DCT
    for i=1:Block_Size_t
        Y_encoded{1,k+i}=IC(:,:,i);
    end   
    for i=Support_XY_end/Block_Size_x+1:(sizex/Block_Size_x-Support_XY_end/Block_Size_x)
        for j=Support_XY_end/Block_Size_y+1:(sizey/Block_Size_y-Support_XY_end/Block_Size_y)
            DCTCOEFs{i,j,(k)/Block_Size_t+1} = dct2(IC((i-1)*Block_Size_x+1:i*Block_Size_x,(j-1)*Block_Size_y+1:j*Block_Size_y));
            % Quantize
            [DCTCOEFsCompressed{i,j,(k)/Block_Size_t+1}, maxindexmat, minindexmat] = QuantizeCoefs2(DCTCOEFs{i,j,(k)/Block_Size_t+1}, stepsize, maxindexmat, minindexmat);
            Inverse_DCTCOEFs{i,j,(k-1)/Block_Size_t+1} = idct2(DCTCOEFsCompressed{i,j,(k)/Block_Size_t+1}*stepsize);
            for ii=1:Block_Size_t
                Y_encoded{1,k+ii}((i-1)*Block_Size_x+1:i*Block_Size_x,(j-1)*Block_Size_y+1:j*Block_Size_y)=Inverse_DCTCOEFs{i,j,(k-1)/Block_Size_t+1}(:,:,ii);
            end             
         end
    end        
end

%PSNR calculation
PSNR = GetPSNR(Y, Y_encoded, [Support_XY_end+1 sizex-Support_XY_end], [1+Support_XY_end sizey-Support_XY_end], [StartSupport endframe]);




% set up symbol frequency structures
symbolfreq = cell(Block_Size_x, Block_Size_y);
for i=1:Block_Size_x
   for j=1:Block_Size_y
		symbolfreq{i,j} = zeros(1, maxindexmat(i,j)-minindexmat(i,j)+1);      
   end
end

% find symbol frequencies
for kk=StartSupport:Block_Size_t:endframe
    	kk    
    	for i=Support_XY_end/Block_Size_x+1:(sizex/Block_Size_x-Support_XY_end/Block_Size_x)
        for j=Support_XY_end/Block_Size_y+1:(sizey/Block_Size_y-Support_XY_end/Block_Size_y)
           symbolfreq = FindSymbolFreq2(DCTCOEFsCompressed{i,j,(kk-1)/Block_Size_t+1}, symbolfreq, maxindexmat, minindexmat);
        end
     end
end

% calculate probabilities
prob = cell(Block_Size_x, Block_Size_y);
H = zeros(Block_Size_x, Block_Size_y);
for i=1:Block_Size_x
   for j=1:Block_Size_y
      prob{i,j} = symbolfreq{i,j}/sum(symbolfreq{i,j});      
     	nonzero = find(prob{i,j}~=0);
      H(i,j) = sum(-prob{i,j}(nonzero).*log2(prob{i,j}(nonzero)));
   end
end

Htotal = sum(sum(H)); 
rate = Htotal/(Block_Size_x*Block_Size_y*Block_Size_t);


eval(['save ./SAVEALL/NoMC_2D_DCT_' num2str(stepsize) outputfile ' Htotal rate PSNR stepsize Y_encoded H']);




