summaryrefslogtreecommitdiff
path: root/i965_avc_ildb.c
blob: 19146ac9c33572ba46325bf1470c6d053af8f2b4 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
259
260
261
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
 * Copyright © 2010 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, sub license, 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
 *
 * Authors:
 *    Xiang Haihao <haihao.xiang@intel.com>
 *
 */

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "va_backend.h"

#include "intel_batchbuffer.h"
#include "intel_driver.h"

#include "i965_defines.h"
#include "i965_drv_video.h"
#include "i965_avc_ildb.h"
#include "i965_media_h264.h"
#include "i965_media.h"

/* On Cantiga */
#include "shaders/h264/mc/export.inc"

/* On Ironlake */
#include "shaders/h264/mc/export.inc.gen5"

#define PICTURE_FRAME   0
#define PICTURE_FIELD   1
#define PICTURE_MBAFF   2

enum {
    AVC_ILDB_ROOT_Y_ILDB_FRAME,
    AVC_ILDB_CHILD_Y_ILDB_FRAME,
    AVC_ILDB_ROOT_UV_ILDB_FRAME,
    AVC_ILDB_CHILD_UV_ILDB_FRAME,
    AVC_ILDB_ROOT_Y_ILDB_FIELD,
    AVC_ILDB_CHILD_Y_ILDB_FIELD,
    AVC_ILDB_ROOT_UV_ILDB_FIELD,
    AVC_ILDB_CHILD_UV_ILDB_FIELD,
    AVC_ILDB_ROOT_Y_ILDB_MBAFF,
    AVC_ILDB_CHILD_Y_ILDB_MBAFF,
    AVC_ILDB_ROOT_UV_ILDB_MBAFF,
    AVC_ILDB_CHILD_UV_ILDB_MBAFF
};

static unsigned long avc_ildb_kernel_offset_gen4[] = {
    AVC_ILDB_ROOT_Y_ILDB_FRAME_IP * INST_UNIT_GEN4,
    AVC_ILDB_CHILD_Y_ILDB_FRAME_IP * INST_UNIT_GEN4,
    AVC_ILDB_ROOT_UV_ILDB_FRAME_IP * INST_UNIT_GEN4,
    AVC_ILDB_CHILD_UV_ILDB_FRAME_IP * INST_UNIT_GEN4,
    AVC_ILDB_ROOT_Y_ILDB_FIELD_IP * INST_UNIT_GEN4,
    AVC_ILDB_CHILD_Y_ILDB_FIELD_IP * INST_UNIT_GEN4,
    AVC_ILDB_ROOT_UV_ILDB_FIELD_IP * INST_UNIT_GEN4,
    AVC_ILDB_CHILD_UV_ILDB_FIELD_IP * INST_UNIT_GEN4,
    AVC_ILDB_ROOT_Y_ILDB_MBAFF_IP * INST_UNIT_GEN4,
    AVC_ILDB_CHILD_Y_ILDB_MBAFF_IP * INST_UNIT_GEN4,
    AVC_ILDB_ROOT_UV_ILDB_MBAFF_IP * INST_UNIT_GEN4,
    AVC_ILDB_CHILD_UV_ILDB_MBAFF_IP * INST_UNIT_GEN4
};

static unsigned long avc_ildb_kernel_offset_gen5[] = {
    AVC_ILDB_ROOT_Y_ILDB_FRAME_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_CHILD_Y_ILDB_FRAME_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_ROOT_UV_ILDB_FRAME_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_CHILD_UV_ILDB_FRAME_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_ROOT_Y_ILDB_FIELD_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_CHILD_Y_ILDB_FIELD_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_ROOT_UV_ILDB_FIELD_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_CHILD_UV_ILDB_FIELD_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_ROOT_Y_ILDB_MBAFF_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_CHILD_Y_ILDB_MBAFF_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_ROOT_UV_ILDB_MBAFF_IP_GEN5 * INST_UNIT_GEN5,
    AVC_ILDB_CHILD_UV_ILDB_MBAFF_IP_GEN5 * INST_UNIT_GEN5
};

struct avc_ildb_root_input
{
    unsigned int blocks_per_row : 16;
    unsigned int blocks_per_column : 16;

    unsigned int picture_type : 16;
    unsigned int max_concurrent_threads : 16;

