This is mostly for convenience, so that API users (including possibly
libavcodec) don't have to duplicate native format mapping tables.

Not sure if these should really have a device_ctx as argument. hw_format
is probably enough, but I wasn't sure whether future implementations
might possibly depend on runtime state. The intention is not to return
_supported_ formats only, so I don't foresee any actual need for a
device_ctx, but it might also be short-sighted to require one.

There is also the question what to do if multiple hwcontext
implementations support the same hw_format. Should (hw_format,
sw_format) be enough to uniquely map to a native format?
---
 libavutil/hwcontext.c          | 26 ++++++++++++++++++++++++
 libavutil/hwcontext.h          | 46 ++++++++++++++++++++++++++++++++++++++++++
 libavutil/hwcontext_internal.h |  8 ++++++++
 3 files changed, 80 insertions(+)

diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c
index d82df56abf..b9fc68f8d7 100644
--- a/libavutil/hwcontext.c
+++ b/libavutil/hwcontext.c
@@ -195,6 +195,32 @@ fail:
     return ret;
 }
 
+enum AVPixelFormat av_hwdevice_map_format_to_pixfmt(AVBufferRef *ref,
+                                                    enum AVPixelFormat 
hw_format,
+                                                    const void *native_format)
+{
+    AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
+    if (ctx->internal->hw_type->hw_format_to_pixfmt)
+        return ctx->internal->hw_type->hw_format_to_pixfmt(ctx, hw_format,
+                                                           native_format);
+
+    return AV_PIX_FMT_NONE;
+}
+
+int av_hwdevice_map_format_from_pixfmt(AVBufferRef *ref,
+                                       enum AVPixelFormat hw_format,
+                                       enum AVPixelFormat sw_format,
+                                       void *native_format)
+{
+    AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
+    if (ctx->internal->hw_type->hw_format_from_pixfmt)
+        return ctx->internal->hw_type->hw_format_from_pixfmt(ctx, hw_format,
+                                                             sw_format,
+                                                             native_format);
+
+    return AVERROR(ENOSYS);
+}
+
 static const AVClass hwframe_ctx_class = {
     .class_name = "AVHWFramesContext",
     .item_name  = av_default_item_name,
diff --git a/libavutil/hwcontext.h b/libavutil/hwcontext.h
index 203ea510ec..c1d5b560a0 100644
--- a/libavutil/hwcontext.h
+++ b/libavutil/hwcontext.h
@@ -322,6 +322,52 @@ int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,
                                    enum AVHWDeviceType type,
                                    AVBufferRef *src_ctx, int flags);
 
+/**
+ * This function converts a native format to a corresponding AVPixelFormat.
+ * The native format is specific to the hwcontext implementation, and usually
+ * depends on the underlying API and surface type. For example,
+ * AV_HWDEVICE_TYPE_DXVA2 could choose to use D3DFORMAT as type for
+ * AV_PIX_FMT_DXVA2_VLD surfaces. The corresponding header (hwcontext_*.h)
+ * documents the details.
+ *
+ * If mapping is not supported, or the format is not known, AV_PIX_FMT_NONE is
+ * returned.
+ *
+ * This API does not check whether the native format is supported for a 
specific
+ * purpose. For example, a specific GPU might not support surfaces with all
+ * formats the libavutil implementation knows about.
+ *
+ * @param device_ctx a reference to a AVHWDeviceContext.
+ * @param hw_format the wrapper format specifying the surface type (for example
+ *                  AV_PIX_FMT_DXVA2_VLD).
+ * @param native_format pointer to a variable of an implementation-dependent
+ *                      type containing the value of the native surface format.
+ * @return the equivalent software format (see AVHWFramesContext.sw_format),
+ *         or AV_PIX_FMT_NONE on failure.
+ */
+enum AVPixelFormat av_hwdevice_map_format_to_pixfmt(AVBufferRef *device_ctx,
+                                                    enum AVPixelFormat 
hw_format,
+                                                    const void *native_format);
+
+/**
+ * This function converts a AVPixelFormat into a corresponding native format.
+ * It does the reverse of av_hwdevice_map_format_to_pixfmt(). This returns an
+ * error code in failure. On success it sets the native_format parameter.
+ *
+ * @param device_ctx a reference to a AVHWDeviceContext.
+ * @param hw_format the wrapper format specifying the surface type (for example
+ *                  AV_PIX_FMT_DXVA2_VLD).
+ * @param sw_format a software format (like AVHWFramesContext.sw_format).
+ * @param native_format pointer to a variable of an implementation-dependent
+ *                      type containing the value of the native surface format.
+ *                      On success, it will be set to a format corresponding to
+ *                      sw_format for a surface type identified by hw_format.
+ * @return 0 on success, a negative AVERROR error code on failure.
+ */
+int av_hwdevice_map_format_from_pixfmt(AVBufferRef *device_ctx,
+                                       enum AVPixelFormat hw_format,
+                                       enum AVPixelFormat sw_format,
+                                       void *native_format);
 
 /**
  * Allocate an AVHWFramesContext tied to a given device context.
diff --git a/libavutil/hwcontext_internal.h b/libavutil/hwcontext_internal.h
index 99612931f2..b8731be2fe 100644
--- a/libavutil/hwcontext_internal.h
+++ b/libavutil/hwcontext_internal.h
@@ -72,6 +72,14 @@ typedef struct HWContextType {
     int              (*device_init)(AVHWDeviceContext *ctx);
     void             (*device_uninit)(AVHWDeviceContext *ctx);
 
+    enum AVPixelFormat (*hw_format_to_pixfmt)(AVHWDeviceContext *ctx,
+                                              enum AVPixelFormat hw_format,
+                                              const void *native_format);
+    int                (*hw_format_from_pixfmt)(AVHWDeviceContext *ctx,
+                                                enum AVPixelFormat hw_format,
+                                                enum AVPixelFormat sw_format,
+                                                void *native_format);
+
     int              (*frames_get_constraints)(AVHWDeviceContext *ctx,
                                                const void *hwconfig,
                                                AVHWFramesConstraints 
*constraints);
-- 
2.11.0

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to