Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / media / cast / test / video_utility.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <math.h>
6 #include <cstdio>
7
8 #include "media/cast/test/video_utility.h"
9 #include "third_party/libyuv/include/libyuv/compare.h"
10 #include "ui/gfx/size.h"
11
12 namespace media {
13 namespace cast {
14
15 double I420PSNR(const scoped_refptr<media::VideoFrame>& frame1,
16                 const scoped_refptr<media::VideoFrame>& frame2) {
17   if (frame1->coded_size().width() != frame2->coded_size().width() ||
18       frame1->coded_size().height() != frame2->coded_size().height()) return -1;
19
20   return libyuv::I420Psnr(
21       frame1->data(VideoFrame::kYPlane), frame1->stride(VideoFrame::kYPlane),
22       frame1->data(VideoFrame::kUPlane), frame1->stride(VideoFrame::kUPlane),
23       frame1->data(VideoFrame::kVPlane), frame1->stride(VideoFrame::kVPlane),
24       frame2->data(VideoFrame::kYPlane), frame2->stride(VideoFrame::kYPlane),
25       frame2->data(VideoFrame::kUPlane), frame2->stride(VideoFrame::kUPlane),
26       frame2->data(VideoFrame::kVPlane), frame2->stride(VideoFrame::kVPlane),
27       frame1->coded_size().width(), frame1->coded_size().height());
28 }
29
30 void PopulateVideoFrame(VideoFrame* frame, int start_value) {
31   int width = frame->coded_size().width();
32   int height = frame->coded_size().height();
33   int half_width = (width + 1) / 2;
34   int half_height = (height + 1) / 2;
35   uint8* y_plane = frame->data(VideoFrame::kYPlane);
36   uint8* u_plane = frame->data(VideoFrame::kUPlane);
37   uint8* v_plane = frame->data(VideoFrame::kVPlane);
38
39   // Set Y.
40   for (int i = 0; i < width * height; ++i) {
41     y_plane[i] = static_cast<uint8>(start_value + i);
42   }
43
44   // Set U.
45   for (int i = 0; i < half_width * half_height; ++i) {
46     u_plane[i] = static_cast<uint8>(start_value + i);
47   }
48
49   // Set V.
50   for (int i = 0; i < half_width * half_height; ++i) {
51     v_plane[i] = static_cast<uint8>(start_value + i);
52   }
53 }
54
55 bool PopulateVideoFrameFromFile(VideoFrame* frame, FILE* video_file) {
56   int width = frame->coded_size().width();
57   int height = frame->coded_size().height();
58   int half_width = (width + 1) / 2;
59   int half_height = (height + 1) / 2;
60   size_t frame_size = width * height + 2 * half_width * half_height;
61   uint8* y_plane = frame->data(VideoFrame::kYPlane);
62   uint8* u_plane = frame->data(VideoFrame::kUPlane);
63   uint8* v_plane = frame->data(VideoFrame::kVPlane);
64
65   uint8* raw_data = new uint8[frame_size];
66   size_t count = fread(raw_data, 1, frame_size, video_file);
67   if (count != frame_size) return false;
68
69   memcpy(y_plane, raw_data, width * height);
70   memcpy(u_plane, raw_data + width * height, half_width * half_height);
71   memcpy(v_plane, raw_data + width * height +
72       half_width * half_height, half_width * half_height);
73   delete [] raw_data;
74   return true;
75 }
76
77 }  // namespace cast
78 }  // namespace media