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