xvmc_vld.c 33.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright © 2009 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author:
 *    Zou Nan hai <nanhai.zou@intel.com>
 */
26
#include "intel_xvmc.h"
27
#include "i830_hwmc.h"
28 29 30 31 32 33 34 35 36
#include "i810_reg.h"
#include "brw_defines.h"
#include "brw_structs.h"

#ifndef ALIGN
#define ALIGN(m,n) (((m) + (n) - 1) & ~((n) - 1))
#endif

#define BATCH_STRUCT(x) intelBatchbufferData(&x, sizeof(x), 0)
Li Shao Hua's avatar
Li Shao Hua committed
37
#define VLD_MAX_SLICE_SIZE (32 * 1024)
38 39
#define CS_SIZE 	30
#define URB_SIZE 	384
40 41 42 43 44 45 46 47 48 49
/* idct table */
#define C0 23170
#define C1 22725
#define C2 21407
#define C3 19266
#define C4 16383
#define C5 12873
#define C6 8867
#define C7 4520
const uint32_t idct_table[] = {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	C4, C1, C2, C3, C4, C5, C6, C7,	//g5
	C4, C1, C2, C3, C4, C5, C6, C7,
	C4, C3, C6, -C7, -C4, -C1, -C2, -C5,
	C4, C3, C6, -C7, -C4, -C1, -C2, -C5,
	C4, C5, -C6, -C1, -C4, C7, C2, C3,
	C4, C5, -C6, -C1, -C4, C7, C2, C3,
	C4, C7, -C2, -C5, C4, C3, -C6, -C1,
	C4, C7, -C2, -C5, C4, C3, -C6, -C1,
	C4, -C7, -C2, C5, C4, -C3, -C6, C1,
	C4, -C7, -C2, C5, C4, -C3, -C6, C1,
	C4, -C5, -C6, C1, -C4, -C7, C2, -C3,
	C4, -C5, -C6, C1, -C4, -C7, C2, -C3,
	C4, -C3, C6, C7, -C4, C1, -C2, C5,
	C4, -C3, C6, C7, -C4, C1, -C2, C5,
	C4, -C1, C2, -C3, C4, -C5, C6, -C7,
	C4, -C1, C2, -C3, C4, -C5, C6, -C7	//g20
66
};
67 68 69 70 71 72 73 74 75

#undef C0
#undef C1
#undef C2
#undef C3
#undef C4
#undef C5
#undef C6
#undef C7
76

77
#define INTERFACE_NUM	8
78
enum interface {
79
	FRAME_INTRA = 0,
80 81 82 83 84 85
	FRAME_FRAME_PRED_FORWARD,
	FRAME_FRAME_PRED_BACKWARD,
	FRAME_FRAME_PRED_BIDIRECT,
	FRAME_FIELD_PRED_FORWARD,
	FRAME_FIELD_PRED_BACKWARD,
	FRAME_FIELD_PRED_BIDIRECT,
86
	LIB_INTERFACE
87 88
};

89
/*kernels for vld mode*/
90
static uint32_t lib_kernel[][4] = {
91
#include "shader/vld/lib.g4b"
92
};
93

94
static uint32_t ipicture_kernel[][4] = {
95
#include "shader/vld/ipicture.g4b"
96
};
97

98
static uint32_t frame_forward_kernel[][4] = {
99
#include "shader/vld/frame_forward.g4b"
100
};
101

102
static uint32_t frame_backward_kernel[][4] = {
103
#include "shader/vld/frame_backward.g4b"
104
};
105

106
static uint32_t frame_f_b_kernel[][4] = {
107
#include "shader/vld/frame_f_b.g4b"
108
};
109

110
static uint32_t field_forward_kernel[][4] = {
111
#include "shader/vld/field_forward.g4b"
112
};
113

114
static uint32_t field_backward_kernel[][4] = {
115
#include "shader/vld/field_backward.g4b"
116
};
117

118
static uint32_t field_f_b_kernel[][4] = {
119
#include "shader/vld/field_f_b.g4b"
120 121
};

122 123
/* on IGDNG */
static uint32_t lib_kernel_gen5[][4] = {
124
#include "shader/vld/lib.g4b.gen5"
125
};
126

127
static uint32_t ipicture_kernel_gen5[][4] = {
128
#include "shader/vld/ipicture.g4b.gen5"
129
};
130

131
static uint32_t frame_forward_kernel_gen5[][4] = {
132
#include "shader/vld/frame_forward.g4b.gen5"
133
};
134

135
static uint32_t frame_backward_kernel_gen5[][4] = {
136
#include "shader/vld/frame_backward.g4b.gen5"
137
};
138

139
static uint32_t frame_f_b_kernel_gen5[][4] = {
140
#include "shader/vld/frame_f_b.g4b.gen5"
141
};
142

143
static uint32_t field_forward_kernel_gen5[][4] = {
144
#include "shader/vld/field_forward.g4b.gen5"
145
};
146

147
static uint32_t field_backward_kernel_gen5[][4] = {
148
#include "shader/vld/field_backward.g4b.gen5"
149
};
150

151
static uint32_t field_f_b_kernel_gen5[][4] = {
152
#include "shader/vld/field_f_b.g4b.gen5"
153 154
};

155 156
/*kernels for mc mode*/
static uint32_t lib_kernel_idct[][4] = {
157
#include "shader/mc/lib_igd.g4b"
158
};
159

160
static uint32_t ipicture_kernel_idct[][4] = {
161
#include "shader/mc/ipicture_igd.g4b"
162
};
163

164
static uint32_t frame_forward_kernel_idct[][4] = {
165
#include "shader/mc/frame_forward_igd.g4b"
166
};
167

168
static uint32_t frame_backward_kernel_idct[][4] = {
169
#include "shader/mc/frame_backward_igd.g4b"
170
};
171

172
static uint32_t frame_f_b_kernel_idct[][4] = {
173
#include "shader/mc/frame_f_b_igd.g4b"
174
};
175

176
static uint32_t field_forward_kernel_idct[][4] = {
177
#include "shader/mc/field_forward_igd.g4b"
178
};
179

180
static uint32_t field_backward_kernel_idct[][4] = {
181
#include "shader/mc/field_backward_igd.g4b"
182
};
183

