blob: 2362c4d82c10abe613ca125ee0fabe87a637c91d [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
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
16namespace webrtc {
17
18// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
19static const int kBufferAlignment = 64;
20
21Plane::Plane()
22 : buffer_(NULL),
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;
mikhal@webrtc.org3bbed742012-10-24 18:33:04 +000033 if (MaybeResize(allocated_size) < 0)
34 return -1;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000035 plane_size_ = plane_size;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000036 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 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
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