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/plane.h" 12 13#include <string.h> // memcpy 14 15#include <algorithm> // swap 16 17namespace webrtc { 18 19// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. 20static const int kBufferAlignment = 64; 21 22Plane::Plane() 23 : allocated_size_(0), 24 plane_size_(0), 25 stride_(0) {} 26 27Plane::~Plane() {} 28 29int Plane::CreateEmptyPlane(int allocated_size, int stride, int plane_size) { 30 if (allocated_size < 1 || stride < 1 || plane_size < 1) 31 return -1; 32 stride_ = stride; 33 if (MaybeResize(allocated_size) < 0) 34 return -1; 35 plane_size_ = plane_size; 36 return 0; 37} 38 39int Plane::MaybeResize(int new_size) { 40 if (new_size <= 0) 41 return -1; 42 if (new_size <= allocated_size_) 43 return 0; 44 scoped_ptr<uint8_t, AlignedFreeDeleter> new_buffer(static_cast<uint8_t*>( 45 AlignedMalloc(new_size, kBufferAlignment))); 46 if (buffer_.get()) { 47 memcpy(new_buffer.get(), buffer_.get(), plane_size_); 48 } 49 buffer_.reset(new_buffer.release()); 50 allocated_size_ = new_size; 51 return 0; 52} 53 54int Plane::Copy(const Plane& plane) { 55 if (MaybeResize(plane.allocated_size_) < 0) 56 return -1; 57 if (plane.buffer_.get()) 58 memcpy(buffer_.get(), plane.buffer_.get(), plane.plane_size_); 59 stride_ = plane.stride_; 60 plane_size_ = plane.plane_size_; 61 return 0; 62} 63 64int Plane::Copy(int size, int stride, const uint8_t* buffer) { 65 if (MaybeResize(size) < 0) 66 return -1; 67 memcpy(buffer_.get(), buffer, size); 68 plane_size_ = size; 69 stride_ = stride; 70 return 0; 71} 72 73void Plane::Swap(Plane& plane) { 74 std::swap(stride_, plane.stride_); 75 std::swap(allocated_size_, plane.allocated_size_); 76 std::swap(plane_size_, plane.plane_size_); 77 buffer_.swap(plane.buffer_); 78} 79 80} // namespace webrtc 81