Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avutil/hwcontext_mediacodec: fix backward compatibility
@ 2023-01-11 15:33 Zhao Zhili
  0 siblings, 0 replies; only message in thread
From: Zhao Zhili @ 2023-01-11 15:33 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Zhao Zhili

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".

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-11  7:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-11 15:33 [FFmpeg-devel] [PATCH] avutil/hwcontext_mediacodec: fix backward compatibility Zhao Zhili

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