a437a5b90f1ae58125673a9a312ed88d6f54963e
[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 11
53 #define VL_VA_ENC_GOP_COEFF 16
54
55 #define UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
56 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
57
58 static inline unsigned handle_hash(void *key)
59 {
60 return PTR_TO_UINT(key);
61 }
62
63 static inline int handle_compare(void *key1, void *key2)
64 {
65 return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
66 }
67
68 static inline enum pipe_video_chroma_format
69 ChromaToPipe(int format)
70 {
71 switch (format) {
72 case VA_RT_FORMAT_YUV420:
73 case VA_RT_FORMAT_YUV420_10BPP:
74 return PIPE_VIDEO_CHROMA_FORMAT_420;
75 case VA_RT_FORMAT_YUV422:
76 return PIPE_VIDEO_CHROMA_FORMAT_422;
77 case VA_RT_FORMAT_YUV444:
78 return PIPE_VIDEO_CHROMA_FORMAT_444;
79 default:
80 return PIPE_VIDEO_CHROMA_FORMAT_NONE;
81 }
82 }
83
84 static inline enum pipe_format
85 VaFourccToPipeFormat(unsigned format)
86 {
87 switch(format) {
88 case VA_FOURCC('N','V','1','2'):
89 return PIPE_FORMAT_NV12;
90 case VA_FOURCC('P','0','1','0'):
91 case VA_FOURCC('P','0','1','6'):
92 return PIPE_FORMAT_P016;
93 case VA_FOURCC('I','4','2','0'):
94 return PIPE_FORMAT_IYUV;
95 case VA_FOURCC('Y','V','1','2'):
96 return PIPE_FORMAT_YV12;
97 case VA_FOURCC('Y','U','Y','V'):
98 return PIPE_FORMAT_YUYV;
99 case VA_FOURCC('U','Y','V','Y'):
100 return PIPE_FORMAT_UYVY;
101 case VA_FOURCC('B','G','R','A'):
102 return PIPE_FORMAT_B8G8R8A8_UNORM;
103 case VA_FOURCC('R','G','B','A'):
104 return PIPE_FORMAT_R8G8B8A8_UNORM;
105 case VA_FOURCC('B','G','R','X'):
106 return PIPE_FORMAT_B8G8R8X8_UNORM;
107 case VA_FOURCC('R','G','B','X'):
108 return PIPE_FORMAT_R8G8B8X8_UNORM;
109 default:
110 assert(0);
111 return PIPE_FORMAT_NONE;
112 }
113 }
114
115 static inline unsigned
116 PipeFormatToVaFourcc(enum pipe_format p_format)
117 {
118 switch (p_format) {
119 case PIPE_FORMAT_NV12:
120 return VA_FOURCC('N','V','1','2');
121 case PIPE_FORMAT_P016:
122 return VA_FOURCC('P','0','1','6');
123 case PIPE_FORMAT_IYUV:
124 return VA_FOURCC('I','4','2','0');
125 case PIPE_FORMAT_YV12:
126 return VA_FOURCC('Y','V','1','2');
127 case PIPE_FORMAT_UYVY:
128 return VA_FOURCC('U','Y','V','Y');
129 case PIPE_FORMAT_YUYV:
130 return VA_FOURCC('Y','U','Y','V');
131 case PIPE_FORMAT_B8G8R8A8_UNORM:
132 return VA_FOURCC('B','G','R','A');
133 case PIPE_FORMAT_R8G8B8A8_UNORM:
134 return VA_FOURCC('R','G','B','A');
135 case PIPE_FORMAT_B8G8R8X8_UNORM:
136 return VA_FOURCC('B','G','R','X');
137 case PIPE_FORMAT_R8G8B8X8_UNORM:
138 return VA_FOURCC('R','G','B','X');
139 default:
140 assert(0);
141 return -1;
142 }
143 }
144
145 static inline VAProfile
146 PipeToProfile(enum pipe_video_profile profile)
147 {
148 switch (profile) {
149 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
150 return VAProfileMPEG2Simple;
151 case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
152 return VAProfileMPEG2Main;
153 case PIPE_VIDEO_PROFILE_MPEG4_SIMPLE:
154 return VAProfileMPEG4Simple;
155 case PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE:
156 return VAProfileMPEG4AdvancedSimple;
157 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
158 return VAProfileVC1Simple;
159 case PIPE_VIDEO_PROFILE_VC1_MAIN:
160 return VAProfileVC1Main;
161 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
162 return VAProfileVC1Advanced;
163 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
164 return VAProfileH264ConstrainedBaseline;
165 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
166 return VAProfileH264Main;
167 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
168 return VAProfileH264High;
169 case PIPE_VIDEO_PROFILE_HEVC_MAIN:
170 return VAProfileHEVCMain;
171 case PIPE_VIDEO_PROFILE_HEVC_MAIN_10:
172 return VAProfileHEVCMain10;
173 case PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED:
174 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
175 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH422:
176 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH444:
177 case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
178 case PIPE_VIDEO_PROFILE_HEVC_MAIN_12:
179 case PIPE_VIDEO_PROFILE_HEVC_MAIN_STILL:
180 case PIPE_VIDEO_PROFILE_HEVC_MAIN_444:
181 case PIPE_VIDEO_PROFILE_UNKNOWN:
182 return VAProfileNone;
183 default:
184 assert(0);
185 return -1;
186 }
187 }
188
189 static inline enum pipe_video_profile
190 ProfileToPipe(VAProfile profile)
191 {
192 switch (profile) {
193 case VAProfileMPEG2Simple:
194 return PIPE_VIDEO_PROFILE_MPEG2_SIMPLE;
195 case VAProfileMPEG2Main:
196 return PIPE_VIDEO_PROFILE_MPEG2_MAIN;
197 case VAProfileMPEG4Simple:
198 return PIPE_VIDEO_PROFILE_MPEG4_SIMPLE;
199 case VAProfileMPEG4AdvancedSimple:
200 return PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE;
201 case VAProfileVC1Simple:
202 return PIPE_VIDEO_PROFILE_VC1_SIMPLE;
203 case VAProfileVC1Main:
204 return PIPE_VIDEO_PROFILE_VC1_MAIN;
205 case VAProfileVC1Advanced:
206 return PIPE_VIDEO_PROFILE_VC1_ADVANCED;
207 case VAProfileH264ConstrainedBaseline:
208 return PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE;
209 case VAProfileH264Main:
210 return PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN;
211 case VAProfileH264High:
212 return PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH;
213 case VAProfileHEVCMain:
214 return PIPE_VIDEO_PROFILE_HEVC_MAIN;
215 case VAProfileHEVCMain10:
216 return PIPE_VIDEO_PROFILE_HEVC_MAIN_10;
217 case VAProfileNone:
218 return PIPE_VIDEO_PROFILE_UNKNOWN;
219 default:
220 return PIPE_VIDEO_PROFILE_UNKNOWN;
221 }
222 }
223
224 typedef struct {
225 struct vl_screen *vscreen;
226 struct pipe_context *pipe;
227 struct handle_table *htab;
228 struct vl_compositor compositor;
229 struct vl_compositor_state cstate;
230 vl_csc_matrix csc;
231 mtx_t mutex;
232 } vlVaDriver;
233
234 typedef struct {
235 VAImage *image;
236
237 struct u_rect src_rect;
238 struct u_rect dst_rect;
239
240 struct pipe_sampler_view *sampler;
241 } vlVaSubpicture;
242
243 typedef struct {
244 VABufferType type;
245 unsigned int size;
246 unsigned int num_elements;
247 void *data;
248 struct {
249 struct pipe_resource *resource;
250 struct pipe_transfer *transfer;
251 } derived_surface;
252 unsigned int export_refcount;
253 VABufferInfo export_state;
254 unsigned int coded_size;
255 } vlVaBuffer;
256
257 typedef struct {
258 struct pipe_video_codec templat, *decoder;
259 struct pipe_video_buffer *target;
260 union {
261 struct pipe_picture_desc base;
262 struct pipe_mpeg12_picture_desc mpeg12;
263 struct pipe_mpeg4_picture_desc mpeg4;
264 struct pipe_vc1_picture_desc vc1;
265 struct pipe_h264_picture_desc h264;
266 struct pipe_h265_picture_desc h265;
267 struct pipe_mjpeg_picture_desc mjpeg;
268 struct pipe_h264_enc_picture_desc h264enc;
269 } desc;
270
271 struct {
272 unsigned long long int frame_num;
273 unsigned int start_code_size;
274 unsigned int vti_bits;
275 unsigned int quant_scale;
276 VAPictureParameterBufferMPEG4 pps;
277 uint8_t start_code[32];
278 } mpeg4;
279
280 struct vl_deint_filter *deint;
281 vlVaBuffer *coded_buf;
282 int target_id;
283 bool first_single_submitted;
284 int gop_coeff;
285 bool needs_begin_frame;
286 } vlVaContext;
287
288 typedef struct {
289 enum pipe_video_profile profile;
290 enum pipe_video_entrypoint entrypoint;
291 enum pipe_h264_enc_rate_control_method rc;
292 unsigned int rt_format;
293 } vlVaConfig;
294
295 typedef struct {
296 struct pipe_video_buffer templat, *buffer;
297 struct util_dynarray subpics; /* vlVaSubpicture */
298 VAContextID ctx;
299 vlVaBuffer *coded_buf;
300 void *feedback;
301 unsigned int frame_num_cnt;
302 bool force_flushed;
303 } vlVaSurface;
304
305 // Public functions:
306 VAStatus VA_DRIVER_INIT_FUNC(VADriverContextP ctx);
307
308 // vtable functions:
309 VAStatus vlVaTerminate(VADriverContextP ctx);
310 VAStatus vlVaQueryConfigProfiles(VADriverContextP ctx, VAProfile *profile_list,int *num_profiles);
311 VAStatus vlVaQueryConfigEntrypoints(VADriverContextP ctx, VAProfile profile,
312 VAEntrypoint *entrypoint_list, int *num_entrypoints);
313 VAStatus vlVaGetConfigAttributes(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
314 VAConfigAttrib *attrib_list, int num_attribs);
315 VAStatus vlVaCreateConfig(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
316 VAConfigAttrib *attrib_list, int num_attribs, VAConfigID *config_id);
317 VAStatus vlVaDestroyConfig(VADriverContextP ctx, VAConfigID config_id);
318 VAStatus vlVaQueryConfigAttributes(VADriverContextP ctx, VAConfigID config_id, VAProfile *profile,
319 VAEntrypoint *entrypoint, VAConfigAttrib *attrib_list, int *num_attribs);
320 VAStatus vlVaCreateSurfaces(VADriverContextP ctx, int width, int height, int format,
321 int num_surfaces, VASurfaceID *surfaces);
322 VAStatus vlVaDestroySurfaces(VADriverContextP ctx, VASurfaceID *surface_list, int num_surfaces);
323 VAStatus vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width, int picture_height,
324 int flag, VASurfaceID *render_targets, int num_render_targets, VAContextID *context);
325 VAStatus vlVaDestroyContext(VADriverContextP ctx, VAContextID context);
326 VAStatus vlVaCreateBuffer(VADriverContextP ctx, VAContextID context, VABufferType type, unsigned int size,
327 unsigned int num_elements, void *data, VABufferID *buf_id);
328 VAStatus vlVaBufferSetNumElements(VADriverContextP ctx, VABufferID buf_id, unsigned int num_elements);
329 VAStatus vlVaMapBuffer(VADriverContextP ctx, VABufferID buf_id, void **pbuf);
330 VAStatus vlVaUnmapBuffer(VADriverContextP ctx, VABufferID buf_id);
331 VAStatus vlVaDestroyBuffer(VADriverContextP ctx, VABufferID buffer_id);
332 VAStatus vlVaBeginPicture(VADriverContextP ctx, VAContextID context, VASurfaceID render_target);
333 VAStatus vlVaRenderPicture(VADriverContextP ctx, VAContextID context, VABufferID *buffers, int num_buffers);
334 VAStatus vlVaEndPicture(VADriverContextP ctx, VAContextID context);
335 VAStatus vlVaSyncSurface(VADriverContextP ctx, VASurfaceID render_target);
336 VAStatus vlVaQuerySurfaceStatus(VADriverContextP ctx, VASurfaceID render_target, VASurfaceStatus *status);
337 VAStatus vlVaQuerySurfaceError(VADriverContextP ctx, VASurfaceID render_target,
338 VAStatus error_status, void **error_info);
339 VAStatus vlVaPutSurface(VADriverContextP ctx, VASurfaceID surface, void* draw, short srcx, short srcy,
340 unsigned short srcw, unsigned short srch, short destx, short desty, unsigned short destw,
341 unsigned short desth, VARectangle *cliprects, unsigned int number_cliprects,
342 unsigned int flags);
343 VAStatus vlVaQueryImageFormats(VADriverContextP ctx, VAImageFormat *format_list, int *num_formats);
344 VAStatus vlVaQuerySubpictureFormats(VADriverContextP ctx, VAImageFormat *format_list,
345 unsigned int *flags, unsigned int *num_formats);
346 VAStatus vlVaCreateImage(VADriverContextP ctx, VAImageFormat *format, int width, int height, VAImage *image);
347 VAStatus vlVaDeriveImage(VADriverContextP ctx, VASurfaceID surface, VAImage *image);
348 VAStatus vlVaDestroyImage(VADriverContextP ctx, VAImageID image);
349 VAStatus vlVaSetImagePalette(VADriverContextP ctx, VAImageID image, unsigned char *palette);
350 VAStatus vlVaGetImage(VADriverContextP ctx, VASurfaceID surface, int x, int y,
351 unsigned int width, unsigned int height, VAImageID image);
352 VAStatus vlVaPutImage(VADriverContextP ctx, VASurfaceID surface, VAImageID image, int src_x, int src_y,
353 unsigned int src_width, unsigned int src_height, int dest_x, int dest_y,
354 unsigned int dest_width, unsigned int dest_height);
355 VAStatus vlVaQuerySubpictureFormats(VADriverContextP ctx, VAImageFormat *format_list,
356 unsigned int *flags, unsigned int *num_formats);
357 VAStatus vlVaCreateSubpicture(VADriverContextP ctx, VAImageID image, VASubpictureID *subpicture);
358 VAStatus vlVaDestroySubpicture(VADriverContextP ctx, VASubpictureID subpicture);
359 VAStatus vlVaSubpictureImage(VADriverContextP ctx, VASubpictureID subpicture, VAImageID image);
360 VAStatus vlVaSetSubpictureChromakey(VADriverContextP ctx, VASubpictureID subpicture,
361 unsigned int chromakey_min, unsigned int chromakey_max,
362 unsigned int chromakey_mask);
363 VAStatus vlVaSetSubpictureGlobalAlpha(VADriverContextP ctx, VASubpictureID subpicture, float global_alpha);
364 VAStatus vlVaAssociateSubpicture(VADriverContextP ctx, VASubpictureID subpicture, VASurfaceID *target_surfaces,
365 int num_surfaces, short src_x, short src_y,
366 unsigned short src_width, unsigned short src_height,
367 short dest_x, short dest_y, unsigned short dest_width, unsigned short dest_height,
368 unsigned int flags);
369 VAStatus vlVaDeassociateSubpicture(VADriverContextP ctx, VASubpictureID subpicture,
370 VASurfaceID *target_surfaces, int num_surfaces);
371 VAStatus vlVaQueryDisplayAttributes(VADriverContextP ctx, VADisplayAttribute *attr_list, int *num_attributes);
372 VAStatus vlVaGetDisplayAttributes(VADriverContextP ctx, VADisplayAttribute *attr_list, int num_attributes);
373 VAStatus vlVaSetDisplayAttributes(VADriverContextP ctx, VADisplayAttribute *attr_list, int num_attributes);
374 VAStatus vlVaBufferInfo(VADriverContextP ctx, VABufferID buf_id, VABufferType *type,
375 unsigned int *size, unsigned int *num_elements);
376 VAStatus vlVaLockSurface(VADriverContextP ctx, VASurfaceID surface, unsigned int *fourcc,
377 unsigned int *luma_stride, unsigned int *chroma_u_stride, unsigned int *chroma_v_stride,
378 unsigned int *luma_offset, unsigned int *chroma_u_offset, unsigned int *chroma_v_offset,
379 unsigned int *buffer_name, void **buffer);
380 VAStatus vlVaUnlockSurface(VADriverContextP ctx, VASurfaceID surface);
381 VAStatus vlVaCreateSurfaces2(VADriverContextP ctx, unsigned int format, unsigned int width, unsigned int height,
382 VASurfaceID *surfaces, unsigned int num_surfaces, VASurfaceAttrib *attrib_list,
383 unsigned int num_attribs);
384 VAStatus vlVaQuerySurfaceAttributes(VADriverContextP ctx, VAConfigID config, VASurfaceAttrib *attrib_list,
385 unsigned int *num_attribs);
386
387 VAStatus vlVaAcquireBufferHandle(VADriverContextP ctx, VABufferID buf_id, VABufferInfo *out_buf_info);
388 VAStatus vlVaReleaseBufferHandle(VADriverContextP ctx, VABufferID buf_id);
389
390 VAStatus vlVaQueryVideoProcFilters(VADriverContextP ctx, VAContextID context, VAProcFilterType *filters,
391 unsigned int *num_filters);
392 VAStatus vlVaQueryVideoProcFilterCaps(VADriverContextP ctx, VAContextID context, VAProcFilterType type,
393 void *filter_caps, unsigned int *num_filter_caps);
394 VAStatus vlVaQueryVideoProcPipelineCaps(VADriverContextP ctx, VAContextID context, VABufferID *filters,
395 unsigned int num_filters, VAProcPipelineCaps *pipeline_cap);
396
397 // internal functions
398 VAStatus vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
399 VAStatus vlVaHandleSurfaceAllocate(VADriverContextP ctx, vlVaSurface *surface, struct pipe_video_buffer *templat);
400 void vlVaGetReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id, struct pipe_video_buffer **ref_frame);
401 void vlVaHandlePictureParameterBufferMPEG12(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
402 void vlVaHandleIQMatrixBufferMPEG12(vlVaContext *context, vlVaBuffer *buf);
403 void vlVaHandleSliceParameterBufferMPEG12(vlVaContext *context, vlVaBuffer *buf);
404 void vlVaHandlePictureParameterBufferH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
405 void vlVaHandleIQMatrixBufferH264(vlVaContext *context, vlVaBuffer *buf);
406 void vlVaHandleSliceParameterBufferH264(vlVaContext *context, vlVaBuffer *buf);
407 void vlVaHandlePictureParameterBufferVC1(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
408 void vlVaHandleSliceParameterBufferVC1(vlVaContext *context, vlVaBuffer *buf);
409 void vlVaHandlePictureParameterBufferMPEG4(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
410 void vlVaHandleIQMatrixBufferMPEG4(vlVaContext *context, vlVaBuffer *buf);
411 void vlVaHandleSliceParameterBufferMPEG4(vlVaContext *context, vlVaBuffer *buf);
412 void vlVaDecoderFixMPEG4Startcode(vlVaContext *context);
413 void vlVaHandlePictureParameterBufferHEVC(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
414 void vlVaHandleIQMatrixBufferHEVC(vlVaContext *context, vlVaBuffer *buf);
415 void vlVaHandleSliceParameterBufferHEVC(vlVaContext *context, vlVaBuffer *buf);
416 void vlVaHandlePictureParameterBufferMJPEG(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf);
417 void vlVaHandleIQMatrixBufferMJPEG(vlVaContext *context, vlVaBuffer *buf);
418 void vlVaHandleHuffmanTableBufferType(vlVaContext *context, vlVaBuffer *buf);
419 void vlVaHandleSliceParameterBufferMJPEG(vlVaContext *context, vlVaBuffer *buf);
420
421 #endif //VA_PRIVATE_H