Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/common_video/include/corevideo_frame_buffer.h" | 11 #include "webrtc/common_video/include/corevideo_frame_buffer.h" |
| 12 | 12 |
| 13 #include "libyuv/convert.h" | 13 #include "libyuv/convert.h" |
| 14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
| 16 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | |
| 16 | 17 |
| 17 namespace webrtc { | 18 namespace webrtc { |
| 18 | 19 |
| 20 CoreVideoFrameBuffer::CoreVideoFrameBuffer(CVPixelBufferRef pixel_buffer, | |
| 21 int adapted_width, | |
| 22 int adapted_height, | |
| 23 int crop_width, | |
| 24 int crop_height, | |
| 25 int crop_x, | |
| 26 int crop_y) | |
| 27 : NativeHandleBuffer(pixel_buffer, adapted_width, adapted_height), | |
| 28 pixel_buffer_(pixel_buffer), | |
| 29 buffer_width_(CVPixelBufferGetWidth(pixel_buffer)), | |
| 30 buffer_height_(CVPixelBufferGetHeight(pixel_buffer)), | |
| 31 crop_width_(crop_width), crop_height_(crop_height), | |
| 32 // Can only crop at even pixels. | |
| 33 crop_x_(crop_x & ~1), crop_y_(crop_y & ~1) { | |
| 34 CVBufferRetain(pixel_buffer_); | |
| 35 } | |
| 36 | |
| 19 CoreVideoFrameBuffer::CoreVideoFrameBuffer(CVPixelBufferRef pixel_buffer) | 37 CoreVideoFrameBuffer::CoreVideoFrameBuffer(CVPixelBufferRef pixel_buffer) |
| 20 : NativeHandleBuffer(pixel_buffer, | 38 : NativeHandleBuffer(pixel_buffer, |
| 21 CVPixelBufferGetWidth(pixel_buffer), | 39 CVPixelBufferGetWidth(pixel_buffer), |
| 22 CVPixelBufferGetHeight(pixel_buffer)), | 40 CVPixelBufferGetHeight(pixel_buffer)), |
| 23 pixel_buffer_(pixel_buffer) { | 41 pixel_buffer_(pixel_buffer), |
| 42 buffer_width_(width_), buffer_height_(height_), | |
| 43 crop_width_(width_), crop_height_(height_), | |
| 44 crop_x_(0), crop_y_(0) { | |
| 24 CVBufferRetain(pixel_buffer_); | 45 CVBufferRetain(pixel_buffer_); |
| 25 } | 46 } |
| 26 | 47 |
| 27 CoreVideoFrameBuffer::~CoreVideoFrameBuffer() { | 48 CoreVideoFrameBuffer::~CoreVideoFrameBuffer() { |
| 28 CVBufferRelease(pixel_buffer_); | 49 CVBufferRelease(pixel_buffer_); |
| 29 } | 50 } |
| 30 | 51 |
| 31 rtc::scoped_refptr<VideoFrameBuffer> | 52 rtc::scoped_refptr<VideoFrameBuffer> |
| 32 CoreVideoFrameBuffer::NativeToI420Buffer() { | 53 CoreVideoFrameBuffer::NativeToI420Buffer() { |
| 33 RTC_DCHECK(CVPixelBufferGetPixelFormatType(pixel_buffer_) == | 54 const OSType pixel_format = CVPixelBufferGetPixelFormatType(pixel_buffer_); |
| 34 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange); | 55 RTC_DCHECK(pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange || |
| 35 size_t width = CVPixelBufferGetWidthOfPlane(pixel_buffer_, 0); | 56 pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange); |
| 36 size_t height = CVPixelBufferGetHeightOfPlane(pixel_buffer_, 0); | 57 |
| 37 // TODO(tkchin): Use a frame buffer pool. | |
| 38 rtc::scoped_refptr<webrtc::I420Buffer> buffer = | |
| 39 new rtc::RefCountedObject<webrtc::I420Buffer>(width, height); | |
| 40 CVPixelBufferLockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly); | 58 CVPixelBufferLockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly); |
| 41 const uint8_t* src_y = static_cast<const uint8_t*>( | 59 const uint8_t* src_y = static_cast<const uint8_t*>( |
| 42 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 0)); | 60 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 0)); |
| 43 int src_y_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 0); | 61 const int src_y_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 0); |
| 44 const uint8_t* src_uv = static_cast<const uint8_t*>( | 62 const uint8_t* src_uv = static_cast<const uint8_t*>( |
| 45 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 1)); | 63 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 1)); |
| 46 int src_uv_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 1); | 64 const int src_uv_stride = |
| 47 int ret = libyuv::NV12ToI420( | 65 CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 1); |
| 48 src_y, src_y_stride, src_uv, src_uv_stride, | 66 |
| 67 // Crop just by modifying pointers. | |
| 68 src_y += src_y_stride * crop_y_ + crop_x_; | |
| 69 src_uv += src_uv_stride * (crop_y_ / 2) + crop_x_; | |
| 70 | |
| 71 // TODO(magjed): Use a frame buffer pool. | |
| 72 NV12ToI420Scaler nv12_to_i420_scaler; | |
| 73 rtc::scoped_refptr<I420Buffer> buffer = | |
| 74 new rtc::RefCountedObject<I420Buffer>(width_, height_); | |
| 75 nv12_to_i420_scaler.NV12ToI420Scale( | |
| 76 src_y, src_y_stride, | |
| 77 src_uv, src_uv_stride, | |
| 78 crop_width_, crop_height_, | |
| 49 buffer->MutableDataY(), buffer->StrideY(), | 79 buffer->MutableDataY(), buffer->StrideY(), |
| 50 buffer->MutableDataU(), buffer->StrideU(), | 80 buffer->MutableDataU(), buffer->StrideU(), |
| 51 buffer->MutableDataV(), buffer->StrideV(), | 81 buffer->MutableDataV(), buffer->StrideV(), |
| 52 width, height); | 82 buffer->width(), buffer->height()); |
| 83 | |
| 53 CVPixelBufferUnlockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly); | 84 CVPixelBufferUnlockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly); |
| 54 if (ret) { | 85 |
| 55 LOG(LS_ERROR) << "Error converting NV12 to I420: " << ret; | |
| 56 return nullptr; | |
| 57 } | |
| 58 return buffer; | 86 return buffer; |
| 59 } | 87 } |
| 60 | 88 |
| 89 bool CoreVideoFrameBuffer::RequiresCropping() const { | |
| 90 return crop_width_ != buffer_width_ || crop_height_ != buffer_height_; | |
| 91 } | |
| 92 | |
| 93 bool CoreVideoFrameBuffer::CropAndScaleTo( | |
| 94 std::vector<uint8_t>* tmp_buffer, | |
|
tkchin_webrtc
2016/10/19 00:48:32
is buffer passed in so that it can be re-used acro
magjed_webrtc
2016/10/19 13:19:33
Yes exactly.
| |
| 95 CVPixelBufferRef output_pixel_buffer) const { | |
| 96 // Prepare output pointers. | |
| 97 RTC_DCHECK_EQ(CVPixelBufferGetPixelFormatType(output_pixel_buffer), | |
| 98 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange); | |
| 99 CVReturn cvRet = CVPixelBufferLockBaseAddress(output_pixel_buffer, 0); | |
|
tkchin_webrtc
2016/10/19 00:48:32
nit: cv_ret
magjed_webrtc
2016/10/19 13:19:34
Done.
| |
| 100 if (cvRet != kCVReturnSuccess) { | |
| 101 LOG(LS_ERROR) << "Failed to lock base address: " << cvRet; | |
| 102 return false; | |
| 103 } | |
| 104 const int dst_width = CVPixelBufferGetWidth(output_pixel_buffer); | |
| 105 const int dst_height = CVPixelBufferGetHeight(output_pixel_buffer); | |
| 106 uint8_t* dst_y = reinterpret_cast<uint8_t*>( | |
| 107 CVPixelBufferGetBaseAddressOfPlane(output_pixel_buffer, 0)); | |
| 108 const int dst_y_stride = | |
| 109 CVPixelBufferGetBytesPerRowOfPlane(output_pixel_buffer, 0); | |
| 110 uint8_t* dst_uv = reinterpret_cast<uint8_t*>( | |
| 111 CVPixelBufferGetBaseAddressOfPlane(output_pixel_buffer, 1)); | |
| 112 const int dst_uv_stride = | |
| 113 CVPixelBufferGetBytesPerRowOfPlane(output_pixel_buffer, 1); | |
| 114 | |
| 115 // Prepare source pointers. | |
| 116 const OSType src_pixel_format = | |
| 117 CVPixelBufferGetPixelFormatType(pixel_buffer_); | |
| 118 RTC_DCHECK( | |
| 119 src_pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange || | |
| 120 src_pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange); | |
| 121 CVPixelBufferLockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly); | |
| 122 const uint8_t* src_y = static_cast<const uint8_t*>( | |
| 123 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 0)); | |
| 124 const int src_y_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 0); | |
| 125 const uint8_t* src_uv = static_cast<const uint8_t*>( | |
| 126 CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 1)); | |
| 127 const int src_uv_stride = | |
| 128 CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 1); | |
| 129 | |
| 130 // Crop just by modifying pointers. | |
| 131 src_y += src_y_stride * crop_y_ + crop_x_; | |
| 132 src_uv += src_uv_stride * (crop_y_ / 2) + crop_x_; | |
| 133 | |
| 134 NV12Scale(tmp_buffer, | |
| 135 src_y, src_y_stride, | |
| 136 src_uv, src_uv_stride, | |
| 137 crop_width_, crop_height_, | |
| 138 dst_y, dst_y_stride, | |
| 139 dst_uv, dst_uv_stride, | |
| 140 dst_width, dst_height); | |
| 141 | |
| 142 CVPixelBufferUnlockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly); | |
|
tkchin_webrtc
2016/10/19 00:48:32
not locked as readonly
magjed_webrtc
2016/10/19 13:19:34
Yes it is?
tkchin_webrtc
2016/10/19 22:50:17
ah, misread. You are correct.
| |
| 143 CVPixelBufferUnlockBaseAddress(output_pixel_buffer, 0); | |
| 144 | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 61 } // namespace webrtc | 148 } // namespace webrtc |
| OLD | NEW |