%Corpo da funcao.
%Abre o arquivo.
filename_in = dir('../Prep-seq-Exp3/YUV_Movies/blkblr_v12*_0.yuv');
in_size = size(filename_in,1);
w = 1280;
h = 720;
nframes = 500;
sum_psnr = 0;
nBits = 8;

%Monta os frames de Luminancia.
frames_used = 0;
    
for inF = 1 : in_size
    filename_rf = dir(strcat('../Prep-seq-Exp3/YUV_Movies/',filename_in(inF).name(1:10),'*.yuv'));
    rf_size = size(filename_rf,1);
    
    for inR = 1 : rf_size
        arq_in = fopen(strcat('../Prep-seq-Exp3/YUV_Movies/',filename_in(inF).name),'r');
        arq_rf = fopen(strcat('../Prep-seq-Exp3/YUV_Movies/',filename_rf(inR).name),'r');
        for n = 1 : nframes
            fseek(arq_in, 1.5 * w * h * (n - 1),'bof');
            fseek(arq_rf, 1.5 * w * h * (n - 1),'bof');
            img_in = fread(arq_in, h * w,'uint8=>double');
            img_ref = fread(arq_rf,h * w,'uint8=>double');
            mse = (1/(w*h)) * sum((img_in - img_ref).^2);
        end
        % Salvar os Pesos (psnrY)
        fid = fopen(strcat(filename_in(inF).name(1:end-4),'--MSE.txt'),'a');
        fprintf(fid, '%s: %f \n\n', filename_rf(inR).name, mse);
        fclose all;
        disp (strcat('Done:',num2str(inR),'_of_', num2str(rf_size)))
    end
end
clear all