    unsigned int debug_field : 16;
    unsigned int mbaff_frame_flag : 1;
    unsigned int bottom_field_flag : 1;
    unsigned int control_data_expansion_flag : 1;
    unsigned int chroma_format : 1;
    unsigned int pad0 : 12;

    unsigned int ramp_constant_0;
    
    unsigned int ramp_constant_1;

    int constant_0 : 8;
    int constant_1 : 8;
    int pad1 : 16;

    unsigned int pad2;
    unsigned int pad3;
};

extern struct i965_kernel *h264_avc_kernels;

#define NUM_AVC_ILDB_INTERFACES ARRAY_ELEMS(avc_ildb_kernel_offset_gen4)
static unsigned long *avc_ildb_kernel_offset = NULL;

static void
i965_avc_ildb_surface_state(VADriverContextP ctx,
                            struct decode_state *decode_state,
                            struct i965_h264_context *i965_h264_context)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;
    struct i965_surface_state *ss;
    struct object_surface *obj_surface;
    VAPictureParameterBufferH264 *pic_param;
    VAPictureH264 *va_pic;
    dri_bo *bo;
    int i;

    assert(decode_state->pic_param && decode_state->pic_param->buffer);
    pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
    va_pic = &pic_param->CurrPic;
    assert(!(va_pic->flags & VA_PICTURE_H264_INVALID));
    obj_surface = SURFACE(va_pic->picture_id);
    assert(obj_surface);

    avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].s_bo = i965_h264_context->avc_ildb_data.bo;
    dri_bo_reference(avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].s_bo);
    avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].offset = 0;
    avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].surface_type = I965_SURFACE_BUFFER;
    avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].width = ((avc_ildb_context->mbs_per_picture * EDGE_CONTROL_DATA_IN_DWS - 1) & 0x7f);
    avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].height = (((avc_ildb_context->mbs_per_picture * EDGE_CONTROL_DATA_IN_DWS - 1) >> 7) & 0x1fff);
    avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].depth = (((avc_ildb_context->mbs_per_picture * EDGE_CONTROL_DATA_IN_DWS - 1) >> 20) & 0x7f);
    avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].pitch = EDGE_CONTROL_DATA_IN_BTYES - 1;
    avc_ildb_context->surface[SURFACE_EDGE_CONTROL_DATA].is_target = 0;
    
    avc_ildb_context->surface[SURFACE_SRC_Y].s_bo = obj_surface->bo;
    dri_bo_reference(avc_ildb_context->surface[SURFACE_SRC_Y].s_bo);
    avc_ildb_context->surface[SURFACE_SRC_Y].offset = 0;
    avc_ildb_context->surface[SURFACE_SRC_Y].surface_type = I965_SURFACE_2D;
    avc_ildb_context->surface[SURFACE_SRC_Y].format = I965_SURFACEFORMAT_R8_SINT;
    avc_ildb_context->surface[SURFACE_SRC_Y].width = obj_surface->width / 4 - 1;
    avc_ildb_context->surface[SURFACE_SRC_Y].height = obj_surface->height - 1;
    avc_ildb_context->surface[SURFACE_SRC_Y].depth = 0;
    avc_ildb_context->surface[SURFACE_SRC_Y].pitch = obj_surface->width - 1;
    avc_ildb_context->surface[SURFACE_SRC_Y].vert_line_stride = !!(va_pic->flags & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD));
    avc_ildb_context->surface[SURFACE_SRC_Y].vert_line_stride_ofs = !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
    avc_ildb_context->surface[SURFACE_SRC_Y].is_target = 0;
    
    avc_ildb_context->surface[SURFACE_SRC_UV].s_bo = obj_surface->bo;
    dri_bo_reference(avc_ildb_context->surface[SURFACE_SRC_UV].s_bo);
    avc_ildb_context->surface[SURFACE_SRC_UV].offset = obj_surface->width * obj_surface->height;
    avc_ildb_context->surface[SURFACE_SRC_UV].surface_type = I965_SURFACE_2D;
    avc_ildb_context->surface[SURFACE_SRC_UV].format = I965_SURFACEFORMAT_R8G8_SINT;
    avc_ildb_context->surface[SURFACE_SRC_UV].width = obj_surface->width / 4 - 1;
    avc_ildb_context->surface[SURFACE_SRC_UV].height = obj_surface->height / 2 - 1;
    avc_ildb_context->surface[SURFACE_SRC_UV].depth = 0;
    avc_ildb_context->surface[SURFACE_SRC_UV].pitch = obj_surface->width - 1;
    avc_ildb_context->surface[SURFACE_SRC_UV].vert_line_stride = !!(va_pic->flags & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD));
    avc_ildb_context->surface[SURFACE_SRC_UV].vert_line_stride_ofs = !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
    avc_ildb_context->surface[SURFACE_SRC_UV].is_target = 0;

    avc_ildb_context->surface[SURFACE_DEST_Y].s_bo = obj_surface->bo;
    dri_bo_reference(avc_ildb_context->surface[SURFACE_DEST_Y].s_bo);
    avc_ildb_context->surface[SURFACE_DEST_Y].offset = 0;
    avc_ildb_context->surface[SURFACE_DEST_Y].surface_type = I965_SURFACE_2D;
    avc_ildb_context->surface[SURFACE_DEST_Y].format = I965_SURFACEFORMAT_R8_SINT;
    avc_ildb_context->surface[SURFACE_DEST_Y].width = obj_surface->width / 4 - 1;
    avc_ildb_context->surface[SURFACE_DEST_Y].height = obj_surface->height - 1;
    avc_ildb_context->surface[SURFACE_DEST_Y].depth = 0;
    avc_ildb_context->surface[SURFACE_DEST_Y].pitch = obj_surface->width - 1;
    avc_ildb_context->surface[SURFACE_DEST_Y].vert_line_stride = !!(va_pic->flags & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD));
    avc_ildb_context->surface[SURFACE_DEST_Y].vert_line_stride_ofs = !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
    avc_ildb_context->surface[SURFACE_DEST_Y].is_target = 1;

    avc_ildb_context->surface[SURFACE_DEST_UV].s_bo = obj_surface->bo;
    dri_bo_reference(avc_ildb_context->surface[SURFACE_DEST_UV].s_bo);
    avc_ildb_context->surface[SURFACE_DEST_UV].offset = obj_surface->width * obj_surface->height;
    avc_ildb_context->surface[SURFACE_DEST_UV].surface_type = I965_SURFACE_2D;
    avc_ildb_context->surface[SURFACE_DEST_UV].format = I965_SURFACEFORMAT_R8G8_SINT;
    avc_ildb_context->surface[SURFACE_DEST_UV].width = obj_surface->width / 4 - 1;
    avc_ildb_context->surface[SURFACE_DEST_UV].height = obj_surface->height / 2 - 1;
    avc_ildb_context->surface[SURFACE_DEST_UV].depth = 0;
    avc_ildb_context->surface[SURFACE_DEST_UV].pitch = obj_surface->width - 1;
    avc_ildb_context->surface[SURFACE_DEST_UV].vert_line_stride = !!(va_pic->flags & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD));
    avc_ildb_context->surface[SURFACE_DEST_UV].vert_line_stride_ofs = !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
    avc_ildb_context->surface[SURFACE_DEST_UV].is_target = 1;

    for (i = 0; i < NUM_AVC_ILDB_SURFACES; i++) {
        bo = avc_ildb_context->surface[i].ss_bo;
        dri_bo_map(bo, 1);
        assert(bo->virtual);
        ss = bo->virtual;
        memset(ss, 0, sizeof(*ss));
        ss->ss0.surface_type = avc_ildb_context->surface[i].surface_type;
        ss->ss0.surface_format = avc_ildb_context->surface[i].format;
        ss->ss0.vert_line_stride = avc_ildb_context->surface[i].vert_line_stride;
        ss->ss0.vert_line_stride_ofs = avc_ildb_context->surface[i].vert_line_stride_ofs;
        ss->ss1.base_addr = avc_ildb_context->surface[i].s_bo->offset + avc_ildb_context->surface[i].offset;
        ss->ss2.width = avc_ildb_context->surface[i].width;
        ss->ss2.height = avc_ildb_context->surface[i].height;
        ss->ss3.depth = avc_ildb_context->surface[i].depth;
        ss->ss3.pitch = avc_ildb_context->surface[i].pitch;
        dri_bo_emit_reloc(bo,
                          I915_GEM_DOMAIN_RENDER, 
                          avc_ildb_context->surface[i].is_target ? I915_GEM_DOMAIN_RENDER : 0,
                          avc_ildb_context->surface[i].offset,
                          offsetof(struct i965_surface_state, ss1),
                          avc_ildb_context->surface[i].s_bo);
        dri_bo_unmap(bo);
    }
}

