OSDN Git Service

Add vdenc common commands for CNL
[android-x86/hardware-intel-common-vaapi.git] / src / gen6_mfd.c
1 /*
2  * Copyright © 2010 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  *
27  */
28
29 #include "sysdeps.h"
30 #include "intel_batchbuffer.h"
31 #include "intel_driver.h"
32 #include "i965_defines.h"
33 #include "i965_drv_video.h"
34 #include "i965_decoder_utils.h"
35
36 #include "gen6_mfd.h"
37 #include "intel_media.h"
38
39 static const uint32_t zigzag_direct[64] = {
40     0,   1,  8, 16,  9,  2,  3, 10,
41     17, 24, 32, 25, 18, 11,  4,  5,
42     12, 19, 26, 33, 40, 48, 41, 34,
43     27, 20, 13,  6,  7, 14, 21, 28,
44     35, 42, 49, 56, 57, 50, 43, 36,
45     29, 22, 15, 23, 30, 37, 44, 51,
46     58, 59, 52, 45, 38, 31, 39, 46,
47     53, 60, 61, 54, 47, 55, 62, 63
48 };
49
50 static void
51 gen6_mfd_init_avc_surface(VADriverContextP ctx,
52                           VAPictureParameterBufferH264 *pic_param,
53                           struct object_surface *obj_surface)
54 {
55     struct i965_driver_data *i965 = i965_driver_data(ctx);
56     GenAvcSurface *gen6_avc_surface = obj_surface->private_data;
57     int height_in_mbs;
58
59     obj_surface->free_private_data = gen_free_avc_surface;
60     height_in_mbs = ((pic_param->picture_height_in_mbs_minus1 + 1) & 0xff); /* frame height */
61
62     if (!gen6_avc_surface) {
63         gen6_avc_surface = calloc(sizeof(GenAvcSurface), 1);
64         assert(gen6_avc_surface);
65         gen6_avc_surface->base.frame_store_id = -1;
66         assert((obj_surface->size & 0x3f) == 0);
67         obj_surface->private_data = gen6_avc_surface;
68     }
69
70     gen6_avc_surface->dmv_bottom_flag = (pic_param->pic_fields.bits.field_pic_flag &&
71                                          !pic_param->seq_fields.bits.direct_8x8_inference_flag);
72
73     if (gen6_avc_surface->dmv_top == NULL) {
74         gen6_avc_surface->dmv_top = dri_bo_alloc(i965->intel.bufmgr,
75                                                  "direct mv w/r buffer",
76                                                  128 * height_in_mbs * 64,      /* scalable with frame height */
77                                                  0x1000);
78     }
79
80     if (gen6_avc_surface->dmv_bottom_flag &&
81         gen6_avc_surface->dmv_bottom == NULL) {
82         gen6_avc_surface->dmv_bottom = dri_bo_alloc(i965->intel.bufmgr,
83                                                     "direct mv w/r buffer",
84                                                     128 * height_in_mbs * 64,   /* scalable with frame height */
85                                                     0x1000);
86     }
87 }
88
89 static void
90 gen6_mfd_pipe_mode_select(VADriverContextP ctx,
91                           struct decode_state *decode_state,
92                           int standard_select,
93                           struct gen6_mfd_context *gen6_mfd_context)
94 {
95     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
96
97     assert(standard_select == MFX_FORMAT_MPEG2 ||
98            standard_select == MFX_FORMAT_AVC ||
99            standard_select == MFX_FORMAT_VC1);
100
101     BEGIN_BCS_BATCH(batch, 4);
102     OUT_BCS_BATCH(batch, MFX_PIPE_MODE_SELECT | (4 - 2));
103     OUT_BCS_BATCH(batch,
104                   (MFD_MODE_VLD << 16) | /* VLD mode */
105                   (0 << 10) | /* disable Stream-Out */
106                   (gen6_mfd_context->post_deblocking_output.valid << 9)  | /* Post Deblocking Output */
107                   (gen6_mfd_context->pre_deblocking_output.valid << 8)  | /* Pre Deblocking Output */
108                   (0 << 7)  | /* disable TLB prefectch */
109                   (0 << 5)  | /* not in stitch mode */
110                   (MFX_CODEC_DECODE << 4)  | /* decoding mode */
111                   (standard_select << 0));
112     OUT_BCS_BATCH(batch,
113                   (0 << 20) | /* round flag in PB slice */
114                   (0 << 19) | /* round flag in Intra8x8 */
115                   (0 << 7)  | /* expand NOA bus flag */
116                   (1 << 6)  | /* must be 1 */
117                   (0 << 5)  | /* disable clock gating for NOA */
118                   (0 << 4)  | /* terminate if AVC motion and POC table error occurs */
119                   (0 << 3)  | /* terminate if AVC mbdata error occurs */
120                   (0 << 2)  | /* terminate if AVC CABAC/CAVLC decode error occurs */
121                   (0 << 1)  | /* AVC long field motion vector */
122                   (1 << 0));  /* always calculate AVC ILDB boundary strength */
123     OUT_BCS_BATCH(batch, 0);
124     ADVANCE_BCS_BATCH(batch);
125 }
126
127 static void
128 gen6_mfd_surface_state(VADriverContextP ctx,
129                        struct decode_state *decode_state,
130                        int standard_select,
131                        struct gen6_mfd_context *gen6_mfd_context)
132 {
133     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
134     struct object_surface *obj_surface = decode_state->render_object;
135     unsigned int surface_format;
136
137     surface_format = obj_surface->fourcc == VA_FOURCC_Y800 ?
138                      MFX_SURFACE_MONOCHROME : MFX_SURFACE_PLANAR_420_8;
139
140     BEGIN_BCS_BATCH(batch, 6);
141     OUT_BCS_BATCH(batch, MFX_SURFACE_STATE | (6 - 2));
142     OUT_BCS_BATCH(batch, 0);
143     OUT_BCS_BATCH(batch,
144                   ((obj_surface->orig_height - 1) << 19) |
145                   ((obj_surface->orig_width - 1) << 6));
146     OUT_BCS_BATCH(batch,
147                   (surface_format << 28) | /* 420 planar YUV surface */
148                   (1 << 27) | /* must be 1 for interleave U/V, hardware requirement */
149                   (0 << 22) | /* surface object control state, FIXME??? */
150                   ((obj_surface->width - 1) << 3) | /* pitch */
151                   (0 << 2)  | /* must be 0 for interleave U/V */
152                   (1 << 1)  | /* must be y-tiled */
153                   (I965_TILEWALK_YMAJOR << 0));  /* tile walk, FIXME: must be 1 ??? */
154     OUT_BCS_BATCH(batch,
155                   (0 << 16) | /* must be 0 for interleave U/V */
156                   (obj_surface->height)); /* y offset for U(cb) */
157     OUT_BCS_BATCH(batch, 0);
158     ADVANCE_BCS_BATCH(batch);
159 }
160
161 static void
162 gen6_mfd_pipe_buf_addr_state(VADriverContextP ctx,
163                              struct decode_state *decode_state,
164                              int standard_select,
165                              struct gen6_mfd_context *gen6_mfd_context)
166 {
167     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
168     int i;
169
170     BEGIN_BCS_BATCH(batch, 24);
171     OUT_BCS_BATCH(batch, MFX_PIPE_BUF_ADDR_STATE | (24 - 2));
172     if (gen6_mfd_context->pre_deblocking_output.valid)
173         OUT_BCS_RELOC(batch, gen6_mfd_context->pre_deblocking_output.bo,
174                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
175                       0);
176     else
177         OUT_BCS_BATCH(batch, 0);
178
179     if (gen6_mfd_context->post_deblocking_output.valid)
180         OUT_BCS_RELOC(batch, gen6_mfd_context->post_deblocking_output.bo,
181                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
182                       0);
183     else
184         OUT_BCS_BATCH(batch, 0);
185
186     OUT_BCS_BATCH(batch, 0); /* ignore for decoding */
187     OUT_BCS_BATCH(batch, 0); /* ignore for decoding */
188
189     if (gen6_mfd_context->intra_row_store_scratch_buffer.valid)
190         OUT_BCS_RELOC(batch, gen6_mfd_context->intra_row_store_scratch_buffer.bo,
191                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
192                       0);
193     else
194         OUT_BCS_BATCH(batch, 0);
195
196     if (gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.valid)
197         OUT_BCS_RELOC(batch, gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.bo,
198                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
199                       0);
200     else
201         OUT_BCS_BATCH(batch, 0);
202
203     /* DW 7..22 */
204     for (i = 0; i < ARRAY_ELEMS(gen6_mfd_context->reference_surface); i++) {
205         struct object_surface *obj_surface;
206
207         if (gen6_mfd_context->reference_surface[i].surface_id != VA_INVALID_ID &&
208             gen6_mfd_context->reference_surface[i].obj_surface &&
209             gen6_mfd_context->reference_surface[i].obj_surface->bo) {
210             obj_surface = gen6_mfd_context->reference_surface[i].obj_surface;
211
212             OUT_BCS_RELOC(batch, obj_surface->bo,
213                           I915_GEM_DOMAIN_INSTRUCTION, 0,
214                           0);
215         } else {
216             OUT_BCS_BATCH(batch, 0);
217         }
218     }
219
220     OUT_BCS_BATCH(batch, 0);   /* ignore DW23 for decoding */
221     ADVANCE_BCS_BATCH(batch);
222 }
223
224 static void
225 gen6_mfd_ind_obj_base_addr_state(VADriverContextP ctx,
226                                  dri_bo *slice_data_bo,
227                                  int standard_select,
228                                  struct gen6_mfd_context *gen6_mfd_context)
229 {
230     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
231
232     BEGIN_BCS_BATCH(batch, 11);
233     OUT_BCS_BATCH(batch, MFX_IND_OBJ_BASE_ADDR_STATE | (11 - 2));
234     OUT_BCS_RELOC(batch, slice_data_bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 0); /* MFX Indirect Bitstream Object Base Address */
235     OUT_BCS_BATCH(batch, 0);
236     OUT_BCS_BATCH(batch, 0); /* ignore for VLD mode */
237     OUT_BCS_BATCH(batch, 0);
238     OUT_BCS_BATCH(batch, 0); /* ignore for VLD mode */
239     OUT_BCS_BATCH(batch, 0);
240     OUT_BCS_BATCH(batch, 0); /* ignore for VLD mode */
241     OUT_BCS_BATCH(batch, 0);
242     OUT_BCS_BATCH(batch, 0); /* ignore for VLD mode */
243     OUT_BCS_BATCH(batch, 0);
244     ADVANCE_BCS_BATCH(batch);
245 }
246
247 static void
248 gen6_mfd_bsp_buf_base_addr_state(VADriverContextP ctx,
249                                  struct decode_state *decode_state,
250                                  int standard_select,
251                                  struct gen6_mfd_context *gen6_mfd_context)
252 {
253     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
254
255     BEGIN_BCS_BATCH(batch, 4);
256     OUT_BCS_BATCH(batch, MFX_BSP_BUF_BASE_ADDR_STATE | (4 - 2));
257
258     if (gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.valid)
259         OUT_BCS_RELOC(batch, gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo,
260                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
261                       0);
262     else
263         OUT_BCS_BATCH(batch, 0);
264
265     if (gen6_mfd_context->mpr_row_store_scratch_buffer.valid)
266         OUT_BCS_RELOC(batch, gen6_mfd_context->mpr_row_store_scratch_buffer.bo,
267                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
268                       0);
269     else
270         OUT_BCS_BATCH(batch, 0);
271
272     if (gen6_mfd_context->bitplane_read_buffer.valid)
273         OUT_BCS_RELOC(batch, gen6_mfd_context->bitplane_read_buffer.bo,
274                       I915_GEM_DOMAIN_INSTRUCTION, 0,
275                       0);
276     else
277         OUT_BCS_BATCH(batch, 0);
278
279     ADVANCE_BCS_BATCH(batch);
280 }
281
282 static void
283 gen6_mfd_avc_img_state(VADriverContextP ctx,
284                        struct decode_state *decode_state,
285                        struct gen6_mfd_context *gen6_mfd_context)
286 {
287     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
288     int qm_present_flag;
289     int img_struct;
290     int mbaff_frame_flag;
291     unsigned int width_in_mbs, height_in_mbs;
292     VAPictureParameterBufferH264 *pic_param;
293
294     assert(decode_state->pic_param && decode_state->pic_param->buffer);
295     pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
296
297     if (decode_state->iq_matrix && decode_state->iq_matrix->buffer)
298         qm_present_flag = 1;
299     else
300         qm_present_flag = 0; /* built-in QM matrices */
301
302     if (pic_param->CurrPic.flags & VA_PICTURE_H264_TOP_FIELD)
303         img_struct = 1;
304     else if (pic_param->CurrPic.flags & VA_PICTURE_H264_BOTTOM_FIELD)
305         img_struct = 3;
306     else
307         img_struct = 0;
308
309     if ((img_struct & 0x1) == 0x1) {
310         assert(pic_param->pic_fields.bits.field_pic_flag == 0x1);
311     } else {
312         assert(pic_param->pic_fields.bits.field_pic_flag == 0x0);
313     }
314
315     if (pic_param->seq_fields.bits.frame_mbs_only_flag) { /* a frame containing only frame macroblocks */
316         assert(pic_param->seq_fields.bits.mb_adaptive_frame_field_flag == 0);
317         assert(pic_param->pic_fields.bits.field_pic_flag == 0);
318     } else {
319         assert(pic_param->seq_fields.bits.direct_8x8_inference_flag == 1); /* see H.264 spec */
320     }
321
322     mbaff_frame_flag = (pic_param->seq_fields.bits.mb_adaptive_frame_field_flag &&
323                         !pic_param->pic_fields.bits.field_pic_flag);
324
325     width_in_mbs = ((pic_param->picture_width_in_mbs_minus1 + 1) & 0xff);
326     height_in_mbs = ((pic_param->picture_height_in_mbs_minus1 + 1) & 0xff); /* frame height */
327     assert(!((width_in_mbs * height_in_mbs) & 0x8000)); /* hardware requirement */
328
329     /* MFX unit doesn't support 4:2:2 and 4:4:4 picture */
330     assert(pic_param->seq_fields.bits.chroma_format_idc == 0 || /* monochrome picture */
331            pic_param->seq_fields.bits.chroma_format_idc == 1);  /* 4:2:0 */
332     assert(pic_param->seq_fields.bits.residual_colour_transform_flag == 0); /* only available for 4:4:4 */
333
334     BEGIN_BCS_BATCH(batch, 13);
335     OUT_BCS_BATCH(batch, MFX_AVC_IMG_STATE | (13 - 2));
336     OUT_BCS_BATCH(batch,
337                   ((width_in_mbs * height_in_mbs) & 0x7fff));
338     OUT_BCS_BATCH(batch,
339                   (height_in_mbs << 16) |
340                   (width_in_mbs << 0));
341     OUT_BCS_BATCH(batch,
342                   ((pic_param->second_chroma_qp_index_offset & 0x1f) << 24) |
343                   ((pic_param->chroma_qp_index_offset & 0x1f) << 16) |
344                   (0 << 14) | /* Max-bit conformance Intra flag ??? FIXME */
345                   (0 << 13) | /* Max Macroblock size conformance Inter flag ??? FIXME */
346                   (1 << 12) | /* always 1, hardware requirement */
347                   (qm_present_flag << 10) |
348                   (img_struct << 8) |
349                   (16 << 0));
350     OUT_BCS_BATCH(batch,
351                   (pic_param->seq_fields.bits.chroma_format_idc << 10) |
352                   (pic_param->pic_fields.bits.entropy_coding_mode_flag << 7) |
353                   ((!pic_param->pic_fields.bits.reference_pic_flag) << 6) |
354                   (pic_param->pic_fields.bits.constrained_intra_pred_flag << 5) |
355                   (pic_param->seq_fields.bits.direct_8x8_inference_flag << 4) |
356                   (pic_param->pic_fields.bits.transform_8x8_mode_flag << 3) |
357                   (pic_param->seq_fields.bits.frame_mbs_only_flag << 2) |
358                   (mbaff_frame_flag << 1) |
359                   (pic_param->pic_fields.bits.field_pic_flag << 0));
360     OUT_BCS_BATCH(batch, 0);
361     OUT_BCS_BATCH(batch, 0);
362     OUT_BCS_BATCH(batch, 0);
363     OUT_BCS_BATCH(batch, 0);
364     OUT_BCS_BATCH(batch, 0);
365     OUT_BCS_BATCH(batch, 0);
366     OUT_BCS_BATCH(batch, 0);
367     OUT_BCS_BATCH(batch, 0);
368     ADVANCE_BCS_BATCH(batch);
369 }
370
371 static void
372 gen6_mfd_avc_qm_state(VADriverContextP ctx,
373                       struct decode_state *decode_state,
374                       struct gen6_mfd_context *gen6_mfd_context)
375 {
376     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
377     int cmd_len;
378     VAIQMatrixBufferH264 *iq_matrix;
379     VAPictureParameterBufferH264 *pic_param;
380
381     if (!decode_state->iq_matrix || !decode_state->iq_matrix->buffer)
382         return;
383
384     iq_matrix = (VAIQMatrixBufferH264 *)decode_state->iq_matrix->buffer;
385
386     assert(decode_state->pic_param && decode_state->pic_param->buffer);
387     pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
388
389     cmd_len = 2 + 6 * 4; /* always load six 4x4 scaling matrices */
390
391     if (pic_param->pic_fields.bits.transform_8x8_mode_flag)
392         cmd_len += 2 * 16; /* load two 8x8 scaling matrices */
393
394     BEGIN_BCS_BATCH(batch, cmd_len);
395     OUT_BCS_BATCH(batch, MFX_AVC_QM_STATE | (cmd_len - 2));
396
397     if (pic_param->pic_fields.bits.transform_8x8_mode_flag)
398         OUT_BCS_BATCH(batch,
399                       (0x0  << 8) | /* don't use default built-in matrices */
400                       (0xff << 0)); /* six 4x4 and two 8x8 scaling matrices */
401     else
402         OUT_BCS_BATCH(batch,
403                       (0x0  << 8) | /* don't use default built-in matrices */
404                       (0x3f << 0)); /* six 4x4 scaling matrices */
405
406     intel_batchbuffer_data(batch, &iq_matrix->ScalingList4x4[0][0], 6 * 4 * 4);
407
408     if (pic_param->pic_fields.bits.transform_8x8_mode_flag)
409         intel_batchbuffer_data(batch, &iq_matrix->ScalingList8x8[0][0], 2 * 16 * 4);
410
411     ADVANCE_BCS_BATCH(batch);
412 }
413
414 static void
415 gen6_mfd_avc_directmode_state(VADriverContextP ctx,
416                               struct decode_state *decode_state,
417                               VAPictureParameterBufferH264 *pic_param,
418                               VASliceParameterBufferH264 *slice_param,
419                               struct gen6_mfd_context *gen6_mfd_context)
420 {
421     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
422     struct object_surface *obj_surface;
423     GenAvcSurface *gen6_avc_surface;
424     VAPictureH264 *va_pic;
425     int i;
426
427     BEGIN_BCS_BATCH(batch, 69);
428     OUT_BCS_BATCH(batch, MFX_AVC_DIRECTMODE_STATE | (69 - 2));
429
430     /* reference surfaces 0..15 */
431     for (i = 0; i < ARRAY_ELEMS(gen6_mfd_context->reference_surface); i++) {
432         if (gen6_mfd_context->reference_surface[i].surface_id != VA_INVALID_ID &&
433             gen6_mfd_context->reference_surface[i].obj_surface &&
434             gen6_mfd_context->reference_surface[i].obj_surface->private_data) {
435
436             obj_surface = gen6_mfd_context->reference_surface[i].obj_surface;
437             gen6_avc_surface = obj_surface->private_data;
438             OUT_BCS_RELOC(batch, gen6_avc_surface->dmv_top,
439                           I915_GEM_DOMAIN_INSTRUCTION, 0,
440                           0);
441
442             if (gen6_avc_surface->dmv_bottom_flag == 1)
443                 OUT_BCS_RELOC(batch, gen6_avc_surface->dmv_bottom,
444                               I915_GEM_DOMAIN_INSTRUCTION, 0,
445                               0);
446             else
447                 OUT_BCS_RELOC(batch, gen6_avc_surface->dmv_top,
448                               I915_GEM_DOMAIN_INSTRUCTION, 0,
449                               0);
450         } else {
451             OUT_BCS_BATCH(batch, 0);
452             OUT_BCS_BATCH(batch, 0);
453         }
454     }
455
456     /* the current decoding frame/field */
457     va_pic = &pic_param->CurrPic;
458     obj_surface = decode_state->render_object;
459     assert(obj_surface->bo && obj_surface->private_data);
460     gen6_avc_surface = obj_surface->private_data;
461
462     OUT_BCS_RELOC(batch, gen6_avc_surface->dmv_top,
463                   I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
464                   0);
465
466     if (gen6_avc_surface->dmv_bottom_flag == 1)
467         OUT_BCS_RELOC(batch, gen6_avc_surface->dmv_bottom,
468                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
469                       0);
470     else
471         OUT_BCS_RELOC(batch, gen6_avc_surface->dmv_top,
472                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
473                       0);
474
475     /* POC List */
476     for (i = 0; i < ARRAY_ELEMS(gen6_mfd_context->reference_surface); i++) {
477         obj_surface = gen6_mfd_context->reference_surface[i].obj_surface;
478
479         if (obj_surface) {
480             const VAPictureH264 * const va_pic = avc_find_picture(
481                                                      obj_surface->base.id, pic_param->ReferenceFrames,
482                                                      ARRAY_ELEMS(pic_param->ReferenceFrames));
483
484             assert(va_pic != NULL);
485             OUT_BCS_BATCH(batch, va_pic->TopFieldOrderCnt);
486             OUT_BCS_BATCH(batch, va_pic->BottomFieldOrderCnt);
487         } else {
488             OUT_BCS_BATCH(batch, 0);
489             OUT_BCS_BATCH(batch, 0);
490         }
491     }
492
493     va_pic = &pic_param->CurrPic;
494     OUT_BCS_BATCH(batch, va_pic->TopFieldOrderCnt);
495     OUT_BCS_BATCH(batch, va_pic->BottomFieldOrderCnt);
496
497     ADVANCE_BCS_BATCH(batch);
498 }
499
500 static void
501 gen6_mfd_avc_slice_state(VADriverContextP ctx,
502                          VAPictureParameterBufferH264 *pic_param,
503                          VASliceParameterBufferH264 *slice_param,
504                          VASliceParameterBufferH264 *next_slice_param,
505                          struct gen6_mfd_context *gen6_mfd_context)
506 {
507     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
508     int width_in_mbs = pic_param->picture_width_in_mbs_minus1 + 1;
509     int height_in_mbs = pic_param->picture_height_in_mbs_minus1 + 1;
510     int slice_hor_pos, slice_ver_pos, next_slice_hor_pos, next_slice_ver_pos;
511     int num_ref_idx_l0, num_ref_idx_l1;
512     int mbaff_picture = (!pic_param->pic_fields.bits.field_pic_flag &&
513                          pic_param->seq_fields.bits.mb_adaptive_frame_field_flag);
514     int weighted_pred_idc = 0;
515     int first_mb_in_slice = 0, first_mb_in_next_slice = 0;
516     unsigned int chroma_log2_weight_denom, luma_log2_weight_denom;
517     int slice_type;
518
519     if (slice_param->slice_type == SLICE_TYPE_I ||
520         slice_param->slice_type == SLICE_TYPE_SI) {
521         slice_type = SLICE_TYPE_I;
522     } else if (slice_param->slice_type == SLICE_TYPE_P ||
523                slice_param->slice_type == SLICE_TYPE_SP) {
524         slice_type = SLICE_TYPE_P;
525     } else {
526         assert(slice_param->slice_type == SLICE_TYPE_B);
527         slice_type = SLICE_TYPE_B;
528     }
529
530     luma_log2_weight_denom   = slice_param->luma_log2_weight_denom;
531     chroma_log2_weight_denom = slice_param->chroma_log2_weight_denom;
532
533     if (slice_type == SLICE_TYPE_I) {
534         assert(slice_param->num_ref_idx_l0_active_minus1 == 0);
535         assert(slice_param->num_ref_idx_l1_active_minus1 == 0);
536         num_ref_idx_l0 = 0;
537         num_ref_idx_l1 = 0;
538     } else if (slice_type == SLICE_TYPE_P) {
539         assert(slice_param->num_ref_idx_l1_active_minus1 == 0);
540         num_ref_idx_l0 = slice_param->num_ref_idx_l0_active_minus1 + 1;
541         num_ref_idx_l1 = 0;
542         weighted_pred_idc = (pic_param->pic_fields.bits.weighted_pred_flag == 1);
543     } else {
544         num_ref_idx_l0 = slice_param->num_ref_idx_l0_active_minus1 + 1;
545         num_ref_idx_l1 = slice_param->num_ref_idx_l1_active_minus1 + 1;
546         weighted_pred_idc = pic_param->pic_fields.bits.weighted_bipred_idc;
547
548         if (weighted_pred_idc == 2) {
549             /* 8.4.3 - Derivation process for prediction weights (8-279) */
550             luma_log2_weight_denom   = 5;
551             chroma_log2_weight_denom = 5;
552         }
553     }
554
555     first_mb_in_slice = slice_param->first_mb_in_slice;
556     slice_hor_pos = first_mb_in_slice % width_in_mbs;
557     slice_ver_pos = first_mb_in_slice / width_in_mbs;
558
559     if (mbaff_picture)
560         slice_ver_pos = slice_ver_pos << 1;
561
562     if (next_slice_param) {
563         first_mb_in_next_slice = next_slice_param->first_mb_in_slice;
564         next_slice_hor_pos = first_mb_in_next_slice % width_in_mbs;
565         next_slice_ver_pos = first_mb_in_next_slice / width_in_mbs;
566
567         if (mbaff_picture)
568             next_slice_ver_pos = next_slice_ver_pos << 1;
569     } else {
570         next_slice_hor_pos = 0;
571         next_slice_ver_pos = height_in_mbs;
572     }
573
574     BEGIN_BCS_BATCH(batch, 11); /* FIXME: is it 10??? */
575     OUT_BCS_BATCH(batch, MFX_AVC_SLICE_STATE | (11 - 2));
576     OUT_BCS_BATCH(batch, slice_type);
577     OUT_BCS_BATCH(batch,
578                   (num_ref_idx_l1 << 24) |
579                   (num_ref_idx_l0 << 16) |
580                   (chroma_log2_weight_denom << 8) |
581                   (luma_log2_weight_denom << 0));
582     OUT_BCS_BATCH(batch,
583                   (weighted_pred_idc << 30) |
584                   (slice_param->direct_spatial_mv_pred_flag << 29) |
585                   (slice_param->disable_deblocking_filter_idc << 27) |
586                   (slice_param->cabac_init_idc << 24) |
587                   ((pic_param->pic_init_qp_minus26 + 26 + slice_param->slice_qp_delta) << 16) |
588                   ((slice_param->slice_beta_offset_div2 & 0xf) << 8) |
589                   ((slice_param->slice_alpha_c0_offset_div2 & 0xf) << 0));
590     OUT_BCS_BATCH(batch,
591                   (slice_ver_pos << 24) |
592                   (slice_hor_pos << 16) |
593                   (first_mb_in_slice << 0));
594     OUT_BCS_BATCH(batch,
595                   (next_slice_ver_pos << 16) |
596                   (next_slice_hor_pos << 0));
597     OUT_BCS_BATCH(batch,
598                   (next_slice_param == NULL) << 19); /* last slice flag */
599     OUT_BCS_BATCH(batch, 0);
600     OUT_BCS_BATCH(batch, 0);
601     OUT_BCS_BATCH(batch, 0);
602     OUT_BCS_BATCH(batch, 0);
603     ADVANCE_BCS_BATCH(batch);
604 }
605
606 static inline void
607 gen6_mfd_avc_ref_idx_state(VADriverContextP ctx,
608                            VAPictureParameterBufferH264 *pic_param,
609                            VASliceParameterBufferH264 *slice_param,
610                            struct gen6_mfd_context *gen6_mfd_context)
611 {
612     gen6_send_avc_ref_idx_state(
613         gen6_mfd_context->base.batch,
614         slice_param,
615         gen6_mfd_context->reference_surface
616     );
617 }
618
619 static void
620 gen6_mfd_avc_weightoffset_state(VADriverContextP ctx,
621                                 VAPictureParameterBufferH264 *pic_param,
622                                 VASliceParameterBufferH264 *slice_param,
623                                 struct gen6_mfd_context *gen6_mfd_context)
624 {
625     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
626     int i, j, num_weight_offset_table = 0;
627     short weightoffsets[32 * 6];
628
629     if ((slice_param->slice_type == SLICE_TYPE_P ||
630          slice_param->slice_type == SLICE_TYPE_SP) &&
631         (pic_param->pic_fields.bits.weighted_pred_flag == 1)) {
632         num_weight_offset_table = 1;
633     }
634
635     if ((slice_param->slice_type == SLICE_TYPE_B) &&
636         (pic_param->pic_fields.bits.weighted_bipred_idc == 1)) {
637         num_weight_offset_table = 2;
638     }
639
640     for (i = 0; i < num_weight_offset_table; i++) {
641         BEGIN_BCS_BATCH(batch, 98);
642         OUT_BCS_BATCH(batch, MFX_AVC_WEIGHTOFFSET_STATE | (98 - 2));
643         OUT_BCS_BATCH(batch, i);
644
645         if (i == 0) {
646             for (j = 0; j < 32; j++) {
647                 weightoffsets[j * 6 + 0] = slice_param->luma_weight_l0[j];
648                 weightoffsets[j * 6 + 1] = slice_param->luma_offset_l0[j];
649                 weightoffsets[j * 6 + 2] = slice_param->chroma_weight_l0[j][0];
650                 weightoffsets[j * 6 + 3] = slice_param->chroma_offset_l0[j][0];
651                 weightoffsets[j * 6 + 4] = slice_param->chroma_weight_l0[j][1];
652                 weightoffsets[j * 6 + 5] = slice_param->chroma_offset_l0[j][1];
653             }
654         } else {
655             for (j = 0; j < 32; j++) {
656                 weightoffsets[j * 6 + 0] = slice_param->luma_weight_l1[j];
657                 weightoffsets[j * 6 + 1] = slice_param->luma_offset_l1[j];
658                 weightoffsets[j * 6 + 2] = slice_param->chroma_weight_l1[j][0];
659                 weightoffsets[j * 6 + 3] = slice_param->chroma_offset_l1[j][0];
660                 weightoffsets[j * 6 + 4] = slice_param->chroma_weight_l1[j][1];
661                 weightoffsets[j * 6 + 5] = slice_param->chroma_offset_l1[j][1];
662             }
663         }
664
665         intel_batchbuffer_data(batch, weightoffsets, sizeof(weightoffsets));
666         ADVANCE_BCS_BATCH(batch);
667     }
668 }
669
670 static void
671 gen6_mfd_avc_bsd_object(VADriverContextP ctx,
672                         VAPictureParameterBufferH264 *pic_param,
673                         VASliceParameterBufferH264 *slice_param,
674                         dri_bo *slice_data_bo,
675                         struct gen6_mfd_context *gen6_mfd_context)
676 {
677     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
678     unsigned int slice_data_bit_offset;
679
680     slice_data_bit_offset = avc_get_first_mb_bit_offset(
681                                 slice_data_bo,
682                                 slice_param,
683                                 pic_param->pic_fields.bits.entropy_coding_mode_flag
684                             );
685
686     BEGIN_BCS_BATCH(batch, 6);
687     OUT_BCS_BATCH(batch, MFD_AVC_BSD_OBJECT | (6 - 2));
688     OUT_BCS_BATCH(batch,
689                   (slice_param->slice_data_size - slice_param->slice_data_offset));
690     OUT_BCS_BATCH(batch, slice_param->slice_data_offset);
691     OUT_BCS_BATCH(batch,
692                   (0 << 31) |
693                   (0 << 14) |
694                   (0 << 12) |
695                   (0 << 10) |
696                   (0 << 8));
697     OUT_BCS_BATCH(batch,
698                   ((slice_data_bit_offset >> 3) << 16) |
699                   (1 << 7)  |
700                   (1 << 6)  |
701                   ((0x7 - (slice_data_bit_offset & 0x7)) << 0));
702     OUT_BCS_BATCH(batch, 0);
703     ADVANCE_BCS_BATCH(batch);
704 }
705
706 static void
707 gen6_mfd_avc_phantom_slice_first(VADriverContextP ctx,
708                                  VAPictureParameterBufferH264 *pic_param,
709                                  VASliceParameterBufferH264 *next_slice_param,
710                                  struct gen6_mfd_context *gen6_mfd_context)
711 {
712     gen6_mfd_avc_phantom_slice(ctx, pic_param, next_slice_param, gen6_mfd_context->base.batch);
713 }
714
715 static void
716 gen6_mfd_avc_phantom_slice_last(VADriverContextP ctx,
717                                 VAPictureParameterBufferH264 *pic_param,
718                                 struct gen6_mfd_context *gen6_mfd_context)
719 {
720     gen6_mfd_avc_phantom_slice(ctx, pic_param, NULL, gen6_mfd_context->base.batch);
721 }
722
723 static void
724 gen6_mfd_avc_decode_init(VADriverContextP ctx,
725                          struct decode_state *decode_state,
726                          struct gen6_mfd_context *gen6_mfd_context)
727 {
728     VAPictureParameterBufferH264 *pic_param;
729     VASliceParameterBufferH264 *slice_param;
730     struct i965_driver_data *i965 = i965_driver_data(ctx);
731     struct object_surface *obj_surface;
732     dri_bo *bo;
733     int i, j, enable_avc_ildb = 0;
734     int width_in_mbs;
735
736     for (j = 0; j < decode_state->num_slice_params && enable_avc_ildb == 0; j++) {
737         assert(decode_state->slice_params && decode_state->slice_params[j]->buffer);
738         slice_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j]->buffer;
739
740         for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
741             assert(slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_ALL);
742             assert((slice_param->slice_type == SLICE_TYPE_I) ||
743                    (slice_param->slice_type == SLICE_TYPE_SI) ||
744                    (slice_param->slice_type == SLICE_TYPE_P) ||
745                    (slice_param->slice_type == SLICE_TYPE_SP) ||
746                    (slice_param->slice_type == SLICE_TYPE_B));
747
748             if (slice_param->disable_deblocking_filter_idc != 1) {
749                 enable_avc_ildb = 1;
750                 break;
751             }
752
753             slice_param++;
754         }
755     }
756
757     assert(decode_state->pic_param && decode_state->pic_param->buffer);
758     pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
759     intel_update_avc_frame_store_index(ctx, decode_state, pic_param,
760                                        gen6_mfd_context->reference_surface, &gen6_mfd_context->fs_ctx);
761     width_in_mbs = ((pic_param->picture_width_in_mbs_minus1 + 1) & 0xff);
762
763     /* Current decoded picture */
764     obj_surface = decode_state->render_object;
765     if (pic_param->pic_fields.bits.reference_pic_flag)
766         obj_surface->flags |= SURFACE_REFERENCED;
767     else
768         obj_surface->flags &= ~SURFACE_REFERENCED;
769
770     avc_ensure_surface_bo(ctx, decode_state, obj_surface, pic_param);
771     gen6_mfd_init_avc_surface(ctx, pic_param, obj_surface);
772
773     dri_bo_unreference(gen6_mfd_context->post_deblocking_output.bo);
774     gen6_mfd_context->post_deblocking_output.bo = obj_surface->bo;
775     dri_bo_reference(gen6_mfd_context->post_deblocking_output.bo);
776     gen6_mfd_context->post_deblocking_output.valid = enable_avc_ildb;
777
778     dri_bo_unreference(gen6_mfd_context->pre_deblocking_output.bo);
779     gen6_mfd_context->pre_deblocking_output.bo = obj_surface->bo;
780     dri_bo_reference(gen6_mfd_context->pre_deblocking_output.bo);
781     gen6_mfd_context->pre_deblocking_output.valid = !enable_avc_ildb;
782
783     dri_bo_unreference(gen6_mfd_context->intra_row_store_scratch_buffer.bo);
784     bo = dri_bo_alloc(i965->intel.bufmgr,
785                       "intra row store",
786                       width_in_mbs * 64,
787                       0x1000);
788     assert(bo);
789     gen6_mfd_context->intra_row_store_scratch_buffer.bo = bo;
790     gen6_mfd_context->intra_row_store_scratch_buffer.valid = 1;
791
792     dri_bo_unreference(gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.bo);
793     bo = dri_bo_alloc(i965->intel.bufmgr,
794                       "deblocking filter row store",
795                       width_in_mbs * 64 * 4,
796                       0x1000);
797     assert(bo);
798     gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.bo = bo;
799     gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.valid = 1;
800
801     dri_bo_unreference(gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo);
802     bo = dri_bo_alloc(i965->intel.bufmgr,
803                       "bsd mpc row store",
804                       width_in_mbs * 96,
805                       0x1000);
806     assert(bo);
807     gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo = bo;
808     gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.valid = 1;
809
810     dri_bo_unreference(gen6_mfd_context->mpr_row_store_scratch_buffer.bo);
811     bo = dri_bo_alloc(i965->intel.bufmgr,
812                       "mpr row store",
813                       width_in_mbs * 64,
814                       0x1000);
815     assert(bo);
816     gen6_mfd_context->mpr_row_store_scratch_buffer.bo = bo;
817     gen6_mfd_context->mpr_row_store_scratch_buffer.valid = 1;
818
819     gen6_mfd_context->bitplane_read_buffer.valid = 0;
820 }
821
822 static void
823 gen6_mfd_avc_decode_picture(VADriverContextP ctx,
824                             struct decode_state *decode_state,
825                             struct gen6_mfd_context *gen6_mfd_context)
826 {
827     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
828     VAPictureParameterBufferH264 *pic_param;
829     VASliceParameterBufferH264 *slice_param, *next_slice_param, *next_slice_group_param;
830     dri_bo *slice_data_bo;
831     int i, j;
832
833     assert(decode_state->pic_param && decode_state->pic_param->buffer);
834     pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
835     gen6_mfd_avc_decode_init(ctx, decode_state, gen6_mfd_context);
836
837     intel_batchbuffer_start_atomic_bcs(batch, 0x1000);
838     intel_batchbuffer_emit_mi_flush(batch);
839     gen6_mfd_pipe_mode_select(ctx, decode_state, MFX_FORMAT_AVC, gen6_mfd_context);
840     gen6_mfd_surface_state(ctx, decode_state, MFX_FORMAT_AVC, gen6_mfd_context);
841     gen6_mfd_pipe_buf_addr_state(ctx, decode_state, MFX_FORMAT_AVC, gen6_mfd_context);
842     gen6_mfd_bsp_buf_base_addr_state(ctx, decode_state, MFX_FORMAT_AVC, gen6_mfd_context);
843     gen6_mfd_avc_img_state(ctx, decode_state, gen6_mfd_context);
844     gen6_mfd_avc_qm_state(ctx, decode_state, gen6_mfd_context);
845
846     for (j = 0; j < decode_state->num_slice_params; j++) {
847         assert(decode_state->slice_params && decode_state->slice_params[j]->buffer);
848         slice_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j]->buffer;
849         slice_data_bo = decode_state->slice_datas[j]->bo;
850         gen6_mfd_ind_obj_base_addr_state(ctx, slice_data_bo, MFX_FORMAT_AVC, gen6_mfd_context);
851
852         if (j == decode_state->num_slice_params - 1)
853             next_slice_group_param = NULL;
854         else
855             next_slice_group_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j + 1]->buffer;
856
857         if (j == 0 &&
858             slice_param->first_mb_in_slice)
859             gen6_mfd_avc_phantom_slice_first(ctx, pic_param, slice_param, gen6_mfd_context);
860
861         for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
862             assert(slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_ALL);
863             assert((slice_param->slice_type == SLICE_TYPE_I) ||
864                    (slice_param->slice_type == SLICE_TYPE_SI) ||
865                    (slice_param->slice_type == SLICE_TYPE_P) ||
866                    (slice_param->slice_type == SLICE_TYPE_SP) ||
867                    (slice_param->slice_type == SLICE_TYPE_B));
868
869             if (i < decode_state->slice_params[j]->num_elements - 1)
870                 next_slice_param = slice_param + 1;
871             else
872                 next_slice_param = next_slice_group_param;
873
874             gen6_mfd_avc_directmode_state(ctx, decode_state, pic_param, slice_param, gen6_mfd_context);
875             gen6_mfd_avc_slice_state(ctx, pic_param, slice_param, next_slice_param, gen6_mfd_context);
876             gen6_mfd_avc_ref_idx_state(ctx, pic_param, slice_param, gen6_mfd_context);
877             gen6_mfd_avc_weightoffset_state(ctx, pic_param, slice_param, gen6_mfd_context);
878             gen6_mfd_avc_bsd_object(ctx, pic_param, slice_param, slice_data_bo, gen6_mfd_context);
879             slice_param++;
880         }
881     }
882
883     gen6_mfd_avc_phantom_slice_last(ctx, pic_param, gen6_mfd_context);
884     intel_batchbuffer_end_atomic(batch);
885     intel_batchbuffer_flush(batch);
886 }
887
888 static void
889 gen6_mfd_mpeg2_decode_init(VADriverContextP ctx,
890                            struct decode_state *decode_state,
891                            struct gen6_mfd_context *gen6_mfd_context)
892 {
893     VAPictureParameterBufferMPEG2 *pic_param;
894     struct i965_driver_data *i965 = i965_driver_data(ctx);
895     struct object_surface *obj_surface;
896     dri_bo *bo;
897     unsigned int width_in_mbs;
898
899     assert(decode_state->pic_param && decode_state->pic_param->buffer);
900     pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
901     width_in_mbs = ALIGN(pic_param->horizontal_size, 16) / 16;
902
903     mpeg2_set_reference_surfaces(
904         ctx,
905         gen6_mfd_context->reference_surface,
906         decode_state,
907         pic_param
908     );
909
910     /* Current decoded picture */
911     obj_surface = decode_state->render_object;
912     i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
913
914     dri_bo_unreference(gen6_mfd_context->pre_deblocking_output.bo);
915     gen6_mfd_context->pre_deblocking_output.bo = obj_surface->bo;
916     dri_bo_reference(gen6_mfd_context->pre_deblocking_output.bo);
917     gen6_mfd_context->pre_deblocking_output.valid = 1;
918
919     dri_bo_unreference(gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo);
920     bo = dri_bo_alloc(i965->intel.bufmgr,
921                       "bsd mpc row store",
922                       width_in_mbs * 96,
923                       0x1000);
924     assert(bo);
925     gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo = bo;
926     gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.valid = 1;
927
928     gen6_mfd_context->post_deblocking_output.valid = 0;
929     gen6_mfd_context->intra_row_store_scratch_buffer.valid = 0;
930     gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.valid = 0;
931     gen6_mfd_context->mpr_row_store_scratch_buffer.valid = 0;
932     gen6_mfd_context->bitplane_read_buffer.valid = 0;
933 }
934
935 static void
936 gen6_mfd_mpeg2_pic_state(VADriverContextP ctx,
937                          struct decode_state *decode_state,
938                          struct gen6_mfd_context *gen6_mfd_context)
939 {
940     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
941     VAPictureParameterBufferMPEG2 *pic_param;
942     unsigned int tff, pic_structure;
943
944     assert(decode_state->pic_param && decode_state->pic_param->buffer);
945     pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
946
947     pic_structure = pic_param->picture_coding_extension.bits.picture_structure;
948     if (pic_structure == MPEG_FRAME)
949         tff = pic_param->picture_coding_extension.bits.top_field_first;
950     else
951         tff = !(pic_param->picture_coding_extension.bits.is_first_field ^
952                 (pic_structure & MPEG_TOP_FIELD));
953
954     BEGIN_BCS_BATCH(batch, 4);
955     OUT_BCS_BATCH(batch, MFX_MPEG2_PIC_STATE | (4 - 2));
956     OUT_BCS_BATCH(batch,
957                   (pic_param->f_code & 0xf) << 28 | /* f_code[1][1] */
958                   ((pic_param->f_code >> 4) & 0xf) << 24 | /* f_code[1][0] */
959                   ((pic_param->f_code >> 8) & 0xf) << 20 | /* f_code[0][1] */
960                   ((pic_param->f_code >> 12) & 0xf) << 16 | /* f_code[0][0] */
961                   pic_param->picture_coding_extension.bits.intra_dc_precision << 14 |
962                   pic_param->picture_coding_extension.bits.picture_structure << 12 |
963                   tff << 11 |
964                   pic_param->picture_coding_extension.bits.frame_pred_frame_dct << 10 |
965                   pic_param->picture_coding_extension.bits.concealment_motion_vectors << 9 |
966                   pic_param->picture_coding_extension.bits.q_scale_type << 8 |
967                   pic_param->picture_coding_extension.bits.intra_vlc_format << 7 |
968                   pic_param->picture_coding_extension.bits.alternate_scan << 6);
969     OUT_BCS_BATCH(batch,
970                   pic_param->picture_coding_type << 9);
971     OUT_BCS_BATCH(batch,
972                   (ALIGN(pic_param->vertical_size, 16) / 16) << 16 |
973                   (ALIGN(pic_param->horizontal_size, 16) / 16));
974     ADVANCE_BCS_BATCH(batch);
975 }
976
977 static void
978 gen6_mfd_mpeg2_qm_state(VADriverContextP ctx,
979                         struct decode_state *decode_state,
980                         struct gen6_mfd_context *gen6_mfd_context)
981 {
982     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
983     VAIQMatrixBufferMPEG2 * const gen_iq_matrix = &gen6_mfd_context->iq_matrix.mpeg2;
984     int i, j;
985
986     /* Update internal QM state */
987     if (decode_state->iq_matrix && decode_state->iq_matrix->buffer) {
988         VAIQMatrixBufferMPEG2 * const iq_matrix =
989             (VAIQMatrixBufferMPEG2 *)decode_state->iq_matrix->buffer;
990
991         gen_iq_matrix->load_intra_quantiser_matrix =
992             iq_matrix->load_intra_quantiser_matrix;
993         if (iq_matrix->load_intra_quantiser_matrix) {
994             for (j = 0; j < 64; j++)
995                 gen_iq_matrix->intra_quantiser_matrix[zigzag_direct[j]] =
996                     iq_matrix->intra_quantiser_matrix[j];
997         }
998
999         gen_iq_matrix->load_non_intra_quantiser_matrix =
1000             iq_matrix->load_non_intra_quantiser_matrix;
1001         if (iq_matrix->load_non_intra_quantiser_matrix) {
1002             for (j = 0; j < 64; j++)
1003                 gen_iq_matrix->non_intra_quantiser_matrix[zigzag_direct[j]] =
1004                     iq_matrix->non_intra_quantiser_matrix[j];
1005         }
1006     }
1007
1008     /* Commit QM state to HW */
1009     for (i = 0; i < 2; i++) {
1010         unsigned char *qm = NULL;
1011
1012         if (i == 0) {
1013             if (gen_iq_matrix->load_intra_quantiser_matrix)
1014                 qm = gen_iq_matrix->intra_quantiser_matrix;
1015         } else {
1016             if (gen_iq_matrix->load_non_intra_quantiser_matrix)
1017                 qm = gen_iq_matrix->non_intra_quantiser_matrix;
1018         }
1019
1020         if (!qm)
1021             continue;
1022
1023         BEGIN_BCS_BATCH(batch, 18);
1024         OUT_BCS_BATCH(batch, MFX_MPEG2_QM_STATE | (18 - 2));
1025         OUT_BCS_BATCH(batch, i);
1026         intel_batchbuffer_data(batch, qm, 64);
1027         ADVANCE_BCS_BATCH(batch);
1028     }
1029 }
1030
1031 static void
1032 gen6_mfd_mpeg2_bsd_object(VADriverContextP ctx,
1033                           VAPictureParameterBufferMPEG2 *pic_param,
1034                           VASliceParameterBufferMPEG2 *slice_param,
1035                           VASliceParameterBufferMPEG2 *next_slice_param,
1036                           struct gen6_mfd_context *gen6_mfd_context)
1037 {
1038     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
1039     unsigned int width_in_mbs = ALIGN(pic_param->horizontal_size, 16) / 16;
1040     int mb_count, vpos0, hpos0, vpos1, hpos1, is_field_pic_wa, is_field_pic = 0;
1041
1042     if (pic_param->picture_coding_extension.bits.picture_structure == MPEG_TOP_FIELD ||
1043         pic_param->picture_coding_extension.bits.picture_structure == MPEG_BOTTOM_FIELD)
1044         is_field_pic = 1;
1045     is_field_pic_wa = is_field_pic &&
1046                       gen6_mfd_context->wa_mpeg2_slice_vertical_position > 0;
1047
1048     vpos0 = slice_param->slice_vertical_position / (1 + is_field_pic_wa);
1049     hpos0 = slice_param->slice_horizontal_position;
1050
1051     if (next_slice_param == NULL) {
1052         vpos1 = ALIGN(pic_param->vertical_size, 16) / 16 / (1 + is_field_pic);
1053         hpos1 = 0;
1054     } else {
1055         vpos1 = next_slice_param->slice_vertical_position / (1 + is_field_pic_wa);
1056         hpos1 = next_slice_param->slice_horizontal_position;
1057     }
1058
1059     mb_count = (vpos1 * width_in_mbs + hpos1) - (vpos0 * width_in_mbs + hpos0);
1060
1061     BEGIN_BCS_BATCH(batch, 5);
1062     OUT_BCS_BATCH(batch, MFD_MPEG2_BSD_OBJECT | (5 - 2));
1063     OUT_BCS_BATCH(batch,
1064                   slice_param->slice_data_size - (slice_param->macroblock_offset >> 3));
1065     OUT_BCS_BATCH(batch,
1066                   slice_param->slice_data_offset + (slice_param->macroblock_offset >> 3));
1067     OUT_BCS_BATCH(batch,
1068                   hpos0 << 24 |
1069                   vpos0 << 16 |
1070                   mb_count << 8 |
1071                   (next_slice_param == NULL) << 5 |
1072                   (next_slice_param == NULL) << 3 |
1073                   (slice_param->macroblock_offset & 0x7));
1074     OUT_BCS_BATCH(batch,
1075                   slice_param->quantiser_scale_code << 24);
1076     ADVANCE_BCS_BATCH(batch);
1077 }
1078
1079 static void
1080 gen6_mfd_mpeg2_decode_picture(VADriverContextP ctx,
1081                               struct decode_state *decode_state,
1082                               struct gen6_mfd_context *gen6_mfd_context)
1083 {
1084     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
1085     VAPictureParameterBufferMPEG2 *pic_param;
1086     VASliceParameterBufferMPEG2 *slice_param, *next_slice_param;
1087     dri_bo *slice_data_bo;
1088     int group_idx = 0, pre_group_idx = -1, element_idx = 0;
1089
1090     assert(decode_state->pic_param && decode_state->pic_param->buffer);
1091     pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
1092
1093     gen6_mfd_mpeg2_decode_init(ctx, decode_state, gen6_mfd_context);
1094     intel_batchbuffer_start_atomic_bcs(batch, 0x1000);
1095     intel_batchbuffer_emit_mi_flush(batch);
1096     gen6_mfd_pipe_mode_select(ctx, decode_state, MFX_FORMAT_MPEG2, gen6_mfd_context);
1097     gen6_mfd_surface_state(ctx, decode_state, MFX_FORMAT_MPEG2, gen6_mfd_context);
1098     gen6_mfd_pipe_buf_addr_state(ctx, decode_state, MFX_FORMAT_MPEG2, gen6_mfd_context);
1099     gen6_mfd_bsp_buf_base_addr_state(ctx, decode_state, MFX_FORMAT_MPEG2, gen6_mfd_context);
1100     gen6_mfd_mpeg2_pic_state(ctx, decode_state, gen6_mfd_context);
1101     gen6_mfd_mpeg2_qm_state(ctx, decode_state, gen6_mfd_context);
1102
1103     if (gen6_mfd_context->wa_mpeg2_slice_vertical_position < 0)
1104         gen6_mfd_context->wa_mpeg2_slice_vertical_position =
1105             mpeg2_wa_slice_vertical_position(decode_state, pic_param);
1106
1107     slice_param = (VASliceParameterBufferMPEG2 *)decode_state->slice_params[group_idx]->buffer;
1108
1109     for (; slice_param;) {
1110         if (pre_group_idx != group_idx) {
1111             slice_data_bo = decode_state->slice_datas[group_idx]->bo;
1112             gen6_mfd_ind_obj_base_addr_state(ctx, slice_data_bo, MFX_FORMAT_MPEG2, gen6_mfd_context);
1113             pre_group_idx = group_idx;
1114         }
1115
1116         next_slice_param = intel_mpeg2_find_next_slice(decode_state, pic_param, slice_param, &group_idx, &element_idx);
1117         gen6_mfd_mpeg2_bsd_object(ctx, pic_param, slice_param, next_slice_param, gen6_mfd_context);
1118         slice_param = next_slice_param;
1119     }
1120
1121     intel_batchbuffer_end_atomic(batch);
1122     intel_batchbuffer_flush(batch);
1123 }
1124
1125 static const int va_to_gen6_vc1_mv[4] = {
1126     1, /* 1-MV */
1127     2, /* 1-MV half-pel */
1128     3, /* 1-MV half-pef bilinear */
1129     0, /* Mixed MV */
1130 };
1131
1132 static const int b_picture_scale_factor[21] = {
1133     128, 85,  170, 64,  192,
1134     51,  102, 153, 204, 43,
1135     215, 37,  74,  111, 148,
1136     185, 222, 32,  96,  160,
1137     224,
1138 };
1139
1140 static const int va_to_gen6_vc1_condover[3] = {
1141     0,
1142     2,
1143     3
1144 };
1145
1146 static const int va_to_gen6_vc1_profile[4] = {
1147     GEN6_VC1_SIMPLE_PROFILE,
1148     GEN6_VC1_MAIN_PROFILE,
1149     GEN6_VC1_RESERVED_PROFILE,
1150     GEN6_VC1_ADVANCED_PROFILE
1151 };
1152
1153 static void
1154 gen6_mfd_free_vc1_surface(void **data)
1155 {
1156     struct gen6_vc1_surface *gen6_vc1_surface = *data;
1157
1158     if (!gen6_vc1_surface)
1159         return;
1160
1161     dri_bo_unreference(gen6_vc1_surface->dmv);
1162     free(gen6_vc1_surface);
1163     *data = NULL;
1164 }
1165
1166 static void
1167 gen6_mfd_init_vc1_surface(VADriverContextP ctx,
1168                           VAPictureParameterBufferVC1 *pic_param,
1169                           struct object_surface *obj_surface)
1170 {
1171     struct i965_driver_data *i965 = i965_driver_data(ctx);
1172     struct gen6_vc1_surface *gen6_vc1_surface = obj_surface->private_data;
1173     int height_in_mbs = ALIGN(obj_surface->orig_height, 16) / 16;
1174
1175     obj_surface->free_private_data = gen6_mfd_free_vc1_surface;
1176
1177     if (!gen6_vc1_surface) {
1178         gen6_vc1_surface = calloc(sizeof(struct gen6_vc1_surface), 1);
1179
1180         if (!gen6_vc1_surface)
1181             return;
1182
1183         assert((obj_surface->size & 0x3f) == 0);
1184         obj_surface->private_data = gen6_vc1_surface;
1185     }
1186
1187     gen6_vc1_surface->picture_type = pic_param->picture_fields.bits.picture_type;
1188     gen6_vc1_surface->intensity_compensation = 0;
1189     gen6_vc1_surface->luma_scale = 0;
1190     gen6_vc1_surface->luma_shift = 0;
1191
1192     /*
1193      * The Direct MV buffer is scalable with frame height, but
1194      * does not scale with frame width as the hardware assumes
1195      * that frame width is fixed at 128 MBs.
1196      */
1197
1198     if (gen6_vc1_surface->dmv == NULL) {
1199         gen6_vc1_surface->dmv = dri_bo_alloc(i965->intel.bufmgr,
1200                                              "direct mv w/r buffer",
1201                                              128 * height_in_mbs * 64,
1202                                              0x1000);
1203     }
1204 }
1205
1206 static void
1207 gen6_mfd_vc1_decode_init(VADriverContextP ctx,
1208                          struct decode_state *decode_state,
1209                          struct gen6_mfd_context *gen6_mfd_context)
1210 {
1211     VAPictureParameterBufferVC1 *pic_param;
1212     struct i965_driver_data *i965 = i965_driver_data(ctx);
1213     struct object_surface *obj_surface;
1214     dri_bo *bo;
1215     int width_in_mbs;
1216     int picture_type;
1217     int intensity_compensation;
1218
1219     assert(decode_state->pic_param && decode_state->pic_param->buffer);
1220     pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
1221     width_in_mbs = ALIGN(pic_param->coded_width, 16) / 16;
1222     picture_type = pic_param->picture_fields.bits.picture_type;
1223     intensity_compensation = (pic_param->mv_fields.bits.mv_mode == VAMvModeIntensityCompensation);
1224
1225     intel_update_vc1_frame_store_index(ctx,
1226                                        decode_state,
1227                                        pic_param,
1228                                        gen6_mfd_context->reference_surface);
1229
1230     /* Forward reference picture */
1231     obj_surface = decode_state->reference_objects[0];
1232     if (pic_param->forward_reference_picture != VA_INVALID_ID &&
1233         obj_surface &&
1234         obj_surface->private_data) {
1235         if (picture_type == 1 && intensity_compensation) { /* P picture */
1236             struct gen6_vc1_surface *gen6_vc1_surface = obj_surface->private_data;
1237
1238             gen6_vc1_surface->intensity_compensation = intensity_compensation;
1239             gen6_vc1_surface->luma_scale = pic_param->luma_scale;
1240             gen6_vc1_surface->luma_shift = pic_param->luma_shift;
1241         }
1242     }
1243
1244     /* Current decoded picture */
1245     obj_surface = decode_state->render_object;
1246     i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
1247     gen6_mfd_init_vc1_surface(ctx, pic_param, obj_surface);
1248
1249     dri_bo_unreference(gen6_mfd_context->post_deblocking_output.bo);
1250     gen6_mfd_context->post_deblocking_output.bo = obj_surface->bo;
1251     dri_bo_reference(gen6_mfd_context->post_deblocking_output.bo);
1252
1253     dri_bo_unreference(gen6_mfd_context->pre_deblocking_output.bo);
1254     gen6_mfd_context->pre_deblocking_output.bo = obj_surface->bo;
1255     dri_bo_reference(gen6_mfd_context->pre_deblocking_output.bo);
1256
1257     if (picture_type == GEN6_VC1_SKIPPED_PICTURE) {
1258         gen6_mfd_context->post_deblocking_output.valid = 0;
1259         gen6_mfd_context->pre_deblocking_output.valid = 1;
1260     } else {
1261         gen6_mfd_context->post_deblocking_output.valid = pic_param->entrypoint_fields.bits.loopfilter;
1262         gen6_mfd_context->pre_deblocking_output.valid = !pic_param->entrypoint_fields.bits.loopfilter;
1263     }
1264
1265     dri_bo_unreference(gen6_mfd_context->intra_row_store_scratch_buffer.bo);
1266     bo = dri_bo_alloc(i965->intel.bufmgr,
1267                       "intra row store",
1268                       width_in_mbs * 64,
1269                       0x1000);
1270     assert(bo);
1271     gen6_mfd_context->intra_row_store_scratch_buffer.bo = bo;
1272     gen6_mfd_context->intra_row_store_scratch_buffer.valid = 1;
1273
1274     dri_bo_unreference(gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.bo);
1275     bo = dri_bo_alloc(i965->intel.bufmgr,
1276                       "deblocking filter row store",
1277                       width_in_mbs * 7 * 64,
1278                       0x1000);
1279     assert(bo);
1280     gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.bo = bo;
1281     gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.valid = 1;
1282
1283     dri_bo_unreference(gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo);
1284     bo = dri_bo_alloc(i965->intel.bufmgr,
1285                       "bsd mpc row store",
1286                       width_in_mbs * 96,
1287                       0x1000);
1288     assert(bo);
1289     gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo = bo;
1290     gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.valid = 1;
1291
1292     gen6_mfd_context->mpr_row_store_scratch_buffer.valid = 0;
1293
1294     if (picture_type == GEN6_VC1_SKIPPED_PICTURE)
1295         gen6_mfd_context->bitplane_read_buffer.valid = 1;
1296     else
1297         gen6_mfd_context->bitplane_read_buffer.valid = !!(pic_param->bitplane_present.value & 0x7f);
1298     dri_bo_unreference(gen6_mfd_context->bitplane_read_buffer.bo);
1299
1300     if (gen6_mfd_context->bitplane_read_buffer.valid) {
1301         int width_in_mbs = ALIGN(pic_param->coded_width, 16) / 16;
1302         int height_in_mbs = ALIGN(pic_param->coded_height, 16) / 16;
1303         int bitplane_width = ALIGN(width_in_mbs, 2) / 2;
1304         int src_w, src_h;
1305         uint8_t *src = NULL, *dst = NULL;
1306
1307         bo = dri_bo_alloc(i965->intel.bufmgr,
1308                           "VC-1 Bitplane",
1309                           bitplane_width * height_in_mbs,
1310                           0x1000);
1311         assert(bo);
1312         gen6_mfd_context->bitplane_read_buffer.bo = bo;
1313
1314         dri_bo_map(bo, True);
1315         assert(bo->virtual);
1316         dst = bo->virtual;
1317
1318         if (picture_type == GEN6_VC1_SKIPPED_PICTURE) {
1319             for (src_h = 0; src_h < height_in_mbs; src_h++) {
1320                 for (src_w = 0; src_w < width_in_mbs; src_w++) {
1321                     int dst_index;
1322                     uint8_t src_value = 0x2;
1323
1324                     dst_index = src_w / 2;
1325                     dst[dst_index] = ((dst[dst_index] >> 4) | (src_value << 4));
1326                 }
1327
1328                 if (src_w & 1)
1329                     dst[src_w / 2] >>= 4;
1330
1331                 dst += bitplane_width;
1332             }
1333         } else {
1334             assert(decode_state->bit_plane->buffer);
1335             src = decode_state->bit_plane->buffer;
1336
1337             for (src_h = 0; src_h < height_in_mbs; src_h++) {
1338                 for (src_w = 0; src_w < width_in_mbs; src_w++) {
1339                     int src_index, dst_index;
1340                     int src_shift;
1341                     uint8_t src_value;
1342
1343                     src_index = (src_h * width_in_mbs + src_w) / 2;
1344                     src_shift = !((src_h * width_in_mbs + src_w) & 1) * 4;
1345                     src_value = ((src[src_index] >> src_shift) & 0xf);
1346
1347                     dst_index = src_w / 2;
1348                     dst[dst_index] = ((dst[dst_index] >> 4) | (src_value << 4));
1349                 }
1350
1351                 if (src_w & 1)
1352                     dst[src_w / 2] >>= 4;
1353
1354                 dst += bitplane_width;
1355             }
1356         }
1357
1358         dri_bo_unmap(bo);
1359     } else
1360         gen6_mfd_context->bitplane_read_buffer.bo = NULL;
1361 }
1362
1363 static void
1364 gen6_mfd_vc1_pic_state(VADriverContextP ctx,
1365                        struct decode_state *decode_state,
1366                        struct gen6_mfd_context *gen6_mfd_context)
1367 {
1368     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
1369     VAPictureParameterBufferVC1 *pic_param;
1370     struct object_surface *obj_surface;
1371     int alt_pquant_config = 0, alt_pquant_edge_mask = 0, alt_pq;
1372     int dquant, dquantfrm, dqprofile, dqdbedge, dqsbedge, dqbilevel;
1373     int unified_mv_mode;
1374     int ref_field_pic_polarity = 0;
1375     int scale_factor = 0;
1376     int trans_ac_y = 0;
1377     int dmv_surface_valid = 0;
1378     int brfd = 0;
1379     int fcm = 0;
1380     int picture_type;
1381     int ptype;
1382     int overlap = 0;
1383     int loopfilter = 0;
1384     int bitplane_present;
1385     int forward_mb = 0, mv_type_mb = 0, skip_mb = 0, direct_mb = 0;
1386     int overflags = 0, ac_pred = 0, field_tx = 0;
1387
1388     assert(decode_state->pic_param && decode_state->pic_param->buffer);
1389     pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
1390
1391     picture_type = pic_param->picture_fields.bits.picture_type;
1392
1393     dquant = pic_param->pic_quantizer_fields.bits.dquant;
1394     dquantfrm = pic_param->pic_quantizer_fields.bits.dq_frame;
1395     dqprofile = pic_param->pic_quantizer_fields.bits.dq_profile;
1396     dqdbedge = pic_param->pic_quantizer_fields.bits.dq_db_edge;
1397     dqsbedge = pic_param->pic_quantizer_fields.bits.dq_sb_edge;
1398     dqbilevel = pic_param->pic_quantizer_fields.bits.dq_binary_level;
1399     alt_pq = pic_param->pic_quantizer_fields.bits.alt_pic_quantizer;
1400
1401     if (dquant == 0) {
1402         alt_pquant_config = 0;
1403         alt_pquant_edge_mask = 0;
1404     } else if (dquant == 2) {
1405         alt_pquant_config = 1;
1406         alt_pquant_edge_mask = 0xf;
1407     } else {
1408         assert(dquant == 1);
1409         if (dquantfrm == 0) {
1410             alt_pquant_config = 0;
1411             alt_pquant_edge_mask = 0;
1412             alt_pq = 0;
1413         } else {
1414             assert(dquantfrm == 1);
1415             alt_pquant_config = 1;
1416
1417             switch (dqprofile) {
1418             case 3:
1419                 if (dqbilevel == 0) {
1420                     alt_pquant_config = 2;
1421                     alt_pquant_edge_mask = 0;
1422                 } else {
1423                     assert(dqbilevel == 1);
1424                     alt_pquant_config = 3;
1425                     alt_pquant_edge_mask = 0;
1426                 }
1427                 break;
1428
1429             case 0:
1430                 alt_pquant_edge_mask = 0xf;
1431                 break;
1432
1433             case 1:
1434                 if (dqdbedge == 3)
1435                     alt_pquant_edge_mask = 0x9;
1436                 else
1437                     alt_pquant_edge_mask = (0x3 << dqdbedge);
1438
1439                 break;
1440
1441             case 2:
1442                 alt_pquant_edge_mask = (0x1 << dqsbedge);
1443                 break;
1444
1445             default:
1446                 assert(0);
1447             }
1448         }
1449     }
1450
1451     if (pic_param->mv_fields.bits.mv_mode == VAMvModeIntensityCompensation) {
1452         assert(pic_param->mv_fields.bits.mv_mode2 < 4);
1453         unified_mv_mode = va_to_gen6_vc1_mv[pic_param->mv_fields.bits.mv_mode2];
1454     } else {
1455         assert(pic_param->mv_fields.bits.mv_mode < 4);
1456         unified_mv_mode = va_to_gen6_vc1_mv[pic_param->mv_fields.bits.mv_mode];
1457     }
1458
1459     if (pic_param->sequence_fields.bits.interlace == 1 &&
1460         pic_param->picture_fields.bits.frame_coding_mode != 0) { /* frame-interlace or field-interlace */
1461         /* FIXME: calculate reference field picture polarity */
1462         assert(0);
1463         ref_field_pic_polarity = 0;
1464     }
1465
1466     if (pic_param->b_picture_fraction < 21)
1467         scale_factor = b_picture_scale_factor[pic_param->b_picture_fraction];
1468
1469     if (picture_type == GEN6_VC1_SKIPPED_PICTURE) {
1470         ptype = GEN6_VC1_P_PICTURE;
1471         bitplane_present = 1;
1472     } else {
1473         ptype = pic_param->picture_fields.bits.picture_type;
1474         bitplane_present = !!(pic_param->bitplane_present.value & 0x7f);
1475         forward_mb = pic_param->raw_coding.flags.forward_mb;
1476         mv_type_mb = pic_param->raw_coding.flags.mv_type_mb;
1477         skip_mb = pic_param->raw_coding.flags.skip_mb;
1478         direct_mb = pic_param->raw_coding.flags.direct_mb;
1479         overflags = pic_param->raw_coding.flags.overflags;
1480         ac_pred = pic_param->raw_coding.flags.ac_pred;
1481         field_tx = pic_param->raw_coding.flags.field_tx;
1482         loopfilter = pic_param->entrypoint_fields.bits.loopfilter;
1483     }
1484
1485     if (picture_type == GEN6_VC1_I_PICTURE || picture_type == GEN6_VC1_BI_PICTURE) /* I picture */
1486         trans_ac_y = pic_param->transform_fields.bits.transform_ac_codingset_idx2;
1487     else {
1488         trans_ac_y = pic_param->transform_fields.bits.transform_ac_codingset_idx1;
1489         /*
1490          * 8.3.6.2.1 Transform Type Selection
1491          * If variable-sized transform coding is not enabled,
1492          * then the 8x8 transform shall be used for all blocks.
1493          * it is also MFX_VC1_PIC_STATE requirement.
1494          */
1495         if (pic_param->transform_fields.bits.variable_sized_transform_flag == 0) {
1496             pic_param->transform_fields.bits.mb_level_transform_type_flag   = 1;
1497             pic_param->transform_fields.bits.frame_level_transform_type     = 0;
1498         }
1499     }
1500
1501     if (picture_type == GEN6_VC1_B_PICTURE) {
1502         struct gen6_vc1_surface *gen6_vc1_surface = NULL;
1503
1504         obj_surface = decode_state->reference_objects[1];
1505
1506         if (obj_surface)
1507             gen6_vc1_surface = obj_surface->private_data;
1508
1509         if (gen6_vc1_surface &&
1510             gen6_vc1_surface->picture_type == GEN6_VC1_P_PICTURE)
1511             dmv_surface_valid = 1;
1512     }
1513
1514     assert(pic_param->picture_fields.bits.frame_coding_mode < 3);
1515
1516     if (pic_param->picture_fields.bits.frame_coding_mode < 2)
1517         fcm = pic_param->picture_fields.bits.frame_coding_mode;
1518     else {
1519         if (pic_param->picture_fields.bits.top_field_first)
1520             fcm = 2;
1521         else
1522             fcm = 3;
1523     }
1524
1525     if (picture_type == GEN6_VC1_B_PICTURE) { /* B picture */
1526         brfd = pic_param->reference_fields.bits.reference_distance;
1527         brfd = (scale_factor * brfd) >> 8;
1528         brfd = pic_param->reference_fields.bits.reference_distance - brfd - 1;
1529
1530         if (brfd < 0)
1531             brfd = 0;
1532     }
1533
1534     if (pic_param->sequence_fields.bits.overlap) {
1535         if (pic_param->sequence_fields.bits.profile == 3) { /* Advanced Profile */
1536             if (picture_type == GEN6_VC1_P_PICTURE &&
1537                 pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9) {
1538                 overlap = 1;
1539             }
1540             if (picture_type == GEN6_VC1_I_PICTURE ||
1541                 picture_type == GEN6_VC1_BI_PICTURE) {
1542                 if (pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9) {
1543                     overlap = 1;
1544                 } else if (pic_param->conditional_overlap_flag == 1 || /* all block boundaries */
1545                            pic_param->conditional_overlap_flag == 2) { /* coded by OVERFLAGSMB bitplane */
1546                     overlap = 1;
1547                 }
1548             }
1549         } else {
1550             if (pic_param->pic_quantizer_fields.bits.pic_quantizer_scale >= 9 &&
1551                 picture_type != GEN6_VC1_B_PICTURE) {
1552                 overlap = 1;
1553             }
1554         }
1555     }
1556
1557     assert(pic_param->conditional_overlap_flag < 3);
1558     assert(pic_param->mv_fields.bits.mv_table < 4); /* FIXME: interlace mode */
1559
1560     BEGIN_BCS_BATCH(batch, 6);
1561     OUT_BCS_BATCH(batch, MFX_VC1_PIC_STATE | (6 - 2));
1562     OUT_BCS_BATCH(batch,
1563                   (ALIGN(pic_param->coded_height, 16) / 16) << 16 |
1564                   (ALIGN(pic_param->coded_width, 16) / 16));
1565     OUT_BCS_BATCH(batch,
1566                   pic_param->sequence_fields.bits.syncmarker << 31 |
1567                   1 << 29 | /* concealment */
1568                   alt_pq << 24 |
1569                   loopfilter << 23 |
1570                   overlap << 22 |
1571                   (pic_param->pic_quantizer_fields.bits.quantizer == 0) << 21 | /* implicit quantizer */
1572                   pic_param->pic_quantizer_fields.bits.pic_quantizer_scale << 16 |
1573                   alt_pquant_edge_mask << 12 |
1574                   alt_pquant_config << 10 |
1575                   pic_param->pic_quantizer_fields.bits.half_qp << 9 |
1576                   pic_param->pic_quantizer_fields.bits.pic_quantizer_type << 8 |
1577                   va_to_gen6_vc1_condover[pic_param->conditional_overlap_flag] << 6 |
1578                   !pic_param->picture_fields.bits.is_first_field << 5 |
1579                   ptype << 2 |
1580                   fcm << 0);
1581     OUT_BCS_BATCH(batch,
1582                   bitplane_present << 23 |
1583                   forward_mb << 22 |
1584                   mv_type_mb << 21 |
1585                   skip_mb << 20 |
1586                   direct_mb << 19 |
1587                   overflags << 18 |
1588                   ac_pred << 17 |
1589                   field_tx << 16 |
1590                   pic_param->mv_fields.bits.extended_dmv_range << 14 |
1591                   pic_param->mv_fields.bits.extended_mv_range << 12 |
1592                   pic_param->mv_fields.bits.four_mv_switch << 11 |
1593                   pic_param->fast_uvmc_flag << 10 |
1594                   unified_mv_mode << 8 |
1595                   ref_field_pic_polarity << 6 |
1596                   pic_param->reference_fields.bits.num_reference_pictures << 5 |
1597                   pic_param->reference_fields.bits.reference_distance << 0);
1598     OUT_BCS_BATCH(batch,
1599                   scale_factor << 24 |
1600                   pic_param->mv_fields.bits.mv_table << 20 |
1601                   pic_param->mv_fields.bits.four_mv_block_pattern_table << 18 |
1602                   pic_param->mv_fields.bits.two_mv_block_pattern_table << 16 |
1603                   pic_param->transform_fields.bits.frame_level_transform_type << 12 |
1604                   pic_param->transform_fields.bits.mb_level_transform_type_flag << 11 |
1605                   pic_param->mb_mode_table << 8 |
1606                   trans_ac_y << 6 |
1607                   pic_param->transform_fields.bits.transform_ac_codingset_idx1 << 4 |
1608                   pic_param->transform_fields.bits.intra_transform_dc_table << 3 |
1609                   pic_param->cbp_table << 0);
1610     OUT_BCS_BATCH(batch,
1611                   dmv_surface_valid << 13 |
1612                   brfd << 8 |
1613                   ((ALIGN(pic_param->coded_width, 16) / 16 + 1) / 2 - 1));
1614     ADVANCE_BCS_BATCH(batch);
1615 }
1616
1617 static void
1618 gen6_mfd_vc1_pred_pipe_state(VADriverContextP ctx,
1619                              struct decode_state *decode_state,
1620                              struct gen6_mfd_context *gen6_mfd_context)
1621 {
1622     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
1623     VAPictureParameterBufferVC1 *pic_param;
1624     int picture_type;
1625     int interpolation_mode = 0;
1626     int intensitycomp_single_fwd = 0;
1627     int luma_scale1 = 0;
1628     int luma_shift1 = 0;
1629
1630     assert(decode_state->pic_param && decode_state->pic_param->buffer);
1631     pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
1632     picture_type = pic_param->picture_fields.bits.picture_type;
1633
1634     if (pic_param->mv_fields.bits.mv_mode == VAMvMode1MvHalfPelBilinear ||
1635         (pic_param->mv_fields.bits.mv_mode == VAMvModeIntensityCompensation &&
1636          pic_param->mv_fields.bits.mv_mode2 == VAMvMode1MvHalfPelBilinear))
1637         interpolation_mode = 2; /* Half-pel bilinear */
1638     else if (pic_param->mv_fields.bits.mv_mode == VAMvMode1MvHalfPel ||
1639              (pic_param->mv_fields.bits.mv_mode == VAMvModeIntensityCompensation &&
1640               pic_param->mv_fields.bits.mv_mode2 == VAMvMode1MvHalfPel))
1641         interpolation_mode = 0; /* Half-pel bicubic */
1642     else
1643         interpolation_mode = 1; /* Quarter-pel bicubic */
1644
1645     assert(decode_state->pic_param && decode_state->pic_param->buffer);
1646     pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
1647
1648     if (gen6_mfd_context->reference_surface[0].surface_id != VA_INVALID_ID) {
1649         if (picture_type == 1 || picture_type == 2) { /* P/B picture */
1650             struct gen6_vc1_surface *gen6_vc1_surface = gen6_mfd_context->reference_surface[0].obj_surface->private_data;
1651             if (gen6_vc1_surface) {
1652                 intensitycomp_single_fwd = gen6_vc1_surface->intensity_compensation;
1653                 luma_scale1 = gen6_vc1_surface->luma_scale;
1654                 luma_shift1 = gen6_vc1_surface->luma_shift;
1655             }
1656         }
1657     }
1658
1659     BEGIN_BCS_BATCH(batch, 7);
1660     OUT_BCS_BATCH(batch, MFX_VC1_PRED_PIPE_STATE | (7 - 2));
1661     OUT_BCS_BATCH(batch,
1662                   0 << 8 | /* FIXME: interlace mode */
1663                   pic_param->rounding_control << 4 |
1664                   va_to_gen6_vc1_profile[pic_param->sequence_fields.bits.profile] << 2);
1665     OUT_BCS_BATCH(batch,
1666                   luma_shift1 << 16 |
1667                   luma_scale1 << 0);
1668     OUT_BCS_BATCH(batch, 0);
1669     OUT_BCS_BATCH(batch, 0);
1670     OUT_BCS_BATCH(batch, 0);
1671     OUT_BCS_BATCH(batch,
1672                   interpolation_mode << 19 |
1673                   pic_param->fast_uvmc_flag << 18 |
1674                   0 << 17 | /* FIXME: scale up or down ??? */
1675                   pic_param->range_reduction_frame << 16 |
1676                   0 << 6 | /* FIXME: double ??? */
1677                   0 << 4 |
1678                   intensitycomp_single_fwd << 2 |
1679                   0 << 0);
1680     ADVANCE_BCS_BATCH(batch);
1681 }
1682
1683
1684 static void
1685 gen6_mfd_vc1_directmode_state(VADriverContextP ctx,
1686                               struct decode_state *decode_state,
1687                               struct gen6_mfd_context *gen6_mfd_context)
1688 {
1689     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
1690     VAPictureParameterBufferVC1 *pic_param;
1691     struct object_surface *obj_surface;
1692     dri_bo *dmv_read_buffer = NULL, *dmv_write_buffer = NULL;
1693     int picture_type;
1694
1695     pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
1696     picture_type = pic_param->picture_fields.bits.picture_type;
1697
1698     if (picture_type == GEN6_VC1_P_PICTURE ||
1699         picture_type == GEN6_VC1_SKIPPED_PICTURE) {
1700         obj_surface = decode_state->render_object;
1701         dmv_write_buffer = ((struct gen6_vc1_surface *)(obj_surface->private_data))->dmv;
1702     }
1703
1704     if (picture_type == GEN6_VC1_B_PICTURE) {
1705         obj_surface = decode_state->reference_objects[1];
1706         if (pic_param->backward_reference_picture != VA_INVALID_ID &&
1707             obj_surface) {
1708             dmv_read_buffer = ((struct gen6_vc1_surface *)(obj_surface->private_data))->dmv;
1709         }
1710     }
1711
1712     BEGIN_BCS_BATCH(batch, 3);
1713     OUT_BCS_BATCH(batch, MFX_VC1_DIRECTMODE_STATE | (3 - 2));
1714
1715     if (dmv_write_buffer)
1716         OUT_BCS_RELOC(batch, dmv_write_buffer,
1717                       I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
1718                       0);
1719     else
1720         OUT_BCS_BATCH(batch, 0);
1721
1722     if (dmv_read_buffer)
1723         OUT_BCS_RELOC(batch, dmv_read_buffer,
1724                       I915_GEM_DOMAIN_INSTRUCTION, 0,
1725                       0);
1726     else
1727         OUT_BCS_BATCH(batch, 0);
1728
1729     ADVANCE_BCS_BATCH(batch);
1730 }
1731
1732 static int
1733 gen6_mfd_vc1_get_macroblock_bit_offset(uint8_t *buf, int in_slice_data_bit_offset, int profile)
1734 {
1735     int out_slice_data_bit_offset;
1736     int slice_header_size = in_slice_data_bit_offset / 8;
1737     int i, j;
1738
1739     if (profile == 3 && slice_header_size) { /* Advanced Profile */
1740         for (i = 0, j = 0; i < slice_header_size - 1; i++, j++)
1741             if (!buf[j] && !buf[j + 1] && buf[j + 2] == 3 && buf[j + 3] < 4)
1742                     i++, j += 2;
1743
1744         if (i == slice_header_size - 1) {
1745             if (!buf[j] && !buf[j + 1] && buf[j + 2] == 3 && buf[j + 3] < 4) {
1746                 buf[j + 2] = 0;
1747                 j++;
1748             }
1749
1750             j++;
1751         }
1752
1753         out_slice_data_bit_offset = 8 * j + in_slice_data_bit_offset % 8;
1754     } else /* Simple or Main Profile */
1755         out_slice_data_bit_offset = in_slice_data_bit_offset;
1756
1757     return out_slice_data_bit_offset;
1758 }
1759
1760 static void
1761 gen6_mfd_vc1_bsd_object(VADriverContextP ctx,
1762                         VAPictureParameterBufferVC1 *pic_param,
1763                         VASliceParameterBufferVC1 *slice_param,
1764                         VASliceParameterBufferVC1 *next_slice_param,
1765                         dri_bo *slice_data_bo,
1766                         struct gen6_mfd_context *gen6_mfd_context)
1767 {
1768     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
1769     int next_slice_start_vert_pos;
1770     int macroblock_offset;
1771     uint8_t *slice_data = NULL;
1772
1773     dri_bo_map(slice_data_bo, True);
1774     slice_data = (uint8_t *)(slice_data_bo->virtual + slice_param->slice_data_offset);
1775     macroblock_offset = gen6_mfd_vc1_get_macroblock_bit_offset(slice_data,
1776                                                                slice_param->macroblock_offset,
1777                                                                pic_param->sequence_fields.bits.profile);
1778     dri_bo_unmap(slice_data_bo);
1779
1780     if (next_slice_param)
1781         next_slice_start_vert_pos = next_slice_param->slice_vertical_position;
1782     else
1783         next_slice_start_vert_pos = ALIGN(pic_param->coded_height, 16) / 16;
1784
1785     BEGIN_BCS_BATCH(batch, 4);
1786     OUT_BCS_BATCH(batch, MFD_VC1_BSD_OBJECT | (4 - 2));
1787     OUT_BCS_BATCH(batch,
1788                   slice_param->slice_data_size - (macroblock_offset >> 3));
1789     OUT_BCS_BATCH(batch,
1790                   slice_param->slice_data_offset + (macroblock_offset >> 3));
1791     OUT_BCS_BATCH(batch,
1792                   slice_param->slice_vertical_position << 24 |
1793                   next_slice_start_vert_pos << 16 |
1794                   (macroblock_offset & 0x7));
1795     ADVANCE_BCS_BATCH(batch);
1796 }
1797
1798 static void
1799 gen6_mfd_vc1_decode_picture(VADriverContextP ctx,
1800                             struct decode_state *decode_state,
1801                             struct gen6_mfd_context *gen6_mfd_context)
1802 {
1803     struct intel_batchbuffer *batch = gen6_mfd_context->base.batch;
1804     VAPictureParameterBufferVC1 *pic_param;
1805     VASliceParameterBufferVC1 *slice_param, *next_slice_param, *next_slice_group_param;
1806     dri_bo *slice_data_bo;
1807     int i, j;
1808
1809     assert(decode_state->pic_param && decode_state->pic_param->buffer);
1810     pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
1811
1812     gen6_mfd_vc1_decode_init(ctx, decode_state, gen6_mfd_context);
1813     intel_batchbuffer_start_atomic_bcs(batch, 0x1000);
1814     intel_batchbuffer_emit_mi_flush(batch);
1815     gen6_mfd_pipe_mode_select(ctx, decode_state, MFX_FORMAT_VC1, gen6_mfd_context);
1816     gen6_mfd_surface_state(ctx, decode_state, MFX_FORMAT_VC1, gen6_mfd_context);
1817     gen6_mfd_pipe_buf_addr_state(ctx, decode_state, MFX_FORMAT_VC1, gen6_mfd_context);
1818     gen6_mfd_bsp_buf_base_addr_state(ctx, decode_state, MFX_FORMAT_VC1, gen6_mfd_context);
1819     gen6_mfd_vc1_pic_state(ctx, decode_state, gen6_mfd_context);
1820     gen6_mfd_vc1_pred_pipe_state(ctx, decode_state, gen6_mfd_context);
1821     gen6_mfd_vc1_directmode_state(ctx, decode_state, gen6_mfd_context);
1822
1823     for (j = 0; j < decode_state->num_slice_params; j++) {
1824         assert(decode_state->slice_params && decode_state->slice_params[j]->buffer);
1825         slice_param = (VASliceParameterBufferVC1 *)decode_state->slice_params[j]->buffer;
1826         slice_data_bo = decode_state->slice_datas[j]->bo;
1827         gen6_mfd_ind_obj_base_addr_state(ctx, slice_data_bo, MFX_FORMAT_VC1, gen6_mfd_context);
1828
1829         if (j == decode_state->num_slice_params - 1)
1830             next_slice_group_param = NULL;
1831         else
1832             next_slice_group_param = (VASliceParameterBufferVC1 *)decode_state->slice_params[j + 1]->buffer;
1833
1834         for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
1835             assert(slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_ALL);
1836
1837             if (i < decode_state->slice_params[j]->num_elements - 1)
1838                 next_slice_param = slice_param + 1;
1839             else
1840                 next_slice_param = next_slice_group_param;
1841
1842             gen6_mfd_vc1_bsd_object(ctx, pic_param, slice_param, next_slice_param, slice_data_bo, gen6_mfd_context);
1843             slice_param++;
1844         }
1845     }
1846
1847     intel_batchbuffer_end_atomic(batch);
1848     intel_batchbuffer_flush(batch);
1849 }
1850
1851 static VAStatus
1852 gen6_mfd_decode_picture(VADriverContextP ctx,
1853                         VAProfile profile,
1854                         union codec_state *codec_state,
1855                         struct hw_context *hw_context)
1856
1857 {
1858     struct gen6_mfd_context *gen6_mfd_context = (struct gen6_mfd_context *)hw_context;
1859     struct decode_state *decode_state = &codec_state->decode;
1860     VAStatus vaStatus;
1861
1862     assert(gen6_mfd_context);
1863
1864     vaStatus = intel_decoder_sanity_check_input(ctx, profile, decode_state);
1865
1866     if (vaStatus != VA_STATUS_SUCCESS)
1867         goto out;
1868
1869     switch (profile) {
1870     case VAProfileMPEG2Simple:
1871     case VAProfileMPEG2Main:
1872         gen6_mfd_mpeg2_decode_picture(ctx, decode_state, gen6_mfd_context);
1873         break;
1874
1875     case VAProfileH264ConstrainedBaseline:
1876     case VAProfileH264Main:
1877     case VAProfileH264High:
1878     case VAProfileH264StereoHigh:
1879         gen6_mfd_avc_decode_picture(ctx, decode_state, gen6_mfd_context);
1880         break;
1881
1882     case VAProfileVC1Simple:
1883     case VAProfileVC1Main:
1884     case VAProfileVC1Advanced:
1885         gen6_mfd_vc1_decode_picture(ctx, decode_state, gen6_mfd_context);
1886         break;
1887
1888     default:
1889         assert(0);
1890         break;
1891     }
1892
1893     vaStatus = VA_STATUS_SUCCESS;
1894
1895 out:
1896     return vaStatus;
1897 }
1898
1899 static void
1900 gen6_mfd_context_destroy(void *hw_context)
1901 {
1902     struct gen6_mfd_context *gen6_mfd_context = (struct gen6_mfd_context *)hw_context;
1903
1904     dri_bo_unreference(gen6_mfd_context->post_deblocking_output.bo);
1905     gen6_mfd_context->post_deblocking_output.bo = NULL;
1906
1907     dri_bo_unreference(gen6_mfd_context->pre_deblocking_output.bo);
1908     gen6_mfd_context->pre_deblocking_output.bo = NULL;
1909
1910     dri_bo_unreference(gen6_mfd_context->intra_row_store_scratch_buffer.bo);
1911     gen6_mfd_context->intra_row_store_scratch_buffer.bo = NULL;
1912
1913     dri_bo_unreference(gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.bo);
1914     gen6_mfd_context->deblocking_filter_row_store_scratch_buffer.bo = NULL;
1915
1916     dri_bo_unreference(gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo);
1917     gen6_mfd_context->bsd_mpc_row_store_scratch_buffer.bo = NULL;
1918
1919     dri_bo_unreference(gen6_mfd_context->mpr_row_store_scratch_buffer.bo);
1920     gen6_mfd_context->mpr_row_store_scratch_buffer.bo = NULL;
1921
1922     dri_bo_unreference(gen6_mfd_context->bitplane_read_buffer.bo);
1923     gen6_mfd_context->bitplane_read_buffer.bo = NULL;
1924
1925     intel_batchbuffer_free(gen6_mfd_context->base.batch);
1926     free(gen6_mfd_context);
1927 }
1928
1929 struct hw_context *
1930 gen6_dec_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
1931 {
1932     struct intel_driver_data *intel = intel_driver_data(ctx);
1933     struct gen6_mfd_context *gen6_mfd_context = calloc(1, sizeof(struct gen6_mfd_context));
1934     int i;
1935
1936     if (!gen6_mfd_context)
1937         return NULL;
1938
1939     gen6_mfd_context->base.destroy = gen6_mfd_context_destroy;
1940     gen6_mfd_context->base.run = gen6_mfd_decode_picture;
1941     gen6_mfd_context->base.batch = intel_batchbuffer_new(intel, I915_EXEC_RENDER, 0);
1942
1943     for (i = 0; i < ARRAY_ELEMS(gen6_mfd_context->reference_surface); i++) {
1944         gen6_mfd_context->reference_surface[i].surface_id = VA_INVALID_ID;
1945         gen6_mfd_context->reference_surface[i].frame_store_id = -1;
1946         gen6_mfd_context->reference_surface[i].obj_surface = NULL;
1947     }
1948
1949     gen6_mfd_context->wa_mpeg2_slice_vertical_position = -1;
1950
1951     return (struct hw_context *)gen6_mfd_context;
1952 }