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 */