|
12 | 12 |
|
13 | 13 | #include <utility> |
14 | 14 |
|
| 15 | +#include "third_party/libyuv/include/libyuv/rotate.h" |
| 16 | + |
15 | 17 | namespace webrtc { |
16 | 18 |
|
17 | 19 | AndroidVideoTrackSource::AndroidVideoTrackSource(rtc::Thread* signaling_thread, |
@@ -106,42 +108,51 @@ void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data, |
106 | 108 | return; |
107 | 109 | } |
108 | 110 |
|
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::I420Buffer> buffer = |
119 | | - pre_scale_pool_.CreateBuffer(rotated_width, rotated_height); |
120 | | - |
121 | 111 | const uint8_t* y_plane = static_cast<const uint8_t*>(frame_data); |
122 | 112 | const uint8_t* uv_plane = y_plane + width * height; |
123 | | - int uv_width = (width + 1) / 2; |
| 113 | + const int uv_width = (width + 1) / 2; |
124 | 114 |
|
125 | 115 | RTC_CHECK_GE(length, width * height + 2 * uv_width * ((height + 1) / 2)); |
126 | 116 |
|
127 | 117 | // Can only crop at even pixels. |
128 | 118 | crop_x &= ~1; |
129 | 119 | crop_y &= ~1; |
| 120 | + // Crop just by modifying pointers. |
| 121 | + y_plane += width * crop_y + crop_x; |
| 122 | + uv_plane += uv_width * crop_y + crop_x; |
| 123 | + |
| 124 | + rtc::scoped_refptr<webrtc::I420Buffer> buffer = |
| 125 | + buffer_pool_.CreateBuffer(adapted_width, adapted_height); |
130 | 126 |
|
131 | | - libyuv::NV12ToI420Rotate( |
132 | | - y_plane + width * crop_y + crop_x, width, |
133 | | - uv_plane + uv_width * crop_y + crop_x, width, buffer->MutableDataY(), |
134 | | - buffer->StrideY(), |
| 127 | + nv12toi420_scaler_.NV12ToI420Scale( |
| 128 | + y_plane, width, |
| 129 | + uv_plane, uv_width * 2, |
| 130 | + crop_width, crop_height, |
| 131 | + buffer->MutableDataY(), buffer->StrideY(), |
135 | 132 | // 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)); |
139 | | - |
140 | | - if (adapted_width != buffer->width() || adapted_height != buffer->height()) { |
141 | | - rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer( |
142 | | - post_scale_pool_.CreateBuffer(adapted_width, adapted_height)); |
143 | | - scaled_buffer->ScaleFrom(buffer); |
144 | | - buffer = scaled_buffer; |
| 133 | + buffer->MutableDataV(), buffer->StrideV(), |
| 134 | + buffer->MutableDataU(), buffer->StrideU(), |
| 135 | + buffer->width(), buffer->height()); |
| 136 | + |
| 137 | + // Applying rotation is only supported for legacy reasons, and the performance |
| 138 | + // for this path is not critical. |
| 139 | + rtc::CritScope lock(&apply_rotation_crit_); |
| 140 | + if (apply_rotation_ && rotation != 0) { |
| 141 | + rtc::scoped_refptr<I420Buffer> rotated_buffer = |
| 142 | + rotation == 180 ? I420Buffer::Create(buffer->width(), buffer->height()) |
| 143 | + : I420Buffer::Create(buffer->height(), buffer->width()); |
| 144 | + |
| 145 | + libyuv::I420Rotate( |
| 146 | + buffer->DataY(), buffer->StrideY(), |
| 147 | + buffer->DataU(), buffer->StrideU(), |
| 148 | + buffer->DataV(), buffer->StrideV(), |
| 149 | + rotated_buffer->MutableDataY(), rotated_buffer->StrideY(), |
| 150 | + rotated_buffer->MutableDataU(), rotated_buffer->StrideU(), |
| 151 | + rotated_buffer->MutableDataV(), rotated_buffer->StrideV(), |
| 152 | + buffer->width(), buffer->height(), |
| 153 | + static_cast<libyuv::RotationMode>(rotation)); |
| 154 | + |
| 155 | + buffer = rotated_buffer; |
145 | 156 | } |
146 | 157 |
|
147 | 158 | OnFrame(cricket::WebRtcVideoFrame( |
|
0 commit comments