blob: 6113747c578c44fe072265d08645a768ae6bc126 [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
nisseaf916892017-01-10 07:44:26 -080011#include <stdio.h>
12#include <string.h>
13
14#include "webrtc/api/video/i420_buffer.h"
15#include "webrtc/api/video/video_frame.h"
Niels Möller739fcb92016-02-29 13:11:45 +010016#include "webrtc/test/frame_utils.h"
Niels Möller739fcb92016-02-29 13:11:45 +010017
18namespace webrtc {
19namespace test {
20
21bool EqualPlane(const uint8_t* data1,
22 const uint8_t* data2,
nisse26acec42016-04-15 03:43:39 -070023 int stride1,
24 int stride2,
Niels Möller739fcb92016-02-29 13:11:45 +010025 int width,
26 int height) {
27 for (int y = 0; y < height; ++y) {
28 if (memcmp(data1, data2, width) != 0)
29 return false;
nisse26acec42016-04-15 03:43:39 -070030 data1 += stride1;
31 data2 += stride2;
Niels Möller739fcb92016-02-29 13:11:45 +010032 }
33 return true;
34}
nisse26acec42016-04-15 03:43:39 -070035
Niels Möller739fcb92016-02-29 13:11:45 +010036bool FramesEqual(const webrtc::VideoFrame& f1, const webrtc::VideoFrame& f2) {
nisse26acec42016-04-15 03:43:39 -070037 if (f1.timestamp() != f2.timestamp() ||
Niels Möller739fcb92016-02-29 13:11:45 +010038 f1.ntp_time_ms() != f2.ntp_time_ms() ||
39 f1.render_time_ms() != f2.render_time_ms()) {
40 return false;
41 }
nisse26acec42016-04-15 03:43:39 -070042 return FrameBufsEqual(f1.video_frame_buffer(), f2.video_frame_buffer());
Niels Möller739fcb92016-02-29 13:11:45 +010043}
44
nisse7cc9cc02016-03-29 23:44:19 -070045bool FrameBufsEqual(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f1,
46 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f2) {
nisse26acec42016-04-15 03:43:39 -070047 if (f1 == f2) {
48 return true;
49 }
50 // Exlude nullptr (except if both are nullptr, as above)
51 if (!f1 || !f2) {
52 return false;
53 }
54
55 if (f1->width() != f2->width() || f1->height() != f2->height()) {
56 return false;
57 }
58 // Exclude native handle
59 if (f1->native_handle()) {
60 return f1->native_handle() == f2->native_handle();
61 }
62
63 if (f2->native_handle()) {
nisse7cc9cc02016-03-29 23:44:19 -070064 return false;
65 }
66 const int half_width = (f1->width() + 1) / 2;
67 const int half_height = (f1->height() + 1) / 2;
nisse06176e42016-04-18 05:34:40 -070068 return EqualPlane(f1->DataY(), f2->DataY(),
69 f1->StrideY(), f2->StrideY(),
nisse26acec42016-04-15 03:43:39 -070070 f1->width(), f1->height()) &&
nisse06176e42016-04-18 05:34:40 -070071 EqualPlane(f1->DataU(), f2->DataU(),
72 f1->StrideU(), f2->StrideU(),
nisse26acec42016-04-15 03:43:39 -070073 half_width, half_height) &&
nisse06176e42016-04-18 05:34:40 -070074 EqualPlane(f1->DataV(), f2->DataV(),
75 f1->StrideV(), f2->StrideV(),
nisse26acec42016-04-15 03:43:39 -070076 half_width, half_height);
nisse7cc9cc02016-03-29 23:44:19 -070077}
78
nisse115bd152016-09-30 04:14:07 -070079rtc::scoped_refptr<I420Buffer> ReadI420Buffer(int width, int height, FILE *f) {
80 int half_width = (width + 1) / 2;
81 rtc::scoped_refptr<I420Buffer> buffer(
82 // Explicit stride, no padding between rows.
83 I420Buffer::Create(width, height, width, half_width, half_width));
84 size_t size_y = static_cast<size_t>(width) * height;
85 size_t size_uv = static_cast<size_t>(half_width) * ((height + 1) / 2);
86
87 if (fread(buffer->MutableDataY(), 1, size_y, f) < size_y)
88 return nullptr;
89 if (fread(buffer->MutableDataU(), 1, size_uv, f) < size_uv)
90 return nullptr;
91 if (fread(buffer->MutableDataV(), 1, size_uv, f) < size_uv)
92 return nullptr;
93 return buffer;
94}
95
Niels Möller739fcb92016-02-29 13:11:45 +010096} // namespace test
97} // namespace webrtc