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