andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [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 | |
pbos@webrtc.org | 026d1ce | 2013-06-04 09:02:37 | [diff] [blame] | 11 | #include "webrtc/common_video/interface/i420_video_frame.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 12 | |
wuchengli@chromium.org | 1bdf186 | 2014-05-28 07:00:51 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 15 | #include <algorithm> // swap |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | I420VideoFrame::I420VideoFrame() |
| 20 | : width_(0), |
| 21 | height_(0), |
| 22 | timestamp_(0), |
wu@webrtc.org | 9d10769 | 2014-04-15 17:46:33 | [diff] [blame] | 23 | ntp_time_ms_(0), |
guoweis@webrtc.org | fd7343a | 2015-02-11 18:37:54 | [diff] [blame] | 24 | render_time_ms_(0), |
| 25 | rotation_(kVideoRotation_0) { |
| 26 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 27 | |
| 28 | I420VideoFrame::~I420VideoFrame() {} |
| 29 | |
| 30 | int I420VideoFrame::CreateEmptyFrame(int width, int height, |
| 31 | int stride_y, int stride_u, int stride_v) { |
| 32 | if (CheckDimensions(width, height, stride_y, stride_u, stride_v) < 0) |
| 33 | return -1; |
| 34 | int size_y = stride_y * height; |
| 35 | int half_height = (height + 1) / 2; |
| 36 | int size_u = stride_u * half_height; |
| 37 | int size_v = stride_v * half_height; |
| 38 | width_ = width; |
| 39 | height_ = height; |
| 40 | y_plane_.CreateEmptyPlane(size_y, stride_y, size_y); |
| 41 | u_plane_.CreateEmptyPlane(size_u, stride_u, size_u); |
| 42 | v_plane_.CreateEmptyPlane(size_v, stride_v, size_v); |
mikhal@webrtc.org | 3bbed74 | 2012-10-24 18:33:04 | [diff] [blame] | 43 | // Creating empty frame - reset all values. |
| 44 | timestamp_ = 0; |
wu@webrtc.org | 9d10769 | 2014-04-15 17:46:33 | [diff] [blame] | 45 | ntp_time_ms_ = 0; |
mikhal@webrtc.org | 3bbed74 | 2012-10-24 18:33:04 | [diff] [blame] | 46 | render_time_ms_ = 0; |
guoweis@webrtc.org | fd7343a | 2015-02-11 18:37:54 | [diff] [blame] | 47 | rotation_ = kVideoRotation_0; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | int I420VideoFrame::CreateFrame(int size_y, const uint8_t* buffer_y, |
| 52 | int size_u, const uint8_t* buffer_u, |
| 53 | int size_v, const uint8_t* buffer_v, |
| 54 | int width, int height, |
| 55 | int stride_y, int stride_u, int stride_v) { |
guoweis@webrtc.org | fd7343a | 2015-02-11 18:37:54 | [diff] [blame] | 56 | return CreateFrame(size_y, buffer_y, size_u, buffer_u, size_v, buffer_v, |
| 57 | width, height, stride_y, stride_u, stride_v, |
| 58 | kVideoRotation_0); |
| 59 | } |
| 60 | |
| 61 | int I420VideoFrame::CreateFrame(int size_y, |
| 62 | const uint8_t* buffer_y, |
| 63 | int size_u, |
| 64 | const uint8_t* buffer_u, |
| 65 | int size_v, |
| 66 | const uint8_t* buffer_v, |
| 67 | int width, |
| 68 | int height, |
| 69 | int stride_y, |
| 70 | int stride_u, |
| 71 | int stride_v, |
| 72 | VideoRotation rotation) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 73 | if (size_y < 1 || size_u < 1 || size_v < 1) |
| 74 | return -1; |
| 75 | if (CheckDimensions(width, height, stride_y, stride_u, stride_v) < 0) |
| 76 | return -1; |
| 77 | y_plane_.Copy(size_y, stride_y, buffer_y); |
| 78 | u_plane_.Copy(size_u, stride_u, buffer_u); |
| 79 | v_plane_.Copy(size_v, stride_v, buffer_v); |
| 80 | width_ = width; |
| 81 | height_ = height; |
guoweis@webrtc.org | fd7343a | 2015-02-11 18:37:54 | [diff] [blame] | 82 | rotation_ = rotation; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int I420VideoFrame::CopyFrame(const I420VideoFrame& videoFrame) { |
| 87 | int ret = CreateFrame(videoFrame.allocated_size(kYPlane), |
| 88 | videoFrame.buffer(kYPlane), |
| 89 | videoFrame.allocated_size(kUPlane), |
| 90 | videoFrame.buffer(kUPlane), |
| 91 | videoFrame.allocated_size(kVPlane), |
| 92 | videoFrame.buffer(kVPlane), |
| 93 | videoFrame.width_, videoFrame.height_, |
| 94 | videoFrame.stride(kYPlane), videoFrame.stride(kUPlane), |
| 95 | videoFrame.stride(kVPlane)); |
| 96 | if (ret < 0) |
| 97 | return ret; |
| 98 | timestamp_ = videoFrame.timestamp_; |
wu@webrtc.org | 9d10769 | 2014-04-15 17:46:33 | [diff] [blame] | 99 | ntp_time_ms_ = videoFrame.ntp_time_ms_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 100 | render_time_ms_ = videoFrame.render_time_ms_; |
guoweis@webrtc.org | fd7343a | 2015-02-11 18:37:54 | [diff] [blame] | 101 | rotation_ = videoFrame.rotation_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
wuchengli@chromium.org | 1bdf186 | 2014-05-28 07:00:51 | [diff] [blame] | 105 | I420VideoFrame* I420VideoFrame::CloneFrame() const { |
kwiberg@webrtc.org | deb9dae | 2015-02-26 14:34:55 | [diff] [blame^] | 106 | rtc::scoped_ptr<I420VideoFrame> new_frame(new I420VideoFrame()); |
wuchengli@chromium.org | 1bdf186 | 2014-05-28 07:00:51 | [diff] [blame] | 107 | if (new_frame->CopyFrame(*this) == -1) { |
| 108 | // CopyFrame failed. |
| 109 | return NULL; |
| 110 | } |
| 111 | return new_frame.release(); |
| 112 | } |
| 113 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 114 | void I420VideoFrame::SwapFrame(I420VideoFrame* videoFrame) { |
| 115 | y_plane_.Swap(videoFrame->y_plane_); |
| 116 | u_plane_.Swap(videoFrame->u_plane_); |
| 117 | v_plane_.Swap(videoFrame->v_plane_); |
| 118 | std::swap(width_, videoFrame->width_); |
| 119 | std::swap(height_, videoFrame->height_); |
| 120 | std::swap(timestamp_, videoFrame->timestamp_); |
wu@webrtc.org | 9d10769 | 2014-04-15 17:46:33 | [diff] [blame] | 121 | std::swap(ntp_time_ms_, videoFrame->ntp_time_ms_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 122 | std::swap(render_time_ms_, videoFrame->render_time_ms_); |
guoweis@webrtc.org | fd7343a | 2015-02-11 18:37:54 | [diff] [blame] | 123 | std::swap(rotation_, videoFrame->rotation_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | uint8_t* I420VideoFrame::buffer(PlaneType type) { |
| 127 | Plane* plane_ptr = GetPlane(type); |
| 128 | if (plane_ptr) |
| 129 | return plane_ptr->buffer(); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | const uint8_t* I420VideoFrame::buffer(PlaneType type) const { |
| 134 | const Plane* plane_ptr = GetPlane(type); |
| 135 | if (plane_ptr) |
| 136 | return plane_ptr->buffer(); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | int I420VideoFrame::allocated_size(PlaneType type) const { |
| 141 | const Plane* plane_ptr = GetPlane(type); |
| 142 | if (plane_ptr) |
| 143 | return plane_ptr->allocated_size(); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | int I420VideoFrame::stride(PlaneType type) const { |
| 148 | const Plane* plane_ptr = GetPlane(type); |
| 149 | if (plane_ptr) |
| 150 | return plane_ptr->stride(); |
| 151 | return -1; |
| 152 | } |
| 153 | |
mikhal@webrtc.org | 3bbed74 | 2012-10-24 18:33:04 | [diff] [blame] | 154 | bool I420VideoFrame::IsZeroSize() const { |
magjed@webrtc.org | 48c809f | 2015-02-19 15:34:55 | [diff] [blame] | 155 | return width() == 0 || height() == 0; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 156 | } |
| 157 | |
wu@webrtc.org | ea7b33e | 2013-08-05 20:36:57 | [diff] [blame] | 158 | void* I420VideoFrame::native_handle() const { return NULL; } |
| 159 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 160 | int I420VideoFrame::CheckDimensions(int width, int height, |
| 161 | int stride_y, int stride_u, int stride_v) { |
| 162 | int half_width = (width + 1) / 2; |
| 163 | if (width < 1 || height < 1 || |
| 164 | stride_y < width || stride_u < half_width || stride_v < half_width) |
| 165 | return -1; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | const Plane* I420VideoFrame::GetPlane(PlaneType type) const { |
| 170 | switch (type) { |
| 171 | case kYPlane : |
| 172 | return &y_plane_; |
| 173 | case kUPlane : |
| 174 | return &u_plane_; |
| 175 | case kVPlane : |
| 176 | return &v_plane_; |
| 177 | default: |
| 178 | assert(false); |
| 179 | } |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | Plane* I420VideoFrame::GetPlane(PlaneType type) { |
| 184 | switch (type) { |
| 185 | case kYPlane : |
| 186 | return &y_plane_; |
| 187 | case kUPlane : |
| 188 | return &u_plane_; |
| 189 | case kVPlane : |
| 190 | return &v_plane_; |
| 191 | default: |
| 192 | assert(false); |
| 193 | } |
| 194 | return NULL; |
| 195 | } |
| 196 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 197 | } // namespace webrtc |