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 "test/frame_utils.h"
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "api/video/i420_buffer.h"
17 #include "api/video/nv12_buffer.h"
18 #include "api/video/video_frame.h"
19
20 namespace webrtc {
21 namespace test {
22
EqualPlane(const uint8_t * data1,const uint8_t * data2,int stride1,int stride2,int width,int height)23 bool EqualPlane(const uint8_t* data1,
24 const uint8_t* data2,
25 int stride1,
26 int stride2,
27 int width,
28 int height) {
29 for (int y = 0; y < height; ++y) {
30 if (memcmp(data1, data2, width) != 0)
31 return false;
32 data1 += stride1;
33 data2 += stride2;
34 }
35 return true;
36 }
37
FramesEqual(const webrtc::VideoFrame & f1,const webrtc::VideoFrame & f2)38 bool FramesEqual(const webrtc::VideoFrame& f1, const webrtc::VideoFrame& f2) {
39 if (f1.timestamp() != f2.timestamp() ||
40 f1.ntp_time_ms() != f2.ntp_time_ms() ||
41 f1.render_time_ms() != f2.render_time_ms()) {
42 return false;
43 }
44 return FrameBufsEqual(f1.video_frame_buffer(), f2.video_frame_buffer());
45 }
46
FrameBufsEqual(const rtc::scoped_refptr<webrtc::VideoFrameBuffer> & f1,const rtc::scoped_refptr<webrtc::VideoFrameBuffer> & f2)47 bool FrameBufsEqual(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f1,
48 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& f2) {
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
57 if (f1->width() != f2->width() || f1->height() != f2->height() ||
58 f1->type() != f2->type()) {
59 return false;
60 }
61
62 rtc::scoped_refptr<webrtc::I420BufferInterface> f1_i420 = f1->ToI420();
63 rtc::scoped_refptr<webrtc::I420BufferInterface> f2_i420 = f2->ToI420();
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());
72 }
73
ReadI420Buffer(int width,int height,FILE * f)74 rtc::scoped_refptr<I420Buffer> ReadI420Buffer(int width, int height, FILE* f) {
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
ReadNV12Buffer(int width,int height,FILE * f)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
103 } // namespace test
104 } // namespace webrtc
105