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