From patchwork Wed Nov 6 12:31:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 52612 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:2fe5:b0:4a6:1c7:11b7 with SMTP id kw5csp614639vqb; Wed, 6 Nov 2024 04:33:35 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXPJKGm8Fl7cvhxjzIrTdSoKewJwo0fPT+mi3p94gBtnwIk3xaxfyiugW4bJIQwFh9iqTuZKEov28lo0/iORZn2@gmail.com X-Google-Smtp-Source: AGHT+IFDoVc5nuV36USuUAHNsQsSKv3WBf+aIkzn0SPZ/seE6Xf/usoDMO2L8iPSePNX6W3RT8UX X-Received: by 2002:a17:907:7d89:b0:a9e:b379:429e with SMTP id a640c23a62f3a-a9eb3794464mr222652766b.14.1730896414868; Wed, 06 Nov 2024 04:33:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1730896414; cv=none; d=google.com; s=arc-20240605; b=cW0kWf01Pz5nLyrLjy6hYMRtdemDxr9GLT0s2J5lsEVnLJx4BYMD4l9hDKBfdcV7Xt Y0KqP+QGymEO2Ocp4X8F4fOhWrjKYVu6Wr+SY1Ieyz0WUgBVxwNEdExo77YeTT+fg8Nj jxt35PgsCgvBFqwbReIBapm4Jl4oW2ykA1vUN/DpWobkUChrA7XPdST9n50RKV/EUcSo nvBaiD9fiDLjyXwrb+TGJOBPW8uXmJrQaFsdjvWTbewDFdXk1nWyB6gKvF54+yJsPFKy abQlRy8A234rNb6OirgVzpF6aplrRtDX9FZrEX7iF4ozaXSb5VE+M4Q8kj/cIzFJyEzn ul4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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:date:to:from:message-id :dkim-signature:delivered-to; bh=/IF9Gkuz0rdrSVEjE/FeVxwBad8mGc1n6B0OVbWpkhM=; fh=HnHYuZ9XgUo86ZRXTLWWmQxhslYEI9B9taZ5X1DLFfc=; b=JNQ//FTtLIdTE6EkYpeZ6vk2X/kwwzZXtv2RZXg6n+8Vu9gRf8Gwc9dYUQW3vA+VSs yYkJd30pM6XaXvQ3IAjWlHMD8ke80xcL4TjEbRXU7DDUXR3TDVnDoMTYjQ285JcjDGL2 6Eu5FVj1yM2yY6c6WpNN8WPdnqFYWe+SDGlH+8d3gq/goGznMOfsZVz6DJmpyQyGVLgz Cn3WuAiuZHQP87DKZZgGKDSmYHy2sV6o7AGoZTS+NNmcQjBdWcAECTC3IHVAOnVOMQ+W np8uhVLajShlKYcb5PVE4vp3ld33lu+oZn+pLXQy4xoqJlSs9qok/n8AVM6rqsZ2d7DP y2TQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=Pqmjeedl; 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 a640c23a62f3a-a9eb1816ff8si261403466b.819.2024.11.06.04.33.34; Wed, 06 Nov 2024 04:33:34 -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; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=Pqmjeedl; 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 125FF68DDDA; Wed, 6 Nov 2024 14:33:32 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out162-62-57-137.mail.qq.com (out162-62-57-137.mail.qq.com [162.62.57.137]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 332E668DE0A for ; Wed, 6 Nov 2024 14:33:24 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1730896402; bh=pkXdjljJzHlnJrwhQ2bumvNneTsAWvIeMglWF1U3UuQ=; h=From:To:Cc:Subject:Date; b=Pqmjeedlaw9nxVmymZdXMPgHwsFnFsB3q0kU9l1TBb7O0ks3vcj/Ph1C328g27WeA 5YLuB0ww+e4vkKgBQML+LJms2ztJAsl5VfUBVPHgXaVvWN5yJ7+9wNT661VusCFEFk zhit48Jo7d7TdSNWwpqSqiTbCZc36HAI/8byxSHQ= Received: from ZHILIZHAO-MB1.tencent.com ([59.37.125.101]) by newxmesmtplogicsvrsza29-0.qq.com (NewEsmtp) with SMTP id 7C5B72C0; Wed, 06 Nov 2024 20:31:05 +0800 X-QQ-mid: xmsmtpt1730896265t01dvszv1 Message-ID: X-QQ-XMAILINFO: M8Cd2byC8kc4P9gY+w37gYhnsbxNiECsgtIXPdkUhkmfvis94EiomrZlRCNTmW Pw4+vQtnSuQxFV0ztsuCJad2jBoewZXRkmScOPFZfPgY8ngGm5hI99o9nF4mSEpoXMGc+irQ+cyl AtOzupbwaHT4QNhy+0Wkmy/XJQ2zb2A6eORTbucI6bDQ2Jtp615VUVou8cN0yovO8wjo7EDEy1IJ cvFNMG2aXIeSh08Lhb7ii36R4tI4RABE5TiDx85AZqu1VC16kNkMLlTdhpMiBiWJVjg2P1h6vr/i wNUeXpGff/ysbYwwlw1BlqD7jDqQlhFnMVJYMG7K7WHpCLXhWTpeUpcpGyguOy+JVoIj5pXrw+Yr xXGUxpjaN7RfrcJIDdjW7NxxSJ9nmnCwFcBjcKMP8QoHkTO7M+W2Heaxs1fF/XWeZQUP8ctR5mB0 9hP01PkmaqG/rrZSYY+yC1TtmNb1GarqT0sjCMCQ5e8BsWaYTt64Z5yN8YNzVLBvCutyYDSoSHHu qgtjj/g4OZ1Oa0dhw0PsjzeoFxHprllnYpAMXyDxjbZ9Gj4uQS3/FBGEz5cQ9kyOkFs1W4MR8lDt r2I0YXWYhaRTfrCNUf98LwwpeJX2VF8C4Wbd6kaL7HRk5VOY/NPxL/vSxBwJ1BLmqBXjTJpPlrJu RCkyK8bNgd+d3xcLA63s+uyTTQ52CgnRWUyRRs8tSz553CGhyQBn1BXlQBVpJb/Hdt6S2ct55/Vh cizv7FucNOrpILkzqYl5TzBXlhEudmH8cReAOeHNqhEINbfIjBFqdLDofRrb0eYjtvA4ZkLZDF39 bmxc1s33QRVcoBtAeZXwNOf0QLOsNr1OiRQfg2aVfUsnHy96PRXqdr0B/Koh6N9ga3hm5l+H6d0U mSnmql3Yl1PEs7OOQha5/QaJNoreJ1Ze0W1V3HEVInDCyez+QuuQ6ogWTk1a9+RvOVrWwr/bJuev gPfpzdZx73/7aspdz7swPFMHsAr/iX X-QQ-XMRINFO: NS+P29fieYNw95Bth2bWPxk= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Nov 2024 20:31:00 +0800 X-OQ-MSGID: <20241106123103.17111-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] avcodec/mediacodecdec: Add operating_rate option 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: PbFMnnw4wKXg From: Zhao Zhili The codec wants to know whether the usecase is realtime playback or full-speed transcoding, or playback at a higher speed. The codec runs faster when operating_rate higher than framerate. --- libavcodec/mediacodecdec.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libavcodec/mediacodecdec.c b/libavcodec/mediacodecdec.c index cc55b306bd..4937828f21 100644 --- a/libavcodec/mediacodecdec.c +++ b/libavcodec/mediacodecdec.c @@ -57,6 +57,8 @@ typedef struct MediaCodecH264DecContext { int amlogic_mpeg2_api23_workaround; int use_ndk_codec; + // Ref. MediaFormat KEY_OPERATING_RATE + int operating_rate; } MediaCodecH264DecContext; static av_cold int mediacodec_decode_close(AVCodecContext *avctx) @@ -441,6 +443,8 @@ static av_cold int mediacodec_decode_init(AVCodecContext *avctx) ff_AMediaFormat_setInt32(format, "channel-count", avctx->ch_layout.nb_channels); ff_AMediaFormat_setInt32(format, "sample-rate", avctx->sample_rate); } + if (s->operating_rate > 0) + ff_AMediaFormat_setInt32(format, "operating-rate", s->operating_rate); s->ctx = av_mallocz(sizeof(*s->ctx)); if (!s->ctx) { @@ -599,6 +603,8 @@ static const AVOption ff_mediacodec_vdec_options[] = { OFFSET(delay_flush), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD }, { "ndk_codec", "Use MediaCodec from NDK", OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VD }, + { "operating_rate", "The desired operating rate that the codec will need to operate at, zero for unspecified", + OFFSET(operating_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VD }, { NULL } }; @@ -662,6 +668,8 @@ DECLARE_MEDIACODEC_VDEC(av1, "AV1", AV_CODEC_ID_AV1, NULL) static const AVOption ff_mediacodec_adec_options[] = { { "ndk_codec", "Use MediaCodec from NDK", OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AD }, + { "operating_rate", "The desired operating rate that the codec will need to operate at, zero for unspecified", + OFFSET(operating_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AD }, { NULL } }; From patchwork Wed Nov 6 12:31:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 52613 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:2fe5:b0:4a6:1c7:11b7 with SMTP id kw5csp617804vqb; Wed, 6 Nov 2024 04:39:08 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVDtlxbff4Jc9fLFYKxhyYYgUOaSjLW7H3MJ+Q3olyUwgKSyv1ymVkugmi6/E/22uvUyxJ5yzBBACv9QVPX6gOT@gmail.com X-Google-Smtp-Source: AGHT+IGe9E4H6SK2QetjXreFldG0x0B6vQHuGSaDPUi5LSBXODhM01ea4PAI4zfvAA0iG1gazRby X-Received: by 2002:a17:907:9608:b0:a99:4a35:2400 with SMTP id a640c23a62f3a-a9de5fa8260mr1363328266b.7.1730896747959; Wed, 06 Nov 2024 04:39:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1730896747; cv=none; d=google.com; s=arc-20240605; b=WYVSBdiYkLJovdLA+srK+ac94QTQXV2x2tjvADN3GBYqoGanOI1byhP+fR5zdovUYJ 99LExrUIewtMzqWRt9zqvyGksHUmExp89sTqrYFHaESnITHh+WQE6kEviwCVqdneChbG ZqlIwXgNmzxwK30XMGTpCSsoN5cqB7sHGFwBwsAyA9lkYNyTiIA2qMMzLNT755OlX1+B Ji7s7wUw5Sc6w1iqJtIsxEneTmI4PkE2eE3qkuRAUKxRAzAUsyDusih+Bii4mRrXpbzy n6QhtpaV/iejE1138HQyapL9xmADw+NqM9ER7d+KxhOG6L60NAX4oiG9hjGpxI4O4G9h 0RaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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:date:to:from:message-id :dkim-signature:delivered-to; bh=QxNUt4gvH/i6n/ew10pFtaPRRoAwzatrH/8wm3q0GBY=; fh=HnHYuZ9XgUo86ZRXTLWWmQxhslYEI9B9taZ5X1DLFfc=; b=BP8nglfIcqkFx2MvsZ+7zL245dXShG/Ntz5zs9uBOkg52vXhzN87k2g1Sm3r/Z0imC VocE7TnPnBpYKVK82XuR+A0FUwNf3oHILxXaKn6E2ZAmEbNt4+ODdi5OpKiOXyB/Z6l0 QntxgHZ71LpFA8pZjlf/hliGccchEgLBqs+gdZ2bVjMHJfYfFz9j3Lg/j/oCqNyAqKWY 0lB3HxLDZdKeO7XsHU5hbi1aXp9wonKeCCahP9wc365ItYTkc3xxzCltqyuhIDvwzhuG da3Lh6t5xnVKmXsrp5JvQXDVoW6+0acR/l2N+QADc9GRGyCHdk2SlRpEeTrFz1l3eIhm suEg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=MQ5Fl6O4; 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 a640c23a62f3a-a9eb17ce0c2si126106266b.346.2024.11.06.04.39.07; Wed, 06 Nov 2024 04:39:07 -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; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=MQ5Fl6O4; 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 8B5BF68DC62; Wed, 6 Nov 2024 14:31:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-236.mail.qq.com (out203-205-221-236.mail.qq.com [203.205.221.236]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5417D68DD1F for ; Wed, 6 Nov 2024 14:31:34 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1730896286; bh=HKu1VOJlE5p1rQtHReJmqjqsJOSyy4OgbUc0r/6UeNk=; h=From:To:Cc:Subject:Date; b=MQ5Fl6O4vVaU/vx3aY02+svMieuu9QSwoIyofvfsqwbK4KTIpJwCo+B133puMJ3ej ZcCiaz5BC/JLQVB06ue2bDAMGtf+kfO8aWdcK8WhNfvq1tuK0kmXq0qY2p54dqG4al 87mYRaldDxMbExzatB3KfROnynf/4nm3BoS6zaOc= Received: from ZHILIZHAO-MB1.tencent.com ([59.37.125.101]) by newxmesmtplogicsvrsza36-0.qq.com (NewEsmtp) with SMTP id 7D8B0E0D; Wed, 06 Nov 2024 20:31:24 +0800 X-QQ-mid: xmsmtpt1730896284tjigj7a1m Message-ID: X-QQ-XMAILINFO: NkHKfw09D6j8Mr3e6EuYzqu22Zhec5siHINJxBJ4cCZT8gst6tr6n5lXqpV5wR GdQR7+Ny7fbnAgoTg8xOxrN25xACWRZYUhxK2I7VtSTzzXrSpy3A7/zYORYkkbe3kok4obqW1iqt 3+0qK9bdJ4mIAtyPLiLMm6pDLiheMnlqELaH6l04Y3V0T7wqMiEfK3NTEDrPItfF3o4jtn77VIWu ZQIgzQ8wyYS/GR1MYyubPjvUu0fZwEyvHpTPh5ruH427PqVGFyPvGyYBMb6KWX8ubFjRJcmoik63 ank4Up73gCFZ4zUqRfLCjODOwhFG2+Wx2U1+okAzKQzMPBiEhdgruX3S6P1MlXLUgsXDozNLSHx6 peHDV25zo4rKOQPPELsSZS66MD0uU+d48QFyImn2tjwuCvepDpIMeFAemFrI03+7vRlhJQvpxCS0 AxUEfTSB5+ph27gCa7i4+m9vyh6c693D3JMbhzJ6Cq/vONS4wCi239t2EImd2dQeTntSAM+/QfU8 V38mSja0qayY6BSSRc5SGq6HV8vYcQnwauBCfuMhD7XGB470H1Q/yMJQB5o4noUKhXLXY3eCPFMt EGmxFsNDwfvXqG6bhGPAfDVKl4DZU4XyJAse7QmfW2ojzL2JMGaRDpXLsllusgRgMu4NoxvSfHiP Qszht4ESi1i4nxPE0LuJBZWGjpAgDuwCnnjxjQxLi2TivPa6DSg4WO5nmGcEyFXsj5blG6miA/S7 gH+oeMB6ZTkS2BvXOedZr+AyBjN9WGzpcBf91aoxQyD0bqaMggaWAC/ngXh7f9P/hCeYeWLjeORz 5UTQWtV9R4FbTqcGan/vxOGtweUkG8SlhWU0SWGwzFKnTlIeXXg9jP5igEZsIonjLIdw40Fvm+bx mBzbgJlKdqf4nqISxB2O3jZazG3psk+pLi1Ee6YjKv2p16TEkGjjW0KCXfJg0gqzZHk58rCDaNyD x6LXIGEysf+zHGQfUq8v2Gn71DnM49grOOQUv3leb+z0Cl6lzen2ppdK3gnfvj9oIl4ICdKdk4pd KvU4IBsw== X-QQ-XMRINFO: Nq+8W0+stu50PRdwbJxPCL0= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Nov 2024 20:31:21 +0800 X-OQ-MSGID: <20241106123121.17284-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avcodec/mediacodecenc: Add operating_rate option 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 26M+9Q/brtvq From: Zhao Zhili For example, with ./ffmpeg -operating_rate 400 -hwaccel mediacodec -i test.mp4 -an \ -c:v h264_mediacodec -operating_rate 400 -b:v 5M -f null - The transcoding speed is 254 FPS. Without -operating_rate on dec/enc, the speed is 148 FPS. With -operating_rate on decoder only, the speed is 239 FPS. --- libavcodec/mediacodecenc.c | 7 ++++++- libavcodec/version.h | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c index 3880ea2fe9..e79fe29964 100644 --- a/libavcodec/mediacodecenc.c +++ b/libavcodec/mediacodecenc.c @@ -76,6 +76,8 @@ typedef struct MediaCodecEncContext { int level; int pts_as_dts; int extract_extradata; + // Ref. MediaFormat KEY_OPERATING_RATE + int operating_rate; } MediaCodecEncContext; enum { @@ -354,6 +356,8 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) } if (s->pts_as_dts == -1) s->pts_as_dts = avctx->max_b_frames <= 0; + if (s->operating_rate > 0) + ff_AMediaFormat_setInt32(format, "operating-rate", s->operating_rate); ret = ff_AMediaCodec_getConfigureFlagEncode(s->codec); ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret); @@ -764,7 +768,8 @@ static const AVCodecHWConfigInternal *const mediacodec_hw_configs[] = { { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \ "since most of Android devices don't output B frames by default.", \ OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \ - + { "operating_rate", "The desired operating rate that the codec will need to operate at, zero for unspecified", \ + OFFSET(operating_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE }, \ #define MEDIACODEC_ENCODER_CLASS(name) \ static const AVClass name ## _mediacodec_class = { \ diff --git a/libavcodec/version.h b/libavcodec/version.h index 9411511e04..9f55381cf1 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -30,7 +30,7 @@ #include "version_major.h" #define LIBAVCODEC_VERSION_MINOR 22 -#define LIBAVCODEC_VERSION_MICRO 100 +#define LIBAVCODEC_VERSION_MICRO 101 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \ From patchwork Wed Nov 6 12:31:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 52611 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:2fe5:b0:4a6:1c7:11b7 with SMTP id kw5csp613684vqb; Wed, 6 Nov 2024 04:31:57 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUm1XgNTdK/5tU7x7wtSmeq+uRm4WCtlknPAOJTEV1MyekrDWp4rpQrpHmPB2gOyXq3iIpj8AcSdbvXHW4jxdQI@gmail.com X-Google-Smtp-Source: AGHT+IFDYbdubPi6fp5LDFCuefGwtseVzSdujtHPWacwbMOCQGR8zTULTzC2kAMWmdNmOY8DsifG X-Received: by 2002:a17:907:9482:b0:a9e:2ff8:c440 with SMTP id a640c23a62f3a-a9e508aa606mr2435699766b.9.1730896316763; Wed, 06 Nov 2024 04:31:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1730896316; cv=none; d=google.com; s=arc-20240605; b=MZW0K9cadgIKWev4sZv6DxI5R5nfZ5N0FJNZAxOD8U0J3tFIVhTMldREh7Qbs9VIcl DlfcdCOBe0f7ECy0B73bT7gzODOKqLMLDv0c+wAhQ5JraG5gWzs8WSg2lQOQ6Kf6/O70 UGvXpqFkFSCy4u1M9B4vw42P23U7Kp/8soOnnR7tIH/3WwkrUZt9H2RZQIhL5bTdzuh5 aSnTIOdg9TT5ExMJrZFMi0p+HDibQGpocs60/vvrjws9KgiBb/kFMDuJ7uOO4+QpsJhK NYGcPxqqO3+LPH/8Sie83RQTfUOuRhATv+aZMCl7d7XuJusJ8+03WaLk+CEghaQbNPU8 20XQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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:date:to:from:message-id :dkim-signature:delivered-to; bh=PNj/fqQTtEaNzdm73aUzwPdaZatiTnWkBkB1qDbwGZg=; fh=HnHYuZ9XgUo86ZRXTLWWmQxhslYEI9B9taZ5X1DLFfc=; b=O/uVvFTjd51KzhTVALupBFGnt7OF/m4m3WsHtpAe38pVc26WMwvmDqf/kC8us9XLia r6jsdXYHAOtuzosIx8h0HfwRW0cwv3zhIcaT+6WQ4IcJjsbGq1s2XNLGyx1TogEj1fhz nc/9/+D+DX2nP0t/Ho8nsLevUYQUv9twH1wzjM05bvt7gc73NYcJ3HGzZWZlchjKQzvt DpIqyzU1l/bol4Fv5FsFLF0Vh8oqAk6jGmAnSB65V9//BIud1Jw/h0FczMym4I6/uF1I wl5yMoa3BXxZWAPSUrpKCfQQL22eQaX+mhdL5VZ00MS1Ze+sVTWzQaKeOooymXeZO+hT Korw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b="N+/zLe9y"; 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 a640c23a62f3a-a9eb18428ecsi264273366b.906.2024.11.06.04.31.56; Wed, 06 Nov 2024 04:31: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; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b="N+/zLe9y"; 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 E56F268DDAC; Wed, 6 Nov 2024 14:31:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-164.mail.qq.com (out203-205-221-164.mail.qq.com [203.205.221.164]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 596FF68DDCF for ; Wed, 6 Nov 2024 14:31:44 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1730896296; bh=5kr2tuY0XHCUF6mU05wWhGY0ThlHXivCT46+32nf1I0=; h=From:To:Cc:Subject:Date; b=N+/zLe9ypfA033xxHp1RU7TjDdBCE5nkbHmJo/VERLY82fgvSkn0MVaaSOKsbKzwm Fuo9zbxRjoQr2bcvyAGfhXZsHOMZgmN+Tx7c5GybImymnb0IVLUt5PIoCwh3jNHRQQ GJAnWXCud00Rn9lRvf0WO3/8Bom8QDAr6gbipmkU= Received: from ZHILIZHAO-MB1.tencent.com ([59.37.125.101]) by newxmesmtplogicsvrsza15-1.qq.com (NewEsmtp) with SMTP id 7E31B07E; Wed, 06 Nov 2024 20:31:35 +0800 X-QQ-mid: xmsmtpt1730896295thx13aokl Message-ID: X-QQ-XMAILINFO: Mk9vQ32lX3utYzlY9lXaXahz2WiGD2556eEHY0rRyGGQQ7BjSLxdq6scMbIWfs 6MjCR8cVTdzkPf6Cie2IfTXj2ZvGy5ww9F52QNCZ8LpvJ1sHr2pOh8WiuVDj17LEhlVQCd3dQHgg 3LyiNVAsFeY1/uT1uqYBeHr41qaMhkVSaMeoSMY7YiUGcNbUHRMwWaCWH0t9+jTe9+ld+VRjD5kT 5EuCu+3iAfvd7+XCHwPaQLYkWJkd9hr2j1n/2vYb7Xu30mTIJ9w4Qw03yB7ggyK8a/tnbp5K7O6j P/fd+sqA25WKqb3b/L7w14nx36nDJeqIZr9NNR9RHkL6inSre1y0J3FW/K6qEszIRH6MlvnF3DA9 +GMqLdEV2NHaDTAkJ/0SBircLkGoD4ft7Y0CHoO3YVW0e/tpig44D0fy/rRC/CDC3xOATKnji5Zm PRrajReiAsJtLown4WnYsHUwX1z+3b9d/28SrXgvkWroAcwoIu2GTyU6UH1ZqpaxicPGTrRe3kji 0jMEy4X+MIOSzHJQtlU9DX4s8OffOI2I2SfsnIJoFysfPaZAeWvHIGJCDof8eQC5iCEWEdY5BnWd pPY6dOkfPX/g06kdGAMj64WVnAbAkoP4ZV/PRscEx3Dik/cM600h4zdiorVlQmOJtk3AYqQcudIP XdRVoP7yUU52vSTR1G6d8zEnMWsEn72uVzAO9ma3NXjFNSqdsRgjYs+FxJmclDFFo+9HmA7RGP93 ZXaBxYerZAp1eV7iY4ZpQWC0ez5+HoWZv2AABYHDbg78MM8dI3Pin7riaxNKZGFtliPDS8njslBk 1vyqE8llDFaP2JyN2i7NE3Dns3TL9z5JExzRqyhHD/9Xl4TQhGjDnA0SL+kKEbYe+IF+mBN4dyDv QVnyG38Y2LQ/KeqGu40PFzCOGTJZbcooqq8lxljyYmgEXlqVmqa01tx3i+KL3IGgOVBlOqBHSPz3 CGg2jKySM5yH0iJiKgTYe3RO/Y44LUyH2OUHqtr1s= X-QQ-XMRINFO: MSVp+SPm3vtS1Vd6Y4Mggwc= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Nov 2024 20:31:34 +0800 X-OQ-MSGID: <20241106123134.17396-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] avcodec/mediacodecenc: add async mode support 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: SPtIBfQv93mi From: Zhao Zhili It has better performance than poll in a loop. --- configure | 2 +- libavcodec/mediacodecenc.c | 296 +++++++++++++++++++++++++++++++++---- libavcodec/version.h | 2 +- 3 files changed, 266 insertions(+), 34 deletions(-) diff --git a/configure b/configure index 9f508a2527..48e4c97dbf 100755 --- a/configure +++ b/configure @@ -3153,7 +3153,7 @@ d3d11va_deps="dxva_h ID3D11VideoDecoder ID3D11VideoContext" d3d12va_deps="dxva_h ID3D12Device ID3D12VideoDecoder" dxva2_deps="dxva2api_h DXVA2_ConfigPictureDecode ole32 user32" ffnvcodec_deps_any="libdl LoadLibrary" -mediacodec_deps="android mediandk" +mediacodec_deps="android mediandk pthreads" nvdec_deps="ffnvcodec" vaapi_x11_deps="xlib_x11" videotoolbox_hwaccel_deps="videotoolbox pthreads" diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c index e79fe29964..62bd7b4fda 100644 --- a/libavcodec/mediacodecenc.c +++ b/libavcodec/mediacodecenc.c @@ -23,11 +23,13 @@ #include "config_components.h" #include "libavutil/avassert.h" +#include "libavutil/fifo.h" #include "libavutil/avstring.h" #include "libavutil/hwcontext_mediacodec.h" #include "libavutil/imgutils.h" #include "libavutil/mem.h" #include "libavutil/opt.h" +#include "libavutil/thread.h" #include "avcodec.h" #include "bsf.h" @@ -78,6 +80,17 @@ typedef struct MediaCodecEncContext { int extract_extradata; // Ref. MediaFormat KEY_OPERATING_RATE int operating_rate; + int async_mode; + + AVMutex input_mutex; + AVCond input_cond; + AVFifo *input_index; + + AVMutex output_mutex; + AVCond output_cond; + int encode_status; + AVFifo *output_index; + AVFifo *output_buf_info; } MediaCodecEncContext; enum { @@ -102,17 +115,26 @@ static const enum AVPixelFormat avc_pix_fmts[] = { AV_PIX_FMT_NONE }; -static void mediacodec_output_format(AVCodecContext *avctx) +static void mediacodec_dump_format(AVCodecContext *avctx, + FFAMediaFormat *out_format) { MediaCodecEncContext *s = avctx->priv_data; - char *name = ff_AMediaCodec_getName(s->codec); - FFAMediaFormat *out_format = ff_AMediaCodec_getOutputFormat(s->codec); + const char *name = s->name; char *str = ff_AMediaFormat_toString(out_format); av_log(avctx, AV_LOG_DEBUG, "MediaCodec encoder %s output format %s\n", name ? name : "unknown", str); - av_free(name); av_free(str); +} + +static void mediacodec_output_format(AVCodecContext *avctx) +{ + MediaCodecEncContext *s = avctx->priv_data; + FFAMediaFormat *out_format = ff_AMediaCodec_getOutputFormat(s->codec); + + if (!s->name) + s->name = ff_AMediaCodec_getName(s->codec); + mediacodec_dump_format(avctx, out_format); ff_AMediaFormat_delete(out_format); } @@ -185,6 +207,135 @@ static int mediacodec_init_bsf(AVCodecContext *avctx) return ret; } +static void copy_frame_to_buffer(AVCodecContext *avctx, const AVFrame *frame, + uint8_t *dst, size_t size) +{ + MediaCodecEncContext *s = avctx->priv_data; + uint8_t *dst_data[4] = {}; + int dst_linesize[4] = {}; + + if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) { + dst_data[0] = dst; + dst_data[1] = dst + s->width * s->height; + dst_data[2] = dst_data[1] + s->width * s->height / 4; + + dst_linesize[0] = s->width; + dst_linesize[1] = dst_linesize[2] = s->width / 2; + } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) { + dst_data[0] = dst; + dst_data[1] = dst + s->width * s->height; + + dst_linesize[0] = s->width; + dst_linesize[1] = s->width; + } else { + av_assert0(0); + } + + av_image_copy2(dst_data, dst_linesize, frame->data, frame->linesize, + avctx->pix_fmt, avctx->width, avctx->height); +} + +static void on_input_available(FFAMediaCodec *codec, void *userdata, + int32_t index) +{ + AVCodecContext *avctx = userdata; + MediaCodecEncContext *s = avctx->priv_data; + + ff_mutex_lock(&s->input_mutex); + + av_fifo_write(s->input_index, &index, 1); + + ff_mutex_unlock(&s->input_mutex); + ff_cond_signal(&s->input_cond); +} + +static void on_output_available(FFAMediaCodec *codec, void *userdata, + int32_t index, + FFAMediaCodecBufferInfo *out_info) +{ + AVCodecContext *avctx = userdata; + MediaCodecEncContext *s = avctx->priv_data; + + ff_mutex_lock(&s->output_mutex); + + av_fifo_write(s->output_index, &index, 1); + av_fifo_write(s->output_buf_info, out_info, 1); + + ff_mutex_unlock(&s->output_mutex); + ff_cond_signal(&s->output_cond); +} + +static void on_format_changed(FFAMediaCodec *codec, void *userdata, + FFAMediaFormat *format) +{ + mediacodec_dump_format(userdata, format); +} + +static void on_error(FFAMediaCodec *codec, void *userdata, int error, + const char *detail) +{ + AVCodecContext *avctx = userdata; + MediaCodecEncContext *s = avctx->priv_data; + + if (error == AVERROR(EAGAIN)) + return; + + av_log(avctx, AV_LOG_ERROR, "On error, %s, %s\n", av_err2str(error), detail); + + ff_mutex_lock(&s->input_mutex); + ff_mutex_lock(&s->output_mutex); + s->encode_status = error; + ff_mutex_unlock(&s->output_mutex); + ff_mutex_unlock(&s->input_mutex); + + ff_cond_signal(&s->output_cond); + ff_cond_signal(&s->input_cond); +} + +static int mediacodec_init_async_state(AVCodecContext *avctx) +{ + MediaCodecEncContext *s = avctx->priv_data; + size_t fifo_size = 16; + + if (!s->async_mode) + return 0; + + ff_mutex_init(&s->input_mutex, NULL); + ff_cond_init(&s->input_cond, NULL); + + ff_mutex_init(&s->output_mutex, NULL); + ff_cond_init(&s->output_cond, NULL); + + s->input_index = av_fifo_alloc2(fifo_size, sizeof(int32_t), AV_FIFO_FLAG_AUTO_GROW); + s->output_index = av_fifo_alloc2(fifo_size, sizeof(int32_t), AV_FIFO_FLAG_AUTO_GROW); + s->output_buf_info = av_fifo_alloc2(fifo_size, sizeof(FFAMediaCodecBufferInfo), AV_FIFO_FLAG_AUTO_GROW); + + if (!s->input_index || !s->output_index || !s->output_buf_info) + return AVERROR(ENOMEM); + + return 0; +} + +static void mediacodec_uninit_async_state(AVCodecContext *avctx) +{ + MediaCodecEncContext *s = avctx->priv_data; + + if (!s->async_mode) + return; + + ff_mutex_destroy(&s->input_mutex); + ff_cond_destroy(&s->input_cond); + + ff_mutex_destroy(&s->output_mutex); + ff_cond_destroy(&s->output_cond); + + av_fifo_freep2(&s->input_index); + av_fifo_freep2(&s->output_index); + av_fifo_freep2(&s->output_buf_info); + + s->async_mode = 0; +} + static int mediacodec_generate_extradata(AVCodecContext *avctx); static av_cold int mediacodec_init(AVCodecContext *avctx) @@ -195,6 +346,11 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) int ret; int gop; + // Init async state first, so we can do cleanup safely on error path. + ret = mediacodec_init_async_state(avctx); + if (ret < 0) + return ret; + if (s->use_ndk_codec < 0) s->use_ndk_codec = !av_jni_get_java_vm(avctx); @@ -369,10 +525,21 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) goto bailout; } - ret = ff_AMediaCodec_start(s->codec); - if (ret) { - av_log(avctx, AV_LOG_ERROR, "MediaCodec failed to start, %s\n", av_err2str(ret)); - goto bailout; + if (s->async_mode) { + FFAMediaCodecOnAsyncNotifyCallback cb = { + .onAsyncInputAvailable = on_input_available, + .onAsyncOutputAvailable = on_output_available, + .onAsyncFormatChanged = on_format_changed, + .onAsyncError = on_error, + }; + + ret = ff_AMediaCodec_setAsyncNotifyCallback(s->codec, &cb, avctx); + if (ret < 0) { + av_log(avctx, AV_LOG_WARNING, + "Try MediaCodec async mode failed, %s, switch to sync mode\n", + av_err2str(ret)); + mediacodec_uninit_async_state(avctx); + } } ret = mediacodec_init_bsf(avctx); @@ -387,6 +554,13 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) goto bailout; } + ret = ff_AMediaCodec_start(s->codec); + if (ret) { + av_log(avctx, AV_LOG_ERROR, "MediaCodec failed to start, %s\n", + av_err2str(ret)); + goto bailout; + } + ret = mediacodec_generate_extradata(avctx); bailout: @@ -395,17 +569,62 @@ bailout: return ret; } +static int mediacodec_get_output_index(AVCodecContext *avctx, ssize_t *index, + FFAMediaCodecBufferInfo *out_info) +{ + MediaCodecEncContext *s = avctx->priv_data; + FFAMediaCodec *codec = s->codec; + int64_t timeout_us = s->eof_sent ? OUTPUT_DEQUEUE_TIMEOUT_US : 0; + int n; + int ret; + + if (!s->async_mode) { + *index = ff_AMediaCodec_dequeueOutputBuffer(codec, out_info, timeout_us); + return 0; + } + + ff_mutex_lock(&s->output_mutex); + + n = -1; + while (n < 0 && !s->encode_status) { + if (av_fifo_can_read(s->output_index)) { + av_fifo_read(s->output_index, &n, 1); + av_fifo_read(s->output_buf_info, out_info, 1); + break; + } + + // Only wait after signalEndOfInputStream + if (n < 0 && s->eof_sent && !s->encode_status) + ff_cond_wait(&s->output_cond, &s->output_mutex); + else + break; + } + + ret = s->encode_status; + *index = n; + ff_mutex_unlock(&s->output_mutex); + + // Get output index success + if (*index >= 0) + return 0; + + return ret ? ret : AVERROR(EAGAIN); +} + static int mediacodec_receive(AVCodecContext *avctx, AVPacket *pkt) { MediaCodecEncContext *s = avctx->priv_data; FFAMediaCodec *codec = s->codec; + ssize_t index; FFAMediaCodecBufferInfo out_info = {0}; uint8_t *out_buf; size_t out_size = 0; int ret; int extradata_size = 0; - int64_t timeout_us = s->eof_sent ? OUTPUT_DEQUEUE_TIMEOUT_US : 0; - ssize_t index = ff_AMediaCodec_dequeueOutputBuffer(codec, &out_info, timeout_us); + + ret = mediacodec_get_output_index(avctx, &index, &out_info); + if (ret < 0) + return ret; if (ff_AMediaCodec_infoTryAgainLater(codec, index)) return AVERROR(EAGAIN); @@ -480,33 +699,39 @@ bailout: return ret; } -static void copy_frame_to_buffer(AVCodecContext *avctx, const AVFrame *frame, uint8_t *dst, size_t size) +static int mediacodec_get_input_index(AVCodecContext *avctx, ssize_t *index) { MediaCodecEncContext *s = avctx->priv_data; - uint8_t *dst_data[4] = {}; - int dst_linesize[4] = {}; + FFAMediaCodec *codec = s->codec; + int ret = 0; + int32_t n; - if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) { - dst_data[0] = dst; - dst_data[1] = dst + s->width * s->height; - dst_data[2] = dst_data[1] + s->width * s->height / 4; + if (!s->async_mode) { + *index = ff_AMediaCodec_dequeueInputBuffer(codec, INPUT_DEQUEUE_TIMEOUT_US); + return 0; + } - dst_linesize[0] = s->width; - dst_linesize[1] = dst_linesize[2] = s->width / 2; - } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) { - dst_data[0] = dst; - dst_data[1] = dst + s->width * s->height; + ff_mutex_lock(&s->input_mutex); - dst_linesize[0] = s->width; - dst_linesize[1] = s->width; - } else { - av_assert0(0); + n = -1; + while (n < 0 && !s->encode_status) { + if (av_fifo_can_read(s->input_index) > 0) { + av_fifo_read(s->input_index, &n, 1); + break; + } + + if (n < 0 && !s->encode_status) + ff_cond_wait(&s->input_cond, &s->input_mutex); } - av_image_copy2(dst_data, dst_linesize, frame->data, frame->linesize, - avctx->pix_fmt, avctx->width, avctx->height); + ret = s->encode_status; + *index = n; + ff_mutex_unlock(&s->input_mutex); + + return ret; } + static int mediacodec_send(AVCodecContext *avctx, const AVFrame *frame) { MediaCodecEncContext *s = avctx->priv_data; @@ -516,7 +741,7 @@ static int mediacodec_send(AVCodecContext *avctx, size_t input_size = 0; int64_t pts = 0; uint32_t flags = 0; - int64_t timeout_us; + int ret; if (s->eof_sent) return 0; @@ -532,8 +757,10 @@ static int mediacodec_send(AVCodecContext *avctx, return 0; } - timeout_us = INPUT_DEQUEUE_TIMEOUT_US; - index = ff_AMediaCodec_dequeueInputBuffer(codec, timeout_us); + ret = mediacodec_get_input_index(avctx, &index); + if (ret < 0) + return ret; + if (ff_AMediaCodec_infoTryAgainLater(codec, index)) return AVERROR(EAGAIN); @@ -666,7 +893,8 @@ static int mediacodec_generate_extradata(AVCodecContext *avctx) if (!(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) return 0; - if (!s->extract_extradata) { + // Send dummy frame and receive a packet doesn't work in async mode + if (s->async_mode || !s->extract_extradata) { av_log(avctx, AV_LOG_WARNING, "Mediacodec encoder doesn't support AV_CODEC_FLAG_GLOBAL_HEADER. " "Use extract_extradata bsf when necessary.\n"); @@ -723,6 +951,8 @@ static av_cold int mediacodec_close(AVCodecContext *avctx) av_bsf_free(&s->bsf); av_frame_free(&s->frame); + mediacodec_uninit_async_state(avctx); + return 0; } @@ -753,6 +983,8 @@ static const AVCodecHWConfigInternal *const mediacodec_hw_configs[] = { #define COMMON_OPTION \ { "ndk_codec", "Use MediaCodec from NDK", \ OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \ + { "ndk_async", "Try NDK MediaCodec in async mode", \ + OFFSET(async_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VE }, \ { "codec_name", "Select codec by name", \ OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \ { "bitrate_mode", "Bitrate control method", \ diff --git a/libavcodec/version.h b/libavcodec/version.h index 9f55381cf1..db2a4f85f5 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -30,7 +30,7 @@ #include "version_major.h" #define LIBAVCODEC_VERSION_MINOR 22 -#define LIBAVCODEC_VERSION_MICRO 101 +#define LIBAVCODEC_VERSION_MICRO 102 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \