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 
11 #include "api/video/video_frame.h"
12 
13 #include <algorithm>
14 #include <utility>
15 
16 #include "rtc_base/checks.h"
17 #include "rtc_base/time_utils.h"
18 
19 namespace webrtc {
20 
Union(const UpdateRect & other)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 
Intersect(const UpdateRect & other)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 
MakeEmptyUpdate()55 void VideoFrame::UpdateRect::MakeEmptyUpdate() {
56   width = height = offset_x = offset_y = 0;
57 }
58 
IsEmpty() const59 bool VideoFrame::UpdateRect::IsEmpty() const {
60   return width == 0 && height == 0;
61 }
62 
ScaleWithFrame(int frame_width,int frame_height,int crop_x,int crop_y,int crop_width,int crop_height,int scaled_width,int scaled_height) const63 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 
160 VideoFrame::Builder::Builder() = default;
161 
162 VideoFrame::Builder::~Builder() = default;
163 
build()164 VideoFrame VideoFrame::Builder::build() {
165   RTC_CHECK(video_frame_buffer_ != nullptr);
166   return VideoFrame(id_, video_frame_buffer_, timestamp_us_, timestamp_rtp_,
167                     ntp_time_ms_, rotation_, color_space_, update_rect_,
168                     packet_infos_);
169 }
170 
set_video_frame_buffer(const rtc::scoped_refptr<VideoFrameBuffer> & buffer)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 
set_timestamp_ms(int64_t timestamp_ms)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 
set_timestamp_us(int64_t timestamp_us)183 VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us(
184     int64_t timestamp_us) {
185   timestamp_us_ = timestamp_us;
186   return *this;
187 }
188 
set_timestamp_rtp(uint32_t timestamp_rtp)189 VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp(
190     uint32_t timestamp_rtp) {
191   timestamp_rtp_ = timestamp_rtp;
192   return *this;
193 }
194 
set_ntp_time_ms(int64_t ntp_time_ms)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 
set_rotation(VideoRotation rotation)200 VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) {
201   rotation_ = rotation;
202   return *this;
203 }
204 
set_color_space(const absl::optional<ColorSpace> & color_space)205 VideoFrame::Builder& VideoFrame::Builder::set_color_space(
206     const absl::optional<ColorSpace>& color_space) {
207   color_space_ = color_space;
208   return *this;
209 }
210 
set_color_space(const ColorSpace * color_space)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;
215   return *this;
216 }
217 
set_id(uint16_t id)218 VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) {
219   id_ = id;
220   return *this;
221 }
222 
set_update_rect(const absl::optional<VideoFrame::UpdateRect> & update_rect)223 VideoFrame::Builder& VideoFrame::Builder::set_update_rect(
224     const absl::optional<VideoFrame::UpdateRect>& update_rect) {
225   update_rect_ = update_rect;
226   return *this;
227 }
228 
set_packet_infos(RtpPacketInfos packet_infos)229 VideoFrame::Builder& VideoFrame::Builder::set_packet_infos(
230     RtpPacketInfos packet_infos) {
231   packet_infos_ = std::move(packet_infos);
232   return *this;
233 }
234 
VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer> & buffer,webrtc::VideoRotation rotation,int64_t timestamp_us)235 VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
236                        webrtc::VideoRotation rotation,
237                        int64_t timestamp_us)
238     : video_frame_buffer_(buffer),
239       timestamp_rtp_(0),
240       ntp_time_ms_(0),
241       timestamp_us_(timestamp_us),
242       rotation_(rotation) {}
243 
VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer> & buffer,uint32_t timestamp_rtp,int64_t render_time_ms,VideoRotation rotation)244 VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
245                        uint32_t timestamp_rtp,
246                        int64_t render_time_ms,
247                        VideoRotation rotation)
248     : video_frame_buffer_(buffer),
249       timestamp_rtp_(timestamp_rtp),
250       ntp_time_ms_(0),
251       timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec),
252       rotation_(rotation) {
253   RTC_DCHECK(buffer);
254 }
255 
VideoFrame(uint16_t id,const rtc::scoped_refptr<VideoFrameBuffer> & buffer,int64_t timestamp_us,uint32_t timestamp_rtp,int64_t ntp_time_ms,VideoRotation rotation,const absl::optional<ColorSpace> & color_space,const absl::optional<UpdateRect> & update_rect,RtpPacketInfos packet_infos)256 VideoFrame::VideoFrame(uint16_t id,
257                        const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
258                        int64_t timestamp_us,
259                        uint32_t timestamp_rtp,
260                        int64_t ntp_time_ms,
261                        VideoRotation rotation,
262                        const absl::optional<ColorSpace>& color_space,
263                        const absl::optional<UpdateRect>& update_rect,
264                        RtpPacketInfos packet_infos)
265     : id_(id),
266       video_frame_buffer_(buffer),
267       timestamp_rtp_(timestamp_rtp),
268       ntp_time_ms_(ntp_time_ms),
269       timestamp_us_(timestamp_us),
270       rotation_(rotation),
271       color_space_(color_space),
272       update_rect_(update_rect),
273       packet_infos_(std::move(packet_infos)) {
274   if (update_rect_) {
275     RTC_DCHECK_GE(update_rect_->offset_x, 0);
276     RTC_DCHECK_GE(update_rect_->offset_y, 0);
277     RTC_DCHECK_LE(update_rect_->offset_x + update_rect_->width, width());
278     RTC_DCHECK_LE(update_rect_->offset_y + update_rect_->height, height());
279   }
280 }
281 
282 VideoFrame::~VideoFrame() = default;
283 
284 VideoFrame::VideoFrame(const VideoFrame&) = default;
285 VideoFrame::VideoFrame(VideoFrame&&) = default;
286 VideoFrame& VideoFrame::operator=(const VideoFrame&) = default;
287 VideoFrame& VideoFrame::operator=(VideoFrame&&) = default;
288 
width() const289 int VideoFrame::width() const {
290   return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
291 }
292 
height() const293 int VideoFrame::height() const {
294   return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
295 }
296 
size() const297 uint32_t VideoFrame::size() const {
298   return width() * height();
299 }
300 
video_frame_buffer() const301 rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
302   return video_frame_buffer_;
303 }
304 
set_video_frame_buffer(const rtc::scoped_refptr<VideoFrameBuffer> & buffer)305 void VideoFrame::set_video_frame_buffer(
306     const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
307   RTC_CHECK(buffer);
308   video_frame_buffer_ = buffer;
309 }
310 
render_time_ms() const311 int64_t VideoFrame::render_time_ms() const {
312   return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
313 }
314 
315 }  // namespace webrtc
316