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