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 | |
| 11 | #include "common_video/plane.h" |
| 12 | |
| 13 | #include <algorithm> // swap |
| 14 | #include <cstring> // memcpy |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
| 19 | static const int kBufferAlignment = 64; |
| 20 | |
| 21 | Plane::Plane() |
| 22 | : buffer_(NULL), |
| 23 | allocated_size_(0), |
| 24 | plane_size_(0), |
| 25 | stride_(0) {} |
| 26 | |
| 27 | Plane::~Plane() {} |
| 28 | |
| 29 | int 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; |
mikhal@webrtc.org | 3bbed74 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 33 | if (MaybeResize(allocated_size) < 0) |
| 34 | return -1; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 35 | plane_size_ = plane_size; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | int Plane::MaybeResize(int new_size) { |
| 40 | if (new_size <= 0) |
| 41 | return -1; |
| 42 | if (new_size <= allocated_size_) |
| 43 | return 0; |
| 44 | Allocator<uint8_t>::scoped_ptr_aligned new_buffer( |
| 45 | AlignedMalloc<uint8_t>(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 | |
| 54 | int 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 | |
| 64 | int 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 | |
| 73 | void 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 |