OSDN Git Service

support VP9 profile2 10bits decoding
[android-x86/hardware-intel-common-vaapi.git] / src / i965_drv_video.h
1 /*
2  * Copyright ?2009 Intel Corporation
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  *    Xiang Haihao <haihao.xiang@intel.com>
26  *    Zou Nan hai <nanhai.zou@intel.com>
27  *
28  */
29
30 #ifndef _I965_DRV_VIDEO_H_
31 #define _I965_DRV_VIDEO_H_
32
33 #include <va/va.h>
34 #include <va/va_enc_h264.h>
35 #include <va/va_enc_mpeg2.h>
36 #include <va/va_enc_hevc.h>
37 #include <va/va_enc_jpeg.h>
38 #include <va/va_enc_vp8.h>
39 #include <va/va_vpp.h>
40 #include <va/va_backend.h>
41 #include <va/va_backend_vpp.h>
42
43 #include "i965_mutext.h"
44 #include "object_heap.h"
45 #include "intel_driver.h"
46 #include "i965_fourcc.h"
47
48 #define I965_MAX_PROFILES                       20
49 #define I965_MAX_ENTRYPOINTS                    5
50 #define I965_MAX_CONFIG_ATTRIBUTES              10
51 #define I965_MAX_IMAGE_FORMATS                  10
52 #define I965_MAX_SUBPIC_FORMATS                 6
53 #define I965_MAX_SUBPIC_SUM                     4
54 #define I965_MAX_SURFACE_ATTRIBUTES             16
55
56 #define INTEL_STR_DRIVER_VENDOR                 "Intel"
57 #define INTEL_STR_DRIVER_NAME                   "i965"
58
59 #define I965_SURFACE_TYPE_IMAGE                 0
60 #define I965_SURFACE_TYPE_SURFACE               1
61
62 #define I965_SURFACE_FLAG_FRAME                 0x00000000
63 #define I965_SURFACE_FLAG_TOP_FIELD_FIRST       0x00000001
64 #define I965_SURFACE_FLAG_BOTTOME_FIELD_FIRST   0x00000002
65
66 #define DEFAULT_BRIGHTNESS      0
67 #define DEFAULT_CONTRAST        50
68 #define DEFAULT_HUE             0
69 #define DEFAULT_SATURATION      50
70
71 #define ENCODER_QUALITY_RANGE     2
72 #define ENCODER_DEFAULT_QUALITY   1
73 #define ENCODER_HIGH_QUALITY      ENCODER_DEFAULT_QUALITY
74 #define ENCODER_LOW_QUALITY       2
75
76 struct i965_surface
77 {
78     struct object_base *base;
79     int type;
80     int flags;
81 };
82
83 struct i965_kernel 
84 {
85     char *name;
86     int interface;
87     const uint32_t (*bin)[4];
88     int size;
89     dri_bo *bo;
90     unsigned int kernel_offset;
91 };
92
93 struct buffer_store
94 {
95     unsigned char *buffer;
96     dri_bo *bo;
97     int ref_count;
98     int num_elements;
99 };
100     
101 struct object_config 
102 {
103     struct object_base base;
104     VAProfile profile;
105     VAEntrypoint entrypoint;
106     VAConfigAttrib attrib_list[I965_MAX_CONFIG_ATTRIBUTES];
107     int num_attribs;
108
109     VAGenericID wrapper_config;
110 };
111
112 #define NUM_SLICES     10
113
114 struct codec_state_base {
115     uint32_t chroma_formats;
116 };
117
118 struct decode_state
119 {
120     struct codec_state_base base;
121     struct buffer_store *pic_param;
122     struct buffer_store **slice_params;
123     struct buffer_store *iq_matrix;
124     struct buffer_store *bit_plane;
125     struct buffer_store *huffman_table;
126     struct buffer_store **slice_datas;
127     struct buffer_store *probability_data;
128     VASurfaceID current_render_target;
129     int max_slice_params;
130     int max_slice_datas;
131     int num_slice_params;
132     int num_slice_datas;
133
134     struct object_surface *render_object;
135     struct object_surface *reference_objects[16]; /* Up to 2 reference surfaces are valid for MPEG-2,*/
136 };
137
138 #define SLICE_PACKED_DATA_INDEX_TYPE    0x80000000
139 #define SLICE_PACKED_DATA_INDEX_MASK    0x00FFFFFF
140
141 struct encode_state
142 {
143     struct codec_state_base base;
144     struct buffer_store *seq_param;
145     struct buffer_store *pic_param;
146     struct buffer_store *pic_control;
147     struct buffer_store *iq_matrix;
148     struct buffer_store *q_matrix;
149     struct buffer_store **slice_params;
150     struct buffer_store *huffman_table;
151     int max_slice_params;
152     int num_slice_params;
153
154     /* for ext */
155     struct buffer_store *seq_param_ext;
156     struct buffer_store *pic_param_ext;
157     struct buffer_store *packed_header_param[5];
158     struct buffer_store *packed_header_data[5];
159     struct buffer_store **slice_params_ext;
160     int max_slice_params_ext;
161     int num_slice_params_ext;
162
163     /* Check the user-configurable packed_header attribute.
164      * Currently it is mainly used to check whether the packed slice_header data
165      * is provided by user or the driver.
166      * TBD: It will check for the packed SPS/PPS/MISC/RAWDATA and so on.
167      */
168     unsigned int packed_header_flag;
169     /* For the packed data that needs to be inserted into video clip */
170     /* currently it is mainly to track packed raw data and packed slice_header data. */
171     struct buffer_store **packed_header_params_ext;
172     int max_packed_header_params_ext;
173     int num_packed_header_params_ext;
174     struct buffer_store **packed_header_data_ext;
175     int max_packed_header_data_ext;
176     int num_packed_header_data_ext;
177
178     /* the index of current vps and sps ,special for HEVC*/
179     int vps_sps_seq_index;
180     /* the index of current slice */
181     int slice_index;
182     /* the array is determined by max_slice_params_ext */
183     int max_slice_num;
184     /* This is to store the first index of packed data for one slice */
185     int *slice_rawdata_index;
186     /* This is to store the number of packed data for one slice.
187      * Both packed rawdata and slice_header data are tracked by this
188      * this variable. That is to say: When one packed slice_header is parsed,
189      * this variable will also be increased.
190      */
191     int *slice_rawdata_count;
192
193     /* This is to store the index of packed slice header for one slice */
194     int *slice_header_index;
195
196     int last_packed_header_type;
197
198     struct buffer_store *misc_param[16];
199
200     VASurfaceID current_render_target;
201     struct object_surface *input_yuv_object;
202     struct object_surface *reconstructed_object;
203     struct object_buffer *coded_buf_object;
204     struct object_surface *reference_objects[16]; /* Up to 2 reference surfaces are valid for MPEG-2,*/
205 };
206
207 struct proc_state
208 {
209     struct codec_state_base base;
210     struct buffer_store *pipeline_param;
211
212     VASurfaceID current_render_target;
213 };
214
215 #define CODEC_DEC       0
216 #define CODEC_ENC       1
217 #define CODEC_PROC      2
218
219 union codec_state
220 {
221     struct codec_state_base base;
222     struct decode_state decode;
223     struct encode_state encode;
224     struct proc_state proc;
225 };
226
227 struct hw_context
228 {
229     VAStatus (*run)(VADriverContextP ctx, 
230                     VAProfile profile, 
231                     union codec_state *codec_state,
232                     struct hw_context *hw_context);
233     void (*destroy)(void *);
234     struct intel_batchbuffer *batch;
235 };
236
237 struct object_context 
238 {
239     struct object_base base;
240     VAContextID context_id;
241     struct object_config *obj_config;
242     VASurfaceID *render_targets;                //input->encode, output->decode
243     int num_render_targets;
244     int picture_width;
245     int picture_height;
246     int flags;
247     int codec_type;
248     union codec_state codec_state;
249     struct hw_context *hw_context;
250
251     VAGenericID       wrapper_context;
252 };
253
254 #define SURFACE_REFERENCED      (1 << 0)
255 #define SURFACE_DERIVED         (1 << 2)
256 #define SURFACE_ALL_MASK        ((SURFACE_REFERENCED) | \
257                                  (SURFACE_DERIVED))
258
259 struct object_surface 
260 {
261     struct object_base base;
262     VASurfaceStatus status;
263     VASubpictureID subpic[I965_MAX_SUBPIC_SUM];
264     struct object_subpic *obj_subpic[I965_MAX_SUBPIC_SUM];
265     unsigned int subpic_render_idx;
266
267     int width;          /* the pitch of plane 0 in bytes in horizontal direction */
268     int height;         /* the pitch of plane 0 in bytes in vertical direction */
269     int size;
270     int orig_width;     /* the width of plane 0 in pixels */
271     int orig_height;    /* the height of plane 0 in pixels */
272     int flags;
273     unsigned int fourcc;    
274     dri_bo *bo;
275     unsigned int expected_format;
276     VAImageID locked_image_id;
277     VAImageID derived_image_id;
278     void (*free_private_data)(void **data);
279     void *private_data;
280     unsigned int subsampling;
281     int x_cb_offset;
282     int y_cb_offset;
283     int x_cr_offset;
284     int y_cr_offset;
285     int cb_cr_width;
286     int cb_cr_height;
287     int cb_cr_pitch;
288     /* user specified attributes see: VASurfaceAttribExternalBuffers/VA_SURFACE_ATTRIB_MEM_TYPE_VA */
289     uint32_t user_disable_tiling : 1;
290     uint32_t user_h_stride_set   : 1;
291     uint32_t user_v_stride_set   : 1;
292
293     VAGenericID wrapper_surface;
294
295     int exported_primefd;
296 };
297
298 struct object_buffer 
299 {
300     struct object_base base;
301     struct buffer_store *buffer_store;
302     int max_num_elements;
303     int num_elements;
304     int size_element;
305     VABufferType type;
306
307     /* Export state */
308     unsigned int export_refcount;
309     VABufferInfo export_state;
310
311     VAGenericID wrapper_buffer;
312 };
313
314 struct object_image 
315 {
316     struct object_base base;
317     VAImage image;
318     dri_bo *bo;
319     unsigned int *palette;
320     VASurfaceID derived_surface;
321 };
322
323 struct object_subpic 
324 {
325     struct object_base base;
326     VAImageID image;
327     struct object_image *obj_image;
328     VARectangle src_rect;
329     VARectangle dst_rect;
330     unsigned int format;
331     int width;
332     int height;
333     int pitch;
334     float global_alpha;
335     dri_bo *bo;
336     unsigned int flags;
337 };
338
339 #define I965_RING_NULL  0
340 #define I965_RING_BSD   1
341 #define I965_RING_BLT   2
342 #define I965_RING_VEBOX 3
343
344 struct i965_filter
345 {
346     VAProcFilterType type;
347     int ring;
348 };
349
350 struct hw_codec_info
351 {
352     struct hw_context *(*dec_hw_context_init)(VADriverContextP, struct object_config *);
353     struct hw_context *(*enc_hw_context_init)(VADriverContextP, struct object_config *);
354     struct hw_context *(*proc_hw_context_init)(VADriverContextP, struct object_config *);
355     bool (*render_init)(VADriverContextP);
356     void (*post_processing_context_init)(VADriverContextP, void *, struct intel_batchbuffer *);
357     void (*preinit_hw_codec)(VADriverContextP, struct hw_codec_info *);
358
359     int max_width;
360     int max_height;
361     int min_linear_wpitch;
362     int min_linear_hpitch;
363
364     unsigned int h264_mvc_dec_profiles;
365     unsigned int vp9_dec_profiles;
366
367     unsigned int h264_dec_chroma_formats;
368     unsigned int jpeg_dec_chroma_formats;
369     unsigned int jpeg_enc_chroma_formats;
370     unsigned int hevc_dec_chroma_formats;
371     unsigned int vp9_dec_chroma_formats;
372
373     unsigned int has_mpeg2_decoding:1;
374     unsigned int has_mpeg2_encoding:1;
375     unsigned int has_h264_decoding:1;
376     unsigned int has_h264_encoding:1;
377     unsigned int has_vc1_decoding:1;
378     unsigned int has_vc1_encoding:1;
379     unsigned int has_jpeg_decoding:1;
380     unsigned int has_jpeg_encoding:1;
381     unsigned int has_vpp:1;
382     unsigned int has_accelerated_getimage:1;
383     unsigned int has_accelerated_putimage:1;
384     unsigned int has_tiled_surface:1;
385     unsigned int has_di_motion_adptive:1;
386     unsigned int has_di_motion_compensated:1;
387     unsigned int has_vp8_decoding:1;
388     unsigned int has_vp8_encoding:1;
389     unsigned int has_h264_mvc_encoding:1;
390     unsigned int has_hevc_decoding:1;
391     unsigned int has_hevc_encoding:1;
392     unsigned int has_hevc10_decoding:1;
393     unsigned int has_vp9_decoding:1;
394     unsigned int has_vpp_p010:1;
395
396     unsigned int num_filters;
397     struct i965_filter filters[VAProcFilterCount];
398 };
399
400
401 #include "i965_render.h"
402
403 struct i965_driver_data 
404 {
405     struct intel_driver_data intel;
406     struct object_heap config_heap;
407     struct object_heap context_heap;
408     struct object_heap surface_heap;
409     struct object_heap buffer_heap;
410     struct object_heap image_heap;
411     struct object_heap subpic_heap;
412     struct hw_codec_info *codec_info;
413
414     _I965Mutex render_mutex;
415     _I965Mutex pp_mutex;
416     struct intel_batchbuffer *batch;
417     struct intel_batchbuffer *pp_batch;
418     struct i965_render_state render_state;
419     void *pp_context;
420     char va_vendor[256];
421  
422     VADisplayAttribute *display_attributes;
423     unsigned int num_display_attributes;
424     VADisplayAttribute *rotation_attrib;
425     VADisplayAttribute *brightness_attrib;
426     VADisplayAttribute *contrast_attrib;
427     VADisplayAttribute *hue_attrib;
428     VADisplayAttribute *saturation_attrib;
429     VAContextID current_context_id;
430
431     /* VA/DRI (X11) specific data */
432     struct va_dri_output *dri_output;
433
434     /* VA/Wayland specific data */
435     struct va_wl_output *wl_output;
436
437     VADriverContextP wrapper_pdrvctx;
438 };
439
440 #define NEW_CONFIG_ID() object_heap_allocate(&i965->config_heap);
441 #define NEW_CONTEXT_ID() object_heap_allocate(&i965->context_heap);
442 #define NEW_SURFACE_ID() object_heap_allocate(&i965->surface_heap);
443 #define NEW_BUFFER_ID() object_heap_allocate(&i965->buffer_heap);
444 #define NEW_IMAGE_ID() object_heap_allocate(&i965->image_heap);
445 #define NEW_SUBPIC_ID() object_heap_allocate(&i965->subpic_heap);
446
447 #define CONFIG(id) ((struct object_config *)object_heap_lookup(&i965->config_heap, id))
448 #define CONTEXT(id) ((struct object_context *)object_heap_lookup(&i965->context_heap, id))
449 #define SURFACE(id) ((struct object_surface *)object_heap_lookup(&i965->surface_heap, id))
450 #define BUFFER(id) ((struct object_buffer *)object_heap_lookup(&i965->buffer_heap, id))
451 #define IMAGE(id) ((struct object_image *)object_heap_lookup(&i965->image_heap, id))
452 #define SUBPIC(id) ((struct object_subpic *)object_heap_lookup(&i965->subpic_heap, id))
453
454 #define FOURCC_IA44 0x34344149
455 #define FOURCC_AI44 0x34344941
456
457 #define STRIDE(w)               (((w) + 0xf) & ~0xf)
458 #define SIZE_YUV420(w, h)       (h * (STRIDE(w) + STRIDE(w >> 1)))
459
460 static INLINE struct i965_driver_data *
461 i965_driver_data(VADriverContextP ctx)
462 {
463     return (struct i965_driver_data *)(ctx->pDriverData);
464 }
465
466 VAStatus
467 i965_check_alloc_surface_bo(VADriverContextP ctx,
468                             struct object_surface *obj_surface,
469                             int tiled,
470                             unsigned int fourcc,
471                             unsigned int subsampling);
472
473 int
474 va_enc_packed_type_to_idx(int packed_type);
475
476 /* reserve 2 byte for internal using */
477 #define CODEC_H264      0
478 #define CODEC_MPEG2     1
479 #define CODEC_H264_MVC  2
480 #define CODEC_JPEG      3
481 #define CODEC_VP8       4
482 #define CODEC_HEVC      5
483
484 #define H264_DELIMITER0 0x00
485 #define H264_DELIMITER1 0x00
486 #define H264_DELIMITER2 0x00
487 #define H264_DELIMITER3 0x00
488 #define H264_DELIMITER4 0x00
489
490 #define MPEG2_DELIMITER0        0x00
491 #define MPEG2_DELIMITER1        0x00
492 #define MPEG2_DELIMITER2        0x00
493 #define MPEG2_DELIMITER3        0x00
494 #define MPEG2_DELIMITER4        0xb0
495
496 #define HEVC_DELIMITER0 0x00
497 #define HEVC_DELIMITER1 0x00
498 #define HEVC_DELIMITER2 0x00
499 #define HEVC_DELIMITER3 0x00
500 #define HEVC_DELIMITER4 0x00
501
502 struct i965_coded_buffer_segment
503 {
504     VACodedBufferSegment base;
505     unsigned char mapped;
506     unsigned char codec;
507 };
508
509 #define I965_CODEDBUFFER_HEADER_SIZE   ALIGN(sizeof(struct i965_coded_buffer_segment), 0x1000)
510
511 extern VAStatus i965_MapBuffer(VADriverContextP ctx,
512                 VABufferID buf_id,       /* in */
513                 void **pbuf);            /* out */
514
515 extern VAStatus i965_UnmapBuffer(VADriverContextP ctx, VABufferID buf_id);
516
517 extern VAStatus i965_DestroySurfaces(VADriverContextP ctx,
518                      VASurfaceID *surface_list,
519                      int num_surfaces);
520
521 extern VAStatus i965_CreateSurfaces(VADriverContextP ctx,
522                     int width,
523                     int height,
524                     int format,
525                     int num_surfaces,
526                     VASurfaceID *surfaces);
527
528 #define I965_SURFACE_MEM_NATIVE             0
529 #define I965_SURFACE_MEM_GEM_FLINK          1
530 #define I965_SURFACE_MEM_DRM_PRIME          2
531
532 void
533 i965_destroy_surface_storage(struct object_surface *obj_surface);
534
535 #endif /* _I965_DRV_VIDEO_H_ */