%Corpo da funcao.
%Abre o arquivo.
w = 1280;
h = 720;
nframes = 500;
sum_psnr = 0;
nBits = 8;
videos = [1 7 8 9 10 11 12];

%Monta os frames de Luminancia.
frames_used = 0;

for vF = 1 : length(videos)
    filename_in = dir(strcat('../Prep-seq-Exp3/YUV_Movies/blkblr_v',num2str(videos(vF)),'_*_0.yuv'));
    in_size = size(filename_in,1);

    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');
            
                %%% TSE: Total Square Error
                tse =  sum((img_in - img_ref).^2);
            
            end
            % Salvar os Pesos
            % TSE
            fid = fopen(strcat(filename_in(inF).name(1:end-4),'_TSE.txt'),'a');
            fprintf(fid, '%s: %f\n\n', filename_rf(inR).name, tse);
            fclose all;
            disp (strcat('Done:',num2str(inR),'_of_', num2str(rf_size)))
        end
    end
end
clear all