Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/java/jni/native_handle_impl.h" |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 12 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 13 | #include "webrtc/api/java/jni/jni_helpers.h" |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 14 | #include "webrtc/base/bind.h" |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 16 | #include "webrtc/base/keep_ref_until_done.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 17 | #include "webrtc/base/logging.h" |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 18 | #include "webrtc/base/scoped_ptr.h" |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 19 | #include "webrtc/base/scoped_ref_ptr.h" |
Per | a3c20bb | 2015-11-26 13:41:44 +0100 | [diff] [blame] | 20 | |
Per | a3c20bb | 2015-11-26 13:41:44 +0100 | [diff] [blame] | 21 | using webrtc::NativeHandleBuffer; |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 22 | |
Per | 71f5a9a | 2015-12-11 09:32:37 +0100 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | void RotateMatrix(float a[16], webrtc::VideoRotation rotation) { |
| 26 | // Texture coordinates are in the range 0 to 1. The transformation of the last |
| 27 | // row in each rotation matrix is needed for proper translation, e.g, to |
| 28 | // mirror x, we don't replace x by -x, but by 1-x. |
| 29 | switch (rotation) { |
| 30 | case webrtc::kVideoRotation_0: |
| 31 | break; |
| 32 | case webrtc::kVideoRotation_90: { |
| 33 | const float ROTATE_90[16] = |
| 34 | { a[4], a[5], a[6], a[7], |
| 35 | -a[0], -a[1], -a[2], -a[3], |
| 36 | a[8], a[9], a[10], a[11], |
| 37 | a[0] + a[12], a[1] + a[13], a[2] + a[14], a[3] + a[15]}; |
| 38 | memcpy(a, ROTATE_90, sizeof(ROTATE_90)); |
| 39 | } break; |
| 40 | case webrtc::kVideoRotation_180: { |
| 41 | const float ROTATE_180[16] = |
| 42 | { -a[0], -a[1], -a[2], -a[3], |
| 43 | -a[4], -a[5], -a[6], -a[7], |
| 44 | a[8], a[9], a[10], a[11], |
| 45 | a[0] + a[4] + a[12], a[1] +a[5] + a[13], a[2] + a[6] + a[14], |
| 46 | a[3] + a[11]+ a[15]}; |
| 47 | memcpy(a, ROTATE_180, sizeof(ROTATE_180)); |
| 48 | } |
| 49 | break; |
| 50 | case webrtc::kVideoRotation_270: { |
| 51 | const float ROTATE_270[16] = |
| 52 | { -a[4], -a[5], -a[6], -a[7], |
| 53 | a[0], a[1], a[2], a[3], |
| 54 | a[8], a[9], a[10], a[11], |
| 55 | a[4] + a[12], a[5] + a[13], a[6] + a[14], a[7] + a[15]}; |
| 56 | memcpy(a, ROTATE_270, sizeof(ROTATE_270)); |
| 57 | } break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | } // anonymouse namespace |
| 62 | |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 63 | namespace webrtc_jni { |
| 64 | |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 65 | // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
| 66 | static const int kBufferAlignment = 64; |
| 67 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 68 | NativeHandleImpl::NativeHandleImpl(JNIEnv* jni, |
| 69 | jint j_oes_texture_id, |
| 70 | jfloatArray j_transform_matrix) |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 71 | : oes_texture_id(j_oes_texture_id) { |
Magnus Jedvert | 91b348c | 2015-10-07 22:57:06 +0200 | [diff] [blame] | 72 | RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix)); |
| 73 | jfloat* transform_matrix_ptr = |
| 74 | jni->GetFloatArrayElements(j_transform_matrix, nullptr); |
| 75 | for (int i = 0; i < 16; ++i) { |
| 76 | sampling_matrix[i] = transform_matrix_ptr[i]; |
| 77 | } |
| 78 | jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0); |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 79 | } |
| 80 | |
magjed | 52a30e3 | 2015-10-12 06:53:20 -0700 | [diff] [blame] | 81 | AndroidTextureBuffer::AndroidTextureBuffer( |
| 82 | int width, |
| 83 | int height, |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 84 | const NativeHandleImpl& native_handle, |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 85 | jobject surface_texture_helper, |
magjed | 52a30e3 | 2015-10-12 06:53:20 -0700 | [diff] [blame] | 86 | const rtc::Callback0<void>& no_longer_used) |
| 87 | : webrtc::NativeHandleBuffer(&native_handle_, width, height), |
| 88 | native_handle_(native_handle), |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 89 | surface_texture_helper_(surface_texture_helper), |
magjed | 52a30e3 | 2015-10-12 06:53:20 -0700 | [diff] [blame] | 90 | no_longer_used_cb_(no_longer_used) {} |
| 91 | |
| 92 | AndroidTextureBuffer::~AndroidTextureBuffer() { |
| 93 | no_longer_used_cb_(); |
| 94 | } |
| 95 | |
| 96 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> |
| 97 | AndroidTextureBuffer::NativeToI420Buffer() { |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 98 | int uv_width = (width()+7) / 8; |
| 99 | int stride = 8 * uv_width; |
| 100 | int uv_height = (height()+1)/2; |
| 101 | size_t size = stride * (height() + uv_height); |
| 102 | // The data is owned by the frame, and the normal case is that the |
| 103 | // data is deleted by the frame's destructor callback. |
| 104 | // |
| 105 | // TODO(nisse): Use an I420BufferPool. We then need to extend that |
| 106 | // class, and I420Buffer, to support our memory layout. |
| 107 | rtc::scoped_ptr<uint8_t, webrtc::AlignedFreeDeleter> yuv_data( |
| 108 | static_cast<uint8_t*>(webrtc::AlignedMalloc(size, kBufferAlignment))); |
| 109 | // See SurfaceTextureHelper.java for the required layout. |
| 110 | uint8_t* y_data = yuv_data.get(); |
| 111 | uint8_t* u_data = y_data + height() * stride; |
| 112 | uint8_t* v_data = u_data + stride/2; |
| 113 | |
| 114 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> copy = |
| 115 | new rtc::RefCountedObject<webrtc::WrappedI420Buffer>( |
| 116 | width(), height(), |
| 117 | y_data, stride, |
| 118 | u_data, stride, |
| 119 | v_data, stride, |
| 120 | rtc::Bind(&webrtc::AlignedFree, yuv_data.release())); |
| 121 | |
| 122 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 123 | ScopedLocalRefFrame local_ref_frame(jni); |
| 124 | |
| 125 | jmethodID transform_mid = GetMethodID( |
| 126 | jni, |
| 127 | GetObjectClass(jni, surface_texture_helper_), |
| 128 | "textureToYUV", |
| 129 | "(Ljava/nio/ByteBuffer;IIII[F)V"); |
| 130 | |
| 131 | jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size); |
| 132 | |
| 133 | // TODO(nisse): Keep java transform matrix around. |
| 134 | jfloatArray sampling_matrix = jni->NewFloatArray(16); |
| 135 | jni->SetFloatArrayRegion(sampling_matrix, 0, 16, |
| 136 | native_handle_.sampling_matrix); |
| 137 | |
| 138 | jni->CallVoidMethod(surface_texture_helper_, |
| 139 | transform_mid, |
| 140 | byte_buffer, width(), height(), stride, |
| 141 | native_handle_.oes_texture_id, sampling_matrix); |
| 142 | CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception"; |
| 143 | |
| 144 | return copy; |
magjed | 52a30e3 | 2015-10-12 06:53:20 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Per | 71f5a9a | 2015-12-11 09:32:37 +0100 | [diff] [blame] | 147 | rtc::scoped_refptr<AndroidTextureBuffer> |
| 148 | AndroidTextureBuffer::ScaleAndRotate(int dst_widht, |
| 149 | int dst_height, |
| 150 | webrtc::VideoRotation rotation) { |
| 151 | if (width() == dst_widht && height() == dst_height && |
| 152 | rotation == webrtc::kVideoRotation_0) { |
| 153 | return this; |
| 154 | } |
| 155 | int rotated_width = (rotation % 180 == 0) ? dst_widht : dst_height; |
| 156 | int rotated_height = (rotation % 180 == 0) ? dst_height : dst_widht; |
Per | a3c20bb | 2015-11-26 13:41:44 +0100 | [diff] [blame] | 157 | |
| 158 | // Here we use Bind magic to add a reference count to |this| until the newly |
Per | 71f5a9a | 2015-12-11 09:32:37 +0100 | [diff] [blame] | 159 | // created AndroidTextureBuffer is destructed |
| 160 | rtc::scoped_refptr<AndroidTextureBuffer> buffer( |
| 161 | new rtc::RefCountedObject<AndroidTextureBuffer>( |
| 162 | rotated_width, rotated_height, native_handle_, |
| 163 | surface_texture_helper_, rtc::KeepRefUntilDone(this))); |
| 164 | |
| 165 | RotateMatrix(buffer->native_handle_.sampling_matrix, rotation); |
| 166 | return buffer; |
Per | a3c20bb | 2015-11-26 13:41:44 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 169 | } // namespace webrtc_jni |