OSDN Git Service

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