[FFmpeg-devel] [PATCH] avutil/hwcontext_mediacodec: fix backward compatibility

Zhao Zhili quinkblack at foxmail.com
Wed Jan 11 17:33:12 EET 2023


From: Zhao Zhili <zhilizhao at 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 at 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



More information about the ffmpeg-devel mailing list