summaryrefslogtreecommitdiff
path: root/common_video/libyuv/libyuv_unittest.cc
blob: 8c79816e376fd92b8b06a710a14f51e8b083bb60 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <math.h>
#include <string.h>

#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/common_video/interface/i420_video_frame.h"
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
#include "webrtc/system_wrappers/interface/tick_util.h"
#include "webrtc/test/testsupport/fileutils.h"

namespace webrtc {

int PrintBuffer(const uint8_t* buffer, int width, int height, int stride) {
  if (buffer == NULL)
    return -1;
  int k;
  const uint8_t* tmp_buffer = buffer;
  for (int i = 0; i < height; i++) {
    k = 0;
    for (int j = 0; j < width; j++) {
      printf("%d ", tmp_buffer[k++]);
    }
    tmp_buffer += stride;
    printf(" \n");
  }
  printf(" \n");
  return 0;
}


int PrintFrame(const I420VideoFrame* frame, const char* str) {
  if (frame == NULL)
     return -1;
  printf("%s %dx%d \n", str, frame->width(), frame->height());

  int ret = 0;
  for (int plane_num = 0; plane_num < kNumOfPlanes; ++plane_num) {
    PlaneType plane_type = static_cast<PlaneType>(plane_num);
    int width = (plane_num ? (frame->width() + 1) / 2 : frame->width());
    int height = (plane_num ? (frame->height() + 1) / 2 : frame->height());
    ret += PrintBuffer(frame->buffer(plane_type), width, height,
                       frame->stride(plane_type));
  }
  return ret;
}


// Create an image from on a YUV frame. Every plane value starts with a start
// value, and will be set to increasing values.
void CreateImage(I420VideoFrame* frame, int plane_offset[kNumOfPlanes]) {
  if (frame == NULL)
    return;
  for (int plane_num = 0; plane_num < kNumOfPlanes; ++plane_num) {
    int width = (plane_num != kYPlane ? (frame->width() + 1) / 2 :
      frame->width());
    int height = (plane_num != kYPlane ? (frame->height() + 1) / 2 :
      frame->height());
    PlaneType plane_type = static_cast<PlaneType>(plane_num);
    uint8_t *data = frame->buffer(plane_type);
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        data[j] = static_cast<uint8_t>(i + plane_offset[plane_num] + j);
      }
      data += frame->stride(plane_type);
    }
  }
}

class TestLibYuv : public ::testing::Test {
 protected:
  TestLibYuv();
  virtual void SetUp();
  virtual void TearDown();

  FILE* source_file_;
  I420VideoFrame orig_frame_;
  scoped_array<uint8_t> orig_buffer_;
  const int width_;
  const int height_;
  const int size_y_;
  const int size_uv_;
  const int frame_length_;
};

TestLibYuv::TestLibYuv()
    : source_file_(NULL),
      orig_frame_(),
      width_(352),
      height_(288),
      size_y_(width_ * height_),
      size_uv_(((width_ + 1 ) / 2) * ((height_ + 1) / 2)),
      frame_length_(CalcBufferSize(kI420, 352, 288)) {
  orig_buffer_.reset(new uint8_t[frame_length_]);
}

void TestLibYuv::SetUp() {
  const std::string input_file_name = webrtc::test::ProjectRootPath() +
                                      "resources/foreman_cif.yuv";
  source_file_  = fopen(input_file_name.c_str(), "rb");
  ASSERT_TRUE(source_file_ != NULL) << "Cannot read file: "<<
                                       input_file_name << "\n";

  EXPECT_EQ(fread(orig_buffer_.get(), 1, frame_length_, source_file_),
            static_cast<unsigned int>(frame_length_));
  EXPECT_EQ(0, orig_frame_.CreateFrame(size_y_, orig_buffer_.get(),
                                       size_uv_, orig_buffer_.get() + size_y_,
                                       size_uv_, orig_buffer_.get() +
                                       size_y_ + size_uv_,
                                       width_, height_,
                                       width_, (width_ + 1) / 2,
                                       (width_ + 1) / 2));
}

void TestLibYuv::TearDown() {
  if (source_file_ != NULL) {
    ASSERT_EQ(0, fclose(source_file_));
  }
  source_file_ = NULL;
}

TEST_F(TestLibYuv, ConvertSanityTest) {
  // TODO(mikhal)
}

