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 workaround
444      */
445     uint64_t ctp_type;
446 
447     unsigned long msvdx_tile; /* normal tile | (rotate tile << 4) */
448 #ifdef SLICE_HEADER_PARSING
449     int msvdx_frame_end;
450     int modular_drm;
451 #endif
452 
453     uint32_t msvdx_context;
454 
455     int scaling_width;
456     int scaling_height;
457     int scaling_buffer_width;
458     int scaling_buffer_height;
459     int scaling_offset_x;
460     int scaling_offset_y;
461     int scaling_update;
462 
463     /* Debug */
464     uint32_t frame_count;
465     uint32_t slice_count;
466 };
467 
468 #define ROTATE_VA2MSVDX(va_rotate)  (va_rotate)
469 #define CONTEXT_ROTATE(obj_context) (obj_context->msvdx_rotate != ROTATE_VA2MSVDX(VA_ROTATION_NONE))
470 #define CONTEXT_SCALING(obj_context) (obj_context->msvdx_scaling)
471 #define CONTEXT_ALTERNATIVE_OUTPUT(obj_context) (CONTEXT_ROTATE(obj_context) || CONTEXT_SCALING(obj_context))
472 
473 enum force_output_method_t {
474     OUTPUT_FORCE_NULL = 0,
475     OUTPUT_FORCE_GPU,
476     OUTPUT_FORCE_OVERLAY,
477     OUTPUT_FORCE_OVERLAY_FOR_SW_DECODE,
478 };
479 
480 #define MAX_SHARE_INFO_KHANDLES 32
481 struct psb_surface_share_info_s {
482     //int rotation_sf;                    /*rotaion degree from surface flinger.*/
483     int surface_rotate;                 /*rotation degree of current rotation surface*/
484     int metadata_rotate;                /*rotation degree of meta data*/
485     int width_r;
486     int height_r;
487     int surface_protected;              /*whether this surface need be protected*/
488     /*Force render path.
489     0 : no fore.
490     1 : force gpu render;
491     2 : force overlay render.*/
492     int force_output_method;
493     unsigned int out_loop_khandle;
494     unsigned int renderStatus;
495     unsigned int used_by_widi;
496     int bob_deinterlace;
497     int tiling;
498     unsigned int width;
499     unsigned int height;
500     unsigned int luma_stride;
501     unsigned int chroma_u_stride;
502     unsigned int chroma_v_stride;
503     unsigned int format;
504     unsigned int khandle;
505     long long timestamp;
506 
507     unsigned int out_loop_luma_stride;
508     unsigned int out_loop_chroma_u_stride;
509     unsigned int out_loop_chroma_v_stride;
510 
511     long long hwc_timestamp;
512     unsigned int layer_transform;
513 
514     void *native_window;
515     unsigned int scaling_khandle;
516     unsigned int width_s;
517     unsigned int height_s;
518 
519     unsigned int scaling_luma_stride;
520     unsigned int scaling_chroma_u_stride;
521     unsigned int scaling_chroma_v_stride;
522 
523     unsigned int crop_width;
524     unsigned int crop_height;
525 
526     unsigned int coded_width;
527     unsigned int coded_height;
528     unsigned int initialized;
529 
530     unsigned int csc_mode;
531     unsigned int video_range;
532 
533 };
534 
535 struct object_surface_s {
536     struct object_base_s base;
537     VASurfaceID surface_id;
538     VAContextID context_id;
539     int width;
540     int height;
541     int height_origin;
542     int width_r;
543     int height_r;
544     int width_s;
545     int height_s;
546     int buffer_width_s;
547     int buffer_height_s;
548     int offset_x_s;
549     int offset_y_s;
550 
551     struct psb_surface_s *psb_surface;
552     struct psb_surface_s *out_loop_surface; /* Alternative output surface for rotation */
553     struct psb_surface_s *scaling_surface; /* Alternative output surface for scaling */
554     void *subpictures;/* if not NULL, have subpicture information */
555     unsigned int subpic_count; /* to ensure output have enough space for PDS & RAST */
556     unsigned int derived_imgcnt; /* is the surface derived by a VAImage? */
557     unsigned long display_timestamp; /* record the time point of put surface*/
558     void *rotate_vaddr;
559     struct psb_surface_share_info_s *share_info;
560     int is_ref_surface; /* If true, vaDeriveImage returns error */
561 };
562 
563 #define PSB_CODEDBUF_SLICE_NUM_MASK (0xff)
564 #define PSB_CODEDBUF_SLICE_NUM_SHIFT (0)
565 
566 #define PSB_CODEDBUF_NONE_VCL_NUM_MASK (0xff)
567 #define PSB_CODEDBUF_NONE_VCL_NUM_SHIFT (8)
568 
569 #define SET_CODEDBUF_INFO(flag, aux_info, slice_num) \
570     do {\
571         (aux_info) &= ~(PSB_CODEDBUF_##flag##_MASK<<PSB_CODEDBUF_##flag##_SHIFT);\
572         (aux_info) |= ((slice_num) & PSB_CODEDBUF_##flag##_MASK)\
573         <<PSB_CODEDBUF_##flag##_SHIFT;\
574     } while (0)
575 
576 #define CLEAR_CODEDBUF_INFO(flag, aux_info) \
577     do {\
578         (aux_info) &= ~(PSB_CODEDBUF_##flag##_MASK<<PSB_CODEDBUF_##flag##_SHIFT);\
579     } while (0)
580 
581 #define GET_CODEDBUF_INFO(flag, aux_info) \
582         (((aux_info)>>PSB_CODEDBUF_##flag##_SHIFT) & PSB_CODEDBUF_##flag##_MASK)
583 
584 
585 #define PSB_CODEDBUF_SEGMENT_MAX  (9)
586 
587 struct object_buffer_s {
588     struct object_base_s base;
589     object_buffer_p ptr_next; /* Generic ptr for linked list */
590     object_buffer_p *pptr_prev_next; /* Generic ptr for linked list */
591     struct psb_buffer_s *psb_buffer;
592     unsigned char *buffer_data;
593     unsigned int size;
594     unsigned int alloc_size;
595     unsigned int max_num_elements;
596     unsigned int num_elements;
597     object_context_p context;
598     VABufferType type;
599     uint32_t last_used;
600 
601     /* for VAEncCodedBufferType */
602     VACodedBufferSegment codedbuf_mapinfo[PSB_CODEDBUF_SEGMENT_MAX];
603     uint32_t codedbuf_aux_info;
604 };
605 
606 struct object_image_s {
607     struct object_base_s base;
608     VAImage image;
609     unsigned int palette[16];
610     int subpic_ref;
611     VASurfaceID derived_surface;
612 };
613 
614 struct object_subpic_s {
615     struct object_base_s base;
616     VASubpictureID subpic_id;
617 
618     VAImageID image_id;
619 
620     /* chromakey range */
621     unsigned int chromakey_min;
622     unsigned int chromakey_max;
623     unsigned int chromakey_mask;
624 
625     /* global alpha */
626     unsigned int global_alpha;
627 
628     /* flags */
629     unsigned int flags; /* see below */
630 
631     unsigned char *surfaces; /* surfaces, associated with this subpicture */
632 };
633 
634 typedef struct _PsbSurfaceAttributeTPI {
635     VASurfaceMemoryType type;
636     unsigned int width;
637     unsigned int height;
638     unsigned int size;
639     unsigned int pixel_format; /* buffer format */
640     unsigned int tiling; /* the memory is tiling or not */
641     unsigned int luma_stride; /* luma stride, could be width aligned with a special value */
642     unsigned int chroma_u_stride; /* chroma stride */
643     unsigned int chroma_v_stride;
644     unsigned int luma_offset; /* could be 0 */
645     unsigned int chroma_u_offset; /* U offset from the beginning of the memory */
646     unsigned int chroma_v_offset; /* V offset from the beginning of the memory */
647     unsigned int count; /* buffer count for surface creation */
648     unsigned long *buffers; /* buffer handles or user pointers */
649     unsigned long reserved[4]; /* used to pass additional information, like	362
650                         * Android native window pointer	363
651                         */
652 } PsbSurfaceAttributeTPI;
653 
654 #define MEMSET_OBJECT(ptr, data_struct) \
655         memset((unsigned char *)ptr + sizeof(struct object_base_s),\
656                 0,                          \
657                sizeof(data_struct) - sizeof(struct object_base_s))
658 
659 struct format_vtable_s {
660     void (*queryConfigAttributes)(
661         VAProfile profile,
662         VAEntrypoint entrypoint,
663         VAConfigAttrib *attrib_list,
664         int num_attribs
665     );
666     VAStatus(*validateConfig)(
667         object_config_p obj_config
668     );
669     VAStatus(*createContext)(
670         object_context_p obj_context,
671         object_config_p obj_config
672     );
673     void (*destroyContext)(
674         object_context_p obj_context
675     );
676     VAStatus(*beginPicture)(
677         object_context_p obj_context
678     );
679     VAStatus(*renderPicture)(
680         object_context_p obj_context,
681         object_buffer_p *buffers,
682         int num_buffers
683     );
684     VAStatus(*endPicture)(
685         object_context_p obj_context
686     );
687 };
688 
689 #define psb__bounds_check(x, max)                                       \
690     do { ASSERT(x < max); if (x >= max) x = max - 1; } while(0);
691 
GetTickCount()692 static inline unsigned long GetTickCount()
693 {
694     struct timeval tv;
695     if (gettimeofday(&tv, NULL))
696         return 0;
697     return tv.tv_usec / 1000 + tv.tv_sec * 1000;
698 }
699 
buffer_type_to_string(int type)700 inline static char * buffer_type_to_string(int type)
701 {
702     switch (type) {
703     case VAPictureParameterBufferType:
704         return "VAPictureParameterBufferType";
705     case VAIQMatrixBufferType:
706         return "VAIQMatrixBufferType";
707     case VABitPlaneBufferType:
708         return "VABitPlaneBufferType";
709     case VASliceGroupMapBufferType:
710         return "VASliceGroupMapBufferType";
711     case VASliceParameterBufferType:
712         return "VASliceParameterBufferType";
713     case VASliceDataBufferType:
714         return "VASliceDataBufferType";
715     case VAProtectedSliceDataBufferType:
716         return "VAProtectedSliceDataBufferType";
717     case VAMacroblockParameterBufferType:
718         return "VAMacroblockParameterBufferType";
719     case VAResidualDataBufferType:
720         return "VAResidualDataBufferType";
721     case VADeblockingParameterBufferType:
722         return "VADeblockingParameterBufferType";
723     case VAImageBufferType:
724         return "VAImageBufferType";
725     case VAEncCodedBufferType:
726         return "VAEncCodedBufferType";
727     case VAEncSequenceParameterBufferType:
728         return "VAEncSequenceParameterBufferType";
729     case VAEncPictureParameterBufferType:
730         return "VAEncPictureParameterBufferType";
731     case VAEncSliceParameterBufferType:
732         return "VAEncSliceParameterBufferType";
733     case VAEncMiscParameterBufferType:
734         return "VAEncMiscParameterBufferType";
735     case VAProbabilityBufferType:
736         return "VAProbabilityBufferType";
737     case VAHuffmanTableBufferType:
738         return "VAHuffmanTableBufferType";
739     case VAQMatrixBufferType:
740         return "VAQMatrixBufferType";
741     default:
742         return "UnknowBuffer";
743     }
744 }
745 
Angle2Rotation(int angle)746 inline static int Angle2Rotation(int angle)
747 {
748     angle %= 360;
749     switch (angle) {
750     case 0:
751         return VA_ROTATION_NONE;
752     case 90:
753         return VA_ROTATION_90;
754     case 180:
755         return VA_ROTATION_180;
756     case 270:
757         return VA_ROTATION_270;
758     default:
759         return -1;
760     }
761 }
762 
Rotation2Angle(int rotation)763 inline static int Rotation2Angle(int rotation)
764 {
765     switch (rotation) {
766     case VA_ROTATION_NONE:
767         return 0;
768     case VA_ROTATION_90:
769         return 90;
770     case VA_ROTATION_180:
771         return 180;
772     case VA_ROTATION_270:
773         return 270;
774     default:
775         return -1;
776     }
777 }
778 
779 int psb_parse_config(char *env, char *env_value);
780 void psb__destroy_surface(psb_driver_data_p driver_data, object_surface_p obj_surface);
781 unsigned long psb_tile_stride_mode(int w);
782 VAStatus psb__checkSurfaceDimensions(psb_driver_data_p driver_data, int width, int height);
783 
784 int LOCK_HARDWARE(psb_driver_data_p driver_data);
785 int UNLOCK_HARDWARE(psb_driver_data_p driver_data);
786 unsigned long psb__tile_stride_log2_256(int w);
787 int psb_update_context(psb_driver_data_p driver_data, unsigned long ctx_type);
788 
789 #define CHECK_SURFACE(obj_surface) \
790     do { \
791         if (NULL == obj_surface) { \
792             vaStatus = VA_STATUS_ERROR_INVALID_SURFACE; \
793             DEBUG_FAILURE; \
794             return vaStatus; \
795         } \
796     } while (0)
797 
798 #define CHECK_CONFIG(obj_config) \
799     do { \
800         if (NULL == obj_config) { \
801             vaStatus = VA_STATUS_ERROR_INVALID_CONFIG; \
802             DEBUG_FAILURE; \
803             return vaStatus; \
804         } \
805     } while (0)
806 
807 #define CHECK_CONTEXT(obj_context) \
808     do { \
809         if (NULL == obj_context) { \
810             vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT; \
811             DEBUG_FAILURE; \
812             return vaStatus; \
813         } \
814     } while (0)
815 
816 #define CHECK_BUFFER(obj_buffer) \
817     do { \
818         if (NULL == obj_buffer) { \
819             vaStatus = VA_STATUS_ERROR_INVALID_BUFFER; \
820             DEBUG_FAILURE; \
821             return vaStatus; \
822         } \
823     } while (0)
824 
825 #define CHECK_IMAGE(obj_image) \
826     do { \
827         if (NULL == obj_image) { \
828             vaStatus = VA_STATUS_ERROR_INVALID_IMAGE; \
829             DEBUG_FAILURE; \
830             return vaStatus; \
831         } \
832     } while (0)
833 
834 #define CHECK_SUBPICTURE(obj_subpic) \
835     do { \
836         if (NULL == obj_subpic) { \
837             vaStatus = VA_STATUS_ERROR_INVALID_SUBPICTURE; \
838             DEBUG_FAILURE; \
839             return vaStatus; \
840         } \
841     } while (0)
842 
843 #define CHECK_ALLOCATION(buf) \
844     do { \
845         if (buf == NULL) { \
846             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; \
847             DEBUG_FAILURE; \
848             return vaStatus; \
849         } \
850     } while (0)
851 
852 #define CHECK_VASTATUS() \
853     do { \
854         if (VA_STATUS_SUCCESS != vaStatus) { \
855             DEBUG_FAILURE; \
856             return vaStatus; \
857         } \
858     } while (0)
859 
860 #define CHECK_INVALID_PARAM(param) \
861     do { \
862         if (param) { \
863             vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER; \
864             DEBUG_FAILURE; \
865             return vaStatus; \
866         } \
867     } while (0)
868 
869 #endif /* _PSB_DRV_VIDEO_H_ */
870