OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 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 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 const float ROTATE_270[16] = | 52 const float ROTATE_270[16] = |
53 { -a[4], -a[5], -a[6], -a[7], | 53 { -a[4], -a[5], -a[6], -a[7], |
54 a[0], a[1], a[2], a[3], | 54 a[0], a[1], a[2], a[3], |
55 a[8], a[9], a[10], a[11], | 55 a[8], a[9], a[10], a[11], |
56 a[4] + a[12], a[5] + a[13], a[6] + a[14], a[7] + a[15]}; | 56 a[4] + a[12], a[5] + a[13], a[6] + a[14], a[7] + a[15]}; |
57 memcpy(a, ROTATE_270, sizeof(ROTATE_270)); | 57 memcpy(a, ROTATE_270, sizeof(ROTATE_270)); |
58 } break; | 58 } break; |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
| 62 // Calculates result = a * b, in column-major order. |
| 63 void MultiplyMatrix(const float a[16], const float b[16], float result[16]) { |
| 64 for (int i = 0; i < 4; ++i) { |
| 65 for (int j = 0; j < 4; ++j) { |
| 66 float sum = 0; |
| 67 for (int k = 0; k < 4; ++k) { |
| 68 sum += a[k * 4 + j] * b[i * 4 + k]; |
| 69 } |
| 70 result[i * 4 + j] = sum; |
| 71 } |
| 72 } |
| 73 } |
| 74 |
| 75 // Center crop by keeping xFraction of the width and yFraction of the height, |
| 76 // so e.g. cropping from 640x480 to 640x360 would use |
| 77 // xFraction=1, yFraction=360/480. |
| 78 void CropMatrix(float a[16], float xFraction, float yFraction) { |
| 79 // Move cropped area to the center of the frame by offsetting half the |
| 80 // removed area. |
| 81 const float xOffset = (1 - xFraction) / 2; |
| 82 const float yOffset = (1 - yFraction) / 2; |
| 83 const float crop_matrix[16] = { |
| 84 xFraction, 0, 0, 0, |
| 85 0, yFraction, 0, 0, |
| 86 0, 0, 1, 0, |
| 87 xOffset, yOffset, 0, 1}; |
| 88 float mul_result[16]; |
| 89 MultiplyMatrix(crop_matrix, a, mul_result); |
| 90 memcpy(a, mul_result, sizeof(mul_result)); |
| 91 } |
| 92 |
62 } // anonymouse namespace | 93 } // anonymouse namespace |
63 | 94 |
64 namespace webrtc_jni { | 95 namespace webrtc_jni { |
65 | 96 |
66 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. | 97 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
67 static const int kBufferAlignment = 64; | 98 static const int kBufferAlignment = 64; |
68 | 99 |
69 NativeHandleImpl::NativeHandleImpl(JNIEnv* jni, | 100 NativeHandleImpl::NativeHandleImpl(JNIEnv* jni, |
70 jint j_oes_texture_id, | 101 jint j_oes_texture_id, |
71 jfloatArray j_transform_matrix) | 102 jfloatArray j_transform_matrix) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 jni->CallVoidMethod(surface_texture_helper_, | 170 jni->CallVoidMethod(surface_texture_helper_, |
140 transform_mid, | 171 transform_mid, |
141 byte_buffer, width(), height(), stride, | 172 byte_buffer, width(), height(), stride, |
142 native_handle_.oes_texture_id, sampling_matrix); | 173 native_handle_.oes_texture_id, sampling_matrix); |
143 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception"; | 174 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception"; |
144 | 175 |
145 return copy; | 176 return copy; |
146 } | 177 } |
147 | 178 |
148 rtc::scoped_refptr<AndroidTextureBuffer> | 179 rtc::scoped_refptr<AndroidTextureBuffer> |
149 AndroidTextureBuffer::ScaleAndRotate(int dst_widht, | 180 AndroidTextureBuffer::CropScaleAndRotate(int cropped_width, |
150 int dst_height, | 181 int cropped_height, |
151 webrtc::VideoRotation rotation) { | 182 int dst_width, |
152 if (width() == dst_widht && height() == dst_height && | 183 int dst_height, |
| 184 webrtc::VideoRotation rotation) { |
| 185 if (cropped_width == dst_width && cropped_height == dst_height && |
| 186 width() == dst_width && height() == dst_height && |
153 rotation == webrtc::kVideoRotation_0) { | 187 rotation == webrtc::kVideoRotation_0) { |
154 return this; | 188 return this; |
155 } | 189 } |
156 int rotated_width = (rotation % 180 == 0) ? dst_widht : dst_height; | 190 int rotated_width = (rotation % 180 == 0) ? dst_width : dst_height; |
157 int rotated_height = (rotation % 180 == 0) ? dst_height : dst_widht; | 191 int rotated_height = (rotation % 180 == 0) ? dst_height : dst_width; |
158 | 192 |
159 // Here we use Bind magic to add a reference count to |this| until the newly | 193 // Here we use Bind magic to add a reference count to |this| until the newly |
160 // created AndroidTextureBuffer is destructed | 194 // created AndroidTextureBuffer is destructed |
161 rtc::scoped_refptr<AndroidTextureBuffer> buffer( | 195 rtc::scoped_refptr<AndroidTextureBuffer> buffer( |
162 new rtc::RefCountedObject<AndroidTextureBuffer>( | 196 new rtc::RefCountedObject<AndroidTextureBuffer>( |
163 rotated_width, rotated_height, native_handle_, | 197 rotated_width, rotated_height, native_handle_, |
164 surface_texture_helper_, rtc::KeepRefUntilDone(this))); | 198 surface_texture_helper_, rtc::KeepRefUntilDone(this))); |
165 | 199 |
| 200 if (cropped_width != width() || cropped_height != height()) { |
| 201 CropMatrix(buffer->native_handle_.sampling_matrix, |
| 202 cropped_width / static_cast<float>(width()), |
| 203 cropped_height / static_cast<float>(height())); |
| 204 } |
166 RotateMatrix(buffer->native_handle_.sampling_matrix, rotation); | 205 RotateMatrix(buffer->native_handle_.sampling_matrix, rotation); |
167 return buffer; | 206 return buffer; |
168 } | 207 } |
169 | 208 |
170 } // namespace webrtc_jni | 209 } // namespace webrtc_jni |
OLD | NEW |