TEST_F(TestLibYuv, ConvertTest) {
  // Reading YUV frame - testing on the first frame of the foreman sequence
  int j = 0;
  std::string output_file_name = webrtc::test::OutputPath() +
                                 "LibYuvTest_conversion.yuv";
  FILE*  output_file = fopen(output_file_name.c_str(), "wb");
  ASSERT_TRUE(output_file != NULL);

  double psnr = 0.0;

  I420VideoFrame res_i420_frame;
  EXPECT_EQ(0,res_i420_frame.CreateEmptyFrame(width_, height_, width_,
                                              (width_ + 1) / 2,
                                              (width_ + 1) / 2));
  printf("\nConvert #%d I420 <-> I420 \n", j);
  scoped_array<uint8_t> out_i420_buffer(new uint8_t[frame_length_]);
  EXPECT_EQ(0, ConvertFromI420(orig_frame_, kI420, 0,
                               out_i420_buffer.get()));
  EXPECT_EQ(0, ConvertToI420(kI420, out_i420_buffer.get(), 0, 0,
                             width_, height_,
                             0, kRotateNone, &res_i420_frame));

  if (PrintI420VideoFrame(res_i420_frame, output_file) < 0) {
    return;
  }
  psnr = I420PSNR(&orig_frame_, &res_i420_frame);
  EXPECT_EQ(48.0, psnr);
  j++;

  printf("\nConvert #%d I420 <-> RGB24\n", j);
  scoped_array<uint8_t> res_rgb_buffer2(new uint8_t[width_ * height_ * 3]);
  // Align the stride values for the output frame.
  int stride_y = 0;
  int stride_uv = 0;
  Calc16ByteAlignedStride(width_, &stride_y, &stride_uv);
  res_i420_frame.CreateEmptyFrame(width_, height_, stride_y,
                                  stride_uv, stride_uv);
  EXPECT_EQ(0, ConvertFromI420(orig_frame_, kRGB24, 0, res_rgb_buffer2.get()));

  EXPECT_EQ(0, ConvertToI420(kRGB24, res_rgb_buffer2.get(), 0, 0, width_,
                             height_, 0, kRotateNone, &res_i420_frame));

  if (PrintI420VideoFrame(res_i420_frame, output_file) < 0) {
    return;
  }
  psnr = I420PSNR(&orig_frame_, &res_i420_frame);

  // Optimization Speed- quality trade-off => 45 dB only (platform dependant).
  EXPECT_GT(ceil(psnr), 44);
  j++;

  printf("\nConvert #%d I420 <-> UYVY\n", j);
  scoped_array<uint8_t> out_uyvy_buffer(new uint8_t[width_ * height_ * 2]);
  EXPECT_EQ(0, ConvertFromI420(orig_frame_,  kUYVY, 0, out_uyvy_buffer.get()));
  EXPECT_EQ(0, ConvertToI420(kUYVY, out_uyvy_buffer.get(), 0, 0, width_,
                             height_, 0, kRotateNone, &res_i420_frame));
  psnr = I420PSNR(&orig_frame_, &res_i420_frame);
  EXPECT_EQ(48.0, psnr);
  if (PrintI420VideoFrame(res_i420_frame, output_file) < 0) {
    return;
  }
  j++;

  printf("\nConvert #%d I420 <-> YV12\n", j);
  scoped_array<uint8_t> outYV120Buffer(new uint8_t[frame_length_]);
  scoped_array<uint8_t> res_i420_buffer(new uint8_t[frame_length_]);
  I420VideoFrame yv12_frame;
  EXPECT_EQ(0, ConvertFromI420(orig_frame_, kYV12, 0, outYV120Buffer.get()));
  yv12_frame.CreateFrame(size_y_, outYV120Buffer.get(),
                         size_uv_, outYV120Buffer.get() + size_y_,
                         size_uv_, outYV120Buffer.get() + size_y_ + size_uv_,
                         width_, height_,
                         width_, (width_ + 1) / 2, (width_ + 1) / 2);
  EXPECT_EQ(0, ConvertFromYV12(yv12_frame, kI420, 0, res_i420_buffer.get()));
  if (fwrite(res_i420_buffer.get(), 1, frame_length_,
             output_file) != static_cast<unsigned int>(frame_length_)) {
    return;
  }

  psnr = I420PSNR(orig_buffer_.get(), res_i420_buffer.get(), width_, height_);
  EXPECT_EQ(48.0, psnr);
  j++;

  printf("\nConvert #%d I420 <-> YUY2\n", j);
  scoped_array<uint8_t> out_yuy2_buffer(new uint8_t[width_ * height_ * 2]);
  EXPECT_EQ(0, ConvertFromI420(orig_frame_,  kYUY2, 0, out_yuy2_buffer.get()));

  EXPECT_EQ(0, ConvertToI420(kYUY2, out_yuy2_buffer.get(), 0, 0, width_,
                             height_, 0, kRotateNone, &res_i420_frame));

  if (PrintI420VideoFrame(res_i420_frame, output_file) < 0) {
    return;
  }

  psnr = I420PSNR(&orig_frame_, &res_i420_frame);
  EXPECT_EQ(48.0, psnr);
  printf("\nConvert #%d I420 <-> RGB565\n", j);
  scoped_array<uint8_t> out_rgb565_buffer(new uint8_t[width_ * height_ * 2]);
  EXPECT_EQ(0, ConvertFromI420(orig_frame_, kRGB565, 0,
                               out_rgb565_buffer.get()));

  EXPECT_EQ(0, ConvertToI420(kRGB565, out_rgb565_buffer.get(), 0, 0, width_,
                             height_, 0, kRotateNone, &res_i420_frame));

  if (PrintI420VideoFrame(res_i420_frame, output_file) < 0) {
    return;
  }
  j++;

  psnr = I420PSNR(&orig_frame_, &res_i420_frame);
  // TODO(leozwang) Investigate the right psnr should be set for I420ToRGB565,
  // Another example is I420ToRGB24, the psnr is 44
  // TODO(mikhal): Add psnr for RGB565, 1555, 4444, convert to ARGB.
  EXPECT_GT(ceil(psnr), 40);

  printf("\nConvert #%d I420 <-> ARGB8888\n", j);
  scoped_array<uint8_t> out_argb8888_buffer(new uint8_t[width_ * height_ * 4]);
  EXPECT_EQ(0, ConvertFromI420(orig_frame_, kARGB, 0,
                               out_argb8888_buffer.get()));

  EXPECT_EQ(0, ConvertToI420(kARGB, out_argb8888_buffer.get(), 0, 0, width_,
                             height_, 0, kRotateNone, &res_i420_frame));

  if (PrintI420VideoFrame(res_i420_frame, output_file) < 0) {
    return;
  }

  psnr = I420PSNR(&orig_frame_, &res_i420_frame);
  // TODO(leozwang) Investigate the right psnr should be set for I420ToARGB8888,
  EXPECT_GT(ceil(psnr), 42);

  ASSERT_EQ(0, fclose(output_file));
}

