OSDN Git Service

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