summaryrefslogtreecommitdiffstats
path: root/media/base/yuv_convert_unittest.cc
blob: 29dbeea44c9a57e370d4e4cf3022ad5540dd1a68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/base_paths.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "media/base/djb2.h"
#include "media/base/yuv_convert.h"
#include "media/base/yuv_row.h"
#include "testing/gtest/include/gtest/gtest.h"

// Reference images were created with the following steps
// ffmpeg -vframes 25 -i bali.mov -vcodec rawvideo -pix_fmt yuv420p -an
//   bali_1280x720_P420.yuv
// yuvhalf -yv12 -skip 24 bali_1280x720_P420.yuv bali_640x360_P420.yuv

// ffmpeg -vframes 25 -i bali.mov -vcodec rawvideo -pix_fmt yuv422p -an
//   bali_1280x720_P422.yuv
// yuvhalf -yv16 -skip 24 bali_1280x720_P422.yuv bali_640x360_P422.yuv
// Size of raw image.

// Size of raw image.
static const int kWidth = 640;
static const int kHeight = 360;
static const int kScaledWidth = 1024;
static const int kScaledHeight = 768;
static const int kBpp = 4;

// Surface sizes.
static const size_t kYUV12Size = kWidth * kHeight * 12 / 8;
static const size_t kYUV16Size = kWidth * kHeight * 16 / 8;
static const size_t kRGBSize = kWidth * kHeight * kBpp;
static const size_t kRGBSizeConverted = kWidth * kHeight * kBpp;

// Set to 100 to time ConvertYUVToRGB32.
static const int kTestTimes = 1;

TEST(YUVConvertTest, YV12) {
  // Allocate all surfaces.
  scoped_array<uint8> yuv_bytes(new uint8[kYUV12Size]);
  scoped_array<uint8> rgb_bytes(new uint8[kRGBSize]);
  scoped_array<uint8> rgb_converted_bytes(new uint8[kRGBSizeConverted]);

  // Read YUV reference data from file.
  FilePath yuv_url;
  EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &yuv_url));
  yuv_url = yuv_url.Append(FILE_PATH_LITERAL("media"))
                   .Append(FILE_PATH_LITERAL("test"))
                   .Append(FILE_PATH_LITERAL("data"))
                   .Append(FILE_PATH_LITERAL("bali_640x360_P420.yuv"));
  EXPECT_EQ(static_cast<int>(kYUV12Size),
            file_util::ReadFile(yuv_url,
                                reinterpret_cast<char*>(yuv_bytes.get()),
                                static_cast<int>(kYUV12Size)));

  for (int i = 0; i < kTestTimes; ++i) {
    // Convert a frame of YUV to 32 bit ARGB.
    media::ConvertYUVToRGB32(yuv_bytes.get(),                             // Y
                             yuv_bytes.get() + kWidth * kHeight,          // U
                             yuv_bytes.get() + kWidth * kHeight * 5 / 4,  // V
                             rgb_converted_bytes.get(),  // RGB output
                             kWidth, kHeight,            // Dimensions
                             kWidth,                     // YStride
                             kWidth / 2,                 // UVStride
                             kWidth * kBpp,              // RGBStride
                             media::YV12);
  }

  unsigned int rgb_hash = DJB2Hash(rgb_converted_bytes.get(), kRGBSizeConverted,
                                   kDJB2HashSeed);

  // To get this hash value, run once and examine the following EXPECT_EQ.
  // Then plug new hash value into EXPECT_EQ statements.

  // TODO(fbarchard): Make reference code mimic MMX exactly
#if USE_MMX
  EXPECT_EQ(2413171226u, rgb_hash);
#else
  EXPECT_EQ(2936300063u, rgb_hash);
#endif
}

