Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [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 | |
| 11 | #include "webrtc/video_frame.h" |
| 12 | |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <algorithm> // swap |
| 16 | |
| 17 | #include "webrtc/base/bind.h" |
| 18 | #include "webrtc/base/checks.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | VideoFrame::VideoFrame() { |
| 23 | // Intentionally using Reset instead of initializer list so that any missed |
| 24 | // fields in Reset will be caught by memory checkers. |
| 25 | Reset(); |
| 26 | } |
| 27 | |
| 28 | VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 29 | uint32_t timestamp, |
| 30 | int64_t render_time_ms, |
| 31 | VideoRotation rotation) |
| 32 | : video_frame_buffer_(buffer), |
| 33 | timestamp_(timestamp), |
| 34 | ntp_time_ms_(0), |
| 35 | render_time_ms_(render_time_ms), |
| 36 | rotation_(rotation) { |
| 37 | } |
| 38 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 39 | int VideoFrame::CreateEmptyFrame(int width, |
| 40 | int height, |
| 41 | int stride_y, |
| 42 | int stride_u, |
| 43 | int stride_v) { |
| 44 | const int half_width = (width + 1) / 2; |
| 45 | DCHECK_GT(width, 0); |
| 46 | DCHECK_GT(height, 0); |
| 47 | DCHECK_GE(stride_y, width); |
| 48 | DCHECK_GE(stride_u, half_width); |
| 49 | DCHECK_GE(stride_v, half_width); |
| 50 | |
| 51 | // Creating empty frame - reset all values. |
| 52 | timestamp_ = 0; |
| 53 | ntp_time_ms_ = 0; |
| 54 | render_time_ms_ = 0; |
| 55 | rotation_ = kVideoRotation_0; |
| 56 | |
| 57 | // Check if it's safe to reuse allocation. |
| 58 | if (video_frame_buffer_ && video_frame_buffer_->HasOneRef() && |
| 59 | !video_frame_buffer_->native_handle() && |
| 60 | width == video_frame_buffer_->width() && |
| 61 | height == video_frame_buffer_->height() && stride_y == stride(kYPlane) && |
| 62 | stride_u == stride(kUPlane) && stride_v == stride(kVPlane)) { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | // Need to allocate new buffer. |
| 67 | video_frame_buffer_ = new rtc::RefCountedObject<I420Buffer>( |
| 68 | width, height, stride_y, stride_u, stride_v); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int VideoFrame::CreateFrame(const uint8_t* buffer_y, |
| 73 | const uint8_t* buffer_u, |
| 74 | const uint8_t* buffer_v, |
| 75 | int width, |
| 76 | int height, |
| 77 | int stride_y, |
| 78 | int stride_u, |
| 79 | int stride_v) { |
| 80 | return CreateFrame(buffer_y, buffer_u, buffer_v, width, height, stride_y, |
| 81 | stride_u, stride_v, kVideoRotation_0); |
| 82 | } |
| 83 | |
| 84 | int VideoFrame::CreateFrame(const uint8_t* buffer_y, |
| 85 | const uint8_t* buffer_u, |
| 86 | const uint8_t* buffer_v, |
| 87 | int width, |
| 88 | int height, |
| 89 | int stride_y, |
| 90 | int stride_u, |
| 91 | int stride_v, |
| 92 | VideoRotation rotation) { |
| 93 | const int half_height = (height + 1) / 2; |
| 94 | const int expected_size_y = height * stride_y; |
| 95 | const int expected_size_u = half_height * stride_u; |
| 96 | const int expected_size_v = half_height * stride_v; |
| 97 | CreateEmptyFrame(width, height, stride_y, stride_u, stride_v); |
| 98 | memcpy(buffer(kYPlane), buffer_y, expected_size_y); |
| 99 | memcpy(buffer(kUPlane), buffer_u, expected_size_u); |
| 100 | memcpy(buffer(kVPlane), buffer_v, expected_size_v); |
| 101 | rotation_ = rotation; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | int VideoFrame::CreateFrame(const uint8_t* buffer, |
| 106 | int width, |
| 107 | int height, |
| 108 | VideoRotation rotation) { |
| 109 | const int stride_y = width; |
| 110 | const int stride_uv = (width + 1) / 2; |
| 111 | |
| 112 | const uint8_t* buffer_y = buffer; |
| 113 | const uint8_t* buffer_u = buffer_y + stride_y * height; |
| 114 | const uint8_t* buffer_v = buffer_u + stride_uv * ((height + 1) / 2); |
| 115 | return CreateFrame(buffer_y, buffer_u, buffer_v, width, height, stride_y, |
| 116 | stride_uv, stride_uv, rotation); |
| 117 | } |
| 118 | |
| 119 | int VideoFrame::CopyFrame(const VideoFrame& videoFrame) { |
| 120 | if (videoFrame.IsZeroSize()) { |
| 121 | video_frame_buffer_ = nullptr; |
| 122 | } else if (videoFrame.native_handle()) { |
| 123 | video_frame_buffer_ = videoFrame.video_frame_buffer(); |
| 124 | } else { |
| 125 | CreateFrame(videoFrame.buffer(kYPlane), videoFrame.buffer(kUPlane), |
| 126 | videoFrame.buffer(kVPlane), videoFrame.width(), |
| 127 | videoFrame.height(), videoFrame.stride(kYPlane), |
| 128 | videoFrame.stride(kUPlane), videoFrame.stride(kVPlane)); |
| 129 | } |
| 130 | |
| 131 | timestamp_ = videoFrame.timestamp_; |
| 132 | ntp_time_ms_ = videoFrame.ntp_time_ms_; |
| 133 | render_time_ms_ = videoFrame.render_time_ms_; |
| 134 | rotation_ = videoFrame.rotation_; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | void VideoFrame::ShallowCopy(const VideoFrame& videoFrame) { |
| 139 | video_frame_buffer_ = videoFrame.video_frame_buffer(); |
| 140 | timestamp_ = videoFrame.timestamp_; |
| 141 | ntp_time_ms_ = videoFrame.ntp_time_ms_; |
| 142 | render_time_ms_ = videoFrame.render_time_ms_; |
| 143 | rotation_ = videoFrame.rotation_; |
| 144 | } |
| 145 | |
| 146 | void VideoFrame::Reset() { |
| 147 | video_frame_buffer_ = nullptr; |
| 148 | timestamp_ = 0; |
| 149 | ntp_time_ms_ = 0; |
| 150 | render_time_ms_ = 0; |
| 151 | rotation_ = kVideoRotation_0; |
| 152 | } |
| 153 | |
| 154 | uint8_t* VideoFrame::buffer(PlaneType type) { |
| 155 | return video_frame_buffer_ ? video_frame_buffer_->data(type) : nullptr; |
| 156 | } |
| 157 | |
| 158 | const uint8_t* VideoFrame::buffer(PlaneType type) const { |
| 159 | // Const cast to call the correct const-version of data. |
| 160 | const VideoFrameBuffer* const_buffer = video_frame_buffer_.get(); |
| 161 | return const_buffer ? const_buffer->data(type) : nullptr; |
| 162 | } |
| 163 | |
| 164 | int VideoFrame::allocated_size(PlaneType type) const { |
| 165 | const int plane_height = (type == kYPlane) ? height() : (height() + 1) / 2; |
| 166 | return plane_height * stride(type); |
| 167 | } |
| 168 | |
| 169 | int VideoFrame::stride(PlaneType type) const { |
| 170 | return video_frame_buffer_ ? video_frame_buffer_->stride(type) : 0; |
| 171 | } |
| 172 | |
| 173 | int VideoFrame::width() const { |
| 174 | return video_frame_buffer_ ? video_frame_buffer_->width() : 0; |
| 175 | } |
| 176 | |
| 177 | int VideoFrame::height() const { |
| 178 | return video_frame_buffer_ ? video_frame_buffer_->height() : 0; |
| 179 | } |
| 180 | |
| 181 | bool VideoFrame::IsZeroSize() const { |
| 182 | return !video_frame_buffer_; |
| 183 | } |
| 184 | |
| 185 | void* VideoFrame::native_handle() const { |
| 186 | return video_frame_buffer_ ? video_frame_buffer_->native_handle() : nullptr; |
| 187 | } |
| 188 | |
| 189 | rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const { |
| 190 | return video_frame_buffer_; |
| 191 | } |
| 192 | |
| 193 | void VideoFrame::set_video_frame_buffer( |
| 194 | const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer) { |
| 195 | video_frame_buffer_ = buffer; |
| 196 | } |
| 197 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame^] | 198 | VideoFrame VideoFrame::ConvertNativeToI420Frame() const { |
| 199 | DCHECK(native_handle()); |
| 200 | VideoFrame frame; |
| 201 | frame.ShallowCopy(*this); |
| 202 | frame.set_video_frame_buffer(video_frame_buffer_->NativeToI420Buffer()); |
| 203 | return frame; |
| 204 | } |
| 205 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 206 | } // namespace webrtc |