fclose all; clear all; close all;

nframes = 12;

%qps = [22 27 32 37];
qps = 0;
%camera = [39 41];
camera = [39];
prob = 1-[0.02 0.03 0.05];
prob2 = [3 5 8]; % Corresponde a porcentagem fixa de erros com rand(1) para 12 pacotes;
%prob2 = [1 5 7]; % Corresponde a porcentagem fixa 3 pacotes [1 3 7] ACK4
%ilambda = 5:0.5:18; 
%lambda = 2.^ilambda;
npacotesPorFrame = 12; 
ACK_frame = 4;
w_file1 = 'none';
%w_fileA='none';


w = 1024;
h = 768;

%PARTE 1 % O codificador nao leva em consideracao os erros
for ncam = 1:1:length(camera)
    seq_orig1 = ['Results\color-cam' num2str(camera(ncam)) '.yuv'];
%     seq_orig2 = ['depth_pantomime_' num2str(camera(ncam)) '.yuv'];
%     w_file1 = 'none';
    
    for nqps = 1:1:length(qps)
        
        rec_out1 = [seq_orig1(1:end-4) '_Normal_QP' num2str(qps(nqps)) '.yuv'];
%         rec_out2 = [seq_orig2(1:end-4) '_Normal_QP' num2str(qps(nqps)) '.yuv'];
        bin_out1 = [seq_orig1(1:end-4) '_Normal_QP' num2str(qps(nqps)) '.264'];
%         bin_out2 = [seq_orig2(1:end-4) '_Normal_QP' num2str(qps(nqps)) '.264'];
        
         system(['H264_Original\bin\lencod -d H264_Original\bin\encoder_color.cfg -p InputFile="' seq_orig1 '" -p FramesToBeEncoded=' num2str(nframes) ' -p SourceWidth=' num2str(w) ' -p SourceHeight=' num2str(h)...
             ' -p OutputWidth=' num2str(w) ' -p OutputHeight=' num2str(h) ' -p ReconFile="' rec_out1 '" -p OutputFile="' bin_out1 '" -p QPISlice='  num2str(qps(nqps)-1)...
             ' -p QPPSlice=' num2str(qps(nqps)) ' -p Depth_coding=0 -p ACK_frame=' num2str(ACK_frame) ' -p NumberReferenceFrames=' num2str(ACK_frame) ' -p SliceMode=1 -p SliceArgument=' num2str(((w*h)/(16*16))/npacotesPorFrame)]);
                         
         % Parte 3: Gera erros
        for nprob = 1:1:length(prob)
            bin_out1Err = [bin_out1(1:end-4) '_pckErr' num2str(nprob) '.264'];
%             bin_out2Err = [bin_out2(1:end-4) '_pckErr' num2str(nprob) '.264'];
            system(['ErrorGen\Release\NalTools' num2str(ncam) ' -o' bin_out1Err ' ' bin_out1 ':0:' num2str(npacotesPorFrame-1) ' ' bin_out1 ':'  num2str((prob2(nprob))) ':' num2str(npacotesPorFrame) ':' num2str(npacotesPorFrame*ACK_frame) ':' num2str(npacotesPorFrame*nframes)]);
%             system(['ErrorGen\Release\NalTools -o' bin_out2Err ' ' bin_out2 ':0:' num2str(npacotesPorFrame-1) ' ' bin_out2 ':'  num2str((prob2(nprob))) ':' num2str(npacotesPorFrame) ':' num2str(npacotesPorFrame*ACK_frame) ':' num2str(npacotesPorFrame*nframes)]);
%             Parte 4.5: Decodifica
            system(['H264_Decodificar\bin\ldecod -i ' bin_out1Err ' -o ' bin_out1Err(1:end-4) '.yuv']);
%             system(['H264_Decodificar\bin\ldecod -i ' bin_out2Err ' -o ' bin_out2Err(1:end-4) '.yuv']);
        end
       
    end
end


