blob: 2ef8d8d196ddee4db905fa12f15bd8b01d94e58f [file] [log] [blame]
nisseaf916892017-01-10 07:44:26 -08001/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/video/video_frame.h"
nisseaf916892017-01-10 07:44:26 -080012
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +010013#include <algorithm>
Chen Xingf00bf422019-06-20 10:05:55 +020014#include <utility>
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +010015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/time_utils.h"
nisseaf916892017-01-10 07:44:26 -080018
19namespace webrtc {
20
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +010021void VideoFrame::UpdateRect::Union(const UpdateRect& other) {
22 if (other.IsEmpty())
23 return;
24 if (IsEmpty()) {
25 *this = other;
26 return;
27 }
28 int right = std::max(offset_x + width, other.offset_x + other.width);
29 int bottom = std::max(offset_y + height, other.offset_y + other.height);
30 offset_x = std::min(offset_x, other.offset_x);
31 offset_y = std::min(offset_y, other.offset_y);
32 width = right - offset_x;
33 height = bottom - offset_y;
34 RTC_DCHECK_GT(width, 0);
35 RTC_DCHECK_GT(height, 0);
36}
37
38void VideoFrame::UpdateRect::Intersect(const UpdateRect& other) {
39 if (other.IsEmpty() || IsEmpty()) {
40 MakeEmptyUpdate();
41 return;
42 }
43
44 int right = std::min(offset_x + width, other.offset_x + other.width);
45 int bottom = std::min(offset_y + height, other.offset_y + other.height);
46 offset_x = std::max(offset_x, other.offset_x);
47 offset_y = std::max(offset_y, other.offset_y);
48 width = right - offset_x;
49 height = bottom - offset_y;
50 if (width <= 0 || height <= 0) {
51 MakeEmptyUpdate();
52 }
53}
54
55void VideoFrame::UpdateRect::MakeEmptyUpdate() {
56 width = height = offset_x = offset_y = 0;
57}
58
59bool VideoFrame::UpdateRect::IsEmpty() const {
60 return width == 0 && height == 0;
61}
62
Emircan Uysaler800787f2018-07-16 10:01:49 -070063VideoFrame::Builder::Builder() = default;
64
65VideoFrame::Builder::~Builder() = default;
66
67VideoFrame VideoFrame::Builder::build() {
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010068 RTC_CHECK(video_frame_buffer_ != nullptr);
Artem Titov1ebfb6a2019-01-03 23:49:37 +010069 return VideoFrame(id_, video_frame_buffer_, timestamp_us_, timestamp_rtp_,
Chen Xingf00bf422019-06-20 10:05:55 +020070 ntp_time_ms_, rotation_, color_space_, update_rect_,
71 packet_infos_);
Emircan Uysaler800787f2018-07-16 10:01:49 -070072}
73
74VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer(
75 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
76 video_frame_buffer_ = buffer;
77 return *this;
78}
79
80VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms(
81 int64_t timestamp_ms) {
82 timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec;
83 return *this;
84}
85
86VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us(
87 int64_t timestamp_us) {
88 timestamp_us_ = timestamp_us;
89 return *this;
90}
91
92VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp(
93 uint32_t timestamp_rtp) {
94 timestamp_rtp_ = timestamp_rtp;
95 return *this;
96}
97
98VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) {
99 ntp_time_ms_ = ntp_time_ms;
100 return *this;
101}
102
103VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) {
104 rotation_ = rotation;
105 return *this;
106}
107
108VideoFrame::Builder& VideoFrame::Builder::set_color_space(
Danil Chapovalovb7698942019-02-05 11:32:19 +0100109 const absl::optional<ColorSpace>& color_space) {
Emircan Uysaler800787f2018-07-16 10:01:49 -0700110 color_space_ = color_space;
111 return *this;
112}
113
Johannes Kron4749e4e2018-11-21 10:18:18 +0100114VideoFrame::Builder& VideoFrame::Builder::set_color_space(
115 const ColorSpace* color_space) {
116 color_space_ =
117 color_space ? absl::make_optional(*color_space) : absl::nullopt;
Johannes Kronfbf16832018-11-05 16:13:02 +0100118 return *this;
119}
120
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100121VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) {
122 id_ = id;
123 return *this;
124}
125
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100126VideoFrame::Builder& VideoFrame::Builder::set_update_rect(
127 const VideoFrame::UpdateRect& update_rect) {
128 update_rect_ = update_rect;
129 return *this;
130}
131
Chen Xingf00bf422019-06-20 10:05:55 +0200132VideoFrame::Builder& VideoFrame::Builder::set_packet_infos(
133 RtpPacketInfos packet_infos) {
134 packet_infos_ = std::move(packet_infos);
135 return *this;
136}
137
nisseaf916892017-01-10 07:44:26 -0800138VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
139 webrtc::VideoRotation rotation,
140 int64_t timestamp_us)
141 : video_frame_buffer_(buffer),
142 timestamp_rtp_(0),
143 ntp_time_ms_(0),
144 timestamp_us_(timestamp_us),
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100145 rotation_(rotation),
146 update_rect_{0, 0, buffer->width(), buffer->height()} {}
nisseaf916892017-01-10 07:44:26 -0800147
148VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +0200149 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -0800150 int64_t render_time_ms,
151 VideoRotation rotation)
152 : video_frame_buffer_(buffer),
Niels Möller2ac64462018-06-11 11:14:32 +0200153 timestamp_rtp_(timestamp_rtp),
nisseaf916892017-01-10 07:44:26 -0800154 ntp_time_ms_(0),
155 timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec),
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100156 rotation_(rotation),
157 update_rect_{0, 0, buffer->width(), buffer->height()} {
nisseaf916892017-01-10 07:44:26 -0800158 RTC_DCHECK(buffer);
159}
160
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100161VideoFrame::VideoFrame(uint16_t id,
162 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
163 int64_t timestamp_us,
164 uint32_t timestamp_rtp,
165 int64_t ntp_time_ms,
166 VideoRotation rotation,
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100167 const absl::optional<ColorSpace>& color_space,
Chen Xingf00bf422019-06-20 10:05:55 +0200168 const absl::optional<UpdateRect>& update_rect,
169 RtpPacketInfos packet_infos)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100170 : id_(id),
171 video_frame_buffer_(buffer),
Emircan Uysaler800787f2018-07-16 10:01:49 -0700172 timestamp_rtp_(timestamp_rtp),
173 ntp_time_ms_(ntp_time_ms),
174 timestamp_us_(timestamp_us),
175 rotation_(rotation),
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100176 color_space_(color_space),
177 update_rect_(update_rect.value_or(UpdateRect{
Chen Xingf00bf422019-06-20 10:05:55 +0200178 0, 0, video_frame_buffer_->width(), video_frame_buffer_->height()})),
179 packet_infos_(std::move(packet_infos)) {
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100180 RTC_DCHECK_GE(update_rect_.offset_x, 0);
181 RTC_DCHECK_GE(update_rect_.offset_y, 0);
182 RTC_DCHECK_LE(update_rect_.offset_x + update_rect_.width, width());
183 RTC_DCHECK_LE(update_rect_.offset_y + update_rect_.height, height());
184}
Emircan Uysaler800787f2018-07-16 10:01:49 -0700185
nisseaf916892017-01-10 07:44:26 -0800186VideoFrame::~VideoFrame() = default;
187
188VideoFrame::VideoFrame(const VideoFrame&) = default;
189VideoFrame::VideoFrame(VideoFrame&&) = default;
190VideoFrame& VideoFrame::operator=(const VideoFrame&) = default;
191VideoFrame& VideoFrame::operator=(VideoFrame&&) = default;
192
193int VideoFrame::width() const {
194 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
195}
196
197int VideoFrame::height() const {
198 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
199}
200
kthelgason2bc68642017-02-07 07:02:22 -0800201uint32_t VideoFrame::size() const {
202 return width() * height();
203}
204
nisseaf916892017-01-10 07:44:26 -0800205rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
206 return video_frame_buffer_;
207}
208
Ilya Nikolaevskiy4fc08552019-06-05 15:59:12 +0200209void VideoFrame::set_video_frame_buffer(
210 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
211 RTC_CHECK(buffer);
212 video_frame_buffer_ = buffer;
213}
214
nisseaf916892017-01-10 07:44:26 -0800215int64_t VideoFrame::render_time_ms() const {
216 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
217}
218
219} // namespace webrtc