static void
i965_avc_ildb_binding_table(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;
    unsigned int *binding_table;
    dri_bo *bo = avc_ildb_context->binding_table.bo;
    int i;

    dri_bo_map(bo, 1);
    assert(bo->virtual);
    binding_table = bo->virtual;
    memset(binding_table, 0, bo->size);

    for (i = 0; i < NUM_AVC_ILDB_SURFACES; i++) {
        binding_table[i] = avc_ildb_context->surface[i].ss_bo->offset;
        dri_bo_emit_reloc(bo,
                          I915_GEM_DOMAIN_INSTRUCTION, 0,
                          0,
                          i * sizeof(*binding_table),
                          avc_ildb_context->surface[i].ss_bo);
    }

    dri_bo_unmap(bo);
}

static void
i965_avc_ildb_interface_descriptor_table(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;
    struct i965_interface_descriptor *desc;
    dri_bo *bo;
    int i;

    bo = avc_ildb_context->idrt.bo;
    dri_bo_map(bo, 1);
    assert(bo->virtual);
    desc = bo->virtual;

    for (i = 0; i < NUM_AVC_ILDB_INTERFACES; i++) {
        int kernel_offset = avc_ildb_kernel_offset[i];
        memset(desc, 0, sizeof(*desc));
        desc->desc0.grf_reg_blocks = 7; 
        desc->desc0.kernel_start_pointer = (h264_avc_kernels[H264_AVC_COMBINED].bo->offset + kernel_offset) >> 6; /* reloc */
        desc->desc1.const_urb_entry_read_offset = 0;
        desc->desc1.const_urb_entry_read_len = ((i == AVC_ILDB_ROOT_Y_ILDB_FRAME ||
                                                 i == AVC_ILDB_ROOT_Y_ILDB_FIELD ||
                                                 i == AVC_ILDB_ROOT_Y_ILDB_MBAFF) ? 1 : 0);
        desc->desc3.binding_table_entry_count = 0;
        desc->desc3.binding_table_pointer = 
            avc_ildb_context->binding_table.bo->offset >> 5; /*reloc */

        dri_bo_emit_reloc(bo,
                          I915_GEM_DOMAIN_INSTRUCTION, 0,
                          desc->desc0.grf_reg_blocks + kernel_offset,
                          i * sizeof(*desc) + offsetof(struct i965_interface_descriptor, desc0),
                          h264_avc_kernels[H264_AVC_COMBINED].bo);

        dri_bo_emit_reloc(bo,
                          I915_GEM_DOMAIN_INSTRUCTION, 0,
                          desc->desc3.binding_table_entry_count,
                          i * sizeof(*desc) + offsetof(struct i965_interface_descriptor, desc3),
                          avc_ildb_context->binding_table.bo);
        desc++;
    }

    dri_bo_unmap(bo);
}

