xref: /aosp_15_r20/external/webrtc/sdk/android/src/jni/nv21_buffer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #include <jni.h>
12 #include <vector>
13 
14 #include "third_party/libyuv/include/libyuv/convert.h"
15 #include "third_party/libyuv/include/libyuv/scale.h"
16 
17 #include "common_video/libyuv/include/webrtc_libyuv.h"
18 #include "rtc_base/checks.h"
19 #include "sdk/android/generated_video_jni/NV21Buffer_jni.h"
20 
21 namespace webrtc {
22 namespace jni {
23 
JNI_NV21Buffer_CropAndScale(JNIEnv * jni,jint crop_x,jint crop_y,jint crop_width,jint crop_height,jint scale_width,jint scale_height,const JavaParamRef<jbyteArray> & j_src,jint src_width,jint src_height,const JavaParamRef<jobject> & j_dst_y,jint dst_stride_y,const JavaParamRef<jobject> & j_dst_u,jint dst_stride_u,const JavaParamRef<jobject> & j_dst_v,jint dst_stride_v)24 static void JNI_NV21Buffer_CropAndScale(JNIEnv* jni,
25                                         jint crop_x,
26                                         jint crop_y,
27                                         jint crop_width,
28                                         jint crop_height,
29                                         jint scale_width,
30                                         jint scale_height,
31                                         const JavaParamRef<jbyteArray>& j_src,
32                                         jint src_width,
33                                         jint src_height,
34                                         const JavaParamRef<jobject>& j_dst_y,
35                                         jint dst_stride_y,
36                                         const JavaParamRef<jobject>& j_dst_u,
37                                         jint dst_stride_u,
38                                         const JavaParamRef<jobject>& j_dst_v,
39                                         jint dst_stride_v) {
40   const int src_stride_y = src_width;
41   const int src_stride_uv = src_width;
42   const int crop_chroma_x = crop_x / 2;
43   const int crop_chroma_y = crop_y / 2;
44 
45   jboolean was_copy;
46   jbyte* src_bytes = jni->GetByteArrayElements(j_src.obj(), &was_copy);
47   RTC_DCHECK(!was_copy);
48   uint8_t const* src_y = reinterpret_cast<uint8_t const*>(src_bytes);
49   uint8_t const* src_uv = src_y + src_height * src_stride_y;
50 
51   uint8_t* dst_y =
52       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_y.obj()));
53   uint8_t* dst_u =
54       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_u.obj()));
55   uint8_t* dst_v =
56       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_v.obj()));
57 
58   // Crop using pointer arithmetic.
59   src_y += crop_x + crop_y * src_stride_y;
60   src_uv += 2 * crop_chroma_x + crop_chroma_y * src_stride_uv;
61 
62   NV12ToI420Scaler scaler;
63   // U- and V-planes are swapped because this is NV21 not NV12.
64   scaler.NV12ToI420Scale(src_y, src_stride_y, src_uv, src_stride_uv, crop_width,
65                          crop_height, dst_y, dst_stride_y, dst_v, dst_stride_v,
66                          dst_u, dst_stride_u, scale_width, scale_height);
67 
68   jni->ReleaseByteArrayElements(j_src.obj(), src_bytes, JNI_ABORT);
69 }
70 
71 }  // namespace jni
72 }  // namespace webrtc
73