Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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 <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #include "libyuv/convert.h" | |
| 14 #include "libyuv/scale.h" | |
| 15 | |
| 13 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
| 14 #include "webrtc/media/engine/webrtcvideoframe.h" | 17 #include "webrtc/media/engine/webrtcvideoframe.h" |
| 15 #include "webrtc/media/engine/webrtcvideoframefactory.h" | 18 #include "webrtc/media/engine/webrtcvideoframefactory.h" |
| 16 | 19 |
| 20 // TODO(nisse): Needed for the struct CapturedFrame. Move declaration | |
| 21 // to videoframefactory.h instead? | |
| 22 #include "webrtc/media/base/videocapturer.h" | |
| 23 | |
| 17 namespace cricket { | 24 namespace cricket { |
| 18 | 25 |
| 26 std::unique_ptr<VideoFrame> WebRtcVideoFrameFactory::CreateScaledFrame( | |
| 27 const CapturedFrame* input_frame, | |
| 28 int dst_width, | |
| 29 int dst_height) const { | |
| 30 // Size of the used portion of the input frame, updated for | |
| 31 // cropping, but pre-rotation. | |
| 32 int crop_width = input_frame->width; | |
| 33 int crop_height = input_frame->height; | |
| 34 | |
| 35 // Cropp the input frame, if needed to preserve aspect ratio. | |
| 36 long aspect_diff = static_cast<long>(crop_width) * dst_height - | |
| 37 static_cast<long>(crop_height) * dst_width; | |
| 38 if (aspect_diff > 0) { | |
|
magjed_webrtc
2016/05/09 13:44:55
This code will be the same for all implementations
nisse-webrtc
2016/05/10 07:56:22
It used to be a helper function in videocommon. I
| |
| 39 // Horizontal crop. | |
| 40 | |
| 41 // TODO(nisse): Old code carried a comment saying that MJPG can | |
| 42 // only be cropped vertically, and disabled horizontal cropping in | |
| 43 // that case. Still needed? | |
| 44 crop_width -= aspect_diff / dst_height; | |
| 45 RTC_DCHECK(crop_width > 0); | |
| 46 } else if (aspect_diff > 0) { | |
| 47 // Vertical crop. | |
| 48 crop_height -= (-aspect_diff) / dst_width; | |
| 49 RTC_DCHECK(crop_height > 0); | |
| 50 } | |
| 51 | |
| 52 webrtc::VideoRotation rotation = input_frame->rotation; | |
| 53 // The size of the input frame, after cropping and rotation. | |
| 54 int src_width = crop_width; | |
| 55 int src_height = crop_height; | |
| 56 if (apply_rotation_ && (rotation == webrtc::kVideoRotation_90 || | |
| 57 rotation == webrtc::kVideoRotation_270)) { | |
| 58 std::swap(dst_width, dst_height); | |
| 59 std::swap(src_width, src_height); | |
| 60 } | |
| 61 | |
| 62 rtc::scoped_refptr<webrtc::I420Buffer> buffer( | |
| 63 new rtc::RefCountedObject<webrtc::I420Buffer>(src_width, src_height)); | |
| 64 | |
| 65 // Color space conversion, cropping, and rotation. | |
| 66 uint32_t format = CanonicalFourCC(input_frame->fourcc); | |
| 67 int r = libyuv::ConvertToI420( | |
| 68 static_cast<const uint8_t*>(input_frame->data), input_frame->data_size, | |
| 69 buffer->MutableDataY(), buffer->StrideY(), | |
| 70 buffer->MutableDataU(), buffer->StrideU(), | |
| 71 buffer->MutableDataV(), buffer->StrideV(), | |
| 72 // Cropping coordinates, and width and height, are pre-rotation values. | |
| 73 (input_frame->width - crop_width) / 2, | |
| 74 (input_frame->height - crop_height) / 2, | |
| 75 input_frame->width, input_frame->height, | |
| 76 crop_width, crop_height, | |
| 77 static_cast<libyuv::RotationMode>( | |
| 78 apply_rotation_ ? rotation : webrtc::kVideoRotation_0), | |
| 79 format); | |
| 80 if (r) { | |
| 81 LOG(LS_ERROR) << "Error parsing format: " << GetFourccName(format) | |
| 82 << " return code : " << r; | |
| 83 return nullptr; | |
| 84 } | |
| 85 | |
| 86 // Scaling, if needed. | |
| 87 if (src_width != dst_width || src_height != dst_height) { | |
| 88 rtc::scoped_refptr<webrtc::I420Buffer> scaled( | |
| 89 new rtc::RefCountedObject<webrtc::I420Buffer>(dst_width, dst_height)); | |
| 90 | |
| 91 if (libyuv::I420Scale(buffer->DataY(), buffer->StrideY(), | |
| 92 buffer->DataU(), buffer->StrideU(), | |
| 93 buffer->DataV(), buffer->StrideV(), | |
| 94 buffer->width(), buffer->height(), | |
| 95 scaled->MutableDataY(), scaled->StrideY(), | |
| 96 scaled->MutableDataU(), scaled->StrideU(), | |
| 97 scaled->MutableDataV(), scaled->StrideV(), | |
| 98 scaled->width(), scaled->height(), | |
| 99 libyuv::kFilterBox) < 0) { | |
| 100 LOG(LS_ERROR) << "I420Scale failed: src size " | |
| 101 << buffer->width() << " x " << buffer->height() | |
| 102 << ", dst size: " | |
| 103 << scaled->width() << " x " << scaled->height(); | |
| 104 return nullptr; | |
| 105 } | |
| 106 buffer = scaled; | |
| 107 } | |
| 108 return std::unique_ptr<VideoFrame>(new WebRtcVideoFrame( | |
| 109 buffer, apply_rotation_ ? webrtc::kVideoRotation_0 : rotation, | |
| 110 input_frame->time_stamp / rtc::kNumNanosecsPerMicrosec)); | |
| 111 } | |
| 112 | |
| 19 VideoFrame* WebRtcVideoFrameFactory::CreateAliasedFrame( | 113 VideoFrame* WebRtcVideoFrameFactory::CreateAliasedFrame( |
| 20 const CapturedFrame* aliased_frame, int width, int height) const { | 114 const CapturedFrame* aliased_frame, int width, int height) const { |
| 21 std::unique_ptr<WebRtcVideoFrame> frame(new WebRtcVideoFrame()); | 115 std::unique_ptr<WebRtcVideoFrame> frame(new WebRtcVideoFrame()); |
| 22 if (!frame->Init(aliased_frame, width, height, apply_rotation_)) { | 116 if (!frame->Init(aliased_frame, width, height, apply_rotation_)) { |
| 23 LOG(LS_ERROR) << | 117 LOG(LS_ERROR) << |
| 24 "Failed to create WebRtcVideoFrame in CreateAliasedFrame."; | 118 "Failed to create WebRtcVideoFrame in CreateAliasedFrame."; |
| 25 return NULL; | 119 return NULL; |
| 26 } | 120 } |
| 27 return frame.release(); | 121 return frame.release(); |
| 28 } | 122 } |
| 29 | 123 |
| 30 } // namespace cricket | 124 } // namespace cricket |
| OLD | NEW |