184
static uint32_t field_f_b_kernel_idct[][4] = {
185
#include "shader/mc/field_f_b_igd.g4b"
186 187
};

188 189
/* on IGDNG */
static uint32_t lib_kernel_idct_gen5[][4] = {
190
#include "shader/mc/lib_igd.g4b.gen5"
191
};
192

193
static uint32_t ipicture_kernel_idct_gen5[][4] = {
194
#include "shader/mc/ipicture_igd.g4b.gen5"
195
};
196

197
static uint32_t frame_forward_kernel_idct_gen5[][4] = {
198
#include "shader/mc/frame_forward_igd.g4b.gen5"
199
};
200

201
static uint32_t frame_backward_kernel_idct_gen5[][4] = {
202
#include "shader/mc/frame_backward_igd.g4b.gen5"
203
};
204

205
static uint32_t frame_f_b_kernel_idct_gen5[][4] = {
206
#include "shader/mc/frame_f_b_igd.g4b.gen5"
207
};
208

209
static uint32_t field_forward_kernel_idct_gen5[][4] = {
210
#include "shader/mc/field_forward_igd.g4b.gen5"
211
};
212

213
static uint32_t field_backward_kernel_idct_gen5[][4] = {
214
#include "shader/mc/field_backward_igd.g4b.gen5"
215
};
216

217
static uint32_t field_f_b_kernel_idct_gen5[][4] = {
218
#include "shader/mc/field_f_b_igd.g4b.gen5"
219 220
};

221
struct media_kernel {
222 223
	uint32_t(*bin)[4];
	int size;
224 225
};
static struct media_kernel media_kernels[] = {
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	/*kernels for vld mode */
	{ipicture_kernel, sizeof(ipicture_kernel)}
	,
	{frame_forward_kernel, sizeof(frame_forward_kernel)}
	,
	{frame_backward_kernel, sizeof(frame_backward_kernel)}
	,
	{frame_f_b_kernel, sizeof(frame_f_b_kernel)}
	,
	{field_forward_kernel, sizeof(field_forward_kernel)}
	,
	{field_backward_kernel, sizeof(field_backward_kernel)}
	,
	{field_f_b_kernel, sizeof(field_f_b_kernel)}
	,
	{lib_kernel, sizeof(lib_kernel)}
	,
	/*kernels for mc mode */
	{ipicture_kernel_idct, sizeof(ipicture_kernel_idct)}
	,
	{frame_forward_kernel_idct, sizeof(frame_forward_kernel_idct)}
	,
	{frame_backward_kernel_idct, sizeof(frame_backward_kernel_idct)}
	,
	{frame_f_b_kernel_idct, sizeof(frame_f_b_kernel_idct)}
	,
	{field_forward_kernel_idct, sizeof(field_forward_kernel_idct)}
	,
	{field_backward_kernel_idct, sizeof(field_backward_kernel_idct)}
	,
	{field_f_b_kernel_idct, sizeof(field_f_b_kernel_idct)}
	,
	{lib_kernel_idct, sizeof(lib_kernel_idct)}
259 260
};

261
static struct media_kernel media_gen5_kernels[] = {
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	/*kernels for vld mode */
	{ipicture_kernel_gen5, sizeof(ipicture_kernel_gen5)}
	,
	{frame_forward_kernel_gen5, sizeof(frame_forward_kernel_gen5)}
	,
	{frame_backward_kernel_gen5, sizeof(frame_backward_kernel_gen5)}
	,
	{frame_f_b_kernel_gen5, sizeof(frame_f_b_kernel_gen5)}
	,
	{field_forward_kernel_gen5, sizeof(field_forward_kernel_gen5)}
	,
	{field_backward_kernel_gen5, sizeof(field_backward_kernel_gen5)}
	,
	{field_f_b_kernel_gen5, sizeof(field_f_b_kernel_gen5)}
	,
	{lib_kernel_gen5, sizeof(lib_kernel_gen5)}
	,
	/*kernels for mc mode */
	{ipicture_kernel_idct_gen5, sizeof(ipicture_kernel_idct_gen5)}
	,
	{frame_forward_kernel_idct_gen5, sizeof(frame_forward_kernel_idct_gen5)}
	,
	{frame_backward_kernel_idct_gen5,
	 sizeof(frame_backward_kernel_idct_gen5)}
	,
	{frame_f_b_kernel_idct_gen5, sizeof(frame_f_b_kernel_idct_gen5)}
	,
	{field_forward_kernel_idct_gen5, sizeof(field_forward_kernel_idct_gen5)}
	,
	{field_backward_kernel_idct_gen5,
	 sizeof(field_backward_kernel_idct_gen5)}
	,
	{field_f_b_kernel_idct_gen5, sizeof(field_f_b_kernel_idct_gen5)}
	,
	{lib_kernel_idct_gen5, sizeof(lib_kernel_idct_gen5)}
297 298
};

299 300
#define MEDIA_KERNEL_NUM (sizeof(media_kernels)/sizeof(media_kernels[0]))

Li Shao Hua's avatar
Li Shao Hua committed
301
struct media_kernel_obj {
302
	dri_bo *bo;
Li Shao Hua's avatar
Li Shao Hua committed
303 304 305
};

struct interface_descriptor_obj {
306 307
	dri_bo *bo;
	struct media_kernel_obj kernels[MEDIA_KERNEL_NUM];
Li Shao Hua's avatar
Li Shao Hua committed
308 309 310
};

struct vfe_state_obj {
311 312
	dri_bo *bo;
	struct interface_descriptor_obj interface;
Li Shao Hua's avatar
Li Shao Hua committed
313 314 315
};

struct vld_state_obj {
316
	dri_bo *bo;
Li Shao Hua's avatar
Li Shao Hua committed
317 318 319
};

struct surface_obj {
320
	dri_bo *bo;
Li Shao Hua's avatar
Li Shao Hua committed
321 322 323
};

struct surface_state_obj {
324 325
	struct surface_obj surface;
	dri_bo *bo;
Li Shao Hua's avatar
Li Shao Hua committed
326 327
};

328
#define MAX_SURFACES 12
Li Shao Hua's avatar
Li Shao Hua committed
329
struct binding_table_obj {
330
	dri_bo *bo;
331
	struct surface_state_obj surface_states[MAX_SURFACES];
Li Shao Hua's avatar
Li Shao Hua committed
332 333 334
};

