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