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