blob: 4f6bd86530287f035ecc8aab9a60ce1cc68d8129 [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
Ilya Nikolaevskiy0660cee2019-11-19 14:57:57 +010063VideoFrame::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 10:01:49 -0700160VideoFrame::Builder::Builder() = default;
161
162VideoFrame::Builder::~Builder() = default;
163
164VideoFrame VideoFrame::Builder::build() {
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100165 RTC_CHECK(video_frame_buffer_ != nullptr);
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100166 return VideoFrame(id_, video_frame_buffer_, timestamp_us_, timestamp_rtp_,
Chen Xingf00bf422019-06-20 10:05:55 +0200167 ntp_time_ms_, rotation_, color_space_, update_rect_,
Markus Handelle6eded32019-11-15 16:18:21 +0100168 packet_infos_, encoded_frame_buffer_);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700169}
170
171VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer(
172 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
173 video_frame_buffer_ = buffer;
174 return *this;
175}
176
177VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms(
178 int64_t timestamp_ms) {
179 timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec;
180 return *this;
181}
182
183VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us(
184 int64_t timestamp_us) {
185 timestamp_us_ = timestamp_us;
186 return *this;
187}
188
189VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp(
190 uint32_t timestamp_rtp) {
191 timestamp_rtp_ = timestamp_rtp;
192 return *this;
193}
194
195VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) {
196 ntp_time_ms_ = ntp_time_ms;
197 return *this;
198}
199
200VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) {
201 rotation_ = rotation;
202 return *this;
203}
204
205VideoFrame::Builder& VideoFrame::Builder::set_color_space(
Danil Chapovalovb7698942019-02-05 11:32:19 +0100206 const absl::optional<ColorSpace>& color_space) {
Emircan Uysaler800787f2018-07-16 10:01:49 -0700207 color_space_ = color_space;
208 return *this;
209}
210
Johannes Kron4749e4e2018-11-21 10:18:18 +0100211VideoFrame::Builder& VideoFrame::Builder::set_color_space(
212 const ColorSpace* color_space) {
213 color_space_ =
214 color_space ? absl::make_optional(*color_space) : absl::nullopt;
Johannes Kronfbf16832018-11-05 16:13:02 +0100215 return *this;
216}
217
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100218VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) {
219 id_ = id;
220 return *this;
221}
222
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100223VideoFrame::Builder& VideoFrame::Builder::set_update_rect(
224 const VideoFrame::UpdateRect& update_rect) {
225 update_rect_ = update_rect;
226 return *this;
227}
228
Chen Xingf00bf422019-06-20 10:05:55 +0200229VideoFrame::Builder& VideoFrame::Builder::set_packet_infos(
230 RtpPacketInfos packet_infos) {
231 packet_infos_ = std::move(packet_infos);
232 return *this;
233}
234
Markus Handelle6eded32019-11-15 16:18:21 +0100235VideoFrame::Builder& VideoFrame::Builder::set_encoded_video_frame_buffer(
236 rtc::scoped_refptr<VideoFrame::EncodedVideoFrameBuffer>
237 encoded_frame_buffer) {
238 encoded_frame_buffer_ = std::move(encoded_frame_buffer);
239 return *this;
240}
241
nisseaf916892017-01-10 07:44:26 -0800242VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
243 webrtc::VideoRotation rotation,
244 int64_t timestamp_us)
245 : video_frame_buffer_(buffer),
246 timestamp_rtp_(0),
247 ntp_time_ms_(0),
248 timestamp_us_(timestamp_us),
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100249 rotation_(rotation) {}
nisseaf916892017-01-10 07:44:26 -0800250
251VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +0200252 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -0800253 int64_t render_time_ms,
254 VideoRotation rotation)
255 : video_frame_buffer_(buffer),
Niels Möller2ac64462018-06-11 11:14:32 +0200256 timestamp_rtp_(timestamp_rtp),
nisseaf916892017-01-10 07:44:26 -0800257 ntp_time_ms_(0),
258 timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec),
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100259 rotation_(rotation) {
nisseaf916892017-01-10 07:44:26 -0800260 RTC_DCHECK(buffer);
261}
262
Markus Handelle6eded32019-11-15 16:18:21 +0100263VideoFrame::VideoFrame(
264 uint16_t id,
265 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
266 int64_t timestamp_us,
267 uint32_t timestamp_rtp,
268 int64_t ntp_time_ms,
269 VideoRotation rotation,
270 const absl::optional<ColorSpace>& color_space,
271 const absl::optional<UpdateRect>& update_rect,
272 RtpPacketInfos packet_infos,
273 const rtc::scoped_refptr<EncodedVideoFrameBuffer>& encoded_frame_buffer)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100274 : id_(id),
275 video_frame_buffer_(buffer),
Markus Handelle6eded32019-11-15 16:18:21 +0100276 encoded_frame_buffer_(encoded_frame_buffer),
Emircan Uysaler800787f2018-07-16 10:01:49 -0700277 timestamp_rtp_(timestamp_rtp),
278 ntp_time_ms_(ntp_time_ms),
279 timestamp_us_(timestamp_us),
280 rotation_(rotation),
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100281 color_space_(color_space),
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100282 update_rect_(update_rect),
Chen Xingf00bf422019-06-20 10:05:55 +0200283 packet_infos_(std::move(packet_infos)) {
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100284 if (update_rect_) {
285 RTC_DCHECK_GE(update_rect_->offset_x, 0);
286 RTC_DCHECK_GE(update_rect_->offset_y, 0);
287 RTC_DCHECK_LE(update_rect_->offset_x + update_rect_->width, width());
288 RTC_DCHECK_LE(update_rect_->offset_y + update_rect_->height, height());
289 }
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100290}
Emircan Uysaler800787f2018-07-16 10:01:49 -0700291
nisseaf916892017-01-10 07:44:26 -0800292VideoFrame::~VideoFrame() = default;
293
294VideoFrame::VideoFrame(const VideoFrame&) = default;
295VideoFrame::VideoFrame(VideoFrame&&) = default;
296VideoFrame& VideoFrame::operator=(const VideoFrame&) = default;
297VideoFrame& VideoFrame::operator=(VideoFrame&&) = default;
298
299int VideoFrame::width() const {
300 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
301}
302
303int VideoFrame::height() const {
304 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
305}
306
kthelgason2bc68642017-02-07 07:02:22 -0800307uint32_t VideoFrame::size() const {
308 return width() * height();
309}
310
nisseaf916892017-01-10 07:44:26 -0800311rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
312 return video_frame_buffer_;
313}
314
Ilya Nikolaevskiy4fc08552019-06-05 15:59:12 +0200315void VideoFrame::set_video_frame_buffer(
316 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
317 RTC_CHECK(buffer);
318 video_frame_buffer_ = buffer;
319}
320
nisseaf916892017-01-10 07:44:26 -0800321int64_t VideoFrame::render_time_ms() const {
322 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
323}
324
Markus Handelle6eded32019-11-15 16:18:21 +0100325void VideoFrame::set_encoded_video_frame_buffer(
326 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer) {
327 encoded_frame_buffer_ = std::move(encoded_frame_buffer);
328}
329
330rtc::scoped_refptr<VideoFrame::EncodedVideoFrameBuffer>
331VideoFrame::encoded_video_frame_buffer() const {
332 return encoded_frame_buffer_;
333}
334
nisseaf916892017-01-10 07:44:26 -0800335} // namespace webrtc