mirror of
https://github.com/mpv-player/mpv
synced 2025-02-20 06:46:55 +00:00
Historically, we have not attempted to support hw->hw format conversion in the autoconvert logic. If a user needed to do these kinds of conversions, they needed to manually insert the hw format's conversion filter manually (eg: scale_vaapi). This was usually fine because the general rule is that any format supported by the hardware can be used as well as any other. ie: You would only need to do conversion if you have a specific goal in mind. However, we now have two situations where we can find ourselves with a hardware format being produced by a decoder that cannot be accepted by a VO via hwdec-interop: * dmabuf-wayland can only accept formats that the Wayland compositor accepts. In the case of GNOME, it can only accept a handful of RGB formats. * When decoding via VAAPI on Intel hardware, some of the more unusual video encodings (4:2:2, 10bit 4:4:4) lead to packed frame formats which gpu-next cannot handle, causing rendering to fail. In both these cases (at least when using VAAPI with dmabuf-wayland), if we could detect the failure case and insert a `scale_vaapi` filter, we could get successful output automatically. For `hwdec=drm`, there is currently no conversion filter, so dmabuf-wayland is still out of luck there. The basic approach to implementing this is to detect the case where we are evaluating a hardware format where the VO can accept the hardware format itself, but may not accept the underlying sw format. In the current code, we bypass autoconvert as soon as we see the hardware format is compatible. My first observation was that we actually have logic in autoconvert to detect when the input sw format is not in the list of allowed sw formats passed into the autoconverter. Unfortunately, we never populate this list, and the way you would expect to do that is vo-query-format returning sw format information, which it does not. We could define an extended vo-query-format-2, but we'd still need to implement the probing logic to fill it in. On the other hand, we already have the probing logic in the hwupload filter - and most recently I used that logic to implement conversion on upload. So if we could leverage that, we could both detect when hw->hw conversion is required, and pick the best target format. This exercise is then primarily one of detecting when we are in this case and letting that code run in a useful way. The hwupload filter is a bit awkward to work with today, and so I refactored a bunch of the set up code to actually make it more encapsulated. Now, instead of the caller instantiating it and then triggering the probe, we probe on creation and instantiate the correct underlying filter (hwupload vs conversion) automatically.
105 lines
4.0 KiB
C
105 lines
4.0 KiB
C
#ifndef MP_HWDEC_H_
|
|
#define MP_HWDEC_H_
|
|
|
|
#include <libavutil/buffer.h>
|
|
|
|
#include "options/m_option.h"
|
|
|
|
struct mp_image_pool;
|
|
|
|
struct mp_hwdec_ctx {
|
|
const char *driver_name; // NULL if unknown/not loaded
|
|
|
|
// libavutil-wrapped context, if available.
|
|
struct AVBufferRef *av_device_ref; // AVHWDeviceContext*
|
|
|
|
// List of allowed IMGFMT_s, terminated with 0.
|
|
// If NULL, all software formats are considered to be supported.
|
|
const int *supported_formats;
|
|
// HW format used by the hwdec
|
|
int hw_imgfmt;
|
|
|
|
// The name of this hwdec's matching conversion filter if available.
|
|
// This will be used for hardware conversion of frame formats.
|
|
// NULL otherwise.
|
|
const char *conversion_filter_name;
|
|
};
|
|
|
|
// Used to communicate hardware decoder device handles from VO to video decoder.
|
|
struct mp_hwdec_devices;
|
|
|
|
struct mp_hwdec_devices *hwdec_devices_create(void);
|
|
void hwdec_devices_destroy(struct mp_hwdec_devices *devs);
|
|
|
|
struct mp_hwdec_ctx *hwdec_devices_get_by_imgfmt(struct mp_hwdec_devices *devs,
|
|
int hw_imgfmt);
|
|
|
|
// For code which still strictly assumes there is 1 (or none) device.
|
|
struct mp_hwdec_ctx *hwdec_devices_get_first(struct mp_hwdec_devices *devs);
|
|
|
|
// Return the n-th device. NULL if none.
|
|
struct mp_hwdec_ctx *hwdec_devices_get_n(struct mp_hwdec_devices *devs, int n);
|
|
|
|
// Add this to the list of internal devices. Adding the same pointer twice must
|
|
// be avoided.
|
|
void hwdec_devices_add(struct mp_hwdec_devices *devs, struct mp_hwdec_ctx *ctx);
|
|
|
|
// Remove this from the list of internal devices. Idempotent/ignores entries
|
|
// not added yet. This is not thread-safe.
|
|
void hwdec_devices_remove(struct mp_hwdec_devices *devs, struct mp_hwdec_ctx *ctx);
|
|
|
|
struct hwdec_imgfmt_request {
|
|
int imgfmt;
|
|
bool probing;
|
|
};
|
|
|
|
// Can be used to enable lazy loading of an API with hwdec_devices_request().
|
|
// If used at all, this must be set/unset during initialization/uninitialization,
|
|
// as concurrent use with hwdec_devices_request() is a race condition.
|
|
void hwdec_devices_set_loader(struct mp_hwdec_devices *devs,
|
|
void (*load_api)(void *ctx, struct hwdec_imgfmt_request *params),
|
|
void *load_api_ctx);
|
|
|
|
// Cause VO to lazily load all devices for a specified img format, and will
|
|
// block until this is done (even if not available). Pass IMGFMT_NONE to load
|
|
// all available devices.
|
|
void hwdec_devices_request_for_img_fmt(struct mp_hwdec_devices *devs,
|
|
struct hwdec_imgfmt_request *params);
|
|
|
|
// Return "," concatenated list (for introspection/debugging). Use talloc_free().
|
|
char *hwdec_devices_get_names(struct mp_hwdec_devices *devs);
|
|
|
|
struct mp_image;
|
|
struct mpv_global;
|
|
|
|
struct hwcontext_create_dev_params {
|
|
bool probing; // if true, don't log errors if unavailable
|
|
};
|
|
|
|
// Per AV_HWDEVICE_TYPE_* functions, queryable via hwdec_get_hwcontext_fns().
|
|
// All entries are strictly optional.
|
|
struct hwcontext_fns {
|
|
int av_hwdevice_type;
|
|
// Fill in special format-specific requirements.
|
|
void (*refine_hwframes)(struct AVBufferRef *hw_frames_ctx);
|
|
// Returns a AVHWDeviceContext*. Used for copy hwdecs.
|
|
struct AVBufferRef *(*create_dev)(struct mpv_global *global,
|
|
struct mp_log *log,
|
|
struct hwcontext_create_dev_params *params);
|
|
// Return whether this is using some sort of sub-optimal emulation layer.
|
|
bool (*is_emulated)(struct AVBufferRef *hw_device_ctx);
|
|
};
|
|
|
|
// The parameter is of type enum AVHWDeviceType (as in int to avoid extensive
|
|
// recursive includes). May return NULL for unknown device types.
|
|
const struct hwcontext_fns *hwdec_get_hwcontext_fns(int av_hwdevice_type);
|
|
|
|
extern const struct hwcontext_fns hwcontext_fns_cuda;
|
|
extern const struct hwcontext_fns hwcontext_fns_d3d11;
|
|
extern const struct hwcontext_fns hwcontext_fns_drmprime;
|
|
extern const struct hwcontext_fns hwcontext_fns_dxva2;
|
|
extern const struct hwcontext_fns hwcontext_fns_vaapi;
|
|
extern const struct hwcontext_fns hwcontext_fns_vdpau;
|
|
|
|
#endif
|