[FFmpeg-devel] [PATCH v2 0/4] Add derive-device function which searches for existing devices in both directions
ffmpegagent
ffmpegagent at gmail.com
Sat May 21 17:07:38 EEST 2022
This is an updated version of: [PATCH v4 1/1] avutils/hwcontext: When
deriving a hwdevice, search for existing device in both directions
There has been an objection that the earlier patchset would change API
behavior, and that this change should be limited to ffmpeg cli.
To achieve this, the API behavior is left unchanged now and a new function
av_hwdevice_ctx_get_or_create_derived() is added and used by the hwupload
and hwmap filters.
v2: Implemented concept for "weak references" to avoid circular reference
lockup.
softworkz (4):
avutil/buffer: add av_ref_from_buffer() function
avutils/hwcontext: add derive-device function which searches for
existing devices in both directions
lavu: bump minor version and add doc/APIchanges entry for
av_hwdevice_ctx_get_or_create_derived()
avfilter/hwmap,hwupload: use new av_hwdevice_ctx_get_or_create_derived
method
doc/APIchanges | 6 ++
libavfilter/vf_hwmap.c | 4 +-
libavfilter/vf_hwupload.c | 2 +-
libavutil/buffer.c | 16 ++++
libavutil/buffer.h | 8 ++
libavutil/hwcontext.c | 167 +++++++++++++++++++++++++++++++--
libavutil/hwcontext.h | 20 ++++
libavutil/hwcontext_internal.h | 11 +++
libavutil/hwcontext_qsv.c | 11 ++-
libavutil/version.h | 4 +-
10 files changed, 234 insertions(+), 15 deletions(-)
base-commit: 9ab20b1614194280b862d98dfcdb7b1bcff03329
Published-As: https://github.com/ffstaging/FFmpeg/releases/tag/pr-ffstaging-25%2Fsoftworkz%2Fderive_devices-v2
Fetch-It-Via: git fetch https://github.com/ffstaging/FFmpeg pr-ffstaging-25/softworkz/derive_devices-v2
Pull-Request: https://github.com/ffstaging/FFmpeg/pull/25
Range-diff vs v1:
-: ---------- > 1: e9c920c237 avutil/buffer: add av_ref_from_buffer() function
1: 85ff784b6f ! 2: bec8ccfcc2 avutils/hwcontext: add derive-device function which searches for existing devices in both directions
@@ Commit message
Signed-off-by: softworkz <softworkz at hotmail.com>
## libavutil/hwcontext.c ##
-@@ libavutil/hwcontext.c: static const AVClass hwdevice_ctx_class = {
- static void hwdevice_ctx_free(void *opaque, uint8_t *data)
+@@
+ #include "buffer.h"
+ #include "common.h"
+ #include "hwcontext.h"
++
++#include <stdatomic.h>
++
++#include "buffer_internal.h"
+ #include "hwcontext_internal.h"
+ #include "imgutils.h"
+ #include "log.h"
+ #include "mem.h"
+ #include "pixdesc.h"
+ #include "pixfmt.h"
++#include "thread.h"
+
+ static const HWContextType * const hw_table[] = {
+ #if CONFIG_CUDA
+@@ libavutil/hwcontext.c: static const char *const hw_type_names[] = {
+ [AV_HWDEVICE_TYPE_VULKAN] = "vulkan",
+ };
+
++#define DEVICE_REGISTRY_SIZE 1024
++
++static AVMutex mutex;
++static int is_mutex_initialized = 0;
++static int max_device_reg_id = 1;
++static AVBuffer *hw_device_registry[DEVICE_REGISTRY_SIZE];
++
++static int register_hw_device(const AVBufferRef *ref)
++{
++ AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
++ const int reg_id = max_device_reg_id;
++
++ if (ctx == NULL)
++ return AVERROR(EINVAL);
++
++ if (!is_mutex_initialized) {
++ int ret;
++ ret = ff_mutex_init(&mutex, NULL);
++ if (ret) {
++ av_log(ctx, AV_LOG_ERROR, "hwcontext: mutex initialization failed! Error code: %d\n", ret);
++ return AVERROR(EINVAL);
++ }
++
++ is_mutex_initialized = 1;
++ }
++
++ ff_mutex_lock(&mutex);
++
++ for (int i = 0; i < max_device_reg_id; ++i) {
++ if (hw_device_registry[i] != NULL && hw_device_registry[i] == ref->buffer) {
++ ff_mutex_unlock(&mutex);
++ return i;
++ }
++ }
++
++ if (max_device_reg_id >= DEVICE_REGISTRY_SIZE) {
++ ff_mutex_unlock(&mutex);
++ av_log(ctx, AV_LOG_ERROR, "Device registry limit (%d) reached. Please check for excessive device creation.", DEVICE_REGISTRY_SIZE);
++ return AVERROR(ENOMEM);
++ }
++
++ hw_device_registry[reg_id] = ref->buffer;
++ max_device_reg_id++;
++
++ ff_mutex_unlock(&mutex);
++
++ return reg_id;
++}
++
++static void unregister_hw_device(const AVHWDeviceContext *ctx)
++{
++ if (ctx == NULL || !is_mutex_initialized)
++ return;
++
++ ff_mutex_lock(&mutex);
++
++ hw_device_registry[ctx->internal->registered_device_id] = NULL;
++
++ ff_mutex_unlock(&mutex);
++}
++
++static AVBufferRef *get_registered_hw_device(int registered_id)
++{
++ if (registered_id <= 0 || registered_id >= max_device_reg_id)
++ return NULL;
++
++ ff_mutex_lock(&mutex);
++
++ if (hw_device_registry[registered_id] != NULL && hw_device_registry[registered_id]->data != NULL) {
++ AVBufferRef *ref = av_ref_from_buffer(hw_device_registry[registered_id]);
++ return ref;
++ }
++
++ ff_mutex_unlock(&mutex);
++
++ return NULL;
++}
++
+ enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
+ {
+ int type;
+@@ libavutil/hwcontext.c: static void hwdevice_ctx_free(void *opaque, uint8_t *data)
{
AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
-+ int i;
++ unregister_hw_device(ctx);
++
/* uninit might still want access the hw context and the user
* free() callback might destroy it, so uninit has to be called first */
-@@ libavutil/hwcontext.c: static void hwdevice_ctx_free(void *opaque, uint8_t *data)
- ctx->free(ctx);
+ if (ctx->internal->hw_type->device_uninit)
+@@ libavutil/hwcontext.c: int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
+ const char *device, AVDictionary *opts, int flags)
+ {
+ AVBufferRef *device_ref = NULL;
+- AVHWDeviceContext *device_ctx;
++ AVHWDeviceContext *device_ctx = NULL;
+ int ret = 0;
- av_buffer_unref(&ctx->internal->source_device);
-+ for (i = 0; i < AV_HWDEVICE_TYPE_NB; i++)
-+ av_buffer_unref(&ctx->internal->derived_devices[i]);
+ device_ref = av_hwdevice_ctx_alloc(type);
+@@ libavutil/hwcontext.c: int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
+ if (ret < 0)
+ goto fail;
- av_freep(&ctx->hwctx);
- av_freep(&ctx->internal->priv);
-@@ libavutil/hwcontext.c: fail:
++ ret = register_hw_device(device_ref);
++ if (ret < 0)
++ goto fail;
++
+ ret = av_hwdevice_ctx_init(device_ref);
+ if (ret < 0)
+ goto fail;
+
++ device_ctx->internal->registered_device_id = ret;
++
+ *pdevice_ref = device_ref;
+ return 0;
+ fail:
++ unregister_hw_device(device_ctx);
+ av_buffer_unref(&device_ref);
+ *pdevice_ref = NULL;
return ret;
}
@@ libavutil/hwcontext.c: fail:
- AVDictionary *options, int flags)
+static AVBufferRef* find_derived_hwdevice_ctx(AVBufferRef *src_ref, enum AVHWDeviceType type)
+{
-+ AVBufferRef *tmp_ref;
++ AVBufferRef *derived_ref;
+ AVHWDeviceContext *src_ctx;
+ int i;
+
@@ libavutil/hwcontext.c: fail:
+ return src_ref;
+
+ for (i = 0; i < AV_HWDEVICE_TYPE_NB; i++)
-+ if (src_ctx->internal->derived_devices[i]) {
-+ tmp_ref = find_derived_hwdevice_ctx(src_ctx->internal->derived_devices[i], type);
-+ if (tmp_ref)
-+ return tmp_ref;
++ if (src_ctx->internal->derived_device_ids[i]) {
++ AVBufferRef *tmp_ref = get_registered_hw_device(src_ctx->internal->derived_device_ids[i]);
++
++ if (tmp_ref) {
++ derived_ref = find_derived_hwdevice_ctx(tmp_ref, type);
++
++ if (tmp_ref != derived_ref)
++ av_buffer_unref(&tmp_ref);
++
++ if (derived_ref)
++ return derived_ref;
++ }
+ }
+
+ return NULL;
@@ libavutil/hwcontext.c: int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst
ret = AVERROR(ENOMEM);
goto fail;
}
-+ if (!tmp_ctx->internal->derived_devices[type]) {
-+ tmp_ctx->internal->derived_devices[type] = av_buffer_ref(dst_ref);
-+ if (!tmp_ctx->internal->derived_devices[type]) {
-+ ret = AVERROR(ENOMEM);
-+ goto fail;
-+ }
-+ }
++ if (!tmp_ctx->internal->derived_device_ids[type])
++ tmp_ctx->internal->derived_device_ids[type] = dst_ctx->internal->registered_device_id;
++
ret = av_hwdevice_ctx_init(dst_ref);
if (ret < 0)
goto fail;
@@ libavutil/hwcontext_internal.h: struct AVHWDeviceInternal {
AVBufferRef *source_device;
+
+ /**
-+ * An array of reference to device contexts which
++ * An array of device registration ids from device contexts which
+ * were derived from this device.
+ */
-+ AVBufferRef *derived_devices[AV_HWDEVICE_TYPE_NB];
++ int derived_device_ids[AV_HWDEVICE_TYPE_NB];
++
++ /**
++ * ID under wich the hw context is registered internally.
++ */
++ int registered_device_id;
};
struct AVHWFramesInternal {
@@ libavutil/hwcontext_qsv.c: static int qsv_device_create(AVHWDeviceContext *ctx,
+ ret = qsv_device_derive_from_child(ctx, impl, child_device, 0);
+ if (ret >= 0) {
+ ctx->internal->source_device = av_buffer_ref(priv->child_device_ctx);
-+ child_device->internal->derived_devices[ctx->type] = av_buffer_create((uint8_t*)ctx, sizeof(*ctx), qsv_release_dummy, ctx, 0);
-+ if (!child_device->internal->derived_devices[ctx->type])
-+ return AVERROR(ENOMEM);
++ child_device->internal->derived_device_ids[ctx->type] = ctx->internal->registered_device_id;
+ }
- return qsv_device_derive_from_child(ctx, impl, child_device, 0);
2: 8c6a1a8b8c ! 3: a2c8e79e8a lavu: bump minor version and add doc/APIchanges entry for av_hwdevice_ctx_get_or_create_derived()
@@ doc/APIchanges: libavutil: 2021-04-27
API changes, most recent first:
-+2022-04-30 - xxxxxxxxxx - lavu 57.25.100 - hwcontext.h
++2022-05-21 - xxxxxxxxxx - lavu 57.25.100 - hwcontext.h
+ Add av_hwdevice_ctx_get_or_create_derived().
++
++2022-04-30 - xxxxxxxxxx - lavu 57.25.100 - buffer.h
++ Add av_ref_from_buffer().
+
2022-03-16 - xxxxxxxxxx - all libraries - version_major.h
Add lib<name>/version_major.h as new installed headers, which only
3: 023597a557 = 4: d0957afde6 avfilter/hwmap,hwupload: use new av_hwdevice_ctx_get_or_create_derived method
--
ffmpeg-codebot
More information about the ffmpeg-devel
mailing list