struct slice_data_obj {
335
	dri_bo *bo;
Li Shao Hua's avatar
Li Shao Hua committed
336 337
};

338
struct mb_data_obj {
339
	dri_bo *bo;
340 341
};

Li Shao Hua's avatar
Li Shao Hua committed
342
struct cs_state_obj {
343
	dri_bo *bo;
Li Shao Hua's avatar
Li Shao Hua committed
344 345
};

346
static struct media_state {
347 348 349 350 351
	struct vfe_state_obj vfe_state;
	struct vld_state_obj vld_state;
	struct binding_table_obj binding_table;
	struct cs_state_obj cs_object;
	struct slice_data_obj slice_data;
352
	struct mb_data_obj mb_data;
353 354
} media_state;

Li Shao Hua's avatar
Li Shao Hua committed
355 356 357
/* XvMCQMatrix * 2 + idct_table + 8 * kernel offset pointer */
#define CS_OBJECT_SIZE (32*20 + sizeof(unsigned int) * 8)
static int free_object(struct media_state *s)
358
{
359
	int i;
Li Shao Hua's avatar
Li Shao Hua committed
360 361 362
#define FREE_ONE_BO(bo) \
    if (bo) \
        drm_intel_bo_unreference(bo)
363 364 365 366 367
	FREE_ONE_BO(s->vfe_state.bo);
	FREE_ONE_BO(s->vfe_state.interface.bo);
	for (i = 0; i < MEDIA_KERNEL_NUM; i++)
		FREE_ONE_BO(s->vfe_state.interface.kernels[i].bo);
	FREE_ONE_BO(s->binding_table.bo);
368
	for (i = 0; i < MAX_SURFACES; i++)
369 370 371 372 373
		FREE_ONE_BO(s->binding_table.surface_states[i].bo);
	FREE_ONE_BO(s->slice_data.bo);
	FREE_ONE_BO(s->mb_data.bo);
	FREE_ONE_BO(s->cs_object.bo);
	FREE_ONE_BO(s->vld_state.bo);
374
}
Li Shao Hua's avatar
Li Shao Hua committed
375 376

static int alloc_object(struct media_state *s)
377
{
378 379
	int i;

380
	for (i = 0; i < MAX_SURFACES; i++) {
381 382 383 384 385 386 387 388
		s->binding_table.surface_states[i].bo =
		    drm_intel_bo_alloc(xvmc_driver->bufmgr, "surface_state",
				       sizeof(struct brw_surface_state),
				       0x1000);
		if (!s->binding_table.surface_states[i].bo)
			goto out;
	}
	return 0;
Li Shao Hua's avatar
Li Shao Hua committed
389
out:
390 391
	free_object(s);
	return BadAlloc;
392 393 394 395
}

static void flush()
{
396 397 398 399 400 401
#define FLUSH_STATE_CACHE  	1
	struct brw_mi_flush f;
	memset(&f, 0, sizeof(f));
	f.opcode = CMD_MI_FLUSH;
	f.flags = (1 << FLUSH_STATE_CACHE);
	BATCH_STRUCT(f);
402 403
}

404
static Status vfe_state(int vfe_mode)
405
{
406 407 408 409 410
	struct brw_vfe_state tmp, *vfe_state = &tmp;
	memset(vfe_state, 0, sizeof(*vfe_state));
	if (vfe_mode == VFE_VLD_MODE) {
		vfe_state->vfe0.extend_vfe_state_present = 1;
	} else {
411
		vfe_state->vfe0.extend_vfe_state_present = 0;
412 413 414 415 416 417 418
	}
	vfe_state->vfe1.vfe_mode = vfe_mode;
	vfe_state->vfe1.num_urb_entries = 1;
	vfe_state->vfe1.children_present = 0;
	vfe_state->vfe1.urb_entry_alloc_size = 2;
	vfe_state->vfe1.max_threads = 31;
	vfe_state->vfe2.interface_descriptor_base =
419
	    media_state.vfe_state.interface.bo->offset >> 4;
Li Shao Hua's avatar
Li Shao Hua committed
420

421 422
	if (media_state.vfe_state.bo)
		drm_intel_bo_unreference(media_state.vfe_state.bo);
423

424 425 426 427 428 429 430
	media_state.vfe_state.bo = drm_intel_bo_alloc(xvmc_driver->bufmgr,
						      "vfe state",
						      sizeof(struct
							     brw_vfe_state),
						      0x1000);
	if (!media_state.vfe_state.bo)
		return BadAlloc;
Li Shao Hua's avatar
Li Shao Hua committed
431

432
	drm_intel_bo_subdata(media_state.vfe_state.bo, 0, sizeof(tmp), &tmp);
Li Shao Hua's avatar
Li Shao Hua committed
433

434 435 436 437 438
	drm_intel_bo_emit_reloc(media_state.vfe_state.bo,
				offsetof(struct brw_vfe_state, vfe2),
				media_state.vfe_state.interface.bo, 0,
				I915_GEM_DOMAIN_INSTRUCTION, 0);
	return Success;
439 440
}

Li Shao Hua's avatar
Li Shao Hua committed
441
static Status interface_descriptor()
442
{
443 444
	int i;
	struct brw_interface_descriptor tmp, *desc = &tmp;
Li Shao Hua's avatar
Li Shao Hua committed
445

446 447
	if (media_state.vfe_state.interface.bo)
		drm_intel_bo_unreference(media_state.vfe_state.interface.bo);
Li Shao Hua's avatar
Li Shao Hua committed
448

449 450 451 452 453 454
	media_state.vfe_state.interface.bo =
	    drm_intel_bo_alloc(xvmc_driver->bufmgr, "interfaces",
			       MEDIA_KERNEL_NUM *
			       sizeof(struct brw_interface_descriptor), 0x1000);
	if (!media_state.vfe_state.interface.bo)
		return BadAlloc;
Li Shao Hua's avatar
Li Shao Hua committed
455

456
	for (i = 0; i < MEDIA_KERNEL_NUM; i++) {
457 458 459
		memset(desc, 0, sizeof(*desc));
		desc->desc0.grf_reg_blocks = 15;
		desc->desc0.kernel_start_pointer =
460
		    media_state.vfe_state.interface.kernels[i].bo->offset >> 6;
461 462 463 464

		desc->desc1.const_urb_entry_read_offset = 0;
		desc->desc1.const_urb_entry_read_len = 30;

465
		desc->desc3.binding_table_entry_count = MAX_SURFACES - 1;
466
		desc->desc3.binding_table_pointer =
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
		    media_state.binding_table.bo->offset >> 5;

		drm_intel_bo_subdata(media_state.vfe_state.interface.bo,
				     i * sizeof(tmp), sizeof(tmp), desc);

		drm_intel_bo_emit_reloc(media_state.vfe_state.interface.bo,
					i * sizeof(*desc) + offsetof(struct
								     brw_interface_descriptor,
								     desc0),
					media_state.vfe_state.
					interface.kernels[i].bo,
					desc->desc0.grf_reg_blocks,
					I915_GEM_DOMAIN_INSTRUCTION, 0);

		drm_intel_bo_emit_reloc(media_state.vfe_state.interface.bo,
					i * sizeof(*desc) + offsetof(struct
								     brw_interface_descriptor,
								     desc3),
					media_state.binding_table.bo,
					desc->desc3.binding_table_entry_count,
					I915_GEM_DOMAIN_INSTRUCTION, 0);
	}
	return Success;
490 491
}