static void
i965_avc_ildb_vfe_state(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;
    struct i965_vfe_state *vfe_state;
    dri_bo *bo;

    bo = avc_ildb_context->vfe_state.bo;
    dri_bo_map(bo, 1);
    assert(bo->virtual);
    vfe_state = bo->virtual;
    memset(vfe_state, 0, sizeof(*vfe_state));
    vfe_state->vfe1.max_threads = 0;
    vfe_state->vfe1.urb_entry_alloc_size = avc_ildb_context->urb.size_vfe_entry - 1;
    vfe_state->vfe1.num_urb_entries = avc_ildb_context->urb.num_vfe_entries;
    vfe_state->vfe1.vfe_mode = VFE_GENERIC_MODE;
    vfe_state->vfe1.children_present = 1;
    vfe_state->vfe2.interface_descriptor_base = 
        avc_ildb_context->idrt.bo->offset >> 4; /* reloc */
    dri_bo_emit_reloc(bo,
                      I915_GEM_DOMAIN_INSTRUCTION, 0,
                      0,
                      offsetof(struct i965_vfe_state, vfe2),
                      avc_ildb_context->idrt.bo);
    dri_bo_unmap(bo);
}

static void
i965_avc_ildb_upload_constants(VADriverContextP ctx,
                               struct decode_state *decode_state,
                               struct i965_h264_context *i965_h264_context)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;
    VAPictureParameterBufferH264 *pic_param;
    struct avc_ildb_root_input *root_input;

    assert(decode_state->pic_param && decode_state->pic_param->buffer);
    pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;

    dri_bo_map(avc_ildb_context->curbe.bo, 1);
    assert(avc_ildb_context->curbe.bo->virtual);
    root_input = avc_ildb_context->curbe.bo->virtual;

    if (IS_IRONLAKE(i965->intel.device_id)) {
        root_input->max_concurrent_threads = 76; /* 72 - 2 + 8 - 2 */
    } else {
        root_input->max_concurrent_threads = 54; /* 50 - 2 + 8 - 2 */
    }

    if (pic_param->pic_fields.bits.field_pic_flag)
        root_input->picture_type = PICTURE_FIELD;
    else {
        if (pic_param->seq_fields.bits.mb_adaptive_frame_field_flag)
            root_input->picture_type = PICTURE_MBAFF;
        else
            root_input->picture_type = PICTURE_FRAME;
    }

    avc_ildb_context->picture_type = root_input->picture_type;
    root_input->blocks_per_row = pic_param->picture_width_in_mbs_minus1 + 1;
    root_input->blocks_per_column = (pic_param->picture_height_in_mbs_minus1 + 1) / 
        (1 + (root_input->picture_type != PICTURE_FRAME));
    avc_ildb_context->mbs_per_picture = (pic_param->picture_width_in_mbs_minus1 + 1) *
        (pic_param->picture_height_in_mbs_minus1 + 1);
    
    root_input->mbaff_frame_flag = (root_input->picture_type == PICTURE_MBAFF);
    root_input->bottom_field_flag = !!(pic_param->CurrPic.flags & VA_PICTURE_H264_BOTTOM_FIELD);
    root_input->control_data_expansion_flag = 1; /* Always 1 on G4x+ */
    root_input->chroma_format = (pic_param->seq_fields.bits.chroma_format_idc != 1); /* 0=4:0:0, 1=4:2:0 */
    
    root_input->ramp_constant_0 = 0x03020100;
    
    root_input->ramp_constant_1 = 0x07060504;

    root_input->constant_0 = -2;
    root_input->constant_1 = 1;

    dri_bo_unmap(avc_ildb_context->curbe.bo);
}

