-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPsnrCalculation.java
More file actions
142 lines (122 loc) · 5.54 KB
/
Copy pathPsnrCalculation.java
File metadata and controls
142 lines (122 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cn.edu.ustc.aaron.common;
import java.util.*;
import Jama.Matrix;
public class PsnrCalculation {
private int width;
private int height;
private int widthC;
private int heightC;
private int totalFrames;
private int gopSize;
private boolean codeCbCr;
private double totalBytes;
private int frameRate;
private ArrayList<Picture> picOrgList;
private ArrayList<Picture> picDecList;
public PsnrCalculation (boolean codeCbCr, double totalBytes, int frameRate, int width, int height, int totalFrames, int gopSize) {
this.codeCbCr = codeCbCr;
this.totalBytes = totalBytes;
this.frameRate = frameRate;
this.width = width;
this.height = height;
this.widthC = width >> 1;
this.heightC = height >> 1;
this.totalFrames = totalFrames;
this.gopSize = gopSize;
}
public static void main(String[] args) {
// read paras
XmlDocument paraXml = new DomXmlDocument();
HashMap<String, String> paraMap = paraXml.parseXml(args[0]);
int width = Integer.parseInt(paraMap.get("Width"));
int height = Integer.parseInt(paraMap.get("Height"));
int totalFrames = Integer.parseInt(paraMap.get("TotalFrames"));
int gopSize = Integer.parseInt(paraMap.get("GopSize"));
boolean codeCbCr = Boolean.parseBoolean(paraMap.get("CodeCbCr"));
double totalBytes = Double.parseDouble(paraMap.get("TotalBytes"));
int frameRate = Integer.parseInt(paraMap.get("FrameRate"));
PsnrCalculation psnrCalc = new PsnrCalculation(codeCbCr, totalBytes, frameRate, width, height, totalFrames, gopSize);
psnrCalc.readPic(paraMap.get("OrgYuv"), paraMap.get("DecYuv"));
psnrCalc.calcPsnr();
psnrCalc.calcBitRate();
}
public static void calcPsnr (String paraFile) {
// read paras
DomXmlDocument paraXml = new DomXmlDocument();
HashMap<String, String> paraMap = paraXml.parseXml(paraFile);
int width = Integer.parseInt(paraMap.get("Width"));
int height = Integer.parseInt(paraMap.get("Height"));
int totalFrames = Integer.parseInt(paraMap.get("TotalFrames"));
int gopSize = Integer.parseInt(paraMap.get("GopSize"));
boolean codeCbCr = Boolean.parseBoolean(paraMap.get("CodeCbCr"));
double totalBytes = Double.parseDouble(paraMap.get("TotalBytes"));
int frameRate = Integer.parseInt(paraMap.get("FrameRate"));
PsnrCalculation psnrCalc = new PsnrCalculation(codeCbCr, totalBytes, frameRate, width, height, totalFrames, gopSize);
psnrCalc.readPic(paraMap.get("OrgYuv"), paraMap.get("DecYuv"));
System.out.println("BitRate Y-PSNR Cb-PSNR Cr-PSNR");
psnrCalc.calcBitRate();
psnrCalc.calcPsnr();
}
private void calcBitRate () {
double time = (double)(totalFrames - gopSize) / (double)frameRate;
double bitrate = 0.008 * totalBytes / time;
System.out.printf("%.4f", bitrate);
}
private void readPic (String rFilename, String rDecFilename) {
picOrgList = new ArrayList<>();
ReadYCbCr rYCbCr = ReadYCbCr.getInstance();
// read Org
rYCbCr.startReading(rFilename, width, width >> 1);
rYCbCr.readPic(width, height, 0, totalFrames, picOrgList);
picDecList = new ArrayList<>();
rYCbCr = ReadYCbCr.getInstance();
// read Dec
rYCbCr.startReading(rDecFilename, width, width >> 1);
rYCbCr.readPic(width, height, 0, totalFrames - gopSize, picDecList);
}
private void calcPsnr () {
long ssdY = 0;
long ssdCb = 0;
long ssdCr = 0;
double yPsnr = 0.0;
double cbPsnr = 0.0;
double crPsnr = 0.0;
int maxvalY = 255;
int maxvalC = 255;
int iSize = width * height;
double fRefValueY = (double) maxvalY * maxvalY * iSize;
double fRefValueC = (double) maxvalC * maxvalC * iSize / 4.0;
Picture orgPic = null;
Picture decPic = null;
for (int framesNo = gopSize; framesNo < totalFrames; framesNo++) {
orgPic = picOrgList.get(framesNo);
decPic = picDecList.get(framesNo - gopSize);
ssdY = calcSSD(orgPic.getY(), decPic.getY(), width, height);
yPsnr += ((ssdY != 0) ? 10.0 * log(fRefValueY / (double)ssdY, 10.0) : 99.99 );
if (codeCbCr) {
ssdCb = calcSSD(orgPic.getCb(), decPic.getCb(), widthC, heightC);
ssdCr = calcSSD(orgPic.getCr(), decPic.getCr(), widthC, heightC);
cbPsnr += ((ssdCb != 0) ? 10.0 * log(fRefValueC / (double)ssdCb, 10.0) : 99.99 );
crPsnr += ((ssdCr != 0) ? 10.0 * log(fRefValueC / (double)ssdCr, 10.0) : 99.99 );
}
}
System.out.printf(" %.4f", yPsnr/(totalFrames - gopSize));
if (codeCbCr) {
System.out.printf(" %.4f", cbPsnr/(totalFrames - gopSize));
System.out.printf(" %.4f", crPsnr/(totalFrames - gopSize));
}
}
private long calcSSD (int[] org, int[] dec, int width, int height) {
long ssd = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int diff = org[y * width + x] - dec[y * width + x];
ssd += diff * diff;
}
}
return ssd;
}
private double log(double value, double base) {
return Math.log(value) / Math.log(base);
}
}