summaryrefslogtreecommitdiff
path: root/media/cast/test/video_utility.cc
diff options
context:
space:
mode:
Diffstat (limited to 'media/cast/test/video_utility.cc')
-rw-r--r--media/cast/test/video_utility.cc113
1 files changed, 68 insertions, 45 deletions
diff --git a/media/cast/test/video_utility.cc b/media/cast/test/video_utility.cc
index 156329a305..30f00193dd 100644
--- a/media/cast/test/video_utility.cc
+++ b/media/cast/test/video_utility.cc
@@ -3,8 +3,12 @@
// found in the LICENSE file.
#include <math.h>
+#include <cstdio>
+#include "media/base/video_frame.h"
#include "media/cast/test/video_utility.h"
+#include "third_party/libyuv/include/libyuv/compare.h"
+#include "ui/gfx/size.h"
namespace media {
namespace cast {
@@ -12,55 +16,52 @@ namespace cast {
double I420PSNR(const I420VideoFrame& frame1, const I420VideoFrame& frame2) {
// Frames should have equal resolution.
if (frame1.width != frame2.width || frame1.height != frame2.height) return -1;
+ return libyuv::I420Psnr(frame1.y_plane.data, frame1.y_plane.stride,
+ frame1.u_plane.data, frame1.u_plane.stride,
+ frame1.v_plane.data, frame1.v_plane.stride,
+ frame2.y_plane.data, frame2.y_plane.stride,
+ frame2.u_plane.data, frame2.u_plane.stride,
+ frame2.v_plane.data, frame2.v_plane.stride,
+ frame1.width, frame1.height);
+}
- double y_mse = 0.0;
- // Y.
- uint8* data1 = frame1.y_plane.data;
- uint8* data2 = frame2.y_plane.data;
- for (int i = 0; i < frame1.height; ++i) {
- for (int j = 0; j < frame1.width; ++j) {
- y_mse += (data1[j] - data2[j]) * (data1[j] - data2[j]);
- }
- // Account for stride.
- data1 += frame1.y_plane.stride;
- data2 += frame2.y_plane.stride;
- }
- y_mse /= (frame1.width * frame1.height);
-
- int half_width = (frame1.width + 1) / 2;
- int half_height = (frame1.height + 1) / 2;
- // U.
- double u_mse = 0.0;
- data1 = frame1.u_plane.data;
- data2 = frame2.u_plane.data;
- for (int i = 0; i < half_height; ++i) {
- for (int j = 0; j < half_width; ++j) {
- u_mse += (data1[j] - data2[j]) * (data1[j] - data2[j]);
- }
- // Account for stride.
- data1 += frame1.u_plane.stride;
- data2 += frame2.u_plane.stride;
+double I420PSNR(const VideoFrame& frame1, const I420VideoFrame& frame2) {
+ if (frame1.coded_size().width() != frame2.width ||
+ frame1.coded_size().height() != frame2.height) return -1;
+
+ return libyuv::I420Psnr(
+ frame1.data(VideoFrame::kYPlane), frame1.stride(VideoFrame::kYPlane),
+ frame1.data(VideoFrame::kUPlane), frame1.stride(VideoFrame::kUPlane),
+ frame1.data(VideoFrame::kVPlane), frame1.stride(VideoFrame::kVPlane),
+ frame2.y_plane.data, frame2.y_plane.stride,
+ frame2.u_plane.data, frame2.u_plane.stride,
+ frame2.v_plane.data, frame2.v_plane.stride,
+ frame2.width, frame2.height);
+}
+
+void PopulateVideoFrame(VideoFrame* frame, int start_value) {
+ int width = frame->coded_size().width();
+ int height = frame->coded_size().height();
+ int half_width = (width + 1) / 2;
+ int half_height = (height + 1) / 2;
+ uint8* y_plane = frame->data(VideoFrame::kYPlane);
+ uint8* u_plane = frame->data(VideoFrame::kUPlane);
+ uint8* v_plane = frame->data(VideoFrame::kVPlane);
+
+ // Set Y.
+ for (int i = 0; i < width * height; ++i) {
+ y_plane[i] = static_cast<uint8>(start_value + i);
}
- u_mse /= half_width * half_height;
-
- // V.
- double v_mse = 0.0;
- data1 = frame1.v_plane.data;
- data2 = frame2.v_plane.data;
- for (int i = 0; i < half_height; ++i) {
- for (int j = 0; j < half_width; ++j) {
- v_mse += (data1[j] - data2[j]) * (data1[j] - data2[j]);
- }
- // Account for stride.
- data1 += frame1.v_plane.stride;
- data2 += frame2.v_plane.stride;
+
+ // Set U.
+ for (int i = 0; i < half_width * half_height; ++i) {
+ u_plane[i] = static_cast<uint8>(start_value + i);
}
- v_mse /= half_width * half_height;
- // Combine to one psnr value.
- static const double kVideoBitRange = 255.0;
- return 20.0 * log10(kVideoBitRange) -
- 10.0 * log10((y_mse + u_mse + v_mse) / 3.0);
+ // Set V.
+ for (int i = 0; i < half_width * half_height; ++i) {
+ v_plane[i] = static_cast<uint8>(start_value + i);
+ }
}
void PopulateVideoFrame(I420VideoFrame* frame, int start_value) {
@@ -94,5 +95,27 @@ void PopulateVideoFrame(I420VideoFrame* frame, int start_value) {
}
}
+bool PopulateVideoFrameFromFile(VideoFrame* frame, FILE* video_file) {
+ int width = frame->coded_size().width();
+ int height = frame->coded_size().height();
+ int half_width = (width + 1) / 2;
+ int half_height = (height + 1) / 2;
+ size_t frame_size = width * height + 2 * half_width * half_height;
+ uint8* y_plane = frame->data(VideoFrame::kYPlane);
+ uint8* u_plane = frame->data(VideoFrame::kUPlane);
+ uint8* v_plane = frame->data(VideoFrame::kVPlane);
+
+ uint8* raw_data = new uint8[frame_size];
+ size_t count = fread(raw_data, 1, frame_size, video_file);
+ if (count != frame_size) return false;
+
+ memcpy(y_plane, raw_data, width * height);
+ memcpy(u_plane, raw_data + width * height, half_width * half_height);
+ memcpy(v_plane, raw_data + width * height +
+ half_width * half_height, half_width * half_height);
+ delete [] raw_data;
+ return true;
+}
+
} // namespace cast
} // namespace media