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