Add support for aliasing a I420VideoFrame (and internally, a Plane) to an existing memory buffer without taking ownership. Use this to remove an extra copy in VideoCaptureImpl::IncomingFrameI420. BUG=1128 TEST=local build, run Chromium on ARM, build, run Chromium/unittests on Linux R=fischman@webrtc.org, mflodman@webrtc.org, mikhal@webrtc.org Review URL: https://webrtc-codereview.appspot.com/3179004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@5068 4adac7df-926f-26a2-2b94-8c16560cd09d
92 lines
2.3 KiB
C++
92 lines
2.3 KiB
C++
/*
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "webrtc/common_video/plane.h"
|
|
|
|
#include <string.h> // memcpy
|
|
|
|
#include <algorithm> // swap
|
|
|
|
namespace webrtc {
|
|
|
|
// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
|
|
static const size_t kBufferAlignment = 64;
|
|
|
|
Plane::Plane()
|
|
: pointer_(NULL),
|
|
allocation_(NULL),
|
|
allocated_size_(0),
|
|
plane_size_(0),
|
|
stride_(0) {}
|
|
|
|
Plane::~Plane() {}
|
|
|
|
int Plane::CreateEmptyPlane(size_t allocated_size,
|
|
size_t stride,
|
|
size_t plane_size) {
|
|
if (allocated_size < 1 || stride < 1 || plane_size < 1)
|
|
return -1;
|
|
stride_ = stride;
|
|
if (Reallocate(allocated_size) < 0)
|
|
return -1;
|
|
plane_size_ = plane_size;
|
|
return 0;
|
|
}
|
|
|
|
int Plane::Reallocate(size_t new_size) {
|
|
if (new_size <= 0)
|
|
return -1;
|
|
if (new_size <= allocated_size_)
|
|
return 0;
|
|
Allocator<uint8_t>::scoped_ptr_aligned new_buffer(
|
|
AlignedMalloc<uint8_t>(new_size, kBufferAlignment));
|
|
allocation_.reset(new_buffer.release());
|
|
pointer_ = allocation_.get();
|
|
allocated_size_ = new_size;
|
|
return 0;
|
|
}
|
|
|
|
int Plane::Copy(const Plane& plane) {
|
|
if (Reallocate(plane.allocated_size_) < 0)
|
|
return -1;
|
|
if (plane.pointer_)
|
|
memcpy(pointer_, plane.pointer_, plane.plane_size_);
|
|
stride_ = plane.stride_;
|
|
plane_size_ = plane.plane_size_;
|
|
return 0;
|
|
}
|
|
|
|
int Plane::Copy(size_t size, size_t stride, const uint8_t* buffer) {
|
|
if (Reallocate(size) < 0)
|
|
return -1;
|
|
memcpy(pointer_, buffer, size);
|
|
plane_size_ = size;
|
|
stride_ = stride;
|
|
return 0;
|
|
}
|
|
|
|
void Plane::Alias(size_t size, size_t stride, uint8_t* buffer) {
|
|
allocation_.reset();
|
|
allocated_size_ = 0;
|
|
pointer_ = buffer;
|
|
stride_ = stride;
|
|
plane_size_ = size;
|
|
}
|
|
|
|
void Plane::Swap(Plane& plane) {
|
|
std::swap(pointer_, plane.pointer_);
|
|
allocation_.swap(plane.allocation_);
|
|
std::swap(allocated_size_, plane.allocated_size_);
|
|
std::swap(plane_size_, plane.plane_size_);
|
|
std::swap(stride_, plane.stride_);
|
|
}
|
|
|
|
} // namespace webrtc
|