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