OSDN Git Service

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