Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 28 | #include "webrtc/api/java/jni/native_handle_impl.h" |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 29 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 30 | #include "webrtc/api/java/jni/jni_helpers.h" |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 31 | #include "webrtc/base/bind.h" |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 32 | #include "webrtc/base/checks.h" |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 33 | #include "webrtc/base/keep_ref_until_done.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 34 | #include "webrtc/base/logging.h" |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 35 | #include "webrtc/base/scoped_ptr.h" |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 36 | #include "webrtc/base/scoped_ref_ptr.h" |
Per | a3c20bb | 2015-11-26 13:41:44 +0100 | [diff] [blame] | 37 | |
Per | a3c20bb | 2015-11-26 13:41:44 +0100 | [diff] [blame] | 38 | using webrtc::NativeHandleBuffer; |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 39 | |
Per | 71f5a9a | 2015-12-11 09:32:37 +0100 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | void RotateMatrix(float a[16], webrtc::VideoRotation rotation) { |
| 43 | // Texture coordinates are in the range 0 to 1. The transformation of the last |
| 44 | // row in each rotation matrix is needed for proper translation, e.g, to |
| 45 | // mirror x, we don't replace x by -x, but by 1-x. |
| 46 | switch (rotation) { |
| 47 | case webrtc::kVideoRotation_0: |
| 48 | break; |
| 49 | case webrtc::kVideoRotation_90: { |
| 50 | const float ROTATE_90[16] = |
| 51 | { a[4], a[5], a[6], a[7], |
| 52 | -a[0], -a[1], -a[2], -a[3], |
| 53 | a[8], a[9], a[10], a[11], |
| 54 | a[0] + a[12], a[1] + a[13], a[2] + a[14], a[3] + a[15]}; |
| 55 | memcpy(a, ROTATE_90, sizeof(ROTATE_90)); |
| 56 | } break; |
| 57 | case webrtc::kVideoRotation_180: { |
| 58 | const float ROTATE_180[16] = |
| 59 | { -a[0], -a[1], -a[2], -a[3], |
| 60 | -a[4], -a[5], -a[6], -a[7], |
| 61 | a[8], a[9], a[10], a[11], |
| 62 | a[0] + a[4] + a[12], a[1] +a[5] + a[13], a[2] + a[6] + a[14], |
| 63 | a[3] + a[11]+ a[15]}; |
| 64 | memcpy(a, ROTATE_180, sizeof(ROTATE_180)); |
| 65 | } |
| 66 | break; |
| 67 | case webrtc::kVideoRotation_270: { |
| 68 | const float ROTATE_270[16] = |
| 69 | { -a[4], -a[5], -a[6], -a[7], |
| 70 | a[0], a[1], a[2], a[3], |
| 71 | a[8], a[9], a[10], a[11], |
| 72 | a[4] + a[12], a[5] + a[13], a[6] + a[14], a[7] + a[15]}; |
| 73 | memcpy(a, ROTATE_270, sizeof(ROTATE_270)); |
| 74 | } break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } // anonymouse namespace |
| 79 | |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 80 | namespace webrtc_jni { |
| 81 | |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 82 | // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
| 83 | static const int kBufferAlignment = 64; |
| 84 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 85 | NativeHandleImpl::NativeHandleImpl(JNIEnv* jni, |
| 86 | jint j_oes_texture_id, |
| 87 | jfloatArray j_transform_matrix) |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 88 | : oes_texture_id(j_oes_texture_id) { |
Magnus Jedvert | 91b348c | 2015-10-07 22:57:06 +0200 | [diff] [blame] | 89 | RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix)); |
| 90 | jfloat* transform_matrix_ptr = |
| 91 | jni->GetFloatArrayElements(j_transform_matrix, nullptr); |
| 92 | for (int i = 0; i < 16; ++i) { |
| 93 | sampling_matrix[i] = transform_matrix_ptr[i]; |
| 94 | } |
| 95 | jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0); |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 96 | } |
| 97 | |
magjed | 52a30e3 | 2015-10-12 06:53:20 -0700 | [diff] [blame] | 98 | AndroidTextureBuffer::AndroidTextureBuffer( |
| 99 | int width, |
| 100 | int height, |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 101 | const NativeHandleImpl& native_handle, |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 102 | jobject surface_texture_helper, |
magjed | 52a30e3 | 2015-10-12 06:53:20 -0700 | [diff] [blame] | 103 | const rtc::Callback0<void>& no_longer_used) |
| 104 | : webrtc::NativeHandleBuffer(&native_handle_, width, height), |
| 105 | native_handle_(native_handle), |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 106 | surface_texture_helper_(surface_texture_helper), |
magjed | 52a30e3 | 2015-10-12 06:53:20 -0700 | [diff] [blame] | 107 | no_longer_used_cb_(no_longer_used) {} |
| 108 | |
| 109 | AndroidTextureBuffer::~AndroidTextureBuffer() { |
| 110 | no_longer_used_cb_(); |
| 111 | } |
| 112 | |
| 113 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> |
| 114 | AndroidTextureBuffer::NativeToI420Buffer() { |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 115 | int uv_width = (width()+7) / 8; |
| 116 | int stride = 8 * uv_width; |
| 117 | int uv_height = (height()+1)/2; |
| 118 | size_t size = stride * (height() + uv_height); |
| 119 | // The data is owned by the frame, and the normal case is that the |
| 120 | // data is deleted by the frame's destructor callback. |
| 121 | // |
| 122 | // TODO(nisse): Use an I420BufferPool. We then need to extend that |
| 123 | // class, and I420Buffer, to support our memory layout. |
| 124 | rtc::scoped_ptr<uint8_t, webrtc::AlignedFreeDeleter> yuv_data( |
| 125 | static_cast<uint8_t*>(webrtc::AlignedMalloc(size, kBufferAlignment))); |
| 126 | // See SurfaceTextureHelper.java for the required layout. |
| 127 | uint8_t* y_data = yuv_data.get(); |
| 128 | uint8_t* u_data = y_data + height() * stride; |
| 129 | uint8_t* v_data = u_data + stride/2; |
| 130 | |
| 131 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> copy = |
| 132 | new rtc::RefCountedObject<webrtc::WrappedI420Buffer>( |
| 133 | width(), height(), |
| 134 | y_data, stride, |
| 135 | u_data, stride, |
| 136 | v_data, stride, |
| 137 | rtc::Bind(&webrtc::AlignedFree, yuv_data.release())); |
| 138 | |
| 139 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 140 | ScopedLocalRefFrame local_ref_frame(jni); |
| 141 | |
| 142 | jmethodID transform_mid = GetMethodID( |
| 143 | jni, |
| 144 | GetObjectClass(jni, surface_texture_helper_), |
| 145 | "textureToYUV", |
| 146 | "(Ljava/nio/ByteBuffer;IIII[F)V"); |
| 147 | |
| 148 | jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size); |
| 149 | |
| 150 | // TODO(nisse): Keep java transform matrix around. |
| 151 | jfloatArray sampling_matrix = jni->NewFloatArray(16); |
| 152 | jni->SetFloatArrayRegion(sampling_matrix, 0, 16, |
| 153 | native_handle_.sampling_matrix); |
| 154 | |
| 155 | jni->CallVoidMethod(surface_texture_helper_, |
| 156 | transform_mid, |
| 157 | byte_buffer, width(), height(), stride, |
| 158 | native_handle_.oes_texture_id, sampling_matrix); |
| 159 | CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception"; |
| 160 | |
| 161 | return copy; |
magjed | 52a30e3 | 2015-10-12 06:53:20 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Per | 71f5a9a | 2015-12-11 09:32:37 +0100 | [diff] [blame] | 164 | rtc::scoped_refptr<AndroidTextureBuffer> |
| 165 | AndroidTextureBuffer::ScaleAndRotate(int dst_widht, |
| 166 | int dst_height, |
| 167 | webrtc::VideoRotation rotation) { |
| 168 | if (width() == dst_widht && height() == dst_height && |
| 169 | rotation == webrtc::kVideoRotation_0) { |
| 170 | return this; |
| 171 | } |
| 172 | int rotated_width = (rotation % 180 == 0) ? dst_widht : dst_height; |
| 173 | int rotated_height = (rotation % 180 == 0) ? dst_height : dst_widht; |
Per | a3c20bb | 2015-11-26 13:41:44 +0100 | [diff] [blame] | 174 | |
| 175 | // 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] | 176 | // created AndroidTextureBuffer is destructed |
| 177 | rtc::scoped_refptr<AndroidTextureBuffer> buffer( |
| 178 | new rtc::RefCountedObject<AndroidTextureBuffer>( |
| 179 | rotated_width, rotated_height, native_handle_, |
| 180 | surface_texture_helper_, rtc::KeepRefUntilDone(this))); |
| 181 | |
| 182 | RotateMatrix(buffer->native_handle_.sampling_matrix, rotation); |
| 183 | return buffer; |
Per | a3c20bb | 2015-11-26 13:41:44 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Magnus Jedvert | 6781ea4 | 2015-10-02 13:56:04 +0200 | [diff] [blame] | 186 | } // namespace webrtc_jni |