blob: fd975dfd8738b10505a1b0652196ad009438f408 [file] [log] [blame]
nisseaf916892017-01-10 15:44:261/*
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 04:47:3111#include "api/video/video_frame.h"
nisseaf916892017-01-10 15:44:2612
Ilya Nikolaevskiy71aee3a2019-02-18 12:01:2613#include <algorithm>
Chen Xingf00bf422019-06-20 08:05:5514#include <utility>
Ilya Nikolaevskiy71aee3a2019-02-18 12:01:2615
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0017#include "rtc_base/time_utils.h"
nisseaf916892017-01-10 15:44:2618
19namespace webrtc {
20
Ilya Nikolaevskiy71aee3a2019-02-18 12:01:2621void 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
Ilya Nikolaevskiy0660cee2019-11-19 13:57:5763VideoFrame::UpdateRect VideoFrame::UpdateRect::ScaleWithFrame(
64 int frame_width,
65 int frame_height,
66 int crop_x,
67 int crop_y,
68 int crop_width,
69 int crop_height,
70 int scaled_width,
71 int scaled_height) const {
72 RTC_DCHECK_GT(frame_width, 0);
73 RTC_DCHECK_GT(frame_height, 0);
74
75 RTC_DCHECK_GT(crop_width, 0);
76 RTC_DCHECK_GT(crop_height, 0);
77
78 RTC_DCHECK_LE(crop_width + crop_x, frame_width);
79 RTC_DCHECK_LE(crop_height + crop_y, frame_height);
80
81 RTC_DCHECK_GT(scaled_width, 0);
82 RTC_DCHECK_GT(scaled_height, 0);
83
84 // Check if update rect is out of the cropped area.
85 if (offset_x + width < crop_x || offset_x > crop_x + crop_width ||
86 offset_y + height < crop_y || offset_y > crop_y + crop_width) {
87 return {0, 0, 0, 0};
88 }
89
90 int x = offset_x - crop_x;
91 int w = width;
92 if (x < 0) {
93 w += x;
94 x = 0;
95 }
96 int y = offset_y - crop_y;
97 int h = height;
98 if (y < 0) {
99 h += y;
100 y = 0;
101 }
102
103 // Lower corner is rounded down.
104 x = x * scaled_width / crop_width;
105 y = y * scaled_height / crop_height;
106 // Upper corner is rounded up.
107 w = (w * scaled_width + crop_width - 1) / crop_width;
108 h = (h * scaled_height + crop_height - 1) / crop_height;
109
110 // Round to full 2x2 blocks due to possible subsampling in the pixel data.
111 if (x % 2) {
112 --x;
113 ++w;
114 }
115 if (y % 2) {
116 --y;
117 ++h;
118 }
119 if (w % 2) {
120 ++w;
121 }
122 if (h % 2) {
123 ++h;
124 }
125
126 // Expand the update rect by 2 pixels in each direction to include any
127 // possible scaling artifacts.
128 if (scaled_width != crop_width || scaled_height != crop_height) {
129 if (x > 0) {
130 x -= 2;
131 w += 2;
132 }
133 if (y > 0) {
134 y -= 2;
135 h += 2;
136 }
137 w += 2;
138 h += 2;
139 }
140
141 // Ensure update rect is inside frame dimensions.
142 if (x + w > scaled_width) {
143 w = scaled_width - x;
144 }
145 if (y + h > scaled_height) {
146 h = scaled_height - y;
147 }
148 RTC_DCHECK_GE(w, 0);
149 RTC_DCHECK_GE(h, 0);
150 if (w == 0 || h == 0) {
151 w = 0;
152 h = 0;
153 x = 0;
154 y = 0;
155 }
156
157 return {x, y, w, h};
158}
159
Emircan Uysaler800787f2018-07-16 17:01:49160VideoFrame::Builder::Builder() = default;
161
162VideoFrame::Builder::~Builder() = default;
163
164VideoFrame VideoFrame::Builder::build() {
Ilya Nikolaevskiy6aca0b72019-02-13 10:55:57165 RTC_CHECK(video_frame_buffer_ != nullptr);
Palak Agarwal617d89a32023-02-06 12:43:46166 return VideoFrame(id_, video_frame_buffer_, timestamp_us_,
henrika7b6f9962023-10-30 10:33:23167 capture_time_identifier_, reference_time_, timestamp_rtp_,
168 ntp_time_ms_, rotation_, color_space_, render_parameters_,
169 update_rect_, packet_infos_);
Emircan Uysaler800787f2018-07-16 17:01:49170}
171
172VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer(
173 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
174 video_frame_buffer_ = buffer;
175 return *this;
176}
177
178VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms(
179 int64_t timestamp_ms) {
180 timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec;
181 return *this;
182}
183
184VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us(
185 int64_t timestamp_us) {
186 timestamp_us_ = timestamp_us;
187 return *this;
188}
189
Palak Agarwalb57053e2023-02-20 12:29:46190VideoFrame::Builder& VideoFrame::Builder::set_capture_time_identifier(
191 const absl::optional<Timestamp>& capture_time_identifier) {
192 capture_time_identifier_ = capture_time_identifier;
Palak Agarwal617d89a32023-02-06 12:43:46193 return *this;
194}
195
henrika7b6f9962023-10-30 10:33:23196VideoFrame::Builder& VideoFrame::Builder::set_reference_time(
197 const absl::optional<Timestamp>& reference_time) {
198 reference_time_ = reference_time;
199 return *this;
200}
201
Emircan Uysaler800787f2018-07-16 17:01:49202VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp(
203 uint32_t timestamp_rtp) {
204 timestamp_rtp_ = timestamp_rtp;
205 return *this;
206}
207
208VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) {
209 ntp_time_ms_ = ntp_time_ms;
210 return *this;
211}
212
213VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) {
214 rotation_ = rotation;
215 return *this;
216}
217
218VideoFrame::Builder& VideoFrame::Builder::set_color_space(
Danil Chapovalovb7698942019-02-05 10:32:19219 const absl::optional<ColorSpace>& color_space) {
Emircan Uysaler800787f2018-07-16 17:01:49220 color_space_ = color_space;
221 return *this;
222}
223
Johannes Kron4749e4e2018-11-21 09:18:18224VideoFrame::Builder& VideoFrame::Builder::set_color_space(
225 const ColorSpace* color_space) {
226 color_space_ =
227 color_space ? absl::make_optional(*color_space) : absl::nullopt;
Johannes Kronfbf16832018-11-05 15:13:02228 return *this;
229}
230
Artem Titov1ebfb6a2019-01-03 22:49:37231VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) {
232 id_ = id;
233 return *this;
234}
235
Ilya Nikolaevskiy6aca0b72019-02-13 10:55:57236VideoFrame::Builder& VideoFrame::Builder::set_update_rect(
Artem Titov5256d8b2019-12-02 09:34:12237 const absl::optional<VideoFrame::UpdateRect>& update_rect) {
Ilya Nikolaevskiy6aca0b72019-02-13 10:55:57238 update_rect_ = update_rect;
239 return *this;
240}
241
Chen Xingf00bf422019-06-20 08:05:55242VideoFrame::Builder& VideoFrame::Builder::set_packet_infos(
243 RtpPacketInfos packet_infos) {
244 packet_infos_ = std::move(packet_infos);
245 return *this;
246}
247
nisseaf916892017-01-10 15:44:26248VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
249 webrtc::VideoRotation rotation,
250 int64_t timestamp_us)
251 : video_frame_buffer_(buffer),
252 timestamp_rtp_(0),
253 ntp_time_ms_(0),
254 timestamp_us_(timestamp_us),
Ilya Nikolaevskiy9560d7d2019-10-30 10:19:47255 rotation_(rotation) {}
nisseaf916892017-01-10 15:44:26256
257VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 09:14:32258 uint32_t timestamp_rtp,
nisseaf916892017-01-10 15:44:26259 int64_t render_time_ms,
260 VideoRotation rotation)
261 : video_frame_buffer_(buffer),
Niels Möller2ac64462018-06-11 09:14:32262 timestamp_rtp_(timestamp_rtp),
nisseaf916892017-01-10 15:44:26263 ntp_time_ms_(0),
264 timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec),
Ilya Nikolaevskiy9560d7d2019-10-30 10:19:47265 rotation_(rotation) {
nisseaf916892017-01-10 15:44:26266 RTC_DCHECK(buffer);
267}
268
Palak Agarwalb57053e2023-02-20 12:29:46269VideoFrame::VideoFrame(uint16_t id,
270 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
271 int64_t timestamp_us,
272 const absl::optional<Timestamp>& capture_time_identifier,
henrika7b6f9962023-10-30 10:33:23273 const absl::optional<Timestamp>& reference_time,
Palak Agarwalb57053e2023-02-20 12:29:46274 uint32_t timestamp_rtp,
275 int64_t ntp_time_ms,
276 VideoRotation rotation,
277 const absl::optional<ColorSpace>& color_space,
278 const RenderParameters& render_parameters,
279 const absl::optional<UpdateRect>& update_rect,
280 RtpPacketInfos packet_infos)
Artem Titov1ebfb6a2019-01-03 22:49:37281 : id_(id),
282 video_frame_buffer_(buffer),
Emircan Uysaler800787f2018-07-16 17:01:49283 timestamp_rtp_(timestamp_rtp),
284 ntp_time_ms_(ntp_time_ms),
285 timestamp_us_(timestamp_us),
Palak Agarwalb57053e2023-02-20 12:29:46286 capture_time_identifier_(capture_time_identifier),
henrika7b6f9962023-10-30 10:33:23287 reference_time_(reference_time),
Emircan Uysaler800787f2018-07-16 17:01:49288 rotation_(rotation),
Ilya Nikolaevskiy6aca0b72019-02-13 10:55:57289 color_space_(color_space),
Johannes Kronbbf639e2022-06-15 10:27:23290 render_parameters_(render_parameters),
Ilya Nikolaevskiy9560d7d2019-10-30 10:19:47291 update_rect_(update_rect),
Chen Xingf00bf422019-06-20 08:05:55292 packet_infos_(std::move(packet_infos)) {
Ilya Nikolaevskiy9560d7d2019-10-30 10:19:47293 if (update_rect_) {
294 RTC_DCHECK_GE(update_rect_->offset_x, 0);
295 RTC_DCHECK_GE(update_rect_->offset_y, 0);
296 RTC_DCHECK_LE(update_rect_->offset_x + update_rect_->width, width());
297 RTC_DCHECK_LE(update_rect_->offset_y + update_rect_->height, height());
298 }
Ilya Nikolaevskiy6aca0b72019-02-13 10:55:57299}
Emircan Uysaler800787f2018-07-16 17:01:49300
nisseaf916892017-01-10 15:44:26301VideoFrame::~VideoFrame() = default;
302
303VideoFrame::VideoFrame(const VideoFrame&) = default;
304VideoFrame::VideoFrame(VideoFrame&&) = default;
305VideoFrame& VideoFrame::operator=(const VideoFrame&) = default;
306VideoFrame& VideoFrame::operator=(VideoFrame&&) = default;
307
308int VideoFrame::width() const {
309 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
310}
311
312int VideoFrame::height() const {
313 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
314}
315
kthelgason2bc68642017-02-07 15:02:22316uint32_t VideoFrame::size() const {
317 return width() * height();
318}
319
nisseaf916892017-01-10 15:44:26320rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
321 return video_frame_buffer_;
322}
323
Ilya Nikolaevskiy4fc08552019-06-05 13:59:12324void VideoFrame::set_video_frame_buffer(
325 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
326 RTC_CHECK(buffer);
327 video_frame_buffer_ = buffer;
328}
329
nisseaf916892017-01-10 15:44:26330int64_t VideoFrame::render_time_ms() const {
331 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
332}
333
nisseaf916892017-01-10 15:44:26334} // namespace webrtc