nisse | af91689 | 2017-01-10 15:44:26 | [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 04:47:31 | [diff] [blame] | 11 | #include "api/video/video_frame.h" |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 12 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 12:01:26 | [diff] [blame] | 13 | #include <algorithm> |
Chen Xing | f00bf42 | 2019-06-20 08:05:55 | [diff] [blame] | 14 | #include <utility> |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 12:01:26 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 17 | #include "rtc_base/time_utils.h" |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 12:01:26 | [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 13:57:57 | [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 17:01:49 | [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 10:55:57 | [diff] [blame] | 165 | RTC_CHECK(video_frame_buffer_ != nullptr); |
Palak Agarwal | 617d89a3 | 2023-02-06 12:43:46 | [diff] [blame] | 166 | return VideoFrame(id_, video_frame_buffer_, timestamp_us_, |
henrika | 7b6f996 | 2023-10-30 10:33:23 | [diff] [blame] | 167 | capture_time_identifier_, reference_time_, timestamp_rtp_, |
| 168 | ntp_time_ms_, rotation_, color_space_, render_parameters_, |
| 169 | update_rect_, packet_infos_); |
Emircan Uysaler | 800787f | 2018-07-16 17:01:49 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer( |
| 173 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer) { |
| 174 | video_frame_buffer_ = buffer; |
| 175 | return *this; |
| 176 | } |
| 177 | |
| 178 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms( |
| 179 | int64_t timestamp_ms) { |
| 180 | timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec; |
| 181 | return *this; |
| 182 | } |
| 183 | |
| 184 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us( |
| 185 | int64_t timestamp_us) { |
| 186 | timestamp_us_ = timestamp_us; |
| 187 | return *this; |
| 188 | } |
| 189 | |
Palak Agarwal | b57053e | 2023-02-20 12:29:46 | [diff] [blame] | 190 | VideoFrame::Builder& VideoFrame::Builder::set_capture_time_identifier( |
| 191 | const absl::optional<Timestamp>& capture_time_identifier) { |
| 192 | capture_time_identifier_ = capture_time_identifier; |
Palak Agarwal | 617d89a3 | 2023-02-06 12:43:46 | [diff] [blame] | 193 | return *this; |
| 194 | } |
| 195 | |
henrika | 7b6f996 | 2023-10-30 10:33:23 | [diff] [blame] | 196 | VideoFrame::Builder& VideoFrame::Builder::set_reference_time( |
| 197 | const absl::optional<Timestamp>& reference_time) { |
| 198 | reference_time_ = reference_time; |
| 199 | return *this; |
| 200 | } |
| 201 | |
Emircan Uysaler | 800787f | 2018-07-16 17:01:49 | [diff] [blame] | 202 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp( |
| 203 | uint32_t timestamp_rtp) { |
| 204 | timestamp_rtp_ = timestamp_rtp; |
| 205 | return *this; |
| 206 | } |
| 207 | |
| 208 | VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) { |
| 209 | ntp_time_ms_ = ntp_time_ms; |
| 210 | return *this; |
| 211 | } |
| 212 | |
| 213 | VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) { |
| 214 | rotation_ = rotation; |
| 215 | return *this; |
| 216 | } |
| 217 | |
| 218 | VideoFrame::Builder& VideoFrame::Builder::set_color_space( |
Danil Chapovalov | b769894 | 2019-02-05 10:32:19 | [diff] [blame] | 219 | const absl::optional<ColorSpace>& color_space) { |
Emircan Uysaler | 800787f | 2018-07-16 17:01:49 | [diff] [blame] | 220 | color_space_ = color_space; |
| 221 | return *this; |
| 222 | } |
| 223 | |
Johannes Kron | 4749e4e | 2018-11-21 09:18:18 | [diff] [blame] | 224 | VideoFrame::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 Kron | fbf1683 | 2018-11-05 15:13:02 | [diff] [blame] | 228 | return *this; |
| 229 | } |
| 230 | |
Artem Titov | 1ebfb6a | 2019-01-03 22:49:37 | [diff] [blame] | 231 | VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) { |
| 232 | id_ = id; |
| 233 | return *this; |
| 234 | } |
| 235 | |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 10:55:57 | [diff] [blame] | 236 | VideoFrame::Builder& VideoFrame::Builder::set_update_rect( |
Artem Titov | 5256d8b | 2019-12-02 09:34:12 | [diff] [blame] | 237 | const absl::optional<VideoFrame::UpdateRect>& update_rect) { |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 10:55:57 | [diff] [blame] | 238 | update_rect_ = update_rect; |
| 239 | return *this; |
| 240 | } |
| 241 | |
Chen Xing | f00bf42 | 2019-06-20 08:05:55 | [diff] [blame] | 242 | VideoFrame::Builder& VideoFrame::Builder::set_packet_infos( |
| 243 | RtpPacketInfos packet_infos) { |
| 244 | packet_infos_ = std::move(packet_infos); |
| 245 | return *this; |
| 246 | } |
| 247 | |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 248 | VideoFrame::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 Nikolaevskiy | 9560d7d | 2019-10-30 10:19:47 | [diff] [blame] | 255 | rotation_(rotation) {} |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 256 | |
| 257 | VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
Niels Möller | 2ac6446 | 2018-06-11 09:14:32 | [diff] [blame] | 258 | uint32_t timestamp_rtp, |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 259 | int64_t render_time_ms, |
| 260 | VideoRotation rotation) |
| 261 | : video_frame_buffer_(buffer), |
Niels Möller | 2ac6446 | 2018-06-11 09:14:32 | [diff] [blame] | 262 | timestamp_rtp_(timestamp_rtp), |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 263 | ntp_time_ms_(0), |
| 264 | timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec), |
Ilya Nikolaevskiy | 9560d7d | 2019-10-30 10:19:47 | [diff] [blame] | 265 | rotation_(rotation) { |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 266 | RTC_DCHECK(buffer); |
| 267 | } |
| 268 | |
Palak Agarwal | b57053e | 2023-02-20 12:29:46 | [diff] [blame] | 269 | VideoFrame::VideoFrame(uint16_t id, |
| 270 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 271 | int64_t timestamp_us, |
| 272 | const absl::optional<Timestamp>& capture_time_identifier, |
henrika | 7b6f996 | 2023-10-30 10:33:23 | [diff] [blame] | 273 | const absl::optional<Timestamp>& reference_time, |
Palak Agarwal | b57053e | 2023-02-20 12:29:46 | [diff] [blame] | 274 | 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 Titov | 1ebfb6a | 2019-01-03 22:49:37 | [diff] [blame] | 281 | : id_(id), |
| 282 | video_frame_buffer_(buffer), |
Emircan Uysaler | 800787f | 2018-07-16 17:01:49 | [diff] [blame] | 283 | timestamp_rtp_(timestamp_rtp), |
| 284 | ntp_time_ms_(ntp_time_ms), |
| 285 | timestamp_us_(timestamp_us), |
Palak Agarwal | b57053e | 2023-02-20 12:29:46 | [diff] [blame] | 286 | capture_time_identifier_(capture_time_identifier), |
henrika | 7b6f996 | 2023-10-30 10:33:23 | [diff] [blame] | 287 | reference_time_(reference_time), |
Emircan Uysaler | 800787f | 2018-07-16 17:01:49 | [diff] [blame] | 288 | rotation_(rotation), |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 10:55:57 | [diff] [blame] | 289 | color_space_(color_space), |
Johannes Kron | bbf639e | 2022-06-15 10:27:23 | [diff] [blame] | 290 | render_parameters_(render_parameters), |
Ilya Nikolaevskiy | 9560d7d | 2019-10-30 10:19:47 | [diff] [blame] | 291 | update_rect_(update_rect), |
Chen Xing | f00bf42 | 2019-06-20 08:05:55 | [diff] [blame] | 292 | packet_infos_(std::move(packet_infos)) { |
Ilya Nikolaevskiy | 9560d7d | 2019-10-30 10:19:47 | [diff] [blame] | 293 | 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 Nikolaevskiy | 6aca0b7 | 2019-02-13 10:55:57 | [diff] [blame] | 299 | } |
Emircan Uysaler | 800787f | 2018-07-16 17:01:49 | [diff] [blame] | 300 | |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 301 | VideoFrame::~VideoFrame() = default; |
| 302 | |
| 303 | VideoFrame::VideoFrame(const VideoFrame&) = default; |
| 304 | VideoFrame::VideoFrame(VideoFrame&&) = default; |
| 305 | VideoFrame& VideoFrame::operator=(const VideoFrame&) = default; |
| 306 | VideoFrame& VideoFrame::operator=(VideoFrame&&) = default; |
| 307 | |
| 308 | int VideoFrame::width() const { |
| 309 | return video_frame_buffer_ ? video_frame_buffer_->width() : 0; |
| 310 | } |
| 311 | |
| 312 | int VideoFrame::height() const { |
| 313 | return video_frame_buffer_ ? video_frame_buffer_->height() : 0; |
| 314 | } |
| 315 | |
kthelgason | 2bc6864 | 2017-02-07 15:02:22 | [diff] [blame] | 316 | uint32_t VideoFrame::size() const { |
| 317 | return width() * height(); |
| 318 | } |
| 319 | |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 320 | rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const { |
| 321 | return video_frame_buffer_; |
| 322 | } |
| 323 | |
Ilya Nikolaevskiy | 4fc0855 | 2019-06-05 13:59:12 | [diff] [blame] | 324 | void VideoFrame::set_video_frame_buffer( |
| 325 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer) { |
| 326 | RTC_CHECK(buffer); |
| 327 | video_frame_buffer_ = buffer; |
| 328 | } |
| 329 | |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 330 | int64_t VideoFrame::render_time_ms() const { |
| 331 | return timestamp_us() / rtc::kNumMicrosecsPerMillisec; |
| 332 | } |
| 333 | |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 334 | } // namespace webrtc |