From patchwork Fri Feb 24 09:00:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Tomas_H=C3=A4rdin?= X-Patchwork-Id: 40491 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp1074940pzk; Fri, 24 Feb 2023 01:01:02 -0800 (PST) X-Google-Smtp-Source: AK7set/P1Sk3GmcPX6UXNsZio3I/DDTyBSNrybEnjqVPsrUsSKbXEHG6e8EUZOj/1QWM4oOLbtGf X-Received: by 2002:a05:6402:48e:b0:4af:5aa1:6646 with SMTP id k14-20020a056402048e00b004af5aa16646mr9753157edv.14.1677229261727; Fri, 24 Feb 2023 01:01:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229261; cv=none; d=google.com; s=arc-20160816; b=rHyEOx9dOD5ct2YU/qkpd8NzxLiKWUCBpOjL0voTF6BYTRZ+rgSM2TAmzglUGoV2pX dvvwEk3QaIc9zJDCDk2kYYoOXW8UTK7rQNrN3sMbeRyxRHSeJDpD0RVS4IadFfBQg18e rEUIot5/VyMJ3zZSZrUFdwnTeGzp+ACwd2DyWtVCcRtl/L0GzQ4k87oOXCLh2KBMcxqh PCyJw4194gvSSsVwFF/thloVmB95Ww/jXU8DjthcMIeyimY56QxEwO+67JPWl1kftCbw tG+6n4MU9hw++sf946NUq5VynYqxAfB+UzM4MUtSLEc5b6efSMgoibhZY2/HcthC8kib 9qwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:user-agent:references:in-reply-to:date:to:from :message-id:delivered-to; bh=unkXuAb1TENP1E2fYvPiv13yA/3EAdQY97J9VbR2NyM=; b=jr0aclFHNqRLi7eF2zAmhG9P2jwepQahiQBxxYqvwaUABL2zkvT832wGkJXuTa72Zy pMhftlVky0YGvmZQuqES8revyAU1PwTNbM9rUkdcra0xczBIiYEITAVnV7ZN1a/jp4mv PGLcKBIGmdpSCQDUYQxTt4V0XaUc0BKnGgFMrnTH0cRX1R4k0XHVVoUkTgd8ajjpmxEn s2ULpxlQ5Jv9m70Dtl4AS0ISnMhi0Uv2iz2CRBQXyJTT/w48BFsCCdKOB5y6AQQ5ZwH9 vSrWRFR4AEz337Q9PzWc6tRhK4Ctbehpb8gB2MA99m6Y4oNyC9IKM2bF2wxaIhPrGnBa Gx3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id n3-20020aa7d043000000b004acb5db1ff3si13708003edo.576.2023.02.24.01.01.01; Fri, 24 Feb 2023 01:01:01 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EEE3968C10D; Fri, 24 Feb 2023 11:00:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.frobbit.se (mail.frobbit.se [85.30.129.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5AC9368C11C for ; Fri, 24 Feb 2023 11:00:54 +0200 (EET) Received: from [10.10.150.69] (1346516434.ip.84grams.net [134.65.164.34]) by mail.frobbit.se (Postfix) with ESMTPSA id 4CFF621ED0 for ; Fri, 24 Feb 2023 10:00:54 +0100 (CET) Message-ID: From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Fri, 24 Feb 2023 10:00:53 +0100 In-Reply-To: References: User-Agent: Evolution 3.38.3-1+deb11u1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/4] lavc/mediacodec_wrapper: Add function for probing supported color formats X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: i2HaikDb5Tko From 557f23650726af7f4881d29411de02f9f8bc78f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= 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(+) diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c index 82eead2833..9d2560205f 100644 --- a/libavcodec/mediacodec_wrapper.c +++ b/libavcodec/mediacodec_wrapper.c @@ -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; diff --git a/libavcodec/mediacodec_wrapper.h b/libavcodec/mediacodec_wrapper.h index 1b81e6db84..7ab32c4dc7 100644 --- a/libavcodec/mediacodec_wrapper.h +++ b/libavcodec/mediacodec_wrapper.h @@ -2,6 +2,7 @@ * Android MediaCodec Wrapper * * Copyright (c) 2015-2016 Matthieu Bouron + * 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