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> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/time_utils.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 20 | void VideoFrame::UpdateRect::Union(const UpdateRect& other) { |
| 21 | if (other.IsEmpty()) |
| 22 | return; |
| 23 | if (IsEmpty()) { |
| 24 | *this = other; |
| 25 | return; |
| 26 | } |
| 27 | int right = std::max(offset_x + width, other.offset_x + other.width); |
| 28 | int bottom = std::max(offset_y + height, other.offset_y + other.height); |
| 29 | offset_x = std::min(offset_x, other.offset_x); |
| 30 | offset_y = std::min(offset_y, other.offset_y); |
| 31 | width = right - offset_x; |
| 32 | height = bottom - offset_y; |
| 33 | RTC_DCHECK_GT(width, 0); |
| 34 | RTC_DCHECK_GT(height, 0); |
| 35 | } |
| 36 | |
| 37 | void VideoFrame::UpdateRect::Intersect(const UpdateRect& other) { |
| 38 | if (other.IsEmpty() || IsEmpty()) { |
| 39 | MakeEmptyUpdate(); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | int right = std::min(offset_x + width, other.offset_x + other.width); |
| 44 | int bottom = std::min(offset_y + height, other.offset_y + other.height); |
| 45 | offset_x = std::max(offset_x, other.offset_x); |
| 46 | offset_y = std::max(offset_y, other.offset_y); |
| 47 | width = right - offset_x; |
| 48 | height = bottom - offset_y; |
| 49 | if (width <= 0 || height <= 0) { |
| 50 | MakeEmptyUpdate(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void VideoFrame::UpdateRect::MakeEmptyUpdate() { |
| 55 | width = height = offset_x = offset_y = 0; |
| 56 | } |
| 57 | |
| 58 | bool VideoFrame::UpdateRect::IsEmpty() const { |
| 59 | return width == 0 && height == 0; |
| 60 | } |
| 61 | |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 62 | VideoFrame::Builder::Builder() = default; |
| 63 | |
| 64 | VideoFrame::Builder::~Builder() = default; |
| 65 | |
| 66 | VideoFrame VideoFrame::Builder::build() { |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 67 | RTC_CHECK(video_frame_buffer_ != nullptr); |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 68 | return VideoFrame(id_, video_frame_buffer_, timestamp_us_, timestamp_rtp_, |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 69 | ntp_time_ms_, rotation_, color_space_, update_rect_); |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer( |
| 73 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer) { |
| 74 | video_frame_buffer_ = buffer; |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms( |
| 79 | int64_t timestamp_ms) { |
| 80 | timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec; |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us( |
| 85 | int64_t timestamp_us) { |
| 86 | timestamp_us_ = timestamp_us; |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp( |
| 91 | uint32_t timestamp_rtp) { |
| 92 | timestamp_rtp_ = timestamp_rtp; |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) { |
| 97 | ntp_time_ms_ = ntp_time_ms; |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) { |
| 102 | rotation_ = rotation; |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | VideoFrame::Builder& VideoFrame::Builder::set_color_space( |
Danil Chapovalov | b769894 | 2019-02-05 11:32:19 +0100 | [diff] [blame] | 107 | const absl::optional<ColorSpace>& color_space) { |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 108 | color_space_ = color_space; |
| 109 | return *this; |
| 110 | } |
| 111 | |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 112 | VideoFrame::Builder& VideoFrame::Builder::set_color_space( |
| 113 | const ColorSpace* color_space) { |
| 114 | color_space_ = |
| 115 | color_space ? absl::make_optional(*color_space) : absl::nullopt; |
Johannes Kron | fbf1683 | 2018-11-05 16:13:02 +0100 | [diff] [blame] | 116 | return *this; |
| 117 | } |
| 118 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 119 | VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) { |
| 120 | id_ = id; |
| 121 | return *this; |
| 122 | } |
| 123 | |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 124 | VideoFrame::Builder& VideoFrame::Builder::set_update_rect( |
| 125 | const VideoFrame::UpdateRect& update_rect) { |
| 126 | update_rect_ = update_rect; |
| 127 | return *this; |
| 128 | } |
| 129 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 130 | VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 131 | webrtc::VideoRotation rotation, |
| 132 | int64_t timestamp_us) |
| 133 | : video_frame_buffer_(buffer), |
| 134 | timestamp_rtp_(0), |
| 135 | ntp_time_ms_(0), |
| 136 | timestamp_us_(timestamp_us), |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 137 | rotation_(rotation), |
| 138 | update_rect_{0, 0, buffer->width(), buffer->height()} {} |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 139 | |
| 140 | VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
Niels Möller | 2ac6446 | 2018-06-11 11:14:32 +0200 | [diff] [blame] | 141 | uint32_t timestamp_rtp, |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 142 | int64_t render_time_ms, |
| 143 | VideoRotation rotation) |
| 144 | : video_frame_buffer_(buffer), |
Niels Möller | 2ac6446 | 2018-06-11 11:14:32 +0200 | [diff] [blame] | 145 | timestamp_rtp_(timestamp_rtp), |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 146 | ntp_time_ms_(0), |
| 147 | timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec), |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 148 | rotation_(rotation), |
| 149 | update_rect_{0, 0, buffer->width(), buffer->height()} { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 150 | RTC_DCHECK(buffer); |
| 151 | } |
| 152 | |
Ilya Nikolaevskiy | 871e144 | 2019-02-11 13:35:03 +0100 | [diff] [blame] | 153 | VideoFrame::VideoFrame(uint16_t id, |
| 154 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 155 | int64_t timestamp_us, |
| 156 | uint32_t timestamp_rtp, |
| 157 | int64_t ntp_time_ms, |
| 158 | VideoRotation rotation, |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 159 | const absl::optional<ColorSpace>& color_space, |
| 160 | const absl::optional<UpdateRect>& update_rect) |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 161 | : id_(id), |
| 162 | video_frame_buffer_(buffer), |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 163 | timestamp_rtp_(timestamp_rtp), |
| 164 | ntp_time_ms_(ntp_time_ms), |
| 165 | timestamp_us_(timestamp_us), |
| 166 | rotation_(rotation), |
Ilya Nikolaevskiy | 6aca0b7 | 2019-02-13 11:55:57 +0100 | [diff] [blame] | 167 | color_space_(color_space), |
| 168 | update_rect_(update_rect.value_or(UpdateRect{ |
| 169 | 0, 0, video_frame_buffer_->width(), video_frame_buffer_->height()})) { |
| 170 | RTC_DCHECK_GE(update_rect_.offset_x, 0); |
| 171 | RTC_DCHECK_GE(update_rect_.offset_y, 0); |
| 172 | RTC_DCHECK_LE(update_rect_.offset_x + update_rect_.width, width()); |
| 173 | RTC_DCHECK_LE(update_rect_.offset_y + update_rect_.height, height()); |
| 174 | } |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 175 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 176 | VideoFrame::~VideoFrame() = default; |
| 177 | |
| 178 | VideoFrame::VideoFrame(const VideoFrame&) = default; |
| 179 | VideoFrame::VideoFrame(VideoFrame&&) = default; |
| 180 | VideoFrame& VideoFrame::operator=(const VideoFrame&) = default; |
| 181 | VideoFrame& VideoFrame::operator=(VideoFrame&&) = default; |
| 182 | |
| 183 | int VideoFrame::width() const { |
| 184 | return video_frame_buffer_ ? video_frame_buffer_->width() : 0; |
| 185 | } |
| 186 | |
| 187 | int VideoFrame::height() const { |
| 188 | return video_frame_buffer_ ? video_frame_buffer_->height() : 0; |
| 189 | } |
| 190 | |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 191 | uint32_t VideoFrame::size() const { |
| 192 | return width() * height(); |
| 193 | } |
| 194 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 195 | rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const { |
| 196 | return video_frame_buffer_; |
| 197 | } |
| 198 | |
Ilya Nikolaevskiy | 4fc0855 | 2019-06-05 15:59:12 +0200 | [diff] [blame^] | 199 | void VideoFrame::set_video_frame_buffer( |
| 200 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer) { |
| 201 | RTC_CHECK(buffer); |
| 202 | video_frame_buffer_ = buffer; |
| 203 | } |
| 204 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 205 | int64_t VideoFrame::render_time_ms() const { |
| 206 | return timestamp_us() / rtc::kNumMicrosecsPerMillisec; |
| 207 | } |
| 208 | |
| 209 | } // namespace webrtc |