Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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/video_frame_buffer.h" | 11 #include "webrtc/common_video/include/video_frame_buffer.h" |
| 12 | 12 |
| 13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/keep_ref_until_done.h" | 14 #include "webrtc/base/keep_ref_until_done.h" |
| 15 #include "libyuv/convert.h" | 15 #include "libyuv/convert.h" |
| 16 #include "libyuv/scale.h" | |
| 16 | 17 |
| 17 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. | 18 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
| 18 static const int kBufferAlignment = 64; | 19 static const int kBufferAlignment = 64; |
| 19 | 20 |
| 20 namespace webrtc { | 21 namespace webrtc { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 int I420DataSize(int height, int stride_y, int stride_u, int stride_v) { | 25 int I420DataSize(int height, int stride_y, int stride_u, int stride_v) { |
| 25 return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2); | 26 return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 buffer->DataU(), buffer->StrideU(), | 201 buffer->DataU(), buffer->StrideU(), |
| 201 buffer->DataV(), buffer->StrideV(), | 202 buffer->DataV(), buffer->StrideV(), |
| 202 copy->MutableDataY(), copy->StrideY(), | 203 copy->MutableDataY(), copy->StrideY(), |
| 203 copy->MutableDataU(), copy->StrideU(), | 204 copy->MutableDataU(), copy->StrideU(), |
| 204 copy->MutableDataV(), copy->StrideV(), | 205 copy->MutableDataV(), copy->StrideV(), |
| 205 width, height) == 0); | 206 width, height) == 0); |
| 206 | 207 |
| 207 return copy; | 208 return copy; |
| 208 } | 209 } |
| 209 | 210 |
| 211 rtc::scoped_refptr<I420Buffer> I420Buffer::CropAndScale( | |
| 212 const rtc::scoped_refptr<VideoFrameBuffer>& buffer, | |
| 213 int offset_x, | |
| 214 int offset_y, | |
| 215 int crop_width, | |
| 216 int crop_height, | |
| 217 int dst_width, | |
| 218 int dst_height) { | |
| 219 RTC_CHECK_LE(crop_width, buffer->width()); | |
| 220 RTC_CHECK_LE(crop_height, buffer->height()); | |
| 221 RTC_CHECK_LE(crop_width + offset_x, buffer->width()); | |
| 222 RTC_CHECK_LE(crop_height + offset_y, buffer->height()); | |
| 223 | |
| 224 rtc::scoped_refptr<I420Buffer> scaled = | |
| 225 new rtc::RefCountedObject<I420Buffer>(dst_width, dst_height); | |
| 226 | |
| 227 // Make sure offset is even so that u/v plane becomes aligned. | |
| 228 // TODO(nisse): Duplicated in ShallowCrop. | |
|
perkj_webrtc
2016/05/16 06:48:47
remove this todo. I am fine with this duplication.
| |
| 229 const int uv_offset_x = offset_x / 2; | |
| 230 const int uv_offset_y = offset_y / 2; | |
| 231 offset_x = uv_offset_x * 2; | |
| 232 offset_y = uv_offset_y * 2; | |
| 233 | |
| 234 const uint8_t* y_plane = buffer->DataY() + | |
| 235 buffer->StrideY() * offset_y + offset_x; | |
| 236 const uint8_t* u_plane = buffer->DataU() + | |
| 237 buffer->StrideU() * uv_offset_y + uv_offset_x; | |
| 238 const uint8_t* v_plane = buffer->DataV() + | |
| 239 buffer->StrideV() * uv_offset_y + uv_offset_x; | |
| 240 if (libyuv::I420Scale(y_plane, buffer->StrideY(), | |
| 241 u_plane, buffer->StrideU(), | |
| 242 v_plane, buffer->StrideV(), | |
| 243 crop_width, crop_height, | |
| 244 scaled->MutableDataY(), scaled->StrideY(), | |
| 245 scaled->MutableDataU(), scaled->StrideU(), | |
| 246 scaled->MutableDataV(), scaled->StrideV(), | |
| 247 dst_width, dst_height, libyuv::kFilterBox) < 0) { | |
| 248 return nullptr; | |
| 249 } | |
| 250 return scaled; | |
| 251 } | |
| 252 | |
| 253 rtc::scoped_refptr<I420Buffer> I420Buffer::Scale( | |
| 254 const rtc::scoped_refptr<VideoFrameBuffer>& buffer, | |
| 255 int dst_width, | |
| 256 int dst_height) { | |
| 257 return CropAndScale(buffer, 0, 0, buffer->width(), buffer->height(), | |
| 258 dst_width, dst_height); | |
| 259 } | |
| 260 | |
| 210 NativeHandleBuffer::NativeHandleBuffer(void* native_handle, | 261 NativeHandleBuffer::NativeHandleBuffer(void* native_handle, |
| 211 int width, | 262 int width, |
| 212 int height) | 263 int height) |
| 213 : native_handle_(native_handle), width_(width), height_(height) { | 264 : native_handle_(native_handle), width_(width), height_(height) { |
| 214 RTC_DCHECK(native_handle != nullptr); | 265 RTC_DCHECK(native_handle != nullptr); |
| 215 RTC_DCHECK_GT(width, 0); | 266 RTC_DCHECK_GT(width, 0); |
| 216 RTC_DCHECK_GT(height, 0); | 267 RTC_DCHECK_GT(height, 0); |
| 217 } | 268 } |
| 218 | 269 |
| 219 bool NativeHandleBuffer::IsMutable() { | 270 bool NativeHandleBuffer::IsMutable() { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 | 368 |
| 318 void* WrappedI420Buffer::native_handle() const { | 369 void* WrappedI420Buffer::native_handle() const { |
| 319 return nullptr; | 370 return nullptr; |
| 320 } | 371 } |
| 321 | 372 |
| 322 rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { | 373 rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { |
| 323 RTC_NOTREACHED(); | 374 RTC_NOTREACHED(); |
| 324 return nullptr; | 375 return nullptr; |
| 325 } | 376 } |
| 326 | 377 |
| 327 rtc::scoped_refptr<VideoFrameBuffer> ShallowCenterCrop( | 378 rtc::scoped_refptr<WrappedI420Buffer> WrappedI420Buffer::ShallowCrop( |
| 328 const rtc::scoped_refptr<VideoFrameBuffer>& buffer, | 379 const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 380 int offset_x, | |
| 381 int offset_y, | |
| 329 int cropped_width, | 382 int cropped_width, |
| 330 int cropped_height) { | 383 int cropped_height) { |
| 331 RTC_CHECK(buffer->native_handle() == nullptr); | 384 RTC_CHECK(buffer->native_handle() == nullptr); |
| 332 RTC_CHECK_LE(cropped_width, buffer->width()); | 385 RTC_CHECK_LE(cropped_width, buffer->width()); |
| 333 RTC_CHECK_LE(cropped_height, buffer->height()); | 386 RTC_CHECK_LE(cropped_height, buffer->height()); |
| 334 if (buffer->width() == cropped_width && buffer->height() == cropped_height) | |
| 335 return buffer; | |
| 336 | 387 |
| 337 // Center crop to |cropped_width| x |cropped_height|. | |
| 338 // Make sure offset is even so that u/v plane becomes aligned. | 388 // Make sure offset is even so that u/v plane becomes aligned. |
| 339 const int uv_offset_x = (buffer->width() - cropped_width) / 4; | 389 const int uv_offset_x = offset_x / 2; |
| 340 const int uv_offset_y = (buffer->height() - cropped_height) / 4; | 390 const int uv_offset_y = offset_y / 2; |
| 341 const int offset_x = uv_offset_x * 2; | 391 offset_x = uv_offset_x * 2; |
| 342 const int offset_y = uv_offset_y * 2; | 392 offset_y = uv_offset_y * 2; |
| 343 | 393 |
| 344 const uint8_t* y_plane = buffer->DataY() + | 394 const uint8_t* y_plane = buffer->DataY() + |
| 345 buffer->StrideY() * offset_y + offset_x; | 395 buffer->StrideY() * offset_y + offset_x; |
| 346 const uint8_t* u_plane = buffer->DataU() + | 396 const uint8_t* u_plane = buffer->DataU() + |
| 347 buffer->StrideU() * uv_offset_y + uv_offset_x; | 397 buffer->StrideU() * uv_offset_y + uv_offset_x; |
| 348 const uint8_t* v_plane = buffer->DataV() + | 398 const uint8_t* v_plane = buffer->DataV() + |
| 349 buffer->StrideV() * uv_offset_y + uv_offset_x; | 399 buffer->StrideV() * uv_offset_y + uv_offset_x; |
| 350 return new rtc::RefCountedObject<WrappedI420Buffer>( | 400 return new rtc::RefCountedObject<WrappedI420Buffer>( |
| 351 cropped_width, cropped_height, | 401 cropped_width, cropped_height, |
| 352 y_plane, buffer->StrideY(), | 402 y_plane, buffer->StrideY(), |
| 353 u_plane, buffer->StrideU(), | 403 u_plane, buffer->StrideU(), |
| 354 v_plane, buffer->StrideV(), | 404 v_plane, buffer->StrideV(), |
| 355 rtc::KeepRefUntilDone(buffer)); | 405 rtc::KeepRefUntilDone(buffer)); |
| 356 } | 406 } |
| 357 | 407 |
| 408 // TODO(nisse): Used only by | |
| 409 // AndroidVideoCapturer::FrameFactory::CreateAliasedFrame. Delete when | |
| 410 // the CreateAliasedFrame method is removed. | |
| 411 rtc::scoped_refptr<VideoFrameBuffer> ShallowCenterCrop( | |
| 412 const rtc::scoped_refptr<VideoFrameBuffer>& buffer, | |
| 413 int cropped_width, | |
| 414 int cropped_height) { | |
| 415 return WrappedI420Buffer::ShallowCrop( | |
| 416 buffer, | |
| 417 (buffer->width() - cropped_width) / 2, | |
| 418 (buffer->height() - cropped_height) / 2, | |
| 419 cropped_width, cropped_height); | |
| 420 } | |
| 421 | |
| 358 } // namespace webrtc | 422 } // namespace webrtc |
| OLD | NEW |