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