move Calc functions for psnr into header to avoid duplicate links.
BUG=339
TESTED=gyp build
R=harryjin@google.com

Review URL: https://webrtc-codereview.appspot.com/16769004

git-svn-id: http://libyuv.googlecode.com/svn/trunk@1022 16f28f9a-4ce2-e073-06de-1de4eb20be90
diff --git a/README.chromium b/README.chromium
index 0a150d4..9430208 100644
--- a/README.chromium
+++ b/README.chromium
@@ -1,6 +1,6 @@
 Name: libyuv
 URL: http://code.google.com/p/libyuv/
-Version: 1021
+Version: 1022
 License: BSD
 License File: LICENSE
 
diff --git a/include/libyuv/version.h b/include/libyuv/version.h
index 4053fd0..efe2879 100644
--- a/include/libyuv/version.h
+++ b/include/libyuv/version.h
@@ -11,6 +11,6 @@
 #ifndef INCLUDE_LIBYUV_VERSION_H_  // NOLINT
 #define INCLUDE_LIBYUV_VERSION_H_
 
-#define LIBYUV_VERSION 1021
+#define LIBYUV_VERSION 1022
 
 #endif  // INCLUDE_LIBYUV_VERSION_H_  NOLINT
diff --git a/util/Makefile b/util/Makefile
index be6de35..6044d2a 100644
--- a/util/Makefile
+++ b/util/Makefile
@@ -1,6 +1,6 @@
-psnr: psnr.cc ssim.cc psnr_main.cc

-ifeq ($(CXX),icl)

-	$(CXX) /arch:SSE2 /Ox /openmp psnr.cc ssim.cc psnr_main.cc

-else

-	$(CXX) -msse2 -O3 -fopenmp -static -o psnr psnr.cc ssim.cc psnr_main.cc -Wl,--strip-all

-endif

+psnr: psnr.cc ssim.cc psnr_main.cc
+ifeq ($(CXX),icl)
+	$(CXX) /arch:SSE2 /Ox /openmp psnr.cc ssim.cc psnr_main.cc
+else
+	$(CXX) -msse2 -O3 -fopenmp -static -o psnr psnr.cc ssim.cc psnr_main.cc -Wl,--strip-all
+endif
diff --git a/util/psnr.cc b/util/psnr.cc
index e8fd16a..77a2f9f 100644
--- a/util/psnr.cc
+++ b/util/psnr.cc
@@ -10,8 +10,6 @@
 
 #include "./psnr.h"  // NOLINT
 
-#include <math.h>
-
 #ifdef _OPENMP
 #include <omp.h>
 #endif
@@ -34,14 +32,6 @@
 #endif  // __LP64__
 #endif  // _MSC_VER
 
