magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame^] | 1 | /* |
| 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. |
| 16 | static const int kBufferAlignment = 64; |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | VideoFrameBuffer::~VideoFrameBuffer() {} |
| 21 | |
| 22 | I420Buffer::I420Buffer(int width, int height) |
| 23 | : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) { |
| 24 | } |
| 25 | |
| 26 | I420Buffer::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 | |
| 46 | I420Buffer::~I420Buffer() { |
| 47 | } |
| 48 | |
| 49 | int I420Buffer::width() const { |
| 50 | return width_; |
| 51 | } |
| 52 | |
| 53 | int I420Buffer::height() const { |
| 54 | return height_; |
| 55 | } |
| 56 | |
| 57 | const 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 | |
| 72 | uint8_t* I420Buffer::data(PlaneType type) { |
| 73 | DCHECK(HasOneRef()); |
| 74 | return const_cast<uint8_t*>( |
| 75 | static_cast<const VideoFrameBuffer*>(this)->data(type)); |
| 76 | } |
| 77 | |
| 78 | int 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 | |
| 92 | rtc::scoped_refptr<NativeHandle> I420Buffer::native_handle() const { |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
| 96 | TextureBuffer::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 | |
| 106 | TextureBuffer::~TextureBuffer() { |
| 107 | } |
| 108 | |
| 109 | int TextureBuffer::width() const { |
| 110 | return width_; |
| 111 | } |
| 112 | |
| 113 | int TextureBuffer::height() const { |
| 114 | return height_; |
| 115 | } |
| 116 | |
| 117 | const uint8_t* TextureBuffer::data(PlaneType type) const { |
| 118 | RTC_NOTREACHED(); // Should not be called. |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | uint8_t* TextureBuffer::data(PlaneType type) { |
| 123 | RTC_NOTREACHED(); // Should not be called. |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
| 127 | int TextureBuffer::stride(PlaneType type) const { |
| 128 | RTC_NOTREACHED(); // Should not be called. |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | rtc::scoped_refptr<NativeHandle> TextureBuffer::native_handle() const { |
| 133 | return native_handle_; |
| 134 | } |
| 135 | |
| 136 | } // namespace webrtc |