blob: 76e97b7e4775ddef9125cdf2848e66291aec59f1 [file] [log] [blame]
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +00001/*
2 * Copyright (c) 2015 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/video_frame_buffer.h"
12
13#include "webrtc/base/checks.h"
14
15// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
16static const int kBufferAlignment = 64;
17
18namespace webrtc {
19
20VideoFrameBuffer::~VideoFrameBuffer() {}
21
22I420Buffer::I420Buffer(int width, int height)
23 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {
24}
25
26I420Buffer::I420Buffer(int width,
27 int height,
28 int stride_y,
29 int stride_u,
30 int stride_v)
31 : width_(width),
32 height_(height),
33 stride_y_(stride_y),
34 stride_u_(stride_u),
35 stride_v_(stride_v),
36 data_(static_cast<uint8_t*>(AlignedMalloc(
37 stride_y * height + (stride_u + stride_v) * ((height + 1) / 2),
38 kBufferAlignment))) {
39 DCHECK_GT(width, 0);
40 DCHECK_GT(height, 0);
41 DCHECK_GE(stride_y, width);
42 DCHECK_GE(stride_u, (width + 1) / 2);
43 DCHECK_GE(stride_v, (width + 1) / 2);
44}
45
46I420Buffer::~I420Buffer() {
47}
48
49int I420Buffer::width() const {
50 return width_;
51}
52
53int I420Buffer::height() const {
54 return height_;
55}
56
57const uint8_t* I420Buffer::data(PlaneType type) const {
58 switch (type) {
59 case kYPlane:
60 return data_.get();
61 case kUPlane:
62 return data_.get() + stride_y_ * height_;
63 case kVPlane:
64 return data_.get() + stride_y_ * height_ +
65 stride_u_ * ((height_ + 1) / 2);
66 default:
67 RTC_NOTREACHED();
68 return nullptr;
69 }
70}
71
72uint8_t* I420Buffer::data(PlaneType type) {
73 DCHECK(HasOneRef());
74 return const_cast<uint8_t*>(
75 static_cast<const VideoFrameBuffer*>(this)->data(type));
76}
77
78int I420Buffer::stride(PlaneType type) const {
79 switch (type) {
80 case kYPlane:
81 return stride_y_;
82 case kUPlane:
83 return stride_u_;
84 case kVPlane:
85 return stride_v_;
86 default:
87 RTC_NOTREACHED();
88 return 0;
89 }
90}
91
92rtc::scoped_refptr<NativeHandle> I420Buffer::native_handle() const {
93 return nullptr;
94}
95
96TextureBuffer::TextureBuffer(
97 const rtc::scoped_refptr<NativeHandle>& native_handle,
98 int width,
99 int height)
100 : native_handle_(native_handle), width_(width), height_(height) {
101 DCHECK(native_handle.get());
102 DCHECK_GT(width, 0);
103 DCHECK_GT(height, 0);
104}
105
106TextureBuffer::~TextureBuffer() {
107}
108
109int TextureBuffer::width() const {
110 return width_;
111}
112
113int TextureBuffer::height() const {
114 return height_;
115}
116
117const uint8_t* TextureBuffer::data(PlaneType type) const {
118 RTC_NOTREACHED(); // Should not be called.
119 return nullptr;
120}
121
122uint8_t* TextureBuffer::data(PlaneType type) {
123 RTC_NOTREACHED(); // Should not be called.
124 return nullptr;
125}
126
127int TextureBuffer::stride(PlaneType type) const {
128 RTC_NOTREACHED(); // Should not be called.
129 return 0;
130}
131
132rtc::scoped_refptr<NativeHandle> TextureBuffer::native_handle() const {
133 return native_handle_;
134}
135
136} // namespace webrtc