blob: b280de1ad1ad24c22f4e7fcecd3e24e5f2ee0c01 [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "test/frame_utils.h"
12
nisseaf916892017-01-10 07:44:26 -080013#include <stdio.h>
14#include <string.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/video/i420_buffer.h"
Artem Titov9b731592022-10-07 15:01:32 +020017#include "api/video/nv12_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/video_frame.h"
Niels Möller739fcb92016-02-29 13:11:45 +010019
20namespace webrtc {
21namespace test {
22
23bool EqualPlane(const uint8_t* data1,
24 const uint8_t* data2,
nisse26acec42016-04-15 03:43:39 -070025 int stride1,
26 int stride2,
Niels Möller739fcb92016-02-29 13:11:45 +010027 int width,
28 int height) {
29 for (int y = 0; y < height; ++y) {
30 if (memcmp(data1, data2, width) != 0)
31 return false;
nisse26acec42016-04-15 03:43:39 -070032 data1 += stride1;
33 data2 += stride2;
Niels Möller739fcb92016-02-29 13:11:45 +010034 }
35 return true;
36}
nisse26acec42016-04-15 03:43:39 -070037
Niels Möller739fcb92016-02-29 13:11:45 +010038bool FramesEqual(const webrtc::VideoFrame& f1, const webrtc::VideoFrame& f2) {
nisse26acec42016-04-15 03:43:39 -070039 if (f1.timestamp() != f2.timestamp() ||
Niels Möller739fcb92016-02-29 13:11:45 +010040 f1.ntp_time_ms() != f2.ntp_time_ms() ||
41 f1.render_time_ms() != f2.render_time_ms()) {
42 return false;
43 }
nisse26acec42016-04-15 03:43:39 -070044 return FrameBufsEqual(f1.video_frame_buffer(), f2.video_frame_buffer());
Niels Möller739fcb92016-02-29 13:11:45 +010045}
46
nisse7cc9cc02016-03-29 23:44:19 -070047bool FrameBufsEqual(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f1,
48 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f2) {
nisse26acec42016-04-15 03:43:39 -070049 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 Jedvert90e31902017-06-07 11:32:50 +020057 if (f1->width() != f2->width() || f1->height() != f2->height() ||
58 f1->type() != f2->type()) {
nisse26acec42016-04-15 03:43:39 -070059 return false;
60 }
nisse26acec42016-04-15 03:43:39 -070061
Magnus Jedvert90e31902017-06-07 11:32:50 +020062 rtc::scoped_refptr<webrtc::I420BufferInterface> f1_i420 = f1->ToI420();
63 rtc::scoped_refptr<webrtc::I420BufferInterface> f2_i420 = f2->ToI420();
Yves Gerey665174f2018-06-19 15:03:05 +020064 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());
nisse7cc9cc02016-03-29 23:44:19 -070072}
73
Yves Gerey665174f2018-06-19 15:03:05 +020074rtc::scoped_refptr<I420Buffer> ReadI420Buffer(int width, int height, FILE* f) {
nisse115bd152016-09-30 04:14:07 -070075 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 Titov9b731592022-10-07 15:01:32 +020091rtc::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öller739fcb92016-02-29 13:11:45 +0100103} // namespace test
104} // namespace webrtc