OSDN Git Service

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