nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/video/video_frame.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 12 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 13 | #include <algorithm> |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 14 | #include <utility> |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "rtc_base/time_utils.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 21 | void 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 | |
| 38 | void 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 | |
| 55 | void VideoFrame::UpdateRect::MakeEmptyUpdate() { |
| 56 | width = height = offset_x = offset_y = 0; |
| 57 | } |
| 58 | |
| 59 | bool VideoFrame::UpdateRect::IsEmpty() const { |
| 60 | return width == 0 && height == 0; |
| 61 | } |
| 62 | |
Ilya Nikolaevskiy | 0660cee | 2019-11-19 14:57:57 +0100 | [diff] [blame^] | 63 | VideoFrame::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 Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 160 | VideoFrame::Builder::Builder() = default; |
| 161 | |
| 162 | VideoFrame::Builder::~Builder() = default; |
| 163 | |
| 164 | VideoFrame VideoFrame::Builder::build() { |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 165 | RTC_CHECK(video_frame_buffer_ != nullptr); |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 166 | return VideoFrame(id_, video_frame_buffer_, timestamp_us_, timestamp_rtp_, |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 167 | ntp_time_ms_, rotation_, color_space_, update_rect_, |
Markus Handell | e6eded3 | 2019-11-15 16:18:21 +0100 | [diff] [blame] | 168 | packet_infos_, encoded_frame_buffer_); |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer( |
| 172 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer) { |
| 173 | video_frame_buffer_ = buffer; |
| 174 | return *this; |
| 175 | } |
| 176 | |
| 177 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms( |
| 178 | int64_t timestamp_ms) { |
| 179 | timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec; |
| 180 | return *this; |
| 181 | } |
| 182 | |
| 183 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us( |
| 184 | int64_t timestamp_us) { |
| 185 | timestamp_us_ = timestamp_us; |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp( |
| 190 | uint32_t timestamp_rtp) { |
| 191 | timestamp_rtp_ = timestamp_rtp; |
| 192 | return *this; |
| 193 | } |
| 194 | |
| 195 | VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) { |
| 196 | ntp_time_ms_ = ntp_time_ms; |
| 197 | return *this; |
| 198 | } |
| 199 | |
| 200 | VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) { |
| 201 | rotation_ = rotation; |
| 202 | return *this; |
| 203 | } |
| 204 | |
| 205 | VideoFrame::Builder& VideoFrame::Builder::set_color_space( |
Danil Chapovalov | b769894 | 2019-02-05 11:32:19 +0100 | [diff] [blame] | 206 | const absl::optional<ColorSpace>& color_space) { |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 207 | color_space_ = color_space; |
| 208 | return *this; |
| 209 | } |
| 210 | |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 211 | VideoFrame::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 Kron | fbf1683 | 2018-11-05 16:13:02 +0100 | [diff] [blame] | 215 | return *this; |
| 216 | } |
| 217 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 218 | VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) { |
| 219 | id_ = id; |
| 220 | return *this; |
| 221 | } |
| 222 | |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 223 | VideoFrame::Builder& VideoFrame::Builder::set_update_rect( |
| 224 | const VideoFrame::UpdateRect& update_rect) { |
| 225 | update_rect_ = update_rect; |
| 226 | return *this; |
| 227 | } |
| 228 | |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 229 | VideoFrame::Builder& VideoFrame::Builder::set_packet_infos( |
| 230 | RtpPacketInfos packet_infos) { |
| 231 | packet_infos_ = std::move(packet_infos); |
| 232 | return *this; |
| 233 | } |
| 234 | |
Markus Handell | e6eded3 | 2019-11-15 16:18:21 +0100 | [diff] [blame] | 235 | VideoFrame::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 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 242 | VideoFrame::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 Nikolaevskiy | 9560d7d | 2019-10-30 11:19:47 +0100 | [diff] [blame] | 249 | rotation_(rotation) {} |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 250 | |
| 251 | VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
Niels Möller | 2ac6446 | 2018-06-11 11:14:32 +0200 | [diff] [blame] | 252 | uint32_t timestamp_rtp, |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 253 | int64_t render_time_ms, |
| 254 | VideoRotation rotation) |
| 255 | : video_frame_buffer_(buffer), |
Niels Möller | 2ac6446 | 2018-06-11 11:14:32 +0200 | [diff] [blame] | 256 | timestamp_rtp_(timestamp_rtp), |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 257 | ntp_time_ms_(0), |
| 258 | timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec), |
Ilya Nikolaevskiy | 9560d7d | 2019-10-30 11:19:47 +0100 | [diff] [blame] | 259 | rotation_(rotation) { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 260 | RTC_DCHECK(buffer); |
| 261 | } |
| 262 | |
Markus Handell | e6eded3 | 2019-11-15 16:18:21 +0100 | [diff] [blame] | 263 | VideoFrame::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 Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 274 | : id_(id), |
| 275 | video_frame_buffer_(buffer), |
Markus Handell | e6eded3 | 2019-11-15 16:18:21 +0100 | [diff] [blame] | 276 | encoded_frame_buffer_(encoded_frame_buffer), |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 277 | timestamp_rtp_(timestamp_rtp), |
| 278 | ntp_time_ms_(ntp_time_ms), |
| 279 | timestamp_us_(timestamp_us), |
| 280 | rotation_(rotation), |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 281 | color_space_(color_space), |
Ilya Nikolaevskiy | 9560d7d | 2019-10-30 11:19:47 +0100 | [diff] [blame] | 282 | update_rect_(update_rect), |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 283 | packet_infos_(std::move(packet_infos)) { |
Ilya Nikolaevskiy | 9560d7d | 2019-10-30 11:19:47 +0100 | [diff] [blame] | 284 | 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 Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 290 | } |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 291 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 292 | VideoFrame::~VideoFrame() = default; |
| 293 | |
| 294 | VideoFrame::VideoFrame(const VideoFrame&) = default; |
| 295 | VideoFrame::VideoFrame(VideoFrame&&) = default; |
| 296 | VideoFrame& VideoFrame::operator=(const VideoFrame&) = default; |
| 297 | VideoFrame& VideoFrame::operator=(VideoFrame&&) = default; |
| 298 | |
| 299 | int VideoFrame::width() const { |
| 300 | return video_frame_buffer_ ? video_frame_buffer_->width() : 0; |
| 301 | } |
| 302 | |
| 303 | int VideoFrame::height() const { |
| 304 | return video_frame_buffer_ ? video_frame_buffer_->height() : 0; |
| 305 | } |
| 306 | |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 307 | uint32_t VideoFrame::size() const { |
| 308 | return width() * height(); |
| 309 | } |
| 310 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 311 | rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const { |
| 312 | return video_frame_buffer_; |
| 313 | } |
| 314 | |
Ilya Nikolaevskiy | 4fc0855 | 2019-06-05 15:59:12 +0200 | [diff] [blame] | 315 | void VideoFrame::set_video_frame_buffer( |
| 316 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer) { |
| 317 | RTC_CHECK(buffer); |
| 318 | video_frame_buffer_ = buffer; |
| 319 | } |
| 320 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 321 | int64_t VideoFrame::render_time_ms() const { |
| 322 | return timestamp_us() / rtc::kNumMicrosecsPerMillisec; |
| 323 | } |
| 324 | |
Markus Handell | e6eded3 | 2019-11-15 16:18:21 +0100 | [diff] [blame] | 325 | void 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 | |
| 330 | rtc::scoped_refptr<VideoFrame::EncodedVideoFrameBuffer> |
| 331 | VideoFrame::encoded_video_frame_buffer() const { |
| 332 | return encoded_frame_buffer_; |
| 333 | } |
| 334 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 335 | } // namespace webrtc |