From patchwork Fri Feb 24 08:59:37 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: 40489 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp1074122pzk; Fri, 24 Feb 2023 00:59:51 -0800 (PST) X-Google-Smtp-Source: AK7set+0yw/oj+Jo5BGUCMXzK5NXnBOY6GU8NouOAyyqsgIj24BVryiSb11+5flE38qgvrP1tShV X-Received: by 2002:a17:907:2cd7:b0:8de:e66a:ece9 with SMTP id hg23-20020a1709072cd700b008dee66aece9mr11254817ejc.24.1677229190972; Fri, 24 Feb 2023 00:59:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229190; cv=none; d=google.com; s=arc-20160816; b=tca8Dx0Q/9QXSez2Zkk86DnKcjbMiML32eHvVWi5GzgRfR5cp4xDAA7/JwenGuDngf jNCa7Dwnm3YWwMMrkFKTIFUx3M9qF22cgs3jMAWwuDTMD1+FRIk+xjKyw+K0bgvY8t8/ MPZMHoXnofgDupoZUWDoLyZkT01iKoQSncCdGeXIMibwrsz3Ky+xwBPQ0G5i/3/3eQAw akbHlFqTqw91ytHdvfFnWYz2dCuAUnDxECnvU/JE0U/PJyVOXHx+FpycVHtR4nthfNgp QN5p+LcDE9v5nWW+vvOqV4ruNAgrsaao7joVgQJp4YFCXWBGr+dclwwHDhl/XMfU6IEP 0Bhg== 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:date:to:from:message-id:delivered-to; bh=PWmI66qNh8TlZzF/2lTsO/td89gFLGPxH5kFREZBpv4=; b=WsssfqXANVqKPXfhVgCKFkq0ycocT0Nj8FhfiSX+Sa1ow934/fmMKomwck+6+udC23 oZDLxLOM9iJH6kUQcf73JeD68IibGhTYBqZrCQiLPy8oe3oSebB4fODnukhqDKUjMuGs KSvb7WrZW7P1ec8+NVvIE1O5LVKfbg445o2lR4vgfS+kJWv5MtqMNOVkDCludpKHzf2y 1gbA77iSoQ2a39rshLjZguw4U5cJNxeu/WAC3dkIR27F1209jtTNbrNn3GSiMWYK9GYy 01IzQoLvDVGksSl9+W3QgsNPdeYhCEd5Ymz+FhsfVFfaCRb7bISI5C4vHeeT019JA0J7 Koug== 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 if9-20020a170906df4900b008c8f5756068si15661420ejc.766.2023.02.24.00.59.50; Fri, 24 Feb 2023 00:59:50 -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 B421468C10C; Fri, 24 Feb 2023 10:59:46 +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 B19D368BED2 for ; Fri, 24 Feb 2023 10:59:39 +0200 (EET) Received: from [10.10.150.69] (1346516434.ip.84grams.net [134.65.164.34]) by mail.frobbit.se (Postfix) with ESMTPSA id 8301221BA9 for ; Fri, 24 Feb 2023 09:59:38 +0100 (CET) Message-ID: From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Fri, 24 Feb 2023 09:59:37 +0100 User-Agent: Evolution 3.38.3-1+deb11u1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] lavc/mediacodecenc: Add pix2color_fmt() and color2pix_fmt() 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: Mz/plo9wepSE From 7374dab21cb37dc14d2481c72d333b435e23a76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Tue, 10 Jan 2023 20:37:18 +0100 Subject: [PATCH 1/4] lavc/mediacodecenc: Add pix2color_fmt() and color2pix_fmt() This patch has been released by Epic Games' legal department. --- libavcodec/mediacodecenc.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c index a92a8dc5a9..03c80cbf99 100644 --- a/libavcodec/mediacodecenc.c +++ b/libavcodec/mediacodecenc.c @@ -2,6 +2,7 @@ * Android MediaCodec encoders * * Copyright (c) 2022 Zhao Zhili + * Modifications by Epic Games, Inc., 2023. * * This file is part of FFmpeg. * @@ -110,6 +111,26 @@ static void mediacodec_output_format(AVCodecContext *avctx) ff_AMediaFormat_delete(out_format); } +static int pix2color_fmt(AVCodecContext *avctx, enum AVPixelFormat pix_fmt) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) { + if (pix_fmt == color_formats[i].pix_fmt) { + return color_formats[i].color_format; + } + } + av_assert0(0); +} + +static enum AVPixelFormat color2pix_fmt(AVCodecContext *avctx, int color_format) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) { + if (color_format == color_formats[i].color_format) { + return color_formats[i].pix_fmt; + } + } + av_assert0(0); +} + static int mediacodec_init_bsf(AVCodecContext *avctx) { MediaCodecEncContext *s = avctx->priv_data; @@ -235,13 +256,7 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) } } - for (int i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) { - if (avctx->pix_fmt == color_formats[i].pix_fmt) { - ff_AMediaFormat_setInt32(format, "color-format", - color_formats[i].color_format); - break; - } - } + ff_AMediaFormat_setInt32(format, "color-format", pix2color_fmt(avctx, avctx->pix_fmt)); if (avctx->bit_rate) ff_AMediaFormat_setInt32(format, "bitrate", avctx->bit_rate); -- 2.30.2 From patchwork Fri Feb 24 09:00:25 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: 40490 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp1074615pzk; Fri, 24 Feb 2023 01:00:38 -0800 (PST) X-Google-Smtp-Source: AK7set9rhuUAiVi+cs84E5lWNv/B/HXE++n75T8D4HClDJnA2AHk0vBqXWW5mqcSSYK3xo/zzbQp X-Received: by 2002:a17:906:24d3:b0:8c8:bd1:1d7e with SMTP id f19-20020a17090624d300b008c80bd11d7emr17592150ejb.18.1677229237000; Fri, 24 Feb 2023 01:00:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229236; cv=none; d=google.com; s=arc-20160816; b=IYnNvW/p4GG4SAQdfWrxZTOHm8HUFA96eeqHwMI4ZVcpXwdimoZdx72GyMCtFI0MN9 Vi8mv2S5x+WeE86voYd0M3oSeaDl7GI1la9NviY/bSUlCetLNys6gxd9R+x0r/HOyMON MtaGh4gQHosblvcqCJVyUsm7snEbNWYApdHoYS7OzGfSYBL52ISiJ9hF+jMm/gD/hmGA J7RS8bTWyBfCNbmKYOAgXoV5KYlK4Ca00Hl9XFgpUGSo4rhI/Lpmjzk5fRcbGf+NwJlH rfrwtT66Qy6zjcHVK2zCjidoCxeMAMFjE7WMPc2vDy9ElMrUZQ5y4Z/fQvopfAwaoELW SfLQ== 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=PpsijQsH3QEWyM7AqmEHDLMymiEvFv6z6wg/f/XgKWU=; b=GUJcAD7EflbgZ5GiWNp2Y7lPQLsSstY3A3LKNCK4Nv2LeRIPXH7MQB9L5JPSe0uiYW ZthB1axHyhoiJp8axXF8UbOGarHdrZQpTNqXxQZGdA9a3YKvQNJmJMGNH3DVEZlPkNQS Ped3Dxiqt3Yu+A6sX8vgm8jRdrhysq9reLhdzce7+KP1eo5UdeAK7tPjPTkIeXw2KJvE AJ2kFTRZzxV7MB11E2Ydnzp8+TO7x2njYXUwYV3dxYJAmsURiYpYytpUoUbJa369932l Bmr0rY7o0h+03CwUnLQzHblrDr9YYIGvopHp8lOI5Opn+ItqJBWnofPkkcRtGCBo4k1F au6Q== 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 12-20020a170906018c00b008ddf3c18313si11593414ejb.729.2023.02.24.01.00.36; Fri, 24 Feb 2023 01:00:36 -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 D228868C118; Fri, 24 Feb 2023 11:00:33 +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 5AD3368C100 for ; Fri, 24 Feb 2023 11:00:27 +0200 (EET) Received: from [10.10.150.69] (1346516434.ip.84grams.net [134.65.164.34]) by mail.frobbit.se (Postfix) with ESMTPSA id E8BE9207DD for ; Fri, 24 Feb 2023 10:00:26 +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:25 +0100 In-Reply-To: References: User-Agent: Evolution 3.38.3-1+deb11u1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] lavc/mediacodec_wrapper: Refactor ff_AMediaCodecList_getCodecNameByType() 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: Zb8JS71v/069 From 9d4defbc92e4976f7d652149a215e2c165239e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Wed, 11 Jan 2023 21:49:30 +0100 Subject: [PATCH 2/4] lavc/mediacodec_wrapper: Refactor ff_AMediaCodecList_getCodecNameByType() A lot shorter, nested variables and keeps going in case of exceptions. Makes use of C99 declarations. This patch has been released by Epic Games' legal department. --- libavcodec/mediacodec_wrapper.c | 162 +++++++++----------------------- 1 file changed, 45 insertions(+), 117 deletions(-) diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c index 34ec2134aa..82eead2833 100644 --- a/libavcodec/mediacodec_wrapper.c +++ b/libavcodec/mediacodec_wrapper.c @@ -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. * @@ -365,26 +366,13 @@ int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx) char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx) { int ret; - int i; - int codec_count; int found_codec = 0; char *name = NULL; - char *supported_type = NULL; JNIEnv *env = NULL; struct JNIAMediaCodecListFields jfields = { 0 }; struct JNIAMediaFormatFields mediaformat_jfields = { 0 }; - jobject codec_name = NULL; - - jobject info = NULL; - jobject type = NULL; - jobjectArray types = NULL; - - jobject capabilities = NULL; - jobject profile_level = NULL; - jobjectArray profile_levels = NULL; - JNI_GET_ENV_OR_RETURN(env, log_ctx, NULL); if ((ret = ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx)) < 0) { @@ -395,29 +383,26 @@ char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int e goto done; } - codec_count = (*env)->CallStaticIntMethod(env, jfields.mediacodec_list_class, jfields.get_codec_count_id); + 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(i = 0; i < codec_count; i++) { - int j; - int type_count; - int is_encoder; - - info = (*env)->CallStaticObjectMethod(env, jfields.mediacodec_list_class, jfields.get_codec_info_at_id, i); + 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) { - goto done; + continue; } - types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types_id); + jobject codec_name = NULL; + jobjectArray types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types_id); if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; + goto done_with_info; } - is_encoder = (*env)->CallBooleanMethod(env, info, jfields.is_encoder_id); + int is_encoder = (*env)->CallBooleanMethod(env, info, jfields.is_encoder_id); if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; + goto done_with_info; } if (is_encoder != encoder) { @@ -427,7 +412,7 @@ char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int e if (jfields.is_software_only_id) { int is_software_only = (*env)->CallBooleanMethod(env, info, jfields.is_software_only_id); if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; + goto done_with_info; } if (is_software_only) { @@ -437,17 +422,12 @@ char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int e codec_name = (*env)->CallObjectMethod(env, info, jfields.get_name_id); if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; + goto done_with_info; } name = ff_jni_jstring_to_utf_chars(env, codec_name, log_ctx); if (!name) { - goto done; - } - - if (codec_name) { - (*env)->DeleteLocalRef(env, codec_name); - codec_name = NULL; + goto done_with_info; } /* Skip software decoders */ @@ -459,141 +439,89 @@ char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int e goto done_with_info; } - type_count = (*env)->GetArrayLength(env, types); - for (j = 0; j < type_count; j++) { - int k; - int profile_count; - - type = (*env)->GetObjectArrayElement(env, types, j); + int type_count = (*env)->GetArrayLength(env, types); + for (int j = 0; j < type_count && !found_codec; j++) { + jobject type = (*env)->GetObjectArrayElement(env, types, j); if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; + continue; } - supported_type = ff_jni_jstring_to_utf_chars(env, type, log_ctx); + jobject capabilities = NULL; + jobjectArray profile_levels = NULL; + char *supported_type = ff_jni_jstring_to_utf_chars(env, type, log_ctx); if (!supported_type) { - goto done; + goto done_with_type; } - if (av_strcasecmp(supported_type, mime)) { + int strcasecmp_ret = av_strcasecmp(supported_type, mime); + av_freep(&supported_type); + if (strcasecmp_ret) { goto done_with_type; } capabilities = (*env)->CallObjectMethod(env, info, jfields.get_codec_capabilities_id, type); if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; + goto done_with_type; } profile_levels = (*env)->GetObjectField(env, capabilities, jfields.profile_levels_id); if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; + goto done_with_type; } - profile_count = (*env)->GetArrayLength(env, profile_levels); - if (!profile_count) { + // match profile if desired by user (profile >= 0) and codec has non-empty profileLevels + int profile_count = (*env)->GetArrayLength(env, profile_levels); + if (!profile_count || profile < 0) { found_codec = 1; - } - for (k = 0; k < profile_count; k++) { - int supported_profile = 0; - - if (profile < 0) { - found_codec = 1; - break; - } - - profile_level = (*env)->GetObjectArrayElement(env, profile_levels, k); - if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; - } - - supported_profile = (*env)->GetIntField(env, profile_level, jfields.profile_id); - if (ff_jni_exception_check(env, 1, log_ctx) < 0) { - goto done; - } - - found_codec = profile == supported_profile; + } else { + for (int k = 0; k < profile_count && !found_codec; k++) { + jobject profile_level = (*env)->GetObjectArrayElement(env, profile_levels, k); + if (ff_jni_exception_check(env, 1, log_ctx) < 0) { + continue; + } + + int supported_profile = (*env)->GetIntField(env, profile_level, jfields.profile_id); + if (ff_jni_exception_check(env, 1, log_ctx) >= 0) { + found_codec = profile == supported_profile; + } - if (profile_level) { (*env)->DeleteLocalRef(env, profile_level); - profile_level = NULL; - } - - if (found_codec) { - break; } } done_with_type: if (profile_levels) { (*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); - - if (found_codec) { - break; } } done_with_info: if (info) { (*env)->DeleteLocalRef(env, info); - info = NULL; } if (types) { (*env)->DeleteLocalRef(env, types); - types = NULL; } - if (found_codec) { - break; + if (codec_name) { + (*env)->DeleteLocalRef(env, codec_name); } - av_freep(&name); + if (!found_codec) { + av_freep(&name); + } } done: - if (codec_name) { - (*env)->DeleteLocalRef(env, codec_name); - } - - if (info) { - (*env)->DeleteLocalRef(env, info); - } - - if (type) { - (*env)->DeleteLocalRef(env, type); - } - - if (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); - 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); -- 2.30.2 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 From patchwork Fri Feb 24 09:03:44 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: 40488 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp1076759pzk; Fri, 24 Feb 2023 01:03:56 -0800 (PST) X-Google-Smtp-Source: AK7set+or59c2XFPnbIfh3OvMz+uqBIen0Vi69rXDAawkZmpBNw6+9rzIU9KYSFebKg5EPWSvaRN X-Received: by 2002:a17:906:198f:b0:8b1:77bf:5b9f with SMTP id g15-20020a170906198f00b008b177bf5b9fmr23940288ejd.13.1677229436278; Fri, 24 Feb 2023 01:03:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229436; cv=none; d=google.com; s=arc-20160816; b=mQxCcwtTKCAoa2V4inrO4POK/Crmh7Q+/HYpEdM73YBLGoXKxtAZgxRl8pc005yqGl y49oG29GttIGh4WxIcKqMKDpdMBHc3H8pYAGqb1cjGtAtantNTqswH/hxKWs/zGFBre2 K8lQjedUJm6sTU2nybtyn+zQf9yo0Dr6QbiZqhmSzSLbsSQliM6xh7TzVfYwwjZSJWUN D7LKpaZyhvFg6Bkdi4GLmMavnZvv0KzLpfgyVKAGOMIPf4AbOGMJV0hgsvcOf58WdmC7 gePrrBe3Ok27LU8Ckb7I2H1WZxUYbNc0JZKyiFTWa3bSeKa67v3Gwc40u0TClPx2SHgo hR0w== 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=oVWBYmAZnOxNs0x0KVj3GzHdFOHUeaCZPtSxax7SXeI=; b=VHC+2AOD+FbXQhef2iPctGTYp9dHOsDR0XbJCihyAaJ6pZn8Xr3XIDpWeSa84wkhvU 9MYzdhwbSathPfxcAlAhNvBL8MaL/Rinpgi8yTJ6D/qKMHNEIyZLuOx0HMl4FOqAz5V8 ZL03d5q0yD9TY0K4jndPPqJwB/LL18H839TpNk98qcjTG8BVW+F9/rsleiTqsQAAAnKd MsvpNIs931Yl4otgvHFs4pYA24098ts1rKrbNvHRhNhg6QqC8JdiDZMvtPmorK/eqikk iSgsPP0K0maYD2g9z8sjmMl+QFv4kVCTGXp+amBY2nV27o/0f0bXj5LnZjcdD7puxJuX K2Dg== 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 ew24-20020a170907951800b008d999499f63si13454143ejc.83.2023.02.24.01.03.55; Fri, 24 Feb 2023 01:03:56 -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 475C568C11C; Fri, 24 Feb 2023 11:03:53 +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 73F86689C24 for ; Fri, 24 Feb 2023 11:03:46 +0200 (EET) Received: from [10.10.150.69] (1346516434.ip.84grams.net [134.65.164.34]) by mail.frobbit.se (Postfix) with ESMTPSA id F36FC21F10 for ; Fri, 24 Feb 2023 10:03:45 +0100 (CET) Message-ID: <015df2df4818b87922ab0e358d1949d50698a71b.camel@haerdin.se> From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Fri, 24 Feb 2023 10:03:44 +0100 In-Reply-To: References: User-Agent: Evolution 3.38.3-1+deb11u1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] lavc/mediacodecenc: List supported pixel formats on configuration error 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: yesIdrbAHU5x This makes the situation a bit better for users, even if we can't reliably probe pixel formats in init_static_data. I decided to keep this patchset separate from that probing, since printing pixel formats this way should be reliable. /Tomas From eed0ed202bf8c496ab945b32e2e7fc7cad062c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Tue, 10 Jan 2023 20:38:56 +0100 Subject: [PATCH 4/4] lavc/mediacodecenc: List supported pixel formats on configuration error This patch has been released by Epic Games' legal department. --- libavcodec/mediacodecenc.c | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c index 03c80cbf99..1b8a2837c4 100644 --- a/libavcodec/mediacodecenc.c +++ b/libavcodec/mediacodecenc.c @@ -97,6 +97,12 @@ static const enum AVPixelFormat avc_pix_fmts[] = { AV_PIX_FMT_NONE }; +static const int in_formats[] = { + COLOR_FormatYUV420Planar, + COLOR_FormatYUV420SemiPlanar, + COLOR_FormatSurface, +}; + static void mediacodec_output_format(AVCodecContext *avctx) { MediaCodecEncContext *s = avctx->priv_data; @@ -131,6 +137,45 @@ static enum AVPixelFormat color2pix_fmt(AVCodecContext *avctx, int color_format) av_assert0(0); } +// list pixel formats if the user tried to use one that isn't supported on this device +static void list_pix_fmts(AVCodecContext *avctx, const char *mime) +{ + MediaCodecEncContext *s = avctx->priv_data; + int out_formats[FF_ARRAY_ELEMS(in_formats)], n; + char *name = ff_AMediaCodec_getName(s->codec); + + if (!name) { + // API level likely below 28 + return; + } + + if ((n = ff_AMediaCodec_color_formats_intersect(name, mime, in_formats, + FF_ARRAY_ELEMS(in_formats), + out_formats, avctx)) < 0) { + goto done; + } + + for (int i = 0; i < n; i++) { + if (color2pix_fmt(avctx, out_formats[i]) == avctx->pix_fmt) { + // user specified a pixel format that is actually supported, + // no need to print anything + goto done; + } + } + + AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); + av_log(avctx, AV_LOG_ERROR, "pixel format %s not supported by MediaCodec %s\n", desc->name, name); + av_log(avctx, AV_LOG_INFO, "supported formats are:"); + for (int i = 0; i < n; i++) { + desc = av_pix_fmt_desc_get(color2pix_fmt(avctx, out_formats[i])); + av_log(avctx, AV_LOG_INFO, " %s", desc->name); + } + av_log(avctx, AV_LOG_INFO, "\n"); + +done: + av_free(name); +} + static int mediacodec_init_bsf(AVCodecContext *avctx) { MediaCodecEncContext *s = avctx->priv_data; @@ -308,6 +353,7 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret); if (ret) { av_log(avctx, AV_LOG_ERROR, "MediaCodec configure failed, %s\n", av_err2str(ret)); + list_pix_fmts(avctx, codec_mime); goto bailout; } -- 2.30.2