Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / cast / test / utility / video_utility.cc
blob8c4b0cbd9edcd3822a7ec157735c4f61923b67f5
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.
5 #include "media/cast/test/utility/video_utility.h"
7 #include <math.h>
8 #include <cstdio>
10 #include "base/rand_util.h"
11 #include "third_party/libyuv/include/libyuv/compare.h"
12 #include "ui/gfx/geometry/size.h"
14 namespace media {
15 namespace cast {
17 double I420PSNR(const scoped_refptr<media::VideoFrame>& frame1,
18 const scoped_refptr<media::VideoFrame>& frame2) {
19 if (frame1->coded_size().width() != frame2->coded_size().width() ||
20 frame1->coded_size().height() != frame2->coded_size().height())
21 return -1;
23 return libyuv::I420Psnr(frame1->data(VideoFrame::kYPlane),
24 frame1->stride(VideoFrame::kYPlane),
25 frame1->data(VideoFrame::kUPlane),
26 frame1->stride(VideoFrame::kUPlane),
27 frame1->data(VideoFrame::kVPlane),
28 frame1->stride(VideoFrame::kVPlane),
29 frame2->data(VideoFrame::kYPlane),
30 frame2->stride(VideoFrame::kYPlane),
31 frame2->data(VideoFrame::kUPlane),
32 frame2->stride(VideoFrame::kUPlane),
33 frame2->data(VideoFrame::kVPlane),
34 frame2->stride(VideoFrame::kVPlane),
35 frame1->coded_size().width(),
36 frame1->coded_size().height());
39 double I420SSIM(const scoped_refptr<media::VideoFrame>& frame1,
40 const scoped_refptr<media::VideoFrame>& frame2) {
41 if (frame1->coded_size().width() != frame2->coded_size().width() ||
42 frame1->coded_size().height() != frame2->coded_size().height())
43 return -1;
45 return libyuv::I420Ssim(frame1->data(VideoFrame::kYPlane),
46 frame1->stride(VideoFrame::kYPlane),
47 frame1->data(VideoFrame::kUPlane),
48 frame1->stride(VideoFrame::kUPlane),
49 frame1->data(VideoFrame::kVPlane),
50 frame1->stride(VideoFrame::kVPlane),
51 frame2->data(VideoFrame::kYPlane),
52 frame2->stride(VideoFrame::kYPlane),
53 frame2->data(VideoFrame::kUPlane),
54 frame2->stride(VideoFrame::kUPlane),
55 frame2->data(VideoFrame::kVPlane),
56 frame2->stride(VideoFrame::kVPlane),
57 frame1->coded_size().width(),
58 frame1->coded_size().height());
61 void PopulateVideoFrame(VideoFrame* frame, int start_value) {
62 const gfx::Size frame_size = frame->coded_size();
63 const int stripe_size =
64 std::max(32, std::min(frame_size.width(), frame_size.height()) / 8) & -2;
66 // Set Y.
67 const int height = frame_size.height();
68 const int stride_y = frame->stride(VideoFrame::kYPlane);
69 uint8* y_plane = frame->data(VideoFrame::kYPlane);
70 for (int j = 0; j < height; ++j) {
71 const int stripe_j = (j / stripe_size) * stripe_size;
72 for (int i = 0; i < stride_y; ++i) {
73 const int stripe_i = (i / stripe_size) * stripe_size;
74 *y_plane = static_cast<uint8>(start_value + stripe_i + stripe_j);
75 ++y_plane;
79 const int half_height = (height + 1) / 2;
80 #if defined(OS_MACOSX)
81 if (frame->format() == VideoFrame::NV12) {
82 const int stride_uv = frame->stride(VideoFrame::kUVPlane);
83 uint8* uv_plane = frame->data(VideoFrame::kUVPlane);
85 // Set U and V.
86 for (int j = 0; j < half_height; ++j) {
87 const int stripe_j = (j / stripe_size) * stripe_size;
88 for (int i = 0; i < stride_uv; i += 2) {
89 const int stripe_i = (i / stripe_size) * stripe_size;
90 *uv_plane = *(uv_plane + 1) =
91 static_cast<uint8>(start_value + stripe_i + stripe_j);
92 uv_plane += 2;
95 } else
96 #endif
98 DCHECK(frame->format() == VideoFrame::I420 ||
99 frame->format() == VideoFrame::YV12);
100 const int stride_u = frame->stride(VideoFrame::kUPlane);
101 const int stride_v = frame->stride(VideoFrame::kVPlane);
102 uint8* u_plane = frame->data(VideoFrame::kUPlane);
103 uint8* v_plane = frame->data(VideoFrame::kVPlane);
105 // Set U.
106 for (int j = 0; j < half_height; ++j) {
107 const int stripe_j = (j / stripe_size) * stripe_size;
108 for (int i = 0; i < stride_u; ++i) {
109 const int stripe_i = (i / stripe_size) * stripe_size;
110 *u_plane = static_cast<uint8>(start_value + stripe_i + stripe_j);
111 ++u_plane;
115 // Set V.
116 for (int j = 0; j < half_height; ++j) {
117 const int stripe_j = (j / stripe_size) * stripe_size;
118 for (int i = 0; i < stride_v; ++i) {
119 const int stripe_i = (i / stripe_size) * stripe_size;
120 *v_plane = static_cast<uint8>(start_value + stripe_i + stripe_j);
121 ++v_plane;
127 void PopulateVideoFrameWithNoise(VideoFrame* frame) {
128 const int height = frame->coded_size().height();
129 const int stride_y = frame->stride(VideoFrame::kYPlane);
130 const int stride_u = frame->stride(VideoFrame::kUPlane);
131 const int stride_v = frame->stride(VideoFrame::kVPlane);
132 const int half_height = (height + 1) / 2;
133 uint8* const y_plane = frame->data(VideoFrame::kYPlane);
134 uint8* const u_plane = frame->data(VideoFrame::kUPlane);
135 uint8* const v_plane = frame->data(VideoFrame::kVPlane);
137 base::RandBytes(y_plane, height * stride_y);
138 base::RandBytes(u_plane, half_height * stride_u);
139 base::RandBytes(v_plane, half_height * stride_v);
142 bool PopulateVideoFrameFromFile(VideoFrame* frame, FILE* video_file) {
143 const int width = frame->coded_size().width();
144 const int height = frame->coded_size().height();
145 const int half_width = (width + 1) / 2;
146 const int half_height = (height + 1) / 2;
147 const size_t frame_size = width * height + 2 * half_width * half_height;
148 uint8* const y_plane = frame->data(VideoFrame::kYPlane);
149 uint8* const u_plane = frame->data(VideoFrame::kUPlane);
150 uint8* const v_plane = frame->data(VideoFrame::kVPlane);
152 uint8* const raw_data = new uint8[frame_size];
153 const size_t count = fread(raw_data, 1, frame_size, video_file);
154 if (count != frame_size)
155 return false;
157 memcpy(y_plane, raw_data, width * height);
158 memcpy(u_plane, raw_data + width * height, half_width * half_height);
159 memcpy(v_plane,
160 raw_data + width * height + half_width * half_height,
161 half_width * half_height);
162 delete[] raw_data;
163 return true;
166 } // namespace cast
167 } // namespace media