OSDN Git Service

i965_drv: add support for per-codec max resolution
[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 ENCODER_LP_QUALITY_RANGE  8
77
78 struct i965_surface
79 {
80     struct object_base *base;
81     int type;
82     int flags;
83 };
84
85 struct i965_kernel 
86 {
87     char *name;
88     int interface;
89     const uint32_t (*bin)[4];
90     int size;
91     dri_bo *bo;
92     unsigned int kernel_offset;
93 };
94
95 struct buffer_store
96 {
97     unsigned char *buffer;
98     dri_bo *bo;
99     int ref_count;
100     int num_elements;
101 };
102     
103 struct object_config 
104 {
105     struct object_base base;
106     VAProfile profile;
107     VAEntrypoint entrypoint;
108     VAConfigAttrib attrib_list[I965_MAX_CONFIG_ATTRIBUTES];
109     int num_attribs;
110
111     VAGenericID wrapper_config;
112 };
113
114 #define NUM_SLICES     10
115
116 struct codec_state_base {
117     uint32_t chroma_formats;
118 };
119
120 struct decode_state
121 {
122     struct codec_state_base base;
123     struct buffer_store *pic_param;
124     struct buffer_store **slice_params;
125     struct buffer_store *iq_matrix;
126     struct buffer_store *bit_plane;
127     struct buffer_store *huffman_table;
128     struct buffer_store **slice_datas;
129     struct buffer_store *probability_data;
130     VASurfaceID current_render_target;
131     int max_slice_params;
132     int max_slice_datas;
133     int num_slice_params;
134     int num_slice_datas;
135
136     struct object_surface *render_object;
137     struct object_surface *reference_objects[16]; /* Up to 2 reference surfaces are valid for MPEG-2,*/
138 };
139
140 #define SLICE_PACKED_DATA_INDEX_TYPE    0x80000000
141 #define SLICE_PACKED_DATA_INDEX_MASK    0x00FFFFFF
142
143 struct encode_state
144 {
145     struct codec_state_base base;
146     struct buffer_store *seq_param;
147     struct buffer_store *pic_param;
148     struct buffer_store *pic_control;
149     struct buffer_store *iq_matrix;
150     struct buffer_store *q_matrix;
151     struct buffer_store **slice_params;
152     struct buffer_store *huffman_table;
153     int max_slice_params;
154     int num_slice_params;
155
156     /* for ext */
157     struct buffer_store *seq_param_ext;
158     struct buffer_store *pic_param_ext;
159     struct buffer_store *packed_header_param[5];
160     struct buffer_store *packed_header_data[5];
161     struct buffer_store **slice_params_ext;
162     struct buffer_store *encmb_map;
163     int max_slice_params_ext;
164     int num_slice_params_ext;
165
166     /* Check the user-configurable packed_header attribute.
167      * Currently it is mainly used to check whether the packed slice_header data
168      * is provided by user or the driver.
169      * TBD: It will check for the packed SPS/PPS/MISC/RAWDATA and so on.
170      */
171     unsigned int packed_header_flag;
172     /* For the packed data that needs to be inserted into video clip */
173     /* currently it is mainly to track packed raw data and packed slice_header data. */
174     struct buffer_store **packed_header_params_ext;
175     int max_packed_header_params_ext;
176     int num_packed_header_params_ext;
177     struct buffer_store **packed_header_data_ext;
178     int max_packed_header_data_ext;
179     int num_packed_header_data_ext;
180
181     /* the index of current vps and sps ,special for HEVC*/
182     int vps_sps_seq_index;
183     /* the index of current slice */
184     int slice_index;
185     /* the array is determined by max_slice_params_ext */
186     int max_slice_num;
187     /* This is to store the first index of packed data for one slice */
188     int *slice_rawdata_index;
189     /* This is to store the number of packed data for one slice.
190      * Both packed rawdata and slice_header data are tracked by this
191      * this variable. That is to say: When one packed slice_header is parsed,
192      * this variable will also be increased.
193      */
194     int *slice_rawdata_count;
195
196     /* This is to store the index of packed slice header for one slice */
197     int *slice_header_index;
198
199     int last_packed_header_type;
200
201     struct buffer_store *misc_param[16];
202
203     VASurfaceID current_render_target;
204     struct object_surface *input_yuv_object;
205     struct object_surface *reconstructed_object;
206     struct object_buffer *coded_buf_object;
207     struct object_surface *reference_objects[16]; /* Up to 2 reference surfaces are valid for MPEG-2,*/
208 };
209
210 struct proc_state
211 {
212     struct codec_state_base base;
213     struct buffer_store *pipeline_param;
214
215     VASurfaceID current_render_target;
216 };
217
218 #define CODEC_DEC       0
219 #define CODEC_ENC       1
220 #define CODEC_PROC      2
221
222 union codec_state
223 {
224     struct codec_state_base base;
225     struct decode_state decode;
226     struct encode_state encode;
227     struct proc_state proc;
228 };
229
230 struct hw_context
231 {
232     VAStatus (*run)(VADriverContextP ctx, 
233                     VAProfile profile, 
234                     union codec_state *codec_state,
235                     struct hw_context *hw_context);
236     void (*destroy)(void *);
237     VAStatus (*get_status)(VADriverContextP ctx,
238                            struct hw_context *hw_context,
239                            void *buffer);
240     struct intel_batchbuffer *batch;
241 };
242
243 struct object_context 
244 {
245     struct object_base base;
246     VAContextID context_id;
247     struct object_config *obj_config;
248     VASurfaceID *render_targets;                //input->encode, output->decode
249     int num_render_targets;
250     int picture_width;
251     int picture_height;
252     int flags;
253     int codec_type;
254     union codec_state codec_state;
255     struct hw_context *hw_context;
256
257     VAGenericID       wrapper_context;
258 };
259
260 #define SURFACE_REFERENCED      (1 << 0)
261 #define SURFACE_DERIVED         (1 << 2)
262 #define SURFACE_ALL_MASK        ((SURFACE_REFERENCED) | \
263                                  (SURFACE_DERIVED))
264
265 struct object_surface 
266 {
267     struct object_base base;
268     VASurfaceStatus status;
269     VASubpictureID subpic[I965_MAX_SUBPIC_SUM];
270     struct object_subpic *obj_subpic[I965_MAX_SUBPIC_SUM];
271     unsigned int subpic_render_idx;
272
273     int width;          /* the pitch of plane 0 in bytes in horizontal direction */
274     int height;         /* the pitch of plane 0 in bytes in vertical direction */
275     int size;
276     int orig_width;     /* the width of plane 0 in pixels */
277     int orig_height;    /* the height of plane 0 in pixels */
278     int flags;
279     unsigned int fourcc;    
280     dri_bo *bo;
281     unsigned int expected_format;
282     VAImageID locked_image_id;
283     VAImageID derived_image_id;
284     void (*free_private_data)(void **data);
285     void *private_data;
286     unsigned int subsampling;
287     int x_cb_offset;
288     int y_cb_offset;
289     int x_cr_offset;
290     int y_cr_offset;
291     int cb_cr_width;
292     int cb_cr_height;
293     int cb_cr_pitch;
294     /* user specified attributes see: VASurfaceAttribExternalBuffers/VA_SURFACE_ATTRIB_MEM_TYPE_VA */
295     uint32_t user_disable_tiling : 1;
296     uint32_t user_h_stride_set   : 1;
297     uint32_t user_v_stride_set   : 1;
298
299     VAGenericID wrapper_surface;
300
301     int exported_primefd;
302 };
303
304 struct object_buffer 
305 {
306     struct object_base base;
307     struct buffer_store *buffer_store;
308     int max_num_elements;
309     int num_elements;
310     int size_element;
311     VABufferType type;
312
313     /* Export state */
314     unsigned int export_refcount;
315     VABufferInfo export_state;
316
317     VAGenericID wrapper_buffer;
318     VAContextID context_id;
319 };
320
321 struct object_image 
322 {
323     struct object_base base;
324     VAImage image;
325     dri_bo *bo;
326     unsigned int *palette;
327     VASurfaceID derived_surface;
328 };
329
330 struct object_subpic 
331 {
332     struct object_base base;
333     VAImageID image;
334     struct object_image *obj_image;
335     VARectangle src_rect;
336     VARectangle dst_rect;
337     unsigned int format;
338     int width;
339     int height;
340     int pitch;
341     float global_alpha;
342     dri_bo *bo;
343     unsigned int flags;
344 };
345
346 #define I965_RING_NULL  0
347 #define I965_RING_BSD   1
348 #define I965_RING_BLT   2
349 #define I965_RING_VEBOX 3
350
351 struct i965_filter
352 {
353     VAProcFilterType type;
354     int ring;
355 };
356
357 struct i965_driver_data;
358
359 struct hw_codec_info
360 {
361     struct hw_context *(*dec_hw_context_init)(VADriverContextP, struct object_config *);
362     struct hw_context *(*enc_hw_context_init)(VADriverContextP, struct object_config *);
363     struct hw_context *(*proc_hw_context_init)(VADriverContextP, struct object_config *);
364     bool (*render_init)(VADriverContextP);
365     void (*post_processing_context_init)(VADriverContextP, void *, struct intel_batchbuffer *);
366     void (*preinit_hw_codec)(VADriverContextP, struct hw_codec_info *);
367
368     /**
369      * Allows HW info to support per-codec max resolution.  If this functor is
370      * not initialized, then @max_width and @max_height will be used as the
371      * default maximum resolution for all codecs on this HW info.
372      */
373     void (*max_resolution)(struct i965_driver_data *, struct object_config *, int *, int *);
374
375     int max_width;
376     int max_height;
377     int min_linear_wpitch;
378     int min_linear_hpitch;
379
380     unsigned int h264_mvc_dec_profiles;
381     unsigned int vp9_dec_profiles;
382
383     unsigned int h264_dec_chroma_formats;
384     unsigned int jpeg_dec_chroma_formats;
385     unsigned int jpeg_enc_chroma_formats;
386     unsigned int hevc_dec_chroma_formats;
387     unsigned int vp9_dec_chroma_formats;
388
389     unsigned int has_mpeg2_decoding:1;
390     unsigned int has_mpeg2_encoding:1;
391     unsigned int has_h264_decoding:1;
392     unsigned int has_h264_encoding:1;
393     unsigned int has_vc1_decoding:1;
394     unsigned int has_vc1_encoding:1;
395     unsigned int has_jpeg_decoding:1;
396     unsigned int has_jpeg_encoding:1;
397     unsigned int has_vpp:1;
398     unsigned int has_accelerated_getimage:1;
399     unsigned int has_accelerated_putimage:1;
400     unsigned int has_tiled_surface:1;
401     unsigned int has_di_motion_adptive:1;
402     unsigned int has_di_motion_compensated:1;
403     unsigned int has_vp8_decoding:1;
404     unsigned int has_vp8_encoding:1;
405     unsigned int has_h264_mvc_encoding:1;
406     unsigned int has_hevc_decoding:1;
407     unsigned int has_hevc_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
557 #define I965_SURFACE_MEM_NATIVE             0
558 #define I965_SURFACE_MEM_GEM_FLINK          1
559 #define I965_SURFACE_MEM_DRM_PRIME          2
560
561 void
562 i965_destroy_surface_storage(struct object_surface *obj_surface);
563
564 #endif /* _I965_DRV_VIDEO_H_ */