static void
i965_avc_ildb_states_setup(VADriverContextP ctx,
                           struct decode_state *decode_state,
                           struct i965_h264_context *i965_h264_context)
{
    i965_avc_ildb_surface_state(ctx, decode_state, i965_h264_context);
    i965_avc_ildb_binding_table(ctx, i965_h264_context);
    i965_avc_ildb_interface_descriptor_table(ctx, i965_h264_context);
    i965_avc_ildb_vfe_state(ctx, i965_h264_context);
    i965_avc_ildb_upload_constants(ctx, decode_state, i965_h264_context);
}

static void
i965_avc_ildb_pipeline_select(VADriverContextP ctx)
{
    BEGIN_BATCH(ctx, 1);
    OUT_BATCH(ctx, CMD_PIPELINE_SELECT | PIPELINE_SELECT_MEDIA);
    ADVANCE_BATCH(ctx);
}

static void
i965_avc_ildb_urb_layout(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;

    unsigned int vfe_fence, cs_fence;

    vfe_fence = avc_ildb_context->urb.cs_start;
    cs_fence = URB_SIZE((&i965->intel));

    BEGIN_BATCH(ctx, 3);
    OUT_BATCH(ctx, CMD_URB_FENCE | UF0_VFE_REALLOC | UF0_CS_REALLOC | 1);
    OUT_BATCH(ctx, 0);
    OUT_BATCH(ctx, 
              (vfe_fence << UF2_VFE_FENCE_SHIFT) |      /* VFE_SIZE */
              (cs_fence << UF2_CS_FENCE_SHIFT));        /* CS_SIZE */
    ADVANCE_BATCH(ctx);
}

