mpv/video/out/gpu/hwdec.h

147 lines
5.7 KiB
C
Raw Normal View History

#ifndef MPGL_HWDEC_H_
#define MPGL_HWDEC_H_
#include "video/mp_image.h"
#include "ra.h"
#include "video/hwdec.h"
// Helper to organize/load hwdecs dynamically
struct ra_hwdec_ctx {
// Set these before calling `ra_hwdec_ctx_init`
struct mp_log *log;
struct mpv_global *global;
struct ra *ra;
bool loading_done;
struct ra_hwdec **hwdecs;
int num_hwdecs;
};
int ra_hwdec_validate_opt(struct mp_log *log, const m_option_t *opt,
struct bstr name, const char **value);
void ra_hwdec_ctx_init(struct ra_hwdec_ctx *ctx, struct mp_hwdec_devices *devs,
const char *opt, bool load_all_by_default);
void ra_hwdec_ctx_uninit(struct ra_hwdec_ctx *ctx);
void ra_hwdec_ctx_load_fmt(struct ra_hwdec_ctx *ctx, struct mp_hwdec_devices *devs,
struct hwdec_imgfmt_request *params);
// Gets the right `ra_hwdec` for a format, if any
struct ra_hwdec *ra_hwdec_get(struct ra_hwdec_ctx *ctx, int imgfmt);
struct ra_hwdec {
const struct ra_hwdec_driver *driver;
struct mp_log *log;
struct mpv_global *global;
struct ra *ra;
struct mp_hwdec_devices *devs;
// GLSL extensions required to sample textures from this.
const char **glsl_extensions;
// For free use by hwdec driver
void *priv;
// For working around the vdpau vs. vaapi mess.
bool probing;
// Used in overlay mode only.
float overlay_colorkey[4];
vo_opengl: refactor how hwdec interop exports textures Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a struct gl_hwdec_frame describing the exact texture layout. This gives more flexibility to what the hwdec interop can export. In particular, it can export strange component orders/permutations and textures with padded size. (The latter originating from cropped video.) The way gl_hwdec_frame works is in the spirit of the rest of the vo_opengl video processing code, which tends to put as much information in immediate state (as part of the dataflow), instead of declaring it globally. To some degree this duplicates the texplane and img_tex structs, but until we somehow unify those, it's better to give the hwdec state its own struct. The fact that changing the hwdec struct would require changes and testing on at least 4 platform/GPU combinations makes duplicating it almost a requirement to avoid pain later. Make gl_hwdec_driver.reinit set the new image format and remove the gl_hwdec.converted_imgfmt field. Likewise, gl_hwdec.gl_texture_target is replaced with gl_hwdec_plane.gl_target. Split out a init_image_desc function from init_format. The latter is not called in the hwdec case at all anymore. Setting up most of struct texplane is also completely separate in the hwdec and normal cases. video.c does not check whether the hwdec "mapped" image format is supported. This should not really happen anyway, and if it does, the hwdec interop backend must fail at creation time, so this is not an issue.
2016-05-10 16:29:10 +00:00
};
struct ra_hwdec_mapper {
const struct ra_hwdec_mapper_driver *driver;
struct mp_log *log;
struct ra *ra;
void *priv;
struct ra_hwdec *owner;
// Input frame parameters. (Set before init(), immutable.)
struct mp_image_params src_params;
// Output frame parameters (represents the format the textures return). Must
// be set by init(), immutable afterwards,
struct mp_image_params dst_params;
// The currently mapped source image (or the image about to be mapped in
// ->map()). NULL if unmapped. The mapper can also clear this reference if
// the mapped textures contain a full copy.
struct mp_image *src;
vo_opengl: refactor how hwdec interop exports textures Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a struct gl_hwdec_frame describing the exact texture layout. This gives more flexibility to what the hwdec interop can export. In particular, it can export strange component orders/permutations and textures with padded size. (The latter originating from cropped video.) The way gl_hwdec_frame works is in the spirit of the rest of the vo_opengl video processing code, which tends to put as much information in immediate state (as part of the dataflow), instead of declaring it globally. To some degree this duplicates the texplane and img_tex structs, but until we somehow unify those, it's better to give the hwdec state its own struct. The fact that changing the hwdec struct would require changes and testing on at least 4 platform/GPU combinations makes duplicating it almost a requirement to avoid pain later. Make gl_hwdec_driver.reinit set the new image format and remove the gl_hwdec.converted_imgfmt field. Likewise, gl_hwdec.gl_texture_target is replaced with gl_hwdec_plane.gl_target. Split out a init_image_desc function from init_format. The latter is not called in the hwdec case at all anymore. Setting up most of struct texplane is also completely separate in the hwdec and normal cases. video.c does not check whether the hwdec "mapped" image format is supported. This should not really happen anyway, and if it does, the hwdec interop backend must fail at creation time, so this is not an issue.
2016-05-10 16:29:10 +00:00
// The mapped textures and metadata about them. These fields change if a
// new frame is mapped (or unmapped), but otherwise remain constant.
// The common code won't mess with these, so you can e.g. set them in the
// .init() callback.
struct ra_tex *tex[4];
};
// This can be used to map frames of a specific hw format as GL textures.
struct ra_hwdec_mapper_driver {
// Used to create ra_hwdec_mapper.priv.
size_t priv_size;
// Init the mapper implementation. At this point, the field src_params,
// fns, devs, priv are initialized.
int (*init)(struct ra_hwdec_mapper *mapper);
// Destroy the mapper. unmap is called before this.
void (*uninit)(struct ra_hwdec_mapper *mapper);
// Map mapper->src as texture, and set mapper->frame to textures using it.
// It is expected that that the textures remain valid until the next unmap
// or uninit call.
// The function is allowed to unref mapper->src if it's not needed (i.e.
// this function creates a copy).
// The underlying format can change, so you might need to do some form
// of change detection. You also must reject unsupported formats with an
// error.
// On error, returns negative value on error and remains unmapped.
int (*map)(struct ra_hwdec_mapper *mapper);
// Unmap the frame. Does nothing if already unmapped. Optional.
void (*unmap)(struct ra_hwdec_mapper *mapper);
};
struct ra_hwdec_driver {
// Name of the interop backend. This is used for informational purposes and
// for use with debugging options.
const char *name;
// Used to create ra_hwdec.priv.
size_t priv_size;
// One of the hardware surface IMGFMT_ that must be passed to map_image later.
// Terminated with a 0 entry. (Extend the array size as needed.)
const int imgfmts[3];
// Create the hwdec device. It must add it to hw->devs, if applicable.
int (*init)(struct ra_hwdec *hw);
void (*uninit)(struct ra_hwdec *hw);
// This will be used to create a ra_hwdec_mapper from ra_hwdec.
const struct ra_hwdec_mapper_driver *mapper;
// The following function provides an alternative API. Each ra_hwdec_driver
// must have either provide a mapper or overlay_frame (not both or none), and
// if overlay_frame is set, it operates in overlay mode. In this mode,
// OSD etc. is rendered via OpenGL, but the video is rendered as a separate
// layer below it.
// Non-overlay mode is strictly preferred, so try not to use overlay mode.
// Set the given frame as overlay, replacing the previous one. This can also
// just change the position of the overlay.
// hw_image==src==dst==NULL is passed to clear the overlay.
int (*overlay_frame)(struct ra_hwdec *hw, struct mp_image *hw_image,
struct mp_rect *src, struct mp_rect *dst, bool newframe);
};
vo_gpu: make it possible to load multiple hwdec interop drivers Make the VO<->decoder interface capable of supporting multiple hwdec APIs at once. The main gain is that this simplifies autoprobing a lot. Before this change, it could happen that the VO loaded the "wrong" hwdec API, and the decoder was stuck with the choice (breaking hw decoding). With the change applied, the VO simply loads all available APIs, so autoprobing trickery is left entirely to the decoder. In the past, we were quite careful about not accidentally loading the wrong interop drivers. This was in part to make sure autoprobing works, but also because libva had this obnoxious bug of dumping garbage to stderr when using the API. libva was fixed, so this is not a problem anymore. The --opengl-hwdec-interop option is changed in various ways (again...), and renamed to --gpu-hwdec-interop. It does not have much use anymore, other than debugging. It's notable that the order in the hwdec interop array ra_hwdec_drivers[] still matters if multiple drivers support the same image formats, so the option can explicitly force one, if that should ever be necessary, or more likely, for debugging. One example are the ra_hwdec_d3d11egl and ra_hwdec_d3d11eglrgb drivers, which both support d3d11 input. vo_gpu now always loads the interop lazily by default, but when it does, it loads them all. vo_opengl_cb now always loads them when the GL context handle is initialized. I don't expect that this causes any problems. It's now possible to do things like changing between vdpau and nvdec decoding at runtime. This is also preparation for cleaning up vd_lavc.c hwdec autoprobing. It's another reason why hwdec_devices_request_all() does not take a hwdec type anymore.
2017-12-01 04:05:00 +00:00
extern const struct ra_hwdec_driver *const ra_hwdec_drivers[];
struct ra_hwdec *ra_hwdec_load_driver(struct ra *ra, struct mp_log *log,
struct mpv_global *global,
struct mp_hwdec_devices *devs,
const struct ra_hwdec_driver *drv,
bool is_auto);
void ra_hwdec_uninit(struct ra_hwdec *hwdec);
bool ra_hwdec_test_format(struct ra_hwdec *hwdec, int imgfmt);
struct ra_hwdec_mapper *ra_hwdec_mapper_create(struct ra_hwdec *hwdec,
struct mp_image_params *params);
void ra_hwdec_mapper_free(struct ra_hwdec_mapper **mapper);
void ra_hwdec_mapper_unmap(struct ra_hwdec_mapper *mapper);
int ra_hwdec_mapper_map(struct ra_hwdec_mapper *mapper, struct mp_image *img);
#endif