mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 0fe2171 | 2012-02-22 11:21:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "third_party/libyuv/include/libyuv.h" |
| 12 | |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 13 | #include <math.h> |
| 14 | #include <string.h> |
| 15 | |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/video/i420_buffer.h" |
| 19 | #include "api/video/video_frame.h" |
| 20 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
| 21 | #include "test/frame_utils.h" |
| 22 | #include "test/gmock.h" |
| 23 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "test/testsupport/file_utils.h" |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
nisse | 69adc9c | 2016-06-02 00:58:35 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | void Calc16ByteAlignedStride(int width, int* stride_y, int* stride_uv) { |
| 30 | *stride_y = 16 * ((width + 15) / 16); |
| 31 | *stride_uv = 16 * ((width + 31) / 32); |
| 32 | } |
| 33 | |
| 34 | } // Anonymous namespace |
| 35 | |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 36 | class TestLibYuv : public ::testing::Test { |
| 37 | protected: |
| 38 | TestLibYuv(); |
Mirko Bonadei | d93a51d | 2018-07-17 15:47:51 +0200 | [diff] [blame] | 39 | void SetUp() override; |
| 40 | void TearDown() override; |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 41 | |
| 42 | FILE* source_file_; |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame] | 43 | std::unique_ptr<VideoFrame> orig_frame_; |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 44 | const int width_; |
| 45 | const int height_; |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 46 | const int size_y_; |
| 47 | const int size_uv_; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 48 | const size_t frame_length_; |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 51 | TestLibYuv::TestLibYuv() |
| 52 | : source_file_(NULL), |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 53 | orig_frame_(), |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 54 | width_(352), |
| 55 | height_(288), |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 56 | size_y_(width_ * height_), |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 57 | size_uv_(((width_ + 1) / 2) * ((height_ + 1) / 2)), |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 58 | frame_length_(CalcBufferSize(VideoType::kI420, 352, 288)) {} |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 59 | |
| 60 | void TestLibYuv::SetUp() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 61 | const std::string input_file_name = |
| 62 | webrtc::test::ResourcePath("foreman_cif", "yuv"); |
| 63 | source_file_ = fopen(input_file_name.c_str(), "rb"); |
| 64 | ASSERT_TRUE(source_file_ != NULL) |
| 65 | << "Cannot read file: " << input_file_name << "\n"; |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 66 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 67 | rtc::scoped_refptr<I420BufferInterface> buffer( |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame] | 68 | test::ReadI420Buffer(width_, height_, source_file_)); |
| 69 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 70 | orig_frame_ = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 71 | std::make_unique<VideoFrame>(VideoFrame::Builder() |
| 72 | .set_video_frame_buffer(buffer) |
| 73 | .set_rotation(webrtc::kVideoRotation_0) |
| 74 | .set_timestamp_us(0) |
| 75 | .build()); |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void TestLibYuv::TearDown() { |
| 79 | if (source_file_ != NULL) { |
| 80 | ASSERT_EQ(0, fclose(source_file_)); |
| 81 | } |
| 82 | source_file_ = NULL; |
| 83 | } |
| 84 | |
| 85 | TEST_F(TestLibYuv, ConvertSanityTest) { |
| 86 | // TODO(mikhal) |
| 87 | } |
| 88 | |
| 89 | TEST_F(TestLibYuv, ConvertTest) { |
| 90 | // Reading YUV frame - testing on the first frame of the foreman sequence |
| 91 | int j = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 92 | std::string output_file_name = |
| 93 | webrtc::test::OutputPath() + "LibYuvTest_conversion.yuv"; |
| 94 | FILE* output_file = fopen(output_file_name.c_str(), "wb"); |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 95 | ASSERT_TRUE(output_file != NULL); |
| 96 | |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 97 | double psnr = 0.0; |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 98 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 99 | rtc::scoped_refptr<I420Buffer> res_i420_buffer = |
| 100 | I420Buffer::Create(width_, height_); |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 101 | |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 102 | printf("\nConvert #%d I420 <-> I420 \n", j); |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 103 | std::unique_ptr<uint8_t[]> out_i420_buffer(new uint8_t[frame_length_]); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 104 | EXPECT_EQ(0, ConvertFromI420(*orig_frame_, VideoType::kI420, 0, |
| 105 | out_i420_buffer.get())); |
mallikarjun82 | 12e555b | 2017-11-15 14:35:56 +0530 | [diff] [blame] | 106 | int y_size = width_ * height_; |
| 107 | int u_size = res_i420_buffer->ChromaWidth() * res_i420_buffer->ChromaHeight(); |
| 108 | int ret = libyuv::I420Copy( |
| 109 | out_i420_buffer.get(), width_, out_i420_buffer.get() + y_size, |
| 110 | width_ >> 1, out_i420_buffer.get() + y_size + u_size, width_ >> 1, |
| 111 | res_i420_buffer.get()->MutableDataY(), res_i420_buffer.get()->StrideY(), |
| 112 | res_i420_buffer.get()->MutableDataU(), res_i420_buffer.get()->StrideU(), |
| 113 | res_i420_buffer.get()->MutableDataV(), res_i420_buffer.get()->StrideV(), |
| 114 | width_, height_); |
| 115 | EXPECT_EQ(0, ret); |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 116 | |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 117 | if (PrintVideoFrame(*res_i420_buffer, output_file) < 0) { |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 120 | psnr = |
| 121 | I420PSNR(*orig_frame_->video_frame_buffer()->GetI420(), *res_i420_buffer); |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 122 | EXPECT_EQ(48.0, psnr); |
| 123 | j++; |
| 124 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 125 | printf("\nConvert #%d I420 <-> RGB24\n", j); |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 126 | std::unique_ptr<uint8_t[]> res_rgb_buffer2(new uint8_t[width_ * height_ * 3]); |
mikhal@webrtc.org | 91a0340 | 2012-10-30 19:19:32 +0000 | [diff] [blame] | 127 | // Align the stride values for the output frame. |
| 128 | int stride_y = 0; |
| 129 | int stride_uv = 0; |
| 130 | Calc16ByteAlignedStride(width_, &stride_y, &stride_uv); |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 131 | res_i420_buffer = |
| 132 | I420Buffer::Create(width_, height_, stride_y, stride_uv, stride_uv); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 133 | EXPECT_EQ(0, ConvertFromI420(*orig_frame_, VideoType::kRGB24, 0, |
| 134 | res_rgb_buffer2.get())); |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 135 | |
mallikarjun82 | 12e555b | 2017-11-15 14:35:56 +0530 | [diff] [blame] | 136 | ret = libyuv::ConvertToI420( |
| 137 | res_rgb_buffer2.get(), 0, res_i420_buffer.get()->MutableDataY(), |
| 138 | res_i420_buffer.get()->StrideY(), res_i420_buffer.get()->MutableDataU(), |
| 139 | res_i420_buffer.get()->StrideU(), res_i420_buffer.get()->MutableDataV(), |
| 140 | res_i420_buffer.get()->StrideV(), 0, 0, width_, height_, |
| 141 | res_i420_buffer->width(), res_i420_buffer->height(), libyuv::kRotate0, |
| 142 | ConvertVideoType(VideoType::kRGB24)); |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 143 | |
mallikarjun82 | 12e555b | 2017-11-15 14:35:56 +0530 | [diff] [blame] | 144 | EXPECT_EQ(0, ret); |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 145 | if (PrintVideoFrame(*res_i420_buffer, output_file) < 0) { |
leozwang@webrtc.org | 2fc6e38 | 2012-05-29 17:33:13 +0000 | [diff] [blame] | 146 | return; |
| 147 | } |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 148 | psnr = |
| 149 | I420PSNR(*orig_frame_->video_frame_buffer()->GetI420(), *res_i420_buffer); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 150 | |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 151 | // Optimization Speed- quality trade-off => 45 dB only (platform dependant). |
stefan@webrtc.org | 0fe2171 | 2012-02-22 11:21:18 +0000 | [diff] [blame] | 152 | EXPECT_GT(ceil(psnr), 44); |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 153 | j++; |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 154 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 155 | printf("\nConvert #%d I420 <-> UYVY\n", j); |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 156 | std::unique_ptr<uint8_t[]> out_uyvy_buffer(new uint8_t[width_ * height_ * 2]); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 157 | EXPECT_EQ(0, ConvertFromI420(*orig_frame_, VideoType::kUYVY, 0, |
| 158 | out_uyvy_buffer.get())); |
mallikarjun82 | 12e555b | 2017-11-15 14:35:56 +0530 | [diff] [blame] | 159 | |
| 160 | ret = libyuv::ConvertToI420( |
| 161 | out_uyvy_buffer.get(), 0, res_i420_buffer.get()->MutableDataY(), |
| 162 | res_i420_buffer.get()->StrideY(), res_i420_buffer.get()->MutableDataU(), |
| 163 | res_i420_buffer.get()->StrideU(), res_i420_buffer.get()->MutableDataV(), |
| 164 | res_i420_buffer.get()->StrideV(), 0, 0, width_, height_, |
| 165 | res_i420_buffer->width(), res_i420_buffer->height(), libyuv::kRotate0, |
| 166 | ConvertVideoType(VideoType::kUYVY)); |
| 167 | |
| 168 | EXPECT_EQ(0, ret); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 169 | psnr = |
| 170 | I420PSNR(*orig_frame_->video_frame_buffer()->GetI420(), *res_i420_buffer); |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 171 | EXPECT_EQ(48.0, psnr); |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 172 | if (PrintVideoFrame(*res_i420_buffer, output_file) < 0) { |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 173 | return; |
| 174 | } |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 175 | j++; |
| 176 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 177 | printf("\nConvert #%d I420 <-> YUY2\n", j); |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 178 | std::unique_ptr<uint8_t[]> out_yuy2_buffer(new uint8_t[width_ * height_ * 2]); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 179 | EXPECT_EQ(0, ConvertFromI420(*orig_frame_, VideoType::kYUY2, 0, |
| 180 | out_yuy2_buffer.get())); |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 181 | |
mallikarjun82 | 12e555b | 2017-11-15 14:35:56 +0530 | [diff] [blame] | 182 | ret = libyuv::ConvertToI420( |
| 183 | out_yuy2_buffer.get(), 0, res_i420_buffer.get()->MutableDataY(), |
| 184 | res_i420_buffer.get()->StrideY(), res_i420_buffer.get()->MutableDataU(), |
| 185 | res_i420_buffer.get()->StrideU(), res_i420_buffer.get()->MutableDataV(), |
| 186 | res_i420_buffer.get()->StrideV(), 0, 0, width_, height_, |
| 187 | res_i420_buffer->width(), res_i420_buffer->height(), libyuv::kRotate0, |
| 188 | ConvertVideoType(VideoType::kYUY2)); |
| 189 | |
| 190 | EXPECT_EQ(0, ret); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 191 | |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 192 | if (PrintVideoFrame(*res_i420_buffer, output_file) < 0) { |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 193 | return; |
leozwang@webrtc.org | 2fc6e38 | 2012-05-29 17:33:13 +0000 | [diff] [blame] | 194 | } |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 195 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 196 | psnr = |
| 197 | I420PSNR(*orig_frame_->video_frame_buffer()->GetI420(), *res_i420_buffer); |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 198 | EXPECT_EQ(48.0, psnr); |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 199 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 200 | printf("\nConvert #%d I420 <-> RGB565\n", j); |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 201 | std::unique_ptr<uint8_t[]> out_rgb565_buffer( |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 202 | new uint8_t[width_ * height_ * 2]); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 203 | EXPECT_EQ(0, ConvertFromI420(*orig_frame_, VideoType::kRGB565, 0, |
| 204 | out_rgb565_buffer.get())); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 205 | |
mallikarjun82 | 12e555b | 2017-11-15 14:35:56 +0530 | [diff] [blame] | 206 | ret = libyuv::ConvertToI420( |
| 207 | out_rgb565_buffer.get(), 0, res_i420_buffer.get()->MutableDataY(), |
| 208 | res_i420_buffer.get()->StrideY(), res_i420_buffer.get()->MutableDataU(), |
| 209 | res_i420_buffer.get()->StrideU(), res_i420_buffer.get()->MutableDataV(), |
| 210 | res_i420_buffer.get()->StrideV(), 0, 0, width_, height_, |
| 211 | res_i420_buffer->width(), res_i420_buffer->height(), libyuv::kRotate0, |
| 212 | ConvertVideoType(VideoType::kRGB565)); |
| 213 | |
| 214 | EXPECT_EQ(0, ret); |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 215 | if (PrintVideoFrame(*res_i420_buffer, output_file) < 0) { |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 216 | return; |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 217 | } |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 218 | j++; |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 219 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 220 | psnr = |
| 221 | I420PSNR(*orig_frame_->video_frame_buffer()->GetI420(), *res_i420_buffer); |
leozwang@webrtc.org | 1ea25b4 | 2012-05-04 17:55:57 +0000 | [diff] [blame] | 222 | // TODO(leozwang) Investigate the right psnr should be set for I420ToRGB565, |
| 223 | // Another example is I420ToRGB24, the psnr is 44 |
mikhal@webrtc.org | 737ed3b | 2012-11-01 15:45:38 +0000 | [diff] [blame] | 224 | // TODO(mikhal): Add psnr for RGB565, 1555, 4444, convert to ARGB. |
leozwang@webrtc.org | 1ea25b4 | 2012-05-04 17:55:57 +0000 | [diff] [blame] | 225 | EXPECT_GT(ceil(psnr), 40); |
leozwang@webrtc.org | 83958df | 2012-05-04 17:07:45 +0000 | [diff] [blame] | 226 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 227 | printf("\nConvert #%d I420 <-> ARGB8888\n", j); |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 228 | std::unique_ptr<uint8_t[]> out_argb8888_buffer( |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 229 | new uint8_t[width_ * height_ * 4]); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 230 | EXPECT_EQ(0, ConvertFromI420(*orig_frame_, VideoType::kARGB, 0, |
| 231 | out_argb8888_buffer.get())); |
leozwang@webrtc.org | 83958df | 2012-05-04 17:07:45 +0000 | [diff] [blame] | 232 | |
mallikarjun82 | 12e555b | 2017-11-15 14:35:56 +0530 | [diff] [blame] | 233 | ret = libyuv::ConvertToI420( |
| 234 | out_argb8888_buffer.get(), 0, res_i420_buffer.get()->MutableDataY(), |
| 235 | res_i420_buffer.get()->StrideY(), res_i420_buffer.get()->MutableDataU(), |
| 236 | res_i420_buffer.get()->StrideU(), res_i420_buffer.get()->MutableDataV(), |
| 237 | res_i420_buffer.get()->StrideV(), 0, 0, width_, height_, |
| 238 | res_i420_buffer->width(), res_i420_buffer->height(), libyuv::kRotate0, |
| 239 | ConvertVideoType(VideoType::kARGB)); |
| 240 | |
| 241 | EXPECT_EQ(0, ret); |
leozwang@webrtc.org | 83958df | 2012-05-04 17:07:45 +0000 | [diff] [blame] | 242 | |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 243 | if (PrintVideoFrame(*res_i420_buffer, output_file) < 0) { |
leozwang@webrtc.org | 2fc6e38 | 2012-05-29 17:33:13 +0000 | [diff] [blame] | 244 | return; |
| 245 | } |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 246 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 247 | psnr = |
| 248 | I420PSNR(*orig_frame_->video_frame_buffer()->GetI420(), *res_i420_buffer); |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 249 | // TODO(leozwang) Investigate the right psnr should be set for |
| 250 | // I420ToARGB8888, |
leozwang@webrtc.org | 329fcbb | 2012-05-04 21:31:22 +0000 | [diff] [blame] | 251 | EXPECT_GT(ceil(psnr), 42); |
leozwang@webrtc.org | 83958df | 2012-05-04 17:07:45 +0000 | [diff] [blame] | 252 | |
kjellander@webrtc.org | 94558d8 | 2012-01-04 13:51:50 +0000 | [diff] [blame] | 253 | ASSERT_EQ(0, fclose(output_file)); |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 254 | } |
| 255 | |
mikhal@webrtc.org | 32b3f40 | 2012-11-20 23:52:50 +0000 | [diff] [blame] | 256 | TEST_F(TestLibYuv, ConvertAlignedFrame) { |
| 257 | // Reading YUV frame - testing on the first frame of the foreman sequence |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 258 | std::string output_file_name = |
| 259 | webrtc::test::OutputPath() + "LibYuvTest_conversion.yuv"; |
| 260 | FILE* output_file = fopen(output_file_name.c_str(), "wb"); |
mikhal@webrtc.org | 32b3f40 | 2012-11-20 23:52:50 +0000 | [diff] [blame] | 261 | ASSERT_TRUE(output_file != NULL); |
| 262 | |
| 263 | double psnr = 0.0; |
| 264 | |
mikhal@webrtc.org | 32b3f40 | 2012-11-20 23:52:50 +0000 | [diff] [blame] | 265 | int stride_y = 0; |
| 266 | int stride_uv = 0; |
| 267 | Calc16ByteAlignedStride(width_, &stride_y, &stride_uv); |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 268 | |
| 269 | rtc::scoped_refptr<I420Buffer> res_i420_buffer = |
| 270 | I420Buffer::Create(width_, height_, stride_y, stride_uv, stride_uv); |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 271 | std::unique_ptr<uint8_t[]> out_i420_buffer(new uint8_t[frame_length_]); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 272 | EXPECT_EQ(0, ConvertFromI420(*orig_frame_, VideoType::kI420, 0, |
mikhal@webrtc.org | 32b3f40 | 2012-11-20 23:52:50 +0000 | [diff] [blame] | 273 | out_i420_buffer.get())); |
mallikarjun82 | 12e555b | 2017-11-15 14:35:56 +0530 | [diff] [blame] | 274 | int y_size = width_ * height_; |
| 275 | int u_size = res_i420_buffer->ChromaWidth() * res_i420_buffer->ChromaHeight(); |
| 276 | int ret = libyuv::I420Copy( |
| 277 | out_i420_buffer.get(), width_, out_i420_buffer.get() + y_size, |
| 278 | width_ >> 1, out_i420_buffer.get() + y_size + u_size, width_ >> 1, |
| 279 | res_i420_buffer.get()->MutableDataY(), res_i420_buffer.get()->StrideY(), |
| 280 | res_i420_buffer.get()->MutableDataU(), res_i420_buffer.get()->StrideU(), |
| 281 | res_i420_buffer.get()->MutableDataV(), res_i420_buffer.get()->StrideV(), |
| 282 | width_, height_); |
| 283 | |
| 284 | EXPECT_EQ(0, ret); |
mikhal@webrtc.org | 32b3f40 | 2012-11-20 23:52:50 +0000 | [diff] [blame] | 285 | |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 286 | if (PrintVideoFrame(*res_i420_buffer, output_file) < 0) { |
mikhal@webrtc.org | 32b3f40 | 2012-11-20 23:52:50 +0000 | [diff] [blame] | 287 | return; |
| 288 | } |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 289 | psnr = |
| 290 | I420PSNR(*orig_frame_->video_frame_buffer()->GetI420(), *res_i420_buffer); |
mikhal@webrtc.org | 32b3f40 | 2012-11-20 23:52:50 +0000 | [diff] [blame] | 291 | EXPECT_EQ(48.0, psnr); |
| 292 | } |
| 293 | |
magjed | 5a87245 | 2016-10-20 03:34:29 -0700 | [diff] [blame] | 294 | static uint8_t Average(int a, int b, int c, int d) { |
| 295 | return (a + b + c + d + 2) / 4; |
| 296 | } |
| 297 | |
| 298 | TEST_F(TestLibYuv, NV12Scale2x2to2x2) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 299 | const std::vector<uint8_t> src_y = {0, 1, 2, 3}; |
magjed | 5a87245 | 2016-10-20 03:34:29 -0700 | [diff] [blame] | 300 | const std::vector<uint8_t> src_uv = {0, 1}; |
| 301 | std::vector<uint8_t> dst_y(4); |
| 302 | std::vector<uint8_t> dst_uv(2); |
| 303 | |
Anders Carlsson | bfe45c2 | 2017-06-19 16:46:31 +0200 | [diff] [blame] | 304 | uint8_t* tmp_buffer = nullptr; |
| 305 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 306 | NV12Scale(tmp_buffer, src_y.data(), 2, src_uv.data(), 2, 2, 2, dst_y.data(), |
| 307 | 2, dst_uv.data(), 2, 2, 2); |
magjed | 5a87245 | 2016-10-20 03:34:29 -0700 | [diff] [blame] | 308 | |
| 309 | EXPECT_THAT(dst_y, ::testing::ContainerEq(src_y)); |
| 310 | EXPECT_THAT(dst_uv, ::testing::ContainerEq(src_uv)); |
| 311 | } |
| 312 | |
| 313 | TEST_F(TestLibYuv, NV12Scale4x4to2x2) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 314 | const uint8_t src_y[] = {0, 1, 2, 3, 4, 5, 6, 7, |
| 315 | 8, 9, 10, 11, 12, 13, 14, 15}; |
| 316 | const uint8_t src_uv[] = {0, 1, 2, 3, 4, 5, 6, 7}; |
magjed | 5a87245 | 2016-10-20 03:34:29 -0700 | [diff] [blame] | 317 | std::vector<uint8_t> dst_y(4); |
| 318 | std::vector<uint8_t> dst_uv(2); |
| 319 | |
| 320 | std::vector<uint8_t> tmp_buffer; |
Anders Carlsson | bfe45c2 | 2017-06-19 16:46:31 +0200 | [diff] [blame] | 321 | const int src_chroma_width = (4 + 1) / 2; |
| 322 | const int src_chroma_height = (4 + 1) / 2; |
| 323 | const int dst_chroma_width = (2 + 1) / 2; |
| 324 | const int dst_chroma_height = (2 + 1) / 2; |
| 325 | tmp_buffer.resize(src_chroma_width * src_chroma_height * 2 + |
| 326 | dst_chroma_width * dst_chroma_height * 2); |
| 327 | tmp_buffer.shrink_to_fit(); |
| 328 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 329 | NV12Scale(tmp_buffer.data(), src_y, 4, src_uv, 4, 4, 4, dst_y.data(), 2, |
| 330 | dst_uv.data(), 2, 2, 2); |
magjed | 5a87245 | 2016-10-20 03:34:29 -0700 | [diff] [blame] | 331 | |
| 332 | EXPECT_THAT(dst_y, ::testing::ElementsAre( |
| 333 | Average(0, 1, 4, 5), Average(2, 3, 6, 7), |
| 334 | Average(8, 9, 12, 13), Average(10, 11, 14, 15))); |
| 335 | EXPECT_THAT(dst_uv, |
| 336 | ::testing::ElementsAre(Average(0, 2, 4, 6), Average(1, 3, 5, 7))); |
| 337 | } |
| 338 | |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 339 | } // namespace webrtc |