summaryrefslogtreecommitdiffstats
path: root/video/hwdec.h
blob: 1ffd5ca26607155407c9f16ab173ffbfff72ea03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef MP_HWDEC_H_
#define MP_HWDEC_H_

#include "options/m_option.h"

struct mp_image_pool;

// (for some legacy stuff)
enum hwdec_type {
    HWDEC_NONE = 0,
    HWDEC_VDPAU,
    HWDEC_VIDEOTOOLBOX,
    HWDEC_VAAPI,
    HWDEC_DXVA2,
    HWDEC_D3D11VA,
    HWDEC_CUDA,
};

struct mp_hwdec_ctx {
    enum hwdec_type type; // (never HWDEC_NONE)
    const char *driver_name; // NULL if unknown/not loaded

    // This is never NULL. Its meaning depends on the .type field:
    //  HWDEC_VDPAU:            struct mp_vdpau_ctx*
    //  HWDEC_VIDEOTOOLBOX:     non-NULL dummy pointer
    //  HWDEC_VAAPI:            struct mp_vaapi_ctx*
    //  HWDEC_D3D11VA:          ID3D11Device*
    //  HWDEC_DXVA2:            IDirect3DDevice9*
    //  HWDEC_CUDA:             CUcontext*
    void *ctx;

    // libavutil-wrapped context, if available.
    struct AVBufferRef *av_device_ref; // AVHWDeviceContext*

    // List of IMGFMT_s, terminated with 0. NULL if N/A.
    int *supported_formats;

    // Hint to generic code: it's using a wrapper API
    bool emulated;

    // Optional. Crap for vdpau. Makes sure preemption recovery is run if needed.
    void (*restore_device)(struct mp_hwdec_ctx *ctx);

    // Optional. Do not set for VO-bound devices.
    void (*destroy)(struct mp_hwdec_ctx *ctx);
};

// 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);

// Return the device context for the given API type. Returns NULL if none
// available. Logically, the returned pointer remains valid until VO
// uninitialization is started (all users of it must be uninitialized before).
// hwdec_devices_request() may be used before this to lazily load devices.
struct mp_hwdec_ctx *hwdec_devices_get(struct mp_hwdec_devices *devs,
                                       enum hwdec_type type);

struct AVBufferRef;

// Like hwdec_devices_get(), but search by AV_HWDEVICE_TYPE_* type.
// Contains a wrapped AVHWDeviceContext.
// Beware that this creates a _new_ reference.
struct AVBufferRef *hwdec_devices_get_lavc(struct mp_hwdec_devices *devs,
                                           int av_hwdevice_type);

// 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);

// 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);

// 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), void *load_api_ctx);

// Cause VO to lazily load all devices, and will block until this is done (even
// if not available).
void hwdec_devices_request_all(struct mp_hwdec_devices *devs);

// Convenience function:
// - return NULL if devs==NULL
// - call hwdec_devices_request(devs, type)
// - call hwdec_devices_get(devs, type)
// - then return the mp_hwdec_ctx.ctx field
void *hwdec_devices_load(struct mp_hwdec_devices *devs, enum hwdec_type type);

// 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;
    // Set any mp_image fields that require hwcontext specific code, such as
    // fields or flags not present in AVFrame or AVHWFramesContext in a
    // portable way. This is called directly after img is converted from an
    // AVFrame, with all other fields already set. img.hwctx will be set, and
    // use the correct AV_HWDEVICE_TYPE_.
    void (*complete_image_params)(struct mp_image *img);
    // 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);
};

// 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_d3d11;
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