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" |
| 17 // #include "webrtc/common_video/include/video_frame_buffer.h" | |
|
magjed_webrtc
2016/05/09 10:58:08
remove if not used
nisse-webrtc
2016/05/09 11:06:01
Done.
| |
| 14 #include "webrtc/media/engine/webrtcvideoframe.h" | 18 #include "webrtc/media/engine/webrtcvideoframe.h" |
| 15 #include "webrtc/media/engine/webrtcvideoframefactory.h" | 19 #include "webrtc/media/engine/webrtcvideoframefactory.h" |
| 16 | 20 |
| 21 // TODO(nisse): Needed for the struct CapturedFrame. Move declaration | |
| 22 // to videoframefactory.h instead? | |
| 23 #include "webrtc/media/base/videocapturer.h" | |
| 24 | |
| 17 namespace cricket { | 25 namespace cricket { |
| 18 | 26 |
| 27 std::unique_ptr<VideoFrame> WebRtcVideoFrameFactory::CreateScaledFrame( | |
| 28 const CapturedFrame* input_frame, | |
| 29 int width, | |
| 30 int height) const { | |
| 31 webrtc::VideoRotation rotation = input_frame->rotation; | |
| 32 int rotated_width; | |
| 33 int rotated_height; | |
| 34 if (apply_rotation_ && (rotation == webrtc::kVideoRotation_90 || | |
| 35 rotation == webrtc::kVideoRotation_270)) { | |
| 36 rotated_width = input_frame->height; | |
| 37 rotated_height = input_frame->width; | |
| 38 } else { | |
| 39 rotated_width = input_frame->width; | |
| 40 rotated_height = input_frame->height; | |
| 41 } | |
| 42 uint32_t format = CanonicalFourCC(input_frame->fourcc); | |
|
magjed_webrtc
2016/05/09 10:58:08
Move this to the line above libyuv::ConvertToI420.
nisse-webrtc
2016/05/09 11:06:01
Done.
| |
| 43 rtc::scoped_refptr<webrtc::I420Buffer> buffer( | |
| 44 new rtc::RefCountedObject<webrtc::I420Buffer>(rotated_width, | |
| 45 rotated_height)); | |
| 46 | |
| 47 // TODO(nisse): Do center crop if needed to preserve aspect ratio. | |
|
magjed_webrtc
2016/05/09 10:58:08
I would like if you implemented this immediately.
nisse-webrtc
2016/05/09 11:06:01
Ok, I'll give it a try.
| |
| 48 // Old code carried a comment saying that MJPG can only be cropped | |
| 49 // vertically, and disabled cropping in this case. | |
| 50 | |
| 51 // Color space conversion, and rotation. | |
| 52 int r = libyuv::ConvertToI420( | |
| 53 static_cast<const uint8_t*>(input_frame->data), input_frame->data_size, | |
| 54 buffer->MutableDataY(), buffer->StrideY(), | |
| 55 buffer->MutableDataU(), buffer->StrideU(), | |
| 56 buffer->MutableDataV(), buffer->StrideV(), | |
| 57 0, 0, /* No cropping. */ | |
| 58 buffer->width(), buffer->height(), | |
| 59 rotated_width, rotated_height, | |
| 60 static_cast<libyuv::RotationMode>( | |
| 61 apply_rotation_ ? rotation : webrtc::kVideoRotation_0), | |
| 62 format); | |
| 63 if (r) { | |
| 64 LOG(LS_ERROR) << "Error parsing format: " << GetFourccName(format) | |
| 65 << " return code : " << r; | |
| 66 return nullptr; | |
| 67 } | |
| 68 | |
| 69 // Scaling, if needed. Currently doesn't respect aspect ratio. | |
| 70 if (rotated_width != width || rotated_height != height) { | |
| 71 rtc::scoped_refptr<webrtc::I420Buffer> scaled( | |
| 72 new rtc::RefCountedObject<webrtc::I420Buffer>(width, height)); | |
| 73 | |
| 74 if (libyuv::I420Scale(buffer->DataY(), buffer->StrideY(), | |
| 75 buffer->DataU(), buffer->StrideU(), | |
| 76 buffer->DataV(), buffer->StrideV(), | |
| 77 buffer->width(), buffer->height(), | |
| 78 scaled->MutableDataY(), scaled->StrideY(), | |
| 79 scaled->MutableDataU(), scaled->StrideU(), | |
| 80 scaled->MutableDataV(), scaled->StrideV(), | |
| 81 scaled->width(), scaled->height(), | |
| 82 libyuv::kFilterBox) < 0) { | |
| 83 LOG(LS_ERROR) << "I420Scale failed: src size " | |
| 84 << buffer->width() << " x " << buffer->height() | |
| 85 << ", dst size: " | |
| 86 << scaled->width() << " x " << scaled->height(); | |
| 87 return nullptr; | |
| 88 } | |
| 89 buffer = scaled; | |
| 90 } | |
| 91 return std::unique_ptr<VideoFrame>(new WebRtcVideoFrame( | |
| 92 buffer, apply_rotation_ ? webrtc::kVideoRotation_0 : rotation, | |
| 93 input_frame->time_stamp / rtc::kNumNanosecsPerMicrosec)); | |
| 94 } | |
| 95 | |
| 19 VideoFrame* WebRtcVideoFrameFactory::CreateAliasedFrame( | 96 VideoFrame* WebRtcVideoFrameFactory::CreateAliasedFrame( |
| 20 const CapturedFrame* aliased_frame, int width, int height) const { | 97 const CapturedFrame* aliased_frame, int width, int height) const { |
| 21 std::unique_ptr<WebRtcVideoFrame> frame(new WebRtcVideoFrame()); | 98 std::unique_ptr<WebRtcVideoFrame> frame(new WebRtcVideoFrame()); |
| 22 if (!frame->Init(aliased_frame, width, height, apply_rotation_)) { | 99 if (!frame->Init(aliased_frame, width, height, apply_rotation_)) { |
| 23 LOG(LS_ERROR) << | 100 LOG(LS_ERROR) << |
| 24 "Failed to create WebRtcVideoFrame in CreateAliasedFrame."; | 101 "Failed to create WebRtcVideoFrame in CreateAliasedFrame."; |
| 25 return NULL; | 102 return NULL; |
| 26 } | 103 } |
| 27 return frame.release(); | 104 return frame.release(); |
| 28 } | 105 } |
| 29 | 106 |
| 30 } // namespace cricket | 107 } // namespace cricket |
| OLD | NEW |