blob: b332b8fb918d8ff041e7090fae6b705631994d08 [file] [log] [blame]
Niels Möller739fcb92016-02-29 13:11:45 +01001/*
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
14namespace webrtc {
15namespace test {
16
17bool EqualPlane(const uint8_t* data1,
18 const uint8_t* data2,
nisse26acec42016-04-15 03:43:39 -070019 int stride1,
20 int stride2,
Niels Möller739fcb92016-02-29 13:11:45 +010021 int width,
22 int height) {
23 for (int y = 0; y < height; ++y) {
24 if (memcmp(data1, data2, width) != 0)
25 return false;
nisse26acec42016-04-15 03:43:39 -070026 data1 += stride1;
27 data2 += stride2;
Niels Möller739fcb92016-02-29 13:11:45 +010028 }
29 return true;
30}
nisse26acec42016-04-15 03:43:39 -070031
Niels Möller739fcb92016-02-29 13:11:45 +010032bool FramesEqual(const webrtc::VideoFrame& f1, const webrtc::VideoFrame& f2) {
nisse26acec42016-04-15 03:43:39 -070033 if (f1.timestamp() != f2.timestamp() ||
Niels Möller739fcb92016-02-29 13:11:45 +010034 f1.ntp_time_ms() != f2.ntp_time_ms() ||
35 f1.render_time_ms() != f2.render_time_ms()) {
36 return false;
37 }
nisse26acec42016-04-15 03:43:39 -070038 return FrameBufsEqual(f1.video_frame_buffer(), f2.video_frame_buffer());
Niels Möller739fcb92016-02-29 13:11:45 +010039}
40
nisse7cc9cc02016-03-29 23:44:19 -070041bool FrameBufsEqual(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f1,
42 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f2) {
nisse26acec42016-04-15 03:43:39 -070043 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()) {
nisse7cc9cc02016-03-29 23:44:19 -070060 return false;
61 }
62 const int half_width = (f1->width() + 1) / 2;
63 const int half_height = (f1->height() + 1) / 2;
nisse06176e42016-04-18 05:34:40 -070064 return EqualPlane(f1->DataY(), f2->DataY(),
65 f1->StrideY(), f2->StrideY(),
nisse26acec42016-04-15 03:43:39 -070066 f1->width(), f1->height()) &&
nisse06176e42016-04-18 05:34:40 -070067 EqualPlane(f1->DataU(), f2->DataU(),
68 f1->StrideU(), f2->StrideU(),
nisse26acec42016-04-15 03:43:39 -070069 half_width, half_height) &&
nisse06176e42016-04-18 05:34:40 -070070 EqualPlane(f1->DataV(), f2->DataV(),
71 f1->StrideV(), f2->StrideV(),
nisse26acec42016-04-15 03:43:39 -070072 half_width, half_height);
nisse7cc9cc02016-03-29 23:44:19 -070073}
74
nisse115bd152016-09-30 04:14:07 -070075rtc::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öller739fcb92016-02-29 13:11:45 +010092} // namespace test
93} // namespace webrtc