From 557f23650726af7f4881d29411de02f9f8bc78f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Wed, 11 Jan 2023 22:25:39 +0100
Subject: [PATCH 3/4] lavc/mediacodec_wrapper: Add function for probing
supported color formats
This patch has been released by Epic Games' legal department.
---
libavcodec/mediacodec_wrapper.c | 112 ++++++++++++++++++++++++++++++++
libavcodec/mediacodec_wrapper.h | 15 +++++
2 files changed, 127 insertions(+)
@@ -532,6 +532,118 @@ done:
return name;
}
+int ff_AMediaCodec_color_formats_intersect(const char *codec_name, const char *mime,
+ const int *in_formats, int nin_formats,
+ int *out_formats, void *log_ctx)
+{
+ int ret, found_codec = 0;
+ jstring jmime = NULL;
+ JNIEnv *env = NULL;
+ struct JNIAMediaCodecListFields jfields = { 0 };
+ struct JNIAMediaFormatFields mediaformat_jfields = { 0 };
+
+ JNI_GET_ENV_OR_RETURN(env, log_ctx, -1);
+
+ if ((ret = ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx)) < 0) {
+ goto done;
+ }
+
+ if ((ret = ff_jni_init_jfields(env, &mediaformat_jfields, jni_amediaformat_mapping, 0, log_ctx)) < 0) {
+ goto done;
+ }
+
+ // convert const char* to java.lang.String
+ jmime = ff_jni_utf_chars_to_jstring(env, mime, log_ctx);
+ if (!jmime) {
+ goto done;
+ }
+
+ int 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 (int i = 0; i < codec_count && !found_codec; i++) {
+ jobject 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) {
+ continue;
+ }
+
+ char *name = NULL;
+ jboolean is_copy;
+ jintArray color_formats = NULL;
+ jobject capabilities = NULL;
+ jobject jcodec_name = (*env)->CallObjectMethod(env, info, jfields.get_name_id);
+ if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
+ goto done_with_info;
+ }
+
+ if (!(name = ff_jni_jstring_to_utf_chars(env, jcodec_name, log_ctx))) {
+ goto done_with_info;
+ }
+
+ if (strcmp(name, codec_name)) {
+ goto done_with_info;
+ }
+
+ capabilities = (*env)->CallObjectMethod(env, info, jfields.get_codec_capabilities_id, jmime);
+ if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
+ goto done_with_info;
+ }
+
+ color_formats = (*env)->GetObjectField(env, capabilities, jfields.color_formats_id);
+ if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
+ goto done_with_info;
+ }
+
+ int color_count = (*env)->GetArrayLength(env, color_formats);
+ int *elems = (*env)->GetIntArrayElements(env, color_formats, &is_copy);
+ ret = 0;
+ found_codec = 1;
+
+ // out_formats = intersect(in_formats, elems)
+ for (int j = 0; j < nin_formats; j++) {
+ for (int k = 0; k < color_count; k++) {
+ if (elems[k] == in_formats[j]) {
+ out_formats[ret++] = in_formats[j];
+ break;
+ }
+ }
+ }
+
+ (*env)->ReleaseIntArrayElements(env, color_formats, elems, JNI_ABORT);
+
+done_with_info:
+ if (color_formats) {
+ (*env)->DeleteLocalRef(env, color_formats);
+ }
+
+ if (capabilities) {
+ (*env)->DeleteLocalRef(env, capabilities);
+ }
+
+ if (jcodec_name) {
+ (*env)->DeleteLocalRef(env, jcodec_name);
+ }
+
+ if (info) {
+ (*env)->DeleteLocalRef(env, info);
+ }
+
+ av_freep(&name);
+ }
+
+done:
+ if (jmime) {
+ (*env)->DeleteLocalRef(env, jmime);
+ }
+
+ ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx);
+ ff_jni_reset_jfields(env, &mediaformat_jfields, jni_amediaformat_mapping, 0, log_ctx);
+
+ return found_codec ? ret : -1;
+}
+
static FFAMediaFormat *mediaformat_jni_new(void)
{
JNIEnv *env = NULL;
@@ -2,6 +2,7 @@
* Android MediaCodec Wrapper
*
* Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
+ * Modifications by Epic Games, Inc., 2023.
*
* This file is part of FFmpeg.
*
@@ -230,6 +231,20 @@ FFAMediaCodec* ff_AMediaCodec_createCodecByName(const char *name, int ndk);
FFAMediaCodec* ff_AMediaCodec_createDecoderByType(const char *mime_type, int ndk);
FFAMediaCodec* ff_AMediaCodec_createEncoderByType(const char *mime_type, int ndk);
+/**
+ * Intersects the given list of color formats with the formats actually supported by the named codec.
+ * @param[in] codec_name Name of the codec
+ * @param[in] mime MIME format. Typically "video/avc" or "video/hevc"
+ * @param[in] in_formats Array of input color formats
+ * @param[in] nin_formats Number of elements in in_formats
+ * @param[out] out_formats Computed intersection of in_formats and supported formats
+ * @param[in] log_ctx Logging context
+ * @return Size of out_formats or negative in case of error
+ */
+int ff_AMediaCodec_color_formats_intersect(const char *codec_name, const char *mime,
+ const int *in_formats, int nin_formats,
+ int *out_formats, void *log_ctx);
+
static inline int ff_AMediaCodec_configure(FFAMediaCodec *codec,
const FFAMediaFormat *format,
FFANativeWindow *surface,
--
2.30.2