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