1*4e366538SXin Li /*
2*4e366538SXin Li * Copyright 2013 The LibYuv Project Authors. All rights reserved.
3*4e366538SXin Li *
4*4e366538SXin Li * Use of this source code is governed by a BSD-style license
5*4e366538SXin Li * that can be found in the LICENSE file in the root of the source
6*4e366538SXin Li * tree. An additional intellectual property rights grant can be found
7*4e366538SXin Li * in the file PATENTS. All contributing project authors may
8*4e366538SXin Li * be found in the AUTHORS file in the root of the source tree.
9*4e366538SXin Li */
10*4e366538SXin Li
11*4e366538SXin Li // Get PSNR or SSIM for video sequence. Assuming RAW 4:2:0 Y:Cb:Cr format
12*4e366538SXin Li // To build: g++ -O3 -o psnr psnr.cc ssim.cc psnr_main.cc
13*4e366538SXin Li // or VisualC: cl /Ox psnr.cc ssim.cc psnr_main.cc
14*4e366538SXin Li //
15*4e366538SXin Li // To enable OpenMP and SSE2
16*4e366538SXin Li // gcc: g++ -msse2 -O3 -fopenmp -o psnr psnr.cc ssim.cc psnr_main.cc
17*4e366538SXin Li // vc: cl /arch:SSE2 /Ox /openmp psnr.cc ssim.cc psnr_main.cc
18*4e366538SXin Li //
19*4e366538SXin Li // Usage: psnr org_seq rec_seq -s width height [-skip skip_org skip_rec]
20*4e366538SXin Li
21*4e366538SXin Li #ifndef _CRT_SECURE_NO_WARNINGS
22*4e366538SXin Li #define _CRT_SECURE_NO_WARNINGS
23*4e366538SXin Li #endif
24*4e366538SXin Li
25*4e366538SXin Li #include <stddef.h>
26*4e366538SXin Li #include <stdio.h>
27*4e366538SXin Li #include <stdlib.h>
28*4e366538SXin Li #include <string.h>
29*4e366538SXin Li #ifdef _OPENMP
30*4e366538SXin Li #include <omp.h>
31*4e366538SXin Li #endif
32*4e366538SXin Li
33*4e366538SXin Li #include "./psnr.h"
34*4e366538SXin Li #include "./ssim.h"
35*4e366538SXin Li #ifdef HAVE_JPEG
36*4e366538SXin Li #include "libyuv/compare.h"
37*4e366538SXin Li #include "libyuv/convert.h"
38*4e366538SXin Li #endif
39*4e366538SXin Li
40*4e366538SXin Li struct metric {
41*4e366538SXin Li double y, u, v, all;
42*4e366538SXin Li double min_y, min_u, min_v, min_all;
43*4e366538SXin Li double global_y, global_u, global_v, global_all;
44*4e366538SXin Li int min_frame;
45*4e366538SXin Li };
46*4e366538SXin Li
47*4e366538SXin Li // options
48*4e366538SXin Li bool verbose = false;
49*4e366538SXin Li bool quiet = false;
50*4e366538SXin Li bool show_name = false;
51*4e366538SXin Li bool do_swap_uv = false;
52*4e366538SXin Li bool do_psnr = false;
53*4e366538SXin Li bool do_ssim = false;
54*4e366538SXin Li bool do_mse = false;
55*4e366538SXin Li bool do_lssim = false;
56*4e366538SXin Li int image_width = 0, image_height = 0;
57*4e366538SXin Li int fileindex_org = 0; // argv argument contains the source file name.
58*4e366538SXin Li int fileindex_rec = 0; // argv argument contains the destination file name.
59*4e366538SXin Li int num_rec = 0;
60*4e366538SXin Li int num_skip_org = 0;
61*4e366538SXin Li int num_skip_rec = 0;
62*4e366538SXin Li int num_frames = 0;
63*4e366538SXin Li #ifdef _OPENMP
64*4e366538SXin Li int num_threads = 0;
65*4e366538SXin Li #endif
66*4e366538SXin Li
67*4e366538SXin Li // Parse PYUV format. ie name.1920x800_24Hz_P420.yuv
ExtractResolutionFromFilename(const char * name,int * width_ptr,int * height_ptr)68*4e366538SXin Li bool ExtractResolutionFromFilename(const char* name,
69*4e366538SXin Li int* width_ptr,
70*4e366538SXin Li int* height_ptr) {
71*4e366538SXin Li // Isolate the .width_height. section of the filename by searching for a
72*4e366538SXin Li // dot or underscore followed by a digit.
73*4e366538SXin Li for (int i = 0; name[i]; ++i) {
74*4e366538SXin Li if ((name[i] == '.' || name[i] == '_') && name[i + 1] >= '0' &&
75*4e366538SXin Li name[i + 1] <= '9') {
76*4e366538SXin Li int n = sscanf(name + i + 1, "%dx%d", width_ptr, height_ptr); // NOLINT
77*4e366538SXin Li if (2 == n) {
78*4e366538SXin Li return true;
79*4e366538SXin Li }
80*4e366538SXin Li }
81*4e366538SXin Li }
82*4e366538SXin Li
83*4e366538SXin Li #ifdef HAVE_JPEG
84*4e366538SXin Li // Try parsing file as a jpeg.
85*4e366538SXin Li FILE* const file_org = fopen(name, "rb");
86*4e366538SXin Li if (file_org == NULL) {
87*4e366538SXin Li fprintf(stderr, "Cannot open %s\n", name);
88*4e366538SXin Li return false;
89*4e366538SXin Li }
90*4e366538SXin Li fseek(file_org, 0, SEEK_END);
91*4e366538SXin Li size_t total_size = ftell(file_org);
92*4e366538SXin Li fseek(file_org, 0, SEEK_SET);
93*4e366538SXin Li uint8_t* const ch_org = new uint8_t[total_size];
94*4e366538SXin Li memset(ch_org, 0, total_size);
95*4e366538SXin Li size_t bytes_org = fread(ch_org, sizeof(uint8_t), total_size, file_org);
96*4e366538SXin Li fclose(file_org);
97*4e366538SXin Li if (bytes_org == total_size) {
98*4e366538SXin Li if (0 == libyuv::MJPGSize(ch_org, total_size, width_ptr, height_ptr)) {
99*4e366538SXin Li delete[] ch_org;
100*4e366538SXin Li return true;
101*4e366538SXin Li }
102*4e366538SXin Li }
103*4e366538SXin Li delete[] ch_org;
104*4e366538SXin Li #endif // HAVE_JPEG
105*4e366538SXin Li return false;
106*4e366538SXin Li }
107*4e366538SXin Li
108*4e366538SXin Li // Scale Y channel from 16..240 to 0..255.
109*4e366538SXin Li // This can be useful when comparing codecs that are inconsistant about Y
ScaleY(uint8_t y)110*4e366538SXin Li uint8_t ScaleY(uint8_t y) {
111*4e366538SXin Li int ny = (y - 16) * 256 / 224;
112*4e366538SXin Li if (ny < 0) {
113*4e366538SXin Li ny = 0;
114*4e366538SXin Li }
115*4e366538SXin Li if (ny > 255) {
116*4e366538SXin Li ny = 255;
117*4e366538SXin Li }
118*4e366538SXin Li return static_cast<uint8_t>(ny);
119*4e366538SXin Li }
120*4e366538SXin Li
121*4e366538SXin Li // MSE = Mean Square Error
GetMSE(double sse,double size)122*4e366538SXin Li double GetMSE(double sse, double size) {
123*4e366538SXin Li return sse / size;
124*4e366538SXin Li }
125*4e366538SXin Li
PrintHelp(const char * program)126*4e366538SXin Li void PrintHelp(const char* program) {
127*4e366538SXin Li printf("%s [-options] org_seq rec_seq [rec_seq2.. etc]\n", program);
128*4e366538SXin Li #ifdef HAVE_JPEG
129*4e366538SXin Li printf("jpeg or raw YUV 420 supported.\n");
130*4e366538SXin Li #endif
131*4e366538SXin Li printf("options:\n");
132*4e366538SXin Li printf(
133*4e366538SXin Li " -s <width> <height> .... specify YUV size, mandatory if none of the "
134*4e366538SXin Li "sequences have the\n");
135*4e366538SXin Li printf(
136*4e366538SXin Li " resolution embedded in their filename (ie. "
137*4e366538SXin Li "name.1920x800_24Hz_P420.yuv)\n");
138*4e366538SXin Li printf(" -psnr .................. compute PSNR (default)\n");
139*4e366538SXin Li printf(" -ssim .................. compute SSIM\n");
140*4e366538SXin Li printf(" -mse ................... compute MSE\n");
141*4e366538SXin Li printf(" -swap .................. Swap U and V plane\n");
142*4e366538SXin Li printf(" -skip <org> <rec> ...... Number of frame to skip of org and rec\n");
143*4e366538SXin Li printf(" -frames <num> .......... Number of frames to compare\n");
144*4e366538SXin Li #ifdef _OPENMP
145*4e366538SXin Li printf(" -t <num> ............... Number of threads\n");
146*4e366538SXin Li #endif
147*4e366538SXin Li printf(" -n ..................... Show file name\n");
148*4e366538SXin Li printf(" -v ..................... verbose++\n");
149*4e366538SXin Li printf(" -q ..................... quiet\n");
150*4e366538SXin Li printf(" -h ..................... this help\n");
151*4e366538SXin Li exit(0);
152*4e366538SXin Li }
153*4e366538SXin Li
ParseOptions(int argc,const char * argv[])154*4e366538SXin Li void ParseOptions(int argc, const char* argv[]) {
155*4e366538SXin Li if (argc <= 1) {
156*4e366538SXin Li PrintHelp(argv[0]);
157*4e366538SXin Li }
158*4e366538SXin Li for (int c = 1; c < argc; ++c) {
159*4e366538SXin Li if (!strcmp(argv[c], "-v")) {
160*4e366538SXin Li verbose = true;
161*4e366538SXin Li } else if (!strcmp(argv[c], "-q")) {
162*4e366538SXin Li quiet = true;
163*4e366538SXin Li } else if (!strcmp(argv[c], "-n")) {
164*4e366538SXin Li show_name = true;
165*4e366538SXin Li } else if (!strcmp(argv[c], "-psnr")) {
166*4e366538SXin Li do_psnr = true;
167*4e366538SXin Li } else if (!strcmp(argv[c], "-mse")) {
168*4e366538SXin Li do_mse = true;
169*4e366538SXin Li } else if (!strcmp(argv[c], "-ssim")) {
170*4e366538SXin Li do_ssim = true;
171*4e366538SXin Li } else if (!strcmp(argv[c], "-lssim")) {
172*4e366538SXin Li do_ssim = true;
173*4e366538SXin Li do_lssim = true;
174*4e366538SXin Li } else if (!strcmp(argv[c], "-swap")) {
175*4e366538SXin Li do_swap_uv = true;
176*4e366538SXin Li } else if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
177*4e366538SXin Li PrintHelp(argv[0]);
178*4e366538SXin Li } else if (!strcmp(argv[c], "-s") && c + 2 < argc) {
179*4e366538SXin Li image_width = atoi(argv[++c]); // NOLINT
180*4e366538SXin Li image_height = atoi(argv[++c]); // NOLINT
181*4e366538SXin Li } else if (!strcmp(argv[c], "-skip") && c + 2 < argc) {
182*4e366538SXin Li num_skip_org = atoi(argv[++c]); // NOLINT
183*4e366538SXin Li num_skip_rec = atoi(argv[++c]); // NOLINT
184*4e366538SXin Li } else if (!strcmp(argv[c], "-frames") && c + 1 < argc) {
185*4e366538SXin Li num_frames = atoi(argv[++c]); // NOLINT
186*4e366538SXin Li #ifdef _OPENMP
187*4e366538SXin Li } else if (!strcmp(argv[c], "-t") && c + 1 < argc) {
188*4e366538SXin Li num_threads = atoi(argv[++c]); // NOLINT
189*4e366538SXin Li #endif
190*4e366538SXin Li } else if (argv[c][0] == '-') {
191*4e366538SXin Li fprintf(stderr, "Unknown option. %s\n", argv[c]);
192*4e366538SXin Li } else if (fileindex_org == 0) {
193*4e366538SXin Li fileindex_org = c;
194*4e366538SXin Li } else if (fileindex_rec == 0) {
195*4e366538SXin Li fileindex_rec = c;
196*4e366538SXin Li num_rec = 1;
197*4e366538SXin Li } else {
198*4e366538SXin Li ++num_rec;
199*4e366538SXin Li }
200*4e366538SXin Li }
201*4e366538SXin Li if (fileindex_org == 0 || fileindex_rec == 0) {
202*4e366538SXin Li fprintf(stderr, "Missing filenames\n");
203*4e366538SXin Li PrintHelp(argv[0]);
204*4e366538SXin Li }
205*4e366538SXin Li if (num_skip_org < 0 || num_skip_rec < 0) {
206*4e366538SXin Li fprintf(stderr, "Skipped frames incorrect\n");
207*4e366538SXin Li PrintHelp(argv[0]);
208*4e366538SXin Li }
209*4e366538SXin Li if (num_frames < 0) {
210*4e366538SXin Li fprintf(stderr, "Number of frames incorrect\n");
211*4e366538SXin Li PrintHelp(argv[0]);
212*4e366538SXin Li }
213*4e366538SXin Li if (image_width == 0 || image_height == 0) {
214*4e366538SXin Li int org_width, org_height;
215*4e366538SXin Li int rec_width, rec_height;
216*4e366538SXin Li bool org_res_avail = ExtractResolutionFromFilename(argv[fileindex_org],
217*4e366538SXin Li &org_width, &org_height);
218*4e366538SXin Li bool rec_res_avail = ExtractResolutionFromFilename(argv[fileindex_rec],
219*4e366538SXin Li &rec_width, &rec_height);
220*4e366538SXin Li if (org_res_avail) {
221*4e366538SXin Li if (rec_res_avail) {
222*4e366538SXin Li if ((org_width == rec_width) && (org_height == rec_height)) {
223*4e366538SXin Li image_width = org_width;
224*4e366538SXin Li image_height = org_height;
225*4e366538SXin Li } else {
226*4e366538SXin Li fprintf(stderr, "Sequences have different resolutions.\n");
227*4e366538SXin Li PrintHelp(argv[0]);
228*4e366538SXin Li }
229*4e366538SXin Li } else {
230*4e366538SXin Li image_width = org_width;
231*4e366538SXin Li image_height = org_height;
232*4e366538SXin Li }
233*4e366538SXin Li } else if (rec_res_avail) {
234*4e366538SXin Li image_width = rec_width;
235*4e366538SXin Li image_height = rec_height;
236*4e366538SXin Li } else {
237*4e366538SXin Li fprintf(stderr, "Missing dimensions.\n");
238*4e366538SXin Li PrintHelp(argv[0]);
239*4e366538SXin Li }
240*4e366538SXin Li }
241*4e366538SXin Li }
242*4e366538SXin Li
UpdateMetrics(uint8_t * ch_org,uint8_t * ch_rec,const int y_size,const int uv_size,const size_t total_size,int number_of_frames,metric * cur_distortion_psnr,metric * distorted_frame,bool compute_psnr)243*4e366538SXin Li bool UpdateMetrics(uint8_t* ch_org,
244*4e366538SXin Li uint8_t* ch_rec,
245*4e366538SXin Li const int y_size,
246*4e366538SXin Li const int uv_size,
247*4e366538SXin Li const size_t total_size,
248*4e366538SXin Li int number_of_frames,
249*4e366538SXin Li metric* cur_distortion_psnr,
250*4e366538SXin Li metric* distorted_frame,
251*4e366538SXin Li bool compute_psnr) {
252*4e366538SXin Li const int uv_offset = (do_swap_uv ? uv_size : 0);
253*4e366538SXin Li const uint8_t* const u_org = ch_org + y_size + uv_offset;
254*4e366538SXin Li const uint8_t* const u_rec = ch_rec + y_size;
255*4e366538SXin Li const uint8_t* const v_org = ch_org + y_size + (uv_size - uv_offset);
256*4e366538SXin Li const uint8_t* const v_rec = ch_rec + y_size + uv_size;
257*4e366538SXin Li if (compute_psnr) {
258*4e366538SXin Li #ifdef HAVE_JPEG
259*4e366538SXin Li double y_err = static_cast<double>(
260*4e366538SXin Li libyuv::ComputeSumSquareError(ch_org, ch_rec, y_size));
261*4e366538SXin Li double u_err = static_cast<double>(
262*4e366538SXin Li libyuv::ComputeSumSquareError(u_org, u_rec, uv_size));
263*4e366538SXin Li double v_err = static_cast<double>(
264*4e366538SXin Li libyuv::ComputeSumSquareError(v_org, v_rec, uv_size));
265*4e366538SXin Li #else
266*4e366538SXin Li double y_err = ComputeSumSquareError(ch_org, ch_rec, y_size);
267*4e366538SXin Li double u_err = ComputeSumSquareError(u_org, u_rec, uv_size);
268*4e366538SXin Li double v_err = ComputeSumSquareError(v_org, v_rec, uv_size);
269*4e366538SXin Li #endif
270*4e366538SXin Li const double total_err = y_err + u_err + v_err;
271*4e366538SXin Li cur_distortion_psnr->global_y += y_err;
272*4e366538SXin Li cur_distortion_psnr->global_u += u_err;
273*4e366538SXin Li cur_distortion_psnr->global_v += v_err;
274*4e366538SXin Li cur_distortion_psnr->global_all += total_err;
275*4e366538SXin Li distorted_frame->y = ComputePSNR(y_err, static_cast<double>(y_size));
276*4e366538SXin Li distorted_frame->u = ComputePSNR(u_err, static_cast<double>(uv_size));
277*4e366538SXin Li distorted_frame->v = ComputePSNR(v_err, static_cast<double>(uv_size));
278*4e366538SXin Li distorted_frame->all =
279*4e366538SXin Li ComputePSNR(total_err, static_cast<double>(total_size));
280*4e366538SXin Li } else {
281*4e366538SXin Li distorted_frame->y = CalcSSIM(ch_org, ch_rec, image_width, image_height);
282*4e366538SXin Li distorted_frame->u =
283*4e366538SXin Li CalcSSIM(u_org, u_rec, (image_width + 1) / 2, (image_height + 1) / 2);
284*4e366538SXin Li distorted_frame->v =
285*4e366538SXin Li CalcSSIM(v_org, v_rec, (image_width + 1) / 2, (image_height + 1) / 2);
286*4e366538SXin Li distorted_frame->all =
287*4e366538SXin Li (distorted_frame->y + distorted_frame->u + distorted_frame->v) /
288*4e366538SXin Li total_size;
289*4e366538SXin Li distorted_frame->y /= y_size;
290*4e366538SXin Li distorted_frame->u /= uv_size;
291*4e366538SXin Li distorted_frame->v /= uv_size;
292*4e366538SXin Li
293*4e366538SXin Li if (do_lssim) {
294*4e366538SXin Li distorted_frame->all = CalcLSSIM(distorted_frame->all);
295*4e366538SXin Li distorted_frame->y = CalcLSSIM(distorted_frame->y);
296*4e366538SXin Li distorted_frame->u = CalcLSSIM(distorted_frame->u);
297*4e366538SXin Li distorted_frame->v = CalcLSSIM(distorted_frame->v);
298*4e366538SXin Li }
299*4e366538SXin Li }
300*4e366538SXin Li
301*4e366538SXin Li cur_distortion_psnr->y += distorted_frame->y;
302*4e366538SXin Li cur_distortion_psnr->u += distorted_frame->u;
303*4e366538SXin Li cur_distortion_psnr->v += distorted_frame->v;
304*4e366538SXin Li cur_distortion_psnr->all += distorted_frame->all;
305*4e366538SXin Li
306*4e366538SXin Li bool ismin = false;
307*4e366538SXin Li if (distorted_frame->y < cur_distortion_psnr->min_y) {
308*4e366538SXin Li cur_distortion_psnr->min_y = distorted_frame->y;
309*4e366538SXin Li }
310*4e366538SXin Li if (distorted_frame->u < cur_distortion_psnr->min_u) {
311*4e366538SXin Li cur_distortion_psnr->min_u = distorted_frame->u;
312*4e366538SXin Li }
313*4e366538SXin Li if (distorted_frame->v < cur_distortion_psnr->min_v) {
314*4e366538SXin Li cur_distortion_psnr->min_v = distorted_frame->v;
315*4e366538SXin Li }
316*4e366538SXin Li if (distorted_frame->all < cur_distortion_psnr->min_all) {
317*4e366538SXin Li cur_distortion_psnr->min_all = distorted_frame->all;
318*4e366538SXin Li cur_distortion_psnr->min_frame = number_of_frames;
319*4e366538SXin Li ismin = true;
320*4e366538SXin Li }
321*4e366538SXin Li return ismin;
322*4e366538SXin Li }
323*4e366538SXin Li
main(int argc,const char * argv[])324*4e366538SXin Li int main(int argc, const char* argv[]) {
325*4e366538SXin Li ParseOptions(argc, argv);
326*4e366538SXin Li if (!do_psnr && !do_ssim) {
327*4e366538SXin Li do_psnr = true;
328*4e366538SXin Li }
329*4e366538SXin Li
330*4e366538SXin Li #ifdef _OPENMP
331*4e366538SXin Li if (num_threads) {
332*4e366538SXin Li omp_set_num_threads(num_threads);
333*4e366538SXin Li }
334*4e366538SXin Li if (verbose) {
335*4e366538SXin Li printf("OpenMP %d procs\n", omp_get_num_procs());
336*4e366538SXin Li }
337*4e366538SXin Li #endif
338*4e366538SXin Li // Open original file (first file argument)
339*4e366538SXin Li FILE* const file_org = fopen(argv[fileindex_org], "rb");
340*4e366538SXin Li if (file_org == NULL) {
341*4e366538SXin Li fprintf(stderr, "Cannot open %s\n", argv[fileindex_org]);
342*4e366538SXin Li exit(1);
343*4e366538SXin Li }
344*4e366538SXin Li
345*4e366538SXin Li // Open all files to compare to
346*4e366538SXin Li FILE** file_rec = new FILE*[num_rec];
347*4e366538SXin Li memset(file_rec, 0, num_rec * sizeof(FILE*)); // NOLINT
348*4e366538SXin Li for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
349*4e366538SXin Li file_rec[cur_rec] = fopen(argv[fileindex_rec + cur_rec], "rb");
350*4e366538SXin Li if (file_rec[cur_rec] == NULL) {
351*4e366538SXin Li fprintf(stderr, "Cannot open %s\n", argv[fileindex_rec + cur_rec]);
352*4e366538SXin Li fclose(file_org);
353*4e366538SXin Li for (int i = 0; i < cur_rec; ++i) {
354*4e366538SXin Li fclose(file_rec[i]);
355*4e366538SXin Li }
356*4e366538SXin Li delete[] file_rec;
357*4e366538SXin Li exit(1);
358*4e366538SXin Li }
359*4e366538SXin Li }
360*4e366538SXin Li
361*4e366538SXin Li const int y_size = image_width * image_height;
362*4e366538SXin Li const int uv_size = ((image_width + 1) / 2) * ((image_height + 1) / 2);
363*4e366538SXin Li const size_t total_size = y_size + 2 * uv_size; // NOLINT
364*4e366538SXin Li #if defined(_MSC_VER)
365*4e366538SXin Li _fseeki64(
366*4e366538SXin Li file_org,
367*4e366538SXin Li static_cast<__int64>(num_skip_org) * static_cast<__int64>(total_size),
368*4e366538SXin Li SEEK_SET);
369*4e366538SXin Li #else
370*4e366538SXin Li fseek(file_org, num_skip_org * total_size, SEEK_SET);
371*4e366538SXin Li #endif
372*4e366538SXin Li for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
373*4e366538SXin Li #if defined(_MSC_VER)
374*4e366538SXin Li _fseeki64(
375*4e366538SXin Li file_rec[cur_rec],
376*4e366538SXin Li static_cast<__int64>(num_skip_rec) * static_cast<__int64>(total_size),
377*4e366538SXin Li SEEK_SET);
378*4e366538SXin Li #else
379*4e366538SXin Li fseek(file_rec[cur_rec], num_skip_rec * total_size, SEEK_SET);
380*4e366538SXin Li #endif
381*4e366538SXin Li }
382*4e366538SXin Li
383*4e366538SXin Li uint8_t* const ch_org = new uint8_t[total_size];
384*4e366538SXin Li uint8_t* const ch_rec = new uint8_t[total_size];
385*4e366538SXin Li if (ch_org == NULL || ch_rec == NULL) {
386*4e366538SXin Li fprintf(stderr, "No memory available\n");
387*4e366538SXin Li fclose(file_org);
388*4e366538SXin Li for (int i = 0; i < num_rec; ++i) {
389*4e366538SXin Li fclose(file_rec[i]);
390*4e366538SXin Li }
391*4e366538SXin Li delete[] ch_org;
392*4e366538SXin Li delete[] ch_rec;
393*4e366538SXin Li delete[] file_rec;
394*4e366538SXin Li exit(1);
395*4e366538SXin Li }
396*4e366538SXin Li
397*4e366538SXin Li metric* const distortion_psnr = new metric[num_rec];
398*4e366538SXin Li metric* const distortion_ssim = new metric[num_rec];
399*4e366538SXin Li for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
400*4e366538SXin Li metric* cur_distortion_psnr = &distortion_psnr[cur_rec];
401*4e366538SXin Li cur_distortion_psnr->y = 0.0;
402*4e366538SXin Li cur_distortion_psnr->u = 0.0;
403*4e366538SXin Li cur_distortion_psnr->v = 0.0;
404*4e366538SXin Li cur_distortion_psnr->all = 0.0;
405*4e366538SXin Li cur_distortion_psnr->min_y = kMaxPSNR;
406*4e366538SXin Li cur_distortion_psnr->min_u = kMaxPSNR;
407*4e366538SXin Li cur_distortion_psnr->min_v = kMaxPSNR;
408*4e366538SXin Li cur_distortion_psnr->min_all = kMaxPSNR;
409*4e366538SXin Li cur_distortion_psnr->min_frame = 0;
410*4e366538SXin Li cur_distortion_psnr->global_y = 0.0;
411*4e366538SXin Li cur_distortion_psnr->global_u = 0.0;
412*4e366538SXin Li cur_distortion_psnr->global_v = 0.0;
413*4e366538SXin Li cur_distortion_psnr->global_all = 0.0;
414*4e366538SXin Li distortion_ssim[cur_rec] = cur_distortion_psnr[cur_rec];
415*4e366538SXin Li }
416*4e366538SXin Li
417*4e366538SXin Li if (verbose) {
418*4e366538SXin Li printf("Size: %dx%d\n", image_width, image_height);
419*4e366538SXin Li }
420*4e366538SXin Li
421*4e366538SXin Li if (!quiet) {
422*4e366538SXin Li printf("Frame");
423*4e366538SXin Li if (do_psnr) {
424*4e366538SXin Li printf("\t PSNR-Y \t PSNR-U \t PSNR-V \t PSNR-All \t Frame");
425*4e366538SXin Li }
426*4e366538SXin Li if (do_ssim) {
427*4e366538SXin Li printf("\t SSIM-Y\t SSIM-U\t SSIM-V\t SSIM-All\t Frame");
428*4e366538SXin Li }
429*4e366538SXin Li if (show_name) {
430*4e366538SXin Li printf("\tName\n");
431*4e366538SXin Li } else {
432*4e366538SXin Li printf("\n");
433*4e366538SXin Li }
434*4e366538SXin Li }
435*4e366538SXin Li
436*4e366538SXin Li int number_of_frames;
437*4e366538SXin Li for (number_of_frames = 0;; ++number_of_frames) {
438*4e366538SXin Li if (num_frames && number_of_frames >= num_frames) {
439*4e366538SXin Li break;
440*4e366538SXin Li }
441*4e366538SXin Li
442*4e366538SXin Li size_t bytes_org = fread(ch_org, sizeof(uint8_t), total_size, file_org);
443*4e366538SXin Li if (bytes_org < total_size) {
444*4e366538SXin Li #ifdef HAVE_JPEG
445*4e366538SXin Li // Try parsing file as a jpeg.
446*4e366538SXin Li uint8_t* const ch_jpeg = new uint8_t[bytes_org];
447*4e366538SXin Li memcpy(ch_jpeg, ch_org, bytes_org);
448*4e366538SXin Li memset(ch_org, 0, total_size);
449*4e366538SXin Li
450*4e366538SXin Li if (0 != libyuv::MJPGToI420(ch_jpeg, bytes_org, ch_org, image_width,
451*4e366538SXin Li ch_org + y_size, (image_width + 1) / 2,
452*4e366538SXin Li ch_org + y_size + uv_size,
453*4e366538SXin Li (image_width + 1) / 2, image_width,
454*4e366538SXin Li image_height, image_width, image_height)) {
455*4e366538SXin Li delete[] ch_jpeg;
456*4e366538SXin Li break;
457*4e366538SXin Li }
458*4e366538SXin Li delete[] ch_jpeg;
459*4e366538SXin Li #else
460*4e366538SXin Li break;
461*4e366538SXin Li #endif // HAVE_JPEG
462*4e366538SXin Li }
463*4e366538SXin Li
464*4e366538SXin Li for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
465*4e366538SXin Li size_t bytes_rec =
466*4e366538SXin Li fread(ch_rec, sizeof(uint8_t), total_size, file_rec[cur_rec]);
467*4e366538SXin Li if (bytes_rec < total_size) {
468*4e366538SXin Li #ifdef HAVE_JPEG
469*4e366538SXin Li // Try parsing file as a jpeg.
470*4e366538SXin Li uint8_t* const ch_jpeg = new uint8_t[bytes_rec];
471*4e366538SXin Li memcpy(ch_jpeg, ch_rec, bytes_rec);
472*4e366538SXin Li memset(ch_rec, 0, total_size);
473*4e366538SXin Li
474*4e366538SXin Li if (0 != libyuv::MJPGToI420(ch_jpeg, bytes_rec, ch_rec, image_width,
475*4e366538SXin Li ch_rec + y_size, (image_width + 1) / 2,
476*4e366538SXin Li ch_rec + y_size + uv_size,
477*4e366538SXin Li (image_width + 1) / 2, image_width,
478*4e366538SXin Li image_height, image_width, image_height)) {
479*4e366538SXin Li delete[] ch_jpeg;
480*4e366538SXin Li break;
481*4e366538SXin Li }
482*4e366538SXin Li delete[] ch_jpeg;
483*4e366538SXin Li #else
484*4e366538SXin Li break;
485*4e366538SXin Li #endif // HAVE_JPEG
486*4e366538SXin Li }
487*4e366538SXin Li
488*4e366538SXin Li if (verbose) {
489*4e366538SXin Li printf("%5d", number_of_frames);
490*4e366538SXin Li }
491*4e366538SXin Li if (do_psnr) {
492*4e366538SXin Li metric distorted_frame = {};
493*4e366538SXin Li metric* cur_distortion_psnr = &distortion_psnr[cur_rec];
494*4e366538SXin Li bool ismin = UpdateMetrics(ch_org, ch_rec, y_size, uv_size, total_size,
495*4e366538SXin Li number_of_frames, cur_distortion_psnr,
496*4e366538SXin Li &distorted_frame, true);
497*4e366538SXin Li if (verbose) {
498*4e366538SXin Li printf("\t%10.6f", distorted_frame.y);
499*4e366538SXin Li printf("\t%10.6f", distorted_frame.u);
500*4e366538SXin Li printf("\t%10.6f", distorted_frame.v);
501*4e366538SXin Li printf("\t%10.6f", distorted_frame.all);
502*4e366538SXin Li printf("\t%5s", ismin ? "min" : "");
503*4e366538SXin Li }
504*4e366538SXin Li }
505*4e366538SXin Li if (do_ssim) {
506*4e366538SXin Li metric distorted_frame = {};
507*4e366538SXin Li metric* cur_distortion_ssim = &distortion_ssim[cur_rec];
508*4e366538SXin Li bool ismin = UpdateMetrics(ch_org, ch_rec, y_size, uv_size, total_size,
509*4e366538SXin Li number_of_frames, cur_distortion_ssim,
510*4e366538SXin Li &distorted_frame, false);
511*4e366538SXin Li if (verbose) {
512*4e366538SXin Li printf("\t%10.6f", distorted_frame.y);
513*4e366538SXin Li printf("\t%10.6f", distorted_frame.u);
514*4e366538SXin Li printf("\t%10.6f", distorted_frame.v);
515*4e366538SXin Li printf("\t%10.6f", distorted_frame.all);
516*4e366538SXin Li printf("\t%5s", ismin ? "min" : "");
517*4e366538SXin Li }
518*4e366538SXin Li }
519*4e366538SXin Li if (verbose) {
520*4e366538SXin Li if (show_name) {
521*4e366538SXin Li printf("\t%s", argv[fileindex_rec + cur_rec]);
522*4e366538SXin Li }
523*4e366538SXin Li printf("\n");
524*4e366538SXin Li }
525*4e366538SXin Li }
526*4e366538SXin Li }
527*4e366538SXin Li
528*4e366538SXin Li // Final PSNR computation.
529*4e366538SXin Li for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
530*4e366538SXin Li metric* cur_distortion_psnr = &distortion_psnr[cur_rec];
531*4e366538SXin Li metric* cur_distortion_ssim = &distortion_ssim[cur_rec];
532*4e366538SXin Li if (number_of_frames > 0) {
533*4e366538SXin Li const double norm = 1. / static_cast<double>(number_of_frames);
534*4e366538SXin Li cur_distortion_psnr->y *= norm;
535*4e366538SXin Li cur_distortion_psnr->u *= norm;
536*4e366538SXin Li cur_distortion_psnr->v *= norm;
537*4e366538SXin Li cur_distortion_psnr->all *= norm;
538*4e366538SXin Li cur_distortion_ssim->y *= norm;
539*4e366538SXin Li cur_distortion_ssim->u *= norm;
540*4e366538SXin Li cur_distortion_ssim->v *= norm;
541*4e366538SXin Li cur_distortion_ssim->all *= norm;
542*4e366538SXin Li }
543*4e366538SXin Li
544*4e366538SXin Li if (do_psnr) {
545*4e366538SXin Li const double global_psnr_y =
546*4e366538SXin Li ComputePSNR(cur_distortion_psnr->global_y,
547*4e366538SXin Li static_cast<double>(y_size) * number_of_frames);
548*4e366538SXin Li const double global_psnr_u =
549*4e366538SXin Li ComputePSNR(cur_distortion_psnr->global_u,
550*4e366538SXin Li static_cast<double>(uv_size) * number_of_frames);
551*4e366538SXin Li const double global_psnr_v =
552*4e366538SXin Li ComputePSNR(cur_distortion_psnr->global_v,
553*4e366538SXin Li static_cast<double>(uv_size) * number_of_frames);
554*4e366538SXin Li const double global_psnr_all =
555*4e366538SXin Li ComputePSNR(cur_distortion_psnr->global_all,
556*4e366538SXin Li static_cast<double>(total_size) * number_of_frames);
557*4e366538SXin Li printf("Global:\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%5d", global_psnr_y,
558*4e366538SXin Li global_psnr_u, global_psnr_v, global_psnr_all, number_of_frames);
559*4e366538SXin Li if (show_name) {
560*4e366538SXin Li printf("\t%s", argv[fileindex_rec + cur_rec]);
561*4e366538SXin Li }
562*4e366538SXin Li printf("\n");
563*4e366538SXin Li }
564*4e366538SXin Li
565*4e366538SXin Li if (!quiet) {
566*4e366538SXin Li printf("Avg:");
567*4e366538SXin Li if (do_psnr) {
568*4e366538SXin Li printf("\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%5d", cur_distortion_psnr->y,
569*4e366538SXin Li cur_distortion_psnr->u, cur_distortion_psnr->v,
570*4e366538SXin Li cur_distortion_psnr->all, number_of_frames);
571*4e366538SXin Li }
572*4e366538SXin Li if (do_ssim) {
573*4e366538SXin Li printf("\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%5d", cur_distortion_ssim->y,
574*4e366538SXin Li cur_distortion_ssim->u, cur_distortion_ssim->v,
575*4e366538SXin Li cur_distortion_ssim->all, number_of_frames);
576*4e366538SXin Li }
577*4e366538SXin Li if (show_name) {
578*4e366538SXin Li printf("\t%s", argv[fileindex_rec + cur_rec]);
579*4e366538SXin Li }
580*4e366538SXin Li printf("\n");
581*4e366538SXin Li }
582*4e366538SXin Li if (!quiet) {
583*4e366538SXin Li printf("Min:");
584*4e366538SXin Li if (do_psnr) {
585*4e366538SXin Li printf("\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%5d",
586*4e366538SXin Li cur_distortion_psnr->min_y, cur_distortion_psnr->min_u,
587*4e366538SXin Li cur_distortion_psnr->min_v, cur_distortion_psnr->min_all,
588*4e366538SXin Li cur_distortion_psnr->min_frame);
589*4e366538SXin Li }
590*4e366538SXin Li if (do_ssim) {
591*4e366538SXin Li printf("\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%5d",
592*4e366538SXin Li cur_distortion_ssim->min_y, cur_distortion_ssim->min_u,
593*4e366538SXin Li cur_distortion_ssim->min_v, cur_distortion_ssim->min_all,
594*4e366538SXin Li cur_distortion_ssim->min_frame);
595*4e366538SXin Li }
596*4e366538SXin Li if (show_name) {
597*4e366538SXin Li printf("\t%s", argv[fileindex_rec + cur_rec]);
598*4e366538SXin Li }
599*4e366538SXin Li printf("\n");
600*4e366538SXin Li }
601*4e366538SXin Li
602*4e366538SXin Li if (do_mse) {
603*4e366538SXin Li double global_mse_y =
604*4e366538SXin Li GetMSE(cur_distortion_psnr->global_y,
605*4e366538SXin Li static_cast<double>(y_size) * number_of_frames);
606*4e366538SXin Li double global_mse_u =
607*4e366538SXin Li GetMSE(cur_distortion_psnr->global_u,
608*4e366538SXin Li static_cast<double>(uv_size) * number_of_frames);
609*4e366538SXin Li double global_mse_v =
610*4e366538SXin Li GetMSE(cur_distortion_psnr->global_v,
611*4e366538SXin Li static_cast<double>(uv_size) * number_of_frames);
612*4e366538SXin Li double global_mse_all =
613*4e366538SXin Li GetMSE(cur_distortion_psnr->global_all,
614*4e366538SXin Li static_cast<double>(total_size) * number_of_frames);
615*4e366538SXin Li printf("MSE:\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%5d", global_mse_y,
616*4e366538SXin Li global_mse_u, global_mse_v, global_mse_all, number_of_frames);
617*4e366538SXin Li if (show_name) {
618*4e366538SXin Li printf("\t%s", argv[fileindex_rec + cur_rec]);
619*4e366538SXin Li }
620*4e366538SXin Li printf("\n");
621*4e366538SXin Li }
622*4e366538SXin Li }
623*4e366538SXin Li fclose(file_org);
624*4e366538SXin Li for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
625*4e366538SXin Li fclose(file_rec[cur_rec]);
626*4e366538SXin Li }
627*4e366538SXin Li delete[] distortion_psnr;
628*4e366538SXin Li delete[] distortion_ssim;
629*4e366538SXin Li delete[] ch_org;
630*4e366538SXin Li delete[] ch_rec;
631*4e366538SXin Li delete[] file_rec;
632*4e366538SXin Li return 0;
633*4e366538SXin Li }
634