OSDN Git Service

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