1 /*
2  * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Waldo Bastian <waldo.bastian@intel.com>
26  *
27  */
28 
29 #ifndef _PSB_DRV_VIDEO_H_
30 #define _PSB_DRV_VIDEO_H_
31 
32 #include <pthread.h> /* POSIX threads headers */
33 
34 #include <va/va_backend.h>
35 #include <va/va.h>
36 #include <va/va_tpi.h>
37 
38 #include <va/va_vpp.h>
39 
40 #include "object_heap.h"
41 #include "psb_def.h"
42 //#include "psb_drv_debug.h"
43 #include "xf86drm.h"
44 #ifdef ANDROID
45 #ifdef BAYTRAIL
46 #include <linux/vxd_drm.h>
47 #else
48 #include <linux/psb_drm.h>
49 #endif
50 #endif
51 #include "psb_overlay.h"
52 #include "psb_texture.h"
53 #include <stdint.h>
54 #ifndef ANDROID
55 #include <psb_drm.h>
56 #include <X11/Xlibint.h>
57 #include <X11/X.h>
58 #include <X11/extensions/Xv.h>
59 #include <X11/extensions/Xvlib.h>
60 #include <X11/Xlib.h>
61 #else
62 #define XID unsigned int
63 #define INT16 unsigned int
64 #include <cutils/log.h>
65 #include <system/window.h>
66 #undef  LOG_TAG
67 #define LOG_TAG "pvr_drv_video"
68 #endif
69 #include "hwdefs/dxva_fw_flags.h"
70 #include <wsbm/wsbm_pool.h>
71 
72 #ifdef __GNUC__
73 # define __maybe_unused __attribute__((__unused__))
74 #else
75 # define __maybe_unused
76 #endif
77 
78 #ifndef min
79 #define min(a, b) ((a) < (b)) ? (a) : (b)
80 #endif
81 
82 #ifndef max
83 #define max(a, b) ((a) > (b)) ? (a) : (b)
84 #endif
85 
86 //#define _TOPAZHP_PDUMP_
87 #define TOPAZHP_PIPE_NUM 2
88 
89 #define _TNG_RELOC_
90 //#define _TNG_FRAMES_
91 #define FORCED_REFERENCE 1
92 #define LTREFHEADER 1
93 //#define _TP_DEBUG_MMU_
94 //#define _TOPAZHP_REC_
95 
96 /*
97  * WORKAROUND_DMA_OFF_BY_ONE: LLDMA requests may access one additional byte which can cause
98  * a MMU fault if the next byte after the buffer end is on a different page that isn't mapped.
99  */
100 #define WORKAROUND_DMA_OFF_BY_ONE
101 #define FOURCC_XVVA     (('A' << 24) + ('V' << 16) + ('V' << 8) + 'X')
102 
103 #define PSB_MAX_PROFILES			32
104 #define PSB_MAX_ENTRYPOINTS			32
105 #define PSB_MAX_CONFIG_ATTRIBUTES		VAConfigAttribTypeMax
106 
107 /* VABufferTypeMax is large(1000+) because there is big blank space between common libva buffer
108  * type and Intel specific buffer types(for GEN usage only). If use VABufferTypeMax as
109  * PSB_MAX_BUFFERTYPES to define the size of buffer type related arrays in context object,
110  * we will waste about 16K memory. Currently, the max value of buffer type used in psb_video is
111  * VAParseSliceHeaderGroupBufferType(44).
112  */
113 #define PSB_MAX_BUFFERTYPES			64
114 
115 /* Max # of command submission buffers */
116 #define PSB_MAX_CMDBUFS                         10
117 #define LNC_MAX_CMDBUFS_ENCODE                  4
118 #define PNW_MAX_CMDBUFS_ENCODE                  4
119 #define TNG_MAX_CMDBUFS_ENCODE                  4
120 #define VSP_MAX_CMDBUFS				10
121 
122 #define PSB_SURFACE_DISPLAYING_F (0x1U<<0)
123 #define PSB_SURFACE_IS_FLAG_SET(flags, mask) (((flags)& PSB_SURFACE_DISPLAYING_F) != 0)
124 
125 #define PSB_CTX_TILING_MASK	0x00FF0000
126 
127 /*xrandr dirty flag*/
128 #define PSB_NEW_ROTATION        1
129 #define PSB_NEW_EXTVIDEO        2
130 
131 #define PSB_NEW_VA_ROTATION     1 << 0
132 #define PSB_NEW_WM_ROTATION     1 << 1
133 
134 #define MAX_SLICES_PER_PICTURE 72
135 #define MAX_MB_ERRORS 72
136 
137 /* Some funtions aren't used but we'd like to keep them as reference code in future */
138 #define PSB_MFLD_DUMMY_CODE     0
139 
140 /*
141  * ITU-R BT.601 and BT.709 transfer matrices from VA 2.0
142  * Video Color Field definitions Design Spec(Version 0.03).
143  * [R', G', B'] values are in the range [0, 1], Y' is in the range [0,1]
144  * and [Pb, Pr] components are in the range [-0.5, 0.5].
145  */
146 static float s601[9] = {
147     1, -0.000001, 1.402,
148     1, -0.344136, -0.714136,
149     1, 1.772, 0
150 };
151 
152 static float s709[9] = {
153     1, 0, 1.5748,
154     1, -0.187324, -0.468124,
155     1, 1.8556, 0
156 };
157 
158 typedef struct object_config_s *object_config_p;
159 typedef struct object_context_s *object_context_p;
160 typedef struct object_surface_s *object_surface_p;
161 typedef struct object_buffer_s *object_buffer_p;
162 typedef struct object_image_s *object_image_p;
163 typedef struct object_subpic_s *object_subpic_p;
164 typedef struct format_vtable_s *format_vtable_p;
165 typedef struct psb_driver_data_s *psb_driver_data_p;
166 
167 typedef struct psb_surface_share_info_s psb_surface_share_info_t, *psb_surface_share_info_p;
168 /* post-processing data structure */
169 enum psb_output_method_t {
170     PSB_PUTSURFACE_NONE = 0,
171     PSB_PUTSURFACE_X11,/* use x11 method */
172     PSB_PUTSURFACE_TEXTURE,/* texture xvideo */
173     PSB_PUTSURFACE_OVERLAY,/* overlay xvideo */
174     PSB_PUTSURFACE_COVERLAY,/* client overlay */
175     PSB_PUTSURFACE_CTEXTURE,/* client textureblit */
176     PSB_PUTSURFACE_TEXSTREAMING,/* texsteaming */
177     PSB_PUTSURFACE_FORCE_TEXTURE,/* force texture xvideo */
178     PSB_PUTSURFACE_FORCE_OVERLAY,/* force overlay xvideo */
179     PSB_PUTSURFACE_FORCE_CTEXTURE,/* force client textureblit */
180     PSB_PUTSURFACE_FORCE_COVERLAY,/* force client overlay */
181     PSB_PUTSURFACE_FORCE_TEXSTREAMING,/* force texstreaming */
182 };
183 
184 typedef struct psb_decode_info {
185     uint32_t num_surface;
186     uint32_t surface_id;
187 } psb_decode_info_t;
188 typedef struct msvdx_decode_info *psb_decode_info_p;
189 
190 #define CSC_MATRIX_X  (3)
191 #define CSC_MATRIX_Y  (3)
192 
193 struct psb_driver_data_s {
194     struct object_heap_s        config_heap;
195     struct object_heap_s        context_heap;
196     struct object_heap_s        surface_heap;
197     struct object_heap_s        buffer_heap;
198     struct object_heap_s        image_heap;
199     struct object_heap_s        subpic_heap;
200     char *                      bus_id;
201     uint32_t                    dev_id;
202     int                         drm_fd;
203     int                         dup_drm_fd;
204 
205     /*  PM_QoS */
206     int                         pm_qos_fd;
207     int                         dri2;
208     int                         dri_dummy;
209     XID                         context_id;
210     drm_context_t               drm_context;
211     drmLock                     *drm_lock;
212     int                         contended_lock;
213     pthread_mutex_t             drm_mutex;
214     format_vtable_p             profile2Format[PSB_MAX_PROFILES][PSB_MAX_ENTRYPOINTS];
215 #ifdef PSBVIDEO_MRFL_VPP
216     format_vtable_p             vpp_profile;
217 #endif
218 #ifdef PSBVIDEO_MFLD
219     format_vtable_p             vpp_profile;
220 #endif
221     uint32_t                    msvdx_context_base;
222     int                         video_sd_disabled;
223     int                         video_hd_disabled;
224     unsigned char *             camera_bo;
225     uint32_t                    camera_phyaddr;
226     uint32_t                    camera_size;
227     unsigned char *             rar_bo;
228     uint32_t                    rar_phyaddr;
229     uint32_t                    rar_size;
230 
231     int encode_supported;
232     int decode_supported;
233     int hd_encode_supported;
234     int hd_decode_supported;
235 
236     int execIoctlOffset;
237     int getParamIoctlOffset;
238 
239     struct _WsbmBufferPool *main_pool;
240     struct _WsbmFenceMgr *fence_mgr;
241 
242     enum psb_output_method_t output_method;
243 
244     /* whether the post-processing use client overlay or not */
245     int coverlay;
246     int coverlay_init;
247     PsbPortPrivRec coverlay_priv;
248 
249 
250     /* whether the post-processing use client textureblit or not */
251     int ctexture;
252     struct psb_texture_s ctexture_priv;
253 
254     /*
255     //whether the post-processing use texstreaing or not
256     int ctexstreaing;
257     struct psb_texstreaing ctexstreaing_priv;
258     */
259 
260     unsigned char *ws_priv; /* window system related data structure */
261 
262 
263     VASurfaceID cur_displaying_surface;
264     VASurfaceID last_displaying_surface;
265 
266     VADisplayAttribute ble_black_mode;
267     VADisplayAttribute ble_white_mode;
268 
269     VADisplayAttribute blueStretch_gain;
270     VADisplayAttribute skinColorCorrection_gain;
271 
272     VADisplayAttribute brightness;
273     VADisplayAttribute hue;
274     VADisplayAttribute contrast;
275     VADisplayAttribute saturation;
276     /*Save RenderMode and RenderRect attribute
277      * for medfield android extend video mode.*/
278     uint32_t render_device;
279     uint32_t render_mode;
280     VARectangle  render_rect;
281 
282     unsigned int clear_color;
283 
284     int  is_oold;
285 
286     unsigned int load_csc_matrix;
287     float   csc_matrix[CSC_MATRIX_X][CSC_MATRIX_Y];
288     int  is_BT601;
289 
290     unsigned int set_video_range;
291     unsigned int video_range;
292 
293     /* subpic number current buffers support */
294     unsigned int max_subpic;
295 
296     /* for multi-thread safe */
297     int use_xrandr_thread;
298     pthread_mutex_t output_mutex;
299     pthread_t xrandr_thread_id;
300     int extend_fullscreen;
301 
302     int drawable_info;
303     int dummy_putsurface;
304     int fixed_fps;
305     unsigned int frame_count;
306 
307     uint32_t blend_mode;
308     uint32_t blend_color;
309     uint32_t overlay_auto_paint_color_key;
310     uint32_t color_key;
311 
312     /*output rotation info*/
313     int disable_msvdx_rotate;
314     int disable_msvdx_rotate_backup;
315     int msvdx_rotate_want; /* msvdx rotate info programed to msvdx */
316     int va_rotate; /* VA rotate passed from APP */
317     int mipi0_rotation; /* window manager rotation */
318     int mipi1_rotation; /* window manager rotation */
319     int hdmi_rotation; /* window manager rotation */
320     int local_rotation; /* final device rotate: VA rotate+wm rotate */
321     int extend_rotation; /* final device rotate: VA rotate+wm rotate */
322     int rotation_dirty;  /*flag for recaculate final rotation*/
323 
324     unsigned int outputmethod_checkinterval;
325 
326     uint32_t xrandr_dirty;
327     uint32_t xrandr_update;
328     /*only VAProfileH264ConstrainedBaseline profile enable error concealment*/
329     uint32_t ec_enabled;
330     uint32_t ved_vpp;
331 
332     /* vpp is on or off */
333     int vpp_on;
334 
335     uint32_t pre_surfaceid;
336     psb_decode_info_t decode_info;
337     drm_psb_msvdx_decode_status_t *msvdx_decode_status;
338     VASurfaceDecodeMBErrors *surface_mb_error;
339 
340     unsigned char *hPVR2DContext;
341 
342     VAGenericID wrapped_surface_id[VIDEO_BUFFER_NUM];
343     VAGenericID wrapped_subpic_id[VIDEO_BUFFER_NUM];
344     PVR2DMEMINFO *videoBuf[VIDEO_BUFFER_NUM];
345     PVR2DMEMINFO *subpicBuf[VIDEO_BUFFER_NUM];
346     void *native_window;
347     int is_android;
348     /* VA_RT_FORMAT_PROTECTED is set to protected for Widevine case */
349     int protected;
350 };
351 
352 
353 #ifdef _FOR_FPGA_
354 #define IS_CTP(driver_data)  0
355 #define IS_MFLD(driver_data) 0
356 #define IS_MRFL(driver_data) 1
357 #define IS_MRST(driver_data) 0
358 #else
359 #define IS_CTP(driver_data) (((driver_data->dev_id & 0xffff) == 0x08c0) ||  \
360                      ((driver_data->dev_id & 0xffff) == 0x08c7) ||  \
361                      ((driver_data->dev_id & 0xffff) == 0x08c8))
362 #define IS_MRST(driver_data) ((driver_data->dev_id & 0xFFFC) == 0x4100)
363 #define IS_MFLD(driver_data) (((driver_data->dev_id & 0xFFFC) == 0x0130) || ((driver_data->dev_id & 0xFFFF) == 0x08C0) || ((driver_data->dev_id & 0xFFFF) == 0x08C7) || ((driver_data->dev_id & 0xFFFF) == 0x01FF) || ((driver_data->dev_id & 0xFFFF) == 0x08C8))
364 #define IS_MRFL(driver_data) (((driver_data->dev_id & 0xFFFC) == 0x1180) || ((driver_data->dev_id & 0xFFFC) == 0x1480))
365 #define IS_MOFD(driver_data) ((driver_data->dev_id & 0xFFFC) == 0x1480)
366 #define IS_LEXINGTON(driver_data) ((driver_data->dev_id & 0xFFFF) == 0x01FF)
367 #define IS_BAYTRAIL(driver_data) ((driver_data->dev_id & 0xFFFF) == 0x0F31)
368 #endif
369 
370 struct object_config_s {
371     struct object_base_s base;
372     VAProfile profile;
373     VAEntrypoint entrypoint;
374     VAConfigAttrib attrib_list[PSB_MAX_CONFIG_ATTRIBUTES];
375     int attrib_count;
376     format_vtable_p format_vtable;
377 };
378 
379 struct object_context_s {
380     struct object_base_s base;
381     VAContextID context_id;
382     VAConfigID config_id;
383     VAProfile profile;
384     VAEntrypoint entry_point;
385     int picture_width;
386     int picture_height;
387     int num_render_targets;
388     VASurfaceID *render_targets;
389     int va_flags;
390 
391     object_surface_p current_render_target;
392     object_surface_p ec_target;
393     object_surface_p ec_candidate;
394     VASurfaceID current_render_surface_id;
395     psb_driver_data_p driver_data;
396     format_vtable_p format_vtable;
397     unsigned char *format_data;
398     struct psb_cmdbuf_s *cmdbuf_list[PSB_MAX_CMDBUFS];
399     struct lnc_cmdbuf_s *lnc_cmdbuf_list[LNC_MAX_CMDBUFS_ENCODE];
400     struct pnw_cmdbuf_s *pnw_cmdbuf_list[PNW_MAX_CMDBUFS_ENCODE];
401     struct tng_cmdbuf_s	*tng_cmdbuf_list[TNG_MAX_CMDBUFS_ENCODE];
402     struct vsp_cmdbuf_s *vsp_cmdbuf_list[VSP_MAX_CMDBUFS];
403 
404     struct psb_cmdbuf_s *cmdbuf; /* Current cmd buffer */
405     struct lnc_cmdbuf_s *lnc_cmdbuf;
406     struct pnw_cmdbuf_s *pnw_cmdbuf;
407     struct tng_cmdbuf_s *tng_cmdbuf;
408     struct vsp_cmdbuf_s *vsp_cmdbuf;
409 
410     int cmdbuf_current;
411 
412     /* Buffers */
413     object_buffer_p buffers_unused[PSB_MAX_BUFFERTYPES]; /* Linked lists (HEAD) of unused buffers for each buffer type */
414     int buffers_unused_count[PSB_MAX_BUFFERTYPES]; /* Linked lists (HEAD) of unused buffers for each buffer type */
415     object_buffer_p buffers_unused_tail[PSB_MAX_BUFFERTYPES]; /* Linked lists (TAIL) of unused buffers for each buffer type */
416     object_buffer_p buffers_active[PSB_MAX_BUFFERTYPES]; /* Linked lists of active buffers for each buffer type */
417 
418     object_buffer_p *buffer_list; /* for vaRenderPicture */
419     int num_buffers;
420 
421     enum {
422         psb_video_none = 0,
423         psb_video_mc,
424         psb_video_vld,
425         psb_video_deblock
426     } video_op;
427 
428     uint32_t operating_mode;
429     uint32_t flags; /* See render flags below */
430     uint32_t first_mb;
431     uint32_t last_mb;
432 
433     int is_oold;
434     int msvdx_rotate;
435     int msvdx_scaling;
436     int interlaced_stream;
437 
438     /* value is 64bits value, consist of 8 bytes
439      * bytes[0]: entrypoint
440      * bytes[1]: profile
441      * bytes[2]: tile stride | rotated tile stride
442      * bytes[3]: driver_data->protected
443      * bytes[4]: width_in_mb; pass width kernel for VC1/H.264 workaround
444      * bytes[5]: height_in_mb; pass width kernel for VC1/H.264 workaround
445      */
446     uint64_t ctp_type;
447 
448     unsigned long msvdx_tile; /* normal tile | (rotate tile << 4) */
449 #ifdef SLICE_HEADER_PARSING
450     int msvdx_frame_end;
451     int modular_drm;
452 #endif
453 
454     uint32_t msvdx_context;
455 
456     int scaling_width;
457     int scaling_height;
458     int scaling_buffer_width;
459     int scaling_buffer_height;
460     int scaling_offset_x;
461     int scaling_offset_y;
462     int scaling_update;
463 
464     /* Debug */
465     uint32_t frame_count;
466     uint32_t slice_count;
467 };
468 
469 #define ROTATE_VA2MSVDX(va_rotate)  (va_rotate)
470 #define CONTEXT_ROTATE(obj_context) (obj_context->msvdx_rotate != ROTATE_VA2MSVDX(VA_ROTATION_NONE))
471 #define CONTEXT_SCALING(obj_context) (obj_context->msvdx_scaling)
472 #define CONTEXT_ALTERNATIVE_OUTPUT(obj_context) (CONTEXT_ROTATE(obj_context) || CONTEXT_SCALING(obj_context))
473 
474 enum force_output_method_t {
475     OUTPUT_FORCE_NULL = 0,
476     OUTPUT_FORCE_GPU,
477     OUTPUT_FORCE_OVERLAY,
478     OUTPUT_FORCE_OVERLAY_FOR_SW_DECODE,
479 };
480 
481 #define MAX_SHARE_INFO_KHANDLES 32
482 struct psb_surface_share_info_s {
483     //int rotation_sf;                    /*rotaion degree from surface flinger.*/
484     int surface_rotate;                 /*rotation degree of current rotation surface*/
485     int metadata_rotate;                /*rotation degree of meta data*/
486     int width_r;
487     int height_r;
488     int surface_protected;              /*whether this surface need be protected*/
489     /*Force render path.
490     0 : no fore.
491     1 : force gpu render;
492     2 : force overlay render.*/
493     int force_output_method;
494     unsigned int out_loop_khandle;
495     unsigned int renderStatus;
496     unsigned int used_by_widi;
497     int bob_deinterlace;
498     int tiling;
499     unsigned int width;
500     unsigned int height;
501     unsigned int luma_stride;
502     unsigned int chroma_u_stride;
503     unsigned int chroma_v_stride;
504     unsigned int format;
505     unsigned int khandle;
506     long long timestamp;
507 
508     unsigned int out_loop_luma_stride;
509     unsigned int out_loop_chroma_u_stride;
510     unsigned int out_loop_chroma_v_stride;
511 
512     long long hwc_timestamp;
513     unsigned int layer_transform;
514 
515     void *native_window;
516     unsigned int scaling_khandle;
517     unsigned int width_s;
518     unsigned int height_s;
519 
520     unsigned int scaling_luma_stride;
521     unsigned int scaling_chroma_u_stride;
522     unsigned int scaling_chroma_v_stride;
523 
524     unsigned int crop_width;
525     unsigned int crop_height;
526 
527     unsigned int coded_width;
528     unsigned int coded_height;
529     unsigned int initialized;
530 
531     unsigned int csc_mode;
532     unsigned int video_range;
533 
534 };
535 
536 struct object_surface_s {
537     struct object_base_s base;
538     VASurfaceID surface_id;
539     VAContextID context_id;
540     int width;
541     int height;
542     int height_origin;
543     int width_r;
544     int height_r;
545     int width_s;
546     int height_s;
547     int buffer_width_s;
548     int buffer_height_s;
549     int offset_x_s;
550     int offset_y_s;
551 
552     struct psb_surface_s *psb_surface;
553     struct psb_surface_s *out_loop_surface; /* Alternative output surface for rotation */
554     struct psb_surface_s *scaling_surface; /* Alternative output surface for scaling */
555     void *subpictures;/* if not NULL, have subpicture information */
556     unsigned int subpic_count; /* to ensure output have enough space for PDS & RAST */
557     unsigned int derived_imgcnt; /* is the surface derived by a VAImage? */
558     unsigned long display_timestamp; /* record the time point of put surface*/
559     void *rotate_vaddr;
560     struct psb_surface_share_info_s *share_info;
561     int is_ref_surface; /* If true, vaDeriveImage returns error */
562 };
563 
564 #define PSB_CODEDBUF_SLICE_NUM_MASK (0xff)
565 #define PSB_CODEDBUF_SLICE_NUM_SHIFT (0)
566 
567 #define PSB_CODEDBUF_NONE_VCL_NUM_MASK (0xff)
568 #define PSB_CODEDBUF_NONE_VCL_NUM_SHIFT (8)
569 
570 #define SET_CODEDBUF_INFO(flag, aux_info, slice_num) \
571     do {\
572         (aux_info) &= ~(PSB_CODEDBUF_##flag##_MASK<<PSB_CODEDBUF_##flag##_SHIFT);\
573         (aux_info) |= ((slice_num) & PSB_CODEDBUF_##flag##_MASK)\
574         <<PSB_CODEDBUF_##flag##_SHIFT;\
575     } while (0)
576 
577 #define CLEAR_CODEDBUF_INFO(flag, aux_info) \
578     do {\
579         (aux_info) &= ~(PSB_CODEDBUF_##flag##_MASK<<PSB_CODEDBUF_##flag##_SHIFT);\
580     } while (0)
581 
582 #define GET_CODEDBUF_INFO(flag, aux_info) \
583         (((aux_info)>>PSB_CODEDBUF_##flag##_SHIFT) & PSB_CODEDBUF_##flag##_MASK)
584 
585 
586 #define PSB_CODEDBUF_SEGMENT_MAX  (9)
587 
588 struct object_buffer_s {
589     struct object_base_s base;
590     object_buffer_p ptr_next; /* Generic ptr for linked list */
591     object_buffer_p *pptr_prev_next; /* Generic ptr for linked list */
592     struct psb_buffer_s *psb_buffer;
593     unsigned char *buffer_data;
594     unsigned int size;
595     unsigned int alloc_size;
596     unsigned int max_num_elements;
597     unsigned int num_elements;
598     object_context_p context;
599     VABufferType type;
600     uint32_t last_used;
601 
602     /* for VAEncCodedBufferType */
603     VACodedBufferSegment codedbuf_mapinfo[PSB_CODEDBUF_SEGMENT_MAX];
604     uint32_t codedbuf_aux_info;
605 };
606 
607 struct object_image_s {
608     struct object_base_s base;
609     VAImage image;
610     unsigned int palette[16];
611     int subpic_ref;
612     VASurfaceID derived_surface;
613 };
614 
615 struct object_subpic_s {
616     struct object_base_s base;
617     VASubpictureID subpic_id;
618 
619     VAImageID image_id;
620 
621     /* chromakey range */
622     unsigned int chromakey_min;
623     unsigned int chromakey_max;
624     unsigned int chromakey_mask;
625 
626     /* global alpha */
627     unsigned int global_alpha;
628 
629     /* flags */
630     unsigned int flags; /* see below */
631 
632     unsigned char *surfaces; /* surfaces, associated with this subpicture */
633 };
634 
635 typedef struct _PsbSurfaceAttributeTPI {
636     VASurfaceMemoryType type;
637     unsigned int width;
638     unsigned int height;
639     unsigned int size;
640     unsigned int pixel_format; /* buffer format */
641     unsigned int tiling; /* the memory is tiling or not */
642     unsigned int luma_stride; /* luma stride, could be width aligned with a special value */
643     unsigned int chroma_u_stride; /* chroma stride */
644     unsigned int chroma_v_stride;
645     unsigned int luma_offset; /* could be 0 */
646     unsigned int chroma_u_offset; /* U offset from the beginning of the memory */
647     unsigned int chroma_v_offset; /* V offset from the beginning of the memory */
648     unsigned int count; /* buffer count for surface creation */
649     unsigned long *buffers; /* buffer handles or user pointers */
650     unsigned long reserved[4]; /* used to pass additional information, like	362
651                         * Android native window pointer	363
652                         */
653 } PsbSurfaceAttributeTPI;
654 
655 #define MEMSET_OBJECT(ptr, data_struct) \
656         memset((unsigned char *)ptr + sizeof(struct object_base_s),\
657                 0,                          \
658                sizeof(data_struct) - sizeof(struct object_base_s))
659 
660 struct format_vtable_s {
661     void (*queryConfigAttributes)(
662         VAProfile profile,
663         VAEntrypoint entrypoint,
664         VAConfigAttrib *attrib_list,
665         int num_attribs
666     );
667     VAStatus(*validateConfig)(
668         object_config_p obj_config
669     );
670     VAStatus(*createContext)(
671         object_context_p obj_context,
672         object_config_p obj_config
673     );
674     void (*destroyContext)(
675         object_context_p obj_context
676     );
677     VAStatus(*beginPicture)(
678         object_context_p obj_context
679     );
680     VAStatus(*renderPicture)(
681         object_context_p obj_context,
682         object_buffer_p *buffers,
683         int num_buffers
684     );
685     VAStatus(*endPicture)(
686         object_context_p obj_context
687     );
688 };
689 
690 #define psb__bounds_check(x, max)                                       \
691     do { ASSERT(x < max); if (x >= max) x = max - 1; } while(0);
692 
GetTickCount()693 static inline unsigned long GetTickCount()
694 {
695     struct timeval tv;
696     if (gettimeofday(&tv, NULL))
697         return 0;
698     return tv.tv_usec / 1000 + tv.tv_sec * 1000;
699 }
700 
buffer_type_to_string(int type)701 inline static char * buffer_type_to_string(int type)
702 {
703     switch (type) {
704     case VAPictureParameterBufferType:
705         return "VAPictureParameterBufferType";
706     case VAIQMatrixBufferType:
707         return "VAIQMatrixBufferType";
708     case VABitPlaneBufferType:
709         return "VABitPlaneBufferType";
710     case VASliceGroupMapBufferType:
711         return "VASliceGroupMapBufferType";
712     case VASliceParameterBufferType:
713         return "VASliceParameterBufferType";
714     case VASliceDataBufferType:
715         return "VASliceDataBufferType";
716     case VAProtectedSliceDataBufferType:
717         return "VAProtectedSliceDataBufferType";
718     case VAMacroblockParameterBufferType:
719         return "VAMacroblockParameterBufferType";
720     case VAResidualDataBufferType:
721         return "VAResidualDataBufferType";
722     case VADeblockingParameterBufferType:
723         return "VADeblockingParameterBufferType";
724     case VAImageBufferType:
725         return "VAImageBufferType";
726     case VAEncCodedBufferType:
727         return "VAEncCodedBufferType";
728     case VAEncSequenceParameterBufferType:
729         return "VAEncSequenceParameterBufferType";
730     case VAEncPictureParameterBufferType:
731         return "VAEncPictureParameterBufferType";
732     case VAEncSliceParameterBufferType:
733         return "VAEncSliceParameterBufferType";
734     case VAEncMiscParameterBufferType:
735         return "VAEncMiscParameterBufferType";
736     case VAProbabilityBufferType:
737         return "VAProbabilityBufferType";
738     case VAHuffmanTableBufferType:
739         return "VAHuffmanTableBufferType";
740     case VAQMatrixBufferType:
741         return "VAQMatrixBufferType";
742     default:
743         return "UnknowBuffer";
744     }
745 }
746 
Angle2Rotation(int angle)747 inline static int Angle2Rotation(int angle)
748 {
749     angle %= 360;
750     switch (angle) {
751     case 0:
752         return VA_ROTATION_NONE;
753     case 90:
754         return VA_ROTATION_90;
755     case 180:
756         return VA_ROTATION_180;
757     case 270:
758         return VA_ROTATION_270;
759     default:
760         return -1;
761     }
762 }
763 
Rotation2Angle(int rotation)764 inline static int Rotation2Angle(int rotation)
765 {
766     switch (rotation) {
767     case VA_ROTATION_NONE:
768         return 0;
769     case VA_ROTATION_90:
770         return 90;
771     case VA_ROTATION_180:
772         return 180;
773     case VA_ROTATION_270:
774         return 270;
775     default:
776         return -1;
777     }
778 }
779 
780 int psb_parse_config(char *env, char *env_value);
781 void psb__destroy_surface(psb_driver_data_p driver_data, object_surface_p obj_surface);
782 unsigned long psb_tile_stride_mode(int w);
783 VAStatus psb__checkSurfaceDimensions(psb_driver_data_p driver_data, int width, int height);
784 
785 int LOCK_HARDWARE(psb_driver_data_p driver_data);
786 int UNLOCK_HARDWARE(psb_driver_data_p driver_data);
787 unsigned long psb__tile_stride_log2_256(int w);
788 int psb_update_context(psb_driver_data_p driver_data, unsigned long ctx_type);
789 
790 #define CHECK_SURFACE(obj_surface) \
791     do { \
792         if (NULL == obj_surface) { \
793             vaStatus = VA_STATUS_ERROR_INVALID_SURFACE; \
794             DEBUG_FAILURE; \
795             return vaStatus; \
796         } \
797     } while (0)
798 
799 #define CHECK_CONFIG(obj_config) \
800     do { \
801         if (NULL == obj_config) { \
802             vaStatus = VA_STATUS_ERROR_INVALID_CONFIG; \
803             DEBUG_FAILURE; \
804             return vaStatus; \
805         } \
806     } while (0)
807 
808 #define CHECK_CONTEXT(obj_context) \
809     do { \
810         if (NULL == obj_context) { \
811             vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT; \
812             DEBUG_FAILURE; \
813             return vaStatus; \
814         } \
815     } while (0)
816 
817 #define CHECK_BUFFER(obj_buffer) \
818     do { \
819         if (NULL == obj_buffer) { \
820             vaStatus = VA_STATUS_ERROR_INVALID_BUFFER; \
821             DEBUG_FAILURE; \
822             return vaStatus; \
823         } \
824     } while (0)
825 
826 #define CHECK_IMAGE(obj_image) \
827     do { \
828         if (NULL == obj_image) { \
829             vaStatus = VA_STATUS_ERROR_INVALID_IMAGE; \
830             DEBUG_FAILURE; \
831             return vaStatus; \
832         } \
833     } while (0)
834 
835 #define CHECK_SUBPICTURE(obj_subpic) \
836     do { \
837         if (NULL == obj_subpic) { \
838             vaStatus = VA_STATUS_ERROR_INVALID_SUBPICTURE; \
839             DEBUG_FAILURE; \
840             return vaStatus; \
841         } \
842     } while (0)
843 
844 #define CHECK_ALLOCATION(buf) \
845     do { \
846         if (buf == NULL) { \
847             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; \
848             DEBUG_FAILURE; \
849             return vaStatus; \
850         } \
851     } while (0)
852 
853 #define CHECK_VASTATUS() \
854     do { \
855         if (VA_STATUS_SUCCESS != vaStatus) { \
856             DEBUG_FAILURE; \
857             return vaStatus; \
858         } \
859     } while (0)
860 
861 #define CHECK_INVALID_PARAM(param) \
862     do { \
863         if (param) { \
864             vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER; \
865             DEBUG_FAILURE; \
866             return vaStatus; \
867         } \
868     } while (0)
869 
870 #endif /* _PSB_DRV_VIDEO_H_ */
871