2 * Android MediaCodec Wrapper
4 * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/avassert.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/avstring.h"
32 #include "mediacodec_wrapper.h"
34 struct JNIAMediaCodecListFields {
36 jclass mediacodec_list_class;
38 jmethodID find_decoder_for_format_id;
40 jmethodID get_codec_count_id;
41 jmethodID get_codec_info_at_id;
43 jclass mediacodec_info_class;
44 jmethodID get_name_id;
45 jmethodID get_codec_capabilities_id;
46 jmethodID get_supported_types_id;
47 jmethodID is_encoder_id;
49 jclass codec_capabilities_class;
50 jfieldID color_formats_id;
51 jfieldID profile_levels_id;
53 jclass codec_profile_level_class;
57 jfieldID avc_profile_baseline_id;
58 jfieldID avc_profile_main_id;
59 jfieldID avc_profile_extended_id;
60 jfieldID avc_profile_high_id;
61 jfieldID avc_profile_high10_id;
62 jfieldID avc_profile_high422_id;
63 jfieldID avc_profile_high444_id;
65 } JNIAMediaCodecListFields;
67 static const struct FFJniField jni_amediacodeclist_mapping[] = {
68 { "android/media/MediaCodecList", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediacodec_list_class), 1 },
69 { "android/media/MediaCodecList", "<init>", "(I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, init_id), 0 },
70 { "android/media/MediaCodecList", "findDecoderForFormat", "(Landroid/media/MediaFormat;)Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, find_decoder_for_format_id), 0 },
72 { "android/media/MediaCodecList", "getCodecCount", "()I", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_count_id), 1 },
73 { "android/media/MediaCodecList", "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_info_at_id), 1 },
75 { "android/media/MediaCodecInfo", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediacodec_info_class), 1 },
76 { "android/media/MediaCodecInfo", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_name_id), 1 },
77 { "android/media/MediaCodecInfo", "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_capabilities_id), 1 },
78 { "android/media/MediaCodecInfo", "getSupportedTypes", "()[Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_supported_types_id), 1 },
79 { "android/media/MediaCodecInfo", "isEncoder", "()Z", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, is_encoder_id), 1 },
81 { "android/media/MediaCodecInfo$CodecCapabilities", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, codec_capabilities_class), 1 },
82 { "android/media/MediaCodecInfo$CodecCapabilities", "colorFormats", "[I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, color_formats_id), 1 },
83 { "android/media/MediaCodecInfo$CodecCapabilities", "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, profile_levels_id), 1 },
85 { "android/media/MediaCodecInfo$CodecProfileLevel", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, codec_profile_level_class), 1 },
86 { "android/media/MediaCodecInfo$CodecProfileLevel", "profile", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, profile_id), 1 },
87 { "android/media/MediaCodecInfo$CodecProfileLevel", "level", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, level_id), 1 },
89 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileBaseline", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_baseline_id), 1 },
90 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileMain", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_main_id), 1 },
91 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileExtended", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_extended_id), 1 },
92 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high_id), 1 },
93 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh10", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high10_id), 1 },
94 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh422", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high422_id), 1 },
95 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh444", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high444_id), 1 },
100 struct JNIAMediaFormatFields {
102 jclass mediaformat_class;
106 jmethodID get_integer_id;
107 jmethodID get_long_id;
108 jmethodID get_float_id;
109 jmethodID get_bytebuffer_id;
110 jmethodID get_string_id;
112 jmethodID set_integer_id;
113 jmethodID set_long_id;
114 jmethodID set_float_id;
115 jmethodID set_bytebuffer_id;
116 jmethodID set_string_id;
118 jmethodID to_string_id;
120 } JNIAMediaFormatFields;
122 static const struct FFJniField jni_amediaformat_mapping[] = {
123 { "android/media/MediaFormat", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaFormatFields, mediaformat_class), 1 },
125 { "android/media/MediaFormat", "<init>", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, init_id), 1 },
127 { "android/media/MediaFormat", "getInteger", "(Ljava/lang/String;)I", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_integer_id), 1 },
128 { "android/media/MediaFormat", "getLong", "(Ljava/lang/String;)J", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_long_id), 1 },
129 { "android/media/MediaFormat", "getFloat", "(Ljava/lang/String;)F", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_float_id), 1 },
130 { "android/media/MediaFormat", "getByteBuffer", "(Ljava/lang/String;)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_bytebuffer_id), 1 },
131 { "android/media/MediaFormat", "getString", "(Ljava/lang/String;)Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_string_id), 1 },
133 { "android/media/MediaFormat", "setInteger", "(Ljava/lang/String;I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_integer_id), 1 },
134 { "android/media/MediaFormat", "setLong", "(Ljava/lang/String;J)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_long_id), 1 },
135 { "android/media/MediaFormat", "setFloat", "(Ljava/lang/String;F)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_float_id), 1 },
136 { "android/media/MediaFormat", "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_bytebuffer_id), 1 },
137 { "android/media/MediaFormat", "setString", "(Ljava/lang/String;Ljava/lang/String;)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_string_id), 1 },
139 { "android/media/MediaFormat", "toString", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, to_string_id), 1 },
144 static const AVClass amediaformat_class = {
145 .class_name = "amediaformat",
146 .item_name = av_default_item_name,
147 .version = LIBAVCODEC_VERSION_INT,
150 struct FFAMediaFormat {
152 const AVClass *class;
153 struct JNIAMediaFormatFields jfields;
157 struct JNIAMediaCodecFields {
159 jclass mediacodec_class;
161 jfieldID info_try_again_later_id;
162 jfieldID info_output_buffers_changed_id;
163 jfieldID info_output_format_changed_id;
165 jfieldID buffer_flag_codec_config_id;
166 jfieldID buffer_flag_end_of_stream_id;
167 jfieldID buffer_flag_key_frame_id;
169 jfieldID configure_flag_encode_id;
171 jmethodID create_by_codec_name_id;
172 jmethodID create_decoder_by_type_id;
173 jmethodID create_encoder_by_type_id;
175 jmethodID get_name_id;
177 jmethodID configure_id;
181 jmethodID release_id;
183 jmethodID get_output_format_id;
185 jmethodID dequeue_input_buffer_id;
186 jmethodID queue_input_buffer_id;
187 jmethodID get_input_buffer_id;
188 jmethodID get_input_buffers_id;
190 jmethodID dequeue_output_buffer_id;
191 jmethodID get_output_buffer_id;
192 jmethodID get_output_buffers_id;
193 jmethodID release_output_buffer_id;
194 jmethodID release_output_buffer_at_time_id;
196 jclass mediainfo_class;
202 jfieldID presentation_time_us_id;
205 } JNIAMediaCodecFields;
207 static const struct FFJniField jni_amediacodec_mapping[] = {
208 { "android/media/MediaCodec", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecFields, mediacodec_class), 1 },
210 { "android/media/MediaCodec", "INFO_TRY_AGAIN_LATER", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_try_again_later_id), 1 },
211 { "android/media/MediaCodec", "INFO_OUTPUT_BUFFERS_CHANGED", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_output_buffers_changed_id), 1 },
212 { "android/media/MediaCodec", "INFO_OUTPUT_FORMAT_CHANGED", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_output_format_changed_id), 1 },
214 { "android/media/MediaCodec", "BUFFER_FLAG_CODEC_CONFIG", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, buffer_flag_codec_config_id), 1 },
215 { "android/media/MediaCodec", "BUFFER_FLAG_END_OF_STREAM", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, buffer_flag_end_of_stream_id), 1 },
216 { "android/media/MediaCodec", "BUFFER_FLAG_KEY_FRAME", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, buffer_flag_key_frame_id), 0 },
218 { "android/media/MediaCodec", "CONFIGURE_FLAG_ENCODE", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, configure_flag_encode_id), 1 },
220 { "android/media/MediaCodec", "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_by_codec_name_id), 1 },
221 { "android/media/MediaCodec", "createDecoderByType", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_decoder_by_type_id), 1 },
222 { "android/media/MediaCodec", "createEncoderByType", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_encoder_by_type_id), 1 },
224 { "android/media/MediaCodec", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_name_id), 1 },
226 { "android/media/MediaCodec", "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, configure_id), 1 },
227 { "android/media/MediaCodec", "start", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, start_id), 1 },
228 { "android/media/MediaCodec", "flush", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, flush_id), 1 },
229 { "android/media/MediaCodec", "stop", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, stop_id), 1 },
230 { "android/media/MediaCodec", "release", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_id), 1 },
232 { "android/media/MediaCodec", "getOutputFormat", "()Landroid/media/MediaFormat;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_format_id), 1 },
234 { "android/media/MediaCodec", "dequeueInputBuffer", "(J)I", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, dequeue_input_buffer_id), 1 },
235 { "android/media/MediaCodec", "queueInputBuffer", "(IIIJI)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, queue_input_buffer_id), 1 },
236 { "android/media/MediaCodec", "getInputBuffer", "(I)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_input_buffer_id), 0 },
237 { "android/media/MediaCodec", "getInputBuffers", "()[Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_input_buffers_id), 1 },
239 { "android/media/MediaCodec", "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, dequeue_output_buffer_id), 1 },
240 { "android/media/MediaCodec", "getOutputBuffer", "(I)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_buffer_id), 0 },
241 { "android/media/MediaCodec", "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_buffers_id), 1 },
242 { "android/media/MediaCodec", "releaseOutputBuffer", "(IZ)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_output_buffer_id), 1 },
243 { "android/media/MediaCodec", "releaseOutputBuffer", "(IJ)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_output_buffer_at_time_id), 0 },
245 { "android/media/MediaCodec$BufferInfo", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecFields, mediainfo_class), 1 },
247 { "android/media/MediaCodec.BufferInfo", "<init>", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, init_id), 1 },
248 { "android/media/MediaCodec.BufferInfo", "flags", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, flags_id), 1 },
249 { "android/media/MediaCodec.BufferInfo", "offset", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, offset_id), 1 },
250 { "android/media/MediaCodec.BufferInfo", "presentationTimeUs", "J", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, presentation_time_us_id), 1 },
251 { "android/media/MediaCodec.BufferInfo", "size", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, size_id), 1 },
256 static const AVClass amediacodec_class = {
257 .class_name = "amediacodec",
258 .item_name = av_default_item_name,
259 .version = LIBAVCODEC_VERSION_INT,
262 struct FFAMediaCodec {
264 const AVClass *class;
266 struct JNIAMediaCodecFields jfields;
270 jobject input_buffers;
271 jobject output_buffers;
273 int INFO_TRY_AGAIN_LATER;
274 int INFO_OUTPUT_BUFFERS_CHANGED;
275 int INFO_OUTPUT_FORMAT_CHANGED;
277 int BUFFER_FLAG_CODEC_CONFIG;
278 int BUFFER_FLAG_END_OF_STREAM;
279 int BUFFER_FLAG_KEY_FRAME;
281 int CONFIGURE_FLAG_ENCODE;
283 int has_get_i_o_buffer;
286 #define JNI_ATTACH_ENV_OR_RETURN(env, attached, log_ctx, ret) do { \
287 (env) = ff_jni_attach_env(attached, log_ctx); \
293 #define JNI_ATTACH_ENV_OR_RETURN_VOID(env, attached, log_ctx) do { \
294 (env) = ff_jni_attach_env(attached, log_ctx); \
300 #define JNI_DETACH_ENV(attached, log_ctx) do { \
302 ff_jni_detach_env(log_ctx); \
307 int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
313 struct JNIAMediaCodecListFields jfields = { 0 };
315 JNI_ATTACH_ENV_OR_RETURN(env, &attached, avctx, -1);
317 if (ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, avctx) < 0) {
321 if (avctx->codec_id == AV_CODEC_ID_H264) {
322 jfieldID field_id = 0;
324 switch(avctx->profile) {
325 case FF_PROFILE_H264_BASELINE:
326 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
327 field_id = jfields.avc_profile_baseline_id;
329 case FF_PROFILE_H264_MAIN:
330 field_id = jfields.avc_profile_main_id;
332 case FF_PROFILE_H264_EXTENDED:
333 field_id = jfields.avc_profile_extended_id;
335 case FF_PROFILE_H264_HIGH:
336 field_id = jfields.avc_profile_high_id;
338 case FF_PROFILE_H264_HIGH_10:
339 case FF_PROFILE_H264_HIGH_10_INTRA:
340 field_id = jfields.avc_profile_high10_id;
342 case FF_PROFILE_H264_HIGH_422:
343 case FF_PROFILE_H264_HIGH_422_INTRA:
344 field_id = jfields.avc_profile_high422_id;
346 case FF_PROFILE_H264_HIGH_444:
347 case FF_PROFILE_H264_HIGH_444_INTRA:
348 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
349 field_id = jfields.avc_profile_high444_id;
354 ret = (*env)->GetStaticIntField(env, jfields.codec_profile_level_class, field_id);
355 if (ff_jni_exception_check(env, 1, avctx) < 0) {
363 ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, avctx);
365 JNI_DETACH_ENV(attached, avctx);
370 char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx)
377 char *supported_type = NULL;
381 struct JNIAMediaCodecListFields jfields = { 0 };
382 struct JNIAMediaFormatFields mediaformat_jfields = { 0 };
384 jobject format = NULL;
385 jobject codec = NULL;
386 jobject codec_name = NULL;
390 jobjectArray types = NULL;
392 jobject capabilities = NULL;
393 jobject profile_level = NULL;
394 jobjectArray profile_levels = NULL;
396 JNI_ATTACH_ENV_OR_RETURN(env, &attached, log_ctx, NULL);
398 if ((ret = ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx)) < 0) {
402 if ((ret = ff_jni_init_jfields(env, &mediaformat_jfields, jni_amediaformat_mapping, 0, log_ctx)) < 0) {
406 codec_count = (*env)->CallStaticIntMethod(env, jfields.mediacodec_list_class, jfields.get_codec_count_id);
407 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
411 for(i = 0; i < codec_count; i++) {
416 info = (*env)->CallStaticObjectMethod(env, jfields.mediacodec_list_class, jfields.get_codec_info_at_id, i);
417 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
421 types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types_id);
422 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
426 is_encoder = (*env)->CallBooleanMethod(env, info, jfields.is_encoder_id);
427 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
431 if (is_encoder != encoder) {
435 type_count = (*env)->GetArrayLength(env, types);
436 for (j = 0; j < type_count; j++) {
440 type = (*env)->GetObjectArrayElement(env, types, j);
441 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
445 supported_type = ff_jni_jstring_to_utf_chars(env, type, log_ctx);
446 if (!supported_type) {
450 if (!av_strcasecmp(supported_type, mime)) {
451 codec_name = (*env)->CallObjectMethod(env, info, jfields.get_name_id);
452 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
456 name = ff_jni_jstring_to_utf_chars(env, codec_name, log_ctx);
461 if (strstr(name, "OMX.google")) {
466 capabilities = (*env)->CallObjectMethod(env, info, jfields.get_codec_capabilities_id, type);
467 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
471 profile_levels = (*env)->GetObjectField(env, capabilities, jfields.profile_levels_id);
472 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
476 profile_count = (*env)->GetArrayLength(env, profile_levels);
477 for (k = 0; k < profile_count; k++) {
478 int supported_profile = 0;
485 profile_level = (*env)->GetObjectArrayElement(env, profile_levels, k);
486 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
490 supported_profile = (*env)->GetIntField(env, profile_level, jfields.profile_id);
491 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
495 found_codec = profile == supported_profile;
498 (*env)->DeleteLocalRef(env, profile_level);
499 profile_level = NULL;
509 if (profile_levels) {
510 (*env)->DeleteLocalRef(env, profile_levels);
511 profile_levels = NULL;
515 (*env)->DeleteLocalRef(env, capabilities);
520 (*env)->DeleteLocalRef(env, type);
524 av_freep(&supported_type);
535 (*env)->DeleteLocalRef(env, info);
540 (*env)->DeleteLocalRef(env, types);
551 (*env)->DeleteLocalRef(env, format);
555 (*env)->DeleteLocalRef(env, codec);
559 (*env)->DeleteLocalRef(env, codec_name);
563 (*env)->DeleteLocalRef(env, info);
567 (*env)->DeleteLocalRef(env, type);
571 (*env)->DeleteLocalRef(env, types);
575 (*env)->DeleteLocalRef(env, capabilities);
579 (*env)->DeleteLocalRef(env, profile_level);
582 if (profile_levels) {
583 (*env)->DeleteLocalRef(env, profile_levels);
586 av_freep(&supported_type);
588 ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx);
589 ff_jni_reset_jfields(env, &mediaformat_jfields, jni_amediaformat_mapping, 0, log_ctx);
591 JNI_DETACH_ENV(attached, log_ctx);
600 FFAMediaFormat *ff_AMediaFormat_new(void)
604 FFAMediaFormat *format = NULL;
606 format = av_mallocz(sizeof(FFAMediaFormat));
610 format->class = &amediaformat_class;
612 env = ff_jni_attach_env(&attached, format);
618 if (ff_jni_init_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format) < 0) {
622 format->object = (*env)->NewObject(env, format->jfields.mediaformat_class, format->jfields.init_id);
623 if (!format->object) {
627 format->object = (*env)->NewGlobalRef(env, format->object);
628 if (!format->object) {
632 JNI_DETACH_ENV(attached, format);
636 ff_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format);
638 JNI_DETACH_ENV(attached, format);
645 static FFAMediaFormat *ff_AMediaFormat_newFromObject(void *object)
649 FFAMediaFormat *format = NULL;
651 format = av_mallocz(sizeof(FFAMediaFormat));
655 format->class = &amediaformat_class;
657 env = ff_jni_attach_env(&attached, format);
663 if (ff_jni_init_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format) < 0) {
667 format->object = (*env)->NewGlobalRef(env, object);
668 if (!format->object) {
672 JNI_DETACH_ENV(attached, format);
676 ff_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format);
678 JNI_DETACH_ENV(attached, format);
685 int ff_AMediaFormat_delete(FFAMediaFormat* format)
696 JNI_ATTACH_ENV_OR_RETURN(env, &attached, format, AVERROR_EXTERNAL);
698 (*env)->DeleteGlobalRef(env, format->object);
699 format->object = NULL;
701 ff_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format);
703 JNI_DETACH_ENV(attached, format);
710 char* ff_AMediaFormat_toString(FFAMediaFormat* format)
716 jstring description = NULL;
718 av_assert0(format != NULL);
720 JNI_ATTACH_ENV_OR_RETURN(env, &attached, format, NULL);
722 description = (*env)->CallObjectMethod(env, format->object, format->jfields.to_string_id);
723 if (ff_jni_exception_check(env, 1, NULL) < 0) {
727 ret = ff_jni_jstring_to_utf_chars(env, description, format);
731 (*env)->DeleteLocalRef(env, description);
734 JNI_DETACH_ENV(attached, format);
739 int ff_AMediaFormat_getInt32(FFAMediaFormat* format, const char *name, int32_t *out)
747 av_assert0(format != NULL);
749 JNI_ATTACH_ENV_OR_RETURN(env, &attached, format, 0);
751 key = ff_jni_utf_chars_to_jstring(env, name, format);
757 *out = (*env)->CallIntMethod(env, format->object, format->jfields.get_integer_id, key);
758 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
766 (*env)->DeleteLocalRef(env, key);
769 JNI_DETACH_ENV(attached, format);
774 int ff_AMediaFormat_getInt64(FFAMediaFormat* format, const char *name, int64_t *out)
782 av_assert0(format != NULL);
784 JNI_ATTACH_ENV_OR_RETURN(env, &attached, format, 0);
786 key = ff_jni_utf_chars_to_jstring(env, name, format);
792 *out = (*env)->CallLongMethod(env, format->object, format->jfields.get_long_id, key);
793 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
801 (*env)->DeleteLocalRef(env, key);
804 JNI_DETACH_ENV(attached, format);
809 int ff_AMediaFormat_getFloat(FFAMediaFormat* format, const char *name, float *out)
817 av_assert0(format != NULL);
819 JNI_ATTACH_ENV_OR_RETURN(env, &attached, format, 0);
821 key = ff_jni_utf_chars_to_jstring(env, name, format);
827 *out = (*env)->CallFloatMethod(env, format->object, format->jfields.get_float_id, key);
828 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
836 (*env)->DeleteLocalRef(env, key);
839 JNI_DETACH_ENV(attached, format);
844 int ff_AMediaFormat_getBuffer(FFAMediaFormat* format, const char *name, void** data, size_t *size)
851 jobject result = NULL;
853 av_assert0(format != NULL);
855 JNI_ATTACH_ENV_OR_RETURN(env, &attached, format, 0);
857 key = ff_jni_utf_chars_to_jstring(env, name, format);
863 result = (*env)->CallObjectMethod(env, format->object, format->jfields.get_bytebuffer_id, key);
864 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
869 *data = (*env)->GetDirectBufferAddress(env, result);
870 *size = (*env)->GetDirectBufferCapacity(env, result);
872 if (*data && *size) {
874 *data = av_malloc(*size);
880 memcpy(*data, src, *size);
886 (*env)->DeleteLocalRef(env, key);
890 (*env)->DeleteLocalRef(env, result);
893 JNI_DETACH_ENV(attached, format);
898 int ff_AMediaFormat_getString(FFAMediaFormat* format, const char *name, const char **out)
905 jstring result = NULL;
907 av_assert0(format != NULL);
909 JNI_ATTACH_ENV_OR_RETURN(env, &attached, format, 0);
911 key = ff_jni_utf_chars_to_jstring(env, name, format);
917 result = (*env)->CallObjectMethod(env, format->object, format->jfields.get_string_id, key);
918 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
923 *out = ff_jni_jstring_to_utf_chars(env, result, format);
932 (*env)->DeleteLocalRef(env, key);
936 (*env)->DeleteLocalRef(env, result);
939 JNI_DETACH_ENV(attached, format);
944 void ff_AMediaFormat_setInt32(FFAMediaFormat* format, const char* name, int32_t value)
950 av_assert0(format != NULL);
952 JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, format);
954 key = ff_jni_utf_chars_to_jstring(env, name, format);
959 (*env)->CallVoidMethod(env, format->object, format->jfields.set_integer_id, key, value);
960 if (ff_jni_exception_check(env, 1, format) < 0) {
966 (*env)->DeleteLocalRef(env, key);
969 JNI_DETACH_ENV(attached, format);
972 void ff_AMediaFormat_setInt64(FFAMediaFormat* format, const char* name, int64_t value)
978 av_assert0(format != NULL);
980 JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, format);
982 key = ff_jni_utf_chars_to_jstring(env, name, format);
987 (*env)->CallVoidMethod(env, format->object, format->jfields.set_long_id, key, value);
988 if (ff_jni_exception_check(env, 1, format) < 0) {
994 (*env)->DeleteLocalRef(env, key);
997 JNI_DETACH_ENV(attached, NULL);
1000 void ff_AMediaFormat_setFloat(FFAMediaFormat* format, const char* name, float value)
1006 av_assert0(format != NULL);
1008 JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, format);
1010 key = ff_jni_utf_chars_to_jstring(env, name, format);
1015 (*env)->CallVoidMethod(env, format->object, format->jfields.set_float_id, key, value);
1016 if (ff_jni_exception_check(env, 1, format) < 0) {
1022 (*env)->DeleteLocalRef(env, key);
1025 JNI_DETACH_ENV(attached, NULL);
1028 void ff_AMediaFormat_setString(FFAMediaFormat* format, const char* name, const char* value)
1033 jstring string = NULL;
1035 av_assert0(format != NULL);
1037 JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, format);
1039 key = ff_jni_utf_chars_to_jstring(env, name, format);
1044 string = ff_jni_utf_chars_to_jstring(env, value, format);
1049 (*env)->CallVoidMethod(env, format->object, format->jfields.set_string_id, key, string);
1050 if (ff_jni_exception_check(env, 1, format) < 0) {
1056 (*env)->DeleteLocalRef(env, key);
1060 (*env)->DeleteLocalRef(env, string);
1063 JNI_DETACH_ENV(attached, format);
1066 void ff_AMediaFormat_setBuffer(FFAMediaFormat* format, const char* name, void* data, size_t size)
1071 jobject buffer = NULL;
1072 void *buffer_data = NULL;
1074 av_assert0(format != NULL);
1076 JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, format);
1078 key = ff_jni_utf_chars_to_jstring(env, name, format);
1083 if (!data || !size) {
1087 buffer_data = av_malloc(size);
1092 memcpy(buffer_data, data, size);
1094 buffer = (*env)->NewDirectByteBuffer(env, buffer_data, size);
1099 (*env)->CallVoidMethod(env, format->object, format->jfields.set_bytebuffer_id, key, buffer);
1100 if (ff_jni_exception_check(env, 1, format) < 0) {
1106 (*env)->DeleteLocalRef(env, key);
1110 (*env)->DeleteLocalRef(env, buffer);
1113 JNI_DETACH_ENV(attached, format);
1116 static int codec_init_static_fields(FFAMediaCodec *codec)
1122 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1124 codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
1125 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1129 codec->BUFFER_FLAG_CODEC_CONFIG = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_codec_config_id);
1130 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1134 codec->BUFFER_FLAG_END_OF_STREAM = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_end_of_stream_id);
1135 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1139 if (codec->jfields.buffer_flag_key_frame_id) {
1140 codec->BUFFER_FLAG_KEY_FRAME = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_key_frame_id);
1141 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1146 codec->CONFIGURE_FLAG_ENCODE = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.configure_flag_encode_id);
1147 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1151 codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
1152 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1156 codec->INFO_OUTPUT_BUFFERS_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_buffers_changed_id);
1157 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1161 codec->INFO_OUTPUT_FORMAT_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_format_changed_id);
1162 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1167 JNI_DETACH_ENV(attached, NULL);
1172 FFAMediaCodec* ff_AMediaCodec_createCodecByName(const char *name)
1176 FFAMediaCodec *codec = NULL;
1177 jstring codec_name = NULL;
1179 codec = av_mallocz(sizeof(FFAMediaCodec));
1183 codec->class = &amediacodec_class;
1185 env = ff_jni_attach_env(&attached, codec);
1191 if (ff_jni_init_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec) < 0) {
1195 codec_name = ff_jni_utf_chars_to_jstring(env, name, codec);
1200 codec->object = (*env)->CallStaticObjectMethod(env, codec->jfields.mediacodec_class, codec->jfields.create_by_codec_name_id, codec_name);
1201 if (ff_jni_exception_check(env, 1, codec) < 0) {
1205 codec->object = (*env)->NewGlobalRef(env, codec->object);
1206 if (!codec->object) {
1210 if (codec_init_static_fields(codec) < 0) {
1214 if (codec->jfields.get_input_buffer_id && codec->jfields.get_output_buffer_id) {
1215 codec->has_get_i_o_buffer = 1;
1218 JNI_DETACH_ENV(attached, codec);
1222 ff_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec);
1225 (*env)->DeleteLocalRef(env, codec_name);
1228 JNI_DETACH_ENV(attached, codec);
1235 FFAMediaCodec* ff_AMediaCodec_createDecoderByType(const char *mime)
1239 FFAMediaCodec *codec = NULL;
1240 jstring mime_type = NULL;
1242 codec = av_mallocz(sizeof(FFAMediaCodec));
1246 codec->class = &amediacodec_class;
1248 env = ff_jni_attach_env(&attached, codec);
1254 if (ff_jni_init_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec) < 0) {
1258 mime_type = ff_jni_utf_chars_to_jstring(env, mime, codec);
1263 codec->object = (*env)->CallStaticObjectMethod(env, codec->jfields.mediacodec_class, codec->jfields.create_decoder_by_type_id, mime_type);
1264 if (ff_jni_exception_check(env, 1, codec) < 0) {
1268 codec->object = (*env)->NewGlobalRef(env, codec->object);
1269 if (!codec->object) {
1273 if (codec_init_static_fields(codec) < 0) {
1277 if (codec->jfields.get_input_buffer_id && codec->jfields.get_output_buffer_id) {
1278 codec->has_get_i_o_buffer = 1;
1281 JNI_DETACH_ENV(attached, codec);
1285 ff_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec);
1288 (*env)->DeleteLocalRef(env, mime_type);
1291 JNI_DETACH_ENV(attached, codec);
1298 FFAMediaCodec* ff_AMediaCodec_createEncoderByType(const char *mime)
1302 FFAMediaCodec *codec = NULL;
1303 jstring mime_type = NULL;
1305 codec = av_mallocz(sizeof(FFAMediaCodec));
1309 codec->class = &amediacodec_class;
1311 env = ff_jni_attach_env(&attached, codec);
1317 if (ff_jni_init_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec) < 0) {
1321 mime_type = ff_jni_utf_chars_to_jstring(env, mime, codec);
1326 codec->object = (*env)->CallStaticObjectMethod(env, codec->jfields.mediacodec_class, codec->jfields.create_encoder_by_type_id, mime_type);
1327 if (ff_jni_exception_check(env, 1, codec) < 0) {
1331 codec->object = (*env)->NewGlobalRef(env, codec->object);
1332 if (!codec->object) {
1336 if (codec_init_static_fields(codec) < 0) {
1340 if (codec->jfields.get_input_buffer_id && codec->jfields.get_output_buffer_id) {
1341 codec->has_get_i_o_buffer = 1;
1344 JNI_DETACH_ENV(attached, NULL);
1348 ff_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec);
1351 (*env)->DeleteLocalRef(env, mime_type);
1354 JNI_DETACH_ENV(attached, codec);
1361 int ff_AMediaCodec_delete(FFAMediaCodec* codec)
1372 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1374 (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_id);
1375 if (ff_jni_exception_check(env, 1, codec) < 0) {
1376 ret = AVERROR_EXTERNAL;
1379 (*env)->DeleteGlobalRef(env, codec->object);
1380 codec->object = NULL;
1382 ff_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec);
1384 JNI_DETACH_ENV(attached, codec);
1391 char *ff_AMediaCodec_getName(FFAMediaCodec *codec)
1396 jobject *name = NULL;
1398 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, NULL);
1400 name = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_name_id);
1401 if (ff_jni_exception_check(env, 1, codec) < 0) {
1405 ret = ff_jni_jstring_to_utf_chars(env, name, codec);
1408 JNI_DETACH_ENV(attached, NULL);
1413 int ff_AMediaCodec_configure(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags)
1419 /* TODO: implement surface handling */
1420 av_assert0(surface == NULL);
1422 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1424 (*env)->CallVoidMethod(env, codec->object, codec->jfields.configure_id, format->object, NULL, NULL, flags);
1425 if (ff_jni_exception_check(env, 1, codec) < 0) {
1426 ret = AVERROR_EXTERNAL;
1431 JNI_DETACH_ENV(attached, NULL);
1436 int ff_AMediaCodec_start(FFAMediaCodec* codec)
1442 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1444 (*env)->CallVoidMethod(env, codec->object, codec->jfields.start_id);
1445 if (ff_jni_exception_check(env, 1, codec) < 0) {
1446 ret = AVERROR_EXTERNAL;
1451 JNI_DETACH_ENV(attached, codec);
1456 int ff_AMediaCodec_stop(FFAMediaCodec* codec)
1462 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1464 (*env)->CallVoidMethod(env, codec->object, codec->jfields.stop_id);
1465 if (ff_jni_exception_check(env, 1, codec) < 0) {
1466 ret = AVERROR_EXTERNAL;
1471 JNI_DETACH_ENV(attached, codec);
1476 int ff_AMediaCodec_flush(FFAMediaCodec* codec)
1482 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1484 (*env)->CallVoidMethod(env, codec->object, codec->jfields.flush_id);
1485 if (ff_jni_exception_check(env, 1, codec) < 0) {
1486 ret = AVERROR_EXTERNAL;
1491 JNI_DETACH_ENV(attached, codec);
1496 int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec* codec, size_t idx, int render)
1502 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1504 (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_output_buffer_id, idx, render);
1505 if (ff_jni_exception_check(env, 1, codec) < 0) {
1506 ret = AVERROR_EXTERNAL;
1511 JNI_DETACH_ENV(attached, codec);
1516 int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs)
1522 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1524 (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_output_buffer_at_time_id, idx, timestampNs);
1525 if (ff_jni_exception_check(env, 1, codec) < 0) {
1526 ret = AVERROR_EXTERNAL;
1531 JNI_DETACH_ENV(attached, codec);
1536 ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec* codec, int64_t timeoutUs)
1542 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1544 ret = (*env)->CallIntMethod(env, codec->object, codec->jfields.dequeue_input_buffer_id, timeoutUs);
1545 if (ff_jni_exception_check(env, 1, codec) < 0) {
1546 ret = AVERROR_EXTERNAL;
1551 JNI_DETACH_ENV(attached, codec);
1556 int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec* codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
1562 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1564 (*env)->CallVoidMethod(env, codec->object, codec->jfields.queue_input_buffer_id, idx, offset, size, time, flags);
1565 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1566 ret = AVERROR_EXTERNAL;
1571 JNI_DETACH_ENV(attached, codec);
1576 ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
1582 jobject mediainfo = NULL;
1584 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, AVERROR_EXTERNAL);
1586 mediainfo = (*env)->NewObject(env, codec->jfields.mediainfo_class, codec->jfields.init_id);
1587 if (ff_jni_exception_check(env, 1, codec) < 0) {
1588 ret = AVERROR_EXTERNAL;
1592 ret = (*env)->CallIntMethod(env, codec->object, codec->jfields.dequeue_output_buffer_id, mediainfo, timeoutUs);
1593 if (ff_jni_exception_check(env, 1, codec) < 0) {
1594 ret = AVERROR_EXTERNAL;
1598 info->flags = (*env)->GetIntField(env, mediainfo, codec->jfields.flags_id);
1599 if (ff_jni_exception_check(env, 1, codec) < 0) {
1600 ret = AVERROR_EXTERNAL;
1604 info->offset = (*env)->GetIntField(env, mediainfo, codec->jfields.offset_id);
1605 if (ff_jni_exception_check(env, 1, codec) < 0) {
1606 ret = AVERROR_EXTERNAL;
1610 info->presentationTimeUs = (*env)->GetLongField(env, mediainfo, codec->jfields.presentation_time_us_id);
1611 if (ff_jni_exception_check(env, 1, codec) < 0) {
1612 ret = AVERROR_EXTERNAL;
1616 info->size = (*env)->GetIntField(env, mediainfo, codec->jfields.size_id);
1617 if (ff_jni_exception_check(env, 1, codec) < 0) {
1618 ret = AVERROR_EXTERNAL;
1623 (*env)->DeleteLocalRef(env, mediainfo);
1626 JNI_DETACH_ENV(attached, NULL);
1631 uint8_t* ff_AMediaCodec_getInputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size)
1633 uint8_t *ret = NULL;
1637 jobject buffer = NULL;
1639 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, NULL);
1641 if (codec->has_get_i_o_buffer) {
1642 buffer = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_input_buffer_id, idx);
1643 if (ff_jni_exception_check(env, 1, codec) < 0) {
1647 if (!codec->input_buffers) {
1648 codec->input_buffers = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_input_buffers_id);
1649 if (ff_jni_exception_check(env, 1, codec) < 0) {
1653 codec->input_buffers = (*env)->NewGlobalRef(env, codec->input_buffers);
1654 if (ff_jni_exception_check(env, 1, codec) < 0) {
1659 buffer = (*env)->GetObjectArrayElement(env, codec->input_buffers, idx);
1660 if (ff_jni_exception_check(env, 1, codec) < 0) {
1665 ret = (*env)->GetDirectBufferAddress(env, buffer);
1666 *out_size = (*env)->GetDirectBufferCapacity(env, buffer);
1669 (*env)->DeleteLocalRef(env, buffer);
1672 JNI_DETACH_ENV(attached, codec);
1677 uint8_t* ff_AMediaCodec_getOutputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size)
1679 uint8_t *ret = NULL;
1683 jobject buffer = NULL;
1685 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, NULL);
1687 if (codec->has_get_i_o_buffer) {
1688 buffer = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_buffer_id, idx);
1689 if (ff_jni_exception_check(env, 1, codec) < 0) {
1693 if (!codec->output_buffers) {
1694 codec->output_buffers = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_buffers_id);
1695 if (ff_jni_exception_check(env, 1, codec) < 0) {
1699 codec->output_buffers = (*env)->NewGlobalRef(env, codec->output_buffers);
1700 if (ff_jni_exception_check(env, 1, codec) < 0) {
1705 buffer = (*env)->GetObjectArrayElement(env, codec->output_buffers, idx);
1706 if (ff_jni_exception_check(env, 1, codec) < 0) {
1711 ret = (*env)->GetDirectBufferAddress(env, buffer);
1712 *out_size = (*env)->GetDirectBufferCapacity(env, buffer);
1715 (*env)->DeleteLocalRef(env, buffer);
1718 JNI_DETACH_ENV(attached, codec);
1723 FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec)
1725 FFAMediaFormat *ret = NULL;
1729 jobject mediaformat = NULL;
1731 JNI_ATTACH_ENV_OR_RETURN(env, &attached, codec, NULL);
1733 mediaformat = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_format_id);
1734 if (ff_jni_exception_check(env, 1, codec) < 0) {
1738 ret = ff_AMediaFormat_newFromObject(mediaformat);
1741 (*env)->DeleteLocalRef(env, mediaformat);
1744 JNI_DETACH_ENV(attached, codec);
1749 int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx)
1751 return idx == codec->INFO_TRY_AGAIN_LATER;
1754 int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx)
1756 return idx == codec->INFO_OUTPUT_BUFFERS_CHANGED;
1759 int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx)
1761 return idx == codec->INFO_OUTPUT_FORMAT_CHANGED;
1764 int ff_AMediaCodec_getBufferFlagCodecConfig(FFAMediaCodec *codec)
1766 return codec->BUFFER_FLAG_CODEC_CONFIG;
1769 int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec)
1771 return codec->BUFFER_FLAG_END_OF_STREAM;
1774 int ff_AMediaCodec_getBufferFlagKeyFrame(FFAMediaCodec *codec)
1776 return codec->BUFFER_FLAG_KEY_FRAME;
1779 int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec)
1781 return codec->CONFIGURE_FLAG_ENCODE;
1784 int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec)
1788 if (!codec->has_get_i_o_buffer) {
1789 if (codec->output_buffers) {
1793 env = ff_jni_attach_env(&attached, codec);
1795 ret = AVERROR_EXTERNAL;
1799 (*env)->DeleteGlobalRef(env, codec->output_buffers);
1800 codec->output_buffers = NULL;
1802 JNI_DETACH_ENV(attached, codec);