From patchwork Mon Oct 24 03:16:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 38945 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp1998374pzb; Sun, 23 Oct 2022 20:19:46 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7zrY7gcimlSAb0XXNw7bIh0wWIRmwLBfLp3BJ40Vna3lIqlBa6FLJfO6e3C97kkby6zLX1 X-Received: by 2002:a17:906:cc0f:b0:7a0:b91c:855f with SMTP id ml15-20020a170906cc0f00b007a0b91c855fmr8872328ejb.26.1666581586604; Sun, 23 Oct 2022 20:19:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666581586; cv=none; d=google.com; s=arc-20160816; b=VDgNFo7I2cbby1VPQZcTksiqj1Dfm8lKIR+FDdPrtkYen/sfJkmUdGm8SqgBj4Bx4G 157OXSHzNLWtsHjxkVRb/PwaSfqB0RnQ3G4LLaE+Z8vY0rawEp5MzpdOUmSITVizjBV1 TEGAUPLCz3J1aprQnCQ4BOK+UwfTEs+TgC2Hm0md09PIZJYShpb7O4a/s4T0BHfw4+Cj uxSdv6YO4JxaSKFwEZiOcpRQITajXARapKLPCCjF/V2y16JZV5O8jBjc/l5ND/fbxuQZ lQCLboAjW8sAmdHJ9op3XGaQmDSbgjO9s1iV8TM75aCbaCO3JfdvP499yGzIB/Z4tiF0 3LBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to:date :to:from:message-id:dkim-signature:delivered-to; bh=ifySuZb+cijYNaQBsrh6fWsJKongNm7crU+1pa0mqiE=; b=IlOCy4jEKbDW4VSlhVFuymRsDSHdCWE7Y1IBLIT+Y2g2DJjBjZ3c7Pz1hixopGPQ6k SnMOmcKAnuFmQuf28w6YDMisAF9oF+QtwqCF7HbjXWDWMxo9iCMhrRE3rsIg9Fe/BZs8 WGoQZpGAG2HdwjHnQy5sHMs/3+Z6g3yGOTHfGUXZb5AIwjrR8vf1WxIC3CQsvtDbnnww HLEUA3O6kuSkBv5J6t3hRsMlLz3+fXXB7l6kFpe4frx+afFtBupPxlHoH3llomQWyTVS VQFY+MG1s9kmvNFjGaIpLqzuyWbwmuwbx7sYcVJltiialTqymvW7vno8YqfNge33R1cb qTdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b="jN8K/88T"; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i8-20020a05640242c800b0045945eed10asi30634002edc.5.2022.10.23.20.19.46; Sun, 23 Oct 2022 20:19:46 -0700 (PDT) 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; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b="jN8K/88T"; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 38C1168BDA7; Mon, 24 Oct 2022 06:19:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-205.mail.qq.com (out203-205-221-205.mail.qq.com [203.205.221.205]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3BF9568BD2D for ; Mon, 24 Oct 2022 06:17:23 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1666581440; bh=5WT92i5iG9ElscGVw4V6WlitzhgIJkqZg0r2ou3Qick=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jN8K/88Tcw5BWoiD7I87QQKWWR0Qvdh+WCTUUB6yCfDwkRqrOMgYj2nYFp35DPgc3 NQo8/RxCzjEroVZTEA1clFss0YUcEO8SrC5UR3AntY+cTR1EKuTPJA4tf+fg/IAN3L GKK63ueFS+BW3QrEE/6LHtvc+QUjeh5tKdUpDN00= Received: from localhost.localdomain ([59.37.125.102]) by newxmesmtplogicsvrsza31.qq.com (NewEsmtp) with SMTP id 44A14270; Mon, 24 Oct 2022 11:17:10 +0800 X-QQ-mid: xmsmtpt1666581437t4yhbnlc2 Message-ID: X-QQ-XMAILINFO: M/NR0wiIuy70K/iGIwS7Dp1Kbu7Oc6nEdvuVr/nl8LLE02e+YQFYj6/Tx2KJYQ UssFaMDxrCHKGnlYriKCkrxsmUw3lLF9vAZG9GH3XKC92IBAKMcrHaXEI+dmCFy9b3JP0XXiUfM/ 38WLBa1nyYXYIx3oTZWpE2KVkeTfxVOAXmE7vsPmT7J6cj91QLJ81lAbqXEi9kABHS/F954ARKmU 6no9QWOV8kKQLSXiOk9Kx3vd964BT/Scf8FmXDr0otZD8edU139AE7Qzl4XDM9y26wpq4R5dcjTD /TWcSivhfoKVolZFLyFW8GoF6caHI7DR4hFDqqZFLqcQdCSh200DqfNLE3Qojo5EwddsywC73crE Nax5VDoltrzUxDVnONx80eBnK/1AuwbGzK5wvGa6fSVI0pmt+23EYj0yCnXb02fsXnq1pIChfOzy yX/HO4Jj4Hk3+Nhg1Pa1tur91oijWHwTFa/2aDpHcOmzvPSjzecsAeceqIEVFc1tjra5ZVcx7hqQ t4SEZDK3e9DoB/8rTRkY7Tz5Mz8S2AncD//J+Can6fN2hov8x7Blm0PAg5EYKoREPnxEUuUNctt0 iyqFFBDg9bLCSxxNo0Qx6tCeLcpgdhm8048eyvg6J3ZlfJSY3ElYeFDHIOBzgwW4l5ZiHtC4oe5N Li4IYksXzRSCHk3w8ePAwTo30O8Tox/WwkIsguOSoCPjK68ARMw/Jph6TjLK/XEMDzgirG/4wQ56 N71r0ZSZ6yKzrCUfbtdp4MhHHGUWsA5xgjQm/hNXj+M/eKsT6+egSomQ7w6TVMCwOU0iGB+cGj2l tAMNrF/rSC2Xke14chmmizK6GY10cTCOmfqsuB2Bhg83ssfMYzBwdVLeqEwPs1Xj7UBeK/xvUSW7 4U5zQZCLPfNEP7iqsd+V/dNinIsV2RfytU6DBMK0PRxOPXFXTjkrjgAuypRNCSrGCFxkFc3Th8jD AtxcoOEcLhXUEDlaGusW97GN/K7iPQ From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Oct 2022 11:16:51 +0800 X-OQ-MSGID: <20221024031658.1026118-6-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221024031658.1026118-1-quinkblack@foxmail.com> References: <20221024031658.1026118-1-quinkblack@foxmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/12] avcodec/mediacodec_wrapper: separate implementation from interface 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 Cc: matthieu.bouron@stupeflix.com, Zhao Zhili , aman@tmm1.net Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 75U44Yc1Wv1T From: Zhao Zhili This is in preparation for NDK media codec wrapper. Signed-off-by: Zhao Zhili --- libavcodec/mediacodec_wrapper.c | 244 +++++++++++++++++++++--------- libavcodec/mediacodec_wrapper.h | 255 +++++++++++++++++++++++++++----- 2 files changed, 394 insertions(+), 105 deletions(-) diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c index 8ffc58e1d8..e0c614680e 100644 --- a/libavcodec/mediacodec_wrapper.c +++ b/libavcodec/mediacodec_wrapper.c @@ -160,12 +160,14 @@ static const AVClass amediaformat_class = { .version = LIBAVUTIL_VERSION_INT, }; -struct FFAMediaFormat { +typedef struct FFAMediaFormatJni { + FFAMediaFormat api; - const AVClass *class; struct JNIAMediaFormatFields jfields; jobject object; -}; +} FFAMediaFormatJni; + +static const FFAMediaFormat media_format_jni; struct JNIAMediaCodecFields { @@ -272,9 +274,8 @@ static const AVClass amediacodec_class = { .version = LIBAVUTIL_VERSION_INT, }; -struct FFAMediaCodec { - - const AVClass *class; +typedef struct FFAMediaCodecJni { + FFAMediaCodec api; struct JNIAMediaCodecFields jfields; @@ -295,7 +296,9 @@ struct FFAMediaCodec { int CONFIGURE_FLAG_ENCODE; int has_get_i_o_buffer; -}; +} FFAMediaCodecJni; + +static const FFAMediaCodec media_codec_jni; #define JNI_GET_ENV_OR_RETURN(env, log_ctx, ret) do { \ (env) = ff_jni_get_env(log_ctx); \ @@ -622,17 +625,17 @@ done: return name; } -FFAMediaFormat *ff_AMediaFormat_new(void) +static FFAMediaFormat *mediaformat_jni_new(void) { JNIEnv *env = NULL; - FFAMediaFormat *format = NULL; + FFAMediaFormatJni *format = NULL; jobject object = NULL; - format = av_mallocz(sizeof(FFAMediaFormat)); + format = av_mallocz(sizeof(*format)); if (!format) { return NULL; } - format->class = &amediaformat_class; + format->api = media_format_jni; env = ff_jni_get_env(format); if (!env) { @@ -664,19 +667,19 @@ fail: av_freep(&format); } - return format; + return (FFAMediaFormat *)format; } -static FFAMediaFormat *ff_AMediaFormat_newFromObject(void *object) +static FFAMediaFormat *mediaformat_jni_newFromObject(void *object) { JNIEnv *env = NULL; - FFAMediaFormat *format = NULL; + FFAMediaFormatJni *format = NULL; - format = av_mallocz(sizeof(FFAMediaFormat)); + format = av_mallocz(sizeof(*format)); if (!format) { return NULL; } - format->class = &amediaformat_class; + format->api = media_format_jni; env = ff_jni_get_env(format); if (!env) { @@ -693,7 +696,7 @@ static FFAMediaFormat *ff_AMediaFormat_newFromObject(void *object) goto fail; } - return format; + return (FFAMediaFormat *)format; fail: ff_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format); @@ -702,10 +705,10 @@ fail: return NULL; } -int ff_AMediaFormat_delete(FFAMediaFormat* format) +static int mediaformat_jni_delete(FFAMediaFormat* ctx) { int ret = 0; - + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; JNIEnv *env = NULL; if (!format) { @@ -724,10 +727,10 @@ int ff_AMediaFormat_delete(FFAMediaFormat* format) return ret; } -char* ff_AMediaFormat_toString(FFAMediaFormat* format) +static char* mediaformat_jni_toString(FFAMediaFormat* ctx) { char *ret = NULL; - + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; JNIEnv *env = NULL; jstring description = NULL; @@ -749,10 +752,10 @@ fail: return ret; } -int ff_AMediaFormat_getInt32(FFAMediaFormat* format, const char *name, int32_t *out) +static int mediaformat_jni_getInt32(FFAMediaFormat* ctx, const char *name, int32_t *out) { int ret = 1; - + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; JNIEnv *env = NULL; jstring key = NULL; jboolean contains_key; @@ -788,10 +791,10 @@ fail: return ret; } -int ff_AMediaFormat_getInt64(FFAMediaFormat* format, const char *name, int64_t *out) +static int mediaformat_jni_getInt64(FFAMediaFormat* ctx, const char *name, int64_t *out) { int ret = 1; - + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; JNIEnv *env = NULL; jstring key = NULL; jboolean contains_key; @@ -827,10 +830,10 @@ fail: return ret; } -int ff_AMediaFormat_getFloat(FFAMediaFormat* format, const char *name, float *out) +static int mediaformat_jni_getFloat(FFAMediaFormat* ctx, const char *name, float *out) { int ret = 1; - + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; JNIEnv *env = NULL; jstring key = NULL; jboolean contains_key; @@ -866,10 +869,10 @@ fail: return ret; } -int ff_AMediaFormat_getBuffer(FFAMediaFormat* format, const char *name, void** data, size_t *size) +static int mediaformat_jni_getBuffer(FFAMediaFormat* ctx, const char *name, void** data, size_t *size) { int ret = 1; - + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; JNIEnv *env = NULL; jstring key = NULL; jboolean contains_key; @@ -924,10 +927,10 @@ fail: return ret; } -int ff_AMediaFormat_getString(FFAMediaFormat* format, const char *name, const char **out) +static int mediaformat_jni_getString(FFAMediaFormat* ctx, const char *name, const char **out) { int ret = 1; - + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; JNIEnv *env = NULL; jstring key = NULL; jboolean contains_key; @@ -974,10 +977,11 @@ fail: return ret; } -void ff_AMediaFormat_setInt32(FFAMediaFormat* format, const char* name, int32_t value) +static void mediaformat_jni_setInt32(FFAMediaFormat* ctx, const char* name, int32_t value) { JNIEnv *env = NULL; jstring key = NULL; + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; av_assert0(format != NULL); @@ -999,10 +1003,11 @@ fail: } } -void ff_AMediaFormat_setInt64(FFAMediaFormat* format, const char* name, int64_t value) +static void mediaformat_jni_setInt64(FFAMediaFormat* ctx, const char* name, int64_t value) { JNIEnv *env = NULL; jstring key = NULL; + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; av_assert0(format != NULL); @@ -1024,10 +1029,11 @@ fail: } } -void ff_AMediaFormat_setFloat(FFAMediaFormat* format, const char* name, float value) +static void mediaformat_jni_setFloat(FFAMediaFormat* ctx, const char* name, float value) { JNIEnv *env = NULL; jstring key = NULL; + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; av_assert0(format != NULL); @@ -1049,11 +1055,12 @@ fail: } } -void ff_AMediaFormat_setString(FFAMediaFormat* format, const char* name, const char* value) +static void mediaformat_jni_setString(FFAMediaFormat* ctx, const char* name, const char* value) { JNIEnv *env = NULL; jstring key = NULL; jstring string = NULL; + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; av_assert0(format != NULL); @@ -1084,12 +1091,13 @@ fail: } } -void ff_AMediaFormat_setBuffer(FFAMediaFormat* format, const char* name, void* data, size_t size) +static void mediaformat_jni_setBuffer(FFAMediaFormat* ctx, const char* name, void* data, size_t size) { JNIEnv *env = NULL; jstring key = NULL; jobject buffer = NULL; void *buffer_data = NULL; + FFAMediaFormatJni *format = (FFAMediaFormatJni *)ctx; av_assert0(format != NULL); @@ -1131,7 +1139,7 @@ fail: } } -static int codec_init_static_fields(FFAMediaCodec *codec) +static int codec_init_static_fields(FFAMediaCodecJni *codec) { int ret = 0; JNIEnv *env = NULL; @@ -1193,17 +1201,17 @@ static inline FFAMediaCodec *codec_create(int method, const char *arg) { int ret = -1; JNIEnv *env = NULL; - FFAMediaCodec *codec = NULL; + FFAMediaCodecJni *codec = NULL; jstring jarg = NULL; jobject object = NULL; jobject buffer_info = NULL; jmethodID create_id = NULL; - codec = av_mallocz(sizeof(FFAMediaCodec)); + codec = av_mallocz(sizeof(*codec)); if (!codec) { return NULL; } - codec->class = &amediacodec_class; + codec->api = media_codec_jni; env = ff_jni_get_env(codec); if (!env) { @@ -1286,11 +1294,11 @@ fail: av_freep(&codec); } - return codec; + return (FFAMediaCodec *)codec; } #define DECLARE_FF_AMEDIACODEC_CREATE_FUNC(name, method) \ -FFAMediaCodec *ff_AMediaCodec_##name(const char *arg) \ +static FFAMediaCodec *mediacodec_jni_##name(const char *arg) \ { \ return codec_create(method, arg); \ } \ @@ -1299,10 +1307,10 @@ DECLARE_FF_AMEDIACODEC_CREATE_FUNC(createCodecByName, CREATE_CODEC_BY_NAME) DECLARE_FF_AMEDIACODEC_CREATE_FUNC(createDecoderByType, CREATE_DECODER_BY_TYPE) DECLARE_FF_AMEDIACODEC_CREATE_FUNC(createEncoderByType, CREATE_ENCODER_BY_TYPE) -int ff_AMediaCodec_delete(FFAMediaCodec* codec) +static int mediacodec_jni_delete(FFAMediaCodec* ctx) { int ret = 0; - + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNIEnv *env = NULL; if (!codec) { @@ -1335,11 +1343,12 @@ int ff_AMediaCodec_delete(FFAMediaCodec* codec) return ret; } -char *ff_AMediaCodec_getName(FFAMediaCodec *codec) +static char *mediacodec_jni_getName(FFAMediaCodec *ctx) { char *ret = NULL; JNIEnv *env = NULL; jobject *name = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, NULL); @@ -1358,10 +1367,12 @@ fail: return ret; } -int ff_AMediaCodec_configure(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags) +static int mediacodec_jni_configure(FFAMediaCodec* ctx, const FFAMediaFormat* format_ctx, void* surface, void *crypto, uint32_t flags) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; + const FFAMediaFormatJni *format = (FFAMediaFormatJni *)format_ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1375,10 +1386,11 @@ fail: return ret; } -int ff_AMediaCodec_start(FFAMediaCodec* codec) +static int mediacodec_jni_start(FFAMediaCodec* ctx) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1392,10 +1404,11 @@ fail: return ret; } -int ff_AMediaCodec_stop(FFAMediaCodec* codec) +static int mediacodec_jni_stop(FFAMediaCodec* ctx) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1409,10 +1422,11 @@ fail: return ret; } -int ff_AMediaCodec_flush(FFAMediaCodec* codec) +static int mediacodec_jni_flush(FFAMediaCodec* ctx) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1426,10 +1440,11 @@ fail: return ret; } -int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec* codec, size_t idx, int render) +static int mediacodec_jni_releaseOutputBuffer(FFAMediaCodec* ctx, size_t idx, int render) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1443,10 +1458,11 @@ fail: return ret; } -int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs) +static int mediacodec_jni_releaseOutputBufferAtTime(FFAMediaCodec *ctx, size_t idx, int64_t timestampNs) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1460,10 +1476,11 @@ fail: return ret; } -ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec* codec, int64_t timeoutUs) +static ssize_t mediacodec_jni_dequeueInputBuffer(FFAMediaCodec* ctx, int64_t timeoutUs) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1477,10 +1494,11 @@ fail: return ret; } -int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec* codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags) +static int mediacodec_jni_queueInputBuffer(FFAMediaCodec* ctx, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1494,10 +1512,11 @@ fail: return ret; } -ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs) +static ssize_t mediacodec_jni_dequeueOutputBuffer(FFAMediaCodec* ctx, FFAMediaCodecBufferInfo *info, int64_t timeoutUs) { int ret = 0; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL); @@ -1529,11 +1548,11 @@ ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBu return ret; } -uint8_t* ff_AMediaCodec_getInputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size) +static uint8_t* mediacodec_jni_getInputBuffer(FFAMediaCodec* ctx, size_t idx, size_t *out_size) { uint8_t *ret = NULL; JNIEnv *env = NULL; - + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; jobject buffer = NULL; jobject input_buffers = NULL; @@ -1577,11 +1596,11 @@ fail: return ret; } -uint8_t* ff_AMediaCodec_getOutputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size) +static uint8_t* mediacodec_jni_getOutputBuffer(FFAMediaCodec* ctx, size_t idx, size_t *out_size) { uint8_t *ret = NULL; JNIEnv *env = NULL; - + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; jobject buffer = NULL; jobject output_buffers = NULL; @@ -1625,10 +1644,11 @@ fail: return ret; } -FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec) +static FFAMediaFormat* mediacodec_jni_getOutputFormat(FFAMediaCodec* ctx) { FFAMediaFormat *ret = NULL; JNIEnv *env = NULL; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; jobject mediaformat = NULL; @@ -1639,7 +1659,7 @@ FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec) goto fail; } - ret = ff_AMediaFormat_newFromObject(mediaformat); + ret = mediaformat_jni_newFromObject(mediaformat); fail: if (mediaformat) { (*env)->DeleteLocalRef(env, mediaformat); @@ -1648,44 +1668,52 @@ fail: return ret; } -int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx) +static int mediacodec_jni_infoTryAgainLater(FFAMediaCodec *ctx, ssize_t idx) { + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; return idx == codec->INFO_TRY_AGAIN_LATER; } -int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx) +static int mediacodec_jni_infoOutputBuffersChanged(FFAMediaCodec *ctx, ssize_t idx) { + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; return idx == codec->INFO_OUTPUT_BUFFERS_CHANGED; } -int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx) +static int mediacodec_jni_infoOutputFormatChanged(FFAMediaCodec *ctx, ssize_t idx) { + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; return idx == codec->INFO_OUTPUT_FORMAT_CHANGED; } -int ff_AMediaCodec_getBufferFlagCodecConfig(FFAMediaCodec *codec) +static int mediacodec_jni_getBufferFlagCodecConfig(FFAMediaCodec *ctx) { + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; return codec->BUFFER_FLAG_CODEC_CONFIG; } -int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec) +static int mediacodec_jni_getBufferFlagEndOfStream(FFAMediaCodec *ctx) { + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; return codec->BUFFER_FLAG_END_OF_STREAM; } -int ff_AMediaCodec_getBufferFlagKeyFrame(FFAMediaCodec *codec) +static int mediacodec_jni_getBufferFlagKeyFrame(FFAMediaCodec *ctx) { + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; return codec->BUFFER_FLAG_KEY_FRAME; } -int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec) +static int mediacodec_jni_getConfigureFlagEncode(FFAMediaCodec *ctx) { + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; return codec->CONFIGURE_FLAG_ENCODE; } -int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec) +static int mediacodec_jni_cleanOutputBuffers(FFAMediaCodec *ctx) { int ret = 0; + FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx; if (!codec->has_get_i_o_buffer) { if (codec->output_buffers) { @@ -1706,6 +1734,86 @@ fail: return ret; } +static const FFAMediaFormat media_format_jni = { + .class = &amediaformat_class, + + .create = mediaformat_jni_new, + .delete = mediaformat_jni_delete, + + .toString = mediaformat_jni_toString, + + .getInt32 = mediaformat_jni_getInt32, + .getInt64 = mediaformat_jni_getInt64, + .getFloat = mediaformat_jni_getFloat, + .getBuffer = mediaformat_jni_getBuffer, + .getString = mediaformat_jni_getString, + + .setInt32 = mediaformat_jni_setInt32, + .setInt64 = mediaformat_jni_setInt64, + .setFloat = mediaformat_jni_setFloat, + .setString = mediaformat_jni_setString, + .setBuffer = mediaformat_jni_setBuffer, +}; + +static const FFAMediaCodec media_codec_jni = { + .class = &amediacodec_class, + + .getName = mediacodec_jni_getName, + + .createCodecByName = mediacodec_jni_createCodecByName, + .createDecoderByType = mediacodec_jni_createDecoderByType, + .createEncoderByType = mediacodec_jni_createEncoderByType, + .delete = mediacodec_jni_delete, + + .configure = mediacodec_jni_configure, + .start = mediacodec_jni_start, + .stop = mediacodec_jni_stop, + .flush = mediacodec_jni_flush, + + .getInputBuffer = mediacodec_jni_getInputBuffer, + .getOutputBuffer = mediacodec_jni_getOutputBuffer, + + .dequeueInputBuffer = mediacodec_jni_dequeueInputBuffer, + .queueInputBuffer = mediacodec_jni_queueInputBuffer, + + .dequeueOutputBuffer = mediacodec_jni_dequeueOutputBuffer, + .getOutputFormat = mediacodec_jni_getOutputFormat, + + .releaseOutputBuffer = mediacodec_jni_releaseOutputBuffer, + .releaseOutputBufferAtTime = mediacodec_jni_releaseOutputBufferAtTime, + + .infoTryAgainLater = mediacodec_jni_infoTryAgainLater, + .infoOutputBuffersChanged = mediacodec_jni_infoOutputBuffersChanged, + .infoOutputFormatChanged = mediacodec_jni_infoOutputFormatChanged, + + .getBufferFlagCodecConfig = mediacodec_jni_getBufferFlagCodecConfig, + .getBufferFlagEndOfStream = mediacodec_jni_getBufferFlagEndOfStream, + .getBufferFlagKeyFrame = mediacodec_jni_getBufferFlagKeyFrame, + + .getConfigureFlagEncode = mediacodec_jni_getConfigureFlagEncode, + .cleanOutputBuffers = mediacodec_jni_cleanOutputBuffers, +}; + +FFAMediaFormat *ff_AMediaFormat_new(void) +{ + return media_format_jni.create(); +} + +FFAMediaCodec* ff_AMediaCodec_createCodecByName(const char *name) +{ + return media_codec_jni.createCodecByName(name); +} + +FFAMediaCodec* ff_AMediaCodec_createDecoderByType(const char *mime_type) +{ + return media_codec_jni.createDecoderByType(mime_type); +} + +FFAMediaCodec* ff_AMediaCodec_createEncoderByType(const char *mime_type) +{ + return media_codec_jni.createEncoderByType(mime_type); +} + int ff_Build_SDK_INT(AVCodecContext *avctx) { int ret = -1; diff --git a/libavcodec/mediacodec_wrapper.h b/libavcodec/mediacodec_wrapper.h index b106ff315a..606fdbede5 100644 --- a/libavcodec/mediacodec_wrapper.h +++ b/libavcodec/mediacodec_wrapper.h @@ -58,28 +58,90 @@ int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx); char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx); -struct FFAMediaFormat; typedef struct FFAMediaFormat FFAMediaFormat; +struct FFAMediaFormat { + const AVClass *class; + + FFAMediaFormat *(*create)(void); + int (*delete)(FFAMediaFormat *); + + char* (*toString)(FFAMediaFormat* format); + + int (*getInt32)(FFAMediaFormat* format, const char *name, int32_t *out); + int (*getInt64)(FFAMediaFormat* format, const char *name, int64_t *out); + int (*getFloat)(FFAMediaFormat* format, const char *name, float *out); + int (*getBuffer)(FFAMediaFormat* format, const char *name, void** data, size_t *size); + int (*getString)(FFAMediaFormat* format, const char *name, const char **out); + + void (*setInt32)(FFAMediaFormat* format, const char* name, int32_t value); + void (*setInt64)(FFAMediaFormat* format, const char* name, int64_t value); + void (*setFloat)(FFAMediaFormat* format, const char* name, float value); + void (*setString)(FFAMediaFormat* format, const char* name, const char* value); + void (*setBuffer)(FFAMediaFormat* format, const char* name, void* data, size_t size); +}; FFAMediaFormat *ff_AMediaFormat_new(void); -int ff_AMediaFormat_delete(FFAMediaFormat* format); -char* ff_AMediaFormat_toString(FFAMediaFormat* format); +static inline int ff_AMediaFormat_delete(FFAMediaFormat* format) +{ + return format->delete(format); +} -int ff_AMediaFormat_getInt32(FFAMediaFormat* format, const char *name, int32_t *out); -int ff_AMediaFormat_getInt64(FFAMediaFormat* format, const char *name, int64_t *out); -int ff_AMediaFormat_getFloat(FFAMediaFormat* format, const char *name, float *out); -int ff_AMediaFormat_getBuffer(FFAMediaFormat* format, const char *name, void** data, size_t *size); -int ff_AMediaFormat_getString(FFAMediaFormat* format, const char *name, const char **out); +static inline char* ff_AMediaFormat_toString(FFAMediaFormat* format) +{ + return format->toString(format); +} -void ff_AMediaFormat_setInt32(FFAMediaFormat* format, const char* name, int32_t value); -void ff_AMediaFormat_setInt64(FFAMediaFormat* format, const char* name, int64_t value); -void ff_AMediaFormat_setFloat(FFAMediaFormat* format, const char* name, float value); -void ff_AMediaFormat_setString(FFAMediaFormat* format, const char* name, const char* value); -void ff_AMediaFormat_setBuffer(FFAMediaFormat* format, const char* name, void* data, size_t size); +static inline int ff_AMediaFormat_getInt32(FFAMediaFormat* format, const char *name, int32_t *out) +{ + return format->getInt32(format, name, out); +} + +static inline int ff_AMediaFormat_getInt64(FFAMediaFormat* format, const char *name, int64_t *out) +{ + return format->getInt64(format, name, out); +} + +static inline int ff_AMediaFormat_getFloat(FFAMediaFormat* format, const char *name, float *out) +{ + return format->getFloat(format, name, out); +} + +static inline int ff_AMediaFormat_getBuffer(FFAMediaFormat* format, const char *name, void** data, size_t *size) +{ + return format->getBuffer(format, name, data, size); +} + +static inline int ff_AMediaFormat_getString(FFAMediaFormat* format, const char *name, const char **out) +{ + return format->getString(format, name, out); +} + +static inline void ff_AMediaFormat_setInt32(FFAMediaFormat* format, const char* name, int32_t value) +{ + format->setInt32(format, name, value); +} + +static inline void ff_AMediaFormat_setInt64(FFAMediaFormat* format, const char* name, int64_t value) +{ + format->setInt64(format, name, value); +} + +static inline void ff_AMediaFormat_setFloat(FFAMediaFormat* format, const char* name, float value) +{ + format->setFloat(format, name, value); +} + +static inline void ff_AMediaFormat_setString(FFAMediaFormat* format, const char* name, const char* value) +{ + format->setString(format, name, value); +} + +static inline void ff_AMediaFormat_setBuffer(FFAMediaFormat* format, const char* name, void* data, size_t size) +{ + format->setBuffer(format, name, data, size); +} -struct FFAMediaCodec; -typedef struct FFAMediaCodec FFAMediaCodec; typedef struct FFAMediaCodecCryptoInfo FFAMediaCodecCryptoInfo; struct FFAMediaCodecBufferInfo { @@ -90,41 +152,160 @@ struct FFAMediaCodecBufferInfo { }; typedef struct FFAMediaCodecBufferInfo FFAMediaCodecBufferInfo; -char *ff_AMediaCodec_getName(FFAMediaCodec *codec); +typedef struct FFAMediaCodec FFAMediaCodec; +struct FFAMediaCodec { + const AVClass *class; + + char *(*getName)(FFAMediaCodec *codec); + + FFAMediaCodec* (*createCodecByName)(const char *name); + FFAMediaCodec* (*createDecoderByType)(const char *mime_type); + FFAMediaCodec* (*createEncoderByType)(const char *mime_type); + int (*delete)(FFAMediaCodec* codec); + + int (*configure)(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags); + int (*start)(FFAMediaCodec* codec); + int (*stop)(FFAMediaCodec* codec); + int (*flush)(FFAMediaCodec* codec); + + uint8_t* (*getInputBuffer)(FFAMediaCodec* codec, size_t idx, size_t *out_size); + uint8_t* (*getOutputBuffer)(FFAMediaCodec* codec, size_t idx, size_t *out_size); + + ssize_t (*dequeueInputBuffer)(FFAMediaCodec* codec, int64_t timeoutUs); + int (*queueInputBuffer)(FFAMediaCodec* codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags); + + ssize_t (*dequeueOutputBuffer)(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs); + FFAMediaFormat* (*getOutputFormat)(FFAMediaCodec* codec); + + int (*releaseOutputBuffer)(FFAMediaCodec* codec, size_t idx, int render); + int (*releaseOutputBufferAtTime)(FFAMediaCodec *codec, size_t idx, int64_t timestampNs); + + int (*infoTryAgainLater)(FFAMediaCodec *codec, ssize_t idx); + int (*infoOutputBuffersChanged)(FFAMediaCodec *codec, ssize_t idx); + int (*infoOutputFormatChanged)(FFAMediaCodec *codec, ssize_t indx); + + int (*getBufferFlagCodecConfig)(FFAMediaCodec *codec); + int (*getBufferFlagEndOfStream)(FFAMediaCodec *codec); + int (*getBufferFlagKeyFrame)(FFAMediaCodec *codec); + + int (*getConfigureFlagEncode)(FFAMediaCodec *codec); + + int (*cleanOutputBuffers)(FFAMediaCodec *codec); +}; + +static inline char *ff_AMediaCodec_getName(FFAMediaCodec *codec) +{ + return codec->getName(codec); +} FFAMediaCodec* ff_AMediaCodec_createCodecByName(const char *name); FFAMediaCodec* ff_AMediaCodec_createDecoderByType(const char *mime_type); FFAMediaCodec* ff_AMediaCodec_createEncoderByType(const char *mime_type); -int ff_AMediaCodec_configure(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags); -int ff_AMediaCodec_start(FFAMediaCodec* codec); -int ff_AMediaCodec_stop(FFAMediaCodec* codec); -int ff_AMediaCodec_flush(FFAMediaCodec* codec); -int ff_AMediaCodec_delete(FFAMediaCodec* codec); +static inline int ff_AMediaCodec_configure(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags) +{ + return codec->configure(codec, format, surface, crypto, flags); +} + +static inline int ff_AMediaCodec_start(FFAMediaCodec* codec) +{ + return codec->start(codec); +} + +static inline int ff_AMediaCodec_stop(FFAMediaCodec* codec) +{ + return codec->stop(codec); +} + +static inline int ff_AMediaCodec_flush(FFAMediaCodec* codec) +{ + return codec->flush(codec); +} + +static inline int ff_AMediaCodec_delete(FFAMediaCodec* codec) +{ + return codec->delete(codec); +} + +static inline uint8_t* ff_AMediaCodec_getInputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size) +{ + return codec->getInputBuffer(codec, idx, out_size); +} + +static inline uint8_t* ff_AMediaCodec_getOutputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size) +{ + return codec->getOutputBuffer(codec, idx, out_size); +} + +static inline ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec* codec, int64_t timeoutUs) +{ + return codec->dequeueInputBuffer(codec, timeoutUs); +} + +static inline int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec *codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags) +{ + return codec->queueInputBuffer(codec, idx, offset, size, time, flags); +} + +static inline ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs) +{ + return codec->dequeueOutputBuffer(codec, info, timeoutUs); +} + +static inline FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec) +{ + return codec->getOutputFormat(codec); +} + +static inline int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec* codec, size_t idx, int render) +{ + return codec->releaseOutputBuffer(codec, idx, render); +} + +static inline int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs) +{ + return codec->releaseOutputBufferAtTime(codec, idx, timestampNs); +} -uint8_t* ff_AMediaCodec_getInputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size); -uint8_t* ff_AMediaCodec_getOutputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size); +static inline int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx) +{ + return codec->infoTryAgainLater(codec, idx); +} -ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec* codec, int64_t timeoutUs); -int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec* codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags); +static inline int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx) +{ + return codec->infoOutputBuffersChanged(codec, idx); +} -ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs); -FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec); +static inline int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx) +{ + return codec->infoOutputFormatChanged(codec, idx); +} -int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec* codec, size_t idx, int render); -int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs); +static inline int ff_AMediaCodec_getBufferFlagCodecConfig(FFAMediaCodec *codec) +{ + return codec->getBufferFlagCodecConfig(codec); +} -int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx); -int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx); -int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t indx); +static inline int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec) +{ + return codec->getBufferFlagEndOfStream(codec); +} -int ff_AMediaCodec_getBufferFlagCodecConfig (FFAMediaCodec *codec); -int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec); -int ff_AMediaCodec_getBufferFlagKeyFrame(FFAMediaCodec *codec); +static inline int ff_AMediaCodec_getBufferFlagKeyFrame(FFAMediaCodec *codec) +{ + return codec->getBufferFlagKeyFrame(codec); +} -int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec); +static inline int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec) +{ + return codec->getConfigureFlagEncode(codec); +} -int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec); +static inline int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec) +{ + return codec->cleanOutputBuffers(codec); +} int ff_Build_SDK_INT(AVCodecContext *avctx);