492
static int setup_media_kernels(struct intel_xvmc_hw_context *ctx)
493
{
494 495 496 497 498 499
	int i;

	assert(MEDIA_KERNEL_NUM ==
	       sizeof(media_gen5_kernels) / sizeof(media_gen5_kernels[0]));

	for (i = 0; i < MEDIA_KERNEL_NUM; i++) {
500
		if (ctx->i965.is_igdng)
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
			media_state.vfe_state.interface.kernels[i].bo =
			    drm_intel_bo_alloc(xvmc_driver->bufmgr, "kernel",
					       media_gen5_kernels[i].size,
					       0x1000);
		else
			media_state.vfe_state.interface.kernels[i].bo =
			    drm_intel_bo_alloc(xvmc_driver->bufmgr, "kernels",
					       media_kernels[i].size, 0x1000);

		if (!media_state.vfe_state.interface.kernels[i].bo)
			goto out;
	}

	for (i = 0; i < MEDIA_KERNEL_NUM; i++) {
		dri_bo *bo = media_state.vfe_state.interface.kernels[i].bo;

517
		if (ctx->i965.is_igdng)
518 519 520 521 522 523 524
			drm_intel_bo_subdata(bo, 0, media_gen5_kernels[i].size,
					     media_gen5_kernels[i].bin);
		else
			drm_intel_bo_subdata(bo, 0, media_kernels[i].size,
					     media_kernels[i].bin);
	}
	return 0;
Li Shao Hua's avatar
Li Shao Hua committed
525
out:
526 527
	free_object(&media_state);
	return BadAlloc;
528 529
}

Li Shao Hua's avatar
Li Shao Hua committed
530
static Status binding_tables()
531
{
532
	unsigned int table[MAX_SURFACES];
533 534 535 536 537 538
	int i;

	if (media_state.binding_table.bo)
		drm_intel_bo_unreference(media_state.binding_table.bo);
	media_state.binding_table.bo =
	    drm_intel_bo_alloc(xvmc_driver->bufmgr, "binding_table",
539
			       MAX_SURFACES * 4, 0x1000);
540 541 542
	if (!media_state.binding_table.bo)
		return BadAlloc;

543
	for (i = 0; i < MAX_SURFACES; i++) {
544 545 546 547 548 549 550 551 552 553 554 555
		table[i] =
		    media_state.binding_table.surface_states[i].bo->offset;
		drm_intel_bo_emit_reloc(media_state.binding_table.bo,
					i * sizeof(unsigned int),
					media_state.
					binding_table.surface_states[i].bo, 0,
					I915_GEM_DOMAIN_INSTRUCTION, 0);
	}

	drm_intel_bo_subdata(media_state.binding_table.bo, 0, sizeof(table),
			     table);
	return Success;
556 557
}

558
static Status cs_init(int interface_offset)
559
{
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
	char buf[CS_OBJECT_SIZE];
	unsigned int *lib_reloc;
	int i;

	if (media_state.cs_object.bo)
		drm_intel_bo_unreference(media_state.cs_object.bo);

	media_state.cs_object.bo =
	    drm_intel_bo_alloc(xvmc_driver->bufmgr, "cs object", CS_OBJECT_SIZE,
			       64);
	if (!media_state.cs_object.bo)
		return BadAlloc;

	memcpy(buf + 32 * 4, idct_table, sizeof(idct_table));
	/* idct lib reloction */
	lib_reloc = (unsigned int *)(buf + 32 * 20);
	for (i = 0; i < 8; i++)
		lib_reloc[i] =
		    media_state.vfe_state.interface.kernels[LIB_INTERFACE +
							    interface_offset].bo->
		    offset;
	drm_intel_bo_subdata(media_state.cs_object.bo, 32 * 4,
			     32 * 16 + 8 * sizeof(unsigned int), buf + 32 * 4);

	for (i = 0; i < 8; i++)
		drm_intel_bo_emit_reloc(media_state.cs_object.bo,
					32 * 20 + sizeof(unsigned int) * i,
					media_state.vfe_state.
					interface.kernels[LIB_INTERFACE +
							  interface_offset].bo,
					0, I915_GEM_DOMAIN_INSTRUCTION, 0);

	return Success;
593 594
}

595 596
#define STRIDE(w)               (w)
#define SIZE_YUV420(w, h)       (h * (STRIDE(w) + STRIDE(w >> 1)))
597 598
static Status create_context(Display * display, XvMCContext * context,
			     int priv_count, CARD32 * priv_data)
599
{
600 601 602 603 604 605 606 607 608
	struct intel_xvmc_context *intel_ctx;
	struct intel_xvmc_hw_context *hw_ctx;
	hw_ctx = (struct intel_xvmc_hw_context *)priv_data;

	intel_ctx = calloc(1, sizeof(struct intel_xvmc_context));
	if (!intel_ctx)
		return BadAlloc;
	intel_ctx->hw = hw_ctx;
	context->privData = intel_ctx;
609 610
	intel_ctx->surface_bo_size
		= SIZE_YUV420(context->width, context->height);
Li Shao Hua's avatar
Li Shao Hua committed
611

612 613
	if (alloc_object(&media_state))
		return BadAlloc;
Li Shao Hua's avatar
Li Shao Hua committed
614

615
	if (setup_media_kernels(hw_ctx))
616 617
		return BadAlloc;
	return Success;
618 619
}

