OSDN Git Service

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