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