Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 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 "test/frame_utils.h" |
| 12 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "api/video/i420_buffer.h" |
| 17 | #include "api/video/video_frame.h" |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | |
| 22 | bool EqualPlane(const uint8_t* data1, |
| 23 | const uint8_t* data2, |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 24 | int stride1, |
| 25 | int stride2, |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 26 | int width, |
| 27 | int height) { |
| 28 | for (int y = 0; y < height; ++y) { |
| 29 | if (memcmp(data1, data2, width) != 0) |
| 30 | return false; |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 31 | data1 += stride1; |
| 32 | data2 += stride2; |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 33 | } |
| 34 | return true; |
| 35 | } |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 36 | |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 37 | bool FramesEqual(const webrtc::VideoFrame& f1, const webrtc::VideoFrame& f2) { |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 38 | if (f1.timestamp() != f2.timestamp() || |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 39 | f1.ntp_time_ms() != f2.ntp_time_ms() || |
| 40 | f1.render_time_ms() != f2.render_time_ms()) { |
| 41 | return false; |
| 42 | } |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 43 | return FrameBufsEqual(f1.video_frame_buffer(), f2.video_frame_buffer()); |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 44 | } |
| 45 | |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 46 | bool FrameBufsEqual(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f1, |
| 47 | const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f2) { |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 48 | if (f1 == f2) { |
| 49 | return true; |
| 50 | } |
| 51 | // Exlude nullptr (except if both are nullptr, as above) |
| 52 | if (!f1 || !f2) { |
| 53 | return false; |
| 54 | } |
| 55 | |
Magnus Jedvert | 90e3190 | 2017-06-07 11:32:50 +0200 | [diff] [blame] | 56 | if (f1->width() != f2->width() || f1->height() != f2->height() || |
| 57 | f1->type() != f2->type()) { |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 58 | return false; |
| 59 | } |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 60 | |
Magnus Jedvert | 90e3190 | 2017-06-07 11:32:50 +0200 | [diff] [blame] | 61 | rtc::scoped_refptr<webrtc::I420BufferInterface> f1_i420 = f1->ToI420(); |
| 62 | rtc::scoped_refptr<webrtc::I420BufferInterface> f2_i420 = f2->ToI420(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 63 | return EqualPlane(f1_i420->DataY(), f2_i420->DataY(), f1_i420->StrideY(), |
| 64 | f2_i420->StrideY(), f1_i420->width(), f1_i420->height()) && |
| 65 | EqualPlane(f1_i420->DataU(), f2_i420->DataU(), f1_i420->StrideU(), |
| 66 | f2_i420->StrideU(), f1_i420->ChromaWidth(), |
| 67 | f1_i420->ChromaHeight()) && |
| 68 | EqualPlane(f1_i420->DataV(), f2_i420->DataV(), f1_i420->StrideV(), |
| 69 | f2_i420->StrideV(), f1_i420->ChromaWidth(), |
| 70 | f1_i420->ChromaHeight()); |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | rtc::scoped_refptr<I420Buffer> ReadI420Buffer(int width, int height, FILE* f) { |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame] | 74 | int half_width = (width + 1) / 2; |
| 75 | rtc::scoped_refptr<I420Buffer> buffer( |
| 76 | // Explicit stride, no padding between rows. |
| 77 | I420Buffer::Create(width, height, width, half_width, half_width)); |
| 78 | size_t size_y = static_cast<size_t>(width) * height; |
| 79 | size_t size_uv = static_cast<size_t>(half_width) * ((height + 1) / 2); |
| 80 | |
| 81 | if (fread(buffer->MutableDataY(), 1, size_y, f) < size_y) |
| 82 | return nullptr; |
| 83 | if (fread(buffer->MutableDataU(), 1, size_uv, f) < size_uv) |
| 84 | return nullptr; |
| 85 | if (fread(buffer->MutableDataV(), 1, size_uv, f) < size_uv) |
| 86 | return nullptr; |
| 87 | return buffer; |
| 88 | } |
| 89 | |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 90 | } // namespace test |
| 91 | } // namespace webrtc |