TEST(YUVConvertTest, YV16) {
  // Allocate all surfaces.
  scoped_array<uint8> yuv_bytes(new uint8[kYUV16Size]);
  scoped_array<uint8> rgb_bytes(new uint8[kRGBSize]);
  scoped_array<uint8> rgb_converted_bytes(new uint8[kRGBSizeConverted]);

  // Read YV16 reference data from file.
  FilePath yuv_url;
  EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &yuv_url));
  yuv_url = yuv_url.Append(FILE_PATH_LITERAL("media"))
                   .Append(FILE_PATH_LITERAL("test"))
                   .Append(FILE_PATH_LITERAL("data"))
                   .Append(FILE_PATH_LITERAL("bali_640x360_P422.yuv"));
  EXPECT_EQ(static_cast<int>(kYUV16Size),
            file_util::ReadFile(yuv_url,
                                reinterpret_cast<char*>(yuv_bytes.get()),
                                static_cast<int>(kYUV16Size)));

  for (int i = 0; i < kTestTimes; ++i) {
    // Convert a frame of YUV to 32 bit ARGB.
    media::ConvertYUVToRGB32(yuv_bytes.get(),                             // Y
                             yuv_bytes.get() + kWidth * kHeight,          // U
                             yuv_bytes.get() + kWidth * kHeight * 3 / 2,  // V
                             rgb_converted_bytes.get(),  // RGB output
                             kWidth, kHeight,            // Dimensions
                             kWidth,                     // YStride
                             kWidth / 2,                 // UVStride
                             kWidth * kBpp,              // RGBStride
                             media::YV16);
  }

  unsigned int rgb_hash = DJB2Hash(rgb_converted_bytes.get(), kRGBSizeConverted,
                                   kDJB2HashSeed);

  // To get this hash value, run once and examine the following EXPECT_EQ.
  // Then plug new hash value into EXPECT_EQ statements.

  // TODO(fbarchard): Make reference code mimic MMX exactly
#if USE_MMX
  EXPECT_EQ(4222342047u, rgb_hash);
#else
  EXPECT_EQ(106869773u, rgb_hash);
#endif
}

TEST(YUVScaleTest, YV12) {
  // Read YUV reference data from file.
  FilePath yuv_url;
  EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &yuv_url));
  yuv_url = yuv_url.Append(FILE_PATH_LITERAL("media"))
                   .Append(FILE_PATH_LITERAL("test"))
                   .Append(FILE_PATH_LITERAL("data"))
                   .Append(FILE_PATH_LITERAL("bali_640x360_P420.yuv"));
  const size_t size_of_yuv = kWidth * kHeight * 12 / 8;  // 12 bpp.
  scoped_array<uint8> yuv_bytes(new uint8[size_of_yuv]);
  EXPECT_EQ(static_cast<int>(size_of_yuv),
            file_util::ReadFile(yuv_url,
                                reinterpret_cast<char*>(yuv_bytes.get()),
                                static_cast<int>(size_of_yuv)));

  // Scale a frame of YUV to 32 bit ARGB.
  const size_t size_of_rgb_scaled = kScaledWidth * kScaledHeight * kBpp;
  scoped_array<uint8> rgb_scaled_bytes(new uint8[size_of_rgb_scaled]);

  for (int i = 0; i < kTestTimes; ++i) {
    media::ScaleYUVToRGB32(yuv_bytes.get(),                           // Y
                         yuv_bytes.get() + kWidth * kHeight,          // U
                         yuv_bytes.get() + kWidth * kHeight * 5 / 4,  // V
                         rgb_scaled_bytes.get(),       // Rgb output
                         kWidth, kHeight,              // Dimensions
                         kScaledWidth, kScaledHeight,  // Dimensions
                         kWidth,                       // YStride
                         kWidth / 2,                   // UvStride
                         kScaledWidth * kBpp,          // RgbStride
                         media::YV12,
                         media::ROTATE_0,
                         media::FILTER_NONE);
  }

  unsigned int rgb_hash = DJB2Hash(rgb_scaled_bytes.get(), size_of_rgb_scaled,
                                   kDJB2HashSeed);

  // To get this hash value, run once and examine the following EXPECT_EQ.
  // Then plug new hash value into EXPECT_EQ statements.

  // TODO(fbarchard): Make reference code mimic MMX exactly
