Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Zhao Zhili <quinkblack@foxmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Zhao Zhili <zhilizhao@tencent.com>
Subject: [FFmpeg-devel] [PATCH] avutil/hwcontext_mediacodec: fix backward compatibility
Date: Wed, 11 Jan 2023 23:33:12 +0800
Message-ID: <tencent_515F3F3B57459C4AE1165A2234BB80E23509@qq.com> (raw)

From: Zhao Zhili <zhilizhao@tencent.com>

AVMediaCodecDeviceContext without surface or native_window is
useless, it shouldn't be created at all. Such dummy AVHWDeviceContext
is allowed before, and it's used by mpv player. Creating a ANativeWindow
automatically breaks such usecases.

So disable creating a ANativeWindow by default. It can be enabled
via the create_window flag, or by set the AVDictionary of
av_hwdevice_ctx_create(). The downside is that

ffmpeg -hwaccel mediacodec -i input.mp4 \
	-c:a copy -c:v hevc_mediacodec output.mp4

use ByteBuffer mode which isn't as efficient as before. The upside
is libavfilter works now, which should be less surprise.

To enable create_window on ffmpeg command line, use
ffmpeg -hwaccel mediacodec \
	-init_hw_device mediacodec=mediacodec,create_window=1 \
	-i input.mp4 -c:a copy -c:v hevc_mediacodec output.mp4

Users should know what it is to enable create_window. It should
be OK to take sometime to figure out the option. And there are comments
inside hwcontext_mediacodec.h to help user figure it out.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
---
 libavutil/hwcontext_mediacodec.c | 17 +++++++++++++++++
 libavutil/hwcontext_mediacodec.h | 16 +++++++++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/libavutil/hwcontext_mediacodec.c b/libavutil/hwcontext_mediacodec.c
index bb1779d34d..9ed6a8717a 100644
--- a/libavutil/hwcontext_mediacodec.c
+++ b/libavutil/hwcontext_mediacodec.c
@@ -39,11 +39,23 @@ typedef struct MediaCodecDeviceContext {
 static int mc_device_create(AVHWDeviceContext *ctx, const char *device,
                             AVDictionary *opts, int flags)
 {
+    const AVDictionaryEntry *entry = NULL;
+    MediaCodecDeviceContext *s = ctx->hwctx;
+    AVMediaCodecDeviceContext *dev = &s->ctx;
+
     if (device && device[0]) {
         av_log(ctx, AV_LOG_ERROR, "Device selection unsupported.\n");
         return AVERROR_UNKNOWN;
     }
 
+    while ((entry = av_dict_iterate(opts, entry))) {
+        if (!strcmp(entry->key, "create_window"))
+            dev->create_window = atoi(entry->value);
+    }
+
+    av_log(ctx, AV_LOG_DEBUG, "%s createPersistentInputSurface\n",
+            dev->create_window ? "Enable" : "Disable");
+
     return 0;
 }
 
@@ -59,6 +71,11 @@ static int mc_device_init(AVHWDeviceContext *ctx)
     if (dev->native_window)
         return 0;
 
+    // For backward compatibility, don't return error for a dummy
+    // AVHWDeviceContext without surface or native_window.
+    if (!dev->create_window)
+        return 0;
+
     s->libmedia = dlopen("libmediandk.so", RTLD_NOW);
     if (!s->libmedia)
         return AVERROR_UNKNOWN;
diff --git a/libavutil/hwcontext_mediacodec.h b/libavutil/hwcontext_mediacodec.h
index 920e17764f..fc0263cabc 100644
--- a/libavutil/hwcontext_mediacodec.h
+++ b/libavutil/hwcontext_mediacodec.h
@@ -36,12 +36,26 @@ typedef struct AVMediaCodecDeviceContext {
      * Pointer to ANativeWindow.
      *
      * It both surface and native_window is NULL, try to create it
-     * automatically if OS support.
+     * automatically if create_window is true and OS support
+     * createPersistentInputSurface.
      *
      * It can be used as output surface for decoder and input surface for
      * encoder.
      */
     void *native_window;
+
+    /**
+     * Enable createPersistentInputSurface automatically.
+     *
+     * Disabled by default.
+     *
+     * It can be enabled by setting this flag directly, or by setting
+     * AVDictionary of av_hwdevice_ctx_create(), with "create_window" as key.
+     * The second method is useful for ffmpeg cmdline, e.g., we can enable it
+     * via:
+     *   -init_hw_device mediacodec=mediacodec,create_window=1
+     */
+    int create_window;
 } AVMediaCodecDeviceContext;
 
 #endif /* AVUTIL_HWCONTEXT_MEDIACODEC_H */
-- 
2.25.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

                 reply	other threads:[~2023-01-11  7:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_515F3F3B57459C4AE1165A2234BB80E23509@qq.com \
    --to=quinkblack@foxmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=zhilizhao@tencent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git