OSDN Git Service

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