620
static Status destroy_context(Display * display, XvMCContext * context)
621
{
622 623 624 625
	struct intel_xvmc_context *intel_ctx;
	intel_ctx = context->privData;
	Xfree(intel_ctx->hw);
	free(intel_ctx);
626
	return Success;
627 628
}

629 630
static Status load_qmatrix(Display * display, XvMCContext * context,
			   const XvMCQMatrix * qmx)
631
{
632 633 634 635 636 637 638 639 640 641
	Status ret;
	ret = cs_init(0);
	if (ret != Success)
		return ret;
	drm_intel_bo_subdata(media_state.cs_object.bo, 0, 64,
			     qmx->intra_quantiser_matrix);
	drm_intel_bo_subdata(media_state.cs_object.bo, 64, 64,
			     qmx->non_intra_quantiser_matrix);

	return Success;
642 643
}

644
static Status vld_state(const XvMCMpegControl * control)
645
{
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	struct brw_vld_state tmp, *vld = &tmp;

	if (media_state.vld_state.bo)
		drm_intel_bo_unreference(media_state.vld_state.bo);
	media_state.vld_state.bo = drm_intel_bo_alloc(xvmc_driver->bufmgr,
						      "vld state",
						      sizeof(struct
							     brw_vld_state),
						      64);
	if (!media_state.vld_state.bo)
		return BadAlloc;

	memset(vld, 0, sizeof(*vld));
	vld->vld0.f_code_0_0 = control->FHMV_range + 1;
	vld->vld0.f_code_0_1 = control->FVMV_range + 1;
	vld->vld0.f_code_1_0 = control->BHMV_range + 1;
	vld->vld0.f_code_1_1 = control->BVMV_range + 1;
	vld->vld0.intra_dc_precision = control->intra_dc_precision;
	vld->vld0.picture_structure = control->picture_structure;
	vld->vld0.top_field_first = !!(control->flags & XVMC_TOP_FIELD_FIRST);
	vld->vld0.frame_predict_frame_dct =
	    !!(control->flags & XVMC_PRED_DCT_FRAME);
	vld->vld0.concealment_motion_vector =
	    !!(control->flags & XVMC_CONCEALMENT_MOTION_VECTORS);
	vld->vld0.quantizer_scale_type = !!(control->flags & XVMC_Q_SCALE_TYPE);
	vld->vld0.intra_vlc_format = !!(control->flags & XVMC_INTRA_VLC_FORMAT);
	vld->vld0.scan_order = !!(control->flags & XVMC_ALTERNATE_SCAN);

	vld->vld1.picture_coding_type = control->picture_coding_type;

	vld->desc_remap_table0.index_0 = FRAME_INTRA;
	vld->desc_remap_table0.index_1 = FRAME_FRAME_PRED_FORWARD;
	vld->desc_remap_table0.index_2 = FRAME_FIELD_PRED_FORWARD;
	vld->desc_remap_table0.index_3 = FRAME_FIELD_PRED_BIDIRECT;	/* dual prime */
	vld->desc_remap_table0.index_4 = FRAME_FRAME_PRED_BACKWARD;
	vld->desc_remap_table0.index_5 = FRAME_FIELD_PRED_BACKWARD;
	vld->desc_remap_table0.index_6 = FRAME_FRAME_PRED_BIDIRECT;
	vld->desc_remap_table0.index_7 = FRAME_FIELD_PRED_BIDIRECT;

	vld->desc_remap_table1.index_8 = FRAME_INTRA;
	vld->desc_remap_table1.index_9 = FRAME_FRAME_PRED_FORWARD;
	vld->desc_remap_table1.index_10 = FRAME_FIELD_PRED_FORWARD;
	vld->desc_remap_table1.index_11 = FRAME_FIELD_PRED_BIDIRECT;
	vld->desc_remap_table1.index_12 = FRAME_FRAME_PRED_BACKWARD;
	vld->desc_remap_table1.index_13 = FRAME_FIELD_PRED_BACKWARD;
	vld->desc_remap_table1.index_14 = FRAME_FRAME_PRED_BIDIRECT;
	vld->desc_remap_table1.index_15 = FRAME_FIELD_PRED_BIDIRECT;

	drm_intel_bo_subdata(media_state.vld_state.bo, 0, sizeof(tmp), vld);
	return Success;
696 697
}

698 699 700
static Status setup_media_surface(int index, dri_bo * bo,
				  unsigned long offset, int w, int h,
				  Bool write)
701
{
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	struct brw_surface_state tmp, *ss = &tmp;
	memset(ss, 0, sizeof(*ss));
	ss->ss0.surface_type = BRW_SURFACE_2D;
	ss->ss0.surface_format = BRW_SURFACEFORMAT_R8_SINT;
	ss->ss1.base_addr = offset + bo->offset;
	ss->ss2.width = w - 1;
	ss->ss2.height = h - 1;
	ss->ss3.pitch = w - 1;

	if (media_state.binding_table.surface_states[index].bo)
		drm_intel_bo_unreference(media_state.
					 binding_table.surface_states[index].
					 bo);

	media_state.binding_table.surface_states[index].bo =
	    drm_intel_bo_alloc(xvmc_driver->bufmgr, "surface_state",
			       sizeof(struct brw_surface_state), 0x1000);
	if (!media_state.binding_table.surface_states[index].bo)
		return BadAlloc;

	drm_intel_bo_subdata(media_state.binding_table.surface_states[index].bo,
			     0, sizeof(*ss), ss);
	drm_intel_bo_emit_reloc(media_state.binding_table.
				surface_states[index].bo,
				offsetof(struct brw_surface_state, ss1), bo,
				offset, I915_GEM_DOMAIN_RENDER,
				write ? I915_GEM_DOMAIN_RENDER : 0);
	return Success;
730 731
}

732 733 734
static Status setup_surface(struct intel_xvmc_surface *target,
			    struct intel_xvmc_surface *past,
			    struct intel_xvmc_surface *future, int w, int h)
