avutil/hwcontext_mediacodec: fix backward compatibility

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>
This commit is contained in:
Zhao Zhili 2023-01-11 23:33:12 +08:00
parent 2c3107c3e9
commit 1263b0a6ca
2 changed files with 32 additions and 1 deletions

View File

@ -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;

View File

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