1 /*****************************************************************************
2  * vlc_vaapi.h: VAAPI helper for VLC
3  *****************************************************************************
4  * Copyright (C) 2017 VLC authors, VideoLAN and VideoLabs
5  *
6  * Authors: Thomas Guillem <thomas@gllm.fr>
7  *          Petri Hintukainen <phintuka@gmail.com>
8  *          Victorien Le Couviour--Tuffet <victorien.lecouviour.tuffet@gmail.com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 
25 #ifndef VLC_VAAPI_H
26 # define VLC_VAAPI_H
27 
28 #include <va/va.h>
29 
30 #ifndef VA_RT_FORMAT_YUV420_10BPP
31 # define VA_RT_FORMAT_YUV420_10BPP 0x00000100
32 #endif
33 
34 #ifndef VA_FOURCC_P010
35 #define VA_FOURCC_P010 0x30313050
36 #endif
37 
38 #include <vlc_common.h>
39 #include <vlc_fourcc.h>
40 #include <vlc_picture_pool.h>
41 
42 /**************************
43  * VA instance management *
44  **************************/
45 
46 typedef void (*vlc_vaapi_native_destroy_cb)(VANativeDisplay);
47 struct vlc_vaapi_instance;
48 
49 /* Initializes the VADisplay and sets the reference counter to 1. If not NULL,
50  * native_destroy_cb will be called when the instance is released in order to
51  * destroy the native holder (that can be a drm/x11/wl). On error, dpy is
52  * terminated and the destroy callback is called. */
53 struct vlc_vaapi_instance *
54 vlc_vaapi_InitializeInstance(vlc_object_t *o, VADisplay dpy,
55                              VANativeDisplay native,
56                              vlc_vaapi_native_destroy_cb native_destroy_cb);
57 
58 /* Get and Initializes a VADisplay from a DRM device. If device is NULL, this
59  * function will try to open default devices. */
60 struct vlc_vaapi_instance *
61 vlc_vaapi_InitializeInstanceDRM(vlc_object_t *o,
62                                 VADisplay (*pf_getDisplayDRM)(int),
63                                 VADisplay *pdpy, const char *device);
64 
65 
66 /* Increments the VAAPI instance refcount */
67 VADisplay
68 vlc_vaapi_HoldInstance(struct vlc_vaapi_instance *inst);
69 
70 /* Decrements the VAAPI instance refcount, and call vaTerminate if that counter
71  * reaches 0 */
72 void
73 vlc_vaapi_ReleaseInstance(struct vlc_vaapi_instance *inst);
74 
75 /**************************
76  * VAAPI create & destroy *
77  **************************/
78 
79 /* Creates a VA context from the VA configuration and the width / height of the
80  * pictures to process. */
81 VAContextID
82 vlc_vaapi_CreateContext(vlc_object_t *o, VADisplay dpy, VAConfigID conf,
83                         int pic_w, int pic_h, int flag,
84                         VASurfaceID *render_targets, int num_render_targets);
85 
86 /* Creates a VA buffer for 'num_elements' elements of 'size' bytes and
87  * initalized with 'data'. If 'data' is NULL, then the content of the buffer is
88  * undefined. */
89 VABufferID
90 vlc_vaapi_CreateBuffer(vlc_object_t *o, VADisplay dpy, VAContextID ctx,
91                        VABufferType type, unsigned int size,
92                        unsigned int num_elements, void *data);
93 
94 /* Creates a VA image from a VA surface. */
95 int
96 vlc_vaapi_DeriveImage(vlc_object_t *o, VADisplay dpy,
97                       VASurfaceID surface, VAImage *image);
98 
99 /* Creates a VA image */
100 int
101 vlc_vaapi_CreateImage(vlc_object_t *o, VADisplay dpy, VAImageFormat *format,
102                       int width, int height, VAImage *image);
103 
104 /* Destroys a VA configuration. */
105 int
106 vlc_vaapi_DestroyConfig(vlc_object_t *o, VADisplay dpy, VAConfigID conf);
107 
108 /* Destroys a VA context. */
109 int
110 vlc_vaapi_DestroyContext(vlc_object_t *o, VADisplay dpy, VAContextID ctx);
111 
112 /* Destroys a VA buffer. */
113 int
114 vlc_vaapi_DestroyBuffer(vlc_object_t *o, VADisplay dpy, VABufferID buf);
115 
116 /* Destroys a VA image. */
117 int
118 vlc_vaapi_DestroyImage(vlc_object_t *o, VADisplay dpy, VAImageID image);
119 
120 /***********************
121  * VAAPI buffer access *
122  ***********************/
123 
124 /* Maps the specified buffer to '*p_buf'. */
125 int
126 vlc_vaapi_MapBuffer(vlc_object_t *o, VADisplay dpy,
127                     VABufferID buf_id, void **p_buf);
128 
129 /* Unmaps the specified buffer so that the driver can read from it. */
130 int
131 vlc_vaapi_UnmapBuffer(vlc_object_t *o, VADisplay dpy, VABufferID buf_id);
132 
133 int
134 vlc_vaapi_AcquireBufferHandle(vlc_object_t *o, VADisplay dpy, VABufferID buf_id,
135                               VABufferInfo *buf_info);
136 
137 int
138 vlc_vaapi_ReleaseBufferHandle(vlc_object_t *o, VADisplay dpy, VABufferID buf_id);
139 
140 /*****************
141  * VAAPI queries *
142  *****************/
143 
144 /* Checks if the specified filter is available. */
145 int
146 vlc_vaapi_IsVideoProcFilterAvailable(vlc_object_t *o,
147                                      VADisplay dpy, VAContextID ctx,
148                                      VAProcFilterType filter);
149 
150 /* Retrieves the list of available capabilities of a filter. */
151 int
152 vlc_vaapi_QueryVideoProcFilterCaps(vlc_object_t *o, VADisplay dpy,
153                                    VAContextID ctx,
154                                    VAProcFilterType filter, void *caps,
155                                    unsigned int *p_num_caps);
156 
157 /* Retrieves the available capabilities of the pipeline. */
158 int
159 vlc_vaapi_QueryVideoProcPipelineCaps(vlc_object_t *o, VADisplay dpy,
160                                      VAContextID ctx, VABufferID *filters,
161                                      unsigned int num_filters,
162                                      VAProcPipelineCaps *pipeline_caps);
163 
164 /*******************
165  * VAAPI rendering *
166  *******************/
167 
168 /* Tells the driver the specified surface is the next surface to render. */
169 int
170 vlc_vaapi_BeginPicture(vlc_object_t *o, VADisplay dpy,
171                        VAContextID ctx, VASurfaceID surface);
172 
173 /* Send buffers (describing rendering operations to perform on the current
174  * surface) to the driver, which are automatically destroyed afterwards. */
175 int
176 vlc_vaapi_RenderPicture(vlc_object_t *o, VADisplay dpy, VAContextID ctx,
177                         VABufferID *buffers, int num_buffers);
178 
179 /* Tells the driver it can begins to process all the pending operations
180  * (specified with vlc_vaapi_RenderPicture) on the current surface. */
181 int
182 vlc_vaapi_EndPicture(vlc_object_t *o, VADisplay dpy, VAContextID ctx);
183 
184 /*****************
185  * VAAPI helpers *
186  *****************/
187 
188 /* Creates a VAConfigID */
189 VAConfigID
190 vlc_vaapi_CreateConfigChecked(vlc_object_t *o, VADisplay dpy,
191                               VAProfile i_profile, VAEntrypoint entrypoint,
192                               int i_force_vlc_chroma);
193 
194 /* Create a pool backed by VASurfaceID. render_targets will destroyed once
195  * the pool and every pictures are released. */
196 picture_pool_t *
197 vlc_vaapi_PoolNew(vlc_object_t *o, struct vlc_vaapi_instance *vainst,
198                   VADisplay dpy, unsigned count, VASurfaceID **render_targets,
199                   const video_format_t *restrict fmt, bool b_force_fourcc);
200 
201 /* Get render targets from a pic_sys allocated by the vaapi pool (see
202  * vlc_vaapi_PoolNew()) */
203 unsigned
204 vlc_vaapi_PicSysGetRenderTargets(picture_sys_t *sys,
205                                  VASurfaceID **render_targets);
206 
207 /* Get and hold the VADisplay instance attached to the picture sys */
208 struct vlc_vaapi_instance *
209 vlc_vaapi_PicSysHoldInstance(picture_sys_t *sys, VADisplay *dpy);
210 
211 /* Attachs the VASurface to the picture context, the picture must be allocated
212  * by a vaapi pool (see vlc_vaapi_PoolNew()) */
213 void
214 vlc_vaapi_PicAttachContext(picture_t *pic);
215 
216 /* Get the VASurfaceID attached to the pic */
217 VASurfaceID
218 vlc_vaapi_PicGetSurface(picture_t *pic);
219 
220 /* Get the VADisplay attached to the pic (valid while the pic is alive) */
221 VADisplay
222 vlc_vaapi_PicGetDisplay(picture_t *pic);
223 
224 static inline bool
vlc_vaapi_IsChromaOpaque(int i_vlc_chroma)225 vlc_vaapi_IsChromaOpaque(int i_vlc_chroma)
226 {
227     return i_vlc_chroma == VLC_CODEC_VAAPI_420
228         || i_vlc_chroma == VLC_CODEC_VAAPI_420_10BPP;
229 }
230 
231 #endif /* VLC_VAAPI_H */
232