%   MarkedtseYUVvideo generated video with marked areas of high TSE
%
%	tseYUVvideo('File1','File2', 'Fille3', width, height, format,parameter) reads
%	the specified files using width and height for resolution and format 
%   for YUV-subsampling. With parameter you can chose if you want to
%   compute the TSE between the y, the u, the v, or the yuv part of the
%   videos
%	
%	File1 --> Name of File one (e.g. 'Test1.yuv') % original
%   File2 --> Name of File two (e.g. 'Test2.yuv') % degradaded
%   File3 --> Name of File three (e.g. 'Teste3.yuv') % File with areas
%   marked
%   width    --> width of a frame  (e.g. 352)
%   height   --> height of a frame (e.g. 280)
%   format   --> subsampling rate ('400','411','420','422' or '444')
%   parameter -->chose the components of the video which are used to compute 
%                 the psnr ('y', 'u', 'v', 'yuv') 
%
%   example: MarkedTseYUVvideo('Test1.yuv','Test2.yuv',352,288,'420','y');
MarkedTseYUVvideo('v1_1280x720_I4.yuv','v1_1280x720_I4_pckErr3.yuv','Nv1_1280x720_I4_pckErr3.yuv',1280,720,'420','y'); 

function MarkedTseYUVvideo(File1,File2, File3, width,height,format,parameter)

%set factor for UV-sampling
    fwidth = 0.5;
    fheight= 0.5;
    if strcmp(format,'400')
        fwidth = 0;
        fheight= 0;
    elseif strcmp(format,'411')
        fwidth = 0.25;
        fheight= 1;
    elseif strcmp(format,'420')
        fwidth = 0.5;
        fheight= 0.5;
    elseif strcmp(format,'422')
        fwidth = 0.5;
        fheight= 1;
    elseif strcmp(format,'444')
        fwidth = 1;
        fheight= 1;
    else
        display('Error: wrong format');
    end
    
    % Open Files
    % get Filesize and Framenumber
    filep = dir(File1); 
    fileBytes = filep.bytes; %Filesize1
    clear filep
    framenumber1 = fileBytes/(width*height*(1+2*fheight*fwidth)); %Framenumber1
    filep = dir(File1);
    fileBytes = filep.bytes; %Filesize2
    clear filep
    total_error = 0;
    framenumber2 = fileBytes/(width*height*(1+2*fheight*fwidth)); %Framenumber2
    if mod(framenumber1,1) ~= 0 | mod(framenumber2,1) ~= 0 | framenumber1~=framenumber2
        display('Error: wrong resolution, format, filesize or different video lengths');
    else
    NewVideo = fopen (File3,'w');
    % wait bar deactivated for now
    % h = waitbar(0,'Please wait ...');
    for cntf = 1:framenumber1
           %waitbar(cntf/framenumber1,h);
           %load data of frames
           YUV1 = loadFileYUV(width,height,cntf,File1,fheight,fwidth);  % original
           YUV2 = loadFileYUV(width,height,cntf,File2,fheight,fwidth);  % degraded
           error_frame = (((YUV1-YUV2).^2)); % calculate the squared difference
           YUV3 = YUV1 + error_frame ; % marked video
           save_yuv(YUV3,NewVideo,width,height,fheight,fwidth);
    end
%        close(h);
    end



       %Embedding the mark into mid-frequency DCT coefficients
       LYdct(123:210,133:220) = LYdct(123:210,133:220) + (alpha .* FinalMark);
                 
       % Take the inverse DCT
       Yidct = idct2 (LYdct);
       Uidct = idct2 (Udct);
       Vidct = idct2 (Vdct);
       % Put the pixels in the yuv video format	
       Yvector = reshape (Yidct, 1, Width * Height);
       Uvector = reshape (Uidct, 1,  Widthuv * Heightuv);
       Vvector = reshape (Vidct, 1, Widthuv * Heightuv);
   
       fwrite (NewFile, Yvector);
       fwrite (NewFile, Uvector);
       fwrite (NewFile, Vvector);
       % Look for position of next frame
       fseek (Fileop, Frame*factorx*Width*Height, 'bof');
   end
   % close video
   fclose(NewFile);

  %%%%%%%%%%%%%%%%%%%%%%
  % Extracting the mark
  %%%%%%%%%%%%%%%%%%%%%%
   
   % open marked video
   MarkedVideo = fopen (video_out,'r');
   % setting up matrix to store extracted mark
   ExtractedMark = double(zeros(88,88));
   % setting up seed again
   randn('seed',44);
   % Reading N_R frames to extract mark
   for Frame=1:N_R
       % generating random matrices
       PRN = (randn(88));
       PRN = 2*(PRN - mean(mean(PRN)));
       PRN = sign(PRN);             
   
       %read Y component 
       Yvector = fread (MarkedVideo, Width * Height, 'uchar');
       Ymatrix = reshape (Yvector, Height, Width);
       LYdct = dct2 (Ymatrix);
       ExtractedMark = (LYdct(123:210,133:220) .* PRN) + ExtractedMark;

       fseek (MarkedVideo, Frame*factorx*Width*Height, 'bof');
     end
   
   ExtractedMark = ExtractedMark/(N_R*alpha);
   figure, imshow(ExtractedMark);
   ExtractedMark2 = ExtractedMark-mean(mean(ExtractedMark));

   pause
   for i=1:88
       for j=1:88
          if ExtractedMark2(i,j) <0
              ExtractedMark2(i,j)=0;
           else
              ExtractedMark2(i,j)=1;
           end
       end
   end
   imwrite(ExtractedMark2, 'Imagemark.tif','tif');
   
   figure, imshow(ExtractedMark2);
   pause
   

   %Calculating the Etse(Degradation of the Mark)
   Etse = 0;
   for i=1:88
       for j=1:88
           Etse = ((ImageMark(i,j) - ExtractedMark2(i,j)).^2) + Etse;
       end
   end

   fclose(Fileop);
   fclose(MarkedVideo);
