OSDN Git Service

decode: release huffman_table from decode state
[android-x86/hardware-intel-common-vaapi.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 #include <unistd.h>
32 #include <dlfcn.h>
33
34 #ifdef HAVE_VA_X11
35 # include "i965_output_dri.h"
36 #endif
37
38 #ifdef HAVE_VA_WAYLAND
39 # include "i965_output_wayland.h"
40 #endif
41
42 #include "intel_version.h"
43 #include "intel_driver.h"
44 #include "intel_memman.h"
45 #include "intel_batchbuffer.h"
46 #include "i965_defines.h"
47 #include "i965_drv_video.h"
48 #include "i965_decoder.h"
49 #include "i965_encoder.h"
50
51 #include "i965_post_processing.h"
52
53 #include "gen9_vp9_encapi.h"
54
55 #define CONFIG_ID_OFFSET                0x01000000
56 #define CONTEXT_ID_OFFSET               0x02000000
57 #define SURFACE_ID_OFFSET               0x04000000
58 #define BUFFER_ID_OFFSET                0x08000000
59 #define IMAGE_ID_OFFSET                 0x0a000000
60 #define SUBPIC_ID_OFFSET                0x10000000
61
62 static int get_sampling_from_fourcc(unsigned int fourcc);
63
64 /* Check whether we are rendering to X11 (VA/X11 or VA/GLX API) */
65 #define IS_VA_X11(ctx) \
66     (((ctx)->display_type & VA_DISPLAY_MAJOR_MASK) == VA_DISPLAY_X11)
67
68 /* Check whether we are rendering to Wayland */
69 #define IS_VA_WAYLAND(ctx) \
70     (((ctx)->display_type & VA_DISPLAY_MAJOR_MASK) == VA_DISPLAY_WAYLAND)
71
72 #define I965_BIT        1
73 #define I965_2BITS      (I965_BIT << 1)
74 #define I965_4BITS      (I965_BIT << 2)
75 #define I965_8BITS      (I965_BIT << 3)
76 #define I965_16BITS     (I965_BIT << 4)
77 #define I965_32BITS     (I965_BIT << 5)
78
79 #define PLANE_0         0
80 #define PLANE_1         1
81 #define PLANE_2         2
82
83 #define OFFSET_0        0
84 #define OFFSET_4        4
85 #define OFFSET_8        8
86 #define OFFSET_16       16
87 #define OFFSET_24       24
88
89 /* hfactor, vfactor, num_planes, bpp[], num_components, components[] */
90 #define I_NV12  2, 2, 2, {I965_8BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_1, OFFSET_8} }
91 #define I_I420  2, 2, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
92 #define I_IYUV  I_I420
93 #define I_IMC3  I_I420
94 #define I_YV12  2, 2, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_2, OFFSET_0}, {PLANE_1, OFFSET_0} }
95 #define I_IMC1  I_YV12
96
97 #define I_P010  2, 2, 2, {I965_16BITS, I965_8BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_1, OFFSET_16} }
98
99 #define I_I010  2, 2, 3, {I965_16BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
100
101 #define I_422H  2, 1, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
102 #define I_422V  1, 2, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
103 #define I_YV16  2, 1, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_2, OFFSET_0}, {PLANE_1, OFFSET_0} }
104 #define I_YUY2  2, 1, 1, {I965_16BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_24} }
105 #define I_UYVY  2, 1, 1, {I965_16BITS}, 3, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_16} }
106
107 #define I_444P  1, 1, 3, {I965_8BITS, I965_8BITS, I965_8BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
108
109 #define I_411P  4, 1, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
110
111 #define I_Y800  1, 1, 1, {I965_8BITS}, 1, { {PLANE_0, OFFSET_0} }
112
113 #define I_RGBA  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_24} }
114 #define I_RGBX  1, 1, 1, {I965_32BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16} }
115 #define I_BGRA  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_24} }
116 #define I_BGRX  1, 1, 1, {I965_32BITS}, 3, { {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
117
118 #define I_ARGB  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_24}, {PLANE_0, OFFSET_0} }
119 #define I_ABGR  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_24}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
120
121 #define I_IA88  1, 1, 1, {I965_16BITS}, 2, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8} }
122 #define I_AI88  1, 1, 1, {I965_16BITS}, 2, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
123
124 #define I_IA44  1, 1, 1, {I965_8BITS}, 2, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_4} }
125 #define I_AI44  1, 1, 1, {I965_8BITS}, 2, { {PLANE_0, OFFSET_4}, {PLANE_0, OFFSET_0} }
126
127 /* flag */
128 #define I_S             1
129 #define I_I             2
130 #define I_SI            (I_S | I_I)
131
132 #define DEF_FOUCC_INFO(FOURCC, FORMAT, SUB, FLAG)       { VA_FOURCC_##FOURCC, I965_COLOR_##FORMAT, SUBSAMPLE_##SUB, FLAG, I_##FOURCC }
133 #define DEF_YUV(FOURCC, SUB, FLAG)                      DEF_FOUCC_INFO(FOURCC, YUV, SUB, FLAG)
134 #define DEF_RGB(FOURCC, SUB, FLAG)                      DEF_FOUCC_INFO(FOURCC, RGB, SUB, FLAG)
135 #define DEF_INDEX(FOURCC, SUB, FLAG)                    DEF_FOUCC_INFO(FOURCC, INDEX, SUB, FLAG)
136
137 static const i965_fourcc_info i965_fourcc_infos[] = {
138     DEF_YUV(NV12, YUV420, I_SI),
139     DEF_YUV(I420, YUV420, I_SI),
140     DEF_YUV(IYUV, YUV420, I_S),
141     DEF_YUV(IMC3, YUV420, I_S),
142     DEF_YUV(YV12, YUV420, I_SI),
143     DEF_YUV(IMC1, YUV420, I_S),
144
145     DEF_YUV(P010, YUV420, I_SI),
146     DEF_YUV(I010, YUV420, I_S),
147
148     DEF_YUV(422H, YUV422H, I_SI),
149     DEF_YUV(422V, YUV422V, I_S),
150     DEF_YUV(YV16, YUV422H, I_S),
151     DEF_YUV(YUY2, YUV422H, I_SI),
152     DEF_YUV(UYVY, YUV422H, I_SI),
153
154     DEF_YUV(444P, YUV444, I_S),
155
156     DEF_YUV(411P, YUV411, I_S),
157
158     DEF_YUV(Y800, YUV400, I_S),
159
160     DEF_RGB(RGBA, RGBX, I_SI),
161     DEF_RGB(RGBX, RGBX, I_SI),
162     DEF_RGB(BGRA, RGBX, I_SI),
163     DEF_RGB(BGRX, RGBX, I_SI),
164
165     DEF_RGB(ARGB, RGBX, I_I),
166     DEF_RGB(ABGR, RGBX, I_I),
167
168     DEF_INDEX(IA88, RGBX, I_I),
169     DEF_INDEX(AI88, RGBX, I_I),
170
171     DEF_INDEX(IA44, RGBX, I_I),
172     DEF_INDEX(AI44, RGBX, I_I)
173 };
174
175 const i965_fourcc_info *
176 get_fourcc_info(unsigned int fourcc)
177 {
178     unsigned int i;
179
180     for (i = 0; i < ARRAY_ELEMS(i965_fourcc_infos); i++) {
181         const i965_fourcc_info * const info = &i965_fourcc_infos[i];
182
183         if (info->fourcc == fourcc)
184             return info;
185     }
186
187     return NULL;
188 }
189
190 static int
191 get_bpp_from_fourcc(unsigned int fourcc)
192 {
193     const i965_fourcc_info *info = get_fourcc_info(fourcc);
194     unsigned int i = 0;
195     unsigned int bpp = 0;
196
197     if (!info)
198         return 0;
199
200     for (i = 0; i < info->num_planes; i++)
201         bpp += info->bpp[i];
202
203     return bpp;
204 }
205
206 enum {
207     I965_SURFACETYPE_RGBA = 1,
208     I965_SURFACETYPE_YUV,
209     I965_SURFACETYPE_INDEXED
210 };
211
212 /* List of supported display attributes */
213 static const VADisplayAttribute i965_display_attributes[] = {
214     {
215         VADisplayAttribBrightness,
216         -100, 100, DEFAULT_BRIGHTNESS,
217         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
218     },
219
220     {
221         VADisplayAttribContrast,
222         0, 100, DEFAULT_CONTRAST,
223         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
224     },
225
226     {
227         VADisplayAttribHue,
228         -180, 180, DEFAULT_HUE,
229         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
230     },
231
232     {
233         VADisplayAttribSaturation,
234         0, 100, DEFAULT_SATURATION,
235         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
236     },
237
238     {
239         VADisplayAttribRotation,
240         0, 3, VA_ROTATION_NONE,
241         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
242     },
243 };
244
245 /* List of supported image formats */
246 typedef struct {
247     unsigned int        type;
248     VAImageFormat       va_format;
249 } i965_image_format_map_t;
250
251 static const i965_image_format_map_t
252 i965_image_formats_map[I965_MAX_IMAGE_FORMATS + 1] = {
253     {
254         I965_SURFACETYPE_YUV,
255         { VA_FOURCC_YV12, VA_LSB_FIRST, 12, }
256     },
257     {
258         I965_SURFACETYPE_YUV,
259         { VA_FOURCC_I420, VA_LSB_FIRST, 12, }
260     },
261     {
262         I965_SURFACETYPE_YUV,
263         { VA_FOURCC_NV12, VA_LSB_FIRST, 12, }
264     },
265     {
266         I965_SURFACETYPE_YUV,
267         { VA_FOURCC_YUY2, VA_LSB_FIRST, 16, }
268     },
269     {
270         I965_SURFACETYPE_YUV,
271         { VA_FOURCC_UYVY, VA_LSB_FIRST, 16, }
272     },
273     {
274         I965_SURFACETYPE_YUV,
275         { VA_FOURCC_422H, VA_LSB_FIRST, 16, }
276     },
277     {
278         I965_SURFACETYPE_RGBA,
279         { VA_FOURCC_RGBX, VA_LSB_FIRST, 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000 }
280     },
281     {
282         I965_SURFACETYPE_RGBA,
283         { VA_FOURCC_BGRX, VA_LSB_FIRST, 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff }
284     },
285     {
286         I965_SURFACETYPE_YUV,
287         { VA_FOURCC_P010, VA_LSB_FIRST, 24, }
288     },
289 };
290
291 /* List of supported subpicture formats */
292 typedef struct {
293     unsigned int        type;
294     unsigned int        format;
295     VAImageFormat       va_format;
296     unsigned int        va_flags;
297 } i965_subpic_format_map_t;
298
299 #define COMMON_SUBPICTURE_FLAGS                 \
300     (VA_SUBPICTURE_DESTINATION_IS_SCREEN_COORD| \
301      VA_SUBPICTURE_GLOBAL_ALPHA)
302
303 static const i965_subpic_format_map_t
304 i965_subpic_formats_map[I965_MAX_SUBPIC_FORMATS + 1] = {
305     {
306         I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_P4A4_UNORM,
307         { VA_FOURCC_IA44, VA_MSB_FIRST, 8, },
308         COMMON_SUBPICTURE_FLAGS
309     },
310     {
311         I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_A4P4_UNORM,
312         { VA_FOURCC_AI44, VA_MSB_FIRST, 8, },
313         COMMON_SUBPICTURE_FLAGS
314     },
315     {
316         I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_P8A8_UNORM,
317         { VA_FOURCC_IA88, VA_MSB_FIRST, 16, },
318         COMMON_SUBPICTURE_FLAGS
319     },
320     {
321         I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_A8P8_UNORM,
322         { VA_FOURCC_AI88, VA_MSB_FIRST, 16, },
323         COMMON_SUBPICTURE_FLAGS
324     },
325     {
326         I965_SURFACETYPE_RGBA, I965_SURFACEFORMAT_B8G8R8A8_UNORM,
327         {
328             VA_FOURCC_BGRA, VA_LSB_FIRST, 32,
329             32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000
330         },
331         COMMON_SUBPICTURE_FLAGS
332     },
333     {
334         I965_SURFACETYPE_RGBA, I965_SURFACEFORMAT_R8G8B8A8_UNORM,
335         {
336             VA_FOURCC_RGBA, VA_LSB_FIRST, 32,
337             32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
338         },
339         COMMON_SUBPICTURE_FLAGS
340     },
341 };
342
343 static const i965_subpic_format_map_t *
344 get_subpic_format(const VAImageFormat *va_format)
345 {
346     unsigned int i;
347     for (i = 0; i965_subpic_formats_map[i].type != 0; i++) {
348         const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[i];
349         if (m->va_format.fourcc == va_format->fourcc &&
350             (m->type == I965_SURFACETYPE_RGBA ?
351              (m->va_format.byte_order == va_format->byte_order &&
352               m->va_format.red_mask   == va_format->red_mask   &&
353               m->va_format.green_mask == va_format->green_mask &&
354               m->va_format.blue_mask  == va_format->blue_mask  &&
355               m->va_format.alpha_mask == va_format->alpha_mask) : 1))
356             return m;
357     }
358     return NULL;
359 }
360
361 /* Checks whether the surface is in busy state */
362 static bool
363 is_surface_busy(struct i965_driver_data *i965,
364                 struct object_surface *obj_surface)
365 {
366     assert(obj_surface != NULL);
367
368     if (obj_surface->locked_image_id != VA_INVALID_ID)
369         return true;
370     if (obj_surface->derived_image_id != VA_INVALID_ID)
371         return true;
372     return false;
373 }
374
375 /* Checks whether the image is in busy state */
376 static bool
377 is_image_busy(struct i965_driver_data *i965, struct object_image *obj_image, VASurfaceID surface)
378 {
379     struct object_buffer *obj_buffer;
380
381     assert(obj_image != NULL);
382
383     if (obj_image->derived_surface != VA_INVALID_ID &&
384         obj_image->derived_surface == surface)
385         return true;
386
387     obj_buffer = BUFFER(obj_image->image.buf);
388     if (obj_buffer && obj_buffer->export_refcount > 0)
389         return true;
390     return false;
391 }
392
393 #define I965_PACKED_HEADER_BASE         0
394 #define I965_SEQ_PACKED_HEADER_BASE     0
395 #define I965_SEQ_PACKED_HEADER_END      2
396 #define I965_PIC_PACKED_HEADER_BASE     2
397 #define I965_PACKED_MISC_HEADER_BASE    4
398
399 int
400 va_enc_packed_type_to_idx(int packed_type)
401 {
402     int idx = 0;
403
404     if (packed_type & VAEncPackedHeaderMiscMask) {
405         idx = I965_PACKED_MISC_HEADER_BASE;
406         packed_type = (~VAEncPackedHeaderMiscMask & packed_type);
407         ASSERT_RET(packed_type > 0, 0);
408         idx += (packed_type - 1);
409     } else {
410         idx = I965_PACKED_HEADER_BASE;
411
412         switch (packed_type) {
413         case VAEncPackedHeaderSequence:
414             idx = I965_SEQ_PACKED_HEADER_BASE + 0;
415             break;
416
417         case VAEncPackedHeaderPicture:
418             idx = I965_PIC_PACKED_HEADER_BASE + 0;
419             break;
420
421         case VAEncPackedHeaderSlice:
422             idx = I965_PIC_PACKED_HEADER_BASE + 1;
423             break;
424
425         default:
426             /* Should not get here */
427             ASSERT_RET(0, 0);
428             break;
429         }
430     }
431
432     ASSERT_RET(idx < 5, 0);
433     return idx;
434 }
435
436 #define CALL_VTABLE(vawr, status, param) status = (vawr->vtable->param)
437
438 static VAStatus
439 i965_surface_wrapper(VADriverContextP ctx, VASurfaceID surface)
440 {
441     struct i965_driver_data *i965 = i965_driver_data(ctx);
442     struct object_surface *obj_surface = SURFACE(surface);
443     VAStatus va_status = VA_STATUS_SUCCESS;
444
445     if (!obj_surface) {
446         return VA_STATUS_ERROR_INVALID_SURFACE;
447     }
448
449     if (obj_surface->wrapper_surface != VA_INVALID_ID) {
450         /* the wrapped surface already exists. just return it */
451         return va_status;
452     }
453
454     if (obj_surface->fourcc == 0)
455         i965_check_alloc_surface_bo(ctx, obj_surface,
456                                     1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
457
458     /*
459      * TBD: Support more surface formats.
460      * Currently only NV12 is support as NV12 is used by decoding.
461      */
462     if (obj_surface->fourcc != VA_FOURCC_NV12)
463         return VA_STATUS_ERROR_INVALID_PARAMETER;
464
465     if ((i965->wrapper_pdrvctx == NULL) ||
466         (obj_surface->bo == NULL))
467         return VA_STATUS_ERROR_INVALID_PARAMETER;
468
469     {
470         int fd_handle;
471         VASurfaceAttrib attrib_list[2];
472         VASurfaceAttribExternalBuffers buffer_descriptor;
473         VAGenericID wrapper_surface;
474
475         if (drm_intel_bo_gem_export_to_prime(obj_surface->bo, &fd_handle) != 0)
476             return VA_STATUS_ERROR_OPERATION_FAILED;
477
478         obj_surface->exported_primefd = fd_handle;
479
480         memset(&attrib_list, 0, sizeof(attrib_list));
481         memset(&buffer_descriptor, 0, sizeof(buffer_descriptor));
482
483         attrib_list[0].type = VASurfaceAttribExternalBufferDescriptor;
484         attrib_list[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
485         attrib_list[0].value.value.p = &buffer_descriptor;
486         attrib_list[0].value.type = VAGenericValueTypePointer;
487
488         attrib_list[1].type = VASurfaceAttribMemoryType;
489         attrib_list[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
490         attrib_list[1].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
491         attrib_list[1].value.type = VAGenericValueTypeInteger;
492
493         buffer_descriptor.num_buffers = 1;
494         buffer_descriptor.num_planes = 2;
495         buffer_descriptor.width = obj_surface->orig_width;
496         buffer_descriptor.height = obj_surface->orig_height;
497         buffer_descriptor.pixel_format = obj_surface->fourcc;
498         buffer_descriptor.data_size = obj_surface->size;
499         buffer_descriptor.pitches[0] = obj_surface->width;
500         buffer_descriptor.pitches[1] = obj_surface->cb_cr_pitch;
501         buffer_descriptor.offsets[0] = 0;
502         buffer_descriptor.offsets[1] = obj_surface->width * obj_surface->height;
503         buffer_descriptor.buffers = (void *)&fd_handle;
504
505         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
506                     vaCreateSurfaces2(i965->wrapper_pdrvctx,
507                                       VA_RT_FORMAT_YUV420,
508                                       obj_surface->orig_width,
509                                       obj_surface->orig_height,
510                                       &wrapper_surface, 1,
511                                       attrib_list, 2));
512
513         if (va_status == VA_STATUS_SUCCESS) {
514             obj_surface->wrapper_surface = wrapper_surface;
515         } else {
516             /* This needs to be checked */
517             va_status = VA_STATUS_ERROR_OPERATION_FAILED;
518         }
519         return va_status;
520     }
521
522 }
523
524 VAStatus
525 i965_QueryConfigProfiles(VADriverContextP ctx,
526                          VAProfile *profile_list,       /* out */
527                          int *num_profiles)             /* out */
528 {
529     struct i965_driver_data * const i965 = i965_driver_data(ctx);
530     int i = 0;
531
532     if (HAS_MPEG2_DECODING(i965) ||
533         HAS_MPEG2_ENCODING(i965)) {
534         profile_list[i++] = VAProfileMPEG2Simple;
535         profile_list[i++] = VAProfileMPEG2Main;
536     }
537
538     if (HAS_H264_DECODING(i965) ||
539         HAS_H264_ENCODING(i965) ||
540         HAS_LP_H264_ENCODING(i965)) {
541         profile_list[i++] = VAProfileH264ConstrainedBaseline;
542         profile_list[i++] = VAProfileH264Main;
543         profile_list[i++] = VAProfileH264High;
544     }
545     if (HAS_H264_MVC_DECODING_PROFILE(i965, VAProfileH264MultiviewHigh) ||
546         HAS_H264_MVC_ENCODING(i965))
547         profile_list[i++] = VAProfileH264MultiviewHigh;
548     if (HAS_H264_MVC_DECODING_PROFILE(i965, VAProfileH264StereoHigh) ||
549         HAS_H264_MVC_ENCODING(i965))
550         profile_list[i++] = VAProfileH264StereoHigh;
551
552     if (HAS_VC1_DECODING(i965)) {
553         profile_list[i++] = VAProfileVC1Simple;
554         profile_list[i++] = VAProfileVC1Main;
555         profile_list[i++] = VAProfileVC1Advanced;
556     }
557
558     if (HAS_VPP(i965)) {
559         profile_list[i++] = VAProfileNone;
560     }
561
562     if (HAS_JPEG_DECODING(i965) ||
563         HAS_JPEG_ENCODING(i965)) {
564         profile_list[i++] = VAProfileJPEGBaseline;
565     }
566
567     if (HAS_VP8_DECODING(i965) ||
568         HAS_VP8_ENCODING(i965)) {
569         profile_list[i++] = VAProfileVP8Version0_3;
570     }
571
572     if (HAS_HEVC_DECODING(i965) ||
573         HAS_HEVC_ENCODING(i965)) {
574         profile_list[i++] = VAProfileHEVCMain;
575     }
576
577     if (HAS_HEVC10_DECODING(i965) ||
578         HAS_HEVC10_ENCODING(i965)) {
579         profile_list[i++] = VAProfileHEVCMain10;
580     }
581
582     if (HAS_VP9_DECODING_PROFILE(i965, VAProfileVP9Profile0) ||
583         HAS_VP9_ENCODING(i965)) {
584         profile_list[i++] = VAProfileVP9Profile0;
585     }
586
587     if (HAS_VP9_DECODING_PROFILE(i965, VAProfileVP9Profile2)) {
588         profile_list[i++] = VAProfileVP9Profile2;
589     }
590
591     if (i965->wrapper_pdrvctx) {
592         VAProfile wrapper_list[4];
593         int wrapper_num;
594         VADriverContextP pdrvctx;
595         VAStatus va_status;
596
597         pdrvctx = i965->wrapper_pdrvctx;
598         CALL_VTABLE(pdrvctx, va_status,
599                     vaQueryConfigProfiles(pdrvctx,
600                                           wrapper_list, &wrapper_num));
601
602         if (va_status == VA_STATUS_SUCCESS) {
603             int j;
604             for (j = 0; j < wrapper_num; j++)
605                 if (wrapper_list[j] != VAProfileNone)
606                     profile_list[i++] = wrapper_list[j];
607         }
608     }
609
610     /* If the assert fails then I965_MAX_PROFILES needs to be bigger */
611     ASSERT_RET(i <= I965_MAX_PROFILES, VA_STATUS_ERROR_OPERATION_FAILED);
612     *num_profiles = i;
613
614     return VA_STATUS_SUCCESS;
615 }
616
617 VAStatus
618 i965_QueryConfigEntrypoints(VADriverContextP ctx,
619                             VAProfile profile,
620                             VAEntrypoint *entrypoint_list,      /* out */
621                             int *num_entrypoints)               /* out */
622 {
623     struct i965_driver_data * const i965 = i965_driver_data(ctx);
624     int n = 0;
625
626     switch (profile) {
627     case VAProfileMPEG2Simple:
628     case VAProfileMPEG2Main:
629         if (HAS_MPEG2_DECODING(i965))
630             entrypoint_list[n++] = VAEntrypointVLD;
631
632         if (HAS_MPEG2_ENCODING(i965))
633             entrypoint_list[n++] = VAEntrypointEncSlice;
634
635         break;
636
637     case VAProfileH264ConstrainedBaseline:
638     case VAProfileH264Main:
639     case VAProfileH264High:
640         if (HAS_H264_DECODING(i965))
641             entrypoint_list[n++] = VAEntrypointVLD;
642
643         if (HAS_H264_ENCODING(i965))
644             entrypoint_list[n++] = VAEntrypointEncSlice;
645
646         if (HAS_LP_H264_ENCODING(i965))
647             entrypoint_list[n++] = VAEntrypointEncSliceLP;
648
649         break;
650     case VAProfileH264MultiviewHigh:
651     case VAProfileH264StereoHigh:
652         if (HAS_H264_MVC_DECODING_PROFILE(i965, profile))
653             entrypoint_list[n++] = VAEntrypointVLD;
654
655         if (HAS_H264_MVC_ENCODING(i965))
656             entrypoint_list[n++] = VAEntrypointEncSlice;
657         break;
658
659     case VAProfileVC1Simple:
660     case VAProfileVC1Main:
661     case VAProfileVC1Advanced:
662         if (HAS_VC1_DECODING(i965))
663             entrypoint_list[n++] = VAEntrypointVLD;
664         break;
665
666     case VAProfileNone:
667         if (HAS_VPP(i965))
668             entrypoint_list[n++] = VAEntrypointVideoProc;
669         break;
670
671     case VAProfileJPEGBaseline:
672         if (HAS_JPEG_DECODING(i965))
673             entrypoint_list[n++] = VAEntrypointVLD;
674
675         if (HAS_JPEG_ENCODING(i965))
676             entrypoint_list[n++] = VAEntrypointEncPicture;
677         break;
678
679     case VAProfileVP8Version0_3:
680         if (HAS_VP8_DECODING(i965))
681             entrypoint_list[n++] = VAEntrypointVLD;
682
683         if (HAS_VP8_ENCODING(i965))
684             entrypoint_list[n++] = VAEntrypointEncSlice;
685
686         break;
687
688     case VAProfileHEVCMain:
689         if (HAS_HEVC_DECODING(i965))
690             entrypoint_list[n++] = VAEntrypointVLD;
691
692         if (HAS_HEVC_ENCODING(i965))
693             entrypoint_list[n++] = VAEntrypointEncSlice;
694
695         break;
696
697     case VAProfileHEVCMain10:
698         if (HAS_HEVC10_DECODING(i965))
699             entrypoint_list[n++] = VAEntrypointVLD;
700
701         if (HAS_HEVC10_ENCODING(i965))
702             entrypoint_list[n++] = VAEntrypointEncSlice;
703
704         break;
705
706     case VAProfileVP9Profile0:
707     case VAProfileVP9Profile2:
708         if (HAS_VP9_DECODING_PROFILE(i965, profile))
709             entrypoint_list[n++] = VAEntrypointVLD;
710
711         if (HAS_VP9_ENCODING(i965) && (profile == VAProfileVP9Profile0))
712             entrypoint_list[n++] = VAEntrypointEncSlice;
713
714         if (profile == VAProfileVP9Profile0) {
715             if (i965->wrapper_pdrvctx) {
716                 VAStatus va_status = VA_STATUS_SUCCESS;
717                 VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
718
719                 CALL_VTABLE(pdrvctx, va_status,
720                             vaQueryConfigEntrypoints(pdrvctx, profile,
721                                                      entrypoint_list,
722                                                      num_entrypoints));
723                 return va_status;
724             }
725         }
726
727         break;
728
729     default:
730         break;
731     }
732
733     /* If the assert fails then I965_MAX_ENTRYPOINTS needs to be bigger */
734     ASSERT_RET(n <= I965_MAX_ENTRYPOINTS, VA_STATUS_ERROR_OPERATION_FAILED);
735     *num_entrypoints = n;
736     return n > 0 ? VA_STATUS_SUCCESS : VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
737 }
738
739 static VAStatus
740 i965_validate_config(VADriverContextP ctx, VAProfile profile,
741                      VAEntrypoint entrypoint)
742 {
743     struct i965_driver_data * const i965 = i965_driver_data(ctx);
744     VAStatus va_status;
745
746     /* Validate profile & entrypoint */
747     switch (profile) {
748     case VAProfileMPEG2Simple:
749     case VAProfileMPEG2Main:
750         if ((HAS_MPEG2_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
751             (HAS_MPEG2_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)) {
752             va_status = VA_STATUS_SUCCESS;
753         } else if (!HAS_MPEG2_DECODING(i965) && !HAS_MPEG2_ENCODING(i965)) {
754             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
755         } else {
756             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
757         }
758         break;
759
760     case VAProfileH264ConstrainedBaseline:
761     case VAProfileH264Main:
762     case VAProfileH264High:
763         if ((HAS_H264_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
764             (HAS_H264_ENCODING(i965) && entrypoint == VAEntrypointEncSlice) ||
765             (HAS_LP_H264_ENCODING(i965) && entrypoint == VAEntrypointEncSliceLP)) {
766             va_status = VA_STATUS_SUCCESS;
767         } else if (!HAS_H264_DECODING(i965) && !HAS_H264_ENCODING(i965) &&
768                    !HAS_LP_H264_ENCODING(i965)) {
769             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
770         } else {
771             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
772         }
773         break;
774
775     case VAProfileVC1Simple:
776     case VAProfileVC1Main:
777     case VAProfileVC1Advanced:
778         if (HAS_VC1_DECODING(i965) && entrypoint == VAEntrypointVLD) {
779             va_status = VA_STATUS_SUCCESS;
780         } else if (!HAS_VC1_DECODING(i965)) {
781             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
782         } else {
783             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
784         }
785         break;
786
787     case VAProfileNone:
788         if (HAS_VPP(i965) && VAEntrypointVideoProc == entrypoint) {
789             va_status = VA_STATUS_SUCCESS;
790         } else if (!HAS_VPP(i965)) {
791             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
792         } else {
793             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
794         }
795         break;
796
797     case VAProfileJPEGBaseline:
798         if ((HAS_JPEG_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
799             (HAS_JPEG_ENCODING(i965) && entrypoint == VAEntrypointEncPicture)) {
800             va_status = VA_STATUS_SUCCESS;
801         } else if (!HAS_JPEG_DECODING(i965) && !HAS_JPEG_ENCODING(i965)) {
802             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
803         } else {
804             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
805         }
806         break;
807
808     case VAProfileVP8Version0_3:
809         if ((HAS_VP8_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
810             (HAS_VP8_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)) {
811             va_status = VA_STATUS_SUCCESS;
812         } else if (!HAS_VP8_DECODING(i965) && !HAS_VP8_ENCODING(i965)) {
813             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
814         } else {
815             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
816         }
817         break;
818
819     case VAProfileH264MultiviewHigh:
820     case VAProfileH264StereoHigh:
821         if ((HAS_H264_MVC_DECODING_PROFILE(i965, profile) &&
822              entrypoint == VAEntrypointVLD) ||
823             (HAS_H264_MVC_ENCODING(i965) &&
824              entrypoint == VAEntrypointEncSlice)) {
825             va_status = VA_STATUS_SUCCESS;
826         } else if (!HAS_H264_MVC_DECODING_PROFILE(i965, profile) &&
827                    !HAS_H264_MVC_ENCODING(i965)) {
828             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
829         } else {
830             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
831         }
832
833         break;
834
835     case VAProfileHEVCMain:
836         if ((HAS_HEVC_DECODING(i965) && (entrypoint == VAEntrypointVLD)) ||
837             (HAS_HEVC_ENCODING(i965) && (entrypoint == VAEntrypointEncSlice))) {
838             va_status = VA_STATUS_SUCCESS;
839         } else if (!HAS_HEVC_DECODING(i965) && !HAS_HEVC_ENCODING(i965)) {
840             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
841         } else {
842             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
843         }
844
845         break;
846
847     case VAProfileHEVCMain10:
848         if ((HAS_HEVC10_DECODING(i965) && (entrypoint == VAEntrypointVLD)) ||
849             (HAS_HEVC10_ENCODING(i965) &&
850              (entrypoint == VAEntrypointEncSlice))) {
851             va_status = VA_STATUS_SUCCESS;
852         } else if (!HAS_HEVC10_DECODING(i965) && !HAS_HEVC10_ENCODING(i965)) {
853             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
854         } else {
855             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
856         }
857
858         break;
859
860     case VAProfileVP9Profile0:
861     case VAProfileVP9Profile2:
862         if ((HAS_VP9_DECODING_PROFILE(i965, profile)) &&
863             (entrypoint == VAEntrypointVLD)) {
864             va_status = VA_STATUS_SUCCESS;
865         } else if ((HAS_VP9_ENCODING_PROFILE(i965, profile)) &&
866                    (entrypoint == VAEntrypointEncSlice)) {
867             va_status = VA_STATUS_SUCCESS;
868         } else if (profile == VAProfileVP9Profile0 &&
869                    entrypoint == VAEntrypointVLD &&
870                    i965->wrapper_pdrvctx) {
871             va_status = VA_STATUS_SUCCESS;
872         } else if (!HAS_VP9_DECODING_PROFILE(i965, profile) &&
873                    !HAS_VP9_ENCODING(i965) && !i965->wrapper_pdrvctx) {
874             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
875         } else {
876             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
877         }
878
879         break;
880
881     default:
882         va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
883         break;
884     }
885     return va_status;
886 }
887
888 static uint32_t
889 i965_get_default_chroma_formats(VADriverContextP ctx, VAProfile profile,
890                                 VAEntrypoint entrypoint)
891 {
892     struct i965_driver_data * const i965 = i965_driver_data(ctx);
893     uint32_t chroma_formats = VA_RT_FORMAT_YUV420;
894
895     switch (profile) {
896     case VAProfileH264ConstrainedBaseline:
897     case VAProfileH264Main:
898     case VAProfileH264High:
899         if (HAS_H264_DECODING(i965) && entrypoint == VAEntrypointVLD)
900             chroma_formats |= i965->codec_info->h264_dec_chroma_formats;
901         break;
902
903     case VAProfileH264MultiviewHigh:
904     case VAProfileH264StereoHigh:
905         if (HAS_H264_MVC_DECODING(i965) && entrypoint == VAEntrypointVLD)
906             chroma_formats |= i965->codec_info->h264_dec_chroma_formats;
907         break;
908
909     case VAProfileJPEGBaseline:
910         if (HAS_JPEG_DECODING(i965) && entrypoint == VAEntrypointVLD)
911             chroma_formats |= i965->codec_info->jpeg_dec_chroma_formats;
912         if (HAS_JPEG_ENCODING(i965) && entrypoint == VAEntrypointEncPicture)
913             chroma_formats |= i965->codec_info->jpeg_enc_chroma_formats;
914         break;
915
916     case VAProfileHEVCMain10:
917         if (HAS_HEVC10_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)
918             chroma_formats = VA_RT_FORMAT_YUV420_10BPP;
919         if (HAS_HEVC10_DECODING(i965) && entrypoint == VAEntrypointVLD)
920             chroma_formats |= i965->codec_info->hevc_dec_chroma_formats;
921         break;
922
923     case VAProfileNone:
924         if (HAS_VPP_P010(i965))
925             chroma_formats |= VA_RT_FORMAT_YUV420_10BPP;
926
927         if (HAS_VPP(i965))
928             chroma_formats |= VA_RT_FORMAT_YUV422 | VA_RT_FORMAT_RGB32;
929         break;
930
931     case VAProfileVP9Profile0:
932     case VAProfileVP9Profile2:
933         if (HAS_VP9_DECODING_PROFILE(i965, profile) && entrypoint == VAEntrypointVLD)
934             chroma_formats |= i965->codec_info->vp9_dec_chroma_formats;
935         break;
936
937     default:
938         break;
939     }
940     return chroma_formats;
941 }
942
943 VAStatus
944 i965_GetConfigAttributes(VADriverContextP ctx,
945                          VAProfile profile,
946                          VAEntrypoint entrypoint,
947                          VAConfigAttrib *attrib_list,  /* in/out */
948                          int num_attribs)
949 {
950     struct i965_driver_data * const i965 = i965_driver_data(ctx);
951     VAStatus va_status;
952     int i;
953
954     va_status = i965_validate_config(ctx, profile, entrypoint);
955     if (va_status != VA_STATUS_SUCCESS)
956         return va_status;
957
958     /* Other attributes don't seem to be defined */
959     /* What to do if we don't know the attribute? */
960     for (i = 0; i < num_attribs; i++) {
961         attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
962         switch (attrib_list[i].type) {
963         case VAConfigAttribRTFormat:
964             attrib_list[i].value = i965_get_default_chroma_formats(ctx,
965                                                                    profile, entrypoint);
966             break;
967
968         case VAConfigAttribRateControl:
969             if (entrypoint == VAEntrypointEncSlice) {
970                 attrib_list[i].value = VA_RC_CQP;
971
972                 if (profile != VAProfileMPEG2Main &&
973                     profile != VAProfileMPEG2Simple)
974                     attrib_list[i].value |= VA_RC_CBR;
975
976                 if (profile == VAProfileVP8Version0_3 ||
977                     profile == VAProfileVP9Profile0 ||
978                     profile == VAProfileH264ConstrainedBaseline ||
979                     profile == VAProfileH264Main ||
980                     profile == VAProfileH264High ||
981                     profile == VAProfileHEVCMain)
982                     attrib_list[i].value |= VA_RC_VBR;
983
984                 break;
985             } else if (entrypoint == VAEntrypointEncSliceLP) {
986                 struct i965_driver_data * const i965 = i965_driver_data(ctx);
987
988                 /* Support low power encoding for H.264 only by now */
989                 if (profile == VAProfileH264ConstrainedBaseline ||
990                     profile == VAProfileH264Main ||
991                     profile == VAProfileH264High)
992                     attrib_list[i].value = i965->codec_info->lp_h264_brc_mode;
993                 else
994                     attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
995             } else
996                 attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
997
998             break;
999
1000         case VAConfigAttribEncPackedHeaders:
1001             if (entrypoint == VAEntrypointEncSlice ||
1002                 entrypoint == VAEntrypointEncSliceLP) {
1003                 attrib_list[i].value = VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE | VA_ENC_PACKED_HEADER_MISC;
1004                 if (profile == VAProfileH264ConstrainedBaseline ||
1005                     profile == VAProfileH264Main ||
1006                     profile == VAProfileH264High ||
1007                     profile == VAProfileH264StereoHigh ||
1008                     profile == VAProfileH264MultiviewHigh ||
1009                     profile == VAProfileHEVCMain ||
1010                     profile == VAProfileHEVCMain10) {
1011                     attrib_list[i].value |= (VA_ENC_PACKED_HEADER_RAW_DATA |
1012                                              VA_ENC_PACKED_HEADER_SLICE);
1013                 } else if (profile == VAProfileVP9Profile0)
1014                     attrib_list[i].value = VA_ENC_PACKED_HEADER_RAW_DATA;
1015                 break;
1016             } else if (entrypoint == VAEntrypointEncPicture) {
1017                 if (profile == VAProfileJPEGBaseline)
1018                     attrib_list[i].value = VA_ENC_PACKED_HEADER_RAW_DATA;
1019             }
1020             break;
1021
1022         case VAConfigAttribEncMaxRefFrames:
1023             if (entrypoint == VAEntrypointEncSlice)
1024                 attrib_list[i].value = (1 << 16) | (1 << 0);
1025             else if (entrypoint == VAEntrypointEncSliceLP) {
1026                 /* Don't support B frame for low power mode */
1027                 if (profile == VAProfileH264ConstrainedBaseline ||
1028                     profile == VAProfileH264Main ||
1029                     profile == VAProfileH264High)
1030                     attrib_list[i].value = (1 << 0);
1031                 else
1032                     attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1033             }
1034
1035             break;
1036
1037         case VAConfigAttribEncQualityRange:
1038             if (entrypoint == VAEntrypointEncSlice ||
1039                 entrypoint == VAEntrypointEncSliceLP) {
1040                 attrib_list[i].value = 1;
1041                 if (profile == VAProfileH264ConstrainedBaseline ||
1042                     profile == VAProfileH264Main ||
1043                     profile == VAProfileH264High) {
1044                     attrib_list[i].value = ENCODER_QUALITY_RANGE;
1045                     if (entrypoint == VAEntrypointEncSlice) {
1046                         if (IS_GEN9(i965->intel.device_info))
1047                             attrib_list[i].value = ENCODER_QUALITY_RANGE_AVC;
1048                     }
1049                 } else if (profile == VAProfileHEVCMain ||
1050                            profile == VAProfileHEVCMain10)
1051                     attrib_list[i].value = ENCODER_QUALITY_RANGE_HEVC;
1052                 break;
1053             }
1054             break;
1055
1056         case VAConfigAttribEncJPEG:
1057             if (entrypoint == VAEntrypointEncPicture) {
1058                 VAConfigAttribValEncJPEG *configVal = (VAConfigAttribValEncJPEG*) & (attrib_list[i].value);
1059                 (configVal->bits).arithmatic_coding_mode = 0; // Huffman coding is used
1060                 (configVal->bits).progressive_dct_mode = 0;   // Only Sequential DCT is supported
1061                 (configVal->bits).non_interleaved_mode = 1;   // Support both interleaved and non-interleaved
1062                 (configVal->bits).differential_mode = 0;      // Baseline DCT is non-differential
1063                 (configVal->bits).max_num_components = 3;     // Only 3 components supported
1064                 (configVal->bits).max_num_scans = 1;          // Only 1 scan per frame
1065                 (configVal->bits).max_num_huffman_tables = 3; // Max 3 huffman tables
1066                 (configVal->bits).max_num_quantization_tables = 3; // Max 3 quantization tables
1067             }
1068             break;
1069
1070         case VAConfigAttribDecSliceMode:
1071             attrib_list[i].value = VA_DEC_SLICE_MODE_NORMAL;
1072             break;
1073
1074         case VAConfigAttribEncROI:
1075             if (entrypoint == VAEntrypointEncSlice ||
1076                 entrypoint == VAEntrypointEncSliceLP) {
1077
1078                 if (profile == VAProfileH264ConstrainedBaseline ||
1079                     profile == VAProfileH264Main ||
1080                     profile == VAProfileH264High) {
1081
1082                     if (IS_GEN9(i965->intel.device_info) &&
1083                         entrypoint == VAEntrypointEncSlice)
1084                         attrib_list[i].value = 0;
1085                     else {
1086
1087                         VAConfigAttribValEncROI *roi_config =
1088                             (VAConfigAttribValEncROI *) & (attrib_list[i].value);
1089
1090                         if (entrypoint == VAEntrypointEncSliceLP) {
1091                             roi_config->bits.num_roi_regions = 3;
1092                             roi_config->bits.roi_rc_priority_support = 0;
1093                             roi_config->bits.roi_rc_qp_delat_support = 0;
1094                         } else {
1095                             roi_config->bits.num_roi_regions =
1096                                 I965_MAX_NUM_ROI_REGIONS;
1097                             roi_config->bits.roi_rc_priority_support = 0;
1098                             roi_config->bits.roi_rc_qp_delat_support = 1;
1099                         }
1100                     }
1101                 } else {
1102                     attrib_list[i].value = 0;
1103                 }
1104             }
1105
1106             break;
1107
1108         case VAConfigAttribEncRateControlExt:
1109             if ((profile == VAProfileH264ConstrainedBaseline ||
1110                  profile == VAProfileH264Main ||
1111                  profile == VAProfileH264High) &&
1112                 entrypoint == VAEntrypointEncSlice) {
1113                 if (IS_GEN9(i965->intel.device_info))
1114                     attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1115                 else {
1116                     VAConfigAttribValEncRateControlExt *val_config = (VAConfigAttribValEncRateControlExt *) & (attrib_list[i].value);
1117
1118                     val_config->bits.max_num_temporal_layers_minus1 = MAX_TEMPORAL_LAYERS - 1;
1119                     val_config->bits.temporal_layer_bitrate_control_flag = 1;
1120                 }
1121             } else {
1122                 attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1123             }
1124
1125             break;
1126
1127         case VAConfigAttribEncMaxSlices:
1128             attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1129             if (entrypoint == VAEntrypointEncSlice) {
1130                 if ((profile == VAProfileH264ConstrainedBaseline ||
1131                      profile == VAProfileH264Main ||
1132                      profile == VAProfileH264High) ||
1133                     profile == VAProfileH264StereoHigh ||
1134                     profile == VAProfileH264MultiviewHigh) {
1135                     attrib_list[i].value = I965_MAX_NUM_SLICE;
1136                 } else if (profile == VAProfileHEVCMain ||
1137                            profile == VAProfileHEVCMain10) {
1138                     attrib_list[i].value = I965_MAX_NUM_SLICE;
1139                 }
1140             } else if (entrypoint == VAEntrypointEncSliceLP) {
1141                 if ((profile == VAProfileH264ConstrainedBaseline ||
1142                      profile == VAProfileH264Main ||
1143                      profile == VAProfileH264High) ||
1144                     profile == VAProfileH264StereoHigh ||
1145                     profile == VAProfileH264MultiviewHigh)
1146                     attrib_list[i].value = I965_MAX_NUM_SLICE;
1147             }
1148
1149             break;
1150
1151         case VAConfigAttribEncSliceStructure:
1152             attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1153             if (entrypoint == VAEntrypointEncSlice) {
1154                 if ((profile == VAProfileH264ConstrainedBaseline ||
1155                      profile == VAProfileH264Main ||
1156                      profile == VAProfileH264High) ||
1157                     profile == VAProfileH264StereoHigh ||
1158                     profile == VAProfileH264MultiviewHigh ||
1159                     profile == VAProfileHEVCMain ||
1160                     profile == VAProfileHEVCMain10) {
1161                     attrib_list[i].value = VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS;
1162                 }
1163             }
1164
1165             break;
1166         default:
1167             /* Do nothing */
1168             attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1169             break;
1170         }
1171     }
1172
1173     return VA_STATUS_SUCCESS;
1174 }
1175
1176 static void
1177 i965_destroy_config(struct object_heap *heap, struct object_base *obj)
1178 {
1179     object_heap_free(heap, obj);
1180 }
1181
1182 static VAConfigAttrib *
1183 i965_lookup_config_attribute(struct object_config *obj_config,
1184                              VAConfigAttribType type)
1185 {
1186     int i;
1187
1188     for (i = 0; i < obj_config->num_attribs; i++) {
1189         VAConfigAttrib * const attrib = &obj_config->attrib_list[i];
1190         if (attrib->type == type)
1191             return attrib;
1192     }
1193     return NULL;
1194 }
1195
1196 static VAStatus
1197 i965_append_config_attribute(struct object_config *obj_config,
1198                              const VAConfigAttrib *new_attrib)
1199 {
1200     VAConfigAttrib *attrib;
1201
1202     if (obj_config->num_attribs >= I965_MAX_CONFIG_ATTRIBUTES)
1203         return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
1204
1205     attrib = &obj_config->attrib_list[obj_config->num_attribs++];
1206     attrib->type = new_attrib->type;
1207     attrib->value = new_attrib->value;
1208     return VA_STATUS_SUCCESS;
1209 }
1210
1211 static VAStatus
1212 i965_ensure_config_attribute(struct object_config *obj_config,
1213                              const VAConfigAttrib *new_attrib)
1214 {
1215     VAConfigAttrib *attrib;
1216
1217     /* Check for existing attributes */
1218     attrib = i965_lookup_config_attribute(obj_config, new_attrib->type);
1219     if (attrib) {
1220         /* Update existing attribute */
1221         attrib->value = new_attrib->value;
1222         return VA_STATUS_SUCCESS;
1223     }
1224     return i965_append_config_attribute(obj_config, new_attrib);
1225 }
1226
1227 VAStatus
1228 i965_CreateConfig(VADriverContextP ctx,
1229                   VAProfile profile,
1230                   VAEntrypoint entrypoint,
1231                   VAConfigAttrib *attrib_list,
1232                   int num_attribs,
1233                   VAConfigID *config_id)        /* out */
1234 {
1235     struct i965_driver_data * const i965 = i965_driver_data(ctx);
1236     struct object_config *obj_config;
1237     int configID;
1238     int i;
1239     VAStatus vaStatus;
1240
1241     vaStatus = i965_validate_config(ctx, profile, entrypoint);
1242
1243     if (VA_STATUS_SUCCESS != vaStatus) {
1244         return vaStatus;
1245     }
1246
1247     configID = NEW_CONFIG_ID();
1248     obj_config = CONFIG(configID);
1249
1250     if (NULL == obj_config) {
1251         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1252         return vaStatus;
1253     }
1254
1255     obj_config->profile = profile;
1256     obj_config->entrypoint = entrypoint;
1257     obj_config->num_attribs = 0;
1258     obj_config->wrapper_config = VA_INVALID_ID;
1259
1260     for (i = 0; i < num_attribs; i++) {
1261         // add it later and ignore the user input for VAConfigAttribEncMaxSlices
1262         if (attrib_list[i].type == VAConfigAttribEncMaxSlices ||
1263             attrib_list[i].type == VAConfigAttribEncSliceStructure)
1264             continue;
1265         vaStatus = i965_ensure_config_attribute(obj_config, &attrib_list[i]);
1266         if (vaStatus != VA_STATUS_SUCCESS)
1267             break;
1268     }
1269
1270     if (vaStatus == VA_STATUS_SUCCESS) {
1271         VAConfigAttrib attrib, *attrib_found;
1272         attrib.type = VAConfigAttribRTFormat;
1273         attrib.value = i965_get_default_chroma_formats(ctx, profile, entrypoint);
1274         attrib_found = i965_lookup_config_attribute(obj_config, attrib.type);
1275         if (!attrib_found || !attrib_found->value)
1276             vaStatus = i965_append_config_attribute(obj_config, &attrib);
1277         else if (!(attrib_found->value & attrib.value))
1278             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
1279     }
1280
1281     if (vaStatus == VA_STATUS_SUCCESS) {
1282         VAConfigAttrib attrib;
1283         attrib.type = VAConfigAttribEncMaxSlices;
1284         attrib.value = VA_ATTRIB_NOT_SUPPORTED;
1285         if (entrypoint == VAEntrypointEncSlice) {
1286             if ((profile == VAProfileH264ConstrainedBaseline ||
1287                  profile == VAProfileH264Main ||
1288                  profile == VAProfileH264High) ||
1289                 profile == VAProfileH264StereoHigh ||
1290                 profile == VAProfileH264MultiviewHigh) {
1291                 attrib.value = I965_MAX_NUM_SLICE;
1292             } else if (profile == VAProfileHEVCMain ||
1293                        profile == VAProfileHEVCMain10)
1294                 attrib.value = I965_MAX_NUM_SLICE;
1295         } else if (entrypoint == VAEntrypointEncSliceLP) {
1296             if ((profile == VAProfileH264ConstrainedBaseline ||
1297                  profile == VAProfileH264Main ||
1298                  profile == VAProfileH264High) ||
1299                 profile == VAProfileH264StereoHigh ||
1300                 profile == VAProfileH264MultiviewHigh)
1301                 attrib.value = I965_MAX_NUM_SLICE;
1302         }
1303
1304         if (attrib.value != VA_ATTRIB_NOT_SUPPORTED)
1305             vaStatus = i965_append_config_attribute(obj_config, &attrib);
1306     }
1307
1308     if (vaStatus == VA_STATUS_SUCCESS) {
1309         VAConfigAttrib attrib;
1310         attrib.type = VAConfigAttribEncSliceStructure;
1311         attrib.value = VA_ATTRIB_NOT_SUPPORTED;
1312         if (entrypoint == VAEntrypointEncSlice) {
1313             if ((profile == VAProfileH264ConstrainedBaseline ||
1314                  profile == VAProfileH264Main ||
1315                  profile == VAProfileH264High) ||
1316                 profile == VAProfileH264StereoHigh ||
1317                 profile == VAProfileH264MultiviewHigh) {
1318                 attrib.value = VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS;
1319             }
1320         }
1321
1322         if (attrib.value != VA_ATTRIB_NOT_SUPPORTED)
1323             vaStatus = i965_append_config_attribute(obj_config, &attrib);
1324     }
1325
1326     if ((vaStatus == VA_STATUS_SUCCESS) &&
1327         (profile == VAProfileVP9Profile0) &&
1328         (entrypoint == VAEntrypointVLD) &&
1329         !HAS_VP9_DECODING(i965)) {
1330
1331         if (i965->wrapper_pdrvctx) {
1332             VAGenericID wrapper_config;
1333
1334             CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
1335                         vaCreateConfig(i965->wrapper_pdrvctx, profile,
1336                                        entrypoint, attrib_list,
1337                                        num_attribs, &wrapper_config));
1338
1339             if (vaStatus == VA_STATUS_SUCCESS)
1340                 obj_config->wrapper_config = wrapper_config;
1341         }
1342     }
1343
1344     /* Error recovery */
1345     if (VA_STATUS_SUCCESS != vaStatus) {
1346         i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
1347     } else {
1348         *config_id = configID;
1349     }
1350
1351     return vaStatus;
1352 }
1353
1354 VAStatus
1355 i965_DestroyConfig(VADriverContextP ctx, VAConfigID config_id)
1356 {
1357     struct i965_driver_data *i965 = i965_driver_data(ctx);
1358     struct object_config *obj_config = CONFIG(config_id);
1359     VAStatus vaStatus;
1360
1361     if (NULL == obj_config) {
1362         vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
1363         return vaStatus;
1364     }
1365
1366     if ((obj_config->wrapper_config != VA_INVALID_ID) &&
1367         i965->wrapper_pdrvctx) {
1368         CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
1369                     vaDestroyConfig(i965->wrapper_pdrvctx,
1370                                     obj_config->wrapper_config));
1371         obj_config->wrapper_config = VA_INVALID_ID;
1372     }
1373
1374     i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
1375     return VA_STATUS_SUCCESS;
1376 }
1377
1378 VAStatus i965_QueryConfigAttributes(VADriverContextP ctx,
1379                                     VAConfigID config_id,
1380                                     VAProfile *profile,                 /* out */
1381                                     VAEntrypoint *entrypoint,           /* out */
1382                                     VAConfigAttrib *attrib_list,        /* out */
1383                                     int *num_attribs)                   /* out */
1384 {
1385     struct i965_driver_data *i965 = i965_driver_data(ctx);
1386     struct object_config *obj_config = CONFIG(config_id);
1387     VAStatus vaStatus = VA_STATUS_SUCCESS;
1388     int i;
1389
1390     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
1391     *profile = obj_config->profile;
1392     *entrypoint = obj_config->entrypoint;
1393     *num_attribs = obj_config->num_attribs;
1394
1395     for (i = 0; i < obj_config->num_attribs; i++) {
1396         attrib_list[i] = obj_config->attrib_list[i];
1397     }
1398
1399     return vaStatus;
1400 }
1401
1402 void
1403 i965_destroy_surface_storage(struct object_surface *obj_surface)
1404 {
1405     if (!obj_surface)
1406         return;
1407
1408     dri_bo_unreference(obj_surface->bo);
1409     obj_surface->bo = NULL;
1410
1411     if (obj_surface->free_private_data != NULL) {
1412         obj_surface->free_private_data(&obj_surface->private_data);
1413         obj_surface->private_data = NULL;
1414     }
1415 }
1416
1417 static void
1418 i965_destroy_surface(struct object_heap *heap, struct object_base *obj)
1419 {
1420     struct object_surface *obj_surface = (struct object_surface *)obj;
1421
1422     i965_destroy_surface_storage(obj_surface);
1423     object_heap_free(heap, obj);
1424 }
1425
1426 /* byte-per-pixel of the first plane */
1427 static int
1428 bpp_1stplane_by_fourcc(unsigned int fourcc)
1429 {
1430     const i965_fourcc_info *info = get_fourcc_info(fourcc);
1431
1432     if (info && (info->flag & I_S))
1433         return info->bpp[0] / 8;
1434     else
1435         return 0;
1436 }
1437
1438 static VAStatus
1439 i965_surface_native_memory(VADriverContextP ctx,
1440                            struct object_surface *obj_surface,
1441                            int format,
1442                            int expected_fourcc)
1443 {
1444     struct i965_driver_data *i965 = i965_driver_data(ctx);
1445     int tiling = HAS_TILED_SURFACE(i965);
1446
1447     if (!expected_fourcc)
1448         return VA_STATUS_SUCCESS;
1449
1450     // todo, should we disable tiling for 422 format?
1451     if (expected_fourcc == VA_FOURCC_I420 ||
1452         expected_fourcc == VA_FOURCC_IYUV ||
1453         expected_fourcc == VA_FOURCC_I010 ||
1454         expected_fourcc == VA_FOURCC_YV12 ||
1455         expected_fourcc == VA_FOURCC_YV16)
1456         tiling = 0;
1457
1458     return i965_check_alloc_surface_bo(ctx, obj_surface, tiling, expected_fourcc, get_sampling_from_fourcc(expected_fourcc));
1459 }
1460
1461 static VAStatus
1462 i965_suface_external_memory(VADriverContextP ctx,
1463                             struct object_surface *obj_surface,
1464                             int external_memory_type,
1465                             VASurfaceAttribExternalBuffers *memory_attibute,
1466                             int index)
1467 {
1468     struct i965_driver_data *i965 = i965_driver_data(ctx);
1469     unsigned int tiling, swizzle;
1470
1471     if (!memory_attibute ||
1472         !memory_attibute->buffers ||
1473         index >= memory_attibute->num_buffers)
1474         return VA_STATUS_ERROR_INVALID_PARAMETER;
1475
1476     obj_surface->size = memory_attibute->data_size;
1477     if (external_memory_type == I965_SURFACE_MEM_GEM_FLINK)
1478         obj_surface->bo = drm_intel_bo_gem_create_from_name(i965->intel.bufmgr,
1479                                                             "gem flinked vaapi surface",
1480                                                             memory_attibute->buffers[index]);
1481     else if (external_memory_type == I965_SURFACE_MEM_DRM_PRIME)
1482         obj_surface->bo = drm_intel_bo_gem_create_from_prime(i965->intel.bufmgr,
1483                                                              memory_attibute->buffers[index],
1484                                                              obj_surface->size);
1485
1486     if (!obj_surface->bo)
1487         return VA_STATUS_ERROR_INVALID_PARAMETER;
1488
1489     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
1490
1491     ASSERT_RET(obj_surface->orig_width == memory_attibute->width, VA_STATUS_ERROR_INVALID_PARAMETER);
1492     ASSERT_RET(obj_surface->orig_height == memory_attibute->height, VA_STATUS_ERROR_INVALID_PARAMETER);
1493     ASSERT_RET(memory_attibute->num_planes >= 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1494
1495     obj_surface->fourcc = memory_attibute->pixel_format;
1496     obj_surface->width = memory_attibute->pitches[0];
1497     int bpp_1stplane = bpp_1stplane_by_fourcc(obj_surface->fourcc);
1498     ASSERT_RET(IS_ALIGNED(obj_surface->width, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
1499     ASSERT_RET(obj_surface->width >= obj_surface->orig_width * bpp_1stplane, VA_STATUS_ERROR_INVALID_PARAMETER);
1500
1501     if (memory_attibute->num_planes == 1)
1502         obj_surface->height = memory_attibute->data_size / obj_surface->width;
1503     else
1504         obj_surface->height = memory_attibute->offsets[1] / obj_surface->width;
1505     ASSERT_RET(IS_ALIGNED(obj_surface->height, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
1506     ASSERT_RET(obj_surface->height >= obj_surface->orig_height, VA_STATUS_ERROR_INVALID_PARAMETER);
1507
1508     if (tiling) {
1509         ASSERT_RET(IS_ALIGNED(obj_surface->width, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1510         ASSERT_RET(IS_ALIGNED(obj_surface->height, 32), VA_STATUS_ERROR_INVALID_PARAMETER);
1511     } else {
1512         ASSERT_RET(IS_ALIGNED(obj_surface->width, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1513         ASSERT_RET(IS_ALIGNED(obj_surface->height, i965->codec_info->min_linear_hpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1514     }
1515
1516     obj_surface->x_cb_offset = 0; /* X offset is always 0 */
1517     obj_surface->x_cr_offset = 0;
1518     if ((obj_surface->fourcc == VA_FOURCC_I420 ||
1519          obj_surface->fourcc == VA_FOURCC_IYUV ||
1520          obj_surface->fourcc == VA_FOURCC_I010 ||
1521          obj_surface->fourcc == VA_FOURCC_YV12 ||
1522          obj_surface->fourcc == VA_FOURCC_YV16) && tiling)
1523         return VA_STATUS_ERROR_INVALID_PARAMETER;
1524
1525     switch (obj_surface->fourcc) {
1526     case VA_FOURCC_NV12:
1527     case VA_FOURCC_P010:
1528         ASSERT_RET(memory_attibute->num_planes == 2, VA_STATUS_ERROR_INVALID_PARAMETER);
1529         ASSERT_RET(memory_attibute->pitches[0] == memory_attibute->pitches[1], VA_STATUS_ERROR_INVALID_PARAMETER);
1530
1531         obj_surface->subsampling = SUBSAMPLE_YUV420;
1532         obj_surface->y_cb_offset = obj_surface->height;
1533         obj_surface->y_cr_offset = obj_surface->height;
1534         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1535         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1536         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1537         if (tiling)
1538             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1539         else
1540             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1541
1542         break;
1543
1544     case VA_FOURCC_YV12:
1545     case VA_FOURCC_IMC1:
1546         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1547         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1548
1549         obj_surface->subsampling = SUBSAMPLE_YUV420;
1550         obj_surface->y_cr_offset = obj_surface->height;
1551         obj_surface->y_cb_offset = memory_attibute->offsets[2] / obj_surface->width;
1552         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1553         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1554         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1555
1556         if (tiling)
1557             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1558         else
1559             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1560
1561         break;
1562
1563     case VA_FOURCC_I420:
1564     case VA_FOURCC_IYUV:
1565     case VA_FOURCC_IMC3:
1566     case VA_FOURCC_I010:
1567         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1568         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1569
1570         obj_surface->subsampling = SUBSAMPLE_YUV420;
1571         obj_surface->y_cb_offset = obj_surface->height;
1572         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1573         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1574         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1575         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1576         if (tiling)
1577             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1578         else
1579             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1580
1581         break;
1582
1583     case VA_FOURCC_YUY2:
1584     case VA_FOURCC_UYVY:
1585         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1586
1587         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1588         obj_surface->y_cb_offset = 0;
1589         obj_surface->y_cr_offset = 0;
1590         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1591         obj_surface->cb_cr_height = obj_surface->orig_height;
1592         obj_surface->cb_cr_pitch = memory_attibute->pitches[0];
1593
1594         break;
1595
1596     case VA_FOURCC_RGBA:
1597     case VA_FOURCC_RGBX:
1598     case VA_FOURCC_BGRA:
1599     case VA_FOURCC_BGRX:
1600         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1601
1602         obj_surface->subsampling = SUBSAMPLE_RGBX;
1603         obj_surface->y_cb_offset = 0;
1604         obj_surface->y_cr_offset = 0;
1605         obj_surface->cb_cr_width = 0;
1606         obj_surface->cb_cr_height = 0;
1607         obj_surface->cb_cr_pitch = 0;
1608
1609         break;
1610
1611     case VA_FOURCC_Y800: /* monochrome surface */
1612         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1613
1614         obj_surface->subsampling = SUBSAMPLE_YUV400;
1615         obj_surface->y_cb_offset = 0;
1616         obj_surface->y_cr_offset = 0;
1617         obj_surface->cb_cr_width = 0;
1618         obj_surface->cb_cr_height = 0;
1619         obj_surface->cb_cr_pitch = 0;
1620
1621         break;
1622
1623     case VA_FOURCC_411P:
1624         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1625         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1626
1627         obj_surface->subsampling = SUBSAMPLE_YUV411;
1628         obj_surface->y_cb_offset = obj_surface->height;
1629         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1630         obj_surface->cb_cr_width = obj_surface->orig_width / 4;
1631         obj_surface->cb_cr_height = obj_surface->orig_height;
1632         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1633         if (tiling)
1634             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1635         else
1636             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1637         break;
1638
1639     case VA_FOURCC_422H:
1640         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1641         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1642
1643         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1644         obj_surface->y_cb_offset = obj_surface->height;
1645         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1646         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1647         obj_surface->cb_cr_height = obj_surface->orig_height;
1648         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1649         if (tiling)
1650             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1651         else
1652             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1653
1654         break;
1655
1656     case VA_FOURCC_YV16:
1657         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1658         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1659
1660         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1661         obj_surface->y_cr_offset = memory_attibute->offsets[1] / obj_surface->width;
1662         obj_surface->y_cb_offset = memory_attibute->offsets[2] / obj_surface->width;
1663         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1664         obj_surface->cb_cr_height = obj_surface->orig_height;
1665         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1666         ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1667
1668         break;
1669
1670     case VA_FOURCC_422V:
1671         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1672         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1673
1674         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1675         obj_surface->y_cb_offset = obj_surface->height;
1676         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1677         obj_surface->cb_cr_width = obj_surface->orig_width;
1678         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1679         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1680         if (tiling)
1681             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1682         else
1683             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1684
1685         break;
1686
1687     case VA_FOURCC_444P:
1688         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1689         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1690
1691         obj_surface->subsampling = SUBSAMPLE_YUV444;
1692         obj_surface->y_cb_offset = obj_surface->height;
1693         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1694         obj_surface->cb_cr_width = obj_surface->orig_width;
1695         obj_surface->cb_cr_height = obj_surface->orig_height;
1696         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1697         if (tiling)
1698             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1699         else
1700             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1701
1702         break;
1703
1704     default:
1705         return VA_STATUS_ERROR_INVALID_PARAMETER;
1706     }
1707
1708     return VA_STATUS_SUCCESS;
1709 }
1710
1711 static VAStatus
1712 i965_CreateSurfaces2(
1713     VADriverContextP    ctx,
1714     unsigned int        format,
1715     unsigned int        width,
1716     unsigned int        height,
1717     VASurfaceID        *surfaces,
1718     unsigned int        num_surfaces,
1719     VASurfaceAttrib    *attrib_list,
1720     unsigned int        num_attribs
1721 )
1722 {
1723     struct i965_driver_data *i965 = i965_driver_data(ctx);
1724     int i, j;
1725     VAStatus vaStatus = VA_STATUS_SUCCESS;
1726     int expected_fourcc = 0;
1727     int memory_type = I965_SURFACE_MEM_NATIVE; /* native */
1728     VASurfaceAttribExternalBuffers *memory_attibute = NULL;
1729
1730     for (i = 0; i < num_attribs && attrib_list; i++) {
1731         if ((attrib_list[i].type == VASurfaceAttribPixelFormat) &&
1732             (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
1733             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypeInteger, VA_STATUS_ERROR_INVALID_PARAMETER);
1734             expected_fourcc = attrib_list[i].value.value.i;
1735         }
1736
1737         if ((attrib_list[i].type == VASurfaceAttribMemoryType) &&
1738             (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
1739
1740             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypeInteger, VA_STATUS_ERROR_INVALID_PARAMETER);
1741
1742             if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM)
1743                 memory_type = I965_SURFACE_MEM_GEM_FLINK; /* flinked GEM handle */
1744             else if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME)
1745                 memory_type = I965_SURFACE_MEM_DRM_PRIME; /* drm prime fd */
1746             else if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_VA)
1747                 memory_type = I965_SURFACE_MEM_NATIVE; /* va native memory, to be allocated */
1748         }
1749
1750         if ((attrib_list[i].type == VASurfaceAttribExternalBufferDescriptor) &&
1751             (attrib_list[i].flags == VA_SURFACE_ATTRIB_SETTABLE)) {
1752             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypePointer, VA_STATUS_ERROR_INVALID_PARAMETER);
1753             memory_attibute = (VASurfaceAttribExternalBuffers *)attrib_list[i].value.value.p;
1754         }
1755     }
1756
1757     /* support 420 & 422 & RGB32 format, 422 and RGB32 are only used
1758      * for post-processing (including color conversion) */
1759     if (VA_RT_FORMAT_YUV420 != format &&
1760         VA_RT_FORMAT_YUV420_10BPP != format &&
1761         VA_RT_FORMAT_YUV422 != format &&
1762         VA_RT_FORMAT_YUV444 != format &&
1763         VA_RT_FORMAT_YUV411 != format &&
1764         VA_RT_FORMAT_YUV400 != format &&
1765         VA_RT_FORMAT_RGB32  != format) {
1766         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
1767     }
1768
1769     for (i = 0; i < num_surfaces; i++) {
1770         int surfaceID = NEW_SURFACE_ID();
1771         struct object_surface *obj_surface = SURFACE(surfaceID);
1772
1773         if (NULL == obj_surface) {
1774             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1775             break;
1776         }
1777
1778         surfaces[i] = surfaceID;
1779         obj_surface->status = VASurfaceReady;
1780         obj_surface->orig_width = width;
1781         obj_surface->orig_height = height;
1782         obj_surface->user_disable_tiling = false;
1783         obj_surface->user_h_stride_set = false;
1784         obj_surface->user_v_stride_set = false;
1785         obj_surface->border_cleared = false;
1786
1787         obj_surface->subpic_render_idx = 0;
1788         for (j = 0; j < I965_MAX_SUBPIC_SUM; j++) {
1789             obj_surface->subpic[j] = VA_INVALID_ID;
1790             obj_surface->obj_subpic[j] = NULL;
1791         }
1792
1793         assert(i965->codec_info->min_linear_wpitch);
1794         assert(i965->codec_info->min_linear_hpitch);
1795         obj_surface->width = ALIGN(width, i965->codec_info->min_linear_wpitch);
1796         obj_surface->height = ALIGN(height, i965->codec_info->min_linear_hpitch);
1797         obj_surface->flags = SURFACE_REFERENCED;
1798         obj_surface->fourcc = 0;
1799         obj_surface->expected_format = format;
1800         obj_surface->bo = NULL;
1801         obj_surface->locked_image_id = VA_INVALID_ID;
1802         obj_surface->derived_image_id = VA_INVALID_ID;
1803         obj_surface->private_data = NULL;
1804         obj_surface->free_private_data = NULL;
1805         obj_surface->subsampling = SUBSAMPLE_YUV420;
1806
1807         obj_surface->wrapper_surface = VA_INVALID_ID;
1808         obj_surface->exported_primefd = -1;
1809
1810         switch (memory_type) {
1811         case I965_SURFACE_MEM_NATIVE:
1812             if (memory_attibute) {
1813                 if (!(memory_attibute->flags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING))
1814                     obj_surface->user_disable_tiling = true;
1815
1816                 if (memory_attibute->pixel_format) {
1817                     if (expected_fourcc)
1818                         ASSERT_RET(memory_attibute->pixel_format == expected_fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
1819                     else
1820                         expected_fourcc = memory_attibute->pixel_format;
1821                 }
1822                 ASSERT_RET(expected_fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
1823                 if (memory_attibute->pitches[0]) {
1824                     int bpp_1stplane = bpp_1stplane_by_fourcc(expected_fourcc);
1825                     ASSERT_RET(bpp_1stplane, VA_STATUS_ERROR_INVALID_PARAMETER);
1826                     obj_surface->width = memory_attibute->pitches[0];
1827                     obj_surface->user_h_stride_set = true;
1828                     ASSERT_RET(IS_ALIGNED(obj_surface->width, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
1829                     ASSERT_RET(obj_surface->width >= width * bpp_1stplane, VA_STATUS_ERROR_INVALID_PARAMETER);
1830
1831                     if (memory_attibute->offsets[1]) {
1832                         ASSERT_RET(!memory_attibute->offsets[0], VA_STATUS_ERROR_INVALID_PARAMETER);
1833                         obj_surface->height = memory_attibute->offsets[1] / memory_attibute->pitches[0];
1834                         obj_surface->user_v_stride_set = true;
1835                         ASSERT_RET(IS_ALIGNED(obj_surface->height, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
1836                         ASSERT_RET(obj_surface->height >= height, VA_STATUS_ERROR_INVALID_PARAMETER);
1837                     }
1838                 }
1839             }
1840             vaStatus = i965_surface_native_memory(ctx,
1841                                                   obj_surface,
1842                                                   format,
1843                                                   expected_fourcc);
1844             break;
1845
1846         case I965_SURFACE_MEM_GEM_FLINK:
1847         case I965_SURFACE_MEM_DRM_PRIME:
1848             vaStatus = i965_suface_external_memory(ctx,
1849                                                    obj_surface,
1850                                                    memory_type,
1851                                                    memory_attibute,
1852                                                    i);
1853             break;
1854         }
1855         if (VA_STATUS_SUCCESS != vaStatus) {
1856             i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
1857             break;
1858         }
1859     }
1860
1861     /* Error recovery */
1862     if (VA_STATUS_SUCCESS != vaStatus) {
1863         /* surfaces[i-1] was the last successful allocation */
1864         for (; i--;) {
1865             struct object_surface *obj_surface = SURFACE(surfaces[i]);
1866
1867             surfaces[i] = VA_INVALID_SURFACE;
1868             assert(obj_surface);
1869             i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
1870         }
1871     }
1872
1873     return vaStatus;
1874 }
1875
1876 VAStatus
1877 i965_CreateSurfaces(VADriverContextP ctx,
1878                     int width,
1879                     int height,
1880                     int format,
1881                     int num_surfaces,
1882                     VASurfaceID *surfaces)      /* out */
1883 {
1884     return i965_CreateSurfaces2(ctx,
1885                                 format,
1886                                 width,
1887                                 height,
1888                                 surfaces,
1889                                 num_surfaces,
1890                                 NULL,
1891                                 0);
1892 }
1893
1894 VAStatus
1895 i965_DestroySurfaces(VADriverContextP ctx,
1896                      VASurfaceID *surface_list,
1897                      int num_surfaces)
1898 {
1899     struct i965_driver_data *i965 = i965_driver_data(ctx);
1900     int i;
1901     VAStatus va_status = VA_STATUS_SUCCESS;
1902
1903     for (i = num_surfaces; i--;) {
1904         struct object_surface *obj_surface = SURFACE(surface_list[i]);
1905
1906         ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
1907
1908         if ((obj_surface->wrapper_surface != VA_INVALID_ID) &&
1909             i965->wrapper_pdrvctx) {
1910             CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
1911                         vaDestroySurfaces(i965->wrapper_pdrvctx,
1912                                           &(obj_surface->wrapper_surface),
1913                                           1));
1914             obj_surface->wrapper_surface = VA_INVALID_ID;
1915         }
1916         if (obj_surface->exported_primefd >= 0) {
1917             close(obj_surface->exported_primefd);
1918             obj_surface->exported_primefd = -1;
1919         }
1920
1921         i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
1922     }
1923
1924     return va_status;
1925 }
1926
1927 VAStatus
1928 i965_QueryImageFormats(VADriverContextP ctx,
1929                        VAImageFormat *format_list,      /* out */
1930                        int *num_formats)                /* out */
1931 {
1932     int n;
1933
1934     for (n = 0; i965_image_formats_map[n].va_format.fourcc != 0; n++) {
1935         const i965_image_format_map_t * const m = &i965_image_formats_map[n];
1936         if (format_list)
1937             format_list[n] = m->va_format;
1938     }
1939
1940     if (num_formats)
1941         *num_formats = n;
1942
1943     return VA_STATUS_SUCCESS;
1944 }
1945
1946 /*
1947  * Guess the format when the usage of a VA surface is unknown
1948  * 1. Without a valid context: YV12
1949  * 2. The current context is valid:
1950  *    a) always NV12 on GEN6 and later
1951  *    b) I420 for MPEG-2 and NV12 for other codec on GEN4 & GEN5
1952  */
1953 static void
1954 i965_guess_surface_format(VADriverContextP ctx,
1955                           VASurfaceID surface,
1956                           unsigned int *fourcc,
1957                           unsigned int *is_tiled)
1958 {
1959     struct i965_driver_data *i965 = i965_driver_data(ctx);
1960     struct object_context *obj_context = NULL;
1961     struct object_config *obj_config = NULL;
1962
1963     *fourcc = VA_FOURCC_YV12;
1964     *is_tiled = 0;
1965
1966     if (i965->current_context_id == VA_INVALID_ID)
1967         return;
1968
1969     obj_context = CONTEXT(i965->current_context_id);
1970
1971     if (!obj_context)
1972         return;
1973
1974     obj_config = obj_context->obj_config;
1975     assert(obj_config);
1976
1977     if (!obj_config)
1978         return;
1979
1980     if (IS_GEN6(i965->intel.device_info) ||
1981         IS_GEN7(i965->intel.device_info) ||
1982         IS_GEN8(i965->intel.device_info) ||
1983         IS_GEN9(i965->intel.device_info)) {
1984         *fourcc = VA_FOURCC_NV12;
1985         *is_tiled = 1;
1986         return;
1987     }
1988
1989     switch (obj_config->profile) {
1990     case VAProfileMPEG2Simple:
1991     case VAProfileMPEG2Main:
1992         *fourcc = VA_FOURCC_I420;
1993         *is_tiled = 0;
1994         break;
1995
1996     default:
1997         *fourcc = VA_FOURCC_NV12;
1998         *is_tiled = 0;
1999         break;
2000     }
2001 }
2002
2003 VAStatus
2004 i965_QuerySubpictureFormats(VADriverContextP ctx,
2005                             VAImageFormat *format_list,         /* out */
2006                             unsigned int *flags,                /* out */
2007                             unsigned int *num_formats)          /* out */
2008 {
2009     int n;
2010
2011     for (n = 0; i965_subpic_formats_map[n].va_format.fourcc != 0; n++) {
2012         const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[n];
2013         if (format_list)
2014             format_list[n] = m->va_format;
2015         if (flags)
2016             flags[n] = m->va_flags;
2017     }
2018
2019     if (num_formats)
2020         *num_formats = n;
2021
2022     return VA_STATUS_SUCCESS;
2023 }
2024
2025 static void
2026 i965_destroy_subpic(struct object_heap *heap, struct object_base *obj)
2027 {
2028     //    struct object_subpic *obj_subpic = (struct object_subpic *)obj;
2029
2030     object_heap_free(heap, obj);
2031 }
2032
2033 VAStatus
2034 i965_CreateSubpicture(VADriverContextP ctx,
2035                       VAImageID image,
2036                       VASubpictureID *subpicture)         /* out */
2037 {
2038     struct i965_driver_data *i965 = i965_driver_data(ctx);
2039     VASubpictureID subpicID = NEW_SUBPIC_ID()
2040                               struct object_subpic * obj_subpic = SUBPIC(subpicID);
2041
2042     if (!obj_subpic)
2043         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2044
2045     struct object_image *obj_image = IMAGE(image);
2046     if (!obj_image)
2047         return VA_STATUS_ERROR_INVALID_IMAGE;
2048
2049     const i965_subpic_format_map_t * const m = get_subpic_format(&obj_image->image.format);
2050     if (!m)
2051         return VA_STATUS_ERROR_UNKNOWN; /* XXX: VA_STATUS_ERROR_UNSUPPORTED_FORMAT? */
2052
2053     *subpicture = subpicID;
2054     obj_subpic->image  = image;
2055     obj_subpic->obj_image = obj_image;
2056     obj_subpic->format = m->format;
2057     obj_subpic->width  = obj_image->image.width;
2058     obj_subpic->height = obj_image->image.height;
2059     obj_subpic->pitch  = obj_image->image.pitches[0];
2060     obj_subpic->bo     = obj_image->bo;
2061     obj_subpic->global_alpha = 1.0;
2062
2063     return VA_STATUS_SUCCESS;
2064 }
2065
2066 VAStatus
2067 i965_DestroySubpicture(VADriverContextP ctx,
2068                        VASubpictureID subpicture)
2069 {
2070     struct i965_driver_data *i965 = i965_driver_data(ctx);
2071     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2072
2073     if (!obj_subpic)
2074         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2075
2076     ASSERT_RET(obj_subpic->obj_image, VA_STATUS_ERROR_INVALID_SUBPICTURE);
2077     i965_destroy_subpic(&i965->subpic_heap, (struct object_base *)obj_subpic);
2078     return VA_STATUS_SUCCESS;
2079 }
2080
2081 VAStatus
2082 i965_SetSubpictureImage(VADriverContextP ctx,
2083                         VASubpictureID subpicture,
2084                         VAImageID image)
2085 {
2086     /* TODO */
2087     return VA_STATUS_ERROR_UNIMPLEMENTED;
2088 }
2089
2090 VAStatus
2091 i965_SetSubpictureChromakey(VADriverContextP ctx,
2092                             VASubpictureID subpicture,
2093                             unsigned int chromakey_min,
2094                             unsigned int chromakey_max,
2095                             unsigned int chromakey_mask)
2096 {
2097     /* TODO */
2098     return VA_STATUS_ERROR_UNIMPLEMENTED;
2099 }
2100
2101 VAStatus
2102 i965_SetSubpictureGlobalAlpha(VADriverContextP ctx,
2103                               VASubpictureID subpicture,
2104                               float global_alpha)
2105 {
2106     struct i965_driver_data *i965 = i965_driver_data(ctx);
2107     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2108
2109     if (global_alpha > 1.0 || global_alpha < 0.0) {
2110         return VA_STATUS_ERROR_INVALID_PARAMETER;
2111     }
2112
2113     if (!obj_subpic)
2114         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2115
2116     obj_subpic->global_alpha  = global_alpha;
2117
2118     return VA_STATUS_SUCCESS;
2119 }
2120
2121 VAStatus
2122 i965_AssociateSubpicture(VADriverContextP ctx,
2123                          VASubpictureID subpicture,
2124                          VASurfaceID *target_surfaces,
2125                          int num_surfaces,
2126                          short src_x, /* upper left offset in subpicture */
2127                          short src_y,
2128                          unsigned short src_width,
2129                          unsigned short src_height,
2130                          short dest_x, /* upper left offset in surface */
2131                          short dest_y,
2132                          unsigned short dest_width,
2133                          unsigned short dest_height,
2134                          /*
2135                           * whether to enable chroma-keying or global-alpha
2136                           * see VA_SUBPICTURE_XXX values
2137                           */
2138                          unsigned int flags)
2139 {
2140     struct i965_driver_data *i965 = i965_driver_data(ctx);
2141     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2142     int i, j;
2143
2144     if (!obj_subpic)
2145         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2146
2147     ASSERT_RET(obj_subpic->obj_image, VA_STATUS_ERROR_INVALID_SUBPICTURE);
2148
2149     obj_subpic->src_rect.x      = src_x;
2150     obj_subpic->src_rect.y      = src_y;
2151     obj_subpic->src_rect.width  = src_width;
2152     obj_subpic->src_rect.height = src_height;
2153     obj_subpic->dst_rect.x      = dest_x;
2154     obj_subpic->dst_rect.y      = dest_y;
2155     obj_subpic->dst_rect.width  = dest_width;
2156     obj_subpic->dst_rect.height = dest_height;
2157     obj_subpic->flags           = flags;
2158
2159     for (i = 0; i < num_surfaces; i++) {
2160         struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
2161         if (!obj_surface)
2162             return VA_STATUS_ERROR_INVALID_SURFACE;
2163
2164         for (j = 0; j < I965_MAX_SUBPIC_SUM; j ++) {
2165             if (obj_surface->subpic[j] == VA_INVALID_ID) {
2166                 assert(obj_surface->obj_subpic[j] == NULL);
2167                 obj_surface->subpic[j] = subpicture;
2168                 obj_surface->obj_subpic[j] = obj_subpic;
2169                 break;
2170             }
2171         }
2172
2173         if (j == I965_MAX_SUBPIC_SUM) {
2174             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2175         }
2176
2177     }
2178     return VA_STATUS_SUCCESS;
2179 }
2180
2181
2182 VAStatus
2183 i965_DeassociateSubpicture(VADriverContextP ctx,
2184                            VASubpictureID subpicture,
2185                            VASurfaceID *target_surfaces,
2186                            int num_surfaces)
2187 {
2188     struct i965_driver_data *i965 = i965_driver_data(ctx);
2189     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2190     int i, j;
2191
2192     if (!obj_subpic)
2193         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2194
2195     for (i = 0; i < num_surfaces; i++) {
2196         struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
2197         if (!obj_surface)
2198             return VA_STATUS_ERROR_INVALID_SURFACE;
2199
2200         for (j = 0; j < I965_MAX_SUBPIC_SUM; j ++) {
2201             if (obj_surface->subpic[j] == subpicture) {
2202                 assert(obj_surface->obj_subpic[j] == obj_subpic);
2203                 obj_surface->subpic[j] = VA_INVALID_ID;
2204                 obj_surface->obj_subpic[j] = NULL;
2205                 break;
2206             }
2207         }
2208
2209         if (j == I965_MAX_SUBPIC_SUM) {
2210             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2211         }
2212     }
2213     return VA_STATUS_SUCCESS;
2214 }
2215
2216 void
2217 i965_reference_buffer_store(struct buffer_store **ptr,
2218                             struct buffer_store *buffer_store)
2219 {
2220     assert(*ptr == NULL);
2221
2222     if (buffer_store) {
2223         buffer_store->ref_count++;
2224         *ptr = buffer_store;
2225     }
2226 }
2227
2228 void
2229 i965_release_buffer_store(struct buffer_store **ptr)
2230 {
2231     struct buffer_store *buffer_store = *ptr;
2232
2233     if (buffer_store == NULL)
2234         return;
2235
2236     assert(buffer_store->bo || buffer_store->buffer);
2237     assert(!(buffer_store->bo && buffer_store->buffer));
2238     buffer_store->ref_count--;
2239
2240     if (buffer_store->ref_count == 0) {
2241         dri_bo_unreference(buffer_store->bo);
2242         free(buffer_store->buffer);
2243         buffer_store->bo = NULL;
2244         buffer_store->buffer = NULL;
2245         free(buffer_store);
2246     }
2247
2248     *ptr = NULL;
2249 }
2250
2251 static void
2252 i965_destroy_context(struct object_heap *heap, struct object_base *obj)
2253 {
2254     struct object_context *obj_context = (struct object_context *)obj;
2255     int i, j;
2256
2257     if (obj_context->hw_context) {
2258         obj_context->hw_context->destroy(obj_context->hw_context);
2259         obj_context->hw_context = NULL;
2260     }
2261
2262     if (obj_context->codec_type == CODEC_PROC) {
2263         i965_release_buffer_store(&obj_context->codec_state.proc.pipeline_param);
2264
2265     } else if (obj_context->codec_type == CODEC_ENC) {
2266         i965_release_buffer_store(&obj_context->codec_state.encode.q_matrix);
2267         i965_release_buffer_store(&obj_context->codec_state.encode.huffman_table);
2268
2269         assert(obj_context->codec_state.encode.num_slice_params_ext <= obj_context->codec_state.encode.max_slice_params_ext);
2270         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param_ext);
2271         i965_release_buffer_store(&obj_context->codec_state.encode.seq_param_ext);
2272
2273         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_param); i++)
2274             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_param[i]);
2275
2276         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_data); i++)
2277             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data[i]);
2278
2279         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param); i++)
2280             for (j = 0; j < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param[0]); j++)
2281                 i965_release_buffer_store(&obj_context->codec_state.encode.misc_param[i][j]);
2282
2283         for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext; i++)
2284             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params_ext[i]);
2285
2286         free(obj_context->codec_state.encode.slice_params_ext);
2287         if (obj_context->codec_state.encode.slice_rawdata_index) {
2288             free(obj_context->codec_state.encode.slice_rawdata_index);
2289             obj_context->codec_state.encode.slice_rawdata_index = NULL;
2290         }
2291         if (obj_context->codec_state.encode.slice_rawdata_count) {
2292             free(obj_context->codec_state.encode.slice_rawdata_count);
2293             obj_context->codec_state.encode.slice_rawdata_count = NULL;
2294         }
2295
2296         if (obj_context->codec_state.encode.slice_header_index) {
2297             free(obj_context->codec_state.encode.slice_header_index);
2298             obj_context->codec_state.encode.slice_header_index = NULL;
2299         }
2300
2301         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_params_ext; i++)
2302             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_params_ext[i]);
2303         free(obj_context->codec_state.encode.packed_header_params_ext);
2304
2305         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_data_ext; i++)
2306             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data_ext[i]);
2307         free(obj_context->codec_state.encode.packed_header_data_ext);
2308
2309         i965_release_buffer_store(&obj_context->codec_state.encode.encmb_map);
2310     } else {
2311         assert(obj_context->codec_state.decode.num_slice_params <= obj_context->codec_state.decode.max_slice_params);
2312         assert(obj_context->codec_state.decode.num_slice_datas <= obj_context->codec_state.decode.max_slice_datas);
2313
2314         i965_release_buffer_store(&obj_context->codec_state.decode.pic_param);
2315         i965_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
2316         i965_release_buffer_store(&obj_context->codec_state.decode.huffman_table);
2317         i965_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
2318
2319         for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++)
2320             i965_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
2321
2322         for (i = 0; i < obj_context->codec_state.decode.num_slice_datas; i++)
2323             i965_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
2324
2325         free(obj_context->codec_state.decode.slice_params);
2326         free(obj_context->codec_state.decode.slice_datas);
2327     }
2328
2329     free(obj_context->render_targets);
2330     object_heap_free(heap, obj);
2331 }
2332
2333 static inline void
2334 max_resolution(struct i965_driver_data *i965,
2335                struct object_config *obj_config,
2336                int *w,                                  /* out */
2337                int *h)                                  /* out */
2338 {
2339     if (i965->codec_info->max_resolution) {
2340         i965->codec_info->max_resolution(i965, obj_config, w, h);
2341     } else {
2342         *w = i965->codec_info->max_width;
2343         *h = i965->codec_info->max_height;
2344     }
2345 }
2346
2347 VAStatus
2348 i965_CreateContext(VADriverContextP ctx,
2349                    VAConfigID config_id,
2350                    int picture_width,
2351                    int picture_height,
2352                    int flag,
2353                    VASurfaceID *render_targets,
2354                    int num_render_targets,
2355                    VAContextID *context)                /* out */
2356 {
2357     struct i965_driver_data *i965 = i965_driver_data(ctx);
2358     struct object_config *obj_config = CONFIG(config_id);
2359     struct object_context *obj_context = NULL;
2360     VAConfigAttrib *attrib;
2361     VAStatus vaStatus = VA_STATUS_SUCCESS;
2362     int contextID;
2363     int i;
2364     int max_width;
2365     int max_height;
2366
2367     if (NULL == obj_config) {
2368         vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
2369         return vaStatus;
2370     }
2371
2372     max_resolution(i965, obj_config, &max_width, &max_height);
2373
2374     if (picture_width > max_width ||
2375         picture_height > max_height) {
2376         vaStatus = VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
2377         return vaStatus;
2378     }
2379
2380     /* Validate flag */
2381     /* Validate picture dimensions */
2382     contextID = NEW_CONTEXT_ID();
2383     obj_context = CONTEXT(contextID);
2384
2385     if (NULL == obj_context) {
2386         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
2387         return vaStatus;
2388     }
2389
2390     *context = contextID;
2391     obj_context->flags = flag;
2392     obj_context->context_id = contextID;
2393     obj_context->obj_config = obj_config;
2394     obj_context->picture_width = picture_width;
2395     obj_context->picture_height = picture_height;
2396     obj_context->num_render_targets = num_render_targets;
2397     obj_context->render_targets =
2398         (VASurfaceID *)calloc(num_render_targets, sizeof(VASurfaceID));
2399     obj_context->hw_context = NULL;
2400     obj_context->wrapper_context = VA_INVALID_ID;
2401
2402     if (!obj_context->render_targets)
2403         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2404
2405     for (i = 0; i < num_render_targets; i++) {
2406         if (NULL == SURFACE(render_targets[i])) {
2407             vaStatus = VA_STATUS_ERROR_INVALID_SURFACE;
2408             break;
2409         }
2410
2411         obj_context->render_targets[i] = render_targets[i];
2412     }
2413
2414     if (VA_STATUS_SUCCESS == vaStatus) {
2415         if (VAEntrypointVideoProc == obj_config->entrypoint) {
2416             obj_context->codec_type = CODEC_PROC;
2417             memset(&obj_context->codec_state.proc, 0, sizeof(obj_context->codec_state.proc));
2418             obj_context->codec_state.proc.current_render_target = VA_INVALID_ID;
2419             assert(i965->codec_info->proc_hw_context_init);
2420             obj_context->hw_context = i965->codec_info->proc_hw_context_init(ctx, obj_config);
2421         } else if ((VAEntrypointEncSlice == obj_config->entrypoint) ||
2422                    (VAEntrypointEncPicture == obj_config->entrypoint) ||
2423                    (VAEntrypointEncSliceLP == obj_config->entrypoint)) {
2424             VAConfigAttrib *packed_attrib;
2425             obj_context->codec_type = CODEC_ENC;
2426             memset(&obj_context->codec_state.encode, 0, sizeof(obj_context->codec_state.encode));
2427             obj_context->codec_state.encode.current_render_target = VA_INVALID_ID;
2428             obj_context->codec_state.encode.max_packed_header_params_ext = NUM_SLICES;
2429             obj_context->codec_state.encode.packed_header_params_ext =
2430                 calloc(obj_context->codec_state.encode.max_packed_header_params_ext,
2431                        sizeof(struct buffer_store *));
2432
2433             obj_context->codec_state.encode.max_packed_header_data_ext = NUM_SLICES;
2434             obj_context->codec_state.encode.packed_header_data_ext =
2435                 calloc(obj_context->codec_state.encode.max_packed_header_data_ext,
2436                        sizeof(struct buffer_store *));
2437
2438             obj_context->codec_state.encode.max_slice_num = NUM_SLICES;
2439             obj_context->codec_state.encode.slice_rawdata_index =
2440                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2441             obj_context->codec_state.encode.slice_rawdata_count =
2442                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2443
2444             obj_context->codec_state.encode.slice_header_index =
2445                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2446
2447             obj_context->codec_state.encode.vps_sps_seq_index = 0;
2448
2449             obj_context->codec_state.encode.slice_index = 0;
2450             packed_attrib = i965_lookup_config_attribute(obj_config, VAConfigAttribEncPackedHeaders);
2451             if (packed_attrib) {
2452                 obj_context->codec_state.encode.packed_header_flag = packed_attrib->value;
2453                 if (obj_config->profile == VAProfileVP9Profile0)
2454                     obj_context->codec_state.encode.packed_header_flag =
2455                         packed_attrib->value & VA_ENC_PACKED_HEADER_RAW_DATA;
2456             } else {
2457                 /* use the default value. SPS/PPS/RAWDATA is passed from user
2458                  * while Slice_header data is generated by driver.
2459                  */
2460                 obj_context->codec_state.encode.packed_header_flag =
2461                     VA_ENC_PACKED_HEADER_SEQUENCE |
2462                     VA_ENC_PACKED_HEADER_PICTURE |
2463                     VA_ENC_PACKED_HEADER_RAW_DATA;
2464
2465                 /* it is not used for VP9 */
2466                 if (obj_config->profile == VAProfileVP9Profile0)
2467                     obj_context->codec_state.encode.packed_header_flag = 0;
2468             }
2469             assert(i965->codec_info->enc_hw_context_init);
2470             obj_context->hw_context = i965->codec_info->enc_hw_context_init(ctx, obj_config);
2471         } else {
2472             obj_context->codec_type = CODEC_DEC;
2473             memset(&obj_context->codec_state.decode, 0, sizeof(obj_context->codec_state.decode));
2474             obj_context->codec_state.decode.current_render_target = -1;
2475             obj_context->codec_state.decode.max_slice_params = NUM_SLICES;
2476             obj_context->codec_state.decode.max_slice_datas = NUM_SLICES;
2477             obj_context->codec_state.decode.slice_params = calloc(obj_context->codec_state.decode.max_slice_params,
2478                                                                   sizeof(*obj_context->codec_state.decode.slice_params));
2479             obj_context->codec_state.decode.slice_datas = calloc(obj_context->codec_state.decode.max_slice_datas,
2480                                                                  sizeof(*obj_context->codec_state.decode.slice_datas));
2481
2482             assert(i965->codec_info->dec_hw_context_init);
2483             obj_context->hw_context = i965->codec_info->dec_hw_context_init(ctx, obj_config);
2484         }
2485     }
2486
2487     attrib = i965_lookup_config_attribute(obj_config, VAConfigAttribRTFormat);
2488     if (!attrib)
2489         return VA_STATUS_ERROR_INVALID_CONFIG;
2490     obj_context->codec_state.base.chroma_formats = attrib->value;
2491
2492     if (obj_config->wrapper_config != VA_INVALID_ID) {
2493         /* The wrapper_pdrvctx should exist when wrapper_config is valid.
2494          * So it won't check i965->wrapper_pdrvctx again.
2495          * Fixme if it is incorrect.
2496          */
2497         VAGenericID wrapper_context;
2498
2499         /*
2500          * The render_surface is not passed when calling
2501          * vaCreateContext.
2502          * If it is needed, we must get the wrapped surface
2503          * for the corresponding Surface_list.
2504          * So the wrapped surface conversion is deferred.
2505          */
2506         CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
2507                     vaCreateContext(i965->wrapper_pdrvctx,
2508                                     obj_config->wrapper_config,
2509                                     picture_width, picture_height,
2510                                     flag, NULL, 0,
2511                                     &wrapper_context));
2512
2513         if (vaStatus == VA_STATUS_SUCCESS)
2514             obj_context->wrapper_context = wrapper_context;
2515     }
2516     /* Error recovery */
2517     if (VA_STATUS_SUCCESS != vaStatus) {
2518         i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
2519     }
2520
2521     i965->current_context_id = contextID;
2522
2523     return vaStatus;
2524 }
2525
2526 VAStatus
2527 i965_DestroyContext(VADriverContextP ctx, VAContextID context)
2528 {
2529     struct i965_driver_data *i965 = i965_driver_data(ctx);
2530     struct object_context *obj_context = CONTEXT(context);
2531     VAStatus va_status = VA_STATUS_SUCCESS;
2532
2533     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
2534
2535     if (i965->current_context_id == context)
2536         i965->current_context_id = VA_INVALID_ID;
2537
2538     if ((obj_context->wrapper_context != VA_INVALID_ID) &&
2539         i965->wrapper_pdrvctx) {
2540         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
2541                     vaDestroyContext(i965->wrapper_pdrvctx,
2542                                      obj_context->wrapper_context));
2543
2544         obj_context->wrapper_context = VA_INVALID_ID;
2545     }
2546
2547     i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
2548
2549     return va_status;
2550 }
2551
2552 static void
2553 i965_destroy_buffer(struct object_heap *heap, struct object_base *obj)
2554 {
2555     struct object_buffer *obj_buffer = (struct object_buffer *)obj;
2556
2557     assert(obj_buffer->buffer_store);
2558     i965_release_buffer_store(&obj_buffer->buffer_store);
2559     object_heap_free(heap, obj);
2560 }
2561
2562 static VAStatus
2563 i965_create_buffer_internal(VADriverContextP ctx,
2564                             VAContextID context,
2565                             VABufferType type,
2566                             unsigned int size,
2567                             unsigned int num_elements,
2568                             void *data,
2569                             dri_bo *store_bo,
2570                             VABufferID *buf_id)
2571 {
2572     struct i965_driver_data *i965 = i965_driver_data(ctx);
2573     struct object_buffer *obj_buffer = NULL;
2574     struct buffer_store *buffer_store = NULL;
2575     int bufferID;
2576     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
2577     struct object_context *obj_context = CONTEXT(context);
2578     int wrapper_flag = 0;
2579
2580     /* Validate type */
2581     switch (type) {
2582     case VAPictureParameterBufferType:
2583     case VAIQMatrixBufferType:
2584     case VAQMatrixBufferType:
2585     case VABitPlaneBufferType:
2586     case VASliceGroupMapBufferType:
2587     case VASliceParameterBufferType:
2588     case VASliceDataBufferType:
2589     case VAMacroblockParameterBufferType:
2590     case VAResidualDataBufferType:
2591     case VADeblockingParameterBufferType:
2592     case VAImageBufferType:
2593     case VAEncCodedBufferType:
2594     case VAEncSequenceParameterBufferType:
2595     case VAEncPictureParameterBufferType:
2596     case VAEncSliceParameterBufferType:
2597     case VAEncPackedHeaderParameterBufferType:
2598     case VAEncPackedHeaderDataBufferType:
2599     case VAEncMiscParameterBufferType:
2600     case VAProcPipelineParameterBufferType:
2601     case VAProcFilterParameterBufferType:
2602     case VAHuffmanTableBufferType:
2603     case VAProbabilityBufferType:
2604     case VAEncMacroblockMapBufferType:
2605         /* Ok */
2606         break;
2607
2608     default:
2609         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
2610     }
2611
2612     bufferID = NEW_BUFFER_ID();
2613     obj_buffer = BUFFER(bufferID);
2614
2615     if (NULL == obj_buffer) {
2616         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2617     }
2618
2619     if (type == VAEncCodedBufferType) {
2620         size += I965_CODEDBUFFER_HEADER_SIZE;
2621         size += 0x1000; /* for upper bound check */
2622     }
2623
2624     obj_buffer->max_num_elements = num_elements;
2625     obj_buffer->num_elements = num_elements;
2626     obj_buffer->size_element = size;
2627     obj_buffer->type = type;
2628     obj_buffer->export_refcount = 0;
2629     obj_buffer->buffer_store = NULL;
2630     obj_buffer->wrapper_buffer = VA_INVALID_ID;
2631     obj_buffer->context_id = context;
2632
2633     buffer_store = calloc(1, sizeof(struct buffer_store));
2634     assert(buffer_store);
2635     buffer_store->ref_count = 1;
2636
2637     if (obj_context &&
2638         (obj_context->wrapper_context != VA_INVALID_ID) &&
2639         i965->wrapper_pdrvctx) {
2640         VAGenericID wrapper_buffer;
2641         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2642
2643         CALL_VTABLE(pdrvctx, vaStatus,
2644                     vaCreateBuffer(pdrvctx, obj_context->wrapper_context, type, size, num_elements,
2645                                    data, &wrapper_buffer));
2646         if (vaStatus == VA_STATUS_SUCCESS) {
2647             obj_buffer->wrapper_buffer = wrapper_buffer;
2648         } else {
2649             free(buffer_store);
2650             return vaStatus;
2651         }
2652         wrapper_flag = 1;
2653     }
2654
2655     if (store_bo != NULL) {
2656         buffer_store->bo = store_bo;
2657         dri_bo_reference(buffer_store->bo);
2658
2659         /* If the buffer is wrapped, the buffer_store is bogus. Unnecessary to copy it */
2660         if (data && !wrapper_flag)
2661             dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
2662     } else if (type == VASliceDataBufferType ||
2663                type == VAImageBufferType ||
2664                type == VAEncCodedBufferType ||
2665                type == VAEncMacroblockMapBufferType ||
2666                type == VAProbabilityBufferType) {
2667
2668         /* If the buffer is wrapped, the bo/buffer of buffer_store is bogus.
2669          * So it is enough to allocate one 64 byte bo
2670          */
2671         if (wrapper_flag)
2672             buffer_store->bo = dri_bo_alloc(i965->intel.bufmgr, "Bogus buffer",
2673                                             64, 64);
2674         else
2675             buffer_store->bo = dri_bo_alloc(i965->intel.bufmgr,
2676                                             "Buffer",
2677                                             size * num_elements, 64);
2678         assert(buffer_store->bo);
2679
2680         /* If the buffer is wrapped, the bo/buffer of buffer_store is bogus.
2681          * In fact it can be skipped. But it is still allocated and it is
2682          * only to follow the normal flowchart of buffer_allocation/release.
2683          */
2684         if (!wrapper_flag) {
2685             if (type == VAEncCodedBufferType) {
2686                 struct i965_coded_buffer_segment *coded_buffer_segment;
2687
2688                 dri_bo_map(buffer_store->bo, 1);
2689                 coded_buffer_segment = (struct i965_coded_buffer_segment *)buffer_store->bo->virtual;
2690                 coded_buffer_segment->base.size = size - I965_CODEDBUFFER_HEADER_SIZE;
2691                 coded_buffer_segment->base.bit_offset = 0;
2692                 coded_buffer_segment->base.status = 0;
2693                 coded_buffer_segment->base.buf = NULL;
2694                 coded_buffer_segment->base.next = NULL;
2695                 coded_buffer_segment->mapped = 0;
2696                 coded_buffer_segment->codec = 0;
2697                 coded_buffer_segment->status_support = 0;
2698                 dri_bo_unmap(buffer_store->bo);
2699             } else if (data) {
2700                 dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
2701             }
2702         }
2703
2704     } else {
2705         int msize = size;
2706
2707         if (type == VAEncPackedHeaderDataBufferType) {
2708             msize = ALIGN(size, 4);
2709         }
2710
2711         /* If the buffer is wrapped, it is enough to allocate 4 bytes */
2712         if (wrapper_flag)
2713             buffer_store->buffer = malloc(4);
2714         else
2715             buffer_store->buffer = malloc(msize * num_elements);
2716         assert(buffer_store->buffer);
2717
2718         if (!wrapper_flag) {
2719             if (data)
2720                 memcpy(buffer_store->buffer, data, size * num_elements);
2721             else
2722                 memset(buffer_store->buffer, 0, size * num_elements);
2723         }
2724     }
2725
2726     buffer_store->num_elements = obj_buffer->num_elements;
2727     i965_reference_buffer_store(&obj_buffer->buffer_store, buffer_store);
2728     i965_release_buffer_store(&buffer_store);
2729     *buf_id = bufferID;
2730
2731     return VA_STATUS_SUCCESS;
2732 }
2733
2734 VAStatus
2735 i965_CreateBuffer(VADriverContextP ctx,
2736                   VAContextID context,          /* in */
2737                   VABufferType type,            /* in */
2738                   unsigned int size,            /* in */
2739                   unsigned int num_elements,    /* in */
2740                   void *data,                   /* in */
2741                   VABufferID *buf_id)           /* out */
2742 {
2743     return i965_create_buffer_internal(ctx, context, type, size, num_elements, data, NULL, buf_id);
2744 }
2745
2746
2747 VAStatus
2748 i965_BufferSetNumElements(VADriverContextP ctx,
2749                           VABufferID buf_id,           /* in */
2750                           unsigned int num_elements)   /* in */
2751 {
2752     struct i965_driver_data *i965 = i965_driver_data(ctx);
2753     struct object_buffer *obj_buffer = BUFFER(buf_id);
2754     VAStatus vaStatus = VA_STATUS_SUCCESS;
2755
2756     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
2757
2758     /* When the wrapper_buffer exists, it will wrapper to the
2759      * buffer allocated from backend driver.
2760      */
2761     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2762         i965->wrapper_pdrvctx) {
2763         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2764
2765         CALL_VTABLE(pdrvctx, vaStatus,
2766                     vaBufferSetNumElements(pdrvctx, obj_buffer->wrapper_buffer,
2767                                            num_elements));
2768         return vaStatus;
2769     }
2770
2771     if ((num_elements < 0) ||
2772         (num_elements > obj_buffer->max_num_elements)) {
2773         vaStatus = VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2774     } else {
2775         obj_buffer->num_elements = num_elements;
2776         if (obj_buffer->buffer_store != NULL) {
2777             obj_buffer->buffer_store->num_elements = num_elements;
2778         }
2779     }
2780
2781     return vaStatus;
2782 }
2783
2784 VAStatus
2785 i965_MapBuffer(VADriverContextP ctx,
2786                VABufferID buf_id,       /* in */
2787                void **pbuf)             /* out */
2788 {
2789     struct i965_driver_data *i965 = i965_driver_data(ctx);
2790     struct object_buffer *obj_buffer = BUFFER(buf_id);
2791     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
2792     struct object_context *obj_context;
2793
2794     ASSERT_RET(obj_buffer && obj_buffer->buffer_store, VA_STATUS_ERROR_INVALID_BUFFER);
2795
2796     obj_context = CONTEXT(obj_buffer->context_id);
2797
2798     /* When the wrapper_buffer exists, it will wrapper to the
2799      * buffer allocated from backend driver.
2800      */
2801     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2802         i965->wrapper_pdrvctx) {
2803         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2804
2805         CALL_VTABLE(pdrvctx, vaStatus,
2806                     vaMapBuffer(pdrvctx, obj_buffer->wrapper_buffer, pbuf));
2807         return vaStatus;
2808     }
2809
2810     ASSERT_RET(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
2811     ASSERT_RET(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer), VA_STATUS_ERROR_INVALID_BUFFER);
2812
2813     if (obj_buffer->export_refcount > 0)
2814         return VA_STATUS_ERROR_INVALID_BUFFER;
2815
2816     if (NULL != obj_buffer->buffer_store->bo) {
2817         unsigned int tiling, swizzle;
2818
2819         dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);
2820
2821         if (tiling != I915_TILING_NONE)
2822             drm_intel_gem_bo_map_gtt(obj_buffer->buffer_store->bo);
2823         else
2824             dri_bo_map(obj_buffer->buffer_store->bo, 1);
2825
2826         ASSERT_RET(obj_buffer->buffer_store->bo->virtual, VA_STATUS_ERROR_OPERATION_FAILED);
2827         *pbuf = obj_buffer->buffer_store->bo->virtual;
2828         vaStatus = VA_STATUS_SUCCESS;
2829
2830         if (obj_buffer->type == VAEncCodedBufferType) {
2831             int i;
2832             unsigned char *buffer = NULL;
2833             unsigned int  header_offset = I965_CODEDBUFFER_HEADER_SIZE;
2834             struct i965_coded_buffer_segment *coded_buffer_segment = (struct i965_coded_buffer_segment *)(obj_buffer->buffer_store->bo->virtual);
2835
2836             if (!coded_buffer_segment->mapped) {
2837                 unsigned char delimiter0, delimiter1, delimiter2, delimiter3, delimiter4;
2838
2839                 coded_buffer_segment->base.buf = buffer = (unsigned char *)(obj_buffer->buffer_store->bo->virtual) + I965_CODEDBUFFER_HEADER_SIZE;
2840
2841                 if (obj_context &&
2842                     obj_context->hw_context &&
2843                     obj_context->hw_context->get_status &&
2844                     coded_buffer_segment->status_support) {
2845                     vaStatus = obj_context->hw_context->get_status(ctx, obj_context->hw_context, coded_buffer_segment);
2846                 } else {
2847                     if (coded_buffer_segment->codec == CODEC_H264 ||
2848                         coded_buffer_segment->codec == CODEC_H264_MVC) {
2849                         delimiter0 = H264_DELIMITER0;
2850                         delimiter1 = H264_DELIMITER1;
2851                         delimiter2 = H264_DELIMITER2;
2852                         delimiter3 = H264_DELIMITER3;
2853                         delimiter4 = H264_DELIMITER4;
2854                     } else if (coded_buffer_segment->codec == CODEC_MPEG2) {
2855                         delimiter0 = MPEG2_DELIMITER0;
2856                         delimiter1 = MPEG2_DELIMITER1;
2857                         delimiter2 = MPEG2_DELIMITER2;
2858                         delimiter3 = MPEG2_DELIMITER3;
2859                         delimiter4 = MPEG2_DELIMITER4;
2860                     } else if (coded_buffer_segment->codec == CODEC_JPEG) {
2861                         //In JPEG End of Image (EOI = 0xDDF9) marker can be used for delimiter.
2862                         delimiter0 = 0xFF;
2863                         delimiter1 = 0xD9;
2864                     } else if (coded_buffer_segment->codec == CODEC_HEVC) {
2865                         delimiter0 = HEVC_DELIMITER0;
2866                         delimiter1 = HEVC_DELIMITER1;
2867                         delimiter2 = HEVC_DELIMITER2;
2868                         delimiter3 = HEVC_DELIMITER3;
2869                         delimiter4 = HEVC_DELIMITER4;
2870                     } else if (coded_buffer_segment->codec != CODEC_VP8) {
2871                         ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_PROFILE);
2872                     }
2873
2874                     if (coded_buffer_segment->codec == CODEC_JPEG) {
2875                         for (i = 0; i <  obj_buffer->size_element - header_offset - 1 - 0x1000; i++) {
2876                             if ((buffer[i] == 0xFF) && (buffer[i + 1] == 0xD9)) {
2877                                 break;
2878                             }
2879                         }
2880                         coded_buffer_segment->base.size = i + 2;
2881                     } else if (coded_buffer_segment->codec != CODEC_VP8) {
2882                         /* vp8 coded buffer size can be told by vp8 internal statistics buffer,
2883                            so it don't need to traversal the coded buffer */
2884                         for (i = 0; i < obj_buffer->size_element - header_offset - 3 - 0x1000; i++) {
2885                             if ((buffer[i] == delimiter0) &&
2886                                 (buffer[i + 1] == delimiter1) &&
2887                                 (buffer[i + 2] == delimiter2) &&
2888                                 (buffer[i + 3] == delimiter3) &&
2889                                 (buffer[i + 4] == delimiter4))
2890                                 break;
2891                         }
2892
2893                         if (i == obj_buffer->size_element - header_offset - 3 - 0x1000) {
2894                             coded_buffer_segment->base.status |= VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK;
2895                         }
2896                         coded_buffer_segment->base.size = i;
2897                     }
2898
2899                     if (coded_buffer_segment->base.size >= obj_buffer->size_element - header_offset - 0x1000) {
2900                         coded_buffer_segment->base.status |= VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK;
2901                     }
2902
2903                     vaStatus = VA_STATUS_SUCCESS;
2904                 }
2905
2906                 coded_buffer_segment->mapped = 1;
2907             } else {
2908                 assert(coded_buffer_segment->base.buf);
2909                 vaStatus = VA_STATUS_SUCCESS;
2910             }
2911         }
2912     } else if (NULL != obj_buffer->buffer_store->buffer) {
2913         *pbuf = obj_buffer->buffer_store->buffer;
2914         vaStatus = VA_STATUS_SUCCESS;
2915     }
2916
2917     return vaStatus;
2918 }
2919
2920 VAStatus
2921 i965_UnmapBuffer(VADriverContextP ctx, VABufferID buf_id)
2922 {
2923     struct i965_driver_data *i965 = i965_driver_data(ctx);
2924     struct object_buffer *obj_buffer = BUFFER(buf_id);
2925     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
2926
2927     if ((buf_id & OBJECT_HEAP_OFFSET_MASK) != BUFFER_ID_OFFSET)
2928         return VA_STATUS_ERROR_INVALID_BUFFER;
2929
2930     ASSERT_RET(obj_buffer && obj_buffer->buffer_store, VA_STATUS_ERROR_INVALID_BUFFER);
2931     /* When the wrapper_buffer exists, it will wrapper to the
2932      * buffer allocated from backend driver.
2933      */
2934     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2935         i965->wrapper_pdrvctx) {
2936         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2937
2938         CALL_VTABLE(pdrvctx, vaStatus,
2939                     vaUnmapBuffer(pdrvctx, obj_buffer->wrapper_buffer));
2940         return vaStatus;
2941     }
2942
2943     ASSERT_RET(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_OPERATION_FAILED);
2944     ASSERT_RET(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer), VA_STATUS_ERROR_OPERATION_FAILED);
2945
2946     if (NULL != obj_buffer->buffer_store->bo) {
2947         unsigned int tiling, swizzle;
2948
2949         dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);
2950
2951         if (tiling != I915_TILING_NONE)
2952             drm_intel_gem_bo_unmap_gtt(obj_buffer->buffer_store->bo);
2953         else
2954             dri_bo_unmap(obj_buffer->buffer_store->bo);
2955
2956         vaStatus = VA_STATUS_SUCCESS;
2957     } else if (NULL != obj_buffer->buffer_store->buffer) {
2958         /* Do nothing */
2959         vaStatus = VA_STATUS_SUCCESS;
2960     }
2961
2962     return vaStatus;
2963 }
2964
2965 VAStatus
2966 i965_DestroyBuffer(VADriverContextP ctx, VABufferID buffer_id)
2967 {
2968     struct i965_driver_data *i965 = i965_driver_data(ctx);
2969     struct object_buffer *obj_buffer = BUFFER(buffer_id);
2970     VAStatus va_status = VA_STATUS_SUCCESS;
2971
2972     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
2973
2974     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2975         i965->wrapper_pdrvctx) {
2976         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
2977                     vaDestroyBuffer(i965->wrapper_pdrvctx,
2978                                     obj_buffer->wrapper_buffer));
2979         obj_buffer->wrapper_buffer = VA_INVALID_ID;
2980     }
2981
2982     i965_destroy_buffer(&i965->buffer_heap, (struct object_base *)obj_buffer);
2983
2984     return va_status;
2985 }
2986
2987 VAStatus
2988 i965_BeginPicture(VADriverContextP ctx,
2989                   VAContextID context,
2990                   VASurfaceID render_target)
2991 {
2992     struct i965_driver_data *i965 = i965_driver_data(ctx);
2993     struct object_context *obj_context = CONTEXT(context);
2994     struct object_surface *obj_surface = SURFACE(render_target);
2995     struct object_config *obj_config;
2996     VAStatus vaStatus = VA_STATUS_SUCCESS;
2997     int i, j;
2998
2999     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3000     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
3001     obj_config = obj_context->obj_config;
3002     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3003
3004     if (is_surface_busy(i965, obj_surface))
3005         return VA_STATUS_ERROR_SURFACE_BUSY;
3006
3007     if (obj_context->codec_type == CODEC_PROC) {
3008         obj_context->codec_state.proc.current_render_target = render_target;
3009     } else if (obj_context->codec_type == CODEC_ENC) {
3010         /* ext */
3011         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param_ext);
3012
3013         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_param); i++)
3014             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_param[i]);
3015
3016         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_data); i++)
3017             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data[i]);
3018
3019         for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext; i++)
3020             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params_ext[i]);
3021
3022         obj_context->codec_state.encode.num_slice_params_ext = 0;
3023         obj_context->codec_state.encode.current_render_target = render_target;     /*This is input new frame*/
3024         obj_context->codec_state.encode.last_packed_header_type = 0;
3025         memset(obj_context->codec_state.encode.slice_rawdata_index, 0,
3026                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
3027         memset(obj_context->codec_state.encode.slice_rawdata_count, 0,
3028                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
3029         memset(obj_context->codec_state.encode.slice_header_index, 0,
3030                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
3031
3032         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_params_ext; i++)
3033             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_params_ext[i]);
3034         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_data_ext; i++)
3035             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data_ext[i]);
3036         obj_context->codec_state.encode.num_packed_header_params_ext = 0;
3037         obj_context->codec_state.encode.num_packed_header_data_ext = 0;
3038         obj_context->codec_state.encode.slice_index = 0;
3039         obj_context->codec_state.encode.vps_sps_seq_index = 0;
3040
3041         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param); i++)
3042             for (j = 0; j < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param[0]); j++)
3043                 i965_release_buffer_store(&obj_context->codec_state.encode.misc_param[i][j]);
3044
3045         i965_release_buffer_store(&obj_context->codec_state.encode.encmb_map);
3046     } else {
3047         obj_context->codec_state.decode.current_render_target = render_target;
3048         i965_release_buffer_store(&obj_context->codec_state.decode.pic_param);
3049         i965_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
3050         i965_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
3051         i965_release_buffer_store(&obj_context->codec_state.decode.huffman_table);
3052
3053         for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++) {
3054             i965_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
3055             i965_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
3056         }
3057
3058         obj_context->codec_state.decode.num_slice_params = 0;
3059         obj_context->codec_state.decode.num_slice_datas = 0;
3060
3061         if ((obj_context->wrapper_context != VA_INVALID_ID) &&
3062             i965->wrapper_pdrvctx) {
3063             if (obj_surface->wrapper_surface == VA_INVALID_ID)
3064                 vaStatus = i965_surface_wrapper(ctx, render_target);
3065
3066             if (vaStatus != VA_STATUS_SUCCESS)
3067                 return vaStatus;
3068
3069             CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
3070                         vaBeginPicture(i965->wrapper_pdrvctx,
3071                                        obj_context->wrapper_context,
3072                                        obj_surface->wrapper_surface));
3073         }
3074     }
3075
3076     return vaStatus;
3077 }
3078
3079 #define I965_RENDER_BUFFER(category, name) i965_render_##category##_##name##_buffer(ctx, obj_context, obj_buffer)
3080
3081 #define DEF_RENDER_SINGLE_BUFFER_FUNC(category, name, member)           \
3082     static VAStatus                                                     \
3083     i965_render_##category##_##name##_buffer(VADriverContextP ctx,      \
3084                                              struct object_context *obj_context, \
3085                                              struct object_buffer *obj_buffer) \
3086     {                                                                   \
3087         struct category##_state *category = &obj_context->codec_state.category; \
3088         i965_release_buffer_store(&category->member);                   \
3089         i965_reference_buffer_store(&category->member, obj_buffer->buffer_store); \
3090         return VA_STATUS_SUCCESS;                                       \
3091     }
3092
3093 #define DEF_RENDER_MULTI_BUFFER_FUNC(category, name, member)            \
3094     static VAStatus                                                     \
3095     i965_render_##category##_##name##_buffer(VADriverContextP ctx,      \
3096                                              struct object_context *obj_context, \
3097                                              struct object_buffer *obj_buffer) \
3098     {                                                                   \
3099         struct category##_state *category = &obj_context->codec_state.category; \
3100         if (category->num_##member == category->max_##member) {         \
3101             category->member = realloc(category->member, (category->max_##member + NUM_SLICES) * sizeof(*category->member)); \
3102             memset(category->member + category->max_##member, 0, NUM_SLICES * sizeof(*category->member)); \
3103             category->max_##member += NUM_SLICES;                       \
3104         }                                                               \
3105         i965_release_buffer_store(&category->member[category->num_##member]); \
3106         i965_reference_buffer_store(&category->member[category->num_##member], obj_buffer->buffer_store); \
3107         category->num_##member++;                                       \
3108         return VA_STATUS_SUCCESS;                                       \
3109     }
3110
3111 #define I965_RENDER_DECODE_BUFFER(name) I965_RENDER_BUFFER(decode, name)
3112
3113 #define DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(decode, name, member)
3114 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(picture_parameter, pic_param)
3115 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(iq_matrix, iq_matrix)
3116 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(bit_plane, bit_plane)
3117 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(huffman_table, huffman_table)
3118 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(probability_data, probability_data)
3119
3120 #define DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(name, member) DEF_RENDER_MULTI_BUFFER_FUNC(decode, name, member)
3121 DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(slice_parameter, slice_params)
3122 DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(slice_data, slice_datas)
3123
3124
3125 static VAStatus
3126 i965_decoder_vp9_wrapper_picture(VADriverContextP ctx,
3127                                  VABufferID *buffers,
3128                                  int num_buffers)
3129 {
3130     struct i965_driver_data *i965 = i965_driver_data(ctx);
3131     VAStatus vaStatus = VA_STATUS_SUCCESS;
3132     int i;
3133     VADecPictureParameterBufferVP9 *pVp9PicParams;
3134     VADriverContextP pdrvctx;
3135     struct object_buffer *obj_buffer;
3136
3137     pdrvctx = i965->wrapper_pdrvctx;
3138     /* do the conversion of VADecPictureParameterBufferVP9 */
3139     for (i = 0; i < num_buffers; i++) {
3140         obj_buffer = BUFFER(buffers[i]);
3141
3142         if (!obj_buffer)
3143             continue;
3144
3145         if (obj_buffer->wrapper_buffer == VA_INVALID_ID)
3146             continue;
3147
3148         if (obj_buffer->type == VAPictureParameterBufferType) {
3149             int j;
3150             VASurfaceID surface_id;
3151             struct object_surface *obj_surface;
3152
3153             pdrvctx = i965->wrapper_pdrvctx;
3154
3155             CALL_VTABLE(pdrvctx, vaStatus,
3156                         vaMapBuffer(pdrvctx, obj_buffer->wrapper_buffer,
3157                                     (void **)(&pVp9PicParams)));
3158
3159             if (vaStatus != VA_STATUS_SUCCESS)
3160                 return vaStatus;
3161
3162             for (j = 0; j < 8; j++) {
3163                 surface_id = pVp9PicParams->reference_frames[j];
3164                 obj_surface = SURFACE(surface_id);
3165
3166                 if (!obj_surface)
3167                     continue;
3168
3169                 if (obj_surface->wrapper_surface == VA_INVALID_ID) {
3170                     vaStatus = i965_surface_wrapper(ctx, surface_id);
3171                     if (vaStatus != VA_STATUS_SUCCESS) {
3172                         pdrvctx->vtable->vaUnmapBuffer(pdrvctx,
3173                                                        obj_buffer->wrapper_buffer);
3174                         goto fail_out;
3175                     }
3176                 }
3177
3178                 pVp9PicParams->reference_frames[j] = obj_surface->wrapper_surface;
3179             }
3180             CALL_VTABLE(pdrvctx, vaStatus,
3181                         vaUnmapBuffer(pdrvctx, obj_buffer->wrapper_buffer));
3182             break;
3183         }
3184     }
3185
3186     return VA_STATUS_SUCCESS;
3187
3188 fail_out:
3189     return vaStatus;
3190 }
3191
3192 static VAStatus
3193 i965_decoder_wrapper_picture(VADriverContextP ctx,
3194                              VAContextID context,
3195                              VABufferID *buffers,
3196                              int num_buffers)
3197 {
3198     struct i965_driver_data *i965 = i965_driver_data(ctx);
3199     struct object_context *obj_context = CONTEXT(context);
3200     VAStatus vaStatus = VA_STATUS_SUCCESS;
3201     int i;
3202     VADriverContextP pdrvctx;
3203     struct object_buffer *obj_buffer;
3204
3205     if (obj_context == NULL)
3206         return VA_STATUS_ERROR_INVALID_CONTEXT;
3207
3208     /* When it is not wrapped context, continue the normal flowchart */
3209     if (obj_context->wrapper_context == VA_INVALID_ID)
3210         return vaStatus;
3211
3212     if (obj_context->obj_config &&
3213         (obj_context->obj_config->profile == VAProfileVP9Profile0)) {
3214         vaStatus = i965_decoder_vp9_wrapper_picture(ctx, buffers, num_buffers);
3215     } else
3216         return VA_STATUS_ERROR_INVALID_PARAMETER;
3217
3218     pdrvctx = i965->wrapper_pdrvctx;
3219
3220     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3221         obj_buffer = BUFFER(buffers[i]);
3222
3223         if (!obj_buffer)
3224             continue;
3225
3226         if (obj_buffer->wrapper_buffer == VA_INVALID_ID) {
3227             vaStatus = VA_STATUS_ERROR_INVALID_BUFFER;
3228             break;
3229         }
3230
3231         CALL_VTABLE(pdrvctx, vaStatus,
3232                     vaRenderPicture(pdrvctx, obj_context->wrapper_context,
3233                                     &(obj_buffer->wrapper_buffer), 1));
3234     }
3235     return vaStatus;
3236 }
3237
3238 static VAStatus
3239 i965_decoder_render_picture(VADriverContextP ctx,
3240                             VAContextID context,
3241                             VABufferID *buffers,
3242                             int num_buffers)
3243 {
3244     struct i965_driver_data *i965 = i965_driver_data(ctx);
3245     struct object_context *obj_context = CONTEXT(context);
3246     VAStatus vaStatus = VA_STATUS_SUCCESS;
3247     int i;
3248
3249     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3250
3251     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3252         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3253
3254         if (!obj_buffer)
3255             return VA_STATUS_ERROR_INVALID_BUFFER;
3256
3257         switch (obj_buffer->type) {
3258         case VAPictureParameterBufferType:
3259             vaStatus = I965_RENDER_DECODE_BUFFER(picture_parameter);
3260             break;
3261
3262         case VAIQMatrixBufferType:
3263             vaStatus = I965_RENDER_DECODE_BUFFER(iq_matrix);
3264             break;
3265
3266         case VABitPlaneBufferType:
3267             vaStatus = I965_RENDER_DECODE_BUFFER(bit_plane);
3268             break;
3269
3270         case VASliceParameterBufferType:
3271             vaStatus = I965_RENDER_DECODE_BUFFER(slice_parameter);
3272             break;
3273
3274         case VASliceDataBufferType:
3275             vaStatus = I965_RENDER_DECODE_BUFFER(slice_data);
3276             break;
3277
3278         case VAHuffmanTableBufferType:
3279             vaStatus = I965_RENDER_DECODE_BUFFER(huffman_table);
3280             break;
3281
3282         case VAProbabilityBufferType:
3283             vaStatus = I965_RENDER_DECODE_BUFFER(probability_data);
3284             break;
3285
3286         default:
3287             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3288             break;
3289         }
3290     }
3291
3292     if ((vaStatus == VA_STATUS_SUCCESS) &&
3293         (obj_context->wrapper_context != VA_INVALID_ID))
3294         vaStatus = i965_decoder_wrapper_picture(ctx, context, buffers, num_buffers);
3295
3296     return vaStatus;
3297 }
3298
3299 #define I965_RENDER_ENCODE_BUFFER(name) I965_RENDER_BUFFER(encode, name)
3300
3301 #define DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(encode, name, member)
3302 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(sequence_parameter, seq_param)
3303 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_parameter, pic_param)
3304 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_control, pic_control)
3305 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(qmatrix, q_matrix)
3306 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(iqmatrix, iq_matrix)
3307 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(huffman_table, huffman_table)
3308 /* extended buffer */
3309 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(sequence_parameter_ext, seq_param_ext)
3310 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_parameter_ext, pic_param_ext)
3311 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(encmb_map, encmb_map)
3312
3313 #define DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(name, member) DEF_RENDER_MULTI_BUFFER_FUNC(encode, name, member)
3314 // DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(slice_parameter, slice_params)
3315 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(slice_parameter_ext, slice_params_ext)
3316
3317 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(packed_header_params_ext, packed_header_params_ext)
3318 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(packed_header_data_ext, packed_header_data_ext)
3319
3320 static VAStatus
3321 i965_encoder_render_packed_header_parameter_buffer(VADriverContextP ctx,
3322                                                    struct object_context *obj_context,
3323                                                    struct object_buffer *obj_buffer,
3324                                                    int type_index)
3325 {
3326     struct encode_state *encode = &obj_context->codec_state.encode;
3327
3328     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3329     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3330     i965_release_buffer_store(&encode->packed_header_param[type_index]);
3331     i965_reference_buffer_store(&encode->packed_header_param[type_index], obj_buffer->buffer_store);
3332
3333     return VA_STATUS_SUCCESS;
3334 }
3335
3336 static VAStatus
3337 i965_encoder_render_packed_header_data_buffer(VADriverContextP ctx,
3338                                               struct object_context *obj_context,
3339                                               struct object_buffer *obj_buffer,
3340                                               int type_index)
3341 {
3342     struct encode_state *encode = &obj_context->codec_state.encode;
3343
3344     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3345     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3346     i965_release_buffer_store(&encode->packed_header_data[type_index]);
3347     i965_reference_buffer_store(&encode->packed_header_data[type_index], obj_buffer->buffer_store);
3348
3349     return VA_STATUS_SUCCESS;
3350 }
3351
3352 static int
3353 i965_encoder_get_misc_paramerter_buffer_index(VADriverContextP ctx,
3354                                               struct encode_state *encode,
3355                                               VAEncMiscParameterBuffer *misc_param)
3356 {
3357     int index = 0;
3358
3359     if (!encode->has_layers)
3360         return 0;
3361
3362     if (misc_param->type == VAEncMiscParameterTypeRateControl) {
3363         VAEncMiscParameterRateControl *misc_rate_control = (VAEncMiscParameterRateControl *)misc_param->data;
3364
3365         index = misc_rate_control->rc_flags.bits.temporal_id;
3366     } else if (misc_param->type == VAEncMiscParameterTypeFrameRate) {
3367         VAEncMiscParameterFrameRate *misc_frame_rate = (VAEncMiscParameterFrameRate *)misc_param->data;
3368
3369         index = misc_frame_rate->framerate_flags.bits.temporal_id;
3370     }
3371
3372     return index;
3373 }
3374
3375 static VAStatus
3376 i965_encoder_render_misc_parameter_buffer(VADriverContextP ctx,
3377                                           struct object_context *obj_context,
3378                                           struct object_buffer *obj_buffer)
3379 {
3380     struct encode_state *encode = &obj_context->codec_state.encode;
3381     VAEncMiscParameterBuffer *param = NULL;
3382     int index;
3383
3384     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3385     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3386
3387     param = (VAEncMiscParameterBuffer *)obj_buffer->buffer_store->buffer;
3388
3389     if (param->type >= ARRAY_ELEMS(encode->misc_param))
3390         return VA_STATUS_ERROR_INVALID_PARAMETER;
3391
3392     if (param->type == VAEncMiscParameterTypeTemporalLayerStructure)
3393         encode->has_layers = 1;
3394
3395     index = i965_encoder_get_misc_paramerter_buffer_index(ctx, encode, param);
3396
3397     if (index >= ARRAY_ELEMS(encode->misc_param[0]))
3398         return VA_STATUS_ERROR_INVALID_PARAMETER;
3399
3400     i965_release_buffer_store(&encode->misc_param[param->type][index]);
3401     i965_reference_buffer_store(&encode->misc_param[param->type][index], obj_buffer->buffer_store);
3402
3403     return VA_STATUS_SUCCESS;
3404 }
3405
3406 static VAStatus
3407 i965_encoder_render_picture(VADriverContextP ctx,
3408                             VAContextID context,
3409                             VABufferID *buffers,
3410                             int num_buffers)
3411 {
3412     struct i965_driver_data *i965 = i965_driver_data(ctx);
3413     struct object_context *obj_context = CONTEXT(context);
3414     struct object_config *obj_config;
3415     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3416     struct encode_state *encode;
3417     int i;
3418
3419     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3420     obj_config = obj_context->obj_config;
3421     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3422
3423     encode = &obj_context->codec_state.encode;
3424     for (i = 0; i < num_buffers; i++) {
3425         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3426
3427         if (!obj_buffer)
3428             return VA_STATUS_ERROR_INVALID_BUFFER;
3429
3430         switch (obj_buffer->type) {
3431         case VAQMatrixBufferType:
3432             vaStatus = I965_RENDER_ENCODE_BUFFER(qmatrix);
3433             break;
3434
3435         case VAIQMatrixBufferType:
3436             vaStatus = I965_RENDER_ENCODE_BUFFER(iqmatrix);
3437             break;
3438
3439         case VAEncSequenceParameterBufferType:
3440             vaStatus = I965_RENDER_ENCODE_BUFFER(sequence_parameter_ext);
3441             break;
3442
3443         case VAEncPictureParameterBufferType:
3444             vaStatus = I965_RENDER_ENCODE_BUFFER(picture_parameter_ext);
3445             break;
3446
3447         case VAHuffmanTableBufferType:
3448             vaStatus = I965_RENDER_ENCODE_BUFFER(huffman_table);
3449             break;
3450
3451         case VAEncSliceParameterBufferType:
3452             vaStatus = I965_RENDER_ENCODE_BUFFER(slice_parameter_ext);
3453             if (vaStatus == VA_STATUS_SUCCESS) {
3454                 /* When the max number of slices is updated, it also needs
3455                  * to reallocate the arrays that is used to store
3456                  * the packed data index/count for the slice
3457                  */
3458                 if (!(encode->packed_header_flag & VA_ENC_PACKED_HEADER_SLICE)) {
3459                     encode->slice_index++;
3460                 }
3461                 if (encode->slice_index == encode->max_slice_num) {
3462                     int slice_num = encode->max_slice_num;
3463                     encode->slice_rawdata_index = realloc(encode->slice_rawdata_index,
3464                                                           (slice_num + NUM_SLICES) * sizeof(int));
3465                     encode->slice_rawdata_count = realloc(encode->slice_rawdata_count,
3466                                                           (slice_num + NUM_SLICES) * sizeof(int));
3467                     encode->slice_header_index = realloc(encode->slice_header_index,
3468                                                          (slice_num + NUM_SLICES) * sizeof(int));
3469                     memset(encode->slice_rawdata_index + slice_num, 0,
3470                            sizeof(int) * NUM_SLICES);
3471                     memset(encode->slice_rawdata_count + slice_num, 0,
3472                            sizeof(int) * NUM_SLICES);
3473                     memset(encode->slice_header_index + slice_num, 0,
3474                            sizeof(int) * NUM_SLICES);
3475
3476                     encode->max_slice_num += NUM_SLICES;
3477                     if ((encode->slice_rawdata_index == NULL) ||
3478                         (encode->slice_header_index == NULL)  ||
3479                         (encode->slice_rawdata_count == NULL)) {
3480                         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
3481                         return vaStatus;
3482                     }
3483                 }
3484             }
3485             break;
3486
3487         case VAEncPackedHeaderParameterBufferType: {
3488             VAEncPackedHeaderParameterBuffer *param = (VAEncPackedHeaderParameterBuffer *)obj_buffer->buffer_store->buffer;
3489             encode->last_packed_header_type = param->type;
3490
3491             if ((param->type == VAEncPackedHeaderRawData) ||
3492                 (param->type == VAEncPackedHeaderSlice)) {
3493                 vaStatus = I965_RENDER_ENCODE_BUFFER(packed_header_params_ext);
3494             } else if ((obj_config->profile == VAProfileHEVCMain ||
3495                         obj_config->profile == VAProfileHEVCMain10) &&
3496                        (encode->last_packed_header_type == VAEncPackedHeaderSequence)) {
3497                 vaStatus = i965_encoder_render_packed_header_parameter_buffer(ctx,
3498                                                                               obj_context,
3499                                                                               obj_buffer,
3500                                                                               va_enc_packed_type_to_idx(encode->last_packed_header_type) + encode->vps_sps_seq_index);
3501             } else {
3502                 vaStatus = i965_encoder_render_packed_header_parameter_buffer(ctx,
3503                                                                               obj_context,
3504                                                                               obj_buffer,
3505                                                                               va_enc_packed_type_to_idx(encode->last_packed_header_type));
3506             }
3507             break;
3508         }
3509
3510         case VAEncPackedHeaderDataBufferType: {
3511             if (encode->last_packed_header_type == 0) {
3512                 WARN_ONCE("the packed header data is passed without type!\n");
3513                 vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
3514                 return vaStatus;
3515             }
3516
3517             if (encode->last_packed_header_type == VAEncPackedHeaderRawData ||
3518                 encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3519                 vaStatus = I965_RENDER_ENCODE_BUFFER(packed_header_data_ext);
3520
3521                 if (obj_config->profile == VAProfileVP9Profile0)
3522                     break;
3523
3524                 /* When the PACKED_SLICE_HEADER flag is passed, it will use
3525                  * the packed_slice_header as the delimeter to decide how
3526                  * the packed rawdata is inserted for the given slice.
3527                  * Otherwise it will use the VAEncSequenceParameterBuffer
3528                  * as the delimeter
3529                  */
3530                 if (encode->packed_header_flag & VA_ENC_PACKED_HEADER_SLICE) {
3531                     /* store the first index of the packed header data for current slice */
3532                     if (encode->slice_rawdata_index[encode->slice_index] == 0) {
3533                         encode->slice_rawdata_index[encode->slice_index] =
3534                             SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3535                     }
3536                     encode->slice_rawdata_count[encode->slice_index]++;
3537                     if (encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3538                         /* find one packed slice_header delimeter. And the following
3539                          * packed data is for the next slice
3540                          */
3541                         encode->slice_header_index[encode->slice_index] =
3542                             SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3543                         encode->slice_index++;
3544                         /* Reallocate the buffer to record the index/count of
3545                          * packed_data for one slice.
3546                          */
3547                         if (encode->slice_index == encode->max_slice_num) {
3548                             int slice_num = encode->max_slice_num;
3549
3550                             encode->slice_rawdata_index = realloc(encode->slice_rawdata_index,
3551                                                                   (slice_num + NUM_SLICES) * sizeof(int));
3552                             encode->slice_rawdata_count = realloc(encode->slice_rawdata_count,
3553                                                                   (slice_num + NUM_SLICES) * sizeof(int));
3554                             encode->slice_header_index = realloc(encode->slice_header_index,
3555                                                                  (slice_num + NUM_SLICES) * sizeof(int));
3556                             memset(encode->slice_rawdata_index + slice_num, 0,
3557                                    sizeof(int) * NUM_SLICES);
3558                             memset(encode->slice_rawdata_count + slice_num, 0,
3559                                    sizeof(int) * NUM_SLICES);
3560                             memset(encode->slice_header_index + slice_num, 0,
3561                                    sizeof(int) * NUM_SLICES);
3562                             encode->max_slice_num += NUM_SLICES;
3563                         }
3564                     }
3565                 } else {
3566                     if (vaStatus == VA_STATUS_SUCCESS) {
3567                         /* store the first index of the packed header data for current slice */
3568                         if (encode->slice_rawdata_index[encode->slice_index] == 0) {
3569                             encode->slice_rawdata_index[encode->slice_index] =
3570                                 SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3571                         }
3572                         encode->slice_rawdata_count[encode->slice_index]++;
3573                         if (encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3574                             if (encode->slice_header_index[encode->slice_index] == 0) {
3575                                 encode->slice_header_index[encode->slice_index] =
3576                                     SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3577                             } else {
3578                                 WARN_ONCE("Multi slice header data is passed for"
3579                                           " slice %d!\n", encode->slice_index);
3580                             }
3581                         }
3582                     }
3583                 }
3584             } else {
3585                 ASSERT_RET(encode->last_packed_header_type == VAEncPackedHeaderSequence ||
3586                            encode->last_packed_header_type == VAEncPackedHeaderPicture ||
3587                            encode->last_packed_header_type == VAEncPackedHeaderSlice ||
3588                            (((encode->last_packed_header_type & VAEncPackedHeaderMiscMask) == VAEncPackedHeaderMiscMask) &&
3589                             ((encode->last_packed_header_type & (~VAEncPackedHeaderMiscMask)) != 0)),
3590                            VA_STATUS_ERROR_ENCODING_ERROR);
3591
3592                 if ((obj_config->profile == VAProfileHEVCMain ||
3593                      obj_config->profile == VAProfileHEVCMain10) &&
3594                     (encode->last_packed_header_type == VAEncPackedHeaderSequence)) {
3595
3596                     vaStatus = i965_encoder_render_packed_header_data_buffer(ctx,
3597                                                                              obj_context,
3598                                                                              obj_buffer,
3599                                                                              va_enc_packed_type_to_idx(encode->last_packed_header_type) + encode->vps_sps_seq_index);
3600                     encode->vps_sps_seq_index = (encode->vps_sps_seq_index + 1) % I965_SEQ_PACKED_HEADER_END;
3601                 } else {
3602                     vaStatus = i965_encoder_render_packed_header_data_buffer(ctx,
3603                                                                              obj_context,
3604                                                                              obj_buffer,
3605                                                                              va_enc_packed_type_to_idx(encode->last_packed_header_type));
3606
3607                 }
3608             }
3609             encode->last_packed_header_type = 0;
3610             break;
3611         }
3612
3613         case VAEncMiscParameterBufferType:
3614             vaStatus = i965_encoder_render_misc_parameter_buffer(ctx,
3615                                                                  obj_context,
3616                                                                  obj_buffer);
3617             break;
3618
3619         case VAEncMacroblockMapBufferType:
3620             vaStatus = I965_RENDER_ENCODE_BUFFER(encmb_map);
3621             break;
3622
3623         default:
3624             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3625             break;
3626         }
3627     }
3628
3629     return vaStatus;
3630 }
3631
3632 #define I965_RENDER_PROC_BUFFER(name) I965_RENDER_BUFFER(proc, name)
3633
3634 #define DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(proc, name, member)
3635 DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(pipeline_parameter, pipeline_param)
3636
3637 static VAStatus
3638 i965_proc_render_picture(VADriverContextP ctx,
3639                          VAContextID context,
3640                          VABufferID *buffers,
3641                          int num_buffers)
3642 {
3643     struct i965_driver_data *i965 = i965_driver_data(ctx);
3644     struct object_context *obj_context = CONTEXT(context);
3645     VAStatus vaStatus = VA_STATUS_SUCCESS;
3646     int i;
3647
3648     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3649
3650     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3651         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3652
3653         if (!obj_buffer)
3654             return VA_STATUS_ERROR_INVALID_BUFFER;
3655
3656         switch (obj_buffer->type) {
3657         case VAProcPipelineParameterBufferType:
3658             vaStatus = I965_RENDER_PROC_BUFFER(pipeline_parameter);
3659             break;
3660
3661         default:
3662             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3663             break;
3664         }
3665     }
3666
3667     return vaStatus;
3668 }
3669
3670 VAStatus
3671 i965_RenderPicture(VADriverContextP ctx,
3672                    VAContextID context,
3673                    VABufferID *buffers,
3674                    int num_buffers)
3675 {
3676     struct i965_driver_data *i965 = i965_driver_data(ctx);
3677     struct object_context *obj_context;
3678     struct object_config *obj_config;
3679     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3680
3681     obj_context = CONTEXT(context);
3682     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3683
3684     if (num_buffers <= 0)
3685         return VA_STATUS_ERROR_INVALID_PARAMETER;
3686
3687     obj_config = obj_context->obj_config;
3688     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3689
3690     if (VAEntrypointVideoProc == obj_config->entrypoint) {
3691         vaStatus = i965_proc_render_picture(ctx, context, buffers, num_buffers);
3692     } else if ((VAEntrypointEncSlice == obj_config->entrypoint) ||
3693                (VAEntrypointEncPicture == obj_config->entrypoint) ||
3694                (VAEntrypointEncSliceLP == obj_config->entrypoint)) {
3695         vaStatus = i965_encoder_render_picture(ctx, context, buffers, num_buffers);
3696     } else {
3697         vaStatus = i965_decoder_render_picture(ctx, context, buffers, num_buffers);
3698     }
3699
3700     return vaStatus;
3701 }
3702
3703 VAStatus
3704 i965_EndPicture(VADriverContextP ctx, VAContextID context)
3705 {
3706     struct i965_driver_data *i965 = i965_driver_data(ctx);
3707     struct object_context *obj_context = CONTEXT(context);
3708     struct object_config *obj_config;
3709
3710     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3711     obj_config = obj_context->obj_config;
3712     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3713
3714     if (obj_context->codec_type == CODEC_PROC) {
3715         ASSERT_RET(VAEntrypointVideoProc == obj_config->entrypoint, VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT);
3716     } else if (obj_context->codec_type == CODEC_ENC) {
3717         ASSERT_RET(((VAEntrypointEncSlice == obj_config->entrypoint) ||
3718                     (VAEntrypointEncPicture == obj_config->entrypoint) ||
3719                     (VAEntrypointEncSliceLP == obj_config->entrypoint)),
3720                    VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT);
3721
3722         if (obj_context->codec_state.encode.num_packed_header_params_ext !=
3723             obj_context->codec_state.encode.num_packed_header_data_ext) {
3724             WARN_ONCE("the packed header/data is not paired for encoding!\n");
3725             return VA_STATUS_ERROR_INVALID_PARAMETER;
3726         }
3727         if (!obj_context->codec_state.encode.pic_param_ext) {
3728             return VA_STATUS_ERROR_INVALID_PARAMETER;
3729         }
3730         if (!obj_context->codec_state.encode.seq_param_ext &&
3731             (VAEntrypointEncPicture != obj_config->entrypoint)) {
3732             /* The seq_param is not mandatory for VP9 encoding */
3733             if (obj_config->profile != VAProfileVP9Profile0)
3734                 return VA_STATUS_ERROR_INVALID_PARAMETER;
3735         }
3736         if ((obj_context->codec_state.encode.num_slice_params_ext <= 0) &&
3737             ((obj_config->profile != VAProfileVP8Version0_3) &&
3738              (obj_config->profile != VAProfileVP9Profile0))) {
3739             return VA_STATUS_ERROR_INVALID_PARAMETER;
3740         }
3741
3742         if ((obj_context->codec_state.encode.packed_header_flag & VA_ENC_PACKED_HEADER_SLICE) &&
3743             (obj_context->codec_state.encode.num_slice_params_ext !=
3744              obj_context->codec_state.encode.slice_index)) {
3745             WARN_ONCE("packed slice_header data is missing for some slice"
3746                       " under packed SLICE_HEADER mode\n");
3747             return VA_STATUS_ERROR_INVALID_PARAMETER;
3748         }
3749     } else {
3750         if (obj_context->codec_state.decode.pic_param == NULL) {
3751             return VA_STATUS_ERROR_INVALID_PARAMETER;
3752         }
3753         if (obj_context->codec_state.decode.num_slice_params <= 0) {
3754             return VA_STATUS_ERROR_INVALID_PARAMETER;
3755         }
3756         if (obj_context->codec_state.decode.num_slice_datas <= 0) {
3757             return VA_STATUS_ERROR_INVALID_PARAMETER;
3758         }
3759
3760         if (obj_context->codec_state.decode.num_slice_params !=
3761             obj_context->codec_state.decode.num_slice_datas) {
3762             return VA_STATUS_ERROR_INVALID_PARAMETER;
3763         }
3764
3765         if (obj_context->wrapper_context != VA_INVALID_ID) {
3766             /* call the vaEndPicture of wrapped driver */
3767             VADriverContextP pdrvctx;
3768             VAStatus va_status;
3769
3770             pdrvctx = i965->wrapper_pdrvctx;
3771             CALL_VTABLE(pdrvctx, va_status,
3772                         vaEndPicture(pdrvctx, obj_context->wrapper_context));
3773
3774             return va_status;
3775         }
3776     }
3777
3778     ASSERT_RET(obj_context->hw_context->run, VA_STATUS_ERROR_OPERATION_FAILED);
3779     return obj_context->hw_context->run(ctx, obj_config->profile, &obj_context->codec_state, obj_context->hw_context);
3780 }
3781
3782 VAStatus
3783 i965_SyncSurface(VADriverContextP ctx,
3784                  VASurfaceID render_target)
3785 {
3786     struct i965_driver_data *i965 = i965_driver_data(ctx);
3787     struct object_surface *obj_surface = SURFACE(render_target);
3788
3789     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
3790
3791     if (obj_surface->bo)
3792         drm_intel_bo_wait_rendering(obj_surface->bo);
3793
3794     return VA_STATUS_SUCCESS;
3795 }
3796
3797 VAStatus
3798 i965_QuerySurfaceStatus(VADriverContextP ctx,
3799                         VASurfaceID render_target,
3800                         VASurfaceStatus *status)        /* out */
3801 {
3802     struct i965_driver_data *i965 = i965_driver_data(ctx);
3803     struct object_surface *obj_surface = SURFACE(render_target);
3804
3805     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
3806
3807     if (obj_surface->bo) {
3808         if (drm_intel_bo_busy(obj_surface->bo)) {
3809             *status = VASurfaceRendering;
3810         } else {
3811             *status = VASurfaceReady;
3812         }
3813     } else {
3814         *status = VASurfaceReady;
3815     }
3816
3817     return VA_STATUS_SUCCESS;
3818 }
3819
3820 static VADisplayAttribute *
3821 get_display_attribute(VADriverContextP ctx, VADisplayAttribType type)
3822 {
3823     struct i965_driver_data * const i965 = i965_driver_data(ctx);
3824     unsigned int i;
3825
3826     if (!i965->display_attributes)
3827         return NULL;
3828
3829     for (i = 0; i < i965->num_display_attributes; i++) {
3830         if (i965->display_attributes[i].type == type)
3831             return &i965->display_attributes[i];
3832     }
3833     return NULL;
3834 }
3835
3836 static void
3837 i965_display_attributes_terminate(VADriverContextP ctx)
3838 {
3839     struct i965_driver_data * const i965 = i965_driver_data(ctx);
3840
3841     if (i965->display_attributes) {
3842         free(i965->display_attributes);
3843         i965->display_attributes = NULL;
3844         i965->num_display_attributes = 0;
3845     }
3846 }
3847
3848 static bool
3849 i965_display_attributes_init(VADriverContextP ctx)
3850 {
3851     struct i965_driver_data * const i965 = i965_driver_data(ctx);
3852
3853     i965->num_display_attributes = ARRAY_ELEMS(i965_display_attributes);
3854     i965->display_attributes = malloc(
3855                                    i965->num_display_attributes * sizeof(i965->display_attributes[0]));
3856     if (!i965->display_attributes)
3857         goto error;
3858
3859     memcpy(
3860         i965->display_attributes,
3861         i965_display_attributes,
3862         sizeof(i965_display_attributes)
3863     );
3864
3865     i965->rotation_attrib = get_display_attribute(ctx, VADisplayAttribRotation);
3866     i965->brightness_attrib = get_display_attribute(ctx, VADisplayAttribBrightness);
3867     i965->contrast_attrib = get_display_attribute(ctx, VADisplayAttribContrast);
3868     i965->hue_attrib = get_display_attribute(ctx, VADisplayAttribHue);
3869     i965->saturation_attrib = get_display_attribute(ctx, VADisplayAttribSaturation);
3870
3871     if (!i965->rotation_attrib ||
3872         !i965->brightness_attrib ||
3873         !i965->contrast_attrib ||
3874         !i965->hue_attrib ||
3875         !i965->saturation_attrib) {
3876         goto error;
3877     }
3878     return true;
3879
3880 error:
3881     i965_display_attributes_terminate(ctx);
3882     return false;
3883 }
3884
3885 /*
3886  * Query display attributes
3887  * The caller must provide a "attr_list" array that can hold at
3888  * least vaMaxNumDisplayAttributes() entries. The actual number of attributes
3889  * returned in "attr_list" is returned in "num_attributes".
3890  */
3891 VAStatus
3892 i965_QueryDisplayAttributes(
3893     VADriverContextP    ctx,
3894     VADisplayAttribute *attribs,        /* out */
3895     int                *num_attribs_ptr /* out */
3896 )
3897 {
3898     const int num_attribs = ARRAY_ELEMS(i965_display_attributes);
3899
3900     if (attribs && num_attribs > 0)
3901         memcpy(attribs, i965_display_attributes, sizeof(i965_display_attributes));
3902
3903     if (num_attribs_ptr)
3904         *num_attribs_ptr = num_attribs;
3905
3906     return VA_STATUS_SUCCESS;
3907 }
3908
3909 /*
3910  * Get display attributes
3911  * This function returns the current attribute values in "attr_list".
3912  * Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
3913  * from vaQueryDisplayAttributes() can have their values retrieved.
3914  */
3915 VAStatus
3916 i965_GetDisplayAttributes(
3917     VADriverContextP    ctx,
3918     VADisplayAttribute *attribs,        /* inout */
3919     int                 num_attribs     /* in */
3920 )
3921 {
3922     int i;
3923
3924     for (i = 0; i < num_attribs; i++) {
3925         VADisplayAttribute *src_attrib, * const dst_attrib = &attribs[i];
3926
3927         src_attrib = get_display_attribute(ctx, dst_attrib->type);
3928
3929         if (src_attrib && (src_attrib->flags & VA_DISPLAY_ATTRIB_GETTABLE)) {
3930             dst_attrib->min_value = src_attrib->min_value;
3931             dst_attrib->max_value = src_attrib->max_value;
3932             dst_attrib->value     = src_attrib->value;
3933             dst_attrib->flags     = src_attrib->flags;
3934         } else if (src_attrib &&
3935                    (src_attrib->flags & VA_DISPLAY_ATTRIB_SETTABLE)) {
3936             dst_attrib->flags     = src_attrib->flags;
3937         } else
3938             dst_attrib->flags = VA_DISPLAY_ATTRIB_NOT_SUPPORTED;
3939     }
3940     return VA_STATUS_SUCCESS;
3941 }
3942
3943 /*
3944  * Set display attributes
3945  * Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
3946  * from vaQueryDisplayAttributes() can be set.  If the attribute is not settable or
3947  * the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
3948  */
3949 VAStatus
3950 i965_SetDisplayAttributes(
3951     VADriverContextP    ctx,
3952     VADisplayAttribute *attribs,        /* in */
3953     int                 num_attribs     /* in */
3954 )
3955 {
3956     int i;
3957
3958     for (i = 0; i < num_attribs; i++) {
3959         VADisplayAttribute *dst_attrib, * const src_attrib = &attribs[i];
3960
3961         dst_attrib = get_display_attribute(ctx, src_attrib->type);
3962         if (!dst_attrib)
3963             return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
3964
3965         if (!(dst_attrib->flags & VA_DISPLAY_ATTRIB_SETTABLE))
3966             continue;
3967
3968         if (src_attrib->value < dst_attrib->min_value ||
3969             src_attrib->value > dst_attrib->max_value)
3970             return VA_STATUS_ERROR_INVALID_PARAMETER;
3971
3972         dst_attrib->value = src_attrib->value;
3973         /* XXX: track modified attributes through timestamps */
3974     }
3975     return VA_STATUS_SUCCESS;
3976 }
3977
3978 VAStatus
3979 i965_DbgCopySurfaceToBuffer(VADriverContextP ctx,
3980                             VASurfaceID surface,
3981                             void **buffer,              /* out */
3982                             unsigned int *stride)       /* out */
3983 {
3984     /* TODO */
3985     return VA_STATUS_ERROR_UNIMPLEMENTED;
3986 }
3987
3988 static void
3989 i965_destroy_heap(struct object_heap *heap,
3990                   void (*func)(struct object_heap *heap, struct object_base *object))
3991 {
3992     struct object_base *object;
3993     object_heap_iterator iter;
3994
3995     object = object_heap_first(heap, &iter);
3996
3997     while (object) {
3998         if (func)
3999             func(heap, object);
4000
4001         object = object_heap_next(heap, &iter);
4002     }
4003
4004     object_heap_destroy(heap);
4005 }
4006
4007
4008 VAStatus
4009 i965_DestroyImage(VADriverContextP ctx, VAImageID image);
4010
4011 VAStatus
4012 i965_CreateImage(VADriverContextP ctx,
4013                  VAImageFormat *format,
4014                  int width,
4015                  int height,
4016                  VAImage *out_image)        /* out */
4017 {
4018     struct i965_driver_data *i965 = i965_driver_data(ctx);
4019     struct object_image *obj_image;
4020     VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
4021     VAImageID image_id;
4022     unsigned int size2, size, awidth, aheight;
4023
4024     out_image->image_id = VA_INVALID_ID;
4025     out_image->buf      = VA_INVALID_ID;
4026
4027     image_id = NEW_IMAGE_ID();
4028     if (image_id == VA_INVALID_ID)
4029         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4030
4031     obj_image = IMAGE(image_id);
4032     if (!obj_image)
4033         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4034     obj_image->bo         = NULL;
4035     obj_image->palette    = NULL;
4036     obj_image->derived_surface = VA_INVALID_ID;
4037
4038     VAImage * const image = &obj_image->image;
4039     image->image_id       = image_id;
4040     image->buf            = VA_INVALID_ID;
4041
4042     awidth = ALIGN(width, i965->codec_info->min_linear_wpitch);
4043
4044     if ((format->fourcc == VA_FOURCC_YV12) ||
4045         (format->fourcc == VA_FOURCC_I420)) {
4046         if (awidth % 128 != 0) {
4047             awidth = ALIGN(width, 128);
4048         }
4049     }
4050
4051     aheight = ALIGN(height, i965->codec_info->min_linear_hpitch);
4052     size    = awidth * aheight;
4053     size2    = (awidth / 2) * (aheight / 2);
4054
4055     image->num_palette_entries = 0;
4056     image->entry_bytes         = 0;
4057     memset(image->component_order, 0, sizeof(image->component_order));
4058
4059     switch (format->fourcc) {
4060     case VA_FOURCC_IA44:
4061     case VA_FOURCC_AI44:
4062         image->num_planes = 1;
4063         image->pitches[0] = awidth;
4064         image->offsets[0] = 0;
4065         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
4066         image->num_palette_entries = 16;
4067         image->entry_bytes         = 3;
4068         image->component_order[0]  = 'R';
4069         image->component_order[1]  = 'G';
4070         image->component_order[2]  = 'B';
4071         break;
4072     case VA_FOURCC_IA88:
4073     case VA_FOURCC_AI88:
4074         image->num_planes = 1;
4075         image->pitches[0] = awidth * 2;
4076         image->offsets[0] = 0;
4077         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
4078         image->num_palette_entries = 256;
4079         image->entry_bytes         = 3;
4080         image->component_order[0]  = 'R';
4081         image->component_order[1]  = 'G';
4082         image->component_order[2]  = 'B';
4083         break;
4084     case VA_FOURCC_ARGB:
4085     case VA_FOURCC_ABGR:
4086     case VA_FOURCC_BGRA:
4087     case VA_FOURCC_RGBA:
4088     case VA_FOURCC_BGRX:
4089     case VA_FOURCC_RGBX:
4090         image->num_planes = 1;
4091         image->pitches[0] = awidth * 4;
4092         image->offsets[0] = 0;
4093         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
4094         break;
4095     case VA_FOURCC_YV12:
4096         image->num_planes = 3;
4097         image->pitches[0] = awidth;
4098         image->offsets[0] = 0;
4099         image->pitches[1] = awidth / 2;
4100         image->offsets[1] = size;
4101         image->pitches[2] = awidth / 2;
4102         image->offsets[2] = size + size2;
4103         image->data_size  = size + 2 * size2;
4104         break;
4105     case VA_FOURCC_I420:
4106         image->num_planes = 3;
4107         image->pitches[0] = awidth;
4108         image->offsets[0] = 0;
4109         image->pitches[1] = awidth / 2;
4110         image->offsets[1] = size;
4111         image->pitches[2] = awidth / 2;
4112         image->offsets[2] = size + size2;
4113         image->data_size  = size + 2 * size2;
4114         break;
4115     case VA_FOURCC_422H:
4116         image->num_planes = 3;
4117         image->pitches[0] = awidth;
4118         image->offsets[0] = 0;
4119         image->pitches[1] = awidth / 2;
4120         image->offsets[1] = size;
4121         image->pitches[2] = awidth / 2;
4122         image->offsets[2] = size + (awidth / 2) * aheight;
4123         image->data_size  = size + 2 * ((awidth / 2) * aheight);
4124         break;
4125     case VA_FOURCC_NV12:
4126         image->num_planes = 2;
4127         image->pitches[0] = awidth;
4128         image->offsets[0] = 0;
4129         image->pitches[1] = awidth;
4130         image->offsets[1] = size;
4131         image->data_size  = size + 2 * size2;
4132         break;
4133     case VA_FOURCC_YUY2:
4134     case VA_FOURCC_UYVY:
4135         image->num_planes = 1;
4136         image->pitches[0] = awidth * 2;
4137         image->offsets[0] = 0;
4138         image->data_size  = size * 2;
4139         break;
4140     case VA_FOURCC_P010:
4141         image->num_planes = 2;
4142         image->pitches[0] = awidth * 2;
4143         image->offsets[0] = 0;
4144         image->pitches[1] = awidth * 2;
4145         image->offsets[1] = size * 2;
4146         image->data_size  = size * 2 + 2 * size2 * 2;
4147         break;
4148     default:
4149         goto error;
4150     }
4151
4152     va_status = i965_CreateBuffer(ctx, 0, VAImageBufferType,
4153                                   image->data_size, 1, NULL, &image->buf);
4154     if (va_status != VA_STATUS_SUCCESS)
4155         goto error;
4156
4157     struct object_buffer *obj_buffer = BUFFER(image->buf);
4158
4159     if (!obj_buffer ||
4160         !obj_buffer->buffer_store ||
4161         !obj_buffer->buffer_store->bo)
4162         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4163
4164     obj_image->bo = obj_buffer->buffer_store->bo;
4165     dri_bo_reference(obj_image->bo);
4166
4167     if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
4168         obj_image->palette = malloc(image->num_palette_entries * sizeof(*obj_image->palette));
4169         if (!obj_image->palette)
4170             goto error;
4171     }
4172
4173     image->image_id             = image_id;
4174     image->format               = *format;
4175     image->width                = width;
4176     image->height               = height;
4177
4178     *out_image                  = *image;
4179     return VA_STATUS_SUCCESS;
4180
4181 error:
4182     i965_DestroyImage(ctx, image_id);
4183     return va_status;
4184 }
4185
4186 VAStatus
4187 i965_check_alloc_surface_bo(VADriverContextP ctx,
4188                             struct object_surface *obj_surface,
4189                             int tiled,
4190                             unsigned int fourcc,
4191                             unsigned int subsampling)
4192 {
4193     struct i965_driver_data *i965 = i965_driver_data(ctx);
4194     int region_width, region_height;
4195
4196     if (obj_surface->bo) {
4197         ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4198         ASSERT_RET(obj_surface->fourcc == fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4199         ASSERT_RET(obj_surface->subsampling == subsampling, VA_STATUS_ERROR_INVALID_SURFACE);
4200         return VA_STATUS_SUCCESS;
4201     }
4202
4203     obj_surface->x_cb_offset = 0; /* X offset is always 0 */
4204     obj_surface->x_cr_offset = 0;
4205
4206     int bpp_1stplane = bpp_1stplane_by_fourcc(fourcc);
4207
4208     if ((tiled && !obj_surface->user_disable_tiling)) {
4209         ASSERT_RET(fourcc != VA_FOURCC_I420 &&
4210                    fourcc != VA_FOURCC_IYUV &&
4211                    fourcc != VA_FOURCC_YV12,
4212                    VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4213
4214         if (obj_surface->user_h_stride_set) {
4215             ASSERT_RET(IS_ALIGNED(obj_surface->width, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
4216         } else
4217             obj_surface->width = ALIGN(obj_surface->orig_width * bpp_1stplane, 128);
4218
4219         if (obj_surface->user_v_stride_set) {
4220             ASSERT_RET(IS_ALIGNED(obj_surface->height, 32), VA_STATUS_ERROR_INVALID_PARAMETER);
4221         } else
4222             obj_surface->height = ALIGN(obj_surface->orig_height, 32);
4223
4224         region_height = obj_surface->height;
4225
4226         switch (fourcc) {
4227         case VA_FOURCC_NV12:
4228         case VA_FOURCC_P010:
4229             assert(subsampling == SUBSAMPLE_YUV420);
4230             obj_surface->cb_cr_pitch = obj_surface->width;
4231             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4232             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4233             obj_surface->y_cb_offset = obj_surface->height;
4234             obj_surface->y_cr_offset = obj_surface->height;
4235             region_width = obj_surface->width;
4236             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32);
4237
4238             break;
4239
4240         case VA_FOURCC_IMC1:
4241             assert(subsampling == SUBSAMPLE_YUV420);
4242             obj_surface->cb_cr_pitch = obj_surface->width;
4243             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4244             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4245             obj_surface->y_cr_offset = obj_surface->height;
4246             obj_surface->y_cb_offset = obj_surface->y_cr_offset + ALIGN(obj_surface->cb_cr_height, 32);
4247             region_width = obj_surface->width;
4248             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4249
4250             break;
4251
4252         case VA_FOURCC_IMC3:
4253             assert(subsampling == SUBSAMPLE_YUV420);
4254             obj_surface->cb_cr_pitch = obj_surface->width;
4255             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4256             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4257             obj_surface->y_cb_offset = obj_surface->height;
4258             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4259             region_width = obj_surface->width;
4260             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4261
4262             break;
4263
4264         case VA_FOURCC_422H:
4265             assert(subsampling == SUBSAMPLE_YUV422H);
4266             obj_surface->cb_cr_pitch = obj_surface->width;
4267             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4268             obj_surface->cb_cr_height = obj_surface->orig_height;
4269             obj_surface->y_cb_offset = obj_surface->height;
4270             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4271             region_width = obj_surface->width;
4272             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4273
4274             break;
4275
4276         case VA_FOURCC_422V:
4277             assert(subsampling == SUBSAMPLE_YUV422V);
4278             obj_surface->cb_cr_pitch = obj_surface->width;
4279             obj_surface->cb_cr_width = obj_surface->orig_width;
4280             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4281             obj_surface->y_cb_offset = obj_surface->height;
4282             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4283             region_width = obj_surface->width;
4284             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4285
4286             break;
4287
4288         case VA_FOURCC_411P:
4289             assert(subsampling == SUBSAMPLE_YUV411);
4290             obj_surface->cb_cr_pitch = obj_surface->width;
4291             obj_surface->cb_cr_width = obj_surface->orig_width / 4;
4292             obj_surface->cb_cr_height = obj_surface->orig_height;
4293             obj_surface->y_cb_offset = obj_surface->height;
4294             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4295             region_width = obj_surface->width;
4296             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4297
4298             break;
4299
4300         case VA_FOURCC_444P:
4301             assert(subsampling == SUBSAMPLE_YUV444);
4302             obj_surface->cb_cr_pitch = obj_surface->width;
4303             obj_surface->cb_cr_width = obj_surface->orig_width;
4304             obj_surface->cb_cr_height = obj_surface->orig_height;
4305             obj_surface->y_cb_offset = obj_surface->height;
4306             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4307             region_width = obj_surface->width;
4308             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4309
4310             break;
4311
4312         case VA_FOURCC_Y800:
4313             assert(subsampling == SUBSAMPLE_YUV400);
4314             obj_surface->cb_cr_pitch = 0;
4315             obj_surface->cb_cr_width = 0;
4316             obj_surface->cb_cr_height = 0;
4317             obj_surface->y_cb_offset = 0;
4318             obj_surface->y_cr_offset = 0;
4319             region_width = obj_surface->width;
4320             region_height = obj_surface->height;
4321
4322             break;
4323
4324         case VA_FOURCC_YUY2:
4325         case VA_FOURCC_UYVY:
4326             assert(subsampling == SUBSAMPLE_YUV422H);
4327             obj_surface->width = ALIGN(obj_surface->orig_width * 2, 128);
4328             obj_surface->cb_cr_pitch = obj_surface->width;
4329             obj_surface->y_cb_offset = 0;
4330             obj_surface->y_cr_offset = 0;
4331             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4332             obj_surface->cb_cr_height = obj_surface->orig_height;
4333             region_width = obj_surface->width;
4334             region_height = obj_surface->height;
4335
4336             break;
4337
4338         case VA_FOURCC_RGBA:
4339         case VA_FOURCC_RGBX:
4340         case VA_FOURCC_BGRA:
4341         case VA_FOURCC_BGRX:
4342             assert(subsampling == SUBSAMPLE_RGBX);
4343
4344             obj_surface->width = ALIGN(obj_surface->orig_width * 4, 128);
4345             region_width = obj_surface->width;
4346             region_height = obj_surface->height;
4347             break;
4348
4349         default:
4350             /* Never get here */
4351             ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4352             break;
4353         }
4354     } else {
4355         assert(subsampling == SUBSAMPLE_YUV420 ||
4356                subsampling == SUBSAMPLE_YUV422H ||
4357                subsampling == SUBSAMPLE_YUV422V ||
4358                subsampling == SUBSAMPLE_RGBX);
4359
4360         region_width = obj_surface->width;
4361         region_height = obj_surface->height;
4362
4363         switch (fourcc) {
4364         case VA_FOURCC_NV12:
4365         case VA_FOURCC_P010:
4366             obj_surface->y_cb_offset = obj_surface->height;
4367             obj_surface->y_cr_offset = obj_surface->height;
4368             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4369             obj_surface->width = ALIGN(obj_surface->cb_cr_width * 2, i965->codec_info->min_linear_wpitch) *
4370                                  bpp_1stplane;
4371             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4372             obj_surface->cb_cr_pitch = obj_surface->width;
4373             region_width = obj_surface->width;
4374             region_height = obj_surface->height + obj_surface->height / 2;
4375             break;
4376
4377         case VA_FOURCC_YV16:
4378             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4379             obj_surface->width = ALIGN(obj_surface->cb_cr_width, i965->codec_info->min_linear_wpitch) * 2;
4380             obj_surface->cb_cr_height = obj_surface->orig_height;
4381             obj_surface->y_cr_offset = obj_surface->height;
4382             obj_surface->y_cb_offset = obj_surface->y_cr_offset + ALIGN(obj_surface->cb_cr_height, 32) / 2;
4383             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4384             region_width = obj_surface->width;
4385             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32);
4386             break;
4387
4388         case VA_FOURCC_YV12:
4389         case VA_FOURCC_I420:
4390         case VA_FOURCC_IYUV:
4391             if (fourcc == VA_FOURCC_YV12) {
4392                 obj_surface->y_cr_offset = obj_surface->height;
4393                 obj_surface->y_cb_offset = obj_surface->height + obj_surface->height / 4;
4394             } else {
4395                 obj_surface->y_cb_offset = obj_surface->height;
4396                 obj_surface->y_cr_offset = obj_surface->height + obj_surface->height / 4;
4397             }
4398
4399             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4400             obj_surface->width = ALIGN(obj_surface->cb_cr_width, i965->codec_info->min_linear_wpitch) * 2;
4401             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4402             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4403             region_width = obj_surface->width;
4404             region_height = obj_surface->height + obj_surface->height / 2;
4405             break;
4406
4407         case VA_FOURCC_I010:
4408             obj_surface->y_cb_offset = obj_surface->height;
4409             obj_surface->y_cr_offset = obj_surface->height + obj_surface->height / 4;
4410             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4411             obj_surface->width = ALIGN(obj_surface->cb_cr_width * 2, i965->codec_info->min_linear_wpitch) * 2;
4412             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4413             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4414             region_width = obj_surface->width;
4415             region_height = obj_surface->height + obj_surface->height / 2;
4416
4417             break;
4418         case VA_FOURCC_YUY2:
4419         case VA_FOURCC_UYVY:
4420             obj_surface->width = ALIGN(obj_surface->orig_width * 2, i965->codec_info->min_linear_wpitch);
4421             obj_surface->y_cb_offset = 0;
4422             obj_surface->y_cr_offset = 0;
4423             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4424             obj_surface->cb_cr_height = obj_surface->orig_height;
4425             obj_surface->cb_cr_pitch = obj_surface->width;
4426             region_width = obj_surface->width;
4427             region_height = obj_surface->height;
4428             break;
4429         case VA_FOURCC_RGBA:
4430         case VA_FOURCC_RGBX:
4431         case VA_FOURCC_BGRA:
4432         case VA_FOURCC_BGRX:
4433             obj_surface->width = ALIGN(obj_surface->orig_width * 4, i965->codec_info->min_linear_wpitch);
4434             region_width = obj_surface->width;
4435             region_height = obj_surface->height;
4436             break;
4437
4438         default:
4439             /* Never get here */
4440             ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4441             break;
4442         }
4443     }
4444
4445     obj_surface->size = ALIGN(region_width * region_height, 0x1000);
4446
4447     if ((tiled && !obj_surface->user_disable_tiling)) {
4448         uint32_t tiling_mode = I915_TILING_Y; /* always uses Y-tiled format */
4449         unsigned long pitch;
4450
4451         obj_surface->bo = drm_intel_bo_alloc_tiled(i965->intel.bufmgr,
4452                                                    "vaapi surface",
4453                                                    region_width,
4454                                                    region_height,
4455                                                    1,
4456                                                    &tiling_mode,
4457                                                    &pitch,
4458                                                    0);
4459         assert(tiling_mode == I915_TILING_Y);
4460         assert(pitch == obj_surface->width);
4461     } else {
4462         obj_surface->bo = dri_bo_alloc(i965->intel.bufmgr,
4463                                        "vaapi surface",
4464                                        obj_surface->size,
4465                                        0x1000);
4466     }
4467
4468     obj_surface->fourcc = fourcc;
4469     obj_surface->subsampling = subsampling;
4470     assert(obj_surface->bo);
4471     return VA_STATUS_SUCCESS;
4472 }
4473
4474 VAStatus i965_DeriveImage(VADriverContextP ctx,
4475                           VASurfaceID surface,
4476                           VAImage *out_image)        /* out */
4477 {
4478     struct i965_driver_data *i965 = i965_driver_data(ctx);
4479     struct object_image *obj_image;
4480     struct object_surface *obj_surface;
4481     VAImageID image_id;
4482     unsigned int w_pitch;
4483     VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
4484
4485     out_image->image_id = VA_INVALID_ID;
4486     obj_surface = SURFACE(surface);
4487
4488     if (!obj_surface)
4489         return VA_STATUS_ERROR_INVALID_SURFACE;
4490
4491     if (!obj_surface->bo) {
4492         unsigned int is_tiled = 0;
4493         unsigned int fourcc = VA_FOURCC_YV12;
4494         i965_guess_surface_format(ctx, surface, &fourcc, &is_tiled);
4495         int sampling = get_sampling_from_fourcc(fourcc);
4496         va_status = i965_check_alloc_surface_bo(ctx, obj_surface, is_tiled, fourcc, sampling);
4497         if (va_status != VA_STATUS_SUCCESS)
4498             return va_status;
4499     }
4500
4501     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4502
4503     w_pitch = obj_surface->width;
4504
4505     image_id = NEW_IMAGE_ID();
4506
4507     if (image_id == VA_INVALID_ID)
4508         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4509
4510     obj_image = IMAGE(image_id);
4511
4512     if (!obj_image)
4513         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4514
4515     obj_image->bo = NULL;
4516     obj_image->palette = NULL;
4517     obj_image->derived_surface = VA_INVALID_ID;
4518
4519     VAImage * const image = &obj_image->image;
4520
4521     memset(image, 0, sizeof(*image));
4522     image->image_id = image_id;
4523     image->buf = VA_INVALID_ID;
4524     image->num_palette_entries = 0;
4525     image->entry_bytes = 0;
4526     image->width = obj_surface->orig_width;
4527     image->height = obj_surface->orig_height;
4528     image->data_size = obj_surface->size;
4529
4530     image->format.fourcc = obj_surface->fourcc;
4531     image->format.byte_order = VA_LSB_FIRST;
4532     image->format.bits_per_pixel = get_bpp_from_fourcc(obj_surface->fourcc);
4533
4534     if (!image->format.bits_per_pixel)
4535         goto error;
4536
4537     switch (image->format.fourcc) {
4538     case VA_FOURCC_YV12:
4539         image->num_planes = 3;
4540         image->pitches[0] = w_pitch; /* Y */
4541         image->offsets[0] = 0;
4542         image->pitches[1] = obj_surface->cb_cr_pitch; /* V */
4543         image->offsets[1] = w_pitch * obj_surface->y_cr_offset;
4544         image->pitches[2] = obj_surface->cb_cr_pitch; /* U */
4545         image->offsets[2] = w_pitch * obj_surface->y_cb_offset;
4546         break;
4547
4548     case VA_FOURCC_YV16:
4549         image->num_planes = 3;
4550         image->pitches[0] = w_pitch; /* Y */
4551         image->offsets[0] = 0;
4552         image->pitches[1] = obj_surface->cb_cr_pitch; /* V */
4553         image->offsets[1] = w_pitch * obj_surface->y_cr_offset;
4554         image->pitches[2] = obj_surface->cb_cr_pitch; /* U */
4555         image->offsets[2] = w_pitch * obj_surface->y_cb_offset;
4556         break;
4557
4558     case VA_FOURCC_NV12:
4559     case VA_FOURCC_P010:
4560         image->num_planes = 2;
4561         image->pitches[0] = w_pitch; /* Y */
4562         image->offsets[0] = 0;
4563         image->pitches[1] = obj_surface->cb_cr_pitch; /* UV */
4564         image->offsets[1] = w_pitch * obj_surface->y_cb_offset;
4565         break;
4566
4567     case VA_FOURCC_I420:
4568     case VA_FOURCC_I010:
4569     case VA_FOURCC_422H:
4570     case VA_FOURCC_IMC3:
4571     case VA_FOURCC_444P:
4572     case VA_FOURCC_422V:
4573     case VA_FOURCC_411P:
4574         image->num_planes = 3;
4575         image->pitches[0] = w_pitch; /* Y */
4576         image->offsets[0] = 0;
4577         image->pitches[1] = obj_surface->cb_cr_pitch; /* U */
4578         image->offsets[1] = w_pitch * obj_surface->y_cb_offset;
4579         image->pitches[2] = obj_surface->cb_cr_pitch; /* V */
4580         image->offsets[2] = w_pitch * obj_surface->y_cr_offset;
4581         break;
4582
4583     case VA_FOURCC_YUY2:
4584     case VA_FOURCC_UYVY:
4585     case VA_FOURCC_Y800:
4586         image->num_planes = 1;
4587         image->pitches[0] = obj_surface->width; /* Y, width is aligned already */
4588         image->offsets[0] = 0;
4589         break;
4590     case VA_FOURCC_RGBA:
4591     case VA_FOURCC_RGBX:
4592     case VA_FOURCC_BGRA:
4593     case VA_FOURCC_BGRX:
4594         image->num_planes = 1;
4595         image->pitches[0] = obj_surface->width;
4596
4597         switch (image->format.fourcc) {
4598         case VA_FOURCC_RGBA:
4599         case VA_FOURCC_RGBX:
4600             image->format.red_mask = 0x000000ff;
4601             image->format.green_mask = 0x0000ff00;
4602             image->format.blue_mask = 0x00ff0000;
4603             break;
4604         case VA_FOURCC_BGRA:
4605         case VA_FOURCC_BGRX:
4606             image->format.red_mask = 0x00ff0000;
4607             image->format.green_mask = 0x0000ff00;
4608             image->format.blue_mask = 0x000000ff;
4609             break;
4610         default:
4611             goto error;
4612         }
4613
4614         switch (image->format.fourcc) {
4615         case VA_FOURCC_RGBA:
4616         case VA_FOURCC_BGRA:
4617             image->format.alpha_mask = 0xff000000;
4618             image->format.depth = 32;
4619             break;
4620         case VA_FOURCC_RGBX:
4621         case VA_FOURCC_BGRX:
4622             image->format.alpha_mask = 0x00000000;
4623             image->format.depth = 24;
4624             break;
4625         default:
4626             goto error;
4627         }
4628
4629         break;
4630     default:
4631         goto error;
4632     }
4633
4634     va_status = i965_create_buffer_internal(ctx, 0, VAImageBufferType,
4635                                             obj_surface->size, 1, NULL, obj_surface->bo, &image->buf);
4636     if (va_status != VA_STATUS_SUCCESS)
4637         goto error;
4638
4639     struct object_buffer *obj_buffer = BUFFER(image->buf);
4640
4641     if (!obj_buffer ||
4642         !obj_buffer->buffer_store ||
4643         !obj_buffer->buffer_store->bo)
4644         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4645
4646     obj_image->bo = obj_buffer->buffer_store->bo;
4647     dri_bo_reference(obj_image->bo);
4648
4649     if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
4650         obj_image->palette = malloc(image->num_palette_entries * sizeof(*obj_image->palette));
4651         if (!obj_image->palette) {
4652             va_status = VA_STATUS_ERROR_ALLOCATION_FAILED;
4653             goto error;
4654         }
4655     }
4656
4657     *out_image = *image;
4658     obj_surface->flags |= SURFACE_DERIVED;
4659     obj_surface->derived_image_id = image_id;
4660     obj_image->derived_surface = surface;
4661
4662     return VA_STATUS_SUCCESS;
4663
4664 error:
4665     i965_DestroyImage(ctx, image_id);
4666     return va_status;
4667 }
4668
4669 static void
4670 i965_destroy_image(struct object_heap *heap, struct object_base *obj)
4671 {
4672     object_heap_free(heap, obj);
4673 }
4674
4675
4676 VAStatus
4677 i965_DestroyImage(VADriverContextP ctx, VAImageID image)
4678 {
4679     struct i965_driver_data *i965 = i965_driver_data(ctx);
4680     struct object_image *obj_image = IMAGE(image);
4681     struct object_surface *obj_surface;
4682
4683     if (!obj_image)
4684         return VA_STATUS_SUCCESS;
4685
4686     dri_bo_unreference(obj_image->bo);
4687     obj_image->bo = NULL;
4688
4689     if (obj_image->image.buf != VA_INVALID_ID) {
4690         i965_DestroyBuffer(ctx, obj_image->image.buf);
4691         obj_image->image.buf = VA_INVALID_ID;
4692     }
4693
4694     if (obj_image->palette) {
4695         free(obj_image->palette);
4696         obj_image->palette = NULL;
4697     }
4698
4699     obj_surface = SURFACE(obj_image->derived_surface);
4700
4701     if (obj_surface) {
4702         obj_surface->flags &= ~SURFACE_DERIVED;
4703         obj_surface->derived_image_id = VA_INVALID_ID;
4704     }
4705
4706     i965_destroy_image(&i965->image_heap, (struct object_base *)obj_image);
4707
4708     return VA_STATUS_SUCCESS;
4709 }
4710
4711 /*
4712  * pointer to an array holding the palette data.  The size of the array is
4713  * num_palette_entries * entry_bytes in size.  The order of the components
4714  * in the palette is described by the component_order in VASubpicture struct
4715  */
4716 VAStatus
4717 i965_SetImagePalette(VADriverContextP ctx,
4718                      VAImageID image,
4719                      unsigned char *palette)
4720 {
4721     struct i965_driver_data *i965 = i965_driver_data(ctx);
4722     unsigned int i;
4723
4724     struct object_image *obj_image = IMAGE(image);
4725     if (!obj_image)
4726         return VA_STATUS_ERROR_INVALID_IMAGE;
4727
4728     if (!obj_image->palette)
4729         return VA_STATUS_ERROR_ALLOCATION_FAILED; /* XXX: unpaletted/error */
4730
4731     for (i = 0; i < obj_image->image.num_palette_entries; i++)
4732         obj_image->palette[i] = (((unsigned int)palette[3 * i + 0] << 16) |
4733                                  ((unsigned int)palette[3 * i + 1] <<  8) |
4734                                  (unsigned int)palette[3 * i + 2]);
4735     return VA_STATUS_SUCCESS;
4736 }
4737
4738 static int
4739 get_sampling_from_fourcc(unsigned int fourcc)
4740 {
4741     const i965_fourcc_info *info = get_fourcc_info(fourcc);
4742
4743     if (info && (info->flag & I_S))
4744         return info->subsampling;
4745     else
4746         return -1;
4747 }
4748
4749 static inline void
4750 memcpy_pic(uint8_t *dst, unsigned int dst_stride,
4751            const uint8_t *src, unsigned int src_stride,
4752            unsigned int len, unsigned int height)
4753 {
4754     unsigned int i;
4755
4756     for (i = 0; i < height; i++) {
4757         memcpy(dst, src, len);
4758         dst += dst_stride;
4759         src += src_stride;
4760     }
4761 }
4762
4763 static VAStatus
4764 get_image_i420(struct object_image *obj_image, uint8_t *image_data,
4765                struct object_surface *obj_surface,
4766                const VARectangle *rect)
4767 {
4768     uint8_t *dst[3], *src[3];
4769     const int Y = 0;
4770     const int U = obj_image->image.format.fourcc == obj_surface->fourcc ? 1 : 2;
4771     const int V = obj_image->image.format.fourcc == obj_surface->fourcc ? 2 : 1;
4772     unsigned int tiling, swizzle;
4773     VAStatus va_status = VA_STATUS_SUCCESS;
4774
4775     if (!obj_surface->bo)
4776         return VA_STATUS_ERROR_INVALID_SURFACE;
4777
4778     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4779     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
4780
4781     if (tiling != I915_TILING_NONE)
4782         drm_intel_gem_bo_map_gtt(obj_surface->bo);
4783     else
4784         dri_bo_map(obj_surface->bo, 0);
4785
4786     if (!obj_surface->bo->virtual)
4787         return VA_STATUS_ERROR_INVALID_SURFACE;
4788
4789     /* Dest VA image has either I420 or YV12 format.
4790        Source VA surface alway has I420 format */
4791     dst[Y] = image_data + obj_image->image.offsets[Y];
4792     src[0] = (uint8_t *)obj_surface->bo->virtual;
4793     dst[U] = image_data + obj_image->image.offsets[U];
4794     src[1] = src[0] + obj_surface->width * obj_surface->height;
4795     dst[V] = image_data + obj_image->image.offsets[V];
4796     src[2] = src[1] + (obj_surface->width / 2) * (obj_surface->height / 2);
4797
4798     /* Y plane */
4799     dst[Y] += rect->y * obj_image->image.pitches[Y] + rect->x;
4800     src[0] += rect->y * obj_surface->width + rect->x;
4801     memcpy_pic(dst[Y], obj_image->image.pitches[Y],
4802                src[0], obj_surface->width,
4803                rect->width, rect->height);
4804
4805     /* U plane */
4806     dst[U] += (rect->y / 2) * obj_image->image.pitches[U] + rect->x / 2;
4807     src[1] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
4808     memcpy_pic(dst[U], obj_image->image.pitches[U],
4809                src[1], obj_surface->width / 2,
4810                rect->width / 2, rect->height / 2);
4811
4812     /* V plane */
4813     dst[V] += (rect->y / 2) * obj_image->image.pitches[V] + rect->x / 2;
4814     src[2] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
4815     memcpy_pic(dst[V], obj_image->image.pitches[V],
4816                src[2], obj_surface->width / 2,
4817                rect->width / 2, rect->height / 2);
4818
4819     if (tiling != I915_TILING_NONE)
4820         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
4821     else
4822         dri_bo_unmap(obj_surface->bo);
4823
4824     return va_status;
4825 }
4826
4827 static VAStatus
4828 get_image_nv12(struct object_image *obj_image, uint8_t *image_data,
4829                struct object_surface *obj_surface,
4830                const VARectangle *rect)
4831 {
4832     uint8_t *dst[2], *src[2];
4833     unsigned int tiling, swizzle;
4834     VAStatus va_status = VA_STATUS_SUCCESS;
4835
4836     if (!obj_surface->bo)
4837         return VA_STATUS_ERROR_INVALID_SURFACE;
4838
4839     assert(obj_surface->fourcc);
4840     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
4841
4842     if (tiling != I915_TILING_NONE)
4843         drm_intel_gem_bo_map_gtt(obj_surface->bo);
4844     else
4845         dri_bo_map(obj_surface->bo, 0);
4846
4847     if (!obj_surface->bo->virtual)
4848         return VA_STATUS_ERROR_INVALID_SURFACE;
4849
4850     /* Both dest VA image and source surface have NV12 format */
4851     dst[0] = image_data + obj_image->image.offsets[0];
4852     src[0] = (uint8_t *)obj_surface->bo->virtual;
4853     dst[1] = image_data + obj_image->image.offsets[1];
4854     src[1] = src[0] + obj_surface->width * obj_surface->height;
4855
4856     /* Y plane */
4857     dst[0] += rect->y * obj_image->image.pitches[0] + rect->x;
4858     src[0] += rect->y * obj_surface->width + rect->x;
4859     memcpy_pic(dst[0], obj_image->image.pitches[0],
4860                src[0], obj_surface->width,
4861                rect->width, rect->height);
4862
4863     /* UV plane */
4864     dst[1] += (rect->y / 2) * obj_image->image.pitches[1] + (rect->x & -2);
4865     src[1] += (rect->y / 2) * obj_surface->width + (rect->x & -2);
4866     memcpy_pic(dst[1], obj_image->image.pitches[1],
4867                src[1], obj_surface->width,
4868                rect->width, rect->height / 2);
4869
4870     if (tiling != I915_TILING_NONE)
4871         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
4872     else
4873         dri_bo_unmap(obj_surface->bo);
4874
4875     return va_status;
4876 }
4877
4878 static VAStatus
4879 get_image_yuy2(struct object_image *obj_image, uint8_t *image_data,
4880                struct object_surface *obj_surface,
4881                const VARectangle *rect)
4882 {
4883     uint8_t *dst, *src;
4884     unsigned int tiling, swizzle;
4885     VAStatus va_status = VA_STATUS_SUCCESS;
4886
4887     if (!obj_surface->bo)
4888         return VA_STATUS_ERROR_INVALID_SURFACE;
4889
4890     assert(obj_surface->fourcc);
4891     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
4892
4893     if (tiling != I915_TILING_NONE)
4894         drm_intel_gem_bo_map_gtt(obj_surface->bo);
4895     else
4896         dri_bo_map(obj_surface->bo, 0);
4897
4898     if (!obj_surface->bo->virtual)
4899         return VA_STATUS_ERROR_INVALID_SURFACE;
4900
4901     /* Both dest VA image and source surface have YUYV format */
4902     dst = image_data + obj_image->image.offsets[0];
4903     src = (uint8_t *)obj_surface->bo->virtual;
4904
4905     /* Y plane */
4906     dst += rect->y * obj_image->image.pitches[0] + rect->x * 2;
4907     src += rect->y * obj_surface->width + rect->x * 2;
4908     memcpy_pic(dst, obj_image->image.pitches[0],
4909                src, obj_surface->width * 2,
4910                rect->width * 2, rect->height);
4911
4912     if (tiling != I915_TILING_NONE)
4913         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
4914     else
4915         dri_bo_unmap(obj_surface->bo);
4916
4917     return va_status;
4918 }
4919
4920 static VAStatus
4921 i965_sw_getimage(VADriverContextP ctx,
4922                  struct object_surface *obj_surface, struct object_image *obj_image,
4923                  const VARectangle *rect)
4924 {
4925     void *image_data = NULL;
4926     VAStatus va_status;
4927
4928     if (obj_surface->fourcc != obj_image->image.format.fourcc)
4929         return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
4930
4931     va_status = i965_MapBuffer(ctx, obj_image->image.buf, &image_data);
4932     if (va_status != VA_STATUS_SUCCESS)
4933         return va_status;
4934
4935     switch (obj_image->image.format.fourcc) {
4936     case VA_FOURCC_YV12:
4937     case VA_FOURCC_I420:
4938         get_image_i420(obj_image, image_data, obj_surface, rect);
4939         break;
4940     case VA_FOURCC_NV12:
4941         get_image_nv12(obj_image, image_data, obj_surface, rect);
4942         break;
4943     case VA_FOURCC_YUY2:
4944         /* YUY2 is the format supported by overlay plane */
4945         get_image_yuy2(obj_image, image_data, obj_surface, rect);
4946         break;
4947     default:
4948         va_status = VA_STATUS_ERROR_OPERATION_FAILED;
4949         break;
4950     }
4951     if (va_status != VA_STATUS_SUCCESS)
4952         return va_status;
4953
4954     va_status = i965_UnmapBuffer(ctx, obj_image->image.buf);
4955     return va_status;
4956 }
4957
4958 static VAStatus
4959 i965_hw_getimage(VADriverContextP ctx,
4960                  struct object_surface *obj_surface, struct object_image *obj_image,
4961                  const VARectangle *rect)
4962 {
4963     struct i965_surface src_surface;
4964     struct i965_surface dst_surface;
4965
4966     src_surface.base = (struct object_base *)obj_surface;
4967     src_surface.type = I965_SURFACE_TYPE_SURFACE;
4968     src_surface.flags = I965_SURFACE_FLAG_FRAME;
4969
4970     dst_surface.base = (struct object_base *)obj_image;
4971     dst_surface.type = I965_SURFACE_TYPE_IMAGE;
4972     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
4973
4974     return i965_image_processing(ctx, &src_surface, rect, &dst_surface, rect);
4975 }
4976
4977 VAStatus
4978 i965_GetImage(VADriverContextP ctx,
4979               VASurfaceID surface,
4980               int x,   /* coordinates of the upper left source pixel */
4981               int y,
4982               unsigned int width,      /* width and height of the region */
4983               unsigned int height,
4984               VAImageID image)
4985 {
4986     struct i965_driver_data * const i965 = i965_driver_data(ctx);
4987     struct object_surface * const obj_surface = SURFACE(surface);
4988     struct object_image * const obj_image = IMAGE(image);
4989     VARectangle rect;
4990     VAStatus va_status;
4991
4992     if (!obj_surface)
4993         return VA_STATUS_ERROR_INVALID_SURFACE;
4994     if (!obj_surface->bo) /* don't get anything, keep previous data */
4995         return VA_STATUS_SUCCESS;
4996     if (is_surface_busy(i965, obj_surface))
4997         return VA_STATUS_ERROR_SURFACE_BUSY;
4998
4999     if (!obj_image || !obj_image->bo)
5000         return VA_STATUS_ERROR_INVALID_IMAGE;
5001     if (is_image_busy(i965, obj_image, surface))
5002         return VA_STATUS_ERROR_SURFACE_BUSY;
5003
5004     if (x < 0 || y < 0)
5005         return VA_STATUS_ERROR_INVALID_PARAMETER;
5006     if (x + width > obj_surface->orig_width ||
5007         y + height > obj_surface->orig_height)
5008         return VA_STATUS_ERROR_INVALID_PARAMETER;
5009     if (x + width > obj_image->image.width ||
5010         y + height > obj_image->image.height)
5011         return VA_STATUS_ERROR_INVALID_PARAMETER;
5012
5013     rect.x = x;
5014     rect.y = y;
5015     rect.width = width;
5016     rect.height = height;
5017
5018     if (HAS_ACCELERATED_GETIMAGE(i965))
5019         va_status = i965_hw_getimage(ctx, obj_surface, obj_image, &rect);
5020     else
5021         va_status = i965_sw_getimage(ctx, obj_surface, obj_image, &rect);
5022
5023     return va_status;
5024 }
5025
5026 static VAStatus
5027 put_image_i420(struct object_surface *obj_surface,
5028                const VARectangle *dst_rect,
5029                struct object_image *obj_image, uint8_t *image_data,
5030                const VARectangle *src_rect)
5031 {
5032     uint8_t *dst[3], *src[3];
5033     const int Y = 0;
5034     const int U = obj_image->image.format.fourcc == obj_surface->fourcc ? 1 : 2;
5035     const int V = obj_image->image.format.fourcc == obj_surface->fourcc ? 2 : 1;
5036     unsigned int tiling, swizzle;
5037     VAStatus va_status = VA_STATUS_SUCCESS;
5038
5039     ASSERT_RET(obj_surface->bo, VA_STATUS_ERROR_INVALID_SURFACE);
5040
5041     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5042     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
5043     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
5044     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5045
5046     if (tiling != I915_TILING_NONE)
5047         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5048     else
5049         dri_bo_map(obj_surface->bo, 0);
5050
5051     if (!obj_surface->bo->virtual)
5052         return VA_STATUS_ERROR_INVALID_SURFACE;
5053
5054     /* Dest VA image has either I420 or YV12 format.
5055        Source VA surface alway has I420 format */
5056     dst[0] = (uint8_t *)obj_surface->bo->virtual;
5057     src[Y] = image_data + obj_image->image.offsets[Y];
5058     dst[1] = dst[0] + obj_surface->width * obj_surface->height;
5059     src[U] = image_data + obj_image->image.offsets[U];
5060     dst[2] = dst[1] + (obj_surface->width / 2) * (obj_surface->height / 2);
5061     src[V] = image_data + obj_image->image.offsets[V];
5062
5063     /* Y plane */
5064     dst[0] += dst_rect->y * obj_surface->width + dst_rect->x;
5065     src[Y] += src_rect->y * obj_image->image.pitches[Y] + src_rect->x;
5066     memcpy_pic(dst[0], obj_surface->width,
5067                src[Y], obj_image->image.pitches[Y],
5068                src_rect->width, src_rect->height);
5069
5070     /* U plane */
5071     dst[1] += (dst_rect->y / 2) * obj_surface->width / 2 + dst_rect->x / 2;
5072     src[U] += (src_rect->y / 2) * obj_image->image.pitches[U] + src_rect->x / 2;
5073     memcpy_pic(dst[1], obj_surface->width / 2,
5074                src[U], obj_image->image.pitches[U],
5075                src_rect->width / 2, src_rect->height / 2);
5076
5077     /* V plane */
5078     dst[2] += (dst_rect->y / 2) * obj_surface->width / 2 + dst_rect->x / 2;
5079     src[V] += (src_rect->y / 2) * obj_image->image.pitches[V] + src_rect->x / 2;
5080     memcpy_pic(dst[2], obj_surface->width / 2,
5081                src[V], obj_image->image.pitches[V],
5082                src_rect->width / 2, src_rect->height / 2);
5083
5084     if (tiling != I915_TILING_NONE)
5085         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5086     else
5087         dri_bo_unmap(obj_surface->bo);
5088
5089     return va_status;
5090 }
5091
5092 static VAStatus
5093 put_image_nv12(struct object_surface *obj_surface,
5094                const VARectangle *dst_rect,
5095                struct object_image *obj_image, uint8_t *image_data,
5096                const VARectangle *src_rect)
5097 {
5098     uint8_t *dst[2], *src[2];
5099     unsigned int tiling, swizzle;
5100     VAStatus va_status = VA_STATUS_SUCCESS;
5101
5102     if (!obj_surface->bo)
5103         return VA_STATUS_ERROR_INVALID_SURFACE;
5104
5105     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5106     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
5107     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
5108     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5109
5110     if (tiling != I915_TILING_NONE)
5111         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5112     else
5113         dri_bo_map(obj_surface->bo, 0);
5114
5115     if (!obj_surface->bo->virtual)
5116         return VA_STATUS_ERROR_INVALID_SURFACE;
5117
5118     /* Both dest VA image and source surface have NV12 format */
5119     dst[0] = (uint8_t *)obj_surface->bo->virtual;
5120     src[0] = image_data + obj_image->image.offsets[0];
5121     dst[1] = dst[0] + obj_surface->width * obj_surface->height;
5122     src[1] = image_data + obj_image->image.offsets[1];
5123
5124     /* Y plane */
5125     dst[0] += dst_rect->y * obj_surface->width + dst_rect->x;
5126     src[0] += src_rect->y * obj_image->image.pitches[0] + src_rect->x;
5127     memcpy_pic(dst[0], obj_surface->width,
5128                src[0], obj_image->image.pitches[0],
5129                src_rect->width, src_rect->height);
5130
5131     /* UV plane */
5132     dst[1] += (dst_rect->y / 2) * obj_surface->width + (dst_rect->x & -2);
5133     src[1] += (src_rect->y / 2) * obj_image->image.pitches[1] + (src_rect->x & -2);
5134     memcpy_pic(dst[1], obj_surface->width,
5135                src[1], obj_image->image.pitches[1],
5136                src_rect->width, src_rect->height / 2);
5137
5138     if (tiling != I915_TILING_NONE)
5139         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5140     else
5141         dri_bo_unmap(obj_surface->bo);
5142
5143     return va_status;
5144 }
5145
5146 static VAStatus
5147 put_image_yuy2(struct object_surface *obj_surface,
5148                const VARectangle *dst_rect,
5149                struct object_image *obj_image, uint8_t *image_data,
5150                const VARectangle *src_rect)
5151 {
5152     uint8_t *dst, *src;
5153     unsigned int tiling, swizzle;
5154     VAStatus va_status = VA_STATUS_SUCCESS;
5155
5156     ASSERT_RET(obj_surface->bo, VA_STATUS_ERROR_INVALID_SURFACE);
5157     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5158     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
5159     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
5160     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5161
5162     if (tiling != I915_TILING_NONE)
5163         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5164     else
5165         dri_bo_map(obj_surface->bo, 0);
5166
5167     if (!obj_surface->bo->virtual)
5168         return VA_STATUS_ERROR_INVALID_SURFACE;
5169
5170     /* Both dest VA image and source surface have YUY2 format */
5171     dst = (uint8_t *)obj_surface->bo->virtual;
5172     src = image_data + obj_image->image.offsets[0];
5173
5174     /* YUYV packed plane */
5175     dst += dst_rect->y * obj_surface->width + dst_rect->x * 2;
5176     src += src_rect->y * obj_image->image.pitches[0] + src_rect->x * 2;
5177     memcpy_pic(dst, obj_surface->width * 2,
5178                src, obj_image->image.pitches[0],
5179                src_rect->width * 2, src_rect->height);
5180
5181     if (tiling != I915_TILING_NONE)
5182         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5183     else
5184         dri_bo_unmap(obj_surface->bo);
5185
5186     return va_status;
5187 }
5188
5189 static VAStatus
5190 i965_sw_putimage(VADriverContextP ctx,
5191                  struct object_surface *obj_surface, struct object_image *obj_image,
5192                  const VARectangle *src_rect, const VARectangle *dst_rect)
5193 {
5194     VAStatus va_status = VA_STATUS_SUCCESS;
5195     void *image_data = NULL;
5196
5197     /* XXX: don't allow scaling */
5198     if (src_rect->width != dst_rect->width ||
5199         src_rect->height != dst_rect->height)
5200         return VA_STATUS_ERROR_INVALID_PARAMETER;
5201
5202     if (obj_surface->fourcc) {
5203         /* Don't allow format mismatch */
5204         if (obj_surface->fourcc != obj_image->image.format.fourcc)
5205             return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
5206     }
5207
5208     else {
5209         /* VA is surface not used for decoding, use same VA image format */
5210         va_status = i965_check_alloc_surface_bo(
5211                         ctx,
5212                         obj_surface,
5213                         0, /* XXX: don't use tiled surface */
5214                         obj_image->image.format.fourcc,
5215                         get_sampling_from_fourcc(obj_image->image.format.fourcc));
5216     }
5217
5218     if (va_status != VA_STATUS_SUCCESS)
5219         return va_status;
5220
5221     va_status = i965_MapBuffer(ctx, obj_image->image.buf, &image_data);
5222     if (va_status != VA_STATUS_SUCCESS)
5223         return va_status;
5224
5225     switch (obj_image->image.format.fourcc) {
5226     case VA_FOURCC_YV12:
5227     case VA_FOURCC_I420:
5228         va_status = put_image_i420(obj_surface, dst_rect, obj_image, image_data, src_rect);
5229         break;
5230     case VA_FOURCC_NV12:
5231         va_status = put_image_nv12(obj_surface, dst_rect, obj_image, image_data, src_rect);
5232         break;
5233     case VA_FOURCC_YUY2:
5234         va_status = put_image_yuy2(obj_surface, dst_rect, obj_image, image_data, src_rect);
5235         break;
5236     default:
5237         va_status = VA_STATUS_ERROR_OPERATION_FAILED;
5238         break;
5239     }
5240     if (va_status != VA_STATUS_SUCCESS)
5241         return va_status;
5242
5243     va_status = i965_UnmapBuffer(ctx, obj_image->image.buf);
5244     return va_status;
5245 }
5246
5247 static VAStatus
5248 i965_hw_putimage(VADriverContextP ctx,
5249                  struct object_surface *obj_surface, struct object_image *obj_image,
5250                  const VARectangle *src_rect, const VARectangle *dst_rect)
5251 {
5252     struct i965_surface src_surface, dst_surface;
5253     VAStatus va_status = VA_STATUS_SUCCESS;
5254
5255     if (!obj_surface->bo) {
5256         unsigned int tiling, swizzle;
5257         int surface_sampling = get_sampling_from_fourcc(obj_image->image.format.fourcc);;
5258         dri_bo_get_tiling(obj_image->bo, &tiling, &swizzle);
5259
5260         i965_check_alloc_surface_bo(ctx,
5261                                     obj_surface,
5262                                     !!tiling,
5263                                     obj_image->image.format.fourcc,
5264                                     surface_sampling);
5265     }
5266
5267     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5268
5269     src_surface.base = (struct object_base *)obj_image;
5270     src_surface.type = I965_SURFACE_TYPE_IMAGE;
5271     src_surface.flags = I965_SURFACE_FLAG_FRAME;
5272
5273     dst_surface.base = (struct object_base *)obj_surface;
5274     dst_surface.type = I965_SURFACE_TYPE_SURFACE;
5275     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
5276
5277     va_status = i965_image_processing(ctx,
5278                                       &src_surface,
5279                                       src_rect,
5280                                       &dst_surface,
5281                                       dst_rect);
5282
5283     return  va_status;
5284 }
5285
5286 static VAStatus
5287 i965_PutImage(VADriverContextP ctx,
5288               VASurfaceID surface,
5289               VAImageID image,
5290               int src_x,
5291               int src_y,
5292               unsigned int src_width,
5293               unsigned int src_height,
5294               int dest_x,
5295               int dest_y,
5296               unsigned int dest_width,
5297               unsigned int dest_height)
5298 {
5299     struct i965_driver_data * const i965 = i965_driver_data(ctx);
5300     struct object_surface * const obj_surface = SURFACE(surface);
5301     struct object_image * const obj_image = IMAGE(image);
5302     VARectangle src_rect, dst_rect;
5303     VAStatus va_status;
5304
5305     if (!obj_surface)
5306         return VA_STATUS_ERROR_INVALID_SURFACE;
5307     if (is_surface_busy(i965, obj_surface))
5308         return VA_STATUS_ERROR_SURFACE_BUSY;
5309
5310     if (!obj_image || !obj_image->bo)
5311         return VA_STATUS_ERROR_INVALID_IMAGE;
5312     if (is_image_busy(i965, obj_image, surface))
5313         return VA_STATUS_ERROR_SURFACE_BUSY;
5314
5315     if (src_x < 0 ||
5316         src_y < 0 ||
5317         src_x + src_width > obj_image->image.width ||
5318         src_y + src_height > obj_image->image.height)
5319         return VA_STATUS_ERROR_INVALID_PARAMETER;
5320
5321     src_rect.x       = src_x;
5322     src_rect.y       = src_y;
5323     src_rect.width   = src_width;
5324     src_rect.height  = src_height;
5325
5326     if (dest_x < 0 ||
5327         dest_y < 0 ||
5328         dest_x + dest_width > obj_surface->orig_width ||
5329         dest_y + dest_height > obj_surface->orig_height)
5330         return VA_STATUS_ERROR_INVALID_PARAMETER;
5331
5332     dst_rect.x      = dest_x;
5333     dst_rect.y      = dest_y;
5334     dst_rect.width  = dest_width;
5335     dst_rect.height = dest_height;
5336
5337     if (HAS_ACCELERATED_PUTIMAGE(i965))
5338         va_status = i965_hw_putimage(ctx, obj_surface, obj_image,
5339                                      &src_rect, &dst_rect);
5340     else
5341         va_status = i965_sw_putimage(ctx, obj_surface, obj_image,
5342                                      &src_rect, &dst_rect);
5343
5344     return va_status;
5345 }
5346
5347 VAStatus
5348 i965_PutSurface(VADriverContextP ctx,
5349                 VASurfaceID surface,
5350                 void *draw, /* X Drawable */
5351                 short srcx,
5352                 short srcy,
5353                 unsigned short srcw,
5354                 unsigned short srch,
5355                 short destx,
5356                 short desty,
5357                 unsigned short destw,
5358                 unsigned short desth,
5359                 VARectangle *cliprects, /* client supplied clip list */
5360                 unsigned int number_cliprects, /* number of clip rects in the clip list */
5361                 unsigned int flags) /* de-interlacing flags */
5362 {
5363 #ifdef HAVE_VA_X11
5364     if (IS_VA_X11(ctx)) {
5365         VARectangle src_rect, dst_rect;
5366
5367         src_rect.x      = srcx;
5368         src_rect.y      = srcy;
5369         src_rect.width  = srcw;
5370         src_rect.height = srch;
5371
5372         dst_rect.x      = destx;
5373         dst_rect.y      = desty;
5374         dst_rect.width  = destw;
5375         dst_rect.height = desth;
5376
5377         return i965_put_surface_dri(ctx, surface, draw, &src_rect, &dst_rect,
5378                                     cliprects, number_cliprects, flags);
5379     }
5380 #endif
5381     return VA_STATUS_ERROR_UNIMPLEMENTED;
5382 }
5383
5384 static VAStatus
5385 i965_BufferInfo(
5386     VADriverContextP ctx,       /* in */
5387     VABufferID buf_id,          /* in */
5388     VABufferType *type,         /* out */
5389     unsigned int *size,         /* out */
5390     unsigned int *num_elements  /* out */
5391 )
5392 {
5393     struct i965_driver_data *i965 = NULL;
5394     struct object_buffer *obj_buffer = NULL;
5395
5396     i965 = i965_driver_data(ctx);
5397     obj_buffer = BUFFER(buf_id);
5398
5399     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
5400
5401     *type = obj_buffer->type;
5402     *size = obj_buffer->size_element;
5403     *num_elements = obj_buffer->num_elements;
5404
5405     return VA_STATUS_SUCCESS;
5406 }
5407
5408 static VAStatus
5409 i965_LockSurface(
5410     VADriverContextP ctx,           /* in */
5411     VASurfaceID surface,            /* in */
5412     unsigned int *fourcc,           /* out */
5413     unsigned int *luma_stride,      /* out */
5414     unsigned int *chroma_u_stride,  /* out */
5415     unsigned int *chroma_v_stride,  /* out */
5416     unsigned int *luma_offset,      /* out */
5417     unsigned int *chroma_u_offset,  /* out */
5418     unsigned int *chroma_v_offset,  /* out */
5419     unsigned int *buffer_name,      /* out */
5420     void **buffer                   /* out */
5421 )
5422 {
5423     VAStatus vaStatus = VA_STATUS_SUCCESS;
5424     struct i965_driver_data *i965 = i965_driver_data(ctx);
5425     struct object_surface *obj_surface = NULL;
5426     VAImage tmpImage;
5427
5428     ASSERT_RET(fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
5429     ASSERT_RET(luma_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5430     ASSERT_RET(chroma_u_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5431     ASSERT_RET(chroma_v_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5432     ASSERT_RET(luma_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5433     ASSERT_RET(chroma_u_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5434     ASSERT_RET(chroma_v_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5435     ASSERT_RET(buffer_name, VA_STATUS_ERROR_INVALID_PARAMETER);
5436     ASSERT_RET(buffer, VA_STATUS_ERROR_INVALID_PARAMETER);
5437
5438     tmpImage.image_id = VA_INVALID_ID;
5439
5440     obj_surface = SURFACE(surface);
5441     if (obj_surface == NULL) {
5442         // Surface is absent.
5443         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5444         goto error;
5445     }
5446
5447     // Lock functionality is absent now.
5448     if (obj_surface->locked_image_id != VA_INVALID_ID) {
5449         // Surface is locked already.
5450         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5451         goto error;
5452     }
5453
5454     vaStatus = i965_DeriveImage(
5455                    ctx,
5456                    surface,
5457                    &tmpImage);
5458     if (vaStatus != VA_STATUS_SUCCESS) {
5459         goto error;
5460     }
5461
5462     obj_surface->locked_image_id = tmpImage.image_id;
5463
5464     vaStatus = i965_MapBuffer(
5465                    ctx,
5466                    tmpImage.buf,
5467                    buffer);
5468     if (vaStatus != VA_STATUS_SUCCESS) {
5469         goto error;
5470     }
5471
5472     *fourcc = tmpImage.format.fourcc;
5473     *luma_offset = tmpImage.offsets[0];
5474     *luma_stride = tmpImage.pitches[0];
5475     *chroma_u_offset = tmpImage.offsets[1];
5476     *chroma_u_stride = tmpImage.pitches[1];
5477     *chroma_v_offset = tmpImage.offsets[2];
5478     *chroma_v_stride = tmpImage.pitches[2];
5479     *buffer_name = tmpImage.buf;
5480
5481 error:
5482     if (vaStatus != VA_STATUS_SUCCESS) {
5483         buffer = NULL;
5484     }
5485
5486     return vaStatus;
5487 }
5488
5489 static VAStatus
5490 i965_UnlockSurface(
5491     VADriverContextP ctx,   /* in */
5492     VASurfaceID surface     /* in */
5493 )
5494 {
5495     VAStatus vaStatus = VA_STATUS_SUCCESS;
5496     struct i965_driver_data *i965 = i965_driver_data(ctx);
5497     struct object_image *locked_img = NULL;
5498     struct object_surface *obj_surface = NULL;
5499
5500     obj_surface = SURFACE(surface);
5501
5502     if (obj_surface == NULL) {
5503         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;   // Surface is absent
5504         return vaStatus;
5505     }
5506     if (obj_surface->locked_image_id == VA_INVALID_ID) {
5507         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;   // Surface is not locked
5508         return vaStatus;
5509     }
5510
5511     locked_img = IMAGE(obj_surface->locked_image_id);
5512     if (locked_img == NULL || (locked_img->image.image_id == VA_INVALID_ID)) {
5513         // Work image was deallocated before i965_UnlockSurface()
5514         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5515         goto error;
5516     }
5517
5518     vaStatus = i965_UnmapBuffer(
5519                    ctx,
5520                    locked_img->image.buf);
5521     if (vaStatus != VA_STATUS_SUCCESS) {
5522         goto error;
5523     }
5524
5525     vaStatus = i965_DestroyImage(
5526                    ctx,
5527                    locked_img->image.image_id);
5528     if (vaStatus != VA_STATUS_SUCCESS) {
5529         goto error;
5530     }
5531
5532     locked_img->image.image_id = VA_INVALID_ID;
5533
5534 error:
5535     obj_surface->locked_image_id = VA_INVALID_ID;
5536
5537     return vaStatus;
5538 }
5539
5540 static VAStatus
5541 i965_GetSurfaceAttributes(
5542     VADriverContextP ctx,
5543     VAConfigID config,
5544     VASurfaceAttrib *attrib_list,
5545     unsigned int num_attribs
5546 )
5547 {
5548     VAStatus vaStatus = VA_STATUS_SUCCESS;
5549     struct i965_driver_data *i965 = i965_driver_data(ctx);
5550     struct object_config *obj_config;
5551     int i;
5552
5553     if (config == VA_INVALID_ID)
5554         return VA_STATUS_ERROR_INVALID_CONFIG;
5555
5556     obj_config = CONFIG(config);
5557
5558     if (obj_config == NULL)
5559         return VA_STATUS_ERROR_INVALID_CONFIG;
5560
5561     if (attrib_list == NULL || num_attribs == 0)
5562         return VA_STATUS_ERROR_INVALID_PARAMETER;
5563
5564     for (i = 0; i < num_attribs; i++) {
5565         switch (attrib_list[i].type) {
5566         case VASurfaceAttribPixelFormat:
5567             attrib_list[i].value.type = VAGenericValueTypeInteger;
5568             attrib_list[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5569
5570             if (attrib_list[i].value.value.i == 0) {
5571                 if (IS_G4X(i965->intel.device_info)) {
5572                     if (obj_config->profile == VAProfileMPEG2Simple ||
5573                         obj_config->profile == VAProfileMPEG2Main) {
5574                         attrib_list[i].value.value.i = VA_FOURCC_I420;
5575                     } else {
5576                         assert(0);
5577                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5578                     }
5579                 } else if (IS_IRONLAKE(i965->intel.device_info)) {
5580                     if (obj_config->profile == VAProfileMPEG2Simple ||
5581                         obj_config->profile == VAProfileMPEG2Main) {
5582                         attrib_list[i].value.value.i = VA_FOURCC_I420;
5583                     } else if (obj_config->profile == VAProfileH264ConstrainedBaseline ||
5584                                obj_config->profile == VAProfileH264Main ||
5585                                obj_config->profile == VAProfileH264High) {
5586                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5587                     } else if (obj_config->profile == VAProfileNone) {
5588                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5589                     } else {
5590                         assert(0);
5591                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5592                     }
5593                 } else if (IS_GEN6(i965->intel.device_info)) {
5594                     attrib_list[i].value.value.i = VA_FOURCC_NV12;
5595                 } else if (IS_GEN7(i965->intel.device_info) ||
5596                            IS_GEN8(i965->intel.device_info) ||
5597                            IS_GEN9(i965->intel.device_info)) {
5598                     if (obj_config->profile == VAProfileJPEGBaseline)
5599                         attrib_list[i].value.value.i = 0; /* internal format */
5600                     else
5601                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5602                 }
5603             } else {
5604                 if (IS_G4X(i965->intel.device_info)) {
5605                     if (obj_config->profile == VAProfileMPEG2Simple ||
5606                         obj_config->profile == VAProfileMPEG2Main) {
5607                         if (attrib_list[i].value.value.i != VA_FOURCC_I420) {
5608                             attrib_list[i].value.value.i = 0;
5609                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5610                         }
5611                     } else {
5612                         assert(0);
5613                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5614                     }
5615                 } else if (IS_IRONLAKE(i965->intel.device_info)) {
5616                     if (obj_config->profile == VAProfileMPEG2Simple ||
5617                         obj_config->profile == VAProfileMPEG2Main) {
5618                         if (attrib_list[i].value.value.i != VA_FOURCC_I420) {
5619                             attrib_list[i].value.value.i = 0;
5620                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5621                         }
5622                     } else if (obj_config->profile == VAProfileH264ConstrainedBaseline ||
5623                                obj_config->profile == VAProfileH264Main ||
5624                                obj_config->profile == VAProfileH264High) {
5625                         if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5626                             attrib_list[i].value.value.i = 0;
5627                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5628                         }
5629                     } else if (obj_config->profile == VAProfileNone) {
5630                         switch (attrib_list[i].value.value.i) {
5631                         case VA_FOURCC_NV12:
5632                         case VA_FOURCC_I420:
5633                         case VA_FOURCC_YV12:
5634                         case VA_FOURCC_YUY2:
5635                         case VA_FOURCC_BGRA:
5636                         case VA_FOURCC_BGRX:
5637                         case VA_FOURCC_RGBX:
5638                         case VA_FOURCC_RGBA:
5639                             break;
5640                         default:
5641                             attrib_list[i].value.value.i = 0;
5642                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5643                             break;
5644                         }
5645                     } else {
5646                         assert(0);
5647                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5648                     }
5649                 } else if (IS_GEN6(i965->intel.device_info)) {
5650                     if (obj_config->entrypoint == VAEntrypointEncSlice ||
5651                         obj_config->entrypoint == VAEntrypointVideoProc) {
5652                         switch (attrib_list[i].value.value.i) {
5653                         case VA_FOURCC_NV12:
5654                         case VA_FOURCC_I420:
5655                         case VA_FOURCC_YV12:
5656                         case VA_FOURCC_YUY2:
5657                         case VA_FOURCC_BGRA:
5658                         case VA_FOURCC_BGRX:
5659                         case VA_FOURCC_RGBX:
5660                         case VA_FOURCC_RGBA:
5661                             break;
5662                         default:
5663                             attrib_list[i].value.value.i = 0;
5664                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5665                             break;
5666                         }
5667                     } else {
5668                         if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5669                             attrib_list[i].value.value.i = 0;
5670                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5671                         }
5672                     }
5673                 } else if (IS_GEN7(i965->intel.device_info) ||
5674                            IS_GEN8(i965->intel.device_info) ||
5675                            IS_GEN9(i965->intel.device_info)) {
5676                     if (obj_config->entrypoint == VAEntrypointEncSlice ||
5677                         obj_config->entrypoint == VAEntrypointVideoProc ||
5678                         obj_config->entrypoint == VAEntrypointEncSliceLP) {
5679                         switch (attrib_list[i].value.value.i) {
5680                         case VA_FOURCC_NV12:
5681                         case VA_FOURCC_I420:
5682                         case VA_FOURCC_YV12:
5683                             break;
5684                         default:
5685                             attrib_list[i].value.value.i = 0;
5686                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5687                             break;
5688                         }
5689                     } else {
5690                         if (obj_config->profile == VAProfileJPEGBaseline) {
5691                             attrib_list[i].value.value.i = 0;   /* JPEG decoding always uses an internal format */
5692                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5693                         } else {
5694                             if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5695                                 attrib_list[i].value.value.i = 0;
5696                                 attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5697                             }
5698                         }
5699                     }
5700                 }
5701             }
5702
5703             break;
5704         case VASurfaceAttribMinWidth:
5705             /* FIXME: add support for it later */
5706             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5707             break;
5708         case VASurfaceAttribMaxWidth:
5709             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5710             break;
5711         case VASurfaceAttribMinHeight:
5712             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5713             break;
5714         case VASurfaceAttribMaxHeight:
5715             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5716             break;
5717         default:
5718             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5719             break;
5720         }
5721     }
5722
5723     return vaStatus;
5724 }
5725
5726 static VAStatus
5727 i965_QuerySurfaceAttributes(VADriverContextP ctx,
5728                             VAConfigID config,
5729                             VASurfaceAttrib *attrib_list,
5730                             unsigned int *num_attribs)
5731 {
5732     VAStatus vaStatus = VA_STATUS_SUCCESS;
5733     struct i965_driver_data *i965 = i965_driver_data(ctx);
5734     struct object_config *obj_config;
5735     int i = 0;
5736     VASurfaceAttrib *attribs = NULL;
5737     int max_width;
5738     int max_height;
5739
5740     if (config == VA_INVALID_ID)
5741         return VA_STATUS_ERROR_INVALID_CONFIG;
5742
5743     obj_config = CONFIG(config);
5744
5745     if (obj_config == NULL)
5746         return VA_STATUS_ERROR_INVALID_CONFIG;
5747
5748     if (!attrib_list && !num_attribs)
5749         return VA_STATUS_ERROR_INVALID_PARAMETER;
5750
5751     if (attrib_list == NULL) {
5752         *num_attribs = I965_MAX_SURFACE_ATTRIBUTES;
5753         return VA_STATUS_SUCCESS;
5754     }
5755
5756     attribs = malloc(I965_MAX_SURFACE_ATTRIBUTES * sizeof(*attribs));
5757
5758     if (attribs == NULL)
5759         return VA_STATUS_ERROR_ALLOCATION_FAILED;
5760
5761     if (IS_G4X(i965->intel.device_info)) {
5762         if (obj_config->profile == VAProfileMPEG2Simple ||
5763             obj_config->profile == VAProfileMPEG2Main) {
5764             attribs[i].type = VASurfaceAttribPixelFormat;
5765             attribs[i].value.type = VAGenericValueTypeInteger;
5766             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5767             attribs[i].value.value.i = VA_FOURCC_I420;
5768             i++;
5769         }
5770     } else if (IS_IRONLAKE(i965->intel.device_info)) {
5771         switch (obj_config->profile) {
5772         case VAProfileMPEG2Simple:
5773         case VAProfileMPEG2Main:
5774             attribs[i].type = VASurfaceAttribPixelFormat;
5775             attribs[i].value.type = VAGenericValueTypeInteger;
5776             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5777             attribs[i].value.value.i = VA_FOURCC_I420;
5778             i++;
5779
5780             break;
5781
5782         case VAProfileH264ConstrainedBaseline:
5783         case VAProfileH264Main:
5784         case VAProfileH264High:
5785             attribs[i].type = VASurfaceAttribPixelFormat;
5786             attribs[i].value.type = VAGenericValueTypeInteger;
5787             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5788             attribs[i].value.value.i = VA_FOURCC_NV12;
5789             i++;
5790
5791         case VAProfileNone:
5792             attribs[i].type = VASurfaceAttribPixelFormat;
5793             attribs[i].value.type = VAGenericValueTypeInteger;
5794             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5795             attribs[i].value.value.i = VA_FOURCC_NV12;
5796             i++;
5797
5798             attribs[i].type = VASurfaceAttribPixelFormat;
5799             attribs[i].value.type = VAGenericValueTypeInteger;
5800             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5801             attribs[i].value.value.i = VA_FOURCC_I420;
5802             i++;
5803
5804             break;
5805
5806         default:
5807             break;
5808         }
5809     } else if (IS_GEN6(i965->intel.device_info)) {
5810         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
5811             attribs[i].type = VASurfaceAttribPixelFormat;
5812             attribs[i].value.type = VAGenericValueTypeInteger;
5813             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5814             attribs[i].value.value.i = VA_FOURCC_NV12;
5815             i++;
5816         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
5817                    obj_config->entrypoint == VAEntrypointVideoProc) { /* vpp */
5818             attribs[i].type = VASurfaceAttribPixelFormat;
5819             attribs[i].value.type = VAGenericValueTypeInteger;
5820             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5821             attribs[i].value.value.i = VA_FOURCC_NV12;
5822             i++;
5823
5824             attribs[i].type = VASurfaceAttribPixelFormat;
5825             attribs[i].value.type = VAGenericValueTypeInteger;
5826             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5827             attribs[i].value.value.i = VA_FOURCC_I420;
5828             i++;
5829
5830             attribs[i].type = VASurfaceAttribPixelFormat;
5831             attribs[i].value.type = VAGenericValueTypeInteger;
5832             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5833             attribs[i].value.value.i = VA_FOURCC_YV12;
5834             i++;
5835
5836             if (obj_config->entrypoint == VAEntrypointVideoProc) {
5837                 attribs[i].type = VASurfaceAttribPixelFormat;
5838                 attribs[i].value.type = VAGenericValueTypeInteger;
5839                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5840                 attribs[i].value.value.i = VA_FOURCC_YUY2;
5841                 i++;
5842
5843                 attribs[i].type = VASurfaceAttribPixelFormat;
5844                 attribs[i].value.type = VAGenericValueTypeInteger;
5845                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5846                 attribs[i].value.value.i = VA_FOURCC_RGBA;
5847                 i++;
5848
5849                 attribs[i].type = VASurfaceAttribPixelFormat;
5850                 attribs[i].value.type = VAGenericValueTypeInteger;
5851                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5852                 attribs[i].value.value.i = VA_FOURCC_RGBX;
5853                 i++;
5854             }
5855         }
5856     } else if (IS_GEN7(i965->intel.device_info)) {
5857         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
5858             if (obj_config->profile == VAProfileJPEGBaseline) {
5859                 attribs[i].type = VASurfaceAttribPixelFormat;
5860                 attribs[i].value.type = VAGenericValueTypeInteger;
5861                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5862                 attribs[i].value.value.i = VA_FOURCC_IMC3;
5863                 i++;
5864
5865                 attribs[i].type = VASurfaceAttribPixelFormat;
5866                 attribs[i].value.type = VAGenericValueTypeInteger;
5867                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5868                 attribs[i].value.value.i = VA_FOURCC_IMC1;
5869                 i++;
5870
5871                 attribs[i].type = VASurfaceAttribPixelFormat;
5872                 attribs[i].value.type = VAGenericValueTypeInteger;
5873                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5874                 attribs[i].value.value.i = VA_FOURCC_Y800;
5875                 i++;
5876
5877                 attribs[i].type = VASurfaceAttribPixelFormat;
5878                 attribs[i].value.type = VAGenericValueTypeInteger;
5879                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5880                 attribs[i].value.value.i = VA_FOURCC_411P;
5881                 i++;
5882
5883                 attribs[i].type = VASurfaceAttribPixelFormat;
5884                 attribs[i].value.type = VAGenericValueTypeInteger;
5885                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5886                 attribs[i].value.value.i = VA_FOURCC_422H;
5887                 i++;
5888
5889                 attribs[i].type = VASurfaceAttribPixelFormat;
5890                 attribs[i].value.type = VAGenericValueTypeInteger;
5891                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5892                 attribs[i].value.value.i = VA_FOURCC_422V;
5893                 i++;
5894
5895                 attribs[i].type = VASurfaceAttribPixelFormat;
5896                 attribs[i].value.type = VAGenericValueTypeInteger;
5897                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5898                 attribs[i].value.value.i = VA_FOURCC_444P;
5899                 i++;
5900             } else if (obj_config->profile == VAProfileHEVCMain10) {
5901                 attribs[i].type = VASurfaceAttribPixelFormat;
5902                 attribs[i].value.type = VAGenericValueTypeInteger;
5903                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5904                 attribs[i].value.value.i = VA_FOURCC_P010;
5905                 i++;
5906             } else {
5907                 attribs[i].type = VASurfaceAttribPixelFormat;
5908                 attribs[i].value.type = VAGenericValueTypeInteger;
5909                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5910                 attribs[i].value.value.i = VA_FOURCC_NV12;
5911                 i++;
5912             }
5913         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
5914                    obj_config->entrypoint == VAEntrypointVideoProc) { /* vpp */
5915             attribs[i].type = VASurfaceAttribPixelFormat;
5916             attribs[i].value.type = VAGenericValueTypeInteger;
5917             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5918             attribs[i].value.value.i = VA_FOURCC_NV12;
5919             i++;
5920
5921             attribs[i].type = VASurfaceAttribPixelFormat;
5922             attribs[i].value.type = VAGenericValueTypeInteger;
5923             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5924             attribs[i].value.value.i = VA_FOURCC_I420;
5925             i++;
5926
5927             attribs[i].type = VASurfaceAttribPixelFormat;
5928             attribs[i].value.type = VAGenericValueTypeInteger;
5929             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5930             attribs[i].value.value.i = VA_FOURCC_YV12;
5931             i++;
5932
5933             attribs[i].type = VASurfaceAttribPixelFormat;
5934             attribs[i].value.type = VAGenericValueTypeInteger;
5935             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5936             attribs[i].value.value.i = VA_FOURCC_IMC3;
5937             i++;
5938
5939             if (obj_config->entrypoint == VAEntrypointVideoProc) {
5940                 attribs[i].type = VASurfaceAttribPixelFormat;
5941                 attribs[i].value.type = VAGenericValueTypeInteger;
5942                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5943                 attribs[i].value.value.i = VA_FOURCC_YUY2;
5944                 i++;
5945
5946                 attribs[i].type = VASurfaceAttribPixelFormat;
5947                 attribs[i].value.type = VAGenericValueTypeInteger;
5948                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5949                 attribs[i].value.value.i = VA_FOURCC_RGBA;
5950                 i++;
5951
5952                 attribs[i].type = VASurfaceAttribPixelFormat;
5953                 attribs[i].value.type = VAGenericValueTypeInteger;
5954                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5955                 attribs[i].value.value.i = VA_FOURCC_RGBX;
5956                 i++;
5957
5958                 attribs[i].type = VASurfaceAttribPixelFormat;
5959                 attribs[i].value.type = VAGenericValueTypeInteger;
5960                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5961                 attribs[i].value.value.i = VA_FOURCC_BGRA;
5962                 i++;
5963
5964                 attribs[i].type = VASurfaceAttribPixelFormat;
5965                 attribs[i].value.type = VAGenericValueTypeInteger;
5966                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5967                 attribs[i].value.value.i = VA_FOURCC_BGRX;
5968                 i++;
5969
5970                 attribs[i].type = VASurfaceAttribPixelFormat;
5971                 attribs[i].value.type = VAGenericValueTypeInteger;
5972                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5973                 attribs[i].value.value.i = VA_FOURCC_YV16;
5974                 i++;
5975             }
5976         }
5977     } else if (IS_GEN8(i965->intel.device_info) ||
5978                IS_GEN9(i965->intel.device_info)) {
5979         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
5980             if (obj_config->profile == VAProfileJPEGBaseline) {
5981                 attribs[i].type = VASurfaceAttribPixelFormat;
5982                 attribs[i].value.type = VAGenericValueTypeInteger;
5983                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5984                 attribs[i].value.value.i = VA_FOURCC_IMC3;
5985                 i++;
5986
5987                 attribs[i].type = VASurfaceAttribPixelFormat;
5988                 attribs[i].value.type = VAGenericValueTypeInteger;
5989                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5990                 attribs[i].value.value.i = VA_FOURCC_IMC1;
5991                 i++;
5992
5993                 attribs[i].type = VASurfaceAttribPixelFormat;
5994                 attribs[i].value.type = VAGenericValueTypeInteger;
5995                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5996                 attribs[i].value.value.i = VA_FOURCC_Y800;
5997                 i++;
5998
5999                 attribs[i].type = VASurfaceAttribPixelFormat;
6000                 attribs[i].value.type = VAGenericValueTypeInteger;
6001                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6002                 attribs[i].value.value.i = VA_FOURCC_411P;
6003                 i++;
6004
6005                 attribs[i].type = VASurfaceAttribPixelFormat;
6006                 attribs[i].value.type = VAGenericValueTypeInteger;
6007                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6008                 attribs[i].value.value.i = VA_FOURCC_422H;
6009                 i++;
6010
6011                 attribs[i].type = VASurfaceAttribPixelFormat;
6012                 attribs[i].value.type = VAGenericValueTypeInteger;
6013                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6014                 attribs[i].value.value.i = VA_FOURCC_422V;
6015                 i++;
6016
6017                 attribs[i].type = VASurfaceAttribPixelFormat;
6018                 attribs[i].value.type = VAGenericValueTypeInteger;
6019                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6020                 attribs[i].value.value.i = VA_FOURCC_444P;
6021                 i++;
6022             } else {
6023                 attribs[i].type = VASurfaceAttribPixelFormat;
6024                 attribs[i].value.type = VAGenericValueTypeInteger;
6025                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6026                 attribs[i].value.value.i = VA_FOURCC_NV12;
6027                 i++;
6028
6029                 if ((obj_config->profile == VAProfileHEVCMain10) ||
6030                     (obj_config->profile == VAProfileVP9Profile2)) {
6031                     attribs[i].type = VASurfaceAttribPixelFormat;
6032                     attribs[i].value.type = VAGenericValueTypeInteger;
6033                     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6034                     attribs[i].value.value.i = VA_FOURCC_P010;
6035                     i++;
6036                 }
6037             }
6038         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
6039                    obj_config->entrypoint == VAEntrypointVideoProc ||
6040                    obj_config->entrypoint == VAEntrypointEncSliceLP ||
6041                    obj_config->entrypoint == VAEntrypointEncPicture) {
6042
6043             if (obj_config->profile == VAProfileHEVCMain10) {
6044                 attribs[i].type = VASurfaceAttribPixelFormat;
6045                 attribs[i].value.type = VAGenericValueTypeInteger;
6046                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6047                 attribs[i].value.value.i = VA_FOURCC_P010;
6048                 i++;
6049             } else {
6050                 attribs[i].type = VASurfaceAttribPixelFormat;
6051                 attribs[i].value.type = VAGenericValueTypeInteger;
6052                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6053                 attribs[i].value.value.i = VA_FOURCC_NV12;
6054                 i++;
6055
6056                 attribs[i].type = VASurfaceAttribPixelFormat;
6057                 attribs[i].value.type = VAGenericValueTypeInteger;
6058                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6059                 attribs[i].value.value.i = VA_FOURCC_I420;
6060                 i++;
6061
6062                 attribs[i].type = VASurfaceAttribPixelFormat;
6063                 attribs[i].value.type = VAGenericValueTypeInteger;
6064                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6065                 attribs[i].value.value.i = VA_FOURCC_YV12;
6066                 i++;
6067
6068                 attribs[i].type = VASurfaceAttribPixelFormat;
6069                 attribs[i].value.type = VAGenericValueTypeInteger;
6070                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6071                 attribs[i].value.value.i = VA_FOURCC_IMC3;
6072                 i++;
6073             }
6074
6075             if (obj_config->entrypoint == VAEntrypointVideoProc) {
6076                 attribs[i].type = VASurfaceAttribPixelFormat;
6077                 attribs[i].value.type = VAGenericValueTypeInteger;
6078                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6079                 attribs[i].value.value.i = VA_FOURCC_YUY2;
6080                 i++;
6081
6082                 attribs[i].type = VASurfaceAttribPixelFormat;
6083                 attribs[i].value.type = VAGenericValueTypeInteger;
6084                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6085                 attribs[i].value.value.i = VA_FOURCC_RGBA;
6086                 i++;
6087
6088                 attribs[i].type = VASurfaceAttribPixelFormat;
6089                 attribs[i].value.type = VAGenericValueTypeInteger;
6090                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6091                 attribs[i].value.value.i = VA_FOURCC_RGBX;
6092                 i++;
6093
6094                 attribs[i].type = VASurfaceAttribPixelFormat;
6095                 attribs[i].value.type = VAGenericValueTypeInteger;
6096                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6097                 attribs[i].value.value.i = VA_FOURCC_BGRA;
6098                 i++;
6099
6100                 attribs[i].type = VASurfaceAttribPixelFormat;
6101                 attribs[i].value.type = VAGenericValueTypeInteger;
6102                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6103                 attribs[i].value.value.i = VA_FOURCC_BGRX;
6104                 i++;
6105
6106                 attribs[i].type = VASurfaceAttribPixelFormat;
6107                 attribs[i].value.type = VAGenericValueTypeInteger;
6108                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6109                 attribs[i].value.value.i = VA_FOURCC_YV16;
6110                 i++;
6111
6112                 if (HAS_VPP_P010(i965)) {
6113                     attribs[i].type = VASurfaceAttribPixelFormat;
6114                     attribs[i].value.type = VAGenericValueTypeInteger;
6115                     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6116                     attribs[i].value.value.i = VA_FOURCC_P010;
6117                     i++;
6118
6119                     attribs[i].type = VASurfaceAttribPixelFormat;
6120                     attribs[i].value.type = VAGenericValueTypeInteger;
6121                     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6122                     attribs[i].value.value.i = VA_FOURCC_I010;
6123                     i++;
6124                 }
6125             }
6126
6127             /* Additional support for jpeg encoder */
6128             if (obj_config->profile == VAProfileJPEGBaseline
6129                 && obj_config->entrypoint == VAEntrypointEncPicture) {
6130                 attribs[i].type = VASurfaceAttribPixelFormat;
6131                 attribs[i].value.type = VAGenericValueTypeInteger;
6132                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6133                 attribs[i].value.value.i = VA_FOURCC_YUY2;
6134                 i++;
6135
6136                 attribs[i].type = VASurfaceAttribPixelFormat;
6137                 attribs[i].value.type = VAGenericValueTypeInteger;
6138                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6139                 attribs[i].value.value.i = VA_FOURCC_UYVY;
6140                 i++;
6141
6142                 attribs[i].type = VASurfaceAttribPixelFormat;
6143                 attribs[i].value.type = VAGenericValueTypeInteger;
6144                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6145                 attribs[i].value.value.i = VA_FOURCC_YV16;
6146                 i++;
6147
6148                 attribs[i].type = VASurfaceAttribPixelFormat;
6149                 attribs[i].value.type = VAGenericValueTypeInteger;
6150                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6151                 attribs[i].value.value.i = VA_FOURCC_Y800;
6152                 i++;
6153             }
6154         }
6155     }
6156
6157     attribs[i].type = VASurfaceAttribMemoryType;
6158     attribs[i].value.type = VAGenericValueTypeInteger;
6159     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6160     attribs[i].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA |
6161                                VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM |
6162                                VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
6163     i++;
6164
6165     attribs[i].type = VASurfaceAttribExternalBufferDescriptor;
6166     attribs[i].value.type = VAGenericValueTypePointer;
6167     attribs[i].flags = VA_SURFACE_ATTRIB_SETTABLE;
6168     attribs[i].value.value.p = NULL; /* ignore */
6169     i++;
6170
6171     max_resolution(i965, obj_config, &max_width, &max_height);
6172
6173     attribs[i].type = VASurfaceAttribMaxWidth;
6174     attribs[i].value.type = VAGenericValueTypeInteger;
6175     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
6176     attribs[i].value.value.i = max_width;
6177     i++;
6178
6179     attribs[i].type = VASurfaceAttribMaxHeight;
6180     attribs[i].value.type = VAGenericValueTypeInteger;
6181     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
6182     attribs[i].value.value.i = max_height;
6183     i++;
6184
6185     if (i > *num_attribs) {
6186         *num_attribs = i;
6187         free(attribs);
6188         return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6189     }
6190
6191     *num_attribs = i;
6192     memcpy(attrib_list, attribs, i * sizeof(*attribs));
6193     free(attribs);
6194
6195     return vaStatus;
6196 }
6197
6198 /* Acquires buffer handle for external API usage (internal implementation) */
6199 static VAStatus
6200 i965_acquire_buffer_handle(struct object_buffer *obj_buffer,
6201                            uint32_t mem_type, VABufferInfo *out_buf_info)
6202 {
6203     struct buffer_store *buffer_store;
6204
6205     buffer_store = obj_buffer->buffer_store;
6206     if (!buffer_store || !buffer_store->bo)
6207         return VA_STATUS_ERROR_INVALID_BUFFER;
6208
6209     /* Synchronization point */
6210     drm_intel_bo_wait_rendering(buffer_store->bo);
6211
6212     if (obj_buffer->export_refcount > 0) {
6213         if (obj_buffer->export_state.mem_type != mem_type)
6214             return VA_STATUS_ERROR_INVALID_PARAMETER;
6215     } else {
6216         VABufferInfo * const buf_info = &obj_buffer->export_state;
6217
6218         switch (mem_type) {
6219         case VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM: {
6220             uint32_t name;
6221             if (drm_intel_bo_flink(buffer_store->bo, &name) != 0)
6222                 return VA_STATUS_ERROR_INVALID_BUFFER;
6223             buf_info->handle = name;
6224             break;
6225         }
6226         case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: {
6227             int fd;
6228             if (drm_intel_bo_gem_export_to_prime(buffer_store->bo, &fd) != 0)
6229                 return VA_STATUS_ERROR_INVALID_BUFFER;
6230             buf_info->handle = (intptr_t)fd;
6231             break;
6232         }
6233         }
6234
6235         buf_info->type = obj_buffer->type;
6236         buf_info->mem_type = mem_type;
6237         buf_info->mem_size =
6238             obj_buffer->num_elements * obj_buffer->size_element;
6239     }
6240
6241     obj_buffer->export_refcount++;
6242     *out_buf_info = obj_buffer->export_state;
6243     return VA_STATUS_SUCCESS;
6244 }
6245
6246 /* Releases buffer handle after usage (internal implementation) */
6247 static VAStatus
6248 i965_release_buffer_handle(struct object_buffer *obj_buffer)
6249 {
6250     if (obj_buffer->export_refcount == 0)
6251         return VA_STATUS_ERROR_INVALID_BUFFER;
6252
6253     if (--obj_buffer->export_refcount == 0) {
6254         VABufferInfo * const buf_info = &obj_buffer->export_state;
6255
6256         switch (buf_info->mem_type) {
6257         case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: {
6258             close((intptr_t)buf_info->handle);
6259             break;
6260         }
6261         }
6262         buf_info->mem_type = 0;
6263     }
6264     return VA_STATUS_SUCCESS;
6265 }
6266
6267 /** Acquires buffer handle for external API usage */
6268 static VAStatus
6269 i965_AcquireBufferHandle(VADriverContextP ctx, VABufferID buf_id,
6270                          VABufferInfo *buf_info)
6271 {
6272     struct i965_driver_data * const i965 = i965_driver_data(ctx);
6273     struct object_buffer * const obj_buffer = BUFFER(buf_id);
6274     uint32_t i, mem_type;
6275
6276     /* List of supported memory types, in preferred order */
6277     static const uint32_t mem_types[] = {
6278         VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
6279         VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM,
6280         0
6281     };
6282
6283     if (!obj_buffer)
6284         return VA_STATUS_ERROR_INVALID_BUFFER;
6285     /* XXX: only VA surface|image like buffers are supported for now */
6286     if (obj_buffer->type != VAImageBufferType)
6287         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
6288
6289     /*
6290      * As the allocated buffer by calling vaCreateBuffer is related with
6291      * the specific context, it is unnecessary to export it.
6292      * So it is not supported when the buffer is allocated from wrapped
6293      * backend dirver.
6294      */
6295     if (obj_buffer->wrapper_buffer != VA_INVALID_ID) {
6296         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
6297     }
6298
6299     if (!buf_info)
6300         return VA_STATUS_ERROR_INVALID_PARAMETER;
6301
6302     if (!buf_info->mem_type)
6303         mem_type = mem_types[0];
6304     else {
6305         mem_type = 0;
6306         for (i = 0; mem_types[i] != 0; i++) {
6307             if (buf_info->mem_type & mem_types[i]) {
6308                 mem_type = buf_info->mem_type;
6309                 break;
6310             }
6311         }
6312         if (!mem_type)
6313             return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
6314     }
6315     return i965_acquire_buffer_handle(obj_buffer, mem_type, buf_info);
6316 }
6317
6318 /** Releases buffer handle after usage from external API */
6319 static VAStatus
6320 i965_ReleaseBufferHandle(VADriverContextP ctx, VABufferID buf_id)
6321 {
6322     struct i965_driver_data * const i965 = i965_driver_data(ctx);
6323     struct object_buffer * const obj_buffer = BUFFER(buf_id);
6324
6325     if (!obj_buffer)
6326         return VA_STATUS_ERROR_INVALID_BUFFER;
6327
6328     if (obj_buffer->wrapper_buffer != VA_INVALID_ID) {
6329         return VA_STATUS_ERROR_INVALID_BUFFER;
6330     }
6331
6332     return i965_release_buffer_handle(obj_buffer);
6333 }
6334
6335 static int
6336 i965_os_has_ring_support(VADriverContextP ctx,
6337                          int ring)
6338 {
6339     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6340
6341     switch (ring) {
6342     case I965_RING_BSD:
6343         return i965->intel.has_bsd;
6344
6345     case I965_RING_BLT:
6346         return i965->intel.has_blt;
6347
6348     case I965_RING_VEBOX:
6349         return i965->intel.has_vebox;
6350
6351     case I965_RING_NULL:
6352         return 1; /* Always support */
6353
6354     default:
6355         /* should never get here */
6356         assert(0);
6357         break;
6358     }
6359
6360     return 0;
6361 }
6362
6363 /*
6364  * Query video processing pipeline
6365  */
6366 VAStatus i965_QueryVideoProcFilters(
6367     VADriverContextP    ctx,
6368     VAContextID         context,
6369     VAProcFilterType   *filters,
6370     unsigned int       *num_filters
6371 )
6372 {
6373     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6374     unsigned int i = 0, num = 0;
6375
6376     if (!num_filters  || !filters)
6377         return VA_STATUS_ERROR_INVALID_PARAMETER;
6378
6379     for (i = 0; i < i965->codec_info->num_filters; i++) {
6380         if (i965_os_has_ring_support(ctx, i965->codec_info->filters[i].ring)) {
6381             if (num == *num_filters) {
6382                 *num_filters = i965->codec_info->num_filters;
6383
6384                 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6385             }
6386
6387             filters[num++] = i965->codec_info->filters[i].type;
6388         }
6389     }
6390
6391     *num_filters = num;
6392
6393     return VA_STATUS_SUCCESS;
6394 }
6395
6396 VAStatus i965_QueryVideoProcFilterCaps(
6397     VADriverContextP    ctx,
6398     VAContextID         context,
6399     VAProcFilterType    type,
6400     void               *filter_caps,
6401     unsigned int       *num_filter_caps
6402 )
6403 {
6404     unsigned int i = 0;
6405     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6406
6407     if (!filter_caps || !num_filter_caps)
6408         return VA_STATUS_ERROR_INVALID_PARAMETER;
6409
6410     for (i = 0; i < i965->codec_info->num_filters; i++) {
6411         if (type == i965->codec_info->filters[i].type &&
6412             i965_os_has_ring_support(ctx, i965->codec_info->filters[i].ring))
6413             break;
6414     }
6415
6416     if (i == i965->codec_info->num_filters)
6417         return VA_STATUS_ERROR_UNSUPPORTED_FILTER;
6418
6419     i = 0;
6420
6421     switch (type) {
6422     case VAProcFilterNoiseReduction:
6423     case VAProcFilterSharpening: {
6424         VAProcFilterCap *cap = filter_caps;
6425
6426         if (*num_filter_caps < 1) {
6427             *num_filter_caps = 1;
6428             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6429         }
6430
6431         cap->range.min_value = 0.0;
6432         cap->range.max_value = 1.0;
6433         cap->range.default_value = 0.5;
6434         cap->range.step = 0.03125; /* 1.0 / 32 */
6435         i++;
6436     }
6437
6438     break;
6439
6440     case VAProcFilterDeinterlacing: {
6441         VAProcFilterCapDeinterlacing *cap = filter_caps;
6442
6443         if (*num_filter_caps < VAProcDeinterlacingCount) {
6444             *num_filter_caps = VAProcDeinterlacingCount;
6445             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6446         }
6447
6448         cap->type = VAProcDeinterlacingBob;
6449         i++;
6450         cap++;
6451
6452
6453         if (i965->codec_info->has_di_motion_adptive) {
6454             cap->type = VAProcDeinterlacingMotionAdaptive;
6455             i++;
6456             cap++;
6457         }
6458
6459         if (i965->codec_info->has_di_motion_compensated) {
6460             cap->type = VAProcDeinterlacingMotionCompensated;
6461             i++;
6462             cap++;
6463         }
6464     }
6465
6466     break;
6467
6468     case VAProcFilterColorBalance: {
6469         VAProcFilterCapColorBalance *cap = filter_caps;
6470
6471         if (*num_filter_caps < VAProcColorBalanceCount) {
6472             *num_filter_caps = VAProcColorBalanceCount;
6473             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6474         }
6475
6476         cap->type = VAProcColorBalanceHue;
6477         cap->range.min_value = -180.0;
6478         cap->range.max_value = 180.0;
6479         cap->range.default_value = 0.0;
6480         cap->range.step = 1.0;
6481         i++;
6482         cap++;
6483
6484         cap->type = VAProcColorBalanceSaturation;
6485         cap->range.min_value = 0.0;
6486         cap->range.max_value = 10.0;
6487         cap->range.default_value = 1.0;
6488         cap->range.step = 0.1;
6489         i++;
6490         cap++;
6491
6492         cap->type = VAProcColorBalanceBrightness;
6493         cap->range.min_value = -100.0;
6494         cap->range.max_value = 100.0;
6495         cap->range.default_value = 0.0;
6496         cap->range.step = 1.0;
6497         i++;
6498         cap++;
6499
6500         cap->type = VAProcColorBalanceContrast;
6501         cap->range.min_value = 0.0;
6502         cap->range.max_value = 10.0;
6503         cap->range.default_value = 1.0;
6504         cap->range.step = 0.1;
6505         i++;
6506         cap++;
6507     }
6508
6509     break;
6510
6511     default:
6512
6513         break;
6514     }
6515
6516     *num_filter_caps = i;
6517
6518     return VA_STATUS_SUCCESS;
6519 }
6520
6521 static VAProcColorStandardType vpp_input_color_standards[VAProcColorStandardCount] = {
6522     VAProcColorStandardBT601,
6523 };
6524
6525 static VAProcColorStandardType vpp_output_color_standards[VAProcColorStandardCount] = {
6526     VAProcColorStandardBT601,
6527 };
6528
6529 VAStatus i965_QueryVideoProcPipelineCaps(
6530     VADriverContextP ctx,
6531     VAContextID context,
6532     VABufferID *filters,
6533     unsigned int num_filters,
6534     VAProcPipelineCaps *pipeline_cap     /* out */
6535 )
6536 {
6537     struct i965_driver_data * const i965 = i965_driver_data(ctx);
6538     unsigned int i = 0;
6539
6540     pipeline_cap->pipeline_flags = 0;
6541     pipeline_cap->filter_flags = 0;
6542     pipeline_cap->num_forward_references = 0;
6543     pipeline_cap->num_backward_references = 0;
6544     pipeline_cap->num_input_color_standards = 1;
6545     pipeline_cap->input_color_standards = vpp_input_color_standards;
6546     pipeline_cap->num_output_color_standards = 1;
6547     pipeline_cap->output_color_standards = vpp_output_color_standards;
6548
6549     for (i = 0; i < num_filters; i++) {
6550         struct object_buffer *obj_buffer = BUFFER(filters[i]);
6551
6552         if (!obj_buffer ||
6553             !obj_buffer->buffer_store ||
6554             !obj_buffer->buffer_store->buffer)
6555             return VA_STATUS_ERROR_INVALID_BUFFER;
6556
6557         VAProcFilterParameterBufferBase *base = (VAProcFilterParameterBufferBase *)obj_buffer->buffer_store->buffer;
6558
6559         if (base->type == VAProcFilterNoiseReduction) {
6560             VAProcFilterParameterBuffer *denoise = (VAProcFilterParameterBuffer *)base;
6561             (void)denoise;
6562         } else if (base->type == VAProcFilterDeinterlacing) {
6563             VAProcFilterParameterBufferDeinterlacing *deint = (VAProcFilterParameterBufferDeinterlacing *)base;
6564
6565             ASSERT_RET(deint->algorithm == VAProcDeinterlacingBob ||
6566                        deint->algorithm == VAProcDeinterlacingMotionAdaptive ||
6567                        deint->algorithm == VAProcDeinterlacingMotionCompensated,
6568                        VA_STATUS_ERROR_INVALID_PARAMETER);
6569
6570             if (deint->algorithm == VAProcDeinterlacingMotionAdaptive ||
6571                 deint->algorithm == VAProcDeinterlacingMotionCompensated)
6572                 pipeline_cap->num_forward_references++;
6573         } else if (base->type == VAProcFilterSkinToneEnhancement) {
6574             VAProcFilterParameterBuffer *stde = (VAProcFilterParameterBuffer *)base;
6575             (void)stde;
6576         }
6577     }
6578
6579     return VA_STATUS_SUCCESS;
6580 }
6581
6582 extern struct hw_codec_info *i965_get_codec_info(int devid);
6583
6584 static bool
6585 i965_driver_data_init(VADriverContextP ctx)
6586 {
6587     struct i965_driver_data *i965 = i965_driver_data(ctx);
6588
6589     i965->codec_info = i965_get_codec_info(i965->intel.device_id);
6590
6591     if (!i965->codec_info)
6592         return false;
6593
6594     if (object_heap_init(&i965->config_heap,
6595                          sizeof(struct object_config),
6596                          CONFIG_ID_OFFSET))
6597         goto err_config_heap;
6598     if (object_heap_init(&i965->context_heap,
6599                          sizeof(struct object_context),
6600                          CONTEXT_ID_OFFSET))
6601         goto err_context_heap;
6602
6603     if (object_heap_init(&i965->surface_heap,
6604                          sizeof(struct object_surface),
6605                          SURFACE_ID_OFFSET))
6606         goto err_surface_heap;
6607     if (object_heap_init(&i965->buffer_heap,
6608                          sizeof(struct object_buffer),
6609                          BUFFER_ID_OFFSET))
6610         goto err_buffer_heap;
6611     if (object_heap_init(&i965->image_heap,
6612                          sizeof(struct object_image),
6613                          IMAGE_ID_OFFSET))
6614         goto err_image_heap;
6615     if (object_heap_init(&i965->subpic_heap,
6616                          sizeof(struct object_subpic),
6617                          SUBPIC_ID_OFFSET))
6618         goto err_subpic_heap;
6619
6620     i965->batch = intel_batchbuffer_new(&i965->intel, I915_EXEC_RENDER, 0);
6621     i965->pp_batch = intel_batchbuffer_new(&i965->intel, I915_EXEC_RENDER, 0);
6622     _i965InitMutex(&i965->render_mutex);
6623     _i965InitMutex(&i965->pp_mutex);
6624
6625     return true;
6626
6627 err_subpic_heap:
6628     object_heap_destroy(&i965->image_heap);
6629 err_image_heap:
6630     object_heap_destroy(&i965->buffer_heap);
6631 err_buffer_heap:
6632     object_heap_destroy(&i965->surface_heap);
6633 err_surface_heap:
6634     object_heap_destroy(&i965->context_heap);
6635 err_context_heap:
6636     object_heap_destroy(&i965->config_heap);
6637 err_config_heap:
6638
6639     return false;
6640 }
6641
6642 static void
6643 i965_driver_data_terminate(VADriverContextP ctx)
6644 {
6645     struct i965_driver_data *i965 = i965_driver_data(ctx);
6646
6647     _i965DestroyMutex(&i965->pp_mutex);
6648     _i965DestroyMutex(&i965->render_mutex);
6649
6650     if (i965->batch)
6651         intel_batchbuffer_free(i965->batch);
6652
6653     if (i965->pp_batch)
6654         intel_batchbuffer_free(i965->pp_batch);
6655
6656     i965_destroy_heap(&i965->subpic_heap, i965_destroy_subpic);
6657     i965_destroy_heap(&i965->image_heap, i965_destroy_image);
6658     i965_destroy_heap(&i965->buffer_heap, i965_destroy_buffer);
6659     i965_destroy_heap(&i965->surface_heap, i965_destroy_surface);
6660     i965_destroy_heap(&i965->context_heap, i965_destroy_context);
6661     i965_destroy_heap(&i965->config_heap, i965_destroy_config);
6662 }
6663
6664 struct {
6665     bool (*init)(VADriverContextP ctx);
6666     void (*terminate)(VADriverContextP ctx);
6667     int display_type;
6668 } i965_sub_ops[] =  {
6669     {
6670         intel_driver_init,
6671         intel_driver_terminate,
6672         0,
6673     },
6674
6675     {
6676         i965_driver_data_init,
6677         i965_driver_data_terminate,
6678         0,
6679     },
6680
6681     {
6682         i965_display_attributes_init,
6683         i965_display_attributes_terminate,
6684         0,
6685     },
6686
6687     {
6688         i965_post_processing_init,
6689         i965_post_processing_terminate,
6690         0,
6691     },
6692
6693     {
6694         i965_render_init,
6695         i965_render_terminate,
6696         0,
6697     },
6698
6699 #ifdef HAVE_VA_WAYLAND
6700     {
6701         i965_output_wayland_init,
6702         i965_output_wayland_terminate,
6703         VA_DISPLAY_WAYLAND,
6704     },
6705 #endif
6706
6707 #ifdef HAVE_VA_X11
6708     {
6709         i965_output_dri_init,
6710         i965_output_dri_terminate,
6711         VA_DISPLAY_X11,
6712     },
6713 #endif
6714
6715     {
6716         i965_gpe_table_init,
6717         i965_gpe_table_terminate,
6718         0,
6719     },
6720 };
6721
6722 static bool
6723 ensure_vendor_string(struct i965_driver_data *i965, const char *chipset)
6724 {
6725     int ret, len;
6726
6727     if (i965->va_vendor[0] != '\0')
6728         return true;
6729
6730     len = 0;
6731     ret = snprintf(i965->va_vendor, sizeof(i965->va_vendor),
6732                    "%s %s driver for %s - %d.%d.%d",
6733                    INTEL_STR_DRIVER_VENDOR, INTEL_STR_DRIVER_NAME, chipset,
6734                    INTEL_DRIVER_MAJOR_VERSION, INTEL_DRIVER_MINOR_VERSION,
6735                    INTEL_DRIVER_MICRO_VERSION);
6736     if (ret < 0 || ret >= sizeof(i965->va_vendor))
6737         goto error;
6738     len = ret;
6739
6740     if (INTEL_DRIVER_PRE_VERSION > 0) {
6741         ret = snprintf(&i965->va_vendor[len], sizeof(i965->va_vendor) - len,
6742                        ".pre%d", INTEL_DRIVER_PRE_VERSION);
6743         if (ret < 0 || ret >= sizeof(i965->va_vendor))
6744             goto error;
6745         len += ret;
6746
6747         ret = snprintf(&i965->va_vendor[len], sizeof(i965->va_vendor) - len,
6748                        " (%s)", INTEL_DRIVER_GIT_VERSION);
6749         if (ret < 0 || ret >= sizeof(i965->va_vendor))
6750             goto error;
6751         len += ret;
6752     }
6753     return true;
6754
6755 error:
6756     i965->va_vendor[0] = '\0';
6757     ASSERT_RET(ret > 0 && len < sizeof(i965->va_vendor), false);
6758     return false;
6759 }
6760
6761 /* Only when the option of "enable-wrapper" is passed, it is possible
6762  * to initialize/load the wrapper context of backend driver.
6763  * Otherwise it is not loaded.
6764  */
6765 #if HAVE_HYBRID_CODEC
6766
6767 static VAStatus
6768 i965_initialize_wrapper(VADriverContextP ctx, const char *driver_name)
6769 {
6770 #define DRIVER_EXTENSION    "_drv_video.so"
6771
6772     struct i965_driver_data *i965 = i965_driver_data(ctx);
6773
6774     VADriverContextP wrapper_pdrvctx;
6775     struct VADriverVTable *vtable;
6776     char *search_path, *driver_dir;
6777     char *saveptr;
6778     char driver_path[256];
6779     void *handle = NULL;
6780     VAStatus va_status = VA_STATUS_SUCCESS;
6781     bool driver_loaded = false;
6782
6783     if (HAS_VP9_DECODING(i965)) {
6784         i965->wrapper_pdrvctx = NULL;
6785         return va_status;
6786     }
6787
6788     wrapper_pdrvctx = calloc(1, sizeof(*wrapper_pdrvctx));
6789     vtable = calloc(1, sizeof(*vtable));
6790
6791     if (!wrapper_pdrvctx || !vtable) {
6792         fprintf(stderr, "Failed to allocate memory for wrapper \n");
6793         free(wrapper_pdrvctx);
6794         free(vtable);
6795         return VA_STATUS_ERROR_ALLOCATION_FAILED;
6796     }
6797
6798     /* use the same drm_state with CTX */
6799     wrapper_pdrvctx->drm_state = ctx->drm_state;
6800     wrapper_pdrvctx->display_type = ctx->display_type;
6801     wrapper_pdrvctx->vtable = vtable;
6802
6803     search_path = VA_DRIVERS_PATH;
6804     search_path = strdup((const char *)search_path);
6805
6806     driver_dir = strtok_r(search_path, ":", &saveptr);
6807     while (driver_dir && !driver_loaded) {
6808         memset(driver_path, 0, sizeof(driver_path));
6809         sprintf(driver_path, "%s/%s%s", driver_dir, driver_name, DRIVER_EXTENSION);
6810
6811         handle = dlopen(driver_path, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
6812         if (!handle) {
6813             fprintf(stderr, "failed to open %s\n", driver_path);
6814             driver_dir = strtok_r(NULL, ":", &saveptr);
6815             continue;
6816         }
6817         {
6818             VADriverInit init_func = NULL;
6819             char init_func_s[256];
6820             int i;
6821
6822             static const struct {
6823                 int major;
6824                 int minor;
6825             } compatible_versions[] = {
6826                 { VA_MAJOR_VERSION, VA_MINOR_VERSION },
6827                 { 0, 37 },
6828                 { 0, 36 },
6829                 { 0, 35 },
6830                 { 0, 34 },
6831                 { 0, 33 },
6832                 { 0, 32 },
6833                 { -1, }
6834             };
6835             for (i = 0; compatible_versions[i].major >= 0; i++) {
6836                 snprintf(init_func_s, sizeof(init_func_s),
6837                          "__vaDriverInit_%d_%d",
6838                          compatible_versions[i].major,
6839                          compatible_versions[i].minor);
6840                 init_func = (VADriverInit)dlsym(handle, init_func_s);
6841                 if (init_func) {
6842                     break;
6843                 }
6844             }
6845             if (compatible_versions[i].major < 0) {
6846                 dlclose(handle);
6847                 fprintf(stderr, "%s has no function %s\n",
6848                         driver_path, init_func_s);
6849                 driver_dir = strtok_r(NULL, ":", &saveptr);
6850                 continue;
6851             }
6852
6853             if (init_func)
6854                 va_status = (*init_func)(wrapper_pdrvctx);
6855
6856             if (va_status != VA_STATUS_SUCCESS) {
6857                 dlclose(handle);
6858                 fprintf(stderr, "%s init failed\n", driver_path);
6859                 driver_dir = strtok_r(NULL, ":", &saveptr);
6860                 continue;
6861             }
6862
6863             wrapper_pdrvctx->handle = handle;
6864             driver_loaded = true;
6865         }
6866     }
6867
6868     free(search_path);
6869
6870     if (driver_loaded) {
6871         i965->wrapper_pdrvctx = wrapper_pdrvctx;
6872         return VA_STATUS_SUCCESS;
6873     } else {
6874         fprintf(stderr, "Failed to wrapper %s%s\n", driver_name, DRIVER_EXTENSION);
6875         free(vtable);
6876         free(wrapper_pdrvctx);
6877         return VA_STATUS_ERROR_OPERATION_FAILED;
6878     }
6879 }
6880 #endif
6881
6882 static VAStatus
6883 i965_Init(VADriverContextP ctx)
6884 {
6885     struct i965_driver_data *i965 = i965_driver_data(ctx);
6886     int i;
6887     const char *chipset;
6888
6889     for (i = 0; i < ARRAY_ELEMS(i965_sub_ops); i++) {
6890         if ((i965_sub_ops[i].display_type == 0 ||
6891              i965_sub_ops[i].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) &&
6892             !i965_sub_ops[i].init(ctx))
6893             break;
6894     }
6895
6896     if (i == ARRAY_ELEMS(i965_sub_ops)) {
6897         switch (i965->intel.device_id) {
6898 #undef CHIPSET
6899 #define CHIPSET(id, family, dev, str) case id: chipset = str; break;
6900 #include "i965_pciids.h"
6901         default:
6902             chipset = "Unknown Intel Chipset";
6903             break;
6904         }
6905
6906         if (!ensure_vendor_string(i965, chipset))
6907             return VA_STATUS_ERROR_ALLOCATION_FAILED;
6908
6909         i965->current_context_id = VA_INVALID_ID;
6910
6911         if (i965->codec_info && i965->codec_info->preinit_hw_codec)
6912             i965->codec_info->preinit_hw_codec(ctx, i965->codec_info);
6913
6914 #if HAVE_HYBRID_CODEC
6915         i965_initialize_wrapper(ctx, "hybrid");
6916 #endif
6917
6918         return VA_STATUS_SUCCESS;
6919     } else {
6920         i--;
6921
6922         for (; i >= 0; i--) {
6923             if (i965_sub_ops[i].display_type == 0 ||
6924                 i965_sub_ops[i].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) {
6925                 i965_sub_ops[i].terminate(ctx);
6926             }
6927         }
6928
6929         return VA_STATUS_ERROR_UNKNOWN;
6930     }
6931 }
6932
6933 VAStatus
6934 i965_Terminate(VADriverContextP ctx)
6935 {
6936     struct i965_driver_data *i965 = i965_driver_data(ctx);
6937     int i;
6938
6939     if (i965) {
6940 #if HAVE_HYBRID_CODEC
6941         if (i965->wrapper_pdrvctx) {
6942             VADriverContextP pdrvctx;
6943             pdrvctx = i965->wrapper_pdrvctx;
6944             if (pdrvctx->handle) {
6945                 pdrvctx->vtable->vaTerminate(pdrvctx);
6946                 dlclose(pdrvctx->handle);
6947                 pdrvctx->handle = NULL;
6948             }
6949             free(pdrvctx->vtable);
6950             free(pdrvctx);
6951             i965->wrapper_pdrvctx = NULL;
6952         }
6953 #endif
6954
6955         for (i = ARRAY_ELEMS(i965_sub_ops); i > 0; i--)
6956             if (i965_sub_ops[i - 1].display_type == 0 ||
6957                 i965_sub_ops[i - 1].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) {
6958                 i965_sub_ops[i - 1].terminate(ctx);
6959             }
6960
6961         free(i965);
6962         ctx->pDriverData = NULL;
6963     }
6964
6965     return VA_STATUS_SUCCESS;
6966 }
6967
6968 VAStatus DLL_EXPORT
6969 VA_DRIVER_INIT_FUNC(VADriverContextP ctx);
6970
6971 VAStatus
6972 VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
6973 {
6974     struct VADriverVTable * const vtable = ctx->vtable;
6975     struct VADriverVTableVPP * const vtable_vpp = ctx->vtable_vpp;
6976
6977     struct i965_driver_data *i965;
6978     VAStatus ret = VA_STATUS_ERROR_UNKNOWN;
6979
6980     ctx->version_major = VA_MAJOR_VERSION;
6981     ctx->version_minor = VA_MINOR_VERSION;
6982     ctx->max_profiles = I965_MAX_PROFILES;
6983     ctx->max_entrypoints = I965_MAX_ENTRYPOINTS;
6984     ctx->max_attributes = I965_MAX_CONFIG_ATTRIBUTES;
6985     ctx->max_image_formats = I965_MAX_IMAGE_FORMATS;
6986     ctx->max_subpic_formats = I965_MAX_SUBPIC_FORMATS;
6987     ctx->max_display_attributes = 1 + ARRAY_ELEMS(i965_display_attributes);
6988
6989     vtable->vaTerminate = i965_Terminate;
6990     vtable->vaQueryConfigEntrypoints = i965_QueryConfigEntrypoints;
6991     vtable->vaQueryConfigProfiles = i965_QueryConfigProfiles;
6992     vtable->vaQueryConfigAttributes = i965_QueryConfigAttributes;
6993     vtable->vaCreateConfig = i965_CreateConfig;
6994     vtable->vaDestroyConfig = i965_DestroyConfig;
6995     vtable->vaGetConfigAttributes = i965_GetConfigAttributes;
6996     vtable->vaCreateSurfaces = i965_CreateSurfaces;
6997     vtable->vaDestroySurfaces = i965_DestroySurfaces;
6998     vtable->vaCreateContext = i965_CreateContext;
6999     vtable->vaDestroyContext = i965_DestroyContext;
7000     vtable->vaCreateBuffer = i965_CreateBuffer;
7001     vtable->vaBufferSetNumElements = i965_BufferSetNumElements;
7002     vtable->vaMapBuffer = i965_MapBuffer;
7003     vtable->vaUnmapBuffer = i965_UnmapBuffer;
7004     vtable->vaDestroyBuffer = i965_DestroyBuffer;
7005     vtable->vaBeginPicture = i965_BeginPicture;
7006     vtable->vaRenderPicture = i965_RenderPicture;
7007     vtable->vaEndPicture = i965_EndPicture;
7008     vtable->vaSyncSurface = i965_SyncSurface;
7009     vtable->vaQuerySurfaceStatus = i965_QuerySurfaceStatus;
7010     vtable->vaPutSurface = i965_PutSurface;
7011     vtable->vaQueryImageFormats = i965_QueryImageFormats;
7012     vtable->vaCreateImage = i965_CreateImage;
7013     vtable->vaDeriveImage = i965_DeriveImage;
7014     vtable->vaDestroyImage = i965_DestroyImage;
7015     vtable->vaSetImagePalette = i965_SetImagePalette;
7016     vtable->vaGetImage = i965_GetImage;
7017     vtable->vaPutImage = i965_PutImage;
7018     vtable->vaQuerySubpictureFormats = i965_QuerySubpictureFormats;
7019     vtable->vaCreateSubpicture = i965_CreateSubpicture;
7020     vtable->vaDestroySubpicture = i965_DestroySubpicture;
7021     vtable->vaSetSubpictureImage = i965_SetSubpictureImage;
7022     vtable->vaSetSubpictureChromakey = i965_SetSubpictureChromakey;
7023     vtable->vaSetSubpictureGlobalAlpha = i965_SetSubpictureGlobalAlpha;
7024     vtable->vaAssociateSubpicture = i965_AssociateSubpicture;
7025     vtable->vaDeassociateSubpicture = i965_DeassociateSubpicture;
7026     vtable->vaQueryDisplayAttributes = i965_QueryDisplayAttributes;
7027     vtable->vaGetDisplayAttributes = i965_GetDisplayAttributes;
7028     vtable->vaSetDisplayAttributes = i965_SetDisplayAttributes;
7029     vtable->vaBufferInfo = i965_BufferInfo;
7030     vtable->vaLockSurface = i965_LockSurface;
7031     vtable->vaUnlockSurface = i965_UnlockSurface;
7032     vtable->vaGetSurfaceAttributes = i965_GetSurfaceAttributes;
7033     vtable->vaQuerySurfaceAttributes = i965_QuerySurfaceAttributes;
7034     vtable->vaCreateSurfaces2 = i965_CreateSurfaces2;
7035
7036     /* 0.36.0 */
7037     vtable->vaAcquireBufferHandle = i965_AcquireBufferHandle;
7038     vtable->vaReleaseBufferHandle = i965_ReleaseBufferHandle;
7039
7040     vtable_vpp->vaQueryVideoProcFilters = i965_QueryVideoProcFilters;
7041     vtable_vpp->vaQueryVideoProcFilterCaps = i965_QueryVideoProcFilterCaps;
7042     vtable_vpp->vaQueryVideoProcPipelineCaps = i965_QueryVideoProcPipelineCaps;
7043
7044     i965 = (struct i965_driver_data *)calloc(1, sizeof(*i965));
7045
7046     if (i965 == NULL) {
7047         ctx->pDriverData = NULL;
7048
7049         return VA_STATUS_ERROR_ALLOCATION_FAILED;
7050     }
7051
7052     i965->wrapper_pdrvctx = NULL;
7053     ctx->pDriverData = (void *)i965;
7054     ret = i965_Init(ctx);
7055
7056     if (ret == VA_STATUS_SUCCESS) {
7057         ctx->str_vendor = i965->va_vendor;
7058     } else {
7059         free(i965);
7060         ctx->pDriverData = NULL;
7061     }
7062
7063     return ret;
7064 }