TEST_F(TestLibYuv, ConvertAlignedFrame) {
  // Reading YUV frame - testing on the first frame of the foreman sequence
  std::string output_file_name = webrtc::test::OutputPath() +
                                 "LibYuvTest_conversion.yuv";
  FILE*  output_file = fopen(output_file_name.c_str(), "wb");
  ASSERT_TRUE(output_file != NULL);

  double psnr = 0.0;

  I420VideoFrame res_i420_frame;
  int stride_y = 0;
  int stride_uv = 0;
  Calc16ByteAlignedStride(width_, &stride_y, &stride_uv);
  EXPECT_EQ(0,res_i420_frame.CreateEmptyFrame(width_, height_,
                                              stride_y, stride_uv, stride_uv));
  scoped_array<uint8_t> out_i420_buffer(new uint8_t[frame_length_]);
  EXPECT_EQ(0, ConvertFromI420(orig_frame_, kI420, 0,
                               out_i420_buffer.get()));
  EXPECT_EQ(0, ConvertToI420(kI420, out_i420_buffer.get(), 0, 0,
                             width_, height_,
                             0, kRotateNone, &res_i420_frame));

  if (PrintI420VideoFrame(res_i420_frame, output_file) < 0) {
    return;
  }
  psnr = I420PSNR(&orig_frame_, &res_i420_frame);
  EXPECT_EQ(48.0, psnr);
}


TEST_F(TestLibYuv, RotateTest) {
  // Use ConvertToI420 for multiple roatations - see that nothing breaks, all
  // memory is properly allocated and end result is equal to the starting point.
  I420VideoFrame rotated_res_i420_frame;
  int rotated_width = height_;
  int rotated_height = width_;
  int stride_y ;
  int stride_uv;
  Calc16ByteAlignedStride(rotated_width, &stride_y, &stride_uv);
  EXPECT_EQ(0,rotated_res_i420_frame.CreateEmptyFrame(rotated_width,
                                                      rotated_height,
                                                      stride_y,
                                                      stride_uv,
                                                      stride_uv));
  EXPECT_EQ(0, ConvertToI420(kI420, orig_buffer_.get(), 0, 0,
                             width_, height_,
                             0, kRotate90, &rotated_res_i420_frame));
  EXPECT_EQ(0, ConvertToI420(kI420, orig_buffer_.get(), 0, 0,
                             width_, height_,
                             0, kRotate270, &rotated_res_i420_frame));
  EXPECT_EQ(0,rotated_res_i420_frame.CreateEmptyFrame(width_, height_,
                                                      width_, (width_ + 1) / 2,
                                                      (width_ + 1) / 2));
  EXPECT_EQ(0, ConvertToI420(kI420, orig_buffer_.get(), 0, 0,
                             width_, height_,
                             0, kRotate180, &rotated_res_i420_frame));
}

