OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 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/api/androidvideotracksource.h" | 11 #include "webrtc/api/androidvideotracksource.h" |
12 | 12 |
13 #include <utility> | 13 #include <utility> |
14 | 14 |
15 #include "third_party/libyuv/include/libyuv/convert.h" | |
16 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | |
17 | |
18 namespace webrtc { | 15 namespace webrtc { |
19 | 16 |
20 AndroidVideoTrackSource::AndroidVideoTrackSource(rtc::Thread* signaling_thread, | 17 AndroidVideoTrackSource::AndroidVideoTrackSource(rtc::Thread* signaling_thread, |
21 JNIEnv* jni, | 18 JNIEnv* jni, |
22 jobject j_egl_context, | 19 jobject j_egl_context, |
23 bool is_screencast) | 20 bool is_screencast) |
24 : signaling_thread_(signaling_thread), | 21 : signaling_thread_(signaling_thread), |
25 surface_texture_helper_(webrtc_jni::SurfaceTextureHelper::create( | 22 surface_texture_helper_(webrtc_jni::SurfaceTextureHelper::create( |
26 jni, | 23 jni, |
27 "Camera SurfaceTextureHelper", | 24 "Camera SurfaceTextureHelper", |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 int crop_x; | 99 int crop_x; |
103 int crop_y; | 100 int crop_y; |
104 int64_t translated_camera_time_us; | 101 int64_t translated_camera_time_us; |
105 | 102 |
106 if (!AdaptFrame(width, height, timestamp_ns / rtc::kNumNanosecsPerMicrosec, | 103 if (!AdaptFrame(width, height, timestamp_ns / rtc::kNumNanosecsPerMicrosec, |
107 &adapted_width, &adapted_height, &crop_width, &crop_height, | 104 &adapted_width, &adapted_height, &crop_width, &crop_height, |
108 &crop_x, &crop_y, &translated_camera_time_us)) { | 105 &crop_x, &crop_y, &translated_camera_time_us)) { |
109 return; | 106 return; |
110 } | 107 } |
111 | 108 |
| 109 int rotated_width = crop_width; |
| 110 int rotated_height = crop_height; |
| 111 |
| 112 rtc::CritScope lock(&apply_rotation_crit_); |
| 113 if (apply_rotation_ && (rotation == 90 || rotation == 270)) { |
| 114 std::swap(adapted_width, adapted_height); |
| 115 std::swap(rotated_width, rotated_height); |
| 116 } |
| 117 |
| 118 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = |
| 119 pre_scale_pool_.CreateBuffer(rotated_width, rotated_height); |
| 120 |
112 const uint8_t* y_plane = static_cast<const uint8_t*>(frame_data); | 121 const uint8_t* y_plane = static_cast<const uint8_t*>(frame_data); |
113 const uint8_t* uv_plane = y_plane + width * height; | 122 const uint8_t* uv_plane = y_plane + width * height; |
114 const int uv_width = (width + 1) / 2; | 123 int uv_width = (width + 1) / 2; |
115 | 124 |
116 RTC_CHECK_GE(length, width * height + 2 * uv_width * ((height + 1) / 2)); | 125 RTC_CHECK_GE(length, width * height + 2 * uv_width * ((height + 1) / 2)); |
117 | 126 |
118 // Can only crop at even pixels. | 127 // Can only crop at even pixels. |
119 crop_x &= ~1; | 128 crop_x &= ~1; |
120 crop_y &= ~1; | 129 crop_y &= ~1; |
121 // Crop just by modifying pointers. | |
122 y_plane += width * crop_y + crop_x; | |
123 uv_plane += uv_width * crop_y + crop_x; | |
124 | 130 |
125 rtc::scoped_refptr<webrtc::I420Buffer> buffer = | 131 libyuv::NV12ToI420Rotate( |
126 buffer_pool_.CreateBuffer(adapted_width, adapted_height); | 132 y_plane + width * crop_y + crop_x, width, |
| 133 uv_plane + uv_width * crop_y + crop_x, width, buffer->MutableDataY(), |
| 134 buffer->StrideY(), |
| 135 // Swap U and V, since we have NV21, not NV12. |
| 136 buffer->MutableDataV(), buffer->StrideV(), buffer->MutableDataU(), |
| 137 buffer->StrideU(), crop_width, crop_height, |
| 138 static_cast<libyuv::RotationMode>(apply_rotation_ ? rotation : 0)); |
127 | 139 |
128 if (adapted_width == crop_width && adapted_height == crop_height) { | 140 if (adapted_width != buffer->width() || adapted_height != buffer->height()) { |
129 // No scaling. | 141 rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer( |
130 libyuv::NV12ToI420( | 142 post_scale_pool_.CreateBuffer(adapted_width, adapted_height)); |
131 y_plane, width, | 143 scaled_buffer->ScaleFrom(buffer); |
132 uv_plane, uv_width * 2, | 144 buffer = scaled_buffer; |
133 buffer->MutableDataY(), buffer->StrideY(), | |
134 // Swap U and V, since we have NV21, not NV12. | |
135 buffer->MutableDataV(), buffer->StrideV(), | |
136 buffer->MutableDataU(), buffer->StrideU(), | |
137 buffer->width(), buffer->height()); | |
138 | |
139 } else { | |
140 // Scaling. | |
141 const int crop_uv_width = (crop_width + 1) / 2; | |
142 const int crop_uv_height = (crop_height + 1) / 2; | |
143 unscaled_uv_planes_.resize(crop_uv_width * crop_uv_height * 2); | |
144 | |
145 NV12ToI420Scale( | |
146 unscaled_uv_planes_.data(), | |
147 y_plane, width, | |
148 uv_plane, uv_width * 2, | |
149 crop_width, crop_height, | |
150 buffer->MutableDataY(), buffer->StrideY(), | |
151 // Swap U and V, since we have NV21, not NV12. | |
152 buffer->MutableDataV(), buffer->StrideV(), | |
153 buffer->MutableDataU(), buffer->StrideU(), | |
154 buffer->width(), buffer->height()); | |
155 } | |
156 | |
157 // Applying rotation is only supported for legacy reasons, and the performance | |
158 // for this path is not critical. | |
159 rtc::CritScope lock(&apply_rotation_crit_); | |
160 if (apply_rotation_ && rotation != 0) { | |
161 rtc::scoped_refptr<I420Buffer> rotated_buffer = I420Buffer::Create( | |
162 rotation == 180 ? buffer->width() : buffer->height(), | |
163 rotation == 180 ? buffer->height() : buffer->width()); | |
164 | |
165 libyuv::I420Rotate( | |
166 buffer->DataY(), buffer->StrideY(), | |
167 buffer->DataU(), buffer->StrideU(), | |
168 buffer->DataV(), buffer->StrideV(), | |
169 rotated_buffer->MutableDataY(), rotated_buffer->StrideY(), | |
170 rotated_buffer->MutableDataU(), rotated_buffer->StrideU(), | |
171 rotated_buffer->MutableDataV(), rotated_buffer->StrideV(), | |
172 buffer->width(), buffer->height(), | |
173 static_cast<libyuv::RotationMode>(rotation)); | |
174 | |
175 buffer = rotated_buffer; | |
176 } | 145 } |
177 | 146 |
178 OnFrame(cricket::WebRtcVideoFrame( | 147 OnFrame(cricket::WebRtcVideoFrame( |
179 buffer, | 148 buffer, |
180 apply_rotation_ ? webrtc::kVideoRotation_0 | 149 apply_rotation_ ? webrtc::kVideoRotation_0 |
181 : static_cast<webrtc::VideoRotation>(rotation), | 150 : static_cast<webrtc::VideoRotation>(rotation), |
182 translated_camera_time_us, 0), | 151 translated_camera_time_us, 0), |
183 width, height); | 152 width, height); |
184 } | 153 } |
185 | 154 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 } | 253 } |
285 *crop_x = (width - *crop_width) / 2; | 254 *crop_x = (width - *crop_width) / 2; |
286 *crop_y = (height - *crop_height) / 2; | 255 *crop_y = (height - *crop_height) / 2; |
287 | 256 |
288 *translated_camera_time_us = timestamp_aligner_.ClipTimestamp( | 257 *translated_camera_time_us = timestamp_aligner_.ClipTimestamp( |
289 camera_time_us + offset_us, system_time_us); | 258 camera_time_us + offset_us, system_time_us); |
290 return true; | 259 return true; |
291 } | 260 } |
292 | 261 |
293 } // namespace webrtc | 262 } // namespace webrtc |
OLD | NEW |