%PARTE 2 Codificador que leva em consideracao os erros
%-------------------------------------------------------------------------
%codificar utilizando o modo depth (SOMENTE PARA H264 COM FEEDBACK!!!!)
%-------------------------------------------------------------------------
for ncam = 1:1:length(camera)
    seq_orig1 = ['Results\color-cam' num2str(camera(ncam)) '.yuv'];
%     seq_orig2 = ['Results\depth_pantomime_' num2str(camera(ncam)) '.yuv'];

        
    for nqps = 1:1:length(qps)
        
        seq_out1 = [seq_orig1(1:end-4) '_QP' num2str(qps(nqps))];
%         seq_out2 = [seq_orig2(1:end-4) '_QP' num2str(qps(nqps))];
        
        for nprob = 1:1:length(prob)
            seq_out_1 = [seq_out1 '_RobustoErr' num2str(nprob)];
%             seq_out_2 = [seq_out2 '_RobustoErr' num2str(nprob)];
            
            for nlambda = 1:1:length(lambda)
                rec_out1 = [seq_out_1 '_L' num2str(nlambda) '.yuv'];
%                 rec_out2 = [seq_out_2 '_L' num2str(nlambda) '.yuv'];
                
                bin_out1 = [seq_out_1 '_L' num2str(nlambda) '.264'];
%                 bin_out2 = [seq_out_2 '_L' num2str(nlambda) '.264'];
                               
                system(['H264_color\JM\bin\lencod -d H264_color\JM\bin\encoder_color.cfg -p InputFile="' seq_orig1 '" -p FramesToBeEncoded=' num2str(nframes) ' -p SourceWidth=' num2str(w) ' -p SourceHeight=' num2str(h)...
                    ' -p OutputWidth=' num2str(w) ' -p OutputHeight=' num2str(h) ' -p ReconFile="' rec_out1 '" -p OutputFile="' bin_out1 '" -p QPISlice='  num2str(qps(nqps)-1)...
                    ' -p QPPSlice=' num2str(qps(nqps)) ' -p Depth_coding=1 -p ACK_frame=' num2str(ACK_frame) ' -p prob=' num2str(prob(nprob)) ' -p lambda=' num2str(lambda(nlambda)) ' -p WeightFile="none" -p NumberReferenceFrames=' num2str(ACK_frame)...
                    ' -p SliceMode=1 -p SliceArgument=' num2str(((w*h)/(16*16))/npacotesPorFrame)]);
                                
                % Parte 4: Gera erros
                bin_out1Err = [bin_out1(1:end-4) '_pckErr' num2str(nprob) '.264'];
%                 bin_out2Err = [bin_out2(1:end-4) '_pckErr' num2str(nprob) '.264'];
                system(['ErrorGen\Release\NalTools' num2str(ncam) ' -o' bin_out1Err ' ' bin_out1 ':0:' num2str(npacotesPorFrame-1) ' ' bin_out1 ':'  num2str((prob2(nprob))) ':' num2str(npacotesPorFrame) ':' num2str(npacotesPorFrame*ACK_frame) ':' num2str(npacotesPorFrame*nframes)]);
%                 system(['ErrorGen\Release\NalTools -o' bin_out2Err ' ' bin_out2 ':0:' num2str(npacotesPorFrame-1) ' ' bin_out2 ':'  num2str((prob2(nprob))) ':' num2str(npacotesPorFrame) ':' num2str(npacotesPorFrame*ACK_frame) ':' num2str(npacotesPorFrame*nframes)]);
%                 % Parte xxx
%                     arquivo = dir(bin_out1Err);
%                     rateErrCorr(nlambda, nprob, nqps, ncam) = arquivo.bytes;
                % Parte 4.5: Decodifica
                system(['H264_Decodificar\bin\ldecod -i ' bin_out1Err ' -o ' bin_out1Err(1:end-4) '.yuv']);
%                 system(['H264_Decodificar\bin\ldecod -i ' bin_out2Err ' -o ' bin_out2Err(1:end-4) '.yuv']);
            end
        end
    end
end