735
{
736 737
	Status ret;
	ret = setup_media_surface(0, target->bo, 0, w, h, TRUE);
738
	if (ret != Success)
739 740
		return ret;
	ret = setup_media_surface(1, target->bo, w * h, w / 2, h / 2, TRUE);
741
	if (ret != Success)
742 743 744 745
		return ret;
	ret =
	    setup_media_surface(2, target->bo, w * h + w * h / 4, w / 2, h / 2,
				TRUE);
746
	if (ret != Success)
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
		return ret;
	if (past) {
		ret = setup_media_surface(4, past->bo, 0, w, h, FALSE);
		if (ret != Success)
			return ret;
		ret =
		    setup_media_surface(5, past->bo, w * h, w / 2, h / 2,
					FALSE);
		if (ret != Success)
			return ret;
		ret =
		    setup_media_surface(6, past->bo, w * h + w * h / 4, w / 2,
					h / 2, FALSE);
		if (ret != Success)
			return ret;
	}
	if (future) {
		ret = setup_media_surface(7, future->bo, 0, w, h, FALSE);
		if (ret != Success)
			return ret;
		ret =
		    setup_media_surface(8, future->bo, w * h, w / 2, h / 2,
					FALSE);
		if (ret != Success)
			return ret;
		ret =
		    setup_media_surface(9, future->bo, w * h + w * h / 4, w / 2,
					h / 2, FALSE);
		if (ret != Success)
			return ret;
	}
	return Success;
}

static Status begin_surface(Display * display, XvMCContext * context,
			    XvMCSurface * target,
			    XvMCSurface * past,
			    XvMCSurface * future,
			    const XvMCMpegControl * control)
{
	struct i965_xvmc_contex *i965_ctx;
788
	struct intel_xvmc_surface *priv_target, *priv_past, *priv_future;
789
	intel_xvmc_context_ptr intel_ctx = context->privData;
790 791 792 793 794 795 796
	Status ret;

	priv_target = target->privData;
	priv_past = past ? past->privData : NULL;
	priv_future = future ? future->privData : NULL;

	ret = vld_state(control);
797
	if (ret != Success)
798 799 800
		return ret;
	ret = setup_surface(priv_target, priv_past, priv_future,
			    context->width, context->height);
801
	if (ret != Success)
802 803
		return ret;
	ret = binding_tables();
804
	if (ret != Success)
805 806 807 808 809 810 811
		return ret;
	ret = interface_descriptor();
	if (ret != Success)
		return ret;
	ret = vfe_state(VFE_VLD_MODE);
	if (ret != Success)
		return ret;
812

813 814 815 816
	LOCK_HARDWARE(intel_ctx->hw_context);
	flush();
	UNLOCK_HARDWARE(intel_ctx->hw_context);
	return Success;
817 818
}

819 820
static Status put_slice(Display * display, XvMCContext * context,
			unsigned char *slice, int nbytes)
821
{
822
	return Success;
823 824
}

825
static void state_base_address(struct intel_xvmc_hw_context *ctx)
826
{
827 828
	BATCH_LOCALS;

829
	if (ctx->i965.is_igdng) {
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
		BEGIN_BATCH(8);
		OUT_BATCH(BRW_STATE_BASE_ADDRESS | 6);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		ADVANCE_BATCH();
	} else {
		BEGIN_BATCH(6);
		OUT_BATCH(BRW_STATE_BASE_ADDRESS | 4);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
		ADVANCE_BATCH();
	}
850
}
851

852 853
static void pipeline_select()
{
854 855 856 857
	BATCH_LOCALS;
	BEGIN_BATCH(1);
	OUT_BATCH(NEW_PIPELINE_SELECT | PIPELINE_SELECT_MEDIA);
	ADVANCE_BATCH();
858
}
859 860

static void media_state_pointers(int vfe_mode)
861
{
862 863 864 865 866 867
	BATCH_LOCALS;
	BEGIN_BATCH(3);
	OUT_BATCH(BRW_MEDIA_STATE_POINTERS | 1);
	if (vfe_mode == VFE_VLD_MODE)
		OUT_RELOC(media_state.vld_state.bo, I915_GEM_DOMAIN_INSTRUCTION,
			  0, 1);
868 869
	else
		OUT_BATCH(0);
870 871
	OUT_RELOC(media_state.vfe_state.bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 0);
	ADVANCE_BATCH();
872
}
873

874 875
static void align_urb_fence()
{
876 877 878 879 880 881 882 883 884 885 886
	BATCH_LOCALS;
	int i, offset_to_next_cacheline;
	unsigned long batch_offset;
	BEGIN_BATCH(3);
	batch_offset = (void *)batch_ptr - xvmc_driver->alloc.ptr;
	offset_to_next_cacheline = ALIGN(batch_offset, 64) - batch_offset;
	if (offset_to_next_cacheline <= 12 && offset_to_next_cacheline != 0) {
		for (i = 0; i < offset_to_next_cacheline / 4; i++)
			OUT_BATCH(0);
		ADVANCE_BATCH();
	}
887
}
888

889 890
static void urb_layout()
{
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
	BATCH_LOCALS;
	align_urb_fence();
	BEGIN_BATCH(3);
	OUT_BATCH(BRW_URB_FENCE |
		  UF0_VFE_REALLOC |
		  UF0_CS_REALLOC |
		  UF0_SF_REALLOC |
		  UF0_CLIP_REALLOC | UF0_GS_REALLOC | UF0_VS_REALLOC | 1);

	OUT_BATCH((0 << UF1_CLIP_FENCE_SHIFT) |
		  (0 << UF1_GS_FENCE_SHIFT) | (0 << UF1_VS_FENCE_SHIFT));

	OUT_BATCH((0 << UF2_CS_FENCE_SHIFT) | (0 << UF2_SF_FENCE_SHIFT) | ((URB_SIZE - CS_SIZE - 1) << UF2_VFE_FENCE_SHIFT) |	/* VFE_SIZE */
		  ((URB_SIZE) << UF2_CS_FENCE_SHIFT));	/* CS_SIZE */
	ADVANCE_BATCH();
906 907 908 909
}

static void cs_urb_layout()
{
910 911 912 913 914 915
	BATCH_LOCALS;
	BEGIN_BATCH(2);
	OUT_BATCH(BRW_CS_URB_STATE | 0);
	OUT_BATCH((CS_SIZE << 4) |	/* URB Entry Allocation Size */
		  (1 << 0));	/* Number of URB Entries */
	ADVANCE_BATCH();
916 917 918 919
}

