fclose all; clear all; close all;

nframes = 50;

%qps = [22 27 32 37];
qps = 27;
camera = [39 41];
prob = 1-[0.02 0.03 0.05];
%prob2 = [1 6 10]; % Corresponde a porcentagem fixa de erros com rand(1) para 12 pacotes;
%prob2 = [1 3 7]; 
ilambda = 5:0.5:18; %antes desde 1.5
lambda = 2.^ilambda;
npacotesPorFrame = 3; 
ACK_frame = 4;
w_fileA='Results\weight_cam';
%w_fileA='none';
w = 1280;
h = 960;
raw_size = h*w*nframes*1.5;

    
dep_orig1 = ['Results\depth-cam' num2str(camera(1)) '.yuv']; 
dep_orig2 = ['Results\depth-cam' num2str(camera(2)) '.yuv']; 
cor_orig1 = ['Results\color-cam' num2str(camera(1)) '.yuv'];
cor_orig2 = ['Results\color-cam' num2str(camera(2)) '.yuv'];
for nqps = 1:1:length(qps)
    
       dep_nom1 = [dep_orig1(1:end-4) '_Normal_QP' num2str(qps(nqps)) '.yuv'];
       dep_nom2 = [dep_orig2(1:end-4) '_Normal_QP' num2str(qps(nqps)) '.yuv'];
       cor_nom1 = [cor_orig1(1:end-4) '_Normal_QP' num2str(qps(nqps)) '.yuv'];
       cor_nom2 = [cor_orig2(1:end-4) '_Normal_QP' num2str(qps(nqps)) '.yuv'];
       
       syn_normal = ['Results\Syth_Normal_QP' num2str(qps(nqps)) '.yuv'];
              
       %sintetizem sem erro H.264 
       config_file_ViewSyn_ptm40('configfile.cfg', 'ViewSynthesizer\calibParams-pantomime.txt', cor_nom1, cor_nom2, dep_nom1, dep_nom2, nframes, syn_normal);
       system('ViewSynthesizer\ViewSynVC8.exe configfile.cfg');
       
       for nprob = 1:1:length(prob)
            dep_nom1Err = [dep_nom1(1:end-4) '_pckErr' num2str(nprob) '.yuv'];
            dep_nom2Err = [dep_nom2(1:end-4) '_pckErr' num2str(nprob) '.yuv'];
            cor_nom1Err = [cor_nom1(1:end-4) '_pckErr' num2str(nprob) '.yuv'];
            cor_nom2Err = [cor_nom2(1:end-4) '_pckErr' num2str(nprob) '.yuv'];
            
            syn_nomErr = ['Results\Syth_Normal_QP' num2str(qps(nqps)) '_pckErr' num2str(nprob) '.yuv'];
            
       %sintetizem com erro H.264 

        config_file_ViewSyn_ptm40('configfile.cfg', 'ViewSynthesizer\calibParams-pantomime.txt', cor_nom1Err, cor_nom2Err, dep_nom1Err, dep_nom2Err, nframes, syn_nomErr);
        system('ViewSynthesizer\ViewSynVC8.exe configfile.cfg');

       end
       
end




%PARTE 2 Codificador que leva em consideracao os erros
%-------------------------------------------------------------------------
%codificar utilizando o modo depth (SOMENTE PARA H264 COM FEEDBACK!!!!)
%-------------------------------------------------------------------------

dep_orig1 = ['Results\depth-cam' num2str(camera(1)) '.yuv']; 
dep_orig2 = ['Results\depth-cam' num2str(camera(2)) '.yuv']; 
cor_orig1 = ['Results\color-cam' num2str(camera(1)) '.yuv'];
cor_orig2 = ['Results\color-cam' num2str(camera(2)) '.yuv'];
for nqps = 1:1:length(qps)
    
    dep_out1 = [dep_orig1(1:end-4) '_QP' num2str(qps(nqps))];
    dep_out2 = [dep_orig2(1:end-4) '_QP' num2str(qps(nqps))];
    cor_out1 = [cor_orig1(1:end-4) '_QP' num2str(qps(nqps))];
    cor_out2 = [cor_orig2(1:end-4) '_QP' num2str(qps(nqps))];
    
    for nprob = 1:1:length(prob)
        dep_out_1 = [dep_out1 '_RobustoErr' num2str(nprob)];
        dep_out_2 = [dep_out2 '_RobustoErr' num2str(nprob)];
        cor_out_1 = [cor_out1 '_RobustoErr' num2str(nprob)];
        cor_out_2 = [cor_out2 '_RobustoErr' num2str(nprob)];
        
        for nlambda = 1:1:length(lambda)
            dep_out1L = [dep_out_1 '_L' num2str(nlambda) '.yuv'];
            dep_out2L = [dep_out_2 '_L' num2str(nlambda) '.yuv'];
            cor_out1L = [cor_out_1 '_L' num2str(nlambda) '.yuv'];
            cor_out2L = [cor_out_2 '_L' num2str(nlambda) '.yuv'];
            
            %syn_RobustoErr = ['Results\Syth_QP' num2str(qps(nqps)) '_RobustoErr' num2str(nprob) '_L' num2str(nlambda) '.yuv'];
                    
            % Gera erros
            dep_out1Err = [dep_out1L(1:end-4) '_pckErr' num2str(nprob) '.yuv'];
            dep_out2Err = [dep_out2L(1:end-4) '_pckErr' num2str(nprob) '.yuv'];
            cor_out1Err = [cor_out1L(1:end-4) '_pckErr' num2str(nprob) '.yuv'];
            cor_out2Err = [cor_out2L(1:end-4) '_pckErr' num2str(nprob) '.yuv'];
            
            syn_pckErr = ['Results\Syth_QP' num2str(qps(nqps)) '_RobustoErr' num2str(nprob) '_L' num2str(nlambda) '_pckErr' num2str(nprob) '.yuv'];
            
            yuv_file = dir(dep_out1Err);
            yuv_file2 = dir(dep_out2Err);
            if (yuv_file.bytes == raw_size && yuv_file2.bytes == raw_size)
               %sintetizem robusto com erros (10 frames)
  %             system(['ViewSynthesizer\ViewSynth2.exe '  cor_out1 ' ' rec_out1Err ' ' cor_out2 ' ' rec_out2Err ' ViewSynthesizer\calibParams-champagne-MS.txt 0 80 39 41 40 0 10 1 2031.588498 7784.110870 1280 960 ' syn_pckErr]);
               config_file_ViewSyn_ptm40('configfile.cfg', 'ViewSynthesizer\calibParams-pantomime.txt', cor_out1Err, cor_out2Err, dep_out1Err, dep_out2Err, nframes, syn_pckErr);
               system('ViewSynthesizer\ViewSynVC8.exe configfile.cfg');  
            end
        end
    end
end



