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