static void cs_buffer()
{
920 921 922 923 924 925
	BATCH_LOCALS;
	BEGIN_BATCH(2);
	OUT_BATCH(BRW_CONSTANT_BUFFER | 0 | (1 << 8));
	OUT_RELOC(media_state.cs_object.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
		  CS_SIZE);
	ADVANCE_BATCH();
926 927
}

928
/* kick media object to gpu in idct mode*/
929 930
static void send_media_object(XvMCMacroBlock * mb, dri_bo * bo,
			      uint32_t offset, enum interface interface)
931
{
932 933 934 935 936
	BATCH_LOCALS;
	BEGIN_BATCH(13);
	OUT_BATCH(BRW_MEDIA_OBJECT | 11);
	OUT_BATCH(interface);
	OUT_BATCH(6 * 128);
937
	OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, 0, offset);
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956

	OUT_BATCH(mb->x << 4);
	OUT_BATCH(mb->y << 4);
	OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, 0, offset);
	OUT_BATCH_SHORT(mb->coded_block_pattern);
	OUT_BATCH_SHORT(mb->PMV[0][0][0]);
	OUT_BATCH_SHORT(mb->PMV[0][0][1]);
	OUT_BATCH_SHORT(mb->PMV[0][1][0]);
	OUT_BATCH_SHORT(mb->PMV[0][1][1]);

	OUT_BATCH_SHORT(mb->PMV[1][0][0]);
	OUT_BATCH_SHORT(mb->PMV[1][0][1]);
	OUT_BATCH_SHORT(mb->PMV[1][1][0]);
	OUT_BATCH_SHORT(mb->PMV[1][1][1]);
	OUT_BATCH_CHAR(mb->dct_type);
	OUT_BATCH_CHAR(mb->motion_vertical_field_select);

	OUT_BATCH(0xffffffff);
	ADVANCE_BATCH();
957 958 959
}

/* kick media object to gpu in vld mode*/
960 961 962 963
static void vld_send_media_object(dri_bo * bo,
				  int slice_len, int mb_h_pos, int mb_v_pos,
				  int mb_bit_offset, int mb_count,
				  int q_scale_code)
964
{
965 966 967 968 969 970 971 972 973 974
	BATCH_LOCALS;
	BEGIN_BATCH(6);
	OUT_BATCH(BRW_MEDIA_OBJECT | 4);
	OUT_BATCH(0);
	OUT_BATCH(slice_len);
	OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 0);
	OUT_BATCH((mb_h_pos << 24) | (mb_v_pos << 16) | (mb_count << 8) |
		  (mb_bit_offset));
	OUT_BATCH(q_scale_code << 24);
	ADVANCE_BATCH();
975
}
Li Shao Hua's avatar
Li Shao Hua committed
976

977 978
static Status put_slice2(Display * display, XvMCContext * context,
			 unsigned char *slice, int nbytes, int sliceCode)
979
{
980
	unsigned int bit_buf;
981 982
	intel_xvmc_context_ptr intel_ctx = context->privData;
	struct intel_xvmc_hw_context *hw_ctx = intel_ctx->hw;
983 984 985 986 987 988 989 990 991
	int q_scale_code, mb_row;

	mb_row = *(slice - 1) - 1;
	bit_buf =
	    (slice[0] << 24) | (slice[1] << 16) | (slice[2] << 8) | (slice[3]);

	q_scale_code = bit_buf >> 27;

	if (media_state.slice_data.bo) {
992
		drm_intel_gem_bo_unmap_gtt(media_state.slice_data.bo);
993 994 995 996 997 998 999 1000

		drm_intel_bo_unreference(media_state.slice_data.bo);
	}
	media_state.slice_data.bo = drm_intel_bo_alloc(xvmc_driver->bufmgr,
						       "slice data",
						       VLD_MAX_SLICE_SIZE, 64);
	if (!media_state.slice_data.bo)
		return BadAlloc;
1001
	drm_intel_gem_bo_map_gtt(media_state.slice_data.bo);
1002 1003 1004 1005

	memcpy(media_state.slice_data.bo->virtual, slice, nbytes);

	LOCK_HARDWARE(intel_ctx->hw_context);
1006
	state_base_address(hw_ctx);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	pipeline_select();
	media_state_pointers(VFE_VLD_MODE);
	urb_layout();
	cs_urb_layout();
	cs_buffer();
	vld_send_media_object(media_state.slice_data.bo,
			      nbytes, 0, mb_row, 6, 127, q_scale_code);
	intelFlushBatch(TRUE);
	UNLOCK_HARDWARE(intel_ctx->hw_context);

	return Success;
1018 1019
}

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
static Status render_surface(Display * display,
			     XvMCContext * context,
			     unsigned int picture_structure,
			     XvMCSurface * target_surface,
			     XvMCSurface * past_surface,
			     XvMCSurface * future_surface,
			     unsigned int flags,
			     unsigned int num_macroblocks,
			     unsigned int first_macroblock,
			     XvMCMacroBlockArray * macroblock_array,
			     XvMCBlockArray * blocks)
