blob: 1f180ade9cc79ff33bcd6f1174d6817e44187b4a [file] [log] [blame]
Magnus Jedvert6781ea42015-10-02 13:56:04 +02001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
Magnus Jedvert6781ea42015-10-02 13:56:04 +02003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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 Jedvert6781ea42015-10-02 13:56:04 +02009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/java/jni/native_handle_impl.h"
Magnus Jedvert6781ea42015-10-02 13:56:04 +020012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
14
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/java/jni/jni_helpers.h"
nissec490e012015-12-10 06:23:33 -080016#include "webrtc/base/bind.h"
Magnus Jedvert6781ea42015-10-02 13:56:04 +020017#include "webrtc/base/checks.h"
perkj14f41442015-11-30 22:15:45 -080018#include "webrtc/base/keep_ref_until_done.h"
kjellandera96e2d72016-02-04 23:52:28 -080019#include "webrtc/base/logging.h"
perkj14f41442015-11-30 22:15:45 -080020#include "webrtc/base/scoped_ref_ptr.h"
Pera3c20bb2015-11-26 13:41:44 +010021
Pera3c20bb2015-11-26 13:41:44 +010022using webrtc::NativeHandleBuffer;
Magnus Jedvert6781ea42015-10-02 13:56:04 +020023
Per71f5a9a2015-12-11 09:32:37 +010024namespace {
25
26void RotateMatrix(float a[16], webrtc::VideoRotation rotation) {
27 // Texture coordinates are in the range 0 to 1. The transformation of the last
28 // row in each rotation matrix is needed for proper translation, e.g, to
29 // mirror x, we don't replace x by -x, but by 1-x.
30 switch (rotation) {
31 case webrtc::kVideoRotation_0:
32 break;
33 case webrtc::kVideoRotation_90: {
34 const float ROTATE_90[16] =
35 { a[4], a[5], a[6], a[7],
36 -a[0], -a[1], -a[2], -a[3],
37 a[8], a[9], a[10], a[11],
38 a[0] + a[12], a[1] + a[13], a[2] + a[14], a[3] + a[15]};
39 memcpy(a, ROTATE_90, sizeof(ROTATE_90));
40 } break;
41 case webrtc::kVideoRotation_180: {
42 const float ROTATE_180[16] =
43 { -a[0], -a[1], -a[2], -a[3],
44 -a[4], -a[5], -a[6], -a[7],
45 a[8], a[9], a[10], a[11],
46 a[0] + a[4] + a[12], a[1] +a[5] + a[13], a[2] + a[6] + a[14],
47 a[3] + a[11]+ a[15]};
48 memcpy(a, ROTATE_180, sizeof(ROTATE_180));
49 }
50 break;
51 case webrtc::kVideoRotation_270: {
52 const float ROTATE_270[16] =
53 { -a[4], -a[5], -a[6], -a[7],
54 a[0], a[1], a[2], a[3],
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]};
57 memcpy(a, ROTATE_270, sizeof(ROTATE_270));
58 } break;
59 }
60}
61
Magnus Jedverta3002db2016-05-13 12:51:04 +020062// Calculates result = a * b, in column-major order.
63void 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.
78void 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
Per71f5a9a2015-12-11 09:32:37 +010093} // anonymouse namespace
94
Magnus Jedvert6781ea42015-10-02 13:56:04 +020095namespace webrtc_jni {
96
nissec490e012015-12-10 06:23:33 -080097// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
98static const int kBufferAlignment = 64;
99
Per488e75f2015-11-19 10:43:36 +0100100NativeHandleImpl::NativeHandleImpl(JNIEnv* jni,
101 jint j_oes_texture_id,
102 jfloatArray j_transform_matrix)
nissec490e012015-12-10 06:23:33 -0800103 : oes_texture_id(j_oes_texture_id) {
Magnus Jedvert91b348c2015-10-07 22:57:06 +0200104 RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix));
105 jfloat* transform_matrix_ptr =
106 jni->GetFloatArrayElements(j_transform_matrix, nullptr);
107 for (int i = 0; i < 16; ++i) {
108 sampling_matrix[i] = transform_matrix_ptr[i];
109 }
110 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0);
Magnus Jedvert6781ea42015-10-02 13:56:04 +0200111}
112
magjed52a30e32015-10-12 06:53:20 -0700113AndroidTextureBuffer::AndroidTextureBuffer(
114 int width,
115 int height,
Per488e75f2015-11-19 10:43:36 +0100116 const NativeHandleImpl& native_handle,
nissec490e012015-12-10 06:23:33 -0800117 jobject surface_texture_helper,
magjed52a30e32015-10-12 06:53:20 -0700118 const rtc::Callback0<void>& no_longer_used)
119 : webrtc::NativeHandleBuffer(&native_handle_, width, height),
120 native_handle_(native_handle),
nissec490e012015-12-10 06:23:33 -0800121 surface_texture_helper_(surface_texture_helper),
magjed52a30e32015-10-12 06:53:20 -0700122 no_longer_used_cb_(no_longer_used) {}
123
124AndroidTextureBuffer::~AndroidTextureBuffer() {
125 no_longer_used_cb_();
126}
127
128rtc::scoped_refptr<webrtc::VideoFrameBuffer>
129AndroidTextureBuffer::NativeToI420Buffer() {
nissec490e012015-12-10 06:23:33 -0800130 int uv_width = (width()+7) / 8;
131 int stride = 8 * uv_width;
132 int uv_height = (height()+1)/2;
133 size_t size = stride * (height() + uv_height);
134 // The data is owned by the frame, and the normal case is that the
135 // data is deleted by the frame's destructor callback.
136 //
137 // TODO(nisse): Use an I420BufferPool. We then need to extend that
138 // class, and I420Buffer, to support our memory layout.
kwibergd1fe2812016-04-27 06:47:29 -0700139 std::unique_ptr<uint8_t, webrtc::AlignedFreeDeleter> yuv_data(
nissec490e012015-12-10 06:23:33 -0800140 static_cast<uint8_t*>(webrtc::AlignedMalloc(size, kBufferAlignment)));
141 // See SurfaceTextureHelper.java for the required layout.
142 uint8_t* y_data = yuv_data.get();
143 uint8_t* u_data = y_data + height() * stride;
144 uint8_t* v_data = u_data + stride/2;
145
146 rtc::scoped_refptr<webrtc::VideoFrameBuffer> copy =
147 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
148 width(), height(),
149 y_data, stride,
150 u_data, stride,
151 v_data, stride,
152 rtc::Bind(&webrtc::AlignedFree, yuv_data.release()));
153
154 JNIEnv* jni = AttachCurrentThreadIfNeeded();
155 ScopedLocalRefFrame local_ref_frame(jni);
156
157 jmethodID transform_mid = GetMethodID(
158 jni,
159 GetObjectClass(jni, surface_texture_helper_),
160 "textureToYUV",
161 "(Ljava/nio/ByteBuffer;IIII[F)V");
162
163 jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size);
164
165 // TODO(nisse): Keep java transform matrix around.
166 jfloatArray sampling_matrix = jni->NewFloatArray(16);
167 jni->SetFloatArrayRegion(sampling_matrix, 0, 16,
168 native_handle_.sampling_matrix);
169
170 jni->CallVoidMethod(surface_texture_helper_,
171 transform_mid,
172 byte_buffer, width(), height(), stride,
173 native_handle_.oes_texture_id, sampling_matrix);
174 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception";
175
176 return copy;
magjed52a30e32015-10-12 06:53:20 -0700177}
178
Per71f5a9a2015-12-11 09:32:37 +0100179rtc::scoped_refptr<AndroidTextureBuffer>
Magnus Jedverta3002db2016-05-13 12:51:04 +0200180AndroidTextureBuffer::CropScaleAndRotate(int cropped_width,
181 int cropped_height,
182 int dst_width,
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 &&
Per71f5a9a2015-12-11 09:32:37 +0100187 rotation == webrtc::kVideoRotation_0) {
188 return this;
189 }
Magnus Jedverta3002db2016-05-13 12:51:04 +0200190 int rotated_width = (rotation % 180 == 0) ? dst_width : dst_height;
191 int rotated_height = (rotation % 180 == 0) ? dst_height : dst_width;
Pera3c20bb2015-11-26 13:41:44 +0100192
193 // Here we use Bind magic to add a reference count to |this| until the newly
Per71f5a9a2015-12-11 09:32:37 +0100194 // created AndroidTextureBuffer is destructed
195 rtc::scoped_refptr<AndroidTextureBuffer> buffer(
196 new rtc::RefCountedObject<AndroidTextureBuffer>(
197 rotated_width, rotated_height, native_handle_,
198 surface_texture_helper_, rtc::KeepRefUntilDone(this)));
199
Magnus Jedverta3002db2016-05-13 12:51:04 +0200200 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 }
Per71f5a9a2015-12-11 09:32:37 +0100205 RotateMatrix(buffer->native_handle_.sampling_matrix, rotation);
206 return buffer;
Pera3c20bb2015-11-26 13:41:44 +0100207}
208
Magnus Jedvert6781ea42015-10-02 13:56:04 +0200209} // namespace webrtc_jni