OSDN Git Service

Fix wrong y_cb/cr_offset when the imported surfaces's fourcc is VA_FOURCC_411P
[android-x86/hardware-intel-common-vaapi.git] / src / i965_drv_video.c
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 #include "sysdeps.h"
31 #include <unistd.h>
32 #include <dlfcn.h>
33
34 #ifdef HAVE_VA_X11
35 # include "i965_output_dri.h"
36 #endif
37
38 #ifdef HAVE_VA_WAYLAND
39 # include "i965_output_wayland.h"
40 #endif
41
42 #include "intel_version.h"
43 #include "intel_driver.h"
44 #include "intel_memman.h"
45 #include "intel_batchbuffer.h"
46 #include "i965_defines.h"
47 #include "i965_drv_video.h"
48 #include "i965_decoder.h"
49 #include "i965_encoder.h"
50
51 #include "i965_post_processing.h"
52
53 #include "gen9_vp9_encapi.h"
54
55 #define CONFIG_ID_OFFSET                0x01000000
56 #define CONTEXT_ID_OFFSET               0x02000000
57 #define SURFACE_ID_OFFSET               0x04000000
58 #define BUFFER_ID_OFFSET                0x08000000
59 #define IMAGE_ID_OFFSET                 0x0a000000
60 #define SUBPIC_ID_OFFSET                0x10000000
61
62 static int get_sampling_from_fourcc(unsigned int fourcc);
63
64 /* Check whether we are rendering to X11 (VA/X11 or VA/GLX API) */
65 #define IS_VA_X11(ctx) \
66     (((ctx)->display_type & VA_DISPLAY_MAJOR_MASK) == VA_DISPLAY_X11)
67
68 /* Check whether we are rendering to Wayland */
69 #define IS_VA_WAYLAND(ctx) \
70     (((ctx)->display_type & VA_DISPLAY_MAJOR_MASK) == VA_DISPLAY_WAYLAND)
71
72 #define I965_BIT        1
73 #define I965_2BITS      (I965_BIT << 1)
74 #define I965_4BITS      (I965_BIT << 2)
75 #define I965_8BITS      (I965_BIT << 3)
76 #define I965_16BITS     (I965_BIT << 4)
77 #define I965_32BITS     (I965_BIT << 5)
78
79 #define PLANE_0         0
80 #define PLANE_1         1
81 #define PLANE_2         2
82
83 #define OFFSET_0        0
84 #define OFFSET_4        4
85 #define OFFSET_8        8
86 #define OFFSET_16       16
87 #define OFFSET_24       24
88
89 /* hfactor, vfactor, num_planes, bpp[], num_components, components[] */
90 #define I_NV12  2, 2, 2, {I965_8BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_1, OFFSET_8} }
91 #define I_I420  2, 2, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
92 #define I_IYUV  I_I420
93 #define I_IMC3  I_I420
94 #define I_YV12  2, 2, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_2, OFFSET_0}, {PLANE_1, OFFSET_0} }
95 #define I_IMC1  I_YV12
96
97 #define I_P010  2, 2, 2, {I965_16BITS, I965_8BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_1, OFFSET_16} }
98
99 #define I_I010  2, 2, 3, {I965_16BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
100
101 #define I_422H  2, 1, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
102 #define I_422V  1, 2, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
103 #define I_YV16  2, 1, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_2, OFFSET_0}, {PLANE_1, OFFSET_0} }
104 #define I_YUY2  2, 1, 1, {I965_16BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_24} }
105 #define I_UYVY  2, 1, 1, {I965_16BITS}, 3, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_16} }
106
107 #define I_444P  1, 1, 3, {I965_8BITS, I965_8BITS, I965_8BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
108
109 #define I_411P  4, 1, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
110
111 #define I_Y800  1, 1, 1, {I965_8BITS}, 1, { {PLANE_0, OFFSET_0} }
112
113 #define I_RGBA  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_24} }
114 #define I_RGBX  1, 1, 1, {I965_32BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16} }
115 #define I_BGRA  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_24} }
116 #define I_BGRX  1, 1, 1, {I965_32BITS}, 3, { {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
117
118 #define I_ARGB  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_24}, {PLANE_0, OFFSET_0} }
119 #define I_ABGR  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_24}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
120
121 #define I_IA88  1, 1, 1, {I965_16BITS}, 2, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8} }
122 #define I_AI88  1, 1, 1, {I965_16BITS}, 2, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
123
124 #define I_IA44  1, 1, 1, {I965_8BITS}, 2, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_4} }
125 #define I_AI44  1, 1, 1, {I965_8BITS}, 2, { {PLANE_0, OFFSET_4}, {PLANE_0, OFFSET_0} }
126
127 /* flag */
128 #define I_S             1
129 #define I_I             2
130 #define I_SI            (I_S | I_I)
131
132 #define DEF_FOUCC_INFO(FOURCC, FORMAT, SUB, FLAG)       { VA_FOURCC_##FOURCC, I965_COLOR_##FORMAT, SUBSAMPLE_##SUB, FLAG, I_##FOURCC }
133 #define DEF_YUV(FOURCC, SUB, FLAG)                      DEF_FOUCC_INFO(FOURCC, YUV, SUB, FLAG)
134 #define DEF_RGB(FOURCC, SUB, FLAG)                      DEF_FOUCC_INFO(FOURCC, RGB, SUB, FLAG)
135 #define DEF_INDEX(FOURCC, SUB, FLAG)                    DEF_FOUCC_INFO(FOURCC, INDEX, SUB, FLAG)
136
137 static const i965_fourcc_info i965_fourcc_infos[] = {
138     DEF_YUV(NV12, YUV420, I_SI),
139     DEF_YUV(I420, YUV420, I_SI),
140     DEF_YUV(IYUV, YUV420, I_S),
141     DEF_YUV(IMC3, YUV420, I_S),
142     DEF_YUV(YV12, YUV420, I_SI),
143     DEF_YUV(IMC1, YUV420, I_S),
144
145     DEF_YUV(P010, YUV420, I_SI),
146     DEF_YUV(I010, YUV420, I_S),
147
148     DEF_YUV(422H, YUV422H, I_SI),
149     DEF_YUV(422V, YUV422V, I_S),
150     DEF_YUV(YV16, YUV422H, I_S),
151     DEF_YUV(YUY2, YUV422H, I_SI),
152     DEF_YUV(UYVY, YUV422H, I_SI),
153
154     DEF_YUV(444P, YUV444, I_S),
155
156     DEF_YUV(411P, YUV411, I_S),
157
158     DEF_YUV(Y800, YUV400, I_S),
159
160     DEF_RGB(RGBA, RGBX, I_SI),
161     DEF_RGB(RGBX, RGBX, I_SI),
162     DEF_RGB(BGRA, RGBX, I_SI),
163     DEF_RGB(BGRX, RGBX, I_SI),
164
165     DEF_RGB(ARGB, RGBX, I_I),
166     DEF_RGB(ABGR, RGBX, I_I),
167
168     DEF_INDEX(IA88, RGBX, I_I),
169     DEF_INDEX(AI88, RGBX, I_I),
170
171     DEF_INDEX(IA44, RGBX, I_I),
172     DEF_INDEX(AI44, RGBX, I_I)
173 };
174
175 const i965_fourcc_info *
176 get_fourcc_info(unsigned int fourcc)
177 {
178     unsigned int i;
179
180     for (i = 0; i < ARRAY_ELEMS(i965_fourcc_infos); i++) {
181         const i965_fourcc_info * const info = &i965_fourcc_infos[i];
182
183         if (info->fourcc == fourcc)
184             return info;
185     }
186
187     return NULL;
188 }
189
190 static int
191 get_bpp_from_fourcc(unsigned int fourcc)
192 {
193     const i965_fourcc_info *info = get_fourcc_info(fourcc);
194     unsigned int i = 0;
195     unsigned int bpp = 0;
196
197     if (!info)
198         return 0;
199
200     for (i = 0; i < info->num_planes; i++)
201         bpp += info->bpp[i];
202
203     return bpp;
204 }
205
206 enum {
207     I965_SURFACETYPE_RGBA = 1,
208     I965_SURFACETYPE_YUV,
209     I965_SURFACETYPE_INDEXED
210 };
211
212 /* List of supported display attributes */
213 static const VADisplayAttribute i965_display_attributes[] = {
214     {
215         VADisplayAttribBrightness,
216         -100, 100, DEFAULT_BRIGHTNESS,
217         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
218     },
219
220     {
221         VADisplayAttribContrast,
222         0, 100, DEFAULT_CONTRAST,
223         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
224     },
225
226     {
227         VADisplayAttribHue,
228         -180, 180, DEFAULT_HUE,
229         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
230     },
231
232     {
233         VADisplayAttribSaturation,
234         0, 100, DEFAULT_SATURATION,
235         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
236     },
237
238     {
239         VADisplayAttribRotation,
240         0, 3, VA_ROTATION_NONE,
241         VA_DISPLAY_ATTRIB_GETTABLE|VA_DISPLAY_ATTRIB_SETTABLE
242     },
243 };
244
245 /* List of supported image formats */
246 typedef struct {
247     unsigned int        type;
248     VAImageFormat       va_format;
249 } i965_image_format_map_t;
250
251 static const i965_image_format_map_t
252 i965_image_formats_map[I965_MAX_IMAGE_FORMATS + 1] = {
253     { I965_SURFACETYPE_YUV,
254       { VA_FOURCC_YV12, VA_LSB_FIRST, 12, } },
255     { I965_SURFACETYPE_YUV,
256       { VA_FOURCC_I420, VA_LSB_FIRST, 12, } },
257     { I965_SURFACETYPE_YUV,
258       { VA_FOURCC_NV12, VA_LSB_FIRST, 12, } },
259     { I965_SURFACETYPE_YUV,
260       { VA_FOURCC_YUY2, VA_LSB_FIRST, 16, } },
261     { I965_SURFACETYPE_YUV,
262       { VA_FOURCC_UYVY, VA_LSB_FIRST, 16, } },
263     { I965_SURFACETYPE_YUV,
264       { VA_FOURCC_422H, VA_LSB_FIRST, 16, } },
265     { I965_SURFACETYPE_RGBA,
266       { VA_FOURCC_RGBX, VA_LSB_FIRST, 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000 } },
267     { I965_SURFACETYPE_RGBA,
268       { VA_FOURCC_BGRX, VA_LSB_FIRST, 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff } },
269     { I965_SURFACETYPE_YUV,
270       { VA_FOURCC_P010, VA_LSB_FIRST, 24, } },
271 };
272
273 /* List of supported subpicture formats */
274 typedef struct {
275     unsigned int        type;
276     unsigned int        format;
277     VAImageFormat       va_format;
278     unsigned int        va_flags;
279 } i965_subpic_format_map_t;
280
281 #define COMMON_SUBPICTURE_FLAGS                 \
282     (VA_SUBPICTURE_DESTINATION_IS_SCREEN_COORD| \
283      VA_SUBPICTURE_GLOBAL_ALPHA)
284
285 static const i965_subpic_format_map_t
286 i965_subpic_formats_map[I965_MAX_SUBPIC_FORMATS + 1] = {
287     { I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_P4A4_UNORM,
288       { VA_FOURCC_IA44, VA_MSB_FIRST, 8, },
289       COMMON_SUBPICTURE_FLAGS },
290     { I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_A4P4_UNORM,
291       { VA_FOURCC_AI44, VA_MSB_FIRST, 8, },
292       COMMON_SUBPICTURE_FLAGS },
293     { I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_P8A8_UNORM,
294       { VA_FOURCC_IA88, VA_MSB_FIRST, 16, },
295       COMMON_SUBPICTURE_FLAGS },
296     { I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_A8P8_UNORM,
297       { VA_FOURCC_AI88, VA_MSB_FIRST, 16, },
298       COMMON_SUBPICTURE_FLAGS },
299      { I965_SURFACETYPE_RGBA, I965_SURFACEFORMAT_B8G8R8A8_UNORM,
300       { VA_FOURCC_BGRA, VA_LSB_FIRST, 32,
301         32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 },
302       COMMON_SUBPICTURE_FLAGS },
303     { I965_SURFACETYPE_RGBA, I965_SURFACEFORMAT_R8G8B8A8_UNORM,
304       { VA_FOURCC_RGBA, VA_LSB_FIRST, 32,
305         32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 },
306       COMMON_SUBPICTURE_FLAGS },
307 };
308
309 static const i965_subpic_format_map_t *
310 get_subpic_format(const VAImageFormat *va_format)
311 {
312     unsigned int i;
313     for (i = 0; i965_subpic_formats_map[i].type != 0; i++) {
314         const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[i];
315         if (m->va_format.fourcc == va_format->fourcc &&
316             (m->type == I965_SURFACETYPE_RGBA ?
317              (m->va_format.byte_order == va_format->byte_order &&
318               m->va_format.red_mask   == va_format->red_mask   &&
319               m->va_format.green_mask == va_format->green_mask &&
320               m->va_format.blue_mask  == va_format->blue_mask  &&
321               m->va_format.alpha_mask == va_format->alpha_mask) : 1))
322             return m;
323     }
324     return NULL;
325 }
326
327 /* Checks whether the surface is in busy state */
328 static bool
329 is_surface_busy(struct i965_driver_data *i965,
330     struct object_surface *obj_surface)
331 {
332     assert(obj_surface != NULL);
333
334     if (obj_surface->locked_image_id != VA_INVALID_ID)
335         return true;
336     if (obj_surface->derived_image_id != VA_INVALID_ID)
337         return true;
338     return false;
339 }
340
341 /* Checks whether the image is in busy state */
342 static bool
343 is_image_busy(struct i965_driver_data *i965, struct object_image *obj_image, VASurfaceID surface)
344 {
345     struct object_buffer *obj_buffer;
346
347     assert(obj_image != NULL);
348
349     if (obj_image->derived_surface != VA_INVALID_ID &&
350         obj_image->derived_surface == surface)
351         return true;
352
353     obj_buffer = BUFFER(obj_image->image.buf);
354     if (obj_buffer && obj_buffer->export_refcount > 0)
355         return true;
356     return false;
357 }
358
359 #define I965_PACKED_HEADER_BASE         0
360 #define I965_SEQ_PACKED_HEADER_BASE     0
361 #define I965_SEQ_PACKED_HEADER_END      2
362 #define I965_PIC_PACKED_HEADER_BASE     2
363 #define I965_PACKED_MISC_HEADER_BASE    4
364
365 int
366 va_enc_packed_type_to_idx(int packed_type)
367 {
368     int idx = 0;
369
370     if (packed_type & VAEncPackedHeaderMiscMask) {
371         idx = I965_PACKED_MISC_HEADER_BASE;
372         packed_type = (~VAEncPackedHeaderMiscMask & packed_type);
373         ASSERT_RET(packed_type > 0, 0);
374         idx += (packed_type - 1);
375     } else {
376         idx = I965_PACKED_HEADER_BASE;
377
378         switch (packed_type) {
379         case VAEncPackedHeaderSequence:
380             idx = I965_SEQ_PACKED_HEADER_BASE + 0;
381             break;
382
383         case VAEncPackedHeaderPicture:
384             idx = I965_PIC_PACKED_HEADER_BASE + 0;
385             break;
386
387         case VAEncPackedHeaderSlice:
388             idx = I965_PIC_PACKED_HEADER_BASE + 1;
389             break;
390
391         default:
392             /* Should not get here */
393             ASSERT_RET(0, 0);
394             break;
395         }
396     }
397
398     ASSERT_RET(idx < 5, 0);
399     return idx;
400 }
401
402 #define CALL_VTABLE(vawr, status, param) status = (vawr->vtable->param)
403
404 static VAStatus
405 i965_surface_wrapper(VADriverContextP ctx, VASurfaceID surface)
406 {
407     struct i965_driver_data *i965 = i965_driver_data(ctx);
408     struct object_surface *obj_surface = SURFACE(surface);
409     VAStatus va_status = VA_STATUS_SUCCESS;
410
411     if (!obj_surface) {
412         return VA_STATUS_ERROR_INVALID_SURFACE;
413     }
414
415     if (obj_surface->wrapper_surface != VA_INVALID_ID) {
416         /* the wrapped surface already exists. just return it */
417        return va_status;
418     }
419
420     if (obj_surface->fourcc == 0)
421         i965_check_alloc_surface_bo(ctx, obj_surface,
422                                     1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
423
424     /*
425      * TBD: Support more surface formats.
426      * Currently only NV12 is support as NV12 is used by decoding.
427      */
428     if (obj_surface->fourcc != VA_FOURCC_NV12 )
429         return VA_STATUS_ERROR_INVALID_PARAMETER;
430
431     if ((i965->wrapper_pdrvctx == NULL) ||
432         (obj_surface->bo == NULL))
433         return VA_STATUS_ERROR_INVALID_PARAMETER;
434
435     {
436         int fd_handle;
437         VASurfaceAttrib attrib_list[2];
438         VASurfaceAttribExternalBuffers buffer_descriptor;
439         VAGenericID wrapper_surface;
440
441         if (drm_intel_bo_gem_export_to_prime(obj_surface->bo, &fd_handle) != 0)
442             return VA_STATUS_ERROR_OPERATION_FAILED;
443
444         obj_surface->exported_primefd = fd_handle;
445
446         memset(&attrib_list, 0, sizeof(attrib_list));
447         memset(&buffer_descriptor, 0, sizeof(buffer_descriptor));
448
449         attrib_list[0].type = VASurfaceAttribExternalBufferDescriptor;
450         attrib_list[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
451         attrib_list[0].value.value.p = &buffer_descriptor;
452         attrib_list[0].value.type = VAGenericValueTypePointer;
453
454         attrib_list[1].type = VASurfaceAttribMemoryType;
455         attrib_list[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
456         attrib_list[1].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
457         attrib_list[1].value.type = VAGenericValueTypeInteger;
458
459         buffer_descriptor.num_buffers = 1;
460         buffer_descriptor.num_planes = 2;
461         buffer_descriptor.width = obj_surface->orig_width;
462         buffer_descriptor.height = obj_surface->orig_height;
463         buffer_descriptor.pixel_format = obj_surface->fourcc;
464         buffer_descriptor.data_size = obj_surface->size;
465         buffer_descriptor.pitches[0] = obj_surface->width;
466         buffer_descriptor.pitches[1] = obj_surface->cb_cr_pitch;
467         buffer_descriptor.offsets[0] = 0;
468         buffer_descriptor.offsets[1] = obj_surface->width * obj_surface->height;
469         buffer_descriptor.buffers = (void *)&fd_handle;
470
471         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
472                     vaCreateSurfaces2(i965->wrapper_pdrvctx,
473                                       VA_RT_FORMAT_YUV420,
474                                       obj_surface->orig_width,
475                                       obj_surface->orig_height,
476                                       &wrapper_surface, 1,
477                                       attrib_list, 2));
478
479         if (va_status == VA_STATUS_SUCCESS) {
480             obj_surface->wrapper_surface = wrapper_surface;
481         } else {
482             /* This needs to be checked */
483             va_status = VA_STATUS_ERROR_OPERATION_FAILED;
484         }
485         return va_status;
486     }
487
488 }
489
490 VAStatus 
491 i965_QueryConfigProfiles(VADriverContextP ctx,
492                          VAProfile *profile_list,       /* out */
493                          int *num_profiles)             /* out */
494 {
495     struct i965_driver_data * const i965 = i965_driver_data(ctx);
496     int i = 0;
497
498     if (HAS_MPEG2_DECODING(i965) ||
499         HAS_MPEG2_ENCODING(i965)) {
500         profile_list[i++] = VAProfileMPEG2Simple;
501         profile_list[i++] = VAProfileMPEG2Main;
502     }
503
504     if (HAS_H264_DECODING(i965) ||
505         HAS_H264_ENCODING(i965) ||
506         HAS_LP_H264_ENCODING(i965)) {
507         profile_list[i++] = VAProfileH264ConstrainedBaseline;
508         profile_list[i++] = VAProfileH264Main;
509         profile_list[i++] = VAProfileH264High;
510     }
511     if (HAS_H264_MVC_DECODING_PROFILE(i965, VAProfileH264MultiviewHigh) ||
512         HAS_H264_MVC_ENCODING(i965))
513         profile_list[i++] = VAProfileH264MultiviewHigh;
514     if (HAS_H264_MVC_DECODING_PROFILE(i965, VAProfileH264StereoHigh) ||
515         HAS_H264_MVC_ENCODING(i965))
516         profile_list[i++] = VAProfileH264StereoHigh;
517
518     if (HAS_VC1_DECODING(i965)) {
519         profile_list[i++] = VAProfileVC1Simple;
520         profile_list[i++] = VAProfileVC1Main;
521         profile_list[i++] = VAProfileVC1Advanced;
522     }
523
524     if (HAS_VPP(i965)) {
525         profile_list[i++] = VAProfileNone;
526     }
527
528     if (HAS_JPEG_DECODING(i965) ||
529         HAS_JPEG_ENCODING(i965)) {
530         profile_list[i++] = VAProfileJPEGBaseline;
531     }
532
533     if (HAS_VP8_DECODING(i965) ||
534         HAS_VP8_ENCODING(i965)) {
535         profile_list[i++] = VAProfileVP8Version0_3;
536     }
537
538     if (HAS_HEVC_DECODING(i965)||
539         HAS_HEVC_ENCODING(i965)) {
540         profile_list[i++] = VAProfileHEVCMain;
541     }
542
543     if (HAS_HEVC10_DECODING(i965)||
544         HAS_HEVC10_ENCODING(i965)) {
545         profile_list[i++] = VAProfileHEVCMain10;
546     }
547
548     if(HAS_VP9_DECODING_PROFILE(i965, VAProfileVP9Profile0) ||
549         HAS_VP9_ENCODING(i965)) {
550         profile_list[i++] = VAProfileVP9Profile0;
551     }
552
553     if(HAS_VP9_DECODING_PROFILE(i965, VAProfileVP9Profile2)) {
554         profile_list[i++] = VAProfileVP9Profile2;
555     }
556
557     if (i965->wrapper_pdrvctx) {
558         VAProfile wrapper_list[4];
559         int wrapper_num;
560         VADriverContextP pdrvctx;
561         VAStatus va_status;
562
563         pdrvctx = i965->wrapper_pdrvctx;
564         CALL_VTABLE(pdrvctx, va_status,
565                     vaQueryConfigProfiles(pdrvctx,
566                                           wrapper_list, &wrapper_num));
567
568         if (va_status == VA_STATUS_SUCCESS) {
569             int j;
570             for (j = 0; j < wrapper_num; j++)
571                 if (wrapper_list[j] != VAProfileNone)
572                     profile_list[i++] = wrapper_list[j];
573         }
574     }
575
576     /* If the assert fails then I965_MAX_PROFILES needs to be bigger */
577     ASSERT_RET(i <= I965_MAX_PROFILES, VA_STATUS_ERROR_OPERATION_FAILED);
578     *num_profiles = i;
579
580     return VA_STATUS_SUCCESS;
581 }
582
583 VAStatus 
584 i965_QueryConfigEntrypoints(VADriverContextP ctx,
585                             VAProfile profile,
586                             VAEntrypoint *entrypoint_list,      /* out */
587                             int *num_entrypoints)               /* out */
588 {
589     struct i965_driver_data * const i965 = i965_driver_data(ctx);
590     int n = 0;
591
592     switch (profile) {
593     case VAProfileMPEG2Simple:
594     case VAProfileMPEG2Main:
595         if (HAS_MPEG2_DECODING(i965))
596             entrypoint_list[n++] = VAEntrypointVLD;
597
598         if (HAS_MPEG2_ENCODING(i965))
599             entrypoint_list[n++] = VAEntrypointEncSlice;
600
601         break;
602
603     case VAProfileH264ConstrainedBaseline:
604     case VAProfileH264Main:
605     case VAProfileH264High:
606         if (HAS_H264_DECODING(i965))
607             entrypoint_list[n++] = VAEntrypointVLD;
608
609         if (HAS_H264_ENCODING(i965))
610             entrypoint_list[n++] = VAEntrypointEncSlice;
611
612         if (HAS_LP_H264_ENCODING(i965))
613             entrypoint_list[n++] = VAEntrypointEncSliceLP;
614
615         break;
616    case VAProfileH264MultiviewHigh:
617    case VAProfileH264StereoHigh:
618        if (HAS_H264_MVC_DECODING_PROFILE(i965, profile))
619             entrypoint_list[n++] = VAEntrypointVLD;
620
621        if (HAS_H264_MVC_ENCODING(i965))
622             entrypoint_list[n++] = VAEntrypointEncSlice;
623         break;
624
625     case VAProfileVC1Simple:
626     case VAProfileVC1Main:
627     case VAProfileVC1Advanced:
628         if (HAS_VC1_DECODING(i965))
629             entrypoint_list[n++] = VAEntrypointVLD;
630         break;
631
632     case VAProfileNone:
633         if (HAS_VPP(i965))
634             entrypoint_list[n++] = VAEntrypointVideoProc;
635         break;
636
637     case VAProfileJPEGBaseline:
638         if (HAS_JPEG_DECODING(i965))
639             entrypoint_list[n++] = VAEntrypointVLD;
640         
641         if (HAS_JPEG_ENCODING(i965))
642             entrypoint_list[n++] = VAEntrypointEncPicture;
643         break;
644
645     case VAProfileVP8Version0_3:
646         if (HAS_VP8_DECODING(i965))
647             entrypoint_list[n++] = VAEntrypointVLD;
648         
649         if (HAS_VP8_ENCODING(i965))
650             entrypoint_list[n++] = VAEntrypointEncSlice;
651
652         break;
653
654     case VAProfileHEVCMain:
655         if (HAS_HEVC_DECODING(i965))
656             entrypoint_list[n++] = VAEntrypointVLD;
657
658         if (HAS_HEVC_ENCODING(i965))
659             entrypoint_list[n++] = VAEntrypointEncSlice;
660
661         break;
662
663     case VAProfileHEVCMain10:
664         if (HAS_HEVC10_DECODING(i965))
665             entrypoint_list[n++] = VAEntrypointVLD;
666
667         if (HAS_HEVC10_ENCODING(i965))
668             entrypoint_list[n++] = VAEntrypointEncSlice;
669
670         break;
671
672     case VAProfileVP9Profile0:
673     case VAProfileVP9Profile2:
674         if(HAS_VP9_DECODING_PROFILE(i965, profile))
675             entrypoint_list[n++] = VAEntrypointVLD;
676
677         if (HAS_VP9_ENCODING(i965) && (profile == VAProfileVP9Profile0))
678             entrypoint_list[n++] = VAEntrypointEncSlice;
679
680         if(profile == VAProfileVP9Profile0) {
681           if (i965->wrapper_pdrvctx) {
682               VAStatus va_status = VA_STATUS_SUCCESS;
683               VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
684
685               CALL_VTABLE(pdrvctx, va_status,
686                           vaQueryConfigEntrypoints(pdrvctx, profile,
687                                                    entrypoint_list,
688                                                    num_entrypoints));
689               return va_status;
690           }
691         }
692
693         break;
694
695     default:
696         break;
697     }
698
699     /* If the assert fails then I965_MAX_ENTRYPOINTS needs to be bigger */
700     ASSERT_RET(n <= I965_MAX_ENTRYPOINTS, VA_STATUS_ERROR_OPERATION_FAILED);
701     *num_entrypoints = n;
702     return n > 0 ? VA_STATUS_SUCCESS : VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
703 }
704
705 static VAStatus
706 i965_validate_config(VADriverContextP ctx, VAProfile profile,
707     VAEntrypoint entrypoint)
708 {
709     struct i965_driver_data * const i965 = i965_driver_data(ctx);
710     VAStatus va_status;
711
712     /* Validate profile & entrypoint */
713     switch (profile) {
714     case VAProfileMPEG2Simple:
715     case VAProfileMPEG2Main:
716         if ((HAS_MPEG2_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
717             (HAS_MPEG2_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)) {
718             va_status = VA_STATUS_SUCCESS;
719         } else if (!HAS_MPEG2_DECODING(i965) && !HAS_MPEG2_ENCODING(i965)){
720             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
721         } else {
722             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
723         }
724         break;
725
726     case VAProfileH264ConstrainedBaseline:
727     case VAProfileH264Main:
728     case VAProfileH264High:
729         if ((HAS_H264_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
730             (HAS_H264_ENCODING(i965) && entrypoint == VAEntrypointEncSlice) ||
731             (HAS_LP_H264_ENCODING(i965) && entrypoint == VAEntrypointEncSliceLP)) {
732             va_status = VA_STATUS_SUCCESS;
733         } else if (!HAS_H264_DECODING(i965) && !HAS_H264_ENCODING(i965) &&
734                    !HAS_LP_H264_ENCODING(i965)){
735             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
736         } else {
737             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
738         }
739         break;
740
741     case VAProfileVC1Simple:
742     case VAProfileVC1Main:
743     case VAProfileVC1Advanced:
744         if (HAS_VC1_DECODING(i965) && entrypoint == VAEntrypointVLD) {
745             va_status = VA_STATUS_SUCCESS;
746         } else if (!HAS_VC1_DECODING(i965)) {
747             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
748         } else {
749             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
750         }
751         break;
752
753     case VAProfileNone:
754         if (HAS_VPP(i965) && VAEntrypointVideoProc == entrypoint) {
755             va_status = VA_STATUS_SUCCESS;
756         } else if (!HAS_VPP(i965)){
757             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
758         } else {
759             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
760         }
761         break;
762
763     case VAProfileJPEGBaseline:
764         if ((HAS_JPEG_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
765             (HAS_JPEG_ENCODING(i965) && entrypoint == VAEntrypointEncPicture)) {
766             va_status = VA_STATUS_SUCCESS;
767         } else if (!HAS_JPEG_DECODING(i965) && !HAS_JPEG_ENCODING(i965)){
768             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
769         } else {
770             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
771         }
772         break;
773
774     case VAProfileVP8Version0_3:
775         if ((HAS_VP8_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
776             (HAS_VP8_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)) {
777             va_status = VA_STATUS_SUCCESS;
778         } else if (!HAS_VP8_DECODING(i965) && !HAS_VP8_ENCODING(i965)){
779             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
780         } else {
781             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
782         }
783         break;
784
785     case VAProfileH264MultiviewHigh:
786     case VAProfileH264StereoHigh:
787         if ((HAS_H264_MVC_DECODING_PROFILE(i965, profile) &&
788              entrypoint == VAEntrypointVLD) ||
789             (HAS_H264_MVC_ENCODING(i965) &&
790              entrypoint == VAEntrypointEncSlice)) {
791             va_status = VA_STATUS_SUCCESS;
792         } else if(!HAS_H264_MVC_DECODING_PROFILE(i965, profile) &&
793                   !HAS_H264_MVC_ENCODING(i965)) {
794             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
795         } else {
796             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
797         }
798
799         break;
800
801     case VAProfileHEVCMain:
802         if ((HAS_HEVC_DECODING(i965) && (entrypoint == VAEntrypointVLD))||
803             (HAS_HEVC_ENCODING(i965) && (entrypoint == VAEntrypointEncSlice))) {
804             va_status = VA_STATUS_SUCCESS;
805         } else if (!HAS_HEVC_DECODING(i965) && !HAS_HEVC_ENCODING(i965)) {
806             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
807         } else {
808             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
809         }
810
811         break;
812
813     case VAProfileHEVCMain10:
814         if ((HAS_HEVC10_DECODING(i965) && (entrypoint == VAEntrypointVLD))||
815             (HAS_HEVC10_ENCODING(i965) &&
816              (entrypoint == VAEntrypointEncSlice))) {
817             va_status = VA_STATUS_SUCCESS;
818         } else if (!HAS_HEVC10_DECODING(i965) && !HAS_HEVC10_ENCODING(i965)) {
819             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
820         } else {
821             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
822         }
823
824         break;
825
826     case VAProfileVP9Profile0:
827     case VAProfileVP9Profile2:
828         if ((HAS_VP9_DECODING_PROFILE(i965, profile)) &&
829             (entrypoint == VAEntrypointVLD)) {
830             va_status = VA_STATUS_SUCCESS;
831         } else if ((HAS_VP9_ENCODING_PROFILE(i965, profile)) &&
832                    (entrypoint == VAEntrypointEncSlice)) {
833             va_status = VA_STATUS_SUCCESS;
834         } else if (profile == VAProfileVP9Profile0 &&
835                    entrypoint == VAEntrypointVLD &&
836                    i965->wrapper_pdrvctx) {
837             va_status = VA_STATUS_SUCCESS;
838         } else if(!HAS_VP9_DECODING_PROFILE(i965, profile) &&
839                   !HAS_VP9_ENCODING(i965) && !i965->wrapper_pdrvctx) {
840             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
841         } else {
842             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
843         }
844
845         break;
846
847     default:
848         va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
849         break;
850     }
851     return va_status;
852 }
853
854 static uint32_t
855 i965_get_default_chroma_formats(VADriverContextP ctx, VAProfile profile,
856     VAEntrypoint entrypoint)
857 {
858     struct i965_driver_data * const i965 = i965_driver_data(ctx);
859     uint32_t chroma_formats = VA_RT_FORMAT_YUV420;
860
861     switch (profile) {
862     case VAProfileH264ConstrainedBaseline:
863     case VAProfileH264Main:
864     case VAProfileH264High:
865         if (HAS_H264_DECODING(i965) && entrypoint == VAEntrypointVLD)
866             chroma_formats |= i965->codec_info->h264_dec_chroma_formats;
867         break;
868
869     case VAProfileH264MultiviewHigh:
870     case VAProfileH264StereoHigh:
871         if (HAS_H264_MVC_DECODING(i965) && entrypoint == VAEntrypointVLD)
872             chroma_formats |= i965->codec_info->h264_dec_chroma_formats;
873         break;
874
875     case VAProfileJPEGBaseline:
876         if (HAS_JPEG_DECODING(i965) && entrypoint == VAEntrypointVLD)
877             chroma_formats |= i965->codec_info->jpeg_dec_chroma_formats;
878         if (HAS_JPEG_ENCODING(i965) && entrypoint == VAEntrypointEncPicture)
879             chroma_formats |= i965->codec_info->jpeg_enc_chroma_formats;
880         break;
881
882     case VAProfileHEVCMain10:
883         if (HAS_HEVC10_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)
884             chroma_formats = VA_RT_FORMAT_YUV420_10BPP;
885         if (HAS_HEVC10_DECODING(i965) && entrypoint == VAEntrypointVLD)
886             chroma_formats |= i965->codec_info->hevc_dec_chroma_formats;
887         break;
888
889     case VAProfileNone:
890         if (HAS_VPP_P010(i965))
891             chroma_formats |= VA_RT_FORMAT_YUV420_10BPP;
892
893         if (HAS_VPP(i965))
894             chroma_formats |= VA_RT_FORMAT_YUV422 | VA_RT_FORMAT_RGB32;
895         break;
896
897     case VAProfileVP9Profile0:
898     case VAProfileVP9Profile2:
899         if (HAS_VP9_DECODING_PROFILE(i965, profile) && entrypoint == VAEntrypointVLD)
900             chroma_formats |= i965->codec_info->vp9_dec_chroma_formats;
901         break;
902
903     default:
904         break;
905     }
906     return chroma_formats;
907 }
908
909 VAStatus 
910 i965_GetConfigAttributes(VADriverContextP ctx,
911                          VAProfile profile,
912                          VAEntrypoint entrypoint,
913                          VAConfigAttrib *attrib_list,  /* in/out */
914                          int num_attribs)
915 {
916     struct i965_driver_data * const i965 = i965_driver_data(ctx);
917     VAStatus va_status;
918     int i;
919
920     va_status = i965_validate_config(ctx, profile, entrypoint);
921     if (va_status != VA_STATUS_SUCCESS)
922         return va_status;
923
924     /* Other attributes don't seem to be defined */
925     /* What to do if we don't know the attribute? */
926     for (i = 0; i < num_attribs; i++) {
927         attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
928         switch (attrib_list[i].type) {
929         case VAConfigAttribRTFormat:
930             attrib_list[i].value = i965_get_default_chroma_formats(ctx,
931                 profile, entrypoint);
932             break;
933
934         case VAConfigAttribRateControl:
935             if (entrypoint == VAEntrypointEncSlice) {
936                 attrib_list[i].value = VA_RC_CQP;
937
938                 if (profile != VAProfileMPEG2Main &&
939                     profile != VAProfileMPEG2Simple)
940                     attrib_list[i].value |= VA_RC_CBR;
941
942                 if (profile == VAProfileVP9Profile0 ||
943                     profile == VAProfileH264ConstrainedBaseline ||
944                     profile == VAProfileH264Main ||
945                     profile == VAProfileH264High)
946                     attrib_list[i].value |= VA_RC_VBR;
947
948                 break;
949             } else if (entrypoint == VAEntrypointEncSliceLP) {
950                 struct i965_driver_data * const i965 = i965_driver_data(ctx);
951
952                 /* Support low power encoding for H.264 only by now */
953                 if (profile == VAProfileH264ConstrainedBaseline ||
954                     profile == VAProfileH264Main ||
955                     profile == VAProfileH264High)
956                     attrib_list[i].value = i965->codec_info->lp_h264_brc_mode;
957                 else
958                     attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
959             } else
960                 attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
961
962             break;
963
964         case VAConfigAttribEncPackedHeaders:
965             if (entrypoint == VAEntrypointEncSlice ||
966                 entrypoint == VAEntrypointEncSliceLP) {
967                 attrib_list[i].value = VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE | VA_ENC_PACKED_HEADER_MISC;
968                 if (profile == VAProfileH264ConstrainedBaseline ||
969                     profile == VAProfileH264Main ||
970                     profile == VAProfileH264High ||
971                     profile == VAProfileH264StereoHigh ||
972                     profile == VAProfileH264MultiviewHigh ||
973                     profile == VAProfileHEVCMain ||
974                     profile == VAProfileHEVCMain10) {
975                     attrib_list[i].value |= (VA_ENC_PACKED_HEADER_RAW_DATA |
976                                              VA_ENC_PACKED_HEADER_SLICE);
977                 }
978                 else if (profile == VAProfileVP9Profile0)
979                     attrib_list[i].value = VA_ENC_PACKED_HEADER_RAW_DATA;
980                 break;
981             }
982             else if (entrypoint == VAEntrypointEncPicture) {
983                 if (profile == VAProfileJPEGBaseline)
984                     attrib_list[i].value = VA_ENC_PACKED_HEADER_RAW_DATA;
985             }
986             break;
987
988         case VAConfigAttribEncMaxRefFrames:
989             if (entrypoint == VAEntrypointEncSlice)
990                 attrib_list[i].value = (1 << 16) | (1 << 0);
991             else if (entrypoint == VAEntrypointEncSliceLP) {
992                 /* Don't support B frame for low power mode */
993                 if (profile == VAProfileH264ConstrainedBaseline ||
994                     profile == VAProfileH264Main ||
995                     profile == VAProfileH264High)
996                     attrib_list[i].value = (1 << 0);
997                 else
998                     attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
999             }
1000
1001             break;
1002
1003         case VAConfigAttribEncQualityRange:
1004             if (entrypoint == VAEntrypointEncSlice ||
1005                 entrypoint == VAEntrypointEncSliceLP) {
1006                 attrib_list[i].value = 1;
1007                 if (profile == VAProfileH264ConstrainedBaseline ||
1008                     profile == VAProfileH264Main ||
1009                     profile == VAProfileH264High ){
1010                         attrib_list[i].value = ENCODER_QUALITY_RANGE;
1011                         if(entrypoint == VAEntrypointEncSlice){
1012                             if (IS_GEN9(i965->intel.device_info))
1013                                 attrib_list[i].value = ENCODER_QUALITY_RANGE_AVC;
1014                         }
1015                 }
1016                 break;
1017             }
1018             break;
1019     
1020         case VAConfigAttribEncJPEG:
1021             if( entrypoint == VAEntrypointEncPicture) {
1022                 VAConfigAttribValEncJPEG *configVal = (VAConfigAttribValEncJPEG*)&(attrib_list[i].value);
1023                 (configVal->bits).arithmatic_coding_mode = 0; // Huffman coding is used
1024                 (configVal->bits).progressive_dct_mode = 0;   // Only Sequential DCT is supported
1025                 (configVal->bits).non_interleaved_mode = 1;   // Support both interleaved and non-interleaved
1026                 (configVal->bits).differential_mode = 0;      // Baseline DCT is non-differential 
1027                 (configVal->bits).max_num_components = 3;     // Only 3 components supported
1028                 (configVal->bits).max_num_scans = 1;          // Only 1 scan per frame
1029                 (configVal->bits).max_num_huffman_tables = 3; // Max 3 huffman tables
1030                 (configVal->bits).max_num_quantization_tables = 3; // Max 3 quantization tables
1031             }
1032             break;
1033
1034         case VAConfigAttribDecSliceMode:
1035             attrib_list[i].value = VA_DEC_SLICE_MODE_NORMAL;
1036             break;
1037
1038         case VAConfigAttribEncROI:
1039             if (entrypoint == VAEntrypointEncSlice ||
1040                 entrypoint == VAEntrypointEncSliceLP) {
1041
1042                 if (profile == VAProfileH264ConstrainedBaseline ||
1043                     profile == VAProfileH264Main ||
1044                     profile == VAProfileH264High) {
1045
1046                     if (IS_GEN9(i965->intel.device_info)&&
1047                         entrypoint == VAEntrypointEncSlice)
1048                         attrib_list[i].value = 0;
1049                     else {
1050
1051                         VAConfigAttribValEncROI *roi_config =
1052                             (VAConfigAttribValEncROI *)&(attrib_list[i].value);
1053
1054                         if(entrypoint == VAEntrypointEncSliceLP) {
1055                             roi_config->bits.num_roi_regions = 3;
1056                             roi_config->bits.roi_rc_priority_support = 0;
1057                             roi_config->bits.roi_rc_qp_delat_support = 0;
1058                         } else {
1059                             roi_config->bits.num_roi_regions =
1060                                 I965_MAX_NUM_ROI_REGIONS;
1061                             roi_config->bits.roi_rc_priority_support = 0;
1062                             roi_config->bits.roi_rc_qp_delat_support = 1;
1063                         }
1064                     }
1065                 }else {
1066                     attrib_list[i].value = 0;
1067                 }
1068             }
1069
1070             break;
1071
1072         case VAConfigAttribEncRateControlExt:
1073             if ((profile == VAProfileH264ConstrainedBaseline ||
1074                  profile == VAProfileH264Main ||
1075                  profile == VAProfileH264High) &&
1076                 entrypoint == VAEntrypointEncSlice) {
1077                     if (IS_GEN9(i965->intel.device_info))
1078                         attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1079                     else {
1080                         VAConfigAttribValEncRateControlExt *val_config = (VAConfigAttribValEncRateControlExt *)&(attrib_list[i].value);
1081
1082                         val_config->bits.max_num_temporal_layers_minus1 = MAX_TEMPORAL_LAYERS - 1;
1083                         val_config->bits.temporal_layer_bitrate_control_flag = 1;
1084                     }
1085             } else {
1086                 attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1087             }
1088
1089             break;
1090
1091         case VAConfigAttribEncMaxSlices:
1092             if (entrypoint == VAEntrypointEncSlice) {
1093                 if ((profile == VAProfileH264ConstrainedBaseline ||
1094                     profile == VAProfileH264Main ||
1095                     profile == VAProfileH264High) ||
1096                     profile == VAProfileH264StereoHigh ||
1097                     profile == VAProfileH264MultiviewHigh) {
1098                         if (IS_GEN9(i965->intel.device_info))
1099                             attrib_list[i].value = 1;
1100                         else
1101                             attrib_list[i].value = I965_MAX_NUM_SLICE;
1102                 }else if (profile == VAProfileHEVCMain ||
1103                     profile == VAProfileHEVCMain10) {
1104                         attrib_list[i].value = I965_MAX_NUM_SLICE;
1105                 }
1106             } else if (entrypoint == VAEntrypointEncSliceLP) {
1107                 if ((profile == VAProfileH264ConstrainedBaseline ||
1108                     profile == VAProfileH264Main ||
1109                     profile == VAProfileH264High) ||
1110                     profile == VAProfileH264StereoHigh ||
1111                     profile == VAProfileH264MultiviewHigh)
1112                     attrib_list[i].value = I965_MAX_NUM_SLICE;
1113             } else
1114                 attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1115
1116             break;
1117
1118         default:
1119             /* Do nothing */
1120             attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1121             break;
1122         }
1123     }
1124
1125     return VA_STATUS_SUCCESS;
1126 }
1127
1128 static void 
1129 i965_destroy_config(struct object_heap *heap, struct object_base *obj)
1130 {
1131     object_heap_free(heap, obj);
1132 }
1133
1134 static VAConfigAttrib *
1135 i965_lookup_config_attribute(struct object_config *obj_config,
1136     VAConfigAttribType type)
1137 {
1138     int i;
1139
1140     for (i = 0; i < obj_config->num_attribs; i++) {
1141         VAConfigAttrib * const attrib = &obj_config->attrib_list[i];
1142         if (attrib->type == type)
1143             return attrib;
1144     }
1145     return NULL;
1146 }
1147
1148 static VAStatus
1149 i965_append_config_attribute(struct object_config *obj_config,
1150     const VAConfigAttrib *new_attrib)
1151 {
1152     VAConfigAttrib *attrib;
1153
1154     if (obj_config->num_attribs >= I965_MAX_CONFIG_ATTRIBUTES)
1155         return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
1156
1157     attrib = &obj_config->attrib_list[obj_config->num_attribs++];
1158     attrib->type = new_attrib->type;
1159     attrib->value = new_attrib->value;
1160     return VA_STATUS_SUCCESS;
1161 }
1162
1163 static VAStatus
1164 i965_ensure_config_attribute(struct object_config *obj_config,
1165     const VAConfigAttrib *new_attrib)
1166 {
1167     VAConfigAttrib *attrib;
1168
1169     /* Check for existing attributes */
1170     attrib = i965_lookup_config_attribute(obj_config, new_attrib->type);
1171     if (attrib) {
1172         /* Update existing attribute */
1173         attrib->value = new_attrib->value;
1174         return VA_STATUS_SUCCESS;
1175     }
1176     return i965_append_config_attribute(obj_config, new_attrib);
1177 }
1178
1179 VAStatus 
1180 i965_CreateConfig(VADriverContextP ctx,
1181                   VAProfile profile,
1182                   VAEntrypoint entrypoint,
1183                   VAConfigAttrib *attrib_list,
1184                   int num_attribs,
1185                   VAConfigID *config_id)        /* out */
1186 {
1187     struct i965_driver_data * const i965 = i965_driver_data(ctx);
1188     struct object_config *obj_config;
1189     int configID;
1190     int i;
1191     VAStatus vaStatus;
1192
1193     vaStatus = i965_validate_config(ctx, profile, entrypoint);
1194
1195     if (VA_STATUS_SUCCESS != vaStatus) {
1196         return vaStatus;
1197     }
1198
1199     configID = NEW_CONFIG_ID();
1200     obj_config = CONFIG(configID);
1201
1202     if (NULL == obj_config) {
1203         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1204         return vaStatus;
1205     }
1206
1207     obj_config->profile = profile;
1208     obj_config->entrypoint = entrypoint;
1209     obj_config->num_attribs = 0;
1210     obj_config->wrapper_config = VA_INVALID_ID;
1211
1212     for (i = 0; i < num_attribs; i++) {
1213         // add it later and ignore the user input for VAConfigAttribEncMaxSlices
1214         if(attrib_list[i].type == VAConfigAttribEncMaxSlices)
1215             continue;
1216         vaStatus = i965_ensure_config_attribute(obj_config, &attrib_list[i]);
1217         if (vaStatus != VA_STATUS_SUCCESS)
1218             break;
1219     }
1220
1221     if (vaStatus == VA_STATUS_SUCCESS) {
1222         VAConfigAttrib attrib, *attrib_found;
1223         attrib.type = VAConfigAttribRTFormat;
1224         attrib.value = i965_get_default_chroma_formats(ctx, profile, entrypoint);
1225         attrib_found = i965_lookup_config_attribute(obj_config, attrib.type);
1226         if (!attrib_found || !attrib_found->value)
1227             vaStatus = i965_append_config_attribute(obj_config, &attrib);
1228         else if (!(attrib_found->value & attrib.value))
1229             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
1230     }
1231
1232     if (vaStatus == VA_STATUS_SUCCESS) {
1233         VAConfigAttrib attrib;
1234         attrib.type = VAConfigAttribEncMaxSlices;
1235         attrib.value = VA_ATTRIB_NOT_SUPPORTED;
1236         if (entrypoint == VAEntrypointEncSlice) {
1237             if ((profile == VAProfileH264ConstrainedBaseline ||
1238                 profile == VAProfileH264Main ||
1239                 profile == VAProfileH264High) ||
1240                 profile == VAProfileH264StereoHigh ||
1241                 profile == VAProfileH264MultiviewHigh) {
1242                     if (IS_GEN9(i965->intel.device_info))
1243                         attrib.value = 1;
1244                     else
1245                         attrib.value = I965_MAX_NUM_SLICE;
1246             }else if (profile == VAProfileHEVCMain ||
1247                 profile == VAProfileHEVCMain10) {
1248                 attrib.value = I965_MAX_NUM_SLICE;
1249             }
1250         } else if (entrypoint == VAEntrypointEncSliceLP) {
1251             if ((profile == VAProfileH264ConstrainedBaseline ||
1252                 profile == VAProfileH264Main ||
1253                 profile == VAProfileH264High) ||
1254                 profile == VAProfileH264StereoHigh ||
1255                 profile == VAProfileH264MultiviewHigh)
1256                 attrib.value = I965_MAX_NUM_SLICE;
1257         } else
1258             attrib.value = VA_ATTRIB_NOT_SUPPORTED;
1259
1260         if(attrib.value != VA_ATTRIB_NOT_SUPPORTED)
1261             vaStatus = i965_append_config_attribute(obj_config, &attrib);
1262     }
1263
1264     if ((vaStatus == VA_STATUS_SUCCESS) &&
1265         (profile == VAProfileVP9Profile0) &&
1266         (entrypoint == VAEntrypointVLD) &&
1267         !HAS_VP9_DECODING(i965)) {
1268
1269         if (i965->wrapper_pdrvctx) {
1270             VAGenericID wrapper_config;
1271
1272             CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
1273                         vaCreateConfig(i965->wrapper_pdrvctx, profile,
1274                                        entrypoint, attrib_list,
1275                                        num_attribs, &wrapper_config));
1276
1277             if (vaStatus == VA_STATUS_SUCCESS)
1278                 obj_config->wrapper_config = wrapper_config;
1279         }
1280     }
1281
1282     /* Error recovery */
1283     if (VA_STATUS_SUCCESS != vaStatus) {
1284         i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
1285     } else {
1286         *config_id = configID;
1287     }
1288
1289     return vaStatus;
1290 }
1291
1292 VAStatus 
1293 i965_DestroyConfig(VADriverContextP ctx, VAConfigID config_id)
1294 {
1295     struct i965_driver_data *i965 = i965_driver_data(ctx);
1296     struct object_config *obj_config = CONFIG(config_id);
1297     VAStatus vaStatus;
1298
1299     if (NULL == obj_config) {
1300         vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
1301         return vaStatus;
1302     }
1303
1304     if ((obj_config->wrapper_config != VA_INVALID_ID) &&
1305         i965->wrapper_pdrvctx) {
1306         CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
1307                     vaDestroyConfig(i965->wrapper_pdrvctx,
1308                                     obj_config->wrapper_config));
1309         obj_config->wrapper_config = VA_INVALID_ID;
1310     }
1311
1312     i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
1313     return VA_STATUS_SUCCESS;
1314 }
1315
1316 VAStatus i965_QueryConfigAttributes(VADriverContextP ctx,
1317                                     VAConfigID config_id,
1318                                     VAProfile *profile,                 /* out */
1319                                     VAEntrypoint *entrypoint,           /* out */
1320                                     VAConfigAttrib *attrib_list,        /* out */
1321                                     int *num_attribs)                   /* out */
1322 {
1323     struct i965_driver_data *i965 = i965_driver_data(ctx);
1324     struct object_config *obj_config = CONFIG(config_id);
1325     VAStatus vaStatus = VA_STATUS_SUCCESS;
1326     int i;
1327
1328     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
1329     *profile = obj_config->profile;
1330     *entrypoint = obj_config->entrypoint;
1331     *num_attribs = obj_config->num_attribs;
1332
1333     for(i = 0; i < obj_config->num_attribs; i++) {
1334         attrib_list[i] = obj_config->attrib_list[i];
1335     }
1336
1337     return vaStatus;
1338 }
1339
1340 void
1341 i965_destroy_surface_storage(struct object_surface *obj_surface)
1342 {
1343     if (!obj_surface)
1344         return;
1345
1346     dri_bo_unreference(obj_surface->bo);
1347     obj_surface->bo = NULL;
1348
1349     if (obj_surface->free_private_data != NULL) {
1350         obj_surface->free_private_data(&obj_surface->private_data);
1351         obj_surface->private_data = NULL;
1352     }
1353 }
1354
1355 static void 
1356 i965_destroy_surface(struct object_heap *heap, struct object_base *obj)
1357 {
1358     struct object_surface *obj_surface = (struct object_surface *)obj;
1359
1360     i965_destroy_surface_storage(obj_surface);
1361     object_heap_free(heap, obj);
1362 }
1363
1364 static VAStatus
1365 i965_surface_native_memory(VADriverContextP ctx,
1366                            struct object_surface *obj_surface,
1367                            int format,
1368                            int expected_fourcc)
1369 {
1370     struct i965_driver_data *i965 = i965_driver_data(ctx);
1371     int tiling = HAS_TILED_SURFACE(i965);
1372
1373     if (!expected_fourcc)
1374         return VA_STATUS_SUCCESS;
1375
1376     // todo, should we disable tiling for 422 format?
1377     if (expected_fourcc == VA_FOURCC_I420 ||
1378         expected_fourcc == VA_FOURCC_IYUV ||
1379         expected_fourcc == VA_FOURCC_I010 ||
1380         expected_fourcc == VA_FOURCC_YV12 ||
1381         expected_fourcc == VA_FOURCC_YV16)
1382         tiling = 0;
1383
1384     return i965_check_alloc_surface_bo(ctx, obj_surface, tiling, expected_fourcc, get_sampling_from_fourcc(expected_fourcc));
1385 }
1386     
1387 static VAStatus
1388 i965_suface_external_memory(VADriverContextP ctx,
1389                             struct object_surface *obj_surface,
1390                             int external_memory_type,
1391                             VASurfaceAttribExternalBuffers *memory_attibute,
1392                             int index)
1393 {
1394     struct i965_driver_data *i965 = i965_driver_data(ctx);
1395
1396     if (!memory_attibute ||
1397         !memory_attibute->buffers ||
1398         index > memory_attibute->num_buffers)
1399         return VA_STATUS_ERROR_INVALID_PARAMETER;
1400
1401     ASSERT_RET(obj_surface->orig_width == memory_attibute->width, VA_STATUS_ERROR_INVALID_PARAMETER);
1402     ASSERT_RET(obj_surface->orig_height == memory_attibute->height, VA_STATUS_ERROR_INVALID_PARAMETER);
1403     ASSERT_RET(memory_attibute->num_planes >= 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1404
1405     obj_surface->fourcc = memory_attibute->pixel_format;
1406     obj_surface->width = memory_attibute->pitches[0];
1407     obj_surface->size = memory_attibute->data_size;
1408
1409     if (memory_attibute->num_planes == 1)
1410         obj_surface->height = memory_attibute->data_size / obj_surface->width;
1411     else 
1412         obj_surface->height = memory_attibute->offsets[1] / obj_surface->width;
1413
1414     obj_surface->x_cb_offset = 0; /* X offset is always 0 */
1415     obj_surface->x_cr_offset = 0;
1416
1417     switch (obj_surface->fourcc) {
1418     case VA_FOURCC_NV12:
1419     case VA_FOURCC_P010:
1420         ASSERT_RET(memory_attibute->num_planes == 2, VA_STATUS_ERROR_INVALID_PARAMETER);
1421         ASSERT_RET(memory_attibute->pitches[0] == memory_attibute->pitches[1], VA_STATUS_ERROR_INVALID_PARAMETER);
1422
1423         obj_surface->subsampling = SUBSAMPLE_YUV420;
1424         obj_surface->y_cb_offset = obj_surface->height;
1425         obj_surface->y_cr_offset = obj_surface->height;
1426         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1427         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1428         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1429
1430         break;
1431
1432     case VA_FOURCC_YV12:
1433     case VA_FOURCC_IMC1:
1434         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1435         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1436
1437         obj_surface->subsampling = SUBSAMPLE_YUV420;
1438         obj_surface->y_cr_offset = obj_surface->height;
1439         obj_surface->y_cb_offset = memory_attibute->offsets[2] / obj_surface->width;
1440         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1441         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1442         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1443         
1444         break;
1445
1446     case VA_FOURCC_I420:
1447     case VA_FOURCC_IYUV:
1448     case VA_FOURCC_IMC3:
1449     case VA_FOURCC_I010:
1450         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1451         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1452
1453         obj_surface->subsampling = SUBSAMPLE_YUV420;
1454         obj_surface->y_cb_offset = obj_surface->height;
1455         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1456         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1457         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1458         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1459
1460         break;
1461
1462     case VA_FOURCC_YUY2:
1463     case VA_FOURCC_UYVY:
1464         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1465
1466         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1467         obj_surface->y_cb_offset = 0;
1468         obj_surface->y_cr_offset = 0;
1469         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1470         obj_surface->cb_cr_height = obj_surface->orig_height;
1471         obj_surface->cb_cr_pitch = memory_attibute->pitches[0];
1472
1473         break;
1474
1475     case VA_FOURCC_RGBA:
1476     case VA_FOURCC_RGBX:
1477     case VA_FOURCC_BGRA:
1478     case VA_FOURCC_BGRX:
1479         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1480
1481         obj_surface->subsampling = SUBSAMPLE_RGBX;
1482         obj_surface->y_cb_offset = 0;
1483         obj_surface->y_cr_offset = 0;
1484         obj_surface->cb_cr_width = 0;
1485         obj_surface->cb_cr_height = 0;
1486         obj_surface->cb_cr_pitch = 0;
1487
1488         break;
1489
1490     case VA_FOURCC_Y800: /* monochrome surface */
1491         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1492         
1493         obj_surface->subsampling = SUBSAMPLE_YUV400;
1494         obj_surface->y_cb_offset = 0;
1495         obj_surface->y_cr_offset = 0;
1496         obj_surface->cb_cr_width = 0;
1497         obj_surface->cb_cr_height = 0;
1498         obj_surface->cb_cr_pitch = 0;
1499
1500         break;
1501
1502     case VA_FOURCC_411P:
1503         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1504         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1505
1506         obj_surface->subsampling = SUBSAMPLE_YUV411;
1507         obj_surface->y_cb_offset = obj_surface->height;
1508         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1509         obj_surface->cb_cr_width = obj_surface->orig_width / 4;
1510         obj_surface->cb_cr_height = obj_surface->orig_height;
1511         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1512
1513         break;
1514
1515     case VA_FOURCC_422H:
1516         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1517         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1518
1519         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1520         obj_surface->y_cb_offset = obj_surface->height;
1521         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1522         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1523         obj_surface->cb_cr_height = obj_surface->orig_height;
1524         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1525
1526         break;
1527
1528     case VA_FOURCC_YV16:
1529         assert(memory_attibute->num_planes == 3);
1530         assert(memory_attibute->pitches[1] == memory_attibute->pitches[2]);
1531
1532         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1533         obj_surface->y_cr_offset = memory_attibute->offsets[1] / obj_surface->width;
1534         obj_surface->y_cb_offset = memory_attibute->offsets[2] / obj_surface->width;
1535         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1536         obj_surface->cb_cr_height = obj_surface->orig_height;
1537         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1538
1539         break;
1540
1541     case VA_FOURCC_422V:
1542         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1543         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1544
1545         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1546         obj_surface->y_cb_offset = obj_surface->height;
1547         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1548         obj_surface->cb_cr_width = obj_surface->orig_width;
1549         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1550         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1551
1552         break;
1553
1554     case VA_FOURCC_444P:
1555         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1556         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1557
1558         obj_surface->subsampling = SUBSAMPLE_YUV444;
1559         obj_surface->y_cb_offset = obj_surface->height;
1560         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1561         obj_surface->cb_cr_width = obj_surface->orig_width;
1562         obj_surface->cb_cr_height = obj_surface->orig_height;
1563         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1564
1565         break;
1566
1567     default:
1568
1569         return VA_STATUS_ERROR_INVALID_PARAMETER;
1570     }
1571
1572     if (external_memory_type == I965_SURFACE_MEM_GEM_FLINK)
1573         obj_surface->bo = drm_intel_bo_gem_create_from_name(i965->intel.bufmgr,
1574                                                             "gem flinked vaapi surface",
1575                                                             memory_attibute->buffers[index]);
1576     else if (external_memory_type == I965_SURFACE_MEM_DRM_PRIME)
1577         obj_surface->bo = drm_intel_bo_gem_create_from_prime(i965->intel.bufmgr,
1578                                                              memory_attibute->buffers[index],
1579                                                              obj_surface->size);
1580
1581     if (!obj_surface->bo)
1582         return VA_STATUS_ERROR_INVALID_PARAMETER;
1583
1584     return VA_STATUS_SUCCESS;
1585 }
1586
1587 /* byte-per-pixel of the first plane */
1588 static int
1589 bpp_1stplane_by_fourcc(unsigned int fourcc)
1590 {
1591     const i965_fourcc_info *info = get_fourcc_info(fourcc);
1592
1593     if (info && (info->flag & I_S))
1594         return info->bpp[0] / 8;
1595     else
1596         return 0;
1597 }
1598
1599 static VAStatus
1600 i965_CreateSurfaces2(
1601     VADriverContextP    ctx,
1602     unsigned int        format,
1603     unsigned int        width,
1604     unsigned int        height,
1605     VASurfaceID        *surfaces,
1606     unsigned int        num_surfaces,
1607     VASurfaceAttrib    *attrib_list,
1608     unsigned int        num_attribs
1609     )
1610 {
1611     struct i965_driver_data *i965 = i965_driver_data(ctx);
1612     int i,j;
1613     VAStatus vaStatus = VA_STATUS_SUCCESS;
1614     int expected_fourcc = 0;
1615     int memory_type = I965_SURFACE_MEM_NATIVE; /* native */
1616     VASurfaceAttribExternalBuffers *memory_attibute = NULL;
1617
1618     for (i = 0; i < num_attribs && attrib_list; i++) {
1619         if ((attrib_list[i].type == VASurfaceAttribPixelFormat) &&
1620             (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
1621             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypeInteger, VA_STATUS_ERROR_INVALID_PARAMETER);
1622             expected_fourcc = attrib_list[i].value.value.i;
1623         }
1624
1625         if ((attrib_list[i].type == VASurfaceAttribMemoryType) &&
1626             (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
1627             
1628             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypeInteger, VA_STATUS_ERROR_INVALID_PARAMETER);
1629
1630             if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM)
1631                 memory_type = I965_SURFACE_MEM_GEM_FLINK; /* flinked GEM handle */
1632             else if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME)
1633                 memory_type = I965_SURFACE_MEM_DRM_PRIME; /* drm prime fd */
1634             else if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_VA)
1635                 memory_type = I965_SURFACE_MEM_NATIVE; /* va native memory, to be allocated */
1636         }
1637
1638         if ((attrib_list[i].type == VASurfaceAttribExternalBufferDescriptor) &&
1639             (attrib_list[i].flags == VA_SURFACE_ATTRIB_SETTABLE)) {
1640             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypePointer, VA_STATUS_ERROR_INVALID_PARAMETER);
1641             memory_attibute = (VASurfaceAttribExternalBuffers *)attrib_list[i].value.value.p;
1642         }
1643     }
1644
1645     /* support 420 & 422 & RGB32 format, 422 and RGB32 are only used
1646      * for post-processing (including color conversion) */
1647     if (VA_RT_FORMAT_YUV420 != format &&
1648         VA_RT_FORMAT_YUV420_10BPP != format &&
1649         VA_RT_FORMAT_YUV422 != format &&
1650         VA_RT_FORMAT_YUV444 != format &&
1651         VA_RT_FORMAT_YUV411 != format &&
1652         VA_RT_FORMAT_YUV400 != format &&
1653         VA_RT_FORMAT_RGB32  != format) {
1654         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
1655     }
1656
1657     for (i = 0; i < num_surfaces; i++) {
1658         int surfaceID = NEW_SURFACE_ID();
1659         struct object_surface *obj_surface = SURFACE(surfaceID);
1660
1661         if (NULL == obj_surface) {
1662             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1663             break;
1664         }
1665
1666         surfaces[i] = surfaceID;
1667         obj_surface->status = VASurfaceReady;
1668         obj_surface->orig_width = width;
1669         obj_surface->orig_height = height;
1670         obj_surface->user_disable_tiling = false;
1671         obj_surface->user_h_stride_set = false;
1672         obj_surface->user_v_stride_set = false;
1673         obj_surface->border_cleared = false;
1674
1675         obj_surface->subpic_render_idx = 0;
1676         for(j = 0; j < I965_MAX_SUBPIC_SUM; j++){
1677            obj_surface->subpic[j] = VA_INVALID_ID;
1678            obj_surface->obj_subpic[j] = NULL;
1679         }
1680
1681         assert(i965->codec_info->min_linear_wpitch);
1682         assert(i965->codec_info->min_linear_hpitch);
1683         obj_surface->width = ALIGN(width, i965->codec_info->min_linear_wpitch);
1684         obj_surface->height = ALIGN(height, i965->codec_info->min_linear_hpitch);
1685         obj_surface->flags = SURFACE_REFERENCED;
1686         obj_surface->fourcc = 0;
1687         obj_surface->expected_format = format;
1688         obj_surface->bo = NULL;
1689         obj_surface->locked_image_id = VA_INVALID_ID;
1690         obj_surface->derived_image_id = VA_INVALID_ID;
1691         obj_surface->private_data = NULL;
1692         obj_surface->free_private_data = NULL;
1693         obj_surface->subsampling = SUBSAMPLE_YUV420;
1694
1695         obj_surface->wrapper_surface = VA_INVALID_ID;
1696         obj_surface->exported_primefd = -1;
1697
1698         switch (memory_type) {
1699         case I965_SURFACE_MEM_NATIVE:
1700             if (memory_attibute) {
1701                 if (!(memory_attibute->flags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING))
1702                     obj_surface->user_disable_tiling = true;
1703
1704                 if (memory_attibute->pixel_format) {
1705                     if (expected_fourcc)
1706                         ASSERT_RET(memory_attibute->pixel_format == expected_fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
1707                     else
1708                         expected_fourcc = memory_attibute->pixel_format;
1709                 }
1710                 ASSERT_RET(expected_fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
1711                 if (memory_attibute->pitches[0]) {
1712                     int bpp_1stplane = bpp_1stplane_by_fourcc(expected_fourcc);
1713                     ASSERT_RET(bpp_1stplane, VA_STATUS_ERROR_INVALID_PARAMETER);
1714                     obj_surface->width = memory_attibute->pitches[0];
1715                     obj_surface->user_h_stride_set = true;
1716                     ASSERT_RET(IS_ALIGNED(obj_surface->width, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
1717                     ASSERT_RET(obj_surface->width >= width * bpp_1stplane, VA_STATUS_ERROR_INVALID_PARAMETER);
1718
1719                     if (memory_attibute->offsets[1]) {
1720                         ASSERT_RET(!memory_attibute->offsets[0], VA_STATUS_ERROR_INVALID_PARAMETER);
1721                         obj_surface->height = memory_attibute->offsets[1]/memory_attibute->pitches[0];
1722                         obj_surface->user_v_stride_set = true;
1723                         ASSERT_RET(IS_ALIGNED(obj_surface->height, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
1724                         ASSERT_RET(obj_surface->height >= height, VA_STATUS_ERROR_INVALID_PARAMETER);
1725                     }
1726                 }
1727             }
1728             vaStatus = i965_surface_native_memory(ctx,
1729                                                   obj_surface,
1730                                                   format,
1731                                                   expected_fourcc);
1732             break;
1733
1734         case I965_SURFACE_MEM_GEM_FLINK:
1735         case I965_SURFACE_MEM_DRM_PRIME:
1736             vaStatus = i965_suface_external_memory(ctx,
1737                                                    obj_surface,
1738                                                    memory_type,
1739                                                    memory_attibute,
1740                                                    i);
1741             break;
1742         }
1743         if (VA_STATUS_SUCCESS != vaStatus) {
1744             i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
1745             break;
1746         }
1747     }
1748
1749     /* Error recovery */
1750     if (VA_STATUS_SUCCESS != vaStatus) {
1751         /* surfaces[i-1] was the last successful allocation */
1752         for (; i--; ) {
1753             struct object_surface *obj_surface = SURFACE(surfaces[i]);
1754
1755             surfaces[i] = VA_INVALID_SURFACE;
1756             assert(obj_surface);
1757             i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
1758         }
1759     }
1760
1761     return vaStatus;
1762 }
1763
1764 VAStatus 
1765 i965_CreateSurfaces(VADriverContextP ctx,
1766                     int width,
1767                     int height,
1768                     int format,
1769                     int num_surfaces,
1770                     VASurfaceID *surfaces)      /* out */
1771 {
1772     return i965_CreateSurfaces2(ctx,
1773                                 format,
1774                                 width,
1775                                 height,
1776                                 surfaces,
1777                                 num_surfaces,
1778                                 NULL,
1779                                 0);
1780 }
1781
1782 VAStatus 
1783 i965_DestroySurfaces(VADriverContextP ctx,
1784                      VASurfaceID *surface_list,
1785                      int num_surfaces)
1786 {
1787     struct i965_driver_data *i965 = i965_driver_data(ctx);
1788     int i;
1789     VAStatus va_status = VA_STATUS_SUCCESS;
1790
1791     for (i = num_surfaces; i--; ) {
1792         struct object_surface *obj_surface = SURFACE(surface_list[i]);
1793
1794         ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
1795
1796         if ((obj_surface->wrapper_surface != VA_INVALID_ID) &&
1797             i965->wrapper_pdrvctx) {
1798             CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
1799                         vaDestroySurfaces(i965->wrapper_pdrvctx,
1800                                           &(obj_surface->wrapper_surface),
1801                                           1));
1802             obj_surface->wrapper_surface = VA_INVALID_ID;
1803         }
1804         if (obj_surface->exported_primefd >= 0) {
1805            close(obj_surface->exported_primefd);
1806            obj_surface->exported_primefd = -1;
1807         }
1808
1809         i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
1810     }
1811
1812     return va_status;
1813 }
1814
1815 VAStatus 
1816 i965_QueryImageFormats(VADriverContextP ctx,
1817                        VAImageFormat *format_list,      /* out */
1818                        int *num_formats)                /* out */
1819 {
1820     int n;
1821
1822     for (n = 0; i965_image_formats_map[n].va_format.fourcc != 0; n++) {
1823         const i965_image_format_map_t * const m = &i965_image_formats_map[n];
1824         if (format_list)
1825             format_list[n] = m->va_format;
1826     }
1827
1828     if (num_formats)
1829         *num_formats = n;
1830
1831     return VA_STATUS_SUCCESS;
1832 }
1833
1834 /*
1835  * Guess the format when the usage of a VA surface is unknown
1836  * 1. Without a valid context: YV12
1837  * 2. The current context is valid:
1838  *    a) always NV12 on GEN6 and later
1839  *    b) I420 for MPEG-2 and NV12 for other codec on GEN4 & GEN5
1840  */
1841 static void
1842 i965_guess_surface_format(VADriverContextP ctx,
1843                           VASurfaceID surface,
1844                           unsigned int *fourcc,
1845                           unsigned int *is_tiled)
1846 {
1847     struct i965_driver_data *i965 = i965_driver_data(ctx);
1848     struct object_context *obj_context = NULL;
1849     struct object_config *obj_config = NULL;
1850
1851     *fourcc = VA_FOURCC_YV12;
1852     *is_tiled = 0;
1853
1854     if (i965->current_context_id == VA_INVALID_ID)
1855         return;
1856
1857     obj_context = CONTEXT(i965->current_context_id);
1858
1859     if (!obj_context)
1860         return;
1861
1862     obj_config = obj_context->obj_config;
1863     assert(obj_config);
1864
1865     if (!obj_config)
1866         return;
1867
1868     if (IS_GEN6(i965->intel.device_info) ||
1869         IS_GEN7(i965->intel.device_info) ||
1870         IS_GEN8(i965->intel.device_info) ||
1871         IS_GEN9(i965->intel.device_info)) {
1872         *fourcc = VA_FOURCC_NV12;
1873         *is_tiled = 1;
1874         return;
1875     }
1876
1877     switch (obj_config->profile) {
1878     case VAProfileMPEG2Simple:
1879     case VAProfileMPEG2Main:
1880         *fourcc = VA_FOURCC_I420;
1881         *is_tiled = 0;
1882         break;
1883
1884     default:
1885         *fourcc = VA_FOURCC_NV12;
1886         *is_tiled = 0;
1887         break;
1888     }
1889 }
1890
1891 VAStatus 
1892 i965_QuerySubpictureFormats(VADriverContextP ctx,
1893                             VAImageFormat *format_list,         /* out */
1894                             unsigned int *flags,                /* out */
1895                             unsigned int *num_formats)          /* out */
1896 {
1897     int n;
1898
1899     for (n = 0; i965_subpic_formats_map[n].va_format.fourcc != 0; n++) {
1900         const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[n];
1901         if (format_list)
1902             format_list[n] = m->va_format;
1903         if (flags)
1904             flags[n] = m->va_flags;
1905     }
1906
1907     if (num_formats)
1908         *num_formats = n;
1909
1910     return VA_STATUS_SUCCESS;
1911 }
1912
1913 static void 
1914 i965_destroy_subpic(struct object_heap *heap, struct object_base *obj)
1915 {
1916     //    struct object_subpic *obj_subpic = (struct object_subpic *)obj;
1917
1918     object_heap_free(heap, obj);
1919 }
1920
1921 VAStatus 
1922 i965_CreateSubpicture(VADriverContextP ctx,
1923                       VAImageID image,
1924                       VASubpictureID *subpicture)         /* out */
1925 {
1926     struct i965_driver_data *i965 = i965_driver_data(ctx);
1927     VASubpictureID subpicID = NEW_SUBPIC_ID()
1928     struct object_subpic *obj_subpic = SUBPIC(subpicID);
1929
1930     if (!obj_subpic)
1931         return VA_STATUS_ERROR_ALLOCATION_FAILED;
1932
1933     struct object_image *obj_image = IMAGE(image);
1934     if (!obj_image)
1935         return VA_STATUS_ERROR_INVALID_IMAGE;
1936
1937     const i965_subpic_format_map_t * const m = get_subpic_format(&obj_image->image.format);
1938     if (!m)
1939         return VA_STATUS_ERROR_UNKNOWN; /* XXX: VA_STATUS_ERROR_UNSUPPORTED_FORMAT? */
1940
1941     *subpicture = subpicID;
1942     obj_subpic->image  = image;
1943     obj_subpic->obj_image = obj_image;
1944     obj_subpic->format = m->format;
1945     obj_subpic->width  = obj_image->image.width;
1946     obj_subpic->height = obj_image->image.height;
1947     obj_subpic->pitch  = obj_image->image.pitches[0];
1948     obj_subpic->bo     = obj_image->bo;
1949     obj_subpic->global_alpha = 1.0;
1950  
1951     return VA_STATUS_SUCCESS;
1952 }
1953
1954 VAStatus 
1955 i965_DestroySubpicture(VADriverContextP ctx,
1956                        VASubpictureID subpicture)
1957 {
1958     struct i965_driver_data *i965 = i965_driver_data(ctx);
1959     struct object_subpic *obj_subpic = SUBPIC(subpicture);
1960
1961     if (!obj_subpic)
1962         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
1963
1964     ASSERT_RET(obj_subpic->obj_image, VA_STATUS_ERROR_INVALID_SUBPICTURE);
1965     i965_destroy_subpic(&i965->subpic_heap, (struct object_base *)obj_subpic);
1966     return VA_STATUS_SUCCESS;
1967 }
1968
1969 VAStatus 
1970 i965_SetSubpictureImage(VADriverContextP ctx,
1971                         VASubpictureID subpicture,
1972                         VAImageID image)
1973 {
1974     /* TODO */
1975     return VA_STATUS_ERROR_UNIMPLEMENTED;
1976 }
1977
1978 VAStatus 
1979 i965_SetSubpictureChromakey(VADriverContextP ctx,
1980                             VASubpictureID subpicture,
1981                             unsigned int chromakey_min,
1982                             unsigned int chromakey_max,
1983                             unsigned int chromakey_mask)
1984 {
1985     /* TODO */
1986     return VA_STATUS_ERROR_UNIMPLEMENTED;
1987 }
1988
1989 VAStatus 
1990 i965_SetSubpictureGlobalAlpha(VADriverContextP ctx,
1991                               VASubpictureID subpicture,
1992                               float global_alpha)
1993 {
1994     struct i965_driver_data *i965 = i965_driver_data(ctx);
1995     struct object_subpic *obj_subpic = SUBPIC(subpicture);
1996
1997     if(global_alpha > 1.0 || global_alpha < 0.0){
1998        return VA_STATUS_ERROR_INVALID_PARAMETER;
1999     }
2000
2001     if (!obj_subpic)
2002         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2003
2004     obj_subpic->global_alpha  = global_alpha;
2005
2006     return VA_STATUS_SUCCESS;
2007 }
2008
2009 VAStatus 
2010 i965_AssociateSubpicture(VADriverContextP ctx,
2011                          VASubpictureID subpicture,
2012                          VASurfaceID *target_surfaces,
2013                          int num_surfaces,
2014                          short src_x, /* upper left offset in subpicture */
2015                          short src_y,
2016                          unsigned short src_width,
2017                          unsigned short src_height,
2018                          short dest_x, /* upper left offset in surface */
2019                          short dest_y,
2020                          unsigned short dest_width,
2021                          unsigned short dest_height,
2022                          /*
2023                           * whether to enable chroma-keying or global-alpha
2024                           * see VA_SUBPICTURE_XXX values
2025                           */
2026                          unsigned int flags)
2027 {
2028     struct i965_driver_data *i965 = i965_driver_data(ctx);
2029     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2030     int i, j;
2031
2032     if (!obj_subpic)
2033         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2034     
2035     ASSERT_RET(obj_subpic->obj_image, VA_STATUS_ERROR_INVALID_SUBPICTURE);
2036
2037     obj_subpic->src_rect.x      = src_x;
2038     obj_subpic->src_rect.y      = src_y;
2039     obj_subpic->src_rect.width  = src_width;
2040     obj_subpic->src_rect.height = src_height;
2041     obj_subpic->dst_rect.x      = dest_x;
2042     obj_subpic->dst_rect.y      = dest_y;
2043     obj_subpic->dst_rect.width  = dest_width;
2044     obj_subpic->dst_rect.height = dest_height;
2045     obj_subpic->flags           = flags;
2046
2047     for (i = 0; i < num_surfaces; i++) {
2048         struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
2049         if (!obj_surface)
2050             return VA_STATUS_ERROR_INVALID_SURFACE;
2051
2052         for(j = 0; j < I965_MAX_SUBPIC_SUM; j ++){
2053             if(obj_surface->subpic[j] == VA_INVALID_ID){
2054                 assert(obj_surface->obj_subpic[j] == NULL);
2055                 obj_surface->subpic[j] = subpicture;
2056                 obj_surface->obj_subpic[j] = obj_subpic;
2057                 break;
2058             }
2059         }
2060         
2061         if(j == I965_MAX_SUBPIC_SUM){
2062             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2063         }
2064
2065     }
2066     return VA_STATUS_SUCCESS;
2067 }
2068
2069
2070 VAStatus 
2071 i965_DeassociateSubpicture(VADriverContextP ctx,
2072                            VASubpictureID subpicture,
2073                            VASurfaceID *target_surfaces,
2074                            int num_surfaces)
2075 {
2076     struct i965_driver_data *i965 = i965_driver_data(ctx);
2077     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2078     int i, j;
2079
2080     if (!obj_subpic)
2081         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2082
2083     for (i = 0; i < num_surfaces; i++) {
2084         struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
2085         if (!obj_surface)
2086             return VA_STATUS_ERROR_INVALID_SURFACE;
2087
2088         for(j = 0; j < I965_MAX_SUBPIC_SUM; j ++){
2089             if (obj_surface->subpic[j] == subpicture) {
2090                 assert(obj_surface->obj_subpic[j] == obj_subpic);
2091                 obj_surface->subpic[j] = VA_INVALID_ID;
2092                 obj_surface->obj_subpic[j] = NULL;
2093                 break;
2094             }
2095         }
2096         
2097         if(j == I965_MAX_SUBPIC_SUM){
2098             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2099         }
2100     }
2101     return VA_STATUS_SUCCESS;
2102 }
2103
2104 void
2105 i965_reference_buffer_store(struct buffer_store **ptr, 
2106                             struct buffer_store *buffer_store)
2107 {
2108     assert(*ptr == NULL);
2109
2110     if (buffer_store) {
2111         buffer_store->ref_count++;
2112         *ptr = buffer_store;
2113     }
2114 }
2115
2116 void 
2117 i965_release_buffer_store(struct buffer_store **ptr)
2118 {
2119     struct buffer_store *buffer_store = *ptr;
2120
2121     if (buffer_store == NULL)
2122         return;
2123
2124     assert(buffer_store->bo || buffer_store->buffer);
2125     assert(!(buffer_store->bo && buffer_store->buffer));
2126     buffer_store->ref_count--;
2127     
2128     if (buffer_store->ref_count == 0) {
2129         dri_bo_unreference(buffer_store->bo);
2130         free(buffer_store->buffer);
2131         buffer_store->bo = NULL;
2132         buffer_store->buffer = NULL;
2133         free(buffer_store);
2134     }
2135
2136     *ptr = NULL;
2137 }
2138
2139 static void 
2140 i965_destroy_context(struct object_heap *heap, struct object_base *obj)
2141 {
2142     struct object_context *obj_context = (struct object_context *)obj;
2143     int i, j;
2144
2145     if (obj_context->hw_context) {
2146         obj_context->hw_context->destroy(obj_context->hw_context);
2147         obj_context->hw_context = NULL;
2148     }
2149
2150     if (obj_context->codec_type == CODEC_PROC) {
2151         i965_release_buffer_store(&obj_context->codec_state.proc.pipeline_param);
2152
2153     } else if (obj_context->codec_type == CODEC_ENC) {
2154         i965_release_buffer_store(&obj_context->codec_state.encode.q_matrix);
2155         i965_release_buffer_store(&obj_context->codec_state.encode.huffman_table);
2156
2157         assert(obj_context->codec_state.encode.num_slice_params_ext <= obj_context->codec_state.encode.max_slice_params_ext);
2158         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param_ext);
2159         i965_release_buffer_store(&obj_context->codec_state.encode.seq_param_ext);
2160
2161         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_param); i++)
2162             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_param[i]);
2163
2164         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_data); i++)
2165             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data[i]);
2166
2167         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param); i++)
2168             for (j = 0; j < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param[0]); j++)
2169                 i965_release_buffer_store(&obj_context->codec_state.encode.misc_param[i][j]);
2170
2171         for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext; i++)
2172             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params_ext[i]);
2173
2174         free(obj_context->codec_state.encode.slice_params_ext);
2175         if (obj_context->codec_state.encode.slice_rawdata_index) {
2176             free(obj_context->codec_state.encode.slice_rawdata_index);
2177             obj_context->codec_state.encode.slice_rawdata_index = NULL;
2178         }
2179         if (obj_context->codec_state.encode.slice_rawdata_count) {
2180             free(obj_context->codec_state.encode.slice_rawdata_count);
2181             obj_context->codec_state.encode.slice_rawdata_count = NULL;
2182         }
2183
2184         if (obj_context->codec_state.encode.slice_header_index) {
2185             free(obj_context->codec_state.encode.slice_header_index);
2186             obj_context->codec_state.encode.slice_header_index = NULL;
2187         }
2188
2189         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_params_ext; i++)
2190             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_params_ext[i]);
2191         free(obj_context->codec_state.encode.packed_header_params_ext);
2192
2193         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_data_ext; i++)
2194             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data_ext[i]);
2195         free(obj_context->codec_state.encode.packed_header_data_ext);
2196
2197         i965_release_buffer_store(&obj_context->codec_state.encode.encmb_map);
2198     } else {
2199         assert(obj_context->codec_state.decode.num_slice_params <= obj_context->codec_state.decode.max_slice_params);
2200         assert(obj_context->codec_state.decode.num_slice_datas <= obj_context->codec_state.decode.max_slice_datas);
2201
2202         i965_release_buffer_store(&obj_context->codec_state.decode.pic_param);
2203         i965_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
2204         i965_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
2205
2206         for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++)
2207             i965_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
2208
2209         for (i = 0; i < obj_context->codec_state.decode.num_slice_datas; i++)
2210             i965_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
2211
2212         free(obj_context->codec_state.decode.slice_params);
2213         free(obj_context->codec_state.decode.slice_datas);
2214     }
2215
2216     free(obj_context->render_targets);
2217     object_heap_free(heap, obj);
2218 }
2219
2220 static inline void
2221 max_resolution(struct i965_driver_data *i965,
2222                struct object_config *obj_config,
2223                int *w,                                  /* out */
2224                int *h)                                  /* out */
2225 {
2226     if (i965->codec_info->max_resolution) {
2227         i965->codec_info->max_resolution(i965, obj_config, w, h);
2228     } else {
2229         *w = i965->codec_info->max_width;
2230         *h = i965->codec_info->max_height;
2231     }
2232 }
2233
2234 VAStatus
2235 i965_CreateContext(VADriverContextP ctx,
2236                    VAConfigID config_id,
2237                    int picture_width,
2238                    int picture_height,
2239                    int flag,
2240                    VASurfaceID *render_targets,
2241                    int num_render_targets,
2242                    VAContextID *context)                /* out */
2243 {
2244     struct i965_driver_data *i965 = i965_driver_data(ctx);
2245     struct object_config *obj_config = CONFIG(config_id);
2246     struct object_context *obj_context = NULL;
2247     VAConfigAttrib *attrib;
2248     VAStatus vaStatus = VA_STATUS_SUCCESS;
2249     int contextID;
2250     int i;
2251     int max_width;
2252     int max_height;
2253
2254     if (NULL == obj_config) {
2255         vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
2256         return vaStatus;
2257     }
2258
2259     max_resolution(i965, obj_config, &max_width, &max_height);
2260
2261     if (picture_width > max_width ||
2262         picture_height > max_height) {
2263         vaStatus = VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
2264         return vaStatus;
2265     }
2266
2267     /* Validate flag */
2268     /* Validate picture dimensions */
2269     contextID = NEW_CONTEXT_ID();
2270     obj_context = CONTEXT(contextID);
2271
2272     if (NULL == obj_context) {
2273         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
2274         return vaStatus;
2275     }
2276
2277     *context = contextID;
2278     obj_context->flags = flag;
2279     obj_context->context_id = contextID;
2280     obj_context->obj_config = obj_config;
2281     obj_context->picture_width = picture_width;
2282     obj_context->picture_height = picture_height;
2283     obj_context->num_render_targets = num_render_targets;
2284     obj_context->render_targets = 
2285         (VASurfaceID *)calloc(num_render_targets, sizeof(VASurfaceID));
2286     obj_context->hw_context = NULL;
2287     obj_context->wrapper_context = VA_INVALID_ID;
2288
2289     if (!obj_context->render_targets)
2290         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2291
2292     for(i = 0; i < num_render_targets; i++) {
2293         if (NULL == SURFACE(render_targets[i])) {
2294             vaStatus = VA_STATUS_ERROR_INVALID_SURFACE;
2295             break;
2296         }
2297
2298         obj_context->render_targets[i] = render_targets[i];
2299     }
2300
2301     if (VA_STATUS_SUCCESS == vaStatus) {
2302         if (VAEntrypointVideoProc == obj_config->entrypoint) {
2303             obj_context->codec_type = CODEC_PROC;
2304             memset(&obj_context->codec_state.proc, 0, sizeof(obj_context->codec_state.proc));
2305             obj_context->codec_state.proc.current_render_target = VA_INVALID_ID;
2306             assert(i965->codec_info->proc_hw_context_init);
2307             obj_context->hw_context = i965->codec_info->proc_hw_context_init(ctx, obj_config);
2308          } else if ((VAEntrypointEncSlice == obj_config->entrypoint) || 
2309                     (VAEntrypointEncPicture == obj_config->entrypoint) ||
2310                     (VAEntrypointEncSliceLP == obj_config->entrypoint)) {
2311             VAConfigAttrib *packed_attrib;
2312             obj_context->codec_type = CODEC_ENC;
2313             memset(&obj_context->codec_state.encode, 0, sizeof(obj_context->codec_state.encode));
2314             obj_context->codec_state.encode.current_render_target = VA_INVALID_ID;
2315             obj_context->codec_state.encode.max_packed_header_params_ext = NUM_SLICES;
2316             obj_context->codec_state.encode.packed_header_params_ext =
2317                 calloc(obj_context->codec_state.encode.max_packed_header_params_ext,
2318                        sizeof(struct buffer_store *));
2319
2320             obj_context->codec_state.encode.max_packed_header_data_ext = NUM_SLICES;
2321             obj_context->codec_state.encode.packed_header_data_ext =
2322                 calloc(obj_context->codec_state.encode.max_packed_header_data_ext,
2323                        sizeof(struct buffer_store *));
2324
2325             obj_context->codec_state.encode.max_slice_num = NUM_SLICES;
2326             obj_context->codec_state.encode.slice_rawdata_index =
2327                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2328             obj_context->codec_state.encode.slice_rawdata_count =
2329                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2330
2331             obj_context->codec_state.encode.slice_header_index =
2332                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2333
2334             obj_context->codec_state.encode.vps_sps_seq_index = 0;
2335
2336             obj_context->codec_state.encode.slice_index = 0;
2337             packed_attrib = i965_lookup_config_attribute(obj_config, VAConfigAttribEncPackedHeaders);
2338             if (packed_attrib) {
2339                 obj_context->codec_state.encode.packed_header_flag = packed_attrib->value;
2340                 if (obj_config->profile == VAProfileVP9Profile0)
2341                     obj_context->codec_state.encode.packed_header_flag =
2342                             packed_attrib->value & VA_ENC_PACKED_HEADER_RAW_DATA;
2343             } else {
2344                 /* use the default value. SPS/PPS/RAWDATA is passed from user
2345                  * while Slice_header data is generated by driver.
2346                  */
2347                 obj_context->codec_state.encode.packed_header_flag =
2348                                VA_ENC_PACKED_HEADER_SEQUENCE |
2349                                VA_ENC_PACKED_HEADER_PICTURE |
2350                                VA_ENC_PACKED_HEADER_RAW_DATA;
2351
2352                 /* it is not used for VP9 */
2353                 if (obj_config->profile == VAProfileVP9Profile0)
2354                     obj_context->codec_state.encode.packed_header_flag = 0;
2355             }
2356             assert(i965->codec_info->enc_hw_context_init);
2357             obj_context->hw_context = i965->codec_info->enc_hw_context_init(ctx, obj_config);
2358         } else {
2359             obj_context->codec_type = CODEC_DEC;
2360             memset(&obj_context->codec_state.decode, 0, sizeof(obj_context->codec_state.decode));
2361             obj_context->codec_state.decode.current_render_target = -1;
2362             obj_context->codec_state.decode.max_slice_params = NUM_SLICES;
2363             obj_context->codec_state.decode.max_slice_datas = NUM_SLICES;
2364             obj_context->codec_state.decode.slice_params = calloc(obj_context->codec_state.decode.max_slice_params,
2365                                                                sizeof(*obj_context->codec_state.decode.slice_params));
2366             obj_context->codec_state.decode.slice_datas = calloc(obj_context->codec_state.decode.max_slice_datas,
2367                                                               sizeof(*obj_context->codec_state.decode.slice_datas));
2368
2369             assert(i965->codec_info->dec_hw_context_init);
2370             obj_context->hw_context = i965->codec_info->dec_hw_context_init(ctx, obj_config);
2371         }
2372     }
2373
2374     attrib = i965_lookup_config_attribute(obj_config, VAConfigAttribRTFormat);
2375     if (!attrib)
2376         return VA_STATUS_ERROR_INVALID_CONFIG;
2377     obj_context->codec_state.base.chroma_formats = attrib->value;
2378
2379     if (obj_config->wrapper_config != VA_INVALID_ID) {
2380         /* The wrapper_pdrvctx should exist when wrapper_config is valid.
2381          * So it won't check i965->wrapper_pdrvctx again.
2382          * Fixme if it is incorrect.
2383          */
2384         VAGenericID wrapper_context;
2385
2386         /*
2387          * The render_surface is not passed when calling
2388          * vaCreateContext.
2389          * If it is needed, we must get the wrapped surface
2390          * for the corresponding Surface_list.
2391          * So the wrapped surface conversion is deferred.
2392          */
2393         CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
2394                     vaCreateContext(i965->wrapper_pdrvctx,
2395                                     obj_config->wrapper_config,
2396                                     picture_width, picture_height,
2397                                     flag, NULL, 0,
2398                                     &wrapper_context));
2399
2400         if (vaStatus == VA_STATUS_SUCCESS)
2401             obj_context->wrapper_context = wrapper_context;
2402     }
2403     /* Error recovery */
2404     if (VA_STATUS_SUCCESS != vaStatus) {
2405         i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
2406     }
2407
2408     i965->current_context_id = contextID;
2409
2410     return vaStatus;
2411 }
2412
2413 VAStatus 
2414 i965_DestroyContext(VADriverContextP ctx, VAContextID context)
2415 {
2416     struct i965_driver_data *i965 = i965_driver_data(ctx);
2417     struct object_context *obj_context = CONTEXT(context);
2418     VAStatus va_status = VA_STATUS_SUCCESS;
2419
2420     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
2421
2422     if (i965->current_context_id == context)
2423         i965->current_context_id = VA_INVALID_ID;
2424
2425     if ((obj_context->wrapper_context != VA_INVALID_ID) &&
2426         i965->wrapper_pdrvctx) {
2427         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
2428                     vaDestroyContext(i965->wrapper_pdrvctx,
2429                                      obj_context->wrapper_context));
2430
2431         obj_context->wrapper_context = VA_INVALID_ID;
2432     }
2433
2434     i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
2435
2436     return va_status;
2437 }
2438
2439 static void 
2440 i965_destroy_buffer(struct object_heap *heap, struct object_base *obj)
2441 {
2442     struct object_buffer *obj_buffer = (struct object_buffer *)obj;
2443
2444     assert(obj_buffer->buffer_store);
2445     i965_release_buffer_store(&obj_buffer->buffer_store);
2446     object_heap_free(heap, obj);
2447 }
2448
2449 static VAStatus
2450 i965_create_buffer_internal(VADriverContextP ctx,
2451                             VAContextID context,
2452                             VABufferType type,
2453                             unsigned int size,
2454                             unsigned int num_elements,
2455                             void *data,
2456                             dri_bo *store_bo,
2457                             VABufferID *buf_id)
2458 {
2459     struct i965_driver_data *i965 = i965_driver_data(ctx);
2460     struct object_buffer *obj_buffer = NULL;
2461     struct buffer_store *buffer_store = NULL;
2462     int bufferID;
2463     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
2464     struct object_context *obj_context = CONTEXT(context);
2465     int wrapper_flag = 0;
2466
2467     /* Validate type */
2468     switch (type) {
2469     case VAPictureParameterBufferType:
2470     case VAIQMatrixBufferType:
2471     case VAQMatrixBufferType:
2472     case VABitPlaneBufferType:
2473     case VASliceGroupMapBufferType:
2474     case VASliceParameterBufferType:
2475     case VASliceDataBufferType:
2476     case VAMacroblockParameterBufferType:
2477     case VAResidualDataBufferType:
2478     case VADeblockingParameterBufferType:
2479     case VAImageBufferType:
2480     case VAEncCodedBufferType:
2481     case VAEncSequenceParameterBufferType:
2482     case VAEncPictureParameterBufferType:
2483     case VAEncSliceParameterBufferType:
2484     case VAEncPackedHeaderParameterBufferType:
2485     case VAEncPackedHeaderDataBufferType:
2486     case VAEncMiscParameterBufferType:
2487     case VAProcPipelineParameterBufferType:
2488     case VAProcFilterParameterBufferType:
2489     case VAHuffmanTableBufferType:
2490     case VAProbabilityBufferType:
2491     case VAEncMacroblockMapBufferType:
2492         /* Ok */
2493         break;
2494
2495     default:
2496         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
2497     }
2498
2499     bufferID = NEW_BUFFER_ID();
2500     obj_buffer = BUFFER(bufferID);
2501
2502     if (NULL == obj_buffer) {
2503         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2504     }
2505
2506     if (type == VAEncCodedBufferType) {
2507         size += I965_CODEDBUFFER_HEADER_SIZE;
2508         size += 0x1000; /* for upper bound check */
2509     }
2510
2511     obj_buffer->max_num_elements = num_elements;
2512     obj_buffer->num_elements = num_elements;
2513     obj_buffer->size_element = size;
2514     obj_buffer->type = type;
2515     obj_buffer->export_refcount = 0;
2516     obj_buffer->buffer_store = NULL;
2517     obj_buffer->wrapper_buffer = VA_INVALID_ID;
2518     obj_buffer->context_id = context;
2519
2520     buffer_store = calloc(1, sizeof(struct buffer_store));
2521     assert(buffer_store);
2522     buffer_store->ref_count = 1;
2523
2524     if (obj_context &&
2525         (obj_context->wrapper_context != VA_INVALID_ID) &&
2526         i965->wrapper_pdrvctx) {
2527         VAGenericID wrapper_buffer;
2528         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2529
2530         CALL_VTABLE(pdrvctx, vaStatus,
2531                     vaCreateBuffer(pdrvctx, obj_context->wrapper_context, type, size, num_elements,
2532                                    data, &wrapper_buffer));
2533         if (vaStatus == VA_STATUS_SUCCESS) {
2534             obj_buffer->wrapper_buffer = wrapper_buffer;
2535         } else {
2536             free(buffer_store);
2537             return vaStatus;
2538         }
2539         wrapper_flag = 1;
2540     }
2541
2542     if (store_bo != NULL) {
2543         buffer_store->bo = store_bo;
2544         dri_bo_reference(buffer_store->bo);
2545
2546         /* If the buffer is wrapped, the buffer_store is bogus. Unnecessary to copy it */
2547         if (data && !wrapper_flag)
2548             dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
2549     } else if (type == VASliceDataBufferType || 
2550                type == VAImageBufferType || 
2551                type == VAEncCodedBufferType ||
2552                type == VAEncMacroblockMapBufferType ||
2553                type == VAProbabilityBufferType) {
2554
2555         /* If the buffer is wrapped, the bo/buffer of buffer_store is bogus.
2556          * So it is enough to allocate one 64 byte bo
2557          */
2558         if (wrapper_flag)
2559             buffer_store->bo = dri_bo_alloc(i965->intel.bufmgr, "Bogus buffer",
2560                                             64, 64);
2561         else
2562             buffer_store->bo = dri_bo_alloc(i965->intel.bufmgr,
2563                                             "Buffer",
2564                                             size * num_elements, 64);
2565         assert(buffer_store->bo);
2566
2567         /* If the buffer is wrapped, the bo/buffer of buffer_store is bogus.
2568          * In fact it can be skipped. But it is still allocated and it is
2569          * only to follow the normal flowchart of buffer_allocation/release.
2570          */
2571         if (!wrapper_flag) {
2572           if (type == VAEncCodedBufferType) {
2573             struct i965_coded_buffer_segment *coded_buffer_segment;
2574
2575             dri_bo_map(buffer_store->bo, 1);
2576             coded_buffer_segment = (struct i965_coded_buffer_segment *)buffer_store->bo->virtual;
2577             coded_buffer_segment->base.size = size - I965_CODEDBUFFER_HEADER_SIZE;
2578             coded_buffer_segment->base.bit_offset = 0;
2579             coded_buffer_segment->base.status = 0;
2580             coded_buffer_segment->base.buf = NULL;
2581             coded_buffer_segment->base.next = NULL;
2582             coded_buffer_segment->mapped = 0;
2583             coded_buffer_segment->codec = 0;
2584             coded_buffer_segment->status_support = 0;
2585             dri_bo_unmap(buffer_store->bo);
2586           } else if (data) {
2587               dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
2588           }
2589        }
2590
2591     } else {
2592         int msize = size;
2593         
2594         if (type == VAEncPackedHeaderDataBufferType) {
2595             msize = ALIGN(size, 4);
2596         }
2597
2598         /* If the buffer is wrapped, it is enough to allocate 4 bytes */
2599         if (wrapper_flag)
2600             buffer_store->buffer = malloc(4);
2601         else
2602             buffer_store->buffer = malloc(msize * num_elements);
2603         assert(buffer_store->buffer);
2604
2605         if (!wrapper_flag) {
2606             if (data)
2607                 memcpy(buffer_store->buffer, data, size * num_elements);
2608             else
2609                 memset(buffer_store->buffer, 0, size * num_elements);
2610         }
2611     }
2612
2613     buffer_store->num_elements = obj_buffer->num_elements;
2614     i965_reference_buffer_store(&obj_buffer->buffer_store, buffer_store);
2615     i965_release_buffer_store(&buffer_store);
2616     *buf_id = bufferID;
2617
2618     return VA_STATUS_SUCCESS;
2619 }
2620
2621 VAStatus 
2622 i965_CreateBuffer(VADriverContextP ctx,
2623                   VAContextID context,          /* in */
2624                   VABufferType type,            /* in */
2625                   unsigned int size,            /* in */
2626                   unsigned int num_elements,    /* in */
2627                   void *data,                   /* in */
2628                   VABufferID *buf_id)           /* out */
2629 {
2630     return i965_create_buffer_internal(ctx, context, type, size, num_elements, data, NULL, buf_id);
2631 }
2632
2633
2634 VAStatus 
2635 i965_BufferSetNumElements(VADriverContextP ctx,
2636                           VABufferID buf_id,           /* in */
2637                           unsigned int num_elements)   /* in */
2638 {
2639     struct i965_driver_data *i965 = i965_driver_data(ctx);
2640     struct object_buffer *obj_buffer = BUFFER(buf_id);
2641     VAStatus vaStatus = VA_STATUS_SUCCESS;
2642
2643     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
2644
2645     /* When the wrapper_buffer exists, it will wrapper to the
2646      * buffer allocated from backend driver.
2647      */
2648     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2649         i965->wrapper_pdrvctx) {
2650         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2651
2652         CALL_VTABLE(pdrvctx, vaStatus,
2653                     vaBufferSetNumElements(pdrvctx, obj_buffer->wrapper_buffer,
2654                                          num_elements));
2655         return vaStatus;
2656     }
2657
2658     if ((num_elements < 0) || 
2659         (num_elements > obj_buffer->max_num_elements)) {
2660         vaStatus = VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2661     } else {
2662         obj_buffer->num_elements = num_elements;
2663         if (obj_buffer->buffer_store != NULL) {
2664             obj_buffer->buffer_store->num_elements = num_elements;
2665         }
2666     }
2667
2668     return vaStatus;
2669 }
2670
2671 VAStatus 
2672 i965_MapBuffer(VADriverContextP ctx,
2673                VABufferID buf_id,       /* in */
2674                void **pbuf)             /* out */
2675 {
2676     struct i965_driver_data *i965 = i965_driver_data(ctx);
2677     struct object_buffer *obj_buffer = BUFFER(buf_id);
2678     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
2679     struct object_context *obj_context;
2680
2681     ASSERT_RET(obj_buffer && obj_buffer->buffer_store, VA_STATUS_ERROR_INVALID_BUFFER);
2682
2683     obj_context = CONTEXT(obj_buffer->context_id);
2684
2685     /* When the wrapper_buffer exists, it will wrapper to the
2686      * buffer allocated from backend driver.
2687      */
2688     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2689         i965->wrapper_pdrvctx) {
2690         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2691
2692         CALL_VTABLE(pdrvctx, vaStatus,
2693                     vaMapBuffer(pdrvctx, obj_buffer->wrapper_buffer, pbuf));
2694         return vaStatus;
2695     }
2696
2697     ASSERT_RET(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
2698     ASSERT_RET(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer), VA_STATUS_ERROR_INVALID_BUFFER);
2699
2700     if (obj_buffer->export_refcount > 0)
2701         return VA_STATUS_ERROR_INVALID_BUFFER;
2702
2703     if (NULL != obj_buffer->buffer_store->bo) {
2704         unsigned int tiling, swizzle;
2705
2706         dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);
2707
2708         if (tiling != I915_TILING_NONE)
2709             drm_intel_gem_bo_map_gtt(obj_buffer->buffer_store->bo);
2710         else
2711             dri_bo_map(obj_buffer->buffer_store->bo, 1);
2712
2713         ASSERT_RET(obj_buffer->buffer_store->bo->virtual, VA_STATUS_ERROR_OPERATION_FAILED);
2714         *pbuf = obj_buffer->buffer_store->bo->virtual;
2715         vaStatus = VA_STATUS_SUCCESS;
2716
2717         if (obj_buffer->type == VAEncCodedBufferType) {
2718             int i;
2719             unsigned char *buffer = NULL;
2720             unsigned int  header_offset = I965_CODEDBUFFER_HEADER_SIZE;
2721             struct i965_coded_buffer_segment *coded_buffer_segment = (struct i965_coded_buffer_segment *)(obj_buffer->buffer_store->bo->virtual);
2722
2723             if (!coded_buffer_segment->mapped) {
2724                 unsigned char delimiter0, delimiter1, delimiter2, delimiter3, delimiter4;
2725
2726                 coded_buffer_segment->base.buf = buffer = (unsigned char *)(obj_buffer->buffer_store->bo->virtual) + I965_CODEDBUFFER_HEADER_SIZE;
2727
2728                 if (obj_context &&
2729                     obj_context->hw_context &&
2730                     obj_context->hw_context->get_status &&
2731                     coded_buffer_segment->status_support) {
2732                     vaStatus = obj_context->hw_context->get_status(ctx, obj_context->hw_context, coded_buffer_segment);
2733                 } else {
2734                     if (coded_buffer_segment->codec == CODEC_H264 ||
2735                         coded_buffer_segment->codec == CODEC_H264_MVC) {
2736                         delimiter0 = H264_DELIMITER0;
2737                         delimiter1 = H264_DELIMITER1;
2738                         delimiter2 = H264_DELIMITER2;
2739                         delimiter3 = H264_DELIMITER3;
2740                         delimiter4 = H264_DELIMITER4;
2741                     } else if (coded_buffer_segment->codec == CODEC_MPEG2) {
2742                         delimiter0 = MPEG2_DELIMITER0;
2743                         delimiter1 = MPEG2_DELIMITER1;
2744                         delimiter2 = MPEG2_DELIMITER2;
2745                         delimiter3 = MPEG2_DELIMITER3;
2746                         delimiter4 = MPEG2_DELIMITER4;
2747                     } else if(coded_buffer_segment->codec == CODEC_JPEG) {
2748                         //In JPEG End of Image (EOI = 0xDDF9) marker can be used for delimiter.
2749                         delimiter0 = 0xFF;
2750                         delimiter1 = 0xD9;
2751                     } else if (coded_buffer_segment->codec == CODEC_HEVC) {
2752                         delimiter0 = HEVC_DELIMITER0;
2753                         delimiter1 = HEVC_DELIMITER1;
2754                         delimiter2 = HEVC_DELIMITER2;
2755                         delimiter3 = HEVC_DELIMITER3;
2756                         delimiter4 = HEVC_DELIMITER4;
2757                     } else if (coded_buffer_segment->codec != CODEC_VP8) {
2758                         ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_PROFILE);
2759                     }
2760
2761                     if(coded_buffer_segment->codec == CODEC_JPEG) {
2762                         for(i = 0; i <  obj_buffer->size_element - header_offset - 1 - 0x1000; i++) {
2763                             if( (buffer[i] == 0xFF) && (buffer[i + 1] == 0xD9)) {
2764                                 break;
2765                             }
2766                         }
2767                         coded_buffer_segment->base.size = i + 2;
2768                     } else if (coded_buffer_segment->codec != CODEC_VP8) {
2769                         /* vp8 coded buffer size can be told by vp8 internal statistics buffer,
2770                            so it don't need to traversal the coded buffer */
2771                         for (i = 0; i < obj_buffer->size_element - header_offset - 3 - 0x1000; i++) {
2772                             if ((buffer[i] == delimiter0) &&
2773                                 (buffer[i + 1] == delimiter1) &&
2774                                 (buffer[i + 2] == delimiter2) &&
2775                                 (buffer[i + 3] == delimiter3) &&
2776                                 (buffer[i + 4] == delimiter4))
2777                                 break;
2778                         }
2779
2780                         if (i == obj_buffer->size_element - header_offset - 3 - 0x1000) {
2781                             coded_buffer_segment->base.status |= VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK;
2782                         }
2783                         coded_buffer_segment->base.size = i;
2784                     }
2785
2786                     if (coded_buffer_segment->base.size >= obj_buffer->size_element - header_offset - 0x1000) {
2787                         coded_buffer_segment->base.status |= VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK;
2788                     }
2789
2790                     vaStatus = VA_STATUS_SUCCESS;
2791                 }
2792
2793                 coded_buffer_segment->mapped = 1;
2794             } else {
2795                 assert(coded_buffer_segment->base.buf);
2796                 vaStatus = VA_STATUS_SUCCESS;
2797             }
2798         }
2799     } else if (NULL != obj_buffer->buffer_store->buffer) {
2800         *pbuf = obj_buffer->buffer_store->buffer;
2801         vaStatus = VA_STATUS_SUCCESS;
2802     }
2803
2804     return vaStatus;
2805 }
2806
2807 VAStatus 
2808 i965_UnmapBuffer(VADriverContextP ctx, VABufferID buf_id)
2809 {
2810     struct i965_driver_data *i965 = i965_driver_data(ctx);
2811     struct object_buffer *obj_buffer = BUFFER(buf_id);
2812     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
2813
2814     if ((buf_id & OBJECT_HEAP_OFFSET_MASK) != BUFFER_ID_OFFSET)
2815         return VA_STATUS_ERROR_INVALID_BUFFER;
2816
2817     ASSERT_RET(obj_buffer && obj_buffer->buffer_store, VA_STATUS_ERROR_INVALID_BUFFER);
2818     /* When the wrapper_buffer exists, it will wrapper to the
2819      * buffer allocated from backend driver.
2820      */
2821     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2822         i965->wrapper_pdrvctx) {
2823         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2824
2825         CALL_VTABLE(pdrvctx, vaStatus,
2826                     vaUnmapBuffer(pdrvctx, obj_buffer->wrapper_buffer));
2827         return vaStatus;
2828     }
2829
2830     ASSERT_RET(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_OPERATION_FAILED);
2831     ASSERT_RET(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer), VA_STATUS_ERROR_OPERATION_FAILED);
2832
2833     if (NULL != obj_buffer->buffer_store->bo) {
2834         unsigned int tiling, swizzle;
2835
2836         dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);
2837
2838         if (tiling != I915_TILING_NONE)
2839             drm_intel_gem_bo_unmap_gtt(obj_buffer->buffer_store->bo);
2840         else
2841             dri_bo_unmap(obj_buffer->buffer_store->bo);
2842
2843         vaStatus = VA_STATUS_SUCCESS;
2844     } else if (NULL != obj_buffer->buffer_store->buffer) {
2845         /* Do nothing */
2846         vaStatus = VA_STATUS_SUCCESS;
2847     }
2848
2849     return vaStatus;    
2850 }
2851
2852 VAStatus 
2853 i965_DestroyBuffer(VADriverContextP ctx, VABufferID buffer_id)
2854 {
2855     struct i965_driver_data *i965 = i965_driver_data(ctx);
2856     struct object_buffer *obj_buffer = BUFFER(buffer_id);
2857     VAStatus va_status = VA_STATUS_SUCCESS;
2858
2859     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
2860
2861     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2862         i965->wrapper_pdrvctx) {
2863         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
2864                     vaDestroyBuffer(i965->wrapper_pdrvctx,
2865                                     obj_buffer->wrapper_buffer));
2866         obj_buffer->wrapper_buffer = VA_INVALID_ID;
2867     }
2868
2869     i965_destroy_buffer(&i965->buffer_heap, (struct object_base *)obj_buffer);
2870
2871     return va_status;
2872 }
2873
2874 VAStatus 
2875 i965_BeginPicture(VADriverContextP ctx,
2876                   VAContextID context,
2877                   VASurfaceID render_target)
2878 {
2879     struct i965_driver_data *i965 = i965_driver_data(ctx); 
2880     struct object_context *obj_context = CONTEXT(context);
2881     struct object_surface *obj_surface = SURFACE(render_target);
2882     struct object_config *obj_config;
2883     VAStatus vaStatus = VA_STATUS_SUCCESS;
2884     int i, j;
2885
2886     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
2887     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
2888     obj_config = obj_context->obj_config;
2889     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
2890
2891     if (is_surface_busy(i965, obj_surface))
2892         return VA_STATUS_ERROR_SURFACE_BUSY;
2893
2894     if (obj_context->codec_type == CODEC_PROC) {
2895         obj_context->codec_state.proc.current_render_target = render_target;
2896     } else if (obj_context->codec_type == CODEC_ENC) {
2897         /* ext */
2898         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param_ext);
2899
2900         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_param); i++)
2901             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_param[i]);
2902
2903         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_data); i++)
2904             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data[i]);
2905
2906         for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext; i++)
2907             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params_ext[i]);
2908
2909         obj_context->codec_state.encode.num_slice_params_ext = 0;
2910         obj_context->codec_state.encode.current_render_target = render_target;     /*This is input new frame*/
2911         obj_context->codec_state.encode.last_packed_header_type = 0;
2912         memset(obj_context->codec_state.encode.slice_rawdata_index, 0,
2913                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
2914         memset(obj_context->codec_state.encode.slice_rawdata_count, 0,
2915                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
2916         memset(obj_context->codec_state.encode.slice_header_index, 0,
2917                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
2918
2919         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_params_ext; i++)
2920             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_params_ext[i]);
2921         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_data_ext; i++)
2922             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data_ext[i]);
2923         obj_context->codec_state.encode.num_packed_header_params_ext = 0;
2924         obj_context->codec_state.encode.num_packed_header_data_ext = 0;
2925         obj_context->codec_state.encode.slice_index = 0;
2926         obj_context->codec_state.encode.vps_sps_seq_index = 0;
2927
2928         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param); i++)
2929             for (j = 0; j < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param[0]); j++)
2930                 i965_release_buffer_store(&obj_context->codec_state.encode.misc_param[i][j]);
2931
2932         i965_release_buffer_store(&obj_context->codec_state.encode.encmb_map);
2933     } else {
2934         obj_context->codec_state.decode.current_render_target = render_target;
2935         i965_release_buffer_store(&obj_context->codec_state.decode.pic_param);
2936         i965_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
2937         i965_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
2938         i965_release_buffer_store(&obj_context->codec_state.decode.huffman_table);
2939
2940         for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++) {
2941             i965_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
2942             i965_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
2943         }
2944
2945         obj_context->codec_state.decode.num_slice_params = 0;
2946         obj_context->codec_state.decode.num_slice_datas = 0;
2947
2948         if ((obj_context->wrapper_context != VA_INVALID_ID) &&
2949             i965->wrapper_pdrvctx) {
2950             if (obj_surface->wrapper_surface == VA_INVALID_ID)
2951                 vaStatus = i965_surface_wrapper(ctx, render_target);
2952
2953             if (vaStatus != VA_STATUS_SUCCESS)
2954                 return vaStatus;
2955
2956             CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
2957                         vaBeginPicture(i965->wrapper_pdrvctx,
2958                                        obj_context->wrapper_context,
2959                                        obj_surface->wrapper_surface));
2960         }
2961     }
2962
2963     return vaStatus;
2964 }
2965
2966 #define I965_RENDER_BUFFER(category, name) i965_render_##category##_##name##_buffer(ctx, obj_context, obj_buffer)
2967
2968 #define DEF_RENDER_SINGLE_BUFFER_FUNC(category, name, member)           \
2969     static VAStatus                                                     \
2970     i965_render_##category##_##name##_buffer(VADriverContextP ctx,      \
2971                                              struct object_context *obj_context, \
2972                                              struct object_buffer *obj_buffer) \
2973     {                                                                   \
2974         struct category##_state *category = &obj_context->codec_state.category; \
2975         i965_release_buffer_store(&category->member);                   \
2976         i965_reference_buffer_store(&category->member, obj_buffer->buffer_store); \
2977         return VA_STATUS_SUCCESS;                                       \
2978     }
2979
2980 #define DEF_RENDER_MULTI_BUFFER_FUNC(category, name, member)            \
2981     static VAStatus                                                     \
2982     i965_render_##category##_##name##_buffer(VADriverContextP ctx,      \
2983                                              struct object_context *obj_context, \
2984                                              struct object_buffer *obj_buffer) \
2985     {                                                                   \
2986         struct category##_state *category = &obj_context->codec_state.category; \
2987         if (category->num_##member == category->max_##member) {         \
2988             category->member = realloc(category->member, (category->max_##member + NUM_SLICES) * sizeof(*category->member)); \
2989             memset(category->member + category->max_##member, 0, NUM_SLICES * sizeof(*category->member)); \
2990             category->max_##member += NUM_SLICES;                       \
2991         }                                                               \
2992         i965_release_buffer_store(&category->member[category->num_##member]); \
2993         i965_reference_buffer_store(&category->member[category->num_##member], obj_buffer->buffer_store); \
2994         category->num_##member++;                                       \
2995         return VA_STATUS_SUCCESS;                                       \
2996     }
2997
2998 #define I965_RENDER_DECODE_BUFFER(name) I965_RENDER_BUFFER(decode, name)
2999
3000 #define DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(decode, name, member)
3001 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(picture_parameter, pic_param)
3002 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(iq_matrix, iq_matrix)
3003 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(bit_plane, bit_plane)
3004 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(huffman_table, huffman_table)
3005 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(probability_data, probability_data)
3006
3007 #define DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(name, member) DEF_RENDER_MULTI_BUFFER_FUNC(decode, name, member)
3008 DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(slice_parameter, slice_params)
3009 DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(slice_data, slice_datas)
3010
3011
3012 static VAStatus
3013 i965_decoder_vp9_wrapper_picture(VADriverContextP ctx,
3014                              VABufferID *buffers,
3015                              int num_buffers)
3016 {
3017     struct i965_driver_data *i965 = i965_driver_data(ctx);
3018     VAStatus vaStatus = VA_STATUS_SUCCESS;
3019     int i;
3020     VADecPictureParameterBufferVP9 *pVp9PicParams;
3021     VADriverContextP pdrvctx;
3022     struct object_buffer *obj_buffer;
3023
3024     pdrvctx = i965->wrapper_pdrvctx;
3025     /* do the conversion of VADecPictureParameterBufferVP9 */
3026     for (i = 0; i < num_buffers; i++) {
3027         obj_buffer = BUFFER(buffers[i]);
3028
3029         if (!obj_buffer)
3030             continue;
3031
3032         if (obj_buffer->wrapper_buffer == VA_INVALID_ID)
3033             continue;
3034
3035         if (obj_buffer->type == VAPictureParameterBufferType) {
3036             int j;
3037             VASurfaceID surface_id;
3038             struct object_surface *obj_surface;
3039
3040             pdrvctx = i965->wrapper_pdrvctx;
3041
3042             CALL_VTABLE(pdrvctx, vaStatus,
3043                         vaMapBuffer(pdrvctx, obj_buffer->wrapper_buffer,
3044                                     (void **)(&pVp9PicParams)));
3045
3046             if (vaStatus != VA_STATUS_SUCCESS)
3047                 return vaStatus;
3048
3049             for (j = 0; j < 8; j++) {
3050                 surface_id = pVp9PicParams->reference_frames[j];
3051                 obj_surface = SURFACE(surface_id);
3052
3053                 if (!obj_surface)
3054                     continue;
3055
3056                 if (obj_surface->wrapper_surface == VA_INVALID_ID) {
3057                     vaStatus = i965_surface_wrapper(ctx, surface_id);
3058                     if (vaStatus != VA_STATUS_SUCCESS) {
3059                         pdrvctx->vtable->vaUnmapBuffer(pdrvctx,
3060                                     obj_buffer->wrapper_buffer);
3061                         goto fail_out;
3062                     }
3063                 }
3064
3065                 pVp9PicParams->reference_frames[j] = obj_surface->wrapper_surface;
3066             }
3067             CALL_VTABLE(pdrvctx, vaStatus,
3068                         vaUnmapBuffer(pdrvctx, obj_buffer->wrapper_buffer));
3069             break;
3070         }
3071     }
3072
3073     return VA_STATUS_SUCCESS;
3074
3075 fail_out:
3076     return vaStatus;
3077 }
3078
3079 static VAStatus
3080 i965_decoder_wrapper_picture(VADriverContextP ctx,
3081                              VAContextID context,
3082                              VABufferID *buffers,
3083                              int num_buffers)
3084 {
3085     struct i965_driver_data *i965 = i965_driver_data(ctx);
3086     struct object_context *obj_context = CONTEXT(context);
3087     VAStatus vaStatus = VA_STATUS_SUCCESS;
3088     int i;
3089     VADriverContextP pdrvctx;
3090     struct object_buffer *obj_buffer;
3091
3092     if (obj_context == NULL)
3093         return VA_STATUS_ERROR_INVALID_CONTEXT;
3094
3095     /* When it is not wrapped context, continue the normal flowchart */
3096     if (obj_context->wrapper_context == VA_INVALID_ID)
3097         return vaStatus;
3098
3099     if (obj_context->obj_config &&
3100         (obj_context->obj_config->profile == VAProfileVP9Profile0)) {
3101         vaStatus = i965_decoder_vp9_wrapper_picture(ctx, buffers, num_buffers);
3102     } else
3103         return VA_STATUS_ERROR_INVALID_PARAMETER;
3104
3105     pdrvctx = i965->wrapper_pdrvctx;
3106
3107     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3108         obj_buffer = BUFFER(buffers[i]);
3109
3110         if (!obj_buffer)
3111             continue;
3112
3113         if (obj_buffer->wrapper_buffer == VA_INVALID_ID) {
3114             vaStatus = VA_STATUS_ERROR_INVALID_BUFFER;
3115             break;
3116         }
3117
3118         CALL_VTABLE(pdrvctx, vaStatus,
3119                     vaRenderPicture(pdrvctx, obj_context->wrapper_context,
3120                                     &(obj_buffer->wrapper_buffer), 1));
3121     }
3122     return vaStatus;
3123 }
3124
3125 static VAStatus 
3126 i965_decoder_render_picture(VADriverContextP ctx,
3127                             VAContextID context,
3128                             VABufferID *buffers,
3129                             int num_buffers)
3130 {
3131     struct i965_driver_data *i965 = i965_driver_data(ctx); 
3132     struct object_context *obj_context = CONTEXT(context);
3133     VAStatus vaStatus = VA_STATUS_SUCCESS;
3134     int i;
3135     
3136     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3137
3138     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3139         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3140
3141         if (!obj_buffer)
3142             return VA_STATUS_ERROR_INVALID_BUFFER;
3143
3144         switch (obj_buffer->type) {
3145         case VAPictureParameterBufferType:
3146             vaStatus = I965_RENDER_DECODE_BUFFER(picture_parameter);
3147             break;
3148             
3149         case VAIQMatrixBufferType:
3150             vaStatus = I965_RENDER_DECODE_BUFFER(iq_matrix);
3151             break;
3152
3153         case VABitPlaneBufferType:
3154             vaStatus = I965_RENDER_DECODE_BUFFER(bit_plane);
3155             break;
3156
3157         case VASliceParameterBufferType:
3158             vaStatus = I965_RENDER_DECODE_BUFFER(slice_parameter);
3159             break;
3160
3161         case VASliceDataBufferType:
3162             vaStatus = I965_RENDER_DECODE_BUFFER(slice_data);
3163             break;
3164
3165         case VAHuffmanTableBufferType:
3166             vaStatus = I965_RENDER_DECODE_BUFFER(huffman_table);
3167             break;
3168
3169         case VAProbabilityBufferType:
3170             vaStatus = I965_RENDER_DECODE_BUFFER(probability_data);
3171             break;
3172
3173         default:
3174             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3175             break;
3176         }
3177     }
3178
3179     if ((vaStatus == VA_STATUS_SUCCESS) &&
3180         (obj_context->wrapper_context != VA_INVALID_ID))
3181         vaStatus = i965_decoder_wrapper_picture(ctx, context, buffers, num_buffers);
3182
3183     return vaStatus;
3184 }
3185
3186 #define I965_RENDER_ENCODE_BUFFER(name) I965_RENDER_BUFFER(encode, name)
3187
3188 #define DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(encode, name, member)
3189 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(sequence_parameter, seq_param)    
3190 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_parameter, pic_param)
3191 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_control, pic_control)
3192 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(qmatrix, q_matrix)
3193 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(iqmatrix, iq_matrix)
3194 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(huffman_table, huffman_table)
3195 /* extended buffer */
3196 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(sequence_parameter_ext, seq_param_ext)
3197 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_parameter_ext, pic_param_ext)
3198 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(encmb_map, encmb_map)
3199
3200 #define DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(name, member) DEF_RENDER_MULTI_BUFFER_FUNC(encode, name, member)
3201 // DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(slice_parameter, slice_params)
3202 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(slice_parameter_ext, slice_params_ext)
3203
3204 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(packed_header_params_ext, packed_header_params_ext)
3205 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(packed_header_data_ext, packed_header_data_ext)
3206
3207 static VAStatus
3208 i965_encoder_render_packed_header_parameter_buffer(VADriverContextP ctx,
3209                                                    struct object_context *obj_context,
3210                                                    struct object_buffer *obj_buffer,
3211                                                    int type_index)
3212 {
3213     struct encode_state *encode = &obj_context->codec_state.encode;
3214
3215     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3216     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3217     i965_release_buffer_store(&encode->packed_header_param[type_index]);
3218     i965_reference_buffer_store(&encode->packed_header_param[type_index], obj_buffer->buffer_store);
3219
3220     return VA_STATUS_SUCCESS;
3221 }
3222
3223 static VAStatus
3224 i965_encoder_render_packed_header_data_buffer(VADriverContextP ctx,
3225                                               struct object_context *obj_context,
3226                                               struct object_buffer *obj_buffer,
3227                                               int type_index)
3228 {
3229     struct encode_state *encode = &obj_context->codec_state.encode;
3230
3231     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3232     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3233     i965_release_buffer_store(&encode->packed_header_data[type_index]);
3234     i965_reference_buffer_store(&encode->packed_header_data[type_index], obj_buffer->buffer_store);
3235
3236     return VA_STATUS_SUCCESS;
3237 }
3238
3239 static int
3240 i965_encoder_get_misc_paramerter_buffer_index(VADriverContextP ctx,
3241                                               struct encode_state *encode,
3242                                               VAEncMiscParameterBuffer *misc_param)
3243 {
3244     int index = 0;
3245
3246     if (!encode->has_layers)
3247         return 0;
3248
3249     if (misc_param->type == VAEncMiscParameterTypeRateControl) {
3250         VAEncMiscParameterRateControl *misc_rate_control = (VAEncMiscParameterRateControl *)misc_param->data;
3251
3252         index = misc_rate_control->rc_flags.bits.temporal_id;
3253     } else if (misc_param->type == VAEncMiscParameterTypeFrameRate) {
3254         VAEncMiscParameterFrameRate *misc_frame_rate = (VAEncMiscParameterFrameRate *)misc_param->data;
3255
3256         index = misc_frame_rate->framerate_flags.bits.temporal_id;
3257     }
3258
3259     return index;
3260 }
3261
3262 static VAStatus
3263 i965_encoder_render_misc_parameter_buffer(VADriverContextP ctx,
3264                                           struct object_context *obj_context,
3265                                           struct object_buffer *obj_buffer)
3266 {
3267     struct encode_state *encode = &obj_context->codec_state.encode;
3268     VAEncMiscParameterBuffer *param = NULL;
3269     int index;
3270
3271     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3272     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3273
3274     param = (VAEncMiscParameterBuffer *)obj_buffer->buffer_store->buffer;
3275
3276     if (param->type >= ARRAY_ELEMS(encode->misc_param))
3277         return VA_STATUS_ERROR_INVALID_PARAMETER;
3278
3279     if (param->type == VAEncMiscParameterTypeTemporalLayerStructure)
3280         encode->has_layers = 1;
3281
3282     index = i965_encoder_get_misc_paramerter_buffer_index(ctx, encode, param);
3283
3284     if (index >= ARRAY_ELEMS(encode->misc_param[0]))
3285         return VA_STATUS_ERROR_INVALID_PARAMETER;
3286
3287     i965_release_buffer_store(&encode->misc_param[param->type][index]);
3288     i965_reference_buffer_store(&encode->misc_param[param->type][index], obj_buffer->buffer_store);
3289
3290     return VA_STATUS_SUCCESS;
3291 }
3292
3293 static VAStatus 
3294 i965_encoder_render_picture(VADriverContextP ctx,
3295                             VAContextID context,
3296                             VABufferID *buffers,
3297                             int num_buffers)
3298 {
3299     struct i965_driver_data *i965 = i965_driver_data(ctx); 
3300     struct object_context *obj_context = CONTEXT(context);
3301     struct object_config *obj_config;
3302     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3303     struct encode_state *encode;
3304     int i;
3305
3306     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3307     obj_config = obj_context->obj_config;
3308     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3309
3310     encode = &obj_context->codec_state.encode;
3311     for (i = 0; i < num_buffers; i++) {  
3312         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3313
3314         if (!obj_buffer)
3315             return VA_STATUS_ERROR_INVALID_BUFFER;
3316
3317         switch (obj_buffer->type) {
3318         case VAQMatrixBufferType:
3319             vaStatus = I965_RENDER_ENCODE_BUFFER(qmatrix);
3320             break;
3321
3322         case VAIQMatrixBufferType:
3323             vaStatus = I965_RENDER_ENCODE_BUFFER(iqmatrix);
3324             break;
3325
3326         case VAEncSequenceParameterBufferType:
3327             vaStatus = I965_RENDER_ENCODE_BUFFER(sequence_parameter_ext);
3328             break;
3329
3330         case VAEncPictureParameterBufferType:
3331             vaStatus = I965_RENDER_ENCODE_BUFFER(picture_parameter_ext);
3332             break;
3333
3334         case VAHuffmanTableBufferType:
3335             vaStatus = I965_RENDER_ENCODE_BUFFER(huffman_table);
3336             break;
3337
3338         case VAEncSliceParameterBufferType:
3339             vaStatus = I965_RENDER_ENCODE_BUFFER(slice_parameter_ext);
3340             if (vaStatus == VA_STATUS_SUCCESS) {
3341                 /* When the max number of slices is updated, it also needs
3342                  * to reallocate the arrays that is used to store
3343                  * the packed data index/count for the slice
3344                  */
3345                 if (!(encode->packed_header_flag & VA_ENC_PACKED_HEADER_SLICE)) {
3346                    encode->slice_index++;
3347                 }
3348                 if (encode->slice_index == encode->max_slice_num) {
3349                     int slice_num = encode->max_slice_num;
3350                     encode->slice_rawdata_index = realloc(encode->slice_rawdata_index,
3351                                                           (slice_num + NUM_SLICES) * sizeof(int));
3352                     encode->slice_rawdata_count = realloc(encode->slice_rawdata_count,
3353                                                           (slice_num + NUM_SLICES) * sizeof(int));
3354                     encode->slice_header_index = realloc(encode->slice_header_index,
3355                                                           (slice_num + NUM_SLICES) * sizeof(int));
3356                     memset(encode->slice_rawdata_index + slice_num, 0,
3357                         sizeof(int) * NUM_SLICES);
3358                     memset(encode->slice_rawdata_count + slice_num, 0,
3359                         sizeof(int) * NUM_SLICES);
3360                     memset(encode->slice_header_index + slice_num, 0,
3361                         sizeof(int) * NUM_SLICES);
3362
3363                     encode->max_slice_num += NUM_SLICES;
3364                     if ((encode->slice_rawdata_index == NULL) ||
3365                         (encode->slice_header_index == NULL)  ||
3366                         (encode->slice_rawdata_count == NULL)) {
3367                         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
3368                         return vaStatus;
3369                     }
3370                 }
3371             }
3372             break;
3373
3374         case VAEncPackedHeaderParameterBufferType:
3375         {
3376             VAEncPackedHeaderParameterBuffer *param = (VAEncPackedHeaderParameterBuffer *)obj_buffer->buffer_store->buffer;
3377             encode->last_packed_header_type = param->type;
3378
3379             if ((param->type == VAEncPackedHeaderRawData) ||
3380                 (param->type == VAEncPackedHeaderSlice)) {
3381                 vaStatus = I965_RENDER_ENCODE_BUFFER(packed_header_params_ext);
3382             } else if((obj_config->profile == VAProfileHEVCMain ||
3383                 obj_config->profile == VAProfileHEVCMain10) &&
3384                 (encode->last_packed_header_type == VAEncPackedHeaderSequence)) {
3385                 vaStatus = i965_encoder_render_packed_header_parameter_buffer(ctx,
3386                                                                           obj_context,
3387                                                                           obj_buffer,
3388                                                                           va_enc_packed_type_to_idx(encode->last_packed_header_type) + encode->vps_sps_seq_index);
3389             } else {
3390                 vaStatus = i965_encoder_render_packed_header_parameter_buffer(ctx,
3391                                                                           obj_context,
3392                                                                           obj_buffer,
3393                                                                           va_enc_packed_type_to_idx(encode->last_packed_header_type));
3394             }
3395             break;
3396         }
3397
3398         case VAEncPackedHeaderDataBufferType:
3399         {
3400             if (encode->last_packed_header_type == 0) {
3401                 WARN_ONCE("the packed header data is passed without type!\n");
3402                 vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
3403                 return vaStatus;
3404             }
3405
3406             if (encode->last_packed_header_type == VAEncPackedHeaderRawData ||
3407                 encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3408                 vaStatus = I965_RENDER_ENCODE_BUFFER(packed_header_data_ext);
3409
3410                 if (obj_config->profile == VAProfileVP9Profile0)
3411                     break;
3412
3413                 /* When the PACKED_SLICE_HEADER flag is passed, it will use
3414                  * the packed_slice_header as the delimeter to decide how
3415                  * the packed rawdata is inserted for the given slice.
3416                  * Otherwise it will use the VAEncSequenceParameterBuffer
3417                  * as the delimeter
3418                  */
3419                 if (encode->packed_header_flag & VA_ENC_PACKED_HEADER_SLICE) {
3420                     /* store the first index of the packed header data for current slice */
3421                     if (encode->slice_rawdata_index[encode->slice_index] == 0) {
3422                         encode->slice_rawdata_index[encode->slice_index] =
3423                             SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3424                     }
3425                     encode->slice_rawdata_count[encode->slice_index]++;
3426                     if (encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3427                         /* find one packed slice_header delimeter. And the following
3428                          * packed data is for the next slice
3429                          */
3430                         encode->slice_header_index[encode->slice_index] =
3431                             SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3432                         encode->slice_index++;
3433                         /* Reallocate the buffer to record the index/count of
3434                          * packed_data for one slice.
3435                          */
3436                         if (encode->slice_index == encode->max_slice_num) {
3437                             int slice_num = encode->max_slice_num;
3438
3439                             encode->slice_rawdata_index = realloc(encode->slice_rawdata_index,
3440                                                           (slice_num + NUM_SLICES) * sizeof(int));
3441                             encode->slice_rawdata_count = realloc(encode->slice_rawdata_count,
3442                                                           (slice_num + NUM_SLICES) * sizeof(int));
3443                             encode->slice_header_index = realloc(encode->slice_header_index,
3444                                                           (slice_num + NUM_SLICES) * sizeof(int));
3445                             memset(encode->slice_rawdata_index + slice_num, 0,
3446                                    sizeof(int) * NUM_SLICES);
3447                             memset(encode->slice_rawdata_count + slice_num, 0,
3448                                    sizeof(int) * NUM_SLICES);
3449                             memset(encode->slice_header_index + slice_num, 0,
3450                                    sizeof(int) * NUM_SLICES);
3451                             encode->max_slice_num += NUM_SLICES;
3452                         }
3453                     }
3454                 } else {
3455                     if (vaStatus == VA_STATUS_SUCCESS) {
3456                         /* store the first index of the packed header data for current slice */
3457                         if (encode->slice_rawdata_index[encode->slice_index] == 0) {
3458                             encode->slice_rawdata_index[encode->slice_index] =
3459                                 SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3460                         }
3461                         encode->slice_rawdata_count[encode->slice_index]++;
3462                         if (encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3463                             if (encode->slice_header_index[encode->slice_index] == 0) {
3464                                 encode->slice_header_index[encode->slice_index] =
3465                                     SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3466                             } else {
3467                                 WARN_ONCE("Multi slice header data is passed for"
3468                                       " slice %d!\n", encode->slice_index);
3469                             }
3470                         }
3471                     }
3472                 }
3473             } else {
3474                 ASSERT_RET(encode->last_packed_header_type == VAEncPackedHeaderSequence ||
3475                     encode->last_packed_header_type == VAEncPackedHeaderPicture ||
3476                     encode->last_packed_header_type == VAEncPackedHeaderSlice ||
3477                    (((encode->last_packed_header_type & VAEncPackedHeaderMiscMask) == VAEncPackedHeaderMiscMask) &&
3478                     ((encode->last_packed_header_type & (~VAEncPackedHeaderMiscMask)) != 0)),
3479                     VA_STATUS_ERROR_ENCODING_ERROR);
3480
3481                 if((obj_config->profile == VAProfileHEVCMain ||
3482                     obj_config->profile == VAProfileHEVCMain10) &&
3483                     (encode->last_packed_header_type == VAEncPackedHeaderSequence)) {
3484
3485                         vaStatus = i965_encoder_render_packed_header_data_buffer(ctx,
3486                             obj_context,
3487                             obj_buffer,
3488                             va_enc_packed_type_to_idx(encode->last_packed_header_type) + encode->vps_sps_seq_index);
3489                         encode->vps_sps_seq_index = (encode->vps_sps_seq_index + 1) % I965_SEQ_PACKED_HEADER_END;
3490                 }else{
3491                     vaStatus = i965_encoder_render_packed_header_data_buffer(ctx,
3492                         obj_context,
3493                         obj_buffer,
3494                         va_enc_packed_type_to_idx(encode->last_packed_header_type));
3495
3496                 }
3497             }
3498             encode->last_packed_header_type = 0;
3499             break;       
3500         }
3501
3502         case VAEncMiscParameterBufferType:
3503             vaStatus = i965_encoder_render_misc_parameter_buffer(ctx,
3504                                                                  obj_context,
3505                                                                  obj_buffer);
3506             break;
3507             
3508         case VAEncMacroblockMapBufferType:
3509             vaStatus = I965_RENDER_ENCODE_BUFFER(encmb_map);
3510             break;
3511
3512         default:
3513             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3514             break;
3515         }
3516     }   
3517
3518     return vaStatus;
3519 }
3520
3521 #define I965_RENDER_PROC_BUFFER(name) I965_RENDER_BUFFER(proc, name)
3522
3523 #define DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(proc, name, member)
3524 DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(pipeline_parameter, pipeline_param)    
3525
3526 static VAStatus 
3527 i965_proc_render_picture(VADriverContextP ctx,
3528                          VAContextID context,
3529                          VABufferID *buffers,
3530                          int num_buffers)
3531 {
3532     struct i965_driver_data *i965 = i965_driver_data(ctx); 
3533     struct object_context *obj_context = CONTEXT(context);
3534     VAStatus vaStatus = VA_STATUS_SUCCESS;
3535     int i;
3536
3537     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3538
3539     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3540         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3541
3542         if (!obj_buffer)
3543             return VA_STATUS_ERROR_INVALID_BUFFER;
3544
3545         switch (obj_buffer->type) {
3546         case VAProcPipelineParameterBufferType:
3547             vaStatus = I965_RENDER_PROC_BUFFER(pipeline_parameter);
3548             break;
3549
3550         default:
3551             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3552             break;
3553         }
3554     }
3555
3556     return vaStatus;
3557 }
3558
3559 VAStatus 
3560 i965_RenderPicture(VADriverContextP ctx,
3561                    VAContextID context,
3562                    VABufferID *buffers,
3563                    int num_buffers)
3564 {
3565     struct i965_driver_data *i965 = i965_driver_data(ctx);
3566     struct object_context *obj_context;
3567     struct object_config *obj_config;
3568     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3569
3570     obj_context = CONTEXT(context);
3571     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3572
3573     if (num_buffers <= 0)
3574         return VA_STATUS_ERROR_INVALID_PARAMETER;
3575
3576     obj_config = obj_context->obj_config;
3577     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3578
3579     if (VAEntrypointVideoProc == obj_config->entrypoint) {
3580         vaStatus = i965_proc_render_picture(ctx, context, buffers, num_buffers);
3581     } else if ((VAEntrypointEncSlice == obj_config->entrypoint ) || 
3582                (VAEntrypointEncPicture == obj_config->entrypoint) ||
3583                (VAEntrypointEncSliceLP == obj_config->entrypoint)) {
3584         vaStatus = i965_encoder_render_picture(ctx, context, buffers, num_buffers);
3585     } else {
3586         vaStatus = i965_decoder_render_picture(ctx, context, buffers, num_buffers);
3587     }
3588
3589     return vaStatus;
3590 }
3591
3592 VAStatus 
3593 i965_EndPicture(VADriverContextP ctx, VAContextID context)
3594 {
3595     struct i965_driver_data *i965 = i965_driver_data(ctx); 
3596     struct object_context *obj_context = CONTEXT(context);
3597     struct object_config *obj_config;
3598
3599     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3600     obj_config = obj_context->obj_config;
3601     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3602
3603     if (obj_context->codec_type == CODEC_PROC) {
3604         ASSERT_RET(VAEntrypointVideoProc == obj_config->entrypoint, VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT);
3605     } else if (obj_context->codec_type == CODEC_ENC) {
3606         ASSERT_RET(((VAEntrypointEncSlice == obj_config->entrypoint) ||
3607                     (VAEntrypointEncPicture == obj_config->entrypoint) ||
3608                     (VAEntrypointEncSliceLP == obj_config->entrypoint)),
3609                    VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT);
3610
3611         if (obj_context->codec_state.encode.num_packed_header_params_ext !=
3612                obj_context->codec_state.encode.num_packed_header_data_ext) {
3613             WARN_ONCE("the packed header/data is not paired for encoding!\n");
3614             return VA_STATUS_ERROR_INVALID_PARAMETER;
3615         }
3616         if (!obj_context->codec_state.encode.pic_param_ext) {
3617             return VA_STATUS_ERROR_INVALID_PARAMETER;
3618         }
3619         if (!obj_context->codec_state.encode.seq_param_ext &&
3620                 (VAEntrypointEncPicture != obj_config->entrypoint)) {
3621             /* The seq_param is not mandatory for VP9 encoding */
3622             if (obj_config->profile != VAProfileVP9Profile0)
3623                 return VA_STATUS_ERROR_INVALID_PARAMETER;
3624         }
3625         if ((obj_context->codec_state.encode.num_slice_params_ext <=0) &&
3626                 ((obj_config->profile != VAProfileVP8Version0_3) &&
3627                  (obj_config->profile != VAProfileVP9Profile0))) {
3628             return VA_STATUS_ERROR_INVALID_PARAMETER;
3629         }
3630
3631         if ((obj_context->codec_state.encode.packed_header_flag & VA_ENC_PACKED_HEADER_SLICE) &&
3632             (obj_context->codec_state.encode.num_slice_params_ext !=
3633              obj_context->codec_state.encode.slice_index)) {
3634             WARN_ONCE("packed slice_header data is missing for some slice"
3635                       " under packed SLICE_HEADER mode\n");
3636             return VA_STATUS_ERROR_INVALID_PARAMETER;
3637         }
3638     } else {
3639         if (obj_context->codec_state.decode.pic_param == NULL) {
3640             return VA_STATUS_ERROR_INVALID_PARAMETER;
3641         }
3642         if (obj_context->codec_state.decode.num_slice_params <=0) {
3643             return VA_STATUS_ERROR_INVALID_PARAMETER;
3644         }
3645         if (obj_context->codec_state.decode.num_slice_datas <=0) {
3646             return VA_STATUS_ERROR_INVALID_PARAMETER;
3647         }
3648
3649         if (obj_context->codec_state.decode.num_slice_params !=
3650                 obj_context->codec_state.decode.num_slice_datas) {
3651             return VA_STATUS_ERROR_INVALID_PARAMETER;
3652         }
3653
3654         if (obj_context->wrapper_context != VA_INVALID_ID) {
3655             /* call the vaEndPicture of wrapped driver */
3656             VADriverContextP pdrvctx;
3657             VAStatus va_status;
3658
3659             pdrvctx = i965->wrapper_pdrvctx;
3660             CALL_VTABLE(pdrvctx, va_status,
3661                         vaEndPicture(pdrvctx, obj_context->wrapper_context));
3662
3663             return va_status;
3664         }
3665     }
3666
3667     ASSERT_RET(obj_context->hw_context->run, VA_STATUS_ERROR_OPERATION_FAILED);
3668     return obj_context->hw_context->run(ctx, obj_config->profile, &obj_context->codec_state, obj_context->hw_context);
3669 }
3670
3671 VAStatus 
3672 i965_SyncSurface(VADriverContextP ctx,
3673                  VASurfaceID render_target)
3674 {
3675     struct i965_driver_data *i965 = i965_driver_data(ctx); 
3676     struct object_surface *obj_surface = SURFACE(render_target);
3677
3678     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
3679
3680     if(obj_surface->bo)
3681         drm_intel_bo_wait_rendering(obj_surface->bo);
3682
3683     return VA_STATUS_SUCCESS;
3684 }
3685
3686 VAStatus 
3687 i965_QuerySurfaceStatus(VADriverContextP ctx,
3688                         VASurfaceID render_target,
3689                         VASurfaceStatus *status)        /* out */
3690 {
3691     struct i965_driver_data *i965 = i965_driver_data(ctx); 
3692     struct object_surface *obj_surface = SURFACE(render_target);
3693
3694     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
3695
3696     if (obj_surface->bo) {
3697         if (drm_intel_bo_busy(obj_surface->bo)){
3698             *status = VASurfaceRendering;
3699         }
3700         else {
3701             *status = VASurfaceReady;
3702         }
3703     } else {
3704         *status = VASurfaceReady;
3705     }
3706
3707     return VA_STATUS_SUCCESS;
3708 }
3709
3710 static VADisplayAttribute *
3711 get_display_attribute(VADriverContextP ctx, VADisplayAttribType type)
3712 {
3713     struct i965_driver_data * const i965 = i965_driver_data(ctx);
3714     unsigned int i;
3715
3716     if (!i965->display_attributes)
3717         return NULL;
3718
3719     for (i = 0; i < i965->num_display_attributes; i++) {
3720         if (i965->display_attributes[i].type == type)
3721             return &i965->display_attributes[i];
3722     }
3723     return NULL;
3724 }
3725
3726 static void
3727 i965_display_attributes_terminate(VADriverContextP ctx)
3728 {
3729     struct i965_driver_data * const i965 = i965_driver_data(ctx);
3730
3731     if (i965->display_attributes) {
3732         free(i965->display_attributes);
3733         i965->display_attributes = NULL;
3734         i965->num_display_attributes = 0;
3735     }
3736 }
3737
3738 static bool
3739 i965_display_attributes_init(VADriverContextP ctx)
3740 {
3741     struct i965_driver_data * const i965 = i965_driver_data(ctx);
3742
3743     i965->num_display_attributes = ARRAY_ELEMS(i965_display_attributes);
3744     i965->display_attributes = malloc(
3745         i965->num_display_attributes * sizeof(i965->display_attributes[0]));
3746     if (!i965->display_attributes)
3747         goto error;
3748
3749     memcpy(
3750         i965->display_attributes,
3751         i965_display_attributes,
3752         sizeof(i965_display_attributes)
3753     );
3754
3755     i965->rotation_attrib = get_display_attribute(ctx, VADisplayAttribRotation);
3756     i965->brightness_attrib = get_display_attribute(ctx, VADisplayAttribBrightness);
3757     i965->contrast_attrib = get_display_attribute(ctx, VADisplayAttribContrast);
3758     i965->hue_attrib = get_display_attribute(ctx, VADisplayAttribHue);
3759     i965->saturation_attrib = get_display_attribute(ctx, VADisplayAttribSaturation);
3760
3761     if (!i965->rotation_attrib ||
3762         !i965->brightness_attrib ||
3763         !i965->contrast_attrib ||
3764         !i965->hue_attrib ||
3765         !i965->saturation_attrib) {
3766         goto error;
3767     }
3768     return true;
3769
3770 error:
3771     i965_display_attributes_terminate(ctx);
3772     return false;
3773 }
3774
3775 /* 
3776  * Query display attributes 
3777  * The caller must provide a "attr_list" array that can hold at
3778  * least vaMaxNumDisplayAttributes() entries. The actual number of attributes
3779  * returned in "attr_list" is returned in "num_attributes".
3780  */
3781 VAStatus 
3782 i965_QueryDisplayAttributes(
3783     VADriverContextP    ctx,
3784     VADisplayAttribute *attribs,        /* out */
3785     int                *num_attribs_ptr /* out */
3786 )
3787 {
3788     const int num_attribs = ARRAY_ELEMS(i965_display_attributes);
3789
3790     if (attribs && num_attribs > 0)
3791         memcpy(attribs, i965_display_attributes, sizeof(i965_display_attributes));
3792
3793     if (num_attribs_ptr)
3794         *num_attribs_ptr = num_attribs;
3795
3796     return VA_STATUS_SUCCESS;
3797 }
3798
3799 /* 
3800  * Get display attributes 
3801  * This function returns the current attribute values in "attr_list".
3802  * Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
3803  * from vaQueryDisplayAttributes() can have their values retrieved.  
3804  */
3805 VAStatus 
3806 i965_GetDisplayAttributes(
3807     VADriverContextP    ctx,
3808     VADisplayAttribute *attribs,        /* inout */
3809     int                 num_attribs     /* in */
3810 )
3811 {
3812     int i;
3813
3814     for (i = 0; i < num_attribs; i++) {
3815         VADisplayAttribute *src_attrib, * const dst_attrib = &attribs[i];
3816
3817         src_attrib = get_display_attribute(ctx, dst_attrib->type);
3818
3819         if (src_attrib && (src_attrib->flags & VA_DISPLAY_ATTRIB_GETTABLE)) {
3820             dst_attrib->min_value = src_attrib->min_value;
3821             dst_attrib->max_value = src_attrib->max_value;
3822             dst_attrib->value     = src_attrib->value;
3823             dst_attrib->flags     = src_attrib->flags;
3824         } else if (src_attrib &&
3825                 (src_attrib->flags & VA_DISPLAY_ATTRIB_SETTABLE)) {
3826             dst_attrib->flags     = src_attrib->flags;
3827         }
3828         else
3829             dst_attrib->flags = VA_DISPLAY_ATTRIB_NOT_SUPPORTED;
3830     }
3831     return VA_STATUS_SUCCESS;
3832 }
3833
3834 /* 
3835  * Set display attributes 
3836  * Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
3837  * from vaQueryDisplayAttributes() can be set.  If the attribute is not settable or 
3838  * the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
3839  */
3840 VAStatus 
3841 i965_SetDisplayAttributes(
3842     VADriverContextP    ctx,
3843     VADisplayAttribute *attribs,        /* in */
3844     int                 num_attribs     /* in */
3845 )
3846 {
3847     int i;
3848
3849     for (i = 0; i < num_attribs; i++) {
3850         VADisplayAttribute *dst_attrib, * const src_attrib = &attribs[i];
3851
3852         dst_attrib = get_display_attribute(ctx, src_attrib->type);
3853         if (!dst_attrib)
3854             return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
3855
3856         if (!(dst_attrib->flags & VA_DISPLAY_ATTRIB_SETTABLE))
3857             continue;
3858
3859         if (src_attrib->value < dst_attrib->min_value ||
3860             src_attrib->value > dst_attrib->max_value)
3861             return VA_STATUS_ERROR_INVALID_PARAMETER;
3862
3863         dst_attrib->value = src_attrib->value;
3864         /* XXX: track modified attributes through timestamps */
3865     }
3866     return VA_STATUS_SUCCESS;
3867 }
3868
3869 VAStatus 
3870 i965_DbgCopySurfaceToBuffer(VADriverContextP ctx,
3871                             VASurfaceID surface,
3872                             void **buffer,              /* out */
3873                             unsigned int *stride)       /* out */
3874 {
3875     /* TODO */
3876     return VA_STATUS_ERROR_UNIMPLEMENTED;
3877 }
3878
3879 static void
3880 i965_destroy_heap(struct object_heap *heap, 
3881                   void (*func)(struct object_heap *heap, struct object_base *object))
3882 {
3883     struct object_base *object;
3884     object_heap_iterator iter;    
3885
3886     object = object_heap_first(heap, &iter);
3887
3888     while (object) {
3889         if (func)
3890             func(heap, object);
3891
3892         object = object_heap_next(heap, &iter);
3893     }
3894
3895     object_heap_destroy(heap);
3896 }
3897
3898
3899 VAStatus 
3900 i965_DestroyImage(VADriverContextP ctx, VAImageID image);
3901
3902 VAStatus 
3903 i965_CreateImage(VADriverContextP ctx,
3904                  VAImageFormat *format,
3905                  int width,
3906                  int height,
3907                  VAImage *out_image)        /* out */
3908 {
3909     struct i965_driver_data *i965 = i965_driver_data(ctx);
3910     struct object_image *obj_image;
3911     VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
3912     VAImageID image_id;
3913     unsigned int size2, size, awidth, aheight;
3914
3915     out_image->image_id = VA_INVALID_ID;
3916     out_image->buf      = VA_INVALID_ID;
3917
3918     image_id = NEW_IMAGE_ID();
3919     if (image_id == VA_INVALID_ID)
3920         return VA_STATUS_ERROR_ALLOCATION_FAILED;
3921
3922     obj_image = IMAGE(image_id);
3923     if (!obj_image)
3924         return VA_STATUS_ERROR_ALLOCATION_FAILED;
3925     obj_image->bo         = NULL;
3926     obj_image->palette    = NULL;
3927     obj_image->derived_surface = VA_INVALID_ID;
3928
3929     VAImage * const image = &obj_image->image;
3930     image->image_id       = image_id;
3931     image->buf            = VA_INVALID_ID;
3932
3933     awidth = ALIGN(width, i965->codec_info->min_linear_wpitch);
3934
3935     if ((format->fourcc == VA_FOURCC_YV12) ||
3936         (format->fourcc == VA_FOURCC_I420)) {
3937             if (awidth % 128 != 0) {
3938                 awidth = ALIGN(width, 128);     
3939             }
3940     }
3941
3942     aheight = ALIGN(height, i965->codec_info->min_linear_hpitch);
3943     size    = awidth * aheight;
3944     size2    = (awidth / 2) * (aheight / 2);
3945
3946     image->num_palette_entries = 0;
3947     image->entry_bytes         = 0;
3948     memset(image->component_order, 0, sizeof(image->component_order));
3949
3950     switch (format->fourcc) {
3951     case VA_FOURCC_IA44:
3952     case VA_FOURCC_AI44:
3953         image->num_planes = 1;
3954         image->pitches[0] = awidth;
3955         image->offsets[0] = 0;
3956         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
3957         image->num_palette_entries = 16;
3958         image->entry_bytes         = 3;
3959         image->component_order[0]  = 'R';
3960         image->component_order[1]  = 'G';
3961         image->component_order[2]  = 'B';
3962         break;
3963     case VA_FOURCC_IA88:
3964     case VA_FOURCC_AI88:
3965         image->num_planes = 1;
3966         image->pitches[0] = awidth * 2;
3967         image->offsets[0] = 0;
3968         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
3969         image->num_palette_entries = 256;
3970         image->entry_bytes         = 3;
3971         image->component_order[0]  = 'R';
3972         image->component_order[1]  = 'G';
3973         image->component_order[2]  = 'B';
3974         break;
3975     case VA_FOURCC_ARGB:
3976     case VA_FOURCC_ABGR:
3977     case VA_FOURCC_BGRA:
3978     case VA_FOURCC_RGBA:
3979     case VA_FOURCC_BGRX:
3980     case VA_FOURCC_RGBX:
3981         image->num_planes = 1;
3982         image->pitches[0] = awidth * 4;
3983         image->offsets[0] = 0;
3984         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
3985         break;
3986     case VA_FOURCC_YV12:
3987         image->num_planes = 3;
3988         image->pitches[0] = awidth;
3989         image->offsets[0] = 0;
3990         image->pitches[1] = awidth / 2;
3991         image->offsets[1] = size;
3992         image->pitches[2] = awidth / 2;
3993         image->offsets[2] = size + size2;
3994         image->data_size  = size + 2 * size2;
3995         break;
3996     case VA_FOURCC_I420:
3997         image->num_planes = 3;
3998         image->pitches[0] = awidth;
3999         image->offsets[0] = 0;
4000         image->pitches[1] = awidth / 2;
4001         image->offsets[1] = size;
4002         image->pitches[2] = awidth / 2;
4003         image->offsets[2] = size + size2;
4004         image->data_size  = size + 2 * size2;
4005         break;
4006     case VA_FOURCC_422H:
4007         image->num_planes = 3;
4008         image->pitches[0] = awidth;
4009         image->offsets[0] = 0;
4010         image->pitches[1] = awidth / 2;
4011         image->offsets[1] = size;
4012         image->pitches[2] = awidth / 2;
4013         image->offsets[2] = size + (awidth / 2) * aheight;
4014         image->data_size  = size + 2 * ((awidth / 2) * aheight);
4015         break;
4016     case VA_FOURCC_NV12:
4017         image->num_planes = 2;
4018         image->pitches[0] = awidth;
4019         image->offsets[0] = 0;
4020         image->pitches[1] = awidth;
4021         image->offsets[1] = size;
4022         image->data_size  = size + 2 * size2;
4023         break;
4024     case VA_FOURCC_YUY2:
4025     case VA_FOURCC_UYVY:
4026         image->num_planes = 1;
4027         image->pitches[0] = awidth * 2;
4028         image->offsets[0] = 0;
4029         image->data_size  = size * 2;
4030         break;
4031     case VA_FOURCC_P010:
4032         image->num_planes = 2;
4033         image->pitches[0] = awidth * 2;
4034         image->offsets[0] = 0;
4035         image->pitches[1] = awidth * 2;
4036         image->offsets[1] = size * 2;
4037         image->data_size  = size * 2 + 2 * size2 * 2;
4038         break;
4039     default:
4040         goto error;
4041     }
4042
4043     va_status = i965_CreateBuffer(ctx, 0, VAImageBufferType,
4044                                   image->data_size, 1, NULL, &image->buf);
4045     if (va_status != VA_STATUS_SUCCESS)
4046         goto error;
4047
4048     struct object_buffer *obj_buffer = BUFFER(image->buf);
4049
4050     if (!obj_buffer ||
4051         !obj_buffer->buffer_store ||
4052         !obj_buffer->buffer_store->bo)
4053         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4054
4055     obj_image->bo = obj_buffer->buffer_store->bo;
4056     dri_bo_reference(obj_image->bo);
4057
4058     if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
4059         obj_image->palette = malloc(image->num_palette_entries * sizeof(*obj_image->palette));
4060         if (!obj_image->palette)
4061             goto error;
4062     }
4063
4064     image->image_id             = image_id;
4065     image->format               = *format;
4066     image->width                = width;
4067     image->height               = height;
4068
4069     *out_image                  = *image;
4070     return VA_STATUS_SUCCESS;
4071
4072  error:
4073     i965_DestroyImage(ctx, image_id);
4074     return va_status;
4075 }
4076
4077 VAStatus
4078 i965_check_alloc_surface_bo(VADriverContextP ctx,
4079                             struct object_surface *obj_surface,
4080                             int tiled,
4081                             unsigned int fourcc,
4082                             unsigned int subsampling)
4083 {
4084     struct i965_driver_data *i965 = i965_driver_data(ctx);
4085     int region_width, region_height;
4086
4087     if (obj_surface->bo) {
4088         ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4089         ASSERT_RET(obj_surface->fourcc == fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4090         ASSERT_RET(obj_surface->subsampling == subsampling, VA_STATUS_ERROR_INVALID_SURFACE);
4091         return VA_STATUS_SUCCESS;
4092     }
4093
4094     obj_surface->x_cb_offset = 0; /* X offset is always 0 */
4095     obj_surface->x_cr_offset = 0;
4096
4097     int bpp_1stplane = bpp_1stplane_by_fourcc(fourcc);
4098
4099     if ((tiled && !obj_surface->user_disable_tiling)) {
4100         ASSERT_RET(fourcc != VA_FOURCC_I420 &&
4101                fourcc != VA_FOURCC_IYUV &&
4102                fourcc != VA_FOURCC_YV12,
4103                VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4104
4105         if (obj_surface->user_h_stride_set) {
4106            ASSERT_RET(IS_ALIGNED(obj_surface->width, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
4107         } else
4108           obj_surface->width = ALIGN(obj_surface->orig_width * bpp_1stplane, 128);
4109
4110         if (obj_surface->user_v_stride_set) {
4111           ASSERT_RET(IS_ALIGNED(obj_surface->height, 32), VA_STATUS_ERROR_INVALID_PARAMETER);
4112         }else
4113           obj_surface->height = ALIGN(obj_surface->orig_height, 32);
4114
4115         region_height = obj_surface->height;
4116
4117         switch (fourcc) {
4118         case VA_FOURCC_NV12:
4119         case VA_FOURCC_P010:
4120             assert(subsampling == SUBSAMPLE_YUV420);
4121             obj_surface->cb_cr_pitch = obj_surface->width;
4122             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4123             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4124             obj_surface->y_cb_offset = obj_surface->height;
4125             obj_surface->y_cr_offset = obj_surface->height;
4126             region_width = obj_surface->width;
4127             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32);
4128             
4129             break;
4130
4131         case VA_FOURCC_IMC1:
4132             assert(subsampling == SUBSAMPLE_YUV420);
4133             obj_surface->cb_cr_pitch = obj_surface->width;
4134             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4135             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4136             obj_surface->y_cr_offset = obj_surface->height;
4137             obj_surface->y_cb_offset = obj_surface->y_cr_offset + ALIGN(obj_surface->cb_cr_height, 32);
4138             region_width = obj_surface->width;
4139             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4140
4141             break;
4142
4143         case VA_FOURCC_IMC3:
4144             assert(subsampling == SUBSAMPLE_YUV420);
4145             obj_surface->cb_cr_pitch = obj_surface->width;
4146             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4147             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4148             obj_surface->y_cb_offset = obj_surface->height;
4149             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4150             region_width = obj_surface->width;
4151             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4152             
4153             break;
4154
4155         case VA_FOURCC_422H:
4156             assert(subsampling == SUBSAMPLE_YUV422H);
4157             obj_surface->cb_cr_pitch = obj_surface->width;
4158             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4159             obj_surface->cb_cr_height = obj_surface->orig_height;
4160             obj_surface->y_cb_offset = obj_surface->height;
4161             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4162             region_width = obj_surface->width;
4163             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4164
4165             break;
4166
4167         case VA_FOURCC_422V:
4168             assert(subsampling == SUBSAMPLE_YUV422V);
4169             obj_surface->cb_cr_pitch = obj_surface->width;
4170             obj_surface->cb_cr_width = obj_surface->orig_width;
4171             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4172             obj_surface->y_cb_offset = obj_surface->height;
4173             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4174             region_width = obj_surface->width;
4175             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4176
4177             break;
4178
4179         case VA_FOURCC_411P:
4180             assert(subsampling == SUBSAMPLE_YUV411);
4181             obj_surface->cb_cr_pitch = obj_surface->width;
4182             obj_surface->cb_cr_width = obj_surface->orig_width / 4;
4183             obj_surface->cb_cr_height = obj_surface->orig_height;
4184             obj_surface->y_cb_offset = obj_surface->height;
4185             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4186             region_width = obj_surface->width;
4187             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4188
4189             break;
4190
4191         case VA_FOURCC_444P:
4192             assert(subsampling == SUBSAMPLE_YUV444);
4193             obj_surface->cb_cr_pitch = obj_surface->width;
4194             obj_surface->cb_cr_width = obj_surface->orig_width;
4195             obj_surface->cb_cr_height = obj_surface->orig_height;
4196             obj_surface->y_cb_offset = obj_surface->height;
4197             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4198             region_width = obj_surface->width;
4199             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4200
4201             break;
4202
4203         case VA_FOURCC_Y800:
4204             assert(subsampling == SUBSAMPLE_YUV400);
4205             obj_surface->cb_cr_pitch = 0;
4206             obj_surface->cb_cr_width = 0;
4207             obj_surface->cb_cr_height = 0;
4208             obj_surface->y_cb_offset = 0;
4209             obj_surface->y_cr_offset = 0;
4210             region_width = obj_surface->width;
4211             region_height = obj_surface->height;
4212
4213             break;
4214
4215         case VA_FOURCC_YUY2:
4216         case VA_FOURCC_UYVY:
4217             assert(subsampling == SUBSAMPLE_YUV422H);
4218             obj_surface->width = ALIGN(obj_surface->orig_width * 2, 128);
4219             obj_surface->cb_cr_pitch = obj_surface->width;
4220             obj_surface->y_cb_offset = 0; 
4221             obj_surface->y_cr_offset = 0; 
4222             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4223             obj_surface->cb_cr_height = obj_surface->orig_height;
4224             region_width = obj_surface->width;
4225             region_height = obj_surface->height;
4226             
4227             break;
4228
4229         case VA_FOURCC_RGBA:
4230         case VA_FOURCC_RGBX:
4231         case VA_FOURCC_BGRA:
4232         case VA_FOURCC_BGRX:
4233             assert(subsampling == SUBSAMPLE_RGBX);
4234
4235             obj_surface->width = ALIGN(obj_surface->orig_width * 4, 128);
4236             region_width = obj_surface->width;
4237             region_height = obj_surface->height;
4238             break;
4239
4240         default:
4241             /* Never get here */
4242             ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4243             break;
4244         }
4245     } else {
4246         assert(subsampling == SUBSAMPLE_YUV420 || 
4247                subsampling == SUBSAMPLE_YUV422H || 
4248                subsampling == SUBSAMPLE_YUV422V ||
4249                subsampling == SUBSAMPLE_RGBX);
4250
4251         region_width = obj_surface->width;
4252         region_height = obj_surface->height;
4253
4254         switch (fourcc) {
4255         case VA_FOURCC_NV12:
4256         case VA_FOURCC_P010:
4257             obj_surface->y_cb_offset = obj_surface->height;
4258             obj_surface->y_cr_offset = obj_surface->height;
4259             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4260             obj_surface->width = ALIGN(obj_surface->cb_cr_width * 2, i965->codec_info->min_linear_wpitch) *
4261                                  bpp_1stplane;
4262             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4263             obj_surface->cb_cr_pitch = obj_surface->width;
4264             region_width = obj_surface->width;
4265             region_height = obj_surface->height + obj_surface->height / 2;
4266             break;
4267
4268         case VA_FOURCC_YV16:
4269             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4270             obj_surface->width = ALIGN(obj_surface->cb_cr_width, i965->codec_info->min_linear_wpitch) * 2;
4271             obj_surface->cb_cr_height = obj_surface->orig_height;
4272             obj_surface->y_cr_offset = obj_surface->height;
4273             obj_surface->y_cb_offset = obj_surface->y_cr_offset + ALIGN(obj_surface->cb_cr_height, 32) / 2;
4274             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4275             region_width = obj_surface->width;
4276             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32);
4277             break;
4278
4279         case VA_FOURCC_YV12:
4280         case VA_FOURCC_I420:
4281         case VA_FOURCC_IYUV:
4282             if (fourcc == VA_FOURCC_YV12) {
4283                 obj_surface->y_cr_offset = obj_surface->height;
4284                 obj_surface->y_cb_offset = obj_surface->height + obj_surface->height / 4;
4285             } else {
4286                 obj_surface->y_cb_offset = obj_surface->height;
4287                 obj_surface->y_cr_offset = obj_surface->height + obj_surface->height / 4;
4288             }
4289
4290             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4291             obj_surface->width = ALIGN(obj_surface->cb_cr_width, i965->codec_info->min_linear_wpitch) * 2;
4292             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4293             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4294             region_width = obj_surface->width;
4295             region_height = obj_surface->height + obj_surface->height / 2;
4296             break;
4297
4298         case VA_FOURCC_I010:
4299             obj_surface->y_cb_offset = obj_surface->height;
4300             obj_surface->y_cr_offset = obj_surface->height + obj_surface->height / 4;
4301             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4302             obj_surface->width = ALIGN(obj_surface->cb_cr_width * 2, i965->codec_info->min_linear_wpitch) * 2;
4303             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4304             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4305             region_width = obj_surface->width;
4306             region_height = obj_surface->height + obj_surface->height / 2;
4307
4308             break;
4309         case VA_FOURCC_YUY2:
4310         case VA_FOURCC_UYVY:
4311             obj_surface->width = ALIGN(obj_surface->orig_width * 2, i965->codec_info->min_linear_wpitch);
4312             obj_surface->y_cb_offset = 0;
4313             obj_surface->y_cr_offset = 0;
4314             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4315             obj_surface->cb_cr_height = obj_surface->orig_height;
4316             obj_surface->cb_cr_pitch = obj_surface->width;
4317             region_width = obj_surface->width;
4318             region_height = obj_surface->height;
4319             break;
4320         case VA_FOURCC_RGBA:
4321         case VA_FOURCC_RGBX:
4322         case VA_FOURCC_BGRA:
4323         case VA_FOURCC_BGRX:
4324             obj_surface->width = ALIGN(obj_surface->orig_width * 4, i965->codec_info->min_linear_wpitch);
4325             region_width = obj_surface->width;
4326             region_height = obj_surface->height;
4327             break;
4328
4329         default:
4330             /* Never get here */
4331             ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4332             break;
4333         }
4334     }
4335
4336     obj_surface->size = ALIGN(region_width * region_height, 0x1000);
4337
4338     if ((tiled && !obj_surface->user_disable_tiling)) {
4339         uint32_t tiling_mode = I915_TILING_Y; /* always uses Y-tiled format */
4340         unsigned long pitch;
4341
4342         obj_surface->bo = drm_intel_bo_alloc_tiled(i965->intel.bufmgr, 
4343                                                    "vaapi surface",
4344                                                    region_width,
4345                                                    region_height,
4346                                                    1,
4347                                                    &tiling_mode,
4348                                                    &pitch,
4349                                                    0);
4350         assert(tiling_mode == I915_TILING_Y);
4351         assert(pitch == obj_surface->width);
4352     } else {
4353         obj_surface->bo = dri_bo_alloc(i965->intel.bufmgr,
4354                                        "vaapi surface",
4355                                        obj_surface->size,
4356                                        0x1000);
4357     }
4358
4359     obj_surface->fourcc = fourcc;
4360     obj_surface->subsampling = subsampling;
4361     assert(obj_surface->bo);
4362     return VA_STATUS_SUCCESS;
4363 }
4364
4365 VAStatus i965_DeriveImage(VADriverContextP ctx,
4366                           VASurfaceID surface,
4367                           VAImage *out_image)        /* out */
4368 {
4369     struct i965_driver_data *i965 = i965_driver_data(ctx);
4370     struct object_image *obj_image;
4371     struct object_surface *obj_surface; 
4372     VAImageID image_id;
4373     unsigned int w_pitch;
4374     VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
4375
4376     out_image->image_id = VA_INVALID_ID;
4377     obj_surface = SURFACE(surface);
4378
4379     if (!obj_surface)
4380         return VA_STATUS_ERROR_INVALID_SURFACE;
4381
4382     if (!obj_surface->bo) {
4383         unsigned int is_tiled = 0;
4384         unsigned int fourcc = VA_FOURCC_YV12;
4385         i965_guess_surface_format(ctx, surface, &fourcc, &is_tiled);
4386         int sampling = get_sampling_from_fourcc(fourcc);
4387         va_status = i965_check_alloc_surface_bo(ctx, obj_surface, is_tiled, fourcc, sampling);
4388         if (va_status != VA_STATUS_SUCCESS)
4389             return va_status;
4390     }
4391
4392     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4393
4394     w_pitch = obj_surface->width;
4395
4396     image_id = NEW_IMAGE_ID();
4397
4398     if (image_id == VA_INVALID_ID)
4399         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4400
4401     obj_image = IMAGE(image_id);
4402     
4403     if (!obj_image)
4404         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4405
4406     obj_image->bo = NULL;
4407     obj_image->palette = NULL;
4408     obj_image->derived_surface = VA_INVALID_ID;
4409
4410     VAImage * const image = &obj_image->image;
4411     
4412     memset(image, 0, sizeof(*image));
4413     image->image_id = image_id;
4414     image->buf = VA_INVALID_ID;
4415     image->num_palette_entries = 0;
4416     image->entry_bytes = 0;
4417     image->width = obj_surface->orig_width;
4418     image->height = obj_surface->orig_height;
4419     image->data_size = obj_surface->size;
4420
4421     image->format.fourcc = obj_surface->fourcc;
4422     image->format.byte_order = VA_LSB_FIRST;
4423     image->format.bits_per_pixel = get_bpp_from_fourcc(obj_surface->fourcc);
4424
4425     if (!image->format.bits_per_pixel)
4426         goto error;
4427
4428     switch (image->format.fourcc) {
4429     case VA_FOURCC_YV12:
4430         image->num_planes = 3;
4431         image->pitches[0] = w_pitch; /* Y */
4432         image->offsets[0] = 0;
4433         image->pitches[1] = obj_surface->cb_cr_pitch; /* V */
4434         image->offsets[1] = w_pitch * obj_surface->y_cr_offset;
4435         image->pitches[2] = obj_surface->cb_cr_pitch; /* U */
4436         image->offsets[2] = w_pitch * obj_surface->y_cb_offset;
4437         break;
4438
4439     case VA_FOURCC_YV16:
4440         image->num_planes = 3;
4441         image->pitches[0] = w_pitch; /* Y */
4442         image->offsets[0] = 0;
4443         image->pitches[1] = obj_surface->cb_cr_pitch; /* V */
4444         image->offsets[1] = w_pitch * obj_surface->y_cr_offset;
4445         image->pitches[2] = obj_surface->cb_cr_pitch; /* U */
4446         image->offsets[2] = w_pitch * obj_surface->y_cb_offset;
4447         break;
4448
4449     case VA_FOURCC_NV12:
4450     case VA_FOURCC_P010:
4451         image->num_planes = 2;
4452         image->pitches[0] = w_pitch; /* Y */
4453         image->offsets[0] = 0;
4454         image->pitches[1] = obj_surface->cb_cr_pitch; /* UV */
4455         image->offsets[1] = w_pitch * obj_surface->y_cb_offset;
4456         break;
4457
4458     case VA_FOURCC_I420:
4459     case VA_FOURCC_I010:
4460     case VA_FOURCC_422H:
4461     case VA_FOURCC_IMC3:
4462     case VA_FOURCC_444P:
4463     case VA_FOURCC_422V:
4464     case VA_FOURCC_411P:
4465         image->num_planes = 3;
4466         image->pitches[0] = w_pitch; /* Y */
4467         image->offsets[0] = 0;
4468         image->pitches[1] = obj_surface->cb_cr_pitch; /* U */
4469         image->offsets[1] = w_pitch * obj_surface->y_cb_offset;
4470         image->pitches[2] = obj_surface->cb_cr_pitch; /* V */
4471         image->offsets[2] = w_pitch * obj_surface->y_cr_offset;
4472         break;
4473
4474     case VA_FOURCC_YUY2:
4475     case VA_FOURCC_UYVY:
4476     case VA_FOURCC_Y800:
4477         image->num_planes = 1;
4478         image->pitches[0] = obj_surface->width; /* Y, width is aligned already */
4479         image->offsets[0] = 0;
4480         break;
4481     case VA_FOURCC_RGBA:
4482     case VA_FOURCC_RGBX:
4483     case VA_FOURCC_BGRA:
4484     case VA_FOURCC_BGRX:
4485         image->num_planes = 1;
4486         image->pitches[0] = obj_surface->width;
4487
4488         switch (image->format.fourcc) {
4489         case VA_FOURCC_RGBA:
4490         case VA_FOURCC_RGBX:
4491             image->format.red_mask = 0x000000ff;
4492             image->format.green_mask = 0x0000ff00;
4493             image->format.blue_mask = 0x00ff0000;
4494             break;
4495         case VA_FOURCC_BGRA:
4496         case VA_FOURCC_BGRX:
4497             image->format.red_mask = 0x00ff0000;
4498             image->format.green_mask = 0x0000ff00;
4499             image->format.blue_mask = 0x000000ff;
4500             break;
4501         default:
4502             goto error;
4503         }
4504
4505         switch (image->format.fourcc) {
4506         case VA_FOURCC_RGBA:
4507         case VA_FOURCC_BGRA:
4508             image->format.alpha_mask = 0xff000000;
4509             image->format.depth = 32;
4510             break;
4511         case VA_FOURCC_RGBX:
4512         case VA_FOURCC_BGRX:
4513             image->format.alpha_mask = 0x00000000;
4514             image->format.depth = 24;
4515             break;
4516         default:
4517             goto error;
4518         }
4519
4520         break;
4521     default:
4522         goto error;
4523     }
4524
4525     va_status = i965_create_buffer_internal(ctx, 0, VAImageBufferType,
4526                                             obj_surface->size, 1, NULL, obj_surface->bo, &image->buf);
4527     if (va_status != VA_STATUS_SUCCESS)
4528         goto error;
4529
4530     struct object_buffer *obj_buffer = BUFFER(image->buf);
4531
4532     if (!obj_buffer ||
4533         !obj_buffer->buffer_store ||
4534         !obj_buffer->buffer_store->bo)
4535         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4536
4537     obj_image->bo = obj_buffer->buffer_store->bo;
4538     dri_bo_reference(obj_image->bo);
4539
4540     if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
4541         obj_image->palette = malloc(image->num_palette_entries * sizeof(*obj_image->palette));
4542         if (!obj_image->palette) {
4543             va_status = VA_STATUS_ERROR_ALLOCATION_FAILED;
4544             goto error;
4545         }
4546     }
4547
4548     *out_image = *image;
4549     obj_surface->flags |= SURFACE_DERIVED;
4550     obj_surface->derived_image_id = image_id;
4551     obj_image->derived_surface = surface;
4552
4553     return VA_STATUS_SUCCESS;
4554
4555  error:
4556     i965_DestroyImage(ctx, image_id);
4557     return va_status;
4558 }
4559
4560 static void 
4561 i965_destroy_image(struct object_heap *heap, struct object_base *obj)
4562 {
4563     object_heap_free(heap, obj);
4564 }
4565
4566
4567 VAStatus 
4568 i965_DestroyImage(VADriverContextP ctx, VAImageID image)
4569 {
4570     struct i965_driver_data *i965 = i965_driver_data(ctx);
4571     struct object_image *obj_image = IMAGE(image); 
4572     struct object_surface *obj_surface; 
4573
4574     if (!obj_image)
4575         return VA_STATUS_SUCCESS;
4576
4577     dri_bo_unreference(obj_image->bo);
4578     obj_image->bo = NULL;
4579
4580     if (obj_image->image.buf != VA_INVALID_ID) {
4581         i965_DestroyBuffer(ctx, obj_image->image.buf);
4582         obj_image->image.buf = VA_INVALID_ID;
4583     }
4584
4585     if (obj_image->palette) {
4586         free(obj_image->palette);
4587         obj_image->palette = NULL;
4588     }
4589
4590     obj_surface = SURFACE(obj_image->derived_surface);
4591
4592     if (obj_surface) {
4593         obj_surface->flags &= ~SURFACE_DERIVED;
4594         obj_surface->derived_image_id = VA_INVALID_ID;
4595     }
4596
4597     i965_destroy_image(&i965->image_heap, (struct object_base *)obj_image);
4598
4599     return VA_STATUS_SUCCESS;
4600 }
4601
4602 /*
4603  * pointer to an array holding the palette data.  The size of the array is
4604  * num_palette_entries * entry_bytes in size.  The order of the components
4605  * in the palette is described by the component_order in VASubpicture struct
4606  */
4607 VAStatus 
4608 i965_SetImagePalette(VADriverContextP ctx,
4609                      VAImageID image,
4610                      unsigned char *palette)
4611 {
4612     struct i965_driver_data *i965 = i965_driver_data(ctx);
4613     unsigned int i;
4614
4615     struct object_image *obj_image = IMAGE(image);
4616     if (!obj_image)
4617         return VA_STATUS_ERROR_INVALID_IMAGE;
4618
4619     if (!obj_image->palette)
4620         return VA_STATUS_ERROR_ALLOCATION_FAILED; /* XXX: unpaletted/error */
4621
4622     for (i = 0; i < obj_image->image.num_palette_entries; i++)
4623         obj_image->palette[i] = (((unsigned int)palette[3*i + 0] << 16) |
4624                                  ((unsigned int)palette[3*i + 1] <<  8) |
4625                                  (unsigned int)palette[3*i + 2]);
4626     return VA_STATUS_SUCCESS;
4627 }
4628
4629 static int 
4630 get_sampling_from_fourcc(unsigned int fourcc)
4631 {
4632     const i965_fourcc_info *info = get_fourcc_info(fourcc);
4633
4634     if (info && (info->flag & I_S))
4635         return info->subsampling;
4636     else
4637         return -1;
4638 }
4639
4640 static inline void
4641 memcpy_pic(uint8_t *dst, unsigned int dst_stride,
4642            const uint8_t *src, unsigned int src_stride,
4643            unsigned int len, unsigned int height)
4644 {
4645     unsigned int i;
4646
4647     for (i = 0; i < height; i++) {
4648         memcpy(dst, src, len);
4649         dst += dst_stride;
4650         src += src_stride;
4651     }
4652 }
4653
4654 static VAStatus
4655 get_image_i420(struct object_image *obj_image, uint8_t *image_data,
4656                struct object_surface *obj_surface,
4657                const VARectangle *rect)
4658 {
4659     uint8_t *dst[3], *src[3];
4660     const int Y = 0;
4661     const int U = obj_image->image.format.fourcc == obj_surface->fourcc ? 1 : 2;
4662     const int V = obj_image->image.format.fourcc == obj_surface->fourcc ? 2 : 1;
4663     unsigned int tiling, swizzle;
4664     VAStatus va_status = VA_STATUS_SUCCESS;
4665
4666     if (!obj_surface->bo)
4667         return VA_STATUS_ERROR_INVALID_SURFACE;
4668
4669     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4670     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
4671
4672     if (tiling != I915_TILING_NONE)
4673         drm_intel_gem_bo_map_gtt(obj_surface->bo);
4674     else
4675         dri_bo_map(obj_surface->bo, 0);
4676
4677     if (!obj_surface->bo->virtual)
4678         return VA_STATUS_ERROR_INVALID_SURFACE;
4679
4680     /* Dest VA image has either I420 or YV12 format.
4681        Source VA surface alway has I420 format */
4682     dst[Y] = image_data + obj_image->image.offsets[Y];
4683     src[0] = (uint8_t *)obj_surface->bo->virtual;
4684     dst[U] = image_data + obj_image->image.offsets[U];
4685     src[1] = src[0] + obj_surface->width * obj_surface->height;
4686     dst[V] = image_data + obj_image->image.offsets[V];
4687     src[2] = src[1] + (obj_surface->width / 2) * (obj_surface->height / 2);
4688
4689     /* Y plane */
4690     dst[Y] += rect->y * obj_image->image.pitches[Y] + rect->x;
4691     src[0] += rect->y * obj_surface->width + rect->x;
4692     memcpy_pic(dst[Y], obj_image->image.pitches[Y],
4693                src[0], obj_surface->width,
4694                rect->width, rect->height);
4695
4696     /* U plane */
4697     dst[U] += (rect->y / 2) * obj_image->image.pitches[U] + rect->x / 2;
4698     src[1] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
4699     memcpy_pic(dst[U], obj_image->image.pitches[U],
4700                src[1], obj_surface->width / 2,
4701                rect->width / 2, rect->height / 2);
4702
4703     /* V plane */
4704     dst[V] += (rect->y / 2) * obj_image->image.pitches[V] + rect->x / 2;
4705     src[2] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
4706     memcpy_pic(dst[V], obj_image->image.pitches[V],
4707                src[2], obj_surface->width / 2,
4708                rect->width / 2, rect->height / 2);
4709
4710     if (tiling != I915_TILING_NONE)
4711         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
4712     else
4713         dri_bo_unmap(obj_surface->bo);
4714
4715     return va_status;
4716 }
4717
4718 static VAStatus
4719 get_image_nv12(struct object_image *obj_image, uint8_t *image_data,
4720                struct object_surface *obj_surface,
4721                const VARectangle *rect)
4722 {
4723     uint8_t *dst[2], *src[2];
4724     unsigned int tiling, swizzle;
4725     VAStatus va_status = VA_STATUS_SUCCESS;
4726
4727     if (!obj_surface->bo)
4728         return VA_STATUS_ERROR_INVALID_SURFACE;
4729
4730     assert(obj_surface->fourcc);
4731     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
4732
4733     if (tiling != I915_TILING_NONE)
4734         drm_intel_gem_bo_map_gtt(obj_surface->bo);
4735     else
4736         dri_bo_map(obj_surface->bo, 0);
4737
4738     if (!obj_surface->bo->virtual)
4739         return VA_STATUS_ERROR_INVALID_SURFACE;
4740
4741     /* Both dest VA image and source surface have NV12 format */
4742     dst[0] = image_data + obj_image->image.offsets[0];
4743     src[0] = (uint8_t *)obj_surface->bo->virtual;
4744     dst[1] = image_data + obj_image->image.offsets[1];
4745     src[1] = src[0] + obj_surface->width * obj_surface->height;
4746
4747     /* Y plane */
4748     dst[0] += rect->y * obj_image->image.pitches[0] + rect->x;
4749     src[0] += rect->y * obj_surface->width + rect->x;
4750     memcpy_pic(dst[0], obj_image->image.pitches[0],
4751                src[0], obj_surface->width,
4752                rect->width, rect->height);
4753
4754     /* UV plane */
4755     dst[1] += (rect->y / 2) * obj_image->image.pitches[1] + (rect->x & -2);
4756     src[1] += (rect->y / 2) * obj_surface->width + (rect->x & -2);
4757     memcpy_pic(dst[1], obj_image->image.pitches[1],
4758                src[1], obj_surface->width,
4759                rect->width, rect->height / 2);
4760
4761     if (tiling != I915_TILING_NONE)
4762         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
4763     else
4764         dri_bo_unmap(obj_surface->bo);
4765
4766     return va_status;
4767 }
4768
4769 static VAStatus
4770 get_image_yuy2(struct object_image *obj_image, uint8_t *image_data,
4771                struct object_surface *obj_surface,
4772                const VARectangle *rect)
4773 {
4774     uint8_t *dst, *src;
4775     unsigned int tiling, swizzle;
4776     VAStatus va_status = VA_STATUS_SUCCESS;
4777
4778     if (!obj_surface->bo)
4779         return VA_STATUS_ERROR_INVALID_SURFACE;
4780
4781     assert(obj_surface->fourcc);
4782     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
4783
4784     if (tiling != I915_TILING_NONE)
4785         drm_intel_gem_bo_map_gtt(obj_surface->bo);
4786     else
4787         dri_bo_map(obj_surface->bo, 0);
4788
4789     if (!obj_surface->bo->virtual)
4790         return VA_STATUS_ERROR_INVALID_SURFACE;
4791
4792     /* Both dest VA image and source surface have YUYV format */
4793     dst = image_data + obj_image->image.offsets[0];
4794     src = (uint8_t *)obj_surface->bo->virtual;
4795
4796     /* Y plane */
4797     dst += rect->y * obj_image->image.pitches[0] + rect->x*2;
4798     src += rect->y * obj_surface->width + rect->x*2;
4799     memcpy_pic(dst, obj_image->image.pitches[0],
4800                src, obj_surface->width*2,
4801                rect->width*2, rect->height);
4802
4803     if (tiling != I915_TILING_NONE)
4804         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
4805     else
4806         dri_bo_unmap(obj_surface->bo);
4807
4808     return va_status;
4809 }
4810
4811 static VAStatus 
4812 i965_sw_getimage(VADriverContextP ctx,
4813     struct object_surface *obj_surface, struct object_image *obj_image,
4814     const VARectangle *rect)
4815 {
4816     void *image_data = NULL;
4817     VAStatus va_status;
4818
4819     if (obj_surface->fourcc != obj_image->image.format.fourcc)
4820         return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
4821
4822     va_status = i965_MapBuffer(ctx, obj_image->image.buf, &image_data);
4823     if (va_status != VA_STATUS_SUCCESS)
4824         return va_status;
4825
4826     switch (obj_image->image.format.fourcc) {
4827     case VA_FOURCC_YV12:
4828     case VA_FOURCC_I420:
4829         get_image_i420(obj_image, image_data, obj_surface, rect);
4830         break;
4831     case VA_FOURCC_NV12:
4832         get_image_nv12(obj_image, image_data, obj_surface, rect);
4833         break;
4834     case VA_FOURCC_YUY2:
4835         /* YUY2 is the format supported by overlay plane */
4836         get_image_yuy2(obj_image, image_data, obj_surface, rect);
4837         break;
4838     default:
4839         va_status = VA_STATUS_ERROR_OPERATION_FAILED;
4840         break;
4841     }
4842     if (va_status != VA_STATUS_SUCCESS)
4843         return va_status;
4844
4845     va_status = i965_UnmapBuffer(ctx, obj_image->image.buf);
4846     return va_status;
4847 }
4848
4849 static VAStatus 
4850 i965_hw_getimage(VADriverContextP ctx,
4851     struct object_surface *obj_surface, struct object_image *obj_image,
4852     const VARectangle *rect)
4853 {
4854     struct i965_surface src_surface;
4855     struct i965_surface dst_surface;
4856
4857     src_surface.base = (struct object_base *)obj_surface;
4858     src_surface.type = I965_SURFACE_TYPE_SURFACE;
4859     src_surface.flags = I965_SURFACE_FLAG_FRAME;
4860
4861     dst_surface.base = (struct object_base *)obj_image;
4862     dst_surface.type = I965_SURFACE_TYPE_IMAGE;
4863     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
4864
4865     return i965_image_processing(ctx, &src_surface, rect, &dst_surface, rect);
4866 }
4867
4868 VAStatus 
4869 i965_GetImage(VADriverContextP ctx,
4870               VASurfaceID surface,
4871               int x,   /* coordinates of the upper left source pixel */
4872               int y,
4873               unsigned int width,      /* width and height of the region */
4874               unsigned int height,
4875               VAImageID image)
4876 {
4877     struct i965_driver_data * const i965 = i965_driver_data(ctx);
4878     struct object_surface * const obj_surface = SURFACE(surface);
4879     struct object_image * const obj_image = IMAGE(image);
4880     VARectangle rect;
4881     VAStatus va_status;
4882
4883     if (!obj_surface)
4884         return VA_STATUS_ERROR_INVALID_SURFACE;
4885     if (!obj_surface->bo) /* don't get anything, keep previous data */
4886         return VA_STATUS_SUCCESS;
4887     if (is_surface_busy(i965, obj_surface))
4888         return VA_STATUS_ERROR_SURFACE_BUSY;
4889
4890     if (!obj_image || !obj_image->bo)
4891         return VA_STATUS_ERROR_INVALID_IMAGE;
4892     if (is_image_busy(i965, obj_image, surface))
4893         return VA_STATUS_ERROR_SURFACE_BUSY;
4894
4895     if (x < 0 || y < 0)
4896         return VA_STATUS_ERROR_INVALID_PARAMETER;
4897     if (x + width > obj_surface->orig_width ||
4898         y + height > obj_surface->orig_height)
4899         return VA_STATUS_ERROR_INVALID_PARAMETER;
4900     if (x + width > obj_image->image.width ||
4901         y + height > obj_image->image.height)
4902         return VA_STATUS_ERROR_INVALID_PARAMETER;
4903
4904     rect.x = x;
4905     rect.y = y;
4906     rect.width = width;
4907     rect.height = height;
4908
4909     if (HAS_ACCELERATED_GETIMAGE(i965))
4910         va_status = i965_hw_getimage(ctx, obj_surface, obj_image, &rect);
4911     else
4912         va_status = i965_sw_getimage(ctx, obj_surface, obj_image, &rect);
4913
4914     return va_status;
4915 }
4916
4917 static VAStatus
4918 put_image_i420(struct object_surface *obj_surface,
4919                const VARectangle *dst_rect,
4920                struct object_image *obj_image, uint8_t *image_data,
4921                const VARectangle *src_rect)
4922 {
4923     uint8_t *dst[3], *src[3];
4924     const int Y = 0;
4925     const int U = obj_image->image.format.fourcc == obj_surface->fourcc ? 1 : 2;
4926     const int V = obj_image->image.format.fourcc == obj_surface->fourcc ? 2 : 1;
4927     unsigned int tiling, swizzle;
4928     VAStatus va_status = VA_STATUS_SUCCESS;
4929
4930     ASSERT_RET(obj_surface->bo, VA_STATUS_ERROR_INVALID_SURFACE);
4931
4932     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4933     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
4934     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
4935     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
4936
4937     if (tiling != I915_TILING_NONE)
4938         drm_intel_gem_bo_map_gtt(obj_surface->bo);
4939     else
4940         dri_bo_map(obj_surface->bo, 0);
4941
4942     if (!obj_surface->bo->virtual)
4943         return VA_STATUS_ERROR_INVALID_SURFACE;
4944
4945     /* Dest VA image has either I420 or YV12 format.
4946        Source VA surface alway has I420 format */
4947     dst[0] = (uint8_t *)obj_surface->bo->virtual;
4948     src[Y] = image_data + obj_image->image.offsets[Y];
4949     dst[1] = dst[0] + obj_surface->width * obj_surface->height;
4950     src[U] = image_data + obj_image->image.offsets[U];
4951     dst[2] = dst[1] + (obj_surface->width / 2) * (obj_surface->height / 2);
4952     src[V] = image_data + obj_image->image.offsets[V];
4953
4954     /* Y plane */
4955     dst[0] += dst_rect->y * obj_surface->width + dst_rect->x;
4956     src[Y] += src_rect->y * obj_image->image.pitches[Y] + src_rect->x;
4957     memcpy_pic(dst[0], obj_surface->width,
4958                src[Y], obj_image->image.pitches[Y],
4959                src_rect->width, src_rect->height);
4960
4961     /* U plane */
4962     dst[1] += (dst_rect->y / 2) * obj_surface->width / 2 + dst_rect->x / 2;
4963     src[U] += (src_rect->y / 2) * obj_image->image.pitches[U] + src_rect->x / 2;
4964     memcpy_pic(dst[1], obj_surface->width / 2,
4965                src[U], obj_image->image.pitches[U],
4966                src_rect->width / 2, src_rect->height / 2);
4967
4968     /* V plane */
4969     dst[2] += (dst_rect->y / 2) * obj_surface->width / 2 + dst_rect->x / 2;
4970     src[V] += (src_rect->y / 2) * obj_image->image.pitches[V] + src_rect->x / 2;
4971     memcpy_pic(dst[2], obj_surface->width / 2,
4972                src[V], obj_image->image.pitches[V],
4973                src_rect->width / 2, src_rect->height / 2);
4974
4975     if (tiling != I915_TILING_NONE)
4976         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
4977     else
4978         dri_bo_unmap(obj_surface->bo);
4979
4980     return va_status;
4981 }
4982
4983 static VAStatus
4984 put_image_nv12(struct object_surface *obj_surface,
4985                const VARectangle *dst_rect,
4986                struct object_image *obj_image, uint8_t *image_data,
4987                const VARectangle *src_rect)
4988 {
4989     uint8_t *dst[2], *src[2];
4990     unsigned int tiling, swizzle;
4991     VAStatus va_status = VA_STATUS_SUCCESS;
4992
4993     if (!obj_surface->bo)
4994         return VA_STATUS_ERROR_INVALID_SURFACE;
4995
4996     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4997     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
4998     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
4999     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5000
5001     if (tiling != I915_TILING_NONE)
5002         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5003     else
5004         dri_bo_map(obj_surface->bo, 0);
5005
5006     if (!obj_surface->bo->virtual)
5007         return VA_STATUS_ERROR_INVALID_SURFACE;
5008
5009     /* Both dest VA image and source surface have NV12 format */
5010     dst[0] = (uint8_t *)obj_surface->bo->virtual;
5011     src[0] = image_data + obj_image->image.offsets[0];
5012     dst[1] = dst[0] + obj_surface->width * obj_surface->height;
5013     src[1] = image_data + obj_image->image.offsets[1];
5014
5015     /* Y plane */
5016     dst[0] += dst_rect->y * obj_surface->width + dst_rect->x;
5017     src[0] += src_rect->y * obj_image->image.pitches[0] + src_rect->x;
5018     memcpy_pic(dst[0], obj_surface->width,
5019                src[0], obj_image->image.pitches[0],
5020                src_rect->width, src_rect->height);
5021
5022     /* UV plane */
5023     dst[1] += (dst_rect->y / 2) * obj_surface->width + (dst_rect->x & -2);
5024     src[1] += (src_rect->y / 2) * obj_image->image.pitches[1] + (src_rect->x & -2);
5025     memcpy_pic(dst[1], obj_surface->width,
5026                src[1], obj_image->image.pitches[1],
5027                src_rect->width, src_rect->height / 2);
5028
5029     if (tiling != I915_TILING_NONE)
5030         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5031     else
5032         dri_bo_unmap(obj_surface->bo);
5033
5034     return va_status;
5035 }
5036
5037 static VAStatus
5038 put_image_yuy2(struct object_surface *obj_surface,
5039                const VARectangle *dst_rect,
5040                struct object_image *obj_image, uint8_t *image_data,
5041                const VARectangle *src_rect)
5042 {
5043     uint8_t *dst, *src;
5044     unsigned int tiling, swizzle;
5045     VAStatus va_status = VA_STATUS_SUCCESS;
5046
5047     ASSERT_RET(obj_surface->bo, VA_STATUS_ERROR_INVALID_SURFACE);
5048     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5049     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
5050     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
5051     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5052
5053     if (tiling != I915_TILING_NONE)
5054         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5055     else
5056         dri_bo_map(obj_surface->bo, 0);
5057
5058     if (!obj_surface->bo->virtual)
5059         return VA_STATUS_ERROR_INVALID_SURFACE;
5060
5061     /* Both dest VA image and source surface have YUY2 format */
5062     dst = (uint8_t *)obj_surface->bo->virtual;
5063     src = image_data + obj_image->image.offsets[0];
5064
5065     /* YUYV packed plane */
5066     dst += dst_rect->y * obj_surface->width + dst_rect->x*2;
5067     src += src_rect->y * obj_image->image.pitches[0] + src_rect->x*2;
5068     memcpy_pic(dst, obj_surface->width*2,
5069                src, obj_image->image.pitches[0],
5070                src_rect->width*2, src_rect->height);
5071
5072     if (tiling != I915_TILING_NONE)
5073         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5074     else
5075         dri_bo_unmap(obj_surface->bo);
5076
5077     return va_status;
5078 }
5079
5080 static VAStatus
5081 i965_sw_putimage(VADriverContextP ctx,
5082     struct object_surface *obj_surface, struct object_image *obj_image,
5083     const VARectangle *src_rect, const VARectangle *dst_rect)
5084 {
5085     VAStatus va_status = VA_STATUS_SUCCESS;
5086     void *image_data = NULL;
5087
5088     /* XXX: don't allow scaling */
5089     if (src_rect->width != dst_rect->width ||
5090         src_rect->height != dst_rect->height)
5091         return VA_STATUS_ERROR_INVALID_PARAMETER;
5092
5093     if (obj_surface->fourcc) {
5094         /* Don't allow format mismatch */
5095         if (obj_surface->fourcc != obj_image->image.format.fourcc)
5096             return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
5097     }
5098
5099     else {
5100         /* VA is surface not used for decoding, use same VA image format */
5101         va_status = i965_check_alloc_surface_bo(
5102             ctx,
5103             obj_surface,
5104             0, /* XXX: don't use tiled surface */
5105             obj_image->image.format.fourcc,
5106             get_sampling_from_fourcc (obj_image->image.format.fourcc));
5107     }
5108
5109     if (va_status != VA_STATUS_SUCCESS)
5110         return va_status;
5111
5112     va_status = i965_MapBuffer(ctx, obj_image->image.buf, &image_data);
5113     if (va_status != VA_STATUS_SUCCESS)
5114         return va_status;
5115      
5116     switch (obj_image->image.format.fourcc) {
5117     case VA_FOURCC_YV12:
5118     case VA_FOURCC_I420:
5119         va_status = put_image_i420(obj_surface, dst_rect, obj_image, image_data, src_rect);
5120         break;
5121     case VA_FOURCC_NV12:
5122         va_status = put_image_nv12(obj_surface, dst_rect, obj_image, image_data, src_rect);
5123         break;
5124     case VA_FOURCC_YUY2:
5125         va_status = put_image_yuy2(obj_surface, dst_rect, obj_image, image_data, src_rect);
5126         break;
5127     default:
5128         va_status = VA_STATUS_ERROR_OPERATION_FAILED;
5129         break;
5130     }
5131     if (va_status != VA_STATUS_SUCCESS)
5132         return va_status;
5133
5134     va_status = i965_UnmapBuffer(ctx, obj_image->image.buf);
5135     return va_status;
5136 }
5137
5138 static VAStatus 
5139 i965_hw_putimage(VADriverContextP ctx,
5140     struct object_surface *obj_surface, struct object_image *obj_image,
5141     const VARectangle *src_rect, const VARectangle *dst_rect)
5142 {
5143     struct i965_surface src_surface, dst_surface;
5144     VAStatus va_status = VA_STATUS_SUCCESS;
5145
5146     if (!obj_surface->bo) {
5147         unsigned int tiling, swizzle;
5148         int surface_sampling = get_sampling_from_fourcc (obj_image->image.format.fourcc);;
5149         dri_bo_get_tiling(obj_image->bo, &tiling, &swizzle);
5150
5151         i965_check_alloc_surface_bo(ctx,
5152                                     obj_surface,
5153                                     !!tiling,
5154                                     obj_image->image.format.fourcc,
5155                                     surface_sampling);
5156     }
5157
5158     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5159
5160     src_surface.base = (struct object_base *)obj_image;
5161     src_surface.type = I965_SURFACE_TYPE_IMAGE;
5162     src_surface.flags = I965_SURFACE_FLAG_FRAME;
5163
5164     dst_surface.base = (struct object_base *)obj_surface;
5165     dst_surface.type = I965_SURFACE_TYPE_SURFACE;
5166     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
5167
5168     va_status = i965_image_processing(ctx,
5169                                       &src_surface,
5170                                       src_rect,
5171                                       &dst_surface,
5172                                       dst_rect);
5173
5174     return  va_status;
5175 }
5176
5177 static VAStatus 
5178 i965_PutImage(VADriverContextP ctx,
5179               VASurfaceID surface,
5180               VAImageID image,
5181               int src_x,
5182               int src_y,
5183               unsigned int src_width,
5184               unsigned int src_height,
5185               int dest_x,
5186               int dest_y,
5187               unsigned int dest_width,
5188               unsigned int dest_height)
5189 {
5190     struct i965_driver_data * const i965 = i965_driver_data(ctx);
5191     struct object_surface * const obj_surface = SURFACE(surface);
5192     struct object_image * const obj_image = IMAGE(image);
5193     VARectangle src_rect, dst_rect;
5194     VAStatus va_status;
5195
5196     if (!obj_surface)
5197         return VA_STATUS_ERROR_INVALID_SURFACE;
5198     if (is_surface_busy(i965, obj_surface))
5199         return VA_STATUS_ERROR_SURFACE_BUSY;
5200
5201     if (!obj_image || !obj_image->bo)
5202         return VA_STATUS_ERROR_INVALID_IMAGE;
5203     if (is_image_busy(i965, obj_image, surface))
5204         return VA_STATUS_ERROR_SURFACE_BUSY;
5205
5206     if (src_x < 0 ||
5207         src_y < 0 ||
5208         src_x + src_width > obj_image->image.width ||
5209         src_y + src_height > obj_image->image.height)
5210         return VA_STATUS_ERROR_INVALID_PARAMETER;
5211
5212     src_rect.x       = src_x;
5213     src_rect.y       = src_y;
5214     src_rect.width   = src_width;
5215     src_rect.height  = src_height;
5216
5217     if (dest_x < 0 ||
5218         dest_y < 0 ||
5219         dest_x + dest_width > obj_surface->orig_width ||
5220         dest_y + dest_height > obj_surface->orig_height)
5221         return VA_STATUS_ERROR_INVALID_PARAMETER;
5222
5223     dst_rect.x      = dest_x;
5224     dst_rect.y      = dest_y;
5225     dst_rect.width  = dest_width;
5226     dst_rect.height = dest_height;
5227
5228     if (HAS_ACCELERATED_PUTIMAGE(i965))
5229         va_status = i965_hw_putimage(ctx, obj_surface, obj_image,
5230             &src_rect, &dst_rect);
5231     else 
5232         va_status = i965_sw_putimage(ctx, obj_surface, obj_image,
5233             &src_rect, &dst_rect);
5234
5235     return va_status;
5236 }
5237
5238 VAStatus 
5239 i965_PutSurface(VADriverContextP ctx,
5240                 VASurfaceID surface,
5241                 void *draw, /* X Drawable */
5242                 short srcx,
5243                 short srcy,
5244                 unsigned short srcw,
5245                 unsigned short srch,
5246                 short destx,
5247                 short desty,
5248                 unsigned short destw,
5249                 unsigned short desth,
5250                 VARectangle *cliprects, /* client supplied clip list */
5251                 unsigned int number_cliprects, /* number of clip rects in the clip list */
5252                 unsigned int flags) /* de-interlacing flags */
5253 {
5254 #ifdef HAVE_VA_X11
5255     if (IS_VA_X11(ctx)) {
5256         VARectangle src_rect, dst_rect;
5257
5258         src_rect.x      = srcx;
5259         src_rect.y      = srcy;
5260         src_rect.width  = srcw;
5261         src_rect.height = srch;
5262
5263         dst_rect.x      = destx;
5264         dst_rect.y      = desty;
5265         dst_rect.width  = destw;
5266         dst_rect.height = desth;
5267
5268         return i965_put_surface_dri(ctx, surface, draw, &src_rect, &dst_rect,
5269                                     cliprects, number_cliprects, flags);
5270     }
5271 #endif
5272     return VA_STATUS_ERROR_UNIMPLEMENTED;
5273 }
5274
5275 static VAStatus
5276 i965_BufferInfo(
5277     VADriverContextP ctx,       /* in */
5278     VABufferID buf_id,          /* in */
5279     VABufferType *type,         /* out */
5280     unsigned int *size,         /* out */
5281     unsigned int *num_elements  /* out */
5282 )
5283 {
5284     struct i965_driver_data *i965 = NULL;
5285     struct object_buffer *obj_buffer = NULL;
5286
5287     i965 = i965_driver_data(ctx);
5288     obj_buffer = BUFFER(buf_id);
5289
5290     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
5291
5292     *type = obj_buffer->type;
5293     *size = obj_buffer->size_element;
5294     *num_elements = obj_buffer->num_elements;
5295
5296     return VA_STATUS_SUCCESS;
5297 }
5298
5299 static VAStatus
5300 i965_LockSurface(
5301     VADriverContextP ctx,           /* in */
5302     VASurfaceID surface,            /* in */
5303     unsigned int *fourcc,           /* out */
5304     unsigned int *luma_stride,      /* out */
5305     unsigned int *chroma_u_stride,  /* out */
5306     unsigned int *chroma_v_stride,  /* out */
5307     unsigned int *luma_offset,      /* out */
5308     unsigned int *chroma_u_offset,  /* out */
5309     unsigned int *chroma_v_offset,  /* out */
5310     unsigned int *buffer_name,      /* out */
5311     void **buffer                   /* out */
5312 )
5313 {
5314     VAStatus vaStatus = VA_STATUS_SUCCESS;
5315     struct i965_driver_data *i965 = i965_driver_data(ctx);
5316     struct object_surface *obj_surface = NULL;
5317     VAImage tmpImage;
5318
5319     ASSERT_RET(fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
5320     ASSERT_RET(luma_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5321     ASSERT_RET(chroma_u_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5322     ASSERT_RET(chroma_v_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5323     ASSERT_RET(luma_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5324     ASSERT_RET(chroma_u_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5325     ASSERT_RET(chroma_v_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5326     ASSERT_RET(buffer_name, VA_STATUS_ERROR_INVALID_PARAMETER);
5327     ASSERT_RET(buffer, VA_STATUS_ERROR_INVALID_PARAMETER);
5328
5329     tmpImage.image_id = VA_INVALID_ID;
5330
5331     obj_surface = SURFACE(surface);
5332     if (obj_surface == NULL) {
5333         // Surface is absent.
5334         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5335         goto error;
5336     }
5337
5338     // Lock functionality is absent now.
5339     if (obj_surface->locked_image_id != VA_INVALID_ID) {
5340         // Surface is locked already.
5341         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5342         goto error;
5343     }
5344
5345     vaStatus = i965_DeriveImage(
5346         ctx,
5347         surface,
5348         &tmpImage);
5349     if (vaStatus != VA_STATUS_SUCCESS) {
5350         goto error;
5351     }
5352
5353     obj_surface->locked_image_id = tmpImage.image_id;
5354
5355     vaStatus = i965_MapBuffer(
5356         ctx,
5357         tmpImage.buf,
5358         buffer);
5359     if (vaStatus != VA_STATUS_SUCCESS) {
5360         goto error;
5361     }
5362
5363     *fourcc = tmpImage.format.fourcc;
5364     *luma_offset = tmpImage.offsets[0];
5365     *luma_stride = tmpImage.pitches[0];
5366     *chroma_u_offset = tmpImage.offsets[1];
5367     *chroma_u_stride = tmpImage.pitches[1];
5368     *chroma_v_offset = tmpImage.offsets[2];
5369     *chroma_v_stride = tmpImage.pitches[2];
5370     *buffer_name = tmpImage.buf;
5371
5372 error:
5373     if (vaStatus != VA_STATUS_SUCCESS) {
5374         buffer = NULL;
5375     }
5376
5377     return vaStatus;
5378 }
5379
5380 static VAStatus
5381 i965_UnlockSurface(
5382     VADriverContextP ctx,   /* in */
5383     VASurfaceID surface     /* in */
5384 )
5385 {
5386     VAStatus vaStatus = VA_STATUS_SUCCESS;
5387     struct i965_driver_data *i965 = i965_driver_data(ctx);
5388     struct object_image *locked_img = NULL;
5389     struct object_surface *obj_surface = NULL;
5390
5391     obj_surface = SURFACE(surface);
5392
5393     if (obj_surface == NULL) {
5394         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;   // Surface is absent
5395         return vaStatus;
5396     }
5397     if (obj_surface->locked_image_id == VA_INVALID_ID) {
5398         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;   // Surface is not locked
5399         return vaStatus;
5400     }
5401
5402     locked_img = IMAGE(obj_surface->locked_image_id);
5403     if (locked_img == NULL || (locked_img->image.image_id == VA_INVALID_ID)) {
5404         // Work image was deallocated before i965_UnlockSurface()
5405         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5406         goto error;
5407     }
5408
5409     vaStatus = i965_UnmapBuffer(
5410         ctx,
5411         locked_img->image.buf);
5412     if (vaStatus != VA_STATUS_SUCCESS) {
5413         goto error;
5414     }
5415
5416     vaStatus = i965_DestroyImage(
5417         ctx,
5418         locked_img->image.image_id);
5419     if (vaStatus != VA_STATUS_SUCCESS) {
5420         goto error;
5421     }
5422
5423     locked_img->image.image_id = VA_INVALID_ID;
5424
5425  error:
5426     obj_surface->locked_image_id = VA_INVALID_ID;
5427
5428     return vaStatus;
5429 }
5430
5431 static VAStatus
5432 i965_GetSurfaceAttributes(
5433     VADriverContextP ctx,
5434     VAConfigID config,
5435     VASurfaceAttrib *attrib_list,
5436     unsigned int num_attribs
5437     )
5438 {
5439     VAStatus vaStatus = VA_STATUS_SUCCESS;
5440     struct i965_driver_data *i965 = i965_driver_data(ctx);
5441     struct object_config *obj_config;
5442     int i;
5443
5444     if (config == VA_INVALID_ID)
5445         return VA_STATUS_ERROR_INVALID_CONFIG;
5446
5447     obj_config = CONFIG(config);
5448
5449     if (obj_config == NULL)
5450         return VA_STATUS_ERROR_INVALID_CONFIG;
5451     
5452     if (attrib_list == NULL || num_attribs == 0)
5453         return VA_STATUS_ERROR_INVALID_PARAMETER;
5454
5455     for (i = 0; i < num_attribs; i++) {
5456         switch (attrib_list[i].type) {
5457         case VASurfaceAttribPixelFormat:
5458             attrib_list[i].value.type = VAGenericValueTypeInteger;
5459             attrib_list[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5460
5461             if (attrib_list[i].value.value.i == 0) {
5462                 if (IS_G4X(i965->intel.device_info)) {
5463                     if (obj_config->profile == VAProfileMPEG2Simple ||
5464                         obj_config->profile == VAProfileMPEG2Main) {
5465                         attrib_list[i].value.value.i = VA_FOURCC_I420;
5466                     } else {
5467                         assert(0);
5468                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5469                     }
5470                 } else if (IS_IRONLAKE(i965->intel.device_info)) {
5471                     if (obj_config->profile == VAProfileMPEG2Simple ||
5472                         obj_config->profile == VAProfileMPEG2Main) {
5473                         attrib_list[i].value.value.i = VA_FOURCC_I420;
5474                     } else if (obj_config->profile == VAProfileH264ConstrainedBaseline ||
5475                                obj_config->profile == VAProfileH264Main ||
5476                                obj_config->profile == VAProfileH264High) {
5477                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5478                     } else if (obj_config->profile == VAProfileNone) {
5479                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5480                     } else {
5481                         assert(0);
5482                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5483                     }
5484                 } else if (IS_GEN6(i965->intel.device_info)) {
5485                     attrib_list[i].value.value.i = VA_FOURCC_NV12;
5486                 } else if (IS_GEN7(i965->intel.device_info) ||
5487                            IS_GEN8(i965->intel.device_info) ||
5488                            IS_GEN9(i965->intel.device_info)) {
5489                     if (obj_config->profile == VAProfileJPEGBaseline)
5490                         attrib_list[i].value.value.i = 0; /* internal format */
5491                     else
5492                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5493                 }
5494             } else {
5495                 if (IS_G4X(i965->intel.device_info)) {
5496                     if (obj_config->profile == VAProfileMPEG2Simple ||
5497                         obj_config->profile == VAProfileMPEG2Main) {
5498                         if (attrib_list[i].value.value.i != VA_FOURCC_I420) {
5499                             attrib_list[i].value.value.i = 0;
5500                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5501                         }
5502                     } else {
5503                         assert(0);
5504                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5505                     }
5506                 } else if (IS_IRONLAKE(i965->intel.device_info)) {
5507                     if (obj_config->profile == VAProfileMPEG2Simple ||
5508                         obj_config->profile == VAProfileMPEG2Main) {
5509                         if (attrib_list[i].value.value.i != VA_FOURCC_I420) {
5510                             attrib_list[i].value.value.i = 0;                            
5511                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5512                         }
5513                     } else if (obj_config->profile == VAProfileH264ConstrainedBaseline ||
5514                                obj_config->profile == VAProfileH264Main ||
5515                                obj_config->profile == VAProfileH264High) {
5516                         if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5517                             attrib_list[i].value.value.i = 0;
5518                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5519                         }
5520                     } else if (obj_config->profile == VAProfileNone) {
5521                         switch (attrib_list[i].value.value.i) {
5522                         case VA_FOURCC_NV12:
5523                         case VA_FOURCC_I420:
5524                         case VA_FOURCC_YV12:
5525                         case VA_FOURCC_YUY2:
5526                         case VA_FOURCC_BGRA:
5527                         case VA_FOURCC_BGRX:
5528                         case VA_FOURCC_RGBX:
5529                         case VA_FOURCC_RGBA:
5530                             break;
5531                         default:
5532                             attrib_list[i].value.value.i = 0;                            
5533                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5534                             break;
5535                         }
5536                     } else {
5537                         assert(0);
5538                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5539                     }
5540                 } else if (IS_GEN6(i965->intel.device_info)) {
5541                     if (obj_config->entrypoint == VAEntrypointEncSlice ||
5542                         obj_config->entrypoint == VAEntrypointVideoProc) {
5543                         switch (attrib_list[i].value.value.i) {
5544                         case VA_FOURCC_NV12:
5545                         case VA_FOURCC_I420:
5546                         case VA_FOURCC_YV12:
5547                         case VA_FOURCC_YUY2:
5548                         case VA_FOURCC_BGRA:
5549                         case VA_FOURCC_BGRX:
5550                         case VA_FOURCC_RGBX:
5551                         case VA_FOURCC_RGBA:
5552                             break;
5553                         default:
5554                             attrib_list[i].value.value.i = 0;                            
5555                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5556                             break;
5557                         }
5558                     } else {
5559                         if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5560                             attrib_list[i].value.value.i = 0;
5561                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5562                         }
5563                     }
5564                 } else if (IS_GEN7(i965->intel.device_info) ||
5565                            IS_GEN8(i965->intel.device_info) ||
5566                            IS_GEN9(i965->intel.device_info)) {
5567                     if (obj_config->entrypoint == VAEntrypointEncSlice ||
5568                         obj_config->entrypoint == VAEntrypointVideoProc ||
5569                         obj_config->entrypoint == VAEntrypointEncSliceLP) {
5570                         switch (attrib_list[i].value.value.i) {
5571                         case VA_FOURCC_NV12:
5572                         case VA_FOURCC_I420:
5573                         case VA_FOURCC_YV12:
5574                             break;
5575                         default:
5576                             attrib_list[i].value.value.i = 0;                            
5577                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5578                             break;
5579                         }
5580                     } else {
5581                         if (obj_config->profile == VAProfileJPEGBaseline) {
5582                             attrib_list[i].value.value.i = 0;   /* JPEG decoding always uses an internal format */
5583                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5584                         } else {
5585                             if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5586                                 attrib_list[i].value.value.i = 0;
5587                                 attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5588                             }
5589                         }
5590                     }
5591                 }
5592             }
5593
5594             break;
5595         case VASurfaceAttribMinWidth:
5596             /* FIXME: add support for it later */
5597             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5598             break;
5599         case VASurfaceAttribMaxWidth:
5600             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5601             break;
5602         case VASurfaceAttribMinHeight:
5603             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5604             break;
5605         case VASurfaceAttribMaxHeight:
5606             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5607             break;
5608         default:
5609             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5610             break;
5611         }
5612     }
5613
5614     return vaStatus;
5615 }
5616
5617 static VAStatus
5618 i965_QuerySurfaceAttributes(VADriverContextP ctx,
5619                             VAConfigID config,
5620                             VASurfaceAttrib *attrib_list,
5621                             unsigned int *num_attribs)
5622 {
5623     VAStatus vaStatus = VA_STATUS_SUCCESS;
5624     struct i965_driver_data *i965 = i965_driver_data(ctx);
5625     struct object_config *obj_config;
5626     int i = 0;
5627     VASurfaceAttrib *attribs = NULL;
5628     int max_width;
5629     int max_height;
5630
5631     if (config == VA_INVALID_ID)
5632         return VA_STATUS_ERROR_INVALID_CONFIG;
5633
5634     obj_config = CONFIG(config);
5635
5636     if (obj_config == NULL)
5637         return VA_STATUS_ERROR_INVALID_CONFIG;
5638     
5639     if (!attrib_list && !num_attribs)
5640         return VA_STATUS_ERROR_INVALID_PARAMETER;
5641
5642     if (attrib_list == NULL) {
5643         *num_attribs = I965_MAX_SURFACE_ATTRIBUTES;
5644         return VA_STATUS_SUCCESS;
5645     }
5646
5647     attribs = malloc(I965_MAX_SURFACE_ATTRIBUTES *sizeof(*attribs));
5648     
5649     if (attribs == NULL)
5650         return VA_STATUS_ERROR_ALLOCATION_FAILED;
5651
5652     if (IS_G4X(i965->intel.device_info)) {
5653         if (obj_config->profile == VAProfileMPEG2Simple ||
5654             obj_config->profile == VAProfileMPEG2Main) {
5655             attribs[i].type = VASurfaceAttribPixelFormat;
5656             attribs[i].value.type = VAGenericValueTypeInteger;
5657             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5658             attribs[i].value.value.i = VA_FOURCC_I420;
5659             i++;
5660         }
5661     } else if (IS_IRONLAKE(i965->intel.device_info)) {
5662         switch (obj_config->profile) {
5663         case VAProfileMPEG2Simple:
5664         case VAProfileMPEG2Main:
5665             attribs[i].type = VASurfaceAttribPixelFormat;
5666             attribs[i].value.type = VAGenericValueTypeInteger;
5667             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5668             attribs[i].value.value.i = VA_FOURCC_I420;
5669             i++;
5670             
5671             break;
5672
5673         case VAProfileH264ConstrainedBaseline:
5674         case VAProfileH264Main:
5675         case VAProfileH264High:
5676             attribs[i].type = VASurfaceAttribPixelFormat;
5677             attribs[i].value.type = VAGenericValueTypeInteger;
5678             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5679             attribs[i].value.value.i = VA_FOURCC_NV12;
5680             i++;
5681
5682         case VAProfileNone:
5683             attribs[i].type = VASurfaceAttribPixelFormat;
5684             attribs[i].value.type = VAGenericValueTypeInteger;
5685             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5686             attribs[i].value.value.i = VA_FOURCC_NV12;
5687             i++;
5688
5689             attribs[i].type = VASurfaceAttribPixelFormat;
5690             attribs[i].value.type = VAGenericValueTypeInteger;
5691             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5692             attribs[i].value.value.i = VA_FOURCC_I420;
5693             i++;
5694
5695             break;
5696             
5697         default:
5698             break;
5699         }
5700     } else if (IS_GEN6(i965->intel.device_info)) {
5701         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
5702             attribs[i].type = VASurfaceAttribPixelFormat;
5703             attribs[i].value.type = VAGenericValueTypeInteger;
5704             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5705             attribs[i].value.value.i = VA_FOURCC_NV12;
5706             i++;
5707         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
5708                    obj_config->entrypoint == VAEntrypointVideoProc) { /* vpp */ 
5709             attribs[i].type = VASurfaceAttribPixelFormat;
5710             attribs[i].value.type = VAGenericValueTypeInteger;
5711             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5712             attribs[i].value.value.i = VA_FOURCC_NV12;
5713             i++;
5714
5715             attribs[i].type = VASurfaceAttribPixelFormat;
5716             attribs[i].value.type = VAGenericValueTypeInteger;
5717             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5718             attribs[i].value.value.i = VA_FOURCC_I420;
5719             i++;
5720
5721             attribs[i].type = VASurfaceAttribPixelFormat;
5722             attribs[i].value.type = VAGenericValueTypeInteger;
5723             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5724             attribs[i].value.value.i = VA_FOURCC_YV12;
5725             i++;
5726
5727             if (obj_config->entrypoint == VAEntrypointVideoProc) {
5728                 attribs[i].type = VASurfaceAttribPixelFormat;
5729                 attribs[i].value.type = VAGenericValueTypeInteger;
5730                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5731                 attribs[i].value.value.i = VA_FOURCC_YUY2;
5732                 i++;
5733
5734                 attribs[i].type = VASurfaceAttribPixelFormat;
5735                 attribs[i].value.type = VAGenericValueTypeInteger;
5736                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5737                 attribs[i].value.value.i = VA_FOURCC_RGBA;
5738                 i++;
5739
5740                 attribs[i].type = VASurfaceAttribPixelFormat;
5741                 attribs[i].value.type = VAGenericValueTypeInteger;
5742                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5743                 attribs[i].value.value.i = VA_FOURCC_RGBX;
5744                 i++;
5745             }
5746         }
5747     } else if (IS_GEN7(i965->intel.device_info)) {
5748         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
5749             if (obj_config->profile == VAProfileJPEGBaseline) {
5750                 attribs[i].type = VASurfaceAttribPixelFormat;
5751                 attribs[i].value.type = VAGenericValueTypeInteger;
5752                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5753                 attribs[i].value.value.i = VA_FOURCC_IMC3;
5754                 i++;
5755
5756                 attribs[i].type = VASurfaceAttribPixelFormat;
5757                 attribs[i].value.type = VAGenericValueTypeInteger;
5758                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5759                 attribs[i].value.value.i = VA_FOURCC_IMC1;
5760                 i++;
5761
5762                 attribs[i].type = VASurfaceAttribPixelFormat;
5763                 attribs[i].value.type = VAGenericValueTypeInteger;
5764                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5765                 attribs[i].value.value.i = VA_FOURCC_Y800;
5766                 i++;
5767
5768                 attribs[i].type = VASurfaceAttribPixelFormat;
5769                 attribs[i].value.type = VAGenericValueTypeInteger;
5770                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5771                 attribs[i].value.value.i = VA_FOURCC_411P;
5772                 i++;
5773
5774                 attribs[i].type = VASurfaceAttribPixelFormat;
5775                 attribs[i].value.type = VAGenericValueTypeInteger;
5776                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5777                 attribs[i].value.value.i = VA_FOURCC_422H;
5778                 i++;
5779
5780                 attribs[i].type = VASurfaceAttribPixelFormat;
5781                 attribs[i].value.type = VAGenericValueTypeInteger;
5782                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5783                 attribs[i].value.value.i = VA_FOURCC_422V;
5784                 i++;
5785
5786                 attribs[i].type = VASurfaceAttribPixelFormat;
5787                 attribs[i].value.type = VAGenericValueTypeInteger;
5788                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5789                 attribs[i].value.value.i = VA_FOURCC_444P;
5790                 i++;
5791             } else if (obj_config->profile == VAProfileHEVCMain10) {
5792                 attribs[i].type = VASurfaceAttribPixelFormat;
5793                 attribs[i].value.type = VAGenericValueTypeInteger;
5794                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5795                 attribs[i].value.value.i = VA_FOURCC_P010;
5796                 i++;
5797              } else {
5798                 attribs[i].type = VASurfaceAttribPixelFormat;
5799                 attribs[i].value.type = VAGenericValueTypeInteger;
5800                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5801                 attribs[i].value.value.i = VA_FOURCC_NV12;
5802                 i++;
5803             }
5804         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
5805                    obj_config->entrypoint == VAEntrypointVideoProc) { /* vpp */ 
5806             attribs[i].type = VASurfaceAttribPixelFormat;
5807             attribs[i].value.type = VAGenericValueTypeInteger;
5808             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5809             attribs[i].value.value.i = VA_FOURCC_NV12;
5810             i++;
5811
5812             attribs[i].type = VASurfaceAttribPixelFormat;
5813             attribs[i].value.type = VAGenericValueTypeInteger;
5814             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5815             attribs[i].value.value.i = VA_FOURCC_I420;
5816             i++;
5817
5818             attribs[i].type = VASurfaceAttribPixelFormat;
5819             attribs[i].value.type = VAGenericValueTypeInteger;
5820             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5821             attribs[i].value.value.i = VA_FOURCC_YV12;
5822             i++;
5823
5824             attribs[i].type = VASurfaceAttribPixelFormat;
5825             attribs[i].value.type = VAGenericValueTypeInteger;
5826             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5827             attribs[i].value.value.i = VA_FOURCC_IMC3;
5828             i++;
5829
5830             if (obj_config->entrypoint == VAEntrypointVideoProc) {
5831                 attribs[i].type = VASurfaceAttribPixelFormat;
5832                 attribs[i].value.type = VAGenericValueTypeInteger;
5833                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5834                 attribs[i].value.value.i = VA_FOURCC_YUY2;
5835                 i++;
5836
5837                 attribs[i].type = VASurfaceAttribPixelFormat;
5838                 attribs[i].value.type = VAGenericValueTypeInteger;
5839                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5840                 attribs[i].value.value.i = VA_FOURCC_RGBA;
5841                 i++;
5842
5843                 attribs[i].type = VASurfaceAttribPixelFormat;
5844                 attribs[i].value.type = VAGenericValueTypeInteger;
5845                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5846                 attribs[i].value.value.i = VA_FOURCC_RGBX;
5847                 i++;
5848
5849                 attribs[i].type = VASurfaceAttribPixelFormat;
5850                 attribs[i].value.type = VAGenericValueTypeInteger;
5851                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5852                 attribs[i].value.value.i = VA_FOURCC_BGRA;
5853                 i++;
5854
5855                 attribs[i].type = VASurfaceAttribPixelFormat;
5856                 attribs[i].value.type = VAGenericValueTypeInteger;
5857                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5858                 attribs[i].value.value.i = VA_FOURCC_BGRX;
5859                 i++;
5860
5861                 attribs[i].type = VASurfaceAttribPixelFormat;
5862                 attribs[i].value.type = VAGenericValueTypeInteger;
5863                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5864                 attribs[i].value.value.i = VA_FOURCC_YV16;
5865                 i++;
5866             }
5867         }
5868     } else if (IS_GEN8(i965->intel.device_info) ||
5869                IS_GEN9(i965->intel.device_info)) {
5870         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
5871             if (obj_config->profile == VAProfileJPEGBaseline) {
5872                 attribs[i].type = VASurfaceAttribPixelFormat;
5873                 attribs[i].value.type = VAGenericValueTypeInteger;
5874                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5875                 attribs[i].value.value.i = VA_FOURCC_IMC3;
5876                 i++;
5877
5878                 attribs[i].type = VASurfaceAttribPixelFormat;
5879                 attribs[i].value.type = VAGenericValueTypeInteger;
5880                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5881                 attribs[i].value.value.i = VA_FOURCC_IMC1;
5882                 i++;
5883
5884                 attribs[i].type = VASurfaceAttribPixelFormat;
5885                 attribs[i].value.type = VAGenericValueTypeInteger;
5886                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5887                 attribs[i].value.value.i = VA_FOURCC_Y800;
5888                 i++;
5889
5890                 attribs[i].type = VASurfaceAttribPixelFormat;
5891                 attribs[i].value.type = VAGenericValueTypeInteger;
5892                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5893                 attribs[i].value.value.i = VA_FOURCC_411P;
5894                 i++;
5895
5896                 attribs[i].type = VASurfaceAttribPixelFormat;
5897                 attribs[i].value.type = VAGenericValueTypeInteger;
5898                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5899                 attribs[i].value.value.i = VA_FOURCC_422H;
5900                 i++;
5901
5902                 attribs[i].type = VASurfaceAttribPixelFormat;
5903                 attribs[i].value.type = VAGenericValueTypeInteger;
5904                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5905                 attribs[i].value.value.i = VA_FOURCC_422V;
5906                 i++;
5907
5908                 attribs[i].type = VASurfaceAttribPixelFormat;
5909                 attribs[i].value.type = VAGenericValueTypeInteger;
5910                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5911                 attribs[i].value.value.i = VA_FOURCC_444P;
5912                 i++;
5913             } else {
5914                 attribs[i].type = VASurfaceAttribPixelFormat;
5915                 attribs[i].value.type = VAGenericValueTypeInteger;
5916                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5917                 attribs[i].value.value.i = VA_FOURCC_NV12;
5918                 i++;
5919
5920                 if ((obj_config->profile == VAProfileHEVCMain10) ||
5921                     (obj_config->profile == VAProfileVP9Profile2)) {
5922                     attribs[i].type = VASurfaceAttribPixelFormat;
5923                     attribs[i].value.type = VAGenericValueTypeInteger;
5924                     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5925                     attribs[i].value.value.i = VA_FOURCC_P010;
5926                     i++;
5927                 }
5928             }
5929         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
5930                    obj_config->entrypoint == VAEntrypointVideoProc ||
5931                    obj_config->entrypoint == VAEntrypointEncSliceLP ||
5932                    obj_config->entrypoint == VAEntrypointEncPicture) {
5933
5934             if (obj_config->profile == VAProfileHEVCMain10) {
5935                 attribs[i].type = VASurfaceAttribPixelFormat;
5936                 attribs[i].value.type = VAGenericValueTypeInteger;
5937                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5938                 attribs[i].value.value.i = VA_FOURCC_P010;
5939                 i++;
5940             } else {
5941               attribs[i].type = VASurfaceAttribPixelFormat;
5942               attribs[i].value.type = VAGenericValueTypeInteger;
5943               attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5944               attribs[i].value.value.i = VA_FOURCC_NV12;
5945               i++;
5946
5947               attribs[i].type = VASurfaceAttribPixelFormat;
5948               attribs[i].value.type = VAGenericValueTypeInteger;
5949               attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5950               attribs[i].value.value.i = VA_FOURCC_I420;
5951               i++;
5952
5953               attribs[i].type = VASurfaceAttribPixelFormat;
5954               attribs[i].value.type = VAGenericValueTypeInteger;
5955               attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5956               attribs[i].value.value.i = VA_FOURCC_YV12;
5957               i++;
5958
5959               attribs[i].type = VASurfaceAttribPixelFormat;
5960               attribs[i].value.type = VAGenericValueTypeInteger;
5961               attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5962               attribs[i].value.value.i = VA_FOURCC_IMC3;
5963               i++;
5964             }
5965
5966             if (obj_config->entrypoint == VAEntrypointVideoProc) {
5967                 attribs[i].type = VASurfaceAttribPixelFormat;
5968                 attribs[i].value.type = VAGenericValueTypeInteger;
5969                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5970                 attribs[i].value.value.i = VA_FOURCC_YUY2;
5971                 i++;
5972
5973                 attribs[i].type = VASurfaceAttribPixelFormat;
5974                 attribs[i].value.type = VAGenericValueTypeInteger;
5975                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5976                 attribs[i].value.value.i = VA_FOURCC_RGBA;
5977                 i++;
5978
5979                 attribs[i].type = VASurfaceAttribPixelFormat;
5980                 attribs[i].value.type = VAGenericValueTypeInteger;
5981                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5982                 attribs[i].value.value.i = VA_FOURCC_RGBX;
5983                 i++;
5984
5985                 attribs[i].type = VASurfaceAttribPixelFormat;
5986                 attribs[i].value.type = VAGenericValueTypeInteger;
5987                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5988                 attribs[i].value.value.i = VA_FOURCC_BGRA;
5989                 i++;
5990
5991                 attribs[i].type = VASurfaceAttribPixelFormat;
5992                 attribs[i].value.type = VAGenericValueTypeInteger;
5993                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5994                 attribs[i].value.value.i = VA_FOURCC_BGRX;
5995                 i++;
5996
5997                 attribs[i].type = VASurfaceAttribPixelFormat;
5998                 attribs[i].value.type = VAGenericValueTypeInteger;
5999                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6000                 attribs[i].value.value.i = VA_FOURCC_YV16;
6001                 i++;
6002
6003                 if(HAS_VPP_P010(i965)) {
6004                   attribs[i].type = VASurfaceAttribPixelFormat;
6005                   attribs[i].value.type = VAGenericValueTypeInteger;
6006                   attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6007                   attribs[i].value.value.i = VA_FOURCC_P010;
6008                   i++;
6009
6010                   attribs[i].type = VASurfaceAttribPixelFormat;
6011                   attribs[i].value.type = VAGenericValueTypeInteger;
6012                   attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6013                   attribs[i].value.value.i = VA_FOURCC_I010;
6014                   i++;
6015                 }
6016             }
6017
6018             /* Additional support for jpeg encoder */
6019             if (obj_config->profile == VAProfileJPEGBaseline
6020                     && obj_config->entrypoint == VAEntrypointEncPicture) {
6021                 attribs[i].type = VASurfaceAttribPixelFormat;
6022                 attribs[i].value.type = VAGenericValueTypeInteger;
6023                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6024                 attribs[i].value.value.i = VA_FOURCC_YUY2;
6025                 i++;
6026
6027                 attribs[i].type = VASurfaceAttribPixelFormat;
6028                 attribs[i].value.type = VAGenericValueTypeInteger;
6029                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6030                 attribs[i].value.value.i = VA_FOURCC_UYVY;
6031                 i++;
6032
6033                 attribs[i].type = VASurfaceAttribPixelFormat;
6034                 attribs[i].value.type = VAGenericValueTypeInteger;
6035                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6036                 attribs[i].value.value.i = VA_FOURCC_YV16;
6037                 i++;
6038
6039                 attribs[i].type = VASurfaceAttribPixelFormat;
6040                 attribs[i].value.type = VAGenericValueTypeInteger;
6041                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6042                 attribs[i].value.value.i = VA_FOURCC_Y800;
6043                 i++;
6044             }
6045         }
6046     }
6047
6048     attribs[i].type = VASurfaceAttribMemoryType;
6049     attribs[i].value.type = VAGenericValueTypeInteger;
6050     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6051     attribs[i].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA |
6052         VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM |
6053         VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
6054     i++;
6055
6056     attribs[i].type = VASurfaceAttribExternalBufferDescriptor;
6057     attribs[i].value.type = VAGenericValueTypePointer;
6058     attribs[i].flags = VA_SURFACE_ATTRIB_SETTABLE;
6059     attribs[i].value.value.p = NULL; /* ignore */
6060     i++;
6061
6062     max_resolution(i965, obj_config, &max_width, &max_height);
6063
6064     attribs[i].type = VASurfaceAttribMaxWidth;
6065     attribs[i].value.type = VAGenericValueTypeInteger;
6066     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
6067     attribs[i].value.value.i = max_width;
6068     i++;
6069
6070     attribs[i].type = VASurfaceAttribMaxHeight;
6071     attribs[i].value.type = VAGenericValueTypeInteger;
6072     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
6073     attribs[i].value.value.i = max_height;
6074     i++;
6075
6076     if (i > *num_attribs) {
6077         *num_attribs = i;
6078         free(attribs);
6079         return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6080     }
6081
6082     *num_attribs = i;
6083     memcpy(attrib_list, attribs, i * sizeof(*attribs));
6084     free(attribs);
6085
6086     return vaStatus;
6087 }
6088
6089 /* Acquires buffer handle for external API usage (internal implementation) */
6090 static VAStatus
6091 i965_acquire_buffer_handle(struct object_buffer *obj_buffer,
6092     uint32_t mem_type, VABufferInfo *out_buf_info)
6093 {
6094     struct buffer_store *buffer_store;
6095
6096     buffer_store = obj_buffer->buffer_store;
6097     if (!buffer_store || !buffer_store->bo)
6098         return VA_STATUS_ERROR_INVALID_BUFFER;
6099
6100     /* Synchronization point */
6101     drm_intel_bo_wait_rendering(buffer_store->bo);
6102
6103     if (obj_buffer->export_refcount > 0) {
6104         if (obj_buffer->export_state.mem_type != mem_type)
6105             return VA_STATUS_ERROR_INVALID_PARAMETER;
6106     }
6107     else {
6108         VABufferInfo * const buf_info = &obj_buffer->export_state;
6109
6110         switch (mem_type) {
6111         case VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM: {
6112             uint32_t name;
6113             if (drm_intel_bo_flink(buffer_store->bo, &name) != 0)
6114                 return VA_STATUS_ERROR_INVALID_BUFFER;
6115             buf_info->handle = name;
6116             break;
6117         }
6118         case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: {
6119             int fd;
6120             if (drm_intel_bo_gem_export_to_prime(buffer_store->bo, &fd) != 0)
6121                 return VA_STATUS_ERROR_INVALID_BUFFER;
6122             buf_info->handle = (intptr_t)fd;
6123             break;
6124         }
6125         }
6126
6127         buf_info->type = obj_buffer->type;
6128         buf_info->mem_type = mem_type;
6129         buf_info->mem_size =
6130             obj_buffer->num_elements * obj_buffer->size_element;
6131     }
6132
6133     obj_buffer->export_refcount++;
6134     *out_buf_info = obj_buffer->export_state;
6135     return VA_STATUS_SUCCESS;
6136 }
6137
6138 /* Releases buffer handle after usage (internal implementation) */
6139 static VAStatus
6140 i965_release_buffer_handle(struct object_buffer *obj_buffer)
6141 {
6142     if (obj_buffer->export_refcount == 0)
6143         return VA_STATUS_ERROR_INVALID_BUFFER;
6144
6145     if (--obj_buffer->export_refcount == 0) {
6146         VABufferInfo * const buf_info = &obj_buffer->export_state;
6147
6148         switch (buf_info->mem_type) {
6149         case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: {
6150             close((intptr_t)buf_info->handle);
6151             break;
6152         }
6153         }
6154         buf_info->mem_type = 0;
6155     }
6156     return VA_STATUS_SUCCESS;
6157 }
6158
6159 /** Acquires buffer handle for external API usage */
6160 static VAStatus
6161 i965_AcquireBufferHandle(VADriverContextP ctx, VABufferID buf_id,
6162     VABufferInfo *buf_info)
6163 {
6164     struct i965_driver_data * const i965 = i965_driver_data(ctx);
6165     struct object_buffer * const obj_buffer = BUFFER(buf_id);
6166     uint32_t i, mem_type;
6167
6168     /* List of supported memory types, in preferred order */
6169     static const uint32_t mem_types[] = {
6170         VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
6171         VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM,
6172         0
6173     };
6174
6175     if (!obj_buffer)
6176         return VA_STATUS_ERROR_INVALID_BUFFER;
6177     /* XXX: only VA surface|image like buffers are supported for now */
6178     if (obj_buffer->type != VAImageBufferType)
6179         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
6180
6181     /*
6182      * As the allocated buffer by calling vaCreateBuffer is related with
6183      * the specific context, it is unnecessary to export it.
6184      * So it is not supported when the buffer is allocated from wrapped
6185      * backend dirver.
6186      */
6187     if (obj_buffer->wrapper_buffer != VA_INVALID_ID) {
6188         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
6189     }
6190
6191     if (!buf_info)
6192         return VA_STATUS_ERROR_INVALID_PARAMETER;
6193
6194     if (!buf_info->mem_type)
6195         mem_type = mem_types[0];
6196     else {
6197         mem_type = 0;
6198         for (i = 0; mem_types[i] != 0; i++) {
6199             if (buf_info->mem_type & mem_types[i]) {
6200                 mem_type = buf_info->mem_type;
6201                 break;
6202             }
6203         }
6204         if (!mem_type)
6205             return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
6206     }
6207     return i965_acquire_buffer_handle(obj_buffer, mem_type, buf_info);
6208 }
6209
6210 /** Releases buffer handle after usage from external API */
6211 static VAStatus
6212 i965_ReleaseBufferHandle(VADriverContextP ctx, VABufferID buf_id)
6213 {
6214     struct i965_driver_data * const i965 = i965_driver_data(ctx);
6215     struct object_buffer * const obj_buffer = BUFFER(buf_id);
6216
6217     if (!obj_buffer)
6218         return VA_STATUS_ERROR_INVALID_BUFFER;
6219
6220     if (obj_buffer->wrapper_buffer != VA_INVALID_ID) {
6221         return VA_STATUS_ERROR_INVALID_BUFFER;
6222     }
6223
6224     return i965_release_buffer_handle(obj_buffer);
6225 }
6226
6227 static int
6228 i965_os_has_ring_support(VADriverContextP ctx,
6229                          int ring)
6230 {
6231     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6232
6233     switch (ring) {
6234     case I965_RING_BSD:
6235         return i965->intel.has_bsd;
6236         
6237     case I965_RING_BLT:
6238         return i965->intel.has_blt;
6239         
6240     case I965_RING_VEBOX:
6241         return i965->intel.has_vebox;
6242
6243     case I965_RING_NULL:
6244         return 1; /* Always support */
6245
6246     default:
6247         /* should never get here */
6248         assert(0);
6249         break;
6250     }
6251
6252     return 0;
6253 }
6254                                 
6255 /* 
6256  * Query video processing pipeline 
6257  */
6258 VAStatus i965_QueryVideoProcFilters(
6259     VADriverContextP    ctx,
6260     VAContextID         context,
6261     VAProcFilterType   *filters,
6262     unsigned int       *num_filters
6263     )
6264 {
6265     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6266     unsigned int i = 0, num = 0;
6267
6268     if (!num_filters  || !filters)
6269         return VA_STATUS_ERROR_INVALID_PARAMETER;
6270
6271     for (i = 0; i < i965->codec_info->num_filters; i++) {
6272         if (i965_os_has_ring_support(ctx, i965->codec_info->filters[i].ring)) {
6273             if (num == *num_filters) {
6274                 *num_filters = i965->codec_info->num_filters;
6275
6276                 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6277             }
6278          
6279             filters[num++] = i965->codec_info->filters[i].type;
6280         }
6281     }
6282
6283     *num_filters = num;
6284
6285     return VA_STATUS_SUCCESS;
6286 }
6287
6288 VAStatus i965_QueryVideoProcFilterCaps(
6289     VADriverContextP    ctx,
6290     VAContextID         context,
6291     VAProcFilterType    type,
6292     void               *filter_caps,
6293     unsigned int       *num_filter_caps
6294     )
6295 {
6296     unsigned int i = 0;
6297     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6298
6299     if (!filter_caps || !num_filter_caps)
6300         return VA_STATUS_ERROR_INVALID_PARAMETER;
6301
6302     for (i = 0; i < i965->codec_info->num_filters; i++) {
6303         if (type == i965->codec_info->filters[i].type &&
6304             i965_os_has_ring_support(ctx, i965->codec_info->filters[i].ring))
6305             break;
6306     }
6307
6308     if (i == i965->codec_info->num_filters)
6309         return VA_STATUS_ERROR_UNSUPPORTED_FILTER;
6310
6311     i = 0;
6312
6313     switch (type) {
6314     case VAProcFilterNoiseReduction:
6315     case VAProcFilterSharpening:
6316         {
6317             VAProcFilterCap *cap = filter_caps;
6318
6319             if (*num_filter_caps < 1) {
6320                 *num_filter_caps = 1;
6321                 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6322             }
6323             
6324             cap->range.min_value = 0.0;
6325             cap->range.max_value = 1.0;
6326             cap->range.default_value = 0.5;
6327             cap->range.step = 0.03125; /* 1.0 / 32 */
6328             i++;
6329         }
6330
6331         break;
6332
6333     case VAProcFilterDeinterlacing:
6334         {
6335             VAProcFilterCapDeinterlacing *cap = filter_caps;
6336
6337             if (*num_filter_caps < VAProcDeinterlacingCount) {
6338                 *num_filter_caps = VAProcDeinterlacingCount;
6339                 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6340             }
6341         
6342             cap->type = VAProcDeinterlacingBob;
6343             i++;
6344             cap++;
6345
6346
6347             if (i965->codec_info->has_di_motion_adptive) {
6348                 cap->type = VAProcDeinterlacingMotionAdaptive;
6349                 i++;
6350                 cap++;
6351             }
6352
6353             if (i965->codec_info->has_di_motion_compensated) {
6354                 cap->type = VAProcDeinterlacingMotionCompensated;
6355                 i++;
6356                 cap++;
6357             }
6358        }
6359
6360         break;
6361
6362     case VAProcFilterColorBalance:
6363         {
6364             VAProcFilterCapColorBalance *cap = filter_caps;
6365
6366             if (*num_filter_caps < VAProcColorBalanceCount) {
6367                 *num_filter_caps = VAProcColorBalanceCount;
6368                 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6369             }
6370
6371             cap->type = VAProcColorBalanceHue;
6372             cap->range.min_value = -180.0;
6373             cap->range.max_value = 180.0;
6374             cap->range.default_value = 0.0;
6375             cap->range.step = 1.0; 
6376             i++;
6377             cap++; 
6378  
6379             cap->type = VAProcColorBalanceSaturation;
6380             cap->range.min_value = 0.0;
6381             cap->range.max_value = 10.0;
6382             cap->range.default_value = 1.0;
6383             cap->range.step = 0.1; 
6384             i++;
6385             cap++; 
6386  
6387             cap->type = VAProcColorBalanceBrightness;
6388             cap->range.min_value = -100.0;
6389             cap->range.max_value = 100.0;
6390             cap->range.default_value = 0.0;
6391             cap->range.step = 1.0; 
6392             i++;
6393             cap++; 
6394  
6395             cap->type = VAProcColorBalanceContrast;
6396             cap->range.min_value = 0.0;
6397             cap->range.max_value = 10.0;
6398             cap->range.default_value = 1.0;
6399             cap->range.step = 0.1; 
6400             i++;
6401             cap++; 
6402         }
6403
6404         break;
6405
6406     default:
6407         
6408         break;
6409     }
6410
6411     *num_filter_caps = i;
6412
6413     return VA_STATUS_SUCCESS;
6414 }
6415
6416 static VAProcColorStandardType vpp_input_color_standards[VAProcColorStandardCount] = {
6417     VAProcColorStandardBT601,
6418 };
6419
6420 static VAProcColorStandardType vpp_output_color_standards[VAProcColorStandardCount] = {
6421     VAProcColorStandardBT601,
6422 };
6423
6424 VAStatus i965_QueryVideoProcPipelineCaps(
6425     VADriverContextP ctx,
6426     VAContextID context,
6427     VABufferID *filters,
6428     unsigned int num_filters,
6429     VAProcPipelineCaps *pipeline_cap     /* out */
6430     )
6431 {
6432     struct i965_driver_data * const i965 = i965_driver_data(ctx);
6433     unsigned int i = 0;
6434
6435     pipeline_cap->pipeline_flags = 0;
6436     pipeline_cap->filter_flags = 0;
6437     pipeline_cap->num_forward_references = 0;
6438     pipeline_cap->num_backward_references = 0;
6439     pipeline_cap->num_input_color_standards = 1;
6440     pipeline_cap->input_color_standards = vpp_input_color_standards;
6441     pipeline_cap->num_output_color_standards = 1;
6442     pipeline_cap->output_color_standards = vpp_output_color_standards;
6443
6444     for (i = 0; i < num_filters; i++) {
6445         struct object_buffer *obj_buffer = BUFFER(filters[i]);
6446
6447         if (!obj_buffer ||
6448             !obj_buffer->buffer_store ||
6449             !obj_buffer->buffer_store->buffer)
6450             return VA_STATUS_ERROR_INVALID_BUFFER;
6451
6452         VAProcFilterParameterBufferBase *base = (VAProcFilterParameterBufferBase *)obj_buffer->buffer_store->buffer;
6453
6454         if (base->type == VAProcFilterNoiseReduction) {
6455             VAProcFilterParameterBuffer *denoise = (VAProcFilterParameterBuffer *)base;
6456             (void)denoise;
6457         } else if (base->type == VAProcFilterDeinterlacing) {
6458             VAProcFilterParameterBufferDeinterlacing *deint = (VAProcFilterParameterBufferDeinterlacing *)base;
6459
6460             ASSERT_RET(deint->algorithm == VAProcDeinterlacingBob ||
6461                    deint->algorithm == VAProcDeinterlacingMotionAdaptive ||
6462                    deint->algorithm == VAProcDeinterlacingMotionCompensated,
6463                    VA_STATUS_ERROR_INVALID_PARAMETER);
6464             
6465             if (deint->algorithm == VAProcDeinterlacingMotionAdaptive ||
6466                 deint->algorithm == VAProcDeinterlacingMotionCompensated)
6467                 pipeline_cap->num_forward_references++;
6468         } else if (base->type == VAProcFilterSkinToneEnhancement) {
6469                 VAProcFilterParameterBuffer *stde = (VAProcFilterParameterBuffer *)base;
6470                 (void)stde;
6471         }
6472     }
6473
6474     return VA_STATUS_SUCCESS;
6475 }
6476
6477 extern struct hw_codec_info *i965_get_codec_info(int devid);
6478
6479 static bool
6480 i965_driver_data_init(VADriverContextP ctx)
6481 {
6482     struct i965_driver_data *i965 = i965_driver_data(ctx); 
6483
6484     i965->codec_info = i965_get_codec_info(i965->intel.device_id);
6485
6486     if (!i965->codec_info)
6487         return false;
6488
6489     if (object_heap_init(&i965->config_heap,
6490                          sizeof(struct object_config),
6491                          CONFIG_ID_OFFSET))
6492         goto err_config_heap;
6493     if (object_heap_init(&i965->context_heap,
6494                          sizeof(struct object_context),
6495                          CONTEXT_ID_OFFSET))
6496         goto err_context_heap;
6497     
6498     if (object_heap_init(&i965->surface_heap,
6499                          sizeof(struct object_surface),
6500                          SURFACE_ID_OFFSET))
6501         goto err_surface_heap;
6502     if (object_heap_init(&i965->buffer_heap,
6503                          sizeof(struct object_buffer),
6504                          BUFFER_ID_OFFSET))
6505         goto err_buffer_heap;
6506     if (object_heap_init(&i965->image_heap,
6507                          sizeof(struct object_image),
6508                          IMAGE_ID_OFFSET))
6509         goto err_image_heap;
6510     if (object_heap_init(&i965->subpic_heap,
6511                          sizeof(struct object_subpic),
6512                          SUBPIC_ID_OFFSET))
6513         goto err_subpic_heap;
6514
6515     i965->batch = intel_batchbuffer_new(&i965->intel, I915_EXEC_RENDER, 0);
6516     i965->pp_batch = intel_batchbuffer_new(&i965->intel, I915_EXEC_RENDER, 0);
6517     _i965InitMutex(&i965->render_mutex);
6518     _i965InitMutex(&i965->pp_mutex);
6519
6520     return true;
6521
6522 err_subpic_heap:    
6523     object_heap_destroy(&i965->image_heap);
6524 err_image_heap:
6525     object_heap_destroy(&i965->buffer_heap);
6526 err_buffer_heap:
6527     object_heap_destroy(&i965->surface_heap);
6528 err_surface_heap:
6529     object_heap_destroy(&i965->context_heap);
6530 err_context_heap:
6531     object_heap_destroy(&i965->config_heap);
6532 err_config_heap:
6533
6534     return false;
6535 }
6536
6537 static void
6538 i965_driver_data_terminate(VADriverContextP ctx)
6539 {
6540     struct i965_driver_data *i965 = i965_driver_data(ctx); 
6541
6542     _i965DestroyMutex(&i965->pp_mutex);
6543     _i965DestroyMutex(&i965->render_mutex);
6544
6545     if (i965->batch)
6546         intel_batchbuffer_free(i965->batch);
6547
6548     if (i965->pp_batch)
6549         intel_batchbuffer_free(i965->pp_batch);
6550
6551     i965_destroy_heap(&i965->subpic_heap, i965_destroy_subpic);
6552     i965_destroy_heap(&i965->image_heap, i965_destroy_image);
6553     i965_destroy_heap(&i965->buffer_heap, i965_destroy_buffer);
6554     i965_destroy_heap(&i965->surface_heap, i965_destroy_surface);
6555     i965_destroy_heap(&i965->context_heap, i965_destroy_context);
6556     i965_destroy_heap(&i965->config_heap, i965_destroy_config);
6557 }
6558
6559 struct {
6560     bool (*init)(VADriverContextP ctx);
6561     void (*terminate)(VADriverContextP ctx);
6562     int display_type;
6563 } i965_sub_ops[] =  {
6564     {   
6565         intel_driver_init,
6566         intel_driver_terminate,
6567         0,
6568     },
6569
6570     {
6571         i965_driver_data_init,
6572         i965_driver_data_terminate,
6573         0,
6574     },
6575
6576     {
6577         i965_display_attributes_init,
6578         i965_display_attributes_terminate,
6579         0,
6580     },
6581
6582     {
6583         i965_post_processing_init,
6584         i965_post_processing_terminate,
6585         0,
6586     },
6587
6588     {
6589         i965_render_init,
6590         i965_render_terminate,
6591         0,
6592     },
6593
6594 #ifdef HAVE_VA_WAYLAND
6595     {
6596         i965_output_wayland_init,
6597         i965_output_wayland_terminate,
6598         VA_DISPLAY_WAYLAND,
6599     },
6600 #endif
6601
6602 #ifdef HAVE_VA_X11
6603     {
6604         i965_output_dri_init,
6605         i965_output_dri_terminate,
6606         VA_DISPLAY_X11,
6607     },
6608 #endif
6609
6610     {
6611         i965_gpe_table_init,
6612         i965_gpe_table_terminate,
6613         0,
6614     },
6615 };
6616
6617 static bool
6618 ensure_vendor_string(struct i965_driver_data *i965, const char *chipset)
6619 {
6620     int ret, len;
6621
6622     if (i965->va_vendor[0] != '\0')
6623         return true;
6624
6625     len = 0;
6626     ret = snprintf(i965->va_vendor, sizeof(i965->va_vendor),
6627         "%s %s driver for %s - %d.%d.%d",
6628         INTEL_STR_DRIVER_VENDOR, INTEL_STR_DRIVER_NAME, chipset,
6629         INTEL_DRIVER_MAJOR_VERSION, INTEL_DRIVER_MINOR_VERSION,
6630         INTEL_DRIVER_MICRO_VERSION);
6631     if (ret < 0 || ret >= sizeof(i965->va_vendor))
6632         goto error;
6633     len = ret;
6634
6635     if (INTEL_DRIVER_PRE_VERSION > 0) {
6636         ret = snprintf(&i965->va_vendor[len], sizeof(i965->va_vendor) - len,
6637             ".pre%d", INTEL_DRIVER_PRE_VERSION);
6638         if (ret < 0 || ret >= sizeof(i965->va_vendor))
6639             goto error;
6640         len += ret;
6641
6642         ret = snprintf(&i965->va_vendor[len], sizeof(i965->va_vendor) - len,
6643             " (%s)", INTEL_DRIVER_GIT_VERSION);
6644         if (ret < 0 || ret >= sizeof(i965->va_vendor))
6645             goto error;
6646         len += ret;
6647     }
6648     return true;
6649
6650 error:
6651     i965->va_vendor[0] = '\0';
6652     ASSERT_RET(ret > 0 && len < sizeof(i965->va_vendor), false);
6653     return false;
6654 }
6655
6656 /* Only when the option of "enable-wrapper" is passed, it is possible
6657  * to initialize/load the wrapper context of backend driver.
6658  * Otherwise it is not loaded.
6659  */
6660 #if HAVE_HYBRID_CODEC
6661
6662 static VAStatus
6663 i965_initialize_wrapper(VADriverContextP ctx, const char *driver_name)
6664 {
6665 #define DRIVER_EXTENSION        "_drv_video.so"
6666
6667     struct i965_driver_data *i965 = i965_driver_data(ctx);
6668
6669     VADriverContextP wrapper_pdrvctx;
6670     struct VADriverVTable *vtable;
6671     char *search_path, *driver_dir;
6672     char *saveptr;
6673     char driver_path[256];
6674     void *handle = NULL;
6675     VAStatus va_status = VA_STATUS_SUCCESS;
6676     bool driver_loaded = false;
6677
6678     if (HAS_VP9_DECODING(i965)) {
6679         i965->wrapper_pdrvctx = NULL;
6680         return va_status;
6681     }
6682
6683     wrapper_pdrvctx = calloc(1, sizeof(*wrapper_pdrvctx));
6684     vtable = calloc(1, sizeof(*vtable));
6685
6686     if (!wrapper_pdrvctx || !vtable) {
6687         fprintf(stderr, "Failed to allocate memory for wrapper \n");
6688         free(wrapper_pdrvctx);
6689         free(vtable);
6690         return VA_STATUS_ERROR_ALLOCATION_FAILED;
6691     }
6692
6693     /* use the same drm_state with CTX */
6694     wrapper_pdrvctx->drm_state = ctx->drm_state;
6695     wrapper_pdrvctx->display_type = ctx->display_type;
6696     wrapper_pdrvctx->vtable = vtable;
6697
6698     search_path = VA_DRIVERS_PATH;
6699     search_path = strdup((const char *)search_path);
6700
6701     driver_dir = strtok_r(search_path, ":", &saveptr);
6702     while (driver_dir && !driver_loaded) {
6703         memset(driver_path, 0, sizeof(driver_path));
6704         sprintf(driver_path, "%s/%s%s", driver_dir, driver_name, DRIVER_EXTENSION);
6705
6706         handle = dlopen(driver_path, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
6707         if (!handle) {
6708             fprintf(stderr, "failed to open %s\n", driver_path);
6709             driver_dir = strtok_r(NULL, ":", &saveptr);
6710             continue;
6711         }
6712         {
6713             VADriverInit init_func = NULL;
6714             char init_func_s[256];
6715             int i;
6716
6717             static const struct {
6718                 int major;
6719                 int minor;
6720             } compatible_versions[] = {
6721                 { VA_MAJOR_VERSION, VA_MINOR_VERSION },
6722                 { 0, 37 },
6723                 { 0, 36 },
6724                 { 0, 35 },
6725                 { 0, 34 },
6726                 { 0, 33 },
6727                 { 0, 32 },
6728                 { -1, }
6729             };
6730             for (i = 0; compatible_versions[i].major >= 0; i++) {
6731                 snprintf(init_func_s, sizeof(init_func_s),
6732                      "__vaDriverInit_%d_%d",
6733                      compatible_versions[i].major,
6734                      compatible_versions[i].minor);
6735                 init_func = (VADriverInit)dlsym(handle, init_func_s);
6736                 if (init_func) {
6737                     break;
6738                 }
6739             }
6740             if (compatible_versions[i].major < 0) {
6741                 dlclose(handle);
6742                 fprintf(stderr, "%s has no function %s\n",
6743                             driver_path, init_func_s);
6744                 driver_dir = strtok_r(NULL, ":", &saveptr);
6745                 continue;
6746             }
6747
6748             if (init_func)
6749                 va_status = (*init_func)(wrapper_pdrvctx);
6750
6751             if (va_status != VA_STATUS_SUCCESS) {
6752                 dlclose(handle);
6753                 fprintf(stderr, "%s init failed\n", driver_path);
6754                 driver_dir = strtok_r(NULL, ":", &saveptr);
6755                 continue;
6756             }
6757
6758             wrapper_pdrvctx->handle = handle;
6759             driver_loaded = true;
6760         }
6761     }
6762
6763     free(search_path);
6764
6765     if (driver_loaded) {
6766         i965->wrapper_pdrvctx = wrapper_pdrvctx;
6767         return VA_STATUS_SUCCESS;
6768     } else {
6769         fprintf(stderr, "Failed to wrapper %s%s\n", driver_name, DRIVER_EXTENSION);
6770         free(vtable);
6771         free(wrapper_pdrvctx);
6772         return VA_STATUS_ERROR_OPERATION_FAILED;
6773     }
6774 }
6775 #endif
6776
6777 static VAStatus 
6778 i965_Init(VADriverContextP ctx)
6779 {
6780     struct i965_driver_data *i965 = i965_driver_data(ctx); 
6781     int i;
6782     const char *chipset;
6783
6784     for (i = 0; i < ARRAY_ELEMS(i965_sub_ops); i++) {
6785         if ((i965_sub_ops[i].display_type == 0 ||
6786              i965_sub_ops[i].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) &&
6787             !i965_sub_ops[i].init(ctx))
6788             break;
6789     }
6790
6791     if (i == ARRAY_ELEMS(i965_sub_ops)) {
6792         switch (i965->intel.device_id) {
6793 #undef CHIPSET
6794 #define CHIPSET(id, family, dev, str) case id: chipset = str; break;
6795 #include "i965_pciids.h"
6796         default:
6797             chipset = "Unknown Intel Chipset";
6798             break;
6799         }
6800
6801         if (!ensure_vendor_string(i965, chipset))
6802             return VA_STATUS_ERROR_ALLOCATION_FAILED;
6803
6804         i965->current_context_id = VA_INVALID_ID;
6805
6806         if (i965->codec_info && i965->codec_info->preinit_hw_codec)
6807             i965->codec_info->preinit_hw_codec(ctx, i965->codec_info);
6808
6809 #if HAVE_HYBRID_CODEC
6810         i965_initialize_wrapper(ctx, "hybrid");
6811 #endif
6812
6813         return VA_STATUS_SUCCESS;
6814     } else {
6815         i--;
6816
6817         for (; i >= 0; i--) {
6818             if (i965_sub_ops[i].display_type == 0 ||
6819                 i965_sub_ops[i].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) {
6820                 i965_sub_ops[i].terminate(ctx);
6821             }
6822         }
6823
6824         return VA_STATUS_ERROR_UNKNOWN;
6825     }
6826 }
6827
6828 VAStatus 
6829 i965_Terminate(VADriverContextP ctx)
6830 {
6831     struct i965_driver_data *i965 = i965_driver_data(ctx);
6832     int i;
6833
6834     if (i965) {
6835 #if HAVE_HYBRID_CODEC
6836         if (i965->wrapper_pdrvctx) {
6837             VADriverContextP pdrvctx;
6838             pdrvctx = i965->wrapper_pdrvctx;
6839             if (pdrvctx->handle) {
6840                 pdrvctx->vtable->vaTerminate(pdrvctx);
6841                 dlclose(pdrvctx->handle);
6842                 pdrvctx->handle = NULL;
6843             }
6844             free(pdrvctx->vtable);
6845             free(pdrvctx);
6846             i965->wrapper_pdrvctx = NULL;
6847         }
6848 #endif
6849
6850         for (i = ARRAY_ELEMS(i965_sub_ops); i > 0; i--)
6851             if (i965_sub_ops[i - 1].display_type == 0 ||
6852                 i965_sub_ops[i - 1].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) {
6853                 i965_sub_ops[i - 1].terminate(ctx);
6854             }
6855
6856         free(i965);
6857         ctx->pDriverData = NULL;        
6858     }
6859
6860     return VA_STATUS_SUCCESS;
6861 }
6862
6863 VAStatus DLL_EXPORT
6864 VA_DRIVER_INIT_FUNC(VADriverContextP ctx);
6865
6866 VAStatus 
6867 VA_DRIVER_INIT_FUNC(  VADriverContextP ctx )
6868 {
6869     struct VADriverVTable * const vtable = ctx->vtable;
6870     struct VADriverVTableVPP * const vtable_vpp = ctx->vtable_vpp;
6871
6872     struct i965_driver_data *i965;
6873     VAStatus ret = VA_STATUS_ERROR_UNKNOWN;
6874
6875     ctx->version_major = VA_MAJOR_VERSION;
6876     ctx->version_minor = VA_MINOR_VERSION;
6877     ctx->max_profiles = I965_MAX_PROFILES;
6878     ctx->max_entrypoints = I965_MAX_ENTRYPOINTS;
6879     ctx->max_attributes = I965_MAX_CONFIG_ATTRIBUTES;
6880     ctx->max_image_formats = I965_MAX_IMAGE_FORMATS;
6881     ctx->max_subpic_formats = I965_MAX_SUBPIC_FORMATS;
6882     ctx->max_display_attributes = 1 + ARRAY_ELEMS(i965_display_attributes);
6883
6884     vtable->vaTerminate = i965_Terminate;
6885     vtable->vaQueryConfigEntrypoints = i965_QueryConfigEntrypoints;
6886     vtable->vaQueryConfigProfiles = i965_QueryConfigProfiles;
6887     vtable->vaQueryConfigAttributes = i965_QueryConfigAttributes;
6888     vtable->vaCreateConfig = i965_CreateConfig;
6889     vtable->vaDestroyConfig = i965_DestroyConfig;
6890     vtable->vaGetConfigAttributes = i965_GetConfigAttributes;
6891     vtable->vaCreateSurfaces = i965_CreateSurfaces;
6892     vtable->vaDestroySurfaces = i965_DestroySurfaces;
6893     vtable->vaCreateContext = i965_CreateContext;
6894     vtable->vaDestroyContext = i965_DestroyContext;
6895     vtable->vaCreateBuffer = i965_CreateBuffer;
6896     vtable->vaBufferSetNumElements = i965_BufferSetNumElements;
6897     vtable->vaMapBuffer = i965_MapBuffer;
6898     vtable->vaUnmapBuffer = i965_UnmapBuffer;
6899     vtable->vaDestroyBuffer = i965_DestroyBuffer;
6900     vtable->vaBeginPicture = i965_BeginPicture;
6901     vtable->vaRenderPicture = i965_RenderPicture;
6902     vtable->vaEndPicture = i965_EndPicture;
6903     vtable->vaSyncSurface = i965_SyncSurface;
6904     vtable->vaQuerySurfaceStatus = i965_QuerySurfaceStatus;
6905     vtable->vaPutSurface = i965_PutSurface;
6906     vtable->vaQueryImageFormats = i965_QueryImageFormats;
6907     vtable->vaCreateImage = i965_CreateImage;
6908     vtable->vaDeriveImage = i965_DeriveImage;
6909     vtable->vaDestroyImage = i965_DestroyImage;
6910     vtable->vaSetImagePalette = i965_SetImagePalette;
6911     vtable->vaGetImage = i965_GetImage;
6912     vtable->vaPutImage = i965_PutImage;
6913     vtable->vaQuerySubpictureFormats = i965_QuerySubpictureFormats;
6914     vtable->vaCreateSubpicture = i965_CreateSubpicture;
6915     vtable->vaDestroySubpicture = i965_DestroySubpicture;
6916     vtable->vaSetSubpictureImage = i965_SetSubpictureImage;
6917     vtable->vaSetSubpictureChromakey = i965_SetSubpictureChromakey;
6918     vtable->vaSetSubpictureGlobalAlpha = i965_SetSubpictureGlobalAlpha;
6919     vtable->vaAssociateSubpicture = i965_AssociateSubpicture;
6920     vtable->vaDeassociateSubpicture = i965_DeassociateSubpicture;
6921     vtable->vaQueryDisplayAttributes = i965_QueryDisplayAttributes;
6922     vtable->vaGetDisplayAttributes = i965_GetDisplayAttributes;
6923     vtable->vaSetDisplayAttributes = i965_SetDisplayAttributes;
6924     vtable->vaBufferInfo = i965_BufferInfo;
6925     vtable->vaLockSurface = i965_LockSurface;
6926     vtable->vaUnlockSurface = i965_UnlockSurface;
6927     vtable->vaGetSurfaceAttributes = i965_GetSurfaceAttributes;
6928     vtable->vaQuerySurfaceAttributes = i965_QuerySurfaceAttributes;
6929     vtable->vaCreateSurfaces2 = i965_CreateSurfaces2;
6930
6931     /* 0.36.0 */
6932     vtable->vaAcquireBufferHandle = i965_AcquireBufferHandle;
6933     vtable->vaReleaseBufferHandle = i965_ReleaseBufferHandle;
6934
6935     vtable_vpp->vaQueryVideoProcFilters = i965_QueryVideoProcFilters;
6936     vtable_vpp->vaQueryVideoProcFilterCaps = i965_QueryVideoProcFilterCaps;
6937     vtable_vpp->vaQueryVideoProcPipelineCaps = i965_QueryVideoProcPipelineCaps;
6938
6939     i965 = (struct i965_driver_data *)calloc(1, sizeof(*i965));
6940
6941     if (i965 == NULL) {
6942         ctx->pDriverData = NULL;
6943
6944         return VA_STATUS_ERROR_ALLOCATION_FAILED;
6945     }
6946
6947     i965->wrapper_pdrvctx = NULL;
6948     ctx->pDriverData = (void *)i965;
6949     ret = i965_Init(ctx);
6950
6951     if (ret == VA_STATUS_SUCCESS) {
6952         ctx->str_vendor = i965->va_vendor;
6953     } else {
6954         free(i965);
6955         ctx->pDriverData = NULL;
6956     }
6957
6958     return ret;
6959 }