#if USE_MMX
  EXPECT_EQ(4259656254u, rgb_hash);
#else
  EXPECT_EQ(197274901u, rgb_hash);
#endif
}

TEST(YUVScaleTest, YV16) {
  // Read YV16 reference data from file.
  FilePath yuv_url;
  EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &yuv_url));
  yuv_url = yuv_url.Append(FILE_PATH_LITERAL("media"))
                   .Append(FILE_PATH_LITERAL("test"))
                   .Append(FILE_PATH_LITERAL("data"))
                   .Append(FILE_PATH_LITERAL("bali_640x360_P422.yuv"));
  const size_t size_of_yuv = kWidth * kHeight * 16 / 8;  // 16 bpp.
  scoped_array<uint8> yuv_bytes(new uint8[size_of_yuv]);
  EXPECT_EQ(static_cast<int>(size_of_yuv),
            file_util::ReadFile(yuv_url,
                                reinterpret_cast<char*>(yuv_bytes.get()),
                                static_cast<int>(size_of_yuv)));

  // Scale a frame of YUV to 32 bit ARGB.
  const size_t size_of_rgb_scaled = kScaledWidth * kScaledHeight * kBpp;
  scoped_array<uint8> rgb_scaled_bytes(new uint8[size_of_rgb_scaled]);

  for (int i = 0; i < kTestTimes; ++i) {
    media::ScaleYUVToRGB32(yuv_bytes.get(),                           // Y
                         yuv_bytes.get() + kWidth * kHeight,          // U
                         yuv_bytes.get() + kWidth * kHeight * 3 / 2,  // V
                         rgb_scaled_bytes.get(),       // Rgb output
                         kWidth, kHeight,              // Dimensions
                         kScaledWidth, kScaledHeight,  // Dimensions
                         kWidth,                       // YStride
                         kWidth / 2,                   // UvStride
                         kScaledWidth * kBpp,          // RgbStride
                         media::YV16,
                         media::ROTATE_0,
                         media::FILTER_NONE);
  }

  unsigned int rgb_hash = DJB2Hash(rgb_scaled_bytes.get(), size_of_rgb_scaled,
                                   kDJB2HashSeed);

  // To get this hash value, run once and examine the following EXPECT_EQ.
  // Then plug new hash value into EXPECT_EQ statements.

  // TODO(fbarchard): Make reference code mimic MMX exactly
#if USE_MMX
  EXPECT_EQ(974965419u, rgb_hash);
#else
  EXPECT_EQ(2946450771u, rgb_hash);
#endif
}

// This tests a known worst case YUV value, and for overflow.
TEST(YUVConvertTest, Clamp) {
  // Allocate all surfaces.
  scoped_array<uint8> yuv_bytes(new uint8[1]);
  scoped_array<uint8> rgb_bytes(new uint8[1]);
  scoped_array<uint8> rgb_converted_bytes(new uint8[1]);

  // Values that failed previously in bug report.
  unsigned char y = 255u;
  unsigned char u = 255u;
  unsigned char v = 19u;

  // Prefill extra large destination buffer to test for overflow.
  unsigned char rgb[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  // TODO(fbarchard): Make reference code mimic MMX exactly
  // The code is fixed point and has slight rounding differences.
#if USE_MMX
  unsigned char expected[8] = { 255, 255, 104, 255, 4, 5, 6, 7 };
#else
  unsigned char expected[8] = { 255, 255, 105, 255, 4, 5, 6, 7 };
#endif
  // Convert a frame of YUV to 32 bit ARGB.
  media::ConvertYUVToRGB32(&y,  // Y
                           &u,  // U
                           &v,  // V
                           &rgb[0],  // RGB output
                           1, 1,     // Dimensions
                           0,        // YStride
                           0,        // UVStride
                           0,        // RGBStride
                           media::YV12);

  int expected_test = memcmp(rgb, expected, sizeof(expected));
  EXPECT_EQ(0, expected_test);
}