xref: /aosp_15_r20/external/webrtc/sdk/android/src/jni/java_i420_buffer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2015 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 "sdk/android/generated_video_jni/JavaI420Buffer_jni.h"
12 #include "third_party/libyuv/include/libyuv/scale.h"
13 
14 namespace webrtc {
15 namespace jni {
16 
JNI_JavaI420Buffer_CropAndScaleI420(JNIEnv * jni,const JavaParamRef<jobject> & j_src_y,jint src_stride_y,const JavaParamRef<jobject> & j_src_u,jint src_stride_u,const JavaParamRef<jobject> & j_src_v,jint src_stride_v,jint crop_x,jint crop_y,jint crop_width,jint crop_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,jint scale_width,jint scale_height)17 static void JNI_JavaI420Buffer_CropAndScaleI420(
18     JNIEnv* jni,
19     const JavaParamRef<jobject>& j_src_y,
20     jint src_stride_y,
21     const JavaParamRef<jobject>& j_src_u,
22     jint src_stride_u,
23     const JavaParamRef<jobject>& j_src_v,
24     jint src_stride_v,
25     jint crop_x,
26     jint crop_y,
27     jint crop_width,
28     jint crop_height,
29     const JavaParamRef<jobject>& j_dst_y,
30     jint dst_stride_y,
31     const JavaParamRef<jobject>& j_dst_u,
32     jint dst_stride_u,
33     const JavaParamRef<jobject>& j_dst_v,
34     jint dst_stride_v,
35     jint scale_width,
36     jint scale_height) {
37   uint8_t const* src_y =
38       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_src_y.obj()));
39   uint8_t const* src_u =
40       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_src_u.obj()));
41   uint8_t const* src_v =
42       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_src_v.obj()));
43   uint8_t* dst_y =
44       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_y.obj()));
45   uint8_t* dst_u =
46       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_u.obj()));
47   uint8_t* dst_v =
48       static_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_v.obj()));
49 
50   // Perform cropping using pointer arithmetic.
51   src_y += crop_x + crop_y * src_stride_y;
52   src_u += crop_x / 2 + crop_y / 2 * src_stride_u;
53   src_v += crop_x / 2 + crop_y / 2 * src_stride_v;
54 
55   bool ret = libyuv::I420Scale(
56       src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, crop_width,
57       crop_height, dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
58       dst_stride_v, scale_width, scale_height, libyuv::kFilterBox);
59   RTC_DCHECK_EQ(ret, 0) << "I420Scale failed";
60 }
61 
62 }  // namespace jni
63 }  // namespace webrtc
64