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