Home | History | Annotate | Download | only in src
      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 (at) 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 typedef struct object_config_s *object_config_p;
    141 typedef struct object_context_s *object_context_p;
    142 typedef struct object_surface_s *object_surface_p;
    143 typedef struct object_buffer_s *object_buffer_p;
    144 typedef struct object_image_s *object_image_p;
    145 typedef struct object_subpic_s *object_subpic_p;
    146 typedef struct format_vtable_s *format_vtable_p;
    147 typedef struct psb_driver_data_s *psb_driver_data_p;
    148 
    149 typedef struct psb_surface_share_info_s psb_surface_share_info_t, *psb_surface_share_info_p;
    150 /* post-processing data structure */
    151 enum psb_output_method_t {
    152     PSB_PUTSURFACE_NONE = 0,
    153     PSB_PUTSURFACE_X11,/* use x11 method */
    154     PSB_PUTSURFACE_TEXTURE,/* texture xvideo */
    155     PSB_PUTSURFACE_OVERLAY,/* overlay xvideo */
    156     PSB_PUTSURFACE_COVERLAY,/* client overlay */
    157     PSB_PUTSURFACE_CTEXTURE,/* client textureblit */
    158     PSB_PUTSURFACE_TEXSTREAMING,/* texsteaming */
    159     PSB_PUTSURFACE_FORCE_TEXTURE,/* force texture xvideo */
    160     PSB_PUTSURFACE_FORCE_OVERLAY,/* force overlay xvideo */
    161     PSB_PUTSURFACE_FORCE_CTEXTURE,/* force client textureblit */
    162     PSB_PUTSURFACE_FORCE_COVERLAY,/* force client overlay */
    163     PSB_PUTSURFACE_FORCE_TEXSTREAMING,/* force texstreaming */
    164 };
    165 
    166 typedef struct psb_decode_info {
    167     uint32_t num_surface;
    168     uint32_t surface_id;
    169 } psb_decode_info_t;
    170 typedef struct msvdx_decode_info *psb_decode_info_p;
    171 
    172 #define CSC_MATRIX_X  (3)
    173 #define CSC_MATRIX_Y  (3)
    174 
    175 struct psb_driver_data_s {
    176     struct object_heap_s        config_heap;
    177     struct object_heap_s        context_heap;
    178     struct object_heap_s        surface_heap;
    179     struct object_heap_s        buffer_heap;
    180     struct object_heap_s        image_heap;
    181     struct object_heap_s        subpic_heap;
    182     char *                      bus_id;
    183     uint32_t                    dev_id;
    184     int                         drm_fd;
    185     int                         dup_drm_fd;
    186 
    187     /*  PM_QoS */
    188     int                         pm_qos_fd;
    189     int                         dri2;
    190     int                         dri_dummy;
    191     XID                         context_id;
    192     drm_context_t               drm_context;
    193     drmLock                     *drm_lock;
    194     int                         contended_lock;
    195     pthread_mutex_t             drm_mutex;
    196     format_vtable_p             profile2Format[PSB_MAX_PROFILES][PSB_MAX_ENTRYPOINTS];
    197 #ifdef PSBVIDEO_MRFL_VPP
    198     format_vtable_p             vpp_profile;
    199 #endif
    200 #ifdef PSBVIDEO_MFLD
    201     format_vtable_p             vpp_profile;
    202 #endif
    203     uint32_t                    msvdx_context_base;
    204     int                         video_sd_disabled;
    205     int                         video_hd_disabled;
    206     unsigned char *             camera_bo;
    207     uint32_t                    camera_phyaddr;
    208     uint32_t                    camera_size;
    209     unsigned char *             rar_bo;
    210     uint32_t                    rar_phyaddr;
    211     uint32_t                    rar_size;
    212 
    213     int encode_supported;
    214     int decode_supported;
    215     int hd_encode_supported;
    216     int hd_decode_supported;
    217 
    218     int execIoctlOffset;
    219     int getParamIoctlOffset;
    220 
    221     struct _WsbmBufferPool *main_pool;
    222     struct _WsbmFenceMgr *fence_mgr;
    223 
    224     enum psb_output_method_t output_method;
    225 
    226     /* whether the post-processing use client overlay or not */
    227     int coverlay;
    228     int coverlay_init;
    229     PsbPortPrivRec coverlay_priv;
    230 
    231 
    232     /* whether the post-processing use client textureblit or not */
    233     int ctexture;
    234     struct psb_texture_s ctexture_priv;
    235 
    236     /*
    237     //whether the post-processing use texstreaing or not
    238     int ctexstreaing;
    239     struct psb_texstreaing ctexstreaing_priv;
    240     */
    241 
    242     unsigned char *ws_priv; /* window system related data structure */
    243 
    244 
    245     VASurfaceID cur_displaying_surface;
    246     VASurfaceID last_displaying_surface;
    247 
    248     VADisplayAttribute ble_black_mode;
    249     VADisplayAttribute ble_white_mode;
    250 
    251     VADisplayAttribute blueStretch_gain;
    252     VADisplayAttribute skinColorCorrection_gain;
    253 
    254     VADisplayAttribute brightness;
    255     VADisplayAttribute hue;
    256     VADisplayAttribute contrast;
    257     VADisplayAttribute saturation;
    258     /*Save RenderMode and RenderRect attribute
    259      * for medfield android extend video mode.*/
    260     uint32_t render_device;
    261     uint32_t render_mode;
    262     VARectangle  render_rect;
    263 
    264     unsigned int clear_color;
    265 
    266     int  is_oold;
    267 
    268     unsigned int load_csc_matrix;
    269     signed int   csc_matrix[CSC_MATRIX_X][CSC_MATRIX_Y];
    270 
    271     /* subpic number current buffers support */
    272     unsigned int max_subpic;
    273 
    274     /* for multi-thread safe */
    275     int use_xrandr_thread;
    276     pthread_mutex_t output_mutex;
    277     pthread_t xrandr_thread_id;
    278     int extend_fullscreen;
    279 
    280     int drawable_info;
    281     int dummy_putsurface;
    282     int fixed_fps;
    283     unsigned int frame_count;
    284 
    285     uint32_t blend_mode;
    286     uint32_t blend_color;
    287     uint32_t overlay_auto_paint_color_key;
    288     uint32_t color_key;
    289 
    290     /*output rotation info*/
    291     int disable_msvdx_rotate;
    292     int disable_msvdx_rotate_backup;
    293     int msvdx_rotate_want; /* msvdx rotate info programed to msvdx */
    294     int va_rotate; /* VA rotate passed from APP */
    295     int mipi0_rotation; /* window manager rotation */
    296     int mipi1_rotation; /* window manager rotation */
    297     int hdmi_rotation; /* window manager rotation */
    298     int local_rotation; /* final device rotate: VA rotate+wm rotate */
    299     int extend_rotation; /* final device rotate: VA rotate+wm rotate */
    300     int rotation_dirty;  /*flag for recaculate final rotation*/
    301 
    302     unsigned int outputmethod_checkinterval;
    303 
    304     uint32_t xrandr_dirty;
    305     uint32_t xrandr_update;
    306     /*only VAProfileH264ConstrainedBaseline profile enable error concealment*/
    307     uint32_t ec_enabled;
    308     uint32_t ved_vpp;
    309 
    310     /* vpp is on or off */
    311     int vpp_on;
    312 
    313     uint32_t pre_surfaceid;
    314     psb_decode_info_t decode_info;
    315     drm_psb_msvdx_decode_status_t *msvdx_decode_status;
    316     VASurfaceDecodeMBErrors *surface_mb_error;
    317 
    318     unsigned char *hPVR2DContext;
    319 
    320     VAGenericID wrapped_surface_id[VIDEO_BUFFER_NUM];
    321     VAGenericID wrapped_subpic_id[VIDEO_BUFFER_NUM];
    322     PVR2DMEMINFO *videoBuf[VIDEO_BUFFER_NUM];
    323     PVR2DMEMINFO *subpicBuf[VIDEO_BUFFER_NUM];
    324     void *native_window;
    325     int is_android;
    326     /* VA_RT_FORMAT_PROTECTED is set to protected for Widevine case */
    327     int protected;
    328 };
    329 
    330 
    331 #ifdef _FOR_FPGA_
    332 #define IS_CTP(driver_data)  0
    333 #define IS_MFLD(driver_data) 0
    334 #define IS_MRFL(driver_data) 1
    335 #define IS_MRST(driver_data) 0
    336 #else
    337 #define IS_CTP(driver_data) (((driver_data->dev_id & 0xffff) == 0x08c0) ||  \
    338                      ((driver_data->dev_id & 0xffff) == 0x08c7) ||  \
    339                      ((driver_data->dev_id & 0xffff) == 0x08c8))
    340 #define IS_MRST(driver_data) ((driver_data->dev_id & 0xFFFC) == 0x4100)
    341 #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))
    342 #define IS_MRFL(driver_data) (((driver_data->dev_id & 0xFFFC) == 0x1180) || ((driver_data->dev_id & 0xFFFC) == 0x1480))
    343 #define IS_MOFD(driver_data) ((driver_data->dev_id & 0xFFFC) == 0x1480)
    344 #define IS_LEXINGTON(driver_data) ((driver_data->dev_id & 0xFFFF) == 0x01FF)
    345 #define IS_BAYTRAIL(driver_data) ((driver_data->dev_id & 0xFFFF) == 0x0F31)
    346 #endif
    347 
    348 struct object_config_s {
    349     struct object_base_s base;
    350     VAProfile profile;
    351     VAEntrypoint entrypoint;
    352     VAConfigAttrib attrib_list[PSB_MAX_CONFIG_ATTRIBUTES];
    353     int attrib_count;
    354     format_vtable_p format_vtable;
    355 };
    356 
    357 struct object_context_s {
    358     struct object_base_s base;
    359     VAContextID context_id;
    360     VAConfigID config_id;
    361     VAProfile profile;
    362     VAEntrypoint entry_point;
    363     int picture_width;
    364     int picture_height;
    365     int num_render_targets;
    366     VASurfaceID *render_targets;
    367     int va_flags;
    368 
    369     object_surface_p current_render_target;
    370     object_surface_p ec_target;
    371     object_surface_p ec_candidate;
    372     VASurfaceID current_render_surface_id;
    373     psb_driver_data_p driver_data;
    374     format_vtable_p format_vtable;
    375     unsigned char *format_data;
    376     struct psb_cmdbuf_s *cmdbuf_list[PSB_MAX_CMDBUFS];
    377     struct lnc_cmdbuf_s *lnc_cmdbuf_list[LNC_MAX_CMDBUFS_ENCODE];
    378     struct pnw_cmdbuf_s *pnw_cmdbuf_list[PNW_MAX_CMDBUFS_ENCODE];
    379     struct tng_cmdbuf_s	*tng_cmdbuf_list[TNG_MAX_CMDBUFS_ENCODE];
    380 #ifdef PSBVIDEO_MRFL_VPP
    381     struct vsp_cmdbuf_s *vsp_cmdbuf_list[VSP_MAX_CMDBUFS];
    382 #endif
    383 
    384     struct psb_cmdbuf_s *cmdbuf; /* Current cmd buffer */
    385     struct lnc_cmdbuf_s *lnc_cmdbuf;
    386     struct pnw_cmdbuf_s *pnw_cmdbuf;
    387     struct tng_cmdbuf_s *tng_cmdbuf;
    388 #ifdef PSBVIDEO_MRFL_VPP
    389     struct vsp_cmdbuf_s *vsp_cmdbuf;
    390 #endif
    391 
    392     int cmdbuf_current;
    393 
    394     /* Buffers */
    395     object_buffer_p buffers_unused[PSB_MAX_BUFFERTYPES]; /* Linked lists (HEAD) of unused buffers for each buffer type */
    396     int buffers_unused_count[PSB_MAX_BUFFERTYPES]; /* Linked lists (HEAD) of unused buffers for each buffer type */
    397     object_buffer_p buffers_unused_tail[PSB_MAX_BUFFERTYPES]; /* Linked lists (TAIL) of unused buffers for each buffer type */
    398     object_buffer_p buffers_active[PSB_MAX_BUFFERTYPES]; /* Linked lists of active buffers for each buffer type */
    399 
    400     object_buffer_p *buffer_list; /* for vaRenderPicture */
    401     int num_buffers;
    402 
    403     enum {
    404         psb_video_none = 0,
    405         psb_video_mc,
    406         psb_video_vld,
    407         psb_video_deblock
    408     } video_op;
    409 
    410     uint32_t operating_mode;
    411     uint32_t flags; /* See render flags below */
    412     uint32_t first_mb;
    413     uint32_t last_mb;
    414 
    415     int is_oold;
    416     int msvdx_rotate;
    417     int msvdx_scaling;
    418     int interlaced_stream;
    419 
    420     /* value is 64bits value, consist of 8 bytes
    421      * bytes[0]: entrypoint
    422      * bytes[1]: profile
    423      * bytes[2]: tile stride | rotated tile stride
    424      * bytes[3]: driver_data->protected
    425      * bytes[4]: width_in_mb; pass width kernel for VC1 workaround
    426      */
    427     uint64_t ctp_type;
    428 
    429     unsigned long msvdx_tile; /* normal tile | (rotate tile << 4) */
    430 #ifdef SLICE_HEADER_PARSING
    431     int msvdx_frame_end;
    432     int modular_drm;
    433 #endif
    434 
    435     uint32_t msvdx_context;
    436 
    437     int scaling_width;
    438     int scaling_height;
    439     int scaling_buffer_width;
    440     int scaling_buffer_height;
    441     int scaling_offset_x;
    442     int scaling_offset_y;
    443     int scaling_update;
    444 
    445     /* Debug */
    446     uint32_t frame_count;
    447     uint32_t slice_count;
    448 };
    449 
    450 #define ROTATE_VA2MSVDX(va_rotate)  (va_rotate)
    451 #define CONTEXT_ROTATE(obj_context) (obj_context->msvdx_rotate != ROTATE_VA2MSVDX(VA_ROTATION_NONE))
    452 #define CONTEXT_SCALING(obj_context) (obj_context->msvdx_scaling)
    453 #define CONTEXT_ALTERNATIVE_OUTPUT(obj_context) (CONTEXT_ROTATE(obj_context) || CONTEXT_SCALING(obj_context))
    454 
    455 enum force_output_method_t {
    456     OUTPUT_FORCE_NULL = 0,
    457     OUTPUT_FORCE_GPU,
    458     OUTPUT_FORCE_OVERLAY,
    459     OUTPUT_FORCE_OVERLAY_FOR_SW_DECODE,
    460 };
    461 
    462 #define MAX_SHARE_INFO_KHANDLES 32
    463 struct psb_surface_share_info_s {
    464     //int rotation_sf;                    /*rotaion degree from surface flinger.*/
    465     int surface_rotate;                 /*rotation degree of current rotation surface*/
    466     int metadata_rotate;                /*rotation degree of meta data*/
    467     int width_r;
    468     int height_r;
    469     int surface_protected;              /*whether this surface need be protected*/
    470     /*Force render path.
    471     0 : no fore.
    472     1 : force gpu render;
    473     2 : force overlay render.*/
    474     int force_output_method;
    475     unsigned int out_loop_khandle;
    476     unsigned int renderStatus;
    477     unsigned int used_by_widi;
    478     int bob_deinterlace;
    479     int tiling;
    480     unsigned int width;
    481     unsigned int height;
    482     unsigned int luma_stride;
    483     unsigned int chroma_u_stride;
    484     unsigned int chroma_v_stride;
    485     unsigned int format;
    486     unsigned int khandle;
    487     long long timestamp;
    488 
    489     unsigned int out_loop_luma_stride;
    490     unsigned int out_loop_chroma_u_stride;
    491     unsigned int out_loop_chroma_v_stride;
    492 
    493     long long hwc_timestamp;
    494     unsigned int layer_transform;
    495 
    496     void *native_window;
    497     unsigned int scaling_khandle;
    498     unsigned int width_s;
    499     unsigned int height_s;
    500 
    501     unsigned int scaling_luma_stride;
    502     unsigned int scaling_chroma_u_stride;
    503     unsigned int scaling_chroma_v_stride;
    504 
    505     unsigned int crop_width;
    506     unsigned int crop_height;
    507 
    508     unsigned int coded_width;
    509     unsigned int coded_height;
    510 };
    511 
    512 struct object_surface_s {
    513     struct object_base_s base;
    514     VASurfaceID surface_id;
    515     VAContextID context_id;
    516     int width;
    517     int height;
    518     int height_origin;
    519     int width_r;
    520     int height_r;
    521     int width_s;
    522     int height_s;
    523     int buffer_width_s;
    524     int buffer_height_s;
    525     int offset_x_s;
    526     int offset_y_s;
    527 
    528     struct psb_surface_s *psb_surface;
    529     struct psb_surface_s *out_loop_surface; /* Alternative output surface for rotation */
    530     struct psb_surface_s *scaling_surface; /* Alternative output surface for scaling */
    531     void *subpictures;/* if not NULL, have subpicture information */
    532     unsigned int subpic_count; /* to ensure output have enough space for PDS & RAST */
    533     unsigned int derived_imgcnt; /* is the surface derived by a VAImage? */
    534     unsigned long display_timestamp; /* record the time point of put surface*/
    535     void *rotate_vaddr;
    536     struct psb_surface_share_info_s *share_info;
    537     int is_ref_surface; /* If true, vaDeriveImage returns error */
    538 };
    539 
    540 #define PSB_CODEDBUF_SLICE_NUM_MASK (0xff)
    541 #define PSB_CODEDBUF_SLICE_NUM_SHIFT (0)
    542 
    543 #define PSB_CODEDBUF_NONE_VCL_NUM_MASK (0xff)
    544 #define PSB_CODEDBUF_NONE_VCL_NUM_SHIFT (8)
    545 
    546 #define SET_CODEDBUF_INFO(flag, aux_info, slice_num) \
    547     do {\
    548 	(aux_info) &= ~(PSB_CODEDBUF_##flag##_MASK<<PSB_CODEDBUF_##flag##_SHIFT);\
    549 	(aux_info) |= ((slice_num) & PSB_CODEDBUF_##flag##_MASK)\
    550 	<<PSB_CODEDBUF_##flag##_SHIFT;\
    551     } while (0)
    552 
    553 #define CLEAR_CODEDBUF_INFO(flag, aux_info) \
    554     do {\
    555 	(aux_info) &= ~(PSB_CODEDBUF_##flag##_MASK<<PSB_CODEDBUF_##flag##_SHIFT);\
    556     } while (0)
    557 
    558 #define GET_CODEDBUF_INFO(flag, aux_info) \
    559 	(((aux_info)>>PSB_CODEDBUF_##flag##_SHIFT) & PSB_CODEDBUF_##flag##_MASK)
    560 
    561 
    562 #define PSB_CODEDBUF_SEGMENT_MAX  (9)
    563 
    564 struct object_buffer_s {
    565     struct object_base_s base;
    566     object_buffer_p ptr_next; /* Generic ptr for linked list */
    567     object_buffer_p *pptr_prev_next; /* Generic ptr for linked list */
    568     struct psb_buffer_s *psb_buffer;
    569     unsigned char *buffer_data;
    570     unsigned int size;
    571     unsigned int alloc_size;
    572     unsigned int max_num_elements;
    573     unsigned int num_elements;
    574     object_context_p context;
    575     VABufferType type;
    576     uint32_t last_used;
    577 
    578     /* for VAEncCodedBufferType */
    579     VACodedBufferSegment codedbuf_mapinfo[PSB_CODEDBUF_SEGMENT_MAX];
    580     uint32_t codedbuf_aux_info;
    581 };
    582 
    583 struct object_image_s {
    584     struct object_base_s base;
    585     VAImage image;
    586     unsigned int palette[16];
    587     int subpic_ref;
    588     VASurfaceID derived_surface;
    589 };
    590 
    591 struct object_subpic_s {
    592     struct object_base_s base;
    593     VASubpictureID subpic_id;
    594 
    595     VAImageID image_id;
    596 
    597     /* chromakey range */
    598     unsigned int chromakey_min;
    599     unsigned int chromakey_max;
    600     unsigned int chromakey_mask;
    601 
    602     /* global alpha */
    603     unsigned int global_alpha;
    604 
    605     /* flags */
    606     unsigned int flags; /* see below */
    607 
    608     unsigned char *surfaces; /* surfaces, associated with this subpicture */
    609 };
    610 
    611 typedef struct _PsbSurfaceAttributeTPI {
    612     VASurfaceMemoryType type;
    613     unsigned int width;
    614     unsigned int height;
    615     unsigned int size;
    616     unsigned int pixel_format; /* buffer format */
    617     unsigned int tiling; /* the memory is tiling or not */
    618     unsigned int luma_stride; /* luma stride, could be width aligned with a special value */
    619     unsigned int chroma_u_stride; /* chroma stride */
    620     unsigned int chroma_v_stride;
    621     unsigned int luma_offset; /* could be 0 */
    622     unsigned int chroma_u_offset; /* U offset from the beginning of the memory */
    623     unsigned int chroma_v_offset; /* V offset from the beginning of the memory */
    624     unsigned int count; /* buffer count for surface creation */
    625     unsigned long *buffers; /* buffer handles or user pointers */
    626     unsigned long reserved[4]; /* used to pass additional information, like	362
    627 			* Android native window pointer	363
    628 			*/
    629 } PsbSurfaceAttributeTPI;
    630 
    631 #define MEMSET_OBJECT(ptr, data_struct) \
    632         memset((unsigned char *)ptr + sizeof(struct object_base_s),\
    633                 0,                          \
    634                sizeof(data_struct) - sizeof(struct object_base_s))
    635 
    636 struct format_vtable_s {
    637     void (*queryConfigAttributes)(
    638         VAProfile profile,
    639         VAEntrypoint entrypoint,
    640         VAConfigAttrib *attrib_list,
    641         int num_attribs
    642     );
    643     VAStatus(*validateConfig)(
    644         object_config_p obj_config
    645     );
    646     VAStatus(*createContext)(
    647         object_context_p obj_context,
    648         object_config_p obj_config
    649     );
    650     void (*destroyContext)(
    651         object_context_p obj_context
    652     );
    653     VAStatus(*beginPicture)(
    654         object_context_p obj_context
    655     );
    656     VAStatus(*renderPicture)(
    657         object_context_p obj_context,
    658         object_buffer_p *buffers,
    659         int num_buffers
    660     );
    661     VAStatus(*endPicture)(
    662         object_context_p obj_context
    663     );
    664 };
    665 
    666 #define psb__bounds_check(x, max)                                       \
    667     do { ASSERT(x < max); if (x >= max) x = max - 1; } while(0);
    668 
    669 static inline unsigned long GetTickCount()
    670 {
    671     struct timeval tv;
    672     if (gettimeofday(&tv, NULL))
    673         return 0;
    674     return tv.tv_usec / 1000 + tv.tv_sec * 1000;
    675 }
    676 
    677 inline static char * buffer_type_to_string(int type)
    678 {
    679     switch (type) {
    680     case VAPictureParameterBufferType:
    681         return "VAPictureParameterBufferType";
    682     case VAIQMatrixBufferType:
    683         return "VAIQMatrixBufferType";
    684     case VABitPlaneBufferType:
    685         return "VABitPlaneBufferType";
    686     case VASliceGroupMapBufferType:
    687         return "VASliceGroupMapBufferType";
    688     case VASliceParameterBufferType:
    689         return "VASliceParameterBufferType";
    690     case VASliceDataBufferType:
    691         return "VASliceDataBufferType";
    692     case VAProtectedSliceDataBufferType:
    693         return "VAProtectedSliceDataBufferType";
    694     case VAMacroblockParameterBufferType:
    695         return "VAMacroblockParameterBufferType";
    696     case VAResidualDataBufferType:
    697         return "VAResidualDataBufferType";
    698     case VADeblockingParameterBufferType:
    699         return "VADeblockingParameterBufferType";
    700     case VAImageBufferType:
    701         return "VAImageBufferType";
    702     case VAEncCodedBufferType:
    703         return "VAEncCodedBufferType";
    704     case VAEncSequenceParameterBufferType:
    705         return "VAEncSequenceParameterBufferType";
    706     case VAEncPictureParameterBufferType:
    707         return "VAEncPictureParameterBufferType";
    708     case VAEncSliceParameterBufferType:
    709         return "VAEncSliceParameterBufferType";
    710     case VAEncMiscParameterBufferType:
    711         return "VAEncMiscParameterBufferType";
    712     case VAProbabilityBufferType:
    713 	return "VAProbabilityBufferType";
    714     case VAHuffmanTableBufferType:
    715         return "VAHuffmanTableBufferType";
    716     case VAQMatrixBufferType:
    717         return "VAQMatrixBufferType";
    718     default:
    719         return "UnknowBuffer";
    720     }
    721 }
    722 
    723 inline static int Angle2Rotation(int angle)
    724 {
    725     angle %= 360;
    726     switch (angle) {
    727     case 0:
    728         return VA_ROTATION_NONE;
    729     case 90:
    730         return VA_ROTATION_90;
    731     case 180:
    732         return VA_ROTATION_180;
    733     case 270:
    734         return VA_ROTATION_270;
    735     default:
    736         return -1;
    737     }
    738 }
    739 
    740 inline static int Rotation2Angle(int rotation)
    741 {
    742     switch (rotation) {
    743     case VA_ROTATION_NONE:
    744         return 0;
    745     case VA_ROTATION_90:
    746         return 90;
    747     case VA_ROTATION_180:
    748         return 180;
    749     case VA_ROTATION_270:
    750         return 270;
    751     default:
    752         return -1;
    753     }
    754 }
    755 
    756 int psb_parse_config(char *env, char *env_value);
    757 void psb__destroy_surface(psb_driver_data_p driver_data, object_surface_p obj_surface);
    758 unsigned long psb_tile_stride_mode(int w);
    759 VAStatus psb__checkSurfaceDimensions(psb_driver_data_p driver_data, int width, int height);
    760 
    761 int LOCK_HARDWARE(psb_driver_data_p driver_data);
    762 int UNLOCK_HARDWARE(psb_driver_data_p driver_data);
    763 unsigned long psb__tile_stride_log2_256(int w);
    764 int psb_update_context(psb_driver_data_p driver_data, unsigned long ctx_type);
    765 
    766 #define CHECK_SURFACE(obj_surface) \
    767     do { \
    768         if (NULL == obj_surface) { \
    769             vaStatus = VA_STATUS_ERROR_INVALID_SURFACE; \
    770             DEBUG_FAILURE; \
    771             return vaStatus; \
    772         } \
    773     } while (0)
    774 
    775 #define CHECK_CONFIG(obj_config) \
    776     do { \
    777         if (NULL == obj_config) { \
    778             vaStatus = VA_STATUS_ERROR_INVALID_CONFIG; \
    779             DEBUG_FAILURE; \
    780             return vaStatus; \
    781         } \
    782     } while (0)
    783 
    784 #define CHECK_CONTEXT(obj_context) \
    785     do { \
    786         if (NULL == obj_context) { \
    787             vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT; \
    788             DEBUG_FAILURE; \
    789             return vaStatus; \
    790         } \
    791     } while (0)
    792 
    793 #define CHECK_BUFFER(obj_buffer) \
    794     do { \
    795         if (NULL == obj_buffer) { \
    796             vaStatus = VA_STATUS_ERROR_INVALID_BUFFER; \
    797             DEBUG_FAILURE; \
    798             return vaStatus; \
    799         } \
    800     } while (0)
    801 
    802 #define CHECK_IMAGE(obj_image) \
    803     do { \
    804         if (NULL == obj_image) { \
    805             vaStatus = VA_STATUS_ERROR_INVALID_IMAGE; \
    806             DEBUG_FAILURE; \
    807             return vaStatus; \
    808         } \
    809     } while (0)
    810 
    811 #define CHECK_SUBPICTURE(obj_subpic) \
    812     do { \
    813         if (NULL == obj_subpic) { \
    814             vaStatus = VA_STATUS_ERROR_INVALID_SUBPICTURE; \
    815             DEBUG_FAILURE; \
    816             return vaStatus; \
    817         } \
    818     } while (0)
    819 
    820 #define CHECK_ALLOCATION(buf) \
    821     do { \
    822         if (buf == NULL) { \
    823             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; \
    824             DEBUG_FAILURE; \
    825             return vaStatus; \
    826         } \
    827     } while (0)
    828 
    829 #define CHECK_VASTATUS() \
    830     do { \
    831         if (VA_STATUS_SUCCESS != vaStatus) { \
    832             DEBUG_FAILURE; \
    833             return vaStatus; \
    834         } \
    835     } while (0)
    836 
    837 #define CHECK_INVALID_PARAM(param) \
    838     do { \
    839         if (param) { \
    840             vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER; \
    841             DEBUG_FAILURE; \
    842             return vaStatus; \
    843         } \
    844     } while (0)
    845 
    846 #endif /* _PSB_DRV_VIDEO_H_ */
    847