Commit e452abc5 authored by Matthieu Bouron's avatar Matthieu Bouron

lavc/mediacodec: refactor ff_AMediaCodecList_getCodecByType

Allows to select a codec (encoder or decoder) only if it supports a
specific profile.

Adds ff_AMediaCodecProfile_getProfileFromAVCodecContext to convert an
AVCodecContext profile to a MediaCodec profile. It only supports H264
for now.

The codepath using MediaCodecList.findDecoderForFormat() (Android >= 5.0)
has been dropped as this method does not allow to select a decoder
compatible with a specific profile.
parent 5b95b461
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "libavutil/mem.h" #include "libavutil/mem.h"
#include "libavutil/avstring.h" #include "libavutil/avstring.h"
#include "avcodec.h"
#include "ffjni.h" #include "ffjni.h"
#include "version.h" #include "version.h"
#include "mediacodec_wrapper.h" #include "mediacodec_wrapper.h"
...@@ -41,9 +42,26 @@ struct JNIAMediaCodecListFields { ...@@ -41,9 +42,26 @@ struct JNIAMediaCodecListFields {
jclass mediacodec_info_class; jclass mediacodec_info_class;
jmethodID get_name_id; jmethodID get_name_id;
jmethodID get_codec_capabilities_id;
jmethodID get_supported_types_id; jmethodID get_supported_types_id;
jmethodID is_encoder_id; jmethodID is_encoder_id;
jclass codec_capabilities_class;
jfieldID color_formats_id;
jfieldID profile_levels_id;
jclass codec_profile_level_class;
jfieldID profile_id;
jfieldID level_id;
jfieldID avc_profile_baseline_id;
jfieldID avc_profile_main_id;
jfieldID avc_profile_extended_id;
jfieldID avc_profile_high_id;
jfieldID avc_profile_high10_id;
jfieldID avc_profile_high422_id;
jfieldID avc_profile_high444_id;
} JNIAMediaCodecListFields; } JNIAMediaCodecListFields;
static const struct FFJniField jni_amediacodeclist_mapping[] = { static const struct FFJniField jni_amediacodeclist_mapping[] = {
...@@ -56,9 +74,26 @@ static const struct FFJniField jni_amediacodeclist_mapping[] = { ...@@ -56,9 +74,26 @@ static const struct FFJniField jni_amediacodeclist_mapping[] = {
{ "android/media/MediaCodecInfo", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediacodec_info_class), 1 }, { "android/media/MediaCodecInfo", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediacodec_info_class), 1 },
{ "android/media/MediaCodecInfo", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_name_id), 1 }, { "android/media/MediaCodecInfo", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_name_id), 1 },
{ "android/media/MediaCodecInfo", "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_capabilities_id), 1 },
{ "android/media/MediaCodecInfo", "getSupportedTypes", "()[Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_supported_types_id), 1 }, { "android/media/MediaCodecInfo", "getSupportedTypes", "()[Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_supported_types_id), 1 },
{ "android/media/MediaCodecInfo", "isEncoder", "()Z", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, is_encoder_id), 1 }, { "android/media/MediaCodecInfo", "isEncoder", "()Z", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, is_encoder_id), 1 },
{ "android/media/MediaCodecInfo$CodecCapabilities", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, codec_capabilities_class), 1 },
{ "android/media/MediaCodecInfo$CodecCapabilities", "colorFormats", "[I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, color_formats_id), 1 },
{ "android/media/MediaCodecInfo$CodecCapabilities", "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, profile_levels_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, codec_profile_level_class), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "profile", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, profile_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "level", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecListFields, level_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileBaseline", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_baseline_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileMain", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_main_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileExtended", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_extended_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh10", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high10_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh422", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high422_id), 1 },
{ "android/media/MediaCodecInfo$CodecProfileLevel", "AVCProfileHigh444", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecListFields, avc_profile_high444_id), 1 },
{ NULL } { NULL }
}; };
...@@ -267,9 +302,77 @@ struct FFAMediaCodec { ...@@ -267,9 +302,77 @@ struct FFAMediaCodec {
ff_jni_detach_env(log_ctx); \ ff_jni_detach_env(log_ctx); \
} while (0) } while (0)
char *ff_AMediaCodecList_getCodecNameByType(const char *mime, void *log_ctx)
int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
{
int ret = -1;
int attached = 0;
JNIEnv *env = NULL;
struct JNIAMediaCodecListFields jfields = { 0 };
JNI_ATTACH_ENV_OR_RETURN(env, &attached, avctx, -1);
if (ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, avctx) < 0) {
goto done;
}
if (avctx->codec_id == AV_CODEC_ID_H264) {
jfieldID field_id = 0;
switch(avctx->profile) {
case FF_PROFILE_H264_BASELINE:
case FF_PROFILE_H264_CONSTRAINED_BASELINE:
field_id = jfields.avc_profile_baseline_id;
break;
case FF_PROFILE_H264_MAIN:
field_id = jfields.avc_profile_main_id;
break;
case FF_PROFILE_H264_EXTENDED:
field_id = jfields.avc_profile_extended_id;
break;
case FF_PROFILE_H264_HIGH:
field_id = jfields.avc_profile_high_id;
break;
case FF_PROFILE_H264_HIGH_10:
case FF_PROFILE_H264_HIGH_10_INTRA:
field_id = jfields.avc_profile_high10_id;
break;
case FF_PROFILE_H264_HIGH_422:
case FF_PROFILE_H264_HIGH_422_INTRA:
field_id = jfields.avc_profile_high422_id;
break;
case FF_PROFILE_H264_HIGH_444:
case FF_PROFILE_H264_HIGH_444_INTRA:
case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
field_id = jfields.avc_profile_high444_id;
break;
}
if (field_id) {
ret = (*env)->GetStaticIntField(env, jfields.codec_profile_level_class, field_id);
if (ff_jni_exception_check(env, 1, avctx) < 0) {
ret = -1;
goto done;
}
}
}
done:
ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, avctx);
JNI_DETACH_ENV(attached, avctx);
return ret;
}
char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx)
{ {
int ret; int ret;
int i;
int codec_count;
int found_codec = 0;
char *name = NULL; char *name = NULL;
char *supported_type = NULL; char *supported_type = NULL;
...@@ -280,13 +383,16 @@ char *ff_AMediaCodecList_getCodecNameByType(const char *mime, void *log_ctx) ...@@ -280,13 +383,16 @@ char *ff_AMediaCodecList_getCodecNameByType(const char *mime, void *log_ctx)
jobject format = NULL; jobject format = NULL;
jobject codec = NULL; jobject codec = NULL;
jstring key = NULL; jobject codec_name = NULL;
jstring tmp = NULL;
jobject info = NULL; jobject info = NULL;
jobject type = NULL; jobject type = NULL;
jobjectArray types = NULL; jobjectArray types = NULL;
jobject capabilities = NULL;
jobject profile_level = NULL;
jobjectArray profile_levels = NULL;
JNI_ATTACH_ENV_OR_RETURN(env, &attached, log_ctx, NULL); JNI_ATTACH_ENV_OR_RETURN(env, &attached, log_ctx, NULL);
if ((ret = ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx)) < 0) { if ((ret = ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx)) < 0) {
...@@ -297,128 +403,145 @@ char *ff_AMediaCodecList_getCodecNameByType(const char *mime, void *log_ctx) ...@@ -297,128 +403,145 @@ char *ff_AMediaCodecList_getCodecNameByType(const char *mime, void *log_ctx)
goto done; goto done;
} }
if (jfields.init_id && jfields.find_decoder_for_format_id) { codec_count = (*env)->CallStaticIntMethod(env, jfields.mediacodec_list_class, jfields.get_codec_count_id);
key = ff_jni_utf_chars_to_jstring(env, "mime", log_ctx); if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
if (!key) {
goto done; goto done;
} }
tmp = ff_jni_utf_chars_to_jstring(env, mime, log_ctx); for(i = 0; i < codec_count; i++) {
if (!tmp) { int j;
int type_count;
int is_encoder;
info = (*env)->CallStaticObjectMethod(env, jfields.mediacodec_list_class, jfields.get_codec_info_at_id, i);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
format = (*env)->NewObject(env, mediaformat_jfields.mediaformat_class, mediaformat_jfields.init_id); types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types_id);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
(*env)->CallVoidMethod(env, format, mediaformat_jfields.set_string_id, key, tmp); is_encoder = (*env)->CallBooleanMethod(env, info, jfields.is_encoder_id);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
(*env)->DeleteLocalRef(env, key); if (is_encoder != encoder) {
key = NULL; goto done_with_info;
}
(*env)->DeleteLocalRef(env, tmp); type_count = (*env)->GetArrayLength(env, types);
tmp = NULL; for (j = 0; j < type_count; j++) {
int k;
int profile_count;
codec = (*env)->NewObject(env, jfields.mediacodec_list_class, jfields.init_id, 0); type = (*env)->GetObjectArrayElement(env, types, j);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
tmp = (*env)->CallObjectMethod(env, codec, jfields.find_decoder_for_format_id, format); supported_type = ff_jni_jstring_to_utf_chars(env, type, log_ctx);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { if (!supported_type) {
goto done; goto done;
} }
if (!tmp) {
av_log(NULL, AV_LOG_ERROR, "Could not find decoder in media codec list " if (!av_strcasecmp(supported_type, mime)) {
"for format { mime=%s }\n", mime); codec_name = (*env)->CallObjectMethod(env, info, jfields.get_name_id);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
name = ff_jni_jstring_to_utf_chars(env, tmp, log_ctx); name = ff_jni_jstring_to_utf_chars(env, codec_name, log_ctx);
if (!name) { if (!name) {
goto done; goto done;
} }
} else { if (strstr(name, "OMX.google")) {
int i; av_freep(&name);
int codec_count; goto done_with_type;
codec_count = (*env)->CallStaticIntMethod(env, jfields.mediacodec_list_class, jfields.get_codec_count_id);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done;
} }
for(i = 0; i < codec_count; i++) { capabilities = (*env)->CallObjectMethod(env, info, jfields.get_codec_capabilities_id, type);
int j;
int type_count;
int is_encoder;
info = (*env)->CallStaticObjectMethod(env, jfields.mediacodec_list_class, jfields.get_codec_info_at_id, i);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types_id); profile_levels = (*env)->GetObjectField(env, capabilities, jfields.profile_levels_id);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
is_encoder = (*env)->CallBooleanMethod(env, info, jfields.is_encoder_id); profile_count = (*env)->GetArrayLength(env, profile_levels);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { for (k = 0; k < profile_count; k++) {
goto done; int supported_profile = 0;
}
if (is_encoder) { if (profile < 0) {
continue; found_codec = 1;
break;
} }
type_count = (*env)->GetArrayLength(env, types); profile_level = (*env)->GetObjectArrayElement(env, profile_levels, k);
for (j = 0; j < type_count; j++) {
type = (*env)->GetObjectArrayElement(env, types, j);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
supported_type = ff_jni_jstring_to_utf_chars(env, type, log_ctx); supported_profile = (*env)->GetIntField(env, profile_level, jfields.profile_id);
if (!supported_type) { if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
goto done; goto done;
} }
if (!av_strcasecmp(supported_type, mime)) { found_codec = profile == supported_profile;
jobject codec_name;
codec_name = (*env)->CallObjectMethod(env, info, jfields.get_name_id); if (profile_level) {
if (ff_jni_exception_check(env, 1, log_ctx) < 0) { (*env)->DeleteLocalRef(env, profile_level);
goto done; profile_level = NULL;
} }
name = ff_jni_jstring_to_utf_chars(env, codec_name, log_ctx); if (found_codec) {
if (!name) { break;
goto done; }
}
} }
if (strstr(name, "OMX.google")) { done_with_type:
av_freep(&name); if (profile_levels) {
continue; (*env)->DeleteLocalRef(env, profile_levels);
profile_levels = NULL;
} }
if (capabilities) {
(*env)->DeleteLocalRef(env, capabilities);
capabilities = NULL;
}
if (type) {
(*env)->DeleteLocalRef(env, type);
type = NULL;
} }
av_freep(&supported_type); av_freep(&supported_type);
if (found_codec) {
break;
}
av_freep(&name);
} }
done_with_info:
if (info) {
(*env)->DeleteLocalRef(env, info); (*env)->DeleteLocalRef(env, info);
info = NULL; info = NULL;
}
if (types) {
(*env)->DeleteLocalRef(env, types); (*env)->DeleteLocalRef(env, types);
types = NULL; types = NULL;
}
if (name) if (found_codec) {
break; break;
} }
} }
...@@ -432,12 +555,8 @@ done: ...@@ -432,12 +555,8 @@ done:
(*env)->DeleteLocalRef(env, codec); (*env)->DeleteLocalRef(env, codec);
} }
if (key) { if (codec_name) {
(*env)->DeleteLocalRef(env, key); (*env)->DeleteLocalRef(env, codec_name);
}
if (tmp) {
(*env)->DeleteLocalRef(env, tmp);
} }
if (info) { if (info) {
...@@ -452,6 +571,18 @@ done: ...@@ -452,6 +571,18 @@ done:
(*env)->DeleteLocalRef(env, types); (*env)->DeleteLocalRef(env, types);
} }
if (capabilities) {
(*env)->DeleteLocalRef(env, capabilities);
}
if (profile_level) {
(*env)->DeleteLocalRef(env, profile_level);
}
if (profile_levels) {
(*env)->DeleteLocalRef(env, profile_levels);
}
av_freep(&supported_type); av_freep(&supported_type);
ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx); ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx);
...@@ -459,6 +590,10 @@ done: ...@@ -459,6 +590,10 @@ done:
JNI_DETACH_ENV(attached, log_ctx); JNI_DETACH_ENV(attached, log_ctx);
if (!found_codec) {
av_freep(&name);
}
return name; return name;
} }
......
...@@ -52,7 +52,9 @@ ...@@ -52,7 +52,9 @@
* *
*/ */
char *ff_AMediaCodecList_getCodecNameByType(const char *mime, void *log_ctx); int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx);
char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx);
struct FFAMediaFormat; struct FFAMediaFormat;
typedef struct FFAMediaFormat FFAMediaFormat; typedef struct FFAMediaFormat FFAMediaFormat;
......
...@@ -308,10 +308,16 @@ int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, ...@@ -308,10 +308,16 @@ int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s,
{ {
int ret = 0; int ret = 0;
int status; int status;
int profile;
s->first_buffer_at = av_gettime(); s->first_buffer_at = av_gettime();
s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, avctx); profile = ff_AMediaCodecProfile_getProfileFromAVCodecContext(avctx);
if (profile < 0) {
av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile");
}
s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
if (!s->codec_name) { if (!s->codec_name) {
ret = AVERROR_EXTERNAL; ret = AVERROR_EXTERNAL;
goto fail; goto fail;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment