function PSNR_estimated=xml2psnr(xmlfile, W, H)
% Function for PSNR estimation of the MPEG 4 AVC
% coded video using extrated data from ldecodec
% in a XML file.
% PSNR_estimated=xml2psnr(xmlfile, W, H), where
% xmlfile is the adress to the XML file from ldecod,
% and W and H specifie videos resolution.
% Created: May, 2013. Ondrej Zach.

data=xml2struct(xmlfile);
data=data.AVCTrace.Picture;
nFrames=size(data,2);
nMB=W*H/256;
xmldata=uint8(zeros(nMB,2,nFrames));
for i=1:nFrames
    xmldata(:,:,i)=str2num(data{1,i}.Text);
end
clear data
QP=squeeze(mode(single(xmldata(:,1,:))));
meanZ=squeeze(mean(xmldata(:,2,:)));
minZ=squeeze(min(xmldata(:,2,:)));
clear xmldata W H i nFrames nMB
save(xmlfile(1:end-4))
%%

if mode(QP) < 31
    PSNR_estimated=285.8914*(1/(log(mean(QP))))-.2652*mean(meanZ)+.0995*mode(single(minZ));
 elseif mode(QP) > 30 & mode(QP) < 37
    PSNR_estimated=285.8914*(1/(log(mode(QP))))-.2652*mean(meanZ)+.0995*mode(single(minZ));
elseif mode(QP) > 36
    PSNR_estimated=285.8914*(1/(log(mode(QP))))-.2652*mean(meanZ)+.0995*mean(single(minZ));
end
str=sprintf('%s          %s',xmlfile, num2str(PSNR_estimated));
disp(str)