clc;
clear; close all;
L=255;
uncomp_cached=0;
%compression='h264';
%compression='h265';
%compression='dcp';

%compression_type='intra';
%compression_type='intra';
compression_type='inter';

sequence='ducks';
%sequence='lupo';
%sequence='xxx';
delete('compvids/*.*');
ib=1;

Nbitrates=10;
bitrates_K=logspace(0,2,Nbitrates)*1000;
dcp_comp_levels=round(linspace(10,50,Nbitrates));

for bitrate_K=bitrates_K;
    for icoder=1:3,
       
        fprintf('Deleting temporary images ....\n');
        delete('images/*.png');
        
        switch icoder
            case 1
                compression='h264';
                if strcmp(compression_type,'intra')
                fname=sprintf('compvids/%s_h264_intra_%d_K.mkv',sequence,round(bitrate_K));
                else
                fname=sprintf('compvids/%s_h264_%d_K.mkv',sequence,round(bitrate_K));
                end
            case 2
                compression='h265';
                if strcmp(compression_type,'intra')
                fname=sprintf('compvids/%s_h265_intra_%d_K.mkv',sequence,round(bitrate_K));
                else
                fname=sprintf('compvids/%s_h265_%d_K.mkv',sequence,round(bitrate_K));
                end
            case 3
                compression='dcp';
                
                cmp=dcp_comp_levels(ib);
                fname=sprintf('compvids/%s_dcp_%d_cmp.mxf',sequence,cmp);
        end
        
        
        fprintf('Compressing with %s at %f Mbps \n',compression,bitrate_K/1000);
        
        tic;
        if strcmp(compression,'h264')
            if strcmp(compression_type,'intra')
                dos(sprintf('ffmpeg -i %s.mkv -vcodec libx264 -b:v %dK -minrate %dK -maxrate %dK -intra %s',...
                    sequence,bitrate_K,bitrate_K,bitrate_K,fname));
            else
                dos(sprintf('ffmpeg -i %s.mkv -vcodec libx264 -b:v %dK -minrate %dK -maxrate %dK %s',...
                    sequence,bitrate_K,bitrate_K,bitrate_K,fname));
            end
        elseif strcmp(compression,'h265')
            if strcmp(compression_type,'intra')
                dos(sprintf('ffmpeg -i %s.mkv -vcodec libx265 -b:v %dK -minrate %dK -maxrate %dK -intra %s',...
                    sequence,bitrate_K,bitrate_K,bitrate_K,fname));
            else
                dos(sprintf('ffmpeg -i %s.mkv -vcodec libx265 -b:v %dK -minrate %dK -maxrate %dK %s',...
                    sequence,bitrate_K,bitrate_K,bitrate_K,fname));
            end
        elseif strcmp(compression,'dcp')
            %if not(strcmp(compression_type,'intra'))
            %    error('No inter-frame compression for JPEG2000 MXF');
            %end
            
            dos(sprintf('ffmpeg -i %s.mkv -vcodec libopenjpeg -compression_level %d  %s',sequence,cmp,fname));
        end
        t_coder(icoder,ib)=toc;
        if strcmp(compression,'dcp')
            % determine file size
            ddd=dir(fname);
            fsize=ddd.bytes;
            fsize_K=fsize*8/1000/10;
            fname2=sprintf('compvids/%s_dcp_%d_cmp_%d_K.mxf',sequence,cmp,round(fsize_K));
            movefile(fname,fname2);
            fname=fname2;
            dcp_bitrates_K(ib)=fsize_K;
        end    
          
        
        
        if not(uncomp_cached)
        dos(sprintf('ffmpeg -i %s.mkv -an -f image2 -pix_fmt rgb24 images/uncomp_%%4d.png',sequence));
        uncomp_cached=1;
        end
        dos(sprintf('ffmpeg -i %s -an -f image2 -pix_fmt rgb24 images/comp_%%4d.png',fname));
        
    
        filelist=dir('images/uncomp*.png');
        
        WPSNR_YCbCr=[];
        
        for i=1:length(filelist),
            
            fprintf(' Analyzing frame %d out of %d \n',i,length(filelist));
            
            uncomp_image=imread(sprintf('images/uncomp_%04d.png',i));
            comp_image=imread(sprintf('images/comp_%04d.png',i));
            
            uncomp_image=YCbCr(uncomp_image);
            comp_image=YCbCr(comp_image);
            
    
            
            [WPSNR_Y, WMSE_Y]=PSNR(uncomp_image(:,:,1),comp_image(:,:,1),L);
            [WPSNR_Cb, WMSE_Cb]=PSNR(uncomp_image(:,:,2),comp_image(:,:,2),L);
            [WPSNR_Cr, WMSE_Cr]=PSNR(uncomp_image(:,:,3),comp_image(:,:,3),L);
            
            WPSNR_YCbCr(i)=0.8*WPSNR_Y+0.1*WPSNR_Cb+0.1*WPSNR_Cr;
            
            %    [mssimy, ssim_mapy] = ssim(uncomp_image(:,:,1),comp_image(:,:,1),K,window,L);
            %    [mssimcb, ssim_mapcb] = ssim(uncomp_image(:,:,2),comp_image(:,:,2),K,window,L);
            %    [mssimcr, ssim_mapcr] = ssim(uncomp_image(:,:,3),comp_image(:,:,3),K,window,L);
            
            %    WSSIM_YCbCr(i)=0.8*mssimy+0.1*mssimcb+0.1*mssimcr;
        end
        
        WPSNR_YCbCr_mean(icoder,ib)=mean(WPSNR_YCbCr);
        WPSNR_YCbCr_struct{icoder,ib}=WPSNR_YCbCr;
        
    end
    ib=ib+1;
end

%figure(1);
%subplot(1,2,1),plot(WPSNR_YCbCr),xlabel('Frame number'),ylabel('dB'); title('W-PSNR [dB]');
%subplot(1,2,2),plot(WSSIM_YCbCr),xlabel('Frame number'),ylabel('SSIM'); title('W-SSIM');

%fprintf('Mean W-PSNR: %f, Mean W-SSIM: %f \n',mean(WPSNR_YCbCr),mean(WSSIM_YCbCr));
%fprintf('Mean W-PSNR: %f \n',mean(WPSNR_YCbCr));