TEST_F(TestLibYuv, MirrorTest) {
  // TODO(mikhal): Add an automated test to confirm output.
  std::string str;
  int width = 16;
  int half_width = (width + 1) / 2;
  int height = 8;
  int half_height = (height + 1) / 2;

  I420VideoFrame test_frame;
  test_frame.CreateEmptyFrame(width, height, width,
                              half_width, half_width);
  memset(test_frame.buffer(kYPlane), 255, width * height);
  memset(test_frame.buffer(kUPlane), 255, half_width * half_height);
  memset(test_frame.buffer(kVPlane), 255, half_width * half_height);

  // Create input frame.
  I420VideoFrame in_frame, test_in_frame;
  in_frame.CreateEmptyFrame(width, height, width,
                            half_width ,half_width);
  int plane_offset[kNumOfPlanes];
  plane_offset[kYPlane] = 10;
  plane_offset[kUPlane] = 100;
  plane_offset[kVPlane] = 200;
  CreateImage(&in_frame, plane_offset);
  EXPECT_EQ(0, PrintFrame(&in_frame, "InputFrame"));
  test_in_frame.CopyFrame(in_frame);

  I420VideoFrame out_frame, test_out_frame;
  out_frame.CreateEmptyFrame(width, height, width,
                             half_width ,half_width);
  CreateImage(&out_frame, plane_offset);
  test_out_frame.CopyFrame(out_frame);

  // Left-Right.
  std::cout << "Test Mirror function: LeftRight" << std::endl;
  EXPECT_EQ(0, MirrorI420LeftRight(&in_frame, &out_frame));
  EXPECT_EQ(0, PrintFrame(&out_frame, "OutputFrame"));
  EXPECT_EQ(0, MirrorI420LeftRight(&out_frame, &in_frame));

  EXPECT_EQ(0, memcmp(in_frame.buffer(kYPlane),
    test_in_frame.buffer(kYPlane), width * height));
  EXPECT_EQ(0, memcmp(in_frame.buffer(kUPlane),
    test_in_frame.buffer(kUPlane), half_width * half_height));
  EXPECT_EQ(0, memcmp(in_frame.buffer(kVPlane),
    test_in_frame.buffer(kVPlane), half_width * half_height));

  // UpDown
  std::cout << "Test Mirror function: UpDown" << std::endl;
  EXPECT_EQ(0, MirrorI420UpDown(&in_frame, &out_frame));
  EXPECT_EQ(0, PrintFrame(&out_frame, "OutputFrame"));
  EXPECT_EQ(0, MirrorI420UpDown(&out_frame, &test_frame));
  EXPECT_EQ(0, memcmp(in_frame.buffer(kYPlane),
    test_in_frame.buffer(kYPlane), width * height));
  EXPECT_EQ(0, memcmp(in_frame.buffer(kUPlane),
    test_in_frame.buffer(kUPlane), half_width * half_height));
  EXPECT_EQ(0, memcmp(in_frame.buffer(kVPlane),
    test_in_frame.buffer(kVPlane), half_width * half_height));

  // TODO(mikhal): Write to a file, and ask to look at the file.

  std::cout << "Do the mirrored frames look correct?" << std::endl;
}

TEST_F(TestLibYuv, alignment) {
  int value = 0x3FF; // 1023
  EXPECT_EQ(0x400, AlignInt(value, 128));  // Low 7 bits are zero.
  EXPECT_EQ(0x400, AlignInt(value, 64));  // Low 6 bits are zero.
  EXPECT_EQ(0x400, AlignInt(value, 32));  // Low 5 bits are zero.
}

TEST_F(TestLibYuv, StrideAlignment) {
  int stride_y = 0;
  int stride_uv = 0;
  int width = 52;
  Calc16ByteAlignedStride(width, &stride_y, &stride_uv);
  EXPECT_EQ(64, stride_y);
  EXPECT_EQ(32, stride_uv);
  width = 128;
  Calc16ByteAlignedStride(width, &stride_y, &stride_uv);
  EXPECT_EQ(128, stride_y);
  EXPECT_EQ(64, stride_uv);
  width = 127;
  Calc16ByteAlignedStride(width, &stride_y, &stride_uv);
  EXPECT_EQ(128, stride_y);
  EXPECT_EQ(64, stride_uv);
}

}  // namespace