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