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