2 * Copyright © 2010-2011 Intel Corporation
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
25 * Zhou Chang <chang.zhou@intel.com>
35 #include "intel_batchbuffer.h"
36 #include "i965_defines.h"
37 #include "i965_structs.h"
38 #include "i965_drv_video.h"
39 #include "i965_encoder.h"
42 gen6_mfc_pipe_mode_select(VADriverContextP ctx,
43 struct gen6_encoder_context *gen6_encoder_context,
44 struct intel_batchbuffer *batch)
47 batch = gen6_encoder_context->base.batch;
49 BEGIN_BCS_BATCH(batch, 4);
51 OUT_BCS_BATCH(batch, MFX_PIPE_MODE_SELECT | (4 - 2));
53 (0 << 10) | /* disable Stream-Out */
54 (1 << 9) | /* Post Deblocking Output */
55 (0 << 8) | /* Pre Deblocking Output */
56 (0 << 7) | /* disable TLB prefectch */
57 (0 << 5) | /* not in stitch mode */
58 (1 << 4) | /* encoding mode */
59 (2 << 0)); /* Standard Select: AVC */
61 (0 << 20) | /* round flag in PB slice */
62 (0 << 19) | /* round flag in Intra8x8 */
63 (0 << 7) | /* expand NOA bus flag */
64 (1 << 6) | /* must be 1 */
65 (0 << 5) | /* disable clock gating for NOA */
66 (0 << 4) | /* terminate if AVC motion and POC table error occurs */
67 (0 << 3) | /* terminate if AVC mbdata error occurs */
68 (0 << 2) | /* terminate if AVC CABAC/CAVLC decode error occurs */
69 (0 << 1) | /* AVC long field motion vector */
70 (0 << 0)); /* always calculate AVC ILDB boundary strength */
71 OUT_BCS_BATCH(batch, 0);
73 ADVANCE_BCS_BATCH(batch);
77 gen7_mfc_pipe_mode_select(VADriverContextP ctx,
79 struct gen6_encoder_context *gen6_encoder_context,
80 struct intel_batchbuffer *batch)
83 batch = gen6_encoder_context->base.batch;
85 assert(standard_select == MFX_FORMAT_MPEG2 ||
86 standard_select == MFX_FORMAT_AVC);
88 BEGIN_BCS_BATCH(batch, 5);
89 OUT_BCS_BATCH(batch, MFX_PIPE_MODE_SELECT | (5 - 2));
91 (MFX_LONG_MODE << 17) | /* Must be long format for encoder */
92 (MFD_MODE_VLD << 15) | /* VLD mode */
93 (0 << 10) | /* disable Stream-Out */
94 (1 << 9) | /* Post Deblocking Output */
95 (0 << 8) | /* Pre Deblocking Output */
96 (0 << 5) | /* not in stitch mode */
97 (1 << 4) | /* encoding mode */
98 (standard_select << 0)); /* standard select: avc or mpeg2 */
100 (0 << 7) | /* expand NOA bus flag */
101 (0 << 6) | /* disable slice-level clock gating */
102 (0 << 5) | /* disable clock gating for NOA */
103 (0 << 4) | /* terminate if AVC motion and POC table error occurs */
104 (0 << 3) | /* terminate if AVC mbdata error occurs */
105 (0 << 2) | /* terminate if AVC CABAC/CAVLC decode error occurs */
108 OUT_BCS_BATCH(batch, 0);
109 OUT_BCS_BATCH(batch, 0);
111 ADVANCE_BCS_BATCH(batch);
115 gen6_mfc_surface_state(VADriverContextP ctx,
116 struct gen6_encoder_context *gen6_encoder_context,
117 struct intel_batchbuffer *batch)
119 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
122 batch = gen6_encoder_context->base.batch;
124 BEGIN_BCS_BATCH(batch, 6);
126 OUT_BCS_BATCH(batch, MFX_SURFACE_STATE | (6 - 2));
127 OUT_BCS_BATCH(batch, 0);
129 ((mfc_context->surface_state.height - 1) << 19) |
130 ((mfc_context->surface_state.width - 1) << 6));
132 (MFX_SURFACE_PLANAR_420_8 << 28) | /* 420 planar YUV surface */
133 (1 << 27) | /* must be 1 for interleave U/V, hardware requirement */
134 (0 << 22) | /* surface object control state, FIXME??? */
135 ((mfc_context->surface_state.w_pitch - 1) << 3) | /* pitch */
136 (0 << 2) | /* must be 0 for interleave U/V */
137 (1 << 1) | /* must be y-tiled */
138 (I965_TILEWALK_YMAJOR << 0)); /* tile walk, TILEWALK_YMAJOR */
140 (0 << 16) | /* must be 0 for interleave U/V */
141 (mfc_context->surface_state.h_pitch)); /* y offset for U(cb) */
142 OUT_BCS_BATCH(batch, 0);
143 ADVANCE_BCS_BATCH(batch);
147 gen7_mfc_surface_state(VADriverContextP ctx,
148 struct gen6_encoder_context *gen6_encoder_context,
149 struct intel_batchbuffer *batch)
151 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
154 batch = gen6_encoder_context->base.batch;
156 BEGIN_BCS_BATCH(batch, 6);
158 OUT_BCS_BATCH(batch, MFX_SURFACE_STATE | (6 - 2));
159 OUT_BCS_BATCH(batch, 0);
161 ((mfc_context->surface_state.height - 1) << 18) |
162 ((mfc_context->surface_state.width - 1) << 4));
164 (MFX_SURFACE_PLANAR_420_8 << 28) | /* 420 planar YUV surface */
165 (1 << 27) | /* must be 1 for interleave U/V, hardware requirement */
166 (0 << 22) | /* surface object control state, FIXME??? */
167 ((mfc_context->surface_state.w_pitch - 1) << 3) | /* pitch */
168 (0 << 2) | /* must be 0 for interleave U/V */
169 (1 << 1) | /* must be tiled */
170 (I965_TILEWALK_YMAJOR << 0)); /* tile walk, TILEWALK_YMAJOR */
172 (0 << 16) | /* must be 0 for interleave U/V */
173 (mfc_context->surface_state.h_pitch)); /* y offset for U(cb) */
174 OUT_BCS_BATCH(batch, 0);
175 ADVANCE_BCS_BATCH(batch);
179 gen6_mfc_pipe_buf_addr_state(VADriverContextP ctx,
180 struct gen6_encoder_context *gen6_encoder_context,
181 struct intel_batchbuffer *batch)
183 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
187 batch = gen6_encoder_context->base.batch;
189 BEGIN_BCS_BATCH(batch, 24);
191 OUT_BCS_BATCH(batch, MFX_PIPE_BUF_ADDR_STATE | (24 - 2));
193 OUT_BCS_BATCH(batch, 0); /* pre output addr */
195 OUT_BCS_RELOC(batch, mfc_context->post_deblocking_output.bo,
196 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
197 0); /* post output addr */
199 OUT_BCS_RELOC(batch, mfc_context->uncompressed_picture_source.bo,
200 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
201 0); /* uncompressed data */
203 OUT_BCS_BATCH(batch, 0); /* StreamOut data*/
204 OUT_BCS_RELOC(batch, mfc_context->intra_row_store_scratch_buffer.bo,
205 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
207 OUT_BCS_RELOC(batch, mfc_context->deblocking_filter_row_store_scratch_buffer.bo,
208 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
210 /* 7..22 Reference pictures*/
211 for (i = 0; i < ARRAY_ELEMS(mfc_context->reference_surfaces); i++) {
212 if ( mfc_context->reference_surfaces[i].bo != NULL) {
213 OUT_BCS_RELOC(batch, mfc_context->reference_surfaces[i].bo,
214 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
217 OUT_BCS_BATCH(batch, 0);
220 OUT_BCS_BATCH(batch, 0); /* no block status */
222 ADVANCE_BCS_BATCH(batch);
226 gen6_mfc_ind_obj_base_addr_state(VADriverContextP ctx,
227 struct gen6_encoder_context *gen6_encoder_context,
228 struct intel_batchbuffer *batch)
230 struct gen6_vme_context *vme_context = &gen6_encoder_context->vme_context;
233 batch = gen6_encoder_context->base.batch;
235 BEGIN_BCS_BATCH(batch, 11);
237 OUT_BCS_BATCH(batch, MFX_IND_OBJ_BASE_ADDR_STATE | (11 - 2));
238 OUT_BCS_BATCH(batch, 0);
239 OUT_BCS_BATCH(batch, 0);
240 /* MFX Indirect MV Object Base Address */
241 OUT_BCS_RELOC(batch, vme_context->vme_output.bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 0);
242 OUT_BCS_BATCH(batch, 0);
243 OUT_BCS_BATCH(batch, 0);
244 OUT_BCS_BATCH(batch, 0);
245 OUT_BCS_BATCH(batch, 0);
246 OUT_BCS_BATCH(batch, 0);
247 /*MFC Indirect PAK-BSE Object Base Address for Encoder*/
248 OUT_BCS_BATCH(batch, 0);
249 OUT_BCS_BATCH(batch, 0);
251 ADVANCE_BCS_BATCH(batch);
255 gen7_mfc_ind_obj_base_addr_state(VADriverContextP ctx,
256 struct gen6_encoder_context *gen6_encoder_context,
257 struct intel_batchbuffer *batch)
259 struct gen6_vme_context *vme_context = &gen6_encoder_context->vme_context;
262 batch = gen6_encoder_context->base.batch;
264 BEGIN_BCS_BATCH(batch, 11);
266 OUT_BCS_BATCH(batch, MFX_IND_OBJ_BASE_ADDR_STATE | (11 - 2));
267 OUT_BCS_BATCH(batch, 0);
268 OUT_BCS_BATCH(batch, 0);
269 /* MFX Indirect MV Object Base Address */
270 OUT_BCS_RELOC(batch, vme_context->vme_output.bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 0);
271 OUT_BCS_BATCH(batch, 0x80000000); /* must set, up to 2G */
272 OUT_BCS_BATCH(batch, 0);
273 OUT_BCS_BATCH(batch, 0);
274 OUT_BCS_BATCH(batch, 0);
275 OUT_BCS_BATCH(batch, 0);
276 /*MFC Indirect PAK-BSE Object Base Address for Encoder*/
277 OUT_BCS_BATCH(batch, 0);
278 OUT_BCS_BATCH(batch, 0x80000000); /* must set, up to 2G */
280 ADVANCE_BCS_BATCH(batch);
284 gen6_mfc_bsp_buf_base_addr_state(VADriverContextP ctx,
285 struct gen6_encoder_context *gen6_encoder_context,
286 struct intel_batchbuffer *batch)
288 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
291 batch = gen6_encoder_context->base.batch;
293 BEGIN_BCS_BATCH(batch, 4);
295 OUT_BCS_BATCH(batch, MFX_BSP_BUF_BASE_ADDR_STATE | (4 - 2));
296 OUT_BCS_RELOC(batch, mfc_context->bsd_mpc_row_store_scratch_buffer.bo,
297 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
299 OUT_BCS_BATCH(batch, 0);
300 OUT_BCS_BATCH(batch, 0);
302 ADVANCE_BCS_BATCH(batch);
306 gen6_mfc_avc_img_state(VADriverContextP ctx,
307 struct gen6_encoder_context *gen6_encoder_context,
308 struct intel_batchbuffer *batch)
310 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
311 int width_in_mbs = (mfc_context->surface_state.width + 15) / 16;
312 int height_in_mbs = (mfc_context->surface_state.height + 15) / 16;
315 batch = gen6_encoder_context->base.batch;
317 BEGIN_BCS_BATCH(batch, 13);
318 OUT_BCS_BATCH(batch, MFX_AVC_IMG_STATE | (13 - 2));
320 ((width_in_mbs * height_in_mbs) & 0xFFFF));
322 (height_in_mbs << 16) |
323 (width_in_mbs << 0));
325 (0 << 24) | /*Second Chroma QP Offset*/
326 (0 << 16) | /*Chroma QP Offset*/
327 (0 << 14) | /*Max-bit conformance Intra flag*/
328 (0 << 13) | /*Max Macroblock size conformance Inter flag*/
329 (1 << 12) | /*Should always be written as "1" */
330 (0 << 10) | /*QM Preset FLag */
331 (0 << 8) | /*Image Structure*/
332 (0 << 0) ); /*Current Decoed Image Frame Store ID, reserved in Encode mode*/
334 (0 << 16) | /*Mininum Frame size*/
335 (0 << 15) | /*Disable reading of Macroblock Status Buffer*/
336 (0 << 14) | /*Load BitStream Pointer only once, 1 slic 1 frame*/
337 (0 << 13) | /*CABAC 0 word insertion test enable*/
338 (1 << 12) | /*MVUnpackedEnable,compliant to DXVA*/
339 (1 << 10) | /*Chroma Format IDC, 4:2:0*/
340 (1 << 7) | /*0:CAVLC encoding mode,1:CABAC*/
341 (0 << 6) | /*Only valid for VLD decoding mode*/
342 (0 << 5) | /*Constrained Intra Predition Flag, from PPS*/
343 (0 << 4) | /*Direct 8x8 inference flag*/
344 (0 << 3) | /*Only 8x8 IDCT Transform Mode Flag*/
345 (1 << 2) | /*Frame MB only flag*/
346 (0 << 1) | /*MBAFF mode is in active*/
347 (0 << 0) ); /*Field picture flag*/
348 OUT_BCS_BATCH(batch, 0); /*Mainly about MB rate control and debug, just ignoring*/
349 OUT_BCS_BATCH(batch, /*Inter and Intra Conformance Max size limit*/
350 (0xBB8 << 16) | /*InterMbMaxSz*/
351 (0xEE8) ); /*IntraMbMaxSz*/
352 OUT_BCS_BATCH(batch, 0); /*Reserved*/
353 OUT_BCS_BATCH(batch, 0); /*Slice QP Delta for bitrate control*/
354 OUT_BCS_BATCH(batch, 0); /*Slice QP Delta for bitrate control*/
355 OUT_BCS_BATCH(batch, 0x8C000000);
356 OUT_BCS_BATCH(batch, 0x00010000);
357 OUT_BCS_BATCH(batch, 0);
359 ADVANCE_BCS_BATCH(batch);
363 gen7_mfc_avc_img_state(VADriverContextP ctx,
364 struct gen6_encoder_context *gen6_encoder_context,
365 struct intel_batchbuffer *batch)
367 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
368 int width_in_mbs = (mfc_context->surface_state.width + 15) / 16;
369 int height_in_mbs = (mfc_context->surface_state.height + 15) / 16;
372 batch = gen6_encoder_context->base.batch;
374 BEGIN_BCS_BATCH(batch, 16);
375 OUT_BCS_BATCH(batch, MFX_AVC_IMG_STATE | (16 - 2));
377 ((width_in_mbs * height_in_mbs) & 0xFFFF));
379 ((height_in_mbs - 1) << 16) |
380 ((width_in_mbs - 1) << 0));
382 (0 << 24) | /* Second Chroma QP Offset */
383 (0 << 16) | /* Chroma QP Offset */
384 (0 << 14) | /* Max-bit conformance Intra flag */
385 (0 << 13) | /* Max Macroblock size conformance Inter flag */
386 (0 << 12) | /* FIXME: Weighted_Pred_Flag */
387 (0 << 10) | /* FIXME: Weighted_BiPred_Idc */
388 (0 << 8) | /* FIXME: Image Structure */
389 (0 << 0) ); /* Current Decoed Image Frame Store ID, reserved in Encode mode */
391 (0 << 16) | /* Mininum Frame size */
392 (0 << 15) | /* Disable reading of Macroblock Status Buffer */
393 (0 << 14) | /* Load BitStream Pointer only once, 1 slic 1 frame */
394 (0 << 13) | /* CABAC 0 word insertion test enable */
395 (1 << 12) | /* MVUnpackedEnable,compliant to DXVA */
396 (1 << 10) | /* Chroma Format IDC, 4:2:0 */
397 (0 << 9) | /* FIXME: MbMvFormatFlag */
398 (1 << 7) | /* 0:CAVLC encoding mode,1:CABAC */
399 (0 << 6) | /* Only valid for VLD decoding mode */
400 (0 << 5) | /* Constrained Intra Predition Flag, from PPS */
401 (0 << 4) | /* Direct 8x8 inference flag */
402 (0 << 3) | /* Only 8x8 IDCT Transform Mode Flag */
403 (1 << 2) | /* Frame MB only flag */
404 (0 << 1) | /* MBAFF mode is in active */
405 (0 << 0)); /* Field picture flag */
406 OUT_BCS_BATCH(batch, 0); /* Mainly about MB rate control and debug, just ignoring */
407 OUT_BCS_BATCH(batch, /* Inter and Intra Conformance Max size limit */
408 (0xBB8 << 16) | /* InterMbMaxSz */
409 (0xEE8) ); /* IntraMbMaxSz */
410 OUT_BCS_BATCH(batch, 0); /* Reserved */
411 OUT_BCS_BATCH(batch, 0); /* Slice QP Delta for bitrate control */
412 OUT_BCS_BATCH(batch, 0); /* Slice QP Delta for bitrate control */
413 OUT_BCS_BATCH(batch, 0x8C000000);
414 OUT_BCS_BATCH(batch, 0x00010000);
415 OUT_BCS_BATCH(batch, 0);
416 OUT_BCS_BATCH(batch, 0);
417 OUT_BCS_BATCH(batch, 0);
418 OUT_BCS_BATCH(batch, 0);
420 ADVANCE_BCS_BATCH(batch);
423 static void gen6_mfc_avc_directmode_state(VADriverContextP ctx,
424 struct gen6_encoder_context *gen6_encoder_context,
425 struct intel_batchbuffer *batch)
430 batch = gen6_encoder_context->base.batch;
432 BEGIN_BCS_BATCH(batch, 69);
434 OUT_BCS_BATCH(batch, MFX_AVC_DIRECTMODE_STATE | (69 - 2));
435 //TODO: reference DMV
436 for(i = 0; i < 16; i++){
437 OUT_BCS_BATCH(batch, 0);
438 OUT_BCS_BATCH(batch, 0);
441 //TODO: current DMV just for test
443 OUT_BCS_RELOC(batch, mfc_context->direct_mv_buffers[0].bo,
444 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
447 //drm_intel_bo_pin(mfc_context->direct_mv_buffers[0].bo, 0x1000);
448 //OUT_BCS_BATCH(batch, mfc_context->direct_mv_buffers[0].bo->offset);
449 OUT_BCS_BATCH(batch, 0);
453 OUT_BCS_BATCH(batch, 0);
456 for(i = 0; i < 34; i++) {
457 OUT_BCS_BATCH(batch, 0);
460 ADVANCE_BCS_BATCH(batch);
463 static void gen6_mfc_avc_slice_state(VADriverContextP ctx,
465 struct gen6_encoder_context *gen6_encoder_context,
466 struct intel_batchbuffer *batch)
468 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
471 batch = gen6_encoder_context->base.batch;
473 BEGIN_BCS_BATCH(batch, 11);;
475 OUT_BCS_BATCH(batch, MFX_AVC_SLICE_STATE | (11 - 2) );
478 OUT_BCS_BATCH(batch, 2); /*Slice Type: I Slice*/
480 OUT_BCS_BATCH(batch, 0); /*Slice Type: P Slice*/
483 OUT_BCS_BATCH(batch, 0); /*no reference frames and pred_weight_table*/
485 OUT_BCS_BATCH(batch, 0x00010000); /*1 reference frame*/
487 OUT_BCS_BATCH(batch, (0<<24) | /*Enable deblocking operation*/
488 (26<<16) | /*Slice Quantization Parameter*/
490 OUT_BCS_BATCH(batch, 0); /*First MB X&Y , the postion of current slice*/
491 OUT_BCS_BATCH(batch, ( ((mfc_context->surface_state.height+15)/16) << 16) );
494 (0<<31) | /*RateControlCounterEnable = disable*/
495 (1<<30) | /*ResetRateControlCounter*/
496 (2<<28) | /*RC Triggle Mode = Loose Rate Control*/
497 (1<<19) | /*IsLastSlice*/
498 (0<<18) | /*BitstreamOutputFlag Compressed BitStream Output Disable Flag 0:enable 1:disable*/
499 (0<<17) | /*HeaderPresentFlag*/
500 (1<<16) | /*SliceData PresentFlag*/
501 (0<<15) | /*TailPresentFlag*/
502 (1<<13) | /*RBSP NAL TYPE*/
503 (0<<12) ); /*CabacZeroWordInsertionEnable*/
505 OUT_BCS_RELOC(batch, mfc_context->mfc_indirect_pak_bse_object.bo,
506 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
507 mfc_context->mfc_indirect_pak_bse_object.offset);
509 OUT_BCS_BATCH(batch, 0);
510 OUT_BCS_BATCH(batch, 0);
511 OUT_BCS_BATCH(batch, 0);
513 ADVANCE_BCS_BATCH(batch);
515 static void gen6_mfc_avc_qm_state(VADriverContextP ctx,
516 struct gen6_encoder_context *gen6_encoder_context,
517 struct intel_batchbuffer *batch)
522 batch = gen6_encoder_context->base.batch;
524 BEGIN_BCS_BATCH(batch, 58);
526 OUT_BCS_BATCH(batch, MFX_AVC_QM_STATE | 56);
527 OUT_BCS_BATCH(batch, 0xFF ) ;
528 for( i = 0; i < 56; i++) {
529 OUT_BCS_BATCH(batch, 0x10101010);
532 ADVANCE_BCS_BATCH(batch);
535 static void gen6_mfc_avc_fqm_state(VADriverContextP ctx,
536 struct gen6_encoder_context *gen6_encoder_context,
537 struct intel_batchbuffer *batch)
542 batch = gen6_encoder_context->base.batch;
544 BEGIN_BCS_BATCH(batch, 113);
545 OUT_BCS_BATCH(batch, MFC_AVC_FQM_STATE | (113 - 2));
547 for(i = 0; i < 112;i++) {
548 OUT_BCS_BATCH(batch, 0x10001000);
551 ADVANCE_BCS_BATCH(batch);
555 gen7_mfc_qm_state(VADriverContextP ctx,
559 struct gen6_encoder_context *gen6_encoder_context,
560 struct intel_batchbuffer *batch)
562 unsigned int qm_buffer[16];
565 batch = gen6_encoder_context->base.batch;
567 assert(qm_length <= 16);
568 assert(sizeof(*qm) == 4);
569 memcpy(qm_buffer, qm, qm_length * 4);
571 BEGIN_BCS_BATCH(batch, 18);
572 OUT_BCS_BATCH(batch, MFX_QM_STATE | (18 - 2));
573 OUT_BCS_BATCH(batch, qm_type << 0);
574 intel_batchbuffer_data(batch, qm_buffer, 16 * 4);
575 ADVANCE_BCS_BATCH(batch);
578 static void gen7_mfc_avc_qm_state(VADriverContextP ctx,
579 struct gen6_encoder_context *gen6_encoder_context,
580 struct intel_batchbuffer *batch)
582 unsigned int qm[16] = {
583 0x10101010, 0x10101010, 0x10101010, 0x10101010,
584 0x10101010, 0x10101010, 0x10101010, 0x10101010,
585 0x10101010, 0x10101010, 0x10101010, 0x10101010,
586 0x10101010, 0x10101010, 0x10101010, 0x10101010
589 gen7_mfc_qm_state(ctx, MFX_QM_AVC_4X4_INTRA_MATRIX, qm, 12, gen6_encoder_context, batch);
590 gen7_mfc_qm_state(ctx, MFX_QM_AVC_4X4_INTER_MATRIX, qm, 12, gen6_encoder_context, batch);
591 gen7_mfc_qm_state(ctx, MFX_QM_AVC_8x8_INTRA_MATRIX, qm, 16, gen6_encoder_context, batch);
592 gen7_mfc_qm_state(ctx, MFX_QM_AVC_8x8_INTER_MATRIX, qm, 16, gen6_encoder_context, batch);
596 gen7_mfc_fqm_state(VADriverContextP ctx,
600 struct gen6_encoder_context *gen6_encoder_context,
601 struct intel_batchbuffer *batch)
603 unsigned int fqm_buffer[32];
606 batch = gen6_encoder_context->base.batch;
608 assert(fqm_length <= 32);
609 assert(sizeof(*fqm) == 4);
610 memcpy(fqm_buffer, fqm, fqm_length * 4);
612 BEGIN_BCS_BATCH(batch, 34);
613 OUT_BCS_BATCH(batch, MFX_FQM_STATE | (34 - 2));
614 OUT_BCS_BATCH(batch, fqm_type << 0);
615 intel_batchbuffer_data(batch, fqm_buffer, 32 * 4);
616 ADVANCE_BCS_BATCH(batch);
619 static void gen7_mfc_avc_fqm_state(VADriverContextP ctx,
620 struct gen6_encoder_context *gen6_encoder_context,
621 struct intel_batchbuffer *batch)
623 unsigned int qm[32] = {
624 0x10001000, 0x10001000, 0x10001000, 0x10001000,
625 0x10001000, 0x10001000, 0x10001000, 0x10001000,
626 0x10001000, 0x10001000, 0x10001000, 0x10001000,
627 0x10001000, 0x10001000, 0x10001000, 0x10001000,
628 0x10001000, 0x10001000, 0x10001000, 0x10001000,
629 0x10001000, 0x10001000, 0x10001000, 0x10001000,
630 0x10001000, 0x10001000, 0x10001000, 0x10001000,
631 0x10001000, 0x10001000, 0x10001000, 0x10001000
634 gen7_mfc_fqm_state(ctx, MFX_QM_AVC_4X4_INTRA_MATRIX, qm, 24, gen6_encoder_context, batch);
635 gen7_mfc_fqm_state(ctx, MFX_QM_AVC_4X4_INTER_MATRIX, qm, 24, gen6_encoder_context, batch);
636 gen7_mfc_fqm_state(ctx, MFX_QM_AVC_8x8_INTRA_MATRIX, qm, 32, gen6_encoder_context, batch);
637 gen7_mfc_fqm_state(ctx, MFX_QM_AVC_8x8_INTER_MATRIX, qm, 32, gen6_encoder_context, batch);
640 static void gen6_mfc_avc_ref_idx_state(VADriverContextP ctx,
641 struct gen6_encoder_context *gen6_encoder_context,
642 struct intel_batchbuffer *batch)
647 batch = gen6_encoder_context->base.batch;
649 BEGIN_BCS_BATCH(batch, 10);
651 OUT_BCS_BATCH(batch, MFX_AVC_REF_IDX_STATE | 8);
652 OUT_BCS_BATCH(batch, 0); //Select L0
654 OUT_BCS_BATCH(batch, 0x80808000); //Only 1 reference
655 for(i = 0; i < 7; i++) {
656 OUT_BCS_BATCH(batch, 0x80808080);
659 ADVANCE_BCS_BATCH(batch);
663 gen6_mfc_avc_insert_object(VADriverContextP ctx, int flush_data,
664 struct gen6_encoder_context *gen6_encoder_context,
665 struct intel_batchbuffer *batch)
668 batch = gen6_encoder_context->base.batch;
670 BEGIN_BCS_BATCH(batch, 4);
672 OUT_BCS_BATCH(batch, MFC_AVC_INSERT_OBJECT | (4 -2 ) );
673 OUT_BCS_BATCH(batch, (32<<8) |
678 OUT_BCS_BATCH(batch, 0x00000003);
679 OUT_BCS_BATCH(batch, 0xABCD1234);
681 ADVANCE_BCS_BATCH(batch);
685 gen6_mfc_avc_pak_object_intra(VADriverContextP ctx, int x, int y, int end_mb, int qp,unsigned int *msg,
686 struct gen6_encoder_context *gen6_encoder_context,
687 struct intel_batchbuffer *batch)
689 int len_in_dwords = 11;
692 batch = gen6_encoder_context->base.batch;
694 BEGIN_BCS_BATCH(batch, len_in_dwords);
696 OUT_BCS_BATCH(batch, MFC_AVC_PAK_OBJECT | (len_in_dwords - 2));
697 OUT_BCS_BATCH(batch, 0);
698 OUT_BCS_BATCH(batch, 0);
700 (0 << 24) | /* PackedMvNum, Debug*/
701 (0 << 20) | /* No motion vector */
702 (1 << 19) | /* CbpDcY */
703 (1 << 18) | /* CbpDcU */
704 (1 << 17) | /* CbpDcV */
707 OUT_BCS_BATCH(batch, (0xFFFF<<16) | (y << 8) | x); /* Code Block Pattern for Y*/
708 OUT_BCS_BATCH(batch, 0x000F000F); /* Code Block Pattern */
709 OUT_BCS_BATCH(batch, (0 << 27) | (end_mb << 26) | qp); /* Last MB */
711 /*Stuff for Intra MB*/
712 OUT_BCS_BATCH(batch, msg[1]); /* We using Intra16x16 no 4x4 predmode*/
713 OUT_BCS_BATCH(batch, msg[2]);
714 OUT_BCS_BATCH(batch, msg[3]&0xFC);
716 OUT_BCS_BATCH(batch, 0x8040000); /*MaxSizeInWord and TargetSzieInWord*/
718 ADVANCE_BCS_BATCH(batch);
720 return len_in_dwords;
723 static int gen6_mfc_avc_pak_object_inter(VADriverContextP ctx, int x, int y, int end_mb, int qp, unsigned int offset,
724 struct gen6_encoder_context *gen6_encoder_context, struct intel_batchbuffer *batch)
726 int len_in_dwords = 11;
729 batch = gen6_encoder_context->base.batch;
731 BEGIN_BCS_BATCH(batch, len_in_dwords);
733 OUT_BCS_BATCH(batch, MFC_AVC_PAK_OBJECT | (len_in_dwords - 2));
735 OUT_BCS_BATCH(batch, 32); /* 32 MV*/
736 OUT_BCS_BATCH(batch, offset);
739 (1 << 24) | /* PackedMvNum, Debug*/
740 (4 << 20) | /* 8 MV, SNB don't use it*/
741 (1 << 19) | /* CbpDcY */
742 (1 << 18) | /* CbpDcU */
743 (1 << 17) | /* CbpDcV */
744 (0 << 15) | /* Transform8x8Flag = 0*/
745 (0 << 14) | /* Frame based*/
746 (0 << 13) | /* Inter MB */
747 (1 << 8) | /* MbType = P_L0_16x16 */
748 (0 << 7) | /* MBZ for frame */
750 (2 << 4) | /* MBZ for inter*/
752 (0 << 2) | /* SkipMbFlag */
753 (0 << 0)); /* InterMbMode */
755 OUT_BCS_BATCH(batch, (0xFFFF<<16) | (y << 8) | x); /* Code Block Pattern for Y*/
756 OUT_BCS_BATCH(batch, 0x000F000F); /* Code Block Pattern */
757 OUT_BCS_BATCH(batch, (0 << 27) | (end_mb << 26) | qp); /* Last MB */
759 /*Stuff for Inter MB*/
760 OUT_BCS_BATCH(batch, 0x0);
761 OUT_BCS_BATCH(batch, 0x0);
762 OUT_BCS_BATCH(batch, 0x0);
764 OUT_BCS_BATCH(batch, 0xF0020000); /*MaxSizeInWord and TargetSzieInWord*/
766 ADVANCE_BCS_BATCH(batch);
768 return len_in_dwords;
771 static void gen6_mfc_init(VADriverContextP ctx,
772 struct encode_state *encode_state,
773 struct gen6_encoder_context *gen6_encoder_context)
775 struct i965_driver_data *i965 = i965_driver_data(ctx);
776 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
779 VAEncSequenceParameterBufferH264 *pSequenceParameter = (VAEncSequenceParameterBufferH264 *)encode_state->seq_param->buffer;
780 int width_in_mbs = pSequenceParameter->picture_width_in_mbs;
781 int height_in_mbs = pSequenceParameter->picture_height_in_mbs;
783 /*Encode common setup for MFC*/
784 dri_bo_unreference(mfc_context->post_deblocking_output.bo);
785 mfc_context->post_deblocking_output.bo = NULL;
787 dri_bo_unreference(mfc_context->pre_deblocking_output.bo);
788 mfc_context->pre_deblocking_output.bo = NULL;
790 dri_bo_unreference(mfc_context->uncompressed_picture_source.bo);
791 mfc_context->uncompressed_picture_source.bo = NULL;
793 dri_bo_unreference(mfc_context->mfc_indirect_pak_bse_object.bo);
794 mfc_context->mfc_indirect_pak_bse_object.bo = NULL;
796 for (i = 0; i < NUM_MFC_DMV_BUFFERS; i++){
797 dri_bo_unreference(mfc_context->direct_mv_buffers[i].bo);
798 mfc_context->direct_mv_buffers[i].bo = NULL;
801 for (i = 0; i < MAX_MFC_REFERENCE_SURFACES; i++){
802 if (mfc_context->reference_surfaces[i].bo != NULL)
803 dri_bo_unreference(mfc_context->reference_surfaces[i].bo);
804 mfc_context->reference_surfaces[i].bo = NULL;
807 dri_bo_unreference(mfc_context->intra_row_store_scratch_buffer.bo);
808 bo = dri_bo_alloc(i965->intel.bufmgr,
813 mfc_context->intra_row_store_scratch_buffer.bo = bo;
815 dri_bo_unreference(mfc_context->deblocking_filter_row_store_scratch_buffer.bo);
816 bo = dri_bo_alloc(i965->intel.bufmgr,
818 4 * width_in_mbs * 64, /* 4 * width_in_mbs * 64 */
821 mfc_context->deblocking_filter_row_store_scratch_buffer.bo = bo;
823 dri_bo_unreference(mfc_context->bsd_mpc_row_store_scratch_buffer.bo);
824 bo = dri_bo_alloc(i965->intel.bufmgr,
826 128 * width_in_mbs, /* 2 * widht_in_mbs * 64 */
829 mfc_context->bsd_mpc_row_store_scratch_buffer.bo = bo;
832 void gen6_mfc_avc_pipeline_programing(VADriverContextP ctx,
833 struct encode_state *encode_state,
834 struct gen6_encoder_context *gen6_encoder_context)
836 struct i965_driver_data *i965 = i965_driver_data(ctx);
837 struct intel_batchbuffer *batch = gen6_encoder_context->base.batch;
838 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
839 struct gen6_vme_context *vme_context = &gen6_encoder_context->vme_context;
840 VAEncSequenceParameterBufferH264 *pSequenceParameter = (VAEncSequenceParameterBufferH264 *)encode_state->seq_param->buffer;
841 VAEncSliceParameterBuffer *pSliceParameter = (VAEncSliceParameterBuffer *)encode_state->slice_params[0]->buffer; /* FIXME: multi slices */
842 unsigned int *msg = NULL, offset = 0;
843 int emit_new_state = 1, object_len_in_bytes;
844 int is_intra = pSliceParameter->slice_flags.bits.is_intra;
845 int width_in_mbs = (mfc_context->surface_state.width + 15) / 16;
846 int height_in_mbs = (mfc_context->surface_state.height + 15) / 16;
849 intel_batchbuffer_start_atomic_bcs(batch, 0x1000);
852 dri_bo_map(vme_context->vme_output.bo , 1);
853 msg = (unsigned int *)vme_context->vme_output.bo->virtual;
856 for (y = 0; y < height_in_mbs; y++) {
857 for (x = 0; x < width_in_mbs; x++) {
858 int last_mb = (y == (height_in_mbs-1)) && ( x == (width_in_mbs-1) );
859 int qp = pSequenceParameter->initial_qp;
861 if (emit_new_state) {
862 intel_batchbuffer_emit_mi_flush(batch);
864 if (IS_GEN7(i965->intel.device_id)) {
865 gen7_mfc_pipe_mode_select(ctx, MFX_FORMAT_AVC, gen6_encoder_context, batch);
866 gen7_mfc_surface_state(ctx, gen6_encoder_context, batch);
867 gen7_mfc_ind_obj_base_addr_state(ctx, gen6_encoder_context, batch);
869 gen6_mfc_pipe_mode_select(ctx, gen6_encoder_context, batch);
870 gen6_mfc_surface_state(ctx, gen6_encoder_context, batch);
871 gen6_mfc_ind_obj_base_addr_state(ctx, gen6_encoder_context, batch);
874 gen6_mfc_pipe_buf_addr_state(ctx, gen6_encoder_context, batch);
875 gen6_mfc_bsp_buf_base_addr_state(ctx, gen6_encoder_context, batch);
877 if (IS_GEN7(i965->intel.device_id)) {
878 gen7_mfc_avc_img_state(ctx, gen6_encoder_context, batch);
879 gen7_mfc_avc_qm_state(ctx, gen6_encoder_context, batch);
880 gen7_mfc_avc_fqm_state(ctx, gen6_encoder_context, batch);
882 gen6_mfc_avc_img_state(ctx, gen6_encoder_context, batch);
883 gen6_mfc_avc_qm_state(ctx, gen6_encoder_context, batch);
884 gen6_mfc_avc_fqm_state(ctx, gen6_encoder_context, batch);
887 gen6_mfc_avc_ref_idx_state(ctx, gen6_encoder_context, batch);
888 gen6_mfc_avc_slice_state(ctx, is_intra, gen6_encoder_context, batch);
894 object_len_in_bytes = gen6_mfc_avc_pak_object_intra(ctx, x, y, last_mb, qp, msg, gen6_encoder_context, batch);
897 object_len_in_bytes = gen6_mfc_avc_pak_object_inter(ctx, x, y, last_mb, qp, offset, gen6_encoder_context, batch);
901 if (intel_batchbuffer_check_free_space(batch, object_len_in_bytes) == 0) {
902 intel_batchbuffer_end_atomic(batch);
903 intel_batchbuffer_flush(batch);
905 intel_batchbuffer_start_atomic_bcs(batch, 0x1000);
911 dri_bo_unmap(vme_context->vme_output.bo);
913 intel_batchbuffer_end_atomic(batch);
916 static VAStatus gen6_mfc_avc_prepare(VADriverContextP ctx,
917 struct encode_state *encode_state,
918 struct gen6_encoder_context *gen6_encoder_context)
920 struct i965_driver_data *i965 = i965_driver_data(ctx);
921 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
922 struct object_surface *obj_surface;
923 struct object_buffer *obj_buffer;
925 VAEncPictureParameterBufferH264 *pPicParameter = (VAEncPictureParameterBufferH264 *)encode_state->pic_param->buffer;
926 VAStatus vaStatus = VA_STATUS_SUCCESS;
928 /*Setup all the input&output object*/
929 obj_surface = SURFACE(pPicParameter->reconstructed_picture);
931 i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC('N','V','1','2'), SUBSAMPLE_YUV420);
932 mfc_context->post_deblocking_output.bo = obj_surface->bo;
933 dri_bo_reference(mfc_context->post_deblocking_output.bo);
935 mfc_context->surface_state.width = obj_surface->orig_width;
936 mfc_context->surface_state.height = obj_surface->orig_height;
937 mfc_context->surface_state.w_pitch = obj_surface->width;
938 mfc_context->surface_state.h_pitch = obj_surface->height;
940 obj_surface = SURFACE(pPicParameter->reference_picture);
942 if (obj_surface->bo != NULL) {
943 mfc_context->reference_surfaces[0].bo = obj_surface->bo;
944 dri_bo_reference(obj_surface->bo);
947 obj_surface = SURFACE(encode_state->current_render_target);
948 assert(obj_surface && obj_surface->bo);
949 mfc_context->uncompressed_picture_source.bo = obj_surface->bo;
950 dri_bo_reference(mfc_context->uncompressed_picture_source.bo);
952 obj_buffer = BUFFER (pPicParameter->coded_buf); /* FIXME: fix this later */
953 bo = obj_buffer->buffer_store->bo;
955 mfc_context->mfc_indirect_pak_bse_object.bo = bo;
956 mfc_context->mfc_indirect_pak_bse_object.offset = ALIGN(sizeof(VACodedBufferSegment), 64);
957 dri_bo_reference(mfc_context->mfc_indirect_pak_bse_object.bo);
959 /*Programing bcs pipeline*/
960 gen6_mfc_avc_pipeline_programing(ctx, encode_state, gen6_encoder_context); //filling the pipeline
965 static VAStatus gen6_mfc_run(VADriverContextP ctx,
966 struct encode_state *encode_state,
967 struct gen6_encoder_context *gen6_encoder_context)
969 struct intel_batchbuffer *batch = gen6_encoder_context->base.batch;
971 intel_batchbuffer_flush(batch); //run the pipeline
973 return VA_STATUS_SUCCESS;
976 static VAStatus gen6_mfc_stop(VADriverContextP ctx,
977 struct encode_state *encode_state,
978 struct gen6_encoder_context *gen6_encoder_context)
981 struct i965_driver_data *i965 = i965_driver_data(ctx);
982 struct gen6_mfc_context *mfc_context = &gen6_encoder_context->mfc_context;
984 VAEncPictureParameterBufferH264 *pPicParameter = (VAEncPictureParameterBufferH264 *)encode_state->pic_param->buffer;
986 struct object_surface *obj_surface = SURFACE(pPicParameter->reconstructed_picture);
987 //struct object_surface *obj_surface = SURFACE(pPicParameter->reference_picture[0]);
988 //struct object_surface *obj_surface = SURFACE(encode_state->current_render_target);
989 my_debug(obj_surface);
993 return VA_STATUS_SUCCESS;
997 gen6_mfc_avc_encode_picture(VADriverContextP ctx,
998 struct encode_state *encode_state,
999 struct gen6_encoder_context *gen6_encoder_context)
1001 gen6_mfc_init(ctx, encode_state, gen6_encoder_context);
1002 gen6_mfc_avc_prepare(ctx, encode_state, gen6_encoder_context);
1003 gen6_mfc_run(ctx, encode_state, gen6_encoder_context);
1004 gen6_mfc_stop(ctx, encode_state, gen6_encoder_context);
1006 return VA_STATUS_SUCCESS;
1010 gen6_mfc_pipeline(VADriverContextP ctx,
1012 struct encode_state *encode_state,
1013 struct gen6_encoder_context *gen6_encoder_context)
1018 case VAProfileH264Baseline:
1019 vaStatus = gen6_mfc_avc_encode_picture(ctx, encode_state, gen6_encoder_context);
1022 /* FIXME: add for other profile */
1024 vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
1031 Bool gen6_mfc_context_init(VADriverContextP ctx, struct gen6_mfc_context *mfc_context)
1036 Bool gen6_mfc_context_destroy(struct gen6_mfc_context *mfc_context)
1040 dri_bo_unreference(mfc_context->post_deblocking_output.bo);
1041 mfc_context->post_deblocking_output.bo = NULL;
1043 dri_bo_unreference(mfc_context->pre_deblocking_output.bo);
1044 mfc_context->pre_deblocking_output.bo = NULL;
1046 dri_bo_unreference(mfc_context->uncompressed_picture_source.bo);
1047 mfc_context->uncompressed_picture_source.bo = NULL;
1049 dri_bo_unreference(mfc_context->mfc_indirect_pak_bse_object.bo);
1050 mfc_context->mfc_indirect_pak_bse_object.bo = NULL;
1052 for (i = 0; i < NUM_MFC_DMV_BUFFERS; i++){
1053 dri_bo_unreference(mfc_context->direct_mv_buffers[i].bo);
1054 mfc_context->direct_mv_buffers[i].bo = NULL;
1057 dri_bo_unreference(mfc_context->intra_row_store_scratch_buffer.bo);
1058 mfc_context->intra_row_store_scratch_buffer.bo = NULL;
1060 dri_bo_unreference(mfc_context->deblocking_filter_row_store_scratch_buffer.bo);
1061 mfc_context->deblocking_filter_row_store_scratch_buffer.bo = NULL;
1063 dri_bo_unreference(mfc_context->bsd_mpc_row_store_scratch_buffer.bo);
1064 mfc_context->bsd_mpc_row_store_scratch_buffer.bo = NULL;