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