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 jfieldID hevc_profile_main_id;
66 jfieldID hevc_profile_main10_id;
67 jfieldID hevc_profile_main10_hdr10_id;
71 static const struct FFJniField jni_amediacodeclist_mapping[] = {
72 { "android/media/MediaCodecList", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediacodec_list_class), 1 },
73 { "android/media/MediaCodecList", "<init>", "(I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, init_id), 0 },
74 { "android/media/MediaCodecList", "findDecoderForFormat", "(Landroid/media/MediaFormat;)Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, find_decoder_for_format_id), 0 },
76 { "android/media/MediaCodecList", "getCodecCount", "()I", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_count_id), 1 },
77 { "android/media/MediaCodecList", "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_info_at_id), 1 },
79 { "android/media/MediaCodecInfo", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediacodec_info_class), 1 },
80 { "android/media/MediaCodecInfo", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_name_id), 1 },
81 { "android/media/MediaCodecInfo", "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_capabilities_id), 1 },
82 { "android/media/MediaCodecInfo", "getSupportedTypes", "()[Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_supported_types_id), 1 },
83 { "android/media/MediaCodecInfo", "isEncoder", "()Z", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, is_encoder_id), 1 },
85 { "android/media/MediaCodecInfo$CodecCapabilities", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, codec_capabilities_class), 1 },
86 { "android/media/MediaCodecInfo$CodecCapabilities", "colorFormats", "[I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, color_formats_id), 1 },
87 { "android/media/MediaCodecInfo$CodecCapabilities", "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, profile_levels_id), 1 },
89 { "android/media/MediaCodecInfo$CodecProfileLevel", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, codec_profile_level_class), 1 },
90 { "android/media/MediaCodecInfo$CodecProfileLevel", "profile", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, profile_id), 1 },
91 { "android/media/MediaCodecInfo$CodecProfileLevel", "level", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, level_id), 1 },
93 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileBaseline", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_baseline_id), 1 },
94 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileMain", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_main_id), 1 },
95 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileExtended", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_extended_id), 1 },
96 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high_id), 1 },
97 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh10", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high10_id), 1 },
98 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh422", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high422_id), 1 },
99 { "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh444", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high444_id), 1 },
101 { "android/media/MediaCodecInfo$CodecProfileLevel", "HEVCProfileMain", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, hevc_profile_main_id), 0 },
102 { "android/media/MediaCodecInfo$CodecProfileLevel", "HEVCProfileMain10", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, hevc_profile_main10_id), 0 },
103 { "android/media/MediaCodecInfo$CodecProfileLevel", "HEVCProfileMain10HDR10", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, hevc_profile_main10_hdr10_id), 0 },
108 struct JNIAMediaFormatFields {
110 jclass mediaformat_class;
114 jmethodID contains_key_id;
116 jmethodID get_integer_id;
117 jmethodID get_long_id;
118 jmethodID get_float_id;
119 jmethodID get_bytebuffer_id;
120 jmethodID get_string_id;
122 jmethodID set_integer_id;
123 jmethodID set_long_id;
124 jmethodID set_float_id;
125 jmethodID set_bytebuffer_id;
126 jmethodID set_string_id;
128 jmethodID to_string_id;
132 static const struct FFJniField jni_amediaformat_mapping[] = {
133 { "android/media/MediaFormat", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaFormatFields, mediaformat_class), 1 },
135 { "android/media/MediaFormat", "<init>", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, init_id), 1 },
137 { "android/media/MediaFormat", "containsKey", "(Ljava/lang/String;)Z", FF_JNI_METHOD,offsetof(struct JNIAMediaFormatFields, contains_key_id), 1 },
139 { "android/media/MediaFormat", "getInteger", "(Ljava/lang/String;)I", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_integer_id), 1 },
140 { "android/media/MediaFormat", "getLong", "(Ljava/lang/String;)J", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_long_id), 1 },
141 { "android/media/MediaFormat", "getFloat", "(Ljava/lang/String;)F", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_float_id), 1 },
142 { "android/media/MediaFormat", "getByteBuffer", "(Ljava/lang/String;)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_bytebuffer_id), 1 },
143 { "android/media/MediaFormat", "getString", "(Ljava/lang/String;)Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_string_id), 1 },
145 { "android/media/MediaFormat", "setInteger", "(Ljava/lang/String;I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_integer_id), 1 },
146 { "android/media/MediaFormat", "setLong", "(Ljava/lang/String;J)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_long_id), 1 },
147 { "android/media/MediaFormat", "setFloat", "(Ljava/lang/String;F)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_float_id), 1 },
148 { "android/media/MediaFormat", "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_bytebuffer_id), 1 },
149 { "android/media/MediaFormat", "setString", "(Ljava/lang/String;Ljava/lang/String;)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_string_id), 1 },
151 { "android/media/MediaFormat", "toString", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, to_string_id), 1 },
156 static const AVClass amediaformat_class = {
157 .class_name = "amediaformat",
158 .item_name = av_default_item_name,
159 .version = LIBAVUTIL_VERSION_INT,
162 struct FFAMediaFormat {
164 const AVClass *class;
165 struct JNIAMediaFormatFields jfields;
169 struct JNIAMediaCodecFields {
171 jclass mediacodec_class;
173 jfieldID info_try_again_later_id;
174 jfieldID info_output_buffers_changed_id;
175 jfieldID info_output_format_changed_id;
177 jfieldID buffer_flag_codec_config_id;
178 jfieldID buffer_flag_end_of_stream_id;
179 jfieldID buffer_flag_key_frame_id;
181 jfieldID configure_flag_encode_id;
183 jmethodID create_by_codec_name_id;
184 jmethodID create_decoder_by_type_id;
185 jmethodID create_encoder_by_type_id;
187 jmethodID get_name_id;
189 jmethodID configure_id;
193 jmethodID release_id;
195 jmethodID get_output_format_id;
197 jmethodID dequeue_input_buffer_id;
198 jmethodID queue_input_buffer_id;
199 jmethodID get_input_buffer_id;
200 jmethodID get_input_buffers_id;
202 jmethodID dequeue_output_buffer_id;
203 jmethodID get_output_buffer_id;
204 jmethodID get_output_buffers_id;
205 jmethodID release_output_buffer_id;
206 jmethodID release_output_buffer_at_time_id;
208 jclass mediainfo_class;
214 jfieldID presentation_time_us_id;
219 static const struct FFJniField jni_amediacodec_mapping[] = {
220 { "android/media/MediaCodec", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecFields, mediacodec_class), 1 },
222 { "android/media/MediaCodec", "INFO_TRY_AGAIN_LATER", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_try_again_later_id), 1 },
223 { "android/media/MediaCodec", "INFO_OUTPUT_BUFFERS_CHANGED", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_output_buffers_changed_id), 1 },
224 { "android/media/MediaCodec", "INFO_OUTPUT_FORMAT_CHANGED", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_output_format_changed_id), 1 },
226 { "android/media/MediaCodec", "BUFFER_FLAG_CODEC_CONFIG", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, buffer_flag_codec_config_id), 1 },
227 { "android/media/MediaCodec", "BUFFER_FLAG_END_OF_STREAM", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, buffer_flag_end_of_stream_id), 1 },
228 { "android/media/MediaCodec", "BUFFER_FLAG_KEY_FRAME", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, buffer_flag_key_frame_id), 0 },
230 { "android/media/MediaCodec", "CONFIGURE_FLAG_ENCODE", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, configure_flag_encode_id), 1 },
232 { "android/media/MediaCodec", "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_by_codec_name_id), 1 },
233 { "android/media/MediaCodec", "createDecoderByType", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_decoder_by_type_id), 1 },
234 { "android/media/MediaCodec", "createEncoderByType", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_encoder_by_type_id), 1 },
236 { "android/media/MediaCodec", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_name_id), 1 },
238 { "android/media/MediaCodec", "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, configure_id), 1 },
239 { "android/media/MediaCodec", "start", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, start_id), 1 },
240 { "android/media/MediaCodec", "flush", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, flush_id), 1 },
241 { "android/media/MediaCodec", "stop", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, stop_id), 1 },
242 { "android/media/MediaCodec", "release", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_id), 1 },
244 { "android/media/MediaCodec", "getOutputFormat", "()Landroid/media/MediaFormat;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_format_id), 1 },
246 { "android/media/MediaCodec", "dequeueInputBuffer", "(J)I", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, dequeue_input_buffer_id), 1 },
247 { "android/media/MediaCodec", "queueInputBuffer", "(IIIJI)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, queue_input_buffer_id), 1 },
248 { "android/media/MediaCodec", "getInputBuffer", "(I)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_input_buffer_id), 0 },
249 { "android/media/MediaCodec", "getInputBuffers", "()[Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_input_buffers_id), 1 },
251 { "android/media/MediaCodec", "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, dequeue_output_buffer_id), 1 },
252 { "android/media/MediaCodec", "getOutputBuffer", "(I)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_buffer_id), 0 },
253 { "android/media/MediaCodec", "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_buffers_id), 1 },
254 { "android/media/MediaCodec", "releaseOutputBuffer", "(IZ)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_output_buffer_id), 1 },
255 { "android/media/MediaCodec", "releaseOutputBuffer", "(IJ)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_output_buffer_at_time_id), 0 },
257 { "android/media/MediaCodec$BufferInfo", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecFields, mediainfo_class), 1 },
259 { "android/media/MediaCodec.BufferInfo", "<init>", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, init_id), 1 },
260 { "android/media/MediaCodec.BufferInfo", "flags", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, flags_id), 1 },
261 { "android/media/MediaCodec.BufferInfo", "offset", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, offset_id), 1 },
262 { "android/media/MediaCodec.BufferInfo", "presentationTimeUs", "J", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, presentation_time_us_id), 1 },
263 { "android/media/MediaCodec.BufferInfo", "size", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, size_id), 1 },
268 static const AVClass amediacodec_class = {
269 .class_name = "amediacodec",
270 .item_name = av_default_item_name,
271 .version = LIBAVUTIL_VERSION_INT,
274 struct FFAMediaCodec {
276 const AVClass *class;
278 struct JNIAMediaCodecFields jfields;
283 jobject input_buffers;
284 jobject output_buffers;
286 int INFO_TRY_AGAIN_LATER;
287 int INFO_OUTPUT_BUFFERS_CHANGED;
288 int INFO_OUTPUT_FORMAT_CHANGED;
290 int BUFFER_FLAG_CODEC_CONFIG;
291 int BUFFER_FLAG_END_OF_STREAM;
292 int BUFFER_FLAG_KEY_FRAME;
294 int CONFIGURE_FLAG_ENCODE;
296 int has_get_i_o_buffer;
299 #define JNI_GET_ENV_OR_RETURN(env, log_ctx, ret) do { \
300 (env) = ff_jni_get_env(log_ctx); \
306 #define JNI_GET_ENV_OR_RETURN_VOID(env, log_ctx) do { \
307 (env) = ff_jni_get_env(log_ctx); \
313 int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
318 struct JNIAMediaCodecListFields jfields = { 0 };
319 jfieldID field_id = 0;
321 JNI_GET_ENV_OR_RETURN(env, avctx, -1);
323 if (ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, avctx) < 0) {
327 if (avctx->codec_id == AV_CODEC_ID_H264) {
328 switch(avctx->profile) {
329 case FF_PROFILE_H264_BASELINE:
330 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
331 field_id = jfields.avc_profile_baseline_id;
333 case FF_PROFILE_H264_MAIN:
334 field_id = jfields.avc_profile_main_id;
336 case FF_PROFILE_H264_EXTENDED:
337 field_id = jfields.avc_profile_extended_id;
339 case FF_PROFILE_H264_HIGH:
340 field_id = jfields.avc_profile_high_id;
342 case FF_PROFILE_H264_HIGH_10:
343 case FF_PROFILE_H264_HIGH_10_INTRA:
344 field_id = jfields.avc_profile_high10_id;
346 case FF_PROFILE_H264_HIGH_422:
347 case FF_PROFILE_H264_HIGH_422_INTRA:
348 field_id = jfields.avc_profile_high422_id;
350 case FF_PROFILE_H264_HIGH_444:
351 case FF_PROFILE_H264_HIGH_444_INTRA:
352 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
353 field_id = jfields.avc_profile_high444_id;
356 } else if (avctx->codec_id == AV_CODEC_ID_HEVC) {
357 switch (avctx->profile) {
358 case FF_PROFILE_HEVC_MAIN:
359 case FF_PROFILE_HEVC_MAIN_STILL_PICTURE:
360 field_id = jfields.hevc_profile_main_id;
362 case FF_PROFILE_HEVC_MAIN_10:
363 field_id = jfields.hevc_profile_main10_id;
369 ret = (*env)->GetStaticIntField(env, jfields.codec_profile_level_class, field_id);
370 if (ff_jni_exception_check(env, 1, avctx) < 0) {
377 ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, avctx);
382 char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx)
389 char *supported_type = NULL;
392 struct JNIAMediaCodecListFields jfields = { 0 };
393 struct JNIAMediaFormatFields mediaformat_jfields = { 0 };
395 jobject format = NULL;
396 jobject codec = NULL;
397 jobject codec_name = NULL;
401 jobjectArray types = NULL;
403 jobject capabilities = NULL;
404 jobject profile_level = NULL;
405 jobjectArray profile_levels = NULL;
407 JNI_GET_ENV_OR_RETURN(env, log_ctx, NULL);
409 if ((ret = ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx)) < 0) {
413 if ((ret = ff_jni_init_jfields(env, &mediaformat_jfields, jni_amediaformat_mapping, 0, log_ctx)) < 0) {
417 codec_count = (*env)->CallStaticIntMethod(env, jfields.mediacodec_list_class, jfields.get_codec_count_id);
418 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
422 for(i = 0; i < codec_count; i++) {
427 info = (*env)->CallStaticObjectMethod(env, jfields.mediacodec_list_class, jfields.get_codec_info_at_id, i);
428 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
432 types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types_id);
433 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
437 is_encoder = (*env)->CallBooleanMethod(env, info, jfields.is_encoder_id);
438 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
442 if (is_encoder != encoder) {
446 type_count = (*env)->GetArrayLength(env, types);
447 for (j = 0; j < type_count; j++) {
451 type = (*env)->GetObjectArrayElement(env, types, j);
452 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
456 supported_type = ff_jni_jstring_to_utf_chars(env, type, log_ctx);
457 if (!supported_type) {
461 if (!av_strcasecmp(supported_type, mime)) {
462 codec_name = (*env)->CallObjectMethod(env, info, jfields.get_name_id);
463 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
467 name = ff_jni_jstring_to_utf_chars(env, codec_name, log_ctx);
472 /* Skip software decoders */
474 strstr(name, "OMX.google") ||
475 strstr(name, "OMX.ffmpeg") ||
476 (strstr(name, "OMX.SEC") && strstr(name, ".sw.")) ||
477 !strcmp(name, "OMX.qcom.video.decoder.hevcswvdec")) {
482 capabilities = (*env)->CallObjectMethod(env, info, jfields.get_codec_capabilities_id, type);
483 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
487 profile_levels = (*env)->GetObjectField(env, capabilities, jfields.profile_levels_id);
488 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
492 profile_count = (*env)->GetArrayLength(env, profile_levels);
493 if (!profile_count) {
496 for (k = 0; k < profile_count; k++) {
497 int supported_profile = 0;
504 profile_level = (*env)->GetObjectArrayElement(env, profile_levels, k);
505 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
509 supported_profile = (*env)->GetIntField(env, profile_level, jfields.profile_id);
510 if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
514 found_codec = profile == supported_profile;
517 (*env)->DeleteLocalRef(env, profile_level);
518 profile_level = NULL;
528 if (profile_levels) {
529 (*env)->DeleteLocalRef(env, profile_levels);
530 profile_levels = NULL;
534 (*env)->DeleteLocalRef(env, capabilities);
539 (*env)->DeleteLocalRef(env, type);
543 av_freep(&supported_type);
554 (*env)->DeleteLocalRef(env, info);
559 (*env)->DeleteLocalRef(env, types);
570 (*env)->DeleteLocalRef(env, format);
574 (*env)->DeleteLocalRef(env, codec);
578 (*env)->DeleteLocalRef(env, codec_name);
582 (*env)->DeleteLocalRef(env, info);
586 (*env)->DeleteLocalRef(env, type);
590 (*env)->DeleteLocalRef(env, types);
594 (*env)->DeleteLocalRef(env, capabilities);
598 (*env)->DeleteLocalRef(env, profile_level);
601 if (profile_levels) {
602 (*env)->DeleteLocalRef(env, profile_levels);
605 av_freep(&supported_type);
607 ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx);
608 ff_jni_reset_jfields(env, &mediaformat_jfields, jni_amediaformat_mapping, 0, log_ctx);
617 FFAMediaFormat *ff_AMediaFormat_new(void)
620 FFAMediaFormat *format = NULL;
621 jobject object = NULL;
623 format = av_mallocz(sizeof(FFAMediaFormat));
627 format->class = &amediaformat_class;
629 env = ff_jni_get_env(format);
635 if (ff_jni_init_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format) < 0) {
639 object = (*env)->NewObject(env, format->jfields.mediaformat_class, format->jfields.init_id);
644 format->object = (*env)->NewGlobalRef(env, object);
645 if (!format->object) {
651 (*env)->DeleteLocalRef(env, object);
654 if (!format->object) {
655 ff_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format);
662 static FFAMediaFormat *ff_AMediaFormat_newFromObject(void *object)
665 FFAMediaFormat *format = NULL;
667 format = av_mallocz(sizeof(FFAMediaFormat));
671 format->class = &amediaformat_class;
673 env = ff_jni_get_env(format);
679 if (ff_jni_init_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format) < 0) {
683 format->object = (*env)->NewGlobalRef(env, object);
684 if (!format->object) {
690 ff_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format);
697 int ff_AMediaFormat_delete(FFAMediaFormat* format)
707 JNI_GET_ENV_OR_RETURN(env, format, AVERROR_EXTERNAL);
709 (*env)->DeleteGlobalRef(env, format->object);
710 format->object = NULL;
712 ff_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format);
719 char* ff_AMediaFormat_toString(FFAMediaFormat* format)
724 jstring description = NULL;
726 av_assert0(format != NULL);
728 JNI_GET_ENV_OR_RETURN(env, format, NULL);
730 description = (*env)->CallObjectMethod(env, format->object, format->jfields.to_string_id);
731 if (ff_jni_exception_check(env, 1, NULL) < 0) {
735 ret = ff_jni_jstring_to_utf_chars(env, description, format);
738 (*env)->DeleteLocalRef(env, description);
744 int ff_AMediaFormat_getInt32(FFAMediaFormat* format, const char *name, int32_t *out)
750 jboolean contains_key;
752 av_assert0(format != NULL);
754 JNI_GET_ENV_OR_RETURN(env, format, 0);
756 key = ff_jni_utf_chars_to_jstring(env, name, format);
762 contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
763 if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
768 *out = (*env)->CallIntMethod(env, format->object, format->jfields.get_integer_id, key);
769 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
777 (*env)->DeleteLocalRef(env, key);
783 int ff_AMediaFormat_getInt64(FFAMediaFormat* format, const char *name, int64_t *out)
789 jboolean contains_key;
791 av_assert0(format != NULL);
793 JNI_GET_ENV_OR_RETURN(env, format, 0);
795 key = ff_jni_utf_chars_to_jstring(env, name, format);
801 contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
802 if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
807 *out = (*env)->CallLongMethod(env, format->object, format->jfields.get_long_id, key);
808 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
816 (*env)->DeleteLocalRef(env, key);
822 int ff_AMediaFormat_getFloat(FFAMediaFormat* format, const char *name, float *out)
828 jboolean contains_key;
830 av_assert0(format != NULL);
832 JNI_GET_ENV_OR_RETURN(env, format, 0);
834 key = ff_jni_utf_chars_to_jstring(env, name, format);
840 contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
841 if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
846 *out = (*env)->CallFloatMethod(env, format->object, format->jfields.get_float_id, key);
847 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
855 (*env)->DeleteLocalRef(env, key);
861 int ff_AMediaFormat_getBuffer(FFAMediaFormat* format, const char *name, void** data, size_t *size)
867 jboolean contains_key;
868 jobject result = NULL;
870 av_assert0(format != NULL);
872 JNI_GET_ENV_OR_RETURN(env, format, 0);
874 key = ff_jni_utf_chars_to_jstring(env, name, format);
880 contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
881 if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
886 result = (*env)->CallObjectMethod(env, format->object, format->jfields.get_bytebuffer_id, key);
887 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
892 *data = (*env)->GetDirectBufferAddress(env, result);
893 *size = (*env)->GetDirectBufferCapacity(env, result);
895 if (*data && *size) {
897 *data = av_malloc(*size);
903 memcpy(*data, src, *size);
909 (*env)->DeleteLocalRef(env, key);
913 (*env)->DeleteLocalRef(env, result);
919 int ff_AMediaFormat_getString(FFAMediaFormat* format, const char *name, const char **out)
925 jboolean contains_key;
926 jstring result = NULL;
928 av_assert0(format != NULL);
930 JNI_GET_ENV_OR_RETURN(env, format, 0);
932 key = ff_jni_utf_chars_to_jstring(env, name, format);
938 contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
939 if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
944 result = (*env)->CallObjectMethod(env, format->object, format->jfields.get_string_id, key);
945 if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
950 *out = ff_jni_jstring_to_utf_chars(env, result, format);
959 (*env)->DeleteLocalRef(env, key);
963 (*env)->DeleteLocalRef(env, result);
969 void ff_AMediaFormat_setInt32(FFAMediaFormat* format, const char* name, int32_t value)
974 av_assert0(format != NULL);
976 JNI_GET_ENV_OR_RETURN_VOID(env, format);
978 key = ff_jni_utf_chars_to_jstring(env, name, format);
983 (*env)->CallVoidMethod(env, format->object, format->jfields.set_integer_id, key, value);
984 if (ff_jni_exception_check(env, 1, format) < 0) {
990 (*env)->DeleteLocalRef(env, key);
994 void ff_AMediaFormat_setInt64(FFAMediaFormat* format, const char* name, int64_t value)
999 av_assert0(format != NULL);
1001 JNI_GET_ENV_OR_RETURN_VOID(env, format);
1003 key = ff_jni_utf_chars_to_jstring(env, name, format);
1008 (*env)->CallVoidMethod(env, format->object, format->jfields.set_long_id, key, value);
1009 if (ff_jni_exception_check(env, 1, format) < 0) {
1015 (*env)->DeleteLocalRef(env, key);
1019 void ff_AMediaFormat_setFloat(FFAMediaFormat* format, const char* name, float value)
1024 av_assert0(format != NULL);
1026 JNI_GET_ENV_OR_RETURN_VOID(env, format);
1028 key = ff_jni_utf_chars_to_jstring(env, name, format);
1033 (*env)->CallVoidMethod(env, format->object, format->jfields.set_float_id, key, value);
1034 if (ff_jni_exception_check(env, 1, format) < 0) {
1040 (*env)->DeleteLocalRef(env, key);
1044 void ff_AMediaFormat_setString(FFAMediaFormat* format, const char* name, const char* value)
1048 jstring string = NULL;
1050 av_assert0(format != NULL);
1052 JNI_GET_ENV_OR_RETURN_VOID(env, format);
1054 key = ff_jni_utf_chars_to_jstring(env, name, format);
1059 string = ff_jni_utf_chars_to_jstring(env, value, format);
1064 (*env)->CallVoidMethod(env, format->object, format->jfields.set_string_id, key, string);
1065 if (ff_jni_exception_check(env, 1, format) < 0) {
1071 (*env)->DeleteLocalRef(env, key);
1075 (*env)->DeleteLocalRef(env, string);
1079 void ff_AMediaFormat_setBuffer(FFAMediaFormat* format, const char* name, void* data, size_t size)
1083 jobject buffer = NULL;
1084 void *buffer_data = NULL;
1086 av_assert0(format != NULL);
1088 JNI_GET_ENV_OR_RETURN_VOID(env, format);
1090 key = ff_jni_utf_chars_to_jstring(env, name, format);
1095 if (!data || !size) {
1099 buffer_data = av_malloc(size);
1104 memcpy(buffer_data, data, size);
1106 buffer = (*env)->NewDirectByteBuffer(env, buffer_data, size);
1111 (*env)->CallVoidMethod(env, format->object, format->jfields.set_bytebuffer_id, key, buffer);
1112 if (ff_jni_exception_check(env, 1, format) < 0) {
1118 (*env)->DeleteLocalRef(env, key);
1122 (*env)->DeleteLocalRef(env, buffer);
1126 static int codec_init_static_fields(FFAMediaCodec *codec)
1131 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1133 codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
1134 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1138 codec->BUFFER_FLAG_CODEC_CONFIG = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_codec_config_id);
1139 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1143 codec->BUFFER_FLAG_END_OF_STREAM = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_end_of_stream_id);
1144 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1148 if (codec->jfields.buffer_flag_key_frame_id) {
1149 codec->BUFFER_FLAG_KEY_FRAME = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_key_frame_id);
1150 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1155 codec->CONFIGURE_FLAG_ENCODE = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.configure_flag_encode_id);
1156 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1160 codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
1161 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1165 codec->INFO_OUTPUT_BUFFERS_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_buffers_changed_id);
1166 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1170 codec->INFO_OUTPUT_FORMAT_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_format_changed_id);
1171 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1180 #define CREATE_CODEC_BY_NAME 0
1181 #define CREATE_DECODER_BY_TYPE 1
1182 #define CREATE_ENCODER_BY_TYPE 2
1184 static inline FFAMediaCodec *codec_create(int method, const char *arg)
1188 FFAMediaCodec *codec = NULL;
1189 jstring jarg = NULL;
1190 jobject object = NULL;
1191 jobject buffer_info = NULL;
1192 jmethodID create_id = NULL;
1194 codec = av_mallocz(sizeof(FFAMediaCodec));
1198 codec->class = &amediacodec_class;
1200 env = ff_jni_get_env(codec);
1206 if (ff_jni_init_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec) < 0) {
1210 jarg = ff_jni_utf_chars_to_jstring(env, arg, codec);
1216 case CREATE_CODEC_BY_NAME: create_id = codec->jfields.create_by_codec_name_id; break;
1217 case CREATE_DECODER_BY_TYPE: create_id = codec->jfields.create_decoder_by_type_id; break;
1218 case CREATE_ENCODER_BY_TYPE: create_id = codec->jfields.create_encoder_by_type_id; break;
1223 object = (*env)->CallStaticObjectMethod(env,
1224 codec->jfields.mediacodec_class,
1227 if (ff_jni_exception_check(env, 1, codec) < 0) {
1231 codec->object = (*env)->NewGlobalRef(env, object);
1232 if (!codec->object) {
1236 if (codec_init_static_fields(codec) < 0) {
1240 if (codec->jfields.get_input_buffer_id && codec->jfields.get_output_buffer_id) {
1241 codec->has_get_i_o_buffer = 1;
1244 buffer_info = (*env)->NewObject(env, codec->jfields.mediainfo_class, codec->jfields.init_id);
1245 if (ff_jni_exception_check(env, 1, codec) < 0) {
1249 codec->buffer_info = (*env)->NewGlobalRef(env, buffer_info);
1250 if (!codec->buffer_info) {
1257 (*env)->DeleteLocalRef(env, jarg);
1261 (*env)->DeleteLocalRef(env, object);
1265 (*env)->DeleteLocalRef(env, buffer_info);
1269 if (codec->object) {
1270 (*env)->DeleteGlobalRef(env, codec->object);
1273 if (codec->buffer_info) {
1274 (*env)->DeleteGlobalRef(env, codec->buffer_info);
1277 ff_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec);
1284 #define DECLARE_FF_AMEDIACODEC_CREATE_FUNC(name, method) \
1285 FFAMediaCodec *ff_AMediaCodec_##name(const char *arg) \
1287 return codec_create(method, arg); \
1290 DECLARE_FF_AMEDIACODEC_CREATE_FUNC(createCodecByName, CREATE_CODEC_BY_NAME)
1291 DECLARE_FF_AMEDIACODEC_CREATE_FUNC(createDecoderByType, CREATE_DECODER_BY_TYPE)
1292 DECLARE_FF_AMEDIACODEC_CREATE_FUNC(createEncoderByType, CREATE_ENCODER_BY_TYPE)
1294 int ff_AMediaCodec_delete(FFAMediaCodec* codec)
1304 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1306 (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_id);
1307 if (ff_jni_exception_check(env, 1, codec) < 0) {
1308 ret = AVERROR_EXTERNAL;
1311 (*env)->DeleteGlobalRef(env, codec->object);
1312 codec->object = NULL;
1314 (*env)->DeleteGlobalRef(env, codec->buffer_info);
1315 codec->buffer_info = NULL;
1317 ff_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec);
1324 char *ff_AMediaCodec_getName(FFAMediaCodec *codec)
1328 jobject *name = NULL;
1330 JNI_GET_ENV_OR_RETURN(env, codec, NULL);
1332 name = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_name_id);
1333 if (ff_jni_exception_check(env, 1, codec) < 0) {
1337 ret = ff_jni_jstring_to_utf_chars(env, name, codec);
1343 int ff_AMediaCodec_configure(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags)
1348 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1350 (*env)->CallVoidMethod(env, codec->object, codec->jfields.configure_id, format->object, surface, NULL, flags);
1351 if (ff_jni_exception_check(env, 1, codec) < 0) {
1352 ret = AVERROR_EXTERNAL;
1360 int ff_AMediaCodec_start(FFAMediaCodec* codec)
1365 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1367 (*env)->CallVoidMethod(env, codec->object, codec->jfields.start_id);
1368 if (ff_jni_exception_check(env, 1, codec) < 0) {
1369 ret = AVERROR_EXTERNAL;
1377 int ff_AMediaCodec_stop(FFAMediaCodec* codec)
1382 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1384 (*env)->CallVoidMethod(env, codec->object, codec->jfields.stop_id);
1385 if (ff_jni_exception_check(env, 1, codec) < 0) {
1386 ret = AVERROR_EXTERNAL;
1394 int ff_AMediaCodec_flush(FFAMediaCodec* codec)
1399 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1401 (*env)->CallVoidMethod(env, codec->object, codec->jfields.flush_id);
1402 if (ff_jni_exception_check(env, 1, codec) < 0) {
1403 ret = AVERROR_EXTERNAL;
1411 int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec* codec, size_t idx, int render)
1416 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1418 (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_output_buffer_id, (jint)idx, (jboolean)render);
1419 if (ff_jni_exception_check(env, 1, codec) < 0) {
1420 ret = AVERROR_EXTERNAL;
1428 int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs)
1433 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1435 (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_output_buffer_at_time_id, (jint)idx, (jlong)timestampNs);
1436 if (ff_jni_exception_check(env, 1, codec) < 0) {
1437 ret = AVERROR_EXTERNAL;
1445 ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec* codec, int64_t timeoutUs)
1450 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1452 ret = (*env)->CallIntMethod(env, codec->object, codec->jfields.dequeue_input_buffer_id, timeoutUs);
1453 if (ff_jni_exception_check(env, 1, codec) < 0) {
1454 ret = AVERROR_EXTERNAL;
1462 int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec* codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
1467 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1469 (*env)->CallVoidMethod(env, codec->object, codec->jfields.queue_input_buffer_id, (jint)idx, (jint)offset, (jint)size, time, flags);
1470 if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1471 ret = AVERROR_EXTERNAL;
1479 ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
1484 JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
1486 ret = (*env)->CallIntMethod(env, codec->object, codec->jfields.dequeue_output_buffer_id, codec->buffer_info, timeoutUs);
1487 if (ff_jni_exception_check(env, 1, codec) < 0) {
1488 return AVERROR_EXTERNAL;
1491 info->flags = (*env)->GetIntField(env, codec->buffer_info, codec->jfields.flags_id);
1492 if (ff_jni_exception_check(env, 1, codec) < 0) {
1493 return AVERROR_EXTERNAL;
1496 info->offset = (*env)->GetIntField(env, codec->buffer_info, codec->jfields.offset_id);
1497 if (ff_jni_exception_check(env, 1, codec) < 0) {
1498 return AVERROR_EXTERNAL;
1501 info->presentationTimeUs = (*env)->GetLongField(env, codec->buffer_info, codec->jfields.presentation_time_us_id);
1502 if (ff_jni_exception_check(env, 1, codec) < 0) {
1503 return AVERROR_EXTERNAL;
1506 info->size = (*env)->GetIntField(env, codec->buffer_info, codec->jfields.size_id);
1507 if (ff_jni_exception_check(env, 1, codec) < 0) {
1508 return AVERROR_EXTERNAL;
1514 uint8_t* ff_AMediaCodec_getInputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size)
1516 uint8_t *ret = NULL;
1519 jobject buffer = NULL;
1520 jobject input_buffers = NULL;
1522 JNI_GET_ENV_OR_RETURN(env, codec, NULL);
1524 if (codec->has_get_i_o_buffer) {
1525 buffer = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_input_buffer_id, (jint)idx);
1526 if (ff_jni_exception_check(env, 1, codec) < 0) {
1530 if (!codec->input_buffers) {
1531 input_buffers = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_input_buffers_id);
1532 if (ff_jni_exception_check(env, 1, codec) < 0) {
1536 codec->input_buffers = (*env)->NewGlobalRef(env, input_buffers);
1537 if (ff_jni_exception_check(env, 1, codec) < 0) {
1542 buffer = (*env)->GetObjectArrayElement(env, codec->input_buffers, idx);
1543 if (ff_jni_exception_check(env, 1, codec) < 0) {
1548 ret = (*env)->GetDirectBufferAddress(env, buffer);
1549 *out_size = (*env)->GetDirectBufferCapacity(env, buffer);
1552 (*env)->DeleteLocalRef(env, buffer);
1555 if (input_buffers) {
1556 (*env)->DeleteLocalRef(env, input_buffers);
1562 uint8_t* ff_AMediaCodec_getOutputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size)
1564 uint8_t *ret = NULL;
1567 jobject buffer = NULL;
1568 jobject output_buffers = NULL;
1570 JNI_GET_ENV_OR_RETURN(env, codec, NULL);
1572 if (codec->has_get_i_o_buffer) {
1573 buffer = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_buffer_id, (jint)idx);
1574 if (ff_jni_exception_check(env, 1, codec) < 0) {
1578 if (!codec->output_buffers) {
1579 output_buffers = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_buffers_id);
1580 if (ff_jni_exception_check(env, 1, codec) < 0) {
1584 codec->output_buffers = (*env)->NewGlobalRef(env, output_buffers);
1585 if (ff_jni_exception_check(env, 1, codec) < 0) {
1590 buffer = (*env)->GetObjectArrayElement(env, codec->output_buffers, idx);
1591 if (ff_jni_exception_check(env, 1, codec) < 0) {
1596 ret = (*env)->GetDirectBufferAddress(env, buffer);
1597 *out_size = (*env)->GetDirectBufferCapacity(env, buffer);
1600 (*env)->DeleteLocalRef(env, buffer);
1603 if (output_buffers) {
1604 (*env)->DeleteLocalRef(env, output_buffers);
1610 FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec)
1612 FFAMediaFormat *ret = NULL;
1615 jobject mediaformat = NULL;
1617 JNI_GET_ENV_OR_RETURN(env, codec, NULL);
1619 mediaformat = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_format_id);
1620 if (ff_jni_exception_check(env, 1, codec) < 0) {
1624 ret = ff_AMediaFormat_newFromObject(mediaformat);
1627 (*env)->DeleteLocalRef(env, mediaformat);
1633 int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx)
1635 return idx == codec->INFO_TRY_AGAIN_LATER;
1638 int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx)
1640 return idx == codec->INFO_OUTPUT_BUFFERS_CHANGED;
1643 int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx)
1645 return idx == codec->INFO_OUTPUT_FORMAT_CHANGED;
1648 int ff_AMediaCodec_getBufferFlagCodecConfig(FFAMediaCodec *codec)
1650 return codec->BUFFER_FLAG_CODEC_CONFIG;
1653 int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec)
1655 return codec->BUFFER_FLAG_END_OF_STREAM;
1658 int ff_AMediaCodec_getBufferFlagKeyFrame(FFAMediaCodec *codec)
1660 return codec->BUFFER_FLAG_KEY_FRAME;
1663 int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec)
1665 return codec->CONFIGURE_FLAG_ENCODE;
1668 int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec)
1672 if (!codec->has_get_i_o_buffer) {
1673 if (codec->output_buffers) {
1676 env = ff_jni_get_env(codec);
1678 ret = AVERROR_EXTERNAL;
1682 (*env)->DeleteGlobalRef(env, codec->output_buffers);
1683 codec->output_buffers = NULL;
1691 int ff_Build_SDK_INT(AVCodecContext *avctx)
1695 jclass versionClass;
1696 jfieldID sdkIntFieldID;
1697 JNI_GET_ENV_OR_RETURN(env, avctx, -1);
1699 versionClass = (*env)->FindClass(env, "android/os/Build$VERSION");
1700 sdkIntFieldID = (*env)->GetStaticFieldID(env, versionClass, "SDK_INT", "I");
1701 ret = (*env)->GetStaticIntField(env, versionClass, sdkIntFieldID);
1702 (*env)->DeleteLocalRef(env, versionClass);