1031
{
1032
	struct intel_xvmc_surface *priv_target, *priv_past, *priv_future;
1033 1034 1035 1036 1037 1038
	intel_xvmc_context_ptr intel_ctx;
	XvMCMacroBlock *mb;
	Status ret;
	unsigned short *block_ptr;
	int i, j;
	int block_offset = 0;
1039
	struct intel_xvmc_hw_context *hw_ctx;
1040

1041
	intel_ctx = context->privData;
1042

1043
	hw_ctx = (struct intel_xvmc_hw_context *)context->privData;
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	priv_target = target_surface->privData;
	priv_past = past_surface ? past_surface->privData : NULL;
	priv_future = future_surface ? future_surface->privData : NULL;

	ret = setup_surface(priv_target, priv_past, priv_future,
			    context->width, context->height);
	if (ret != Success)
		return ret;
	ret = binding_tables();
	if (ret != Success)
		return ret;
	ret = interface_descriptor();
	if (ret != Success)
		return ret;
	ret = cs_init(INTERFACE_NUM);
	if (ret != Success)
		return ret;
	ret = vfe_state(VFE_GENERIC_MODE);
	if (ret != Success)
		return ret;
1064

1065
	if (media_state.mb_data.bo) {
1066
		drm_intel_gem_bo_unmap_gtt(media_state.mb_data.bo);
1067 1068

		drm_intel_bo_unreference(media_state.mb_data.bo);
1069
	}
1070 1071 1072 1073 1074 1075 1076 1077
	unsigned int block_num =
	    (((context->width + 15) >> 4) * ((context->height + 15) >> 4));
	unsigned int surface_size = (64 * sizeof(short) * 6 * block_num);
	media_state.mb_data.bo = drm_intel_bo_alloc(xvmc_driver->bufmgr,
						    "macroblock data",
						    surface_size, 64);
	if (!media_state.mb_data.bo)
		return BadAlloc;
1078
	drm_intel_gem_bo_map_gtt(media_state.mb_data.bo);
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

	block_ptr = media_state.mb_data.bo->virtual;
	unsigned short *mb_block_ptr;
	for (i = first_macroblock; i < num_macroblocks + first_macroblock; i++) {
		mb = &macroblock_array->macro_blocks[i];
		mb_block_ptr = &blocks->blocks[(mb->index << 6)];

		if (mb->coded_block_pattern & 0x20) {
			for (j = 0; j < 8; j++)
				memcpy(block_ptr + 16 * j, mb_block_ptr + 8 * j,
				       16);
			mb_block_ptr += 64;
		}
		if (mb->coded_block_pattern & 0x10) {
			for (j = 0; j < 8; j++)
				memcpy(block_ptr + 16 * j + 8,
				       mb_block_ptr + 8 * j, 16);
			mb_block_ptr += 64;
		}
1098

1099 1100 1101 1102 1103 1104
		block_ptr += 2 * 64;
		if (mb->coded_block_pattern & 0x08) {
			for (j = 0; j < 8; j++)
				memcpy(block_ptr + 16 * j, mb_block_ptr + 8 * j,
				       16);
			mb_block_ptr += 64;
1105
		}
1106 1107 1108 1109 1110
		if (mb->coded_block_pattern & 0x04) {
			for (j = 0; j < 8; j++)
				memcpy(block_ptr + 16 * j + 8,
				       mb_block_ptr + 8 * j, 16);
			mb_block_ptr += 64;
1111
		}
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

		block_ptr += 2 * 64;
		if (mb->coded_block_pattern & 0x2) {
			memcpy(block_ptr, mb_block_ptr, 128);
			mb_block_ptr += 64;
		}

		block_ptr += 64;
		if (mb->coded_block_pattern & 0x1)
			memcpy(block_ptr, mb_block_ptr, 128);
		block_ptr += 64;
1123
	}
1124 1125

	LOCK_HARDWARE(intel_ctx->hw_context);
1126
	state_base_address(hw_ctx);
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	flush();
	pipeline_select();
	urb_layout();
	media_state_pointers(VFE_GENERIC_MODE);
	cs_urb_layout();
	cs_buffer();
	for (i = first_macroblock;
	     i < num_macroblocks + first_macroblock;
	     i++, block_offset += 128 * 6) {
		mb = &macroblock_array->macro_blocks[i];

		if (mb->macroblock_type & XVMC_MB_TYPE_INTRA) {
			send_media_object(mb, media_state.mb_data.bo,
					  block_offset,
					  FRAME_INTRA + INTERFACE_NUM);
		} else {
			if (((mb->motion_type & 3) == XVMC_PREDICTION_FRAME)) {
				if ((mb->macroblock_type &
				     XVMC_MB_TYPE_MOTION_FORWARD)) {
					if ((mb->macroblock_type &
					     XVMC_MB_TYPE_MOTION_BACKWARD)) {
						send_media_object(mb,
								  media_state.mb_data.
								  bo,
								  block_offset,
								  FRAME_FRAME_PRED_BIDIRECT
								  +
								  INTERFACE_NUM);
					} else {
						send_media_object(mb,
								  media_state.mb_data.
								  bo,
								  block_offset,
								  FRAME_FRAME_PRED_FORWARD
								  +
								  INTERFACE_NUM);
					}
				} else
				    if ((mb->macroblock_type &
					 XVMC_MB_TYPE_MOTION_BACKWARD)) {
					send_media_object(mb,
							  media_state.
							  mb_data.bo,
							  block_offset,
							  FRAME_FRAME_PRED_BACKWARD
							  + INTERFACE_NUM);
				}
			} else if ((mb->motion_type & 3) ==
				   XVMC_PREDICTION_FIELD) {
				if ((mb->macroblock_type &
				     XVMC_MB_TYPE_MOTION_FORWARD)) {
					if (((mb->macroblock_type &
					      XVMC_MB_TYPE_MOTION_BACKWARD))) {
						send_media_object(mb,
								  media_state.mb_data.
								  bo,
								  block_offset,
								  FRAME_FIELD_PRED_BIDIRECT
								  +
								  INTERFACE_NUM);
					} else {
						send_media_object(mb,
								  media_state.mb_data.
								  bo,
								  block_offset,
								  FRAME_FIELD_PRED_FORWARD
								  +
								  INTERFACE_NUM);
					}
				} else
				    if ((mb->macroblock_type &
					 XVMC_MB_TYPE_MOTION_BACKWARD)) {
					send_media_object(mb,
							  media_state.
							  mb_data.bo,
							  block_offset,
							  FRAME_FIELD_PRED_BACKWARD
							  + INTERFACE_NUM);
				}
			} else {
				send_media_object(mb, media_state.mb_data.bo, block_offset, FRAME_FIELD_PRED_BIDIRECT + INTERFACE_NUM);	/*dual prime */
			}
		}
	}
	intelFlushBatch(TRUE);
	UNLOCK_HARDWARE(intel_ctx->hw_context);
	return Success;
1214 1215
}

1216
struct _intel_xvmc_driver xvmc_vld_driver = {
1217 1218 1219 1220 1221
	.type = XVMC_I965_MPEG2_VLD,
	.create_context = create_context,
	.destroy_context = destroy_context,
	.load_qmatrix = load_qmatrix,
	.begin_surface = begin_surface,
1222
	.render_surface = render_surface,
1223 1224
	.put_slice = put_slice,
	.put_slice2 = put_slice2
1225
};