function tse = tseGENvideo(orig_videofn, deg_videofn, frame, nframes)
% open original and degraded video
fid_o = fopen(orig_videofn,'r');
fid_d = fopen(deg_videofn,'r');
if ((fid_o == -1)||(fid_d == -1)),
   error('Could not open video file.');
end

% calculate length of files
l=length(orig_videofn);
ld=length(deg_videofn);

% read header from both degraded and original videos 
len = 2*486*720;	% Color variance = Y variance, true?
Ji = fread(fid_o,2^13,'uint8')';
Ji = fread(fid_d,2^13,'uint8')';

% calculate number of the last frame to read
lastframe=frame+nframes-1;
total_error = 0;
for i=1:lastframe,
%     disp(['Processing Frame# ' num2str(i)])
    J_o = fread(fid_o,len,'uint8')'; % read frame i
    J_d = fread(fid_d,len,'uint8')'; % read frame i

    if (i>=frame)
%         disp(['Processing Frame# ' num2str(i)])
        % read frame i from original video
        Y_o = J_o(2:2:length(J_o)); % Read Y component
        C_o =  J_o(1:2:length(J_o)); % Read UV component
        
        % read frame i from degraded video
        Y_d = J_d(2:2:length(J_d)); % upsample matrix to create a Y matrix
        C_d =  J_d(1:2:length(J_d)); % upsample matrix to create a Y matrix
        
        TSE_Y = sum(sum((Y_d-Y_o).^2));
        TSE_C = sum(sum((C_d-C_o).^2));
        total_error = total_error + TSE_Y + TSE_C; 
    end;  
end;
tse = total_error/nframes;
fclose(fid_o);
fclose(fid_d);
% disp(['Finished'])


