OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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/include/corevideo_frame_buffer.h" |
| 12 |
| 13 #include "libyuv/convert.h" |
| 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/logging.h" |
| 16 |
| 17 namespace webrtc { |
| 18 |
| 19 CoreVideoFrameBuffer::CoreVideoFrameBuffer(CVPixelBufferRef pixel_buffer) |
| 20 : NativeHandleBuffer(pixel_buffer, |
| 21 CVPixelBufferGetWidth(pixel_buffer), |
| 22 CVPixelBufferGetHeight(pixel_buffer)), |
| 23 pixel_buffer_(pixel_buffer) { |
| 24 CVBufferRetain(pixel_buffer_); |
| 25 } |
| 26 |
| 27 CoreVideoFrameBuffer::~CoreVideoFrameBuffer() { |
| 28 CVBufferRelease(pixel_buffer_); |
| 29 } |
| 30 |
| 31 rtc::scoped_refptr<VideoFrameBuffer> |
| 32 CoreVideoFrameBuffer::NativeToI420Buffer() { |
| 33 RTC_DCHECK(CVPixelBufferGetPixelFormatType(pixel_buffer_) == |
| 34 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange); |
| 35 size_t width = CVPixelBufferGetWidthOfPlane(pixel_buffer_, 0); |
| 36 size_t height = CVPixelBufferGetHeightOfPlane(pixel_buffer_, 0); |
| 37 // TODO(tkchin): Use a frame buffer pool. |
| 38 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = |
| 39 new rtc::RefCountedObject<webrtc::I420Buffer>(width, height); |
| 40 CVPixelBufferLockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly); |
| 41 const uint8_t* src_y = static_cast<const uint8_t*>( |
| 42 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 0)); |
| 43 int src_y_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 0); |
| 44 const uint8_t* src_uv = static_cast<const uint8_t*>( |
| 45 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 1)); |
| 46 int src_uv_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 1); |
| 47 int ret = libyuv::NV12ToI420( |
| 48 src_y, src_y_stride, src_uv, src_uv_stride, |
| 49 buffer->MutableData(webrtc::kYPlane), buffer->stride(webrtc::kYPlane), |
| 50 buffer->MutableData(webrtc::kUPlane), buffer->stride(webrtc::kUPlane), |
| 51 buffer->MutableData(webrtc::kVPlane), buffer->stride(webrtc::kVPlane), |
| 52 width, height); |
| 53 CVPixelBufferUnlockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly); |
| 54 if (ret) { |
| 55 LOG(LS_ERROR) << "Error converting NV12 to I420: " << ret; |
| 56 return nullptr; |
| 57 } |
| 58 return buffer; |
| 59 } |
| 60 |
| 61 } // namespace webrtc |
OLD | NEW |