static void
i965_avc_ildb_state_base_address(VADriverContextP ctx)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx); 

    if (IS_IRONLAKE(i965->intel.device_id)) {
        BEGIN_BATCH(ctx, 8);
        OUT_BATCH(ctx, CMD_STATE_BASE_ADDRESS | 6);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        ADVANCE_BATCH(ctx);
    } else {
        BEGIN_BATCH(ctx, 6);
        OUT_BATCH(ctx, CMD_STATE_BASE_ADDRESS | 4);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        OUT_BATCH(ctx, 0 | BASE_ADDRESS_MODIFY);
        ADVANCE_BATCH(ctx);
    }
}

static void
i965_avc_ildb_state_pointers(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;

    BEGIN_BATCH(ctx, 3);
    OUT_BATCH(ctx, CMD_MEDIA_STATE_POINTERS | 1);
    OUT_BATCH(ctx, 0);
    OUT_RELOC(ctx, avc_ildb_context->vfe_state.bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 0);
    ADVANCE_BATCH(ctx);
}

static void 
i965_avc_ildb_cs_urb_layout(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;

    BEGIN_BATCH(ctx, 2);
    OUT_BATCH(ctx, CMD_CS_URB_STATE | 0);
    OUT_BATCH(ctx,
              ((avc_ildb_context->urb.size_cs_entry - 1) << 4) |     /* URB Entry Allocation Size */
              (avc_ildb_context->urb.num_cs_entries << 0));          /* Number of URB Entries */
    ADVANCE_BATCH(ctx);
}

static void
i965_avc_ildb_constant_buffer(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;

    BEGIN_BATCH(ctx, 2);
    OUT_BATCH(ctx, CMD_CONSTANT_BUFFER | (1 << 8) | (2 - 2));
    OUT_RELOC(ctx, avc_ildb_context->curbe.bo,
              I915_GEM_DOMAIN_INSTRUCTION, 0,
              avc_ildb_context->urb.size_cs_entry - 1);
    ADVANCE_BATCH(ctx);    
}

static void
i965_avc_ildb_objects(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;

    BEGIN_BATCH(ctx, 6);
    OUT_BATCH(ctx, CMD_MEDIA_OBJECT | 4);

    switch (avc_ildb_context->picture_type) {
    case PICTURE_FRAME:
        OUT_BATCH(ctx, AVC_ILDB_ROOT_Y_ILDB_FRAME);
        break;

    case PICTURE_FIELD:
        OUT_BATCH(ctx, AVC_ILDB_ROOT_Y_ILDB_FIELD);
        break;

    case PICTURE_MBAFF:
        OUT_BATCH(ctx, AVC_ILDB_ROOT_Y_ILDB_MBAFF);
        break;

    default:
        assert(0);
        OUT_BATCH(ctx, 0);
        break;
    }

    OUT_BATCH(ctx, 0); /* no indirect data */
    OUT_BATCH(ctx, 0);
    OUT_BATCH(ctx, 0);
    OUT_BATCH(ctx, 0);
    ADVANCE_BATCH(ctx);
}

static void
i965_avc_ildb_pipeline_setup(VADriverContextP ctx, struct i965_h264_context *i965_h264_context)
{
    intel_batchbuffer_emit_mi_flush(ctx);
    i965_avc_ildb_pipeline_select(ctx);
    i965_avc_ildb_state_base_address(ctx);
    i965_avc_ildb_state_pointers(ctx, i965_h264_context);
    i965_avc_ildb_urb_layout(ctx, i965_h264_context);
    i965_avc_ildb_cs_urb_layout(ctx, i965_h264_context);
    i965_avc_ildb_constant_buffer(ctx, i965_h264_context);
    i965_avc_ildb_objects(ctx, i965_h264_context);
}

void
i965_avc_ildb(VADriverContextP ctx, struct decode_state *decode_state, void *h264_context)
{
    struct i965_h264_context *i965_h264_context = (struct i965_h264_context *)h264_context;

    if (i965_h264_context->enable_avc_ildb) {
        i965_avc_ildb_states_setup(ctx, decode_state, i965_h264_context);
        i965_avc_ildb_pipeline_setup(ctx, i965_h264_context);
    }
}

