Guess the format of a VA surface in vaDeriveImage()
[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                                 (ctx)->render_state.interleaved_uv)
69
70 #define HAS_ENCODER(ctx)        ((IS_GEN7((ctx)->intel.device_id) ||    \
71                                   IS_GEN6((ctx)->intel.device_id)) &&   \
72                                  (ctx)->intel.has_bsd)
73
74 #define HAS_VPP(ctx)    (IS_IRONLAKE((ctx)->intel.device_id) ||     \
75                          IS_GEN6((ctx)->intel.device_id) ||         \
76                          IS_GEN7((ctx)->intel.device_id))
77
78 #define HAS_JPEG(ctx)   (IS_GEN7((ctx)->intel.device_id) &&     \
79                          (ctx)->intel.has_bsd)
80
81 #define HAS_ACCELERATED_GETIMAGE(ctx)   (IS_GEN6((ctx)->intel.device_id) ||     \
82                                          IS_GEN7((ctx)->intel.device_id))
83
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, VAProfile);
152 extern struct hw_context *g4x_dec_hw_context_init(VADriverContextP, VAProfile);
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, VAProfile);
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, VAProfile);
167 extern struct hw_context *gen6_enc_hw_context_init(VADriverContextP, VAProfile);
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, VAProfile);
175 extern struct hw_context *gen7_enc_hw_context_init(VADriverContextP, VAProfile);
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             attrib_list[i].value = VA_RC_VBR;
295             break;
296
297         case VAConfigAttribEncHeaderPacking:
298             if (entrypoint == VAEntrypointEncSlice) {
299                 attrib_list[i].value = VA_ENC_HEADER_PACKING_SLICE;
300                 break;
301             }
302
303         default:
304             /* Do nothing */
305             attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
306             break;
307         }
308     }
309
310     return VA_STATUS_SUCCESS;
311 }
312
313 static void 
314 i965_destroy_config(struct object_heap *heap, struct object_base *obj)
315 {
316     object_heap_free(heap, obj);
317 }
318
319 static VAStatus 
320 i965_update_attribute(struct object_config *obj_config, VAConfigAttrib *attrib)
321 {
322     int i;
323
324     /* Check existing attrbiutes */
325     for (i = 0; obj_config->num_attribs < i; i++) {
326         if (obj_config->attrib_list[i].type == attrib->type) {
327             /* Update existing attribute */
328             obj_config->attrib_list[i].value = attrib->value;
329             return VA_STATUS_SUCCESS;
330         }
331     }
332
333     if (obj_config->num_attribs < I965_MAX_CONFIG_ATTRIBUTES) {
334         i = obj_config->num_attribs;
335         obj_config->attrib_list[i].type = attrib->type;
336         obj_config->attrib_list[i].value = attrib->value;
337         obj_config->num_attribs++;
338         return VA_STATUS_SUCCESS;
339     }
340
341     return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
342 }
343
344 VAStatus 
345 i965_CreateConfig(VADriverContextP ctx,
346                   VAProfile profile,
347                   VAEntrypoint entrypoint,
348                   VAConfigAttrib *attrib_list,
349                   int num_attribs,
350                   VAConfigID *config_id)        /* out */
351 {
352     struct i965_driver_data * const i965 = i965_driver_data(ctx);
353     struct object_config *obj_config;
354     int configID;
355     int i;
356     VAStatus vaStatus;
357
358     /* Validate profile & entrypoint */
359     switch (profile) {
360     case VAProfileMPEG2Simple:
361     case VAProfileMPEG2Main:
362         if (HAS_MPEG2(i965) && VAEntrypointVLD == entrypoint) {
363             vaStatus = VA_STATUS_SUCCESS;
364         } else {
365             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
366         }
367         break;
368
369     case VAProfileH264Baseline:
370     case VAProfileH264Main:
371     case VAProfileH264High:
372         if ((HAS_H264(i965) && VAEntrypointVLD == entrypoint) ||
373             (HAS_ENCODER(i965) && VAEntrypointEncSlice == entrypoint)) {
374             vaStatus = VA_STATUS_SUCCESS;
375         } else {
376             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
377         }
378
379         break;
380
381     case VAProfileVC1Simple:
382     case VAProfileVC1Main:
383     case VAProfileVC1Advanced:
384         if (HAS_VC1(i965) && VAEntrypointVLD == entrypoint) {
385             vaStatus = VA_STATUS_SUCCESS;
386         } else {
387             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
388         }
389
390         break;
391
392     case VAProfileNone:
393         if (HAS_VPP(i965) && VAEntrypointVideoProc == entrypoint) {
394             vaStatus = VA_STATUS_SUCCESS;
395         } else {
396             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
397         }
398
399         break;
400
401     case VAProfileJPEGBaseline:
402         if (HAS_JPEG(i965) && VAEntrypointVLD == entrypoint) {
403             vaStatus = VA_STATUS_SUCCESS;
404         } else {
405             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
406         }
407
408         break;
409     default:
410         vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
411         break;
412     }
413
414     if (VA_STATUS_SUCCESS != vaStatus) {
415         return vaStatus;
416     }
417
418     configID = NEW_CONFIG_ID();
419     obj_config = CONFIG(configID);
420
421     if (NULL == obj_config) {
422         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
423         return vaStatus;
424     }
425
426     obj_config->profile = profile;
427     obj_config->entrypoint = entrypoint;
428     obj_config->attrib_list[0].type = VAConfigAttribRTFormat;
429     obj_config->attrib_list[0].value = VA_RT_FORMAT_YUV420;
430     obj_config->num_attribs = 1;
431
432     for(i = 0; i < num_attribs; i++) {
433         vaStatus = i965_update_attribute(obj_config, &(attrib_list[i]));
434
435         if (VA_STATUS_SUCCESS != vaStatus) {
436             break;
437         }
438     }
439
440     /* Error recovery */
441     if (VA_STATUS_SUCCESS != vaStatus) {
442         i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
443     } else {
444         *config_id = configID;
445     }
446
447     return vaStatus;
448 }
449
450 VAStatus 
451 i965_DestroyConfig(VADriverContextP ctx, VAConfigID config_id)
452 {
453     struct i965_driver_data *i965 = i965_driver_data(ctx);
454     struct object_config *obj_config = CONFIG(config_id);
455     VAStatus vaStatus;
456
457     if (NULL == obj_config) {
458         vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
459         return vaStatus;
460     }
461
462     i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
463     return VA_STATUS_SUCCESS;
464 }
465
466 VAStatus i965_QueryConfigAttributes(VADriverContextP ctx,
467                                     VAConfigID config_id,
468                                     VAProfile *profile,                 /* out */
469                                     VAEntrypoint *entrypoint,           /* out */
470                                     VAConfigAttrib *attrib_list,        /* out */
471                                     int *num_attribs)                   /* out */
472 {
473     struct i965_driver_data *i965 = i965_driver_data(ctx);
474     struct object_config *obj_config = CONFIG(config_id);
475     VAStatus vaStatus = VA_STATUS_SUCCESS;
476     int i;
477
478     assert(obj_config);
479     *profile = obj_config->profile;
480     *entrypoint = obj_config->entrypoint;
481     *num_attribs = obj_config->num_attribs;
482
483     for(i = 0; i < obj_config->num_attribs; i++) {
484         attrib_list[i] = obj_config->attrib_list[i];
485     }
486
487     return vaStatus;
488 }
489
490 static void 
491 i965_destroy_surface(struct object_heap *heap, struct object_base *obj)
492 {
493     struct object_surface *obj_surface = (struct object_surface *)obj;
494
495     dri_bo_unreference(obj_surface->bo);
496     obj_surface->bo = NULL;
497
498     if (obj_surface->free_private_data != NULL) {
499         obj_surface->free_private_data(&obj_surface->private_data);
500         obj_surface->private_data = NULL;
501     }
502
503     object_heap_free(heap, obj);
504 }
505
506 VAStatus 
507 i965_CreateSurfaces(VADriverContextP ctx,
508                     int width,
509                     int height,
510                     int format,
511                     int num_surfaces,
512                     VASurfaceID *surfaces)      /* out */
513 {
514     struct i965_driver_data *i965 = i965_driver_data(ctx);
515     int i;
516     VAStatus vaStatus = VA_STATUS_SUCCESS;
517
518     /* We only support one format */
519     if (VA_RT_FORMAT_YUV420 != format) {
520         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
521     }
522
523     for (i = 0; i < num_surfaces; i++) {
524         int surfaceID = NEW_SURFACE_ID();
525         struct object_surface *obj_surface = SURFACE(surfaceID);
526
527         if (NULL == obj_surface) {
528             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
529             break;
530         }
531
532         surfaces[i] = surfaceID;
533         obj_surface->status = VASurfaceReady;
534         obj_surface->subpic = VA_INVALID_ID;
535         obj_surface->orig_width = width;
536         obj_surface->orig_height = height;
537
538         obj_surface->width = ALIGN(width, 16);
539         obj_surface->height = ALIGN(height, 16);
540         obj_surface->flags = SURFACE_REFERENCED;
541         obj_surface->fourcc = 0;
542         obj_surface->bo = NULL;
543         obj_surface->locked_image_id = VA_INVALID_ID;
544         obj_surface->private_data = NULL;
545         obj_surface->free_private_data = NULL;
546         obj_surface->subsampling = SUBSAMPLE_YUV420;
547     }
548
549     /* Error recovery */
550     if (VA_STATUS_SUCCESS != vaStatus) {
551         /* surfaces[i-1] was the last successful allocation */
552         for (; i--; ) {
553             struct object_surface *obj_surface = SURFACE(surfaces[i]);
554
555             surfaces[i] = VA_INVALID_SURFACE;
556             assert(obj_surface);
557             i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
558         }
559     }
560
561     return vaStatus;
562 }
563
564 VAStatus 
565 i965_DestroySurfaces(VADriverContextP ctx,
566                      VASurfaceID *surface_list,
567                      int num_surfaces)
568 {
569     struct i965_driver_data *i965 = i965_driver_data(ctx);
570     int i;
571
572     for (i = num_surfaces; i--; ) {
573         struct object_surface *obj_surface = SURFACE(surface_list[i]);
574
575         assert(obj_surface);
576         i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
577     }
578
579     return VA_STATUS_SUCCESS;
580 }
581
582 VAStatus 
583 i965_QueryImageFormats(VADriverContextP ctx,
584                        VAImageFormat *format_list,      /* out */
585                        int *num_formats)                /* out */
586 {
587     int n;
588
589     for (n = 0; i965_image_formats_map[n].va_format.fourcc != 0; n++) {
590         const i965_image_format_map_t * const m = &i965_image_formats_map[n];
591         if (format_list)
592             format_list[n] = m->va_format;
593     }
594
595     if (num_formats)
596         *num_formats = n;
597
598     return VA_STATUS_SUCCESS;
599 }
600
601 /*
602  * Guess the format when the usage of a VA surface is unknown
603  * 1. Without a valid context: YV12
604  * 2. The current context is valid:
605  *    a) always NV12 on GEN6 and later
606  *    b) I420 for MPEG-2 and NV12 for other codec on GEN4 & GEN5
607  */
608 static void
609 i965_guess_surface_format(VADriverContextP ctx,
610                           VASurfaceID surface,
611                           unsigned int *fourcc,
612                           unsigned int *is_tiled)
613 {
614     struct i965_driver_data *i965 = i965_driver_data(ctx);
615     struct object_context *obj_context = NULL;
616     struct object_config *obj_config = NULL;
617
618     *fourcc = VA_FOURCC('Y', 'V', '1', '2');
619     *is_tiled = 0;
620
621     if (i965->current_context_id == VA_INVALID_ID)
622         return;
623
624     obj_context = CONTEXT(i965->current_context_id);
625
626     if (!obj_context || obj_context->config_id == VA_INVALID_ID)
627         return;
628
629     obj_config = CONFIG(obj_context->config_id);
630
631     if (!obj_config)
632         return;
633
634     if (IS_GEN6(i965->intel.device_id) || IS_GEN7(i965->intel.device_id)) {
635         *fourcc = VA_FOURCC('N', 'V', '1', '2');
636         *is_tiled = 1;
637         return;
638     }
639
640     switch (obj_config->profile) {
641     case VAProfileMPEG2Simple:
642     case VAProfileMPEG2Main:
643         *fourcc = VA_FOURCC('I', '4', '2', '0');
644         *is_tiled = 0;
645         break;
646
647     default:
648         *fourcc = VA_FOURCC('N', 'V', '1', '2');
649         *is_tiled = 0;
650         break;
651     }
652 }
653
654 VAStatus 
655 i965_PutImage(VADriverContextP ctx,
656               VASurfaceID surface,
657               VAImageID image,
658               int src_x,
659               int src_y,
660               unsigned int src_width,
661               unsigned int src_height,
662               int dest_x,
663               int dest_y,
664               unsigned int dest_width,
665               unsigned int dest_height)
666 {
667     struct i965_driver_data *i965 = i965_driver_data(ctx);
668     struct object_surface *obj_surface = SURFACE(surface);
669     struct object_image *obj_image = IMAGE(image);
670     struct i965_surface src_surface, dst_surface;
671     VAStatus va_status = VA_STATUS_SUCCESS;
672     VARectangle src_rect, dst_rect;
673
674     if (!obj_surface)
675         return VA_STATUS_ERROR_INVALID_SURFACE;
676
677     if (!obj_image || !obj_image->bo)
678         return VA_STATUS_ERROR_INVALID_IMAGE;
679
680     if (src_x < 0 ||
681         src_y < 0 ||
682         src_x + src_width > obj_image->image.width ||
683         src_y + src_height > obj_image->image.height)
684         return VA_STATUS_ERROR_INVALID_PARAMETER;
685
686     if (dest_x < 0 ||
687         dest_y < 0 ||
688         dest_x + dest_width > obj_surface->orig_width ||
689         dest_y + dest_height > obj_surface->orig_height)
690         return VA_STATUS_ERROR_INVALID_PARAMETER;
691
692     i965_check_alloc_surface_bo(ctx, obj_surface, HAS_TILED_SURFACE(i965), VA_FOURCC('N', 'V', '1', '2'), SUBSAMPLE_YUV420);
693
694     src_surface.id = image;
695     src_surface.type = I965_SURFACE_TYPE_IMAGE;
696     src_surface.flags = I965_SURFACE_FLAG_FRAME;
697     src_rect.x = src_x;
698     src_rect.y = src_y;
699     src_rect.width = src_width;
700     src_rect.height = src_height;
701
702     dst_surface.id = surface;
703     dst_surface.type = I965_SURFACE_TYPE_SURFACE;
704     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
705     dst_rect.x = dest_x;
706     dst_rect.y = dest_y;
707     dst_rect.width = dest_width;
708     dst_rect.height = dest_height;
709
710     va_status = i965_image_processing(ctx,
711                                       &src_surface,
712                                       &src_rect,
713                                       &dst_surface,
714                                       &dst_rect);
715
716     return  va_status;
717 }
718
719 VAStatus 
720 i965_QuerySubpictureFormats(VADriverContextP ctx,
721                             VAImageFormat *format_list,         /* out */
722                             unsigned int *flags,                /* out */
723                             unsigned int *num_formats)          /* out */
724 {
725     int n;
726
727     for (n = 0; i965_subpic_formats_map[n].va_format.fourcc != 0; n++) {
728         const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[n];
729         if (format_list)
730             format_list[n] = m->va_format;
731         if (flags)
732             flags[n] = m->va_flags;
733     }
734
735     if (num_formats)
736         *num_formats = n;
737
738     return VA_STATUS_SUCCESS;
739 }
740
741 static void 
742 i965_destroy_subpic(struct object_heap *heap, struct object_base *obj)
743 {
744     //    struct object_subpic *obj_subpic = (struct object_subpic *)obj;
745
746     object_heap_free(heap, obj);
747 }
748
749 VAStatus 
750 i965_CreateSubpicture(VADriverContextP ctx,
751                       VAImageID image,
752                       VASubpictureID *subpicture)         /* out */
753 {
754     struct i965_driver_data *i965 = i965_driver_data(ctx);
755     VASubpictureID subpicID = NEW_SUBPIC_ID()
756     struct object_subpic *obj_subpic = SUBPIC(subpicID);
757
758     if (!obj_subpic)
759         return VA_STATUS_ERROR_ALLOCATION_FAILED;
760
761     struct object_image *obj_image = IMAGE(image);
762     if (!obj_image)
763         return VA_STATUS_ERROR_INVALID_IMAGE;
764
765     const i965_subpic_format_map_t * const m = get_subpic_format(&obj_image->image.format);
766     if (!m)
767         return VA_STATUS_ERROR_UNKNOWN; /* XXX: VA_STATUS_ERROR_UNSUPPORTED_FORMAT? */
768
769     *subpicture = subpicID;
770     obj_subpic->image  = image;
771     obj_subpic->format = m->format;
772     obj_subpic->width  = obj_image->image.width;
773     obj_subpic->height = obj_image->image.height;
774     obj_subpic->pitch  = obj_image->image.pitches[0];
775     obj_subpic->bo     = obj_image->bo;
776     return VA_STATUS_SUCCESS;
777 }
778
779 VAStatus 
780 i965_DestroySubpicture(VADriverContextP ctx,
781                        VASubpictureID subpicture)
782 {
783     struct i965_driver_data *i965 = i965_driver_data(ctx);
784     struct object_subpic *obj_subpic = SUBPIC(subpicture);
785     i965_destroy_subpic(&i965->subpic_heap, (struct object_base *)obj_subpic);
786     return VA_STATUS_SUCCESS;
787 }
788
789 VAStatus 
790 i965_SetSubpictureImage(VADriverContextP ctx,
791                         VASubpictureID subpicture,
792                         VAImageID image)
793 {
794     /* TODO */
795     return VA_STATUS_ERROR_UNIMPLEMENTED;
796 }
797
798 VAStatus 
799 i965_SetSubpictureChromakey(VADriverContextP ctx,
800                             VASubpictureID subpicture,
801                             unsigned int chromakey_min,
802                             unsigned int chromakey_max,
803                             unsigned int chromakey_mask)
804 {
805     /* TODO */
806     return VA_STATUS_ERROR_UNIMPLEMENTED;
807 }
808
809 VAStatus 
810 i965_SetSubpictureGlobalAlpha(VADriverContextP ctx,
811                               VASubpictureID subpicture,
812                               float global_alpha)
813 {
814     /* TODO */
815     return VA_STATUS_ERROR_UNIMPLEMENTED;
816 }
817
818 VAStatus 
819 i965_AssociateSubpicture(VADriverContextP ctx,
820                          VASubpictureID subpicture,
821                          VASurfaceID *target_surfaces,
822                          int num_surfaces,
823                          short src_x, /* upper left offset in subpicture */
824                          short src_y,
825                          unsigned short src_width,
826                          unsigned short src_height,
827                          short dest_x, /* upper left offset in surface */
828                          short dest_y,
829                          unsigned short dest_width,
830                          unsigned short dest_height,
831                          /*
832                           * whether to enable chroma-keying or global-alpha
833                           * see VA_SUBPICTURE_XXX values
834                           */
835                          unsigned int flags)
836 {
837     struct i965_driver_data *i965 = i965_driver_data(ctx);
838     struct object_subpic *obj_subpic = SUBPIC(subpicture);
839     int i;
840
841     obj_subpic->src_rect.x      = src_x;
842     obj_subpic->src_rect.y      = src_y;
843     obj_subpic->src_rect.width  = src_width;
844     obj_subpic->src_rect.height = src_height;
845     obj_subpic->dst_rect.x      = dest_x;
846     obj_subpic->dst_rect.y      = dest_y;
847     obj_subpic->dst_rect.width  = dest_width;
848     obj_subpic->dst_rect.height = dest_height;
849     obj_subpic->flags           = flags;
850
851     for (i = 0; i < num_surfaces; i++) {
852         struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
853         if (!obj_surface)
854             return VA_STATUS_ERROR_INVALID_SURFACE;
855         obj_surface->subpic = subpicture;
856     }
857     return VA_STATUS_SUCCESS;
858 }
859
860
861 VAStatus 
862 i965_DeassociateSubpicture(VADriverContextP ctx,
863                            VASubpictureID subpicture,
864                            VASurfaceID *target_surfaces,
865                            int num_surfaces)
866 {
867     struct i965_driver_data *i965 = i965_driver_data(ctx);
868     int i;
869
870     for (i = 0; i < num_surfaces; i++) {
871         struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
872         if (!obj_surface)
873             return VA_STATUS_ERROR_INVALID_SURFACE;
874         if (obj_surface->subpic == subpicture)
875             obj_surface->subpic = VA_INVALID_ID;
876     }
877     return VA_STATUS_SUCCESS;
878 }
879
880 void
881 i965_reference_buffer_store(struct buffer_store **ptr, 
882                             struct buffer_store *buffer_store)
883 {
884     assert(*ptr == NULL);
885
886     if (buffer_store) {
887         buffer_store->ref_count++;
888         *ptr = buffer_store;
889     }
890 }
891
892 void 
893 i965_release_buffer_store(struct buffer_store **ptr)
894 {
895     struct buffer_store *buffer_store = *ptr;
896
897     if (buffer_store == NULL)
898         return;
899
900     assert(buffer_store->bo || buffer_store->buffer);
901     assert(!(buffer_store->bo && buffer_store->buffer));
902     buffer_store->ref_count--;
903     
904     if (buffer_store->ref_count == 0) {
905         dri_bo_unreference(buffer_store->bo);
906         free(buffer_store->buffer);
907         buffer_store->bo = NULL;
908         buffer_store->buffer = NULL;
909         free(buffer_store);
910     }
911
912     *ptr = NULL;
913 }
914
915 static void 
916 i965_destroy_context(struct object_heap *heap, struct object_base *obj)
917 {
918     struct object_context *obj_context = (struct object_context *)obj;
919     int i;
920
921     if (obj_context->hw_context) {
922         obj_context->hw_context->destroy(obj_context->hw_context);
923         obj_context->hw_context = NULL;
924     }
925
926     if (obj_context->codec_type == CODEC_PROC) {
927         i965_release_buffer_store(&obj_context->codec_state.proc.pipeline_param);
928         i965_release_buffer_store(&obj_context->codec_state.proc.input_param);
929
930         for (i = 0; i < VA_PROC_PIPELINE_MAX_NUM_FILTERS; i++)
931             i965_release_buffer_store(&obj_context->codec_state.proc.filter_param[i]);
932     } else if (obj_context->codec_type == CODEC_ENC) {
933         assert(obj_context->codec_state.encode.num_slice_params <= obj_context->codec_state.encode.max_slice_params);
934         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param);
935         i965_release_buffer_store(&obj_context->codec_state.encode.seq_param);
936
937         for (i = 0; i < obj_context->codec_state.encode.num_slice_params; i++)
938             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params[i]);
939
940         free(obj_context->codec_state.encode.slice_params);
941
942         assert(obj_context->codec_state.encode.num_slice_params_ext <= obj_context->codec_state.encode.max_slice_params_ext);
943         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param_ext);
944         i965_release_buffer_store(&obj_context->codec_state.encode.seq_param_ext);
945         i965_release_buffer_store(&obj_context->codec_state.encode.dec_ref_pic_marking);
946
947         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_param); i++)
948             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_param[i]);
949
950         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_data); i++)
951             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data[i]);
952
953         for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext; i++)
954             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params_ext[i]);
955
956         free(obj_context->codec_state.encode.slice_params_ext);
957     } else {
958         assert(obj_context->codec_state.decode.num_slice_params <= obj_context->codec_state.decode.max_slice_params);
959         assert(obj_context->codec_state.decode.num_slice_datas <= obj_context->codec_state.decode.max_slice_datas);
960
961         i965_release_buffer_store(&obj_context->codec_state.decode.pic_param);
962         i965_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
963         i965_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
964
965         for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++)
966             i965_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
967
968         for (i = 0; i < obj_context->codec_state.decode.num_slice_datas; i++)
969             i965_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
970
971         free(obj_context->codec_state.decode.slice_params);
972         free(obj_context->codec_state.decode.slice_datas);
973     }
974
975     free(obj_context->render_targets);
976     object_heap_free(heap, obj);
977 }
978
979 VAStatus
980 i965_CreateContext(VADriverContextP ctx,
981                    VAConfigID config_id,
982                    int picture_width,
983                    int picture_height,
984                    int flag,
985                    VASurfaceID *render_targets,
986                    int num_render_targets,
987                    VAContextID *context)                /* out */
988 {
989     struct i965_driver_data *i965 = i965_driver_data(ctx);
990     struct i965_render_state *render_state = &i965->render_state;
991     struct object_config *obj_config = CONFIG(config_id);
992     struct object_context *obj_context = NULL;
993     VAStatus vaStatus = VA_STATUS_SUCCESS;
994     int contextID;
995     int i;
996
997     if (NULL == obj_config) {
998         vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
999         return vaStatus;
1000     }
1001
1002     /* Validate flag */
1003     /* Validate picture dimensions */
1004     contextID = NEW_CONTEXT_ID();
1005     obj_context = CONTEXT(contextID);
1006
1007     if (NULL == obj_context) {
1008         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1009         return vaStatus;
1010     }
1011
1012     render_state->inited = 1;
1013
1014     switch (obj_config->profile) {
1015     case VAProfileH264Baseline:
1016     case VAProfileH264Main:
1017     case VAProfileH264High:
1018         if (!HAS_H264(i965))
1019             return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
1020         render_state->interleaved_uv = 1;
1021         break;
1022     default:
1023         render_state->interleaved_uv = !!(IS_GEN6(i965->intel.device_id) || IS_GEN7(i965->intel.device_id));
1024         break;
1025     }
1026
1027     *context = contextID;
1028     obj_context->flags = flag;
1029     obj_context->context_id = contextID;
1030     obj_context->config_id = config_id;
1031     obj_context->picture_width = picture_width;
1032     obj_context->picture_height = picture_height;
1033     obj_context->num_render_targets = num_render_targets;
1034     obj_context->render_targets = 
1035         (VASurfaceID *)calloc(num_render_targets, sizeof(VASurfaceID));
1036     obj_context->hw_context = NULL;
1037
1038     for(i = 0; i < num_render_targets; i++) {
1039         if (NULL == SURFACE(render_targets[i])) {
1040             vaStatus = VA_STATUS_ERROR_INVALID_SURFACE;
1041             break;
1042         }
1043
1044         obj_context->render_targets[i] = render_targets[i];
1045     }
1046
1047     if (VA_STATUS_SUCCESS == vaStatus) {
1048         if (VAEntrypointVideoProc == obj_config->entrypoint) {
1049             obj_context->codec_type = CODEC_PROC;
1050             memset(&obj_context->codec_state.proc, 0, sizeof(obj_context->codec_state.proc));
1051             obj_context->codec_state.proc.current_render_target = VA_INVALID_ID;
1052             assert(i965->codec_info->proc_hw_context_init);
1053             obj_context->hw_context = i965->codec_info->proc_hw_context_init(ctx, obj_config->profile);
1054         } else if (VAEntrypointEncSlice == obj_config->entrypoint) { /*encode routin only*/
1055             obj_context->codec_type = CODEC_ENC;
1056             memset(&obj_context->codec_state.encode, 0, sizeof(obj_context->codec_state.encode));
1057             obj_context->codec_state.encode.current_render_target = VA_INVALID_ID;
1058             obj_context->codec_state.encode.max_slice_params = NUM_SLICES;
1059             obj_context->codec_state.encode.slice_params = calloc(obj_context->codec_state.encode.max_slice_params,
1060                                                                sizeof(*obj_context->codec_state.encode.slice_params));
1061             assert(i965->codec_info->enc_hw_context_init);
1062             obj_context->hw_context = i965->codec_info->enc_hw_context_init(ctx, obj_config->profile);
1063         } else {
1064             obj_context->codec_type = CODEC_DEC;
1065             memset(&obj_context->codec_state.decode, 0, sizeof(obj_context->codec_state.decode));
1066             obj_context->codec_state.decode.current_render_target = -1;
1067             obj_context->codec_state.decode.max_slice_params = NUM_SLICES;
1068             obj_context->codec_state.decode.max_slice_datas = NUM_SLICES;
1069             obj_context->codec_state.decode.slice_params = calloc(obj_context->codec_state.decode.max_slice_params,
1070                                                                sizeof(*obj_context->codec_state.decode.slice_params));
1071             obj_context->codec_state.decode.slice_datas = calloc(obj_context->codec_state.decode.max_slice_datas,
1072                                                               sizeof(*obj_context->codec_state.decode.slice_datas));
1073
1074             assert(i965->codec_info->dec_hw_context_init);
1075             obj_context->hw_context = i965->codec_info->dec_hw_context_init(ctx, obj_config->profile);
1076         }
1077     }
1078
1079     /* Error recovery */
1080     if (VA_STATUS_SUCCESS != vaStatus) {
1081         i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
1082     }
1083
1084     i965->current_context_id = contextID;
1085
1086     return vaStatus;
1087 }
1088
1089 VAStatus 
1090 i965_DestroyContext(VADriverContextP ctx, VAContextID context)
1091 {
1092     struct i965_driver_data *i965 = i965_driver_data(ctx);
1093     struct object_context *obj_context = CONTEXT(context);
1094
1095     assert(obj_context);
1096
1097     if (i965->current_context_id == context)
1098         i965->current_context_id = VA_INVALID_ID;
1099
1100     i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
1101
1102     return VA_STATUS_SUCCESS;
1103 }
1104
1105 static void 
1106 i965_destroy_buffer(struct object_heap *heap, struct object_base *obj)
1107 {
1108     struct object_buffer *obj_buffer = (struct object_buffer *)obj;
1109
1110     assert(obj_buffer->buffer_store);
1111     i965_release_buffer_store(&obj_buffer->buffer_store);
1112     object_heap_free(heap, obj);
1113 }
1114
1115 static VAStatus
1116 i965_create_buffer_internal(VADriverContextP ctx,
1117                             VAContextID context,
1118                             VABufferType type,
1119                             unsigned int size,
1120                             unsigned int num_elements,
1121                             void *data,
1122                             dri_bo *store_bo,
1123                             VABufferID *buf_id)
1124 {
1125     struct i965_driver_data *i965 = i965_driver_data(ctx);
1126     struct object_buffer *obj_buffer = NULL;
1127     struct buffer_store *buffer_store = NULL;
1128     int bufferID;
1129
1130     /* Validate type */
1131     switch (type) {
1132     case VAPictureParameterBufferType:
1133     case VAIQMatrixBufferType:
1134     case VAQMatrixBufferType:
1135     case VABitPlaneBufferType:
1136     case VASliceGroupMapBufferType:
1137     case VASliceParameterBufferType:
1138     case VASliceDataBufferType:
1139     case VAMacroblockParameterBufferType:
1140     case VAResidualDataBufferType:
1141     case VADeblockingParameterBufferType:
1142     case VAImageBufferType:
1143     case VAEncCodedBufferType:
1144     case VAEncSequenceParameterBufferType:
1145     case VAEncPictureParameterBufferType:
1146     case VAEncSliceParameterBufferType:
1147     case VAEncDecRefPicMarkingBufferH264Type:
1148     case VAEncPackedHeaderParameterBufferType:
1149     case VAEncPackedHeaderDataBufferType:
1150     case VAProcPipelineParameterBufferType:
1151     case VAProcInputParameterBufferType:
1152     case VAProcFilterBaseParameterBufferType:
1153     case VAProcFilterDeinterlacingParameterBufferType:
1154     case VAProcFilterProcAmpParameterBufferType:
1155     case VAHuffmanTableBufferType:
1156         /* Ok */
1157         break;
1158
1159     default:
1160         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
1161     }
1162
1163     bufferID = NEW_BUFFER_ID();
1164     obj_buffer = BUFFER(bufferID);
1165
1166     if (NULL == obj_buffer) {
1167         return VA_STATUS_ERROR_ALLOCATION_FAILED;
1168     }
1169
1170     if (type == VAEncCodedBufferType) {
1171         size += ALIGN(sizeof(VACodedBufferSegment), 64);
1172         size += 0x1000; /* for upper bound check */
1173     }
1174
1175     obj_buffer->max_num_elements = num_elements;
1176     obj_buffer->num_elements = num_elements;
1177     obj_buffer->size_element = size;
1178     obj_buffer->type = type;
1179     obj_buffer->buffer_store = NULL;
1180     buffer_store = calloc(1, sizeof(struct buffer_store));
1181     assert(buffer_store);
1182     buffer_store->ref_count = 1;
1183
1184     if (store_bo != NULL) {
1185         buffer_store->bo = store_bo;
1186         dri_bo_reference(buffer_store->bo);
1187         
1188         if (data)
1189             dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
1190     } else if (type == VASliceDataBufferType || 
1191                type == VAImageBufferType || 
1192                type == VAEncCodedBufferType) {
1193         buffer_store->bo = dri_bo_alloc(i965->intel.bufmgr, 
1194                                         "Buffer", 
1195                                         size * num_elements, 64);
1196         assert(buffer_store->bo);
1197
1198         if (type == VAEncCodedBufferType) {
1199             VACodedBufferSegment *coded_buffer_segment;
1200             dri_bo_map(buffer_store->bo, 1);
1201             coded_buffer_segment = (VACodedBufferSegment *)buffer_store->bo->virtual;
1202             coded_buffer_segment->size = size - ALIGN(sizeof(VACodedBufferSegment), 64);
1203             coded_buffer_segment->bit_offset = 0;
1204             coded_buffer_segment->status = 0;
1205             coded_buffer_segment->buf = NULL;
1206             coded_buffer_segment->next = NULL;
1207             dri_bo_unmap(buffer_store->bo);
1208         } else if (data) {
1209             dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
1210         }
1211
1212     } else if (type == VAEncPackedHeaderParameterBufferType) {
1213         VAEncPackedHeaderParameterBuffer *param;
1214         int msize;
1215
1216         assert(data);
1217         assert(num_elements == 1);
1218         assert(size == sizeof(*param));
1219
1220         param = (VAEncPackedHeaderParameterBuffer *)data;
1221         msize = ALIGN(size, 32) + param->num_headers * sizeof(int) * 2;
1222         buffer_store->buffer = malloc(msize);
1223         assert(buffer_store->buffer);
1224
1225         memcpy(buffer_store->buffer,
1226                data, 
1227                size);
1228         memcpy((unsigned char *)buffer_store->buffer + ALIGN(size, 32),
1229                param->length_in_bits,
1230                param->num_headers * sizeof(int));
1231         memcpy((unsigned char *)buffer_store->buffer + ALIGN(size, 32) + param->num_headers * sizeof(int),
1232                param->offset_in_bytes,
1233                param->num_headers * sizeof(int));
1234
1235         param = (VAEncPackedHeaderParameterBuffer *)buffer_store->buffer;
1236         param->length_in_bits = (unsigned int *)((unsigned char *)buffer_store->buffer + ALIGN(size, 32));
1237         param->offset_in_bytes = (unsigned int *)((unsigned char *)buffer_store->buffer + ALIGN(size, 32) + param->num_headers * sizeof(int));
1238     } else {
1239         int msize = size;
1240         
1241         if (type == VAEncPackedHeaderDataBufferType) {
1242             msize = ALIGN(size, 4);
1243         }
1244
1245         buffer_store->buffer = malloc(msize * num_elements);
1246         assert(buffer_store->buffer);
1247
1248         if (data)
1249             memcpy(buffer_store->buffer, data, size * num_elements);
1250     }
1251
1252     buffer_store->num_elements = obj_buffer->num_elements;
1253     i965_reference_buffer_store(&obj_buffer->buffer_store, buffer_store);
1254     i965_release_buffer_store(&buffer_store);
1255     *buf_id = bufferID;
1256
1257     return VA_STATUS_SUCCESS;
1258 }
1259
1260 VAStatus 
1261 i965_CreateBuffer(VADriverContextP ctx,
1262                   VAContextID context,          /* in */
1263                   VABufferType type,            /* in */
1264                   unsigned int size,            /* in */
1265                   unsigned int num_elements,    /* in */
1266                   void *data,                   /* in */
1267                   VABufferID *buf_id)           /* out */
1268 {
1269     return i965_create_buffer_internal(ctx, context, type, size, num_elements, data, NULL, buf_id);
1270 }
1271
1272
1273 VAStatus 
1274 i965_BufferSetNumElements(VADriverContextP ctx,
1275                           VABufferID buf_id,           /* in */
1276                           unsigned int num_elements)   /* in */
1277 {
1278     struct i965_driver_data *i965 = i965_driver_data(ctx);
1279     struct object_buffer *obj_buffer = BUFFER(buf_id);
1280     VAStatus vaStatus = VA_STATUS_SUCCESS;
1281
1282     assert(obj_buffer);
1283
1284     if ((num_elements < 0) || 
1285         (num_elements > obj_buffer->max_num_elements)) {
1286         vaStatus = VA_STATUS_ERROR_UNKNOWN;
1287     } else {
1288         obj_buffer->num_elements = num_elements;
1289         if (obj_buffer->buffer_store != NULL) {
1290             obj_buffer->buffer_store->num_elements = num_elements;
1291         }
1292     }
1293
1294     return vaStatus;
1295 }
1296
1297 VAStatus 
1298 i965_MapBuffer(VADriverContextP ctx,
1299                VABufferID buf_id,       /* in */
1300                void **pbuf)             /* out */
1301 {
1302     struct i965_driver_data *i965 = i965_driver_data(ctx);
1303     struct object_buffer *obj_buffer = BUFFER(buf_id);
1304     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
1305
1306     assert(obj_buffer && obj_buffer->buffer_store);
1307     assert(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer);
1308     assert(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer));
1309
1310     if (NULL != obj_buffer->buffer_store->bo) {
1311         unsigned int tiling, swizzle;
1312
1313         dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);
1314
1315         if (tiling != I915_TILING_NONE)
1316             drm_intel_gem_bo_map_gtt(obj_buffer->buffer_store->bo);
1317         else
1318             dri_bo_map(obj_buffer->buffer_store->bo, 1);
1319
1320         assert(obj_buffer->buffer_store->bo->virtual);
1321         *pbuf = obj_buffer->buffer_store->bo->virtual;
1322
1323         if (obj_buffer->type == VAEncCodedBufferType) {
1324             int i;
1325             unsigned char *buffer = NULL;
1326             VACodedBufferSegment *coded_buffer_segment = (VACodedBufferSegment *)(obj_buffer->buffer_store->bo->virtual);
1327
1328             coded_buffer_segment->buf = buffer = (unsigned char *)(obj_buffer->buffer_store->bo->virtual) + ALIGN(sizeof(VACodedBufferSegment), 64);
1329             
1330             for (i = 0; i < obj_buffer->size_element - ALIGN(sizeof(VACodedBufferSegment), 64) - 3 - 0x1000; i++) {
1331                 if (!buffer[i] &&
1332                     !buffer[i + 1] &&
1333                     !buffer[i + 2] &&
1334                     !buffer[i + 3])
1335                     break;
1336             }
1337
1338             if (i == obj_buffer->size_element - ALIGN(sizeof(VACodedBufferSegment), 64) - 3 - 0x1000) {
1339                 coded_buffer_segment->status |= VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK;
1340             }
1341
1342             coded_buffer_segment->size = i;
1343         }
1344
1345         vaStatus = VA_STATUS_SUCCESS;
1346     } else if (NULL != obj_buffer->buffer_store->buffer) {
1347         *pbuf = obj_buffer->buffer_store->buffer;
1348         vaStatus = VA_STATUS_SUCCESS;
1349     }
1350
1351     return vaStatus;
1352 }
1353
1354 VAStatus 
1355 i965_UnmapBuffer(VADriverContextP ctx, VABufferID buf_id)
1356 {
1357     struct i965_driver_data *i965 = i965_driver_data(ctx);
1358     struct object_buffer *obj_buffer = BUFFER(buf_id);
1359     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
1360
1361     assert(obj_buffer && obj_buffer->buffer_store);
1362     assert(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer);
1363     assert(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer));
1364
1365     if (NULL != obj_buffer->buffer_store->bo) {
1366         unsigned int tiling, swizzle;
1367
1368         dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);
1369
1370         if (tiling != I915_TILING_NONE)
1371             drm_intel_gem_bo_unmap_gtt(obj_buffer->buffer_store->bo);
1372         else
1373             dri_bo_unmap(obj_buffer->buffer_store->bo);
1374
1375         vaStatus = VA_STATUS_SUCCESS;
1376     } else if (NULL != obj_buffer->buffer_store->buffer) {
1377         /* Do nothing */
1378         vaStatus = VA_STATUS_SUCCESS;
1379     }
1380
1381     return vaStatus;    
1382 }
1383
1384 VAStatus 
1385 i965_DestroyBuffer(VADriverContextP ctx, VABufferID buffer_id)
1386 {
1387     struct i965_driver_data *i965 = i965_driver_data(ctx);
1388     struct object_buffer *obj_buffer = BUFFER(buffer_id);
1389
1390     assert(obj_buffer);
1391     i965_destroy_buffer(&i965->buffer_heap, (struct object_base *)obj_buffer);
1392
1393     return VA_STATUS_SUCCESS;
1394 }
1395
1396 VAStatus 
1397 i965_BeginPicture(VADriverContextP ctx,
1398                   VAContextID context,
1399                   VASurfaceID render_target)
1400 {
1401     struct i965_driver_data *i965 = i965_driver_data(ctx); 
1402     struct object_context *obj_context = CONTEXT(context);
1403     struct object_surface *obj_surface = SURFACE(render_target);
1404     struct object_config *obj_config;
1405     VAContextID config;
1406     VAStatus vaStatus;
1407     int i;
1408
1409     assert(obj_context);
1410     assert(obj_surface);
1411
1412     config = obj_context->config_id;
1413     obj_config = CONFIG(config);
1414     assert(obj_config);
1415
1416     switch (obj_config->profile) {
1417     case VAProfileMPEG2Simple:
1418     case VAProfileMPEG2Main:
1419         vaStatus = VA_STATUS_SUCCESS;
1420         break;
1421
1422     case VAProfileH264Baseline:
1423     case VAProfileH264Main:
1424     case VAProfileH264High:
1425         vaStatus = VA_STATUS_SUCCESS;
1426         break;
1427
1428     case VAProfileVC1Simple:
1429     case VAProfileVC1Main:
1430     case VAProfileVC1Advanced:
1431         vaStatus = VA_STATUS_SUCCESS;
1432         break;
1433
1434     case VAProfileJPEGBaseline:
1435         vaStatus = VA_STATUS_SUCCESS;
1436         break;
1437
1438     case VAProfileNone:
1439         vaStatus = VA_STATUS_SUCCESS;
1440         break;
1441
1442     default:
1443         assert(0);
1444         vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
1445         break;
1446     }
1447
1448     if (obj_context->codec_type == CODEC_PROC) {
1449         obj_context->codec_state.proc.current_render_target = render_target;
1450     } else if (obj_context->codec_type == CODEC_ENC) {
1451         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param);
1452         i965_release_buffer_store(&obj_context->codec_state.encode.seq_param);
1453
1454         for (i = 0; i < obj_context->codec_state.encode.num_slice_params; i++) {
1455             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params[i]);
1456         }
1457
1458         obj_context->codec_state.encode.num_slice_params = 0;
1459
1460         /* ext */
1461         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param_ext);
1462         i965_release_buffer_store(&obj_context->codec_state.encode.seq_param_ext);
1463         i965_release_buffer_store(&obj_context->codec_state.encode.dec_ref_pic_marking);
1464
1465
1466         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_param); i++)
1467             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_param[i]);
1468
1469         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_data); i++)
1470             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data[i]);
1471
1472         for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext; i++)
1473             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params_ext[i]);
1474
1475         obj_context->codec_state.encode.num_slice_params_ext = 0;
1476         obj_context->codec_state.encode.current_render_target = render_target;     /*This is input new frame*/
1477         obj_context->codec_state.encode.last_packed_header_type = 0;
1478     } else {
1479         obj_context->codec_state.decode.current_render_target = render_target;
1480         i965_release_buffer_store(&obj_context->codec_state.decode.pic_param);
1481         i965_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
1482         i965_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
1483
1484         for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++) {
1485             i965_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
1486             i965_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
1487         }
1488
1489         obj_context->codec_state.decode.num_slice_params = 0;
1490         obj_context->codec_state.decode.num_slice_datas = 0;
1491     }
1492
1493     return vaStatus;
1494 }
1495
1496 #define I965_RENDER_BUFFER(category, name) i965_render_##category##_##name##_buffer(ctx, obj_context, obj_buffer)
1497
1498 #define DEF_RENDER_SINGLE_BUFFER_FUNC(category, name, member)           \
1499     static VAStatus                                                     \
1500     i965_render_##category##_##name##_buffer(VADriverContextP ctx,      \
1501                                              struct object_context *obj_context, \
1502                                              struct object_buffer *obj_buffer) \
1503     {                                                                   \
1504         struct category##_state *category = &obj_context->codec_state.category; \
1505         assert(obj_buffer->buffer_store->bo == NULL);                   \
1506         assert(obj_buffer->buffer_store->buffer);                       \
1507         i965_release_buffer_store(&category->member);                   \
1508         i965_reference_buffer_store(&category->member, obj_buffer->buffer_store); \
1509         return VA_STATUS_SUCCESS;                                       \
1510     }
1511
1512 #define DEF_RENDER_MULTI_BUFFER_FUNC(category, name, member)            \
1513     static VAStatus                                                     \
1514     i965_render_##category##_##name##_buffer(VADriverContextP ctx,      \
1515                                              struct object_context *obj_context, \
1516                                              struct object_buffer *obj_buffer) \
1517     {                                                                   \
1518         struct category##_state *category = &obj_context->codec_state.category; \
1519         if (category->num_##member == category->max_##member) {         \
1520             category->member = realloc(category->member, (category->max_##member + NUM_SLICES) * sizeof(*category->member)); \
1521             memset(category->member + category->max_##member, 0, NUM_SLICES * sizeof(*category->member)); \
1522             category->max_##member += NUM_SLICES;                       \
1523         }                                                               \
1524         i965_release_buffer_store(&category->member[category->num_##member]); \
1525         i965_reference_buffer_store(&category->member[category->num_##member], obj_buffer->buffer_store); \
1526         category->num_##member++;                                       \
1527         return VA_STATUS_SUCCESS;                                       \
1528     }
1529
1530 #define I965_RENDER_DECODE_BUFFER(name) I965_RENDER_BUFFER(decode, name)
1531
1532 #define DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(decode, name, member)
1533 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(picture_parameter, pic_param)
1534 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(iq_matrix, iq_matrix)
1535 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(bit_plane, bit_plane)
1536 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(huffman_table, huffman_table)
1537
1538 #define DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(name, member) DEF_RENDER_MULTI_BUFFER_FUNC(decode, name, member)
1539 DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(slice_parameter, slice_params)
1540 DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(slice_data, slice_datas)
1541
1542 static VAStatus 
1543 i965_decoder_render_picture(VADriverContextP ctx,
1544                             VAContextID context,
1545                             VABufferID *buffers,
1546                             int num_buffers)
1547 {
1548     struct i965_driver_data *i965 = i965_driver_data(ctx); 
1549     struct object_context *obj_context = CONTEXT(context);
1550     VAStatus vaStatus = VA_STATUS_SUCCESS;
1551     int i;
1552
1553     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
1554         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
1555         assert(obj_buffer);
1556
1557         switch (obj_buffer->type) {
1558         case VAPictureParameterBufferType:
1559             vaStatus = I965_RENDER_DECODE_BUFFER(picture_parameter);
1560             break;
1561             
1562         case VAIQMatrixBufferType:
1563             vaStatus = I965_RENDER_DECODE_BUFFER(iq_matrix);
1564             break;
1565
1566         case VABitPlaneBufferType:
1567             vaStatus = I965_RENDER_DECODE_BUFFER(bit_plane);
1568             break;
1569
1570         case VASliceParameterBufferType:
1571             vaStatus = I965_RENDER_DECODE_BUFFER(slice_parameter);
1572             break;
1573
1574         case VASliceDataBufferType:
1575             vaStatus = I965_RENDER_DECODE_BUFFER(slice_data);
1576             break;
1577
1578         case VAHuffmanTableBufferType:
1579             vaStatus = I965_RENDER_DECODE_BUFFER(huffman_table);
1580             break;
1581
1582         default:
1583             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
1584             break;
1585         }
1586     }
1587
1588     return vaStatus;
1589 }
1590
1591 #define I965_RENDER_ENCODE_BUFFER(name) I965_RENDER_BUFFER(encode, name)
1592
1593 #define DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(encode, name, member)
1594 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(sequence_parameter, seq_param)    
1595 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_parameter, pic_param)
1596 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_control, pic_control)
1597 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(qmatrix, q_matrix)
1598 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(iqmatrix, iq_matrix)
1599 /* extended buffer */
1600 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(sequence_parameter_ext, seq_param_ext)
1601 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_parameter_ext, pic_param_ext)
1602 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(dec_ref_pic_marking, dec_ref_pic_marking)
1603
1604 #define DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(name, member) DEF_RENDER_MULTI_BUFFER_FUNC(encode, name, member)
1605 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(slice_parameter, slice_params)
1606 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(slice_parameter_ext, slice_params_ext)
1607
1608 static VAStatus
1609 i965_encoder_render_packed_header_parameter_buffer(VADriverContextP ctx,
1610                                                    struct object_context *obj_context,
1611                                                    struct object_buffer *obj_buffer,
1612                                                    VAEncPackedHeaderType type)
1613 {
1614     struct encode_state *encode = &obj_context->codec_state.encode;
1615
1616     assert(obj_buffer->buffer_store->bo == NULL);
1617     assert(obj_buffer->buffer_store->buffer);
1618     i965_release_buffer_store(&encode->packed_header_param[type]);
1619     i965_reference_buffer_store(&encode->packed_header_param[type], obj_buffer->buffer_store);
1620
1621     return VA_STATUS_SUCCESS;
1622 }
1623
1624 static VAStatus
1625 i965_encoder_render_packed_header_data_buffer(VADriverContextP ctx,
1626                                               struct object_context *obj_context,
1627                                               struct object_buffer *obj_buffer,
1628                                               VAEncPackedHeaderType type)
1629 {
1630     struct encode_state *encode = &obj_context->codec_state.encode;
1631
1632     assert(obj_buffer->buffer_store->bo == NULL);
1633     assert(obj_buffer->buffer_store->buffer);
1634     i965_release_buffer_store(&encode->packed_header_data[type]);
1635     i965_reference_buffer_store(&encode->packed_header_data[type], obj_buffer->buffer_store);
1636
1637     return VA_STATUS_SUCCESS;
1638 }
1639
1640 static VAStatus 
1641 i965_encoder_render_picture(VADriverContextP ctx,
1642                             VAContextID context,
1643                             VABufferID *buffers,
1644                             int num_buffers)
1645 {
1646     struct i965_driver_data *i965 = i965_driver_data(ctx); 
1647     struct object_context *obj_context = CONTEXT(context);
1648     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
1649     int i;
1650
1651     for (i = 0; i < num_buffers; i++) {  
1652         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
1653         assert(obj_buffer);
1654
1655         switch (obj_buffer->type) {
1656         case VAQMatrixBufferType:
1657             vaStatus = I965_RENDER_ENCODE_BUFFER(qmatrix);
1658             break;
1659
1660         case VAIQMatrixBufferType:
1661             vaStatus = I965_RENDER_ENCODE_BUFFER(iqmatrix);
1662             break;
1663
1664         case VAEncSequenceParameterBufferType:
1665             vaStatus = I965_RENDER_ENCODE_BUFFER(sequence_parameter_ext);
1666             break;
1667
1668         case VAEncPictureParameterBufferType:
1669             vaStatus = I965_RENDER_ENCODE_BUFFER(picture_parameter_ext);
1670             break;
1671
1672         case VAEncSliceParameterBufferType:
1673             vaStatus = I965_RENDER_ENCODE_BUFFER(slice_parameter_ext);
1674             break;
1675
1676         case VAEncDecRefPicMarkingBufferH264Type:
1677             vaStatus = I965_RENDER_ENCODE_BUFFER(dec_ref_pic_marking);
1678             break;
1679
1680         case VAEncPackedHeaderParameterBufferType:
1681         {
1682             struct encode_state *encode = &obj_context->codec_state.encode;
1683             VAEncPackedHeaderParameterBuffer *param = (VAEncPackedHeaderParameterBuffer *)obj_buffer->buffer_store->buffer;
1684             encode->last_packed_header_type = param->type;
1685
1686             vaStatus = i965_encoder_render_packed_header_parameter_buffer(ctx,
1687                                                                           obj_context,
1688                                                                           obj_buffer,
1689                                                                           encode->last_packed_header_type);
1690             break;
1691         }
1692
1693         case VAEncPackedHeaderDataBufferType:
1694         {
1695             struct encode_state *encode = &obj_context->codec_state.encode;
1696
1697             assert(encode->last_packed_header_type == VAEncPackedHeaderSPS ||
1698                    encode->last_packed_header_type == VAEncPackedHeaderPPS ||
1699                    encode->last_packed_header_type == VAEncPackedHeaderSlice);
1700             vaStatus = i965_encoder_render_packed_header_data_buffer(ctx, 
1701                                                                      obj_context,
1702                                                                      obj_buffer,
1703                                                                      encode->last_packed_header_type);
1704             break;       
1705         }
1706
1707         default:
1708             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
1709             break;
1710         }
1711     }   
1712
1713     return vaStatus;
1714 }
1715
1716 #define I965_RENDER_PROC_BUFFER(name) I965_RENDER_BUFFER(proc, name)
1717
1718 #define DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(proc, name, member)
1719 DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(pipeline_parameter, pipeline_param)    
1720 DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(input_parameter, input_param)
1721
1722 static VAStatus
1723 i965_render_proc_filter_parameter_buffer(VADriverContextP ctx,
1724                                          struct object_context *obj_context,
1725                                          struct object_buffer *obj_buffer,
1726                                          VAProcFilterType type)
1727 {
1728     struct proc_state *proc = &obj_context->codec_state.proc;
1729
1730     assert(obj_buffer->buffer_store->bo == NULL);
1731     assert(obj_buffer->buffer_store->buffer);
1732     i965_release_buffer_store(&proc->filter_param[type]);
1733     i965_reference_buffer_store(&proc->filter_param[type], obj_buffer->buffer_store);
1734
1735     return VA_STATUS_SUCCESS;
1736 }
1737
1738 static VAStatus 
1739 i965_proc_render_picture(VADriverContextP ctx,
1740                          VAContextID context,
1741                          VABufferID *buffers,
1742                          int num_buffers)
1743 {
1744     struct i965_driver_data *i965 = i965_driver_data(ctx); 
1745     struct object_context *obj_context = CONTEXT(context);
1746     VAStatus vaStatus = VA_STATUS_SUCCESS;
1747     int i;
1748
1749     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
1750         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
1751         assert(obj_buffer);
1752
1753         switch (obj_buffer->type) {
1754         case VAProcPipelineParameterBufferType:
1755             vaStatus = I965_RENDER_PROC_BUFFER(pipeline_parameter);
1756             break;
1757
1758         case VAProcInputParameterBufferType:
1759             vaStatus = I965_RENDER_PROC_BUFFER(input_parameter);
1760             break;              
1761
1762         case VAProcFilterBaseParameterBufferType:
1763         {
1764             VAProcFilterBaseParameterBuffer *param = (VAProcFilterBaseParameterBuffer *)obj_buffer->buffer_store->buffer;
1765             vaStatus = i965_render_proc_filter_parameter_buffer(ctx, obj_context, obj_buffer, param->filter);
1766             break;
1767         }
1768
1769         case VAProcFilterDeinterlacingParameterBufferType:
1770             vaStatus = i965_render_proc_filter_parameter_buffer(ctx, obj_context, obj_buffer, VAProcFilterDeinterlacing);
1771             break;
1772
1773         case VAProcFilterProcAmpParameterBufferType:
1774             vaStatus = i965_render_proc_filter_parameter_buffer(ctx, obj_context, obj_buffer, VAProcFilterProcAmp);
1775             break;
1776
1777         default:
1778             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
1779             break;
1780         }
1781     }
1782
1783     return vaStatus;
1784 }
1785
1786 VAStatus 
1787 i965_RenderPicture(VADriverContextP ctx,
1788                    VAContextID context,
1789                    VABufferID *buffers,
1790                    int num_buffers)
1791 {
1792     struct i965_driver_data *i965 = i965_driver_data(ctx);
1793     struct object_context *obj_context;
1794     struct object_config *obj_config;
1795     VAContextID config;
1796     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
1797
1798     obj_context = CONTEXT(context);
1799     assert(obj_context);
1800
1801     config = obj_context->config_id;
1802     obj_config = CONFIG(config);
1803     assert(obj_config);
1804
1805     if (VAEntrypointVideoProc == obj_config->entrypoint) {
1806         vaStatus = i965_proc_render_picture(ctx, context, buffers, num_buffers);
1807     } else if (VAEntrypointEncSlice == obj_config->entrypoint ) {
1808         vaStatus = i965_encoder_render_picture(ctx, context, buffers, num_buffers);
1809     } else {
1810         vaStatus = i965_decoder_render_picture(ctx, context, buffers, num_buffers);
1811     }
1812
1813     return vaStatus;
1814 }
1815
1816 VAStatus 
1817 i965_EndPicture(VADriverContextP ctx, VAContextID context)
1818 {
1819     struct i965_driver_data *i965 = i965_driver_data(ctx); 
1820     struct object_context *obj_context = CONTEXT(context);
1821     struct object_config *obj_config;
1822     VAContextID config;
1823
1824     assert(obj_context);
1825     config = obj_context->config_id;
1826     obj_config = CONFIG(config);
1827     assert(obj_config);
1828
1829     if (obj_context->codec_type == CODEC_PROC) {
1830         assert(VAEntrypointVideoProc == obj_config->entrypoint);
1831     } else if (obj_context->codec_type == CODEC_ENC) {
1832         assert(VAEntrypointEncSlice == obj_config->entrypoint);
1833
1834         assert(obj_context->codec_state.encode.pic_param ||
1835                obj_context->codec_state.encode.pic_param_ext);
1836         assert(obj_context->codec_state.encode.seq_param ||
1837                obj_context->codec_state.encode.seq_param_ext);
1838         assert(obj_context->codec_state.encode.num_slice_params >= 1 ||
1839                obj_context->codec_state.encode.num_slice_params_ext >= 1);
1840     } else {
1841         assert(obj_context->codec_state.decode.pic_param);
1842         assert(obj_context->codec_state.decode.num_slice_params >= 1);
1843         assert(obj_context->codec_state.decode.num_slice_datas >= 1);
1844         assert(obj_context->codec_state.decode.num_slice_params == obj_context->codec_state.decode.num_slice_datas);
1845     }
1846
1847     assert(obj_context->hw_context->run);
1848     obj_context->hw_context->run(ctx, obj_config->profile, &obj_context->codec_state, obj_context->hw_context);
1849
1850     return VA_STATUS_SUCCESS;
1851 }
1852
1853 VAStatus 
1854 i965_SyncSurface(VADriverContextP ctx,
1855                  VASurfaceID render_target)
1856 {
1857     struct i965_driver_data *i965 = i965_driver_data(ctx); 
1858     struct object_surface *obj_surface = SURFACE(render_target);
1859
1860     assert(obj_surface);
1861
1862     return VA_STATUS_SUCCESS;
1863 }
1864
1865 VAStatus 
1866 i965_QuerySurfaceStatus(VADriverContextP ctx,
1867                         VASurfaceID render_target,
1868                         VASurfaceStatus *status)        /* out */
1869 {
1870     struct i965_driver_data *i965 = i965_driver_data(ctx); 
1871     struct object_surface *obj_surface = SURFACE(render_target);
1872
1873     assert(obj_surface);
1874
1875     /* Usually GEM will handle synchronization with the graphics hardware */
1876 #if 0
1877     if (obj_surface->bo) {
1878         dri_bo_map(obj_surface->bo, 0);
1879         dri_bo_unmap(obj_surface->bo);
1880     }
1881 #endif
1882     
1883     *status = obj_surface->status;
1884
1885     return VA_STATUS_SUCCESS;
1886 }
1887
1888
1889 /* 
1890  * Query display attributes 
1891  * The caller must provide a "attr_list" array that can hold at
1892  * least vaMaxNumDisplayAttributes() entries. The actual number of attributes
1893  * returned in "attr_list" is returned in "num_attributes".
1894  */
1895 VAStatus 
1896 i965_QueryDisplayAttributes(VADriverContextP ctx,
1897                             VADisplayAttribute *attr_list,    /* out */
1898                             int *num_attributes)              /* out */
1899 {
1900     if (num_attributes)
1901         *num_attributes = 0;
1902
1903     return VA_STATUS_SUCCESS;
1904 }
1905
1906 /* 
1907  * Get display attributes 
1908  * This function returns the current attribute values in "attr_list".
1909  * Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
1910  * from vaQueryDisplayAttributes() can have their values retrieved.  
1911  */
1912 VAStatus 
1913 i965_GetDisplayAttributes(VADriverContextP ctx,
1914                           VADisplayAttribute *attr_list,    /* in/out */
1915                           int num_attributes)
1916 {
1917     /* TODO */
1918     return VA_STATUS_ERROR_UNIMPLEMENTED;
1919 }
1920
1921 /* 
1922  * Set display attributes 
1923  * Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
1924  * from vaQueryDisplayAttributes() can be set.  If the attribute is not settable or 
1925  * the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
1926  */
1927 VAStatus 
1928 i965_SetDisplayAttributes(VADriverContextP ctx,
1929                           VADisplayAttribute *attr_list,
1930                           int num_attributes)
1931 {
1932     /* TODO */
1933     return VA_STATUS_ERROR_UNIMPLEMENTED;
1934 }
1935
1936 VAStatus 
1937 i965_DbgCopySurfaceToBuffer(VADriverContextP ctx,
1938                             VASurfaceID surface,
1939                             void **buffer,              /* out */
1940                             unsigned int *stride)       /* out */
1941 {
1942     /* TODO */
1943     return VA_STATUS_ERROR_UNIMPLEMENTED;
1944 }
1945
1946 static VAStatus 
1947 i965_Init(VADriverContextP ctx)
1948 {
1949     struct i965_driver_data *i965 = i965_driver_data(ctx); 
1950
1951     if (intel_driver_init(ctx) == False)
1952         return VA_STATUS_ERROR_UNKNOWN;
1953
1954     if (IS_G4X(i965->intel.device_id))
1955         i965->codec_info = &g4x_hw_codec_info;
1956     else if (IS_IRONLAKE(i965->intel.device_id))
1957         i965->codec_info = &ironlake_hw_codec_info;
1958     else if (IS_GEN6(i965->intel.device_id))
1959         i965->codec_info = &gen6_hw_codec_info;
1960     else if (IS_GEN7(i965->intel.device_id))
1961         i965->codec_info = &gen7_hw_codec_info;
1962     else
1963         return VA_STATUS_ERROR_UNKNOWN;
1964
1965     i965->batch = intel_batchbuffer_new(&i965->intel, I915_EXEC_RENDER);
1966
1967     if (i965_post_processing_init(ctx) == False)
1968         return VA_STATUS_ERROR_UNKNOWN;
1969
1970     if (i965_render_init(ctx) == False)
1971         return VA_STATUS_ERROR_UNKNOWN;
1972
1973     _i965InitMutex(&i965->render_mutex);
1974
1975     return VA_STATUS_SUCCESS;
1976 }
1977
1978 static void
1979 i965_destroy_heap(struct object_heap *heap, 
1980                   void (*func)(struct object_heap *heap, struct object_base *object))
1981 {
1982     struct object_base *object;
1983     object_heap_iterator iter;    
1984
1985     object = object_heap_first(heap, &iter);
1986
1987     while (object) {
1988         if (func)
1989             func(heap, object);
1990
1991         object = object_heap_next(heap, &iter);
1992     }
1993
1994     object_heap_destroy(heap);
1995 }
1996
1997
1998 VAStatus 
1999 i965_DestroyImage(VADriverContextP ctx, VAImageID image);
2000
2001 VAStatus 
2002 i965_CreateImage(VADriverContextP ctx,
2003                  VAImageFormat *format,
2004                  int width,
2005                  int height,
2006                  VAImage *out_image)        /* out */
2007 {
2008     struct i965_driver_data *i965 = i965_driver_data(ctx);
2009     struct object_image *obj_image;
2010     VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
2011     VAImageID image_id;
2012     unsigned int width2, height2, size2, size;
2013
2014     out_image->image_id = VA_INVALID_ID;
2015     out_image->buf      = VA_INVALID_ID;
2016
2017     image_id = NEW_IMAGE_ID();
2018     if (image_id == VA_INVALID_ID)
2019         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2020
2021     obj_image = IMAGE(image_id);
2022     if (!obj_image)
2023         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2024     obj_image->bo         = NULL;
2025     obj_image->palette    = NULL;
2026     obj_image->derived_surface = VA_INVALID_ID;
2027
2028     VAImage * const image = &obj_image->image;
2029     image->image_id       = image_id;
2030     image->buf            = VA_INVALID_ID;
2031
2032     size    = width * height;
2033     width2  = (width  + 1) / 2;
2034     height2 = (height + 1) / 2;
2035     size2   = width2 * height2;
2036
2037     image->num_palette_entries = 0;
2038     image->entry_bytes         = 0;
2039     memset(image->component_order, 0, sizeof(image->component_order));
2040
2041     switch (format->fourcc) {
2042     case VA_FOURCC('I','A','4','4'):
2043     case VA_FOURCC('A','I','4','4'):
2044         image->num_planes = 1;
2045         image->pitches[0] = width;
2046         image->offsets[0] = 0;
2047         image->data_size  = image->offsets[0] + image->pitches[0] * height;
2048         image->num_palette_entries = 16;
2049         image->entry_bytes         = 3;
2050         image->component_order[0]  = 'R';
2051         image->component_order[1]  = 'G';
2052         image->component_order[2]  = 'B';
2053         break;
2054     case VA_FOURCC('A','R','G','B'):
2055     case VA_FOURCC('A','B','G','R'):
2056     case VA_FOURCC('B','G','R','A'):
2057     case VA_FOURCC('R','G','B','A'):
2058         image->num_planes = 1;
2059         image->pitches[0] = width * 4;
2060         image->offsets[0] = 0;
2061         image->data_size  = image->offsets[0] + image->pitches[0] * height;
2062         break;
2063     case VA_FOURCC('Y','V','1','2'):
2064         image->num_planes = 3;
2065         image->pitches[0] = width;
2066         image->offsets[0] = 0;
2067         image->pitches[1] = width2;
2068         image->offsets[1] = size + size2;
2069         image->pitches[2] = width2;
2070         image->offsets[2] = size;
2071         image->data_size  = size + 2 * size2;
2072         break;
2073     case VA_FOURCC('I','4','2','0'):
2074         image->num_planes = 3;
2075         image->pitches[0] = width;
2076         image->offsets[0] = 0;
2077         image->pitches[1] = width2;
2078         image->offsets[1] = size;
2079         image->pitches[2] = width2;
2080         image->offsets[2] = size + size2;
2081         image->data_size  = size + 2 * size2;
2082         break;
2083     case VA_FOURCC('N','V','1','2'):
2084         image->num_planes = 2;
2085         image->pitches[0] = width;
2086         image->offsets[0] = 0;
2087         image->pitches[1] = width;
2088         image->offsets[1] = size;
2089         image->data_size  = size + 2 * size2;
2090         break;
2091     default:
2092         goto error;
2093     }
2094
2095     va_status = i965_CreateBuffer(ctx, 0, VAImageBufferType,
2096                                   image->data_size, 1, NULL, &image->buf);
2097     if (va_status != VA_STATUS_SUCCESS)
2098         goto error;
2099
2100     obj_image->bo = BUFFER(image->buf)->buffer_store->bo;
2101     dri_bo_reference(obj_image->bo);
2102
2103     if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
2104         obj_image->palette = malloc(image->num_palette_entries * sizeof(obj_image->palette));
2105         if (!obj_image->palette)
2106             goto error;
2107     }
2108
2109     image->image_id             = image_id;
2110     image->format               = *format;
2111     image->width                = width;
2112     image->height               = height;
2113
2114     *out_image                  = *image;
2115     return VA_STATUS_SUCCESS;
2116
2117  error:
2118     i965_DestroyImage(ctx, image_id);
2119     return va_status;
2120 }
2121
2122 void 
2123 i965_check_alloc_surface_bo(VADriverContextP ctx,
2124                             struct object_surface *obj_surface,
2125                             int tiled,
2126                             unsigned int fourcc,
2127                             unsigned int subsampling)
2128 {
2129     struct i965_driver_data *i965 = i965_driver_data(ctx);
2130     int region_width, region_height;
2131
2132     if (obj_surface->bo) {
2133         assert(obj_surface->fourcc);
2134         assert(obj_surface->fourcc == fourcc);
2135         assert(obj_surface->subsampling == subsampling);
2136         return;
2137     }
2138
2139     obj_surface->x_cb_offset = 0; /* X offset is always 0 */
2140     obj_surface->x_cr_offset = 0;
2141
2142     if (tiled) {
2143         assert(fourcc == VA_FOURCC('N', 'V', '1', '2') ||
2144                fourcc == VA_FOURCC('I', 'M', 'C', '1') ||
2145                fourcc == VA_FOURCC('I', 'M', 'C', '3'));
2146
2147         obj_surface->width = ALIGN(obj_surface->orig_width, 128);
2148         obj_surface->height = ALIGN(obj_surface->orig_height, 32);
2149         obj_surface->cb_cr_pitch = obj_surface->width;
2150         region_width = obj_surface->width;
2151         region_height = obj_surface->height;
2152
2153         if (fourcc == VA_FOURCC('N', 'V', '1', '2')) {
2154             assert(subsampling == SUBSAMPLE_YUV420);
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             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32);
2160         } else if (fourcc == VA_FOURCC('I', 'M', 'C', '1') ||
2161                    fourcc == VA_FOURCC('I', 'M', 'C', '3')) {
2162             switch (subsampling) {
2163             case SUBSAMPLE_YUV400:
2164                 obj_surface->cb_cr_width = 0;
2165                 obj_surface->cb_cr_height = 0;
2166                 break;
2167
2168             case SUBSAMPLE_YUV420:
2169                 obj_surface->cb_cr_width = obj_surface->orig_width / 2;
2170                 obj_surface->cb_cr_height = obj_surface->orig_height / 2;
2171                 break;
2172
2173             case SUBSAMPLE_YUV422H:
2174                 obj_surface->cb_cr_width = obj_surface->orig_width / 2;
2175                 obj_surface->cb_cr_height = obj_surface->orig_height;
2176                 break;
2177
2178             case SUBSAMPLE_YUV422V:
2179                 obj_surface->cb_cr_width = obj_surface->orig_width;
2180                 obj_surface->cb_cr_height = obj_surface->orig_height / 2;
2181                 break;
2182
2183             case SUBSAMPLE_YUV444:
2184                 obj_surface->cb_cr_width = obj_surface->orig_width;
2185                 obj_surface->cb_cr_height = obj_surface->orig_height;
2186                 break;
2187
2188             case SUBSAMPLE_YUV411:
2189                 obj_surface->cb_cr_width = obj_surface->orig_width / 4;
2190                 obj_surface->cb_cr_height = obj_surface->orig_height;
2191                 break;
2192
2193             default:
2194                 assert(0);
2195                 break;
2196             }
2197
2198             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
2199
2200             if (fourcc == VA_FOURCC('I', 'M', 'C', '1')) {
2201                 obj_surface->y_cr_offset = obj_surface->height;
2202                 obj_surface->y_cb_offset = obj_surface->y_cr_offset + ALIGN(obj_surface->cb_cr_height, 32);
2203             } else {
2204                 obj_surface->y_cb_offset = obj_surface->height;
2205                 obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
2206             }
2207         }
2208     } else {
2209         assert(fourcc != VA_FOURCC('I', 'M', 'C', '1') &&
2210                fourcc != VA_FOURCC('I', 'M', 'C', '3'));
2211         assert(subsampling == SUBSAMPLE_YUV420);
2212
2213         region_width = obj_surface->width;
2214         region_height = obj_surface->height;
2215
2216         switch (fourcc) {
2217         case VA_FOURCC('N', 'V', '1', '2'):
2218             obj_surface->y_cb_offset = obj_surface->height;
2219             obj_surface->y_cr_offset = obj_surface->height;
2220             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
2221             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
2222             obj_surface->cb_cr_pitch = obj_surface->width;
2223             region_height = obj_surface->height + obj_surface->height / 2;
2224             break;
2225
2226         case VA_FOURCC('Y', 'V', '1', '2'):
2227         case VA_FOURCC('I', '4', '2', '0'):
2228             if (fourcc == VA_FOURCC('Y', 'V', '1', '2')) {
2229                 obj_surface->y_cr_offset = obj_surface->height;
2230                 obj_surface->y_cb_offset = obj_surface->height + obj_surface->height / 4;
2231             } else {
2232                 obj_surface->y_cb_offset = obj_surface->height;
2233                 obj_surface->y_cr_offset = obj_surface->height + obj_surface->height / 4;
2234             }
2235
2236             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
2237             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
2238             obj_surface->cb_cr_pitch = obj_surface->width / 2;
2239             region_height = obj_surface->height + obj_surface->height / 2;
2240             break;
2241
2242         default:
2243             assert(0);
2244             break;
2245         }
2246     }
2247
2248     obj_surface->size = ALIGN(region_width * region_height, 0x1000);
2249
2250     if (tiled) {
2251         uint32_t tiling_mode = I915_TILING_Y; /* always uses Y-tiled format */
2252         unsigned long pitch;
2253
2254         obj_surface->bo = drm_intel_bo_alloc_tiled(i965->intel.bufmgr, 
2255                                                    "vaapi surface",
2256                                                    region_width,
2257                                                    region_height,
2258                                                    1,
2259                                                    &tiling_mode,
2260                                                    &pitch,
2261                                                    0);
2262         assert(tiling_mode == I915_TILING_Y);
2263         assert(pitch == obj_surface->width);
2264     } else {
2265         obj_surface->bo = dri_bo_alloc(i965->intel.bufmgr,
2266                                        "vaapi surface",
2267                                        obj_surface->size,
2268                                        0x1000);
2269     }
2270
2271     obj_surface->fourcc = fourcc;
2272     obj_surface->subsampling = subsampling;
2273     assert(obj_surface->bo);
2274 }
2275
2276 VAStatus i965_DeriveImage(VADriverContextP ctx,
2277                           VASurfaceID surface,
2278                           VAImage *out_image)        /* out */
2279 {
2280     struct i965_driver_data *i965 = i965_driver_data(ctx);
2281     struct object_image *obj_image;
2282     struct object_surface *obj_surface; 
2283     VAImageID image_id;
2284     unsigned int w_pitch, h_pitch;
2285     VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
2286
2287     out_image->image_id = VA_INVALID_ID;
2288     obj_surface = SURFACE(surface);
2289
2290     if (!obj_surface)
2291         return VA_STATUS_ERROR_INVALID_SURFACE;
2292
2293     if (!obj_surface->bo) {
2294         unsigned int is_tiled = 0;
2295         unsigned int fourcc = VA_FOURCC('Y', 'V', '1', '2');
2296         i965_guess_surface_format(ctx, surface, &fourcc, &is_tiled);
2297         i965_check_alloc_surface_bo(ctx, obj_surface, is_tiled, fourcc, SUBSAMPLE_YUV420);
2298     }
2299
2300     assert(obj_surface->fourcc);
2301
2302     w_pitch = obj_surface->width;
2303     h_pitch = obj_surface->height;
2304
2305     image_id = NEW_IMAGE_ID();
2306
2307     if (image_id == VA_INVALID_ID)
2308         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2309
2310     obj_image = IMAGE(image_id);
2311     
2312     if (!obj_image)
2313         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2314
2315     obj_image->bo = NULL;
2316     obj_image->palette = NULL;
2317     obj_image->derived_surface = VA_INVALID_ID;
2318
2319     VAImage * const image = &obj_image->image;
2320     
2321     memset(image, 0, sizeof(*image));
2322     image->image_id = image_id;
2323     image->buf = VA_INVALID_ID;
2324     image->num_palette_entries = 0;
2325     image->entry_bytes = 0;
2326     image->width = obj_surface->orig_width;
2327     image->height = obj_surface->orig_height;
2328     image->data_size = obj_surface->size;
2329
2330     image->format.fourcc = obj_surface->fourcc;
2331     image->format.byte_order = VA_LSB_FIRST;
2332     image->format.bits_per_pixel = 12;
2333
2334     switch (image->format.fourcc) {
2335     case VA_FOURCC('Y', 'V', '1', '2'):
2336         image->num_planes = 3;
2337         image->pitches[0] = w_pitch; /* Y */
2338         image->offsets[0] = 0;
2339         image->pitches[1] = obj_surface->cb_cr_pitch; /* V */
2340         image->offsets[1] = w_pitch * obj_surface->y_cr_offset;
2341         image->pitches[2] = obj_surface->cb_cr_pitch; /* U */
2342         image->offsets[2] = w_pitch * obj_surface->y_cb_offset;
2343         break;
2344
2345     case VA_FOURCC('N', 'V', '1', '2'):
2346         image->num_planes = 2;
2347         image->pitches[0] = w_pitch; /* Y */
2348         image->offsets[0] = 0;
2349         image->pitches[1] = obj_surface->cb_cr_pitch; /* UV */
2350         image->offsets[1] = w_pitch * obj_surface->y_cb_offset;
2351         break;
2352
2353     case VA_FOURCC('I', '4', '2', '0'):
2354         image->num_planes = 3;
2355         image->pitches[0] = w_pitch; /* Y */
2356         image->offsets[0] = 0;
2357         image->pitches[1] = obj_surface->cb_cr_pitch; /* U */
2358         image->offsets[1] = w_pitch * obj_surface->y_cb_offset;
2359         image->pitches[2] = obj_surface->cb_cr_pitch; /* V */
2360         image->offsets[2] = w_pitch * obj_surface->y_cr_offset;
2361         break;
2362
2363     default:
2364         goto error;
2365     }
2366
2367     va_status = i965_create_buffer_internal(ctx, 0, VAImageBufferType,
2368                                             obj_surface->size, 1, NULL, obj_surface->bo, &image->buf);
2369     if (va_status != VA_STATUS_SUCCESS)
2370         goto error;
2371
2372     obj_image->bo = BUFFER(image->buf)->buffer_store->bo;
2373     dri_bo_reference(obj_image->bo);
2374
2375     if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
2376         obj_image->palette = malloc(image->num_palette_entries * sizeof(obj_image->palette));
2377         if (!obj_image->palette) {
2378             va_status = VA_STATUS_ERROR_ALLOCATION_FAILED;
2379             goto error;
2380         }
2381     }
2382
2383     *out_image = *image;
2384     obj_surface->flags |= SURFACE_DERIVED;
2385     obj_image->derived_surface = surface;
2386
2387     return VA_STATUS_SUCCESS;
2388
2389  error:
2390     i965_DestroyImage(ctx, image_id);
2391     return va_status;
2392 }
2393
2394 static void 
2395 i965_destroy_image(struct object_heap *heap, struct object_base *obj)
2396 {
2397     object_heap_free(heap, obj);
2398 }
2399
2400
2401 VAStatus 
2402 i965_DestroyImage(VADriverContextP ctx, VAImageID image)
2403 {
2404     struct i965_driver_data *i965 = i965_driver_data(ctx);
2405     struct object_image *obj_image = IMAGE(image); 
2406     struct object_surface *obj_surface; 
2407
2408     if (!obj_image)
2409         return VA_STATUS_SUCCESS;
2410
2411     dri_bo_unreference(obj_image->bo);
2412     obj_image->bo = NULL;
2413
2414     if (obj_image->image.buf != VA_INVALID_ID) {
2415         i965_DestroyBuffer(ctx, obj_image->image.buf);
2416         obj_image->image.buf = VA_INVALID_ID;
2417     }
2418
2419     if (obj_image->palette) {
2420         free(obj_image->palette);
2421         obj_image->palette = NULL;
2422     }
2423
2424     obj_surface = SURFACE(obj_image->derived_surface);
2425
2426     if (obj_surface) {
2427         obj_surface->flags &= ~SURFACE_DERIVED;
2428     }
2429
2430     i965_destroy_image(&i965->image_heap, (struct object_base *)obj_image);
2431
2432     return VA_STATUS_SUCCESS;
2433 }
2434
2435 /*
2436  * pointer to an array holding the palette data.  The size of the array is
2437  * num_palette_entries * entry_bytes in size.  The order of the components
2438  * in the palette is described by the component_order in VASubpicture struct
2439  */
2440 VAStatus 
2441 i965_SetImagePalette(VADriverContextP ctx,
2442                      VAImageID image,
2443                      unsigned char *palette)
2444 {
2445     struct i965_driver_data *i965 = i965_driver_data(ctx);
2446     unsigned int i;
2447
2448     struct object_image *obj_image = IMAGE(image);
2449     if (!obj_image)
2450         return VA_STATUS_ERROR_INVALID_IMAGE;
2451
2452     if (!obj_image->palette)
2453         return VA_STATUS_ERROR_ALLOCATION_FAILED; /* XXX: unpaletted/error */
2454
2455     for (i = 0; i < obj_image->image.num_palette_entries; i++)
2456         obj_image->palette[i] = (((unsigned int)palette[3*i + 0] << 16) |
2457                                  ((unsigned int)palette[3*i + 1] <<  8) |
2458                                  (unsigned int)palette[3*i + 2]);
2459     return VA_STATUS_SUCCESS;
2460 }
2461
2462 static inline void
2463 memcpy_pic(uint8_t *dst, unsigned int dst_stride,
2464            const uint8_t *src, unsigned int src_stride,
2465            unsigned int len, unsigned int height)
2466 {
2467     unsigned int i;
2468
2469     for (i = 0; i < height; i++) {
2470         memcpy(dst, src, len);
2471         dst += dst_stride;
2472         src += src_stride;
2473     }
2474 }
2475
2476 static void
2477 get_image_i420(struct object_image *obj_image, uint8_t *image_data,
2478                struct object_surface *obj_surface,
2479                const VARectangle *rect)
2480 {
2481     uint8_t *dst[3], *src[3];
2482     const int Y = 0;
2483     const int U = obj_image->image.format.fourcc == obj_surface->fourcc ? 1 : 2;
2484     const int V = obj_image->image.format.fourcc == obj_surface->fourcc ? 2 : 1;
2485     unsigned int tiling, swizzle;
2486
2487     if (!obj_surface->bo)
2488         return;
2489
2490     assert(obj_surface->fourcc);
2491     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
2492
2493     if (tiling != I915_TILING_NONE)
2494         drm_intel_gem_bo_map_gtt(obj_surface->bo);
2495     else
2496         dri_bo_map(obj_surface->bo, 0);
2497
2498     if (!obj_surface->bo->virtual)
2499         return;
2500
2501     /* Dest VA image has either I420 or YV12 format.
2502        Source VA surface alway has I420 format */
2503     dst[Y] = image_data + obj_image->image.offsets[Y];
2504     src[0] = (uint8_t *)obj_surface->bo->virtual;
2505     dst[U] = image_data + obj_image->image.offsets[U];
2506     src[1] = src[0] + obj_surface->width * obj_surface->height;
2507     dst[V] = image_data + obj_image->image.offsets[V];
2508     src[2] = src[1] + (obj_surface->width / 2) * (obj_surface->height / 2);
2509
2510     /* Y plane */
2511     dst[Y] += rect->y * obj_image->image.pitches[Y] + rect->x;
2512     src[0] += rect->y * obj_surface->width + rect->x;
2513     memcpy_pic(dst[Y], obj_image->image.pitches[Y],
2514                src[0], obj_surface->width,
2515                rect->width, rect->height);
2516
2517     /* U plane */
2518     dst[U] += (rect->y / 2) * obj_image->image.pitches[U] + rect->x / 2;
2519     src[1] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
2520     memcpy_pic(dst[U], obj_image->image.pitches[U],
2521                src[1], obj_surface->width / 2,
2522                rect->width / 2, rect->height / 2);
2523
2524     /* V plane */
2525     dst[V] += (rect->y / 2) * obj_image->image.pitches[V] + rect->x / 2;
2526     src[2] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
2527     memcpy_pic(dst[V], obj_image->image.pitches[V],
2528                src[2], obj_surface->width / 2,
2529                rect->width / 2, rect->height / 2);
2530
2531     if (tiling != I915_TILING_NONE)
2532         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
2533     else
2534         dri_bo_unmap(obj_surface->bo);
2535 }
2536
2537 static void
2538 get_image_nv12(struct object_image *obj_image, uint8_t *image_data,
2539                struct object_surface *obj_surface,
2540                const VARectangle *rect)
2541 {
2542     uint8_t *dst[2], *src[2];
2543     unsigned int tiling, swizzle;
2544
2545     if (!obj_surface->bo)
2546         return;
2547
2548     assert(obj_surface->fourcc);
2549     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
2550
2551     if (tiling != I915_TILING_NONE)
2552         drm_intel_gem_bo_map_gtt(obj_surface->bo);
2553     else
2554         dri_bo_map(obj_surface->bo, 0);
2555
2556     if (!obj_surface->bo->virtual)
2557         return;
2558
2559     /* Both dest VA image and source surface have NV12 format */
2560     dst[0] = image_data + obj_image->image.offsets[0];
2561     src[0] = (uint8_t *)obj_surface->bo->virtual;
2562     dst[1] = image_data + obj_image->image.offsets[1];
2563     src[1] = src[0] + obj_surface->width * obj_surface->height;
2564
2565     /* Y plane */
2566     dst[0] += rect->y * obj_image->image.pitches[0] + rect->x;
2567     src[0] += rect->y * obj_surface->width + rect->x;
2568     memcpy_pic(dst[0], obj_image->image.pitches[0],
2569                src[0], obj_surface->width,
2570                rect->width, rect->height);
2571
2572     /* UV plane */
2573     dst[1] += (rect->y / 2) * obj_image->image.pitches[1] + (rect->x & -2);
2574     src[1] += (rect->y / 2) * obj_surface->width + (rect->x & -2);
2575     memcpy_pic(dst[1], obj_image->image.pitches[1],
2576                src[1], obj_surface->width,
2577                rect->width, rect->height / 2);
2578
2579     if (tiling != I915_TILING_NONE)
2580         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
2581     else
2582         dri_bo_unmap(obj_surface->bo);
2583 }
2584
2585 static VAStatus 
2586 i965_sw_getimage(VADriverContextP ctx,
2587                  VASurfaceID surface,
2588                  int x,   /* coordinates of the upper left source pixel */
2589                  int y,
2590                  unsigned int width,      /* width and height of the region */
2591                  unsigned int height,
2592                  VAImageID image)
2593 {
2594     struct i965_driver_data *i965 = i965_driver_data(ctx);
2595     struct i965_render_state *render_state = &i965->render_state;
2596
2597     struct object_surface *obj_surface = SURFACE(surface);
2598     if (!obj_surface)
2599         return VA_STATUS_ERROR_INVALID_SURFACE;
2600
2601     struct object_image *obj_image = IMAGE(image);
2602     if (!obj_image)
2603         return VA_STATUS_ERROR_INVALID_IMAGE;
2604
2605     if (x < 0 || y < 0)
2606         return VA_STATUS_ERROR_INVALID_PARAMETER;
2607     if (x + width > obj_surface->orig_width ||
2608         y + height > obj_surface->orig_height)
2609         return VA_STATUS_ERROR_INVALID_PARAMETER;
2610     if (x + width > obj_image->image.width ||
2611         y + height > obj_image->image.height)
2612         return VA_STATUS_ERROR_INVALID_PARAMETER;
2613
2614     VAStatus va_status;
2615     void *image_data = NULL;
2616
2617     va_status = i965_MapBuffer(ctx, obj_image->image.buf, &image_data);
2618     if (va_status != VA_STATUS_SUCCESS)
2619         return va_status;
2620
2621     VARectangle rect;
2622     rect.x = x;
2623     rect.y = y;
2624     rect.width = width;
2625     rect.height = height;
2626
2627     switch (obj_image->image.format.fourcc) {
2628     case VA_FOURCC('Y','V','1','2'):
2629     case VA_FOURCC('I','4','2','0'):
2630         /* I420 is native format for MPEG-2 decoded surfaces */
2631         if (render_state->interleaved_uv)
2632             goto operation_failed;
2633         get_image_i420(obj_image, image_data, obj_surface, &rect);
2634         break;
2635     case VA_FOURCC('N','V','1','2'):
2636         /* NV12 is native format for H.264 decoded surfaces */
2637         if (!render_state->interleaved_uv)
2638             goto operation_failed;
2639         get_image_nv12(obj_image, image_data, obj_surface, &rect);
2640         break;
2641     default:
2642     operation_failed:
2643         va_status = VA_STATUS_ERROR_OPERATION_FAILED;
2644         break;
2645     }
2646
2647     i965_UnmapBuffer(ctx, obj_image->image.buf);
2648     return va_status;
2649 }
2650
2651 static VAStatus 
2652 i965_hw_getimage(VADriverContextP ctx,
2653                  VASurfaceID surface,
2654                  int x,   /* coordinates of the upper left source pixel */
2655                  int y,
2656                  unsigned int width,      /* width and height of the region */
2657                  unsigned int height,
2658                  VAImageID image)
2659 {
2660     struct i965_driver_data *i965 = i965_driver_data(ctx);
2661     struct i965_surface src_surface;
2662     struct i965_surface dst_surface;
2663     VAStatus va_status;
2664     VARectangle rect;
2665     struct object_surface *obj_surface = SURFACE(surface);
2666     struct object_image *obj_image = IMAGE(image);
2667
2668     if (!obj_surface)
2669         return VA_STATUS_ERROR_INVALID_SURFACE;
2670
2671     if (!obj_image)
2672         return VA_STATUS_ERROR_INVALID_IMAGE;
2673
2674     if (x < 0 || y < 0)
2675         return VA_STATUS_ERROR_INVALID_PARAMETER;
2676     if (x + width > obj_surface->orig_width ||
2677         y + height > obj_surface->orig_height)
2678         return VA_STATUS_ERROR_INVALID_PARAMETER;
2679     if (x + width > obj_image->image.width ||
2680         y + height > obj_image->image.height)
2681         return VA_STATUS_ERROR_INVALID_PARAMETER;
2682
2683     if (!obj_surface->bo)
2684         return VA_STATUS_SUCCESS;
2685
2686     rect.x = x;
2687     rect.y = y;
2688     rect.width = width;
2689     rect.height = height;
2690
2691     src_surface.id = surface;
2692     src_surface.type = I965_SURFACE_TYPE_SURFACE;
2693     src_surface.flags = I965_SURFACE_FLAG_FRAME;
2694
2695     dst_surface.id = image;
2696     dst_surface.type = I965_SURFACE_TYPE_IMAGE;
2697     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
2698
2699     va_status = i965_image_processing(ctx,
2700                                       &src_surface,
2701                                       &rect,
2702                                       &dst_surface,
2703                                       &rect);
2704
2705
2706     return va_status;
2707 }
2708
2709 VAStatus 
2710 i965_GetImage(VADriverContextP ctx,
2711               VASurfaceID surface,
2712               int x,   /* coordinates of the upper left source pixel */
2713               int y,
2714               unsigned int width,      /* width and height of the region */
2715               unsigned int height,
2716               VAImageID image)
2717 {
2718     struct i965_driver_data * const i965 = i965_driver_data(ctx);
2719     VAStatus va_status;
2720
2721     if (HAS_ACCELERATED_GETIMAGE(i965))
2722         va_status = i965_hw_getimage(ctx,
2723                                      surface,
2724                                      x, y,
2725                                      width, height,
2726                                      image);
2727     else
2728         va_status = i965_sw_getimage(ctx,
2729                                      surface,
2730                                      x, y,
2731                                      width, height,
2732                                      image);
2733
2734     return va_status;
2735 }
2736
2737 VAStatus 
2738 i965_PutSurface(VADriverContextP ctx,
2739                 VASurfaceID surface,
2740                 void *draw, /* X Drawable */
2741                 short srcx,
2742                 short srcy,
2743                 unsigned short srcw,
2744                 unsigned short srch,
2745                 short destx,
2746                 short desty,
2747                 unsigned short destw,
2748                 unsigned short desth,
2749                 VARectangle *cliprects, /* client supplied clip list */
2750                 unsigned int number_cliprects, /* number of clip rects in the clip list */
2751                 unsigned int flags) /* de-interlacing flags */
2752 {
2753     struct i965_driver_data *i965 = i965_driver_data(ctx); 
2754     struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
2755     struct i965_render_state *render_state = &i965->render_state;
2756     struct dri_drawable *dri_drawable;
2757     union dri_buffer *buffer;
2758     struct intel_region *dest_region;
2759     struct object_surface *obj_surface; 
2760     VARectangle src_rect, dst_rect;
2761     int ret;
2762     uint32_t name;
2763     Bool new_region = False;
2764     int pp_flag = 0;
2765
2766     /* Currently don't support DRI1 */
2767     if (dri_state->driConnectedFlag != VA_DRI2)
2768         return VA_STATUS_ERROR_UNKNOWN;
2769
2770     /* Some broken sources such as H.264 conformance case FM2_SVA_C
2771      * will get here
2772      */
2773     obj_surface = SURFACE(surface);
2774     if (!obj_surface || !obj_surface->bo)
2775         return VA_STATUS_SUCCESS;
2776
2777     _i965LockMutex(&i965->render_mutex);
2778
2779     dri_drawable = dri_get_drawable(ctx, (Drawable)draw);
2780     assert(dri_drawable);
2781
2782     buffer = dri_get_rendering_buffer(ctx, dri_drawable);
2783     assert(buffer);
2784     
2785     dest_region = render_state->draw_region;
2786
2787     if (dest_region) {
2788         assert(dest_region->bo);
2789         dri_bo_flink(dest_region->bo, &name);
2790         
2791         if (buffer->dri2.name != name) {
2792             new_region = True;
2793             dri_bo_unreference(dest_region->bo);
2794         }
2795     } else {
2796         dest_region = (struct intel_region *)calloc(1, sizeof(*dest_region));
2797         assert(dest_region);
2798         render_state->draw_region = dest_region;
2799         new_region = True;
2800     }
2801
2802     if (new_region) {
2803         dest_region->x = dri_drawable->x;
2804         dest_region->y = dri_drawable->y;
2805         dest_region->width = dri_drawable->width;
2806         dest_region->height = dri_drawable->height;
2807         dest_region->cpp = buffer->dri2.cpp;
2808         dest_region->pitch = buffer->dri2.pitch;
2809
2810         dest_region->bo = intel_bo_gem_create_from_name(i965->intel.bufmgr, "rendering buffer", buffer->dri2.name);
2811         assert(dest_region->bo);
2812
2813         ret = dri_bo_get_tiling(dest_region->bo, &(dest_region->tiling), &(dest_region->swizzle));
2814         assert(ret == 0);
2815     }
2816
2817     if ((flags & VA_FILTER_SCALING_MASK) == VA_FILTER_SCALING_NL_ANAMORPHIC)
2818         pp_flag |= I965_PP_FLAG_AVS;
2819
2820     if (flags & VA_TOP_FIELD)
2821         pp_flag |= I965_PP_FLAG_DEINTERLACING_TOP_FISRT;
2822     else if (flags & VA_BOTTOM_FIELD)
2823         pp_flag |= I965_PP_FLAG_DEINTERLACING_BOTTOM_FIRST;
2824
2825     src_rect.x      = srcx;
2826     src_rect.y      = srcy;
2827     src_rect.width  = srcw;
2828     src_rect.height = srch;
2829
2830     dst_rect.x      = destx;
2831     dst_rect.y      = desty;
2832     dst_rect.width  = destw;
2833     dst_rect.height = desth;
2834
2835     intel_render_put_surface(ctx, surface, &src_rect, &dst_rect, pp_flag);
2836
2837     if(obj_surface->subpic != VA_INVALID_ID) {
2838         intel_render_put_subpicture(ctx, surface, &src_rect, &dst_rect);
2839     }
2840
2841     dri_swap_buffer(ctx, dri_drawable);
2842     obj_surface->flags |= SURFACE_DISPLAYED;
2843
2844     if ((obj_surface->flags & SURFACE_ALL_MASK) == SURFACE_DISPLAYED) {
2845         dri_bo_unreference(obj_surface->bo);
2846         obj_surface->bo = NULL;
2847         obj_surface->flags &= ~SURFACE_REF_DIS_MASK;
2848
2849         if (obj_surface->free_private_data)
2850             obj_surface->free_private_data(&obj_surface->private_data);
2851     }
2852
2853     _i965UnlockMutex(&i965->render_mutex);
2854
2855     return VA_STATUS_SUCCESS;
2856 }
2857
2858 VAStatus 
2859 i965_Terminate(VADriverContextP ctx)
2860 {
2861     struct i965_driver_data *i965 = i965_driver_data(ctx);
2862
2863     if (i965->batch)
2864         intel_batchbuffer_free(i965->batch);
2865
2866     _i965DestroyMutex(&i965->render_mutex);
2867
2868     if (i965_render_terminate(ctx) == False)
2869         return VA_STATUS_ERROR_UNKNOWN;
2870
2871     if (i965_post_processing_terminate(ctx) == False)
2872         return VA_STATUS_ERROR_UNKNOWN;
2873
2874     if (intel_driver_terminate(ctx) == False)
2875         return VA_STATUS_ERROR_UNKNOWN;
2876
2877     i965_destroy_heap(&i965->buffer_heap, i965_destroy_buffer);
2878     i965_destroy_heap(&i965->image_heap, i965_destroy_image);
2879     i965_destroy_heap(&i965->subpic_heap, i965_destroy_subpic);
2880     i965_destroy_heap(&i965->surface_heap, i965_destroy_surface);
2881     i965_destroy_heap(&i965->context_heap, i965_destroy_context);
2882     i965_destroy_heap(&i965->config_heap, i965_destroy_config);
2883
2884     free(ctx->pDriverData);
2885     ctx->pDriverData = NULL;
2886
2887     return VA_STATUS_SUCCESS;
2888 }
2889
2890 static VAStatus
2891 i965_BufferInfo(
2892     VADriverContextP ctx,       /* in */
2893     VABufferID buf_id,          /* in */
2894     VABufferType *type,         /* out */
2895     unsigned int *size,         /* out */
2896     unsigned int *num_elements  /* out */
2897 )
2898 {
2899     struct i965_driver_data *i965 = NULL;
2900     struct object_buffer *obj_buffer = NULL;
2901
2902     i965 = i965_driver_data(ctx);
2903     obj_buffer = BUFFER(buf_id);
2904
2905     *type = obj_buffer->type;
2906     *size = obj_buffer->size_element;
2907     *num_elements = obj_buffer->num_elements;
2908
2909     return VA_STATUS_SUCCESS;
2910 }
2911
2912 static VAStatus
2913 i965_LockSurface(
2914     VADriverContextP ctx,           /* in */
2915     VASurfaceID surface,            /* in */
2916     unsigned int *fourcc,           /* out */
2917     unsigned int *luma_stride,      /* out */
2918     unsigned int *chroma_u_stride,  /* out */
2919     unsigned int *chroma_v_stride,  /* out */
2920     unsigned int *luma_offset,      /* out */
2921     unsigned int *chroma_u_offset,  /* out */
2922     unsigned int *chroma_v_offset,  /* out */
2923     unsigned int *buffer_name,      /* out */
2924     void **buffer                   /* out */
2925 )
2926 {
2927     VAStatus vaStatus = VA_STATUS_SUCCESS;
2928     struct i965_driver_data *i965 = i965_driver_data(ctx);
2929     struct object_surface *obj_surface = NULL;
2930     VAImage tmpImage;
2931
2932     assert(fourcc);
2933     assert(luma_stride);
2934     assert(chroma_u_stride);
2935     assert(chroma_v_stride);
2936     assert(luma_offset);
2937     assert(chroma_u_offset);
2938     assert(chroma_v_offset);
2939     assert(buffer_name);
2940     assert(buffer);
2941
2942     tmpImage.image_id = VA_INVALID_ID;
2943
2944     obj_surface = SURFACE(surface);
2945     if (obj_surface == NULL) {
2946         // Surface is absent.
2947         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
2948         goto error;
2949     }
2950
2951     // Lock functionality is absent now.
2952     if (obj_surface->locked_image_id != VA_INVALID_ID) {
2953         // Surface is locked already.
2954         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
2955         goto error;
2956     }
2957
2958     vaStatus = i965_DeriveImage(
2959         ctx,
2960         surface,
2961         &tmpImage);
2962     if (vaStatus != VA_STATUS_SUCCESS) {
2963         goto error;
2964     }
2965
2966     obj_surface->locked_image_id = tmpImage.image_id;
2967
2968     vaStatus = i965_MapBuffer(
2969         ctx,
2970         tmpImage.buf,
2971         buffer);
2972     if (vaStatus != VA_STATUS_SUCCESS) {
2973         goto error;
2974     }
2975
2976     *fourcc = tmpImage.format.fourcc;
2977     *luma_offset = tmpImage.offsets[0];
2978     *luma_stride = tmpImage.pitches[0];
2979     *chroma_u_offset = tmpImage.offsets[1];
2980     *chroma_u_stride = tmpImage.pitches[1];
2981     *chroma_v_offset = tmpImage.offsets[2];
2982     *chroma_v_stride = tmpImage.pitches[2];
2983     *buffer_name = tmpImage.buf;
2984
2985 error:
2986     if (vaStatus != VA_STATUS_SUCCESS) {
2987         buffer = NULL;
2988     }
2989
2990     return vaStatus;
2991 }
2992
2993 static VAStatus
2994 i965_UnlockSurface(
2995     VADriverContextP ctx,   /* in */
2996     VASurfaceID surface     /* in */
2997 )
2998 {
2999     VAStatus vaStatus = VA_STATUS_SUCCESS;
3000     struct i965_driver_data *i965 = i965_driver_data(ctx);
3001     struct object_image *locked_img = NULL;
3002     struct object_surface *obj_surface = NULL;
3003
3004     obj_surface = SURFACE(surface);
3005
3006     if (obj_surface == NULL) {
3007         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;   // Surface is absent
3008         goto error;
3009     }
3010     if (obj_surface->locked_image_id == VA_INVALID_ID) {
3011         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;   // Surface is not locked
3012         goto error;
3013     }
3014
3015     locked_img = IMAGE(obj_surface->locked_image_id);
3016     if (locked_img == NULL || (locked_img->image.image_id == VA_INVALID_ID)) {
3017         // Work image was deallocated before i965_UnlockSurface()
3018         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
3019         goto error;
3020     }
3021
3022     vaStatus = i965_UnmapBuffer(
3023         ctx,
3024         locked_img->image.buf);
3025     if (vaStatus != VA_STATUS_SUCCESS) {
3026         goto error;
3027     }
3028
3029     vaStatus = i965_DestroyImage(
3030         ctx,
3031         locked_img->image.image_id);
3032     if (vaStatus != VA_STATUS_SUCCESS) {
3033         goto error;
3034     }
3035
3036     locked_img->image.image_id = VA_INVALID_ID;
3037
3038  error:
3039     return vaStatus;
3040 }
3041
3042 /* 
3043  * Query video processing pipeline 
3044  */
3045 VAStatus i965_QueryVideoProcPipelineCap(
3046     VADriverContextP ctx,
3047     VAContextID context,
3048     VAProcPipelineCap *pipeline_cap     /* out */
3049     )
3050 {
3051     struct i965_driver_data * const i965 = i965_driver_data(ctx);
3052     int i = 0;
3053
3054     if (HAS_VPP(i965)) {
3055         pipeline_cap->filter_pipeline[i] = VAProcFilterNoiseReduction;
3056         pipeline_cap->bypass[i++] = 1;
3057         pipeline_cap->filter_pipeline[i] = VAProcFilterDeinterlacing;
3058         pipeline_cap->bypass[i++] = 1;
3059     }
3060
3061     for (; i < VA_PROC_PIPELINE_MAX_NUM_FILTERS; i++) {
3062         pipeline_cap->filter_pipeline[i] = VAProcFilterNone;
3063         pipeline_cap->bypass[i] = 1;
3064     }
3065
3066     return VA_STATUS_SUCCESS;
3067 }
3068
3069 VAStatus i965_QueryVideoProcFilterCap(
3070     VADriverContextP ctx,
3071     VAContextID context,
3072     VAProcFilterType filter,
3073     void *cap   /* out */
3074     ) 
3075 {
3076     assert(cap);
3077
3078     if (filter == VAProcFilterNoiseReduction) {
3079         VAProcFilterCapBase *base_cap = cap;
3080         base_cap->range.min = 0.0;
3081         base_cap->range.max = 1.0;
3082         base_cap->range.default_value = 0.5;
3083         base_cap->range.step = 0.03125; /* 1.0 / 32 */
3084     }
3085
3086     return VA_STATUS_SUCCESS;
3087 }
3088
3089 VAStatus i965_QueryVideoProcReferenceFramesCap(
3090     VADriverContextP ctx,
3091     VAContextID context,
3092     unsigned int *num_forward_reference, /* out */
3093     unsigned int *num_backward_reference /* out */
3094     )
3095 {
3096
3097     *num_forward_reference = 0;
3098     *num_backward_reference = 0;
3099
3100     return VA_STATUS_SUCCESS;
3101 }
3102
3103 VAStatus DLL_EXPORT
3104 VA_DRIVER_INIT_FUNC(VADriverContextP ctx);
3105
3106 VAStatus 
3107 VA_DRIVER_INIT_FUNC(  VADriverContextP ctx )
3108 {
3109     struct VADriverVTable * const vtable = ctx->vtable;
3110     struct i965_driver_data *i965;
3111     int result;
3112
3113     ctx->version_major = VA_MAJOR_VERSION;
3114     ctx->version_minor = VA_MINOR_VERSION;
3115     ctx->max_profiles = I965_MAX_PROFILES;
3116     ctx->max_entrypoints = I965_MAX_ENTRYPOINTS;
3117     ctx->max_attributes = I965_MAX_CONFIG_ATTRIBUTES;
3118     ctx->max_image_formats = I965_MAX_IMAGE_FORMATS;
3119     ctx->max_subpic_formats = I965_MAX_SUBPIC_FORMATS;
3120     ctx->max_display_attributes = I965_MAX_DISPLAY_ATTRIBUTES;
3121
3122     vtable->vaTerminate = i965_Terminate;
3123     vtable->vaQueryConfigEntrypoints = i965_QueryConfigEntrypoints;
3124     vtable->vaQueryConfigProfiles = i965_QueryConfigProfiles;
3125     vtable->vaQueryConfigEntrypoints = i965_QueryConfigEntrypoints;
3126     vtable->vaQueryConfigAttributes = i965_QueryConfigAttributes;
3127     vtable->vaCreateConfig = i965_CreateConfig;
3128     vtable->vaDestroyConfig = i965_DestroyConfig;
3129     vtable->vaGetConfigAttributes = i965_GetConfigAttributes;
3130     vtable->vaCreateSurfaces = i965_CreateSurfaces;
3131     vtable->vaDestroySurfaces = i965_DestroySurfaces;
3132     vtable->vaCreateContext = i965_CreateContext;
3133     vtable->vaDestroyContext = i965_DestroyContext;
3134     vtable->vaCreateBuffer = i965_CreateBuffer;
3135     vtable->vaBufferSetNumElements = i965_BufferSetNumElements;
3136     vtable->vaMapBuffer = i965_MapBuffer;
3137     vtable->vaUnmapBuffer = i965_UnmapBuffer;
3138     vtable->vaDestroyBuffer = i965_DestroyBuffer;
3139     vtable->vaBeginPicture = i965_BeginPicture;
3140     vtable->vaRenderPicture = i965_RenderPicture;
3141     vtable->vaEndPicture = i965_EndPicture;
3142     vtable->vaSyncSurface = i965_SyncSurface;
3143     vtable->vaQuerySurfaceStatus = i965_QuerySurfaceStatus;
3144     vtable->vaPutSurface = i965_PutSurface;
3145     vtable->vaQueryImageFormats = i965_QueryImageFormats;
3146     vtable->vaCreateImage = i965_CreateImage;
3147     vtable->vaDeriveImage = i965_DeriveImage;
3148     vtable->vaDestroyImage = i965_DestroyImage;
3149     vtable->vaSetImagePalette = i965_SetImagePalette;
3150     vtable->vaGetImage = i965_GetImage;
3151     vtable->vaPutImage = i965_PutImage;
3152     vtable->vaQuerySubpictureFormats = i965_QuerySubpictureFormats;
3153     vtable->vaCreateSubpicture = i965_CreateSubpicture;
3154     vtable->vaDestroySubpicture = i965_DestroySubpicture;
3155     vtable->vaSetSubpictureImage = i965_SetSubpictureImage;
3156     vtable->vaSetSubpictureChromakey = i965_SetSubpictureChromakey;
3157     vtable->vaSetSubpictureGlobalAlpha = i965_SetSubpictureGlobalAlpha;
3158     vtable->vaAssociateSubpicture = i965_AssociateSubpicture;
3159     vtable->vaDeassociateSubpicture = i965_DeassociateSubpicture;
3160     vtable->vaQueryDisplayAttributes = i965_QueryDisplayAttributes;
3161     vtable->vaGetDisplayAttributes = i965_GetDisplayAttributes;
3162     vtable->vaSetDisplayAttributes = i965_SetDisplayAttributes;
3163     vtable->vaBufferInfo = i965_BufferInfo;
3164     vtable->vaLockSurface = i965_LockSurface;
3165     vtable->vaUnlockSurface = i965_UnlockSurface;
3166     vtable->vaQueryVideoProcPipelineCap = i965_QueryVideoProcPipelineCap;
3167     vtable->vaQueryVideoProcFilterCap = i965_QueryVideoProcFilterCap;
3168     vtable->vaQueryVideoProcReferenceFramesCap = i965_QueryVideoProcReferenceFramesCap;
3169
3170     //    vtable->vaDbgCopySurfaceToBuffer = i965_DbgCopySurfaceToBuffer;
3171
3172     i965 = (struct i965_driver_data *)calloc(1, sizeof(*i965));
3173     assert(i965);
3174     ctx->pDriverData = (void *)i965;
3175
3176     result = object_heap_init(&i965->config_heap, 
3177                               sizeof(struct object_config), 
3178                               CONFIG_ID_OFFSET);
3179     assert(result == 0);
3180
3181     result = object_heap_init(&i965->context_heap, 
3182                               sizeof(struct object_context), 
3183                               CONTEXT_ID_OFFSET);
3184     assert(result == 0);
3185
3186     result = object_heap_init(&i965->surface_heap, 
3187                               sizeof(struct object_surface), 
3188                               SURFACE_ID_OFFSET);
3189     assert(result == 0);
3190
3191     result = object_heap_init(&i965->buffer_heap, 
3192                               sizeof(struct object_buffer), 
3193                               BUFFER_ID_OFFSET);
3194     assert(result == 0);
3195
3196     result = object_heap_init(&i965->image_heap, 
3197                               sizeof(struct object_image), 
3198                               IMAGE_ID_OFFSET);
3199     assert(result == 0);
3200
3201     result = object_heap_init(&i965->subpic_heap, 
3202                               sizeof(struct object_subpic), 
3203                               SUBPIC_ID_OFFSET);
3204     assert(result == 0);
3205
3206     sprintf(i965->va_vendor, "%s %s driver - %d.%d.%d",
3207             INTEL_STR_DRIVER_VENDOR,
3208             INTEL_STR_DRIVER_NAME,
3209             INTEL_DRIVER_MAJOR_VERSION,
3210             INTEL_DRIVER_MINOR_VERSION,
3211             INTEL_DRIVER_MICRO_VERSION);
3212
3213     if (INTEL_DRIVER_PRE_VERSION > 0) {
3214         const int len = strlen(i965->va_vendor);
3215         sprintf(&i965->va_vendor[len], ".pre%d", INTEL_DRIVER_PRE_VERSION);
3216     }
3217
3218     i965->current_context_id = VA_INVALID_ID;
3219
3220     ctx->str_vendor = i965->va_vendor;
3221     
3222     return i965_Init(ctx);
3223 }