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