-// PSNR formula: psnr = 10 * log10 (Peak Signal^2 * size / sse)
-double ComputePSNR(double sse, double size) {
-  const double kMINSSE = 255.0 * 255.0 * size / pow(10., kMaxPSNR / 10.);
-  if (sse <= kMINSSE)
-    sse = kMINSSE;  // Produces max PSNR of 128
-  return 10.0 * log10(65025.0 * size / sse);
-}
-
 #if !defined(LIBYUV_DISABLE_NEON) && defined(__ARM_NEON__)
 #define HAS_SUMSQUAREERROR_NEON
 static uint32 SumSquareError_NEON(const uint8* src_a,
diff --git a/util/psnr.h b/util/psnr.h
index 370337a..8254266 100644
--- a/util/psnr.h
+++ b/util/psnr.h
@@ -13,6 +13,8 @@
 #ifndef UTIL_PSNR_H_  // NOLINT
 #define UTIL_PSNR_H_
 
+#include <math.h>  // For log10()
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -24,14 +26,20 @@
 
 static const double kMaxPSNR = 128.0;
 
-// PSNR formula: psnr = 10 * log10 (Peak Signal^2 * size / sse).
+// PSNR formula: psnr = 10 * log10 (Peak Signal^2 * size / sse)
 // Returns 128.0 (kMaxPSNR) if sse is 0 (perfect match).
-double ComputePSNR(double sse, double size);
+static double ComputePSNR(double sse, double size) {
+  const double kMINSSE = 255.0 * 255.0 * size / pow(10.0, kMaxPSNR / 10.0);
+  if (sse <= kMINSSE)
+    sse = kMINSSE;  // Produces max PSNR of 128
+  return 10.0 * log10(255.0 * 255.0 * size / sse);
+}
 
 // Computer Sum of Squared Error (SSE).
 // Pass this to ComputePSNR for final result.
 double ComputeSumSquareError(const uint8* org, const uint8* rec, int size);
 
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/util/psnr_main.cc b/util/psnr_main.cc
index ae8ee01..c29610b 100644
--- a/util/psnr_main.cc
+++ b/util/psnr_main.cc
@@ -256,10 +256,10 @@
                                        static_cast<double>(total_size));
   } else {
     distorted_frame->y = CalcSSIM(ch_org, ch_rec, image_width, image_height);
-    distorted_frame->u = CalcSSIM(u_org, u_rec, image_width / 2,
-                                 image_height / 2);
-    distorted_frame->v = CalcSSIM(v_org, v_rec, image_width / 2,
-                                 image_height / 2);
+    distorted_frame->u = CalcSSIM(u_org, u_rec, (image_width + 1) / 2,
+                                 (image_height + 1) / 2);
+    distorted_frame->v = CalcSSIM(v_org, v_rec, (image_width + 1) / 2,
+                                 (image_height + 1) / 2);
     distorted_frame->all =
       (distorted_frame->y + distorted_frame->u + distorted_frame->v)
         / total_size;
@@ -417,11 +417,12 @@
       // Try parsing file as a jpeg.
       uint8* const ch_jpeg = new uint8[bytes_org];
       memcpy(ch_jpeg, ch_org, bytes_org);
+      memset(ch_org, 0, total_size);
 
       if (0 != libyuv::MJPGToI420(ch_jpeg, bytes_org,
                                   ch_org, image_width,
-                                  ch_org + y_size, image_width / 2,
-                                  ch_org + y_size + uv_size, image_width / 2,
+                                  ch_org + y_size, (image_width + 1) / 2,
+                                  ch_org + y_size + uv_size, (image_width + 1) / 2,
                                   image_width, image_height,
                                   image_width, image_height)) {
         delete[] ch_jpeg;
@@ -441,11 +442,12 @@
         // Try parsing file as a jpeg.
         uint8* const ch_jpeg = new uint8[bytes_rec];
         memcpy(ch_jpeg, ch_rec, bytes_rec);
+        memset(ch_rec, 0, total_size);
 
         if (0 != libyuv::MJPGToI420(ch_jpeg, bytes_rec,
                                     ch_rec, image_width,
-                                    ch_rec + y_size, image_width / 2,
-                                    ch_rec + y_size + uv_size, image_width / 2,
+                                    ch_rec + y_size, (image_width + 1) / 2,
+                                    ch_rec + y_size + uv_size, (image_width + 1) / 2,
                                     image_width, image_height,
                                     image_width, image_height)) {
           delete[] ch_jpeg;
diff --git a/util/ssim.cc b/util/ssim.cc
index d07889a..9da30df 100644
--- a/util/ssim.cc
+++ b/util/ssim.cc
@@ -10,7 +10,6 @@
 
 #include "../util/ssim.h"  // NOLINT
 
-#include <math.h>
 #include <string.h>
 
 #ifdef __cplusplus
@@ -327,10 +326,6 @@
   return SSIM;
 }
 
-double CalcLSSIM(double ssim) {
-  return -10.0 * log10(1.0 - ssim);
-}
-
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/util/ssim.h b/util/ssim.h
index 40120b4..52a8932 100644
--- a/util/ssim.h
+++ b/util/ssim.h
@@ -13,6 +13,8 @@
 #ifndef UTIL_SSIM_H_  // NOLINT
 #define UTIL_SSIM_H_
 
+#include <math.h>  // For log10()
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -25,8 +27,9 @@
 double CalcSSIM(const uint8* org, const uint8* rec,
                 const int image_width, const int image_height);
 
-// does -10.0 * log10(1.0 - ssim)
-double CalcLSSIM(double ssim);
+static double CalcLSSIM(double ssim) {
+  return -10.0 * log10(1.0 - ssim);
+}
 
 #ifdef __cplusplus
 }  // extern "C"