void
i965_avc_ildb_decode_init(VADriverContextP ctx, void *h264_context)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct i965_h264_context *i965_h264_context = (struct i965_h264_context *)h264_context;
    struct i965_avc_ildb_context *avc_ildb_context = &i965_h264_context->avc_ildb_context;;
    dri_bo *bo;
    int i;

    dri_bo_unreference(avc_ildb_context->curbe.bo);
    bo = dri_bo_alloc(i965->intel.bufmgr,
                      "constant buffer",
                      4096, 64);
    assert(bo);
    avc_ildb_context->curbe.bo = bo;

    dri_bo_unreference(avc_ildb_context->binding_table.bo);
    bo = dri_bo_alloc(i965->intel.bufmgr, 
                      "binding table",
                      NUM_AVC_ILDB_SURFACES * sizeof(unsigned int), 32);
    assert(bo);
    avc_ildb_context->binding_table.bo = bo;

    dri_bo_unreference(avc_ildb_context->idrt.bo);
    bo = dri_bo_alloc(i965->intel.bufmgr, 
                      "interface discriptor", 
                      NUM_AVC_ILDB_INTERFACES * sizeof(struct i965_interface_descriptor), 16);
    assert(bo);
    avc_ildb_context->idrt.bo = bo;

    dri_bo_unreference(avc_ildb_context->vfe_state.bo);
    bo = dri_bo_alloc(i965->intel.bufmgr, 
                      "vfe state", 
                      sizeof(struct i965_vfe_state), 32);
    assert(bo);
    avc_ildb_context->vfe_state.bo = bo;

    avc_ildb_context->urb.num_vfe_entries = 1;
    avc_ildb_context->urb.size_vfe_entry = 640;
    avc_ildb_context->urb.num_cs_entries = 1;
    avc_ildb_context->urb.size_cs_entry = 1;
    avc_ildb_context->urb.vfe_start = 0;
    avc_ildb_context->urb.cs_start = avc_ildb_context->urb.vfe_start + 
        avc_ildb_context->urb.num_vfe_entries * avc_ildb_context->urb.size_vfe_entry;
    assert(avc_ildb_context->urb.cs_start + 
           avc_ildb_context->urb.num_cs_entries * avc_ildb_context->urb.size_cs_entry <= URB_SIZE((&i965->intel)));

    for (i = 0; i < NUM_AVC_ILDB_SURFACES; i++) {
        dri_bo_unreference(avc_ildb_context->surface[i].s_bo);
        avc_ildb_context->surface[i].s_bo = NULL;

        dri_bo_unreference(avc_ildb_context->surface[i].ss_bo);
        bo = dri_bo_alloc(i965->intel.bufmgr, 
                          "surface state", 
                          sizeof(struct i965_surface_state), 32);
        assert(bo);
        avc_ildb_context->surface[i].ss_bo = bo;
    }

    /* kernel offset */
    assert(NUM_AVC_ILDB_INTERFACES == ARRAY_ELEMS(avc_ildb_kernel_offset_gen5));

    if (IS_IRONLAKE(i965->intel.device_id)) {
        avc_ildb_kernel_offset = avc_ildb_kernel_offset_gen5;
    } else {
        avc_ildb_kernel_offset = avc_ildb_kernel_offset_gen4;
    }
}

Bool 
i965_avc_ildb_ternimate(struct i965_avc_ildb_context *avc_ildb_context)
{
    int i;

    dri_bo_unreference(avc_ildb_context->curbe.bo);
    avc_ildb_context->curbe.bo = NULL;

    dri_bo_unreference(avc_ildb_context->binding_table.bo);
    avc_ildb_context->binding_table.bo = NULL;

    dri_bo_unreference(avc_ildb_context->idrt.bo);
    avc_ildb_context->idrt.bo = NULL;

    dri_bo_unreference(avc_ildb_context->vfe_state.bo);
    avc_ildb_context->vfe_state.bo = NULL;

    for (i = 0; i < NUM_AVC_ILDB_SURFACES; i++) {
        dri_bo_unreference(avc_ildb_context->surface[i].ss_bo);
        avc_ildb_context->surface[i].ss_bo = NULL;

        dri_bo_unreference(avc_ildb_context->surface[i].s_bo);
        avc_ildb_context->surface[i].s_bo = NULL;
    }

    return True;
}