e9ccdbf7263147cbb992c4682d716986671b27e2
[mesa.git] / src / gallium / state_trackers / va / va_private.h
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #ifndef VA_PRIVATE_H
30 #define VA_PRIVATE_H
31
32 #include <assert.h>
33
34 #include <va/va.h>
35 #include <va/va_backend.h>
36 #include <va/va_backend_vpp.h>
37 #include <va/va_drmcommon.h>
38
39 #include "pipe/p_video_enums.h"
40 #include "pipe/p_video_codec.h"
41 #include "pipe/p_video_state.h"
42
43 #include "vl/vl_compositor.h"
44 #include "vl/vl_csc.h"
45
46 #include "util/u_dynarray.h"
47 #include "os/os_thread.h"
48
49 #define VL_VA_DRIVER(ctx) ((vlVaDriver *)ctx->pDriverData)
50 #define VL_VA_PSCREEN(ctx) (VL_VA_DRIVER(ctx)->vscreen->pscreen)
51
52 #define VL_VA_MAX_IMAGE_FORMATS 9
53
54 static inline enum pipe_video_chroma_format
55 ChromaToPipe(int format)
56 {
57 switch (format) {
58 case VA_RT_FORMAT_YUV420:
59 return PIPE_VIDEO_CHROMA_FORMAT_420;
60 case VA_RT_FORMAT_YUV422:
61 return PIPE_VIDEO_CHROMA_FORMAT_422;
62 case VA_RT_FORMAT_YUV444:
63 return PIPE_VIDEO_CHROMA_FORMAT_444;
64 default:
65 return PIPE_VIDEO_CHROMA_FORMAT_NONE;
66 }
67 }
68
69 static inline enum pipe_format
70 VaFourccToPipeFormat(unsigned format)
71 {
72 switch(format) {
73 case VA_FOURCC('N','V','1','2'):
74 return PIPE_FORMAT_NV12;
75 case VA_FOURCC('I','4','2','0'):
76 return PIPE_FORMAT_IYUV;
77 case VA_FOURCC('Y','V','1','2'):
78 return PIPE_FORMAT_YV12;
79 case VA_FOURCC('Y','U','Y','V'):
80 return PIPE_FORMAT_YUYV;
81 case VA_FOURCC('U','Y','V','Y'):
82 return PIPE_FORMAT_UYVY;
83 case VA_FOURCC('B','G','R','A'):
84 return PIPE_FORMAT_B8G8R8A8_UNORM;
85 case VA_FOURCC('R','G','B','A'):
86 return PIPE_FORMAT_R8G8B8A8_UNORM;
87 case VA_FOURCC('B','G','R','X'):
88 return PIPE_FORMAT_B8G8R8X8_UNORM;
89 case VA_FOURCC('R','G','B','X'):
90 return PIPE_FORMAT_R8G8B8X8_UNORM;
91 default:
92 assert(0);
93 return PIPE_FORMAT_NONE;
94 }
95 }
96
97 static inline unsigned
98 PipeFormatToVaFourcc(enum pipe_format p_format)
99 {
100 switch (p_format) {
101 case PIPE_FORMAT_NV12:
102 return VA_FOURCC('N','V','1','2');
103 case PIPE_FORMAT_IYUV:
104 return VA_FOURCC('I','4','2','0');
105 case PIPE_FORMAT_YV12:
106 return VA_FOURCC('Y','V','1','2');
107 case PIPE_FORMAT_UYVY:
108 return VA_FOURCC('U','Y','V','Y');
109 case PIPE_FORMAT_YUYV:
110 return VA_FOURCC('Y','U','Y','V');
111 case PIPE_FORMAT_B8G8R8A8_UNORM:
112 return VA_FOURCC('B','G','R','A');
113 case PIPE_FORMAT_R8G8B8A8_UNORM:
114 return VA_FOURCC('R','G','B','A');
115 case PIPE_FORMAT_B8G8R8X8_UNORM:
116 return VA_FOURCC('B','G','R','X');
117 case PIPE_FORMAT_R8G8B8X8_UNORM:
118 return VA_FOURCC('R','G','B','X');
119 default:
120 assert(0);
121 return -1;
122 }
123 }
124
125 static inline VAProfile
126 PipeToProfile(enum pipe_video_profile profile)
127 {
128 switch (profile) {
129 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
130 return VAProfileMPEG2Simple;
131 case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
132 return VAProfileMPEG2Main;
133 case PIPE_VIDEO_PROFILE_MPEG4_SIMPLE:
134 return VAProfileMPEG4Simple;
135 case PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE:
136 return VAProfileMPEG4AdvancedSimple;
137 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
138 return VAProfileVC1Simple;
139 case PIPE_VIDEO_PROFILE_VC1_MAIN:
140 return VAProfileVC1Main;
141 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
142 return VAProfileVC1Advanced;
143 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
144 return VAProfileH264Baseline;
145 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
146 return VAProfileH264Main;
147 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
148 return VAProfileH264High;
149 case PIPE_VIDEO_PROFILE_HEVC_MAIN:
150 return VAProfileHEVCMain;
151 case PIPE_VIDEO_PROFILE_HEVC_MAIN_10:
152 return VAProfileHEVCMain10;
153 case PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED:
154 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
155 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH422:
156 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH444:
157 case PIPE_VIDEO_PROFILE_HEVC_MAIN_12:
158 case PIPE_VIDEO_PROFILE_HEVC_MAIN_STILL:
159 case PIPE_VIDEO_PROFILE_HEVC_MAIN_444:
160 case PIPE_VIDEO_PROFILE_UNKNOWN:
161 return VAProfileNone;
162 default:
163 assert(0);
164 return -1;
165 }
166 }
167
168 static inline enum pipe_video_profile
169 ProfileToPipe(VAProfile profile)
170 {
171 switch (profile) {
172 case VAProfileMPEG2Simple:
173 return PIPE_VIDEO_PROFILE_MPEG2_SIMPLE;
174 case VAProfileMPEG2Main:
175 return PIPE_VIDEO_PROFILE_MPEG2_MAIN;
176 case VAProfileMPEG4Simple:
177 return PIPE_VIDEO_PROFILE_MPEG4_SIMPLE;
178 case VAProfileMPEG4AdvancedSimple:
179 return PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE;
180 case VAProfileVC1Simple:
181 return PIPE_VIDEO_PROFILE_VC1_SIMPLE;
182 case VAProfileVC1Main:
183 return PIPE_VIDEO_PROFILE_VC1_MAIN;
184 case VAProfileVC1Advanced:
185 return PIPE_VIDEO_PROFILE_VC1_ADVANCED;
186 case VAProfileH264Baseline:
187 return PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE;
188 case VAProfileH264Main:
189 return PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN;
190 case VAProfileH264High:
191 return PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH;
192 case VAProfileHEVCMain:
193 return PIPE_VIDEO_PROFILE_HEVC_MAIN;
194 case VAProfileHEVCMain10:
195 return PIPE_VIDEO_PROFILE_HEVC_MAIN_10;
196 case VAProfileNone:
197 return PIPE_VIDEO_PROFILE_UNKNOWN;
198 default:
199 return PIPE_VIDEO_PROFILE_UNKNOWN;
200 }
201 }
202
203 typedef struct {
204 struct vl_screen *vscreen;
205 struct pipe_context *pipe;
206 struct handle_table *htab;
207 struct vl_compositor compositor;
208 struct vl_compositor_state cstate;
209 vl_csc_matrix csc;
210 pipe_mutex mutex;
211 } vlVaDriver;
212
213 typedef struct {
214 VAImage *image;
215
216 struct u_rect src_rect;
217 struct u_rect dst_rect;
218
219 struct pipe_sampler_view *sampler;
220 } vlVaSubpicture;
221
222 typedef struct {
223 struct pipe_video_codec templat, *decoder;
224 struct pipe_video_buffer *target;
225 union {
226 struct pipe_picture_desc base;
227 struct pipe_mpeg12_picture_desc mpeg12;
228 struct pipe_mpeg4_picture_desc mpeg4;
229 struct pipe_vc1_picture_desc vc1;
230 struct pipe_h264_picture_desc h264;
231 struct pipe_h265_picture_desc h265;
232 struct pipe_h264_enc_picture_desc h264enc;
233 } desc;
234
235 struct {
236 unsigned long long int frame_num;
237 unsigned int start_code_size;
238 unsigned int vti_bits;
239 unsigned int quant_scale;
240 VAPictureParameterBufferMPEG4 pps;
241 uint8_t start_code[32];
242 } mpeg4;
243
244 struct vl_deint_filter *deint;
245 struct vlVaBuffer *coded_buf;
246 int target_id;
247 } vlVaContext;
248
249 typedef struct {
250 enum pipe_video_profile profile;
251 enum pipe_video_entrypoint entrypoint;
252 enum pipe_h264_enc_rate_control_method rc;
253 } vlVaConfig;
254
255 typedef struct {
256 VABufferType type;
257 unsigned int size;
258 unsigned int num_elements;
259 void *data;
260 struct {
261 struct pipe_resource *resource;
262 struct pipe_transfer *transfer;
263 } derived_surface;
264 unsigned int export_refcount;
265 VABufferInfo export_state;
266 unsigned int coded_size;
267 } vlVaBuffer;
268
269 typedef struct {
270 struct pipe_video_buffer templat, *buffer;
271 struct util_dynarray subpics; /* vlVaSubpicture */
272 VAContextID ctx;
273 vlVaBuffer *coded_buf;
274 void *feedback;
275 unsigned int frame_num_cnt;
276 } vlVaSurface;
277
278 // Public functions:
279 VAStatus VA_DRIVER_INIT_FUNC(VADriverContextP ctx);
280
281 // vtable functions:
282 VAStatus vlVaTerminate(VADriverContextP ctx);
283 VAStatus vlVaQueryConfigProfiles(VADriverContextP ctx, VAProfile *profile_list,int *num_profiles);
284 VAStatus vlVaQueryConfigEntrypoints(VADriverContextP ctx, VAProfile profile,
285 VAEntrypoint *entrypoint_list, int *num_entrypoints);
286 VAStatus vlVaGetConfigAttributes(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
287 VAConfigAttrib *attrib_list, int num_attribs);
288 VAStatus vlVaCreateConfig(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
289 VAConfigAttrib *attrib_list, int num_attribs, VAConfigID *config_id);
290 VAStatus vlVaDestroyConfig(VADriverContextP ctx, VAConfigID config_id);
291 VAStatus vlVaQueryConfigAttributes(VADriverContextP ctx, VAConfigID config_id, VAProfile *profile,
292 VAEntrypoint *entrypoint, VAConfigAttrib *attrib_list, int *num_attribs);
293 VAStatus vlVaCreateSurfaces(VADriverContextP ctx, int width, int height, int format,
294 int num_surfaces, VASurfaceID *surfaces);
295 VAStatus vlVaDestroySurfaces(VADriverContextP ctx, VASurfaceID *surface_list, int num_surfaces);
296 VAStatus vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width, int picture_height,
297 int flag, VASurfaceID *render_targets, int num_render_targets, VAContextID *context);
298 VAStatus vlVaDestroyContext(VADriverContextP ctx, VAContextID context);
299 VAStatus vlVaCreateBuffer(VADriverContextP ctx, VAContextID context, VABufferType type, unsigned int size,
300 unsigned int num_elements, void *data, VABufferID *buf_id);
301 VAStatus vlVaBufferSetNumElements(VADriverContextP ctx, VABufferID buf_id, unsigned int num_elements);
302 VAStatus vlVaMapBuffer(VADriverContextP ctx, VABufferID buf_id, void **pbuf);
303 VAStatus vlVaUnmapBuffer(VADriverContextP ctx, VABufferID buf_id);
304 VAStatus vlVaDestroyBuffer(VADriverContextP ctx, VABufferID buffer_id);
305 VAStatus vlVaBeginPicture(VADriverContextP ctx, VAContextID context, VASurfaceID render_target);
306 VAStatus vlVaRenderPicture(VADriverContextP ctx, VAContextID context, VABufferID *buffers, int num_buffers);
307 VAStatus vlVaEndPicture(VADriverContextP ctx, VAContextID context);
308 VAStatus vlVaSyncSurface(VADriverContextP ctx, VASurfaceID render_target);
309 VAStatus vlVaQuerySurfaceStatus(VADriverContextP ctx, VASurfaceID render_target, VASurfaceStatus *status);
310 VAStatus vlVaQuerySurfaceError(VADriverContextP ctx, VASurfaceID render_target,
311 VAStatus error_status, void **error_info);
312 VAStatus vlVaPutSurface(VADriverContextP ctx, VASurfaceID surface, void* draw, short srcx, short srcy,
313 unsigned short srcw, unsigned short srch, short destx, short desty, unsigned short destw,
314 unsigned short desth, VARectangle *cliprects, unsigned int number_cliprects,
315 unsigned int flags);
316 VAStatus vlVaQueryImageFormats(VADriverContextP ctx, VAImageFormat *format_list, int *num_formats);
317 VAStatus vlVaQuerySubpictureFormats(VADriverContextP ctx, VAImageFormat *format_list,
318 unsigned int *flags, unsigned int *num_formats);
319 VAStatus vlVaCreateImage(VADriverContextP ctx, VAImageFormat *format, int width, int height, VAImage *image);
320 VAStatus vlVaDeriveImage(VADriverContextP ctx, VASurfaceID surface, VAImage *image);
321 VAStatus vlVaDestroyImage(VADriverContextP ctx, VAImageID image);
322 VAStatus vlVaSetImagePalette(VADriverContextP ctx, VAImageID image, unsigned char *palette);
323 VAStatus vlVaGetImage(VADriverContextP ctx, VASurfaceID surface, int x, int y,
324 unsigned int width, unsigned int height, VAImageID image);
325 VAStatus vlVaPutImage(VADriverContextP ctx, VASurfaceID surface, VAImageID image, int src_x, int src_y,
326 unsigned int src_width, unsigned int src_height, int dest_x, int dest_y,
327 unsigned int dest_width, unsigned int dest_height);
328 VAStatus vlVaQuerySubpictureFormats(VADriverContextP ctx, VAImageFormat *format_list,
329 unsigned int *flags, unsigned int *num_formats);
330 VAStatus vlVaCreateSubpicture(VADriverContextP ctx, VAImageID image, VASubpictureID *subpicture);
331 VAStatus vlVaDestroySubpicture(VADriverContextP ctx, VASubpictureID subpicture);
332 VAStatus vlVaSubpictureImage(VADriverContextP ctx, VASubpictureID subpicture, VAImageID image);
333 VAStatus vlVaSetSubpictureChromakey(VADriverContextP ctx, VASubpictureID subpicture,
334 unsigned int chromakey_min, unsigned int chromakey_max,
335 unsigned int chromakey_mask);
336 VAStatus vlVaSetSubpictureGlobalAlpha(VADriverContextP ctx, VASubpictureID subpicture, float global_alpha);
337 VAStatus vlVaAssociateSubpicture(VADriverContextP ctx, VASubpictureID subpicture, VASurfaceID *target_surfaces,
338 int num_surfaces, short src_x, short src_y,
339 unsigned short src_width, unsigned short src_height,
340 short dest_x, short dest_y, unsigned short dest_width, unsigned short dest_height,
341 unsigned int flags);
342 VAStatus vlVaDeassociateSubpicture(VADriverContextP ctx, VASubpictureID subpicture,
343 VASurfaceID *target_surfaces, int num_surfaces);
344 VAStatus vlVaQueryDisplayAttributes(VADriverContextP ctx, VADisplayAttribute *attr_list, int *num_attributes);
345 VAStatus vlVaGetDisplayAttributes(VADriverContextP ctx, VADisplayAttribute *attr_list, int num_attributes);
346 VAStatus vlVaSetDisplayAttributes(VADriverContextP ctx, VADisplayAttribute *attr_list, int num_attributes);
347 VAStatus vlVaBufferInfo(VADriverContextP ctx, VABufferID buf_id, VABufferType *type,
348 unsigned int *size, unsigned int *num_elements);
349 VAStatus vlVaLockSurface(VADriverContextP ctx, VASurfaceID surface, unsigned int *fourcc,
350 unsigned int *luma_stride, unsigned int *chroma_u_stride, unsigned int *chroma_v_stride,
351 unsigned int *luma_offset, unsigned int *chroma_u_offset, unsigned int *chroma_v_offset,
352 unsigned int *buffer_name, void **buffer);
353 VAStatus vlVaUnlockSurface(VADriverContextP ctx, VASurfaceID surface);
354 VAStatus vlVaCreateSurfaces2(VADriverContextP ctx, unsigned int format, unsigned int width, unsigned int height,
355 VASurfaceID *surfaces, unsigned int num_surfaces, VASurfaceAttrib *attrib_list,
356 unsigned int num_attribs);
357 VAStatus vlVaQuerySurfaceAttributes(VADriverContextP ctx, VAConfigID config, VASurfaceAttrib *attrib_list,
358 unsigned int *num_attribs);
359
360 VAStatus vlVaAcquireBufferHandle(VADriverContextP ctx, VABufferID buf_id, VABufferInfo *out_buf_info);
361 VAStatus vlVaReleaseBufferHandle(VADriverContextP ctx, VABufferID buf_id);
362
363 VAStatus vlVaQueryVideoProcFilters(VADriverContextP ctx, VAContextID context, VAProcFilterType *filters,
364 unsigned int *num_filters);
365 VAStatus vlVaQueryVideoProcFilterCaps(VADriverContextP ctx, VAContextID context, VAProcFilterType type,
366 void *filter_caps, unsigned int *num_filter_caps);
367 VAStatus vlVaQueryVideoProcPipelineCaps(VADriverContextP ctx, VAContextID context, VABufferID *filters,
368 unsigned int num_filters, VAProcPipelineCaps *pipeline_cap);
369
370 // internal functions
371 VAStatus vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
372 void vlVaGetReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id, struct pipe_video_buffer **ref_frame);
373 void vlVaHandlePictureParameterBufferMPEG12(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
374 void vlVaHandleIQMatrixBufferMPEG12(vlVaContext *context, vlVaBuffer *buf);
375 void vlVaHandleSliceParameterBufferMPEG12(vlVaContext *context, vlVaBuffer *buf);
376 void vlVaHandlePictureParameterBufferH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
377 void vlVaHandleIQMatrixBufferH264(vlVaContext *context, vlVaBuffer *buf);
378 void vlVaHandleSliceParameterBufferH264(vlVaContext *context, vlVaBuffer *buf);
379 void vlVaHandlePictureParameterBufferVC1(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
380 void vlVaHandleSliceParameterBufferVC1(vlVaContext *context, vlVaBuffer *buf);
381 void vlVaHandlePictureParameterBufferMPEG4(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
382 void vlVaHandleIQMatrixBufferMPEG4(vlVaContext *context, vlVaBuffer *buf);
383 void vlVaHandleSliceParameterBufferMPEG4(vlVaContext *context, vlVaBuffer *buf);
384 void vlVaDecoderFixMPEG4Startcode(vlVaContext *context);
385 void vlVaHandlePictureParameterBufferHEVC(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
386 void vlVaHandleIQMatrixBufferHEVC(vlVaContext *context, vlVaBuffer *buf);
387 void vlVaHandleSliceParameterBufferHEVC(vlVaContext *context, vlVaBuffer *buf);
388
389 #endif //VA_PRIVATE_H