Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / cast / test / utility / video_utility.cc
1 // Copyright 2014 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 "media/cast/test/utility/video_utility.h"
6
7 #include <math.h>
8 #include <cstdio>
9
10 #include "third_party/libyuv/include/libyuv/compare.h"
11 #include "ui/gfx/size.h"
12
13 namespace media {
14 namespace cast {
15
16 double I420PSNR(const scoped_refptr<media::VideoFrame>& frame1,
17                 const scoped_refptr<media::VideoFrame>& frame2) {
18   if (frame1->coded_size().width() != frame2->coded_size().width() ||
19       frame1->coded_size().height() != frame2->coded_size().height())
20     return -1;
21
22   return libyuv::I420Psnr(frame1->data(VideoFrame::kYPlane),
23                           frame1->stride(VideoFrame::kYPlane),
24                           frame1->data(VideoFrame::kUPlane),
25                           frame1->stride(VideoFrame::kUPlane),
26                           frame1->data(VideoFrame::kVPlane),
27                           frame1->stride(VideoFrame::kVPlane),
28                           frame2->data(VideoFrame::kYPlane),
29                           frame2->stride(VideoFrame::kYPlane),
30                           frame2->data(VideoFrame::kUPlane),
31                           frame2->stride(VideoFrame::kUPlane),
32                           frame2->data(VideoFrame::kVPlane),
33                           frame2->stride(VideoFrame::kVPlane),
34                           frame1->coded_size().width(),
35                           frame1->coded_size().height());
36 }
37
38 void PopulateVideoFrame(VideoFrame* frame, int start_value) {
39   int height = frame->coded_size().height();
40   int stride_y = frame->stride(VideoFrame::kYPlane);
41   int stride_u = frame->stride(VideoFrame::kUPlane);
42   int stride_v = frame->stride(VideoFrame::kVPlane);
43   int half_height = (height + 1) / 2;
44   uint8* y_plane = frame->data(VideoFrame::kYPlane);
45   uint8* u_plane = frame->data(VideoFrame::kUPlane);
46   uint8* v_plane = frame->data(VideoFrame::kVPlane);
47
48   // Set Y.
49   for (int j = 0; j < height; ++j)
50     for (int i = 0; i < stride_y; ++i) {
51       *y_plane = static_cast<uint8>(start_value + i + j);
52       ++y_plane;
53     }
54
55   // Set U.
56   for (int j = 0; j < half_height; ++j)
57     for (int i = 0; i < stride_u; ++i) {
58       *u_plane = static_cast<uint8>(start_value + i + j);
59       ++u_plane;
60     }
61
62   // Set V.
63   for (int j = 0; j < half_height; ++j)
64     for (int i = 0; i < stride_v; ++i) {
65       *v_plane = static_cast<uint8>(start_value + i + j);
66       ++v_plane;
67     }
68 }
69
70 bool PopulateVideoFrameFromFile(VideoFrame* frame, FILE* video_file) {
71   int width = frame->coded_size().width();
72   int height = frame->coded_size().height();
73   int half_width = (width + 1) / 2;
74   int half_height = (height + 1) / 2;
75   size_t frame_size = width * height + 2 * half_width * half_height;
76   uint8* y_plane = frame->data(VideoFrame::kYPlane);
77   uint8* u_plane = frame->data(VideoFrame::kUPlane);
78   uint8* v_plane = frame->data(VideoFrame::kVPlane);
79
80   uint8* raw_data = new uint8[frame_size];
81   size_t count = fread(raw_data, 1, frame_size, video_file);
82   if (count != frame_size)
83     return false;
84
85   memcpy(y_plane, raw_data, width * height);
86   memcpy(u_plane, raw_data + width * height, half_width * half_height);
87   memcpy(v_plane,
88          raw_data + width * height + half_width * half_height,
89          half_width * half_height);
90   delete[] raw_data;
91   return true;
92 }
93
94 }  // namespace cast
95 }  // namespace media